ConstantPool.scala 1.29 KB
Newer Older
1 2 3 4 5 6 7 8
package uvm.refimpl.itpr

import uvm._
import uvm.types._
import uvm.ssavariables._
import uvm.refimpl.MicroVM
import scala.collection.mutable.HashMap

9
class ConstantPool(implicit microVM: MicroVM) {
10 11 12 13 14 15 16 17 18 19 20 21 22
  val globalVarBoxes = HashMap[GlobalVariable, ValueBox]()

  def addGlobalVar(g: GlobalVariable) {
    maybeMakeBox(g)
  }

  def maybeMakeBox(g: GlobalVariable): ValueBox = {
    val box = globalVarBoxes.get(g).getOrElse(makeBox(g))
    globalVarBoxes.put(g, box)
    box
  }

  def makeBox(g: GlobalVariable): ValueBox = g match {
23
    case ConstInt(ty, num) => BoxInt(OpHelper.unprepare(num, ty.asInstanceOf[TypeInt].length))
24 25 26 27 28 29 30 31 32 33
    case ConstFloat(ty, num) => BoxFloat(num)
    case ConstDouble(ty, num) => BoxDouble(num)
    case ConstStruct(ty, flds) => BoxStruct(flds.map(maybeMakeBox))
    case ConstNull(ty) => ty match {
      case _:TypeRef => BoxRef(0L)
      case _:TypeIRef => BoxIRef(0L, 0L)
      case _:TypeFunc => BoxFunc(None)
      case _:TypeThread => BoxThread(None)
      case _:TypeStack => BoxStack(None)
    }
34
    case ConstVector(ty, elems) => BoxVector(elems.map(maybeMakeBox))
35 36 37 38 39 40
    case gc:GlobalCell => BoxIRef(0L, microVM.memoryManager.globalMemory.addrForGlobalCell(gc))
    case f:Function => BoxFunc(Some(f))
  }
  
  def getGlobalVarBox(g: GlobalVariable): ValueBox = globalVarBoxes(g)
}