WARNING! Access to this system is limited to authorised users only.
Unauthorised users may be subject to prosecution.
Unauthorised access to this system is a criminal offence under Australian law (Federal Crimes Act 1914 Part VIA)
It is a criminal offence to:
(1) Obtain access to data without authority. -Penalty 2 years imprisonment.
(2) Damage, delete, alter or insert data without authority. -Penalty 10 years imprisonment.
User activity is monitored and recorded. Anyone using this system expressly consents to such monitoring and recording.

To protect your data, the CISO officer has suggested users to enable 2FA as soon as possible.
Currently 2.7% of users enabled 2FA.

internals.scala 7 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
7
8
9
import uvm.ir.textinput.IDFactory
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 {
Kunshan Wang's avatar
Kunshan Wang committed
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
    }
Kunshan Wang's avatar
Kunshan Wang committed
21
22
23
24
25
26
27

    def :=(idName: (Int, String)): T = {
      val (id, name) = idName
      i.id = id
      i.name = Some(name)
      i
    }
Kunshan Wang's avatar
Kunshan Wang committed
28
  }
29

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

Kunshan Wang's avatar
Kunshan Wang committed
32
33
  val I1 = TypeInt(1) := internal("i1")
  val I6 = TypeInt(6) := internal("i6")
Kunshan Wang's avatar
Kunshan Wang committed
34
35
  val I8 = TypeInt(6) := internal("i8")
  val I16 = TypeInt(6) := internal("i16")
Kunshan Wang's avatar
Kunshan Wang committed
36
  val I32 = TypeInt(32) := internal("i32")
Kunshan Wang's avatar
Kunshan Wang committed
37
38
  val I52 = TypeInt(52) := internal("i52")
  val I64 = TypeInt(52) := internal("i64")
39
  val FLOAT = TypeFloat() := internal("float")
Kunshan Wang's avatar
Kunshan Wang committed
40
  val DOUBLE = TypeDouble() := internal("double")
Kunshan Wang's avatar
Kunshan Wang committed
41
  val VOID = TypeVoid() := internal("void")
Kunshan Wang's avatar
Kunshan Wang committed
42

Kunshan Wang's avatar
Kunshan Wang committed
43
  val BYTE = TypeInt(8) := internal("byte")
Kunshan Wang's avatar
Kunshan Wang committed
44
  val BYTE_ARRAY = TypeHybrid(Seq(), BYTE) := internal("byte_array")
Kunshan Wang's avatar
Kunshan Wang committed
45
46
47

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

Kunshan Wang's avatar
Kunshan Wang committed
48
49
  val STACK = TypeStackRef() := internal("stack")
  val THREAD = TypeThreadRef() := internal("thread")
Kunshan Wang's avatar
Kunshan Wang committed
50
  val TAGREF64 = TypeTagRef64() := internal("tagref64")
Kunshan Wang's avatar
Kunshan Wang committed
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")
56
57
58
}

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

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

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

Kunshan Wang's avatar
Kunshan Wang committed
80
  def inferType(v: SSAVariable): Type = v match {
Kunshan Wang's avatar
Kunshan Wang committed
81
    case c: Constant   => c.constTy
Kunshan Wang's avatar
Kunshan Wang committed
82
    case g: GlobalCell => irefOf(g.cellTy)
Kunshan Wang's avatar
Kunshan Wang committed
83
84
85
86
87
88
89
90
    case f: Function   => funcOf(f.sig)
    case p: NorParam   => p.ty
    case p: ExcParam   => REF_VOID
    case r: InstResult => {
      val resTys = inferInstResultTypes(r.inst)
      try {
        resTys(r.index)
      } catch {
Kunshan Wang's avatar
Kunshan Wang committed
91
        case e: IndexOutOfBoundsException => throw new UvmRefImplException(
Kunshan Wang's avatar
Kunshan Wang committed
92
93
94
95
96
97
98
          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)
99
    case i: InstCmp => i.opndTy match {
Kunshan Wang's avatar
Kunshan Wang committed
100
101
      case TypeVector(_, l) => Seq(vecOf(I1, l))
      case _                => Seq(I1)
102
    }
Kunshan Wang's avatar
Kunshan Wang committed
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
129
130
131
132
133
134
    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)
    case i: InstExtractElement => Seq(i.seqTy.elemTy)
    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))
    case i: InstGetVarPartIRef => Seq(ptrOrIRefOf(i.ptr, i.referentTy.varTy))
    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
    case i: InstNewThread      => Seq(THREAD)
Kunshan Wang's avatar
Kunshan Wang committed
135
136
    case i: InstSwapStack => i.curStackAction match {
      case RetWith(t) => t
Kunshan Wang's avatar
Kunshan Wang committed
137
      case _: KillOld => Seq()
Kunshan Wang's avatar
Kunshan Wang committed
138
139
    }
    case i: InstCommInst => i.inst.name.get match {
Kunshan Wang's avatar
Kunshan Wang committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
      case "@uvm.new_stack"          => Seq(STACK)
      case "@uvm.kill_stack"         => Seq()
      case "@uvm.thread_exit"        => Seq()
      case "@uvm.current_stack"      => Seq(STACK)
      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)
      case "@uvm.futex.wait_timeout" => Seq(I32)
      case "@uvm.futex.wake"         => Seq(I32)
      case "@uvm.futex.cmp_requeue"  => Seq(I32)
      case "@uvm.kill_dependency"    => Seq(i.typeList(0))
      case "@uvm.native.pin" => i.typeList(0) match {
        case TypeRef(t)  => Seq(ptrOf(t))
        case TypeIRef(t) => Seq(ptrOf(t))
Kunshan Wang's avatar
Kunshan Wang committed
162
      }
Kunshan Wang's avatar
Kunshan Wang committed
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
      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)

      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)

      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)

      case "@uvm.meta.pop_frame"          => Seq()
      case "@uvm.meta.push_frame"         => Seq()

      case "@uvm.meta.enable_watchpoint"  => Seq()
      case "@uvm.meta.disable_watchpoint" => Seq()

      case "@uvm.meta.set_trap_handler"   => Seq()
Kunshan Wang's avatar
Kunshan Wang committed
185
186
    }
  }
187
}