Commit 7d8a67d4 by Enkelmann Committed by Enkelmann

Add some missing doc strings

parent babf80f5
...@@ -203,6 +203,8 @@ impl Expression { ...@@ -203,6 +203,8 @@ impl Expression {
} }
} }
/// Compute the bitsize of the value that the expression computes.
/// Return zero for `Store` expressions.
pub fn bitsize(&self) -> BitSize { pub fn bitsize(&self) -> BitSize {
use Expression::*; use Expression::*;
match self { match self {
...@@ -234,6 +236,9 @@ impl Expression { ...@@ -234,6 +236,9 @@ impl Expression {
} }
impl From<Expression> for IrExpression { impl From<Expression> for IrExpression {
/// Convert a BAP IR expression to an internal IR expression.
/// Panics on expressions that are not expressions in the internal IR.
/// Replaces `IfThenElse` expressions with `Unknown` expressions (thus losing some information).
fn from(expr: Expression) -> IrExpression { fn from(expr: Expression) -> IrExpression {
use Expression::*; use Expression::*;
match expr { match expr {
...@@ -373,6 +378,7 @@ pub enum BinOpType { ...@@ -373,6 +378,7 @@ pub enum BinOpType {
} }
impl From<BinOpType> for IrBinOpType { impl From<BinOpType> for IrBinOpType {
/// Translate binary operation types.
fn from(op: BinOpType) -> IrBinOpType { fn from(op: BinOpType) -> IrBinOpType {
use BinOpType::*; use BinOpType::*;
use IrBinOpType::*; use IrBinOpType::*;
...@@ -407,6 +413,7 @@ pub enum UnOpType { ...@@ -407,6 +413,7 @@ pub enum UnOpType {
} }
impl From<UnOpType> for IrUnOpType { impl From<UnOpType> for IrUnOpType {
/// Translate unary operation types.
fn from(op: UnOpType) -> IrUnOpType { fn from(op: UnOpType) -> IrUnOpType {
use UnOpType::*; use UnOpType::*;
match op { match op {
......
...@@ -54,6 +54,12 @@ pub struct Def { ...@@ -54,6 +54,12 @@ pub struct Def {
} }
impl Def { impl Def {
/// Convert one `Def` into one or more `Def`s of the internal IR.
///
/// `Load` expressions get transferred to their own `Def`,
/// since they are not representable as expressions in the internal IR.
/// `IfThenElse` expressions are translated to `Unknown` expressions in the process,
/// thus resulting in possible information loss.
fn into_ir_defs(self) -> Vec<IrDef> { fn into_ir_defs(self) -> Vec<IrDef> {
match self.rhs { match self.rhs {
Expression::Load { address, .. } => { Expression::Load { address, .. } => {
...@@ -141,6 +147,8 @@ impl Def { ...@@ -141,6 +147,8 @@ impl Def {
} }
} }
/// Translate a `Load` into its internal IR representation.
/// Panics if right hand side expression is not a `Load`.
fn into_ir_load(self) -> IrDef { fn into_ir_load(self) -> IrDef {
if let Expression::Load { address, .. } = self.rhs { if let Expression::Load { address, .. } = self.rhs {
IrDef::Load { IrDef::Load {
...@@ -168,6 +176,7 @@ pub enum JmpKind { ...@@ -168,6 +176,7 @@ pub enum JmpKind {
} }
impl From<Jmp> for IrJmp { impl From<Jmp> for IrJmp {
/// Translate jump types.
fn from(jmp: Jmp) -> IrJmp { fn from(jmp: Jmp) -> IrJmp {
match jmp.kind { match jmp.kind {
JmpKind::Goto(Label::Direct(tid)) => IrJmp::Branch(tid), JmpKind::Goto(Label::Direct(tid)) => IrJmp::Branch(tid),
...@@ -218,6 +227,7 @@ pub struct Blk { ...@@ -218,6 +227,7 @@ pub struct Blk {
} }
impl From<Blk> for IrBlk { impl From<Blk> for IrBlk {
/// Translates block types.
fn from(blk: Blk) -> IrBlk { fn from(blk: Blk) -> IrBlk {
let mut ir_def_terms = Vec::new(); let mut ir_def_terms = Vec::new();
for def_term in blk.defs { for def_term in blk.defs {
...@@ -278,6 +288,7 @@ pub struct Sub { ...@@ -278,6 +288,7 @@ pub struct Sub {
} }
impl From<Sub> for IrSub { impl From<Sub> for IrSub {
/// Translate `Sub` types.
fn from(sub: Sub) -> IrSub { fn from(sub: Sub) -> IrSub {
let blocks = sub let blocks = sub
.blocks .blocks
...@@ -302,6 +313,7 @@ pub struct Program { ...@@ -302,6 +313,7 @@ pub struct Program {
} }
impl From<Program> for IrProgram { impl From<Program> for IrProgram {
/// Translate program types.
fn from(program: Program) -> IrProgram { fn from(program: Program) -> IrProgram {
let subs = program let subs = program
.subs .subs
...@@ -370,6 +382,7 @@ impl Project { ...@@ -370,6 +382,7 @@ impl Project {
} }
impl From<Project> for IrProject { impl From<Project> for IrProject {
/// Translate project types.
fn from(project: Project) -> IrProject { fn from(project: Project) -> IrProject {
let program = Term { let program = Term {
tid: project.program.tid, tid: project.program.tid,
...@@ -424,6 +437,7 @@ impl ArgIntent { ...@@ -424,6 +437,7 @@ impl ArgIntent {
} }
impl From<Arg> for IrArg { impl From<Arg> for IrArg {
/// Translate extern symbol argument types.
fn from(arg: Arg) -> IrArg { fn from(arg: Arg) -> IrArg {
match arg.location { match arg.location {
Expression::Var(var) => IrArg::Register(var.into()), Expression::Var(var) => IrArg::Register(var.into()),
...@@ -457,6 +471,11 @@ impl From<Arg> for IrArg { ...@@ -457,6 +471,11 @@ impl From<Arg> for IrArg {
} }
} }
/// Substitute each `Load` subexpression with a temporary variable
/// and a `Def` containing the `Load` into said variable.
///
/// The function is recursive and the counter is needed to keep track how many `Load` expressions
/// have already been extracted (which is used to generate unique names for the temporary variables).
fn extract_loads_from_expression(expr: Expression, counter: u64) -> (Vec<Def>, Expression, u64) { fn extract_loads_from_expression(expr: Expression, counter: u64) -> (Vec<Def>, Expression, u64) {
use Expression::*; use Expression::*;
match expr { match expr {
...@@ -580,6 +599,8 @@ fn extract_loads_from_expression(expr: Expression, counter: u64) -> (Vec<Def>, E ...@@ -580,6 +599,8 @@ fn extract_loads_from_expression(expr: Expression, counter: u64) -> (Vec<Def>, E
} }
} }
/// Substitutes each `Load` expression in the target or conditition fields of a jump
/// with a temporary variable and a `Def` containing the `Load` into said variable.
fn extract_loads_from_jump(mut jmp: Jmp) -> (Jmp, Vec<Def>) { fn extract_loads_from_jump(mut jmp: Jmp) -> (Jmp, Vec<Def>) {
let mut counter = 0; let mut counter = 0;
let mut defs = Vec::new(); let mut defs = Vec::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