Skip to content

Commit 73aacd7

Browse files
committed
continue eleminate intermediate variables
1 parent 9bc7efb commit 73aacd7

File tree

112 files changed

+1715
-2000
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+1715
-2000
lines changed

jscomp/core/js_fun_env.ml

+3-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ let make ?immutable_mask n = {
6969
}
7070

7171
let no_tailcall x =
72-
x.immutable_mask = All_immutable_and_no_tail_call
72+
match x.immutable_mask with
73+
| All_immutable_and_no_tail_call -> []
74+
| Immutable_mask arr -> Array.to_list arr
7375

7476
let mark_unused t i =
7577
t.used_mask.(i) <- true

jscomp/core/js_fun_env.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type t
3737

3838
val make : ?immutable_mask:bool array -> int -> t
3939

40-
val no_tailcall : t -> bool
40+
val no_tailcall : t -> bool list
4141

4242
(* val is_empty : t -> bool *)
4343

jscomp/core/js_pass_tailcall_inline.ml

+24-21
Original file line numberDiff line numberDiff line change
@@ -55,37 +55,40 @@ let substitue_variables (map : Ident.t Map_ident.t) =
5555
since we have to guarantee that the one use
5656
case is substituted
5757
we already have this? in [defined_idents]
58+
59+
At this time, when tailcall happened, the parameter can be assigned
60+
for example {[
61+
function (_x,y){
62+
_x = u
63+
}
64+
]}
65+
if it is substitued, the assignment will align the value which is incorrect
5866
*)
5967

6068
let inline_call
61-
no_tailcall
62-
params (args : J.expression list) processed_blocks =
63-
if no_tailcall then
64-
let map, block =
69+
(immutable_list : bool list)
70+
params (args : J.expression list) processed_blocks =
71+
let map, block =
72+
if immutable_list = [] then
6573
Ext_list.fold_right2
6674
params args (Map_ident.empty, processed_blocks)
6775
(fun param arg (map,acc) ->
6876
match arg.expression_desc with
6977
| Var (Id id) ->
7078
Map_ident.add map param id, acc
7179
| _ ->
72-
map, S.define_variable ~kind:Variable param arg :: acc) in
73-
if Map_ident.is_empty map then block
74-
else (substitue_variables map) # block block
75-
else
76-
(*
77-
At this time, when tailcall happened, the parameter can be assigned
78-
for example {[
79-
function (_x,y){
80-
_x = u
81-
}
82-
]}
83-
if it is substitued, the assignment will align the value which is incorrect
84-
*)
85-
Ext_list.fold_right2
86-
params args processed_blocks
87-
(fun param arg acc ->
88-
S.define_variable ~kind:Variable param arg :: acc)
80+
map, S.define_variable ~kind:Variable param arg :: acc)
81+
else
82+
Ext_list.fold_right3
83+
params args immutable_list (Map_ident.empty, processed_blocks)
84+
(fun param arg mask (map,acc) ->
85+
match mask, arg.expression_desc with
86+
| true, Var (Id id) ->
87+
Map_ident.add map param id, acc
88+
| _ ->
89+
map, S.define_variable ~kind:Variable param arg :: acc) in
90+
if Map_ident.is_empty map then block
91+
else (substitue_variables map) # block block
8992

9093
(** There is a side effect when traversing dead code, since
9194
we assume that substitue a node would mark a node as dead node,

jscomp/ext/ext_list.ml

+14
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,20 @@ let rec fold_right2 l r acc f =
244244
f a0 b0 (f a1 b1 (f a2 b2 (f a3 b3 (f a4 b4 (fold_right2 arest brest acc f )))))
245245
| _, _ -> invalid_arg "Ext_list.fold_right2"
246246

247+
let rec fold_right3 l r last acc f =
248+
match l,r,last with
249+
| [],[],[] -> acc
250+
| [a0],[b0],[c0] -> f a0 b0 c0 acc
251+
| [a0;a1],[b0;b1],[c0; c1] -> f a0 b0 c0 (f a1 b1 c1 acc)
252+
| [a0;a1;a2],[b0;b1;b2],[c0;c1;c2] -> f a0 b0 c0 (f a1 b1 c1 (f a2 b2 c2 acc))
253+
| [a0;a1;a2;a3],[b0;b1;b2;b3],[c0;c1;c2;c3] ->
254+
f a0 b0 c0 (f a1 b1 c1 (f a2 b2 c2 (f a3 b3 c3 acc)))
255+
| [a0;a1;a2;a3;a4], [b0;b1;b2;b3;b4], [c0;c1;c2;c3;c4] ->
256+
f a0 b0 c0 (f a1 b1 c1 (f a2 b2 c2 (f a3 b3 c3 (f a4 b4 c4 acc))))
257+
| a0::a1::a2::a3::a4::arest, b0::b1::b2::b3::b4::brest, c0::c1::c2::c3::c4::crest ->
258+
f a0 b0 c0 (f a1 b1 c1 (f a2 b2 c2 (f a3 b3 c3 (f a4 b4 c4 (fold_right3 arest brest crest acc f )))))
259+
| _, _, _ -> invalid_arg "Ext_list.fold_right2"
260+
247261
let rec map2 l r f =
248262
match l,r with
249263
| [],[] -> []

jscomp/ext/ext_list.mli

+9
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ val fold_right2 :
9595
'c ->
9696
('a -> 'b -> 'c -> 'c) -> 'c
9797

98+
val fold_right3 :
99+
'a list ->
100+
'b list ->
101+
'c list ->
102+
'd ->
103+
('a -> 'b -> 'c -> 'd -> 'd) ->
104+
'd
105+
106+
98107
val map2 :
99108
'a list ->
100109
'b list ->

jscomp/test/ast_js_mapper_test.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,10 @@ tFromJs({
4141

4242
function searchForSureExists(xs, k) {
4343
var _i = 0;
44-
var xs$1 = xs;
45-
var k$1 = k;
4644
while(true) {
4745
var i = _i;
48-
var match = xs$1[i];
49-
if (match[0] === k$1) {
46+
var match = xs[i];
47+
if (match[0] === k) {
5048
return match[1];
5149
}
5250
_i = i + 1 | 0;

jscomp/test/ext_array_test.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,9 @@ function rfind_and_split(arr, cmp, v) {
214214
function find_with_index(arr, cmp, v) {
215215
var len = arr.length;
216216
var _i = 0;
217-
var len$1 = len;
218217
while(true) {
219218
var i = _i;
220-
if (i >= len$1) {
219+
if (i >= len) {
221220
return -1;
222221
}
223222
if (Curry._2(cmp, arr[i], v)) {

jscomp/test/ext_string_test.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -352,15 +352,13 @@ function tail_from(s, x) {
352352
function digits_of_str(s, offset, x) {
353353
var _i = 0;
354354
var _acc = 0;
355-
var s$1 = s;
356-
var x$1 = x;
357355
while(true) {
358356
var acc = _acc;
359357
var i = _i;
360-
if (i >= x$1) {
358+
if (i >= x) {
361359
return acc;
362360
}
363-
_acc = (Caml_int32.imul(10, acc) + Caml_string.get(s$1, offset + i | 0) | 0) - 48 | 0;
361+
_acc = (Caml_int32.imul(10, acc) + Caml_string.get(s, offset + i | 0) | 0) - 48 | 0;
364362
_i = i + 1 | 0;
365363
continue ;
366364
};

0 commit comments

Comments
 (0)