chore: extract id stuff into its own file
This commit is contained in:
parent
ca8cf0cc52
commit
6395efbeab
2 changed files with 62 additions and 45 deletions
61
crates/ir/src/id.rs
Normal file
61
crates/ir/src/id.rs
Normal file
|
@ -0,0 +1,61 @@
|
|||
//! Instance identification for instructions and their glue.
|
||||
//!
|
||||
//! Instructions as defined in [`crate::instruction::Kind`] and descendants are very useful,
|
||||
//! but they cannot be directly used as vertices in the graph IR,
|
||||
//! as there may easily be multiple instructions of the same kind in the same program.
|
||||
//!
|
||||
//! Instead, this module offers an alternative way to refer to specific instances:
|
||||
//!
|
||||
//! - [`Instruction`]s are referred to as their **byte [`Span`]s** in the source code,
|
||||
//! so effectively where they are written in the source code.
|
||||
//! (Or if coming from [`crate::semi_human::GraphIr`],
|
||||
//! it's just a fictional number floating in space.)
|
||||
//! - [`Socket`]s contain
|
||||
//! - what [`Instruction`] they belong to
|
||||
//! - which index they occupy on it
|
||||
//!
|
||||
//! The distinction between [`Input`] and [`Output`] is implemented
|
||||
//! as them being different types.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::Span;
|
||||
|
||||
/// One specific instruction, and where it is found in code.
|
||||
///
|
||||
/// It does **not** contain what kind of instruction this is.
|
||||
/// Refer to [`crate::instruction::Kind`] for this instead.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
|
||||
pub struct Instruction(pub(super) Span);
|
||||
|
||||
impl Instruction {
|
||||
/// Where this instruction is written down.
|
||||
#[must_use]
|
||||
pub fn span(&self) -> &Span {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
/// On an **instruction**, accepts incoming data.
|
||||
///
|
||||
/// An **instruction** cannot run if any of these are not connected.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
|
||||
pub struct Input(pub(super) Socket);
|
||||
|
||||
/// On an **instruction**, returns outgoing data to be fed to [`Input`]s.
|
||||
///
|
||||
/// In contrast to [`Input`]s, [`Output`]s may be used or unused.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
|
||||
pub struct Output(pub(super) Socket);
|
||||
|
||||
/// An unspecified socket on a specific **instruction**,
|
||||
/// and where it is on that **instruction**.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
|
||||
pub struct Socket {
|
||||
pub belongs_to: Instruction,
|
||||
pub idx: SocketIdx,
|
||||
}
|
||||
|
||||
/// Where a [`Socket`] is on an **instruction**.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
|
||||
pub struct SocketIdx(pub u16);
|
|
@ -2,6 +2,7 @@ use std::ops::RangeInclusive;
|
|||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub mod id;
|
||||
pub mod instruction;
|
||||
|
||||
// feel free to make this public if there's sufficient reason to do so
|
||||
|
@ -70,51 +71,6 @@ pub struct GraphIr {
|
|||
rev_edges: Map<id::Input, Set<id::Output>>,
|
||||
}
|
||||
|
||||
pub mod id {
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::Span;
|
||||
|
||||
/// One specific instruction, and where it is found in code.
|
||||
///
|
||||
/// It does **not** contain what kind of instruction this is.
|
||||
/// Refer to [`crate::instruction::Kind`] for this instead.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
|
||||
pub struct Instruction(pub(super) Span);
|
||||
|
||||
impl Instruction {
|
||||
/// Where this instruction is written down.
|
||||
#[must_use]
|
||||
pub fn span(&self) -> &Span {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
/// On an **instruction**, accepts incoming data.
|
||||
///
|
||||
/// An **instruction** cannot run if any of these are not connected.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
|
||||
pub struct Input(pub(super) Socket);
|
||||
|
||||
/// On an **instruction**, returns outgoing data to be fed to [`Input`]s.
|
||||
///
|
||||
/// In contrast to [`Input`]s, [`Output`]s may be used or unused.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
|
||||
pub struct Output(pub(super) Socket);
|
||||
|
||||
/// An unspecified socket on a specific **instruction**,
|
||||
/// and where it is on that **instruction**.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
|
||||
pub struct Socket {
|
||||
pub belongs_to: Instruction,
|
||||
pub idx: SocketIdx,
|
||||
}
|
||||
|
||||
/// Where a [`Socket`] is on an **instruction**.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
|
||||
pub struct SocketIdx(pub u16);
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
|
||||
pub struct Span {
|
||||
range: RangeInclusive<usize>,
|
||||
|
|
Loading…
Reference in a new issue