diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..173f992 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,295 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "ahash" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" +dependencies = [ + "cfg-if", + "once_cell", + "version_check", + "zerocopy", +] + +[[package]] +name = "arbitrary" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d5a26814d8dcb93b0e5a0ff3c6d80a8843bafb21b39e8e18a6f05471870e110" + +[[package]] +name = "bumpalo" +version = "3.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "cranelift-bforest" +version = "0.113.0" +source = "git+https://github.com/jakubDoka/wasmtime.git#a27ac7aed78fae886e45f119139a1e54b20dd523" +dependencies = [ + "cranelift-entity", +] + +[[package]] +name = "cranelift-bitset" +version = "0.113.0" +source = "git+https://github.com/jakubDoka/wasmtime.git#a27ac7aed78fae886e45f119139a1e54b20dd523" + +[[package]] +name = "cranelift-codegen" +version = "0.113.0" +source = "git+https://github.com/jakubDoka/wasmtime.git#a27ac7aed78fae886e45f119139a1e54b20dd523" +dependencies = [ + "bumpalo", + "cranelift-bforest", + "cranelift-bitset", + "cranelift-codegen-meta", + "cranelift-codegen-shared", + "cranelift-control", + "cranelift-entity", + "cranelift-isle", + "hashbrown", + "log", + "regalloc2 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-hash", + "smallvec", + "target-lexicon", +] + +[[package]] +name = "cranelift-codegen-meta" +version = "0.113.0" +source = "git+https://github.com/jakubDoka/wasmtime.git#a27ac7aed78fae886e45f119139a1e54b20dd523" +dependencies = [ + "cranelift-codegen-shared", +] + +[[package]] +name = "cranelift-codegen-shared" +version = "0.113.0" +source = "git+https://github.com/jakubDoka/wasmtime.git#a27ac7aed78fae886e45f119139a1e54b20dd523" + +[[package]] +name = "cranelift-control" +version = "0.113.0" +source = "git+https://github.com/jakubDoka/wasmtime.git#a27ac7aed78fae886e45f119139a1e54b20dd523" +dependencies = [ + "arbitrary", +] + +[[package]] +name = "cranelift-entity" +version = "0.113.0" +source = "git+https://github.com/jakubDoka/wasmtime.git#a27ac7aed78fae886e45f119139a1e54b20dd523" +dependencies = [ + "cranelift-bitset", +] + +[[package]] +name = "cranelift-isle" +version = "0.113.0" +source = "git+https://github.com/jakubDoka/wasmtime.git#a27ac7aed78fae886e45f119139a1e54b20dd523" + +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" +dependencies = [ + "ahash", +] + +[[package]] +name = "hbbytecode" +version = "0.1.0" + +[[package]] +name = "hbcb" +version = "0.1.0" +dependencies = [ + "cranelift-codegen", + "cranelift-codegen-meta", + "cranelift-control", + "cranelift-isle", + "log", + "regalloc2 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec", + "target-lexicon", +] + +[[package]] +name = "hbjit" +version = "0.1.0" + +[[package]] +name = "hblang" +version = "0.1.0" +dependencies = [ + "hbbytecode", + "hbvm", + "regalloc2 0.10.2 (git+https://github.com/jakubDoka/regalloc2)", +] + +[[package]] +name = "hbvm" +version = "0.1.0" +dependencies = [ + "hbbytecode", +] + +[[package]] +name = "hbxrt" +version = "0.1.0" +dependencies = [ + "hbvm", + "memmap2", +] + +[[package]] +name = "libc" +version = "0.2.158" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" + +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + +[[package]] +name = "memmap2" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd3f7eed9d3848f8b98834af67102b720745c4ec028fcd0aa0239277e7de374f" +dependencies = [ + "libc", +] + +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regalloc2" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12908dbeb234370af84d0579b9f68258a0f67e201412dd9a2814e6f45b2fc0f0" +dependencies = [ + "hashbrown", + "log", + "rustc-hash", + "slice-group-by", + "smallvec", +] + +[[package]] +name = "regalloc2" +version = "0.10.2" +source = "git+https://github.com/jakubDoka/regalloc2#7e74b2fde4f022816cded93ab5685e46f8e3a159" +dependencies = [ + "hashbrown", + "rustc-hash", + "smallvec", +] + +[[package]] +name = "rustc-hash" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "583034fd73374156e66797ed8e5b0d5690409c9226b22d87cb7f19821c05d152" + +[[package]] +name = "slice-group-by" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "826167069c09b99d56f31e9ae5c99049e932a98c9dc2dac47645b08dbbf76ba7" + +[[package]] +name = "smallvec" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" + +[[package]] +name = "syn" +version = "2.0.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "target-lexicon" +version = "0.12.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1" + +[[package]] +name = "unicode-ident" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "xtask" +version = "0.1.0" + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/hbbytecode/Cargo.toml b/hbbytecode/Cargo.toml index 1b50f51..a542869 100644 --- a/hbbytecode/Cargo.toml +++ b/hbbytecode/Cargo.toml @@ -2,3 +2,8 @@ name = "hbbytecode" version = "0.1.0" edition = "2018" + +[features] +default = ["disasm"] +std = [] +disasm = ["std"] diff --git a/hbbytecode/build.rs b/hbbytecode/build.rs index 9ac45d2..6b549c6 100644 --- a/hbbytecode/build.rs +++ b/hbbytecode/build.rs @@ -1,58 +1,208 @@ #![feature(iter_next_chunk)] +use std::{collections::HashSet, fmt::Write}; + fn main() -> Result<(), Box> { println!("cargo:rerun-if-changed=build.rs"); println!("cargo:rerun-if-changed=instructions.in"); let mut generated = String::new(); - gen_op_structs(&mut generated)?; - std::fs::write("src/ops.rs", generated)?; - - let mut generated = String::new(); - gen_op_codes(&mut generated)?; - std::fs::write("src/opcode.rs", generated)?; + gen_instrs(&mut generated)?; + std::fs::write("src/instrs.rs", generated)?; Ok(()) } -fn gen_op_structs(generated: &mut String) -> std::fmt::Result { - use std::fmt::Write; - let mut seen = std::collections::HashSet::new(); +fn gen_instrs(generated: &mut String) -> Result<(), Box> { + writeln!(generated, "#![allow(dead_code)] #![allow(clippy::upper_case_acronyms)]")?; writeln!(generated, "use crate::*;")?; - for [.., args, _] in instructions() { - if !seen.insert(args) { - continue; - } - writeln!(generated, "#[derive(Clone, Copy, Debug)]")?; - writeln!(generated, "#[repr(packed)]")?; - write!(generated, "pub struct Ops{args}(")?; - let mut first = true; - for ch in args.chars().filter(|&ch| ch != 'N') { - if !std::mem::take(&mut first) { - write!(generated, ",")?; + '_opcode_structs: { + let mut seen = HashSet::new(); + for [.., args, _] in instructions() { + if !seen.insert(args) { + continue; } - write!(generated, "pub Op{ch}")?; + + writeln!(generated, "#[derive(Clone, Copy, Debug)]")?; + writeln!(generated, "#[repr(packed)]")?; + write!(generated, "pub struct Ops{args}(")?; + let mut first = true; + for ch in args.chars().filter(|&ch| ch != 'N') { + if !std::mem::take(&mut first) { + write!(generated, ",")?; + } + write!(generated, "pub Op{ch}")?; + } + writeln!(generated, ");")?; + writeln!(generated, "unsafe impl BytecodeItem for Ops{args} {{}}")?; } - writeln!(generated, ");")?; - writeln!(generated, "unsafe impl BytecodeItem for Ops{args} {{}}")?; } + '_max_size: { + let max = instructions() + .map( + |[_, _, ty, _]| { + if ty == "N" { + 1 + } else { + iter_args(ty).map(arg_to_width).sum::() + 1 + } + }, + ) + .max() + .unwrap(); + + writeln!(generated, "pub const MAX_SIZE: usize = {max};")?; + } + + '_encoders: { + for [op, name, ty, doc] in instructions() { + writeln!(generated, "/// {}", doc.trim_matches('"'))?; + let name = name.to_lowercase(); + let args = comma_sep( + iter_args(ty) + .enumerate() + .map(|(i, c)| format!("{}{i}: {}", arg_to_name(c), arg_to_type(c))), + ); + writeln!(generated, "pub fn {name}({args}) -> (usize, [u8; MAX_SIZE]) {{")?; + let arg_names = + comma_sep(iter_args(ty).enumerate().map(|(i, c)| format!("{}{i}", arg_to_name(c)))); + writeln!(generated, " unsafe {{ crate::encode({ty}({op}, {arg_names})) }}")?; + writeln!(generated, "}}")?; + } + } + + '_structs: { + let mut seen = std::collections::HashSet::new(); + for [_, _, ty, _] in instructions() { + if !seen.insert(ty) { + continue; + } + let types = comma_sep(iter_args(ty).map(arg_to_type).map(|s| s.to_string())); + writeln!(generated, "#[repr(packed)] pub struct {ty}(u8, {types});")?; + } + } + + '_name_list: { + writeln!(generated, "pub const NAMES: [&str; {}] = [", instructions().count())?; + for [_, name, _, _] in instructions() { + writeln!(generated, " \"{}\",", name.to_lowercase())?; + } + writeln!(generated, "];")?; + } + + let instr = "Instr"; + let oper = "Oper"; + + '_instr_enum: { + writeln!(generated, "#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[repr(u8)]")?; + writeln!(generated, "pub enum {instr} {{")?; + for [id, name, ..] in instructions() { + writeln!(generated, " {name} = {id},")?; + } + writeln!(generated, "}}")?; + } + + '_arg_kind: { + writeln!(generated, "#[derive(Debug, Clone, Copy, PartialEq, Eq)]")?; + writeln!(generated, "pub enum {oper} {{")?; + let mut seen = HashSet::new(); + for ty in instructions().flat_map(|[.., ty, _]| iter_args(ty)) { + if !seen.insert(ty) { + continue; + } + writeln!(generated, " {ty}({}),", arg_to_type(ty))?; + } + writeln!(generated, "}}")?; + } + + '_parse_opers: { + writeln!( + generated, + "/// This assumes the instruction byte is still at the beginning of the buffer" + )?; + writeln!(generated, "#[cfg(feature = \"disasm\")]")?; + writeln!(generated, "pub fn parse_args(bytes: &mut &[u8], kind: {instr}, buf: &mut std::vec::Vec<{oper}>) -> Option<()> {{")?; + writeln!(generated, " match kind {{")?; + let mut instrs = instructions().collect::>(); + instrs.sort_unstable_by_key(|&[.., ty, _]| ty); + for group in instrs.chunk_by(|[.., a, _], [.., b, _]| a == b) { + let ty = group[0][2]; + for &[_, name, ..] in group { + writeln!(generated, " | {instr}::{name}")?; + } + generated.pop(); + writeln!(generated, " => {{")?; + if iter_args(ty).count() != 0 { + writeln!(generated, " let data = crate::decode::<{ty}>(bytes)?;")?; + writeln!( + generated, + " buf.extend([{}]);", + comma_sep( + iter_args(ty).zip(1u32..).map(|(t, i)| format!("{oper}::{t}(data.{i})")) + ) + )?; + } else { + writeln!(generated, " crate::decode::<{ty}>(bytes)?;")?; + } + + writeln!(generated, " }}")?; + } + writeln!(generated, " }}")?; + writeln!(generated, " Some(())")?; + writeln!(generated, "}}")?; + } + + std::fs::write("src/instrs.rs", generated)?; Ok(()) } -fn gen_op_codes(generated: &mut String) -> std::fmt::Result { - use std::fmt::Write; - for [op, name, _, comment] in instructions() { - writeln!(generated, "#[doc = {comment}]")?; - writeln!(generated, "pub const {name}: u8 = {op};")?; - } - Ok(()) +fn comma_sep(items: impl Iterator) -> String { + items.map(|item| item.to_string()).collect::>().join(", ") } fn instructions() -> impl Iterator { include_str!("../hbbytecode/instructions.in") .lines() - .map(|line| line.strip_suffix(';').unwrap()) + .filter_map(|line| line.strip_suffix(';')) .map(|line| line.splitn(4, ',').map(str::trim).next_chunk().unwrap()) } + +fn arg_to_type(arg: char) -> &'static str { + match arg { + 'R' | 'B' => "u8", + 'H' => "u16", + 'W' => "u32", + 'D' | 'A' => "u64", + 'P' => "i16", + 'O' => "i32", + _ => panic!("unknown type: {}", arg), + } +} + +fn arg_to_width(arg: char) -> usize { + match arg { + 'R' | 'B' => 1, + 'H' => 2, + 'W' => 4, + 'D' | 'A' => 8, + 'P' => 2, + 'O' => 4, + _ => panic!("unknown type: {}", arg), + } +} + +fn arg_to_name(arg: char) -> &'static str { + match arg { + 'R' => "reg", + 'B' | 'H' | 'W' | 'D' => "imm", + 'P' | 'O' => "offset", + 'A' => "addr", + _ => panic!("unknown type: {}", arg), + } +} + +fn iter_args(ty: &'static str) -> impl Iterator { + ty.chars().filter(|c| *c != 'N') +} diff --git a/hbbytecode/src/instrs.rs b/hbbytecode/src/instrs.rs new file mode 100644 index 0000000..4d63acd --- /dev/null +++ b/hbbytecode/src/instrs.rs @@ -0,0 +1,1025 @@ +#![allow(dead_code)] #![allow(clippy::upper_case_acronyms)] +use crate::*; +#[derive(Clone, Copy, Debug)] +#[repr(packed)] +pub struct OpsN(); +unsafe impl BytecodeItem for OpsN {} +#[derive(Clone, Copy, Debug)] +#[repr(packed)] +pub struct OpsRRR(pub OpR,pub OpR,pub OpR); +unsafe impl BytecodeItem for OpsRRR {} +#[derive(Clone, Copy, Debug)] +#[repr(packed)] +pub struct OpsRRRR(pub OpR,pub OpR,pub OpR,pub OpR); +unsafe impl BytecodeItem for OpsRRRR {} +#[derive(Clone, Copy, Debug)] +#[repr(packed)] +pub struct OpsRR(pub OpR,pub OpR); +unsafe impl BytecodeItem for OpsRR {} +#[derive(Clone, Copy, Debug)] +#[repr(packed)] +pub struct OpsRRB(pub OpR,pub OpR,pub OpB); +unsafe impl BytecodeItem for OpsRRB {} +#[derive(Clone, Copy, Debug)] +#[repr(packed)] +pub struct OpsRRH(pub OpR,pub OpR,pub OpH); +unsafe impl BytecodeItem for OpsRRH {} +#[derive(Clone, Copy, Debug)] +#[repr(packed)] +pub struct OpsRRW(pub OpR,pub OpR,pub OpW); +unsafe impl BytecodeItem for OpsRRW {} +#[derive(Clone, Copy, Debug)] +#[repr(packed)] +pub struct OpsRRD(pub OpR,pub OpR,pub OpD); +unsafe impl BytecodeItem for OpsRRD {} +#[derive(Clone, Copy, Debug)] +#[repr(packed)] +pub struct OpsRB(pub OpR,pub OpB); +unsafe impl BytecodeItem for OpsRB {} +#[derive(Clone, Copy, Debug)] +#[repr(packed)] +pub struct OpsRH(pub OpR,pub OpH); +unsafe impl BytecodeItem for OpsRH {} +#[derive(Clone, Copy, Debug)] +#[repr(packed)] +pub struct OpsRW(pub OpR,pub OpW); +unsafe impl BytecodeItem for OpsRW {} +#[derive(Clone, Copy, Debug)] +#[repr(packed)] +pub struct OpsRD(pub OpR,pub OpD); +unsafe impl BytecodeItem for OpsRD {} +#[derive(Clone, Copy, Debug)] +#[repr(packed)] +pub struct OpsRRO(pub OpR,pub OpR,pub OpO); +unsafe impl BytecodeItem for OpsRRO {} +#[derive(Clone, Copy, Debug)] +#[repr(packed)] +pub struct OpsRRAH(pub OpR,pub OpR,pub OpA,pub OpH); +unsafe impl BytecodeItem for OpsRRAH {} +#[derive(Clone, Copy, Debug)] +#[repr(packed)] +pub struct OpsRROH(pub OpR,pub OpR,pub OpO,pub OpH); +unsafe impl BytecodeItem for OpsRROH {} +#[derive(Clone, Copy, Debug)] +#[repr(packed)] +pub struct OpsO(pub OpO); +unsafe impl BytecodeItem for OpsO {} +#[derive(Clone, Copy, Debug)] +#[repr(packed)] +pub struct OpsRRA(pub OpR,pub OpR,pub OpA); +unsafe impl BytecodeItem for OpsRRA {} +#[derive(Clone, Copy, Debug)] +#[repr(packed)] +pub struct OpsRRP(pub OpR,pub OpR,pub OpP); +unsafe impl BytecodeItem for OpsRRP {} +#[derive(Clone, Copy, Debug)] +#[repr(packed)] +pub struct OpsRRPH(pub OpR,pub OpR,pub OpP,pub OpH); +unsafe impl BytecodeItem for OpsRRPH {} +#[derive(Clone, Copy, Debug)] +#[repr(packed)] +pub struct OpsP(pub OpP); +unsafe impl BytecodeItem for OpsP {} +pub const MAX_SIZE: usize = 13; +/// Cause an unreachable code trap +pub fn un() -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(N(0x00, )) } +} +/// Termiante execution +pub fn tx() -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(N(0x01, )) } +} +/// Do nothing +pub fn nop() -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(N(0x02, )) } +} +/// Addition (8b) +pub fn add8(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x03, reg0, reg1, reg2)) } +} +/// Addition (16b) +pub fn add16(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x04, reg0, reg1, reg2)) } +} +/// Addition (32b) +pub fn add32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x05, reg0, reg1, reg2)) } +} +/// Addition (64b) +pub fn add64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x06, reg0, reg1, reg2)) } +} +/// Subtraction (8b) +pub fn sub8(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x07, reg0, reg1, reg2)) } +} +/// Subtraction (16b) +pub fn sub16(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x08, reg0, reg1, reg2)) } +} +/// Subtraction (32b) +pub fn sub32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x09, reg0, reg1, reg2)) } +} +/// Subtraction (64b) +pub fn sub64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x0A, reg0, reg1, reg2)) } +} +/// Multiplication (8b) +pub fn mul8(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x0B, reg0, reg1, reg2)) } +} +/// Multiplication (16b) +pub fn mul16(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x0C, reg0, reg1, reg2)) } +} +/// Multiplication (32b) +pub fn mul32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x0D, reg0, reg1, reg2)) } +} +/// Multiplication (64b) +pub fn mul64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x0E, reg0, reg1, reg2)) } +} +/// Bitand +pub fn and(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x0F, reg0, reg1, reg2)) } +} +/// Bitor +pub fn or(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x10, reg0, reg1, reg2)) } +} +/// Bitxor +pub fn xor(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x11, reg0, reg1, reg2)) } +} +/// Unsigned left bitshift (8b) +pub fn slu8(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x12, reg0, reg1, reg2)) } +} +/// Unsigned left bitshift (16b) +pub fn slu16(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x13, reg0, reg1, reg2)) } +} +/// Unsigned left bitshift (32b) +pub fn slu32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x14, reg0, reg1, reg2)) } +} +/// Unsigned left bitshift (64b) +pub fn slu64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x15, reg0, reg1, reg2)) } +} +/// Unsigned right bitshift (8b) +pub fn sru8(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x16, reg0, reg1, reg2)) } +} +/// Unsigned right bitshift (16b) +pub fn sru16(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x17, reg0, reg1, reg2)) } +} +/// Unsigned right bitshift (32b) +pub fn sru32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x18, reg0, reg1, reg2)) } +} +/// Unsigned right bitshift (64b) +pub fn sru64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x19, reg0, reg1, reg2)) } +} +/// Signed right bitshift (8b) +pub fn srs8(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x1A, reg0, reg1, reg2)) } +} +/// Signed right bitshift (16b) +pub fn srs16(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x1B, reg0, reg1, reg2)) } +} +/// Signed right bitshift (32b) +pub fn srs32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x1C, reg0, reg1, reg2)) } +} +/// Signed right bitshift (64b) +pub fn srs64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x1D, reg0, reg1, reg2)) } +} +/// Unsigned comparsion +pub fn cmpu(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x1E, reg0, reg1, reg2)) } +} +/// Signed comparsion +pub fn cmps(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x1F, reg0, reg1, reg2)) } +} +/// Merged divide-remainder (unsigned 8b) +pub fn diru8(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRRR(0x20, reg0, reg1, reg2, reg3)) } +} +/// Merged divide-remainder (unsigned 16b) +pub fn diru16(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRRR(0x21, reg0, reg1, reg2, reg3)) } +} +/// Merged divide-remainder (unsigned 32b) +pub fn diru32(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRRR(0x22, reg0, reg1, reg2, reg3)) } +} +/// Merged divide-remainder (unsigned 64b) +pub fn diru64(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRRR(0x23, reg0, reg1, reg2, reg3)) } +} +/// Merged divide-remainder (signed 8b) +pub fn dirs8(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRRR(0x24, reg0, reg1, reg2, reg3)) } +} +/// Merged divide-remainder (signed 16b) +pub fn dirs16(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRRR(0x25, reg0, reg1, reg2, reg3)) } +} +/// Merged divide-remainder (signed 32b) +pub fn dirs32(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRRR(0x26, reg0, reg1, reg2, reg3)) } +} +/// Merged divide-remainder (signed 64b) +pub fn dirs64(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRRR(0x27, reg0, reg1, reg2, reg3)) } +} +/// Bit negation +pub fn neg(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RR(0x28, reg0, reg1)) } +} +/// Logical negation +pub fn not(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RR(0x29, reg0, reg1)) } +} +/// Sign extend 8b to 64b +pub fn sxt8(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RR(0x2A, reg0, reg1)) } +} +/// Sign extend 16b to 64b +pub fn sxt16(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RR(0x2B, reg0, reg1)) } +} +/// Sign extend 32b to 64b +pub fn sxt32(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RR(0x2C, reg0, reg1)) } +} +/// Addition with immediate (8b) +pub fn addi8(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRB(0x2D, reg0, reg1, imm2)) } +} +/// Addition with immediate (16b) +pub fn addi16(reg0: u8, reg1: u8, imm2: u16) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRH(0x2E, reg0, reg1, imm2)) } +} +/// Addition with immediate (32b) +pub fn addi32(reg0: u8, reg1: u8, imm2: u32) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRW(0x2F, reg0, reg1, imm2)) } +} +/// Addition with immediate (64b) +pub fn addi64(reg0: u8, reg1: u8, imm2: u64) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRD(0x30, reg0, reg1, imm2)) } +} +/// Multiplication with immediate (8b) +pub fn muli8(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRB(0x31, reg0, reg1, imm2)) } +} +/// Multiplication with immediate (16b) +pub fn muli16(reg0: u8, reg1: u8, imm2: u16) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRH(0x32, reg0, reg1, imm2)) } +} +/// Multiplication with immediate (32b) +pub fn muli32(reg0: u8, reg1: u8, imm2: u32) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRW(0x33, reg0, reg1, imm2)) } +} +/// Multiplication with immediate (64b) +pub fn muli64(reg0: u8, reg1: u8, imm2: u64) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRD(0x34, reg0, reg1, imm2)) } +} +/// Bitand with immediate +pub fn andi(reg0: u8, reg1: u8, imm2: u64) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRD(0x35, reg0, reg1, imm2)) } +} +/// Bitor with immediate +pub fn ori(reg0: u8, reg1: u8, imm2: u64) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRD(0x36, reg0, reg1, imm2)) } +} +/// Bitxor with immediate +pub fn xori(reg0: u8, reg1: u8, imm2: u64) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRD(0x37, reg0, reg1, imm2)) } +} +/// Unsigned left bitshift with immedidate (8b) +pub fn slui8(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRB(0x38, reg0, reg1, imm2)) } +} +/// Unsigned left bitshift with immedidate (16b) +pub fn slui16(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRB(0x39, reg0, reg1, imm2)) } +} +/// Unsigned left bitshift with immedidate (32b) +pub fn slui32(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRB(0x3A, reg0, reg1, imm2)) } +} +/// Unsigned left bitshift with immedidate (64b) +pub fn slui64(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRB(0x3B, reg0, reg1, imm2)) } +} +/// Unsigned right bitshift with immediate (8b) +pub fn srui8(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRB(0x3C, reg0, reg1, imm2)) } +} +/// Unsigned right bitshift with immediate (16b) +pub fn srui16(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRB(0x3D, reg0, reg1, imm2)) } +} +/// Unsigned right bitshift with immediate (32b) +pub fn srui32(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRB(0x3E, reg0, reg1, imm2)) } +} +/// Unsigned right bitshift with immediate (64b) +pub fn srui64(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRB(0x3F, reg0, reg1, imm2)) } +} +/// Signed right bitshift with immediate +pub fn srsi8(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRB(0x40, reg0, reg1, imm2)) } +} +/// Signed right bitshift with immediate +pub fn srsi16(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRB(0x41, reg0, reg1, imm2)) } +} +/// Signed right bitshift with immediate +pub fn srsi32(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRB(0x42, reg0, reg1, imm2)) } +} +/// Signed right bitshift with immediate +pub fn srsi64(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRB(0x43, reg0, reg1, imm2)) } +} +/// Unsigned compare with immediate +pub fn cmpui(reg0: u8, reg1: u8, imm2: u64) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRD(0x44, reg0, reg1, imm2)) } +} +/// Signed compare with immediate +pub fn cmpsi(reg0: u8, reg1: u8, imm2: u64) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRD(0x45, reg0, reg1, imm2)) } +} +/// Copy register +pub fn cp(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RR(0x46, reg0, reg1)) } +} +/// Swap registers +pub fn swa(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RR(0x47, reg0, reg1)) } +} +/// Load immediate (8b) +pub fn li8(reg0: u8, imm1: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RB(0x48, reg0, imm1)) } +} +/// Load immediate (16b) +pub fn li16(reg0: u8, imm1: u16) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RH(0x49, reg0, imm1)) } +} +/// Load immediate (32b) +pub fn li32(reg0: u8, imm1: u32) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RW(0x4A, reg0, imm1)) } +} +/// Load immediate (64b) +pub fn li64(reg0: u8, imm1: u64) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RD(0x4B, reg0, imm1)) } +} +/// Load relative address +pub fn lra(reg0: u8, reg1: u8, offset2: i32) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRO(0x4C, reg0, reg1, offset2)) } +} +/// Load from absolute address +pub fn ld(reg0: u8, reg1: u8, addr2: u64, imm3: u16) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRAH(0x4D, reg0, reg1, addr2, imm3)) } +} +/// Store to absolute address +pub fn st(reg0: u8, reg1: u8, addr2: u64, imm3: u16) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRAH(0x4E, reg0, reg1, addr2, imm3)) } +} +/// Load from relative address +pub fn ldr(reg0: u8, reg1: u8, offset2: i32, imm3: u16) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RROH(0x4F, reg0, reg1, offset2, imm3)) } +} +/// Store to relative address +pub fn str(reg0: u8, reg1: u8, offset2: i32, imm3: u16) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RROH(0x50, reg0, reg1, offset2, imm3)) } +} +/// Copy block of memory +pub fn bmc(reg0: u8, reg1: u8, imm2: u16) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRH(0x51, reg0, reg1, imm2)) } +} +/// Copy register block +pub fn brc(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRB(0x52, reg0, reg1, imm2)) } +} +/// Relative jump +pub fn jmp(offset0: i32) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(O(0x53, offset0)) } +} +/// Linking relative jump +pub fn jal(reg0: u8, reg1: u8, offset2: i32) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRO(0x54, reg0, reg1, offset2)) } +} +/// Linking absolute jump +pub fn jala(reg0: u8, reg1: u8, addr2: u64) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRA(0x55, reg0, reg1, addr2)) } +} +/// Branch on equal +pub fn jeq(reg0: u8, reg1: u8, offset2: i16) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRP(0x56, reg0, reg1, offset2)) } +} +/// Branch on nonequal +pub fn jne(reg0: u8, reg1: u8, offset2: i16) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRP(0x57, reg0, reg1, offset2)) } +} +/// Branch on lesser-than (unsigned) +pub fn jltu(reg0: u8, reg1: u8, offset2: i16) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRP(0x58, reg0, reg1, offset2)) } +} +/// Branch on greater-than (unsigned) +pub fn jgtu(reg0: u8, reg1: u8, offset2: i16) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRP(0x59, reg0, reg1, offset2)) } +} +/// Branch on lesser-than (signed) +pub fn jlts(reg0: u8, reg1: u8, offset2: i16) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRP(0x5A, reg0, reg1, offset2)) } +} +/// Branch on greater-than (signed) +pub fn jgts(reg0: u8, reg1: u8, offset2: i16) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRP(0x5B, reg0, reg1, offset2)) } +} +/// Environment call trap +pub fn eca() -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(N(0x5C, )) } +} +/// Environment breakpoint +pub fn ebp() -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(N(0x5D, )) } +} +/// Floating point addition (32b) +pub fn fadd32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x5E, reg0, reg1, reg2)) } +} +/// Floating point addition (64b) +pub fn fadd64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x5F, reg0, reg1, reg2)) } +} +/// Floating point subtraction (32b) +pub fn fsub32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x60, reg0, reg1, reg2)) } +} +/// Floating point subtraction (64b) +pub fn fsub64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x61, reg0, reg1, reg2)) } +} +/// Floating point multiply (32b) +pub fn fmul32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x62, reg0, reg1, reg2)) } +} +/// Floating point multiply (64b) +pub fn fmul64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x63, reg0, reg1, reg2)) } +} +/// Floating point division (32b) +pub fn fdiv32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x64, reg0, reg1, reg2)) } +} +/// Floating point division (64b) +pub fn fdiv64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x65, reg0, reg1, reg2)) } +} +/// Float fused multiply-add (32b) +pub fn fma32(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRRR(0x66, reg0, reg1, reg2, reg3)) } +} +/// Float fused multiply-add (64b) +pub fn fma64(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRRR(0x67, reg0, reg1, reg2, reg3)) } +} +/// Float reciprocal (32b) +pub fn finv32(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RR(0x68, reg0, reg1)) } +} +/// Float reciprocal (64b) +pub fn finv64(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RR(0x69, reg0, reg1)) } +} +/// Flaot compare less than (32b) +pub fn fcmplt32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x6A, reg0, reg1, reg2)) } +} +/// Flaot compare less than (64b) +pub fn fcmplt64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x6B, reg0, reg1, reg2)) } +} +/// Flaot compare greater than (32b) +pub fn fcmpgt32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x6C, reg0, reg1, reg2)) } +} +/// Flaot compare greater than (64b) +pub fn fcmpgt64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRR(0x6D, reg0, reg1, reg2)) } +} +/// Int to 32 bit float +pub fn itf32(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RR(0x6E, reg0, reg1)) } +} +/// Int to 64 bit float +pub fn itf64(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RR(0x6F, reg0, reg1)) } +} +/// Float 32 to int +pub fn fti32(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRB(0x70, reg0, reg1, imm2)) } +} +/// Float 64 to int +pub fn fti64(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRB(0x71, reg0, reg1, imm2)) } +} +/// Float 64 to Float 32 +pub fn fc32t64(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RR(0x72, reg0, reg1)) } +} +/// Float 32 to Float 64 +pub fn fc64t32(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRB(0x73, reg0, reg1, imm2)) } +} +/// Load relative immediate (16 bit) +pub fn lra16(reg0: u8, reg1: u8, offset2: i16) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRP(0x74, reg0, reg1, offset2)) } +} +/// Load from relative address (16 bit) +pub fn ldr16(reg0: u8, reg1: u8, offset2: i16, imm3: u16) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRPH(0x75, reg0, reg1, offset2, imm3)) } +} +/// Store to relative address (16 bit) +pub fn str16(reg0: u8, reg1: u8, offset2: i16, imm3: u16) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(RRPH(0x76, reg0, reg1, offset2, imm3)) } +} +/// Relative jump (16 bit) +pub fn jmp16(offset0: i16) -> (usize, [u8; MAX_SIZE]) { + unsafe { crate::encode(P(0x77, offset0)) } +} +#[repr(packed)] pub struct N(u8, ); +#[repr(packed)] pub struct RRR(u8, u8, u8, u8); +#[repr(packed)] pub struct RRRR(u8, u8, u8, u8, u8); +#[repr(packed)] pub struct RR(u8, u8, u8); +#[repr(packed)] pub struct RRB(u8, u8, u8, u8); +#[repr(packed)] pub struct RRH(u8, u8, u8, u16); +#[repr(packed)] pub struct RRW(u8, u8, u8, u32); +#[repr(packed)] pub struct RRD(u8, u8, u8, u64); +#[repr(packed)] pub struct RB(u8, u8, u8); +#[repr(packed)] pub struct RH(u8, u8, u16); +#[repr(packed)] pub struct RW(u8, u8, u32); +#[repr(packed)] pub struct RD(u8, u8, u64); +#[repr(packed)] pub struct RRO(u8, u8, u8, i32); +#[repr(packed)] pub struct RRAH(u8, u8, u8, u64, u16); +#[repr(packed)] pub struct RROH(u8, u8, u8, i32, u16); +#[repr(packed)] pub struct O(u8, i32); +#[repr(packed)] pub struct RRA(u8, u8, u8, u64); +#[repr(packed)] pub struct RRP(u8, u8, u8, i16); +#[repr(packed)] pub struct RRPH(u8, u8, u8, i16, u16); +#[repr(packed)] pub struct P(u8, i16); +pub const NAMES: [&str; 120] = [ + "un", + "tx", + "nop", + "add8", + "add16", + "add32", + "add64", + "sub8", + "sub16", + "sub32", + "sub64", + "mul8", + "mul16", + "mul32", + "mul64", + "and", + "or", + "xor", + "slu8", + "slu16", + "slu32", + "slu64", + "sru8", + "sru16", + "sru32", + "sru64", + "srs8", + "srs16", + "srs32", + "srs64", + "cmpu", + "cmps", + "diru8", + "diru16", + "diru32", + "diru64", + "dirs8", + "dirs16", + "dirs32", + "dirs64", + "neg", + "not", + "sxt8", + "sxt16", + "sxt32", + "addi8", + "addi16", + "addi32", + "addi64", + "muli8", + "muli16", + "muli32", + "muli64", + "andi", + "ori", + "xori", + "slui8", + "slui16", + "slui32", + "slui64", + "srui8", + "srui16", + "srui32", + "srui64", + "srsi8", + "srsi16", + "srsi32", + "srsi64", + "cmpui", + "cmpsi", + "cp", + "swa", + "li8", + "li16", + "li32", + "li64", + "lra", + "ld", + "st", + "ldr", + "str", + "bmc", + "brc", + "jmp", + "jal", + "jala", + "jeq", + "jne", + "jltu", + "jgtu", + "jlts", + "jgts", + "eca", + "ebp", + "fadd32", + "fadd64", + "fsub32", + "fsub64", + "fmul32", + "fmul64", + "fdiv32", + "fdiv64", + "fma32", + "fma64", + "finv32", + "finv64", + "fcmplt32", + "fcmplt64", + "fcmpgt32", + "fcmpgt64", + "itf32", + "itf64", + "fti32", + "fti64", + "fc32t64", + "fc64t32", + "lra16", + "ldr16", + "str16", + "jmp16", +]; +#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[repr(u8)] +pub enum Instr { + UN = 0x00, + TX = 0x01, + NOP = 0x02, + ADD8 = 0x03, + ADD16 = 0x04, + ADD32 = 0x05, + ADD64 = 0x06, + SUB8 = 0x07, + SUB16 = 0x08, + SUB32 = 0x09, + SUB64 = 0x0A, + MUL8 = 0x0B, + MUL16 = 0x0C, + MUL32 = 0x0D, + MUL64 = 0x0E, + AND = 0x0F, + OR = 0x10, + XOR = 0x11, + SLU8 = 0x12, + SLU16 = 0x13, + SLU32 = 0x14, + SLU64 = 0x15, + SRU8 = 0x16, + SRU16 = 0x17, + SRU32 = 0x18, + SRU64 = 0x19, + SRS8 = 0x1A, + SRS16 = 0x1B, + SRS32 = 0x1C, + SRS64 = 0x1D, + CMPU = 0x1E, + CMPS = 0x1F, + DIRU8 = 0x20, + DIRU16 = 0x21, + DIRU32 = 0x22, + DIRU64 = 0x23, + DIRS8 = 0x24, + DIRS16 = 0x25, + DIRS32 = 0x26, + DIRS64 = 0x27, + NEG = 0x28, + NOT = 0x29, + SXT8 = 0x2A, + SXT16 = 0x2B, + SXT32 = 0x2C, + ADDI8 = 0x2D, + ADDI16 = 0x2E, + ADDI32 = 0x2F, + ADDI64 = 0x30, + MULI8 = 0x31, + MULI16 = 0x32, + MULI32 = 0x33, + MULI64 = 0x34, + ANDI = 0x35, + ORI = 0x36, + XORI = 0x37, + SLUI8 = 0x38, + SLUI16 = 0x39, + SLUI32 = 0x3A, + SLUI64 = 0x3B, + SRUI8 = 0x3C, + SRUI16 = 0x3D, + SRUI32 = 0x3E, + SRUI64 = 0x3F, + SRSI8 = 0x40, + SRSI16 = 0x41, + SRSI32 = 0x42, + SRSI64 = 0x43, + CMPUI = 0x44, + CMPSI = 0x45, + CP = 0x46, + SWA = 0x47, + LI8 = 0x48, + LI16 = 0x49, + LI32 = 0x4A, + LI64 = 0x4B, + LRA = 0x4C, + LD = 0x4D, + ST = 0x4E, + LDR = 0x4F, + STR = 0x50, + BMC = 0x51, + BRC = 0x52, + JMP = 0x53, + JAL = 0x54, + JALA = 0x55, + JEQ = 0x56, + JNE = 0x57, + JLTU = 0x58, + JGTU = 0x59, + JLTS = 0x5A, + JGTS = 0x5B, + ECA = 0x5C, + EBP = 0x5D, + FADD32 = 0x5E, + FADD64 = 0x5F, + FSUB32 = 0x60, + FSUB64 = 0x61, + FMUL32 = 0x62, + FMUL64 = 0x63, + FDIV32 = 0x64, + FDIV64 = 0x65, + FMA32 = 0x66, + FMA64 = 0x67, + FINV32 = 0x68, + FINV64 = 0x69, + FCMPLT32 = 0x6A, + FCMPLT64 = 0x6B, + FCMPGT32 = 0x6C, + FCMPGT64 = 0x6D, + ITF32 = 0x6E, + ITF64 = 0x6F, + FTI32 = 0x70, + FTI64 = 0x71, + FC32T64 = 0x72, + FC64T32 = 0x73, + LRA16 = 0x74, + LDR16 = 0x75, + STR16 = 0x76, + JMP16 = 0x77, +} +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Oper { + R(u8), + B(u8), + H(u16), + W(u32), + D(u64), + O(i32), + A(u64), + P(i16), +} +/// This assumes the instruction byte is still at the beginning of the buffer +#[cfg(feature = "disasm")] +pub fn parse_args(bytes: &mut &[u8], kind: Instr, buf: &mut std::vec::Vec) -> Option<()> { + match kind { + | Instr::TX + | Instr::NOP + | Instr::UN + | Instr::ECA + | Instr::EBP => { + crate::decode::(bytes)?; + } + | Instr::JMP => { + let data = crate::decode::(bytes)?; + buf.extend([Oper::O(data.1)]); + } + | Instr::JMP16 => { + let data = crate::decode::

