Skip to content

Commit 33441df

Browse files
authored
Merge pull request rescript-lang#4893 from rescript-lang/simplify_number
simplify numbers in JS IR
2 parents 7668157 + 58cdd2a commit 33441df

File tree

107 files changed

+1802
-1863
lines changed

Some content is hidden

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

107 files changed

+1802
-1863
lines changed

jscomp/core/j.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ and required_modules = module_id list
7373
currently we always use quote
7474
*)
7575
and property_name = Js_op.property_name
76-
and jsint = Js_op.jsint
76+
and jsint = int32
7777
and ident = Ident.t
7878
and module_id = {
7979
id : ident; kind : Js_op.kind

jscomp/core/js_analyzer.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ let rec eq_expression
178178
begin match x0 with
179179
| Null -> y0 = Null
180180
| Undefined -> y0 = Undefined
181-
| Number (Int i) ->
181+
| Number (Int {i}) ->
182182
begin match y0 with
183-
| Number (Int j) -> i = j
183+
| Number (Int {i = j}) -> i = j
184184
| _ -> false
185185
end
186186
| Number (Float _) -> false

jscomp/core/js_dump.ml

+3-1
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,9 @@ and expression_desc cxt ~(level:int) f x : cxt =
671671
| Float {f} ->
672672
Js_number.caml_float_literal_to_js_string f
673673
(* attach string here for float constant folding?*)
674-
| Int { i; _}
674+
| Int { i; c = Some c}
675+
-> Format.asprintf "/* %C */%ld" c i
676+
| Int { i; c = None}
675677
-> Int32.to_string i (* check , js convention with ocaml lexical convention *)
676678
| Uint i
677679
-> Format.asprintf "%lu" i

jscomp/core/js_exp_make.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ val extension_assign :
247247
val assign_by_int :
248248
?comment:string ->
249249
t ->
250-
Js_op.jsint ->
250+
int32 ->
251251
t ->
252252
t
253253

jscomp/core/js_fold.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ class virtual fold =
316316
method length_object : length_object -> 'self_type = o#unknown
317317
method label : label -> 'self_type = o#string
318318
method kind : kind -> 'self_type = o#unknown
319-
method jsint : jsint -> 'self_type = o#unknown
319+
method jsint : jsint -> 'self_type = o#int32
320320
method int_op : int_op -> 'self_type = o#unknown
321321
method ident_info : ident_info -> 'self_type = o#unknown
322322
method ident : ident -> 'self_type = o#unknown

jscomp/core/js_map.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ class virtual map =
338338
method length_object : length_object -> length_object = o#unknown
339339
method label : label -> label = o#string
340340
method kind : kind -> kind = o#unknown
341-
method jsint : jsint -> jsint = o#unknown
341+
method jsint : jsint -> jsint = o#int32
342342
method int_op : int_op -> int_op = o#unknown
343343
method ident_info : ident_info -> ident_info = o#unknown
344344
method ident : ident -> ident = o#unknown

jscomp/core/js_of_lam_string.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ module E = Js_exp_make
4040
*)
4141

4242
let const_char (i : char) =
43-
E.int ~comment:("\"" ^ String.escaped (String.make 1 i) ^ "\"")
43+
E.int
4444
~c:i (Int32.of_int @@ Char.code i)
4545

4646
let caml_char_of_int (v : J.expression) = v

jscomp/core/js_op.ml

+1-10
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,13 @@ type property_name =
153153
type 'a access =
154154
| Getter
155155
| Setter
156-
type jsint = Int32.t
157-
158-
type int_or_char =
159-
{ i : jsint;
160-
(* we can not use [int] on 32 bit platform, if we dont use
161-
[Int32.t], we need a configuration step
162-
*)
163-
c : char option
164-
}
165156

166157
(* literal char *)
167158
type float_lit = { f : string } [@@unboxed]
168159

169160
type number =
170161
| Float of float_lit
171-
| Int of int_or_char
162+
| Int of { i : int32; c : char option}
172163
| Uint of int32
173164

174165
(* becareful when constant folding +/-,

jscomp/core/lam_analysis.ml

+8-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,14 @@ let rec no_side_effects (lam : Lam.t) : bool =
5858
| "caml_make_vect"
5959
| "caml_create_bytes"
6060
| "caml_obj_dup"
61-
| "caml_array_dup"
61+
| "caml_array_dup"
62+
63+
| "nativeint_add"
64+
| "nativeint_div"
65+
| "nativeint_mod"
66+
| "nativeint_lsr"
67+
| "nativeint_mul"
68+
6269
), _ -> true
6370
| "caml_ml_open_descriptor_in", [Lconst ( (Const_int {i = 0l}))] -> true
6471
| "caml_ml_open_descriptor_out",

jscomp/core/lam_dispatch_primitive.ml

