Эх сурвалжийг харах

Now using Int32 for integer bounds checking

Taddeus Kroes 12 жил өмнө
parent
commit
4f4ff50e16
2 өөрчлөгдсөн 9 нэмэгдсэн , 4 устгасан
  1. 3 0
      README.md
  2. 6 4
      phases/typecheck.ml

+ 3 - 0
README.md

@@ -14,3 +14,6 @@ Issues & TODO
 - Assembly printer should print optimized instructions.
 - Erronous array initialisation needs more insightfull error messages and array
   assignment needs better better type checking.
+- Typechecking now gives an error when integers are not in the 32-bit range, as
+  the reference compiler implements it. However, 64-bit should imo also be
+  supported (using Nativeint instead of Int32).

+ 6 - 4
phases/typecheck.ml

@@ -188,11 +188,13 @@ let rec typecheck node =
     | Const (BoolVal value, ann) ->
         Const (BoolVal value, Type Bool :: ann)
     | Const (IntVal value, ann) ->
-        (* Do a bound check on integers (use Nativeint because default ints in
-         * ocaml are 31- or 64-bit *)
+        (* Do a bound check on integers (use Int32 because default ints in ocaml
+         * are 31- or 63-bit *)
         let cmpval = Nativeint.of_int value in
-        if cmpval < Nativeint.min_int || cmpval > Nativeint.max_int then (
-            raise (NodeError (node, "integer value out of range"))
+        let min = Nativeint.of_int32 Int32.min_int in
+        let max = Nativeint.of_int32 Int32.max_int in
+        if cmpval < min || cmpval > max then (
+            raise (NodeError (node, "integer value out of range (signed 32-bit)"))
         );
         Const (IntVal value, Type Int :: ann)
     | Const (FloatVal value, ann) ->