mirror of
https://github.com/circify/circ.git
synced 2026-05-14 03:00:33 -04:00
adding function stack
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
int main(__attribute__((private(0))) int a, __attribute__((private(1))) int b) {
|
||||
int add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
int main(__attribute__((private(0))) int a, __attribute__((private(1))) int b) {
|
||||
return add(a, b);
|
||||
}
|
||||
@@ -4,20 +4,22 @@ from utils 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 + \
|
||||
c_array_tests + \
|
||||
div_tests + \
|
||||
array_tests
|
||||
# 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 + \
|
||||
# c_array_tests + \
|
||||
# div_tests + \
|
||||
# array_tests
|
||||
# shift_tests
|
||||
|
||||
test = add_tests
|
||||
|
||||
# TODO: add support for return value - int promotion
|
||||
# unsigned_arithmetic_tests + \
|
||||
|
||||
@@ -1,3 +1,20 @@
|
||||
add_tests = [
|
||||
[
|
||||
"Add two numbers - 1",
|
||||
3,
|
||||
"./third_party/ABY/build/bin/2pc_add",
|
||||
{"a": 1, "b": 0},
|
||||
{"a": 0, "b": 2},
|
||||
],
|
||||
[
|
||||
"Add two numbers - 2",
|
||||
2,
|
||||
"./third_party/ABY/build/bin/2pc_add",
|
||||
{"a": 0, "b": 0},
|
||||
{"a": 0, "b": 2},
|
||||
],
|
||||
]
|
||||
|
||||
arithmetic_tests = [
|
||||
[
|
||||
"Add two numbers - 1",
|
||||
|
||||
@@ -26,57 +26,57 @@ function mpc_test {
|
||||
|
||||
# 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/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/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
|
||||
# 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 nary arithmetic tests
|
||||
mpc_test 2 ./examples/C/mpc/unit_tests/nary_arithmetic_tests/2pc_nary_arithmetic_add.c
|
||||
# # build mpc nary arithmetic tests
|
||||
# mpc_test 2 ./examples/C/mpc/unit_tests/nary_arithmetic_tests/2pc_nary_arithmetic_add.c
|
||||
|
||||
# build mpc 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 mpc 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 mpc 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 mpc 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 mpc nary boolean tests
|
||||
mpc_test 2 ./examples/C/mpc/unit_tests/nary_boolean_tests/2pc_nary_boolean_and.c
|
||||
# # build mpc nary boolean tests
|
||||
# mpc_test 2 ./examples/C/mpc/unit_tests/nary_boolean_tests/2pc_nary_boolean_and.c
|
||||
|
||||
# build mpc 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 mpc 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 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 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 div tests
|
||||
mpc_test 2 ./examples/C/mpc/unit_tests/div_tests/2pc_div.c
|
||||
# # build div tests
|
||||
# mpc_test 2 ./examples/C/mpc/unit_tests/div_tests/2pc_div.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 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 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 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 shift tests
|
||||
# # mpc_test 2 ./examples/C/mpc/unit_tests/shift_tests/2pc_lhs.c
|
||||
|
||||
@@ -17,7 +17,7 @@ use lang_c::ast::*;
|
||||
use lang_c::span::Node;
|
||||
use log::debug;
|
||||
|
||||
// use std::collections::HashMap;
|
||||
use std::collections::HashMap;
|
||||
use std::fmt::Display;
|
||||
use std::path::PathBuf;
|
||||
|
||||
@@ -54,17 +54,12 @@ impl FrontEnd for C {
|
||||
let parser = parser::CParser::new();
|
||||
let p = parser.parse_file(&i.file).unwrap();
|
||||
let mut g = CGen::new(i.inputs, i.mode, p.unit);
|
||||
g.gen();
|
||||
g.visit_files();
|
||||
g.entry_fn("main");
|
||||
g.circ.consume().borrow().clone()
|
||||
}
|
||||
}
|
||||
|
||||
struct CGen {
|
||||
circ: Circify<Ct>,
|
||||
mode: Mode,
|
||||
tu: TranslationUnit,
|
||||
}
|
||||
|
||||
enum CLoc {
|
||||
Var(Loc),
|
||||
Idx(Box<CLoc>, CTerm),
|
||||
@@ -79,12 +74,20 @@ impl CLoc {
|
||||
}
|
||||
}
|
||||
|
||||
struct CGen {
|
||||
circ: Circify<Ct>,
|
||||
mode: Mode,
|
||||
tu: TranslationUnit,
|
||||
functions: HashMap<String, FnInfo>,
|
||||
}
|
||||
|
||||
impl CGen {
|
||||
fn new(inputs: Option<PathBuf>, mode: Mode, tu: TranslationUnit) -> Self {
|
||||
let this = Self {
|
||||
circ: Circify::new(Ct::new(inputs.map(parser::parse_inputs))),
|
||||
mode,
|
||||
tu,
|
||||
functions: HashMap::new(),
|
||||
};
|
||||
this.circ
|
||||
.cir_ctx()
|
||||
@@ -617,4 +620,78 @@ impl CGen {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn entry_fn(&mut self, n: &str) {
|
||||
debug!("Entry: {}", n);
|
||||
// find the entry function
|
||||
let fn_info = self
|
||||
.functions
|
||||
.get(n)
|
||||
.unwrap_or_else(|| panic!("No function '{}'", n))
|
||||
.clone();
|
||||
|
||||
self.circ
|
||||
.enter_fn(fn_info.name.to_owned(), fn_info.ret_ty.clone());
|
||||
|
||||
for arg in fn_info.args.iter() {
|
||||
// TODO: self.gen_decl(arg);
|
||||
let p = &arg.specifiers[0];
|
||||
let vis = self.interpret_visibility(&p.node);
|
||||
let base_ty = d_type_(arg.specifiers[1..].to_vec());
|
||||
let d = &arg.declarator.as_ref().unwrap().node;
|
||||
let derived_ty = self.derived_type_(base_ty.unwrap(), d.derived.to_vec());
|
||||
let name = name_from_decl(d);
|
||||
let res = self.circ.declare(name.clone(), &derived_ty, true, vis);
|
||||
self.unwrap(res);
|
||||
}
|
||||
|
||||
self.gen_stmt(fn_info.body.clone());
|
||||
if let Some(r) = self.circ.exit_fn() {
|
||||
match self.mode {
|
||||
Mode::Mpc(_) => {
|
||||
let ret_term = r.unwrap_term();
|
||||
let ret_terms = ret_term.term.terms();
|
||||
self.circ
|
||||
.cir_ctx()
|
||||
.cs
|
||||
.borrow_mut()
|
||||
.outputs
|
||||
.extend(ret_terms);
|
||||
}
|
||||
Mode::Proof => {
|
||||
let ty = fn_info.ret_ty.as_ref().unwrap();
|
||||
let name = "return".to_owned();
|
||||
let term = r.unwrap_term();
|
||||
let _r = self.circ.declare(name.clone(), ty, false, PROVER_VIS);
|
||||
self.circ.assign_with_assertions(name, term, ty, PUBLIC_VIS);
|
||||
unimplemented!();
|
||||
}
|
||||
_ => unimplemented!("Mode: {}", self.mode),
|
||||
}
|
||||
}
|
||||
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
fn visit_files(&mut self) {
|
||||
// TODO: Add support for multiple files
|
||||
|
||||
let TranslationUnit(nodes) = self.tu.clone();
|
||||
for n in nodes.iter() {
|
||||
match n.node {
|
||||
ExternalDeclaration::Declaration(ref decl) => {
|
||||
debug!("{:#?}", decl);
|
||||
}
|
||||
ExternalDeclaration::FunctionDefinition(ref fn_def) => {
|
||||
let fn_info = ast_utils::get_fn_info(&fn_def.node);
|
||||
let fname = fn_info.name.clone();
|
||||
self.functions.insert(fname, fn_info);
|
||||
}
|
||||
_ => unimplemented!("Haven't implemented node: {:?}", n.node),
|
||||
};
|
||||
}
|
||||
|
||||
// TODO: imports
|
||||
// TODO: structs
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user