@@ -4,7 +4,8 @@ use crate::num::MaskedU8;
4
4
pub mod load_immediate;
5
5
pub mod memory;
6
6
pub mod mnemonic;
7
- pub mod vector;
7
+ pub mod register;
8
+ pub mod unary;
8
9
9
10
bitfield ! {
10
11
/// A bitfield showcasing how to specify bit ranges.
@@ -21,8 +22,8 @@ pub enum Instruction {
21
22
Mnemonic ( mnemonic:: Operation ) ,
22
23
23
24
LoadImmediate ( load_immediate:: Operation ) ,
24
- BuildVector ( vector :: BuildVectorOperation ) ,
25
- UnBuildVector ( vector :: UnBuildVectorOperation ) ,
25
+ BuildVector ( register :: BuildVectorOperation ) ,
26
+ UnBuildVector ( register :: UnBuildVectorOperation ) ,
26
27
27
28
ReadMemory ( memory:: ReadOperation ) ,
28
29
WriteMemory ( memory:: WriteOperation ) ,
@@ -31,45 +32,84 @@ pub enum Instruction {
31
32
Lock ( memory:: LockOperation ) ,
32
33
Branch ( memory:: BranchOperation ) ,
33
34
34
- Copy ,
35
+ Copy ( register:: CopyOperation ) ,
36
+
37
+ Unary ( unary:: Operation ) ,
38
+ Negate ( unary:: NegateOperation ) ,
39
+ ExtendSign ( unary:: ExtendSignOperation ) ,
35
40
36
- Unary ,
37
- Negate ,
38
- SignExtend ,
39
41
Binary ,
40
42
RegroupingBinary ,
43
+
41
44
RegroupingQuaternary
42
45
}
43
46
44
47
impl Instruction {
45
- pub const MNEMONIC_CODE : u8 = 0 ;
46
- pub const LOAD_IMMEDIATE_CODE : u8 = 1 ;
47
- pub const BUILD_VECTOR_CODE : u8 = 2 ;
48
- pub const UNBUILD_VECTOR_CODE : u8 = 3 ;
49
- pub const READ_MEMORY_CODE : u8 = 4 ;
50
- pub const WRITE_MEMORY_CODE : u8 = 5 ;
51
- pub const STACK_CODE : u8 = 6 ;
52
- pub const UNSTACK_CODE : u8 = 7 ;
53
- pub const LOCK_CODE : u8 = 8 ;
54
- pub const BRANCH_CODE : u8 = 9 ;
48
+ pub const MNEMONIC_CODE : u8 = 0 ;
49
+ pub const LOAD_IMMEDIATE_CODE : u8 = 1 ;
50
+ pub const BUILD_VECTOR_CODE : u8 = 2 ;
51
+ pub const UNBUILD_VECTOR_CODE : u8 = 3 ;
52
+ pub const READ_MEMORY_CODE : u8 = 4 ;
53
+ pub const WRITE_MEMORY_CODE : u8 = 5 ;
54
+ pub const STACK_CODE : u8 = 6 ;
55
+ pub const UNSTACK_CODE : u8 = 7 ;
56
+ pub const LOCK_CODE : u8 = 8 ;
57
+ pub const BRANCH_CODE : u8 = 9 ;
58
+ pub const COPY_CODE : u8 = 10 ;
59
+ pub const UNARY_CODE : u8 = 11 ;
60
+ pub const NEGATE_CODE : u8 = 12 ;
61
+ pub const EXTEND_SIGN_CODE : u8 = 13 ;
62
+ pub const BINARY_CODE : u8 = 14 ;
63
+ pub const REGROUP_BINARY_CODE : u8 = 15 ;
64
+ pub const REGROUP_QUATERNARY_CODE : u8 = 16 ;
55
65
56
66
pub fn decode ( encoded : u32 ) -> Self {
57
- let operation = Format :: from ( encoded) ;
58
- match operation. operation ( ) {
67
+ let operation = Format :: from ( encoded) . operation ( ) ;
68
+ match operation {
59
69
Self :: MNEMONIC_CODE => Self :: Mnemonic ( mnemonic:: Format :: from ( encoded) . operation ( ) ) ,
60
70
Self :: LOAD_IMMEDIATE_CODE => Self :: LoadImmediate ( load_immediate:: Operation :: from ( encoded) ) ,
61
- Self :: BUILD_VECTOR_CODE => Self :: BuildVector ( vector :: BuildVectorOperation :: from ( encoded) ) ,
62
- Self :: UNBUILD_VECTOR_CODE => Self :: UnBuildVector ( vector :: UnBuildVectorOperation :: from ( encoded) ) ,
71
+ Self :: BUILD_VECTOR_CODE => Self :: BuildVector ( register :: BuildVectorOperation :: from ( encoded) ) ,
72
+ Self :: UNBUILD_VECTOR_CODE => Self :: UnBuildVector ( register :: UnBuildVectorOperation :: from ( encoded) ) ,
63
73
Self :: READ_MEMORY_CODE => Self :: ReadMemory ( memory:: ReadOperation :: from ( encoded) ) ,
64
74
Self :: WRITE_MEMORY_CODE => Self :: WriteMemory ( memory:: WriteOperation :: from ( encoded) ) ,
65
75
Self :: STACK_CODE => Self :: Stack ( memory:: StackOperation :: from ( encoded) ) ,
66
76
Self :: UNSTACK_CODE => Self :: UnStack ( memory:: UnStackOperation :: from ( encoded) ) ,
67
77
Self :: LOCK_CODE => Self :: Lock ( memory:: LockOperation :: from ( encoded) ) ,
68
78
Self :: BRANCH_CODE => Self :: Branch ( memory:: BranchOperation :: from ( encoded) ) ,
79
+ Self :: COPY_CODE => Self :: Copy ( register:: CopyOperation :: from ( encoded) ) ,
80
+ Self :: UNARY_CODE => Self :: Unary ( unary:: Operation :: from ( encoded) ) ,
81
+ Self :: NEGATE_CODE => Self :: Negate ( unary:: NegateOperation :: from ( encoded) ) ,
82
+ Self :: EXTEND_SIGN_CODE => Self :: ExtendSign ( unary:: ExtendSignOperation :: from ( encoded) ) ,
69
83
70
84
_ => unimplemented ! ( )
71
85
}
72
86
}
87
+
88
+ pub fn encode ( self ) -> u32 {
89
+ let ( opcode, operands) = match self {
90
+ Instruction :: Mnemonic ( op) => ( Self :: MNEMONIC_CODE , u32:: from ( op) ) ,
91
+ Instruction :: LoadImmediate ( op) => ( Self :: LOAD_IMMEDIATE_CODE , u32:: from ( op) ) ,
92
+ Instruction :: BuildVector ( op) => ( Self :: BUILD_VECTOR_CODE , u32:: from ( op) ) ,
93
+ Instruction :: UnBuildVector ( op) => ( Self :: UNBUILD_VECTOR_CODE , u32:: from ( op) ) ,
94
+ Instruction :: ReadMemory ( op) => ( Self :: READ_MEMORY_CODE , u32:: from ( op) ) ,
95
+ Instruction :: WriteMemory ( op) => ( Self :: WRITE_MEMORY_CODE , u32:: from ( op) ) ,
96
+ Instruction :: Stack ( op) => ( Self :: STACK_CODE , u32:: from ( op) ) ,
97
+ Instruction :: UnStack ( op) => ( Self :: UNSTACK_CODE , u32:: from ( op) ) ,
98
+ Instruction :: Lock ( op) => ( Self :: LOCK_CODE , u32:: from ( op) ) ,
99
+ Instruction :: Branch ( op) => ( Self :: BRANCH_CODE , u32:: from ( op) ) ,
100
+ Instruction :: Copy ( op) => ( Self :: COPY_CODE , u32:: from ( op) ) ,
101
+ Instruction :: Unary ( op) => ( Self :: UNARY_CODE , u32:: from ( op) ) ,
102
+ Instruction :: Negate ( op) => ( Self :: NEGATE_CODE , u32:: from ( op) ) ,
103
+ Instruction :: ExtendSign ( op) => ( Self :: EXTEND_SIGN_CODE , u32:: from ( op) ) ,
104
+ // Instruction::Binary(op) => (Self::BINARY_CODE, u32::from(op)),
105
+ // Instruction::RegroupingBinary(op) => (Self::REGROUPING_BINARY_CODE, u32::from(op)),
106
+ // Instruction::RegroupingQuaternary(op) => (Self::REGROUPING_QUATERNARY_CODE, u32::from(op)),
107
+ _ => todo ! ( )
108
+ } ;
109
+
110
+ let opcode = opcode as u32 ;
111
+ opcode | operands
112
+ }
73
113
}
74
114
75
115
impl Default for Instruction {
0 commit comments