Commit 7d36baaf authored by Kunshan Wang's avatar Kunshan Wang

Fixed gen_sym.

It should treat the string parameter as an iref to a 0-terminated
string.
parent af11723e
......@@ -8,6 +8,7 @@ import uvm.refimpl.mem._
import uvm.refimpl.nat.CDefs
import uvm.ssavariables._
import uvm.types._
import uvm.refimpl.nat.NativeMemoryAccessHelper
object IRBuilderCommInstExecutor {
implicit def toBinOptr(i: Int): BinOptr.Value = CDefs.toBinOptr(i)
......@@ -63,17 +64,13 @@ trait IRBuilderCommInstExecutor extends InterpreterActions with ObjectPinner {
protected def loadCString(ir: (Word, Word)): String = {
val (obj, off) = ir
val loc = obj + off
MemoryOperations.bytesToStr(loc)
NativeMemoryAccessHelper.readCString(loc)
}
protected def loadMaybeCString(ir: (Word, Word)): Option[String] = {
val (obj, off) = ir
val loc = obj + off
loc match {
case 0L => None
case _ => Some(MemoryOperations.bytesToStr(loc))
}
NativeMemoryAccessHelper.readCStringOptional(loc)
}
private type TB = TrantientBundle
......
......@@ -12,6 +12,7 @@ import uvm.refimpl.mem.TypeSizes._
import uvm.ssavariables._
import uvm.ssavariables.AtomicRMWOptr._
import uvm.types._
import uvm.refimpl.nat.NativeMemoryAccessHelper
object OpHelper {
......
package uvm.refimpl.nat
import java.nio.charset.StandardCharsets
object NativeMemoryAccessHelper {
import NativeSupport._
val MAX_NAME_SIZE = 65536
def readCString(addr: Long): String = {
val str = theMemory.getString(addr, MAX_NAME_SIZE, StandardCharsets.US_ASCII)
str
}
def readCStringArray(base: Long, len: Long): IndexedSeq[String] = readLongArray(base, len).map(readCString)
def readCStringOptional(addr: Long): Option[String] = {
if (addr == 0L) None else Some(readCString(addr))
}
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)
str
}
def unsignedLongSeqToBigInt(nums: Seq[Long]): BigInt = {
var bigNum = BigInt(0)
for (num <- nums) {
bigNum = (bigNum << 64) | (BigInt(num) & 0xffffffffffffffffL)
}
bigNum
}
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
}
}
}
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
}
}
}
}
\ No newline at end of file
......@@ -7,6 +7,7 @@ import uvm.refimpl._
import uvm.ssavariables.{BinOptr, CmpOptr, ConvOptr, MemoryOrder, AtomicRMWOptr}
object CDefs {
import NativeMemoryAccessHelper._
import CDefsHelperFunctions._
import NativeClientSupport._
......
......@@ -38,6 +38,7 @@ class SimpleClosure(name: String, f: Buffer => Unit) extends Closure {
private object CDefsHelperFunctions {
import NativeClientSupport._
import NativeMemoryAccessHelper._
def exposedMethod(name: String, jRetTy: JType, jParamTys: Array[JType])(invokeFunc: Buffer => Unit) = {
new ExposedMethod(name: String, jRetTy, jParamTys, invokeFunc)
......@@ -47,30 +48,6 @@ private object CDefsHelperFunctions {
if (id == 0) None else Some(id)
}
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
}
}
}
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
}
}
}
def readMuIDArray(base: Long, len: Long): IndexedSeq[MuID] = readIntArray(base, len)
def readMuValueArray(base: Long, len: Long): IndexedSeq[MuValue] = {
......@@ -92,32 +69,6 @@ private object CDefsHelperFunctions {
def toCommInst(ci: Int): CommInst = CommInsts(ci)
val MAX_NAME_SIZE = 65536
def readCString(addr: Long): String = {
val str = theMemory.getString(addr, MAX_NAME_SIZE, StandardCharsets.US_ASCII)
str
}
def readCStringArray(base: Long, len: Long): IndexedSeq[String] = readLongArray(base, len).map(readCString)
def readCStringOptional(addr: Long): Option[String] = {
if (addr == 0L) None else Some(readCString(addr))
}
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)
str
}
def unsignedLongSeqToBigInt(nums: Seq[Long]): BigInt = {
var bigNum = BigInt(0)
for (num <- nums) {
bigNum = (bigNum << 64) | (BigInt(num) & 0xffffffffffffffffL)
}
bigNum
}
def toBinOpStatus(s: MuBinOpStatus): BinOpStatus = s
......
......@@ -24,9 +24,10 @@ class UvmInterpreterIRBuilderTests extends UvmBundleTesterBase with ExtraMatcher
preloadBundles("tests/uvm-refimpl-test/primitives.uir",
"tests/uvm-refimpl-test/irbuilder-tests.uir")
preloadHails("tests/uvm-refimpl-test/irbuilder-tests.hail")
// Dump bundles to see run-time-loaded bundles
override def makeMicroVM = MicroVM(new VMConf(dumpBundle=true))
override def makeMicroVM = MicroVM(new VMConf(sourceInfo=true, dumpBundle=true))
"The new_ir_builder COMMINST" should "create an IR builder" in {
val ctx = microVM.newContext()
......@@ -46,10 +47,14 @@ class UvmInterpreterIRBuilderTests extends UvmBundleTesterBase with ExtraMatcher
returnFromTrap(st)
}
case "@irbuilder_test.v1.entry.trap2" => {
val Seq(mySecondConstByID: MuIntValue) = ctx.dumpKeepalives(st, 0)
val Seq(mySecondConstByID: MuIntValue, id2: MuIntValue) = ctx.dumpKeepalives(st, 0)
mySecondConstByID.asInt32 shouldBe 50
val id2_i32 = id2.asInt32.toInt
val myname = microVM.nameOf(id2_i32)
myname shouldEqual "@irbuilder_test.hello"
returnFromTrap(st)
}
case _ => fail("Should not hit " + trapName)
......
// "@irbuilder_test.hello\0", zero terminated
.init @g_myname = {64 105 114 98 117 105 108 100 101 114 95 116 101 115 116 46 104 101 108 108 111 0}
......@@ -4,11 +4,15 @@
.const @I32_64 <@i32> = 64
.typedef @myname_t = array<@i8 32>
.global @g_myname <@myname_t>
.funcdef @irbuilder_test VERSION %v1 <@v_v> {
%entry():
%irb = COMMINST @uvm.irbuilder.new_ir_builder
%id1 = COMMINST @uvm.irbuilder.gen_sym (%irb @NULLIREFI8)
%id2 = COMMINST @uvm.irbuilder.gen_sym (%irb @NULLIREFI8)
%g_myname_0 = GETELEMIREF <@myname_t @i64> @g_myname @I64_0
%id2 = COMMINST @uvm.irbuilder.gen_sym (%irb %g_myname_0)
COMMINST @uvm.irbuilder.new_type_int(%irb %id1 @I32_64)
%my_second_const = ADD <@i64> @MYFIRSTCONST @I64_1
......@@ -20,7 +24,7 @@
%my_second_const_byid = COMMINST @uvm.meta.constant_by_id <@i32> (%id2)
[%trap2] TRAP <> KEEPALIVE (%my_second_const_byid)
[%trap2] TRAP <> KEEPALIVE (%my_second_const_byid %id2)
COMMINST @uvm.thread_exit
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment