[MLton] Constant folding vector expressions
    Vesa Karvonen 
    vesa.a.j.k at gmail.com
       
    Mon Sep 17 10:56:44 PDT 2007
    
    
  
On 9/17/07, Matthew Fluet <fluet at tti-c.org> wrote:
> You want "WordSize.seqIndex ()", the size of sequence (i.e., array and
> vector) indices (and lengths).  [...]
Seems to work.  Regression tests seem to pass and compiling with
-default-type int64 and -default-type int32 seems to work on my 32-bit
machine.
One thing that makes me wonder is the int type used by the compiler.
It might differ from the sequence index type of the target executable.
 OTOH, for this to make a difference, you'd need to have a vector
constant of over 2G elements.
Here is the proposed patch:
Index: mlton/atoms/prim.fun
===================================================================
--- mlton/atoms/prim.fun	(revision 6026)
+++ mlton/atoms/prim.fun	(working copy)
@@ -1242,6 +1242,9 @@
       datatype z = datatype t
       datatype z = datatype Const.t
       val bool = ApplyResult.Bool
+      fun seqIndexConst i =
+         ApplyResult.Const
+         (Const.word (WordX.fromIntInf (i, WordSize.seqIndex ())))
       val intInf = ApplyResult.Const o Const.intInf
       val intInfConst = intInf o IntInf.fromInt
       val null = ApplyResult.Const Const.null
@@ -1303,6 +1306,10 @@
                     then null
                  else ApplyResult.Unknown
            | (CPointer_toWord, [Null]) => word (WordX.zero
(WordSize.cpointer ()))
+           | (Vector_length, [WordVector v]) =>
+                 seqIndexConst (IntInf.fromInt (WordXVector.length v))
+           | (Vector_sub, [WordVector v, Word i]) =>
+                 word (WordXVector.sub (v, WordX.toInt i))
            | (Word_add _, [Word w1, Word w2]) => word (WordX.add (w1, w2))
            | (Word_addCheck s, [Word w1, Word w2]) => wcheck (op +, s, w1, w2)
            | (Word_andb _, [Word w1, Word w2]) => word (WordX.andb (w1, w2))
Index: mlton/atoms/const.sig
===================================================================
--- mlton/atoms/const.sig	(revision 6026)
+++ mlton/atoms/const.sig	(working copy)
@@ -13,6 +13,7 @@
       structure RealX: REAL_X
       structure WordX: WORD_X
       structure WordXVector: WORD_X_VECTOR
+      sharing WordX = WordXVector.WordX
    end
 signature CONST =
Any objections to applying it?
-Vesa Karvonen
    
    
More information about the MLton
mailing list