Debian Codegraph @

gcc/cobol/except.cc

raw
1 /*
2 * Copyright (c) 2021-2025 Symas Corporation
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of the Symas Corporation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "cobol-system.h"
33 #include "coretypes.h"
34 #include "tree.h"
35 #include "../../libgcobol/ec.h"
36 #include "../../libgcobol/common-defs.h"
37 #include "util.h"
38 #include "cbldiag.h"
39 #include "symbols.h"
40 #include "inspect.h"
41 #include "../../libgcobol/io.h"
42 #include "genapi.h"
43 #include "gengen.h"
44 #include "../../libgcobol/exceptl.h"
45 #include "util.h"
46 #include "genutil.h"
47
48 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
49
50 static const ec_descr_t *
51 ec_type_descr( ec_type_t type ) {
52 auto p = std::find( __gg__exception_table, __gg__exception_table_end, type );
53 if( p == __gg__exception_table_end ) {
54 cbl_internal_error("no such exception: 0x%x", type);
55 }
56 return p;
57 }
58
59 const char *
60 ec_type_str( ec_type_t type ) {
61 auto p = ec_type_descr(type);
62 return p->name;
63 }
64
65 ec_disposition_t
66 ec_type_disposition( ec_type_t type ) {
67 auto p = ec_type_descr(type);
68 return p->disposition;
69 }
70
71 static size_t
72 ec_level( ec_type_t ec ) {
73 if( ec == ec_all_e ) return 1;
74 if( 0 == (static_cast<unsigned int>(ec) & ~EC_ALL_E) ) return 2;
75 return 3;
76 }
77
78 void
79 cbl_enabled_exception_t::dump( int i ) const {
80 cbl_message(2, "cbl_enabled_exception_t: %2d {%s, %s, %zu}",
81 i,
82 location? "location" : " none",
83 ec_type_str(ec),
84 file );
85 }
86
87 void
88 cbl_enabled_exceptions_t::dump() const {
89 extern int yydebug;
90 int debug = 1;
91 std::swap(debug, yydebug); // dbgmsg needs yydebug
92
93 if( empty() ) {
94 dbgmsg("cbl_enabled_exceptions_t: no exceptions" );
95 std::swap(debug, yydebug);
96 return;
97 }
98 int i = 1;
99 for( auto& elem : *this ) { // cppcheck-suppress constVariableReference
100 dbgmsg("cbl_enabled_exceptions_t: %2d {%s, %s, %lu}",
101 i++,
102 elem.location? "with location" : " no location",
103 ec_type_str(elem.ec),
104 gb4(elem.file) );
105 }
106 std::swap(debug, yydebug);
107 }
108
109 // cppcheck-suppress-begin [useStlAlgorithm] because why?
110 uint32_t
111 cbl_enabled_exceptions_t::status() const {
112 uint32_t status_word = 0;
113 for( const auto& ena : *this ) {
114 status_word |= (EC_ALL_E & ena.ec );
115 }
116 return status_word;
117 }
118 // cppcheck-suppress-end useStlAlgorithm
119
120 std::vector<uint64_t>
121 cbl_enabled_exceptions_t::encode() const {
122 std::vector<uint64_t> encoded;
123 auto p = std::back_inserter(encoded);
124 for( const auto& ec : *this ) {
125 *p++ = ec.location;
126 *p++ = ec.ec;
127 *p++ = ec.file;
128 }
129 return encoded;
130 }
131
132 void
133 cbl_enabled_exceptions_t::turn_on_off( bool enabled,
134 bool location,
135 ec_type_t type,
136 const std::set<size_t>& files )
137 {
138 // Update current enabled ECs tree on leaving this function.
139 class update_parser_t {
140 const cbl_enabled_exceptions_t& ecs;
141 public:
142 explicit update_parser_t(const cbl_enabled_exceptions_t& ecs) : ecs(ecs) {}
143 ~update_parser_t() {
144 tree ena = parser_compile_ecs(ecs.encode());
145 current_enabled_ecs(ena);
146 }
147 } update_parser(*this);
148
149 // A Level 3 EC is added unilaterally; it can't affect a higher level.
150 if( ec_level(type) == 3 ) {
151 if( files.empty() ) {
152 auto elem = cbl_enabled_exception_t(location, type);
153 apply(enabled, elem);
154 return;
155 }
156
157 for( size_t file : files ) {
158 auto elem = cbl_enabled_exception_t(location, type, file);
159 apply(enabled, elem);
160 }
161 return;
162 }
163
164 // A new Level 1 or Level 2 EC is likewise simply added.
165 if( enabled ) {
166 if( files.empty() ) {
167 auto elem = cbl_enabled_exception_t(location, type);
168 apply(enabled, elem);
169 return;
170 }
171 for( size_t file: files ) {
172 auto elem = cbl_enabled_exception_t(location, type, file);
173 apply(enabled, elem);
174 }
175 return;
176 }
177
178 assert(!enabled);
179 assert(ec_level(type) < 3);
180
181 /*
182 * >> TURN EC [files] CHECKING OFF
183 */
184
185 if( files.empty() ) {
186 // A Level 1 EC with no files disables all ECs
187 if( type == ec_all_e ) {
188 clear();
189 return;
190 }
191 // Because TURN CHECKING OFF mentioned no files, Remove any matching
192 // Level-2 or Level-3 ECs, regardless of their files.
193 auto p = begin();
194 while( end() != (p = std::find_if( begin(), end(),
195 [ec = type]( const auto& elem ) {
196 return
197 elem.ec != ec_all_e &&
198 ec_cmp(ec, elem.ec); } )) ) {
199 erase(p);
200 }
201 // Keep the EC as an override if a higher-level would othewise apply.
202 p = std::find_if( begin(), end(),
203 [ec = type]( const auto& elem ) {
204 return
205 (elem.ec == ec_all_e || elem.ec < ec) &&
206 elem.file == 0 &&
207 ec_cmp(ec, elem.ec); } );
208 if( p != end() ) {
209 auto elem = cbl_enabled_exception_t(location, type);
210 apply(enabled, elem);
211 }
212 } else {
213 // Remove any matching or lower-level EC for the same file.
214 for( size_t file: files ) {
215 auto p = begin();
216 while( end() != (p = std::find_if( begin(), end(),
217 [ec = type, file]( const auto& elem ) {
218 return
219 // ec is higher level and matches
220 (ec == ec_all_e || ec <= elem.ec) &&
221 file == elem.file &&
222 ec_cmp(ec, elem.ec); } )) ) {
223 erase(p);
224 }
225 // Keep the EC as an override if a higher-level would othewise apply.
226 p = std::find_if( begin(), end(),
227 [ec = type, file]( const auto& elem ) {
228 return
229 (elem.ec == ec_all_e || elem.ec < ec) &&
230 file == elem.file &&
231 ec_cmp(ec, elem.ec); } );
232 if( p != end() ) {
233 auto elem = cbl_enabled_exception_t(location, type, file);
234 apply(enabled, elem);
235 }
236 }
237 }
238 return;
239 }
240
241 const cbl_enabled_exception_t *
242 cbl_enabled_exceptions_t::match( ec_type_t type, size_t file ) const {
243 auto output = enabled_exception_match( begin(), end(), type, file );
244 return output != end()? &*output : NULL;
245 }
246
247 bool
248 sort_supers_last( const cbl_declarative_t& a, const cbl_declarative_t& b ) {
249 if( symbol_at(a.section)->program == symbol_at(b.section)->program ) {
250 return a.section < b.section;
251 }
252 return symbol_at(a.section)->program > symbol_at(b.section)->program;
253 }
254
255 cbl_field_t * new_temporary_decl();
256 /*
257 * Generate the code to evaluate declaratives. This is the "secret
258 * section" right after END DECLARATIVES. Its name is
259 * _DECLARATIVES_EVAL, and it is performed after every statement that
260 * could raise an exception.
261 *
262 * The code calls the library routine __gg__match_exception, which
263 * compares the raised exception to the criteria set by the USE
264 * statements in the DECLARATIVES super-section. It returns an
265 * integer, which is an index to the label in the symbol table that
266 * defines the section for the matching USE criteria.
267 *
268 * The generated code is a sequence of IF statements comparing the
269 * returned integer to that of each declarative. If equal, that
270 * section is PERFORMed, and control branches to the end of this
271 * section, and thence back to the statement it came from.
272 */
273 #include "../../libgcobol/io.h"
274 size_t current_file_index();
275 file_status_t current_file_handled_status();
276
277 void
278 declarative_runtime_match( const std::list<cbl_declarative_t>& declaratives,
279 cbl_label_t *lave )
280 {
281 if( getenv("GCOBOL_SHOW") )
282 {
283 fprintf(stderr, "( %d ) %s: \n", cobol_location().first_line, __func__);
284 }
285 if( getenv("GCOBOL_TRACE") )
286 {
287 gg_printf(">>>>>>( %d )(%s) declaratives: lave:%s\n",
288 build_int_cst_type(INT, cobol_location().first_line),
289 gg_string_literal(__func__),
290 gg_string_literal(lave->name),
291 NULL_TREE);
292 }
293 static auto yes = new_temporary(FldConditional);
294 static auto isection = new_temporary(FldNumericBin5);
295 static auto index = new_temporary(FldNumericBin5);
296
297 /*
298 * Generate a sequence of COBOL IF statements to match the Declarative's
299 * symbol table index to its performable section. The entire sequence is
300 * guarded by a runtime IF that evaluates to TRUE only if the "current EC" is
301 * nonzero. This way, when _DECLARATIVES_EVAL is performed, it does nothing
302 * if no EC was raised.
303 */
304 IF( var_decl_exception_code, ne_op, integer_zero_node ) {
305 // Get declarative section index matching any raised EC.
306 parser_match_exception(index);
307
308 // Compare returned index to each section index.
309 for( const auto& dcl : declaratives ) {
310 parser_set_numeric( isection, dcl.section );
311 parser_relop( yes, index, eq_op, isection );
312 parser_if( yes );
313 auto section = cbl_label_of(symbol_at(dcl.section));
314 parser_push_exception();
315 parser_perform(section);
316 parser_pop_exception();
317 parser_label_goto(lave);
318 parser_else();
319 parser_fi();
320 }
321 }
322 ELSE {
323 if( getenv("GCOBOL_TRACE") )
324 {
325 gg_printf(">>>>>>( %d )(%s) __gg__exception_code is zero\n",
326 build_int_cst_type(INT, cobol_location().first_line),
327 gg_string_literal(__func__),
328 NULL_TREE);
329 }
330 }
331 ENDIF
332 }
333
334 ec_type_t
335 ec_type_of( const cbl_name_t name ) {
336 auto p = std::find_if( __gg__exception_table, __gg__exception_table_end,
337 [name]( const ec_descr_t& descr ) {
338 return 0 == strcasecmp(name, descr.name);
339 } );
340 return p == __gg__exception_table_end? ec_none_e : p->type;
341 }
342