Debian Codegraph @

libgcobol/common-defs.h

raw
1 /*
2 * Copyright (c) 2021-2025 Symas Corporation
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of the Symas Corporation nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 #ifndef COMMON_DEFS_H_
31 #define COMMON_DEFS_H_
32
33 #include <cassert>
34 #include <cstdio>
35 #include <cstdint>
36 #include <list>
37
38 #define COUNT_OF(X) (sizeof(X) / sizeof(X[0]))
39
40 // This constant establishes the maximum number of digits in a fixed point
41 // number. We are using 37 digits as a maximum because a full-size 37-digit
42 // number (10**37) takes 123 bits, and a full-size 38-digit number (10**38)
43 // takes 127 bits. By using a maximum of 37, that gives us an additional digit
44 // of headroom in order to accomplish rounding.
45
46 // You should keep in mind that the 128-bit binary floating point numbers that
47 // we use can reliably reproduce numbers of 33 decimal digits when going to
48 // binary and back.
49
50 #define MAX_FIXED_POINT_DIGITS (37)
51
52 // COBOL tables can have up to seven subscripts
53 #define MAXIMUM_TABLE_DIMENSIONS 7
54
55 // This bit gets turned on in the first or last byte (depending on the leading_e attribute
56 // phrase) of a NumericDisplay to indicate that the value is negative.
57
58 // When running the EBCDIC character set, the meaning of this bit is flipped,
59 // because an EBCDIC zero is 0xF0, while ASCII is 0x30
60 #define NUMERIC_DISPLAY_SIGN_BIT 0x40
61
62 #define LEVEL01 (1)
63 #define LEVEL49 (49)
64 #define LEVEL77 (77)
65
66 // In the __gg__move_literala() call, we piggyback this bit onto the
67 // cbl_round_t parameter, just to cut down on the number of parameters passed
68
69 #define REFER_ALL_BIT 0x80
70
71 // Other bits for handling MOVE ALL and so on.
72 #define REFER_T_ALL_FLAGS_MASK 0x0FF // We allow for seven subscripts
73 #define REFER_T_MOVE_ALL 0x100 // This is the move_all flag
74 #define REFER_T_ADDRESS_OF 0x200 // This is the address_of flag
75 #define REFER_T_REFMOD 0x400 // Indicates to library the refer was a refmod
76
77 #define MIN_FIELD_BLOCK_SIZE (16)
78
79 #define A_ZILLION (1000000) // Absurdly large number for __gg__call_parameter_count
80
81 // These bits are used for the "call flags" of arithmetic operations
82 #define ON_SIZE_ERROR 0x01
83 #define REMAINDER_PRESENT 0x02
84
85 #define MINIMUM_ALLOCATION_SIZE 16
86
87 // This was part of an exercise to make cppcheck shut up about invalid
88 // pointer type conversions.
89 // It was also to avoid having reinterpret_cast<> all over the place.
90 // So, instead of reinterpret_cast<char *>(VALUE)
91 // I sometimes use PTRCAST(char, VALUE)
92 // Note that "(char *)" is implied by "PTRCAST(char, VALUE)"
93 #define PTRCAST(TYPE, VALUE) static_cast<TYPE *>(static_cast<void *>(VALUE))
94
95 /*
96 * User-defined names in IBM COBOL can have at most 30 characters.
97 * For DBCS, the maximum is 14.
98 *
99 * Per ISO/IEC 1989:2023(E), 8.3.2 COBOL words,
100 * "A COBOL word is a character-string of not more than 63 characters"
101 */
102 typedef char cbl_name_t[64];
103
104 // Note that the field_type enum is duplicated in the source code for the
105 // COBOL-aware GDB, and so any changes here (or there) have to be reflected
106 // there (or here)
107
108 // Note further that if this list changes, then the valid_move() matrix has to
109 // change as will. Currently that matrix is in util.cc.
110
111 enum cbl_field_type_t {
112 FldInvalid, // uninitialized
113 FldGroup,
114 FldAlphanumeric, // For X(n).
115 FldNumericBinary, // For 999v9 comp big-endian, 1 to 16 bytes
116 FldFloat, // 4-, 8-, and 16-byte floating point. See ieeedec_e and big_endian_e flags
117 FldPacked, // For 999v9 comp-3 internal decimal, packed decimal representation;
118 FldNumericBin5, // For 999v9 comp-5 little-endian, 1 to 16 bytes. (Native binary)
119 FldNumericDisplay, // For 999v9 one decimal character per byte
120 FldNumericEdited, // For 999.9 PIC BPVZ90/,.+-CRDB*cs; must have one of B/Z0,.*+-CRDBcs
121 FldAlphaEdited, // PIC AX9B0/; must have at least one A or X, and at least one B0/
122 FldLiteralA, // Alphanumeric literal
123 FldLiteralN, // Numeric literal
124 FldClass,
125 FldConditional, // Target for parser_relop()
126 FldForward,
127 FldIndex,
128 FldSwitch,
129 FldDisplay,
130 FldPointer,
131 FldBlob,
132 };
133
134
135 /* BINARY, COMP, COMPUTATIONAL, COMP-4, COMPUTATIONAL-4 are the same:
136 * Storage, by default, is big-endian.
137 * PIC 9(1 to 4) is 2 bytes
138 * PIC 9(5 to 9) is 4 bytes
139 * PIC 9(10 to 18) is 8 bytes
140 * PIC 9(19-37) is 16 bytes
141 * COMP-1, COMPUTATIONAL-1
142 * 4-byte floating point (single)
143 * COMP-2, COMPUTATIONAL-2
144 * 8-byte floating point (double)
145 * PACKED-DECIMAL, COMP-3, COMPUTATIONAL-3
146 * Packed decimal. Final nybble is 0xF for unsigned numbers. For signable
147 * values, it is 0xD for negative, and 0xC for non-negative
148 * COMP-5, COMPUTATIONAL-5
149 * Native binary. The maximum number of digits is implied by
150 * the 2, 4, or 8 bytes of data storage. By "native", little-endian
151 * is implied on Intel processors.
152 */
153
154 /*
155 * Enumerated bit mask of variable attributes.
156 * A field as either left- or right-justified.
157 * A field is padded (in the unjustified direction) either with 0 or SPC.
158 * (But maybe the fill character should just be an explicit character.)
159 */
160 enum cbl_field_attr_t : uint64_t {
161 none_e = 0x0000000000,
162 figconst_1_e = 0x0000000001, // This needs to be 1 - don't change the position
163 figconst_2_e = 0x0000000002, // This needs to be 2
164 figconst_4_e = 0x0000000004, // This needs to be 4
165 rjust_e = 0x0000000008, // justify right
166 ljust_e = 0x0000000010, // justify left
167 zeros_e = 0x0000000020, // zero fill
168 signable_e = 0x0000000040,
169 constant_e = 0x0000000080, // pre-assigned constant
170 function_e = 0x0000000100,
171 quoted_e = 0x0000000200,
172 filler_e = 0x0000000400,
173 _spare_e = 0x0000000800, //
174 intermediate_e = 0x0000001000, // Compiler-defined temporary variable
175 embiggened_e = 0x0000002000, // redefined numeric made 64-bit by USAGE POINTER
176 all_alpha_e = 0x0000004000, // FldAlphanumeric, but all A's
177 all_x_e = 0x0000008000, // picture is all X's
178 all_ax_e = 0x000000a000, // picture is all A's or all X's
179 prog_ptr_e = 0x0000010000, // FUNCTION-POINTER or PROGRAM-POINTER
180 scaled_e = 0x0000020000,
181 refmod_e = 0x0000040000, // Runtime; indicates a refmod is active
182 based_e = 0x0000080000, // pointer capacity, for ADDRESS OF or ALLOCATE
183 any_length_e = 0x0000100000, // inferred length of linkage in nested program
184 global_e = 0x0000200000, // field has global scope
185 external_e = 0x0000400000, // field has external scope
186 blank_zero_e = 0x0000800000, // BLANK WHEN ZERO
187 // data division uses 2 low bits of high byte
188 linkage_e = 0x0001000000, // field is in linkage section
189 local_e = 0x0002000000, // field is in local section
190 leading_e = 0x0004000000, // leading sign (signable_e alone means trailing)
191 separate_e = 0x0008000000, // separate sign
192 envar_e = 0x0010000000, // names an environment variable
193 dnu_1_e = 0x0020000000, // unused: this attribute bit is available
194 bool_encoded_e = 0x0040000000, // data.initial is a boolean string
195 hex_encoded_e = 0x0080000000, // data.initial is a hex-encoded string
196 depends_on_e = 0x0100000000, // A group hierachy contains a DEPENDING_ON
197 initialized_e = 0x0200000000, // Don't call parser_initialize from parser_symbol_add
198 has_value_e = 0x0400000000, // Flag to hierarchical descendents to ignore .initial
199 ieeedec_e = 0x0800000000, // Indicates a FldFloat is IEEE 754 decimal, rather than binary
200 big_endian_e = 0x1000000000, // Indicates a value is big-endian
201 same_as_e = 0x2000000000, // Field produced by SAME AS (cannot take new members)
202 record_key_e = 0x4000000000,
203 typedef_e = 0x8000000000, // IS TYPEDEF
204 strongdef_e = typedef_e + intermediate_e, // STRONG TYPEDEF (not temporary)
205 };
206 // The separate_e value does double-duty for FldPacked/COMP-6, which is not
207 // the same as FldPacked COMP-3. A COMP-3 can have signable_e, meaning that the
208 // final nybble is 0x0D for negative, and 0x0C for non-negative. When a COMP-3
209 // has no sign, then the final nybble is 0x0F. The packed_no_sign_e bit means
210 // that there is no sign nybble.
211 #define packed_no_sign_e separate_e
212
213 enum cbl_figconst_t
214 {
215 normal_value_e = 0, // This one must be zero
216 low_value_e = 1, // The order is important, because
217 null_value_e = 2,
218 zero_value_e = 3, // at times we compare, for example, low_value_e to
219 space_value_e = 4,
220 quote_value_e = 5, //
221 high_value_e = 6, // high_value_e to determine that low is less than high
222 };
223 #define FIGCONST_MASK (figconst_1_e|figconst_2_e|figconst_4_e)
224 #define DATASECT_MASK (linkage_e | local_e)
225
226
227 enum cbl_file_org_t {
228 file_disorganized_e,
229 file_sequential_e,
230 file_line_sequential_e,
231 file_indexed_e,
232 file_relative_e,
233 };
234
235 enum cbl_file_access_t {
236 file_inaccessible_e,
237 file_access_seq_e,
238 file_access_rnd_e,
239 file_access_dyn_e,
240 };
241
242 enum cbl_file_mode_t {
243 file_mode_none_e,
244 file_mode_input_e = 'r',
245 file_mode_output_e = 'w',
246 file_mode_extend_e = 'a',
247 file_mode_io_e = '+',
248 file_mode_any_e,
249 };
250
251 enum cbl_round_t {
252 away_from_zero_e,
253 nearest_toward_zero_e,
254 toward_greater_e,
255 toward_lesser_e,
256 nearest_away_from_zero_e,
257 nearest_even_e,
258 prohibited_e,
259 truncation_e,
260 };
261
262 #define RELOP_START 0
263 enum relop_t {
264 lt_op = RELOP_START,
265 le_op,
266 eq_op,
267 ne_op,
268 ge_op,
269 gt_op,
270 };
271
272 #define LOGOP_START 100
273 enum logop_t {
274 not_op = LOGOP_START,
275 and_op,
276 or_op,
277 xor_op,
278 xnor_op,
279 true_op,
280 false_op,
281 };
282
283 #define SETOP_START 200
284 enum setop_t {
285 is_op = SETOP_START,
286 };
287
288 enum bitop_t {
289 bit_set_op, // set bit on
290 bit_clear_op, // set bit off
291 bit_on_op, // true if bit is on
292 bit_off_op, // true if bit is off
293 bit_and_op,
294 bit_or_op,
295 bit_xor_op,
296 };
297
298 enum file_stmt_t {
299 file_stmt_delete_e,
300 file_stmt_merge_e,
301 file_stmt_read_e,
302 file_stmt_rewrite_e,
303 file_stmt_sort_e,
304 file_stmt_start_e,
305 file_stmt_write_e,
306 };
307
308 enum file_close_how_t {
309 file_close_no_how_e = 0x00,
310 file_close_removal_e = 0x01,
311 file_close_no_rewind_e = 0x02,
312 file_close_with_lock_e = 0x04,
313 file_close_reel_unit_e = 0x08,
314 };
315
316 enum cbl_compute_error_code_t {
317 compute_error_none = 0x0000,
318 compute_error_truncate = 0x0001,
319 compute_error_divide_by_zero = 0x0002,
320 compute_error_exp_zero_by_zero = 0x0004,
321 compute_error_exp_zero_by_minus = 0x0008,
322 compute_error_exp_minus_by_frac = 0x0010,
323 compute_error_overflow = 0x0020,
324 compute_error_underflow = 0x0040,
325 };
326
327 enum cbl_arith_format_t {
328 not_expected_e,
329 no_giving_e, giving_e,
330 corresponding_e };
331
332 enum cbl_encoding_t {
333 ASCII_e, // STANDARD-1 (in caps to avoid conflict with ascii_e in libgcobol.cc)
334 iso646_e, // STANDARD-2
335 EBCDIC_e, // NATIVE or EBCDIC
336 custom_encoding_e,
337 };
338
339 enum cbl_truncation_mode {
340 trunc_std_e,
341 trunc_opt_e,
342 trunc_bin_e,
343 };
344
345 enum cbl_inspect_bound_t {
346 bound_characters_e,
347 bound_all_e,
348 bound_first_e,
349 bound_leading_e,
350 bound_trailing_e,
351 };
352
353 // a SPECIAL-NAME
354 enum special_name_t {
355 SYSIN_e, SYSIPT_e, SYSOUT_e,
356 SYSLIST_e, SYSLST_e,
357 SYSPUNCH_e, SYSPCH_e,
358 CONSOLE_e,
359 C01_e, C02_e, C03_e, C04_e, C05_e, C06_e,
360 C07_e, C08_e, C09_e, C10_e, C11_e, C12_e,
361 CSP_e,
362 S01_e, S02_e, S03_e, S04_e, S05_e,
363 AFP_5A_e,
364 STDIN_e, STDOUT_e, STDERR_e, SYSERR_e,
365 ARG_NUM_e, ARG_VALUE_e, ENV_NAME_e, ENV_VALUE_e,
366 };
367
368 enum classify_t {
369 ClassInvalidType,
370 ClassNumericType,
371 ClassAlphabeticType,
372 ClassLowerType,
373 ClassUpperType,
374 ClassDbcsType,
375 ClassKanjiType,
376 };
377
378 static inline const char *
379 classify_str( enum classify_t classify ) {
380 switch(classify) {
381 case ClassInvalidType: return "ClassInvalidType";
382 case ClassNumericType: return "ClassNumericType";
383 case ClassAlphabeticType: return "ClassAlphabeticType";
384 case ClassLowerType: return "ClassLowerType";
385 case ClassUpperType: return "ClassUpperType";
386 case ClassDbcsType: return "ClassDbcsType";
387 case ClassKanjiType: return "ClassKanjiType";
388 };
389 return "(unknown classification)";
390 }
391
392 static inline const char *
393 cbl_file_mode_str( cbl_file_mode_t mode ) {
394 switch(mode) {
395 case file_mode_none_e: return "file_mode_none_e";
396 case file_mode_input_e: return "file_mode_input_e: 'r'";
397 case file_mode_output_e: return "file_mode_output_e: 'w'";
398 case file_mode_io_e: return "file_mode_io_e: '+'";
399 case file_mode_extend_e: return "file_mode_extend_e: 'a'";
400 case file_mode_any_e: return "file_mode_any_e";
401 }
402 return "???";
403 };
404
405 enum module_type_t {
406 module_activating_e,
407 module_current_e,
408 module_nested_e,
409 module_stack_e,
410 module_toplevel_e,
411 };
412
413 /*
414 * Compare a "raised" EC to an enabled EC or of a declarative. "raised" may in
415 * fact not be raised; in the compiler this function is used to compare a TURN
416 * directive to the list of enabled ECs.
417 */
418 static bool
419 ec_cmp( ec_type_t raised, ec_type_t ec )
420 {
421 if( raised == ec ) return true;
422
423 // If both low bytes are nonzero, we had to match exactly, above.
424 if( (~EC_ALL_E & static_cast<uint32_t>(raised))
425 &&
426 (~EC_ALL_E & static_cast<uint32_t>(ec)) ) {
427 return false;
428 }
429
430 // Level 1 and 2 have low byte of zero.
431 // If one low byte is zero, see if they're the same kind.
432 return 0xFF < ( static_cast<uint32_t>(raised)
433 &
434 static_cast<uint32_t>(ec) );
435 }
436
437 struct cbl_enabled_exception_t {
438 bool location;
439 ec_type_t ec;
440 size_t file;
441
442 cbl_enabled_exception_t()
443 : location(false)
444 , ec(ec_none_e)
445 , file(0)
446 {}
447
448 cbl_enabled_exception_t( bool location, ec_type_t ec, size_t file = 0 )
449 : location(location)
450 , ec(ec)
451 , file(file)
452 {}
453
454 // sort by ec and file
455 bool operator<( const cbl_enabled_exception_t& that ) const {
456 if( ec == that.ec ) return file < that.file;
457 return ec < that.ec;
458 }
459 // match on ec and file
460 bool operator==( const cbl_enabled_exception_t& that ) const {
461 return ec == that.ec && file == that.file;
462 }
463
464 void dump( int i ) const;
465 };
466
467 struct cbl_declarative_t {
468 enum { files_max = 16 };
469 size_t section; // implies program
470 bool global;
471 ec_type_t type;
472 uint32_t nfile, files[files_max];
473 cbl_file_mode_t mode;
474
475 explicit cbl_declarative_t( cbl_file_mode_t mode = file_mode_none_e )
476 : section(0)
477 , global(false)
478 , type(ec_none_e)
479 , nfile(0)
480 , mode(mode)
481 {
482 std::fill(files, files + COUNT_OF(files), 0);
483 }
484 explicit cbl_declarative_t( ec_type_t type )
485 : section(0)
486 , global(false)
487 , type(type)
488 , nfile(0)
489 , mode(file_mode_none_e)
490 {
491 std::fill(files, files + COUNT_OF(files), 0);
492 }
493
494 cbl_declarative_t( size_t section, ec_type_t type,
495 const std::list<size_t>& files,
496 cbl_file_mode_t mode,
497 bool global = false )
498 : section(section), global(global)
499 , type(type)
500 , nfile(files.size())
501 , mode(mode)
502 {
503 assert( files.size() <= COUNT_OF(this->files) );
504 std::fill(this->files, this->files + COUNT_OF(this->files), 0);
505 if( nfile > 0 ) {
506 std::copy( files.begin(), files.end(), this->files );
507 }
508 }
509 cbl_declarative_t( const cbl_declarative_t& that )
510 : section(that.section)
511 , global(that.global)
512 , type(that.type)
513 , nfile(that.nfile)
514 , mode(that.mode)
515 {
516 std::fill(files, files + COUNT_OF(files), 0);
517 if( nfile > 0 ) {
518 std::copy( that.files, that.files + nfile, this->files );
519 }
520 }
521 cbl_declarative_t& operator=(const cbl_declarative_t&) = default;
522
523 std::vector<uint64_t> encode() const;
524
525 /*
526 * Sort file names before file modes, and file modes before non-IO.
527 */
528 bool operator<( const cbl_declarative_t& that ) const {
529 // file name declaratives first, in section order
530 if( nfile != 0 ) {
531 if( that.nfile != 0 ) return section < that.section;
532 return true;
533 }
534 // file mode declaratives between file name declaratives and non-IO
535 if( mode != file_mode_none_e ) {
536 if( that.nfile != 0 ) return false;
537 if( that.mode == file_mode_none_e ) return true;
538 return section < that.section;
539 }
540 // all others by section, after names and modes
541 if( that.nfile != 0 ) return false;
542 if( that.mode != file_mode_none_e ) return false;
543 return section < that.section;
544 }
545
546 // TRUE if there are no files to match, or the provided file is in the list.
547 bool match_file( size_t file ) const {
548 static const uint32_t * pend = files + nfile;
549
550 return nfile == 0 || pend != std::find(files, files + nfile, file);
551 }
552
553 // USE Format 1 names a file mode, or at least one file, and not an EC.
554 bool is_format_1() const {
555 return mode != file_mode_none_e;
556 }
557 };
558
559 typedef std::vector<cbl_declarative_t> cbl_declaratives_t;
560
561 class cbl_enabled_exceptions_t : protected std::set<cbl_enabled_exception_t>
562 {
563 void apply( bool enabled, const cbl_enabled_exception_t& elem ) {
564 if( ! enabled ) {
565 erase(elem);
566 return;
567 }
568 auto inserted = insert( elem );
569 if( ! inserted.second ) {
570 erase(inserted.first);
571 insert(elem);
572 }
573 }
574
575 public:
576 cbl_enabled_exceptions_t() {}
577 cbl_enabled_exceptions_t( size_t nec, const cbl_enabled_exception_t *ecs )
578 : std::set<cbl_enabled_exception_t>(ecs, ecs + nec)
579 {}
580 void turn_on_off( bool enabled, bool location, ec_type_t type,
581 const std::set<size_t>& files );
582
583 const cbl_enabled_exception_t * match( ec_type_t ec, size_t file = 0 ) const;
584
585 void dump() const;
586 void dump( const char tag[] ) const;
587 uint32_t status() const;
588
589 void clear() { std::set<cbl_enabled_exception_t>::clear(); }
590
591 bool empty() const { return std::set<cbl_enabled_exception_t>::empty(); }
592 size_t size() const { return std::set<cbl_enabled_exception_t>::size(); }
593
594 std::vector<uint64_t> encode() const;
595 cbl_enabled_exceptions_t& decode( const std::vector<uint64_t>& encoded );
596
597 cbl_enabled_exceptions_t& operator=( const cbl_enabled_exceptions_t& ) = default;
598 };
599
600 cbl_enabled_exceptions_t& cdf_enabled_exceptions();
601
602 template <typename T>
603 T enabled_exception_match( T beg, T end, ec_type_t type, size_t file ) {
604 cbl_enabled_exception_t input( true, // doesn't matter
605 type, file );
606 auto output = std::find(beg, end, input);
607 if( output == end ) {
608 output = std::find_if( beg, end, // match any file
609 [ec = type]( const cbl_enabled_exception_t& elem ) {
610 return
611 elem.file == 0 &&
612 ec_cmp(ec, elem.ec); } );
613 }
614 return output;
615 }
616
617 enum substitute_flags_t
618 {
619 substitute_anycase_e = 1,
620 substitute_first_e = 2, // first and last are mutually exclusive
621 substitute_last_e = 4,
622 };
623
624 #endif