Debian Codegraph @

gcc/cobol/genutil.cc

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 #include "cobol-system.h"
31 #include "coretypes.h"
32 #include "tree.h"
33 #include "../../libgcobol/ec.h"
34 #include "../../libgcobol/common-defs.h"
35 #include "util.h"
36 #include "cbldiag.h"
37 #include "symbols.h"
38 #include "gengen.h"
39 #include "inspect.h"
40 #include "../../libgcobol/io.h"
41 #include "genapi.h"
42 #include "genutil.h"
43 #include "structs.h"
44 #include "../../libgcobol/gcobolio.h"
45 #include "../../libgcobol/charmaps.h"
46 #include "show_parse.h"
47 #include "../../libgcobol/exceptl.h"
48 #include "exceptg.h"
49
50 bool internal_codeset_is_ebcdic() { return gcobol_feature_internal_ebcdic(); }
51
52 bool exception_location_active = true;
53 bool skip_exception_processing = true;
54
55 bool suppress_dest_depends = false;
56
57 std::vector<std::string>current_filename;
58
59 tree var_decl_exception_code; // int __gg__exception_code;
60 tree var_decl_exception_file_status; // int __gg__exception_file_status;
61 tree var_decl_exception_file_name; // const char *__gg__exception_file_name;
62 tree var_decl_exception_statement; // const char *__gg__exception_statement;
63 tree var_decl_exception_source_file; // const char *__gg__exception_source_file;
64 tree var_decl_exception_line_number; // int __gg__exception_line_number;
65 tree var_decl_exception_program_id; // const char *__gg__exception_program_id;
66 tree var_decl_exception_section; // const char *__gg__exception_section;
67 tree var_decl_exception_paragraph; // const char *__gg__exception_paragraph;
68
69 tree var_decl_default_compute_error; // int __gg__default_compute_error;
70 tree var_decl_rdigits; // int __gg__rdigits;
71 tree var_decl_unique_prog_id; // size_t __gg__unique_prog_id;
72
73 tree var_decl_entry_location; // This is for managing ENTRY statements
74 tree var_decl_exit_address; // This is for implementing pseudo_return_pop
75
76 tree var_decl_call_parameter_signature; // char *__gg__call_parameter_signature
77 tree var_decl_call_parameter_count; // int __gg__call_parameter_count
78 tree var_decl_call_parameter_lengths; // size_t *__gg__call_parameter_count
79
80 tree var_decl_return_code; // short __gg__data_return_code
81
82 tree var_decl_arithmetic_rounds_size; // size_t __gg__arithmetic_rounds_size;
83 tree var_decl_arithmetic_rounds; // int* __gg__arithmetic_rounds;
84 tree var_decl_fourplet_flags_size; // size_t __gg__fourplet_flags_size;
85 tree var_decl_fourplet_flags; // int* __gg__fourplet_flags;
86
87 tree var_decl_treeplet_1f; // cblc_field_pp_type_node , "__gg__treeplet_1f"
88 tree var_decl_treeplet_1o; // SIZE_T_P , "__gg__treeplet_1o"
89 tree var_decl_treeplet_1s; // SIZE_T_P , "__gg__treeplet_1s"
90 tree var_decl_treeplet_2f; // cblc_field_pp_type_node , "__gg__treeplet_2f"
91 tree var_decl_treeplet_2o; // SIZE_T_P , "__gg__treeplet_2o"
92 tree var_decl_treeplet_2s; // SIZE_T_P , "__gg__treeplet_2s"
93 tree var_decl_treeplet_3f; // cblc_field_pp_type_node , "__gg__treeplet_3f"
94 tree var_decl_treeplet_3o; // SIZE_T_P , "__gg__treeplet_3o"
95 tree var_decl_treeplet_3s; // SIZE_T_P , "__gg__treeplet_3s"
96 tree var_decl_treeplet_4f; // cblc_field_pp_type_node , "__gg__treeplet_4f"
97 tree var_decl_treeplet_4o; // SIZE_T_P , "__gg__treeplet_4o"
98 tree var_decl_treeplet_4s; // SIZE_T_P , "__gg__treeplet_4s"
99
100 // There are times when I need to insert a NOP into the code, mainly to force
101 // a .loc directive into the assembly language so that the GDB-COBOL debugger
102 // can show the COBOL source code. This is true, for example, the CONTINUE
103 // statement which otherwise would produce no assembly language. Since I
104 // wasn't successful figuring out how to create an actual NOP assembly language
105 // instruction, I instead gg_assign(var_decl_nop, integer_zero_node)
106 tree var_decl_nop; // int __gg__nop;
107 tree var_decl_main_called; // int __gg__main_called;
108
109 #if 0
110 #define REFER(a)
111 #else
112 #define REFER(a) do \
113 { \
114 if( getenv("REFER") ) \
115 { \
116 fprintf(stderr, "REFER %s %s\n", __func__, a); \
117 } \
118 }while(0);
119 #endif
120
121 int
122 get_scaled_rdigits(cbl_field_t *field)
123 {
124 int retval;
125 if( !(field->attr & scaled_e) )
126 {
127 // The value is not P-scaled, so we just use the unchanged rdigits value
128 retval = field->data.rdigits;
129 }
130 else
131 {
132 if( field->data.rdigits < 0 )
133 {
134 // The PIC string was something like 999PPPP, which means an rdigits value
135 // of -4. We return zero; somebody else will have the job of multiplying
136 // the three significant digits by 10^4 to get the magnitude correct.
137 retval = 0;
138 }
139 else
140 {
141 // The PIC string was something like PPPP999, which means an rdigits value
142 // of +4. We return an rdigits value of 4 + 3 = 7, which will mean that
143 // the three significant digits will be scaled to 0.0000999
144 retval = field->data.digits + field->data.rdigits;
145 }
146 }
147 return retval;
148 }
149
150 int
151 get_scaled_digits(cbl_field_t *field)
152 {
153 int retval;
154 if( !(field->attr & scaled_e) )
155 {
156 // The value is not P-scaled, so we just use the unchanged rdigits value
157 retval = field->data.digits;
158 }
159 else
160 {
161 if( field->data.rdigits < 0 )
162 {
163 // The PIC string was something like 999PPPP, which means an rdigits value
164 // of -4. digits is 3, reflecting the 9(3). We return seven, reflecting
165 // that all of the final digits are to the left of the decimal point
166 retval = field->data.digits - field->data.rdigits;
167 }
168 else
169 {
170 // The PIC string was something like PPPP999, which means an rdigits value
171 // of +4. We return and rdigits value of 4 + 3 = 7, which will mean that
172 // the three significant digits will be scaled to 0.0000999 and all of the
173 // seven digits are to the left of the decimal point
174 retval = field->data.digits + field->data.rdigits;
175 }
176 }
177 return retval;
178 }
179
180 tree
181 tree_type_from_digits(size_t digits, int signable)
182 {
183 tree retval = NULL_TREE;
184
185 if( signable )
186 {
187 if(digits <= 2 )
188 {
189 retval = CHAR;
190 }
191 else if (digits <= 4 )
192 {
193 retval = SHORT;
194 }
195 else if (digits <= 9 )
196 {
197 retval = INT;
198 }
199 else if (digits <= 18 )
200 {
201 retval = LONGLONG;
202 }
203 else
204 {
205 retval = INT128;
206 }
207 }
208 else
209 {
210 if(digits <= 2 )
211 {
212 retval = UCHAR;
213 }
214 else if (digits <= 4 )
215 {
216 retval = USHORT;
217 }
218 else if (digits <= 9 )
219 {
220 retval = UINT;
221 }
222 else if (digits <= 18 )
223 {
224 retval = ULONGLONG;
225 }
226 else
227 {
228 retval = UINT128;
229 }
230 }
231 return retval;
232 }
233
234 void
235 get_integer_value(tree value, // We know this is a LONG
236 cbl_field_t *field,
237 tree offset,
238 bool check_for_fractional_digits)
239 {
240 if( field->type == FldLiteralN && field->data.rdigits==0 )
241 {
242 gg_assign(value, gg_cast(LONG, field->data_decl_node));
243 return;
244 }
245
246 Analyze();
247 // Call this routine when you know the result has to be an integer with no
248 // rdigits. This routine became necessary the first time I saw an
249 // intermediate value for an array subscript: table((3 + 1) / 2))
250 //
251 // If the field_i has rdigits, and if any of those rdigits are non-zero, we
252 // return a 1 so that our caller can decide what to do.
253
254 static tree temp = gg_define_variable(INT128, "..giv_temp", vs_file_static);
255 static tree rdigits = gg_define_variable(INT, "..giv_rdigits", vs_file_static);
256
257 if( field->attr & intermediate_e )
258 {
259 // Get the binary value, which for 99V99 can be 1234, meaning 12.34
260 get_binary_value(temp, NULL, field, offset);
261
262 // Pick up the run-time number of rdigits:
263 gg_assign(rdigits, gg_cast(INT, member(field, "rdigits")));
264
265 // Scale by the number of rdigits, which turns 12.34 into 12.
266 // When check_for_fractional_digits is true, __gg__rdigits will be set
267 // to 1 for 12.34, and will be set to zero 12.00
268 scale_by_power_of_ten(temp,
269 gg_negate(rdigits),
270 check_for_fractional_digits);
271 }
272 else
273 {
274 get_binary_value(temp, rdigits, field, offset);
275 scale_by_power_of_ten_N(temp,
276 -get_scaled_rdigits(field),
277 check_for_fractional_digits);
278 }
279 gg_assign(value, gg_cast(TREE_TYPE(value), temp));
280 }
281
282 static
283 tree // This is a SIZE_T
284 get_any_capacity(cbl_field_t *field)
285 {
286 if( field->attr & (any_length_e | intermediate_e) )
287 {
288 return member(field->var_decl_node, "capacity");
289 }
290 else
291 {
292 return build_int_cst_type(SIZE_T, field->data.capacity);
293 }
294 }
295
296 /* This routine, used by both get_data_offset and refer_refmod_length,
297 fetches the refmod_from and refmod_length. If ec-bound-ref-mod checking
298 is enabled, it does those checks and sets the exception condition when they
299 are violated.
300
301 The return value for refstart is the actual offset, that is val(7:3) returns
302 the value 7-1, that is, 6.
303 */
304 static
305 void
306 get_and_check_refstart_and_reflen( tree refstart,// LONG returned value
307 tree reflen, // LONG returned value
308 const cbl_refer_t &refer)
309 {
310 const cbl_enabled_exceptions_t&
311 enabled_exceptions( cdf_enabled_exceptions() );
312
313 if( !enabled_exceptions.match(ec_bound_ref_mod_e) )
314 {
315 // This is normal operation -- no exception checking. Thus, we won't
316 // be trying to check for boundaries or integerness. And the programmer
317 // is accepting the responsibility for bad code: "If you specify
318 // disaster, disaster is what you get."
319
320 get_integer_value(refstart,
321 refer.refmod.from->field,
322 refer_offset(*refer.refmod.from));
323 gg_decrement(refstart);
324
325 if( refer.refmod.len )
326 {
327 // The length was specified, so that's what we return:
328 get_integer_value(reflen,
329 refer.refmod.len->field,
330 refer_offset(*refer.refmod.len));
331 }
332 else
333 {
334 // The length was not specified, so we need to return the distance
335 // between refmod.from and the end of the field:
336 gg_assign(reflen, gg_subtract( get_any_capacity(refer.field), refstart) );
337 }
338 return;
339 }
340
341 // ec_bound_ref_mode_e checking is enabled:
342
343 get_integer_value(refstart,
344 refer.refmod.from->field,
345 refer_offset(*refer.refmod.from),
346 CHECK_FOR_FRACTIONAL_DIGITS);
347
348 IF( var_decl_rdigits,
349 ne_op,
350 integer_zero_node )
351 {
352 // The value for refstart had non-zero decimal places. This is an
353 // error condition:
354 set_exception_code(ec_bound_ref_mod_e);
355 gg_assign(refstart, gg_cast(LONG, integer_one_node));
356 gg_assign(var_decl_rdigits, integer_zero_node);
357 }
358 ELSE
359 ENDIF
360
361 // Make refstart zero-based:
362 gg_decrement(refstart);
363
364 IF( refstart, lt_op, build_int_cst_type(LONG, 0 ) )
365 {
366 // A negative value for refstart is an error condition:
367 set_exception_code(ec_bound_ref_mod_e);
368 gg_assign(refstart, gg_cast(LONG, integer_zero_node));
369 // Set reflen to one here, because otherwise it won't be established.
370 gg_assign(reflen, gg_cast(TREE_TYPE(reflen), integer_one_node));
371 }
372 ELSE
373 {
374 IF( refstart, gt_op, gg_cast(TREE_TYPE(refstart), get_any_capacity(refer.field)) )
375 {
376 // refstart greater than zero is an error condition:
377 set_exception_code(ec_bound_ref_mod_e);
378 gg_assign(refstart, gg_cast(LONG, integer_zero_node));
379 // Set reflen to one here, because otherwise it won't be established.
380 gg_assign(reflen, gg_cast(TREE_TYPE(reflen), integer_one_node));
381 }
382 ELSE
383 {
384 if( refer.refmod.len )
385 {
386 get_integer_value(reflen,
387 refer.refmod.len->field,
388 refer_offset(*refer.refmod.len),
389 CHECK_FOR_FRACTIONAL_DIGITS);
390 IF( var_decl_rdigits,
391 ne_op,
392 integer_zero_node )
393 {
394 // length is not an integer, which is an error condition
395 set_exception_code(ec_bound_ref_mod_e);
396 gg_assign(reflen, gg_cast(LONG, integer_one_node));
397 gg_assign(var_decl_rdigits, integer_zero_node);
398 }
399 ELSE
400 {
401 // The length is an integer, so we can keep going.
402 IF( reflen, lt_op, gg_cast(LONG, integer_one_node) )
403 {
404 // length is too small, which is an error condition.
405 set_exception_code(ec_bound_ref_mod_e);
406 gg_assign(reflen, gg_cast(LONG, integer_one_node));
407 }
408 ELSE
409 {
410 IF( gg_add(refstart, reflen),
411 gt_op,
412 gg_cast(TREE_TYPE(refstart), get_any_capacity(refer.field)) )
413 {
414 // Start + Length is too large, which yet again is an error
415 // condition
416 set_exception_code(ec_bound_ref_mod_e);
417
418 // Our intentions are honorable. But at this point, where
419 // we notice that start + length is too long, the
420 // get_data_offset routine has already been run and
421 // it's too late to actually change the refstart. There are
422 // theoretical solutions to this -- mainly,
423 // get_data_offset needs to check the start + len for
424 // validity. But I am not going to do it now. Think of this
425 // as the TODO item.
426 gg_assign(refstart, gg_cast(LONG, integer_zero_node));
427 gg_assign(reflen, gg_cast(LONG, integer_one_node));
428 }
429 ELSE
430 {
431 // There are no problems, so there is no error condition, and
432 // refstart and reflen are correct.
433 }
434 ENDIF
435 }
436 ENDIF
437 }
438 ENDIF
439 }
440 else
441 {
442 // There is no refmod length, so we default to the remaining characters
443 gg_assign(reflen, gg_subtract(get_any_capacity(refer.field),
444 refstart));
445 }
446 }
447 ENDIF
448 }
449 ENDIF
450 }
451
452 void
453 get_depending_on_value_from_odo(tree retval, cbl_field_t *odo)
454 {
455 /* This routine, called only when we know there is an OCCURS DEPENDING ON
456 clause, returns the current value of the DEPENDING ON variable. When
457 ec_bound_odo_e is turned on, and there is any kind of ec-bound-odo
458 error condition, the value returned is occurs.bounds.lower.
459
460 This should ensure that there is no memory violation in the event of a
461 declarative with a RESUME NEXT STATEMENT, or before the default_condition
462 processing can do a controlled exit.
463 */
464 const cbl_enabled_exceptions_t&
465 enabled_exceptions( cdf_enabled_exceptions() );
466 cbl_field_t *depending_on;
467 depending_on = cbl_field_of(symbol_at(odo->occurs.depending_on));
468
469 if( !enabled_exceptions.match(ec_bound_odo_e) )
470 {
471 // With no exception testing, just pick up the value. If there is a
472 // the programmer will simply have to live with the consequences.
473 get_integer_value(retval,
474 depending_on,
475 NULL);
476 return;
477 }
478
479 // Bounds checking is enabled, so we test the DEPENDING ON value to be
480 // between the lower and upper OCCURS limits:
481 get_integer_value(retval,
482 depending_on,
483 NULL,
484 CHECK_FOR_FRACTIONAL_DIGITS);
485
486 IF( var_decl_rdigits, ne_op, integer_zero_node )
487 {
488 // This needs to evaluate to an integer
489 set_exception_code(ec_bound_odo_e);
490 gg_assign(retval, build_int_cst_type( TREE_TYPE(retval),
491 odo->occurs.bounds.lower));
492 gg_assign(var_decl_rdigits, integer_zero_node);
493 }
494 ELSE
495 ENDIF
496
497 IF( retval, gt_op, build_int_cst_type(TREE_TYPE(retval),
498 odo->occurs.bounds.upper) )
499 {
500 set_exception_code(ec_bound_odo_e);
501 gg_assign(retval, build_int_cst_type( TREE_TYPE(retval),
502 odo->occurs.bounds.lower));
503 }
504 ELSE
505 {
506 IF( retval, lt_op, build_int_cst_type(TREE_TYPE(retval),
507 odo->occurs.bounds.lower) )
508 {
509 set_exception_code(ec_bound_odo_e);
510 gg_assign(retval, build_int_cst_type( TREE_TYPE(retval),
511 odo->occurs.bounds.lower));
512 }
513 ELSE
514 ENDIF
515 IF( retval, lt_op, gg_cast(TREE_TYPE(retval), integer_zero_node) )
516 {
517 set_exception_code(ec_bound_odo_e);
518 gg_assign(retval, gg_cast(TREE_TYPE(retval), integer_zero_node));
519 }
520 ELSE
521 ENDIF
522 }
523 ENDIF
524 }
525
526 static
527 void
528 get_depending_on_value(tree retval, const cbl_refer_t &refer)
529 {
530 /* This routine, called only when we know there is an OCCURS DEPENDING ON
531 clause, returns the current value of the DEPENDING ON variable. When
532 ec_bound_odo_e is turned on, and there is any kind of ec-bound-odo
533 error condition, the value returned is occurs.bounds.lower.
534
535 This should ensure that there is no memory violation in the event of a
536 declarative with a RESUME NEXT STATEMENT, or before the default_condition
537 processing can do a controlled exit.
538 */
539 cbl_field_t *odo = symbol_find_odo(refer.field);
540 get_depending_on_value_from_odo(retval, odo);
541 }
542
543 static
544 tree
545 get_data_offset(const cbl_refer_t &refer,
546 int *pflags = NULL)
547 {
548 Analyze();
549 // This routine returns a tree which is the size_t offset to the data in the
550 // refer/field
551
552 // Because this is for source / sending variables, checks are made for
553 // OCCURS DEPENDING ON violations (when those exceptions are enabled)
554
555 tree retval = gg_define_variable(SIZE_T);
556 gg_assign(retval, size_t_zero_node);
557
558 // We have a refer.
559 // At the very least, we have an constant offset
560 int all_flags = 0;
561
562 if( refer.nsubscript() )
563 {
564 REFER("subscript");
565 // We have at least one subscript:
566
567 // Figure we have three subscripts, so nsubscript is 3
568 // Figure that the subscripts are {5, 4, 3}
569
570 // We expect that starting from refer.field, that three of our ancestors --
571 // call them A1, A2, and A3 -- have occurs clauses.
572
573 // We need to start with the rightmost subscript, and work our way up through
574 // our parents. As we find each parent with an OCCURS, we increment qual_data
575 // by (subscript-1)*An->data.capacity
576
577 // Establish the field_t pointer for walking up through our ancestors:
578 cbl_field_t *parent = refer.field;
579
580 int all_flag_bit = 1;
581 // Note the backwards test, because refer->nsubscript is an unsigned value
582 for(size_t i=refer.nsubscript()-1; i<refer.nsubscript(); i-- )
583 {
584 // We need to search upward for an ancestor with occurs_max:
585 while(parent)
586 {
587 if( parent->occurs.ntimes() )
588 {
589 break;
590 }
591 parent = parent_of(parent);
592 }
593 // we might have an error condition at this point:
594 if( !parent )
595 {
596 cbl_internal_error("Too many subscripts");
597 }
598 // Pick up the integer value of the subscript:
599 tree subscript = gg_define_variable(LONG);
600
601 if( (refer.subscripts[i].field->attr & FIGCONST_MASK) == zero_value_e )
602 {
603 // This refer is a figconst ZERO; we treat it as an ALL ZERO
604 // This is our internal representation for ALL, as in TABLE(ALL)
605
606 // Set the subscript to 1
607 gg_assign(subscript,
608 build_int_cst_type( TREE_TYPE(subscript), 1));
609 // Flag this position as ALL
610 all_flags |= all_flag_bit;
611 }
612 else
613 {
614 const cbl_enabled_exceptions_t&
615 enabled_exceptions( cdf_enabled_exceptions() );
616 if( !enabled_exceptions.match(ec_bound_subscript_e) )
617 {
618 // With no exception testing, just pick up the value
619 get_integer_value(subscript,
620 refer.subscripts[i].field,
621 refer_offset(refer.subscripts[i]));
622 }
623 else
624 {
625 get_integer_value(subscript,
626 refer.subscripts[i].field,
627 refer_offset(refer.subscripts[i]),
628 CHECK_FOR_FRACTIONAL_DIGITS);
629 IF( var_decl_rdigits,
630 ne_op,
631 integer_zero_node )
632 {
633 // The subscript isn't an integer
634 set_exception_code(ec_bound_subscript_e);
635 gg_assign(subscript, build_int_cst_type(TREE_TYPE(subscript), 1));
636 gg_assign(var_decl_rdigits, integer_zero_node);
637 }
638 ELSE
639 {
640 IF( subscript, lt_op, gg_cast(TREE_TYPE(subscript),
641 integer_one_node) )
642 {
643 // The subscript is too small
644 set_exception_code(ec_bound_subscript_e);
645 gg_assign(subscript, build_int_cst_type(TREE_TYPE(subscript),
646 1));
647 }
648 ELSE
649 {
650 IF( subscript,
651 ge_op,
652 build_int_cst_type( TREE_TYPE(subscript),
653 parent->occurs.ntimes()) )
654 {
655 // The subscript is too large
656 set_exception_code(ec_bound_subscript_e);
657 gg_assign(subscript, build_int_cst_type(TREE_TYPE(subscript),
658 1));
659 }
660 ELSE
661 {
662 }
663 ENDIF
664 }
665 ENDIF
666 }
667 ENDIF
668 }
669 }
670
671 all_flag_bit <<= 1;
672
673 // Although we strictly don't need to look at the ODO value at this
674 // point, we do want it checked for the purposes of ec-bound-odo
675
676 const cbl_enabled_exceptions_t&
677 enabled_exceptions( cdf_enabled_exceptions() );
678
679 if( enabled_exceptions.match(ec_bound_odo_e) )
680 {
681 if( parent->occurs.depending_on )
682 {
683 static tree value64 = gg_define_variable( LONG,
684 ".._gdos_value64",
685 vs_file_static);
686 cbl_field_t *odo = symbol_find_odo(parent);
687 get_depending_on_value_from_odo(value64, odo);
688 }
689 }
690
691 // Subscript is now a one-based integer
692 // Make it zero-based:
693
694 gg_decrement(subscript);
695
696 tree augment = gg_multiply(subscript, get_any_capacity(parent));
697 gg_assign(retval, gg_add(retval, gg_cast(SIZE_T, augment)));
698
699 parent = parent_of(parent);
700 }
701 }
702
703 if( refer.refmod.from )
704 {
705 REFER("refmod refstart");
706 // We have a refmod to deal with
707 static tree refstart = gg_define_variable(LONG, "..gdo_refstart", vs_file_static);
708 static tree reflen = gg_define_variable(LONG, "..gdo_reflen", vs_file_static);
709 get_and_check_refstart_and_reflen(refstart, reflen, refer);
710
711 gg_assign(retval, gg_add(retval, gg_cast(SIZE_T, refstart)));
712 }
713
714 if( pflags )
715 {
716 *pflags = all_flags;
717 }
718
719 return retval;
720 }
721
722 static tree tree_type_from_field(const cbl_field_t *field);
723
724 void
725 get_binary_value( tree value,
726 tree rdigits,
727 cbl_field_t *field,
728 tree field_offset,
729 tree hilo
730 )
731 {
732 Analyze();
733 if( hilo )
734 {
735 gg_assign(hilo, integer_zero_node);
736 }
737
738 bool needs_scaling = true;
739 static const bool debugging=false;
740
741 // Very special case:
742 if( strcmp(field->name, "ZEROS") == 0 )
743 {
744 gg_assign(value, gg_cast(TREE_TYPE(value), integer_zero_node));
745 if( rdigits )
746 {
747 gg_assign(rdigits, gg_cast(TREE_TYPE(rdigits), integer_zero_node));
748 }
749 return;
750 }
751
752 static tree pointer = gg_define_variable(UCHAR_P, "..gbv_pointer", vs_file_static);
753 static tree pend = gg_define_variable(UCHAR_P, "..gbv_pend", vs_file_static);
754
755 switch(field->type)
756 {
757 case FldLiteralN:
758 {
759 if( SCALAR_FLOAT_TYPE_P(value) )
760 {
761 cbl_internal_error("cannot get %<float%> value from %s", field->name);
762 }
763 else
764 {
765 if( rdigits )
766 {
767 gg_assign(rdigits, build_int_cst_type(TREE_TYPE(rdigits),
768 field->data.rdigits));
769 }
770 tree dest_type = TREE_TYPE(value);
771 tree source_type = tree_type_from_field(field);
772
773 gg_assign(value,
774 gg_cast(dest_type,
775 gg_indirect( gg_cast(build_pointer_type(source_type),
776 gg_get_address_of(field->data_decl_node)))));
777 }
778
779 break;
780 }
781
782 case FldNumericDisplay:
783 {
784 Analyzer.Message("FldNumericDisplay");
785 // Establish the source
786 tree source_address = get_data_address(field, field_offset);
787
788 // We need to check early on for HIGH-VALUE and LOW-VALUE
789 // Pick up the byte
790 tree digit = gg_get_indirect_reference(source_address, NULL_TREE);
791 IF( digit, eq_op, build_int_cst(UCHAR, 0xFF) )
792 {
793 if( hilo )
794 {
795 gg_assign(hilo, integer_one_node);
796 }
797 if( rdigits )
798 {
799 gg_assign(rdigits,
800 build_int_cst_type( TREE_TYPE(rdigits),
801 get_scaled_rdigits(field)));
802 }
803 gg_assign(value, build_int_cst_type(TREE_TYPE(value), 0xFFFFFFFFFFFFFFFUL));
804 }
805 ELSE
806 {
807 IF( digit, eq_op, build_int_cst(UCHAR, 0x00) )
808 {
809 if( hilo )
810 {
811 gg_assign(hilo, integer_minus_one_node);
812 }
813 }
814 ELSE
815 {
816 // Establish rdigits:
817 if( rdigits )
818 {
819 gg_assign(rdigits,
820 build_int_cst_type( TREE_TYPE(rdigits),
821 get_scaled_rdigits(field)));
822 }
823 // Zero out the destination
824 gg_assign(value, gg_cast(TREE_TYPE(value), integer_zero_node));
825 // Pick up a pointer to the source bytes:
826
827 gg_assign(pointer, source_address);
828
829 // This is the we-are-done pointer
830 gg_assign(pend, gg_add( pointer,
831 get_any_capacity(field)));
832
833 static tree signbyte = gg_define_variable(UCHAR, "..gbv_signbyte", vs_file_static);
834
835 // The big decision is whether or not the variable is signed:
836 if( field->attr & signable_e )
837 {
838 // The variable is signed
839 if( field->attr & separate_e )
840 {
841 // The sign byte is separate
842 if( field->attr & leading_e)
843 {
844 // The first byte is '+' or '-'
845 gg_increment(pointer);
846 }
847 else
848 {
849 // The final byte is '+' or '-'
850 gg_decrement(pend);
851 }
852 }
853 else
854 {
855 // The sign byte is internal
856 if( field->attr & leading_e)
857 {
858 // The first byte has the sign bit:
859 gg_assign(signbyte,
860 gg_get_indirect_reference(source_address, NULL_TREE));
861 if( internal_codeset_is_ebcdic() )
862 {
863 // We need to make sure the EBCDIC sign bit is ON, for positive
864 gg_assign(gg_get_indirect_reference(source_address, NULL_TREE),
865 gg_bitwise_or(signbyte,
866 build_int_cst_type( UCHAR,
867 NUMERIC_DISPLAY_SIGN_BIT)));
868 }
869 else
870 {
871 // We need to make sure the ascii sign bit is Off, for positive
872 gg_assign(gg_get_indirect_reference(source_address, NULL_TREE),
873 gg_bitwise_and( signbyte,
874 build_int_cst_type( UCHAR,
875 ~NUMERIC_DISPLAY_SIGN_BIT)));
876 }
877 }
878 else
879 {
880 // The final byte has the sign bit:
881 gg_assign(signbyte,
882 gg_get_indirect_reference(source_address,
883 build_int_cst_type(SIZE_T,
884 field->data.capacity-1)));
885 if( internal_codeset_is_ebcdic() )
886 {
887 // We need to make sure the EBCDIC sign bit is ON, for positive
888 gg_assign(gg_get_indirect_reference(source_address,
889 build_int_cst_type( SIZE_T,
890 field->data.capacity-1)),
891 gg_bitwise_or(signbyte,
892 build_int_cst_type( UCHAR,
893 NUMERIC_DISPLAY_SIGN_BIT)));
894 }
895 else
896 {
897 // We need to make sure the ASCII sign bit is Off, for positive
898 gg_assign(gg_get_indirect_reference(source_address,
899 build_int_cst_type( SIZE_T,
900 field->data.capacity-1)),
901 gg_bitwise_and( signbyte,
902 build_int_cst_type( UCHAR,
903 ~NUMERIC_DISPLAY_SIGN_BIT)));
904 }
905 }
906 }
907 }
908 // We can now set up the byte-by-byte processing loop:
909 if( internal_codeset_is_ebcdic() )
910 {
911 // We are working in EBCDIC
912 WHILE( pointer, lt_op, pend )
913 {
914 // Pick up the byte
915 digit = gg_get_indirect_reference(pointer, NULL_TREE);
916 IF( digit, lt_op, build_int_cst_type(UCHAR, EBCDIC_ZERO) )
917 {
918 // break on a non-digit
919 gg_assign(pointer, pend);
920 }
921 ELSE
922 {
923 IF( digit, gt_op, build_int_cst_type(UCHAR, EBCDIC_NINE) )
924 {
925 // break on a non-digit
926 gg_assign(pointer, pend);
927 }
928 ELSE
929 {
930 // Whether ASCII or EBCDIC, the bottom four bits tell the tale:
931 // Multiply our accumulator by ten:
932 gg_assign(value, gg_multiply(value, build_int_cst_type(TREE_TYPE(value), 10)));
933 // And add in the current digit
934 gg_assign(value,
935 gg_add(value, gg_cast(TREE_TYPE(value), gg_bitwise_and( digit,
936 build_int_cst_type(UCHAR, 0x0F) ))));
937 gg_increment(pointer);
938 }
939 ENDIF
940 }
941 ENDIF
942 }
943 WEND
944 }
945 else
946 {
947 // We are working in ASCII:
948 WHILE( pointer, lt_op, pend )
949 {
950 // Pick up the byte
951 digit = gg_get_indirect_reference(pointer, NULL_TREE);
952 // Whether ASCII or EBCDIC, the bottom four bits tell the tale:
953 // Multiply our accumulator by ten:
954 gg_assign(value, gg_multiply(value, build_int_cst_type(TREE_TYPE(value), 10)));
955 // And add in the current digit
956 gg_assign(value, gg_add(value, gg_cast(TREE_TYPE(value), gg_bitwise_and(digit, build_int_cst_type(UCHAR, 0x0F)))));
957 gg_increment(pointer);
958 }
959 WEND
960 }
961
962 // Value contains the binary value. The last thing is to apply -- and
963 // undo -- the signable logic:
964
965 if( field->attr & signable_e )
966 {
967 // The variable is signed
968 if( field->attr & separate_e )
969 {
970 // The sign byte is separate
971 if( field->attr & leading_e)
972 {
973 // The first byte is '+' or '-'
974 if( internal_codeset_is_ebcdic() )
975 {
976 // We are operating in EBCDIC, so we look for a 96 (is minus sign)
977 IF( gg_get_indirect_reference(source_address, NULL_TREE),
978 eq_op,
979 build_int_cst_type(UCHAR, 96) )
980 {
981 gg_assign(value, gg_negate(value));
982 }
983 ELSE
984 ENDIF
985 }
986 else
987 {
988 // We are operating in ASCII
989 IF( gg_get_indirect_reference(source_address, NULL_TREE),
990 eq_op,
991 build_int_cst_type(UCHAR, '-') )
992 {
993 gg_assign(value, gg_negate(value));
994 }
995 ELSE
996 ENDIF
997 }
998 }
999 else
1000 {
1001 // The final byte is '+' or '-'
1002 if( internal_codeset_is_ebcdic() )
1003 {
1004 // We are operating in EBCDIC, so we look for a 96 (is minus sign)
1005 IF( gg_get_indirect_reference(source_address, build_int_cst_type(SIZE_T, field->data.capacity-1)),
1006 eq_op,
1007 build_int_cst_type(UCHAR, 96) )
1008 {
1009 gg_assign(value, gg_negate(value));
1010 }
1011 ELSE
1012 ENDIF
1013 }
1014 else
1015 {
1016 // We are operating in ASCII
1017 IF( gg_get_indirect_reference(source_address, build_int_cst_type(SIZE_T, field->data.capacity-1)),
1018 eq_op,
1019 build_int_cst_type(UCHAR, '-') )
1020 {
1021 gg_assign(value, gg_negate(value));
1022 }
1023 ELSE
1024 ENDIF
1025 }
1026 }
1027 }
1028 else
1029 {
1030 // The sign byte is internal. Check the sign bit
1031 if(internal_codeset_is_ebcdic())
1032 {
1033 IF( gg_bitwise_and( signbyte,
1034 build_int_cst_type( UCHAR,
1035 NUMERIC_DISPLAY_SIGN_BIT)), eq_op, build_int_cst_type(UCHAR, 0) )
1036 {
1037 // The EBCDIC sign bit was OFF, so negate the result
1038 gg_assign(value, gg_negate(value));
1039 }
1040 ELSE
1041 ENDIF
1042 }
1043 else
1044 {
1045 IF( gg_bitwise_and( signbyte,
1046 build_int_cst_type( UCHAR,
1047 NUMERIC_DISPLAY_SIGN_BIT)), ne_op, build_int_cst_type(UCHAR, 0) )
1048 {
1049 // The ASCII sign bit was on, so negate the result
1050 gg_assign(value, gg_negate(value));
1051 }
1052 ELSE
1053 ENDIF
1054 }
1055 // It's time to put back the original data:
1056 if( field->attr & leading_e)
1057 {
1058 // The first byte has the sign bit:
1059 gg_assign(gg_get_indirect_reference(source_address, NULL_TREE),
1060 signbyte);
1061 }
1062 else
1063 {
1064 // The final byte has the sign bit:
1065 gg_assign(gg_get_indirect_reference(source_address,
1066 build_int_cst_type(SIZE_T, field->data.capacity-1)),
1067 signbyte);
1068 }
1069 }
1070 }
1071 }
1072 ENDIF
1073 }
1074 ENDIF
1075
1076 break;
1077 }
1078
1079 case FldNumericBinary:
1080 {
1081 // As of this writing, the source value is big-endian
1082 // We have to convert it to a little-endian destination.
1083 tree dest = gg_cast(build_pointer_type(UCHAR), gg_get_address_of(value));
1084 tree source = get_data_address(field, field_offset);
1085
1086 size_t dest_nbytes = gg_sizeof(value);
1087 size_t source_nbytes = field->data.capacity;
1088
1089 if( debugging )
1090 {
1091 gg_printf("dest_bytes/source_bytes %ld/%ld\n",
1092 build_int_cst_type(SIZE_T, dest_nbytes),
1093 build_int_cst_type(SIZE_T, source_nbytes),
1094 NULL_TREE);
1095 gg_printf("Starting value: ", NULL_TREE);
1096 hex_dump(source, source_nbytes);
1097 gg_printf("\n", NULL_TREE);
1098 }
1099
1100 if( dest_nbytes <= source_nbytes )
1101 {
1102 // Destination is too small. We will move what we can, throwing away
1103 // the most significant source bytes:
1104 for(size_t i=0; i<dest_nbytes; i++)
1105 {
1106 gg_assign(gg_array_value(dest, i),
1107 gg_array_value(source, source_nbytes-1-i) );
1108 }
1109 }
1110 else
1111 {
1112 // Destination is too big. We'll need to fill the high-order bytes with
1113 // either 0x00 for positive numbers, or 0xFF for negative
1114 static tree extension = gg_define_variable( UCHAR,
1115 "..gbv_extension",
1116 vs_file_static);
1117 if( field->attr & signable_e )
1118 {
1119 IF( gg_array_value(gg_cast(build_pointer_type(SCHAR), source)), lt_op, gg_cast(SCHAR, integer_zero_node) )
1120 {
1121 gg_assign(extension, build_int_cst_type(UCHAR, 0xFF));
1122 }
1123 ELSE
1124 {
1125 gg_assign(extension, build_int_cst_type(UCHAR, 0));
1126 }
1127 ENDIF
1128 }
1129 else
1130 {
1131 gg_assign(extension, build_int_cst_type(UCHAR, 0));
1132 }
1133
1134 // Flip the source end-for-end and put it into the dest:
1135 size_t i=0;
1136 while(i < source_nbytes)
1137 {
1138 gg_assign(gg_array_value(dest, i),
1139 gg_array_value(source, source_nbytes-1-i) );
1140 i += 1;
1141 }
1142 // Fill the extra high-end bytes with 0x00 or 0xFF extension
1143
1144 while(i < dest_nbytes)
1145 {
1146 gg_assign(gg_array_value(dest, i),
1147 extension);
1148 i += 1;
1149 }
1150 }
1151 if( debugging )
1152 {
1153 gg_printf("Ending value: ", NULL_TREE);
1154 hex_dump(dest, dest_nbytes);
1155 gg_printf("\n", NULL_TREE);
1156 }
1157 break;
1158 }
1159
1160 case FldNumericBin5:
1161 case FldIndex:
1162 case FldPointer:
1163 {
1164 if( field->attr & intermediate_e )
1165 {
1166 // It is a intermediate, so rdigits has to come from the run-time structure
1167 if( rdigits )
1168 {
1169 gg_assign(rdigits,
1170 gg_cast( TREE_TYPE(rdigits),
1171 member(field, "rdigits")));
1172 }
1173 }
1174 else
1175 {
1176 // It isn't an intermediate, so we can safely use field->rdigits
1177 if( rdigits )
1178 {
1179 gg_assign(rdigits,
1180 build_int_cst_type( TREE_TYPE(rdigits),
1181 get_scaled_rdigits(field)));
1182 }
1183 }
1184 tree source_address = get_data_address(field, field_offset);
1185 tree dest_type = TREE_TYPE(value);
1186 tree source_type = tree_type_from_size( field->data.capacity,
1187 field->attr & signable_e);
1188 if( debugging && rdigits)
1189 {
1190 gg_printf("get_binary_value bin5 rdigits: %d\n", rdigits, NULL_TREE);
1191 }
1192
1193 gg_assign(value,
1194 gg_cast(dest_type,
1195 gg_indirect(gg_cast( build_pointer_type(source_type),
1196 source_address ))));
1197 break;
1198 }
1199
1200 case FldPacked:
1201 {
1202 // Zero out the destination:
1203 gg_assign(value, gg_cast(TREE_TYPE(value), integer_zero_node));
1204 gg_assign(pointer, get_data_address(field, field_offset));
1205 gg_assign(pend,
1206 gg_add(pointer,
1207 build_int_cst_type(SIZE_T, field->data.capacity-1)));
1208
1209 // Convert all but the last byte of the packed decimal sequence
1210 WHILE( pointer, lt_op, pend )
1211 {
1212 // Convert the first nybble
1213 gg_assign(value, gg_multiply(value, build_int_cst_type(TREE_TYPE(value), 10)));
1214 gg_assign(value, gg_add(value, gg_cast(TREE_TYPE(value), gg_rshift(gg_get_indirect_reference(pointer, NULL_TREE), build_int_cst(UINT, 4)))));
1215
1216 // Convert the second nybble
1217 gg_assign(value, gg_multiply(value, build_int_cst_type(TREE_TYPE(value), 10)));
1218 gg_assign(value, gg_add(value, gg_cast(TREE_TYPE(value), gg_bitwise_and(gg_get_indirect_reference(pointer, NULL_TREE), build_int_cst_type(UCHAR, 0xF)))));
1219 gg_increment(pointer);
1220 }
1221 WEND
1222
1223 // This is the final byte:
1224 gg_assign(value, gg_multiply(value, build_int_cst_type(TREE_TYPE(value), 10)));
1225 gg_assign(value, gg_add(value, gg_cast(TREE_TYPE(value), gg_rshift(gg_get_indirect_reference(pointer, NULL_TREE), build_int_cst(UINT, 4)))));
1226
1227 IF( gg_bitwise_and(gg_get_indirect_reference(pointer, NULL_TREE), build_int_cst_type(UCHAR, 0xF)), eq_op, build_int_cst_type(UCHAR, 0x0D) )
1228 {
1229 gg_assign(value, gg_negate(value));
1230 }
1231 ELSE
1232 {
1233 IF( gg_bitwise_and(gg_get_indirect_reference(pointer, NULL_TREE), build_int_cst_type(UCHAR, 0xF)), eq_op, build_int_cst_type(UCHAR, 0x0B) )
1234 {
1235 gg_assign(value, gg_negate(value));
1236 }
1237 ELSE
1238 ENDIF
1239 }
1240 ENDIF
1241 break;
1242 }
1243
1244 case FldFloat:
1245 {
1246 // We are going to assume that the float value contains an integer.
1247 if( rdigits )
1248 {
1249 gg_assign(rdigits,
1250 gg_cast( TREE_TYPE(rdigits), integer_zero_node));
1251 }
1252 gg_assign(value,
1253 gg_cast(TREE_TYPE(value),
1254 gg_call_expr( INT128,
1255 "__gg__integer_from_float128",
1256 gg_get_address_of(field->var_decl_node),
1257 NULL_TREE)));
1258 needs_scaling = false;
1259 break;
1260 }
1261
1262 default:
1263 {
1264 char *err = xasprintf("%s(): We know not how to"
1265 " get a binary value from %s\n",
1266 __func__,
1267 cbl_field_type_str(field->type) );
1268 cbl_internal_error("%s", err);
1269 abort();
1270 // break; // break not needed after abort();
1271 }
1272 }
1273
1274 if( needs_scaling )
1275 {
1276 if( field->attr & scaled_e )
1277 {
1278 if( field->data.rdigits < 0 )
1279 {
1280 scale_by_power_of_ten_N(value, -field->data.rdigits);
1281 }
1282 }
1283 }
1284 }
1285
1286 static tree
1287 tree_type_from_field(const cbl_field_t *field)
1288 {
1289 gcc_assert(field);
1290 return tree_type_from_size(field->data.capacity, field->attr & signable_e);
1291 }
1292
1293 tree
1294 get_data_address( cbl_field_t *field,
1295 tree offset) // Offset is SIZE_T
1296 {
1297 if( offset )
1298 {
1299 return gg_cast( UCHAR_P,
1300 gg_add( gg_cast(SIZE_T,
1301 member( field->var_decl_node,
1302 "data")),
1303 offset));
1304 }
1305 else
1306 {
1307 return member(field->var_decl_node, "data");
1308 }
1309 }
1310
1311 FIXED_WIDE_INT(128)
1312 get_power_of_ten(int n)
1313 {
1314 // 2** 64 = 1.8E19
1315 // 2**128 = 3.4E38
1316 FIXED_WIDE_INT(128) retval = 1;
1317 static const int MAX_POWER = 19 ;
1318 static const unsigned long long pos[MAX_POWER+1] =
1319 {
1320 1ULL, // 00
1321 10ULL, // 01
1322 100ULL, // 02
1323 1000ULL, // 03
1324 10000ULL, // 04
1325 100000ULL, // 05
1326 1000000ULL, // 06
1327 10000000ULL, // 07
1328 100000000ULL, // 08
1329 1000000000ULL, // 09
1330 10000000000ULL, // 10
1331 100000000000ULL, // 11
1332 1000000000000ULL, // 12
1333 10000000000000ULL, // 13
1334 100000000000000ULL, // 14
1335 1000000000000000ULL, // 15
1336 10000000000000000ULL, // 16
1337 100000000000000000ULL, // 17
1338 1000000000000000000ULL, // 18
1339 10000000000000000000ULL, // 19
1340 };
1341 if( n < 0 || n>MAX_POWER*2) // The most we can handle is 10**38
1342 {
1343 fprintf(stderr, "Trying to raise 10 to %d as an int128, which we can't do.\n", n);
1344 fprintf(stderr, "The problem is in %s.\n", __func__);
1345 abort();
1346 }
1347 if( n <= MAX_POWER )
1348 {
1349 // Up to 10**18 we do directly:
1350 retval = pos[n];
1351 }
1352 else
1353 {
1354 // 19 through 38 is handled in a second step, because when this was written,
1355 // GCC couldn't handle 128-bit constants:
1356 retval = pos[n/2];
1357 retval *= retval;
1358 if( n & 1 )
1359 {
1360 retval *= 10;
1361 }
1362 }
1363 return retval;
1364 }
1365
1366 void
1367 scale_by_power_of_ten_N(tree value,
1368 int N,
1369 bool check_for_fractional)
1370 {
1371 // This routine is called when we know N at compile time.
1372
1373 Analyze();
1374 Analyzer.Message("takes int N");
1375 if( N == 0 )
1376 {
1377 }
1378 else if( N > 0 )
1379 {
1380 tree value_type = TREE_TYPE(value);
1381 FIXED_WIDE_INT(128) power_of_ten = get_power_of_ten(N);
1382 gg_assign(value, gg_multiply(value, wide_int_to_tree( value_type,
1383 power_of_ten)));
1384 }
1385 if( N < 0 )
1386 {
1387 tree value_type = TREE_TYPE(value);
1388 FIXED_WIDE_INT(128) power_of_ten = get_power_of_ten(-N);
1389 if( check_for_fractional )
1390 {
1391 IF( gg_mod(value, wide_int_to_tree( value_type,
1392 power_of_ten)),
1393 ne_op,
1394 gg_cast(value_type, integer_zero_node) )
1395 {
1396 gg_assign(var_decl_rdigits, integer_one_node);
1397 }
1398 ELSE
1399 ENDIF
1400 }
1401 gg_assign(value, gg_divide(value, wide_int_to_tree( value_type,
1402 power_of_ten)));
1403 }
1404 }
1405
1406 tree
1407 scale_by_power_of_ten(tree value,
1408 tree N,
1409 bool check_for_fractional)
1410 {
1411 Analyze();
1412 static tree retval = gg_define_variable(INT, "..sbpot2_retval", vs_file_static);
1413
1414 if( check_for_fractional )
1415 {
1416 // Our caller expects us to return 1 if value was something like 99v99 and
1417 // the fractional part was non-zero
1418 gg_assign(value,
1419 gg_cast(TREE_TYPE(value),
1420 gg_call_expr(INT128,
1421 "__gg__scale_by_power_of_ten_1",
1422 gg_cast(INT128, value),
1423 N,
1424 NULL_TREE)));
1425 }
1426 else
1427 {
1428 // Our caller does not expect us to test for fractional values
1429 gg_assign(value,
1430 gg_cast(TREE_TYPE(value),
1431 gg_call_expr(INT128,
1432 "__gg__scale_by_power_of_ten_2",
1433 gg_cast(INT128, value),
1434 N,
1435 NULL_TREE)));
1436
1437 }
1438
1439 gg_assign(retval, integer_zero_node);
1440 return retval;
1441 }
1442
1443 void
1444 scale_and_round(tree value,
1445 int value_rdigits,
1446 bool target_is_signable,
1447 int target_rdigits,
1448 cbl_round_t rounded)
1449 {
1450 if( !target_is_signable )
1451 {
1452 // The target has to be positive, so take the absolute value of the input
1453 gg_assign(value, gg_abs(value));
1454 }
1455
1456 if( target_rdigits >= value_rdigits )
1457 {
1458 // The value doesn't have enough rdigits. All we need to do is multiply it
1459 // by a power of ten to get it right:
1460 scale_by_power_of_ten_N(value,
1461 target_rdigits - value_rdigits);
1462 }
1463 else
1464 {
1465 // The value has too few rdigits.
1466 switch(rounded)
1467 {
1468 case nearest_away_from_zero_e:
1469 {
1470 // This is rounding away from zero
1471
1472 // We want to adjust value so that the extra digit is in the units
1473 // place:
1474 scale_by_power_of_ten_N(value,
1475 target_rdigits - value_rdigits + 1);
1476 // Add five to the result:
1477 IF( value, lt_op, gg_cast(TREE_TYPE(value), integer_zero_node) )
1478 {
1479 gg_assign(value,
1480 gg_add( value,
1481 build_int_cst_type(TREE_TYPE(value), -5)));
1482 }
1483 ELSE
1484 {
1485 gg_assign(value,
1486 gg_add( value,
1487 build_int_cst_type(TREE_TYPE(value), +5)));
1488 }
1489 // And now get rid of the lowest decimal digit
1490 scale_by_power_of_ten_N(value, -1);
1491
1492 break;
1493 }
1494
1495 case truncation_e:
1496 {
1497 // Without rounding, just scale the result
1498 scale_by_power_of_ten_N(value, target_rdigits - value_rdigits);
1499 break;
1500 }
1501 default:
1502 abort();
1503 break;
1504 }
1505 }
1506 }
1507
1508 void
1509 hex_dump(tree data, size_t bytes)
1510 {
1511 gg_printf("0x", NULL_TREE);
1512 for(size_t i=0; i<bytes; i++)
1513 {
1514 gg_printf("%2.2x",
1515 gg_cast(UINT,
1516 gg_array_value( gg_cast(build_pointer_type(UCHAR), data),
1517 i)),
1518 NULL_TREE);
1519 }
1520 }
1521
1522 tree
1523 tree_type_from_size(size_t bytes, int signable)
1524 {
1525 tree retval = NULL_TREE;
1526
1527 if( signable )
1528 {
1529 switch( bytes )
1530 {
1531 case 1:
1532 retval = CHAR;
1533 break;
1534 case 2:
1535 retval = SHORT;
1536 break;
1537 case 4:
1538 retval = INT;
1539 break;
1540 case 8:
1541 retval = LONGLONG;
1542 break;
1543 case 16:
1544 retval = INT128;
1545 break;
1546 default:
1547 gcc_unreachable();
1548 break;
1549 }
1550 }
1551 else
1552 {
1553 switch( bytes )
1554 {
1555 case 1:
1556 retval = UCHAR;
1557 break;
1558 case 2:
1559 retval = USHORT;
1560 break;
1561 case 4:
1562 retval = UINT;
1563 break;
1564 case 8:
1565 retval = ULONGLONG;
1566 break;
1567 case 16:
1568 retval = UINT128;
1569 break;
1570 default:
1571 gcc_unreachable();
1572 break;
1573 }
1574 }
1575 return retval;
1576 }
1577
1578 static
1579 bool
1580 refer_has_depends(const cbl_refer_t &refer, refer_type_t refer_type)
1581 {
1582 if( suppress_dest_depends )
1583 {
1584 // This is set, for example, by parser_initialize, which needs to set a
1585 // variable's value regardless of the impact of a DEPENDING ON clause.
1586 return false;
1587 }
1588
1589 if( refer.field
1590 && (refer.field->attr & (intermediate_e)) )
1591 {
1592 // This field can't have a DEPENDING ON
1593 return false;
1594 }
1595
1596 // Check if there there is an occurs with a depending_on in the hierarchy
1597 bool proceed = false;
1598 const cbl_field_t *odo = symbol_find_odo(refer.field);
1599 cbl_field_t *depending_on;
1600 if( odo && odo != refer.field )
1601 {
1602 // We have an ODO and refer.field is not the ODO, so we can keep looking
1603 depending_on = cbl_field_of(symbol_at(odo->occurs.depending_on));
1604 if( depending_on->var_decl_node )
1605 {
1606 // The depending_on has been initialized
1607 if( refer_type == refer_source )
1608 {
1609 proceed = true;
1610 }
1611 else
1612 {
1613 // In ISO/IEC 1989:2023, "OCCURS 13.18.38.4 General rules", talks about the
1614 // three situations we know how to deal with.
1615
1616 // Rule 7) We need to detect if depending_on is completely independent
1617 // of refer.field
1618 cbl_field_t *p;
1619 cbl_field_t *parent1 = refer.field;
1620 while( (p = parent_of(parent1)) )
1621 {
1622 parent1 = p;
1623 }
1624 const cbl_field_t *parent2 = depending_on;
1625 while( (p = parent_of(parent2)) )
1626 {
1627 parent2 = p;
1628 }
1629 if( parent1 != parent2 )
1630 {
1631 // refer.field and depending_on have two different ultimate parents, so
1632 // Rule 7) applies, and we have to trim the destination according to
1633 // depending_on
1634 //gg_printf("Rule 7 applies\n", NULL_TREE);
1635 proceed = true;
1636 }
1637 else
1638 {
1639 // Rule 7) doesn't apply, so we have to check Rule 8)
1640 // In this case:
1641 // 01 digtab.
1642 // 05 depl pic 9.
1643 // 05 digitgrp.
1644 // 10 digits occurs 1 to 9 depending on depl pic x.
1645 // MOVE ... TO digitgrp
1646 // The DEPENDING ON variable depl is not subordinate to digitgrp, and
1647 // consequently we have to trim according to depl:
1648 if( depending_on->offset < refer.field->offset )
1649 {
1650 // depending_on comes before refer.field, so rule 8a) applies
1651 //gg_printf("Rule 8a) applies\n", NULL_TREE);
1652 proceed = true;
1653 }
1654 else
1655 {
1656 // depending_on comes after refer.field, so Rule 8b) for receiving
1657 // items applies, and we will not trim according to depending_on
1658 //gg_printf("Rule 8b) applies\n", NULL_TREE);
1659 }
1660 }
1661 }
1662 }
1663 }
1664 return proceed;
1665 }
1666
1667 void
1668 set_exception_code_func(ec_type_t ec, int /*line*/, int from_raise_statement)
1669 {
1670 if( ec )
1671 {
1672 gg_call(VOID,
1673 "__gg__set_exception_code",
1674 build_int_cst_type(INT, ec),
1675 build_int_cst_type(INT, from_raise_statement),
1676 NULL_TREE);
1677 }
1678 else
1679 {
1680 gg_printf("set_exception_code: set it to ZERO\n", NULL_TREE);
1681 gg_assign(var_decl_exception_code, integer_zero_node);
1682 }
1683 }
1684
1685 bool
1686 process_this_exception(const ec_type_t ec)
1687 {
1688 const cbl_enabled_exceptions_t& enabled_exceptions( cdf_enabled_exceptions() );
1689 bool retval;
1690 if( enabled_exceptions.match(ec) || !skip_exception_processing )
1691 {
1692 retval = true;
1693 }
1694 else
1695 {
1696 retval = false;
1697 }
1698 return retval;
1699 }
1700
1701 void
1702 rt_error(const char *msg)
1703 {
1704 // Come here with a fatal run-time error message
1705 char ach[256];
1706 snprintf( ach, sizeof(ach), "%s:%d: %s",
1707 current_filename.back().c_str(),
1708 CURRENT_LINE_NUMBER,
1709 msg);
1710 gg_printf("%s\n", gg_string_literal(ach), NULL_TREE);
1711 gg_abort();
1712 }
1713
1714 void
1715 copy_little_endian_into_place(cbl_field_t *dest,
1716 tree dest_offset,
1717 tree value,
1718 int rhs_rdigits,
1719 bool check_for_error,
1720 const tree &size_error)
1721 {
1722 if( check_for_error )
1723 {
1724 // We need to see if value can fit into destref
1725
1726 // We do this by comparing value to 10^(lhs.ldigits + rhs_rdigits)
1727 // Example: rhs is 123.45, whichis 12345 with rdigits 2
1728 // lhs is 99.999. So, lhs.digits is 5, and lhs.rdigits is 3.
1729 // 10^(5 - 3 + 2) is 10^4, which is 10000. Because 12345 is >= 10000, the
1730 // source can't fit into the destination.
1731
1732 // Note: I am not trying to avoid the use of stack variables, because I am
1733 // not sure how to declare a file-static variable of unknown type.
1734 tree abs_value = gg_define_variable(TREE_TYPE(value));
1735 IF( value, lt_op, build_int_cst_type(TREE_TYPE(value), 0) )
1736 {
1737 gg_assign(abs_value, gg_negate(value));
1738 }
1739 ELSE
1740 {
1741 gg_assign(abs_value, value);
1742 }
1743 ENDIF
1744
1745 FIXED_WIDE_INT(128) power_of_ten = get_power_of_ten( dest->data.digits
1746 - dest->data.rdigits
1747 + rhs_rdigits );
1748 IF( gg_cast(INT128, abs_value),
1749 ge_op,
1750 wide_int_to_tree(INT128, power_of_ten) )
1751 {
1752 // Flag the size error
1753 gg_assign(size_error, integer_one_node);
1754 }
1755 ELSE
1756 ENDIF
1757 }
1758 scale_by_power_of_ten_N(value, dest->data.rdigits - rhs_rdigits);
1759
1760 tree dest_type = tree_type_from_size( dest->data.capacity,
1761 dest->attr & signable_e);
1762 tree dest_pointer = gg_add(member(dest->var_decl_node, "data"),
1763 dest_offset);
1764 gg_assign(gg_indirect(gg_cast(build_pointer_type(dest_type), dest_pointer)),
1765 gg_cast(dest_type, value));
1766 }
1767
1768 void
1769 build_array_of_treeplets( int ngroup,
1770 size_t N,
1771 cbl_refer_t *refers)
1772 {
1773 if( N )
1774 {
1775 // At the present time the most this routine is called is four times, for
1776 // the implementation of the UNSTRING verb.
1777
1778 if( N > MIN_FIELD_BLOCK_SIZE )
1779 {
1780 gg_call(VOID,
1781 "__gg__resize_treeplet",
1782 build_int_cst_type(INT, ngroup),
1783 build_int_cst_type(SIZE_T, N),
1784 NULL_TREE
1785 );
1786 }
1787 switch(ngroup)
1788 {
1789 case 1:
1790 for(size_t i=0; i<N; i++)
1791 {
1792 gg_assign(gg_array_value(var_decl_treeplet_1f, i),
1793 refers[i].field ? gg_get_address_of(refers[i].field->var_decl_node)
1794 : gg_cast(cblc_field_p_type_node, null_pointer_node));
1795 gg_assign(gg_array_value(var_decl_treeplet_1o, i),
1796 refer_offset(refers[i]));
1797 gg_assign(gg_array_value(var_decl_treeplet_1s, i),
1798 refer_size_source(refers[i]));
1799 }
1800 break;
1801 case 2:
1802 for(size_t i=0; i<N; i++)
1803 {
1804 gg_assign(gg_array_value(var_decl_treeplet_2f, i),
1805 refers[i].field ? gg_get_address_of(refers[i].field->var_decl_node)
1806 : gg_cast(cblc_field_p_type_node, null_pointer_node));
1807 gg_assign(gg_array_value(var_decl_treeplet_2o, i),
1808 refer_offset(refers[i]));
1809 gg_assign(gg_array_value(var_decl_treeplet_2s, i),
1810 refer_size_source(refers[i]));
1811 }
1812 break;
1813 case 3:
1814 for(size_t i=0; i<N; i++)
1815 {
1816 gg_assign(gg_array_value(var_decl_treeplet_3f, i),
1817 refers[i].field ? gg_get_address_of(refers[i].field->var_decl_node)
1818 : gg_cast(cblc_field_p_type_node, null_pointer_node));
1819 gg_assign(gg_array_value(var_decl_treeplet_3o, i),
1820 refer_offset(refers[i]));
1821 gg_assign(gg_array_value(var_decl_treeplet_3s, i),
1822 refer_size_source(refers[i]));
1823 }
1824 break;
1825 case 4:
1826 for(size_t i=0; i<N; i++)
1827 {
1828 gg_assign(gg_array_value(var_decl_treeplet_4f, i),
1829 refers[i].field ? gg_get_address_of(refers[i].field->var_decl_node)
1830 : gg_cast(cblc_field_p_type_node, null_pointer_node));
1831 gg_assign(gg_array_value(var_decl_treeplet_4o, i),
1832 refer_offset(refers[i]));
1833 gg_assign(gg_array_value(var_decl_treeplet_4s, i),
1834 refer_size_source(refers[i]));
1835 }
1836 break;
1837 default:
1838 abort();
1839 break;
1840 }
1841 }
1842 else
1843 {
1844 // Just do nothing
1845 }
1846 }
1847
1848 void
1849 build_array_of_fourplets( int ngroup,
1850 size_t N,
1851 cbl_refer_t *refers)
1852 {
1853 int flag_bits = 0;
1854 if( N )
1855 {
1856 if( N > MIN_FIELD_BLOCK_SIZE )
1857 {
1858 gg_call(VOID,
1859 "__gg__resize_treeplet",
1860 build_int_cst_type(INT, ngroup),
1861 build_int_cst_type(SIZE_T, N),
1862 NULL_TREE);
1863
1864 gg_call(VOID,
1865 "__gg__resize_int_p",
1866 gg_get_address_of(var_decl_fourplet_flags_size),
1867 gg_get_address_of(var_decl_fourplet_flags),
1868 build_int_cst_type(SIZE_T, N),
1869 NULL_TREE);
1870 }
1871
1872 for(size_t i=0; i<N; i++)
1873 {
1874 gg_assign(gg_array_value(var_decl_treeplet_1f, i),
1875 gg_get_address_of(refers[i].field->var_decl_node));
1876 gg_assign(gg_array_value(var_decl_treeplet_1o, i),
1877 refer_offset(refers[i], &flag_bits));
1878 gg_assign(gg_array_value(var_decl_treeplet_1s, i),
1879 refer_size_source(refers[i]));
1880 gg_assign(gg_array_value(var_decl_fourplet_flags, i),
1881 build_int_cst_type(INT, flag_bits));
1882 }
1883 }
1884 else
1885 {
1886 abort();
1887 }
1888 }
1889
1890 tree
1891 build_array_of_size_t( size_t N,
1892 const size_t *values)
1893 {
1894 // We create and populate an array of size_t values
1895
1896 // This only works because it is used in but one spot. If this routine is
1897 // called twice, be careful about how the first one is used. It's a static
1898 // variable, you see.
1899 static tree values_p = gg_define_variable(SIZE_T_P, "..baost_values_p", vs_file_static);
1900 if( N )
1901 {
1902 gg_assign( values_p,
1903 gg_cast(build_pointer_type(SIZE_T),
1904 gg_malloc(N*sizeof(SIZE_T))));
1905
1906 for(size_t i=0; i<N; i++)
1907 {
1908 gg_assign( gg_array_value(values_p, i),
1909 build_int_cst_type(SIZE_T, values[i]));
1910 }
1911 }
1912 else
1913 {
1914 gg_assign( values_p,
1915 gg_cast(build_pointer_type(SIZE_T), null_pointer_node ));
1916 }
1917 return values_p;
1918 }
1919
1920 void
1921 parser_display_internal_field(tree file_descriptor,
1922 cbl_field_t *field,
1923 bool advance)
1924 {
1925 cbl_refer_t wrapper = {};
1926 wrapper.field = field;
1927 parser_display_internal(file_descriptor, wrapper, advance);
1928 }
1929
1930 char *
1931 get_literal_string(cbl_field_t *field)
1932 {
1933 assert(field->type == FldLiteralA);
1934 size_t buffer_length = field->data.capacity+1;
1935 char *buffer = static_cast<char *>(xcalloc(1, buffer_length));
1936
1937 for(size_t i=0; i<field->data.capacity; i++)
1938 {
1939 buffer[i] = ascii_to_internal(field->data.initial[i]);
1940 }
1941
1942 return buffer;
1943 }
1944
1945 bool
1946 refer_is_clean(const cbl_refer_t &refer)
1947 {
1948 if( !refer.field || refer.field->type == FldLiteralN )
1949 {
1950 // It is routine for a refer to have no field. It happens when the parser
1951 // passes us a refer for an optional parameter that has been omitted, for
1952 // example.
1953
1954 // It is also the case that a FldLiteralN will never have suscripts, or the
1955 // like.
1956 return true;
1957 }
1958
1959 return !refer.all
1960 && !refer.addr_of
1961 && !refer.nsubscript()
1962 && !refer.refmod.from
1963 && !refer.refmod.len
1964 && !refer_has_depends(refer, refer_source)
1965 ;
1966 }
1967
1968
1969 /* This routine returns the length portion of a refmod(start:length) reference.
1970 It extracts both the start and the length so that it can add them together
1971 to make sure that result falls within refer.capacity.
1972
1973 This routine shouldn't be called unless there is refmod involved.
1974 */
1975 static
1976 tree // size_t
1977 refer_refmod_length(const cbl_refer_t &refer)
1978 {
1979 Analyze();
1980 REFER("refstart and reflen");
1981 static tree refstart = gg_define_variable(LONG, "..rrl_refstart", vs_file_static);
1982 static tree reflen = gg_define_variable(LONG, "..rrl_reflen", vs_file_static);
1983
1984 get_and_check_refstart_and_reflen( refstart, reflen, refer);
1985
1986 // Arrive here with a valid value for reflen:
1987
1988 return gg_cast(SIZE_T, reflen);
1989 }
1990
1991 static
1992 tree // size_t
1993 refer_fill_depends(const cbl_refer_t &refer)
1994 {
1995 REFER("");
1996 // This returns a positive number which is the amount a depends-limited
1997 // capacity needs to be reduced.
1998 Analyze();
1999 cbl_field_t *odo = symbol_find_odo(refer.field);
2000
2001 static tree value64 = gg_define_variable(LONG, "..rfd_value64", vs_file_static);
2002
2003 get_depending_on_value(value64, refer);
2004
2005 // value64 is >= zero and < bounds.upper
2006
2007 // We multiply the ODO value by the size of the data capacity to get the
2008 // shortened length:
2009
2010 tree mult_expr = gg_multiply( build_int_cst_type(TREE_TYPE(value64), odo->data.capacity),
2011 value64 );
2012
2013 // And we add that to the distance from the requested variable to the odo
2014 // variable to get the modified length:
2015 tree add_expr = gg_add(mult_expr, build_int_cst_type(SIZE_T, odo->offset - refer.field->offset));
2016 return add_expr;
2017 }
2018
2019 tree // size_t
2020 refer_offset(const cbl_refer_t &refer,
2021 int *pflags)
2022 {
2023 // This routine calculates the effect of a refer offset on the
2024 // refer.field->data location. When there are subscripts, the data location
2025 // gets augmented by the (subscript-1)*element_size calculation. And when
2026 // there is a refmod, the data location additionally gets augmented by
2027 // (refmod.from-1)
2028
2029 if( !refer.field )
2030 {
2031 // It's common for the field to be missing. It generally means that an
2032 // optional parameter wasn't supplied.
2033 return size_t_zero_node;
2034 }
2035
2036 if( refer.field->type == FldLiteralN || refer.field->type == FldLiteralA )
2037 {
2038 // We know that literals have no offset
2039 return size_t_zero_node;
2040 }
2041
2042 tree retval = get_data_offset(refer, pflags);
2043 return retval;
2044 }
2045
2046 static
2047 tree // size_t
2048 refer_size(const cbl_refer_t &refer, refer_type_t refer_type)
2049 {
2050 Analyze();
2051 static tree retval = gg_define_variable(SIZE_T, "..rs_retval", vs_file_static);
2052
2053 if( !refer.field )
2054 {
2055 return size_t_zero_node;
2056 }
2057
2058 if( refer_is_clean(refer) )
2059 {
2060 return get_any_capacity(refer.field);
2061 }
2062
2063 // Step the first: Get the actual full length:
2064
2065 if( refer_has_depends(refer, refer_type) )
2066 {
2067 // Because there is a depends, we might have to change the length:
2068 gg_assign(retval, refer_fill_depends(refer));
2069 }
2070 else
2071 {
2072 gg_assign(retval, get_any_capacity(refer.field));
2073 }
2074
2075 if( refer.refmod.from || refer.refmod.len )
2076 {
2077 tree refmod = refer_refmod_length(refer);
2078 // retval is the ODO based total length.
2079 // refmod is the length resulting from refmod(from:len)
2080 // We have to reduce retval by the effect of refmod:
2081 tree diff = gg_subtract(get_any_capacity(refer.field),
2082 refmod);
2083 gg_assign(retval, gg_subtract(retval, diff));
2084 }
2085 return retval;
2086 }
2087
2088 tree // size_t
2089 refer_size_dest(const cbl_refer_t &refer)
2090 {
2091 return refer_size(refer, refer_dest);
2092 }
2093
2094 tree // size_t
2095 refer_size_source(const cbl_refer_t &refer)
2096 {
2097 /* There are oddities involved with refer_size_source and refer_size_dest.
2098 See the comments in refer_has_depends for some explanation. There are
2099 other considerations, as well. For example, consider a move, where you
2100 have both a source and a dest. Given that refer_size returns a static,
2101 there are ways that the source and dest can trip over each other.
2102
2103 The logic here avoids all known cases where they might trip over each
2104 other. But there conceivably might be others,.
2105
2106 You have been warned.
2107
2108 */
2109
2110 // This test has to be here, otherwise there are failures in regression
2111 // testing.
2112 if( !refer.field )
2113 {
2114 return size_t_zero_node;
2115 }
2116
2117 // This test has to be here, otherwise there are failures in regression
2118 // testing.
2119 if( refer_is_clean(refer) )
2120 {
2121 return get_any_capacity(refer.field);
2122 }
2123
2124 // This assignment has to be here. Simply returning refer_size() results
2125 // in regression testing errors.
2126 static tree retval = gg_define_variable(SIZE_T, "..rss_retval", vs_file_static);
2127 gg_assign(retval, refer_size(refer, refer_source));
2128 return retval;
2129 }
2130
2131 tree
2132 qualified_data_location(const cbl_refer_t &refer)
2133 {
2134 return gg_add(member(refer.field->var_decl_node, "data"),
2135 refer_offset(refer));
2136 }
2137
2138 uint64_t
2139 get_time_nanoseconds()
2140 {
2141 // This code was unabashedly stolen from gcc/timevar.cc.
2142 // It returns the Unix epoch with nine decimal places.
2143
2144 uint64_t retval = 0;
2145
2146 #ifdef HAVE_CLOCK_GETTIME
2147 struct timespec ts;
2148 clock_gettime (CLOCK_REALTIME, &ts);
2149 retval = ts.tv_sec * 1000000000 + ts.tv_nsec;
2150 return retval;
2151 #endif
2152 #ifdef HAVE_GETTIMEOFDAY
2153 struct timeval tv;
2154 gettimeofday (&tv, NULL);
2155 retval = tv.tv_sec * 1000000000 + tv.tv_usec * 1000;
2156 return retval;
2157 #endif
2158 return retval;
2159 }