a
    Ö³aÅ<  ã                   @   s  d Z dZdZdZdZdZdZdZdZd	Z	d
Z
dZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZd Z d!Z!d"Z"d#Z#d$Z$d%Z%G d&d'„ d'ƒZ&d(d)„ Z'd*d+„ Z(e'ƒ Z)e(ƒ Z*G d,d-„ d-ƒZ+G d.d/„ d/ƒZ,G d0d1„ d1ƒZ-G d2d3„ d3ƒZ.e.ƒ Z/d4d5„ Z0G d6d7„ d7ƒZ1d8S )9ÚSPZR0ÚNOPÚHLTÚBRKÚADDÚSUBÚINCÚDECÚMLTÚDIVÚMODÚORÚANDÚXORÚNOTÚLSHÚRSHÚBSLÚBSRÚMOVÚIMMÚLODÚSTRÚCPYÚPSHÚPOPÚCALÚRETÚJMPÚBRZÚBNZÚBREÚBNEÚBRLÚBLEÚBRGÚBGEc                   @   s*   e Zd ZdZddg dfdd„Zdd„ ZdS )	ÚClassz Represents a compile-time class.Z	classNameNFc                 C   sH   || _ |d kr&|jr&td|j  d ƒ‚|| _|| _|| _d| _d| _d S )NzClass 'z%' is sealed and can not be inherited.F)ÚNameZSealedÚ
ValueErrorÚParentClassÚFieldsÚ
_ValueTypeÚ_Pending)ÚselfÚnameZparentClassZfieldsÚsealed© r0   úf/media/fuse/drivefs-1ef8861adf0c828aef58e871a600f64f/root/Programming/Python/URCL/URCL-Toolkit/urcl.pyÚ__init__/   s    zClass.__init__c                 C   s4   | j rtdƒ‚t| jƒ}| jdkr0|| j ¡ 7 }|S )z%Get the size of this class in memory.z!Can not get size of pending type.N)r,   r(   Úlenr*   r)   ÚGetSize©r-   Úresultr0   r0   r1   r4   9   s    

zClass.GetSize)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r2   r4   r0   r0   r0   r1   r&   -   s   
r&   c                  C   s   t ddd} d| _| S )z+Get the type that represents a native word.ÚWORDT©r/   )r&   r+   ©r6   r0   r0   r1   ÚGetWordClassB   s    r>   c                  C   s   t ddd} d| _| S )z9Get the type that can be replaced by another class later.ÚPENDINGTr<   )r&   r,   r=   r0   r0   r1   ÚGetPendingClassH   s    r@   c                   @   s.   e Zd ZdZedfdd„Zdd„ Zdd„ Zd	S )
ÚFieldz Represents a compile-time field.Z	fieldNamec                 C   s   || _ || _d S ©N)ÚTyper'   )r-   Útyper.   r0   r0   r1   r2   S   s    zField.__init__c                 C   s   | j jS )z*Returns true if the field is a value type.©rC   r+   ©r-   r0   r0   r1   ÚIsValueW   s    zField.IsValuec                 C   s
   | j j S )z,Returns true if the field is a pointer type.rE   rF   r0   r0   r1   Ú	IsPointer[   s    zField.IsPointerN)r7   r8   r9   r:   r?   r2   rG   rH   r0   r0   r0   r1   rA   Q   s   rA   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚRegisterMapzAn allocator for registers.c                 C   s   d| _ | j | _g | _d S )Né   ©Z_RegistersStartÚ_NextRegisterÚ_FreeRegistersrF   r0   r0   r1   r2   a   s    zRegisterMap.__init__c                 C   s@   t | jƒdkrt| j ¡ ƒS | j}|  jd7  _dt|ƒ S dS )úAllocate a register for use.é    rJ   ÚRN)r3   rM   ÚstrÚpoprL   r5   r0   r0   r1   ÚNewf   s
    zRegisterMap.Newc                 C   s   | j  |¡ dS ©z"Make a register available for use.N)rM   Úappend©r-   Zregr0   r0   r1   ÚFreeo   s    zRegisterMap.Freec                 C   s   | j | _g | _dS )z$Free all registers currently in use.NrK   rF   r0   r0   r1   ÚResets   s    zRegisterMap.ResetN)r7   r8   r9   r:   r2   rS   rW   rX   r0   r0   r0   r1   rI   _   s
   	rI   c                   @   s2   e Zd ZdZddd„Zdd„ Zdd	„ Zd
