Add serde functionality into lib. (#422)

* Adds decoration to the verifier errors.

example:

function %bad(i32) fast {
ebb0(v0: i32):
    brnz.i32 v0, ebb1
    return
    ^~~~~~

verifier inst1: Internal return not allowed with return_at_end=1

ebb1:
    trapz.i32 v0, user6
    return
}

Fixes #68

* Making it so that pretty_function_error and write_function_plain are both private.

* Changes write_ebb to decorate_ebb.
Adds documentation line to decorate_function.

* Removing Cargo.toml lib/serde addition

* Add serde functionality into lib.

* Fix so code is compatible with Rust version 1.25.0.

* Move ser/de functions to utility file, update description, remove borrow from arms.

* Remove commented out code.
This commit is contained in:
Caroline Cullen
2018-07-31 07:48:12 -07:00
committed by Dan Gohman
parent 13fea26c95
commit 65a1a6bb28
5 changed files with 991 additions and 0 deletions

121
lib/serde/src/clif-json.rs Normal file
View File

@@ -0,0 +1,121 @@
#![deny(trivial_numeric_casts)]
#![warn(unused_import_braces, unstable_features, unused_extern_crates)]
#![cfg_attr(
feature = "cargo-clippy",
warn(
float_arithmetic, mut_mut, nonminimal_bool, option_map_unwrap_or, option_map_unwrap_or_else,
unicode_not_nfc, use_self
)
)]
extern crate cranelift_reader;
extern crate docopt;
#[macro_use]
extern crate serde_derive;
extern crate cranelift_codegen;
extern crate serde_json;
use cranelift_reader::parse_functions;
use docopt::Docopt;
use std::fs::File;
use std::io::prelude::*;
use std::io::{self, Write};
use std::process;
use std::vec::Vec;
mod serde_clif_json;
const USAGE: &str = "
Cranelift JSON serializer/deserializer utility
Usage:
clif-json serialize [-p] <file>
clif-json deserialize <file>
Options:
-p, --pretty print pretty json
";
#[derive(Deserialize, Debug)]
struct Args {
cmd_serialize: bool,
cmd_deserialize: bool,
flag_pretty: bool,
arg_file: Vec<String>,
}
/// A command either succeeds or fails with an error message.
pub type CommandResult = Result<(), String>;
fn call_ser(file: &str, pretty: bool) -> CommandResult {
let ret_of_parse = parse_functions(file);
match ret_of_parse {
Ok(funcs) => {
let ser_funcs = serde_clif_json::SerObj::new(&funcs);
let ser_str: String;
if pretty {
ser_str = serde_json::to_string_pretty(&ser_funcs).unwrap();
} else {
ser_str = serde_json::to_string(&ser_funcs).unwrap();
}
println!("{}", ser_str);
Ok(())
}
Err(_pe) => Err(format!("this was a parsing error")),
}
}
fn call_de(file: &File) -> CommandResult {
let de: serde_clif_json::SerObj = match serde_json::from_reader(file) {
Result::Ok(val) => val,
Result::Err(err) => panic!("{}", err),
};
println!("{:?}", de);
Ok(())
}
/// Parse the command line arguments and run the requested command.
fn clif_json() -> CommandResult {
// Parse command line arguments.
let args: Args = Docopt::new(USAGE)
.and_then(|d| d.help(true).deserialize())
.unwrap_or_else(|e| e.exit());
// Find the sub-command to execute.
let result = if args.cmd_serialize {
if let Some(first_file) = args.arg_file.first() {
let mut file = File::open(first_file).expect("Unable to open the file");
let mut contents = String::new();
file.read_to_string(&mut contents)
.expect("Unable to read the file");
call_ser(&contents, args.flag_pretty)
} else {
Err(format!("No file was passed"))
}
} else if args.cmd_deserialize {
if let Some(first_file) = args.arg_file.first() {
let mut file = File::open(first_file).expect("Unable to open the file");
call_de(&file)
} else {
Err(format!("No file was passed"))
}
} else {
// Debugging / shouldn't happen with proper command line handling above.
Err(format!("Unhandled args: {:?}", args))
};
result
}
fn main() {
if let Err(mut msg) = clif_json() {
if !msg.ends_with('\n') {
msg.push('\n');
}
io::stdout().flush().expect("flushing stdout");
io::stderr().write_all(msg.as_bytes()).unwrap();
process::exit(1);
}
}