Debian Codegraph @

gcc/cobol/gengen.h

raw
1 /*
2 * Copyright (c) 2021-2025 Symas Corporation
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of the Symas Corporation nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 #ifndef __GENGEN_H
31 #define __GENGEN_H
32
33 // Note how the definitions of IF and WHILE lets you use them as
34 // IF(a,b,c) and WHILE(a,b,c) with no semicolon.
35 // And, yes, I see that ELSE, ENDIF, and WEND are all the same. Sometimes
36 // looks *are* important, and the multiple definitions make things easier
37 // to understand.
38
39 #define IF(a,b,c) gg_if((a),(b),(c));
40 #define ELSE current_function->statement_list_stack.pop_back();
41 #define ENDIF current_function->statement_list_stack.pop_back();
42 #define WHILE(a,b,c) gg_while((a),(b),(c));
43 #define WEND current_function->statement_list_stack.pop_back();
44
45 // mnemonics for variable types:
46
47 #define VOID void_type_node
48 #define BOOL boolean_type_node
49 #define CHAR char_type_node
50 #define SCHAR signed_char_type_node
51 #define UCHAR unsigned_char_type_node
52 #define SHORT short_integer_type_node
53 #define USHORT short_unsigned_type_node
54 #define WCHAR short_unsigned_type_node
55 #define INT integer_type_node
56 #define INT_P build_pointer_type(integer_type_node)
57 #define UINT unsigned_type_node
58 #define LONG long_integer_type_node
59 #define ULONG long_unsigned_type_node
60 #define LONGLONG long_long_integer_type_node
61 #define ULONGLONG long_long_unsigned_type_node
62 #define SIZE_T size_type_node
63 #define SIZE_T_P (build_pointer_type(SIZE_T))
64 #define SSIZE_T ptrdiff_type_node
65 #define INT128 intTI_type_node
66 #define UINT128 unsigned_intTI_type_node
67 #define FLOAT float32_type_node
68 #define DOUBLE float64_type_node
69 #define LONGDOUBLE long_double_type_node
70 #define FLOAT128 float128_type_node
71 #define VOID_P ptr_type_node
72 #define VOID_P_P (build_pointer_type(VOID_P))
73 #define CHAR_P char_ptr_type_node
74 #define UCHAR_P uchar_ptr_type_node
75 #define WCHAR_P wchar_ptr_type_node
76 #define FILE_P fileptr_type_node
77
78 #define SIZE128 (16) // In bytes
79
80 /* Explanatory note for vs_file_static variables
81
82 In a C program, you can have this variable declaration outside of a
83 function:
84
85 static const int intvar = 12321;
86
87 It will be visible to any function that follows. After several days of
88 experimentation and research, I found I was unable to duplicate this
89 behavior in the GCOBOL code generator. I simply wasn't able to reverse
90 engineer whatever magical incantations are necessary to declare and define]
91 variables on the translation unit level rather than on the function level.
92
93 Having reached the point where the structural integrity of my desk was being
94 threatened by the repeated percussive strikes from my forehead, I turned my
95 attention to an equivalent workaround.
96
97 On the assembly language level, there is no fundamental way of making a
98 variable visible to only a specific function. So, to distinguish between
99 two non-global variables named "fred" in two different functions, the C
100 compiler appends a dot and a number, with the "number" being different for
101 the two functions.
102
103 The GCOBOL compiler has been doing just that. So, to implement a
104 vs_file_static variable, I treat it just like a vs_static variable, but
105 without appending a differentiator.
106
107 */
108
109 enum gg_variable_scope_t {
110 vs_stack,
111 vs_static,
112 vs_file_static, // static variable of file scope
113 vs_external, // Creates a PUBLIC STATIC variable of file scope
114 vs_external_reference, // References the previous
115 vs_file, // variable of file scope, without static
116 };
117
118 struct gg_function_t
119 {
120 // Nomenclature Alert: The "function" in gg_function_t was chosen
121 // originally because a PROGRAM-ID is implemented as a C-style "function",
122 // and there are numerous tree variables that refer to "functions".
123 // Eventually the COBOL compiler grew to handle not just COBOL PROGRAM-ID
124 // "programs", but also user-defined COBOL FUNCTION-ID "functions". This
125 // inevitably is confusing. Sorry about that.
126
127 // This structure contains state variables for a single function.
128
129 const char *our_unmangled_name; // This is the original name
130 const char *our_name; // This is our mangled name
131 tree function_address;
132 size_t our_symbol_table_index;
133
134 // The function_decl is fundamental to many, many things
135 tree function_decl;
136
137 // We keep track of the end of the chain of blocks:
138 tree current_block;
139
140 // Every function has a context, wherein temporary variables get created
141 // and whose names won't collide with the names in other function.
142
143 // But it is often necessary to create subcontexts, which inherit names from
144 // its parent function, but can reuse names, and create new ones, without
145 // collisions. Each context gets its own bind_expr, each bind_expr points
146 // to its own block. So, to create subcontexts, we need to know which
147 // bind_expr we add variable declarations to.
148 std::vector<tree> bind_expr_stack;
149
150 // Every function has a statement list. But there can be statements
151 // that consist of statement lists. This happens when building IF
152 // statements (TRUE gets its own list, as does FALSE) and WHILE statements
153 // (where the execution block is a statement list. This stack enables that
154 // to happen cleanly, so the programmer doesn't have to be concerned about
155 // which list is being built.
156
157 // Note that the gg_statement_list_stack can grow larger than the
158 // current_function->bind_expr_stack stack, because
159 // there are times -- like inside of WHILE() and IF constructs -- where we
160 // push onto the statement_list_stack and even create new bind_expr nodes,
161 // but don't need a full new context. But every new context gets a new
162 // statement list, and when
163 // current_function->bind_expr_stack is popped,
164 // statement_list_stack is popped, too.
165 std::vector<tree> statement_list_stack;
166
167 // COBOL sections and paragraphs are handled identically; it's the context
168 // that makes them different: PROGRAMS contain SECTIONS, and SECTIONS
169 // contain paragraphs. I call both SECTIONS and PARAGRAPHS "procs"
170
171 // At any given moment, there is one "current section" and one "current
172 // paragraph".
173 struct cbl_proc_t *current_section;
174 struct cbl_proc_t *current_paragraph;
175
176 tree void_star_temp; // At the end of every paragraph and section, we
177 // // we need a variable "void *temp" to hold a
178 // // label for one instruction. Rather than clutter
179 // // up the code with temporaries, we use this one
180 // // instance instead.
181
182 tree first_time_through;
183
184 tree skip_init_goto;
185 tree skip_init_label;
186
187 // We use context_count to detect a mismatch between gg_push_context() and
188 // gg_pop_context calls, which should be equal at the point gimplify is
189 // invoked:
190 int context_count;
191
192 // When a function is called, it comes with zero to N parameters on the
193 // stack. We treat it as variadic; see parser_division(PROCEDURE) to see
194 // how we pick up the N values on the stack:
195 tree formal_parameters;
196
197 // When parser_division(PROCEDURE) is called, it provides a cbl_field_t
198 // *returning parameter. We stash it here; it's used during parser_exit()
199 // to provide the data for the program's return value.
200 cbl_field_t *returning; // This one is on the stack, like a LOCAL-STORAGE
201
202 size_t program_id_number; // Used to give static variables
203 // // a unique .<n> suffix
204
205 // There are two types of nesting. COBOL nesting is implemented in a
206 // logical way: All programs are siblings, with the context being the source
207 // code module. The nested aspect is not reflected in the GENERIC tree.
208
209 // This variable, which appears on the stack, contains the exit_address
210 // for the terminating proc of a PERFORM A or PERFORM A THROUGH B
211 tree perform_exit_address;
212
213 // This variable is a pointer to the first declarative section of this
214 // program-id/function. It's used in when creating the linked list of
215 // declaratives, because the last declarative of a nested function links
216 // back to the first declarative of its immediate parent.
217 tree first_declarative_section;
218
219 // is_function is true when this structure is describing a COBOL FUNCTION-ID
220 // and is false for a PROGRAM-ID
221 bool is_function;
222
223 // This integer is initially set to one when this function is called by
224 // our generated main(). It gets incremented by 1 when the routine is
225 // re-entered: main() -> us -> B -> us
226 // When processing EXIT PROGRAM, if the counter is greater then 1, it is
227 // decremented and a return is created. When the counter is 1, the
228 // EXIT program is treated as a CONTINUE.
229 tree called_by_main_counter;
230 };
231
232 struct cbl_translation_unit_t
233 {
234 // GCC calls a source file a "translation unit". This structure contains
235 // all of the information needed by and for a translation unit. There
236 // probably should be one, and only one, of these instantiated by the COBOL
237 // front end.
238
239 // Every function in this code module gets this translation_unit_decl
240 // as its context. This node is built in parse_enter_file()
241 tree trans_unit_decl;
242
243 // This is the filename of this trans_unit_decl
244 const char *filename;
245
246 // This is the stack of function_decls in this translation unit; each
247 // call to parser_enter_program() pushes onto this stack; each call to
248 // parser_end_program() pops it.
249 std::vector<struct gg_function_t> function_stack;
250
251 // This is where we keep var_decls because of my inability to figure out how
252 // to tell the compiler to create data definitions for translation_unit_decl
253 // variables:
254 std::unordered_map<std::string, tree> trans_unit_var_decls;
255 };
256
257 extern struct cbl_translation_unit_t gg_trans_unit;
258
259 #define current_function (&gg_trans_unit.function_stack.back())
260
261 extern GTY(()) tree char_nodes[256] ;
262 extern GTY(()) tree pvoid_type_node ;
263 extern GTY(()) tree integer_minusone_node;
264 extern GTY(()) tree integer_two_node ;
265 extern GTY(()) tree integer_eight_node ;
266 extern GTY(()) tree size_t_zero_node ;
267 extern GTY(()) tree int128_zero_node ;
268 extern GTY(()) tree int128_five_node ;
269 extern GTY(()) tree int128_ten_node ;
270 extern GTY(()) tree bool_true_node ;
271 extern GTY(()) tree bool_false_node ;
272 extern GTY(()) tree char_ptr_type_node ;
273 extern GTY(()) tree uchar_ptr_type_node ;
274 extern GTY(()) tree wchar_ptr_type_node ;
275 extern GTY(()) tree long_double_ten_node ;
276 extern GTY(()) tree sizeof_size_t ;
277 extern GTY(()) tree sizeof_pointer ;
278
279 // These routines happen when beginning to process a new file, which is also
280 // known, in GCC, as a "translation unit"
281 extern void gg_build_translation_unit(const char *filename);
282
283 // For an expression type to actually be implemented in the target
284 // runtime binary, it has to find its way onto a statement list. (Or be used
285 // as the second operand of a modify_expr, and so on.)
286 extern void gg_append_statement(tree stmt);
287 //// extern void gg_insert_statement(struct tree_stmt_iterator *tsi, tree stmt);
288
289 // For variables:
290 extern void gg_append_var_decl(tree var);
291
292 // type cast
293 extern tree gg_float(tree float_type, tree integer_var);
294 extern tree gg_trunc(tree integer_type, tree float_var);
295 extern tree gg_cast(tree type, tree var);
296
297 // Assignment, that is to say, A = B
298 extern tree gg_assign(tree dest, const tree source);
299
300 // struct creation and field access
301 // Create struct, and access a field in a struct
302 extern tree gg_get_local_struct_type_decl(const char *type_name, int count, ...);
303 extern tree gg_get_filelevel_struct_type_decl(const char *type_name, int count, ...);
304 extern tree gg_get_filelevel_union_type_decl(const char *type_name, int count, ...);
305 extern tree gg_define_local_struct(const char *type_name, const char * var_name, int count ,...);
306 extern tree gg_find_field_in_struct(const tree var_decl, const char *field_name);
307 extern tree gg_struct_field_ref(const tree struct_decl, const char *field);
308 extern tree gg_assign_to_structure(tree var_decl_struct, const char *field, const tree source);
309 extern tree gg_assign_to_structure(tree var_decl_struct, const char *field, int N);
310
311 // Generalized variable declareres. This don't create storage
312 extern tree gg_declare_variable(tree type_decl,
313 const char *name=NULL,
314 tree initial_value=NULL_TREE,
315 gg_variable_scope_t vs_scope=vs_stack,
316 bool *already_defined = NULL);
317 extern tree gg_define_from_declaration(tree var_decl);
318
319 // Generalized variable definers. These create storage
320 extern tree gg_define_variable(tree type_decl);
321 extern tree gg_define_variable(tree type_decl, tree initial_value);
322 extern tree gg_define_variable(tree type_decl, gg_variable_scope_t vs_scope);
323 extern tree gg_define_variable(tree type_decl,
324 const char *name,
325 gg_variable_scope_t vs_scope=vs_stack);
326 extern tree gg_define_variable(tree type_decl,
327 const char *name,
328 gg_variable_scope_t vs_scope,
329 tree initial_value);
330 // Utility definers:
331 extern tree gg_define_bool();
332 extern tree gg_define_char();
333 extern tree gg_define_char(const char *variable_name);
334 extern tree gg_define_char(const char *variable_name, tree ch);
335 extern tree gg_define_char(const char *variable_name, int ch);
336
337 extern tree gg_define_uchar();
338 extern tree gg_define_uchar(const char *variable_name);
339 extern tree gg_define_uchar(const char *variable_name, tree ch);
340 extern tree gg_define_uchar(const char *variable_name, int ch);
341
342 extern tree gg_define_int();
343 extern tree gg_define_int(int N);
344 extern tree gg_define_int(const char *variable_name);
345 extern tree gg_define_int(const char *variable_name, tree N);
346 extern tree gg_define_int(const char *variable_name, int N);
347
348 extern tree gg_define_size_t();
349 extern tree gg_define_size_t(const char *variable_name);
350 extern tree gg_define_size_t(const char *variable_name, tree N);
351 extern tree gg_define_size_t(const char *variable_name, size_t N);
352 extern tree gg_define_size_t(tree N);
353 extern tree gg_define_size_t(size_t N);
354
355 extern tree gg_define_int128();
356 extern tree gg_define_int128(const char *variable_name);
357 extern tree gg_define_int128(const char *variable_name, tree N);
358 extern tree gg_define_int128(const char *variable_name, int N);
359
360 extern tree gg_define_longdouble();
361
362 extern tree gg_define_void_star();
363 extern tree gg_define_void_star(tree var);
364 extern tree gg_define_void_star(const char *variable_name);
365 extern tree gg_define_void_star(const char *variable_name, tree var);
366 extern tree gg_define_void_star(const char *variable_name, gg_variable_scope_t scope);
367
368 extern tree gg_define_char_star();
369 extern tree gg_define_char_star(tree var);
370 extern tree gg_define_char_star(const char *variable_name);
371 extern tree gg_define_char_star(const char *variable_name, tree var);
372 extern tree gg_define_char_star(const char *variable_name, gg_variable_scope_t scope);
373
374 extern tree gg_define_uchar_star();
375 extern tree gg_define_uchar_star(const char *variable_name);
376 extern tree gg_define_uchar_star(const char *variable_name, gg_variable_scope_t scope);
377 extern tree gg_define_uchar_star(tree var);
378 extern tree gg_define_uchar_star(const char *variable_name, tree var);
379
380 // address_of operator; equivalent of C "&buffer"
381 extern tree gg_get_address_of(const tree var_decl);
382
383 // Array creation and access:
384 extern tree gg_define_array(tree type_decl, size_t size);
385 extern tree gg_define_array(tree type_decl, const char *name, size_t size);
386 extern tree gg_define_array(tree type_decl, size_t size, gg_variable_scope_t scope);
387 extern tree gg_define_array(tree type_decl, const char *name, size_t size, gg_variable_scope_t scope);
388
389 extern tree gg_array_value(tree pointer, tree offset=NULL_TREE);
390 extern tree gg_array_value(tree pointer, int N);
391
392 // Here are some unary operations
393 extern void gg_increment(tree var);
394 extern void gg_decrement(tree var);
395 extern tree gg_negate(tree var); // Two's complement negation
396 extern tree gg_bitwise_not(tree var); // Bitwise inversion
397 extern tree gg_abs(tree var); // Absolute value
398
399 // And some binary operations:
400
401 extern tree gg_add(tree addend1, tree addend2);
402 extern tree gg_subtract(tree A, tree B);
403 extern tree gg_multiply(tree A, tree B);
404 extern tree gg_real_divide(tree A, tree B); // Floating point division
405 extern tree gg_divide(tree A, tree B); // Integer division
406 extern tree gg_mod(tree A, tree B);
407 extern tree gg_lshift(tree A, tree B);
408 extern tree gg_rshift(tree A, tree B);
409 extern tree gg_bitwise_or(tree A, tree B);
410 extern tree gg_bitwise_xor(tree A, tree B);
411 extern tree gg_bitwise_and(tree A, tree B);
412
413 // Conditionals: Use the IF() and WHILE() macros, which generated
414 // code that calls these functions. Calling them yourself is
415 // probably a bad idea because there are stacks that have to be
416 // kept in the right states.
417
418 extern tree gg_build_relational_expression( tree operand_a,
419 enum relop_t op,
420 tree operand_b);
421 extern tree gg_build_logical_expression(tree operand_a,
422 enum logop_t op,
423 tree operand_b);
424
425 extern void gg_create_true_false_statement_lists(tree relational_expression);
426 extern void gg_while(tree operand_a, enum relop_t op, tree operand_b);
427 extern void gg_if( tree operand_a, enum relop_t op, tree operand_b);
428
429 // Are are some system functions that can be useful. gg_printf is
430 // particularly useful for generating run-time messages. Actual run-time
431 // code is built using write(), because it allows for file descriptors and
432 // doesn't require null-terminated strings.
433
434 extern tree gg_get_function_address(tree return_type, const char *funcname);
435 extern void gg_printf(const char *format_string, ...);
436 extern tree gg_fprintf(tree fd, int nargs, const char *format_string, ...);
437 extern tree gg_read(tree fd, tree buf, tree count);
438 extern void gg_write(tree fd, tree buf, tree count);
439 extern void gg_memset(tree dest, const tree value, tree size);
440 extern tree gg_memchr(tree s, tree c, tree n);
441 extern void gg_memcpy(tree dest, const tree src, tree size);
442 extern void gg_memmove(tree dest, const tree src, tree size);
443 extern tree gg_memdup(tree data, tree length);
444 extern tree gg_memdup(tree data, size_t length);
445 extern void gg_strcpy(tree char_star_A, tree char_star_B);
446 extern tree gg_strdup(tree char_star_A);
447 extern tree gg_strcmp(tree char_star_A, tree char_star_B);
448 extern tree gg_strncmp(tree char_star_A, tree char_star_B, tree size_t_N);
449
450 // Flow control inside a function
451 extern void gg_return(tree operand = NULL_TREE);
452
453 // These routines are the preample and postamble that bracket everything else
454 extern tree gg_build_fn_decl(const char *funcname, tree fndecl_type);
455 extern tree gg_peek_fn_decl(const char *funcname);
456 extern tree gg_define_function( tree return_type,
457 const char *funcname,
458 const char *unmangled_name,
459 ...);
460 extern void chain_parameter_to_function( tree function_decl,
461 const tree param_type,
462 const char *name);
463 extern void gg_modify_function_type(tree function_decl, tree return_type);
464
465 extern void gg_finalize_function();
466 extern void gg_push_context();
467 extern void gg_pop_context();
468
469 // These are a generalized call constructor. The first for when you just want
470 // the function called, because you don't care about the return value. The others
471 // are for when you do need the return value.
472 extern tree gg_call_expr_list(tree return_type,
473 tree function_pointer,
474 int param_count, tree[]);
475
476 // The following is a garden-variety call, with known return type and known
477 // but in the case where the return value is unimportant.
478 extern void gg_call (tree return_type, const char *function_name, ...);
479 extern tree gg_call_expr(tree return_type, const char *function_name, ...);
480
481 // Returns a simple entangled goto/comefrom pair. Used for things like
482 // IF/ELSE/ENDIF and WHILE/WEND
483 void gg_create_goto_pair(tree *goto_expr, tree *label_expr);
484 void gg_create_goto_pair(tree *goto_expr, tree *label_expr, const char *name);
485
486 // This more complex version is used for implementing SECTIONS and PARAGRAPHS.
487 void gg_create_goto_pair( tree *goto_expr,
488 tree *label_expr,
489 tree *label_addr,
490 const char *name);
491 void gg_create_goto_pair( tree *goto_expr,
492 tree *label_expr,
493 tree *label_addr);
494 void gg_create_goto_pair( tree *goto_expr,
495 tree *label_expr,
496 tree *label_addr,
497 tree *label_decl);
498 void gg_goto_label_decl(tree label_decl);
499
500 // Used for implementing SECTIONS and PARAGRAPHS. When you have a
501 // void *pointer = &&label, gg_goto is the same as
502 // goto *pointer
503 void gg_goto(tree pointer);
504
505 void gg_record_statement_list_start();
506 tree gg_record_statement_list_finish();
507
508 // Used to call system exit()
509 extern void gg_exit(tree exit_code);
510 extern void gg_abort();
511
512 extern tree gg_malloc(tree length);
513 extern tree gg_malloc(size_t length);
514 extern tree gg_realloc(tree base, tree length);
515 extern tree gg_realloc(tree base, size_t length);
516 extern void gg_free(tree pointer);
517 extern tree gg_strlen(tree psz);
518 extern size_t gg_sizeof(tree decl_node);
519
520 extern tree gg_array_of_field_pointers( size_t N,
521 cbl_field_t **fields );
522 extern tree gg_array_of_size_t( size_t N, size_t *values);
523 extern tree gg_array_of_bytes( size_t N, unsigned char *values);
524 extern tree gg_indirect(tree pointer, tree byte_offset = NULL_TREE);
525 extern tree gg_string_literal(const char *string);
526
527 #define CURRENT_LINE_NUMBER (cobol_location().first_line)
528 extern location_t gg_token_location();
529 extern location_t current_token_location();
530 extern location_t current_location_minus_one();
531 extern void current_location_minus_one_clear();
532 extern void token_location_override(location_t loc);
533
534 extern tree gg_trans_unit_var_decl(const char *var_name);
535
536 extern tree gg_open(tree char_star_A, tree int_B);
537 extern tree gg_close(tree int_A);
538 extern tree gg_get_indirect_reference(tree pointer, tree offset);
539
540 extern void gg_insert_into_assembler(const char ach[]);
541 extern void gg_insert_into_assemblerf(const char *format, ...) ATTRIBUTE_PRINTF_1;
542
543 extern char *gg_show_type(tree type);
544 extern void gg_leaving_the_source_code_file();
545 #endif