BundleConstructor.scala 18.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
package uvm.ir.irbuilder

import uvm._
import uvm.ssavariables._
import uvm.types._
import uvm.utils.Later
import uvm.utils.Later._
import uvm.ir.irbuilder.IRBuilderNode._
import scala.collection.mutable.ArrayBuffer

/** This class converts the soup of ID-referenced nodes into the refimpl's internal AST. */
class BundleConstructor(idNameMap: Map[MuID, MuName], nodeList: Seq[IRBuilderNode], globalBundle: GlobalBundle) {
13

14 15
  val idNodeMap = nodeList.map(n => (n.id, n)).toMap

Kunshan Wang's avatar
Kunshan Wang committed
16 17 18 19 20 21 22
  def findNode[T <: IRBuilderNode](id: MuID): T = {
    idNodeMap.getOrElse(id, {
      val maybeName = idNameMap.get(id)
      throw new NodeIDNotFoundException(id, maybeName)
    }).asInstanceOf[T]
  }

23 24 25 26 27 28 29 30 31 32 33
  val bundle = new TrantientBundle()

  def setIDAndMaybeName[T <: IdentifiedSettable](obj: T, id: MuID): T = {
    obj.id = id
    obj.name = idNameMap.get(id)
    obj
  }

  def cascadeLookup[T <: Identified](id: MuID, ns1: Namespace[T], ns2: Namespace[T]): T = {
    ns1.get(id).getOrElse {
      ns2.get(id).getOrElse {
Kunshan Wang's avatar
Kunshan Wang committed
34 35
        val maybeName = idNameMap.get(id)
        throw new UnknownIDException(id, maybeName)
36 37 38 39 40
      }
    }
  }

  implicit def resTy(id: MuID): Type = cascadeLookup(id, bundle.typeNs, globalBundle.typeNs)
41
  implicit def resTys(ids: Seq[MuID]): Seq[Type] = ids.map(resTy)
42 43 44

  implicit def resSig(id: MuID): FuncSig = cascadeLookup(id, bundle.funcSigNs, globalBundle.funcSigNs)

45 46
  // used by InstSwitch
  def resConst(id: MuID): Constant = cascadeLookup(id, bundle.constantNs, globalBundle.constantNs)
47

48 49
  // These auto-resolution may conflict with the resVar method when building instructions. Explicitly enable them in methods.
  def resGlobalVar(id: MuID): GlobalVariable = cascadeLookup(id, bundle.globalVarNs, globalBundle.globalVarNs)
50

51
  def resFunc(id: MuID): Function = cascadeLookup(id, bundle.funcNs, globalBundle.funcNs)
52

53
  def resConstInt(id: MuID): ConstInt = resGlobalVar(id) match {
54 55 56 57
    case ci: ConstInt => ci
    case other        => throw new AssertionError("Constant int expected, but %d found. ID: %d".format(other.getClass.getName, id))
  }

58 59 60 61
  def needType[E <: Type](id: MuID, expectedType: Class[E], n: String): E = {
    val t = resTy(id)
    if (!(expectedType.isAssignableFrom(t.getClass))) {
      throw new IRBuilderException("Type id:%d name:%s does not match expected type. Expected %s, actually %s.".format(
Kunshan Wang's avatar
Kunshan Wang committed
62
        id, idNameMap.getOrElse(id, "(no name)"), n, t))
63 64 65 66 67 68 69 70 71 72 73 74
    }
    t.asInstanceOf[E]
  }

