cStubsHelperFunctions.scala 7.07 KB
Newer Older
Kunshan Wang's avatar
Kunshan Wang committed
1 2
package uvm.refimpl.nat

3 4
import java.nio.charset.StandardCharsets

Kunshan Wang's avatar
Kunshan Wang committed
5 6 7 8 9 10
import com.kenai.jffi.CallingConvention
import com.kenai.jffi.Closure
import com.kenai.jffi.Closure.Buffer
import com.kenai.jffi.{ Type => JType }

import CDefsHelperFunctions._
11
import NativeSupport._
Kunshan Wang's avatar
Kunshan Wang committed
12 13
import uvm.comminsts.CommInst
import uvm.comminsts.CommInsts
14 15 16 17 18
import uvm.ir.irbuilder.IRBuilder
import uvm.refimpl._
import uvm.refimpl.MicroVM
import uvm.ssavariables.{ MemoryOrder, AtomicRMWOptr }
import uvm.ssavariables.Flag
Kunshan Wang's avatar
Kunshan Wang committed
19
import uvm.ssavariables.BinOpStatus
Kunshan Wang's avatar
Kunshan Wang committed
20

Kunshan Wang's avatar
Kunshan Wang committed
21 22
class ExposedMethod(name: String, jRetTy: JType, jParamTys: Array[JType], invokeFunc: Buffer => Unit) {
  val closure = new SimpleClosure(name, invokeFunc)
Kunshan Wang's avatar
Kunshan Wang committed
23 24 25 26
  val handle = jffiClosureManager.newClosure(closure, jRetTy, jParamTys, CallingConvention.DEFAULT)
  def address = handle.getAddress()
}

Kunshan Wang's avatar
Kunshan Wang committed
27 28 29 30 31 32 33 34 35 36
class SimpleClosure(name: String, f: Buffer => Unit) extends Closure {
  def invoke(buffer: Buffer): Unit = try {
    f(buffer)
  } catch {
    case t: Throwable => {
      MuErrorNumber.setMuError(MuErrorNumber.MU_NATIVE_ERRNO)
      NativeClientSupport.logger.error(
          "Error thrown in Mu while native is calling %s. This is fatal.".format(name), t)
    }
  }
Kunshan Wang's avatar
Kunshan Wang committed
37 38 39
}

private object CDefsHelperFunctions {
40
  import NativeClientSupport._
Kunshan Wang's avatar
Kunshan Wang committed
41

Kunshan Wang's avatar
Kunshan Wang committed
42 43
  def exposedMethod(name: String, jRetTy: JType, jParamTys: Array[JType])(invokeFunc: Buffer => Unit) = {
    new ExposedMethod(name: String, jRetTy, jParamTys, invokeFunc)
Kunshan Wang's avatar
Kunshan Wang committed
44
  }
Kunshan Wang's avatar
Kunshan Wang committed
45 46 47 48
  
  def readMuIDOptional(id: MuID): Option[MuID] = {
    if (id == 0) None else Some(id)
  }
49

Kunshan Wang's avatar
Kunshan Wang committed
50 51 52 53 54 55 56 57 58 59 60
  def readIntArray(base: Long, len: Long): IndexedSeq[Int] = {
    if (base == 0L) {
      IndexedSeq[Int]()
    } else {
      for (i <- 0L until len) yield {
        val addr = base + i * 4L
        val v = theMemory.getInt(addr)
        v
      }
    }
  }
61

Kunshan Wang's avatar
Kunshan Wang committed
62 63 64 65 66 67 68 69 70 71 72
  def readLongArray(base: Long, len: Long): IndexedSeq[Long] = {
    if (base == 0L) {
      IndexedSeq[Long]()
    } else {
      for (i <- 0L until len) yield {
        val addr = base + i * 8L
        val v = theMemory.getLong(addr)
        v
      }
    }
  }
Kunshan Wang's avatar
Kunshan Wang committed
73 74
  
  def readMuIDArray(base: Long, len: Long): IndexedSeq[MuID] = readIntArray(base, len)
Kunshan Wang's avatar
Kunshan Wang committed
75 76 77 78

