🧪 Add evaluation tests to ensure that passes retain NGR semantics. (#2)

This change adds `Arbitrary` instances to the key IR data types (both as
syntax and as native IR), as well as evaluator functions for both. This
way, we can ensure that the evaluation of one version of the IR is
observationally equivalent to another version of the IR, or even a later
IR. It also adds a similar ability through both static file compilation
and the JIT, to ensure that the translation through Cranelift and our
runtime works as expected.

This actually found a couple issues in its creation, and I hope is
helpful extensions into more interesting programs.
This commit is contained in:
2023-04-16 16:07:45 -07:00
parent 99fb12910f
commit f4594bf2cc
20 changed files with 610 additions and 22 deletions

View File

@@ -4,6 +4,7 @@ use cranelift_jit::JITBuilder;
use cranelift_module::{FuncId, Linkage, Module, ModuleResult};
use std::collections::HashMap;
use std::ffi::CStr;
use std::fmt::Write;
use target_lexicon::Triple;
use thiserror::Error;
@@ -12,16 +13,21 @@ pub struct RuntimeFunctions {
_referenced_functions: Vec<String>,
}
#[derive(Debug, Error)]
#[derive(Debug, Error, PartialEq)]
pub enum RuntimeFunctionError {
#[error("Could not find runtime function named '{0}'")]
CannotFindRuntimeFunction(String),
}
extern "C" fn runtime_print(name: *const i8, value: u64) {
extern "C" fn runtime_print(output_buffer: *mut String, name: *const i8, value: i64) {
let cstr = unsafe { CStr::from_ptr(name) };
let reconstituted = cstr.to_string_lossy();
println!("{} = {}", reconstituted, value);
if let Some(output_buffer) = unsafe { output_buffer.as_mut() } {
writeln!(output_buffer, "{} = {}i64", reconstituted, value).unwrap();
} else {
println!("{} = {}", reconstituted, value);
}
}
impl RuntimeFunctions {
@@ -36,7 +42,7 @@ impl RuntimeFunctions {
"print",
Linkage::Import,
&Signature {
params: vec![string_param, int64_param],
params: vec![string_param, string_param, int64_param],
returns: vec![],
call_conv: CallConv::triple_default(platform),
},