Rename to allow_rust_tests
[binutils-gdb.git] / gdb / compile / compile-object-run.c
1 /* Call module for 'compile' command.
2
3 Copyright (C) 2014-2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #include "defs.h"
21 #include "compile-object-run.h"
22 #include "value.h"
23 #include "infcall.h"
24 #include "objfiles.h"
25 #include "compile-internal.h"
26 #include "dummy-frame.h"
27 #include "block.h"
28 #include "valprint.h"
29 #include "compile.h"
30
31 /* Helper for do_module_cleanup. */
32
33 struct do_module_cleanup
34 {
35 do_module_cleanup (int *ptr, compile_module_up &&mod)
36 : executedp (ptr),
37 module (std::move (mod))
38 {
39 }
40
41 DISABLE_COPY_AND_ASSIGN (do_module_cleanup);
42
43 /* Boolean to set true upon a call of do_module_cleanup.
44 The pointer may be NULL. */
45 int *executedp;
46
47 /* The compile module. */
48 compile_module_up module;
49 };
50
51 /* Cleanup everything after the inferior function dummy frame gets
52 discarded. */
53
54 static dummy_frame_dtor_ftype do_module_cleanup;
55 static void
56 do_module_cleanup (void *arg, int registers_valid)
57 {
58 struct do_module_cleanup *data = (struct do_module_cleanup *) arg;
59
60 if (data->executedp != NULL)
61 {
62 *data->executedp = 1;
63
64 /* This code cannot be in compile_object_run as OUT_VALUE_TYPE
65 no longer exists there. */
66 if (data->module->scope == COMPILE_I_PRINT_ADDRESS_SCOPE
67 || data->module->scope == COMPILE_I_PRINT_VALUE_SCOPE)
68 {
69 struct value *addr_value;
70 struct type *ptr_type
71 = lookup_pointer_type (data->module->out_value_type);
72
73 addr_value = value_from_pointer (ptr_type,
74 data->module->out_value_addr);
75
76 /* SCOPE_DATA would be stale unless EXECUTEDP != NULL. */
77 compile_print_value (value_ind (addr_value),
78 data->module->scope_data);
79 }
80 }
81
82 objfile *objfile = data->module->objfile;
83 gdb_assert (objfile != nullptr);
84
85 /* We have to make a copy of the name so that we can unlink the
86 underlying file -- removing the objfile will cause the name to be
87 freed, so we can't simply keep a reference to it. */
88 std::string objfile_name_s = objfile_name (objfile);
89
90 objfile->unlink ();
91
92 /* It may be a bit too pervasive in this dummy_frame dtor callback. */
93 clear_symtab_users (0);
94
95 /* Delete the .c file. */
96 unlink (data->module->source_file.c_str ());
97
98 /* Delete the .o file. */
99 unlink (objfile_name_s.c_str ());
100
101 delete data;
102 }
103
104 /* Create a copy of FUNC_TYPE that is independent of OBJFILE. */
105
106 static type *
107 create_copied_type_recursive (objfile *objfile, type *func_type)
108 {
109 htab_up copied_types = create_copied_types_hash ();
110 func_type = copy_type_recursive (func_type, copied_types.get ());
111 return func_type;
112 }
113
114 /* Perform inferior call of MODULE. This function may throw an error.
115 This function may leave files referenced by MODULE on disk until
116 the inferior call dummy frame is discarded. This function may throw errors.
117 Thrown errors and left MODULE files are unrelated events. Caller must no
118 longer touch MODULE's memory after this function has been called. */
119
120 void
121 compile_object_run (compile_module_up &&module)
122 {
123 struct value *func_val;
124 struct do_module_cleanup *data;
125 int dtor_found, executed = 0;
126 struct symbol *func_sym = module->func_sym;
127 CORE_ADDR regs_addr = module->regs_addr;
128 struct objfile *objfile = module->objfile;
129
130 data = new struct do_module_cleanup (&executed, std::move (module));
131
132 try
133 {
134 struct type *func_type = func_sym->type ();
135 int current_arg = 0;
136 struct value **vargs;
137
138 /* OBJFILE may disappear while FUNC_TYPE is still in use as a
139 result of the call to DO_MODULE_CLEANUP below, so we need a copy
140 that does not depend on the objfile in anyway. */
141 func_type = create_copied_type_recursive (objfile, func_type);
142
143 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
144 func_val = value_from_pointer (lookup_pointer_type (func_type),
145 func_sym->value_block ()->entry_pc ());
146
147 vargs = XALLOCAVEC (struct value *, func_type->num_fields ());
148 if (func_type->num_fields () >= 1)
149 {
150 gdb_assert (regs_addr != 0);
151 vargs[current_arg] = value_from_pointer
152 (func_type->field (current_arg).type (), regs_addr);
153 ++current_arg;
154 }
155 if (func_type->num_fields () >= 2)
156 {
157 gdb_assert (data->module->out_value_addr != 0);
158 vargs[current_arg] = value_from_pointer
159 (func_type->field (current_arg).type (),
160 data->module->out_value_addr);
161 ++current_arg;
162 }
163 gdb_assert (current_arg == func_type->num_fields ());
164 auto args = gdb::make_array_view (vargs, func_type->num_fields ());
165 call_function_by_hand_dummy (func_val, NULL, args,
166 do_module_cleanup, data);
167 }
168 catch (const gdb_exception_error &ex)
169 {
170 /* In the case of DTOR_FOUND or in the case of EXECUTED nothing
171 needs to be done. */
172 dtor_found = find_dummy_frame_dtor (do_module_cleanup, data);
173 if (!executed)
174 data->executedp = NULL;
175 gdb_assert (!(dtor_found && executed));
176 if (!dtor_found && !executed)
177 do_module_cleanup (data, 0);
178 throw;
179 }
180
181 dtor_found = find_dummy_frame_dtor (do_module_cleanup, data);
182 gdb_assert (!dtor_found && executed);
183 }