To protect your data, the CISO officer has suggested users to enable GitLab 2FA as soon as possible.

irReader.scala 14.2 KB
Newer Older
Kunshan Wang's avatar
Kunshan Wang committed
1
2
3
4
5
package uvm.ir.textinput

import uvm._
import uvm.types._
import uvm.ssavalues._
Kunshan Wang's avatar
Kunshan Wang committed
6
import uvm.ifuncs._
Kunshan Wang's avatar
Kunshan Wang committed
7

Kunshan Wang's avatar
Kunshan Wang committed
8
object UvmIRReader { 
Kunshan Wang's avatar
Kunshan Wang committed
9
  import UvmIRAST._
Kunshan Wang's avatar
Kunshan Wang committed
10
  import Later.Laterable
Kunshan Wang's avatar
Kunshan Wang committed
11

Kunshan Wang's avatar
Kunshan Wang committed
12
  def read(ir: CharSequence, globalBundle: Bundle): Bundle = {
Kunshan Wang's avatar
Kunshan Wang committed
13
    val ast = UvmIRParser(ir)
Kunshan Wang's avatar
Kunshan Wang committed
14
    read(ast, globalBundle)
Kunshan Wang's avatar
Kunshan Wang committed
15
  }
Kunshan Wang's avatar
Kunshan Wang committed
16

Kunshan Wang's avatar
Kunshan Wang committed
17
  def read(ir: java.io.Reader, globalBundle: Bundle): Bundle = {
Kunshan Wang's avatar
Kunshan Wang committed
18
    val ast = UvmIRParser(ir)
Kunshan Wang's avatar
Kunshan Wang committed
19
    read(ast, globalBundle)
Kunshan Wang's avatar
Kunshan Wang committed
20
  }
Kunshan Wang's avatar
Kunshan Wang committed
21
22
23
24
25
26
27
28
29
30