  def needInt(id: MuID) = needType(id, classOf[TypeInt], "int")
  def needStruct(id: MuID) = needType(id, classOf[TypeStruct], "struct")
  def needAbsStruct(id: MuID) = needType(id, classOf[AbstractStructType], "struct or hybrid")
  def needArray(id: MuID) = needType(id, classOf[TypeArray], "array")
  def needVector(id: MuID) = needType(id, classOf[TypeVector], "vector")
  def needHybrid(id: MuID) = needType(id, classOf[TypeHybrid], "hybrid")
  def needSeq(id: MuID) = needType(id, classOf[AbstractSeqType], "array or vector")

75
  def makeBundle(): TrantientBundle = {
76 77 78
    // Enable auto resolution in this global scope
    @inline implicit def _resGlobalVar = resGlobalVar _
    @inline implicit def _resFunc = resFunc _
79 80 81 82 83

    // Classify nodes into categories for phasing.
    val typeNodes = new ArrayBuffer[IRTypeNode]
    val sigNodes = new ArrayBuffer[IRSigNode]
    val constNodes = new ArrayBuffer[IRConstNode]
84
    val expFuncNodes = new ArrayBuffer[NodeExpFunc]
85 86 87 88 89 90 91 92
    val otherTopLevelNodes = new ArrayBuffer[OtherTopLevelNode]
    val funcVers = new ArrayBuffer[NodeFuncVer]()
    val otherNodes = new ArrayBuffer[IRBuilderNode]

    nodeList foreach {
      case n: IRTypeNode        => typeNodes += n
      case n: IRSigNode         => sigNodes += n
      case n: IRConstNode       => constNodes += n
93
      case n: NodeExpFunc       => expFuncNodes += n
94 95 96 97 98 99 100 101 102 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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
      case n: OtherTopLevelNode => otherTopLevelNodes += n
      case n: NodeFuncVer       => funcVers += n
      case n                    => otherNodes += n
    }

    // Phase 1: create types and sigs, and resolve their refs

    val phase1 = new Later()

    typeNodes foreach { irNode =>
      val muNode: Type = irNode match {
        case NodeTypeInt(id, len)      => new TypeInt(len)
        case NodeTypeFloat(id)         => new TypeFloat()
        case NodeTypeDouble(id)        => new TypeDouble()
        case NodeTypeUPtr(id, ty)      => new TypeUPtr(null).later(phase1) { t => t.ty = ty }
        case NodeTypeUFuncPtr(id, sig) => new TypeUFuncPtr(null).later(phase1) { t => t.sig = sig }

        case NodeTypeStruct(id, fieldTys) => new TypeStruct(null).later(phase1) { t =>
          t.fieldTys = fieldTys.map(resTy)
        }
        case NodeTypeHybrid(id, fixedTys, varTy) => new TypeHybrid(null, null).later(phase1) { t =>
          t.fieldTys = fixedTys.map(resTy)
          t.varTy = varTy
        }
        case NodeTypeArray(id, elemTy, len) => new TypeArray(null, len).later(phase1) { t =>
          t.elemTy = elemTy
        }
        case NodeTypeVector(id, elemTy, len) => new TypeVector(null, len).later(phase1) { t =>
          t.elemTy = elemTy
        }
        case NodeTypeVoid(id)           => new TypeVoid()

        case NodeTypeRef(id, ty)        => new TypeRef(null).later(phase1) { t => t.ty = ty }
        case NodeTypeIRef(id, ty)       => new TypeIRef(null).later(phase1) { t => t.ty = ty }
        case NodeTypeWeakRef(id, ty)    => new TypeWeakRef(null).later(phase1) { t => t.ty = ty }
        case NodeTypeFuncRef(id, sig)   => new TypeFuncRef(null).later(phase1) { t => t.sig = sig }

        case NodeTypeTagRef64(id)       => new TypeTagRef64()
        case NodeTypeThreadRef(id)      => new TypeThreadRef()
        case NodeTypeStackRef(id)       => new TypeStackRef()
        case NodeTypeFrameCursorRef(id) => new TypeFrameCursorRef()
        case NodeTypeIRBuilderRef(id)   => new TypeIRBuilderRef()
      }
      setIDAndMaybeName(muNode, irNode.id)
      bundle.typeNs.add(muNode)
    }

    sigNodes foreach { irNode =>
      val NodeFuncSig(id, paramTys, retTys) = irNode
      val muNode: FuncSig = new FuncSig(null, null).later(phase1) { sig =>
        sig.paramTys = paramTys.map(resTy)
        sig.retTys = retTys.map(resTy)
      }
      setIDAndMaybeName(muNode, irNode.id)
      bundle.funcSigNs.add(muNode)
    }

    phase1.doAll()

    // Now, all types and sigs are created and their references are resolved.

    // Phase 2: create and resolve consts, global cells, funcs, expfuncs and resolve their refs.

    val phase2 = new Later()

    constNodes foreach { irNode =>
      val muNode: Constant = irNode match {
        case NodeConstInt(id, ty, value)     => new ConstInt(ty, value)
        case NodeConstFloat(id, ty, value)   => new ConstFloat(ty, value)
        case NodeConstDouble(id, ty, value)  => new ConstDouble(ty, value)
        case NodeConstNull(id, ty)           => new ConstNull(ty)
        case NodeConstExtern(id, ty, symbol) => new ConstExtern(ty, symbol)
        case NodeConstSeq(id, ty, elems) => new ConstSeq(ty, null).later(phase2) { c =>
          c.elems = elems.map(resGlobalVar)
        }
      }
      setIDAndMaybeName(muNode, irNode.id)
      bundle.constantNs.add(muNode)
    }

    otherTopLevelNodes foreach {
      case NodeGlobalCell(id: MuID, ty: MuTypeNode) => {
        val muNode = new GlobalCell(ty)
        setIDAndMaybeName(muNode, id)
        bundle.globalCellNs.add(muNode)
      }
      case NodeFunc(id: MuID, sig: MuFuncSigNode) => {
        val muNode = new Function()
        muNode.sig = sig
        setIDAndMaybeName(muNode, id)
        bundle.funcNs.add(muNode)
      }
186 187 188 189
    }
    
    // Build ExposedFunc instances after building Function instances.
    expFuncNodes foreach {
190
      case NodeExpFunc(id: MuID, func: MuFuncNode, callconv: Flag, cookie: MuConstIntNode) => {
191
        val muNode = new ExposedFunc(func, callconv, resConstInt(cookie))
192 193 194 195
        setIDAndMaybeName(muNode, id)
        bundle.expFuncNs.add(muNode)
      }
    }
196

197
    phase2.doAll()
198

199
    // Now all top-level definitions are created and their inter-references are resolved
200

201 202
    // Phase 3: Define all defined functions (.funcdef)
    val phase3 = new Later()
203

204 205 206 207
    funcVers foreach { irNode =>
      val muNode = new FuncVer(irNode.func)
      setIDAndMaybeName(muNode, irNode.id)
      bundle.funcVerNs.add(muNode)
208 209

      phase3 { () =>
210 211 212
        defineFunction(irNode, muNode)
      }
    }
213

214 215 216 217
    phase3.doAll()

    bundle
  }
218 219

  def defineFunction(irNode: NodeFuncVer, muFuncVer: FuncVer): Unit = {
Kunshan Wang's avatar
Kunshan Wang committed
220 221
    muFuncVer.bbs = new ArrayBuffer()
    muFuncVer.bbNs = bundle.allNs.makeSubSpace("basic block")
222
    val irBBmuBBs = irNode.bbs.map { bbID =>
Kunshan Wang's avatar
Kunshan Wang committed
223
      val irBB = findNode[NodeBB](bbID)
224 225 226 227 228 229 230 231
      val muBB = new BasicBlock(muFuncVer)
      setIDAndMaybeName(muBB, bbID)
      muFuncVer.bbs += muBB
      muFuncVer.bbNs.add(muBB)
      (irBB, muBB)
    }

    for ((irBB, muBB) <- irBBmuBBs) {
Kunshan Wang's avatar
Kunshan Wang committed
232 233 234 235 236
      muBB.norParams = new ArrayBuffer()
      muBB.excParam = None
      muBB.insts = new ArrayBuffer()
      muBB.localVarNs = bundle.allNs.makeSubSpace("local variable")
      muBB.localInstNs = bundle.allNs.makeSubSpace("instruction")
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
      for ((norParamID, norParamTyID) <- irBB.norParamIDs zip irBB.norParamTys) {
        val muType = resTy(norParamTyID)
        val muNorParam = NorParam(muType)
        setIDAndMaybeName(muNorParam, norParamID)
        muBB.norParams += muNorParam
        muBB.localVarNs.add(muNorParam)
      }

      for (excParamID <- irBB.excParamID) {
        val muExcParam = ExcParam()
        setIDAndMaybeName(muExcParam, excParamID)
        muBB.excParam = Some(muExcParam)
        muBB.localVarNs.add(muExcParam)
      }

      for (instID <- irBB.insts) {
Kunshan Wang's avatar
Kunshan Wang committed
253
        val irInst = findNode[IRInstNode](instID)
254 255 256 257 258 259
        val muInst = defineInst(muFuncVer, muBB, irInst)
        setIDAndMaybeName(muInst, instID)
        muBB.insts += muInst
        muBB.localInstNs.add(muInst)
      }
    }
260
  }
261 262 263 264 265 266 267 268 269 270 271 272 273

