From 72b44eec00e18036bae520750720691db164a9be Mon Sep 17 00:00:00 2001 From: Edward Chen Date: Wed, 15 Sep 2021 20:54:57 -0400 Subject: [PATCH] adding in fn def --- src/front/c/ast_utils.rs | 72 ++++++++++++++++++++++++++++++++++++++++ src/front/c/mod.rs | 25 +++++--------- src/front/c/term.rs | 21 ++++++------ src/front/c/types.rs | 3 +- src/target/aby/trans.rs | 2 +- 5 files changed, 94 insertions(+), 29 deletions(-) create mode 100644 src/front/c/ast_utils.rs diff --git a/src/front/c/ast_utils.rs b/src/front/c/ast_utils.rs new file mode 100644 index 00000000..2f77272d --- /dev/null +++ b/src/front/c/ast_utils.rs @@ -0,0 +1,72 @@ +use lang_c::ast::{ + BlockItem, + DeclaratorKind, + DerivedDeclarator, + FunctionDefinition, + ParameterDeclaration, + Statement +}; +use lang_c::span::Node; + +use std::fmt::{self, Display, Formatter}; + + +pub struct FnInfo { + name: String, + args: Vec, + body: Vec>, +} + +impl FnInfo { + fn new(name: String, args: Vec, body: Vec>) -> Self { + Self { name, args, body } + } +} + +impl Display for FnInfo { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "name: {},\nargs: {:#?},\nbody: {:#?}", self.name, self.args, self.body) + } +} + +pub fn get_fn_info(fn_def: &FunctionDefinition) -> FnInfo { + let name = name_from_func(fn_def); + let args = args_from_func(fn_def).unwrap(); + let body = body_from_func(fn_def); + + FnInfo { + name, + args: args.to_vec(), + body: body.to_vec(), + } +} + + +fn name_from_func(fn_def: &FunctionDefinition) -> String { + let dec = &fn_def.declarator.node; + match dec.kind.node { + DeclaratorKind::Identifier(ref id) => id.node.name.clone(), + _ => panic!("Function name not found: {:?}", dec), + } +} + +fn args_from_func(fn_def: &FunctionDefinition) -> Option> { + let dec = &fn_def.declarator.node; + dec.derived.iter().find_map(|d| match d.node { + DerivedDeclarator::Function(ref fn_dec) => { + let args = fn_dec.node.parameters.iter().map(|a| { + a.node.clone() + }).collect::>(); + Some(args) + } + _ => None + }) +} + +fn body_from_func(fn_def: &FunctionDefinition) -> &Vec> { + let stmt = &fn_def.statement.node; + match stmt { + Statement::Compound(nodes) => nodes, + _ => panic!("Function body not found: {:?}", stmt), + } +} \ No newline at end of file diff --git a/src/front/c/mod.rs b/src/front/c/mod.rs index 37567577..29beaadc 100644 --- a/src/front/c/mod.rs +++ b/src/front/c/mod.rs @@ -1,25 +1,20 @@ //! The C front-end +mod ast_utils; mod parser; mod term; +mod types; use super::FrontEnd; -use crate::circify::{Circify, Loc, Val}; -use crate::ir::proof::{self, ConstraintMetadata}; +use crate::ir::proof; use crate::ir::term::*; use lang_c::ast::{ - Declarator, DeclaratorKind, ExternalDeclaration, TranslationUnit, }; -use lang_c::span::Node; -use lang_c::visit; -use lang_c::visit::Visit; -use log::debug; // use std::collections::HashMap; use std::fmt::{self, Display, Formatter}; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; -use term::*; const PROVER_VIS: Option = Some(proof::PROVER_ID); const PUBLIC_VIS: Option = None; @@ -65,6 +60,7 @@ impl Display for Mode { } } +/// The C front-end. Implements [FrontEnd]. pub struct C; impl FrontEnd for C { @@ -96,11 +92,8 @@ impl CGen { Self { source, tu, mode } } - fn get_name(&self, dec: &Declarator) -> String { - match dec.kind.node { - DeclaratorKind::Identifier(ref id) => id.node.name.clone(), - _ => panic!("Declarator Identified not found: {:?}", id.node), - } + fn gen_stmt(&mut self, ) { + } fn gen(&mut self) { @@ -111,8 +104,8 @@ impl CGen { println!("{:#?}", decl); } ExternalDeclaration::FunctionDefinition(ref fn_def) => { - let fn_name = self.get_name(&fn_def.node.declarator.node); - let ret_ty = + let fn_info = ast_utils::get_fn_info(&fn_def.node); + println!("{}", fn_info); } _ => panic!("Haven't implemented node: {:?}", n.node), } diff --git a/src/front/c/term.rs b/src/front/c/term.rs index 86bfbfbb..2e8109e1 100644 --- a/src/front/c/term.rs +++ b/src/front/c/term.rs @@ -1,15 +1,14 @@ //! C Terms -use crate::ir::term::*; +// use crate::ir::term::*; +// use crate::front::c::types::*; -pub enum CTermData { - CBool, - CInt(Bool, usize) -} +// pub enum CTermData { +// CBool, +// CInt(Ty::Bool, usize) +// } - -#[derive(Eq)] -pub struct CTerm { - term: CTermData, - udef: Bool, -} +// pub struct CTerm { +// term: CTermData, +// udef: Ty::Bool, +// } diff --git a/src/front/c/types.rs b/src/front/c/types.rs index e818518d..507ab2f7 100644 --- a/src/front/c/types.rs +++ b/src/front/c/types.rs @@ -1,6 +1,7 @@ //! C Types -pub enum Type { +#[derive(Clone, PartialEq, Eq)] +pub enum Ty { Uint(usize), Bool, } \ No newline at end of file diff --git a/src/target/aby/trans.rs b/src/target/aby/trans.rs index 6be3d9a0..d216b06f 100644 --- a/src/target/aby/trans.rs +++ b/src/target/aby/trans.rs @@ -510,7 +510,7 @@ impl ToABY { _ => panic!("Invalid bv-op in BvBinOp: {:?}", o), } } - Op::BvExtract(start, end) => { + Op::BvExtract(_start, _end) => { } _ => panic!("Non-field in embed_bv: {:?}", t),