ssavariables.scala 10.9 KB
Newer Older
1 2 3 4 5 6
package uvm.ssavariables

import uvm._
import uvm.comminsts._
import uvm.types._

7
abstract class SSAVariable extends IdentifiedSettable
8 9 10 11 12 13 14 15 16

// Global variables: Constants, Global Cells and Functions (Function is defined in controlFlow.scala)

abstract class GlobalVariable extends SSAVariable

abstract class Constant extends GlobalVariable {
  var constTy: Type
}

17
/** For both int<n> and pointers. Convert to Long when needed. */
18 19 20 21 22 23
case class ConstInt(var constTy: Type, var num: BigInt) extends Constant

case class ConstFloat(var constTy: Type, var num: Float) extends Constant

case class ConstDouble(var constTy: Type, var num: Double) extends Constant

24
/** For struct, array and vector. */
Kunshan Wang's avatar
Kunshan Wang committed
25
case class ConstSeq(var constTy: Type, var elems: Seq[GlobalVariable]) extends Constant
Kunshan Wang's avatar
Kunshan Wang committed
26

27 28
/** For all NULL-able values. Note: null pointers are ConstInt(???, 0) */
case class ConstNull(var constTy: Type) extends Constant
29

30 31
case class GlobalCell(var cellTy: Type) extends GlobalVariable

32
case class ExposedFunc(var func: Function, var callConv: Flag, var cookie: ConstInt) extends GlobalVariable
33

34 35
// Local variables: Parameters and Instructions

Kunshan Wang's avatar
Kunshan Wang committed
36
abstract class LocalVariable extends SSAVariable
37

Kunshan Wang's avatar
Kunshan Wang committed
38 39 40
abstract class Parameter extends LocalVariable
case class NorParam(ty: Type) extends Parameter
case class ExcParam() extends Parameter
41

42 43
case class InstResult(inst: Instruction, index: Int) extends LocalVariable

44 45
// Instructions

46 47
abstract class Instruction extends IdentifiedSettable {
  var results: Seq[InstResult] = Seq()
Kunshan Wang's avatar
Kunshan Wang committed
48
  
Kunshan Wang's avatar
Kunshan Wang committed
49
  override def toString = "[(%s) = %s %s]".format(this.results.map(_.repr).mkString(" "), this.repr, this.getClass.getSimpleName)
50
}
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

/// 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 uvm.ssavariables.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 uvm.ssavariables.CmpOptr.CmpOptr

object ConvOptr extends Enumeration {
  type ConvOptr = Value
73
  val TRUNC, ZEXT, SEXT, FPTRUNC, FPEXT, FPTOUI, FPTOSI, UITOFP, SITOFP, BITCAST, REFCAST, PTRCAST = Value
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
}

import uvm.ssavariables.ConvOptr.ConvOptr

object MemoryOrder extends Enumeration {
  type MemoryOrder = Value
  val NOT_ATOMIC, RELAXED, CONSUME, ACQUIRE, RELEASE, ACQ_REL, SEQ_CST = Value
}

import uvm.ssavariables.MemoryOrder.MemoryOrder

object AtomicRMWOptr extends Enumeration {
  type AtomicRMWOptr = Value
  val XCHG, ADD, SUB, AND, NAND, OR, XOR, MIN, MAX, UMIN, UMAX = Value
}

import uvm.ssavariables.AtomicRMWOptr.AtomicRMWOptr

/// Abstract instructions and traits

94 95 96 97 98 99
trait MaybeTerminator extends Instruction {
  def canTerminate: Boolean
}
trait Terminator extends MaybeTerminator {
  override def canTerminate: Boolean = true
}
100 101 102

trait OSRPoint extends Instruction

103 104 105 106 107 108 109 110 111 112 113 114 115
trait HasTypeList extends Instruction {
  var typeList: Seq[Type]
}

trait HasArgList extends Instruction {
  var argList: Seq[SSAVariable]
}

trait CallLike extends HasArgList {
  var sig: FuncSig
  var callee: SSAVariable
}

Kunshan Wang's avatar
Kunshan Wang committed
116 117 118
case class DestClause(val bb: BasicBlock, val args: Seq[SSAVariable])

case class ExcClause(val nor: DestClause, val exc: DestClause)
119

120
trait HasExcClause extends Instruction with MaybeTerminator {
121
  var excClause: Option[ExcClause]
122
  override def canTerminate: Boolean = excClause.isDefined
123 124 125
}

trait HasKeepAliveClause extends Instruction {
Kunshan Wang's avatar
Kunshan Wang committed
126
  var keepAlives: Seq[LocalVariable]
127 128 129 130 131 132
}

abstract class AbstractCall extends CallLike

abstract class AbstractRet extends Instruction

Kunshan Wang's avatar
Kunshan Wang committed
133
abstract class AbstractAlloc extends HasExcClause {
134 135 136 137 138 139
  def allocTy: Type
}

trait FixedAlloc extends AbstractAlloc

trait HybridAlloc extends AbstractAlloc {
Kunshan Wang's avatar
Kunshan Wang committed
140
  def lenTy: TypeInt
141 142 143 144 145 146 147
  def length: SSAVariable
}

abstract class HeapAlloc extends AbstractAlloc

abstract class StackAlloc extends AbstractAlloc

148 149 150 151
trait WorksWithPointer extends Instruction {
  var ptr: Boolean
}

152 153
abstract class AbstractTrap extends HasKeepAliveClause with OSRPoint {
  var retTys: Seq[Type]
154 155 156
}

abstract class CurStackAction
157
case class RetWith(var retTys: Seq[Type]) extends CurStackAction
158 159 160
case class KillOld() extends CurStackAction

abstract class NewStackAction
161
case class PassValues(var argTys: Seq[Type], var args: Seq[SSAVariable]) extends NewStackAction
162 163
case class ThrowExc(var exc: SSAVariable) extends NewStackAction

164 165 166 167 168
/**
 * Flags are used in common instructions.
 */
case class Flag(name: String)

169 170 171 172 173 174 175 176
/// Concrete instructions
case class InstBinOp(var op: BinOptr, var opndTy: Type, var op1: SSAVariable, var op2: SSAVariable,
                     var excClause: Option[ExcClause]) extends HasExcClause

case class InstCmp(var op: CmpOptr, var opndTy: Type, var op1: SSAVariable, var op2: SSAVariable) extends Instruction

case class InstConv(var op: ConvOptr, var fromTy: Type, var toTy: Type, var opnd: SSAVariable) extends Instruction

177
case class InstSelect(var condTy: Type, var opndTy: Type,
178 179
                      var cond: SSAVariable, var ifTrue: SSAVariable, var ifFalse: SSAVariable) extends Instruction

180
case class InstBranch(var dest: DestClause) extends Instruction with Terminator
181

182
case class InstBranch2(var cond: SSAVariable, var ifTrue: DestClause, var ifFalse: DestClause) extends Instruction with Terminator
183

Kunshan Wang's avatar
Kunshan Wang committed
184
case class InstSwitch(var opndTy: Type, var opnd: SSAVariable, var defDest: DestClause,
185
                      var cases: Seq[(SSAVariable, DestClause)]) extends Instruction with Terminator
186 187

case class InstCall(var sig: FuncSig, var callee: SSAVariable, var argList: Seq[SSAVariable],
188 189
                    var excClause: Option[ExcClause], var keepAlives: Seq[LocalVariable])
    extends AbstractCall with HasExcClause with HasKeepAliveClause with OSRPoint
190

191
case class InstTailCall(var sig: FuncSig, var callee: SSAVariable, var argList: Seq[SSAVariable]) extends AbstractCall with Terminator
192

193
case class InstRet(val funcVer: FuncVer, var retVals: Seq[SSAVariable]) extends AbstractRet with Terminator
194

195
case class InstThrow(var excVal: SSAVariable) extends Instruction with Terminator
196

Kunshan Wang's avatar
Kunshan Wang committed
197
case class InstExtractValue(var strTy: TypeStruct, var index: Int, var opnd: SSAVariable) extends Instruction
198

Kunshan Wang's avatar
Kunshan Wang committed
199
case class InstInsertValue(var strTy: TypeStruct, var index: Int, var opnd: SSAVariable, var newVal: SSAVariable) extends Instruction
200

Kunshan Wang's avatar
Kunshan Wang committed
201
case class InstExtractElement(var seqTy: AbstractSeqType, var indTy: TypeInt,
202 203
                              var opnd: SSAVariable, var index: SSAVariable) extends Instruction

Kunshan Wang's avatar
Kunshan Wang committed
204
case class InstInsertElement(var seqTy: AbstractSeqType, var indTy: TypeInt,
205 206
                             var opnd: SSAVariable, var index: SSAVariable, var newVal: SSAVariable) extends Instruction

Kunshan Wang's avatar
Kunshan Wang committed
207
case class InstShuffleVector(var vecTy: TypeVector, var maskTy: TypeVector,
208 209
                             var vec1: SSAVariable, var vec2: SSAVariable, var mask: SSAVariable) extends Instruction

Kunshan Wang's avatar
Kunshan Wang committed
210
case class InstNew(var allocTy: Type, var excClause: Option[ExcClause]) extends HeapAlloc with FixedAlloc
211

Kunshan Wang's avatar
Kunshan Wang committed
212
case class InstNewHybrid(var allocTy: TypeHybrid, var lenTy: TypeInt, var length: SSAVariable, var excClause: Option[ExcClause]) extends HeapAlloc with HybridAlloc
213

