
    uIj>                       d Z ddlmZ ddlZddlZddlZddlZddlZddlm	Z	 ddl
mZ ej                  j                  d e e	e      j!                         j"                               ddlmZ  e	e      j!                         j(                  d   Zedz  Zd	d
dd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!dZ!d"dZ"e#dk(  r e"        yy)#zBSummarize globals used by save-selector opcode 0x24 mode dispatch.    )annotationsN)Path)Any)read_sections   outzopcode24 mode1 source bytezopcode24 mode2 source bytezcurrent runtime object indexz!runtime object/event enabled flag)
0x0059e348
0x0059e347
0x0059e33e
0x0059e34di0Y iPY c                p    | j                         s|S t        j                  | j                  d            S )Nutf-8encoding)existsjsonloads	read_text)pathfallbacks     E/home/exedev/hwanse/tools/summarize_save_selector_opcode24_globals.py	load_jsonr       s*    ;;=::dnngn677    c                `   | t        d|dz
        | }| |dz   |dz    }|j                  d      rdddS |j                  d	      rd
ddS |j                  d      rdddS |j                  d      rdddS |j                  d      rd
ddS |j                  d      rd
ddS |r|rdddS dddS )Nr               readzmov al, ds:[addr])kindinstruction   writezmov ds:[addr], al   zmov cl, byte ptr ds:[addr]   zmov dl, byte ptr ds:[addr]   zmov byte ptr ds:[addr], cl   zmov byte ptr ds:[addr], dlunknownz%unclassified direct address referenceunclassified)maxendswith)section_dataposbeforeafters       r   classify_accessr0   &   s    #aq/#.Fqr*Ew/BCCw0CDD{#/KLL{#/KLL{#0LMM{#0LMM!2YZZn==r   c                   t        j                  d|      }g }|D ]  }|j                  d      dk7  r|d   }||d   z   }| || }|j                  |      }	|	dk\  sAt	        ||	      }
|j                  d|d   |	z   d	d|d   t        d|	t        ||	      z
        z   d	|
d
   |
d   d       |j                  ||	dz         }	|	dk\  rn |S )Nz<Inamez.textrawraw_sizer   0xva08xr    r!   )refVaHexinstructionVaHex
accessKindr!   r   )structpackgetfindr0   appendr*   instruction_prefix_len)exesectionsvalueneedlerefssectionstartenddatar-   accesss              r   refs_for_valuerK   :   s   [[u%FD -;;v')gj))5~iiQh$T3/FKK !4S 9:&(QF\]acfFg@g9h)hil(m$n$Vn%m4	  ))FC!G,C Qh-  Kr   c                r     dg dfdddgffD ]%  \  }k\  st         fd|D              s#c S  y)N   )r$   r%   r&   r'   r   r   r"   c              3  4   K   | ]  }z
   |k(    y wN ).0suffixrI   r-   sizes     r   	<genexpr>z)instruction_prefix_len.<locals>.<genexpr>U   s!     U&tC$Js3v=Us   r   )any)rI   r-   suffixesrS   s   `` @r   r@   r@   P   sN    	
@A	
Wg h $;3UHUUK r   c                   t        |       }g }t        j                         D ]]  \  }}t        | |t	        |d            }|j                  ||t        |      t        d |D              t        d |D              |d       _ g }t        t        t              D ]m  }d|d}	t        | ||      }|s|j                  |	t        j                  |	d      t        |      t        d |D              t        d	 |D              |d       o t        d
 |D              }
t        d |D              }t        d |D              }t        d |D              }|D cg c]  }|j                  dd      dkD  r| }}t        d |D              }t	        dd      }|D cg c]
  }||k  s	| }}|D cg c]
  }||kD  s	| }}|rt        |      nd }|rt        |      nd }|d   dk(  xr
 |d uxr |d u}|j                  d      xs g D cg c]'  }|j                  d      dk(  r|j                  d      ) c}ddt        d   dt        d   t!        d |j                  d      xs g D              xr% t!        d |j                  d      xs g D              ddd	}d}i dd d!|j                  d"      xs i j                  d#      d$|d%dt        dd&t        dd'|d(|d)   d*|d   d+|
d   d,|d   d-|D cg c]  }d|d
 c}d.|d|dnd d/|d|dnd d0|d1|
d   dk(  xr |d   dk(  d2|d3|S c c}w c c}w c c}w c c}w c c}w )4N   c              3  J   K   | ]  }|j                  d       dk(  sd  ywr:   r   r   Nr=   rQ   refs     r   rT   z build_summary.<locals>.<genexpr>c   !     "X8MQW8W1"X   ##c              3  J   K   | ]  }|j                  d       dk(  sd  ywr:   r#   r   Nr[   r\   s     r   rT   z build_summary.<locals>.<genexpr>d   !     #Z#9NRY9YA#Zr_   )globalVaHexmeaningdirectTextRefCountdirectReadCountdirectWriteCountrE   r5   r7    c              3  J   K   | ]  }|j                  d       dk(  sd  ywrZ   r[   r\   s     r   rT   z build_summary.<locals>.<genexpr>q   r^   r_   c              3  J   K   | ]  }|j                  d       dk(  sd  ywra   r[   r\   s     r   rT   z build_summary.<locals>.<genexpr>r   rb   r_   c              3  2   K   | ]  }|d    dk(  s|  ywrc   r	   NrP   rQ   rows     r   rT   z build_summary.<locals>.<genexpr>u        KM(:l(JK   c              3  2   K   | ]  }|d    dk(  s|  yw)rc   r
   NrP   rm   s     r   rT   z build_summary.<locals>.<genexpr>v   ro   rp   c              3  2   K   | ]  }|d    dk(  s|  yw)rc   r   NrP   rm   s     r   rT   z build_summary.<locals>.<genexpr>w   s     T#]1C|1S#Trp   c              3  2   K   | ]  }|d    dk(  s|  ywrl   rP   rm   s     r   rT   z build_summary.<locals>.<genexpr>x   s     ec#mBTXdBdcerp   rg   r   c              3  \   K   | ]$  }|j                  d       rt        |d    d       & yw)rc   rX   N)r=   intrm   s     r   rT   z build_summary.<locals>.<genexpr>}   s0      $77=! 	C#$s   *,r	   rE   r:   r#   r9   
0x0040fb1cr   r
   c              3  D   K   | ]  }|j                  d       dk(    yw)r9   rv   Nr[   r\   s     r   rT   z build_summary.<locals>.<genexpr>   s'      /
 GG&'<7/
s    c              3  l   K   | ],  }|j                  d       dk(  r|j                  d      dk(   . yw)r:   r#   r9   
0x0040fb21Nr[   r\   s     r   rT   z build_summary.<locals>.<genexpr>   s8      
ww|$/ GG&'<7
s   24z0mov al, ds:[0x0059e33e]; mov ds:[0x0059e347], alzBadjacent mode2 object-index cache, not a mode1 0x0059e348 producer)	writerInstructionVaHexessourceReadInstructionVaHexsourceGlobalVaHexsourceMeaningtargetGlobalVaHextargetMeaningcopiesCurrentRuntimeObjectIndexinstructionSummarypromotionImpacta  Opcode 0x24 mode 1 reads 0x0059e348, but no direct .text write to 0x0059e348 was found. The adjacent mode 2 byte 0x0059e347 does have a direct writer at 0x0040fb1c that copies the current runtime object index 0x0059e33e. A wider 0x0059e330..0x0059e34f neighborhood scan shows that nearby object/status bytes are heavily referenced while 0x0059e348 still has only that single read and no direct writer. Direct writes are present on both sides of the mode1 source, so the scan is seeing adjacent runtime bytes but leaves 0x0059e348 as a direct-write hole. This makes 0x0059e348 an unresolved runtime/global source rather than a proven selector leaf chooser.scopez6direct .text references to opcode 0x24 runtime globalscurrentOpcode24ModecurrentBoundarystreamPlus1HexrowsneighborhoodRangeHexz..0xneighborhoodRows mode1NeighborhoodDirectReadCountrf   !mode1NeighborhoodDirectWriteCountmode1DirectWriteCountmode2DirectWriteCount#neighborhoodDirectWriteAddressHexesmode1NearestLowerDirectWriteHex mode1NearestHigherDirectWriteHex)mode1DirectWriteHoleBetweenNeighborWrites!mode1UnwrittenRuntimeGlobalSourcemode2WriterContext
conclusion)r   GLOBALSitemsrK   ru   r?   lensumrangeNEIGHBORHOOD_STARTNEIGHBORHOOD_ENDr=   nextsortedr*   minrU   )rA   handler_summaryrB   r   	value_hexrd   rE   neighborhood_rowsaddressaddress_hexmode1mode2current_objectmode1_neighborhoodrn   direct_write_rowsdirect_write_addressesmode1_addresslower_writeshigher_writesnearest_lower_writenearest_higher_writemode1_direct_write_holer]   mode2_writer_contextr   s                             r   build_summaryr   Z   s~   S!HD%mmo 		7c8SB-?@$"%d)""X"XX ##Z#Z Z
 		 +-=> 73-(c8W5  &{{;3"%d)""X"XX ##Z#Z Z"
 	 KKKEKKKETTTNe->ee(77%q)A- 	  $ $$$ 
 b)M+A]W}E\G]L],B^gP]F]W^M^/;#l+1>3}-D-.!3 	-t+	- ,  yy(.B%
ww|$/ GG&'%

 '3) .) .+. /
%))&17R/
 ,
 ,
  
yy(.B
 

 Q_).	L I 3 34E F L"QQRbc 	 	"%7$<DAQRU@V W	
 	- 	+,>?P,Q 	,-?@R-S 	 '9!: 	 '9!: 	..D0
#*b0
 	*.A.Mb$S)*SW  	+/C/Ob%c*+UY#& 	45L'( 	,U3E-F!-K-{PbcuPvz{P{)* 	2+, 	j- e ^^%
P0
s*   L:
L?!L?+
M6M,M	7Mc                "   ddd| j                  d       ddd| j                  d       dd	| j                  d
       d| j                  d       d| j                  d       d| j                  d      xs i j                  d       d| j                  d      xs i j                  d       d| j                  d       dd| j                  d       d| j                  d       ddj                  | j                  d      xs g       xs d d| j                  d       d | j                  d!       dd"| j                  d#       d$| j                  d%       dd&d'g}| j                  d(      xs g D ]  }d)j                  d* |j                  d+      xs g D              xs d}|j                  d,|j                  d-       d.|j                  d/       d0|j                  d1       d0|j                  d2       d0|j                  d3       d0| d4        |j                  g d5       | j                  d6      xs g D ]  }d)j                  d7 |j                  d+      xs g d d8 D              xs d}|j                  d,|j                  d-       d.|j                  d/      xs d d0|j                  d1       d0|j                  d2       d0|j                  d3       d0| d4        |j                  d       d9j                  |      S ):Nz## Save Selector Opcode 0x24 Globalsrh   zScope: r   .z- current opcode 0x24 mode: `r   `z- mode1 direct writes: r   z- mode2 direct writes: r   z)- mode1 unwritten runtime/global source: r   z- mode2 writer context: r   r   z- mode2 promotion impact: r   z- neighborhood range: `r   z#- mode1 neighborhood reads/writes: r    / r   z'- neighborhood direct-write addresses: , r   -z-- mode1 nearest lower/higher direct writes: `r   z` / `r   z3- mode1 direct-write hole between neighbor writes: r   z- conclusion: r   z:| global | meaning | refs | reads | writes | direct refs |(| --- | --- | ---: | ---: | ---: | --- |r   <br>c              3     K   | ];  }|j                  d        d|j                  d       d|j                  d        = ywr9    r:   r!   Nr[   r\   s     r   rT   zmarkdown.<locals>.<genexpr>   H      
 ww)*+1SWW\-B,C1SWW]E[D\]
   AArE   z| `rc   z` | rd   z | re   rf   rg   z |)rh   z## Neighborhood Direct Refsrh   z:| global | meaning | refs | reads | writes | sample refs |r   r   c              3     K   | ];  }|j                  d        d|j                  d       d|j                  d        = ywr   r[   r\   s     r   rT   zmarkdown.<locals>.<genexpr>   r   r   r   
)r=   joinr?   extend)summarylinesrn   rE   s       r   markdownr      s   -

