Skip to content

Commit bafeccc

Browse files
committed
fix(instruction) Invalid enum matches become default.
1 parent 30a22cd commit bafeccc

File tree

6 files changed

+198
-15
lines changed

6 files changed

+198
-15
lines changed

src/instruction.rs

+27-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,33 @@ bitfield! {
1313
struct Format(pub u32): Debug, FromRaw, IntoRaw { operation: u8 @ 0..=4 }
1414
}
1515

16-
#[derive(Debug, Clone, Copy, PartialEq, ConvRaw)]
17-
pub enum Scale { X8, X16, X32, X64 }
16+
#[derive(Debug, Clone, Copy, PartialEq, Default)]
17+
#[repr(u8)]
18+
pub enum Scale {
19+
#[default]
20+
X8,
21+
X16,
22+
X32,
23+
X64
24+
}
25+
26+
impl From<u8> for Scale {
27+
fn from(code: u8) -> Self {
28+
match code {
29+
0 => Self::X8,
30+
1 => Self::X16,
31+
2 => Self::X32,
32+
3 => Self::X64,
33+
_ => Self::default()
34+
}
35+
}
36+
}
37+
38+
impl From<Scale> for u8 {
39+
fn from(scale: Scale) -> Self {
40+
scale as Self
41+
}
42+
}
1843

1944
#[derive(Debug, Clone, Copy, PartialEq)]
2045
#[repr(u8)]

src/instruction/binary.rs

+62-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use proc_bitfield::{bitfield, ConvRaw};
1+
use proc_bitfield::{bitfield};
22

