mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-02 08:11:12 +00:00
Add a basic CLI
This commit is contained in:
parent
70936841c8
commit
5f817d8019
26 changed files with 2461 additions and 291 deletions
272
cli/src/main.rs
272
cli/src/main.rs
|
@ -1,15 +1,32 @@
|
|||
extern crate roc;
|
||||
extern crate roc_gen;
|
||||
|
||||
use roc::eval::Evaluated::*;
|
||||
use roc::eval::{call, eval, Evaluated};
|
||||
use roc::expr::Expr;
|
||||
use roc::parse;
|
||||
use roc::region::{Located, Region};
|
||||
use crate::helpers::{infer_expr, uniq_expr_with};
|
||||
use bumpalo::Bump;
|
||||
use inkwell::context::Context;
|
||||
use inkwell::module::Linkage;
|
||||
use inkwell::passes::PassManager;
|
||||
use inkwell::types::BasicType;
|
||||
use inkwell::OptimizationLevel;
|
||||
use roc_collections::all::ImMap;
|
||||
use roc_gen::llvm::build::{build_proc, build_proc_header, get_call_conventions};
|
||||
use roc_gen::llvm::convert::basic_type_from_layout;
|
||||
use roc_mono::expr::{Expr, Procs};
|
||||
use roc_mono::layout::Layout;
|
||||
use std::time::SystemTime;
|
||||
|
||||
use inkwell::targets::{
|
||||
CodeModel, FileType, InitializationConfig, RelocMode, Target, TargetTriple,
|
||||
};
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::io::prelude::*;
|
||||
use std::path::Path;
|
||||
use target_lexicon::Triple;
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
pub mod helpers;
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
let now = SystemTime::now();
|
||||
let argv = std::env::args().into_iter().collect::<Vec<String>>();
|
||||
|
||||
match argv.get(1) {
|
||||
|
@ -19,9 +36,15 @@ fn main() -> std::io::Result<()> {
|
|||
|
||||
file.read_to_string(&mut contents)?;
|
||||
|
||||
let expr = parse::parse_string(contents.as_str()).unwrap();
|
||||
let dest_filename = Path::new(filename).with_extension("o");
|
||||
|
||||
process_task(eval(expr))
|
||||
gen(contents.as_str(), Triple::host(), &dest_filename);
|
||||
|
||||
let end_time = now.elapsed().unwrap();
|
||||
|
||||
println!("Finished in {} ms\n", end_time.as_millis());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
None => {
|
||||
println!("Usage: roc FILENAME.roc");
|
||||
|
@ -31,109 +54,158 @@ fn main() -> std::io::Result<()> {
|
|||
}
|
||||
}
|
||||
|
||||
fn process_task(evaluated: Evaluated) -> std::io::Result<()> {
|
||||
match evaluated {
|
||||
EvalError(region, problem) => {
|
||||
println!(
|
||||
"\n\u{001B}[4mruntime error\u{001B}[24m\n\n{} at {}\n",
|
||||
format!("{}", problem),
|
||||
format!("line {}, column {}", region.start_line, region.start_col)
|
||||
);
|
||||
fn gen(src: &str, target: Triple, dest_filename: &Path) {
|
||||
// Build the expr
|
||||
let arena = Bump::new();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
ApplyVariant(name, Some(mut vals)) => {
|
||||
match name.as_str() {
|
||||
"Echo" => {
|
||||
// Extract the string from the Echo variant.
|
||||
let string_to_be_displayed = match vals.pop() {
|
||||
Some(Str(payload)) => payload,
|
||||
Some(EvalError(region, err)) => {
|
||||
panic!(
|
||||
"RUNTIME ERROR in Echo: {} at {}",
|
||||
format!("{}", err),
|
||||
format!("line {}, column {}", region.start_line, region.start_col)
|
||||
);
|
||||
}
|
||||
Some(val) => {
|
||||
panic!("TYPE MISMATCH in Echo: {}", format!("{}", val));
|
||||
}
|
||||
None => {
|
||||
panic!("TYPE MISMATCH in Echo: None");
|
||||
}
|
||||
};
|
||||
let (loc_expr, _output, _problems, subs, var, constraint, home, interns) =
|
||||
uniq_expr_with(&arena, src, &ImMap::default());
|
||||
|
||||
// Print the string to the console, since that's what Echo does!
|
||||
println!("{}", string_to_be_displayed);
|
||||
let mut unify_problems = Vec::new();
|
||||
let (content, mut subs) = infer_expr(subs, &mut unify_problems, &constraint, var);
|
||||
|
||||
// Continue with the callback.
|
||||
let callback = vals.pop().unwrap();
|
||||
let context = Context::create();
|
||||
let module = context.create_module("app");
|
||||
let builder = context.create_builder();
|
||||
let fpm = PassManager::create(&module);
|
||||
|
||||
process_task(call(
|
||||
Region {
|
||||
start_line: 0,
|
||||
start_col: 0,
|
||||
end_line: 0,
|
||||
end_col: 0,
|
||||
},
|
||||
callback,
|
||||
vec![with_zero_loc(Expr::EmptyRecord)],
|
||||
))
|
||||
}
|
||||
"Read" => {
|
||||
// Read a line from from stdin, since that's what Read does!
|
||||
let mut input = String::new();
|
||||
roc_gen::llvm::build::add_passes(&fpm);
|
||||
|
||||
io::stdin().read_line(&mut input)?;
|
||||
fpm.initialize();
|
||||
|
||||
// Continue with the callback.
|
||||
let callback = vals.pop().unwrap();
|
||||
// Compute main_fn_type before moving subs to Env
|
||||
let pointer_bytes = target.pointer_width().unwrap().bytes() as u32;
|
||||
let layout = Layout::from_content(&arena, content, &subs, pointer_bytes)
|
||||
.unwrap_or_else(|err| panic!("Code gen error in test: could not convert to layout. Err was {:?} and Subs were {:?}", err, subs));
|
||||
|
||||
process_task(call(
|
||||
Region {
|
||||
start_line: 0,
|
||||
start_col: 0,
|
||||
end_line: 0,
|
||||
end_col: 0,
|
||||
},
|
||||
callback,
|
||||
vec![with_zero_loc(Expr::Str(input.trim().to_string()))],
|
||||
))
|
||||
}
|
||||
"Success" => {
|
||||
// We finished all our tasks. Great! No need to print anything.
|
||||
Ok(())
|
||||
}
|
||||
_ => {
|
||||
// We don't recognize this variant, so display it and exit.
|
||||
display_val(ApplyVariant(name, Some(vals)));
|
||||
let execution_engine = module
|
||||
.create_jit_execution_engine(OptimizationLevel::None)
|
||||
.expect("Error creating JIT execution engine for test");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
output => {
|
||||
// We don't recognize this value, so display it and exit.
|
||||
display_val(output);
|
||||
let ptr_bytes = execution_engine
|
||||
.get_target_data()
|
||||
.get_pointer_byte_size(None);
|
||||
let main_fn_type =
|
||||
basic_type_from_layout(&arena, &context, &layout, ptr_bytes).fn_type(&[], false);
|
||||
let main_fn_name = "$Test.main";
|
||||
|
||||
Ok(())
|
||||
// Compile and add all the Procs before adding main
|
||||
let mut env = roc_gen::llvm::build::Env {
|
||||
arena: &arena,
|
||||
builder: &builder,
|
||||
context: &context,
|
||||
interns,
|
||||
module: arena.alloc(module),
|
||||
ptr_bytes,
|
||||
};
|
||||
let mut procs = Procs::default();
|
||||
let mut ident_ids = env.interns.all_ident_ids.remove(&home).unwrap();
|
||||
|
||||
// Populate Procs and get the low-level Expr from the canonical Expr
|
||||
let main_body = Expr::new(
|
||||
&arena,
|
||||
&mut subs,
|
||||
loc_expr.value,
|
||||
&mut procs,
|
||||
home,
|
||||
&mut ident_ids,
|
||||
pointer_bytes,
|
||||
);
|
||||
|
||||
// Put this module's ident_ids back in the interns, so we can use them in env.
|
||||
env.interns.all_ident_ids.insert(home, ident_ids);
|
||||
|
||||
let mut headers = Vec::with_capacity(procs.len());
|
||||
|
||||
// Add all the Proc headers to the module.
|
||||
// We have to do this in a separate pass first,
|
||||
// because their bodies may reference each other.
|
||||
for (symbol, opt_proc) in procs.as_map().into_iter() {
|
||||
if let Some(proc) = opt_proc {
|
||||
let (fn_val, arg_basic_types) = build_proc_header(&env, symbol, &proc);
|
||||
|
||||
headers.push((proc, fn_val, arg_basic_types));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn with_zero_loc<T>(val: T) -> Located<T> {
|
||||
Located::new(
|
||||
val,
|
||||
Region {
|
||||
start_line: 0,
|
||||
start_col: 0,
|
||||
// Build each proc using its header info.
|
||||
for (proc, fn_val, arg_basic_types) in headers {
|
||||
// NOTE: This is here to be uncommented in case verification fails.
|
||||
// (This approach means we don't have to defensively clone name here.)
|
||||
//
|
||||
// println!("\n\nBuilding and then verifying function {}\n\n", name);
|
||||
build_proc(&env, proc, &procs, fn_val, arg_basic_types);
|
||||
|
||||
end_line: 0,
|
||||
end_col: 0,
|
||||
},
|
||||
)
|
||||
}
|
||||
if fn_val.verify(true) {
|
||||
fpm.run_on(&fn_val);
|
||||
} else {
|
||||
// NOTE: If this fails, uncomment the above println to debug.
|
||||
panic!(
|
||||
"Non-main function failed LLVM verification. Uncomment the above println to debug!"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn display_val(evaluated: Evaluated) {
|
||||
println!("\n\u{001B}[4mroc out\u{001B}[24m\n\n{}\n", evaluated);
|
||||
// Add main to the module.
|
||||
let cc = get_call_conventions(target.default_calling_convention().unwrap());
|
||||
let main_fn = env.module.add_function(main_fn_name, main_fn_type, None);
|
||||
|
||||
main_fn.set_call_conventions(cc);
|
||||
main_fn.set_linkage(Linkage::External);
|
||||
|
||||
// Add main's body
|
||||
let basic_block = context.append_basic_block(main_fn, "entry");
|
||||
|
||||
builder.position_at_end(basic_block);
|
||||
|
||||
let ret = roc_gen::llvm::build::build_expr(
|
||||
&env,
|
||||
&ImMap::default(),
|
||||
main_fn,
|
||||
&main_body,
|
||||
&mut Procs::default(),
|
||||
);
|
||||
|
||||
builder.build_return(Some(&ret));
|
||||
|
||||
// Uncomment this to see the module's un-optimized LLVM instruction output:
|
||||
// env.module.print_to_stderr();
|
||||
|
||||
if main_fn.verify(true) {
|
||||
fpm.run_on(&main_fn);
|
||||
} else {
|
||||
panic!("Function {} failed LLVM verification.", main_fn_name);
|
||||
}
|
||||
|
||||
// Verify the module
|
||||
if let Err(errors) = env.module.verify() {
|
||||
panic!("Errors defining module: {:?}", errors);
|
||||
}
|
||||
|
||||
// Uncomment this to see the module's optimized LLVM instruction output:
|
||||
// env.module.print_to_stderr();
|
||||
|
||||
// Emit
|
||||
Target::initialize_x86(&InitializationConfig::default());
|
||||
|
||||
let opt = OptimizationLevel::Default;
|
||||
let reloc = RelocMode::Default;
|
||||
let model = CodeModel::Default;
|
||||
let target = Target::from_name("x86-64").unwrap();
|
||||
let target_machine = target
|
||||
.create_target_machine(
|
||||
&TargetTriple::create("x86_64-pc-linux-gnu"),
|
||||
"x86-64",
|
||||
"+avx2",
|
||||
opt,
|
||||
reloc,
|
||||
model,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
target_machine
|
||||
.write_to_file(&env.module, FileType::Object, &dest_filename)
|
||||
.expect("Writing .o file failed");
|
||||
|
||||
println!("\nSuccess! 🎉\n\n\t➡ {}\n", dest_filename.display());
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue