eva_asm/instruction/
arithmetic.rs

1//! Stop and Arithmetic Operations.
2
3use derive_more::Display;
4
5use crate::{
6    instruction::Instruction,
7    opcode::{Mnemonic, OpCode},
8};
9
10/// Halts execution.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Display)]
12#[display("{}", self.opcode())]
13pub struct Stop;
14
15impl Instruction for Stop {
16    fn opcode(&self) -> OpCode {
17        OpCode::Known(Mnemonic::STOP)
18    }
19}
20
21/// Addition operation.
22#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Display)]
23#[display("{}", self.opcode())]
24pub struct Add;
25
26impl Instruction for Add {
27    fn opcode(&self) -> OpCode {
28        OpCode::Known(Mnemonic::ADD)
29    }
30}
31
32/// Multiplication operation.
33#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Display)]
34#[display("{}", self.opcode())]
35pub struct Mul;
36
37impl Instruction for Mul {
38    fn opcode(&self) -> OpCode {
39        OpCode::Known(Mnemonic::MUL)
40    }
41}
42
43/// Subtraction operation.
44#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Display)]
45#[display("{}", self.opcode())]
46pub struct Sub;
47
48impl Instruction for Sub {
49    fn opcode(&self) -> OpCode {
50        OpCode::Known(Mnemonic::SUB)
51    }
52}
53
54/// Integer division operation.
55#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Display)]
56#[display("{}", self.opcode())]
57pub struct Div;
58
59impl Instruction for Div {
60    fn opcode(&self) -> OpCode {
61        OpCode::Known(Mnemonic::DIV)
62    }
63}
64
65/// Signed integer division operation (truncated).
66#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Display)]
67#[display("{}", self.opcode())]
68pub struct SDiv;
69
70impl Instruction for SDiv {
71    fn opcode(&self) -> OpCode {
72        OpCode::Known(Mnemonic::SDIV)
73    }
74}
75
76/// Modulo remainder operation.
77#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Display)]
78#[display("{}", self.opcode())]
79pub struct Mod;
80
81impl Instruction for Mod {
82    fn opcode(&self) -> OpCode {
83        OpCode::Known(Mnemonic::MOD)
84    }
85}
86
87/// Signed modulo remainder operation.
88#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Display)]
89#[display("{}", self.opcode())]
90pub struct SMod;
91
92impl Instruction for SMod {
93    fn opcode(&self) -> OpCode {
94        OpCode::Known(Mnemonic::SMOD)
95    }
96}
97
98/// Modulo addition operation.
99#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Display)]
100#[display("{}", self.opcode())]
101pub struct AddMod;
102
103impl Instruction for AddMod {
104    fn opcode(&self) -> OpCode {
105        OpCode::Known(Mnemonic::ADDMOD)
106    }
107}
108
109/// Modulo multiplication operation.
110#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Display)]
111#[display("{}", self.opcode())]
112pub struct MulMod;
113
114impl Instruction for MulMod {
115    fn opcode(&self) -> OpCode {
116        OpCode::Known(Mnemonic::MULMOD)
117    }
118}
119
120/// Exponential operation.
121#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Display)]
122#[display("{}", self.opcode())]
123pub struct Exp;
124
125impl Instruction for Exp {
126    fn opcode(&self) -> OpCode {
127        OpCode::Known(Mnemonic::EXP)
128    }
129}
130
131/// Extend length of two’s complement signed integer.
132#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Display)]
133#[display("{}", self.opcode())]
134pub struct SignExtend;
135
136impl Instruction for SignExtend {
137    fn opcode(&self) -> OpCode {
138        OpCode::Known(Mnemonic::SIGNEXTEND)
139    }
140}
141
142#[cfg(test)]
143mod tests {
144    use super::*;
145
146    #[test]
147    fn stop_fmt() {
148        assert_eq!(format!("{Stop}"), String::from("STOP"));
149    }
150}