o
    cZhi5                  
   @   s.  d dl Z d dlZd dlmZmZ d dlmZmZmZ G dd dZ	dee
 fddZde
fd	d
ZG dd deZG dd deZG dd dZdgZde
de
de
de
ddf
ddZedkre  Zejddddd ejdddd ejd dd!d ejd"dd#d$d e Zeejejejej dS dS )%    N)ABCabstractmethod)DictListOptionalc                   @   s   e Zd ZU eed< eed< eed< ee ed< ee ed< dededed	ee d
ee ddfddZedefddZedefddZ	edefddZ
edee fddZedee fddZdS )Symbol_name_op_name	_ret_type
_arg_names
_arg_typesnameop_nameret_type	arg_names	arg_typesreturnNc                 C   s*   || _ || _|| _t|| _t|| _dS )a#  
        A symbol is a function declaration.
        :param name: name of the symbol
        :param op_name: name of the operation
        :param ret_type: return type of the operation
        :param arg_names: names of the arguments
        :param arg_types: types of the arguments
        N)r   r	   r
   listr   r   )selfr   r   r   r   r    r   H/var/www/auris/lib/python3.10/site-packages/triton/tools/build_extern.py__init__   s
   
zSymbol.__init__c                 C      | j S Nr   r   r   r   r   r   $      zSymbol.namec                 C   r   r   )r	   r   r   r   r   r   (   r   zSymbol.op_namec                 C   r   r   )r
   r   r   r   r   r   ,   r   zSymbol.ret_typec                 C   r   r   )r   r   r   r   r   r   0   r   zSymbol.arg_namesc                 C   r   r   )r   r   r   r   r   r   4   r   zSymbol.arg_types)__name__
__module____qualname__str__annotations__r   r   propertyr   r   r   r   r   r   r   r   r   r      s:   
 
r   r   c                 C   sL   | dkrdS | dkrdS | dkrdS | dkrdS | d	krd
S | dkr$dS d S )NZi32int32u32uint32Zi64int64Zu64uint64floatZfp32doubleZfp64r   Ztype_strr   r   r   convert_type9   s   r+   c                 C   s   | dkrdS | dkrdS | S )Nr#   r%   r&   r'   r   r*   r   r   r   to_unsignedK   s
   r,   c                   @   s   e Zd ZU eed< eed< eeef ed< eed< eed< 		ddeded	ed
eddf
ddZe	defddZ
e	defddZe	deeef fddZe	defddZedddZedefddZdddZdS )ExternLibraryr   _path_symbols_format	_groupingTr   pathformatgroupingr   Nc                 C   s"   || _ || _i | _|| _|| _dS )z
        Abstract class for extern library.
        :param name: name of the library
        :param path: path of the library
        :param format: whether to format the generated stub file
        N)r   r.   r/   r0   r1   )r   r   r2   r3   r4   r   r   r   r   [   s
   
zExternLibrary.__init__c                 C   r   r   r   r   r   r   r   r   n   r   zExternLibrary.namec                 C   r   r   r.   r   r   r   r   r2   r   r   zExternLibrary.pathc                 C   r   r   )r/   r   r   r   r   symbolsv   r   zExternLibrary.symbolsc                 C   r   r   )r1   r   r   r   r   r4   z   r   zExternLibrary.groupingc                 C      d S r   r   )r   
input_filer   r   r   parse_symbols~      zExternLibrary.parse_symbolsc                 C   r7   r   r   r   r   r   r   _output_stubs   r:   zExternLibrary._output_stubsc                 C   s   |   }|d u st|dkrtd| d| j d}t|d8}|| |  | jrPtj	dddd	|gtj
d
  tj	d|gtj
d
  W d    d S W d    d S 1 s[w   Y  d S )Nr   zfile_str is empty/z.pywZautopep8z-az-rz-istdoutZisort)r;   len	Exceptionr   openwritecloser0   
subprocessPopenPIPEcommunicate)r   
output_dirfile_strZoutput_filefr   r   r   generate_stub_file   s   
"z ExternLibrary.generate_stub_file)TTr   N)r   r   r   r    r!   r   r   boolr   r"   r   r2   r6   r4   r   r9   r;   rL   r   r   r   r   r-   T   s@   
 
r-   c                       sl   e Zd ZU eeee f ed< d fddZe	de
e fddZddd	Zdd
dZdefddZ  ZS )	Libdevice_symbol_groupsr   Nc                    s   t  d| i | _d| _dS )z_
        Constructor for Libdevice.
        :param path: path of the libdevice library
        	libdeviceTN)superr   rP   is_purer   r2   	__class__r   r   r      s   
