@@ -12,7 +12,7 @@ use super::iterators::{MatchIter, Matched};
1212use super :: register:: { RegisterExt , RegisterInfoExt } ;
1313use super :: yaml_ext:: { AsType , GetVal , ToYaml } ;
1414use super :: {
15- check_offsets, common_description, make_dim_element, matchname, matchsubspec,
15+ adding_pos , check_offsets, common_description, make_dim_element, matchname, matchsubspec,
1616 modify_dim_element, spec_ind, Config , PatchResult , Spec , VAL_LVL ,
1717} ;
1818use super :: { make_cluster, make_interrupt, make_register} ;
@@ -161,6 +161,14 @@ pub(crate) trait RegisterBlockExt: Name {
161161
162162 fn add_child ( & mut self , child : RegisterCluster ) ;
163163
164+ fn insert_child ( & mut self , pos : usize , child : RegisterCluster ) {
165+ if let Some ( children) = self . children_mut ( ) {
166+ children. insert ( pos, child) ;
167+ } else {
168+ self . add_child ( child) ;
169+ }
170+ }
171+
164172 /// Delete registers and clusters matched by rspec inside ptag
165173 fn delete_child ( & mut self , rcspec : & str ) -> PatchResult {
166174 if let Some ( children) = self . children_mut ( ) {
@@ -214,7 +222,8 @@ pub(crate) trait RegisterBlockExt: Name {
214222 Self :: RB_TYPE
215223 ) ) ;
216224 }
217- self . add_child ( RegisterCluster :: Register ( {
225+
226+ let rnew = RegisterCluster :: Register ( {
218227 let reg = make_register ( radd, Some ( bpath) ) ?
219228 . name ( rname. into ( ) )
220229 . build ( VAL_LVL ) ?;
@@ -223,7 +232,18 @@ pub(crate) trait RegisterBlockExt: Name {
223232 } else {
224233 reg. single ( )
225234 }
226- } ) ) ;
235+ } ) ;
236+
237+ if let Some ( children) = self . children ( ) {
238+ let pos = adding_pos ( & rnew, children, |rc| match rc {
239+ RegisterCluster :: Register ( r) => r. address_offset ,
240+ RegisterCluster :: Cluster ( c) => c. address_offset ,
241+ } ) ;
242+ self . insert_child ( pos, rnew) ;
243+ } else {
244+ self . add_child ( rnew) ;
245+ }
246+
227247 Ok ( ( ) )
228248 }
229249
@@ -235,7 +255,8 @@ pub(crate) trait RegisterBlockExt: Name {
235255 Self :: RB_TYPE
236256 ) ) ;
237257 }
238- self . add_child ( RegisterCluster :: Cluster ( {
258+
259+ let cnew = RegisterCluster :: Cluster ( {
239260 let cl = make_cluster ( cadd, Some ( bpath) ) ?
240261 . name ( cname. into ( ) )
241262 . build ( VAL_LVL ) ?;
@@ -244,7 +265,18 @@ pub(crate) trait RegisterBlockExt: Name {
244265 } else {
245266 cl. single ( )
246267 }
247- } ) ) ;
268+ } ) ;
269+
270+ if let Some ( children) = self . children ( ) {
271+ let pos = adding_pos ( & cnew, children, |rc| match rc {
272+ RegisterCluster :: Register ( r) => r. address_offset ,
273+ RegisterCluster :: Cluster ( c) => c. address_offset ,
274+ } ) ;
275+ self . insert_child ( pos, cnew) ;
276+ } else {
277+ self . add_child ( cnew) ;
278+ }
279+
248280 Ok ( ( ) )
249281 }
250282
0 commit comments