GlobalMemory.scala 1.15 KB
Newer Older
1 2 3 4 5 6
package uvm.refimpl.mem

import uvm.types._
import uvm.ssavariables.GlobalCell
import uvm.refimpl._
import uvm.refimpl.mem.TypeSizes._
7
import uvm.refimpl.mem.bumppointer.RewindableBumpPointerAllocator
8 9
import java.util.HashMap

10 11 12
class GlobalMemory(begin: Word, size: Word)(
  implicit microVM: MicroVM, memorySupport: MemorySupport)
    extends Space("GlobalSpace", begin, size) {
13

14
  val allocator = new RewindableBumpPointerAllocator(begin, size)
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

  private val locationMap = new HashMap[GlobalCell, Word]()

  def addGlobalCell(gc: GlobalCell) {
    val ty = gc.cellTy
    if (ty.isInstanceOf[TypeHybrid]) {
      throw new UvmRefImplException("It does not make sense to make global hybrid (use array and any fixed types). global data: " + gc.repr)
    }
    val addr = allocateGlobalCellMemory(ty)
    locationMap.put(gc, addr)
  }

  private def allocateGlobalCellMemory(ty: Type): Word = {
    val tag = ty.id
    val size = sizeOf(ty)
    val align = alignOf(ty)
    val objAddr = allocator.alloc(size, align, TypeSizes.GC_HEADER_SIZE_SCALAR)
    HeaderUtils.postAllocScalar(objAddr, tag)
    objAddr
  }

  def addrForGlobalCell(gc: GlobalCell): Word = locationMap.get(gc)
}