writers.scala 2.67 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
package uvm.clientsupport.text

import scala.collection.JavaConversions._

object TextIRWriter {
  def bundleToText(bundle: Bundle): String = {
    val sb = new StringBuilder()

    sb.toString()
  }

  def typeToText(typeDef: TypeDef): String = {
    val name = typeDef.name
    val ctor = typeDef match {
      case t: TypeInt     => "int<%d>".format(t.len)
      case t: TypeFloat   => "float"
      case t: TypeDouble  => "double"
      case t: TypeRef     => "ref<%s>".format(t.ty)
      case t: TypeIRef    => "iref<%s>".format(t.ty)
      case t: TypeWeakRef => "weakref<%s>".format(t.ty)
      case t: TypeStruct => {
        val sb = new StringBuilder("struct<")
        for (ty <- t.fieldTy) sb ++= " " ++= ty
        sb ++= " >"
        sb.toString
      }
      case t: TypeArray    => "array<%s %d>".format(t.elemTy, t.len)
      case t: TypeHybrid   => "hybrid<%s %s>".format(t.fixedTy, t.varTy)
      case t: TypeVoid     => "void"
      case t: TypeFunc     => "func<%s>".format(t.sig)
      case t: TypeThread   => "thread"
      case t: TypeStack    => "stack"
      case t: TypeTagRef64 => "tagref64"
      case t: TypePtr      => "ptr<%s>".format(t.ty)
      case t: TypeFuncPtr  => "funcptr<%s>".format(t.sig)
    }

    ".typedef %s = %s".format(name, ctor)
  }
  
  def funcSigToText(funcSigDef: FuncSigDef): String = {
    val paramTys = {
      val sb = new StringBuilder(" ")
      for (ty <- funcSigDef.paramTy) sb ++= ty ++= " "
      sb.toString
    }
    ".funcsig %s = %s (%s)".format(funcSigDef.name, funcSigDef.retTy, funcSigDef.paramTy)
  }
  
  def constToText(constDef: ConstDef): String = {
    val name = constDef.name
    val ty = constDef.ty
    val ctor: String = constDef match {
      case c: ConstInt => c.num.toString
      case c: ConstFloat => "bitsf(0x%x)".format(java.lang.Float.floatToRawIntBits(c.num))
      case c: ConstDouble => "bitsd(0x%x)".format(java.lang.Double.doubleToRawLongBits(c.num))
      case c: ConstStruct => {
        val sb = new StringBuilder("{ ")
        for (f <- c.fields) sb ++= f ++= " "
        sb ++= "}"
        sb.toString
      }
      case c: ConstNull => "NULL"
      case c: ConstPointer => c.addr.toString
    }
    ".const %s <%s> = %s".format(name, ty, ctor)
  }
  
  def globalToText(globalCellDef: GlobalCellDef): String = {
    ".global %s <%s>".format(globalCellDef.name, globalCellDef.ty)
  }
  
  def funcDeclToText(funcDecl: FuncDecl): String = {
    ".funcdecl %s <%s>".format(funcDecl.name, funcDecl.sig)
  }
  
  def funcExpToText(funcExp: FuncExpDef): String = {
    ".expose %s = %s %s %s".format(funcExp.name, funcExp.func, funcExp.callConv, funcExp.cookie)
  }
  
  def funcDefToText(funcDef: FuncDef): String = {
    ???
  }
}