@@ -581,65 +581,93 @@ pub fn gen_register(r: &Register,
581
581
_ => unreachable ! ( ) ,
582
582
}
583
583
584
- if let Some ( ref fields) = r. fields {
585
- let mut reexported = HashSet :: new ( ) ;
586
- let mut mod_items = vec ! [ ] ;
587
- let mut impl_items = vec ! [ ] ;
588
-
589
- if access == Access :: ReadWrite {
590
- impl_items. push ( quote ! {
591
- pub fn modify<F >( & mut self , f: F )
592
- where for <' w> F : FnOnce ( & R , & ' w mut W ) -> & ' w mut W ,
593
- {
594
- let bits = self . register. read( ) ;
595
- let r = R { bits: bits } ;
596
- let mut w = W { bits: bits } ;
597
- f( & r, & mut w) ;
598
- self . register. write( w. bits) ;
599
- }
600
- } ) ;
601
- }
584
+ let mut mod_items = vec ! [ ] ;
585
+ let mut impl_items = vec ! [ ] ;
586
+ let mut r_impl_items = vec ! [ ] ;
587
+ let mut w_impl_items = vec ! [ ] ;
588
+ if access == Access :: ReadWrite {
589
+ impl_items. push ( quote ! {
590
+ pub fn modify<F >( & mut self , f: F )
591
+ where for <' w> F : FnOnce ( & R , & ' w mut W ) -> & ' w mut W ,
592
+ {
593
+ let bits = self . register. read( ) ;
594
+ let r = R { bits: bits } ;
595
+ let mut w = W { bits: bits } ;
596
+ f( & r, & mut w) ;
597
+ self . register. write( w. bits) ;
598
+ }
599
+ } ) ;
600
+ }
602
601
603
- if access == Access :: ReadOnly || access == Access :: ReadWrite {
604
- impl_items. push ( quote ! {
605
- pub fn read( & self ) -> R {
606
- R { bits: self . register. read( ) }
607
- }
608
- } ) ;
609
- }
602
+ if access == Access :: ReadOnly || access == Access :: ReadWrite {
603
+ impl_items. push ( quote ! {
604
+ pub fn read( & self ) -> R {
605
+ R { bits: self . register. read( ) }
606
+ }
607
+ } ) ;
610
608
611
- if access == Access :: WriteOnly || access == Access :: ReadWrite {
612
- impl_items. push ( quote ! {
613
- pub fn write<F >( & mut self , f: F )
614
- where F : FnOnce ( & mut W ) -> & mut W ,
615
- {
616
- let mut w = W :: reset_value( ) ;
617
- f( & mut w) ;
618
- self . register. write( w. bits) ;
619
- }
620
- } ) ;
621
- }
609
+ mod_items. push ( quote ! {
610
+ pub struct R {
611
+ bits: #reg_ty,
612
+ }
613
+ } ) ;
614
+
615
+ r_impl_items. push ( quote ! {
616
+ pub fn bits( & self ) -> #reg_ty {
617
+ self . bits
618
+ }
619
+ } ) ;
620
+ }
621
+
622
+ if access == Access :: WriteOnly || access == Access :: ReadWrite {
623
+ impl_items. push ( quote ! {
624
+ pub fn write<F >( & mut self , f: F )
625
+ where F : FnOnce ( & mut W ) -> & mut W ,
626
+ {
627
+ let mut w = W :: reset_value( ) ;
628
+ f( & mut w) ;
629
+ self . register. write( w. bits) ;
630
+ }
631
+ } ) ;
622
632
623
633
mod_items. push ( quote ! {
624
- impl super :: #name_pc {
625
- # ( #impl_items ) *
634
+ pub struct W {
635
+ bits : #reg_ty ,
626
636
}
627
637
} ) ;
628
638
629
- if access == Access :: ReadOnly || access == Access :: ReadWrite {
630
- mod_items. push ( quote ! {
631
- pub struct R {
632
- bits: #reg_ty,
639
+ if let Some ( reset_value) =
640
+ r. reset_value
641
+ . or ( d. reset_value )
642
+ . map ( |x| Lit :: Int ( x as u64 , IntTy :: Unsuffixed ) ) {
643
+ w_impl_items. push ( quote ! {
644
+ /// Reset value
645
+ pub fn reset_value( ) -> W {
646
+ W { bits: #reset_value }
633
647
}
634
648
} ) ;
649
+ }
635
650
636
- let mut impl_items = vec ! [ ] ;
637
- impl_items. push ( quote ! {
638
- pub fn bits( & self ) -> #reg_ty {
639
- self . bits
640
- }
641
- } ) ;
651
+ w_impl_items. push ( quote ! {
652
+ pub unsafe fn bits( & mut self , bits: #reg_ty) -> & mut Self {
653
+ self . bits = bits;
654
+ self
655
+ }
656
+ } ) ;
657
+ }
658
+
659
+ mod_items. push ( quote ! {
660
+ impl super :: #name_pc {
661
+ #( #impl_items) *
662
+ }
663
+ } ) ;
664
+
665
+ let fields = r. fields . as_ref ( ) . map ( |fs| & * * fs) . unwrap_or ( & [ ] ) ;
642
666
667
+ if !fields. is_empty ( ) {
668
+ let mut reexported = HashSet :: new ( ) ;
669
+
670
+ if access == Access :: ReadOnly || access == Access :: ReadWrite {
643
671
for field in fields {
644
672
let field_name = Ident :: new ( & * field. name
645
673
. to_sanitized_snake_case ( ) ) ;
@@ -652,7 +680,7 @@ pub fn gen_register(r: &Register,
652
680
IntTy :: Unsuffixed ) ;
653
681
let field_ty = width. to_ty ( ) ;
654
682
655
- impl_items . push ( quote ! {
683
+ r_impl_items . push ( quote ! {
656
684
fn #_field_name( & self ) -> #field_ty {
657
685
const MASK : #field_ty = #mask;
658
686
const OFFSET : u8 = #offset;
@@ -721,7 +749,7 @@ pub fn gen_register(r: &Register,
721
749
}
722
750
}
723
751
724
- impl_items . push ( quote ! {
752
+ r_impl_items . push ( quote ! {
725
753
pub fn #field_name( & self ) -> #enum_name {
726
754
#enum_name:: _from( self . #_field_name( ) )
727
755
}
@@ -744,8 +772,8 @@ pub fn gen_register(r: &Register,
744
772
let pc = & v. pc ;
745
773
746
774
quote ! {
747
- #enum_name:: #pc => #value
748
- }
775
+ #enum_name:: #pc => #value
776
+ }
749
777
} ) ;
750
778
enum_items. push ( quote ! {
751
779
pub fn bits( & self ) -> #field_ty {
@@ -761,8 +789,8 @@ pub fn gen_register(r: &Register,
761
789
let pc = & v. pc ;
762
790
763
791
quote ! {
764
- #i => #enum_name:: #pc
765
- }
792
+ #i => #enum_name:: #pc
793
+ }
766
794
} ) ;
767
795
768
796
enum_items. push ( quote ! {
@@ -780,14 +808,15 @@ pub fn gen_register(r: &Register,
780
808
if let Some ( ref sc) = v. sc {
781
809
let pc = & v. pc ;
782
810
783
- let is_variant = Ident :: new ( & * format ! ( "is_{}" ,
784
- sc) ) ;
811
+ let is_variant = {
812
+ Ident :: new ( & * format ! ( "is_{}" , sc) )
813
+ } ;
785
814
786
815
enum_items. push ( quote ! {
787
816
pub fn #is_variant( & self ) -> bool {
788
817
* self == #enum_name:: #pc
789
818
}
790
- } )
819
+ } ) ;
791
820
}
792
821
}
793
822
@@ -798,48 +827,16 @@ pub fn gen_register(r: &Register,
798
827
} ) ;
799
828
}
800
829
} else {
801
- impl_items . push ( quote ! {
830
+ r_impl_items . push ( quote ! {
802
831
pub fn #field_name( & self ) -> #field_ty {
803
832
self . #_field_name( )
804
833
}
805
834
} ) ;
806
835
}
807
836
}
808
-
809
- mod_items. push ( quote ! {
810
- impl R {
811
- #( #impl_items) *
812
- }
813
- } ) ;
814
837
}
815
838
816
839
if access == Access :: WriteOnly || access == Access :: ReadWrite {
817
- mod_items. push ( quote ! {
818
- pub struct W {
819
- bits: #reg_ty,
820
- }
821
- } ) ;
822
-
823
- let mut impl_items = vec ! [ ] ;
824
- if let Some ( reset_value) =
825
- r. reset_value
826
- . or ( d. reset_value )
827
- . map ( |x| Lit :: Int ( x as u64 , IntTy :: Unsuffixed ) ) {
828
- impl_items. push ( quote ! {
829
- /// Reset value
830
- pub fn reset_value( ) -> W {
831
- W { bits: #reset_value }
832
- }
833
- } ) ;
834
- }
835
-
836
- impl_items. push ( quote ! {
837
- pub unsafe fn bits( & mut self , bits: #reg_ty) -> & mut Self {
838
- self . bits = bits;
839
- self
840
- }
841
- } ) ;
842
-
843
840
for field in fields {
844
841
let field_name_sc = Ident :: new ( & * field. name
845
842
. to_sanitized_snake_case ( ) ) ;
@@ -849,7 +846,8 @@ pub fn gen_register(r: &Register,
849
846
IntTy :: Unsuffixed ) ;
850
847
let field_ty = width. to_ty ( ) ;
851
848
let proxy = Ident :: new ( & * format ! ( "_{}W" ,
852
- field. name. to_pascal_case( ) ) ) ;
849
+ field. name
850
+ . to_pascal_case( ) ) ) ;
853
851
854
852
mod_items. push ( quote ! {
855
853
/// Proxy
@@ -891,8 +889,8 @@ pub fn gen_register(r: &Register,
891
889
892
890
if !reexported. contains ( & enum_name) {
893
891
mod_items. push ( quote ! {
894
- pub use super :: #register:: #enum_name;
895
- } ) ;
892
+ pub use super :: #register:: #enum_name;
893
+ } ) ;
896
894
897
895
reexported. insert ( enum_name. clone ( ) ) ;
898
896
}
@@ -914,9 +912,9 @@ pub fn gen_register(r: &Register,
914
912
. collect :: < Vec < _ > > ( ) ;
915
913
916
914
// Whether the `bits` method should be `unsafe`.
917
- // `bits` can be safe when enumeratedValues covers all the
918
- // possible values of the bitfield or, IOW, when there are
919
- // no reserved bit patterns.
915
+ // `bits` can be safe when enumeratedValues covers all
916
+ // the possible values of the bitfield or, IOW, when
917
+ // there are no reserved bit patterns.
920
918
bits_is_safe = variants. len ( ) == 1 << width;
921
919
922
920
if base. is_none ( ) {
@@ -952,14 +950,14 @@ pub fn gen_register(r: &Register,
952
950
if bits_is_safe {
953
951
proxy_items. push ( quote ! {
954
952
pub fn variant( self ,
955
- variant: #enum_name) -> & ' a mut W {
953
+ variant: #enum_name) -> & ' a mut W {
956
954
self . bits( variant. _bits( ) )
957
955
}
958
956
} ) ;
959
957
} else {
960
958
proxy_items. push ( quote ! {
961
959
pub fn variant( self ,
962
- variant: #enum_name) -> & ' a mut W {
960
+ variant: #enum_name) -> & ' a mut W {
963
961
unsafe {
964
962
self . bits( variant. _bits( ) )
965
963
}
@@ -1015,59 +1013,39 @@ pub fn gen_register(r: &Register,
1015
1013
}
1016
1014
} ) ;
1017
1015
1018
- impl_items . push ( quote ! {
1016
+ w_impl_items . push ( quote ! {
1019
1017
pub fn #field_name_sc( & mut self ) -> #proxy {
1020
1018
#proxy {
1021
1019
register: self ,
1022
1020
}
1023
1021
}
1024
1022
} ) ;
1025
1023
}
1026
-
1027
- mod_items. push ( quote ! {
1028
- impl W {
1029
- #( #impl_items) *
1030
- }
1031
- } ) ;
1032
1024
}
1025
+ }
1033
1026
1034
- items. push ( quote ! {
1035
- pub mod #name_sc {
1036
- #( #mod_items) *
1027
+ if access == Access :: ReadOnly || access == Access :: ReadWrite {
1028
+ mod_items. push ( quote ! {
1029
+ impl R {
1030
+ #( #r_impl_items) *
1037
1031
}
1038
1032
} ) ;
1039
- } else {
1040
- let mut impl_items = vec ! [ ] ;
1041
-
1042
- match access {
1043
- Access :: ReadOnly | Access :: ReadWrite => {
1044
- impl_items. push ( quote ! {
1045
- pub fn read( & self ) -> #reg_ty {
1046
- self . register. read( )
1047
- }
1048
- } ) ;
1049
- }
1050
- _ => { }
1051
- }
1052
-
1053
- match access {
1054
- Access :: ReadOnly | Access :: ReadWrite => {
1055
- impl_items. push ( quote ! {
1056
- pub fn write( & mut self , value: #reg_ty) {
1057
- self . register. write( value) ;
1058
- }
1059
- } ) ;
1060
- }
1061
- _ => { }
1062
- }
1033
+ }
1063
1034
1064
- items. push ( quote ! {
1065
- impl #name_pc {
1066
- #( #impl_items) *
1035
+ if access == Access :: WriteOnly || access == Access :: ReadWrite {
1036
+ mod_items. push ( quote ! {
1037
+ impl W {
1038
+ #( #w_impl_items) *
1067
1039
}
1068
1040
} ) ;
1069
1041
}
1070
1042
1043
+ items. push ( quote ! {
1044
+ pub mod #name_sc {
1045
+ #( #mod_items) *
1046
+ }
1047
+ } ) ;
1048
+
1071
1049
items
1072
1050
}
1073
1051
0 commit comments