  def readMuValueArray(base: Long, len: Long): IndexedSeq[MuValue] = {
    readLongArray(base, len).map(getMuValueNotNull)
  }
79

Kunshan Wang's avatar
Kunshan Wang committed
80
  def readFlagArray(base: Long, len: Long): IndexedSeq[Flag] = {
Kunshan Wang's avatar
Kunshan Wang committed
81
    readIntArray(base, len).map(toCallConv)
Kunshan Wang's avatar
Kunshan Wang committed
82
  }
83 84 85 86
  
  def intToBoolean(v: Int): Boolean = v != 0
  def booleanToInt(v: Boolean): Int = if (v) 1 else 0
  
Kunshan Wang's avatar
Kunshan Wang committed
87 88
  def toMuFlag(cc: Int): MuFlag = cc
  def toCallConv(cc: Int): Flag = cc match {
Kunshan Wang's avatar
Kunshan Wang committed
89
    case 0x00 => Flag("#DEFAULT")
Kunshan Wang's avatar
Kunshan Wang committed
90
    case _    => throw new IllegalArgumentException("Unknown calling convention %d (0x%x)".format(cc, cc))
91
  }
Kunshan Wang's avatar
Kunshan Wang committed
92 93
  
  def toCommInst(ci: Int): CommInst = CommInsts(ci)
94 95 96 97 98 99 100

  val MAX_NAME_SIZE = 65536

  def readCString(addr: Long): String = {
    val str = theMemory.getString(addr, MAX_NAME_SIZE, StandardCharsets.US_ASCII)
    str
  }
Kunshan Wang's avatar
Kunshan Wang committed
101
  
102 103
  def readCStringArray(base: Long, len: Long): IndexedSeq[String] = readLongArray(base, len).map(readCString)
  
Kunshan Wang's avatar
Kunshan Wang committed
104 105 106
  def readCStringOptional(addr: Long): Option[String] = {
    if (addr == 0L) None else Some(readCString(addr))
  }
107

108 109 110
  def readCharArray(base: Long, len: Long): String = {
    require(len <= Int.MaxValue, "Currently Mu refimpl2 only supports string length with 32-bit range")
    val str = theMemory.getString(base, len.toInt, StandardCharsets.US_ASCII)
111
    str
Kunshan Wang's avatar
Kunshan Wang committed
112 113
  }
  
114 115 116 117 118 119 120
  def unsignedLongSeqToBigInt(nums: Seq[Long]): BigInt = {
      var bigNum = BigInt(0)
      for (num <- nums) {
        bigNum = (bigNum << 64) | (BigInt(num) & 0xffffffffffffffffL)
      }
      bigNum
  }
Kunshan Wang's avatar
Kunshan Wang committed
121 122
  
  def toBinOpStatus(s: MuBinOpStatus): BinOpStatus = s
123 124 125 126 127 128

  implicit class RichMuVM(val mvm: MicroVM) extends AnyVal {
    def setTrapHandler(trap_handler: MuTrapHandlerFP, userdata: MuCPtr): Unit = {
      mvm.setTrapHandler(new NativeTrapHandler(trap_handler, userdata))
    }

Kunshan Wang's avatar
Kunshan Wang committed
129
    def getMuErrorPtr(): MuCPtr = MuErrorNumber.muErrorPtr.address()
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
  }