d„ ZdS )ÚInstructionzRepresents an URCL instruction.r   Nc                 C   s   || _ || _|| _|| _d S rB   )Ú	OperationÚOperandAÚOperandBÚOperandC©r-   Z	operationZoperandAZoperandBZoperandCr0   r0   r1   r2   z   s    zInstruction.__init__c                 C   s2   | j dkrdS | jdkrdS | jdkr*dS dS dS )z/Get the number of operands in this instruction.NrO   rJ   é   é   ©r[   r\   r]   rF   r0   r0   r1   ÚGetOperandCount€   s    


zInstruction.GetOperandCountc                 C   sJ   | j dkrg S | jdkr | j gS | jdkr6| j | jgS | j | j| jgS dS )z/Get a list of the operands in this instruction.Nra   rF   r0   r0   r1   ÚGetOperands‹   s    


zInstruction.GetOperandsc                 C   sŽ   | j dkr| jS | jdkr.| jd t| j ƒ S | jdkrZ| jd t| j ƒ d t| jƒ S | jd t| j ƒ d t| jƒ d t| jƒ S dS )z$Convert the instruction to a string.Nú )r[   rZ   r\   rQ   r]   rF   r0   r0   r1   Ú__str__–   s    


"zInstruction.__str__)r   NNN)r7   r8   r9   r:   r2   rb   rc   re   r0   r0   r0   r1   rY   x   s
   
rY   c                   @   s   e Zd ZdZdd„ ZdS )ÚURCLEmitzLThe default emitter target type. Outputs emitter instructions as plain URCL.c                 C   s¼   d}t t|jƒƒD ]V}||jv rRt t|j| ƒƒD ]}|t|j| | ƒd 7 }q2|t|j| ƒd 7 }qt|jƒ|jv r¸t t|jt|jƒ ƒƒD ]$}|t|jt|jƒ | ƒd 7 }q’|S )NÚ Ú
)Úranger3   ÚInstructionsÚLabelsrQ   )r-   Úemitterr6   ÚiÚjr0   r0   r1   ÚEmit£   s    
"zURCLEmit.EmitN)r7   r8   r9   r:   ro   r0   r0   r0   r1   rf   ¡   s   rf   c                 C   sÌ   t | ƒ ¡ } t| ƒdkrdS |  d¡r*dS |  d¡r@d| vr@| S |  dd¡ dd¡ dd¡ d¡} d	}td
ƒD ]:}|t| ƒkrŠ|  d¡ qn| |  d¡sœ|rnd| |< d}qnt| d  	¡ | d | d | d ƒS )zdParse an URCL instruction from a string. Returns None is the instruction is a comment or empty line.rO   Nz//Ú.rd   ú,z //z  Fé   TrJ   r_   r`   )
rQ   Ústripr3   Ú
startswithÚreplaceÚsplitri   rU   rY   Úupper)ÚtextZcommentrm   r0   r0   r1   ÚParseInstruction±   s     
"ry   c                   @   s  e Zd ZdZeddddfdd„Zdd	„ Zd7dd„Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zeefdd„Zefdd„Zeefdd„Zd8dd „Zd!d"„ Zeddfd#d$„Zd%d&„ Zdedfd'd(„Zddefd)d*„Zdefd+d,„Zdefd-d.„Zedefd/d0„Zedefd1d2„Zd3d4„ Zd5d6„ ZdS )9ÚEmitterz!An emitter for URCL instructions.FrO   l   ÿÿÿÿ Tc                 C   s&  g | _ i | _tƒ | _|| _|r,| j ¡ | _nd | _d| _||k rL|}|}|}|| _|| _	|| _
d | _d | _d | _d| _|s"d| _
d| _|  ¡ }|  ¡ | _|  ¡ | _|  ¡ | _|  t| jt¡ |  t|¡ |  | j¡ |  | j| j¡ |  t¡ |  | j¡ |  | j¡ |  t¡ |  |¡ d| _
d S )NrO   FT)rj   rk   rI   Ú
_RegistersÚ_EmitterTargetrS   ÚBPÚ_NextAnonLabelÚ_MemoryManagerMinAddressÚ_MemoryManagerMaxAddressÚ_InlineMemoryManagementÚ_MemoryManagerAllocateÚ_MemoryManagerFreeÚ_MemoryManagerRegisterÚ_PushRegistersOnMemManageÚNewLabelÚNewRegisterro   r   ÚZEROr   Ú	MarkLabelÚ
NewPointerr   ÚFreePointer)r-   Ú
emitTargetZuseR1AsBasePointerZmemoryManagerMinAddressZmemoryManagerMaxAddressZinlineMemoryManagementZswapZ
entryPointr0   r0   r1   r2   Æ   sH    