zLibdevice.__init__c                 C   s<  |  d}|d }|d }|  }|d dkrd S t|d }|d u r%d S | d}|d dd}|dd}d|v r>d S |d  d	}	g }
g }t|	D ]#\}}t|  d }|d u r` d S d
t| }|
| || qM|dkr~t|
d |
d< n|drt|}t|
D ]
\}}t||
|< qt|||||
S )N@r      Zinternal( Z__nv_Zieee,argsadu)	splitr+   replace	enumerater    appendr,   
startswithr   )lineentriesZret_strfunc_strZret_strsr   Z	func_strs	func_namer   Zarg_strsr   r   iZarg_strarg_typearg_namer   r   r   _extract_symbol   s>   



zLibdevice._extract_symbolc                 C   s`  i }| j  D ]	}|j}|||< qi ddddddddd	dd
dddddddddddddddddddddddi dd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d2d4d5d6d5d7d8d9d8d:d;d<d;i d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOddPddQdRdSdTdUdVdWdXdYdZd[d\i d]d^d_d`dadbdcdddedfdgdhdidjdkdldmdldndodpdodqdrdsdtdudvdwdxdydzd{d|i d}d~ddddddddddddddddddddddddddddddddi ddddddddddddddddddddddddddddddddddi ddddddddddddddddddddddddœddœddȓddʓdd̓ddΓi ddГddғddԓdd֓ddؓddړddܓddޓddޓddddddddddddddddi ddddddddddddddddddddddddd ddddddddd	d
dd}| j  D ](}|j}||v r|| }||_|| jv r&| j| | q|g| j|< qd S (  NZllabsabsZacosfacosZacoshfacoshZdadd_rdZadd_rdZfadd_rdZdadd_rnZadd_rnZfadd_rnZdadd_ruZadd_ruZfadd_ruZdadd_rzZadd_rzZfadd_rzZasinfasinZasinhfasinhZatanfatanZatan2fatan2ZatanhfatanhZbrevllZbrevZcbrtfZcbrtZceilfceilZclzllZclzZ	copysignfcopysignZcosfcosZcoshfcoshZcospifZcospiZcyl_bessel_i0fZcyl_bessel_i0Zcyl_bessel_i1fZcyl_bessel_i1Zfdiv_rdZdiv_rdZddiv_rdZfdiv_rnZdiv_rnZddiv_rnZfdiv_ruZdiv_ruZddiv_ruZfdiv_rzZdiv_rzZddiv_rzZerfferfZerfcferfcZerfcinvfZerfcinvZerfcxfZerfcxZerfinvfZerfinvZexpfexpZexp10fZexp10Zexp2fZexp2Zexpm1fexpm1ZfabsffabsZfast_fdividefZfast_dividefZfdimfZfdimZffsllZffsZfloorffloorZfmaffmaZfmaf_rdZfma_rdZfmaf_rnZfma_rnZfmaf_ruZfma_ruZfmaf_rzZfma_rzZfmodffmodZuhaddZhaddZhypotfhypotZilogbfZilogbZisinffisinfZisinfdZisnanfisnanZisnandZj0fZj0Zj1fZj1ZjnfZjnZldexpfldexpZlgammaflgammaZllrintfZllrintZllroundfZllroundZlogflogZlog10flog10Zlog1pflog1pZlog2flog2ZlogbflogbZumaxmaxZllmaxZullmaxZfmaxfZfmaxZuminminZllminZullminZfminfZfminZdmul_rdZmul_rdZfmul_rdZdmul_rnZmul_rnZfmul_rnZdmul_ruZmul_ruZfmul_ruZdmul_rzZmul_rzZfmul_rzZumul24Zmul24ZumulhiZmulhiZmul64hiZumul64hiZ
nearbyintfZ	nearbyintZ
nextafterf	nextafterZnorm3dfZnorm3dZnorm4dfZnorm4dZnormcdffZnormcdfZnormcdfinvfZ
normcdfinvZpopcllZpopcZpowifpowZpowiZpowfZrcbrtfZrcbrtZfrcp_rdZrcp_rdZdrcp_rdZfrcp_rnZrcp_rnZdrcp_rnZfrcp_ruZrcp_ruZdrcp_ruZfrcp_rzZrcp_rzZdrcp_rzZ
remainderf	remainderZurhaddZrhaddZrhypotfZrhypotZrintfZrintZrnorm3dfZrnorm3dZrnorm4dfZrnorm4dZroundfroundZrsqrtfZrsqrtZ	frsqrt_rnZrsqrt_rnZusadr]   ZscalbnfZscalbnZsignbitfZsignbitZsignbitdZsinfsinZsinhfsinhZsinpifZsinpiZsqrtfsqrtZfsqrt_rdZsqrt_rdZdsqrt_rdZfsqrt_rnZsqrt_rnZdsqrt_rnZfsqrt_ruZsqrt_ruZdsqrt_ruZfsqrt_rzZsqrt_rzZdsqrt_rzZfsub_rdZsub_rdZdsub_rdZfsub_rnZsub_rnZdsub_rnZfsub_ruZsub_ruZdsub_ruZfsub_rzZsub_rzZdsub_rzZtanftanZtanhftanhZtgammafZtgammaZtruncftruncZy0fZy0y1Zyn)Zy1fZynf)r/   valuesr   r	   rP   rc   )r   Z
symbol_setsymbolr   Zrenamingr   r   r   _group_symbols   s  
						





