GitLab will be upgraded to the 12.10.14-ce.0 on 28 Sept 2020 at 2.00pm (AEDT) to 2.30pm (AEDT). During the update, GitLab and Mattermost services will not be available. If you have any concerns with this, please talk to us at N110 (b) CSIT building.

CommInstExecutor.scala 12 KB
Newer Older
Kunshan Wang's avatar
Kunshan Wang committed
1
package uvm.refimpl.itpr
2

Kunshan Wang's avatar
Kunshan Wang committed
3 4 5 6 7 8
import uvm._
import uvm.comminsts._
import uvm.refimpl._
import uvm.refimpl.mem._
import uvm.ssavariables._
import uvm.types._
9

Kunshan Wang's avatar
Kunshan Wang committed
10 11 12
/**
 * A part of the InterpreterThread that interprets common instructions
 */
Kunshan Wang's avatar
Kunshan Wang committed
13
trait CommInstExecutor extends InterpreterActions with ObjectPinner with IRBuilderCommInstExecutor {
Kunshan Wang's avatar
Kunshan Wang committed
14 15
  import InterpreterThread.logger

16
  implicit protected def mutator: Mutator
Kunshan Wang's avatar
Kunshan Wang committed
17 18 19 20
  implicit protected def memorySupport: MemorySupport

