B
    ;9\zr                 @   sx  d dl Z d dlZd dlZd dlZdZd dlmZmZmZ d dl	m
Z
 ejZejd  dk rbeZeZnejZejZdddd	d
ddddZdddddddddZededededededediZejZejZeej Zddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2gZejd  d3kr2d4ed< ded4< dee< ee  Z!G d5d6 d6e"Z#d7d8 Z$d9d: Z%d;d< Z&G d=d> d>e"Z'G d?d@ d@e'Z(dAdB Z)dCdD Z*dvdEdFZ+dGdH Z,dIdJ Z-dKdL Z.dMdN Z/dOdP Z0dQdR Z1dSdT Z2dUdV Z3dWdX Z4dYdZ Z5d[d\ Z6d]d^gZ7dwd`daZ8dbi fdcddZ9dxdedfZ:dgdh Z;didj Z<dkdl Z=dydmdnZ>e
doZ?e
doZ@i aAeB ZCdzdrdsZDd{dtduZEdS )|    NF)interpreterexpressionsuse_vml)	CacheDict   boolintlongfloatdoublecomplexbytesnone)bilfdcsnr   r   r   r   r   r   r   r   )r   r   r	   r
   r   r   r   r   ZdivinvpowZsqrtZsinZcosZtanZarcsinZarccosZarctanZsinhZcoshZtanhZarcsinhZarccoshZarctanhlogZlog1pZlog10ZexpZexpm1Zabsolute	conjugateZarctan2ZfmodZceilZfloor   strc               @   sn   e Zd ZdZddddg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S )ASTNodea  Abstract Syntax Tree node.

    Members:

    astType      -- type of node (op, constant, variable, raw, or alias)
    astKind      -- the type of the result (bool, float, etc.)
    value        -- value associated with this node.
                    An opcode, numerical value, a variable name, etc.
    children     -- the children below this node
    reg          -- the register assigned to the result for this node.
    astTypeastKindvaluechildrengenericunknownN c             C   s0   t |  || _|| _|| _t|| _d | _d S )N)object__init__r   r   r    tupler!   reg)selfr   r   r    r!   r$   r$   1lib/python3.7/site-packages/numexpr/necompiler.pyr&   ^   s    

zASTNode.__init__c             C   sZ   | j dkr| j} |j dkr |j}t|ts.dS x&| jD ]}t| |t||kr6dS q6W dS )NaliasFT)r   r    
isinstancer   cmpnamesgetattr)r)   othernamer$   r$   r*   __eq__g   s    


zASTNode.__eq__c             C   sL   | j dkr:| j|jkr.t| jt|jk S | j|jk S td| j  d S )Nconstantz'Sorting not implemented for astType: %s)r   r   numpyZarrayr    	TypeError)r)   r/   r$   r$   r*   __lt__s   s
    
zASTNode.__lt__c             C   s(   | j dkr| j} t| j | j| j| jfS )Nr+   )r   r    hashr   r!   )r)   r$   r$   r*   __hash__   s    
zASTNode.__hash__c             C   s   d| j | j| j| j| jf S )NzAST(%s, %s, %s, %s, %s))r   r   r    r!   r(   )r)   r$   r$   r*   __str__   s    
zASTNode.__str__c             C   s   dt |  S )Nz<AST object at %s>)id)r)   r$   r$   r*   __repr__   s    zASTNode.__repr__c             C   s   | j | j| j| jfS )N)r   r   r    r!   )r)   r$   r$   r*   key   s    zASTNode.keyc             C   s
   t | j S )N)kind_to_typecoder   )r)   r$   r$   r*   typecode   s    zASTNode.typecodec             c   s2   x&| j D ]}x| D ]
}|V  qW qW | V  d S )N)r!   postorderWalk)r)   r   wr$   r$   r*   r>      s    zASTNode.postorderWalkc             g   s.   t |}x |  D ]}|j|kr|V  qW d S )N)setr>   r   )r)   ZastTypesr?   r$   r$   r*   allOf   s    
zASTNode.allOf)r"   r#   Nr$   )__name__
__module____qualname____doc__r-   r&   r1   r5   r7   r8   r:   r;   r=   r>   rA   r$   r$   r$   r*   r   P   s    
r   c             C   s    t | j| j| jdd | jD S )zTake an expression tree made out of expressions.ExpressionNode,
    and convert to an AST tree.

    This is necessary as ExpressionNode overrides many methods to act
    like a number.
    c             S   s   g | ]}t |qS r$   )expressionToAST).0r   r$   r$   r*   
<listcomp>   s    z#expressionToAST.<locals>.<listcomp>)r   r   r   r    r!   )exr$   r$   r*   rF      s    rF   c             c   s   d}| sdV  n| d |krf| | d }xr||d D ](}x"t| dd D ]}|| V  qNW q8W n8| d dkrx*t| dd D ]}d| V  qW n| V  dS )zSGenerate all possible signatures derived by upcasting the given
    signature.
    Zbilfdc r   N   r   )indexsigPerms)r   Zcodesstartxyr$   r$   r*   rM      s    rM   c             C   s  t | j}| jdkr^|  }ddd t | jD }xt|D ](}| jd | | d}|tj	krBP qBW xxt|D ]R}| jd | | d}|tj
krxd||  d}|tdd	tj
| g7 }P qxW td
| jd | |  xtt||D ]`\}\}}	||	krt|	 }
|| jdkr<td|
|| j||< qd}td|
||| g||< qW n| j}| j}t| j| j|dd |D S )zAssign appropiate types to each node in the AST.

    Will convert opcodes and functions to appropiate upcast version,
    and add "cast" ops if needed.
    oprJ   c             s   s   | ]}|  V  qd S )N)r=   )rG   rO   r$   r$   r*   	<genexpr>   s    z!typeCompileAst.<locals>.<genexpr>_asciizfunc_%snrawr   z&couldn't find matching opcode for '%s'r2   castc             S   s   g | ]}t |qS r$   )typeCompileAst)rG   r   r$   r$   r*   rH      s    z"typeCompileAst.<locals>.<listcomp>)listr!   r   r=   joinrM   r    encoder   opcodesZ	funccodesr   NotImplementedError	enumerateziptypecode_to_kindr   )astr!   ZretsigZbasesigZsigr    funcnamer   ZhaveZwantkindopnamer$   r$   r*   rW      s:    


