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

3 4
import java.nio.charset.Charset

Kunshan Wang's avatar
Kunshan Wang committed
5
import uvm.refimpl._
6
import uvm.refimpl.mem.MemorySupport
7 8 9 10 11 12
import uvm.refimpl.mem.Mutator
import uvm.refimpl.mem.TypeSizes._
import uvm.refimpl.mem.TypeSizes
import uvm.ssavariables._
import uvm.ssavariables.AtomicRMWOptr._
import uvm.types._
13
import uvm.IRNode
Kunshan Wang's avatar
Kunshan Wang committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

object OpHelper {

  val ONE = BigInt(1)

  def mask(n: Int): BigInt = {
    (ONE << n) - ONE
  }

  def truncFromBigInt(n: BigInt, len: Int): BigInt = n & mask(len)

  def zextToBigInt(n: BigInt, len: Int): BigInt = n & mask(len)

  def sextToBigInt(n: BigInt, len: Int): BigInt = {
    val bit = n.testBit(len - 1)
    if (bit) {
      n | (~mask(len - 1))
    } else {
      n & (mask(len - 1))
    }
  }

36 37 38 39 40
  // The BigInt in a BoxInt is always truncated to len bits.
  //
  // "prepare" means sign- or zero-extend the content to get the real math value.
  // "unprepare" means truncating a math value to len bits to be stored in a BoxInt.

Kunshan Wang's avatar
Kunshan Wang committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  def prepareUnsigned(n: BigInt, len: Int): BigInt = truncFromBigInt(n, len)

  def prepareSigned(n: BigInt, len: Int): BigInt = {
    sextToBigInt(truncFromBigInt(n, len), len)
  }

  def unprepare(n: BigInt, len: Int): BigInt = truncFromBigInt(n, len)

  def trunc(n: BigInt, toLen: Int): BigInt = truncFromBigInt(n, toLen)

  def zext(n: BigInt, fromLen: Int, toLen: Int): BigInt = truncFromBigInt(n, fromLen)

  def sext(n: BigInt, fromLen: Int, toLen: Int): BigInt = {
    truncFromBigInt(sextToBigInt(n, fromLen), toLen)
  }

  def maxSInt(l: Int): BigInt = (BigInt(1) << (l - 1)) - 1
  def minSIntAbs(l: Int): BigInt = BigInt(1) << (l - 1)
  def maxUInt(l: Int): BigInt = (BigInt(1) << l) - 1

  /**
   * Convert a float to an integer. The result only has the lowest iLen binary digits.
   */
  def floatToI(n: Float, iLen: Int, signed: Boolean): BigInt = {
    val lExp = Math.getExponent(n)
    val rExp = lExp - 23
    val frac = (java.lang.Float.floatToRawIntBits(n) & 0x7fffff) | 0x800000;

    if (java.lang.Float.isNaN(n)) 0
    else if (signed) {
      if (java.lang.Float.isInfinite(n)) { if (n > 0.0F) maxSInt(iLen) else minSIntAbs(iLen) }
      else {
        if (lExp >= (iLen - 1)) if (n > 0.0F) maxSInt(iLen) else minSIntAbs(iLen)
        else if (lExp < 0) 0
        else {
          val abs = BigInt(frac) << rExp
          unprepare(if (n < 0.0F) -abs else abs, iLen)
        }
      }
    } else {
      if (n < 0.0F) 0
      else if (java.lang.Float.isInfinite(n)) maxUInt(iLen)
      else {
        if (lExp >= iLen) maxUInt(iLen)
        else if (lExp < 0) 0
        else unprepare(BigInt(frac) << rExp, iLen)
      }
    }
  }

  /**
   * Convert a float to an integer. The result only has the lowest iLen binary digits.
   */
  def doubleToI(n: Double, iLen: Int, signed: Boolean): BigInt = {
    val lExp = Math.getExponent(n)
    val rExp = lExp - 52
    val frac = (java.lang.Double.doubleToRawLongBits(n) & 0xfffffffffffffL) | 0x10000000000000L;
98

Kunshan Wang's avatar
Kunshan Wang committed
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
    if (java.lang.Double.isNaN(n)) 0
    else if (signed) {
      if (java.lang.Double.isInfinite(n)) { if (n > 0.0D) maxSInt(iLen) else minSIntAbs(iLen) }
      else {
        if (lExp >= (iLen - 1)) if (n > 0.0D) maxSInt(iLen) else minSIntAbs(iLen)
        else if (lExp < 0) 0
        else {
          val abs = BigInt(frac) << rExp
          unprepare(if (n < 0.0D) -abs else abs, iLen)
        }
      }
    } else {
      if (n < 0.0D) 0
      else if (java.lang.Double.isInfinite(n)) maxUInt(iLen)
      else {
        if (lExp >= iLen) maxUInt(iLen)
        else if (lExp < 0) 0
        else unprepare(BigInt(frac) << rExp, iLen)
      }
    }
  }

