B
     \H                 @   sR  d dl mZmZmZ d dlmZ d dlmZmZ d dl	Z	d dl
Z
d dlZddlmZ d dlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ d dlmZm Z  d d	l!m"Z" d d
l#m$Z$m%Z%m&Z&m'Z' d dl(m)Z) d dl*m+Z+ d dl,m-Z- d dl.m/Z/ d dl0m1Z1 e2 Z3G dd dej4Z5e5 Z6e67d dddddddddddddd gZ8G d!d" d"ed#e8Z9ed$ddd%d&gZ:d'd( Z;de6i fd)d*Z<d+d, Z=G d-d. d.e>Z?G d/d0 d0e@ZAG d1d2 d2e>ZBG d3d4 d4e>ZCG d5d6 d6eCZDd7d8 ZEdeDfd9d:ZFd;ddeDfd<d=ZGd>d? ZHd@dA ZIdBdC ZJdDdE ZKi fdFdGZLdHdI ZMdJdK ZNdS )L    )print_functiondivisionabsolute_import)contextmanager)
namedtupledefaultdictN   )event)bytecodeinterpreterfuncdescpostproctyping	typeinferlowering
pyloweringutilsconfigerrorstypesirrewrites
transforms)cpucallconv)type_annotations)PreParforPass
ParforPassParforParforDiagnostics)InlineClosureCallPass)CompilerError)raise_on_unsupported_feature)global_compiler_lock)dead_branch_prunec               @   s8   e Zd ZddddddddddedddddddZdS )FlagsFZpython)enable_loopliftenable_pyobjectenable_pyobject_loopliftforce_pyobjectrelease_gil
no_compile	debuginfo
boundcheckforceinlineno_cpython_wrapperauto_parallelnrtno_rewriteserror_modelfastmathnoaliasN)__name__
__module____qualname__r   ZParallelOptionsZOPTIONS r9   r9   -lib/python3.7/site-packages/numba/compiler.pyr%      s    r%   r1   typing_contexttarget_contextentry_pointtyping_errortype_annotation	signature
objectmodeliftedfndesc
interpmodelibrarycall_helperenvironmentmetadatac               @   s$   e Zd ZdZdd Zedd ZdS )CompileResultr9   c             C   sF   | j  }t| j}| j}d |_|_|| j| j| j| j	| j
| j|fS )zA
        Reduce a CompileResult to picklable components.
        N)rE   Zserialize_using_object_codestrr?   rC   typemap	calltypesrG   r@   rA   rD   rB   )selflibdatatypeannrC   r9   r9   r:   _reduceQ   s    

zCompileResult._reducec
             C   sF   |  |}
||
||}| ||j|
||||	||||d d d d}|S )N)r<   r;   rE   rG   r=   rC   r?   r@   rA   rD   rB   r>   rF   rH   )codegenZunserialize_libraryget_executabler;   )clsr<   rN   rC   envr@   rA   rD   rB   rO   rE   cfunccrr9   r9   r:   _rebuild_   s"    zCompileResult._rebuildN)r6   r7   r8   	__slots__rP   classmethodrW   r9   r9   r9   r:   rI   N   s   rI   Z_CompileResult_LowerResultrU   rT   c              K   sx   t |  }t t}|| }|r(t| || }x|D ]}d | |< q6W tjdkrn| d }|d k	rn|d | d< tf | S )N)   r>   )setkeys	CR_FIELDS	NameErrorsysversion_infowith_tracebackrI   )Zkwsr]   ZfieldsetZbadnamesZmissingkerrr9   r9   r:   compile_result~   s    

re   c          	   C   sJ   ddl m} t }t|}||| t||| ||||S Q R X dS )zp
    Compile the function in an isolated environment (typing and target
    context).
    Good for testing.
    r   )
cpu_targetN)Ztargets.registryrf   r   ZContextr   Z
CPUContextZnested_contextcompile_extra)funcargsreturn_typeflagslocalsrf   	typingctx	targetctxr9   r9   r:   compile_isolated   s    
ro   c             C   sB   t j| }t|}t j|d}||}t|}|	  |S )zu
    Run the compiler frontend over the given Python function, and return
    the function's canonical Numba IR.
    )func_id)
r
   FunctionIdentityfrom_functionr   InterpreterByteCode	interpretr   PostProcessorrun)rh   rp   interpbcfunc_ir	post_procr9   r9   r:   run_frontend   s    


r|   c               @   s*   e Zd ZdZdddgZdd Zdd Zd	S )
_CompileStatusz
    Used like a C record
    fail_reasoncan_fallback
can_giveupc             C   s   d | _ || _|| _d S )N)r~   r   r   )rM   r   r   r9   r9   r:   __init__   s    z_CompileStatus.__init__c             C   s8   g }x(| j D ]}|dj|t| |d qW d|S )Nz{k}={v})rc   vz, )rX   appendformatgetattrjoin)rM   Zvalsrc   r9   r9   r:   __repr__   s    z_CompileStatus.__repr__N)r6   r7   r8   __doc__rX   r   r   r9   r9   r9   r:   r}      s   
r}   c               @   s   e Zd Zdd ZdS )_EarlyPipelineCompletionc             C   s
   || _ d S )N)result)rM   r   r9   r9   r:   r      s    z!_EarlyPipelineCompletion.__init__N)r6   r7   r8   r   r9   r9   r9   r:   r      s   r   c               @   s@   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zedd Z	dS )_PipelineManagerc             C   s   g | _ i | _d| _d S )NF)pipeline_orderpipeline_stages
_finalized)rM   r9   r9   r:   r      s    z_PipelineManager.__init__c             C   s.   | j rtd| j| g | j|< || _d S )Nz Pipelines can no longer be added)r   AssertionErrorr   r   r   Zcurrent)rM   pipeline_namer9   r9   r:   create_pipeline   s    
z _PipelineManager.create_pipelinec             C   s4   | j rtd| jd }||f}| j| | d S )NzStages can no longer be added.)r   r   r   r   r   )rM   Zstage_functionZstage_descriptionZcurrent_pipeline_nameZfunc_desc_tupler9   r9   r:   	add_stage   s    
z_PipelineManager.add_stagec             C   s
   d| _ d S )NT)r   )rM   r9   r9   r:   finalize   s    z_PipelineManager.finalizec             C   sP   dj ||d}tjsDtjrDt|dd }d}|j t	 |d}|f|_
|S )zG
        Patches the error to show the stage that it arose in.
        z{desc}
{exc})descexc
r   zCaused By:
{tb}
{newmsg})tbnewmsg)r   r   ZIS_PY3r   ZFULL_TRACEBACKS
_termcolorerrmsgsplit	traceback
format_excri   )rM   r   r   r   strippedZfmtr9   r9   r:   _patch_error   s    z_PipelineManager._patch_errorc       	      C   s   | j stdx| jD ]}t| || jd k}x| j| D ]\}}yt| |  W q< tk
rz } z|jS d }~X Y q< tk
r } z.d||f }| ||}|r|n||_	P W d d }~X Y q<X q<W d S qW t
dd S )Nz!PM must be finalized before run()r   z%Failed in %s mode pipeline (step: %s)zAll pipelines have failed)r   r   r   r	   r   r   r   BaseExceptionr   r~   r!   )	rM   statusr   Zis_final_pipelineZstageZ
stage_nameemsgZpatched_exceptionr9   r9   r:   rw      s(    

z_PipelineManager.runN)
r6   r7   r8   r   r   r   r   r   r#   rw   r9   r9   r9   r:   r      s   r   c               @   s  e Zd ZdZdd Zedd Zedd Zdd	 Zd
d Z	d_d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 Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Z d<d= Z!d>d? Z"d@dA Z#dBdC Z$dDdE Z%dFdG Z&dHdI Z'dJdK Z(dLdM Z)dNdO Z*d`dQdRZ+dadTdUZ,dbdWdXZ-dYdZ Z.d[d\ Z/d]d^ Z0dS )cBasePipelinez=
    Stores and manages states for the compiler pipeline
    c             C   s   t   |  |  || _t||| _|| _|| _|| _|| _	|| _
d | _d | _d | _d | _d | _d | _d | _d | _d | _i | _t | _| j| jd< t| j	jt jd| _d S )Nparfor_diagnostics)r   r   )r   Zreload_configZrefreshrm   _make_subtargetrn   rE   ri   rj   rk   rl   ry   rp   rz   func_ir_originalrB   lifted_fromrK   rL   r?   rH   r   r   r}   r'   ZCOMPATIBILITY_MODEr   )rM   rm   rn   rE   ri   rj   rk   rl   r9   r9   r:   r     s2    zBasePipeline.__init__c          
   c   sv   y
dV  W nf t k
rp } zH| jjs( n8tjdkr<|d}td||f tj	| j
j| j
j  W dd}~X Y nX dS )zH
        Wraps code that would signal a fallback to object mode
        N)r[   z%s: %s)r   r   r   r   	PYVERSIONrb   warningswarn_explicitr   NumbaWarningrp   filenamefirstlineno)rM   r   r   r9   r9   r:   fallback_context1  s    



zBasePipeline.fallback_contextc          
   c   sv   y
dV  W nf t k
rp } zH| jjs( n8tjdkr<|d}td||f tj	| j
j| j
j  W dd}~X Y nX dS )zM
        Wraps code that would signal a fallback to interpreter mode
        N)r[   z%s: %s)r   r   r   r   r   rb   r   r   r   r   rp   r   r   )rM   r   r   r9   r9   r:   giveup_contextF  s    



zBasePipeline.giveup_contextc             C   s    t |}tjrt|  |S )z0
        Extract bytecode from function
        )r
   rt   r   ZDUMP_BYTECODEprintdump)rM   rp   ry   r9   r9   r:   extract_bytecode[  s    
zBasePipeline.extract_bytecodec          
   C   sv   t j|| _y| | j}W n< tk
rZ } z| jjrF|   | j	S |W d d }~X Y nX || _
d| _d | _|  S )Nr9   )r
   rq   rr   rp   r   r   r   r   stage_compile_interp_moderV   ry   rB   r   _compile_bytecode)rM   rh   ry   r   r9   r9   r:   rg   e  s    zBasePipeline.compile_extrar9   Nc             C   s&   |j | _ || _|| _| | |  S )N)rp   rB   r   _set_and_check_ir_compile_ir)rM   rz   rB   r   r9   r9   r:   
compile_irv  s
    
zBasePipeline.compile_irc             C   s   t | j| j}| | dS )z>
        Analyze bytecode and translating to Numba IR
        N)translate_stagerp   ry   r   )rM   rz   r9   r9   r:   stage_analyze_bytecode~  s    z#BasePipeline.stage_analyze_bytecodec             C   s\   || _ | j j| _| js0| jjr0tjf| j | _n(t| j| jkrXt	dt| j| jf d S )NzLSignature mismatch: %d argument types given, but function takes %d arguments)
rz   	arg_countnargsri   rk   r)   r   pyobjectlen	TypeError)rM   rz   r9   r9   r:   r     s    
zBasePipeline._set_and_check_irc             C   s   t | j d S )N)ir_processing_stagerz   )rM   r9   r9   r:   stage_process_ir  s    zBasePipeline.stage_process_irc             C   s   | j  | _d S )N)rz   copyr   )rM   r9   r9   r:   stage_preserve_ir  s    zBasePipeline.stage_preserve_irc             C   s   | j  }| j  }|d |d | j js:|d tj| j| j| j| j	|d\}}|rt
jsjt
jrx|D ]}td|  qpW t| j| j|| j| j|| j	t|dd	}|S dS )z:
        Loop lifting analysis and transformation
        r&   r'   )rm   rn   rl   rk   zLifting loopN)rB   r   )rk   r   Zunsetr(   r   Zloop_liftingrz   rm   rn   rl   r   ZDEBUG_FRONTENDDEBUGr   Zget_source_locationr   ri   rj   tuple)rM   Z
loop_flagsZouter_flagsmainZloopsZloopcresr9   r9   r:   frontend_looplift  s(    





zBasePipeline.frontend_loopliftc             C   sb   t j| j| j| j| j| jd\}}|r^t| j| j|| j| j	| j| jt
|dt| d
}t|dS )z'
        Extract with-contexts
        )rz   rm   rn   rk   rl   N)rB   r   pipeline_class)r   Zwith_liftingrz   rm   rn   rk   rl   r   ri   rj   r   typer   )rM   r   Zwithsr   r9   r9   r:   stage_frontend_withlift  s    z$BasePipeline.stage_frontend_withliftc             C   s`   | j p
| j| _| jjr8| jr t|  }|dk	r8t|tdd | _	tdd | _
tj| _dS )zM
        Front-end: Analyze bytecode, generate Numba IR, infer types
        Nc               S   s   t jS )N)r   r   r9   r9   r9   r:   <lambda>  s    z8BasePipeline.stage_objectmode_frontend.<locals>.<lambda>c               S   s   t jS )N)r   r   r9   r9   r9   r:   r     s    )r   rz   rk   r&   rB   r   r   r   r   rK   rL   r   r   rj   )rM   r   r9   r9   r:   stage_objectmode_frontend  s    
z&BasePipeline.stage_objectmode_frontendc          	   C   sz   | j s
td| jjf }| | t| j | j W dQ R X tjsHtj	rvt
ddd t
| j   t
ddd dS )z
        This prunes dead branches, a dead branch is one which is derivable as
        not taken at compile time purely based on const/literal evaluation.
        zhInternal error in pre-inference dead branch pruning pass encountered during compilation of function "%s"NZbranch_pruned_irP   -zend branch_pruned_ir)rz   r   rp   	func_namer   r$   ri   r   r   DUMP_IRr   centerr   )rM   r   r9   r9   r:   stage_dead_branch_prune  s    
z$BasePipeline.stage_dead_branch_prunec          	   C   s   |  d| jjf 8 t| j| j| j| j| j\}}}|| _	|| _|| _
W dQ R X |  d| jjf  t| j| j| j W dQ R X dS )z1
        Type inference and legalization
        z#Function "%s" failed type inferenceNz%Function "%s" has invalid return type)r   rp   r   type_inference_stagerm   rz   ri   rj   rl   rK   rL   legalize_return_typern   )rM   rK   rj   rL   r9   r9   r:   stage_nopython_frontend  s    
z$BasePipeline.stage_nopython_frontendc          	   C   sD   | j s
td| jjf }| | tjd| | j  W dQ R X dS )za
        Perform any intermediate representation rewrites before type
        inference.
        z^Internal error in pre-inference rewriting pass encountered during compilation of function "%s"zbefore-inferenceN)rz   r   rp   r   r   r   rewrite_registryapply)rM   r   r9   r9   r:   stage_generic_rewrites  s    
z#BasePipeline.stage_generic_rewritesc          	   C   sp   | j s
ttt| ddts ttt| ddts6td| jjf }| | tj	
d| | j  W dQ R X dS )z`
        Perform any intermediate representation rewrites after type
        inference.
        rK   NrL   z_Internal error in post-inference rewriting pass encountered during compilation of function "%s"zafter-inference)rz   r   
isinstancer   dictrp   r   r   r   r   r   )rM   r   r9   r9   r:   stage_nopython_rewrites  s    
z$BasePipeline.stage_nopython_rewritesc             C   s<   | j s
tt| j | jj| jj| j| jj| j	j
}|  dS )z?
        Preprocessing for data-parallel computations.
        N)rz   r   r   r?   rK   rL   rm   rk   r0   r   replaced_fnsrw   )rM   Zpreparfor_passr9   r9   r:   stage_pre_parfor_pass  s    


z"BasePipeline.stage_pre_parfor_passc          	   C   s   | j s
tt| j | jj| jj| j| j| jj	| j| j
}|  tjrd}x8| j j D ](}x |jD ]}t|trbd}P qbW qVP qVW |s| j jjdksd}t|tj| jj| jj dS )zF
        Convert data-parallel computations into Parfor nodes
        FTz<string>zVparallel=True was specified but no transformation for parallel execution was possible.N)rz   r   r   r?   rK   rL   rj   rm   rk   r0   r   rw   r   ZWARNINGSblocksvaluesbodyr   r   Zlocr   r   r   r   r   rp   r   )rM   Zparfor_passZ
has_parforblkZstmntr   r9   r9   r:   stage_parfor_pass+  s,    

zBasePipeline.stage_parfor_passc             C   st   | j s
tt| j | jj| jj}|  t	| j }|  t
jsHt
jrp| j jj}td| dd | j   dS )z;
        Inline calls to locally defined closures.
        zIR DUMP: %sr   r   N)rz   r   r    rk   r0   r   r   rw   r   rv   r   r   r   rp   func_qualnamer   r   r   )rM   Zinline_passr{   namer9   r9   r:   stage_inline_passN  s    


zBasePipeline.stage_inline_passc          
   C   s   t j| j| j| j| j| j| j| jt	j
d| _t	jrTtddd t| j td t	j
r~tt	j
d}| j| W dQ R X dS )z=
        Create type annotation after type inference
        )rz   rK   rL   rB   r   ri   rj   Zhtml_outputZ
ANNOTATIONr   r   zP================================================================================wN)r   ZTypeAnnotationrz   rK   rL   rB   r   ri   rj   r   ZHTMLr?   ZANNOTATEr   r   openZhtml_annotate)rM   Zfoutr9   r9   r:   stage_annotate_typea  s     
z BasePipeline.stage_annotate_typec             C   s6   | j jjr2tjr2| jd k	r*| jtj ntdd S )NzDiagnostics failed.)rk   r0   enabledr   ZPARALLEL_DIAGNOSTICSr   r   RuntimeError)rM   r9   r9   r:   stage_dump_diagnosticsw  s
    

z#BasePipeline.stage_dump_diagnosticsc          	   C   sj   |  d| jjf L t| j| jkrJt| jtjf| jt| j   | _t	| j
| j| j| jS Q R X dS )z)
        Object mode compilation
        z*Function %s failed at object mode loweringN)r   rp   r   r   ri   r   r   r   r   py_lowering_stagern   rE   rz   rk   )rM   r9   r9   r:   backend_object_mode  s    z BasePipeline.backend_object_modec          
   C   sJ   d| j jf }| |( t| j| j| j| j| j| j	| j
| jS Q R X dS )zNative mode compilationz,Function %s failed at nopython mode loweringN)rp   r   r   native_lowering_stagern   rE   rz   rK   rj   rL   rk   rH   )rM   r   r9   r9   r:   backend_nopython_mode  s    z"BasePipeline.backend_nopython_modec             C   s   | j dkr.| j }|| jj| _ | j   | }tj| j	f| j
 }t| j| j|j| jj| j| j |j||d| j|j|j| jd| _dS )zS
        Back-end: Generate LLVM IR from Numba IR, compile to machine code
        NF)r;   r<   r=   r>   r?   rE   rF   r@   rA   rD   rB   rC   rG   rH   )rE   rn   rQ   Zcreate_libraryrp   r   Zenable_object_cachingr   r@   rj   ri   re   rm   rU   r   r~   r?   rF   rB   rC   rT   rH   rV   )rM   lowerfnrA   rQ   Zloweredr@   r9   r9   r:   _backend  s*    


