Search
lxdream.org :: lxdream/src/xlat/xlat.h
lxdream 0.9.1
released Jun 29
Download Now
filename src/xlat/xlat.h
changeset 1011:fdd58619b760
prev1006:3a169c224c12
author nkeynes
date Sun Apr 12 07:24:45 2009 +0000 (15 years ago)
branchxlat-refactor
permissions -rw-r--r--
last change Restructure operand types -
rename to forms to avoid conflict for actual data types
temporary operands are now a first class form
remove explicit types for immediates - now implied by opcode
Initial work on promote-source-reg pass
view annotate diff log raw
     1 /**
     2  * $Id: xlat.h 931 2008-10-31 02:57:59Z nkeynes $
     3  * 
     4  * Internal translation data structures and functions.
     5  *
     6  * Copyright (c) 2009 Nathan Keynes.
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    19 #ifndef lxdream_xlat_H
    20 #define lxdream_xlat_H 1
    22 #include "xlat/xiropt.h"
    23 #include "xlat/xltcache.h"
    25 typedef struct target_data *target_data_t;
    27 /**
    28  * Source machine description. This should be immutable once constructed.
    29  **/
    30 struct xlat_source_machine {
    31     const char *name;
    32     void *state_data;   /* Pointer to source machine state structure */
    33     uint32_t pc_offset; /* Offset of source PC, relative to state_data */
    34     uint32_t delayed_pc_offset; /* Offset of source delayed PC offset, relative to state_data */
    35     uint32_t t_offset; /* Offset of source T reg, relative to state_data */
    36     uint32_t m_offset;
    37     uint32_t q_offset;
    38     uint32_t s_offset;
    40     /**
    41      * Return the name of the register with the given type,
    42      * or NULL if no such register exists
    43      */
    44     const char * (*get_register_name)( uint32_t reg, xir_type_t type );
    46     /**
    47      * Decode a basic block of instructions from start, stopping after a
    48      * control transfer or before the given end instruction.
    49      * @param sd source data. This method should set the address_space field.
    50      * @return the pc value after the last decoded instruction.
    51      */ 
    52     uint32_t (*decode_basic_block)(xir_basic_block_t xbb);
    53 };
    56 /* Target machine description (no these are not meant to be symmetrical) */
    57 struct xlat_target_machine {
    58     const char *name;
    60     /* Required functions */
    62     /**
    63      * Return the name of the register with the given type,
    64      * or NULL if no such register exists
    65      */
    66     const char * (*get_register_name)( uint32_t reg, xir_type_t type );
    68     /**
    69      * Test if the given operands are legal for the opcode. Note that it is assumed that
    70      * target register operands are always legal. This is used by the register allocator
    71      * to determine when it can fuse load/stores with another operation.
    72      */
    73     gboolean (*is_legal)( xir_opcode_t op, xir_operand_form_t arg0, xir_operand_form_t arg1 );
    75     /**
    76      * Lower IR closer to the machine, handling machine-specific issues that can't
    77      * wait until final code-gen. Can add additional instructions where required.
    78      */
    79     void (*lower)( xir_basic_block_t xbb, xir_op_t begin, xir_op_t end );
    81     /**
    82      * Determine the memory required to emit code for the specified block excluding
    83      * exceptions. This can be an overestimate,
    84      * as long as it is at least large enough for the final code.
    85      * @param begin start of code block
    86      * @param end end of code block
    87      * @return estimated size of emitted code.
    88      */
    89     uint32_t (*get_code_size)( xir_op_t begin, xir_op_t end );
    91     /**
    92      * Final target code generation.
    93      * @param td target_data information.
    94      * @param begin start of code block
    95      * @param end end of code block
    96      * @param exception_table Table of pointers to exception code 
    97      * @return number of bytes actually emitted.
    98      */
    99     uint32_t (*codegen)( target_data_t td, xir_op_t begin, xir_op_t end ); 
   100 };
   103 /**
   104  * Fixup records generated while assembling the code. Records are one of the
   105  * following types:
   106  *    Constant (32 or 64-bit)
   107  *    Exception (from a memory call or RAISEME instruction)
   108  *    
   109  * Relocations may be 32/64 bit absolute or 32-bit PC-relative. The value in the
   110  * relocation cell is taken as the addend if nonzero. For relative relocations, 
   111  * the relative displacement is calculated from the end of the fixup value - 
   112  * that is, for a REL32, the result will be
   113  *   *fixup_loc += &target - (fixup_loc+4)
   114  *  
   115  * In principle we could use a global constant table, but that adds complexity
   116  * at this stage. 
   117  */
   119 #define TARGET_FIXUP_CONST32  0x00  
   120 #define TARGET_FIXUP_CONST64  0x01
   121 #define TARGET_FIXUP_RAISE    0x02
   122 #define TARGET_FIXUP_RAISEEXT 0x03 /* An exception that can be raised from outside the generated code */
   123 #define TARGET_FIXUP_OFFSET   0x04 /* Offset within the code block */
   124 #define TARGET_FIXUP_POINTER  0x05 /* Absolute pointer */
   126 #define TARGET_FIXUP_ABS32    0x00
   127 #define TARGET_FIXUP_ABS64    0x10
   128 #define TARGET_FIXUP_REL32    0x20
   129 #define TARGET_FIXUP_REL64    0x30
   131 #define TARGET_FIXUP_TARGET(x) ((x)&0x0F)
   132 #define TARGET_FIXUP_MODE(x)   ((x)&0xF0)
   134 typedef struct target_fixup_struct {
   135     int fixup_type; /* Combination of TARGET_FIXUP flags above */
   136     uint32_t fixup_offset; /* Location of fixup (to be modified) relative to start of block */
   137     uint32_t target_offset;
   138     union {
   139         uint32_t i;
   140         uint64_t q;
   141         float f;
   142         double d;
   143         void *p;
   144         xir_op_t *exc;
   145     } value;
   146 } *target_fixup_t;
   148 /**
   149  * Temporary data maintained during code generation
   150  */
   151 struct target_data {
   152     struct xlat_target_machine *mach;
   153     struct xlat_source_macine *src;
   154     xlat_cache_block_t block;
   155     uint8_t *xlat_output;
   156     target_fixup_t fixup_table;
   157     int fixup_table_posn;
   158     int fixup_table_size;
   159 };
   161 /** Add fixup to a 32-bit constant memory value, adding the value to the constant table */
   162 void target_add_const32_fixup( target_data_t td, int mode, void *location, uint32_t i );
   163 /** Add fixup to a 64-bit constant memory value, adding the value to the constant table */
   164 void target_add_const64_fixup( target_data_t td, int mode, void *location, uint64_t i );
   165 /** Add fixup to an internal exception handler block */
   166 void target_add_raise_fixup( target_data_t td, int type, void *location, xir_op_t *exc );
   167 /** Add fixup to an externally accessible exception handle block */
   168 void target_add_raiseext_fixup( target_data_t td, int type, void *location, xir_op_t *exc );
   169 /** Add fixup to an arbitrary offset within the code block */
   170 void target_add_offset_fixup( target_data_t td, int type, void *location, uint32_t off );
   171 /** Add fixup to an arbitrary pointer */
   172 void target_add_pointer_fixup( target_data_t td, int type, void *location, void *p );
   174 /**
   175  * Generate final code for the block.
   176  * @return entry point of the code block.
   177  */ 
   178 void *target_codegen( xlat_target_machine_t target, xir_basic_block_t xbb );   
   180 #endif /* lxdream_xlat_H */
.