  override def interpretCurrentCommonInstruction(): Unit = {
    assert(curInst.isInstanceOf[InstCommInst])
Kunshan Wang's avatar
Kunshan Wang committed
21
    val InstCommInst(ci, flagList, typeList, sigList, argList, excClause, keepalives) = curInst
Kunshan Wang's avatar
Kunshan Wang committed
22 23 24

    microVM.stats.onInstExec("InstCommInst/" + ci.name.get)

Kunshan Wang's avatar
Kunshan Wang committed
25 26 27
    if (ci.name.get.startsWith("@uvm.irbuilder")) {
      return interpretCurrentIRBuilderCommonInstruction()
    }
Kunshan Wang's avatar
Kunshan Wang committed
28 29 30 31 32 33

    ci.name.get match {
      // Thread and stack operations
      case "@uvm.new_stack" => {
        val Seq(sig) = sigList
        val Seq(func) = argList
34
        val funcVal = func.asFunc.getOrElse {
35
          throw new UvmNullGenRefException(ctx + "Attempt to create new thread for NULL Mu function.")
Kunshan Wang's avatar
Kunshan Wang committed
36 37 38
        }

        val sta = microVM.threadStackManager.newStack(funcVal, mutator)
39
        resultBox(0).asStack = Some(sta)
Kunshan Wang's avatar
Kunshan Wang committed
40 41 42 43 44
        continueNormally()
      }

      case "@uvm.kill_stack" => {
        val Seq(s) = argList
45
        val sta = s.asStack.getOrElse {
46
          throw new UvmNullGenRefException(ctx + "Attempt to kill NULL stack.")
Kunshan Wang's avatar
Kunshan Wang committed
47 48 49 50 51 52 53 54 55 56
        }
        sta.kill()
        continueNormally()
      }

      case "@uvm.thread_exit" => {
        threadExit()
      }

      case "@uvm.current_stack" => {
57
        results(0).asStack = Some(curStack)
Kunshan Wang's avatar
Kunshan Wang committed
58 59 60
        continueNormally()
      }

61 62 63 64 65 66 67 68 69 70 71
      case "@uvm.set_threadlocal" => {
        val Seq(tl) = argList
        threadLocal copyFrom tl
        continueNormally()
      }

      case "@uvm.get_threadlocal" => {
        results(0) copyFrom threadLocal
        continueNormally()
      }

Kunshan Wang's avatar
Kunshan Wang committed
72 73 74 75
      // 64-bit Tagged Reference

      case "@uvm.tr64.is_fp" => {
        val Seq(tr) = argList
Kunshan Wang's avatar
Kunshan Wang committed
76
        results(0).asBoolean = OpHelper.tr64IsFP(tr.asTR64Raw)
Kunshan Wang's avatar
Kunshan Wang committed
77 78 79 80 81
        continueNormally()
      }

      case "@uvm.tr64.is_int" => {
        val Seq(tr) = argList
82
        results(0).asBoolean = OpHelper.tr64IsInt(tr.asTR64Raw)
Kunshan Wang's avatar
Kunshan Wang committed
83 84 85 86 87
        continueNormally()
      }

      case "@uvm.tr64.is_ref" => {
        val Seq(tr) = argList
88
        results(0).asBoolean = OpHelper.tr64IsRef(tr.asTR64Raw)
Kunshan Wang's avatar
Kunshan Wang committed
89 90 91 92 93
        continueNormally()
      }

      case "@uvm.tr64.from_fp" => {
        val Seq(v) = argList
94
        results(0).asTR64Raw = OpHelper.fpToTr64(v.asDouble)
Kunshan Wang's avatar
Kunshan Wang committed
95 96 97 98 99
        continueNormally()
      }

      case "@uvm.tr64.from_int" => {
        val Seq(v) = argList
100
        results(0).asTR64Raw = OpHelper.intToTr64(v.getUInt(52).longValue)
Kunshan Wang's avatar
Kunshan Wang committed
101 102 103 104 105
        continueNormally()
      }

      case "@uvm.tr64.from_ref" => {
        val Seq(ref, tag) = argList
106 107 108
        val vRef = ref.asRef
        val vTag = tag.getSInt(6).longValue
        resultBox(0).asTR64Raw = OpHelper.refToTr64(vRef, vTag)
Kunshan Wang's avatar
Kunshan Wang committed
109 110 111 112 113
        continueNormally()
      }

      case "@uvm.tr64.to_fp" => {
        val Seq(tr) = argList
114
        val raw = tr.asTR64Raw
Kunshan Wang's avatar
Kunshan Wang committed
115 116
        if (OpHelper.tr64IsFP(raw)) {
          val result = OpHelper.tr64ToFP(raw)
117
          results(0).asDouble = result
Kunshan Wang's avatar
Kunshan Wang committed
118 119 120 121 122 123 124 125
          continueNormally()
        } else {
          throw new UvmRuntimeException(ctx + "Attempt to extract double from a tagref64 which is not holding a double")
        }
      }

      case "@uvm.tr64.to_int" => {
        val Seq(tr) = argList
126
        val raw = tr.asTR64Raw
Kunshan Wang's avatar
Kunshan Wang committed
127 128
        if (OpHelper.tr64IsInt(raw)) {
          val result = OpHelper.tr64ToInt(raw)
129
          results(0).setInt(result, 52)
Kunshan Wang's avatar
Kunshan Wang committed
130 131 132 133 134 135 136 137
          continueNormally()
        } else {
          throw new UvmRuntimeException(ctx + "Attempt to extract int from a tagref64 which is not holding a int")
        }
      }

      case "@uvm.tr64.to_ref" => {
        val Seq(tr) = argList
138
        val raw = tr.asTR64Raw
Kunshan Wang's avatar
Kunshan Wang committed
139 140
        if (OpHelper.tr64IsRef(raw)) {
          val result = OpHelper.tr64ToRef(raw)
141
          results(0).asRef = result
Kunshan Wang's avatar
Kunshan Wang committed
142 143 144 145 146 147 148 149
          continueNormally()
        } else {
          throw new UvmRuntimeException(ctx + "Attempt to extract ref from a tagref64 which is not holding a ref")
        }
      }

      case "@uvm.tr64.to_tag" => {
        val Seq(tr) = argList
150
        val raw = tr.asTR64Raw
Kunshan Wang's avatar
Kunshan Wang committed
151 152
        if (OpHelper.tr64IsRef(raw)) {
          val result = OpHelper.tr64ToTag(raw)
153
          results(0).setInt(result, 6)
Kunshan Wang's avatar
Kunshan Wang committed
154 155 156 157 158 159 160
          continueNormally()
        } else {
          throw new UvmRuntimeException(ctx + "Attempt to extract tag from a tagref64 which is not holding a ref")
        }
      }

      case "@uvm.futex.wait" => {
161
        val Seq(ty: TypeInt) = typeList
Kunshan Wang's avatar
Kunshan Wang committed
162 163
        val Seq(loc, v) = argList

164 165
        val len = ty.length
        val (objRef, offset) = loc.asIRef
Kunshan Wang's avatar
Kunshan Wang committed
166
        val locWord = objRef + offset
167
        val exp = v.asIntRaw
Kunshan Wang's avatar
Kunshan Wang committed
168

169
        val equal = MemoryOperations.cmpInt(len, locWord, exp)
Kunshan Wang's avatar
Kunshan Wang committed
170 171 172 173 174 175 176 177 178 179 180

        if (equal) {
          microVM.threadStackManager.futexManager.futexWaitNoCheck(objRef, offset, curThread, None)
          logger.debug(ctx + "Waiting in the futex waiting queue.")
        } else {
          logger.debug(ctx + "Memory location does not contain expected value. Don't wait.")
          futexReturn(-1)
        }
      }

      case "@uvm.futex.wait_timeout" => {
181
        val Seq(ty: TypeInt) = typeList
Kunshan Wang's avatar
Kunshan Wang committed
182 183
        val Seq(loc, v, timeout) = argList

184 185
        val len = ty.length
        val (objRef, offset) = loc.asIRef
Kunshan Wang's avatar
Kunshan Wang committed
186
        val locWord = objRef + offset
187 188
        val toVal = timeout.asSInt64.longValue
        val exp = v.asIntRaw
Kunshan Wang's avatar
Kunshan Wang committed
189 190 191

        if (toVal < 0L) throw new UvmRefImplException(ctx + "This refimpl treats timeout as signed due to restriction of Java.")

192
        val equal = MemoryOperations.cmpInt(len, locWord, exp)
Kunshan Wang's avatar
Kunshan Wang committed
193 194 195 196 197 198 199 200 201 202 203

        if (equal) {
          microVM.threadStackManager.futexManager.futexWaitNoCheck(objRef, offset, curThread, Some(toVal))
          logger.debug(ctx + "Waiting in the futex waiting queue.")
        } else {
          logger.debug(ctx + "Memory location does not contain expected value. Don't wait.")
          futexReturn(-1)
        }
      }

      case "@uvm.futex.wake" => {
204
        val Seq(ty: TypeInt) = typeList
Kunshan Wang's avatar
Kunshan Wang committed
205 206
        val Seq(loc, nthread) = argList

207 208
        val len = ty.length
        val (objRef, offset) = loc.asIRef
Kunshan Wang's avatar
Kunshan Wang committed
209
        val locWord = objRef + offset
210
        val nth = nthread.asSInt32.intValue
Kunshan Wang's avatar
Kunshan Wang committed
211 212 213 214 215 216 217 218 219 220 221 222

        if (nth < 0) throw new UvmRuntimeException(ctx + "nthread must not be negative")

        val nWoken = microVM.threadStackManager.futexManager.futexWake(objRef, offset, nth)
        futexReturn(nWoken)
      }

      case "@uvm.futex.cmp_requeue" => {
        val Seq(ty) = typeList
        val Seq(locSrc, locDst, expected, nthread) = argList

        val len = ty.asInstanceOf[TypeInt].length
223 224 225 226
        val (objRefSrc, offsetSrc) = locSrc.asIRef
        val (objRefDst, offsetDst) = locDst.asIRef
        val exp = expected.asIntRaw
        val nth = nthread.asSInt32.intValue
Kunshan Wang's avatar
Kunshan Wang committed
227 228 229

        if (nth < 0) throw new UvmRuntimeException(ctx + "nthread must not be negative")

230
        val equal = MemoryOperations.cmpInt(len, objRefSrc + offsetSrc, exp)
Kunshan Wang's avatar
Kunshan Wang committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251

        if (equal) {
          val nWoken = microVM.threadStackManager.futexManager.futexRequeue(objRefSrc, offsetSrc, objRefDst, offsetDst, nth)
          futexReturn(nWoken)
        } else {
          futexReturn(-1)
        }
      }

      case "@uvm.kill_dependency" => {
        val Seq(v) = argList
        val vBox = boxOf(v)
        resultBox(0).copyFrom(vBox)
        continueNormally()
      }

      case "@uvm.native.pin" => {
        val Seq(ty) = typeList
        val Seq(r) = argList

        val (addr, offset) = ty match {
252 253
          case TypeRef(_)  => (r.asRef, 0L)
          case TypeIRef(_) => r.asIRef
Kunshan Wang's avatar
Kunshan Wang committed
254 255 256 257 258 259 260 261 262 263 264 265 266
        }

        pin(addr)

        resultBox(0).asInstanceOf[BoxPointer].addr = addr + offset
        continueNormally()
      }

      case "@uvm.native.unpin" => {
        val Seq(ty) = typeList
        val Seq(r) = argList

        val addr = ty match {
267 268
          case TypeRef(_)  => r.asRef
          case TypeIRef(_) => r.asIRef._1
Kunshan Wang's avatar
Kunshan Wang committed
269 270 271 272 273 274 275 276 277 278 279 280
        }

        unpin(addr)

        continueNormally()
      }

      case "@uvm.native.expose" => {
        val Seq(callConv) = flagList
        val Seq(sig) = sigList
        val Seq(func, cookie) = argList

281
        val f = func.asFunc.getOrElse {
282
          throw new UvmNullGenRefException(ctx + "Attempt to expose NULL Mu function")
Kunshan Wang's avatar
Kunshan Wang committed
283 284
        }

285
        val c = boxOf(cookie).asSInt64.longValue
Kunshan Wang's avatar
Kunshan Wang committed
286 287 288 289 290 291 292 293 294 295 296 297

        val addr = microVM.nativeCallHelper.exposeFuncDynamic(f, c)

        resultBox(0).asInstanceOf[BoxPointer].addr = addr

        continueNormally()
      }

      case "@uvm.native.unexpose" => {
        val Seq(callConv) = flagList
        val Seq(addr) = argList

298
        val a = addr.asPtr
Kunshan Wang's avatar
Kunshan Wang committed
299 300 301 302 303 304 305 306

        microVM.nativeCallHelper.unexposeFunc(a)

        continueNormally()
      }

      case "@uvm.native.get_cookie" => {
        val cookie = topDefMu.cookie
307
        results(0).asInt64 = cookie
Kunshan Wang's avatar
Kunshan Wang committed
308 309 310
        continueNormally()
      }

311 312
      case "@uvm.meta.id_of" => {
        val Seq(name) = argList
313
        val nameStr = MemoryOperations.bytesToStr(name.asRef)
314 315
        val theID = microVM.idOf(nameStr)

316
        results(0).asInt32 = theID
317 318 319 320 321 322

        continueNormally()
      }

      case "@uvm.meta.name_of" => {
        val Seq(theID) = argList
323
        val idInt = theID.asSInt32.toInt
324 325 326
        val name = microVM.nameOf(idInt)
        val bytesRef = MemoryOperations.strToBytes(name)

327
        results(0).asRef = bytesRef
328 329 330 331 332 333

        continueNormally()
      }

      case "@uvm.meta.load_bundle" => {
        val Seq(bundle) = argList
334
        val bundleStr = MemoryOperations.bytesToStr(bundle.asRef)
335 336 337 338 339

        MetaOperations.loadBundle(bundleStr)

        continueNormally()
      }
340

341 342
      case "@uvm.meta.load_hail" => {
        val Seq(hailScript) = argList
343
        val hailStr = MemoryOperations.bytesToStr(hailScript.asRef)
344 345 346 347 348 349

        MetaOperations.loadHail(hailStr)

        continueNormally()
      }

350 351 352 353 354
      case "@uvm.meta.cur_func" => {
        val Seq(stack) = argList
        ???
      }

355 356 357
      case "@uvm.meta.cur_func_ver"       => ???
      case "@uvm.meta.cur_inst"           => ???
      case "@uvm.meta.dump_keepalives"    => ???
358

359 360
      case "@uvm.meta.pop_frame"          => ???
      case "@uvm.meta.push_frame"         => ???
361

Kunshan Wang's avatar
Kunshan Wang committed
362 363
      case "@uvm.meta.enable_watchPoint"  => ???
      case "@uvm.meta.disable_watchPoint" => ???
364

365
      case "@uvm.meta.set_trap_handler"   => ???
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 395 396 397 398 399 400 401 402 403 404 405

      case "@uvm.meta.constant_by_id" => {
        val Seq(idBox) = argList
        val id = idBox.asInt32.toInt
        val gvBox = globalVarBoxByID(id)
        
        results(0).copyFrom(gvBox)
        
        continueNormally()
      }

      case "@uvm.meta.global_by_id" => {
        val Seq(idBox) = argList
        val id = idBox.asInt32.toInt
        val gvBox = globalVarBoxByID(id)
        
        results(0).copyFrom(gvBox)
        
        continueNormally()
      }

      case "@uvm.meta.func_by_id" => {
        val Seq(idBox) = argList
        val id = idBox.asInt32.toInt
        val gvBox = globalVarBoxByID(id)
        
        results(0).copyFrom(gvBox)
        
        continueNormally()
      }

      case "@uvm.meta.expfunc_by_id" => {
        val Seq(idBox) = argList
        val id = idBox.asInt32.toInt
        val gvBox = globalVarBoxByID(id)
        
        results(0).copyFrom(gvBox)
        
        continueNormally()
      }
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
      
      case "@uvm.ext.print_stats" => {
        val Seq(vUserNum) = argList
        
        val userNum = vUserNum.asSInt64.toLong
        microVM.stats.printStats(userNum)
        
        continueNormally()
      }
      
      case "@uvm.ext.clear_stats" => {
        microVM.stats.clearStats()

        continueNormally()
      }
421

Kunshan Wang's avatar
Kunshan Wang committed
422 423 424 425 426 427 428 429 430
      // Insert more CommInsts here.

      case ciName => {
        throw new UvmRefImplException("Unimplemented common instruction %s".format(ciName))
      }

    }

  }
431 432 433 434 435 436
  
  private def globalVarBoxByID(id: MuID): ValueBox = {
    val gv = microVM.globalBundle.globalVarNs(id)
    val gvBox = microVM.constantPool.getGlobalVarBox(gv)
    gvBox
  }
Kunshan Wang's avatar
Kunshan Wang committed
437
}