Skip to content

Commit

Permalink
remove Id
Browse files Browse the repository at this point in the history
  • Loading branch information
vincenthz committed Dec 17, 2023
1 parent 4d70d5d commit 5a3ea50
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 110 deletions.
44 changes: 30 additions & 14 deletions werbolg-compile/src/code.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,65 @@
use super::instructions::Instruction;
use super::symbols::{IdVec, IdVecAfter};
use werbolg_core::id::{Id, IdRemapper};
use werbolg_core::id::IdF;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct InstructionAddress(Id);
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct InstructionAddress(u32);

impl Default for InstructionAddress {
fn default() -> Self {
Self(Id::from_collection_len(0))
Self::from_collection_len(0)
}
}

impl InstructionAddress {
pub fn next(self) -> Self {
Self(Id::add(self.0, 1))
InstructionAddress::add(self, 1)
}
}

impl IdRemapper for InstructionAddress {
fn uncat(self) -> Id {
self.0
impl IdF for InstructionAddress {
fn as_index(self) -> usize {
self.0 as usize
}

fn cat(id: Id) -> Self {
Self(id)
fn from_slice_len<T>(slice: &[T]) -> Self {
Self(slice.len() as u32)
}

fn from_collection_len(len: usize) -> Self {
Self(len as u32)
}

fn remap(left: Self, right: Self) -> Self {
Self(left.0 + right.0)
}

fn add(left: Self, right: u32) -> Self {
Self(left.0.checked_add(right).expect("ID valid add"))
}

fn diff(left: Self, right: Self) -> u32 {
left.0.checked_sub(right.0).expect("ID valid diff")
}
}

impl core::ops::AddAssign<InstructionDiff> for InstructionAddress {
fn add_assign(&mut self, rhs: InstructionDiff) {
self.0 = Id::add(self.0, rhs.0)
*self = InstructionAddress::add(*self, rhs.0)
}
}

impl core::ops::Sub for InstructionAddress {
type Output = InstructionDiff;

fn sub(self, rhs: Self) -> Self::Output {
InstructionDiff(Id::diff(self.0, rhs.0))
InstructionDiff(InstructionAddress::diff(self, rhs))
}
}

impl core::fmt::Display for InstructionAddress {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let idx = self.0.as_index();
let idx = self.as_index();
write!(f, "{:04x}_{:04x}", idx >> 16, idx & 0xffff)
}
}
Expand Down Expand Up @@ -91,7 +107,7 @@ impl Code {
let ofs = self.stmts.next_id();
self.stmts
.concat(&mut IdVecAfter::from_idvec(later.stmts, ofs));
InstructionDiff(ofs.uncat().as_index() as u32)
InstructionDiff(ofs.as_index() as u32)
}

pub fn finalize(self) -> IdVec<InstructionAddress, Instruction> {
Expand Down
56 changes: 28 additions & 28 deletions werbolg-compile/src/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ use alloc::vec::Vec;
use core::hash::Hash;
use core::marker::PhantomData;
use hashbrown::HashMap;
use werbolg_core::id::{Id, IdRemapper};
use werbolg_core::id::IdF;
use werbolg_core::Ident;

pub struct SymbolsTable<ID: IdRemapper> {
pub(crate) tbl: HashMap<Ident, Id>,
pub struct SymbolsTable<ID: IdF> {
pub(crate) tbl: HashMap<Ident, ID>,
phantom: PhantomData<ID>,
}

impl<ID: IdRemapper> SymbolsTable<ID> {
impl<ID: IdF> SymbolsTable<ID> {
pub fn new() -> Self {
Self {
tbl: Default::default(),
Expand All @@ -19,15 +19,15 @@ impl<ID: IdRemapper> SymbolsTable<ID> {
}

pub fn insert(&mut self, ident: Ident, id: ID) {
self.tbl.insert(ident, id.uncat());
self.tbl.insert(ident, id);
}

pub fn get(&self, ident: &Ident) -> Option<ID> {
self.tbl.get(ident).map(|i| ID::cat(*i))
self.tbl.get(ident).map(|i| *i)
}

pub fn iter(&self) -> impl Iterator<Item = (&Ident, ID)> {
self.tbl.iter().map(|(ident, id)| (ident, ID::cat(*id)))
self.tbl.iter().map(|(ident, id)| (ident, *id))
}
}

Expand All @@ -36,21 +36,21 @@ pub struct IdVec<ID, T> {
phantom: PhantomData<ID>,
}

impl<ID: IdRemapper, T> core::ops::Index<ID> for IdVec<ID, T> {
impl<ID: IdF, T> core::ops::Index<ID> for IdVec<ID, T> {
type Output = T;

fn index(&self, index: ID) -> &Self::Output {
&self.vec[index.uncat().as_index()]
&self.vec[index.as_index()]
}
}

impl<ID: IdRemapper, T> core::ops::IndexMut<ID> for IdVec<ID, T> {
impl<ID: IdF, T> core::ops::IndexMut<ID> for IdVec<ID, T> {
fn index_mut(&mut self, index: ID) -> &mut T {
&mut self.vec[index.uncat().as_index()]
&mut self.vec[index.as_index()]
}
}

impl<ID: IdRemapper, T> IdVec<ID, T> {
impl<ID: IdF, T> IdVec<ID, T> {
pub fn new() -> Self {
Self {
vec: Vec::new(),
Expand All @@ -59,7 +59,7 @@ impl<ID: IdRemapper, T> IdVec<ID, T> {
}

pub fn get(&self, id: ID) -> Option<&T> {
let idx = id.uncat().as_index();
let idx = id.as_index();
if self.vec.len() > idx {
Some(&self.vec[idx])
} else {
Expand All @@ -68,13 +68,13 @@ impl<ID: IdRemapper, T> IdVec<ID, T> {
}

pub fn next_id(&self) -> ID {
ID::cat(Id::from_slice_len(&self.vec))
ID::from_slice_len(&self.vec)
}

pub fn push(&mut self, v: T) -> ID {
let id = Id::from_slice_len(&self.vec);
let id = ID::from_slice_len(&self.vec);
self.vec.push(v);
ID::cat(id)
id
}

pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
Expand All @@ -85,18 +85,18 @@ impl<ID: IdRemapper, T> IdVec<ID, T> {
self.vec
.iter()
.enumerate()
.map(|(i, t)| (ID::cat(Id::from_collection_len(i)), t))
.map(|(i, t)| (ID::from_collection_len(i), t))
}

pub fn into_iter(self) -> impl Iterator<Item = (ID, T)> {
self.vec
.into_iter()
.enumerate()
.map(|(i, t)| (ID::cat(Id::from_collection_len(i)), t))
.map(|(i, t)| (ID::from_collection_len(i), t))
}

pub fn concat(&mut self, after: &mut IdVecAfter<ID, T>) {
assert!(self.vec.len() == after.ofs.uncat().as_index());
assert!(self.vec.len() == after.ofs.as_index());
self.vec.append(&mut after.id_vec.vec)
}

Expand All @@ -107,7 +107,7 @@ impl<ID: IdRemapper, T> IdVec<ID, T> {
let mut new = IdVec::<ID, U>::new();
for (id, t) in self.into_iter() {
let new_id = new.push(f(t));
assert_eq!(new_id.uncat(), id.uncat());
assert_eq!(new_id, id);
}
new
}
Expand All @@ -118,7 +118,7 @@ pub struct IdVecAfter<ID, T> {
ofs: ID,
}

impl<ID: IdRemapper, T> IdVecAfter<ID, T> {
impl<ID: IdF, T> IdVecAfter<ID, T> {
pub fn new(first_id: ID) -> Self {
Self {
id_vec: IdVec::new(),
Expand All @@ -134,9 +134,9 @@ impl<ID: IdRemapper, T> IdVecAfter<ID, T> {
}

pub fn push(&mut self, v: T) -> ID {
let id = self.id_vec.push(v).uncat();
let new_id = Id::remap(id, self.ofs.uncat());
ID::cat(new_id)
let id = self.id_vec.push(v);
let new_id = ID::remap(id, self.ofs);
new_id
}

pub fn remap<F>(&mut self, f: F)
Expand All @@ -149,12 +149,12 @@ impl<ID: IdRemapper, T> IdVecAfter<ID, T> {
}
}

pub struct SymbolsTableData<ID: IdRemapper, T> {
pub struct SymbolsTableData<ID: IdF, T> {
pub table: SymbolsTable<ID>,
pub vecdata: IdVec<ID, T>,
}

impl<ID: IdRemapper, T> SymbolsTableData<ID, T> {
impl<ID: IdF, T> SymbolsTableData<ID, T> {
pub fn new() -> Self {
Self {
table: SymbolsTable::new(),
Expand All @@ -176,13 +176,13 @@ impl<ID: IdRemapper, T> SymbolsTableData<ID, T> {
}
}

pub struct UniqueTableBuilder<ID: IdRemapper, T: Eq + Hash> {
pub struct UniqueTableBuilder<ID: IdF, T: Eq + Hash> {
pub symtbl: HashMap<T, ID>,
pub syms: IdVec<ID, T>,
pub phantom: PhantomData<ID>,
}

impl<ID: IdRemapper, T: Clone + Eq + Hash> UniqueTableBuilder<ID, T> {
impl<ID: IdF, T: Clone + Eq + Hash> UniqueTableBuilder<ID, T> {
pub fn new() -> Self {
Self {
symtbl: HashMap::new(),
Expand Down
96 changes: 42 additions & 54 deletions werbolg-core/src/id.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Id(u32);

impl Id {
pub fn as_index(self) -> usize {
self.0 as usize
}
pub trait IdF:
core::fmt::Debug + core::hash::Hash + PartialEq + Eq + PartialOrd + Ord + Copy
{
fn as_index(self) -> usize;
fn from_slice_len<T>(slice: &[T]) -> Self;
fn from_collection_len(len: usize) -> Self;
fn remap(left: Self, right: Self) -> Self;
fn add(left: Self, right: u32) -> Self;
fn diff(left: Self, right: Self) -> u32;
}

pub fn from_slice_len<T>(slice: &[T]) -> Self {
Id(slice.len() as u32)
}
macro_rules! define_id_remapper {
($constr:ident, $n:literal, $c:expr) => {
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct $constr(u32);

pub fn from_collection_len(len: usize) -> Self {
Id(len as u32)
}
impl IdF for $constr {
fn as_index(self) -> usize {
self.0 as usize
}

pub fn remap(left: Self, right: Self) -> Self {
Self(left.0 + right.0)
}
fn from_slice_len<T>(slice: &[T]) -> Self {
Self(slice.len() as u32)
}

pub fn add(left: Self, right: u32) -> Self {
Self(left.0.checked_add(right).expect("ID valid add"))
}
fn from_collection_len(len: usize) -> Self {
Self(len as u32)
}

pub fn diff(left: Self, right: Self) -> u32 {
left.0.checked_sub(right.0).expect("ID valid diff")
}
}
fn remap(left: Self, right: Self) -> Self {
Self(left.0 + right.0)
}

impl core::fmt::Debug for Id {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.0)
}
}
fn add(left: Self, right: u32) -> Self {
Self(left.0.checked_add(right).expect("ID valid add"))
}

pub trait IdRemapper: Copy {
fn uncat(self) -> Id;
fn cat(id: Id) -> Self;
}
fn diff(left: Self, right: Self) -> u32 {
left.0.checked_sub(right.0).expect("ID valid diff")
}
}

macro_rules! define_id_remapper {
($constr:ident, $c:expr) => {
/*
impl IdRemapper for $constr {
fn uncat(self) -> Id {
self.0
Expand All @@ -49,6 +50,7 @@ macro_rules! define_id_remapper {
Self(id)
}
}
*/

impl core::fmt::Debug for $constr {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
Expand All @@ -58,26 +60,12 @@ macro_rules! define_id_remapper {
};
}

#[derive(Copy, Clone, PartialEq, Eq)]
pub struct FunId(Id);

#[derive(Copy, Clone, PartialEq, Eq)]
pub struct LitId(Id);

#[derive(Copy, Clone, PartialEq, Eq)]
pub struct ConstrId(Id);

#[derive(Copy, Clone, PartialEq, Eq)]
pub struct NifId(Id);

#[derive(Copy, Clone, PartialEq, Eq)]
pub struct GlobalId(Id);

define_id_remapper!(FunId, 'F');
define_id_remapper!(LitId, 'L');
define_id_remapper!(ConstrId, 'C');
define_id_remapper!(NifId, 'N');
define_id_remapper!(GlobalId, 'G');
define_id_remapper!(FunId, 32, 'F');
define_id_remapper!(LitId, 32, 'L');
define_id_remapper!(ConstrId, 32, 'C');
define_id_remapper!(NifId, 32, 'N');
define_id_remapper!(GlobalId, 32, 'G');
define_id_remapper!(InstructionAddress, 32, '%');

#[derive(Clone, Copy, Debug)]
pub enum ValueFun {
Expand Down
2 changes: 1 addition & 1 deletion werbolg-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ mod location;
pub mod symbols;

pub use basic::*;
pub use id::{ConstrId, FunId, GlobalId, Id, LitId, NifId, ValueFun};
pub use id::{ConstrId, FunId, GlobalId, LitId, NifId, ValueFun};
pub use ir::*;
pub use location::*;
Loading

0 comments on commit 5a3ea50

Please sign in to comment.