o
    h                     @   s
  d dl Z d dlmZ d dlZd dl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dejjded d	eje
B fd
dZdejded	eje
B fddZdejded	eje
B fddZdedejd	eje
B fddZdedejd	ee
B fddZdS )    N)	dataclass)Literal)	TracebackzCould not parse urcl sourceinstructionbits)    @   returnc                 C   s   t dg }tjD ]H}| j|jv rQt| j|jkr6t	j
d| jj  dt| j d|j | j| jd  S || |}t|t	rG|}|  S |j|j  |S q	t	
d| jj | j| jS )Nr   z Incorrect number of operands to z	 - found z, expected line_numbercolumn_numberz,No x86 translation for for URCL instruction )x86ASMCodecompile_x86TRANSLATIONSmnemonic	mnemonicslenoperandsoperand_countr   newnameupperr   r   compile
isinstancecodeextend)r   r   x86_codefamilytranslationerror r!   1/home/seanl/mnt/public/Repos/urclelf/./compile.py urcl_instruction_to_x86_assembly   s   
:
r#   urcl_programis_mainc                 C      t d g }|r|jt d | jD ]U}t|tjj	r1t|j
tjr0|jt |j
j qqt|d}t|trO|}|jd| d|j|jd |  S |jD ]}t|t jr[qR||jdd |jD  qRq|S )N_startr   URCL Instruction  could not be compiled into x86r
   c                 S      g | ]}|j qS r!   value.0opr!   r!   r"   
<listcomp>>       z+compile_urcl_to_x86_asm.<locals>.<listcomp>r   r   r   appendLabellinesr   urclurclcstTerminalr,   r   r#   r   	elaborater   r   add_instructionr   r   r$   r%   x86_assembly_codelinex86_instructionr    r!   r!   r"   compile_urcl_to_x86_asm%   (   



r?   c                 C   r&   )Nr'   r   r(   r)   r
   c                 S   r*   r!   r+   r-   r!   r!   r"   r0   [   r1   z+compile_urcl_to_x64_asm.<locals>.<listcomp>r2   r;   r!   r!   r"   compile_urcl_to_x64_asmB   r@   rA   sourceoptionsc                 C   s   t | }t|tr|}|d |S |jjtjjkrOt	||j
}t|tr/|}|d |S t||jjtjjkr<dnd}t|trM|}|d |S |S |jjtjjkrt||j
}t|trk|}|d |S t||jjtjjkrxdnd}t|tr|}|d |S |S tdS )	Nz+Code cannot be parsed, aborting compilationz#URCL code does not translate to x86r   r   z6Assembled x86 program does not convert to machine codez#URCL code does not translate to x64z6Assembled x64 program does not convert to machine codez(Only x86 and x64 are currently supported)r6   parser   r   r9   targetisaIsaX86r?   r%   r   assembleX64rA   r   )rB   rC   r$   r    r<   machine_coder!   r!   r"   "compile_urcl_source_to_flat_binary_   s<   




 



 


rL   c                 C   s   |j tjjkrt| |}n%|j tjjfvrtd|j  dS t| |}t|tr2|}|	d |S |j tjjkrJt
j||jjtjjkd}t|S d}t|S )NzExecutable format z is not supportedz#Machine code could not be generated)	is_64_bit    )executable_formatrE   ExecutableFormatFLATrL   ELFr   r   r   r9   elfmake_relocatable_elfrF   rG   rJ   bytes)rB   rC   output_binaryassembled_coder    r!   r!   r"   compile_urcl_to_executable   s   


rX   )mathdataclassesr   enumtypingr   r6   r   rS   r    r   r   rE   PARSING_ERROR_MESSAGENO_ERROR_EXIT_CODEr7   InstructionCSTNoder   r#   CSTboolr?   rA   strCompileOptionsAssembledMachineCoderL   rU   rX   r!   r!   r!   r"   <module>   s$    $ )