+7-15
Original file line numberDiff line numberDiff line change
@@ -98,24 +98,21 @@ let translate loc (prim_name : string)
9898
| _ -> assert false
9999
end
100100
| "caml_int32_of_int"
101-
| "caml_nativeint_of_int"
102-
| "caml_nativeint_of_int32" ->
101+
->
103102
begin match args with
104103
| [e] -> e
105104
| _ -> assert false
106105
end
107106
| "caml_int32_of_float"
108107
| "caml_int_of_float"
109-
| "caml_nativeint_of_float" ->
108+
->
110109
begin match args with
111110
| [e] -> E.to_int32 e
112111
| _ -> assert false
113112
end
114113
| "caml_int32_to_float"
115-
| "caml_int32_to_int"
116-
| "caml_nativeint_to_int"
117-
| "caml_nativeint_to_float"
118-
| "caml_nativeint_to_int32" ->
114+
| "caml_int32_to_int"
115+
->
119116
begin match args with
120117
| [e] -> e (* TODO: do more checking when [to_int32]*)
121118
| _ -> assert false
@@ -290,11 +287,6 @@ let translate loc (prim_name : string)
290287
| "caml_int32_equal_null"
291288
| "caml_int32_equal_nullable"
292289
| "caml_int32_equal_undefined"
293-
294-
295-
| "caml_nativeint_equal_null"
296-
| "caml_nativeint_equal_nullable"
297-
| "caml_nativeint_equal_undefined"
298290
->
299291
begin match args with
300292
| [e0;e1]
@@ -355,7 +347,7 @@ let translate loc (prim_name : string)
355347
end
356348
| "caml_int_compare"
357349
| "caml_int32_compare"
358-
| "caml_nativeint_compare"
350+
359351
| "caml_float_compare"
360352

361353
| "caml_string_compare"
@@ -365,7 +357,7 @@ let translate loc (prim_name : string)
365357
| "caml_int_min"
366358
| "caml_float_min"
367359
| "caml_string_min"
368-
| "caml_nativeint_min"
360+
369361
| "caml_int32_min"
370362

371363
->
@@ -381,7 +373,7 @@ let translate loc (prim_name : string)
381373
| "caml_int_max"
382374
| "caml_float_max"
383375
| "caml_string_max"
384-
| "caml_nativeint_max"
376+
385377
| "caml_int32_max"
386378
->
387379
begin match args with

jscomp/runtime/caml_nativeint_extern.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ external rem : int -> int -> int = "nativeint_mod"
99
external shift_right_logical : int -> int -> int = "nativeint_lsr"
1010
external mul : int -> int -> int = "nativeint_mul"
1111

12-
external to_float : int -> float = "caml_nativeint_to_float"
13-
external of_float : float -> int = "caml_nativeint_of_float"
12+
external to_float : int -> float = "%identity"
13+
external of_float : float -> int = "caml_int_of_float"
1414
external to_string : int -> string = "String" [@@bs.val]
1515

jscomp/runtime/caml_primitive.ml

-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ let caml_bool_compare (x : bool) (y : bool): int =
3232
| false, true -> -1
3333

3434
let caml_int32_compare = caml_int_compare
35-
let caml_nativeint_compare = caml_int_compare
3635

3736
let caml_float_compare (x : float) (y : float ) =
3837
if x = y then 0
@@ -60,8 +59,6 @@ let caml_float_min (x : float) y =
6059
if x < y then x else y
6160
let caml_string_min (x : string) y =
6261
if x < y then x else y
63-
let caml_nativeint_min (x : int) y =
64-
if x < y then x else y
6562
let caml_int32_min (x : int32) y =
6663
if x < y then x else y
6764

@@ -73,8 +70,6 @@ let caml_float_max (x : float) y =
7370
if x > y then x else y
7471
let caml_string_max (x : string) y =
7572
if x > y then x else y
76-
let caml_nativeint_max (x : int) y =
77-
if x > y then x else y
7873
let caml_int32_max (x : int32) y =
7974
if x > y then x else y
8075

jscomp/runtime/caml_primitive.mli

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type 'a selector = 'a -> 'a -> 'a
3636
val caml_int_compare : int -> int -> int
3737
val caml_bool_compare : bool -> bool -> int
3838
val caml_float_compare : float -> float -> int
39-
val caml_nativeint_compare : int -> int -> int
39+
4040
val caml_string_compare : string -> string -> int
4141
val caml_int32_compare : int -> int -> int
4242

@@ -45,12 +45,12 @@ val caml_bool_min : bool selector
4545
val caml_int_min : int selector
4646
val caml_float_min : float selector
4747
val caml_string_min : string selector
48-
val caml_nativeint_min : int selector
48+
4949
val caml_int32_min : int32 selector
5050

5151
val caml_bool_max : bool selector
5252
val caml_int_max : int selector
5353
val caml_float_max : float selector
5454
val caml_string_max : string selector
55-
val caml_nativeint_max : int selector
55+
5656
val caml_int32_max : int32 selector