Kunshan Wang's avatar
Kunshan Wang committed
214
case class InstAlloca(var allocTy: Type, var excClause: Option[ExcClause]) extends StackAlloc with FixedAlloc
215

Kunshan Wang's avatar
Kunshan Wang committed
216
case class InstAllocaHybrid(var allocTy: TypeHybrid, var lenTy: TypeInt, var length: SSAVariable, var excClause: Option[ExcClause]) extends StackAlloc with HybridAlloc
217 218 219

case class InstGetIRef(var referentTy: Type, var opnd: SSAVariable) extends Instruction

Kunshan Wang's avatar
Kunshan Wang committed
220
case class InstGetFieldIRef(var ptr: Boolean, var referentTy: AbstractStructType, var index: Int, var opnd: SSAVariable) extends WorksWithPointer
221

222 223
case class InstGetElemIRef(var ptr: Boolean, var referentTy: AbstractSeqType, var indTy: TypeInt,
                           var opnd: SSAVariable, var index: SSAVariable) extends WorksWithPointer
224

225 226
case class InstShiftIRef(var ptr: Boolean, var referentTy: Type, var offTy: TypeInt,
                         var opnd: SSAVariable, var offset: SSAVariable) extends WorksWithPointer
227

228
case class InstGetVarPartIRef(var ptr: Boolean, var referentTy: TypeHybrid, var opnd: SSAVariable) extends WorksWithPointer
229

230
case class InstLoad(var ptr: Boolean, var ord: MemoryOrder, var referentTy: Type, var loc: SSAVariable, var excClause: Option[ExcClause]) extends WorksWithPointer with HasExcClause
231

232
case class InstStore(var ptr: Boolean, var ord: MemoryOrder, var referentTy: Type, var loc: SSAVariable, var newVal: SSAVariable, var excClause: Option[ExcClause]) extends WorksWithPointer with HasExcClause
233

234 235
case class InstCmpXchg(var ptr: Boolean, var weak: Boolean, var ordSucc: MemoryOrder, var ordFail: MemoryOrder, var referentTy: Type,
                       var loc: SSAVariable, var expected: SSAVariable, var desired: SSAVariable, var excClause: Option[ExcClause]) extends WorksWithPointer with HasExcClause
236

237 238
case class InstAtomicRMW(var ptr: Boolean, var ord: MemoryOrder, var op: AtomicRMWOptr,
                         var referentTy: Type, var loc: SSAVariable, var opnd: SSAVariable, var excClause: Option[ExcClause]) extends WorksWithPointer with HasExcClause
Kunshan Wang's avatar
Kunshan Wang committed
239 240

case class InstFence(var ord: MemoryOrder) extends Instruction
241

242 243
case class InstTrap(var retTys: Seq[Type], var excClause: Option[ExcClause], var keepAlives: Seq[LocalVariable])
  extends AbstractTrap with HasExcClause
244

245
case class InstWatchPoint(var wpID: Int, var retTys: Seq[Type],
Kunshan Wang's avatar
Kunshan Wang committed
246
                          var dis: DestClause, var ena: DestClause, var exc: Option[DestClause],
247 248 249
                          var keepAlives: Seq[LocalVariable]) extends AbstractTrap with Terminator

case class InstWPBranch(var wpID: Int, var dis: DestClause, var ena: DestClause) extends Instruction with Terminator
250

251 252 253
case class InstCCall(var callConv: Flag, var funcTy: Type, var sig: FuncSig, var callee: SSAVariable,
                     var argList: Seq[SSAVariable], var excClause: Option[ExcClause], var keepAlives: Seq[LocalVariable])
    extends CallLike with HasExcClause with HasKeepAliveClause with OSRPoint
254

255
case class InstNewThread(var stack: SSAVariable, var newStackAction: NewStackAction, var excClause: Option[ExcClause]) extends Instruction with HasExcClause
256 257

case class InstSwapStack(var swappee: SSAVariable, var curStackAction: CurStackAction, var newStackAction: NewStackAction,
258 259 260
                         var excClause: Option[ExcClause], var keepAlives: Seq[LocalVariable]) extends HasExcClause with HasKeepAliveClause with OSRPoint {
  override def canTerminate: Boolean = curStackAction == KillOld() || excClause.isDefined
}
261

262
case class InstCommInst(var inst: CommInst, var flagList: Seq[Flag], var typeList: Seq[Type], var funcSigList: Seq[FuncSig], var argList: Seq[SSAVariable],
Kunshan Wang's avatar
Kunshan Wang committed
263
                        var excClause: Option[ExcClause], var keepAlives: Seq[LocalVariable])
264 265 266
    extends HasTypeList with HasArgList with HasExcClause with HasKeepAliveClause {
  override def canTerminate: Boolean = excClause.isDefined || inst.isTerminator
}