o
    0 i                     @   s   d dl Z d dlZd dlZd dlZd dlmZ d dlZd dlZd dl	m
Z
mZmZmZ d dlmZ d dlmZmZ ee
jjZdd ZG dd deZG d	d
 d
eZedkr]e  dS dS )    N)typestypingcgutilscpu)global_compiler_lock)TestCaserun_in_subprocessc                 C   s   t t| S N)irConstantmachine_int)n r   d/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/numba/tests/test_cgutils.pymachine_const   s   r   c                   @   s`   e Zd Zdd Zejdd Zdd Zdd Zejdd
dZ	ejdd Z
dd Zdd ZdS )StructureTestCasec                 C   s   t  }t|| _d S r	   )r   Contextr   Z
CPUContextcontext)selfZtyping_contextr   r   r   setUp   s   zStructureTestCase.setUpc                 #   s    t ttg| }tjtjgtjf| R   jdt	|
 js,J d}t |}dg}t fdd}j|j|fV  d S )N entryTc                     sB   j  }|d  }| |j} |}||  S )Nztest_module.%s)r   codegenZcreate_libraryidZadd_ir_moduleZget_pointer_to_functionname)argsr   ZlibraryZcptrZcfuncZctypes_fntyfunctionmoduler   r   r   	call_func)   s   

z5StructureTestCase.compile_function.<locals>.call_func)r
   FunctionTyper   ctypesZ	CFUNCTYPEZc_size_tr   create_moduler   Zget_or_insert_functionr   Zis_declarationZappend_basic_blockZ	IRBuilderr   r   )r   nargsZ	llvm_fntyZentry_blockbuilderfirstr   r   r   r   compile_function   s   




z"StructureTestCase.compile_functionc                 C   s0   t |tsJ tjj}tjg|_tj|_||S r	   )	
isinstance	bytearrayr!   Z	pythonapiZPyByteArray_AsStringZ	py_objectargtypesZc_void_prestype)r   baZba_as_stringr   r   r   get_bytearray_addr5   s
   
z$StructureTestCase.get_bytearray_addrc                 C   sx   |  d\}}}}||d |d }|| W d    n1 s#w   Y  | |ddd | |ddd d S )	N   r                  )r&   addretassertEqual)r   r   r$   r   callresr   r   r   test_compile_function<   s   z'StructureTestCase.test_compile_functionr   c           
      c   s    |  d<\}}}}|||}||d t|}	||	t|j}	||||	d}||||fV  |tt	d W d    n1 sEw   Y  || 
| d S )Nr.   r   )ref)r&   r4   r   Zinttoptrr
   ZPointerType_typer5   r   r   r,   )
r   struct_classbufoffsetr   r$   r   r7   instZsptrr   r   r   run_struct_accessD   s   
	z#StructureTestCase.run_struct_accessc                 c   s    t dd }|d d  }d}| |||\}}}	}
|||
fV  W d    n1 s+w   Y  | || tj|||g|R   | || d S )N   !(      )r(   r@   ZassertNotEqualstruct	pack_intor6   )r   r<   Z
struct_fmtZstruct_argsr=   expectedr>   r   r$   r   r?   r   r   r   run_simple_struct_testQ   s   
z(StructureTestCase.run_simple_struct_testc                 C   sx   G dd dt j}d}| ||d \}}}ttdd|_ttdd|_W d    d S 1 s5w   Y  d S )	Nc                   @       e Zd ZdejfdejfgZdS )z,StructureTestCase.test_int_fields.<locals>.SabN)__name__
__module____qualname__r   Zint32Zuint16_fieldsr   r   r   r   Sb       rO   z=iH)xV4ͫ      rQ      rR   )r   	StructurerG   r
   r   IntTyperI   rJ   r   rO   fmtr   r$   r?   r   r   r   test_int_fieldsa   s   "z!StructureTestCase.test_int_fieldsc                 C   st   G dd dt j}d}| ||d\}}}tt d|_tt d|_W d    d S 1 s3w   Y  d S )Nc                   @   rH   )z.StructureTestCase.test_float_fields.<locals>.SrI   rJ   N)rK   rL   rM   r   Zfloat64Zfloat32rN   r   r   r   r   rO   m   rP   rO   z=df)Gz?=
ףp=@rZ   r[   )	r   rU   rG   r
   r   Z
DoubleTyperI   Z	FloatTyperJ   rW   r   r   r   test_float_fieldsl   s   "z#StructureTestCase.test_float_fieldsN)r   )rK   rL   rM   r   
contextlibcontextmanagerr&   r,   r9   r@   rG   rY   r\   r   r   r   r   r      s    

r   c                   @   s   e Zd ZdZdd ZdS )TestCGContextz/Tests for code generation context functionalityc                 C   s4   d}d| d}t |\}}| t||  d S )Ni@ az  if 1:
        from numba import njit, types
        from numba.extending import intrinsic

        @intrinsic
        def printf(tyctx, int_arg):
            sig = types.void(int_arg)
            def codegen(cgctx, builder, sig, llargs):
                cgctx.printf(builder, "%d\n", *llargs)
            return sig, codegen

        @njit
        def foo():
            printf(z)

        foo()
        )r   ZassertInstrdecode)r   valuecodeout_r   r   r   test_printf{   s   zTestCGContext.test_printfN)rK   rL   rM   __doc__rf   r   r   r   r   r_   x   s    r_   __main__)r]   r!   rD   sysZllvmlite.irr
   numpynpZunittestZ
numba.corer   r   r   r   Znumba.core.compiler_lockr   Znumba.tests.supportr   r   rV   ZintpZbitwidthr   r   r   r_   rK   mainr   r   r   r   <module>   s"    c