forked from HaxeFoundation/haxe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenswf9.ml
1827 lines (1738 loc) · 49.8 KB
/
genswf9.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
(*
* Haxe Compiler
* Copyright (c)2006 Nicolas Cannasse
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)
open Ast
open Type
open As3
type ('a,'b) gen_lookup = {
h : ('a,'b) Hashtbl.t;
a : 'a DynArray.t;
c : int -> 'b;
}
type 'a lookup = ('a,'a index) gen_lookup
type 'a lookup_nz = ('a,'a index_nz) gen_lookup
type read = Read
type write = Unused__ | Write
type tkind =
| KInt
| KUInt
| KFloat
| KBool
| KType of as3_name
| KDynamic
type register = {
rid : int;
rtype : tkind;
mutable rused : bool;
mutable rinit : bool;
mutable rcond : bool;
}
type 'a access =
| VReg of register
| VId of as3_name
| VGlobal of as3_name
| VArray
| VScope of as3_slot
type local =
| LReg of register
| LScope of as3_slot
| LGlobal of as3_name
type code_infos = {
mutable iregs : register DynArray.t;
mutable ipos : int;
mutable istack : int;
mutable imax : int;
mutable iscopes : int;
mutable imaxscopes : int;
mutable iloop : int;
mutable icond : bool;
}
type try_infos = {
tr_pos : int;
tr_end : int;
tr_catch_pos : int;
tr_type : t;
}
type context = {
(* globals *)
strings : string lookup;
ints : int32 lookup;
floats : float lookup;
namespaces : as3_namespace lookup;
nsets : as3_ns_set lookup;
names : as3_multi_name lookup;
mtypes : as3_method_type lookup_nz;
mutable classes : as3_class list;
mutable statics : as3_static list;
functions : as3_function lookup;
rpublic : as3_namespace index;
gpublic : as3_ns_set index;
debug : bool;
mutable last_line : int;
boot : string;
(* per-function *)
mutable locals : (string,local) PMap.t;
mutable code : as3_opcode DynArray.t;
mutable infos : code_infos;
mutable trys : try_infos list;
mutable breaks : (unit -> unit) list;
mutable continues : (int -> unit) list;
mutable in_static : bool;
mutable curblock : texpr list;
mutable block_vars : (as3_slot * string) list;
mutable try_scope_reg : register option;
}
let error p = Typer.error "Invalid expression" p
let stack_error p = Typer.error "Stack error" p
let index_int (x : int) : 'a index = Obj.magic (x + 1)
let index_nz_int (x : int) : 'a index_nz = Obj.magic x
let tid (x : 'a index) : int = Obj.magic x
let new_lookup() = { h = Hashtbl.create 0; a = DynArray.create(); c = index_int }
let new_lookup_nz() = { h = Hashtbl.create 0; a = DynArray.create(); c = index_nz_int }
let jsize = As3code.length (A3Jump (J3Always,0))
let t_void = TEnum ({
e_path = [],"Void";
e_pos = null_pos;
e_doc = None;
e_private = false;
e_extern = false;
e_types = [];
e_constrs = PMap.empty;
},[])
let t_string = TInst (mk_class ([],"String") null_pos None false,[])
let lookup i w =
try
Hashtbl.find w.h i
with
Not_found ->
let id = w.c (DynArray.length w.a) in
Hashtbl.add w.h i id;
DynArray.add w.a i;
id
let add i w =
let id = w.c (DynArray.length w.a) in
DynArray.add w.a i;
id
let lookup_array w = DynArray.to_array w.a
let string ctx i = lookup i ctx.strings
let write ctx op =
DynArray.add ctx.code op;
ctx.infos.ipos <- As3code.length op + ctx.infos.ipos;
let s = ctx.infos.istack + As3code.stack_delta op in
ctx.infos.istack <- s;
if s > ctx.infos.imax then ctx.infos.imax <- s;
match op with
| A3Scope ->
let n = ctx.infos.iscopes + 1 in
ctx.infos.iscopes <- n;
if n > ctx.infos.imaxscopes then ctx.infos.imaxscopes <- n
| A3PopScope ->
ctx.infos.iscopes <- ctx.infos.iscopes - 1
| _ ->
()
let jump ctx cond =
let op = DynArray.length ctx.code in
write ctx (A3Jump (cond,-4));
let p = ctx.infos.ipos in
(fun () ->
let delta = ctx.infos.ipos - p in
DynArray.set ctx.code op (A3Jump (cond,delta))
)
let jump_back ctx =
let j = jump ctx J3Always in
let p = ctx.infos.ipos in
write ctx A3Label;
j , (fun cond ->
let delta = p + -(ctx.infos.ipos + jsize) in
write ctx (A3Jump (cond,delta))
)
let type_path ctx ?(getclass=false) path =
let pack, name = (match path with
| [] , "Int" -> [] , "int"
| [] , "UInt" -> [] , "uint"
| [] , "Float" -> [] , "Number"
| [] , "Bool" -> [] , "Boolean"
| [] , "Void" -> [] , "void"
| ["flash"] , "FlashXml__" -> [] , "Xml"
| ["flash"] , "Boot" -> [] , ctx.boot
| _ -> path
) in
let pid = string ctx (String.concat "." pack) in
let nameid = string ctx name in
let pid = lookup (A3NPublic (Some pid)) ctx.namespaces in
let tid = lookup (if getclass then A3MMultiName (Some nameid,lookup [pid] ctx.nsets) else A3MName (nameid,pid)) ctx.names in
tid
let rec follow_basic t =
match t with
| TMono r ->
(match !r with
| Some t -> follow_basic t
| _ -> t)
| TLazy f ->
follow_basic (!f())
| TType (t,tl) when t.t_path <> ([],"Null") && t.t_path <> ([],"UInt") ->
follow_basic (apply_params t.t_types tl t.t_type)
| _ -> t
let type_id ctx t =
match follow_basic t with
| TEnum ({ e_path = path; e_extern = false },_) ->
type_path ctx path
| TInst ({ cl_shadow = true } as c,_) ->
(match c.cl_implements with
| [] ->
(match c.cl_super with
| None -> type_path ctx ([],"Object")
| Some (csup,_) -> type_path ctx csup.cl_path)
| [csup,_] -> type_path ctx csup.cl_path
| _ -> type_path ctx ([],"Object"))
| TInst (c,_) ->
type_path ctx c.cl_path
| TFun _ ->
type_path ctx ([],"Function")
| TEnum ({ e_path = ([],"Class") as path },_)
| TEnum ({ e_path = ([],"Void") as path },_)
| TEnum ({ e_path = ([],"Bool") as path },_)
| TType ({ t_path = ([],"UInt") as path },_) ->
type_path ctx path
| _ ->
type_path ctx ([],"Object")
let classify ctx t =
match follow_basic t with
| TInst ({ cl_path = [],"Int" },_) ->
KInt
| TInst ({ cl_path = [],"Float" },_) ->
KFloat
| TEnum ({ e_path = [],"Bool" },_) ->
KBool
| TEnum _
| TInst _ ->
KType (type_id ctx t)
| TType ({ t_path = [],"UInt" },_) ->
KUInt
| TFun _
| TMono _
| TAnon _
| TType _
| TDynamic _ ->
KDynamic
| TLazy _ ->
assert false
let ident ctx i = type_path ctx ([],i)
let default_infos() =
{
ipos = 0;
istack = 0;
imax = 0;
iregs = DynArray.create();
iscopes = 0;
imaxscopes = 0;
iloop = -1;
icond = false;
}
let alloc_reg ctx k =
let regs = ctx.infos.iregs in
try
let p = DynArray.index_of (fun r -> not r.rused && k = r.rtype) regs in
let r = DynArray.unsafe_get regs p in
r.rused <- true;
r.rinit <- false;
r
with
Not_found ->
let r = {
rid = DynArray.length regs + 1;
rused = true;
rinit = false;
rtype = k;
rcond = false;
} in
DynArray.add regs r;
r
let coerce ctx t =
(* it would be useful to know if we don't already have
this type on the stack (as detected by the bytecode verifier)...
maybe this get removed at JIT, so it's only useful to reduce codesize
*)
write ctx (match t with
| KInt -> A3ToInt
| KUInt -> A3ToUInt
| KFloat -> A3ToNumber
| KBool -> A3ToBool
| KType t -> A3AsType t
| KDynamic -> A3AsAny
)
let set_reg ctx r =
if not r.rinit then begin
r.rinit <- true;
if ctx.infos.icond then r.rcond <- true;
end;
coerce ctx r.rtype;
write ctx (A3SetReg r.rid)
let free_reg ctx r =
r.rused <- false
let pop ctx n =
let rec loop n =
if n > 0 then begin
write ctx A3Pop;
loop (n - 1)
end
in
if n < 0 then assert false;
let old = ctx.infos.istack in
loop n;
ctx.infos.istack <- old
let define_local ctx ?(init=false) name t el =
let l = (if List.exists (Transform.local_find false name) el then begin
let pos = (try
fst (List.find (fun (_,x) -> name = x) ctx.block_vars)
with
Not_found ->
let n = List.length ctx.block_vars + 1 in
ctx.block_vars <- (n,name) :: ctx.block_vars;
n
) in
LScope pos
end else
let r = alloc_reg ctx (classify ctx t) in
r.rinit <- init;
LReg r
) in
ctx.locals <- PMap.add name l ctx.locals
let is_set v = (Obj.magic v) = Write
let gen_local_access ctx name p (forset : 'a) : 'a access =
match (try PMap.find name ctx.locals with Not_found -> Typer.error ("Unbound variable " ^ name) p) with
| LReg r ->
VReg r
| LScope n ->
write ctx (A3GetScope 1);
VScope n
| LGlobal id ->
if is_set forset then write ctx (A3FindProp id);
VGlobal id
let rec setvar ctx (acc : write access) retval =
match acc with
| VReg r ->
if retval then write ctx A3Dup;
set_reg ctx r;
| VGlobal g ->
if retval then write ctx A3Dup;
write ctx (A3SetProp g);
| VId _ | VArray | VScope _ when retval ->
let r = alloc_reg ctx KDynamic in
write ctx A3Dup;
set_reg ctx r;
setvar ctx acc false;
write ctx (A3Reg r.rid);
free_reg ctx r
| VId id ->
write ctx (A3InitProp id)
| VArray ->
let id_aset = lookup (A3MMultiNameLate ctx.gpublic) ctx.names in
write ctx (A3InitProp id_aset);
ctx.infos.istack <- ctx.infos.istack - 1
| VScope n ->
write ctx (A3SetSlot n)
let getvar ctx (acc : read access) =
match acc with
| VReg r ->
write ctx (A3Reg r.rid)
| VId id ->
write ctx (A3GetProp id)
| VGlobal g ->
write ctx (A3GetLex g);
| VArray ->
let id_aget = lookup (A3MMultiNameLate ctx.gpublic) ctx.names in
write ctx (A3GetProp id_aget);
ctx.infos.istack <- ctx.infos.istack - 1
| VScope n ->
write ctx (A3GetSlot n)
let open_block ctx el retval =
let old_stack = ctx.infos.istack in
let old_regs = DynArray.map (fun r -> r.rused) ctx.infos.iregs in
let old_locals = ctx.locals in
let old_block = ctx.curblock in
ctx.curblock <- el;
(fun() ->
if ctx.infos.istack <> old_stack + (if retval then 1 else 0) then assert false;
let rcount = DynArray.length old_regs + 1 in
DynArray.iter (fun r ->
if r.rid < rcount then
r.rused <- DynArray.unsafe_get old_regs (r.rid - 1)
else
r.rused <- false
) ctx.infos.iregs;
ctx.locals <- old_locals;
ctx.curblock <- old_block;
)
let begin_branch ctx =
if ctx.infos.icond then
(fun() -> ())
else begin
ctx.infos.icond <- true;
(fun() -> ctx.infos.icond <- false)
end
let debug ctx p =
let line = Lexer.get_error_line p in
if ctx.last_line <> line then begin
write ctx (A3DebugLine line);
ctx.last_line <- line;
end
let begin_fun ctx args tret el stat =
let old_locals = ctx.locals in
let old_code = ctx.code in
let old_infos = ctx.infos in
let old_trys = ctx.trys in
let old_bvars = ctx.block_vars in
let old_static = ctx.in_static in
let last_line = ctx.last_line in
let old_treg = ctx.try_scope_reg in
ctx.infos <- default_infos();
ctx.code <- DynArray.create();
ctx.trys <- [];
ctx.block_vars <- [];
ctx.in_static <- stat;
ctx.last_line <- -1;
(match el with
| [] -> ()
| e :: _ ->
if ctx.debug then begin
write ctx (A3DebugFile (lookup e.epos.pfile ctx.strings));
debug ctx e.epos
end);
ctx.locals <- PMap.foldi (fun name l acc ->
match l with
| LReg _ -> acc
| LScope _ -> PMap.add name (LGlobal (type_path ctx ~getclass:true ([],name))) acc
| LGlobal _ -> PMap.add name l acc
) ctx.locals PMap.empty;
List.iter (fun (name,_,t) ->
define_local ctx name ~init:true t el;
match gen_local_access ctx name null_pos Write with
| VReg _ -> ()
| acc ->
let r = alloc_reg ctx (classify ctx t) in
write ctx (A3Reg r.rid);
setvar ctx acc false
) args;
let args, varargs = (match args with
| ["__arguments__",_,_] -> [], true
| _ -> args, false
) in
let rec loop_try e =
match e.eexpr with
| TFunction _ -> ()
| TTry _ -> raise Exit
| _ -> Type.iter loop_try e
in
ctx.try_scope_reg <- (try List.iter loop_try el; None with Exit -> Some (alloc_reg ctx KDynamic));
(fun () ->
let hasblock = ctx.block_vars <> [] || ctx.trys <> [] in
let dparams = ref None in
List.iter (fun (_,opt,t) ->
match !dparams with
| None -> if opt then dparams := Some [A3VNone]
| Some l -> dparams := Some (A3VNone :: l)
) args;
let mt = {
mt3_ret = Some (type_id ctx tret);
mt3_args = List.map (fun (_,_,t) -> Some (type_id ctx t)) args;
mt3_native = false;
mt3_var_args = varargs;
mt3_debug_name = None;
mt3_dparams = !dparams;
mt3_pnames = None;
mt3_new_block = hasblock;
mt3_unused_flag = false;
mt3_arguments_defined = false;
mt3_uses_dxns = false;
} in
let code = DynArray.to_list ctx.code in
let extra = (
if hasblock then begin
let scope = (match ctx.try_scope_reg with
| None -> [A3Scope]
| Some r -> [A3Dup; A3SetReg r.rid; A3Scope]
) in
A3This :: A3Scope :: A3NewBlock :: scope
end else if not stat then
[A3This; A3Scope]
else
[]
) in
(* add dummy registers initialization *)
let extra = extra @ List.concat (List.map (fun r ->
if not r.rcond then
[]
else
let s = [A3SetReg r.rid] in
match r.rtype with
| KInt -> A3SmallInt 0 :: s
| KUInt -> A3SmallInt 0 :: A3ToUInt :: s
| KFloat -> A3NaN :: s
| KBool -> A3False :: s
| KType t -> A3Null :: A3AsType t :: s
| KDynamic -> A3Null :: A3AsAny :: s
) (DynArray.to_list ctx.infos.iregs)) in
let delta = List.fold_left (fun acc r -> As3code.length r + acc) 0 extra in
let f = {
fun3_id = add mt ctx.mtypes;
fun3_stack_size = (if ctx.infos.imax = 0 && (hasblock || not stat) then 1 else ctx.infos.imax);
fun3_nregs = DynArray.length ctx.infos.iregs + 1;
fun3_init_scope = 1;
fun3_max_scope = ctx.infos.imaxscopes + 1 + (if hasblock then 2 else if not stat then 1 else 0);
fun3_code = extra @ code;
fun3_trys = Array.of_list (List.map (fun t ->
{
tc3_start = t.tr_pos + delta;
tc3_end = t.tr_end + delta;
tc3_handle = t.tr_catch_pos + delta;
tc3_type = (match follow t.tr_type with
| TInst (c,_) -> Some (type_path ctx c.cl_path)
| TEnum (e,_) -> Some (type_path ctx e.e_path)
| TDynamic _ -> None
| _ -> assert false);
tc3_name = None;
}
) (List.rev ctx.trys));
fun3_locals = Array.of_list (List.map (fun (id,name) ->
{
f3_name = ident ctx name;
f3_slot = id;
f3_kind = A3FVar { v3_type = None; v3_value = A3VNone; v3_const = false };
f3_metas = None;
}
) ctx.block_vars);
} in
ignore(add f ctx.functions);
ctx.locals <- old_locals;
ctx.code <- old_code;
ctx.infos <- old_infos;
ctx.trys <- old_trys;
ctx.block_vars <- old_bvars;
ctx.in_static <- old_static;
ctx.last_line <- last_line;
ctx.try_scope_reg <- old_treg;
f.fun3_id
)
let empty_method ctx =
let f = begin_fun ctx [] t_void [] true in
write ctx A3RetVoid;
f()
let begin_loop ctx =
let old_loop = ctx.infos.iloop in
let old_breaks = ctx.breaks in
let old_conts = ctx.continues in
ctx.infos.iloop <- ctx.infos.istack;
(fun cont_pos ->
if ctx.infos.istack <> ctx.infos.iloop then assert false;
List.iter (fun j -> j()) ctx.breaks;
List.iter (fun j -> j cont_pos) ctx.continues;
ctx.infos.iloop <- old_loop;
ctx.breaks <- old_breaks;
ctx.continues <- old_conts;
)
let gen_constant ctx c t p =
match c with
| TInt i ->
if Int32.compare i (-128l) > 0 && Int32.compare i 128l < 0 then
write ctx (A3SmallInt (Int32.to_int i))
else
write ctx (A3IntRef (lookup i ctx.ints));
| TFloat f ->
let f = float_of_string f in
write ctx (A3Float (lookup f ctx.floats));
| TString s ->
write ctx (A3String (lookup s ctx.strings));
| TBool b ->
write ctx (if b then A3True else A3False);
| TNull ->
write ctx A3Null;
(match classify ctx t with
| KInt | KBool | KUInt | KFloat ->
Typer.error ("In Flash9, null can't be used as basic type " ^ s_type (print_context()) t) p
| x -> coerce ctx x)
| TThis ->
write ctx A3This
| TSuper ->
assert false
let no_value ctx retval =
(* does not push a null but still increment the stack like if
a real value was pushed *)
if retval then ctx.infos.istack <- ctx.infos.istack + 1
let gen_expr_ref = ref (fun _ _ _ -> assert false)
let gen_expr ctx e retval = (!gen_expr_ref) ctx e retval
let gen_access ctx e (forset : 'a) : 'a access =
match e.eexpr with
| TLocal i ->
gen_local_access ctx i e.epos forset
| TField (e,f) ->
let id = ident ctx f in
(match e.eexpr with
| TConst TThis when not ctx.in_static -> write ctx (A3FindPropStrict id)
| _ -> gen_expr ctx true e);
VId id
| TArray ({ eexpr = TLocal "__global__" },{ eexpr = TConst (TString s) }) ->
let path = (match List.rev (ExtString.String.nsplit s ".") with [] -> assert false | x :: l -> List.rev l, x) in
let id = type_path ctx path in
if is_set forset then write ctx A3GetGlobalScope;
VGlobal id
| TArray (e,eindex) ->
gen_expr ctx true e;
gen_expr ctx true eindex;
VArray
| TTypeExpr t ->
let id = type_path ctx ~getclass:true (t_path t) in
if is_set forset then write ctx A3GetGlobalScope;
VGlobal id
| _ ->
error e.epos
let rec gen_expr_content ctx retval e =
match e.eexpr with
| TConst c ->
gen_constant ctx c e.etype e.epos
| TThrow e ->
gen_expr ctx true e;
write ctx A3Throw;
no_value ctx retval;
| TParenthesis e ->
gen_expr ctx retval e
| TEnumField (e,s) ->
let id = type_path ctx ~getclass:true e.e_path in
write ctx (A3GetLex id);
write ctx (A3GetProp (ident ctx s));
| TObjectDecl fl ->
List.iter (fun (name,e) ->
write ctx (A3String (lookup name ctx.strings));
gen_expr ctx true e
) fl;
write ctx (A3Object (List.length fl))
| TArrayDecl el ->
List.iter (gen_expr ctx true) el;
write ctx (A3Array (List.length el))
| TBlock el ->
let rec loop = function
| [] ->
if retval then write ctx A3Null
| [e] ->
ctx.curblock <- [];
gen_expr ctx retval e
| e :: l ->
ctx.curblock <- l;
gen_expr ctx false e;
loop l
in
let b = open_block ctx [] retval in
loop el;
b();
| TVars vl ->
List.iter (fun (v,t,ei) ->
define_local ctx v t ctx.curblock;
(match ei with
| None -> ()
| Some e ->
let acc = gen_local_access ctx v e.epos Write in
gen_expr ctx true e;
setvar ctx acc false)
) vl
| TReturn None ->
write ctx A3RetVoid;
no_value ctx retval
| TReturn (Some e) ->
gen_expr ctx true e;
write ctx A3Ret;
no_value ctx retval
| TField _
| TLocal _
| TTypeExpr _ ->
getvar ctx (gen_access ctx e Read)
| TArray _ ->
getvar ctx (gen_access ctx e Read);
coerce ctx (classify ctx e.etype)
| TBinop (op,e1,e2) ->
gen_binop ctx retval op e1 e2
| TCall (e,el) ->
gen_call ctx e el
| TNew (c,_,pl) ->
let id = type_path ctx c.cl_path in
write ctx (A3FindPropStrict id);
List.iter (gen_expr ctx true) pl;
write ctx (A3ConstructProperty (id,List.length pl))
| TFunction f ->
write ctx (A3Function (generate_function ctx f true))
| TIf (e0,e1,e2) ->
gen_expr ctx true e0;
let branch = begin_branch ctx in
let j = jump ctx J3False in
gen_expr ctx retval e1;
let t = classify ctx e.etype in
if retval && classify ctx e1.etype <> t then coerce ctx t;
(match e2 with
| None -> j()
| Some e ->
(* two expresssions, but one per branch *)
if retval then ctx.infos.istack <- ctx.infos.istack - 1;
let jend = jump ctx J3Always in
j();
gen_expr ctx retval e;
if retval && classify ctx e.etype <> t then coerce ctx t;
jend());
branch();
| TWhile (econd,e,flag) ->
let jstart = (match flag with NormalWhile -> (fun()->()) | DoWhile -> jump ctx J3Always) in
let end_loop = begin_loop ctx in
let branch = begin_branch ctx in
let continue_pos = ctx.infos.ipos + jsize in
let here, loop = jump_back ctx in
here();
gen_expr ctx true econd;
let jend = jump ctx J3False in
jstart();
gen_expr ctx false e;
loop J3Always;
jend();
branch();
end_loop continue_pos;
if retval then write ctx A3Null
| TUnop (op,flag,e) ->
gen_unop ctx retval op flag e
| TTry (e2,cases) ->
if ctx.infos.istack <> 0 then Typer.error "Cannot compile try/catch as a right-side expression in Flash9" e.epos;
let branch = begin_branch ctx in
let p = ctx.infos.ipos in
gen_expr ctx retval e2;
let pend = ctx.infos.ipos in
let jend = jump ctx J3Always in
let rec loop ncases = function
| [] -> []
| (ename,t,e) :: l ->
let b = open_block ctx [e] retval in
ctx.trys <- {
tr_pos = p;
tr_end = pend;
tr_catch_pos = ctx.infos.ipos;
tr_type = t;
} :: ctx.trys;
ctx.infos.istack <- ctx.infos.istack + 1;
if ctx.infos.imax < ctx.infos.istack then ctx.infos.imax <- ctx.infos.istack;
write ctx A3This;
write ctx A3Scope;
write ctx (A3Reg (match ctx.try_scope_reg with None -> assert false | Some r -> r.rid));
write ctx A3Scope;
define_local ctx ename t [e];
let r = (try match PMap.find ename ctx.locals with LReg r -> Some (alloc_reg ctx r.rtype) | _ -> None with Not_found -> assert false) in
(match r with None -> () | Some r -> set_reg ctx r);
let acc = gen_local_access ctx ename e.epos Write in
(match r with None -> () | Some r -> write ctx (A3Reg r.rid));
setvar ctx acc false;
gen_expr ctx retval e;
b();
if retval then ctx.infos.istack <- ctx.infos.istack - 1;
match l with
| [] -> []
| _ ->
let j = jump ctx J3Always in
j :: loop (ncases + 1) l
in
let loops = loop (List.length ctx.trys) cases in
List.iter (fun j -> j()) loops;
branch();
jend()
| TFor (v,t,it,e) ->
gen_expr ctx true it;
let r = alloc_reg ctx KDynamic in
set_reg ctx r;
let branch = begin_branch ctx in
let b = open_block ctx [e] retval in
define_local ctx v t [e];
let end_loop = begin_loop ctx in
let continue_pos = ctx.infos.ipos + jsize in
let here, start = jump_back ctx in
here();
write ctx (A3Reg r.rid);
write ctx (A3CallProperty (ident ctx "hasNext",0));
let jend = jump ctx J3False in
let acc = gen_local_access ctx v e.epos Write in
write ctx (A3Reg r.rid);
write ctx (A3CallProperty (ident ctx "next",0));
setvar ctx acc false;
gen_expr ctx false e;
start J3Always;
end_loop continue_pos;
jend();
if retval then getvar ctx (gen_local_access ctx v e.epos Read);
b();
branch();
free_reg ctx r;
| TBreak ->
pop ctx (ctx.infos.istack - ctx.infos.iloop);
ctx.breaks <- jump ctx J3Always :: ctx.breaks;
no_value ctx retval
| TContinue ->
pop ctx (ctx.infos.istack - ctx.infos.iloop);
let op = DynArray.length ctx.code in
write ctx (A3Jump (J3Always,-4));
let p = ctx.infos.ipos in
ctx.continues <- (fun target -> DynArray.set ctx.code op (A3Jump (J3Always,target - p))) :: ctx.continues;
no_value ctx retval
| TSwitch (e0,el,eo) ->
let t = classify ctx e.etype in
let r = alloc_reg ctx (classify ctx e0.etype) in
gen_expr ctx true e0;
set_reg ctx r;
let branch = begin_branch ctx in
let prev = ref (fun () -> ()) in
let jend = List.map (fun (vl,e) ->
(!prev)();
let rec loop = function
| [] ->
assert false
| [v] ->
write ctx (A3Reg r.rid);
gen_expr ctx true v;
prev := jump ctx J3Neq;
| v :: l ->
write ctx (A3Reg r.rid);
gen_expr ctx true v;
let j = jump ctx J3Eq in
loop l;
j()
in
loop vl;
gen_expr ctx retval e;
if retval then begin
if classify ctx e.etype <> t then coerce ctx t;
ctx.infos.istack <- ctx.infos.istack - 1;
end;
jump ctx J3Always
) el in
(!prev)();
free_reg ctx r;
(match eo with
| None ->
if retval then begin
write ctx A3Null;
coerce ctx t;
end;
| Some e ->
gen_expr ctx retval e;
if retval && classify ctx e.etype <> t then coerce ctx t;
);
List.iter (fun j -> j()) jend;
branch();
| TMatch (e0,_,cases,def) ->
let t = classify ctx e.etype in
let rparams = alloc_reg ctx KDynamic in
let rtag = alloc_reg ctx KDynamic in
gen_expr ctx true e0;
write ctx A3Dup;
write ctx (A3GetProp (ident ctx "tag"));
set_reg ctx rtag;
write ctx (A3GetProp (ident ctx "params"));
set_reg ctx rparams;
let branch = begin_branch ctx in
let prev = ref (fun () -> ()) in
let jend = List.map (fun (cl,params,e) ->
(!prev)();
let rec loop = function
| [] ->
assert false
| [tag] ->
write ctx (A3Reg rtag.rid);
write ctx (A3String (lookup tag ctx.strings));
prev := jump ctx J3Neq;
| tag :: l ->
write ctx (A3Reg rtag.rid);
write ctx (A3String (lookup tag ctx.strings));
let j = jump ctx J3Eq in
loop l;
j()
in
loop cl;
let b = open_block ctx [e] retval in
(match params with
| None -> ()
| Some l ->
let p = ref (-1) in
List.iter (fun (name,t) ->
incr p;
match name with
| None -> ()
| Some v ->
define_local ctx v t [e];
let acc = gen_local_access ctx v e.epos Write in
write ctx (A3Reg rparams.rid);
write ctx (A3SmallInt !p);
getvar ctx VArray;
setvar ctx acc false
) l
);
gen_expr ctx retval e;
b();
if retval then begin
ctx.infos.istack <- ctx.infos.istack - 1;
if classify ctx e.etype <> t then coerce ctx t;
end;
jump ctx J3Always;
) cases in
(!prev)();
(match def with
| None ->
if retval then begin
write ctx A3Null;
coerce ctx t;
end;
| Some e ->
gen_expr ctx retval e;
if retval && classify ctx e.etype <> t then coerce ctx t;
);
List.iter (fun j -> j()) jend;
branch();
free_reg ctx rtag;
free_reg ctx rparams
and gen_call ctx e el =
match e.eexpr , el with
| TLocal "__is__" , [e;t] ->
gen_expr ctx true e;
gen_expr ctx true t;
write ctx (A3Op A3OIs)
| TLocal "__as__" , [e;t] ->
gen_expr ctx true e;
gen_expr ctx true t;
write ctx (A3Op A3OAs)
| TLocal "__keys__" , [e] ->
let racc = alloc_reg ctx (KType (type_path ctx ([],"Array"))) in
let rcounter = alloc_reg ctx KInt in
let rtmp = alloc_reg ctx KDynamic in
write ctx (A3SmallInt 0);
set_reg ctx rcounter;
write ctx (A3Array 0);
set_reg ctx racc;
gen_expr ctx true e;
set_reg ctx rtmp;
let start, loop = jump_back ctx in
write ctx (A3Reg racc.rid);
write ctx (A3Reg rtmp.rid);
write ctx (A3Reg rcounter.rid);
write ctx A3ForIn;
write ctx (A3CallPropVoid (ident ctx "push",1));
start();
write ctx (A3Next (rtmp.rid,rcounter.rid));
loop J3True;
write ctx (A3Reg racc.rid);
free_reg ctx rtmp;
free_reg ctx rcounter;
free_reg ctx racc;
| TLocal "__new__" , e :: el ->
gen_expr ctx true e;
List.iter (gen_expr ctx true) el;
write ctx (A3Construct (List.length el))
| TLocal "__delete__" , [o;f] ->
gen_expr ctx true o;
gen_expr ctx true f;
write ctx (A3DeleteProp (lookup (A3MMultiNameLate ctx.gpublic) ctx.names));
| TLocal "__unprotect__" , [e] ->