  def tr64IsInt(opnd: Long): Boolean = {
    (opnd & 0x7ff0000000000001L) == 0x7ff0000000000001L
  }

125
  def tr64IsFP(opnd: Long): Boolean = {
Kunshan Wang's avatar
Kunshan Wang committed
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
    (opnd & 0x7ff0000000000001L) != 0x7ff0000000000001L &&
      (opnd & 0x7ff0000000000003L) != 0x7ff0000000000002L
  }

  def tr64IsRef(opnd: Long): Boolean = {
    (opnd & 0x7ff0000000000003L) == 0x7ff0000000000002L
  }

  def intToTr64(opnd: Long): Long = {
    (0x7ff0000000000001L | ((opnd & 0x7ffffffffffffL) << 1) |
      ((opnd & 0x8000000000000L) << 12))
  }

  def fpToTr64(opnd: Double): Long = {
    var bits = java.lang.Double.doubleToRawLongBits(opnd)
    if (java.lang.Double.isNaN(opnd)) {
      bits = bits & 0xfff8000000000000L | 0x0000000000000008L
    }
    bits
  }

  def refToTr64(opnd: Long, tag: Long): Long = {
    (0x7ff0000000000002L | (opnd & 0x7ffffffffff8L) | ((opnd & 0x800000000000L) << 16) |
      ((tag & 0x3eL) << 46) |
      ((tag & 0x1) << 2))
  }

  def tr64ToInt(opnd: Long): Long = {
    (((opnd & 0xffffffffffffeL) >> 1) | ((opnd & 0x8000000000000000L) >> 12) & (1L << 51))
  }

157
  def tr64ToFP(opnd: Long): Double = java.lang.Double.longBitsToDouble(opnd)
Kunshan Wang's avatar
Kunshan Wang committed
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

  def tr64ToRef(opnd: Long): Long = {
    ((opnd & 0x7ffffffffff8L) |
      (((~(((opnd & 0x8000000000000000L) << 1) - 1)) >> 17) &
        0xffff800000000000L))
  }

  def tr64ToTag(opnd: Long): Long = {
    (((opnd & 0x000f800000000000L) >> 46) | ((opnd & 0x4) >> 2))
  }
}

object PrimOpHelpers {
  @throws(classOf[UvmDivisionByZeroException])
  def intBinOp(op: BinOptr.BinOptr, l: Int, op1v: BigInt, op2v: BigInt, ctx: => String): BigInt = {
    def pu(v: BigInt): BigInt = OpHelper.prepareUnsigned(v, l)
    def ps(v: BigInt): BigInt = OpHelper.prepareSigned(v, l)
    def up(v: BigInt): BigInt = OpHelper.unprepare(v, l)
    def shiftMask = {
      var i = 1
      while (i < l) { i <<= 1 }
      i - 1
    }
    def checkDivByZero(): Unit = {
      if (op2v == 0) throw new UvmDivisionByZeroException(ctx + "Division by zero.")
    }

    up(op match {
186 187 188
      case BinOptr.ADD  => pu(op1v) + pu(op2v)
      case BinOptr.SUB  => pu(op1v) - pu(op2v)
      case BinOptr.MUL  => pu(op1v) * pu(op2v)
Kunshan Wang's avatar
Kunshan Wang committed
189 190 191 192
      case BinOptr.UDIV => { checkDivByZero(); pu(op1v) / pu(op2v) }
      case BinOptr.SDIV => { checkDivByZero(); ps(op1v) / ps(op2v) }
      case BinOptr.UREM => { checkDivByZero(); pu(op1v) % pu(op2v) }
      case BinOptr.SREM => { checkDivByZero(); ps(op1v) % ps(op2v) }
193
      case BinOptr.SHL  => pu(op1v) << (op2v.intValue & shiftMask)
Kunshan Wang's avatar
Kunshan Wang committed
194 195
      case BinOptr.LSHR => pu(op1v) >> (op2v.intValue & shiftMask)
      case BinOptr.ASHR => ps(op1v) >> (op2v.intValue & shiftMask)
196 197 198 199
      case BinOptr.AND  => pu(op1v) & pu(op2v)
      case BinOptr.OR   => pu(op1v) | pu(op2v)
      case BinOptr.XOR  => pu(op1v) ^ pu(op2v)
      case _            => throw new UvmRuntimeException(ctx + "Binary operator %s is not suitable for integer.".format(op))
Kunshan Wang's avatar
Kunshan Wang committed
200 201 202 203 204 205 206 207 208 209
    })
  }

  def floatBinOp(op: BinOptr.BinOptr, op1v: Float, op2v: Float, ctx: => String): Float = {
    op match {
      case BinOptr.FADD => op1v + op2v
      case BinOptr.FSUB => op1v - op2v
      case BinOptr.FMUL => op1v * op2v
      case BinOptr.FDIV => op1v / op2v
      case BinOptr.FREM => Math.IEEEremainder(op1v, op2v).toFloat
210
      case _            => throw new UvmRuntimeException(ctx + "Binary operator %s is not suitable for float.".format(op))
Kunshan Wang's avatar
Kunshan Wang committed
211 212 213 214 215 216 217 218 219 220
    }
  }

  def doubleBinOp(op: BinOptr.BinOptr, op1v: Double, op2v: Double, ctx: => String): Double = {
    op match {
      case BinOptr.FADD => op1v + op2v
      case BinOptr.FSUB => op1v - op2v
      case BinOptr.FMUL => op1v * op2v
      case BinOptr.FDIV => op1v / op2v
      case BinOptr.FREM => Math.IEEEremainder(op1v, op2v)
221
      case _            => throw new UvmRuntimeException(ctx + "Binary operator %s is not suitable for double.".format(op))
Kunshan Wang's avatar
Kunshan Wang committed
222 223 224 225 226 227 228 229
    }
  }

  def intCmp(op: CmpOptr.CmpOptr, l: Int, op1v: BigInt, op2v: BigInt, ctx: => String): Boolean = {
    def pu(v: BigInt): BigInt = OpHelper.prepareUnsigned(v, l)
    def ps(v: BigInt): BigInt = OpHelper.prepareSigned(v, l)

    op match {
230 231
      case CmpOptr.EQ  => pu(op1v) == pu(op2v)
      case CmpOptr.NE  => pu(op1v) != pu(op2v)
Kunshan Wang's avatar
Kunshan Wang committed
232 233 234 235 236 237 238 239
      case CmpOptr.UGT => pu(op1v) > pu(op2v)
      case CmpOptr.UGE => pu(op1v) >= pu(op2v)
      case CmpOptr.ULT => pu(op1v) < pu(op2v)
      case CmpOptr.ULE => pu(op1v) <= pu(op2v)
      case CmpOptr.SGT => ps(op1v) > ps(op2v)
      case CmpOptr.SGE => ps(op1v) >= ps(op2v)
      case CmpOptr.SLT => ps(op1v) < ps(op2v)
      case CmpOptr.SLE => ps(op1v) <= ps(op2v)
240
      case _           => throw new UvmRuntimeException(ctx + "Comparison operator %s not suitable for integers".format(op))
Kunshan Wang's avatar
Kunshan Wang committed
241 242 243 244 245 246 247 248
    }
  }

  def floatCmp(op: CmpOptr.CmpOptr, op1v: Float, op2v: Float, ctx: => String): Boolean = {
    import java.lang.Float.isNaN
    def ord = !isNaN(op1v) && !isNaN(op2v)
    def uno = isNaN(op1v) || isNaN(op2v)
    op match {
249
      case CmpOptr.FTRUE  => true
Kunshan Wang's avatar
Kunshan Wang committed
250
      case CmpOptr.FFALSE => false
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
      case CmpOptr.FOEQ   => ord && op1v == op2v
      case CmpOptr.FONE   => ord && op1v != op2v
      case CmpOptr.FOGT   => ord && op1v > op2v
      case CmpOptr.FOGE   => ord && op1v >= op2v
      case CmpOptr.FOLT   => ord && op1v < op2v
      case CmpOptr.FOLE   => ord && op1v <= op2v
      case CmpOptr.FORD   => ord
      case CmpOptr.FUEQ   => uno || op1v == op2v
      case CmpOptr.FUNE   => uno || op1v != op2v
      case CmpOptr.FUGT   => uno || op1v > op2v
      case CmpOptr.FUGE   => uno || op1v >= op2v
      case CmpOptr.FULT   => uno || op1v < op2v
      case CmpOptr.FULE   => uno || op1v <= op2v
      case CmpOptr.FUNO   => uno
      case _              => throw new UvmRuntimeException(ctx + "Comparison operator %s is not suitable for float.".format(op))
Kunshan Wang's avatar
Kunshan Wang committed
266 267 268 269 270 271 272 273
    }
  }

  def doubleCmp(op: CmpOptr.CmpOptr, op1v: Double, op2v: Double, ctx: => String): Boolean = {
    import java.lang.Double.isNaN
    def ord = !isNaN(op1v) && !isNaN(op2v)
    def uno = isNaN(op1v) || isNaN(op2v)
    op match {
274
      case CmpOptr.FTRUE  => true
Kunshan Wang's avatar
Kunshan Wang committed
275
      case CmpOptr.FFALSE => false
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
      case CmpOptr.FOEQ   => ord && op1v == op2v
      case CmpOptr.FONE   => ord && op1v != op2v
      case CmpOptr.FOGT   => ord && op1v > op2v
      case CmpOptr.FOGE   => ord && op1v >= op2v
      case CmpOptr.FOLT   => ord && op1v < op2v
      case CmpOptr.FOLE   => ord && op1v <= op2v
      case CmpOptr.FORD   => ord
      case CmpOptr.FUEQ   => uno || op1v == op2v
      case CmpOptr.FUNE   => uno || op1v != op2v
      case CmpOptr.FUGT   => uno || op1v > op2v
      case CmpOptr.FUGE   => uno || op1v >= op2v
      case CmpOptr.FULT   => uno || op1v < op2v
      case CmpOptr.FULE   => uno || op1v <= op2v
      case CmpOptr.FUNO   => uno
      case _              => throw new UvmRuntimeException(ctx + "Comparison operator %s is not suitable for double.".format(op))
Kunshan Wang's avatar
Kunshan Wang committed
291 292
    }
  }
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327

