-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcli.rs
executable file
·210 lines (191 loc) · 6.56 KB
/
cli.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
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
use ::parser::parse_program;
use clap::Parser as ClapParser;
use clap::*;
use codegen_llvm::CodeGenLLVM;
use codegen_llvm::opts::CodeGenLLVMOptions;
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
pub enum ClapOptimizationLevel {
None,
O1,
O2,
O3,
}
impl ClapOptimizationLevel {
pub fn as_integer(&self) -> i32 {
match self {
ClapOptimizationLevel::None => 0,
ClapOptimizationLevel::O1 => 1,
ClapOptimizationLevel::O2 => 2,
ClapOptimizationLevel::O3 => 3,
}
}
}
#[derive(Parser, Debug, Clone)]
pub struct ClapCompilerOptions {
#[clap(long, value_enum, default_value_t = ClapOptimizationLevel::None, help = "Set optimization level")]
optimization_level: ClapOptimizationLevel,
#[clap(long, value_name = "PATH", help = "Add a library search path")]
library_path: Vec<String>,
#[clap(long = "library", value_name = "LIB", help = "Link a library")]
libraries: Vec<String>,
#[clap(
long = "build-dir",
value_name = "PATH",
default_value = "./build",
help = "Specifies the directory where build artifacts will be stored.
This includes compiled binaries, intermediate files, and other outputs
generated during the build process. If not provided, a default directory \
(e.g., './build') will be used."
)]
build_dir: String,
}
#[derive(clap::Parser, Clone)]
#[command()]
struct Args {
#[command(subcommand)]
cmd: Commands,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
enum DumpType {
Ir,
Asm,
}
#[derive(clap::Subcommand, Debug, Clone)]
enum Commands {
#[clap(about = "Execute a compiled program")]
Run {
file_path: String,
#[clap(flatten)]
compiler_options: ClapCompilerOptions,
},
#[clap(about = "Dump LLVM-ir or Assembly code")]
Dump {
file_path: String,
dump_type: DumpType,
output_path: String,
#[clap(flatten)]
compiler_options: ClapCompilerOptions,
},
#[clap(about = "Compile source code into an executable")]
Build {
file_path: String,
output_path: String,
#[clap(flatten)]
compiler_options: ClapCompilerOptions,
},
#[clap(about = "Generate an object file")]
Obj {
file_path: String,
output_path: String,
#[clap(flatten)]
compiler_options: ClapCompilerOptions,
},
#[clap(about = "Generate a dynamic library (shared object)")]
Dylib {
file_path: String,
output_path: String,
#[clap(flatten)]
compiler_options: ClapCompilerOptions,
},
#[clap(about = "Print version information")]
Version,
}
macro_rules! init_compiler {
($context:expr, $file_path:expr, $opts:expr) => {{
let (program, file_name) = parse_program($file_path.clone());
let codegen_llvm = match CodeGenLLVM::new($context, $file_path, file_name.clone(), program, $opts) {
Ok(instance) => instance,
Err(err) => {
eprintln!("(cyrus) Creating CodeGenLLVM instance failed: ");
eprintln!("{}", err.to_str().unwrap());
std::process::exit(1);
}
};
codegen_llvm
}};
}
pub fn main() {
let context = CodeGenLLVM::new_context();
let version = env!("CARGO_PKG_VERSION");
let args = Args::parse();
match args.cmd {
Commands::Run {
file_path,
compiler_options,
} => {
let mut codegen_llvm = init_compiler!(&context, file_path.clone(), CodeGenLLVMOptions {
optimization_level: compiler_options.optimization_level.as_integer(),
library_path: compiler_options.library_path,
libraries: compiler_options.libraries,
build_dir: compiler_options.build_dir,
});
codegen_llvm.compile();
codegen_llvm.execute();
}
Commands::Dump {
file_path,
dump_type,
output_path,
compiler_options,
} => {
let mut codegen_llvm = init_compiler!(&context, file_path.clone(), CodeGenLLVMOptions {
optimization_level: compiler_options.optimization_level.as_integer(),
library_path: compiler_options.library_path,
libraries: compiler_options.libraries,
build_dir: compiler_options.build_dir,
});
codegen_llvm.compile();
match dump_type {
DumpType::Ir => codegen_llvm.emit_llvm_ir(output_path),
DumpType::Asm => codegen_llvm.emit_asm(output_path),
}
}
Commands::Build {
file_path,
output_path,
compiler_options,
} => {
let mut codegen_llvm = init_compiler!(&context, file_path.clone(), CodeGenLLVMOptions {
optimization_level: compiler_options.optimization_level.as_integer(),
library_path: compiler_options.library_path,
libraries: compiler_options.libraries,
build_dir: compiler_options.build_dir,
});
// compiler.compile();
// compiler.make_executable_file(output_path);
}
Commands::Obj {
file_path,
output_path,
compiler_options,
} => {
// let mut compiler = init_compiler!(file_path.clone());
// compiler.set_opts(CompilerOptions {
// optimization_level: compiler_options.optimization_level.as_integer(),
// library_path: compiler_options.library_path,
// libraries: compiler_options.libraries,
// build_dir: compiler_options.build_dir,
// });
// compiler.compile();
// compiler.make_object_file(output_path);
}
Commands::Dylib {
file_path,
output_path,
compiler_options,
} => {
// let mut compiler = init_compiler!(file_path.clone());
// compiler.set_opts(CompilerOptions {
// optimization_level: compiler_options.optimization_level.as_integer(),
// library_path: compiler_options.library_path,
// libraries: compiler_options.libraries,
// build_dir: compiler_options.build_dir,
// });
// compiler.compile();
// compiler.make_dynamic_library(output_path);
}
Commands::Version => {
println!("Cyrus {}", version)
}
}
}