zEmitter.__init__c                 C   s   | j dkr|  ¡ | _ | j S )z@Get or allocate a register designated as the stack base pointer.N)r}   r‡   rF   r0   r0   r1   ÚGetBasePointerø   s    

zEmitter.GetBasePointerrg   c                 C   s0   t |ƒdkr(dt| jƒ }|  jd7  _d| S )z0Create a new label with an optional custom name.rO   Z_anonlabel_rJ   rp   )r3   rQ   r~   )r-   r.   r0   r0   r1   r†   þ   s    zEmitter.NewLabelc                 C   sB   t | jƒ| jv r,| jt | jƒ  |g7  < n|g| jt | jƒ< dS )z*Mark a label at the current emit location.N)r3   rj   rk   )r-   Úlabelr0   r0   r1   r‰     s    zEmitter.MarkLabelc                 C   s4   t |ƒ}t|ƒdkr |d dks(|dkr,dS dS dS )z)Determine if a value is an URCL register.rO   rP   r   TFN©rQ   r3   ©r-   Úvaluer0   r0   r1   Ú
IsRegister  s     zEmitter.IsRegisterc                 C   s(   t |ƒ}t|ƒdko&|d dko&d|vS )z&Determine if a value is an URCL label.rO   rp   rd   r   r   r0   r0   r1   ÚIsLabel  s    zEmitter.IsLabelc                 C   s
   | j  ¡ S )rN   )r{   rS   rF   r0   r0   r1   r‡     s    zEmitter.NewRegisterc                 C   s   | j  |¡ dS rT   )r{   rW   rV   r0   r0   r1   ÚFreeRegister  s    zEmitter.FreeRegisterc           
      C   sº  | j rj|  ¡ }|  ¡ }|  ¡ }|  ¡ }|  ¡ }|  ¡ }|  ¡ }	| jrj|  t|¡ |  t|¡ |  t|	¡ |  t|| j¡ |  t|t	¡ |  
|¡ |  t|||¡ |  t||¡ |  t||¡ |  t|	|d¡ |  t||¡ |  t||	¡ |  t|||¡ |  t||¡ |  t||d¡ |  t||¡ |  t||d¡ |  t|¡ |  
|¡ |  t|t	¡ |  t|¡ |  
|¡ |  t||d¡ |  t|¡ |  t|	| j|¡ |  t||	|¡ |  t||¡ |  t||d¡ |  t||¡ |  t||d¡ |  t|¡ |  t|||¡ |  t|t	¡ |  
|¡ | jrJ|  t|	¡ |  t|¡ |  t|¡ |  |¡ |  |¡ |  |	¡ nL|  |¡rˆ|  t| j|¡ n|  t| j|¡ |  t| j¡ |  t|| j¡ dS )z†Allocate a block of memory. It is advisable to use inlineMemoryManagement=False in the emitter options if calling this more than once.rJ   N)r   r†   r‡   r…   ro   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-   ZinSizeÚ
outPointerZ
searchLoopZ	createNewZoutOfMemoryÚfinishÚcurrentBlockÚlengthr‘   r0   r0   r1   rŠ   !  sn    





zEmitter.NewPointerc                 C   s  | j rÄ|  ¡ }|  ¡ }|  ¡ }| jr<|  t|¡ |  t|¡ |  t||¡ |  t||d¡ |  t||¡ |  t	||d¡ |  t
||¡ |  |¡ | jr®|  t|¡ |  t|¡ |  |¡ |  |¡ n:|  |¡rà|  t| j|¡ n|  t| j|¡ |  t| j¡ dS )z‚Free a block of memory. It is advisable to use inlineMemoryManagement=False in the emitter options if calling this more than once.rJ   éþÿÿÿN)r   r†   r‡   r…   ro   r   r   r   r   r   r   r‰   r   r”   r’   r   r„   r   r   rƒ   )r-   Ú	inPointerr–   r—   r˜   r0   r0   r1   r‹   x  s,    