  def refCmp(op: CmpOptr.CmpOptr, op1v: Word, op2v: Word, ctx: => String): Boolean = {
    op match {
      case CmpOptr.EQ => op1v == op2v
      case CmpOptr.NE => op1v != op2v
      case _          => throw new UvmRuntimeException(ctx + "Comparison operator %s is not suitable for ref.".format(op))
    }
  }

  def irefCmp(op: CmpOptr.CmpOptr, op1b: Word, op1o: Word, op2b: Word, op2o: Word, ctx: => String): Boolean = {
    val a1 = op1b + op1o
    val a2 = op2b + op2o

    def warnDiffObj() = if (op1b != op2b) throw new UvmRuntimeException(
      ctx + "Attempt to compare order of irefs in two different objects. lhs: 0x%x+0x%x rhs: 0x%x+0x%x".format(
        op1b, op1o, op2b, op2o))

    op match {
      case CmpOptr.EQ  => a1 == a2
      case CmpOptr.NE  => a1 != a2
      case CmpOptr.ULT => { warnDiffObj(); a1 < a2 }
      case CmpOptr.ULE => { warnDiffObj(); a1 <= a2 }
      case CmpOptr.UGT => { warnDiffObj(); a1 > a2 }
      case CmpOptr.UGE => { warnDiffObj(); a1 >= a2 }
      case _           => throw new UvmRuntimeException(ctx + "Comparison operator %s is not suitable for iref.".format(op))
    }
  }

  def objCmp[T <: AnyRef](op: CmpOptr.CmpOptr, obj1: T, obj2: T, kind: String, ctx: => String): Boolean = {
    op match {
      case CmpOptr.EQ => obj1 eq obj2
      case CmpOptr.NE => obj1 ne obj2
      case _          => throw new UvmRuntimeException(ctx + "Comparison operator %s is not suitable for %s.".format(op, kind))
    }
  }
Kunshan Wang's avatar
Kunshan Wang committed
328
}
Kunshan Wang's avatar
Kunshan Wang committed
329

330
object MemoryOperations {
331 332 333 334 335 336 337 338 339

  def addressOf(ptr: Boolean, vb: ValueBox): Word = {
    if (ptr) {
      vb.asInstanceOf[BoxPointer].addr
    } else {
      val lb = vb.asInstanceOf[BoxIRef]
      lb.objRef + lb.offset
    }
  }
340

341 342 343 344 345 346 347
  def noAccessViaPointer(ptr: Boolean, ty: Type) {
    if (ptr) {
      throw new UvmIllegalMemoryAccessException("Cannot access type %s via pointer".format(ty.repr))
    }
  }

