internals.scala 7.37 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._
Kunshan Wang's avatar
Kunshan Wang committed
6
import uvm.utils.LazyPool
Kunshan Wang's avatar
Kunshan Wang committed
7
import uvm.utils.IDFactory
8 9
import scala.collection.mutable.HashMap
import uvm.FuncSig
Kunshan Wang's avatar
Kunshan Wang committed
10
import uvm.IdentifiedSettable
11 12 13 14

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

object InternalTypes {
15
  import uvm.RichIdentifiedSettable._
16

17 18 19 20
  def internal(name: String): (Int, String) = {
    val id = InternalIDFactory.getID()
    val n = "@uvm.internal.types." + name
    (id, n)
Kunshan Wang's avatar
Kunshan Wang committed
21
  }
22

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

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

  val REF_VOID = TypeRef(VOID) := internal("ref_void")
Kunshan Wang's avatar
Kunshan Wang committed
38
  val IREF_VOID = TypeIRef(VOID) := internal("iref_void")
Kunshan Wang's avatar
Kunshan Wang committed
39

Kunshan Wang's avatar
Kunshan Wang committed
40 41 42 43 44
  val SIG_VV = FuncSig(Seq(), Seq()) := internal("sig_vv")
  val FUNCREF_VV = TypeFuncRef(SIG_VV) := internal("funcref_vv")
  
  val STACKREF = TypeStackRef() := internal("stackref")
  val THREADREF = TypeThreadRef() := internal("threadref")
Kunshan Wang's avatar
Kunshan Wang committed
45
  val FRAMECURSORREF = TypeFrameCursorRef() := internal("framecursorref")
Kunshan Wang's avatar
Kunshan Wang committed
46
  val IRNODEREF = TypeIRNodeRef() := internal("irnoderef")
Kunshan Wang's avatar
Kunshan Wang committed
47
  val TAGREF64 = TypeTagRef64() := internal("tagref64")
48 49 50 51 52

  val BYTES = TypeHybrid(Seq(I64), I8) := (0x260, "@uvm.meta.bytes")
  val BYTES_R = TypeRef(BYTES) := (0x261, "@uvm.meta.bytes_r")
  val REFS = TypeHybrid(Seq(I64), REF_VOID) := (0x262, "@uvm.meta.refs")
  val REFS_R = TypeRef(BYTES) := (0x263, "@uvm.meta.refs_r")
Kunshan Wang's avatar
Kunshan Wang committed
53 54 55 56

  val NULL_REF_VOID = ConstNull(REF_VOID) := internal("null_ref_void")
  val NULL_IREF_VOID = ConstNull(IREF_VOID) := internal("null_iref_void")
  val NULL_FUNCREF_VV = ConstNull(FUNCREF_VV) := internal("null_funcref_vv")
Kunshan Wang's avatar
Kunshan Wang committed
57 58
  val NULL_THREADREF = ConstNull(THREADREF) := internal("null_threadref")
  val NULL_STACKREF = ConstNull(STACKREF) := internal("null_stackref")
59 60 61
}

object InternalTypePool {
62
  val intOf = LazyPool(TypeInt)
Kunshan Wang's avatar
Kunshan Wang committed
63
  val refOf = LazyPool(TypeRef)
Kunshan Wang's avatar
Kunshan Wang committed
64
  val irefOf = LazyPool(TypeIRef)
Kunshan Wang's avatar
Kunshan Wang committed
65 66 67
  val ptrOf = LazyPool(TypeUPtr)
  val funcOf = LazyPool(TypeFuncRef)
  val funcPtrOf = LazyPool(TypeUFuncPtr)
Kunshan Wang's avatar
Kunshan Wang committed
68
  val vecOf = LazyPool[(Type, Long), TypeVector] { case (t, l) => TypeVector(t, l) }
Kunshan Wang's avatar
Kunshan Wang committed
69 70
  def unmarkedOf(t: Type): Type = t match {
    case TypeWeakRef(r) => refOf(r)
71
    case _ => t
Kunshan Wang's avatar
Kunshan Wang committed
72 73 74 75 76 77
  }
}

