MicroVM.scala 2.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
package uvm.refimpl

import uvm._
import uvm.refimpl.itpr._
import uvm.refimpl.mem._
import uvm.refimpl.mem.TypeSizes.Word
import scala.collection.mutable.HashSet
import uvm.ir.textinput.UIRTextReader
import uvm.ir.textinput.IDFactory

object MicroVM {
  val DEFAULT_HEAP_SIZE: Word = 4L * 1024L * 1024L; // 4MiB
  val DEFAULT_GLOBAL_SIZE: Word = 1L * 1024L * 1024L; // 1MiB
  val DEFAULT_STACK_SIZE: Word = 63L * 1024L; // 60KiB per stack
}

class MicroVM(heapSize: Word = MicroVM.DEFAULT_HEAP_SIZE,
  globalSize: Word = MicroVM.DEFAULT_GLOBAL_SIZE,
  stackSize: Word = MicroVM.DEFAULT_STACK_SIZE) {
20 21 22
  
  // implicitly injected resources
  private implicit val microVM = this
23 24

  val globalBundle = new Bundle()
25 26 27 28
  val constantPool = new ConstantPool()
  val memoryManager = new MemoryManager(heapSize, globalSize, stackSize)
  val threadStackManager = new ThreadStackManager()
  val trapManager = new TrapManager()
29 30 31
  val clientAgents = new HashSet[ClientAgent]()
  
  val irReader = new UIRTextReader(new IDFactory())
32 33
  
  {
Kunshan Wang's avatar
Kunshan Wang committed
34
    // The micro VM allocates stacks on the heap in the large object space. It is represented as a bug chunk of byte array.
35 36 37 38 39 40 41 42
    // So the GC must know about this type because the GC looks up the globalBundle for types.
    globalBundle.allNs.add(InternalTypes.VOID)
    globalBundle.typeNs.add(InternalTypes.VOID)
    globalBundle.allNs.add(InternalTypes.BYTE)
    globalBundle.typeNs.add(InternalTypes.BYTE)
    globalBundle.allNs.add(InternalTypes.BYTE_ARRAY)
    globalBundle.typeNs.add(InternalTypes.BYTE_ARRAY)
  }
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

  /**
   * Add things from a bundle to the Micro VM.
   */
  def addBundle(bundle: Bundle) {
    globalBundle.merge(bundle);

    for (gc <- bundle.globalCellNs.all) {
      memoryManager.globalMemory.addGlobalCell(gc)
    }
    for (g <- bundle.globalVarNs.all) {
      constantPool.addGlobalVar(g)
    }
  }
  
58 59 60
  /**
   * Create a new ClientAgent.
   */
61 62
  def newClientAgent(): ClientAgent = new ClientAgent(this)

63 64 65 66 67 68 69 70 71
  /**
   * Given a name, get the ID of an identified entity.
   */
  def idOf(name: String): Int = globalBundle.allNs(name).id
  
  /**
   * Given an ID, get the name of an identified entity.
   */
  def nameOf(id: Int): String = globalBundle.allNs(id).name.get
72
}