Skip to content

Commit

Permalink
Improve docs on macro
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementNerma committed Oct 3, 2024
1 parent 0765a21 commit 4475b97
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions lrvm/src/cpu/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,54 +120,71 @@ impl Cpu {
// 'REG_OR_LIT_1' = parameter is either a register or a 1-byte literal
// 'REG_OR_LIT_2' = parameter is either a register or a 2-bytes literal
macro_rules! args {
// <register>
(REG) => {
params[0]
};
// <register or literal>
(REG_OR_LIT_1) => {
__reg_or_lit!(0, 1)
};
// <register or long literal>
(REG_OR_LIT_2) => {
__reg_or_lit!(0, 2)
};
// <register>, <register>
(REG, REG) => {
(params[0], params[1])
};
// <register>, <register or literal>
(REG, REG_OR_LIT_1) => {
(params[0], __reg_or_lit!(1, 1))
};
// <register>, <register or long literal>
(REG, REG_OR_LIT_2) => {
(params[0], __reg_or_lit!(1, 2))
};
// <register or literal>, <register>
(REG_OR_LIT_1, REG) => {
(__reg_or_lit!(0, 1), params[1])
};
// <register or literal>, <register or literal>
(REG_OR_LIT_1, REG_OR_LIT_1) => {
(__reg_or_lit!(0, 1), __reg_or_lit!(1, 1))
};
// <register or literal>, <register or long literal>
(REG_OR_LIT_1, REG_OR_LIT_2) => {
(__reg_or_lit!(0, 1), __reg_or_lit!(1, 2))
};
// <register>, <register>, <register>
(REG, REG, REG) => {
(params[0], params[1], params[2])
};
// <register>, <register>, <register or literal>
(REG, REG, REG_OR_LIT_1) => {
(params[0], params[1], __reg_or_lit!(2, 1))
};
// <register>, <register or literal>, <register>
(REG, REG_OR_LIT_1, REG) => {
(params[0], __reg_or_lit!(1, 1), params[2])
};
// <register>, <register or literal>, <register or literal>
(REG, REG_OR_LIT_1, REG_OR_LIT_1) => {
(params[0], __reg_or_lit!(1, 1), __reg_or_lit!(2, 1))
};
// <register or literal>, <register>, <register>
(REG_OR_LIT_1, REG, REG) => {
(__reg_or_lit!(0, 1), params[1], params[2])
};
// <register or literal>, <register>, <register or literal>
(REG_OR_LIT_1, REG, REG_OR_LIT_1) => {
(__reg_or_lit!(0, 1), params[1], __reg_or_lit!(2, 1))
};
// <register or ltieral>, <register or ltieral>, <register>
(REG_OR_LIT_1, REG_OR_LIT_1, REG) => {
(__reg_or_lit!(0, 1), __reg_or_lit!(1, 1), params[2])
};
// <register or literal>, <register or literal>, <register or literal>
(REG_OR_LIT_1, REG_OR_LIT_1, REG_OR_LIT_1) => {
(
__reg_or_lit!(0, 1),
Expand All @@ -179,20 +196,26 @@ impl Cpu {

// Decode a register-or-literal parameter
macro_rules! __reg_or_lit {
// '$param' is the parameter first byte's index (starting from 0)
// '$value' is the decoded parameter's value (combined bytes of params[$param..=$param+<param length>])
// If the specified parameter is marked as being a register, the provided value is considered to be a register ID and the we try
// to read its value. Else, the provided value is a plain number.
(with_val $param: expr, $value: expr) => {
if opregs[$param] { self.read_reg(params[$param])? } else { $value }
};
// 1-byte long parameters
($param: expr, 1) => {
__reg_or_lit!(with_val $param, params[$param].into())
__reg_or_lit!(_with_val $param, params[$param].into())
};
// 2-bytes long parameters
($param: expr, 2) => {
__reg_or_lit!(with_val $param, u16::from_be_bytes([ params[$param], params[$param + 1] ]).into())
__reg_or_lit!(_with_val $param, u16::from_be_bytes([ params[$param], params[$param + 1] ]).into())
};
// <internal>
// '$param' is the parameter first byte's index (starting from 0)
// '$literal' is the decoded parameter's value in case it's not a register (combined bytes of params[$param..=$param+<param length>])
(_with_val $param: expr, $literal: expr) => {
// If the parameter is a register...
if opregs[$param] {
// Try to read it
self.read_reg(params[$param])?
} else {
// Otherwise it's a literal
$literal
}
};
}

Expand Down

0 comments on commit 4475b97

Please sign in to comment.