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

SCM Repository

[diderot] View of /branches/vis15/src/compiler/high-ir/high-types.sml
ViewVC logotype

View of /branches/vis15/src/compiler/high-ir/high-types.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3475 - (download) (annotate)
Wed Dec 2 18:48:58 2015 UTC (3 years, 9 months ago) by jhr
File size: 2842 byte(s)
working on merge
(* high-types.sml
 *
 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
 *
 * COPYRIGHT (c) 2015 The University of Chicago
 * All rights reserved.
 *)

structure HighTypes =
  struct

    datatype ty
      = BoolTy | IntTy | StringTy
      | TensorTy of int list
      | TupleTy of ty list              (* tuples; used for multiple return values *)
      | SeqTy of ty * int option
      | ImageTy of ImageInfo.info
      | KernelTy
      | FieldTy
      | AnyTy           		(* this is used in checking when we can't figure out the type *)

    val intTy = IntTy
    val realTy = TensorTy[]
    fun vecTy 1 = realTy
      | vecTy n = TensorTy[n]
    val vec2Ty = TensorTy[2]
    val vec3Ty = TensorTy[3]
    val vec4Ty = TensorTy[4]

  (* smart constructor for tensor type that prunes out dimensions with size 1 *)
    fun tensorTy dd = TensorTy(List.mapPartial (fn 1 => NONE | d => SOME d) dd)

    fun same (AnyTy, _) = true
      | same (_, AnyTy) = true
      | same (BoolTy, BoolTy) = true
      | same (IntTy, IntTy) = true
      | same (StringTy, StringTy) = true
      | same (TensorTy dd1, TensorTy dd2) = (dd1 = dd2)
      | same (TupleTy tys1, TupleTy tys2) = ListPair.allEq same (tys1, tys2)
      | same (SeqTy(ty1, NONE), SeqTy(ty2, NONE)) = same(ty1, ty2)
      | same (SeqTy(ty1, SOME n1), SeqTy(ty2, SOME n2)) = (n1 = n2) andalso same(ty1, ty2)
      | same (ImageTy info1, ImageTy info2) = ImageInfo.sameShape(info1, info2)
      | same (KernelTy, KernelTy) = true
      | same (FieldTy, FieldTy) = true
      | same _ = false

    fun hash BoolTy = 0w1
      | hash StringTy = 0w2
      | hash IntTy = 0w3
      | hash (TensorTy dd) = List.foldl (fn (d, s) => Word.fromInt d + s) 0w5 dd
      | hash (TupleTy tys) = List.foldl (fn (ty, s) => hash ty + s) 0w7 tys
      | hash (SeqTy(ty, NONE)) = hash ty + 0w11
      | hash (SeqTy(ty, SOME n)) = Word.fromInt n * hash ty + 0w13
      | hash (ImageTy info) = 0w17 + 0w3 * ImageInfo.hash info
      | hash KernelTy = 0w19
      | hash FieldTy = 0w23
      | hash AnyTy = raise Fail "hash(AnyTy)"

    fun toString BoolTy = "bool"
      | toString StringTy = "string"
      | toString IntTy = "int"
      | toString (TensorTy[]) = "real"
      | toString (TensorTy dd) = String.concat[
            "tensor[", String.concatWith "," (List.map Int.toString dd), "]"
          ]
      | toString (TupleTy tys) = String.concat[
            "(", String.concatWith " * " (List.map toString tys), ")"
          ]
      | toString (SeqTy(ty, NONE)) = toString ty ^ "[]"
      | toString (SeqTy(ty, SOME n)) = concat[toString ty, "[", Int.toString n, "]"]
      | toString (ImageTy info) = concat["image(", ImageInfo.toString info, ")"]
      | toString KernelTy = "kernel"
      | toString FieldTy = "field"
      | toString AnyTy = "**any**"

  end

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