
    e9                     $   d Z ddlZddl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	m
Z
mZmZmZmZ ddlmZ ddlmZmZmZ ddlmZmZ  ej        e          Zi Zd	Zd
ZdZdZdZ de!fdZ"	 d4de!de!deee!                  de#def
dZ$ e"d          defd            Z% G d d          Z& e"d          d             Z'd Z(d	d
dZ)d  Z* e"d!          defd"            Z+d# Z,d$e	e!e!f         de!fd%Z-d$e	e!e!f         d&e!dee!         fd'Z.defd(Z/d)ede
e         fd*Z0d+ Z1d,ee!ee!d-f         f         fd.Z2 e2eef/          d0             Z3 e2e/          d1             Z4 e2e/          d2             Z5 e2e /          d3             Z6dS )5z#Detection of CPU microarchitectures    N)DictListOptionalSetTupleUnion   )CPUID   )TARGETSMicroarchitecturegeneric_microarchitecture)
CPUID_JSONTARGETS_JSONx86_64aarch64ppc64leppc64riscv64operating_systemc                       fd}|S )zDecorator to mark functions that are meant to return partial information on the current cpu.

    Args:
        operating_system: operating system where this function can be used.
    c                 H    t                                        |            | S N)INFO_FACTORYappend)factoryr   s    3lib/python3.11/site-packages/archspec/cpu/detect.py	decoratorzdetection.<locals>.decorator*   s"    %&--g666     )r   r   s   ` r   	detectionr!   #   s$         r    namevendorfeatures
