Skip to content

Commit 09e42b4

Browse files
committed
feat(unary) Added unary operations encoding/decoding.
1 parent 665ded9 commit 09e42b4

File tree

4 files changed

+141
-22
lines changed

4 files changed

+141
-22
lines changed

examples/counter.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
extern crate arrseq_lightning;
22

33
use arrseq_lightning::instruction::Instruction;
4+
use arrseq_lightning::instruction::memory::LockOperation;
45

56
fn main() {
67
let ins = Instruction::decode(0b00000000_00000000_00000000_00000100);
78
dbg!(ins);
9+
10+
let i = Instruction::Lock(LockOperation::from(0)
11+
.with_relative(false)
12+
.with_base(10));
13+
14+
dbg!(i.encode());
815
}

src/instruction.rs

+61-21
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use crate::num::MaskedU8;
44
pub mod load_immediate;
55
pub mod memory;
66
pub mod mnemonic;
7-
pub mod vector;
7+
pub mod register;
8+
pub mod unary;
89

910
bitfield! {
1011
/// A bitfield showcasing how to specify bit ranges.
@@ -21,8 +22,8 @@ pub enum Instruction {
2122
Mnemonic(mnemonic::Operation),
2223

2324
LoadImmediate(load_immediate::Operation),
24-
BuildVector(vector::BuildVectorOperation),
25-
UnBuildVector(vector::UnBuildVectorOperation),
25+
BuildVector(register::BuildVectorOperation),
26+
UnBuildVector(register::UnBuildVectorOperation),
2627

2728
ReadMemory(memory::ReadOperation),
2829
WriteMemory(memory::WriteOperation),
@@ -31,45 +32,84 @@ pub enum Instruction {
3132
Lock(memory::LockOperation),
3233
Branch(memory::BranchOperation),
3334

34-
Copy,
35+
Copy(register::CopyOperation),
36+
37+
Unary(unary::Operation),
38+
Negate(unary::NegateOperation),
39+
ExtendSign(unary::ExtendSignOperation),
3540

36-
Unary,
37-
Negate,
38-
SignExtend,
3941
Binary,
4042
RegroupingBinary,
43+
4144
RegroupingQuaternary
4245
}
4346

4447
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;
5565

5666
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 {
5969
Self::MNEMONIC_CODE => Self::Mnemonic(mnemonic::Format::from(encoded).operation()),
6070
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)),
6373
Self::READ_MEMORY_CODE => Self::ReadMemory(memory::ReadOperation::from(encoded)),
6474
Self::WRITE_MEMORY_CODE => Self::WriteMemory(memory::WriteOperation::from(encoded)),
6575
Self::STACK_CODE => Self::Stack(memory::StackOperation::from(encoded)),
6676
Self::UNSTACK_CODE => Self::UnStack(memory::UnStackOperation::from(encoded)),
6777
Self::LOCK_CODE => Self::Lock(memory::LockOperation::from(encoded)),
6878
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)),
6983

7084
_ => unimplemented!()
7185
}
7286
}
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+
}
73113
}
74114

75115
impl Default for Instruction {

src/instruction/vector.rs src/instruction/register.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
use proc_bitfield::bitfield;
1+
use proc_bitfield::{bitfield, ConvRaw};
2+
3+
#[derive(Debug, Clone, Copy, PartialEq, Default, ConvRaw)]
4+
#[repr(u8)]
5+
pub enum File {
6+
#[default]
7+
General,
8+
Vector,
9+
Interrupt,
10+
Processor
11+
}
212

313
bitfield! {
414
#[derive(Clone, Copy, PartialEq, Eq)]
@@ -20,4 +30,14 @@ bitfield! {
2030
pub destination_3: u8 @ 22..=26,
2131
pub source: u8 @ 27..=31
2232
}
33+
}
34+
35+
bitfield! {
36+
#[derive(Clone, Copy, PartialEq, Eq)]
37+
pub struct CopyOperation(pub u32): Debug, FromRaw, IntoRaw {
38+
pub source_file: u8 [unsafe! File] @ 5..=6,
39+
pub destination_file: u8 [unsafe! File] @ 7..=8,
40+
pub source: u8 @ 22..=26,
41+
pub destination: u8 @ 27..=31
42+
}
2343
}

src/instruction/unary.rs

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use proc_bitfield::{bitfield, ConvRaw};
2+
use crate::instruction::Scale;
3+
4+
#[derive(Debug, Clone, Copy, PartialEq, Default, ConvRaw)]
5+
#[repr(u8)]
6+
pub enum Mode {
7+
#[default]
8+
Invert,
9+
Increment,
10+
Decrement,
11+
ShiftLeft,
12+
ShiftRight,
13+
TrailingZeros,
14+
LeadingZeros,
15+
CountZeros
16+
}
17+
18+
bitfield! {
19+
#[derive(Clone, Copy, PartialEq, Eq)]
20+
pub struct Operation(pub u32): Debug, FromRaw, IntoRaw {
21+
pub vector: bool @ 5,
22+
pub atomic: bool @ 6,
23+
pub mode: u8 [unsafe! Mode] @ 7..=9,
24+
pub source_offset: u8 @ 10..=15,
25+
pub destination_offset: u8 @ 16..=21,
26+
pub source: u8 @ 21..=26,
27+
pub destination: u8 @ 27..=31
28+
}
29+
}
30+
31+
bitfield! {
32+
#[derive(Clone, Copy, PartialEq, Eq)]
33+
pub struct NegateOperation(pub u32): Debug, FromRaw, IntoRaw {
34+
pub vector: bool @ 5,
35+
pub atomic: bool @ 6,
36+
pub scale: u8 [unsafe! Scale] @ 7..=8,
37+
pub source: u8 @ 21..=26,
38+
pub destination: u8 @ 27..=31
39+
}
40+
}
41+
42+
bitfield! {
43+
#[derive(Clone, Copy, PartialEq, Eq)]
44+
pub struct ExtendSignOperation(pub u32): Debug, FromRaw, IntoRaw {
45+
pub vector: bool @ 5,
46+
pub atomic: bool @ 6,
47+
pub source_scale: u8 [unsafe! Scale] @ 7..=8,
48+
pub destination_scale: u8 [unsafe! Scale] @ 9..=10,
49+
pub source: u8 @ 21..=26,
50+
pub destination: u8 @ 27..=31
51+
}
52+
}

0 commit comments

Comments
 (0)