types.scala 2.21 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)
}

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

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"
Kunshan Wang's avatar
Kunshan Wang committed
47
    case TypeVector(elemTy, len) => "vector<%s %d>".format(elemTy.repr, len)
Kunshan Wang's avatar
Kunshan Wang committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    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()
}