
    7,Jj=                    r   d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	  ee
      j                         j                  d   Zedz  ZdZd	Zd
ZdZdZdZddZddZddZd dZd!dZd"dZd#dZd$dZ	 	 	 	 	 	 	 	 	 	 	 	 d%dZ	 	 	 	 d&	 	 	 	 	 	 	 	 	 	 	 d'dZd(dZd(dZ efd)dZ!d*dZ"e#dk(  r e"        yy)+zJSummarize duplicate save-selector field-map sets around the route blocker.    )annotationsN)Path)Any   outmap1_01amap2_02dz0:0z1:0z2:0z10:0c                p    | j                         s|S t        j                  | j                  d            S )Nutf-8encoding)existsjsonloads	read_text)pathfallbacks     C/home/exedev/hwanse/tools/summarize_save_selector_mapset_aliases.py	load_jsonr      s*    ;;=::dnngn677    c                L    | j                  d       d| j                  d       S )Ngroup:slotget)rows    r   selector_keyr      s&    gggq 122r   c                R    | D ]  }t        |      |k(  s|c S  t        d| d      )Nz	selector z
 not found)r   
ValueError)	selectorskeyr   s      r   find_selectorr#       s9     #J yZ0
11r   c                *    t        t        |             S )N)tuplesorted)mapss    r   
mapset_keyr(   '   s    r   c                    | j                  d      xs g D ci c]%  }|j                  d      r|j                  d      |' c}S c c}w )NrootsrootHexr   )secondary_fill_rootsr   s     r   fill_by_rootr-   +   sN     (++G4:779 		C  s   *Ac                    i }| j                  d      xs g D ]K  }|j                  d      }|j                  d      }|s(|s+|j                  |g       j                  |       M |S )Nsamplesselectorid)r   
setdefaultappend)sample_coverageresultsampler0   	sample_ids        r   sample_ids_by_selectorr8   3   si    #%F!%%i06B >::j)JJt$		h+229=	>
 Mr   c                    t        | D ch c]5  }|j                  d      r"|j                  d      r|j                  d      7 c}d       }t        |      D ci c]  \  }}||
 c}}S c c}w c c}}w )N	fieldMapsselectedPointerHexc                    t        | d      S )N   )int)values    r   <lambda>zaddress_order.<locals>.<lambda>B   s    UB r   r"   )r&   r   	enumerate)r!   r   r*   indexroots        r   address_orderrE   =   ss    77;CGG,@$A 	$% (	)E
 ,5U+;<KE4D%K<<
 =s   :A-A2c                T    | t         k(  ry| t        k(  ry| t        k(  ry| t        k(  ryy)Nzsource-side confirmed selectorzlogical target-side predecessorz(address-adjacent target-side predecessorzcurrent route-pair selectorzadditional map-set alias)SOURCE_SELECTORLOGICAL_PREDECESSOR_SELECTORADDRESS_PREDECESSOR_SELECTORCURRENT_SELECTOR)r0   s    r   
alias_rolerK   F   s4    ?"///0//9##,%r   c                  t        |       }| j                  d      }|j                  |      xs i }|j                  d      xs g }||t        |      ||j                  |      | j                  d      | j                  d      | j                  d      xs g |j                  dd      |r|d   xs i j                  d      nd |r|d	   xs i j                  d      nd |j                  |g       d
S )Nr;   fillsrowPointerHexselectedPointerVaHexr:   	fillCountr   vaHex)r0   selectorIndexroler+   rootAddressOrderIndexrN   selectedPointerEntryHexr:   rP   firstFillHexlastFillHexpublicSampleIds)r   r   rK   )	r   selector_index
root_orderfills_by_rootsamples_by_selectorr0   root_hex	fill_rootrM   s	            r   	alias_rowr`   R   s     C Hww+,H!!(+1rIMM'"(bE'8$!+!91#&77+A#BWW[)/R]];29>qR,,W5D9>b	R,,W5D.228R@ r   c                   |xs i }|xs i }|xs i }|xs i }t        |      }t        |      }t        |       }i }t        |       D ]I  \  }	}
|
j	                  d      xs g }|s|j                  t        |      g       j                  |	|
f       K g }|j                         D ]u  \  }}t        |      dk  r|D 	
cg c]  \  }	}
t        |
|	|||       }}	}
|j                  t        |      t        |      t        |      |t        |v t        |v d       w |j                  d        t        | t               }t        | t"              }t        | t$              }t        |j	                  d      xs g       t'        fd|D              }t)        |j	                  d      xs g       }t)        |j	                  d      xs g       }t)        |j	                  d      xs g       }|d   D 
cg c]  }
|
d	   	 }}
|d   D 
cg c]  }
|
j	                  d
      xs g D ]  }|   }}
}||t        hz  k(  }||z  }t+        ||z
        }t+        ||z
        }d}i dt        dt        dt         dt"        dt,        dt$        dt        |      dt        |      dt/        d |D              d|d|d|dt        |      d|dt        |      d|dt1        |      |||j	                  d      |j	                  d      |j	                  d       |j	                  d!      |j	                  d"      |j	                  d#      d$g d%|d&S c c}
}	w c c}
w c c}}
w )'Nr:   r   )rZ   r[   r\   r]   )mapCountr:   
aliasCountaliasescontainsSourcecontainsTargetc                    | d    | d   fS )Nrc   r:    )r   s    r   r@   z$build_alias_groups.<locals>.<lambda>   s    eL.A-A5CU,V r   rA   c              3  F   K   | ]  }t        |d          k(  s|  yw)r:   N)r%   ).0r   
target_keys     r   	<genexpr>z%build_alias_groups.<locals>.<genexpr>   s$     mU5Q\K]E^blElems   !!rd   r0   rY   a  The target-side map set for map2_02d has three selector aliases: 1:0, 10:0, and 17:0. Selector 1:0 is the logical selector-table predecessor and the only public-sample-covered alias; selector 10:0 is the address-adjacent root immediately before 2:0 and has many secondaryBranchState fills; selector 17:0 is another target-side alias without public sample coverage. The current 2:0 map set equals that target-side map set plus map1_01a, while source selector 0:0 plus the target-side set over-covers the current map set by map1_02b. This supports selector list recomposition/aliasing, but it still does not prove runtime execution order, state persistence into the 0x00542b0c reader, or a strict map1_01a source hotspot.sourcetargetsourceSelectorlogicalPredecessorSelectoraddressPredecessorSelectorcurrentSelectorfieldMapSetGroupCountduplicateMapSetGroupCountduplicateMapSetSelectorCountc              3  &   K   | ]	  }|d      yw)rc   Nrh   rj   r   s     r   rl   z%build_alias_groups.<locals>.<genexpr>   s     +^EE,,?+^s   duplicateGroupstargetAliasGrouptargetAliasSelectorstargetAliasSelectorCounttargetAliasPublicSampleIdstargetAliasPublicSampleCount"currentEqualsTargetAliasPlusSource"sourceTargetUnionOvercoversCurrent0addressPredecessorEqualsLogicalPredecessorMapSet'addressPredecessorToCurrentRootRefCount.addressPredecessorCurrentRootRefClassification addressContiguityProvesExecution"currentEqualsPredecessorPlusSourceexecutionOrderProvenblocked)zBprove which target-side alias executes before current selector 2:0z>prove secondaryBranchState persists into the 0x00542b0c readerzOfind a strict map1_01a source hotspot or equivalent original transition trigger)sourceTargetUnionExtraMapssourceTargetUnionMissingMapsr   r   r   r   2setDecompositionCurrentEqualsPredecessorPlusSource$setDecompositionExecutionOrderProvenpromotionStatusremainingProofs
conclusion)r-   r8   rE   rB   r   r2   r(   r3   itemslenr`   listSOURCETARGETsortr#   rG   rH   rJ   nextsetr&   rI   sumbool) r!   r,   r4   address_predecessor_contextset_decompositionr\   r]   r[   groupsrC   r   r'   duplicate_groupsmaps_keyrowsrd   
source_rowpredecessor_rowcurrent_rowtarget_alias_groupcurrent_mapssource_mapstarget_mapstarget_alias_selectorsr7   public_sample_ids'current_equals_target_alias_plus_sourcesource_target_unionsource_target_union_extra_maps source_target_union_missing_mapsr   rk   s                                   @r   build_alias_groupsr   n   sG    052%+O"="C)/R !56M0Ay)J<>F	* I
sww{#)rj.3::E3<HI  ,,. $t9> #	
 s $%+$7	
 	
 	Hhg,$0$0!
 	* VWy/:J#I/KLO	+;<KO//<BCJm1Amm{{39r:Ljnn[17R8Ko))+6<"=K9KI9VW#c*oWW &i0!239r  	 
 /;{fX?U.V+%3%+,?,,N%O"'-l=P.P'Q$	v !&!&! 	/! 	%&B	!
 	%&B! 	+! 	 V! 	$S)9%:! 	'+^M]+^(^! 	+! 	.! 	 6! 	#C(>$?! 	%&7! 	',=(>!  	-.U!!" 	-d3Q.R#!$ 'E(H<W<[<[  ]O  =P3N3R3RS|3}:U:Y:Y  [K  ;L,G,K,KLn,o>O>S>STx>y0A0E0EF\0]$

 !A! !_	
8 Xs    M4M
#Mc                   ddd| d    d| d    dd| d	    d
| d    ddj                  | d          dd| d    d| d    ddj                  | d         xs d dd| d    d| d    d| d    d| d    dd| d   dddd d!g}| d"   d#   D ]K  }|j                  d$|d%    d&|d'    d(|d)    d&|d*    d+|d,    d(dj                  |d-         xs d d.       M |j                  g d/       | d0   D ]t  }dj                  d1 |d#   D              }dj                  |d2   d d3       }t        |d2         d3kD  r|d4t        |d2         d3z
   z  }|j                  d5| d+| d6       v |j                  d       d7j                  |      S )8Nz# Save Selector Map-Set Aliases z
