Home | History | Annotate | Line # | Download | only in compile
      1 /* Header file for Compile and inject module.
      2 
      3    Copyright (C) 2014-2024 Free Software Foundation, Inc.
      4 
      5    This program is free software; you can redistribute it and/or modify
      6    it under the terms of the GNU General Public License as published by
      7    the Free Software Foundation; either version 3 of the License, or
      8    (at your option) any later version.
      9 
     10    This program is distributed in the hope that it will be useful,
     11    but WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13    GNU General Public License for more details.
     14 
     15    You should have received a copy of the GNU General Public License
     16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     17 
     18 #ifndef COMPILE_COMPILE_H
     19 #define COMPILE_COMPILE_H
     20 
     21 #include "gcc-c-interface.h"
     22 #include "gdbsupport/gdb-hashtab.h"
     23 
     24 struct ui_file;
     25 struct gdbarch;
     26 struct dwarf2_per_cu_data;
     27 struct dwarf2_per_objfile;
     28 struct symbol;
     29 struct dynamic_prop;
     30 
     31 /* Scope types enumerator.  List the types of scopes the compiler will
     32    accept.  */
     33 
     34 enum compile_i_scope_types
     35   {
     36     COMPILE_I_INVALID_SCOPE,
     37 
     38     /* A simple scope.  Wrap an expression into a simple scope that
     39        takes no arguments, returns no value, and uses the generic
     40        function name "_gdb_expr". */
     41 
     42     COMPILE_I_SIMPLE_SCOPE,
     43 
     44     /* Do not wrap the expression,
     45        it has to provide function "_gdb_expr" on its own.  */
     46     COMPILE_I_RAW_SCOPE,
     47 
     48     /* A printable expression scope.  Wrap an expression into a scope
     49        suitable for the "compile print" command.  It uses the generic
     50        function name "_gdb_expr".  COMPILE_I_PRINT_ADDRESS_SCOPE variant
     51        is the usual one, taking address of the object.
     52        COMPILE_I_PRINT_VALUE_SCOPE is needed for arrays where the array
     53        name already specifies its address.  See get_out_value_type.  */
     54     COMPILE_I_PRINT_ADDRESS_SCOPE,
     55     COMPILE_I_PRINT_VALUE_SCOPE,
     56   };
     57 
     58 /* An object of this type holds state associated with a given
     59    compilation job.  */
     60 
     61 class compile_instance
     62 {
     63 public:
     64   compile_instance (struct gcc_base_context *gcc_fe, const char *options);
     65 
     66   virtual ~compile_instance ()
     67   {
     68     m_gcc_fe->ops->destroy (m_gcc_fe);
     69   }
     70 
     71   /* Returns the GCC options to be passed during compilation.  */
     72   const std::string &gcc_target_options () const
     73   {
     74     return m_gcc_target_options;
     75   }
     76 
     77   /* Query the type cache for TYPE, returning the compiler's
     78      type for it in RET.  */
     79   bool get_cached_type (struct type *type, gcc_type *ret) const;
     80 
     81   /* Insert GCC_TYPE into the type cache for TYPE.
     82 
     83      It is ok for a given type to be inserted more than once, provided that
     84      the exact same association is made each time.  */
     85   void insert_type (struct type *type, gcc_type gcc_type);
     86 
     87   /* Associate SYMBOL with some error text.  */
     88   void insert_symbol_error (const struct symbol *sym, const char *text);
     89 
     90   /* Emit the error message corresponding to SYM, if one exists, and
     91      arrange for it not to be emitted again.  */
     92   void error_symbol_once (const struct symbol *sym);
     93 
     94   /* These currently just forward to the underlying ops
     95      vtable.  */
     96 
     97   /* Set the plug-in print callback.  */
     98   void set_print_callback (void (*print_function) (void *, const char *),
     99 			   void *datum);
    100 
    101   /* Return the plug-in's front-end version.  */
    102   unsigned int version () const;
    103 
    104   /* Set the plug-in's verbosity level.  Nop for GCC_FE_VERSION_0.  */
    105   void set_verbose (int level);
    106 
    107   /* Set the plug-in driver program.  Nop for GCC_FE_VERSION_0.  */
    108   void set_driver_filename (const char *filename);
    109 
    110   /* Set the regular expression used to match the configury triplet
    111      prefix to the compiler.  Nop for GCC_FE_VERSION_0.  */
    112   void set_triplet_regexp (const char *regexp);
    113 
    114   /* Set compilation arguments.  REGEXP is only used for protocol
    115      version GCC_FE_VERSION_0.  */
    116   gdb::unique_xmalloc_ptr<char> set_arguments (int argc, char **argv,
    117 					       const char *regexp = NULL);
    118 
    119   /* Set the filename of the program to compile.  Nop for GCC_FE_VERSION_0.  */
    120   void set_source_file (const char *filename);
    121 
    122   /* Compile the previously specified source file to FILENAME.
    123      VERBOSE_LEVEL is only used for protocol version GCC_FE_VERSION_0.  */
    124   bool compile (const char *filename, int verbose_level = -1);
    125 
    126   /* Set the scope type for this compile.  */
    127   void set_scope (enum compile_i_scope_types scope)
    128   {
    129     m_scope = scope;
    130   }
    131 
    132   /* Return the scope type.  */
    133   enum compile_i_scope_types scope () const
    134   {
    135     return m_scope;
    136   }
    137 
    138   /* Set the block to be used for symbol searches.  */
    139   void set_block (const struct block *block)
    140   {
    141     m_block = block;
    142   }
    143 
    144   /* Return the search block.  */
    145   const struct block *block () const
    146   {
    147     return m_block;
    148   }
    149 
    150 protected:
    151 
    152   /* The GCC front end.  */
    153   struct gcc_base_context *m_gcc_fe;
    154 
    155   /* The "scope" of this compilation.  */
    156   enum compile_i_scope_types m_scope;
    157 
    158   /* The block in which an expression is being parsed.  */
    159   const struct block *m_block;
    160 
    161   /* Specify "-std=gnu11", "-std=gnu++11" or similar.  These options are put
    162      after CU's DW_AT_producer compilation options to override them.  */
    163   std::string m_gcc_target_options;
    164 
    165   /* Map from gdb types to gcc types.  */
    166   htab_up m_type_map;
    167 
    168   /* Map from gdb symbols to gcc error messages to emit.  */
    169   htab_up m_symbol_err_map;
    170 };
    171 
    172 /* Public function that is called from compile_control case in the
    173    expression command.  GDB returns either a CMD, or a CMD_STRING, but
    174    never both.  */
    175 
    176 extern void eval_compile_command (struct command_line *cmd,
    177 				  const char *cmd_string,
    178 				  enum compile_i_scope_types scope,
    179 				  void *scope_data);
    180 
    181 /* Compile a DWARF location expression to C, suitable for use by the
    182    compiler.
    183 
    184    STREAM is the stream where the code should be written.
    185 
    186    RESULT_NAME is the name of a variable in the resulting C code.  The
    187    result of the expression will be assigned to this variable.
    188 
    189    SYM is the symbol corresponding to this expression.
    190    PC is the location at which the expression is being evaluated.
    191    ARCH is the architecture to use.
    192 
    193    REGISTERS_USED is an out parameter which is updated to note which
    194    registers were needed by this expression.
    195 
    196    ADDR_SIZE is the DWARF address size to use.
    197 
    198    OPT_PTR and OP_END are the bounds of the DWARF expression.
    199 
    200    PER_CU is the per-CU object used for looking up various other
    201    things.
    202 
    203    PER_OBJFILE is the per-objfile object also used for looking up various other
    204    things.  */
    205 
    206 extern void compile_dwarf_expr_to_c (string_file *stream,
    207 				     const char *result_name,
    208 				     struct symbol *sym,
    209 				     CORE_ADDR pc,
    210 				     struct gdbarch *arch,
    211 				     std::vector<bool> &registers_used,
    212 				     unsigned int addr_size,
    213 				     const gdb_byte *op_ptr,
    214 				     const gdb_byte *op_end,
    215 				     dwarf2_per_cu_data *per_cu,
    216 				     dwarf2_per_objfile *per_objfile);
    217 
    218 /* Compile a DWARF bounds expression to C, suitable for use by the
    219    compiler.
    220 
    221    STREAM is the stream where the code should be written.
    222 
    223    RESULT_NAME is the name of a variable in the resulting C code.  The
    224    result of the expression will be assigned to this variable.
    225 
    226    PROP is the dynamic property for which we're compiling.
    227 
    228    SYM is the symbol corresponding to this expression.
    229    PC is the location at which the expression is being evaluated.
    230    ARCH is the architecture to use.
    231 
    232    REGISTERS_USED is an out parameter which is updated to note which
    233    registers were needed by this expression.
    234 
    235    ADDR_SIZE is the DWARF address size to use.
    236 
    237    OPT_PTR and OP_END are the bounds of the DWARF expression.
    238 
    239    PER_CU is the per-CU object used for looking up various other
    240    things.
    241 
    242    PER_OBJFILE is the per-objfile object also used for looking up various other
    243    things.  */
    244 
    245 extern void compile_dwarf_bounds_to_c (string_file *stream,
    246 				       const char *result_name,
    247 				       const struct dynamic_prop *prop,
    248 				       struct symbol *sym, CORE_ADDR pc,
    249 				       struct gdbarch *arch,
    250 				       std::vector<bool> &registers_used,
    251 				       unsigned int addr_size,
    252 				       const gdb_byte *op_ptr,
    253 				       const gdb_byte *op_end,
    254 				       dwarf2_per_cu_data *per_cu,
    255 				       dwarf2_per_objfile *per_objfile);
    256 
    257 extern void compile_print_value (struct value *val, void *data_voidp);
    258 
    259 /* Command element for the 'compile' command.  */
    260 extern cmd_list_element *compile_cmd_element;
    261 
    262 #endif /* COMPILE_COMPILE_H */
    263