  def load(ptr: Boolean, ty: Type, loc: Word, br: ValueBox)(implicit microVM: MicroVM, memorySupport: MemorySupport): Unit = {
348 349 350
    def loadScalar(ty: Type, loc: Word, br: ValueBox): Unit = ty match {
      case TypeInt(l) =>
        val bi: BigInt = l match {
351 352 353 354
          case 8  => memorySupport.loadByte(loc, !ptr)
          case 16 => memorySupport.loadShort(loc, !ptr)
          case 32 => memorySupport.loadInt(loc, !ptr)
          case 64 => memorySupport.loadLong(loc, !ptr)
Kunshan Wang's avatar
Kunshan Wang committed
355 356 357 358 359
          case 128 => {
            val lowWord = memorySupport.loadLong(loc, !ptr)
            val highWord = memorySupport.loadLong(loc + 8, !ptr)
            (BigInt(highWord) << 64) + lowWord
          }
360
          case _ => throw new UvmUnimplementedOperationException("Loading int of length %d is not supported".format(l))
361 362 363
        }
        br.asInstanceOf[BoxInt].value = OpHelper.unprepare(bi, l)
      case _: TypeFloat =>
364
        val fv = memorySupport.loadFloat(loc, !ptr)
365 366
        br.asInstanceOf[BoxFloat].value = fv
      case _: TypeDouble =>
367
        val dv = memorySupport.loadDouble(loc, !ptr)
368
        br.asInstanceOf[BoxDouble].value = dv
369 370 371
      case _: AbstractPointerType =>
        val addr = memorySupport.loadLong(loc, !ptr)
        br.asInstanceOf[BoxPointer].addr = addr
372
      case _: TypeRef =>
373
        noAccessViaPointer(ptr, ty)
374
        val addr = memorySupport.loadLong(loc)
375 376
        br.asInstanceOf[BoxRef].objRef = addr
      case _: TypeIRef =>
377
        noAccessViaPointer(ptr, ty)
378 379
        val base = memorySupport.loadLong(loc)
        val offset = memorySupport.loadLong(loc + WORD_SIZE_BYTES)
380
        br.asInstanceOf[BoxIRef].oo = (base, offset)
381 382 383 384
      case _: TypeTagRef64 =>
        noAccessViaPointer(ptr, ty)
        val raw = memorySupport.loadLong(loc)
        br.asInstanceOf[BoxTagRef64].raw = raw
385
      case _: TypeFuncRef =>
386
        noAccessViaPointer(ptr, ty)
387
        val fid = memorySupport.loadLong(loc).toInt
388 389
        val func = microVM.globalBundle.funcNs.get(fid)
        br.asInstanceOf[BoxFunc].func = func
390
      case _: TypeThreadRef =>
391
        noAccessViaPointer(ptr, ty)
392
        val tid = memorySupport.loadLong(loc).toInt
Kunshan Wang's avatar
Kunshan Wang committed
393
        val thr = microVM.threadStackManager.threadRegistry.get(tid)
394
        br.asInstanceOf[BoxThread].thread = thr
395
      case _: TypeStackRef =>
396
        noAccessViaPointer(ptr, ty)
397
        val sid = memorySupport.loadLong(loc).toInt
Kunshan Wang's avatar
Kunshan Wang committed
398
        val sta = microVM.threadStackManager.stackRegistry.get(sid)
399
        br.asInstanceOf[BoxStack].stack = sta
400
      case _: TypeIRNodeRef =>
401
        noAccessViaPointer(ptr, ty)
402 403
        val maybeIRNode = loadIRNode(loc)
        br.asInstanceOf[BoxIRNode].node = maybeIRNode
404
      case _ => throw new UvmUnimplementedOperationException("Loading of type %s is not supporing".format(ty.getClass.getName))
405 406 407 408
    }

    ty match {
      case TypeVector(ety, len) =>
Kunshan Wang's avatar
Kunshan Wang committed
409
        val brs = br.asInstanceOf[BoxSeq].values
410 411 412 413 414 415 416 417
        val elemSkip = alignUp(sizeOf(ety), alignOf(ety))
        for ((brElem, i) <- brs.zipWithIndex) {
          loadScalar(ety, loc + elemSkip * i, brElem)
        }
      case sty => loadScalar(sty, loc, br)
    }
  }

418
  def store(ptr: Boolean, ty: Type, loc: Word, nvb: ValueBox)(implicit microVM: MicroVM, memorySupport: MemorySupport): Unit = {
Kunshan Wang's avatar
Kunshan Wang committed
419
    def storeScalar(ty: Type, loc: Word, nvb: ValueBox): Unit = ty match {
420 421 422
      case TypeInt(l) =>
        val bi = nvb.asInstanceOf[BoxInt].value
        l match {
423 424 425 426
          case 8  => memorySupport.storeByte(loc, bi.byteValue, !ptr)
          case 16 => memorySupport.storeShort(loc, bi.shortValue, !ptr)
          case 32 => memorySupport.storeInt(loc, bi.intValue, !ptr)
          case 64 => memorySupport.storeLong(loc, bi.longValue, !ptr)
Kunshan Wang's avatar
Kunshan Wang committed
427 428 429 430
          case 128 => {
            memorySupport.storeLong(loc, (bi & 0xffffffffffffffffL).longValue, !ptr)
            memorySupport.storeLong(loc + 8, (bi >> 64).longValue, !ptr)
          }
431
          case _ => throw new UvmUnimplementedOperationException("Storing int of length %d is not supported".format(l))
432 433 434
        }
      case _: TypeFloat =>
        val fv = nvb.asInstanceOf[BoxFloat].value
435
        memorySupport.storeFloat(loc, fv, !ptr)
436 437
      case _: TypeDouble =>
        val dv = nvb.asInstanceOf[BoxDouble].value
438
        memorySupport.storeDouble(loc, dv, !ptr)
439 440 441
      case _: AbstractPointerType =>
        val addr = nvb.asInstanceOf[BoxPointer].addr
        memorySupport.storeLong(loc, addr, !ptr)
442
      case _: TypeRef =>
443
        noAccessViaPointer(ptr, ty)
444
        val addr = nvb.asInstanceOf[BoxRef].objRef
445
        memorySupport.storeLong(loc, addr)
446
      case _: TypeIRef =>
447
        noAccessViaPointer(ptr, ty)
448
        val BoxIRef(base, offset) = nvb.asInstanceOf[BoxIRef]
449 450
        memorySupport.storeLong(loc, base)
        memorySupport.storeLong(loc + WORD_SIZE_BYTES, offset)
451 452 453 454
      case _: TypeTagRef64 =>
        noAccessViaPointer(ptr, ty)
        val raw = nvb.asInstanceOf[BoxTagRef64].raw
        memorySupport.storeLong(loc, raw)
455
      case _: TypeFuncRef =>
456
        noAccessViaPointer(ptr, ty)
457
        val fid = nvb.asInstanceOf[BoxFunc].func.map(_.id).getOrElse(0)
458
        memorySupport.storeLong(loc, fid.toLong & 0xFFFFFFFFL)
459
      case _: TypeThreadRef =>
460
        noAccessViaPointer(ptr, ty)
461
        val tid = nvb.asInstanceOf[BoxThread].thread.map(_.id).getOrElse(0)
462
        memorySupport.storeLong(loc, tid.toLong & 0xFFFFFFFFL)
463
      case _: TypeStackRef =>
464
        noAccessViaPointer(ptr, ty)
465
        val sid = nvb.asInstanceOf[BoxStack].stack.map(_.id).getOrElse(0)
466
        memorySupport.storeLong(loc, sid.toLong & 0xFFFFFFFFL)
467
      case _: TypeIRNodeRef =>
468
        noAccessViaPointer(ptr, ty)
469 470
        val maybeIRNode = nvb.asInstanceOf[BoxIRNode].node
        storeIRNode(loc, maybeIRNode)
471
      case _ => throw new UvmUnimplementedOperationException("Storing of type %s is not supporing".format(ty.getClass.getName))
472 473 474 475
    }

    ty match {
      case TypeVector(ety, len) =>
Kunshan Wang's avatar
Kunshan Wang committed
476
        val nvbs = nvb.asInstanceOf[BoxSeq].values
477
        val elemSkip = alignUp(sizeOf(ety), alignOf(ety))
Kunshan Wang's avatar
Kunshan Wang committed
478 479
        for ((nvbElem, i) <- nvbs.zipWithIndex) {
          storeScalar(ety, loc + elemSkip * i, nvbElem)
480
        }
Kunshan Wang's avatar
Kunshan Wang committed
481
      case sty => storeScalar(sty, loc, nvb)
482 483 484 485 486 487
    }
  }