- route: `rm   z` -> `rn   `z- duplicate map-set groups: rt   " / selectors in duplicate groups: ru   z- target alias selectors: `, rz   z$- target alias public sample count: r}   z+- current equals target alias plus source: r~   z#- source+target union extra maps: `r   -z:- address predecessor equals logical predecessor map set: r   z,- address predecessor -> current root refs: r   z'- address contiguity proves execution: r   z- promotion status: `r   r   z## Target Alias GroupzC| selector | role | root | address order | fills | public samples |z'| --- | --- | --- | ---: | ---: | --- |ry   rd   z| `r0   z` | rT   z | `r+   rU   z | rP   rY   z` |)r   z## Duplicate Groupsr   z| aliases | maps |z| --- | --- |rx   c              3  .   K   | ]  }d |d    d   yw)r   r0   Nrh   rj   r   s     r   rl   zmarkdown.<locals>.<genexpr>   s     OsaJ02Os   r:      z, +z| z |
)joinr3   extendr   )summarylinesr   r   rd   r'   s         r   markdownr      sx   )

WX&'vgh.?-@B
&w/J'K&LLnov  xV  pW  oX  	Y
%dii8N0O&P%QQRS
.w7U/V.WX
5g>b6c5de
-dii@\8].^.ebe-ffgh
DWM  FA  EB  	C
6w?h7i6jk
1':\2]1^_
(9 :;1=


M1'E* )*95 
#j/"$s6{m4I7GtCPgLhKiil; TYYs3D/E%F%M#$NcS	


 
LL   *+ 0))OeI>NOOyy{+CR01u[!"R'c#eK01B6788Dr'#dV2./0 
LL99Ur   c                   dj                  d | d   d   D              }dj                  d | d   D              }dj                  ddd	d
dt        j                  | d          dt        j                  | d          dd| d    d| d    ddt        j                  dj                  | d                dd| d    dd| d    ddt        j                  dj                  | d         xs d       dd| d     dd!| d"    dd#| d$    dd%t        j                  | d&          dd'd(t        j                  | d)          d*d+d,|d-d.d/|d-g      S )0Nr   c              3    K   | ]  }d t        j                  |d          dt        j                  |d          dt        j                  t        |d                dt        j                  t        |d                dt        j                  t        |d                dt        j                  d	j                  |d
         xs d       d  yw)z<tr><td><code>r0   z</code></td><td>rT   z</td><td><code>r+   rU   	</td><td>rP   r   rY   r   z</code></td></tr>N)htmlescapestrr   r   s     r   rl   zhtml_page.<locals>.<genexpr>  s      
 	[[Z12 3{{3v;'( )[[S^!456 7{{3s#:;<=> ?{{3s;/012 3[[3/@+A!B!IcJK L	
s   CCry   rd   c           
   3     K   | ]K  }d dj                  d |d   D               dt        j                  dj                  |d                d M yw)z<tr><td>r   c              3  X   K   | ]"  }d t        j                  |d         z   dz    $ yw)z<code>r0   z</code>N)r   r   r   s     r   rl   z&html_page.<locals>.<genexpr>.<genexpr>  s(     iQTDKKJ$@@9Lis   (*rd   r   r:   z
</td></tr>Nr   r   r   rw   s     r   rl   zhtml_page.<locals>.<genexpr>  sc      
 		yyiX]^gXhiij k{{499U;%789: ;	s   AArx   zQ<!doctype html><meta charset="utf-8"><title>Save Selector Map-Set Aliases</title>z<style>body{font-family:system-ui,sans-serif;background:#111;color:#eee;max-width:1100px;margin:24px auto}table{border-collapse:collapse;width:100%}td,th{border:1px solid #444;padding:6px 8px;vertical-align:top}code{color:#9bd4ff}</style>z&<h1>Save Selector Map-Set Aliases</h1>z<ul>z<li>route: <code>rm   z</code> -&gt; <code>rn   z</code></li>z<li>duplicate map-set groups: rt   r   ru   z</li>z"<li>target alias selectors: <code>r   rz   z&<li>target alias public sample count: r}   z-<li>current equals target alias plus source: r~   z*<li>source+target union extra maps: <code>r   r   z<<li>address predecessor equals logical predecessor map set: r   z1<li>address predecessor -&gt; current root refs: r   z)<li>address contiguity proves execution: r   z<li>promotion status: <code>r   z</ul>z<p>r   z</p>z<h2>Target Alias Group</h2>z<table><thead><tr><th>selector</th><th>role</th><th>root</th><th>address order</th><th>fills</th><th>public samples</th></tr></thead><tbody>z</tbody></table>z<h2>Duplicate Groups</h2>zC<table><thead><tr><th>aliases</th><th>maps</th></tr></thead><tbody>r   )r   target_rows
group_rowss      r   	html_pager     s   )) 
 -.y9
 
K  
 ./ J 99] 	y0
