forked from pku-minic/koopa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
56 lines (51 loc) · 1.57 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
mod const_fold;
mod dce;
use koopa::back::KoopaGenerator;
use koopa::front::Driver;
use koopa::opt::{Pass, PassManager};
use std::env::args;
use std::{fmt, io, process};
fn main() {
if let Err(error) = try_main() {
eprintln!("{}", error);
process::exit(1);
}
}
enum Error {
InvalidFile(io::Error),
InvalidArgs,
Parse,
Io(io::Error),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::InvalidFile(error) => write!(f, "invalid file operation: {}", error),
Error::InvalidArgs => write!(f, "usage: opt INPUT OUTPUT"),
Error::Parse => write!(f, "error occurred when parsing the input"),
Error::Io(error) => write!(f, "IO error: {}", error),
}
}
}
fn try_main() -> Result<(), Error> {
// parse arguments
let mut args = args();
args.next();
let (driver, output) = match (args.next(), args.next(), args.next()) {
(Some(input), Some(output), None) => (
Driver::from_path(input).map_err(Error::InvalidFile)?,
output,
),
_ => return Err(Error::InvalidArgs),
};
// parse input file
let mut program = driver.generate_program().map_err(|_| Error::Parse)?;
// run passes
let mut passman = PassManager::new();
passman.register(Pass::Function(Box::new(const_fold::ConstantFolding::new())));
passman.register(Pass::Function(Box::new(dce::DeadCodeElimination::new())));
passman.run_passes(&mut program);
// dump the output
let mut generator = KoopaGenerator::from_path(output).map_err(Error::InvalidFile)?;
generator.generate_on(&program).map_err(Error::Io)
}