Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to macro assembly #117

Merged
merged 11 commits into from
May 6, 2024
158 changes: 9 additions & 149 deletions src/isa/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ impl ::std::error::Error for BytecodeError {
/// bound by u16), (3) it provides too many fails in situations when we can't
/// fail because of `u16`-bounding and exclusive in-memory encoding handling.
pub trait Bytecode {
/// Returns number of bytes which instruction and its argument occupies
fn byte_count(&self) -> u16;

/// Returns range of instruction btecodes covered by a set of operations
fn instr_range() -> RangeInclusive<u8>;

Expand Down Expand Up @@ -106,26 +103,6 @@ impl<Extension> Bytecode for Instr<Extension>
where
Extension: InstructionSet,
{
fn byte_count(&self) -> u16 {
match self {
Instr::ControlFlow(instr) => instr.byte_count(),
Instr::Put(instr) => instr.byte_count(),
Instr::Move(instr) => instr.byte_count(),
Instr::Cmp(instr) => instr.byte_count(),
Instr::Arithmetic(instr) => instr.byte_count(),
Instr::Bitwise(instr) => instr.byte_count(),
Instr::Bytes(instr) => instr.byte_count(),
Instr::Digest(instr) => instr.byte_count(),
#[cfg(feature = "secp256k1")]
Instr::Secp256k1(instr) => instr.byte_count(),
#[cfg(feature = "curve25519")]
Instr::Curve25519(instr) => instr.byte_count(),
Instr::ExtensionCodes(instr) => instr.byte_count(),
Instr::ReservedInstruction(instr) => instr.byte_count(),
Instr::Nop => 1,
}
}

#[inline]
fn instr_range() -> RangeInclusive<u8> { 0..=u8::MAX }

Expand Down Expand Up @@ -235,24 +212,13 @@ where
}

impl Bytecode for ControlFlowOp {
fn byte_count(&self) -> u16 {
match self {
ControlFlowOp::Fail | ControlFlowOp::Succ => 1,
ControlFlowOp::Jmp(_) | ControlFlowOp::Jif(_) => 3,
ControlFlowOp::Routine(_) => 3,
ControlFlowOp::Call(_) => 4,
ControlFlowOp::Exec(_) => 4,
ControlFlowOp::Ret => 1,
}
}

#[inline]
fn instr_range() -> RangeInclusive<u8> { INSTR_FAIL..=INSTR_RET }

fn instr_byte(&self) -> u8 {
match self {
ControlFlowOp::Fail => INSTR_FAIL,
ControlFlowOp::Succ => INSTR_SUCC,
ControlFlowOp::Test => INSTR_TEST,
ControlFlowOp::Jmp(_) => INSTR_JMP,
ControlFlowOp::Jif(_) => INSTR_JIF,
ControlFlowOp::Routine(_) => INSTR_ROUTINE,
Expand All @@ -276,7 +242,7 @@ impl Bytecode for ControlFlowOp {
{
match self {
ControlFlowOp::Fail => {}
ControlFlowOp::Succ => {}
ControlFlowOp::Test => {}
ControlFlowOp::Jmp(pos) | ControlFlowOp::Jif(pos) | ControlFlowOp::Routine(pos) => {
writer.write_u16(*pos)?
}
Expand All @@ -295,7 +261,7 @@ impl Bytecode for ControlFlowOp {
{
Ok(match reader.read_u8()? {
INSTR_FAIL => Self::Fail,
INSTR_SUCC => Self::Succ,
INSTR_TEST => Self::Test,
INSTR_JMP => Self::Jmp(reader.read_u16()?),
INSTR_JIF => Self::Jif(reader.read_u16()?),
INSTR_ROUTINE => Self::Routine(reader.read_u16()?),
Expand All @@ -308,17 +274,6 @@ impl Bytecode for ControlFlowOp {
}

impl Bytecode for PutOp {
fn byte_count(&self) -> u16 {
match self {
PutOp::ClrA(_, _) | PutOp::ClrF(_, _) | PutOp::ClrR(_, _) => 2,
PutOp::PutA(_, _, _)
| PutOp::PutIfA(_, _, _)
| PutOp::PutF(_, _, _)
| PutOp::PutR(_, _, _)
| PutOp::PutIfR(_, _, _) => 3,
}
}

#[inline]
fn instr_range() -> RangeInclusive<u8> { INSTR_CLRA..=INSTR_PUTIFR }

Expand Down Expand Up @@ -437,9 +392,6 @@ impl Bytecode for PutOp {
}

impl Bytecode for MoveOp {
#[inline]
fn byte_count(&self) -> u16 { 3 }

#[inline]
fn instr_range() -> RangeInclusive<u8> { INSTR_MOV..=INSTR_CFA }

Expand Down Expand Up @@ -604,23 +556,6 @@ impl Bytecode for MoveOp {
}

impl Bytecode for CmpOp {
fn byte_count(&self) -> u16 {
match self {
CmpOp::GtA(_, _, _, _)
| CmpOp::LtA(_, _, _, _)
| CmpOp::GtF(_, _, _, _)
| CmpOp::LtF(_, _, _, _)
| CmpOp::GtR(_, _, _)
| CmpOp::LtR(_, _, _)
| CmpOp::EqA(_, _, _, _)
| CmpOp::EqF(_, _, _, _)
| CmpOp::EqR(_, _, _, _) => 3,
CmpOp::IfZA(_, _) | CmpOp::IfZR(_, _) | CmpOp::IfNA(_, _) | CmpOp::IfNR(_, _) => 2,
CmpOp::St(_, _, _) => 2,
CmpOp::StInv => 1,
}
}

#[inline]
fn instr_range() -> RangeInclusive<u8> { INSTR_LGT..=INSTR_STINV }

Expand Down Expand Up @@ -775,22 +710,6 @@ impl Bytecode for CmpOp {
}

impl Bytecode for ArithmeticOp {
fn byte_count(&self) -> u16 {
match self {
ArithmeticOp::AddA(_, _, _, _)
| ArithmeticOp::AddF(_, _, _, _)
| ArithmeticOp::SubA(_, _, _, _)
| ArithmeticOp::SubF(_, _, _, _)
| ArithmeticOp::MulA(_, _, _, _)
| ArithmeticOp::MulF(_, _, _, _)
| ArithmeticOp::DivA(_, _, _, _)
| ArithmeticOp::DivF(_, _, _, _)
| ArithmeticOp::Rem(_, _, _, _)
| ArithmeticOp::Stp(_, _, _) => 3,
ArithmeticOp::Neg(_, _) | ArithmeticOp::Abs(_, _) => 2,
}
}

#[inline]
fn instr_range() -> RangeInclusive<u8> { INSTR_ADD..=INSTR_REM }

Expand Down Expand Up @@ -898,23 +817,6 @@ impl Bytecode for ArithmeticOp {
}

impl Bytecode for BitwiseOp {
fn byte_count(&self) -> u16 {
match self {
BitwiseOp::And(_, _, _, _) | BitwiseOp::Or(_, _, _, _) | BitwiseOp::Xor(_, _, _, _) => {
3
}
BitwiseOp::Not(_, _) => 2,

BitwiseOp::Shl(_, _, _, _)
| BitwiseOp::ShrA(_, _, _, _, _)
| BitwiseOp::ShrR(_, _, _, _)
| BitwiseOp::Scl(_, _, _, _)
| BitwiseOp::Scr(_, _, _, _) => 3,

BitwiseOp::RevA(_, _) | BitwiseOp::RevR(_, _) => 2,
}
}

#[inline]
fn instr_range() -> RangeInclusive<u8> { INSTR_AND..=INSTR_REVR }

Expand Down Expand Up @@ -1071,24 +973,6 @@ impl Bytecode for BitwiseOp {
}

impl Bytecode for BytesOp {
fn byte_count(&self) -> u16 {
match self {
BytesOp::Put(_, _, _) => 6,
BytesOp::Mov(_, _) | BytesOp::Swp(_, _) => 2,
BytesOp::Fill(_, _, _, _, _) => 3,
BytesOp::Len(_, _, _) | BytesOp::Cnt(_, _, _) => 3,
BytesOp::Eq(_, _) => 2,
BytesOp::Con(_, _, _, _, _) => 4,
BytesOp::Find(_, _) => 2,
BytesOp::Extr(_, _, _, _) | BytesOp::Inj(_, _, _, _) => 3,
BytesOp::Join(_, _, _) => 3,
BytesOp::Splt(_, _, _, _, _) => 4,
BytesOp::Ins(_, _, _, _) => 3,
BytesOp::Del(_, _, _, _, _, _, _, _, _) => 4,
BytesOp::Rev(_, _) => 2,
}
}

#[inline]
fn instr_range() -> RangeInclusive<u8> { INSTR_PUT..=INSTR_REV }

Expand Down Expand Up @@ -1159,8 +1043,8 @@ impl Bytecode for BytesOp {
writer.write_bool(false)?;
}
BytesOp::Extr(src, dst, index, offset) | BytesOp::Inj(src, dst, index, offset) => {
writer.write_u5(src)?;
writer.write_u3(dst)?;
writer.write_u4(src)?;
writer.write_u4(dst)?;
writer.write_u4(index)?;
writer.write_u4(offset)?;
}
Expand Down Expand Up @@ -1242,14 +1126,14 @@ impl Bytecode for BytesOp {
op
}
INSTR_EXTR => Self::Extr(
reader.read_u5()?.into(),
reader.read_u3()?.into(),
reader.read_u4()?.into(),
reader.read_u4()?.into(),
reader.read_u4()?.into(),
reader.read_u4()?.into(),
),
INSTR_INJ => Self::Inj(
reader.read_u5()?.into(),
reader.read_u3()?.into(),
reader.read_u4()?.into(),
reader.read_u4()?.into(),
reader.read_u4()?.into(),
reader.read_u4()?.into(),
),
Expand Down Expand Up @@ -1288,9 +1172,6 @@ impl Bytecode for BytesOp {
}

impl Bytecode for DigestOp {
#[inline]
fn byte_count(&self) -> u16 { 3 }

#[inline]
fn instr_range() -> RangeInclusive<u8> { INSTR_RIPEMD..=INSTR_SHA512 }

Expand Down Expand Up @@ -1338,15 +1219,6 @@ impl Bytecode for DigestOp {
}

impl Bytecode for Secp256k1Op {
fn byte_count(&self) -> u16 {
match self {
Secp256k1Op::Gen(_, _) => 2,
Secp256k1Op::Mul(_, _, _, _) => 3,
Secp256k1Op::Add(_, _) => 2,
Secp256k1Op::Neg(_, _) => 2,
}
}

#[inline]
fn instr_range() -> RangeInclusive<u8> { INSTR_SECP_GEN..=INSTR_SECP_NEG }

Expand Down Expand Up @@ -1406,15 +1278,6 @@ impl Bytecode for Secp256k1Op {
}

impl Bytecode for Curve25519Op {
fn byte_count(&self) -> u16 {
match self {
Curve25519Op::Gen(_, _) => 2,
Curve25519Op::Mul(_, _, _, _) => 3,
Curve25519Op::Add(_, _, _, _) => 3,
Curve25519Op::Neg(_, _) => 2,
}
}

#[inline]
fn instr_range() -> RangeInclusive<u8> { INSTR_ED_GEN..=INSTR_ED_NEG }

Expand Down Expand Up @@ -1481,9 +1344,6 @@ impl Bytecode for Curve25519Op {
}

impl Bytecode for ReservedOp {
#[inline]
fn byte_count(&self) -> u16 { 1 }

#[inline]
fn instr_range() -> RangeInclusive<u8> { INSTR_RESV_FROM..=INSTR_ISAE_TO }

Expand Down
19 changes: 12 additions & 7 deletions src/isa/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,12 @@ impl InstructionSet for ControlFlowOp {
regs.st0 = false;
ExecStep::Stop
}
ControlFlowOp::Succ => {
regs.st0 = true;
ExecStep::Stop
ControlFlowOp::Test => {
if regs.st0 {
ExecStep::Next
} else {
ExecStep::Stop
}
}
ControlFlowOp::Jmp(offset) => {
regs.jmp().map(|_| ExecStep::Jump(*offset)).unwrap_or(ExecStep::Stop)
Expand Down Expand Up @@ -1317,7 +1320,7 @@ impl InstructionSet for BytesOp {
let offset = regs.a16[*offset as u8 as usize].filter(|e| *e < s_len)?;
let end = offset
.checked_add(dst.layout().bytes())
.filter(|e| *e < s_len)
.filter(|e| *e <= s_len)
.unwrap_or_else(|| {
regs.st0 = false;
s_len
Expand Down Expand Up @@ -1775,7 +1778,9 @@ mod tests {
assert_eq!(register.get_n(RegA::A16, Reg32::Reg1), MaybeNumber::none());
assert_eq!(register.get_n(RegA::A16, Reg32::Reg2), MaybeNumber::none());
assert!(!register.st0);
ControlFlowOp::Succ.exec(&mut register, lib_site, &());
CmpOp::StInv.exec(&mut register, lib_site, &());
assert!(register.st0);
ControlFlowOp::Test.exec(&mut register, lib_site, &());

let s1 = [0u8; u16::MAX as usize];
let s2 = [0u8; u16::MAX as usize];
Expand Down Expand Up @@ -1903,8 +1908,8 @@ mod tests {
&(),
);
assert!(!register.st0);
ControlFlowOp::Succ.exec(&mut register, lib_site, &());
assert!(register.st0);
ControlFlowOp::Test.exec(&mut register, lib_site, &());
assert!(!register.st0);
CmpOp::EqR(NoneEqFlag::NonEqual, RegR::R512, Reg32::Reg0, Reg32::Reg2).exec(
&mut register,
lib_site,
Expand Down
Loading
Loading