  def defineInst(muFuncVer: FuncVer, muBB: BasicBlock, irInst: IRInstNode): Instruction = try {
    import BundleConstructorImplicitMagics._

    implicit val self = this
    implicit val bbNs = muFuncVer.bbNs
    implicit val localVarNs = muBB.localVarNs

    implicit def resVar(id: MuVarNode): SSAVariable = {
      localVarNs.get(id).getOrElse {
        cascadeLookup(id, bundle.globalVarNs, globalBundle.globalVarNs)
      }
    }
Kunshan Wang's avatar
Kunshan Wang committed
274

275 276 277 278 279 280 281
    implicit def resVars(ids: Seq[MuVarNode]): Seq[SSAVariable] = {
      ids.map(resVar)
    }

    // used by keepalives
    def resLocalVar(id: MuVarNode): LocalVariable = {
      localVarNs.get(id).getOrElse {
Kunshan Wang's avatar
Kunshan Wang committed
282 283 284
        val vars = localVarNs.all.map("  " + _.repr).mkString("\n")
        throw new IRBuilderException("Local variable id:%d name:%s not found.\nFYI: Current local variables are:\n%s".format(
          id, idNameMap.getOrElse(id, "(no name)"), vars))
285 286 287 288 289 290 291 292 293 294
      }
    }

    @inline
    implicit def resBB(id: MuBBNode): BasicBlock = {
      bbNs(id)
    }

    @inline
    implicit def resDestClause(id: MuDestClause): DestClause = {
Kunshan Wang's avatar
Kunshan Wang committed
295
      val NodeDestClause(_, dest, vars) = findNode[NodeDestClause](id)
296 297 298 299 300
      DestClause(dest, vars.map(resVar))
    }

    @inline
    implicit def resExcClause(maybeID: Option[MuExcClause]): Option[ExcClause] = maybeID map { id =>
Kunshan Wang's avatar
Kunshan Wang committed
301
      val irExcClause = findNode[NodeExcClause](id)
302 303 304 305 306
      ExcClause(irExcClause.nor, irExcClause.exc)
    }

    @inline
    implicit def resKeepalives(maybeID: Option[MuKeepaliveClause]): Seq[LocalVariable] = maybeID map { id =>
Kunshan Wang's avatar
Kunshan Wang committed
307
      val irKeepaliveClause = findNode[NodeKeepaliveClause](id)
308
      irKeepaliveClause.vars.map(resLocalVar)
Kunshan Wang's avatar
Kunshan Wang committed
309
    } getOrElse (Seq())
310 311 312

    @inline
    implicit def resCurStackClause(id: MuCurStackClause): CurStackAction = {
Kunshan Wang's avatar
Kunshan Wang committed
313
      val irCurStackClause = findNode[IRCurStackClauseNode](id)
314 315
      irCurStackClause match {
        case NodeCscRetWith(_, tys) => RetWith(tys)
Kunshan Wang's avatar
Kunshan Wang committed
316
        case NodeCscKillOld(_)      => KillOld()
317 318
      }
    }
Kunshan Wang's avatar
Kunshan Wang committed
319

320 321
    @inline
    implicit def resNewStackClause(id: MuNewStackClause): NewStackAction = {
Kunshan Wang's avatar
Kunshan Wang committed
322
      val irNewStackClause = findNode[IRNewStackClauseNode](id)
323 324
      irNewStackClause match {
        case NodeNscPassValues(_, tys, vars) => PassValues(tys, vars)
Kunshan Wang's avatar
Kunshan Wang committed
325
        case NodeNscThrowExc(_, exc)         => ThrowExc(exc)
326 327 328 329
      }
    }

    val muInst: Instruction = irInst match {
330 331
      case NodeBinOp(id, resultID, statusResultIDs, optr, flags, ty, opnd1, opnd2, excClause) =>
        InstBinOp(optr, flags, ty, opnd1, opnd2, null) --> (Seq(resultID)++statusResultIDs) EXC excClause
332 333 334 335 336 337 338 339 340 341 342 343 344 345
      case NodeCmp(id, resultID, optr, ty, opnd1, opnd2) =>
        InstCmp(optr, ty, opnd1, opnd2) --> resultID
      case NodeConv(id, resultID, optr, fromTy, toTy, opnd) =>
        InstConv(optr, fromTy, toTy, opnd) --> resultID
      case NodeSelect(id, resultID, condTy, opndTy, cond, ifTrue, ifFalse) =>
        InstSelect(condTy, opndTy, cond, ifTrue, ifFalse) --> resultID
      case NodeBranch(id, dest) =>
        InstBranch(dest)
      case NodeBranch2(id, cond, ifTrue, ifFalse) =>
        InstBranch2(cond, ifTrue, ifFalse)
      case NodeSwitch(id, opndTy, opnd, defaultDest, cases, dests) => {
        val caseDests = for ((c, d) <- cases zip dests) yield {
          (resConst(c), resDestClause(d))
        }
Kunshan Wang's avatar
Kunshan Wang committed
346
        InstSwitch(opndTy, opnd, defaultDest, ArrayBuffer(caseDests: _*))
347 348
      }
      case NodeCall(id, resultIDs, sig, callee, args, excClause, keepaliveClause) =>
349
        InstCall(sig, callee, args, null, keepaliveClause) --> resultIDs EXC excClause
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
      case NodeTailCall(id, sig, callee, args) =>
        InstTailCall(sig, callee, args)
      case NodeRet(id, rvs) =>
        InstRet(muFuncVer, rvs)
      case NodeThrow(id, exc) =>
        InstThrow(exc)
      case NodeExtractValue(id, resultID, strty, index, opnd) =>
        InstExtractValue(needStruct(strty), index, opnd) --> resultID
      case NodeInsertValue(id, resultID, strty, index, opnd, newval) =>
        InstInsertValue(needStruct(strty), index, opnd, newval) --> resultID
      case NodeExtractElement(id, resultID, seqty, indty, opnd, index) =>
        InstExtractElement(needSeq(seqty), needInt(indty), opnd, index) --> resultID
      case NodeInsertElement(id, resultID, seqty, indty, opnd, index, newval) =>
        InstInsertElement(needSeq(seqty), needInt(indty), opnd, index, newval) --> resultID
      case NodeShuffleVector(id, resultID, vecty, maskty, vec1, vec2, mask) =>
        InstShuffleVector(needVector(vecty), needVector(maskty), vec1, vec2, mask) --> resultID
      case NodeNew(id, resultID, allocty, excClause) =>
367
        InstNew(allocty, null) --> resultID EXC excClause
368
      case NodeNewHybrid(id, resultID, allocty, lenty, length, excClause) =>
369
        InstNewHybrid(needHybrid(allocty), needInt(lenty), length, null) --> resultID EXC excClause
370
      case NodeAlloca(id, resultID, allocty, excClause) =>
371
        InstAlloca(allocty, null) --> resultID EXC excClause
372
      case NodeAllocaHybrid(id, resultID, allocty, lenty, length, excClause) =>
373
        InstAllocaHybrid(needHybrid(allocty), needInt(lenty), length, null) --> resultID EXC excClause
374 375 376
      case NodeGetIRef(id, resultID, refty, opnd) =>
        InstGetIRef(refty, opnd) --> resultID
      case NodeGetFieldIRef(id, resultID, isPtr, refty, index, opnd) =>
Kunshan Wang's avatar
Kunshan Wang committed
377
        InstGetFieldIRef(isPtr, needAbsStruct(refty), index, opnd) --> resultID
378 379 380 381 382 383 384
      case NodeGetElemIRef(id, resultID, isPtr, refty, indty, opnd, index) =>
        InstGetElemIRef(isPtr, needSeq(refty), needInt(indty), opnd, index) --> resultID
      case NodeShiftIRef(id, resultID, isPtr, refty, offty, opnd, offset) =>
        InstShiftIRef(isPtr, refty, needInt(offty), opnd, offset) --> resultID
      case NodeGetVarPartIRef(id, resultID, isPtr, refty, opnd) =>
        InstGetVarPartIRef(isPtr, needHybrid(refty), opnd) --> resultID
      case NodeLoad(id, resultID, isPtr, ord, refty, loc, excClause) =>
385
        InstLoad(isPtr, ord, refty, loc, null) --> resultID EXC excClause
386
      case NodeStore(id, isPtr, ord, refty, loc, newval, excClause) =>
387
        InstStore(isPtr, ord, refty, loc, newval, null) EXC excClause
388
      case NodeCmpXchg(id, valueResultID, succResultID, isPtr, isWeak, ordSucc, ordFail, refty, loc, expected, desired, excClause) =>
389
        InstCmpXchg(isPtr, isWeak, ordSucc, ordFail, refty, loc, expected, desired, null) --> (valueResultID, succResultID) EXC excClause
390
      case NodeAtomicRMW(id, resultID, isPtr, ord, optr, refTy, loc, opnd, excClause) =>
391
        InstAtomicRMW(isPtr, ord, optr, refTy, loc, opnd, null) --> resultID EXC excClause
392 393 394
      case NodeFence(id, ord) =>
        InstFence(ord)
      case NodeTrap(id, resultIDs, rettys, excClause, keepaliveClause) =>
395
        InstTrap(rettys, null, keepaliveClause) --> resultIDs EXC excClause
396 397 398 399 400
      case NodeWatchPoint(id, wpid, resultIDs, rettys, dis, ena, exc, keepaliveClause) =>
        InstWatchPoint(wpid, rettys, dis, ena, exc.map(resDestClause), keepaliveClause) --> resultIDs
      case NodeWPBranch(id, wpid, dis, ena) =>
        InstWPBranch(wpid, dis, ena)
      case NodeCCall(id, resultIDs, callconv, calleeTy, sig, callee, args, excClause, keepaliveClause) =>
401
        InstCCall(callconv, calleeTy, sig, callee, args, null, keepaliveClause) --> resultIDs EXC excClause
402
      case NodeNewThread(id, resultID, stack, threadlocal, newStackClause, excClause) =>
403
        InstNewThread(stack, threadlocal.map(resVar), newStackClause, null) --> resultID EXC excClause
404
      case NodeSwapStack(id, resultIDs, swappee, curStackClause, newStackClause, excClause, keepaliveClause) =>
405
        InstSwapStack(swappee, curStackClause, newStackClause, null, keepaliveClause) --> resultIDs EXC excClause
406
      case NodeCommInst(id, resultIDs, opcode, flags, tys, sigs, args, excClause, keepaliveClause) =>
407
        InstCommInst(opcode, flags, tys, sigs.map(resSig), args, null, keepaliveClause) --> resultIDs EXC excClause
408
    }
Kunshan Wang's avatar
Kunshan Wang committed
409
    muInst.bb = muBB
410 411 412 413
    muInst
  } catch {
    case e: Exception => {
      throw new IRBuilderException("Exception thrown when building instruction. Func: %s, ver: %s, bb: %s, inst: %d:%s".format(
Kunshan Wang's avatar
Kunshan Wang committed
414
        muFuncVer.func.repr, muFuncVer.repr, muBB.repr, irInst.id, idNameMap.getOrElse(irInst.id, "(no name)")), e)
415 416 417 418 419 420 421 422
    }
  }
}

