internals.scala 4.65 KB
Newer Older
1 2
package uvm.refimpl

Kunshan Wang's avatar
Kunshan Wang committed
3
import uvm._
4
import uvm.types._
Kunshan Wang's avatar
Kunshan Wang committed
5
import uvm.ssavariables._
6 7 8
import uvm.ir.textinput.IDFactory
import scala.collection.mutable.HashMap
import uvm.FuncSig
Kunshan Wang's avatar
Kunshan Wang committed
9
import uvm.IdentifiedSettable
10 11 12 13

object InternalIDFactory extends IDFactory(32768) // IDs from 32768-65535 are for implementation internal use.

object InternalTypes {
Kunshan Wang's avatar
Kunshan Wang committed
14 15 16 17 18 19 20
  private implicit class IdentifiedSettableAssignable[T <: IdentifiedSettable](i: T) {
    def :=(name: String): T = {
      i.id = InternalIDFactory.getID()
      i.name = Some(name)
      i
    }
  }
21

Kunshan Wang's avatar
Kunshan Wang committed
22
  def internal(name: String) = "@uvm.internal.types." + name
23

Kunshan Wang's avatar
Kunshan Wang committed
24 25
  val I1 = TypeInt(1) := internal("i1")
  val I6 = TypeInt(6) := internal("i6")
Kunshan Wang's avatar
Kunshan Wang committed
26
  val I32 = TypeInt(32) := internal("i32")
Kunshan Wang's avatar
Kunshan Wang committed
27 28 29
  val I52 = TypeInt(52) := internal("i52")
  val I64 = TypeInt(52) := internal("i64")
  val DOUBLE = TypeDouble() := internal("double")
Kunshan Wang's avatar
Kunshan Wang committed
30
  val VOID = TypeVoid() := internal("void")
Kunshan Wang's avatar
Kunshan Wang committed
31

Kunshan Wang's avatar
Kunshan Wang committed
32 33 34 35 36
  val BYTE = TypeInt(8) := internal("byte")
  val BYTE_ARRAY = TypeHybrid(VOID, BYTE) := internal("byte_array")

  val REF_VOID = TypeRef(VOID) := internal("ref_void")

Kunshan Wang's avatar
Kunshan Wang committed
37
  val STACK = TypeStack() := internal("stack")
38
  val THREAD = TypeThread() := internal("thread")
Kunshan Wang's avatar
Kunshan Wang committed
39
  val TAGREF64 = TypeTagRef64() := internal("tagref64")
40 41 42
}

object InternalTypePool {
Kunshan Wang's avatar
Kunshan Wang committed
43 44 45 46 47 48 49 50
  class LazyPool[FromT, ToT](factory: FromT => ToT) {
    val pool = HashMap[FromT, ToT]()
    def apply(obj: FromT): ToT = pool.get(obj).getOrElse(factory(obj))
  }
  object LazyPool {
    def apply[FromT, ToT](factory: FromT => ToT): LazyPool[FromT, ToT] = new LazyPool[FromT, ToT](factory)
  }

Kunshan Wang's avatar
Kunshan Wang committed
51
  val refOf = LazyPool(TypeRef)
Kunshan Wang's avatar
Kunshan Wang committed
52 53
  val irefOf = LazyPool(TypeIRef)
  val funcOf = LazyPool(TypeFunc)
Kunshan Wang's avatar
Kunshan Wang committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  val vecOf = new LazyPool[(Type, Long), TypeVector]({ case (t, l) => TypeVector(t, l) })
  def unmarkedOf(t: Type): Type = t match {
    case TypeWeakRef(r) => refOf(r)
    case _ => t
  }
}

object TypeInferer {
  import InternalTypes._
  import InternalTypePool._
  def inferType(v: SSAVariable): Type = v match {
    case c: Constant => c.constTy
    case g: GlobalCell => irefOf(g.cellTy)
    case f: Function => funcOf(f.sig)
    case p: Parameter => p.funcVer.sig.paramTy(p.index)
    case i: InstBinOp => i.opndTy
70 71 72 73
    case i: InstCmp => i.opndTy match {
      case TypeVector(_, l) => vecOf(I1, l)
      case _ => I1
    }
Kunshan Wang's avatar
Kunshan Wang committed
74 75 76 77
    case i: InstConv => i.toTy
    case i: InstSelect => i.opndTy
    case i: InstBranch => VOID
    case i: InstBranch2 => VOID
78
    case i: InstSwitch => VOID
Kunshan Wang's avatar
Kunshan Wang committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    case i: InstPhi => i.opndTy
    case i: InstCall => i.sig.retTy
    case i: InstTailCall => VOID
    case i: InstRet => VOID
    case i: InstRetVoid => VOID
    case i: InstThrow => VOID
    case i: InstLandingPad => REF_VOID
    case i: InstExtractValue => i.strTy.fieldTy(i.index)
    case i: InstInsertValue => i.strTy
    case i: InstExtractElement => i.vecTy.elemTy
    case i: InstInsertElement => i.vecTy
    case i: InstShuffleVector => vecOf((i.vecTy.elemTy, i.maskTy.len))
    case i: InstNew => refOf(i.allocTy)
    case i: InstNewHybrid => refOf(i.allocTy)
    case i: InstAlloca => irefOf(i.allocTy)
    case i: InstAllocaHybrid => irefOf(i.allocTy)
    case i: InstGetIRef => irefOf(i.referentTy)
    case i: InstGetFieldIRef => irefOf(i.referentTy.fieldTy(i.index))
    case i: InstGetElemIRef => irefOf(i.referentTy.elemTy)
    case i: InstShiftIRef => irefOf(i.referentTy)
    case i: InstGetFixedPartIRef => irefOf(i.referentTy.fixedTy)
    case i: InstGetVarPartIRef => irefOf(i.referentTy.varTy)
    case i: InstLoad => unmarkedOf(i.referentTy)
    case i: InstStore => VOID
    case i: InstCmpXchg => unmarkedOf(i.referentTy)
    case i: InstAtomicRMW => unmarkedOf(i.referentTy)
    case i: InstFence => VOID
    case i: InstTrap => i.retTy
    case i: InstWatchPoint => i.retTy
    case i: InstCCall => i.sig.retTy
    case i: InstNewStack => STACK
    case i: InstSwapStack => i.curStackAction match {
      case RetWith(t) => t
      case _: KillOld => VOID
    }
    case i: InstCommInst => i.inst.name.get match {
      case "@uvm.new_thread" => THREAD
      case "@uvm.kill_stack" => VOID
      case "@uvm.thread_exit" => VOID
      case "@uvm.current_stack" => STACK
      case "@uvm.tr64.is_fp" => I1
      case "@uvm.tr64.is_int" => I1
      case "@uvm.tr64.is_ref" => I1
      case "@uvm.tr64.from_fp" => TAGREF64
      case "@uvm.tr64.from_int" => TAGREF64
      case "@uvm.tr64.from_ref" => TAGREF64
      case "@uvm.tr64.to_fp" => DOUBLE
      case "@uvm.tr64.to_int" => I52
      case "@uvm.tr64.to_ref" => REF_VOID
      case "@uvm.tr64.to_tag" => I6
Kunshan Wang's avatar
Kunshan Wang committed
129 130 131 132
      case "@uvm.futex.wait" => I32
      case "@uvm.futex.wait_timeout" => I32
      case "@uvm.futex.wake" => I32
      case "@uvm.futex.cmp_requeue" => I32
133
      case "@uvm.kill_dependency" => i.typeList(0)
Kunshan Wang's avatar
Kunshan Wang committed
134 135
    }
  }
136
}