jscomp/test/a_string_test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var suites_0 = [
99
(function (param) {
1010
return {
1111
TAG: /* Eq */0,
12-
_0: Ext_string_test.split(true, "hihi", /* "i" */105),
12+
_0: Ext_string_test.split(true, "hihi", /* 'i' */105),
1313
_1: {
1414
hd: "h",
1515
tl: {
@@ -30,7 +30,7 @@ var suites_1 = {
3030
(function (param) {
3131
return {
3232
TAG: /* Eq */0,
33-
_0: Ext_string_test.split(undefined, "hihi", /* "i" */105),
33+
_0: Ext_string_test.split(undefined, "hihi", /* 'i' */105),
3434
_1: {
3535
hd: "h",
3636
tl: {
@@ -47,7 +47,7 @@ var suites_1 = {
4747
(function (param) {
4848
return {
4949
TAG: /* Eq */0,
50-
_0: Ext_string_test.split(true, "", /* "i" */105),
50+
_0: Ext_string_test.split(true, "", /* 'i' */105),
5151
_1: /* [] */0
5252
};
5353
})
@@ -58,7 +58,7 @@ var suites_1 = {
5858
(function (param) {
5959
return {
6060
TAG: /* Eq */0,
61-
_0: Ext_string_test.split(true, "h i i", /* " " */32),
61+
_0: Ext_string_test.split(true, "h i i", /* ' ' */32),
6262
_1: {
6363
hd: "h",
6464
tl: {
@@ -81,10 +81,10 @@ var suites_1 = {
8181
_0: List.filter(function (s) {
8282
return s !== "";
8383
})(Ext_string_test.split_by(undefined, (function (x) {
84-
if (x === /* " " */32) {
84+
if (x === /* ' ' */32) {
8585
return true;
8686
} else {
87-
return x === /* "\t" */9;
87+
return x === /* '\t' */9;
8888
}
8989
}), "h hgso hgso \t hi")),
9090
_1: {

jscomp/test/argv_test.js

+29-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,35 @@ var arg_spec = {
4242
tl: arg_spec_1
4343
};
4444

45-
Arg.parse(arg_spec, anno_fun, usage_msg);
45+
Arg.parse_argv(undefined, [
46+
"prog.exe",
47+
"-c",
48+
"-d"
49+
], arg_spec, anno_fun, usage_msg);
50+
51+
if (compile.contents !== true) {
52+
throw {
53+
RE_EXN_ID: "Assert_failure",
54+
_1: [
55+
"argv_test.ml",
56+
20,
57+
0
58+
],
59+
Error: new Error()
60+
};
61+
}
62+
63+
if (test.contents !== false) {
64+
throw {
65+
RE_EXN_ID: "Assert_failure",
66+
_1: [
67+
"argv_test.ml",
68+
21,
69+
0
70+
],
71+
Error: new Error()
72+
};
73+
}
4674

4775
exports.anno_fun = anno_fun;
4876
exports.usage_msg = usage_msg;

jscomp/test/argv_test.ml

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@ let arg_spec = Arg.[
1414
]
1515

1616
;;
17-
Arg.parse arg_spec anno_fun usage_msg
17+
Arg.parse_argv [|"prog.exe";"-c"; "-d"; |] arg_spec anno_fun usage_msg
18+
;;
19+
20+
assert (!compile = true);
21+
assert (!test = false)

jscomp/test/array_test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,12 @@ var array_suites_1 = {
146146
return {
147147
TAG: /* Eq */0,
148148
_0: [
149-
Caml_array.caml_make_vect(100, /* "a" */97),
149+
Caml_array.caml_make_vect(100, /* 'a' */97),
150150
Caml_array.caml_make_float_vect(100)
151151
],
152152
_1: [
153153
$$Array.init(100, (function (param) {
154-
return /* "a" */97;
154+
return /* 'a' */97;
155155
})),
156156
$$Array.init(100, (function (param) {
157157
return 0;

jscomp/test/buffer_test.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ var suites_0 = [
1313
return {
1414
TAG: /* Eq */0,
1515
_0: [
16-
Caml_bytes.get(Bytes.make(3, /* "a" */97), 0),
17-
Bytes.make(3, /* "a" */97)[0]
16+
Caml_bytes.get(Bytes.make(3, /* 'a' */97), 0),
17+
Bytes.make(3, /* 'a' */97)[0]
1818
],
1919
_1: [
20-
/* "a" */97,
21-
/* "a" */97
20+
/* 'a' */97,
21+
/* 'a' */97
2222
]
2323
};
2424
})
@@ -28,17 +28,17 @@ var suites_1 = {
2828
hd: [
2929
"equal2",
3030
(function (param) {
31-
var u = Bytes.make(3, /* "a" */97);
32-
u[0] = /* "b" */98;
31+
var u = Bytes.make(3, /* 'a' */97);
32+
u[0] = /* 'b' */98;
3333
return {
3434
TAG: /* Eq */0,
3535
_0: [
3636
u[0],
37-
/* "g" */103
37+
/* 'g' */103
3838
],
3939
_1: [
40-
/* "b" */98,
41-
/* "g" */103
40+
/* 'b' */98,
41+
/* 'g' */103
4242
]
4343
};
4444
})

0 commit comments

Comments
 (0)