rW   c               @   s*   e Zd ZdZd
ddZdd Zdd Zd	S )RegisteraM  Abstraction for a register in the VM.

    Members:
    node          -- the AST node this corresponds to
    temporary     -- True if this isn't an input or output
    immediate     -- not a register, but an immediate value
    n             -- the physical register number.
                     None if no number assigned yet.
    Fc             C   s   || _ || _d| _d | _d S )NF)node	temporary	immediater   )r)   astnoderf   r$   r$   r*   r&      s    zRegister.__init__c             C   s*   | j rd}nd}d|| jj| jj| jf S )NZ	Temporaryrd   z%s(%s, %s, %s))rf   re   r   r   r   )r)   r0   r$   r$   r*   r8      s
    
zRegister.__str__c             C   s   |   S )N)r8   )r)   r$   r$   r*   r:      s    zRegister.__repr__N)F)rB   rC   rD   rE   r&   r8   r:   r$   r$   r$   r*   rd      s   	
rd   c               @   s    e Zd ZdZdd Zdd ZdS )	ImmediatezQRepresentation of an immediate (integer) operand, instead of
    a register.
    c             C   s   t | | d| _d S )NT)rd   r&   rg   )r)   rh   r$   r$   r*   r&   	  s    zImmediate.__init__c             C   s   d| j jf S )NzImmediate(%d))re   r    )r)   r$   r$   r*   r8     s    zImmediate.__str__N)rB   rC   rD   rE   r&   r8   r$   r$   r$   r*   ri     s   ri   c       
   	   C   s  t j }zt j| |ddr.tjj}nd}t| dd|}i }xd|j	D ]Z}|dkrbd||< qL|dkrtd	||< qL|d
krd||< qL||t
}t |t| ||< qLW |t j t||}	t |	rt |	t |	}	nt|	t jstdt|	 W dt j| X |	S )z>Given a string, convert it to a tree of ExpressionNode's.
    truedivFr   z<expr>evalNoneNTrueTFalsezunsupported expression type: %s)r   Z_contextZget_current_contextZset_new_contextget
