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

Kunshan Wang's avatar
Kunshan Wang committed
3
import uvm._
Kunshan Wang's avatar
Kunshan Wang committed
4
import uvm.FuncSig
Kunshan Wang's avatar
Kunshan Wang committed
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

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

Kunshan Wang's avatar
Kunshan Wang committed
15
  def internal(name: MuName): (MuID, MuName) = {
Kunshan Wang's avatar
Kunshan Wang committed
16
    val id = internalIDFactory.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

Kunshan Wang's avatar
Kunshan Wang committed
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")
Kunshan Wang's avatar
Kunshan Wang committed
31 32
  val I52 = TypeInt(52) := internal("i52")
  val I64 = TypeInt(52) := internal("i64")
33
  val FLOAT = TypeFloat() := internal("float")
Kunshan Wang's avatar
Kunshan Wang committed
34
  val DOUBLE = TypeDouble() := internal("double")
Kunshan Wang's avatar
Kunshan Wang committed
35
  val VOID = TypeVoid() := internal("void")
Kunshan Wang's avatar
Kunshan Wang committed
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 IRNODEREF = TypeIRNodeRef() := internal("irnoderef")
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)
Kunshan Wang's avatar
Kunshan Wang committed
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) }
Kunshan Wang's avatar
Kunshan Wang committed
72 73
  def unmarkedOf(t: Type): Type = t match {
    case TypeWeakRef(r) => refOf(r)
74
    case _ => t
Kunshan Wang's avatar
Kunshan Wang committed
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

Kunshan Wang's avatar
Kunshan Wang committed
86
  def inferType(v: SSAVariable): Type = v match {
87
    case c: Constant => c.constTy
Kunshan Wang's avatar
Kunshan Wang committed
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 105
          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)
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)
Kunshan Wang's avatar
Kunshan Wang committed
142 143
    case i: InstSwapStack => i.curStackAction match {
      case RetWith(t) => t
Kunshan Wang's avatar
Kunshan Wang committed
144
      case _: KillOld => Seq()
Kunshan Wang's avatar
Kunshan Wang committed
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()
Kunshan Wang's avatar
Kunshan Wang committed
199 200 201 202 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
      
      /// GEN:BEGIN:IRBUILDER_RETVALS
      case "@uvm.irbuilder.new_bundle" => Seq(IRNODEREF)
      case "@uvm.irbuilder.load_bundle_from_node" => Seq()
      case "@uvm.irbuilder.abort_bundle_node" => Seq()
      case "@uvm.irbuilder.get_node" => Seq(IRNODEREF)
      case "@uvm.irbuilder.get_id" => Seq(I32)
      case "@uvm.irbuilder.set_name" => Seq()
      case "@uvm.irbuilder.new_type_int" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_type_float" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_type_double" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_type_uptr" => Seq(IRNODEREF)
      case "@uvm.irbuilder.set_type_uptr" => Seq()
      case "@uvm.irbuilder.new_type_ufuncptr" => Seq(IRNODEREF)
      case "@uvm.irbuilder.set_type_ufuncptr" => Seq()
      case "@uvm.irbuilder.new_type_struct" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_type_hybrid" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_type_array" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_type_vector" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_type_void" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_type_ref" => Seq(IRNODEREF)
      case "@uvm.irbuilder.set_type_ref" => Seq()
      case "@uvm.irbuilder.new_type_iref" => Seq(IRNODEREF)
      case "@uvm.irbuilder.set_type_iref" => Seq()
      case "@uvm.irbuilder.new_type_weakref" => Seq(IRNODEREF)
      case "@uvm.irbuilder.set_type_weakref" => Seq()
      case "@uvm.irbuilder.new_type_funcref" => Seq(IRNODEREF)
      case "@uvm.irbuilder.set_type_funcref" => Seq()
      case "@uvm.irbuilder.new_type_tagref64" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_type_threadref" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_type_stackref" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_type_framecursorref" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_type_irnoderef" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_funcsig" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_const_int" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_const_int_ex" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_const_float" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_const_double" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_const_null" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_const_seq" => Seq(IRNODEREF)
Kunshan Wang's avatar
Kunshan Wang committed
239
      case "@uvm.irbuilder.new_const_extern" => Seq(IRNODEREF)
Kunshan Wang's avatar
Kunshan Wang committed
240 241 242 243 244 245 246
      case "@uvm.irbuilder.new_global_cell" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_func" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_func_ver" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_exp_func" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_bb" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_nor_param" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_exc_param" => Seq(IRNODEREF)
247 248
      case "@uvm.irbuilder.get_inst_res" => Seq(IRNODEREF)
      case "@uvm.irbuilder.get_num_inst_res" => Seq(I32)
Kunshan Wang's avatar
Kunshan Wang committed
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 283 284 285 286 287 288 289 290 291 292
      case "@uvm.irbuilder.add_dest" => Seq()
      case "@uvm.irbuilder.add_keepalives" => Seq()
      case "@uvm.irbuilder.new_binop" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_cmp" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_conv" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_select" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_branch" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_branch2" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_switch" => Seq(IRNODEREF)
      case "@uvm.irbuilder.add_switch_dest" => Seq()
      case "@uvm.irbuilder.new_call" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_tailcall" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_ret" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_throw" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_extractvalue" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_insertvalue" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_extractelement" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_insertelement" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_shufflevector" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_new" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_newhybrid" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_alloca" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_allocahybrid" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_getiref" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_getfieldiref" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_getelemiref" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_shiftiref" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_getvarpartiref" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_load" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_store" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_cmpxchg" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_atomicrmw" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_fence" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_trap" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_watchpoint" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_wpbranch" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_ccall" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_newthread" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_swapstack_ret" => Seq(IRNODEREF)
      case "@uvm.irbuilder.new_swapstack_kill" => Seq(IRNODEREF)
      case "@uvm.irbuilder.set_newstack_pass_values" => Seq()
      case "@uvm.irbuilder.set_newstack_throw_exc" => Seq()
      case "@uvm.irbuilder.new_comminst" => Seq(IRNODEREF)
      /// GEN:END:IRBUILDER_RETVALS
Kunshan Wang's avatar
Kunshan Wang committed
293 294
    }
  }
295
}