Skip to content

WIP: evm codegen #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk



*/target
.DS_Store
.zed/*
10 changes: 10 additions & 0 deletions crates/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@ rustc-hash = "2.0.0"
sonatina-ir = { path = "../ir", version = "0.0.3-alpha" }
sonatina-triple = { path = "../triple", version = "0.0.3-alpha" }
sonatina-macros = { path = "../macros", version = "0.0.3-alpha" }
bit-set = "0.8.0"
indexmap = "2.6.0"
if_chain = "1.0.2"

[dev-dependencies]
dir-test = "0.4.0"
hex = "0.4.3"
insta = "1.41.1"
revm = { version = "18.0.0", default-features = false, features = ["std"] }
sonatina-parser = { path = "../parser", version = "0.0.3-alpha" }
130 changes: 130 additions & 0 deletions crates/codegen/src/bitset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
use bit_set::BitSet as Bs;
use cranelift_entity::EntityRef;
use std::{fmt, marker::PhantomData};

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct BitSet<T> {
bs: Bs,
marker: PhantomData<T>,
}

impl<T> BitSet<T> {
pub fn new() -> Self {
Self {
bs: Bs::new(),
marker: PhantomData,
}
}

pub fn is_empty(&self) -> bool {
self.bs.is_empty()
}

pub fn len(&self) -> usize {
self.bs.len()
}

pub fn union_with(&mut self, other: &Self) {
self.bs.union_with(&other.bs)
}

pub fn intersect_with(&mut self, other: &Self) {
self.bs.intersect_with(&other.bs)
}

pub fn difference_with(&mut self, other: &Self) {
self.bs.difference_with(&other.bs)
}

pub fn symmetric_difference_with(&mut self, other: &Self) {
self.bs.symmetric_difference_with(&other.bs)
}

pub fn is_subset(&self, other: &Self) -> bool {
self.bs.is_subset(&other.bs)
}

pub fn is_superset(&self, other: &Self) -> bool {
self.bs.is_superset(&other.bs)
}

pub fn is_disjoint(&self, other: &Self) -> bool {
self.bs.is_disjoint(&other.bs)
}

pub fn clear(&mut self) {
self.bs.clear()
}
}

impl<T> BitSet<T>
where
T: EntityRef,
{
pub fn difference(a: &Self, b: &Self) -> Self {
let mut d = a.clone();
d.difference_with(b);
d
}

pub fn insert(&mut self, elem: T) -> bool {
self.bs.insert(elem.index())
}
pub fn remove(&mut self, elem: T) -> bool {
self.bs.remove(elem.index())
}
pub fn contains(&self, elem: T) -> bool {
self.bs.contains(elem.index())
}
pub fn iter(&self) -> impl Iterator<Item = T> + '_ {
self.bs.iter().map(|v| T::new(v))
}
}

impl<T> Default for BitSet<T> {
fn default() -> Self {
Self::new()
}
}

impl<T> fmt::Debug for BitSet<T>
where
T: EntityRef + fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_set().entries(self.bs.iter()).finish()
}
}

impl<T: EntityRef> From<&[T]> for BitSet<T> {
fn from(elems: &[T]) -> Self {
let mut bs = BitSet::new();
for e in elems {
bs.insert(*e);
}
bs
}
}

impl<T: EntityRef, const N: usize> From<[T; N]> for BitSet<T> {
fn from(elems: [T; N]) -> Self {
let mut bs = BitSet::new();
for e in elems {
bs.insert(e);
}
bs
}
}

impl<A: EntityRef> FromIterator<A> for BitSet<A> {
fn from_iter<T>(iter: T) -> Self
where
T: IntoIterator<Item = A>,
{
let mut bs = BitSet::new();
for e in iter {
bs.insert(e);
}
bs
}
}
Loading
Loading