forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
298 lines (265 loc) · 9.33 KB
/
Copy pathmain.rs
File metadata and controls
298 lines (265 loc) · 9.33 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//! The `fe` command-line interface.
use std::fs;
use std::io::{Error, Write};
use std::path::Path;
use clap::{arg_enum, values_t, App, Arg};
use fe_common::diagnostics::print_diagnostics;
use fe_common::files::SourceFileId;
use fe_common::panic::install_panic_hook;
use fe_driver::{CompiledModule, Db};
use walkdir::WalkDir;
const DEFAULT_OUTPUT_DIR_NAME: &str = "output";
const VERSION: &str = env!("CARGO_PKG_VERSION");
arg_enum! {
#[derive(PartialEq, Debug)]
pub enum CompilationTarget {
Abi,
Ast,
LoweredAst,
Bytecode,
Tokens,
Yul,
}
}
pub fn main() {
install_panic_hook();
let matches = App::new("Fe")
.version(VERSION)
.about("Compiler for the Fe language")
.arg(
Arg::with_name("input")
.help("The input source file to use e.g erc20.fe")
.index(1)
.required(true),
)
.arg(
Arg::with_name("output-dir")
.short("o")
.long("output-dir")
.help("The directory to store the compiler output e.g /tmp/output")
.takes_value(true)
.default_value(DEFAULT_OUTPUT_DIR_NAME),
)
.arg(
Arg::with_name("emit")
.short("e")
.long("emit")
.help("Comma separated compile targets e.g. -e=bytecode,yul")
.possible_values(&["abi", "bytecode", "ast", "tokens", "yul", "loweredAst"])
.default_value("abi,bytecode")
.use_delimiter(true)
.takes_value(true),
)
.arg(
Arg::with_name("overwrite")
.long("overwrite")
.help("Overwrite contents of output directory`"),
)
.arg(
Arg::with_name("optimize")
.long("optimize")
.help("Enables the Yul optimizer`")
.possible_values(&["true", "false"])
.default_value("true")
.use_delimiter(false)
.takes_value(true),
)
.arg(
Arg::with_name("mir")
.long("mir")
.help("dump mir dot file")
.takes_value(false),
)
.get_matches();
let input_path = matches.value_of("input").unwrap();
let output_dir = matches.value_of("output-dir").unwrap();
let overwrite = matches.is_present("overwrite");
let optimize = matches.value_of("optimize") == Some("true");
let targets =
values_t!(matches.values_of("emit"), CompilationTarget).unwrap_or_else(|e| e.exit());
let with_bytecode = targets.contains(&CompilationTarget::Bytecode);
if matches.is_present("mir") {
return mir_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 = Db::default();
let (content, compiled_module) = 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,
};
let compiled_module = match fe_driver::compile_single_file(
&mut db,
input_path,
&content,
with_bytecode,
optimize,
) {
Ok(module) => module,
Err(error) => {
eprintln!("Unable to compile {}.", input_path);
print_diagnostics(&db, &error.0);
std::process::exit(1)
}
};
(content, compiled_module)
} else {
if !Path::new(input_path).exists() {
eprintln!("Input directory does not exist: `{}`.", input_path);
std::process::exit(1)
}
let files = match load_files_from_dir(input_path) {
Ok(files) if files.is_empty() => {
eprintln!("Input directory is not an ingot: `{}`", input_path);
std::process::exit(1)
}
Ok(files) => files,
Err(err) => {
eprintln!("Failed to load project files. Error: {}", err);
std::process::exit(1)
}
};
let compiled_module = match fe_driver::compile_ingot(
&mut db,
"main", // TODO: real ingot name
&files,
with_bytecode,
optimize,
) {
Ok(module) => module,
Err(error) => {
eprintln!("Unable to compile {}.", input_path);
print_diagnostics(&db, &error.0);
std::process::exit(1)
}
};
// no file content for ingots
("".to_string(), compiled_module)
};
match write_compiled_module(compiled_module, &content, &targets, output_dir, overwrite) {
Ok(_) => println!("Compiled {}. Outputs in `{}`", input_path, output_dir),
Err(err) => {
eprintln!(
"Failed to write output to directory: `{}`. Error: {}",
output_dir, err
);
std::process::exit(1)
}
}
}
fn load_files_from_dir(dir_path: &str) -> Result<Vec<(String, String)>, std::io::Error> {
let entries = WalkDir::new(dir_path);
let mut files = vec![];
for entry in entries.into_iter() {
let entry = entry?;
let path = entry.path();
if path.is_file() && path.extension().and_then(std::ffi::OsStr::to_str) == Some("fe") {
let content = std::fs::read_to_string(path)?;
files.push((path.to_string_lossy().to_string(), content));
}
}
Ok(files)
}
fn write_compiled_module(
mut module: CompiledModule,
file_content: &str,
targets: &[CompilationTarget],
output_dir: &str,
overwrite: bool,
) -> Result<(), String> {
let output_dir = Path::new(output_dir);
if output_dir.is_file() {
return Err(format!(
"A file exists at path `{}`, the location of the output directory. Refusing to overwrite.",
output_dir.display()
));
}
if !overwrite {
verify_nonexistent_or_empty(output_dir)?;
}
fs::create_dir_all(output_dir).map_err(ioerr_to_string)?;
if targets.contains(&CompilationTarget::Ast) {
write_output(&output_dir.join("module.ast"), &module.src_ast)?;
}
if targets.contains(&CompilationTarget::LoweredAst) {
write_output(&output_dir.join("lowered_module.ast"), &module.lowered_ast)?;
}
if targets.contains(&CompilationTarget::Tokens) {
let tokens = {
let lexer = fe_parser::lexer::Lexer::new(SourceFileId::dummy_file(), file_content);
lexer.collect::<Vec<_>>()
};
write_output(&output_dir.join("module.tokens"), &format!("{:#?}", tokens))?;
}
for (name, contract) in module.contracts.drain(0..) {
let contract_output_dir = output_dir.join(&name);
fs::create_dir_all(&contract_output_dir).map_err(ioerr_to_string)?;
if targets.contains(&CompilationTarget::Abi) {
let file_name = format!("{}_abi.json", &name);
write_output(&contract_output_dir.join(file_name), &contract.json_abi)?;
}
if targets.contains(&CompilationTarget::Yul) {
let file_name = format!("{}_ir.yul", &name);
write_output(&contract_output_dir.join(file_name), &contract.yul)?;
}
#[cfg(feature = "solc-backend")]
if targets.contains(&CompilationTarget::Bytecode) {
let file_name = format!("{}.bin", &name);
write_output(&contract_output_dir.join(file_name), &contract.bytecode)?;
}
}
Ok(())
}
fn write_output(path: &Path, content: &str) -> Result<(), String> {
let mut file = fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(path)
.map_err(ioerr_to_string)?;
file.write_all(content.as_bytes())
.map_err(ioerr_to_string)?;
Ok(())
}
fn ioerr_to_string(error: Error) -> String {
format!("{}", error)
}
fn verify_nonexistent_or_empty(dir: &Path) -> Result<(), String> {
if !dir.exists() || dir.read_dir().map_err(ioerr_to_string)?.next().is_none() {
Ok(())
} else {
Err(format!(
"Directory '{}' is not empty. Use --overwrite to overwrite.",
dir.display()
))
}
}
fn mir_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_mir_single_file(&mut db, input_path, &content) {
Ok(text) => println!("{}", text),
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)
}
}