a
    –§»að7  ã                   @   sˆ   d dl Z d dlZd dlmZmZmZ d dlmZmZ d dl	m
Z
 d dlZd dlmZ G dd„ deƒZG dd„ deƒZed	kr„e ¡  dS )
é    N)ÚTestCaseÚoverride_configÚneeds_subprocess)ÚjitÚnjit)Útypesc                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚTestDebugInfozF
    These tests only checks the compiled assembly for debuginfo.
    c                 C   s   |  |¡ | |¡S ©N)ÚcompileZinspect_asm©ÚselfÚfnÚsig© r   úT/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/numba/tests/test_debuginfo.pyÚ_getasm   s    
zTestDebugInfo._getasmc                 C   s>   | j ||d�}t d|tj¡}|d u}| j||d| d� d S )N©r   z\.section.+debugzdebug info not found in:
%s)Úmsg)r   ÚreÚsearchÚIÚassertEqual)r   r   r   ÚexpectZasmÚmÚgotr   r   r   Ú_check   s    zTestDebugInfo._checkc                 C   s,   t ddd�dd„ ƒ}| j|tjfdd� d S )NTF©ÚnopythonÚdebugc                 S   s   | S r	   r   ©Úxr   r   r   Úfoo   s    z3TestDebugInfo.test_no_debuginfo_in_asm.<locals>.foo©r   r   ©r   r   r   Úint32©r   r!   r   r   r   Útest_no_debuginfo_in_asm   s    

z&TestDebugInfo.test_no_debuginfo_in_asmc                 C   s,   t ddd�dd„ ƒ}| j|tjfdd� d S )NTr   c                 S   s   | S r	   r   r   r   r   r   r!   &   s    z0TestDebugInfo.test_debuginfo_in_asm.<locals>.foor"   r#   r%   r   r   r   Útest_debuginfo_in_asm%   s    

z#TestDebugInfo.test_debuginfo_in_asmc                 C   s|   t ddƒ�^ tdd�dd„ ƒ}| j|tjfdd� tddd	�d
d„ ƒ}| j|tjfdd� W d   ƒ n1 sn0    Y  d S )NZDEBUGINFO_DEFAULTé   T)r   c                 S   s   | S r	   r   r   r   r   r   r!   /   s    z4TestDebugInfo.test_environment_override.<locals>.foor"   Fr   c                 S   s   | S r	   r   r   r   r   r   Úbar5   s    z4TestDebugInfo.test_environment_override.<locals>.bar)r   r   r   r   r$   )r   r!   r)   r   r   r   Útest_environment_override,   s    


z'TestDebugInfo.test_environment_overrideN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r&   r'   r*   r   r   r   r   r      s   r   c                   @   sx   e Zd ZdZddiZdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	e
dd„ ƒZdd„ Ze
dd„ ƒZdd„ Zdd„ Zdd„ ZdS )ÚTestDebugInfoEmissionz1 Tests that debug info is emitted correctly.
    Z	NUMBA_OPTÚ0c                 C   sB   t ddƒ�$ | |¡ | |¡W  d   ƒ S 1 s40    Y  d S )NÚOPTr   )r   r
   Úinspect_llvmr   r   r   r   Ú_get_llvmirA   s    
z!TestDebugInfoEmission._get_llvmirc                 C   s@   |   ||¡ ¡ }t d¡}g }|D ]}| |¡r"| |¡ q"|S )Nz![0-9]+ =.*)r3   Ú
splitlinesr   r
   ÚmatchÚappend)r   r   r   ZllZmeta_reÚmetadataÚliner   r   r   Ú_get_metadataF   s    

z#TestDebugInfoEmission._get_metadatac                 C   s(   | j }t| ƒj}| j|||| jd� d S )N)Ztest_moduleZ
test_classÚ	test_nameÚenvvars)r,   Útyper+   Zsubprocess_test_runnerÚ_NUMBA_OPT_0_ENV)r   r:   ZthemodZtheclsr   r   r   Ú_subprocess_test_runnerO   s    
ýz-TestDebugInfoEmission._subprocess_test_runnerc                 C   sX   t dd�dd„ ƒ}| j|dd�}|d }|  d|d d	… ¡ |  d
|¡ |  d|¡ d S )NT©r   c                   S   s   d S r	   r   r   r   r   r   r!   Y   s    z/TestDebugInfoEmission.test_DW_LANG.<locals>.foor   r   r   z!0é   z,!DICompileUnit(language: DW_LANG_C_plus_pluszproducer: "Numba")r   r9   r   ÚassertIn)r   r!   r7   ZDICompileUnitr   r   r   Útest_DW_LANGW   s    
ÿz"TestDebugInfoEmission.test_DW_LANGc           $         s¨  t ddd�dd„ ƒ}tjf}| j||d�}| j||d�}t |¡}|j|jd  j	j
‰‡fdd	„|jD ƒ}|  t|ƒd
¡ |d }dd	„ |jD ƒ}|  t|ƒd
¡ |d }	dd	„ |	jD ƒ}
dd	„ |
D ƒ}d}|  d |¡d |¡¡ tƒ }t d¡}d}|
D ]”}t|ƒ‰ | ˆ ¡}|�s:d}|  t‡ fdd	„|D ƒƒ¡ qü| ¡ }|  t|ƒd
¡ |d }t|d
d… ƒ}|dk�r‚|  ||k¡ |}| |¡ qüt |¡\}‰tƒ }t d¡}|D ]*}| |¡}|�r´| ¡ \}}|||< �q´g d¢}‡fdd	„|D ƒ}t t!|dd„ d�|ƒD ]$\}}|| }|  d|› �|¡ �qd} t | ¡}!| "¡ D ]L}"|! |"¡}|�rL| ¡ }|  t|ƒd
¡ t|d ƒ}#|  |#ˆ¡  �q¤�qL|  #d¡ dS )z: Tests that DILocation information is reasonable.
        TÚnumpy)r   Zerror_modelc                 S   s$   | d }| d }|| }t |ƒ |S )Ng®Gáz®ó?g¸…ëQ¸@)Úprint)ÚaÚbÚcÚdr   r   r   r!   g   s
    z2TestDebugInfoEmission.test_DILocation.<locals>.foor   r   c                    s   g | ]}|j ˆ kr|‘qS r   ©Úname©Ú.0r    rI   r   r   Ú
