Skip to content

Commit

Permalink
clean up, mostly around organising files so filenames and packages co…
Browse files Browse the repository at this point in the history
…rrespond
  • Loading branch information
l-kent committed Feb 27, 2024
1 parent acb0281 commit b3360b5
Show file tree
Hide file tree
Showing 21 changed files with 251 additions and 296 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/main/scala/analysis/IrreducibleLoops.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package analysis

import ir.{CFGPosition, Command, IntraProcIRCursor, Program, Procedure, Block, GoTo, IRWalk}
import intrusivelist.IntrusiveList
import util.intrusive_list.IntrusiveList
import util.Logger

import scala.collection.mutable
Expand Down
14 changes: 5 additions & 9 deletions src/main/scala/analysis/SSAForm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import scala.collection.mutable
case class SSAForm() {

val varMaxTracker = new mutable.HashMap[String, Int]()
val blockBasedMappings = new mutable.HashMap[(Block, String), mutable.Set[Int]]().withDefault(_ => mutable.Set())
val context = new mutable.HashMap[(Procedure, String), mutable.Set[Int]]().withDefault(_ => mutable.Set())
private val blockBasedMappings = new mutable.HashMap[(Block, String), mutable.Set[Int]]().withDefault(_ => mutable.Set())
private val context = new mutable.HashMap[(Procedure, String), mutable.Set[Int]]().withDefault(_ => mutable.Set())
def getMax(varName: String): Int =
val ret = varMaxTracker.getOrElse(varName, 0)
varMaxTracker(varName) = ret + 1
Expand Down Expand Up @@ -75,24 +75,20 @@ case class SSAForm() {
}
}
currentBlock.jump match {
case directCall: DirectCall => {
case directCall: DirectCall =>
// TODO: transfers the whole context but it could be using ANR and RNA to transfer only the relevant context
varMaxTracker.keys.foreach(varr => {
//context((directCall.target, varr)) = context((directCall.target, varr)) ++ blockBasedMappings(block, varr)
context.getOrElseUpdate((directCall.target, varr), mutable.Set()) ++= blockBasedMappings((currentBlock, varr))
})
}
case indirectCall: IndirectCall => {
case indirectCall: IndirectCall =>
transformVariables(indirectCall.target.variables, currentBlock, proc)
}
case goTo: GoTo => {
case goTo: GoTo =>
goTo.targets.foreach(b => {
varMaxTracker.keys.foreach(varr => {
blockBasedMappings((b, varr)) = blockBasedMappings(b, varr) ++ blockBasedMappings(currentBlock, varr)
})
})
}
case _ => {}
}
// Push unvisited successors onto the stack
stack.pushAll(currentBlock.nextBlocks)
Expand Down
132 changes: 0 additions & 132 deletions src/main/scala/ir/IR_Dsl.scala

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/scala/ir/Program.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import scala.collection.mutable.ArrayBuffer
import scala.collection.{IterableOnceExtensionMethods, View, immutable, mutable}
import boogie.*
import analysis.BitVectorEval
import intrusivelist.{IntrusiveList, IntrusiveListElement}
import util.intrusive_list.*

