Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /branches/charisee/src/compiler/tree-il/tree-ops.sml
ViewVC logotype

View of /branches/charisee/src/compiler/tree-il/tree-ops.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3544 - (download) (annotate)
Tue Jan 5 00:01:44 2016 UTC (3 years, 6 months ago) by cchiw
File size: 19622 byte(s)
code cleanup
(* 
 *
 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
 *
 * COPYRIGHT (c) 2015 The University of Chicago
 * All rights reserved.
 *
 * Lower-level version of the Diderot IL input file.
 *
 *)
 
 (*N.T.S. get rid of norm and normalize *)

structure TreeOps =
  struct

  (* required helper functions for types *)
    type ty = TreeILTypes.ty
    val samety = TreeILTypes.same
    val hashty = TreeILTypes.hash
    val tyToString = TreeILTypes.toString
    

  (* required helper functions for type lists *)
    type tys = ty list
    fun sametys (tys1, tys2) = ListPair.allEq samety (tys1, tys2)
    fun hashtys tys = List.foldl (fn (ty, s) => hashty ty + 0w3 * s) 0w0 tys
    fun tysToString tys = String.concat["[", String.concatWith "," (List.map tyToString tys), "]" ]

  (* required helper functions for the int type *)
    fun sameint (i1 : int, i2) = (i1 = i2)
    fun hashint i = Word.fromInt i
    fun intToString i = Int.toString i

  (* required helper functions for the string type *)
    fun samestring (s1 : string, s2) = (s1 = s2)
    val hashstring = HashString.hashString
    fun stringToString s = String.concat["\"", s, "\""]

  (* required helper functions for the input type *)
    type input = ty Inputs.input
    val sameinput = Inputs.same
    val hashinput = Inputs.hash
    val inputToString = Inputs.toString

 (* required helper functions for the intlist type *)
 type intlist=int list
 fun sameintlist  (tys1, tys2) = ListPair.allEq (fn (a,b)=> (a=b)) (tys1, tys2)
 fun hashintlist tys = List.foldl (fn (ty, s) => Word.fromInt ty + 0w3 * s) 0w0 tys
 fun intlistToString tys = String.concat["[", String.concatWith "," (List.map Int.toString tys), "]" ]
 
    datatype rator
      = IAdd
      | ISub
      | IMul
      | IDiv
      | INeg
      | IndexTensor of bool*intlist* ty (*bool-isLocal,indexTy,argTy*)
      | addSca
      | subSca
      | prodSca
      | divSca
      | powSca
      | powInt
      | addVec of int
      | subVec of int
      | prodVec of int
      | prodScaV of int
      | sumVec of int list* int (*nSize, oSize*)
      | clampVec of int
      | lerpVec of int
      | Sqrt
      | Cosine
      | ArcCosine
      | Sine
      | ArcSine
      | Tangent
      | ArcTangent
      | Exp
      | Abs of ty
      | LT of ty
      | LTE of ty
      | EQ of ty
      | NEQ of ty
      | GT of ty
      | GTE of ty
      | Not
      | Max
      | Min
      | Clamp of ty
      | Lerp of ty
      | Norm of ty
      | Normalize of int
      | PrincipleEvec of ty
      | EigenVecs2x2
      | EigenVecs3x3
      | EigenVals2x2
      | EigenVals3x3
      | Zero of ty
      | Select of ty * int
      | Index of ty * int
      | Subscript of ty
      | MkDynamic of ty * int
      | Append of ty
      | Prepend of ty
      | Concat of ty
      | Ceiling of int
      | Floor of int
      | Round of int
      | Trunc of int
      | IntToReal
      | R of ty
      | RealToInt of int
      | Transform of ImageInfo.info*int
      | Translate of ImageInfo.info
      | ImageAddress of ImageInfo.info
      | LoadVoxels of ImageInfo.info * int
      | Inside of ImageInfo.info * int
      | LoadSeq of ty * string
      | imgAddr of ImageInfo.info * ty * int
      | baseAddr  of ImageInfo.info
      | LoadImage of ty * string * ImageInfo.info
      | Input of input
      | Print of tys

    fun resultArity IAdd = 1
      | resultArity ISub = 1
      | resultArity IMul = 1
      | resultArity IDiv = 1
      | resultArity INeg = 1
      | resultArity (IndexTensor _) = 1
      | resultArity addSca = 1
      | resultArity subSca = 1
      | resultArity prodSca = 1
      | resultArity divSca = 1
      | resultArity (addVec _) = 1
      | resultArity (subVec _) = 1
      | resultArity (prodVec _) = 1
      | resultArity (prodScaV _) = 1
      | resultArity (sumVec _) = 1
      | resultArity (clampVec _) = 1
      | resultArity (lerpVec _) = 1
      | resultArity Sqrt =  1
      | resultArity Cosine =  1
      | resultArity ArcCosine =  1
      | resultArity Sine =  1
      | resultArity ArcSine =  1
      | resultArity Tangent =  1
      | resultArity ArcTangent =  1
      | resultArity Exp =  1
      | resultArity (Abs _) = 1
      | resultArity (LT _) = 1
      | resultArity (LTE _) = 1
      | resultArity (EQ _) = 1
      | resultArity (NEQ _) = 1
      | resultArity (GT _) = 1
      | resultArity (GTE _) = 1
      | resultArity Not = 1
      | resultArity Max = 1
      | resultArity Min = 1
      | resultArity (Clamp _) = 1
      | resultArity (Lerp _) = 1
      | resultArity (Norm _) = 1
      | resultArity (Normalize _) = 1
      | resultArity (PrincipleEvec _) = 1
      | resultArity EigenVecs2x2 = 1
      | resultArity EigenVecs3x3 = 1
      | resultArity EigenVals2x2 = 1
      | resultArity EigenVals3x3 = 1
      | resultArity (Zero _) = 1
      | resultArity (Select _) = 1
      | resultArity (Index _) = 1
      | resultArity (Subscript _) = 1
      | resultArity (MkDynamic _) = 1
      | resultArity (Append _) = 2
      | resultArity (Prepend _) = 2
      | resultArity (Concat _) = 2
      | resultArity (Ceiling _) = 1
      | resultArity (Floor _) = 1
      | resultArity (Round _) = 1
      | resultArity (Trunc _) = 1
      | resultArity IntToReal = 1
      | resultArity (RealToInt _) = 1
      | resultArity (Transform _) = 1
      | resultArity (Translate _) = 1
      | resultArity (ImageAddress _) = 1
      | resultArity (LoadVoxels _) = 1
      | resultArity (Inside _) = 1
      | resultArity (LoadSeq _) = 1
      | resultArity (imgAddr _) = 1
      | resultArity (baseAddr _) = 1
      | resultArity (LoadImage _) = 1
      | resultArity (Input _) = 0
      | resultArity (Print _) = 0


    fun arity IAdd = 2
      | arity ISub = 2
      | arity IMul = 2
      | arity IDiv = 2
      | arity INeg = 1
      | arity (IndexTensor _) = 2
      | arity addSca = 2
      | arity subSca = 2
      | arity prodSca = 2
      | arity divSca = 2
      | arity (addVec _) = 2
      | arity (subVec _) = 2
      | arity (prodVec _) = 2
      | arity (prodScaV _) = 2
      | arity (sumVec _) = 2
      | arity (clampVec _) = 2
      | arity (lerpVec _) = 2
      | arity Sqrt = 1
      | arity Cosine = 1
      | arity ArcCosine = 1
      | arity Sine = 1
      | arity  ArcSine = 1
      | arity Tangent= 1
      | arity ArcTangent= 1
      | arity Exp = 1
      | arity (Abs _) = 1
      | arity (LT _) = 2
      | arity (LTE _) = 2
      | arity (EQ _) = 2
      | arity (NEQ _) = 2
      | arity (GT _) = 2
      | arity (GTE _) = 2
      | arity Not = 1
      | arity Max = 2
      | arity Min = 2
      | arity (Clamp _) = 3
      | arity (Lerp _) = 3
      | arity (Norm _) = 1
      | arity (Normalize _) = 1
      | arity (PrincipleEvec _) = 2
      | arity EigenVecs2x2 = 1
      | arity EigenVecs3x3 = 1
      | arity EigenVals2x2 = 1
      | arity EigenVals3x3 = 1
      | arity (Zero _) = 0
      | arity (Select _) = 1
      | arity (Index _) = 1
      | arity (Subscript _) = 2
      | arity (MkDynamic _) = 1
      | arity (Append _) = 1
      | arity (Prepend _) = 1
      | arity (Concat _) = 1
      | arity (Ceiling _) = 1
      | arity (Floor _) = 1
      | arity (Round _) = 1
      | arity (Trunc _) = 1
      | arity IntToReal = 1
      | arity (RealToInt _) = 1
      | arity (Transform _) = 2
      | arity (Translate _) = 2 
      | arity (ImageAddress _) = 1
      | arity (LoadVoxels _) = 1
      | arity (Inside _) = 2
      | arity (LoadSeq _) = 0
      | arity (imgAddr _) = 2
      | arity(baseAddr _) = 2
      | arity (LoadImage _) = 0
      | arity (Input _) = 0
      | arity (Print _) = ~1

    fun isPure (MkDynamic _) = false
      | isPure (Append _) = false
      | isPure (Prepend _) = false
      | isPure (Concat _) = false
      | isPure (Input _) = false
      | isPure (Print _) = false
      | isPure _ = true

    fun same (IAdd, IAdd) = true
      | same (ISub, ISub) = true
      | same (IMul, IMul) = true
      | same (IDiv, IDiv) = true
      | same (INeg, INeg) = true
      | same (IndexTensor(a0,a1,a2), IndexTensor(b0,b1,b2)) = a0=b0  andalso (a1=b1) andalso samety(a2, b2)
      | same (addSca, addSca) = true
      | same (subSca, subSca) = true
      | same (prodSca, prodSca) = true
      | same (divSca, divSca) = true
      | same (Sqrt, Sqrt) = true
      | same (Cosine, Cosine) = true
      | same (ArcCosine, ArcCosine) = true
      | same (Sine, Sine) = true
      | same (ArcSine, ArcSine) = true
      | same (Tanget, Tangent) = true
      | same (ArcTanget, ArcTangent) = true
      | same (Exp, Exp) = true
      | same (addVec(a0), addVec(b0)) = sameint(a0, b0)
      | same (subVec(a0), subVec(b0)) = sameint(a0, b0)
      | same (prodVec(a0), prodVec(b0)) = sameint(a0, b0)
      | same (prodScaV(a0), prodScaV(b0)) = sameint(a0, b0)
      | same (clampVec(a0), clampVec(b0)) = sameint(a0, b0)
      | same (lerpVec(a0), lerpVec(b0)) = sameint(a0, b0)
      | same (Abs(a0), Abs(b0)) = samety(a0, b0)
      | same (LT(a0), LT(b0)) = samety(a0, b0)
      | same (LTE(a0), LTE(b0)) = samety(a0, b0)
      | same (EQ(a0), EQ(b0)) = samety(a0, b0)
      | same (NEQ(a0), NEQ(b0)) = samety(a0, b0)
      | same (GT(a0), GT(b0)) = samety(a0, b0)
      | same (GTE(a0), GTE(b0)) = samety(a0, b0)
      | same (Not, Not) = true
      | same (Max, Max) = true
      | same (Min, Min) = true
      | same (Clamp(a0), Clamp(b0)) = samety(a0, b0)
      | same (Lerp(a0), Lerp(b0)) = samety(a0, b0)
      | same (Norm(a0), Norm(b0)) = samety(a0, b0)
      | same (Normalize(a0), Normalize(b0)) = sameint(a0, b0)
      | same (PrincipleEvec(a0), PrincipleEvec(b0)) = samety(a0, b0)
      | same (EigenVecs2x2, EigenVecs2x2) = true
      | same (EigenVecs3x3, EigenVecs3x3) = true
      | same (EigenVals2x2, EigenVals2x2) = true
      | same (EigenVals3x3, EigenVals3x3) = true
      | same (Zero(a0), Zero(b0)) = samety(a0, b0)
      | same (Select(a0,a1), Select(b0,b1)) = samety(a0, b0) andalso sameint(a1, b1)
      | same (Index(a0,a1), Index(b0,b1)) = samety(a0, b0) andalso sameint(a1, b1)
      | same (Subscript(a0), Subscript(b0)) = samety(a0, b0)
      | same (MkDynamic(a0,a1), MkDynamic(b0,b1)) = samety(a0, b0) andalso sameint(a1, b1)
      | same (Append(a0), Append(b0)) = samety(a0, b0)
      | same (Prepend(a0), Prepend(b0)) = samety(a0, b0)
      | same (Concat(a0), Concat(b0)) = samety(a0, b0)
      | same (Ceiling(a0), Ceiling(b0)) = sameint(a0, b0)
      | same (Floor(a0), Floor(b0)) = sameint(a0, b0)
      | same (Round(a0), Round(b0)) = sameint(a0, b0)
      | same (Trunc(a0), Trunc(b0)) = sameint(a0, b0)
      | same (IntToReal, IntToReal) = true
      | same (RealToInt(a0), RealToInt(b0)) = sameint(a0, b0)
      | same (Transform(a0,a1), Transform(b0,b1)) = ImageInfo.same(a0, b0) andalso sameint(a1,b1)
      | same (Translate(a0), Translate(b0)) = ImageInfo.same(a0, b0)
      | same (ImageAddress(a0), ImageAddress(b0)) = ImageInfo.same(a0, b0)
      | same (LoadVoxels(a0,a1), LoadVoxels(b0,b1)) = ImageInfo.same(a0, b0) andalso sameint(a1, b1)
      | same (Inside(a0,a1), Inside(b0,b1)) = ImageInfo.same(a0, b0) andalso sameint(a1, b1)
      | same (LoadSeq(a0,a1), LoadSeq(b0,b1)) = samety(a0, b0) andalso samestring(a1, b1) 
      | same (imgAddr(a0,a1,a2), imgAddr(b0,b1,b2)) = ImageInfo.same(a0, b0) andalso samety(a1, b1) andalso sameint(a2, b2)
      | same (baseAddr a0, baseAddr b0) = ImageInfo.same(a0, b0)
      | same (LoadImage(a0,a1,a2), LoadImage(b0,b1,b2)) = samety(a0, b0) andalso samestring(a1, b1) andalso ImageInfo.same(a2, b2)
      | same (Input(a0), Input(b0)) = sameinput(a0, b0)
      | same (Print(a0), Print(b0)) = sametys(a0, b0)
      | same _ = false

    fun hashBool true=0w97
      | hashBool false=0w101
 
    fun hashIntList alpha= List.foldl op+ (hashint 0) (List.map hashint alpha)
    fun hash IAdd = 0w3
      | hash ISub = 0w5
      | hash IMul = 0w7
      | hash IDiv = 0w11
      | hash INeg = 0w13
      | hash (IndexTensor(a0,a1,a2)) = 0w19 + hashBool a0+hashIntList a1+hashty a2
      | hash addSca = 0w23
      | hash subSca = 0w29
      | hash prodSca = 0w31
      | hash divSca = 0w37
      | hash (addVec(a0)) = 0w41 + hashint a0
      | hash (subVec(a0)) = 0w43 + hashint a0
      | hash (prodVec(a0)) = 0w47 + hashint a0
      | hash (prodScaV(a0)) = 0w53 + hashint a0
      | hash (sumVec(a0,a1)) = 0w59 + hashIntList a0 +hashint a1
      | hash (clampVec(a0)) = 0w317 + hashint a0
      | hash (lerpVec(a0)) = 0w313 + hashint a0
      | hash (Abs(a0)) = 0w61 + hashty a0
      | hash (LT(a0)) = 0w67 + hashty a0
      | hash (LTE(a0)) = 0w71 + hashty a0
      | hash (EQ(a0)) = 0w73 + hashty a0
      | hash (NEQ(a0)) = 0w79 + hashty a0
      | hash (GT(a0)) = 0w83 + hashty a0
      | hash (GTE(a0)) = 0w89 + hashty a0
      | hash Not = 0w97
      | hash Max = 0w101
      | hash Min = 0w103
      | hash (Clamp(a0)) = 0w107 + hashty a0
      | hash (Lerp(a0)) = 0w109 + hashty a0
      | hash Sqrt  =  0w111
      | hash (Norm(a0)) = 0w113 + hashty a0
      | hash (Normalize(a0)) = 0w127 + hashint a0
      | hash (PrincipleEvec(a0)) = 0w131 + hashty a0
      | hash EigenVecs2x2 = 0w137
      | hash EigenVecs3x3 = 0w139
      | hash EigenVals2x2 = 0w149
      | hash EigenVals3x3 = 0w151
      | hash (Zero(a0)) = 0w157 + hashty a0
      | hash (Select(a0,a1)) = 0w163 + hashty a0 + hashint a1
      | hash (Index(a0,a1)) = 0w167 + hashty a0 + hashint a1
      | hash (Subscript(a0)) = 0w173 + hashty a0
      | hash (MkDynamic(a0,a1)) = 0w179 + hashty a0 + hashint a1
      | hash (Append(a0)) = 0w181 + hashty a0
      | hash (Prepend(a0)) = 0w191 + hashty a0
      | hash (Concat(a0)) = 0w193 + hashty a0
      | hash (Ceiling(a0)) = 0w199 + hashint a0
      | hash (Floor(a0)) = 0w211 + hashint a0
      | hash (Round(a0)) = 0w223 + hashint a0
      | hash (Trunc(a0)) = 0w227 + hashint a0
      | hash IntToReal = 0w229
      | hash (RealToInt(a0)) = 0w233 + hashint a0
      | hash (Transform(a0,a1)) = 0w239 + ImageInfo.hash a0+hashint a1
      | hash (Translate(a0)) = 0w241 + ImageInfo.hash a0
      | hash (ImageAddress(a0)) = 0w257 + ImageInfo.hash a0
      | hash (LoadVoxels(a0,a1)) = 0w263 + ImageInfo.hash a0 + hashint a1
      | hash (Inside(a0,a1)) = 0w269 + ImageInfo.hash a0 + hashint a1
      | hash (LoadSeq(a0,a1)) = 0w271 + hashty a0 + hashstring a1
      | hash (imgAddr(a0,a1,a2)) = 0w281 + ImageInfo.hash a0 + hashty a1 + hashint a2
      | hash(baseAddr a0) = 0w281 + ImageInfo.hash a0
      | hash (LoadImage(a0,a1,a2)) = 0w239 + hashty a0 + hashstring a1 + ImageInfo.hash a2
      | hash (Input(a0)) = 0w241 + hashinput a0
      | hash (Print(a0)) = 0w251 + hashtys a0
      | hash Cosine  =  0w257
      | hash ArcCosine  =  0w263
      | hash Sine  =  0w269
      | hash ArcSine  =  0w271
      | hash Tangent  =  0w269
      | hash ArcTangent  =  0w271
      | hash Exp  =0w271

    fun toStringBool true="local"
        | toStringBool false="global"
 
    fun toString IAdd = "IAdd"
      | toString ISub = "ISub"
      | toString IMul = "IMul"
      | toString IDiv = "IDiv"
      | toString INeg = "INeg"
      | toString (IndexTensor(a0,a1,a2)) = concat["IndexTensor(", toStringBool a0, ")IndexTy<", intlistToString a1, ">_ArgTy_(", tyToString  a2,")"]
      | toString addSca = "addSca"
      | toString subSca = "subSca"
      | toString powSca = "powSca"
      | toString powInt = "powInt"
      | toString (R n) ="real"
      | toString prodSca = "prodSca"
      | toString divSca = "divSca"
      | toString (addVec(a0)) = concat["addVec<", intToString a0, ">"]
      | toString (subVec(a0)) = concat["subVec<", intToString a0, ">"]
      | toString (prodVec(a0)) = concat["prodVec<", intToString a0, ">"]
      | toString (prodScaV(a0)) = concat["scaleVec<", intToString a0, ">"]
      | toString (sumVec(a0,a1)) = concat["sumVec<",intToString a1,
          "|",
          concat(List.map intToString a0),">"]
      | toString (clampVec(a0)) = concat["clampVec<", intToString a0, ">"]
      | toString (lerpVec(a0)) = concat["lerpVec<", intToString a0, ">"]
      | toString Sqrt =concat["sqrt"]
      | toString Cosine =concat["cosine"]
      | toString ArcCosine =concat["ArcCosine"]
      | toString Sine =concat["sine"]
      | toString ArcSine =concat["arcsine"]
      | toString Tangent =concat["Tangent"]
      | toString ArcTangent=concat["arctangent"]
      | toString Exp    ="Exp"
      | toString (Abs(a0)) = concat["Abs<", tyToString a0, ">"]
      | toString (LT(a0)) = concat["LT<", tyToString a0, ">"]
      | toString (LTE(a0)) = concat["LTE<", tyToString a0, ">"]
      | toString (EQ(a0)) = concat["EQ<", tyToString a0, ">"]
      | toString (NEQ(a0)) = concat["NEQ<", tyToString a0, ">"]
      | toString (GT(a0)) = concat["GT<", tyToString a0, ">"]
      | toString (GTE(a0)) = concat["GTE<", tyToString a0, ">"]
      | toString Not = "Not"
      | toString Max = "Max"
      | toString Min = "Min"
      | toString (Clamp(a0)) = concat["Clamp<", tyToString a0, ">"]
      | toString (Lerp(a0)) = concat["Lerp<", tyToString a0, ">"]
      | toString (Norm(a0)) = concat["Norm<", tyToString a0, ">"]
      | toString (Normalize(a0)) = concat["Normalize<", intToString a0, ">"]
      | toString (PrincipleEvec(a0)) = concat["PrincipleEvec<", tyToString a0, ">"]
      | toString EigenVecs2x2 = "EigenVecs2x2"
      | toString EigenVecs3x3 = "EigenVecs3x3"
      | toString EigenVals2x2 = "EigenVals2x2"
      | toString EigenVals3x3 = "EigenVals3x3"
      | toString (Zero(a0)) = concat["Zero<", tyToString a0, ">"]
      | toString (Select(a0,a1)) = concat["Select<", tyToString a0, ",", intToString a1, ">"]
      | toString (Index(a0,a1)) = concat["Index<", tyToString a0, ",", intToString a1, ">"]
      | toString (Subscript(a0)) = concat["Subscript<", tyToString a0, ">"]
      | toString (MkDynamic(a0,a1)) = concat["MkDynamic<", tyToString a0, ",", intToString a1, ">"]
      | toString (Append(a0)) = concat["Append<", tyToString a0, ">"]
      | toString (Prepend(a0)) = concat["Prepend<", tyToString a0, ">"]
      | toString (Concat(a0)) = concat["Concat<", tyToString a0, ">"]
      | toString (Ceiling(a0)) = concat["Ceiling<", intToString a0, ">"]
      | toString (Floor(a0)) = concat["Floor<", intToString a0, ">"]
      | toString (Round(a0)) = concat["Round<", intToString a0, ">"]
      | toString (Trunc(a0)) = concat["Trunc<", intToString a0, ">"]
      | toString IntToReal = "IntToReal"
      | toString (RealToInt(a0)) = concat["RealToInt<", intToString a0, ">"]
      | toString (Transform(a0,a1)) = concat["Transform<", ImageInfo.toString a0, ">","offset",intToString a1]
      | toString (Translate(a0)) = concat["Translate<", ImageInfo.toString a0, ">"]      
      | toString (ImageAddress(a0)) = concat["ImageAddress<", ImageInfo.toString a0, ">"]
      | toString (LoadVoxels(a0,a1)) = concat["LoadVoxels<", ImageInfo.toString a0, ",", intToString a1, ">"]
      | toString (Inside(a0,a1)) = concat["Inside<", ImageInfo.toString a0, ",", intToString a1, ">"]
      | toString (LoadSeq(a0,a1)) = concat["LoadSeq<", tyToString a0, ",", stringToString a1, ">"]
      | toString (imgAddr(a0,a1,a2)) = concat["imgAddr<", ImageInfo.toString a0, ",", tyToString a1, ",", intToString a2, ">"]
      | toString (baseAddr a0) = concat["baseAddr", ImageInfo.toString a0]
      | toString (LoadImage(a0,a1,a2)) = concat["LoadImage<", tyToString a0, ",", stringToString a1, ",", ImageInfo.toString a2, ">"]
      | toString (Input(a0)) = concat["Input<", inputToString a0, ">"]
      | toString (Print(a0)) = concat["Print<", tysToString a0, ">"]

  end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0