  implicit class RichMuCtx(val ctx: MuCtx) extends AnyVal {
    def handleFromSInt8(num: Byte, len: Int): MuIntValue = ctx.handleFromInt(num, len)
    def handleFromUInt8(num: Byte, len: Int): MuIntValue = ctx.handleFromInt(BigInt(num) & 0xff, len)
    def handleFromSInt16(num: Short, len: Int): MuIntValue = ctx.handleFromInt(num, len)
    def handleFromUInt16(num: Short, len: Int): MuIntValue = ctx.handleFromInt(BigInt(num) & 0xffff, len)
    def handleFromSInt32(num: Int, len: Int): MuIntValue = ctx.handleFromInt(num, len)
    def handleFromUInt32(num: Int, len: Int): MuIntValue = ctx.handleFromInt(BigInt(num) & 0xffffffff, len)
    def handleFromSInt64(num: Long, len: Int): MuIntValue = ctx.handleFromInt(num, len)
    def handleFromUInt64(num: Long, len: Int): MuIntValue = ctx.handleFromInt(BigInt(num) & 0xffffffffffffffffL, len)
    def handleFromUInt64s(nums: IndexedSeq[Long], len: Int): MuIntValue = {
      val bigNum = unsignedLongSeqToBigInt(nums)
      ctx.handleFromInt(bigNum, len)
    }
    def handleToSInt8(h: MuIntValue): Byte = ctx.handleToSInt(h).toByte
    def handleToUInt8(h: MuIntValue): Byte = ctx.handleToUInt(h).toByte
    def handleToSInt16(h: MuIntValue): Short = ctx.handleToSInt(h).toShort
    def handleToUInt16(h: MuIntValue): Short = ctx.handleToUInt(h).toShort
    def handleToSInt32(h: MuIntValue): Int = ctx.handleToSInt(h).toInt
    def handleToUInt32(h: MuIntValue): Int = ctx.handleToUInt(h).toInt
    def handleToSInt64(h: MuIntValue): Long = ctx.handleToSInt(h).toLong
    def handleToUInt64(h: MuIntValue): Long = ctx.handleToUInt(h).toLong

    def cmpxchg(ordSucc: MemoryOrder.Value, ordFail: MemoryOrder.Value, weak: Boolean,
      loc: MuIRefValue, expected: MuValue, desired: MuValue, is_succ: IntPtr): MuValue = {
      val (v, s) = ctx.cmpXchg(ordSucc, ordFail, weak, loc, expected, desired)
      val sInt = if (s) 1 else 0
      theMemory.putInt(is_succ, sInt)
      v
    }

    def atomicrmw(ord: MemoryOrder.Value, op: AtomicRMWOptr.Value, loc: MuIRefValue, opnd: MuValue): MuValue = {
      ctx.atomicRMW(ord, op, loc, opnd)
    }

    def newThreadNor(stack: MuStackRefValue, threadLocal: Option[MuRefValue], vals: Seq[MuValue]): MuThreadRefValue = {
      ctx.newThread(stack, threadLocal, HowToResume.PassValues(vals))
    }

    def newThreadExc(stack: MuStackRefValue, threadLocal: Option[MuRefValue], exc: MuRefValue): MuThreadRefValue = {
      ctx.newThread(stack, threadLocal, HowToResume.ThrowExc(exc))
    }

    def dumpKeepalives(cursor: MuFCRefValue, results: MuValueFakArrayPtr): Unit = {
      val kas = ctx.dumpKeepalives(cursor)
      for ((ka, i) <- kas.zipWithIndex) {
        val fak = exposeMuValue(ctx, ka)
        val addr = results + i * WORD_SIZE_BYTES
        theMemory.putAddress(addr, fak)
      }
    }
Kunshan Wang's avatar
Kunshan Wang committed
182 183 184 185
  }
  
  implicit class RichMuIRBuilder(val b: IRBuilder) extends AnyVal {
    def newConstIntEx(id: MuID, ty: MuID, values: Seq[Long]): Unit = {
186
      val bigNum = unsignedLongSeqToBigInt(values)
Kunshan Wang's avatar
Kunshan Wang committed
187
      b.newConstInt(id, ty, bigNum)
188 189 190 191
    }
  }
  
  implicit def makeMuValueSeqCovariant[T <: MuValue, U <: T](seq: Seq[T]): Seq[U] = seq.asInstanceOf[Seq[U]]
Kunshan Wang's avatar
Kunshan Wang committed
192
  implicit def muValueAutoCast[T <: MuValue, U <: T](v: T): U = v.asInstanceOf[U]
193

Kunshan Wang's avatar
Kunshan Wang committed
194
}