Skip to content

Commit f359a56

Browse files
committed
finish imul, add, sub support -- fix print ~ operator (#226)
1 parent e5f1693 commit f359a56

File tree

128 files changed

+1151
-1078
lines changed

Some content is hidden

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

128 files changed

+1151
-1078
lines changed

jscomp/ext_ident.ml

+1
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ let convert (name : string) =
225225
| '|' -> Buffer.add_string buffer "$pipe"
226226
| '.' -> Buffer.add_string buffer "$dot"
227227
| '%' -> Buffer.add_string buffer "$percent"
228+
| '~' -> Buffer.add_string buffer "$tilde"
228229
| 'a'..'z' | 'A'..'Z'| '_'|'$' |'0'..'9'-> Buffer.add_char buffer c
229230
| _ -> Buffer.add_string buffer "$unknown"
230231
done; Buffer.contents buffer)

jscomp/ext_pervasives.ml

+11
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,14 @@ let with_file_as_pp filename f =
4040
Format.pp_print_flush fmt ();
4141
v
4242
) close_out
43+
44+
45+
let is_pos_pow n =
46+
let module M = struct exception E end in
47+
let rec aux c (n : Int32.t) =
48+
if n <= 0l then -2
49+
else if n = 1l then c
50+
else if Int32.logand n 1l = 0l then
51+
aux (c + 1) (Int32.shift_right n 1 )
52+
else raise M.E in
53+
try aux 0 n with M.E -> -1

jscomp/ext_pervasives.mli

