Skip to content

Commit

Permalink
[enhance] compiler, utils: Added utils to checks integers boundaries
Browse files Browse the repository at this point in the history
  • Loading branch information
BourgerieQuentin committed Aug 8, 2012
1 parent 74bd3fd commit dcc1a23
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
35 changes: 33 additions & 2 deletions compiler/libqmlcompil/qmlAstUtils.ml
Expand Up @@ -298,14 +298,45 @@ end

module Const =
struct

let limits byte =
Big_int.minus_big_int (Big_int.power_int_positive_int 2 byte),
Big_int.pred_big_int (Big_int.power_int_positive_int 2 byte)

let int_js_limits (* -2^53, 2^53 - 1*) =
limits 53

let int_ml_limits (* -2^62, 2^62 - 1*) =
#<Ifstatic:OCAML_WORD_SIZE 64>
limits 62
#<Else>
limits 30
#<End>

let int_limits = ref int_js_limits

let compare a b =
match a, b with
| Q.Int a, Q.Int b -> Pervasives.compare a b
| Q.Int a, Q.Int b -> Big_int.compare_big_int a b
| Q.Float a, Q.Float b -> Pervasives.compare a b
| Q.String a, Q.String b -> String.compare a b
| _ -> assert false
| _ -> Pervasives.compare a b

let equal a b = compare a b = 0

let check_int i =
let min, max = !int_limits in
Big_int.le_big_int i max && Big_int.ge_big_int i min

let min_int () =
fst !int_limits

let max_int () =
snd !int_limits

let set_limits = function
| `js -> int_limits := int_js_limits
| `ml -> int_limits := int_ml_limits
end

module Record =
Expand Down
23 changes: 22 additions & 1 deletion compiler/libqmlcompil/qmlAstUtils.mli
@@ -1,5 +1,5 @@
(*
Copyright © 2011 MLstate
Copyright © 2011, 2012 MLstate
This file is part of Opa.
Expand Down Expand Up @@ -265,6 +265,27 @@ module Const : sig
Checks if compare returns 0
*)
val equal : QmlAst.const_expr -> QmlAst.const_expr -> bool

(**
Checks if the given integers is on the backend limits
*)
val check_int : Big_int.big_int -> bool

(**
Get the maximal integer
*)
val max_int : unit -> Big_int.big_int

(**
Get the minimal integer
*)
val min_int : unit -> Big_int.big_int

(**
Set the qml integers limits, defaults 53bits
*)
val set_limits : [`js | `ml] -> unit

end

module Coerce : sig
Expand Down

0 comments on commit dcc1a23

Please sign in to comment.