Skip to content

Commit 9052261

Browse files
committed
add primitive bigint
1 parent c634719 commit 9052261

Some content is hidden

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

61 files changed

+537
-251
lines changed

jscomp/core/js_analyzer.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ let rec eq_expression ({ expression_desc = x0 } : J.expression)
158158
| Undefined x -> y0 = Undefined x
159159
| Number (Int { i }) -> (
160160
match y0 with Number (Int { i = j }) -> i = j | _ -> false)
161+
| Number (Bigint { i }) -> (
162+
match y0 with Number (Bigint { i = j }) -> i = j | _ -> false)
161163
| Number (Float _) -> false
162164
(* begin match y0 with
163165
| Number (Float j) ->

jscomp/core/js_dump.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,7 @@ and expression_desc cxt ~(level : int) f x : cxt =
664664
Int32.to_string i
665665
(* check , js convention with ocaml lexical convention *)
666666
| Uint i -> Format.asprintf "%lu" i
667+
| Bigint { i } -> Format.asprintf "%sn" i
667668
in
668669
let need_paren =
669670
if s.[0] = '-' then level > 13

jscomp/core/js_exp_make.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,10 @@ let obj_int_tag_literal : t =
312312

313313
let int ?comment ?c i : t = { expression_desc = Number (Int { i; c }); comment }
314314

315+
let bigint ?comment i : t = { expression_desc = Number (Bigint { i }); comment}
316+
317+
let zero_bigint_literal : t = {expression_desc = Number (Bigint {i = "0"}); comment = None}
318+
315319
let small_int i : t =
316320
match i with
317321
| 0 -> zero_int_literal
@@ -1253,6 +1257,16 @@ let rec int32_band ?comment (e1 : J.expression) (e2 : J.expression) :
12531257
(* let int32_bin ?comment op e1 e2 : J.expression = *)
12541258
(* {expression_desc = Int32_bin(op,e1, e2); comment} *)
12551259

1260+
let bigint_add ?comment (e1: t) (e2:t) = bin ?comment Plus e1 e2
1261+
1262+
let bigint_minus ?comment (e1: t) (e2: t) = bin ?comment Minus e1 e2
1263+
1264+
let bigint_mul ?comment (e1: t) (e2: t) = bin ?comment Mul e1 e2
1265+
1266+
let bigint_div ?comment (e1: t) (e2: t) = bin ?comment Div e1 e2
1267+
1268+
let bigint_mod ?comment (e1: t) (e2: t) = bin ?comment Mod e1 e2
1269+
12561270
(* TODO -- alpha conversion
12571271
remember to add parens..
12581272
*)

jscomp/core/js_exp_make.mli

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ val uint32 : ?comment:string -> int32 -> t
111111

112112
val small_int : int -> t
113113

114+
val bigint : ?comment:string -> string -> t
115+
114116
val float : ?comment:string -> string -> t
115117

116118
(* val empty_string_literal : t *)
@@ -121,6 +123,8 @@ val zero_int_literal : t
121123
val zero_float_lit : t
122124
(* val obj_int_tag_literal : t *)
123125

126+
val zero_bigint_literal : t
127+
124128
val is_out : ?comment:string -> t -> t -> t
125129
(** [is_out e range] is equivalent to [e > range or e <0]
126130
@@ -272,6 +276,16 @@ val string_comp : Js_op.binop -> ?comment:string -> t -> t -> t
272276

273277
val float_comp : Lam_compat.comparison -> ?comment:string -> t -> t -> t
274278

279+
val bigint_add : ?comment: string -> t -> t -> t
280+
281+
val bigint_minus : ?comment: string -> t-> t -> t
282+
283+
val bigint_mul : ?comment: string -> t -> t -> t
284+
285+
val bigint_div : ?comment: string -> t -> t -> t
286+
287+
val bigint_mod : ?comment: string -> t -> t -> t
288+
275289
val js_comp : Lam_compat.comparison -> ?comment:string -> t -> t -> t
276290

277291
val not : t -> t

jscomp/core/js_op.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,13 @@ type 'a access = Getter | Setter
125125
(* literal char *)
126126
type float_lit = { f : string } [@@unboxed]
127127

128+
type bigint_lit = { i: string } [@@unboxed]
129+
128130
type number =
129131
| Float of float_lit
130132
| Int of { i : int32; c : int option }
131133
| Uint of int32
134+
| Bigint of bigint_lit
132135

133136
(* becareful when constant folding +/-,
134137
since we treat it as js nativeint, bitwise operators:

jscomp/core/lam.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ let rec eval_const_as_bool (v : Lam_constant.t) : bool =
641641
| Const_int64 x -> x <> 0L
642642
| Const_js_false | Const_js_null | Const_module_alias | Const_js_undefined _ ->
643643
false
644-
| Const_js_true | Const_string _ | Const_pointer _ | Const_float _
644+
| Const_js_true | Const_string _ | Const_pointer _ | Const_float _ | Const_bigint _
645645
| Const_block _ | Const_float_array _ ->
646646
true
647647
| Const_some b -> eval_const_as_bool b

jscomp/core/lam_analysis.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ let not_zero_constant (x : Lam_constant.t) =
2727
match x with
2828
| Const_int { i } -> i <> 0l
2929
| Const_int64 i -> i <> 0L
30+
| Const_bigint i -> i <> "0"
3031
| _ -> false
3132

3233
let rec no_side_effects (lam : Lam.t) : bool =
@@ -53,7 +54,7 @@ let rec no_side_effects (lam : Lam.t) : bool =
5354
_ ) ->
5455
true
5556
| _, _ -> false)
56-
| Pmodint | Pdivint | Pdivint64 | Pmodint64 -> (
57+
| Pmodint | Pdivint | Pdivint64 | Pmodint64 | Pdivbigint | Pmodbigint -> (
5758
match args with
5859
| [ _; Lconst cst ] -> not_zero_constant cst
5960
| _ -> false)
@@ -76,6 +77,7 @@ let rec no_side_effects (lam : Lam.t) : bool =
7677
| Pintoffloat | Pfloatofint | Pnegfloat
7778
(* | Pabsfloat *)
7879
| Paddfloat | Psubfloat | Pmulfloat | Pdivfloat | Pfloatcomp _ | Pjscomp _
80+
| Pnegbigint | Paddbigint | Psubbigint | Pmulbigint
7981
(* String operations *)
8082
| Pstringlength | Pstringrefu | Pstringrefs | Pbyteslength | Pbytesrefu
8183
| Pbytesrefs | Pmakearray | Parraylength | Parrayrefu | Parrayrefs
@@ -193,7 +195,7 @@ let rec size (lam : Lam.t) =
193195

194196
and size_constant x =
195197
match x with
196-
| Const_int _ | Const_char _ | Const_float _ | Const_int64 _ | Const_pointer _
198+
| Const_int _ | Const_char _ | Const_float _ | Const_int64 _ | Const_bigint _ | Const_pointer _
197199
| Const_js_null | Const_js_undefined _ | Const_module_alias | Const_js_true
198200
| Const_js_false ->
199201
1

jscomp/core/lam_compat.ml

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25-
type boxed_integer = Lambda.boxed_integer = Pnativeint | Pint32 | Pint64
25+
type boxed_integer = Lambda.boxed_integer = Pbigint | Pint32 | Pint64
2626

2727
let eq_boxed_integer (p : boxed_integer) (p1 : boxed_integer) =
2828
match p with
29-
| Pnativeint -> p1 = Pnativeint
29+
| Pbigint -> p1 = Pbigint
3030
| Pint32 -> p1 = Pint32
3131
| Pint64 -> p1 = Pint64
3232

@@ -59,15 +59,6 @@ let cmp_int64 (cmp : comparison) (a : int64) b : bool =
5959
| Clt -> a < b
6060
| Cge -> a >= b
6161

62-
let cmp_nativeint (cmp : comparison) (a : nativeint) b : bool =
63-
match cmp with
64-
| Ceq -> a = b
65-
| Cneq -> a <> b
66-
| Cgt -> a > b
67-
| Cle -> a <= b
68-
| Clt -> a < b
69-
| Cge -> a >= b
70-
7162
let cmp_float (cmp : comparison) (a : float) b : bool =
7263
match cmp with
7364
| Ceq -> a = b

jscomp/core/lam_compat.mli

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25-
type boxed_integer = Lambda.boxed_integer = Pnativeint | Pint32 | Pint64
25+
type boxed_integer = Lambda.boxed_integer = Pbigint | Pint32 | Pint64
2626

2727
type comparison = Lambda.comparison = Ceq | Cneq | Clt | Cgt | Cle | Cge
2828

@@ -59,8 +59,6 @@ val cmp_int32 : comparison -> int32 -> int32 -> bool
5959

6060
val cmp_int64 : comparison -> int64 -> int64 -> bool
6161

62-
val cmp_nativeint : comparison -> nativeint -> nativeint -> bool
63-
6462
val cmp_float : comparison -> float -> float -> bool
6563

6664
val cmp_int : comparison -> int -> int -> bool

jscomp/core/lam_compile_const.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ and translate (x : Lam_constant.t) : J.expression =
7474
(* E.float (Int64.to_string i) *)
7575
Js_long.of_const i
7676
(* https://github.com/google/closure-library/blob/master/closure%2Fgoog%2Fmath%2Flong.js *)
77+
| Const_bigint i -> E.bigint i
7778
| Const_float f -> E.float f (* TODO: preserve float *)
7879
| Const_string { s; unicode = false } -> E.str s
7980
| Const_string { s; unicode = true } -> E.str ~delim:DStarJ s

jscomp/core/lam_compile_primitive.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,35 +183,44 @@ let translate output_prefix loc (cxt : Lam_compile_context.t)
183183
E.int32_minus E.zero_int_literal (Ext_list.singleton_exn args)
184184
| Pnegint64 -> Js_long.neg args
185185
| Pnegfloat -> E.float_minus E.zero_float_lit (Ext_list.singleton_exn args)
186+
| Pnegbigint -> E.bigint_minus E.zero_bigint_literal (Ext_list.singleton_exn args)
186187
(* Negate boxed int end*)
187188
(* Int addition and subtraction *)
188189
| Paddint -> (
189190
match args with [ e1; e2 ] -> E.int32_add e1 e2 | _ -> assert false)
190191
| Paddint64 -> Js_long.add args
191192
| Paddfloat -> (
192193
match args with [ e1; e2 ] -> E.float_add e1 e2 | _ -> assert false)
194+
| Paddbigint -> (
195+
match args with [ e1; e2 ] -> E.bigint_add e1 e2 | _ -> assert false)
193196
| Psubint -> (
194197
match args with [ e1; e2 ] -> E.int32_minus e1 e2 | _ -> assert false)
195198
| Psubint64 -> Js_long.sub args
196199
| Psubfloat -> (
197200
match args with [ e1; e2 ] -> E.float_minus e1 e2 | _ -> assert false)
201+
| Psubbigint -> (
202+
match args with [ e1; e2 ] -> E.bigint_minus e1 e2 | _ -> assert false)
198203
| Pmulint -> (
199204
match args with [ e1; e2 ] -> E.int32_mul e1 e2 | _ -> assert false)
200205
| Pmulint64 -> Js_long.mul args
201206
| Pmulfloat -> (
202207
match args with [ e1; e2 ] -> E.float_mul e1 e2 | _ -> assert false)
208+
| Pmulbigint -> (
209+
match args with [ e1; e2 ] -> E.bigint_mul e1 e2 | _ -> assert false)
203210
| Pdivfloat -> (
204211
match args with [ e1; e2 ] -> E.float_div e1 e2 | _ -> assert false)
205212
| Pdivint -> (
206213
match args with
207214
| [ e1; e2 ] -> E.int32_div ~checked:!Js_config.check_div_by_zero e1 e2
208215
| _ -> assert false)
209216
| Pdivint64 -> Js_long.div args
217+
| Pdivbigint -> (match args with [ e1; e2 ] -> E.bigint_div e1 e2 | _ -> assert false)
210218
| Pmodint -> (
211219
match args with
212220
| [ e1; e2 ] -> E.int32_mod ~checked:!Js_config.check_div_by_zero e1 e2
213221
| _ -> assert false)
214222
| Pmodint64 -> Js_long.mod_ args
223+
| Pmodbigint -> (match args with [ e1; e2 ] -> E.bigint_mod e1 e2 | _ -> assert false)
215224
| Plslint -> (
216225
match args with [ e1; e2 ] -> E.int32_lsl e1 e2 | _ -> assert false)
217226
| Plslint64 -> Js_long.lsl_ args

jscomp/core/lam_constant_convert.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ let rec convert_constant (const : Lambda.structured_constant) : Lam_constant.t =
3636
| Const_base (Const_float i) -> Const_float i
3737
| Const_base (Const_int32 i) -> Const_int { i; comment = None }
3838
| Const_base (Const_int64 i) -> Const_int64 i
39-
| Const_base (Const_nativeint _) -> assert false
39+
| Const_base (Const_bigint i) -> Const_bigint i
4040
| Const_pointer (0, Pt_constructor { name = "()"; const = 1; non_const = 0 })
4141
->
4242
Const_js_undefined {isUnit = true}

jscomp/core/lam_convert.ml

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,12 @@ let lam_prim ~primitive:(p : Lambda.primitive) ~args loc : Lam.t =
254254
| Psubfloat -> prim ~primitive:Psubfloat ~args loc
255255
| Pmulfloat -> prim ~primitive:Pmulfloat ~args loc
256256
| Pdivfloat -> prim ~primitive:Pdivfloat ~args loc
257+
| Pnegbigint -> prim ~primitive:Pnegbigint ~args loc
258+
| Paddbigint -> prim ~primitive:Paddbigint ~args loc
259+
| Psubbigint -> prim ~primitive:Psubbigint ~args loc
260+
| Pmulbigint -> prim ~primitive:Pmulbigint ~args loc
261+
| Pdivbigint _is_safe (*FIXME*) -> prim ~primitive:Pdivbigint ~args loc
262+
| Pmodbigint _is_safe (*FIXME*) -> prim ~primitive:Pmodbigint ~args loc
257263
| Pintcomp x -> prim ~primitive:(Pintcomp x) ~args loc
258264
| Poffsetint x -> prim ~primitive:(Poffsetint x) ~args loc
259265
| Poffsetref x -> prim ~primitive:(Poffsetref x) ~args loc
@@ -266,59 +272,59 @@ let lam_prim ~primitive:(p : Lambda.primitive) ~args loc : Lam.t =
266272
| Parraysets -> prim ~primitive:Parraysets ~args loc
267273
| Pbintofint x -> (
268274
match x with
269-
| Pint32 | Pnativeint -> Ext_list.singleton_exn args
275+
| Pint32 | Pbigint -> Ext_list.singleton_exn args
270276
| Pint64 -> prim ~primitive:Pint64ofint ~args loc)
271277
| Pintofbint x -> (
272278
match x with
273-
| Pint32 | Pnativeint -> Ext_list.singleton_exn args
279+
| Pint32 | Pbigint -> Ext_list.singleton_exn args
274280
| Pint64 -> prim ~primitive:Pintofint64 ~args loc)
275281
| Pnegbint x -> (
276282
match x with
277-
| Pnativeint | Pint32 -> prim ~primitive:Pnegint ~args loc
283+
| Pbigint | Pint32 -> prim ~primitive:Pnegint ~args loc
278284
| Pint64 -> prim ~primitive:Pnegint64 ~args loc)
279285
| Paddbint x -> (
280286
match x with
281-
| Pnativeint | Pint32 -> prim ~primitive:Paddint ~args loc
287+
| Pbigint | Pint32 -> prim ~primitive:Paddint ~args loc
282288
| Pint64 -> prim ~primitive:Paddint64 ~args loc)
283289
| Psubbint x -> (
284290
match x with
285-
| Pnativeint | Pint32 -> prim ~primitive:Psubint ~args loc
291+
| Pbigint | Pint32 -> prim ~primitive:Psubint ~args loc
286292
| Pint64 -> prim ~primitive:Psubint64 ~args loc)
287293
| Pmulbint x -> (
288294
match x with
289-
| Pnativeint | Pint32 -> prim ~primitive:Pmulint ~args loc
295+
| Pbigint | Pint32 -> prim ~primitive:Pmulint ~args loc
290296
| Pint64 -> prim ~primitive:Pmulint64 ~args loc)
291297
| Pdivbint { size = x; is_safe = _ } (*FIXME*) -> (
292298
match x with
293-
| Pnativeint | Pint32 -> prim ~primitive:Pdivint ~args loc
299+
| Pbigint | Pint32 -> prim ~primitive:Pdivint ~args loc
294300
| Pint64 -> prim ~primitive:Pdivint64 ~args loc)
295301
| Pmodbint { size = x; is_safe = _ } (*FIXME*) -> (
296302
match x with
297-
| Pnativeint | Pint32 -> prim ~primitive:Pmodint ~args loc
303+
| Pbigint | Pint32 -> prim ~primitive:Pmodint ~args loc
298304
| Pint64 -> prim ~primitive:Pmodint64 ~args loc)
299305
| Pandbint x -> (
300306
match x with
301-
| Pnativeint | Pint32 -> prim ~primitive:Pandint ~args loc
307+
| Pbigint | Pint32 -> prim ~primitive:Pandint ~args loc
302308
| Pint64 -> prim ~primitive:Pandint64 ~args loc)
303309
| Porbint x -> (
304310
match x with
305-
| Pnativeint | Pint32 -> prim ~primitive:Porint ~args loc
311+
| Pbigint | Pint32 -> prim ~primitive:Porint ~args loc
306312
| Pint64 -> prim ~primitive:Porint64 ~args loc)
307313
| Pxorbint x -> (
308314
match x with
309-
| Pnativeint | Pint32 -> prim ~primitive:Pxorint ~args loc
315+
| Pbigint | Pint32 -> prim ~primitive:Pxorint ~args loc
310316
| Pint64 -> prim ~primitive:Pxorint64 ~args loc)
311317
| Plslbint x -> (
312318
match x with
313-
| Pnativeint | Pint32 -> prim ~primitive:Plslint ~args loc
319+
| Pbigint | Pint32 -> prim ~primitive:Plslint ~args loc
314320
| Pint64 -> prim ~primitive:Plslint64 ~args loc)
315321
| Plsrbint x -> (
316322
match x with
317-
| Pnativeint | Pint32 -> prim ~primitive:Plsrint ~args loc
323+
| Pbigint | Pint32 -> prim ~primitive:Plsrint ~args loc
318324
| Pint64 -> prim ~primitive:Plsrint64 ~args loc)
319325
| Pasrbint x -> (
320326
match x with
321-
| Pnativeint | Pint32 -> prim ~primitive:Pasrint ~args loc
327+
| Pbigint | Pint32 -> prim ~primitive:Pasrint ~args loc
322328
| Pint64 -> prim ~primitive:Pasrint64 ~args loc)
323329
| Pctconst x -> (
324330
match x with
@@ -333,13 +339,13 @@ let lam_prim ~primitive:(p : Lambda.primitive) ~args loc : Lam.t =
333339
| Backend_type -> prim ~primitive:(Pctconst Backend_type) ~args loc)
334340
| Pcvtbint (a, b) -> (
335341
match (a, b) with
336-
| (Pnativeint | Pint32), (Pnativeint | Pint32) | Pint64, Pint64 ->
342+
| (Pbigint | Pint32), (Pbigint | Pint32) | Pint64, Pint64 ->
337343
Ext_list.singleton_exn args
338-
| Pint64, (Pnativeint | Pint32) -> prim ~primitive:Pintofint64 ~args loc
339-
| (Pnativeint | Pint32), Pint64 -> prim ~primitive:Pint64ofint ~args loc)
344+
| Pint64, (Pbigint | Pint32) -> prim ~primitive:Pintofint64 ~args loc
345+
| (Pbigint | Pint32), Pint64 -> prim ~primitive:Pint64ofint ~args loc)
340346
| Pbintcomp (a, b) -> (
341347
match a with
342-
| Pnativeint | Pint32 -> prim ~primitive:(Pintcomp b) ~args loc
348+
| Pbigint | Pint32 -> prim ~primitive:(Pintcomp b) ~args loc
343349
| Pint64 -> prim ~primitive:(Pint64comp b) ~args loc)
344350
| Popaque -> Ext_list.singleton_exn args
345351

jscomp/core/lam_primitive.ml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ type t =
7979
| Psubfloat
8080
| Pmulfloat
8181
| Pdivfloat
82+
(* Bigint operations *)
83+
| Pnegbigint
84+
| Paddbigint
85+
| Psubbigint
86+
| Pmulbigint
87+
| Pdivbigint
88+
| Pmodbigint
8289
| Pintcomp of Lam_compat.comparison
8390
| Pfloatcomp of Lam_compat.comparison
8491
| Pjscomp of Lam_compat.comparison
@@ -200,6 +207,12 @@ let eq_primitive_approx (lhs : t) (rhs : t) =
200207
| Psubfloat -> rhs = Psubfloat
201208
| Pmulfloat -> rhs = Pmulfloat
202209
| Pdivfloat -> rhs = Pdivfloat
210+
| Pnegbigint -> rhs = Pnegbigint
211+
| Paddbigint -> rhs = Paddbigint
212+
| Psubbigint -> rhs = Psubbigint
213+
| Pmulbigint -> rhs = Pmulbigint
214+
| Pdivbigint -> rhs = Pdivbigint
215+
| Pmodbigint -> rhs = Pmodbigint
203216
| Pjs_apply -> rhs = Pjs_apply
204217
| Pjs_runtime_apply -> rhs = Pjs_runtime_apply
205218
| Pstringlength -> rhs = Pstringlength

jscomp/core/lam_primitive.mli

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ type t =
7070
| Psubfloat
7171
| Pmulfloat
7272
| Pdivfloat
73+
| Pnegbigint
74+
| Paddbigint
75+
| Psubbigint
76+
| Pmulbigint
77+
| Pdivbigint
78+
| Pmodbigint
7379
| Pintcomp of Lam_compat.comparison
7480
| Pfloatcomp of Lam_compat.comparison
7581
| Pjscomp of Lam_compat.comparison

0 commit comments

Comments
 (0)