Debian Codegraph @

gcc/m2/plugin/m2rte.cc

raw
1 /* m2rte.cc a plugin to detect runtime exceptions at compiletime.
2
3 Copyright (C) 2017-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Modula-2; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22
23 #include "gcc-plugin.h"
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "options.h"
28 #include "tree-pass.h"
29 #include "diagnostic-core.h"
30 #include "flags.h"
31 #include "intl.h"
32 #include "plugin.h"
33 #include "tree.h"
34 #include "gimple.h"
35 #include "gimplify.h"
36 #include "gimple-iterator.h"
37 #include "gimplify-me.h"
38 #include "gimple-pretty-print.h"
39 #include "plugin-version.h"
40 #include "diagnostic.h"
41 #include "context.h"
42
43 #include "rtegraph.h"
44 extern bool ggc_force_collect;
45 extern void ggc_collect (void);
46
47 #undef DEBUG_BASICBLOCK
48
49 int plugin_is_GPL_compatible;
50
51 void debug_tree (tree);
52
53 /* All dialects of Modula-2 issue some or all of these runtime error calls.
54 This plugin detects whether a runtime error will be called in the first
55 basic block of a reachable function. */
56
57 static const char *m2_runtime_error_calls[] = {
58 "m2pim_M2RTS_AssignmentException",
59 "m2pim_M2RTS_ReturnException",
60 "m2pim_M2RTS_IncException",
61 "m2pim_M2RTS_DecException",
62 "m2pim_M2RTS_InclException",
63 "m2pim_M2RTS_ExclException",
64 "m2pim_M2RTS_ShiftException",
65 "m2pim_M2RTS_RotateException",
66 "m2pim_M2RTS_StaticArraySubscriptException",
67 "m2pim_M2RTS_DynamicArraySubscriptException",
68 "m2pim_M2RTS_ForLoopBeginException",
69 "m2pim_M2RTS_ForLoopToException",
70 "m2pim_M2RTS_ForLoopEndException",
71 "m2pim_M2RTS_PointerNilException",
72 "m2pim_M2RTS_NoReturnException",
73 "m2pim_M2RTS_CaseException",
74 "m2pim_M2RTS_WholeNonPosDivException",
75 "m2pim_M2RTS_WholeNonPosModException",
76 "m2pim_M2RTS_WholeZeroDivException",
77 "m2pim_M2RTS_WholeZeroRemException",
78 "m2pim_M2RTS_WholeValueException",
79 "m2pim_M2RTS_RealValueException",
80 "m2pim_M2RTS_ParameterException",
81 "m2pim_M2RTS_NoException",
82
83 "m2iso_M2RTS_AssignmentException",
84 "m2iso_M2RTS_ReturnException",
85 "m2iso_M2RTS_IncException",
86 "m2iso_M2RTS_DecException",
87 "m2iso_M2RTS_InclException",
88 "m2iso_M2RTS_ExclException",
89 "m2iso_M2RTS_ShiftException",
90 "m2iso_M2RTS_RotateException",
91 "m2iso_M2RTS_StaticArraySubscriptException",
92 "m2iso_M2RTS_DynamicArraySubscriptException",
93 "m2iso_M2RTS_ForLoopBeginException",
94 "m2iso_M2RTS_ForLoopToException",
95 "m2iso_M2RTS_ForLoopEndException",
96 "m2iso_M2RTS_PointerNilException",
97 "m2iso_M2RTS_NoReturnException",
98 "m2iso_M2RTS_CaseException",
99 "m2iso_M2RTS_WholeNonPosDivException",
100 "m2iso_M2RTS_WholeNonPosModException",
101 "m2iso_M2RTS_WholeZeroDivException",
102 "m2iso_M2RTS_WholeZeroRemException",
103 "m2iso_M2RTS_WholeValueException",
104 "m2iso_M2RTS_RealValueException",
105 "m2iso_M2RTS_ParameterException",
106 "m2iso_M2RTS_NoException",
107 NULL,
108 };
109
110
111 #if defined(DEBUG_BASICBLOCK)
112 /* pretty_function display the name of the function. */
113
114 static void
115 pretty_function (tree fndecl)
116 {
117 if (fndecl != NULL && (DECL_NAME (fndecl) != NULL))
118 {
119 const char *n = IDENTIFIER_POINTER (DECL_NAME (fndecl));
120 fprintf (stderr, "PROCEDURE %s ;\n", n);
121 }
122 }
123 #endif
124
125 void
126 print_rtl (FILE *outf, const_rtx rtx_first);
127
128 /* strend returns true if string name has ending. */
129
130 static bool
131 strend (const char *name, const char *ending)
132 {
133 unsigned int len = strlen (name);
134 return (len > strlen (ending)
135 && (strcmp (&name[len-strlen (ending)], ending) == 0));
136 }
137
138 /* is_constructor returns true if the function name is that of a module
139 constructor or deconstructor. */
140
141 static bool
142 is_constructor (tree fndecl)
143 {
144 const char *name = IDENTIFIER_POINTER (DECL_NAME (fndecl));
145 unsigned int len = strlen (name);
146
147 return ((len > strlen ("_M2_"))
148 && (strncmp (name, "_M2_", strlen ("_M2_")) == 0)
149 && (strend (name, "_init") || strend (name, "_finish")));
150 }
151
152 /* is_external returns true if the function is extern. */
153
154 static bool
155 is_external (tree function)
156 {
157 return (! DECL_EXTERNAL (function))
158 && TREE_PUBLIC (function)
159 && TREE_STATIC (function);
160 }
161
162 /* is_external returns true if the function is a call to a Modula-2
163 runtime exception handler. */
164
165 static bool
166 is_rte (tree fndecl)
167 {
168 const char *n = IDENTIFIER_POINTER (DECL_NAME (fndecl));
169
170 for (int i = 0; m2_runtime_error_calls[i] != NULL; i++)
171 if (strcmp (m2_runtime_error_calls[i], n) == 0)
172 return true;
173 return false;
174 }
175
176 /* examine_call extract the function tree from the gimple call
177 statement and check whether it is a call to a runtime exception. */
178
179 static void
180 examine_call (gimple *stmt)
181 {
182 tree fndecl = gimple_call_fndecl (stmt);
183 rtenode *func = rtegraph_lookup (stmt, fndecl, true);
184 // rtegraph_dump ();
185 if (fndecl != NULL && (DECL_NAME (fndecl) != NULL))
186 {
187 /* Firstly check if the function is a runtime exception. */
188 if (is_rte (fndecl))
189 {
190 /* Remember runtime exception call. */
191 rtegraph_include_rtscall (func);
192 /* Add the callee to the list of candidates to be queried reachable. */
193 rtegraph_candidates_include (func);
194 return;
195 }
196 }
197 /* Add it to the list of calls. */
198 rtegraph_include_function_call (func);
199 }
200
201
202 /* examine_function_decl, check if the current function is a module
203 constructor/deconstructor. Also check if the current function is
204 declared as external. */
205
206 static void
207 examine_function_decl (rtenode *rt)
208 {
209 tree fndecl = rtegraph_get_func (rt);
210 if (fndecl != NULL && (DECL_NAME (fndecl) != NULL))
211 {
212 /* Check if the function is a module constructor. */
213 if (is_constructor (fndecl))
214 rtegraph_constructors_include (rt);
215 /* Can it be called externally? */
216 if (is_external (fndecl))
217 rtegraph_externs_include (rt);
218 }
219 }
220
221
222 /* Check and warn if STMT is a self-assign statement. */
223
224 static void
225 runtime_exception_inevitable (gimple *stmt)
226 {
227 if (is_gimple_call (stmt))
228 examine_call (stmt);
229 }
230
231
232 namespace {
233
234 const pass_data pass_data_exception_detection =
235 {
236 GIMPLE_PASS, /* type */
237 "runtime_exception_inevitable", /* name */
238 OPTGROUP_NONE, /* optinfo_flags */
239 TV_NONE, /* tv_id */
240 PROP_gimple_lcf , /* properties_required */
241 0, /* properties_provided */
242 0, /* properties_destroyed */
243 0, /* todo_flags_start */
244 0, /* todo_flags_finish */
245 };
246
247 class pass_warn_exception_inevitable : public gimple_opt_pass
248 {
249 public:
250 pass_warn_exception_inevitable(gcc::context *ctxt)
251 : gimple_opt_pass(pass_data_exception_detection, ctxt)
252 {}
253
254 virtual unsigned int execute (function *);
255 };
256
257 /* execute checks the first basic block of function fun to see if it
258 calls a runtime exception. */
259
260 unsigned int
261 pass_warn_exception_inevitable::execute (function *fun)
262 {
263 gimple_stmt_iterator gsi;
264 basic_block bb;
265 /* Record a function declaration. */
266 rtenode *fn = rtegraph_lookup (fun->gimple_body, fun->decl, false);
267
268 rtegraph_set_current_function (fn);
269 /* Check if the current function is a module constructor/deconstructor.
270 Also check if the current function is declared as external. */
271 examine_function_decl (fn);
272
273 #if defined(DEBUG_BASICBLOCK)
274 pretty_function (fun->decl);
275 int basic_count = 0;
276 #endif
277 FOR_EACH_BB_FN (bb, fun)
278 {
279 #if defined(DEBUG_BASICBLOCK)
280 int stmt_count = 0;
281 #endif
282 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
283 {
284 #if defined(DEBUG_BASICBLOCK)
285 printf (" [%d][%d] [basic block][statement]\n",
286 basic_count, stmt_count);
287 stmt_count++;
288 #endif
289 runtime_exception_inevitable (gsi_stmt (gsi));
290 #if defined(DEBUG_BASICBLOCK)
291 debug (gsi_stmt (gsi));
292 #endif
293 }
294 /* We only care about the first basic block in each function.
295 We could continue to search if this edge falls though (top
296 of a loop for example) but for now this is cautiously safe.
297 --fixme-- */
298 return 0;
299 #if defined(DEBUG_BASICBLOCK)
300 basic_count++;
301 #endif
302 }
303 return 0;
304 }
305
306 /* analyse_graph discovers any reachable call to a runtime exception in the
307 first basic block of a reachable function. It then calls rtegraph_finish
308 to tidy up and return all dynamic memory used. */
309
310 void analyse_graph (void *gcc_data, void *user_data)
311 {
312 rtegraph_discover ();
313 rtegraph_finish ();
314 }
315
316 } // anon namespace
317
318
319 static gimple_opt_pass *
320 make_pass_warn_exception_inevitable (gcc::context *ctxt)
321 {
322 return new pass_warn_exception_inevitable (ctxt);
323 }
324
325
326 /* plugin_init, check the version and register the plugin. */
327
328 int
329 plugin_init (struct plugin_name_args *plugin_info,
330 struct plugin_gcc_version *version)
331 {
332 struct register_pass_info pass_info;
333 const char *plugin_name = plugin_info->base_name;
334
335 if (!plugin_default_version_check (version, &gcc_version))
336 {
337 fprintf (stderr, "incorrect GCC version (%s) this plugin was built for GCC version %s\n",
338 version->basever, gcc_version.basever);
339 return 1;
340 }
341
342 /* Runtime exception inevitable detection. This plugin is most effective if
343 it is run after all optimizations. This is plugged in at the end of
344 gimple range of optimizations. */
345 pass_info.pass = make_pass_warn_exception_inevitable (g);
346 pass_info.reference_pass_name = "*warn_function_noreturn";
347
348 pass_info.ref_pass_instance_number = 1;
349 pass_info.pos_op = PASS_POS_INSERT_AFTER;
350
351 rtegraph_init ();
352
353 register_callback (plugin_name,
354 PLUGIN_PASS_MANAGER_SETUP,
355 NULL,
356 &pass_info);
357 register_callback (plugin_name,
358 PLUGIN_FINISH, analyse_graph, NULL);
359 return 0;
360 }