2023-06-26 05:18:14 -05:00
|
|
|
#![no_std]
|
|
|
|
#![feature(error_in_core)]
|
|
|
|
|
|
|
|
extern crate alloc;
|
|
|
|
|
2023-07-10 16:18:23 -05:00
|
|
|
pub mod text;
|
2023-06-20 19:07:48 -05:00
|
|
|
|
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-10 16:18:23 -05:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Assembler {
|
|
|
|
pub buf: Vec<u8>,
|
|
|
|
sub: HashSet<usize>,
|
2023-06-20 19:07:48 -05:00
|
|
|
}
|
|
|
|
|
2023-07-10 16:18:23 -05:00
|
|
|
impl Assembler {
|
|
|
|
macros::impl_asm!(
|
|
|
|
bbbb(p0: u8, p1: u8, p2: u8, p3: u8)
|
2023-07-10 19:08:55 -05:00
|
|
|
=> [DIR, DIRF, FMAF],
|
2023-07-10 16:18:23 -05:00
|
|
|
bbb(p0: u8, p1: u8, p2: u8)
|
2023-07-10 19:08:55 -05:00
|
|
|
=> [ADD, SUB, MUL, AND, OR, XOR, SL, SR, SRS, CMP, CMPU, BRC, ADDF, SUBF, MULF],
|
2023-07-10 16:18:23 -05:00
|
|
|
bbdh(p0: u8, p1: u8, p2: impl Imm, p3: u16)
|
|
|
|
=> [LD, ST],
|
|
|
|
bbd(p0: u8, p1: u8, p2: impl Imm)
|
|
|
|
=> [ADDI, MULI, ANDI, ORI, XORI, SLI, SRI, SRSI, CMPI, CMPUI,
|
2023-07-10 19:08:55 -05:00
|
|
|
BMC, JEQ, JNE, JLT, JGT, JLTU, JGTU, ADDFI, MULFI],
|
2023-07-10 16:18:23 -05:00
|
|
|
bb(p0: u8, p1: u8)
|
2023-07-10 19:08:55 -05:00
|
|
|
=> [NEG, NOT, CP, SWA, NEGF, ITF, FTI],
|
2023-07-10 16:18:23 -05:00
|
|
|
bd(p0: u8, p1: impl Imm)
|
|
|
|
=> [LI, JMP],
|
|
|
|
n()
|
|
|
|
=> [NOP, ECALL],
|
|
|
|
);
|
2023-06-20 19:07:48 -05:00
|
|
|
}
|
|
|
|
|
2023-07-10 16:18:23 -05:00
|
|
|
pub trait Imm {
|
2023-07-10 19:08:55 -05:00
|
|
|
fn insert(&self, asm: &mut Assembler);
|
2023-06-20 19:07:48 -05:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
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);
|
|
|
|
|
2023-07-10 16:18:23 -05:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
|
|
pub struct Symbol(pub u64);
|
|
|
|
impl Imm for Symbol {
|
|
|
|
#[inline(always)]
|
2023-07-10 19:08:55 -05:00
|
|
|
fn insert(&self, asm: &mut Assembler) {
|
2023-07-10 16:18:23 -05:00
|
|
|
asm.sub.insert(asm.buf.len());
|
|
|
|
asm.buf.extend(self.0.to_le_bytes());
|
2023-06-20 19:07:48 -05:00
|
|
|
}
|
|
|
|
}
|