zEmitter.FreePointerc                 C   s   |   | ¡ |¡ dS )z2Allocate a block of memory for the specified type.N)rŠ   r4   )r-   rD   r•   r0   r0   r1   Ú	NewObjectœ  s    zEmitter.NewObjectr   Nc                 C   s   | j  t||||ƒ¡ dS )zCEmit an URCL instruction with the specified operation and operands.N)rj   rU   rY   r^   r0   r0   r1   ro      s    zEmitter.Emitc                 C   s   dS )NrO   r0   )r-   Úargr0   r0   r1   Ú_CallFunctionDefaultArguments¤  s    z%Emitter._CallFunctionDefaultArgumentsc                 C   s:   |   ttt|¡ || |ƒ}|   t|¡ |   ttt|¡ dS )z´Emit a function call that passes the arguments pushed onto the stack by the specified emitArgsFunc(emitter, emitArgsFuncArg), which returns the number of arguments that was pushed.N)ro   r   r   r   r   )r-   rŽ   ZemitArgsFuncZemitArgsFuncArgZoutCountZinCountr0   r0   r1   ÚCallFunction§  s    
zEmitter.CallFunctionc                 C   s   d S rB   r0   )r-   rœ   ÚreturnLabelr0   r0   r1   Ú_EmitFunctionDefaultBody®  s    z Emitter._EmitFunctionDefaultBodyc                 C   sŠ   |   |¡ |  t|  ¡ ¡ |  t|  ¡ t¡ |  ttt|¡ |  ¡ }|| ||ƒ |   |¡ |  tt|  ¡ ¡ |  t|  ¡ ¡ |  t	¡ dS )zfEmit a function that executes the code emitted by emitBodyFunc(emitter, emitBodyFuncArg, returnLabel).N)
r‰   ro   r   r   r   r   r   r†   r   r   )r-   rŽ   Z
localCountZemitBodyFuncZemitBodyFuncArgrŸ   r0   r0   r1   ÚEmitFunction±  s    

zEmitter.EmitFunctionc                 C   s.   |   t||  ¡ || d ¡ |   t||¡ dS )z]Get the value of the function argument with the specified total arguments and argument index.rJ   N)ro   r   r   r   )r-   ZargumentCountZargumentIndexÚoutValuer0   r0   r1   ÚEmitGetArgument¾  s    zEmitter.EmitGetArgumentc                 C   s&   |   t||  ¡ |¡ |   t||¡ dS )zCGet the value of the function local with the specified local index.N)ro   r   r   r   )r-   Ú
localIndexr¢   r0   r0   r1   ÚEmitGetLocalÃ  s    zEmitter.EmitGetLocalc                 C   s8   |   ¡ }|  t||  ¡ |¡ |  t||¡ |  |¡ dS )zMSet the value of the function local with the specified local index and value.N)r‡   ro   r   r   r   r”   )r-   r¤   ÚinValueZlocalPointerr0   r0   r1   ÚEmitSetLocalÈ  s    zEmitter.EmitSetLocalc                 C   s4   |   ¡ }|  t|||¡ |  t||¡ |  |¡ dS )zMGet the value of the object field with the specified pointer and field index.N)r‡   ro   r   r   r‹   )r-   rš   Ú
fieldIndexr¢   ÚfieldPointerr0   r0   r1   ÚEmitGetObjectFieldÏ  s    zEmitter.EmitGetObjectFieldc                 C   s4   |   ¡ }|  t|||¡ |  t||¡ |  |¡ dS )zMSet the value of the object field with the specified pointer and field index.N)r‡   ro   r   r   r”   )r-   rš   r¨   r¦   r©   r0   r0   r1   ÚEmitSetObjectFieldÖ  s    zEmitter.EmitSetObjectFieldc                 C   s   t | ƒS ©z9Compile the emitter instructions with the emitter target.)rQ   rF   r0   r0   r1   ÚCompileÝ  s    zEmitter.Compilec                 C   s   | j  | ¡S r¬   )r|   ro   rF   r0   r0   r1   re   á  s    zEmitter.__str__)rg   )r   NNN)r7   r8   r9   r:   ÚDEFAULT_TARGETr2   r   r†   r‰   r’   r“   r‡   r”   rˆ   rŠ   r‹   r?   r›   ro   r   rž   r    r¡   r£   r¥   r§   rª   r«   r­   re   r0   r0   r0   r1   rz   Ä   s0   2
W$
rz   N)2r   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>   r@   r;   r?   rA   rI   rY   rf   r®   ry   rz   r0   r0   r0   r1   Ú<module>   sb   )