!
zLibdevice._group_symbolsc                 C   s`   t | jdkr	d S tdd|g  }|D ]}| |}|d u r#q|| j|j< q| 	  d S )Nr   ZgrepZdefine)
r@   r6   rE   check_outputdecode
splitlinesrl   r/   r   r   )r   r8   outputre   r   r   r   r   r9      s   
zLibdevice.parse_symbolsc                 C   s2  d}d}d}| j  D ]}|d7 }d|d j d}|d jD ]	}|| d7 }q|d7 }d	| j d
}|d jD ]	}|| d7 }q9|d7 }d}|D ](}	|d7 }|	jD ]
}
|d|
 d7 }qTd|	j d}|d|	j d | d 7 }qK|d7 }||7 }|d| j 7 }|d7 }||| d 7 }q|| | }|S )Nzfrom . import core
rZ   z@core.extern
zdef r   rY   z, z_builder=None):
z!	return core.extern_elementwise("z", libdevice_path(), [z], 
{zcore.dtype("z"),z")z): ("z", z),
}z
, is_pure=z, _builder=_builder)

)	rP   r   r   r   r   r   r   r   rS   )r   Z
import_strZ
header_strrg   r6   Zfunc_name_strrk   Z
return_strZarg_type_symbol_dict_strr   rj   r   rJ   r   r   r   r;     s6   
zLibdevice._output_stubsrM   )r   r   r   r   r    r   r   r!   r   staticmethodr   rl   r   r9   r;   __classcell__r   r   rU   r   rO      s   
 	
)
2rO   c                   @   s^   e Zd ZU eed< eed< dddZdeddfdd	Zedefd
dZedefddZ	dS )LLVMDisassemblerr.   _ll_filer   Nc                 C   s   || _ d| _dS )zf
        Invoke llvm-dis to disassemble the given file.
        :param path: path to llvm-dis
        z/tmp/extern_lib.llN)r.   r   rT   r   r   r   r   5  s   
zLLVMDisassembler.__init__lib_pathc                 C   s$   t j| j|d| jgt jd  d S )Nz-or>   )rE   rF   r.   ll_filerG   rH   )r   r   r   r   r   disasm=  s   $zLLVMDisassembler.disasmc                 C   r   r   )r   r   r   r   r   r   @  r   zLLVMDisassembler.ll_filec                 C   r   r   r5   r   r   r   r   r2   D  r   zLLVMDisassembler.pathrM   )
r   r   r   r    r!   r   r   r"   r   r2   r   r   r   r   r   1  s   
 
r   rQ   llvm_dis_pathr   lib_namerI   c                 C   sL   |dkr	t |}ntd| t| }|| ||j || dS )a  
      Interface function to build the library file.
      :param llvm_dis_path: path to the llvm-dis binary
      :param lib_path: path to the external library file
      :param lib_name: name of the library
      :param output_dir: path to the output directory
    rQ   zUnknown extern library: N)rO   rA   r   r   r9   r   rL   )r   r   r   rI   Z
extern_libZllvm_disassemblerr   r   r   buildL  s   

r   __main__z
--llvm-diszPath to llvm-diszllvm-dis)desthelpdefaultz
--lib-pathzPath to the extern library)r   r   z
--lib-namezName of the extern libraryz--outputzOutput file pathz/tmp/)argparserE   abcr   r   typingr   r   r   r   r    r+   r,   r-   rO   r   Zextern_libsr   r   ArgumentParserparseradd_argument
parse_argsargsr   r   r   rI   r   r   r   r   <module>   s@    2	@ 