'++g&'q)

'4I(J'K1M
!'++.E"F!GH
!'++.E"F!GH
3GKK@c4d3ef
"W[[1E%F%L"$Q$QRf$g"ij
$w{{3G'H'NB&S&STe&f$hi
!'++.D"E!FaH
-gkk:\.]-^^abibmbm  oR  cS  bT  	U
1$))GKKHm<n<trt2u2|y|1}~
7De8f7gglmtmxmx  z\  n]  m^  ^_  	`
=gkkJu>v=wx
\234
D2'E* {{6"(b 
{{ 
wwv,"
 
   	 	#''-()cggi.@-ASWWMaEbDccfww()*#cgg6H.I-J#dVSUW	

 
LL   {{-.4" 
{{ 
-2r2
 
   	 	#''-()cggi.@.GC-HCGGThLiKjjmww()*#cgg6H.I-J#dVSUW	

 
LL99Ur   c                   g }| j                  d      xs g D ]  }dj                  d |j                  d      xs g D              xs d}|j                  dt        j                  t        |j                  d                   dt        j                  t        |j                  d	                   d
|j                  d       d
|j                  d       d
|j                  d       d
| d        g }| j                  d      xs g D ]  }dj                  d |j                  d      xs g d d D              xs d}|j                  dt        j                  t        |j                  d                   dt        j                  t        |j                  d	      xs d             d
|j                  d       d
|j                  d       d
|j                  d       d
| d        dj                  ddddt        j                  t        | j                  d                   dddt        j                  t        | j                  d                   dd| j                  d       dd | j                  d!       dd"| j                  d#       dd$t        j                  t        | j                  d%      xs i j                  d&                   dd't        j                  t        | j                  d%      xs i j                  d(                   dd)t        j                  t        | j                  d*                   dd+| j                  d,       d-| j                  d.       dd/t        j                  d0j                  | j                  d1      xs g       xs d       dd2t        j                  t        | j                  d3                   d4t        j                  t        | j                  d5                   dd6| j                  d7       dd8t        j                  t        | j                  d9                   dd:d;dj                  |      d<d=d>dj                  |      d<g      S )?Nr   r   c           
   3     K   | ]N  }t        j                  |j                  d        d|j                  d       d|j                  d              P ywr   htmlescaper=   r\   s     r   rT   zhtml_page.<locals>.<genexpr>   U      
 KK377#567q9N8OqQTQXQXYfQgPhij
   AArE   r   z<tr><td><code>rc   z</code></td><td>rd   z	</td><td>re   rf   rg   z
</td></tr>r   c           
   3     K   | ]N  }t        j                  |j                  d        d|j                  d       d|j                  d              P ywr   r   r\   s     r   rT   zhtml_page.<locals>.<genexpr>  r   r   r   r   zU<!doctype html><meta charset="utf-8"><title>Save Selector Opcode 0x24 Globals</title>z<style>body{font-family:system-ui,sans-serif;background:#111;color:#eee}table{border-collapse:collapse}td,th{border:1px solid #444;padding:6px 8px;vertical-align:top}code{color:#9bd4ff}</style>z*<h1>Save Selector Opcode 0x24 Globals</h1>z
<p>Scope: r   z.</p>z<ul>z$<li>current opcode 0x24 mode: <code>r   z</code></li>z<li>mode1 direct writes: r   z</li>z<li>mode2 direct writes: r   z+<li>mode1 unwritten runtime/global source: r   z<li>mode2 writer context: r   r   z<li>mode2 promotion impact: r   z<li>neighborhood range: <code>r   z%<li>mode1 neighborhood reads/writes: r   r   r   z)<li>neighborhood direct-write addresses: r   r   z4<li>mode1 nearest lower/higher direct writes: <code>r   z</code> / <code>r   z5<li>mode1 direct-write hole between neighbor writes: r   z<li>r   z</ul>z<table><thead><tr><th>global</th><th>meaning</th><th>refs</th><th>reads</th><th>writes</th><th>direct refs</th></tr></thead><tbody>z</tbody></table>z!<h2>Neighborhood Direct Refs</h2>z<table><thead><tr><th>global</th><th>meaning</th><th>refs</th><th>reads</th><th>writes</th><th>sample refs</th></tr></thead><tbody>)r=   r   r?   r   r   str)r   bodyrn   rE   neighborhood_bodys        r   	html_pager      s   D{{6"(b 
{{ 
wwv,"
 
   	 	S)?%@AB C;;s3779#5678 977/01 277,-. /77-./ 0& 		

 {{-.4" 
{{ 
-2r2
 
   	 	  S)?%@AB C;;s3779#5#<=>? @77/01 277,-. /77-./ 0& 		

 99a 	L4
T[[W[[%9!:;<EB
.t{{3w{{K`?a;b/c.ddpq
#GKK0G$H#IO
#GKK0G$H#IO
5gkkBe6f5gglm
$T[[gkkBV6W6][]5b5bcw5x1y%z${  |A  	B
&t{{3DX8Y8_]_7d7dev7w3x'y&zz  	A
(SE[9\5])^(__kl
/<^0_/``cdkdodo  qT  eU  dV  V[  	\
3DKK		'++V{J|  KC  AC  AD  AK  HK  5L  4M  MR  	S
>t{{3w{{[|O}K~?  ?A  AQ  RV  R]  R]  ^a  bi  bm  bm  nP  bQ  ^R  RS  QT  T`  	a
?Lw@x?yy~
t{{3w{{<89:;5A 	N		$+ 	N		#$3  r   c                    |j                  dd       |dz  j                  t        j                  | dd      d       |:|j                  j                  dd       |j                  t        |       d       y y )	NT)parentsexist_ok#save_selector_opcode24_globals.jsonF),:)ensure_ascii
separatorsr   r   )mkdir
write_textr   dumpsparentr   )r   out_dirhtml_outs      r   write_outputsr   6  s{    MM$M.44@@

7:F A  dT:Ig.A r   c                    t        j                         } | j                  dt        t        dz         | j                  dt        t
               | j                  dt        d d       | j                         }t        |j                  j                         t        |j                  dz  i             }t        ||j                  |j                         t        d	|j                  d
z          y )Nz--exezHwanse2.exe)typedefaultz	--out-dirz
--html-outz!Optional HTML report output path.)r   r   helpz#save_selector_opcode24_handler.jsonz%wrote opcode 0x24 globals summary -> r   )argparseArgumentParseradd_argumentr   ROOTOUT
parse_argsr   rA   
read_bytesr   r   r   r   print)parserargsr   s      r   mainr   A  s    $$&F
dD=4HI
$<
4DghD$,,!FFKG '4<<7	1$,,Af2f1g
hir   __main__)r   r   r   r   returnr   )r,   bytesr-   ru   r   dict)rA   r   rB   
list[dict]rC   ru   r   r   )rI   r   r-   ru   r   ru   )rA   r   r   r   r   r   )r   r   r   r   rO   )r   r   r   r   r   zPath | Noner   None)r   r   )$__doc__
__future__r   r   r   r   r;   syspathlibr   typingr   r   insertr   __file__resolver   probe_exe_scene_tablesr   r   r   r   r   r   r   r   r0   rK   r@   r   r   r   r   r   __name__rP   r   r   <module>r      s    H "     
   3tH~--/667 8 0 H~''*
Ul /.05	    8>(,hV0f;|Bj zF r   