1 /* Header file for GDB compile command and supporting functions.
2    Copyright (C) 2014-2021 Free Software Foundation, Inc.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16 
17 #ifndef COMPILE_COMPILE_INTERNAL_H
18 #define COMPILE_COMPILE_INTERNAL_H
19 
20 #include "gcc-c-interface.h"
21 
22 /* Debugging flag for the "compile" family of commands.  */
23 
24 extern bool compile_debug;
25 
26 struct block;
27 
28 /* An object that maps a gdb type to a gcc type.  */
29 
30 struct type_map_instance
31 {
32   /* The gdb type.  */
33 
34   struct type *type;
35 
36   /* The corresponding gcc type handle.  */
37 
38   gcc_type gcc_type_handle;
39 };
40 
41 /* An object of this type holds state associated with a given
42    compilation job.  */
43 
44 class compile_instance
45 {
46 public:
47   compile_instance (struct gcc_base_context *gcc_fe, const char *options);
48 
~compile_instance()49   virtual ~compile_instance ()
50   {
51     m_gcc_fe->ops->destroy (m_gcc_fe);
52   }
53 
54   /* Returns the GCC options to be passed during compilation.  */
gcc_target_options()55   const std::string &gcc_target_options () const
56   {
57     return m_gcc_target_options;
58   }
59 
60   /* Query the type cache for TYPE, returning the compiler's
61      type for it in RET.  */
62   bool get_cached_type (struct type *type, gcc_type *ret) const;
63 
64   /* Insert GCC_TYPE into the type cache for TYPE.
65 
66      It is ok for a given type to be inserted more than once, provided that
67      the exact same association is made each time.  */
68   void insert_type (struct type *type, gcc_type gcc_type);
69 
70   /* Associate SYMBOL with some error text.  */
71   void insert_symbol_error (const struct symbol *sym, const char *text);
72 
73   /* Emit the error message corresponding to SYM, if one exists, and
74      arrange for it not to be emitted again.  */
75   void error_symbol_once (const struct symbol *sym);
76 
77   /* These currently just forward to the underlying ops
78      vtable.  */
79 
80   /* Set the plug-in print callback.  */
81   void set_print_callback (void (*print_function) (void *, const char *),
82 			   void *datum);
83 
84   /* Return the plug-in's front-end version.  */
85   unsigned int version () const;
86 
87   /* Set the plug-in's verbosity level.  Nop for GCC_FE_VERSION_0.  */
88   void set_verbose (int level);
89 
90   /* Set the plug-in driver program.  Nop for GCC_FE_VERSION_0.  */
91   void set_driver_filename (const char *filename);
92 
93   /* Set the regular expression used to match the configury triplet
94      prefix to the compiler.  Nop for GCC_FE_VERSION_0.  */
95   void set_triplet_regexp (const char *regexp);
96 
97   /* Set compilation arguments.  REGEXP is only used for protocol
98      version GCC_FE_VERSION_0.  */
99   gdb::unique_xmalloc_ptr<char> set_arguments (int argc, char **argv,
100 					       const char *regexp = NULL);
101 
102   /* Set the filename of the program to compile.  Nop for GCC_FE_VERSION_0.  */
103   void set_source_file (const char *filename);
104 
105   /* Compile the previously specified source file to FILENAME.
106      VERBOSE_LEVEL is only used for protocol version GCC_FE_VERSION_0.  */
107   bool compile (const char *filename, int verbose_level = -1);
108 
109   /* Set the scope type for this compile.  */
set_scope(enum compile_i_scope_types scope)110   void set_scope (enum compile_i_scope_types scope)
111   {
112     m_scope = scope;
113   }
114 
115   /* Return the scope type.  */
scope()116   enum compile_i_scope_types scope () const
117   {
118     return m_scope;
119   }
120 
121   /* Set the block to be used for symbol searches.  */
set_block(const struct block * block)122   void set_block (const struct block *block)
123   {
124     m_block = block;
125   }
126 
127   /* Return the search block.  */
block()128   const struct block *block () const
129   {
130     return m_block;
131   }
132 
133 protected:
134 
135   /* The GCC front end.  */
136   struct gcc_base_context *m_gcc_fe;
137 
138   /* The "scope" of this compilation.  */
139   enum compile_i_scope_types m_scope;
140 
141   /* The block in which an expression is being parsed.  */
142   const struct block *m_block;
143 
144   /* Specify "-std=gnu11", "-std=gnu++11" or similar.  These options are put
145      after CU's DW_AT_producer compilation options to override them.  */
146   std::string m_gcc_target_options;
147 
148   /* Map from gdb types to gcc types.  */
149   htab_up m_type_map;
150 
151   /* Map from gdb symbols to gcc error messages to emit.  */
152   htab_up m_symbol_err_map;
153 };
154 
155 /* Define header and footers for different scopes.  */
156 
157 /* A simple scope just declares a function named "_gdb_expr", takes no
158    arguments and returns no value.  */
159 
160 #define COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG "__gdb_regs"
161 #define COMPILE_I_SIMPLE_REGISTER_ARG_NAME "__regs"
162 #define COMPILE_I_SIMPLE_REGISTER_DUMMY "_dummy"
163 #define COMPILE_I_PRINT_OUT_ARG_TYPE "void *"
164 #define COMPILE_I_PRINT_OUT_ARG "__gdb_out_param"
165 #define COMPILE_I_EXPR_VAL "__gdb_expr_val"
166 #define COMPILE_I_EXPR_PTR_TYPE "__gdb_expr_ptr_type"
167 
168 /* A "type" to indicate a NULL type.  */
169 
170 const gcc_type GCC_TYPE_NONE = (gcc_type) -1;
171 
172 /* Call gdbarch_register_name (GDBARCH, REGNUM) and convert its result
173    to a form suitable for the compiler source.  The register names
174    should not clash with inferior defined macros. */
175 
176 extern std::string compile_register_name_mangled (struct gdbarch *gdbarch,
177 						  int regnum);
178 
179 /* Convert compiler source register name to register number of
180    GDBARCH.  Returned value is always >= 0, function throws an error
181    for non-matching REG_NAME.  */
182 
183 extern int compile_register_name_demangle (struct gdbarch *gdbarch,
184 					   const char *reg_name);
185 
186 /* Type used to hold and pass around the source and object file names
187    to use for compilation.  */
188 class compile_file_names
189 {
190 public:
compile_file_names(std::string source_file,std::string object_file)191   compile_file_names (std::string source_file, std::string object_file)
192     : m_source_file (source_file), m_object_file (object_file)
193   {}
194 
195   /* Provide read-only views only.  Return 'const char *' instead of
196      std::string to avoid having to use c_str() everywhere in client
197      code.  */
198 
source_file()199   const char *source_file () const
200   { return m_source_file.c_str (); }
201 
object_file()202   const char *object_file () const
203   { return m_object_file.c_str (); }
204 
205 private:
206   /* Storage for the file names.  */
207   std::string m_source_file;
208   std::string m_object_file;
209 };
210 
211 #endif /* COMPILE_COMPILE_INTERNAL_H */
212