forked from paulfitz/haxe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genswf8.ml
1590 lines (1513 loc) · 42.6 KB
/
genswf8.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
(*
* Copyright (C)2005-2013 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*)
open Swf
open Ast
open Type
open Common
type register =
| NoReg
| Reg of int
type context = {
stack : Codegen.stack_context;
(* segs *)
mutable segs : (actions * (string * bool, int) Hashtbl.t) list;
(* code *)
mutable opcodes : actions;
mutable code_pos : int;
mutable stack_size : int;
mutable opt_push : bool;
mutable ident_count : int;
mutable ident_size : int;
(* management *)
com : Common.context;
packages : (string list,unit) Hashtbl.t;
flash6 : bool;
mutable idents : (string * bool,int) Hashtbl.t;
mutable movieclips : path list;
mutable inits : texpr list;
mutable statics : (tclass * bool * string * texpr) list;
mutable regs : (string,register) PMap.t;
mutable reg_count : int;
mutable reg_max : int;
mutable fun_stack : int;
mutable curclass : tclass;
mutable curmethod : (string * bool);
mutable fun_pargs : (int * bool list) list;
mutable static_init : bool;
mutable extern_boot : bool;
(* loops *)
mutable breaks : (unit -> unit) list;
mutable continues : (int -> unit) list;
mutable loop_stack : int;
mutable in_loop : bool;
}
let invalid_expr p = error "Invalid expression" p
let stack_error p = error "Stack error" p
let protect_all = ref true
(* -------------------------------------------------------------- *)
(* Bytecode Helpers *)
type tmp_variable =
| TmpReg of int
| TmpVar of string * int
type kind =
| VarReg of int
| VarStr
| VarObj
| VarClosure
| VarVolatile
type push_style =
| VStr of string * bool
| VInt of int
| VInt32 of int32
| VFloat of float
| VReg of int
| VThis
| VNull
| VUndefined
| VSuper
let stack_delta = function
| APush l -> List.length l
| ASetReg _ -> 0
| AAdd | ADivide | ASubtract | AMultiply | AMod | AStringAdd -> -1
| AAnd | AOr | AXor | AShl | AShr | AAsr -> -1
| ACompare | AGreater -> -1
| AEval | ANot | AJump _ | AToInt | AToNumber | AToString | ATry _ | ASwap -> 0
| ACondJump _ -> -1
| AEqual | APhysEqual -> -1
| ANew -> -1 (** only if 0 params **)
| AObject | AInitArray -> 0 (** calculated outside **)
| ASet -> -2
| APop -> -1
| AFunction _ | AFunction2 _ -> 1
| ADup -> 1
| AWith _ -> -1
| AObjGet -> -1
| AObjSet -> -3
| ALocalVar -> -1
| ALocalAssign -> -2
| AReturn -> -1
| AGetURL2 _ -> -2
| ADeleteObj | AInstanceOf | ACast -> -1
| AExtends | AImplements -> -2
| AEnum2 | ATrace | AThrow -> -1
| AGetTimer -> 1
| AIncrement | ADecrement | AChr | AOrd | ARandom | ADelete | ATypeOf | ATargetPath -> 0
| AObjCall | ACall | ANewMethod -> assert false
| AStringPool _ -> 0
| AFSCommand2 -> 0
| op -> failwith ("Unknown stack delta for " ^ (ActionScript.action_string (fun _ -> "") 0 op))
let overflow ctx =
failwith ("In or near the method " ^ s_type_path ctx.curclass.cl_path ^ "." ^ fst ctx.curmethod ^
" too much code is causing an overflow that can't be handled by the SWF format. " ^
"Please split your code in several methods so it can be correctly compiled.")
let write ctx op =
let write b op =
DynArray.add ctx.opcodes op;
ctx.code_pos <- ctx.code_pos + 1;
ctx.stack_size <- ctx.stack_size + stack_delta op;
ctx.opt_push <- b
in
match op with
| APush l when ctx.opt_push ->
(match DynArray.last ctx.opcodes with
| (APush l2) as a ->
ctx.code_pos <- ctx.code_pos - 1;
ctx.stack_size <- ctx.stack_size - stack_delta a;
DynArray.delete_last ctx.opcodes;
write true (APush (l2 @ l))
| _ ->
assert false)
| APush _ ->
write true op
| _ ->
write false op
let call ctx kind n =
let op , n = (match kind with
| VarReg r ->
write ctx (APush [PReg r;PUndefined]);
AObjCall , n + 2
| VarStr ->
ACall , n + 1
| VarClosure | VarObj ->
AObjCall , n + 2
| VarVolatile ->
assert false
) in
DynArray.add ctx.opcodes op;
ctx.opt_push <- false;
ctx.code_pos <- ctx.code_pos + 1;
ctx.stack_size <- ctx.stack_size - n
let new_call ctx kind n =
let op , n = (match kind with
| VarReg r ->
write ctx (APush [PReg r;PUndefined]);
ANewMethod , n + 2
| VarStr ->
ANew , n + 1
| VarClosure | VarObj ->
ANewMethod , n + 2
| VarVolatile ->
assert false
) in
DynArray.add ctx.opcodes op;
ctx.opt_push <- false;
ctx.code_pos <- ctx.code_pos + 1;
ctx.stack_size <- ctx.stack_size - n
let unprotect a = !protect_all || a = "" || a = "_" || (a.[0] = '_' && a.[1] != '_')
let rec is_protected_path path ext =
match path with
| ["flash"] , "Boot" | ["flash"] , "Lib" -> false
| "flash" :: _ , _ | [] , "flash" -> ext
| [] , "Array" | [] , "Math" | [] , "Date" | [] , "String" | [] , "Bool" -> true
| [] , "Int" | [] , "Float" -> true
| "_global" :: l , n -> is_protected_path (l,n) ext
| _ -> false
let rec is_protected ctx ?(stat=false) t field =
match follow t with
| TInst (c,_) ->
let rec loop c =
(is_protected_path c.cl_path c.cl_extern && PMap.mem field (if stat then c.cl_statics else c.cl_fields))
|| List.exists (fun (i,_) -> i.cl_path = (["mt"],"Protect") || (not stat && loop i)) c.cl_implements
|| (not stat && match c.cl_super with None -> false | Some (c,_) -> loop c)
in
loop c
| TAnon a ->
(match !(a.a_status) with
| Statics c -> is_protected ctx ~stat:true (TInst (c,[])) field
| _ -> false)
| _ -> false
let push ctx items =
write ctx (APush (List.map (fun i ->
match i with
| VStr (str,flag) ->
let flag = if flag || unprotect str then true else flag in
let n = (try
Hashtbl.find ctx.idents (str,flag)
with Not_found ->
let n = ctx.ident_count in
ctx.ident_count <- n + 1;
ctx.ident_size <- ctx.ident_size + 1 + String.length str;
Hashtbl.add ctx.idents (str,flag) n;
n
) in
if n <= 0xFF then
PStack n
else
PStack2 n
| VInt n ->
PInt (Int32.of_int n)
| VInt32 n ->
PInt n
| VFloat f ->
PDouble f
| VThis ->
PReg 1
| VNull ->
PNull
| VUndefined ->
PUndefined
| VSuper ->
PReg 2
| VReg n ->
PReg n
) items))
let pop ctx n dec =
let rec loop n =
if n <> 0 then begin
write ctx APop;
loop (n - 1)
end;
in
if n < 0 then assert false;
let old_s = ctx.stack_size in
loop n;
if not dec then ctx.stack_size <- old_s
let cjmp ctx =
write ctx (ACondJump 0);
let start_pos = ctx.code_pos in
let op_pos = DynArray.length ctx.opcodes - 1 in
(fun() ->
let delta = ctx.code_pos - start_pos in
DynArray.set ctx.opcodes op_pos (ACondJump delta);
ctx.opt_push <- false
)
let jmp ctx =
write ctx (AJump 0);
let start_pos = ctx.code_pos in
let op_pos = DynArray.length ctx.opcodes - 1 in
(fun() ->
let delta = ctx.code_pos - start_pos in
DynArray.set ctx.opcodes op_pos (AJump delta);
ctx.opt_push <- false
)
let pos ctx =
ctx.opt_push <- false;
let start_pos = ctx.code_pos in
(fun ~cond ->
let delta = start_pos - (ctx.code_pos + 1) in
write ctx (if cond then ACondJump delta else AJump delta);
)
let jmp_pos ctx cond =
write ctx (AJump 0);
let start_pos = ctx.code_pos in
let op_pos = DynArray.length ctx.opcodes - 1 in
(fun pos ->
let delta = pos - start_pos in
DynArray.set ctx.opcodes op_pos (if cond then ACondJump delta else AJump delta);
ctx.opt_push <- false
)
let init_array ctx n =
push ctx [VInt n];
write ctx AInitArray;
ctx.stack_size <- ctx.stack_size - n
let setvar ?(retval=false) ctx = function
| VarReg (-1) -> assert false (** true, false, null **)
| VarReg n -> write ctx (ASetReg n); if not retval then write ctx APop
| VarStr
| VarObj
| VarClosure as s ->
if retval then write ctx (ASetReg 0);
write ctx (if s = VarStr then ASet else AObjSet);
if retval then push ctx [VReg 0]
| VarVolatile ->
if retval then write ctx (ASetReg 0);
init_array ctx 1;
write ctx AObjSet;
if retval then push ctx [VReg 0]
let getvar ctx = function
| VarReg (-1) -> () (** true, false, null **)
| VarReg n -> push ctx [VReg n]
| VarStr -> write ctx AEval
| VarObj -> write ctx AObjGet
| VarClosure ->
push ctx [VInt 2; VStr ("@closure",false)];
call ctx VarStr 2
| VarVolatile ->
write ctx AObjGet;
push ctx [VInt 0];
write ctx AObjGet
let gen_path ctx ?(protect=false) (p,t) is_extern =
let flag = is_protected_path (p,t) is_extern in
match p with
| [] ->
push ctx [VStr (t,flag)];
VarStr
| x :: l ->
push ctx [VStr (x,protect && flag)];
write ctx AEval;
List.iter (fun x ->
push ctx [VStr (x,protect && flag)];
write ctx AObjGet;
) l;
push ctx [VStr (t,flag)];
VarObj
let begin_func ctx need_super need_args args =
if ctx.flash6 then
let f = {
f_name = "";
Swf.f_args = List.map snd args;
f_codelen = 0;
} in
write ctx (AFunction f);
let start_pos = ctx.code_pos in
let old_stack = ctx.fun_stack in
ctx.fun_stack <- ctx.stack_size;
(fun() ->
let delta = ctx.code_pos - start_pos in
f.f_codelen <- delta;
let codesize = ActionScript.jump_index_to_size ctx.opcodes (start_pos-1) delta in
if codesize >= 1 lsl 16 then overflow ctx;
if ctx.fun_stack <> ctx.stack_size then assert false;
ctx.fun_stack <- old_stack;
)
else
let default_flags = ThisRegister :: (if need_args then [] else [ArgumentsNoVar]) in
let f = {
f2_name = "";
f2_args = args;
f2_codelen = 0;
f2_nregs = 0;
f2_flags = (if need_super then SuperRegister :: default_flags else SuperNoVar :: default_flags);
} in
write ctx (AFunction2 f);
let start_pos = ctx.code_pos in
let old_stack = ctx.fun_stack in
let old_rmax = ctx.reg_max in
let old_sinit = ctx.static_init in
ctx.fun_stack <- ctx.stack_size;
ctx.reg_max <- ctx.reg_count;
ctx.static_init <- false;
(fun() ->
let delta = ctx.code_pos - start_pos in
f.f2_codelen <- delta;
f.f2_nregs <- ctx.reg_max + 1;
let codesize = ActionScript.jump_index_to_size ctx.opcodes (start_pos-1) delta in
if codesize >= 1 lsl 16 then overflow ctx;
if ctx.fun_stack <> ctx.stack_size then assert false;
ctx.fun_stack <- old_stack;
ctx.reg_max <- old_rmax;
ctx.static_init <- old_sinit;
)
let open_block ctx =
let old_regs = ctx.regs in
let old_rcount = ctx.reg_count in
(fun() ->
ctx.regs <- old_regs;
ctx.reg_count <- old_rcount;
)
let begin_loop ctx =
let old_breaks = ctx.breaks in
let old_cont = ctx.continues in
let old_stack = ctx.loop_stack in
let old_loop = ctx.in_loop in
ctx.breaks <- [];
ctx.continues <- [];
ctx.loop_stack <- ctx.stack_size;
ctx.in_loop <- true;
(fun pos ->
List.iter (fun f -> f()) ctx.breaks;
List.iter (fun f -> f pos) ctx.continues;
ctx.breaks <- old_breaks;
ctx.continues <- old_cont;
ctx.loop_stack <- old_stack;
ctx.in_loop <- old_loop;
)
let alloc_reg ctx =
ctx.reg_count <- ctx.reg_count + 1;
if ctx.reg_count > ctx.reg_max then ctx.reg_max <- ctx.reg_count;
ctx.reg_count
let free_reg ctx r p =
if r <> ctx.reg_count then stack_error p;
ctx.reg_count <- ctx.reg_count - 1
let segment ctx =
ctx.segs <- (ctx.opcodes,ctx.idents) :: ctx.segs;
ctx.opcodes <- DynArray.create();
ctx.idents <- Hashtbl.create 0;
ctx.ident_count <- 0;
ctx.ident_size <- 0;
ctx.code_pos <- 0;
write ctx (AStringPool [])
(* -------------------------------------------------------------- *)
(* Generation Helpers *)
let define_var ctx v ef =
if ctx.flash6 || v.v_capture || ctx.static_init then begin
push ctx [VStr (v.v_name,false)];
ctx.regs <- PMap.add v.v_name NoReg ctx.regs;
match ef with
| None ->
write ctx ALocalVar
| Some f ->
f();
write ctx ALocalAssign
end else begin
let r = alloc_reg ctx in
ctx.regs <- PMap.add v.v_name (Reg r) ctx.regs;
match ef with
| None -> ()
| Some f ->
f();
setvar ctx (VarReg r)
end
let alloc_tmp ctx =
let r = alloc_reg ctx in
if ctx.flash6 then
let name = "$" ^ string_of_int r in
define_var ctx (alloc_var name t_dynamic) None;
TmpVar (name,r);
else
TmpReg r
let get_tmp ctx = function
| TmpVar (v,_) ->
push ctx [VStr (v,false)];
write ctx AEval;
| TmpReg r ->
push ctx [VReg r]
let set_tmp ctx = function
| TmpVar (v,_) ->
write ctx ADup;
push ctx [VStr (v,false)];
write ctx ASwap;
write ctx ASet
| TmpReg r ->
write ctx (ASetReg r)
let free_tmp ctx v p =
match v with
| TmpVar (v,r) ->
ctx.regs <- PMap.remove v ctx.regs;
free_reg ctx r p
| TmpReg r ->
free_reg ctx r p
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.stack_size <- ctx.stack_size + 1
let gen_try ctx =
let tdata = {
tr_style = TryRegister 0;
tr_trylen = 0;
tr_catchlen = None;
tr_finallylen = None;
} in
write ctx (ATry tdata);
let start = ctx.code_pos in
(fun() ->
let jump_end = jmp ctx in
tdata.tr_trylen <- ctx.code_pos - start;
let start = ctx.code_pos in
(fun() ->
if ctx.code_pos <> start then tdata.tr_catchlen <- Some (ctx.code_pos - start);
jump_end()
)
)
(* -------------------------------------------------------------- *)
(* Generation *)
let rec gen_big_string ctx s =
let len = String.length s in
let max = 65000 in
ctx.opt_push <- false;
if len <= max then
write ctx (APush [PString s])
else begin
write ctx (APush [PString (String.sub s 0 max)]);
gen_big_string ctx (String.sub s max (len - max));
write ctx AAdd;
end
let rec gen_constant ctx c p =
match c with
| TInt i -> push ctx [VInt32 i]
| TFloat s -> push ctx [VFloat (try float_of_string s with _ -> invalid_expr p)]
| TString s ->
if String.contains s '\000' then error "A String cannot contain \\0 characters" p;
push ctx [VStr (s,true)]
| TBool b -> write ctx (APush [PBool b])
| TNull -> push ctx [VNull]
| TThis
| TSuper -> assert false
let access_local ctx s =
match (try PMap.find s ctx.regs , false with Not_found -> NoReg, s <> "Enum") with
| NoReg , flag ->
push ctx [VStr (s,flag)];
VarStr
| Reg r , _ ->
VarReg r
let rec gen_access ?(read_write=false) ctx forcall e =
match e.eexpr with
| TConst TSuper ->
(* for superconstructor *)
if ctx.flash6 then begin
push ctx [VStr ("super",true)];
VarStr
end else if forcall then begin
push ctx [VSuper];
write ctx (APush [PUndefined]);
VarObj
end else
VarReg 2
| TConst TThis ->
if ctx.flash6 then begin
push ctx [VStr ("this",true)];
VarStr
end else
VarReg 1
| TLocal { v_name = "__arguments__" } ->
push ctx [VStr ("arguments",true)];
VarStr
| TLocal v ->
access_local ctx v.v_name
| TField (e,FClosure (_,{ cf_name = f })) ->
gen_expr ctx true e;
if read_write then assert false;
push ctx [VStr (f,is_protected ctx e.etype f)];
VarClosure
| TEnumParameter(e,_,i) ->
gen_expr ctx true e;
push ctx [VInt (i + 2)];
VarObj
| TField (e2,f) ->
gen_expr ctx true e2;
if read_write then write ctx ADup;
let f = field_name f in
let p = VStr (f,is_protected ctx e2.etype f) in
push ctx [p];
if read_write then begin
write ctx ASwap;
push ctx [p];
end;
if not !protect_all && Codegen.is_volatile e.etype then
VarVolatile
else
VarObj
| TArray (ea,eb) ->
if read_write then
try
let r = (match ea.eexpr with TLocal l -> (match PMap.find l.v_name ctx.regs with Reg r -> r | _ -> raise Not_found) | _ -> raise Not_found) in
push ctx [VReg r];
gen_expr ctx true eb;
write ctx ADup;
push ctx [VReg r];
write ctx ASwap;
with Not_found ->
gen_expr ctx true eb;
gen_expr ctx true ea;
write ctx (ASetReg 0);
write ctx ASwap;
write ctx ADup;
push ctx [VReg 0];
write ctx ASwap;
else begin
gen_expr ctx true ea;
gen_expr ctx true eb;
end;
VarObj
| TTypeExpr t ->
(match t with
| TClassDecl c -> gen_path ctx c.cl_path c.cl_extern
| TEnumDecl e -> gen_path ctx e.e_path false
| TAbstractDecl a -> gen_path ctx a.a_path false
| TTypeDecl _ -> assert false)
| _ ->
if not forcall then invalid_expr e.epos;
gen_expr ctx true e;
write ctx (APush [PUndefined]);
VarObj
and gen_access_rw ctx e =
match e.eexpr with
| TField ({ eexpr = TLocal _ },_) | TArray ({ eexpr = TLocal _ },{ eexpr = TConst _ }) | TArray ({ eexpr = TLocal _ },{ eexpr = TLocal _ }) ->
ignore(gen_access ctx false e);
gen_access ctx false e
| TField _ | TArray _ ->
gen_access ~read_write:true ctx false e
| _ ->
ignore(gen_access ctx false e);
gen_access ctx false e
and gen_try_catch ctx retval e catchs =
let start_try = gen_try ctx in
gen_expr ctx retval e;
let end_try = start_try() in
let end_throw = ref true in
let jumps = List.map (fun (v,e) ->
if not !end_throw then
(fun () -> ())
else let t = (match follow v.v_type with
| TEnum (e,_) -> Some (TEnumDecl e)
| TInst (c,_) -> Some (TClassDecl c)
| TAbstract (a,_) -> Some (TAbstractDecl a)
| TFun _
| TLazy _
| TType _
| TAnon _ ->
assert false
| TMono _
| TDynamic _ ->
None
) in
let next_catch = (match t with
| None ->
end_throw := false;
(fun() -> ())
| Some t ->
getvar ctx (gen_access ctx false (mk (TTypeExpr t) (mk_mono()) e.epos));
push ctx [VReg 0; VInt 2; VStr ("@instanceof",false)];
call ctx VarStr 2;
write ctx ANot;
cjmp ctx
) in
let block = open_block ctx in
define_var ctx v (Some (fun() -> push ctx [VReg 0]));
gen_expr ctx retval e;
block();
if retval then ctx.stack_size <- ctx.stack_size - 1;
let j = jmp ctx in
next_catch();
j
) catchs in
if !end_throw && catchs <> [] then begin
push ctx [VReg 0];
write ctx AThrow;
end;
List.iter (fun j -> j()) jumps;
end_try()
and gen_switch ctx retval e cases def =
gen_expr ctx true e;
let r = alloc_tmp ctx in
set_tmp ctx r;
let first = ref true in
let dispatch = List.map (fun (el,x) ->
List.map (fun e ->
if !first then first := false else get_tmp ctx r;
gen_expr ctx true e;
write ctx AEqual;
cjmp ctx
) el , x
) cases in
if !first then write ctx APop;
(match def with
| None -> if retval then push ctx [VNull]
| Some e -> gen_expr ctx retval e);
let jend = jmp ctx in
let jends = List.map (fun (jl,e) ->
List.iter (fun j -> j()) jl;
gen_expr ctx retval e;
if retval then ctx.stack_size <- ctx.stack_size - 1;
jmp ctx;
) dispatch in
jend();
free_tmp ctx r e.epos;
List.iter (fun j -> j()) jends
and gen_match ctx retval e cases def =
gen_expr ctx true e;
let renum = alloc_tmp ctx in
set_tmp ctx renum;
push ctx [VInt 1];
write ctx AObjGet;
let rtag = alloc_tmp ctx in
set_tmp ctx rtag;
let first = ref true in
let dispatch = List.map (fun (cl,params,e) ->
List.map (fun c ->
if !first then first := false else get_tmp ctx rtag;
push ctx [VInt c];
write ctx APhysEqual;
cjmp ctx
) cl, params, e
) cases in
if !first then write ctx APop;
free_tmp ctx rtag e.epos;
(match def with
| None -> if retval then push ctx [VNull]
| Some e -> gen_expr ctx retval e);
let jend = jmp ctx in
let jends = List.map (fun (jl,args,e) ->
let regs = ctx.regs in
let nregs = ctx.reg_count in
List.iter (fun j -> j()) jl;
let n = ref 1 in
List.iter (fun v ->
incr n;
match v with
| None -> ()
| Some v ->
define_var ctx v (Some (fun() ->
get_tmp ctx renum;
push ctx [VInt !n];
write ctx AObjGet
))
) (match args with None -> [] | Some l -> l);
gen_expr ctx retval e;
if retval then ctx.stack_size <- ctx.stack_size - 1;
ctx.regs <- regs;
ctx.reg_count <- nregs;
jmp ctx;
) dispatch in
jend();
free_tmp ctx renum e.epos;
List.iter (fun j -> j()) jends
and gen_binop ctx retval op e1 e2 =
let gen a =
gen_expr ctx true e1;
gen_expr ctx true e2;
write ctx a
in
let make_op = function
| OpAdd -> AAdd
| OpMult -> AMultiply
| OpDiv -> ADivide
| OpSub -> ASubtract
| OpAnd -> AAnd
| OpOr -> AOr
| OpXor -> AXor
| OpShl -> AShl
| OpShr -> AShr
| OpUShr -> AAsr
| OpMod -> AMod
| _ -> assert false
in
match op with
| OpAssign ->
let k = gen_access ctx false e1 in
gen_expr ctx true e2;
setvar ~retval ctx k
| OpAssignOp op ->
let k = gen_access_rw ctx e1 in
getvar ctx k;
gen_expr ctx true e2;
write ctx (make_op op);
setvar ~retval ctx k
| OpAdd | OpMult | OpDiv | OpSub | OpAnd | OpOr | OpXor | OpShl | OpShr | OpUShr | OpMod ->
gen (make_op op)
| OpEq ->
gen AEqual
| OpNotEq ->
gen AEqual;
write ctx ANot
| OpGt -> gen AGreater
| OpGte ->
gen ACompare;
write ctx ANot
| OpLt -> gen ACompare
| OpLte ->
gen AGreater;
write ctx ANot
| OpBoolAnd ->
gen_expr ctx true e1;
write ctx ADup;
write ctx ANot;
let jump_end = cjmp ctx in
write ctx APop;
gen_expr ctx true e2;
jump_end()
| OpBoolOr ->
gen_expr ctx true e1;
write ctx ADup;
let jump_end = cjmp ctx in
write ctx APop;
gen_expr ctx true e2;
jump_end()
| OpInterval | OpArrow ->
(* handled by typer *)
assert false
and gen_unop ctx retval op flag e =
match op with
| Not ->
gen_expr ctx true e;
write ctx ANot
| Neg ->
push ctx [VInt 0];
gen_expr ctx true e;
write ctx ASubtract
| NegBits ->
gen_expr ctx true e;
push ctx [VInt (-1)];
write ctx AXor
| Increment
| Decrement ->
let k = gen_access_rw ctx e in
getvar ctx k;
(* store preincr value for later access *)
if retval && flag = Postfix then write ctx (ASetReg 0);
write ctx (match op with Increment -> AIncrement | Decrement -> ADecrement | _ -> assert false);
setvar ~retval:(retval && flag = Prefix) ctx k;
if retval && flag = Postfix then push ctx [VReg 0]
and gen_call ctx e el =
let loc = match e.eexpr with TLocal v -> v.v_name | _ -> "" in
match loc, el with
| "__instanceof__" , [e1;e2] ->
gen_expr ctx true e1;
gen_expr ctx true e2;
write ctx AInstanceOf
| "__typeof__" , [e] ->
gen_expr ctx true e;
write ctx ATypeOf
| "__delete__" , [e1; e2] ->
gen_expr ctx true e1;
gen_expr ctx true e2;
write ctx ADeleteObj
| "__random__" , [e] ->
gen_expr ctx true e;
write ctx ARandom
| "__trace__" , [e] ->
gen_expr ctx true e;
write ctx ATrace
| "__eval__" , [e] ->
gen_expr ctx true e;
write ctx AEval
| "__gettimer__", [] ->
write ctx AGetTimer
| "__undefined__", [] ->
push ctx [VUndefined]
| "__geturl__" , url :: target :: post ->
gen_expr ctx true url;
gen_expr ctx true target;
write ctx (AGetURL2 (match post with [] -> 0 | [{ eexpr = TConst (TString "GET") }] -> 1 | _ -> 2))
| "__new__", e :: el ->
let nargs = List.length el in
List.iter (gen_expr ctx true) (List.rev el);
push ctx [VInt nargs];
let k = gen_access ctx true e in
new_call ctx k nargs
| "__keys__", [e2]
| "__hkeys__", [e2] ->
let r = alloc_tmp ctx in
push ctx [VInt 0; VStr ("Array",true)];
new_call ctx VarStr 0;
set_tmp ctx r;
write ctx APop;
gen_expr ctx true e2;
write ctx AEnum2;
ctx.stack_size <- ctx.stack_size + 1; (* fake *)
let loop = pos ctx in
write ctx (ASetReg 0);
push ctx [VNull];
write ctx AEqual;
let jump_end = cjmp ctx in
if loc = "__hkeys__" then begin
push ctx [VInt 1; VInt 1; VReg 0; VStr ("substr",true)];
call ctx VarObj 1;
end else begin
push ctx [VReg 0];
end;
push ctx [VInt 1];
get_tmp ctx r;
push ctx [VStr ("push",true)];
call ctx VarObj 1;
write ctx APop;
loop false;
jump_end();
get_tmp ctx r;
free_tmp ctx r e2.epos;
| "__physeq__" , [e1;e2] ->
gen_expr ctx true e1;
gen_expr ctx true e2;
write ctx APhysEqual;
| "__unprotect__", [{ eexpr = TConst (TString s) }] ->
push ctx [VStr (s,false)]
| "__resources__", [] ->
let count = ref 0 in
Hashtbl.iter (fun name data ->
incr count;
push ctx [VStr ("name",false);VStr (name,true)];
(* if the data contains \0 or is not UTF8 valid, encode into bytes *)
(try
(try ignore(String.index data '\000'); raise Exit; with Not_found -> ());
UTF8.validate data;
push ctx [VStr ("str",false)];
gen_big_string ctx data;
with _ ->
push ctx [VStr ("data",false)];
gen_big_string ctx (Codegen.bytes_serialize data));
push ctx [VInt 2];
write ctx AObject;
ctx.stack_size <- ctx.stack_size - 4;
) ctx.com.resources;
init_array ctx !count
| "__FSCommand2__", l ->
let nargs = List.length l in
List.iter (gen_expr ctx true) (List.rev l);
push ctx [VInt nargs];
write ctx AFSCommand2;
ctx.stack_size <- ctx.stack_size - nargs
| _ , _ ->
let nargs = List.length el in
List.iter (gen_expr ctx true) (List.rev el);
push ctx [VInt nargs];
let k = gen_access ctx true e in
call ctx k nargs
and gen_expr_2 ctx retval e =
match e.eexpr with
| TConst TSuper
| TConst TThis
| TEnumParameter _
| TField _
| TArray _
| TLocal _
| TTypeExpr _ ->
getvar ctx (gen_access ctx false e)
| TConst c ->
gen_constant ctx c e.epos
| TParenthesis e | TMeta (_,e) ->
gen_expr ctx retval e
| TBlock el ->
let rec loop = function
| [] ->
if retval then push ctx [VNull]
| [e] ->
gen_expr ctx retval e
| e :: l ->
gen_expr ctx false e;
loop l
in
let b = open_block ctx in
loop el;
b()
| TVar (v,eo) ->