__future__divisionZcompiler_flagcompileco_namesdefault_typeZVariableNodetype_to_kindupdateZ	functionsrk   Z
isConstantZConstantNodeZgetKindr,   ZExpressionNoder4   type)
r   typescontextZold_ctxflagsr   namesr0   trI   r$   r$   r*   stringToExpression  s2    






r}   c                s   d}t  fdd|D S )N)s   sum_s   prod_s   min_s   max_c             3   s   | ]} j |V  qd S )N)r    
startswith)rG   p)r`   r$   r*   rR   7  s    zisReduction.<locals>.<genexpr>)any)r`   prefixesr$   )r`   r*   isReduction5  s    r   c                sz   i  x|  dD ]}| |j< qW t  }|rT|t|krNtd||f |}nt|}|   fdd|D }|S )z>Derive the input order of the variables in an expression.
    variablez;input names (%s) don't match those found in expression (%s)c                s   g | ]} | qS r$   r$   )rG   v)	variablesr$   r*   rH   L  s    z!getInputOrder.<locals>.<listcomp>)rA   r    r@   keys
ValueErrorrX   sort)r`   input_orderaZvariable_namesZordered_namesZordered_variablesr$   )r   r*   getInputOrder:  s    r   c             C   s@   |dkrt | S tjd dkr4t| tr4| dS t| | S )Nr
   r   r   rT   )r3   Zfloat32sysversion_infor,   r   rZ   kind_to_type)rO   rb   r$   r$   r*   convertConstantToKindP  s
    

r   c             C   s$   t | d}dd |D }||fS )z
    RAM: implemented magic method __lt__ for ASTNode to fix issues
    #88 and #209. The following test code works now, as does the test suite.
    import numexpr as ne
    a = 1 + 3j; b = 5.0
    ne.evaluate( 'a*2 + 15j - b' )
    r2   c             S   s   g | ]}t |j|jqS r$   )r   r    r   )rG   r   r$   r$   r*   rH   b  s   z getConstants.<locals>.<listcomp>)sortedrA   )r`   constants_order	constantsr$   r$   r*   getConstantsY  s    r   c                sP   i  x"t |D ]\}\}}}| |< qW  fdd| D }|  dd |D S )Nc                s   g | ]} |j  |fqS r$   )r    )rG   r   )	order_mapr$   r*   rH   k  s    z$sortNodesByOrder.<locals>.<listcomp>c             S   s   g | ]}|d  qS )rK   r$   )rG   r   r$   r$   r*   rH   m  s    )r]   r   )nodesorderr   rS   r   Z	dec_nodesr$   )r   r*   sortNodesByOrderg  s    r   c             C   sD   i }x:| D ]2}|  }||kr*|| |_q
|| |_||< q
W dS )z4Assign new registers to each of the leaf nodes.
    N)r;   r(   )inodesregisterMakerZleafRegistersre   r;   r$   r$   r*   assignLeafRegistersp  s    
r   c             C   s    x| D ]}||dd|_ qW dS )z<Assign temporary registers to each of the branch nodes.
    T)rf   N)r(   )r   r   re   r$   r$   r*   assignBranchRegisters|  s    
r   c             C   s   i }g }xH|  dD ]:}||krF|| }d|_||_d|_|| q|||< qW x(|D ] }x|jjdkrv|jj|_q^W qXW |S )z&Common subexpression elimination.
    rQ   r+   r$   )rA   r   r    r!   append)r`   seenaliasesr   targetr$   r$   r*   collapseDuplicateSubtrees  s    
