wrap assembly code generator in a macro

This macro takes as arguments the name of the function
you want to use to retrieve the assembly code, and two labels,
one for the start and one for the end of the assembly code,
and the assembly code itself.

The labels for the start/end of assembly code are identifiers
and because of Rust's rules macros can't create identifiers out
of thin air (I wish instead of saying "partially hygienic", they
just said, "you can't create identifiers that don't exist". Yes,
I'm sure "partially hygienic" has some other meaning to computer
science people but to me it's just pretentious bullshit that
forces me to read through four blog posts and three issues to
come to the practical conclusion I really wanted to know; I
think maybe they wanted to refer to it as hygiene because the
limitation is super frustrating and hygiene sounds like a positive
way to spin something that is annoying. Sure. It's fine. You
didn't want to allow macros to make identifiers because it can lead
to surprising behavior and name space conflicts. That's your choice
as a language designer, just say it.).

Anyways. as usual, rust macro took way too long to write.
This commit is contained in:
bunnie 2024-03-01 16:39:30 +08:00
parent f24746b88a
commit 260ec0b39b
2 changed files with 91 additions and 58 deletions

View file

@ -40,8 +40,8 @@ pub fn hello_multiverse() {
let mut bio_ss = BioSharedState::new();
// stop all the machines, so that code can be loaded
bio_ss.bio.wo(utra::bio::SFR_CTRL, 0x0);
let mv_test_code = fn_to_slice(multiverse_test, multiverse_test_endcap);
bio_ss.load_code(mv_test_code, 0);
let code = multiverse_code();
bio_ss.load_code(code, 0);
// configure & run the 0th machine
// /32 clock
@ -54,59 +54,52 @@ pub fn hello_multiverse() {
report_api(0x1310_10FF);
}
pub unsafe fn multiverse_test() {
core::arch::asm!(
// reset vectors for each machine
"j 0f",
"nop",
"j 1f",
"nop",
"j 2f",
"nop",
"j 3f",
"nop",
// mach 0 code
"0:",
"li x26, 0xFF",
"add x1, zero, 0x10",
"4:",
"add x1, x1, 0x1",
"mv x21, x1",
"mv x20, zero",
"j 4b",
// mach 1 code
"1:",
"li x26, 0xFF00",
"add x1, zero, 0x20",
"5:",
"add x1, x1, 0x1",
"slli x21, x1, 8",
"mv x20, zero",
"j 5b",
// mach 2 code
"2:",
"li x26, 0xFF0000",
"add x1, zero, 0x20",
"6:",
"add x1, x1, 0x1",
"slli x21, x1, 16",
"mv x20, zero",
"j 6b",
// mach 3 code
"3:",
"li x26, 0xFF0000",
"add x1, zero, 0x20",
"7:",
"add x1, x1, 0x1",
"slli x21, x1, 24",
"mv x20, zero",
"j 7b",
);
}
// this marks the "end address" of simple_test
pub unsafe fn multiverse_test_endcap() {
core::arch::asm!(
"nop"
);
}
bio_code!(multiverse_code, MULTIVERSE_START, MULTIVERSE_END,
"jal x0, 0f",
// reset vectors for each machine
// use `jal x0, target` encoding to force non-C interpretation
// as the reset vectors for each core are aligned to 4-byte boundaries
"jal x0, 0f",
"jal x0, 1f",
"jal x0, 2f",
"jal x0, 3f",
// mach 0 code
"0:",
// x26 sets the GPIO mask
"li x26, 0xFF",
"add x1, zero, 0x10",
"4:",
"add x1, x1, 0x1",
// x21 write clobbers the GPIO bits, ANDed with mask in x26
"mv x21, x1",
// x20 write causes core to wait until next sync quantum
"mv x20, zero",
"j 4b",
// mach 1 code
"1:",
"li x26, 0xFF00",
"add x1, zero, 0x20",
"5:",
"add x1, x1, 0x1",
"slli x21, x1, 8",
"mv x20, zero",
"j 5b",
// mach 2 code
"2:",
"li x26, 0xFF0000",
"add x1, zero, 0x20",
"6:",
"add x1, x1, 0x1",
"slli x21, x1, 16",
"mv x20, zero",
"j 6b",
// mach 3 code
"3:",
"li x26, 0xFF0000",
"add x1, zero, 0x20",
"7:",
"add x1, x1, 0x1",
"slli x21, x1, 24",
"mv x20, zero",
"j 7b"
);

View file

@ -94,4 +94,44 @@ impl BioSharedState {
}
}
}
}
#[macro_export]
/// This macro takes three identifiers and assembly code:
/// - name of the function to call to retrieve the assembled code
/// - a unique identifier that serves as label name for the start of the code
/// - a unique identifier that serves as label name for the end of the code
/// - a comma separated list of strings that form the assembly itself
///
/// *** The comma separated list must *not* end in a comma. ***
///
/// The macro is unable to derive names of functions or identifiers for labels
/// due to the partially hygienic macro rules of Rust, so you have to come
/// up with a list of unique names by yourself.
macro_rules! bio_code {
($fn_name:ident, $name_start:ident, $name_end:ident, $($item:expr),*) => {
pub fn $fn_name() -> &'static [u8] {
extern {
static $name_start: *const u8;
static $name_end: *const u8;
}
unsafe {
report_api($name_start as u32);
report_api($name_end as u32);
}
unsafe { core::slice::from_raw_parts($name_start, ($name_end as usize) - ($name_start as usize))}
}
core::arch::global_asm!(
".align 4",
concat!(".globl ", stringify!($name_start)),
concat!(stringify!($name_start), ":"),
".word .",
$($item),*
, ".align 4",
concat!(".globl ", stringify!($name_end)),
concat!(stringify!($name_end), ":"),
".word .",
);
};
}