  object IDFactory {
    private var id: Int = 65536
    def getID(): Int = {
      val myID = id
      id = id + 1
      return myID
    }
  }

Kunshan Wang's avatar
Kunshan Wang committed
31
  def read(ir: IR, globalBundle: Bundle): Bundle = {
Kunshan Wang's avatar
Kunshan Wang committed
32
33
34
35
36
    val bundle = new Bundle()

    val phase1 = new Later()

    def resTy(te: TypeExpr): Type = te match {
Kunshan Wang's avatar
Kunshan Wang committed
37
      case ReferredType(g) => bundle.typeNs.get(g.name).getOrElse(globalBundle.typeNs(g.name))
Kunshan Wang's avatar
Kunshan Wang committed
38
39
40
41
42
43
44
45
46
47
48
49
50
      case tc: TypeCons => mkType(tc)
    }

    def mkType(tc: TypeCons): Type = {
      val ty = tc match {
        case IntCons(l) => TypeInt(l)
        case FloatCons => TypeFloat()
        case DoubleCons => TypeDouble()
        case RefCons(t) => TypeRef(null).later(phase1) { _.ty = resTy(t) }
        case IRefCons(t) => TypeIRef(null).later(phase1) { _.ty = resTy(t) }
        case WeakRefCons(t) => TypeWeakRef(null).later(phase1) { _.ty = resTy(t) }
        case StructCons(fs) => TypeStruct(null).later(phase1) { _.fieldTy = fs.map(resTy) }
        case ArrayCons(et, l) => TypeArray(null, l).later(phase1) { _.elemTy = resTy(et) }
Kunshan Wang's avatar
Kunshan Wang committed
51
        case HybridCons(fp, vp) => TypeHybrid(null, null).later(phase1) { h => h.fixedPart = resTy(fp); h.varPart = resTy(vp) }
Kunshan Wang's avatar
Kunshan Wang committed
52
53
54
55
56
57
58
59
60
61
62
63
        case VoidCons => TypeVoid()
        case FuncCons(s) => TypeFunc(null).later(phase1) { _.sig = resSig(s) }
        case ThreadCons => TypeThread()
        case StackCons => TypeStack()
        case TagRef64Cons => TypeTagRef64()
        case _ => throw new RuntimeException("foo")
      }
      ty.id = IDFactory.getID()
      return ty
    }

    def resSig(se: FuncSigExpr): FuncSig = se match {
Kunshan Wang's avatar
Kunshan Wang committed
64
      case ReferredFuncSig(g) => bundle.funcSigNs.get(g.name).getOrElse(globalBundle.funcSigNs(g.name))
Kunshan Wang's avatar
Kunshan Wang committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
      case FuncSigCons(r, ps) => mkSig(r, ps)
    }

    def mkSig(r: TypeExpr, ps: Seq[TypeExpr]): FuncSig = {
      val sig = FuncSig(null, null).later(phase1) { sig =>
        sig.retTy = resTy(r)
        sig.paramTy = ps.map(resTy)
      }
      sig.id = IDFactory.getID()
      return sig
    }

    ir.topLevels.foreach {
      case TypeDef(n, c) => {
        val ty = mkType(c)
        ty.name = Some(n.name)
        bundle.typeNs.add(ty)
      }
      case FuncSigDef(n, FuncSigCons(r, ps)) => {
        val sig = mkSig(r, ps)
        sig.name = Some(n.name)
        bundle.funcSigNs.add(sig)
      }
Kunshan Wang's avatar
Kunshan Wang committed
88
      case _ => {}
Kunshan Wang's avatar
Kunshan Wang committed
89
90
91
92
93
94
95
    }

    phase1.doAll()

    val phase2 = new Later()

    def resGV(t: Type, ce: ConstExpr): GlobalValue = ce match {
Kunshan Wang's avatar
Kunshan Wang committed
96
      case ReferredConst(g) => bundle.globalValueNs.get(g.name).getOrElse(globalBundle.globalValueNs(g.name))
Kunshan Wang's avatar
Kunshan Wang committed
97
98
99
100
101
102
103
104
105
      case c: ConstCons => mkConst(t, c)
    }

    def mkConst(t: Type, c: ConstCons): DeclaredConstant = {
      val con = c match {
        case IntConstCons(num) => ConstInt(t, num)
        case FloatConstCons(num) => ConstFloat(t, num)
        case DoubleConstCons(num) => ConstDouble(t, num)
        case StructConstCons(fs) => ConstStruct(t, null).later(phase2) {
Kunshan Wang's avatar
Kunshan Wang committed
106
          _.fields = for ((ft,f) <- t.asInstanceOf[TypeStruct].fieldTy.zip(fs)) yield resGV(ft, f)
Kunshan Wang's avatar
Kunshan Wang committed
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 NullConstCons => ConstNull(t)
      }
      con.id = IDFactory.getID()
      return con
    }

    def mkGlobalData(t: Type): GlobalData = {
      val gd = GlobalData(t)
      gd.id = IDFactory.getID()
      return gd
    }

    def mkGlobalDataConst(gd: GlobalData): ConstGlobalData = {
      val gdc = ConstGlobalData(gd)
      return gdc
    }

    def mkFunc(sig: FuncSig): Function = {
      val func = new Function()
      func.sig = sig
      return func
    }

    def mkFuncConst(func: Function): ConstFunc = {
      val fc = ConstFunc(func)
      return fc
    }
Kunshan Wang's avatar
Kunshan Wang committed
135
136
137
138
    
    def tryReuseFuncID(name: String): Option[Int] = {
      globalBundle.funcNs.get(name).map(_.id)
    }
Kunshan Wang's avatar
Kunshan Wang committed
139
140
141
142

    def declFunc(n: GID, s: FuncSigExpr): Function = {
      val sig = resSig(s)
      val func = mkFunc(sig)
Kunshan Wang's avatar
Kunshan Wang committed
143
144
      val maybeOldID = tryReuseFuncID(n.name)
      func.id = maybeOldID.getOrElse(IDFactory.getID())
Kunshan Wang's avatar
Kunshan Wang committed
145
146
      func.name = Some(n.name)
      bundle.funcNs.add(func)
Kunshan Wang's avatar
Kunshan Wang committed
147
148
149
150
151
      
      if (maybeOldID == None) {
        val fc = mkFuncConst(func)
        bundle.globalValueNs.add(fc)
      }
Kunshan Wang's avatar
Kunshan Wang committed
152
153
154
155
156
157
158
159
160
161
162
163

      return func
    }

    var funcDefs: List[(Function, Seq[LID], FuncBodyDef)] = Nil

    ir.topLevels.foreach {
      case ConstDef(n, t, c) => {
        val ty = resTy(t)
        val con = mkConst(ty, c)
        con.name = Some(n.name)
        bundle.declConstNs.add(con)
Kunshan Wang's avatar
Kunshan Wang committed
164
        bundle.globalValueNs.add(con)
Kunshan Wang's avatar
Kunshan Wang committed
165
166
167
168
169
      }
      case GlobalDataDef(n, t) => {
        val ty = resTy(t)
        val gd = mkGlobalData(ty)
        gd.name = Some(n.name)
Kunshan Wang's avatar
Kunshan Wang committed
170
        bundle.globalDataNs.add(gd)
Kunshan Wang's avatar
Kunshan Wang committed
171
172

        val gdc = mkGlobalDataConst(gd)
Kunshan Wang's avatar
Kunshan Wang committed
173
        bundle.globalValueNs.add(gdc)
Kunshan Wang's avatar
Kunshan Wang committed
174
175
176
177
178
179
180
181
      }
      case FuncDecl(n, s) => {
        declFunc(n, s)
      }
      case FuncDef(n, s, ps, body) => {
        val func = declFunc(n, s)
        funcDefs = (func, ps, body) :: funcDefs
      }
Kunshan Wang's avatar
Kunshan Wang committed
182
      case _ => {}
Kunshan Wang's avatar
Kunshan Wang committed
183
184
185
186
187
188
189
    }

    phase2.doAll()

    def defFunc(func: Function, ps: Seq[LID], body: FuncBodyDef) {
      val cfg = new CFG()
      cfg.func = func
Kunshan Wang's avatar
Kunshan Wang committed
190
      func.cfg = Some(cfg)
Kunshan Wang's avatar
Kunshan Wang committed
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211

      cfg.params = ps.zipWithIndex.map {
        case (n, i) =>
          val param = Parameter(func.sig, i)
          param.id = IDFactory.getID()
          param.name = Some(n.name)
          cfg.lvNs.add(param)
          param
      }

      val phase3 = new Later()

      val phase4 = new Later()

      def makeBB(bbd: BasicBlockDef): BasicBlock = {
        val bb = new BasicBlock()
        bb.id = IDFactory.getID()
        bb.name = bbd.name.map(_.name)
        cfg.bbNs.add(bb)

        phase3 { () =>
Kunshan Wang's avatar
Kunshan Wang committed
212
213
214
215
216
          bb.insts = for (instDef <- bbd.insts) yield {
            val inst = mkInst(instDef)
            cfg.lvNs.add(inst)
            inst
          }
Kunshan Wang's avatar
Kunshan Wang committed
217
218
219
220
221
222
223
224
225
        }

        return bb
      }

      def resBB(n: LID): BasicBlock = cfg.bbNs(n.name)

      def resVal(ty: Option[Type], ve: ValueExpr): Value = ve match {
        case RefValue(id) => id match {
Kunshan Wang's avatar
Kunshan Wang committed
226
          case GID(n) => bundle.globalValueNs(n)
Kunshan Wang's avatar
Kunshan Wang committed
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
          case LID(n) => cfg.lvNs(n)
        }
        case InlineValue(cc) => ty match {
          case None => throw new TextIRParsingException(
            "Cannot use inline constant value when its type is not a user-defined type.")
          case Some(t) => mkConst(t, cc)
        }
      }

      def vc(ty: Type, ve: ValueExpr): Value = resVal(Some(ty), ve)
      def vnc(ve: ValueExpr): Value = resVal(None, ve)

      def resArgs(s: FuncSig, a: Seq[ValueExpr]): Seq[Value] = s.paramTy.zip(a).map {
        case (t, v) => vc(t, v)
      }

      def resKA(ka: Seq[LID]): Seq[Value] = ka.map(n => cfg.lvNs(n.name))

      def mkInst(instDef: InstDef): Instruction = {

        val inst: Instruction = instDef.cons match {
          case BinOpCons(optr, t, op1, op2) =>
            InstBinOp(BinOptr.withName(optr), resTy(t), null, null).later(phase4) { i =>
              i.op1 = vc(i.opndTy, op1); i.op2 = vc(i.opndTy, op2)
            }
          case CmpCons(optr, t, op1, op2) =>
            InstCmp(CmpOptr.withName(optr), resTy(t), null, null).later(phase4) { i =>
              i.op1 = vc(i.opndTy, op1); i.op2 = vc(i.opndTy, op2)
            }
          case ConvCons(optr, t1, t2, op) =>
            InstConv(ConvOptr.withName(optr), resTy(t1), resTy(t2), null).later(phase4) { i =>
              i.opnd = vc(i.fromTy, op)
            }
          case SelectCons(t, c, tr, fa) =>
            InstSelect(resTy(t), null, null, null).later(phase4) { i =>
              i.cond = vnc(c); i.ifTrue = vc(i.opndTy, tr); i.ifFalse = vc(i.opndTy, fa)
            }
          case BranchCons(d) =>
            InstBranch(resBB(d))
          case Branch2Cons(c, tr, fa) =>
            InstBranch2(null, resBB(tr), resBB(fa)).later(phase4) { i =>
              i.cond = vnc(c)
            }
          case SwitchCons(t, o, d, cs) =>
            InstSwitch(resTy(t), null, resBB(d), null).later(phase4) { i =>
              i.opnd = vc(i.opndTy, o)
              i.cases = cs.map {
                case (v, b) =>
                  (vc(i.opndTy, v), resBB(b))
              }
            }
          case PhiCons(t, cs) =>
            InstPhi(resTy(t), null).later(phase4) { i =>
              i.cases = cs.map {
                case (b, v) =>
                  (resBB(b), vc(i.opndTy, v))
              }
            }
          case CallCons(s, f, a, ka) =>
            InstCall(resSig(s), null, null, null).later(phase4) { i =>
              i.callee = vnc(f); i.args = resArgs(i.sig, a); i.keepAlives = resKA(ka)
            }
          case InvokeCons(s, f, a, n, e, ka) =>
            InstInvoke(resSig(s), null, null, resBB(n), resBB(e), null).later(phase4) { i =>
              i.callee = vnc(f); i.args = resArgs(i.sig, a); i.keepAlives = resKA(ka)
            }
          case TailCallCons(s, f, a) =>
            InstTailCall(resSig(s), null, null).later(phase4) { i =>
              i.callee = vnc(f); i.args = resArgs(i.sig, a)
            }
          case RetCons(t, v) =>
            InstRet(resTy(t), null).later(phase4) { i =>
              i.retVal = vc(i.retTy, v)
            }
          case RetVoidCons =>
            InstRetVoid()
          case ThrowCons(v) =>
            InstThrow(null).later(phase4) { i =>
              i.excVal = vnc(v)
            }
          case LandingpadCons =>
            InstLandingpad()
Kunshan Wang's avatar
Kunshan Wang committed
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
          case ExtractValueCons(t, n, v) =>
            InstExtractValue(resTy(t).asInstanceOf[TypeStruct], n, null).later(phase4) { i =>
              i.opnd = vc(i.strTy, v)
            }
          case InsertValueCons(t, n, v, nv) =>
            InstInsertValue(resTy(t).asInstanceOf[TypeStruct], n, null, null).later(phase4) { i =>
              i.opnd = vc(i.strTy, v); i.newVal = vc(i.strTy.fieldTy(n), nv)
            }
          case NewCons(t) => InstNew(resTy(t))
          case NewHybridCons(t, v) =>
            InstNewHybrid(resTy(t).asInstanceOf[TypeHybrid], null).later(phase4) { i =>
              i.length = vnc(v)
            }
          case AllocaCons(t) => InstAlloca(resTy(t))
          case AllocaHybridCons(t, v) =>
            InstAllocaHybrid(resTy(t).asInstanceOf[TypeHybrid], null).later(phase4) { i =>
              i.length = vnc(v)
            }
          case GetIRefCons(t, v) =>
            InstGetIRef(resTy(t), null).later(phase4) { i =>
              i.opnd = vc(i.referentTy, v)
            }
          case GetFieldIRefCons(t, n, v) =>
            InstGetFieldIRef(resTy(t).asInstanceOf[TypeStruct], n, null).later(phase4) { i =>
              i.opnd = vc(i.referentTy, v)
            }
          case GetElemIRefCons(t, v, n) =>
            InstGetElemIRef(resTy(t).asInstanceOf[TypeArray], null, null).later(phase4) { i =>
              i.opnd = vc(i.referentTy, v); i.index = vc(i.referentTy, n)
            }
          case ShiftIRefCons(t, v, n) =>
            InstShiftIRef(resTy(t), null, null).later(phase4) { i =>
              i.opnd = vc(i.referentTy, v); i.offset = vc(i.referentTy, n)
            }
          case GetFixedPartIRefCons(t, v) =>
            InstGetFixedPartIRef(resTy(t).asInstanceOf[TypeHybrid], null).later(phase4) { i =>
              i.opnd = vc(i.referentTy, v)
            }
          case GetVarPartIRefCons(t, v) =>
            InstGetVarPartIRef(resTy(t).asInstanceOf[TypeHybrid], null).later(phase4) { i =>
              i.opnd = vc(i.referentTy, v)
            }
          case LoadCons(o, t, v) =>
            InstLoad(MemoryOrdering.withName(o), resTy(t), null).later(phase4) { i =>
              i.loc = vnc(v)
            }
          case StoreCons(o, t, v, nv) =>
            InstStore(MemoryOrdering.withName(o), resTy(t), null, null).later(phase4) { i =>
              i.loc = vnc(v); i.newVal = vc(i.referentTy, nv)
            }
          case CmpXchgCons(os, of, t, v, e, d) =>
            InstCmpXchg(MemoryOrdering.withName(os), MemoryOrdering.withName(of),
              resTy(t), null, null, null).later(phase4) { i =>
                i.loc = vnc(v); i.expected = vc(i.referentTy, e); i.desired = vc(i.referentTy, d)
              }
          case AtomicRMWCons(o, op, t, v, v2) =>
            InstAtomicRMW(MemoryOrdering.withName(o), AtomicRMWOptr.withName(op),
              resTy(t), null, null).later(phase4) { i =>
                i.loc = vnc(v); i.opnd = vc(i.referentTy, v2)
              }
          case FenceCons(o) => InstFence(MemoryOrdering.withName(o))
          case TrapCons(t, n, e, ka) =>
            InstTrap(resTy(t), resBB(n), resBB(e), null).later(phase4) { i =>
              i.keepAlives = resKA(ka)
            }
          case WatchpointCons(wid, t, d, n, e, ka) =>
            InstWatchpoint(wid, resTy(t), resBB(d), resBB(n), resBB(e), null).later(phase4) { i =>
              i.keepAlives = resKA(ka)
            }
          case CCallCons(cc, s, f, a) =>
            InstCCall(CallConv.withName(cc), resSig(s), null, null).later(phase4) { i =>
              i.callee = vnc(f); i.args = resArgs(i.sig, a)
            }
          case NewStackCons(s, f, a) =>
            InstNewStack(resSig(s), null, null).later(phase4) { i =>
              i.callee = vnc(f); i.args = resArgs(i.sig, a)
            }
          case ICallCons(f, a, ka) =>
            InstICall(IFuncs(f.name), null, null).later(phase4) { i =>
              i.args = resArgs(i.iFunc.sig, a); i.keepAlives = resKA(ka)
            }
          case IInvokeCons(f, a, n, e, ka) =>
            InstIInvoke(IFuncs(f.name), null, resBB(n), resBB(e), null).later(phase4) { i =>
              i.args = resArgs(i.iFunc.sig, a); i.keepAlives = resKA(ka)
            }

Kunshan Wang's avatar
Kunshan Wang committed
395
396
        }

Kunshan Wang's avatar
Kunshan Wang committed
397
398
399
        inst.id = IDFactory.getID()
        inst.name = for (lid <- instDef.name) yield lid.name

Kunshan Wang's avatar
Kunshan Wang committed
400
401
402
403
        return inst
      }

      val entry = makeBB(body.entry)
Kunshan Wang's avatar
Kunshan Wang committed
404
      val rest = body.bbs.map(makeBB)
Kunshan Wang's avatar
Kunshan Wang committed
405

Kunshan Wang's avatar
Kunshan Wang committed
406
      val bbs = Seq(entry) ++ rest
Kunshan Wang's avatar
Kunshan Wang committed
407
408
409
410
411
412
413

      cfg.bbs = bbs
      cfg.entry = entry

      phase3.doAll()

      phase4.doAll()
Kunshan Wang's avatar
Kunshan Wang committed
414
415
416
417

      for (bb <- cfg.bbs; i <- bb.insts) {
        i.resolve()
      }
Kunshan Wang's avatar
Kunshan Wang committed
418
419
420
421
422
423
424
425
426
    }

    for ((func, ps, body) <- funcDefs) {
      defFunc(func, ps, body)
    }

    return bundle
  }

Kunshan Wang's avatar
Kunshan Wang committed
427
}