Skip to content

Commit

Permalink
Use proper conventions for code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
jblebrun committed Jan 23, 2024
1 parent 354fd66 commit 1e0ea04
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 23 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
/target
wrausmt-runtime/src/instructions/generated
.vscode
4 changes: 1 addition & 3 deletions codegen/master_ops_list.csv
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,9 @@
| _ec.relop::<u64>(|l, r| l >= r)

0x5b ,f32.eq ,()
| #[allow(clippy::float_cmp)]
| _ec.relop::<f32>(|l, r| l == r)

0x5c ,f32.ne ,()
| #[allow(clippy::float_cmp)]
| _ec.relop::<f32>(|l, r| l != r)

0x5d ,f32.lt ,()
Expand Down Expand Up @@ -399,7 +397,7 @@

0x6d ,i32.div_s ,()
| _ec.binop_trap::<i32>(|l, r| {
| if r == 0 {
| if (r == 0) {
| return Err(TrapKind::IntegerDivideByZero);
| }
| if l == i32::MIN && r == -1 {
Expand Down
4 changes: 2 additions & 2 deletions codegen/src/exec_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ impl<W: Write + std::fmt::Debug> EmitExecTable for W {}
fn exec_table_item(inst: &Option<Instruction>) -> String {
match inst {
None => " bad,\n".into(),
Some(i) => format!(" instructions::{}_exec,\n", i.typename),
Some(i) => format!(" {}_exec,\n", i.typename),
}
}

static EXEC_TABLE_IMPORTS: &[u8] = br#"use super::instructions;
static EXEC_TABLE_IMPORTS: &[u8] = br#"use crate::instructions::code::*;
use crate::instructions::{bad, ExecFn};
"#;

Expand Down
23 changes: 12 additions & 11 deletions codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,46 +149,47 @@ fn read_instruction_list(file: &str, variant: Variant) -> io::Result<Instruction

/// Read master_ops_list.csv and emit functions, function tables, and data
/// tables.
pub fn parse() -> io::Result<()> {
pub fn generate_exec_code() -> io::Result<()> {
let out = std::env::var("OUT_DIR").unwrap();
let out = out.as_str();

let inst_groups = &[
read_instruction_list("../codegen/master_ops_list.csv", Variant::Normal)?,
read_instruction_list("../codegen/master_extended_ops_list.csv", Variant::Extended)?,
read_instruction_list("../codegen/master_simd_ops_list.csv", Variant::Simd)?,
];

fs::create_dir_all("src/instructions/generated")?;

// Emit the module wrapping the various generated items.
emit_module()?;
emit_module(out)?;

// Emit the file containing the code and descriptor structs.
let mut code_file = new_output_file("src/instructions/generated/instructions.rs")?;
let mut code_file = new_output_file(format!("{out}/instruction_code.rs")).unwrap();
code_file.emit_code_file(inst_groups)?;

// Emit the file containing the lookup array.
let mut code_file = new_output_file("src/instructions/generated/exec_table.rs")?;
let mut code_file = new_output_file(format!("{out}/instruction_exec_table.rs"))?;
code_file.emit_exec_table(inst_groups)?;

// Emit the file containing the lookup array for instruction data, by opcode.
let mut code_file = new_output_file("src/instructions/generated/data_table.rs")?;
let mut code_file = new_output_file(format!("{out}/instruction_data_table.rs"))?;
code_file.emit_instruction_data_table(inst_groups)?;

Ok(())
}

fn new_output_file(name: &str) -> io::Result<fs::File> {
fn new_output_file(name: impl AsRef<str>) -> io::Result<fs::File> {
let mut f = fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(name)?;
.open(name.as_ref())?;

f.write_all(GEN_HEADER)?;
Ok(f)
}

fn emit_module() -> io::Result<()> {
let mut f = new_output_file("src/instructions/generated/mod.rs")?;
fn emit_module(out: &str) -> io::Result<()> {
let mut f = new_output_file(format!("{out}/instruction_mod.rs"))?;
f.write_all(GEN_HEADER)?;
f.write_all(MODULE)
}
Expand Down
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ normalize_comments = true
overflow_delimited_expr = true
struct_field_align_threshold = 20
use_field_init_shorthand = true
format_generated_files = true
2 changes: 1 addition & 1 deletion wrausmt-runtime/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn main() {
println!("cargo:rerun-if-changed=codegen/master_extended_ops_list.csv");
println!("cargo:rerun-if-changed=codegen/master_ops_list.csv");
codegen::parse().unwrap();
codegen::generate_exec_code().unwrap();
}
18 changes: 13 additions & 5 deletions wrausmt-runtime/src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,20 @@
//! generated. See the [codegen] crate for more details on the generation
//! process.

mod code {
include!(concat!(env!("OUT_DIR"), "/instruction_code.rs"));
}
mod data_table {
include!(concat!(env!("OUT_DIR"), "/instruction_data_table.rs"));
}

mod exec_table {
include!(concat!(env!("OUT_DIR"), "/instruction_exec_table.rs"));
}

pub use code::opcodes;
use {
self::generated::{
self::{
data_table::{EXTENDED_INSTRUCTION_DATA, INSTRUCTION_DATA, SIMD_INSTRUCTION_DATA},
exec_table::{EXEC_TABLE, EXTENDED_EXEC_TABLE, SIMD_EXEC_TABLE},
},
Expand All @@ -15,10 +27,6 @@ use {
},
};

mod generated;

pub use generated::instructions::opcodes;

/// Function bodies, initialization values for globals, and offsets of element
/// or data segments are given as expressions, which are sequences of
/// instructions terminated by an end marker.
Expand Down

0 comments on commit 1e0ea04

Please sign in to comment.