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

3
import uvm._
Kunshan Wang's avatar
Kunshan Wang committed
4
import uvm.FuncSig
5
import uvm.ssavariables._
Kunshan Wang's avatar
Kunshan Wang committed
6
import uvm.types._
Kunshan Wang's avatar
Kunshan Wang committed
7
import uvm.utils.IDFactory
Kunshan Wang's avatar
Kunshan Wang committed
8
import uvm.utils.LazyPool
9 10

object InternalTypes {
11
  import uvm.RichIdentifiedSettable._
12

13
  val privateIDFactory = IDFactory.newPrivateIDFactory() // IDs from 32768-65535 are for implementation internal use.
Kunshan Wang's avatar
Kunshan Wang committed
14

Kunshan Wang's avatar
Kunshan Wang committed
15
  def internal(name: MuName): (MuID, MuName) = {
16
    val id = privateIDFactory.getID()
17 18
    val n = "@uvm.internal.types." + name
    (id, n)
Kunshan Wang's avatar
Kunshan Wang committed
19
  }
20 21 22 23 24
  
  val C_INT = TypeInt(32) := internal("c_int")
  val C_CHAR = TypeInt(8) := internal("c_char")
  val C_CHARP = TypeUPtr(C_CHAR) := internal("c_charp")
  val C_CHARPP = TypeUPtr(C_CHARP) := internal("c_charpp")
25

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

Kunshan Wang's avatar
Kunshan Wang committed
37
  val BYTE = TypeInt(8) := internal("byte")
Kunshan Wang's avatar
Kunshan Wang committed
38
  val BYTE_ARRAY = TypeHybrid(Seq(), BYTE) := internal("byte_array")
Kunshan Wang's avatar
Kunshan Wang committed
39 40

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

Kunshan Wang's avatar
Kunshan Wang committed
43 44 45 46 47
  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
48
  val FRAMECURSORREF = TypeFrameCursorRef() := internal("framecursorref")
Kunshan Wang's avatar
Kunshan Wang committed
49
  val IRBUILDERREF = TypeIRBuilderRef() := internal("irbuilderref")
Kunshan Wang's avatar
Kunshan Wang committed
50
  val TAGREF64 = TypeTagRef64() := internal("tagref64")
51 52 53 54 55

  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
56 57 58 59

  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
60 61
  val NULL_THREADREF = ConstNull(THREADREF) := internal("null_threadref")
  val NULL_STACKREF = ConstNull(STACKREF) := internal("null_stackref")
62 63 64
}

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

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

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

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

  def inferInstResultTypes(inst: Instruction): Seq[Type] = inst match {
105
    case i: InstBinOp => Seq(i.opndTy) ++ Seq.fill(BinOpStatus.numOfFlags(i.flags))(I1)
106
    case i: InstCmp => i.opndTy match {
Kunshan Wang's avatar
Kunshan Wang committed
107
      case TypeVector(_, l) => Seq(vecOf(I1, l))
108
      case _ => Seq(I1)
109
    }
110 111 112 113 114 115 116 117 118 119 120
    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
121
    case i: InstExtractElement => Seq(i.seqTy.elemTy)
122 123 124 125 126 127 128 129 130 131
    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
132
    case i: InstGetVarPartIRef => Seq(ptrOrIRefOf(i.ptr, i.referentTy.varTy))
133 134 135 136 137 138 139 140
    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
141
    case i: InstNewThread => Seq(THREADREF)
142 143
    case i: InstSwapStack => i.curStackAction match {
      case RetWith(t) => t
Kunshan Wang's avatar
Kunshan Wang committed
144
      case _: KillOld => Seq()
145 146
    }
    case i: InstCommInst => i.inst.name.get match {
Kunshan Wang's avatar
Kunshan Wang committed
147
      case "@uvm.new_stack" => Seq(STACKREF)
148 149
      case "@uvm.kill_stack" => Seq()
      case "@uvm.thread_exit" => Seq()
Kunshan Wang's avatar
Kunshan Wang committed
150
      case "@uvm.current_stack" => Seq(STACKREF)
151 152
      case "@uvm.set_threadlocal" => Seq()
      case "@uvm.get_threadlocal" => Seq(REF_VOID)
153 154 155 156 157 158 159 160 161 162 163
      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
164
      case "@uvm.futex.wait_timeout" => Seq(I32)
165 166 167
      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
168
      case "@uvm.native.pin" => i.typeList(0) match {
169
        case TypeRef(t) => Seq(ptrOf(t))
Kunshan Wang's avatar
Kunshan Wang committed
170
        case TypeIRef(t) => Seq(ptrOf(t))
Kunshan Wang's avatar
Kunshan Wang committed
171
      }
172 173 174 175
      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)
176

177 178 179 180
      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)
181

Kunshan Wang's avatar
Kunshan Wang committed
182 183 184 185 186
      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()
      
187 188 189 190
      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)
191

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

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

198
      case "@uvm.meta.set_trap_handler" => Seq()
199 200

      case "@uvm.irbuilder.new_ir_builder" => Seq(IRBUILDERREF)