  /**
   * Compare exchange. The result (the old value) is written into br. Return true if successful, false otherwise.
   */
488
  def cmpXchg(ptr: Boolean, ty: Type, loc: Word, eb: ValueBox, db: ValueBox, br: ValueBox)(implicit microVM: MicroVM, memorySupport: MemorySupport): Boolean = {
489 490 491 492 493 494
    ty match {
      case TypeInt(l) =>
        val ebi = eb.asInstanceOf[BoxInt].value
        val dbi = db.asInstanceOf[BoxInt].value
        val (succ, rbi) = l match {
          case 32 => {
495
            val (succ2, rv) = memorySupport.cmpXchgInt(loc, ebi.intValue, dbi.intValue, !ptr)
496 497 498
            (succ2, BigInt(rv))
          }
          case 64 => {
499
            val (succ2, rv) = memorySupport.cmpXchgLong(loc, ebi.longValue, dbi.longValue, !ptr)
500 501
            (succ2, BigInt(rv))
          }
502
          case _ => throw new UvmUnimplementedOperationException("CmpXchg on int of length %d is not supported".format(l))
503 504 505
        }
        br.asInstanceOf[BoxInt].value = OpHelper.unprepare(rbi, l)
        succ
506 507 508 509 510 511
      case _: AbstractPointerType =>
        val el = eb.asInstanceOf[BoxPointer].addr
        val dl = db.asInstanceOf[BoxPointer].addr
        val (succ, rl) = memorySupport.cmpXchgLong(loc, el, dl, !ptr)
        br.asInstanceOf[BoxPointer].addr = rl
        succ
512
      case _: TypeRef =>
513
        noAccessViaPointer(ptr, ty)
514 515
        val el = eb.asInstanceOf[BoxRef].objRef
        val dl = db.asInstanceOf[BoxRef].objRef
516
        val (succ, rl) = memorySupport.cmpXchgLong(loc, el, dl)
517 518 519
        br.asInstanceOf[BoxRef].objRef = rl
        succ
      case _: TypeIRef =>
520
        noAccessViaPointer(ptr, ty)
521 522
        val BoxIRef(el, eh) = eb.asInstanceOf[BoxIRef]
        val BoxIRef(dl, dh) = db.asInstanceOf[BoxIRef]
523
        val (succ, (rl, rh)) = memorySupport.cmpXchgI128(loc, (el, eh), (dl, dh))
524 525
        br.asInstanceOf[BoxIRef].oo = (rl, rh)
        succ
526
      case _: TypeFuncRef =>
527
        noAccessViaPointer(ptr, ty)
528 529
        val el = eb.asInstanceOf[BoxFunc].func.map(_.id).getOrElse(0).toLong
        val dl = db.asInstanceOf[BoxFunc].func.map(_.id).getOrElse(0).toLong
530
        val (succ, rl) = memorySupport.cmpXchgLong(loc, el, dl)
531 532 533
        val rf = microVM.globalBundle.funcNs.get(rl.toInt)
        br.asInstanceOf[BoxFunc].func = rf
        succ
534
      case _: TypeThreadRef =>
535
        noAccessViaPointer(ptr, ty)
536 537
        val el = eb.asInstanceOf[BoxThread].thread.map(_.id).getOrElse(0).toLong
        val dl = db.asInstanceOf[BoxThread].thread.map(_.id).getOrElse(0).toLong
538
        val (succ, rl) = memorySupport.cmpXchgLong(loc, el, dl)
Kunshan Wang's avatar
Kunshan Wang committed
539
        val rt = microVM.threadStackManager.threadRegistry.get(rl.toInt)
540 541
        br.asInstanceOf[BoxThread].thread = rt
        succ
542
      case _: TypeStackRef =>
543
        noAccessViaPointer(ptr, ty)
544 545
        val el = eb.asInstanceOf[BoxStack].stack.map(_.id).getOrElse(0).toLong
        val dl = db.asInstanceOf[BoxStack].stack.map(_.id).getOrElse(0).toLong
546
        val (succ, rl) = memorySupport.cmpXchgLong(loc, el, dl)
Kunshan Wang's avatar
Kunshan Wang committed
547
        val rs = microVM.threadStackManager.stackRegistry.get(rl.toInt)
548 549
        br.asInstanceOf[BoxStack].stack = rs
        succ
550
      case _ => throw new UvmUnimplementedOperationException("CmpXchg of type %s is not supporing".format(ty.getClass.getName))
551 552 553
    }
  }

554
  def atomicRMW(ptr: Boolean, ty: Type, op: AtomicRMWOptr, loc: Word, ob: ValueBox, br: ValueBox)(implicit microVM: MicroVM, memorySupport: MemorySupport): Unit = {
555 556 557 558
    ty match {
      case TypeInt(l) =>
        val obi = ob.asInstanceOf[BoxInt].value
        val rbi: BigInt = l match {
559 560
          case 32 => memorySupport.atomicRMWInt(op, loc, obi.intValue, !ptr)
          case 64 => memorySupport.atomicRMWLong(op, loc, obi.longValue, !ptr)
561
          case _  => throw new UvmUnimplementedOperationException("AtomicRMW on int of length %d is not supported".format(l))
562 563 564 565
        }
        br.asInstanceOf[BoxInt].value = OpHelper.unprepare(rbi, l)
      case _ =>
        if (op != XCHG) {
566
          throw new UvmUnimplementedOperationException("AtomicRMW operation other than XCHG only supports int. %s found.".format(ty.getClass.getName))
567 568
        } else {
          ty match {
569 570 571 572
            case _: AbstractPointerType =>
              val ol = ob.asInstanceOf[BoxPointer].addr
              val rl = memorySupport.atomicRMWLong(op, loc, ol, !ptr)
              br.asInstanceOf[BoxPointer].addr = rl
573
            case _: TypeRef =>
574
              noAccessViaPointer(ptr, ty)
575
              val ol = ob.asInstanceOf[BoxRef].objRef
576
              val rl = memorySupport.atomicRMWLong(op, loc, ol)
577 578
              br.asInstanceOf[BoxRef].objRef = rl
            case _: TypeIRef =>
579
              noAccessViaPointer(ptr, ty)
580
              val BoxIRef(ol, oh) = ob.asInstanceOf[BoxIRef]
581
              val (rl, rh) = memorySupport.xchgI128(loc, (ol, oh))
582
              br.asInstanceOf[BoxIRef].oo = (rl, rh)
583
            case _: TypeFuncRef =>
584
              noAccessViaPointer(ptr, ty)
585
              val ol = ob.asInstanceOf[BoxFunc].func.map(_.id).getOrElse(0).toLong
586
              val rl = memorySupport.atomicRMWLong(op, loc, ol)
587 588
              val rf = microVM.globalBundle.funcNs.get(rl.toInt)
              br.asInstanceOf[BoxFunc].func = rf
589
            case _: TypeThreadRef =>
590
              noAccessViaPointer(ptr, ty)
591
              val ol = ob.asInstanceOf[BoxThread].thread.map(_.id).getOrElse(0).toLong
592
              val rl = memorySupport.atomicRMWLong(op, loc, ol)
Kunshan Wang's avatar
Kunshan Wang committed
593
              val rt = microVM.threadStackManager.threadRegistry.get(rl.toInt)
594
              br.asInstanceOf[BoxThread].thread = rt
595
            case _: TypeStackRef =>
596
              noAccessViaPointer(ptr, ty)
597
              val ol = ob.asInstanceOf[BoxStack].stack.map(_.id).getOrElse(0).toLong
598
              val rl = memorySupport.atomicRMWLong(op, loc, ol)
Kunshan Wang's avatar
Kunshan Wang committed
599
              val rs = microVM.threadStackManager.stackRegistry.get(rl.toInt)
600 601
              br.asInstanceOf[BoxStack].stack = rs
            case _: TypeTagRef64 =>
602
              noAccessViaPointer(ptr, ty)
603
              val ol = ob.asInstanceOf[BoxTagRef64].raw
604
              val rl = memorySupport.atomicRMWLong(op, loc, ol)
605 606
              br.asInstanceOf[BoxTagRef64].raw = rl
            case _ =>
607
              throw new UvmUnimplementedOperationException("AtomicRMW XCHG of type %s is not supporing".format(ty.getClass.getName))
608 609 610
          }
        }
    }
Kunshan Wang's avatar
Kunshan Wang committed
611
  }
Kunshan Wang's avatar
Kunshan Wang committed
612 613 614 615

