types.scala 2.73 KB
Newer Older
Kunshan Wang's avatar
Kunshan Wang committed
1 2 3 4 5 6
package uvm.types

import uvm._

abstract class Type extends IdentifiedSettable {
  override final def toString: String = Type.prettyPrint(this)
7 8 9 10 11
  override def hashCode(): Int = System.identityHashCode(this)
  override def equals(that: Any): Boolean = that match {
    case v: AnyRef => this eq v
    case _         => false
  }
Kunshan Wang's avatar
Kunshan Wang committed
12 13
}

14 15 16 17 18 19
abstract class FPType extends Type

abstract class AbstractRefType extends Type {
  def ty: Type
}

Kunshan Wang's avatar
Kunshan Wang committed
20 21 22 23 24
abstract class AbstractSeqType extends Type {
  def elemTy: Type
  def len: Long
}

Kunshan Wang's avatar
Kunshan Wang committed
25
case class TypeInt(var length: Int) extends Type
26 27 28 29 30
case class TypeFloat() extends FPType
case class TypeDouble() extends FPType
case class TypeRef(var ty: Type) extends AbstractRefType
case class TypeIRef(var ty: Type) extends AbstractRefType
case class TypeWeakRef(var ty: Type) extends AbstractRefType
Kunshan Wang's avatar
Kunshan Wang committed
31
case class TypeStruct(var fieldTy: Seq[Type]) extends Type
Kunshan Wang's avatar
Kunshan Wang committed
32
case class TypeArray(var elemTy: Type, var len: Long) extends AbstractSeqType
Kunshan Wang's avatar
Kunshan Wang committed
33
case class TypeHybrid(var fixedTy: Type, var varTy: Type) extends Type
Kunshan Wang's avatar
Kunshan Wang committed
34
case class TypeVoid() extends Type
Kunshan Wang's avatar
Kunshan Wang committed
35 36 37
case class TypeFunc(var sig: FuncSig) extends Type
case class TypeThread() extends Type
case class TypeStack() extends Type
Kunshan Wang's avatar
Kunshan Wang committed
38
case class TypeTagRef64() extends Type
Kunshan Wang's avatar
Kunshan Wang committed
39
case class TypeVector(var elemTy: Type, var len: Long) extends AbstractSeqType
Kunshan Wang's avatar
Kunshan Wang committed
40 41 42

object Type {
  def prettyPrint(ty: Type): String = ty match {
43 44 45 46 47 48 49 50
    case TypeInt(length)                => "int<%d>".format(length)
    case TypeFloat()                    => "float"
    case TypeDouble()                   => "double"
    case TypeRef(ty)                    => "ref<%s>".format(ty.repr)
    case TypeIRef(ty)                   => "iref<%s>".format(ty.repr)
    case TypeWeakRef(ty)                => "weakref<%s>".format(ty.repr)
    case TypeStruct(fieldTy)            => "struct<%s>".format(fieldTy.map(_.repr).mkString(" "))
    case TypeArray(elemTy, len)         => "array<%s %d>".format(elemTy.repr, len)
Kunshan Wang's avatar
Kunshan Wang committed
51
    case TypeHybrid(fixedPart, varPart) => "hybrid<%s %s>".format(fixedPart.repr, varPart.repr)
52 53 54 55 56 57 58
    case TypeVoid()                     => "void"
    case TypeFunc(sig)                  => "func<%s>".format(FuncSig.prettyPrint(sig))
    case TypeThread()                   => "thread"
    case TypeStack()                    => "stack"
    case TypeTagRef64()                 => "tagref64"
    case TypeVector(elemTy, len)        => "vector<%s %d>".format(elemTy.repr, len)
    case _                              => "unknown type " + ty.getClass.getName
Kunshan Wang's avatar
Kunshan Wang committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  }
}

object CommonTypes {
  val I1 = TypeInt(1)
  val I8 = TypeInt(8)
  val I16 = TypeInt(16)
  val I32 = TypeInt(32)
  val I64 = TypeInt(64)
  val FLOAT = TypeFloat()
  val DOUBLE = TypeDouble()
  val VOID = TypeVoid()
  val REFVOID = TypeRef(VOID)
  val THREAD = TypeThread()
  val STACK = TypeStack()
}