Debian Codegraph @

gcc/cobol/genapi.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
31 #include "cobol-system.h"
32
33 #include "coretypes.h"
34 #include "tree.h"
35 #include "tree-iterator.h"
36 #include "stringpool.h"
37 #include "diagnostic-core.h"
38 #include "target.h"
39
40 #include "../../libgcobol/ec.h"
41 #include "../../libgcobol/common-defs.h"
42 #include "util.h"
43 #include "cbldiag.h"
44 #include "symbols.h"
45 #include "gengen.h"
46 #include "inspect.h"
47 #include "../../libgcobol/io.h"
48 #include "genapi.h"
49 #include "genutil.h"
50 #include "genmath.h"
51 #include "structs.h"
52 #include "../../libgcobol/gcobolio.h"
53 #include "../../libgcobol/charmaps.h"
54 #include "../../libgcobol/valconv.h"
55 #include "show_parse.h"
56 #include "fold-const.h"
57 #include "realmpfr.h"
58
59 extern int yylineno;
60
61 #define TSI_BACK (tsi_last(current_function->statement_list_stack.back()))
62
63 extern char *cobol_name_mangler(const char *cobol_name);
64 static tree gg_attribute_bit_get( struct cbl_field_t *var,
65 cbl_field_attr_t bits);
66
67 static tree label_list_out_goto;
68 static tree label_list_out_label;
69 static tree label_list_back_goto;
70 static tree label_list_back_label;
71
72 static void hijack_for_development(const char *funcname);
73
74 static size_t sv_data_name_counter = 1;
75 static int call_counter = 1;
76 static int pseudo_label = 1;
77
78 static bool suppress_cobol_entry_point = false;
79 static char ach_cobol_entry_point[256] = "";
80
81 bool bSHOW_PARSE = getenv("GCOBOL_SHOW");
82 bool show_parse_sol = true;
83 int show_parse_indent = 0;
84
85 static bool sv_is_i_o = false;
86
87 #define DEFAULT_LINE_NUMBER 2
88
89 #ifdef LINE_TICK
90 /* This code is used from time to time when sorting out why compilation
91 takes more time than expected */
92 static void
93 line_tick()
94 {
95 using namespace std::chrono;
96 static high_resolution_clock::time_point t1 = high_resolution_clock::now();
97 static high_resolution_clock::time_point t2;
98 int line_now = CURRENT_LINE_NUMBER;
99 static int line = 0;
100 if( (line_now / 10000) != (line / 10000) )
101 {
102 line = line_now;
103 t2 = high_resolution_clock::now();
104 duration<double> time_span = duration_cast<duration<double>>(t2 - t1);
105 fprintf(stderr, "%6d %6.1lf\n", line, time_span.count());
106 }
107 }
108 #else
109 #define line_tick()
110 #endif
111
112 typedef struct TREEPLET
113 {
114 tree pfield;
115 tree offset;
116 tree length;
117 } TREEPLET;
118
119 static
120 void
121 treeplet_fill_source(TREEPLET &treeplet, const cbl_refer_t &refer)
122 {
123 treeplet.pfield = gg_get_address_of(refer.field->var_decl_node);
124 treeplet.offset = refer_offset(refer);
125 treeplet.length = refer_size_source(refer);
126 }
127
128 tree file_static_variable(tree type, const char *v)
129 {
130 // This routine returns a reference to an already-defined file_static
131 // variable. You need to know the type that was used for the definition.
132 return gg_declare_variable(type, v, NULL, vs_file_static);
133 }
134
135 static void move_helper(tree size_error, // INT
136 cbl_refer_t destref,
137 cbl_refer_t sourceref,
138 TREEPLET &tsource,
139 cbl_round_t rounded,
140 bool check_for_error,
141 bool restore_on_error = false
142 );
143
144 // set using -f-trace-debug, defined in lang.opt
145 int f_trace_debug;
146
147 // When doing WRITE statements, the IBM Language Reference and the
148 // ISO/IEC_2014 standard specify that when the ADVANCING clause is omitted, the
149 // default isAFTER ADVANCING 1 LINE.
150 //
151 // MicroFocus and GnuCOBOL state that the default is BEFORE ADVANCING 1 LINE
152 //
153 // During initial compiler development, we used Michael Coughlin's "Beginning
154 // COBOL For Programmers" textbook for source code examples, and it was clear
155 // from at least one sample program that his compiler used the Microfocus
156 // convention. For ease of development, we took on that same convention, but
157 // we provide here for a switch that changes that behavior:
158
159 static bool auto_advance_is_AFTER_advancing = 0;
160
161 /* This is a little complicated. In order to keep things general, we are
162 assuming that any function we call will be returning a 64-bit value. In
163 places where we know that not to be true, we'll have to do appropriate
164 casts. For example, main() returns an INT, as do functions that
165 return the default RETURN-CODE will have */
166
167 #define COBOL_FUNCTION_RETURN_TYPE SSIZE_T
168
169 #define MAX_AFTERS 8
170
171 // These variables contol a little state machine. When a simple -main is in
172 // effect, the first program in the module becomes the target of a main()
173 // that we synthesize function. When -main=module:progid is in effect, we
174 // create a main() that calls progid. When active, progid is kept in
175 // the map main_strings.
176 static std::unordered_map<std::string, std::string> main_strings;
177 static bool this_module_has_main = false; // sticky switch for the module
178 static bool next_program_is_main = false; // transient switch for the module
179 static char *main_entry_point = NULL;
180
181 static bool static_call = true;
182 bool use_static_call( bool yn ) { return static_call = yn; }
183 static bool use_static_call() { return static_call; }
184
185 // This global variable can be set upstream, like from a compiler
186 // command line switch. "1" for stdout, "2" for stderr, or "filename"
187
188 const char *gv_trace_switch = NULL;
189
190 // The environment variable wins over the command line
191 char const *bTRACE1 = NULL;
192 tree trace_handle;
193 tree trace_indent;
194
195 // This variable is set to true when the output cursor is known to be at the
196 // start-of-line.
197 bool cursor_at_sol = true;
198
199 static void
200 trace1_init()
201 {
202 static bool first_time = true;
203 if( first_time )
204 {
205 first_time = false;
206 trace_handle = gg_define_variable(INT, "trace_handle", vs_static);
207 trace_indent = gg_define_variable(INT, "trace_indent", vs_static);
208
209 bTRACE1 = getenv("GCOBOL_TRACE") ? getenv("GCOBOL_TRACE") :gv_trace_switch;
210
211 if( bTRACE1 && strcmp(bTRACE1, "0") != 0 )
212 {
213 if( strcmp(bTRACE1, "1") == 0 )
214 {
215 gg_assign(trace_handle , integer_one_node);
216 }
217 else if( strcmp(bTRACE1, "2") == 0 )
218 {
219 gg_assign(trace_handle , integer_two_node);
220 }
221 else
222 {
223 gg_assign(trace_handle ,
224 gg_open(gg_string_literal(bTRACE1),
225 build_int_cst_type(INT, O_CREAT|O_WRONLY|O_TRUNC)));
226 }
227 }
228 else
229 {
230 // In case bTRACE1 pointed to an empty string
231 bTRACE1 = NULL;
232 }
233 }
234 }
235
236 static
237 void
238 insert_nop(int n)
239 {
240 gg_assign(var_decl_nop, build_int_cst_type(INT, n));
241 }
242
243 static void
244 create_cblc_string_variable(const char *var_name, const char *var_contents)
245 {
246 // This is a way of having the compiler communicate with GDB. I create a
247 // global const char[] string with a known name so that GDB can look for that
248 // variable and pick up its contents.
249
250 // This probably should be in the .debug_info section, but for the moment I
251 // don't know how to do that, but I do know how to do this:
252
253 tree array_of_characters = build_array_type_nelts(CHAR, strlen(var_contents)+1);
254 TYPE_NAME(array_of_characters) = get_identifier("cblc_string");
255 tree constr = build_string(strlen(var_contents)+1, var_contents);
256 TREE_TYPE(constr) = array_of_characters;
257 TREE_STATIC(constr) = 1;
258 TREE_CONSTANT(constr) = 1;
259 tree entry_point = gg_declare_variable(array_of_characters,
260 var_name,
261 constr,
262 vs_external);
263 gg_define_from_declaration(entry_point);
264 }
265
266 static void
267 build_main_that_calls_something(const char *something)
268 {
269 // This routine generates main(), which has as its body a call to "something".
270 // which is a call to a simple `extern int something(void)` routine.
271
272 SHOW_PARSE
273 {
274 SHOW_PARSE_HEADER
275 SHOW_PARSE_TEXT(" main will call ")
276 SHOW_PARSE_TEXT(something)
277 SHOW_PARSE_END
278 }
279
280 tree function_decl = gg_define_function( INT,
281 "main",
282 "main",
283 INT, "argc",
284 build_pointer_type(CHAR_P), "argv",
285 NULL_TREE);
286
287 // Modify the default settings for main(), as empirically determined from
288 // examining C/C+_+ compilations. (See the comment for gg_build_fn_decl()).
289 TREE_ADDRESSABLE(function_decl) = 0;
290 TREE_USED(function_decl) = 0;
291 TREE_NOTHROW(function_decl) = 0;
292 TREE_STATIC(function_decl) = 1;
293 DECL_EXTERNAL (function_decl) = 0;
294 TREE_PUBLIC (function_decl) = 1;
295 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT(function_decl) = 1;
296
297 // Pick up pointers to the input parameters:
298 // First is the INT which is the number of argv[] entries
299 tree argc = DECL_ARGUMENTS(current_function->function_decl);
300 // Second is the char **argv
301 tree argv = TREE_CHAIN(argc); // overall source length
302
303 gg_call( VOID,
304 "__gg__stash_argc_argv",
305 argc,
306 argv,
307 NULL_TREE);
308
309 // Call the top-level COBOL function. We know it has to return an INT,
310 // so we need to cast it from the SIZE_T that all COBOL are assumed
311 // to return:
312
313 TRACE1
314 {
315 TRACE1_HEADER
316 TRACE1_TEXT_ABC("main calls \"", something, "\"")
317 TRACE1_END
318 }
319
320 // Let MODULE-NAME know that we were launched by a generated -main program
321 gg_call(VOID,
322 "__gg__module_name_push",
323 gg_string_literal("Mmain"),
324 NULL_TREE);
325
326 char *psz = cobol_name_mangler(something);
327 gg_assign(var_decl_main_called, integer_one_node);
328 gg_return(gg_cast(INT, gg_call_expr( COBOL_FUNCTION_RETURN_TYPE,
329 psz,
330 argc,
331 argv,
332 NULL_TREE)));
333 free(psz);
334 gg_finalize_function();
335 }
336
337 static std::unordered_map<std::string, size_t>gotos_labels;
338 #define LABEL_COUNT_OFFSET 100
339
340 static
341 tree
342 get_field_p(size_t index)
343 {
344 if(index)
345 {
346 cbl_field_t *field = cbl_field_of(symbol_at(index));
347
348 if( !field->var_decl_node )
349 {
350 dbgmsg("%s (type: %s) improperly has a NULL var_decl_node",
351 field->name,
352 cbl_field_type_str(field->type));
353 cbl_internal_error(
354 "Probable cause: it was referenced without being defined.");
355 }
356
357 return gg_get_address_of(field->var_decl_node);
358 }
359 else
360 {
361 return gg_cast(cblc_field_p_type_node, null_pointer_node);
362 }
363 }
364
365 static
366 char *
367 level_88_helper(size_t parent_capacity,
368 const cbl_domain_elem_t &elem,
369 size_t &returned_size)
370 {
371 // We return a MALLOCed return value, which the caller must free.
372 char *retval = static_cast<char *>(xmalloc(parent_capacity + 64));
373 gcc_assert(retval);
374 char *builder = static_cast<char *>(xmalloc(parent_capacity + 64));
375 gcc_assert(builder);
376
377 size_t nbuild = 0;
378
379 cbl_figconst_t figconst = cbl_figconst_of( elem.name());
380 if( figconst )
381 {
382 nbuild = 1;
383 strcpy(retval, "1Fx");
384 switch(figconst)
385 {
386 case normal_value_e :
387 // This really should never happend
388 abort();
389 break;
390 case low_value_e :
391 retval[2] = 'L';
392 break;
393 case zero_value_e :
394 retval[2] = 'Z';
395 break;
396 case space_value_e :
397 retval[2] = 'S';
398 break;
399 case quote_value_e :
400 retval[2] = 'Q';
401 break;
402 case high_value_e :
403 retval[2] = 'H';
404 break;
405 case null_value_e:
406 retval[2] = '\0';
407 break;
408 }
409 returned_size = 3;
410 }
411 else
412 {
413 // We are working with an ordinary string.
414
415 // Pick up the string
416 size_t first_name_length = elem.size();
417 char *first_name = static_cast<char *>(xmalloc(first_name_length + 1));
418 gcc_assert(first_name);
419 memcpy(first_name, elem.name(), first_name_length);
420 first_name[first_name_length] = '\0';
421
422 // Convert it to EBCDIC, when necessary; leave it alone when not necessary.
423 for(size_t i=0; i<first_name_length; i++)
424 {
425 first_name[i] = ascii_to_internal(first_name[i]);
426 }
427
428 if( parent_capacity == 0 )
429 {
430 // Special case: parent_capacity is zero when this routine has been
431 // called as part of a debugging trace.
432 if( elem.all )
433 {
434 strcpy(builder+nbuild, "ALL ");
435 nbuild += 4;
436 }
437 memcpy(builder+nbuild, first_name, first_name_length);
438 nbuild += first_name_length;
439 }
440 else
441 {
442 if( elem.all )
443 {
444 while(nbuild < parent_capacity )
445 {
446 builder[nbuild] = first_name[nbuild % first_name_length];
447 nbuild += 1;
448 }
449 }
450 else
451 {
452 memcpy(builder+nbuild, first_name, first_name_length);
453 nbuild += first_name_length;
454 }
455 }
456 returned_size = sprintf(retval, HOST_SIZE_T_PRINT_DEC "A",
457 (fmt_size_t)nbuild);
458 memcpy(retval + returned_size, builder, nbuild);
459 returned_size += nbuild;
460 free(first_name);
461 free(builder);
462 }
463 return retval;
464 }
465
466 static char *
467 get_level_88_domain(size_t parent_capacity, cbl_field_t *var, size_t &returned_size)
468 {
469 if( var->type != FldClass || var->level != 88 )
470 {
471 returned_size = 0;
472 return NULL;
473 }
474
475 // Entering here means we know that this is FldClass of level 88
476
477 // We convert the incoming information at var->data.domains to a single
478 // stream of bytes. We return a malloced pointer to that stream; returned
479 // size is the size of the stream.
480
481 // The nature of an 88 is that each element is a pair
482
483 // The following pairs are zero-terminated strings. It thus
484 // follows that the strings cannot contain '\0' characters.
485
486 // Each element of the pair is converted to a stream:
487 // For strings of bytes:
488 // ddd A <ddd bytes>
489 // For figurative constants:
490 // 1Fx, where x is in [LZSQH], for LOW-VALUE ZERO SPACE QUOTE HIGH-VALUE
491
492 // Numerics are converted to strings, and handled as above
493
494 size_t retval_capacity = 64;
495 char *retval = static_cast<char *>(xmalloc(retval_capacity));
496 size_t output_index = 0;
497
498 // Loop through the provided domains:
499 returned_size = 0;
500 const struct cbl_domain_t *domain = var->data.domain_of();
501 while( domain->first.name() )
502 {
503 // We have another pair to process
504 size_t stream_len;
505 char *stream;
506
507 // Do the first element of the domain
508 stream = level_88_helper(parent_capacity, domain->first, stream_len);
509 if( output_index + stream_len > retval_capacity )
510 {
511 retval_capacity *= 2;
512 retval = static_cast<char *>(xrealloc(retval, retval_capacity));
513 }
514 gcc_assert(retval);
515 memcpy(retval + output_index, stream, stream_len);
516 output_index += stream_len;
517 returned_size += stream_len;
518 free(stream);
519
520 // Do the second element of the domain
521 stream = level_88_helper(parent_capacity, domain->last, stream_len);
522 if( output_index + stream_len > retval_capacity )
523 {
524 retval_capacity *= 2;
525 retval = static_cast<char *>(xrealloc(retval, retval_capacity));
526 }
527 gcc_assert(retval);
528 memcpy(retval + output_index, stream, stream_len);
529 output_index += stream_len;
530 returned_size += stream_len;
531 free(stream);
532 domain += 1;
533 }
534 retval[returned_size++] = '\0';
535 return retval;
536 }
537
538 static
539 char *
540 get_class_condition_string(cbl_field_t *var)
541 {
542 // We know at this point that var is FldClass
543 // The LEVEL is not 88, so this is a CLASS SPECIAL-NAME
544
545 const struct cbl_domain_t *domain = var->data.domain_of();
546
547 /* There are five possibilities we need to deal with.
548
549 66
550 66 THROUGH 91
551 91 THROUGH 66 // This is the same as 66 THROUGH 91
552 "A"
553 "A" THROUGH "Z
554 "Z" THROUGH "A" // This is the same as "A" THROUGH "Z"
555 "ABCJ12" // This is the same as "A" "B" "C" ...
556
557 Expressly presented numbers are the ordinal positions in the run-time
558 character set. So, an ASCII "A" would be given as 66, which is one
559 greater than 65, which is the ASCII codepoint for "A". An EBCDIC "A"
560 would be presented as 194, which is one greater than 193, which is the
561 decimal representation of an EBCDIC "A", whose hex code is 0xC2.
562
563 We need to account for EBCDIC as well as ASCII. In EBCDIC,
564 "A" THROUGH "Z" doesn't mean what it looks like it means, because EBCIDC
565 encoding has gaps between I and J, and between R and S. That isn't true
566 in ASCII. We don't want to deal with these issues at compile time, so we
567 are encoding numeric ordinals with their negated values, while other
568 characters are given as the numeric forms of their ASCII encoding.
569 Conversion to EBCDIC occurs at runtime.
570
571 In support of this strategy, character strings like "ABCD" are broken up
572 into "A" "B" "C" "D" and converted to their hexadecimal representations.
573 */
574
575 char ach[8192];
576 memset(ach, 0, sizeof(ach));
577 char *p = ach;
578
579 while( domain->first.is_numeric || domain->first.name() )
580 {
581 // *What* were they smoking back then?
582
583 uint8_t value1;
584 uint8_t value2;
585
586 char achFirstName[256];
587 char achLastName[256];
588
589 size_t first_name_length = domain->first.size()
590 ? domain->first.size()
591 : strlen(domain->first.name());
592 size_t last_name_length = domain->last.size()
593 ? domain->last.size()
594 : strlen(domain->last.name());
595
596 if( domain->first.is_numeric )
597 {
598 if( strlen(ach) > sizeof(ach) - 1000 )
599 {
600 cbl_internal_error("Nice try, but you cannot fire me.");
601 }
602
603 // We are working with unquoted strings that contain the values 1 through
604 // 256:
605 value1 = (uint8_t)atoi(domain->first.name());
606 value2 = (uint8_t)atoi(domain->last.name());
607 if( value2 < value1 )
608 {
609 std::swap(value1, value2);
610 }
611 if( value1 != value2 )
612 {
613 p += sprintf(p, "-%2.2X/-%2.2X ", value1-1, value2-1);
614 }
615 else
616 {
617 p += sprintf(p, "-%2.2X ", value1-1);
618 }
619 }
620 else if( first_name_length == 1 )
621 {
622 // Since the first.name is a single character, we can do this as
623 // a single-character pair.
624
625 uint8_t ch1;
626 uint8_t ch2;
627
628 char *p2;
629 size_t one;
630 p2 = achFirstName;
631 one = 8;
632 raw_to_internal(&p2, &one, domain->last.name(), last_name_length);
633 ch2 = achFirstName[0];
634
635 p2 = achLastName;
636 one = 8;
637 raw_to_internal(&p2, &one, domain->first.name(), first_name_length);
638 ch1 = achLastName[0];
639
640 if( ch1 < ch2 )
641 {
642 value1 = ch1;
643 value2 = ch2;
644 }
645 else
646 {
647 value2 = ch1;
648 value1 = ch2;
649 }
650 if( value1 != value2 )
651 {
652 p += sprintf(p, "%2.2X/%2.2X ", value1, value2);
653 }
654 else
655 {
656 p += sprintf(p, "%2.2X ", value1);
657 }
658 }
659 else
660 {
661 gcc_assert( first_name_length > 1 );
662
663 // We are working with a string larger than 1 character. The COBOL
664 // spec says there can't be a THROUGH, so we ignore the last.name:
665 char *p2;
666 size_t one;
667 p2 = achFirstName;
668 one = 8;
669 raw_to_internal(&p2, &one, domain->last.name(), last_name_length);
670
671 for(size_t i=0; i<first_name_length; i++)
672 {
673 p += sprintf(p, "%2.2X ", (unsigned char)achFirstName[i]);
674 }
675 }
676 domain += 1;
677 }
678
679 // Wipe out the trailing space
680 ach[strlen(ach)-1] = '\0';
681 char *retval = xstrdup(ach);
682
683 return retval;
684 }
685
686 struct program_reference_t {
687 size_t caller;
688 const char *called;
689
690 program_reference_t( size_t caller, const char called[] )
691 : caller(caller), called(xstrdup(called))
692 {}
693 bool operator==( const program_reference_t& that ) const {
694 return caller == that.caller && 0 == strcasecmp(called, that.called);
695 }
696 bool operator<( const program_reference_t& that ) const {
697 if( caller == that.caller ) return 0 < strcasecmp(called, that.called);
698 return caller < that.caller;
699 }
700 };
701
702 struct called_tree_t {
703 tree node;
704 cbl_call_convention_t convention;
705
706 called_tree_t( tree node,
707 cbl_call_convention_t convention )
708 : node(node), convention(convention)
709 {}
710 bool operator==( const called_tree_t& that ) const {
711 return node == that.node && convention == that.convention;
712 }
713
714 class match_tree { // match node regardless of convention
715 tree node;
716
717 public:
718 explicit match_tree( tree node ) : node(node) {}
719 bool operator()( const called_tree_t& that ) const {
720 return this->node == that.node;
721 }
722 };
723 };
724
725 static std::map<program_reference_t, std::list<tree> > call_targets;
726 static std::map<tree, cbl_call_convention_t> called_targets;
727
728 static
729 void
730 set_call_convention(tree function_decl, cbl_call_convention_t convention)
731 {
732 called_targets[function_decl] = convention;
733 }
734
735 static
736 void
737 parser_call_target( const char *name, tree call_expr )
738 {
739 /* This routine gets called when parser_call() has been invoked with a
740 literal target. That target is a COBOL name like "prog_2". However,
741 there is the case when "prog_2" is a forward reference to a contained
742 program nested inside "prog_1". In that case, the actual definition
743 of "prog_2" will end up with a name like "prog_2.62", and eventually
744 the target of the call will have to be modified from "prog_2" to
745 "prog_2.62".
746
747 We save the call expression for this call, and then we update it later,
748 after we know whether or not it was a forward reference to a local
749 function. */
750
751 program_reference_t key(current_program_index(), name);
752 auto& p = call_targets[key];
753 p.push_back(call_expr);
754 }
755
756 /*
757 * Is the node a recorded call target? The language-dependent
758 * function cobol_set_decl_assembler_name will lower-case the name
759 * unless, for a specific call, this function returns
760 * cbl_call_verbatim_e.
761 */
762 cbl_call_convention_t
763 parser_call_target_convention( tree func )
764 {
765 auto p = called_targets.find(func);
766 if( p != called_targets.end() )
767 {
768 // This was found in our list of call targets
769 return p->second;
770 }
771
772 return cbl_call_cobol_e;
773 }
774
775 void
776 parser_call_targets_dump()
777 {
778 dbgmsg( "call targets for #" HOST_SIZE_T_PRINT_UNSIGNED " NOT dumping",
779 (fmt_size_t)current_program_index() );
780 #if 0 // A change to call_targets rendered this routine useless. Until we get
781 // around to repairing it, this code is left for reference.
782 for( const auto& elem : call_targets ) {
783 const auto& k = elem.first;
784 const auto& v = elem.second;
785 fprintf(stderr, "\t#%-3" GCC_PRISZ "u %s calls %s ",
786 (fmt_size_t)k.caller, cbl_label_of(symbol_at(k.caller))->name,
787 k.called);
788 char ch = '[';
789 for( auto func : v ) {
790 fprintf( stderr, "%c %s", ch, IDENTIFIER_POINTER(DECL_NAME(func)) );
791 ch = ',';
792 }
793 fprintf(stderr, " ]\n");
794 }
795 #endif
796 }
797
798 size_t
799 parser_call_target_update( size_t caller,
800 const char plain_name[],
801 const char mangled_name[] )
802 {
803 auto key = program_reference_t(caller, plain_name);
804 auto p = call_targets.find(key);
805 if( p == call_targets.end() ) return 0;
806
807 for( auto call_expr : p->second )
808 {
809 tree fndecl_type = build_varargs_function_type_array( COBOL_FUNCTION_RETURN_TYPE,
810 0, // No parameters yet
811 NULL); // And, hence, no types
812
813 // Fetch the FUNCTION_DECL for that FUNCTION_TYPE
814 tree function_decl = gg_build_fn_decl(mangled_name, fndecl_type);
815 tree function_address = gg_get_address_of(function_decl);
816
817 TREE_OPERAND(call_expr, 1) = function_address;
818 }
819 return p->second.size();
820 }
821
822 static tree
823 function_pointer_from_name(const cbl_refer_t &name,
824 tree function_return_type)
825 {
826 Analyze();
827
828 tree function_type = build_varargs_function_type_array(
829 function_return_type,
830 0,
831 NULL);
832 tree function_pointer_type = build_pointer_type(function_type);
833 tree function_pointer = gg_define_variable(function_pointer_type,
834 "..function_pointer.1",
835 vs_stack);
836 if( name.field->type == FldPointer )
837 {
838 // If the parameter is a pointer, just pick up the value and head for the
839 // exit
840 if( refer_is_clean(name) )
841 {
842 gg_memcpy(gg_get_address_of(function_pointer),
843 member(name.field->var_decl_node, "data"),
844 sizeof_pointer);
845 }
846 else
847 {
848 gg_memcpy(gg_get_address_of(function_pointer),
849 qualified_data_location(name),
850 sizeof_pointer);
851 }
852 return function_pointer;
853 }
854 else if( use_static_call() && is_literal(name.field) )
855 {
856 tree fndecl_type = build_varargs_function_type_array( function_return_type,
857 0, // No parameters yet
858 NULL); // And, hence, no types
859
860 // Fetch the FUNCTION_DECL for that FUNCTION_TYPE
861 tree function_decl = gg_build_fn_decl(name.field->data.initial,
862 fndecl_type);
863 // Take the address of the function decl:
864 tree address_of_function = gg_get_address_of(function_decl);
865 gg_assign(function_pointer, address_of_function);
866 }
867 else
868 {
869 // We are not using static calls.
870 if( name.field->type == FldLiteralA )
871 {
872 gg_assign(function_pointer,
873 gg_cast(build_pointer_type(function_type),
874 gg_call_expr( VOID_P,
875 "__gg__function_handle_from_literal",
876 build_int_cst_type(INT,
877 current_function->our_symbol_table_index),
878 gg_string_literal(name.field->data.initial),
879 NULL_TREE)));
880 }
881 else
882 {
883 gg_assign(function_pointer,
884 gg_cast(build_pointer_type(function_type),
885 gg_call_expr( VOID_P,
886 "__gg__function_handle_from_name",
887 build_int_cst_type(INT,
888 current_function->our_symbol_table_index),
889 gg_get_address_of(name.field->var_decl_node),
890 refer_offset(name),
891 refer_size_source( name),
892 NULL_TREE)));
893 }
894 }
895
896 return function_pointer;
897 }
898
899 void
900 parser_initialize_programs( size_t nprogs,
901 const struct cbl_refer_t *progs)
902 {
903 Analyze();
904 SHOW_PARSE
905 {
906 SHOW_PARSE_HEADER
907 SHOW_PARSE_TEXT(" ")
908 for( size_t i=0; i<nprogs; i++)
909 {
910 if( i > 0 )
911 {
912 SHOW_PARSE_INDENT
913 }
914 if( progs[i].field->type == FldLiteralA )
915 {
916 SHOW_PARSE_TEXT("\"")
917 SHOW_PARSE_TEXT(progs[i].field->data.initial)
918 SHOW_PARSE_TEXT("\"")
919 }
920 else
921 {
922 SHOW_PARSE_TEXT("")
923 SHOW_PARSE_TEXT(progs[i].field->name)
924 }
925 }
926 SHOW_PARSE_END
927 }
928
929 for( size_t i=0; i<nprogs; i++ )
930 {
931 tree function_pointer = function_pointer_from_name( progs[i],
932 COBOL_FUNCTION_RETURN_TYPE);
933 gg_call(VOID,
934 "__gg__to_be_canceled",
935 gg_cast(SIZE_T, function_pointer),
936 NULL_TREE);
937 }
938 }
939
940 static
941 tree
942 array_of_long_long(const char *name, const std::vector<uint64_t>& vals)
943 {
944 // We need to create a file-static static array of 64-bit integers:
945 tree array_of_ulonglong_type = build_array_type_nelts(ULONGLONG, vals.size()+1);
946 tree array_of_ulonglong = gg_define_variable( array_of_ulonglong_type,
947 name,
948 vs_file_static);
949 // We have the array. Now we need to build the constructor for it
950 tree constr = make_node(CONSTRUCTOR);
951 TREE_TYPE(constr) = array_of_ulonglong_type;
952 TREE_STATIC(constr) = 1;
953 TREE_CONSTANT(constr) = 1;
954
955 // The first element of the array contains the number of elements to follow
956 CONSTRUCTOR_APPEND_ELT( CONSTRUCTOR_ELTS(constr),
957 build_int_cst_type(SIZE_T, 0),
958 build_int_cst_type(ULONGLONG, vals.size()) );
959 for(size_t i=0; i<vals.size(); i++)
960 {
961 CONSTRUCTOR_APPEND_ELT( CONSTRUCTOR_ELTS(constr),
962 build_int_cst_type(SIZE_T, i+1),
963 build_int_cst_type(ULONGLONG, vals[i]) );
964 }
965 DECL_INITIAL(array_of_ulonglong) = constr;
966 return array_of_ulonglong;
967 }
968
969 /*
970 * As ECs are enabled and disabled with >>TURN, the compiler updates its list
971 * of enabled ECs (and any files they apply to). It encodes this list as an
972 * array of integers. parser_compile_ecs converts that array as a static
973 * compile-time vector, which it returns to the compiler.
974 *
975 * Before each statement, the compiler determines what possible EC handling the
976 * program can do. If there's an overlap between potential ECs and
977 * Declaratives, it passes the current pair of static arrays to
978 * parser_statement_begin(), which installs them, for that statement, in the
979 * library.
980 *
981 * After each statement, to effect EC handling, the statement epilog calls uses
982 * parser_match_exception to invoke __gg_match_exception(), which returns the
983 * symbol table index of the matched Declarative, if any. That "ladder"
984 * Performs the matched declarative, and execution continues with the next
985 * statement.
986 */
987 tree
988 parser_compile_ecs( const std::vector<uint64_t>& ecs )
989 {
990 if( ecs.empty() )
991 {
992 SHOW_IF_PARSE(nullptr)
993 {
994 SHOW_PARSE_HEADER
995 SHOW_PARSE_TEXT("ecs is empty");
996 SHOW_PARSE_END
997 }
998 return NULL_TREE;
999 }
1000
1001 char ach[64];
1002 static int counter = 1;
1003 sprintf(ach, "_ecs_table_%d", counter++);
1004 tree retval = array_of_long_long(ach, ecs);
1005 SHOW_IF_PARSE(nullptr)
1006 {
1007 SHOW_PARSE_HEADER
1008 snprintf(ach, sizeof(ach), " Size is %lu; retval is %p",
1009 gb4(ecs.size()), as_voidp(retval));
1010 SHOW_PARSE_TEXT(ach)
1011 SHOW_PARSE_END
1012 }
1013 TRACE1
1014 {
1015 TRACE1_HEADER
1016 snprintf(ach, sizeof(ach), " Size is %lu; retval is %p",
1017 gb4(ecs.size()), as_voidp(retval));
1018 TRACE1_TEXT_ABC("", ach, "");
1019 TRACE1_END
1020 }
1021 return retval;
1022 }
1023
1024 /*
1025 * At the beginning of Procedure Division, we may encounter DECLARATIVES
1026 * SECTION. If so, the compiler composes a list of zero or more Declaratives
1027 * as cbl_declarative_t, representing the USE statement of each
1028 * Declarative. These are encoded as an array of integers, which are returned
1029 * to the compiler for use by parser_statement_begin(). Although the list of
1030 * declaratives never changes for a program, CALL may change which program is
1031 * invoked, and thus the set of active Declaratives. By passing them for each
1032 * statement, code generation is relieved of referring to global variable.
1033 */
1034 tree
1035 parser_compile_dcls( const std::vector<uint64_t>& dcls )
1036 {
1037 if( dcls.empty() )
1038 {
1039 SHOW_IF_PARSE(nullptr)
1040 {
1041 SHOW_PARSE_HEADER
1042 SHOW_PARSE_TEXT("dcls is empty");
1043 SHOW_PARSE_END
1044 }
1045 return NULL_TREE;
1046 }
1047
1048 char ach[64];
1049 static int counter = 1;
1050 sprintf(ach, "_dcls_table_%d", counter++);
1051 tree retval = array_of_long_long(ach, dcls);
1052 SHOW_IF_PARSE(nullptr)
1053 {
1054 SHOW_PARSE_HEADER
1055 snprintf(ach, sizeof(ach), " Size is %lu; retval is %p",
1056 gb4(dcls.size()), as_voidp(retval));
1057 SHOW_PARSE_TEXT(ach);
1058 SHOW_PARSE_END
1059 }
1060 TRACE1
1061 {
1062 TRACE1_HEADER
1063 snprintf(ach, sizeof(ach), " Size is %lu; retval is %p",
1064 gb4(dcls.size()), as_voidp(retval));
1065 TRACE1_TEXT_ABC("", ach, "");
1066 TRACE1_END
1067 }
1068 return retval;
1069 }
1070
1071 static void
1072 store_location_stuff(const cbl_name_t statement_name)
1073 {
1074 if( exception_location_active && !current_declarative_section_name() )
1075 {
1076 // We need to establish some stuff for EXCEPTION- function processing
1077
1078 gg_assign(var_decl_exception_program_id,
1079 gg_string_literal(current_function->our_unmangled_name));
1080
1081 if( strstr(current_function->current_section->label->name, "_implicit")
1082 != current_function->current_section->label->name )
1083 {
1084 gg_assign(var_decl_exception_section,
1085 gg_string_literal(current_function->current_section->label->name));
1086 }
1087 else
1088 {
1089 gg_assign(var_decl_exception_section,
1090 gg_cast(build_pointer_type(CHAR_P),null_pointer_node));
1091 }
1092
1093 if( strstr(current_function->current_paragraph->label->name, "_implicit")
1094 != current_function->current_paragraph->label->name )
1095 {
1096 gg_assign(var_decl_exception_paragraph,
1097 gg_string_literal(current_function->current_paragraph->label->name));
1098 }
1099 else
1100 {
1101 gg_assign(var_decl_exception_paragraph,
1102 gg_cast(build_pointer_type(CHAR_P), null_pointer_node));
1103 }
1104
1105 gg_assign(var_decl_exception_source_file,
1106 gg_string_literal(current_filename.back().c_str()));
1107 gg_assign(var_decl_exception_line_number, build_int_cst_type(INT,
1108 CURRENT_LINE_NUMBER));
1109 gg_assign(var_decl_exception_statement, gg_string_literal(statement_name));
1110 }
1111 }
1112
1113 static
1114 void
1115 set_exception_environment( tree ecs, tree dcls )
1116 {
1117 gg_call(VOID,
1118 "__gg__set_exception_environment",
1119 ecs ? gg_get_address_of(ecs) : null_pointer_node,
1120 dcls ? gg_get_address_of(dcls) : null_pointer_node,
1121 NULL_TREE);
1122 }
1123
1124 void
1125 parser_statement_begin( const cbl_name_t statement_name,
1126 tree ecs,
1127 tree dcls )
1128 {
1129 SHOW_PARSE
1130 {
1131 SHOW_PARSE_HEADER
1132 char ach[64];
1133 snprintf( ach, sizeof(ach),
1134 " yylineno %d first/last %d/%d",
1135 yylineno,
1136 cobol_location().first_line,
1137 cobol_location().last_line );
1138 SHOW_PARSE_TEXT(ach);
1139 if( true || ecs || dcls )
1140 {
1141 SHOW_PARSE_INDENT
1142 snprintf( ach, sizeof(ach),
1143 "Sending ecs/dcls %p / %p", as_voidp(ecs), as_voidp(dcls));
1144 SHOW_PARSE_TEXT(ach);
1145 }
1146 SHOW_PARSE_END
1147 }
1148 TRACE1
1149 {
1150 TRACE1_HEADER
1151 char ach[64];
1152 snprintf(ach, sizeof(ach), " ecs/dcls %p / %p", as_voidp(ecs), as_voidp(dcls));
1153 TRACE1_TEXT_ABC("", ach, "");
1154 TRACE1_END
1155 }
1156
1157 gcc_assert( gg_trans_unit.function_stack.size() );
1158
1159 // In the cases where enabled_exceptions.size() is non-zero, or when
1160 // there is a possibility of an EC-I-O exception because this is a file
1161 // operation, we need to store the location information and do the exception
1162 // overhead:
1163
1164 static const std::set<std::string> file_ops =
1165 {
1166 "OPEN",
1167 "CLOSE",
1168 "READ",
1169 "WRITE",
1170 "DELETE",
1171 "REWRITE",
1172 "START",
1173 };
1174
1175 // Performance note: By doing exception processing only when necessary
1176 // the execution time of a program doing two-billion simple adds in an inner
1177 // loop dropped from 3.8 seconds to 0.175 seconds.
1178
1179 bool exception_processing = cdf_enabled_exceptions().size() ;
1180
1181 if( !exception_processing )
1182 {
1183 exception_processing = file_ops.find(statement_name) != file_ops.end();
1184 }
1185
1186 // At this point, if any exception is enabled, we store the location stuff.
1187 // Each file I-O routine calls store_location_stuff explicitly, because
1188 // those exceptions can't be defeated.
1189
1190 if( exception_processing )
1191 {
1192 store_location_stuff(statement_name);
1193 }
1194
1195 if( exception_processing )
1196 {
1197 set_exception_environment(ecs, dcls);
1198 }
1199 sv_is_i_o = false;
1200 }
1201
1202 static void
1203 initialize_variable_internal( cbl_refer_t refer,
1204 bool explicitly=false,
1205 bool just_once=false)
1206 {
1207 // fprintf(stderr, "initialize_variable_internal for %s\n", refer.field->name);
1208 // gg_printf("initialize_variable_internal for %s\n",
1209 // gg_string_literal(refer.field->name),
1210 // NULL_TREE);
1211 cbl_field_t *parsed_var = refer.field;
1212 if( !parsed_var )
1213 {
1214 cbl_internal_error("%s should not be null", "parsed_var");
1215 }
1216
1217 if( parsed_var->is_key_name() )
1218 {
1219 // This field is actually a placeholder for a RECORD KEY alias. It didn't
1220 // go through parser_symbol_add(), and so any attempt to initialize it
1221 // results in an error because there is no var_decl_node.
1222 return;
1223 }
1224
1225 if( is_register_field( parsed_var) )
1226 {
1227 return;
1228 }
1229
1230 if( parsed_var->type == FldBlob )
1231 {
1232 return;
1233 }
1234
1235 Analyze();
1236 SHOW_PARSE
1237 {
1238 do
1239 {
1240 fprintf( stderr,
1241 "( %d ) %s():",
1242 CURRENT_LINE_NUMBER,
1243 __func__);
1244 }
1245 while(0);
1246 SHOW_PARSE_REF(" ", refer);
1247 if( parsed_var->data.initial )
1248 {
1249 SHOW_PARSE_TEXT(" >>")
1250 if( parsed_var->level == 88)
1251 {
1252 size_t returned_size = 0;
1253 char *string88 = get_level_88_domain(0, parsed_var, returned_size);
1254
1255 char *p = string88;
1256 bool first = true;
1257 while(*p)
1258 {
1259 char *pend;
1260 size_t length1 = strtoull(p, &pend, 10);
1261 char *string1 = pend + 1;
1262 char flag = *pend;
1263 p = string1 + length1;
1264 if(flag == 'A' )
1265 {
1266 char ach2[] = "x";
1267 SHOW_PARSE_TEXT("\"")
1268 for(size_t i=0; i<length1; i++)
1269 {
1270 ach2[0] = string1[i];
1271 SHOW_PARSE_TEXT(ach2)
1272 }
1273 SHOW_PARSE_TEXT("\"")
1274 }
1275 else
1276 {
1277 switch(string1[0])
1278 {
1279 case 'L':
1280 SHOW_PARSE_TEXT("LOW-VALUE")
1281 break;
1282 case 'Z':
1283 SHOW_PARSE_TEXT("ZERO")
1284 break;
1285 case 'S':
1286 SHOW_PARSE_TEXT("SPACE")
1287 break;
1288 case 'Q':
1289 SHOW_PARSE_TEXT("QUOTE")
1290 break;
1291 case 'H':
1292 SHOW_PARSE_TEXT("HIGH-VALUE")
1293 break;
1294 default:
1295 SHOW_PARSE_TEXT("???")
1296 break;
1297 }
1298 }
1299 if( first )
1300 {
1301 SHOW_PARSE_TEXT("/")
1302 }
1303 else
1304 {
1305 if(*p)
1306 {
1307 SHOW_PARSE_TEXT(" ")
1308 }
1309 }
1310 first = !first;
1311 }
1312 free(string88);
1313 }
1314 else if( parsed_var->type == FldClass )
1315 {
1316 char *p = get_class_condition_string(parsed_var);
1317 SHOW_PARSE_TEXT(p);
1318 free(p);
1319 }
1320 else
1321 {
1322 switch(parsed_var->type)
1323 {
1324 case FldGroup:
1325 case FldAlphanumeric:
1326 case FldNumericEdited:
1327 case FldAlphaEdited:
1328 case FldLiteralA:
1329 SHOW_PARSE_TEXT(parsed_var->data.initial);
1330 break;
1331 default:
1332 {
1333 char ach[128];
1334 real_to_decimal (ach,
1335 TREE_REAL_CST_PTR (parsed_var->data.value_of()),
1336 sizeof(ach), 16, 0);
1337 SHOW_PARSE_TEXT(ach);
1338 break;
1339 }
1340 }
1341
1342 }
1343 SHOW_PARSE_TEXT("<<")
1344 }
1345 SHOW_PARSE_END
1346 }
1347
1348 // When initializing a variable, we have to ignore any DEPENDING ON clause
1349 // that might otherwise apply
1350 suppress_dest_depends = true;
1351
1352 bool is_redefined = false;
1353
1354 const cbl_field_t *family_tree = parsed_var;
1355 while(family_tree)
1356 {
1357 if( symbol_redefines(family_tree) )
1358 {
1359 is_redefined = true;
1360 break;
1361 }
1362
1363 family_tree = parent_of(family_tree);
1364 }
1365
1366 if( parsed_var->level == 66 )
1367 {
1368 // Treat RENAMES as if they are redefines:
1369 is_redefined = true;
1370 }
1371
1372 if( parsed_var->data.initial )
1373 {
1374 bool a_parent_initialized = false;
1375 const cbl_field_t *parent = parent_of(parsed_var);
1376 while( parent )
1377 {
1378 if( parent->attr & has_value_e )
1379 {
1380 a_parent_initialized = true;
1381 break;
1382 }
1383 parent = parent_of(parent);
1384 }
1385 if( !a_parent_initialized )
1386 {
1387 parsed_var->attr |= has_value_e;
1388 }
1389 }
1390
1391 static const int DEFAULT_BYTE_MASK = 0x00000000FF;
1392 static const int NSUBSCRIPT_MASK = 0x0000000F00;
1393 static const int NSUBSCRIPT_SHIFT = 8;
1394 static const int DEFAULTBYTE_BIT = 0x0000001000;
1395 static const int EXPLICIT_BIT = 0x0000002000;
1396 static const int REDEFINED_BIT = 0x0000004000;
1397 static const int JUST_ONCE_BIT = 0x0000008000;
1398
1399 int flag_bits = 0;
1400 flag_bits |= explicitly ? EXPLICIT_BIT : 0;
1401 flag_bits |= is_redefined && !explicitly ? REDEFINED_BIT : 0 ;
1402 flag_bits |= wsclear()
1403 ? DEFAULTBYTE_BIT + (*wsclear() & DEFAULT_BYTE_MASK)
1404 : 0;
1405 flag_bits |= (refer.nsubscript() << NSUBSCRIPT_SHIFT) & NSUBSCRIPT_MASK;
1406 flag_bits |= just_once ? JUST_ONCE_BIT : 0 ;
1407
1408 suppress_dest_depends = false; // Set this to false so that refer_is_clean is valid
1409 //fprintf(stderr, "refer_is_clean %2.2d %s %d 0x%lx\n", refer.field->level, refer.field->name, refer_is_clean(refer), refer.field->attr);
1410
1411 if( !refer_is_clean(refer) )
1412 {
1413 gg_call(VOID,
1414 "__gg__initialize_variable",
1415 gg_get_address_of(refer.field->var_decl_node),
1416 refer_offset(refer),
1417 build_int_cst_type(INT, flag_bits),
1418 NULL_TREE);
1419 }
1420 else
1421 {
1422 // We have a clean refer with no mods, so we can send just the pointer to
1423 // the field
1424 gg_call(VOID,
1425 "__gg__initialize_variable_clean",
1426 gg_get_address_of(refer.field->var_decl_node),
1427 build_int_cst_type(INT, flag_bits) ,
1428 NULL_TREE);
1429 }
1430
1431 suppress_dest_depends = true;
1432
1433 TRACE1
1434 {
1435 TRACE1_HEADER
1436 if( refer.field->level )
1437 {
1438 gg_fprintf( trace_handle,
1439 1, "%2.2d ",
1440 build_int_cst_type(INT, refer.field->level));
1441 }
1442 TRACE1_REFER_INFO("", refer)
1443 if( refer.field->level == 88 )
1444 {
1445 TRACE1_TEXT(" [");
1446
1447 size_t returned_size = 0;
1448 char *string88 = get_level_88_domain(0, parsed_var, returned_size);
1449
1450 char *p = string88;
1451 bool first = true;
1452 while(*p)
1453 {
1454 char *pend;
1455 size_t length1 = strtoull(p, &pend, 10);
1456 char *string1 = pend + 1;
1457 char flag = *pend;
1458 p = string1 + length1;
1459 if( flag == 'A' )
1460 {
1461 char ach2[] = "x";
1462 TRACE1_TEXT("\"")
1463 for(size_t i=0; i<length1; i++)
1464 {
1465 ach2[0] = string1[i];
1466 TRACE1_TEXT(ach2)
1467 }
1468 TRACE1_TEXT("\"")
1469 }
1470 else
1471 {
1472 switch(string1[0])
1473 {
1474 case 'L':
1475 TRACE1_TEXT("LOW-VALUE")
1476 break;
1477 case 'Z':
1478 TRACE1_TEXT("ZERO")
1479 break;
1480 case 'S':
1481 TRACE1_TEXT("SPACE")
1482 break;
1483 case 'Q':
1484 TRACE1_TEXT("QUOTE")
1485 break;
1486 case 'H':
1487 TRACE1_TEXT("HIGH-VALUE")
1488 break;
1489 default:
1490 TRACE1_TEXT("???")
1491 break;
1492 }
1493 }
1494 if( first )
1495 {
1496 TRACE1_TEXT("/")
1497 }
1498 else
1499 {
1500 if(*p)
1501 {
1502 TRACE1_TEXT(" ")
1503 }
1504 }
1505 first = !first;
1506 }
1507 free(string88);
1508 TRACE1_TEXT("] ");
1509 }
1510 else if( parsed_var->type == FldClass )
1511 {
1512 char *p = get_class_condition_string(parsed_var);
1513 TRACE1_TEXT(p);
1514 free(p);
1515 }
1516 else
1517 {
1518 // Convert strings of spaces to "<SPACES>"
1519 tree spaces = gg_define_int(0);
1520 if( parsed_var->type == FldGroup
1521 || parsed_var->type == FldAlphanumeric
1522 || parsed_var->type == FldAlphaEdited
1523 || parsed_var->type == FldLiteralA )
1524 {
1525 gg_assign(spaces, integer_one_node);
1526 tree counter = gg_define_int(parsed_var->data.capacity);
1527 WHILE(counter, gt_op, integer_zero_node)
1528 {
1529 gg_decrement(counter);
1530 IF( gg_indirect(member(parsed_var->var_decl_node, "data"), counter),
1531 ne_op,
1532 build_int_cst_type(UCHAR, ' ') )
1533 {
1534 gg_assign(spaces, integer_zero_node);
1535 }
1536 ELSE
1537 {
1538 }
1539 ENDIF
1540 }
1541 WEND
1542 }
1543 IF(spaces, eq_op, integer_one_node)
1544 {
1545 TRACE1_TEXT(" <SPACES>")
1546 }
1547 ELSE
1548 {
1549 TRACE1_FIELD_VALUE("", parsed_var, "")
1550 }
1551 ENDIF
1552 }
1553 TRACE1_END
1554 }
1555 suppress_dest_depends = false;
1556 }
1557
1558 //static void
1559 //initialize_variable_internal( cbl_field_t *field,
1560 // bool explicitly=false,
1561 // bool just_once=false)
1562 // {
1563 // cbl_refer_t wrapper(field);
1564 // initialize_variable_internal( wrapper,
1565 // explicitly,
1566 // just_once);
1567 // }
1568
1569 void
1570 parser_initialize(const cbl_refer_t& refer, bool like_parser_symbol_add)
1571 {
1572 //gg_printf("parser_initialize %s\n", gg_string_literal(refer.field->name), NULL_TREE);
1573 if( like_parser_symbol_add )
1574 {
1575 initialize_variable_internal(refer);
1576 }
1577 else
1578 {
1579 gcc_assert(refer.field->data.initial);
1580 static const bool explicitly = true;
1581 initialize_variable_internal(refer, explicitly);
1582 }
1583 }
1584
1585 static void
1586 get_binary_value_from_float(tree value,
1587 const cbl_refer_t &dest,
1588 cbl_field_t *source,
1589 tree source_offset
1590 )
1591 {
1592 // The destination is something with rdigits; the source is FldFloat
1593 tree ftype;
1594 switch( source->data.capacity )
1595 {
1596 case 4:
1597 ftype = FLOAT;
1598 break;
1599 case 8:
1600 ftype = DOUBLE;
1601 break;
1602 case 16:
1603 ftype = FLOAT128;
1604 break;
1605 default:
1606 gcc_unreachable();
1607 break;
1608 }
1609 tree fvalue = gg_define_variable(ftype);
1610 gg_assign(fvalue,
1611 gg_indirect(gg_cast(build_pointer_type(ftype),
1612 gg_add( member(source->var_decl_node,"data"),
1613 source_offset))));
1614
1615 // We need to convert the floating point value to an integer value with the
1616 // rdigits lined up properly.
1617
1618 int rdigits = get_scaled_rdigits( dest.field );
1619 gg_assign(fvalue,
1620 gg_multiply(fvalue,
1621 gg_float(ftype,
1622 wide_int_to_tree(INT,
1623 get_power_of_ten(rdigits)))));
1624
1625 // And we need to throw away any digits to the left of the leftmost digits:
1626 // At least, we need to do so in principl. I am deferring this problem until
1627 // I understand it better.
1628
1629 // We now have a floating point value that has been multiplied by 10**rdigits
1630 gg_assign(value, gg_trunc(TREE_TYPE(value), fvalue));
1631 }
1632
1633 #pragma GCC diagnostic push
1634 #pragma GCC diagnostic ignored "-Wunused-function"
1635 static void
1636 gg_attribute_bit_clear(struct cbl_field_t *var, cbl_field_attr_t bits)
1637 {
1638 gg_assign( member(var, "attr"),
1639 gg_bitwise_and( member(var, "attr"),
1640 gg_bitwise_not( build_int_cst_type(SIZE_T, bits) )));
1641 }
1642
1643 static
1644 tree
1645 gg_attribute_bit_get(struct cbl_field_t *var, cbl_field_attr_t bits)
1646 {
1647 tree retval = gg_bitwise_and( member(var, "attr"),
1648 build_int_cst_type(SIZE_T, bits) );
1649 return retval;
1650 }
1651
1652 static void
1653 gg_attribute_bit_set(struct cbl_field_t *var, cbl_field_attr_t bits)
1654 {
1655 gg_assign( member(var, "attr"),
1656 gg_bitwise_or(member(var, "attr"),
1657 build_int_cst_type(SIZE_T, bits)));
1658 }
1659 #pragma GCC diagnostic pop
1660
1661 static void
1662 gg_default_qualification(struct cbl_field_t * /*var*/)
1663 {
1664 // gg_attribute_bit_clear(var, refmod_e);
1665 }
1666
1667 static
1668 void
1669 depending_on_value(tree depending_on, cbl_field_t *current_sizer)
1670 {
1671 // We have to deal with the possibility of a DEPENDING_ON variable,
1672 // and we have to apply array bounds whether or not there is a DEPENDING_ON
1673 // variable:
1674
1675 // tree occurs_lower = gg_define_variable(LONG, "_lower");
1676 // tree occurs_upper = gg_define_variable(LONG, "_upper");
1677 //
1678 // gg_assign(occurs_lower, build_int_cst_type(LONG, current_sizer->occurs.bounds.lower));
1679 // gg_assign(occurs_upper, build_int_cst_type(LONG, current_sizer->occurs.bounds.upper));
1680
1681 gcc_assert(current_sizer);
1682 if( current_sizer->occurs.depending_on )
1683 {
1684 get_depending_on_value_from_odo(depending_on, current_sizer);
1685 }
1686 else
1687 {
1688 gg_assign(depending_on,
1689 build_int_cst_type(LONG, current_sizer->occurs.bounds.upper));
1690 }
1691 }
1692
1693 static int
1694 digits_to_bytes(int digits)
1695 {
1696 int retval;
1697 if( digits <= 2 )
1698 {
1699 retval = 1;
1700 }
1701 else if( digits <= 4 )
1702 {
1703 retval = 2;
1704 }
1705 else if( digits <= 9 )
1706 {
1707 retval = 4;
1708 }
1709 else if( digits <= 18 )
1710 {
1711 retval = 8;
1712 }
1713 else
1714 {
1715 retval = 16;
1716 }
1717 return retval;
1718 }
1719
1720 static size_t
1721 get_bytes_needed(cbl_field_t *field)
1722 {
1723 size_t retval = 0;
1724 switch(field->type)
1725 {
1726 case FldIndex:
1727 case FldPointer:
1728 case FldFloat:
1729 case FldLiteralN:
1730 retval = field->data.capacity;
1731 break;
1732
1733 case FldNumericDisplay:
1734 {
1735 int digits;
1736 if( field->attr & scaled_e && field->data.rdigits<0)
1737 {
1738 digits = field->data.digits + -field->data.rdigits;
1739 }
1740 else
1741 {
1742 digits = field->data.digits;
1743 }
1744 retval = digits_to_bytes(digits);
1745 break;
1746 }
1747
1748 case FldPacked:
1749 {
1750 int digits;
1751 if( field->attr & scaled_e && field->data.rdigits<0)
1752 {
1753 digits = field->data.digits + -field->data.rdigits;
1754 }
1755 else
1756 {
1757 digits = field->data.digits;
1758 }
1759 if( !(field->attr & separate_e) )
1760 {
1761 // This is COMP-3, so there is a sign nybble.
1762 digits += 1;
1763 }
1764 retval = (digits+1)/2;
1765 break;
1766 }
1767
1768 case FldNumericBinary:
1769 case FldNumericBin5:
1770 {
1771 if( field->data.digits )
1772 {
1773 int digits;
1774 if( field->attr & scaled_e && field->data.rdigits<0)
1775 {
1776 digits = field->data.digits + -field->data.rdigits;
1777 }
1778 else
1779 {
1780 digits = field->data.digits;
1781 }
1782 retval = digits_to_bytes(digits);
1783 }
1784 else
1785 {
1786 retval = field->data.capacity;
1787 }
1788 break;
1789 }
1790
1791 default:
1792 cbl_internal_error("%s: Knows not the variable type %s for %s",
1793 __func__,
1794 cbl_field_type_str(field->type),
1795 field->name );
1796 break;
1797 }
1798 return retval;
1799 }
1800
1801 static void
1802 normal_normal_compare(bool debugging,
1803 tree return_int,
1804 cbl_refer_t *left_side_ref,
1805 cbl_refer_t *right_side_ref,
1806 tree left_side,
1807 tree right_side )
1808 {
1809 Analyze();
1810
1811 // If a value is intermediate_e, then the rdigits can vary at run-time, so
1812 // we can't rely on the compile-time rdigits.
1813
1814 bool left_intermediate = (left_side_ref->field->attr & intermediate_e);
1815 bool right_intermediate = (right_side_ref->field->attr & intermediate_e);
1816
1817 if( debugging )
1818 {
1819 gg_printf("normal_normal_compare(): left_intermediate/right_intermediate %d/%d\n",
1820 left_intermediate ? integer_one_node : integer_zero_node ,
1821 right_intermediate ? integer_one_node : integer_zero_node ,
1822 NULL_TREE);
1823 }
1824
1825 if( !left_intermediate && !right_intermediate )
1826 {
1827 // Yay! Both sides have fixed rdigit values.
1828
1829 int adjust = get_scaled_rdigits(left_side_ref->field)
1830 - get_scaled_rdigits(right_side_ref->field);
1831 if( adjust > 0 )
1832 {
1833 // We need to make right_side bigger to match the scale of left_side
1834 scale_by_power_of_ten_N(right_side, adjust);
1835 }
1836 else if( adjust < 0 )
1837 {
1838 // We need to make left_side bigger to match the scale of right_side
1839 scale_by_power_of_ten_N(left_side, -adjust);
1840 }
1841 }
1842 else
1843 {
1844 // At least one side is right_intermediate
1845 bool needs_adjusting;
1846
1847 tree adjust;
1848 if( !left_intermediate && right_intermediate )
1849 {
1850 // left is fixed, right is intermediate
1851 adjust = gg_define_int();
1852 gg_assign(adjust,
1853 build_int_cst_type( INT,
1854 get_scaled_rdigits(left_side_ref->field)));
1855
1856 gg_assign(adjust,
1857 gg_subtract(adjust,
1858 gg_cast(INT,
1859 member(right_side_ref->field->var_decl_node,
1860 "rdigits"))));
1861 needs_adjusting = true;
1862 }
1863 else if( left_intermediate && !right_intermediate )
1864 {
1865 // left is intermediate, right is fixed
1866 adjust = gg_define_int();
1867 gg_assign(adjust, gg_cast(INT, member(left_side_ref->field, "rdigits")));
1868 gg_assign(adjust,
1869 gg_subtract(adjust,
1870 build_int_cst_type( INT,
1871 get_scaled_rdigits(right_side_ref->field))));
1872 needs_adjusting = true;
1873 }
1874 else // if( left_intermediate && right_intermediate )
1875 {
1876 // Both sides are intermediate_e
1877 adjust = gg_define_int();
1878 gg_assign(adjust, gg_cast(INT, member(left_side_ref->field, "rdigits")));
1879 gg_assign(adjust,
1880 gg_subtract(adjust,
1881 gg_cast(INT,
1882 member(right_side_ref->field, "rdigits"))));
1883 needs_adjusting = true;
1884 }
1885
1886 if( needs_adjusting )
1887 {
1888 if( debugging )
1889 {
1890 gg_printf("normal_normal_compare(): The value of adjust is %d\n",
1891 adjust,
1892 NULL_TREE);
1893 }
1894 IF( adjust, gt_op, integer_zero_node )
1895 {
1896 // The right side needs to be scaled up
1897 scale_by_power_of_ten(right_side, adjust);
1898 }
1899 ELSE
1900 {
1901 IF( adjust, lt_op, integer_zero_node )
1902 {
1903 // The left side needs to be scaled up
1904 scale_by_power_of_ten(left_side, gg_negate(adjust));
1905 }
1906 ELSE
1907 ENDIF
1908 }
1909 ENDIF
1910 }
1911 }
1912
1913 if( TREE_TYPE(left_side) != TREE_TYPE(right_side) )
1914 {
1915 // One is signed, the other isn't:
1916 if( left_side_ref->field->attr & signable_e )
1917 {
1918 // The left side can be negative. If it is, the return value has to be
1919 // -1 for left < right
1920 IF( left_side, lt_op, gg_cast(TREE_TYPE(left_side), integer_zero_node) )
1921 {
1922 if( debugging )
1923 {
1924 gg_printf("normal_normal_compare(): different types returning -1\n",
1925 NULL_TREE);
1926 }
1927 gg_assign( return_int, integer_minusone_node);
1928 }
1929 ELSE
1930 {
1931 // Both sides are positive, allowing a direct comparison.
1932 IF( gg_cast(TREE_TYPE(right_side), left_side), lt_op, right_side )
1933 {
1934 if( debugging )
1935 {
1936 gg_printf("normal_normal_compare(): returning -1\n", NULL_TREE);
1937 }
1938 gg_assign( return_int, integer_minusone_node);
1939 }
1940 ELSE
1941 {
1942 IF( gg_cast(TREE_TYPE(right_side), left_side), gt_op, right_side)
1943 {
1944 if( debugging )
1945 {
1946 gg_printf("normal_normal_compare(): returning +1\n", NULL_TREE);
1947 }
1948 gg_assign( return_int, integer_one_node);
1949 }
1950 ELSE
1951 {
1952 if( debugging )
1953 {
1954 gg_printf("normal_normal_compare(): returning zero\n", NULL_TREE);
1955 }
1956 gg_assign( return_int, integer_zero_node);
1957 }
1958 ENDIF
1959 }
1960 ENDIF
1961 }
1962 ENDIF
1963 }
1964 else
1965 {
1966 // The right side can be negative. If it is, the return value has to be
1967 // +1 for left > right
1968 IF( right_side, lt_op, gg_cast(TREE_TYPE(right_side), integer_zero_node) )
1969 {
1970 if( debugging )
1971 {
1972 gg_printf("normal_normal_compare(): different types returning +1\n", NULL_TREE);
1973 }
1974 gg_assign( return_int, integer_one_node);
1975 }
1976 ELSE
1977 {
1978 // Both sides are positive, allowing a direct comparison.
1979 IF( left_side, lt_op, gg_cast(TREE_TYPE(left_side), right_side) )
1980 {
1981 if( debugging )
1982 {
1983 gg_printf("normal_normal_compare(): returning -1\n", NULL_TREE);
1984 }
1985 gg_assign( return_int, integer_minusone_node);
1986 }
1987 ELSE
1988 {
1989 IF( left_side, gt_op, gg_cast(TREE_TYPE(left_side), right_side) )
1990 {
1991 if( debugging )
1992 {
1993 gg_printf("normal_normal_compare(): returning +1\n", NULL_TREE);
1994 }
1995 gg_assign( return_int, integer_one_node);
1996 }
1997 ELSE
1998 {
1999 if( debugging )
2000 {
2001 gg_printf("normal_normal_compare(): returning zero\n", NULL_TREE);
2002 }
2003 gg_assign( return_int, integer_zero_node);
2004 }
2005 ENDIF
2006 }
2007 ENDIF
2008 }
2009 ENDIF
2010 }
2011 }
2012 else
2013 {
2014 // Both sides are the same type, allowing a direct comparison.
2015 IF( left_side, lt_op, right_side )
2016 {
2017 if( debugging )
2018 {
2019 gg_printf("normal_normal_compare(): returning -1\n", NULL_TREE);
2020 }
2021 gg_assign( return_int, integer_minusone_node);
2022 }
2023 ELSE
2024 {
2025 IF( left_side, gt_op, right_side )
2026 {
2027 if( debugging )
2028 {
2029 gg_printf("normal_normal_compare(): returning +1\n", NULL_TREE);
2030 }
2031 gg_assign( return_int, integer_one_node);
2032 }
2033 ELSE
2034 {
2035 if( debugging )
2036 {
2037 gg_printf("normal_normal_compare(): returning zero\n", NULL_TREE);
2038 }
2039 gg_assign( return_int, integer_zero_node);
2040 }
2041 ENDIF
2042 }
2043 ENDIF
2044 }
2045 }
2046
2047 static void
2048 compare_binary_binary(tree return_int,
2049 cbl_refer_t *left_side_ref,
2050 cbl_refer_t *right_side_ref )
2051 {
2052 Analyze();
2053 static const bool debugging = false;
2054
2055 // We know the two sides have binary values that can be extracted.
2056 tree left_side;
2057 tree right_side;
2058
2059 // Use SIZE128 when we need two 64-bit registers to hold the value. All
2060 // others fit into 64-bit LONG with pretty much the same efficiency.
2061
2062 size_t left_bytes_needed = get_bytes_needed(left_side_ref->field);
2063 size_t right_bytes_needed = get_bytes_needed(right_side_ref->field);
2064
2065 if( left_bytes_needed >= SIZE128
2066 || right_bytes_needed >= SIZE128 )
2067 {
2068 if( debugging )
2069 {
2070 gg_printf("compare_binary_binary(): using int128\n", NULL_TREE);
2071 }
2072
2073 left_side = gg_define_int128();
2074 right_side = gg_define_int128();
2075 }
2076 else
2077 {
2078 if( debugging )
2079 {
2080 gg_printf("compare_binary_binary(): using int64\n", NULL_TREE);
2081 }
2082 left_side = gg_define_variable( left_side_ref->field->has_attr(signable_e) ? LONG : ULONG );
2083 right_side = gg_define_variable(right_side_ref->field->has_attr(signable_e) ? LONG : ULONG );
2084 }
2085
2086 //tree dummy = gg_define_int();
2087 static tree hilo_left = gg_define_variable(INT, "..cbb_hilo_left", vs_file_static);
2088 static tree hilo_right = gg_define_variable(INT, "..cbb_hilo_right", vs_file_static);
2089
2090 get_binary_value(left_side,
2091 NULL,
2092 left_side_ref->field,
2093 refer_offset(*left_side_ref),
2094 hilo_left);
2095 get_binary_value(right_side,
2096 NULL,
2097 right_side_ref->field,
2098 refer_offset(*right_side_ref),
2099 hilo_right);
2100 IF( hilo_left, eq_op, integer_one_node )
2101 {
2102 // left side is hi-value
2103 IF( hilo_right, eq_op, integer_one_node )
2104 {
2105 if( debugging )
2106 {
2107 gg_printf("compare_binary_binary(): left and right are HIGH-VALUE\n", NULL_TREE);
2108 }
2109 gg_assign(return_int, integer_zero_node);
2110 }
2111 ELSE
2112 {
2113 if( debugging )
2114 {
2115 gg_printf("compare_binary_binary(): left is HIGH-VALUE\n", NULL_TREE);
2116 }
2117 gg_assign(return_int, integer_one_node);
2118 }
2119 ENDIF
2120 }
2121 ELSE
2122 {
2123 // left is not HIGH-VALUE:
2124 IF( hilo_left, eq_op, integer_minus_one_node )
2125 {
2126 // left side is LOW-VALUE
2127 IF( hilo_right, eq_op, integer_minus_one_node )
2128 {
2129 if( debugging )
2130 {
2131 gg_printf("compare_binary_binary(): left and right are LOW-VALUE\n", NULL_TREE);
2132 }
2133 gg_assign(return_int, integer_zero_node);
2134 }
2135 ELSE
2136 {
2137 // Right side is not low-value
2138 if( debugging )
2139 {
2140 gg_printf("compare_binary_binary(): left is LOW-VALUE\n", NULL_TREE);
2141 }
2142 gg_assign(return_int, integer_one_node);
2143 }
2144 ENDIF
2145 }
2146 ELSE
2147 {
2148 // Left side is normal
2149 IF( hilo_right, eq_op, integer_one_node )
2150 {
2151 if( debugging )
2152 {
2153 gg_printf("compare_binary_binary(): right is HIGH-VALUE\n", NULL_TREE);
2154 }
2155 gg_assign(return_int, integer_minus_one_node);
2156 }
2157 ELSE
2158 {
2159 IF( hilo_right, eq_op, integer_minus_one_node )
2160 {
2161 if( debugging )
2162 {
2163 gg_printf("compare_binary_binary(): right is LOW-VALUE\n", NULL_TREE);
2164 }
2165 gg_assign(return_int, integer_one_node);
2166 }
2167 ELSE
2168 {
2169 if( debugging )
2170 {
2171 gg_printf("compare_binary_binary(): left and right are normal\n", NULL_TREE);
2172 }
2173 normal_normal_compare(debugging,
2174 return_int,
2175 left_side_ref,
2176 right_side_ref,
2177 left_side,
2178 right_side
2179 );
2180 }
2181 ENDIF
2182 }
2183 ENDIF
2184 }
2185 ENDIF
2186 }
2187 ENDIF
2188 }
2189
2190 #define DEBUG_COMPARE
2191
2192 static void
2193 cobol_compare( tree return_int,
2194 cbl_refer_t &left_side_ref,
2195 cbl_refer_t &right_side_ref )
2196 {
2197 Analyze();
2198 // gg_printf("cobol_compare %s %s \"%s\" \"%s\"\n",
2199 // gg_string_literal(left_side_ref.field->name),
2200 // gg_string_literal(right_side_ref.field->name),
2201 // member(left_side_ref.field, "data"),
2202 // gg_string_literal(right_side_ref.field->data.initial),
2203 // NULL_TREE);
2204
2205 CHECK_FIELD(left_side_ref.field);
2206 CHECK_FIELD(right_side_ref.field);
2207 // This routine is in support of conditionals in the COBOL program.
2208 // It takes two arbitrary COBOL variables from the parser and compares them
2209 // according to a nightmarish set of rules.
2210
2211 // See ISO/IEC 1989:2014(E) section 8.8.4.1.1 (page 153)
2212
2213 // The return_int value is -1 when left_side < right_side
2214 // 0 left_side == right_side
2215 // 1 left_side > right_side
2216
2217 bool compared = false;
2218
2219 // In the effort to convert to in-line GIMPLE comparisons, I became flummoxed
2220 // by comparisons involving REFMODs. This will have to be revisited, but for
2221 // now I decided to keep using the libgcobol code, which according to NIST
2222 // works properly.
2223
2224 if( !left_side_ref.refmod.from
2225 && !left_side_ref.refmod.len
2226 && !right_side_ref.refmod.from
2227 && !right_side_ref.refmod.len )
2228 {
2229 cbl_refer_t *lefty = &left_side_ref;
2230 cbl_refer_t *righty = &right_side_ref;
2231
2232 int ntries = 1;
2233 while( ntries <= 2 )
2234 {
2235 switch( lefty->field->type )
2236 {
2237 case FldLiteralN:
2238 {
2239 switch( righty->field->type )
2240 {
2241 case FldLiteralN:
2242 case FldNumericBinary:
2243 case FldNumericBin5:
2244 case FldPacked:
2245 case FldNumericDisplay:
2246 case FldIndex:
2247 compare_binary_binary(return_int, lefty, righty);
2248 compared = true;
2249 break;
2250
2251 case FldGroup:
2252 case FldAlphanumeric:
2253 case FldLiteralA:
2254 {
2255 // Comparing a FldLiteralN to an alphanumeric
2256 // It is the case that data.initial is in the original form seen
2257 // in the source code, which means that even in EBCDIC mode the
2258 // characters are in the "ASCII" state.
2259
2260 static size_t buffer_size = 0;
2261 static char *buffer = NULL;
2262 raw_to_internal(&buffer,
2263 &buffer_size,
2264 lefty->field->data.initial,
2265 strlen(lefty->field->data.initial));
2266
2267 gg_assign( return_int, gg_call_expr(
2268 INT,
2269 "__gg__literaln_alpha_compare",
2270 gg_string_literal(buffer),
2271 gg_get_address_of(righty->field->var_decl_node),
2272 refer_offset(*righty),
2273 refer_size_source( *righty),
2274 build_int_cst_type(INT,
2275 (righty->all ? REFER_T_MOVE_ALL : 0)),
2276 NULL_TREE));
2277 compared = true;
2278 break;
2279 }
2280
2281 default:
2282 break;
2283 }
2284 break;
2285 }
2286
2287 case FldNumericBin5:
2288 case FldNumericBinary:
2289 case FldPacked:
2290 case FldNumericDisplay:
2291 {
2292 switch( righty->field->type )
2293 {
2294 case FldNumericBin5:
2295 case FldNumericBinary:
2296 case FldPacked:
2297 case FldNumericDisplay:
2298 {
2299 compare_binary_binary(return_int, lefty, righty);
2300 compared = true;
2301 break;
2302 }
2303
2304 default:
2305 break;
2306 }
2307 break;
2308 }
2309
2310 default:
2311 break;
2312 }
2313 if( compared )
2314 {
2315 break;
2316 }
2317 // We weren't able to compare left/right. Let's see if we understand
2318 // right/left
2319 std::swap(lefty, righty);
2320 ntries += 1;
2321 }
2322
2323 if( compared && ntries == 2 )
2324 {
2325 // We have a successful comparision, but we managed it on the second try,
2326 // which means our result has the wrong sign. Fix it:
2327 gg_assign(return_int, gg_negate(return_int));
2328 }
2329 }
2330
2331 if( !compared )
2332 {
2333 // None of our explicit comparisons up above worked, so we revert to the
2334 // general case:
2335 int leftflags = (left_side_ref.all ? REFER_T_MOVE_ALL : 0)
2336 + (left_side_ref.addr_of ? REFER_T_ADDRESS_OF : 0)
2337 + (left_side_ref.refmod.from ? REFER_T_REFMOD : 0);
2338 int rightflags = (right_side_ref.all ? REFER_T_MOVE_ALL : 0)
2339 + (right_side_ref.addr_of ? REFER_T_ADDRESS_OF : 0)
2340 + (right_side_ref.refmod.from ? REFER_T_REFMOD : 0);
2341 gg_assign( return_int, gg_call_expr(
2342 INT,
2343 "__gg__compare",
2344 gg_get_address_of(left_side_ref.field->var_decl_node),
2345 refer_offset(left_side_ref),
2346 refer_size_source( left_side_ref),
2347 build_int_cst_type(INT, leftflags),
2348 gg_get_address_of(right_side_ref.field->var_decl_node),
2349 refer_offset(right_side_ref),
2350 refer_size_source( right_side_ref),
2351 build_int_cst_type(INT, rightflags),
2352 integer_zero_node,
2353 NULL_TREE));
2354 // compared = true; // Commented out to quiet cppcheck
2355 }
2356
2357 // gg_printf(" result is %d\n", return_int, NULL_TREE);
2358 }
2359
2360 static void
2361 move_tree( cbl_field_t *dest,
2362 tree offset,
2363 tree psz_source,
2364 tree length_bump=integer_zero_node) // psz_source is a null-terminated string
2365 {
2366 Analyze();
2367 SHOW_PARSE
2368 {
2369 SHOW_PARSE_HEADER
2370 SHOW_PARSE_FIELD(" ", dest);
2371 SHOW_PARSE_END
2372 }
2373
2374 CHECK_FIELD(dest);
2375
2376 bool moved = true;
2377
2378 tree source_length = gg_define_size_t();
2379 gg_assign(source_length, gg_strlen(psz_source));
2380 gg_assign(source_length, gg_add(source_length, gg_cast(SIZE_T, length_bump)));
2381
2382 tree min_length = gg_define_size_t();
2383
2384 tree location = gg_define_uchar_star();
2385 tree length = gg_define_size_t();
2386
2387 gg_assign(location,
2388 gg_add(member(dest->var_decl_node, "data"),
2389 offset));
2390 gg_assign(length,
2391 member(dest->var_decl_node, "capacity"));
2392
2393 IF(source_length, lt_op, length)
2394 {
2395 gg_assign(min_length, source_length);
2396 }
2397 ELSE
2398 {
2399 gg_assign(min_length, length);
2400 }
2401 ENDIF
2402
2403 tree value;
2404 tree rdigits;
2405
2406 switch( dest->type )
2407 {
2408 case FldGroup:
2409 case FldAlphanumeric:
2410 // Space out the alphanumeric destination:
2411 gg_memset( location,
2412 build_int_cst_type(INT, internal_space),
2413 length );
2414 // Copy the alphanumeric result over.
2415 gg_memcpy( location,
2416 psz_source,
2417 min_length );
2418 break;
2419
2420 case FldNumericDisplay:
2421 case FldNumericEdited:
2422 case FldNumericBinary:
2423 case FldNumericBin5:
2424 case FldPacked:
2425 case FldIndex:
2426 {
2427 value = gg_define_int128();
2428 rdigits = gg_define_int();
2429
2430 gg_assign(value,
2431 gg_call_expr( INT128,
2432 "__gg__dirty_to_binary_internal",
2433 psz_source,
2434 source_length,
2435 gg_get_address_of(rdigits),
2436 NULL_TREE));
2437
2438 gg_call(VOID,
2439 "__gg__int128_to_qualified_field",
2440 gg_get_address_of(dest->var_decl_node),
2441 offset,
2442 build_int_cst_type(SIZE_T, dest->data.capacity),
2443 value,
2444 rdigits,
2445 build_int_cst_type(INT, truncation_e),
2446 null_pointer_node,
2447 NULL_TREE);
2448 }
2449 break;
2450
2451 case FldAlphaEdited:
2452 {
2453 gg_call(VOID,
2454 "__gg__string_to_alpha_edited_ascii",
2455 location,
2456 psz_source,
2457 min_length,
2458 member(dest->var_decl_node, "picture"),
2459 NULL_TREE);
2460 break;
2461 }
2462
2463 default:
2464 moved = false;
2465 break;
2466 }
2467
2468 TRACE1
2469 {
2470 TRACE1_HEADER
2471 gg_fprintf(trace_handle, 1, "source: \"%s\"", psz_source);
2472 TRACE1_END
2473 TRACE1_INDENT
2474 TRACE1_FIELD( "dest : ", dest, "")
2475 TRACE1_END
2476 }
2477
2478 if( !moved )
2479 {
2480 dbgmsg("%10s in %s:%d", __func__, __FILE__, __LINE__ );
2481 cbl_internal_error( "I don%'t know how to MOVE an alphabetical string to %s(%s)",
2482 cbl_field_type_str(dest->type),
2483 dest->name
2484 );
2485 return;
2486 }
2487 }
2488
2489 static void
2490 move_tree_to_field(cbl_field_t *field, tree psz)
2491 {
2492 move_tree(field, integer_zero_node, psz);
2493 }
2494
2495 static tree
2496 get_string_from(cbl_field_t *field)
2497 {
2498 // This returns a malloced copy of either a literal string or a
2499 // an alphanumeric field. The idea is that eventually free() will be
2500 // called in the runtime space:
2501
2502 tree psz = gg_define_char_star();
2503
2504 if( field )
2505 {
2506 switch( field->type )
2507 {
2508 case FldLiteralA:
2509 {
2510 gg_assign(psz,
2511 gg_cast(CHAR_P,
2512 gg_malloc(build_int_cst_type(SIZE_T,
2513 field->data.capacity+1))));
2514 const char *litstring = get_literal_string(field);
2515 gg_memcpy(psz,
2516 gg_string_literal(litstring),
2517 build_int_cst_type(SIZE_T, field->data.capacity+1));
2518 break;
2519 }
2520
2521 case FldGroup:
2522 case FldAlphanumeric:
2523 // make a copy of .data:
2524 gg_assign(psz,
2525 gg_cast(CHAR_P,
2526 gg_malloc(build_int_cst_type(SIZE_T,
2527 field->data.capacity+1))));
2528 gg_memcpy( psz,
2529 member(field, "data"),
2530 member(field, "capacity"));
2531 // null-terminate it:
2532 gg_assign( gg_array_value(psz, member(field, "capacity")),
2533 char_nodes[0]);
2534 break;
2535
2536 case FldForward:
2537 {
2538 // At the present time, we are assuming this happens when somebody
2539 // specifies an unquoted file name in an ASSIGN statement:
2540 // SELECT file3 ASSIGN DISK.
2541 //
2542 // In that case, we just return DISK, which is field->name:
2543 psz = gg_strdup(gg_string_literal(field->name));
2544 break;
2545 }
2546
2547 default:
2548 cbl_internal_error(
2549 "%s: %<field->type%> %s must be literal or alphanumeric",
2550 __func__, cbl_field_type_str(field->type));
2551 break;
2552 }
2553 }
2554 else
2555 {
2556 gg_assign(psz, gg_cast(CHAR_P, null_pointer_node));
2557 }
2558 return psz;
2559 }
2560
2561 static char *
2562 combined_name(const cbl_label_t *label)
2563 {
2564 // This routine returns a pointer to a static, so make sure you use the result
2565 // before calling the routine again
2566 const char *para_name = nullptr;
2567 const char *sect_name = nullptr;
2568 const char *program_name = current_function->our_unmangled_name;
2569
2570 if( label->type == LblParagraph )
2571 {
2572 para_name = label->name;
2573
2574 if( label->parent )
2575 {
2576 // It's possible for implicit
2577 const cbl_label_t *section_label = cbl_label_of(symbol_at(label->parent));
2578 sect_name = section_label->name;
2579 }
2580 }
2581 else
2582 {
2583 sect_name = label->name;
2584 }
2585
2586 static size_t retval_size = 256;
2587 static char *retval= static_cast<char *>(xmalloc(retval_size));
2588
2589 char *paragraph = cobol_name_mangler(para_name);
2590 char *section = cobol_name_mangler(sect_name);
2591 char *mangled_program_name = cobol_name_mangler(program_name);
2592
2593 while( retval_size < (paragraph ? strlen(paragraph) : 0 )
2594 + (section ? strlen(section) : 0 )
2595 + (mangled_program_name ? strlen(mangled_program_name) : 0 )
2596 + 24 )
2597 {
2598 retval_size *= 2;
2599 retval = static_cast<char *>(xrealloc(retval, retval_size));
2600 }
2601 gcc_assert(retval);
2602
2603 *retval = '\0';
2604 char ach[24];
2605 if( paragraph )
2606 {
2607 strcat(retval, paragraph);
2608 }
2609 strcat(retval, ".");
2610 if( section )
2611 {
2612 strcat(retval, section);
2613 }
2614 strcat(retval, ".");
2615 if( mangled_program_name )
2616 {
2617 strcat(retval, mangled_program_name);
2618 }
2619 sprintf(ach, "." HOST_SIZE_T_PRINT_DEC,
2620 (fmt_size_t)current_function->program_id_number);
2621 strcat(retval, ach);
2622 sprintf(ach, "." HOST_SIZE_T_PRINT_DEC,
2623 (fmt_size_t)symbol_label_id(label));
2624 strcat(retval, ach);
2625 free(mangled_program_name);
2626 free(section);
2627 free(paragraph);
2628
2629 return retval;
2630 }
2631
2632 // We implement SECTION and PARAGRAPH stuff before the rest of program
2633 // structure, because we have some static routines in here that are called
2634 // by enter_ and leave_ program, and so on.
2635
2636 static void
2637 assembler_label(const char *label)
2638 {
2639 // label has to be a valid label for the assembler
2640 static size_t length = 0;
2641 static char *build = nullptr;
2642
2643 const char local_text[] = ":";
2644 if( length < strlen(label) + strlen(local_text) + 1 )
2645 {
2646 length = strlen(label) + strlen(local_text) + 1;
2647 free(build);
2648 build = static_cast<char *>(xmalloc(length));
2649 }
2650 gcc_assert(build);
2651
2652 strcpy(build, label);
2653 strcat(build, local_text);
2654
2655 gg_insert_into_assembler(build);
2656 }
2657
2658 static void
2659 section_label(struct cbl_proc_t *procedure)
2660 {
2661 // With nested programs, you can have multiple program/section pairs with the
2662 // the same names; we use a deconflictor to avoid collisions
2663
2664 size_t deconflictor = symbol_label_id(procedure->label);
2665
2666 cbl_label_t *label = procedure->label;
2667 // The _initialize_program section isn't relevant.
2668 char *psz = xasprintf("%s SECTION %s in %s (" HOST_SIZE_T_PRINT_DEC ")",
2669 ASM_COMMENT_START,
2670 label->name,
2671 current_function->our_unmangled_name,
2672 (fmt_size_t)deconflictor);
2673 gg_insert_into_assembler(psz);
2674 free(psz);
2675
2676 // The label has to start with an underscore. I tried a period, but those
2677 // don't seem to show up in GDB's internal symbol tables.
2678 char *psz2 = xasprintf( "_sect.%s",
2679 combined_name(procedure->label));
2680 SHOW_PARSE
2681 {
2682 SHOW_PARSE_HEADER
2683 SHOW_PARSE_TEXT(psz2);
2684 SHOW_PARSE_END
2685 }
2686 assembler_label(psz2);
2687 free(psz2);
2688 insert_nop(108);
2689 }
2690
2691 static void
2692 paragraph_label(struct cbl_proc_t *procedure)
2693 {
2694 // We need to give each paragraph a unique and assembler-compatible name
2695 // that can be found and used by GDB.
2696 // Complications:
2697 // 1) paragraph names can be reused in the same program, provided they
2698 // are in different sections.
2699 // 2) paragraph names can be duplicated in a section, provided that they
2700 // are not referenced by the program. We provide a deconflictor to
2701 // separate such labels.
2702
2703 cbl_label_t *paragraph = procedure->label;
2704 cbl_label_t *section = nullptr;
2705
2706 if( procedure->label->parent )
2707 {
2708 section = cbl_label_of(symbol_at(procedure->label->parent));
2709 }
2710
2711 char *para_name = paragraph->name;
2712 char *section_name = section ? section->name : nullptr;
2713
2714 size_t deconflictor = symbol_label_id(procedure->label);
2715
2716 char *psz1 =
2717 xasprintf(
2718 "%s PARAGRAPH %s of %s in %s (" HOST_SIZE_T_PRINT_DEC ")",
2719 ASM_COMMENT_START,
2720 para_name ? para_name: "" ,
2721 section_name ? section_name: "(null)" ,
2722 current_function->our_unmangled_name ? current_function->our_unmangled_name: "" ,
2723 (fmt_size_t)deconflictor );
2724
2725 // (0) is wrong, so back up one
2726
2727 gg_insert_into_assembler(psz1);
2728
2729 SHOW_PARSE
2730 {
2731 SHOW_PARSE_HEADER
2732 SHOW_PARSE_TEXT(psz1);
2733 SHOW_PARSE_END
2734 }
2735 free(psz1);
2736
2737 // The label has to start with an underscore. I tried a period, but those
2738 // don't seem to show up in GDB's internal symbol tables.
2739 char *psz2 = xasprintf( "_para.%s",
2740 combined_name(procedure->label));
2741 assembler_label(psz2);
2742 free(psz2);
2743
2744 // We are inserting a NOP after having created a label for the procedure.
2745 // This means that when using GDC_COBOL to step into a procedure, the
2746 // execution will stop there and show "123 para-name." at the stopped point.
2747 //
2748 // Note that because there is no user-specified executable code at that point
2749 // the user can't set a working breakpoint with "break 123". But because
2750 // GDB will pick up the psz2 text and set a breakpoint there (which is the
2751 // location of the NOP) "break para-name" will actually stop and show line
2752 // 123.
2753 //
2754 // This really only makes sense when you look at the assembly language. Keep
2755 // in mind as you read it that issuing a "break 123" causes GDB to set a
2756 // breakpoint at the first executable machine language code following the
2757 // first ".loc 123" directive.
2758 //
2759 // Yes, trying to understand this causes headaches for many people who read
2760 // this. Take an aspirin.
2761 insert_nop(109);
2762 }
2763
2764 static void
2765 pseudo_return_push(cbl_proc_t *procedure, tree return_addr)
2766 {
2767 // Put the return address onto the stack:
2768 //gg_suppress_location(true);
2769
2770 TRACE1
2771 {
2772 TRACE1_HEADER
2773 gg_printf("%s %p %p",
2774 gg_string_literal(procedure->label->name),
2775 gg_cast(SIZE_T, procedure->exit.addr),
2776 return_addr,
2777 NULL_TREE);
2778 TRACE1_END
2779 }
2780
2781 gg_call(VOID,
2782 "__gg__pseudo_return_push",
2783 procedure->exit.addr,
2784 return_addr,
2785 NULL_TREE);
2786
2787 //gg_suppress_location(false);
2788 }
2789
2790 static void
2791 pseudo_return_pop(cbl_proc_t *procedure)
2792 {
2793 //gg_suppress_location(true);
2794
2795 TRACE1
2796 {
2797 TRACE1_HEADER
2798 gg_printf("%s comparing proc_exit %p to global_exit %p -- ",
2799 gg_string_literal(procedure->label->name),
2800 gg_cast(SIZE_T, procedure->exit.addr),
2801 var_decl_exit_address,
2802 NULL_TREE);
2803 }
2804
2805 token_location_override(current_location_minus_one());
2806 IF( var_decl_exit_address, eq_op, procedure->exit.addr )
2807 {
2808 TRACE1
2809 {
2810 TRACE1_TEXT("Returning")
2811 }
2812 // The top of the stack is us!
2813
2814 // Pick up the return address from the pseudo_return stack:
2815 token_location_override(current_location_minus_one());
2816 gg_assign(current_function->void_star_temp,
2817 gg_call_expr( VOID_P,
2818 "__gg__pseudo_return_pop",
2819 NULL_TREE));
2820 // And do the return:
2821 token_location_override(current_location_minus_one());
2822 gg_goto(current_function->void_star_temp);
2823 }
2824 ELSE
2825 {
2826 TRACE1
2827 {
2828 TRACE1_TEXT("No match")
2829 }
2830 ENDIF
2831 }
2832 TRACE1
2833 {
2834 TRACE1_END
2835 }
2836 //gg_suppress_location(false);
2837 }
2838
2839 static void
2840 leave_procedure(struct cbl_proc_t *procedure, bool /*section*/)
2841 {
2842 if(procedure)
2843 {
2844 // fprintf(stderr, "LeavingProcedure: (%p) %s %p %p %p %p %p %p\n",
2845 // procedure,
2846 // procedure->name,
2847 // procedure->top.go_to,
2848 // procedure->top.label,
2849 // procedure->exit.go_to,
2850 // procedure->exit.label,
2851 // procedure->bottom.go_to,
2852 // procedure->bottom.label);
2853 // Procedure can be null, for example at the beginning of a
2854 // new program, or after somebody else has cleared it out.
2855
2856 gg_append_statement(procedure->exit.label);
2857
2858 char *psz;
2859 psz = xasprintf("_procret." HOST_SIZE_T_PRINT_DEC ":",
2860 (fmt_size_t)symbol_label_id(procedure->label));
2861 token_location_override(current_location_minus_one());
2862 gg_insert_into_assembler(psz);
2863 free(psz);
2864 pseudo_return_pop(procedure);
2865 gg_append_statement(procedure->bottom.label);
2866 }
2867 }
2868
2869 static void
2870 leave_section_internal()
2871 {
2872 Analyze();
2873 SHOW_PARSE
2874 {
2875 if(gg_trans_unit.function_stack.size() && current_function && current_function->current_section)
2876 {
2877 SHOW_PARSE_HEADER
2878 SHOW_PARSE_TEXT(" ")
2879 SHOW_PARSE_TEXT(current_function->current_section->label->name)
2880 SHOW_PARSE_END
2881 }
2882 }
2883
2884 if( current_function->current_section )
2885 {
2886 // gg_printf( "Leaving section %s\n",
2887 // build_string_literal( strlen(current_function->current_section->label->name)+1, current_function->current_section->label->name),
2888 // NULL_TREE);
2889 TRACE1
2890 {
2891 TRACE1_HEADER
2892 TRACE1_TEXT_ABC("\"", current_function->current_section->label->name, "\"");
2893 TRACE1_END
2894 }
2895 leave_procedure(current_function->current_section, true);
2896
2897 current_function->current_section = NULL;
2898 }
2899 else
2900 {
2901 //gg_printf("Somebody is leaving a section twice\n", NULL_TREE);
2902 }
2903 }
2904
2905 void
2906 parser_leave_section( struct cbl_label_t */*label*/ ) {}
2907
2908 static void
2909 leave_paragraph_impl()
2910 {
2911 Analyze();
2912 SHOW_PARSE
2913 {
2914 if(gg_trans_unit.function_stack.size() && current_function && current_function->current_paragraph)
2915 {
2916 SHOW_PARSE_HEADER
2917 SHOW_PARSE_TEXT(" ")
2918 SHOW_PARSE_TEXT(current_function->current_paragraph->label->name)
2919 SHOW_PARSE_END
2920 }
2921 }
2922
2923 if( current_function->current_paragraph )
2924 {
2925 // gg_printf( "Leaving paragraph %s\n",
2926 // build_string_literal( strlen(current_function->current_paragraph->label->name)+1, current_function->current_paragraph->label->name),
2927 // NULL_TREE);
2928 TRACE1
2929 {
2930 TRACE1_HEADER
2931 TRACE1_TEXT_ABC("\"", current_function->current_paragraph->label->name, "\"");
2932 TRACE1_END
2933 }
2934 leave_procedure(current_function->current_paragraph, false);
2935 current_function->current_paragraph = NULL;
2936 }
2937 else
2938 {
2939 //gg_printf("Somebody is leaving a paragraph twice\n", NULL_TREE);
2940 }
2941 }
2942
2943 void parser_leave_paragraph( cbl_label_t * ) {}
2944 static inline void leave_paragraph_internal() { leave_paragraph_impl(); }
2945
2946 static struct cbl_proc_t *
2947 find_procedure(cbl_label_t *label)
2948 {
2949 // SHOW_PARSE
2950 // {
2951 // SHOW_PARSE_HEADER
2952 // SHOW_PARSE_LABEL(" ", label)
2953 // SHOW_PARSE_TEXT("\n");
2954 // }
2955
2956 cbl_proc_t *retval = label->structs.proc;
2957
2958 // We have to cope with an oddball circumstance. When label->entered is
2959 // greater than zero, it means that a paragraph with this label has been
2960 // entered and left already. This means that a paragraph name has been
2961 // defined more than once. Had it been referenced with a GOTO or PERFORM,
2962 // that would have been a syntax error.
2963 //
2964 //
2965 // In this case, we need to replace the existing cbl_proc_t structure. We
2966 // will be laying down labels for this second (or more) instance of
2967 // parser_enter_paragraph, and we must create different labels.
2968
2969 if( !retval )
2970 {
2971 static int counter=1;
2972
2973 // This is a new section or paragraph; we need to create its values:
2974 retval = static_cast<struct cbl_proc_t *>
2975 (xmalloc(sizeof(struct cbl_proc_t)));
2976 gcc_assert(retval);
2977 retval->label = label;
2978
2979 gg_create_goto_pair(&retval->top.go_to,
2980 &retval->top.label,
2981 &retval->top.addr,
2982 &retval->top.decl);
2983 gg_create_goto_pair(&retval->exit.go_to,
2984 &retval->exit.label,
2985 &retval->exit.addr
2986 );
2987 gg_create_goto_pair(&retval->bottom.go_to,
2988 &retval->bottom.label,
2989 &retval->bottom.addr
2990 );
2991
2992 // fprintf(stderr, "NewProcedure: (%p) %s %p %p %p %p %p %p\n",
2993 // retval,
2994 // retval->name,
2995 // retval->top.go_to,
2996 // retval->top.label,
2997 // retval->exit.go_to,
2998 // retval->exit.label,
2999 // retval->bottom.go_to,
3000 // retval->bottom.label);
3001
3002 // If this procedure is a paragraph, and it becomes the target of
3003 // an ALTER statement, alter_location will be used to make that change
3004 char *psz = xasprintf("_%s_alter_loc_%d", label->name, counter);
3005 retval->alter_location = gg_define_void_star(psz, vs_static);
3006 free(psz);
3007 DECL_INITIAL(retval->alter_location) = null_pointer_node;
3008
3009 counter +=1 ;
3010
3011 label->structs.proc = retval;
3012 }
3013
3014 return retval;
3015 }
3016
3017 void
3018 parser_enter_section(cbl_label_t *label)
3019 {
3020 Analyze();
3021 // Do the leaving before the SHOW_PARSE; it makes the output more sensible
3022 // A new section ends the current paragraph:
3023 leave_paragraph_internal();
3024
3025 // And the current section:
3026 leave_section_internal();
3027
3028 SHOW_PARSE
3029 {
3030 SHOW_PARSE_HEADER
3031 SHOW_PARSE_LABEL(" ", label)
3032 SHOW_PARSE_INDENT
3033 linemap_dump_location( line_table, current_token_location(), stderr );
3034 SHOW_PARSE_END
3035 }
3036
3037 CHECK_LABEL(label);
3038
3039 // This NOP is needed to give GDB a line number for the entry point of
3040 // paragraphs
3041 insert_nop(101);
3042
3043 struct cbl_proc_t *procedure = find_procedure(label);
3044 gg_append_statement(procedure->top.label);
3045 section_label(procedure);
3046 current_function->current_section = procedure;
3047
3048 TRACE1
3049 {
3050 TRACE1_HEADER
3051 TRACE1_LABEL("\"", label, "\"")
3052 TRACE1_END
3053 }
3054 }
3055
3056 void
3057 parser_enter_paragraph(cbl_label_t *label)
3058 {
3059 Analyze();
3060 // Do the leaving before the SHOW_PARSE; the output makes more sense that way
3061 // A new paragraph ends the current paragraph:
3062 leave_paragraph_internal();
3063
3064 SHOW_PARSE
3065 {
3066 SHOW_PARSE_HEADER
3067 SHOW_PARSE_LABEL(" ", label)
3068 SHOW_PARSE_INDENT
3069 linemap_dump_location( line_table, current_token_location(), stderr );
3070 SHOW_PARSE_END
3071 }
3072
3073 CHECK_LABEL(label);
3074
3075 struct cbl_proc_t *procedure = find_procedure(label);
3076 gg_append_statement(procedure->top.label);
3077 paragraph_label(procedure);
3078 current_function->current_paragraph = procedure;
3079
3080 TRACE1
3081 {
3082 TRACE1_HEADER
3083 TRACE1_LABEL("\"", label, "\"")
3084 TRACE1_END
3085 }
3086 }
3087
3088 void
3089 parser_exit_section(void)
3090 {
3091 Analyze();
3092 SHOW_PARSE
3093 {
3094 SHOW_PARSE_HEADER
3095 SHOW_PARSE_END
3096 }
3097 TRACE1
3098 {
3099 TRACE1_HEADER
3100 TRACE1_TEXT_ABC("\"", current_function->current_section->label->name, "\"")
3101 TRACE1_END
3102 }
3103 gg_append_statement(current_function->current_section->exit.go_to);
3104 }
3105
3106 void
3107 parser_exit_paragraph(void)
3108 {
3109 Analyze();
3110 SHOW_PARSE
3111 {
3112 SHOW_PARSE_HEADER
3113 SHOW_PARSE_END
3114 }
3115 TRACE1
3116 {
3117 TRACE1_HEADER
3118 TRACE1_TEXT_ABC("\"", current_function->current_paragraph->label->name, "\"")
3119 TRACE1_END
3120 }
3121 gg_append_statement(current_function->current_paragraph->exit.go_to);
3122 }
3123
3124 void
3125 parser_exit_perform(struct cbl_perform_tgt_t *tgt, bool cycle)
3126 {
3127 Analyze();
3128 SHOW_PARSE
3129 {
3130 SHOW_PARSE_HEADER
3131 SHOW_PARSE_END
3132 }
3133 TRACE1
3134 {
3135 TRACE1_HEADER
3136 TRACE1_END
3137 }
3138 if(cycle)
3139 {
3140 gg_append_statement(tgt->addresses.testA.go_to);
3141 }
3142 else
3143 {
3144 gg_append_statement(tgt->addresses.exit.go_to);
3145 }
3146 }
3147
3148 void
3149 parser_alter( cbl_perform_tgt_t *tgt )
3150 {
3151 Analyze();
3152 SHOW_PARSE
3153 {
3154 SHOW_PARSE_HEADER
3155 SHOW_PARSE_END
3156 }
3157 TRACE1
3158 {
3159 TRACE1_HEADER
3160 TRACE1_END
3161 }
3162 cbl_label_t *altered = tgt->from();
3163 cbl_label_t *proceed_to = tgt->to();
3164
3165 struct cbl_proc_t *altered_proc = find_procedure(altered);
3166 struct cbl_proc_t *proceed_to_proc = find_procedure(proceed_to);
3167
3168 gg_assign( altered_proc->alter_location,
3169 proceed_to_proc->top.addr);
3170 }
3171
3172 void
3173 parser_goto( cbl_refer_t value_ref, size_t narg, cbl_label_t * const labels[] )
3174 {
3175 // This is part of the Terrible Trio of parser_perform, parser_goto and
3176 // parser_enter_[procedure]. parser_goto has an easier time of it than
3177 // the other two, because it just has to jump from here to the entry point
3178 // of the paragraph [or section]
3179 Analyze();
3180
3181 SHOW_PARSE
3182 {
3183 SHOW_PARSE_HEADER
3184 for(size_t i=0; i<narg; i++)
3185 {
3186 SHOW_PARSE_TEXT(" ");
3187 SHOW_PARSE_TEXT(labels[i]->name);
3188 }
3189 SHOW_PARSE_END
3190 }
3191 TRACE1
3192 {
3193 TRACE1_HEADER
3194 for(size_t i=0; i<narg; i++)
3195 {
3196 TRACE1_TEXT(labels[i]->name);
3197 TRACE1_TEXT(" ");
3198 }
3199 TRACE1_END
3200 }
3201
3202 gcc_assert(narg >= 1);
3203
3204 // This is a computed GOTO. It might have only one element, which is
3205 // an ordinary GOTO without a DEPENDING ON clause. We create that table
3206 // anyway, because in the case of an ALTER statement, we will be replacing
3207 // that sole element with the PROCEED TO element.
3208
3209 // We need to create a static array of pointers to locations:
3210 static int comp_gotos = 1;
3211 char *psz = xasprintf("_comp_goto_%d", comp_gotos++);
3212 tree array_of_pointers_type = build_array_type_nelts(VOID_P, narg);
3213 tree array_of_pointers = gg_define_variable(array_of_pointers_type, psz, vs_static);
3214 free(psz);
3215
3216 // We have the array. Now we need to build the constructor for it
3217 tree constr = make_node(CONSTRUCTOR);
3218 TREE_TYPE(constr) = array_of_pointers_type;
3219 TREE_STATIC(constr) = 1;
3220 TREE_CONSTANT(constr) = 1;
3221
3222 for(size_t i=0; i<narg; i++)
3223 {
3224 CHECK_LABEL(labels[i]);
3225 struct cbl_proc_t *procedure = find_procedure(labels[i]);
3226 CONSTRUCTOR_APPEND_ELT( CONSTRUCTOR_ELTS(constr),
3227 build_int_cst_type(SIZE_T, i),
3228 procedure->top.addr );
3229 }
3230 DECL_INITIAL(array_of_pointers) = constr;
3231
3232 // We need to pick up the value argument as an INT:
3233 tree value = gg_define_int();
3234
3235 if( value_ref.field )
3236 {
3237 get_binary_value( value,
3238 NULL,
3239 value_ref.field,
3240 refer_offset(value_ref));
3241 // Convert it from one-based to zero-based:
3242 gg_decrement(value);
3243 // Check to see if the value is in the range 0...narg-1:
3244 IF( value, ge_op, integer_zero_node)
3245 {
3246 IF( value, lt_op, build_int_cst_type(INT, narg) )
3247 {
3248 // It is in the valid range, so we can do the goto:
3249 Analyzer.ExitMessage();
3250 gg_goto(gg_array_value(array_of_pointers, value));
3251 }
3252 ELSE
3253 {
3254 // Otherwise, just fall through
3255 }
3256 ENDIF
3257 }
3258 ELSE
3259 ENDIF
3260 }
3261 else
3262 {
3263 // This is a simple GOTO. Because it is a simple GO TO, there is the
3264 // possibility that this paragraph was the target of an ALTER statement.
3265 IF( current_function->current_paragraph->alter_location, ne_op, null_pointer_node )
3266 {
3267 // Somebody did an ALTER statement before we got here
3268 gg_assign(current_function->void_star_temp, current_function->current_paragraph->alter_location);
3269 }
3270 ELSE
3271 {
3272 // This paragraph wasn't the target of an ALTER:
3273 gg_assign(current_function->void_star_temp, gg_array_value(array_of_pointers, 0));
3274 }
3275 ENDIF
3276 Analyzer.ExitMessage();
3277 gg_goto(current_function->void_star_temp);
3278 }
3279 return;
3280 }
3281
3282 void
3283 parser_perform(cbl_label_t *label, bool suppress_nexting)
3284 {
3285 Analyze();
3286 SHOW_PARSE
3287 {
3288 SHOW_PARSE_HEADER
3289 SHOW_PARSE_LABEL(" ", label)
3290 char ach[32];
3291 sprintf(ach, " label is at %p", static_cast<void*>(label));
3292 SHOW_PARSE_TEXT(ach)
3293 if( label )
3294 {
3295 sprintf(ach,
3296 " label->proc is %p",
3297 static_cast<void*>(label->structs.proc));
3298 }
3299 SHOW_PARSE_TEXT(ach)
3300 SHOW_PARSE_END
3301 }
3302
3303 TRACE1
3304 {
3305 TRACE1_HEADER
3306 TRACE1_LABEL("", label, "")
3307 TRACE1_END
3308 }
3309
3310 CHECK_LABEL(label);
3311 label->used = yylineno;
3312
3313 struct cbl_proc_t *procedure = find_procedure(label);
3314
3315 // We need to create the unnamed return address that we
3316 // will instantiate right after the goto:
3317 tree return_address_decl = build_decl( UNKNOWN_LOCATION,
3318 LABEL_DECL,
3319 NULL_TREE,
3320 void_type_node);
3321 DECL_CONTEXT(return_address_decl) = current_function->function_decl;
3322 TREE_USED(return_address_decl) = 1;
3323
3324 tree return_label_expr = build1(LABEL_EXPR,
3325 void_type_node,
3326 return_address_decl);
3327 tree return_addr = gg_get_address_of(return_address_decl);
3328
3329 // cbl_parser_mod *parser_mod = new cbl_parser_mod;
3330
3331 // Put the return address onto the pseudo-return stack
3332 pseudo_return_push(procedure, return_addr);
3333
3334 // Create the code that will launch the paragraph
3335 // The following comment is, believe it or not, necessary. The insertion
3336 // includes a line number insertion that's needed because when the goto/label
3337 // pairs were created, the locations of the goto instruction and the label
3338 // were not known.
3339
3340 const char *para_name = nullptr;
3341 const char *sect_name = nullptr;
3342 const char *program_name = current_function->our_unmangled_name;
3343 size_t deconflictor = symbol_label_id(label);
3344
3345 char ach[256];
3346 if( label->type == LblParagraph )
3347 {
3348 const cbl_label_t *sec_label = cbl_label_of(symbol_at(label->parent));
3349 para_name = label->name;
3350 sect_name = sec_label->name;
3351 sprintf(ach,
3352 "%s PERFORM %s of %s of %s (" HOST_SIZE_T_PRINT_DEC ")",
3353 ASM_COMMENT_START,
3354 para_name,
3355 sect_name,
3356 program_name,
3357 (fmt_size_t)deconflictor);
3358
3359 gg_insert_into_assembler(ach);
3360 }
3361 else
3362 {
3363 sect_name = label->name;
3364 sprintf(ach,
3365 "%s PERFORM %s of %s (" HOST_SIZE_T_PRINT_DEC ")",
3366 ASM_COMMENT_START,
3367 sect_name,
3368 program_name,
3369 (fmt_size_t)deconflictor);
3370 gg_insert_into_assembler(ach);
3371 }
3372
3373 if( !suppress_nexting )
3374 {
3375 sprintf(ach,
3376 "_proccall." HOST_SIZE_T_PRINT_DEC ".%d:",
3377 (fmt_size_t)symbol_label_id(label),
3378 call_counter++);
3379 gg_insert_into_assembler( ach );
3380 }
3381
3382 // We do the indirect jump in order to prevent the compiler from complaining
3383 // in the case where we are performing a USE GLOBAL DECLARATIVE. Without the
3384 // indirection, the compiler isn't able to handle the case where we are
3385 // jumping to a location in our parent program-id; it can't find a matching
3386 // local symbol, and crashes.
3387 gg_goto(procedure->top.addr);
3388
3389 // And create the return address label:
3390 gg_append_statement(return_label_expr);
3391 TRACE1
3392 {
3393 TRACE1_HEADER
3394 TRACE1_LABEL("back_from_performing ", label, "")
3395 TRACE1_END
3396 }
3397 }
3398
3399 void
3400 parser_perform_times( cbl_label_t *proc_1, cbl_refer_t count )
3401 {
3402 Analyze();
3403 SHOW_PARSE
3404 {
3405 SHOW_PARSE_HEADER
3406 SHOW_PARSE_LABEL(" ", proc_1)
3407 SHOW_PARSE_REF(" ", count)
3408 SHOW_PARSE_TEXT(" TIMES")
3409 char ach[32];
3410 sprintf(ach, " proc_1 is at %p", static_cast<void*>(proc_1));
3411 SHOW_PARSE_TEXT(ach)
3412 sprintf(ach, " proc_1->proc is %p", static_cast<void*>(proc_1->structs.proc));
3413 SHOW_PARSE_TEXT(ach)
3414 SHOW_PARSE_END
3415 }
3416 TRACE1
3417 {
3418 TRACE1_HEADER
3419 TRACE1_END
3420 }
3421
3422 char ach[256];
3423 size_t our_pseudo_label = pseudo_label++;
3424 sprintf(ach,
3425 "_proccallb." HOST_SIZE_T_PRINT_DEC ":",
3426 (fmt_size_t)our_pseudo_label);
3427 gg_insert_into_assembler( ach );
3428
3429 tree counter = gg_define_variable(LONG);
3430
3431 // Get the count:
3432 get_binary_value( counter,
3433 NULL,
3434 count.field,
3435 refer_offset(count));
3436
3437 // Make sure the initial count is valid:
3438 WHILE( counter, gt_op, gg_cast(LONG, integer_zero_node) )
3439 {
3440 static const bool suppress_nexting = true;
3441 parser_perform(proc_1, suppress_nexting);
3442 gg_decrement(counter);
3443 }
3444 WEND
3445
3446 sprintf(ach,
3447 "_procretb." HOST_SIZE_T_PRINT_DEC ":",
3448 (fmt_size_t)our_pseudo_label);
3449 token_location_override(current_location_minus_one());
3450 gg_insert_into_assembler(ach);
3451 }
3452
3453 static void
3454 internal_perform_through( cbl_label_t *proc_1,
3455 cbl_label_t *proc_2,
3456 bool suppress_nexting )
3457 {
3458 Analyze();
3459 SHOW_PARSE
3460 {
3461 SHOW_PARSE_HEADER
3462 SHOW_PARSE_LABEL(" ", proc_1);
3463 char ach[32];
3464 sprintf(ach, " proc_1 is at %p", static_cast<void*>(proc_1));
3465 SHOW_PARSE_TEXT(ach)
3466 if( proc_1 )
3467 {
3468 sprintf(ach,
3469 " proc_1->proc is %p",
3470 static_cast<void*>(proc_1->structs.proc));
3471 }
3472 SHOW_PARSE_TEXT(ach)
3473 if( proc_2 )
3474 {
3475 SHOW_PARSE_INDENT
3476 SHOW_PARSE_LABEL_OK("", proc_2);
3477 sprintf(ach, " proc_2 is at %p", static_cast<void*>(proc_2));
3478 SHOW_PARSE_TEXT(ach)
3479 sprintf(ach, " proc_2->proc is %p", static_cast<void*>(proc_2->structs.proc));
3480 SHOW_PARSE_TEXT(ach)
3481 }
3482 SHOW_PARSE_END
3483 }
3484 TRACE1
3485 {
3486 TRACE1_HEADER
3487 TRACE1_END
3488 }
3489
3490 CHECK_LABEL(proc_1);
3491
3492 if( !proc_2 )
3493 {
3494 parser_perform(proc_1, suppress_nexting);
3495 return;
3496 }
3497
3498 struct cbl_proc_t *proc1 = find_procedure(proc_1);
3499 struct cbl_proc_t *proc2 = find_procedure(proc_2);
3500
3501 // We need to create the unnamed return address that we
3502 // will instantiate right after the goto:
3503 tree return_address_decl = build_decl( UNKNOWN_LOCATION,
3504 LABEL_DECL,
3505 NULL_TREE,
3506 void_type_node);
3507 DECL_CONTEXT(return_address_decl) = current_function->function_decl;
3508 TREE_USED(return_address_decl) = 1;
3509
3510 tree return_label_expr = build1(LABEL_EXPR,
3511 void_type_node,
3512 return_address_decl);
3513 tree return_addr = gg_get_address_of(return_address_decl);
3514
3515 //cbl_parser_mod *parser_mod_proc1 = new cbl_parser_mod;
3516 //cbl_parser_mod *parser_mod_proc2 = new cbl_parser_mod;
3517
3518 // Put the return address of the second procedure onto the stack:
3519 pseudo_return_push(proc2, return_addr);
3520
3521 // Create the code that will launch the first procedure
3522 gg_insert_into_assemblerf("%s PERFORM %s THROUGH %s",
3523 ASM_COMMENT_START, proc_1->name, proc_2->name);
3524
3525 if( !suppress_nexting )
3526 {
3527 char ach[256];
3528 sprintf(ach,
3529 "_proccall." HOST_SIZE_T_PRINT_DEC ".%d:",
3530 (fmt_size_t)symbol_label_id(proc_2),
3531 call_counter++);
3532 gg_insert_into_assembler(ach);
3533 }
3534
3535 gg_append_statement(proc1->top.go_to);
3536
3537 // And create the return address label:
3538 gg_append_statement(return_label_expr);
3539 }
3540
3541 static void
3542 internal_perform_through_times( cbl_label_t *proc_1,
3543 cbl_label_t *proc_2,
3544 cbl_refer_t &count)
3545 {
3546 Analyze();
3547 SHOW_PARSE
3548 {
3549 SHOW_PARSE_HEADER
3550 SHOW_PARSE_LABEL(" ", proc_1);
3551 char ach[32];
3552 sprintf(ach, " proc_1 is at %p", static_cast<void*>(proc_1));
3553 SHOW_PARSE_TEXT(ach)
3554 if( proc_1 )
3555 {
3556 sprintf(ach,
3557 " proc_1->proc is %p",
3558 static_cast<void*>(proc_1->structs.proc));
3559 }
3560 SHOW_PARSE_TEXT(ach)
3561 if( proc_2 )
3562 {
3563 SHOW_PARSE_INDENT
3564 SHOW_PARSE_LABEL_OK("", proc_2);
3565 sprintf(ach, " proc_2 is at %p", static_cast<void*>(proc_2));
3566 SHOW_PARSE_TEXT(ach)
3567 sprintf(ach, " proc_2->proc is %p", static_cast<void*>(proc_2->structs.proc));
3568 SHOW_PARSE_TEXT(ach)
3569 }
3570 SHOW_PARSE_REF(" ", count);
3571 SHOW_PARSE_TEXT(" TIMES");
3572 SHOW_PARSE_END
3573 }
3574 TRACE1
3575 {
3576 TRACE1_HEADER
3577 TRACE1_END
3578 }
3579
3580 size_t our_pseudo_label = pseudo_label++;
3581
3582 char ach[256];
3583 sprintf(ach,
3584 "_proccallb." HOST_SIZE_T_PRINT_DEC ":",
3585 (fmt_size_t)our_pseudo_label);
3586 gg_insert_into_assembler( ach );
3587
3588 tree counter = gg_define_variable(LONG);
3589 get_binary_value( counter,
3590 NULL,
3591 count.field,
3592 refer_offset(count));
3593 WHILE( counter, gt_op, gg_cast(LONG, integer_zero_node) )
3594 {
3595 internal_perform_through(proc_1, proc_2, true); // true means suppress_nexting
3596 gg_decrement(counter);
3597 }
3598 WEND
3599
3600 sprintf(ach,
3601 "_procretb." HOST_SIZE_T_PRINT_DEC ":",
3602 (fmt_size_t)our_pseudo_label);
3603 token_location_override(current_location_minus_one());
3604 gg_insert_into_assembler( ach );
3605 }
3606
3607 void
3608 register_main_switch(const char *main_string)
3609 {
3610 char *mstr = xstrdup(main_string);
3611 char *p = strchr(mstr, ':');
3612 if( p )
3613 {
3614 *p = '\0';
3615 main_string = p+1;
3616 main_strings[mstr] = main_string;
3617 }
3618 else
3619 {
3620 main_strings[mstr] = "";
3621 }
3622 free(mstr);
3623 }
3624
3625 static int file_level = 0;
3626
3627 void
3628 parser_first_statement( int lineno )
3629 {
3630 // In the event that this routine is the one that main() calls to get the
3631 // execution ball rolling, we want the GDB "start" function to be able
3632 // to set a temporary breakpoint at this location. We get that rolling
3633 // here.
3634
3635 char ach[256];
3636
3637 SHOW_PARSE
3638 {
3639 SHOW_PARSE_HEADER
3640 sprintf(ach, " lineno is %d, suppression is %d", lineno, suppress_cobol_entry_point);
3641 SHOW_PARSE_TEXT(ach)
3642 SHOW_PARSE_END
3643 }
3644
3645 if( strcmp(current_function->our_name, ach_cobol_entry_point) == 0
3646 && !suppress_cobol_entry_point )
3647 {
3648 sprintf(ach,
3649 "%s:%d",
3650 current_filename.back().c_str(),
3651 lineno);
3652 *ach_cobol_entry_point = '\0';
3653 create_cblc_string_variable("_cobol_entry_point", ach);
3654
3655 SHOW_PARSE
3656 {
3657 SHOW_PARSE_INDENT
3658 char ach2[512];
3659 sprintf(ach2, "setting _cobol_entry_point to \"%s\"", ach);
3660 SHOW_PARSE_TEXT(ach2)
3661 SHOW_PARSE_END
3662 }
3663 }
3664
3665 if( !suppress_cobol_entry_point )
3666 {
3667 char achentry[128];
3668 sprintf(ach,
3669 "%s:%d",
3670 current_filename.back().c_str(),
3671 lineno);
3672
3673 sprintf(achentry, "_prog_entry_point_%s", current_function->our_name);
3674 create_cblc_string_variable(achentry, ach);
3675 }
3676 }
3677
3678 void
3679 parser_enter_file(const char *filename)
3680 {
3681 SHOW_PARSE
3682 {
3683 SHOW_PARSE_HEADER
3684 char *psz;
3685 psz = xasprintf(" entering level:%d %s", file_level+1, filename);
3686 SHOW_PARSE_TEXT(psz);
3687 free(psz);
3688 SHOW_PARSE_END
3689 }
3690
3691 current_filename.push_back(filename);
3692
3693 std::unordered_map<std::string, std::string>::const_iterator it
3694 = main_strings.find(filename);
3695
3696 if( it != main_strings.end() )
3697 {
3698 // There was a -main switch for this file.
3699 this_module_has_main = true;
3700 next_program_is_main = true;
3701
3702 const char *pname = it->second.c_str();
3703 if( pname && strlen(pname) )
3704 {
3705 main_entry_point = xstrdup(pname);
3706 }
3707 }
3708
3709 if( file_level == 0 )
3710 {
3711 // Build a translation_unit_decl:
3712 gg_build_translation_unit(filename);
3713 create_our_type_nodes();
3714 }
3715
3716 file_level += 1;
3717
3718 if( file_level == 1 )
3719 {
3720 // This table is used for "creating" the file-static named variables used in
3721 // the GENERIC we generate.
3722
3723 // Establish our variable declarations for global variables in libgcobol:
3724
3725 #define SET_VAR_DECL(A, B, C) \
3726 A = gg_declare_variable(B, C, NULL_TREE, vs_external_reference)
3727
3728 SET_VAR_DECL(var_decl_exception_code , INT , "__gg__exception_code");
3729 SET_VAR_DECL(var_decl_exception_file_status , INT , "__gg__exception_file_status");
3730 SET_VAR_DECL(var_decl_exception_file_name , CHAR_P , "__gg__exception_file_name");
3731 SET_VAR_DECL(var_decl_exception_statement , CHAR_P , "__gg__exception_statement");
3732 SET_VAR_DECL(var_decl_exception_source_file , CHAR_P , "__gg__exception_source_file");
3733 SET_VAR_DECL(var_decl_exception_line_number , INT , "__gg__exception_line_number");
3734 SET_VAR_DECL(var_decl_exception_program_id , CHAR_P , "__gg__exception_program_id");
3735 SET_VAR_DECL(var_decl_exception_section , CHAR_P , "__gg__exception_section");
3736 SET_VAR_DECL(var_decl_exception_paragraph , CHAR_P , "__gg__exception_paragraph");
3737
3738 SET_VAR_DECL(var_decl_default_compute_error , INT , "__gg__default_compute_error");
3739 SET_VAR_DECL(var_decl_rdigits , INT , "__gg__rdigits");
3740 SET_VAR_DECL(var_decl_unique_prog_id , SIZE_T , "__gg__unique_prog_id");
3741
3742 SET_VAR_DECL(var_decl_entry_location , VOID_P , "__gg__entry_pointer");
3743 SET_VAR_DECL(var_decl_exit_address , VOID_P , "__gg__exit_address");
3744
3745 SET_VAR_DECL(var_decl_call_parameter_signature , CHAR_P , "__gg__call_parameter_signature");
3746 SET_VAR_DECL(var_decl_call_parameter_count , INT , "__gg__call_parameter_count");
3747 SET_VAR_DECL(var_decl_call_parameter_lengths , build_array_type(SIZE_T, NULL),
3748 "__gg__call_parameter_lengths");
3749 SET_VAR_DECL(var_decl_return_code , SHORT , "__gg__data_return_code");
3750
3751 SET_VAR_DECL(var_decl_arithmetic_rounds_size , SIZE_T , "__gg__arithmetic_rounds_size");
3752 SET_VAR_DECL(var_decl_arithmetic_rounds , INT_P , "__gg__arithmetic_rounds");
3753 SET_VAR_DECL(var_decl_fourplet_flags_size , SIZE_T , "__gg__fourplet_flags_size");
3754 SET_VAR_DECL(var_decl_fourplet_flags , INT_P , "__gg__fourplet_flags");
3755
3756 SET_VAR_DECL(var_decl_treeplet_1f , cblc_field_pp_type_node , "__gg__treeplet_1f" );
3757 SET_VAR_DECL(var_decl_treeplet_1o , SIZE_T_P , "__gg__treeplet_1o" );
3758 SET_VAR_DECL(var_decl_treeplet_1s , SIZE_T_P , "__gg__treeplet_1s" );
3759 SET_VAR_DECL(var_decl_treeplet_2f , cblc_field_pp_type_node , "__gg__treeplet_2f" );
3760 SET_VAR_DECL(var_decl_treeplet_2o , SIZE_T_P , "__gg__treeplet_2o" );
3761 SET_VAR_DECL(var_decl_treeplet_2s , SIZE_T_P , "__gg__treeplet_2s" );
3762 SET_VAR_DECL(var_decl_treeplet_3f , cblc_field_pp_type_node , "__gg__treeplet_3f" );
3763 SET_VAR_DECL(var_decl_treeplet_3o , SIZE_T_P , "__gg__treeplet_3o" );
3764 SET_VAR_DECL(var_decl_treeplet_3s , SIZE_T_P , "__gg__treeplet_3s" );
3765 SET_VAR_DECL(var_decl_treeplet_4f , cblc_field_pp_type_node , "__gg__treeplet_4f" );
3766 SET_VAR_DECL(var_decl_treeplet_4o , SIZE_T_P , "__gg__treeplet_4o" );
3767 SET_VAR_DECL(var_decl_treeplet_4s , SIZE_T_P , "__gg__treeplet_4s" );
3768 SET_VAR_DECL(var_decl_nop , INT , "__gg__nop" );
3769 SET_VAR_DECL(var_decl_main_called , INT , "__gg__main_called" );
3770 }
3771 }
3772
3773 void
3774 parser_leave_file()
3775 {
3776 Analyze();
3777 SHOW_PARSE
3778 {
3779 SHOW_PARSE_HEADER
3780 char ach[256];
3781 sprintf(ach,
3782 "leaving level:%d %s",
3783 file_level,
3784 current_filename.back().c_str());
3785 SHOW_PARSE_TEXT(ach)
3786 SHOW_PARSE_END
3787 }
3788 file_level -= 1;
3789 current_filename.pop_back();
3790
3791 if( file_level == 0 )
3792 {
3793 // We are leaving the top-level file, which means this compilation is
3794 // done, done, done.
3795
3796 // There is, however, one thing left to do. If the command line says
3797 // that this module needs a main entry point, then this is where
3798 // we create a main() function. We build it at the end, so that all of
3799 // the .loc directives associated with it appear at the end of the
3800 // source code. We used to create the main() entry point at the beginning,
3801 // but that created confusion for GDB when trying to debug the generated
3802 // executable.
3803 if( main_entry_point )
3804 {
3805 next_program_is_main = false;
3806 build_main_that_calls_something(main_entry_point);
3807 free(main_entry_point);
3808 main_entry_point = NULL;
3809 }
3810
3811 gg_leaving_the_source_code_file();
3812 }
3813 }
3814
3815 void
3816 enter_program_common(const char *funcname, const char *funcname_)
3817 {
3818 // We arrive here when processing a PROGRAM-ID.
3819
3820 // At this point, we don't know how many formal parameters there are going
3821 // to be.
3822
3823 // We are going to create a function returning a 64-bit value, but it'll
3824 // have no parameters. We'll chain the parameters on in parser_division(),
3825 // when we process PROCEDURE DIVISION USING...
3826
3827 gg_define_function(COBOL_FUNCTION_RETURN_TYPE,
3828 funcname,
3829 funcname_,
3830 NULL_TREE);
3831
3832 current_function->first_time_through =
3833 gg_define_variable(INT,
3834 "_first_time_through",
3835 vs_static,
3836 integer_one_node);
3837
3838 gg_create_goto_pair(&current_function->skip_init_goto,
3839 &current_function->skip_init_label);
3840
3841 IF( current_function->first_time_through, eq_op, integer_zero_node )
3842 gg_append_statement(current_function->skip_init_goto);
3843 ELSE
3844 ENDIF
3845
3846 gg_assign(current_function->first_time_through, integer_zero_node);
3847
3848 // Establish variables that are function-wide in scope:
3849 current_function->void_star_temp = gg_define_void_star("_void_star_temp");
3850
3851 current_function->perform_exit_address
3852 = gg_define_void_star("_perform_exit_address");
3853
3854 // Make sure the following are null, because when we create the unnamed
3855 // default section, parser_enter_section will attempt to close them out. And
3856 // it's possible on the first go-through that they have garbage values.
3857
3858 current_function->current_section = NULL;
3859 current_function->current_paragraph = NULL;
3860
3861 // Text conversion must be initialized before the code generated by
3862 // parser_symbol_add runs.
3863
3864 // The text_conversion_override exists both in the library and in the compiler
3865
3866 __gg__set_internal_codeset(internal_codeset_is_ebcdic());
3867 gg_call(VOID,
3868 "__gg__set_internal_codeset",
3869 internal_codeset_is_ebcdic()
3870 ? integer_one_node : integer_zero_node,
3871 NULL_TREE);
3872
3873 __gg__text_conversion_override(td_default_e, cs_default_e);
3874 gg_call(VOID,
3875 "__gg__text_conversion_override",
3876 build_int_cst_type(INT, td_default_e),
3877 build_int_cst_type(INT, cs_default_e),
3878 NULL_TREE);
3879
3880 gg_call(VOID,
3881 "__gg__codeset_figurative_constants",
3882 NULL_TREE);
3883
3884 static int counter=1;
3885 char ach[32];
3886
3887 sprintf(ach, "_cf_fds_%d", counter);
3888 current_function->first_declarative_section
3889 = gg_define_variable(CHAR_P,
3890 ach,
3891 vs_static,
3892 null_pointer_node);
3893 sprintf(ach, "_cf_cbmc_%d", counter);
3894 current_function->called_by_main_counter = gg_define_variable(INT,
3895 ach,
3896 vs_static,
3897 integer_zero_node);
3898 counter += 1;
3899
3900 // Initialize the TRACE logic, which has to be done before the first TRACE1
3901 // invocation, but after there is a function to lay down GIMPLE code in.
3902
3903 // That is to say: Here. Multiple invocations of trace1_init are harmless.
3904 trace1_init();
3905 }
3906
3907 /* Creates a function for program-id 'funcname_'. Returns 1 when funcname_
3908 is "main" and the -main compiler switch is active for this moudle */
3909
3910 void
3911 parser_enter_program( const char *funcname_,
3912 bool is_function, // True for user-defined-function
3913 int *pretval)
3914 {
3915 *pretval = 0;
3916
3917 // The first thing we have to do is mangle this name. This is safe even
3918 // though the end result will be mangled again, because the mangler doesn't
3919 // change a mangled name.
3920
3921 char *mangled_name = cobol_name_mangler(funcname_);
3922
3923 size_t parent_index = current_program_index();
3924 char *funcname;
3925 if( parent_index )
3926 {
3927 // This is a nested function. Tack on the parent_index to the end of it.
3928 funcname = xasprintf( "%s." HOST_SIZE_T_PRINT_DEC,
3929 mangled_name,
3930 (fmt_size_t)parent_index);
3931 }
3932 else
3933 {
3934 // This is a top-level function; just use the straight mangled name
3935 funcname = xstrdup(mangled_name);
3936 }
3937 free(mangled_name);
3938
3939 SHOW_PARSE
3940 {
3941 SHOW_PARSE_HEADER
3942 SHOW_PARSE_TEXT(" ")
3943 SHOW_PARSE_TEXT(funcname)
3944 SHOW_PARSE_END
3945 }
3946
3947 if( !is_function && !parent_index )
3948 {
3949 // This is a top_level program-id, and not a function
3950 if( next_program_is_main )
3951 {
3952 // This is the first top-level program-id.
3953 next_program_is_main = false;
3954 if( !main_entry_point )
3955 {
3956 // Because no explicit main_entry_point was specified, this program-id,
3957 // the first in the file, becomes the target of the main() function
3958 // that will be created at parser_leave_file time.
3959 main_entry_point = xstrdup(funcname);
3960
3961 char *psz = cobol_name_mangler(main_entry_point);
3962 strncpy(ach_cobol_entry_point, psz, sizeof(ach_cobol_entry_point)-1);
3963 free(psz);
3964 }
3965 }
3966 }
3967
3968 if( strcmp(funcname_, "main") == 0 && this_module_has_main )
3969 {
3970 // setting 'retval' to 1 let's the caller know that we are being told
3971 // both to synthesize a main() entry point to duplicate GCC's default
3972 // behavior, and to create an explicit entry point named "main". This will
3973 // eventually result in a link error (because of the duplicated entry
3974 // points. The return value serves as an alert; it's up to the caller to
3975 // decide what to do.
3976 *pretval = 1;
3977 }
3978
3979 if( strcmp(funcname, "dubner") == 0)
3980 {
3981 // This should be enabled by an environment variable.
3982 // But for now I am being cutesy
3983 hijack_for_development(funcname);
3984 return;
3985 }
3986
3987 enter_program_common(funcname, funcname_);
3988 current_function->is_function = is_function;
3989
3990 TRACE1
3991 {
3992 TRACE1_HEADER
3993 TRACE1_TEXT("entered program \"")
3994 TRACE1_TEXT(funcname)
3995 TRACE1_TEXT("\"")
3996 TRACE1_END
3997 }
3998
3999 free(funcname);
4000 }
4001
4002 void
4003 parser_end_program(const char *prog_name )
4004 {
4005 if( gg_trans_unit.function_stack.size() )
4006 {
4007 // The body has been created by various parser calls. It's time
4008 // to wrap this sucker up!
4009
4010 // Ending the program ends the current paragraph and section:
4011 leave_paragraph_internal();
4012 leave_section_internal();
4013 }
4014
4015 SHOW_PARSE
4016 {
4017 SHOW_PARSE_HEADER
4018 TRACE1_TEXT_ABC("\"", prog_name, "\"")
4019 SHOW_PARSE_END
4020 }
4021 TRACE1
4022 {
4023 TRACE1_HEADER
4024 TRACE1_TEXT_ABC("\"", prog_name, "\"")
4025 TRACE1_END
4026 }
4027
4028 if( gg_trans_unit.function_stack.size() )
4029 {
4030 // The body has been created by various parser calls. It's time
4031 // to wrap this sucker up!
4032
4033 // Put in a harmless return in case there was no EXIT PROGRAM statement.
4034 // It's harmless because if it isn't needed, a return was already
4035 // executed, and this generated code will never be executed
4036 parser_exit( cbl_refer_t() );
4037
4038 // Tell the GCC compiler to do the GIMPLIFY thing.
4039 gg_finalize_function();
4040 }
4041 }
4042
4043 static void
4044 remove_p_from_picture(char *picture)
4045 {
4046 // At this point, attr has the scaled_e flag, and rdigits tells us
4047 // which way to scale. So, the P characters in picture are now
4048 // a liability.
4049
4050 char *rabbit = picture;
4051 char *fox = picture;
4052
4053 for(;;)
4054 {
4055 char ch = *rabbit++;
4056 if( ch == '\0' )
4057 {
4058 break;
4059 }
4060 if( ch == 'P' || ch == 'p' )
4061 {
4062 if( *rabbit == '(' )
4063 {
4064 while( *rabbit != ')' )
4065 {
4066 rabbit += 1;
4067 }
4068 rabbit += 1;
4069 // rabbit now points to one past the closing parenthesis
4070 }
4071 size_t to_move = strlen(rabbit);
4072 memmove(fox, rabbit, to_move+1); // +1 snags the '\0'
4073 rabbit = fox;
4074 }
4075 else
4076 {
4077 fox += 1;
4078 }
4079 }
4080 }
4081
4082 static tree vti_array;
4083 static tree vti_constructor;
4084 static int vti_list_size;
4085 static int vti_next_variable;
4086
4087 void
4088 parser_init_list_size(int count_of_variables)
4089 {
4090 if( mode_syntax_only() ) return;
4091
4092 vti_list_size = count_of_variables;
4093 char ach[48];
4094 sprintf(ach,
4095 "..variables_to_init_" HOST_SIZE_T_PRINT_DEC,
4096 (fmt_size_t)current_function->our_symbol_table_index);
4097 tree array_of_variables_type = build_array_type_nelts(VOID_P,
4098 count_of_variables+1);
4099 vti_array = gg_define_variable( array_of_variables_type,
4100 ach,
4101 vs_file_static);
4102 vti_constructor = make_node(CONSTRUCTOR);
4103 TREE_TYPE(vti_constructor) = array_of_variables_type;
4104 TREE_STATIC(vti_constructor) = 1;
4105 TREE_CONSTANT(vti_constructor) = 1;
4106 vti_next_variable = 0;
4107 }
4108
4109 void
4110 parser_init_list_element(cbl_field_t *field)
4111 {
4112 if( mode_syntax_only() ) return;
4113
4114 gcc_assert(vti_next_variable < vti_list_size);
4115 CONSTRUCTOR_APPEND_ELT( CONSTRUCTOR_ELTS(vti_constructor),
4116 build_int_cst_type(SIZE_T, vti_next_variable++),
4117 gg_get_address_of(field->var_decl_node) );
4118 if( vti_next_variable == vti_list_size)
4119 {
4120 CONSTRUCTOR_APPEND_ELT( CONSTRUCTOR_ELTS(vti_constructor),
4121 build_int_cst_type(SIZE_T, vti_next_variable++),
4122 null_pointer_node );
4123 DECL_INITIAL(vti_array) = vti_constructor;
4124 }
4125 }
4126
4127 void
4128 parser_init_list()
4129 {
4130 if( mode_syntax_only() ) return;
4131
4132 char ach[48];
4133 sprintf(ach,
4134 "..variables_to_init_" HOST_SIZE_T_PRINT_DEC,
4135 (fmt_size_t)current_function->our_symbol_table_index);
4136 tree array = gg_trans_unit_var_decl(ach);
4137 gg_call(VOID,
4138 "__gg__variables_to_init",
4139 gg_get_address_of(array),
4140 wsclear() ? gg_string_literal(wsclear()) : null_pointer_node,
4141 NULL_TREE);
4142 }
4143
4144 static void
4145 psa_FldLiteralN(struct cbl_field_t *field )
4146 {
4147 Analyze();
4148 SHOW_PARSE
4149 {
4150 SHOW_PARSE_HEADER
4151 SHOW_PARSE_FIELD(" ", field)
4152 SHOW_PARSE_END
4153 }
4154 // We are constructing a completely static constant structure, based on the
4155 // text string in .initial
4156
4157 CHECK_FIELD(field);
4158
4159 FIXED_WIDE_INT(128) value = 0;
4160
4161 do
4162 {
4163 // This is a false do{}while, to isolate the variables:
4164
4165 // We need to convert data.initial to an FIXED_WIDE_INT(128) value
4166 char *p = const_cast<char *>(field->data.initial);
4167 int sign = 1;
4168 if( *p == '-' )
4169 {
4170 field->attr |= signable_e;
4171 sign = -1;
4172 p += 1;
4173 }
4174 else if( *p == '+' )
4175 {
4176 // We set it signable so that the instruction DISPLAY +1
4177 // actually outputs "+1"
4178 field->attr |= signable_e;
4179 p += 1;
4180 }
4181
4182 // We need to be able to handle
4183 // 123
4184 // 123.456
4185 // 123E<exp>
4186 // 123.456E<exp>
4187 // where <exp> can be N, +N and -N
4188 //
4189 // Oh, yeah, and we're talking handling up to 32 digits, or more, so using
4190 // library routines is off the table.
4191
4192 int digits = 0;
4193 int rdigits = 0;
4194 int rdigit_delta = 0;
4195 int exponent = 0;
4196
4197 const char *exp = strchr(p, 'E');
4198 if( !exp )
4199 {
4200 exp = strchr(p, 'e');
4201 }
4202 if(exp)
4203 {
4204 exponent = atoi(exp+1);
4205 }
4206
4207 // We can now calculate the value, and the number of digits and rdigits.
4208
4209 // We count up leading zeroes as part of the attr->digits calculation.
4210 // It turns out that certain comparisons need to know the number of digits,
4211 // because "IF "2" EQUAL 002" is false, while "IF "2" EQUAL 2" is true. So,
4212 // we need to count up leading zeroes.
4213
4214 for(;;)
4215 {
4216 char ch = *p++;
4217 if( ch == symbol_decimal_point() )
4218 {
4219 rdigit_delta = 1;
4220 continue;
4221 }
4222 if( ch < '0' || ch > '9' )
4223 {
4224 break;
4225 }
4226 digits += 1;
4227 rdigits += rdigit_delta;
4228 value *= 10;
4229 value += ch - '0';
4230 }
4231
4232 if( exponent < 0 )
4233 {
4234 rdigits += -exponent;
4235 }
4236 else
4237 {
4238 while(exponent--)
4239 {
4240 if(rdigits)
4241 {
4242 rdigits -= 1;
4243 }
4244 else
4245 {
4246 digits += 1;
4247 value *= 10;
4248 }
4249 }
4250 }
4251
4252 if(digits < rdigits)
4253 {
4254 digits = rdigits;
4255 }
4256 field->data.digits = digits;
4257 field->data.rdigits = rdigits;
4258
4259 // We now need to calculate the capacity.
4260
4261 unsigned int min_prec = wi::min_precision(value, UNSIGNED);
4262 int capacity;
4263 if( min_prec > 64 )
4264 {
4265 // Bytes 15 through 8 are non-zero
4266 capacity = 16;
4267 }
4268 else if( min_prec > 32 )
4269 {
4270 // Bytes 7 through 4 are non-zero
4271 capacity = 8;
4272 }
4273 else if( min_prec > 16 )
4274 {
4275 // Bytes 3 and 2
4276 capacity = 4;
4277 }
4278 else if( min_prec > 8 )
4279 {
4280 // Byte 1 is non-zero
4281 capacity = 2;
4282 }
4283 else
4284 {
4285 // The value is zero through 0xFF
4286 capacity = 1;
4287 }
4288
4289 value *= sign;
4290
4291 // One last adjustment. The number is signable, so the binary value
4292 // is going to be treated as twos complement. That means that the highest
4293 // bit has to be 1 for negative signable numbers, and 0 for positive. If
4294 // necessary, adjust capacity up by one byte so that the variable fits:
4295
4296 if( capacity < 16 && (field->attr & signable_e) )
4297 {
4298 FIXED_WIDE_INT(128) mask
4299 = wi::set_bit_in_zero<FIXED_WIDE_INT(128)>(capacity * 8 - 1);
4300 if( wi::neg_p (value) && (value & mask) == 0 )
4301 {
4302 capacity *= 2;
4303 }
4304 else if( !wi::neg_p (value) && (value & mask) != 0 )
4305 {
4306 capacity *= 2;
4307 }
4308 }
4309 field->data.capacity = capacity;
4310
4311 }while(0);
4312
4313 char base_name[257];
4314 char id_string[32] = "";
4315
4316 static size_t our_index = 0;
4317
4318 sprintf(id_string, "." HOST_SIZE_T_PRINT_DEC, (fmt_size_t)++our_index);
4319 strcpy(base_name, field->name);
4320 strcat(base_name, id_string);
4321
4322 tree var_type;
4323
4324 // The value is 1, 2, 4, 8 or 16 bytes, so an ordinary constructor can be
4325 // used.
4326 var_type = tree_type_from_size( field->data.capacity,
4327 field->attr & signable_e);
4328 tree new_var_decl = gg_define_variable( var_type,
4329 base_name,
4330 vs_static);
4331 DECL_INITIAL(new_var_decl) = wide_int_to_tree(var_type, value);
4332 field->data_decl_node = new_var_decl;
4333
4334 // Note that during compilation, the integer value, assuming it can be
4335 // contained in 128-bit integers, can be accessed with
4336 //
4337 // wi::to_wide( DECL_INITIAL(new_var_decl) )
4338 }
4339
4340 static void
4341 psa_FldBlob(struct cbl_field_t *var )
4342 {
4343 Analyze();
4344 SHOW_PARSE
4345 {
4346 SHOW_PARSE_HEADER
4347 SHOW_PARSE_FIELD(" ", var)
4348 SHOW_PARSE_END
4349 }
4350
4351 CHECK_FIELD(var);
4352
4353 // We are constructing a completely static constant structure. We know the
4354 // capacity. We'll create it from the data.initial. The var_decl_node will
4355 // be a pointer to the data
4356
4357 char base_name[257];
4358 char id_string[32] = "";
4359
4360 static size_t our_index = 0;
4361
4362 sprintf(id_string, "." HOST_SIZE_T_PRINT_DEC, (fmt_size_t)++our_index);
4363 strcpy(base_name, var->name);
4364 strcat(base_name, id_string);
4365
4366 // Build the constructor for the array of bytes
4367
4368 tree array_type = build_array_type_nelts(UCHAR, var->data.capacity);
4369 tree array_constructor = make_node(CONSTRUCTOR);
4370 TREE_TYPE(array_constructor) = array_type;
4371 TREE_STATIC(array_constructor) = 1;
4372 TREE_CONSTANT(array_constructor) = 1;
4373
4374 for(size_t i=0; i<var->data.capacity; i++)
4375 {
4376 CONSTRUCTOR_APPEND_ELT( CONSTRUCTOR_ELTS(array_constructor),
4377 build_int_cst_type(INT, i),
4378 build_int_cst_type(UCHAR, var->data.initial[i]));
4379 }
4380
4381 // The array constructor is ready to be used
4382 tree var_decl_node = gg_define_variable( array_type,
4383 base_name,
4384 vs_static);
4385 DECL_INITIAL(var_decl_node) = array_constructor;
4386 var->var_decl_node = gg_get_address_of(var_decl_node);
4387 }
4388
4389 void
4390 parser_accept(const struct cbl_refer_t &tgt,
4391 special_name_t special_e,
4392 cbl_label_t *error,
4393 cbl_label_t *not_error )
4394 {
4395 SHOW_PARSE
4396 {
4397 SHOW_PARSE_HEADER
4398 if( error )
4399 {
4400 SHOW_PARSE_LABEL(" error ", error)
4401 }
4402 if( not_error )
4403 {
4404 SHOW_PARSE_LABEL(" not_error ", not_error)
4405 }
4406 SHOW_PARSE_END
4407 }
4408
4409 // The ISO spec describes the valid special names for ACCEPT as implementation
4410 // dependent. We are following IBM's lead.
4411
4412 tree environment = build_int_cst_type(INT, special_e);
4413
4414 const char *function_to_call = NULL;
4415
4416 switch(special_e)
4417 {
4418 case STDIN_e:
4419 case CONSOLE_e:
4420 case SYSIPT_e:
4421 case SYSIN_e:
4422 // This is ordinary input from from the stdin:
4423 gg_call(VOID,
4424 "__gg__accept",
4425 environment,
4426 gg_get_address_of(tgt.field->var_decl_node),
4427 refer_offset(tgt),
4428 refer_size_dest(tgt),
4429 NULL_TREE);
4430 return;
4431 break;
4432
4433 case C01_e:
4434 case C02_e:
4435 case C03_e:
4436 case C04_e:
4437 case C05_e:
4438 case C06_e:
4439 case C07_e:
4440 case C08_e:
4441 case C09_e:
4442 case C10_e:
4443 case C11_e:
4444 case C12_e:
4445 case CSP_e:
4446 case S01_e:
4447 case S02_e:
4448 case S03_e:
4449 case S04_e:
4450 case S05_e:
4451 case AFP_5A_e:
4452 case STDOUT_e:
4453 case SYSOUT_e:
4454 case SYSLIST_e:
4455 case SYSLST_e:
4456 case STDERR_e:
4457 case SYSPUNCH_e:
4458 case SYSPCH_e:
4459 case SYSERR_e:
4460 cbl_internal_error("Not valid for ACCEPT statement.");
4461 break;
4462
4463 case ARG_NUM_e:
4464 // This ACCEPT statement wants the number of argv values:
4465 gg_call(VOID,
4466 "__gg__get_argc",
4467 gg_get_address_of(tgt.field->var_decl_node),
4468 refer_offset(tgt),
4469 refer_size_source(tgt),
4470 NULL_TREE);
4471 return;
4472 break;
4473
4474 case ENV_NAME_e:
4475 // This fetches the environment name set by DISPLAY... UPON ENV_NAME_e
4476 gg_call(VOID,
4477 "__gg__get_env_name",
4478 gg_get_address_of(tgt.field->var_decl_node),
4479 refer_offset(tgt),
4480 refer_size_source(tgt),
4481 NULL_TREE);
4482 return;
4483 break;
4484
4485 case ENV_VALUE_e:
4486 // This fetches the environment value associated with the previously
4487 // esablished name
4488 function_to_call = "__gg__get_env_value";
4489 break;
4490
4491 case ARG_VALUE_e:
4492 // We are fetching the variable whose index was established by a prior
4493 // DISPLAY UPON ARGUMENT-NUMBER. After the fetch, the value will be
4494 // incremented by one.
4495 function_to_call = "__gg__accept_arg_value";
4496 break;
4497 }
4498 if( function_to_call )
4499 {
4500 tree erf = gg_define_int();
4501 gg_assign(erf,
4502 gg_call_expr( INT,
4503 function_to_call,
4504 gg_get_address_of(tgt.field->var_decl_node),
4505 refer_offset(tgt),
4506 refer_size_dest(tgt),
4507 NULL_TREE));
4508 if( error )
4509 {
4510 // There is an ON EXCEPTION phrase:
4511 IF( erf, ne_op, integer_zero_node )
4512 {
4513 SHOW_PARSE
4514 {
4515 SHOW_PARSE_INDENT
4516 SHOW_PARSE_TEXT("Laying down GOTO error->INTO for_argv")
4517 SHOW_PARSE_LABEL_OK(" ", error)
4518 }
4519 gg_append_statement( error->structs.arith_error->into.go_to );
4520 }
4521 ELSE
4522 {
4523 }
4524 ENDIF
4525 }
4526 if( not_error )
4527 {
4528 // There is an NOT ON EXCEPTION phrase:
4529 IF( erf, eq_op, integer_zero_node )
4530 {
4531 SHOW_PARSE
4532 {
4533 SHOW_PARSE_INDENT
4534 SHOW_PARSE_TEXT("Laying down GOTO not_error->INTO for_argv")
4535 SHOW_PARSE_LABEL_OK(" ", not_error)
4536 }
4537 gg_append_statement( not_error->structs.arith_error->into.go_to );
4538 }
4539 ELSE
4540 {
4541 }
4542 ENDIF
4543 }
4544 if( error )
4545 {
4546 SHOW_PARSE
4547 {
4548 SHOW_PARSE_INDENT
4549 SHOW_PARSE_TEXT("Laying down LABEL error->bottom")
4550 SHOW_PARSE_LABEL_OK(" ", error)
4551 }
4552 gg_append_statement( error->structs.arith_error->bottom.label );
4553 }
4554 if( not_error )
4555 {
4556 SHOW_PARSE
4557 {
4558 SHOW_PARSE_INDENT
4559 SHOW_PARSE_TEXT("Laying down LABEL not_error->bottom")
4560 SHOW_PARSE_LABEL_OK(" ", not_error)
4561 SHOW_PARSE_END
4562 }
4563 gg_append_statement( not_error->structs.arith_error->bottom.label );
4564 }
4565 }
4566 }
4567
4568 // TODO: update documentation.
4569 void
4570 parser_accept_exception( cbl_label_t *accept_label )
4571 {
4572 // We can't use Analyze() on this one, because the exit ends up being laid
4573 // down before the enter when the goto logic gets untangled by the compiler.
4574
4575 // We are entering either SIZE ERROR or NOT SIZE ERROR code
4576 RETURN_IF_PARSE_ONLY;
4577
4578 SHOW_PARSE
4579 {
4580 SHOW_PARSE_HEADER
4581 SHOW_PARSE_TEXT(" Laying down GOTO OVER")
4582 SHOW_PARSE_LABEL(" ", accept_label)
4583 SHOW_PARSE_INDENT
4584 SHOW_PARSE_TEXT("Laying down LABEL INTO:")
4585 SHOW_PARSE_LABEL(" ", accept_label)
4586 SHOW_PARSE_END
4587 }
4588
4589 CHECK_LABEL(accept_label);
4590 set_up_on_exception_label(accept_label);
4591
4592 // Jump over the [NOT] ON EXCEPTION code that is about to be laid down
4593 gg_append_statement( accept_label->structs.arith_error->over.go_to );
4594 // Create the label that allows the following code to be executed at
4595 // when an ERROR, or NOT ERROR, has been determined to have taken place:
4596 gg_append_statement( accept_label->structs.arith_error->into.label );
4597 }
4598
4599 void
4600 parser_accept_exception_end( cbl_label_t *accept_label )
4601 {
4602 // We can't use Analyze() on this one, because the exit ends up being laid
4603 // down before the enter when the goto logic gets untangled by the compiler.
4604
4605 // We have reached the end of the ERROR, or NOT ERROR, code.
4606
4607 SHOW_PARSE
4608 {
4609 SHOW_PARSE_HEADER
4610 SHOW_PARSE_TEXT(" Laying down GOTO BOTTOM")
4611 SHOW_PARSE_LABEL(" ", accept_label)
4612 SHOW_PARSE_INDENT
4613 SHOW_PARSE_TEXT("Laying down LABEL OVER:")
4614 SHOW_PARSE_LABEL(" ", accept_label)
4615 SHOW_PARSE_END
4616 }
4617
4618 CHECK_LABEL(accept_label);
4619
4620 // Jump to the end of the arithmetic code:
4621 gg_append_statement( accept_label->structs.arith_error->bottom.go_to );
4622 // Lay down the label that allows the ERROR/NOT ERROR instructions
4623 // to exist in a lacuna that doesn't get executed unless somebody jumps
4624 // to it:
4625 gg_append_statement( accept_label->structs.arith_error->over.label );
4626 }
4627
4628 void
4629 parser_accept_command_line( const cbl_refer_t &tgt,
4630 const cbl_refer_t &source,
4631 cbl_label_t *error,
4632 cbl_label_t *not_error )
4633 {
4634 Analyze();
4635 SHOW_PARSE
4636 {
4637 SHOW_PARSE_HEADER
4638 if( error )
4639 {
4640 SHOW_PARSE_LABEL(" error ", error)
4641 }
4642 if( not_error )
4643 {
4644 SHOW_PARSE_LABEL(" not_error ", not_error)
4645 }
4646 SHOW_PARSE_END
4647 }
4648
4649 static tree erf = gg_define_variable(INT, "..pac_erf", vs_file_static);
4650
4651 if( !source.field )
4652 {
4653 // The whole command-line is wanted
4654 gg_assign(erf,
4655 gg_call_expr( INT,
4656 "__gg__get_command_line",
4657 gg_get_address_of(tgt.field->var_decl_node),
4658 refer_offset(tgt),
4659 refer_size_dest(tgt),
4660 NULL_TREE));
4661 if( error )
4662 {
4663 // There is an ON EXCEPTION phrase:
4664 IF( erf, ne_op, integer_zero_node )
4665 {
4666 SHOW_PARSE
4667 {
4668 SHOW_PARSE_INDENT
4669 SHOW_PARSE_TEXT("Laying down GOTO error->INTO for_command_line")
4670 SHOW_PARSE_LABEL_OK(" ", error)
4671 }
4672 gg_append_statement( error->structs.arith_error->into.go_to );
4673 }
4674 ELSE
4675 {
4676 }
4677 ENDIF
4678 }
4679 if( not_error )
4680 {
4681 // There is an NOT ON EXCEPTION phrase:
4682 IF( erf, eq_op, integer_zero_node )
4683 {
4684 SHOW_PARSE
4685 {
4686 SHOW_PARSE_INDENT
4687 SHOW_PARSE_TEXT("Laying down GOTO not_error->INTO for command_line")
4688 SHOW_PARSE_LABEL_OK(" ", not_error)
4689 }
4690 gg_append_statement( not_error->structs.arith_error->into.go_to );
4691 }
4692 ELSE
4693 {
4694 }
4695 ENDIF
4696 }
4697 }
4698 else
4699 {
4700 // A particular parameter has been requested:
4701 gg_assign(erf,
4702 gg_call_expr( INT,
4703 "__gg__get_argv",
4704 gg_get_address_of(tgt.field->var_decl_node),
4705 refer_offset(tgt),
4706 refer_size_dest(tgt),
4707 gg_get_address_of(source.field->var_decl_node),
4708 refer_offset(source),
4709 refer_size_dest(source),
4710 NULL_TREE));
4711 if( error )
4712 {
4713 // There is an ON EXCEPTION phrase:
4714 IF( erf, ne_op, integer_zero_node )
4715 {
4716 SHOW_PARSE
4717 {
4718 SHOW_PARSE_INDENT
4719 SHOW_PARSE_TEXT("Laying down GOTO error->INTO for_argv")
4720 SHOW_PARSE_LABEL_OK(" ", error)
4721 }
4722 gg_append_statement( error->structs.arith_error->into.go_to );
4723 }
4724 ELSE
4725 {
4726 }
4727 ENDIF
4728 }
4729 if( not_error )
4730 {
4731 // There is an NOT ON EXCEPTION phrase:
4732 IF( erf, eq_op, integer_zero_node )
4733 {
4734 SHOW_PARSE
4735 {
4736 SHOW_PARSE_INDENT
4737 SHOW_PARSE_TEXT("Laying down GOTO not_error->INTO for_argv")
4738 SHOW_PARSE_LABEL_OK(" ", not_error)
4739 }
4740 gg_append_statement( not_error->structs.arith_error->into.go_to );
4741 }
4742 ELSE
4743 {
4744 }
4745 ENDIF
4746 }
4747 }
4748 if( error )
4749 {
4750 SHOW_PARSE
4751 {
4752 SHOW_PARSE_INDENT
4753 SHOW_PARSE_TEXT("Laying down LABEL error->bottom")
4754 SHOW_PARSE_LABEL_OK(" ", error)
4755 }
4756 gg_append_statement( error->structs.arith_error->bottom.label );
4757 }
4758 if( not_error )
4759 {
4760 SHOW_PARSE
4761 {
4762 SHOW_PARSE_INDENT
4763 SHOW_PARSE_TEXT("Laying down LABEL not_error->bottom")
4764 SHOW_PARSE_LABEL_OK(" ", not_error)
4765 SHOW_PARSE_END
4766 }
4767 gg_append_statement( not_error->structs.arith_error->bottom.label );
4768 }
4769 }
4770
4771 void
4772 parser_accept_command_line_count( const cbl_refer_t &tgt )
4773 {
4774 Analyze();
4775 SHOW_PARSE
4776 {
4777 SHOW_PARSE_HEADER
4778 SHOW_PARSE_END
4779 }
4780 TRACE1
4781 {
4782 TRACE1_HEADER
4783 TRACE1_END
4784 }
4785 gg_call( VOID,
4786 "__gg__get_argc",
4787 gg_get_address_of(tgt.field->var_decl_node),
4788 refer_offset(tgt),
4789 refer_size_dest(tgt),
4790 NULL_TREE);
4791 }
4792
4793 void
4794 parser_accept_envar(const struct cbl_refer_t &tgt,
4795 const struct cbl_refer_t &envar,
4796 cbl_label_t *error,
4797 cbl_label_t *not_error )
4798 {
4799 Analyze();
4800
4801 SHOW_PARSE
4802 {
4803 SHOW_PARSE_HEADER
4804 if( error )
4805 {
4806 SHOW_PARSE_LABEL(" error ", error)
4807 }
4808 if( not_error )
4809 {
4810 SHOW_PARSE_LABEL(" not_error ", not_error)
4811 }
4812 SHOW_PARSE_END
4813 }
4814 TRACE1
4815 {
4816 TRACE1_HEADER
4817 TRACE1_END
4818 }
4819
4820 static tree erf = gg_define_variable(INT, "..pae_erf", vs_file_static);
4821
4822 gg_assign(erf,
4823 gg_call_expr( INT,
4824 "__gg__accept_envar",
4825 gg_get_address_of(tgt.field->var_decl_node),
4826 refer_offset(tgt),
4827 refer_size_dest(tgt),
4828 gg_get_address_of(envar.field->var_decl_node),
4829 refer_offset(envar),
4830 refer_size_source(envar),
4831 NULL_TREE));
4832 if( error )
4833 {
4834 // There is an ON EXCEPTION phrase:
4835 IF( erf, ne_op, integer_zero_node )
4836 {
4837 gg_append_statement( error->structs.arith_error->into.go_to );
4838 }
4839 ELSE
4840 {
4841 }
4842 ENDIF
4843 }
4844 if( not_error )
4845 {
4846 // There is an NOT ON EXCEPTION phrase:
4847 IF( erf, eq_op, integer_zero_node )
4848 {
4849 gg_append_statement( not_error->structs.arith_error->into.go_to );
4850 }
4851 ELSE
4852 {
4853 }
4854 ENDIF
4855 }
4856 if( error )
4857 {
4858 SHOW_PARSE
4859 {
4860 SHOW_PARSE_INDENT
4861 SHOW_PARSE_TEXT("Laying down LABEL error->bottom")
4862 SHOW_PARSE_LABEL_OK(" ", error)
4863 }
4864 gg_append_statement( error->structs.arith_error->bottom.label );
4865 }
4866 if( not_error )
4867 {
4868 SHOW_PARSE
4869 {
4870 SHOW_PARSE_INDENT
4871 SHOW_PARSE_TEXT("Laying down LABEL not_error->bottom")
4872 SHOW_PARSE_LABEL_OK(" ", not_error)
4873 SHOW_PARSE_END
4874 }
4875 gg_append_statement( not_error->structs.arith_error->bottom.label );
4876 }
4877 }
4878
4879 void
4880 parser_set_envar( const struct cbl_refer_t &name,
4881 const struct cbl_refer_t &value )
4882 {
4883 Analyze();
4884 SHOW_PARSE
4885 {
4886 SHOW_PARSE_HEADER
4887 SHOW_PARSE_END
4888 }
4889 TRACE1
4890 {
4891 TRACE1_HEADER
4892 TRACE1_END
4893 }
4894
4895 // Set name to value using setenv(3)
4896 gg_call(BOOL,
4897 "__gg__set_envar",
4898 gg_get_address_of(name.field->var_decl_node),
4899 refer_offset(name),
4900 refer_size_source(name),
4901 gg_get_address_of(value.field->var_decl_node),
4902 refer_offset(value),
4903 refer_size_source(value),
4904 NULL_TREE);
4905 }
4906
4907 void
4908 parser_accept_date_yymmdd( struct cbl_field_t *target )
4909 {
4910 Analyze();
4911 SHOW_PARSE
4912 {
4913 SHOW_PARSE_HEADER
4914 SHOW_PARSE_END
4915 }
4916
4917 CHECK_FIELD(target);
4918
4919 tree pointer = gg_define_char_star();
4920 gg_assign(pointer, gg_call_expr(CHAR_P,
4921 "__gg__get_date_yymmdd",
4922 NULL_TREE));
4923 gg_default_qualification(target);
4924 move_tree_to_field( target,
4925 pointer);
4926
4927 gg_free(pointer);
4928
4929 TRACE1
4930 {
4931 TRACE1_HEADER
4932 TRACE1_FIELD("", target, "")
4933 TRACE1_END
4934 }
4935 }
4936
4937 void
4938 parser_accept_date_yyyymmdd( struct cbl_field_t *target )
4939 {
4940 Analyze();
4941 SHOW_PARSE
4942 {
4943 SHOW_PARSE_HEADER
4944 SHOW_PARSE_END
4945 }
4946
4947 CHECK_FIELD(target);
4948
4949 tree pointer = gg_define_char_star();
4950 gg_assign(pointer, gg_call_expr(CHAR_P,
4951 "__gg__get_date_yyyymmdd",
4952 NULL_TREE));
4953 gg_default_qualification(target);
4954 move_tree_to_field( target,
4955 pointer);
4956
4957 gg_free(pointer);
4958
4959 TRACE1
4960 {
4961 TRACE1_HEADER
4962 TRACE1_FIELD("", target, "")
4963 TRACE1_END
4964 }
4965 }
4966
4967 void
4968 parser_accept_date_yyddd( struct cbl_field_t *target )
4969 {
4970 Analyze();
4971 SHOW_PARSE
4972 {
4973 SHOW_PARSE_HEADER
4974 SHOW_PARSE_END
4975 }
4976
4977 CHECK_FIELD(target);
4978
4979 tree pointer = gg_define_char_star();
4980 gg_assign(pointer, gg_call_expr(CHAR_P,
4981 "__gg__get_date_yyddd",
4982 NULL_TREE));
4983 gg_default_qualification(target);
4984 move_tree_to_field( target,
4985 pointer);
4986
4987 gg_free(pointer);
4988
4989 TRACE1
4990 {
4991 TRACE1_HEADER
4992 TRACE1_FIELD("", target,"");
4993 TRACE1_END
4994 }
4995 }
4996
4997 void
4998 parser_accept_date_yyyyddd( struct cbl_field_t *target )
4999 {
5000 Analyze();
5001 SHOW_PARSE
5002 {
5003 SHOW_PARSE_HEADER
5004 SHOW_PARSE_END
5005 }
5006
5007 CHECK_FIELD(target);
5008
5009 tree pointer = gg_define_char_star();
5010 gg_assign(pointer, gg_call_expr(CHAR_P,
5011 "__gg__get_yyyyddd",
5012 NULL_TREE));
5013 gg_default_qualification(target);
5014 move_tree_to_field( target,
5015 pointer);
5016
5017 gg_free(pointer);
5018
5019 TRACE1
5020 {
5021 TRACE1_HEADER
5022 TRACE1_FIELD("", target, "")
5023 TRACE1_END
5024 }
5025 }
5026
5027 void
5028 parser_accept_date_dow( struct cbl_field_t *target )
5029 {
5030 Analyze();
5031 SHOW_PARSE
5032 {
5033 SHOW_PARSE_HEADER
5034 SHOW_PARSE_END
5035 }
5036
5037 CHECK_FIELD(target);
5038
5039 tree pointer = gg_define_char_star();
5040 gg_assign(pointer, gg_call_expr(CHAR_P,
5041 "__gg__get_date_dow",
5042 NULL_TREE));
5043 gg_default_qualification(target);
5044 move_tree_to_field( target,
5045 pointer);
5046
5047 gg_free(pointer);
5048
5049 TRACE1
5050 {
5051 TRACE1_HEADER
5052 TRACE1_FIELD("", target, "")
5053 TRACE1_END
5054 }
5055 }
5056
5057 void
5058 parser_accept_date_hhmmssff( struct cbl_field_t *target )
5059 {
5060 Analyze();
5061 SHOW_PARSE
5062 {
5063 SHOW_PARSE_HEADER
5064 SHOW_PARSE_END
5065 }
5066
5067 CHECK_FIELD(target);
5068
5069 tree pointer = gg_define_char_star();
5070 gg_assign(pointer, gg_call_expr(CHAR_P,
5071 "__gg__get_date_hhmmssff",
5072 NULL_TREE));
5073 gg_default_qualification(target);
5074 move_tree_to_field( target,
5075 pointer);
5076
5077 gg_free(pointer);
5078
5079 TRACE1
5080 {
5081 TRACE1_HEADER
5082 TRACE1_FIELD("", target, "")
5083 TRACE1_END
5084 }
5085 }
5086
5087 /*
5088 * If the encoding is anything but custom, the enumerated type
5089 * cbl_encoding_t suffices to describe it. At least for now, the rest
5090 * of cbl_alphabet_t in those cases is unused.
5091 *
5092 * To get the symbol index: symbol_index(symbol_elem_of(&alphabet))
5093 *
5094 * The parameter is always a reference to an element in the symbol table.
5095 */
5096 void
5097 parser_alphabet( cbl_alphabet_t& alphabet )
5098 {
5099 Analyze();
5100 SHOW_PARSE
5101 {
5102 SHOW_PARSE_HEADER
5103 fprintf(stderr, "%s\n", alphabet.name);
5104 switch(alphabet.encoding)
5105 {
5106 case ASCII_e:
5107 fprintf(stderr, "ASCII\n");
5108 break;
5109 case iso646_e:
5110 fprintf(stderr, "ISO646\n");
5111 break;
5112 case EBCDIC_e:
5113 fprintf(stderr, "EBCDIC\n");
5114 break;
5115 case custom_encoding_e:
5116 fprintf(stderr, "%s\n", alphabet.name);
5117 break;
5118 }
5119 SHOW_PARSE_END
5120 }
5121
5122 size_t alphabet_index = symbol_index(symbol_elem_of(&alphabet));
5123
5124 switch(alphabet.encoding)
5125 {
5126 case ASCII_e:
5127 case iso646_e:
5128 case EBCDIC_e:
5129 break;
5130
5131 case custom_encoding_e:
5132 {
5133 unsigned char ach[256];
5134
5135 tree table_type = build_array_type_nelts(UCHAR, 256);
5136 tree table256 = gg_define_variable(table_type);
5137 for( int i=0; i<256; i++ )
5138 {
5139 // character i has the ordinal alphabet[i]
5140 unsigned char ch = ascii_to_internal(i);
5141
5142 ach[ch] = (alphabet.alphabet[i]);
5143 gg_assign( gg_array_value(table256, ch),
5144 build_int_cst_type(UCHAR, (alphabet.alphabet[i])) );
5145 }
5146 __gg__alphabet_create(alphabet.encoding,
5147 alphabet_index,
5148 ach,
5149 alphabet.low_index,
5150 alphabet.high_index);
5151 gg_call(VOID,
5152 "__gg__alphabet_create",
5153 build_int_cst_type(INT, alphabet.encoding),
5154 build_int_cst_type(SIZE_T, alphabet_index),
5155 gg_get_address_of(table256),
5156 build_int_cst_type(INT, alphabet.low_index),
5157 build_int_cst_type(INT, alphabet.high_index),
5158 NULL_TREE );
5159 break;
5160 }
5161 }
5162 }
5163
5164 void
5165 parser_alphabet_use( cbl_alphabet_t& alphabet )
5166 {
5167 Analyze();
5168 SHOW_PARSE
5169 {
5170 SHOW_PARSE_HEADER
5171 switch(alphabet.encoding)
5172 {
5173 case ASCII_e:
5174 fprintf(stderr, "ASCII\n");
5175 break;
5176 case iso646_e:
5177 fprintf(stderr, "ISO646\n");
5178 break;
5179 case EBCDIC_e:
5180 fprintf(stderr, "EBCDIC\n");
5181 break;
5182 case custom_encoding_e:
5183 fprintf(stderr, "%s\n", alphabet.name);
5184 break;
5185 }
5186 SHOW_PARSE_END
5187 }
5188
5189 size_t alphabet_index = symbol_index(symbol_elem_of(&alphabet));
5190
5191 switch(alphabet.encoding)
5192 {
5193 case ASCII_e:
5194 case iso646_e:
5195 case EBCDIC_e:
5196 __gg__low_value_character = DEGENERATE_LOW_VALUE;
5197 __gg__high_value_character = DEGENERATE_HIGH_VALUE;
5198 gg_call(VOID,
5199 "__gg__alphabet_use",
5200 build_int_cst_type(INT, alphabet.encoding),
5201 null_pointer_node,
5202 NULL_TREE);
5203 break;
5204
5205 case custom_encoding_e:
5206 std::unordered_map<size_t, alphabet_state>::const_iterator it =
5207 __gg__alphabet_states.find(alphabet_index);
5208
5209 assert( it != __gg__alphabet_states.end());
5210 __gg__low_value_character = it->second.low_char;
5211 __gg__high_value_character = it->second.high_char;
5212
5213 gg_call(VOID,
5214 "__gg__alphabet_use",
5215 build_int_cst_type(INT, alphabet.encoding),
5216 build_int_cst_type(SIZE_T, alphabet_index),
5217 NULL_TREE);
5218 break;
5219 }
5220 }
5221
5222 void
5223 parser_display_literal(const char *literal, bool advance)
5224 {
5225 Analyze();
5226 SHOW_PARSE
5227 {
5228 SHOW_PARSE_HEADER
5229 SHOW_PARSE_TEXT(" \"");
5230 SHOW_PARSE_TEXT(literal)
5231 SHOW_PARSE_TEXT("\"");
5232 SHOW_PARSE_END
5233 }
5234
5235 TRACE1
5236 {
5237 TRACE1_HEADER
5238 TRACE1_TEXT("About to DISPLAY a literal:")
5239 TRACE1_END
5240 }
5241
5242 tree file_descriptor = integer_one_node; // Just stdout, for now
5243 gg_write( file_descriptor,
5244 gg_string_literal(literal),
5245 build_int_cst_type(integer_type_node,(int)strlen(literal)) );
5246
5247 if( advance )
5248 {
5249 gg_write( file_descriptor,
5250 gg_string_literal("\n"),
5251 integer_one_node);
5252 }
5253 cursor_at_sol = advance;
5254 }
5255
5256 void
5257 parser_display_internal(tree file_descriptor,
5258 cbl_refer_t refer,
5259 bool advance)
5260 {
5261 Analyze();
5262 if( refer.field->type == FldConditional )
5263 {
5264 TRACE1
5265 {
5266 gg_create_true_false_statement_lists(refer.field->var_decl_node);
5267 gg_fprintf(file_descriptor, 0, "TRUE");
5268 ELSE
5269 gg_fprintf(file_descriptor, 0, "FALSE");
5270 ENDIF
5271 }
5272 }
5273 else if( refer.field->type == FldLiteralA )
5274 {
5275 gg_call(VOID,
5276 "__gg__display_string",
5277 file_descriptor,
5278 build_string_literal(refer.field->data.capacity,
5279 refer.field->data.initial),
5280 build_int_cst_type(SIZE_T, refer.field->data.capacity),
5281 advance ? integer_one_node : integer_zero_node,
5282 NULL_TREE );
5283 }
5284 else if( refer.field->type == FldLiteralN )
5285 {
5286 // The parser found the string of digits from the source code and converted
5287 // it to a 128-bit binary floating point number.
5288
5289 // The bad news is that something like 555.55 can't be expressed exactly;
5290 // internally it is 555.5499999999....
5291
5292 // The good news is that we know any string of 33 or fewer decimal digits
5293 // can be converted to and from IEEE 754 binary128 without being changes
5294
5295 // We make use of that here
5296
5297 char ach[128];
5298 real_to_decimal (ach, TREE_REAL_CST_PTR (refer.field->data.value_of()),
5299 sizeof(ach), 33, 0);
5300 char *p = strchr(ach, 'e');
5301 if( !p )
5302 {
5303 // Probably INF -INF NAN or -NAN, so ach has our result
5304 // Except that real_to_decimal prints -0.0 and 0.0 like that with
5305 // no e.
5306 if( ach[0] == '0' || ( ach[0] == '-' && ach[1] == '0' ))
5307 __gg__remove_trailing_zeroes(ach);
5308 }
5309 else
5310 {
5311 int exp = atoi(p+1);
5312 if( exp >= 6 || exp <= -5 )
5313 {
5314 // We are going to stick with the E notation, so ach has our result
5315 // Except that real_to_decimal prints with e notation rather than E
5316 // and doesn't guarantee at least two exponent digits.
5317 *p = 'E';
5318 if( exp < 0 && exp >= -9 )
5319 {
5320 p[1] = '-';
5321 p[2] = '0';
5322 p[3] = '0' - exp;
5323 p[4] = '\0';
5324 }
5325 else if( exp >= 0 && exp <= 9 )
5326 {
5327 p[1] = '+';
5328 p[2] = '0';
5329 p[3] = '0' + exp;
5330 p[4] = '\0';
5331 }
5332 }
5333 else if (exp == 0)
5334 {
5335 p[-1] = '\0';
5336 }
5337 else if (exp < 0)
5338 {
5339 p[-1] = '\0';
5340 char *q = strchr (ach, '.');
5341 char dig = q[-1];
5342 q[-1] = '\0';
5343 char tem[132];
5344 snprintf (tem, 132, "%s0.%0*d%c%s", ach, -exp - 1, 0, dig, q + 1);
5345 strcpy (ach, tem);
5346 }
5347 else // if (exp > 0)
5348 {
5349 p[-1] = '\0';
5350 char *q = strchr (ach, '.');
5351 for (int i = 0; i != exp; ++i)
5352 q[i] = q[i + 1];
5353 q[exp] = '.';
5354 }
5355 __gg__remove_trailing_zeroes(ach);
5356 }
5357
5358 if( symbol_decimal_point() == ',' )
5359 {
5360 char *pdot = strchr(ach, '.' );
5361 if( pdot )
5362 {
5363 *pdot = symbol_decimal_point();
5364 }
5365 }
5366
5367 gg_write( file_descriptor,
5368 gg_string_literal(ach),
5369 build_int_cst_type(SIZE_T, strlen(ach)));
5370 if( advance )
5371 {
5372 gg_write( file_descriptor,
5373 gg_string_literal("\n"),
5374 integer_one_node);
5375 }
5376 }
5377 else
5378 {
5379 if( refer_is_clean(refer) )
5380 {
5381 gg_call(VOID,
5382 "__gg__display_clean",
5383 gg_get_address_of(refer.field->var_decl_node),
5384 file_descriptor,
5385 advance ? integer_one_node : integer_zero_node,
5386 NULL_TREE );
5387 }
5388 else
5389 {
5390 // We might be dealing with a refmod:
5391 if( refer.refmod.from || refer.refmod.len )
5392 {
5393 gg_attribute_bit_set(refer.field, refmod_e);
5394 }
5395 gg_call(VOID,
5396 "__gg__display",
5397 gg_get_address_of(refer.field->var_decl_node),
5398 refer_offset(refer),
5399 refer_size_source( refer),
5400 file_descriptor,
5401 advance ? integer_one_node : integer_zero_node,
5402 NULL_TREE );
5403 if( refer.refmod.from || refer.refmod.len )
5404 {
5405 gg_attribute_bit_clear(refer.field, refmod_e);
5406 }
5407 }
5408 }
5409 cursor_at_sol = advance;
5410 }
5411
5412 void
5413 parser_display_field(cbl_field_t *field)
5414 {
5415 parser_display_internal_field(integer_one_node,
5416 field,
5417 DISPLAY_NO_ADVANCE);
5418 }
5419
5420 void
5421 parser_display( const struct cbl_special_name_t *upon,
5422 const std::vector<cbl_refer_t> &refs,
5423 bool advance,
5424 const cbl_label_t *not_error,
5425 const cbl_label_t *error )
5426 {
5427 const size_t n = refs.size();
5428 /*
5429 * The first parameter to parser_display is the "device" upon which to display
5430 * the data. Besides normal devices, these may include elements that define the
5431 * Unix command line and environment:
5432 * 1. ARG_NUM_e, the ARGUMENT-NUMBER
5433 * 2. ARG_VALUE_e, the ARGUMENT-VALUE
5434 * 3. ENV_NAME_e, the ENVIRONMENT-NAME
5435 * 4. ENV_VALUE_e, the ENVIRONMENT-VALUE
5436 * that need special care and feeding.
5437 */
5438
5439 // At the present time, I am not sure what not_error and error are for
5440 gcc_assert(!not_error);
5441 gcc_assert(!error);
5442
5443 Analyze();
5444 SHOW_PARSE
5445 {
5446 SHOW_PARSE_HEADER
5447 SHOW_PARSE_TEXT(" parser_display of multiple variables:")
5448 for(size_t i=0; i<n; i++)
5449 {
5450 SHOW_PARSE_INDENT
5451 SHOW_PARSE_REF("", refs.at(i));
5452 }
5453 if( advance )
5454 {
5455 SHOW_PARSE_TEXT(" (advance)")
5456 }
5457 SHOW_PARSE_END
5458 }
5459
5460 TRACE1
5461 {
5462 TRACE1_HEADER
5463 for(size_t ii=0; ii<n; ii++)
5464 {
5465 if( ii != 0 )
5466 {
5467 TRACE1_INDENT
5468 }
5469 if(n > 1)
5470 {
5471 gg_fprintf(trace_handle, 1, "%ld: ", build_int_cst_type(INT, ii));
5472 }
5473 TRACE1_REFER("", refs[ii], "")
5474 }
5475 TRACE1_END
5476 }
5477 tree file_descriptor = gg_define_int();
5478 bool needs_closing = false;
5479 if( upon )
5480 {
5481 switch(upon->id)
5482 {
5483 // See table 5 in the IBM Cobol For Linux x86 1.2 document.
5484
5485 case STDIN_e:
5486 case SYSIN_e:
5487 case SYSIPT_e:
5488 cbl_internal_error("Attempting to send to an input device.");
5489 break;
5490
5491 case C01_e:
5492 case C02_e:
5493 case C03_e:
5494 case C04_e:
5495 case C05_e:
5496 case C06_e:
5497 case C07_e:
5498 case C08_e:
5499 case C09_e:
5500 case C10_e:
5501 case C11_e:
5502 case C12_e:
5503 case CSP_e:
5504 case S01_e:
5505 case S02_e:
5506 case S03_e:
5507 case S04_e:
5508 case S05_e:
5509 case AFP_5A_e:
5510 case ARG_VALUE_e:
5511 cbl_internal_error("Not valid for DISPLAY statement.");
5512 break;
5513
5514 case STDOUT_e:
5515 case CONSOLE_e:
5516 // These are inarguably stdout
5517 gg_assign(file_descriptor, integer_one_node);
5518 break;
5519
5520 case STDERR_e:
5521 case SYSERR_e:
5522 // These are inarguably stderr
5523 gg_assign(file_descriptor, integer_two_node);
5524 break;
5525
5526 case SYSOUT_e:
5527 case SYSLIST_e:
5528 case SYSLST_e:
5529 case SYSPUNCH_e:
5530 case SYSPCH_e:
5531 // In the 21st century, when there are no longer valid assumptions to
5532 // be made about the existence of line printers, and where things
5533 // formerly-ubiquitous card punches no longer exist, there is a need
5534 // for the possibility of assigning these "devices" to externally-
5535 // determined Unix gadgetry in /dev:
5536 gg_assign(file_descriptor,
5537 gg_call_expr( INT,
5538 "__gg__get_file_descriptor",
5539 gg_string_literal(upon->os_filename),
5540 NULL_TREE));
5541 needs_closing = true;
5542 break;
5543
5544 case ARG_NUM_e:
5545 // Set the index number for a subsequent ACCEPT FROM ARG_VALUE_e
5546 gg_call(VOID,
5547 "__gg__set_arg_num",
5548 gg_get_address_of(refs[0].field->var_decl_node),
5549 refer_offset(refs[0]),
5550 refer_size_source(refs[0]),
5551 NULL_TREE);
5552 return;
5553 break;
5554
5555 case ENV_NAME_e:
5556 // Establish the name of an environment variable for later use with
5557 // in either DISPLAY UPON or ACCEPT FROM
5558 gg_call(VOID,
5559 "__gg__set_env_name",
5560 gg_get_address_of(refs[0].field->var_decl_node),
5561 refer_offset(refs[0]),
5562 refer_size_source(refs[0]),
5563 NULL_TREE);
5564 return;
5565 break;
5566
5567 case ENV_VALUE_e:
5568 // Set the contents of the environment variable named with ENV_NAME_e
5569 gg_call(VOID,
5570 "__gg__set_env_value",
5571 gg_get_address_of(refs[0].field->var_decl_node),
5572 refer_offset(refs[0]),
5573 refer_size_source(refs[0]),
5574 NULL_TREE);
5575 return;
5576 break;
5577 }
5578 }
5579 else
5580 {
5581 gg_assign(file_descriptor,integer_one_node); // stdout is file descriptor 1.
5582 }
5583
5584 for(size_t i=0; i<n-1; i++)
5585 {
5586 CHECK_FIELD(refs[i].field);
5587 parser_display_internal(file_descriptor, refs[i], DISPLAY_NO_ADVANCE);
5588 }
5589 CHECK_FIELD(refs[n-1].field);
5590 parser_display_internal(file_descriptor, refs[n-1], advance ? DISPLAY_ADVANCE : DISPLAY_NO_ADVANCE);
5591 if( needs_closing )
5592 {
5593 gg_close(file_descriptor);
5594 }
5595
5596 cursor_at_sol = advance;
5597 }
5598
5599 static
5600 bool // Returns false for literals; true for named variables
5601 get_exhibit_name(tree file_descriptor, const cbl_refer_t &arg)
5602 {
5603 bool retval;
5604 if( is_literal(arg.field) )
5605 {
5606 // If something is a literal, we just display the literal value
5607 parser_display_internal(file_descriptor,
5608 arg,
5609 DISPLAY_NO_ADVANCE);
5610 retval = false;
5611 }
5612 else
5613 {
5614 // It's not a literal, so we show its name, and the names or literal
5615 // values) of any qualifier subscripts or refmods
5616 gg_write( file_descriptor,
5617 gg_string_literal(arg.field->name),
5618 build_int_cst_type(SIZE_T, strlen(arg.field->name)) );
5619
5620 if( arg.subscripts.size() )
5621 {
5622 // This refer has subscripts:
5623 gg_write( file_descriptor,
5624 gg_string_literal("("),
5625 integer_one_node );
5626 for(size_t i=0; i<arg.subscripts.size(); i++)
5627 {
5628 if( i > 0 )
5629 {
5630 gg_write( file_descriptor,
5631 gg_string_literal(","),
5632 integer_one_node );
5633 }
5634 get_exhibit_name(file_descriptor, arg.subscripts[i]);
5635 }
5636 gg_write( file_descriptor,
5637 gg_string_literal(")"),
5638 integer_one_node );
5639 }
5640 if( arg.refmod.from || arg.refmod.len )
5641 {
5642 gg_write( file_descriptor,
5643 gg_string_literal("("),
5644 integer_one_node );
5645 if( arg.refmod.from )
5646 {
5647 get_exhibit_name(file_descriptor, *(arg.refmod.from));
5648 }
5649 gg_write( file_descriptor,
5650 gg_string_literal(":"),
5651 integer_one_node );
5652 if( arg.refmod.len )
5653 {
5654 get_exhibit_name(file_descriptor, *(arg.refmod.len));
5655 }
5656 gg_write( file_descriptor,
5657 gg_string_literal(")"),
5658 integer_one_node );
5659 }
5660 retval = true;
5661 }
5662 return retval;
5663 }
5664
5665 void
5666 parser_exhibit( bool /*changed*/, bool /*named*/,
5667 const std::vector<cbl_refer_t> &args )
5668 {
5669 tree file_descriptor = gg_define_int();
5670 gg_assign(file_descriptor, integer_one_node); // stdout is file descriptor 1.
5671
5672 for(size_t i=0; i<args.size(); i++)
5673 {
5674 CHECK_FIELD(args[i].field);
5675 if(i > 0)
5676 {
5677 // When there more than one argument, the second through Nth get a space
5678 // in front of them.
5679 gg_write( file_descriptor,
5680 gg_string_literal(" "),
5681 integer_one_node);
5682 }
5683 if( get_exhibit_name(file_descriptor, args[i]) )
5684 {
5685 gg_write( file_descriptor,
5686 gg_string_literal("="),
5687 integer_one_node);
5688 parser_display_internal(file_descriptor,
5689 args[i],
5690 DISPLAY_NO_ADVANCE);
5691 }
5692 }
5693 gg_write( file_descriptor,
5694 gg_string_literal("\n"),
5695 integer_one_node);
5696 cursor_at_sol = true;
5697 }
5698
5699 static tree
5700 get_literalN_value(cbl_field_t *var)
5701 {
5702 // Get the literal N value from the integer var_decl
5703 tree retval = NULL_TREE;
5704 tree var_type = tree_type_from_size(var->data.capacity,
5705 var->attr & signable_e);
5706 retval = gg_cast(var_type, var->data_decl_node);
5707 return retval;
5708 }
5709
5710 void
5711 parser_assign( size_t nC, cbl_num_result_t *C,
5712 struct cbl_refer_t sourceref,
5713 cbl_label_t *on_error,
5714 cbl_label_t *not_error,
5715 cbl_label_t *compute_error)
5716 {
5717 Analyze();
5718 RETURN_IF_PARSE_ONLY;
5719 // There might, or might not, already be error and/or not_error labels:
5720 set_up_on_exception_label(on_error);
5721 set_up_on_exception_label(not_error);
5722 set_up_compute_error_label(compute_error);
5723
5724 SHOW_PARSE
5725 {
5726 SHOW_PARSE_HEADER
5727 }
5728
5729 TRACE1
5730 {
5731 TRACE1_HEADER
5732 char ach[32];
5733 sprintf(ach, HOST_SIZE_T_PRINT_DEC " target%s",
5734 (fmt_size_t)nC, nC==1 ? "" : "s");
5735 TRACE1_TEXT(ach);
5736 if( on_error )
5737 {
5738 TRACE1_TEXT("; with on_error");
5739 }
5740 if( not_error )
5741 {
5742 TRACE1_TEXT("; with not_error");
5743 }
5744 }
5745
5746 tree error_flag = gg_define_int(0);
5747
5748 for(size_t i=0; i<nC; i++ )
5749 {
5750 TRACE1
5751 {
5752 char ach[48];
5753 sprintf(ach, "Processing target number " HOST_SIZE_T_PRINT_DEC,
5754 (fmt_size_t)i);
5755 TRACE1_INDENT
5756 TRACE1_TEXT(ach);
5757 }
5758 cbl_refer_t& destref( C[i].refer );
5759 cbl_round_t rounded = C[i].rounded;
5760 SHOW_PARSE
5761 {
5762 if(i)
5763 {
5764 SHOW_PARSE_INDENT
5765 }
5766 if( sourceref.field && is_figconst_low(sourceref.field) )
5767 {
5768 SHOW_PARSE_TEXT(" LOW-VALUE")
5769 }
5770 else if( sourceref.field && is_figconst_zero(sourceref.field) )
5771 {
5772 SHOW_PARSE_TEXT(" ZERO-VALUE")
5773 }
5774 else if( sourceref.field && is_figconst_space(sourceref.field) )
5775 {
5776 SHOW_PARSE_TEXT(" SPACE-VALUE")
5777 }
5778 else if( sourceref.field && is_figconst_quote(sourceref.field) )
5779 {
5780 SHOW_PARSE_TEXT(" QUOTE-VALUE")
5781 }
5782 else if( sourceref.field && is_figconst_high(sourceref.field) )
5783 {
5784 SHOW_PARSE_TEXT(" HIGH-VALUE")
5785 }
5786 else
5787 {
5788 SHOW_PARSE_REF(" ", sourceref)
5789 }
5790 SHOW_PARSE_REF(" TO ", destref)
5791 switch(rounded)
5792 {
5793 case away_from_zero_e:
5794 SHOW_PARSE_TEXT(" AWAY_FROM_ZERO")
5795 break;
5796 case nearest_toward_zero_e:
5797 SHOW_PARSE_TEXT(" NEAREST_TOWARD_ZERO")
5798 break;
5799 case toward_greater_e:
5800 SHOW_PARSE_TEXT(" TOWARD_GREATER")
5801 break;
5802 case toward_lesser_e:
5803 SHOW_PARSE_TEXT(" TOWARD_LESSER")
5804 break;
5805 case nearest_away_from_zero_e:
5806 SHOW_PARSE_TEXT(" NEAREST_AWAY_FROM_ZERO")
5807 break;
5808 case nearest_even_e:
5809 SHOW_PARSE_TEXT(" NEAREST_EVEN")
5810 break;
5811 case prohibited_e:
5812 SHOW_PARSE_TEXT(" PROHIBITED")
5813 break;
5814 case truncation_e:
5815 SHOW_PARSE_TEXT(" TRUNCATED")
5816 break;
5817 default:
5818 gcc_unreachable();
5819 break;
5820 }
5821 }
5822
5823 CHECK_FIELD(destref.field);
5824 CHECK_FIELD(sourceref.field);
5825
5826 // gg_printf("parser_assign: The compute_error_code is %d\n",
5827 // gg_cast(INT, compute_error->structs.compute_error->compute_error_code), NULL_TREE);
5828
5829 static tree erf = gg_define_variable(INT, "..pa_erf", vs_file_static);
5830 if( on_error )
5831 {
5832 // There is an ON ERROR clause. When there is an ON ERROR clause, and
5833 // there is an error, the TARGET values are to be left unchanged.
5834 IF(compute_error->structs.compute_error->compute_error_code,
5835 ne_op,
5836 integer_zero_node )
5837 {
5838 // There was an error, so we do NOT replace the destref with the
5839 // sourceref value
5840 TRACE1
5841 {
5842 TRACE1_INDENT
5843 TRACE1_TEXT("on_error clause; computional error occurred")
5844 }
5845 }
5846 ELSE
5847 {
5848 TRACE1
5849 {
5850 TRACE1_INDENT
5851 TRACE1_TEXT("on_error clause; no computational error")
5852 }
5853 // There was no computational error. Call the move routine that does
5854 // not replace the target when there is a size error:
5855 TREEPLET tsource;
5856 treeplet_fill_source(tsource, sourceref);
5857 static bool check_for_error = true;
5858 move_helper(erf,
5859 destref,
5860 sourceref,
5861 tsource,
5862 rounded,
5863 check_for_error,
5864 true);
5865
5866 gg_assign(error_flag, gg_bitwise_or(error_flag, erf));
5867 IF(error_flag, ne_op, integer_zero_node)
5868 {
5869 TRACE1
5870 {
5871 TRACE1_INDENT
5872 TRACE1_TEXT("on_error clause; a move error occurred")
5873 }
5874 // There was an error during the move. Set the exception status
5875 // information:
5876 gg_call( VOID,
5877 "__gg__process_compute_error",
5878 build_int_cst_type(INT, compute_error_truncate),
5879 NULL_TREE);
5880 // But because there is an ON ERROR clause, suppress DECLARATIVE
5881 // processing
5882 gg_assign(var_decl_exception_code, integer_zero_node);
5883 }
5884 ELSE
5885 {
5886 TRACE1
5887 {
5888 TRACE1_INDENT
5889 TRACE1_TEXT("on_error clause; no move")
5890 }
5891 }
5892 ENDIF
5893 }
5894 ENDIF
5895 }
5896 else
5897 {
5898 // There is no ON_ERROR clause, so we do the truncation type move, but
5899 // with one exception. If the error was an exponentiation error that
5900 // resulted in a NaN, we *don't* do the move:
5901
5902 IF( gg_bitwise_and( compute_error->structs.compute_error->compute_error_code,
5903 build_int_cst_type(INT,
5904 compute_error_exp_minus_by_frac
5905 | compute_error_divide_by_zero)),
5906 ne_op,
5907 integer_zero_node )
5908 {
5909 // It was a NaN, so don't do the move
5910 TRACE1
5911 {
5912 TRACE1_INDENT
5913 TRACE1_TEXT("Not moving the NaN")
5914 }
5915 }
5916 ELSE
5917 {
5918 TRACE1
5919 {
5920 TRACE1_INDENT
5921 TRACE1_TEXT("Doing the move")
5922 }
5923 TREEPLET tsource;
5924 treeplet_fill_source(tsource, sourceref);
5925 static bool check_for_error = true;
5926 move_helper(erf,
5927 destref,
5928 sourceref,
5929 tsource,
5930 rounded,
5931 check_for_error,
5932 false);
5933 gg_assign(error_flag, gg_bitwise_or(error_flag, erf));
5934 IF(error_flag, ne_op, integer_zero_node)
5935 {
5936 // There was an error during the move. Set the exception status
5937 // information:
5938 TRACE1
5939 {
5940 TRACE1_INDENT
5941 TRACE1_TEXT("Error during the move; calling __gg__process_compute_error")
5942 }
5943 gg_call( VOID,
5944 "__gg__process_compute_error",
5945 build_int_cst_type(INT, compute_error_truncate),
5946 NULL_TREE);
5947 }
5948 ELSE
5949 {
5950 }
5951 ENDIF
5952 }
5953 ENDIF
5954 }
5955 TRACE1
5956 {
5957 TRACE1_INDENT
5958 TRACE1_FIELD("source ", sourceref.field, "")
5959 TRACE1_INDENT
5960 TRACE1_FIELD("dest ", destref.field, "")
5961 TRACE1_END
5962 }
5963 }
5964
5965 if( on_error )
5966 {
5967 SHOW_PARSE
5968 {
5969 SHOW_PARSE_INDENT
5970 SHOW_PARSE_TEXT(" Laying down on_error GOTO into")
5971 SHOW_PARSE_LABEL_OK(" ", on_error)
5972 }
5973 IF( gg_bitwise_or(error_flag,
5974 compute_error->structs.compute_error->compute_error_code),
5975 ne_op,
5976 integer_zero_node )
5977 {
5978 gg_append_statement( on_error->structs.arith_error->into.go_to );
5979 }
5980 ELSE
5981 ENDIF
5982 }
5983 else
5984 {
5985 // We weren't given an explicit ON SIZE ERROR label, so we need to go
5986 // with the NO ERROR CLAUSE behavior
5987 if( compute_error )
5988 {
5989 gg_call( VOID,
5990 "__gg__process_compute_error",
5991 compute_error->structs.compute_error->compute_error_code,
5992 NULL_TREE);
5993 }
5994 }
5995
5996 if( not_error )
5997 {
5998 SHOW_PARSE
5999 {
6000 SHOW_PARSE_INDENT
6001 SHOW_PARSE_TEXT(" Laying down not_error GOTO into")
6002 SHOW_PARSE_LABEL_OK(" ", not_error)
6003 }
6004 IF( compute_error->structs.compute_error->compute_error_code, eq_op, integer_zero_node )
6005 {
6006 gg_append_statement( not_error->structs.arith_error->into.go_to );
6007 }
6008 ELSE
6009 ENDIF
6010 }
6011
6012 if( on_error )
6013 {
6014 SHOW_PARSE
6015 {
6016 SHOW_PARSE_INDENT
6017 SHOW_PARSE_TEXT(" Laying down on_error LABEL BOTTOM:")
6018 SHOW_PARSE_LABEL_OK(" ", on_error)
6019 }
6020 gg_append_statement( on_error->structs.arith_error->bottom.label );
6021 }
6022
6023 if( not_error )
6024 {
6025 SHOW_PARSE
6026 {
6027 SHOW_PARSE_INDENT
6028 SHOW_PARSE_TEXT(" Laying down not_error LABEL BOTTOM:")
6029 SHOW_PARSE_LABEL_OK(" ", not_error)
6030 }
6031 gg_append_statement( not_error->structs.arith_error->bottom.label );
6032 }
6033
6034 SHOW_PARSE
6035 {
6036 SHOW_PARSE_END
6037 }
6038 }
6039
6040 static cbl_figconst_t
6041 is_figconst_t(const cbl_field_t *field)
6042 {
6043 cbl_figconst_t figconst = (cbl_figconst_t)(field->attr & FIGCONST_MASK);
6044 return figconst;
6045 }
6046
6047 static cbl_figconst_t
6048 is_figconst(const cbl_refer_t &sourceref)
6049 {
6050 return is_figconst_t(sourceref.field);
6051 }
6052
6053 void
6054 parser_move(cbl_refer_t destref,
6055 cbl_refer_t sourceref,
6056 cbl_round_t rounded,
6057 bool skip_fill_from // Defaults to false
6058 )
6059 {
6060 Analyze();
6061 SHOW_PARSE
6062 {
6063 SHOW_PARSE_HEADER
6064 if( sourceref.field && is_figconst_low(sourceref.field) )
6065 {
6066 SHOW_PARSE_TEXT(" LOW-VALUE")
6067 }
6068 else if( sourceref.field && is_figconst_zero(sourceref.field) )
6069 {
6070 SHOW_PARSE_TEXT(" ZERO-VALUE")
6071 }
6072 else if( sourceref.field && is_figconst_space(sourceref.field) )
6073 {
6074 SHOW_PARSE_TEXT(" SPACE-VALUE")
6075 }
6076 else if( sourceref.field && is_figconst_quote(sourceref.field) )
6077 {
6078 SHOW_PARSE_TEXT(" QUOTE-VALUE")
6079 }
6080 else if( sourceref.field && is_figconst_high(sourceref.field) )
6081 {
6082 SHOW_PARSE_TEXT(" HIGH-VALUE")
6083 }
6084 else
6085 {
6086 SHOW_PARSE_REF(" ", sourceref)
6087 }
6088 SHOW_PARSE_REF(" TO ", destref)
6089 switch(rounded)
6090 {
6091 case away_from_zero_e:
6092 SHOW_PARSE_TEXT(" AWAY_FROM_ZERO")
6093 break;
6094 case nearest_toward_zero_e:
6095 SHOW_PARSE_TEXT(" NEAREST_TOWARD_ZERO")
6096 break;
6097 case toward_greater_e:
6098 SHOW_PARSE_TEXT(" TOWARD_GREATER")
6099 break;
6100 case toward_lesser_e:
6101 SHOW_PARSE_TEXT(" TOWARD_LESSER")
6102 break;
6103 case nearest_away_from_zero_e:
6104 SHOW_PARSE_TEXT(" NEAREST_AWAY_FROM_ZERO")
6105 break;
6106 case nearest_even_e:
6107 SHOW_PARSE_TEXT(" NEAREST_EVEN")
6108 break;
6109 case prohibited_e:
6110 SHOW_PARSE_TEXT(" PROHIBITED")
6111 break;
6112 case truncation_e:
6113 SHOW_PARSE_TEXT(" TRUNCATED")
6114 break;
6115 default:
6116 gcc_unreachable();
6117 break;
6118 }
6119 SHOW_PARSE_END
6120 }
6121
6122 if( !skip_fill_from )
6123 {
6124 cbl_figconst_t figconst = is_figconst(sourceref);
6125 if( figconst )
6126 {
6127 sourceref.all = true;
6128 }
6129 }
6130
6131 TRACE1
6132 {
6133 TRACE1_HEADER
6134 TRACE1_TEXT("About to call move_helper")
6135 }
6136 TREEPLET tsource;
6137 treeplet_fill_source(tsource, sourceref);
6138 static bool dont_check_for_error = false;
6139 move_helper(NULL, destref, sourceref, tsource, rounded, dont_check_for_error );
6140
6141 TRACE1
6142 {
6143 TRACE1_INDENT
6144 TRACE1_REFER_INFO("source ", sourceref)
6145 TRACE1_INDENT
6146 TRACE1_REFER_INFO("dest ", destref)
6147 TRACE1_END
6148 }
6149 }
6150
6151 static
6152 void
6153 parser_move_multi(cbl_refer_t destref,
6154 cbl_refer_t sourceref,
6155 TREEPLET tsource,
6156 cbl_round_t rounded,
6157 bool skip_fill_from )
6158 {
6159 Analyze();
6160 SHOW_PARSE
6161 {
6162 SHOW_PARSE_HEADER
6163 if( sourceref.field && is_figconst_low(sourceref.field) )
6164 {
6165 SHOW_PARSE_TEXT(" LOW-VALUE")
6166 }
6167 else if( sourceref.field && is_figconst_zero(sourceref.field) )
6168 {
6169 SHOW_PARSE_TEXT(" ZERO-VALUE")
6170 }
6171 else if( sourceref.field && is_figconst_space(sourceref.field) )
6172 {
6173 SHOW_PARSE_TEXT(" SPACE-VALUE")
6174 }
6175 else if( sourceref.field && is_figconst_quote(sourceref.field) )
6176 {
6177 SHOW_PARSE_TEXT(" QUOTE-VALUE")
6178 }
6179 else if( sourceref.field && is_figconst_high(sourceref.field) )
6180 {
6181 SHOW_PARSE_TEXT(" HIGH-VALUE")
6182 }
6183 else
6184 {
6185 SHOW_PARSE_REF(" ", sourceref)
6186 }
6187 SHOW_PARSE_REF(" TO ", destref)
6188 switch(rounded)
6189 {
6190 case away_from_zero_e:
6191 SHOW_PARSE_TEXT(" AWAY_FROM_ZERO")
6192 break;
6193 case nearest_toward_zero_e:
6194 SHOW_PARSE_TEXT(" NEAREST_TOWARD_ZERO")
6195 break;
6196 case toward_greater_e:
6197 SHOW_PARSE_TEXT(" TOWARD_GREATER")
6198 break;
6199 case toward_lesser_e:
6200 SHOW_PARSE_TEXT(" TOWARD_LESSER")
6201 break;
6202 case nearest_away_from_zero_e:
6203 SHOW_PARSE_TEXT(" NEAREST_AWAY_FROM_ZERO")
6204 break;
6205 case nearest_even_e:
6206 SHOW_PARSE_TEXT(" NEAREST_EVEN")
6207 break;
6208 case prohibited_e:
6209 SHOW_PARSE_TEXT(" PROHIBITED")
6210 break;
6211 case truncation_e:
6212 SHOW_PARSE_TEXT(" TRUNCATED")
6213 break;
6214 default:
6215 gcc_unreachable();
6216 break;
6217 }
6218 SHOW_PARSE_END
6219 }
6220
6221 if( !skip_fill_from )
6222 {
6223 cbl_figconst_t figconst = is_figconst(sourceref);
6224 if( figconst )
6225 {
6226 sourceref.all = true;
6227 }
6228 }
6229
6230 TRACE1
6231 {
6232 TRACE1_HEADER
6233 TRACE1_TEXT("About to call move_helper")
6234 }
6235
6236 static bool dont_check_for_error = false;
6237 move_helper(NULL, destref, sourceref, tsource, rounded, dont_check_for_error );
6238
6239 TRACE1
6240 {
6241 TRACE1_INDENT
6242 TRACE1_REFER_INFO("source ", sourceref)
6243 TRACE1_INDENT
6244 TRACE1_REFER_INFO("dest ", destref)
6245 TRACE1_END
6246 }
6247 }
6248
6249 void
6250 parser_move(size_t ntgt, cbl_refer_t *tgts, cbl_refer_t src, cbl_round_t rounded)
6251 {
6252 if( mode_syntax_only() ) return;
6253
6254 cbl_figconst_t figconst = is_figconst(src);
6255 if( figconst )
6256 {
6257 src.all = true;
6258 }
6259 TREEPLET tsource;
6260 treeplet_fill_source(tsource, src);
6261 static const bool skip_fill_from = true;
6262 for( cbl_refer_t *p=tgts; p < tgts + ntgt; p++ )
6263 {
6264 parser_move_multi(*p, src, tsource, rounded, skip_fill_from);
6265 }
6266 }
6267
6268 /*
6269 * "nelem" represents the number of elements in the table.
6270 * "src" is the already-initialized first element of the table
6271 * to be initialized. If nspan == 0, copy the whole record because
6272 * the record either has no filler, or WITH FILLER was specified.
6273 * Otherwise, the spans array comprises a set of {offset,end+1} pairs
6274 * representing sequences of consecutive non-FILLER fields.
6275 *
6276 * "table" is the symbol table index for the table being initialized.
6277 * It may appear in a subsequent call as part of the (sub)tbls array,
6278 * if it is nested in a higher-level table.
6279 */
6280 void
6281 parser_initialize_table(size_t nelem,
6282 cbl_refer_t src,
6283 size_t nspan,
6284 const cbl_bytespan_t spans[],
6285 size_t table, // symbol table index
6286 size_t ntbl,
6287 const cbl_subtable_t tbls[])
6288 {
6289 if( mode_syntax_only() ) return;
6290
6291 SHOW_PARSE
6292 {
6293 SHOW_PARSE_HEADER
6294 SHOW_PARSE_END
6295 }
6296 TRACE1
6297 {
6298 TRACE1_HEADER
6299 TRACE1_REFER("src: ", src, " ")
6300 TRACE1_END
6301 }
6302 typedef size_t span_t[2];
6303 static_assert(sizeof(spans[0]) == sizeof(span_t), "pair size wrong");
6304 static tree tspans = gg_define_variable(SIZE_T_P,
6305 "..pit_v1",
6306 vs_file_static);
6307 static tree ttbls = gg_define_variable(SIZE_T_P,
6308 "..pit_v2",
6309 vs_file_static);
6310 gg_assign(tspans,
6311 build_array_of_size_t(2*nspan,
6312 reinterpret_cast<const size_t *>(spans)));
6313 gg_assign(ttbls,
6314 build_array_of_size_t(2*ntbl,
6315 reinterpret_cast<const size_t *>(tbls)));
6316
6317 gg_call(VOID,
6318 "__gg__mirror_range",
6319 build_int_cst_type(SIZE_T, nelem),
6320 gg_get_address_of(src.field->var_decl_node),
6321 refer_offset(src),
6322 build_int_cst_type(SIZE_T, nspan),
6323 tspans,
6324 build_int_cst_type(SIZE_T, table),
6325 build_int_cst_type(SIZE_T, ntbl),
6326 ttbls,
6327 NULL_TREE);
6328
6329 gg_free(tspans);
6330 gg_free(ttbls);
6331 }
6332
6333 static
6334 tree
6335 tree_type_from_field_type(cbl_field_t *field, size_t &nbytes)
6336 {
6337 /* This routine is used to determine what action is taken with type of a
6338 CALL ... USING <var> and the matching PROCEDURE DIVISION USING <var> of
6339 a PROGRAM-ID or FUNCTION-ID
6340 */
6341 tree retval = COBOL_FUNCTION_RETURN_TYPE;
6342 nbytes = 8;
6343 if( field )
6344 {
6345 // This maps a Fldxxx to a C-style variable type:
6346 switch(field->type)
6347 {
6348 case FldGroup:
6349 case FldAlphanumeric:
6350 case FldAlphaEdited:
6351 case FldNumericEdited:
6352 retval = CHAR_P;
6353 nbytes = field->data.capacity;
6354 break;
6355
6356 case FldNumericDisplay:
6357 case FldNumericBinary:
6358 case FldPacked:
6359 if( field->data.digits > 18 )
6360 {
6361 retval = UINT128;
6362 nbytes = 16;
6363 }
6364 else
6365 {
6366 retval = SIZE_T;
6367 nbytes = 8;
6368 }
6369 break;
6370
6371 case FldNumericBin5:
6372 case FldIndex:
6373 case FldPointer:
6374 if( field->data.capacity > 8 )
6375 {
6376 retval = UINT128;
6377 nbytes = 16;
6378 }
6379 else
6380 {
6381 retval = SIZE_T;
6382 nbytes = 8;
6383 }
6384 break;
6385
6386 case FldFloat:
6387 if( field->data.capacity == 8 )
6388 {
6389 retval = DOUBLE;
6390 nbytes = 8;
6391 }
6392 else if( field->data.capacity == 4 )
6393 {
6394 retval = FLOAT;
6395 nbytes = 4;
6396 }
6397 else
6398 {
6399 retval = FLOAT128;
6400 nbytes = 16;
6401 }
6402 break;
6403
6404 case FldLiteralN:
6405 // Assume a 64-bit signed integer. This happens for GOBACK STATUS 101,
6406 // the like
6407 retval = LONG;
6408 nbytes = 8;
6409 break;
6410
6411 default:
6412 cbl_internal_error( "%s: Invalid field type %s:",
6413 __func__,
6414 cbl_field_type_str(field->type));
6415 break;
6416 }
6417 if( retval == SIZE_T && field->attr & signable_e )
6418 {
6419 retval = SSIZE_T;
6420 }
6421 if( retval == UINT128 && field->attr & signable_e )
6422 {
6423 retval = INT128;
6424 }
6425 }
6426 return retval;
6427 }
6428
6429 static void
6430 restore_local_variables()
6431 {
6432 gg_call(VOID,
6433 "__gg__pop_local_variables",
6434 NULL_TREE);
6435 gg_decrement(var_decl_unique_prog_id);
6436 }
6437
6438 static inline bool
6439 is_valuable( cbl_field_type_t type ) {
6440 /* The name of this routine is a play on words, in English. It doesn't
6441 mean "Is worth a lot". It means "Can be converted to a value." */
6442 switch ( type ) {
6443 case FldInvalid:
6444 case FldGroup:
6445 case FldAlphanumeric:
6446 case FldNumericEdited:
6447 case FldLiteralA:
6448 case FldClass:
6449 case FldConditional:
6450 case FldForward:
6451 case FldSwitch:
6452 case FldDisplay:
6453 case FldBlob:
6454 return false;
6455 // These are variable types that have to be converted from their
6456 // COBOL form to a little-endian binary representation so that they
6457 // can be conveyed BY CONTENT/BY VALUE in a CALL or user-defined
6458 // function activation.
6459 case FldAlphaEdited:
6460 case FldNumericDisplay:
6461 case FldNumericBinary:
6462 case FldFloat:
6463 case FldPacked:
6464 case FldNumericBin5:
6465 case FldLiteralN:
6466 case FldIndex:
6467 case FldPointer:
6468 return true;
6469 }
6470 cbl_internal_error( "%s:%d: invalid %<symbol_type_t%> %d", __func__, __LINE__, type );
6471 return false;
6472 }
6473
6474 void parser_sleep(const cbl_refer_t &seconds)
6475 {
6476 if( seconds.field )
6477 {
6478 gg_get_address_of(seconds.field->var_decl_node);
6479 //refer_offset(seconds);
6480 //refer_size_source(seconds);
6481
6482 gg_call(VOID,
6483 "__gg__sleep",
6484 gg_get_address_of(seconds.field->var_decl_node),
6485 refer_offset(seconds),
6486 refer_size_source(seconds),
6487 NULL_TREE);
6488 }
6489 else
6490 {
6491 // This is a naked place-holding CONTINUE. Generate some do-nothing
6492 // code that will stick some .LOC information into the assembly language,
6493 // so that GDB-COBOL can display the CONTINUE statement.
6494 insert_nop(103);
6495 }
6496 }
6497
6498 void
6499 parser_exit_program(void) // exits back to COBOL only, else continue
6500 {
6501 static cbl_label_t this_program = {};
6502 static cbl_refer_t magic_refer(&this_program, false);
6503 parser_exit( magic_refer );
6504 }
6505
6506 /*
6507 * If RETURNING was specified, the field is provided as an argument, no lookup
6508 * necessary. refer.field == NULL means exit(0) unless ec != ec_none_e.
6509 * If ec == ec_all_e, that indicates RAISING LAST EXCEPTION was used.
6510 */
6511
6512 static
6513 void
6514 program_end_stuff(cbl_refer_t refer, ec_type_t ec)
6515 {
6516 // This is the moral equivalent of a C "return xyz;".
6517
6518 // There cannot be both a non-zero exit status and an exception condition.
6519 gcc_assert( !(ec != ec_none_e && refer.field != NULL) );
6520
6521 gg_call(VOID,
6522 "__gg__pseudo_return_flush",
6523 NULL_TREE);
6524
6525 cbl_field_t *returner = refer.field ? refer.field : current_function->returning;
6526
6527 if( returner )
6528 {
6529 cbl_field_type_t field_type = returner->type;
6530 size_t nbytes = 0;
6531 tree return_type = tree_type_from_field_type(returner,
6532 nbytes);
6533 tree retval = gg_define_variable(return_type);
6534
6535 gg_assign(retval, gg_cast(return_type, integer_zero_node));
6536
6537 if( is_valuable( field_type ) )
6538 {
6539 // The field being returned is numeric.
6540 if( field_type == FldNumericBin5
6541 || field_type == FldFloat
6542 || field_type == FldPointer
6543 || field_type == FldIndex )
6544 {
6545 // These are easily handled because they are all little-endian.
6546 gg_memcpy(gg_get_address_of(retval),
6547 member(returner, "data"),
6548 build_int_cst_type( SIZE_T,
6549 std::min(nbytes, (size_t)returner->data.capacity)));
6550 }
6551 else
6552 {
6553 // The field_type has a PICTURE string, so we need to convert from the
6554 // COBOL form to little-endian binary:
6555 tree value = gg_define_int128();
6556 get_binary_value( value,
6557 NULL,
6558 returner,
6559 size_t_zero_node);
6560 gg_memcpy(gg_get_address_of(retval),
6561 gg_get_address_of(value),
6562 build_int_cst_type(SIZE_T, nbytes));
6563 }
6564 restore_local_variables();
6565 gg_return(retval);
6566 }
6567 else
6568 {
6569 // The RETURNING type is a group or alphanumeric
6570
6571 // The byte array to be returned is in returning, which is a local
6572 // variable on the stack. We need to make a copy of it to avoid the
6573 // error of returning a pointer to data on the stack.
6574
6575 tree array_type = build_array_type_nelts(UCHAR,
6576 returner->data.capacity);
6577 tree array = gg_define_variable(array_type, vs_static);
6578 gg_memcpy(gg_get_address_of(array),
6579 member(returner->var_decl_node, "data"),
6580 member(returner->var_decl_node, "capacity"));
6581
6582 tree actual = gg_cast(COBOL_FUNCTION_RETURN_TYPE, gg_get_address_of(array));
6583
6584 restore_local_variables();
6585 gg_return(actual);
6586 }
6587 }
6588 else
6589 {
6590 // There is no explicit value. This means, by default (according to)
6591 // IBM), we return the value found in RETURN-CODE:
6592 tree value = gg_define_variable(COBOL_FUNCTION_RETURN_TYPE);
6593 gg_assign(value,
6594 gg_cast(COBOL_FUNCTION_RETURN_TYPE,
6595 var_decl_return_code));
6596 restore_local_variables();
6597 gg_return(gg_cast(COBOL_FUNCTION_RETURN_TYPE, value));
6598 }
6599 }
6600
6601 void
6602 parser_exit( const cbl_refer_t& refer, ec_type_t ec )
6603 {
6604 Analyze();
6605 SHOW_PARSE
6606 {
6607 SHOW_PARSE_HEADER
6608 if( gg_trans_unit.function_stack.size()
6609 && current_function->returning
6610 && !refer.field)
6611 {
6612 // ->returning works only if there is no refer.field
6613 SHOW_PARSE_FIELD(" RETURNING ", current_function->returning);
6614 }
6615 if( gg_trans_unit.function_stack.size() && refer.field )
6616 {
6617 SHOW_PARSE_FIELD(" WITH STATUS ", refer.field);
6618 }
6619 if( gg_trans_unit.function_stack.size() && refer.prog_func )
6620 {
6621 SHOW_PARSE_TEXT(" refer.prog_func is non-zero")
6622 }
6623
6624 SHOW_PARSE_END
6625 }
6626 TRACE1
6627 {
6628 TRACE1_HEADER
6629 TRACE1_END
6630 }
6631
6632 if( refer.prog_func )
6633 {
6634 // We are processing EXIT PROGRAM. If main() called us, we need to do
6635 // nothing. Otherwise, this is a return
6636 IF( current_function->called_by_main_counter, eq_op, integer_zero_node )
6637 {
6638 // This function wasn't called by main, so we treat it like a GOBACK
6639 program_end_stuff(refer, ec);
6640 }
6641 ELSE
6642 {
6643 // This function was called by main. Is it the first call, or is it
6644 // recursive?
6645 IF( current_function->called_by_main_counter, gt_op, integer_one_node )
6646 {
6647 // This was a recursive call into the function originally called by
6648 // main. Because we are under the control of a calling program, we
6649 // treat this like a GOBACK
6650 program_end_stuff(refer, ec);
6651 }
6652 ELSE
6653 {
6654 // We are not under the control of a calling program, meaning that we
6655 // were called by main(). So, we do nothing, meaning we behave like
6656 // a CONTINUE.
6657 }
6658 ENDIF
6659 }
6660 ENDIF
6661 }
6662 else
6663 {
6664 IF( current_function->called_by_main_counter, gt_op, integer_zero_node )
6665 {
6666 // This wasn't an EXIT PROGRAM. But in the case where we are the program
6667 // that was called by main(), we need to do some bookkeeping so that we
6668 // respond properly to an EXIT PROGRAM should one appear
6669 gg_decrement(current_function->called_by_main_counter);
6670 }
6671 ELSE
6672 {
6673 }
6674 ENDIF
6675 program_end_stuff(refer, ec);
6676 }
6677 }
6678
6679 static void
6680 walk_initialization(cbl_field_t *field, bool initialized, bool deallocate)
6681 {
6682 if( !(field->attr & based_e) )
6683 {
6684 // We are concerned only with BASED variables
6685 return;
6686 }
6687 symbol_elem_t *e = symbol_at(field_index(field));
6688 bool first_time = true;
6689 while( e < symbols_end() )
6690 {
6691 symbol_elem_t& element = *e++;
6692 if( element.type == SymField )
6693 {
6694 cbl_field_t *this_one = cbl_field_of(&element);
6695 if( !first_time )
6696 {
6697 if( this_one->level == LEVEL01 || this_one->level == LEVEL77 )
6698 {
6699 // Having encountered the next 01 or 77, we are done
6700 break;
6701 }
6702 }
6703 first_time = false;
6704 if( this_one->level == 00 )
6705 {
6706 // Ignore LEVEL00 "INDEXED BY" variables
6707 continue;
6708 }
6709 if(deallocate)
6710 {
6711 gg_assign(member(this_one->var_decl_node, "data"),
6712 gg_cast(UCHAR_P, null_pointer_node));
6713 }
6714 else
6715 {
6716 gg_assign(member(this_one->var_decl_node, "data"),
6717 gg_add(member(field->var_decl_node, "data"),
6718 build_int_cst_type(SIZE_T, this_one->offset)));
6719 if( this_one->level == 66
6720 || this_one->level == 88
6721 || symbol_redefines(this_one) )
6722 {
6723 continue;
6724 }
6725 if( !initialized )
6726 {
6727 // This is ALLOCATE Rule 9) in ISO 2023
6728 if( this_one->type == FldPointer )
6729 {
6730 gg_memset(member(this_one->var_decl_node, "data"),
6731 integer_zero_node,
6732 build_int_cst_type(SIZE_T, this_one->data.capacity));
6733 }
6734 }
6735 }
6736 }
6737 }
6738 }
6739
6740 void
6741 parser_allocate(cbl_refer_t size_or_based,
6742 cbl_refer_t returning,
6743 bool initialized )
6744 {
6745 /*
6746 * If the 1st parameter has based_e attribute, the field it is based on defines
6747 * the number of bytes to allocate. In that case, "returning" is optional and
6748 * may have a NULL field. Otherwise the 1st parameter is a numeric value and
6749 * allocated space is assigned to "returning", which is of type FldPointer.
6750 */
6751
6752 Analyze();
6753 SHOW_PARSE
6754 {
6755 SHOW_PARSE_HEADER
6756 SHOW_PARSE_REF(" size_or_based from:", size_or_based)
6757 SHOW_PARSE_INDENT
6758 SHOW_PARSE_REF("returning: ", returning)
6759 SHOW_PARSE_END
6760 }
6761 TRACE1
6762 {
6763 TRACE1_HEADER
6764 TRACE1_REFER("size_or_based: ", size_or_based, "");
6765 TRACE1_INDENT
6766 TRACE1_REFER("returning: ", size_or_based, "");
6767 TRACE1_END
6768 }
6769
6770 if( returning.field )
6771 {
6772 // If there is a returning, it has to be a pointer
6773 gcc_assert(returning.field->type == FldPointer);
6774 }
6775
6776 if( !(size_or_based.field->attr & based_e) )
6777 {
6778 // If the first is not based, then there must be a returning
6779 gcc_assert(returning.field);
6780 }
6781
6782 cbl_field_t *f_working = current_options().initial_working();
6783 cbl_field_t *f_local = current_options().initial_local();
6784
6785 int default_byte = wsclear() ? *wsclear() : -1;
6786
6787 gg_call(VOID,
6788 "__gg__allocate",
6789 gg_get_address_of(size_or_based.field->var_decl_node),
6790 refer_offset(size_or_based) ,
6791 initialized ? integer_one_node : integer_zero_node,
6792 build_int_cst_type(INT, default_byte),
6793 f_working ? gg_get_address_of(f_working->var_decl_node) : null_pointer_node,
6794 f_local ? gg_get_address_of(f_local-> var_decl_node) : null_pointer_node,
6795 returning.field ? gg_get_address_of(returning.field->var_decl_node)
6796 : null_pointer_node,
6797 returning.field ? refer_offset(returning)
6798 : size_t_zero_node,
6799 NULL_TREE);
6800 walk_initialization(size_or_based.field, initialized, false);
6801 }
6802
6803 void
6804 parser_free( size_t n, cbl_refer_t refers[] )
6805 {
6806 if( mode_syntax_only() ) return; // Normally handled by SHOW_PARSE, if present
6807
6808 Analyze();
6809 for( auto p = refers; p < refers + n; p++ )
6810 {
6811 gcc_assert( ! p->all );
6812 gcc_assert( ! p->is_refmod_reference() );
6813 if( !(p->field->type == FldPointer || p->addr_of || (p->field->attr & based_e)) )
6814 {
6815 dbgmsg("Deallocating %s means it has to be FldPointer or addr_of or based_e",
6816 p->field->name);
6817 }
6818 gcc_assert( p->field->type == FldPointer || p->addr_of || (p->field->attr & based_e) );
6819
6820 gg_call(VOID,
6821 "__gg__deallocate",
6822 gg_get_address_of(p->field->var_decl_node),
6823 refer_offset(*p),
6824 p->addr_of ? integer_one_node : integer_zero_node,
6825 NULL_TREE);
6826 walk_initialization(p->field, false, true);
6827 }
6828 }
6829
6830 void
6831 parser_arith_error(cbl_label_t *arithmetic_label)
6832 {
6833 // We can't use Analyze() on this one, because the exit ends up being laid
6834 // down before the enter when the goto logic gets untangled by the compiler.
6835
6836 // We are entering either SIZE ERROR or NOT SIZE ERROR code
6837 RETURN_IF_PARSE_ONLY;
6838
6839 SHOW_PARSE
6840 {
6841 SHOW_PARSE_HEADER
6842 SHOW_PARSE_TEXT(" Laying down GOTO OVER")
6843 SHOW_PARSE_LABEL(" ", arithmetic_label)
6844 SHOW_PARSE_INDENT
6845 SHOW_PARSE_TEXT(" Laying down LABEL INTO:")
6846 SHOW_PARSE_LABEL(" ", arithmetic_label)
6847 SHOW_PARSE_END
6848 }
6849
6850 CHECK_LABEL(arithmetic_label);
6851
6852 set_up_on_exception_label(arithmetic_label);
6853
6854 // Jump over the [NOT] ON EXCEPTION code that is about to be laid down
6855 gg_append_statement( arithmetic_label->structs.arith_error->over.go_to );
6856 // Create the label that allows the following code to be executed at
6857 // when an ERROR, or NOT ERROR, has been determined to have taken place:
6858 gg_append_statement( arithmetic_label->structs.arith_error->into.label );
6859 }
6860
6861 void
6862 parser_arith_error_end(cbl_label_t *arithmetic_label)
6863 {
6864 // We can't use Analyze() on this one, because the exit ends up being laid
6865 // down before the enter when the goto logic gets untangled by the compiler.
6866
6867 // We have reached the end of the ERROR, or NOT ERROR, code.
6868
6869 SHOW_PARSE
6870 {
6871 SHOW_PARSE_HEADER
6872 SHOW_PARSE_TEXT(" Laying down GOTO BOTTOM")
6873 SHOW_PARSE_LABEL(" ", arithmetic_label)
6874 SHOW_PARSE_INDENT
6875 SHOW_PARSE_TEXT(" Laying down LABEL OVER:")
6876 SHOW_PARSE_LABEL(" ", arithmetic_label)
6877 SHOW_PARSE_END
6878 }
6879
6880 CHECK_LABEL(arithmetic_label);
6881
6882 // Jump to the end of the arithmetic code:
6883 gg_append_statement( arithmetic_label->structs.arith_error->bottom.go_to );
6884 // Lay down the label that allows the ERROR/NOT ERROR instructions
6885 // to exist in a lacuna that doesn't get executed unless somebody jumps
6886 // to it:
6887 gg_append_statement( arithmetic_label->structs.arith_error->over.label );
6888 }
6889
6890 static void
6891 propogate_linkage_offsets(cbl_field_t *field, tree base)
6892 {
6893 if( field->level == LEVEL01 || field->level == LEVEL77 )
6894 {
6895 field->data_decl_node = base;
6896 symbol_elem_t *e = symbol_at(field_index(field));
6897 // We already updated the data pointer of the first element:
6898 e += 1;
6899 while( e < symbols_end() )
6900 {
6901 symbol_elem_t& element = *e++;
6902 if( element.type == SymField )
6903 {
6904 cbl_field_t *this_one = cbl_field_of(&element);
6905 if( this_one->level == LEVEL01 || this_one->level == LEVEL77 )
6906 {
6907 // We have encountered another level 01/77. If this LEVEL 01 had a
6908 // parent, then we have to assume that this is a redefines of another
6909 // level 01/77.
6910 if( this_one->parent )
6911 {
6912 // And, gloriously and frighteningly, it can be handled by
6913 // recursion:
6914 propogate_linkage_offsets(this_one, base);
6915 }
6916 else
6917 {
6918 // Having encountered the next 01 or 77, we are done
6919 break;
6920 }
6921 }
6922 if( this_one->level == 00 )
6923 {
6924 // Ignore LEVEL00 "INDEXED BY" variables
6925 continue;
6926 }
6927 tree offset = gg_define_variable(SIZE_T);
6928 IF( base, eq_op, gg_cast(UCHAR_P, null_pointer_node) )
6929 {
6930 gg_assign(offset, size_t_zero_node);
6931 }
6932 ELSE
6933 {
6934 gg_assign(offset, member(this_one, "offset"));
6935 }
6936 ENDIF
6937 this_one->data_decl_node = base;
6938 member( this_one,
6939 "data",
6940 gg_add(base, offset));
6941 }
6942 }
6943 }
6944 }
6945
6946 static bool initialized_data = false;
6947 static void
6948 initialize_the_data()
6949 {
6950 if( initialized_data )
6951 {
6952 return;
6953 }
6954 initialized_data = true;
6955 // Here is where we initialize the run-time list of currency symbols:
6956 const char *default_currency = "$";
6957
6958 // This is one-time initialization of the libgcobol program state stack
6959 gg_call(VOID,
6960 "__gg__init_program_state",
6961 NULL_TREE);
6962
6963 __gg__currency_signs = __gg__ct_currency_signs;
6964 // We initialize currency both at compile time and run time
6965 __gg__currency_sign_init();
6966 gg_call(VOID,
6967 "__gg__currency_sign_init",
6968 NULL_TREE);
6969
6970 gg_call(VOID,
6971 "__gg__set_program_name",
6972 gg_string_literal( current_filename.back().c_str() ),
6973 NULL_TREE);
6974
6975 for(int symbol=0; symbol<256; symbol++)
6976 {
6977 const char *sign = symbol_currency(symbol);
6978 if( sign )
6979 {
6980 default_currency = NULL;
6981
6982 // Both compile-time and run-time
6983 __gg__currency_sign(symbol, sign);
6984 gg_call(VOID,
6985 "__gg__currency_sign",
6986 build_int_cst_type(INT, symbol),
6987 build_string_literal(strlen(sign)+1, sign),
6988 NULL_TREE);
6989 }
6990 }
6991 if( default_currency )
6992 {
6993 __gg__currency_sign(default_currency[0], default_currency);
6994 gg_call(VOID,
6995 "__gg__currency_sign",
6996 char_nodes[(int)default_currency[0]],
6997 gg_string_literal(default_currency),
6998 NULL_TREE);
6999 }
7000
7001 // It's time to tell the library about DECIMAL-POINT IS COMMA:
7002 if( symbol_decimal_point() == ',' )
7003 {
7004 __gg__decimal_point = ascii_comma ;
7005 __gg__decimal_separator = ascii_period ;
7006 gg_call(VOID,
7007 "__gg__decimal_point_is_comma",
7008 NULL_TREE);
7009 }
7010 }
7011
7012 void
7013 parser_division(cbl_division_t division,
7014 cbl_field_t *returning,
7015 size_t nusing,
7016 cbl_ffi_arg_t args[] )
7017 {
7018 // This is called when the parser enters a COBOL program DIVISION. See
7019 // parser_divide for the arithmetic operation.
7020
7021 if( mode_syntax_only() ) return;
7022
7023 // Do this before the SHOW_PARSE; it makes a little more sense when reviewing
7024 // the SHOW_PARSE output.
7025 if( division == identification_div_e )
7026 {
7027 initialized_data = false;
7028 if( gg_trans_unit.function_stack.size() >= 1 )
7029 {
7030 // This is a nested program. So, we need to tie off the current
7031 // section:
7032 leave_paragraph_internal();
7033 leave_section_internal();
7034 }
7035 }
7036
7037 if( division == environment_div_e )
7038 {
7039 initialized_data = false;
7040 }
7041
7042 SHOW_PARSE
7043 {
7044 SHOW_PARSE_HEADER
7045 SHOW_PARSE_TEXT(" ")
7046 switch(division)
7047 {
7048 case identification_div_e:
7049 SHOW_PARSE_TEXT("IDENTIFICATION")
7050 break;
7051 case environment_div_e:
7052 SHOW_PARSE_TEXT("ENVIRONMENT")
7053 break;
7054 case data_div_e:
7055 SHOW_PARSE_TEXT("DATA")
7056 break;
7057 case procedure_div_e:
7058 SHOW_PARSE_TEXT("PROCEDURE")
7059 break;
7060 }
7061
7062 SHOW_PARSE_END
7063 }
7064
7065 if( division == data_div_e )
7066 {
7067 Analyze();
7068 initialize_the_data();
7069 }
7070 if( division == environment_div_e )
7071 {
7072 Analyze();
7073 initialize_the_data();
7074 }
7075 else if( division == procedure_div_e )
7076 {
7077 Analyze();
7078 initialize_the_data();
7079
7080 // Do some symbol table index bookkeeping. current_program_index() is valid
7081 // at this point in time:
7082 current_function->our_symbol_table_index = current_program_index();
7083
7084 // We have some housekeeping to do to keep track of the list of functions
7085 // accessible by us:
7086
7087 // For every procedure, we need a variable that points to the list of
7088 // available program names.
7089
7090 // We need a pointer to the array of program names
7091 char ach[2*sizeof(cbl_name_t)];
7092 sprintf(ach,
7093 "..accessible_program_list_" HOST_SIZE_T_PRINT_DEC,
7094 (fmt_size_t)current_function->our_symbol_table_index);
7095 tree prog_list = gg_define_variable(build_pointer_type(CHAR_P),
7096 ach, vs_file_static);
7097
7098 // Likewise, we need a pointer to the array of pointers to functions:
7099 tree function_type =
7100 build_varargs_function_type_array( SIZE_T,
7101 0, // No parameters yet
7102 NULL); // And, hence, no types
7103 tree pointer_type = build_pointer_type(function_type);
7104 tree constructed_array_type = build_array_type_nelts(pointer_type, 1);
7105 sprintf(ach,
7106 "..accessible_program_pointers_" HOST_SIZE_T_PRINT_DEC,
7107 (fmt_size_t)current_function->our_symbol_table_index);
7108 tree prog_pointers = gg_define_variable(
7109 build_pointer_type(constructed_array_type),
7110 ach,
7111 vs_file_static);
7112 gg_call(VOID,
7113 "__gg__set_program_list",
7114 build_int_cst_type(INT, current_function->our_symbol_table_index),
7115 gg_get_address_of(prog_list),
7116 gg_get_address_of(prog_pointers),
7117 NULL_TREE);
7118
7119 if( gg_trans_unit.function_stack.size() == 1 )
7120 {
7121 gg_create_goto_pair(&label_list_out_goto,
7122 &label_list_out_label);
7123 gg_create_goto_pair(&label_list_back_goto,
7124 &label_list_back_label);
7125 gg_append_statement(label_list_out_goto);
7126 gg_append_statement(label_list_back_label);
7127 }
7128
7129 tree globals_are_initialized = gg_declare_variable( INT,
7130 "__gg__globals_are_initialized",
7131 NULL,
7132 vs_external_reference);
7133 IF( globals_are_initialized, eq_op, integer_zero_node )
7134 {
7135 // one-time initialization happens here
7136
7137 // We need to establish the initial value of the UPSI-1 switch register
7138 // We are using IBM's conventions:
7139 // https://www.ibm.com/docs/en/zvse/6.2?topic=SSB27H_6.2.0/fa2sf_communicate_appl_progs_via_job_control.html
7140 // UPSI 10000110 means that bits 0, 5, and 6 are on, which means that
7141 // SW-0, SW-5, and SW-6 are on.
7142 gg_call(VOID,
7143 "__gg__set_initial_switch_value",
7144 NULL_TREE);
7145
7146 // And then flag one-time initialization as having been done.
7147 gg_assign(globals_are_initialized, integer_one_node);
7148 }
7149 ELSE
7150 ENDIF
7151
7152 gg_append_statement(current_function->skip_init_label);
7153 // This is where we check to see if somebody tried to cancel us
7154 tree cancelled = gg_define_int();
7155 gg_assign(cancelled,
7156 gg_call_expr( INT,
7157 "__gg__is_canceled",
7158 gg_cast(SIZE_T,
7159 current_function->function_address),
7160 NULL_TREE));
7161 IF( cancelled, ne_op, integer_zero_node )
7162 {
7163 // Somebody flagged us for CANCEL, which means reinitialization, so we
7164 // need to find the _INITIALIZE_PROGRAM section label.
7165
7166 // gg_printf("Somebody wants to cancel %s\n",
7167 // gg_string_literal(current_function->our_unmangled_name),
7168 // NULL_TREE);
7169 const cbl_label_t *prog = cbl_label_of(symbol_at(current_program_index()));
7170 size_t initializer_index = prog->initial_section;
7171 cbl_label_t *initializer = cbl_label_of(symbol_at(initializer_index));
7172 parser_perform(initializer, true); // true means suppress nexting
7173 }
7174 ELSE
7175 ENDIF
7176
7177 // RETURNING variables are supposed to be in the linkage section, which
7178 // means that we didn't assign any storage to them during
7179 // parser_symbol_add(). We do that here.
7180
7181 // returning also needs to behave like local storage, even though it is
7182 // in linkage.
7183
7184 // This counter is used to help keep track of local variables
7185 gg_increment(var_decl_unique_prog_id);
7186 if( returning )
7187 {
7188 parser_local_add(returning);
7189 current_function->returning = returning;
7190
7191 size_t nbytes = 0;
7192 tree returning_type = tree_type_from_field_type(returning, nbytes);
7193 gg_modify_function_type(current_function->function_decl, returning_type);
7194 }
7195
7196 // Stash the returning variables for use during parser_return()
7197 current_function->returning = returning;
7198
7199 if( gg_trans_unit.function_stack.size() == 1 )
7200 {
7201 // We are entering a new top-level program, so we need to set
7202 // RETURN-CODE to zero
7203 gg_assign(var_decl_return_code, build_int_cst_type(SHORT, 0));
7204 }
7205
7206 // The parameters passed to this program might be 64 bits or 128 bits in
7207 // length. We establish those lengths based on the types of the target
7208 // for each USING.
7209
7210 for(size_t i=0; i<nusing; i++)
7211 {
7212 // This code is relevant at compile time. It takes each
7213 // expected formal parameter and tacks it onto the end of the
7214 // function's arguments chain.
7215
7216 sprintf(ach, "_p_%s", args[i].refer.field->name);
7217
7218 size_t nbytes = 0;
7219 tree par_type = tree_type_from_field_type(args[i].refer.field, nbytes);
7220 if( par_type == FLOAT )
7221 {
7222 par_type = SSIZE_T;
7223 }
7224 if( par_type == DOUBLE )
7225 {
7226 par_type = SSIZE_T;
7227 }
7228 if( par_type == FLOAT128 )
7229 {
7230 par_type = INT128;
7231 }
7232 chain_parameter_to_function(current_function->function_decl, par_type, ach);
7233 }
7234
7235 if( nusing )
7236 {
7237 // During the call, we saved the parameter_count and an array of variable
7238 // lengths. We need to look at those values if, and only if, one or more
7239 // of our USING arguments has an OPTIONAL flag or if one of our targets is
7240 // marked as VARYING.
7241 bool check_for_parameter_count = false;
7242 for(size_t i=0; i<nusing; i++)
7243 {
7244 if( args[i].optional )
7245 {
7246 check_for_parameter_count = true;
7247 break;
7248 }
7249 if( args[i].refer.field->attr & any_length_e )
7250 {
7251 check_for_parameter_count = true;
7252 break;
7253 }
7254 }
7255
7256 if( check_for_parameter_count )
7257 {
7258 IF( var_decl_call_parameter_signature,
7259 eq_op,
7260 gg_cast(CHAR_P, current_function->function_address) )
7261 {
7262 // We know to use var_decl_call_parameter_count, so unflag this
7263 // pointer to avoid problems in the ridiculous possibility of
7264 // COBOL-A calls C_B calls COBOL_A
7265 gg_assign(var_decl_call_parameter_signature,
7266 gg_cast(CHAR_P, null_pointer_node));
7267 }
7268 ELSE
7269 {
7270 // We were apparently called by a C routine, not a COBOL routine, so
7271 // make sure we don't get shortchanged by a count left behind from an
7272 // earlier COBOL call.
7273 gg_assign(var_decl_call_parameter_count,
7274 build_int_cst_type(INT, A_ZILLION));
7275 }
7276 ENDIF
7277 }
7278 else
7279 {
7280 // None of our parameters require a count, so make sure we don't get
7281 // bamboozled by a count left behind from an earlier COBOL call.
7282 gg_assign(var_decl_call_parameter_count,
7283 build_int_cst_type(INT, A_ZILLION));
7284 }
7285
7286 // There are 'nusing' elements in the PROCEDURE DIVISION USING list.
7287
7288 tree parameter = NULL_TREE;
7289 tree rt_i = gg_define_int();
7290 for(size_t i=0; i<nusing; i++)
7291 {
7292 // And this compiler code generates run-time execution code. The
7293 // generated code picks up, at run time, the variable we just
7294 // established in the chain at compile time.
7295
7296 // It makes more sense if you don't think about it too hard.
7297
7298 // We need to be able to restore prior arguments when doing recursive
7299 // calls:
7300 IF( member(args[i].refer.field->var_decl_node, "data"),
7301 ne_op,
7302 gg_cast(UCHAR_P, null_pointer_node) )
7303 {
7304 gg_call(VOID,
7305 "__gg__push_local_variable",
7306 gg_get_address_of(args[i].refer.field->var_decl_node),
7307 NULL_TREE);
7308 }
7309 ELSE
7310 ENDIF
7311
7312 tree base = gg_define_variable(UCHAR_P);
7313 gg_assign(rt_i, build_int_cst_type(INT, i));
7314 //gg_printf("The rt_i counter is %d\n", rt_i, NULL_TREE);
7315 IF( rt_i, lt_op , var_decl_call_parameter_count )
7316 {
7317 if( i == 0 )
7318 {
7319 // This is the first parameter.
7320 parameter = DECL_ARGUMENTS(current_function->function_decl);
7321 }
7322 else
7323 {
7324 // These are subsequent parameters
7325 parameter = TREE_CHAIN(parameter);
7326 }
7327 gg_assign(base, gg_cast(UCHAR_P, parameter));
7328
7329 if( args[i].refer.field->attr & any_length_e )
7330 {
7331 // gg_printf("side channel: Length of \"%s\" is %ld\n",
7332 // member(args[i].refer.field->var_decl_node, "name"),
7333 // gg_array_value(var_decl_call_parameter_lengths, rt_i),
7334 // NULL_TREE);
7335
7336 // Get the length from the global lengths[] side channel. Don't
7337 // forget to use the length mask on the table value.
7338 gg_assign(member(args[i].refer.field->var_decl_node, "capacity"),
7339 gg_array_value(var_decl_call_parameter_lengths, rt_i));
7340 }
7341 }
7342 ELSE
7343 {
7344 gg_assign(base, gg_cast(UCHAR_P, null_pointer_node));
7345 }
7346 ENDIF
7347
7348 // Arriving here means that we are processing an instruction like
7349 // this:
7350 // PROCEDURE DIVISION USING using[0] using[1] ... using using[nusing-1]
7351
7352 // When __gg__call_parameter_count is equal to A_ZILLION, then this is
7353 // an OTHER-TO-COBOL call and the var_decl_call_parameter_lengths array
7354 // is not valid
7355
7356 cbl_ffi_crv_t crv = args[i].crv;
7357 cbl_field_t *new_var = args[i].refer.field;
7358
7359 if( crv == by_value_e )
7360 {
7361 switch(new_var->type)
7362 {
7363 case FldGroup:
7364 case FldAlphanumeric:
7365 case FldAlphaEdited:
7366 case FldNumericEdited:
7367 crv = by_reference_e;
7368 break;
7369 default:
7370 break;
7371 }
7372 }
7373
7374 if( crv == by_value_e )
7375 {
7376 // 'parameter' is the 64-bit or 128-bit value that was placed on the stack
7377
7378 size_t nbytes;
7379 tree_type_from_field_type(new_var, nbytes);
7380 tree parm = gg_define_variable(INT128);
7381
7382 if( nbytes <= 8 )
7383 {
7384 // Our input is a 64-bit number
7385 if( new_var->attr & signable_e )
7386 {
7387 IF( gg_bitwise_and( gg_cast(SIZE_T, base),
7388 build_int_cst_type(SIZE_T, 0x8000000000000000ULL)),
7389 ne_op,
7390 gg_cast(SIZE_T, integer_zero_node) )
7391 {
7392 // Our input is a negative number
7393 gg_assign(parm, gg_cast(INT128, integer_minus_one_node));
7394 }
7395 ELSE
7396 {
7397 // Our input is a positive number
7398 gg_assign(parm, gg_cast(INT128, integer_zero_node));
7399 }
7400 ENDIF
7401 }
7402 else
7403 {
7404 // This is a 64-bit positive number:
7405 gg_assign(parm, gg_cast(INT128, integer_zero_node));
7406 }
7407 }
7408 // At this point, parm has been set to 0 or -1
7409
7410 gg_memcpy(gg_get_address_of(parm),
7411 gg_get_address_of(base),
7412 build_int_cst_type(SIZE_T, nbytes));
7413
7414 tree array_type = build_array_type_nelts(UCHAR, new_var->data.capacity);
7415 tree data_decl_node = gg_define_variable( array_type,
7416 NULL,
7417 vs_static);
7418 gg_assign( member(new_var->var_decl_node, "data"),
7419 gg_get_address_of(data_decl_node) );
7420
7421 // And then move it into place
7422 gg_call(VOID,
7423 "__gg__assign_value_from_stack",
7424 gg_get_address_of(new_var->var_decl_node),
7425 parm,
7426 NULL_TREE);
7427 // We now have to handle an oddball situation. It's possible we are
7428 // dealing with
7429 //
7430 // linkage section.
7431 // 01 var1
7432 // 01 var2 redefines var1
7433 //
7434 // If so, we have to give var2::data_pointer the same value as
7435 // var1::data_pointer
7436 //
7437 size_t our_index = symbol_index(symbol_elem_of(new_var));
7438 size_t next_index = our_index + 1;
7439 // Look ahead in the symbol table for the next LEVEL01/77
7440 for(;;)
7441 {
7442 symbol_elem_t *e = symbol_at(next_index);
7443 if( e->type != SymField )
7444 {
7445 break;
7446 }
7447 cbl_field_t *next_var = cbl_field_of(e);
7448 if( !next_var )
7449 {
7450 break;
7451 }
7452 if( next_var->level == LEVEL01 || next_var->level == LEVEL77 )
7453 {
7454 if( next_var->parent == our_index )
7455 {
7456 gg_assign(member(next_var->var_decl_node, "data"),
7457 member(new_var->var_decl_node, "data"));
7458 }
7459 break;
7460 }
7461 next_index += 1;
7462 }
7463 }
7464 else
7465 {
7466 // 'parameter' is a reference, so it it becomes the data member of
7467 // the cblc_field_t COBOL variable.
7468 gg_assign(member(args[i].field()->var_decl_node, "data"), base);
7469
7470 // We need to apply base + offset to the LINKAGE variable
7471 // and all of its children
7472 propogate_linkage_offsets( args[i].field(), base );
7473 }
7474 }
7475 }
7476
7477 gg_call(VOID,
7478 "__gg__pseudo_return_bookmark",
7479 NULL_TREE);
7480
7481 // The MODULE-NAME function requires a stack of program names. We push the
7482 // name on here. The first character is a 'T' or an 'N', where 'N' means
7483 // this is a nested program.
7484
7485 if( gg_trans_unit.function_stack.size() > 1 )
7486 {
7487 // This is a nested program
7488 strcpy(ach, "N");
7489 }
7490 else
7491 {
7492 // This is a top-level program:
7493 strcpy(ach, "T");
7494 }
7495 strcat(ach, current_function->our_unmangled_name);
7496 gg_call(VOID,
7497 "__gg__module_name_push",
7498 gg_string_literal(ach),
7499 NULL_TREE);
7500
7501 IF( var_decl_main_called, ne_op, integer_zero_node )
7502 {
7503 // We were just called by main:
7504 gg_assign(var_decl_main_called, integer_zero_node);
7505 gg_assign(current_function->called_by_main_counter, integer_one_node);
7506 }
7507 ELSE
7508 {
7509 // This isn't a call from main(), but it might be a recursive call to the
7510 // function that was called by main:
7511 IF(current_function->called_by_main_counter, ne_op, integer_zero_node)
7512 {
7513 // In that case, we bump the counter to keep track of things.
7514 gg_increment(current_function->called_by_main_counter);
7515 }
7516 ELSE
7517 {
7518 }
7519 ENDIF
7520 }
7521 ENDIF
7522 // The first token_location that the parser establishes is caused by the
7523 // parser scanning all of the lines in the source code. This messes up the
7524 // logic for backing up one line, which is needed to correctly step through
7525 // COBOL code with GDB-COBOL. So, we clear it here.
7526 current_location_minus_one_clear();
7527 }
7528 }
7529
7530 void
7531 parser_logop( struct cbl_field_t *tgt,
7532 struct cbl_field_t *a, // Is NULL for single-valued ops
7533 enum logop_t logop,
7534 struct cbl_field_t *b )
7535 {
7536 Analyze();
7537 SHOW_PARSE
7538 {
7539 if( logop == true_op)
7540 {
7541 SHOW_PARSE_HEADER
7542 SHOW_PARSE_FIELD(" ", tgt)
7543 SHOW_PARSE_TEXT(" will be set to TRUE ")
7544 }
7545 else if( logop == false_op)
7546 {
7547 SHOW_PARSE_HEADER
7548 SHOW_PARSE_FIELD(" ", tgt)
7549 SHOW_PARSE_TEXT(" will be set to FALSE ")
7550 }
7551 else
7552 {
7553 SHOW_PARSE_HEADER
7554 SHOW_PARSE_FIELD(" ", tgt)
7555 SHOW_PARSE_TEXT(" = ")
7556 if( a )
7557 {
7558 SHOW_PARSE_FIELD("", a)
7559 }
7560 SHOW_PARSE_TEXT(" ")
7561 SHOW_PARSE_TEXT( cbl_logop_str(logop) )
7562 if( b )
7563 {
7564 SHOW_PARSE_FIELD(" ", b)
7565 }
7566 }
7567 SHOW_PARSE_END
7568 }
7569
7570 CHECK_FIELD(tgt);
7571 switch(logop)
7572 {
7573 case and_op:
7574 case or_op:
7575 case xor_op:
7576 case xnor_op:
7577 case not_op:
7578 CHECK_FIELD(b);
7579 break;
7580 default:
7581 break;
7582 }
7583
7584 TRACE1
7585 {
7586 TRACE1_HEADER
7587 TRACE1_TEXT_ABC("operation: ", cbl_logop_str(logop), "")
7588 TRACE1_END
7589 if( logop != true_op )
7590 {
7591 if( a )
7592 {
7593 TRACE1_INDENT
7594 TRACE1_FIELD("operand A: ", a, "");
7595 }
7596 TRACE1_INDENT
7597 if( b )
7598 {
7599 TRACE1_FIELD("operand B: ", b, "");
7600 }
7601 TRACE1_END
7602 }
7603 }
7604
7605 switch(logop)
7606 {
7607 case and_op:
7608 case or_op:
7609 case xor_op:
7610 case xnor_op:
7611 CHECK_FIELD(a);
7612 break;
7613 default:
7614 break;
7615 }
7616
7617 // This routine takes two conditionals and a logical operator. From those,
7618 // it creates and returns another conditional:
7619
7620 if( tgt->type != FldConditional )
7621 {
7622 cbl_internal_error("%<parser_logop()%> was called with variable %s on line %d"
7623 ", which is not a FldConditional",
7624 tgt->name, cobol_location().first_line);
7625 }
7626 if( a && a->type != FldConditional )
7627 {
7628 cbl_internal_error("%<parser_logop()%> was called with variable %s on line %d"
7629 ", which is not a FldConditional",
7630 a->name, cobol_location().first_line);
7631 }
7632 if( b && b->type != FldConditional )
7633 {
7634 cbl_internal_error("%<parser_logop()%> was called with variable %s on line %d"
7635 ", which is not a FldConditional",
7636 b->name, cobol_location().first_line);
7637 }
7638
7639 switch( logop )
7640 {
7641 case and_op:
7642 gg_assign(tgt->var_decl_node, gg_build_logical_expression(
7643 a->var_decl_node,
7644 and_op,
7645 b->var_decl_node));
7646 break;
7647
7648 case or_op:
7649 gg_assign(tgt->var_decl_node, gg_build_logical_expression(
7650 a->var_decl_node,
7651 or_op,
7652 b->var_decl_node));
7653 break;
7654
7655 case not_op:
7656 gg_assign(tgt->var_decl_node, gg_build_logical_expression(
7657 NULL,
7658 not_op,
7659 b->var_decl_node));
7660 break;
7661
7662 case xor_op:
7663 gg_assign(tgt->var_decl_node, gg_build_logical_expression(
7664 a->var_decl_node,
7665 xor_op,
7666 b->var_decl_node));
7667 break;
7668
7669 case xnor_op:
7670 {
7671 gg_assign( tgt->var_decl_node,
7672 gg_build_logical_expression(a->var_decl_node,
7673 xor_op,
7674 b->var_decl_node));
7675
7676 // I need to negate the result.
7677
7678 gg_assign(tgt->var_decl_node, gg_build_logical_expression(
7679 NULL,
7680 not_op,
7681 tgt->var_decl_node));
7682 }
7683 break;
7684
7685 case true_op:
7686 gg_assign(tgt->var_decl_node, boolean_true_node);
7687 break;
7688
7689 case false_op:
7690 gg_assign(tgt->var_decl_node, boolean_false_node);
7691 break;
7692 }
7693
7694 TRACE1
7695 {
7696 TRACE1_INDENT
7697 TRACE1_TEXT_ABC("result: ", tgt->name, "")
7698 TRACE1_FIELD_VALUE("", tgt, "")
7699 TRACE1_END
7700 }
7701 }
7702
7703 void
7704 parser_relop( cbl_field_t *tgt,
7705 cbl_refer_t aref,
7706 enum relop_t relop,
7707 cbl_refer_t bref )
7708 {
7709 Analyze();
7710 cbl_field_t *a = aref.field, *b = bref.field;
7711 SHOW_PARSE
7712 {
7713 SHOW_PARSE_HEADER
7714 SHOW_PARSE_FIELD(" ", tgt)
7715 SHOW_PARSE_REF(" = ", aref)
7716 SHOW_PARSE_TEXT(" ")
7717 SHOW_PARSE_TEXT(relop_str(relop))
7718 SHOW_PARSE_REF(" ", bref)
7719 SHOW_PARSE_END
7720 }
7721
7722 CHECK_FIELD(tgt);
7723 CHECK_FIELD(a);
7724 CHECK_FIELD(b);
7725
7726 TRACE1
7727 {
7728 TRACE1_HEADER
7729 TRACE1_TEXT_ABC("operation: ", relop_str(relop), "")
7730 TRACE1_INDENT
7731 TRACE1_REFER("operand A: ", aref, "");
7732 TRACE1_INDENT
7733 TRACE1_REFER("operand B: ", bref, "");
7734 }
7735
7736 // This routine builds the relational expression and returns the TREE as
7737 // a conditional:
7738
7739 if( tgt->type != FldConditional )
7740 {
7741 cbl_internal_error("%<parser_relop%> was called with variable %qs, "
7742 "which is not a FldConditional",
7743 tgt->name);
7744 }
7745
7746 static tree comp_res = gg_define_variable(INT, "..pr_comp_res", vs_file_static);
7747 cobol_compare(comp_res, aref, bref);
7748
7749 // comp_res is negative, zero, position for less-than, equal-to, greater-than
7750
7751 // So, we simply compare the result of the comparison to zero using the relop
7752 // we were given to turn it into a TRUE/FALSE
7753 gg_assign( tgt->var_decl_node,
7754 gg_build_relational_expression( comp_res,
7755 relop,
7756 integer_zero_node));
7757 TRACE1
7758 {
7759 TRACE1_INDENT
7760 TRACE1_FIELD("result: ", tgt, "")
7761 TRACE1_END
7762 }
7763 }
7764
7765 void
7766 parser_relop_long(cbl_field_t *tgt,
7767 long avalue,
7768 enum relop_t relop,
7769 cbl_refer_t bref )
7770 {
7771 Analyze();
7772 // We are comparing a long to a field, so the field had best be numerical
7773
7774 cbl_field_t *b = bref.field;
7775 SHOW_PARSE
7776 {
7777 SHOW_PARSE_HEADER
7778 SHOW_PARSE_FIELD(" ", tgt)
7779 SHOW_PARSE_TEXT(" = <long value> ")
7780 SHOW_PARSE_TEXT(relop_str(relop))
7781 SHOW_PARSE_REF(" ", bref)
7782 SHOW_PARSE_END
7783 }
7784
7785 CHECK_FIELD(tgt);
7786 CHECK_FIELD(b);
7787
7788 TRACE1
7789 {
7790 TRACE1_HEADER
7791 TRACE1_TEXT_ABC("operation: ", relop_str(relop), "")
7792 TRACE1_INDENT
7793 char ach[32];
7794 sprintf(ach, "operand A: %ld (long value) ", avalue);
7795 TRACE1_TEXT(ach);
7796 TRACE1_INDENT
7797 TRACE1_REFER("operand B: ", bref, "");
7798 }
7799
7800 // This routine builds the relational expression and returns the TREE as
7801 // a conditional:
7802
7803 if( tgt->type != FldConditional )
7804 {
7805 cbl_internal_error("%<parser_relop()%> was called with variable %s, "
7806 "which is not a FldConditional",
7807 tgt->name);
7808 }
7809
7810 tree tree_a = build_int_cst_type(LONG, avalue);
7811 static tree tree_b = gg_define_variable(LONG, "..prl_tree_b", vs_file_static);
7812 get_binary_value( tree_b,
7813 NULL,
7814 bref.field,
7815 refer_offset(bref) );
7816
7817 static tree comp_res = gg_define_variable(LONG, "..prl_comp_res", vs_file_static);
7818 gg_assign(comp_res, gg_subtract(tree_a, tree_b));
7819
7820 // comp_res is negative, zero, position for less-than, equal-to, greater-than
7821
7822 // So, we simply compare the result of the comparison to zero using the relop
7823 // we were given to turn it into a TRUE/FALSE
7824 gg_assign( tgt->var_decl_node,
7825 gg_build_relational_expression( comp_res,
7826 relop,
7827 gg_cast(LONG, integer_zero_node)));
7828 TRACE1
7829 {
7830 TRACE1_INDENT
7831 TRACE1_FIELD("result: ", tgt, "")
7832 TRACE1_END
7833 }
7834 }
7835
7836 void
7837 parser_if( struct cbl_field_t *conditional )
7838 {
7839 Analyze();
7840 SHOW_PARSE
7841 {
7842 SHOW_PARSE_HEADER
7843 SHOW_PARSE_FIELD(" ", conditional)
7844 SHOW_PARSE_END
7845 }
7846
7847 CHECK_FIELD(conditional);
7848
7849 if( conditional->type != FldConditional )
7850 {
7851 cbl_internal_error("%<parser_if()%> was called with variable %s, "
7852 "which is not a FldConditional",
7853 conditional->name);
7854 }
7855
7856 TRACE1
7857 {
7858 TRACE1_HEADER
7859 TRACE1_TEXT("testing: ")
7860 TRACE1_TEXT(conditional->name)
7861 TRACE1_FIELD_VALUE("", conditional, "")
7862 TRACE1_END
7863 }
7864
7865 gg_create_true_false_statement_lists(conditional->var_decl_node);
7866 }
7867
7868 // The following routines border on abuse of the preprocessor, if not the
7869 // programmer who is trying to understand this. Look at the #defines in
7870 // gengen.h, and check out the comments for gg_if in gengen.c
7871
7872 void
7873 parser_else(void)
7874 {
7875 SHOW_PARSE
7876 {
7877 SHOW_PARSE_HEADER
7878 SHOW_PARSE_END
7879 }
7880 ELSE
7881 TRACE1
7882 {
7883 TRACE1_HEADER
7884 TRACE1_TEXT("taking FALSE branch")
7885 TRACE1_END
7886 }
7887 }
7888
7889 void
7890 parser_fi(void)
7891 {
7892 SHOW_PARSE
7893 {
7894 SHOW_PARSE_HEADER
7895 SHOW_PARSE_END
7896 }
7897 ENDIF
7898 TRACE1
7899 {
7900 TRACE1_HEADER
7901 TRACE1_END
7902 }
7903 }
7904
7905 void
7906 parser_see_stop_run(struct cbl_refer_t exit_status,
7907 const char *message)
7908 {
7909 Analyze();
7910 SHOW_PARSE
7911 {
7912 SHOW_PARSE_HEADER
7913 if( exit_status.field )
7914 {
7915 SHOW_PARSE_FIELD(" ERROR STATUS ", exit_status.field);
7916 }
7917 SHOW_PARSE_END
7918 }
7919 if( message )
7920 {
7921 parser_display_literal(message, DISPLAY_ADVANCE);
7922 }
7923 TRACE1
7924 {
7925 TRACE1_HEADER
7926 }
7927
7928 // It's a stop run. Return return-code to the operating system:
7929 static tree returned_value = gg_define_variable(INT, "..pssr_retval", vs_file_static);
7930
7931 if( exit_status.field )
7932 {
7933 // There is an exit_status, so it wins:
7934 get_binary_value( returned_value,
7935 NULL,
7936 exit_status.field,
7937 refer_offset(exit_status));
7938 TRACE1
7939 {
7940 TRACE1_REFER(" exit_status ", exit_status, "")
7941 }
7942 }
7943 else
7944 {
7945 gg_assign(returned_value, gg_cast(INT, var_decl_return_code));
7946 TRACE1
7947 {
7948 gg_fprintf( trace_handle,
7949 2,
7950 "RETURN-CODE %s [%d]",
7951 gg_string_literal(cbl_field_of(
7952 symbol_at(return_code_register()))->name),
7953 returned_value);
7954 }
7955 }
7956 TRACE1
7957 {
7958 gg_printf(" gg_exit(%d)\n", returned_value, NULL_TREE);
7959 TRACE1_END
7960 }
7961 gg_exit(returned_value);
7962 }
7963
7964 static
7965 cbl_label_addresses_t *
7966 label_fetch(struct cbl_label_t *label)
7967 {
7968 if( !label->structs.goto_trees )
7969 {
7970 label->structs.goto_trees
7971 = static_cast<cbl_label_addresses_t *>
7972 (xmalloc(sizeof(struct cbl_label_addresses_t)));
7973 gcc_assert(label->structs.goto_trees);
7974
7975 gg_create_goto_pair(&label->structs.goto_trees->go_to,
7976 &label->structs.goto_trees->label);
7977 }
7978 return label->structs.goto_trees;
7979 }
7980
7981 void
7982 parser_label_label(struct cbl_label_t *label)
7983 {
7984 label->lain = yylineno;
7985 Analyze();
7986 SHOW_PARSE
7987 {
7988 SHOW_PARSE_HEADER
7989 SHOW_PARSE_LABEL("", label)
7990 char ach[32];
7991 sprintf(ach, " label is at %p", static_cast<void*>(label));
7992 SHOW_PARSE_TEXT(ach)
7993 if( label )
7994 {
7995 sprintf(ach,
7996 " label->proc is %p",
7997 static_cast<void*>(label->structs.proc));
7998 }
7999 SHOW_PARSE_TEXT(ach)
8000 SHOW_PARSE_END
8001 }
8002
8003 TRACE1
8004 {
8005 TRACE1_HEADER
8006 TRACE1_LABEL("Establish label: ", label, "")
8007 TRACE1_END
8008 }
8009
8010 CHECK_LABEL(label);
8011
8012 if(strcmp(label->name, "_end_declaratives") == 0 )
8013 {
8014 suppress_cobol_entry_point = false;
8015 }
8016 gg_append_statement( label_fetch(label)->label );
8017 }
8018
8019 void
8020 parser_label_goto(struct cbl_label_t *label)
8021 {
8022 label->used = yylineno;
8023
8024 Analyze();
8025 SHOW_PARSE
8026 {
8027 SHOW_PARSE_HEADER
8028 SHOW_PARSE_LABEL(" ", label)
8029 char ach[32];
8030 sprintf(ach, " label is at %p", static_cast<void*>(label));
8031 SHOW_PARSE_TEXT(ach)
8032 if( label )
8033 {
8034 sprintf(ach,
8035 " label->proc is %p",
8036 static_cast<void*>(label->structs.proc));
8037 }
8038 SHOW_PARSE_TEXT(ach)
8039 SHOW_PARSE_END
8040 }
8041
8042 TRACE1
8043 {
8044 TRACE1_HEADER
8045 TRACE1_LABEL("GOTO label: ", label, "")
8046 TRACE1_END
8047 }
8048
8049 CHECK_LABEL(label);
8050
8051 if( strcmp(label->name, "_end_declaratives") == 0 )
8052 {
8053 suppress_cobol_entry_point = true;
8054 }
8055
8056 gg_append_statement( label_fetch(label)->go_to );
8057 }
8058
8059 void
8060 parser_setop( struct cbl_field_t *tgt,
8061 struct cbl_field_t *candidate,
8062 enum setop_t op,
8063 struct cbl_field_t *domain)
8064 {
8065 Analyze();
8066 SHOW_PARSE
8067 {
8068 SHOW_PARSE_HEADER
8069 SHOW_PARSE_FIELD(" ", tgt)
8070 SHOW_PARSE_FIELD(" = ", candidate)
8071 if( op == is_op )
8072 {
8073 SHOW_PARSE_TEXT(" is_op ")
8074 }
8075 SHOW_PARSE_FIELD(" = ", domain)
8076 SHOW_PARSE_END
8077 }
8078
8079 CHECK_FIELD(tgt);
8080 CHECK_FIELD(candidate);
8081 CHECK_FIELD(domain);
8082
8083 TRACE1
8084 {
8085 TRACE1_HEADER
8086 TRACE1_FIELD("parser_setop: ", candidate, "")
8087 TRACE1_TEXT(" ")
8088 TRACE1_TEXT(setop_str(op))
8089 TRACE1_FIELD(" ", domain, "")
8090 TRACE1_END
8091 }
8092
8093 gcc_assert(tgt->type == FldConditional);
8094 gcc_assert(domain->data.initial);
8095 gcc_assert(strlen(domain->data.initial));
8096
8097 switch(op)
8098 {
8099 case is_op:
8100 switch(candidate->type)
8101 {
8102 case FldGroup:
8103 case FldAlphanumeric:
8104 gg_assign(tgt->var_decl_node, gg_build_relational_expression(
8105 gg_call_expr(INT,
8106 "__gg__setop_compare",
8107 member(candidate, "data"),
8108 member(candidate, "capacity"),
8109 member(domain, "initial"),
8110 NULL_TREE),
8111 ne_op,
8112 integer_zero_node));
8113 break;
8114 default:
8115 dbgmsg("%10s in %s:%d", __func__, __FILE__, __LINE__ );
8116 cbl_internal_error("candidate %s has unimplemented %<CVT_type%> %d(%s)",
8117 candidate->name,
8118 candidate->type,
8119 cbl_field_type_str(candidate->type));
8120 gcc_unreachable();
8121 break;
8122 }
8123 break;
8124
8125 default:
8126 dbgmsg("%10s in %s:%d", __func__, __FILE__, __LINE__ );
8127 cbl_internal_error("unknown %<setop_t%> code %d", op);
8128 gcc_unreachable();
8129 break;
8130 }
8131 }
8132
8133 void
8134 parser_classify( cbl_field_t *tgt,
8135 const cbl_refer_t &candidate,
8136 enum classify_t type )
8137 {
8138 Analyze();
8139 SHOW_PARSE
8140 {
8141 SHOW_PARSE_HEADER
8142 SHOW_PARSE_FIELD(" ", tgt)
8143 SHOW_PARSE_FIELD(" = ", candidate.field)
8144 SHOW_PARSE_TEXT(" IS ")
8145 SHOW_PARSE_TEXT(classify_str(type))
8146 SHOW_PARSE_END
8147 }
8148
8149 gcc_assert(tgt->type == FldConditional);
8150
8151 TRACE1
8152 {
8153 TRACE1_HEADER
8154 TRACE1_REFER_VALUE("parser_classify: ", candidate, "")
8155 TRACE1_TEXT(" ")
8156 TRACE1_TEXT(classify_str(type))
8157 }
8158
8159 gg_assign(tgt->var_decl_node, gg_build_relational_expression(
8160 gg_call_expr(INT,
8161 "__gg__classify",
8162 build_int_cst_type(INT, type),
8163 gg_get_address_of(candidate.field->var_decl_node),
8164 refer_offset(candidate),
8165 refer_size_dest(candidate),
8166 NULL_TREE),
8167 ne_op,
8168 integer_zero_node));
8169
8170 TRACE1
8171 {
8172 TRACE1_TEXT(" result is ")
8173 TRACE1_TEXT(tgt->name)
8174 TRACE1_FIELD_VALUE(" -> ", tgt, "")
8175 TRACE1_END
8176 }
8177 }
8178
8179 void
8180 parser_perform(const cbl_perform_tgt_t *tgt, cbl_refer_t how_many)
8181 {
8182 const cbl_field_t *N = how_many.field;
8183 // No SHOW_PARSE here; we want to fall through:
8184 if( !tgt->to() )
8185 {
8186 // We only have tgt->from.
8187 if( !N )
8188 {
8189 // There is no N. This is a simple PERFORM proc-1
8190 parser_perform(tgt->from());
8191 }
8192 else
8193 {
8194 // This is a PERFORM proc-1 N TIMES
8195 parser_perform_times(tgt->from(), how_many);
8196 }
8197 }
8198 else
8199 {
8200 // We have both from and to
8201 if( !N )
8202 {
8203 // There is no N. This is PERFORM proc-1 THROUGH proc-2
8204 // false means nexting in GDB will work
8205 internal_perform_through(tgt->from(), tgt->to(), false);
8206 }
8207 else
8208 {
8209 // This is a PERFORM proc-1 THROUGH proc-2 N TIMES
8210 internal_perform_through_times(tgt->from(), tgt->to(), how_many);
8211 }
8212 }
8213 }
8214
8215 static void
8216 create_iline_address_pairs(struct cbl_perform_tgt_t *tgt)
8217 {
8218 gg_create_goto_pair(&tgt->addresses.top.go_to,
8219 &tgt->addresses.top.label);
8220
8221 gg_create_goto_pair(&tgt->addresses.exit.go_to,
8222 &tgt->addresses.exit.label);
8223
8224 gg_create_goto_pair(&tgt->addresses.test.go_to,
8225 &tgt->addresses.test.label);
8226
8227 gg_create_goto_pair(&tgt->addresses.testA.go_to,
8228 &tgt->addresses.testA.label);
8229
8230 gg_create_goto_pair(&tgt->addresses.setup.go_to,
8231 &tgt->addresses.setup.label);
8232 }
8233
8234 void
8235 parser_perform_start( struct cbl_perform_tgt_t *tgt )
8236 {
8237 Analyze();
8238 SHOW_PARSE
8239 {
8240 SHOW_PARSE_HEADER
8241 if( tgt )
8242 {
8243 SHOW_PARSE_TEXT(" cbl_perform_tgt_t is at")
8244 char ach[32];
8245 sprintf(ach, " %p", static_cast<void*>(tgt));
8246 SHOW_PARSE_TEXT(ach);
8247 SHOW_PARSE_LABEL(" ", tgt->from())
8248 if( tgt->to() )
8249 {
8250 SHOW_PARSE_LABEL(" ", tgt->to())
8251 }
8252 }
8253 SHOW_PARSE_END
8254 }
8255
8256 TRACE1
8257 {
8258 TRACE1_HEADER
8259 if( tgt->from() )
8260 {
8261 TRACE1_LABEL(" from ", tgt->from(), "")
8262 }
8263 if( tgt->to() )
8264 {
8265 TRACE1_LABEL(" to ", tgt->to(), "")
8266 }
8267 TRACE1_END
8268 }
8269
8270 // Create the goto/label pairs we are going to be needing:
8271 create_iline_address_pairs(tgt);
8272
8273 // From here we have to jump to the loop setup code:
8274 SHOW_PARSE
8275 {
8276 SHOW_PARSE_INDENT
8277 SHOW_PARSE_TEXT("GOTO SETUP")
8278 SHOW_PARSE_END
8279 }
8280 gg_append_statement(tgt->addresses.setup.go_to);
8281
8282 // The next parser+_generated instructions will be the body of the loop, so we
8283 // need a TOP label here so we can get back to them:
8284 SHOW_PARSE
8285 {
8286 SHOW_PARSE_INDENT
8287 SHOW_PARSE_TEXT("LABEL TOP:")
8288 SHOW_PARSE_END
8289 }
8290
8291 // Give GDB-COBOL something to chew on when NEXTing. This instruction will
8292 // get the line number of the PERFORM N TIMES code.
8293 gg_append_statement(tgt->addresses.top.label);
8294 insert_nop(104);
8295 }
8296
8297 void
8298 parser_perform_conditional( struct cbl_perform_tgt_t *tgt )
8299 {
8300 Analyze();
8301 SHOW_PARSE
8302 {
8303 SHOW_PARSE_HEADER
8304 SHOW_PARSE_TEXT(" cbl_perform_tgt_t is at")
8305 char ach[32];
8306 sprintf(ach, " %p", static_cast<void*>(tgt));
8307 SHOW_PARSE_TEXT(ach);
8308 SHOW_PARSE_END
8309 }
8310
8311 unsigned int i = tgt->addresses.number_of_conditionals;
8312
8313 if( !(i < MAXIMUM_UNTILS) )
8314 {
8315 cbl_internal_error("%s:%d: %u exceeds %<MAXIMUM_UNTILS%> of %d, line %d",
8316 __func__, __LINE__,
8317 i, MAXIMUM_UNTILS, CURRENT_LINE_NUMBER);
8318 }
8319 gcc_assert(i < MAXIMUM_UNTILS);
8320
8321 // Create an unnamed goto/label pair for jumping over the conditional
8322 // calculation.
8323 gg_create_goto_pair(&tgt->addresses.condover[i].go_to,
8324 &tgt->addresses.condover[i].label);
8325
8326 // Create an unnamed goto/label pair for jumping into the
8327 // conditional calculation:
8328 gg_create_goto_pair(&tgt->addresses.condinto[i].go_to,
8329 &tgt->addresses.condinto[i].label);
8330
8331 // Create an unnamed goto/label pair for jumping back from the
8332 // conditional calculation:
8333 gg_create_goto_pair(&tgt->addresses.condback[i].go_to,
8334 &tgt->addresses.condback[i].label);
8335
8336 // The next instructions that the parser will give us are the conditional
8337 // calculation, so the first thing that goes down is the condover:
8338 gg_append_statement(tgt->addresses.condover[i].go_to);
8339
8340 // And then, of course, we need to be able to jump back here to actually
8341 // do the run-time conditional calculations:
8342 gg_append_statement(tgt->addresses.condinto[i].label);
8343
8344 tgt->addresses.number_of_conditionals += 1;
8345 }
8346
8347 void
8348 parser_perform_conditional_end( struct cbl_perform_tgt_t *tgt )
8349 {
8350 Analyze();
8351 SHOW_PARSE
8352 {
8353 SHOW_PARSE_HEADER
8354 SHOW_PARSE_TEXT(" cbl_perform_tgt_t is at")
8355 char ach[32];
8356 sprintf(ach, " %p", static_cast<void*>(tgt));
8357 SHOW_PARSE_TEXT(ach);
8358 SHOW_PARSE_END
8359 }
8360
8361 unsigned int i = tgt->addresses.number_of_conditionals;
8362 gcc_assert(i);
8363
8364 // We need to cap off the prior conditional in this chain of conditionals
8365 gg_append_statement(tgt->addresses.condback[i-1].go_to);
8366 gg_append_statement(tgt->addresses.condover[i-1].label);
8367 }
8368
8369 static void
8370 build_N_pairs(tree *go_to, tree *label, size_t N)
8371 {
8372 for(size_t i=0; i<N; i++)
8373 {
8374 tree a;
8375 tree b;
8376 gg_create_goto_pair(&a, &b);
8377 go_to[i] = a;
8378 label[i] = b;
8379 }
8380 }
8381
8382 static void
8383 perform_outofline_before_until(struct cbl_perform_tgt_t *tgt,
8384 bool /*test_before*/,
8385 size_t /*N*/,
8386 struct cbl_perform_vary_t *varys )
8387 {
8388 SHOW_PARSE
8389 {
8390 SHOW_PARSE_HEADER
8391 SHOW_PARSE_END
8392 }
8393 // This is a PERFORM proc-1 [through proc-2] TEST BEFORE} UNTIL
8394
8395 /*
8396 TOP:
8397 IF CONDITION 0
8398 GOTO EXIT
8399 ELSE
8400 EXECUTE BODY
8401 GOTO TOP
8402 EXIT:
8403 */
8404
8405 create_iline_address_pairs(tgt);
8406
8407 // Tag the top of the perform
8408 gg_append_statement(tgt->addresses.top.label);
8409
8410 // Go do the conditional calculation:
8411
8412 gg_append_statement(tgt->addresses.condinto[0].go_to);
8413
8414 // And put down the label so that the conditional calculation knows
8415 // where to return:
8416 gg_append_statement(tgt->addresses.condback[0].label);
8417
8418 char ach[256];
8419 size_t our_pseudo_label = pseudo_label++;
8420 sprintf(ach,
8421 "_proccallb." HOST_SIZE_T_PRINT_DEC ":",
8422 (fmt_size_t)our_pseudo_label);
8423 gg_insert_into_assembler( ach );
8424
8425 parser_if(varys[0].until);
8426 {
8427 // We're done, so leave
8428 gg_append_statement(tgt->addresses.exit.go_to);
8429 }
8430 parser_else();
8431 {
8432 // We're not done, so execute the body
8433 // true means GDB next will fall through
8434 internal_perform_through(tgt->from(), tgt->to(), true);
8435
8436 // Jump back to the test:
8437 gg_append_statement(tgt->addresses.top.go_to );
8438 }
8439 parser_fi();
8440
8441 // Label the bottom of the PERFORM
8442 gg_append_statement( tgt->addresses.exit.label );
8443 sprintf(ach,
8444 "_procretb." HOST_SIZE_T_PRINT_DEC ":",
8445 (fmt_size_t)our_pseudo_label);
8446 token_location_override(current_location_minus_one());
8447 gg_insert_into_assembler( ach );
8448 }
8449
8450 static void
8451 perform_outofline_after_until(struct cbl_perform_tgt_t *tgt,
8452 bool /*test_before*/,
8453 size_t /*N*/,
8454 struct cbl_perform_vary_t *varys )
8455 {
8456 SHOW_PARSE
8457 {
8458 SHOW_PARSE_HEADER
8459 SHOW_PARSE_END
8460 }
8461 // This is a PERFORM proc-1 [through proc-2] TEST AFTER UNTIL
8462
8463 /*
8464 TOP:
8465 EXECUTE BODY
8466 IF CONDITION 0
8467 GOTO EXIT
8468 ELSE
8469 ADD BY_0 to VARYING_0
8470 GOTO TOP
8471 EXIT:
8472 */
8473
8474 char ach[256];
8475 size_t our_pseudo_label = pseudo_label++;
8476 sprintf(ach,
8477 "_proccallb." HOST_SIZE_T_PRINT_DEC ":",
8478 (fmt_size_t)our_pseudo_label);
8479 gg_insert_into_assembler( ach );
8480
8481 create_iline_address_pairs(tgt);
8482
8483 // Label the top of the loop
8484 gg_append_statement(tgt->addresses.top.label);
8485
8486 // Build the perform:
8487 // true in the next call means that GDB next will not stop until the entire
8488 // until loop is finished
8489 internal_perform_through(tgt->from(), tgt->to(), true);
8490
8491 // Go recalculate the conditional:
8492 gg_append_statement( tgt->addresses.condinto[0].go_to);
8493
8494 // And lay down the label for the come-back from the recalculation:
8495 gg_append_statement( tgt->addresses.condback[0].label);
8496
8497 // Assess the conditional
8498 parser_if(varys[0].until);
8499 // It's true, so we're done
8500 gg_append_statement( tgt->addresses.exit.go_to );
8501 parser_else();
8502 // It's false, so execute the body again
8503 gg_append_statement( tgt->addresses.top.go_to );
8504 parser_fi();
8505 // Label the bottom of the PERFORM
8506 gg_append_statement( tgt->addresses.exit.label );
8507 sprintf(ach,
8508 "_procretb." HOST_SIZE_T_PRINT_DEC ":",
8509 (fmt_size_t)our_pseudo_label);
8510 token_location_override(current_location_minus_one());
8511 gg_insert_into_assembler( ach );
8512 }
8513
8514 static void
8515 perform_outofline_testafter_varying(struct cbl_perform_tgt_t *tgt,
8516 bool /*test_before*/,
8517 size_t N,
8518 struct cbl_perform_vary_t *varys )
8519 {
8520 SHOW_PARSE
8521 {
8522 SHOW_PARSE_HEADER
8523 SHOW_PARSE_END
8524 }
8525 // This is a PERFORM proc-1 [THROUGH proc-2] TEST AFTER VARYING
8526
8527 /*
8528
8529 [ENTRANCE]
8530 MOVE FROM_0 TO VARYING_0
8531 INIT_1:
8532 MOVE FROM_1 TO VARYING_1
8533 INIT_2:
8534 MOVE FROM_2 TO VARYING_2
8535 . . . . . . . . . . . . . . . . . .
8536 INIT_N-2:
8537 MOVE FROM_N-2 TO VARYING_N-2
8538 INIT_N-1:
8539 MOVE FROM_N-1 TO VARYING_N-1
8540 GOTO TOP
8541 TOP:
8542 PERFORM PROC-1 [THROUGH PROC-2]
8543 IF NOT CONDITION_N-1
8544 ADD BY_N-1 TO VARYING_N-1
8545 GOTO TOP
8546 IF NOT CONDITION_N-2
8547 ADD BY_N-2 TO VARYING_N-2
8548 GOTO INIT_N-1
8549 IF NOT CONDITION_N-3
8550 ADD BY_N-3 TO VARYING_N-3
8551 GOTO INIT_N-2
8552 . . . . . . . . . . . . . . . . . .
8553 IF NOT CONDITION_1
8554 ADD BY_1 TO VARYING_1
8555 GOTO INIT_2
8556 IF NOT CONDITION_0
8557 ADD BY_0 TO VARYING_0
8558 GOTO INIT_1
8559 EXIT:
8560
8561 */
8562
8563 // So, we're going to do that. But because the initializations
8564 // and the testing are so nicely loopish, we're going to let
8565 // the computer create them for us.
8566
8567 // We are going to need a set of N label pairs. Actually, we
8568 // only need N-1; we don't use the zeroth pair. But the code
8569 // is cleaner if we just build all N of them.
8570
8571 char ach[256];
8572 size_t our_pseudo_label = pseudo_label++;
8573 sprintf(ach,
8574 "_proccallb." HOST_SIZE_T_PRINT_DEC ":",
8575 (fmt_size_t)our_pseudo_label);
8576 gg_insert_into_assembler( ach );
8577
8578 create_iline_address_pairs(tgt);
8579
8580 tree go_to[MAX_AFTERS];
8581 tree label[MAX_AFTERS];
8582
8583 build_N_pairs(go_to, label, N);
8584
8585 // Build the initialization section:
8586 for(size_t i=0; i<N; i++)
8587 {
8588 gg_append_statement(label[i]);
8589 parser_move(varys[i].varying, varys[i].from);
8590 }
8591 // These next two statements do nothing. But it'll make sense
8592 // when we move the logic around to create an inline VARYING
8593 gg_append_statement(tgt->addresses.top.go_to);
8594 gg_append_statement(tgt->addresses.top.label);
8595
8596 // Build the body:
8597 // true in the next call means that the entire loop will complete
8598 // even in the face of a GDB next
8599 internal_perform_through(tgt->from(), tgt->to(), true);
8600
8601 // Build the test section
8602 // (The oddball test is because N is a size_t, and can't go negative)
8603 for(size_t i=N-1; i<N; i--)
8604 {
8605 // Jump to the conditional calculation:
8606 gg_append_statement( tgt->addresses.condinto[i].go_to);
8607
8608 // And put down the label for the return from that calculation:
8609 gg_append_statement( tgt->addresses.condback[i].label);
8610
8611 parser_if( varys[i].until );
8612 // Condition is true; so we'll fall through
8613 parser_else();
8614 // Condition is false, so we increment, and keep going:
8615 parser_add(varys[i].varying, varys[i].by, varys[i].varying);
8616 if( i == N-1 )
8617 {
8618 gg_append_statement(tgt->addresses.top.go_to);
8619 }
8620 else
8621 {
8622 gg_append_statement(go_to[i+1]);
8623 }
8624 parser_fi();
8625 }
8626 // Arriving here means that we all of the conditions were
8627 // true. So, we're done.
8628 sprintf(ach,
8629 "_procretb." HOST_SIZE_T_PRINT_DEC ":",
8630 (fmt_size_t)our_pseudo_label);
8631 token_location_override(current_location_minus_one());
8632 gg_insert_into_assembler( ach );
8633 }
8634
8635 static void
8636 perform_outofline_before_varying( struct cbl_perform_tgt_t *tgt,
8637 bool /*test_before*/,
8638 size_t N,
8639 struct cbl_perform_vary_t *varys )
8640 {
8641 SHOW_PARSE
8642 {
8643 SHOW_PARSE_HEADER
8644 SHOW_PARSE_END
8645 }
8646 // This is a PERFORM proc-1 [THROUGH proc-2] TEST BEFORE VARYING
8647
8648 /*
8649
8650 ENTRANCE:
8651 SET ALL VARYING-N to FROM-N
8652 TEST_0:
8653 IF CONDITION_0:
8654 GOTO EXIT:
8655 TEST_1:
8656 IF CONDITION_1:
8657 ADD BY_0 TO VARYING_0
8658 MOVE FROM_1 TO VARYING_1
8659 GOTO TEST_0
8660 TEST_2:
8661 IF CONDITION_2:
8662 ADD BY_1 TO VARYING_1:
8663 MOVE FROM_2 TO VARYING_2
8664 GOTO TEST_1:
8665 TEST_3:
8666 IF CONDITION_3:
8667 ADD BY_2 TO VARYING_2:
8668 MOVE FROM_3 TO VARYING_3
8669 GOTO TEST_1:
8670 . . . . . . . . . . . . . . . .
8671 TEST_N-1:
8672 IF CONDITION_N-1:
8673 ADD BY_N-2 TO VARYING_N-2:
8674 MOVE FROM_N-2 TO VARYING_N-2
8675 GOTO TEST_N-2
8676 TOP:
8677 PERFORM proc-1 [THROUGH proc-2]
8678
8679 ADD BY_N-1 TO VARYING_N-1:
8680 GOTO TEST_N-1
8681
8682 */
8683 create_iline_address_pairs(tgt);
8684
8685 tree go_to[MAX_AFTERS];
8686 tree label[MAX_AFTERS];
8687 build_N_pairs(go_to, label, N);
8688
8689 char ach[256];
8690 size_t our_pseudo_label = pseudo_label++;
8691 sprintf(ach,
8692 "_proccallb." HOST_SIZE_T_PRINT_DEC ":",
8693 (fmt_size_t)our_pseudo_label);
8694 gg_insert_into_assembler( ach );
8695
8696 // Initialize all varying:
8697
8698 for(size_t i=0; i<N; i++)
8699 {
8700 parser_move(varys[i].varying, varys[i].from);
8701 }
8702
8703 // Lay down the testing cycle:
8704 for(size_t i=0; i<N; i++)
8705 {
8706 // This is the chain of conditions that gets tested before
8707 // the statements run. Each condition gets its own label.
8708 gg_append_statement(label[i]);
8709
8710 // go back to the instructions that calculate the conditional
8711 gg_append_statement(tgt->addresses.condinto[i].go_to);
8712
8713 // And put down the label that brings us back:
8714 gg_append_statement(tgt->addresses.condback[i].label);
8715
8716 // Now we can test the calculated conditional:
8717 parser_if(varys[i].until);
8718 // This condition has been met, so we increment the
8719 // variable to the left, reset ours, and go check the
8720 // one we just incremented
8721 if(i == 0)
8722 {
8723 // This is the leftmost condition condition, so when it
8724 // is TRUE, we are done.
8725 gg_append_statement( tgt->addresses.exit.go_to );
8726 }
8727 else
8728 {
8729 // This is one of the conditions to the right of the
8730 // first one. So, we augment the VARYING to the
8731 // left, reset our VARYING, and go test the
8732 // condition to the left:
8733 parser_add(varys[i-1].varying, varys[i-1].by, varys[i-1].varying);
8734 parser_move(varys[i].varying, varys[i].from);
8735 gg_append_statement( go_to[i-1] );
8736 }
8737 parser_else();
8738 // This condition has not been met.
8739 if( i == N-1 )
8740 {
8741 // ... and this is the rightmost condition
8742 // This is where we perform the body of the PERFORM.
8743 gg_append_statement( tgt->addresses.top.label );
8744
8745 // Build the body:
8746 // true in the next call means that GDB NEXT will pass through the
8747 // entire loop
8748 internal_perform_through(tgt->from(), tgt->to(), true);
8749
8750 // And now we augment FROM_N-1 by BY__N-1
8751 parser_add(varys[N-1].varying, varys[N-1].by, varys[N-1].varying);
8752
8753 // And we jump back to test that freshly-augmented condition
8754 gg_append_statement( go_to[N-1] );
8755 }
8756 else
8757 {
8758 // At this point, a condition that is not the rightmost
8759 // one has not been met. We could, in principle, just
8760 // fall through at this point. But that makes me nervous.
8761 // So, I am going to put in what may well be an
8762 // unnecessary goto:
8763 gg_append_statement( go_to[i+1] );
8764 }
8765 parser_fi();
8766 }
8767 // The astute observer will have noted that there is no way
8768 // for the generated runtime code to reach this point except by jumpint to
8769 // the EXIT: label.
8770 // We have, you see, reached the egress:
8771 gg_append_statement( tgt->addresses.exit.label );
8772 sprintf(ach,
8773 "_procretb." HOST_SIZE_T_PRINT_DEC ":",
8774 (fmt_size_t)our_pseudo_label);
8775 token_location_override(current_location_minus_one());
8776 gg_insert_into_assembler( ach );
8777 }
8778
8779 static void
8780 perform_outofline( struct cbl_perform_tgt_t *tgt,
8781 bool test_before,
8782 size_t N,
8783 struct cbl_perform_vary_t *varys )
8784 {
8785 SHOW_PARSE
8786 {
8787 SHOW_PARSE_HEADER
8788 SHOW_PARSE_END
8789 }
8790 // This is an out-of-line perform.
8791
8792 // We need to create the address pairs, because there was no parser_perform_start
8793
8794 if( N == 1 && !varys[0].varying.field )
8795 {
8796 // There is no varys.varying, so this is just a PERFORM proc-1 UNTIL
8797 if( test_before )
8798 {
8799 perform_outofline_before_until(tgt, test_before, N, varys);
8800 }
8801 else
8802 {
8803 perform_outofline_after_until(tgt, test_before, N, varys);
8804 }
8805 }
8806 else
8807 {
8808 // This is a PERFORM proc-1 [through proc-2] VARYING
8809 if( test_before )
8810 {
8811 perform_outofline_before_varying(tgt, test_before, N, varys);
8812 }
8813 else
8814 {
8815 perform_outofline_testafter_varying(tgt, test_before, N, varys);
8816 }
8817 }
8818 }
8819
8820 static void
8821 perform_inline_until( struct cbl_perform_tgt_t *tgt,
8822 bool test_before,
8823 size_t /*N*/,
8824 struct cbl_perform_vary_t *varys )
8825 {
8826 SHOW_PARSE
8827 {
8828 SHOW_PARSE_HEADER
8829 SHOW_PARSE_END
8830 }
8831 // This is a PERFORM <inline> [TEST {BEFORE|AFTER}] UNTIL
8832
8833 /*
8834
8835 GOTO SETUP
8836 TOP: S1
8837 S2
8838 EXIT PERFORM -> GOTO EXIT:
8839 S3
8840 S4
8841 EXIT PERFORM CYCLE -> GOTO TEST
8842 S6
8843 S7
8844 TEST: IF CONDITION
8845 GOTO EXIT
8846 ELSE
8847 GOTO TOP
8848 SETUP:
8849 IF TEST BEFORE
8850 GOTO TEST
8851 ELSE
8852 GOTO TOP
8853 EXIT:
8854 */
8855 gg_append_statement(tgt->addresses.test.label);
8856
8857 // Go to where the conditional is recalculated....
8858 gg_append_statement(tgt->addresses.condinto[0].go_to);
8859
8860 // ...and lay down the return address.
8861 gg_append_statement(tgt->addresses.condback[0].label);
8862
8863 parser_if( varys[0].until );
8864 gg_append_statement( tgt->addresses.exit.go_to );
8865 parser_else();
8866 gg_append_statement( tgt->addresses.top.go_to );
8867 parser_fi();
8868 gg_append_statement( tgt->addresses.setup.label );
8869
8870 if( test_before )
8871 {
8872 gg_append_statement( tgt->addresses.test.go_to );
8873 }
8874 else
8875 {
8876 gg_append_statement( tgt->addresses.top.go_to );
8877 }
8878 gg_append_statement( tgt->addresses.exit.label );
8879 }
8880
8881 static void
8882 perform_inline_testbefore_varying( struct cbl_perform_tgt_t *tgt,
8883 bool /*test_before*/,
8884 size_t N,
8885 struct cbl_perform_vary_t *varys )
8886 {
8887 SHOW_PARSE
8888 {
8889 SHOW_PARSE_HEADER
8890 SHOW_PARSE_END
8891 }
8892 // This is a PERFORM proc-1 [THROUGH proc-2] TEST BEFORE VARYING
8893
8894 /*
8895
8896 GOTO SETUP
8897 TOP:
8898 S1
8899 S2
8900 EXIT PERFORM -- GOTO EXIT:
8901 S3
8902 S4
8903 EXIT PERFORM CYCLE -- GOTO TESTA
8904 S5
8905 S6
8906 GOTO AUGMENT_N-1
8907 SETUP:
8908 SET ALL VARYING-N to FROM-N
8909 TEST_0:
8910 IF CONDITION_0:
8911 GOTO EXIT:
8912 TEST_1:
8913 IF CONDITION_1:
8914 ADD BY_0 TO VARYING_0
8915 MOVE FROM_1 TO VARYING_1
8916 GOTO TEST_0
8917 TEST_2:
8918 IF CONDITION_2:
8919 ADD BY_1 TO VARYING_1:
8920 MOVE FROM_2 TO VARYING_2
8921 GOTO TEST_1:
8922 TEST_3:
8923 IF CONDITION_3:
8924 ADD BY_2 TO VARYING_2:
8925 MOVE FROM_3 TO VARYING_3
8926 GOTO TEST_1:
8927 . . . . . . . . . . . . . . . .
8928 TEST_N-1:
8929 IF CONDITION_N-1:
8930 ADD BY_N-2 TO VARYING_N-2:
8931 MOVE FROM_N-2 TO VARYING_N-2
8932 GOTO TEST_N-2
8933
8934 GOTO TOP
8935 TESTA:
8936 ADD BY_N-1 TO VARYING_N-1:
8937 GOTO TEST_N-1
8938
8939 */
8940 tree go_to[MAX_AFTERS];
8941 tree label[MAX_AFTERS];
8942 build_N_pairs(go_to, label, N);
8943
8944 // At this point in the executable, the body of the inline loop has been
8945 // laid down, so we lay down a GOTO TESTA
8946 SHOW_PARSE
8947 {
8948 SHOW_PARSE_INDENT
8949 SHOW_PARSE_TEXT("GOTO TESTA")
8950 SHOW_PARSE_END
8951 }
8952 gg_append_statement(tgt->addresses.testA.go_to);
8953
8954 // It's now safe to setup the whole extravaganza of UNTIL conditions:
8955 SHOW_PARSE
8956 {
8957 SHOW_PARSE_INDENT
8958 SHOW_PARSE_TEXT("LABEL SETUP:")
8959 SHOW_PARSE_END
8960 }
8961 gg_append_statement(tgt->addresses.setup.label);
8962
8963 // Initialize all varying:
8964 for(size_t i=0; i<N; i++)
8965 {
8966 parser_move(varys[i].varying, varys[i].from);
8967 }
8968
8969 // Lay down the testing cycle:
8970 for(size_t i=0; i<N; i++)
8971 {
8972 // This is the chain of conditions that gets tested before
8973 // the statements run. Each condition gets its own label.
8974 SHOW_PARSE
8975 {
8976 SHOW_PARSE_INDENT
8977 char ach[32];
8978 sprintf(ach, "LABEL [" HOST_SIZE_T_PRINT_DEC "]:", (fmt_size_t)i);
8979 SHOW_PARSE_TEXT(ach)
8980 SHOW_PARSE_END
8981 }
8982 gg_append_statement(label[i]);
8983
8984 // Jump to where the conditional is calculated...
8985 SHOW_PARSE
8986 {
8987 SHOW_PARSE_INDENT
8988 char ach[32];
8989 sprintf(ach, "LABEL CONDINTO[" HOST_SIZE_T_PRINT_DEC "]:",
8990 (fmt_size_t)i);
8991 SHOW_PARSE_TEXT(ach)
8992 SHOW_PARSE_END
8993 }
8994 gg_append_statement(tgt->addresses.condinto[i].go_to);
8995
8996 // ...and lay down the label for the return from there
8997 SHOW_PARSE
8998 {
8999 SHOW_PARSE_INDENT
9000 char ach[32];
9001 sprintf(ach, "LABEL CONDBACK[" HOST_SIZE_T_PRINT_DEC "]:",
9002 (fmt_size_t)i);
9003 SHOW_PARSE_TEXT(ach)
9004 SHOW_PARSE_END
9005 }
9006 gg_append_statement(tgt->addresses.condback[i].label);
9007
9008 // Test that conditional
9009 parser_if(varys[i].until);
9010 // This condition has been met, so we increment the
9011 // variable to the left, reset ours, and go check the
9012 // one we just incremented
9013 if(i == 0)
9014 {
9015 // This is the leftmost condition condition, so when it
9016 // is TRUE, we are done.
9017 SHOW_PARSE
9018 {
9019 SHOW_PARSE_INDENT
9020 SHOW_PARSE_TEXT("GOTO EXIT")
9021 SHOW_PARSE_END
9022 }
9023 gg_append_statement( tgt->addresses.exit.go_to );
9024 }
9025 else
9026 {
9027 // This is one of the conditions to the right of the
9028 // first one. So, we augment the VARYING to the
9029 // left, reset our VARYING, and go test the
9030 // condition to the left:
9031 parser_add(varys[i-1].varying, varys[i-1].by, varys[i-1].varying);
9032 parser_move(varys[i].varying, varys[i].from);
9033 SHOW_PARSE
9034 {
9035 SHOW_PARSE_INDENT
9036 char ach[32];
9037 sprintf(ach, "GOTO [" HOST_SIZE_T_PRINT_DEC "]:",
9038 (fmt_size_t)(i-1));
9039 SHOW_PARSE_TEXT(ach)
9040 SHOW_PARSE_END
9041 }
9042 gg_append_statement( go_to[i-1] );
9043 }
9044 parser_else();
9045 // This condition has not been met.
9046 if( i == N-1 )
9047 {
9048 // ... and this is the rightmost condition
9049 // This is where we perform the body of the PERFORM.
9050 SHOW_PARSE
9051 {
9052 SHOW_PARSE_INDENT
9053 SHOW_PARSE_TEXT("GOTO TOP")
9054 SHOW_PARSE_END
9055 }
9056 gg_append_statement( tgt->addresses.top.go_to );
9057
9058 // And now we augment FROM_N-1 by BY__N-1
9059 SHOW_PARSE
9060 {
9061 SHOW_PARSE_INDENT
9062 SHOW_PARSE_TEXT("LABEL TESTA:")
9063 SHOW_PARSE_END
9064 }
9065 gg_append_statement(tgt->addresses.testA.label);
9066 parser_add(varys[N-1].varying, varys[N-1].by, varys[N-1].varying);
9067 // And we jump back to test that freshly-augmented condition
9068 SHOW_PARSE
9069 {
9070 SHOW_PARSE_INDENT
9071 char ach[32];
9072 sprintf(ach, "GOTO [" HOST_SIZE_T_PRINT_DEC "]:",
9073 (fmt_size_t)(N-1));
9074 SHOW_PARSE_TEXT(ach)
9075 SHOW_PARSE_END
9076 }
9077 gg_append_statement( go_to[N-1] );
9078 }
9079 else
9080 {
9081 // At this point, a condition that is not the rightmost
9082 // one has not been met. We could, in principle, just
9083 // fall through at this point. But that makes me nervous.
9084 // So, I am going to put in what may well be an
9085 // unnecessary goto:
9086 SHOW_PARSE
9087 {
9088 SHOW_PARSE_INDENT
9089 char ach[32];
9090 sprintf(ach, "GOTO [" HOST_SIZE_T_PRINT_DEC "]:",
9091 (fmt_size_t)(i-1));
9092 SHOW_PARSE_TEXT(ach)
9093 SHOW_PARSE_END
9094 }
9095 gg_append_statement( go_to[i+1] );
9096 }
9097 parser_fi();
9098 }
9099
9100 // The astute observer will have noted that there is no way
9101 // for the generated runtime code to reach this point.
9102 //
9103 // We have, you see, reached the egress:
9104 gg_append_statement( tgt->addresses.exit.label );
9105 }
9106
9107 static void
9108 perform_inline_testafter_varying( struct cbl_perform_tgt_t *tgt,
9109 bool /*test_before*/,
9110 size_t N,
9111 struct cbl_perform_vary_t *varys )
9112 {
9113 SHOW_PARSE
9114 {
9115 SHOW_PARSE_HEADER
9116 SHOW_PARSE_END
9117 }
9118 // This is a PERFORM <inline> TEST AFTER VARYING
9119
9120 /*
9121
9122 GOTO SETUP
9123 TOP:
9124 S1
9125 S2
9126 EXIT PERFORM -- GOTO EXIT:
9127 S3
9128 S4
9129 EXIT PERFORM CYCLE -- GOTO TESTA
9130 S5
9131 S6
9132 GOTO TESTA:
9133
9134 SETUP:
9135 MOVE FROM_0 TO VARYING_0
9136 INIT_1:
9137 MOVE FROM_1 TO VARYING_1
9138 INIT_2:
9139 MOVE FROM_2 TO VARYING_2
9140 . . . . . . . . . . . . . . . . . .
9141 INIT_N-2:
9142 MOVE FROM_N-2 TO VARYING_N-2
9143 INIT_N-1:
9144 MOVE FROM_N-1 TO VARYING_N-1
9145 GOTO TOP
9146 TESTA:
9147 TEST_N-1:
9148 IF NOT CONDITION_N-1
9149 ADD BY_N-1 TO VARYING_N-1
9150 GOTO TOP
9151 IF NOT CONDITION_N-2
9152 ADD BY_N-2 TO VARYING_N-2
9153 GOTO INIT_N-1
9154 IF NOT CONDITION_N-3
9155 ADD BY_N-3 TO VARYING_N-3
9156 GOTO INIT_N-2
9157 . . . . . . . . . . . . . . . . . .
9158 IF NOT CONDITION_1
9159 ADD BY_1 TO VARYING_1
9160 GOTO INIT_2
9161 IF NOT CONDITION_0
9162 ADD BY_0 TO VARYING_0
9163 GOTO INIT_1
9164 // At this point, all conditions are true
9165 EXIT:
9166
9167 */
9168
9169 // So, we're going to do that. But because the initializations
9170 // and the testing are so nicely loopish, we're going to let
9171 // the computer create them for us.
9172
9173 // We are going to need a set of N label pairs. Actually, we
9174 // only need N-1; we don't use the zeroth pair. But the code
9175 // is cleaner if we just build all N of them.
9176
9177 tree go_to[MAX_AFTERS];
9178 tree label[MAX_AFTERS];
9179
9180 build_N_pairs(go_to, label, N);
9181
9182 // At this point the code being laid down, the GOTO SETUP was created,
9183 // followed by the stream of statements. We terminate it with a
9184 // goto testa
9185 gg_append_statement(tgt->addresses.testA.go_to);
9186
9187 // See the comment in create_iline_address_pairs()
9188 //gg_force_line_number(tgt->addresses.line_number_of_setup_code-1);
9189
9190 // That's followed by the SETUP target:
9191 gg_append_statement(tgt->addresses.setup.label);
9192
9193 // We now build the initialization section,
9194 for(size_t i=0; i<N; i++)
9195 {
9196 gg_append_statement(label[i]);
9197 parser_move(varys[i].varying, varys[i].from);
9198 }
9199
9200 // Having done all the initialization, we jump back to the start of
9201 // the list of statements:
9202 gg_append_statement(tgt->addresses.top.go_to);
9203
9204 // The list of statements ends with a goto TESTA, and that;s here:
9205 gg_append_statement(tgt->addresses.testA.label);
9206
9207 // Build the test section
9208 // (The oddball test is because N is a size_t, and can't go negative)
9209 for(size_t i=N-1; i<N; i--)
9210 {
9211 // Jump to where the conditional is calculated...
9212 gg_append_statement(tgt->addresses.condinto[i].go_to);
9213
9214 // ...and lay down the label to get back from there
9215 gg_append_statement(tgt->addresses.condback[i].label);
9216
9217 // Test the newly-recalculated conditional:
9218 parser_if( varys[i].until );
9219 // Condition is true; so we'll fall through
9220 parser_else();
9221 // Condition is false, so we increment, and keep going:
9222 parser_add(varys[i].varying, varys[i].by, varys[i].varying);
9223 if( i == N-1 )
9224 {
9225 gg_append_statement(tgt->addresses.top.go_to);
9226 }
9227 else
9228 {
9229 gg_append_statement(go_to[i+1]);
9230 }
9231 parser_fi();
9232 }
9233
9234 // Arriving here means that we all of the conditions were
9235 // true. So, we're done.
9236 gg_append_statement( tgt->addresses.exit.label );
9237 }
9238
9239 static void
9240 perform_inline_impl( struct cbl_perform_tgt_t *tgt,
9241 bool test_before,
9242 size_t N,
9243 struct cbl_perform_vary_t *varys )
9244 {
9245 SHOW_PARSE
9246 {
9247 SHOW_PARSE_HEADER
9248 SHOW_PARSE_END
9249 }
9250 if( N == 1 && !varys[0].varying.field )
9251 {
9252 perform_inline_until(tgt, test_before, N, varys);
9253 }
9254 else
9255 {
9256 // This is a PERFORM proc-1 [through proc-2] VARYING
9257 if( !test_before )
9258 {
9259 perform_inline_testafter_varying(tgt, test_before, N, varys);
9260 }
9261 else
9262 {
9263 perform_inline_testbefore_varying(tgt, test_before, N, varys);
9264 }
9265 }
9266 }
9267
9268 void
9269 parser_perform_until( struct cbl_perform_tgt_t *tgt,
9270 bool test_before,
9271 size_t N,
9272 struct cbl_perform_vary_t *varys )
9273 {
9274 Analyze();
9275 SHOW_PARSE
9276 {
9277 SHOW_PARSE_HEADER
9278 SHOW_PARSE_TEXT(" cbl_perform_tgt_t is at")
9279 char ach[32];
9280 sprintf(ach, " %p", static_cast<void*>(tgt));
9281 SHOW_PARSE_TEXT(ach);
9282 SHOW_PARSE_LABEL(" ", tgt->from())
9283 if( tgt->to() )
9284 {
9285 SHOW_PARSE_LABEL(" THROUGH", tgt->to())
9286 }
9287 SHOW_PARSE_END
9288 }
9289
9290 if( tgt->from()->type != LblLoop )
9291 {
9292 perform_outofline( tgt, test_before, N, varys);
9293 }
9294 else
9295 {
9296 perform_inline_impl( tgt, test_before, N, varys);
9297 }
9298 }
9299
9300 void
9301 parser_perform_inline_times(struct cbl_perform_tgt_t *tgt,
9302 struct cbl_refer_t how_many )
9303 {
9304 Analyze();
9305 SHOW_PARSE
9306 {
9307 SHOW_PARSE_HEADER
9308 SHOW_PARSE_LABEL("", tgt->from());
9309 SHOW_PARSE_REF(" how_many is ", how_many);
9310 SHOW_PARSE_END
9311 }
9312
9313 TRACE1
9314 {
9315 TRACE1_HEADER
9316 TRACE1_FIELD(" into ", how_many.field, " times");
9317 TRACE1_END
9318 }
9319
9320 gcc_assert(tgt);
9321 cbl_field_t *count = how_many.field;
9322 if( how_many.is_reference() )
9323 {
9324 cbl_internal_error("%s:%d: ignoring subscripts", __func__, __LINE__);
9325 }
9326 CHECK_FIELD(count);
9327
9328 // This has to be on the stack, because performs can be nested
9329 tree counter = gg_define_variable(LONG);
9330
9331 /*
9332 GOTO SETUP
9333 TOP: S1
9334 EXIT PERFORM --> GOTO EXIT
9335 S2
9336 EXIT PERFORM CYCLE --> GOTO TEST
9337 S3
9338 TESTA:
9339 TEST: INCREMENT COUNTER
9340 IF COUNTER LT LIMIT
9341 GOTO TOP
9342 ELSE
9343 GOTO EXIT
9344 SETUP: INITIALIZE COUNTER
9345 GOTO TOP
9346 EXIT:
9347 */
9348
9349 // At this point, the GOTO SETUP, the label "TOP:" and the
9350 // body of the inline perform have been laid down.
9351
9352 // Tack on the label for TEST and TESTA
9353 gg_append_statement( tgt->addresses.testA.label );
9354 gg_append_statement( tgt->addresses.test.label );
9355
9356 gg_decrement(counter);
9357 // Do the test:
9358 IF( counter, gt_op, gg_cast(LONG, integer_zero_node) )
9359 // We continue
9360 SHOW_PARSE
9361 {
9362 SHOW_PARSE_INDENT
9363 SHOW_PARSE_TEXT("If still counting GOTO TOP")
9364 SHOW_PARSE_END
9365 }
9366 gg_append_statement( tgt->addresses.top.go_to );
9367 ELSE
9368 // We are done
9369 SHOW_PARSE
9370 {
9371 SHOW_PARSE_INDENT
9372 SHOW_PARSE_TEXT("If count complete GOTO EXIT")
9373 SHOW_PARSE_END
9374 }
9375 gg_append_statement( tgt->addresses.exit.go_to );
9376 ENDIF
9377
9378 // Lay down the SETUP: label
9379 SHOW_PARSE
9380 {
9381 SHOW_PARSE_INDENT
9382 SHOW_PARSE_TEXT("LABEL SETUP:")
9383 SHOW_PARSE_END
9384 }
9385
9386 gg_append_statement( tgt->addresses.setup.label );
9387
9388 // Get the count:
9389 SHOW_PARSE
9390 {
9391 SHOW_PARSE_INDENT
9392 SHOW_PARSE_TEXT("Access the how_many parameter")
9393 SHOW_PARSE_REF(" ", how_many)
9394 SHOW_PARSE_END
9395 }
9396
9397 get_binary_value( counter,
9398 NULL,
9399 count,
9400 size_t_zero_node);
9401
9402 SHOW_PARSE
9403 {
9404 SHOW_PARSE_INDENT
9405 SHOW_PARSE_TEXT("GOTO TOP")
9406 SHOW_PARSE_END
9407 }
9408
9409 // Make sure the initial count is valid:
9410 IF( counter, gt_op, gg_cast(LONG, integer_zero_node) )
9411 gg_append_statement( tgt->addresses.top.go_to );
9412 ELSE
9413 gg_append_statement( tgt->addresses.exit.go_to );
9414 ENDIF
9415
9416 SHOW_PARSE
9417 {
9418 SHOW_PARSE_INDENT
9419 SHOW_PARSE_TEXT("LABEL EXIT:")
9420 SHOW_PARSE_END
9421 }
9422 gg_append_statement( tgt->addresses.exit.label );
9423 }
9424
9425 void
9426 parser_set_conditional88( const cbl_refer_t& refer, bool which_way )
9427 {
9428 Analyze();
9429 struct cbl_field_t *tgt = refer.field;
9430 SHOW_PARSE
9431 {
9432 SHOW_PARSE_HEADER
9433 SHOW_PARSE_FIELD(" ", tgt)
9434 if( which_way )
9435 {
9436 SHOW_PARSE_TEXT(" TRUE");
9437 }
9438 else
9439 {
9440 SHOW_PARSE_TEXT(" FALSE");
9441 }
9442 SHOW_PARSE_END
9443 }
9444
9445 CHECK_FIELD(tgt);
9446
9447 struct cbl_field_t *parent = parent_of(tgt);
9448
9449 CHECK_FIELD(parent);
9450
9451 cbl_domain_t *src;
9452
9453 if( which_way )
9454 {
9455 src = tgt->data.domain_of();
9456 }
9457 else
9458 {
9459 src = tgt->data.false_value_of();
9460 }
9461
9462 // We want to set the LEVEL88 target to TRUE (or FALSE), so we need to set
9463 // the parent of this LEVEL88 to the first element in data.domain (or
9464 // data.false_value);
9465
9466 cbl_figconst_t figconst = cbl_figconst_of(src->first.name());
9467
9468 if( !figconst )
9469 {
9470 // We are dealing with an ordinary string.
9471 static size_t buffer_size = 0;
9472 static char *buffer = NULL;
9473 size_t length = src->first.size();
9474 raw_to_internal(&buffer, &buffer_size, src->first.name(), length);
9475 move_tree_to_field( parent,
9476 gg_string_literal(buffer));
9477 }
9478 else
9479 {
9480 // This is a figurative constant
9481 gg_call(VOID,
9482 "__gg__parser_set_conditional",
9483 gg_get_address_of(parent->var_decl_node),
9484 build_int_cst_type(INT, figconst),
9485 NULL_TREE);
9486 }
9487 }
9488
9489 static
9490 void set_user_status(struct cbl_file_t *file)
9491 {
9492 // This routine sets the user_status, if any, to the cblc_file_t::status
9493
9494 // We have to do it this way, because in the case where the file->user_status
9495 // is in linkage, the memory addresses can end up pointing to the wrong
9496 // places
9497 if(file->user_status)
9498 {
9499 cbl_field_t *user_status = cbl_field_of(symbol_at(file->user_status));
9500 gcc_assert( user_status );
9501 gg_call(VOID,
9502 "__gg__set_user_status",
9503 gg_get_address_of(user_status->var_decl_node),
9504 gg_get_address_of(file->var_decl_node),
9505 NULL_TREE);
9506 }
9507 }
9508
9509 void
9510 parser_file_add(struct cbl_file_t *file)
9511 {
9512 Analyze();
9513 SHOW_PARSE
9514 {
9515 SHOW_PARSE_HEADER
9516 if( file )
9517 {
9518 fprintf(stderr, " cbl_file_t: %s", file->name);
9519 if( file->record_length )
9520 {
9521 SHOW_PARSE_TEXT(" file->record_length is %s");
9522 SHOW_PARSE_TEXT(file->name);
9523 }
9524 else
9525 {
9526 SHOW_PARSE_TEXT(" file->record_length is ZERO")
9527 }
9528 }
9529 else
9530 {
9531 SHOW_PARSE_TEXT( " *file pointer is NULL")
9532 }
9533 SHOW_PARSE_END
9534 }
9535
9536 if( !file )
9537 {
9538 cbl_internal_error("%s: called with NULL *file", __func__);
9539 gcc_assert(file);
9540 }
9541
9542 TRACE1
9543 {
9544 TRACE1_HEADER
9545 TRACE1_TEXT("parser_file_add cbl_file_t ")
9546 TRACE1_TEXT(file->name);
9547 TRACE1_END
9548 }
9549
9550 /* The FD record can be flagged external. Without definitive information, I
9551 am going to assume that the *everything* in the cblc_file_t structure is
9552 GLOBAL EXTERNAL. If I have read the specification incorrectly, and it's
9553 possible for two programs to share a file connector but with, say, two
9554 different lists of keys, then the cblc_file_t structure will have to
9555 be changed to have one var_decl node for the common information, and a
9556 second one for local information.
9557
9558 */
9559
9560 gg_variable_scope_t scope;
9561 if( file->attr & external_e )
9562 {
9563 scope = vs_external;
9564 }
9565 else
9566 {
9567 scope = vs_static;
9568 }
9569
9570 char achName[2*sizeof(cbl_name_t)];
9571
9572 // Use the global structure template declaration to produce the specific
9573 // structure definition expression:
9574 strcpy(achName, "_");
9575 strcat(achName, file->name);
9576 strcat(achName, "_fc"); // For "File Connector"
9577 tree new_var_decl = gg_define_variable( cblc_file_type_node,
9578 achName,
9579 scope);
9580
9581 // We have to convert file->nkey and file->keys to the run-time formats.
9582
9583 // There can be 0 through N keys, and each of those keys has M fields. Each of
9584 // the M fields has a "unique" flag, which we pass along as an array of INTs.
9585
9586 int number_of_key_fields = 0;
9587 for( size_t i=0; i<file->nkey; i++ )
9588 {
9589 number_of_key_fields += file->keys[i].nfield;
9590 }
9591
9592 // We create an array of pointers for those fields, adding an additional
9593 // element for a NULL pointer to indicate the end of the list:
9594 strcpy(achName, "_");
9595 strcat(achName, file->name);
9596 strcat(achName, "_keys");
9597 tree array_of_keys = gg_define_variable(
9598 build_pointer_type(cblc_field_p_type_node),
9599 achName,
9600 scope);
9601 gg_assign(array_of_keys,
9602 gg_cast(build_pointer_type(cblc_field_p_type_node),
9603 gg_malloc(build_int_cst_type(SIZE_T,
9604 (number_of_key_fields+1)
9605 *int_size_in_bytes(VOID_P)))));
9606
9607 strcpy(achName, "_");
9608 strcat(achName, file->name);
9609 strcat(achName, "_keynum");
9610 tree key_numbers = gg_define_variable(build_pointer_type(INT),
9611 achName,
9612 scope);
9613 gg_assign(key_numbers,
9614 gg_cast(build_pointer_type(INT),
9615 gg_malloc(build_int_cst_type(SIZE_T,
9616 (number_of_key_fields+1)
9617 *int_size_in_bytes(INT)))));
9618
9619 strcpy(achName, "_");
9620 strcat(achName, file->name);
9621 strcat(achName, "_uniqs");
9622 tree unique_flags = gg_define_variable( build_pointer_type(INT),
9623 achName,
9624 scope);
9625 gg_assign(unique_flags,
9626 gg_cast(build_pointer_type(INT),
9627 gg_malloc(build_int_cst_type(SIZE_T,
9628 (number_of_key_fields+1)
9629 *int_size_in_bytes(INT)))));
9630
9631 size_t index = 0;
9632 for( size_t i=0; i<file->nkey; i++ )
9633 {
9634 for( size_t j=0; j<file->keys[i].nfield; j++ )
9635 {
9636 gg_assign(gg_array_value(array_of_keys, index),
9637 get_field_p(file->keys[i].fields[j]) );
9638
9639 gg_assign(gg_array_value(key_numbers, index),
9640 build_int_cst_type(INT, i+1));
9641
9642 gg_assign(gg_array_value(unique_flags, index),
9643 (file->keys[i].unique ? integer_one_node : integer_zero_node));
9644 index += 1;
9645 }
9646 }
9647 // Terminate the field list with a NULL:
9648 gg_assign( gg_array_value(array_of_keys, index), gg_cast(cblc_field_p_type_node, null_pointer_node) );
9649
9650 // Terminate the key-numbers list with a negative 1 as a guardrail:
9651 gg_assign( gg_array_value(key_numbers, index), integer_minusone_node );
9652
9653 // Terminate the uniques list with a zero, just to avoid garbage:
9654 gg_assign( gg_array_value(unique_flags, index), integer_zero_node );
9655
9656 cbl_file_t::varying_t varies = symbol_file_record_sizes(file);
9657
9658 gcc_assert(varies.min <= varies.max);
9659
9660 if(file->access == file_inaccessible_e)
9661 {
9662 cbl_internal_error(
9663 "%s:%d file %s access mode is %<file_inaccessible_e%> in %s",
9664 current_filename.back().c_str(),
9665 CURRENT_LINE_NUMBER,
9666 file->name,
9667 __func__);
9668 }
9669
9670 size_t symbol_table_index = symbol_index(symbol_elem_of(file));
9671
9672 gg_call(VOID,
9673 "__gg__file_init",
9674 gg_get_address_of(new_var_decl),
9675 gg_string_literal(file->name),
9676 build_int_cst_type(SIZE_T, symbol_table_index),
9677 array_of_keys,
9678 key_numbers,
9679 unique_flags,
9680 gg_get_address_of(symbol_file_record(file)->var_decl_node),
9681 get_field_p(file->password),
9682 get_field_p(file->user_status),
9683 get_field_p(file->vsam_status),
9684 get_field_p(file->record_length),
9685 get_field_p(file_status_register()),
9686 build_int_cst_type(SIZE_T, file->reserve),
9687 build_int_cst_type(INT, (int)file->org),
9688 build_int_cst_type(INT, (int)file->padding),
9689 build_int_cst_type(INT, (int)file->access),
9690 build_int_cst_type(INT, (int)file->optional),
9691 build_int_cst_type(SIZE_T, varies.min),
9692 build_int_cst_type(SIZE_T, varies.max),
9693 NULL_TREE);
9694 file->var_decl_node = new_var_decl;
9695 }
9696
9697 void
9698 parser_file_open( size_t nfiles, struct cbl_file_t *files[], int mode_char )
9699 {
9700 for(size_t i=0; i<nfiles; i++)
9701 {
9702 auto& file = files[i];
9703 parser_file_open(file, mode_char);
9704 }
9705 }
9706
9707 void
9708 parser_file_open( struct cbl_file_t *file, int mode_char )
9709 {
9710 Analyze();
9711 SHOW_PARSE
9712 {
9713 SHOW_PARSE_HEADER
9714 if(file)
9715 {
9716 SHOW_PARSE_TEXT(" ");
9717 SHOW_PARSE_TEXT(file->name);
9718 char ach[64];
9719 sprintf(ach, ", organization is %s", file_org_str(file->org));
9720 SHOW_PARSE_TEXT(ach);
9721 }
9722 else
9723 {
9724 SHOW_PARSE_TEXT(" *file is NULL")
9725 }
9726
9727 SHOW_PARSE_TEXT(", mode_char: ")
9728 char ach[2] = "";
9729 ach[0] = mode_char;
9730 SHOW_PARSE_TEXT(ach)
9731
9732 SHOW_PARSE_END
9733 }
9734
9735 if( !file )
9736 {
9737 cbl_internal_error("%<parser_file_open%> called with NULL *file");
9738 }
9739
9740 if( !file->var_decl_node )
9741 {
9742 cbl_internal_error("%<parser_file_open%> for %s called with NULL "
9743 "%<var_decl_node%>", file->name);
9744 }
9745
9746 if( mode_char == 'a' && (file->access != file_access_seq_e) )
9747 {
9748 cbl_internal_error("EXTEND can only be used where %s is ACCESS MODE SEQUENTIAL", file->name);
9749 }
9750
9751 TRACE1
9752 {
9753 TRACE1_HEADER
9754 TRACE1_TEXT("parser_file_open of ")
9755 TRACE1_TEXT(file->name);
9756 TRACE1_END
9757 }
9758
9759 // The cbl_file_t has a cbl_field_t *filename. This can be a FldAlphanumeric.
9760 // The runtime has a (char *)filename, so we need to
9761 // do a runtime conversion.
9762
9763 tree psz; // This is going to be either the name of the file, or the
9764 // possible run-time environment variable that will contain
9765 // the name of the file.
9766
9767 cbl_field_t *field_of_name = symbol_field_forward(file->filename);
9768 bool quoted_name = false;
9769 if( field_of_name->type == FldForward )
9770 {
9771 // The target of ASSIGN TO was unquoted, but didn't resolve to a
9772 // cbl_field_t. This means that the name of the field is an
9773 // environment variable that will hold the file name
9774 psz = gg_define_char_star();
9775 gg_assign(psz, gg_strdup(gg_string_literal(field_of_name->name)));
9776 }
9777 else
9778 {
9779 // The name is coming from a presumably FldAlphaNumeric variable
9780 psz = get_string_from(field_of_name);
9781 quoted_name = true;
9782 }
9783
9784 sv_is_i_o = true;
9785 store_location_stuff("OPEN");
9786 gg_call(VOID,
9787 "__gg__file_open",
9788 gg_get_address_of(file->var_decl_node),
9789 psz,
9790 build_int_cst_type(INT, mode_char),
9791 quoted_name ? integer_one_node : integer_zero_node,
9792 NULL_TREE);
9793 set_user_status(file);
9794 }
9795
9796 void
9797 parser_file_close( struct cbl_file_t *file, file_close_how_t how )
9798 {
9799 Analyze();
9800 SHOW_PARSE
9801 {
9802 SHOW_PARSE_HEADER
9803 if(file)
9804 {
9805 SHOW_PARSE_TEXT(" ");
9806 SHOW_PARSE_TEXT(file->name);
9807 }
9808 else
9809 {
9810 SHOW_PARSE_TEXT(" *file is NULL ")
9811 }
9812 SHOW_PARSE_END
9813 }
9814
9815 if( !file )
9816 {
9817 cbl_internal_error("%<parser_file_close%> called with NULL *file");
9818 }
9819
9820 if( !file->var_decl_node )
9821 {
9822 cbl_internal_error("%<parser_file_close%> for %s called with "
9823 "NULL %<file->var_decl_node%>", file->name);
9824 }
9825
9826 TRACE1
9827 {
9828 TRACE1_HEADER
9829 TRACE1_TEXT("parser_file_close of ")
9830 TRACE1_TEXT(file->name);
9831 TRACE1_END
9832 }
9833
9834 // We are done with the filename. The library routine will free "filename"
9835 // memory and set it back to null
9836
9837 sv_is_i_o = true;
9838 store_location_stuff("CLOSE");
9839 gg_call(VOID,
9840 "__gg__file_close",
9841 gg_get_address_of(file->var_decl_node),
9842 build_int_cst_type(INT, (int)how),
9843 NULL_TREE);
9844 set_user_status(file);
9845 }
9846
9847 void
9848 parser_file_read( struct cbl_file_t *file,
9849 cbl_refer_t /*data_dest*/,
9850 int where )
9851 {
9852 Analyze();
9853 // where = -2 means PREVIOUS
9854 // where = -1 means NEXT
9855 // where = 1 or more means key N, where N is one-based
9856 SHOW_PARSE
9857 {
9858 SHOW_PARSE_HEADER
9859 if(file)
9860 {
9861 SHOW_PARSE_TEXT(" ");
9862 SHOW_PARSE_TEXT(file->name);
9863 }
9864 else
9865 {
9866 SHOW_PARSE_TEXT(" *file is NULL")
9867 }
9868
9869 char ach[32];
9870 sprintf(ach, " where:%d", where);
9871 SHOW_PARSE_TEXT(ach)
9872 SHOW_PARSE_END
9873 }
9874
9875 if( where == 0 )
9876 {
9877 cbl_internal_error("%s:%d file %s 'where' is zero in %s",
9878 current_filename.back().c_str(),
9879 CURRENT_LINE_NUMBER,
9880 file->name,
9881 __func__);
9882 where = -1;
9883 }
9884
9885 if( !file )
9886 {
9887 cbl_internal_error("%<parser_file_read%> called with NULL *file");
9888 }
9889
9890 if( !file->var_decl_node )
9891 {
9892 cbl_internal_error("%<parser_file_read%> for %s called with "
9893 "NULL %<file->var_decl_node%>", file->name);
9894 }
9895
9896 if( !file )
9897 {
9898 cbl_internal_error("%<parser_file_read%> called with NULL *field");
9899 }
9900
9901 if( !file->var_decl_node )
9902 {
9903 cbl_internal_error("%<parser_file_read%> for %s called with "
9904 "NULL %<field->var_decl_node%>", file->name);
9905 }
9906
9907 if( file->access == file_access_seq_e && where >= 0)
9908 {
9909 cbl_internal_error("%s:%d file %s is RELATIVE/SEQUENTIAL, but %<where >= 0%>",
9910 current_filename.back().c_str(),
9911 CURRENT_LINE_NUMBER,
9912 file->name);
9913 where = -1;
9914 }
9915
9916 if( file->access == file_access_rnd_e && where < 0)
9917 {
9918 cbl_internal_error("%s:%d file %s is RELATIVE/RANDOM, but %<where < 0%>",
9919 current_filename.back().c_str(),
9920 CURRENT_LINE_NUMBER,
9921 file->name);
9922 where = 1;
9923 }
9924
9925 sv_is_i_o = true;
9926 store_location_stuff("READ");
9927 gg_call(VOID,
9928 "__gg__file_read",
9929 gg_get_address_of(file->var_decl_node),
9930 build_int_cst_type(INT, where),
9931 NULL_TREE);
9932 set_user_status(file);
9933
9934 TRACE1
9935 {
9936 TRACE1_HEADER
9937 TRACE1_TEXT("from ")
9938 TRACE1_TEXT(file->name);
9939 TRACE1_INDENT
9940 cbl_field_t *our_return_code
9941 = cbl_field_of(symbol_at(file_status_register()));
9942 TRACE1_FIELD("result: ", our_return_code, "");
9943 TRACE1_END
9944 }
9945 }
9946
9947 void
9948 parser_file_write( cbl_file_t *file,
9949 cbl_field_t *record_area,
9950 bool after,
9951 cbl_refer_t &advance,
9952 bool sequentially
9953 )
9954 {
9955 Analyze();
9956
9957 bool is_random = !( file->access == file_access_seq_e
9958 || file->access == file_inaccessible_e);
9959
9960 if( (is_random ? 1 : 0) != (sequentially ? 0 : 1) )
9961 {
9962 cbl_internal_error("%s:%d file %s 'sequentially' is %d in %s",
9963 current_filename.back().c_str(),
9964 CURRENT_LINE_NUMBER,
9965 file->name,
9966 sequentially ? 1 : 0,
9967 __func__);
9968 }
9969
9970 SHOW_PARSE
9971 {
9972 SHOW_PARSE_HEADER
9973 if(file)
9974 {
9975 SHOW_PARSE_TEXT(" ");
9976 SHOW_PARSE_TEXT(file->name);
9977 }
9978 else
9979 {
9980 SHOW_PARSE_TEXT(" *file is NULL")
9981 }
9982
9983 if( !advance.field )
9984 {
9985 SHOW_PARSE_TEXT(" automatic BEFORE ADVANCING 1 LINE")
9986 }
9987 else
9988 {
9989 if( after )
9990 {
9991 SHOW_PARSE_TEXT(" AFTER")
9992 }
9993 else
9994 {
9995 SHOW_PARSE_TEXT(" BEFORE")
9996 }
9997 SHOW_PARSE_REF(" ADVANCING ", advance);
9998 SHOW_PARSE_TEXT(" LINE(S)")
9999 }
10000
10001 SHOW_PARSE_END
10002 }
10003
10004 if( !file )
10005 {
10006 cbl_internal_error("%s: called with NULL *file", __func__);
10007 }
10008
10009 if( !file->var_decl_node )
10010 {
10011 cbl_internal_error("%s: for %s called with NULL %<file->var_decl_node%>",
10012 __func__, file->name);
10013 }
10014
10015 if( !file )
10016 {
10017 cbl_internal_error("%s: called with NULL *field", __func__);
10018 }
10019
10020 if( !file->var_decl_node )
10021 {
10022 cbl_internal_error( "%s: for %s called with NULL %<field->var_decl_node%>",
10023 __func__,
10024 file->name);
10025 }
10026
10027 static tree t_advance = gg_define_variable(INT, "..pfw_advance", vs_file_static);
10028 if(advance.field)
10029 {
10030 static tree value = gg_define_variable(INT, "..pfw_value", vs_file_static);
10031 get_binary_value( value,
10032 NULL,
10033 advance.field,
10034 refer_offset(advance));
10035 gg_assign(t_advance, gg_cast(INT, value));
10036 }
10037 else
10038 {
10039 if( file->org == file_line_sequential_e )
10040 {
10041 // ISO/IEC_1989-2014 and IBM say the default is AFTER advancing
10042 // MicroFocus and GnuCOBOL say the default is BEFORE advancing.
10043 // See the comment where the variable is defined:
10044 after = auto_advance_is_AFTER_advancing;
10045 gg_assign(t_advance, integer_one_node);
10046 }
10047 else
10048 {
10049 // The default for SEQUENTIAL is no vertical motion
10050 gg_assign(t_advance, integer_minusone_node);
10051 }
10052 }
10053
10054 gcc_assert(record_area);
10055 if( !record_area )
10056 {
10057 record_area = cbl_field_of(symbol_at(file->default_record));
10058 }
10059
10060 sv_is_i_o = true;
10061 store_location_stuff("WRITE");
10062 gg_call(VOID,
10063 "__gg__file_write",
10064 gg_get_address_of(file->var_decl_node),
10065 member(record_area, "data"),
10066 member(record_area, "capacity"),
10067 after ? integer_one_node : integer_zero_node,
10068 t_advance,
10069 is_random ? integer_one_node : integer_zero_node,
10070 NULL_TREE);
10071 set_user_status(file);
10072
10073 TRACE1
10074 {
10075 TRACE1_HEADER
10076 TRACE1_TEXT("to ")
10077 TRACE1_TEXT(file->name);
10078 TRACE1_INDENT
10079 if( advance.field )
10080 {
10081 TRACE1_INDENT
10082 if( after )
10083 {
10084 TRACE1_TEXT("AFTER")
10085 }
10086 else
10087 {
10088 TRACE1_TEXT("BEFORE")
10089 }
10090 TRACE1_REFER(" ADVANCING ", advance, " LINE(S)");
10091 }
10092 TRACE1_INDENT
10093 cbl_field_t *our_return_code
10094 = cbl_field_of(symbol_at(file_status_register()));
10095 TRACE1_FIELD("result: ", our_return_code, "");
10096 TRACE1_END
10097 }
10098 }
10099
10100 void
10101 parser_file_delete( struct cbl_file_t *file, bool /*sequentially*/ )
10102 {
10103 Analyze();
10104
10105 if( !file )
10106 {
10107 cbl_internal_error("The file pointer should not be null");
10108 abort(); // Because cppcheck doesn't recognize [[noerror]]
10109 }
10110
10111 bool sequentially = file->access == file_access_seq_e
10112 || file->org == file_sequential_e
10113 || file->org == file_line_sequential_e;
10114 SHOW_PARSE
10115 {
10116 SHOW_PARSE_HEADER
10117 {
10118 SHOW_PARSE_TEXT(" ");
10119 SHOW_PARSE_TEXT(file->name);
10120 if( sequentially )
10121 {
10122 SHOW_PARSE_TEXT(" sequentially")
10123 }
10124 else
10125 {
10126 SHOW_PARSE_TEXT(" sequentially")
10127 }
10128 }
10129 SHOW_PARSE_END
10130 }
10131
10132 sv_is_i_o = true;
10133 store_location_stuff("DELETE");
10134 gg_call(VOID,
10135 "__gg__file_delete",
10136 gg_get_address_of(file->var_decl_node),
10137 sequentially ? integer_zero_node : integer_one_node,
10138 NULL_TREE);
10139 set_user_status(file);
10140
10141 TRACE1
10142 {
10143 TRACE1_HEADER
10144 TRACE1_TEXT("parser_file_delete record ")
10145 TRACE1_TEXT(file->name);
10146 TRACE1_END
10147 }
10148 }
10149
10150 void
10151 parser_file_rewrite(cbl_file_t *file,
10152 cbl_field_t *record_area,
10153 bool sequentially )
10154 {
10155 Analyze();
10156 if( file->org == file_indexed_e
10157 && file->access == file_access_seq_e
10158 && !sequentially )
10159 {
10160 cbl_internal_error(
10161 "%s:%d file %s is INDEXED/SEQUENTIAL, but 'sequentially' is false",
10162 current_filename.back().c_str(),
10163 CURRENT_LINE_NUMBER,
10164 file->name);
10165 sequentially = true;
10166 }
10167
10168 SHOW_PARSE
10169 {
10170 SHOW_PARSE_HEADER
10171 if(file)
10172 {
10173 SHOW_PARSE_TEXT(" ");
10174 SHOW_PARSE_TEXT(file->name);
10175 }
10176 else
10177 {
10178 SHOW_PARSE_TEXT(" *file is NULL")
10179 }
10180 SHOW_PARSE_END
10181 }
10182
10183 gcc_assert(record_area);
10184 if( !record_area )
10185 {
10186 record_area = cbl_field_of(symbol_at(file->default_record));
10187 }
10188
10189 sv_is_i_o = true;
10190 store_location_stuff("REWRITE");
10191 gg_call(VOID,
10192 "__gg__file_rewrite",
10193 gg_get_address_of(file->var_decl_node),
10194 member(record_area, "capacity"),
10195 sequentially ? integer_zero_node : integer_one_node,
10196 NULL_TREE);
10197 set_user_status(file);
10198 }
10199
10200 /*
10201 * flk is first-last-key. Similar to parser_file_read, it is a
10202 * 1-based index, for consistency. Encoded values:
10203 * -1 FIRST
10204 * -2 LAST
10205 * 0 neither
10206 * >0 1-based index into cbl_file_t::keys
10207 */
10208 void
10209 parser_file_start(struct cbl_file_t *file,
10210 relop_t op,
10211 int flk,
10212 cbl_refer_t length_ref )
10213 {
10214 Analyze();
10215 SHOW_PARSE
10216 {
10217 SHOW_PARSE_HEADER
10218 if(file)
10219 {
10220 SHOW_PARSE_TEXT(" ");
10221 SHOW_PARSE_TEXT(file->name);
10222 switch(op)
10223 {
10224 case lt_op:
10225 SHOW_PARSE_TEXT(" lt_op")
10226 break;
10227 case le_op:
10228 SHOW_PARSE_TEXT(" le_op")
10229 break;
10230 case eq_op:
10231 SHOW_PARSE_TEXT(" eq_op")
10232 break;
10233 case ne_op:
10234 SHOW_PARSE_TEXT(" ne_op")
10235 break;
10236 case ge_op:
10237 SHOW_PARSE_TEXT(" ge_op")
10238 break;
10239 case gt_op:
10240 SHOW_PARSE_TEXT(" gt_op")
10241 break;
10242 }
10243 char ach[32];
10244 sprintf(ach, " first-last-key:%d", flk);
10245 SHOW_PARSE_TEXT(ach)
10246 SHOW_PARSE_REF(" length:", length_ref);
10247 }
10248 else
10249 {
10250 SHOW_PARSE_TEXT(" *file is NULL")
10251 }
10252 SHOW_PARSE_END
10253 }
10254
10255 if( flk == 0
10256 && (file->org == file_indexed_e || file->org == file_relative_e) )
10257 {
10258 flk = 1;
10259 op = eq_op;
10260 }
10261
10262 if( flk == 0
10263 && (file->org == file_sequential_e) )
10264 {
10265 flk = -1;
10266 }
10267
10268 static tree length = gg_define_variable(SIZE_T, "..pfs_length", vs_file_static);
10269 gg_assign(length, size_t_zero_node);
10270
10271 if( flk > 0 && !length_ref.field )
10272 {
10273 // We need a length, and we don't have one. We have to calculate the length
10274 // from the lengths of the fields that make up the specified key.
10275
10276 size_t combined_length = 0;
10277
10278 gcc_assert(flk <= (int)file->nkey);
10279
10280 int key_number = flk-1;
10281
10282 // A key has a number of fields
10283 for(size_t ifield=0; ifield<file->keys[key_number].nfield; ifield++)
10284 {
10285 size_t nfield = file->keys[key_number].fields[ifield];
10286 cbl_field_t *field = cbl_field_of(symbol_at(nfield));
10287 combined_length += field->data.capacity;
10288 }
10289 gg_assign(length, build_int_cst_type(SIZE_T, combined_length));
10290 }
10291 else if( flk > 0 )
10292 {
10293 get_binary_value( length,
10294 NULL,
10295 length_ref.field,
10296 refer_offset(length_ref));
10297 }
10298
10299 sv_is_i_o = true;
10300 store_location_stuff("START");
10301 gg_call(VOID,
10302 "__gg__file_start",
10303 gg_get_address_of(file->var_decl_node),
10304 build_int_cst_type(INT, op),
10305 build_int_cst_type(INT, flk),
10306 length,
10307 NULL_TREE );
10308 set_user_status(file);
10309 }
10310
10311 static void
10312 inspect_tally(bool backward,
10313 const cbl_refer_t &identifier_1,
10314 cbl_inspect_opers_t& identifier_2)
10315 {
10316 Analyze();
10317 // This is an INSPECT FORMAT 1
10318 SHOW_PARSE
10319 {
10320 SHOW_PARSE_HEADER
10321 char ach[128];
10322 sprintf(ach, "There are %lu identifier_2", gb4(identifier_2.size()));
10323 SHOW_PARSE_TEXT(ach);
10324 for(size_t i=0; i<identifier_2.size(); i++)
10325 {
10326 SHOW_PARSE_INDENT
10327 sprintf(ach, "%lu: bounds: %lu", gb4(i), gb4(identifier_2[i].nbound()));
10328 SHOW_PARSE_TEXT(ach);
10329 for(size_t j=0; j<identifier_2[i].nbound(); j++)
10330 {
10331 SHOW_PARSE_INDENT
10332 sprintf(ach, " %lu: matches: %lu",
10333 gb4(j), gb4(identifier_2[i][j].matches.size()));
10334 SHOW_PARSE_TEXT(ach);
10335
10336 SHOW_PARSE_INDENT
10337 if( identifier_2[i][j].bound == bound_characters_e )
10338 {
10339 SHOW_PARSE_TEXT(" bound_characters");
10340 }
10341 else
10342 {
10343 SHOW_PARSE_TEXT(" bound_leading/all");
10344 }
10345
10346 if( identifier_2[i][j].matches.size() )
10347 {
10348 SHOW_PARSE_INDENT
10349 sprintf(ach, " before %p",
10350 as_voidp(identifier_2.at(i).at(j).matches.at(0).before.identifier_4.field));
10351 SHOW_PARSE_TEXT(ach);
10352 SHOW_PARSE_INDENT
10353 sprintf(ach, " after %p",
10354 as_voidp(identifier_2.at(i).at(j).matches.at(0).after.identifier_4.field));
10355 SHOW_PARSE_TEXT(ach);
10356 }
10357 }
10358 }
10359
10360 SHOW_PARSE_END
10361 }
10362
10363 // Make one pass through the inputs to count up the sizes of the arrays
10364 // we will be passing to the library routines. This loop structure simply
10365 // anticipates the more complex one that follows.
10366
10367 size_t int_index = 0;
10368 size_t pcbl_index = 0;
10369 unsigned long n_identifier_2 = identifier_2.size();
10370
10371 // The first integer is the all-important controlling count:
10372 int_index++;
10373
10374 // The first refer is for identifier-1
10375 pcbl_index++;
10376
10377 for( size_t i=0; i<n_identifier_2; i++)
10378 {
10379 // Each identifier-2 has to go into the array:
10380 pcbl_index++;
10381 // For each FOR there is a count of the loops after the FOR
10382 int_index++;
10383 for(size_t j=0; j<identifier_2[i].nbound(); j++)
10384 {
10385 // After each identifier-2, there is a cbl_inspect_bound_t value:
10386 int_index++;
10387 if( identifier_2[i][j].bound == bound_characters_e)
10388 {
10389 // This is a FOR CHARACTERS PHRASE1, so we will need before/after
10390 // for each:
10391 pcbl_index++;
10392 pcbl_index++;
10393 }
10394 else
10395 {
10396 // This is ALL or LEADING. Each has some number of identifier-3
10397 int_index++;
10398 for(size_t k=0; k<identifier_2[i][j].n_identifier_3(); k++)
10399 {
10400 // Put identifier-3 into the array:
10401 pcbl_index++;
10402
10403 // We need the PHRASE1 for that identifier-3
10404 pcbl_index++;
10405 pcbl_index++;
10406 }
10407 }
10408 }
10409 }
10410
10411 // We will be passing the library routine an array of size_t, which contains
10412 // all the integers and cbl_inspect_bound_t values, in a strict sequence so
10413 // that the library routine can peel them off.
10414
10415 static tree int_size = gg_define_variable(INT, "..pit_size", vs_file_static, 0);
10416 static tree integers = gg_define_variable(SIZE_T_P, "..pit", vs_file_static, null_pointer_node);
10417
10418 size_t n_integers = int_index;
10419
10420 IF( build_int_cst_type(INT, n_integers), gt_op, int_size )
10421 {
10422 gg_assign(int_size, build_int_cst_type(INT, n_integers));
10423 gg_assign(integers,
10424 gg_cast(SIZE_T_P,
10425 gg_realloc(integers,
10426 n_integers
10427 * int_size_in_bytes(VOID_P))));
10428 }
10429 ELSE
10430 {
10431 }
10432 ENDIF
10433
10434 const size_t n_resolveds = pcbl_index;
10435 std::vector<cbl_refer_t> pcbl_refers(n_resolveds);
10436
10437 // Now we make a second pass, populating those arrays:
10438 int_index = 0;
10439 pcbl_index = 0;
10440
10441 // The first integer is the all-important controlling count:
10442 gg_assign( gg_array_value(integers, int_index++),
10443 build_int_cst_type(SIZE_T, n_identifier_2) );
10444
10445 // The first refer is for identifier-1
10446 pcbl_refers[pcbl_index++] = identifier_1;
10447
10448 for( size_t i=0; i<n_identifier_2; i++)
10449 {
10450 // Each identifier-2 has to go into the array:
10451 pcbl_refers[pcbl_index++] = identifier_2[i].tally;
10452 // For each FOR there is a count of the loops after the FOR
10453 gg_assign( gg_array_value(integers, int_index++),
10454 build_int_cst_type(SIZE_T, identifier_2[i].nbound()) );
10455 for(size_t j=0; j<identifier_2[i].nbound(); j++)
10456 {
10457
10458 // After each identifier-2, there is a cbl_inspect_bound_t value:
10459 gg_assign( gg_array_value(integers, int_index++),
10460 build_int_cst_type(SIZE_T, identifier_2[i][j].bound));
10461 if( identifier_2[i][j].bound == bound_characters_e)
10462 {
10463 // This is a FOR CHARACTERS PHRASE1, so we will need before/after
10464 // for each:
10465 const auto& m( identifier_2[i][j].matches );
10466 if( m.empty() )
10467 {
10468 pcbl_index += 2;
10469 }
10470 else
10471 {
10472 pcbl_refers[pcbl_index++] = m[0].before.identifier_4;
10473 pcbl_refers[pcbl_index++] = m[0].after.identifier_4;
10474 }
10475 }
10476 else
10477 {
10478 // This is ALL or LEADING. Each has some number of identifier-3
10479 gg_assign( gg_array_value(integers, int_index++),
10480 build_int_cst_type(SIZE_T, identifier_2[i][j].n_identifier_3()));
10481 for(size_t k=0; k<identifier_2[i][j].n_identifier_3(); k++)
10482 {
10483 // Put identifier-3 into the array:
10484 pcbl_refers[pcbl_index++] = identifier_2[i][j].matches[k].matching();
10485
10486 // We need the PHRASE1 for that identifier-3
10487 pcbl_refers[pcbl_index++] = identifier_2[i][j].matches[k].before.identifier_4;
10488
10489 pcbl_refers[pcbl_index++] = identifier_2[i][j].matches[k].after.identifier_4;
10490 }
10491 }
10492 }
10493 }
10494
10495 //fprintf(stderr, " %ld %ld\n", int_index, n_integers);
10496 gcc_assert(int_index == n_integers);
10497 //fprintf(stderr, " %ld %ld\n", pcbl_index, n_resolveds);
10498 gcc_assert(pcbl_index == n_resolveds);
10499
10500 // We have built up an array of integers, and an array of cbl_refer_t.
10501 build_array_of_treeplets(1, pcbl_index, pcbl_refers.data());
10502
10503 // Do the actual call:
10504 gg_call(VOID,
10505 "__gg__inspect_format_1",
10506 backward ? integer_one_node : integer_zero_node,
10507 integers,
10508 NULL_TREE);
10509 }
10510
10511 static void
10512 inspect_replacing(int backward,
10513 const cbl_refer_t &identifier_1,
10514 cbl_inspect_opers_t &operations)
10515 {
10516 Analyze();
10517 // This is an INSPECT FORMAT 2
10518 SHOW_PARSE
10519 {
10520 SHOW_PARSE_HEADER
10521 SHOW_PARSE_TEXT(" ")
10522 }
10523
10524 // For REPLACING, unlike TALLY, there can be but one operation
10525 unsigned long n_ops = operations.size();
10526 gcc_assert(n_ops == 1);
10527
10528 size_t n_id_3 = 0;
10529 size_t n_id_4 = 0;
10530 size_t n_id_5 = 0;
10531 size_t n_all_leading_first = 0;
10532
10533 // Make one pass through the inputs to count up the sizes of the arrays
10534 // we will be passing to the library routines:
10535
10536 for( size_t j=0; j<operations[0].nbound(); j++)
10537 {
10538 if( operations[0][j].bound == bound_characters_e)
10539 {
10540 // This is a FOR CHARACTERS phrase
10541
10542 // Each will have an identifier-5:
10543 n_id_5 += 1;
10544
10545 // Each will have a PHRASE1 comprising BEFORE and AFTER identifier-4 values
10546 n_id_4 += 2;
10547 }
10548 else
10549 {
10550 // This is ALL, LEADING, or FIRST. Each has some number of identifier-3 values:
10551 // The n_identifier_3 value goes into the integer list, so we'll have
10552 // to make room for them:
10553 n_all_leading_first += 1;
10554
10555 // The n_identifier-3 values will go into the resolved values; we have to
10556 // leave room for them
10557 n_id_3 += operations[0][j].n_identifier_3();
10558
10559 // Likewise identifier-5 values:
10560 n_id_5 += operations[0][j].n_identifier_3();
10561
10562 // And each identifier-3 / identifier-5 pair has BEFORE and AFTER phrases:
10563 n_id_4 += 2 * operations[0][j].n_identifier_3();
10564 }
10565 }
10566
10567 // We will be passing the library routine an array of size_t, which contains
10568 // all the integers and cbl_inspect_bound_t values, in a strict sequence so
10569 // that the library routine can peel them off.
10570
10571 size_t n_integers = 1 // Room for operations[0].nbound()
10572 + operations[0].nbound() // Room for all the cbl_inspect_bound_t values
10573 + n_all_leading_first; // Room for all of the n_identifier_3 counts
10574
10575 static tree int_size = gg_define_variable(INT, "..pir_size", vs_file_static, 0);
10576 static tree integers = gg_define_variable(SIZE_T_P, "..pir", vs_file_static, null_pointer_node);
10577
10578 IF( build_int_cst_type(INT, n_integers), gt_op, int_size )
10579 {
10580 gg_assign(int_size, build_int_cst_type(INT, n_integers));
10581 gg_assign(integers,
10582 gg_cast(SIZE_T_P,
10583 gg_realloc(integers,
10584 n_integers
10585 * int_size_in_bytes(VOID_P))));
10586 }
10587 ELSE
10588 {
10589 }
10590 ENDIF
10591
10592 const size_t n_resolveds = 1 // Room for identifier-1
10593 + n_id_3 // Room for the identifier-3 variables
10594 + n_id_4 // Room for the identifier-4 variables
10595 + n_id_5; // Room for the identifier-5 variables
10596
10597 std::vector<cbl_refer_t> pcbl_refers(n_resolveds);
10598
10599 // Now we make a second pass, populating those arrays:
10600 size_t int_index = 0;
10601 size_t pcbl_index = 0;
10602
10603 // The first integer is the all-important controlling count:
10604 gg_assign( gg_array_value(integers, int_index++),
10605 build_int_cst_type(SIZE_T, operations[0].nbound()) );
10606
10607 // The first refer is for identifier-1
10608 pcbl_refers[pcbl_index++] = identifier_1;
10609
10610 for( size_t j=0; j<operations[0].nbound(); j++)
10611 {
10612 // For each FOR there is a count of the loops after the FOR
10613
10614 // For each operation, there is a cbl_inspect_bound_t value:
10615 gg_assign( gg_array_value(integers, int_index++),
10616 build_int_cst_type(SIZE_T, operations[0][j].bound));
10617 if( operations[0][j].bound == bound_characters_e)
10618 {
10619 // This is a FOR CHARACTERS PHRASE1
10620
10621 // Put in the identifier-5 replacement value:
10622 pcbl_refers[pcbl_index++] = operations[0][j].replaces[0].replacement;
10623
10624 // Each identifier-5 gets a PHRASE1:
10625 pcbl_refers[pcbl_index++] = operations[0][j].replaces[0].before.identifier_4;
10626 pcbl_refers[pcbl_index++] = operations[0][j].replaces[0].after.identifier_4;
10627
10628 SHOW_PARSE
10629 {
10630 if( j )
10631 {
10632 SHOW_PARSE_INDENT
10633 }
10634 SHOW_PARSE_FIELD("ID-5 ", operations[0][j].replaces[0].replacement.field)
10635 if(operations[0][j].replaces[0].before.identifier_4.field)
10636 {
10637 SHOW_PARSE_FIELD(" before ", operations[0][j].replaces[0].before.identifier_4.field)
10638 }
10639 if(operations[0][j].replaces[0].after.identifier_4.field)
10640 {
10641 SHOW_PARSE_FIELD(" after ", operations[0][j].replaces[0].after.identifier_4.field)
10642 }
10643 SHOW_PARSE_END
10644 }
10645 }
10646 else
10647 {
10648 // This is ALL or LEADING. Each has some number of identifier-3/identifier-5 pairs
10649 gg_assign( gg_array_value(integers, int_index++),
10650 build_int_cst_type(SIZE_T, operations[0][j].n_identifier_3()));
10651 for(size_t k=0; k<operations[0][j].n_identifier_3(); k++)
10652 {
10653 // Put identifier-3 into the array:
10654 pcbl_refers[pcbl_index++] = operations[0][j].replaces[k].matching();
10655
10656 // Put in the identifier-5 replacement value:
10657 pcbl_refers[pcbl_index++] = operations[0][j].replaces[k].replacement;
10658
10659 // We need the PHRASE1 for that identifier-3/identifier-5 pair:
10660 pcbl_refers[pcbl_index++] = operations[0][j].replaces[k].before.identifier_4;
10661
10662 pcbl_refers[pcbl_index++] = operations[0][j].replaces[k].after.identifier_4;
10663
10664 SHOW_PARSE
10665 {
10666 if( j || k )
10667 {
10668 SHOW_PARSE_INDENT
10669 }
10670 SHOW_PARSE_FIELD("ID-3 ", operations[0][j].replaces[k].matching().field)
10671 SHOW_PARSE_FIELD(" ID-5 ", operations[0][j].replaces[k].replacement.field)
10672 if( operations[0][j].replaces[k].before.identifier_4.field )
10673 {
10674 SHOW_PARSE_FIELD("before ", operations[0][j].replaces[k].before.identifier_4.field)
10675 }
10676 if(operations[0][j].replaces[k].after.identifier_4.field)
10677 {
10678 SHOW_PARSE_FIELD("after ", operations[0][j].replaces[k].after.identifier_4.field)
10679 }
10680 SHOW_PARSE_END
10681 }
10682 }
10683 }
10684 }
10685
10686 //fprintf(stderr, "%s: %ld %ld\n", __func__, int_index, n_integers);
10687 gcc_assert(int_index == n_integers);
10688 //fprintf(stderr, "%s: %ld %ld\n", __func__, pcbl_index, n_resolveds);
10689 gcc_assert(pcbl_index == n_resolveds);
10690
10691 // We have built up an array of integers, and an array of cbl_refer_t.
10692
10693 for(size_t i=0; i<pcbl_index; i++)
10694 {
10695 if( pcbl_refers[i].field && pcbl_refers[i].field->type == FldLiteralN )
10696 {
10697 fprintf(stderr, "INSPECT field %s shouldn't be a FldLiteralN\n",
10698 pcbl_refers[i].field->name);
10699 gcc_unreachable();
10700 }
10701 }
10702
10703 build_array_of_treeplets(1, pcbl_index, pcbl_refers.data());
10704
10705 // Do the actual call:
10706 gg_call(VOID,
10707 "__gg__inspect_format_2",
10708 backward ? integer_one_node : integer_zero_node,
10709 integers,
10710 NULL_TREE);
10711 }
10712
10713 void
10714 parser_inspect(const cbl_refer_t& identifier_1,
10715 bool backward,
10716 cbl_inspect_opers_t& operations)
10717 {
10718 Analyze();
10719 gcc_assert(! operations.empty());
10720
10721 /* Operating philosophy: We are going to minimize the amount of
10722 GENERIC tag creation here at compile time, mainly by eliminating
10723 the generation of cbl_resolved_t structures that we know
10724 contain no information. */
10725
10726 if( operations[0].tally.field )
10727 {
10728 // This is a FORMAT 1 "TALLYING"
10729 inspect_tally(backward, identifier_1, operations);
10730 }
10731 else
10732 {
10733 // This is a FORMAT 2 "REPLACING"
10734 inspect_replacing(backward, identifier_1, operations);
10735 }
10736 }
10737
10738 void
10739 parser_inspect_conv(cbl_refer_t input,
10740 bool backward,
10741 cbl_refer_t original,
10742 cbl_refer_t replacement,
10743 cbl_inspect_qual_t before,
10744 cbl_inspect_qual_t after )
10745 {
10746 Analyze();
10747 SHOW_PARSE
10748 {
10749 SHOW_PARSE_HEADER
10750 SHOW_PARSE_END
10751 }
10752
10753 gg_call(CHAR_P,
10754 "__gg__inspect_format_4",
10755 backward ? integer_one_node : integer_zero_node,
10756 input.field ? gg_get_address_of(input.field->var_decl_node)
10757 : null_pointer_node,
10758 refer_offset(input),
10759 refer_size_source(input),
10760 original.field ? gg_get_address_of(original.field->var_decl_node)
10761 : null_pointer_node,
10762 refer_offset(original),
10763 refer_size_dest(original),
10764 replacement.field ? gg_get_address_of(
10765 replacement.field->var_decl_node)
10766 : null_pointer_node,
10767 refer_offset(replacement),
10768 replacement.all ? build_int_cst_type(SIZE_T, -1LL)
10769 : refer_size_source(replacement),
10770 after.identifier_4.field ? gg_get_address_of(
10771 after.identifier_4.field->var_decl_node)
10772 : null_pointer_node,
10773 refer_offset(after.identifier_4),
10774 refer_size_source(after.identifier_4),
10775 before.identifier_4.field ? gg_get_address_of(
10776 before.identifier_4.field->var_decl_node)
10777 : null_pointer_node,
10778 refer_offset(before.identifier_4),
10779 refer_size_source(before.identifier_4),
10780 NULL_TREE
10781 );
10782 }
10783
10784 void
10785 parser_module_name( cbl_field_t *tgt, module_type_t type )
10786 {
10787 SHOW_PARSE
10788 {
10789 SHOW_PARSE_HEADER
10790 SHOW_PARSE_END
10791 }
10792 gg_call(VOID,
10793 "__gg__module_name",
10794 gg_get_address_of(tgt->var_decl_node),
10795 build_int_cst_type(INT, type),
10796 NULL_TREE);
10797 }
10798
10799 void
10800 parser_intrinsic_numval_c( cbl_field_t *f,
10801 cbl_refer_t& input,
10802 bool locale,
10803 cbl_refer_t& currency,
10804 bool anycase,
10805 bool test_numval_c ) // true for TEST-NUMVAL-C
10806 {
10807 Analyze();
10808 SHOW_PARSE
10809 {
10810 SHOW_PARSE_HEADER
10811 SHOW_PARSE_END
10812 }
10813 TRACE1
10814 {
10815 TRACE1_HEADER
10816 TRACE1_END
10817 }
10818 if( locale || anycase )
10819 {
10820 gcc_unreachable();
10821 }
10822 if( test_numval_c )
10823 {
10824 gg_call(INT,
10825 "__gg__test_numval_c",
10826 gg_get_address_of(f->var_decl_node),
10827 gg_get_address_of(input.field->var_decl_node),
10828 refer_offset(input),
10829 refer_size_source(input),
10830 currency.field ? gg_get_address_of(currency.field->var_decl_node) : null_pointer_node,
10831 refer_offset(currency),
10832 refer_size_source(currency),
10833 NULL_TREE
10834 );
10835 }
10836 else
10837 {
10838 gg_call(INT,
10839 "__gg__numval_c",
10840 gg_get_address_of(f->var_decl_node),
10841 gg_get_address_of(input.field->var_decl_node),
10842 refer_offset(input),
10843 refer_size_source(input),
10844 currency.field ? gg_get_address_of(currency.field->var_decl_node) : null_pointer_node,
10845 refer_offset(currency),
10846 refer_size_source(currency),
10847 NULL_TREE
10848 );
10849 }
10850 }
10851
10852 void
10853 parser_intrinsic_subst( cbl_field_t *f,
10854 const cbl_refer_t& ref1,
10855 size_t argc,
10856 cbl_substitute_t * argv )
10857 {
10858 Analyze();
10859 SHOW_PARSE
10860 {
10861 SHOW_PARSE_HEADER
10862 SHOW_PARSE_FIELD(" TO ", f)
10863 for(size_t i=0; i<argc; i++)
10864 {
10865 SHOW_PARSE_INDENT
10866 SHOW_PARSE_FIELD(" ", argv[i].orig.field)
10867 SHOW_PARSE_FIELD(" ", argv[i].replacement.field)
10868 }
10869 SHOW_PARSE_END
10870 }
10871 TRACE1
10872 {
10873 TRACE1_HEADER
10874 TRACE1_END
10875 }
10876
10877 sv_is_i_o = true;
10878 store_location_stuff("SUBSTITUTE");
10879 unsigned char *control_bytes =
10880 static_cast<unsigned char *>(xmalloc(argc * sizeof(unsigned char)));
10881 gcc_assert(control_bytes);
10882 std::vector<cbl_refer_t> arg1(argc);
10883 std::vector<cbl_refer_t> arg2(argc);
10884
10885 for(size_t i=0; i<argc; i++)
10886 {
10887 control_bytes[i] = (argv[i].anycase ?
10888 substitute_anycase_e : 0)
10889 + (argv[i].first_last == cbl_substitute_t::subst_first_e ?
10890 substitute_first_e : 0)
10891 + (argv[i].first_last == cbl_substitute_t::subst_last_e ?
10892 substitute_last_e : 0);
10893 arg1[i] = argv[i].orig;
10894 arg2[i] = argv[i].replacement;
10895 }
10896
10897 tree control = gg_array_of_bytes(argc, control_bytes);
10898
10899 build_array_of_treeplets(1, argc, arg1.data());
10900 build_array_of_treeplets(2, argc, arg2.data());
10901
10902 gg_call(VOID,
10903 "__gg__substitute",
10904 gg_get_address_of(f->var_decl_node),
10905 gg_get_address_of(ref1.field->var_decl_node),
10906 refer_offset(ref1),
10907 refer_size_source(ref1),
10908 build_int_cst_type(SIZE_T, argc),
10909 control,
10910 NULL_TREE);
10911
10912 gg_free(control);
10913
10914 free(control_bytes);
10915 }
10916
10917 void
10918 parser_intrinsic_callv( cbl_field_t *tgt,
10919 const char function_name[],
10920 size_t nrefs,
10921 cbl_refer_t *refs )
10922 {
10923 Analyze();
10924 // We have been given an array of refs[nrefs]. Each ref is a pointer
10925 // to a cbl_ref_t. We convert that to a table of pointers to run-time
10926 // cblc_ref_t structures, and we pass that to the function_name intrinsic
10927 // function. It is in charge of conversion to whatever form is needed.
10928
10929 // We get back a return value, which we convert to tgt based on the
10930 // intrinsic_return_type
10931
10932 SHOW_PARSE
10933 {
10934 SHOW_PARSE_HEADER
10935 SHOW_PARSE_TEXT(" of ")
10936 SHOW_PARSE_TEXT(function_name)
10937 fprintf(stderr, " with " HOST_SIZE_T_PRINT_DEC " parameters",
10938 (fmt_size_t)nrefs);
10939 SHOW_PARSE_END
10940 }
10941
10942 TRACE1
10943 {
10944 TRACE1_HEADER
10945 TRACE1_TEXT("about to call \"")
10946 TRACE1_TEXT(function_name)
10947 TRACE1_TEXT("\"")
10948 for(size_t i=0; i<nrefs; i++)
10949 {
10950 TRACE1_INDENT
10951 gg_fprintf(trace_handle, 1, "parameter %ld: ", build_int_cst_type(SIZE_T, i+1));
10952 TRACE1_REFER("", refs[i], "")
10953 }
10954 }
10955 store_location_stuff(function_name);
10956 tree ncount = build_int_cst_type(SIZE_T, nrefs);
10957
10958 build_array_of_fourplets(1, nrefs, refs);
10959
10960 gg_call(VOID,
10961 function_name,
10962 gg_get_address_of(tgt->var_decl_node),
10963 ncount,
10964 NULL_TREE);
10965
10966 TRACE1
10967 {
10968 TRACE1_INDENT
10969 TRACE1_FIELD("result: ", tgt, "")
10970 TRACE1_END
10971 }
10972 }
10973
10974 void
10975 parser_intrinsic_call_0(cbl_field_t *tgt,
10976 const char function_name[])
10977 {
10978 Analyze();
10979 SHOW_PARSE
10980 {
10981 SHOW_PARSE_HEADER
10982 SHOW_PARSE_TEXT(" of ")
10983 SHOW_PARSE_TEXT(function_name)
10984 SHOW_PARSE_END
10985 }
10986
10987 TRACE1
10988 {
10989 TRACE1_HEADER
10990 TRACE1_TEXT("about to call \"")
10991 TRACE1_TEXT(function_name)
10992 TRACE1_TEXT("\"")
10993 }
10994
10995 if( strcmp(function_name, "__gg__random") == 0 )
10996 {
10997 // We have no seed value, so call the "next" routine
10998 gg_call(VOID,
10999 "__gg__random_next",
11000 gg_get_address_of(tgt->var_decl_node),
11001 NULL_TREE);
11002 }
11003 else if( strcmp(function_name, "__gg__when_compiled") == 0 )
11004 {
11005 // Pass __gg__when_compiled() the time from right now.
11006 struct timespec tp;
11007 uint64_t now = get_time_nanoseconds();
11008 tp.tv_sec = now / 1000000000;
11009 tp.tv_nsec = now % 1000000000;
11010
11011 store_location_stuff(function_name);
11012 gg_call(VOID,
11013 function_name,
11014 gg_get_address_of(tgt->var_decl_node),
11015 build_int_cst(SIZE_T, tp.tv_sec),
11016 build_int_cst(LONG, tp.tv_nsec),
11017 NULL_TREE);
11018 }
11019 else
11020 {
11021 store_location_stuff(function_name);
11022 gg_call(VOID,
11023 function_name,
11024 gg_get_address_of(tgt->var_decl_node),
11025 NULL_TREE);
11026 }
11027
11028 TRACE1
11029 {
11030 TRACE1_INDENT
11031 TRACE1_FIELD("result: ", tgt, "")
11032 TRACE1_END
11033 }
11034 }
11035
11036 void
11037 parser_intrinsic_call_1( cbl_field_t *tgt,
11038 const char function_name[],
11039 cbl_refer_t& ref1 )
11040 {
11041 Analyze();
11042 SHOW_PARSE
11043 {
11044 SHOW_PARSE_HEADER
11045 SHOW_PARSE_TEXT(" of ")
11046 SHOW_PARSE_TEXT(function_name)
11047 SHOW_PARSE_END
11048 }
11049
11050 // There are special cases:
11051 if( strstr(function_name, "__gg__length") )
11052 {
11053 TRACE1
11054 {
11055 TRACE1_HEADER
11056 TRACE1_TEXT("about to call \"")
11057 TRACE1_TEXT(function_name)
11058 TRACE1_TEXT("\"")
11059 TRACE1_INDENT
11060 TRACE1_REFER("parameter: ", ref1, "")
11061 }
11062 size_t upper = ref1.field->occurs.bounds.upper
11063 ? ref1.field->occurs.bounds.upper : 1;
11064 if( ref1.nsubscript() )
11065 {
11066 upper = 1;
11067 }
11068
11069 if( is_table(ref1.field) && !ref1.nsubscript() )
11070 {
11071 static tree depending_on = gg_define_variable(LONG, "..pic1_dep");
11072 depending_on_value(depending_on, ref1.field);
11073 gg_call(VOID,
11074 "__gg__int128_to_field",
11075 gg_get_address_of(tgt->var_decl_node),
11076 gg_cast(INT128,
11077 gg_multiply(refer_size_source(ref1),
11078 depending_on)),
11079 integer_zero_node,
11080 build_int_cst_type(INT, truncation_e),
11081 null_pointer_node,
11082 NULL_TREE );
11083 }
11084 else
11085 {
11086 if( upper == 1 )
11087 {
11088 gg_call(VOID,
11089 "__gg__int128_to_field",
11090 gg_get_address_of(tgt->var_decl_node),
11091 gg_cast(INT128,
11092 refer_size_source(ref1)),
11093 integer_zero_node,
11094 build_int_cst_type(INT, truncation_e),
11095 null_pointer_node,
11096 NULL_TREE );
11097 }
11098 else
11099 {
11100 gg_call(VOID,
11101 "__gg__int128_to_field",
11102 gg_get_address_of(tgt->var_decl_node),
11103 gg_cast(INT128,
11104 gg_multiply(refer_size_source(ref1),
11105 build_int_cst_type(SIZE_T, upper))),
11106 integer_zero_node,
11107 build_int_cst_type(INT, truncation_e),
11108 null_pointer_node,
11109 NULL_TREE );
11110 }
11111 }
11112 }
11113 else
11114 {
11115 TRACE1
11116 {
11117 TRACE1_HEADER
11118 TRACE1_TEXT("about to call \"")
11119 TRACE1_TEXT(function_name)
11120 TRACE1_TEXT("\"")
11121 TRACE1_INDENT
11122 TRACE1_REFER("parameter: ", ref1, "")
11123 }
11124 gg_call(VOID,
11125 function_name,
11126 gg_get_address_of(tgt->var_decl_node),
11127 gg_get_address_of(ref1.field->var_decl_node),
11128 refer_offset(ref1),
11129 refer_size_source(ref1),
11130 NULL_TREE);
11131 }
11132
11133 TRACE1
11134 {
11135 TRACE1_INDENT
11136 TRACE1_FIELD("result: ", tgt, "")
11137 TRACE1_END
11138 }
11139 }
11140
11141 void
11142 parser_intrinsic_call_2( cbl_field_t *tgt,
11143 const char function_name[],
11144 cbl_refer_t& ref1,
11145 cbl_refer_t& ref2 )
11146 {
11147 Analyze();
11148 SHOW_PARSE
11149 {
11150 SHOW_PARSE_HEADER
11151 SHOW_PARSE_TEXT(" of ")
11152 SHOW_PARSE_TEXT(function_name)
11153 SHOW_PARSE_END
11154 }
11155 TRACE1
11156 {
11157 TRACE1_HEADER
11158 TRACE1_TEXT("about to call \"")
11159 TRACE1_TEXT(function_name)
11160 TRACE1_TEXT("\"")
11161 TRACE1_INDENT
11162 TRACE1_REFER("parameter 1: ", ref1, "")
11163 TRACE1_INDENT
11164 TRACE1_REFER("parameter 2: ", ref2, "")
11165 }
11166 store_location_stuff(function_name);
11167 gg_call(VOID,
11168 function_name,
11169 gg_get_address_of(tgt->var_decl_node),
11170 gg_get_address_of(ref1.field->var_decl_node),
11171 refer_offset(ref1),
11172 refer_size_source(ref1),
11173 ref2.field ? gg_get_address_of(ref2.field->var_decl_node) : null_pointer_node,
11174 refer_offset(ref2),
11175 refer_size_source(ref2),
11176 NULL_TREE);
11177 TRACE1
11178 {
11179 TRACE1_INDENT
11180 TRACE1_FIELD("result: ", tgt, "")
11181 TRACE1_END
11182 }
11183 }
11184
11185 void
11186 parser_intrinsic_call_3( cbl_field_t *tgt,
11187 const char function_name[],
11188 cbl_refer_t& ref1,
11189 cbl_refer_t& ref2,
11190 cbl_refer_t& ref3 )
11191 {
11192 Analyze();
11193 SHOW_PARSE
11194 {
11195 SHOW_PARSE_HEADER
11196 SHOW_PARSE_TEXT(" of ")
11197 SHOW_PARSE_TEXT(function_name)
11198 SHOW_PARSE_END
11199 }
11200
11201 TRACE1
11202 {
11203 TRACE1_HEADER
11204 TRACE1_TEXT("about to call \"")
11205 TRACE1_TEXT(function_name)
11206 TRACE1_TEXT("\"")
11207 TRACE1_INDENT
11208 TRACE1_REFER("parameter 1: ", ref1, "")
11209 TRACE1_INDENT
11210 TRACE1_REFER("parameter 2: ", ref2, "")
11211 TRACE1_INDENT
11212 TRACE1_REFER("parameter 3: ", ref3, "")
11213 }
11214
11215 store_location_stuff(function_name);
11216
11217 gg_call(VOID,
11218 function_name,
11219 gg_get_address_of(tgt->var_decl_node),
11220 ref1.field ? gg_get_address_of(ref1.field->var_decl_node) : null_pointer_node,
11221 refer_offset(ref1),
11222 refer_size_source(ref1),
11223 ref2.field ? gg_get_address_of(ref2.field->var_decl_node) : null_pointer_node,
11224 refer_offset(ref2),
11225 refer_size_source(ref2),
11226 ref3.field ? gg_get_address_of(ref3.field->var_decl_node) : null_pointer_node,
11227 refer_offset(ref3),
11228 refer_size_source(ref3),
11229 NULL_TREE);
11230 TRACE1
11231 {
11232 TRACE1_INDENT
11233 TRACE1_FIELD("result: ", tgt, "")
11234 TRACE1_END
11235 }
11236 }
11237
11238 void
11239 parser_intrinsic_call_4( cbl_field_t *tgt,
11240 const char function_name[],
11241 cbl_refer_t& ref1,
11242 cbl_refer_t& ref2,
11243 cbl_refer_t& ref3,
11244 cbl_refer_t& ref4 )
11245 {
11246 Analyze();
11247 SHOW_PARSE
11248 {
11249 SHOW_PARSE_HEADER
11250 SHOW_PARSE_TEXT(" of ")
11251 SHOW_PARSE_TEXT(function_name)
11252 SHOW_PARSE_END
11253 }
11254
11255 TRACE1
11256 {
11257 TRACE1_HEADER
11258 TRACE1_TEXT("about to call \"")
11259 TRACE1_TEXT(function_name)
11260 TRACE1_TEXT("\"")
11261 TRACE1_INDENT
11262 TRACE1_REFER("parameter 1: ", ref1, "")
11263 TRACE1_INDENT
11264 TRACE1_REFER("parameter 2: ", ref2, "")
11265 TRACE1_INDENT
11266 TRACE1_REFER("parameter 3: ", ref3, "")
11267 TRACE1_INDENT
11268 TRACE1_REFER("parameter 4: ", ref4, "")
11269 }
11270 store_location_stuff(function_name);
11271
11272 gg_call(VOID,
11273 function_name,
11274 gg_get_address_of(tgt->var_decl_node),
11275 ref1.field ? gg_get_address_of(ref1.field->var_decl_node) : null_pointer_node,
11276 refer_offset(ref1),
11277 refer_size_source(ref1),
11278 ref2.field ? gg_get_address_of(ref2.field->var_decl_node) : null_pointer_node,
11279 refer_offset(ref2),
11280 refer_size_source(ref2),
11281 ref3.field ? gg_get_address_of(ref3.field->var_decl_node) : null_pointer_node,
11282 refer_offset(ref3),
11283 refer_size_source(ref3),
11284 ref4.field ? gg_get_address_of(ref4.field->var_decl_node) : null_pointer_node,
11285 refer_offset(ref4),
11286 refer_size_source(ref4),
11287 NULL_TREE);
11288 TRACE1
11289 {
11290 TRACE1_INDENT
11291 TRACE1_FIELD("result: ", tgt, "")
11292 TRACE1_END
11293 }
11294 }
11295
11296 static void
11297 field_increment(cbl_field_t *fld)
11298 {
11299 static tree value = gg_define_variable(INT128, "..fi_value", vs_file_static);
11300 static tree rdigits = gg_define_variable(INT, "..fi_rdigits", vs_file_static);
11301 get_binary_value(value, rdigits, fld, size_t_zero_node);
11302 gg_assign( value,
11303 gg_add(value, gg_cast(SIZE_T, integer_one_node)));
11304 gg_call(VOID,
11305 "__gg__int128_to_field",
11306 gg_get_address_of(fld->var_decl_node),
11307 value,
11308 rdigits,
11309 build_int_cst_type(INT, truncation_e),
11310 null_pointer_node,
11311 NULL_TREE );
11312 }
11313
11314 static void
11315 create_lsearch_address_pairs(struct cbl_label_t *name)
11316 {
11317 // Create the lsearch structure
11318 name->structs.lsearch =
11319 static_cast<cbl_lsearch_t *>(xmalloc(sizeof(cbl_lsearch_t)));
11320 gcc_assert(name->structs.lsearch);
11321 cbl_lsearch_t *lsearch = name->structs.lsearch;
11322
11323 gg_create_goto_pair(&lsearch->addresses.at_exit.go_to,
11324 &lsearch->addresses.at_exit.label);
11325
11326 gg_create_goto_pair(&lsearch->addresses.top.go_to,
11327 &lsearch->addresses.top.label);
11328
11329 gg_create_goto_pair(&lsearch->addresses.bottom.go_to,
11330 &lsearch->addresses.bottom.label);
11331 }
11332
11333 void
11334 parser_next_sentence()
11335 {
11336 // Eventually we'll need this.
11337 }
11338
11339 void
11340 parser_lsearch_start( cbl_label_t *name,
11341 cbl_field_t *table,
11342 cbl_field_t *index,
11343 cbl_field_t *varying )
11344 {
11345 Analyze();
11346 SHOW_PARSE
11347 {
11348 SHOW_PARSE_HEADER
11349 if( name )
11350 {
11351 SHOW_PARSE_TEXT(" ")
11352 SHOW_PARSE_TEXT(name->name)
11353 }
11354 if( table )
11355 {
11356 SHOW_PARSE_TEXT(" linear search of ")
11357 SHOW_PARSE_TEXT(table->name)
11358 }
11359 if( index )
11360 {
11361 SHOW_PARSE_TEXT(" index is ")
11362 SHOW_PARSE_TEXT(index->name)
11363 }
11364 if( varying )
11365 {
11366 SHOW_PARSE_TEXT(" varying ")
11367 SHOW_PARSE_TEXT(varying->name)
11368 }
11369 SHOW_PARSE_END
11370 }
11371 // Create the goto/label pairs we are going to be needing:
11372 create_lsearch_address_pairs(name);
11373 cbl_lsearch_t *lsearch = name->structs.lsearch;
11374 lsearch->first_when = true;
11375
11376 // We need to find the first table element:
11377 cbl_field_t *current = table;
11378 while(current)
11379 {
11380 if( is_table(current) )
11381 {
11382 // Extract the number of elements in that rightmost dimension.
11383 lsearch->limit = gg_define_variable(LONG);
11384 depending_on_value(lsearch->limit, current);
11385 break;
11386 }
11387 current = parent_of(current);
11388 }
11389
11390 // Establish the initial value of our counter:
11391 lsearch->counter = gg_define_variable(LONG);
11392
11393 tree value = gg_define_int128();
11394 if(varying)
11395 {
11396 get_binary_value(value, NULL, varying, size_t_zero_node);
11397 }
11398 else if( index )
11399 {
11400 get_binary_value(value, NULL, index, size_t_zero_node);
11401 }
11402 gg_assign(lsearch->counter, gg_cast(LONG, value));
11403
11404 // And we need these around, so we can increment them:
11405 lsearch->index = index;
11406 lsearch->varying = varying;
11407
11408 // From here we have to jump to the top of the loop:
11409 gg_append_statement(lsearch->addresses.top.go_to);
11410
11411 // The next next instructions will be the body of the at-exit code, so
11412 // we need a label here so that we can get back to them
11413 gg_append_statement(lsearch->addresses.at_exit.label);
11414 }
11415
11416 void
11417 parser_lsearch_conditional(cbl_label_t * name)
11418 {
11419 Analyze();
11420 SHOW_PARSE
11421 {
11422 SHOW_PARSE_HEADER
11423 if( name )
11424 {
11425 SHOW_PARSE_TEXT(" ")
11426 SHOW_PARSE_TEXT(name->name)
11427 }
11428 SHOW_PARSE_END
11429 }
11430 cbl_lsearch_t *lsearch = name->structs.lsearch;
11431
11432 if( lsearch->first_when )
11433 {
11434 lsearch->first_when = false;
11435 // We are the first of the WHEN CONDITIONALs, which means we just laid down the final
11436 // statement of the AT-EXIT imperative statements, which means it's
11437 // time to leave the SEARCH completely.
11438 gg_append_statement(lsearch->addresses.bottom.go_to);
11439
11440 // And that puts us at the top of the loop:
11441 gg_append_statement(lsearch->addresses.top.label);
11442
11443 // It is at this point we check to see if we have reached the limit:
11444 IF( lsearch->counter, gt_op, lsearch->limit )
11445 // The counter has run out.
11446 gg_append_statement(lsearch->addresses.at_exit.go_to);
11447 ELSE
11448 // Just fall through into the following statements, which are
11449 // the statements for the conditional for the first WHEN
11450 ENDIF
11451 }
11452 else
11453 {
11454 // We are at the end of a WHEN TRUE imperative statement.
11455 gg_append_statement(lsearch->addresses.bottom.go_to);
11456
11457 // This is the second or later search_conditional. Note that the
11458 // code generated here executes after the first parser_when call, so
11459 // the jump_over label is ready to be placed.
11460
11461 // We have to lay down the unnamed label so the prior WHEN can jump past
11462 // its imperative statements when its condition is not met:
11463 gg_append_statement(lsearch->jump_over.label);
11464 }
11465 // At this point, the parser starts laying down the statements that make
11466 // up the next conditional.
11467 }
11468
11469 void
11470 parser_lsearch_when( cbl_label_t *name, cbl_field_t *conditional )
11471 {
11472 Analyze();
11473 SHOW_PARSE
11474 {
11475 SHOW_PARSE_HEADER
11476 if( name )
11477 {
11478 SHOW_PARSE_TEXT(" ")
11479 SHOW_PARSE_TEXT(name->name)
11480 }
11481 SHOW_PARSE_END
11482 }
11483 cbl_lsearch_t *lsearch = name->structs.lsearch;
11484
11485 // Arriving here means that all of the conditional statements have been
11486 // laid down, and we are ready to do the WHEN test:
11487
11488 parser_if(conditional);
11489 // We have found what we were looking for. Fall through to the next
11490 // set of instructions, which comprise the imperative statement
11491 // associated with the WHEN condition.
11492 ELSE
11493 // The conditional is false. We thus want to skip over the imperative
11494 // instructions that are about to be laid down.
11495
11496 // Create an unnamed goto/label pair:
11497 gg_create_goto_pair(&lsearch->jump_over.go_to,
11498 &lsearch->jump_over.label);
11499
11500 // And lay down the goto.
11501 gg_append_statement(lsearch->jump_over.go_to);
11502 ENDIF
11503 }
11504
11505 void
11506 parser_lsearch_end( cbl_label_t *name )
11507 {
11508 Analyze();
11509 SHOW_PARSE
11510 {
11511 SHOW_PARSE_HEADER
11512 if( name )
11513 {
11514 SHOW_PARSE_TEXT(" ")
11515 SHOW_PARSE_TEXT(name->name)
11516 }
11517 SHOW_PARSE_END
11518 }
11519 cbl_lsearch_t *lsearch = name->structs.lsearch;
11520
11521 // Arriving here means we have just laid down the final imperative
11522 // statements of the final WHEN. If these statements have been executing,
11523 // it's now time to leave the SEARCH:
11524 gg_append_statement(lsearch->addresses.bottom.go_to);
11525
11526 // It's time to lay down the last jump_over label:
11527 gg_append_statement(lsearch->jump_over.label);
11528
11529 // With that in place, we increment stuff:
11530 gg_assign(lsearch->counter, gg_add(lsearch->counter, gg_cast(LONG, integer_one_node)));
11531 field_increment(lsearch->index);
11532
11533 if( lsearch->varying )
11534 {
11535 field_increment(lsearch->varying);
11536 }
11537 // From here we jump to the top of the loop:
11538 gg_append_statement(lsearch->addresses.top.go_to);
11539
11540 // And that means we now lay down the label for the bottom
11541 gg_append_statement(lsearch->addresses.bottom.label);
11542
11543 // At this point, we are done with the lsearch structure
11544 free(lsearch);
11545 lsearch = NULL;
11546 }
11547
11548 void
11549 parser_bsearch_start( cbl_label_t* name,
11550 cbl_field_t *table )
11551 {
11552 Analyze();
11553 SHOW_PARSE
11554 {
11555 SHOW_PARSE_HEADER
11556 if( name )
11557 {
11558 SHOW_PARSE_TEXT(" ")
11559 SHOW_PARSE_TEXT(name->name)
11560 }
11561 if( table )
11562 {
11563 SHOW_PARSE_TEXT(" binary search of ")
11564 SHOW_PARSE_TEXT(table->name)
11565 }
11566 SHOW_PARSE_END
11567 }
11568
11569 // We need a cbl_bsearch_t structure:
11570 name->structs.bsearch =
11571 static_cast<cbl_bsearch_t *>(xmalloc(sizeof(cbl_bsearch_t)));
11572 gcc_assert(name->structs.bsearch);
11573 cbl_bsearch_t *bsearch = name->structs.bsearch;
11574
11575 // Create the address/label pairs we need
11576 gg_create_goto_pair(&bsearch->too_small.go_to,
11577 &bsearch->too_small.label);
11578
11579 gg_create_goto_pair(&bsearch->too_big.go_to,
11580 &bsearch->too_big.label);
11581
11582 gg_create_goto_pair(&bsearch->top.go_to,
11583 &bsearch->top.label);
11584
11585 gg_create_goto_pair(&bsearch->first_test.go_to,
11586 &bsearch->first_test.label);
11587
11588 gg_create_goto_pair(&bsearch->bottom.go_to,
11589 &bsearch->bottom.label);
11590
11591 // The logic when we first hit a WHEN needs to be different:
11592 bsearch->first_when = true;
11593
11594 // We need to find our table element:
11595 cbl_field_t *current = table;
11596 while(current)
11597 {
11598 if( is_table(current) )
11599 {
11600 break;
11601 }
11602 current = parent_of(current);
11603 }
11604
11605 CHECK_FIELD(current);
11606
11607 // There are a number of things we learn from the field "current"
11608
11609 // We get the index:
11610 gcc_assert(current->occurs.indexes.nfield);
11611 size_t index_index = current->occurs.indexes.fields[0];
11612 bsearch->index = cbl_field_of( symbol_at(index_index) );
11613 gcc_assert(bsearch->index);
11614
11615 // And we get the rightward bound of the number of elements:
11616 // Not that these are LONGS, not SIZE_T. If we are searching for something
11617 // that is smaller than element[0] of the table, then right ends up being
11618 // -1, so we have to have a signed type.
11619 bsearch->left = gg_define_variable(LONG, "_left");
11620 bsearch->right = gg_define_variable(LONG, "_right");
11621 bsearch->middle = gg_define_variable(LONG, "_middle");
11622
11623 // Assign the left and right values:
11624 gg_assign(bsearch->left, build_int_cst_type(LONG, 1));
11625 depending_on_value(bsearch->right, current);
11626
11627 // Create the variable that will take the compare result.
11628 bsearch->compare_result = gg_define_int();
11629
11630 // We now jump to the top of the binary testing loop, which comes right
11631 // after the labels where we handle non-equal cases:
11632 gg_append_statement(bsearch->top.go_to);
11633
11634 gg_append_statement(bsearch->too_small.label);
11635 // Arrive here when the element in the array is smaller than the one we are
11636 // looking for. This means that we move bsearch->left to the right:
11637 gg_assign(bsearch->left, gg_add(bsearch->middle, build_int_cst_type(LONG, 1)));
11638 gg_append_statement(bsearch->top.go_to);
11639
11640 gg_append_statement(bsearch->too_big.label);
11641 // Arrive here when the element in the array is larger than the one we
11642 // are looking for. This means we have to move bsearch->right to the left:
11643 gg_assign(bsearch->right, gg_subtract(bsearch->middle, build_int_cst_type(LONG, 1)));
11644 // Fall through to TOP:
11645
11646 gg_append_statement(bsearch->top.label);
11647 // Arrive here when it is time to check to see if we are done:
11648 IF( bsearch->left, le_op, bsearch->right )
11649 // We are not done. Calculate middle from 'left' and 'right'
11650 gg_assign( bsearch->middle,
11651 gg_add(bsearch->left, bsearch->right) );
11652 gg_assign( bsearch->middle,
11653 gg_divide(bsearch->middle, build_int_cst_type(LONG, 2) ));
11654 //gg_printf("BSEARCH At the top %ld %ld %ld\n", bsearch->left, bsearch->middle, bsearch->right, NULL_TREE);
11655 // We need to assign that value to bsearch->index. It might be possible
11656 // to assume that bsearch->index is a size_t and just cram the bytes into
11657 // place at bsearch->index->var_decl_node->data. But for now we'll
11658 // be cautious and use the slower, but more assured, method:
11659
11660 gg_call(VOID,
11661 "__gg__int128_to_field",
11662 gg_get_address_of(bsearch->index->var_decl_node),
11663 gg_cast(INT128, bsearch->middle),
11664 integer_zero_node,
11665 build_int_cst_type(INT, truncation_e),
11666 null_pointer_node,
11667 NULL_TREE );
11668 // And with middle/index established, we go do the WHEN clause:
11669 gg_append_statement(bsearch->first_test.go_to);
11670 ELSE
11671 // The search ended without finding anything. Fall through to the
11672 // AT-EXIT imperative statements that the parser will lay down right
11673 // after the call to parser_bsearch_start().
11674 ENDIF
11675 }
11676
11677 void
11678 parser_bsearch_conditional( cbl_label_t* name )
11679 {
11680 Analyze();
11681 SHOW_PARSE
11682 {
11683 SHOW_PARSE_HEADER
11684 if( name )
11685 {
11686 SHOW_PARSE_TEXT(" ")
11687 SHOW_PARSE_TEXT(name->name)
11688 }
11689 SHOW_PARSE_END
11690 }
11691 cbl_bsearch_t *bsearch = name->structs.bsearch;
11692
11693 if( bsearch->first_when )
11694 {
11695 bsearch->first_when = false;
11696 // The first time we arrive here is after the WHEN part of the SEARCH ALL
11697 // statement. We have just finished executing any AT-END statements there
11698 // might be, so it's time to jump to the bottom:
11699 gg_append_statement(bsearch->bottom.go_to);
11700
11701 // Otherwise, the TOP part of the loop just calculated the next middle/index,
11702 // and we now start processing it
11703
11704 gg_append_statement(bsearch->first_test.label);
11705 }
11706 // The second parser_bsearch_conditional() is caused by the appearance of
11707 // any subsequent AND clauses. And, it turns out, we do nothing.
11708
11709 // The parser lays down the statements that calculate the conditional,
11710 // and we just wait for parser_bsearch_when()
11711 }
11712
11713 bool
11714 is_ascending_key(const cbl_refer_t& key)
11715 {
11716 bool retval = true;
11717
11718 cbl_field_t *family_tree = key.field;
11719 while( family_tree )
11720 {
11721 if( family_tree->occurs.nkey )
11722 {
11723 break;
11724 }
11725 family_tree = parent_of(family_tree);
11726 }
11727
11728 CHECK_FIELD(family_tree);
11729 gcc_assert(family_tree->occurs.nkey);
11730
11731 for(size_t i=0; i<family_tree->occurs.nkey; i++)
11732 {
11733 for(size_t j=0; j<family_tree->occurs.keys[i].field_list.nfield; j++)
11734 {
11735 size_t index_of_field
11736 = family_tree->occurs.keys[i].field_list.fields[j];
11737 const cbl_field_t *key_field = cbl_field_of(symbol_at(index_of_field));
11738
11739 if( strcmp( key_field->name,
11740 key.field->name ) == 0 )
11741 {
11742 retval = family_tree->occurs.keys[i].ascending;
11743 goto done;
11744 }
11745 }
11746 }
11747
11748 done:
11749 return retval;
11750 }
11751
11752 void
11753 parser_bsearch_when(cbl_label_t* name,
11754 cbl_refer_t key,
11755 cbl_refer_t sarg,
11756 bool ascending)
11757 {
11758 Analyze();
11759 SHOW_PARSE
11760 {
11761 SHOW_PARSE_HEADER
11762 if( name )
11763 {
11764 SHOW_PARSE_TEXT(" ")
11765 SHOW_PARSE_TEXT(name->name)
11766 }
11767 SHOW_PARSE_END
11768 }
11769 cbl_bsearch_t *bsearch = name->structs.bsearch;
11770
11771 if( ascending )
11772 {
11773 cobol_compare( bsearch->compare_result,
11774 key,
11775 sarg );
11776 }
11777 else
11778 {
11779 cobol_compare( bsearch->compare_result,
11780 sarg,
11781 key );
11782 }
11783
11784 IF( bsearch->compare_result, lt_op, integer_zero_node )
11785 // The key is smaller than sarg:
11786 gg_append_statement(bsearch->too_small.go_to);
11787 ELSE
11788 ENDIF
11789 IF( bsearch->compare_result, gt_op, integer_zero_node )
11790 // The key is larger than sarg:
11791 gg_append_statement(bsearch->too_big.go_to);
11792 ELSE
11793 ENDIF
11794
11795 // We are at the Goldilocks point. The clause has been satisfied with
11796 // an equality, so we will just fall through to the next set of statements
11797 // that the parser laid down. They are either the next conditional, or
11798 // the final imperative statements that get executed when all the
11799 // clauses are satisfied.
11800 }
11801
11802 void
11803 parser_bsearch_end( cbl_label_t* name )
11804 {
11805 Analyze();
11806 SHOW_PARSE
11807 {
11808 SHOW_PARSE_HEADER
11809 if( name )
11810 {
11811 SHOW_PARSE_TEXT(" ")
11812 SHOW_PARSE_TEXT(name->name)
11813 }
11814 SHOW_PARSE_END
11815 }
11816 cbl_bsearch_t *bsearch = name->structs.bsearch;
11817
11818 // Arriving here means that either the search ran out without finding
11819 // anything, (see the test up at TOP:), or else we just fell through from
11820 // the statements that executed after all the WHEN/AFTER clauses were
11821 // satisifed by equality (meaning there were no jumps to TOO_SMALL: or
11822 // TOO_LARGE). In other words: we're done.
11823 gg_append_statement(bsearch->bottom.label);
11824
11825 free(bsearch);
11826 }
11827
11828 tree
11829 gg_array_of_field_pointers( size_t N,
11830 cbl_field_t **fields )
11831 {
11832 tree retval = gg_define_variable(build_pointer_type(cblc_field_p_type_node));
11833 gg_assign(retval, gg_cast(build_pointer_type(cblc_field_p_type_node),
11834 gg_malloc(build_int_cst_type(SIZE_T,
11835 N * int_size_in_bytes(VOID_P)))));
11836 for(size_t i=0; i<N; i++)
11837 {
11838 gg_assign(gg_array_value(retval, i), gg_get_address_of(fields[i]->var_decl_node));
11839 }
11840 return retval;
11841 }
11842
11843 static void
11844 push_program_state()
11845 {
11846 gg_call(VOID,
11847 "__gg__push_program_state",
11848 NULL_TREE);
11849 }
11850
11851 static void
11852 pop_program_state()
11853 {
11854 gg_call(VOID,
11855 "__gg__pop_program_state",
11856 NULL_TREE);
11857 }
11858
11859 void
11860 parser_sort(cbl_refer_t tableref,
11861 bool duplicates,
11862 cbl_alphabet_t *alphabet,
11863 const std::vector<cbl_key_t>& keys )
11864 {
11865 Analyze();
11866 SHOW_PARSE
11867 {
11868 SHOW_PARSE_HEADER
11869 if( tableref.field )
11870 {
11871 SHOW_PARSE_REF(" Sort table: ", tableref)
11872 }
11873 SHOW_PARSE_END
11874 }
11875
11876 cbl_field_t *table = tableref.field;
11877 gcc_assert(table);
11878 gcc_assert(table->var_decl_node);
11879 if( !is_table(table) )
11880 {
11881 cbl_internal_error( "%s: asked to sort %s, which is not a table",
11882 __func__,
11883 tableref.field->name);
11884 }
11885 size_t total_keys = std::accumulate( keys.begin(), keys.end(), 0,
11886 [](size_t n, const cbl_key_t& key ) {
11887 return n + key.fields.size();
11888 } );
11889 typedef const cbl_field_t * const_field_t;
11890 const_field_t *flattened_fields =
11891 static_cast<const_field_t *>(xmalloc(total_keys * sizeof(cbl_field_t *)));
11892 gcc_assert(flattened_fields);
11893 size_t *flattened_ascending =
11894 static_cast<size_t *>(xmalloc(total_keys * sizeof(size_t)));
11895 gcc_assert(flattened_ascending);
11896
11897 size_t key_index = 0;
11898 for( size_t i=0; i<keys.size(); i++ )
11899 {
11900 for( size_t j=0; j<keys[i].fields.size(); j++ )
11901 {
11902 flattened_fields[key_index] = keys[i].fields[j];
11903 flattened_ascending[key_index] = keys[i].ascending ? 1 : 0;
11904 key_index += 1;
11905 }
11906 }
11907
11908 // Create the array of cbl_field_t pointers for the keys
11909 tree all_keys = gg_array_of_field_pointers( total_keys,
11910 const_cast<cbl_field_t**>(flattened_fields));
11911
11912 // Create the array of integers that are the flags for ASCENDING:
11913 tree ascending = gg_array_of_size_t( total_keys, flattened_ascending );
11914
11915 tree depending_on = gg_define_variable(LONG, "_sort_size");
11916 depending_on_value(depending_on, table);
11917
11918 if( alphabet )
11919 {
11920 push_program_state();
11921 parser_alphabet_use(*alphabet);
11922 }
11923 gg_call(VOID,
11924 "__gg__sort_table",
11925 gg_get_address_of(tableref.field->var_decl_node),
11926 refer_offset(tableref),
11927 gg_cast(SIZE_T, depending_on),
11928 build_int_cst_type(SIZE_T, key_index),
11929 all_keys,
11930 ascending,
11931 duplicates ? integer_one_node : integer_zero_node,
11932 NULL_TREE);
11933 if( alphabet )
11934 {
11935 pop_program_state();
11936 }
11937
11938 free(flattened_ascending);
11939 free(flattened_fields);
11940
11941 gg_free(ascending);
11942 gg_free(all_keys);
11943 }
11944
11945 void
11946 parser_file_sort( cbl_file_t *workfile,
11947 bool duplicates,
11948 cbl_alphabet_t *alphabet,
11949 const std::vector<cbl_key_t>& keys,
11950 size_t ninput,
11951 cbl_file_t **inputs,
11952 size_t noutput,
11953 cbl_file_t **outputs,
11954 cbl_perform_tgt_t *in_proc,
11955 cbl_perform_tgt_t *out_proc )
11956 {
11957 Analyze();
11958 SHOW_PARSE
11959 {
11960 SHOW_PARSE_HEADER
11961 SHOW_PARSE_END
11962 }
11963 // This is the implementation of SORT FORMAT 1
11964
11965 // It proceeds in three phases.
11966
11967 // The first phase is absorbing the input and writing it out to the workfile:
11968
11969 parser_file_open(workfile, 'w');
11970 IF( member(workfile, "io_status"), ge_op, build_int_cst_type(INT, FsEofSeq) )
11971 {
11972 gg_printf("Couldn't open the SORT workfile for writing\n", NULL_TREE);
11973 gg_exit(integer_one_node);
11974 }
11975 ELSE
11976 ENDIF
11977
11978 if( in_proc && !ninput )
11979 {
11980 // We are getting our inputs from an input procedure
11981 parser_perform(in_proc, NULL);
11982 }
11983 else if( ninput && !in_proc )
11984 {
11985 // ninput means there was a USING clause, specifying input files.
11986
11987 // We are going to transfer the input file[s] to the workfile. The
11988 // transfer will be done so that any newlines in a LINE SEQUENTIAL file
11989 // are skipped, and so that any records that are too long, or too short,
11990 // are all normalized to the format of the SD record.
11991 for(size_t i=0; i<ninput; i++)
11992 {
11993 parser_file_open(inputs[i], 'r');
11994 IF( member(workfile, "io_status"), ge_op, build_int_cst_type(INT, FsEofSeq) )
11995 {
11996 gg_printf("Couldn't open the SORT USING file for input\n", NULL_TREE);
11997 gg_exit(integer_one_node);
11998 }
11999 ELSE
12000 ENDIF
12001
12002 gg_call(VOID,
12003 "__gg__file_sort_ff_input",
12004 gg_get_address_of(workfile-> var_decl_node),
12005 gg_get_address_of(inputs[i]->var_decl_node),
12006 NULL_TREE);
12007 parser_file_close(inputs[i]);
12008 }
12009 }
12010 else
12011 {
12012 // Having both or neither violates SORT syntax
12013 cbl_internal_error("%s: syntax error: both (or neither) USING "
12014 "and input-proc are specified",
12015 __func__);
12016 }
12017 parser_file_close(workfile);
12018
12019 // At this point, we have workfile of unsorted data. We have a library
12020 // routine that sorts the workfile. It needs the keys:
12021
12022 // The following is a tad more complex than it needs to be. It's a partial
12023 // clone of the code for handling multiple keys, each of which can have
12024 // multiple fields.
12025
12026 size_t total_keys = std::accumulate( keys.begin(), keys.end(), 0,
12027 []( size_t n, const cbl_key_t& key ) {
12028 return n + key.fields.size();
12029 } );
12030 typedef const cbl_field_t * const_field_t;
12031 auto flattened_fields
12032 = static_cast<const_field_t *>(xmalloc(total_keys * sizeof(cbl_field_t *)));
12033 gcc_assert(flattened_fields);
12034 size_t *flattened_ascending =
12035 static_cast<size_t *>(xmalloc(total_keys * sizeof(size_t)));
12036 gcc_assert(flattened_ascending);
12037
12038 size_t key_index = 0;
12039 for( size_t i=0; i<keys.size(); i++ )
12040 {
12041 for( size_t j=0; j<keys[i].fields.size(); j++ )
12042 {
12043 flattened_fields[key_index] = keys[i].fields[j];
12044 flattened_ascending[key_index] = keys[i].ascending ? 1 : 0;
12045 key_index += 1;
12046 }
12047 }
12048
12049 // Create the array of cbl_field_t pointers for the keys
12050 tree all_keys = gg_array_of_field_pointers( total_keys,
12051 const_cast<cbl_field_t**>(flattened_fields));
12052
12053 // Create the array of integers that are the flags for ASCENDING:
12054 tree ascending = gg_array_of_size_t( total_keys, flattened_ascending );
12055
12056 // We need to open the workfile for the sorting routine:
12057 parser_file_open(workfile, 'r');
12058 IF( member(workfile, "io_status"),
12059 ge_op,
12060 build_int_cst(INT, FhNotOkay) )
12061 {
12062 rt_error("Couldn't open workfile for sorting in parser_file_sort\n");
12063 }
12064 ELSE
12065 ENDIF
12066 if( alphabet )
12067 {
12068 push_program_state();
12069 parser_alphabet_use(*alphabet);
12070 }
12071 gg_call(VOID,
12072 "__gg__sort_workfile",
12073 gg_get_address_of(workfile->var_decl_node),
12074 build_int_cst_type(SIZE_T, key_index),
12075 all_keys,
12076 ascending,
12077 duplicates ? integer_one_node : integer_zero_node,
12078 NULL_TREE);
12079 if( alphabet )
12080 {
12081 pop_program_state();
12082 }
12083 parser_file_close(workfile);
12084
12085 free(flattened_ascending);
12086 free(flattened_fields);
12087 gg_free(ascending);
12088 gg_free(all_keys);
12089
12090 // The workfile is sorted. We move to Phase 3 -- transferring the workfile
12091 // to the output.
12092
12093 if( noutput && !out_proc)
12094 {
12095 // We have a GIVING phrase:
12096 for(size_t i=0; i<noutput; i++)
12097 {
12098 // Open WORKFILE again to position it at the beginning
12099 parser_file_open(workfile, 'r');
12100 IF( member(workfile, "io_status"),
12101 ge_op,
12102 build_int_cst(INT, FhNotOkay) )
12103 {
12104 rt_error("Couldn't open workfile for transfer to GIVING"
12105 "in parser_file_sort");
12106 }
12107 ELSE
12108 ENDIF
12109 parser_file_open(outputs[i], 'w');
12110 IF( member(outputs[i], "io_status"),
12111 ge_op,
12112 build_int_cst(INT, FhNotOkay) )
12113 {
12114 rt_error("Couldn't open GIVING file in parser_file_sort");
12115 }
12116 ELSE
12117 ENDIF
12118 gg_call(VOID,
12119 "__gg__file_sort_ff_output",
12120 gg_get_address_of(outputs[i]->var_decl_node),
12121 gg_get_address_of(workfile->var_decl_node),
12122 NULL_TREE);
12123 parser_file_close(outputs[i]);
12124 parser_file_close(workfile);
12125 }
12126 }
12127 else if (!noutput && out_proc)
12128 {
12129 // We are going to transfer the workfile to the output procedures.
12130 parser_file_open(workfile,'r');
12131 IF( member(workfile, "io_status"),
12132 ge_op,
12133 build_int_cst(INT, FhNotOkay) )
12134 {
12135 rt_error("Couldn't open workfile for stage-three "
12136 "output in parser_file_sort");
12137 }
12138 ELSE
12139 {
12140 parser_perform(out_proc, NULL);
12141 parser_file_close(workfile);
12142 }
12143 ENDIF
12144 }
12145 else
12146 {
12147 cbl_internal_error("%s: syntax error: both (or neither) GIVING "
12148 "and output-proc are specified", __func__);
12149 }
12150 }
12151
12152 void
12153 parser_release( cbl_field_t *record_area )
12154 {
12155 Analyze();
12156 SHOW_PARSE
12157 {
12158 SHOW_PARSE_HEADER
12159 SHOW_PARSE_END
12160 }
12161
12162 // When this routine is called, it writes the contents of 'record_area' to the
12163 // workfile specified by the cbl_file_t parent of record_area:
12164
12165 cbl_file_t *workfile = symbol_record_file(record_area);
12166
12167 gg_call(VOID,
12168 "__gg__file_write",
12169 gg_get_address_of( workfile->var_decl_node),
12170 member(record_area, "data"),
12171 member(record_area, "capacity"),
12172 integer_zero_node,
12173 integer_minusone_node,
12174 integer_zero_node,
12175 NULL_TREE); // non-random
12176 set_user_status(workfile);
12177 }
12178
12179 void
12180 parser_return_start( cbl_file_t *workfile, cbl_refer_t into )
12181 {
12182 Analyze();
12183 // This function helps implement the COBOL RETURN statement, which is used
12184 // in SORT and MERGE to "return" data from an intermediate sort/merge file
12185 // to SORT/MERGE output procedure.
12186
12187 SHOW_PARSE
12188 {
12189 SHOW_PARSE_HEADER
12190 SHOW_PARSE_END
12191 }
12192
12193 // We assume that workfile is open.
12194
12195 workfile->addresses = static_cast<cbl_sortreturn_t *>
12196 (xmalloc(sizeof(cbl_sortreturn_t)));
12197 gcc_assert(workfile->addresses);
12198 gg_create_goto_pair(&workfile->addresses->at_end.go_to,
12199 &workfile->addresses->at_end.label);
12200 gg_create_goto_pair(&workfile->addresses->not_at_end.go_to,
12201 &workfile->addresses->not_at_end.label);
12202 gg_create_goto_pair(&workfile->addresses->bottom.go_to,
12203 &workfile->addresses->bottom.label);
12204
12205 // Read the data from workfile into the SD record position:
12206 cbl_field_t *data_location = symbol_file_record(workfile);
12207 parser_file_read(workfile, data_location, -1 );
12208
12209 // And jump to either at_end or not_at_end, depending:
12210 IF( member(workfile, "io_status"), lt_op, build_int_cst(INT, FsEofSeq) )
12211 {
12212 // The read was successful. We move the result into place
12213 if( into.field )
12214 {
12215 cbl_field_t *record_area =
12216 cbl_field_of(symbol_at(workfile->default_record));
12217 parser_move(into, record_area, truncation_e);
12218 }
12219 // And having moved -- or not -- the record, jump to the not-at-end
12220 // imperative
12221 gg_append_statement(workfile->addresses->not_at_end.go_to);
12222 }
12223 ELSE
12224 ENDIF
12225
12226 IF( member(workfile, "io_status"), lt_op, build_int_cst(INT, FsKeySeq) )
12227 {
12228 // The read didn't succeed because of an end-of-file condition.
12229
12230 // Because there is an AT END clause, we suppress the error condition that
12231 // was raised.
12232 gg_assign(var_decl_exception_code, integer_zero_node);
12233
12234 // And then we jump to the at_end code:
12235 gg_append_statement(workfile->addresses->at_end.go_to);
12236 }
12237 ELSE
12238 ENDIF
12239
12240 // Arriving here means some kind of error condition. So, we don't do the
12241 // move, and we jump to the end of the statement
12242 gg_append_statement(workfile->addresses->bottom.go_to);
12243 }
12244
12245 void
12246 parser_return_atend( cbl_file_t *workfile )
12247 {
12248 Analyze();
12249 SHOW_PARSE
12250 {
12251 SHOW_PARSE_HEADER
12252 SHOW_PARSE_END
12253 }
12254
12255 // There might or might not be an at_end clause, and it might, or might
12256 // not, appear after a not_at_end clause. If we are appearing after
12257 // a not_at_end clause, we need to finish that clause with a jump to the
12258 // bottom of the logic:
12259 if( !workfile->addresses->not_at_end.label )
12260 {
12261 // We have been preceded by a not_at_end label. So, we need to
12262 // put in a jump to end those statements:
12263 gg_append_statement(workfile->addresses->bottom.go_to);
12264 }
12265 // And now we place the at_end label:
12266 gg_append_statement(workfile->addresses->at_end.label);
12267
12268 // And having placed it, NULL it out
12269 workfile->addresses->at_end.label = NULL;
12270
12271 // The imperative statements of the NOT AT END clause will follow
12272 }
12273
12274 void
12275 parser_return_notatend( cbl_file_t *workfile )
12276 {
12277 Analyze();
12278 SHOW_PARSE
12279 {
12280 SHOW_PARSE_HEADER
12281 SHOW_PARSE_END
12282 }
12283 // There might or might not be a not_at_end clause, and it might, or might
12284 // not, appear after a at_end clause. If we are appearing after
12285 // a at_end clause, we need to finish that clause with a jump to the
12286 // bottom of the logic:
12287 if( !workfile->addresses->at_end.label )
12288 {
12289 // We have been preceded by an at_end label. So, we need to
12290 // put in a jump to end those statements:
12291 gg_append_statement(workfile->addresses->bottom.go_to);
12292 }
12293 // And now we place the not_at_end label:
12294 gg_append_statement(workfile->addresses->not_at_end.label);
12295
12296 // And having placed it, NULL it out
12297 workfile->addresses->not_at_end.label = NULL;
12298
12299 // The imperative statements of the AT END clause will follow
12300 }
12301
12302 void
12303 parser_return_finish( cbl_file_t *workfile )
12304 {
12305 Analyze();
12306 SHOW_PARSE
12307 {
12308 SHOW_PARSE_HEADER
12309 SHOW_PARSE_END
12310 }
12311
12312 // If we are preceded by either an at_end or not_at_end clause, we need
12313 // to end those statements with a jump to the bottom:
12314 if( !workfile->addresses->at_end.label || !workfile->addresses->not_at_end.label)
12315 {
12316 gg_append_statement(workfile->addresses->bottom.go_to);
12317 }
12318
12319 // We need to place labels for clauses that weren't explicitly expressed
12320 // in the COBOL source code. (Both were explicit targets of goto statements
12321 // back in parser_return_start, so we need to place them here if they
12322 // weren't placed elsewhere)
12323 if( workfile->addresses->at_end.label )
12324 {
12325 gg_append_statement(workfile->addresses->at_end.label);
12326 }
12327 if( workfile->addresses->not_at_end.label )
12328 {
12329 gg_append_statement(workfile->addresses->not_at_end.label);
12330 }
12331 // And that brings us to the bottom:
12332 gg_append_statement(workfile->addresses->bottom.label);
12333
12334 free(workfile->addresses);
12335 }
12336
12337 static tree
12338 gg_array_of_file_pointers( size_t N,
12339 cbl_file_t **files )
12340 {
12341 tree retval = gg_define_variable(build_pointer_type(cblc_file_p_type_node));
12342 gg_assign(retval, gg_cast( build_pointer_type(cblc_file_p_type_node),
12343 gg_malloc( build_int_cst_type(SIZE_T,
12344 N * int_size_in_bytes(VOID_P)))));
12345 for(size_t i=0; i<N; i++)
12346 {
12347 gg_assign(gg_array_value(retval, i), gg_get_address_of(files[i]->var_decl_node));
12348 }
12349 return retval;
12350 }
12351
12352 void
12353 parser_file_merge( cbl_file_t *workfile,
12354 cbl_alphabet_t *alphabet,
12355 const std::vector<cbl_key_t>& keys,
12356 size_t ninputs,
12357 cbl_file_t **inputs,
12358 size_t noutputs,
12359 cbl_file_t **outputs,
12360 cbl_perform_tgt_t *out_proc )
12361 {
12362 Analyze();
12363 SHOW_PARSE
12364 {
12365 SHOW_PARSE_HEADER
12366 SHOW_PARSE_END
12367 }
12368
12369 // Our default file organization is LINE SEQUENTIAL, which spectacularly does
12370 // *not* work for a SORT workfile.
12371 if( workfile->org == file_line_sequential_e )
12372 {
12373 workfile->org = file_sequential_e;
12374 gg_assign( member(workfile->var_decl_node, "org"),
12375 build_int_cst_type(INT, file_sequential_e));
12376 }
12377
12378 size_t total_keys = std::accumulate( keys.begin(), keys.end(), 0,
12379 []( size_t i, const cbl_key_t& key ) {
12380 return i + key.fields.size();
12381 } );
12382 typedef const cbl_field_t * const_field_t;
12383 const_field_t *flattened_fields
12384 = static_cast<const_field_t *>
12385 (xmalloc(total_keys * sizeof(cbl_field_t *)));
12386 gcc_assert(flattened_fields);
12387 size_t *flattened_ascending
12388 = static_cast<size_t *>(xmalloc(total_keys * sizeof(size_t)));
12389 gcc_assert(flattened_ascending);
12390
12391 size_t key_index = 0;
12392 for( size_t i=0; i<keys.size(); i++ )
12393 {
12394 for( size_t j=0; j<keys[i].fields.size(); j++ )
12395 {
12396 flattened_fields[key_index] = keys[i].fields[j];
12397 flattened_ascending[key_index] = keys[i].ascending ? 1 : 0;
12398 key_index += 1;
12399 }
12400 }
12401
12402 // Create the array of cbl_field_t pointers for the keys
12403 tree all_keys = gg_array_of_field_pointers(
12404 total_keys,
12405 const_cast<cbl_field_t**>(flattened_fields));
12406
12407 // Create the array of integers that are the flags for ASCENDING:
12408 tree ascending = gg_array_of_size_t(total_keys, flattened_ascending);
12409
12410 tree all_files = gg_array_of_file_pointers(ninputs, inputs);
12411
12412 // We need to open all of the input files and the workfile. It's easiest to
12413 // do that here, rather than in the libgcobol, because of the possibility that
12414 // the filename is in a variable or an environment variable, rather than a
12415 // literal. This is handled by parser_file_open() in a way that would be
12416 // inconvenient in __gg__file_open
12417
12418 parser_file_open(workfile, 'w');
12419 IF( member(workfile, "io_status"),
12420 ge_op,
12421 build_int_cst_type(INT, FhNotOkay) )
12422 {
12423 rt_error("Couldn't open workfile for stage-one "
12424 "writing in parser_file_merge");
12425 }
12426 ELSE
12427 ENDIF
12428
12429 const cbl_enabled_exceptions_t&
12430 enabled_exceptions( cdf_enabled_exceptions() );
12431
12432 for(size_t i=0; i<ninputs; i++)
12433 {
12434 if( process_this_exception(ec_sort_merge_file_open_e) )
12435 {
12436 IF( member(inputs[i], "file_pointer"), ne_op, null_pointer_node )
12437 {
12438 if( enabled_exceptions.match(ec_sort_merge_file_open_e) )
12439 {
12440 set_exception_code(ec_sort_merge_file_open_e);
12441 }
12442 else
12443 {
12444 rt_error("FILE MERGE file not open");
12445 }
12446 }
12447 ELSE
12448 ENDIF
12449 }
12450
12451 parser_file_open(inputs[i], 'r');
12452 IF( member(inputs[i], "io_status"),
12453 ge_op,
12454 build_int_cst_type(INT, FhNotOkay) )
12455 {
12456 char ach[128];
12457 sprintf(ach,
12458 "Couldn't open %s for stage-one reading in parser_file_merge",
12459 inputs[i]->name);
12460 rt_error(ach);
12461 }
12462 ELSE
12463 ENDIF
12464 }
12465
12466 cbl_field_t *sd_record = symbol_file_record(workfile);
12467 if( alphabet )
12468 {
12469 push_program_state();
12470 parser_alphabet_use(*alphabet);
12471 }
12472 gg_call(VOID,
12473 "__gg__merge_files",
12474 gg_get_address_of(workfile->var_decl_node),
12475 build_int_cst_type(SIZE_T, keys.size()),
12476 all_keys,
12477 ascending,
12478 build_int_cst_type(SIZE_T, ninputs),
12479 all_files,
12480 NULL_TREE);
12481 if( alphabet )
12482 {
12483 pop_program_state();
12484 }
12485
12486 free(flattened_ascending);
12487 free(flattened_fields);
12488 gg_free(ascending);
12489 gg_free(all_keys);
12490
12491 parser_file_close(workfile);
12492 for(size_t i=0; i<ninputs; i++)
12493 {
12494 parser_file_close(inputs[i]);
12495 }
12496
12497 // The merged workfile has been created.
12498 if( noutputs && !out_proc)
12499 {
12500 // We are going to transfer the workfile to the output files.
12501 for(size_t i=0; i<noutputs; i++)
12502 {
12503 if( process_this_exception(ec_sort_merge_file_open_e) )
12504 {
12505 IF( member(outputs[i], "file_pointer"), ne_op, null_pointer_node )
12506 {
12507 if( enabled_exceptions.match(ec_sort_merge_file_open_e) )
12508 {
12509 set_exception_code(ec_sort_merge_file_open_e);
12510 }
12511 else
12512 {
12513 rt_error("FILE MERGE file not open");
12514 }
12515 }
12516 ELSE
12517 ENDIF
12518 }
12519 // We keep reopening the workfile as a convenient way to make sure it is
12520 // positioned at the beginning.
12521 parser_file_open(workfile,'r');
12522 IF( member(workfile, "io_status"),
12523 ge_op,
12524 build_int_cst_type(INT, FhNotOkay) )
12525 {
12526 rt_error("Couldn't open workfile for stage-three "
12527 "reading in parser_file_merge\n");
12528 }
12529 ELSE
12530 ENDIF
12531
12532 parser_file_open(outputs[i], 'w');
12533 IF( member(outputs[i], "io_status"),
12534 ge_op,
12535 build_int_cst_type(INT, FhNotOkay) )
12536 {
12537 rt_error("Couldn't open an output file in parser_file_merge");
12538 }
12539 ELSE
12540 ENDIF
12541 gg_call(VOID,
12542 "__gg__file_sort_ff_output",
12543 gg_get_address_of(outputs[i]->var_decl_node),
12544 gg_get_address_of(workfile-> var_decl_node),
12545 gg_get_address_of(sd_record-> var_decl_node),
12546 NULL_TREE);
12547 parser_file_close(outputs[i]);
12548 parser_file_close(workfile);
12549 }
12550 }
12551 else if (!noutputs && out_proc)
12552 {
12553 // We are going to transfer the workfile to the output procedures.
12554 parser_file_open(workfile,'r');
12555 IF( member(workfile, "io_status"),
12556 ge_op,
12557 build_int_cst_type(INT, FhNotOkay) )
12558 {
12559 rt_error("Couldn't open workfile for"
12560 " stage-three output in parser_file_merge");
12561 }
12562 ELSE
12563 ENDIF
12564 parser_perform(out_proc, NULL);
12565 parser_file_close(workfile);
12566 }
12567 else
12568 {
12569 cbl_internal_error("%s: syntax error: both (or neither) "
12570 "files and output-proc are specified", __func__);
12571 }
12572 }
12573
12574 void
12575 parser_string_overflow( cbl_label_t *name )
12576 {
12577 Analyze();
12578 SHOW_PARSE
12579 {
12580 SHOW_PARSE_HEADER
12581 SHOW_PARSE_END
12582 }
12583 /*
12584 * parser_string_overflow is called 0-2 times before the associated
12585 * parser_string.
12586 */
12587
12588 name->structs.unstring
12589 = static_cast<cbl_unstring_t *>(xmalloc(sizeof(struct cbl_unstring_t)));
12590 gcc_assert(name->structs.unstring);
12591
12592 // Set up the address pairs for this clause
12593 gg_create_goto_pair(&name->structs.unstring->over.go_to,
12594 &name->structs.unstring->over.label);
12595 gg_create_goto_pair(&name->structs.unstring->into.go_to,
12596 &name->structs.unstring->into.label);
12597 gg_create_goto_pair(&name->structs.unstring->bottom.go_to,
12598 &name->structs.unstring->bottom.label);
12599
12600 // Jump over the [NOT] ON OVERFLOW code that is about to be laid down
12601 gg_append_statement( name->structs.unstring->over.go_to );
12602
12603 // Create the label that allows the following code to be executed at
12604 // the appropriate time.
12605 gg_append_statement( name->structs.unstring->into.label );
12606 }
12607
12608 void
12609 parser_string_overflow_end( cbl_label_t *name )
12610 {
12611 Analyze();
12612 SHOW_PARSE
12613 {
12614 SHOW_PARSE_HEADER
12615 SHOW_PARSE_END
12616 }
12617 gg_append_statement( name->structs.unstring->bottom.go_to );
12618 }
12619
12620 void
12621 parser_unstring(cbl_refer_t src,
12622 size_t ndelimited,
12623 cbl_refer_t *delimiteds,
12624 size_t noutputs,
12625 cbl_refer_t *outputs,
12626 cbl_refer_t *delimiters,
12627 cbl_refer_t *counts,
12628 cbl_refer_t pointer,
12629 cbl_refer_t tally,
12630 cbl_label_t *overflow,
12631 cbl_label_t *not_overflow )
12632 {
12633 Analyze();
12634 SHOW_PARSE
12635 {
12636 SHOW_PARSE_HEADER
12637 SHOW_PARSE_END
12638 }
12639 if( overflow )
12640 {
12641 gg_append_statement(overflow->structs.unstring->over.label);
12642 }
12643 if( not_overflow )
12644 {
12645 gg_append_statement(not_overflow->structs.unstring->over.label);
12646 }
12647
12648 std::vector<cbl_refer_t> delims(ndelimited);
12649 char *alls = static_cast<char *>(xmalloc(ndelimited+1));
12650 gcc_assert(alls);
12651 for(size_t i=0; i<ndelimited; i++)
12652 {
12653 delims[i] = delimiteds[i];
12654 alls[i] = delimiteds[i].all ? '1' : '0' ;
12655 }
12656 alls[ndelimited] = '\0';
12657
12658 tree t_alls = build_string_literal(ndelimited+1, alls);
12659
12660 build_array_of_treeplets(1, ndelimited, delims.data());
12661 build_array_of_treeplets(2, noutputs, outputs);
12662 build_array_of_treeplets(3, noutputs, delimiters);
12663 build_array_of_treeplets(4, noutputs, counts);
12664
12665 tree t_overflow = gg_define_int();
12666 gg_assign(t_overflow,
12667 gg_call_expr( INT,
12668 "__gg__unstring",
12669 gg_get_address_of(src.field->var_decl_node),
12670 refer_offset(src),
12671 refer_size_source(src),
12672 build_int_cst_type(SIZE_T, ndelimited),
12673 t_alls,
12674 build_int_cst_type(SIZE_T, noutputs),
12675 pointer.field ? gg_get_address_of(pointer.field->var_decl_node) : null_pointer_node,
12676 refer_offset(pointer),
12677 refer_size_dest(pointer),
12678 tally.field ? gg_get_address_of(tally.field->var_decl_node) : null_pointer_node,
12679 refer_offset(tally),
12680 refer_size_dest(tally),
12681 NULL_TREE)
12682 );
12683 free(alls);
12684
12685 if( overflow )
12686 {
12687 // We have an ON OVERFLOW clause:
12688 IF( t_overflow, ne_op, integer_zero_node )
12689 // And we have an overflow condition
12690 gg_append_statement( overflow->structs.unstring->into.go_to );
12691 ELSE
12692 ENDIF
12693 }
12694
12695 if( not_overflow )
12696 {
12697 // We have a NOT ON OVERFLOW clause:
12698 IF( t_overflow, eq_op, integer_zero_node )
12699 // And there isn't an overflow condition:
12700 gg_append_statement( not_overflow->structs.unstring->into.go_to );
12701 ELSE
12702 ENDIF
12703 }
12704
12705 if( overflow )
12706 {
12707 gg_append_statement( overflow->structs.unstring->bottom.label );
12708 free( overflow->structs.unstring );
12709 }
12710
12711 if( not_overflow )
12712 {
12713 gg_append_statement( not_overflow->structs.unstring->bottom.label );
12714 free( not_overflow->structs.unstring );
12715 }
12716 }
12717
12718 void
12719 parser_string(const cbl_refer_t& tgt,
12720 const cbl_refer_t& pointer,
12721 size_t nsource,
12722 cbl_string_src_t *sources,
12723 cbl_label_t *overflow,
12724 cbl_label_t *not_overflow )
12725 {
12726 SHOW_PARSE
12727 {
12728 SHOW_PARSE_HEADER
12729 SHOW_PARSE_END
12730 }
12731 if( overflow )
12732 {
12733 gg_append_statement(overflow->structs.unstring->over.label);
12734 }
12735 if( not_overflow )
12736 {
12737 gg_append_statement(not_overflow->structs.unstring->over.label);
12738 }
12739
12740 // We need an array of nsource+1 integers:
12741 size_t *integers = static_cast<size_t *>(xmalloc((nsource+1)*sizeof(size_t)));
12742 gcc_assert(integers);
12743
12744 // Count up how many treeplets we are going to need:
12745 size_t cblc_count = 2; // tgt and pointer
12746 for(size_t i=0; i<nsource; i++)
12747 {
12748 cblc_count += 1 + sources[i].ninput; // 1 for identifier_2 + ninput identifier_1 values;
12749 }
12750
12751 std::vector<cbl_refer_t> refers(cblc_count);
12752
12753 size_t index_int = 0;
12754 size_t index_cblc = 0;
12755
12756 integers[index_int++] = nsource;
12757
12758 refers[index_cblc++] = tgt;
12759 refers[index_cblc++] = pointer;
12760
12761 for(size_t i=0; i<nsource; i++)
12762 {
12763 integers[index_int++] = sources[i].ninput;
12764 refers[index_cblc++] = sources[i].delimited_by;
12765 for(size_t j=0; j<sources[i].ninput; j++)
12766 {
12767 refers[index_cblc++] = sources[i].inputs[j];
12768 }
12769 }
12770
12771 gcc_assert(index_int == nsource+1);
12772 gcc_assert(index_cblc == cblc_count);
12773
12774 tree pintegers = build_array_of_size_t( index_int, integers);
12775
12776 build_array_of_treeplets(1, index_cblc, refers.data());
12777
12778 tree t_overflow = gg_define_int();
12779 gg_assign(t_overflow, gg_call_expr( INT,
12780 "__gg__string",
12781 pintegers,
12782 NULL_TREE));
12783 gg_free(pintegers);
12784
12785 free(integers);
12786
12787 if( overflow )
12788 {
12789 // We have an ON OVERFLOW clause:
12790 IF( t_overflow, ne_op, integer_zero_node )
12791 // And we have an overflow condition
12792 gg_append_statement( overflow->structs.unstring->into.go_to );
12793 ELSE
12794 ENDIF
12795 }
12796
12797 if( not_overflow )
12798 {
12799 // We have a NOT ON OVERFLOW clause:
12800 IF( t_overflow, eq_op, integer_zero_node )
12801 // And there isn't an overflow condition:
12802 gg_append_statement( not_overflow->structs.unstring->into.go_to );
12803 ELSE
12804 ENDIF
12805 }
12806
12807 if( overflow )
12808 {
12809 gg_append_statement( overflow->structs.unstring->bottom.label );
12810 free( overflow->structs.unstring );
12811 }
12812
12813 if( not_overflow )
12814 {
12815 gg_append_statement( not_overflow->structs.unstring->bottom.label );
12816 free( not_overflow->structs.unstring );
12817 }
12818 }
12819
12820 void
12821 parser_call_exception( cbl_label_t *name )
12822 {
12823 Analyze();
12824 SHOW_PARSE
12825 {
12826 SHOW_PARSE_HEADER
12827 SHOW_PARSE_TEXT(" ")
12828 SHOW_PARSE_TEXT(name->name)
12829 SHOW_PARSE_END
12830 }
12831
12832 name->structs.call_exception
12833 = static_cast<cbl_call_exception_t *>
12834 (xmalloc(sizeof(struct cbl_call_exception_t)));
12835 gcc_assert(name->structs.call_exception);
12836 // Set up the address pairs for this clause
12837 gg_create_goto_pair(&name->structs.call_exception->over.go_to,
12838 &name->structs.call_exception->over.label);
12839 gg_create_goto_pair(&name->structs.call_exception->into.go_to,
12840 &name->structs.call_exception->into.label);
12841 gg_create_goto_pair(&name->structs.call_exception->bottom.go_to,
12842 &name->structs.call_exception->bottom.label);
12843
12844 // Jump over the [NOT] ON EXCEPTION code that is about to be laid down
12845 // char ach[128];
12846 // sprintf(ach, "# parser_call_exception %s: over.goto", name->name);
12847 // gg_insert_into_assembler(ach);
12848 SHOW_PARSE
12849 {
12850 SHOW_PARSE_INDENT
12851 SHOW_PARSE_TEXT("except over.goto")
12852 SHOW_PARSE_END
12853 }
12854 gg_append_statement( name->structs.call_exception->over.go_to );
12855
12856 // Create the label that allows the following code to be executed at
12857 // the appropriate time.
12858 // sprintf(ach, "# parser_call_exception %s: into.label", name->name);
12859 // gg_insert_into_assembler(ach);
12860 gg_append_statement( name->structs.call_exception->into.label );
12861 }
12862
12863 void
12864 parser_call_exception_end( cbl_label_t *name )
12865 {
12866 Analyze();
12867 SHOW_PARSE
12868 {
12869 SHOW_PARSE_HEADER
12870 SHOW_PARSE_TEXT(name->name)
12871 SHOW_PARSE_END
12872 }
12873 // char ach[128];
12874 // sprintf(ach, "# parser_call_exception_end %s: bottom.goto", name->name);
12875 // gg_insert_into_assembler(ach);
12876 gg_append_statement( name->structs.call_exception->bottom.go_to );
12877 }
12878
12879 static
12880 void
12881 create_and_call(size_t narg,
12882 cbl_ffi_arg_t args[],
12883 tree function_pointer,
12884 const char *funcname,
12885 tree returned_value_type,
12886 cbl_refer_t returned,
12887 cbl_label_t *not_except)
12888 {
12889 // We have a good function handle, so we are going to create a call
12890 tree *arguments = NULL;
12891 int *allocated = NULL;
12892
12893 if(narg)
12894 {
12895 arguments = static_cast<tree *>(xmalloc(2*narg * sizeof(tree)));
12896 gcc_assert(arguments);
12897 allocated = static_cast<int *>(xmalloc(narg * sizeof(int)));
12898 gcc_assert(allocated);
12899 }
12900
12901 // Put the arguments onto the "stack" of calling parameters:
12902 for( size_t i=0; i<narg; i++ )
12903 {
12904 cbl_ffi_crv_t crv = args[i].crv;
12905
12906 if( args[i].refer.field && args[i].refer.field->type == FldLiteralN )
12907 {
12908 crv = by_value_e;
12909 }
12910
12911 allocated[i] = 0;
12912
12913 tree location = gg_define_variable(UCHAR_P, "..location.1", vs_stack);
12914 tree length = gg_define_variable(SIZE_T, "..length.1", vs_stack);
12915
12916 if( !args[i].refer.field )
12917 {
12918 // The PARAMETER is OMITTED
12919 arguments[i] = null_pointer_node;
12920 gg_assign(gg_array_value(var_decl_call_parameter_lengths, i),
12921 size_t_zero_node);
12922 continue;
12923 }
12924
12925 if( refer_is_clean(args[i].refer) )
12926 {
12927 if( args[i].refer.field->type == FldLiteralA )
12928 {
12929 crv = by_content_e;
12930 gg_assign(location,
12931 gg_cast(UCHAR_P, build_string_literal(args[i].refer.field->data.capacity,
12932 args[i].refer.field->data.initial)));
12933 gg_assign(length,
12934 build_int_cst_type( SIZE_T,
12935 args[i].refer.field->data.capacity));
12936 }
12937 else
12938 {
12939 gg_assign(location,
12940 member(args[i].refer.field->var_decl_node, "data"));
12941 gg_assign(length,
12942 member(args[i].refer.field->var_decl_node, "capacity"));
12943 }
12944 }
12945 else
12946 {
12947 gg_assign(location,
12948 qualified_data_location(args[i].refer)),
12949 gg_assign(length,
12950 refer_size_source(args[i].refer));
12951 }
12952
12953 switch( crv )
12954 {
12955 case by_default_e:
12956 gcc_unreachable();
12957 break;
12958
12959 case by_reference_e:
12960 {
12961 arguments[i] = location;
12962
12963 // Pass the pointer to the data location, so that the called program
12964 // can both access and change the data.
12965 break;
12966 }
12967
12968 case by_content_e:
12969 {
12970 if( (args[i].refer.field->attr & intermediate_e)
12971 && is_valuable(args[i].refer.field->type) )
12972 {
12973 cbl_unimplemented("CALL USING BY CONTENT <temporary> would require "
12974 "REPOSITORY PROTOTYPES.");
12975 }
12976
12977 // BY CONTENT means that the called program gets a copy of the data.
12978
12979 // We'll free this copy after the called program returns.
12980
12981 switch(args[i].attr)
12982 {
12983 case address_of_e:
12984 {
12985 // Allocate the memory, and make the copy:
12986 arguments[i] = gg_define_char_star();
12987 allocated[i] = 1;
12988 gg_assign(arguments[i], gg_malloc(length) ) ;
12989 gg_memcpy(arguments[i],
12990 location,
12991 length);
12992 break;
12993 }
12994
12995 case length_of_e:
12996 {
12997 // The BY CONTENT LENGTH OF gets passed as an 64-bit big-endian
12998 // value
12999 arguments[i] = gg_define_size_t();
13000 allocated[i] = 1;
13001 gg_assign(arguments[i], gg_malloc(length) ) ;
13002 gg_call(VOID,
13003 "__gg__copy_as_big_endian",
13004 gg_get_address_of(arguments[i]),
13005 length,
13006 NULL_TREE);
13007 break;
13008 }
13009
13010 case none_of_e:
13011 {
13012 // Allocate the memory, and make the copy:
13013 arguments[i] = gg_define_char_star();
13014 allocated[i] = 1;
13015 gg_assign(arguments[i], gg_cast(CHAR_P, gg_malloc(length))) ;
13016 gg_memcpy(arguments[i], location, length);
13017 break;
13018 }
13019 }
13020 break;
13021 }
13022
13023 case by_value_e:
13024 {
13025 // For BY VALUE, we take whatever we've been given and do our best to
13026 // make a 64-bit value out of it, although we move to 128 bits when
13027 // necessary.
13028 switch(args[i].attr)
13029 {
13030 case address_of_e:
13031 {
13032 arguments[i] = gg_define_size_t();
13033 gg_assign(arguments[i], gg_cast(SIZE_T, location ));
13034 break;
13035 }
13036
13037 case length_of_e:
13038 {
13039 arguments[i] = gg_define_size_t();
13040 gg_assign(arguments[i], gg_cast(SIZE_T, length));
13041 break;
13042 }
13043
13044 case none_of_e:
13045 {
13046 assert(args[i].refer.field);
13047 bool as_int128 = false;
13048 if( !(args[i].refer.field->attr & intermediate_e) )
13049 {
13050 // All temporaries are SIZE_T
13051 if( args[i].refer.field->type == FldFloat
13052 && args[i].refer.field->data.capacity == 16 )
13053 {
13054 as_int128 = true;
13055 }
13056 else if( args[i].refer.field->type == FldNumericBin5
13057 && args[i].refer.field->data.digits == 0
13058 && args[i].refer.field->data.capacity == 16 )
13059 {
13060 as_int128 = true;
13061 }
13062 else if( args[i].refer.field->data.digits > 18 )
13063 {
13064 as_int128 = true;
13065 }
13066 }
13067
13068 if( as_int128 )
13069 {
13070 arguments[i] = gg_define_variable(INT128);
13071 gg_assign(arguments[i],
13072 gg_cast(INT128,
13073 gg_call_expr(
13074 INT128,
13075 "__gg__fetch_call_by_value_value",
13076 gg_get_address_of(args[i].refer.field->var_decl_node),
13077 refer_offset(args[i].refer),
13078 refer_size_source(args[i].refer),
13079 NULL_TREE)));
13080 }
13081 else
13082 {
13083 arguments[i] = gg_define_size_t();
13084 gg_assign(arguments[i],
13085 gg_cast(SIZE_T,
13086 gg_call_expr(
13087 INT128,
13088 "__gg__fetch_call_by_value_value",
13089 gg_get_address_of(args[i].refer.field->var_decl_node),
13090 refer_offset(args[i].refer),
13091 refer_size_source(args[i].refer),
13092 NULL_TREE)));
13093 }
13094 break;
13095 }
13096 }
13097 }
13098 }
13099 // The elements in this array tell the called routine the length of each
13100 // variable. This value is used both to handle ANY LENGTH formal
13101 // parameters, and to provide information to the called program when being
13102 // passed expressions BY VALUE and BY CONTENT
13103 gg_assign(gg_array_value(var_decl_call_parameter_lengths, i),length);
13104 }
13105
13106 // Let the called program know how many parameters we are passing
13107 gg_assign(var_decl_call_parameter_count,
13108 build_int_cst_type(INT, narg));
13109
13110 tree call_expr = NULL_TREE;
13111 if( function_pointer )
13112 {
13113 gg_assign(var_decl_call_parameter_signature,
13114 gg_cast(CHAR_P, function_pointer));
13115
13116 call_expr = gg_call_expr_list(returned_value_type,
13117 function_pointer,
13118 narg,
13119 arguments );
13120 }
13121 else
13122 {
13123 tree fndecl_type = build_varargs_function_type_array( returned_value_type,
13124 0, // No parameters yet
13125 NULL); // And, hence, no types
13126
13127 // Fetch the FUNCTION_DECL for that FUNCTION_TYPE
13128 tree function_decl = gg_build_fn_decl(funcname, fndecl_type);
13129 set_call_convention(function_decl, current_call_convention());
13130
13131 // Take the address of the function decl:
13132 tree address_of_function = gg_get_address_of(function_decl);
13133
13134 // Stash that address as the called program's signature:
13135 tree address_as_char_p = gg_cast(CHAR_P, address_of_function);
13136 tree assigment = gg_assign( var_decl_call_parameter_signature,
13137 address_as_char_p);
13138 // The source of the assigment is the second element of a MODIFY_EXPR
13139 parser_call_target( funcname, assigment );
13140
13141 // Create the call_expr from that address
13142 call_expr = build_call_array_loc( gg_token_location(),
13143 returned_value_type,
13144 address_of_function,
13145 narg,
13146 arguments);
13147 // Among other possibilities, this might be a forward reference to a
13148 // contained function. The name here is "prog2", and ultimately will need
13149 // to be replaced with a call to "prog2.62". So, this call expr goes into
13150 // a list of call expressions whose function_decl targets will be replaced.
13151 parser_call_target( funcname, call_expr );
13152 }
13153
13154 tree returned_value;
13155
13156 if( returned.field )
13157 {
13158 // Because the CALL had a RETURNING clause, RETURN-CODE doesn't return a
13159 // value. So, we make sure it is zero
13160 //// gg_assign(var_decl_return_code, build_int_cst_type(SHORT, 0));
13161
13162 // We expect the return value to be a 64-bit or 128-bit integer. How
13163 // we treat that returned value depends on the target.
13164
13165 // Pick up that value:
13166 returned_value = gg_define_variable(returned_value_type);
13167 push_program_state();
13168 gg_assign(returned_value, gg_cast(returned_value_type, call_expr));
13169 pop_program_state();
13170
13171 if( returned_value_type == CHAR_P )
13172 {
13173 tree returned_location = gg_define_uchar_star();
13174 tree returned_length = gg_define_size_t();
13175 // we were given a returned::field, so find its location and length:
13176 gg_assign(returned_location,
13177 gg_add( member(returned.field->var_decl_node, "data"),
13178 refer_offset(returned)));
13179 gg_assign(returned_length,
13180 gg_cast(TREE_TYPE(returned_length), refer_size_dest(returned)));
13181
13182 // The returned value is a string of nbytes, which by specification
13183 // has to be at least as long as the returned_length of the target:
13184 IF( returned_value,
13185 eq_op,
13186 gg_cast(returned_value_type, null_pointer_node ) )
13187 {
13188 // Somebody was discourteous enough to return a NULL pointer
13189 // We'll jam in spaces:
13190 gg_memset( returned_location,
13191 char_nodes[(unsigned char)internal_space],
13192 returned_length );
13193 }
13194 ELSE
13195 {
13196 // There is a valid pointer. Do the assignment.
13197 move_tree(returned.field,
13198 refer_offset(returned),
13199 returned_value,
13200 integer_one_node);
13201 }
13202 ENDIF
13203 TRACE1
13204 {
13205 TRACE1_HEADER
13206 TRACE1_REFER("returned value: ", returned, "")
13207 TRACE1_END
13208 }
13209 }
13210 else if( returned_value_type == SSIZE_T
13211 || returned_value_type == SIZE_T
13212 || returned_value_type == INT128
13213 || returned_value_type == UINT128)
13214 {
13215 // We got back a 64-bit or 128-bit integer. The called and calling
13216 // programs have to agree on size, but other than that, integer numeric
13217 // types are converted one to the other.
13218
13219 gg_call(VOID,
13220 "__gg__int128_to_qualified_field",
13221 gg_get_address_of(returned.field->var_decl_node),
13222 refer_offset(returned),
13223 refer_size_dest(returned),
13224 gg_cast(INT128, returned_value),
13225 gg_cast(INT, member(returned.field->var_decl_node, "rdigits")),
13226 build_int_cst_type(INT, truncation_e),
13227 null_pointer_node,
13228 NULL_TREE );
13229 TRACE1
13230 {
13231 TRACE1_HEADER
13232 TRACE1_REFER("returned value: ", returned, "")
13233 TRACE1_END
13234 }
13235 }
13236 else if( returned_value_type == FLOAT
13237 || returned_value_type == DOUBLE
13238 || returned_value_type == FLOAT128)
13239 {
13240 tree returned_location = gg_define_uchar_star();
13241 tree returned_length = gg_define_size_t();
13242 // we were given a returned::field, so find its location and length:
13243 gg_assign(returned_location,
13244 qualified_data_location(returned));
13245 gg_assign(returned_length,
13246 refer_size_source(returned));
13247
13248 // We are doing float-to-float, and we require that those be identical
13249 // one the caller and callee sides.
13250 gg_memcpy( returned_location,
13251 gg_get_address_of(returned_value),
13252 returned_length);
13253
13254 TRACE1
13255 {
13256 TRACE1_HEADER
13257 TRACE1_REFER("returned value: ", returned, "")
13258 TRACE1_END
13259 }
13260 }
13261 else
13262 {
13263 cbl_internal_error(
13264 "%s: What in the name of Nero are we doing here?",
13265 __func__);
13266 }
13267 }
13268 else
13269 {
13270 // Because no explicit returning value is expected, we just call it. We
13271 // expect COBOL routines to set RETURN-CODE when they think it necessary.
13272 gg_append_statement(call_expr);
13273 }
13274
13275 for( size_t i=0; i<narg; i++ )
13276 {
13277 if( allocated[i] )
13278 {
13279 gg_free(arguments[i]);
13280 }
13281 }
13282 free(arguments);
13283 free(allocated);
13284
13285 if( not_except )
13286 {
13287 // We have an ON EXCEPT clause:
13288 gg_append_statement( not_except->structs.call_exception->into.go_to );
13289 }
13290 }
13291
13292 void
13293 parser_call( cbl_refer_t name,
13294 cbl_refer_t returned, // This is set by RETURNING clause
13295 size_t narg,
13296 cbl_ffi_arg_t args[],
13297 cbl_label_t *except,
13298 cbl_label_t *not_except,
13299 bool /*is_function*/)
13300 {
13301 Analyze();
13302 SHOW_PARSE
13303 {
13304 SHOW_PARSE_HEADER
13305 SHOW_PARSE_FIELD( " calling ", name.field)
13306 if( except )
13307 {
13308 SHOW_PARSE_TEXT(" - except is ")
13309 SHOW_PARSE_TEXT(except->name)
13310 }
13311 if( not_except )
13312 {
13313 SHOW_PARSE_TEXT(" - not_except is ")
13314 SHOW_PARSE_TEXT(not_except->name)
13315 }
13316 SHOW_PARSE_TEXT(" (")
13317 for(size_t i=0; i<narg; i++)
13318 {
13319 const cbl_field_t *p = args[i].refer.field;
13320 SHOW_PARSE_FIELD( " ", p)
13321 }
13322 SHOW_PARSE_TEXT(" )")
13323 SHOW_PARSE_END
13324 }
13325
13326 TRACE1
13327 {
13328 TRACE1_HEADER
13329 TRACE1_REFER("calling ", name, "");
13330 for(size_t i=0; i<narg; i++)
13331 {
13332 TRACE1_INDENT
13333 gg_fprintf(trace_handle, 1, "parameter %d: ", build_int_cst_type(INT, i+1));
13334 switch( args[i].crv )
13335 {
13336 case by_default_e: gcc_unreachable();
13337 case by_reference_e:
13338 TRACE1_TEXT(" BY REFERENCE ")
13339 break;
13340 case by_content_e:
13341 TRACE1_TEXT(" BY CONTENT ")
13342 break;
13343 case by_value_e:
13344 TRACE1_TEXT(" BY VALUE ")
13345 break;
13346 }
13347 TRACE1_REFER("", args[i].refer, "")
13348 }
13349 TRACE1_END
13350 }
13351
13352 // If we have an ON EXCEPTION clause, a GOTO was established in
13353 // parser_call_exception().
13354 // Here is where we place the label for that GOTO
13355
13356 if( except )
13357 {
13358 SHOW_PARSE
13359 {
13360 SHOW_PARSE_INDENT
13361 SHOW_PARSE_TEXT("except over.label:")
13362 }
13363 gg_append_statement(except->structs.call_exception->over.label);
13364 }
13365
13366 // Likewise, for a NOT ON EXCEPTION
13367 if( not_except )
13368 {
13369 SHOW_PARSE
13370 {
13371 SHOW_PARSE_INDENT
13372 SHOW_PARSE_TEXT("not_except over.label:")
13373 }
13374 gg_append_statement(not_except->structs.call_exception->over.label);
13375 }
13376
13377 // We are getting close to establishing the function_type. To do that,
13378 // we want to establish the function's return type.
13379
13380 size_t nbytes;
13381 tree returned_value_type = tree_type_from_field_type(returned.field, nbytes);
13382
13383 if( use_static_call() && is_literal(name.field) )
13384 {
13385 // name is a literal
13386 create_and_call(narg,
13387 args,
13388 NULL_TREE,
13389 name.field->data.initial,
13390 returned_value_type,
13391 returned,
13392 not_except);
13393 }
13394 else if( name.field && name.field->type == FldPointer )
13395 {
13396 tree function_pointer = function_pointer_from_name( name,
13397 returned_value_type);
13398 // This is call-by-pointer; we know function_pointer is good:
13399 create_and_call(narg,
13400 args,
13401 function_pointer,
13402 nullptr,
13403 returned_value_type,
13404 returned,
13405 not_except);
13406 }
13407 else
13408 {
13409 tree function_pointer = function_pointer_from_name( name,
13410 returned_value_type);
13411 // We might not have a good handle, so we have to check:
13412 IF( function_pointer,
13413 ne_op,
13414 gg_cast(TREE_TYPE(function_pointer), null_pointer_node) )
13415 {
13416 create_and_call(narg,
13417 args,
13418 function_pointer,
13419 nullptr,
13420 returned_value_type,
13421 returned,
13422 not_except);
13423 }
13424 ELSE
13425 {
13426 // We have a bad function pointer, which is the except condition:
13427 parser_exception_raise(ec_program_not_found_e);
13428 if( except )
13429 {
13430 // We have an ON EXCEPT clause:
13431 gg_append_statement( except->structs.call_exception->into.go_to );
13432 // Because there is an ON EXCEPTION clause, suppress DECLARATIVE
13433 // processing
13434 gg_assign(var_decl_exception_code, integer_zero_node);
13435 }
13436 else
13437 {
13438 tree mangled_name = gg_define_variable(CHAR_P);
13439
13440 gg_call(VOID,
13441 "__gg__just_mangle_name",
13442 (name.field->var_decl_node
13443 ? gg_get_address_of(name.field->var_decl_node)
13444 : null_pointer_node),
13445 gg_get_address_of( mangled_name),
13446 NULL_TREE);
13447
13448 gg_printf("WARNING: %s:%d \"CALL %s\" not found"
13449 " with no \"CALL ON EXCEPTION\" phrase\n",
13450 gg_string_literal(current_filename.back().c_str()),
13451 build_int_cst_type(INT, CURRENT_LINE_NUMBER),
13452 mangled_name,
13453 NULL_TREE);
13454 }
13455 }
13456 ENDIF
13457 }
13458
13459 // Clean up the label bookkeeping
13460 if( except )
13461 {
13462 gg_append_statement( except->structs.call_exception->bottom.label );
13463 free( except->structs.call_exception );
13464 }
13465 if( not_except )
13466 {
13467 gg_append_statement( not_except->structs.call_exception->bottom.label );
13468 free( not_except->structs.call_exception );
13469 }
13470 }
13471
13472 // Set global variable to use alternative ENTRY point.
13473 void
13474 parser_entry_activate( size_t iprog, const cbl_label_t *declarative )
13475 {
13476 assert(iprog == symbol_elem_of(declarative)->program);
13477 }
13478
13479 // Define ENTRY point with alternative LINKAGE
13480 void
13481 parser_entry( cbl_field_t */*name*/, size_t /*narg*/, cbl_ffi_arg_t */*args*/ )
13482 {
13483 }
13484
13485 void
13486 parser_bitop( struct cbl_field_t *tgt, // tgt has to be a FldConditional
13487 struct cbl_field_t *a, // is modified by SET,CLEAR
13488 enum bitop_t op,
13489 size_t bitmask )
13490 {
13491 Analyze();
13492 // This routine is designed to set, clear, and test BITMASK bits in the
13493 // A operand. For ON and OFF, it sets tgt, a FldConditional, to TRUE or FALSE
13494
13495 // This is clumsy: The ops[] array has to match bitop_t
13496 static const char *ops[] = { "SET", "CLEAR", "ON", "OFF",
13497 "AND", "OR", "XOR" };
13498 gcc_assert( op < COUNT_OF(ops) );
13499 SHOW_PARSE
13500 {
13501 SHOW_PARSE_HEADER
13502 SHOW_PARSE_FIELD( " switch: ", a)
13503 fprintf(stderr, " mask: " HOST_SIZE_T_PRINT_HEX_PURE, (fmt_size_t)bitmask);
13504 fprintf(stderr, " op: %s", ops[op]);
13505 SHOW_PARSE_FIELD( " target ", tgt)
13506 SHOW_PARSE_END
13507 }
13508
13509 if(tgt && tgt->type != FldConditional)
13510 {
13511 fprintf(stderr,
13512 "%s: The target %s has to be a FldConditional, not %s\n",
13513 __func__,
13514 tgt->name,
13515 cbl_field_type_str(tgt->type));
13516 gcc_unreachable();
13517 }
13518
13519 switch(op)
13520 {
13521 case bit_set_op:
13522 case bit_clear_op:
13523 // For set_on and set_off operations, the tgt is superfluous, so I
13524 // did this code just in case the parser doesn't give us anything
13525 // to set
13526 gg_call(BOOL,
13527 "__gg__bitop",
13528 gg_get_address_of(a->var_decl_node),
13529 build_int_cst_type(INT, op),
13530 build_int_cst_type(SIZE_T, bitmask),
13531 NULL_TREE );
13532 break;
13533
13534 case bit_on_op:
13535 case bit_off_op:
13536 gg_assign( tgt->var_decl_node,
13537 gg_call_expr( BOOL,
13538 "__gg__bitop",
13539 gg_get_address_of(a->var_decl_node),
13540 build_int_cst_type(INT, op),
13541 build_int_cst_type(SIZE_T, bitmask),
13542 NULL_TREE));
13543 break;
13544
13545 case bit_and_op:
13546 case bit_or_op:
13547 case bit_xor_op:
13548 fprintf(stderr,
13549 "%s: The %s operation is not valid\n",
13550 __func__,
13551 ops[op]);
13552 gcc_unreachable();
13553 break;
13554 }
13555
13556 TRACE1
13557 {
13558 TRACE1_HEADER
13559 //TRACE1_FIELD_INFO( " target ", tgt)
13560 TRACE1_FIELD_INFO( " a ", a)
13561 TRACE1_END
13562 }
13563 }
13564
13565 void
13566 parser_bitwise_op(struct cbl_field_t *tgt,
13567 struct cbl_field_t *a,
13568 enum bitop_t op,
13569 size_t bitmask )
13570 {
13571 Analyze();
13572 // This routine is a specialized TGT = A op (size_t) bitmask, where OP is
13573 // AND, OR, or XOR. A should be an integer type. tgt should be a valid target
13574 // for a move where an integer is the sender.
13575
13576 // SET and CLEAR are straightforward. ON returns true if any bitmask bit is
13577 // one in 'A'. OFF returns true if any bitmask bit in 'A' is zero.
13578
13579 // This is clumsy: The ops[] array has to match bitop_t
13580 static const char *ops[] = { "SET", "CLEAR", "ON", "OFF",
13581 "AND", "OR", "XOR" };
13582 gcc_assert( op < COUNT_OF(ops) );
13583 SHOW_PARSE
13584 {
13585 SHOW_PARSE_HEADER
13586 SHOW_PARSE_FIELD( " switch: ", a)
13587 fprintf(stderr, " mask: " HOST_SIZE_T_PRINT_HEX_PURE, (fmt_size_t)bitmask);
13588 fprintf(stderr, " op: %s", ops[op]);
13589 SHOW_PARSE_FIELD( " target ", tgt)
13590 SHOW_PARSE_END
13591 }
13592
13593 if( tgt && !is_valuable(tgt->type) && tgt->type != FldLiteralN)
13594 {
13595 fprintf(stderr,
13596 "%s: The target %s has to be is_valuable, not %s\n",
13597 __func__,
13598 tgt->name,
13599 cbl_field_type_str(tgt->type));
13600 gcc_unreachable();
13601 }
13602
13603 switch(op)
13604 {
13605 case bit_set_op:
13606 case bit_clear_op:
13607 case bit_on_op:
13608 case bit_off_op:
13609 fprintf(stderr,
13610 "%s: The %s operation is not valid\n",
13611 __func__,
13612 ops[op]);
13613 gcc_unreachable();
13614 break;
13615
13616 case bit_and_op:
13617 case bit_or_op:
13618 case bit_xor_op:
13619 gg_call(VOID,
13620 "__gg__bitwise_op",
13621 gg_get_address_of(tgt->var_decl_node),
13622 gg_get_address_of(a->var_decl_node),
13623 build_int_cst_type(INT, op),
13624 build_int_cst_type(SIZE_T, bitmask),
13625 NULL_TREE );
13626 break;
13627 }
13628
13629 TRACE1
13630 {
13631 TRACE1_HEADER
13632 //TRACE1_FIELD_INFO( " target ", tgt)
13633 TRACE1_FIELD_INFO( " a ", a)
13634 TRACE1_END
13635 }
13636 }
13637
13638 void
13639 parser_set_pointers( size_t ntgt, cbl_refer_t *tgts, cbl_refer_t source )
13640 {
13641 Analyze();
13642 SHOW_PARSE
13643 {
13644 SHOW_PARSE_HEADER
13645 SHOW_PARSE_FIELD(" source ", source.field);
13646 char ach[128];
13647 sprintf(ach,
13648 " source.addr_of %s",
13649 source.addr_of ? "TRUE" : "FALSE" );
13650 SHOW_PARSE_TEXT(ach);
13651 for( size_t i=0; i<ntgt; i++ )
13652 {
13653 SHOW_PARSE_INDENT
13654 SHOW_PARSE_FIELD("target ", tgts[i].field)
13655 }
13656 SHOW_PARSE_END
13657 }
13658 for( size_t i=0; i<ntgt; i++ )
13659 {
13660 if( !source.addr_of
13661 && (source.field->type == FldAlphanumeric
13662 || source.field->type == FldLiteralA))
13663 {
13664 // This is something like SET varp TO ENTRY "ref".
13665 tree function_pointer = function_pointer_from_name(source,
13666 COBOL_FUNCTION_RETURN_TYPE);
13667 gg_memcpy(qualified_data_location(tgts[i]),
13668 gg_get_address_of(function_pointer),
13669 sizeof_pointer);
13670 }
13671 else
13672 {
13673 if( !tgts[i].addr_of )
13674 {
13675 // When not ADDRESS OF TARGET, the variable must be a POINTER
13676 gcc_assert( tgts[i].field->type == FldPointer );
13677 }
13678 else
13679 {
13680 // When ADDRESS OF TARGET, the target must be linkage or based
13681 gcc_assert( tgts[i].field->attr & (linkage_e | based_e) );
13682 }
13683
13684 gg_call( VOID,
13685 "__gg__set_pointer",
13686 gg_get_address_of(tgts[i].field->var_decl_node),
13687 refer_offset(tgts[i]),
13688 build_int_cst_type(INT, tgts[i].addr_of ? REFER_T_ADDRESS_OF : 0),
13689 source.field ? gg_get_address_of(source.field->var_decl_node) : null_pointer_node,
13690 refer_offset(source),
13691 build_int_cst_type(INT, source.addr_of ? REFER_T_ADDRESS_OF : 0),
13692 NULL_TREE
13693 );
13694
13695 if( tgts[i].addr_of )
13696 {
13697 // When SET ADDRESS OF TARGET TO ..., the library call sets
13698 // tgts[i].field->data. We need to propogate the data+offset
13699 // through the level01 variable's children:
13700 propogate_linkage_offsets(tgts[i].field,
13701 member(tgts[i].field->var_decl_node, "data"));
13702 }
13703 }
13704 }
13705 }
13706 typedef struct hier_node
13707 {
13708 size_t our_index; // In the symbol table
13709 bool common;
13710 struct hier_node *parent_node;
13711 char *name;
13712 std::vector<struct hier_node *>child_nodes;
13713
13714 hier_node() :
13715 our_index(0),
13716 common(false),
13717 parent_node(nullptr),
13718 name(nullptr)
13719 {}
13720 } hier_node;
13721
13722 static hier_node *
13723 find_hier_node( const std::unordered_map<size_t, hier_node *> &node_map,
13724 size_t program_index)
13725 {
13726 std::unordered_map<size_t, hier_node *>::const_iterator it =
13727 node_map.find(program_index);
13728 if( it == node_map.end() )
13729 {
13730 return NULL;
13731 }
13732 return it->second;
13733 }
13734
13735 static bool
13736 sort_by_hier_name(const hier_node *a, const hier_node *b)
13737 {
13738 return strcmp(a->name, b->name) < 0;
13739 }
13740
13741 static void
13742 find_uncles(const hier_node *node, std::vector<const hier_node *> &uncles)
13743 {
13744 const hier_node *parent = node->parent_node;
13745 if( parent )
13746 {
13747 for(size_t i=0; i<parent->child_nodes.size(); i++)
13748 {
13749 if( parent->child_nodes[i] != node )
13750 {
13751 if( parent->child_nodes[i]->common )
13752 {
13753 uncles.push_back(parent->child_nodes[i]);
13754 }
13755 }
13756 }
13757 find_uncles(parent, uncles);
13758 }
13759 }
13760
13761 void
13762 parser_program_hierarchy( const cbl_prog_hier_t& hier )
13763 {
13764 Analyze();
13765 /* This routine gets called near the end of every program-id. It keeps
13766 growing because the parser doesn't know when it is working on the last
13767 program of a list of nested programs. So, we just do what we need to do,
13768 and we keep track of what we've already built so that we don't build it
13769 more than once.
13770 */
13771 SHOW_PARSE
13772 {
13773 SHOW_PARSE_HEADER
13774 if( gg_trans_unit.function_stack.size() != 1 )
13775 {
13776 SHOW_PARSE_TEXT("Ending a nested function")
13777 }
13778 else
13779 {
13780 for( size_t i=0; i<hier.labels.size(); i++ )
13781 {
13782 if( i )
13783 {
13784 SHOW_PARSE_INDENT
13785 }
13786 else
13787 {
13788 SHOW_PARSE_TEXT(" ");
13789 }
13790 char ach[128];
13791 sprintf(ach,
13792 HOST_SIZE_T_PRINT_DEC " %s%s parent:" HOST_SIZE_T_PRINT_DEC,
13793 (fmt_size_t)hier.labels[i].ordinal,
13794 hier.labels[i].label.name,
13795 hier.labels[i].label.common ? " COMMON" : "",
13796 (fmt_size_t)hier.labels[i].label.parent);
13797 SHOW_PARSE_TEXT(ach);
13798 }
13799 }
13800 SHOW_PARSE_END
13801 }
13802
13803 // This needs to be an island that doesn't execute in-line. This is necessary
13804 // when there isn't a GOBACK or GOTO or STOP RUN at the point where a
13805 // [possibly implicit] PROGRAM END is encountered
13806 tree skipper_goto;
13807 tree skipper_label;
13808 gg_create_goto_pair(&skipper_goto,
13809 &skipper_label);
13810 gg_append_statement(skipper_goto);
13811
13812 // The stack.size() test shouldn't be necessary, because the parser should
13813 // be calling us only at the PROGRAM END point of an outermost function.
13814
13815 gcc_assert(gg_trans_unit.function_stack.size() == 1);
13816
13817 gg_append_statement(label_list_out_label);
13818
13819 std::unordered_map<size_t, std::vector<const hier_node *>> map_of_lists;
13820 std::unordered_map<size_t, hier_node *> node_map;
13821 std::vector<hier_node *> nodes;
13822
13823 // We need to avoid duplicating names, because a direct child's name takes
13824 // precedence over a COMMON name above us in the hierarchy:
13825
13826 std::unordered_map<size_t, std::unordered_set<std::string>>map_of_sets;
13827
13828 // We need to build a tree out of the hierarchical structure:
13829 // Create, essentially, a root node:
13830 hier_node *zero_node = new hier_node;
13831 nodes.push_back(zero_node);
13832 node_map[0] = nodes.back();
13833
13834 // Pass 1: Create a node for every program:
13835 for( size_t i=0; i<hier.labels.size(); i++ )
13836 {
13837 const hier_node *existing_node = find_hier_node(node_map, hier.labels[i].ordinal);
13838 gcc_assert( existing_node == NULL );
13839
13840 hier_node *new_node = new hier_node;
13841 new_node->our_index = hier.labels[i].ordinal;
13842 new_node->common = hier.labels[i].label.common;
13843 new_node->name = cobol_name_mangler(hier.labels[i].label.name);
13844 nodes.push_back(new_node);
13845 node_map[hier.labels[i].ordinal] = nodes.back();
13846 }
13847
13848 // Pass 2: populate each node with their parent and children:
13849 for( size_t i=0; i<hier.labels.size(); i++ )
13850 {
13851 hier_node *child_node = find_hier_node(node_map, hier.labels[i].ordinal);
13852 gcc_assert(child_node);
13853
13854 hier_node *parent_node = find_hier_node(node_map,
13855 hier.labels[i].label.parent);
13856 gcc_assert(parent_node);
13857
13858 child_node->parent_node = parent_node;
13859 parent_node->child_nodes.push_back(child_node);
13860 }
13861
13862 // We now build the lists of routines that can be called from every routine
13863
13864 // We are going to create one vector of hier_nodes for each routine:
13865
13866 for(size_t i=0; i<nodes.size(); i++)
13867 {
13868 // First, direct children always take precedence
13869 size_t caller = nodes[i]->our_index;
13870 const hier_node *caller_node = nodes[i];
13871 for(size_t j=0; j<caller_node->child_nodes.size(); j++)
13872 {
13873 map_of_lists[caller].push_back(caller_node->child_nodes[j]);
13874 map_of_sets[caller].insert(caller_node->child_nodes[j]->name);
13875 }
13876
13877 // Sibling routines marked COMMON, and siblings of ancestors marked COMMON
13878 // are also accessible by us. Go find them.
13879 std::vector<const hier_node *>uncles;
13880 find_uncles(nodes[i], uncles);
13881 for( size_t j=0; j<uncles.size(); j++ )
13882 {
13883 const hier_node *uncle = uncles[j];
13884 if( map_of_sets[caller].find(uncle->name) == map_of_sets[caller].end() )
13885 {
13886 // We have a COMMON uncle or sibling we haven't seen before.
13887 map_of_lists[caller].push_back(uncle);
13888 }
13889 }
13890 }
13891
13892 // Having created lists of callables for each caller, we want to sort each
13893 // of those lists to make it easier to bsearch things in them later:
13894 for( std::unordered_map<size_t, std::vector<const hier_node *>>::iterator mol = map_of_lists.begin();
13895 mol != map_of_lists.end();
13896 mol++ )
13897 {
13898 std::sort(mol->second.begin(), mol->second.end(), sort_by_hier_name);
13899 }
13900
13901 // Having built the lists of lists, start pulling them apart
13902
13903 tree function_type =
13904 build_varargs_function_type_array( SIZE_T,
13905 0, // No parameters yet
13906 NULL); // And, hence, no types
13907 tree pointer_type = build_pointer_type(function_type);
13908
13909 static std::unordered_set<size_t>callers;
13910
13911 for( std::unordered_map<size_t, std::vector<const hier_node *>>::const_iterator mol = map_of_lists.begin();
13912 mol != map_of_lists.end();
13913 mol++ )
13914 {
13915 size_t caller = mol->first;
13916 if( caller != 0 )
13917 {
13918 if( callers.find(caller) == callers.end() )
13919 {
13920 // We haven't seen this caller before
13921
13922 char ach[3*sizeof(cbl_name_t)];
13923 tree names_table_type = build_array_type_nelts(CHAR_P, mol->second.size()+1);
13924 sprintf(ach, "..our_accessible_functions_" HOST_SIZE_T_PRINT_DEC,
13925 (fmt_size_t)caller);
13926 tree the_names_table = gg_define_variable(names_table_type, ach, vs_file_static);
13927
13928 // Here is where we build a table out of constructors:
13929 tree constructed_array_type = build_array_type_nelts(pointer_type, mol->second.size());
13930 sprintf(ach, "..our_constructed_table_" HOST_SIZE_T_PRINT_DEC,
13931 (fmt_size_t)caller);
13932 tree the_constructed_table = gg_define_variable(constructed_array_type, ach, vs_file_static);
13933
13934 tree constr_names = make_node(CONSTRUCTOR);
13935 TREE_TYPE(constr_names) = names_table_type;
13936 TREE_STATIC(constr_names) = 1;
13937 TREE_CONSTANT(constr_names) = 1;
13938
13939 tree constr = make_node(CONSTRUCTOR);
13940 TREE_TYPE(constr) = constructed_array_type;
13941 TREE_STATIC(constr) = 1;
13942 TREE_CONSTANT(constr) = 1;
13943
13944 int i=0;
13945 for( std::vector<const hier_node *>::const_iterator callee = mol->second.begin();
13946 callee != mol->second.end();
13947 callee++ )
13948 {
13949 sprintf(ach,
13950 "%s." HOST_SIZE_T_PRINT_DEC,
13951 (*callee)->name,
13952 (fmt_size_t)(*callee)->parent_node->our_index);
13953
13954 CONSTRUCTOR_APPEND_ELT( CONSTRUCTOR_ELTS(constr_names),
13955 build_int_cst_type(SIZE_T, i),
13956 build_string_literal(ach));
13957
13958 // Build the constructor element for that function:
13959 tree function_decl = build_fn_decl (ach, function_type);
13960 tree addr_expr = build1(ADDR_EXPR, pointer_type, function_decl);
13961
13962 CONSTRUCTOR_APPEND_ELT( CONSTRUCTOR_ELTS(constr),
13963 build_int_cst_type(SIZE_T, i),
13964 addr_expr);
13965
13966 i++;
13967 }
13968 // Terminate the names table with NULL
13969 CONSTRUCTOR_APPEND_ELT( CONSTRUCTOR_ELTS(constr_names),
13970 build_int_cst_type(SIZE_T, i),
13971 null_pointer_node);
13972
13973 DECL_INITIAL(the_names_table) = constr_names;
13974 DECL_INITIAL(the_constructed_table) = constr;
13975
13976 // And put a pointer to that table into the file-static variable set aside
13977 // for it:
13978 sprintf(ach, "..accessible_program_list_" HOST_SIZE_T_PRINT_DEC,
13979 (fmt_size_t)caller);
13980 tree accessible_list_var_decl = gg_trans_unit_var_decl(ach);
13981 gg_assign( accessible_list_var_decl, gg_get_address_of(the_names_table) );
13982
13983 sprintf(ach, "..accessible_program_pointers_" HOST_SIZE_T_PRINT_DEC,
13984 (fmt_size_t)caller);
13985 tree accessible_programs_decl = gg_trans_unit_var_decl(ach);
13986 gg_assign( accessible_programs_decl, gg_get_address_of(the_constructed_table) );
13987
13988 callers.insert(caller);
13989 }
13990 }
13991 }
13992 gg_append_statement(label_list_back_goto);
13993 gg_append_statement(skipper_label);
13994 }
13995
13996 void
13997 parser_set_numeric(struct cbl_field_t *tgt, ssize_t value)
13998 {
13999 Analyze();
14000 SHOW_PARSE
14001 {
14002 SHOW_PARSE_HEADER
14003 SHOW_PARSE_TEXT(" set ")
14004 SHOW_PARSE_TEXT(tgt->name)
14005 SHOW_PARSE_TEXT(" to ")
14006 char ach[32];
14007 sprintf(ach, HOST_SIZE_T_PRINT_DEC, (fmt_size_t)value);
14008 SHOW_PARSE_TEXT(ach);
14009 SHOW_PARSE_END
14010 }
14011
14012 gg_call(VOID,
14013 "__gg__int128_to_field",
14014 gg_get_address_of(tgt->var_decl_node),
14015 build_int_cst_type(INT128, value),
14016 integer_zero_node,
14017 build_int_cst_type(INT, truncation_e),
14018 null_pointer_node,
14019 NULL_TREE );
14020 }
14021
14022 void
14023 parser_exception_clear()
14024 {
14025 if( mode_syntax_only() ) return;
14026
14027 Analyze();
14028 gg_assign(var_decl_exception_code, integer_zero_node);
14029 }
14030
14031 void
14032 parser_exception_raise(ec_type_t ec)
14033 {
14034 Analyze();
14035 if( ec == ec_none_e )
14036 {
14037 gg_call(VOID,
14038 "__gg__set_exception_code",
14039 integer_zero_node,
14040 integer_one_node,
14041 NULL_TREE);
14042 }
14043 else
14044 {
14045 set_exception_code_func(ec, __LINE__, 1);
14046 }
14047 }
14048
14049 void
14050 parser_match_exception(cbl_field_t *index)
14051 {
14052 Analyze();
14053 SHOW_PARSE
14054 {
14055 SHOW_PARSE_HEADER
14056 SHOW_PARSE_FIELD(" index ", index)
14057 SHOW_PARSE_INDENT
14058 SHOW_PARSE_END
14059 }
14060
14061 TRACE1
14062 {
14063 TRACE1_HEADER
14064 TRACE1_FIELD("index ", index, "")
14065 TRACE1_INDENT
14066 TRACE1_END
14067 }
14068
14069 gg_call(VOID,
14070 "__gg__match_exception",
14071 gg_get_address_of(index->var_decl_node),
14072 NULL_TREE);
14073
14074 TRACE1
14075 {
14076 static tree index_val = gg_define_variable(INT, "..pme_index", vs_file_static);
14077 get_binary_value(index_val, NULL, index, size_t_zero_node);
14078 TRACE1_INDENT
14079 gg_printf("returned value is 0x%x (%d)", index_val, index_val, NULL_TREE);
14080 TRACE1_END
14081 }
14082 }
14083
14084 void
14085 parser_check_fatal_exception()
14086 {
14087 Analyze();
14088 SHOW_PARSE
14089 {
14090 SHOW_PARSE_HEADER
14091 SHOW_PARSE_TEXT(" Check for fatal EC...")
14092 SHOW_PARSE_END
14093 }
14094 TRACE1
14095 {
14096 TRACE1_HEADER
14097 TRACE1_TEXT(" Check for fatal EC...")
14098 TRACE1_END
14099 }
14100
14101 // Performance note:
14102 // A simple program that does two billion additions of 32-bit binary numbers
14103 // in its innermost loop had an execution time of 19.5 seconds. By putting in
14104 // the if() statement, that was reduced to 3.8 seconds.
14105
14106 if( cdf_enabled_exceptions().size() || sv_is_i_o )
14107 {
14108 gg_call(VOID,
14109 "__gg__check_fatal_exception",
14110 NULL_TREE);
14111 }
14112 }
14113
14114 void
14115 parser_push_exception()
14116 {
14117 gg_call(VOID, "__gg__exception_push", NULL_TREE);
14118 }
14119
14120 void
14121 parser_pop_exception()
14122 {
14123 gg_call(VOID, "__gg__exception_pop", NULL_TREE);
14124 }
14125
14126 void
14127 parser_clear_exception()
14128 {
14129 Analyze();
14130 SHOW_PARSE
14131 {
14132 SHOW_PARSE_HEADER
14133 SHOW_PARSE_TEXT(" Clear raised EC...")
14134 SHOW_PARSE_END
14135 }
14136 gg_call(VOID, "__gg__clear_exception", NULL_TREE);
14137 }
14138
14139 void
14140 parser_exception_file( cbl_field_t *tgt, cbl_file_t *file)
14141 {
14142 Analyze();
14143 gg_call(VOID,
14144 "__gg__func_exception_file",
14145 gg_get_address_of(tgt->var_decl_node),
14146 file ? gg_get_address_of(file->var_decl_node) : null_pointer_node,
14147 NULL_TREE);
14148 }
14149
14150 void
14151 parser_file_stash( struct cbl_file_t *file )
14152 {
14153 Analyze();
14154 SHOW_PARSE
14155 {
14156 SHOW_PARSE_HEADER
14157 if(file)
14158 {
14159 SHOW_PARSE_TEXT(" ");
14160 SHOW_PARSE_TEXT(file->name);
14161 }
14162 else
14163 {
14164 SHOW_PARSE_TEXT(" *file is NULL ")
14165 }
14166 SHOW_PARSE_END
14167 }
14168
14169 if( file )
14170 {
14171 TRACE1
14172 {
14173 TRACE1_HEADER
14174 TRACE1_TEXT("parser_file_stash of ")
14175 TRACE1_TEXT(file->name);
14176 TRACE1_END
14177 }
14178
14179 gg_call(VOID,
14180 "__gg__file_stash",
14181 gg_get_address_of(file->var_decl_node),
14182 NULL_TREE);
14183 }
14184 else
14185 {
14186 TRACE1
14187 {
14188 TRACE1_HEADER
14189 TRACE1_TEXT("parser_file_stash of NULL ")
14190 TRACE1_END
14191 }
14192
14193 gg_call(VOID,
14194 "__gg__file_stash",
14195 null_pointer_node,
14196 NULL_TREE);
14197 }
14198 }
14199
14200 static void
14201 hijack_for_development(const char *funcname)
14202 {
14203 /*
14204
14205 To make sure that things like global symbols and whatnot get initialized, you
14206 should probably create a source file that looks like this:
14207
14208 identification division.
14209 program-id. prog.
14210 procedure division.
14211 call "dubner".
14212 end program prog.
14213 identification division.
14214 program-id. dubner.
14215 procedure division.
14216 goback.
14217 end program dubner.
14218
14219 The first program will cause all of the parser_enter_program() and
14220 parser_division(procedure_div_e) stuff to be initialized. The second program,
14221 named "dubner", will be hijacked and bring you here. */
14222
14223 // Assume that funcname is lowercase with no hyphens
14224 enter_program_common(funcname, funcname);
14225 parser_display_literal("You have been hijacked by a program named \"dubner\"");
14226 gg_insert_into_assemblerf("%s HIJACKED DUBNER CODE START", ASM_COMMENT_START);
14227
14228 for(int i=0; i<10; i++)
14229 {
14230 char ach[64];
14231 sprintf(ach, "Hello, world - %d", i+1);
14232
14233 gg_call(VOID,
14234 "puts",
14235 build_string_literal(strlen(ach)+1, ach),
14236 NULL_TREE);
14237 }
14238
14239 gg_insert_into_assemblerf("%s HIJACKED DUBNER CODE END", ASM_COMMENT_START);
14240 gg_return(0);
14241 }
14242
14243 static void
14244 conditional_abs(tree source, const cbl_field_t *field)
14245 {
14246 Analyze();
14247 if( !(field->attr & signable_e) )
14248 {
14249 gg_assign(source, gg_abs(source));
14250 }
14251 }
14252
14253 static bool
14254 mh_identical(const cbl_refer_t &destref,
14255 const cbl_refer_t &sourceref,
14256 const TREEPLET &tsource)
14257 {
14258 // Check to see if the two variables are identical types, thus allowing
14259 // for a simple byte-for-byte copy of the data areas:
14260 bool moved = false;
14261 if( destref.field->type == sourceref.field->type
14262 && destref.field->data.capacity == sourceref.field->data.capacity
14263 && destref.field->data.digits == sourceref.field->data.digits
14264 && destref.field->data.rdigits == sourceref.field->data.rdigits
14265 && (destref.field->attr & (signable_e|separate_e|leading_e))
14266 == (sourceref.field->attr & (signable_e|separate_e|leading_e))
14267 && !destref.field->occurs.depending_on
14268 && !sourceref.field->occurs.depending_on
14269 && !destref.refmod.from
14270 && !sourceref.refmod.len
14271 && !(destref.field->attr & intermediate_e) // variables with variable
14272 && !(sourceref.field->attr & intermediate_e) // capacities have to be
14273 && !(destref.field->attr & any_length_e) // handled elsewhere
14274 && !(sourceref.field->attr & any_length_e)
14275 )
14276 {
14277 // The source and destination are identical in type
14278 if( !symbol_find_odo(sourceref.field) )
14279 {
14280 Analyze();
14281 // Source doesn't have a depending_on clause
14282 SHOW_PARSE1
14283 {
14284 SHOW_PARSE_INDENT
14285 SHOW_PARSE_TEXT("mh_identical()");
14286 }
14287 gg_memcpy(gg_add(member(destref.field->var_decl_node, "data"),
14288 refer_offset(destref)),
14289 gg_add(member(sourceref.field->var_decl_node, "data"),
14290 tsource.offset),
14291 build_int_cst_type(SIZE_T, sourceref.field->data.capacity));
14292 moved = true;
14293 }
14294 }
14295 return moved;
14296 }
14297
14298 static bool
14299 mh_source_is_literalN(cbl_refer_t &destref,
14300 cbl_refer_t &sourceref,
14301 bool check_for_error,
14302 cbl_round_t rounded,
14303 tree size_error)
14304 {
14305 bool moved = false;
14306 if( sourceref.field->type == FldLiteralN )
14307 {
14308 Analyze();
14309 switch( destref.field->type )
14310 {
14311 case FldGroup:
14312 case FldAlphanumeric:
14313 {
14314 SHOW_PARSE1
14315 {
14316 SHOW_PARSE_INDENT
14317 SHOW_PARSE_TEXT("mh_source_is_literalN: __gg__psz_to_alpha_move")
14318 }
14319
14320 static char *buffer = NULL;
14321 static size_t buffer_size = 0;
14322 raw_to_internal(&buffer,
14323 &buffer_size,
14324 sourceref.field->data.initial,
14325 strlen(sourceref.field->data.initial));
14326 gg_call(VOID,
14327 "__gg__psz_to_alpha_move",
14328 gg_get_address_of(destref.field->var_decl_node),
14329 refer_offset(destref),
14330 refer_size_dest(destref),
14331 gg_string_literal(buffer),
14332 build_int_cst_type(SIZE_T, strlen(sourceref.field->data.initial)),
14333 NULL_TREE);
14334 moved = true;
14335 break;
14336 }
14337
14338 case FldPointer:
14339 case FldIndex:
14340 {
14341 // We know this is a move to an eight-byte value:
14342 SHOW_PARSE1
14343 {
14344 SHOW_PARSE_INDENT
14345 SHOW_PARSE_TEXT("mh_source_is_literalN: pointer/index")
14346 }
14347
14348 if( sourceref.field->data.capacity < 8 )
14349 {
14350 // There are too few bytes in sourceref
14351 if( sourceref.field->attr & signable_e )
14352 {
14353 static tree highbyte = gg_define_variable(UCHAR, "..mh_litN_highbyte", vs_file_static);
14354 // Pick up the source byte that has the sign bit.
14355 gg_assign(highbyte,
14356 gg_get_indirect_reference(gg_add(member(sourceref.field->var_decl_node,
14357 "data"),
14358 build_int_cst_type(SIZE_T,
14359 sourceref.field->data.capacity-1)),
14360 integer_zero_node));
14361 IF( gg_bitwise_and(highbyte, build_int_cst_type(UCHAR, 0x80)),
14362 eq_op,
14363 build_int_cst_type(UCHAR, 0x80) )
14364 {
14365 // We are dealing with a negative number
14366 gg_memset(gg_add(member(destref.field->var_decl_node, "data"),
14367 refer_offset(destref)),
14368 build_int_cst_type(UCHAR, 0xFF),
14369 build_int_cst_type(SIZE_T, 8));
14370 }
14371 ELSE
14372 gg_memset(gg_add(member(destref.field->var_decl_node, "data"),
14373 refer_offset(destref)),
14374 build_int_cst_type(UCHAR, 0x00),
14375 build_int_cst_type(SIZE_T, 8));
14376 ENDIF
14377 }
14378 else
14379 {
14380 // The too-short source is positive.
14381 gg_memset(gg_add(member(destref.field->var_decl_node, "data"),
14382 refer_offset(destref)),
14383 build_int_cst_type(UCHAR, 0x00),
14384 build_int_cst_type(SIZE_T, 8));
14385 }
14386 }
14387
14388 tree literalN_value = get_literalN_value(sourceref.field);
14389 scale_by_power_of_ten_N(literalN_value, -sourceref.field->data.rdigits);
14390 gg_memcpy(gg_add(member(destref.field->var_decl_node, "data"),
14391 refer_offset(destref)),
14392 gg_get_address_of(literalN_value),
14393 build_int_cst_type(SIZE_T, sourceref.field->data.capacity));
14394 moved = true;
14395
14396 break;
14397 }
14398
14399 case FldNumericBin5:
14400 {
14401 // We are moving from a FldLiteralN (which we know has no subscripts or
14402 // refmods), to a NumericBin5, which might.
14403
14404 SHOW_PARSE1
14405 {
14406 SHOW_PARSE_INDENT
14407 SHOW_PARSE_TEXT("mh_source_is_literalN: FldNumericBin5")
14408 }
14409
14410 // For now, we are ignoring intermediates:
14411 assert( !(destref.field->attr & intermediate_e) );
14412
14413 int bytes_needed = std::max(destref.field->data.capacity,
14414 sourceref.field->data.capacity);
14415 tree calc_type = tree_type_from_size(bytes_needed,
14416 sourceref.field->attr & signable_e);
14417 tree dest_type = tree_type_from_size( destref.field->data.capacity,
14418 destref.field->attr & signable_e);
14419
14420 // Pick up the source data.
14421 tree source = gg_define_variable(calc_type);
14422 gg_assign(source, gg_cast(calc_type, sourceref.field->data_decl_node));
14423
14424 // Take the absolute value, if the destination is not signable
14425 conditional_abs(source, destref.field);
14426
14427 // See if it needs to be scaled:
14428 scale_by_power_of_ten_N(
14429 source,
14430 destref.field->data.rdigits-sourceref.field->data.rdigits);
14431
14432 if( check_for_error && size_error )
14433 {
14434 Analyzer.Message("Check to see if result fits");
14435 if( destref.field->data.digits )
14436 {
14437 FIXED_WIDE_INT(128) power_of_ten = get_power_of_ten(destref.field->data.digits);
14438 IF( gg_abs(source), ge_op, wide_int_to_tree(calc_type,
14439 power_of_ten) )
14440 {
14441 gg_assign(size_error, gg_bitwise_or(size_error, integer_one_node));
14442 }
14443 ELSE
14444 ENDIF
14445 }
14446 }
14447
14448 Analyzer.Message("Move to destination location");
14449 tree dest_location = gg_indirect(
14450 gg_cast(build_pointer_type(dest_type),
14451 gg_add(member(destref.field->var_decl_node, "data"),
14452 refer_offset(destref))));
14453 gg_assign(dest_location, gg_cast(dest_type, source));
14454 moved = true;
14455 break;
14456 }
14457
14458 case FldNumericDisplay:
14459 case FldNumericBinary:
14460 case FldNumericEdited:
14461 case FldPacked:
14462 {
14463 static tree berror = gg_define_variable(INT, "..mh_litN_berror", vs_file_static);
14464 gg_assign(berror, integer_zero_node);
14465 SHOW_PARSE1
14466 {
14467 SHOW_PARSE_INDENT
14468 SHOW_PARSE_TEXT("calling get_literalN_value ")
14469 }
14470 tree literalN_value = get_literalN_value(sourceref.field);
14471
14472 SHOW_PARSE1
14473 {
14474 SHOW_PARSE_INDENT
14475 SHOW_PARSE_TEXT("calling __gg__int128_to_qualified_field ")
14476 }
14477
14478 gg_call(INT,
14479 "__gg__int128_to_qualified_field",
14480 gg_get_address_of(destref.field->var_decl_node),
14481 refer_offset(destref),
14482 refer_size_dest(destref),
14483 gg_cast(INT128, literalN_value),
14484 build_int_cst_type(INT, sourceref.field->data.rdigits),
14485 build_int_cst_type(INT, rounded),
14486 gg_get_address_of(berror),
14487 NULL_TREE);
14488
14489 if( size_error )
14490 {
14491 IF( berror, ne_op, integer_zero_node )
14492 {
14493 gg_assign(size_error, gg_bitwise_or(size_error, integer_one_node));
14494 }
14495 ELSE
14496 ENDIF
14497 }
14498 moved = true;
14499 break;
14500 }
14501
14502 case FldAlphaEdited:
14503 {
14504 SHOW_PARSE1
14505 {
14506 SHOW_PARSE_INDENT
14507 SHOW_PARSE_TEXT(" FldAlphaEdited")
14508 }
14509 gg_call(VOID,
14510 "__gg__string_to_alpha_edited_ascii",
14511 gg_add( member(destref.field->var_decl_node, "data"),
14512 refer_offset(destref) ),
14513 gg_string_literal(sourceref.field->data.initial),
14514 build_int_cst_type(INT, strlen(sourceref.field->data.initial)),
14515 gg_string_literal(destref.field->data.picture),
14516 NULL_TREE);
14517 moved = true;
14518 break;
14519 }
14520
14521 case FldFloat:
14522 {
14523 tree tdest = gg_add(member(destref.field->var_decl_node, "data"),
14524 refer_offset(destref) );
14525 switch( destref.field->data.capacity )
14526 {
14527 // For some reason, using FLOAT128 in the build_pointer_type causes
14528 // a SEGFAULT. So, we'll use other types with equivalent sizes. I
14529 // am speculating that the use of floating-point types causes the -O0
14530 // compilation to move things using the mmx registers. So, I am using
14531 // intxx types in the hope that they are simpler.
14532 case 4:
14533 {
14534 // The following generated code is the exact equivalent
14535 // of the C code:
14536 // *(float *)dest = (float)data.value
14537 gg_assign(gg_indirect(gg_cast(build_pointer_type(FLOAT), tdest)),
14538 fold_convert (FLOAT, sourceref.field->data.value_of()));
14539 break;
14540 }
14541 case 8:
14542 {
14543 gg_assign(gg_indirect(gg_cast(build_pointer_type(DOUBLE), tdest)),
14544 fold_convert (DOUBLE, sourceref.field->data.value_of()));
14545 break;
14546 }
14547 case 16:
14548 {
14549 gg_assign(gg_indirect(gg_cast(build_pointer_type(FLOAT128), tdest)),
14550 sourceref.field->data.value_of());
14551 break;
14552 }
14553 }
14554 moved=true;
14555 break;
14556 }
14557
14558 default:
14559 cbl_internal_error(
14560 "In %<parser_move(%s to %s)%>, the move of FldLiteralN to %s "
14561 "is unimplemented",
14562 sourceref.field->name,
14563 destref.field->name,
14564 cbl_field_type_str(destref.field->type));
14565 break;
14566 }
14567 }
14568 return moved;
14569 }
14570
14571 static
14572 tree float_type_of(int n)
14573 {
14574 switch(n)
14575 {
14576 case 4:
14577 return FLOAT;
14578 case 8:
14579 return DOUBLE;
14580 case 16:
14581 return FLOAT128;
14582 default:
14583 gcc_unreachable();
14584 }
14585 return NULL_TREE;
14586 }
14587
14588 static tree
14589 float_type_of(const cbl_field_t *field)
14590 {
14591 gcc_assert(field->type == FldFloat);
14592 return float_type_of(field->data.capacity);
14593 }
14594
14595 static tree
14596 float_type_of(const cbl_refer_t *refer)
14597 {
14598 return float_type_of(refer->field);
14599 }
14600
14601 static bool
14602 mh_dest_is_float( cbl_refer_t &destref,
14603 cbl_refer_t &sourceref,
14604 TREEPLET &tsource,
14605 cbl_round_t rounded,
14606 tree size_error) // int
14607 {
14608 bool moved = false;
14609 if( destref.field->type == FldFloat )
14610 {
14611 Analyze();
14612 switch( sourceref.field->type )
14613 {
14614 case FldPointer:
14615 case FldIndex:
14616 case FldNumericBin5:
14617 case FldNumericDisplay:
14618 case FldNumericBinary:
14619 case FldNumericEdited:
14620 case FldPacked:
14621 {
14622 switch( destref.field->data.capacity )
14623 {
14624 case 4:
14625 gg_call(VOID,
14626 "__gg__float32_from_int128",
14627 gg_get_address_of(destref.field->var_decl_node),
14628 refer_offset(destref),
14629 tsource.pfield,
14630 tsource.offset,
14631 build_int_cst_type(INT, rounded),
14632 size_error ? gg_get_address_of(size_error) : null_pointer_node,
14633 NULL_TREE);
14634 break;
14635 case 8:
14636 gg_call(VOID,
14637 "__gg__float64_from_int128",
14638 gg_get_address_of(destref.field->var_decl_node),
14639 refer_offset(destref),
14640 tsource.pfield,
14641 tsource.offset,
14642 build_int_cst_type(INT, rounded),
14643 size_error ? gg_get_address_of(size_error) : null_pointer_node,
14644 NULL_TREE);
14645 break;
14646 case 16:
14647 gg_call(VOID,
14648 "__gg__float128_from_int128",
14649 gg_get_address_of(destref.field->var_decl_node),
14650 refer_offset(destref),
14651 tsource.pfield,
14652 tsource.offset,
14653 build_int_cst_type(INT, rounded),
14654 size_error ? gg_get_address_of(size_error) : null_pointer_node,
14655 NULL_TREE);
14656 break;
14657 }
14658 moved = true;
14659 break;
14660 }
14661
14662 case FldFloat:
14663 {
14664 // We are testing for size. First, we need to check to see if the
14665 // source is INFINITY. If so, that's an automatic size error
14666
14667 IF( gg_call_expr( INT,
14668 "__gg__is_float_infinite",
14669 tsource.pfield,
14670 tsource.offset,
14671 NULL_TREE),
14672 ne_op,
14673 integer_zero_node )
14674 {
14675 if( size_error )
14676 {
14677 gg_assign(size_error, integer_one_node );
14678 }
14679 }
14680 ELSE
14681 {
14682 // The source isn't infinite.
14683 // If the destination is bigger than the source, then we can
14684 // do an untested move:
14685
14686 if( destref.field->data.capacity >= sourceref.field->data.capacity )
14687 {
14688 tree dtype = float_type_of(&destref);
14689 tree stype = float_type_of(&sourceref);
14690
14691 tree tdest = gg_add(member(destref.field->var_decl_node, "data"),
14692 refer_offset(destref));
14693 tree source = gg_add(member(sourceref.field->var_decl_node, "data"),
14694 refer_offset(sourceref));
14695 gg_assign(gg_indirect(gg_cast(build_pointer_type(dtype), tdest)),
14696 gg_cast(dtype,
14697 gg_indirect(gg_cast(build_pointer_type(stype),
14698 source))));
14699 }
14700 else
14701 {
14702 // There are only three possible moves left:
14703 if(destref.field->data.capacity == 8 )
14704 {
14705 if( size_error )
14706 {
14707 gg_assign(size_error,
14708 gg_call_expr( INT,
14709 "__gg__float64_from_128",
14710 gg_get_address_of(destref.field->var_decl_node),
14711 refer_offset(destref),
14712 tsource.pfield,
14713 tsource.offset,
14714 NULL_TREE));
14715 }
14716 else
14717 {
14718 gg_call( INT,
14719 "__gg__float64_from_128",
14720 gg_get_address_of(destref.field->var_decl_node),
14721 refer_offset(destref),
14722 tsource.pfield,
14723 tsource.offset,
14724 NULL_TREE);
14725 }
14726 }
14727 else
14728 {
14729 // The destination has to be float32
14730 if( sourceref.field->data.capacity == 8 )
14731 {
14732 if( size_error )
14733 {
14734 gg_assign(size_error,
14735 gg_call_expr( INT,
14736 "__gg__float32_from_64",
14737 gg_get_address_of(destref.field->var_decl_node),
14738 refer_offset(destref),
14739 tsource.pfield,
14740 tsource.offset,
14741 NULL_TREE));
14742 }
14743 else
14744 {
14745 gg_call( INT,
14746 "__gg__float32_from_64",
14747 gg_get_address_of(destref.field->var_decl_node),
14748 refer_offset(destref),
14749 tsource.pfield,
14750 tsource.offset,
14751 NULL_TREE);
14752 }
14753
14754 }
14755 else
14756 {
14757 if( size_error )
14758 {
14759 gg_assign(size_error,
14760 gg_call_expr( INT,
14761 "__gg__float32_from_128",
14762 gg_get_address_of(destref.field->var_decl_node),
14763 refer_offset(destref),
14764 tsource.pfield,
14765 tsource.offset,
14766 NULL_TREE));
14767 }
14768 else
14769 {
14770 gg_call( INT,
14771 "__gg__float32_from_128",
14772 gg_get_address_of(destref.field->var_decl_node),
14773 refer_offset(destref),
14774 tsource.pfield,
14775 tsource.offset,
14776 NULL_TREE);
14777 }
14778 }
14779 }
14780 }
14781 }
14782 ENDIF
14783
14784 moved = true;
14785 break;
14786 }
14787
14788 case FldLiteralA:
14789 case FldAlphanumeric:
14790 {
14791 // Alphanumeric to float is inherently slow. Send it off to the library
14792 break;
14793 }
14794
14795 default:
14796 cbl_internal_error("In %<mh_dest_is_float%>(%s to %s), the "
14797 "move of %s to %s is unimplemented",
14798 sourceref.field->name,
14799 destref.field->name,
14800 cbl_field_type_str(sourceref.field->type),
14801 cbl_field_type_str(destref.field->type));
14802 break;
14803 }
14804 }
14805 return moved;
14806 }
14807
14808 static void
14809 picky_memset(tree &dest_p, unsigned char value, size_t length)
14810 {
14811 if( length )
14812 {
14813 tree dest_ep = gg_define_variable(TREE_TYPE(dest_p));
14814 gg_assign(dest_ep,
14815 gg_add( dest_p,
14816 build_int_cst_type(SIZE_T, length)));
14817 WHILE( dest_p, lt_op, dest_ep )
14818 {
14819 gg_assign(gg_indirect(dest_p),
14820 build_int_cst_type(UCHAR, value));
14821 gg_increment(dest_p);
14822 }
14823 WEND
14824 }
14825 }
14826
14827 static void
14828 picky_memcpy(tree &dest_p, const tree &source_p, size_t length)
14829 {
14830 if( length )
14831 {
14832 tree dest_ep = gg_define_variable(TREE_TYPE(dest_p));
14833 gg_assign(dest_ep,
14834 gg_add( dest_p,
14835 build_int_cst_type(SIZE_T, length)));
14836 WHILE( dest_p, lt_op, dest_ep )
14837 {
14838 gg_assign(gg_indirect(dest_p), gg_indirect(source_p));
14839 gg_increment(dest_p);
14840 gg_increment(source_p);
14841 }
14842 WEND
14843 }
14844 }
14845
14846 static bool
14847 mh_numeric_display( const cbl_refer_t &destref,
14848 const cbl_refer_t &sourceref,
14849 const TREEPLET &tsource,
14850 tree size_error)
14851 {
14852 bool moved = false;
14853
14854 if( destref.field->type == FldNumericDisplay
14855 && sourceref.field->type == FldNumericDisplay
14856 && !(destref.field->attr & scaled_e)
14857 && !(sourceref.field->attr & scaled_e) )
14858 {
14859 Analyze();
14860 // I believe that there are 225 pathways through the following code. That's
14861 // because there are five different valid combination of signable_e,
14862 // separate_e, and leading_e. There are three possibilities for
14863 // sender/receiver rdigits (too many, too few, and just right), and the same
14864 // for ldigits. 5 * 5 * 3 * 3 = 225.
14865
14866 // Fasten your seat belts.
14867
14868 // In order to simplify processing of a signable internal sender, we are
14869 // going to pick up the sign byte and temporarily turn off the sign bit in
14870 // the source data. At the end, we will restore that value. This
14871 // reflexively makes me a bit nervous (it isn't, for example, thread-safe),
14872 // but it makes life easier.
14873
14874 static tree source_sign_loc = gg_define_variable(UCHAR_P, "..mhnd_sign_loc", vs_file_static);
14875 static tree source_sign_byte = gg_define_variable(UCHAR, "..mhnd_sign_byte", vs_file_static);
14876 static tree dest_p = gg_define_variable(UCHAR_P, "..mhnd_dest", vs_file_static); // The destination data pointer
14877 static tree source_p = gg_define_variable(UCHAR_P, "..mhnd_source", vs_file_static); // The source data pointer
14878 static tree source_ep = gg_define_variable(UCHAR_P, "..mhnd_source_e", vs_file_static); // When we need an end pointer
14879
14880 gg_assign(dest_p, qualified_data_location(destref));
14881 gg_assign(source_p, gg_add(member(sourceref.field, "data"),
14882 tsource.offset));
14883
14884 if( sourceref.field->attr & signable_e )
14885 {
14886 // The source is signable
14887
14888 if( !(sourceref.field->attr & leading_e) )
14889 {
14890 // The sign location is trailing. Whether separate or not, the location
14891 // is the final byte of the data:
14892 gg_assign(source_sign_loc, gg_add(member( sourceref.field->var_decl_node, "data"),
14893 tsource.offset)),
14894 gg_assign(source_sign_loc,
14895 gg_add(source_sign_loc,
14896 build_int_cst_type(SIZE_T,
14897 sourceref.field->data.capacity-1)));
14898 if( (sourceref.field->attr & separate_e) )
14899 {
14900 // We have trailing separate
14901 }
14902 else
14903 {
14904 // We have trailing internal
14905 }
14906 }
14907 else
14908 {
14909 // The source sign location is in the leading position.
14910 gg_assign(source_sign_loc,
14911 gg_add(member(sourceref.field->var_decl_node, "data"),
14912 tsource.offset));
14913 if( (sourceref.field->attr & separate_e) )
14914 {
14915 // We have leading separate, so the first actual digit is at
14916 // source_p+1.
14917 gg_increment(source_p);
14918 }
14919 else
14920 {
14921 // We have leading internal
14922 }
14923 }
14924 // Pick up the byte that contains the sign data, whether internal or
14925 // external:
14926 gg_assign(source_sign_byte, gg_indirect(source_sign_loc));
14927
14928 if( !(sourceref.field->attr & separate_e) )
14929 {
14930 // This is signable and internal, so we want to turn off the sign bit
14931 // in the original source data
14932 if( internal_codeset_is_ebcdic() )
14933 {
14934 gg_assign(gg_indirect(source_sign_loc),
14935 gg_bitwise_or(source_sign_byte,
14936 build_int_cst_type( UCHAR,
14937 NUMERIC_DISPLAY_SIGN_BIT)));
14938 }
14939 else
14940 {
14941 gg_assign(gg_indirect(source_sign_loc),
14942 gg_bitwise_and( source_sign_byte,
14943 build_int_cst_type( UCHAR,
14944 ~NUMERIC_DISPLAY_SIGN_BIT)));
14945 }
14946 }
14947 }
14948 else
14949 {
14950 // The number is unsigned, so do nothing.
14951 }
14952
14953 // Let the shenanigans begin.
14954
14955 // We are now ready to output the very first byte.
14956
14957 // The first thing to do is see if we need to output a leading sign
14958 // character
14959 if( (destref.field->attr & signable_e)
14960 && (destref.field->attr & leading_e)
14961 && (destref.field->attr & separate_e) )
14962 {
14963 // The output is signed, separate, and leading, so the first character
14964 // needs to be either '+' or '-'
14965 if( (sourceref.field->attr & separate_e) )
14966 {
14967 // The source is signable/separate
14968 // Oooh. Shiny. We already have that character.
14969 gg_assign(gg_indirect(dest_p), source_sign_byte);
14970 }
14971 else
14972 {
14973 // The source is internal. Not that up above we set source_sign_byte
14974 // even for source values that aren't signable
14975 if( internal_codeset_is_ebcdic() )
14976 {
14977 // We are working in EBCDIC
14978 if( sourceref.field->attr & signable_e )
14979 {
14980 IF( gg_bitwise_and( source_sign_byte,
14981 build_int_cst_type( UCHAR,
14982 NUMERIC_DISPLAY_SIGN_BIT)),
14983 eq_op,
14984 build_int_cst_type( UCHAR, 0) )
14985 {
14986 // The source was negative
14987 gg_assign(gg_indirect(dest_p),
14988 build_int_cst_type( UCHAR, EBCDIC_MINUS));
14989
14990 }
14991 ELSE
14992 {
14993 // The source was positive
14994 gg_assign(gg_indirect(dest_p),
14995 build_int_cst_type( UCHAR, EBCDIC_PLUS));
14996 }
14997 ENDIF
14998 }
14999 else
15000 {
15001 // The source is not signable, so the result is positive
15002 gg_assign(gg_indirect(dest_p),
15003 build_int_cst_type( UCHAR, EBCDIC_PLUS));
15004 }
15005 }
15006 else
15007 {
15008 // We are working in ASCII
15009 if( sourceref.field->attr & signable_e )
15010 {
15011 IF( gg_bitwise_and( source_sign_byte,
15012 build_int_cst_type( UCHAR,
15013 NUMERIC_DISPLAY_SIGN_BIT)),
15014 ne_op,
15015 build_int_cst_type( UCHAR, 0) )
15016 {
15017 // The source was negative
15018 gg_assign(gg_indirect(dest_p),
15019 build_int_cst_type( UCHAR, '-'));
15020
15021 }
15022 ELSE
15023 {
15024 // The source was positive
15025 gg_assign(gg_indirect(dest_p),
15026 build_int_cst_type( UCHAR, '+'));
15027 }
15028 ENDIF
15029 }
15030 else
15031 {
15032 // The source is not signable, so the result is positive
15033 gg_assign(gg_indirect(dest_p),
15034 build_int_cst_type( UCHAR, '+'));
15035 }
15036 }
15037 }
15038 gg_increment(dest_p);
15039 }
15040
15041 // We have the leading '+' or '-', assuming one is needed. We can
15042 // now start outputting the digits to the left of the decimal place
15043
15044 int dest_ldigits = (int)destref.field->data.digits
15045 - destref.field->data.rdigits;
15046 int source_ldigits = (int)sourceref.field->data.digits
15047 - sourceref.field->data.rdigits;
15048
15049 int digit_count = 0;
15050
15051 if( dest_ldigits > source_ldigits )
15052 {
15053 // The destination has more ldigits than the source, and needs some
15054 // leading zeroes:
15055 picky_memset( dest_p,
15056 internal_codeset_is_ebcdic() ?
15057 EBCDIC_ZERO : '0' ,
15058 dest_ldigits - source_ldigits);
15059 // With the leading zeros set, copy over the ldigits:
15060 digit_count = source_ldigits;
15061 }
15062 else if( dest_ldigits == source_ldigits )
15063 {
15064 // This is the Goldilocks zone. Everything is *just* right.
15065 digit_count = dest_ldigits;
15066 }
15067 else
15068 {
15069 // The destination is smaller than the source. We have to throw away the
15070 // the high-order digits of the source. If any of them are non-zero, then
15071 // we need to indicate a size error.
15072 gg_assign(source_ep,
15073 gg_add( source_p,
15074 build_int_cst_type( SIZE_T,
15075 source_ldigits-dest_ldigits)));
15076 WHILE(source_p, lt_op, source_ep)
15077 {
15078 if( size_error )
15079 {
15080 IF( gg_indirect(source_p),
15081 ne_op,
15082 build_int_cst_type( UCHAR,
15083 internal_codeset_is_ebcdic() ?
15084 EBCDIC_ZERO : '0') )
15085 {
15086 set_exception_code(ec_size_truncation_e);
15087 gg_assign(size_error, integer_one_node);
15088 }
15089 ELSE
15090 ENDIF
15091 }
15092 gg_increment(source_p);
15093 }
15094 WEND
15095
15096 // Having skipped over the leading digits, we are in position to move the
15097 // remaining digits
15098 digit_count = dest_ldigits;
15099 }
15100
15101 // The ldigits are in place. We now go the very similar exercise for the
15102 // rdigits:
15103
15104 int dest_rdigits = destref.field->data.rdigits;
15105 int source_rdigits = sourceref.field->data.rdigits;
15106
15107 int trailing_zeros = 0;
15108
15109 if( dest_rdigits > source_rdigits )
15110 {
15111 // The destination has more rdigits than the source
15112
15113 // Copy over the available digits:
15114 digit_count += source_rdigits;
15115
15116 // And then tack on the needed trailing zeroes:
15117 trailing_zeros = dest_rdigits - source_rdigits;
15118 }
15119 else if( dest_rdigits == source_rdigits )
15120 {
15121 // This is the Goldilocks zone. Everything is *just* right.
15122 digit_count += dest_rdigits;
15123 }
15124 else
15125 {
15126 // The destination has fewer rdigits than the source. We send
15127 // over only the necessary rdigits, discarding the ones to the right.
15128 digit_count += dest_rdigits;
15129 }
15130
15131 picky_memcpy(dest_p, source_p, digit_count);
15132 picky_memset( dest_p,
15133 internal_codeset_is_ebcdic() ?
15134 EBCDIC_ZERO : '0' ,
15135 trailing_zeros);
15136
15137 // With the digits in place, we need to sort out what to do if the target
15138 // is signable:
15139 if( destref.field->attr & signable_e )
15140 {
15141 if( (destref.field->attr & separate_e)
15142 && !(destref.field->attr & leading_e) )
15143 {
15144 // The target is separate/trailing, so we need to tack a '+'
15145 // or '-' character
15146 if( sourceref.field->attr & separate_e )
15147 {
15148 // The source was separate, so we already have what we need in t
15149 // source_sign_byte:
15150 gg_assign(gg_indirect(dest_p), source_sign_byte);
15151 gg_increment(dest_p);
15152 }
15153 else
15154 {
15155 // The source is either internal, or unsigned
15156 if( sourceref.field->attr & signable_e )
15157 {
15158 // The source is signable/internal, so we need to extract the
15159 // sign bit from source_sign_byte
15160 if( internal_codeset_is_ebcdic() )
15161 {
15162 IF( gg_bitwise_and( source_sign_byte,
15163 build_int_cst_type( UCHAR,
15164 NUMERIC_DISPLAY_SIGN_BIT)),
15165 eq_op,
15166 build_int_cst_type( UCHAR, 0) )
15167 {
15168 // The source was negative
15169 gg_assign(gg_indirect(dest_p),
15170 build_int_cst_type( UCHAR, EBCDIC_MINUS));
15171
15172 }
15173 ELSE
15174 {
15175 // The source was positive
15176 gg_assign(gg_indirect(dest_p),
15177 build_int_cst_type( UCHAR, EBCDIC_PLUS));
15178 }
15179 ENDIF
15180 }
15181 else
15182 {
15183 IF( gg_bitwise_and( source_sign_byte,
15184 build_int_cst_type( UCHAR,
15185 NUMERIC_DISPLAY_SIGN_BIT)),
15186 ne_op,
15187 build_int_cst_type( UCHAR, 0) )
15188 {
15189 // The source was negative
15190 gg_assign(gg_indirect(dest_p),
15191 build_int_cst_type( UCHAR, '-'));
15192
15193 }
15194 ELSE
15195 {
15196 // The source was positive
15197 gg_assign(gg_indirect(dest_p),
15198 build_int_cst_type( UCHAR, '+'));
15199 }
15200 ENDIF
15201 }
15202 }
15203 else
15204 {
15205 // The source is unsigned, so dest is positive
15206 gg_assign(gg_indirect(dest_p),
15207 build_int_cst_type( UCHAR,
15208 internal_codeset_is_ebcdic() ?
15209 EBCDIC_PLUS : '+' ));
15210 }
15211 }
15212 gg_increment(dest_p);
15213 }
15214 else if( !(destref.field->attr & separate_e) )
15215 {
15216 // The destination is signed/internal
15217 if( destref.field->attr & leading_e )
15218 {
15219 // The sign bit goes into the first byte:
15220 gg_assign(dest_p, qualified_data_location(destref));
15221 }
15222 else
15223 {
15224 // The sign bit goes into the last byte:
15225 gg_decrement(dest_p);
15226 }
15227 if( sourceref.field->attr & signable_e )
15228 {
15229 if( sourceref.field->attr & separate_e )
15230 {
15231 // The source is separate, so source_sign_byte is '+' or '-'
15232 IF( source_sign_byte,
15233 eq_op,
15234 build_int_cst_type(UCHAR,
15235 internal_codeset_is_ebcdic() ?
15236 EBCDIC_MINUS : '-') )
15237 {
15238 // The source is negative, so turn the ASCII bit on
15239 if( !internal_codeset_is_ebcdic() )
15240 {
15241 gg_assign(gg_indirect(dest_p),
15242 gg_bitwise_or(gg_indirect(dest_p),
15243 build_int_cst_type(
15244 UCHAR,
15245 NUMERIC_DISPLAY_SIGN_BIT)));
15246
15247 }
15248 else
15249 {
15250 // It's ebcdic, so turn the sign bit OFF
15251 gg_assign(gg_indirect(dest_p),
15252 gg_bitwise_and(gg_indirect(dest_p),
15253 build_int_cst_type(
15254 UCHAR,
15255 ~NUMERIC_DISPLAY_SIGN_BIT)));
15256 }
15257 }
15258 ELSE
15259 {
15260 // The source is positive, so turn the EBCDIC bit ON:
15261 if( internal_codeset_is_ebcdic() )
15262 {
15263 gg_assign(gg_indirect(dest_p),
15264 gg_bitwise_or(gg_indirect(dest_p),
15265 build_int_cst_type(
15266 UCHAR,
15267 NUMERIC_DISPLAY_SIGN_BIT)));
15268 }
15269 }
15270 ENDIF
15271 }
15272 else
15273 {
15274 // The source is signable/internal, so the sign bit is in
15275 // source_sign_byte. Whatever it is, it has to go into dest_p:
15276 if( internal_codeset_is_ebcdic() )
15277 {
15278 // This is EBCDIC, so if the source_sign_byte bit is LOW, we
15279 // clear that bit in dest_p high.
15280 IF( gg_bitwise_and( source_sign_byte,
15281 build_int_cst_type(
15282 UCHAR,
15283 NUMERIC_DISPLAY_SIGN_BIT)),
15284 eq_op,
15285 build_int_cst_type(UCHAR, 0) )
15286 {
15287 // The source was negative, so make the dest negative
15288 gg_assign(gg_indirect(dest_p),
15289 gg_bitwise_and(gg_indirect(dest_p),
15290 build_int_cst_type(
15291 UCHAR,
15292 ~NUMERIC_DISPLAY_SIGN_BIT)));
15293 }
15294 ELSE
15295 ENDIF
15296 }
15297 else
15298 {
15299 // This is ASCII, so if the source_sign_byte bit is high, we
15300 // set that bit in dest_p high.
15301 IF( gg_bitwise_and( source_sign_byte,
15302 build_int_cst_type(
15303 UCHAR,
15304 NUMERIC_DISPLAY_SIGN_BIT)),
15305 ne_op,
15306 build_int_cst_type(UCHAR, 0) )
15307 {
15308 // The source was negative, so make the dest negative
15309 gg_assign(gg_indirect(dest_p),
15310 gg_bitwise_or(gg_indirect(dest_p),
15311 build_int_cst_type(
15312 UCHAR,
15313 NUMERIC_DISPLAY_SIGN_BIT)));
15314 }
15315 ELSE
15316 ENDIF
15317 }
15318 }
15319 }
15320 }
15321 }
15322
15323 if( (sourceref.field->attr & signable_e)
15324 && !(sourceref.field->attr & separate_e))
15325 {
15326 // The source is signable internal, so we need to restore the original
15327 // sign byte in the original source data:
15328 gg_assign(gg_indirect(source_sign_loc), source_sign_byte);
15329 }
15330 moved = true;
15331 }
15332 return moved;
15333 }
15334
15335 static bool
15336 mh_little_endian( const cbl_refer_t &destref,
15337 const cbl_refer_t &sourceref,
15338 const TREEPLET &tsource,
15339 bool check_for_error,
15340 tree size_error)
15341 {
15342 bool moved = false;
15343
15344 cbl_figconst_t figconst = cbl_figconst_of( sourceref.field->data.initial);
15345
15346 if( !figconst
15347 && !(destref.field->attr & scaled_e)
15348 && !(destref.field->attr & (intermediate_e ))
15349 && !(sourceref.field->attr & (intermediate_e ))
15350 && sourceref.field->type != FldLiteralA
15351 && sourceref.field->type != FldAlphanumeric
15352 && sourceref.field->type != FldNumericEdited
15353 && sourceref.field->type != FldPacked
15354 && ( destref.field->type == FldNumericBin5
15355 || destref.field->type == FldPointer
15356 || destref.field->type == FldIndex ) )
15357 {
15358 Analyze();
15359 SHOW_PARSE1
15360 {
15361 SHOW_PARSE_INDENT
15362 SHOW_PARSE_TEXT("mh_little_endian")
15363 SHOW_PARSE_END
15364 }
15365
15366 int bytes_needed = get_bytes_needed(sourceref.field);
15367 tree source_type = tree_type_from_size(bytes_needed,
15368 sourceref.field->attr
15369 & signable_e) ;
15370 tree source = gg_define_variable(source_type);
15371
15372 if( sourceref.field->type == FldFloat )
15373 {
15374 get_binary_value_from_float(source,
15375 destref,
15376 sourceref.field,
15377 tsource.offset);
15378
15379 // Get binary value from float actually scales the source value to the
15380 // dest:: rdigits
15381 copy_little_endian_into_place(destref.field,
15382 refer_offset(destref),
15383 source,
15384 destref.field->data.rdigits,
15385 check_for_error,
15386 size_error);
15387 moved = true;
15388 }
15389 else
15390 {
15391 get_binary_value( source,
15392 NULL,
15393 sourceref.field,
15394 tsource.offset);
15395 copy_little_endian_into_place(destref.field,
15396 refer_offset(destref),
15397 source,
15398 sourceref.field->data.rdigits,
15399 check_for_error,
15400 size_error);
15401 moved = true;
15402 }
15403 }
15404 return moved;
15405 }
15406
15407 static bool
15408 mh_source_is_group( const cbl_refer_t &destref,
15409 const cbl_refer_t &sourceref,
15410 const TREEPLET &tsrc)
15411 {
15412 bool retval = false;
15413 if( sourceref.field->type == FldGroup && !(destref.field->attr & rjust_e) )
15414 {
15415 Analyze();
15416 // We are moving a group to a something. The rule here is just move as
15417 // many bytes as you can, and, if necessary, fill with spaces
15418 tree tdest = gg_add( member(destref.field->var_decl_node, "data"),
15419 refer_offset(destref));
15420 tree tsource = gg_add( member(sourceref.field->var_decl_node, "data"),
15421 tsrc.offset);
15422 tree dbytes = refer_size_dest(destref);
15423 tree sbytes = tsrc.length;
15424
15425 IF( sbytes, ge_op, gg_cast(TREE_TYPE(sbytes), dbytes) )
15426 {
15427 // There are too many source bytes
15428 gg_memcpy(tdest, tsource, dbytes);
15429 }
15430 ELSE
15431 {
15432 // There are too-few source bytes:
15433 gg_memset(tdest, build_int_cst_type(INT, internal_space), dbytes);
15434 gg_memcpy(tdest, tsource, sbytes);
15435 }
15436 ENDIF
15437 retval = true;
15438 }
15439 return retval;
15440 }
15441
15442 static void
15443 move_helper(tree size_error, // This is an INT
15444 cbl_refer_t destref,
15445 cbl_refer_t sourceref, // Call move_helper with this resolved.
15446 TREEPLET &tsource,
15447 cbl_round_t rounded,
15448 bool check_for_error, // True means our called wants to know about truncation errors
15449 bool restore_on_error
15450 )
15451 {
15452 Analyze();
15453 SHOW_PARSE1
15454 {
15455 SHOW_PARSE_INDENT
15456 SHOW_PARSE_TEXT("move_helper()");
15457 }
15458
15459 bool moved = false;
15460
15461 if( size_error )
15462 {
15463 gg_assign(size_error, integer_zero_node);
15464 }
15465
15466 static tree stash = gg_define_variable(UCHAR_P, "..mh_stash", vs_file_static);
15467
15468 tree st_data = NULL_TREE;
15469 tree st_size = NULL_TREE;
15470
15471 if( restore_on_error )
15472 {
15473 // We are creating a copy of the original destination in case we clobber it
15474 // and have to restore it because of a computational error.
15475 static bool first_time = true;
15476 static size_t stash_size = 1024;
15477 if( first_time )
15478 {
15479 first_time = false;
15480 gg_assign(stash, gg_cast(UCHAR_P, gg_malloc(stash_size)));
15481 }
15482 if( stash_size < destref.field->data.capacity )
15483 {
15484 stash_size = destref.field->data.capacity;
15485 gg_assign(stash, gg_cast(UCHAR_P, gg_realloc(stash, stash_size)));
15486 }
15487 st_data = qualified_data_location(destref);
15488 st_size = refer_size_dest(destref);
15489 gg_memcpy(stash,
15490 st_data,
15491 st_size);
15492 }
15493
15494 if( (sourceref.field->attr & (linkage_e | based_e))
15495 || ( destref.field->attr & (linkage_e | based_e)) )
15496 {
15497 //goto dont_be_clever; this will go through to the default.
15498 }
15499
15500 // if( !moved ) // commented out to quiet cppcheck
15501 {
15502 moved = mh_source_is_group(destref, sourceref, tsource);
15503 }
15504
15505 if( !moved )
15506 {
15507 moved = mh_identical(destref, sourceref, tsource);
15508 }
15509
15510 if( !moved )
15511 {
15512 moved = mh_source_is_literalN(destref,
15513 sourceref,
15514 check_for_error,
15515 rounded,
15516 size_error);
15517 }
15518
15519 if( !moved )
15520 {
15521 moved = mh_dest_is_float( destref,
15522 sourceref,
15523 tsource,
15524 rounded,
15525 size_error);
15526 }
15527
15528 if( !moved && rounded == truncation_e )
15529 {
15530 moved = mh_numeric_display( destref,
15531 sourceref,
15532 tsource,
15533 size_error);
15534 }
15535
15536 if( !moved )
15537 {
15538 moved = mh_little_endian( destref,
15539 sourceref,
15540 tsource,
15541 restore_on_error,
15542 size_error);
15543 }
15544
15545 if( !moved && sourceref.field->type == FldLiteralA)
15546 {
15547 SHOW_PARSE1
15548 {
15549 SHOW_PARSE_INDENT
15550 SHOW_PARSE_TEXT("__gg__move_literala")
15551 }
15552
15553 cbl_figconst_t figconst = cbl_figconst_of( sourceref.field->data.initial);
15554
15555 if( destref.refmod.from
15556 || destref.refmod.len )
15557 {
15558 // Let the move routine know to treat the destination as alphanumeric
15559 gg_attribute_bit_set(destref.field, refmod_e);
15560 }
15561
15562 static char *buffer = NULL;
15563 static size_t buffer_size = 0;
15564 size_t source_length = sourceref.field->data.capacity;
15565
15566 if( buffer_size < source_length )
15567 {
15568 buffer_size = source_length;
15569 buffer = static_cast<char *>(xrealloc(buffer, buffer_size));
15570 }
15571 gcc_assert(buffer);
15572
15573 if( figconst )
15574 {
15575 char const_char = 0x7F; // Head off a compiler warning about
15576 // // uninitialized variables
15577 switch(figconst)
15578 {
15579 case normal_value_e :
15580 // This is not possible, it says here in the fine print.
15581 abort();
15582 break;
15583 case low_value_e :
15584 const_char = ascii_to_internal(__gg__low_value_character);
15585 break;
15586 case zero_value_e :
15587 const_char = internal_zero;
15588 break;
15589 case space_value_e :
15590 const_char = internal_space;
15591 break;
15592 case quote_value_e :
15593 const_char = ascii_to_internal(__gg__quote_character);
15594 break;
15595 case high_value_e :
15596 const_char = ascii_to_internal(__gg__high_value_character);
15597 break;
15598 case null_value_e:
15599 const_char = 0x00;
15600 break;
15601 }
15602 memset(buffer, const_char, source_length);
15603 }
15604 else
15605 {
15606 memset( buffer, ascii_space, source_length);
15607 memcpy( buffer,
15608 sourceref.field->data.initial,
15609 std::min(source_length, (size_t)sourceref.field->data.capacity) );
15610 for( size_t i=0; i<source_length; i++)
15611 {
15612 buffer[i] = ascii_to_internal(buffer[i]);
15613 }
15614 }
15615
15616 int rounded_parameter = rounded
15617 | ((sourceref.all || figconst ) ? REFER_ALL_BIT : 0);
15618
15619 if( size_error )
15620 {
15621 gg_assign(size_error,
15622 gg_call_expr( INT,
15623 "__gg__move_literala",
15624 gg_get_address_of(destref.field->var_decl_node),
15625 refer_offset(destref),
15626 refer_size_dest(destref),
15627 build_int_cst_type(INT, rounded_parameter),
15628 build_string_literal(source_length,
15629 buffer),
15630 build_int_cst_type( SIZE_T, source_length),
15631 NULL_TREE));
15632 }
15633 else
15634 {
15635 gg_call ( INT,
15636 "__gg__move_literala",
15637 gg_get_address_of(destref.field->var_decl_node),
15638 refer_offset(destref),
15639 refer_size_dest(destref),
15640 build_int_cst_type(INT, rounded_parameter),
15641 build_string_literal(source_length,
15642 buffer),
15643 build_int_cst_type( SIZE_T, source_length),
15644 NULL_TREE);
15645 }
15646 if( destref.refmod.from
15647 || destref.refmod.len )
15648 {
15649 // Return that value to its original form
15650 gg_attribute_bit_clear(destref.field, refmod_e);
15651 }
15652 moved = true;
15653 }
15654
15655 if( !moved )
15656 {
15657 SHOW_PARSE1
15658 {
15659 SHOW_PARSE_INDENT
15660 SHOW_PARSE_TEXT("default __gg__move")
15661 }
15662
15663 if( destref.refmod.from
15664 || destref.refmod.len
15665 || sourceref.refmod.from
15666 || sourceref.refmod.len )
15667 {
15668 // Let the move routine know to treat the destination as alphanumeric
15669 gg_attribute_bit_set(destref.field, refmod_e);
15670 }
15671
15672 int nflags = (sourceref.all ? REFER_T_MOVE_ALL : 0)
15673 + (sourceref.addr_of ? REFER_T_ADDRESS_OF : 0);
15674
15675 if( size_error )
15676 {
15677 gg_assign(size_error,
15678 gg_call_expr( INT,
15679 "__gg__move",
15680 gg_get_address_of(destref.field->var_decl_node),
15681 refer_offset(destref),
15682 refer_size_dest(destref),
15683 tsource.pfield,
15684 tsource.offset,
15685 tsource.length,
15686 build_int_cst_type(INT, nflags),
15687 build_int_cst_type(INT, rounded),
15688 NULL_TREE));
15689 }
15690 else
15691 {
15692 gg_call ( INT,
15693 "__gg__move",
15694 gg_get_address_of(destref.field->var_decl_node),
15695 refer_offset(destref),
15696 refer_size_dest(destref),
15697 tsource.pfield,
15698 tsource.offset,
15699 tsource.length,
15700 build_int_cst_type(INT, nflags),
15701 build_int_cst_type(INT, rounded),
15702 NULL_TREE);
15703
15704 }
15705 if( destref.refmod.from
15706 || destref.refmod.len
15707 || sourceref.refmod.from
15708 || sourceref.refmod.len )
15709 {
15710 // Return that value to its original form
15711 gg_attribute_bit_clear(destref.field, refmod_e);
15712 }
15713
15714 // moved = true; // commented out to quiet cppcheck
15715 }
15716
15717 if( restore_on_error )
15718 {
15719 IF(size_error, ne_op, integer_zero_node)
15720 {
15721 gg_memcpy(st_data,
15722 stash,
15723 st_size);
15724 }
15725 ELSE
15726 ENDIF
15727 }
15728 else
15729 {
15730 if( check_for_error )
15731 {
15732 IF(size_error, ne_op, integer_zero_node)
15733 {
15734 // We had a size error, but there was no restore_on_error. Pointer
15735 // Let our lord and master know there was a truncation:
15736 set_exception_code(ec_size_truncation_e);
15737 }
15738 ELSE
15739 ENDIF
15740 }
15741 }
15742
15743 SHOW_PARSE1
15744 {
15745 SHOW_PARSE_END
15746 }
15747 }
15748
15749 tree parser_cast_long(tree N)
15750 {
15751 return gg_cast(LONG, N);
15752 }
15753
15754 void
15755 parser_print_long(tree N)
15756 {
15757 gg_printf("%ld", N, NULL_TREE);
15758 }
15759
15760 void
15761 parser_print_long(const char *fmt, tree N)
15762 {
15763 // fmt should have a %ld/%lx in it
15764 gg_printf(fmt, N, NULL_TREE);
15765 }
15766
15767 void
15768 parser_print_long(long N)
15769 {
15770 gg_printf("%ld", build_int_cst_type(LONG, N), NULL_TREE);
15771 }
15772
15773 void
15774 parser_print_long(const char *fmt, long N)
15775 {
15776 // fmt should have a %ld/%lx in it
15777 gg_printf(fmt, build_int_cst_type(LONG, N), NULL_TREE);
15778 }
15779
15780 void
15781 parser_print_string(const char *ach)
15782 {
15783 gg_printf("%s", gg_string_literal(ach), NULL_TREE);
15784 }
15785
15786 void
15787 parser_print_string(const char *fmt, const char *ach)
15788 {
15789 // fmt should have a %s in it
15790 gg_printf(fmt, gg_string_literal(ach), NULL_TREE);
15791 }
15792
15793 REAL_VALUE_TYPE
15794 real_powi10 (uint32_t x)
15795 {
15796 REAL_VALUE_TYPE ten, pow10;
15797 real_from_integer (&ten, TYPE_MODE (FLOAT128), 10, SIGNED);
15798 real_powi (&pow10, TYPE_MODE (FLOAT128), &ten, x);
15799 return pow10;
15800 }
15801
15802 char *
15803 binary_initial_from_float128(cbl_field_t *field, int rdigits,
15804 REAL_VALUE_TYPE value)
15805 {
15806 // This routine returns an xmalloced buffer designed to replace the
15807 // data.initial member of the incoming field
15808 char *retval = NULL;
15809
15810 // We need to adjust value so that it has no decimal places
15811 if( rdigits )
15812 {
15813 REAL_VALUE_TYPE pow10 = real_powi10 (rdigits);
15814 real_arithmetic (&value, MULT_EXPR, &value, &pow10);
15815 real_convert (&value, TYPE_MODE (float128_type_node), &value);
15816 }
15817 // We need to make sure that the resulting string will fit into
15818 // a number with 'digits' digits
15819
15820 // Keep in mind that pure binary types, like BINARY-CHAR, have no digits
15821 if( field->data.digits )
15822 {
15823 REAL_VALUE_TYPE pow10 = real_powi10 (field->data.digits);
15824 mpfr_t m0, m1;
15825
15826 mpfr_inits2 (REAL_MODE_FORMAT (TYPE_MODE (float128_type_node))->p,
15827 m0, m1, NULL);
15828 mpfr_from_real (m0, &value, MPFR_RNDN);
15829 mpfr_from_real (m1, &pow10, MPFR_RNDN);
15830 mpfr_clear_flags ();
15831 mpfr_fmod (m0, m0, m1, MPFR_RNDN);
15832 real_from_mpfr (&value, m0,
15833 REAL_MODE_FORMAT (TYPE_MODE (float128_type_node)),
15834 MPFR_RNDN);
15835 real_convert (&value, TYPE_MODE (float128_type_node), &value);
15836 mpfr_clears (m0, m1, NULL);
15837 }
15838
15839 real_roundeven (&value, TYPE_MODE (float128_type_node), &value);
15840
15841 bool fail = false;
15842 FIXED_WIDE_INT(128) i
15843 = FIXED_WIDE_INT(128)::from (real_to_integer (&value, &fail, 128), SIGNED);
15844
15845 retval = static_cast<char *>(xmalloc(field->data.capacity));
15846 gcc_assert(retval);
15847 switch(field->data.capacity)
15848 {
15849 tree type;
15850 case 1:
15851 case 2:
15852 case 4:
15853 case 8:
15854 case 16:
15855 type = build_nonstandard_integer_type ( field->data.capacity
15856 * BITS_PER_UNIT, 0);
15857 native_encode_wide_int (type, i, PTRCAST(unsigned char, retval),
15858 field->data.capacity);
15859 break;
15860 default:
15861 fprintf(stderr,
15862 "Trouble in binary_initial_from_float128 at %s() %s:%d\n",
15863 __func__,
15864 __FILE__,
15865 __LINE__);
15866 abort();
15867 break;
15868 }
15869
15870 return retval;
15871 }
15872
15873
15874 static void
15875 digits_from_float128(char *retval, cbl_field_t *field, size_t width, int rdigits, REAL_VALUE_TYPE value)
15876 {
15877 char ach[128];
15878
15879 // We need to adjust value so that it has no decimal places
15880 if( rdigits )
15881 {
15882 REAL_VALUE_TYPE pow10 = real_powi10 (rdigits);
15883 real_arithmetic (&value, MULT_EXPR, &value, &pow10);
15884 }
15885 // We need to make sure that the resulting string will fit into
15886 // a number with 'digits' digits
15887 REAL_VALUE_TYPE pow10 = real_powi10 (field->data.digits);
15888 mpfr_t m0, m1;
15889
15890 mpfr_inits2 (FLOAT_MODE_FORMAT (TYPE_MODE (float128_type_node))->p, m0, m1,
15891 NULL);
15892 mpfr_from_real (m0, &value, MPFR_RNDN);
15893 mpfr_from_real (m1, &pow10, MPFR_RNDN);
15894 mpfr_clear_flags ();
15895 mpfr_fmod (m0, m0, m1, MPFR_RNDN);
15896 real_from_mpfr (&value, m0,
15897 REAL_MODE_FORMAT (TYPE_MODE (float128_type_node)),
15898 MPFR_RNDN);
15899 real_convert (&value, TYPE_MODE (float128_type_node), &value);
15900 mpfr_clears (m0, m1, NULL);
15901 real_roundeven (&value, TYPE_MODE (float128_type_node), &value);
15902
15903 bool fail = false;
15904 FIXED_WIDE_INT(128) i
15905 = FIXED_WIDE_INT(128)::from (real_to_integer (&value, &fail, 128), SIGNED);
15906
15907 // We convert it to a integer string of digits:
15908 print_dec (i, ach, SIGNED);
15909
15910 //fprintf(stderr, "digits_from_float128() %s %f %s ", field->name, (double)value, ach);
15911
15912 gcc_assert( strlen(ach) <= field->data.digits );
15913 if( strlen(ach) < width )
15914 {
15915 memset(retval, '0', width-strlen(ach) );
15916 }
15917 strcpy(retval + (width-strlen(ach)), ach);
15918 }
15919
15920 static char *
15921 initial_from_initial(cbl_field_t *field)
15922 {
15923 Analyze();
15924 // This routine returns an xmalloced buffer that is intended to replace the
15925 // data.initial member of the incoming field.
15926
15927 //fprintf(stderr, "initial_from_initial %s\n", field->name);
15928
15929 char *retval = NULL;
15930 int rdigits;
15931
15932 // Let's handle the possibility of a figurative constant
15933 cbl_figconst_t figconst = cbl_figconst_of( field->data.initial);
15934 //cbl_figconst_t figconst = (cbl_figconst_t)(field->attr & FIGCONST_MASK);
15935 if( figconst )
15936 {
15937 int const_char = 0xFF; // Head off a compiler warning about uninitialized
15938 // // variables
15939 switch(figconst)
15940 {
15941 case normal_value_e :
15942 // This really should never happen because normal_value_e is zero
15943 abort();
15944 break;
15945 case low_value_e :
15946 const_char = ascii_to_internal(__gg__low_value_character);
15947 break;
15948 case zero_value_e :
15949 const_char = internal_zero;
15950 break;
15951 case space_value_e :
15952 const_char = internal_space;
15953 break;
15954 case quote_value_e :
15955 const_char = ascii_to_internal(__gg__quote_character);
15956 break;
15957 case high_value_e :
15958 if( __gg__high_value_character == DEGENERATE_HIGH_VALUE )
15959 {
15960 const_char = __gg__high_value_character;
15961 }
15962 else
15963 {
15964 const_char = ascii_to_internal(__gg__high_value_character);
15965 }
15966 break;
15967 case null_value_e:
15968 break;
15969 }
15970 bool set_return = figconst != zero_value_e;
15971 if( !set_return )
15972 {
15973 // The figconst is zero
15974 switch(field->type)
15975 {
15976 case FldGroup:
15977 case FldAlphanumeric:
15978 set_return = true;
15979 break;
15980
15981 default:
15982 break;
15983 }
15984 }
15985 if( set_return )
15986 {
15987 retval = static_cast<char *>(xmalloc(field->data.capacity+1));
15988 gcc_assert(retval);
15989 memset(retval, const_char, field->data.capacity);
15990 retval[field->data.capacity] = '\0';
15991 return retval;
15992 }
15993 }
15994
15995 // ??? Refactoring the cases below that do not need 'value' would
15996 // make this less ugly
15997 REAL_VALUE_TYPE value;
15998 if( field->data.etc_type == cbl_field_data_t::value_e )
15999 value = TREE_REAL_CST (field->data.value_of ());
16000
16001 // There is always the infuriating possibility of a P-scaled number
16002 if( field->attr & scaled_e )
16003 {
16004 rdigits = 0;
16005 if( field->data.rdigits >= 0 )
16006 {
16007 // Suppose our PIC is PPPPPP999, meaning that field->digits
16008 // is 3, and field->rdigits is 6.
16009
16010 // Our result has no decimal places, and we have to multiply the value
16011 // by 10**9 to get the significant bdigits where they belong.
16012
16013 REAL_VALUE_TYPE pow10
16014 = real_powi10 (field->data.digits + field->data.rdigits);
16015 real_arithmetic (&value, MULT_EXPR, &value, &pow10);
16016 }
16017 else
16018 {
16019 // Suppose our target is 999PPPPPP, so there is a ->digits
16020 // of 3 and field->rdigits of -6.
16021
16022 // If our caller gave us 123000000, we need to divide
16023 // it by 1000000 to line up the 123 with where we want it to go:
16024
16025 REAL_VALUE_TYPE pow10 = real_powi10 (-field->data.rdigits);
16026 real_arithmetic (&value, RDIV_EXPR, &value, &pow10);
16027 }
16028 // Either way, we now have everything aligned for the remainder of the
16029 // processing to work:
16030 }
16031 else
16032 {
16033 // Not P-scaled
16034 rdigits = field->data.rdigits;
16035 }
16036
16037 switch(field->type)
16038 {
16039 case FldNumericBin5:
16040 case FldIndex:
16041 retval = binary_initial_from_float128(field, rdigits, value);
16042 break;
16043
16044 case FldNumericBinary:
16045 {
16046 retval = binary_initial_from_float128(field, rdigits, value);
16047 size_t left = 0;
16048 size_t right = field->data.capacity - 1;
16049 while(left < right)
16050 {
16051 std::swap(retval[left++], retval[right--]);
16052 }
16053 break;
16054 }
16055
16056 case FldNumericDisplay:
16057 {
16058 retval = static_cast<char *>(xmalloc(field->data.capacity));
16059 gcc_assert(retval);
16060 char *pretval = retval;
16061 char ach[128];
16062
16063 bool negative;
16064 if( real_isneg (&value) )
16065 {
16066 negative = true;
16067 value = real_value_negate (&value);
16068 }
16069 else
16070 {
16071 negative = false;
16072 }
16073
16074 digits_from_float128(ach, field, field->data.digits, rdigits, value);
16075
16076 const char *digits = ach;
16077 if( (field->attr & signable_e)
16078 && (field->attr & separate_e)
16079 && (field->attr & leading_e ) )
16080 {
16081 if( negative )
16082 {
16083 *pretval++ = internal_minus;
16084 }
16085 else
16086 {
16087 *pretval++ = internal_plus;
16088 }
16089 }
16090 for(size_t i=0; i<field->data.digits; i++)
16091 {
16092 *pretval++ = internal_zero + ((*digits++) & 0x0F);
16093 }
16094 if( (field->attr & signable_e)
16095 && (field->attr & separate_e)
16096 && !(field->attr & leading_e ) )
16097 {
16098 if( negative )
16099 {
16100 *pretval++ = internal_minus;
16101 }
16102 else
16103 {
16104 *pretval++ = internal_plus;
16105 }
16106 }
16107 if( (field->attr & signable_e)
16108 && !(field->attr & separate_e)
16109 && negative)
16110 {
16111 if( field->attr & leading_e )
16112 {
16113 if( internal_is_ebcdic )
16114 {
16115 retval[0] &= ~NUMERIC_DISPLAY_SIGN_BIT;
16116 }
16117 else
16118 {
16119 retval[0] |= NUMERIC_DISPLAY_SIGN_BIT;
16120 }
16121 }
16122 else
16123 {
16124 if( internal_is_ebcdic )
16125 {
16126 pretval[-1] &= ~NUMERIC_DISPLAY_SIGN_BIT;
16127 }
16128 else
16129 {
16130 pretval[-1] |= NUMERIC_DISPLAY_SIGN_BIT;
16131 }
16132 }
16133 }
16134 break;
16135 }
16136
16137 case FldPacked:
16138 {
16139 retval = static_cast<char *>(xmalloc(field->data.capacity));
16140 gcc_assert(retval);
16141 char *pretval = retval;
16142 char ach[128];
16143
16144 bool negative;
16145 if( real_isneg (&value) )
16146 {
16147 negative = true;
16148 value = real_value_negate (&value);
16149 }
16150 else
16151 {
16152 negative = false;
16153 }
16154
16155 // For COMP-6 (flagged by separate_e), the number of required digits is
16156 // twice the capacity.
16157
16158 // For COMP-3, the number of digits is 2*capacity minus 1, because the
16159 // the final "digit" is a sign nybble.
16160
16161 size_t ndigits = (field->attr & separate_e)
16162 ? field->data.capacity * 2
16163 : field->data.capacity * 2 - 1;
16164 digits_from_float128(ach, field, ndigits, rdigits, value);
16165
16166 const char *digits = ach;
16167 for(size_t i=0; i<ndigits; i++)
16168 {
16169 if( !(i & 0x01) )
16170 {
16171 *pretval = ((*digits++) & 0x0F)<<4;;
16172 }
16173 else
16174 {
16175 *pretval++ += (*digits++) & 0x0F;
16176 }
16177 }
16178 if( !(field->attr & separate_e) )
16179 {
16180 // This is COMP-3, so put in a sign nybble
16181 if( (field->attr & signable_e) )
16182 {
16183 if( negative )
16184 {
16185 *pretval++ += 0x0D; // Means signable and negative
16186 }
16187 else
16188 {
16189 *pretval++ += 0x0C; // Means signable and non-negative
16190 }
16191 }
16192 else
16193 {
16194 *pretval++ += 0x0F; // Means not signable
16195 }
16196 }
16197 break;
16198 }
16199
16200 case FldGroup:
16201 case FldAlphanumeric:
16202 case FldLiteralA:
16203 case FldAlphaEdited:
16204 {
16205 if( field->data.initial )
16206 {
16207 retval = static_cast<char *>(xmalloc(field->data.capacity+1));
16208 gcc_assert(retval);
16209 if( field->attr & hex_encoded_e)
16210 {
16211 memcpy(retval, field->data.initial, field->data.capacity);
16212 }
16213 else
16214 {
16215 size_t buffer_size = 0;
16216 size_t length = field->data.capacity;
16217 memset(retval, internal_space, length);
16218 raw_to_internal(&retval, &buffer_size, field->data.initial, length);
16219 if( strlen(field->data.initial) < length )
16220 {
16221 // If this is true, then the initial string must've been Z'xyz'
16222 retval[strlen(field->data.initial)] = '\0';
16223 }
16224 }
16225 retval[field->data.capacity] = '\0';
16226 }
16227 break;
16228 }
16229
16230 case FldNumericEdited:
16231 {
16232 retval = static_cast<char *>(xmalloc(field->data.capacity+1));
16233 gcc_assert(retval);
16234 if( field->data.initial && field->attr & quoted_e )
16235 {
16236 // What the programmer says the value is, the value becomes, no
16237 // matter how wrong it might be.
16238 size_t length = std::min( (size_t)field->data.capacity,
16239 strlen(field->data.initial));
16240 for(size_t i=0; i<length; i++)
16241 {
16242 retval[i] = ascii_to_internal(field->data.initial[i]);
16243 }
16244 if( length < (size_t)field->data.capacity )
16245 {
16246 memset( retval+length,
16247 internal_space,
16248 (size_t)field->data.capacity - length);
16249 }
16250 }
16251 else
16252 {
16253 // It's not a quoted string, so we use data.value:
16254 bool negative;
16255 if( real_isneg (&value) )
16256 {
16257 negative = true;
16258 value = real_value_negate (&value);
16259 }
16260 else
16261 {
16262 negative = false;
16263 }
16264
16265 char ach[128];
16266 memset(ach, 0, sizeof(ach));
16267 memset(retval, 0, field->data.capacity);
16268
16269 if( (field->attr & blank_zero_e) && real_iszero (&value) )
16270 {
16271 memset(retval, internal_space, field->data.capacity);
16272 }
16273 else
16274 {
16275 size_t ndigits = field->data.capacity;
16276 digits_from_float128(ach, field, ndigits, rdigits, value);
16277 /* ??? This resides in libgcobol valconv.cc. */
16278 __gg__string_to_numeric_edited( retval,
16279 ach,
16280 field->data.rdigits,
16281 negative,
16282 field->data.picture);
16283 }
16284 }
16285 break;
16286 }
16287
16288 case FldFloat:
16289 {
16290 retval = static_cast<char *>(xmalloc(field->data.capacity));
16291 gcc_assert(retval);
16292 switch( field->data.capacity )
16293 {
16294 case 4:
16295 value = real_value_truncate (TYPE_MODE (FLOAT), value);
16296 native_encode_real (SCALAR_FLOAT_TYPE_MODE (FLOAT), &value,
16297 PTRCAST(unsigned char, retval), 4, 0);
16298 break;
16299 case 8:
16300 value = real_value_truncate (TYPE_MODE (DOUBLE), value);
16301 native_encode_real (SCALAR_FLOAT_TYPE_MODE (DOUBLE), &value,
16302 PTRCAST(unsigned char, retval), 8, 0);
16303 break;
16304 case 16:
16305 value = real_value_truncate (TYPE_MODE (FLOAT128), value);
16306 native_encode_real (SCALAR_FLOAT_TYPE_MODE (FLOAT128), &value,
16307 PTRCAST(unsigned char, retval), 16, 0);
16308 break;
16309 }
16310 break;
16311 }
16312
16313 case FldLiteralN:
16314 {
16315 break;
16316 }
16317
16318 default:
16319 break;
16320 }
16321 return retval;
16322 }
16323
16324 static void
16325 actually_create_the_static_field( cbl_field_t *new_var,
16326 tree data_area,
16327 size_t length_of_initial_string,
16328 const char *new_initial,
16329 tree immediate_parent,
16330 tree new_var_decl)
16331 {
16332 tree constr = make_node(CONSTRUCTOR);
16333 TREE_TYPE(constr) = cblc_field_type_node;
16334 TREE_STATIC(constr) = 1;
16335 TREE_CONSTANT(constr) = 1;
16336
16337 tree next_field = TYPE_FIELDS(cblc_field_type_node);
16338 // We are going to create the constructors by walking the linked
16339 // list of FIELD_DECLs. We must do it in the same order as the
16340 // structure creation code in create_cblc_field_t()
16341
16342 // UCHAR_P, "data",
16343 CONSTRUCTOR_APPEND_ELT( CONSTRUCTOR_ELTS(constr),
16344 next_field,
16345 data_area );
16346 next_field = TREE_CHAIN(next_field);
16347
16348 // SIZE_T, "capacity",
16349 CONSTRUCTOR_APPEND_ELT( CONSTRUCTOR_ELTS(constr),
16350 next_field,
16351 build_int_cst_type( SIZE_T,
16352 new_var->data.capacity) );
16353 next_field = TREE_CHAIN(next_field);
16354
16355 // SIZE_T, "allocated",
16356 if( data_area != null_pointer_node )
16357 {
16358 CONSTRUCTOR_APPEND_ELT( CONSTRUCTOR_ELTS(constr),
16359 next_field,
16360 build_int_cst_type( SIZE_T,
16361 new_var->data.capacity) );
16362 }
16363 else
16364 {
16365 CONSTRUCTOR_APPEND_ELT( CONSTRUCTOR_ELTS(constr),
16366 next_field,
16367 build_int_cst_type( SIZE_T,
16368 0) );
16369 }
16370
16371 next_field = TREE_CHAIN(next_field);
16372
16373 // SIZE_T, "offset",
16374
16375 if( new_var->type == FldAlphanumeric &&
16376 new_var->attr & intermediate_e )
16377 {
16378 // This is in support of FUNCTION TRIM. That function can make the capacity
16379 // of the intermediate target smaller so that TRIM("abc ") returns
16380 // "abc". By putting the capacity here for such variables, we have a
16381 // mechanism for restoring it the capacity to the original.
16382 CONSTRUCTOR_APPEND_ELT( CONSTRUCTOR_ELTS(constr),
16383 next_field,
16384 build_int_cst_type(SIZE_T, new_var->data.capacity));
16385 }
16386 else
16387 {
16388 CONSTRUCTOR_APPEND_ELT( CONSTRUCTOR_ELTS(constr),
16389 next_field,
16390 build_int_cst_type(SIZE_T, new_var->offset) );
16391 }
16392
16393 next_field = TREE_CHAIN(next_field);
16394
16395 // CHAR_P, "name",
16396 CONSTRUCTOR_APPEND_ELT( CONSTRUCTOR_ELTS(constr),
16397 next_field,
16398 gg_string_literal(new_var->name) );
16399 next_field = TREE_CHAIN(next_field);
16400
16401 // CHAR_P, "picture",
16402 CONSTRUCTOR_APPEND_ELT( CONSTRUCTOR_ELTS(constr),
16403 next_field,
16404 gg_string_literal(new_var->data.picture) );
16405 next_field = TREE_CHAIN(next_field);
16406
16407 // CHAR_P, "initial",
16408 if( length_of_initial_string == 0 )
16409 {
16410 CONSTRUCTOR_APPEND_ELT( CONSTRUCTOR_ELTS(constr),
16411 next_field,
16412 null_pointer_node );
16413 }
16414 else
16415 {
16416 CONSTRUCTOR_APPEND_ELT( CONSTRUCTOR_ELTS(constr),
16417 next_field,
16418 build_string_literal(length_of_initial_string, new_initial) );
16419 }
16420 next_field = TREE_CHAIN(next_field);
16421
16422 // CHAR_P, "parent",
16423 CONSTRUCTOR_APPEND_ELT( CONSTRUCTOR_ELTS(constr),
16424 next_field,
16425 immediate_parent ? gg_get_address_of(immediate_parent) : null_pointer_node );
16426 next_field = TREE_CHAIN(next_field);
16427
16428 // SIZE_T, "occurs_lower",
16429 CONSTRUCTOR_APPEND_ELT( CONSTRUCTOR_ELTS(constr),
16430 next_field,
16431 build_int_cst_type(SIZE_T, new_var->occurs.bounds.lower) );
16432 next_field = TREE_CHAIN(next_field);
16433
16434 // SIZE_T, "occurs_upper");
16435 CONSTRUCTOR_APPEND_ELT( CONSTRUCTOR_ELTS(constr),
16436 next_field,
16437 build_int_cst_type(SIZE_T, new_var->occurs.bounds.upper) );
16438 next_field = TREE_CHAIN(next_field);
16439
16440 // SIZE_T, "attr",
16441 CONSTRUCTOR_APPEND_ELT( CONSTRUCTOR_ELTS(constr),
16442 next_field,
16443 build_int_cst_type(SIZE_T, new_var->attr) );
16444 next_field = TREE_CHAIN(next_field);
16445
16446 // SCHAR, "type",
16447 CONSTRUCTOR_APPEND_ELT( CONSTRUCTOR_ELTS(constr),
16448 next_field,
16449 build_int_cst_type(SCHAR, new_var->type) );
16450 next_field = TREE_CHAIN(next_field);
16451
16452 // SCHAR, "level",
16453 CONSTRUCTOR_APPEND_ELT( CONSTRUCTOR_ELTS(constr),
16454 next_field,
16455 build_int_cst_type(SCHAR, new_var->level) );
16456 next_field = TREE_CHAIN(next_field);
16457
16458 // SCHAR, "digits",
16459 CONSTRUCTOR_APPEND_ELT( CONSTRUCTOR_ELTS(constr),
16460 next_field,
16461 build_int_cst_type(SCHAR, new_var->data.digits) );
16462 next_field = TREE_CHAIN(next_field);
16463
16464 // SCHAR, "rdigits",
16465 CONSTRUCTOR_APPEND_ELT( CONSTRUCTOR_ELTS(constr),
16466 next_field,
16467 build_int_cst_type(SCHAR, new_var->data.rdigits) );
16468 next_field = TREE_CHAIN(next_field);
16469
16470 DECL_INITIAL(new_var_decl) = constr;
16471 }
16472
16473 static void
16474 psa_global(cbl_field_t *new_var)
16475 {
16476 char *mname = cobol_name_mangler(new_var->name);
16477 char ach[2*sizeof(cbl_name_t)];
16478 sprintf(ach, "__gg__%s", mname);
16479 free(mname);
16480
16481 if( strcmp(new_var->name, "_VERY_TRUE") == 0 )
16482 {
16483 new_var->var_decl_node = boolean_true_node;
16484 return;
16485 }
16486 if( strcmp(new_var->name, "_VERY_FALSE") == 0 )
16487 {
16488 new_var->var_decl_node = boolean_false_node;
16489 return;
16490 }
16491
16492 // global variables already have a cblc_field_t defined in constants.cc
16493
16494 strcpy(ach, "__gg__");
16495 strcat(ach, new_var->name);
16496 for(size_t i=0; i<strlen(ach); i++)
16497 {
16498 ach[i] = _tolower(ach[i]);
16499 if(ach[i] == '-')
16500 {
16501 ach[i] = '_';
16502 }
16503 }
16504
16505 if( strcmp(new_var->name, "RETURN-CODE") == 0 )
16506 {
16507 strcpy(ach, "__gg__return_code");
16508 }
16509
16510 if( strcmp(new_var->name, "UPSI-0") == 0 )
16511 {
16512 strcpy(ach, "__gg__upsi");
16513 }
16514
16515 new_var->var_decl_node = gg_declare_variable(cblc_field_type_node, ach, NULL, vs_external_reference);
16516
16517 // global variables already have a .data area defined. We can find that
16518 // variable from the new_var->name. It's lower-case, with hyphens
16519 // converted to underscores
16520 strcpy(ach, "__gg__data_");
16521 strcat(ach, new_var->name);
16522 for(size_t i=0; i<strlen(ach); i++)
16523 {
16524 ach[i] = _tolower(ach[i]);
16525 if(ach[i] == '-')
16526 {
16527 ach[i] = '_';
16528 }
16529 }
16530 new_var->data_decl_node = gg_declare_variable(UCHAR, ach, NULL, vs_external_reference);
16531 }
16532
16533 static tree
16534 psa_new_var_decl(cbl_field_t *new_var, const char *external_record_base)
16535 {
16536 // This routine creates the VAR_DECL for the cblc_field_t that we are about
16537 // to statically create.
16538 tree new_var_decl;
16539
16540 if( *external_record_base )
16541 {
16542 char ach[257];
16543 strcpy(ach, "_");
16544 strcat(ach, external_record_base);
16545 strcat(ach, "_ra"); // For "Record Area"
16546 new_var_decl = gg_define_variable( cblc_field_type_node,
16547 ach,
16548 vs_external);
16549 SET_DECL_MODE(new_var_decl, BLKmode);
16550 }
16551 else
16552 {
16553 size_t our_index = new_var->our_index;
16554
16555 // During the early stages of implementing cbl_field_t::our_index, there
16556 // were execution paths in parse.y and parser.cc that resulted in our_index
16557 // not being set. I hereby try to use field_index() to find the index
16558 // of this field to resolve those. I note that field_index does a linear
16559 // search of the symbols[] table to find that index. That's why I don't
16560 // use it routinely; it results in O(N^squared) computational complexity
16561 // to do a linear search of the symbol table for each symbol
16562
16563 if( !our_index
16564 && new_var->type != FldLiteralN
16565 && !(new_var->attr & intermediate_e))
16566 {
16567 our_index = field_index(new_var);
16568 if( our_index == (size_t)-1 )
16569 {
16570 // Hmm. Couldn't find it. Seems odd.
16571 our_index = 0;
16572 }
16573 }
16574
16575 char base_name[257];
16576 char id_string[32] = "";
16577
16578 if( new_var->attr & external_e )
16579 {
16580 // For external variables, just stick with the original name
16581 sprintf(base_name, "%s_cblc_field", new_var->name);
16582 }
16583 else
16584 {
16585 if( our_index
16586 && new_var->parent
16587 && symbol_at(new_var->parent)->type == SymField )
16588 {
16589 // We have a parent that is a field
16590 sprintf(id_string, "." HOST_SIZE_T_PRINT_DEC "_" HOST_SIZE_T_PRINT_DEC,
16591 (fmt_size_t)our_index, (fmt_size_t)new_var->parent);
16592 }
16593 else
16594 {
16595 // The parent is zero, so it'll be implied:
16596 sprintf(id_string, "." HOST_SIZE_T_PRINT_DEC,
16597 (fmt_size_t)our_index);
16598 }
16599
16600 if(strcasecmp(new_var->name, "filler") == 0)
16601 {
16602 // Multiple "fillers" can have the same parent, so we use filler_count
16603 // to distinguish them. We also prepend an underscore, so that
16604 // the user can't trip us up by creating their *own* cobol variable
16605 // named "FILLER-1"
16606 static int filler_count = 1;
16607 sprintf(base_name, "_filler_%d", filler_count++);
16608 }
16609 else if( strlen(new_var->name) == 0 )
16610 {
16611 // This can happen.
16612 static int empty_count = 1;
16613 sprintf(base_name,
16614 "_%s_%d",
16615 cbl_field_type_str(new_var->type),
16616 empty_count++);
16617 }
16618 else if( new_var->attr & intermediate_e )
16619 {
16620 static int inter_count = 1;
16621 sprintf(base_name,
16622 "_%s_%s_%d",
16623 "intermediate",
16624 new_var->name,
16625 inter_count++);
16626 }
16627 else
16628 {
16629 strcpy(base_name, new_var->name);
16630 }
16631 strcat(base_name, id_string);
16632 }
16633
16634 if( new_var->attr & external_e )
16635 {
16636 //fprintf(stderr, "external_e base name is %s\n", base_name);
16637 new_var_decl = gg_define_variable( cblc_field_type_node,
16638 base_name,
16639 vs_external);
16640 SET_DECL_MODE(new_var_decl, BLKmode);
16641 }
16642 else if( new_var->attr & (intermediate_e)
16643 && new_var->type != FldLiteralA
16644 && new_var->type != FldLiteralN )
16645 {
16646 // new_var_decl = gg_define_variable( cblc_field_type_node,
16647 // base_name,
16648 // vs_static);
16649 new_var_decl = gg_define_variable( cblc_field_type_node,
16650 base_name,
16651 vs_stack);
16652 SET_DECL_MODE(new_var_decl, BLKmode);
16653 }
16654 else
16655 {
16656 new_var_decl = gg_define_variable( cblc_field_type_node,
16657 base_name,
16658 vs_static);
16659 SET_DECL_MODE(new_var_decl, BLKmode);
16660 }
16661 }
16662 return new_var_decl;
16663 }
16664
16665 #if 1
16666 static void
16667 psa_FldLiteralA(struct cbl_field_t *field )
16668 {
16669 Analyze();
16670 SHOW_PARSE
16671 {
16672 SHOW_PARSE_HEADER
16673 SHOW_PARSE_FIELD(" ", field)
16674 SHOW_PARSE_END
16675 }
16676
16677 TRACE1
16678 {
16679 TRACE1_HEADER
16680 TRACE1_END
16681 }
16682
16683 // We are constructing a completely static constant structure. We know the
16684 // capacity. We'll create it from the data.initial. The cblc_field_t:data
16685 // will be an ASCII/EBCDIC copy of the .initial data. The .initial will be
16686 // left as ASCII. The var_decl_node will be an ordinary cblc_field_t, which
16687 // means that at this point in time, a FldLiteralA can be used anywhere a
16688 // FldGroup or FldAlphanumeric can be used. We are counting on the parser
16689 // not allowing a FldLiteralA to be a left-hand-side variable.
16690
16691 // First make room
16692 static size_t buffer_size = 1024;
16693 static char *buffer = static_cast<char *>(xmalloc(buffer_size));
16694 if( buffer_size < field->data.capacity+1 )
16695 {
16696 buffer_size = field->data.capacity+1;
16697 buffer = static_cast<char *>(xrealloc(buffer, buffer_size));
16698 }
16699 gcc_assert(buffer);
16700
16701 cbl_figconst_t figconst = cbl_figconst_of( field->data.initial );
16702 gcc_assert(figconst == normal_value_e);
16703
16704 if( internal_codeset_is_ebcdic() )
16705 {
16706 for( size_t i=0; i<field->data.capacity; i++ )
16707 {
16708 buffer[i] = ascii_to_internal(field->data.initial[i]);
16709 }
16710 }
16711 else
16712 {
16713 memcpy(buffer, field->data.initial, field->data.capacity);
16714 }
16715 buffer[field->data.capacity] = '\0';
16716
16717 // We have the original nul-terminated text at data.initial. We have a
16718 // copy of it in buffer[] in the internal codeset.
16719
16720 static const char name_base[] = "_literal_a_";
16721
16722 // We will reuse a single static structure for each string
16723 static std::unordered_map<std::string, int> seen_before;
16724
16725 std::string field_string(buffer);
16726
16727 #if 0
16728 /* This code is suppoed to re-use literals, and seems to work just fine in
16729 x86_64-linux and on an Apple aarch64 M1 Macbook Pro. But on an M1
16730 mini, using -Os optimization, attempts were made in the generated
16731 assembly language to define _literal_a_1 more than once.
16732
16733 I didn't know how to try to track this one down, so I decided simply to
16734 punt by removing the code.
16735
16736 I am leaving the code here because of a conviction that it someday should
16737 be tracked down. */
16738
16739 std::unordered_map<std::string, int>::const_iterator it =
16740 seen_before.find(field_string);
16741
16742 if( it != seen_before.end() )
16743 {
16744 // We've seen that string before.
16745 int nvar = it->second;
16746 char ach[32];
16747 sprintf(ach, "%s%d", name_base, nvar);
16748 field->var_decl_node = gg_declare_variable(cblc_field_type_node,
16749 ach,
16750 NULL,
16751 vs_file_static);
16752 }
16753 else
16754 #endif
16755 {
16756 // We have not seen that string before
16757 static int nvar = 0;
16758 nvar += 1;
16759 seen_before[field_string] = nvar;
16760
16761 char ach[32];
16762 sprintf(ach, "%s%d", name_base, nvar);
16763 field->var_decl_node = gg_define_variable( cblc_field_type_node,
16764 ach,
16765 vs_file_static);
16766 actually_create_the_static_field(
16767 field,
16768 build_string_literal(field->data.capacity+1,
16769 buffer),
16770 field->data.capacity+1,
16771 field->data.initial,
16772 NULL_TREE,
16773 field->var_decl_node);
16774 TREE_READONLY(field->var_decl_node) = 1;
16775 TREE_USED(field->var_decl_node) = 1;
16776 TREE_STATIC(field->var_decl_node) = 1;
16777 DECL_PRESERVE_P (field->var_decl_node) = 1;
16778 }
16779 // TRACE1
16780 // {
16781 // TRACE1_INDENT
16782 // TRACE1_TEXT("Finished")
16783 // TRACE1_END
16784 // }
16785 }
16786 #endif
16787
16788 void
16789 parser_local_add(struct cbl_field_t *new_var )
16790 {
16791 SHOW_PARSE
16792 {
16793 SHOW_PARSE_HEADER
16794 SHOW_PARSE_FIELD(" ", new_var);
16795 SHOW_PARSE_END
16796 }
16797
16798 CHECK_FIELD(new_var);
16799
16800 IF( member(new_var->var_decl_node, "data"),
16801 ne_op,
16802 gg_cast(UCHAR_P, null_pointer_node) )
16803 {
16804 gg_call(VOID,
16805 "__gg__push_local_variable",
16806 gg_get_address_of(new_var->var_decl_node),
16807 NULL_TREE);
16808 }
16809 ELSE
16810 ENDIF
16811
16812 if( new_var->level == LEVEL01 || new_var->level == LEVEL77)
16813 {
16814 // We need to allocate memory on the stack for this variable
16815 tree array_type = build_array_type_nelts(UCHAR, new_var->data.capacity);
16816 tree data_decl_node = gg_define_variable( array_type,
16817 NULL,
16818 vs_stack);
16819 gg_assign( member(new_var->var_decl_node, "data"),
16820 gg_get_address_of(data_decl_node) );
16821 }
16822 cbl_refer_t wrapper;
16823 wrapper.field = new_var;
16824 initialize_variable_internal(wrapper);
16825 }
16826
16827 void
16828 parser_field_attr_set( cbl_field_t *tgt, cbl_field_attr_t attr, bool on_off )
16829 {
16830 if( on_off )
16831 {
16832 gg_assign(member(tgt, "attr"),
16833 gg_bitwise_or(member(tgt, "attr"),
16834 build_int_cst_type(SIZE_T, attr)));
16835 }
16836 else
16837 {
16838 gg_assign(member(tgt, "attr"),
16839 gg_bitwise_and(member(tgt, "attr"),
16840 build_int_cst_type(SIZE_T, ~attr)));
16841 }
16842 }
16843
16844 void
16845 parser_symbol_add(struct cbl_field_t *new_var )
16846 {
16847 Analyze();
16848 SHOW_PARSE
16849 {
16850 do
16851 {
16852 fprintf(stderr, "( %d ) %s:", CURRENT_LINE_NUMBER, __func__);
16853 }
16854 while(0);
16855
16856 fprintf(stderr, " %2.2u %s<%s> off:" HOST_SIZE_T_PRINT_UNSIGNED " "
16857 "msiz:%u cap:%u dig:%u rdig:%d attr:0x" HOST_SIZE_T_PRINT_HEX_PURE " loc:%p",
16858 new_var->level,
16859 new_var->name,
16860 cbl_field_type_str(new_var->type),
16861 (fmt_size_t)new_var->offset,
16862 new_var->data.memsize,
16863 new_var->data.capacity,
16864 new_var->data.digits,
16865 new_var->data.rdigits,
16866 (fmt_size_t)new_var->attr,
16867 static_cast<void*>(new_var));
16868
16869 if( is_table(new_var) )
16870 {
16871 fprintf(stderr," OCCURS:" HOST_SIZE_T_PRINT_DEC,
16872 (fmt_size_t)new_var->occurs.ntimes());
16873 }
16874 const cbl_field_t *parent = parent_of(new_var);
16875 if( parent )
16876 {
16877 fprintf(stderr,
16878 " parent:(" HOST_SIZE_T_PRINT_DEC ")%s",
16879 (fmt_size_t)new_var->parent,
16880 parent->name);
16881 }
16882 else
16883 {
16884 // Parent isn't a field
16885 size_t parent_index = new_var->parent;
16886 if( parent_index )
16887 {
16888 const symbol_elem_t *e = symbol_at(parent_index);
16889 if( e->type == SymFile )
16890 {
16891 fprintf(stderr,
16892 " parent_file:(" HOST_SIZE_T_PRINT_DEC ")%s",
16893 (fmt_size_t)new_var->parent,
16894 e->elem.file.name);
16895 if( e->elem.file.attr & external_e )
16896 {
16897 fprintf(stderr, " (flagged external)");
16898 }
16899 }
16900 }
16901 }
16902
16903 if( symbol_redefines(new_var) )
16904 {
16905 fprintf(stderr,
16906 " redefines:(%p)%s",
16907 static_cast<void*>(symbol_redefines(new_var)),
16908 symbol_redefines(new_var)->name);
16909 }
16910
16911 SHOW_PARSE_END
16912 }
16913
16914 if( new_var->level == 1 && new_var->occurs.bounds.upper )
16915 {
16916 if( new_var->data.memsize < new_var->data.capacity * new_var->occurs.bounds.upper )
16917 {
16918 cbl_internal_error("LEVEL 01 (%s) OCCURS "
16919 "has insufficient data.memsize", new_var->name);
16920 }
16921 }
16922
16923 if( new_var->var_decl_node )
16924 {
16925 if( new_var->type != FldConditional )
16926 {
16927 // There is a possibility when re-using variables that a temporary that
16928 // was created at compile time might not have a data pointer at run time.
16929 if( new_var->attr & (intermediate_e) )
16930 {
16931 IF( member(new_var->var_decl_node, "allocated"),
16932 lt_op,
16933 member(new_var->var_decl_node, "capacity") )
16934 {
16935 gg_free(member(new_var, "data"));
16936 gg_assign(member(new_var, "data"),
16937 gg_cast(UCHAR_P, gg_malloc(new_var->data.capacity)));
16938 gg_assign(member(new_var, "allocated"),
16939 build_int_cst_type(SIZE_T, new_var->data.capacity));
16940 }
16941 ELSE
16942 {
16943 }
16944 ENDIF
16945 }
16946 }
16947 else
16948 {
16949 gg_assign(new_var->var_decl_node, boolean_false_node);
16950 }
16951
16952 goto done;
16953 }
16954
16955 if( !(new_var->attr & initialized_e) )
16956 {
16957 cbl_field_type_t incoming_type = new_var->type;
16958
16959 if( is_register_field(new_var) )
16960 {
16961 psa_global(new_var);
16962 goto done;
16963 }
16964
16965 if( new_var->type == FldBlob )
16966 {
16967 psa_FldBlob(new_var);
16968 goto done;
16969 }
16970
16971 if( new_var->type == FldLiteralA )
16972 {
16973 new_var->data.picture = "";
16974 psa_FldLiteralA(new_var);
16975 goto done;
16976 }
16977
16978 size_t length_of_initial_string = 0;
16979 const char *new_initial = NULL;
16980
16981 // gg_printf("parser_symbol_add %s\n", build_string_literal( strlen(new_var->name)+1, new_var->name), NULL_TREE);
16982
16983 // If we are dealing with an alphanumeric, and it is not hex_encoded, we
16984 // want to convert to single-byte-encoding (if it happens to be UTF-8) and
16985 // to EBCDIC, if EBCDIC is in force:
16986
16987 // Make sure we have a new variable to work with.
16988 if( !new_var )
16989 {
16990 cbl_internal_error("%<parser_symbol_add()%> was called with a NULL %<new_var%>");
16991 }
16992
16993 TRACE1
16994 {
16995 TRACE1_HEADER
16996 if( new_var->level )
16997 {
16998 gg_fprintf( trace_handle,
16999 1,
17000 "%2.2d ",
17001 build_int_cst_type(INT, new_var->level));
17002 }
17003 TRACE1_TEXT(new_var->name)
17004 TRACE1_TEXT_ABC(" (", cbl_field_type_str(new_var->type) ,")")
17005 if( new_var->type == FldLiteralN)
17006 {
17007 const void *p1 = (new_var->data.initial);
17008 const long *pldata = static_cast<const long *>(p1);
17009 long ldata = *pldata;
17010 gg_fprintf( trace_handle,
17011 1, " [%ld]",
17012 build_int_cst_type(LONG, ldata));
17013 }
17014 TRACE1_END
17015 }
17016
17017 if( is_table(new_var) && new_var->data.capacity == 0)
17018 {
17019 cbl_internal_error(
17020 "%s: %d %s is a table, but it improperly has a capacity of zero",
17021 __func__,
17022 new_var->level,
17023 new_var->name);
17024 }
17025
17026 cbl_field_t *ancestor = NULL;
17027 tree immediate_parent = NULL_TREE;
17028
17029 if( new_var->parent > 0 )
17030 {
17031 symbol_elem_t *parent = symbol_at(new_var->parent);
17032 gcc_assert(parent);
17033 if( parent->type == SymField )
17034 {
17035 ancestor = cbl_field_of(parent);
17036 immediate_parent = ancestor->var_decl_node;
17037 }
17038 }
17039
17040 if( ancestor == NULL )
17041 {
17042 // This is a last ditch effort for handling SAME AREA. Although
17043 // symbol_redefines should work for REDEFINES, LEVEL66, and SAME AREA, I
17044 // decided to leave the existing code alone and added this in when SAME AREA
17045 // was added in.
17046 ancestor = symbol_redefines(new_var);
17047 if( ancestor )
17048 {
17049 immediate_parent = ancestor->var_decl_node;
17050
17051 // This obscure test was put in to find problems caused by SAME AREA,
17052 // which at one point would cause a parent to be erroneously seen after
17053 // the child.
17054 assert(ancestor->our_index < new_var->our_index);
17055 }
17056 }
17057
17058 if( ancestor == new_var )
17059 {
17060 cbl_internal_error("%s: %s is its own ancestor", __func__, new_var->name);
17061 }
17062
17063 if( !ancestor && (new_var->level > LEVEL01 && new_var->level <= LEVEL49 ) )
17064 {
17065 cbl_internal_error("%s: %d %qs has NULL ancestor", __func__,
17066 new_var->level, new_var->name);
17067 }
17068
17069 // new_var's var_decl_node should be NULL at this point
17070 if( new_var->var_decl_node )
17071 {
17072 cbl_internal_error( "%s(%s) improperly has a non-null "
17073 "%<var_decl_node%>", __func__, new_var->name);
17074 }
17075
17076 switch( new_var->type )
17077 {
17078 static int counter=1;
17079 char ach[2*sizeof(cbl_name_t)];
17080 case FldConditional:
17081 // FldConditional corresponds to a C "bool". But we don't carry
17082 // a runtime copy of a structure for the variable; instead,
17083 // var_decl_node becomes a boolean_type_node that is used directly.
17084 sprintf(ach, "_%sconditional_%d", new_var->name, counter++);
17085 new_var->var_decl_node = gg_define_variable(BOOL, ach, vs_static);
17086 goto done;
17087 break;
17088
17089 default:
17090 break;
17091 }
17092
17093 if( new_var->type == FldNumericBinary
17094 || new_var->type == FldNumericBin5 )
17095 {
17096 switch( new_var->data.capacity )
17097 {
17098 case 1:
17099 case 2:
17100 case 4:
17101 case 8:
17102 case 16:
17103 break;
17104 default:
17105 fprintf(stderr,
17106 "%s is type %s and has capacity %u\n",
17107 new_var->name,
17108 cbl_field_type_str(new_var->type),
17109 new_var->data.capacity);
17110 gcc_unreachable();
17111 break;
17112 }
17113 }
17114
17115 size_t level_88_string_size = 0;
17116 char *level_88_string = NULL;
17117 if( ancestor )
17118 {
17119 level_88_string = get_level_88_domain(ancestor->data.capacity, new_var, level_88_string_size);
17120 }
17121
17122 if( !new_var->data.picture )
17123 {
17124 // When picture is NULL, we have to keep testing for NULLness at runtime
17125 // Force it to be a zero-length string here, so that we don't need to
17126 // worry about it.
17127 new_var->data.picture = "";
17128 }
17129
17130 if( new_var->type == FldNumericEdited && (new_var->attr & scaled_e) )
17131 {
17132 char *pic = xstrdup(new_var->data.picture); // duplicate the const char *
17133 remove_p_from_picture(pic);
17134 new_var->data.picture = pic;
17135 }
17136
17137 if( new_var->type == FldClass && new_var->level != 88 )
17138 {
17139 new_var->data.initial = get_class_condition_string(new_var);
17140 }
17141
17142 if( new_var->type == FldLiteralA )
17143 {
17144 length_of_initial_string = new_var->data.capacity;
17145 }
17146 else if( new_var->data.initial && new_var->data.initial[0] != '\0' )
17147 {
17148 if( new_var->type == FldClass )
17149 {
17150 length_of_initial_string = strlen(new_var->data.initial)+1;
17151 }
17152 else if( new_var->type == FldNumericDisplay )
17153 {
17154 length_of_initial_string = strlen(new_var->data.initial)+1;
17155 }
17156 else
17157 {
17158 // This is an ordinary string
17159 // fprintf(stderr, ">>>>>>> parser_symbol_add %s %s \n", cbl_field_type_str(new_var->type), new_var->name);
17160 // fprintf(stderr, " %d %d\n", (int)strlen(new_var->data.initial), (int)new_var->data.capacity);
17161 //length_of_initial_string = strlen(new_var->data.initial) + 1;
17162 length_of_initial_string = new_var->data.capacity + 1;
17163 }
17164 }
17165 else
17166 {
17167 // We have something that doesn't have a data.initial pointer
17168 length_of_initial_string = 0;
17169 }
17170
17171 // GDB needs to know the data hierarchy. We do that by including our_index
17172 // and parent index in the variable name:
17173
17174 size_t our_index = new_var->our_index;
17175
17176 if( !our_index
17177 && new_var->type != FldLiteralN
17178 && !(new_var->attr & intermediate_e))
17179 {
17180 // During the early stages of implementing cbl_field_t::our_index, there
17181 // were execution paths in parse.y and parser.cc that resulted in
17182 // our_index not being set. Those should be gone.
17183 fprintf(stderr, "our_index is NULL under unanticipated circumstances");
17184 gcc_assert(false);
17185 }
17186
17187 // When we create the cblc_field_t structure, we need a data pointer
17188 // for "data". In the case of a variable that has no parent, we
17189 // have to allocate storage. In the case of a variable that has a parent,
17190 // we calculate data as the pointer to our parent's data plus our
17191 // offset.
17192
17193 // Declare and define the structure. This code *must* match
17194 // the C structure declared in libgcobol.c. Towards that end, the
17195 // variables are declared in descending order of size in order to
17196 // make the packing match up.
17197
17198 // This uses a single structure type_decl template for creating each structure
17199
17200 char external_record_base[2*sizeof(cbl_name_t)] = "";
17201
17202 if( new_var->parent > 0 )
17203 {
17204 symbol_elem_t *parent = symbol_at(new_var->parent);
17205 gcc_assert(parent);
17206 if( parent->type == SymField )
17207 {
17208 ancestor = cbl_field_of(parent);
17209 immediate_parent = ancestor->var_decl_node;
17210 }
17211 else if( parent->type == SymFile )
17212 {
17213 if( parent->elem.file.attr & external_e )
17214 {
17215 // The parent of new_var is a SymFile with the external_e attribute
17216 // Therefore, we have to establish new_var as an external with a
17217 // predictable name
17218 strcpy(external_record_base, parent->elem.file.name);
17219 }
17220 }
17221 }
17222
17223 tree new_var_decl = psa_new_var_decl(new_var, external_record_base);
17224
17225 if( new_var->type == FldNumericEdited )
17226 {
17227 // Decide if a NumericEdited can hold negative numbers:
17228 size_t len = strlen( new_var->data.picture);
17229
17230 new_var->attr &= ~signable_e;
17231 if( strchr(new_var->data.picture, '+') )
17232 {
17233 new_var->attr |= signable_e;
17234 }
17235 else if( strchr(new_var->data.picture, '-') )
17236 {
17237 new_var->attr |= signable_e;
17238 }
17239 else if( len > 2 )
17240 {
17241 char ch1 = _toupper(new_var->data.picture[len-2]);
17242 char ch2 = _toupper(new_var->data.picture[len-1]);
17243 if( (ch1 == 'D' && ch2 == 'B')
17244 || (ch1 == 'C' && ch2 == 'R') )
17245 {
17246 new_var->attr |= signable_e;
17247 }
17248 }
17249 }
17250
17251 /*
17252 * Burn after reading. (Delete comment after implementing.)
17253 *
17254 * As of Tue Apr 4 10:29:35 2023, we support 01 CONSTANT numeric values as follows:
17255 * 1. FldNumericBin5
17256 * 2. always constant_e, also potentially global_e
17257 * 3. compile-time value in cbl_field_data_t::value
17258 * 4. cbl_field_data_t::capacity is 0 because it requires no working storage
17259 */
17260
17261 if( new_var->data.capacity == 0
17262 && new_var->level != 88
17263 && new_var->type != FldClass
17264 && new_var->type != FldLiteralN
17265 && new_var->type != FldLiteralA )
17266 {
17267 cbl_internal_error( "%s: %d %s<%s> improperly has a data.capacity of zero",
17268 __func__,
17269 new_var->level,
17270 new_var->name,
17271 cbl_field_type_str(new_var->type));
17272 }
17273
17274 new_var->var_decl_node = new_var_decl;
17275
17276 if( level_88_string )
17277 {
17278 new_var->data.initial = level_88_string;
17279 length_of_initial_string = level_88_string_size;
17280 }
17281
17282 tree data_area = null_pointer_node;
17283
17284 if( *external_record_base )
17285 {
17286 char achDataName[256];
17287 sprintf(achDataName, "__%s_vardata", external_record_base);
17288 tree array_type = build_array_type_nelts(UCHAR, new_var->data.capacity);
17289 new_var->data_decl_node = gg_define_variable(
17290 array_type,
17291 achDataName,
17292 vs_external);
17293 data_area = gg_get_address_of(new_var->data_decl_node);
17294 goto actual_allocate;
17295 }
17296
17297 if( ancestor && new_var->level != 0 )
17298 {
17299 // This variable has an ancestor, so we share its already-allocated data
17300 // area
17301 new_var->data_decl_node = ancestor->data_decl_node;
17302 }
17303 else
17304 {
17305 // We have no ancestor, so data_decl_node must be allocated. Note that
17306 // LEVEL00 variables might have ancestors (INDEXED BY variables, for
17307 // example), but they need data allocated.
17308
17309 if( new_var->type == FldLiteralN )
17310 {
17311 // A numeric literal gets special handling:
17312 psa_FldLiteralN(new_var);
17313 data_area = gg_get_address_of(new_var->data_decl_node);
17314 }
17315 else
17316 {
17317 // Create a static array of UCHAR, and make that the data_decl_node
17318 // size_t bytes_to_allocate = new_var->data.memsize ?
17319 // new_var->data.memsize : new_var->data.capacity;
17320 size_t bytes_to_allocate = std::max(new_var->data.memsize,
17321 new_var->data.capacity);
17322
17323 // A FldClass actually doesn't need any bytes, because the only important
17324 // thing about it is the .initial field. We will allocate a single byte,
17325 // just to keep run-time pointers from being NULL
17326 if( (new_var->type == FldClass && bytes_to_allocate == 0)
17327 || (new_var->type == FldLiteralA && bytes_to_allocate == 0) )
17328 {
17329 bytes_to_allocate = 1;
17330 }
17331
17332 if( !bytes_to_allocate )
17333 {
17334 cbl_internal_error( "%<bytes_to_allocate%> is zero for %s (symbol number "
17335 HOST_SIZE_T_PRINT_DEC ")",
17336 new_var->name,
17337 (fmt_size_t)new_var->our_index);
17338 }
17339
17340 if( new_var->type == FldIndex && new_var->level == 0 )
17341 {
17342 // Do nothing, because the OCCURS INDEXED BY variable needs data
17343 // allocated. This leaves bytes_to_allcate at its value.
17344 }
17345 else
17346 {
17347 if( new_var->attr & based_e
17348 || new_var->attr & linkage_e
17349 || new_var->attr & local_e )
17350 {
17351 // BASED variables get their data through ALLOCATE or SET
17352 // LINKAGE variables get their data from the caller
17353 // LOCAL variables get their data dynamically.
17354 bytes_to_allocate = 0;
17355 }
17356 }
17357
17358 if( bytes_to_allocate )
17359 {
17360 // We need a unique name for the allocated data for this COBOL variable:
17361 char achDataName[256];
17362 if( new_var->attr & external_e )
17363 {
17364 sprintf(achDataName, "%s", new_var->name);
17365 }
17366 else if( new_var->name[0] == '_' )
17367 {
17368 // Avoid doubling up on leading underscore
17369 sprintf(achDataName,
17370 "%s_data_" HOST_SIZE_T_PRINT_UNSIGNED,
17371 new_var->name,
17372 (fmt_size_t)sv_data_name_counter++);
17373 }
17374 else
17375 {
17376 sprintf(achDataName,
17377 "_%s_data_" HOST_SIZE_T_PRINT_UNSIGNED,
17378 new_var->name,
17379 (fmt_size_t)sv_data_name_counter++);
17380 }
17381
17382 if( new_var->attr & external_e )
17383 {
17384 tree array_type = build_array_type_nelts(UCHAR, bytes_to_allocate);
17385 new_var->data_decl_node = gg_define_variable(
17386 array_type,
17387 achDataName,
17388 vs_external);
17389 data_area = gg_get_address_of(new_var->data_decl_node);
17390 }
17391 else
17392 {
17393 gg_variable_scope_t vs_scope = (new_var->attr & intermediate_e)
17394 ? vs_stack : vs_static ;
17395 tree array_type = build_array_type_nelts(UCHAR, bytes_to_allocate);
17396 new_var->data_decl_node = gg_define_variable(
17397 array_type,
17398 achDataName,
17399 vs_scope);
17400 data_area = gg_get_address_of(new_var->data_decl_node);
17401 }
17402 }
17403 }
17404 }
17405
17406 if( new_var->data.initial )
17407 {
17408 new_initial = initial_from_initial(new_var);
17409 }
17410 if( new_initial )
17411 {
17412 switch(new_var->type)
17413 {
17414 case FldGroup:
17415 case FldAlphanumeric:
17416 case FldLiteralA:
17417 length_of_initial_string = new_var->data.capacity+1;
17418 break;
17419
17420 default:
17421 length_of_initial_string = new_var->data.capacity;
17422 break;
17423 }
17424 }
17425 else
17426 {
17427 new_initial = new_var->data.initial;
17428 }
17429
17430 actual_allocate:
17431 actually_create_the_static_field( new_var,
17432 data_area,
17433 length_of_initial_string,
17434 new_initial,
17435 immediate_parent,
17436 new_var_decl);
17437
17438 if( level_88_string )
17439 {
17440 free(level_88_string);
17441 }
17442
17443 if( !(new_var->attr & ( linkage_e | based_e)) )
17444 {
17445 static const bool explicitly = false;
17446 static const bool just_once = true;
17447 initialize_variable_internal( new_var,
17448 explicitly,
17449 just_once);
17450 }
17451
17452 if( new_var->type != incoming_type )
17453 {
17454 fprintf(stderr, "Type mismatch in parser_symbol_add()\n");
17455 gcc_unreachable();
17456 }
17457 new_var->attr |= initialized_e;
17458 }
17459 else
17460 {
17461 fprintf(stderr, "parser_symbol_add() skipping %s", new_var->name);
17462 }
17463 done:
17464 return;
17465 }