  /**
   * Check if a memory location still holds a particular value. Used by futex.
   */
616
  def cmpInt(len: Int, loc: Word, expected: BigInt)(implicit memorySupport: MemorySupport): Boolean = len match {
Kunshan Wang's avatar
Kunshan Wang committed
617
    case 64 => {
618
      val expNum = OpHelper.prepareSigned(expected, len).longValue
619
      val actualNum = memorySupport.loadLong(loc)
Kunshan Wang's avatar
Kunshan Wang committed
620 621 622
      expNum == actualNum
    }
    case 32 => {
623
      val expNum = OpHelper.prepareSigned(expected, len).intValue
624
      val actualNum = memorySupport.loadInt(loc)
Kunshan Wang's avatar
Kunshan Wang committed
625 626
      expNum == actualNum
    }
627
    case _ => throw new UvmUnimplementedOperationException("Futex of %d bit int is not supported".format(len))
Kunshan Wang's avatar
Kunshan Wang committed
628
  }
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659

  val US_ASCII = Charset.forName("US-ASCII")

  /**
   * Read an ASCII string from the memory.
   *
   * @param loc A ref to a @uvm.meta.bytes object.
   */
  def bytesToStr(loc: Word)(implicit memorySupport: MemorySupport): String = {
    // It is a hybrid<@i64 @i8> object. The length is determined by the fixed part. 
    val len = memorySupport.loadLong(loc)
    val bytes = new Array[Byte](len.toInt)
    val begin = loc + TypeSizes.WORD_SIZE_BYTES
    memorySupport.loadBytes(begin, bytes, 0, len, true)

    val result = new String(bytes, US_ASCII)
    result
  }

