1 /* Call module for 'compile' command.
3 Copyright (C) 2014-2024 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 "compile-object-run.h"
24 #include "compile-internal.h"
25 #include "dummy-frame.h"
30 /* Helper for do_module_cleanup. */
32 struct do_module_cleanup
34 do_module_cleanup (int *ptr
, compile_module_up
&&mod
)
36 module (std::move (mod
))
40 DISABLE_COPY_AND_ASSIGN (do_module_cleanup
);
42 /* Boolean to set true upon a call of do_module_cleanup.
43 The pointer may be NULL. */
46 /* The compile module. */
47 compile_module_up module
;
50 /* Cleanup everything after the inferior function dummy frame gets
53 static dummy_frame_dtor_ftype do_module_cleanup
;
55 do_module_cleanup (void *arg
, int registers_valid
)
57 struct do_module_cleanup
*data
= (struct do_module_cleanup
*) arg
;
59 if (data
->executedp
!= NULL
)
63 /* This code cannot be in compile_object_run as OUT_VALUE_TYPE
64 no longer exists there. */
65 if (data
->module
->scope
== COMPILE_I_PRINT_ADDRESS_SCOPE
66 || data
->module
->scope
== COMPILE_I_PRINT_VALUE_SCOPE
)
68 struct value
*addr_value
;
70 = lookup_pointer_type (data
->module
->out_value_type
);
72 addr_value
= value_from_pointer (ptr_type
,
73 data
->module
->out_value_addr
);
75 /* SCOPE_DATA would be stale unless EXECUTEDP != NULL. */
76 compile_print_value (value_ind (addr_value
),
77 data
->module
->scope_data
);
81 objfile
*objfile
= data
->module
->objfile
;
82 gdb_assert (objfile
!= nullptr);
84 /* We have to make a copy of the name so that we can unlink the
85 underlying file -- removing the objfile will cause the name to be
86 freed, so we can't simply keep a reference to it. */
87 std::string objfile_name_s
= objfile_name (objfile
);
91 /* It may be a bit too pervasive in this dummy_frame dtor callback. */
92 clear_symtab_users (0);
94 /* Delete the .c file. */
95 unlink (data
->module
->source_file
.c_str ());
97 /* Delete the .o file. */
98 unlink (objfile_name_s
.c_str ());
103 /* Create a copy of FUNC_TYPE that is independent of OBJFILE. */
106 create_copied_type_recursive (objfile
*objfile
, type
*func_type
)
108 htab_up copied_types
= create_copied_types_hash ();
109 func_type
= copy_type_recursive (func_type
, copied_types
.get ());
113 /* Perform inferior call of MODULE. This function may throw an error.
114 This function may leave files referenced by MODULE on disk until
115 the inferior call dummy frame is discarded. This function may throw errors.
116 Thrown errors and left MODULE files are unrelated events. Caller must no
117 longer touch MODULE's memory after this function has been called. */
120 compile_object_run (compile_module_up
&&module
)
122 struct value
*func_val
;
123 struct do_module_cleanup
*data
;
124 int dtor_found
, executed
= 0;
125 struct symbol
*func_sym
= module
->func_sym
;
126 CORE_ADDR regs_addr
= module
->regs_addr
;
127 struct objfile
*objfile
= module
->objfile
;
129 data
= new struct do_module_cleanup (&executed
, std::move (module
));
133 struct type
*func_type
= func_sym
->type ();
135 struct value
**vargs
;
137 /* OBJFILE may disappear while FUNC_TYPE is still in use as a
138 result of the call to DO_MODULE_CLEANUP below, so we need a copy
139 that does not depend on the objfile in anyway. */
140 func_type
= create_copied_type_recursive (objfile
, func_type
);
142 gdb_assert (func_type
->code () == TYPE_CODE_FUNC
);
143 func_val
= value_from_pointer (lookup_pointer_type (func_type
),
144 func_sym
->value_block ()->entry_pc ());
146 vargs
= XALLOCAVEC (struct value
*, func_type
->num_fields ());
147 if (func_type
->num_fields () >= 1)
149 gdb_assert (regs_addr
!= 0);
150 vargs
[current_arg
] = value_from_pointer
151 (func_type
->field (current_arg
).type (), regs_addr
);
154 if (func_type
->num_fields () >= 2)
156 gdb_assert (data
->module
->out_value_addr
!= 0);
157 vargs
[current_arg
] = value_from_pointer
158 (func_type
->field (current_arg
).type (),
159 data
->module
->out_value_addr
);
162 gdb_assert (current_arg
== func_type
->num_fields ());
163 auto args
= gdb::make_array_view (vargs
, func_type
->num_fields ());
164 call_function_by_hand_dummy (func_val
, NULL
, args
,
165 do_module_cleanup
, data
);
167 catch (const gdb_exception_error
&ex
)
169 /* In the case of DTOR_FOUND or in the case of EXECUTED nothing
171 dtor_found
= find_dummy_frame_dtor (do_module_cleanup
, data
);
173 data
->executedp
= NULL
;
174 gdb_assert (!(dtor_found
&& executed
));
175 if (!dtor_found
&& !executed
)
176 do_module_cleanup (data
, 0);
180 dtor_found
= find_dummy_frame_dtor (do_module_cleanup
, data
);
181 gdb_assert (!dtor_found
&& executed
);