generationreturnc                 H    t          | g ||pt                      i |          S )zTConstruct a partial microarchitecture, from information gathered during system scan.)r#   parentsr$   r%   	compilersr&   )r   set)r#   r$   r%   r&   s       r   partial_uarchr,   1   s6     "SUU   r   Linux)r   c                     i } t          d          5 }|D ]N}|                    d          \  }}}|dk    r| r n*|                                | |                                <   Oddd           n# 1 swxY w Y   t                      }|t          k    r4t          |                     dd          t          | d                    S |t          k    r-t          t          |           t          | d	                    S |t          t          fv rot          j        d
|                     dd                    }	 t          |                    d                    }n# t           $ r d}Y nw xY wt          |          S |t"          k    rG|                     d          dk    rd| d<   t          |                     dt"                              S t%          |          S )zMReturns a partial Microarchitecture, obtained from scanning ``/proc/cpuinfo``z/proc/cpuinfo:N	vendor_idgenericflagskeyr$   r%   Featuresz
POWER(\d+)cpur"   r   r   )r&   uarchzsifive,u74-mcu74mc)r#   )open	partitionstrip_machineX86_64r,   get_feature_setAARCH64_canonicalize_aarch64_vendorPPC64LEPPC64researchintgroupAttributeErrorRISCV64r   )	datafileliner4   	separatorvaluearchitecturegeneration_matchr&   s	            r   proc_cpuinforR   ?   s7    D	o		 .$ 	. 	.D$(NN3$7$7!CE CD %D. . . . . . . . . . . . . . .  ::Lv88K33l4U\>]>]>]
 
 
 	
 w/55!$J777
 
 
 	

 '''9]DHHUB4G4GHH	-33A6677JJ 	 	 	 JJJ		
 
3333w88G//#DM$((7G"<"<====$\222s$   AA00A47A4:"E E,+E,c                   2    e Zd ZdZd Zd ZdededefdZdS )	CpuidInfoCollectorz;Collects the information we need on the host CPU from cpuidc                    t                      | _         | j        j        di t          d         d         }|j        | _        t          j        d|j        |j	        |j
                                      d          | _         | j        j        di t          d         d         }|j        | _        |                                 | _        d S )Nr$   inputIIIutf-8highest_extension_supportr    )r
   cpuidregisters_forr   eaxhighest_basic_supportstructpackebxedxecxdecoder$   rY   	_featuresr%   )self	registerss     r   __init__zCpuidInfoCollector.__init__v   s    WW
,DJ,MMz(/CG/LMM	%.]"k%	y}UU\\
 
 -DJ,``z:U/VW^/_``	)2&((r   c                      t                       fd}t          d         D ]%}|d         d          j        k    r ||           &t          d         D ]%}|d         d          j        k    r ||           &S )Nc                      j         j        di | d         }| d         D ]O}t          ||d                   }                    ||d                   r                    |d                    Pd S )NrV   bitsregisterbitr#   r    )rZ   r[   getattr_is_bit_setadd)rK   rf   feature_checkcurrentresultre   s       r   check_featuresz4CpuidInfoCollector._features.<locals>.check_features   s    0
0AA4=AAI!%f 6 6!)]:-FGG##G]5-ABB 6JJ}V45556 6r   r2   rV   r\   zextension-flags)r+   r   r]   rY   )re   rs   	call_datarr   s   `  @r   rd   zCpuidInfoCollector._features   s    	6 	6 	6 	6 	6 	6 $G, 	& 	&I!%(4+EEEN9%%%%#$56 	& 	&I!%(4+IIIN9%%%%r   rk   rl   r'   c                     d|z  }||z  dk    S )Nr   r   r    )re   rk   rl   masks       r   rn   zCpuidInfoCollector._is_bit_set   s    Cx$""r   N)	__name__
__module____qualname____doc__rg   rd   rG   boolrn   r    r   r   rT   rT   r   sa        EE) ) )  ,#C #c #d # # # # # #r   rT   Windowsc                      t                      } | t          k    r)t                      }t          |j        |j                  S t          |           S )zPReturns a partial Microarchitecture, obtained from running the cpuid instructionr5   )r=   r>   rT   r,   r$   r%   r   )rP   rK   s     r   
cpuid_infor~      sH     ::Lv!##DK$-HHHH$\222r   c                     t          j        | t           j        |          5 }|                                d         }d d d            n# 1 swxY w Y   t	          |                    d                    S )N)stdoutenvr   rX   )
subprocessPopenPIPEcommunicatestrrc   )argsr   procoutputs       r   _check_outputr      s    		$zC	@	@	@ 'D!!##A&' ' ' ' ' ' ' ' ' ' ' ' ' ' 'v}}W%%&&&s   A		AA)AMD64ARM64c                  D   t          j                    } | dvrt          j                    S | dk    r.t          j                    }t                              ||          S t          g dt                                                                }d|v rt          S t          S )z)Return the machine architecture we are on)Darwinr|   r|   )sysctl-nmachdep.cpu.brand_stringr   Apple)
platformsystemmachineWINDOWS_MAPPINGr?   r   _ensure_bin_usrbin_in_pathr<   rA   r>   )r   platform_machiner   s      r   r=   r=      s    (( 444!!! 9$$#+--""#35EFFF 444:T:V:V  egg  & Mr   r   c                     t                      dt          dt          ffd} t                      t          k    r | dd                                           d | dd                                           }t          |                                          }t          d         d	                                         D ]0\  }}||v r'|	                    |                                           1t           | dd
          |          S d} | dd                                          }d|v rd}nd|v rd}nd|v rd}t          |d          S )z;Returns a raw info dictionary parsing the output of sysctl.r   r'   c                  l    t          dgt          |           z                                             S )Nr   r   )r   listr<   )r   child_environments    r   r   zsysctl_info.<locals>.sysctl   s0    hZ$t**4:KLLLRRTTTr   r   zmachdep.cpu.features zmachdep.cpu.leaf7_featuresconversionsdarwin_flagszmachdep.cpu.vendorr5   unknownr   m2m1appler   )r#   r$   )r   r   r=   r>   lowerr+   splitr   itemsupdater,   )r   r%   darwin_flag
linux_flagmodel	model_strr   s         @r   sysctl_infor      s    344Uc Uc U U U U U U zzVvd23399;; D Dvd899??AAD D 	 x~~''(( (4M'B>'R'X'X'Z'Z 	4 	4#Kh&&
 0 0 2 2333FF41E$F$FQYZZZZEt788>>@@Iy				I		eG4444r   c                  B   t          t          j                                                  } |                     dd                              t          j                  }dD ]}||vr|                    |           t          j                            |          | d<   | S )NPATHr"   )z/sbinz	/usr/sbin)	dictosenvironr   r?   r   pathsepr   join)r   search_pathsadditional_paths      r   r   r      s    RZ--//00$((44::2:FFL1 1 1,..000 "
 = =fr   rK   c                 p    d| vrdS t           d         d         }| d         }|                    ||          S )z1Adjust the vendor field to make it human-readablezCPU implementerr1   r   arm_vendors)r   r?   )rK   r   arm_codes      r   rB   rB     sB    $$y }-m<K%&H??8X...r   r4   c                 l    t          |                     |d                                                    S )Nr"   )r+   r?   r   )rK   r4   s     r   r@   r@     s*    txxR  &&(()))r   c                     t           t          j                             D ]F} 	  |             c S # t          $ r+}t	          j        t          |                     Y d}~?d}~ww xY wt          t                                S )a5  Returns a partial Microarchitecture with information on the CPU of the current host.

    This function calls all the viable factories one after the other until there's one that is
    able to produce the requested information. Falls-back to a generic microarchitecture, if none
    of the calls succeed.
    N)	r   r   r   	Exceptionwarningswarnr   r   r=   )r   excs     r   detected_infor     s       1 12 $ $	$799 	$ 	$ 	$M#c((########	$ %XZZ000s   	.
A#!AA#infoc                      t                      }t                              |d            fdt          j                    D             pt          |          gS )zReturns an unordered list of known micro-architectures that are compatible with the
    partial Microarchitecture passed as input.
    c                     dS )NFr    )xys     r   <lambda>z/compatible_microarchitectures.<locals>.<lambda>-  s     r   c                 ,    g | ]} |          |S r    r    ).0r   r   testers     r   
<listcomp>z1compatible_microarchitectures.<locals>.<listcomp>.  s(    ;;;!66$??;A;;;r   )r=   COMPATIBILITY_CHECKSr?   r   valuesr   )r   architecture_familyr   s   ` @r   compatible_microarchitecturesr   '  sh     #**!%%&9;M;MNNF;;;;;w~'';;; !"566@ r   c                      t                      } t          |           }d }d |D             }t          ||          fd|D             }|sS t          ||          S )z3Detects the host micro-architecture and returns it.c                 R    t          | j                  t          | j                  fS r   )len	ancestorsr%   )items    r   
sorting_fnzhost.<locals>.sorting_fn<  s!    4>""C$6$666r   c                 (    g | ]}|j         d k    |S )r1   )r$   )r   cs     r   r   zhost.<locals>.<listcomp>@  s$    III18y3H3H!3H3H3Hr   r3   c                      g | ]
}|k    |S r    r    )r   r   best_generics     r   r   zhost.<locals>.<listcomp>F  s#    <<<1|+;+;!+;+;+;r   )r   r   max)r   
candidatesr   generic_candidatesr   s       @r   hostr   3  s     ??D /t44J7 7 7 JIZIII)z:::L
 =<<<Z<<<J   zz****r   r   .c                 B     t           t                    r f  fd}|S )a  Decorator to register a function as a proper compatibility check.

    A compatibility check function takes a partial Microarchitecture object as a first argument,
    and an arbitrary target Microarchitecture as the second argument. It returns True if the
    target is compatible with first argument, False otherwise.

    Args:
        architecture_family: architecture family for which this test can be used
    c                 V     t                                fdD                         S )Nc                     i | ]}|S r    r    )r   familyfuncs     r   
<dictcomp>z:compatibility_check.<locals>.decorator.<locals>.<dictcomp>`  s    $T$T$TfVT$T$T$Tr   )r   r   )r   r   s   `r   r   z&compatibility_check.<locals>.decorator_  s3    ##$T$T$T$T@S$T$T$TUUUr   )
isinstancer   )r   r   s   ` r   compatibility_checkr   Q  sB     %s++ 524     r   )r   c                 r    t           t                               }||k    p||j        v o|j        | j        k    S )z8Compatibility check for PPC64 and PPC64LE architectures.)r   r=   r   r&   r   target	arch_roots      r   compatibility_check_for_powerr   f  s>    
 

#I)<yF,<</

t
./r   c                     t           t                   }||k    s	||j        v o.|j        | j        dfv o|j                            | j                  S )z-Compatibility check for x86_64 architectures.r1   )r   r>   r   r$   r%   issubsetr   s      r   compatibility_check_for_x86_64r   q  sW    
 I	9		=	V-= = 	4Mdk955	4O$$T]33r   c                 L   |j         dk    r|j        t          k    rdS t          t                   }||j        k    o|j         | j         dfv }t          j                    dk    r#t          | j                 }|o||k    p||j        v S |o|j        	                    | j                  S )z.Compatibility check for AARCH64 architectures.r1   Fr   )
r$   r#   rA   r   r   r   r   r   r%   r   )r   r   r   arch_root_and_vendorr   s        r   compatibility_check_for_aarch64r   ~  s    
 }	!!fkW&<&<u I$5 &-L ; H$$	"#V5)UFeo<UVKFO$<$<T]$K$KKr   c                 z    t           t                   }||k    s	||j        v o|j        | j        k    p
|j        dk    S )z.Compatibility check for riscv64 architectures.r1   )r   rJ   r   r#   r$   r   s      r   compatibility_check_for_riscv64r     sE      Ii@90@#@ ty >FMY$>r   )r"   r"   Nr   )7rz   collectionsr   r   rE   r^   r   r   typingr   r   r   r   r   r   vendor.cpuid.cpuidr
   microarchitecturer   r   r   schemar   r   defaultdictr   r   r   r>   rA   rC   rD   rJ   r   r!   rG   r,   rR   rT   r~   r   r   r=   r   r   rB   r@   r   r   r   r   r   r   r   r   r    r   r   <module>r      s  
 * )     				  				       : : : : : : : : : : : : : : : : & & & & & & T T T T T T T T T T , , , , , , , , '{&t,,   



     ^_ 
08S0BWZ    G$$$/3' /3 /3 /3 %$/3d*# *# *# *# *# *# *# *#Z I&&&3 3 '&3' ' '    < H%%%5& 5 5 5 &%5B  /tCH~ /# / / / /"*tCH~ *C *CH * * * *1( 1 1 1 1"	(9 	dCT>U 	 	 	 	+ + +<U3c3h3G-H    * '5)9:::/ / ;:/ 000	 	 10	 111L L 21L, 111  21  r   