r   c       
      C   s   dd |   D }tdd |D }tdd |D }|rR|d | k	rR|| g }n|}x4|D ],}x&|jD ]}|jjrh||j | qhW q\W tdd tD }x||D ]t}xB|jD ]8}|j}|jr|| }	|	| |	s||jj	 | qW ||j	 r||j	 
 }||j ||< ||_qW dS )	zSAttempt to minimize the number of temporaries needed, by
    reusing old ones.
    c             S   s   g | ]}|j jr|qS r$   )r(   rf   )rG   r   r$   r$   r*   rH     s    z1optimizeTemporariesAllocation.<locals>.<listcomp>c             s   s   | ]}|j t fV  qd S )N)r(   r@   )rG   r   r$   r$   r*   rR     s    z0optimizeTemporariesAllocation.<locals>.<genexpr>c             s   s&   | ]}|t d d |jD fV  qdS )c             s   s   | ]}|j jr|j V  qd S )N)r(   rf   )rG   r   r$   r$   r*   rR     s    z:optimizeTemporariesAllocation.<locals>.<genexpr>.<genexpr>N)r@   r!   )rG   r   r$   r$   r*   rR     s   c             S   s   g | ]}|t  fqS r$   )r@   )rG   Ztcr$   r$   r*   rH     s    N)r>   dictr!   r(   rf   addscalar_constant_kindsdiscardre   r   pop)
r`   r   Zusers_ofZ	node_regsZnodes_to_checkr   r   Zunusedr(   Zusersr$   r$   r*   optimizeTemporariesAllocation  s0    




r   c             C   s.   x t | D ]\}}|| |j_q
W |t|  S )z6Given an order of nodes, assign register numbers.
    )r]   r(   r   len)r   rN   r   re   r$   r$   r*   setOrderedRegisterNumbers  s    r   c             C   s   d}d}g }xn|   D ]b}|jdkr4|| |j}|jjrH|j|j_q|j}|jdkr|| |_|d7 }||j 7 }qW x|D ]}|jj|_qW || |fS )zsAssign register numbers for temporary registers, keeping track of
    aliases and handling immediate operands.
    r   rJ   r+   NrK   )	r>   r   r   r    r(   rg   r   re   r=   )r`   rN   r   	signaturer   re   r(   r$   r$   r*    setRegisterNumbersForTemporaries  s$    





r   c             C   s   dd |  dD S )a  Convert an AST to a three address form.

    Three address form is (op, reg1, reg2, reg3), where reg1 is the
    destination of the result of the instruction.

    I suppose this should be called three register form, but three
    address form is found in compiler theory.
    c             S   s,   g | ]$}|j |jftd d |jD  qS )c             S   s   g | ]
}|j qS r$   )r(   )rG   r   r$   r$   r*   rH     s    z8convertASTtoThreeAddrForm.<locals>.<listcomp>.<listcomp>)r    r(   r'   r!   )rG   re   r$   r$   r*   rH     s   z-convertASTtoThreeAddrForm.<locals>.<listcomp>rQ   )rA   )r`   r$   r$   r*   convertASTtoThreeAddrForm  s    	r   c                s>   dd  d fdd	fddd fd	d
| D }|S )z`Given a three address form of the program, compile it a string that
    the VM understands.
    c             S   sN   | d krdS | j dk r&td| j  n$tjd dk r>t| j S t| j gS d S )N   r   z%negative value for register number %sr   )r   r   r   r   chrr   )r(   r$   r$   r*   nToChr  s    

z$compileThreeAddrForm.<locals>.nToChrNc                s<   t tj|  d} |} |} |}|| | | S )NrT   )r   r   r[   rZ   )opcodestorea1a2ZcopZcsZca1Zca2)r   r$   r*   quadrupleToString  s
    z/compileThreeAddrForm.<locals>.quadrupleToStringc                s   xt | dk r| d7 } qW | d d \}}}} ||||}|g}| dd  } x2| r d| d d  }|| | dd  } qPW d|S )N   )N   noopr       )r   )r   r   rY   )argsr   r   r   r   r   r   )r   r$   r*   toString   s    
z&compileThreeAddrForm.<locals>.toStringr   c                s   g | ]} |qS r$   r$   )rG   r|   )r   r$   r*   rH     s    z(compileThreeAddrForm.<locals>.<listcomp>)NN)rY   )programZprog_strr$   )r   r   r   r*   compileThreeAddrForm  s
    r   )optimization)r   Zmoderate
aggressiver   )rj   )FTautor   rK   c       	      C   s   |   }i }x@tD ]8\}}}|||}||kr:|||< qtd||f qW |rftd| d  |d dkrt|d j}|dd t	j
k|d< |S )Nz'%s' must be one of %szUnknown keyword argument '%s'r   rj   r   rK   rq   )copycontext_infor   r   popitemr   	_getframe	f_globalsro   rp   rq   )	kwargsframe_depthr   ry   r0   Zalloweddefaultr    Zcaller_globalsr$   r$   r*   
getContext  s    
r   r$   c                sX  t | dd |D }t| tr,t|  |} t| }| jdkrRtdd| j|fd}t|}t	|}t
|dt t
|ddt t|dt x|D ]}|jj|_qW t||}t|\}}t|rd	|j_t| d	|j_d
}	d
|j_|	d }
t||
}t||}t||\}}t|}tdd |D }d fdd|D }|||||fS )z4Compile the expression to an intermediate form.
    c             S   s   g | ]\}}|qS r$   r$   )rG   r0   Ztype_r$   r$   r*   rH   .  s    zprecompile.<locals>.<listcomp>rQ   r   )r    r   r!   rU   r   r2   Fr   rK   c             S   s   g | ]
}|j qS r$   )r    )rG   r   r$   r$   r*   rH   Y  s    rJ   c             3   s   | ]}t  |t V  qd S )N)type_to_typecodero   rt   )rG   rO   )rx   r$   r*   rR   Z  s   zprecompile.<locals>.<genexpr>)r   r,   r   r}   rF   r   r   r   rW   r   r   rA   ri   rd   r   r    r(   r   r   r   rf   r   r   r   r   r   r'   rY   )rI   r   ry   r   r`   r   r   r   r   Zr_outputZr_inputsr_constantsr_tempsZr_endtempsigthreeAddrPrograminput_namesr$   )rx   r*   
precompile*  s>    






