Skip to content

Commit f7441b3

Browse files
merge updated changes
2 parents 2adb31a + 6ad45f0 commit f7441b3

File tree

9 files changed

+273
-186
lines changed

9 files changed

+273
-186
lines changed

src/diagnostic.rs

Lines changed: 4 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ impl DiagnosticHandler {
166166

167167
#[cfg(test)]
168168
mod tests {
169-
use crate::typecheck::*;
169+
use crate::{serialize::tests::create_calyx_go_down_transaction, typecheck::*};
170170

171171
use super::*;
172172
use baa::BitVecValue;
@@ -197,87 +197,9 @@ mod tests {
197197
}
198198

199199
#[test]
200-
fn serialize_calyx_go_done_transaction() {
201-
// Manually create the expected result of parsing `calyx_go_done`.
202-
// Note that the order in which things are created will be different in the parser.
203-
204-
// 1) declare symbols
205-
let mut symbols = SymbolTable::default();
206-
let mut handler: DiagnosticHandler = DiagnosticHandler::new();
207-
let ii = symbols.add_without_parent("ii".to_string(), Type::BitVec(32));
208-
let oo = symbols.add_without_parent("oo".to_string(), Type::BitVec(32));
209-
assert_eq!(symbols["oo"], symbols[oo]);
210-
211-
// declare Calyx struct
212-
let dut_struct = symbols.add_struct(
213-
"Calyx".to_string(),
214-
vec![
215-
Field::new("ii".to_string(), Dir::In, Type::BitVec(32)),
216-
Field::new("go".to_string(), Dir::In, Type::BitVec(1)),
217-
Field::new("done".to_string(), Dir::Out, Type::BitVec(1)),
218-
Field::new("oo".to_string(), Dir::Out, Type::BitVec(32)),
219-
],
220-
);
221-
222-
let dut = symbols.add_without_parent("dut".to_string(), Type::Struct(dut_struct));
223-
let dut_ii = symbols.add_with_parent("ii".to_string(), dut);
224-
let dut_go = symbols.add_with_parent("go".to_string(), dut);
225-
let dut_done = symbols.add_with_parent("done".to_string(), dut);
226-
let dut_oo = symbols.add_with_parent("oo".to_string(), dut);
227-
assert_eq!(symbols["dut.oo"], symbols[dut_oo]);
228-
assert_eq!(symbols["oo"], symbols[oo]);
229-
230-
// create fileid and read file
231-
let input =
232-
std::fs::read_to_string("tests/calyx_go_done_struct.prot").expect("failed to load");
233-
let calyx_fileid = handler.add_file("calyx_go_done.prot".to_string(), input);
234-
235-
// 2) create transaction
236-
let mut calyx_go_done = Transaction::new("calyx_go_done".to_string());
237-
calyx_go_done.args = vec![Arg::new(ii, Dir::In), Arg::new(oo, Dir::Out)];
238-
calyx_go_done.type_args = vec![dut];
239-
240-
// 3) create expressions
241-
let ii_expr = calyx_go_done.e(Expr::Sym(ii));
242-
calyx_go_done.add_expr_loc(ii_expr, 153, 155, calyx_fileid);
243-
let dut_oo_expr = calyx_go_done.e(Expr::Sym(dut_oo));
244-
calyx_go_done.add_expr_loc(dut_oo_expr, 260, 266, calyx_fileid);
245-
let one_expr = calyx_go_done.e(Expr::Const(BitVecValue::from_u64(1, 1)));
246-
calyx_go_done.add_expr_loc(one_expr, 170, 171, calyx_fileid);
247-
let zero_expr = calyx_go_done.e(Expr::Const(BitVecValue::from_u64(0, 1)));
248-
calyx_go_done.add_expr_loc(zero_expr, 232, 233, calyx_fileid);
249-
let dut_done_expr = calyx_go_done.e(Expr::Sym(dut_done));
250-
calyx_go_done.add_expr_loc(dut_done_expr, 184, 192, calyx_fileid);
251-
let cond_expr = calyx_go_done.e(Expr::Binary(BinOp::Equal, dut_done_expr, one_expr));
252-
calyx_go_done.add_expr_loc(cond_expr, 183, 198, calyx_fileid);
253-
let not_expr = calyx_go_done.e(Expr::Unary(UnaryOp::Not, cond_expr));
254-
calyx_go_done.add_expr_loc(not_expr, 182, 198, calyx_fileid);
255-
256-
// 4) create statements
257-
let while_body = vec![calyx_go_done.s(Stmt::Step)];
258-
let wbody = calyx_go_done.s(Stmt::Block(while_body));
259-
260-
let dut_ii_assign = calyx_go_done.s(Stmt::Assign(dut_ii, ii_expr));
261-
calyx_go_done.add_stmt_loc(dut_ii_assign, 143, 157, calyx_fileid);
262-
let dut_go_assign = calyx_go_done.s(Stmt::Assign(dut_go, one_expr));
263-
calyx_go_done.add_stmt_loc(dut_go_assign, 160, 172, calyx_fileid);
264-
let dut_while = calyx_go_done.s(Stmt::While(not_expr, wbody));
265-
calyx_go_done.add_stmt_loc(dut_while, 175, 219, calyx_fileid);
266-
let dut_go_reassign = calyx_go_done.s(Stmt::Assign(dut_go, zero_expr));
267-
calyx_go_done.add_stmt_loc(dut_go_reassign, 222, 234, calyx_fileid);
268-
let dut_ii_dontcare = calyx_go_done.s(Stmt::Assign(dut_ii, calyx_go_done.expr_dont_care()));
269-
calyx_go_done.add_stmt_loc(dut_ii_dontcare, 238, 250, calyx_fileid);
270-
let oo_assign = calyx_go_done.s(Stmt::Assign(oo, dut_oo_expr));
271-
calyx_go_done.add_stmt_loc(oo_assign, 254, 268, calyx_fileid);
272-
let body = vec![
273-
dut_ii_assign,
274-
dut_go_assign,
275-
dut_while,
276-
dut_go_reassign,
277-
dut_ii_dontcare,
278-
oo_assign,
279-
];
280-
calyx_go_done.body = calyx_go_done.s(Stmt::Block(body));
200+
fn serialize_calyx_go_down_transaction() {
201+
let mut handler = DiagnosticHandler::new();
202+
let (calyx_go_done, symbols) = create_calyx_go_down_transaction(&mut handler);
281203
type_check(&calyx_go_done, &symbols, &mut handler);
282204
}
283205
}

src/ir.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ impl Arg {
131131
self.dir
132132
}
133133

134+
pub fn symbol(&self) -> SymbolId {
135+
self.symbol
136+
}
137+
134138
pub fn new(symbol: SymbolId, dir: Dir) -> Self {
135139
Self { dir, symbol }
136140
}
@@ -175,6 +179,7 @@ pub enum Stmt {
175179
Fork,
176180
While(ExprId, StmtId),
177181
IfElse(ExprId, StmtId, StmtId),
182+
AssertEq(ExprId, ExprId),
178183
}
179184

180185
#[derive(Clone, Copy, Hash, PartialEq, Eq, Default)]
@@ -439,6 +444,10 @@ impl SymbolTableEntry {
439444
self.tpe.clone()
440445
}
441446

447+
pub fn parent(&self) -> Option<SymbolId> {
448+
self.parent
449+
}
450+
442451
/// full hierarchical name
443452
pub fn full_name(&self, symbols: &SymbolTable) -> String {
444453
let mut name = self.name.clone();

src/serialize.rs

Lines changed: 91 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use baa::BitVecOps;
1010

1111
use crate::{diagnostic::*, ir::*};
1212

13-
fn serialize_to_string(tr: &Transaction, st: &SymbolTable) -> std::io::Result<String> {
13+
pub fn serialize_to_string(tr: &Transaction, st: &SymbolTable) -> std::io::Result<String> {
1414
let mut out = Vec::new();
1515
serialize(&mut out, tr, st)?;
1616
let out = String::from_utf8(out).unwrap();
@@ -90,6 +90,15 @@ fn build_statements(
9090
build_statements(out, tr, st, elsebody, index + 1)?;
9191
writeln!(out, "{}}}", " ".repeat(index))?;
9292
}
93+
Stmt::AssertEq(exprid1, exprid2) => {
94+
writeln!(
95+
out,
96+
"{}assert_eq({}, {});",
97+
" ".repeat(index),
98+
serialize_expr(tr, st, exprid1),
99+
serialize_expr(tr, st, exprid2)
100+
)?;
101+
}
93102
}
94103

95104
Ok(())
@@ -179,9 +188,8 @@ pub mod tests {
179188

180189
use super::*;
181190

182-
#[test]
183-
fn serialize_add_transaction() {
184-
// Manually create the expected result of parsing `addStruct.prot`.
191+
pub fn create_add_transaction(handler: &mut DiagnosticHandler) -> (Transaction, SymbolTable) {
192+
// Manually create the expected result of parsing `add.prot`.
185193
// Note that the order in which things are created will be different in the parser.
186194

187195
// 1) declare symbols
@@ -209,6 +217,10 @@ pub mod tests {
209217
assert_eq!(symbols["DUT.s"], symbols[dut_s]);
210218
assert_eq!(symbols["s"], symbols[s]);
211219

220+
// create fileid and read file
221+
let input = std::fs::read_to_string("tests/addStruct.prot").expect("failed to load");
222+
let add_fileid = handler.add_file("add.prot".to_string(), input);
223+
212224
// 2) create transaction
213225
let mut add = Transaction::new("add".to_string());
214226
add.args = vec![
@@ -220,31 +232,54 @@ pub mod tests {
220232

221233
// 3) create expressions
222234
let a_expr = add.e(Expr::Sym(a));
235+
add.add_expr_loc(a_expr, 193, 194, add_fileid);
223236
let b_expr = add.e(Expr::Sym(b));
237+
add.add_expr_loc(b_expr, 208, 209, add_fileid);
224238
let dut_s_expr = add.e(Expr::Sym(dut_s));
239+
add.add_expr_loc(dut_s_expr, 271, 276, add_fileid);
240+
let s_expr = add.e(Expr::Sym(s));
225241

226242
// 4) create statements
243+
let a_assign = add.s(Stmt::Assign(dut_a, a_expr));
244+
add.add_stmt_loc(a_assign, 184, 195, add_fileid);
245+
let b_assign = add.s(Stmt::Assign(dut_b, b_expr));
246+
add.add_stmt_loc(b_assign, 199, 210, add_fileid);
247+
let step = add.s(Stmt::Step);
248+
add.add_stmt_loc(step, 214, 221, add_fileid);
249+
let fork = add.s(Stmt::Fork);
250+
add.add_stmt_loc(fork, 225, 232, add_fileid);
251+
let dut_a_assign = add.s(Stmt::Assign(dut_a, add.expr_dont_care()));
252+
add.add_stmt_loc(dut_a_assign, 236, 247, add_fileid);
253+
let dut_b_assign = add.s(Stmt::Assign(dut_b, add.expr_dont_care()));
254+
add.add_stmt_loc(dut_b_assign, 251, 262, add_fileid);
255+
let s_assign = add.s(Stmt::Assign(s, dut_s_expr));
256+
add.add_stmt_loc(s_assign, 266, 277, add_fileid);
257+
let dut_s_assign = add.s(Stmt::Assign(dut_s, a_expr));
258+
add.add_stmt_loc(dut_s_assign, 281, 292, add_fileid);
259+
let assert = add.s(Stmt::AssertEq(s_expr, dut_s_expr));
260+
add.add_stmt_loc(assert, 296, 316, add_fileid);
227261
let body = vec![
228-
add.s(Stmt::Assign(dut_a, a_expr)),
229-
add.s(Stmt::Assign(dut_b, b_expr)),
230-
add.s(Stmt::Step),
231-
add.s(Stmt::Fork),
232-
add.s(Stmt::Assign(dut_a, add.expr_dont_care())),
233-
add.s(Stmt::Assign(dut_b, add.expr_dont_care())),
234-
add.s(Stmt::Assign(s, dut_s_expr)),
262+
a_assign,
263+
b_assign,
264+
step,
265+
fork,
266+
dut_a_assign,
267+
dut_b_assign,
268+
s_assign,
269+
dut_s_assign,
270+
assert,
235271
];
236272
add.body = add.s(Stmt::Block(body));
237273

238-
println!("{}", serialize_to_string(&add, &symbols).unwrap());
274+
(add, symbols)
239275
}
240276

241-
#[test]
242-
fn serialize_calyx_go_done_transaction() {
243-
// Manually create the expected result of parsing `calyx_go_done`.
277+
pub fn create_calyx_go_down_transaction(
278+
handler: &mut DiagnosticHandler,
279+
) -> (Transaction, SymbolTable) {
280+
// Manually create the expected result of parsing `calyx_go_down`.
244281
// Note that the order in which things are created will be different in the parser.
245282

246-
// TODO: create this into function that factors our the code to put src code into IR
247-
248283
// 1) declare symbols
249284
let mut symbols = SymbolTable::default();
250285
let ii = symbols.add_without_parent("ii".to_string(), Type::BitVec(32));
@@ -256,7 +291,7 @@ pub mod tests {
256291
"Calyx".to_string(),
257292
vec![
258293
Field::new("ii".to_string(), Dir::In, Type::BitVec(32)),
259-
Field::new("go".to_string(), Dir::In, Type::BitVec(1)),
294+
Field::new("go".to_string(), Dir::In, Type::BitVec(32)),
260295
Field::new("done".to_string(), Dir::Out, Type::BitVec(1)),
261296
Field::new("oo".to_string(), Dir::Out, Type::BitVec(32)),
262297
],
@@ -270,30 +305,48 @@ pub mod tests {
270305
assert_eq!(symbols["dut.oo"], symbols[dut_oo]);
271306
assert_eq!(symbols["oo"], symbols[oo]);
272307

308+
// create fileid and read file
309+
let input =
310+
std::fs::read_to_string("tests/calyx_go_doneStruct.prot").expect("failed to load");
311+
let calyx_fileid = handler.add_file("calyx_go_done.prot".to_string(), input);
312+
273313
// 2) create transaction
274314
let mut calyx_go_done = Transaction::new("calyx_go_done".to_string());
275315
calyx_go_done.args = vec![Arg::new(ii, Dir::In), Arg::new(oo, Dir::Out)];
276316
calyx_go_done.type_args = vec![dut];
277317

278318
// 3) create expressions
279319
let ii_expr = calyx_go_done.e(Expr::Sym(ii));
320+
calyx_go_done.add_expr_loc(ii_expr, 153, 155, calyx_fileid);
280321
let dut_oo_expr = calyx_go_done.e(Expr::Sym(dut_oo));
322+
calyx_go_done.add_expr_loc(dut_oo_expr, 260, 266, calyx_fileid);
281323
let one_expr = calyx_go_done.e(Expr::Const(BitVecValue::from_u64(1, 1)));
324+
calyx_go_done.add_expr_loc(one_expr, 170, 171, calyx_fileid);
282325
let zero_expr = calyx_go_done.e(Expr::Const(BitVecValue::from_u64(0, 1)));
326+
calyx_go_done.add_expr_loc(zero_expr, 232, 233, calyx_fileid);
283327
let dut_done_expr = calyx_go_done.e(Expr::Sym(dut_done));
284-
let cond_expr = calyx_go_done.e(Expr::Binary(BinOp::Equal, dut_done_expr, one_expr));
285-
let not_expr = calyx_go_done.e(Expr::Unary(UnaryOp::Not, cond_expr));
328+
calyx_go_done.add_expr_loc(dut_done_expr, 184, 192, calyx_fileid);
329+
let cond_expr = calyx_go_done.e(Expr::Equal(dut_done_expr, one_expr));
330+
calyx_go_done.add_expr_loc(cond_expr, 183, 198, calyx_fileid);
331+
let not_expr = calyx_go_done.e(Expr::Not(cond_expr));
332+
calyx_go_done.add_expr_loc(not_expr, 182, 198, calyx_fileid);
286333

287334
// 4) create statements
288335
let while_body: Vec<StmtId> = vec![calyx_go_done.s(Stmt::Step)];
289336
let wbody = calyx_go_done.s(Stmt::Block(while_body));
290337

291338
let dut_ii_assign = calyx_go_done.s(Stmt::Assign(dut_ii, ii_expr));
292-
let dut_go_assign = calyx_go_done.s(Stmt::Assign(dut_go, one_expr));
339+
calyx_go_done.add_stmt_loc(dut_ii_assign, 143, 157, calyx_fileid);
340+
let dut_go_assign = calyx_go_done.s(Stmt::Assign(dut_go, one_expr)); // Should cause type mismatch error
341+
calyx_go_done.add_stmt_loc(dut_go_assign, 160, 172, calyx_fileid);
293342
let dut_while = calyx_go_done.s(Stmt::While(not_expr, wbody));
294-
let dut_go_reassign = calyx_go_done.s(Stmt::Assign(dut_go, zero_expr));
343+
calyx_go_done.add_stmt_loc(dut_while, 175, 219, calyx_fileid);
344+
let dut_go_reassign = calyx_go_done.s(Stmt::Assign(dut_go, zero_expr)); // Should cause type mismatch error
345+
calyx_go_done.add_stmt_loc(dut_go_reassign, 222, 234, calyx_fileid);
295346
let dut_ii_dontcare = calyx_go_done.s(Stmt::Assign(dut_ii, calyx_go_done.expr_dont_care()));
347+
calyx_go_done.add_stmt_loc(dut_ii_dontcare, 238, 250, calyx_fileid);
296348
let oo_assign = calyx_go_done.s(Stmt::Assign(oo, dut_oo_expr));
349+
calyx_go_done.add_stmt_loc(oo_assign, 254, 268, calyx_fileid);
297350
let body = vec![
298351
dut_ii_assign,
299352
dut_go_assign,
@@ -303,6 +356,22 @@ pub mod tests {
303356
oo_assign,
304357
];
305358
calyx_go_done.body = calyx_go_done.s(Stmt::Block(body));
359+
360+
(calyx_go_done, symbols)
361+
}
362+
363+
#[test]
364+
fn serialize_add_transaction() {
365+
let mut handler = DiagnosticHandler::new();
366+
let (add, symbols) = create_add_transaction(&mut handler);
367+
368+
println!("{}", serialize_to_string(&add, &symbols).unwrap());
369+
}
370+
371+
#[test]
372+
fn serialize_calyx_go_down_transaction() {
373+
let mut handler = DiagnosticHandler::new();
374+
let (calyx_go_done, symbols) = create_calyx_go_down_transaction(&mut handler);
306375
println!("{}", serialize_to_string(&calyx_go_done, &symbols).unwrap());
307376
}
308377

0 commit comments

Comments
 (0)