+2
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ val finally : 'a -> ('a -> 'b) -> ('a -> 'c) -> 'b
2929
val with_file_as_chan : string -> (out_channel -> 'a) -> 'a
3030

3131
val with_file_as_pp : string -> (Format.formatter -> 'a) -> 'a
32+
33+
val is_pos_pow : Int32.t -> int

jscomp/js_analyzer.ml

+4-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ let free_variables_of_statement used_idents defined_idents st =
7171
let free_variables_of_expression used_idents defined_idents st =
7272
((free_variables used_idents defined_idents)#expression st) # get_depenencies
7373

74-
let rec no_side_effect (x : J.expression) =
75-
match x.expression_desc with
74+
let rec no_side_effect_expression_desc (x : J.expression_desc) =
75+
match x with
7676
| Bool _ -> true
7777
| Var _ -> true
7878
| Access (a,b) -> no_side_effect a && no_side_effect b
@@ -125,6 +125,8 @@ let rec no_side_effect (x : J.expression) =
125125
| Caml_block_set_tag _
126126
| Caml_block_set_length _ (* actually true? *)
127127
-> false
128+
and no_side_effect (x : J.expression) =
129+
no_side_effect_expression_desc x.expression_desc
128130

129131
let no_side_effect_expression (x : J.expression) = no_side_effect x
130132

jscomp/js_analyzer.mli

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ val free_variables_of_statement :
3131
val free_variables_of_expression :
3232
Ident_set.t -> Ident_set.t -> J.finish_ident_expression -> Ident_set.t
3333

34+
val no_side_effect_expression_desc :
35+
J.expression_desc -> bool
3436
val no_side_effect_expression :
35-
J.expression -> bool
37+
J.expression -> bool
3638
(** [no_side_effect] means this expression has no side effect,
3739
but it might *depend on value store*, so you can not just move it around,
3840

jscomp/js_exp_make.ml

+39-22
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,7 @@ let to_uint32 ?comment (e : J.expression) : J.expression =
916916
we can apply a more general optimization here,
917917
do some algebraic rewerite rules to rewrite [triple_equal]
918918
*)
919-
let is_out ?comment (e : t) (range : t) : t =
919+
let rec is_out ?comment (e : t) (range : t) : t =
920920
begin match range.expression_desc, e.expression_desc with
921921

922922
| Number (Int {i = 1l}), Var _
@@ -925,8 +925,8 @@ let is_out ?comment (e : t) (range : t) : t =
925925
| Number (Int {i = 1l}),
926926
(
927927
Bin (Plus , {expression_desc = Number (Int {i ; _}) }, {expression_desc = Var _; _})
928-
| Bin (Plus, {expression_desc = Var _; _}, {expression_desc = Number (Int {i ; _}) })
929-
)
928+
| Bin (Plus, {expression_desc = Var _; _}, {expression_desc = Number (Int {i ; _}) }))
929+
930930
->
931931
not (or_ (triple_equal e (int (Int32.neg i ))) (triple_equal e (int (Int32.sub Int32.one i))))
932932
| Number (Int {i = 1l}),
@@ -946,6 +946,16 @@ let is_out ?comment (e : t) (range : t) : t =
946946
*)
947947
or_ (int_comp Cgt e (int ( k))) (int_comp Clt e zero_int_literal)
948948

949+
| _, Bin (Bor ,
950+
({expression_desc =
951+
(Bin((Plus | Minus ) ,
952+
{expression_desc = Number (Int {i ; _}) }, {expression_desc = Var _; _})
953+
|Bin((Plus | Minus ) ,
954+
{expression_desc = Var _; _}, {expression_desc = Number (Int {i ; _}) } ))
955+
} as e), {expression_desc = Number (Int {i=0l} | Uint 0l | Nint 0n); _})
956+
->
957+
(* TODO: check correctness *)
958+
is_out ?comment e range
949959
| _, _ ->
950960
int_comp ?comment Cgt (to_uint32 e) range
951961
end
@@ -1004,9 +1014,6 @@ let int32_minus ?comment e1 e2 : J.expression =
10041014
let unchecked_int32_minus ?comment e1 e2 : J.expression =
10051015
float_minus ?comment e1 e2
10061016

1007-
let unchecked_prefix_inc ?comment (i : J.vident) =
1008-
let v : t = {expression_desc = Var i; comment = None} in
1009-
assign ?comment v (unchecked_int32_add v one_int_literal)
10101017

10111018

10121019
let float_div ?comment e1 e2 =
@@ -1034,22 +1041,6 @@ let int32_div ?comment (e1 : J.expression) (e2 : J.expression)
10341041
to_int32 (float_div ?comment e1 e2)
10351042

10361043

1037-
(* TODO: call primitive *)
1038-
let int32_mul ?comment
1039-
(e1 : J.expression)
1040-
(e2 : J.expression) : J.expression =
1041-
match e1.expression_desc, e2.expression_desc with
1042-
| Number (Int{i = i0}), Number (Int {i = i1})
1043-
-> int (Int32.mul i0 i1)
1044-
| _ ->
1045-
{ comment ;
1046-
expression_desc = Bin (Mul, e1,e2)
1047-
}
1048-
1049-
let unchecked_int32_mul ?comment e1 e2 : J.expression =
1050-
{ comment ;
1051-
expression_desc = Bin (Mul, e1,e2)
1052-
}
10531044

10541045
let float_mul ?comment e1 e2 =
10551046
bin ?comment Mul e1 e2
@@ -1068,6 +1059,32 @@ let int32_lsl ?comment e1 e2 : J.expression =
10681059
}
10691060

10701061

1062+
let int32_mul ?comment
1063+
(e1 : J.expression)
1064+
(e2 : J.expression) : J.expression =
1065+
match e1, e2 with
1066+
| {expression_desc = Number (Int {i = 0l}| Uint 0l | Nint 0n); _}, x
1067+
| x, {expression_desc = Number (Int {i = 0l}| Uint 0l | Nint 0n); _}
1068+
when Js_analyzer.no_side_effect_expression x
1069+
-> zero_int_literal
1070+
| {expression_desc = Number (Int{i = i0}); _}, {expression_desc = Number (Int {i = i1}); _}
1071+
-> int (Int32.mul i0 i1)
1072+
| e , {expression_desc = Number (Int {i = i0} | Uint i0 ); _}
1073+
| {expression_desc = Number (Int {i = i0} | Uint i0 ); _}, e
1074+
->
1075+
let i = Ext_pervasives.is_pos_pow i0 in
1076+
if i >= 0 then
1077+
int32_lsl e (small_int i)
1078+
else
1079+
runtime_call ?comment Js_config.prim "imul" [e1;e2]
1080+
| _ ->
1081+
runtime_call ?comment Js_config.prim "imul" [e1;e2]
1082+
1083+
let unchecked_int32_mul ?comment e1 e2 : J.expression =
1084+
{ comment ;
1085+
expression_desc = Bin (Mul, e1,e2)
1086+
}
1087+
10711088

