From 7b1cdf9da3d714d808a4b7b10b66c6eb965cde83 Mon Sep 17 00:00:00 2001 From: Minseong Jang Date: Thu, 20 Jan 2022 18:27:01 +0900 Subject: [PATCH] Use `clap::Parser` --- Cargo.lock | 144 +++++++++++++++++++++++++++++------------------ Cargo.toml | 2 +- bin/fuzz.rs | 70 ++++++++++++++++------- bin/fuzz_cli.yml | 32 ----------- bin/kecc.rs | 106 ++++++++++++++++++++++++---------- bin/kecc_cli.yml | 51 ----------------- 6 files changed, 218 insertions(+), 187 deletions(-) delete mode 100644 bin/fuzz_cli.yml delete mode 100644 bin/kecc_cli.yml diff --git a/Cargo.lock b/Cargo.lock index 38bd720..c2ad1eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "addr2line" -version = "0.15.2" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7a2e47a1fbe209ee101dd6d61285226744c6c8d3c21c8dc878ba6cb9f467f3a" +checksum = "b9ecd88a8c8378ca913a680cd98f0f13ac67383d35993f86c90a70e3f137816b" dependencies = [ "gimli", ] @@ -36,9 +36,9 @@ checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" [[package]] name = "backtrace" -version = "0.3.60" +version = "0.3.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7815ea54e4d821e791162e078acbebfd6d8c8939cd559c9335dceb1c8ca7282" +checksum = "321629d8ba6513061f26707241fa9bc89524ff1cd7a915a97ef0c62c666ce1b6" dependencies = [ "addr2line", "cc", @@ -51,15 +51,15 @@ dependencies = [ [[package]] name = "bitflags" -version = "1.2.1" +version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "cc" -version = "1.0.68" +version = "1.0.72" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a72c244c1ff497a746a7e1fb3d14bd08420ecda70c8f25c7112f2781652d787" +checksum = "22a9137b95ea06864e018375b72adfb7db6e6f68cfc8df5a04d00288050485ee" [[package]] name = "cfg-if" @@ -75,13 +75,26 @@ checksum = "7a30c3bf9ff12dfe5dae53f0a96e0febcd18420d1c0e7fad77796d9d5c4b5375" dependencies = [ "atty", "bitflags", + "clap_derive", "indexmap", "lazy_static", "os_str_bytes", "strsim", "termcolor", "textwrap", - "yaml-rust", +] + +[[package]] +name = "clap_derive" +version = "3.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "517358c28fcef6607bf6f76108e02afad7e82297d132a6b846dcc1fc3efcd153" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -123,9 +136,9 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.3" +version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753" +checksum = "418d37c8b1d42553c93648be529cb70f920d3baf8ef469b74b9638df426e0b4c" dependencies = [ "cfg-if", "libc", @@ -134,21 +147,27 @@ dependencies = [ [[package]] name = "gimli" -version = "0.24.0" +version = "0.26.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e4075386626662786ddb0ec9081e7c7eeb1ba31951f447ca780ef9f5d568189" +checksum = "78cc372d058dcf6d5ecd98510e7fbc9e5aec4d21de70f65fea8fecebcd881bd4" [[package]] name = "hashbrown" -version = "0.9.1" +version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04" +checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" + +[[package]] +name = "heck" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" [[package]] name = "hermit-abi" -version = "0.1.18" +version = "0.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" dependencies = [ "libc", ] @@ -161,9 +180,9 @@ checksum = "dfa686283ad6dd069f105e5ab091b04c62850d3e4cf5d67debad1933f55023df" [[package]] name = "indexmap" -version = "1.6.2" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "824845a0bf897a9042383849b02c1bc219c2383772efcd5c6f9766fa4b81aef3" +checksum = "282a6247722caba404c065016bbfa522806e51714c34f5dfc3e4a3a46fcb4223" dependencies = [ "autocfg", "hashbrown", @@ -217,21 +236,15 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.97" +version = "0.2.112" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12b8adadd720df158f4d70dfe7ccc6adb0472d7c55ca83445f6a5ab3e36f8fb6" - -[[package]] -name = "linked-hash-map" -version = "0.5.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3" +checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125" [[package]] name = "memchr" -version = "2.4.0" +version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc" +checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" [[package]] name = "miniz_oxide" @@ -254,9 +267,9 @@ dependencies = [ [[package]] name = "object" -version = "0.25.3" +version = "0.27.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a38f2be3697a57b4060074ff41b44c16870d916ad7877c17696e063257482bc7" +checksum = "67ac1d3f9a1d3616fd9a60c8d74296f22406a238b6a72f5cc1e6f314df4ffbf9" dependencies = [ "memchr", ] @@ -308,24 +321,48 @@ checksum = "f9b0efd3ba03c3a409d44d60425f279ec442bcf0b9e63ff4e410da31c8b0f69f" [[package]] name = "ppv-lite86" -version = "0.2.10" +version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" +checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] [[package]] name = "proc-macro2" -version = "1.0.27" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0d8caf72986c1a598726adc988bb5984792ef84f5ee5aa50209145ee8077038" +checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" dependencies = [ "unicode-xid", ] [[package]] name = "quote" -version = "1.0.9" +version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" +checksum = "47aa80447ce4daf1717500037052af176af5d38cc3e571d9ec1c7353fc10c87d" dependencies = [ "proc-macro2", ] @@ -372,9 +409,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.2.9" +version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ab49abadf3f9e1c4bc499e8845e152ad87d2ad2d30371841171169e9d75feee" +checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff" dependencies = [ "bitflags", ] @@ -390,9 +427,9 @@ dependencies = [ [[package]] name = "rustc-demangle" -version = "0.1.19" +version = "0.1.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "410f7acf3cb3a44527c5d9546bad4bf4e6c460915d5f9f2fc524498bfe8f70ce" +checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342" [[package]] name = "strsim" @@ -402,9 +439,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "syn" -version = "1.0.73" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f71489ff30030d2ae598524f61326b902466f72a0fb1a8564c001cc63425bcc7" +checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" dependencies = [ "proc-macro2", "quote", @@ -413,9 +450,9 @@ dependencies = [ [[package]] name = "synstructure" -version = "0.12.4" +version = "0.12.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b834f2d66f734cb897113e34aaff2f1ab4719ca946f9a7358dba8f8064148701" +checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f" dependencies = [ "proc-macro2", "quote", @@ -458,6 +495,12 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + [[package]] name = "wait-timeout" version = "0.2.0" @@ -502,13 +545,4 @@ dependencies = [ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" - -[[package]] -name = "yaml-rust" -version = "0.4.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" -dependencies = [ - "linked-hash-map", -] +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index a0ec776..b213128 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,7 @@ required-features = ["build-bin"] build-bin = ["clap"] [dependencies] -clap = { version = "3.0.10", features = ["yaml", "cargo"], optional = true } +clap = { version = "3.0.10", features = ["derive"], optional = true } lang-c = "0.11.0" itertools = "0.10.3" failure = "0.1.8" diff --git a/bin/fuzz.rs b/bin/fuzz.rs index 90bd546..1588010 100644 --- a/bin/fuzz.rs +++ b/bin/fuzz.rs @@ -1,58 +1,90 @@ -use clap::{crate_authors, crate_description, crate_version, load_yaml, App}; +use clap::Parser; extern crate kecc; use std::path::Path; +#[derive(Debug, Parser)] +#[clap(name = "fuzz", version, author, about)] +struct FuzzCli { + /// Fuzzes C AST Printer + #[clap(short, long)] + print: bool, + + /// Fuzzes irgen + #[clap(short, long)] + irgen: bool, + + /// Fuzzes irparse + #[clap(long)] + irparse: bool, + + /// Performs simplify-cfg + #[clap(long = "simplify-cfg")] + simplify_cfg: bool, + + /// Performs mem2reg + #[clap(long)] + mem2reg: bool, + + /// Performs deadcode elimination + #[clap(long)] + deadcode: bool, + + /// Performs gvn + #[clap(long)] + gvn: bool, + + /// Fuzzes irgen, optimize and asmgen pipeline + #[clap(long = "end-to-end")] + end_to_end: bool, + + /// Sets the input file to use + input: String, +} + fn main() { - let yaml = load_yaml!("fuzz_cli.yml"); - #[allow(deprecated)] - let matches = App::from_yaml(yaml) - .version(crate_version!()) - .about(crate_description!()) - .author(crate_authors!(", ")) - .get_matches(); + let matches = FuzzCli::parse(); + let input = matches.input; - let input = matches.value_of("INPUT").unwrap(); - - if matches.is_present("print") { + if matches.print { kecc::test_write_c(Path::new(&input)); return; } - if matches.is_present("irgen") { + if matches.irgen { kecc::test_irgen(Path::new(&input)); return; } - if matches.is_present("irparse") { + if matches.irparse { kecc::test_irparse(Path::new(&input)); return; } - if matches.is_present("simplify-cfg") { + if matches.simplify_cfg { todo!("test simplify-cfg"); } - if matches.is_present("mem2erg") { + if matches.mem2reg { todo!("test mem2reg"); } - if matches.is_present("deadcode") { + if matches.deadcode { todo!("test deadcode"); } - if matches.is_present("gvn") { + if matches.gvn { todo!("test gvn"); } - if matches.is_present("end-to-end") { + if matches.end_to_end { kecc::test_end_to_end(Path::new(&input)); return; } assert_eq!( - Path::new(input).extension(), + Path::new(&input).extension(), Some(std::ffi::OsStr::new("ir")) ); kecc::test_asmgen(Path::new(&input)); diff --git a/bin/fuzz_cli.yml b/bin/fuzz_cli.yml deleted file mode 100644 index 3a77567..0000000 --- a/bin/fuzz_cli.yml +++ /dev/null @@ -1,32 +0,0 @@ -name: fuzz -args: - - print: - short: p - long: print - help: Fuzzes C AST printer - - irgen: - short: i - long: irgen - help: Fuzzes irgen - - irparse: - long: irparse - help: Fuzzes irparse - - simplify-cfg: - long: simplify-cfg - help: Performs simplify-cfg - - mem2reg: - long: mem2reg - help: Performs mem2reg - - deadcode: - long: deadcode - help: Performs deadcode elimination - - gvn: - long: gvn - help: Performs gvn - - end-to-end: - long: end-to-end - help: Fuzzes irgen, optimize and asmgen pipeline - - INPUT: - help: Sets the input file to use - required: true - index: 1 diff --git a/bin/kecc.rs b/bin/kecc.rs index c52d71c..51ca8ae 100644 --- a/bin/kecc.rs +++ b/bin/kecc.rs @@ -1,7 +1,7 @@ use std::ffi::OsStr; use std::path::Path; -use clap::{crate_authors, crate_description, crate_version, load_yaml, App}; +use clap::Parser; use lang_c::ast::TranslationUnit; @@ -13,19 +13,71 @@ use kecc::{ Translate, O1, }; +#[derive(Debug, Parser)] +#[clap(name = "kecc", version, author, about)] +struct KeccCli { + /// Parses the input C file + #[clap(long)] + parse: bool, + + /// Prints the input AST + #[clap(short, long)] + print: bool, + + /// Generates IR + #[clap(short, long)] + irgen: bool, + + /// Parses the input IR file + #[clap(long)] + irparse: bool, + + /// Prints the input IR AST + #[clap(long)] + irprint: bool, + + /// Executes the input file + #[clap(long)] + irrun: bool, + + /// Optimizes IR + #[clap(short = 'O', long)] + optimize: bool, + + /// Performs simplify-cfg + #[clap(long = "simplify-cfg")] + simplify_cfg: bool, + + /// Performs mem2reg + #[clap(long)] + mem2reg: bool, + + /// Performs deadcode elimination + #[clap(long)] + deadcode: bool, + + /// Performs gvn + #[clap(long)] + gvn: bool, + + /// Prints the output IR + #[clap(long)] + iroutput: bool, + + /// Sets the output file to use + #[clap(short, long, value_name = "FILE")] + output: Option, + + /// Sets the input file to use + input: String, +} + fn main() { - let yaml = load_yaml!("kecc_cli.yml"); - #[allow(deprecated)] - let matches = App::from_yaml(yaml) - .version(crate_version!()) - .about(crate_description!()) - .author(crate_authors!(", ")) - .get_matches(); + let matches = KeccCli::parse(); + let input = Path::new(&matches.input); - let input = matches.value_of("INPUT").unwrap(); - let input = Path::new(input); + let output = matches.output.clone().unwrap_or_else(|| "-".to_string()); - let output = matches.value_of("output").unwrap_or("-"); let mut output: Box = if output == "-" { Box::new(::std::io::stdout()) } else { @@ -44,16 +96,12 @@ fn main() { } } -fn compile_c( - input: &TranslationUnit, - output: &mut dyn ::std::io::Write, - matches: &clap::ArgMatches, -) { - if matches.is_present("parse") { +fn compile_c(input: &TranslationUnit, output: &mut dyn ::std::io::Write, matches: &KeccCli) { + if matches.parse { return; } - if matches.is_present("print") { + if matches.print { write(input, output).unwrap(); return; } @@ -66,7 +114,7 @@ fn compile_c( } }; - if matches.is_present("irgen") { + if matches.irgen { write(&ir, output).unwrap(); return; } @@ -77,43 +125,43 @@ fn compile_c( fn compile_ir( input: &mut ir::TranslationUnit, output: &mut dyn ::std::io::Write, - matches: &clap::ArgMatches, + matches: &KeccCli, ) { - if matches.is_present("irparse") { + if matches.irparse { return; } - if matches.is_present("irprint") { + if matches.irprint { write(input, output).unwrap(); return; } - if matches.is_present("optimize") { + if matches.optimize { O1::default().optimize(input); } else { - if matches.is_present("simplify-cfg") { + if matches.simplify_cfg { SimplifyCfg::default().optimize(input); } - if matches.is_present("mem2reg") { + if matches.mem2reg { Mem2reg::default().optimize(input); } - if matches.is_present("deadcode") { + if matches.deadcode { Deadcode::default().optimize(input); } - if matches.is_present("gvn") { + if matches.gvn { Gvn::default().optimize(input); } } - if matches.is_present("iroutput") { + if matches.iroutput { write(input, output).unwrap(); return; } - if matches.is_present("irrun") { + if matches.irrun { let result = ir::interp(input, Vec::new()).unwrap(); let (value, width, is_signed) = result.get_int().expect("non-integer value occurs"); assert_eq!(width, 32); diff --git a/bin/kecc_cli.yml b/bin/kecc_cli.yml deleted file mode 100644 index b2e4696..0000000 --- a/bin/kecc_cli.yml +++ /dev/null @@ -1,51 +0,0 @@ -name: kecc -args: - - parse: - long: parse - help: Parses the input C file - - print: - short: p - long: print - help: Prints the input AST - - irgen: - short: i - long: irgen - help: Generates IR - - irparse: - long: irparse - help: Parses the input IR file - - irprint: - long: irprint - help: Prints the input IR AST - - irrun: - long: irrun - help: Executes the input file - - optimize: - short: O - long: optimize - help: Optimizes IR - - simplify-cfg: - long: simplify-cfg - help: Performs simplify-cfg - - mem2reg: - long: mem2reg - help: Performs mem2reg - - deadcode: - long: deadcode - help: Performs deadcode elimination - - gvn: - long: gvn - help: Performs gvn - - iroutput: - long: iroutput - help: Prints the output IR - - output: - short: o - long: output - value_name: FILE - help: Sets the output file to use - takes_value: true - - INPUT: - help: Sets the input file to use - required: true - index: 1