-
-
Notifications
You must be signed in to change notification settings - Fork 671
/
Copy pathhxbWriter.ml
2334 lines (2154 loc) · 70.2 KB
/
hxbWriter.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
open Globals
open Ast
open Type
open HxbData
open Tanon_identification
let rec binop_index op = match op with
| OpAdd -> 0
| OpMult -> 1
| OpDiv -> 2
| OpSub -> 3
| OpAssign -> 4
| OpEq -> 5
| OpNotEq -> 6
| OpGt -> 7
| OpGte -> 8
| OpLt -> 9
| OpLte -> 10
| OpAnd -> 11
| OpOr -> 12
| OpXor -> 13
| OpBoolAnd -> 14
| OpBoolOr -> 15
| OpShl -> 16
| OpShr -> 17
| OpUShr -> 18
| OpMod -> 19
| OpInterval -> 20
| OpArrow -> 21
| OpIn -> 22
| OpNullCoal -> 23
| OpAssignOp op -> 30 + binop_index op
let unop_index op flag = match op,flag with
| Increment,Prefix -> 0
| Decrement,Prefix -> 1
| Not,Prefix -> 2
| Neg,Prefix -> 3
| NegBits,Prefix -> 4
| Spread,Prefix -> 5
| Increment,Postfix -> 6
| Decrement,Postfix -> 7
| Not,Postfix -> 8
| Neg,Postfix -> 9
| NegBits,Postfix -> 10
| Spread,Postfix -> 11
module StringHashtbl = Hashtbl.Make(struct
type t = string
let equal =
String.equal
let hash s =
(* What's the best here? *)
Hashtbl.hash s
end)
module Pool = struct
type ('key,'value) t = {
lut : ('key,int) Hashtbl.t;
items : 'value DynArray.t;
mutable closed : bool;
}
let create () = {
lut = Hashtbl.create 0;
items = DynArray.create ();
closed = false;
}
let add pool (key : 'key) (value : 'value) =
assert (not pool.closed);
let index = DynArray.length pool.items in
DynArray.add pool.items value;
Hashtbl.add pool.lut key index;
index
let get pool (key : 'key) =
Hashtbl.find pool.lut key
let extract pool (key : 'key) =
DynArray.get pool.items (get pool key)
let has pool (key : 'key) =
Hashtbl.mem pool.lut key
let get_or_add pool (key : 'key) (value : 'value) =
try
get pool key
with Not_found ->
add pool key value
let is_empty pool =
DynArray.length pool.items = 0
let advance pool dummy =
DynArray.add pool.items dummy
let finalize pool =
assert (not pool.closed);
pool.closed <- true;
pool.items
end
module IdentityPool = struct
type ('key,'value) t = {
items : ('key * 'value) DynArray.t;
mutable closed : bool;
}
let create () = {
items = DynArray.create ();
closed = false;
}
let add pool (key : 'key) (value : 'value) =
assert (not pool.closed);
let index = DynArray.length pool.items in
DynArray.add pool.items (key,value);
index
let get pool (key : 'key) =
DynArray.index_of (fun (key',_) -> key == key') pool.items
let get_or_add pool (key : 'key) (value : 'value) =
try
get pool key
with Not_found ->
add pool key value
let to_list pool =
DynArray.to_list pool.items
let finalize pool =
assert (not pool.closed);
pool.closed <- true;
pool.items
let length pool = DynArray.length pool.items
end
module HashedIdentityPool = struct
type ('hkey,'key,'value) t = {
lut : ('hkey,('key * int)) Hashtbl.t;
items : ('key * 'value) DynArray.t;
mutable closed : bool;
}
let create () = {
lut = Hashtbl.create 16;
items = DynArray.create ();
closed = false;
}
let add pool (hkey : 'hkey) (key : 'key) (value : 'value) =
assert (not pool.closed);
let index = DynArray.length pool.items in
DynArray.add pool.items (key,value);
Hashtbl.add pool.lut hkey (key,index);
index
let get pool (hkey : 'hkey) (key : 'key) =
let l = Hashtbl.find_all pool.lut hkey in
List.assq key l
let finalize pool =
assert (not pool.closed);
pool.closed <- true;
pool.items
end
module SimnBuffer = struct
type t = {
buffer_size : int;
mutable buffer : bytes;
mutable buffers : bytes Queue.t;
mutable offset : int;
}
let create buffer_size = {
buffer = Bytes.create buffer_size;
buffers = Queue.create ();
offset = 0;
buffer_size = buffer_size;
}
let reset sb =
sb.buffer <- Bytes.create sb.buffer_size;
sb.buffers <- Queue.create ();
sb.offset <- 0
let promote_buffer sb =
Queue.add sb.buffer sb.buffers;
sb.buffer <- Bytes.create sb.buffer_size;
sb.offset <- 0
let add_u8 sb i =
if sb.offset = sb.buffer_size then begin
(* Current buffer is full, promote it. *)
promote_buffer sb;
Bytes.unsafe_set sb.buffer 0 i;
sb.offset <- 1;
end else begin
(* There's room, put it in. *)
Bytes.unsafe_set sb.buffer sb.offset i;
sb.offset <- sb.offset + 1
end
let add_bytes sb bytes =
let rec loop offset left =
let space = sb.buffer_size - sb.offset in
if left > space then begin
(* We need more than we have. Blit as much as we can, promote buffer, recurse. *)
Bytes.unsafe_blit bytes offset sb.buffer sb.offset space;
promote_buffer sb;
loop (offset + space) (left - space)
end else begin
(* It fits, blit it. *)
Bytes.unsafe_blit bytes offset sb.buffer sb.offset left;
sb.offset <- sb.offset + left;
end
in
loop 0 (Bytes.length bytes)
let contents sb =
let size = sb.offset + sb.buffer_size * Queue.length sb.buffers in
let out = Bytes.create size in
let offset = ref 0 in
(* We know that all sb.buffers are of sb.buffer_size length, so blit them together. *)
Queue.iter (fun bytes ->
Bytes.unsafe_blit bytes 0 out !offset sb.buffer_size;
offset := !offset + sb.buffer_size;
) sb.buffers;
(* Append our current buffer until sb.offset *)
Bytes.unsafe_blit sb.buffer 0 out !offset sb.offset;
out
end
module Chunk = struct
type t = {
kind : chunk_kind;
cp : StringPool.t;
ch : SimnBuffer.t;
}
let create kind cp initial_size = {
kind;
cp;
ch = SimnBuffer.create initial_size;
}
let reset chunk =
SimnBuffer.reset chunk.ch
let write_u8 io v =
SimnBuffer.add_u8 io.ch (Char.unsafe_chr v)
let write_i32 io v =
let base = Int32.to_int v in
let big = Int32.to_int (Int32.shift_right_logical v 24) in
write_u8 io base;
write_u8 io (base lsr 8);
write_u8 io (base lsr 16);
write_u8 io big
let write_i64 io v =
write_i32 io (Int64.to_int32 v);
write_i32 io (Int64.to_int32 (Int64.shift_right_logical v 32))
let write_f64 io v =
write_i64 io (Int64.bits_of_float v)
let write_bytes io b =
SimnBuffer.add_bytes io.ch b
let write_ui16 io i =
write_u8 io i;
write_u8 io (i lsr 8)
let get_bytes io =
SimnBuffer.contents io.ch
let rec write_uleb128 io v =
let b = v land 0x7F in
let rest = v lsr 7 in
if rest = 0 then
write_u8 io b
else begin
write_u8 io (b lor 0x80);
write_uleb128 io rest
end
let rec write_leb128 io v =
let b = v land 0x7F in
let rest = v asr 7 in
if (rest = 0 && (b land 0x40 = 0)) || (rest = -1 && (b land 0x40 = 0x40)) then
write_u8 io b
else begin
write_u8 io (b lor 0x80);
write_leb128 io rest
end
let write_bytes_length_prefixed io b =
write_uleb128 io (Bytes.length b);
write_bytes io b
let write_bool io b =
write_u8 io (if b then 1 else 0)
let export : 'a . t -> 'a IO.output -> unit = fun io chex ->
let bytes = get_bytes io in
let length = Bytes.length bytes in
write_chunk_prefix io.kind length chex;
IO.nwrite chex bytes
let write_string chunk s =
write_uleb128 chunk (StringPool.get_or_add chunk.cp s)
let write_list : 'b . t -> 'b list -> ('b -> unit) -> unit = fun chunk l f ->
write_uleb128 chunk (List.length l);
List.iter f l
let write_dynarray chunk d f =
write_uleb128 chunk (DynArray.length d);
DynArray.iter f d
let write_option : 'b . t -> 'b option -> ('b -> unit) -> unit = fun chunk v f -> match v with
| None ->
write_u8 chunk 0
| Some v ->
write_u8 chunk 1;
f v
let export_data chunk_from chunk_to =
let bytes = get_bytes chunk_from in
write_bytes chunk_to bytes
end
module PosWriter = struct
type t = {
mutable p_file : string;
mutable p_min : int;
mutable p_max : int;
}
let do_write_pos (chunk : Chunk.t) (p : pos) =
Chunk.write_string chunk p.pfile;
Chunk.write_leb128 chunk p.pmin;
Chunk.write_leb128 chunk p.pmax
let create chunk p =
do_write_pos chunk p;
{
p_file = p.pfile;
p_min = p.pmin;
p_max = p.pmax;
}
let write_pos pw (chunk : Chunk.t) (write_equal : bool) (offset : int) (p : pos) =
if p.pfile != pw.p_file then begin
(* File changed, write full pos *)
Chunk.write_u8 chunk (4 + offset);
do_write_pos chunk p;
pw.p_file <- p.pfile;
pw.p_min <- p.pmin;
pw.p_max <- p.pmax;
end else if p.pmin <> pw.p_min then begin
if p.pmax <> pw.p_max then begin
(* pmin and pmax changed *)
Chunk.write_u8 chunk (3 + offset);
Chunk.write_leb128 chunk p.pmin;
Chunk.write_leb128 chunk p.pmax;
pw.p_min <- p.pmin;
pw.p_max <- p.pmax;
end else begin
(* pmin changed *)
Chunk.write_u8 chunk (1 + offset);
Chunk.write_leb128 chunk p.pmin;
pw.p_min <- p.pmin;
end
end else if p.pmax <> pw.p_max then begin
(* pmax changed *)
Chunk.write_u8 chunk (2 + offset);
Chunk.write_leb128 chunk p.pmax;
pw.p_max <- p.pmax;
end else begin
if write_equal then
Chunk.write_u8 chunk offset;
end
end
type field_writer_context = {
t_pool : StringPool.t;
pos_writer : PosWriter.t;
mutable texpr_this : texpr option;
vars : (tvar * int) DynArray.t;
}
let create_field_writer_context pos_writer = {
t_pool = StringPool.create ();
pos_writer = pos_writer;
texpr_this = None;
vars = DynArray.create ();
}
type hxb_writer = {
config : HxbWriterConfig.writer_target_config;
warn : Warning.warning -> string -> Globals.pos -> unit;
anon_id : Type.t Tanon_identification.tanon_identification;
mutable current_module : module_def;
chunks : Chunk.t DynArray.t;
has_own_string_pool : bool;
cp : StringPool.t;
docs : StringPool.t;
mutable chunk : Chunk.t;
classes : (path,tclass) Pool.t;
enums : (path,tenum) Pool.t;
typedefs : (path,tdef) Pool.t;
abstracts : (path,tabstract) Pool.t;
anons : (path,bytes option) Pool.t;
anon_fields : (string,tclass_field,bytes option) HashedIdentityPool.t;
tmonos : (tmono,unit) IdentityPool.t;
own_classes : (path,tclass) Pool.t;
own_enums : (path,tenum) Pool.t;
own_typedefs : (path,tdef) Pool.t;
own_abstracts : (path,tabstract) Pool.t;
type_param_lut : (path,(string,typed_type_param) Pool.t) Pool.t;
class_fields : (string,tclass_field,(tclass * class_field_ref_kind * int)) HashedIdentityPool.t;
enum_fields : ((path * string),(tenum * tenum_field)) Pool.t;
mutable type_type_parameters : (string,typed_type_param) Pool.t;
mutable field_type_parameters : (typed_type_param,unit) IdentityPool.t;
mutable local_type_parameters : (typed_type_param,unit) IdentityPool.t;
mutable field_stack : unit list;
mutable wrote_local_type_param : bool;
mutable needs_local_context : bool;
unbound_ttp : (typed_type_param,unit) IdentityPool.t;
unclosed_mono : (tmono,unit) IdentityPool.t;
t_instance_chunk : Chunk.t;
}
module HxbWriter = struct
let get_backtrace () = Printexc.get_raw_backtrace ()
let get_callstack () = Printexc.get_callstack 200
let failwith writer msg backtrace =
let msg =
(Printf.sprintf "Compiler failure while writing hxb chunk %s of %s: %s\n" (string_of_chunk_kind writer.chunk.kind) (s_type_path writer.current_module.m_path) (msg))
^ "Please submit an issue at https://github.com/HaxeFoundation/haxe/issues/new\n"
^ "Attach the following information:"
in
let backtrace = Printexc.raw_backtrace_to_string backtrace in
let s = Printf.sprintf "%s\nHaxe: %s\n%s" msg s_version_full backtrace in
failwith s
let in_nested_scope writer = match writer.field_stack with
| [] -> false (* can happen for cl_init and in EXD *)
| [_] -> false
| _ -> true
(* Chunks *)
let start_chunk writer (kind : chunk_kind) =
let initial_size = match kind with
| EOT | EOF | EOM -> 0
| MDF -> 16
| MTF | IMP | CLR | END | ABD | ENR | ABR | TDR | EFR | CFR | AFD -> 64
| OFR | OFD | OBD | CLD | TDD | EFD -> 128
| STR | DOC -> 256
| CFD | EXD -> 512
in
let new_chunk = Chunk.create kind writer.cp initial_size in
DynArray.add writer.chunks new_chunk;
writer.chunk <- new_chunk
let start_temporary_chunk : 'a . hxb_writer -> int -> (Chunk.t -> 'a) -> 'a = fun writer initial_size ->
let new_chunk = Chunk.create EOM (* TODO: something else? *) writer.cp initial_size in
let old_chunk = writer.chunk in
writer.chunk <- new_chunk;
(fun f ->
writer.chunk <- old_chunk;
f new_chunk
)
let write_inlined_list : 'a . hxb_writer -> int -> int -> (int -> unit) -> (unit -> unit) -> ('a -> unit) -> 'a list -> unit
= fun writer offset max f_byte f_first f_elt l ->
let length = List.length l in
if length > max then begin
f_byte (offset + 9);
f_first ();
Chunk.write_list writer.chunk l f_elt
end else begin
f_byte (offset + length);
f_first();
List.iter (fun elt ->
f_elt elt
) l
end
(* Basic compounds *)
let write_path writer (path : path) =
Chunk.write_list writer.chunk (fst path) (Chunk.write_string writer.chunk);
Chunk.write_string writer.chunk (snd path)
let write_full_path writer (pack : string list) (mname : string) (tname : string) =
Chunk.write_list writer.chunk pack (Chunk.write_string writer.chunk);
if mname = "" || tname = "" then
failwith writer (Printf.sprintf "write_full_path: pack = %s, mname = %s, tname = %s" (String.concat "." pack) mname tname) (get_callstack ());
Chunk.write_string writer.chunk mname;
Chunk.write_string writer.chunk tname
let maybe_write_documentation writer (doc : doc_block option) =
match doc with
| Some doc when writer.config.generate_docs ->
Chunk.write_u8 writer.chunk 1;
Chunk.write_option writer.chunk doc.doc_own (fun s ->
Chunk.write_uleb128 writer.chunk (StringPool.get_or_add writer.docs s)
);
Chunk.write_list writer.chunk doc.doc_inherited (fun s ->
Chunk.write_uleb128 writer.chunk (StringPool.get_or_add writer.docs s)
)
| _ ->
Chunk.write_u8 writer.chunk 0
let write_pos writer (p : pos) =
Chunk.write_string writer.chunk p.pfile;
Chunk.write_leb128 writer.chunk p.pmin;
Chunk.write_leb128 writer.chunk p.pmax
let write_pos_pair writer (p1 : pos) (p2 : pos) =
(* Write second position offset relative to first position's pmin, which is often within 1 byte range. *)
Chunk.write_string writer.chunk p1.pfile;
Chunk.write_leb128 writer.chunk p1.pmin;
Chunk.write_leb128 writer.chunk p1.pmax;
Chunk.write_leb128 writer.chunk (p2.pmin - p1.pmin);
Chunk.write_leb128 writer.chunk (p2.pmax - p1.pmin)
let rec write_metadata_entry writer ((meta,el,p) : metadata_entry) =
Chunk.write_string writer.chunk (Meta.to_string meta);
write_pos writer p;
Chunk.write_list writer.chunk el (write_expr writer)
and write_metadata writer ml =
Chunk.write_list writer.chunk ml (write_metadata_entry writer)
(* expr *)
and write_object_field_key writer (n,p,qs) =
Chunk.write_string writer.chunk n;
write_pos writer p;
begin match qs with
| NoQuotes -> Chunk.write_u8 writer.chunk 0
| DoubleQuotes -> Chunk.write_u8 writer.chunk 1
end
and write_type_path writer tp =
Chunk.write_list writer.chunk tp.tpackage (Chunk.write_string writer.chunk);
Chunk.write_string writer.chunk tp.tname;
Chunk.write_list writer.chunk tp.tparams (write_type_param_or_const writer);
Chunk.write_option writer.chunk tp.tsub (Chunk.write_string writer.chunk)
and write_placed_type_path writer ptp =
write_type_path writer ptp.path;
write_pos_pair writer ptp.pos_full ptp.pos_path
and write_type_param_or_const writer = function
| TPType th ->
Chunk.write_u8 writer.chunk 0;
write_type_hint writer th
| TPExpr e ->
Chunk.write_u8 writer.chunk 1;
write_expr writer e
and write_complex_type writer = function
| CTPath tp ->
Chunk.write_u8 writer.chunk 0;
write_placed_type_path writer tp
| CTFunction(thl,th) ->
Chunk.write_u8 writer.chunk 1;
Chunk.write_list writer.chunk thl (write_type_hint writer);
write_type_hint writer th
| CTAnonymous cffl ->
Chunk.write_u8 writer.chunk 2;
Chunk.write_list writer.chunk cffl (write_cfield writer);
| CTParent th ->
Chunk.write_u8 writer.chunk 3;
write_type_hint writer th
| CTExtend(ptp,cffl) ->
Chunk.write_u8 writer.chunk 4;
Chunk.write_list writer.chunk ptp (write_placed_type_path writer);
Chunk.write_list writer.chunk cffl (write_cfield writer);
| CTOptional th ->
Chunk.write_u8 writer.chunk 5;
write_type_hint writer th
| CTNamed(pn,th) ->
Chunk.write_u8 writer.chunk 6;
write_placed_name writer pn;
write_type_hint writer th
| CTIntersection(thl) ->
Chunk.write_u8 writer.chunk 7;
Chunk.write_list writer.chunk thl (write_type_hint writer)
and write_type_hint writer (ct,p) =
write_complex_type writer ct;
write_pos writer p
and write_type_param writer tp =
write_placed_name writer tp.tp_name;
Chunk.write_list writer.chunk tp.tp_params (write_type_param writer);
Chunk.write_option writer.chunk tp.tp_constraints (write_type_hint writer);
Chunk.write_option writer.chunk tp.tp_default (write_type_hint writer);
Chunk.write_list writer.chunk tp.tp_meta (write_metadata_entry writer)
and write_func_arg writer (pn,b,meta,tho,eo) =
write_placed_name writer pn;
Chunk.write_bool writer.chunk b;
write_metadata writer meta;
Chunk.write_option writer.chunk tho (write_type_hint writer);
Chunk.write_option writer.chunk eo (write_expr writer);
and write_func writer f =
Chunk.write_list writer.chunk f.f_params (write_type_param writer);
Chunk.write_list writer.chunk f.f_args (write_func_arg writer);
Chunk.write_option writer.chunk f.f_type (write_type_hint writer);
Chunk.write_option writer.chunk f.f_expr (write_expr writer)
and write_placed_name writer (s,p) =
Chunk.write_string writer.chunk s;
write_pos writer p
and write_access writer ac =
let i = match ac with
| APublic -> 0
| APrivate -> 1
| AStatic -> 2
| AOverride -> 3
| ADynamic -> 4
| AInline -> 5
| AMacro -> 6
| AFinal -> 7
| AExtern -> 8
| AAbstract -> 9
| AOverload -> 10
| AEnum -> 11
in
Chunk.write_u8 writer.chunk i
and write_placed_access writer (ac,p) =
write_access writer ac;
write_pos writer p
and write_cfield_kind writer = function
| FVar(tho,eo) ->
Chunk.write_u8 writer.chunk 0;
Chunk.write_option writer.chunk tho (write_type_hint writer);
Chunk.write_option writer.chunk eo (write_expr writer);
| FFun f ->
Chunk.write_u8 writer.chunk 1;
write_func writer f;
| FProp(pn1,pn2,tho,eo) ->
Chunk.write_u8 writer.chunk 2;
write_placed_name writer pn1;
write_placed_name writer pn2;
Chunk.write_option writer.chunk tho (write_type_hint writer);
Chunk.write_option writer.chunk eo (write_expr writer)
and write_cfield writer cff =
write_placed_name writer cff.cff_name;
maybe_write_documentation writer cff.cff_doc;
write_pos writer cff.cff_pos;
write_metadata writer cff.cff_meta;
Chunk.write_list writer.chunk cff.cff_access (write_placed_access writer);
write_cfield_kind writer cff.cff_kind
and write_expr writer (e,p) =
write_pos writer p;
match e with
| EConst (Int (s, suffix)) ->
Chunk.write_u8 writer.chunk 0;
Chunk.write_string writer.chunk s;
Chunk.write_option writer.chunk suffix (Chunk.write_string writer.chunk);
| EConst (Float (s, suffix)) ->
Chunk.write_u8 writer.chunk 1;
Chunk.write_string writer.chunk s;
Chunk.write_option writer.chunk suffix (Chunk.write_string writer.chunk);
| EConst (String (s,qs)) ->
Chunk.write_u8 writer.chunk 2;
Chunk.write_string writer.chunk s;
begin match qs with
| SDoubleQuotes -> Chunk.write_u8 writer.chunk 0;
| SSingleQuotes -> Chunk.write_u8 writer.chunk 1;
end
| EConst (Ident s) ->
Chunk.write_u8 writer.chunk 3;
Chunk.write_string writer.chunk s;
| EConst (Regexp(s1,s2)) ->
Chunk.write_u8 writer.chunk 4;
Chunk.write_string writer.chunk s1;
Chunk.write_string writer.chunk s2;
| EArray(e1,e2) ->
Chunk.write_u8 writer.chunk 5;
write_expr writer e1;
write_expr writer e2;
| EBinop(op,e1,e2) ->
Chunk.write_u8 writer.chunk 6;
Chunk.write_u8 writer.chunk (binop_index op);
write_expr writer e1;
write_expr writer e2;
| EField(e1,s,kind) ->
Chunk.write_u8 writer.chunk 7;
write_expr writer e1;
Chunk.write_string writer.chunk s;
begin match kind with
| EFNormal -> Chunk.write_u8 writer.chunk 0;
| EFSafe -> Chunk.write_u8 writer.chunk 1;
end
| EParenthesis e1 ->
Chunk.write_u8 writer.chunk 8;
write_expr writer e1;
| EObjectDecl fl ->
Chunk.write_u8 writer.chunk 9;
let write_field (k,e1) =
write_object_field_key writer k;
write_expr writer e1
in
Chunk.write_list writer.chunk fl write_field;
| EArrayDecl el ->
Chunk.write_u8 writer.chunk 10;
Chunk.write_list writer.chunk el (write_expr writer);
| ECall(e1,el) ->
Chunk.write_u8 writer.chunk 11;
write_expr writer e1;
Chunk.write_list writer.chunk el (write_expr writer)
| ENew(ptp,el) ->
Chunk.write_u8 writer.chunk 12;
write_placed_type_path writer ptp;
Chunk.write_list writer.chunk el (write_expr writer);
| EUnop(op,flag,e1) ->
Chunk.write_u8 writer.chunk 13;
Chunk.write_u8 writer.chunk (unop_index op flag);
write_expr writer e1;
| EVars vl ->
Chunk.write_u8 writer.chunk 14;
let write_var v =
write_placed_name writer v.ev_name;
Chunk.write_bool writer.chunk v.ev_final;
Chunk.write_bool writer.chunk v.ev_static;
Chunk.write_option writer.chunk v.ev_type (write_type_hint writer);
Chunk.write_option writer.chunk v.ev_expr (write_expr writer);
write_metadata writer v.ev_meta;
in
Chunk.write_list writer.chunk vl write_var
| EFunction(fk,f) ->
Chunk.write_u8 writer.chunk 15;
begin match fk with
| FKAnonymous -> Chunk.write_u8 writer.chunk 0;
| FKNamed (pn,inline) ->
Chunk.write_u8 writer.chunk 1;
write_placed_name writer pn;
Chunk.write_bool writer.chunk inline;
| FKArrow -> Chunk.write_u8 writer.chunk 2;
end;
write_func writer f;
| EBlock el ->
Chunk.write_u8 writer.chunk 16;
Chunk.write_list writer.chunk el (write_expr writer)
| EFor(e1,e2) ->
Chunk.write_u8 writer.chunk 17;
write_expr writer e1;
write_expr writer e2;
| EIf(e1,e2,None) ->
Chunk.write_u8 writer.chunk 18;
write_expr writer e1;
write_expr writer e2;
| EIf(e1,e2,Some e3) ->
Chunk.write_u8 writer.chunk 19;
write_expr writer e1;
write_expr writer e2;
write_expr writer e3;
| EWhile(e1,e2,NormalWhile) ->
Chunk.write_u8 writer.chunk 20;
write_expr writer e1;
write_expr writer e2;
| EWhile(e1,e2,DoWhile) ->
Chunk.write_u8 writer.chunk 21;
write_expr writer e1;
write_expr writer e2;
| ESwitch(e1,cases,def) ->
Chunk.write_u8 writer.chunk 22;
write_expr writer e1;
let write_case (el,eg,eo,p) =
Chunk.write_list writer.chunk el (write_expr writer);
Chunk.write_option writer.chunk eg (write_expr writer);
Chunk.write_option writer.chunk eo (write_expr writer);
write_pos writer p;
in
Chunk.write_list writer.chunk cases write_case;
let write_default (eo,p) =
Chunk.write_option writer.chunk eo (write_expr writer);
write_pos writer p
in
Chunk.write_option writer.chunk def write_default;
| ETry(e1,catches) ->
Chunk.write_u8 writer.chunk 23;
write_expr writer e1;
let write_catch (pn,th,e,p) =
write_placed_name writer pn;
Chunk.write_option writer.chunk th (write_type_hint writer);
write_expr writer e;
write_pos writer p;
in
Chunk.write_list writer.chunk catches write_catch;
| EReturn None ->
Chunk.write_u8 writer.chunk 24;
| EReturn (Some e1) ->
Chunk.write_u8 writer.chunk 25;
write_expr writer e1;
| EBreak ->
Chunk.write_u8 writer.chunk 26;
| EContinue ->
Chunk.write_u8 writer.chunk 27;
| EUntyped e1 ->
Chunk.write_u8 writer.chunk 28;
write_expr writer e1;
| EThrow e1 ->
Chunk.write_u8 writer.chunk 29;
write_expr writer e1;
| ECast(e1,None) ->
Chunk.write_u8 writer.chunk 30;
write_expr writer e1;
| ECast(e1,Some th) ->
Chunk.write_u8 writer.chunk 31;
write_expr writer e1;
write_type_hint writer th;
| EIs(e1,th) ->
Chunk.write_u8 writer.chunk 32;
write_expr writer e1;
write_type_hint writer th;
| EDisplay(e1,dk) ->
Chunk.write_u8 writer.chunk 33;
write_expr writer e1;
begin match dk with
| DKCall -> Chunk.write_u8 writer.chunk 0;
| DKDot -> Chunk.write_u8 writer.chunk 1;
| DKStructure -> Chunk.write_u8 writer.chunk 2;
| DKMarked -> Chunk.write_u8 writer.chunk 3;
| DKPattern b ->
Chunk.write_u8 writer.chunk 4;
Chunk.write_bool writer.chunk b;
end
| ETernary(e1,e2,e3) ->
Chunk.write_u8 writer.chunk 34;
write_expr writer e1;
write_expr writer e2;
write_expr writer e3;
| ECheckType(e1,th) ->
Chunk.write_u8 writer.chunk 35;
write_expr writer e1;
write_type_hint writer th;
| EMeta(m,e1) ->
Chunk.write_u8 writer.chunk 36;
write_metadata_entry writer m;
write_expr writer e1
(* References *)
let write_class_ref writer (c : tclass) =
let i = Pool.get_or_add writer.classes c.cl_path c in
Chunk.write_uleb128 writer.chunk i
let write_enum_ref writer (en : tenum) =
let i = Pool.get_or_add writer.enums en.e_path en in
Chunk.write_uleb128 writer.chunk i
let write_typedef_ref writer (td : tdef) =
let i = Pool.get_or_add writer.typedefs td.t_path td in
Chunk.write_uleb128 writer.chunk i
let write_abstract_ref writer (a : tabstract) =
let i = Pool.get_or_add writer.abstracts a.a_path a in
Chunk.write_uleb128 writer.chunk i
let write_tmono_ref writer (mono : tmono) =
let index = IdentityPool.get_or_add writer.tmonos mono () in
Chunk.write_uleb128 writer.chunk index
let write_field_ref writer (c : tclass) (kind : class_field_ref_kind) (cf : tclass_field) =
let index = try
HashedIdentityPool.get writer.class_fields cf.cf_name cf
with Not_found ->
let find_overload c cf_base =
let rec loop depth cfl = match cfl with
| cf' :: cfl ->
if cf' == cf then
Some(c,depth)
else
loop (depth + 1) cfl
| [] ->
None
in
let cfl = cf_base :: cf_base.cf_overloads in
loop 0 cfl
in
let find_overload c =
try
find_overload c (find_field c cf.cf_name kind)
with Not_found ->
None
in
let r = match kind with
| CfrStatic | CfrConstructor ->
find_overload c;
| CfrInit ->
Some(c,0)
| CfrMember ->
(* For member overloads we need to find the correct class, which is a mess. *)
let rec loop c = match find_overload c with
| Some _ as r ->
r
| None ->
if has_class_flag c CInterface then
let rec loopi l = match l with
| [] ->
None
| (c,_) :: l ->
match loop c with
| Some _ as r ->
r
| None ->
loopi l
in
loopi c.cl_implements
else match c.cl_super with
| Some(c,_) ->
loop c
| None ->
None
in
loop c;
in
let c,depth = match r with
| None ->
print_endline (Printf.sprintf "Could not resolve %s overload for %s on %s" (s_class_field_ref_kind kind) cf.cf_name (s_type_path c.cl_path));
c,0
| Some(c,depth) ->
c,depth
in
HashedIdentityPool.add writer.class_fields cf.cf_name cf (c,kind,depth)
in
Chunk.write_uleb128 writer.chunk index
let write_enum_field_ref writer (en : tenum) (ef : tenum_field) =
let key = (en.e_path,ef.ef_name) in
try
Chunk.write_uleb128 writer.chunk (Pool.get writer.enum_fields key)
with Not_found ->
ignore(Pool.get_or_add writer.enums en.e_path en);
Chunk.write_uleb128 writer.chunk (Pool.add writer.enum_fields key (en,ef))
let write_var_kind writer vk =
let b,sl = match vk with
| VUser TVOLocalVariable -> 0, []
| VUser TVOArgument -> 1, []
| VUser TVOForVariable -> 2, []
| VUser TVOPatternVariable -> 3, []
| VUser TVOCatchVariable -> 4, []
| VUser TVOLocalFunction -> 5, []
| VGenerated -> 6, []
| VInlined -> 7, []
| VInlinedConstructorVariable sl -> 8, sl
| VExtractorVariable -> 9, []
| VAbstractThis -> 10, []
in begin
Chunk.write_u8 writer.chunk b;
if (b == 8) then Chunk.write_list writer.chunk sl (Chunk.write_string writer.chunk);
end
let write_var writer fctx v =
Chunk.write_uleb128 writer.chunk v.v_id;
Chunk.write_string writer.chunk v.v_name;
write_var_kind writer v.v_kind;
Chunk.write_uleb128 writer.chunk v.v_flags;
write_metadata writer v.v_meta;
write_pos writer v.v_pos
let rec write_anon writer (an : tanon) =
let write_fields () =
let restore = start_temporary_chunk writer 256 in
let i = ref 0 in
PMap.iter (fun _ cf ->
write_anon_field_ref writer cf;
incr i;
) an.a_fields;
let bytes = restore (fun new_chunk -> Chunk.get_bytes new_chunk) in
Chunk.write_uleb128 writer.chunk !i;
Chunk.write_bytes writer.chunk bytes;
in