forked from libre-chip/fayalite
		
	split int::IntCmp into expr::HdlPartialEq and expr::HdlPartialOrd
This commit is contained in:
		
							parent
							
								
									9ad4ec0f39
								
							
						
					
					
						commit
						78edfc97b2
					
				
					 5 changed files with 69 additions and 26 deletions
				
			
		|  | @ -641,6 +641,18 @@ impl<T: PortType> GetTarget for MemPort<T> { | |||
|     } | ||||
| } | ||||
| 
 | ||||
| pub trait HdlPartialEq<Rhs> { | ||||
|     fn cmp_eq(self, rhs: Rhs) -> Expr<Bool>; | ||||
|     fn cmp_ne(self, rhs: Rhs) -> Expr<Bool>; | ||||
| } | ||||
| 
 | ||||
| pub trait HdlPartialOrd<Rhs>: HdlPartialEq<Rhs> { | ||||
|     fn cmp_lt(self, rhs: Rhs) -> Expr<Bool>; | ||||
|     fn cmp_le(self, rhs: Rhs) -> Expr<Bool>; | ||||
|     fn cmp_gt(self, rhs: Rhs) -> Expr<Bool>; | ||||
|     fn cmp_ge(self, rhs: Rhs) -> Expr<Bool>; | ||||
| } | ||||
| 
 | ||||
| pub trait ReduceBits { | ||||
|     type UIntOutput; | ||||
|     type BoolOutput; | ||||
|  |  | |||
|  | @ -11,11 +11,12 @@ use crate::{ | |||
|             GetTarget, Target, TargetPathArrayElement, TargetPathBundleField, | ||||
|             TargetPathDynArrayElement, TargetPathElement, | ||||
|         }, | ||||
|         CastTo, Expr, ExprEnum, Flow, NotALiteralExpr, ReduceBits, ToExpr, ToLiteralBits, | ||||
|         CastTo, Expr, ExprEnum, Flow, HdlPartialEq, HdlPartialOrd, NotALiteralExpr, ReduceBits, | ||||
|         ToExpr, ToLiteralBits, | ||||
|     }, | ||||
|     int::{ | ||||
|         Bool, BoolOrIntType, DynSize, IntCmp, IntType, KnownSize, SInt, SIntType, SIntValue, Size, | ||||
|         UInt, UIntType, UIntValue, | ||||
|         Bool, BoolOrIntType, DynSize, IntType, KnownSize, SInt, SIntType, SIntValue, Size, UInt, | ||||
|         UIntType, UIntValue, | ||||
|     }, | ||||
|     intern::{Intern, Interned}, | ||||
|     reset::{AsyncReset, Reset, SyncReset, ToAsyncReset, ToReset, ToSyncReset}, | ||||
|  | @ -1422,36 +1423,45 @@ forward_value_to_expr_binary_op_trait! { | |||
|     Shr::shr | ||||
| } | ||||
| 
 | ||||
| pub trait IntCmpExpr<Rhs: Type>: Type { | ||||
| pub trait ExprPartialEq<Rhs: Type>: Type { | ||||
|     fn cmp_eq(lhs: Expr<Self>, rhs: Expr<Rhs>) -> Expr<Bool>; | ||||
|     fn cmp_ne(lhs: Expr<Self>, rhs: Expr<Rhs>) -> Expr<Bool>; | ||||
| } | ||||
| 
 | ||||
| pub trait ExprPartialOrd<Rhs: Type>: ExprPartialEq<Rhs> { | ||||
|     fn cmp_lt(lhs: Expr<Self>, rhs: Expr<Rhs>) -> Expr<Bool>; | ||||
|     fn cmp_le(lhs: Expr<Self>, rhs: Expr<Rhs>) -> Expr<Bool>; | ||||
|     fn cmp_gt(lhs: Expr<Self>, rhs: Expr<Rhs>) -> Expr<Bool>; | ||||
|     fn cmp_ge(lhs: Expr<Self>, rhs: Expr<Rhs>) -> Expr<Bool>; | ||||
| } | ||||
| 
 | ||||
| impl<Lhs: ToExpr, Rhs: ToExpr> IntCmp<Rhs> for Lhs | ||||
| impl<Lhs: ToExpr, Rhs: ToExpr> HdlPartialEq<Rhs> for Lhs | ||||
| where | ||||
|     Lhs::Type: IntCmpExpr<Rhs::Type>, | ||||
|     Lhs::Type: ExprPartialEq<Rhs::Type>, | ||||
| { | ||||
|     fn cmp_eq(self, rhs: Rhs) -> Expr<Bool> { | ||||
|         IntCmpExpr::cmp_eq(self.to_expr(), rhs.to_expr()) | ||||
|         ExprPartialEq::cmp_eq(self.to_expr(), rhs.to_expr()) | ||||
|     } | ||||
|     fn cmp_ne(self, rhs: Rhs) -> Expr<Bool> { | ||||
|         IntCmpExpr::cmp_ne(self.to_expr(), rhs.to_expr()) | ||||
|         ExprPartialEq::cmp_ne(self.to_expr(), rhs.to_expr()) | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| impl<Lhs: ToExpr, Rhs: ToExpr> HdlPartialOrd<Rhs> for Lhs | ||||
| where | ||||
|     Lhs::Type: ExprPartialOrd<Rhs::Type>, | ||||
| { | ||||
|     fn cmp_lt(self, rhs: Rhs) -> Expr<Bool> { | ||||
|         IntCmpExpr::cmp_lt(self.to_expr(), rhs.to_expr()) | ||||
|         ExprPartialOrd::cmp_lt(self.to_expr(), rhs.to_expr()) | ||||
|     } | ||||
|     fn cmp_le(self, rhs: Rhs) -> Expr<Bool> { | ||||
|         IntCmpExpr::cmp_le(self.to_expr(), rhs.to_expr()) | ||||
|         ExprPartialOrd::cmp_le(self.to_expr(), rhs.to_expr()) | ||||
|     } | ||||
|     fn cmp_gt(self, rhs: Rhs) -> Expr<Bool> { | ||||
|         IntCmpExpr::cmp_gt(self.to_expr(), rhs.to_expr()) | ||||
|         ExprPartialOrd::cmp_gt(self.to_expr(), rhs.to_expr()) | ||||
|     } | ||||
|     fn cmp_ge(self, rhs: Rhs) -> Expr<Bool> { | ||||
|         IntCmpExpr::cmp_ge(self.to_expr(), rhs.to_expr()) | ||||
|         ExprPartialOrd::cmp_ge(self.to_expr(), rhs.to_expr()) | ||||
|     } | ||||
| } | ||||
| 
 | ||||
|  | @ -1461,6 +1471,7 @@ macro_rules! impl_compare_op { | |||
|         #[dyn_type($DynTy:ident)] | ||||
|         #[to_dyn_type($lhs:ident => $dyn_lhs:expr, $rhs:ident => $dyn_rhs:expr)] | ||||
|         #[type($Lhs:ty, $Rhs:ty)] | ||||
|         #[trait($Trait:ident)] | ||||
|         $( | ||||
|             struct $name:ident; | ||||
|             fn $method:ident(); | ||||
|  | @ -1512,7 +1523,7 @@ macro_rules! impl_compare_op { | |||
|             } | ||||
|         })* | ||||
| 
 | ||||
|         impl$(<$LhsWidth: Size, $RhsWidth: Size>)? IntCmpExpr<$Rhs> for $Lhs { | ||||
|         impl$(<$LhsWidth: Size, $RhsWidth: Size>)? $Trait<$Rhs> for $Lhs { | ||||
|             $(fn $method($lhs: Expr<Self>, $rhs: Expr<$Rhs>) -> Expr<Bool> { | ||||
|                 $name::new($dyn_lhs, $dyn_rhs).to_expr() | ||||
|             })* | ||||
|  | @ -1524,8 +1535,16 @@ impl_compare_op! { | |||
|     #[dyn_type(Bool)] | ||||
|     #[to_dyn_type(lhs => lhs, rhs => rhs)] | ||||
|     #[type(Bool, Bool)] | ||||
|     #[trait(ExprPartialEq)] | ||||
|     struct CmpEqB; fn cmp_eq(); PartialEq::eq(); | ||||
|     struct CmpNeB; fn cmp_ne(); PartialEq::ne(); | ||||
| } | ||||
| 
 | ||||
| impl_compare_op! { | ||||
|     #[dyn_type(Bool)] | ||||
|     #[to_dyn_type(lhs => lhs, rhs => rhs)] | ||||
|     #[type(Bool, Bool)] | ||||
|     #[trait(ExprPartialOrd)] | ||||
|     struct CmpLtB; fn cmp_lt(); PartialOrd::lt(); | ||||
|     struct CmpLeB; fn cmp_le(); PartialOrd::le(); | ||||
|     struct CmpGtB; fn cmp_gt(); PartialOrd::gt(); | ||||
|  | @ -1537,8 +1556,17 @@ impl_compare_op! { | |||
|     #[dyn_type(UInt)] | ||||
|     #[to_dyn_type(lhs => Expr::as_dyn_int(lhs), rhs => Expr::as_dyn_int(rhs))] | ||||
|     #[type(UIntType<LhsWidth>, UIntType<RhsWidth>)] | ||||
|     #[trait(ExprPartialEq)] | ||||
|     struct CmpEqU; fn cmp_eq(); PartialEq::eq(); | ||||
|     struct CmpNeU; fn cmp_ne(); PartialEq::ne(); | ||||
| } | ||||
| 
 | ||||
| impl_compare_op! { | ||||
|     #[width(LhsWidth, RhsWidth)] | ||||
|     #[dyn_type(UInt)] | ||||
|     #[to_dyn_type(lhs => Expr::as_dyn_int(lhs), rhs => Expr::as_dyn_int(rhs))] | ||||
|     #[type(UIntType<LhsWidth>, UIntType<RhsWidth>)] | ||||
|     #[trait(ExprPartialOrd)] | ||||
|     struct CmpLtU; fn cmp_lt(); PartialOrd::lt(); | ||||
|     struct CmpLeU; fn cmp_le(); PartialOrd::le(); | ||||
|     struct CmpGtU; fn cmp_gt(); PartialOrd::gt(); | ||||
|  | @ -1550,8 +1578,17 @@ impl_compare_op! { | |||
|     #[dyn_type(SInt)] | ||||
|     #[to_dyn_type(lhs => Expr::as_dyn_int(lhs), rhs => Expr::as_dyn_int(rhs))] | ||||
|     #[type(SIntType<LhsWidth>, SIntType<RhsWidth>)] | ||||
|     #[trait(ExprPartialEq)] | ||||
|     struct CmpEqS; fn cmp_eq(); PartialEq::eq(); | ||||
|     struct CmpNeS; fn cmp_ne(); PartialEq::ne(); | ||||
| } | ||||
| 
 | ||||
| impl_compare_op! { | ||||
|     #[width(LhsWidth, RhsWidth)] | ||||
|     #[dyn_type(SInt)] | ||||
|     #[to_dyn_type(lhs => Expr::as_dyn_int(lhs), rhs => Expr::as_dyn_int(rhs))] | ||||
|     #[type(SIntType<LhsWidth>, SIntType<RhsWidth>)] | ||||
|     #[trait(ExprPartialOrd)] | ||||
|     struct CmpLtS; fn cmp_lt(); PartialOrd::lt(); | ||||
|     struct CmpLeS; fn cmp_le(); PartialOrd::le(); | ||||
|     struct CmpGtS; fn cmp_gt(); PartialOrd::gt(); | ||||
|  |  | |||
|  | @ -679,15 +679,6 @@ impl StaticType for Bool { | |||
|     const MASK_TYPE_PROPERTIES: TypeProperties = Bool::TYPE_PROPERTIES; | ||||
| } | ||||
| 
 | ||||
| pub trait IntCmp<Rhs> { | ||||
|     fn cmp_eq(self, rhs: Rhs) -> Expr<Bool>; | ||||
|     fn cmp_ne(self, rhs: Rhs) -> Expr<Bool>; | ||||
|     fn cmp_lt(self, rhs: Rhs) -> Expr<Bool>; | ||||
|     fn cmp_le(self, rhs: Rhs) -> Expr<Bool>; | ||||
|     fn cmp_gt(self, rhs: Rhs) -> Expr<Bool>; | ||||
|     fn cmp_ge(self, rhs: Rhs) -> Expr<Bool>; | ||||
| } | ||||
| 
 | ||||
| impl ToLiteralBits for bool { | ||||
|     fn to_literal_bits(&self) -> Result<Interned<BitSlice>, NotALiteralExpr> { | ||||
|         Ok(interned_bit(*self)) | ||||
|  |  | |||
|  | @ -6,10 +6,10 @@ use crate::{ | |||
|     enum_::{Enum, EnumType, EnumVariant}, | ||||
|     expr::{ | ||||
|         ops::{self, EnumLiteral}, | ||||
|         CastBitsTo, CastToBits, Expr, ExprEnum, ToExpr, | ||||
|         CastBitsTo, CastToBits, Expr, ExprEnum, HdlPartialEq, ToExpr, | ||||
|     }, | ||||
|     hdl, | ||||
|     int::{DynSize, IntCmp, Size, UInt, UIntType}, | ||||
|     int::{DynSize, Size, UInt, UIntType}, | ||||
|     intern::{Intern, Interned}, | ||||
|     memory::{DynPortType, Mem, MemPort}, | ||||
|     module::{ | ||||
|  |  | |||
|  | @ -6,9 +6,12 @@ pub use crate::{ | |||
|     cli::Cli, | ||||
|     clock::{Clock, ClockDomain, ToClock}, | ||||
|     enum_::{HdlNone, HdlOption, HdlSome}, | ||||
|     expr::{CastBitsTo, CastTo, CastToBits, Expr, MakeUninitExpr, ReduceBits, ToExpr}, | ||||
|     expr::{ | ||||
|         CastBitsTo, CastTo, CastToBits, Expr, HdlPartialEq, HdlPartialOrd, MakeUninitExpr, | ||||
|         ReduceBits, ToExpr, | ||||
|     }, | ||||
|     hdl, hdl_module, | ||||
|     int::{Bool, DynSize, IntCmp, KnownSize, SInt, SIntType, Size, UInt, UIntType}, | ||||
|     int::{Bool, DynSize, KnownSize, SInt, SIntType, Size, UInt, UIntType}, | ||||
|     memory::{Mem, MemBuilder, ReadUnderWrite}, | ||||
|     module::{ | ||||
|         annotate, connect, connect_any, incomplete_wire, instance, memory, memory_array, | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue