
    h                        d dl Z d dlmZ d dl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Zd dlZd dl	Z	dZd Ze G d d                      Zdej        j        d	ed
         dej        ez  fdZdej        dedej        ez  fdZdej        dedej        ez  fdZdedej        dej        ez  fdZdedej        deez  fdZdS )    N)	dataclass)LiteralCallable)	TracebackzCould not parse urcl sourcec                   \    e Zd ZU eej        egej        e	z  f         e
d<   ej        e
d<   dS )UrclCodegencompileisaN)__name__
__module____qualname__r   urclCSTboolx86ASMCoder   __annotations__targetIsa     //home/seanl/mnt/public/Repos/urclelf/compile.pyr   r      s?         tx&i(??@@@@	OOOOOr   r   instructionbits)    @   returnc           	      `   t          j        dg           }t          j        D ]}| j        |j        v rt          | j                  |j        k    r_t          j
        d| j        j                                         dt          | j                   d|j         | j        | j                  c S |                    | |          }t!          |t                    r|}|c S |j                            |j                    n.t          j
        d| j        j         | j        | j                  S |S )Nr   z Incorrect number of operands to z	 - found z, expected line_numbercolumn_numberz,No x86 translation for for URCL instruction )r   r   compile_x86TRANSLATIONSmnemonic	mnemonicslenoperandsoperand_countr   newnameupperr    r!   r	   
isinstancecodeextend)r   r   x86_codefamilytranslationerrors         r    urcl_instruction_to_x86_assemblyr3      s   {1b!!H* ] ]6#333;'((F,@@@ }  &qH\HaHgHgHiHi  &q  &qtw  yD  yM  uN  uN  &q  &q  [a  [o  &q  &q  J  V  fq  f  @  @  @  @  @  @ ..d;;K+y11 #$$[%5666 4 }gKL`Leggit  jA  CN  C\  ]  ]  	]Or   urcl_programis_mainc                    t          j        d g           }|r,|j                            t          j        d                     | j        D ]}t          |t          j        j	                  rWt          |j
        t          j                  r7|j                            t          j        |j
        j                             xyt          |d          }t          |t                    r,|}|                    d| d|j        |j                   |c S |j        D ]G}t          |t           j                  r|                    |j        d |j        D                        H|S )N_startr   URCL Instruction  could not be compiled into x86r   c                     g | ]	}|j         
S r   value.0ops     r   
<listcomp>z+compile_urcl_to_x86_asm.<locals>.<listcomp>B       HuHuHuVXHuHuHur   r   r   r-   appendLabellinesr,   r   urclcstTerminalr<   r*   r3   r   	elaborater    r!   add_instructionr$   r'   r4   r5   x86_assembly_codelinex86_instructionr2   s         r   compile_urcl_to_x86_asmrN   )   s   D"--
  ;%%ci&9&9:::" w wdDL122 	$*dj11 !&--ci
.H.HIII:4DDoy11 	#EOOUUUUcgcs  DH  DVO  W  W  WLLL.3 	w 	wO/3955 --o.FHuHu\k\tHuHuHuvvvv	w
 r   c                    t          j        d g           }|r,|j                            t          j        d                     | j        D ]}t          |t          j        j	                  rWt          |j
        t          j                  r7|j                            t          j        |j
        j                             xyt          |d          }t          |t                    r,|}|                    d| d|j        |j                   |c S |j        D ]G}t          |t           j                  r|                    |j        d |j        D                        H|S )Nr7   r   r8   r9   r   c                     g | ]	}|j         
S r   r;   r=   s     r   r@   z+compile_urcl_to_x64_asm.<locals>.<listcomp>\   rA   r   rB   rJ   s         r   compile_urcl_to_x64_asmrQ   F   s}   D"-- ;%%ci&9&9:::" w wdDL122 	$*dj11 !&--ci
.H.HIII:4DDoy11 	#EOOUUUUcgcs  DH  DVO  W  W  WLLL.3 	w 	wO/3955 --o.FHuHu\k\tHuHuHuvvvv	w
 r   sourceoptionsc                    t          j        |           }t          |t                    r|}|                    d           |S t          t          t          j        j	                  }t          t          t          j        j                  }||fD ]}|j        j        |j        k    r|                    ||j                  }t          |t                    r#|}|                    d|j                    |c S t          j        ||j        j        t          j        j        k    rdnd          }t          |t                    r$|}|                    d|j         d           |c S |c S t          j        d|j        j         d          S )	Nz+Code cannot be parsed, aborting compilationz URCL code does not translate to r   r   z
Assembled z) program does not convert to machine codezThe architecture z is not currently supported)r   parser,   r   rH   r   rN   r   r   X86rQ   X64r
   r	   r5   r   assembler)   )	rR   rS   r4   r2   urcl_to_x86_assemberurcl_to_x64_assember	assemblerassembly_codemachine_codes	            r   "compile_urcl_source_to_flat_binaryr^   `   s   :f%%L,	** EFFF&'>
OO&'>
OO*,@A    	>..%--lGOLLM-33 % R9= R RSSS<W^=OQWQ[Q_=_=_rreghhL,	22 $ eY] e e efff / =\W^-?\\\]]]r   c                    t          | |          }t          |t                    r|}|                    d           |S |j        t
          j        j        k    r|j        }nk|j        t
          j        j	        k    r4t          j        ||j        j        t
          j        j        k              }nt          j        d|j         d          S t!          |          S )Nz#Machine code could not be generated)	is_64_bitzExecutable format z is not supported)r^   r,   r   rH   executable_formatr   ExecutableFormatFLATbinaryELFelfmake_relocatable_elfr
   r   rW   r)   bytes)rR   rS   flat_binaryr2   output_binarys        r   compile_urcl_to_executablerk   |   s    4VWEEK+y)) =>>> F$;$@@@#*		"f&=&A	A	A0I[]c]g]kIkmmm}^'2K^^^___r   )mathdataclassesr   enumtypingr   r   r   r   rf   r2   r   r"   r   PARSING_ERROR_MESSAGENO_ERROR_EXIT_CODEr   rF   InstructionCSTNoder   r3   r   r   rN   rQ   strCompileOptionsAssembledMachineCoder^   rh   rk   r   r   r   <module>rv      s    ! ! ! ! ! !  $ $ $ $ $ $ $ $  



 



            



5  
       $,2Q Y`agYh mpmx  |E  nE    &$( T ckT]F]    :$( T ckT]F]    4^s ^V=R ^WZWor{W{ ^ ^ ^ ^8 s  V5J  uW`O`            r   