r   c       
      K   sH   t |dd}t| ||\}}}}}t|}	t|d|d|	||S )aY  
    Compile an expression built using E.<variable> variables to a function.

    ex can also be specified as a string "2*a+3*b".

    The order of the input variables and their types can be specified using the
    signature parameter, which is a list of (name, type) pairs.

    Returns a `NumExpr` object containing the compiled function.
    rK   )r   rT   )r   r   r   r   NumExprrZ   )
rI   r   r   ry   r   Zinputsigr   r   r   r   r$   r$   r*   r   _  s    r   c                s   i xt jD ]}|t j| < qW dt j t j  fdd}g }x~tdt jdD ]h}tjd dk r	t
 j| }n	 j| }||d}||d}||d}|||||f qhW |S )zR
    Given a NumExpr object, return a list which is the program disassembled.
    rK   c                s"  t jd dk r6t j| |  }t j|  }n j| |  } j|  }y|dd |d  }W n tk
r   d S X t jd dkrt|g}|dkrd S |dkr|dkrdS |k rd	| j|d  f 	d
S |k rd| j
|  f 	d
S d|f 	d
S n|S d S )Nr   r      _rK   r         ns   r0zr%d[%s]rT   zc%d[%s]zt%d)r   r   ordr   ro   split
IndexErrorr   r   rZ   r   )pcoffsetargrQ   code)nexr   r   rev_opcodesr$   r*   getArg  s,    


zdisassemble.<locals>.getArgr   r   r   r   )r   r[   r   r   r   ranger   r   r   ro   r   r   )r   rQ   r   sourcer   destZarg1Zarg2r$   )r   r   r   r   r*   disassembley  s     