DKK(9:;;OPTP[P[\cdl\mPnOoo{|
(1L)M(NNpqx  zX  rY  qZ  Z_  	`
,T[[7KaCb9c-d,eeqr
09W1X0YY^_
7@d8e7ffkl
4T[[7SoKpAqAxux5y4z  {G  	H
Fw  PB  HC  GD  DI  	J
;GDm<n;ootu
3G<^4_3``ef
&t{{7;L3M'N&O|\
dkk',/016% 	W#M1  r   c                    |j                  dd       |dz  j                  t        j                  | dd      dz   d	       |d
z  j                  t	        |       d	       y )NT)parentsexist_okz!save_selector_mapset_aliases.jsonF   )ensure_asciiindentr   r   r   !save_selector_mapset_aliases.html)mkdir
write_textr   dumpsr   )r   out_dirs     r   write_outputsr   3  sd    MM$M.22>>

7q9D@ ?  22>>y?Q\c>dr   c            
        t        j                  t              } | j                  dt        t
        dz         | j                  dt        t
        dz         | j                  dt        t
        dz         | j                  d	t        t
        d
z         | j                  dt        t
        dz         | j                  dt        t
               | j                         }t        t        |j                  g       t        |j                  i       t        |j                  i       t        |j                  i       t        |j                  i             }t        ||j                         t!        d|j                  dz          y )N)descriptionz--selectorszsave_scene_selectors.json)typedefaultz--secondary-fill-rootsz'save_selector_secondary_fill_roots.jsonz--sample-coveragezsavedata_sample_coverage.jsonz--address-contextz.save_selector_address_predecessor_context.jsonz--set-decompositionz$save_selector_set_decomposition.jsonz	--out-dirz'wrote save selector map-set aliases -> r   )argparseArgumentParser__doc__add_argumentr   OUT
parse_argsr   r   r!   r,   r4   address_contextr   r   r   print)parserargsr   s      r   mainr   <  sC   $$9F
D#@[:[\
0tSKtEtu
+$Fe@ef
+$Fv@vw
-D#HnBno
$<D $.."%$++R0$&&+$&&+$(("-G '4<<(	3DLLCf4f3g
hir   __main__)r   r   r   r   returnr   )r   dictr   r   )r!   
list[dict]r"   r   r   r   )r'   z	list[str]r   ztuple[str, ...])r,   r   r   dict[str, dict])r4   r   r   dict[str, list[str]])r!   r   r   dict[str, int])r0   r   r   r   )r   r   rZ   r>   r[   r   r\   r   r]   r   r   r   )NNNN)r!   r   r,   dict | Noner4   r   r   r   r   r   r   r   )r   r   r   r   )r   r   r   r   r   None)r   r   )$r   
__future__r   r   r   r   pathlibr   typingr   __file__resolver   ROOTr   r   r   rG   rH   rJ   rI   r   r   r#   r(   r-   r8   rE   rK   r`   r   r   r   r   r   __name__rh   r   r   <module>r      sN   P "      H~''*
Ul		$  % 832=	&	  	
 # . 
< )-#'/3%)gg%g !g "-	g
 #g 
gT)X,^ 25 ej( zF r   