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

import uvm._

abstract class Type extends IdentifiedSettable {
  override final def toString: String = Type.prettyPrint(this)
}

9 10 11 12 13 14
abstract class FPType extends Type

abstract class AbstractRefType extends Type {
  def ty: Type
}

Kunshan Wang's avatar
Kunshan Wang committed
15 16 17 18 19
abstract class AbstractSeqType extends Type {
  def elemTy: Type
  def len: Long
}

Kunshan Wang's avatar
Kunshan Wang committed
20
case class TypeInt(var length: Int) extends Type
21 22 23 24 25
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
26
case class TypeStruct(var fieldTy: Seq[Type]) extends Type
Kunshan Wang's avatar
Kunshan Wang committed
27
case class TypeArray(var elemTy: Type, var len: Long) extends AbstractSeqType
Kunshan Wang's avatar
Kunshan Wang committed
28
case class TypeHybrid(var fixedTy: Type, var varTy: Type) extends Type
Kunshan Wang's avatar
Kunshan Wang committed
29
case class TypeVoid() extends Type
Kunshan Wang's avatar
Kunshan Wang committed
30 31 32
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
33
case class TypeTagRef64() extends Type
Kunshan Wang's avatar
Kunshan Wang committed
34
case class TypeVector(var elemTy: Type, var len: Long) extends AbstractSeqType
Kunshan Wang's avatar
Kunshan Wang committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

object Type {
  def prettyPrint(ty: Type): String = ty match {
    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)
    case TypeHybrid(fixedPart, varPart) => "hybrid<%s %s>".format(fixedPart.repr, varPart.repr)
    case TypeVoid() => "void"
    case TypeFunc(sig) => "func<%s>".format(FuncSig.prettyPrint(sig))
    case TypeThread() => "thread"
    case TypeStack() => "stack"
    case TypeTagRef64() => "tagref64"
52
    case TypeVector(elemTy, len) => "vector<%s %d>".format(elemTy.repr, len)
Kunshan Wang's avatar
Kunshan Wang committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    case _ => "unknown type " + ty.getClass.getName
  }
}

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()
}