Unverified Commit 44ec840b by van den Bosch Committed by GitHub

added Display trait for Expressions and Operands (#341)

parent 78575b7b
......@@ -156,7 +156,7 @@ impl<'a> PointerInference<'a> {
}
if !self.computation.has_stabilized() {
let worklist_size = self.computation.get_worklist().len();
let _ = self.log_info(format!(
self.log_info(format!(
"Fixpoint did not stabilize. Remaining worklist size: {}",
worklist_size,
));
......
......@@ -54,7 +54,7 @@ fn compute_block_end_state(project: &Project, block: &Term<Blk>) -> State {
let _ = state.handle_store(address, value, &project.runtime_memory_image);
}
Def::Assign { var, value } => {
let _ = state.handle_register_assign(var, value);
state.handle_register_assign(var, value);
}
Def::Load { var, address } => {
let _ = state.handle_load(var, address, &project.runtime_memory_image);
......
......@@ -59,7 +59,7 @@ fn get_umask_permission_arg(
let _ = state.handle_store(address, value, &project.runtime_memory_image);
}
Def::Assign { var, value } => {
let _ = state.handle_register_assign(var, value);
state.handle_register_assign(var, value);
}
Def::Load { var, address } => {
let _ = state.handle_load(var, address, &project.runtime_memory_image);
......
use super::*;
use crate::utils::log::LogMessage;
use std::collections::HashSet;
use std::{collections::HashSet, fmt};
/// A basic block is a sequence of `Def` instructions followed by up to two `Jmp` instructions.
///
......@@ -188,6 +188,18 @@ impl Term<Blk> {
}
}
impl fmt::Display for Blk {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for def in self.defs.iter() {
writeln!(f, "{}: {}", def.tid, def.term)?;
}
for jmp in self.jmps.iter() {
writeln!(f, "{}: {}", jmp.tid, jmp.term)?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
......
use std::fmt;
use super::{CastOpType, Expression, Variable};
use crate::prelude::*;
......@@ -86,6 +88,16 @@ impl Term<Def> {
}
}
impl fmt::Display for Def {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Def::Load { var, address } => write!(f, "{} := Load from {}", var, address),
Def::Store { address, value } => write!(f, "Store at {} := {}", address, value),
Def::Assign { var, value } => write!(f, "{} = {}", var, value),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
......
use std::collections::HashMap;
use std::fmt::{self, Debug};
use super::Variable;
use super::{ByteSize, Def};
......@@ -431,5 +432,81 @@ impl Expression {
}
}
impl fmt::Display for Expression {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expression::Var(var) => write!(f, "{}", var),
Expression::Const(c) => {
write!(f, "0x{:016x}:i{}", c, c.bytesize().as_bit_length())
}
Expression::BinOp { op, lhs, rhs } => match op {
BinOpType::IntMult
| BinOpType::IntDiv
| BinOpType::IntRem
| BinOpType::FloatMult
| BinOpType::FloatDiv => write!(f, "{} {} {}", lhs, op, rhs),
_ => write!(f, "({} {} {})", lhs, op, rhs),
},
Expression::UnOp { op, arg } => write!(f, "{}({})", op, arg),
Expression::Cast { op, size: _, arg } => write!(f, "{}({})", op, arg),
Expression::Unknown {
description,
size: _,
} => write!(f, "{}", description),
Expression::Subpiece {
low_byte,
size,
arg,
} => {
if let (Ok(start), Ok(end)) = (u32::try_from(low_byte.0), u32::try_from(size.0)) {
write!(f, "({})[{}-{}]", arg, start, end)
} else {
write!(f, "{}[]", arg)
}
}
}
}
}
impl fmt::Display for BinOpType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
BinOpType::IntEqual => write!(f, "=="),
BinOpType::IntNotEqual => write!(f, "!="),
BinOpType::IntLess => write!(f, "<"),
BinOpType::IntSLess => write!(f, "<"),
BinOpType::IntLessEqual => write!(f, "<="),
BinOpType::IntSLessEqual => write!(f, "<="),
BinOpType::IntAdd => write!(f, "+"),
BinOpType::IntSub => write!(f, "-"),
BinOpType::IntXOr => write!(f, "^"),
BinOpType::IntAnd => write!(f, "&"),
BinOpType::IntOr => write!(f, "|"),
BinOpType::IntLeft => write!(f, "<<"),
BinOpType::IntRight => write!(f, ">>"),
BinOpType::IntMult => write!(f, "*"),
BinOpType::IntDiv => write!(f, "/"),
BinOpType::IntRem => write!(f, "%"),
BinOpType::BoolAnd => write!(f, "&&"),
BinOpType::BoolOr => write!(f, "||"),
_ => write!(f, "{:?}", self),
}
}
}
impl fmt::Display for UnOpType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
UnOpType::BoolNegate => write!(f, "¬"),
_ => write!(f, "{:?}", self),
}
}
}
impl fmt::Display for CastOpType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}
#[cfg(test)]
mod tests;
......@@ -539,3 +539,27 @@ fn processing_sub_registers() {
expr.cast_sub_registers_to_base_register_subpieces(output, &register_map, peeked);
assert_eq!(expr, setup.int_sub_subpiece_expr);
}
#[test]
fn display() {
let expr = Expression::const_from_i32(2);
let mul = Expression::BinOp {
op: BinOpType::IntMult,
lhs: Box::new(Expression::Var(Variable::mock("RAX", 8))),
rhs: Box::new(Expression::Var(Variable::mock("RBP", 8))),
};
let expr = expr.plus(mul);
let expr = Expression::UnOp {
op: UnOpType::IntNegate,
arg: Box::new(expr),
};
let expr = expr
.cast(CastOpType::IntSExt)
.un_op(UnOpType::FloatCeil)
.subpiece(ByteSize(0), ByteSize(20));
assert_eq!(
"(FloatCeil(IntSExt(IntNegate((0x2:i32 + RAX:64 * RBP:64)))))[0-20]",
format!("{}", expr)
);
}
use std::fmt;
use super::Expression;
use crate::prelude::*;
......@@ -58,3 +60,35 @@ pub enum Jmp {
return_: Option<Tid>,
},
}
impl fmt::Display for Jmp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Jmp::Branch(tid) => write!(f, "Jump to {}", tid),
Jmp::BranchInd(expr) => write!(f, "Jump to {}", expr),
Jmp::CBranch { target, condition } => write!(f, "If {} jump to {}", condition, target),
Jmp::Call { target, return_ } => write!(
f,
"call {} ret {}",
target,
return_.as_ref().unwrap_or(&Tid::new("?"))
),
Jmp::CallInd { target, return_ } => write!(
f,
"call {} ret {}",
target,
return_.as_ref().unwrap_or(&Tid::new("?"))
),
Jmp::Return(expr) => write!(f, "ret {}", expr),
Jmp::CallOther {
description,
return_,
} => write!(
f,
"call {} ret {}",
description,
return_.as_ref().unwrap_or(&Tid::new("?"))
),
}
}
}
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