Add support for implicit type conversions to ISLE. This feature allows the DSL user to register to the compiler that a particular term (used as a constructor or extractor) converts from one type to another. The compiler will then *automatically* insert this term whenever a type mismatch involving that specific pair of types occurs. This significantly cleans up many uses of the ISLE DSL. For example, when defining the compiler backends, we often have newtypes like `Gpr` around `Reg` (signifying a particular type of register); we can define a conversion from Gpr to Reg automatically. Conversions can also have side-effects, as long as these side-effects are idempotent. For example, `put_value_in_reg` in a compiler backend has the effect of marking the value as used, causing codegen to produce it, and assigns a register to the value; but multiple invocations of this will return the same register for the same value. Thus it is safe to use it as an implicit conversion that may be invoked multiple times. This is documented in the ISLE-Cranelift integration document. This PR also adds some testing infrastructure to the ISLE compiler, checking that "pass" tests pass through the DSL compiler, "fail" tests do not, and "link" tests are able to generate code and link that code with corresponding Rust code.
64 lines
1.7 KiB
Rust
64 lines
1.7 KiB
Rust
use cranelift_isle::{compile, lexer, parser};
|
|
use miette::{Context, IntoDiagnostic, Result};
|
|
use std::{
|
|
fs,
|
|
io::{self, Write},
|
|
path::PathBuf,
|
|
};
|
|
use structopt::StructOpt;
|
|
|
|
#[derive(StructOpt)]
|
|
struct Opts {
|
|
/// The output file to write the generated Rust code to. `stdout` is used if
|
|
/// this is not given.
|
|
#[structopt(short, long, parse(from_os_str))]
|
|
output: Option<PathBuf>,
|
|
|
|
/// The input ISLE DSL source files.
|
|
#[structopt(parse(from_os_str), required(true))]
|
|
inputs: Vec<PathBuf>,
|
|
}
|
|
|
|
fn main() -> Result<()> {
|
|
let _ = env_logger::try_init();
|
|
|
|
let _ = miette::set_hook(Box::new(|_| {
|
|
Box::new(
|
|
miette::MietteHandlerOpts::new()
|
|
// `miette` mistakenly uses braille-optimized output for emacs's
|
|
// `M-x shell`.
|
|
.force_graphical(true)
|
|
.build(),
|
|
)
|
|
}));
|
|
|
|
let opts = Opts::from_args();
|
|
|
|
let lexer = lexer::Lexer::from_files(opts.inputs)?;
|
|
let defs = parser::parse(lexer)?;
|
|
let code = compile::compile(&defs)?;
|
|
|
|
let stdout = io::stdout();
|
|
let (mut output, output_name): (Box<dyn Write>, _) = match &opts.output {
|
|
Some(f) => {
|
|
let output = Box::new(
|
|
fs::File::create(f)
|
|
.into_diagnostic()
|
|
.with_context(|| format!("failed to create '{}'", f.display()))?,
|
|
);
|
|
(output, f.display().to_string())
|
|
}
|
|
None => {
|
|
let output = Box::new(stdout.lock());
|
|
(output, "<stdout>".to_string())
|
|
}
|
|
};
|
|
|
|
output
|
|
.write_all(code.as_bytes())
|
|
.into_diagnostic()
|
|
.with_context(|| format!("failed to write to '{}'", output_name))?;
|
|
|
|
Ok(())
|
|
}
|