2023-06-26 05:18:14 -05:00
|
|
|
#![no_std]
|
|
|
|
|
|
|
|
extern crate alloc;
|
|
|
|
|
2023-07-10 16:18:23 -05:00
|
|
|
mod macros;
|
2023-06-20 19:07:48 -05:00
|
|
|
|
2023-07-10 16:18:23 -05:00
|
|
|
use {alloc::vec::Vec, hashbrown::HashSet};
|
2023-06-20 19:07:48 -05:00
|
|
|
|
2023-07-21 19:26:03 -05:00
|
|
|
/// Assembler
|
|
|
|
///
|
|
|
|
/// - Opcode-generic, instruction-type-specific methods are named `i_param_<type>`
|
|
|
|
/// - You likely won't need to use them, but they are here, just in case :)
|
|
|
|
/// - Instruction-specific methods are named `i_<instruction>`
|
2023-07-10 16:18:23 -05:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Assembler {
|
|
|
|
pub buf: Vec<u8>,
|
2023-07-11 19:16:23 -05:00
|
|
|
pub sub: HashSet<usize>,
|
2023-06-20 19:07:48 -05:00
|
|
|
}
|
|
|
|
|
2023-07-21 19:26:03 -05:00
|
|
|
|
|
|
|
// Implement both assembler and generate module for text-code-based one
|
2023-07-11 19:16:23 -05:00
|
|
|
macros::impl_both!(
|
|
|
|
bbbb(p0: R, p1: R, p2: R, p3: R)
|
|
|
|
=> [DIR, DIRF, FMAF],
|
|
|
|
bbb(p0: R, p1: R, p2: R)
|
2023-07-11 19:23:47 -05:00
|
|
|
=> [ADD, SUB, MUL, AND, OR, XOR, SL, SR, SRS, CMP, CMPU, /*BRC,*/ ADDF, SUBF, MULF],
|
2023-07-11 19:16:23 -05:00
|
|
|
bbdh(p0: R, p1: R, p2: I, p3: u16)
|
|
|
|
=> [LD, ST],
|
|
|
|
bbd(p0: R, p1: R, p2: I)
|
|
|
|
=> [ADDI, MULI, ANDI, ORI, XORI, SLI, SRI, SRSI, CMPI, CMPUI,
|
2023-07-12 05:45:50 -05:00
|
|
|
BMC, JAL, JEQ, JNE, JLT, JGT, JLTU, JGTU, ADDFI, MULFI],
|
2023-07-11 19:16:23 -05:00
|
|
|
bb(p0: R, p1: R)
|
|
|
|
=> [NEG, NOT, CP, SWA, NEGF, ITF, FTI],
|
|
|
|
bd(p0: R, p1: I)
|
2023-07-12 05:45:50 -05:00
|
|
|
=> [LI],
|
2023-07-11 19:16:23 -05:00
|
|
|
n()
|
2023-07-13 04:05:41 -05:00
|
|
|
=> [UN, NOP, ECALL],
|
2023-07-11 19:16:23 -05:00
|
|
|
);
|
2023-06-20 19:07:48 -05:00
|
|
|
|
2023-07-11 19:23:47 -05:00
|
|
|
impl Assembler {
|
2023-07-21 19:26:03 -05:00
|
|
|
// Special-cased for text-assembler
|
|
|
|
//
|
|
|
|
// `p2` is not a register, but the instruction is still BBB
|
2023-07-11 19:23:47 -05:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn i_brc(&mut self, p0: u8, p1: u8, p2: u8) {
|
|
|
|
self.i_param_bbb(hbbytecode::opcode::BRC, p0, p1, p2)
|
|
|
|
}
|
2023-07-13 04:05:41 -05:00
|
|
|
|
|
|
|
/// Append 12 zeroes (UN) at the end
|
2023-07-21 19:28:05 -05:00
|
|
|
///
|
|
|
|
/// # HBVM lore
|
|
|
|
///
|
|
|
|
/// In reference HBVM implementation checks are done in
|
|
|
|
/// a separate phase before execution.
|
|
|
|
///
|
|
|
|
/// This way execution will be much faster as they have to
|
|
|
|
/// be done only once.
|
|
|
|
///
|
|
|
|
/// There was an issue. You cannot statically check register values and
|
|
|
|
/// `JAL` instruction could hop at the end of program to some byte, which
|
|
|
|
/// will be interpreted as opcode and VM in attempt to decode the instruction
|
|
|
|
/// performed out-of-bounds read which leads to undefined behaviour.
|
|
|
|
///
|
|
|
|
/// Several options were considered to overcome this, but inserting some data at
|
|
|
|
/// program's end which when executed would lead to undesired behaviour, though
|
|
|
|
/// not undefined behaviour.
|
|
|
|
///
|
|
|
|
/// Newly created `UN` (as UNreachable) was chosen as
|
|
|
|
/// - It was a good idea to add some equivalent to `ud2` anyways
|
|
|
|
/// - It was chosen to be zero
|
|
|
|
/// - What if you somehow reached that code, it will appropriately bail :)
|
|
|
|
/// - (yes, originally `NOP` was considered)
|
|
|
|
///
|
|
|
|
/// Why 12 bytes? That's the size of largest instruction parameter part.
|
2023-07-13 04:05:41 -05:00
|
|
|
pub fn finalise(&mut self) {
|
|
|
|
self.buf.extend([0; 12]);
|
|
|
|
}
|
2023-07-11 19:23:47 -05:00
|
|
|
}
|
|
|
|
|
2023-07-21 19:26:03 -05:00
|
|
|
/// Immediate value
|
|
|
|
///
|
|
|
|
/// # Implementor notice
|
|
|
|
/// It should insert exactly 8 bytes, otherwise output will be malformed.
|
|
|
|
/// This is not checked in any way
|
2023-07-10 16:18:23 -05:00
|
|
|
pub trait Imm {
|
2023-07-21 19:26:03 -05:00
|
|
|
/// Insert immediate value
|
2023-07-10 19:08:55 -05:00
|
|
|
fn insert(&self, asm: &mut Assembler);
|
2023-06-20 19:07:48 -05:00
|
|
|
}
|
|
|
|
|
2023-07-21 19:26:03 -05:00
|
|
|
/// Implement immediate values
|
2023-07-10 19:08:55 -05:00
|
|
|
macro_rules! impl_imm_le_bytes {
|
|
|
|
($($ty:ty),* $(,)?) => {
|
|
|
|
$(
|
|
|
|
impl Imm for $ty {
|
|
|
|
#[inline(always)]
|
|
|
|
fn insert(&self, asm: &mut Assembler) {
|
2023-07-21 19:26:03 -05:00
|
|
|
// Convert to little-endian bytes, insert.
|
2023-07-10 19:08:55 -05:00
|
|
|
asm.buf.extend(self.to_le_bytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
};
|
2023-06-20 19:07:48 -05:00
|
|
|
}
|
|
|
|
|
2023-07-10 19:08:55 -05:00
|
|
|
impl_imm_le_bytes!(u64, i64, f64);
|