-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.rs
176 lines (159 loc) · 3.95 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#![allow(dead_code)]
#![feature(assert_matches)]
#![feature(iter_intersperse)]
#![feature(map_try_insert)]
mod attribute;
mod block_emitter;
mod builtin;
mod ccpu;
mod cmdline;
mod compile;
mod constant;
mod ctype;
mod deconstruct;
mod enums;
mod error;
mod flush;
mod function;
mod generic_ir;
mod graph;
mod initializer;
mod ir;
mod lvalue;
mod machine;
mod name_scope;
mod object_location;
mod offsetof;
mod opt;
mod preprocess;
mod regalloc;
mod register;
mod rvalue;
mod ssa;
mod stats;
mod string;
mod struct_union;
mod translation_unit;
mod type_builder;
mod utils;
#[cfg(test)]
mod test;
#[macro_use]
extern crate static_assertions;
#[macro_use]
extern crate lazy_static;
use std::fs::File;
use std::io::Write;
use std::process::exit;
use crate::{error::ErrorCollector, translation_unit::TranslationUnit};
use cmdline::{Cli, Standard};
use lang_c::driver::{parse, Flavor};
impl From<Standard> for Flavor {
fn from(s: Standard) -> Self {
match s {
Standard::C11 => Flavor::StdC11,
Standard::Gnu11 => Flavor::GnuC11,
Standard::Clang11 => Flavor::ClangC11,
}
}
}
fn main() {
let cli = Cli::parse();
let input = cli.get_input().clone();
let cfg = preprocess::get_config(
cli.get_dialect().into(),
cli.define,
cli.include,
cli.isystem,
cli.iquote,
);
let output_path = if let Some(output) = cli.output {
output
} else {
let mut output = input.clone();
if cli.produce_assembly {
output.set_extension("asm");
} else {
output.set_extension("o");
}
output
};
let p = match parse(&cfg, input) {
Ok(p) => p,
Err(e) => {
println!("{}", e);
exit(1);
}
};
let mut ec = ErrorCollector::new();
if cli.verbose {
println!("========== TRANSLATE ===========");
}
let tu = TranslationUnit::translate(p.unit, &mut ec);
ec.print_issues_src(&p.source);
if let Ok(mut tu) = tu {
if cli.verbose {
println!("{}", tu);
println!("========== ENFORCE SSA ===========");
}
tu.enforce_ssa();
if cli.verbose {
println!("{}", tu);
println!("========== OPTIMIZE SSA ===========");
}
tu.optimize_ssa();
if cli.verbose {
println!("{}", tu);
println!("========== UTILISE INTRINSIC CALLS ===========");
}
tu.utilise_intrin_calls();
if cli.verbose {
println!("{}", tu);
println!("========== ENFORCE CALL REGS ===========");
}
tu.enforce_special_regs();
if cli.verbose {
println!("{}", tu);
println!("========== DECONSTRUCT SSA ===========");
}
let mut tu = tu.deconstruct_ssa();
if cli.verbose {
println!("{}", tu);
println!("========== OPTIMIZE DECONSTRUCTED ===========");
}
tu.optimize_deconstructed();
if cli.verbose {
println!("{}", tu);
println!("========== GENERATE ===========");
}
let mut ec = ErrorCollector::new();
let r = ccpu::gen::gen_tu(tu, &mut ec);
ec.print_issues_src(&p.source);
let (w, stats) = if let Ok(r) = r {
r
} else {
exit(1);
};
if cli.verbose {
println!("{}", w);
}
if cli.show_stats {
stats.print_stats();
}
let mut f = match File::create(output_path.clone()) {
Ok(f) => f,
Err(e) => {
println!("Cannot open {} for writing: {}", output_path.display(), e);
exit(1);
}
};
if cli.produce_assembly {
write!(f, "{}", w).unwrap();
} else {
let obj = ccpu::Object::new(&w);
obj.write_to_file(&mut f).unwrap()
}
} else {
exit(1);
}
}