compile: New 'compile print'
[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 #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 /* Boolean to set true upon a call of do_module_cleanup.
36 The pointer may be NULL. */
37 int *executedp;
38
39 /* .c file OBJFILE was built from. It needs to be xfree-d. */
40 char *source_file;
41
42 /* Copy from struct compile_module. */
43 enum compile_i_scope_types scope;
44 void *scope_data;
45
46 /* Copy from struct compile_module. */
47 struct type *out_value_type;
48 CORE_ADDR out_value_addr;
49
50 /* objfile_name of our objfile. */
51 char objfile_name_string[1];
52 };
53
54 /* Cleanup everything after the inferior function dummy frame gets
55 discarded. */
56
57 static dummy_frame_dtor_ftype do_module_cleanup;
58 static void
59 do_module_cleanup (void *arg, int registers_valid)
60 {
61 struct do_module_cleanup *data = arg;
62 struct objfile *objfile;
63
64 if (data->executedp != NULL)
65 {
66 *data->executedp = 1;
67
68 /* This code cannot be in compile_object_run as OUT_VALUE_TYPE
69 no longer exists there. */
70 if (data->scope == COMPILE_I_PRINT_ADDRESS_SCOPE
71 || data->scope == COMPILE_I_PRINT_VALUE_SCOPE)
72 {
73 struct value *addr_value;
74 struct type *ptr_type = lookup_pointer_type (data->out_value_type);
75
76 addr_value = value_from_pointer (ptr_type, data->out_value_addr);
77
78 /* SCOPE_DATA would be stale unlesse EXECUTEDP != NULL. */
79 compile_print_value (value_ind (addr_value), data->scope_data);
80 }
81 }
82
83 ALL_OBJFILES (objfile)
84 if ((objfile->flags & OBJF_USERLOADED) == 0
85 && (strcmp (objfile_name (objfile), data->objfile_name_string) == 0))
86 {
87 free_objfile (objfile);
88
89 /* It may be a bit too pervasive in this dummy_frame dtor callback. */
90 clear_symtab_users (0);
91
92 break;
93 }
94
95 /* Delete the .c file. */
96 unlink (data->source_file);
97 xfree (data->source_file);
98
99 /* Delete the .o file. */
100 unlink (data->objfile_name_string);
101 xfree (data);
102 }
103
104 /* Perform inferior call of MODULE. This function may throw an error.
105 This function may leave files referenced by MODULE on disk until
106 the inferior call dummy frame is discarded. This function may throw errors.
107 Thrown errors and left MODULE files are unrelated events. Caller must no
108 longer touch MODULE's memory after this function has been called. */
109
110 void
111 compile_object_run (struct compile_module *module)
112 {
113 struct value *func_val;
114 struct frame_id dummy_id;
115 struct cleanup *cleanups;
116 struct do_module_cleanup *data;
117 const char *objfile_name_s = objfile_name (module->objfile);
118 int dtor_found, executed = 0;
119 struct symbol *func_sym = module->func_sym;
120 CORE_ADDR regs_addr = module->regs_addr;
121 struct objfile *objfile = module->objfile;
122
123 data = xmalloc (sizeof (*data) + strlen (objfile_name_s));
124 data->executedp = &executed;
125 data->source_file = xstrdup (module->source_file);
126 strcpy (data->objfile_name_string, objfile_name_s);
127 data->scope = module->scope;
128 data->scope_data = module->scope_data;
129 data->out_value_type = module->out_value_type;
130 data->out_value_addr = module->out_value_addr;
131
132 xfree (module->source_file);
133 xfree (module);
134 module = NULL;
135
136 TRY
137 {
138 struct type *func_type = SYMBOL_TYPE (func_sym);
139 htab_t copied_types;
140 int current_arg = 0;
141 struct value **vargs;
142
143 /* OBJFILE may disappear while FUNC_TYPE still will be in use. */
144 copied_types = create_copied_types_hash (objfile);
145 func_type = copy_type_recursive (objfile, func_type, copied_types);
146 htab_delete (copied_types);
147
148 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
149 func_val = value_from_pointer (lookup_pointer_type (func_type),
150 BLOCK_START (SYMBOL_BLOCK_VALUE (func_sym)));
151
152 vargs = alloca (sizeof (*vargs) * TYPE_NFIELDS (func_type));
153 if (TYPE_NFIELDS (func_type) >= 1)
154 {
155 gdb_assert (regs_addr != 0);
156 vargs[current_arg] = value_from_pointer
157 (TYPE_FIELD_TYPE (func_type, current_arg), regs_addr);
158 ++current_arg;
159 }
160 if (TYPE_NFIELDS (func_type) >= 2)
161 {
162 gdb_assert (data->out_value_addr != 0);
163 vargs[current_arg] = value_from_pointer
164 (TYPE_FIELD_TYPE (func_type, current_arg), data->out_value_addr);
165 ++current_arg;
166 }
167 gdb_assert (current_arg == TYPE_NFIELDS (func_type));
168 call_function_by_hand_dummy (func_val, TYPE_NFIELDS (func_type), vargs,
169 do_module_cleanup, data);
170 }
171 CATCH (ex, RETURN_MASK_ERROR)
172 {
173 /* In the case of DTOR_FOUND or in the case of EXECUTED nothing
174 needs to be done. */
175 dtor_found = find_dummy_frame_dtor (do_module_cleanup, data);
176 if (!executed)
177 data->executedp = NULL;
178 gdb_assert (!(dtor_found && executed));
179 if (!dtor_found && !executed)
180 do_module_cleanup (data, 0);
181 throw_exception (ex);
182 }
183 END_CATCH
184
185 dtor_found = find_dummy_frame_dtor (do_module_cleanup, data);
186 gdb_assert (!dtor_found && executed);
187 }
This page took 0.041488 seconds and 5 git commands to generate.