-
Notifications
You must be signed in to change notification settings - Fork 2
/
emit.ml
384 lines (352 loc) · 9.45 KB
/
emit.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
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
open Error
open Exe
open Lambda
open Instruction
open Opcode
open Syntax
(* code buffer *)
let out_buf = ref (Bytes.create 256)
let out_pos = ref 0
let o b =
let len = Bytes.length !out_buf in
if !out_pos >= len then (
let newbuf = Bytes.create (len*2) in
Bytes.blit !out_buf 0 newbuf 0 len;
out_buf := newbuf
);
Bytes.set !out_buf !out_pos (b land 255 |> char_of_int);
incr out_pos
let oo w =
if w < -32768 || 32767 < w then
displacement_overflow ()
else (
o w;
o (w lsr 8)
)
let oooo w =
oo (w land 65535);
oo (w lsr 16 land 65535)
let out_test_int = function
| Peq -> opEQ
| Pneq -> opNEQ
| Plt -> opLTINT
| Ple -> opLEINT
| Pgt -> opGTINT
| Pge -> opGEINT
| _ -> assert false
let out_test_int_b = function
| Peq -> opBRANCHIFEQ
| Pneq -> opBRANCHIFNEQ
| Plt -> opBRANCHIFLT
| Ple -> opBRANCHIFLE
| Pgt -> opBRANCHIFGT
| Pge -> opBRANCHIFGE
| _ -> assert false
let out_test_float = function
| Peq -> opEQFLOAT
| Pneq -> opNEQFLOAT
| Plt -> opLTFLOAT
| Ple -> opLEFLOAT
| Pgt -> opGTFLOAT
| Pge -> opGEFLOAT
| _ -> assert false
let out_test_string = function
| Peq -> opEQSTRING
| Pneq -> opNEQSTRING
| Plt -> opLTSTRING
| Ple -> opLESTRING
| Pgt -> opGTSTRING
| Pge -> opGESTRING
| _ -> assert false
(* label *)
type label_def =
| Label_defined of int
| Label_undefined of (int * int) list
let label_tbl = ref [||]
let extend_label_tbl l =
let len = Array.length !label_tbl in
let newtbl = Array.make ((l/len+1)*len) (Label_undefined []) in
for i = 0 to len-1 do
newtbl.(i) <- (!label_tbl).(i)
done;
label_tbl := newtbl
let define_label l =
if l >= Array.length !label_tbl then
extend_label_tbl l;
match (!label_tbl).(l) with
| Label_defined _ ->
fatal_error "define_label: already defined"
| Label_undefined ls ->
let curr_pos = !out_pos in
(!label_tbl).(l) <- Label_defined curr_pos;
List.iter (fun (pos,orig) ->
out_pos := pos;
oo (curr_pos-orig)
) ls;
out_pos := curr_pos
(* orig must be aligned by 2 *)
let out_label_with_orig orig l =
if l >= Array.length !label_tbl then
extend_label_tbl l;
match (!label_tbl).(l) with
| Label_defined pos ->
oo (pos-orig)
| Label_undefined ls ->
(!label_tbl).(l) <- Label_undefined ((!out_pos,orig)::ls);
oo 0
let out_label l =
if !out_pos mod 2 <> 0 then
o 0;
out_label_with_orig !out_pos l
(* relocation *)
type reloc_entry =
| Reloc_const of constant
| Reloc_getglobal of long_ident
| Reloc_setglobal of long_ident
| Reloc_prim of string
| Reloc_tag of long_ident * int
let relocs = ref []
let enter_reloc info =
relocs := (!out_pos, info) :: !relocs
let slot_for_prim name =
enter_reloc (Reloc_prim name);
o 0
let slot_for_tag (id,stamp) =
enter_reloc (Reloc_tag(id,stamp));
o 0
let out_tag = function
| Constr_tag_regular(n,t) ->
o t
| Constr_tag_extensible(id,stamp) ->
slot_for_tag (id,stamp)
let slot_for_const c =
if !out_pos mod 2 <> 0 then
o 0;
enter_reloc (Reloc_const c);
oo 0
let slot_for_getglobal id =
if !out_pos mod 2 <> 0 then
o 0;
enter_reloc (Reloc_getglobal id);
oo 0
let slot_for_setglobal id =
if !out_pos mod 2 <> 0 then
o 0;
enter_reloc (Reloc_setglobal id);
oo 0
let rec emit code =
let out_const_int i =
if -128 <= i && i < 128 then (
o opCONSTINT8;
o i
) else if -32768 <= i && i < 32768 then (
o opCONSTINT16;
oo i
) else (
o opGETGLOBAL;
slot_for_const (Const_int i)
)
in
let out_header tag n =
if Config.word_size = 32 then (
if !out_pos mod 4 <> 0 then
for i = !out_pos mod 4 to 3 do
o 0;
done;
out_tag tag; (* [0,8) *)
o 0; (* [8,16) *)
o (n lsl 4); (* [16,24) *)
o (n lsr 4) (* [24,32) *)
) else (
if !out_pos mod 8 <> 0 then
for i = !out_pos mod 8 to 7 do
o 0;
done;
out_tag tag; (* [0,8) *)
o 0; (* [8,16) *)
o 0; (* [16,24) *)
o 0; (* [24,32) *)
o (n lsl 4); (* [32,40) *)
o (n lsr 4); (* [40,48) *)
o (n lsr 12); (* [48,56) *)
o (n lsr 20) (* [56,64) *)
)
in
let inst = function
| Kaccess n -> o opACCESS; o n
| Kapply -> o opAPPLY
| Kbranch l -> o opBRANCH; out_label l
| Kbranchif l -> o opBRANCHIF; out_label l
| Kbranchifnot l -> o opBRANCHIFNOT; out_label l
| Kcur l -> o opCUR; out_label l
| Kdummy n -> o opDUMMY; o n
| Kendlet n -> o opENDLET; o n
| Kgetglobal id -> o opGETGLOBAL; slot_for_getglobal id
| Kgrab -> o opGRAB
| Klabel l -> define_label l
| Klet -> o opLET
| Kmakeblock(tag,n) -> o opMAKEBLOCK; out_header tag n
| Kpoptrap -> o opPOPTRAP
| Kprim prim ->
begin match prim with
| Paddint -> o opADDINT
| Pandint -> o opANDINT
| Pasrint -> o opASRINT
| Pccall(arity,name) ->
if arity <= 4 then (
o (opCCALL1+arity-1);
slot_for_prim name
) else
not_implemented()
| Pdecr -> o opDECR
| Pdivint -> o opDIVINT
| Pdummy n ->
o opDUMMY; o n
| Pfield n ->
o opGETFIELD; o n
| Pfloat(Paddfloat) -> o opADDFLOAT
| Pfloat(Psubfloat) -> o opSUBFLOAT
| Pfloat(Pmulfloat) -> o opMULFLOAT
| Pfloat(Pdivfloat) -> o opDIVFLOAT
| Pgetarrayitem ->
o opGETARRAYITEM
| Pgetstringitem ->
o opGETSTRINGITEM
| Pidentity -> ()
| Pincr -> o opINCR
| Plslint -> o opLSLINT
| Plsrint -> o opLSRINT
| Pmakearray init ->
o opMAKEARRAY;
o (if init then 1 else 0)
| Pmakestring ->
o opMAKESTRING
| Pmodint -> o opMODINT
| Pmulint -> o opMULINT
| Pnegint -> o opNEGINT
| Pnot -> o opNOT
| Porint -> o opORINT
| Praise ->
o opRAISE
| Psetfield n ->
o opSETFIELD; o n
| Psetarrayitem ->
o opSETARRAYITEM
| Psetstringitem ->
o opSETSTRINGITEM
| Pstringlength ->
o opSTRINGLENGTH
| Psubint -> o opSUBINT
| Ptest t ->
o begin match t with
| Ptest_eq -> opEQ
| Ptest_neq -> opNEQ
| Ptest_int t -> out_test_int t
| Ptest_float t -> out_test_float t
| Ptest_string t -> out_test_string t
| _ -> assert false
end
| Pxorint -> o opXORINT
| _ ->
dump_prim 3 prim;
fatal_error "TODO"
end
| Kpush -> o opPUSH
| Kpushmark -> o opPUSHMARK
| Kpushtrap l ->
o opPUSHTRAP;
out_label l
| Kquote c ->
begin match c with
| Const_block t ->
o opATOM;
o t
| Const_base c ->
match c with
| Const_char x -> out_const_int (int_of_char x)
| Const_int x -> out_const_int x
| Const_float x -> o opGETGLOBAL; slot_for_const c
| Const_string x -> o opGETGLOBAL; slot_for_const c
end
| Kreturn -> o opRETURN
| Ksetglobal id -> o opSETGLOBAL; slot_for_setglobal id
| Kswitch ls ->
o opSWITCH;
o (Array.length ls);
if !out_pos mod 2 <> 0 then
o 0;
let orig = !out_pos in
Array.iter (out_label_with_orig orig) ls
| Ktermapply -> o opTERMAPPLY
| Ktest(tst,l) ->
begin match tst with
| Ptest_int(Pneqimm x) ->
o opPUSH; o opPUSH; out_const_int x;
o opEQ; o opPOPBRANCHIFNOT; out_label l
| Ptest_int t ->
o (out_test_int_b t);
out_label l
| Ptest_float(Pneqimm x) ->
o opPUSH; o opPUSH; o opGETGLOBAL; slot_for_const (Const_float x);
o opEQFLOAT; o opPOPBRANCHIFNOT; out_label l
| Ptest_float t ->
o (out_test_float t);
o opBRANCHIF; out_label l
| Ptest_string(Pneqimm x) ->
o opPUSH; o opPUSH; o opGETGLOBAL; slot_for_const (Const_string x);
o opEQSTRING; o opPOPBRANCHIFNOT; out_label l
| Ptest_string t ->
o (out_test_string t);
o opBRANCHIF; out_label l
| Ptest_noteqtag tag ->
o opBRANCHIFNEQTAG;
out_tag tag;
out_label l
| _ -> assert false
end
| Kupdate n -> o opUPDATE; o n
in
List.iter inst code
(* phrase *)
type compiled_phrase = {
cph_pos: int;
cph_len: int;
cph_reloc: (int * reloc_entry) list
}
let phr_idx = ref []
let abs_out_pos = ref 0
let start_emit_phrase oc =
phr_idx := [];
if Config.word_size = 32 then
output_string oc Config.obj_magic32
else
output_string oc Config.obj_magic64;
output_bin_int oc 0; (* placeholder of size *)
abs_out_pos := 8
let emit_phrase oc (init,fcts) =
out_pos := 0;
relocs := [];
label_tbl := [|Label_undefined []|];
if fcts = [] then
emit init
else (
emit init;
emit [Kbranch 0];
emit fcts;
emit [Klabel 0]
);
if !out_pos mod Config.sizeof_word <> 0 then
for i = !out_pos mod Config.sizeof_word to Config.sizeof_word-1 do
o opNOP;
done;
output oc !out_buf 0 !out_pos;
phr_idx := {
cph_pos = !abs_out_pos;
cph_len = !out_pos;
cph_reloc = !relocs
} :: !phr_idx;
abs_out_pos := !abs_out_pos + !out_pos
let end_emit_phrase oc =
output_value oc (List.rev !phr_idx);
seek_out oc 4;
output_bin_int oc !abs_out_pos