  /**
   * Create a Mu @uvm.meta.bytes object to hold an ASCII string.
   *
   * @return The address of the allocated object.
   */
  def strToBytes(str: String)(implicit memorySupport: MemorySupport, mutator: Mutator): Word = {
    val bytes = str.getBytes(US_ASCII)
    val len = bytes.length
    val loc = mutator.newHybrid(InternalTypes.BYTES, len)
    memorySupport.storeLong(loc, bytes.length.toLong)
    val begin = loc + TypeSizes.WORD_SIZE_BYTES
    memorySupport.storeBytes(begin, bytes, 0, len, true)
660

661 662
    loc
  }
663 664 665 666 667
  
  /**
   * Load irnoderef value from the memory. Use fake value from microVM.irNodeRegistry
   */
  def loadIRNode(loc: Word)(implicit microVM: MicroVM, memorySupport: MemorySupport): Option[IRNode] = {
668
    memorySupport.loadEntity[IRNode](loc)
669 670 671 672 673 674
  }

  /**
   * Store irnoderef value into the memory. Use fake value from microVM.irNodeRegistry
   */
  def storeIRNode(loc: Word, maybeIRNode: Option[IRNode])(implicit microVM: MicroVM, memorySupport: MemorySupport): Unit = {
675
    memorySupport.storeEntity(loc, maybeIRNode)
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
  }

  def loadInt32Array(base: Word, len: Word)(implicit memorySupport: MemorySupport): IndexedSeq[Int] = {
    if (base == 0L) {
      IndexedSeq[Int]()
    } else {
      for (i <- 0L until len) yield {
        val addr = base + i * 4L
        val v = memorySupport.loadInt(addr)
        v
      }
    }
  }

  def loadInt64Array(base: Word, len: Word)(implicit memorySupport: MemorySupport): IndexedSeq[Long] = {
    if (base == 0L) {
      IndexedSeq[Long]()
    } else {
      for (i <- 0L until len) yield {
Kunshan Wang's avatar
Kunshan Wang committed
695
        val addr = base + i * 8L
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
        val v = memorySupport.loadLong(addr)
        v
      }
    }
  }
  
  def loadIRNodeArray(base: Word, sz: Word)(implicit microVM: MicroVM, memorySupport: MemorySupport): IndexedSeq[IRNode] = {
    if (base == 0L) {
      IndexedSeq[IRNode]()
    } else {
      for (i <- 0L until sz) yield {
        val loc = base + i * WORD_SIZE_BYTES
        loadIRNode(loc).get
      }
    }
  }
Kunshan Wang's avatar
Kunshan Wang committed
712 713
}