<listcomp>ˆ   ó    z9TestDebugInfoEmission.test_DILocation.<locals>.<listcomp>r(   c                 S   s   g | ]}|‘qS r   r   rK   r   r   r   rM   ‹   rN   c                 S   s   g | ]}|j d kr|‘qS )Úcall©ÚopcoderK   r   r   r   rM   �   rN   c                 S   s   g | ]
}|j ‘qS r   rP   rK   r   r   r   rM   ‘   rN   )ZfaddZfmulZfdivÚ z.*!dbg (![0-9]+).*$éÿÿÿÿ)zalloca zstore c                    s   g | ]}|ˆ v ‘qS r   r   rK   )Úinst_as_strr   r   rM   £   rN   Nz(![0-9]+) = (.*))r   r(   r@   é   c                    s   g | ]}ˆ d  | ‘qS )r@   r   rK   )Úpysrc_line_startr   r   rM   Ã   rN   c                 S   s   t | dd … ƒS ©Nr(   )Úintr   r   r   r   Ú<lambda>Æ   rN   z7TestDebugInfoEmission.test_DILocation.<locals>.<lambda>)Úkeyzline: z1.*!DILocalVariable\(name: "a",.*line: ([0-9]+),.*z%Assertion on DILocalVariable not made)$r   r   Úfloat64r9   r3   ÚllvmÚparse_assemblyÚ	overloadsÚ
signaturesÚfndescÚmangled_nameÚ	functionsr   ÚlenÚblocksÚassertGreaterÚinstructionsrA   ÚjoinÚsetr   r
   Ústrr5   Ú
assertTrueÚanyÚgroupsrX   ÚaddÚinspectÚgetsourcelinesÚdictÚzipÚsortedÚvaluesZfail)$r   r!   r   r7   Úfull_irÚmoduleÚfuncsÚfuncrd   ÚblockZinstrsZop_seqZ	op_expectZline2dbgZ
re_dbg_refÚfoundÚinstrÚmatchedÚacceptedrl   Zdbg_valZint_dbg_valÚpysrcZmetadata_definition_mapZmeta_definition_splitr8   ÚinfoÚoffsetsZ
pyln_rangeÚkÚline_noZdilocation_infoÚexprZmatch_local_var_aÚentryZdbg_liner   )rT   rJ   rV   r   Útest_DILocationd   sz    







ÿ


