Skip to content

Commit

Permalink
Use compiler_magic!() instead of todo!()
Browse files Browse the repository at this point in the history
  • Loading branch information
minseongg committed Oct 7, 2024
1 parent 13ad6dc commit 24a13af
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 46 deletions.
2 changes: 1 addition & 1 deletion hazardflow-designs/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
pub use hazardflow_macro::*;

pub use crate::std::value::*;
pub use crate::{display, hassert, hpanic};
pub use crate::{compiler_magic, display, hassert, hpanic};
6 changes: 3 additions & 3 deletions hazardflow-designs/src/std/hazard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,18 @@ pub struct Ready<R> {
pub inner: R,
}

impl<R> Ready<R> {
impl<R: Copy> Ready<R> {
/// Generates a new `Ready` with the given `ready` bit and inner resolver.
pub fn new(ready: bool, inner: R) -> Self {
Self { ready, inner }
}

/// Creates a new invalid signal.
// TODO: We should add `inner` as parameter to set the inner hazard value when creating invalid signal.
// This is needed because the inner hazard value should be allowed as don't-care value only when explicit `unsafe` reasoning by user is given.
// This is needed because the inner hazard value should be allowed as don't-care value only when explicit `unsafe` reasoning by user is given.
#[allow(unreachable_code)]
pub fn invalid() -> Self {
Self { ready: false, inner: todo!("inner should be dont-care value") }
Self { ready: false, inner: unsafe { x::<R>() } }
}

/// Creates a new valid signal.
Expand Down
11 changes: 11 additions & 0 deletions hazardflow-designs/src/std/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,14 @@ pub use utils::*;
pub use valid::*;
pub use valid_ready::*;
pub use value::*;

/// Indicates that the function is implemented as a compiler magic.
#[macro_export]
macro_rules! compiler_magic {
() => {
todo!()
};
($msg:expr) => {
todo!($msg)
};
}
42 changes: 21 additions & 21 deletions hazardflow-designs/src/std/value/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::ops::*;

use hazardflow_macro::magic;

use super::*;
use crate::prelude::*;
use crate::std::clog2;

/// An array of signals.
Expand Down Expand Up @@ -69,7 +69,7 @@ impl<V: Copy, const N: usize> Array<V, N> {
/// Returns a new array with the `idx`-th element set to `elt`.
#[magic(array::set)]
pub fn set<Idx: Into<U<{ clog2(N) }>>>(self, _idx: Idx, _elt: V) -> Array<V, N> {
todo!()
compiler_magic!()
}

/// Returns a new array with the `idx`-th element set to `elt` if `cond` is true.
Expand All @@ -84,13 +84,13 @@ impl<V: Copy, const N: usize> Array<V, N> {
/// Returns a new clipped array of size `M` starting from `index`.
#[magic(array::clip_const)]
pub fn clip_const<const M: usize>(self, _index: usize) -> Array<V, M> {
todo!();
compiler_magic!()
}

/// Returns a new array that has tuples from the two given arrays as elements.
#[magic(array::zip)]
pub fn zip<W: Copy>(self, _other: Array<W, N>) -> Array<(V, W), N> {
todo!()
compiler_magic!()
}

/// Returns a new array whose elements are enumerated with their indices.
Expand All @@ -101,15 +101,15 @@ impl<V: Copy, const N: usize> Array<V, N> {
/// Transforms elements of `self` using `f`.
#[magic(array::map)]
pub fn map<W: Copy, F: FnOnce(V) -> W>(self, _f: F) -> Array<W, N> {
todo!()
compiler_magic!()
}

/// Folds the array into a single value.
///
/// The fold order is from left to right. (i.e. `foldl`)
#[magic(array::fold)]
pub fn fold<B: Copy, F: FnOnce(B, V) -> B>(self, _init: B, _f: F) -> B {
todo!()
compiler_magic!()
}

/// Tests if any element matches a predicate.
Expand All @@ -127,25 +127,25 @@ impl<V: Copy, const N: usize> Array<V, N> {
/// Resizes the given array.
#[magic(array::resize)]
pub fn resize<const M: usize>(self) -> Array<V, M> {
todo!()
compiler_magic!()
}

/// Chunks the array into an array of arrays.
#[magic(array::chunk)]
pub fn chunk<const M: usize>(self) -> Array<Array<V, M>, { N / M }> {
todo!();
compiler_magic!()
}

/// Returns a new array with the two given arrays appended.
#[magic(array::append)]
pub fn append<const M: usize>(self, _other: Array<V, M>) -> Array<V, { N + M }> {
todo!();
compiler_magic!()
}

/// Returns a new array with the `M` elements starting from `index` set to the elements of `other`.
#[magic(array::set_range)]
pub fn set_range<const M: usize>(self, _index: usize, _other: Array<V, M>) -> Array<V, N> {
todo!();
compiler_magic!()
}

/// Returns a Cartesian product of the two arrays.
Expand All @@ -164,7 +164,7 @@ impl<V: Copy, const N: usize, const M: usize> Array<Array<V, N>, M> {
/// Concatenates the array of arrays into a 1D array.
#[magic(array::concat)]
pub fn concat(self) -> Array<V, { M * N }> {
todo!();
compiler_magic!()
}
}

Expand All @@ -173,13 +173,13 @@ impl<V: Copy, const N: usize, const M: usize> Array<Array<V, N>, M> {
// TODO: allow different starting point (FROM..START)
#[magic(array::range)]
pub fn range<const N: usize>() -> Array<U<{ clog2(N) }>, N> {
todo!("compiler magic")
compiler_magic!()
}

impl<V: Copy, const N: usize> From<[V; N]> for Array<V, N> {
#[magic(array::from)]
fn from(_value: [V; N]) -> Self {
todo!();
compiler_magic!()
}
}

Expand All @@ -188,20 +188,20 @@ impl<V: Copy, const N: usize, const M: usize> Index<U<N>> for Array<V, M> {

#[magic(array::index)]
fn index(&self, _idx: U<N>) -> &V {
todo!()
compiler_magic!()
}
}

impl<V: Copy, const N: usize> PartialEq for Array<V, N> {
#[magic(array::eq)]
fn eq(&self, _other: &Self) -> bool {
todo!()
compiler_magic!()
}

#[allow(clippy::partialeq_ne_impl)]
#[magic(array::ne)]
fn ne(&self, _other: &Self) -> bool {
todo!()
compiler_magic!()
}
}

Expand All @@ -210,7 +210,7 @@ impl<V: Copy, const M: usize> Index<usize> for Array<V, M> {

#[magic(array::index)]
fn index(&self, _idx: usize) -> &V {
todo!()
compiler_magic!()
}
}

Expand All @@ -219,7 +219,7 @@ impl<V: Copy, const N: usize> BitOr for Array<V, N> {

#[magic(array::bitor)]
fn bitor(self, _rhs: Self) -> Self::Output {
todo!()
compiler_magic!()
}
}

Expand All @@ -228,7 +228,7 @@ impl<V: Copy, const N: usize> BitAnd for Array<V, N> {

#[magic(array::bitand)]
fn bitand(self, _rhs: Self) -> Self::Output {
todo!()
compiler_magic!()
}
}

Expand All @@ -237,7 +237,7 @@ impl<V: Copy, const N: usize> BitXor for Array<V, N> {

#[magic(array::bitxor)]
fn bitxor(self, _rhs: Self) -> Self {
todo!();
compiler_magic!()
}
}

Expand All @@ -250,6 +250,6 @@ pub trait RepeatExt: Copy {
impl<T: Copy> RepeatExt for T {
#[magic(array::repeat)]
fn repeat<const N: usize>(self) -> Array<Self, N> {
todo!()
compiler_magic!()
}
}
42 changes: 21 additions & 21 deletions hazardflow-designs/src/std/value/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use core::ops::*;
use hazardflow_macro::magic;

use super::Array;
use crate::prelude::RepeatExt;
use crate::prelude::*;

/// An integer with bitwidth `N`.
///
Expand All @@ -18,56 +18,56 @@ pub type U<const N: usize> = Array<bool, N>;
impl<const N: usize> From<U<N>> for u32 {
#[magic(int::convert)]
fn from(_value: U<N>) -> Self {
todo!()
compiler_magic!()
}
}

impl<const N: usize> From<U<N>> for u8 {
#[magic(int::convert)]
fn from(_value: U<N>) -> Self {
todo!()
compiler_magic!()
}
}

impl<const N: usize> From<i32> for U<N> {
#[magic(int::convert)]
fn from(_value: i32) -> U<N> {
todo!()
compiler_magic!()
}
}

impl<const N: usize> From<u32> for U<N> {
#[magic(int::convert)]
fn from(_value: u32) -> U<N> {
todo!()
compiler_magic!()
}
}

impl<const N: usize> From<usize> for U<N> {
#[magic(int::convert)]
fn from(_value: usize) -> U<N> {
todo!()
compiler_magic!()
}
}

impl<const N: usize> From<u128> for U<N> {
#[magic(int::convert)]
fn from(_value: u128) -> U<N> {
todo!()
compiler_magic!()
}
}

impl From<bool> for U<1> {
#[magic(int::convert)]
fn from(_value: bool) -> U<1> {
todo!()
compiler_magic!()
}
}

impl<const N: usize> From<U<N>> for bool {
#[magic(int::convert)]
fn from(_value: U<N>) -> bool {
todo!()
compiler_magic!()
}
}

Expand All @@ -76,7 +76,7 @@ impl<const N: usize> Not for U<N> {

#[magic(int::not)]
fn not(self) -> Self::Output {
todo!();
compiler_magic!()
}
}

Expand All @@ -85,7 +85,7 @@ impl<const N: usize, const M: usize> Shr<U<M>> for U<N> {

#[magic(int::shr)]
fn shr(self, _rhs: U<M>) -> Self::Output {
todo!();
compiler_magic!()
}
}

Expand All @@ -94,7 +94,7 @@ impl<const N: usize> Shr<usize> for U<N> {

#[magic(int::shr)]
fn shr(self, _rhs: usize) -> Self::Output {
todo!();
compiler_magic!()
}
}

Expand All @@ -103,7 +103,7 @@ impl<const N: usize, const M: usize> Shl<U<M>> for U<N> {

#[magic(int::shl)]
fn shl(self, _lhs: U<M>) -> Self::Output {
todo!();
compiler_magic!()
}
}

Expand All @@ -112,7 +112,7 @@ impl<const N: usize> Shl<usize> for U<N> {

#[magic(int::shl)]
fn shl(self, _lhs: usize) -> Self::Output {
todo!();
compiler_magic!()
}
}

Expand All @@ -123,7 +123,7 @@ where [(); N + 1]:

#[magic(int::add)]
fn add(self, _rhs: U<N>) -> U<{ N + 1 }> {
todo!();
compiler_magic!()
}
}

Expand Down Expand Up @@ -182,7 +182,7 @@ impl<const N: usize> Sub<U<N>> for U<N> {

#[magic(int::sub)]
fn sub(self, _other: U<N>) -> U<N> {
todo!();
compiler_magic!()
}
}

Expand All @@ -193,7 +193,7 @@ where [(); N + M]:

#[magic(int::mul)]
fn mul(self, _other: U<M>) -> Self::Output {
todo!();
compiler_magic!()
}
}

Expand All @@ -204,22 +204,22 @@ impl<const N: usize> PartialOrd for U<N> {

#[magic(int::lt)]
fn lt(&self, _other: &Self) -> bool {
todo!("compiler magic")
compiler_magic!()
}

#[magic(int::le)]
fn le(&self, _other: &Self) -> bool {
todo!("compiler magic")
compiler_magic!()
}

#[magic(int::gt)]
fn gt(&self, _other: &Self) -> bool {
todo!("compiler magic")
compiler_magic!()
}

#[magic(int::ge)]
fn ge(&self, _other: &Self) -> bool {
todo!("compiler magic")
compiler_magic!()
}
}

Expand Down

0 comments on commit 24a13af

Please sign in to comment.