private object BundleConstructorImplicitMagics {
  implicit class AddResultsSeq[T <: Instruction](val inst: T) extends AnyVal {
    @inline
    def -->(resultIDs: Seq[MuID])(implicit self: BundleConstructor, localVarNs: Namespace[LocalVariable]): T = {
Kunshan Wang's avatar
Kunshan Wang committed
423
      inst --> (resultIDs: _*)
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
    }
  }
  implicit class AddResultsVarArg[T <: Instruction](val inst: T) extends AnyVal {
    @inline
    def -->(resultIDs: MuID*)(implicit self: BundleConstructor, localVarNs: Namespace[LocalVariable]): T = {
      val instResults = for ((resultID, i) <- resultIDs.zipWithIndex) yield {
        val instRes = InstResult(inst, i)
        self.setIDAndMaybeName(instRes, resultID)
        localVarNs.add(instRes)
        instRes
      }
      inst.results = instResults.toIndexedSeq
      inst
    }
  }
439 440 441 442 443 444 445
  implicit class AddExcClause[T <: HasExcClause](val inst: T) extends AnyVal {
    @inline
    def EXC(excClause: Option[MuID])(implicit resExcClause: Option[MuID] => Option[ExcClause]): T = {
      inst.excClause = resExcClause(excClause)
      inst
    }
  }
446
}