Commit b70f9910 by Enkelmann Committed by Enkelmann

implement conversion for blocks and subs

parent c11a21e9
......@@ -46,3 +46,9 @@ pub struct Blk {
pub defs: Vec<Term<Def>>,
pub jmps: Vec<Term<Jmp>>,
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Hash, Clone)]
pub struct Sub {
pub name: String,
pub blocks: Vec<Term<Blk>>,
}
use super::{Expression, Variable};
use crate::intermediate_representation::Blk as IrBlk;
use crate::intermediate_representation::Def as IrDef;
use crate::intermediate_representation::Expression as IrExpression;
use crate::intermediate_representation::Jmp as IrJmp;
use crate::intermediate_representation::Sub as IrSub;
use crate::prelude::*;
use crate::term::{Term, Tid};
......@@ -151,6 +153,28 @@ pub struct Blk {
pub jmps: Vec<Term<Jmp>>,
}
impl From<Blk> for IrBlk {
fn from(blk: Blk) -> IrBlk {
let defs: Vec<Term<IrDef>> = blk
.defs
.into_iter()
.map(|def_term| Term {
tid: def_term.tid,
term: def_term.term.into(),
})
.collect();
let jmps: Vec<Term<IrJmp>> = blk
.jmps
.into_iter()
.map(|jmp_term| Term {
tid: jmp_term.tid,
term: jmp_term.term.into(),
})
.collect();
IrBlk { defs, jmps }
}
}
// TODO: We need a unit test for stack parameter (that use location instead of var)!
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Hash, Clone)]
pub struct Arg {
......@@ -172,6 +196,23 @@ pub struct Sub {
pub blocks: Vec<Term<Blk>>,
}
impl From<Sub> for IrSub {
fn from(sub: Sub) -> IrSub {
let blocks = sub
.blocks
.into_iter()
.map(|block_term| Term {
tid: block_term.tid,
term: block_term.term.into(),
})
.collect();
IrSub {
name: sub.name,
blocks,
}
}
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Hash, Clone)]
pub struct ExternSymbol {
pub tid: Tid,
......
use crate::bil::*;
use crate::intermediate_representation::Blk as IrBlk;
use crate::intermediate_representation::Def as IrDef;
use crate::intermediate_representation::Expression as IrExpression;
use crate::intermediate_representation::Jmp as IrJmp;
use crate::intermediate_representation::Sub as IrSub;
use serde::{Deserialize, Serialize};
pub mod symbol;
......@@ -178,12 +180,67 @@ pub struct Blk {
pub jmps: Vec<Term<Jmp>>,
}
impl From<Blk> for IrBlk {
fn from(blk: Blk) -> IrBlk {
let mut ir_def_terms = Vec::new();
for def_term in blk.defs {
let ir_defs = def_term.term.to_ir_defs();
assert!(!ir_defs.is_empty());
if ir_defs.len() == 1 {
ir_def_terms.push(Term {
tid: def_term.tid,
term: ir_defs.into_iter().next().unwrap(),
});
} else {
for (counter, ir_def) in ir_defs.into_iter().enumerate() {
ir_def_terms.push(Term {
tid: Tid {
id: format!("{}_{}", def_term.tid.id, counter),
address: def_term.tid.address.clone(),
},
term: ir_def,
});
}
}
}
let ir_jmp_terms = blk
.jmps
.into_iter()
.map(|jmp_term| Term {
tid: jmp_term.tid,
term: jmp_term.term.into(),
})
.collect();
IrBlk {
defs: ir_def_terms,
jmps: ir_jmp_terms,
}
}
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Hash, Clone)]
pub struct Sub {
pub name: String,
pub blocks: Vec<Term<Blk>>,
}
impl From<Sub> for IrSub {
fn from(sub: Sub) -> IrSub {
let blocks = sub
.blocks
.into_iter()
.map(|block_term| Term {
tid: block_term.tid,
term: block_term.term.into(),
})
.collect();
IrSub {
name: sub.name,
blocks,
}
}
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Hash, Clone)]
pub struct Program {
pub subs: Vec<Term<Sub>>,
......
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