class Program(var procedures: ArrayBuffer[Procedure], var mainProcedure: Procedure,
var initialMemory: ArrayBuffer[MemorySection],
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/ir/Statement.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package ir
import intrusivelist.IntrusiveListElement
import util.intrusive_list.IntrusiveListElement

import collection.mutable

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/ir/Visitor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ir

import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable
import intrusivelist.IntrusiveList
import util.intrusive_list.IntrusiveList

abstract class Visitor {

Expand Down
134 changes: 134 additions & 0 deletions src/main/scala/ir/dsl/DSL.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package ir.dsl
import ir.*
import scala.collection.mutable
import scala.collection.immutable.*

val R0: Register = Register("R0", BitVecType(64))
val R1: Register = Register("R1", BitVecType(64))
val R2: Register = Register("R2", BitVecType(64))
val R3: Register = Register("R3", BitVecType(64))
val R4: Register = Register("R4", BitVecType(64))
val R5: Register = Register("R5", BitVecType(64))
val R6: Register = Register("R6", BitVecType(64))
val R7: Register = Register("R7", BitVecType(64))
val R29: Register = Register("R29", BitVecType(64))
val R30: Register = Register("R30", BitVecType(64))
val R31: Register = Register("R31", BitVecType(64))
val ret: EventuallyIndirectCall = EventuallyIndirectCall(Register("R30", BitVecType(64)), None)


def bv32(i: Int): BitVecLiteral = BitVecLiteral(i, 32)

def bv64(i: Int): BitVecLiteral = BitVecLiteral(i, 64)

def bv8(i: Int): BitVecLiteral = BitVecLiteral(i, 8)

def bv16(i: Int): BitVecLiteral = BitVecLiteral(i, 16)

case class DelayNameResolve(ident: String) {
def resolveProc(prog: Program): Option[Procedure] = prog.collectFirst {
case b: Procedure if b.name == ident => b
}

def resolveBlock(prog: Program): Option[Block] = prog.collectFirst {
case b: Block if b.label == ident => b
}
}

trait EventuallyJump {
def resolve(p: Program): Jump
}

case class EventuallyIndirectCall(target: Variable, fallthrough: Option[DelayNameResolve]) extends EventuallyJump {
override def resolve(p: Program): IndirectCall = {
IndirectCall(target, fallthrough.flatMap(_.resolveBlock(p)))
}
}

case class EventuallyCall(target: DelayNameResolve, fallthrough: Option[DelayNameResolve]) extends EventuallyJump {
override def resolve(p: Program): DirectCall = {
val t = target.resolveProc(p).get
val ft = fallthrough.flatMap(_.resolveBlock(p))
DirectCall(t, ft)
}
}

case class EventuallyGoto(targets: List[DelayNameResolve]) extends EventuallyJump {
override def resolve(p: Program): GoTo = {
val tgs = targets.flatMap(tn => tn.resolveBlock(p))
GoTo(tgs)
}
}

def goto(): EventuallyGoto = EventuallyGoto(List.empty)

def goto(targets: String*): EventuallyGoto = {
EventuallyGoto(targets.map(p => DelayNameResolve(p)).toList)
}

def goto(targets: List[String]): EventuallyGoto = {
EventuallyGoto(targets.map(p => DelayNameResolve(p)))
}

def indirectCall(tgt: String, fallthrough: Option[String]): EventuallyCall = EventuallyCall(DelayNameResolve(tgt), fallthrough.map(x => DelayNameResolve(x)))

def call(tgt: String, fallthrough: Option[String]): EventuallyCall = EventuallyCall(DelayNameResolve(tgt), fallthrough.map(x => DelayNameResolve(x)))

def call(tgt: Variable, fallthrough: Option[String]): EventuallyIndirectCall = EventuallyIndirectCall(tgt, fallthrough.map(x => DelayNameResolve(x)))
// def directcall(tgt: String) = EventuallyCall(DelayNameResolve(tgt), None)


case class EventuallyBlock(label: String, sl: Seq[Statement], j: EventuallyJump) {
val tempBlock: Block = Block(label, None, sl, GoTo(List.empty))

def resolve(prog: Program): Block = {
tempBlock.replaceJump(j.resolve(prog))
tempBlock
}
}

def block(label: String, sl: (Statement | EventuallyJump)*): EventuallyBlock = {
val statements = sl.collect {
case s: Statement => s
}
val jump = sl.collectFirst {
case j: EventuallyJump => j
}
EventuallyBlock(label, statements, jump.get)
}

case class EventuallyProcedure(label: String, blocks: Seq[EventuallyBlock]) {
val _blocks: Seq[Block] = blocks.map(_.tempBlock)
val tempProc: Procedure = Procedure(label, None, _blocks.headOption, None, _blocks)
val jumps: Map[Block, EventuallyJump] = blocks.map(b => b.tempBlock -> b.j).toMap

def resolve(prog: Program): Procedure = {
jumps.map((b, j) => b.replaceJump(j.resolve(prog)))
tempProc
}


}

def proc(label: String, blocks: EventuallyBlock*): EventuallyProcedure = {
EventuallyProcedure(label, blocks)
}


def mem: Memory = Memory("mem", 64, 8)

def stack: Memory = Memory("stack", 64, 8)


def prog(procedures: EventuallyProcedure*): Program = {
require(procedures.nonEmpty)

val initialMemory = mutable.ArrayBuffer.empty[MemorySection]
val readOnlyMemory = mutable.ArrayBuffer.empty[MemorySection]
val p = Program(mutable.ArrayBuffer.from(procedures.map(_.tempProc)), procedures.map(_.tempProc).head, initialMemory, readOnlyMemory)

procedures.foreach(_.resolve(p))
p
}


2 changes: 1 addition & 1 deletion src/main/scala/translating/BAPToIR.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import specification.*
import scala.collection.mutable
import scala.collection.mutable.Map
import scala.collection.mutable.ArrayBuffer
import intrusivelist.IntrusiveList
import util.intrusive_list.*

class BAPToIR(var program: BAPProgram, mainAddress: Int) {

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/translating/GTIRBToIR.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import java.nio.charset.*
import scala.util.boundary
import boundary.break
import java.nio.ByteBuffer
import intrusivelist.{IntrusiveList, IntrusiveListElement}
import util.intrusive_list.*
import util.Logger

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/util/PerformanceTimer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import scala.collection
case class PerformanceTimer(timerName: String = "") {
private var lastCheckpoint: Long = System.currentTimeMillis()
private var end: Long = 0
private var checkpoints: mutable.Map[String, Long] = mutable.HashMap()
private val checkpoints: mutable.Map[String, Long] = mutable.HashMap()

def checkPoint(name: String): Long = {
val delta = elapsed()
Expand All @@ -15,7 +15,7 @@ case class PerformanceTimer(timerName: String = "") {
Logger.info(s"PerformanceTimer $timerName [$name]: ${delta}ms")
delta
}
def elapsed() : Long = {
private def elapsed() : Long = {
System.currentTimeMillis() - lastCheckpoint
}

Expand Down
Loading

0 comments on commit b3360b5

Please sign in to comment.