From fa01772ef5dba16f32810bcde29bb3d375b1f106 Mon Sep 17 00:00:00 2001 From: Edward Chen Date: Thu, 31 Mar 2022 13:41:32 -0400 Subject: [PATCH] typedef --- examples/C/mpc/2pc_test.c | 32 ++++----- scripts/aby_tests/c_test_aby.py | 42 +++++------ scripts/aby_tests/test_suite.py | 3 +- scripts/build_mpc_c_test.zsh | 124 ++++++++++++++++---------------- src/front/c/mod.rs | 108 +++++++++++++++++++--------- 5 files changed, 177 insertions(+), 132 deletions(-) diff --git a/examples/C/mpc/2pc_test.c b/examples/C/mpc/2pc_test.c index f86ab29e..66210289 100644 --- a/examples/C/mpc/2pc_test.c +++ b/examples/C/mpc/2pc_test.c @@ -1,21 +1,21 @@ -int f(int a, int b, int len) { - int res = 0; - for(int i = 0; i < len; i++){ - if (a < b) { - res = res + 1; - } else { - res = 2; - } - } - return res; +typedef int DT ; + +int main(__attribute__((private(0))) DT a, __attribute__((private(1))) DT b) { + return a + b; } -int main(__attribute__((private(0))) int a, __attribute__((private(1))) int b) { - int x[1] = {0}; - x[0] = f(a, b, 5); - int cc = x[0]; - return cc; -} + +// int f(int a, int b, int len) { +// int res = 0; +// for(int i = 0; i < len; i++){ +// if (a < b) { +// res = res + 1; +// } else { +// res = 2; +// } +// } +// return res; +// } // struct s { // int c[4]; diff --git a/scripts/aby_tests/c_test_aby.py b/scripts/aby_tests/c_test_aby.py index f575fb94..4885bcbb 100755 --- a/scripts/aby_tests/c_test_aby.py +++ b/scripts/aby_tests/c_test_aby.py @@ -4,29 +4,29 @@ from util import run_tests from test_suite import * if __name__ == "__main__": - tests = arithmetic_tests + \ - mod_tests + \ - arithmetic_boolean_tests + \ - nary_arithmetic_tests + \ - bitwise_tests + \ - boolean_tests + \ - nary_boolean_tests + \ - const_arith_tests + \ - const_bool_tests + \ - ite_tests + \ - div_tests + \ - array_tests + \ - struct_tests + \ - matrix_tests + \ - ptr_tests + \ - c_array_tests + \ - c_misc_tests + \ - biomatch_tests + \ - kmeans_tests + \ - kmeans_tests_2 + # tests = arithmetic_tests + \ + # mod_tests + \ + # arithmetic_boolean_tests + \ + # nary_arithmetic_tests + \ + # bitwise_tests + \ + # boolean_tests + \ + # nary_boolean_tests + \ + # const_arith_tests + \ + # const_bool_tests + \ + # ite_tests + \ + # div_tests + \ + # array_tests + \ + # struct_tests + \ + # matrix_tests + \ + # ptr_tests + \ + # c_array_tests + \ + # c_misc_tests + \ + # biomatch_tests + \ + # kmeans_tests + \ + # kmeans_tests_2 # shift_tests - # tests = new_tests + tests = new_tests # tests = kmeans_tests_2 # tests = struct_tests # tests = matrix_tests diff --git a/scripts/aby_tests/test_suite.py b/scripts/aby_tests/test_suite.py index 3a562a65..5a98120f 100644 --- a/scripts/aby_tests/test_suite.py +++ b/scripts/aby_tests/test_suite.py @@ -15,7 +15,8 @@ new_tests = [ [ "new", "2pc_test", - "./scripts/aby_tests/test_inputs/test.txt", + # "./scripts/aby_tests/test_inputs/test.txt", + "./scripts/aby_tests/test_inputs/add.txt", ], # [ # "biomatch - 1", diff --git a/scripts/build_mpc_c_test.zsh b/scripts/build_mpc_c_test.zsh index b238c192..4a7c389a 100755 --- a/scripts/build_mpc_c_test.zsh +++ b/scripts/build_mpc_c_test.zsh @@ -24,83 +24,85 @@ function mpc_test { RUST_BACKTRACE=1 measure_time $BIN --parties $parties $cpath mpc --cost-model "hycc" } -# build mpc arithmetic tests -mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_add.c -mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_sub.c -mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_mult.c -mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_mult_add_pub.c -mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_mod.c -# mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_add_unsigned.c +mpc_test 2 ./examples/C/mpc/2pc_test.c -mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_int_equals.c -mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_int_greater_than.c -mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_int_greater_equals.c -mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_int_less_than.c -mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_int_less_equals.c +# # build mpc arithmetic tests +# mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_add.c +# mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_sub.c +# mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_mult.c +# mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_mult_add_pub.c +# mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_mod.c +# # mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_add_unsigned.c -# # build nary arithmetic tests -mpc_test 2 ./examples/C/mpc/unit_tests/nary_arithmetic_tests/2pc_nary_arithmetic_add.c +# mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_int_equals.c +# mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_int_greater_than.c +# mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_int_greater_equals.c +# mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_int_less_than.c +# mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_int_less_equals.c -# # build bitwise tests -mpc_test 2 ./examples/C/mpc/unit_tests/bitwise_tests/2pc_bitwise_and.c -mpc_test 2 ./examples/C/mpc/unit_tests/bitwise_tests/2pc_bitwise_or.c -mpc_test 2 ./examples/C/mpc/unit_tests/bitwise_tests/2pc_bitwise_xor.c +# # # build nary arithmetic tests +# mpc_test 2 ./examples/C/mpc/unit_tests/nary_arithmetic_tests/2pc_nary_arithmetic_add.c -# # build boolean tests -mpc_test 2 ./examples/C/mpc/unit_tests/boolean_tests/2pc_boolean_and.c -mpc_test 2 ./examples/C/mpc/unit_tests/boolean_tests/2pc_boolean_or.c -mpc_test 2 ./examples/C/mpc/unit_tests/boolean_tests/2pc_boolean_equals.c +# # # build bitwise tests +# mpc_test 2 ./examples/C/mpc/unit_tests/bitwise_tests/2pc_bitwise_and.c +# mpc_test 2 ./examples/C/mpc/unit_tests/bitwise_tests/2pc_bitwise_or.c +# mpc_test 2 ./examples/C/mpc/unit_tests/bitwise_tests/2pc_bitwise_xor.c -# # build nary boolean tests -mpc_test 2 ./examples/C/mpc/unit_tests/nary_boolean_tests/2pc_nary_boolean_and.c +# # # build boolean tests +# mpc_test 2 ./examples/C/mpc/unit_tests/boolean_tests/2pc_boolean_and.c +# mpc_test 2 ./examples/C/mpc/unit_tests/boolean_tests/2pc_boolean_or.c +# mpc_test 2 ./examples/C/mpc/unit_tests/boolean_tests/2pc_boolean_equals.c -# # build const tests -mpc_test 2 ./examples/C/mpc/unit_tests/const_tests/2pc_const_arith.c -mpc_test 2 ./examples/C/mpc/unit_tests/const_tests/2pc_const_bool.c +# # # build nary boolean tests +# mpc_test 2 ./examples/C/mpc/unit_tests/nary_boolean_tests/a -# build if statement tests -mpc_test 2 ./examples/C/mpc/unit_tests/ite_tests/2pc_ite_ret_bool.c -mpc_test 2 ./examples/C/mpc/unit_tests/ite_tests/2pc_ite_ret_int.c -mpc_test 2 ./examples/C/mpc/unit_tests/ite_tests/2pc_ite_only_if.c +# # # build const tests +# mpc_test 2 ./examples/C/mpc/unit_tests/const_tests/2pc_const_arith.c +# mpc_test 2 ./examples/C/mpc/unit_tests/const_tests/2pc_const_bool.c -# build div tests -mpc_test 2 ./examples/C/mpc/unit_tests/div_tests/2pc_div.c +# # build if statement tests +# mpc_test 2 ./examples/C/mpc/unit_tests/ite_tests/2pc_ite_ret_bool.c +# mpc_test 2 ./examples/C/mpc/unit_tests/ite_tests/2pc_ite_ret_int.c +# mpc_test 2 ./examples/C/mpc/unit_tests/ite_tests/2pc_ite_only_if.c -# build array tests -mpc_test 2 ./examples/C/mpc/unit_tests/array_tests/2pc_array_sum.c -mpc_test 2 ./examples/C/mpc/unit_tests/array_tests/2pc_array_index.c -mpc_test 2 ./examples/C/mpc/unit_tests/array_tests/2pc_array_index_2.c +# # build div tests +# mpc_test 2 ./examples/C/mpc/unit_tests/div_tests/2pc_div.c -# build circ/compiler array tests -mpc_test 2 ./examples/C/mpc/unit_tests/c_array_tests/2pc_array.c -mpc_test 2 ./examples/C/mpc/unit_tests/c_array_tests/2pc_array_1.c -mpc_test 2 ./examples/C/mpc/unit_tests/c_array_tests/2pc_array_2.c -mpc_test 2 ./examples/C/mpc/unit_tests/c_array_tests/2pc_array_3.c -mpc_test 2 ./examples/C/mpc/unit_tests/c_array_tests/2pc_array_sum_c.c +# # build array tests +# mpc_test 2 ./examples/C/mpc/unit_tests/array_tests/2pc_array_sum.c +# mpc_test 2 ./examples/C/mpc/unit_tests/array_tests/2pc_array_index.c +# mpc_test 2 ./examples/C/mpc/unit_tests/array_tests/2pc_array_index_2.c -# build function tests -mpc_test 2 ./examples/C/mpc/unit_tests/function_tests/2pc_function_add.c +# # build circ/compiler array tests +# mpc_test 2 ./examples/C/mpc/unit_tests/c_array_tests/2pc_array.c +# mpc_test 2 ./examples/C/mpc/unit_tests/c_array_tests/2pc_array_1.c +# mpc_test 2 ./examples/C/mpc/unit_tests/c_array_tests/2pc_array_2.c +# mpc_test 2 ./examples/C/mpc/unit_tests/c_array_tests/2pc_array_3.c +# mpc_test 2 ./examples/C/mpc/unit_tests/c_array_tests/2pc_array_sum_c.c -# build struct tests -mpc_test 2 ./examples/C/mpc/unit_tests/struct_tests/2pc_struct_add.c -mpc_test 2 ./examples/C/mpc/unit_tests/struct_tests/2pc_struct_array_add.c +# # build function tests +# mpc_test 2 ./examples/C/mpc/unit_tests/function_tests/2pc_function_add.c -# build matrix tests -mpc_test 2 ./examples/C/mpc/unit_tests/matrix_tests/2pc_matrix_add.c -mpc_test 2 ./examples/C/mpc/unit_tests/matrix_tests/2pc_matrix_assign_add.c -mpc_test 2 ./examples/C/mpc/unit_tests/matrix_tests/2pc_matrix_ptr_add.c +# # build struct tests +# mpc_test 2 ./examples/C/mpc/unit_tests/struct_tests/2pc_struct_add.c +# mpc_test 2 ./examples/C/mpc/unit_tests/struct_tests/2pc_struct_array_add.c -# build ptr tests -mpc_test 2 ./examples/C/mpc/unit_tests/ptr_tests/2pc_ptr_add.c +# # build matrix tests +# mpc_test 2 ./examples/C/mpc/unit_tests/matrix_tests/2pc_matrix_add.c +# mpc_test 2 ./examples/C/mpc/unit_tests/matrix_tests/2pc_matrix_assign_add.c +# mpc_test 2 ./examples/C/mpc/unit_tests/matrix_tests/2pc_matrix_ptr_add.c -# build misc tests -mpc_test 2 ./examples/C/mpc/unit_tests/misc_tests/2pc_millionaires.c -mpc_test 2 ./examples/C/mpc/unit_tests/misc_tests/2pc_multi_var.c +# # build ptr tests +# mpc_test 2 ./examples/C/mpc/unit_tests/ptr_tests/2pc_ptr_add.c -# build hycc benchmarks -mpc_test 2 ./examples/C/mpc/benchmarks/2pc_biomatch.c -mpc_test 2 ./examples/C/mpc/benchmarks/2pc_kmeans.c -mpc_test 2 ./examples/C/mpc/benchmarks/2pc_kmeans_og.c +# # build misc tests +# mpc_test 2 ./examples/C/mpc/unit_tests/misc_tests/2pc_millionaires.c +# mpc_test 2 ./examples/C/mpc/unit_tests/misc_tests/2pc_multi_var.c + +# # build hycc benchmarks +# mpc_test 2 ./examples/C/mpc/benchmarks/2pc_biomatch.c +# mpc_test 2 ./examples/C/mpc/benchmarks/2pc_kmeans.c +# mpc_test 2 ./examples/C/mpc/benchmarks/2pc_kmeans_og.c # # # build shift tests # # mpc_test 2 ./examples/C/mpc/unit_tests/shift_tests/2pc_lhs.c diff --git a/src/front/c/mod.rs b/src/front/c/mod.rs index f3b91b8f..3a446532 100644 --- a/src/front/c/mod.rs +++ b/src/front/c/mod.rs @@ -91,6 +91,7 @@ struct CGen { tu: TranslationUnit, structs: HashMap, functions: HashMap, + typedefs: HashMap, } impl CGen { @@ -101,6 +102,7 @@ impl CGen { tu, structs: HashMap::default(), functions: HashMap::default(), + typedefs: HashMap::default(), }; this.circ .cir_ctx() @@ -152,13 +154,22 @@ impl CGen { TypeSpecifier::Unsigned => Some(Ty::Int(false, 32)), TypeSpecifier::Bool => Some(Ty::Bool), TypeSpecifier::Void => None, + TypeSpecifier::TypedefName(td) => { + let name = td.node.name; + if self.typedefs.contains_key(&name) { + Some(self.typedefs[&name].clone()) + } else { + panic!("Typedef not defined: {}", name); + } + } TypeSpecifier::Struct(s) => { + println!("struct: {:#?}", s.node); + let StructType { kind: _kind, identifier, declarations, } = s.node; - let name = identifier.unwrap().node.name; let mut fs: Vec<(String, Ty)> = Vec::new(); match declarations { Some(decls) => { @@ -188,7 +199,11 @@ impl CGen { } None => {} } - Some(Ty::Struct(name, FieldList::new(fs))) + + match identifier { + Some(name) => Some(Ty::Struct(name.node.name, FieldList::new(fs))), + None => Some(Ty::Struct("".to_string(), FieldList::new(fs))), + } } _ => unimplemented!("Type {:#?} not implemented yet.", t), }; @@ -247,24 +262,30 @@ impl CGen { ty = derived_ty; } - // Save struct declarations to the cache - let new_ty: Ty = match ty.clone() { - Ty::Struct(name, fs) => { - if fs.clone().len() > 0 { - self.structs.insert(name.to_string(), ty.clone()); - ty - } else { - self.structs.get(&name).unwrap().clone() - } - } - _ => ty, - }; - let mut res: Vec = Vec::new(); for node in decl.declarators.into_iter() { - let name = name_from_decl(&node.node.declarator.node); + let decl_name = name_from_decl(&node.node.declarator.node); + + // Save struct declarations to the cache + let new_ty: Ty = match ty.clone() { + Ty::Struct(name, fs) => { + if decl_name != name { + let s_ty: Ty = Ty::Struct(decl_name.clone(), fs); + self.structs + .insert(decl_name.to_string().clone(), s_ty.clone()); + s_ty + } else if fs.clone().len() > 0 { + self.structs.insert(name.to_string(), ty.clone()); + ty.clone() + } else { + self.structs.get(&name).unwrap().clone() + } + } + _ => ty.clone(), + }; + let d = DeclInfo { - name, + name: decl_name, ty: new_ty.clone(), }; res.push(d); @@ -853,24 +874,42 @@ impl CGen { } fn gen_decl(&mut self, decl: Declaration) -> Vec { - let decl_infos = self.get_decl_info(decl.clone()); - let mut exprs: Vec = Vec::new(); - for (d, info) in decl.declarators.iter().zip(decl_infos.iter()) { - let expr: CTerm; - if let Some(init) = d.node.initializer.clone() { - expr = self.gen_init(info.ty.clone(), init.node); - } else { - expr = info.ty.default(&mut self.circ) + println!("decl: {:#?}", decl); + let specs = decl.specifiers.clone(); + if let DeclarationSpecifier::StorageClass(_store_node) = &specs[0].node { + let new_decl = Declaration { + specifiers: decl.specifiers[1..].to_vec().clone(), + declarators: decl.declarators.clone(), + }; + let decl_infos = self.get_decl_info(new_decl); + for info in decl_infos.iter() { + if !self.typedefs.contains_key(&info.name) { + self.typedefs.insert(info.name.clone(), info.ty.clone()); + } else { + panic!("Typedef already defined for: {}", info.name); + } } - let res = self.circ.declare_init( - info.name.clone(), - info.ty.clone(), - Val::Term(cast(Some(info.ty.clone()), expr.clone())), - ); - self.unwrap(res); - exprs.push(expr); + Vec::new() + } else { + let decl_infos = self.get_decl_info(decl.clone()); + let mut exprs: Vec = Vec::new(); + for (d, info) in decl.declarators.iter().zip(decl_infos.iter()) { + let expr: CTerm; + if let Some(init) = d.node.initializer.clone() { + expr = self.gen_init(info.ty.clone(), init.node); + } else { + expr = info.ty.default(&mut self.circ) + } + let res = self.circ.declare_init( + info.name.clone(), + info.ty.clone(), + Val::Term(cast(Some(info.ty.clone()), expr.clone())), + ); + self.unwrap(res); + exprs.push(expr); + } + exprs } - exprs } //TODO: This function is not quite right because the loop body could modify the iteration variable. @@ -1034,6 +1073,8 @@ impl CGen { self.unwrap(r); } + println!("body: {:#?}", f.body.clone()); + self.gen_stmt(f.body.clone()); if let Some(r) = self.circ.exit_fn() { match self.mode { @@ -1063,6 +1104,7 @@ impl CGen { fn visit_files(&mut self) { let TranslationUnit(nodes) = self.tu.clone(); for n in nodes.iter() { + println!("{:#?}", n.node); match n.node { ExternalDeclaration::Declaration(ref decl) => { self.gen_decl(decl.node.clone());