Skip to content

Commit

Permalink
add exception messages
Browse files Browse the repository at this point in the history
  • Loading branch information
l-kent committed Feb 20, 2024
1 parent 4136686 commit f84c7f1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 43 deletions.
49 changes: 26 additions & 23 deletions src/main/scala/translating/GTIRBToIR.scala
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ class GTIRBToIR(mods: Seq[Module], parserMap: immutable.Map[String, Array[Array[

val target = l.rhs match {
case r: Register => r
case _ => throw Exception(" ")
case _ => throw Exception(s"unhandled indirect call $l does not assign a register to __PC")
}
val returnTarget = if (currentBlock.statements.hasNext(l)) {
// unidentified indirect call is mid-block
Expand All @@ -346,7 +346,7 @@ class GTIRBToIR(mods: Seq[Module], parserMap: immutable.Map[String, Array[Array[
afterBlock.replaceJump(currentBlock.jump)
afterBlock
}
case _ => throw Exception(" ")
case _ => throw Exception(s"unhandled indirect call $l is at end of block ${currentBlock.label} that ends in call ${currentBlock.jump}")
}
}
val indirectCall = IndirectCall(target, Some(returnTarget))
Expand Down Expand Up @@ -418,7 +418,7 @@ class GTIRBToIR(mods: Seq[Module], parserMap: immutable.Map[String, Array[Array[
IndirectCall(target, None)
} else if (proxySymbols.size > 1) {
// TODO requires further consideration once encountered
throw Exception(" ")
throw Exception(s"multiple symbols ${proxySymbols.map(_.name).mkString(", ")} associated with proxy block ${byteStringToString(edge.targetUuid)}, target of indirect call from block ${block.label}")
} else {
// indirect call to external procedure with name
val externalName = proxySymbols.head.name
Expand All @@ -440,7 +440,7 @@ class GTIRBToIR(mods: Seq[Module], parserMap: immutable.Map[String, Array[Array[
removePCAssign(block)
GoTo(mutable.Set(target))
} else {
throw Exception(" ")
throw Exception(s"edge from ${block.label} to ${byteStringToString(edge.targetUuid)} does not point to a known block or proxy block")
}
case EdgeLabel(false, true, Type_Branch, _) =>
// direct jump, either goto or tail call
Expand All @@ -460,7 +460,7 @@ class GTIRBToIR(mods: Seq[Module], parserMap: immutable.Map[String, Array[Array[
removePCAssign(block)
GoTo(mutable.Set(target))
} else {
throw Exception("")
throw Exception(s"edge from ${block.label} to ${byteStringToString(edge.targetUuid)} does not point to a known block")
}
case EdgeLabel(false, _, Type_Return, _) =>
// return statement, value of 'direct' is just whether DDisasm has resolved the return target
Expand All @@ -478,7 +478,7 @@ class GTIRBToIR(mods: Seq[Module], parserMap: immutable.Map[String, Array[Array[
val target = uuidToBlock(edge.targetUuid)
GoTo(mutable.Set(target))
} else {
throw Exception(" ")
throw Exception(s"edge from ${block.label} to ${byteStringToString(edge.targetUuid)} does not point to a known block")
}
case EdgeLabel(false, true, Type_Call, _) =>
// call that will not return according to DDisasm even though R30 may be set
Expand All @@ -488,7 +488,7 @@ class GTIRBToIR(mods: Seq[Module], parserMap: immutable.Map[String, Array[Array[
removePCAssign(block)
DirectCall(target, None)
} else {
throw Exception(" ")
throw Exception(s"edge from ${block.label} to ${byteStringToString(edge.targetUuid)} does not point to a known procedure entrance")
}

// case EdgeLabel(false, false, Type_Call, _) => probably what a blr instruction should be
Expand Down Expand Up @@ -571,24 +571,28 @@ class GTIRBToIR(mods: Seq[Module], parserMap: immutable.Map[String, Array[Array[
}

private def handleConditionalBranch(fallthrough: Edge, branch: Edge, block: Block, procedure: Procedure): GoTo = {
if (uuidToBlock.contains(branch.targetUuid) && uuidToBlock.contains(fallthrough.targetUuid)) {
val tempIf = block.statements.last match {
case i: TempIf => i
case _ => throw Exception(s"last statement of block ${block.label} is not an if statement")
}
// maybe need to actually examine the if statement's contents?

val trueBlock = newBlockCondition(block, uuidToBlock(branch.targetUuid), tempIf.cond)
val falseBlock = newBlockCondition(block, uuidToBlock(fallthrough.targetUuid), UnaryExpr(BoolNOT, tempIf.cond))
if (!uuidToBlock.contains(fallthrough.targetUuid)) {
throw Exception(s"block ${block.label} has fallthrough edge to ${byteStringToString(fallthrough.targetUuid)} that does not point to a known block")
}

val newBlocks = ArrayBuffer(trueBlock, falseBlock)
procedure.addBlocks(newBlocks)
block.statements.remove(tempIf)
if (!uuidToBlock.contains(branch.targetUuid)) {
throw Exception(s"block ${block.label} has branch edge to ${byteStringToString(fallthrough.targetUuid)} that does not point to a known block")
}

GoTo(newBlocks)
} else {
throw Exception(s"${branch} ${fallthrough} ")
val tempIf = block.statements.last match {
case i: TempIf => i
case _ => throw Exception(s"last statement of block ${block.label} is not an if statement")
}
// maybe need to actually examine the if statement's contents?

val trueBlock = newBlockCondition(block, uuidToBlock(branch.targetUuid), tempIf.cond)
val falseBlock = newBlockCondition(block, uuidToBlock(fallthrough.targetUuid), UnaryExpr(BoolNOT, tempIf.cond))

val newBlocks = ArrayBuffer(trueBlock, falseBlock)
procedure.addBlocks(newBlocks)
block.statements.remove(tempIf)

GoTo(newBlocks)
}

private def newBlockCondition(block: Block, target: Block, condition: Expr): Block = {
Expand All @@ -597,5 +601,4 @@ class GTIRBToIR(mods: Seq[Module], parserMap: immutable.Map[String, Array[Array[
Block(newLabel, None, ArrayBuffer(assume), GoTo(ArrayBuffer(target)))
}


}
36 changes: 16 additions & 20 deletions src/main/scala/translating/SemanticsLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ class SemanticsLoader(targetuuid: ByteString, parserMap: immutable.Map[String, A
val otherSize = parseInt(args(1)) * 8
val mysteryArg = parseInt(args(2))
if (size != otherSize) {
throw Exception(" ")
throw Exception(s"inconsistent size parameters in Mem.set.0: ${ctx.getText}")
}
if (mysteryArg != 0) {
Logger.debug("")
Logger.debug(s"mystery 3rd arg of Mem.set.0 has value $mysteryArg: ${ctx.getText}")
}

// LittleEndian is an assumption
Expand All @@ -106,7 +106,7 @@ class SemanticsLoader(targetuuid: ByteString, parserMap: immutable.Map[String, A
}

case _ =>
Logger.debug("")
Logger.debug(s"Unidentified function call $function: ${ctx.getText}")
None
}
}
Expand Down Expand Up @@ -245,10 +245,10 @@ class SemanticsLoader(targetuuid: ByteString, parserMap: immutable.Map[String, A
val otherSize = parseInt(args(1)) * 8
val mysteryArg = parseInt(args(2))
if (size != otherSize) {
throw Exception(s"${ctx.getText}")
throw Exception(s"inconsistent size parameters in Mem.read.0: ${ctx.getText}")
}
if (mysteryArg != 0) {
Logger.debug("")
Logger.debug(s"mystery 3rd arg of Mem.read.0 has value $mysteryArg: ${ctx.getText}")
}

if (index.isDefined) {
Expand Down Expand Up @@ -304,7 +304,7 @@ class SemanticsLoader(targetuuid: ByteString, parserMap: immutable.Map[String, A
val arg0 = visitExpr(args(0))
val arg1 = parseInt(args(1))
if (arg1 != newSize) {
throw Exception(" ")
Exception(s"inconsistent size parameters in ZeroExtend.0: ${ctx.getText}")
}
if (arg0.isDefined) {
Some(ZeroExtend(newSize - oldSize, arg0.get))
Expand All @@ -319,7 +319,7 @@ class SemanticsLoader(targetuuid: ByteString, parserMap: immutable.Map[String, A
val arg0 = visitExpr(args(0))
val arg1 = parseInt(args(1))
if (arg1 != newSize) {
throw Exception(" ")
Exception(s"inconsistent size parameters in SignExtend.0: ${ctx.getText}")
}
if (arg0.isDefined) {
Some(SignExtend(newSize - oldSize, arg0.get))
Expand All @@ -328,7 +328,7 @@ class SemanticsLoader(targetuuid: ByteString, parserMap: immutable.Map[String, A
}

case _ =>
Logger.debug(s"unidentified call to $function")
Logger.debug(s"unidentified call to $function: ${ctx.getText}")
None
}

Expand Down Expand Up @@ -422,7 +422,7 @@ class SemanticsLoader(targetuuid: ByteString, parserMap: immutable.Map[String, A
override def visitExprField(ctx: ExprFieldContext): LocalVar = {
val name = ctx.expr match {
case e: ExprVarContext => e.ID.getText
case _ => throw Exception(" ")
case _ => throw Exception(s"expected ${ctx.getText} to have an Expr_Var as first parameter")
}
val field = ctx.field.getText

Expand All @@ -432,7 +432,7 @@ class SemanticsLoader(targetuuid: ByteString, parserMap: immutable.Map[String, A
override def visitExprArray(ctx: ExprArrayContext): Register = {
val name = ctx.array match {
case e: ExprVarContext => e.ID.getText
case _ => throw Exception(" ")
case _ => throw Exception(s"expected ${ctx.getText} to have an Expr_Var as first parameter")
}
val index = parseInt(ctx.index)

Expand Down Expand Up @@ -469,14 +469,14 @@ class SemanticsLoader(targetuuid: ByteString, parserMap: immutable.Map[String, A
case "__BranchTaken" => None
case "BTypeNext" => None
case "BTypeCompatible" => None
case _ => throw Exception(s"could not identify variable '$name'")
case _ => throw Exception(s"could not identify variable '$name' in ${ctx.getText}")
}
}

override def visitLExprField(ctx: LExprFieldContext): LocalVar = {
val name = ctx.lexpr match {
case l: LExprVarContext => l.ID.getText
case _ => throw Exception(" ")
case _ => throw Exception(s"expected ${ctx.getText} to have an LExpr_Var as first parameter")
}
val field = ctx.field.getText

Expand All @@ -486,7 +486,7 @@ class SemanticsLoader(targetuuid: ByteString, parserMap: immutable.Map[String, A
override def visitLExprArray(ctx: LExprArrayContext): Register = {
val name = ctx.lexpr match {
case l: LExprVarContext => l.ID.getText
case _ => throw Exception(" ")
case _ => throw Exception(s"expected ${ctx.getText} to have an LExpr_Var as first parameter")
}
val index = parseInt(ctx.index)

Expand All @@ -495,21 +495,17 @@ class SemanticsLoader(targetuuid: ByteString, parserMap: immutable.Map[String, A

private def resolveFieldExpr(name: String, field: String): LocalVar = {
name match {
case "PSTATE" =>
if (field == "V" || field == "C" || field == "Z" || field == "N") {
case "PSTATE" if field == "V" || field == "C" || field == "Z" || field == "N" =>
LocalVar(field + "F", BitVecType(1))
} else {
throw Exception(" ")
}
case _ => throw Exception(" ")
case _ => throw Exception(s"unidentified Expr_Field ($name, $field)")
}
}

private def resolveArrayExpr(name: String, index: Int): Register = {
name match {
case "_R" => Register(s"R$index", BitVecType(64))
case "_Z" => Register(s"V$index", BitVecType(128))
case _ => throw Exception(" ")
case _ => throw Exception(s"unidentified Expr_Array ($name, $index)")
}
}
}

0 comments on commit f84c7f1

Please sign in to comment.