10721089

10731090
let rec int32_bxor ?comment (e1 : t) (e2 : t) : J.expression =

jscomp/js_exp_make.mli

-3
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,6 @@ val inc : unary_op
228228

229229
val dec : unary_op
230230

231-
val unchecked_prefix_inc : ?comment:string -> J.vident -> t
232-
233-
234231

235232

236233

jscomp/lam_compile.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,7 @@ and
11941194
match lambda with
11951195
| Lprim(Poffsetint v, [Lvar id']) when Ident.same id id' ->
11961196
[ S.exp (E.assign (E.var id)
1197-
(E.unchecked_int32_add (E.var id) (E.small_int v)))
1197+
(E.int32_add (E.var id) (E.small_int v)))
11981198
]
11991199
| _ ->
12001200
begin

jscomp/lam_compile_primitive.ml

+2-8
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,6 @@ let translate
8181
(** Negate boxed int end*)
8282
(* Int addition and subtraction *)
8383
| Paddint
84-
->
85-
begin match args with
86-
| [e1; e2]
87-
-> E.unchecked_int32_add e1 e2
88-
| _ -> assert false
89-
end
9084
| Paddbint Pint32
9185
->
9286
begin match args with
@@ -340,14 +334,14 @@ let translate
340334
end
341335
| Poffsetint n ->
342336
begin match args with
343-
| [e] -> E.unchecked_int32_add e (E.small_int n)
337+
| [e] -> E.int32_add e (E.small_int n)
344338
| _ -> assert false
345339
end
346340
| Poffsetref n ->
347341
begin match args with
348342
| [e] ->
349343
let v = (Js_of_lam_block.field Fld_na e 0l) in
350-
E.assign v (E.unchecked_int32_add v (E.small_int n))
344+
E.assign v (E.int32_add v (E.small_int n))
351345
| _ -> assert false
352346
end
353347

jscomp/runtime/caml_array.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ function caml_array_sub(x, offset, len) {
88
var i = offset;
99
while(j < len) {
1010
result[j] = x[i];
11-
++ j;
12-
++ i;
11+
j = j + 1 | 0;
12+
i = i + 1 | 0;
1313
};
1414
return result;
1515
}
@@ -20,7 +20,7 @@ function len(_acc, _l) {
2020
var acc = _acc;
2121
if (l) {
2222
_l = l[1];
23-
_acc = l[0].length + acc;
23+
_acc = l[0].length + acc | 0;
2424
continue ;
2525

2626
}
@@ -41,8 +41,8 @@ function fill(arr, _i, _l) {
4141
var j = 0;
4242
while(j < l$1) {
4343
arr[k] = x[j];
44-
++ k;
45-
++ j;
44+
k = k + 1 | 0;
45+
j = j + 1 | 0;
4646
};
4747
_l = l[1];
4848
_i = k;
@@ -73,13 +73,13 @@ function caml_make_vect(len, init) {
7373
function caml_array_blit(a1, i1, a2, i2, len) {
7474
if (i2 <= i1) {
7575
for(var j = 0 ,j_finish = len - 1; j<= j_finish; ++j){
76-
a2[j + i2] = a1[j + i1];
76+
a2[j + i2 | 0] = a1[j + i1 | 0];
7777
}
7878
return /* () */0;
7979
}
8080
else {
8181
for(var j$1 = len - 1; j$1>= 0; --j$1){
82-
a2[j$1 + i2] = a1[j$1 + i1];
82+
a2[j$1 + i2 | 0] = a1[j$1 + i1 | 0];
8383
}
8484
return /* () */0;
8585
}

0 commit comments

Comments
 (0)