Skip to main content

sbpf_common/
optype.rs

1use {
2    crate::{
3        errors::{ExecutionError, SBPFError},
4        execute::Vm,
5        instruction::Instruction,
6    },
7    sbpf_common_derive::OpcodeGroup,
8};
9
10pub type DecodeFn = fn(&[u8]) -> Result<Instruction, SBPFError>;
11pub type ValidateFn = fn(&Instruction) -> Result<(), SBPFError>;
12pub type ExecuteFn = fn(&mut dyn Vm, &Instruction) -> Result<(), ExecutionError>;
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, OpcodeGroup)]
15#[handlers(
16    decode = DecodeFn,
17    validate = ValidateFn,
18    execute = ExecuteFn,
19)]
20pub enum OperationType {
21    #[group(
22        title = "Load Immediate Operation",
23        description = "Load a 64-bit immediate value into a register.",
24        decode = crate::decode::decode_load_immediate,
25        validate = crate::validate::validate_load_immediate,
26        execute = crate::execute::execute_load_immediate,
27    )]
28    LoadImmediate,
29    #[group(
30        title = "Load Memory Operations",
31        description = "Load a value from memory into a register.",
32        decode = crate::decode::decode_load_memory,
33        validate = crate::validate::validate_load_memory,
34        execute = crate::execute::execute_load_memory,
35    )]
36    LoadMemory,
37    #[group(
38        title = "Store Immediate Operations",
39        description = "Store an immediate value to memory.",
40        decode = crate::decode::decode_store_immediate,
41        validate = crate::validate::validate_store_immediate,
42        execute = crate::execute::execute_store_immediate,
43    )]
44    StoreImmediate,
45    #[group(
46        title = "Store Register Operations",
47        description = "Store a register value to memory.",
48        decode = crate::decode::decode_store_register,
49        validate = crate::validate::validate_store_register,
50        execute = crate::execute::execute_store_register,
51    )]
52    StoreRegister,
53    #[group(
54        title = "Binary Immediate Operations",
55        description = "Perform a binary ALU operation using an immediate operand.",
56        decode = crate::decode::decode_binary_immediate,
57        validate = crate::validate::validate_binary_immediate,
58        execute = crate::execute::execute_binary_immediate,
59    )]
60    BinaryImmediate,
61    #[group(
62        title = "Binary Register Operations",
63        description = "Perform a binary ALU operation using a register operand.",
64        decode = crate::decode::decode_binary_register,
65        validate = crate::validate::validate_binary_register,
66        execute = crate::execute::execute_binary_register,
67    )]
68    BinaryRegister,
69    #[group(
70        title = "Unary Operations",
71        description = "Perform a unary ALU operation on a register operand.",
72        decode = crate::decode::decode_unary,
73        validate = crate::validate::validate_unary,
74        execute = crate::execute::execute_unary,
75    )]
76    Unary,
77    #[group(
78        title = "Endian Operations",
79        description = "Perform byte-order conversion on a register value.",
80        decode = crate::decode::decode_endian,
81        validate = crate::validate::validate_endian,
82        execute = crate::execute::execute_endian,
83    )]
84    Endian,
85    #[group(
86        title = "Jump Operation",
87        description = "Perform an unconditional jump to a target address.",
88        decode = crate::decode::decode_jump,
89        validate = crate::validate::validate_jump,
90        execute = crate::execute::execute_jump,
91    )]
92    Jump,
93    #[group(
94        title = "Jump Immediate Operations",
95        description = "Perform a conditional jump by comparing a register with an immediate.",
96        decode = crate::decode::decode_jump_immediate,
97        validate = crate::validate::validate_jump_immediate,
98        execute = crate::execute::execute_jump_immediate,
99    )]
100    JumpImmediate,
101    #[group(
102        title = "Jump Register Operations",
103        description = "Perform a conditional jump by comparing two registers.",
104        decode = crate::decode::decode_jump_register,
105        validate = crate::validate::validate_jump_register,
106        execute = crate::execute::execute_jump_register,
107    )]
108    JumpRegister,
109    #[group(
110        title = "Jump32 Immediate Operations",
111        description = "Perform a 32-bit conditional jump by comparing a register with an immediate.",
112        decode = crate::decode::decode_jump32_immediate,
113        validate = crate::validate::validate_jump_immediate,
114        execute = crate::execute::execute_jump_immediate,
115    )]
116    Jump32Immediate,
117    #[group(
118        title = "Jump32 Register Operations",
119        description = "Perform a 32-bit conditional jump by comparing two registers.",
120        decode = crate::decode::decode_jump32_register,
121        validate = crate::validate::validate_jump_register,
122        execute = crate::execute::execute_jump_register,
123    )]
124    Jump32Register,
125    #[group(
126        title = "Call Immediate Operation",
127        description = "Call a function using an immediate target address.",
128        decode = crate::decode::decode_call_immediate,
129        validate = crate::validate::validate_call_immediate,
130        execute = crate::execute::execute_call_immediate,
131    )]
132    CallImmediate,
133    #[group(
134        title = "Call Register Operation",
135        description = "Call a function using a target address stored in a register.",
136        decode = crate::decode::decode_call_register,
137        validate = crate::validate::validate_call_register,
138        execute = crate::execute::execute_call_register,
139    )]
140    CallRegister,
141    #[group(
142        title = "Exit Operation",
143        description = "Return from function or exit program.",
144        decode = crate::decode::decode_exit,
145        validate = crate::validate::validate_exit,
146        execute = crate::execute::execute_exit,
147    )]
148    Exit,
149}