object TypeInferer {
  import InternalTypes._
  import InternalTypePool._
Kunshan Wang's avatar
Kunshan Wang committed
78

79 80 81
  def ptrOrIRefOf(ptr: Boolean, ty: Type): Type = {
    if (ptr) ptrOf(ty) else irefOf(ty)
  }
Kunshan Wang's avatar
Kunshan Wang committed
82

Kunshan Wang's avatar
Kunshan Wang committed
83
  def inferType(v: SSAVariable): Type = v match {
84
    case c: Constant => c.constTy
Kunshan Wang's avatar
Kunshan Wang committed
85
    case g: GlobalCell => irefOf(g.cellTy)
86
    case f: Function => funcOf(f.sig)
Kunshan Wang's avatar
Kunshan Wang committed
87
    case e: ExposedFunc => funcPtrOf(e.func.sig)
88 89
    case p: NorParam => p.ty
    case p: ExcParam => REF_VOID
Kunshan Wang's avatar
Kunshan Wang committed
90 91 92 93 94
    case r: InstResult => {
      val resTys = inferInstResultTypes(r.inst)
      try {
        resTys(r.index)
      } catch {
Kunshan Wang's avatar
Kunshan Wang committed
95
        case e: IndexOutOfBoundsException => throw new UvmRefImplException(
Kunshan Wang's avatar
Kunshan Wang committed
96 97 98 99 100 101 102
          s"Instruction ${r.inst} produces only ${resTys.size} results, but result index ${r.index} is requested")
      }
    }
  }

  def inferInstResultTypes(inst: Instruction): Seq[Type] = inst match {
    case i: InstBinOp => Seq(i.opndTy)
103
    case i: InstCmp => i.opndTy match {
Kunshan Wang's avatar
Kunshan Wang committed
104
      case TypeVector(_, l) => Seq(vecOf(I1, l))
105
      case _ => Seq(I1)
106
    }
107 108 109 110 111 112 113 114 115 116 117
    case i: InstConv => Seq(i.toTy)
    case i: InstSelect => Seq(i.opndTy)
    case i: InstBranch => Seq()
    case i: InstBranch2 => Seq()
    case i: InstSwitch => Seq()
    case i: InstCall => i.sig.retTys
    case i: InstTailCall => Seq()
    case i: InstRet => Seq()
    case i: InstThrow => Seq()
    case i: InstExtractValue => Seq(i.strTy.fieldTys(i.index))
    case i: InstInsertValue => Seq(i.strTy)
Kunshan Wang's avatar
Kunshan Wang committed
118
    case i: InstExtractElement => Seq(i.seqTy.elemTy)
119 120 121 122 123 124 125 126 127 128
    case i: InstInsertElement => Seq(i.seqTy)
    case i: InstShuffleVector => Seq(vecOf((i.vecTy.elemTy, i.maskTy.len)))
    case i: InstNew => Seq(refOf(i.allocTy))
    case i: InstNewHybrid => Seq(refOf(i.allocTy))
    case i: InstAlloca => Seq(irefOf(i.allocTy))
    case i: InstAllocaHybrid => Seq(irefOf(i.allocTy))
    case i: InstGetIRef => Seq(irefOf(i.referentTy))
    case i: InstGetFieldIRef => Seq(ptrOrIRefOf(i.ptr, i.referentTy.fieldTys(i.index)))
    case i: InstGetElemIRef => Seq(ptrOrIRefOf(i.ptr, i.referentTy.elemTy))
    case i: InstShiftIRef => Seq(ptrOrIRefOf(i.ptr, i.referentTy))
Kunshan Wang's avatar
Kunshan Wang committed
129
    case i: InstGetVarPartIRef => Seq(ptrOrIRefOf(i.ptr, i.referentTy.varTy))
130 131 132 133 134 135 136 137
    case i: InstLoad => Seq(unmarkedOf(i.referentTy))
    case i: InstStore => Seq()
    case i: InstCmpXchg => Seq(unmarkedOf(i.referentTy), I1)
    case i: InstAtomicRMW => Seq(unmarkedOf(i.referentTy))
    case i: InstFence => Seq()
    case i: InstTrap => i.retTys
    case i: InstWatchPoint => i.retTys
    case i: InstCCall => i.sig.retTys
Kunshan Wang's avatar
Kunshan Wang committed
138
    case i: InstNewThread => Seq(THREADREF)
Kunshan Wang's avatar
Kunshan Wang committed
139 140
    case i: InstSwapStack => i.curStackAction match {
      case RetWith(t) => t
Kunshan Wang's avatar
Kunshan Wang committed
141
      case _: KillOld => Seq()
Kunshan Wang's avatar
Kunshan Wang committed
142 143
    }
    case i: InstCommInst => i.inst.name.get match {
Kunshan Wang's avatar
Kunshan Wang committed
144
      case "@uvm.new_stack" => Seq(STACKREF)
145 146
      case "@uvm.kill_stack" => Seq()
      case "@uvm.thread_exit" => Seq()
Kunshan Wang's avatar
Kunshan Wang committed
147
      case "@uvm.current_stack" => Seq(STACKREF)
148 149
      case "@uvm.set_threadlocal" => Seq()
      case "@uvm.get_threadlocal" => Seq(REF_VOID)
150 151 152 153 154 155 156 157 158 159 160
      case "@uvm.tr64.is_fp" => Seq(I1)
      case "@uvm.tr64.is_int" => Seq(I1)
      case "@uvm.tr64.is_ref" => Seq(I1)
      case "@uvm.tr64.from_fp" => Seq(TAGREF64)
      case "@uvm.tr64.from_int" => Seq(TAGREF64)
      case "@uvm.tr64.from_ref" => Seq(TAGREF64)
      case "@uvm.tr64.to_fp" => Seq(DOUBLE)
      case "@uvm.tr64.to_int" => Seq(I52)
      case "@uvm.tr64.to_ref" => Seq(REF_VOID)
      case "@uvm.tr64.to_tag" => Seq(I6)
      case "@uvm.futex.wait" => Seq(I32)
Kunshan Wang's avatar
Kunshan Wang committed
161
      case "@uvm.futex.wait_timeout" => Seq(I32)
162 163 164
      case "@uvm.futex.wake" => Seq(I32)
      case "@uvm.futex.cmp_requeue" => Seq(I32)
      case "@uvm.kill_dependency" => Seq(i.typeList(0))
Kunshan Wang's avatar
Kunshan Wang committed
165
      case "@uvm.native.pin" => i.typeList(0) match {
166
        case TypeRef(t) => Seq(ptrOf(t))
Kunshan Wang's avatar
Kunshan Wang committed
167
        case TypeIRef(t) => Seq(ptrOf(t))
Kunshan Wang's avatar
Kunshan Wang committed
168
      }
169 170 171 172
      case "@uvm.native.unpin" => Seq()
      case "@uvm.native.expose" => Seq(funcPtrOf(i.funcSigList(0)))
      case "@uvm.native.unexpose" => Seq()
      case "@uvm.native.get_cookie" => Seq(I64)
173

174 175 176 177
      case "@uvm.meta.id_of" => Seq(I32)
      case "@uvm.meta.name_of" => Seq(BYTES_R)
      case "@uvm.meta.load_bundle" => Seq(BYTES_R)
      case "@uvm.meta.load_hail" => Seq(BYTES_R)
178

Kunshan Wang's avatar
Kunshan Wang committed
179 180 181 182 183
      case "@uvm.meta.new_cursor" => Seq(FRAMECURSORREF)
      case "@uvm.meta.next_frame" => Seq()
      case "@uvm.meta.copy_cursor" => Seq(FRAMECURSORREF)
      case "@uvm.meta.close_cursor" => Seq()
      
184 185 186 187
      case "@uvm.meta.cur_func" => Seq(I32)
      case "@uvm.meta.cur_func_ver" => Seq(I32)
      case "@uvm.meta.cur_inst" => Seq(I32)
      case "@uvm.meta.dump_keepalives" => Seq(REFS_R)
188

Kunshan Wang's avatar
Kunshan Wang committed
189
      case "@uvm.meta.pop_frames_to" => Seq()
190
      case "@uvm.meta.push_frame" => Seq()
191

Kunshan Wang's avatar
Kunshan Wang committed
192 193
      case "@uvm.meta.enable_watchPoint" => Seq()
      case "@uvm.meta.disable_watchPoint" => Seq()
194

195
      case "@uvm.meta.set_trap_handler" => Seq()
Kunshan Wang's avatar
Kunshan Wang committed
196 197
    }
  }
198
}