z%TestDebugInfoEmission.test_DILocationc                    s&  t dd�dd„ ƒ}|dƒ | |jd ¡}t |¡}|j|jd  jj‰ ‡ fdd„|jD ƒ}|  	t
|ƒd	¡ |d }d
d„ |jD ƒ}|  	t
|ƒd¡ |\}}dd„ |jD ƒ}	|	d }
|  	|
jd¡ dd„ |
jD ƒ}|  	t
|ƒd	¡ |d }t|ƒ d¡d  ¡ }|  	||j¡ |  t|
ƒ |¡¡ dS )aV   This tests that the unconditional jump emitted at the tail of
        the entry block has no debug metadata associated with it. In practice,
        if debug metadata is associated with it, it manifests as the
        prologue_end being associated with the end_sequence or similar (due to
        the way code gen works for the entry block).Tr?   c                 S   s   | d S rW   r   )rE   r   r   r   r!   â   s    zATestDebugInfoEmission.test_DILocation_entry_blk_impl.<locals>.fooé{   r   c                    s   g | ]}|j ˆ kr|‘qS r   rI   rK   rI   r   r   rM   ö   rN   zHTestDebugInfoEmission.test_DILocation_entry_blk_impl.<locals>.<listcomp>r(   c                 S   s   g | ]}|‘qS r   r   rK   r   r   r   rM   ù   rN   r@   c                 S   s   g | ]}|‘qS r   r   rK   r   r   r   rM   ÿ   rN   rS   Úbrc                 S   s   g | ]}|‘qS r   r   rK   r   r   r   rM     rN   ú:N)r   r2   r_   r\   r]   r^   r`   ra   rb   r   rc   rd   rf   rQ   Zoperandsri   ÚsplitÚstriprJ   rj   Úendswith)r   r!   rt   ru   rv   rw   rd   Zentry_blockZ
body_blockZentry_instrZujmpZujmp_operandsZtarget_dataÚtargetr   rI   r   Útest_DILocation_entry_blk_implÚ   s*    

z4TestDebugInfoEmission.test_DILocation_entry_blk_implc                 C   s   |   d¡ d S )NrŒ   ©r>   ©r   r   r   r   Útest_DILocation_entry_blk  s    	z/TestDebugInfoEmission.test_DILocation_entry_blkc                    s�   t dd�dd„ ƒ‰ t dd�‡ fdd„ƒ}tjddd… f}| j||d	�}d
}| ¡ D ]*}| ¡ }| d¡rT|  |d¡ |d7 }qT|  |d
¡ dS )zË This tests that decref's generated from `ir.Del`s as variables go
        out of scope do not have debuginfo associated with them (the location of
        `ir.Del` is an implementation detail).
        Tr?   c                  W   s   d S r	   r   r   r   r   r   Úsink  s    z?TestDebugInfoEmission.test_DILocation_decref_impl.<locals>.sinkc                    s0   | | f}| d dkr ˆ |ƒ dS |d d }|S )Nr   é   r   )rE   r    Úz©r�   r   r   r!   "  s    z>TestDebugInfoEmission.test_DILocation_decref_impl.<locals>.fooNr(   r   r   zcall void @NRT_decrefz.*meminfo\.[0-9]+\)$)	r   r   r[   r3   r4   r‰   Ú
startswithÚassertRegexre   )r   r!   r   rt   Úcountr8   Zline_strippedr   r“   r   Útest_DILocation_decref_impl  s    


z1TestDebugInfoEmission.test_DILocation_decref_implc                 C   s   |   d¡ d S )Nr—   r�   rŽ   r   r   r   Útest_DILocation_decref7  s    z,TestDebugInfoEmission.test_DILocation_decrefc                 C   s¬   t dd�dd„ ƒ}tjf}| j||d�}t |¡\}}d}t |¡}tƒ }|D ]<}	| 	|	¡}
|
rN|
 
¡ }|  t|ƒd¡ | t|d ƒ¡ qN|  t|ƒd	¡ |  ||¡ d
S )z� Tests that DILocation information for undefined vars is associated
        with the line of the function definition (so it ends up in the prologue)
        Tr?   c                 S   s$   | r| dkrd}|S |d7 }|S d S )Nr   r(   r   ©ÚnrG   r   r   r   r!   @  s    z<TestDebugInfoEmission.test_DILocation_undefined.<locals>.foor   z:.*!DILocalVariable\(name: "c\$?[0-9]?",.*line: ([0-9]+),.*r(   r   rU   N)r   r   Úintpr9   rn   ro   r   r
   rh   r5   rl   r   rc   rm   rX   rA   )r   r!   r   r7   r}   rV   r‚   ÚmatcherÚassociated_linesÚmdr5   rl   r   r   r   Útest_DILocation_undefined<  s     



z/TestDebugInfoEmission.test_DILocation_undefinedc                 C   sê   t dd�dd„ ƒ}tjf}| j||d�}t |¡\}}d}t |¡}tƒ }|D ]<}	| 	|	¡}
|
rN|
 
¡ }|  t|ƒd¡ | t|d ƒ¡ qN|  t|ƒd	¡ tƒ }t|ƒD ]\}}d
|v rª| || ¡ qª|  t|ƒd	¡ |  ||¡ dS )zi Tests that DILocation information for versions of variables matches
        up to their definition site.Tr?   c                 S   s   | r
d}nd}|S )Né   r(   r   r™   r   r   r   r!   a  s    zFTestDebugInfoEmission.test_DILocation_versioned_variables.<locals>.foor   z9.*!DILocalVariable\(name: "c\$[0-9]?",.*line: ([0-9]+),.*r(   r   r@   zc = N)r   r   r›   r9   rn   ro   r   r
   rh   r5   rl   r   rc   rm   rX   Ú	enumerate)r   r!   r   r7   r}   rV   r‚   rœ   r�   rž   r5   rl   Zpy_linesÚixZpylnr   r   r   Ú#test_DILocation_versioned_variables[  s*    


z9TestDebugInfoEmission.test_DILocation_versioned_variablesN)r+   r,   r-   r.   r=   r3   r9   r>   rB   r„   r   rŒ   r�   r—   r˜   rŸ   r£   r   r   r   r   r/   ;   s   	v
0
 r/   Ú__main__)rn   r   Znumba.tests.supportr   r   r   Znumbar   r   Z
numba.corer   ZunittestZllvmlite.bindingZbindingr\   r   r/   r+   Úmainr   r   r   r   Ú<module>   s   ,  L