TypeInferer.scala 9.81 KB
Newer Older
1
package uvm.staticanalysis
2

3
import uvm._
Kunshan Wang's avatar
Kunshan Wang committed
4
import uvm.types._
5
import uvm.ssavariables._
6

7

8 9 10 11 12
class TypeInferer(
    predefinedEntities: PredefinedEntities,
    internalEntityPool: InternalEntityPool) {
  import predefinedEntities._
  import internalEntityPool._
Kunshan Wang's avatar
Kunshan Wang committed
13

14 15
  def ptrOrIRefOf(ptr: Boolean, ty: Type): Type = {
    if (ptr) ptrOf(ty) else irefOf(ty)
Kunshan Wang's avatar
Kunshan Wang committed
16
  }
17
  
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
  /**
   * Perform type inference on all entities, global or local, in the bundle b.
   */
  def inferBundle(b: Bundle): Unit = {
    for (v <- b.globalVarNs.all) {
      inferAndSet(v)
    }
    
    for (fv <- b.funcVerNs.all) {
      for (bb <- fv.bbs) {
        for (v <- bb.localVarNs.all) {
          inferAndSet(v)
        }
      }
    }
33
  }
34 35 36 37
  
  private def inferAndSet(v: SSAVariable): Unit = {
    val inferredType = inferType(v)
    v.inferredType = inferredType
38
  }
Kunshan Wang's avatar
Kunshan Wang committed
39

40
  def inferType(v: SSAVariable): Type = v match {
41
    case c: Constant => c.constTy
42
    case g: GlobalCell => irefOf(g.cellTy)
43
    case f: Function => funcOf(f.sig)
Kunshan Wang's avatar
Kunshan Wang committed
44
    case e: ExposedFunc => funcPtrOf(e.func.sig)
45 46
    case p: NorParam => p.ty
    case p: ExcParam => REF_VOID
Kunshan Wang's avatar
Kunshan Wang committed
47 48 49 50 51
    case r: InstResult => {
      val resTys = inferInstResultTypes(r.inst)
      try {
        resTys(r.index)
      } catch {
52
        case e: IndexOutOfBoundsException => throw new StaticAnalyserException(
Kunshan Wang's avatar
Kunshan Wang committed
53 54 55 56 57 58
          s"Instruction ${r.inst} produces only ${resTys.size} results, but result index ${r.index} is requested")
      }
    }
  }

  def inferInstResultTypes(inst: Instruction): Seq[Type] = inst match {
59
    case i: InstBinOp => Seq(i.opndTy) ++ Seq.fill(BinOpStatus.numOfFlags(i.flags))(I1)
60
    case i: InstCmp => i.opndTy match {
Kunshan Wang's avatar
Kunshan Wang committed
61
      case TypeVector(_, l) => Seq(vecOf(I1, l))
62
      case _ => Seq(I1)
63
    }
64 65 66 67 68 69 70 71 72 73 74
    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
75
    case i: InstExtractElement => Seq(i.seqTy.elemTy)
76 77 78 79 80 81 82 83 84 85
    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
86
    case i: InstGetVarPartIRef => Seq(ptrOrIRefOf(i.ptr, i.referentTy.varTy))
87 88 89 90 91 92 93 94
    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
95
    case i: InstNewThread => Seq(THREADREF)
96 97
    case i: InstSwapStack => i.curStackAction match {
      case RetWith(t) => t
Kunshan Wang's avatar
Kunshan Wang committed
98
      case _: KillOld => Seq()
99 100
    }
    case i: InstCommInst => i.inst.name.get match {
Kunshan Wang's avatar
Kunshan Wang committed
101
      case "@uvm.new_stack" => Seq(STACKREF)
102 103
      case "@uvm.kill_stack" => Seq()
      case "@uvm.thread_exit" => Seq()
Kunshan Wang's avatar
Kunshan Wang committed
104
      case "@uvm.current_stack" => Seq(STACKREF)
105 106
      case "@uvm.set_threadlocal" => Seq()
      case "@uvm.get_threadlocal" => Seq(REF_VOID)
107 108 109 110 111 112 113 114 115 116 117
      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
118
      case "@uvm.futex.wait_timeout" => Seq(I32)
119 120 121
      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
122
      case "@uvm.native.pin" => i.typeList(0) match {
123
        case TypeRef(t) => Seq(ptrOf(t))
Kunshan Wang's avatar
Kunshan Wang committed
124
        case TypeIRef(t) => Seq(ptrOf(t))
Kunshan Wang's avatar
Kunshan Wang committed
125
      }
126 127 128 129
      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)
130

131 132 133 134
      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)
135

Kunshan Wang's avatar
Kunshan Wang committed
136 137 138 139 140
      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()
      
141 142 143 144
      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)
145

Kunshan Wang's avatar
Kunshan Wang committed
146
      case "@uvm.meta.pop_frames_to" => Seq()
147
      case "@uvm.meta.push_frame" => Seq()
148

Kunshan Wang's avatar
Kunshan Wang committed
149 150
      case "@uvm.meta.enable_watchPoint" => Seq()
      case "@uvm.meta.disable_watchPoint" => Seq()
151

152
      case "@uvm.meta.set_trap_handler" => Seq()
153

154 155 156 157 158
      case "@uvm.meta.constant_by_id" => Seq(i.typeList(0))
      case "@uvm.meta.global_by_id" => Seq(i.typeList(0))
      case "@uvm.meta.func_by_id" => Seq(i.typeList(0))
      case "@uvm.meta.expfunc_by_id" => Seq(i.typeList(0))

159
      case "@uvm.irbuilder.new_ir_builder" => Seq(IRBUILDERREF)
Kunshan Wang's avatar
Kunshan Wang committed
160 161
      
      /// GEN:BEGIN:IRBUILDER_RETVALS
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
      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()
205
      case "@uvm.irbuilder.new_binop_with_status" => Seq()
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
      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
242
      /// GEN:END:IRBUILDER_RETVALS
243 244
    }
  }
245
}