zBasePipeline._backendc             C   s   | j }| j|dd | jjst| jdkr:d| jjf }nd| jjf }t	|t
j| jj| jj | jjrd}t	|t
j| jj| jj dS )z*
        Lowering for object mode
        T)rA   r   zVFunction "%s" was compiled in object mode without forceobj=True, but has lifted loops.z@Function "%s" was compiled in object mode without forceobj=True.zNCode running in object mode won't allow parallel execution despite nogil=True.N)r   r   rk   r)   r   rB   rp   r   r   r   r   r   r   r   r*   )rM   r   Zwarn_msgr9   r9   r:   stage_objectmode_backend  s     


z%BasePipeline.stage_objectmode_backendc             C   s   | j }| j|dd dS )z*
        Do lowering for nopython
        F)rA   N)r   r   )rM   r   r9   r9   r:   stage_nopython_backend  s    z#BasePipeline.stage_nopython_backendc             C   sR   t jgt| j }tjt jf| }t| j| j| j	j
| jjd|ddddd
| _dS )zC
        Just create a compile result for interpreter mode
        z<Interpreter mode function>FTr9   N)
r;   r<   r=   r>   r?   r@   rA   rD   rB   rC   )r   r   r   ri   r   r@   re   rm   rn   rp   rh   r   r~   rV   )rM   ri   r@   r9   r9   r:   r     s    z&BasePipeline.stage_compile_interp_modec             C   s   t | j d S )N)r"   rz   )rM   r9   r9   r:   stage_ir_legalization  s    z"BasePipeline.stage_ir_legalizationc             C   s   dS )zD
        Cleanup intermediate results to release resources.
        Nr9   )rM   r9   r9   r:   stage_cleanup  s    zBasePipeline.stage_cleanupc             C   s
   t  dS )z?Child classes override this to customize the pipeline.
        N)NotImplementedError)rM   pmr9   r9   r:   define_pipelines  s    zBasePipeline.define_pipelinesc             C   s*   | j dkr|| jd || jd dS )z`Add the preprocessing stage that analyzes the bytecode to prepare
        the Numba IR.
        Nzanalyzing bytecodezprocessing IR)rz   r   r   r   )rM   r   r9   r9   r:   add_preprocessing_stage  s    
z$BasePipeline.add_preprocessing_stagec             C   sL   | j js:| jjr|| jd || jd || jd || jd dS )zwAdd any stages that go before type-inference.
        The current stages contain type-agnostic rewrite passes.
        zpreserve IR for fallbackznopython rewriteszdead branch pruningz(inline calls to locally defined closuresN)	rk   r2   r   r   r   r   r   r   r   )rM   r   r9   r9   r:   add_pre_typing_stage  s    z!BasePipeline.add_pre_typing_stagec             C   s    | | jd | | jd dS )zBAdd the type-inference stage necessary for nopython mode.
        znopython frontendzannotate typeN)r   r   r   )rM   r   r9   r9   r:   add_typing_stage  s    zBasePipeline.add_typing_stagec             C   sJ   | j jjr|| jd | j js.|| jd | j jjrF|| jd dS )z!Add optimization stages.
        zPreprocessing for parforsznopython rewriteszconvert to parforsN)rk   r0   r   r   r   r2   r   r   )rM   r   r9   r9   r:   add_optimization_stage  s    

z#BasePipeline.add_optimization_stagec             C   s   | | jd dS )zCAdd the lowering (code-generation) stage for nopython-mode
        znopython mode backendN)r   r   )rM   r   r9   r9   r:   add_lowering_stage!  s    zBasePipeline.add_lowering_stagec             C   s   | | jd dS )z?Add the clean-up stage to remove intermediate results.
        zcleanup intermediate resultsN)r   r   )rM   r   r9   r9   r:   add_cleanup_stage&  s    zBasePipeline.add_cleanup_stagec             C   s   | | jd d S )NzHandle with contexts)r   r   )rM   r   r9   r9   r:   add_with_handling_stage+  s    z$BasePipeline.add_with_handling_stagenopythonc             C   sp   | | | | | | | | | | | | || jd | | || j	d | 
| dS )z?Add the nopython-mode pipeline to the pipeline manager
        z$ensure IR is legal prior to loweringzdump diagnosticsN)r   r   r  r   r   r   r   r   r   r   r   )rM   r   r   r9   r9   r:   define_nopython_pipeline.  s    






z%BasePipeline.define_nopython_pipelineobjectc             C   sh   | | | | || jd || jd || jd || jd || jd | | dS )z=Add the object-mode pipeline to the pipeline manager
        zobject mode frontendz(inline calls to locally defined closureszannotate typez$ensure IR is legal prior to loweringzobject mode backendN)	r   r   r   r   r   r   r   r   r   )rM   r   r   r9   r9   r:   define_objectmode_pipeline=  s    

z'BasePipeline.define_objectmode_pipelinerx   c             C   s&   | | || jd | | dS )zMAdd the interpreted-mode (fallback) pipeline to the pipeline manager
        zcompiling with interpreter modeN)r   r   r   r   )rM   r   r   r9   r9   r:   define_interpreted_pipelineL  s    
z(BasePipeline.define_interpreted_pipelinec             C   sH   t  }| | |  || j}|dk	r0|S | jdk	s>t| jS dS )z4
        Populate and run compiler pipeline
        N)r   r   r   rw   r   rV   r   )rM   r   Zresr9   r9   r:   _compile_coreT  s    
zBasePipeline._compile_corec             C   s   | j dkst|  S )z>
        Populate and run pipeline for bytecode input
        N)rz   r   r  )rM   r9   r9   r:   r   c  s    zBasePipeline._compile_bytecodec             C   s   | j dk	st|  S )z8
        Populate and run pipeline for IR input
        N)rz   r   r  )rM   r9   r9   r:   r   j  s    zBasePipeline._compile_ir)r9   N)r  )r  )rx   )1r6   r7   r8   r   r   r   r   r   r   rg   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r   r   r9   r9   r9   r:   r   	  sZ   $

#


r   c               @   s   e Zd ZdZdd ZdS )Pipelinez"The default compiler pipeline
    c             C   sB   | j js| | | jjs"| j jr,| | | jjr>| | d S )N)rk   r)   r  r   r   r  r   r  )rM   r   r9   r9   r:   r   u  s    

zPipeline.define_pipelinesN)r6   r7   r8   r   r   r9   r9   r9   r:   r  r  s   r  c             C   sn   i }|j rd|d< |jr d|d< |jr.d|d< |jr>|j|d< |jrLd|d< t|j| }||d< | jf |S )zL
    Make a new target context from the given target context and flags.
    TZenable_debuginfoZenable_boundcheck
enable_nrtr0   Zenable_fastmathr3   )	r,   r-   r1   r0   r4   r   Zcreate_error_modelr3   Z	subtarget)rn   rk   Zsubtargetoptionsr3   r9   r9   r:   r   ~  s    
r   c	       
      C   s   || ||||||}	|	 |S )aD  Compiler entry point

    Parameter
    ---------
    typingctx :
        typing context
    targetctx :
        target context
    func : function
        the python function to be compiled
    args : tuple, list
        argument types
    return_type :
        Use ``None`` to indicate void return
    flags : numba.compiler.Flags
        compiler flags
    library : numba.codegen.CodeLibrary
        Used to store the compiled code.
        If it is ``None``, a new CodeLibrary is used.
    pipeline_class : type like numba.compiler.BasePipeline
        compiler pipeline
    )rg   )
rm   rn   rh   ri   rj   rk   rl   rE   r   pipeliner9   r9   r:   rg     s    rg   r9   c             C   s$   |
| ||	||||}|j |||dS )zK
    Compile a function with the given IR.

    For internal use only.
    )rz   rB   r   )r   )rm   rn   rz   ri   rj   rk   rl   rB   r   rE   r   r
  r9   r9   r:   r     s    	r   c       	      C   s   t | ||||||}||S )z 
    For internal use only.
    )r  rg   )	rm   rn   rE   rh   ri   rj   rk   rl   r
  r9   r9   r:   compile_internal  s    r  c             C   s*  |j st| tjrg }i }t }x|j D ]\}}x||jD ]r}t|tj	r\|
|jj q<t|tjr<t|jtjr|jjdkr|j||jj< q<t|jtjr<||jj q<W q,W |stdxb|D ]*}	||	}
|
dks|
jj|krtdqW n.t| tjst| tjr&d}t|| dS )z
    Only accept array return type iff it is passed into the function.
    Reject function object return types if in nopython mode.
    castzNo return statements?NzCOnly accept returning of array passed into the function as argumentz2Can't return function object ({}) in nopython mode)r	  r   r   ZArrayr\   r   itemsr   r   ZReturnr   valuer   ZAssignZExproptargetZArgaddr   getr   ZFunctionZPhantomr   )rj   rx   rn   ZretstmtsZ	caststmtsZargvarsZbidr   Zinstvarr  r   r9   r9   r:   r     s.    

r   c             C   s   t | }||S )N)r   rs   ru   )rp   r
   rx   r9   r9   r:   r     s    
r   c             C   sh   t | }|  tjstjrd| jj}td| 	dd | 
  | jrdtd| 	dd |   | S )NzIR DUMP: %sr   r   zGENERATOR INFO: %s)r   rv   rw   r   r   r   rp   r   r   r   r   Zis_generatorZdump_generator_info)rz   r{   r   r9   r9   r:   r     s    
r   c          	   C   s   t ||jkrtdttj}t| ||}| j	||j
| x.tt|j|D ]\}\}}	||||	 qVW |d k	r|| x | D ]\}
}||
| qW |  |  | \}}}W d Q R X |  |||fS )Nz!Mismatch number of argument types)r   r   r   r   ZWarningsFixerr   r   ZTypeInfererZ	callstackregisterrp   	enumeratezipZ	arg_namesZseed_argumentZseed_returnr  Z	seed_typeZbuild_constraintZ	propagateZunifyflush)rm   rx   ri   rj   rl   r   Zinferindexr   Ztyrc   r   rK   restyperL   r9   r9   r:   r      s     
r   c          	   C   s   t jj||||| j|j|jd}| |B tj| ||||d}	|		  |j
sZ|	|j |	j}
|	j}~	W d Q R X |jrt||d |
dS | |||
}| |||g t||||
dS d S )N)manglerZinliner5   )rH   )rU   rT   )r   PythonFunctionDescriptorZfrom_specialized_functionr  r.   r5   push_code_libraryr   ZLowerlowerr/   create_cpython_wrapperr*   rT   rF   r+   rZ   rR   Zinsert_user_function)rn   rE   rx   rK   r  rL   rk   rH   rC   r  rT   rF   rU   r9   r9   r:   r     s"    r   c       	   	   C   s   t j|}| |: t| |||}|  |js>|  |j	}|j
}~W d Q R X |jrlt||d |dS | |||}t||||dS d S )N)rU   rT   )r   r  Zfrom_object_mode_functionr  r   ZPyLowerr  r/   r  rT   rF   r+   rZ   rR   )	rn   rE   rx   rk   rC   r  rT   rF   rU   r9   r9   r:   r   9  s    r   )OZ
__future__r   r   r   
contextlibr   collectionsr   r   r`   r   r   tracingr	   Znumbar
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Znumba.targetsr   r   Znumba.annotationsr   Znumba.parforr   r   r   r   Znumba.inline_closurecallr    Znumba.errorsr!   Znumba.ir_utilsr"   Znumba.compiler_lockr#   Znumba.analysisr$   Z	termcolorr   ZConfigOptionsr%   ZDEFAULT_FLAGSr\   r^   rI   rZ   re   ro   r|   r  r}   	Exceptionr   r   r   r  r   rg   r   r  r   r   r   r   r   r   r9   r9   r9   r:   <module>   s~   D 
(D    m
#