@@ -10,7 +10,7 @@ use baa::BitVecOps;
1010
1111use 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