pub const XLEN: usize = 32;
pub type Alia<'a> = &'a str;
pub type AliaList<'a> = [Alia<'a>];
pub type AliaTable<'a, 'b, const N: usize> = [&'a AliaList<'b>; N];
pub const RV32_ALIA_TABLE: AliaTable<'static, 'static, XLEN> = [
&["x0", "zero"],
&["x1", "ra"],
&["x2", "sp"],
&["x3", "gp"],
&["x4", "tp"],
&["x5", "t0"],
&["x6", "t1"],
&["x7", "t2"],
&["x8", "s0", "fp"],
&["x9", "s1"],
&["x10", "a0"],
&["x11", "a1"],
&["x12", "a2"],
&["x13", "a3"],
&["x14", "a4"],
&["x15", "a5"],
&["x16", "a6"],
&["x17", "a7"],
&["x18", "s2"],
&["x19", "s3"],
&["x20", "s4"],
&["x21", "s5"],
&["x22", "s6"],
&["x23", "s7"],
&["x24", "s8"],
&["x25", "s9"],
&["x26", "s10"],
&["x27", "s11"],
&["x28", "t3"],
&["x29", "t4"],
&["x30", "t5"],
&["x31", "t6"],
];
#[macro_export]
macro_rules! getnum {
($table: expr, $alia: expr) => {
$table.iter().position(|&x| x.contains(&$alia)).unwrap()
};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_read_rv32_alia_table() {
println!("x8 alia number: {}", RV32_ALIA_TABLE[8].len());
}
#[test]
fn test_getnum() {
let table = RV32_ALIA_TABLE;
assert_eq!(getnum!(table, "x8"), 8);
assert_eq!(getnum!(table, "s0"), 8);
assert_eq!(getnum!(table, "fp"), 8);
assert_eq!(getnum!(table, "t0"), 5);
assert_eq!(getnum!(table, "t6"), 31);
}
}
I'm not sure can the Rust optimize the expression when using the constant.Such as RV32_ALIA_TABLE
. When you use this constant and write a expression (RV32_ALIA_TABLE[0].len()
). Will the Rust allocate enough memory space for RV32_ALIA_TABLE
? Or, use the item of this constant to replace this expression? (Because item of constant is constant too.). If this is no problem, Or the more complex expression (Such as getnum!
) ? Because I am a foreign person, my English is not good. I'm sorry for that.