Kunshan Wang's avatar
Kunshan Wang committed
201 202
      
      /// GEN:BEGIN:IRBUILDER_RETVALS
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
      case "@uvm.irbuilder.load" => Seq()
      case "@uvm.irbuilder.abort" => Seq()
      case "@uvm.irbuilder.gen_sym" => Seq(I32)
      case "@uvm.irbuilder.new_type_int" => Seq()
      case "@uvm.irbuilder.new_type_float" => Seq()
      case "@uvm.irbuilder.new_type_double" => Seq()
      case "@uvm.irbuilder.new_type_uptr" => Seq()
      case "@uvm.irbuilder.new_type_ufuncptr" => Seq()
      case "@uvm.irbuilder.new_type_struct" => Seq()
      case "@uvm.irbuilder.new_type_hybrid" => Seq()
      case "@uvm.irbuilder.new_type_array" => Seq()
      case "@uvm.irbuilder.new_type_vector" => Seq()
      case "@uvm.irbuilder.new_type_void" => Seq()
      case "@uvm.irbuilder.new_type_ref" => Seq()
      case "@uvm.irbuilder.new_type_iref" => Seq()
      case "@uvm.irbuilder.new_type_weakref" => Seq()
      case "@uvm.irbuilder.new_type_funcref" => Seq()
      case "@uvm.irbuilder.new_type_tagref64" => Seq()
      case "@uvm.irbuilder.new_type_threadref" => Seq()
      case "@uvm.irbuilder.new_type_stackref" => Seq()
      case "@uvm.irbuilder.new_type_framecursorref" => Seq()
      case "@uvm.irbuilder.new_type_irbuilderref" => Seq()
      case "@uvm.irbuilder.new_funcsig" => Seq()
      case "@uvm.irbuilder.new_const_int" => Seq()
      case "@uvm.irbuilder.new_const_int_ex" => Seq()
      case "@uvm.irbuilder.new_const_float" => Seq()
      case "@uvm.irbuilder.new_const_double" => Seq()
      case "@uvm.irbuilder.new_const_null" => Seq()
      case "@uvm.irbuilder.new_const_seq" => Seq()
      case "@uvm.irbuilder.new_const_extern" => Seq()
      case "@uvm.irbuilder.new_global_cell" => Seq()
      case "@uvm.irbuilder.new_func" => Seq()
      case "@uvm.irbuilder.new_exp_func" => Seq()
      case "@uvm.irbuilder.new_func_ver" => Seq()
      case "@uvm.irbuilder.new_bb" => Seq()
      case "@uvm.irbuilder.new_dest_clause" => Seq()
      case "@uvm.irbuilder.new_exc_clause" => Seq()
      case "@uvm.irbuilder.new_keepalive_clause" => Seq()
      case "@uvm.irbuilder.new_csc_ret_with" => Seq()
      case "@uvm.irbuilder.new_csc_kill_old" => Seq()
      case "@uvm.irbuilder.new_nsc_pass_values" => Seq()
      case "@uvm.irbuilder.new_nsc_throw_exc" => Seq()
      case "@uvm.irbuilder.new_binop" => Seq()
246
      case "@uvm.irbuilder.new_binop_with_status" => Seq()
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
      case "@uvm.irbuilder.new_cmp" => Seq()
      case "@uvm.irbuilder.new_conv" => Seq()
      case "@uvm.irbuilder.new_select" => Seq()
      case "@uvm.irbuilder.new_branch" => Seq()
      case "@uvm.irbuilder.new_branch2" => Seq()
      case "@uvm.irbuilder.new_switch" => Seq()
      case "@uvm.irbuilder.new_call" => Seq()
      case "@uvm.irbuilder.new_tailcall" => Seq()
      case "@uvm.irbuilder.new_ret" => Seq()
      case "@uvm.irbuilder.new_throw" => Seq()
      case "@uvm.irbuilder.new_extractvalue" => Seq()
      case "@uvm.irbuilder.new_insertvalue" => Seq()
      case "@uvm.irbuilder.new_extractelement" => Seq()
      case "@uvm.irbuilder.new_insertelement" => Seq()
      case "@uvm.irbuilder.new_shufflevector" => Seq()
      case "@uvm.irbuilder.new_new" => Seq()
      case "@uvm.irbuilder.new_newhybrid" => Seq()
      case "@uvm.irbuilder.new_alloca" => Seq()
      case "@uvm.irbuilder.new_allocahybrid" => Seq()
      case "@uvm.irbuilder.new_getiref" => Seq()
      case "@uvm.irbuilder.new_getfieldiref" => Seq()
      case "@uvm.irbuilder.new_getelemiref" => Seq()
      case "@uvm.irbuilder.new_shiftiref" => Seq()
      case "@uvm.irbuilder.new_getvarpartiref" => Seq()
      case "@uvm.irbuilder.new_load" => Seq()
      case "@uvm.irbuilder.new_store" => Seq()
      case "@uvm.irbuilder.new_cmpxchg" => Seq()
      case "@uvm.irbuilder.new_atomicrmw" => Seq()
      case "@uvm.irbuilder.new_fence" => Seq()
      case "@uvm.irbuilder.new_trap" => Seq()
      case "@uvm.irbuilder.new_watchpoint" => Seq()
      case "@uvm.irbuilder.new_wpbranch" => Seq()
      case "@uvm.irbuilder.new_ccall" => Seq()
      case "@uvm.irbuilder.new_newthread" => Seq()
      case "@uvm.irbuilder.new_swapstack" => Seq()
      case "@uvm.irbuilder.new_comminst" => Seq()
Kunshan Wang's avatar
Kunshan Wang committed
283
      /// GEN:END:IRBUILDER_RETVALS
284 285
    }
  }
286
}