compile: Distribute scope, add scope_data
[deliverable/binutils-gdb.git] / gdb / compile / compile-object-run.c
1 /* Call module for 'compile' command.
2
3 Copyright (C) 2014-2015 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
28 /* Helper for do_module_cleanup. */
29
30 struct do_module_cleanup
31 {
32 /* Boolean to set true upon a call of do_module_cleanup.
33 The pointer may be NULL. */
34 int *executedp;
35
36 /* .c file OBJFILE was built from. It needs to be xfree-d. */
37 char *source_file;
38
39 /* Copy from struct compile_module. */
40 enum compile_i_scope_types scope;
41 void *scope_data;
42
43 /* objfile_name of our objfile. */
44 char objfile_name_string[1];
45 };
46
47 /* Cleanup everything after the inferior function dummy frame gets
48 discarded. */
49
50 static dummy_frame_dtor_ftype do_module_cleanup;
51 static void
52 do_module_cleanup (void *arg, int registers_valid)
53 {
54 struct do_module_cleanup *data = arg;
55 struct objfile *objfile;
56
57 if (data->executedp != NULL)
58 *data->executedp = 1;
59
60 ALL_OBJFILES (objfile)
61 if ((objfile->flags & OBJF_USERLOADED) == 0
62 && (strcmp (objfile_name (objfile), data->objfile_name_string) == 0))
63 {
64 free_objfile (objfile);
65
66 /* It may be a bit too pervasive in this dummy_frame dtor callback. */
67 clear_symtab_users (0);
68
69 break;
70 }
71
72 /* Delete the .c file. */
73 unlink (data->source_file);
74 xfree (data->source_file);
75
76 /* Delete the .o file. */
77 unlink (data->objfile_name_string);
78 xfree (data);
79 }
80
81 /* Perform inferior call of MODULE. This function may throw an error.
82 This function may leave files referenced by MODULE on disk until
83 the inferior call dummy frame is discarded. This function may throw errors.
84 Thrown errors and left MODULE files are unrelated events. Caller must no
85 longer touch MODULE's memory after this function has been called. */
86
87 void
88 compile_object_run (struct compile_module *module)
89 {
90 struct value *func_val;
91 struct frame_id dummy_id;
92 struct cleanup *cleanups;
93 struct do_module_cleanup *data;
94 const char *objfile_name_s = objfile_name (module->objfile);
95 int dtor_found, executed = 0;
96 CORE_ADDR func_addr = module->func_addr;
97 CORE_ADDR regs_addr = module->regs_addr;
98
99 data = xmalloc (sizeof (*data) + strlen (objfile_name_s));
100 data->executedp = &executed;
101 data->source_file = xstrdup (module->source_file);
102 strcpy (data->objfile_name_string, objfile_name_s);
103 data->scope = module->scope;
104 data->scope_data = module->scope_data;
105
106 xfree (module->source_file);
107 xfree (module);
108
109 TRY
110 {
111 func_val = value_from_pointer
112 (builtin_type (target_gdbarch ())->builtin_func_ptr,
113 func_addr);
114
115 if (regs_addr == 0)
116 call_function_by_hand_dummy (func_val, 0, NULL,
117 do_module_cleanup, data);
118 else
119 {
120 struct value *arg_val;
121
122 arg_val = value_from_pointer
123 (builtin_type (target_gdbarch ())->builtin_func_ptr,
124 regs_addr);
125 call_function_by_hand_dummy (func_val, 1, &arg_val,
126 do_module_cleanup, data);
127 }
128 }
129 CATCH (ex, RETURN_MASK_ERROR)
130 {
131 /* In the case of DTOR_FOUND or in the case of EXECUTED nothing
132 needs to be done. */
133 dtor_found = find_dummy_frame_dtor (do_module_cleanup, data);
134 if (!executed)
135 data->executedp = NULL;
136 gdb_assert (!(dtor_found && executed));
137 if (!dtor_found && !executed)
138 do_module_cleanup (data, 0);
139 throw_exception (ex);
140 }
141 END_CATCH
142
143 dtor_found = find_dummy_frame_dtor (do_module_cleanup, data);
144 gdb_assert (!dtor_found && executed);
145 }
This page took 0.033202 seconds and 5 git commands to generate.