3-
#[derive(Debug, Clone, Copy, PartialEq, Default, ConvRaw)]
3+
#[derive(Debug, Clone, Copy, PartialEq, Default)]
44
#[repr(u8)]
55
pub enum Mode {
66
#[default]
@@ -18,6 +18,32 @@ pub enum Mode {
1818
ShiftRight
1919
}
2020

21+
impl From<u8> for Mode {
22+
fn from(code: u8) -> Self {
23+
match code {
24+
0 => Self::And,
25+
1 => Self::Nand,
26+
2 => Self::Or,
27+
3 => Self::Nor,
28+
4 => Self::Xor,
29+
5 => Self::XNor,
30+
6 => Self::Add,
31+
7 => Self::Subtract,
32+
8 => Self::Multiply,
33+
9 => Self::Divide,
34+
10 => Self::ShiftLeft,
35+
11 => Self::ShiftRight,
36+
_ => Self::default()
37+
}
38+
}
39+
}
40+
41+
impl From<Mode> for u8 {
42+
fn from(mode: Mode) -> Self {
43+
mode as Self
44+
}
45+
}
46+
2147
bitfield! {
2248
#[derive(Clone, Copy, PartialEq, Eq)]
2349
pub struct Operation(pub u32): Debug, FromRaw, IntoRaw {
@@ -33,13 +59,29 @@ bitfield! {
3359
}
3460
}
3561

36-
#[derive(Debug, Clone, Copy, PartialEq, ConvRaw)]
62+
#[derive(Debug, Clone, Copy, PartialEq, Default)]
3763
#[repr(u8)]
3864
pub enum RegroupingBinaryMode {
65+
#[default]
3966
Add,
4067
Subtract
4168
}
4269

70+
impl From<bool> for RegroupingBinaryMode {
71+
fn from(flag: bool) -> Self {
72+
match flag {
73+
false => Self::Add,
74+
true => Self::Subtract,
75+
}
76+
}
77+
}
78+
79+
impl From<RegroupingBinaryMode> for bool {
80+
fn from(mode: RegroupingBinaryMode) -> Self {
81+
mode as u8 != 0
82+
}
83+
}
84+
4385
bitfield! {
4486
#[derive(Clone, Copy, PartialEq, Eq)]
4587
pub struct RegroupingBinaryOperation(pub u32): Debug, FromRaw, IntoRaw {
@@ -51,13 +93,29 @@ bitfield! {
5193
}
5294
}
5395

54-
#[derive(Debug, Clone, Copy, PartialEq, ConvRaw)]
96+
#[derive(Debug, Clone, Copy, PartialEq, Default)]
5597
#[repr(u8)]
5698
pub enum RegroupingQuaternaryMode {
99+
#[default]
57100
Multiply,
58101
Divide
59102
}
60103

104+
impl From<bool> for RegroupingQuaternaryMode {
105+
fn from(flag: bool) -> Self {
106+
match flag {
107+
false => Self::Multiply,
108+
true => Self::Divide,
109+
}
110+
}
111+
}
112+
113+
impl From<RegroupingQuaternaryMode> for bool {
114+
fn from(mode: RegroupingQuaternaryMode) -> Self {
115+
mode as u8 != 0
116+
}
117+
}
118+
61119
bitfield! {
62120
#[derive(Clone, Copy, PartialEq, Eq)]
63121
pub struct RegroupingQuaternaryOperation(pub u32): Debug, FromRaw, IntoRaw {

src/instruction/memory.rs

+39-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use proc_bitfield::{bitfield, ConvRaw};
1+
use proc_bitfield::{bitfield};
22
use crate::instruction::Scale;
33

44
bitfield! {
@@ -51,7 +51,7 @@ bitfield! {
5151
}
5252
}
5353

54-
#[derive(Debug, Clone, Copy, PartialEq, Default, ConvRaw)]
54+
#[derive(Debug, Clone, Copy, PartialEq, Default)]
5555
#[repr(u8)]
5656
pub enum BranchMode {
5757
#[default]
@@ -60,7 +60,24 @@ pub enum BranchMode {
6060
Demote
6161
}
6262

63-
#[derive(Debug, Clone, Copy, PartialEq, Default, ConvRaw)]
63+
impl From<u8> for BranchMode {
64+
fn from(code: u8) -> Self {
65+
match code {
66+
0 => Self::Basic,
67+
1 => Self::CallStack,
68+
2 => Self::Demote,
69+
_ => Self::default()
70+
}
71+
}
72+
}
73+
74+
impl From<BranchMode> for u8 {
75+
fn from(mode: BranchMode) -> Self {
76+
mode as Self
77+
}
78+
}
79+
80+
#[derive(Debug, Clone, Copy, PartialEq, Default)]
6481
#[repr(u8)]
6582
pub enum BranchCondition {
6683
#[default]
@@ -71,6 +88,25 @@ pub enum BranchCondition {
7188
Overflow
7289
}
7390

91+
impl From<u8> for BranchCondition {
92+
fn from(code: u8) -> Self {
93+
match code {
94+
0 => Self::Zero,
95+
1 => Self::Negative,
96+
2 => Self::Parity,
97+
3 => Self::Regrouping,
98+
4 => Self::Overflow,
99+
_ => Self::default(), // Default case for invalid values
100+
}
101+
}
102+
}
103+
104+
impl From<BranchCondition> for u8 {
105+
fn from(condition: BranchCondition) -> Self {
106+
condition as Self
107+
}
108+
}
109+
74110
#[derive(Debug, Clone, Copy, PartialEq, Default)]
75111
pub struct BranchHint(pub Option<bool>);
76112

src/instruction/mnemonic.rs

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use proc_bitfield::{bitfield, ConvRaw};
1+
use proc_bitfield::{bitfield};
22

33
bitfield! {
44
#[derive(Clone, Copy, PartialEq, Eq)]
55
pub struct Format(pub u32): Debug, FromRaw, IntoRaw { pub operation: u8 [unsafe! Operation] @ 5..=8 }
66
}
77

8-
#[derive(Debug, Clone, Copy, PartialEq, Default, ConvRaw)]
8+
#[derive(Debug, Clone, Copy, PartialEq, Default)]
99
#[repr(u8)]
1010
pub enum Operation {
1111
#[default]
@@ -21,6 +21,30 @@ pub enum Operation {
2121
UnStackRegisters
2222
}
2323

24+
impl From<u8> for Operation {
25+
fn from(code: u8) -> Self {
26+
match code {
27+
0 => Self::Wait,
28+
1 => Self::Interrupt,
29+
2 => Self::End,
30+
3 => Self::EndInterrupt,
31+
4 => Self::Unlock,
32+
5 => Self::Yield,
33+
6 => Self::UnYield,
34+
7 => Self::ClearRegisters,
35+
8 => Self::StackRegisters,
36+
9 => Self::UnStackRegisters,
37+
_ => Self::default()
38+
}
39+
}
40+
}
41+
42+
impl From<Operation> for u8 {
43+
fn from(operation: Operation) -> Self {
44+
operation as Self
45+
}
46+
}
47+
2448
impl Operation {
2549
pub const TABLE: [Operation; 10] = [
2650
Operation::Wait,

src/instruction/register.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use proc_bitfield::{bitfield, ConvRaw};
1+
use proc_bitfield::{bitfield};
22

3-
#[derive(Debug, Clone, Copy, PartialEq, Default, ConvRaw)]
3+
#[derive(Debug, Clone, Copy, PartialEq, Default)]
44
#[repr(u8)]
55
pub enum File {
66
#[default]
@@ -10,6 +10,24 @@ pub enum File {
1010
Processor
1111
}
1212

13+
impl From<u8> for File {
14+
fn from(code: u8) -> Self {
15+
match code {
16+
0 => Self::General,
17+
1 => Self::Vector,
18+
2 => Self::Interrupt,
19+
3 => Self::Processor,
20+
_ => Self::default()
21+
}
22+
}
23+
}
24+
25+
impl From<File> for u8 {
26+
fn from(file: File) -> Self {
27+
file as Self
28+
}
29+
}
30+
1331
bitfield! {
1432
#[derive(Clone, Copy, PartialEq, Eq)]
1533
pub struct LoadImmediateOperation(pub u32): Debug, FromRaw, IntoRaw {

src/instruction/unary.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use proc_bitfield::{bitfield, ConvRaw};
1+
use proc_bitfield::{bitfield};
22
use crate::instruction::Scale;
33

4-
#[derive(Debug, Clone, Copy, PartialEq, Default, ConvRaw)]
4+
#[derive(Debug, Clone, Copy, PartialEq, Default)]
55
#[repr(u8)]
66
pub enum Mode {
77
#[default]
@@ -15,6 +15,28 @@ pub enum Mode {
1515
CountZeros
1616
}
1717

18+
impl From<u8> for Mode {
19+
fn from(code: u8) -> Self {
20+
match code {
21+
0 => Self::Invert,
22+
1 => Self::Increment,
23+
2 => Self::Decrement,
24+
3 => Self::ShiftLeft,
25+
4 => Self::ShiftRight,
26+
5 => Self::TrailingZeros,
27+
6 => Self::LeadingZeros,
28+
7 => Self::CountZeros,
29+
_ => Self::default()
30+
}
31+
}
32+
}
33+
34+
impl From<Mode> for u8 {
35+
fn from(mode: Mode) -> Self {
36+
mode as Self
37+
}
38+
}
39+
1840
bitfield! {
1941
#[derive(Clone, Copy, PartialEq, Eq)]
2042
pub struct Operation(pub u32): Debug, FromRaw, IntoRaw {

0 commit comments

Comments
 (0)