(bytes)?; + buf.extend([Oper::P(data.1)]); + } + | Instr::LI8 => { + let data = crate::decode::(bytes)?; + buf.extend([Oper::R(data.1), Oper::B(data.2)]); + } + | Instr::LI64 => { + let data = crate::decode::(bytes)?; + buf.extend([Oper::R(data.1), Oper::D(data.2)]); + } + | Instr::LI16 => { + let data = crate::decode::(bytes)?; + buf.extend([Oper::R(data.1), Oper::H(data.2)]); + } + | Instr::NEG + | Instr::NOT + | Instr::SXT8 + | Instr::SXT16 + | Instr::SXT32 + | Instr::CP + | Instr::SWA + | Instr::FINV32 + | Instr::FINV64 + | Instr::ITF32 + | Instr::ITF64 + | Instr::FC32T64 => { + let data = crate::decode::(bytes)?; + buf.extend([Oper::R(data.1), Oper::R(data.2)]); + } + | Instr::JALA => { + let data = crate::decode::(bytes)?; + buf.extend([Oper::R(data.1), Oper::R(data.2), Oper::A(data.3)]); + } + | Instr::LD + | Instr::ST => { + let data = crate::decode::(bytes)?; + buf.extend([Oper::R(data.1), Oper::R(data.2), Oper::A(data.3), Oper::H(data.4)]); + } + | Instr::SRSI8 + | Instr::ADDI8 + | Instr::MULI8 + | Instr::SLUI8 + | Instr::SLUI16 + | Instr::SLUI32 + | Instr::SLUI64 + | Instr::SRUI8 + | Instr::SRUI16 + | Instr::SRUI32 + | Instr::SRUI64 + | Instr::SRSI16 + | Instr::SRSI32 + | Instr::BRC + | Instr::FTI32 + | Instr::FTI64 + | Instr::FC64T32 + | Instr::SRSI64 => { + let data = crate::decode::(bytes)?; + buf.extend([Oper::R(data.1), Oper::R(data.2), Oper::B(data.3)]); + } + | Instr::ADDI64 + | Instr::CMPUI + | Instr::CMPSI + | Instr::MULI64 + | Instr::ANDI + | Instr::ORI + | Instr::XORI => { + let data = crate::decode::(bytes)?; + buf.extend([Oper::R(data.1), Oper::R(data.2), Oper::D(data.3)]); + } + | Instr::ADDI16 + | Instr::MULI16 + | Instr::BMC => { + let data = crate::decode::(bytes)?; + buf.extend([Oper::R(data.1), Oper::R(data.2), Oper::H(data.3)]); + } + | Instr::LRA + | Instr::JAL => { + let data = crate::decode::(bytes)?; + buf.extend([Oper::R(data.1), Oper::R(data.2), Oper::O(data.3)]); + } + | Instr::LDR + | Instr::STR => { + let data = crate::decode::(bytes)?; + buf.extend([Oper::R(data.1), Oper::R(data.2), Oper::O(data.3), Oper::H(data.4)]); + } + | Instr::JEQ + | Instr::JNE + | Instr::JLTU + | Instr::JGTU + | Instr::JLTS + | Instr::JGTS + | Instr::LRA16 => { + let data = crate::decode::(bytes)?; + buf.extend([Oper::R(data.1), Oper::R(data.2), Oper::P(data.3)]); + } + | Instr::STR16 + | Instr::LDR16 => { + let data = crate::decode::(bytes)?; + buf.extend([Oper::R(data.1), Oper::R(data.2), Oper::P(data.3), Oper::H(data.4)]); + } + | Instr::SUB32 + | Instr::CMPU + | Instr::CMPS + | Instr::SRS8 + | Instr::SUB64 + | Instr::MUL8 + | Instr::MUL16 + | Instr::MUL32 + | Instr::MUL64 + | Instr::AND + | Instr::OR + | Instr::XOR + | Instr::ADD8 + | Instr::ADD16 + | Instr::SRS16 + | Instr::SLU8 + | Instr::SLU16 + | Instr::FADD32 + | Instr::FADD64 + | Instr::FSUB32 + | Instr::FSUB64 + | Instr::FMUL32 + | Instr::FMUL64 + | Instr::FDIV32 + | Instr::FDIV64 + | Instr::SLU32 + | Instr::SLU64 + | Instr::FCMPLT32 + | Instr::FCMPLT64 + | Instr::FCMPGT32 + | Instr::FCMPGT64 + | Instr::SRU8 + | Instr::SRU16 + | Instr::ADD64 + | Instr::SUB8 + | Instr::SRU32 + | Instr::SUB16 + | Instr::SRS32 + | Instr::SRS64 + | Instr::SRU64 + | Instr::ADD32 => { + let data = crate::decode::(bytes)?; + buf.extend([Oper::R(data.1), Oper::R(data.2), Oper::R(data.3)]); + } + | Instr::DIRU8 + | Instr::DIRS16 + | Instr::DIRS32 + | Instr::DIRS64 + | Instr::DIRU16 + | Instr::DIRU32 + | Instr::FMA32 + | Instr::FMA64 + | Instr::DIRU64 + | Instr::DIRS8 => { + let data = crate::decode::(bytes)?; + buf.extend([Oper::R(data.1), Oper::R(data.2), Oper::R(data.3), Oper::R(data.4)]); + } + | Instr::MULI32 + | Instr::ADDI32 => { + let data = crate::decode::(bytes)?; + buf.extend([Oper::R(data.1), Oper::R(data.2), Oper::W(data.3)]); + } + | Instr::LI32 => { + let data = crate::decode::(bytes)?; + buf.extend([Oper::R(data.1), Oper::W(data.2)]); + } + } + Some(()) +} diff --git a/hbbytecode/src/lib.rs b/hbbytecode/src/lib.rs index bcebd5b..3a45841 100644 --- a/hbbytecode/src/lib.rs +++ b/hbbytecode/src/lib.rs @@ -1,10 +1,12 @@ #![no_std] -pub use crate::ops::*; +#[cfg(feature = "std")] +extern crate std; + +pub use crate::instrs::*; use core::convert::TryFrom; -pub mod opcode; -mod ops; +mod instrs; type OpR = u8; @@ -22,6 +24,38 @@ type OpD = u64; pub unsafe trait BytecodeItem {} unsafe impl BytecodeItem for u8 {} +impl TryFrom for Instr { + type Error = u8; + + #[inline] + fn try_from(value: u8) -> Result { + #[cold] + fn failed(value: u8) -> Result { + Err(value) + } + + if value < NAMES.len() as u8 { + unsafe { Ok(std::mem::transmute::(value)) } + } else { + failed(value) + } + } +} + +#[inline] +unsafe fn encode(instr: T) -> (usize, [u8; instrs::MAX_SIZE]) { + let mut buf = [0; instrs::MAX_SIZE]; + core::ptr::write(buf.as_mut_ptr() as *mut T, instr); + (core::mem::size_of::(), buf) +} + +#[inline] +fn decode(binary: &mut &[u8]) -> Option { + let (front, rest) = std::mem::take(binary).split_at_checked(core::mem::size_of::())?; + *binary = rest; + unsafe { Some(core::ptr::read(front.as_ptr() as *const T)) } +} + /// Rounding mode #[derive(Clone, Copy, Debug, PartialEq, Eq)] #[repr(u8)] @@ -39,3 +73,181 @@ impl TryFrom for RoundingMode { (value <= 3).then(|| unsafe { core::mem::transmute(value) }).ok_or(()) } } + +#[cfg(feature = "disasm")] +#[derive(Clone, Copy)] +pub enum DisasmItem { + Func, + Global, +} + +#[cfg(feature = "disasm")] +pub fn disasm( + binary: &mut &[u8], + functions: &std::collections::BTreeMap, + out: &mut impl std::io::Write, + mut eca_handler: impl FnMut(&mut &[u8]), +) -> std::io::Result<()> { + use { + self::instrs::Instr, + std::{ + collections::{hash_map::Entry, HashMap}, + convert::TryInto, + vec::Vec, + }, + }; + + fn instr_from_byte(b: u8) -> std::io::Result { + if b as usize >= instrs::NAMES.len() { + return Err(std::io::ErrorKind::InvalidData.into()); + } + Ok(unsafe { std::mem::transmute::(b) }) + } + + let mut labels = HashMap::::default(); + let mut buf = Vec::::new(); + let mut has_cycle = false; + let mut has_oob = false; + + '_offset_pass: for (&off, &(_name, len, kind)) in functions.iter() { + if matches!(kind, DisasmItem::Global) { + continue; + } + + let prev = *binary; + + *binary = &binary[..off as usize]; + + let mut label_count = 0; + while let Some(&byte) = binary.first() { + let offset: i32 = (prev.len() - binary.len()).try_into().unwrap(); + if offset as u32 == off + len { + break; + } + let Ok(inst) = instr_from_byte(byte) else { break }; + instrs::parse_args(binary, inst, &mut buf).ok_or(std::io::ErrorKind::OutOfMemory)?; + + for op in buf.drain(..) { + let rel = match op { + instrs::Oper::O(rel) => rel, + instrs::Oper::P(rel) => rel.into(), + _ => continue, + }; + + has_cycle |= rel == 0; + + let global_offset: u32 = (offset + rel).try_into().unwrap(); + if functions.get(&global_offset).is_some() { + continue; + } + label_count += match labels.entry(global_offset) { + Entry::Occupied(_) => 0, + Entry::Vacant(entry) => { + entry.insert(label_count); + 1 + } + } + } + + if matches!(inst, Instr::ECA) { + eca_handler(binary); + } + } + + *binary = prev; + } + + let mut ordered = functions.iter().collect::>(); + ordered.sort_unstable_by_key(|(_, (name, _, _))| name); + + '_dump: for (&off, &(name, len, kind)) in ordered { + if matches!(kind, DisasmItem::Global) { + continue; + } + let prev = *binary; + + writeln!(out, "{name}:")?; + + *binary = &binary[..off as usize]; + while let Some(&byte) = binary.first() { + let offset: i32 = (prev.len() - binary.len()).try_into().unwrap(); + if offset as u32 == off + len { + break; + } + let Ok(inst) = instr_from_byte(byte) else { + writeln!(out, "invalid instr {byte}")?; + break; + }; + instrs::parse_args(binary, inst, &mut buf).unwrap(); + + if let Some(label) = labels.get(&offset.try_into().unwrap()) { + write!(out, "{:>2}: ", label)?; + } else { + write!(out, " ")?; + } + + write!(out, "{inst:<8?} ")?; + + 'a: for (i, op) in buf.drain(..).enumerate() { + if i != 0 { + write!(out, ", ")?; + } + + let rel = 'b: { + match op { + instrs::Oper::O(rel) => break 'b rel, + instrs::Oper::P(rel) => break 'b rel.into(), + instrs::Oper::R(r) => write!(out, "r{r}")?, + instrs::Oper::B(b) => write!(out, "{b}b")?, + instrs::Oper::H(h) => write!(out, "{h}h")?, + instrs::Oper::W(w) => write!(out, "{w}w")?, + instrs::Oper::D(d) if (d as i64) < 0 => write!(out, "{}d", d as i64)?, + instrs::Oper::D(d) => write!(out, "{d}d")?, + instrs::Oper::A(a) => write!(out, "{a}a")?, + } + + continue 'a; + }; + + let global_offset: u32 = (offset + rel).try_into().unwrap(); + if let Some(&(name, ..)) = functions.get(&global_offset) { + if name.contains('\0') { + write!(out, ":{name:?}")?; + } else { + write!(out, ":{name}")?; + } + } else { + let local_has_oob = global_offset < off + || global_offset > off + len + || instr_from_byte(prev[global_offset as usize]).is_err() + || prev[global_offset as usize] == 0; + has_oob |= local_has_oob; + let label = labels.get(&global_offset).unwrap(); + if local_has_oob { + write!(out, "!!!!!!!!!{rel}")?; + } else { + write!(out, ":{label}")?; + } + } + } + + writeln!(out)?; + + if matches!(inst, Instr::ECA) { + eca_handler(binary); + } + } + + *binary = prev; + } + + if has_oob { + return Err(std::io::ErrorKind::InvalidInput.into()); + } + + if has_cycle { + return Err(std::io::ErrorKind::TimedOut.into()); + } + + Ok(()) +} diff --git a/hbcb/Cargo.toml b/hbcb/Cargo.toml index c5d418a..872b124 100644 --- a/hbcb/Cargo.toml +++ b/hbcb/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] -cranelift-codegen = { git = "https://github.com/jakubDoka/wasmtime.git", default-features = false } +cranelift-codegen = { git = "https://github.com/jakubDoka/wasmtime.git", default-features = false, features = ["std"] } cranelift-control = { git = "https://github.com/jakubDoka/wasmtime.git", default-features = false } log = "0.4.22" regalloc2 = "0.10.2" diff --git a/hbcb/build.rs b/hbcb/build.rs index 331d69f..67c63fb 100644 --- a/hbcb/build.rs +++ b/hbcb/build.rs @@ -15,7 +15,6 @@ // current directory is used to find the sources. use { - core::panic, cranelift_codegen_meta::{self as meta, isle::IsleCompilations}, cranelift_isle::error::Errors, meta::isle::IsleCompilation, diff --git a/hbcb/src/abi.rs b/hbcb/src/abi.rs index 55d4387..4b75885 100644 --- a/hbcb/src/abi.rs +++ b/hbcb/src/abi.rs @@ -1,13 +1,15 @@ //! Implementation of a standard Riscv64 ABI. use { - crate::inst::*, + crate::{ + inst::*, + settings::{self, Flags as RiscvFlags}, + }, alloc::{boxed::Box, vec::Vec}, cranelift_codegen::{ ir::{self, types::*, LibCall, Signature}, isa::{self, unwind::UnwindInst, CallConv}, machinst::*, - settings::{self, Flags as RiscvFlags}, CodegenError, CodegenResult, }, regalloc2::{MachineEnv, PReg, PRegSet}, @@ -21,11 +23,6 @@ pub(crate) type Riscv64Callee = Callee; /// Support for the Riscv64 ABI from the caller side (at a callsite). pub(crate) type Riscv64ABICallSite = CallSite; -/// This is the limit for the size of argument and return-value areas on the -/// stack. We place a reasonable limit here to avoid integer overflow issues -/// with 32-bit arithmetic: for now, 128 MB. -static STACK_ARG_RET_SIZE_LIMIT: u32 = 128 * 1024 * 1024; - /// Riscv64-specific ABI behavior. This struct just serves as an implementation /// point for the trait; it is never actually instantiated. pub struct Riscv64MachineDeps; @@ -70,6 +67,11 @@ impl ABIMachineSpec for Riscv64MachineDeps { type F = RiscvFlags; type I = Inst; + /// This is the limit for the size of argument and return-value areas on the + /// stack. We place a reasonable limit here to avoid integer overflow issues + /// with 32-bit arithmetic: for now, 128 MB. + const STACK_ARG_RET_SIZE_LIMIT: u32 = 128 * 1024 * 1024; + fn word_bits() -> u32 { 64 } @@ -651,8 +653,12 @@ impl ABIMachineSpec for Riscv64MachineDeps { } } -impl Riscv64ABICallSite { - pub fn emit_return_call(mut self, ctx: &mut Lower, args: isle::ValueSlice) { +pub trait EmitReturnCall { + fn emit_return_call(mut self, ctx: &mut Lower, args: isle::ValueSlice); +} + +impl EmitReturnCall for Riscv64ABICallSite { + fn emit_return_call(mut self, ctx: &mut Lower, args: isle::ValueSlice) { let new_stack_arg_size = u32::try_from(self.sig(ctx.sigs()).sized_stack_arg_space()).unwrap(); diff --git a/hbcb/src/inst/encode.rs b/hbcb/src/inst/encode.rs index 0e2d4c4..2e14cfb 100644 --- a/hbcb/src/inst/encode.rs +++ b/hbcb/src/inst/encode.rs @@ -6,12 +6,14 @@ //! Some instructions especially in extensions have slight variations from //! the base RISC-V specification. -use super::*; -use crate::lower::isle::generated_code::{ - COpcodeSpace, CaOp, CbOp, CiOp, CiwOp, ClOp, CrOp, CsOp, CssOp, CsznOp, FpuOPWidth, - VecAluOpRImm5, VecAluOpRR, VecAluOpRRRImm5, VecAluOpRRRR, VecOpCategory, ZcbMemOp, +use { + super::*, + crate::lower::isle::generated_code::{ + COpcodeSpace, CaOp, CbOp, CiOp, CiwOp, ClOp, CrOp, CsOp, CssOp, CsznOp, FpuOPWidth, + VecAluOpRImm5, VecAluOpRR, VecAluOpRRRImm5, VecAluOpRRRR, VecOpCategory, ZcbMemOp, + }, + cranelift_codegen::machinst::isle::WritableReg, }; -use crate::machinst::isle::WritableReg; fn unsigned_field_width(value: u32, width: u8) -> u32 { debug_assert_eq!(value & (!0 << width), 0); @@ -199,14 +201,7 @@ pub fn encode_valu_rr(op: VecAluOpRR, vd: WritableReg, vs: Reg, masking: VecOpMa (reg_to_gpr_num(vs), op.aux_encoding()) }; - encode_r_type_bits( - op.opcode(), - reg_to_gpr_num(vd.to_reg()), - op.funct3(), - vs1, - vs2, - funct7, - ) + encode_r_type_bits(op.opcode(), reg_to_gpr_num(vd.to_reg()), op.funct3(), vs1, vs2, funct7) } pub fn encode_valu_r_imm( @@ -222,14 +217,7 @@ pub fn encode_valu_r_imm( let vs1 = imm.bits() as u32; let vs2 = op.aux_encoding(); - encode_r_type_bits( - op.opcode(), - reg_to_gpr_num(vd.to_reg()), - op.funct3(), - vs1, - vs2, - funct7, - ) + encode_r_type_bits(op.opcode(), reg_to_gpr_num(vd.to_reg()), op.funct3(), vs1, vs2, funct7) } /// Encodes a Vector CFG Imm instruction. diff --git a/hbcb/src/inst/regs.rs b/hbcb/src/inst/regs.rs index ffdc484..94bc857 100644 --- a/hbcb/src/inst/regs.rs +++ b/hbcb/src/inst/regs.rs @@ -1,12 +1,11 @@ //! Riscv64 ISA definitions: registers. //! -use crate::machinst::{Reg, Writable}; - -use alloc::vec; -use alloc::vec::Vec; - -use regalloc2::{PReg, RegClass, VReg}; +use { + alloc::{vec, vec::Vec}, + cranelift_codegen::machinst::{Reg, Writable}, + regalloc2::{PReg, RegClass, VReg}, +}; // first argument of function call #[inline] diff --git a/hbcb/src/inst/vector.rs b/hbcb/src/inst/vector.rs index 356c747..a65ed82 100644 --- a/hbcb/src/inst/vector.rs +++ b/hbcb/src/inst/vector.rs @@ -1,19 +1,20 @@ -use crate::lower::isle::generated_code::VecAluOpRRRR; -use crate::lower::isle::generated_code::{ - VecAMode, VecAluOpRImm5, VecAluOpRR, VecAluOpRRImm5, VecAluOpRRR, VecAluOpRRRImm5, VecAvl, - VecElementWidth, VecLmul, VecMaskMode, VecOpCategory, VecOpMasking, VecTailMode, +use { + super::{Type, UImm5}, + crate::{ + lower::isle::generated_code::{ + VecAMode, VecAluOpRImm5, VecAluOpRR, VecAluOpRRImm5, VecAluOpRRR, VecAluOpRRRImm5, + VecAluOpRRRR, VecAvl, VecElementWidth, VecLmul, VecMaskMode, VecOpCategory, + VecOpMasking, VecTailMode, + }, + Reg, + }, + core::fmt, + cranelift_codegen::machinst::{OperandVisitor, RegClass}, }; -use crate::machinst::{OperandVisitor, RegClass}; -use crate::Reg; -use core::fmt; - -use super::{Type, UImm5}; impl VecAvl { pub fn _static(size: u32) -> Self { - VecAvl::Static { - size: UImm5::maybe_from_u8(size as u8).expect("Invalid size for AVL"), - } + VecAvl::Static { size: UImm5::maybe_from_u8(size as u8).expect("Invalid size for AVL") } } pub fn is_static(&self) -> bool { @@ -178,11 +179,7 @@ impl VType { impl fmt::Display for VType { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!( - f, - "{}, {}, {}, {}", - self.sew, self.lmul, self.tail_mode, self.mask_mode - ) + write!(f, "{}, {}, {}, {}", self.sew, self.lmul, self.tail_mode, self.mask_mode) } } @@ -255,6 +252,7 @@ impl VecAluOpRRRR { // Vector Opcode 0x57 } + pub fn funct3(&self) -> u32 { self.category().encode() } @@ -323,6 +321,7 @@ impl VecAluOpRRRImm5 { // Vector Opcode 0x57 } + pub fn funct3(&self) -> u32 { self.category().encode() } @@ -369,9 +368,11 @@ impl VecAluOpRRR { // Vector Opcode 0x57 } + pub fn funct3(&self) -> u32 { self.category().encode() } + pub fn funct6(&self) -> u32 { // See: https://github.com/riscv/riscv-v-spec/blob/master/inst-table.adoc match self { @@ -658,6 +659,7 @@ impl VecAluOpRRImm5 { // Vector Opcode 0x57 } + pub fn funct3(&self) -> u32 { self.category().encode() } @@ -1016,6 +1018,7 @@ impl VecAluOpRImm5 { // Vector Opcode 0x57 } + pub fn funct3(&self) -> u32 { self.category().encode() } diff --git a/hblang/Cargo.toml b/hblang/Cargo.toml index 90a0c45..a2f7887 100644 --- a/hblang/Cargo.toml +++ b/hblang/Cargo.toml @@ -8,5 +8,6 @@ name = "hbc" path = "src/main.rs" [dependencies] +hbbytecode = { version = "0.1.0", path = "../hbbytecode" } hbvm = { path = "../hbvm", features = ["nightly"] } regalloc2 = { git = "https://github.com/jakubDoka/regalloc2" } diff --git a/hblang/build.rs b/hblang/build.rs deleted file mode 100644 index 1598082..0000000 --- a/hblang/build.rs +++ /dev/null @@ -1,183 +0,0 @@ -#![feature(iter_next_chunk)] -use std::{collections::HashSet, fmt::Write}; - -fn main() -> Result<(), Box> { - println!("cargo:rerun-if-changed=build.rs"); - println!("cargo:rerun-if-changed=../hbbytecode/instructions.in"); - - gen_instrs()?; - - Ok(()) -} - -fn gen_instrs() -> Result<(), Box> { - let mut generated = String::new(); - - writeln!(generated, "#![allow(dead_code)] #![allow(clippy::upper_case_acronyms)]")?; - - '_max_size: { - let max = instructions() - .map( - |[_, _, ty, _]| { - if ty == "N" { - 1 - } else { - iter_args(ty).map(arg_to_width).sum::() + 1 - } - }, - ) - .max() - .unwrap(); - - writeln!(generated, "pub const MAX_SIZE: usize = {max};")?; - } - - '_encoders: { - for [op, name, ty, doc] in instructions() { - writeln!(generated, "/// {}", doc.trim_matches('"'))?; - let name = name.to_lowercase(); - let args = comma_sep( - iter_args(ty) - .enumerate() - .map(|(i, c)| format!("{}{i}: {}", arg_to_name(c), arg_to_type(c))), - ); - writeln!(generated, "pub fn {name}({args}) -> (usize, [u8; MAX_SIZE]) {{")?; - let arg_names = - comma_sep(iter_args(ty).enumerate().map(|(i, c)| format!("{}{i}", arg_to_name(c)))); - writeln!(generated, " unsafe {{ crate::encode({ty}({op}, {arg_names})) }}")?; - writeln!(generated, "}}")?; - } - } - - '_structs: { - let mut seen = std::collections::HashSet::new(); - for [_, _, ty, _] in instructions() { - if !seen.insert(ty) { - continue; - } - let types = comma_sep(iter_args(ty).map(arg_to_type).map(|s| s.to_string())); - writeln!(generated, "#[repr(packed)] pub struct {ty}(u8, {types});")?; - } - } - - '_name_list: { - writeln!(generated, "pub const NAMES: [&str; {}] = [", instructions().count())?; - for [_, name, _, _] in instructions() { - writeln!(generated, " \"{}\",", name.to_lowercase())?; - } - writeln!(generated, "];")?; - } - - let instr = "Instr"; - let oper = "Oper"; - - '_instr_enum: { - writeln!(generated, "#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[repr(u8)]")?; - writeln!(generated, "pub enum {instr} {{")?; - for [id, name, ..] in instructions() { - writeln!(generated, " {name} = {id},")?; - } - writeln!(generated, "}}")?; - } - - '_arg_kind: { - writeln!(generated, "#[derive(Debug, Clone, Copy, PartialEq, Eq)]")?; - writeln!(generated, "pub enum {oper} {{")?; - let mut seen = HashSet::new(); - for ty in instructions().flat_map(|[.., ty, _]| iter_args(ty)) { - if !seen.insert(ty) { - continue; - } - writeln!(generated, " {ty}({}),", arg_to_type(ty))?; - } - writeln!(generated, "}}")?; - } - - '_parse_opers: { - writeln!( - generated, - "/// This assumes the instruction byte is still at the beginning of the buffer" - )?; - writeln!(generated, "pub fn parse_args(bytes: &mut &[u8], kind: {instr}, buf: &mut Vec<{oper}>) -> Option<()> {{")?; - writeln!(generated, " match kind {{")?; - let mut instrs = instructions().collect::>(); - instrs.sort_unstable_by_key(|&[.., ty, _]| ty); - for group in instrs.chunk_by(|[.., a, _], [.., b, _]| a == b) { - let ty = group[0][2]; - for &[_, name, ..] in group { - writeln!(generated, " | {instr}::{name}")?; - } - generated.pop(); - writeln!(generated, " => {{")?; - if iter_args(ty).count() != 0 { - writeln!(generated, " let data = crate::decode::<{ty}>(bytes)?;")?; - writeln!( - generated, - " buf.extend([{}]);", - comma_sep( - iter_args(ty).zip(1u32..).map(|(t, i)| format!("{oper}::{t}(data.{i})")) - ) - )?; - } else { - writeln!(generated, " crate::decode::<{ty}>(bytes)?;")?; - } - - writeln!(generated, " }}")?; - } - writeln!(generated, " }}")?; - writeln!(generated, " Some(())")?; - writeln!(generated, "}}")?; - } - - std::fs::write("src/instrs.rs", generated)?; - Ok(()) -} - -fn comma_sep(items: impl Iterator) -> String { - items.map(|item| item.to_string()).collect::>().join(", ") -} - -fn instructions() -> impl Iterator { - include_str!("../hbbytecode/instructions.in") - .lines() - .filter_map(|line| line.strip_suffix(';')) - .map(|line| line.splitn(4, ',').map(str::trim).next_chunk().unwrap()) -} - -fn arg_to_type(arg: char) -> &'static str { - match arg { - 'R' | 'B' => "u8", - 'H' => "u16", - 'W' => "u32", - 'D' | 'A' => "u64", - 'P' => "i16", - 'O' => "i32", - _ => panic!("unknown type: {}", arg), - } -} - -fn arg_to_width(arg: char) -> usize { - match arg { - 'R' | 'B' => 1, - 'H' => 2, - 'W' => 4, - 'D' | 'A' => 8, - 'P' => 2, - 'O' => 4, - _ => panic!("unknown type: {}", arg), - } -} - -fn arg_to_name(arg: char) -> &'static str { - match arg { - 'R' => "reg", - 'B' | 'H' | 'W' | 'D' => "imm", - 'P' | 'O' => "offset", - 'A' => "addr", - _ => panic!("unknown type: {}", arg), - } -} - -fn iter_args(ty: &'static str) -> impl Iterator { - ty.chars().filter(|c| *c != 'N') -} diff --git a/hblang/src/lib.rs b/hblang/src/lib.rs index 4bc6835..d305722 100644 --- a/hblang/src/lib.rs +++ b/hblang/src/lib.rs @@ -27,6 +27,7 @@ use { son::reg, ty::ArrayLen, }, + hbbytecode as instrs, parser::Ast, std::{ collections::{hash_map, BTreeMap, VecDeque}, @@ -52,7 +53,6 @@ pub mod codegen; pub mod parser; pub mod son; -mod instrs; mod lexer; mod task { @@ -715,7 +715,7 @@ impl Types { output: &mut impl std::io::Write, eca_handler: impl FnMut(&mut &[u8]), ) -> std::io::Result<()> { - use crate::DisasmItem; + use instrs::DisasmItem; let functions = self .funcs .iter() @@ -744,7 +744,7 @@ impl Types { (g.offset, (name, g.data.len() as Size, DisasmItem::Global)) })) .collect::>(); - crate::disasm(&mut sluce, &functions, output, eca_handler) + instrs::disasm(&mut sluce, &functions, output, eca_handler) } fn parama(&self, ret: impl Into) -> ParamAlloc { @@ -857,181 +857,6 @@ impl Types { } } -#[inline] -unsafe fn encode(instr: T) -> (usize, [u8; instrs::MAX_SIZE]) { - let mut buf = [0; instrs::MAX_SIZE]; - std::ptr::write(buf.as_mut_ptr() as *mut T, instr); - (std::mem::size_of::(), buf) -} - -#[inline] -fn decode(binary: &mut &[u8]) -> Option { - unsafe { Some(std::ptr::read(binary.take(..std::mem::size_of::())?.as_ptr() as *const T)) } -} - -#[derive(Clone, Copy)] -enum DisasmItem { - Func, - Global, -} - -fn disasm( - binary: &mut &[u8], - functions: &BTreeMap, - out: &mut impl std::io::Write, - mut eca_handler: impl FnMut(&mut &[u8]), -) -> std::io::Result<()> { - use self::instrs::Instr; - - fn instr_from_byte(b: u8) -> std::io::Result { - if b as usize >= instrs::NAMES.len() { - return Err(std::io::ErrorKind::InvalidData.into()); - } - Ok(unsafe { std::mem::transmute::(b) }) - } - - let mut labels = HashMap::::default(); - let mut buf = Vec::::new(); - let mut has_cycle = false; - let mut has_oob = false; - - '_offset_pass: for (&off, &(_name, len, kind)) in functions.iter() { - if matches!(kind, DisasmItem::Global) { - continue; - } - - let prev = *binary; - - binary.take(..off as usize).unwrap(); - - let mut label_count = 0; - while let Some(&byte) = binary.first() { - let offset: i32 = (prev.len() - binary.len()).try_into().unwrap(); - if offset as u32 == off + len { - break; - } - let Ok(inst) = instr_from_byte(byte) else { break }; - instrs::parse_args(binary, inst, &mut buf).ok_or(std::io::ErrorKind::OutOfMemory)?; - - for op in buf.drain(..) { - let rel = match op { - instrs::Oper::O(rel) => rel, - instrs::Oper::P(rel) => rel.into(), - _ => continue, - }; - - has_cycle |= rel == 0; - - let global_offset: u32 = (offset + rel).try_into().unwrap(); - if functions.get(&global_offset).is_some() { - continue; - } - label_count += labels.try_insert(global_offset, label_count).is_ok() as u32; - } - - if matches!(inst, Instr::ECA) { - eca_handler(binary); - } - } - - *binary = prev; - } - - let mut ordered = functions.iter().collect::>(); - ordered.sort_unstable_by_key(|(_, (name, _, _))| name); - - '_dump: for (&off, &(name, len, kind)) in ordered { - if matches!(kind, DisasmItem::Global) { - continue; - } - let prev = *binary; - - writeln!(out, "{name}:")?; - - binary.take(..off as usize).unwrap(); - while let Some(&byte) = binary.first() { - let offset: i32 = (prev.len() - binary.len()).try_into().unwrap(); - if offset as u32 == off + len { - break; - } - let Ok(inst) = instr_from_byte(byte) else { - writeln!(out, "invalid instr {byte}")?; - break; - }; - instrs::parse_args(binary, inst, &mut buf).unwrap(); - - if let Some(label) = labels.get(&offset.try_into().unwrap()) { - write!(out, "{:>2}: ", label)?; - } else { - write!(out, " ")?; - } - - write!(out, "{inst:<8?} ")?; - - 'a: for (i, op) in buf.drain(..).enumerate() { - if i != 0 { - write!(out, ", ")?; - } - - let rel = 'b: { - match op { - instrs::Oper::O(rel) => break 'b rel, - instrs::Oper::P(rel) => break 'b rel.into(), - instrs::Oper::R(r) => write!(out, "r{r}")?, - instrs::Oper::B(b) => write!(out, "{b}b")?, - instrs::Oper::H(h) => write!(out, "{h}h")?, - instrs::Oper::W(w) => write!(out, "{w}w")?, - instrs::Oper::D(d) if (d as i64) < 0 => write!(out, "{}d", d as i64)?, - instrs::Oper::D(d) => write!(out, "{d}d")?, - instrs::Oper::A(a) => write!(out, "{a}a")?, - } - - continue 'a; - }; - - let global_offset: u32 = (offset + rel).try_into().unwrap(); - if let Some(&(name, ..)) = functions.get(&global_offset) { - if name.contains('\0') { - write!(out, ":{name:?}")?; - } else { - write!(out, ":{name}")?; - } - } else { - let local_has_oob = global_offset < off - || global_offset > off + len - || instr_from_byte(prev[global_offset as usize]).is_err() - || prev[global_offset as usize] == 0; - has_oob |= local_has_oob; - let label = labels.get(&global_offset).unwrap(); - if local_has_oob { - write!(out, "!!!!!!!!!{rel}")?; - } else { - write!(out, ":{label}")?; - } - } - } - - writeln!(out)?; - - if matches!(inst, Instr::ECA) { - eca_handler(binary); - } - } - - *binary = prev; - } - - if has_oob { - return Err(std::io::ErrorKind::InvalidInput.into()); - } - - if has_cycle { - return Err(std::io::ErrorKind::TimedOut.into()); - } - - Ok(()) -} - struct TaskQueue { inner: Mutex>, } diff --git a/hbvm/Cargo.toml b/hbvm/Cargo.toml index 447ddf2..a6e25b0 100644 --- a/hbvm/Cargo.toml +++ b/hbvm/Cargo.toml @@ -9,4 +9,4 @@ alloc = [] nightly = [] [dependencies] -hbbytecode = { path = "../hbbytecode" } +hbbytecode = { path = "../hbbytecode", default-features = false } diff --git a/hbvm/src/vmrun.rs b/hbvm/src/vmrun.rs index e2821f0..8d302d4 100644 --- a/hbvm/src/vmrun.rs +++ b/hbvm/src/vmrun.rs @@ -34,7 +34,7 @@ where /// Program can return [`VmRunError`] if a trap handling failed #[cfg_attr(feature = "nightly", repr(align(4096)))] pub fn run(&mut self) -> Result { - use hbbytecode::opcode::*; + use hbbytecode::Instr as I; loop { // Big match // @@ -56,105 +56,112 @@ where // - Yes, we assume you run 64 bit CPU. Else ?conradluget a better CPU // sorry 8 bit fans, HBVM won't run on your Speccy :( unsafe { - match self.memory.prog_read::(self.pc as _) { - UN => { + match self + .memory + .prog_read::(self.pc as _) + .try_into() + .map_err(VmRunError::InvalidOpcode)? + { + I::UN => { self.bump_pc::(); return Err(VmRunError::Unreachable); } - TX => { + I::TX => { self.bump_pc::(); return Ok(VmRunOk::End); } - NOP => handler!(self, |OpsN()| ()), - ADD8 => self.binary_op(u8::wrapping_add), - ADD16 => self.binary_op(u16::wrapping_add), - ADD32 => self.binary_op(u32::wrapping_add), - ADD64 => self.binary_op(u64::wrapping_add), - SUB8 => self.binary_op(u8::wrapping_sub), - SUB16 => self.binary_op(u16::wrapping_sub), - SUB32 => self.binary_op(u32::wrapping_sub), - SUB64 => self.binary_op(u64::wrapping_sub), - MUL8 => self.binary_op(u8::wrapping_mul), - MUL16 => self.binary_op(u16::wrapping_mul), - MUL32 => self.binary_op(u32::wrapping_mul), - MUL64 => self.binary_op(u64::wrapping_mul), - AND => self.binary_op::(ops::BitAnd::bitand), - OR => self.binary_op::(ops::BitOr::bitor), - XOR => self.binary_op::(ops::BitXor::bitxor), - SLU8 => self.binary_op_shift::(u8::wrapping_shl), - SLU16 => self.binary_op_shift::(u16::wrapping_shl), - SLU32 => self.binary_op_shift::(u32::wrapping_shl), - SLU64 => self.binary_op_shift::(u64::wrapping_shl), - SRU8 => self.binary_op_shift::(u8::wrapping_shr), - SRU16 => self.binary_op_shift::(u16::wrapping_shr), - SRU32 => self.binary_op_shift::(u32::wrapping_shr), - SRU64 => self.binary_op_shift::(u64::wrapping_shr), - SRS8 => self.binary_op_shift::(i8::wrapping_shr), - SRS16 => self.binary_op_shift::(i16::wrapping_shr), - SRS32 => self.binary_op_shift::(i32::wrapping_shr), - SRS64 => self.binary_op_shift::(i64::wrapping_shr), - CMPU => handler!(self, |OpsRRR(tg, a0, a1)| self.cmp( + I::NOP => handler!(self, |OpsN()| ()), + I::ADD8 => self.binary_op(u8::wrapping_add), + I::ADD16 => self.binary_op(u16::wrapping_add), + I::ADD32 => self.binary_op(u32::wrapping_add), + I::ADD64 => self.binary_op(u64::wrapping_add), + I::SUB8 => self.binary_op(u8::wrapping_sub), + I::SUB16 => self.binary_op(u16::wrapping_sub), + I::SUB32 => self.binary_op(u32::wrapping_sub), + I::SUB64 => self.binary_op(u64::wrapping_sub), + I::MUL8 => self.binary_op(u8::wrapping_mul), + I::MUL16 => self.binary_op(u16::wrapping_mul), + I::MUL32 => self.binary_op(u32::wrapping_mul), + I::MUL64 => self.binary_op(u64::wrapping_mul), + I::AND => self.binary_op::(ops::BitAnd::bitand), + I::OR => self.binary_op::(ops::BitOr::bitor), + I::XOR => self.binary_op::(ops::BitXor::bitxor), + I::SLU8 => self.binary_op_shift::(u8::wrapping_shl), + I::SLU16 => self.binary_op_shift::(u16::wrapping_shl), + I::SLU32 => self.binary_op_shift::(u32::wrapping_shl), + I::SLU64 => self.binary_op_shift::(u64::wrapping_shl), + I::SRU8 => self.binary_op_shift::(u8::wrapping_shr), + I::SRU16 => self.binary_op_shift::(u16::wrapping_shr), + I::SRU32 => self.binary_op_shift::(u32::wrapping_shr), + I::SRU64 => self.binary_op_shift::(u64::wrapping_shr), + I::SRS8 => self.binary_op_shift::(i8::wrapping_shr), + I::SRS16 => self.binary_op_shift::(i16::wrapping_shr), + I::SRS32 => self.binary_op_shift::(i32::wrapping_shr), + I::SRS64 => self.binary_op_shift::(i64::wrapping_shr), + I::CMPU => handler!(self, |OpsRRR(tg, a0, a1)| self.cmp( tg, a0, self.read_reg(a1).cast::() )), - CMPS => handler!(self, |OpsRRR(tg, a0, a1)| self.cmp( + I::CMPS => handler!(self, |OpsRRR(tg, a0, a1)| self.cmp( tg, a0, self.read_reg(a1).cast::() )), - DIRU8 => self.dir::(), - DIRU16 => self.dir::(), - DIRU32 => self.dir::(), - DIRU64 => self.dir::(), - DIRS8 => self.dir::(), - DIRS16 => self.dir::(), - DIRS32 => self.dir::(), - DIRS64 => self.dir::(), - NEG => handler!(self, |OpsRR(tg, a0)| { + I::DIRU8 => self.dir::(), + I::DIRU16 => self.dir::(), + I::DIRU32 => self.dir::(), + I::DIRU64 => self.dir::(), + I::DIRS8 => self.dir::(), + I::DIRS16 => self.dir::(), + I::DIRS32 => self.dir::(), + I::DIRS64 => self.dir::(), + I::NEG => handler!(self, |OpsRR(tg, a0)| { // Bit negation self.write_reg(tg, self.read_reg(a0).cast::().wrapping_neg()) }), - NOT => handler!(self, |OpsRR(tg, a0)| { + I::NOT => handler!(self, |OpsRR(tg, a0)| { // Logical negation self.write_reg(tg, u64::from(self.read_reg(a0).cast::() == 0)); }), - SXT8 => handler!(self, |OpsRR(tg, a0)| { + I::SXT8 => handler!(self, |OpsRR(tg, a0)| { self.write_reg(tg, self.read_reg(a0).cast::() as i64) }), - SXT16 => handler!(self, |OpsRR(tg, a0)| { + I::SXT16 => handler!(self, |OpsRR(tg, a0)| { self.write_reg(tg, self.read_reg(a0).cast::() as i64) }), - SXT32 => handler!(self, |OpsRR(tg, a0)| { + I::SXT32 => handler!(self, |OpsRR(tg, a0)| { self.write_reg(tg, self.read_reg(a0).cast::() as i64) }), - ADDI8 => self.binary_op_imm(u8::wrapping_add), - ADDI16 => self.binary_op_imm(u16::wrapping_add), - ADDI32 => self.binary_op_imm(u32::wrapping_add), - ADDI64 => self.binary_op_imm(u64::wrapping_add), - MULI8 => self.binary_op_imm(u8::wrapping_mul), - MULI16 => self.binary_op_imm(u16::wrapping_mul), - MULI32 => self.binary_op_imm(u32::wrapping_mul), - MULI64 => self.binary_op_imm(u64::wrapping_mul), - ANDI => self.binary_op_imm::(ops::BitAnd::bitand), - ORI => self.binary_op_imm::(ops::BitOr::bitor), - XORI => self.binary_op_imm::(ops::BitXor::bitxor), - SLUI8 => self.binary_op_ims::(u8::wrapping_shl), - SLUI16 => self.binary_op_ims::(u16::wrapping_shl), - SLUI32 => self.binary_op_ims::(u32::wrapping_shl), - SLUI64 => self.binary_op_ims::(u64::wrapping_shl), - SRUI8 => self.binary_op_ims::(u8::wrapping_shr), - SRUI16 => self.binary_op_ims::(u16::wrapping_shr), - SRUI32 => self.binary_op_ims::(u32::wrapping_shr), - SRUI64 => self.binary_op_ims::(u64::wrapping_shr), - SRSI8 => self.binary_op_ims::(i8::wrapping_shr), - SRSI16 => self.binary_op_ims::(i16::wrapping_shr), - SRSI32 => self.binary_op_ims::(i32::wrapping_shr), - SRSI64 => self.binary_op_ims::(i64::wrapping_shr), - CMPUI => handler!(self, |OpsRRD(tg, a0, imm)| { self.cmp(tg, a0, imm) }), - CMPSI => handler!(self, |OpsRRD(tg, a0, imm)| { self.cmp(tg, a0, imm as i64) }), - CP => handler!(self, |OpsRR(tg, a0)| self.write_reg(tg, self.read_reg(a0))), - SWA => handler!(self, |OpsRR(r0, r1)| { + I::ADDI8 => self.binary_op_imm(u8::wrapping_add), + I::ADDI16 => self.binary_op_imm(u16::wrapping_add), + I::ADDI32 => self.binary_op_imm(u32::wrapping_add), + I::ADDI64 => self.binary_op_imm(u64::wrapping_add), + I::MULI8 => self.binary_op_imm(u8::wrapping_mul), + I::MULI16 => self.binary_op_imm(u16::wrapping_mul), + I::MULI32 => self.binary_op_imm(u32::wrapping_mul), + I::MULI64 => self.binary_op_imm(u64::wrapping_mul), + I::ANDI => self.binary_op_imm::(ops::BitAnd::bitand), + I::ORI => self.binary_op_imm::(ops::BitOr::bitor), + I::XORI => self.binary_op_imm::(ops::BitXor::bitxor), + I::SLUI8 => self.binary_op_ims::(u8::wrapping_shl), + I::SLUI16 => self.binary_op_ims::(u16::wrapping_shl), + I::SLUI32 => self.binary_op_ims::(u32::wrapping_shl), + I::SLUI64 => self.binary_op_ims::(u64::wrapping_shl), + I::SRUI8 => self.binary_op_ims::(u8::wrapping_shr), + I::SRUI16 => self.binary_op_ims::(u16::wrapping_shr), + I::SRUI32 => self.binary_op_ims::(u32::wrapping_shr), + I::SRUI64 => self.binary_op_ims::(u64::wrapping_shr), + I::SRSI8 => self.binary_op_ims::(i8::wrapping_shr), + I::SRSI16 => self.binary_op_ims::(i16::wrapping_shr), + I::SRSI32 => self.binary_op_ims::(i32::wrapping_shr), + I::SRSI64 => self.binary_op_ims::(i64::wrapping_shr), + I::CMPUI => handler!(self, |OpsRRD(tg, a0, imm)| { self.cmp(tg, a0, imm) }), + I::CMPSI => { + handler!(self, |OpsRRD(tg, a0, imm)| { self.cmp(tg, a0, imm as i64) }) + } + I::CP => handler!(self, |OpsRR(tg, a0)| self.write_reg(tg, self.read_reg(a0))), + I::SWA => handler!(self, |OpsRR(r0, r1)| { // Swap registers match (r0, r1) { (0, 0) => (), @@ -167,33 +174,33 @@ where } } }), - LI8 => handler!(self, |OpsRB(tg, imm)| self.write_reg(tg, imm)), - LI16 => handler!(self, |OpsRH(tg, imm)| self.write_reg(tg, imm)), - LI32 => handler!(self, |OpsRW(tg, imm)| self.write_reg(tg, imm)), - LI64 => handler!(self, |OpsRD(tg, imm)| self.write_reg(tg, imm)), - LRA => handler!(self, |OpsRRO(tg, reg, off)| self.write_reg( + I::LI8 => handler!(self, |OpsRB(tg, imm)| self.write_reg(tg, imm)), + I::LI16 => handler!(self, |OpsRH(tg, imm)| self.write_reg(tg, imm)), + I::LI32 => handler!(self, |OpsRW(tg, imm)| self.write_reg(tg, imm)), + I::LI64 => handler!(self, |OpsRD(tg, imm)| self.write_reg(tg, imm)), + I::LRA => handler!(self, |OpsRRO(tg, reg, off)| self.write_reg( tg, self.pcrel(off).wrapping_add(self.read_reg(reg).cast::()).get(), )), // Load. If loading more than register size, continue on adjecent registers - LD => handler!(self, |OpsRRAH(dst, base, off, count)| self + I::LD => handler!(self, |OpsRRAH(dst, base, off, count)| self .load(dst, base, off, count)?), // Store. Same rules apply as to LD - ST => handler!(self, |OpsRRAH(dst, base, off, count)| self + I::ST => handler!(self, |OpsRRAH(dst, base, off, count)| self .store(dst, base, off, count)?), - LDR => handler!(self, |OpsRROH(dst, base, off, count)| self.load( + I::LDR => handler!(self, |OpsRROH(dst, base, off, count)| self.load( dst, base, self.pcrel(off).get(), count )?), - STR => handler!(self, |OpsRROH(dst, base, off, count)| self.store( + I::STR => handler!(self, |OpsRROH(dst, base, off, count)| self.store( dst, base, self.pcrel(off).get(), count )?), - BMC => { + I::BMC => { // Block memory copy match if let Some(copier) = &mut self.copier { // There is some copier, poll. @@ -227,7 +234,7 @@ where core::task::Poll::Pending => (), } } - BRC => handler!(self, |OpsRRB(src, dst, count)| { + I::BRC => handler!(self, |OpsRRB(src, dst, count)| { // Block register copy if src.checked_add(count).is_none() || dst.checked_add(count).is_none() { return Err(VmRunError::RegOutOfBounds); @@ -239,11 +246,11 @@ where usize::from(count), ); }), - JMP => { + I::JMP => { let OpsO(off) = self.decode(); self.pc = self.pc.wrapping_add(off); } - JAL => { + I::JAL => { // Jump and link. Save PC after this instruction to // specified register and jump to reg + relative offset. let OpsRRO(save, reg, offset) = self.decode(); @@ -251,7 +258,7 @@ where self.write_reg(save, self.pc.next::()); self.pc = self.pcrel(offset).wrapping_add(self.read_reg(reg).cast::()); } - JALA => { + I::JALA => { // Jump and link. Save PC after this instruction to // specified register and jump to reg let OpsRRA(save, reg, offset) = self.decode(); @@ -261,8 +268,8 @@ where Address::new(self.read_reg(reg).cast::().wrapping_add(offset)); } // Conditional jumps, jump only to immediates - JEQ => self.cond_jmp::(Ordering::Equal), - JNE => { + I::JEQ => self.cond_jmp::(Ordering::Equal), + I::JNE => { let OpsRRP(a0, a1, ja) = self.decode(); if self.read_reg(a0).cast::() != self.read_reg(a1).cast::() { self.pc = self.pcrel(ja); @@ -270,11 +277,11 @@ where self.bump_pc::(); } } - JLTS => self.cond_jmp::(Ordering::Less), - JGTS => self.cond_jmp::(Ordering::Greater), - JLTU => self.cond_jmp::(Ordering::Less), - JGTU => self.cond_jmp::(Ordering::Greater), - ECA => { + I::JLTS => self.cond_jmp::(Ordering::Less), + I::JGTS => self.cond_jmp::(Ordering::Greater), + I::JLTU => self.cond_jmp::(Ordering::Less), + I::JGTU => self.cond_jmp::(Ordering::Greater), + I::ECA => { // So we don't get timer interrupt after ECALL if TIMER_QUOTIENT != 0 { self.timer = self.timer.wrapping_add(1); @@ -283,33 +290,33 @@ where self.bump_pc::(); return Ok(VmRunOk::Ecall); } - EBP => { + I::EBP => { self.bump_pc::(); return Ok(VmRunOk::Breakpoint); } - FADD32 => self.binary_op::(ops::Add::add), - FADD64 => self.binary_op::(ops::Add::add), - FSUB32 => self.binary_op::(ops::Sub::sub), - FSUB64 => self.binary_op::(ops::Sub::sub), - FMUL32 => self.binary_op::(ops::Mul::mul), - FMUL64 => self.binary_op::(ops::Mul::mul), - FDIV32 => self.binary_op::(ops::Div::div), - FDIV64 => self.binary_op::(ops::Div::div), - FMA32 => self.fma::(), - FMA64 => self.fma::(), - FINV32 => handler!(self, |OpsRR(tg, reg)| self + I::FADD32 => self.binary_op::(ops::Add::add), + I::FADD64 => self.binary_op::(ops::Add::add), + I::FSUB32 => self.binary_op::(ops::Sub::sub), + I::FSUB64 => self.binary_op::(ops::Sub::sub), + I::FMUL32 => self.binary_op::(ops::Mul::mul), + I::FMUL64 => self.binary_op::(ops::Mul::mul), + I::FDIV32 => self.binary_op::(ops::Div::div), + I::FDIV64 => self.binary_op::(ops::Div::div), + I::FMA32 => self.fma::(), + I::FMA64 => self.fma::(), + I::FINV32 => handler!(self, |OpsRR(tg, reg)| self .write_reg(tg, 1. / self.read_reg(reg).cast::())), - FINV64 => handler!(self, |OpsRR(tg, reg)| self + I::FINV64 => handler!(self, |OpsRR(tg, reg)| self .write_reg(tg, 1. / self.read_reg(reg).cast::())), - FCMPLT32 => self.fcmp::(Ordering::Less), - FCMPLT64 => self.fcmp::(Ordering::Less), - FCMPGT32 => self.fcmp::(Ordering::Greater), - FCMPGT64 => self.fcmp::(Ordering::Greater), - ITF32 => handler!(self, |OpsRR(tg, reg)| self + I::FCMPLT32 => self.fcmp::(Ordering::Less), + I::FCMPLT64 => self.fcmp::(Ordering::Less), + I::FCMPGT32 => self.fcmp::(Ordering::Greater), + I::FCMPGT64 => self.fcmp::(Ordering::Greater), + I::ITF32 => handler!(self, |OpsRR(tg, reg)| self .write_reg(tg, self.read_reg(reg).cast::() as f32)), - ITF64 => handler!(self, |OpsRR(tg, reg)| self + I::ITF64 => handler!(self, |OpsRR(tg, reg)| self .write_reg(tg, self.read_reg(reg).cast::() as f64)), - FTI32 => handler!(self, |OpsRRB(tg, reg, mode)| self.write_reg( + I::FTI32 => handler!(self, |OpsRRB(tg, reg, mode)| self.write_reg( tg, crate::float::f32toint( self.read_reg(reg).cast::(), @@ -317,7 +324,7 @@ where .map_err(|()| VmRunError::InvalidOperand)?, ), )), - FTI64 => handler!(self, |OpsRRB(tg, reg, mode)| self.write_reg( + I::FTI64 => handler!(self, |OpsRRB(tg, reg, mode)| self.write_reg( tg, crate::float::f64toint( self.read_reg(reg).cast::(), @@ -325,9 +332,9 @@ where .map_err(|()| VmRunError::InvalidOperand)?, ), )), - FC32T64 => handler!(self, |OpsRR(tg, reg)| self + I::FC32T64 => handler!(self, |OpsRR(tg, reg)| self .write_reg(tg, self.read_reg(reg).cast::() as f64)), - FC64T32 => handler!(self, |OpsRRB(tg, reg, mode)| self.write_reg( + I::FC64T32 => handler!(self, |OpsRRB(tg, reg, mode)| self.write_reg( tg, crate::float::conv64to32( self.read_reg(reg).cast(), @@ -335,27 +342,26 @@ where .map_err(|()| VmRunError::InvalidOperand)?, ), )), - LRA16 => handler!(self, |OpsRRP(tg, reg, imm)| self.write_reg( + I::LRA16 => handler!(self, |OpsRRP(tg, reg, imm)| self.write_reg( tg, (self.pc + self.read_reg(reg).cast::() + imm + 3_u16).get(), )), - LDR16 => handler!(self, |OpsRRPH(dst, base, off, count)| self.load( + I::LDR16 => handler!(self, |OpsRRPH(dst, base, off, count)| self.load( dst, base, self.pcrel(off).get(), count )?), - STR16 => handler!(self, |OpsRRPH(dst, base, off, count)| self.store( + I::STR16 => handler!(self, |OpsRRPH(dst, base, off, count)| self.store( dst, base, self.pcrel(off).get(), count )?), - JMP16 => { + I::JMP16 => { let OpsP(off) = self.decode(); self.pc = self.pcrel(off); } - op => return Err(VmRunError::InvalidOpcode(op)), } }