[PATCH 2/57][Arm][GAS] Add support for MVE instructions: vpst, vadd, vsub and vabd
[binutils-gdb.git] / gdb / compile / compile.c
blob72920642d16677a7a51f70a48aa41bd3e22d5f95
1 /* General Compile and inject code
3 Copyright (C) 2014-2019 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "defs.h"
21 #include "top.h"
22 #include "ui-out.h"
23 #include "command.h"
24 #include "cli/cli-script.h"
25 #include "cli/cli-utils.h"
26 #include "completer.h"
27 #include "gdbcmd.h"
28 #include "compile.h"
29 #include "compile-internal.h"
30 #include "compile-object-load.h"
31 #include "compile-object-run.h"
32 #include "language.h"
33 #include "frame.h"
34 #include "source.h"
35 #include "block.h"
36 #include "arch-utils.h"
37 #include "common/filestuff.h"
38 #include "target.h"
39 #include "osabi.h"
40 #include "common/gdb_wait.h"
41 #include "valprint.h"
42 #include "common/gdb_optional.h"
43 #include "common/gdb_unlinker.h"
44 #include "common/pathstuff.h"
48 /* Initial filename for temporary files. */
50 #define TMP_PREFIX "/tmp/gdbobj-"
52 /* Hold "compile" commands. */
54 static struct cmd_list_element *compile_command_list;
56 /* Debug flag for "compile" commands. */
58 int compile_debug;
60 /* Object of this type are stored in the compiler's symbol_err_map. */
62 struct symbol_error
64 /* The symbol. */
66 const struct symbol *sym;
68 /* The error message to emit. This is malloc'd and owned by the
69 hash table. */
71 char *message;
74 /* Hash a type_map_instance. */
76 static hashval_t
77 hash_type_map_instance (const void *p)
79 const struct type_map_instance *inst = (const struct type_map_instance *) p;
81 return htab_hash_pointer (inst->type);
84 /* Check two type_map_instance objects for equality. */
86 static int
87 eq_type_map_instance (const void *a, const void *b)
89 const struct type_map_instance *insta = (const struct type_map_instance *) a;
90 const struct type_map_instance *instb = (const struct type_map_instance *) b;
92 return insta->type == instb->type;
95 /* Hash function for struct symbol_error. */
97 static hashval_t
98 hash_symbol_error (const void *a)
100 const struct symbol_error *se = (const struct symbol_error *) a;
102 return htab_hash_pointer (se->sym);
105 /* Equality function for struct symbol_error. */
107 static int
108 eq_symbol_error (const void *a, const void *b)
110 const struct symbol_error *sea = (const struct symbol_error *) a;
111 const struct symbol_error *seb = (const struct symbol_error *) b;
113 return sea->sym == seb->sym;
116 /* Deletion function for struct symbol_error. */
118 static void
119 del_symbol_error (void *a)
121 struct symbol_error *se = (struct symbol_error *) a;
123 xfree (se->message);
124 xfree (se);
127 /* Constructor for compile_instance. */
129 compile_instance::compile_instance (struct gcc_base_context *gcc_fe,
130 const char *options)
131 : m_gcc_fe (gcc_fe), m_gcc_target_options (options),
132 m_type_map (htab_create_alloc (10, hash_type_map_instance,
133 eq_type_map_instance,
134 xfree, xcalloc, xfree)),
135 m_symbol_err_map (htab_create_alloc (10, hash_symbol_error,
136 eq_symbol_error, del_symbol_error,
137 xcalloc, xfree))
141 /* See compile-internal.h. */
143 bool
144 compile_instance::get_cached_type (struct type *type, gcc_type *ret) const
146 struct type_map_instance inst, *found;
148 inst.type = type;
149 found = (struct type_map_instance *) htab_find (m_type_map.get (), &inst);
150 if (found != NULL)
152 *ret = found->gcc_type_handle;
153 return true;
156 return false;
159 /* See compile-internal.h. */
161 void
162 compile_instance::insert_type (struct type *type, gcc_type gcc_type)
164 struct type_map_instance inst, *add;
165 void **slot;
167 inst.type = type;
168 inst.gcc_type_handle = gcc_type;
169 slot = htab_find_slot (m_type_map.get (), &inst, INSERT);
171 add = (struct type_map_instance *) *slot;
172 /* The type might have already been inserted in order to handle
173 recursive types. */
174 if (add != NULL && add->gcc_type_handle != gcc_type)
175 error (_("Unexpected type id from GCC, check you use recent enough GCC."));
177 if (add == NULL)
179 add = XNEW (struct type_map_instance);
180 *add = inst;
181 *slot = add;
185 /* See compile-internal.h. */
187 void
188 compile_instance::insert_symbol_error (const struct symbol *sym,
189 const char *text)
191 struct symbol_error e;
192 void **slot;
194 e.sym = sym;
195 slot = htab_find_slot (m_symbol_err_map.get (), &e, INSERT);
196 if (*slot == NULL)
198 struct symbol_error *ep = XNEW (struct symbol_error);
200 ep->sym = sym;
201 ep->message = xstrdup (text);
202 *slot = ep;
206 /* See compile-internal.h. */
208 void
209 compile_instance::error_symbol_once (const struct symbol *sym)
211 struct symbol_error search;
212 struct symbol_error *err;
214 if (m_symbol_err_map == NULL)
215 return;
217 search.sym = sym;
218 err = (struct symbol_error *) htab_find (m_symbol_err_map.get (), &search);
219 if (err == NULL || err->message == NULL)
220 return;
222 gdb::unique_xmalloc_ptr<char> message (err->message);
223 err->message = NULL;
224 error (_("%s"), message.get ());
227 /* Implement "show debug compile". */
229 static void
230 show_compile_debug (struct ui_file *file, int from_tty,
231 struct cmd_list_element *c, const char *value)
233 fprintf_filtered (file, _("Compile debugging is %s.\n"), value);
238 /* Check *ARG for a "-raw" or "-r" argument. Return 0 if not seen.
239 Return 1 if seen and update *ARG. */
241 static int
242 check_raw_argument (const char **arg)
244 *arg = skip_spaces (*arg);
246 if (arg != NULL
247 && (check_for_argument (arg, "-raw", sizeof ("-raw") - 1)
248 || check_for_argument (arg, "-r", sizeof ("-r") - 1)))
249 return 1;
250 return 0;
253 /* Handle the input from the 'compile file' command. The "compile
254 file" command is used to evaluate an expression contained in a file
255 that may contain calls to the GCC compiler. */
257 static void
258 compile_file_command (const char *arg, int from_tty)
260 enum compile_i_scope_types scope = COMPILE_I_SIMPLE_SCOPE;
262 scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
264 /* Check the user did not just <enter> after command. */
265 if (arg == NULL)
266 error (_("You must provide a filename for this command."));
268 /* Check if a raw (-r|-raw) argument is provided. */
269 if (arg != NULL && check_raw_argument (&arg))
271 scope = COMPILE_I_RAW_SCOPE;
272 arg = skip_spaces (arg);
275 /* After processing arguments, check there is a filename at the end
276 of the command. */
277 if (arg[0] == '\0')
278 error (_("You must provide a filename with the raw option set."));
280 if (arg[0] == '-')
281 error (_("Unknown argument specified."));
283 arg = skip_spaces (arg);
284 gdb::unique_xmalloc_ptr<char> abspath = gdb_abspath (arg);
285 std::string buffer = string_printf ("#include \"%s\"\n", abspath.get ());
286 eval_compile_command (NULL, buffer.c_str (), scope, NULL);
289 /* Handle the input from the 'compile code' command. The
290 "compile code" command is used to evaluate an expression that may
291 contain calls to the GCC compiler. The language expected in this
292 compile command is the language currently set in GDB. */
294 static void
295 compile_code_command (const char *arg, int from_tty)
297 enum compile_i_scope_types scope = COMPILE_I_SIMPLE_SCOPE;
299 scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
301 if (arg != NULL && check_raw_argument (&arg))
303 scope = COMPILE_I_RAW_SCOPE;
304 arg = skip_spaces (arg);
307 arg = skip_spaces (arg);
309 if (arg != NULL && !check_for_argument (&arg, "--", sizeof ("--") - 1))
311 if (arg[0] == '-')
312 error (_("Unknown argument specified."));
315 if (arg && *arg)
316 eval_compile_command (NULL, arg, scope, NULL);
317 else
319 counted_command_line l = get_command_line (compile_control, "");
321 l->control_u.compile.scope = scope;
322 execute_control_command_untraced (l.get ());
326 /* Callback for compile_print_command. */
328 void
329 compile_print_value (struct value *val, void *data_voidp)
331 const struct format_data *fmtp = (const struct format_data *) data_voidp;
333 print_value (val, fmtp);
336 /* Handle the input from the 'compile print' command. The "compile
337 print" command is used to evaluate and print an expression that may
338 contain calls to the GCC compiler. The language expected in this
339 compile command is the language currently set in GDB. */
341 static void
342 compile_print_command (const char *arg, int from_tty)
344 enum compile_i_scope_types scope = COMPILE_I_PRINT_ADDRESS_SCOPE;
345 struct format_data fmt;
347 scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
349 /* Passing &FMT as SCOPE_DATA is safe as do_module_cleanup will not
350 touch the stale pointer if compile_object_run has already quit. */
351 print_command_parse_format (&arg, "compile print", &fmt);
353 if (arg && *arg)
354 eval_compile_command (NULL, arg, scope, &fmt);
355 else
357 counted_command_line l = get_command_line (compile_control, "");
359 l->control_u.compile.scope = scope;
360 l->control_u.compile.scope_data = &fmt;
361 execute_control_command_untraced (l.get ());
365 /* A cleanup function to remove a directory and all its contents. */
367 static void
368 do_rmdir (void *arg)
370 const char *dir = (const char *) arg;
371 char *zap;
372 int wstat;
374 gdb_assert (startswith (dir, TMP_PREFIX));
375 zap = concat ("rm -rf ", dir, (char *) NULL);
376 wstat = system (zap);
377 if (wstat == -1 || !WIFEXITED (wstat) || WEXITSTATUS (wstat) != 0)
378 warning (_("Could not remove temporary directory %s"), dir);
379 XDELETEVEC (zap);
382 /* Return the name of the temporary directory to use for .o files, and
383 arrange for the directory to be removed at shutdown. */
385 static const char *
386 get_compile_file_tempdir (void)
388 static char *tempdir_name;
390 #define TEMPLATE TMP_PREFIX "XXXXXX"
391 char tname[sizeof (TEMPLATE)];
393 if (tempdir_name != NULL)
394 return tempdir_name;
396 strcpy (tname, TEMPLATE);
397 #undef TEMPLATE
398 tempdir_name = mkdtemp (tname);
399 if (tempdir_name == NULL)
400 perror_with_name (_("Could not make temporary directory"));
402 tempdir_name = xstrdup (tempdir_name);
403 make_final_cleanup (do_rmdir, tempdir_name);
404 return tempdir_name;
407 /* Compute the names of source and object files to use. */
409 static compile_file_names
410 get_new_file_names ()
412 static int seq;
413 const char *dir = get_compile_file_tempdir ();
415 ++seq;
417 return compile_file_names (string_printf ("%s%sout%d.c",
418 dir, SLASH_STRING, seq),
419 string_printf ("%s%sout%d.o",
420 dir, SLASH_STRING, seq));
423 /* Get the block and PC at which to evaluate an expression. */
425 static const struct block *
426 get_expr_block_and_pc (CORE_ADDR *pc)
428 const struct block *block = get_selected_block (pc);
430 if (block == NULL)
432 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
434 if (cursal.symtab)
435 block = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (cursal.symtab),
436 STATIC_BLOCK);
437 if (block != NULL)
438 *pc = BLOCK_ENTRY_PC (block);
440 else
441 *pc = BLOCK_ENTRY_PC (block);
443 return block;
446 /* Call buildargv (via gdb_argv), set its result for S into *ARGVP but
447 calculate also the number of parsed arguments into *ARGCP. If
448 buildargv has returned NULL then *ARGCP is set to zero. */
450 static void
451 build_argc_argv (const char *s, int *argcp, char ***argvp)
453 gdb_argv args (s);
455 *argcp = args.count ();
456 *argvp = args.release ();
459 /* String for 'set compile-args' and 'show compile-args'. */
460 static char *compile_args;
462 /* Parsed form of COMPILE_ARGS. COMPILE_ARGS_ARGV is NULL terminated. */
463 static int compile_args_argc;
464 static char **compile_args_argv;
466 /* Implement 'set compile-args'. */
468 static void
469 set_compile_args (const char *args, int from_tty, struct cmd_list_element *c)
471 freeargv (compile_args_argv);
472 build_argc_argv (compile_args, &compile_args_argc, &compile_args_argv);
475 /* Implement 'show compile-args'. */
477 static void
478 show_compile_args (struct ui_file *file, int from_tty,
479 struct cmd_list_element *c, const char *value)
481 fprintf_filtered (file, _("Compile command command-line arguments "
482 "are \"%s\".\n"),
483 value);
486 /* Append ARGC and ARGV (as parsed by build_argc_argv) to *ARGCP and *ARGVP.
487 ARGCP+ARGVP can be zero+NULL and also ARGC+ARGV can be zero+NULL. */
489 static void
490 append_args (int *argcp, char ***argvp, int argc, char **argv)
492 int argi;
494 *argvp = XRESIZEVEC (char *, *argvp, (*argcp + argc + 1));
496 for (argi = 0; argi < argc; argi++)
497 (*argvp)[(*argcp)++] = xstrdup (argv[argi]);
498 (*argvp)[(*argcp)] = NULL;
501 /* String for 'set compile-gcc' and 'show compile-gcc'. */
502 static char *compile_gcc;
504 /* Implement 'show compile-gcc'. */
506 static void
507 show_compile_gcc (struct ui_file *file, int from_tty,
508 struct cmd_list_element *c, const char *value)
510 fprintf_filtered (file, _("Compile command GCC driver filename is \"%s\".\n"),
511 value);
514 /* Return DW_AT_producer parsed for get_selected_frame () (if any).
515 Return NULL otherwise.
517 GCC already filters its command-line arguments only for the suitable ones to
518 put into DW_AT_producer - see GCC function gen_producer_string. */
520 static const char *
521 get_selected_pc_producer_options (void)
523 CORE_ADDR pc = get_frame_pc (get_selected_frame (NULL));
524 struct compunit_symtab *symtab = find_pc_compunit_symtab (pc);
525 const char *cs;
527 if (symtab == NULL || symtab->producer == NULL
528 || !startswith (symtab->producer, "GNU "))
529 return NULL;
531 cs = symtab->producer;
532 while (*cs != 0 && *cs != '-')
533 cs = skip_spaces (skip_to_space (cs));
534 if (*cs != '-')
535 return NULL;
536 return cs;
539 /* Filter out unwanted options from *ARGCP and ARGV. */
541 static void
542 filter_args (int *argcp, char **argv)
544 char **destv;
546 for (destv = argv; *argv != NULL; argv++)
548 /* -fpreprocessed may get in commonly from ccache. */
549 if (strcmp (*argv, "-fpreprocessed") == 0)
551 xfree (*argv);
552 (*argcp)--;
553 continue;
555 *destv++ = *argv;
557 *destv = NULL;
560 /* Produce final vector of GCC compilation options.
562 The first element of the combined argument vector are arguments
563 relating to the target size ("-m64", "-m32" etc.). These are
564 sourced from the inferior's architecture.
566 The second element of the combined argument vector are arguments
567 stored in the inferior DW_AT_producer section. If these are stored
568 in the inferior (there is no guarantee that they are), they are
569 added to the vector.
571 The third element of the combined argument vector are argument
572 supplied by the language implementation provided by
573 compile-{lang}-support. These contain language specific arguments.
575 The final element of the combined argument vector are arguments
576 supplied by the "set compile-args" command. These are always
577 appended last so as to override any of the arguments automatically
578 generated above. */
580 static void
581 get_args (const compile_instance *compiler, struct gdbarch *gdbarch,
582 int *argcp, char ***argvp)
584 const char *cs_producer_options;
585 int argc_compiler;
586 char **argv_compiler;
588 build_argc_argv (gdbarch_gcc_target_options (gdbarch),
589 argcp, argvp);
591 cs_producer_options = get_selected_pc_producer_options ();
592 if (cs_producer_options != NULL)
594 int argc_producer;
595 char **argv_producer;
597 build_argc_argv (cs_producer_options, &argc_producer, &argv_producer);
598 filter_args (&argc_producer, argv_producer);
599 append_args (argcp, argvp, argc_producer, argv_producer);
600 freeargv (argv_producer);
603 build_argc_argv (compiler->gcc_target_options ().c_str (),
604 &argc_compiler, &argv_compiler);
605 append_args (argcp, argvp, argc_compiler, argv_compiler);
606 freeargv (argv_compiler);
608 append_args (argcp, argvp, compile_args_argc, compile_args_argv);
611 /* A helper function suitable for use as the "print_callback" in the
612 compiler object. */
614 static void
615 print_callback (void *ignore, const char *message)
617 fputs_filtered (message, gdb_stderr);
620 /* Process the compilation request. On success it returns the object
621 and source file names. On an error condition, error () is
622 called. */
624 static compile_file_names
625 compile_to_object (struct command_line *cmd, const char *cmd_string,
626 enum compile_i_scope_types scope)
628 const struct block *expr_block;
629 CORE_ADDR trash_pc, expr_pc;
630 int argc;
631 char **argv;
632 int ok;
633 struct gdbarch *gdbarch = get_current_arch ();
634 std::string triplet_rx;
636 if (!target_has_execution)
637 error (_("The program must be running for the compile command to "\
638 "work."));
640 expr_block = get_expr_block_and_pc (&trash_pc);
641 expr_pc = get_frame_address_in_block (get_selected_frame (NULL));
643 /* Set up instance and context for the compiler. */
644 if (current_language->la_get_compile_instance == NULL)
645 error (_("No compiler support for language %s."),
646 current_language->la_name);
648 compile_instance *compiler_instance
649 = current_language->la_get_compile_instance ();
650 std::unique_ptr<compile_instance> compiler (compiler_instance);
651 compiler->set_print_callback (print_callback, NULL);
652 compiler->set_scope (scope);
653 compiler->set_block (expr_block);
655 /* From the provided expression, build a scope to pass to the
656 compiler. */
658 string_file input_buf;
659 const char *input;
661 if (cmd != NULL)
663 struct command_line *iter;
665 for (iter = cmd->body_list_0.get (); iter; iter = iter->next)
667 input_buf.puts (iter->line);
668 input_buf.puts ("\n");
671 input = input_buf.c_str ();
673 else if (cmd_string != NULL)
674 input = cmd_string;
675 else
676 error (_("Neither a simple expression, or a multi-line specified."));
678 std::string code
679 = current_language->la_compute_program (compiler.get (), input, gdbarch,
680 expr_block, expr_pc);
681 if (compile_debug)
682 fprintf_unfiltered (gdb_stdlog, "debug output:\n\n%s", code.c_str ());
684 compiler->set_verbose (compile_debug);
686 if (compile_gcc[0] != 0)
688 if (compiler->version () < GCC_FE_VERSION_1)
689 error (_("Command 'set compile-gcc' requires GCC version 6 or higher "
690 "(libcc1 interface version 1 or higher)"));
692 compiler->set_driver_filename (compile_gcc);
694 else
696 const char *os_rx = osabi_triplet_regexp (gdbarch_osabi (gdbarch));
697 const char *arch_rx = gdbarch_gnu_triplet_regexp (gdbarch);
699 /* Allow triplets with or without vendor set. */
700 triplet_rx = std::string (arch_rx) + "(-[^-]*)?-" + os_rx;
701 compiler->set_triplet_regexp (triplet_rx.c_str ());
704 /* Set compiler command-line arguments. */
705 get_args (compiler.get (), gdbarch, &argc, &argv);
706 gdb_argv argv_holder (argv);
708 gdb::unique_xmalloc_ptr<char> error_message;
709 error_message.reset (compiler->set_arguments (argc, argv,
710 triplet_rx.c_str ()));
712 if (error_message != NULL)
713 error ("%s", error_message.get ());
715 if (compile_debug)
717 int argi;
719 fprintf_unfiltered (gdb_stdlog, "Passing %d compiler options:\n", argc);
720 for (argi = 0; argi < argc; argi++)
721 fprintf_unfiltered (gdb_stdlog, "Compiler option %d: <%s>\n",
722 argi, argv[argi]);
725 compile_file_names fnames = get_new_file_names ();
727 gdb::optional<gdb::unlinker> source_remover;
730 gdb_file_up src = gdb_fopen_cloexec (fnames.source_file (), "w");
731 if (src == NULL)
732 perror_with_name (_("Could not open source file for writing"));
734 source_remover.emplace (fnames.source_file ());
736 if (fputs (code.c_str (), src.get ()) == EOF)
737 perror_with_name (_("Could not write to source file"));
740 if (compile_debug)
741 fprintf_unfiltered (gdb_stdlog, "source file produced: %s\n\n",
742 fnames.source_file ());
744 /* Call the compiler and start the compilation process. */
745 compiler->set_source_file (fnames.source_file ());
746 ok = compiler->compile (fnames.object_file (), compile_debug);
747 if (!ok)
748 error (_("Compilation failed."));
750 if (compile_debug)
751 fprintf_unfiltered (gdb_stdlog, "object file produced: %s\n\n",
752 fnames.object_file ());
754 /* Keep the source file. */
755 source_remover->keep ();
756 return fnames;
759 /* The "compile" prefix command. */
761 static void
762 compile_command (const char *args, int from_tty)
764 /* If a sub-command is not specified to the compile prefix command,
765 assume it is a direct code compilation. */
766 compile_code_command (args, from_tty);
769 /* See compile.h. */
771 void
772 eval_compile_command (struct command_line *cmd, const char *cmd_string,
773 enum compile_i_scope_types scope, void *scope_data)
775 struct compile_module *compile_module;
777 compile_file_names fnames = compile_to_object (cmd, cmd_string, scope);
779 gdb::unlinker object_remover (fnames.object_file ());
780 gdb::unlinker source_remover (fnames.source_file ());
782 compile_module = compile_object_load (fnames, scope, scope_data);
783 if (compile_module == NULL)
785 gdb_assert (scope == COMPILE_I_PRINT_ADDRESS_SCOPE);
786 eval_compile_command (cmd, cmd_string,
787 COMPILE_I_PRINT_VALUE_SCOPE, scope_data);
788 return;
791 /* Keep the files. */
792 source_remover.keep ();
793 object_remover.keep ();
795 compile_object_run (compile_module);
798 /* See compile/compile-internal.h. */
800 std::string
801 compile_register_name_mangled (struct gdbarch *gdbarch, int regnum)
803 const char *regname = gdbarch_register_name (gdbarch, regnum);
805 return string_printf ("__%s", regname);
808 /* See compile/compile-internal.h. */
811 compile_register_name_demangle (struct gdbarch *gdbarch,
812 const char *regname)
814 int regnum;
816 if (regname[0] != '_' || regname[1] != '_')
817 error (_("Invalid register name \"%s\"."), regname);
818 regname += 2;
820 for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
821 if (strcmp (regname, gdbarch_register_name (gdbarch, regnum)) == 0)
822 return regnum;
824 error (_("Cannot find gdbarch register \"%s\"."), regname);
827 /* Forwards to the plug-in. */
829 #define FORWARD(OP,...) (m_gcc_fe->ops->OP (m_gcc_fe, ##__VA_ARGS__))
831 /* See compile-internal.h. */
833 void
834 compile_instance::set_print_callback
835 (void (*print_function) (void *, const char *), void *datum)
837 FORWARD (set_print_callback, print_function, datum);
840 /* See compile-internal.h. */
842 unsigned int
843 compile_instance::version () const
845 return m_gcc_fe->ops->version;
848 /* See compile-internal.h. */
850 void
851 compile_instance::set_verbose (int level)
853 if (version () >= GCC_FE_VERSION_1)
854 FORWARD (set_verbose, level);
857 /* See compile-internal.h. */
859 void
860 compile_instance::set_driver_filename (const char *filename)
862 if (version () >= GCC_FE_VERSION_1)
863 FORWARD (set_driver_filename, filename);
866 /* See compile-internal.h. */
868 void
869 compile_instance::set_triplet_regexp (const char *regexp)
871 if (version () >= GCC_FE_VERSION_1)
872 FORWARD (set_triplet_regexp, regexp);
875 /* See compile-internal.h. */
877 char *
878 compile_instance::set_arguments (int argc, char **argv, const char *regexp)
880 if (version () >= GCC_FE_VERSION_1)
881 return FORWARD (set_arguments, argc, argv);
882 else
883 return FORWARD (set_arguments_v0, regexp, argc, argv);
886 /* See compile-internal.h. */
888 void
889 compile_instance::set_source_file (const char *filename)
891 FORWARD (set_source_file, filename);
894 /* See compile-internal.h. */
896 bool
897 compile_instance::compile (const char *filename, int verbose_level)
899 if (version () >= GCC_FE_VERSION_1)
900 return FORWARD (compile, filename);
901 else
902 return FORWARD (compile_v0, filename, verbose_level);
905 #undef FORWARD
907 /* See compile.h. */
908 cmd_list_element *compile_cmd_element = nullptr;
910 void
911 _initialize_compile (void)
913 struct cmd_list_element *c = NULL;
915 compile_cmd_element = add_prefix_cmd ("compile", class_obscure,
916 compile_command, _("\
917 Command to compile source code and inject it into the inferior."),
918 &compile_command_list, "compile ", 1, &cmdlist);
919 add_com_alias ("expression", "compile", class_obscure, 0);
921 add_cmd ("code", class_obscure, compile_code_command,
922 _("\
923 Compile, inject, and execute code.\n\
925 Usage: compile code [-r|-raw] [--] [CODE]\n\
926 -r|-raw: Suppress automatic 'void _gdb_expr () { CODE }' wrapping.\n\
927 --: Do not parse any options beyond this delimiter. All text to the\n\
928 right will be treated as source code.\n\
930 The source code may be specified as a simple one line expression, e.g.:\n\
932 compile code printf(\"Hello world\\n\");\n\
934 Alternatively, you can type a multiline expression by invoking\n\
935 this command with no argument. GDB will then prompt for the\n\
936 expression interactively; type a line containing \"end\" to\n\
937 indicate the end of the expression."),
938 &compile_command_list);
940 c = add_cmd ("file", class_obscure, compile_file_command,
941 _("\
942 Evaluate a file containing source code.\n\
944 Usage: compile file [-r|-raw] [FILENAME]\n\
945 -r|-raw: Suppress automatic 'void _gdb_expr () { CODE }' wrapping."),
946 &compile_command_list);
947 set_cmd_completer (c, filename_completer);
949 add_cmd ("print", class_obscure, compile_print_command,
950 _("\
951 Evaluate EXPR by using the compiler and print result.\n\
953 Usage: compile print[/FMT] [EXPR]\n\
955 The expression may be specified on the same line as the command, e.g.:\n\
957 compile print i\n\
959 Alternatively, you can type a multiline expression by invoking\n\
960 this command with no argument. GDB will then prompt for the\n\
961 expression interactively; type a line containing \"end\" to\n\
962 indicate the end of the expression.\n\
964 EXPR may be preceded with /FMT, where FMT is a format letter\n\
965 but no count or size letter (see \"x\" command)."),
966 &compile_command_list);
968 add_setshow_boolean_cmd ("compile", class_maintenance, &compile_debug, _("\
969 Set compile command debugging."), _("\
970 Show compile command debugging."), _("\
971 When on, compile command debugging is enabled."),
972 NULL, show_compile_debug,
973 &setdebuglist, &showdebuglist);
975 add_setshow_string_cmd ("compile-args", class_support,
976 &compile_args,
977 _("Set compile command GCC command-line arguments"),
978 _("Show compile command GCC command-line arguments"),
979 _("\
980 Use options like -I (include file directory) or ABI settings.\n\
981 String quoting is parsed like in shell, for example:\n\
982 -mno-align-double \"-I/dir with a space/include\""),
983 set_compile_args, show_compile_args, &setlist, &showlist);
985 /* Override flags possibly coming from DW_AT_producer. */
986 compile_args = xstrdup ("-O0 -gdwarf-4"
987 /* We use -fPIE Otherwise GDB would need to reserve space large enough for
988 any object file in the inferior in advance to get the final address when
989 to link the object file to and additionally the default system linker
990 script would need to be modified so that one can specify there the
991 absolute target address.
992 -fPIC is not used at is would require from GDB to generate .got. */
993 " -fPIE"
994 /* We want warnings, except for some commonly happening for GDB commands. */
995 " -Wall "
996 " -Wno-unused-but-set-variable"
997 " -Wno-unused-variable"
998 /* Override CU's possible -fstack-protector-strong. */
999 " -fno-stack-protector"
1001 set_compile_args (compile_args, 0, NULL);
1003 add_setshow_optional_filename_cmd ("compile-gcc", class_support,
1004 &compile_gcc,
1005 _("Set compile command "
1006 "GCC driver filename"),
1007 _("Show compile command "
1008 "GCC driver filename"),
1009 _("\
1010 It should be absolute filename of the gcc executable.\n\
1011 If empty the default target triplet will be searched in $PATH."),
1012 NULL, show_compile_gcc, &setlist,
1013 &showlist);
1014 compile_gcc = xstrdup ("");