r   c             C   s   | j j}|dkrtS |dkrH| j jdkr,tS |dkrD| j jdkrDtS tS |dkrd| j jdkr`tS tS |dkrptS |dkr|t	S t
d| j j d S )	Nr   Ziur   ur   r   Szunknown type %s)Zdtyperb   r   itemsizelong_int_r   r
   r   r   r   r0   )r   rb   r$   r$   r*   getType  s$    r   c             C   sj   t | i |}t|}t|d }ts(d}n0x.| D ]}|jdkr2|jtkr2d}P q2W d}dd |D |fS )NFrQ   Tc             S   s   g | ]
}|j qS r$   )r    )rG   r   r$   r$   r*   rH     s    z getExprNames.<locals>.<listcomp>)r}   rF   r   r   r>   r   r    vml_functions)textry   rI   r`   r   ex_uses_vmlre   r$   r$   r*   getExprNames  s    
r   c       	   	   C   s   t d}d}|dkr |j}d}zn|j}|dkr4|}|o>||k	}g }xF| D ]>}y|| }W n tk
rv   || }Y nX |t| qJW W d|r|  X |S )z%Get the arguments based on the names.r   FNT)	r   r   f_localsr   KeyErrorr   r3   Zasarrayclear)	r{   
local_dictglobal_dictZ
call_frameZclear_local_dictZframe_globals	argumentsr0   r   r$   r$   r*   getArguments  s(    


r      Ksafec          	   K   s   t | tstdt|dd}| tt| f}|tkrHt| |t|< t| \}	}
t	|	||}dd t
|	|D }|t|f }yt| }W n* tk
r   t| |f| }t|< Y nX ||||
d}t||	|dat |||S Q R X dS )	a  Evaluate a simple array expression element-wise, using the new iterator.

    ex is a string forming an expression, like "2*a+3*b". The values for "a"
    and "b" will by default be taken from the calling function's frame
    (through use of sys._getframe()). Alternatively, they can be specifed
    using the 'local_dict' or 'global_dict' arguments.

    Parameters
    ----------

    local_dict : dictionary, optional
        A dictionary that replaces the local operands in current frame.

    global_dict : dictionary, optional
        A dictionary that replaces the global operands in current frame.

    out : NumPy array, optional
        An existing array where the outcome is going to be stored.  Care is
        required so that this array has the same shape and type than the
        actual outcome of the computation.  Useful for avoiding unnecessary
        new array allocations.

    order : {'C', 'F', 'A', or 'K'}, optional
        Controls the iteration order for operands. 'C' means C order, 'F'
        means Fortran order, 'A' means 'F' order if all the arrays are
        Fortran contiguous, 'C' order otherwise, and 'K' means as close to
        the order the array elements appear in memory as possible.  For
        efficient computations, typically 'K'eep order (the default) is
        desired.

    casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional
        Controls what kind of data casting may occur when making a copy or
        buffering.  Setting this to 'unsafe' is not recommended, as it can
        adversely affect accumulations.

          * 'no' means the data types should not be cast at all.
          * 'equiv' means only byte-order changes are allowed.
          * 'safe' means only casts which can preserve values are allowed.
          * 'same_kind' means only safe casts or casts within a kind,
            like float64 to float32, are allowed.
          * 'unsafe' means any data conversions may be done.
    z#must specify expression as a stringrK   )r   c             S   s   g | ]\}}|t |fqS r$   )r   )rG   r0   r   r$   r$   r*   rH   5  s    zevaluate.<locals>.<listcomp>)outr   castingr   )rI   argnamesr   N)r,   r   r   r   r'   r   items_names_cacher   r   r^   _numexpr_cacher   r   r   _numexpr_lastevaluate_lock)rI   r   r   r   r   r   r   ry   Zexpr_keyr{   r   r   r   Znumexpr_keycompiled_exr$   r$   r*   evaluate  s(    -
r   c          	   C   s^   yt d }W n tk
r(   tdY nX t d }t|| }t d }t |||S Q R X dS )a  Re-evaluate the previous executed array expression without any check.

    This is meant for accelerating loops that are re-evaluating the same
    expression repeatedly without changing anything else than the operands.
    If unsure, use evaluate() which is safer.

    Parameters
    ----------

    local_dict : dictionary, optional
        A dictionary that replaces the local operands in current frame.

    rI   z)not a previous evaluate() execution foundr   r   N)r   r   RuntimeErrorr   r   )r   r   r   r   r   r$   r$   r*   re_evaluateE  s    
r   )N)rK   )r$   )NN)NNNr   r   )N)Frp   r   r3   Z	threadingZis_cpu_amd_intelZnumexprr   r   r   Znumexpr.utilsr   r   r   r   r   r   Zint32Zint64r_   r<   r   r
   r   r   r   ru   r   Zdefault_kindrt   r   r   rX   r   r   r%   r   rF   rM   rW   rd   ri   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   ZLockr   r   r   r$   r$   r$   r*   <module>   s   
N+$
		!+
5
4
# 
G