From 3ecb313e5ffacd7b6195054baf4e6fbb7ff79339 Mon Sep 17 00:00:00 2001 From: Yoshitomo Nakanishi Date: Mon, 23 May 2022 23:12:46 +0900 Subject: [PATCH] Remove unused codes --- crates/codegen/src/db.rs | 18 +++++++------- crates/driver/src/lib.rs | 36 +++++++--------------------- crates/fe/src/main.rs | 43 +++------------------------------- crates/mir/src/analysis/cfg.rs | 36 +++++++++++----------------- crates/test-utils/src/lib.rs | 6 ++--- crates/tests/src/crashes.rs | 2 +- 6 files changed, 38 insertions(+), 103 deletions(-) diff --git a/crates/codegen/src/db.rs b/crates/codegen/src/db.rs index 2808dddb80..828b062acb 100644 --- a/crates/codegen/src/db.rs +++ b/crates/codegen/src/db.rs @@ -49,42 +49,42 @@ pub trait CodegenDb: MirDb + Upcast + UpcastMut { // TODO: Move this to driver. #[salsa::database(SourceDbStorage, AnalyzerDbStorage, MirDbStorage, CodegenDbStorage)] #[derive(Default)] -pub struct NewDb { - storage: salsa::Storage, +pub struct Db { + storage: salsa::Storage, } -impl salsa::Database for NewDb {} +impl salsa::Database for Db {} -impl Upcast for NewDb { +impl Upcast for Db { fn upcast(&self) -> &(dyn MirDb + 'static) { &*self } } -impl UpcastMut for NewDb { +impl UpcastMut for Db { fn upcast_mut(&mut self) -> &mut (dyn MirDb + 'static) { &mut *self } } -impl Upcast for NewDb { +impl Upcast for Db { fn upcast(&self) -> &(dyn SourceDb + 'static) { &*self } } -impl UpcastMut for NewDb { +impl UpcastMut for Db { fn upcast_mut(&mut self) -> &mut (dyn SourceDb + 'static) { &mut *self } } -impl Upcast for NewDb { +impl Upcast for Db { fn upcast(&self) -> &(dyn AnalyzerDb + 'static) { &*self } } -impl UpcastMut for NewDb { +impl UpcastMut for Db { fn upcast_mut(&mut self) -> &mut (dyn AnalyzerDb + 'static) { &mut *self } diff --git a/crates/driver/src/lib.rs b/crates/driver/src/lib.rs index 3bfd47de34..80972fc053 100644 --- a/crates/driver/src/lib.rs +++ b/crates/driver/src/lib.rs @@ -1,7 +1,7 @@ #![allow(unused_imports, dead_code)] -pub use fe_codegen::db::{CodegenDb, NewDb}; -use fe_codegen::yul::runtime::RuntimeProvider; +pub use fe_codegen::db::{CodegenDb, Db}; +//use fe_codegen::yul::runtime::RuntimeProvider; use fe_analyzer::namespace::items::{IngotId, IngotMode, ModuleId}; use fe_analyzer::AnalyzerDb; @@ -36,7 +36,7 @@ pub struct CompiledContract { pub struct CompileError(pub Vec); pub fn compile_single_file( - db: &mut NewDb, + db: &mut Db, path: &str, src: &str, with_bytecode: bool, @@ -57,7 +57,7 @@ pub fn compile_single_file( /// If `with_bytecode` is set to false, the compiler will skip the final Yul -> /// Bytecode pass. This is useful when debugging invalid Yul code. pub fn compile_ingot( - db: &mut NewDb, + db: &mut Db, name: &str, files: &[(impl AsRef, impl AsRef)], with_bytecode: bool, @@ -86,7 +86,7 @@ pub fn compile_ingot( /// Returns graphviz string. // TODO: This is temporary function for debugging. -pub fn dump_mir_single_file(db: &mut NewDb, path: &str, src: &str) -> Result { +pub fn dump_mir_single_file(db: &mut Db, path: &str, src: &str) -> Result { let module = ModuleId::new_standalone(db, path, src); let diags = module.diagnostics(db); @@ -99,29 +99,9 @@ pub fn dump_mir_single_file(db: &mut NewDb, path: &str, src: &str) -> Result Result { - let module = ModuleId::new_standalone(db, path, src); - - let diags = module.diagnostics(db); - if !diags.is_empty() { - return Err(CompileError(diags)); - } - - let mut contracts = String::new(); - for contract in db.module_contracts(module).as_ref() { - contracts.push_str(&format!( - "{}", - fe_codegen::yul::isel::lower_contract_deployable(db, *contract) - )) - } - - Ok(contracts) -} - #[cfg(feature = "solc-backend")] fn compile_module_id( - db: &mut NewDb, + db: &mut Db, module_id: ModuleId, with_bytecode: bool, optimize: bool, @@ -158,7 +138,7 @@ fn compile_module_id( #[cfg(not(feature = "solc-backend"))] fn compile_module_id( - db: &mut NewDb, + db: &mut Db, module_id: ModuleId, _with_bytecode: bool, _optimize: bool, @@ -185,7 +165,7 @@ fn compile_module_id( }) } -fn compile_to_yul(db: &mut NewDb, contract: ContractId) -> String { +fn compile_to_yul(db: &mut Db, contract: ContractId) -> String { let yul_contract = fe_codegen::yul::isel::lower_contract_deployable(db, contract); yul_contract.to_string().replace('"', "\\\"") } diff --git a/crates/fe/src/main.rs b/crates/fe/src/main.rs index efdffb1e1b..a53d5027dc 100644 --- a/crates/fe/src/main.rs +++ b/crates/fe/src/main.rs @@ -53,7 +53,7 @@ pub fn main() { .long("emit") .help("Comma separated compile targets e.g. -e=bytecode,yul") .possible_values(&["abi", "bytecode", "ast", "tokens", "yul", "loweredAst"]) - .default_value("abi,bytecode,yul") + .default_value("abi,bytecode") .use_delimiter(true) .takes_value(true), ) @@ -77,12 +77,6 @@ pub fn main() { .help("dump mir dot file") .takes_value(false), ) - .arg( - Arg::with_name("codegen") - .long("codegen") - .help("todo") - .takes_value(false), - ) .get_matches(); let input_path = matches.value_of("input").unwrap(); @@ -97,16 +91,12 @@ pub fn main() { return mir_dump(input_path); } - if matches.is_present("codegen") { - return yul_functions_dump(input_path); - } - #[cfg(not(feature = "solc-backend"))] if with_bytecode { eprintln!("Warning: bytecode output requires 'solc-backend' feature. Try `cargo build --release --features solc-backend`. Skipping."); } - let mut db = fe_driver::NewDb::default(); + let mut db = fe_driver::Db::default(); let (content, compiled_module) = if Path::new(input_path).is_file() { let content = match std::fs::read_to_string(input_path) { @@ -284,7 +274,7 @@ fn verify_nonexistent_or_empty(dir: &Path) -> Result<(), String> { } fn mir_dump(input_path: &str) { - let mut db = fe_driver::NewDb::default(); + let mut db = fe_driver::Db::default(); if Path::new(input_path).is_file() { let content = match std::fs::read_to_string(input_path) { Err(err) => { @@ -307,30 +297,3 @@ fn mir_dump(input_path: &str) { std::process::exit(1) } } - -fn yul_functions_dump(input_path: &str) { - let mut db = fe_driver::NewDb::default(); - if Path::new(input_path).is_file() { - let content = match std::fs::read_to_string(input_path) { - Err(err) => { - eprintln!("Failed to load file: `{}`. Error: {}", input_path, err); - std::process::exit(1) - } - Ok(content) => content, - }; - - match fe_driver::dump_codegen_funcs(&mut db, input_path, &content) { - Ok(contract) => { - println!("{}", contract) - } - Err(err) => { - eprintln!("Unable to dump mir `{}", input_path); - print_diagnostics(&db, &err.0); - std::process::exit(1) - } - } - } else { - eprintln!("mir doesn't support ingot yet"); - std::process::exit(1) - } -} diff --git a/crates/mir/src/analysis/cfg.rs b/crates/mir/src/analysis/cfg.rs index bf1f2a44fa..5e2af0d0d6 100644 --- a/crates/mir/src/analysis/cfg.rs +++ b/crates/mir/src/analysis/cfg.rs @@ -131,18 +131,18 @@ impl<'a> Iterator for CfgPostOrder<'a> { fn next(&mut self) -> Option { while let Some(&block) = self.stack.last() { let node_state = self.node_state.entry(block).or_default(); - if node_state.is_unvisited() { - node_state.set_visited(); + if *node_state == NodeState::Unvisited { + *node_state = NodeState::Visited; for &succ in self.cfg.succs(block) { let pred_state = self.node_state.entry(succ).or_default(); - if pred_state.is_unvisited() { + if *pred_state == NodeState::Unvisited { self.stack.push(succ); } } } else { self.stack.pop().unwrap(); - if !node_state.has_finished() { - node_state.set_finished(); + if *node_state != NodeState::Finished { + *node_state = NodeState::Finished; return Some(block); } } @@ -152,23 +152,15 @@ impl<'a> Iterator for CfgPostOrder<'a> { } } -#[derive(Default, Debug, Clone, Copy)] -struct NodeState(u8); - -impl NodeState { - fn is_unvisited(self) -> bool { - self.0 == 0 - } - - fn has_finished(self) -> bool { - self.0 == 2 - } - - fn set_visited(&mut self) { - self.0 = 1; - } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum NodeState { + Unvisited, + Visited, + Finished, +} - fn set_finished(&mut self) { - self.0 = 2; +impl Default for NodeState { + fn default() -> Self { + Self::Unvisited } } diff --git a/crates/test-utils/src/lib.rs b/crates/test-utils/src/lib.rs index 0d0c2b4d79..4c22822d3d 100644 --- a/crates/test-utils/src/lib.rs +++ b/crates/test-utils/src/lib.rs @@ -329,7 +329,7 @@ pub fn deploy_contract( contract_name: &str, init_params: &[ethabi::Token], ) -> ContractHarness { - let mut db = driver::NewDb::default(); + let mut db = driver::Db::default(); let compiled_module = match driver::compile_single_file( &mut db, fixture, @@ -366,7 +366,7 @@ pub fn deploy_contract_from_ingot( init_params: &[ethabi::Token], ) -> ContractHarness { let files = test_files::fixture_dir_files(path); - let mut db = driver::NewDb::default(); + let mut db = driver::Db::default(); let compiled_module = match driver::compile_ingot(&mut db, "test", &files, true, true) { Ok(module) => module, Err(error) => { @@ -577,7 +577,7 @@ pub fn compile_solidity_contract( #[allow(dead_code)] pub fn load_contract(address: H160, fixture: &str, contract_name: &str) -> ContractHarness { - let mut db = driver::NewDb::default(); + let mut db = driver::Db::default(); let compiled_module = driver::compile_single_file(&mut db, fixture, test_files::fixture(fixture), true, true) .unwrap_or_else(|err| { diff --git a/crates/tests/src/crashes.rs b/crates/tests/src/crashes.rs index 8d0d686c63..8abec5ce35 100644 --- a/crates/tests/src/crashes.rs +++ b/crates/tests/src/crashes.rs @@ -5,7 +5,7 @@ macro_rules! test_file { #[test] #[wasm_bindgen_test] fn $name() { - let mut db = fe_driver::NewDb::default(); + let mut db = fe_driver::Db::default(); let path = concat!("crashes/", stringify!($name), ".fe"); let src = test_files::fixture(path); fe_driver::compile_single_file(&mut db, path, src, true, true).ok();