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

Initial commit.

parents
File added
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/scala"/>
<classpathentry kind="src" path="src/test/scala"/>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="src" path="src/test/java"/>
<classpathentry kind="con" path="org.scala-ide.sdt.launching.SCALA_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="output" path="target/eclipsebuild"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>simplest-microvm-project</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.scala-ide.sdt.core.scalabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.scala-ide.sdt.core.scalanature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7
package uvm
class Bundle {
}
\ No newline at end of file
package uvm
import uvm.types._
class GlobalData extends IdentifiedSettable {
var ty: Type = null
}
\ No newline at end of file
package uvm
trait Identified {
def id: Int
def name: Option[String]
def repr: String = "[%d:%s]".format(id, name.getOrElse("_"))
}
trait IdentifiedSettable extends Identified {
var id: Int = 0
var name: Option[String] = None
}
\ No newline at end of file
package uvm
import uvm.types._
import uvm.ssavalues._
case class FuncSig(var retTy: Type, var paramTy: Seq[Type]) extends IdentifiedSettable
object FuncSig {
def prettyPrint(sig: FuncSig): String =
"%s (%s)".format(sig.retTy.repr, sig.paramTy.map(_.repr).mkString(" "))
}
class Function extends IdentifiedSettable {
var sig: FuncSig = null
var cfg: Option[CFG] = None
}
class CFG {
var func: Function = null
var bbs: Seq[BasicBlock] = Nil
var entry: BasicBlock = null
}
class BasicBlock extends IdentifiedSettable {
var insts: Seq[Instruction] = Nil
}
\ No newline at end of file
package uvm
class UvmException(message: String = null, cause: Throwable = null) extends RuntimeException(message, cause)
class TypeResolutionException(message: String = null, cause: Throwable = null) extends UvmException(message, cause)
class NameConflictException(message: String = null, cause: Throwable = null) extends UvmException(message, cause)
\ No newline at end of file
package uvm.ifuncs
import uvm._
import uvm.types._
import uvm.types.CommonTypes._
case class IFunc(val id: Int, val name: Option[String], val sig: FuncSig) extends Identified {
def ty = sig.retTy
}
object IFuncs extends SimpleNamespace[IFunc] {
private def addIFunc(id: Int, name: String, retTy: Type, paramTy: Seq[Type]): IFunc = {
val iFunc = IFunc(id, Some(name), FuncSig(retTy, paramTy))
add(iFunc)
return iFunc
}
val UVM_NEW_THREAD = addIFunc(0x201, "@uvm.new_thread", VOID, Seq(THREAD))
val UVM_SWAP_STACK = addIFunc(0x202, "@uvm.swap_stack", VOID, Seq(STACK))
val UVM_KILL_STACK = addIFunc(0x203, "@uvm.kill_stack", VOID, Seq(STACK))
val UVM_SWAP_AND_KILL = addIFunc(0x204, "@uvm.swap_and_kill", VOID, Seq(STACK))
val UVM_THREAD_EXIT = addIFunc(0x205, "@uvm.thread_exit", VOID, Seq(THREAD))
val UVM_MATH_SIN = addIFunc(0x101, "@uvm.math.sin", DOUBLE, Seq(DOUBLE))
}
\ No newline at end of file
package uvm
abstract class Namespace[T <: Identified] {
def apply(id: Int): T
def apply(name: String): T
def get(id: Int): Option[T]
def get(name: String): Option[T]
def add(obj: T): Unit
}
class SimpleNamespace[T <: Identified] extends Namespace[T] {
private type MapType[K, V] = collection.mutable.HashMap[K, V]
private val idMap = new MapType[Int, T]()
private val nameMap = new MapType[String, T]()
def apply(id: Int): T = idMap(id)
def apply(name: String): T = nameMap(name)
def get(id: Int): Option[T] = idMap.get(id)
def get(name: String): Option[T] = nameMap.get(name)
def add(obj: T): Unit = {
for (obj2 <- get(obj.id)) {
throw new NameConflictException(
"Object %s ID-conflicts with %s".format(obj.repr, obj2.repr))
}
for (name <- obj.name; obj2 <- get(name)) {
throw new NameConflictException(
"Object %s name-conflicts with %s".format(obj.repr, obj2.repr))
}
idMap.put(obj.id, obj)
obj.name match {
case None =>
case Some(name) => nameMap.put(name, obj)
}
}
}
package uvm.ssavalues
import uvm._
import uvm.types._
import uvm.types.CommonTypes._
import uvm.ifuncs._
abstract class Value extends IdentifiedSettable {
def ty: Type
def resolve() {}
}
// Constants
abstract class Constant extends Value
abstract class DeclaredConstant extends Constant {
var constTy: Type
def ty = constTy
}
case class ConstInt(var constTy: Type, var num: BigInt) extends DeclaredConstant
case class ConstFloat(var constTy: Type, var num: Float) extends DeclaredConstant
case class ConstDouble(var constTy: Type, var num: Double) extends DeclaredConstant
case class ConstStruct(var constTy: Type, var fields: Seq[Constant]) extends DeclaredConstant
case class ConstNull(var constTy: Type) extends DeclaredConstant
case class ConstFunc(var func: Function) extends Constant {
def ty = func.sig.retTy
}
case class ConstGlobalData(var gd: GlobalData) extends Constant {
def ty = gd.ty
}
// Parameter
case class Parameter(var sig: FuncSig, var index: Int) extends Value {
def ty = sig.retTy
}
// Instructions
abstract class Instruction extends Value
/// enumerations
object BinOptr extends Enumeration {
type BinOptr = Value
val ADD, SUB, MUL, UDIV, SDIV, UREM, SREM, SHL, LSHR, ASHR, AND, OR, XOR = Value
val FADD, FSUB, FMUL, FDIV, FREM = Value
}
import BinOptr.BinOptr
object CmpOptr extends Enumeration {
type CmpOptr = Value
val EQ, NE, ULT, ULE, UGT, UGE, SLT, SLE, SGT, SGE = Value
val FTRUE, FFALSE, FORD, FOEQ, FONE, FOLT, FOLE, FOGT, FOGE = Value
val FUNO, FUEQ, FUNE, FULT, FULE, FUGT, FUGE = Value
}
import CmpOptr.CmpOptr
object ConvOptr extends Enumeration {
type ConvOptr = Value
val TRUNC, ZEXT, SEXT, FPTRUNC, FPEXT, FPTOUI, FPTOSI, UITOFP, SITOFP, BITCAST = Value
val REFCAST, IREFCAST, FUNCCAST = Value
}
import ConvOptr.ConvOptr
object MemoryOrdering extends Enumeration {
type MemoryOrdering = Value
val NOT_ATOMIC, UNORDERED, MONOTONIC, ACQUIRE, RELEASE, ACQ_REL, SEQ_CST = Value
}
import MemoryOrdering.MemoryOrdering
object AtomicRMWOptr extends Enumeration {
type AtomicRMWOptr = Value
val XCHG, ADD, SUB, AND, NAND, OR, XOR, MIN, MAX, UMIN, UMAX = Value
}
import AtomicRMWOptr.AtomicRMWOptr
object CallConv extends Enumeration {
type CallConv = Value
val DEFAULT = Value
}
import CallConv.CallConv
/// Abstract instructions and traits
trait HasArgs extends Instruction {
var args: Seq[Value]
}
trait CallLike extends HasArgs {
var sig: FuncSig
var callee: Value
}
trait HasExceptionalDest extends Instruction {
var nor: BasicBlock
var exc: BasicBlock
}
trait HasKeepAlives extends Instruction {
def keepAlives: Seq[Value]
}
abstract class AbstractCall extends HasArgs with CallLike
abstract class NonTailCall extends AbstractCall with HasKeepAlives {
def ty = sig.retTy
}
abstract class AbstractRet extends Instruction {
def ty = VOID
}
abstract class AbstractAlloc extends Instruction {
def allocTy: Type
var ty: Type = null
}
trait ScalarAlloc extends AbstractAlloc {
def allocTy: Type
}
trait HybridAlloc extends AbstractAlloc {
def allocTy: TypeHybrid
def length: Value
}
abstract class HeapAlloc extends AbstractAlloc {
override def resolve() { ty = TypeRef(allocTy) }
}
abstract class StackAlloc extends AbstractAlloc {
override def resolve() { ty = TypeIRef(allocTy) }
}
abstract class WeakRefLoader extends Instruction {
def referentTy: Type
var ty: TypeRef = null
override def resolve() {
referentTy match {
case TypeWeakRef(t) => { ty = TypeRef(t) }
case TypeRef(t) => { ty = referentTy.asInstanceOf[TypeRef] }
case _ => { throw new TypeResolutionException("Expect weakref or ref, %s found".format(referentTy)) }
}
}
}
abstract class AbstractTrap extends HasExceptionalDest with HasKeepAlives {
var retTy: Type
def ty = retTy
}
abstract class AbstractICall extends HasArgs with HasKeepAlives {
var iFunc: IFunc
def ty = iFunc.ty
}
/// Concrete instructions
case class InstBinOp(var op: BinOptr, var opndTy: Type, var op1: Value, var op2: Value) extends Instruction {
def ty = opndTy
}
case class InstCmp(var op: CmpOptr, var opndTy: Type, var op1: Value, var op2: Value) extends Instruction {
def ty = I1
}
case class InstConv(var op: ConvOptr, var fromTy: Type, var toTy: Type, var opnd: Value) extends Instruction {
def ty = toTy
}
case class InstSelect(var opndTy: Type, var cond: Value, var ifTrue: Value, var ifFalse: Value) extends Instruction {
def ty = opndTy
}
case class InstBranch(var dest: BasicBlock) extends Instruction {
def ty = VOID
}
case class InstBranch2(var cond: Value, var ifTrue: BasicBlock, var ifFalse: BasicBlock) extends Instruction {
def ty = VOID
}
case class InstSwitch(var opndTy: Type, var opnd: Value, var defDest: BasicBlock,
var cases: Seq[(Value, BasicBlock)]) extends Instruction {
def ty = VOID
}
case class InstPhi(var opndTy: Type, var cases: Seq[(BasicBlock, Value)]) extends Instruction {
def ty = opndTy
}
case class InstCall(var sig: FuncSig, var callee: Value, var args: Seq[Value],
var keepAlives: Seq[Value]) extends NonTailCall
case class InstInvoke(var sig: FuncSig, var callee: Value, var args: Seq[Value],
var nor: BasicBlock, var exc: BasicBlock, var keepAlives: Seq[Value]) extends NonTailCall with HasExceptionalDest
case class InstTailCall(var sig: FuncSig, var callee: Value, var args: Seq[Value]) extends AbstractCall {
def ty = VOID
}
case class InstRet(var retTy: Type, var retVal: Value) extends AbstractRet
case class InstRetVoid extends AbstractRet
case class InstThrow(var excVal: Value) extends Instruction {
def ty = VOID
}
case class InstLandingpad extends Instruction {
def ty = REFVOID
}
case class InstExtractValue(var strTy: TypeStruct, var index: Int, var opnd: Value) extends Instruction {
def ty = strTy.fieldTy(index)
}
case class InstInsertValue(var strTy: TypeStruct, var index: Int, var opnd: Value, var newVal: Value) extends Instruction {
def ty = strTy
}
case class InstNew(var allocTy: Type) extends HeapAlloc with ScalarAlloc
case class InstNewHybrid(var allocTy: TypeHybrid, var length: Value) extends HeapAlloc with HybridAlloc
case class InstAlloca(var allocTy: Type) extends StackAlloc with ScalarAlloc
case class InstAllocaHybrid(var allocTy: TypeHybrid, var length: Value) extends StackAlloc with HybridAlloc
case class InstGetIRef(var referentTy: Type, var opnd: Value) extends Instruction {
var ty: Type = null
override def resolve() { ty = TypeIRef(referentTy) }
}
case class InstGetFieldIRef(var referentTy: TypeStruct, var index: Int, var opnd: Value) extends Instruction {
var ty: Type = null
override def resolve() { ty = TypeIRef(referentTy.fieldTy(index)) }
}
case class InstGetElemIRef(var referentTy: TypeArray, var opnd: Value, var index: Value) extends Instruction {
var ty: Type = null
override def resolve() { ty = TypeIRef(referentTy.elemTy) }
}
case class InstShiftIRef(var referentTy: Type, var opnd: Value, var offset: Value) extends Instruction {
var ty: Type = null
override def resolve() { ty = TypeIRef(referentTy) }
}
case class InstGetFixedPartIRef(var referentTy: TypeHybrid, var opnd: Value) extends Instruction {
var ty: Type = null
override def resolve() { ty = TypeIRef(referentTy.fixedPart) }
}
case class InstGetVarPartIRef(var referentTy: TypeHybrid, var opnd: Value) extends Instruction {
var ty: Type = null
override def resolve() { ty = TypeIRef(referentTy.varPart) }
}
case class InstLoad(var ord: MemoryOrdering, var referentTy: Type, var loc: Value)
extends WeakRefLoader
case class InstStore(var ord: MemoryOrdering, var referentTy: Type, var loc: Value, var newVal: Value) extends Instruction {
def ty = VOID
}
case class InstCmpXchg(var ordSucc: MemoryOrdering, var ordFail: MemoryOrdering,
var referentTy: Type, var loc: Value, var expected: Value, var desired: Value)
extends WeakRefLoader
case class InstFence(var ord: MemoryOrdering) extends Instruction {
def ty = VOID
}
case class InstAtomicRMW(var ord: MemoryOrdering, var op: AtomicRMWOptr,
var referentTy: Type, var loc: Value, var newVal: Value) extends Instruction {
def ty = referentTy
}
case class InstTrap(var retTy: Type, var nor: BasicBlock, var exc: BasicBlock,
var keepAlives: Seq[Value]) extends AbstractTrap
case class InstWatchpoint(var wpID: Int, var retTy: Type, var dis: BasicBlock,
var nor: BasicBlock, var exc: BasicBlock, var keepAlives: Seq[Value]) extends AbstractTrap
case class InstCCall(var callConv: CallConv, var sig: FuncSig, var callee: Value, var args: Seq[Value])
extends CallLike {
def ty = sig.retTy
}
case class InstNewStack(var sig: FuncSig, var callee: Value, var args: Seq[Value]) extends CallLike {
def ty = STACK
}
case class InstICall(var iFunc: IFunc, var args: Seq[Value], var keepAlives: Seq[Value])
extends AbstractICall
case class InstIInvoke(var iFunc: IFunc, var args: Seq[Value],
var nor: BasicBlock, var exc: BasicBlock, var keepAlives: Seq[Value])
extends AbstractICall with HasExceptionalDest
package uvm.types
import uvm._
abstract class Type extends IdentifiedSettable {
override final def toString: String = Type.prettyPrint(this)
}
case class TypeInt(var length: Int) extends Type
case class TypeFloat() extends Type
case class TypeDouble() extends Type
case class TypeRef(var ty: Type) extends Type
case class TypeIRef(var ty: Type) extends Type
case class TypeWeakRef(var ty: Type) extends Type
case class TypeStruct(var fieldTy: Seq[Type]) extends Type
case class TypeArray(var elemTy: Type, var len: Long) extends Type
case class TypeHybrid(var fixedPart: Type, var varPart: Type) extends Type
case class TypeVoid extends Type
case class TypeFunc(var sig: FuncSig) extends Type
case class TypeThread() extends Type
case class TypeStack() extends Type
case class TypeTagRef64 extends Type
object Type {
def prettyPrint(ty: Type): String = ty match {
case TypeInt(length) => "int<%d>".format(length)
case TypeFloat() => "float"
case TypeDouble() => "double"
case TypeRef(ty) => "ref<%s>".format(ty.repr)
case TypeIRef(ty) => "iref<%s>".format(ty.repr)
case TypeWeakRef(ty) => "weakref<%s>".format(ty.repr)
case TypeStruct(fieldTy) => "struct<%s>".format(fieldTy.map(_.repr).mkString(" "))
case TypeArray(elemTy, len) => "array<%s %d>".format(elemTy.repr, len)
case TypeHybrid(fixedPart, varPart) => "hybrid<%s %s>".format(fixedPart.repr, varPart.repr)
case TypeVoid() => "void"
case TypeFunc(sig) => "func<%s>".format(FuncSig.prettyPrint(sig))
case TypeThread() => "thread"
case TypeStack() => "stack"
case TypeTagRef64() => "tagref64"
case _ => "unknown type " + ty.getClass.getName
}
}
object CommonTypes {
val I1 = TypeInt(1)
val I8 = TypeInt(8)
val I16 = TypeInt(16)
val I32 = TypeInt(32)
val I64 = TypeInt(64)
val IWORD = TypeInt(64)
val FLOAT = TypeFloat()
val DOUBLE = TypeDouble()
val VOID = TypeVoid()
val REFVOID = TypeRef(VOID)
val THREAD = TypeThread()
val STACK = TypeStack()
}
\ No newline at end of file
package junks
import uvm._
import uvm.types._
import uvm.ssavalues._
object Main extends App {
val i64 = TypeInt(64)
println(i64.length)
val ic = ConstInt(i64, 999)
println(ic.ty)
val iNew: Value = InstNew(i64)
iNew.resolve
println(i64)
val ref = TypeRef(i64)
val refNode: TypeRef = TypeRef(null)
refNode.id = 2;
refNode.name = Some("@refNode")
val strNode = TypeStruct(Seq(i64, refNode))
strNode.id = 3;
strNode.name = Some("@strNode")
refNode.ty = strNode
println(refNode)
println(strNode)
val ns = new SimpleNamespace[Type]()
ns.add(i64)
ns.add(refNode)
println(ns("@refNode"))
}
\ No newline at end of file
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