refactor(ir): use u64 directly instead of spans for instructions

This commit is contained in:
multisn8 2024-01-19 14:26:20 +01:00
parent 87343f0a38
commit cdf42417da
Signed by untrusted user: multisamplednight
GPG key ID: 6D525AA147CBDAE2
3 changed files with 10 additions and 46 deletions

View file

@ -6,10 +6,8 @@
//! //!
//! Instead, this module offers an alternative way to refer to specific instances: //! 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, //! - [`Instruction`]s are effectively just a number floating in space,
//! so effectively where they are written in the source code. //! incremented each time a new instruction is referred to.
//! (Or if coming from [`crate::semi_human::GraphIr`],
//! it's just a fictional number floating in space.)
//! - [`Socket`]s contain //! - [`Socket`]s contain
//! - what [`Instruction`] they belong to //! - what [`Instruction`] they belong to
//! - which index they occupy on it //! - which index they occupy on it
@ -19,22 +17,12 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::Span; /// One specific instruction.
/// One specific instruction, and where it is found in code.
/// ///
/// It does **not** contain what kind of instruction this is. /// It does **not** contain what kind of instruction this is.
/// Refer to [`crate::instruction::Kind`] for this instead. /// Refer to [`crate::instruction::Kind`] for this instead.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Instruction(pub(super) Span); pub struct Instruction(pub(super) u64);
impl Instruction {
/// Where this instruction is written down.
#[must_use]
pub fn span(&self) -> &Span {
&self.0
}
}
/// On an **instruction**, accepts incoming data. /// On an **instruction**, accepts incoming data.
/// ///

View file

@ -1,4 +1,4 @@
use std::{num::NonZeroUsize, ops::RangeInclusive}; use std::num::NonZeroUsize;
use instruction::SocketCount; use instruction::SocketCount;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -268,26 +268,6 @@ impl<'ir> Instruction<'ir> {
} }
} }
/// Some part referred to in source code.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Span {
// would love to use an actual [`std::ops::RangeInclusive`], but those don't implement
// `PartialOrd` and `Ord` unfortunately
/// At which byte this span starts, inclusively.
pub from: usize,
/// At which byte this span ends, inclusively.
pub to: usize,
}
impl From<RangeInclusive<usize>> for Span {
fn from(range: RangeInclusive<usize>) -> Self {
Self {
from: *range.start(),
to: *range.end(),
}
}
}
/// Constructs an [`id::Socket`] a bit more tersely. /// Constructs an [`id::Socket`] a bit more tersely.
fn socket(id: &id::Instruction, idx: u16) -> id::Socket { fn socket(id: &id::Instruction, idx: u16) -> id::Socket {
id::Socket { id::Socket {

View file

@ -10,7 +10,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{id, instruction, Map, Set, Span}; use crate::{id, instruction, Map, Set};
/// Semi-human-{read,writ}able [`crate::GraphIr`] with far less useful types. /// Semi-human-{read,writ}able [`crate::GraphIr`] with far less useful types.
/// ///
@ -19,8 +19,7 @@ use crate::{id, instruction, Map, Set, Span};
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] #[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct GraphIr { pub struct GraphIr {
/// See [`crate::GraphIr::instructions`], just that a simple number is used for the ID instead /// See [`crate::GraphIr::instructions`], just that a simple number is used for the ID instead
/// of a proper span. pub(crate) instructions: Map<u64, instruction::Kind>,
pub(crate) instructions: Map<usize, instruction::Kind>,
/// See [`crate::GraphIr::edges`], the forward edges. /// See [`crate::GraphIr::edges`], the forward edges.
/// RON wants you to type the set as if it were a list. /// RON wants you to type the set as if it were a list.
pub(crate) edges: Map<Socket, Set<Socket>>, pub(crate) edges: Map<Socket, Set<Socket>>,
@ -29,17 +28,14 @@ pub struct GraphIr {
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
pub struct Socket { pub struct Socket {
/// ID of the instruction this socket is on. /// ID of the instruction this socket is on.
pub(crate) on: usize, pub(crate) on: u64,
pub(crate) idx: u16, pub(crate) idx: u16,
} }
impl From<Socket> for id::Socket { impl From<Socket> for id::Socket {
fn from(source: Socket) -> Self { fn from(source: Socket) -> Self {
Self { Self {
belongs_to: (id::Instruction(Span { belongs_to: id::Instruction(source.on),
from: source.on,
to: source.on,
})),
idx: id::SocketIdx(source.idx), idx: id::SocketIdx(source.idx),
} }
} }
@ -52,7 +48,7 @@ impl From<GraphIr> for crate::GraphIr {
instructions: source instructions: source
.instructions .instructions
.into_iter() .into_iter()
.map(|(id, kind)| (id::Instruction(Span { from: id, to: id }), kind)) .map(|(id, kind)| (id::Instruction(id), kind))
.collect(), .collect(),
edges: type_edges(source.edges), edges: type_edges(source.edges),
rev_edges: reverse_and_type_edges(edges), rev_edges: reverse_and_type_edges(edges),