| 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 |
/* The compilation process consists of |
| 31 |
|
| 32 |
1) lexing |
| 33 |
2) parsing |
| 34 |
3) generation of the GENERIC abstract syntax tree |
| 35 |
4) reduction |
| 36 |
5) generation of machine code |
| 37 |
|
| 38 |
For your sins, you have wandered into the code that accepts information from |
| 39 |
the parser about what the COBOL source code wants done. |
| 40 |
|
| 41 |
Specifically, the routines in this module, which run at compile time, generate |
| 42 |
the GENERIC tags that describe the equivalent of the COBOL. They are rathernnn |
| 43 |
low level routines, ultimately used for pretty much everything. Specifically, |
| 44 |
they run at compile-time, and they generate the GENERIC tags that control what |
| 45 |
ultimately happens at run-time. |
| 46 |
|
| 47 |
It *is* confusing. |
| 48 |
|
| 49 |
I'll try to collect things in a logical way, and name them in a logical way, |
| 50 |
and I'll try to comment them well enough so that you have some hope of |
| 51 |
understanding what the heck is going on. |
| 52 |
|
| 53 |
There is some information in the GCC internals document, but it was written by |
| 54 |
people who live and breathe this stuff, and they don't remember what it was like |
| 55 |
to know nothing. |
| 56 |
|
| 57 |
I suspect that those who have tried and failed to create GCC front ends have foundered because |
| 58 |
they just couldn't figure out what it was they needed to do. I certainly floundered |
| 59 |
for several days before I hit on the means to figure it out. I created the |
| 60 |
rjd_print_tree() routine, which spits out a text listing of all the nodes |
| 61 |
connected to the specified starting node. (Keep in mind that the GENERIC graph |
| 62 |
is cyclic, and consequently there is no real ordering, except that the starting |
| 63 |
node you specify is NodeNumber0. rjd_print_tree follows all links, but it prints |
| 64 |
out each unique node exactly once.) |
| 65 |
|
| 66 |
I then built into GCC a call to rjd_print_tree right at the point where the GENERIC tree |
| 67 |
is complete and about to be reduced. |
| 68 |
|
| 69 |
And that gave me the ability to create simple C programs and see the resulting GENERIC |
| 70 |
tree. It took a while to sort out what I was seeing, but ultimately things started |
| 71 |
to make sense. The inherent difficulty may start to become clear when you realize that |
| 72 |
the program |
| 73 |
|
| 74 |
void foo() |
| 75 |
{ |
| 76 |
} |
| 77 |
|
| 78 |
is implemented by a GENERIC tree with fifty-six nodes. |
| 79 |
|
| 80 |
I can't try to write a whole manual here. But hopefully there will be enough examples |
| 81 |
throughout the code for you to learn how to do things on a highish level, and you can |
| 82 |
look at the low -level routines to see how it is accomplished. |
| 83 |
|
| 84 |
That said, I will try to comment things well enough to be meaningful at least to me |
| 85 |
when I run across them at some time in the future. Because I fear that whatever |
| 86 |
I do here, the world will little note, and *I* will not long remember, what it was! |
| 87 |
*/ |
| 88 |
|
| 89 |
#include "cobol-system.h" |
| 90 |
#include "coretypes.h" |
| 91 |
#include "tree.h" |
| 92 |
#include "tree-iterator.h" |
| 93 |
#include "stringpool.h" |
| 94 |
#include "cgraph.h" |
| 95 |
#include "toplev.h" |
| 96 |
#include "function.h" |
| 97 |
#include "fold-const.h" |
| 98 |
#include "../../libgcobol/ec.h" |
| 99 |
#include "../../libgcobol/common-defs.h" |
| 100 |
#include "util.h" |
| 101 |
#include "cbldiag.h" |
| 102 |
#include "symbols.h" |
| 103 |
#include "gengen.h" |
| 104 |
#include "dumpfile.h" |
| 105 |
|
| 106 |
// We are limiting the programmer to functions with 512 or fewer arguments. |
| 107 |
// Don't like it? Cry me a river. |
| 108 |
static const int ARG_LIMIT = 512; |
| 109 |
|
| 110 |
// These are globally useful constants |
| 111 |
tree char_nodes[256]; |
| 112 |
|
| 113 |
tree pvoid_type_node; |
| 114 |
tree integer_minusone_node; |
| 115 |
tree integer_two_node; |
| 116 |
tree integer_eight_node; |
| 117 |
tree size_t_zero_node; |
| 118 |
tree int128_zero_node; |
| 119 |
tree int128_five_node; |
| 120 |
tree int128_ten_node; |
| 121 |
tree char_ptr_type_node; |
| 122 |
tree uchar_ptr_type_node; |
| 123 |
tree wchar_ptr_type_node; |
| 124 |
tree long_double_ten_node; |
| 125 |
tree sizeof_size_t; |
| 126 |
tree sizeof_pointer; |
| 127 |
|
| 128 |
tree bool_true_node; |
| 129 |
tree bool_false_node; |
| 130 |
|
| 131 |
// This is the global translation unit structure; it contains everything needed |
| 132 |
// to compile one file that you might otherwise be tempted to instantiate as |
| 133 |
// global variables: |
| 134 |
|
| 135 |
struct cbl_translation_unit_t gg_trans_unit; |
| 136 |
|
| 137 |
// This set is used to prevent duplicated top-level program names from breaking |
| 138 |
// the compiler when a source code module makes that mistake. |
| 139 |
static std::unordered_set<std::string> names_we_have_seen; |
| 140 |
|
| 141 |
// This vector is used to process the function_decls at the point we leave |
| 142 |
// the file. |
| 143 |
static std::vector<tree> finalized_function_decls; |
| 144 |
|
| 145 |
void |
| 146 |
gg_build_translation_unit(const char *filename) |
| 147 |
{ |
| 148 |
// The translation_unit_decl gets declared once for each processing source |
| 149 |
// input file. It serves as an anchor for each function. And the |
| 150 |
// block referred to by its "initial" member is the anchor for any |
| 151 |
// variables whose scope is file. |
| 152 |
|
| 153 |
gg_trans_unit.trans_unit_decl |
| 154 |
= build_translation_unit_decl(get_identifier(filename)); |
| 155 |
|
| 156 |
gg_trans_unit.filename = filename; |
| 157 |
|
| 158 |
tree tree_block = make_node(BLOCK); |
| 159 |
BLOCK_SUPERCONTEXT(tree_block) |
| 160 |
= gg_trans_unit.trans_unit_decl; |
| 161 |
TREE_USED(tree_block) = 1; |
| 162 |
DECL_INITIAL(gg_trans_unit.trans_unit_decl) = tree_block; |
| 163 |
} |
| 164 |
|
| 165 |
// Explanation of context. There is a plate of spaghetti that represents |
| 166 |
// a chain of contexts. |
| 167 |
|
| 168 |
// The deconstructed dinner: The function_decl "initial" points to a block |
| 169 |
// The block points to the first of a chained set of var_decl, one for each |
| 170 |
// variable in the block. The function "saved_tree" entry points to a |
| 171 |
// bind_expr. The bind_expr vars member points to the same chain of var_decl. |
| 172 |
// The bind_expr block member points to the block. And the bind_expr body |
| 173 |
// member points to the statement_list for the context. |
| 174 |
|
| 175 |
// Those four tags constitute the context. To push the context, a new block |
| 176 |
// is chained to the first blocks SUBCHAIN member. A new bind_expr is created |
| 177 |
// and put on the statement_list of the enclosing block. And a new list of |
| 178 |
// var_decls is set up for the new block and the new bind_expr. |
| 179 |
|
| 180 |
// And that's how subcontexts are made. |
| 181 |
|
| 182 |
static void |
| 183 |
gg_chain_onto_block_vars(tree block, tree var) |
| 184 |
{ |
| 185 |
// In order to use a variable in a context, the var_decl has to go |
| 186 |
// onto the chain that starts with the "vars" entry of a block |
| 187 |
|
| 188 |
// Upon discovering that chainon has O(N-squared) complexity because it walks |
| 189 |
// the entire chain looking for the final member, Dubner put in this map. |
| 190 |
static std::unordered_map<tree, tree>blocks; |
| 191 |
if( !BLOCK_VARS(block) ) |
| 192 |
{ |
| 193 |
// This is the first variable: |
| 194 |
BLOCK_VARS(block) = var; |
| 195 |
blocks[block] = var; |
| 196 |
} |
| 197 |
else |
| 198 |
{ |
| 199 |
//chainon(BLOCK_VARS(block), var); |
| 200 |
// What follows is the quicker equivalent of calling chainon() |
| 201 |
TREE_CHAIN(blocks[block]) = var; |
| 202 |
blocks[block] = var; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
void |
| 207 |
gg_append_var_decl(tree var_decl) |
| 208 |
{ |
| 209 |
// The var_decl has to be chained onto the appropriate block. |
| 210 |
|
| 211 |
if( SCOPE_FILE_SCOPE_P(DECL_CONTEXT(var_decl)) ) |
| 212 |
{ |
| 213 |
tree context = gg_trans_unit.trans_unit_decl; |
| 214 |
tree block = DECL_INITIAL(context); |
| 215 |
|
| 216 |
gg_chain_onto_block_vars(block, var_decl); |
| 217 |
|
| 218 |
rest_of_decl_compilation (var_decl, true, false); |
| 219 |
|
| 220 |
// With global variables, it is probably necessary to do something with |
| 221 |
// wrapup_global_declarations. At this writing, I have not yet |
| 222 |
// investigated that. The advice from [email protected] came from |
| 223 |
// David Malcolm: |
| 224 |
/* |
| 225 |
You might find libgccjit's gcc/jit/jit-playback.cc helpful for this, as |
| 226 |
it tends to contain minimal code to build trees (generally |
| 227 |
simplified/reverse-engineered from the C frontend). |
| 228 |
|
| 229 |
playback::context::global_new_decl makes the VAR_DECL node, and such |
| 230 |
trees are added to the jit playback::context's m_globals. |
| 231 |
In playback::context::replay, we have: |
| 232 |
|
| 233 |
/ * Finalize globals. See how FORTRAN 95 does it in gfc_be_parse_file() |
| 234 |
for a simple reference. * / |
| 235 |
FOR_EACH_VEC_ELT (m_globals, i, global) |
| 236 |
rest_of_decl_compilation (global, true, true); |
| 237 |
|
| 238 |
wrapup_global_declarations (m_globals.address(), m_globals.length()); |
| 239 |
*/ |
| 240 |
|
| 241 |
// Stash this var_decl in a map so it can be found elsewhere: |
| 242 |
//fprintf(stderr, "Stashing %s\n", IDENTIFIER_POINTER(DECL_NAME(var_decl))); |
| 243 |
gg_trans_unit.trans_unit_var_decls |
| 244 |
[IDENTIFIER_POINTER(DECL_NAME(var_decl))] = var_decl; |
| 245 |
} |
| 246 |
else |
| 247 |
{ |
| 248 |
// For function-level variables, we use a stack of blocks to keep track |
| 249 |
// of which block is active for the current context: |
| 250 |
|
| 251 |
// fprintf(stderr, "%s(): %30s Function Scope\n", __func__, id_name); |
| 252 |
tree bind_expr = current_function->bind_expr_stack.back(); |
| 253 |
tree block = BIND_EXPR_BLOCK(bind_expr); |
| 254 |
|
| 255 |
gg_chain_onto_block_vars(block, var_decl); |
| 256 |
|
| 257 |
// If saved_tree.bind_expr.vars is null, then var_decl is the very |
| 258 |
// first variable in the block, and it must be set in bind_expr as well |
| 259 |
if( !BIND_EXPR_VARS(bind_expr) ) |
| 260 |
{ |
| 261 |
BIND_EXPR_VARS(bind_expr) = var_decl; |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
void |
| 267 |
gg_append_statement(tree stmt) |
| 268 |
{ |
| 269 |
// Likewise, we have a stack of statement_lists, with the current one |
| 270 |
// at the back. (The statement_list stack can get deeper than the block |
| 271 |
// stack, because you can create a separate statement list for the insides |
| 272 |
// of, say, a WHILE statement without creating a whole context for it) |
| 273 |
|
| 274 |
// This statement list thing looks innocent enough, but it is the general |
| 275 |
// way of actually having a GENERIC tree generate executing code. What goes |
| 276 |
// onto a statement list is an expression. A = B is implemented with a |
| 277 |
// modify_expr |
| 278 |
|
| 279 |
// Actually instantiating a variable requires a var_expr |
| 280 |
|
| 281 |
// A subroutine call is effected by putting a call_expr onto the statement |
| 282 |
// list. |
| 283 |
|
| 284 |
// It's not the only way; you can have a modify_expr that takes a var_decl |
| 285 |
// as a destination, and uses a call_expr as a source. This requires that |
| 286 |
// the type of the var_decl be the same as the type of the function being |
| 287 |
// called. |
| 288 |
|
| 289 |
// And so on. Just keep in mind that you have types, and declarations, and |
| 290 |
// expressions, among other things. |
| 291 |
|
| 292 |
// When trying to figure out location_t, take a look at |
| 293 |
// ./libcpp/include/line-map.h |
| 294 |
// ./libcpp/location-example.txt |
| 295 |
|
| 296 |
gcc_assert( gg_trans_unit.function_stack.size() ); |
| 297 |
|
| 298 |
TREE_SIDE_EFFECTS(stmt) = 1; // If an expression has no side effects, |
| 299 |
// // it won't generate code. |
| 300 |
TREE_SIDE_EFFECTS(current_function->statement_list_stack.back()) = 1; |
| 301 |
append_to_statement_list( stmt, &(current_function->statement_list_stack.back()) ); |
| 302 |
} |
| 303 |
|
| 304 |
tree |
| 305 |
gg_float(tree floating_type, tree integer_var) |
| 306 |
{ |
| 307 |
// I don't know why, but this fails if 'var' is an INT128 |
| 308 |
return build1(FLOAT_EXPR, floating_type, integer_var); |
| 309 |
} |
| 310 |
|
| 311 |
tree |
| 312 |
gg_trunc(tree integer_type, tree floating_var) |
| 313 |
{ |
| 314 |
/* Conversion of real to fixed point by truncation. */ |
| 315 |
return build1(FIX_TRUNC_EXPR, integer_type, floating_var); |
| 316 |
} |
| 317 |
|
| 318 |
tree |
| 319 |
gg_cast(tree type, tree var) |
| 320 |
{ |
| 321 |
return fold_convert(type, var); |
| 322 |
} |
| 323 |
|
| 324 |
static bool saw_pointer; |
| 325 |
|
| 326 |
static |
| 327 |
tree |
| 328 |
adjust_for_type(tree type) |
| 329 |
{ |
| 330 |
tree retval; |
| 331 |
|
| 332 |
switch( TREE_CODE(type) ) |
| 333 |
{ |
| 334 |
case POINTER_TYPE: |
| 335 |
saw_pointer = true; |
| 336 |
retval = adjust_for_type(TREE_TYPE(type)); |
| 337 |
break; |
| 338 |
|
| 339 |
case COMPONENT_REF: |
| 340 |
case ADDR_EXPR: |
| 341 |
case ARRAY_TYPE: |
| 342 |
case VAR_DECL: |
| 343 |
case FUNCTION_TYPE: |
| 344 |
retval = adjust_for_type(TREE_TYPE(type)); |
| 345 |
break; |
| 346 |
case RECORD_TYPE: |
| 347 |
default: |
| 348 |
retval = type; |
| 349 |
break; |
| 350 |
} |
| 351 |
|
| 352 |
return retval; |
| 353 |
} |
| 354 |
|
| 355 |
char * |
| 356 |
gg_show_type(tree type) |
| 357 |
{ |
| 358 |
if( !type ) |
| 359 |
{ |
| 360 |
cbl_internal_error("The given type is NULL, and that is just not fair"); |
| 361 |
} |
| 362 |
|
| 363 |
if( DECL_P(type) ) |
| 364 |
{ |
| 365 |
type = TREE_TYPE(type); |
| 366 |
} |
| 367 |
if( !TYPE_P(type) ) |
| 368 |
{ |
| 369 |
cbl_internal_error("The given type is not a declaration or a TYPE"); |
| 370 |
} |
| 371 |
|
| 372 |
static char ach[1100]; |
| 373 |
static char ach2[1024]; |
| 374 |
static char ach3[1024]; |
| 375 |
switch( TREE_CODE(type) ) |
| 376 |
{ |
| 377 |
case POINTER_TYPE: |
| 378 |
strcpy(ach2, gg_show_type(TREE_TYPE(type))); |
| 379 |
sprintf(ach, "POINTER to %s", ach2); |
| 380 |
break; |
| 381 |
|
| 382 |
case VOID_TYPE: |
| 383 |
sprintf(ach, "VOID"); |
| 384 |
break; |
| 385 |
|
| 386 |
case BOOLEAN_TYPE: |
| 387 |
sprintf(ach, "BOOL"); |
| 388 |
break; |
| 389 |
|
| 390 |
case RECORD_TYPE: |
| 391 |
sprintf(ach, "RECORD"); |
| 392 |
break; |
| 393 |
|
| 394 |
case REAL_TYPE: |
| 395 |
sprintf(ach, |
| 396 |
"%3" PRId64 "-bit REAL", |
| 397 |
TREE_INT_CST_LOW(TYPE_SIZE(type))); |
| 398 |
break; |
| 399 |
|
| 400 |
case INTEGER_TYPE: |
| 401 |
sprintf(ach, |
| 402 |
"%3" PRId64 "-bit %s INT", |
| 403 |
TREE_INT_CST_LOW(TYPE_SIZE(type)), |
| 404 |
(TYPE_UNSIGNED(type) ? "unsigned" : " signed")); |
| 405 |
break; |
| 406 |
|
| 407 |
case FUNCTION_TYPE: |
| 408 |
strcpy(ach3, gg_show_type(TREE_TYPE(type))); |
| 409 |
sprintf(ach, "FUNCTION returning %s", ach3); |
| 410 |
break; |
| 411 |
|
| 412 |
default: |
| 413 |
cbl_internal_error("Unknown type %d", TREE_CODE(type)); |
| 414 |
} |
| 415 |
|
| 416 |
return ach; |
| 417 |
} |
| 418 |
|
| 419 |
tree |
| 420 |
gg_assign(tree dest, const tree source) |
| 421 |
{ |
| 422 |
// This does the equivalent of a C/C++ "dest = source". When X1 is set, it |
| 423 |
// does some checking for conditions that can result in inefficient code, so |
| 424 |
// that is useful during development when even an astute programmer might |
| 425 |
// need an assist with keeping variable types straight. |
| 426 |
|
| 427 |
// This routine also provides for the possibility that the assignment is |
| 428 |
// for a source that is a function invocation, as in |
| 429 |
// "dest = function_call()" |
| 430 |
tree stmt = NULL_TREE; |
| 431 |
|
| 432 |
saw_pointer = false; |
| 433 |
tree dest_type = adjust_for_type(TREE_TYPE(dest)); |
| 434 |
saw_pointer = false; |
| 435 |
tree source_type = adjust_for_type(TREE_TYPE(source)); |
| 436 |
bool p2 = saw_pointer; |
| 437 |
|
| 438 |
bool okay = dest_type == source_type; |
| 439 |
|
| 440 |
if( !okay ) |
| 441 |
{ |
| 442 |
if( TREE_CODE(dest_type) == INTEGER_TYPE |
| 443 |
&& TREE_CODE(source_type) == INTEGER_TYPE |
| 444 |
&& TREE_INT_CST_LOW(TYPE_SIZE(dest_type)) == TREE_INT_CST_LOW(TYPE_SIZE(source_type)) |
| 445 |
&& TYPE_UNSIGNED(dest_type) == TYPE_UNSIGNED(source_type) ) |
| 446 |
{ |
| 447 |
okay = true; |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
if( okay ) |
| 452 |
{ |
| 453 |
stmt = build2_loc(gg_token_location(), |
| 454 |
MODIFY_EXPR, |
| 455 |
TREE_TYPE(dest), |
| 456 |
dest, |
| 457 |
source); |
| 458 |
gg_append_statement(stmt); |
| 459 |
} |
| 460 |
else |
| 461 |
{ |
| 462 |
// We are doing an assignment where the left- and right-hand types are not |
| 463 |
// the same. This is a compilation-time error, since we want the caller to |
| 464 |
// have sorted the types out explicitly. If we don't throw an error here, |
| 465 |
// the gimple reduction will do so. Better to do it here, when we know |
| 466 |
// where we are.S |
| 467 |
static const int debugging = 1; |
| 468 |
if( debugging ) |
| 469 |
{ |
| 470 |
fprintf(stderr, "Inefficient assignment\n"); |
| 471 |
if(DECL_P(dest) && DECL_NAME(dest)) |
| 472 |
{ |
| 473 |
fprintf(stderr, " Destination is %s\n", IDENTIFIER_POINTER(DECL_NAME(dest))); |
| 474 |
} |
| 475 |
fprintf(stderr, " dest type is %s%s\n", gg_show_type(dest_type), p2 ? "_P" : ""); |
| 476 |
if(DECL_P(source) && DECL_NAME(source)) |
| 477 |
{ |
| 478 |
fprintf(stderr, " Source is %s\n", IDENTIFIER_POINTER(DECL_NAME(source))); |
| 479 |
} |
| 480 |
fprintf(stderr, " source type is %s%s\n", gg_show_type(source_type), p2 ? "_P" : ""); |
| 481 |
} |
| 482 |
cbl_internal_error("Attempting an assignment of differing types."); |
| 483 |
} |
| 484 |
return stmt; |
| 485 |
} |
| 486 |
|
| 487 |
tree |
| 488 |
gg_find_field_in_struct(const tree base, const char *field_name) |
| 489 |
{ |
| 490 |
// Finds and returns the field_decl for the named member. 'base' can be |
| 491 |
// a structure or a pointer to a structure. |
| 492 |
tree type = TREE_TYPE(base); |
| 493 |
tree rectype; |
| 494 |
if( POINTER_TYPE_P (type) ) |
| 495 |
{ |
| 496 |
tree pointer_type = TREE_TYPE(base); |
| 497 |
rectype = TREE_TYPE(pointer_type); |
| 498 |
} |
| 499 |
else |
| 500 |
{ |
| 501 |
// Assuming a struct (or union), pick up the record_type |
| 502 |
rectype = TREE_TYPE(base); |
| 503 |
} |
| 504 |
|
| 505 |
tree id_of_field = get_identifier(field_name); |
| 506 |
|
| 507 |
tree field_decl = NULL_TREE; |
| 508 |
|
| 509 |
tree next_value = TYPE_FIELDS(rectype); |
| 510 |
|
| 511 |
// Look through the chain of fields for a match to ours. This is, in the |
| 512 |
// limit, an O(N^2) computational burden. But structures usually small, so we |
| 513 |
// probably don't have to figure out how to make it faster. |
| 514 |
while( next_value ) |
| 515 |
{ |
| 516 |
if( DECL_NAME(next_value) == id_of_field ) |
| 517 |
{ |
| 518 |
field_decl = next_value; |
| 519 |
break; |
| 520 |
} |
| 521 |
next_value = TREE_CHAIN(next_value); |
| 522 |
} |
| 523 |
|
| 524 |
if( !field_decl ) |
| 525 |
{ |
| 526 |
yywarn("Somebody asked for the field %s.%s, which does not exist", |
| 527 |
IDENTIFIER_POINTER(DECL_NAME(base)), |
| 528 |
field_name); |
| 529 |
gcc_unreachable(); |
| 530 |
} |
| 531 |
|
| 532 |
return field_decl; |
| 533 |
} |
| 534 |
|
| 535 |
static tree |
| 536 |
gg_start_building_a_union(const char *type_name, tree type_context) |
| 537 |
{ |
| 538 |
// type_context is current_function->function_decl for union local |
| 539 |
// to a function. |
| 540 |
|
| 541 |
// It is translation_unit_decl for unions common to all functions |
| 542 |
|
| 543 |
// We want to return the type_decl for an empty union |
| 544 |
|
| 545 |
// First, create the record_type whose values will eventually |
| 546 |
// be the chain of of the struct's fields: |
| 547 |
|
| 548 |
tree uniontype = make_node(UNION_TYPE); |
| 549 |
TYPE_CONTEXT(uniontype) = type_context; |
| 550 |
TYPE_SIZE_UNIT(uniontype) = integer_zero_node; |
| 551 |
TYPE_SIZE(uniontype) = integer_zero_node; |
| 552 |
TYPE_NAME(uniontype) = get_identifier(type_name); |
| 553 |
|
| 554 |
TYPE_MODE_RAW(uniontype) = TYPE_MODE (intTI_type_node); |
| 555 |
|
| 556 |
// We need a type_decl for the record_type: |
| 557 |
tree typedecl = make_node(TYPE_DECL); |
| 558 |
|
| 559 |
// The type of the type_decl is the record_type: |
| 560 |
TREE_TYPE(typedecl) = uniontype; |
| 561 |
|
| 562 |
SET_TYPE_ALIGN(uniontype, 16); |
| 563 |
|
| 564 |
// The chain element of the record_type points back to the type_decl: |
| 565 |
TREE_CHAIN(uniontype) = typedecl; |
| 566 |
|
| 567 |
return typedecl; |
| 568 |
} |
| 569 |
|
| 570 |
static tree |
| 571 |
gg_start_building_a_struct(const char *type_name, tree type_context) |
| 572 |
{ |
| 573 |
// type_context is current_function->function_decl for structures local |
| 574 |
// to a function. |
| 575 |
|
| 576 |
// It is translation_unit_decl for structures common to all functions |
| 577 |
|
| 578 |
// We want to return the type_decl for an empty struct |
| 579 |
|
| 580 |
// First, create the record_type whose values will eventually |
| 581 |
// be the chain of of the struct's fields: |
| 582 |
|
| 583 |
tree recordtype = make_node(RECORD_TYPE); |
| 584 |
TYPE_CONTEXT(recordtype) = type_context; |
| 585 |
TYPE_SIZE_UNIT(recordtype) = integer_zero_node; |
| 586 |
TYPE_SIZE(recordtype) = integer_zero_node; |
| 587 |
TYPE_NAME(recordtype) = get_identifier(type_name); |
| 588 |
|
| 589 |
TYPE_MODE_RAW(recordtype) = BLKmode; |
| 590 |
|
| 591 |
// We need a type_decl for the record_type: |
| 592 |
tree typedecl = make_node(TYPE_DECL); |
| 593 |
|
| 594 |
// The type of the type_decl is the record_type: |
| 595 |
TREE_TYPE(typedecl) = recordtype; |
| 596 |
|
| 597 |
SET_TYPE_ALIGN(recordtype, 8); |
| 598 |
|
| 599 |
// The chain element of the record_type points back to the type_decl: |
| 600 |
TREE_CHAIN(recordtype) = typedecl; |
| 601 |
|
| 602 |
return typedecl; |
| 603 |
} |
| 604 |
|
| 605 |
static void |
| 606 |
gg_add_field_to_structure(const tree type_of_field, const char *name_of_field, tree struct_type_decl) |
| 607 |
{ |
| 608 |
// We're given the struct_type_decl. |
| 609 |
// Append the new field to that type_decl's record_type's chain: |
| 610 |
tree struct_record_type = TREE_TYPE(struct_type_decl); |
| 611 |
|
| 612 |
bool is_union = TREE_CODE((struct_record_type)) == UNION_TYPE; |
| 613 |
|
| 614 |
tree id_of_field = get_identifier (name_of_field); |
| 615 |
|
| 616 |
// Create the new field: |
| 617 |
tree new_field_decl = build_decl( gg_token_location(), |
| 618 |
FIELD_DECL, |
| 619 |
id_of_field, |
| 620 |
type_of_field); |
| 621 |
|
| 622 |
// Establish the machine mode for the field_decl: |
| 623 |
SET_DECL_MODE(new_field_decl, TYPE_MODE(type_of_field)); |
| 624 |
|
| 625 |
// Establish the context of the new field as being the record_type |
| 626 |
DECL_CONTEXT (new_field_decl) = struct_record_type; |
| 627 |
|
| 628 |
// Establish the size of the new field as being the same as its prototype: |
| 629 |
DECL_SIZE(new_field_decl) = TYPE_SIZE(type_of_field); // This is in bits |
| 630 |
DECL_SIZE_UNIT(new_field_decl) = TYPE_SIZE_UNIT(type_of_field); // This is in bytes |
| 631 |
|
| 632 |
// We need to establish the offset and bit offset of the new node. |
| 633 |
// Empirically, this seems to be done on 16-bit boundaries, with DECL_FIELD_OFFSET |
| 634 |
// in units of N*16 bytes, and FIELD_BIT_OFFSET being offsets in bits from the DECL_FIELD_OFFSET |
| 635 |
|
| 636 |
// We calculate our desired offset in bits: |
| 637 |
|
| 638 |
// Pick up the current size, in bytes, of the record_type: |
| 639 |
long offset_in_bytes = TREE_INT_CST_LOW(TYPE_SIZE_UNIT(struct_record_type)); |
| 640 |
|
| 641 |
static const int MAGIC_NUMBER_SIXTEEN = 16 ; |
| 642 |
static const int BITS_IN_A_BYTE = 8 ; |
| 643 |
|
| 644 |
// We know the offset_in_bytes, which is the size, of the structure with |
| 645 |
// its current members. |
| 646 |
|
| 647 |
//long type_size = TREE_INT_CST_LOW(TYPE_SIZE_UNIT(type_of_field)); |
| 648 |
long type_align_in_bits = TYPE_ALIGN(type_of_field); |
| 649 |
long type_align_in_bytes = type_align_in_bits/BITS_IN_A_BYTE; |
| 650 |
|
| 651 |
// As per the Amd64 ABI, we need to set the structure's type alignment to be |
| 652 |
// that of most strictly aligned component: |
| 653 |
// This is the current restriction: |
| 654 |
long struct_align_in_bits = TYPE_ALIGN(TREE_TYPE(struct_type_decl)); |
| 655 |
if( type_align_in_bits > struct_align_in_bits ) |
| 656 |
{ |
| 657 |
// The new one is the new champion |
| 658 |
SET_TYPE_ALIGN(TREE_TYPE(struct_type_decl), type_align_in_bits ); |
| 659 |
} |
| 660 |
|
| 661 |
// We know struct_type_decl is a record_type, so we can sneak through this comparison |
| 662 |
if( type_of_field == TREE_TYPE(struct_type_decl) ) |
| 663 |
{ |
| 664 |
printf(" It is a record_type\n"); |
| 665 |
} |
| 666 |
|
| 667 |
// Bump up the offset until we are aligned: |
| 668 |
while( offset_in_bytes % type_align_in_bytes) |
| 669 |
{ |
| 670 |
offset_in_bytes += 1; |
| 671 |
} |
| 672 |
|
| 673 |
if( is_union ) |
| 674 |
{ |
| 675 |
// Turn that into the bytes/bits offsets of the new field: |
| 676 |
DECL_FIELD_OFFSET(new_field_decl) = build_int_cst_type (SIZE_T, 0); |
| 677 |
DECL_FIELD_BIT_OFFSET(new_field_decl) = build_int_cst_type (bitsizetype, 0); |
| 678 |
|
| 679 |
// The size of a union is the size of its largest member: |
| 680 |
offset_in_bytes = std::max(offset_in_bytes, (long)TREE_INT_CST_LOW(DECL_SIZE_UNIT(new_field_decl))); |
| 681 |
} |
| 682 |
else |
| 683 |
{ |
| 684 |
// Turn that into the bytes/bits offsets of the new field: |
| 685 |
long field_offset = (offset_in_bytes/MAGIC_NUMBER_SIXTEEN)*MAGIC_NUMBER_SIXTEEN; |
| 686 |
long field_bit_offset = (offset_in_bytes - field_offset) * BITS_IN_A_BYTE; |
| 687 |
DECL_FIELD_OFFSET(new_field_decl) = build_int_cst_type (SIZE_T, field_offset);; |
| 688 |
DECL_FIELD_BIT_OFFSET(new_field_decl) = build_int_cst_type (bitsizetype, field_bit_offset); |
| 689 |
|
| 690 |
// This was done empirically to make our generated code match that of a C program |
| 691 |
SET_DECL_OFFSET_ALIGN(new_field_decl, 128); |
| 692 |
|
| 693 |
// And now we need to update the size of the record type: |
| 694 |
offset_in_bytes += TREE_INT_CST_LOW(DECL_SIZE_UNIT(new_field_decl)); |
| 695 |
} |
| 696 |
|
| 697 |
TYPE_SIZE_UNIT(struct_record_type) = build_int_cst_type (SIZE_T, offset_in_bytes); // In bytes |
| 698 |
TYPE_SIZE(struct_record_type) = build_int_cst_type (bitsizetype, offset_in_bytes*BITS_IN_A_BYTE); // In bits |
| 699 |
|
| 700 |
if( !TYPE_FIELDS(struct_record_type) ) |
| 701 |
{ |
| 702 |
// This is the first variable of the chain: |
| 703 |
TYPE_FIELDS(struct_record_type) = new_field_decl; |
| 704 |
} |
| 705 |
else |
| 706 |
{ |
| 707 |
// We need to tack the new one onto an already existing chain: |
| 708 |
chainon(TYPE_FIELDS(struct_record_type), new_field_decl); |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
void |
| 713 |
gg_get_struct_type_decl(tree struct_type_decl, int count, va_list params) |
| 714 |
{ |
| 715 |
while( count-- ) |
| 716 |
{ |
| 717 |
tree field_type = va_arg(params, tree); |
| 718 |
const char *name = va_arg(params, const char *); |
| 719 |
gg_add_field_to_structure(field_type, name, struct_type_decl); |
| 720 |
} |
| 721 |
// Note: On 2022-02-18 I removed the call to gg_append_var_decl, which |
| 722 |
// chains the type_decl on the function block. I don't remember why I |
| 723 |
// thought it was necessary. It makes no difference for COBOL compilations. |
| 724 |
// |
| 725 |
// But I must have copied it from a C compilation example. |
| 726 |
// |
| 727 |
// I removed it so that I could create type_decls outside of a function. |
| 728 |
// I know not what the long-term implications might be. |
| 729 |
// |
| 730 |
// You have been served notice. |
| 731 |
// |
| 732 |
// struct_type_decl is the type_decl for our structure. We need to |
| 733 |
// append it to the list of variables in order to use it: |
| 734 |
// The following function call is misnamed. It can take struct type_decls |
| 735 |
//gg_append_var_decl(struct_type_decl); |
| 736 |
} |
| 737 |
|
| 738 |
void |
| 739 |
gg_get_union_type_decl(tree union_type_decl, int count, va_list params) |
| 740 |
{ |
| 741 |
while( count-- ) |
| 742 |
{ |
| 743 |
tree field_type = va_arg(params, tree); |
| 744 |
const char *name = va_arg(params, const char *); |
| 745 |
gg_add_field_to_structure(field_type, name, union_type_decl); |
| 746 |
} |
| 747 |
} |
| 748 |
|
| 749 |
tree |
| 750 |
gg_get_local_struct_type_decl(const char *type_name, int count, ...) |
| 751 |
{ |
| 752 |
tree struct_type_decl = gg_start_building_a_struct(type_name, current_function->function_decl); |
| 753 |
|
| 754 |
va_list params; |
| 755 |
va_start(params, count); |
| 756 |
|
| 757 |
gg_get_struct_type_decl(struct_type_decl, count, params); |
| 758 |
|
| 759 |
va_end(params); |
| 760 |
|
| 761 |
// To use the struct_type_decl, you'll need to execute |
| 762 |
// the following to turn it into a var_decl: |
| 763 |
// tree var_decl = gg_define_variable( TREE_TYPE(struct_type_decl), |
| 764 |
// var_name, |
| 765 |
// vs_static); |
| 766 |
return struct_type_decl; |
| 767 |
} |
| 768 |
|
| 769 |
tree |
| 770 |
gg_get_filelevel_struct_type_decl(const char *type_name, int count, ...) |
| 771 |
{ |
| 772 |
tree struct_type_decl = gg_start_building_a_struct(type_name, gg_trans_unit.trans_unit_decl); |
| 773 |
|
| 774 |
va_list params; |
| 775 |
va_start(params, count); |
| 776 |
|
| 777 |
gg_get_struct_type_decl(struct_type_decl, count, params); |
| 778 |
|
| 779 |
va_end(params); |
| 780 |
|
| 781 |
// To use the struct_type_decl, you'll need to execute |
| 782 |
// the following to turn it into a var_decl: |
| 783 |
// tree var_decl = gg_define_variable( TREE_TYPE(struct_type_decl), |
| 784 |
// var_name, |
| 785 |
// vs_static); |
| 786 |
return struct_type_decl; |
| 787 |
} |
| 788 |
|
| 789 |
tree |
| 790 |
gg_get_filelevel_union_type_decl(const char *type_name, int count, ...) |
| 791 |
{ |
| 792 |
tree struct_type_decl = gg_start_building_a_union(type_name, gg_trans_unit.trans_unit_decl); |
| 793 |
|
| 794 |
va_list params; |
| 795 |
va_start(params, count); |
| 796 |
|
| 797 |
gg_get_union_type_decl(struct_type_decl, count, params); |
| 798 |
|
| 799 |
va_end(params); |
| 800 |
|
| 801 |
// To use the struct_type_decl, you'll need to execute |
| 802 |
// the following to turn it into a var_decl: |
| 803 |
// tree var_decl = gg_define_variable( TREE_TYPE(struct_type_decl), |
| 804 |
// var_name, |
| 805 |
// vs_static); |
| 806 |
return struct_type_decl; |
| 807 |
} |
| 808 |
|
| 809 |
tree |
| 810 |
gg_define_local_struct(const char *type_name, const char * var_name, int count, ...) |
| 811 |
{ |
| 812 |
// Builds a structure, declares it as a static variable in the current function, |
| 813 |
// and returns the var_decl for it. |
| 814 |
tree struct_type_decl = gg_start_building_a_struct(type_name, current_function->function_decl); |
| 815 |
|
| 816 |
va_list params; |
| 817 |
va_start(params, count); |
| 818 |
|
| 819 |
gg_get_struct_type_decl(struct_type_decl, count, params); |
| 820 |
|
| 821 |
va_end(params); |
| 822 |
// We now have a complete struct_type_decl, whose TREE_TYPE is the |
| 823 |
// the type we need when declaring it. |
| 824 |
|
| 825 |
// And with that done, we can actually define the storage: |
| 826 |
tree var_decl = gg_define_variable( TREE_TYPE(struct_type_decl), |
| 827 |
var_name, |
| 828 |
vs_static); |
| 829 |
return var_decl; |
| 830 |
} |
| 831 |
|
| 832 |
tree |
| 833 |
gg_struct_field_ref(const tree base, const char *field) |
| 834 |
{ |
| 835 |
tree retval; |
| 836 |
|
| 837 |
tree type = TREE_TYPE(base); |
| 838 |
if( POINTER_TYPE_P (type) ) |
| 839 |
{ |
| 840 |
tree pointer_type = TREE_TYPE(base); |
| 841 |
tree base_pointer_type = TREE_TYPE(pointer_type); |
| 842 |
// We need a COMPONENT_REF which is an INDIRECT_REF to a FIELD_DECL |
| 843 |
tree field_decl = gg_find_field_in_struct(base, field); |
| 844 |
tree indirect_ref = build1(INDIRECT_REF, base_pointer_type, base); |
| 845 |
retval = build3(COMPONENT_REF, |
| 846 |
TREE_TYPE(field_decl), |
| 847 |
indirect_ref, |
| 848 |
field_decl, |
| 849 |
NULL_TREE); |
| 850 |
} |
| 851 |
else |
| 852 |
{ |
| 853 |
// It's not a pointer, so presumably it's a structure |
| 854 |
tree field_decl = gg_find_field_in_struct(base, field); |
| 855 |
retval = build3(COMPONENT_REF, |
| 856 |
TREE_TYPE(field_decl), |
| 857 |
base, |
| 858 |
field_decl, |
| 859 |
NULL_TREE); |
| 860 |
} |
| 861 |
return retval; |
| 862 |
} |
| 863 |
|
| 864 |
tree |
| 865 |
gg_assign_to_structure(tree var_decl_struct, const char *field, const tree source) |
| 866 |
{ |
| 867 |
// The C equivalent: "struct.field = source" |
| 868 |
tree component_ref = gg_struct_field_ref(var_decl_struct,field); |
| 869 |
gg_assign(component_ref,source); |
| 870 |
return component_ref; |
| 871 |
} |
| 872 |
|
| 873 |
tree |
| 874 |
gg_assign_to_structure(tree var_decl_struct, const char *field, int N) |
| 875 |
{ |
| 876 |
// The C equivalent: "struct.field = N" |
| 877 |
tree component_ref = gg_struct_field_ref(var_decl_struct,field); |
| 878 |
gg_assign(component_ref,build_int_cst(integer_type_node, N)); |
| 879 |
return component_ref; |
| 880 |
} |
| 881 |
|
| 882 |
static tree |
| 883 |
gg_create_assembler_name(const char *cobol_name) |
| 884 |
{ |
| 885 |
char *psz = cobol_name_mangler(cobol_name); |
| 886 |
tree retval = get_identifier(psz); |
| 887 |
free(psz); |
| 888 |
return retval; |
| 889 |
} |
| 890 |
|
| 891 |
static char * |
| 892 |
gg_unique_in_function(const char *var_name, gg_variable_scope_t vs_scope) |
| 893 |
{ |
| 894 |
char *retval = static_cast<char *>(xmalloc(strlen(var_name)+32)); |
| 895 |
if( (vs_scope == vs_stack || vs_scope == vs_static) ) |
| 896 |
{ |
| 897 |
sprintf(retval, "%s." HOST_SIZE_T_PRINT_DEC, var_name, |
| 898 |
(fmt_size_t)current_function->program_id_number); |
| 899 |
} |
| 900 |
else |
| 901 |
{ |
| 902 |
strcpy(retval, var_name); |
| 903 |
} |
| 904 |
return retval; |
| 905 |
} |
| 906 |
|
| 907 |
tree |
| 908 |
gg_declare_variable(tree type_decl, |
| 909 |
const char *name, |
| 910 |
tree initial_value, |
| 911 |
gg_variable_scope_t vs_scope, |
| 912 |
bool *already_defined) |
| 913 |
{ |
| 914 |
// The C/C++ language provides the concept of a *declaration*, which is a |
| 915 |
// prototype for a variable or function. "extern int global_var" is a |
| 916 |
// declaration. Declarations let the compiler know what kind of variable it |
| 917 |
// is looking for so that it can know what to do with it when it is found. |
| 918 |
// |
| 919 |
// A *definition* causes the assembler to actually create data storage for |
| 920 |
// the specified var_decl. |
| 921 |
// |
| 922 |
// Be it hereby known that the various attributes associated with a var_decl, |
| 923 |
// things like TREE_PUBLIC and TREE_STATIC and TREE_CONST seem to line up with |
| 924 |
// their meanings in the C language. But I haven't investigated it enough to |
| 925 |
// be completely sure about that. A hard look at gcc/tree.h is on my list of |
| 926 |
// homework assignments. In the meantime, I continue to learn by compiling |
| 927 |
// C programs with the fdump-generic-nodes option, and copying them as |
| 928 |
// necessary to accomplish specific tasks. |
| 929 |
// |
| 930 |
// Specifically, this routine creates and returns a VAR_DECL, which is the |
| 931 |
// prototype. |
| 932 |
// |
| 933 |
// The gg_define_variable() routines take a VAR_DECL and create a DECL_EXPR |
| 934 |
// node from it. When that DECL_EXPR is appended to the statement list, it |
| 935 |
// causes the storage to be allocated. |
| 936 |
|
| 937 |
// It is routine to let the compiler assign names to stack variables. The |
| 938 |
// assembly code does not use names for variables on the stack; they are |
| 939 |
// referenced by offsets to the base pointer. But static variables have to |
| 940 |
// have names, and there are places in my code generation -- Lord only knows |
| 941 |
// why -- where I didn't give the variables explicit names. We remedy that |
| 942 |
// here: |
| 943 |
|
| 944 |
static std::map<std::string, tree>seen; |
| 945 |
|
| 946 |
tree var_name = NULL_TREE; |
| 947 |
tree var_decl; |
| 948 |
// Assume that for an external reference we know what we want: |
| 949 |
char *unique_name = NULL; |
| 950 |
if( name ) |
| 951 |
{ |
| 952 |
// We were provided a name |
| 953 |
unique_name = gg_unique_in_function(name, vs_scope); |
| 954 |
var_name = get_identifier(unique_name); |
| 955 |
std::map<std::string, tree>::const_iterator it = seen.find(unique_name); |
| 956 |
if( it != seen.end() ) |
| 957 |
{ |
| 958 |
// We've seen this one before |
| 959 |
var_decl = it->second; |
| 960 |
if( already_defined ) |
| 961 |
{ |
| 962 |
*already_defined = true; |
| 963 |
} |
| 964 |
} |
| 965 |
else |
| 966 |
{ |
| 967 |
var_decl = build_decl(UNKNOWN_LOCATION, |
| 968 |
VAR_DECL, |
| 969 |
var_name, |
| 970 |
type_decl); |
| 971 |
} |
| 972 |
} |
| 973 |
else |
| 974 |
{ |
| 975 |
// We were not provided a name, so we have to create one. |
| 976 |
if( vs_scope == vs_static ) |
| 977 |
{ |
| 978 |
// static variables have to have names: |
| 979 |
static int counter = 1; |
| 980 |
char ach[32]; |
| 981 |
sprintf(ach, "__unnamed_static_variable_%d", counter++); |
| 982 |
var_name = get_identifier(ach); |
| 983 |
} |
| 984 |
var_decl = build_decl(UNKNOWN_LOCATION, |
| 985 |
VAR_DECL, |
| 986 |
var_name, |
| 987 |
type_decl); |
| 988 |
} |
| 989 |
switch(vs_scope) |
| 990 |
{ |
| 991 |
case vs_stack: |
| 992 |
// This is a stack variable |
| 993 |
DECL_CONTEXT(var_decl) = current_function->function_decl; |
| 994 |
break; |
| 995 |
case vs_static: |
| 996 |
// This is a function-level static variable |
| 997 |
DECL_CONTEXT(var_decl) = current_function->function_decl; |
| 998 |
TREE_STATIC(var_decl) = 1; |
| 999 |
break; |
| 1000 |
case vs_file_static: |
| 1001 |
// File static variables have translation_unit_scope. I have chosen to |
| 1002 |
// provide access to them through a map; see gg_trans_unit_var_decl(); |
| 1003 |
// TREE_STATIC seems to imply const. |
| 1004 |
DECL_CONTEXT (var_decl) = gg_trans_unit.trans_unit_decl; |
| 1005 |
TREE_STATIC(var_decl) = 1; |
| 1006 |
break; |
| 1007 |
case vs_file: |
| 1008 |
// File variables have translation_unit_scope. |
| 1009 |
// When TREE_STATIC is on, they seem to get put into the .text section |
| 1010 |
DECL_CONTEXT (var_decl) = gg_trans_unit.trans_unit_decl; |
| 1011 |
break; |
| 1012 |
case vs_external: |
| 1013 |
// This is for defining variables with global scope |
| 1014 |
DECL_CONTEXT (var_decl) = gg_trans_unit.trans_unit_decl; |
| 1015 |
TREE_USED(var_decl) = 1; |
| 1016 |
TREE_STATIC(var_decl) = 1; |
| 1017 |
TREE_PUBLIC(var_decl) = 1; |
| 1018 |
seen[unique_name] = var_decl; |
| 1019 |
break; |
| 1020 |
case vs_external_reference: |
| 1021 |
// This is for referencing variables defined elsewhere |
| 1022 |
DECL_CONTEXT (var_decl) = gg_trans_unit.trans_unit_decl; |
| 1023 |
TREE_USED(var_decl) = 1; |
| 1024 |
DECL_EXTERNAL (var_decl) = 1; |
| 1025 |
TREE_PUBLIC(var_decl) = 1; |
| 1026 |
break; |
| 1027 |
} |
| 1028 |
DECL_INITIAL(var_decl) = initial_value; |
| 1029 |
free(unique_name); |
| 1030 |
return var_decl; |
| 1031 |
} |
| 1032 |
|
| 1033 |
tree |
| 1034 |
gg_define_from_declaration(tree var_decl) |
| 1035 |
{ |
| 1036 |
// Append the var_decl to either the chain for the current function or for |
| 1037 |
// the translation_unit, depending on the var_decl's context: |
| 1038 |
gg_append_var_decl(var_decl); |
| 1039 |
|
| 1040 |
if( !SCOPE_FILE_SCOPE_P(DECL_CONTEXT(var_decl)) ) |
| 1041 |
{ |
| 1042 |
// Having made sure the chain of variable declarations is nicely started, |
| 1043 |
// it's time to actually define the storage with a decl_expression: |
| 1044 |
tree stmt = build1_loc (gg_token_location(), |
| 1045 |
DECL_EXPR, |
| 1046 |
TREE_TYPE(var_decl), |
| 1047 |
var_decl); |
| 1048 |
gg_append_statement(stmt); |
| 1049 |
} |
| 1050 |
|
| 1051 |
// And we are done. That variable is now available for computation. |
| 1052 |
return var_decl; |
| 1053 |
} |
| 1054 |
|
| 1055 |
tree |
| 1056 |
gg_define_variable(tree type_decl) |
| 1057 |
{ |
| 1058 |
tree var_decl = gg_declare_variable(type_decl); |
| 1059 |
gg_define_from_declaration(var_decl); |
| 1060 |
return var_decl; |
| 1061 |
} |
| 1062 |
|
| 1063 |
tree |
| 1064 |
gg_define_variable(tree type_decl, tree initial_value) |
| 1065 |
{ |
| 1066 |
tree var_decl = gg_declare_variable(type_decl, |
| 1067 |
NULL, |
| 1068 |
gg_cast(type_decl, initial_value), |
| 1069 |
vs_stack); |
| 1070 |
gg_define_from_declaration(var_decl); |
| 1071 |
return var_decl; |
| 1072 |
} |
| 1073 |
|
| 1074 |
tree |
| 1075 |
gg_define_variable(tree type_decl, gg_variable_scope_t vs_scope) |
| 1076 |
{ |
| 1077 |
tree var_decl = gg_declare_variable(type_decl, NULL, NULL_TREE, vs_scope); |
| 1078 |
gg_define_from_declaration(var_decl); |
| 1079 |
return var_decl; |
| 1080 |
} |
| 1081 |
|
| 1082 |
tree |
| 1083 |
gg_define_variable( tree type_decl, |
| 1084 |
const char *var_name, |
| 1085 |
gg_variable_scope_t vs_scope, |
| 1086 |
tree initial_value) |
| 1087 |
{ |
| 1088 |
tree var_decl = gg_declare_variable(type_decl, var_name, initial_value, vs_scope); |
| 1089 |
gg_define_from_declaration(var_decl); |
| 1090 |
return var_decl; |
| 1091 |
} |
| 1092 |
|
| 1093 |
tree |
| 1094 |
gg_define_variable(tree type_decl, const char *name, gg_variable_scope_t vs_scope) |
| 1095 |
{ |
| 1096 |
bool already_defined = false; |
| 1097 |
tree var_decl = gg_declare_variable(type_decl, name, NULL_TREE, vs_scope, &already_defined); |
| 1098 |
if( !already_defined ) |
| 1099 |
{ |
| 1100 |
gg_define_from_declaration(var_decl); |
| 1101 |
} |
| 1102 |
return var_decl; |
| 1103 |
} |
| 1104 |
|
| 1105 |
tree |
| 1106 |
gg_define_bool() |
| 1107 |
{ |
| 1108 |
tree var_decl = gg_declare_variable(BOOL); |
| 1109 |
gg_define_from_declaration(var_decl); |
| 1110 |
return var_decl; |
| 1111 |
} |
| 1112 |
|
| 1113 |
tree |
| 1114 |
gg_define_char() |
| 1115 |
{ |
| 1116 |
// The nearest C equivalent: "char name;", but this one is given a |
| 1117 |
// compiler-assigned name. |
| 1118 |
// Beware: This is the "implementation specific" version of char, which |
| 1119 |
// in GENERIC seems to be signed on Windows/Linux Intel machines. But we |
| 1120 |
// need to be careful if we use an 8-bit type for numerical calculation. |
| 1121 |
tree var_decl = gg_declare_variable(CHAR); |
| 1122 |
gg_define_from_declaration(var_decl); |
| 1123 |
return var_decl; |
| 1124 |
} |
| 1125 |
|
| 1126 |
tree |
| 1127 |
gg_define_char(const char *variable_name) |
| 1128 |
{ |
| 1129 |
// The C equivalent: "char name;" |
| 1130 |
// Beware: This is the "implementation specific" version of char, which |
| 1131 |
// in GENERIC seems to be signed on Windows/Linux Intel machines. But we |
| 1132 |
// need to be careful if we use an 8-bit type for numerical calculation. |
| 1133 |
tree var_decl = gg_declare_variable(CHAR, variable_name); |
| 1134 |
gg_define_from_declaration(var_decl); |
| 1135 |
return var_decl; |
| 1136 |
} |
| 1137 |
|
| 1138 |
tree |
| 1139 |
gg_define_char(const char *variable_name, tree ch) |
| 1140 |
{ |
| 1141 |
tree var_decl = gg_declare_variable(CHAR, variable_name, ch); |
| 1142 |
gg_define_from_declaration(var_decl); |
| 1143 |
return var_decl; |
| 1144 |
} |
| 1145 |
|
| 1146 |
tree |
| 1147 |
gg_define_char(const char *variable_name, int ch) |
| 1148 |
{ |
| 1149 |
return gg_define_char(variable_name, char_nodes[ch&0xFF]); |
| 1150 |
} |
| 1151 |
|
| 1152 |
tree |
| 1153 |
gg_define_uchar() |
| 1154 |
{ |
| 1155 |
// The C equivalent: "char name;" |
| 1156 |
// Beware: This is the "implementation specific" version of char, which |
| 1157 |
// in GENERIC seems to be signed on Windows/Linux Intel machines. But we |
| 1158 |
// need to be careful if we use an 8-bit type for numerical calculation. |
| 1159 |
return gg_define_variable(UCHAR); |
| 1160 |
} |
| 1161 |
|
| 1162 |
tree |
| 1163 |
gg_define_uchar(const char *variable_name) |
| 1164 |
{ |
| 1165 |
// The C equivalent: "char name;" |
| 1166 |
// Beware: This is the "implementation specific" version of char, which |
| 1167 |
// in GENERIC seems to be signed on Windows/Linux Intel machines. But we |
| 1168 |
// need to be careful if we use an 8-bit type for numerical calculation. |
| 1169 |
return gg_define_variable(UCHAR, variable_name); |
| 1170 |
} |
| 1171 |
|
| 1172 |
tree |
| 1173 |
gg_define_uchar(const char *variable_name, tree ch) |
| 1174 |
{ |
| 1175 |
tree var_decl = gg_declare_variable(UCHAR, variable_name, ch); |
| 1176 |
gg_define_from_declaration(var_decl); |
| 1177 |
return var_decl; |
| 1178 |
} |
| 1179 |
|
| 1180 |
tree |
| 1181 |
gg_define_uchar(const char *variable_name, int ch) |
| 1182 |
{ |
| 1183 |
return gg_define_char(variable_name, char_nodes[ch&0xFF]); |
| 1184 |
} |
| 1185 |
|
| 1186 |
tree |
| 1187 |
gg_define_int() |
| 1188 |
{ |
| 1189 |
tree var_decl = gg_declare_variable(INT); |
| 1190 |
gg_define_from_declaration(var_decl); |
| 1191 |
return var_decl; |
| 1192 |
} |
| 1193 |
|
| 1194 |
tree |
| 1195 |
gg_define_int(int N) |
| 1196 |
{ |
| 1197 |
tree var_decl = gg_declare_variable(INT, NULL, build_int_cst_type(INT, N)); |
| 1198 |
gg_define_from_declaration(var_decl); |
| 1199 |
return var_decl; |
| 1200 |
} |
| 1201 |
|
| 1202 |
tree |
| 1203 |
gg_define_int(const char *variable_name) |
| 1204 |
{ |
| 1205 |
tree var_decl = gg_declare_variable(INT, variable_name); |
| 1206 |
gg_define_from_declaration(var_decl); |
| 1207 |
return var_decl; |
| 1208 |
} |
| 1209 |
|
| 1210 |
tree |
| 1211 |
gg_define_int(const char *variable_name, tree N) |
| 1212 |
{ |
| 1213 |
tree var_decl = gg_declare_variable(INT, variable_name, N); |
| 1214 |
gg_define_from_declaration(var_decl); |
| 1215 |
return var_decl; |
| 1216 |
} |
| 1217 |
|
| 1218 |
tree |
| 1219 |
gg_define_int(const char *variable_name, int N) |
| 1220 |
{ |
| 1221 |
tree var_decl = gg_declare_variable(INT, variable_name, build_int_cst_type(INT, N)); |
| 1222 |
gg_define_from_declaration(var_decl); |
| 1223 |
return var_decl; |
| 1224 |
} |
| 1225 |
|
| 1226 |
tree |
| 1227 |
gg_define_size_t() |
| 1228 |
{ |
| 1229 |
tree var_decl = gg_declare_variable(SIZE_T); |
| 1230 |
gg_define_from_declaration(var_decl); |
| 1231 |
return var_decl; |
| 1232 |
} |
| 1233 |
|
| 1234 |
tree |
| 1235 |
gg_define_size_t(const char *variable_name) |
| 1236 |
{ |
| 1237 |
tree var_decl = gg_declare_variable(SIZE_T, variable_name); |
| 1238 |
gg_define_from_declaration(var_decl); |
| 1239 |
return var_decl; |
| 1240 |
} |
| 1241 |
|
| 1242 |
tree |
| 1243 |
gg_define_size_t(tree N) |
| 1244 |
{ |
| 1245 |
tree retval = gg_define_variable(SIZE_T); |
| 1246 |
gg_assign(retval, N); |
| 1247 |
return retval; |
| 1248 |
} |
| 1249 |
|
| 1250 |
tree |
| 1251 |
gg_define_size_t(size_t N) |
| 1252 |
{ |
| 1253 |
tree var_decl = gg_declare_variable(SIZE_T, NULL, build_int_cst_type(SIZE_T, N)); |
| 1254 |
gg_define_from_declaration(var_decl); |
| 1255 |
return var_decl; |
| 1256 |
} |
| 1257 |
|
| 1258 |
tree |
| 1259 |
gg_define_size_t(const char *variable_name, tree N) |
| 1260 |
{ |
| 1261 |
tree var_decl = gg_declare_variable(SIZE_T, variable_name, N); |
| 1262 |
gg_define_from_declaration(var_decl); |
| 1263 |
return var_decl; |
| 1264 |
} |
| 1265 |
|
| 1266 |
tree |
| 1267 |
gg_define_size_t(const char *variable_name, size_t N) |
| 1268 |
{ |
| 1269 |
tree var_decl = gg_declare_variable(SIZE_T, variable_name, build_int_cst_type(SIZE_T, N)); |
| 1270 |
gg_define_from_declaration(var_decl); |
| 1271 |
return var_decl; |
| 1272 |
} |
| 1273 |
|
| 1274 |
tree |
| 1275 |
gg_define_int128() |
| 1276 |
{ |
| 1277 |
// The C equivalent: "INT128 <compiler_name>;" |
| 1278 |
return gg_define_variable(INT128); |
| 1279 |
} |
| 1280 |
|
| 1281 |
tree |
| 1282 |
gg_define_int128(const char *variable_name) |
| 1283 |
{ |
| 1284 |
// The C equivalent: "INT128 name;" |
| 1285 |
return gg_define_variable(INT128, variable_name); |
| 1286 |
} |
| 1287 |
|
| 1288 |
tree |
| 1289 |
gg_define_int128(const char *variable_name, tree N) |
| 1290 |
{ |
| 1291 |
// The C equivalent: "INT128 name = N" |
| 1292 |
tree var_decl = gg_declare_variable(INT128, variable_name, N); |
| 1293 |
gg_define_from_declaration(var_decl); |
| 1294 |
return var_decl; |
| 1295 |
} |
| 1296 |
|
| 1297 |
tree |
| 1298 |
gg_define_int128(const char *variable_name, int N) |
| 1299 |
{ |
| 1300 |
// The C equivalent: "INT128 name = N" |
| 1301 |
tree var_decl = gg_define_int128(variable_name, build_int_cst_type(INT128, N)); |
| 1302 |
return var_decl; |
| 1303 |
} |
| 1304 |
|
| 1305 |
tree |
| 1306 |
gg_define_char_star() |
| 1307 |
{ |
| 1308 |
// The C equivalent: "char *name;" |
| 1309 |
return gg_define_variable(CHAR_P); |
| 1310 |
} |
| 1311 |
|
| 1312 |
tree |
| 1313 |
gg_define_char_star(const char *variable_name) |
| 1314 |
{ |
| 1315 |
return gg_define_variable(CHAR_P, variable_name); |
| 1316 |
} |
| 1317 |
|
| 1318 |
tree |
| 1319 |
gg_define_char_star(const char *variable_name, gg_variable_scope_t scope) |
| 1320 |
{ |
| 1321 |
tree var_decl = gg_declare_variable(CHAR_P, variable_name, NULL_TREE, scope); |
| 1322 |
gg_define_from_declaration(var_decl); |
| 1323 |
return var_decl; |
| 1324 |
} |
| 1325 |
|
| 1326 |
tree |
| 1327 |
gg_define_char_star(tree var) |
| 1328 |
{ |
| 1329 |
tree var_decl = gg_declare_variable(CHAR_P, NULL, var); |
| 1330 |
gg_define_from_declaration(var_decl); |
| 1331 |
return var_decl; |
| 1332 |
} |
| 1333 |
|
| 1334 |
tree |
| 1335 |
gg_define_char_star(const char *variable_name, tree var) |
| 1336 |
{ |
| 1337 |
tree var_decl = gg_declare_variable(CHAR_P, variable_name, var); |
| 1338 |
gg_define_from_declaration(var_decl); |
| 1339 |
return var_decl; |
| 1340 |
} |
| 1341 |
|
| 1342 |
tree |
| 1343 |
gg_define_uchar_star() |
| 1344 |
{ |
| 1345 |
tree var_decl = gg_declare_variable(UCHAR_P); |
| 1346 |
gg_define_from_declaration(var_decl); |
| 1347 |
return var_decl; |
| 1348 |
} |
| 1349 |
|
| 1350 |
tree |
| 1351 |
gg_define_uchar_star(const char *variable_name) |
| 1352 |
{ |
| 1353 |
tree var_decl = gg_declare_variable(UCHAR_P, variable_name); |
| 1354 |
gg_define_from_declaration(var_decl); |
| 1355 |
return var_decl; |
| 1356 |
} |
| 1357 |
|
| 1358 |
tree |
| 1359 |
gg_define_uchar_star(const char *variable_name, gg_variable_scope_t scope) |
| 1360 |
{ |
| 1361 |
tree var_decl = gg_declare_variable(UCHAR_P, variable_name, NULL_TREE, scope); |
| 1362 |
gg_define_from_declaration(var_decl); |
| 1363 |
return var_decl; |
| 1364 |
} |
| 1365 |
|
| 1366 |
tree |
| 1367 |
gg_define_uchar_star(tree var) |
| 1368 |
{ |
| 1369 |
tree var_decl = gg_declare_variable(UCHAR_P, NULL, var); |
| 1370 |
gg_define_from_declaration(var_decl); |
| 1371 |
return var_decl; |
| 1372 |
} |
| 1373 |
|
| 1374 |
tree |
| 1375 |
gg_define_uchar_star(const char *variable_name, tree var) |
| 1376 |
{ |
| 1377 |
tree var_decl = gg_declare_variable(UCHAR_P, variable_name, var); |
| 1378 |
gg_define_from_declaration(var_decl); |
| 1379 |
return var_decl; |
| 1380 |
} |
| 1381 |
|
| 1382 |
tree |
| 1383 |
gg_define_void_star() |
| 1384 |
{ |
| 1385 |
tree var_decl = gg_declare_variable(VOID_P); |
| 1386 |
gg_define_from_declaration(var_decl); |
| 1387 |
return var_decl; |
| 1388 |
} |
| 1389 |
|
| 1390 |
tree |
| 1391 |
gg_define_void_star(const char *variable_name) |
| 1392 |
{ |
| 1393 |
tree var_decl = gg_declare_variable(VOID_P, variable_name); |
| 1394 |
gg_define_from_declaration(var_decl); |
| 1395 |
return var_decl; |
| 1396 |
} |
| 1397 |
|
| 1398 |
tree |
| 1399 |
gg_define_void_star(const char *variable_name, tree var) |
| 1400 |
{ |
| 1401 |
tree var_decl = gg_declare_variable(VOID_P, variable_name, var); |
| 1402 |
gg_define_from_declaration(var_decl); |
| 1403 |
return var_decl; |
| 1404 |
} |
| 1405 |
|
| 1406 |
tree |
| 1407 |
gg_define_void_star(const char *variable_name, gg_variable_scope_t scope) |
| 1408 |
{ |
| 1409 |
tree var_decl = gg_declare_variable(VOID_P, variable_name, NULL_TREE, scope); |
| 1410 |
gg_define_from_declaration(var_decl); |
| 1411 |
return var_decl; |
| 1412 |
} |
| 1413 |
|
| 1414 |
tree |
| 1415 |
gg_define_longdouble() |
| 1416 |
{ |
| 1417 |
tree var_decl = gg_declare_variable(LONGDOUBLE); |
| 1418 |
gg_define_from_declaration(var_decl); |
| 1419 |
return var_decl; |
| 1420 |
} |
| 1421 |
|
| 1422 |
extern tree |
| 1423 |
gg_define_array(tree type_decl, size_t size) |
| 1424 |
{ |
| 1425 |
tree array_type = build_array_type_nelts(type_decl, size); |
| 1426 |
return gg_define_variable(array_type); |
| 1427 |
} |
| 1428 |
|
| 1429 |
extern tree |
| 1430 |
gg_define_array(tree type_decl, const char *name, size_t size) |
| 1431 |
{ |
| 1432 |
tree array_type = build_array_type_nelts(type_decl, size); |
| 1433 |
return gg_define_variable(array_type, name); |
| 1434 |
} |
| 1435 |
|
| 1436 |
extern tree |
| 1437 |
gg_define_array(tree type_decl, size_t size, gg_variable_scope_t scope) |
| 1438 |
{ |
| 1439 |
tree array_type = build_array_type_nelts(type_decl, size); |
| 1440 |
return gg_define_variable(array_type, scope); |
| 1441 |
} |
| 1442 |
|
| 1443 |
extern tree |
| 1444 |
gg_define_array(tree type_decl, const char *name, size_t size, gg_variable_scope_t scope) |
| 1445 |
{ |
| 1446 |
tree array_type = build_array_type_nelts(type_decl, size); |
| 1447 |
return gg_define_variable(array_type, name, scope); |
| 1448 |
} |
| 1449 |
|
| 1450 |
tree |
| 1451 |
gg_get_address_of(const tree var_decl) |
| 1452 |
{ |
| 1453 |
// Returns an ADDR_EXPR which points to var_decl. |
| 1454 |
// The C equivalent is &variable |
| 1455 |
// We need to be able to use this guy's address directly: |
| 1456 |
|
| 1457 |
// In order to do that, this fellow's "addressable" bit has to be on, otherwise |
| 1458 |
// the GIMPLE reducer creates a temporary variable, sets its value to var_decl's, |
| 1459 |
// and returns the pointer to the temp. I suppose this has something to do with |
| 1460 |
// pass by reference and pass by value, but it makes my head hurt, and, frankly, |
| 1461 |
// I'll take the dangerous road. |
| 1462 |
|
| 1463 |
TREE_ADDRESSABLE(var_decl) = 1; |
| 1464 |
TREE_USED(var_decl) = 1; |
| 1465 |
return build1( ADDR_EXPR, |
| 1466 |
build_pointer_type (TREE_TYPE(var_decl)), |
| 1467 |
var_decl); |
| 1468 |
} |
| 1469 |
|
| 1470 |
tree |
| 1471 |
gg_get_indirect_reference(tree pointer, tree offset) |
| 1472 |
{ |
| 1473 |
// The C equivalent: auto pointer[offset]; |
| 1474 |
|
| 1475 |
// the returned indirect reference has the same type as |
| 1476 |
// what pointer points to. If pointer is a char *, then the returned |
| 1477 |
// value has type char. If pointer is an int *, then the returned |
| 1478 |
// value has type int. |
| 1479 |
|
| 1480 |
// We also want the offset to operate the same way it does in C, so we |
| 1481 |
// are going to find the size of the objects the pointer points to, and |
| 1482 |
// multiply the offset by that size: |
| 1483 |
|
| 1484 |
tree pointer_type = TREE_TYPE(pointer); |
| 1485 |
tree element_type = TREE_TYPE(pointer_type); |
| 1486 |
|
| 1487 |
tree indirect_reference; |
| 1488 |
if( offset ) |
| 1489 |
{ |
| 1490 |
// We can now start building our little shrub: |
| 1491 |
tree distance = build2( MULT_EXPR, |
| 1492 |
SIZE_T, |
| 1493 |
gg_cast(sizetype, offset), |
| 1494 |
TYPE_SIZE_UNIT(element_type)); |
| 1495 |
|
| 1496 |
// Next, we build the pointer_plus_expr: |
| 1497 |
tree pointer_plus_expr = build2(POINTER_PLUS_EXPR, |
| 1498 |
pointer_type, |
| 1499 |
pointer, |
| 1500 |
distance); |
| 1501 |
|
| 1502 |
// With that in hand, we can build the indirect_reference: |
| 1503 |
indirect_reference = build1(INDIRECT_REF, element_type, pointer_plus_expr); |
| 1504 |
} |
| 1505 |
else |
| 1506 |
{ |
| 1507 |
indirect_reference = build1(INDIRECT_REF, element_type, pointer); |
| 1508 |
} |
| 1509 |
|
| 1510 |
return indirect_reference; |
| 1511 |
} |
| 1512 |
|
| 1513 |
tree |
| 1514 |
gg_indirect(tree pointer, tree byte_offset) |
| 1515 |
{ |
| 1516 |
// Unlike gg_get_indirect_reference, which multiplies the offset by the |
| 1517 |
// size of the type pointed to by pointer, this routine simply adds the offset |
| 1518 |
// to the pointer. |
| 1519 |
tree pointer_type = TREE_TYPE(pointer); |
| 1520 |
tree element_type = TREE_TYPE(pointer_type); |
| 1521 |
|
| 1522 |
tree retval; |
| 1523 |
if( byte_offset == NULL_TREE ) |
| 1524 |
{ |
| 1525 |
retval = build1(INDIRECT_REF, element_type, pointer); |
| 1526 |
} |
| 1527 |
else |
| 1528 |
{ |
| 1529 |
tree pointer_plus_expr = build2(POINTER_PLUS_EXPR, |
| 1530 |
pointer_type, |
| 1531 |
pointer, |
| 1532 |
gg_cast(SIZE_T, byte_offset)); |
| 1533 |
retval = build1(INDIRECT_REF, element_type, pointer_plus_expr); |
| 1534 |
} |
| 1535 |
|
| 1536 |
return retval; |
| 1537 |
} |
| 1538 |
|
| 1539 |
tree |
| 1540 |
gg_array_value(tree pointer, tree offset) |
| 1541 |
{ |
| 1542 |
// We arrange the function so that it can work on either an ARRAY_TYPE |
| 1543 |
// or a pointer type |
| 1544 |
tree pointer_type = TREE_TYPE(pointer); |
| 1545 |
tree element_type = TREE_TYPE(pointer_type); |
| 1546 |
if(POINTER_TYPE_P(pointer_type)) |
| 1547 |
{ |
| 1548 |
// It is a pointer |
| 1549 |
tree retval = gg_get_indirect_reference(pointer, offset); |
| 1550 |
return retval; |
| 1551 |
} |
| 1552 |
else |
| 1553 |
{ |
| 1554 |
return build4(ARRAY_REF, |
| 1555 |
element_type, |
| 1556 |
pointer, |
| 1557 |
offset, |
| 1558 |
NULL_TREE, |
| 1559 |
NULL_TREE); |
| 1560 |
} |
| 1561 |
} |
| 1562 |
|
| 1563 |
tree |
| 1564 |
gg_array_value(tree pointer, int N) |
| 1565 |
{ |
| 1566 |
return gg_array_value(pointer, build_int_cst(INT, N)); |
| 1567 |
} |
| 1568 |
|
| 1569 |
void |
| 1570 |
gg_increment(tree var) |
| 1571 |
{ |
| 1572 |
tree var_type = TREE_TYPE(var); |
| 1573 |
gg_assign(var, gg_add(var, build_int_cst_type(var_type, 1))); |
| 1574 |
} |
| 1575 |
|
| 1576 |
void |
| 1577 |
gg_decrement(tree var) |
| 1578 |
{ |
| 1579 |
tree var_type = TREE_TYPE(var); |
| 1580 |
gg_assign(var, |
| 1581 |
gg_cast(var_type, |
| 1582 |
gg_subtract(var, |
| 1583 |
build_int_cst_type(var_type, 1)))); |
| 1584 |
} |
| 1585 |
|
| 1586 |
tree |
| 1587 |
gg_negate(tree var) |
| 1588 |
{ |
| 1589 |
return build1(NEGATE_EXPR, TREE_TYPE(var), var); |
| 1590 |
} |
| 1591 |
|
| 1592 |
tree |
| 1593 |
gg_bitwise_not(tree var) |
| 1594 |
{ |
| 1595 |
return build1(BIT_NOT_EXPR, TREE_TYPE(var), var); |
| 1596 |
} |
| 1597 |
|
| 1598 |
tree |
| 1599 |
gg_abs(tree var) |
| 1600 |
{ |
| 1601 |
return build1(ABS_EXPR, TREE_TYPE(var), var); |
| 1602 |
} |
| 1603 |
|
| 1604 |
static tree |
| 1605 |
gg_get_larger_type(tree A, tree B) |
| 1606 |
{ |
| 1607 |
tree larger = TREE_TYPE(B); |
| 1608 |
if( TREE_INT_CST_LOW(TYPE_SIZE(TREE_TYPE(A))) |
| 1609 |
> TREE_INT_CST_LOW(TYPE_SIZE(TREE_TYPE(B))) ) |
| 1610 |
{ |
| 1611 |
larger = TREE_TYPE(A); |
| 1612 |
} |
| 1613 |
return larger; |
| 1614 |
} |
| 1615 |
|
| 1616 |
tree |
| 1617 |
gg_add(tree addend1, tree addend2) |
| 1618 |
{ |
| 1619 |
tree retval; |
| 1620 |
if( POINTER_TYPE_P(TREE_TYPE(addend1)) ) |
| 1621 |
{ |
| 1622 |
// operand1 is a pointer. |
| 1623 |
// Make this work like C pointer arithmetic. We'll find the |
| 1624 |
// size of the things that pointer points to, and multiply accordingly |
| 1625 |
tree pointer_type = TREE_TYPE(addend1); |
| 1626 |
tree pointer_type_type = TREE_TYPE(pointer_type); |
| 1627 |
tree bytes_per_element = TYPE_SIZE_UNIT(pointer_type_type); |
| 1628 |
|
| 1629 |
tree op2 = gg_cast(SIZE_T, gg_multiply(addend2, bytes_per_element)); |
| 1630 |
retval = build2(POINTER_PLUS_EXPR, |
| 1631 |
TREE_TYPE(addend1), |
| 1632 |
addend1, |
| 1633 |
op2); |
| 1634 |
} |
| 1635 |
else |
| 1636 |
{ |
| 1637 |
// Ordinary addition. Scale both operands to match the larger |
| 1638 |
// type of the two operands. |
| 1639 |
tree larger_type = gg_get_larger_type(addend1, addend2); |
| 1640 |
retval = build2( PLUS_EXPR, |
| 1641 |
larger_type, |
| 1642 |
gg_cast(larger_type, addend1), |
| 1643 |
gg_cast(larger_type, addend2)); |
| 1644 |
} |
| 1645 |
return retval; |
| 1646 |
} |
| 1647 |
|
| 1648 |
tree |
| 1649 |
gg_subtract(tree A, tree B) |
| 1650 |
{ |
| 1651 |
// We are doing A - B, instead. |
| 1652 |
|
| 1653 |
if( POINTER_TYPE_P(TREE_TYPE(A)) && INTEGRAL_TYPE_P(TREE_TYPE(B)) ) |
| 1654 |
{ |
| 1655 |
// We are subtracting an integer from a pointer. That's handled |
| 1656 |
// in gg_add, by converting the integer, possibly signed, to |
| 1657 |
// an unsigned huge number. |
| 1658 |
return gg_add(A, gg_negate(B)); |
| 1659 |
} |
| 1660 |
|
| 1661 |
if( POINTER_TYPE_P(TREE_TYPE(A)) && POINTER_TYPE_P(TREE_TYPE(A)) ) |
| 1662 |
{ |
| 1663 |
// We are subtracting two pointers, yielding a signed size_t |
| 1664 |
return build2(POINTER_DIFF_EXPR, SSIZE_T, A, B); |
| 1665 |
} |
| 1666 |
|
| 1667 |
// This is an ordinary subtraction. Scale everything to the larger_type |
| 1668 |
// of the two operands. |
| 1669 |
tree larger_type = gg_get_larger_type(A, B); |
| 1670 |
tree stmt = build2( MINUS_EXPR, |
| 1671 |
larger_type, |
| 1672 |
gg_cast(larger_type, A), |
| 1673 |
gg_cast(larger_type, B) ); |
| 1674 |
return stmt; |
| 1675 |
} |
| 1676 |
|
| 1677 |
tree |
| 1678 |
gg_multiply(tree A, tree B) |
| 1679 |
{ |
| 1680 |
// We will return the product of A and B, adjusting to |
| 1681 |
// whichever is larger: |
| 1682 |
tree larger_type = gg_get_larger_type(A, B); |
| 1683 |
return build2( MULT_EXPR, larger_type, gg_cast(larger_type, A), gg_cast(larger_type, B) ); |
| 1684 |
} |
| 1685 |
|
| 1686 |
tree |
| 1687 |
gg_real_divide(tree A, tree B) |
| 1688 |
{ |
| 1689 |
// This floating point division: |
| 1690 |
tree larger_type = gg_get_larger_type(A, B); |
| 1691 |
return build2( RDIV_EXPR, larger_type, gg_cast(larger_type,A), gg_cast(larger_type,B)); |
| 1692 |
} |
| 1693 |
|
| 1694 |
tree |
| 1695 |
gg_divide(tree A, tree B) |
| 1696 |
{ |
| 1697 |
// This is the equivalent of C integer divide |
| 1698 |
tree larger_type = gg_get_larger_type(A, B); |
| 1699 |
return build2( TRUNC_DIV_EXPR, larger_type, gg_cast(larger_type,A), gg_cast(larger_type,B)); |
| 1700 |
} |
| 1701 |
|
| 1702 |
tree |
| 1703 |
gg_mod(tree A, tree B) |
| 1704 |
{ |
| 1705 |
// This is the equivalent of C A % B |
| 1706 |
tree larger_type = gg_get_larger_type(A, B); |
| 1707 |
return build2( TRUNC_MOD_EXPR, larger_type, gg_cast(larger_type,A), gg_cast(larger_type,B)); |
| 1708 |
} |
| 1709 |
|
| 1710 |
tree |
| 1711 |
gg_lshift(tree A, tree B) |
| 1712 |
{ |
| 1713 |
// Equivalent of A << B; |
| 1714 |
return build2( LSHIFT_EXPR, TREE_TYPE(A), A, B ); |
| 1715 |
} |
| 1716 |
|
| 1717 |
tree |
| 1718 |
gg_rshift(tree A, tree B) |
| 1719 |
{ |
| 1720 |
// Equivalent of A >> B; |
| 1721 |
return build2( RSHIFT_EXPR, TREE_TYPE(A), A, B ); |
| 1722 |
} |
| 1723 |
|
| 1724 |
tree |
| 1725 |
gg_bitwise_or(tree A, tree B) |
| 1726 |
{ |
| 1727 |
// This is C equivalent to A | B |
| 1728 |
tree larger_type = gg_get_larger_type(A, B); |
| 1729 |
return build2( BIT_IOR_EXPR, larger_type, gg_cast(larger_type,A), gg_cast(larger_type,B)); |
| 1730 |
} |
| 1731 |
|
| 1732 |
tree |
| 1733 |
gg_bitwise_xor(tree A, tree B) |
| 1734 |
{ |
| 1735 |
// This is C equivalent to A ^ B |
| 1736 |
tree larger_type = gg_get_larger_type(A, B); |
| 1737 |
return build2( BIT_XOR_EXPR, larger_type, gg_cast(larger_type,A), gg_cast(larger_type,B)); |
| 1738 |
} |
| 1739 |
|
| 1740 |
tree |
| 1741 |
gg_bitwise_and(tree A, tree B) |
| 1742 |
{ |
| 1743 |
// This is C equivalent to A & B |
| 1744 |
tree larger_type = gg_get_larger_type(A, B); |
| 1745 |
return build2( BIT_AND_EXPR, larger_type, gg_cast(larger_type,A), gg_cast(larger_type,B)); |
| 1746 |
} |
| 1747 |
|
| 1748 |
tree |
| 1749 |
gg_build_relational_expression(tree operand_a, |
| 1750 |
enum relop_t op, |
| 1751 |
tree operand_b) |
| 1752 |
{ |
| 1753 |
tree_code compare = EQ_EXPR; // Assuage the compiler |
| 1754 |
switch(op) |
| 1755 |
{ |
| 1756 |
case eq_op: |
| 1757 |
compare = EQ_EXPR; |
| 1758 |
break; |
| 1759 |
case ne_op: |
| 1760 |
compare = NE_EXPR; |
| 1761 |
break; |
| 1762 |
case lt_op: |
| 1763 |
compare = LT_EXPR; |
| 1764 |
break; |
| 1765 |
case gt_op: |
| 1766 |
compare = GT_EXPR; |
| 1767 |
break; |
| 1768 |
case ge_op: |
| 1769 |
compare = GE_EXPR; |
| 1770 |
break; |
| 1771 |
case le_op: |
| 1772 |
compare = LE_EXPR; |
| 1773 |
break; |
| 1774 |
} |
| 1775 |
tree relational_expression = build2_loc(gg_token_location(), |
| 1776 |
compare, |
| 1777 |
boolean_type_node, |
| 1778 |
operand_a, |
| 1779 |
operand_b); |
| 1780 |
return relational_expression; |
| 1781 |
} |
| 1782 |
|
| 1783 |
tree |
| 1784 |
gg_build_logical_expression(tree operand_a, |
| 1785 |
enum logop_t op, |
| 1786 |
tree operand_b) |
| 1787 |
{ |
| 1788 |
tree logical_expression = NULL_TREE; |
| 1789 |
tree_code logical_op; |
| 1790 |
switch(op) |
| 1791 |
{ |
| 1792 |
case and_op: |
| 1793 |
logical_op = TRUTH_ANDIF_EXPR; |
| 1794 |
logical_expression = build2(logical_op, |
| 1795 |
boolean_type_node, |
| 1796 |
operand_a, |
| 1797 |
operand_b); |
| 1798 |
break; |
| 1799 |
|
| 1800 |
case or_op: |
| 1801 |
logical_op = TRUTH_ORIF_EXPR; |
| 1802 |
logical_expression = build2(logical_op, |
| 1803 |
boolean_type_node, |
| 1804 |
operand_a, |
| 1805 |
operand_b); |
| 1806 |
break; |
| 1807 |
|
| 1808 |
case not_op: |
| 1809 |
logical_op = TRUTH_NOT_EXPR; |
| 1810 |
logical_expression = build1(logical_op, |
| 1811 |
boolean_type_node, |
| 1812 |
operand_b); |
| 1813 |
break; |
| 1814 |
|
| 1815 |
case xor_op: |
| 1816 |
logical_op = TRUTH_XOR_EXPR; |
| 1817 |
logical_expression = build2(logical_op, |
| 1818 |
boolean_type_node, |
| 1819 |
operand_a, |
| 1820 |
operand_b); |
| 1821 |
break; |
| 1822 |
|
| 1823 |
case xnor_op: |
| 1824 |
case true_op: |
| 1825 |
case false_op: |
| 1826 |
// This is handled elsewhere |
| 1827 |
break; |
| 1828 |
} |
| 1829 |
return logical_expression; |
| 1830 |
} |
| 1831 |
|
| 1832 |
void |
| 1833 |
gg_create_goto_pair(tree *goto_expr, tree *label_expr, tree *label_addr, const char *name) |
| 1834 |
{ |
| 1835 |
// We are going to create a pair of expressions for our |
| 1836 |
// caller. They are a matched set of goto/label expressions, |
| 1837 |
// to be included in a statement list |
| 1838 |
tree label_decl = build_decl( UNKNOWN_LOCATION, |
| 1839 |
LABEL_DECL, |
| 1840 |
gg_create_assembler_name(name), |
| 1841 |
void_type_node); |
| 1842 |
DECL_CONTEXT(label_decl) = current_function->function_decl; |
| 1843 |
TREE_USED(label_decl) = 1; |
| 1844 |
|
| 1845 |
*goto_expr = build1(GOTO_EXPR, void_type_node, label_decl); |
| 1846 |
*label_expr = build1(LABEL_EXPR, void_type_node, label_decl); |
| 1847 |
*label_addr = gg_get_address_of(label_decl); |
| 1848 |
} |
| 1849 |
|
| 1850 |
void |
| 1851 |
gg_create_goto_pair(tree *goto_expr, tree *label_expr, tree *label_addr) |
| 1852 |
{ |
| 1853 |
// We are going to create a pair of expressions for our |
| 1854 |
// caller. They are a matched set of goto/label expressions, |
| 1855 |
// to be included in a statement list |
| 1856 |
tree label_decl = build_decl( UNKNOWN_LOCATION, |
| 1857 |
LABEL_DECL, |
| 1858 |
NULL_TREE, |
| 1859 |
void_type_node); |
| 1860 |
DECL_CONTEXT(label_decl) = current_function->function_decl; |
| 1861 |
TREE_USED(label_decl) = 1; |
| 1862 |
|
| 1863 |
*goto_expr = build1(GOTO_EXPR, void_type_node, label_decl); |
| 1864 |
*label_expr = build1(LABEL_EXPR, void_type_node, label_decl); |
| 1865 |
*label_addr = gg_get_address_of(label_decl); |
| 1866 |
} |
| 1867 |
|
| 1868 |
void |
| 1869 |
gg_create_goto_pair(tree *goto_expr, |
| 1870 |
tree *label_expr, |
| 1871 |
tree *label_addr, |
| 1872 |
tree *label_decl) |
| 1873 |
{ |
| 1874 |
// We are going to create a pair of expressions for our |
| 1875 |
// caller. They are a matched set of goto/label expressions, |
| 1876 |
// to be included in a statement list |
| 1877 |
*label_decl = build_decl( UNKNOWN_LOCATION, |
| 1878 |
LABEL_DECL, |
| 1879 |
NULL_TREE, |
| 1880 |
void_type_node); |
| 1881 |
DECL_CONTEXT(*label_decl) = current_function->function_decl; |
| 1882 |
TREE_USED(*label_decl) = 1; |
| 1883 |
|
| 1884 |
*goto_expr = build1(GOTO_EXPR, void_type_node, *label_decl); |
| 1885 |
*label_expr = build1(LABEL_EXPR, void_type_node, *label_decl); |
| 1886 |
*label_addr = gg_get_address_of(*label_decl); |
| 1887 |
} |
| 1888 |
|
| 1889 |
void |
| 1890 |
gg_goto_label_decl(tree label_decl) |
| 1891 |
{ |
| 1892 |
tree goto_expr = build1_loc( gg_token_location(), |
| 1893 |
GOTO_EXPR, |
| 1894 |
void_type_node, |
| 1895 |
label_decl); |
| 1896 |
gg_append_statement(goto_expr); |
| 1897 |
} |
| 1898 |
|
| 1899 |
void |
| 1900 |
gg_create_goto_pair(tree *goto_expr, tree *label_expr) |
| 1901 |
{ |
| 1902 |
// We are going to create a pair of expressions for our |
| 1903 |
// caller. They are a matched set of goto/label expressions, |
| 1904 |
// to be included in a statement list |
| 1905 |
tree label_decl = build_decl( UNKNOWN_LOCATION, |
| 1906 |
LABEL_DECL, |
| 1907 |
NULL_TREE, |
| 1908 |
void_type_node); |
| 1909 |
DECL_CONTEXT(label_decl) = current_function->function_decl; |
| 1910 |
TREE_USED(label_decl) = 1; |
| 1911 |
|
| 1912 |
*goto_expr = build1(GOTO_EXPR, void_type_node, label_decl); |
| 1913 |
*label_expr = build1(LABEL_EXPR, void_type_node, label_decl); |
| 1914 |
} |
| 1915 |
|
| 1916 |
void |
| 1917 |
gg_create_goto_pair(tree *goto_expr, tree *label_expr, const char *name) |
| 1918 |
{ |
| 1919 |
// We are going to create a pair of named expressions for our |
| 1920 |
// caller. They are a matched set of goto/label expressions, |
| 1921 |
// to be included in a statement list |
| 1922 |
tree label_decl = build_decl( UNKNOWN_LOCATION, |
| 1923 |
LABEL_DECL, |
| 1924 |
gg_create_assembler_name(name), |
| 1925 |
void_type_node); |
| 1926 |
DECL_CONTEXT(label_decl) = current_function->function_decl; |
| 1927 |
TREE_USED(label_decl) = 1; |
| 1928 |
|
| 1929 |
*goto_expr = build1(GOTO_EXPR, void_type_node, label_decl); |
| 1930 |
*label_expr = build1(LABEL_EXPR, void_type_node, label_decl); |
| 1931 |
} |
| 1932 |
|
| 1933 |
// Used for implementing SECTIONS and PARAGRAPHS. When you have a |
| 1934 |
// void *pointer = &&label, gg_goto is the same as |
| 1935 |
// goto *pointer |
| 1936 |
void |
| 1937 |
gg_goto(tree var_decl_pointer) |
| 1938 |
{ |
| 1939 |
tree go_to = build1_loc(gg_token_location(), |
| 1940 |
GOTO_EXPR, |
| 1941 |
void_type_node, |
| 1942 |
var_decl_pointer); |
| 1943 |
gg_append_statement(go_to); |
| 1944 |
} |
| 1945 |
|
| 1946 |
void |
| 1947 |
gg_while( tree operand_a, |
| 1948 |
enum relop_t op, |
| 1949 |
tree operand_b) |
| 1950 |
{ |
| 1951 |
/* |
| 1952 |
See demonstration_while_if for the canonical demonstration |
| 1953 |
|
| 1954 |
You use it like this: |
| 1955 |
|
| 1956 |
WHILE |
| 1957 |
.... |
| 1958 |
WEND |
| 1959 |
|
| 1960 |
We do the C construct: |
| 1961 |
|
| 1962 |
while( a OP b ) |
| 1963 |
{ |
| 1964 |
<block> |
| 1965 |
} |
| 1966 |
|
| 1967 |
like this: |
| 1968 |
|
| 1969 |
goto test |
| 1970 |
top: |
| 1971 |
<block> |
| 1972 |
test: |
| 1973 |
if( a OP b) |
| 1974 |
goto top |
| 1975 |
else |
| 1976 |
goto leave: |
| 1977 |
leave: |
| 1978 |
|
| 1979 |
*/ |
| 1980 |
|
| 1981 |
tree goto_top; |
| 1982 |
tree label_top; |
| 1983 |
|
| 1984 |
tree goto_test; |
| 1985 |
tree label_test; |
| 1986 |
|
| 1987 |
tree goto_leave; |
| 1988 |
tree label_leave; |
| 1989 |
|
| 1990 |
gg_create_goto_pair(&goto_top, &label_top); |
| 1991 |
gg_create_goto_pair(&goto_test, &label_test); |
| 1992 |
gg_create_goto_pair(&goto_leave, &label_leave); |
| 1993 |
|
| 1994 |
tree statement_block = make_node(STATEMENT_LIST); |
| 1995 |
TREE_TYPE(statement_block) = void_type_node; |
| 1996 |
|
| 1997 |
// During development, I tried appending a statement_list to a statement_list, |
| 1998 |
// intending it to be collected together that way. But it was too smart for me; |
| 1999 |
// it just unwound the second list and tacked it onto the end of the first. |
| 2000 |
|
| 2001 |
// So I used a BIND_EXPR to collect them together. This isn't a new context, so I don't |
| 2002 |
// point operand[0] at a string of vars, nor operand[2] at a block. |
| 2003 |
tree bind_expr = build3( BIND_EXPR, |
| 2004 |
void_type_node, |
| 2005 |
NULL_TREE, |
| 2006 |
statement_block, |
| 2007 |
NULL_TREE); |
| 2008 |
|
| 2009 |
// With the pairs created and the bind_expr sorted out, we can now put |
| 2010 |
// together our while construction: |
| 2011 |
|
| 2012 |
gg_append_statement(goto_test); |
| 2013 |
gg_append_statement(label_top); |
| 2014 |
gg_append_statement(bind_expr); |
| 2015 |
gg_append_statement(label_test); |
| 2016 |
IF( operand_a, op, operand_b ) |
| 2017 |
gg_append_statement(goto_top); |
| 2018 |
ELSE |
| 2019 |
gg_append_statement(goto_leave); |
| 2020 |
ENDIF |
| 2021 |
gg_append_statement(label_leave); |
| 2022 |
|
| 2023 |
// And here's the statement_list for the programmer to fill |
| 2024 |
// and end with a WEND |
| 2025 |
current_function->statement_list_stack.push_back(statement_block); |
| 2026 |
} |
| 2027 |
|
| 2028 |
void |
| 2029 |
gg_create_true_false_statement_lists(tree relational_expression) |
| 2030 |
{ |
| 2031 |
// Create the two statement_lists for ifness, one for true and |
| 2032 |
// the other for false. Put them on the stack, ready for the first |
| 2033 |
// pop on ELSE and the second pop on ENDIF: |
| 2034 |
|
| 2035 |
tree if_true_statement_list = make_node(STATEMENT_LIST); |
| 2036 |
TREE_TYPE(if_true_statement_list) = void_type_node; |
| 2037 |
tree if_false_statement_list = make_node(STATEMENT_LIST); |
| 2038 |
TREE_TYPE(if_false_statement_list) = void_type_node; |
| 2039 |
|
| 2040 |
tree conditional = build3( COND_EXPR, |
| 2041 |
boolean_type_node, |
| 2042 |
relational_expression, |
| 2043 |
if_true_statement_list, |
| 2044 |
if_false_statement_list); |
| 2045 |
|
| 2046 |
// We need to put our conditional onto the current_stack: |
| 2047 |
gg_append_statement(conditional); |
| 2048 |
|
| 2049 |
// And with that done, we can push the FALSE and TRUE blocks |
| 2050 |
// onto the stack in the correct order: |
| 2051 |
current_function->statement_list_stack.push_back(if_false_statement_list); |
| 2052 |
current_function->statement_list_stack.push_back(if_true_statement_list); |
| 2053 |
} |
| 2054 |
|
| 2055 |
void |
| 2056 |
gg_if( tree operand_a, |
| 2057 |
enum relop_t op, |
| 2058 |
tree operand_b) |
| 2059 |
{ |
| 2060 |
/* Listen up, troops. Here's how you use this constructor. |
| 2061 |
|
| 2062 |
You use it like this: |
| 2063 |
|
| 2064 |
IF( this, LT, that) |
| 2065 |
.... |
| 2066 |
ELSE |
| 2067 |
.... |
| 2068 |
ENDIF |
| 2069 |
|
| 2070 |
You *must* have all three: IF ELSE ENDIF, if you don't, the |
| 2071 |
current_function->statement_list_stack gets all higgledepiggledy |
| 2072 |
|
| 2073 |
It is the C equivalent of |
| 2074 |
|
| 2075 |
if( a OP b ) |
| 2076 |
{ |
| 2077 |
<if_true_statement_list> |
| 2078 |
} |
| 2079 |
else |
| 2080 |
{ |
| 2081 |
<if_false_statement_list> |
| 2082 |
} |
| 2083 |
|
| 2084 |
This routine pushes the false_statement_list onto current_function->statement_list_stack, |
| 2085 |
followed by the true_statement_list. |
| 2086 |
|
| 2087 |
You then generate statements for the TRUE block |
| 2088 |
You then pop the current_function->statement_list_stack. |
| 2089 |
Then you do the same for the FALSE block |
| 2090 |
You then pop the current_function->statement_list_stack again. |
| 2091 |
|
| 2092 |
For the sake of readability, we define ELSE and ENDIF to do |
| 2093 |
that popping. |
| 2094 |
|
| 2095 |
I don't plan on explaining this everywhere it's used. |
| 2096 |
|
| 2097 |
See demonstration_while_if for the canonical demonstration |
| 2098 |
*/ |
| 2099 |
|
| 2100 |
if( TREE_TYPE(operand_a) != TREE_TYPE(operand_b) ) |
| 2101 |
{ |
| 2102 |
fprintf(stderr, "%s(): a and b have different TREE_TYPES\n", __func__); |
| 2103 |
gcc_unreachable(); |
| 2104 |
} |
| 2105 |
|
| 2106 |
// Build the relational expression: |
| 2107 |
tree relational_expression = |
| 2108 |
gg_build_relational_expression(operand_a, |
| 2109 |
op, |
| 2110 |
operand_b); |
| 2111 |
|
| 2112 |
// And with that in hand, create the two statement lists, one for |
| 2113 |
// true and one for false, and set up the stacks: |
| 2114 |
gg_create_true_false_statement_lists(relational_expression); |
| 2115 |
} |
| 2116 |
|
| 2117 |
tree |
| 2118 |
gg_get_function_address(tree return_type, const char *funcname) |
| 2119 |
{ |
| 2120 |
// This routine finds a function by name. It calls build_fn_decl |
| 2121 |
// with an empty array of varargs. I haven't investigated all the |
| 2122 |
// possibilities, but this returns an address expression for a function |
| 2123 |
// that can be built with any argument[s]. |
| 2124 |
|
| 2125 |
// There is no compile-time checking; if you specify disaster, then |
| 2126 |
// disaster will be what you get. |
| 2127 |
tree fndecl_type = build_varargs_function_type_array (return_type, |
| 2128 |
0, |
| 2129 |
NULL); |
| 2130 |
tree function_decl = build_fn_decl (funcname, fndecl_type); |
| 2131 |
DECL_EXTERNAL (function_decl) = 1; |
| 2132 |
|
| 2133 |
tree retval = build1(ADDR_EXPR, build_pointer_type (fndecl_type), function_decl); |
| 2134 |
|
| 2135 |
return retval; |
| 2136 |
} |
| 2137 |
|
| 2138 |
void |
| 2139 |
gg_printf(const char *format_string, ...) |
| 2140 |
{ |
| 2141 |
// This allows you to use fprintf(stderr, ...) with a format string |
| 2142 |
// and a list of arguments ending with a NULL |
| 2143 |
|
| 2144 |
// Use this for conveniently adding print statements into the generated |
| 2145 |
// code, for run-time print-statement debugging. gg_write is used for |
| 2146 |
// actual program code. |
| 2147 |
|
| 2148 |
// Note that the return value from the printf() call is *not* available |
| 2149 |
// to the caller. |
| 2150 |
|
| 2151 |
int nargs = 0; |
| 2152 |
tree args[ARG_LIMIT]; |
| 2153 |
|
| 2154 |
args[nargs++] = build_string_literal(strlen(format_string)+1, format_string); |
| 2155 |
|
| 2156 |
va_list ap; |
| 2157 |
va_start(ap, format_string); |
| 2158 |
tree arg = va_arg(ap, tree); |
| 2159 |
while(arg) |
| 2160 |
{ |
| 2161 |
if(nargs >= ARG_LIMIT) |
| 2162 |
{ |
| 2163 |
yywarn("You *must* be joking"); |
| 2164 |
gcc_unreachable(); |
| 2165 |
} |
| 2166 |
|
| 2167 |
if( TREE_CODE(arg) >= NUM_TREE_CODES) |
| 2168 |
{ |
| 2169 |
// Warning: This test is not completely reliable, because a garbage |
| 2170 |
// byte could have a valid TREE_CODE. But it does help. |
| 2171 |
yywarn("You forgot to put a %<NULL_TREE%> at the end of a " |
| 2172 |
"%<gg_printf()%> again"); |
| 2173 |
gcc_unreachable(); |
| 2174 |
} |
| 2175 |
|
| 2176 |
args[nargs++] = arg; |
| 2177 |
arg = va_arg(ap, tree); |
| 2178 |
} |
| 2179 |
va_end (ap); |
| 2180 |
|
| 2181 |
static tree function = NULL_TREE; |
| 2182 |
if( !function ) |
| 2183 |
{ |
| 2184 |
function = gg_get_function_address(INT, "__gg__fprintf_stderr"); |
| 2185 |
} |
| 2186 |
|
| 2187 |
tree stmt = build_call_array_loc (gg_token_location(), |
| 2188 |
INT, |
| 2189 |
function, |
| 2190 |
nargs, |
| 2191 |
args); |
| 2192 |
gg_append_statement(stmt); |
| 2193 |
} |
| 2194 |
|
| 2195 |
tree |
| 2196 |
gg_fprintf(tree fd, int nargs, const char *format_string, ...) |
| 2197 |
{ |
| 2198 |
tree retval = gg_define_int(); |
| 2199 |
gg_push_context(); |
| 2200 |
tree buffer = gg_define_char_star(); |
| 2201 |
gg_assign(buffer, gg_cast(CHAR_P, gg_malloc(1024))); |
| 2202 |
|
| 2203 |
tree args[ARG_LIMIT]; |
| 2204 |
|
| 2205 |
// Set up a call to sprintf: |
| 2206 |
int argc = 0; |
| 2207 |
args[argc++] = buffer; |
| 2208 |
args[argc++] = build_string_literal(strlen(format_string)+1, format_string); |
| 2209 |
|
| 2210 |
va_list ap; |
| 2211 |
va_start(ap, format_string); |
| 2212 |
tree arg = va_arg(ap, tree); |
| 2213 |
int narg = 0; |
| 2214 |
while(narg++ < nargs) |
| 2215 |
{ |
| 2216 |
if(argc >= ARG_LIMIT) |
| 2217 |
{ |
| 2218 |
yywarn("You *must* be joking"); |
| 2219 |
gcc_unreachable(); |
| 2220 |
} |
| 2221 |
|
| 2222 |
args[argc++] = arg; |
| 2223 |
arg = va_arg(ap, tree); |
| 2224 |
} |
| 2225 |
va_end (ap); |
| 2226 |
|
| 2227 |
static tree function = NULL_TREE; |
| 2228 |
|
| 2229 |
if( !function ) |
| 2230 |
{ |
| 2231 |
function = gg_get_function_address(INT, "sprintf"); |
| 2232 |
} |
| 2233 |
|
| 2234 |
tree stmt = build_call_array_loc (gg_token_location(), |
| 2235 |
INT, |
| 2236 |
function, |
| 2237 |
argc, |
| 2238 |
args); |
| 2239 |
gg_assign(retval, stmt); |
| 2240 |
gg_write(fd, buffer, gg_strlen(buffer)); |
| 2241 |
|
| 2242 |
gg_free(buffer); |
| 2243 |
gg_pop_context(); |
| 2244 |
return retval; |
| 2245 |
} |
| 2246 |
|
| 2247 |
tree |
| 2248 |
gg_read(tree fd, tree buf, tree count) |
| 2249 |
{ |
| 2250 |
// The C equivalent: "read(fd, buf, count)" |
| 2251 |
|
| 2252 |
// Because the caller might need the ssize_t return value, this routine |
| 2253 |
// returns the statement_decl for the call. It is used this way: |
| 2254 |
|
| 2255 |
// tree num_chars = gg_define_int("_num_chars"); |
| 2256 |
// gg_assign(num_chars, gg_read(fd, buf, count)); |
| 2257 |
|
| 2258 |
return gg_call_expr(SSIZE_T, |
| 2259 |
"read", |
| 2260 |
fd, |
| 2261 |
buf, |
| 2262 |
count, |
| 2263 |
NULL_TREE); |
| 2264 |
} |
| 2265 |
|
| 2266 |
void |
| 2267 |
gg_write(tree fd, tree buf, tree count) |
| 2268 |
{ |
| 2269 |
gg_call(SSIZE_T, |
| 2270 |
"write", |
| 2271 |
fd, |
| 2272 |
buf, |
| 2273 |
count, |
| 2274 |
NULL_TREE); |
| 2275 |
} |
| 2276 |
|
| 2277 |
void |
| 2278 |
gg_memset(tree dest, const tree value, tree size) |
| 2279 |
{ |
| 2280 |
tree the_call = |
| 2281 |
build_call_expr_loc(gg_token_location(), |
| 2282 |
builtin_decl_explicit (BUILT_IN_MEMSET), |
| 2283 |
3, |
| 2284 |
dest, |
| 2285 |
value, |
| 2286 |
size); |
| 2287 |
gg_append_statement(the_call); |
| 2288 |
} |
| 2289 |
|
| 2290 |
tree |
| 2291 |
gg_memchr(tree buf, tree ch, tree length) |
| 2292 |
{ |
| 2293 |
tree the_call = fold_convert( |
| 2294 |
pvoid_type_node, |
| 2295 |
build_call_expr_loc(gg_token_location(), |
| 2296 |
builtin_decl_explicit (BUILT_IN_MEMCHR), |
| 2297 |
3, |
| 2298 |
buf, |
| 2299 |
ch, |
| 2300 |
length)); |
| 2301 |
return the_call; |
| 2302 |
} |
| 2303 |
|
| 2304 |
/* Built-in call to memcpy() */ |
| 2305 |
|
| 2306 |
void |
| 2307 |
gg_memcpy(tree dest, const tree src, tree size) |
| 2308 |
{ |
| 2309 |
tree the_call = build_call_expr_loc( |
| 2310 |
gg_token_location(), |
| 2311 |
builtin_decl_explicit (BUILT_IN_MEMCPY), |
| 2312 |
3, |
| 2313 |
dest, |
| 2314 |
src, |
| 2315 |
size); |
| 2316 |
gg_append_statement(the_call); |
| 2317 |
} |
| 2318 |
|
| 2319 |
/* Built-in call to memmove() */ |
| 2320 |
|
| 2321 |
void |
| 2322 |
gg_memmove(tree dest, const tree src, tree size) |
| 2323 |
{ |
| 2324 |
tree the_call = build_call_expr_loc( |
| 2325 |
gg_token_location(), |
| 2326 |
builtin_decl_explicit (BUILT_IN_MEMMOVE), |
| 2327 |
3, |
| 2328 |
dest, |
| 2329 |
src, |
| 2330 |
size); |
| 2331 |
gg_append_statement(the_call); |
| 2332 |
} |
| 2333 |
|
| 2334 |
tree |
| 2335 |
gg_memdup(tree data, tree length) |
| 2336 |
{ |
| 2337 |
// Duplicates data; gg_free should eventually be called |
| 2338 |
tree retval = gg_define_char_star(); |
| 2339 |
gg_assign(retval, gg_malloc(length)); |
| 2340 |
gg_memcpy(retval, data, length); |
| 2341 |
return retval; |
| 2342 |
} |
| 2343 |
|
| 2344 |
tree |
| 2345 |
gg_memdup(tree data, size_t length) |
| 2346 |
{ |
| 2347 |
// Duplicates data; gg_free should eventually be called |
| 2348 |
tree retval = gg_define_char_star(); |
| 2349 |
gg_assign(retval, gg_malloc(length)); |
| 2350 |
gg_memcpy(retval, data, build_int_cst_type(SIZE_T, length)); |
| 2351 |
return retval; |
| 2352 |
} |
| 2353 |
|
| 2354 |
void |
| 2355 |
gg_strcpy(tree dest, tree src) |
| 2356 |
{ |
| 2357 |
tree the_call = |
| 2358 |
build_call_expr_loc(gg_token_location(), |
| 2359 |
builtin_decl_explicit (BUILT_IN_STRCPY), |
| 2360 |
2, |
| 2361 |
dest, |
| 2362 |
src); |
| 2363 |
gg_append_statement(the_call); |
| 2364 |
} |
| 2365 |
|
| 2366 |
tree |
| 2367 |
gg_strcmp(tree A, tree B) |
| 2368 |
{ |
| 2369 |
tree the_call = fold_convert( |
| 2370 |
integer_type_node, |
| 2371 |
build_call_expr_loc(gg_token_location(), |
| 2372 |
builtin_decl_explicit (BUILT_IN_STRCMP), |
| 2373 |
2, |
| 2374 |
A, |
| 2375 |
B)); |
| 2376 |
return the_call; |
| 2377 |
} |
| 2378 |
|
| 2379 |
tree |
| 2380 |
gg_open(tree char_star_A, tree int_B) |
| 2381 |
{ |
| 2382 |
return gg_call_expr(INT, |
| 2383 |
"open", |
| 2384 |
char_star_A, |
| 2385 |
int_B, |
| 2386 |
NULL_TREE); |
| 2387 |
} |
| 2388 |
|
| 2389 |
tree |
| 2390 |
gg_close(tree int_A) |
| 2391 |
{ |
| 2392 |
return gg_call_expr(INT, |
| 2393 |
"close", |
| 2394 |
int_A, |
| 2395 |
NULL_TREE); |
| 2396 |
} |
| 2397 |
|
| 2398 |
tree |
| 2399 |
gg_strncmp(tree char_star_A, tree char_star_B, tree size_t_N) |
| 2400 |
{ |
| 2401 |
tree the_call = fold_convert( |
| 2402 |
integer_type_node, |
| 2403 |
build_call_expr_loc(gg_token_location(), |
| 2404 |
builtin_decl_explicit (BUILT_IN_STRNCMP), |
| 2405 |
3, |
| 2406 |
char_star_A, |
| 2407 |
char_star_B, |
| 2408 |
size_t_N)); |
| 2409 |
return the_call; |
| 2410 |
} |
| 2411 |
|
| 2412 |
void |
| 2413 |
gg_return(tree operand) |
| 2414 |
{ |
| 2415 |
tree stmt; |
| 2416 |
|
| 2417 |
if( !gg_trans_unit.function_stack.size() ) |
| 2418 |
{ |
| 2419 |
// I put this in to cope with the problem of two END PROGRAM statements, which |
| 2420 |
// should be a syntax error but, as of 2021-02-24, is ignored by GnuCOBOL and |
| 2421 |
// by our parser. |
| 2422 |
return ; |
| 2423 |
} |
| 2424 |
|
| 2425 |
// We have to pop ourselves off of the module_name_stack: |
| 2426 |
gg_call(VOID, |
| 2427 |
"__gg__module_name_pop", |
| 2428 |
NULL_TREE); |
| 2429 |
|
| 2430 |
if( !operand || !DECL_RESULT(current_function->function_decl) ) |
| 2431 |
{ |
| 2432 |
// When there is no operand, or if the function result is void, then |
| 2433 |
// we just generate a return_expr. |
| 2434 |
stmt = build1_loc(gg_token_location(), RETURN_EXPR, void_type_node, NULL_TREE); |
| 2435 |
} |
| 2436 |
else |
| 2437 |
{ |
| 2438 |
// Life is a wee bit more complicated, because we want to return the operand |
| 2439 |
tree function_type = TREE_TYPE(DECL_RESULT(current_function->function_decl)); |
| 2440 |
tree modify = build2( MODIFY_EXPR, |
| 2441 |
function_type, |
| 2442 |
DECL_RESULT(current_function->function_decl), |
| 2443 |
gg_cast(function_type, operand)); |
| 2444 |
stmt = build1_loc(gg_token_location(), RETURN_EXPR, void_type_node, modify); |
| 2445 |
} |
| 2446 |
gg_append_statement(stmt); |
| 2447 |
} |
| 2448 |
|
| 2449 |
void |
| 2450 |
chain_parameter_to_function(tree function_decl, const tree param_type, const char *name) |
| 2451 |
{ |
| 2452 |
tree parm = build_decl (gg_token_location(), |
| 2453 |
PARM_DECL, |
| 2454 |
get_identifier (name), |
| 2455 |
param_type); |
| 2456 |
DECL_CONTEXT(parm) = function_decl; |
| 2457 |
TREE_USED(parm) = 1; |
| 2458 |
DECL_INITIAL(parm) = param_type; |
| 2459 |
|
| 2460 |
if( DECL_ARGUMENTS(function_decl) ) |
| 2461 |
{ |
| 2462 |
chainon(DECL_ARGUMENTS(function_decl),parm); |
| 2463 |
} |
| 2464 |
else |
| 2465 |
{ |
| 2466 |
DECL_ARGUMENTS(function_decl) = parm; |
| 2467 |
} |
| 2468 |
} |
| 2469 |
|
| 2470 |
/* There are five ways that we use function_decls: |
| 2471 |
|
| 2472 |
1, We define a main() entry point. |
| 2473 |
2. We call a function that turns out to be a static "t" function local to the source code module. |
| 2474 |
3. We define an global "T" function, and possibly call it later. |
| 2475 |
4. We call a function that we define later in the source code module. |
| 2476 |
5. We call a function that ends up being an extern that is not defined in the source code module. |
| 2477 |
|
| 2478 |
Cases 3. and 4. turn out to require the same flags. Here are the combinations of |
| 2479 |
flags that are required for each flavor of function_decl. This was empirically |
| 2480 |
determind by compiling a C++ program with sample code for each type. |
| 2481 |
|
| 2482 |
| addressable | used | nothrow | static | external | public | no_instrument |
| 2483 |
main | | | | X | | X | X |
| 2484 |
local | X | X | X | X | | | X |
| 2485 |
external defined inside | X | X | X | X | | X | X |
| 2486 |
external defined elsewhere | X | X | | | X | X | |
| 2487 |
|
| 2488 |
*/ |
| 2489 |
|
| 2490 |
|
| 2491 |
static std::unordered_map<std::string, tree> map_of_function_decls; |
| 2492 |
|
| 2493 |
static |
| 2494 |
std::string function_decl_key(const char *funcname, tree fndecl_type) |
| 2495 |
{ |
| 2496 |
std::string retval; |
| 2497 |
retval += funcname; |
| 2498 |
retval += gg_show_type(TREE_TYPE(fndecl_type)); |
| 2499 |
return retval; |
| 2500 |
} |
| 2501 |
|
| 2502 |
tree |
| 2503 |
gg_peek_fn_decl(const char *funcname, tree fndecl_type) |
| 2504 |
{ |
| 2505 |
// When funcname is found in map_of_function_decls, this routine returns |
| 2506 |
// the type of the return value of that function decl. |
| 2507 |
|
| 2508 |
tree retval = NULL_TREE; |
| 2509 |
std::string key = function_decl_key(funcname, fndecl_type); |
| 2510 |
std::unordered_map<std::string, tree>::const_iterator it = |
| 2511 |
map_of_function_decls.find(key); |
| 2512 |
if( it != map_of_function_decls.end() ) |
| 2513 |
{ |
| 2514 |
// This function_decl has already been defined. |
| 2515 |
retval = TREE_TYPE(TREE_TYPE(it->second)); |
| 2516 |
} |
| 2517 |
return retval; |
| 2518 |
} |
| 2519 |
|
| 2520 |
tree |
| 2521 |
gg_build_fn_decl(const char *funcname, tree fndecl_type) |
| 2522 |
{ |
| 2523 |
tree function_decl; |
| 2524 |
|
| 2525 |
std::string key = function_decl_key(funcname, fndecl_type); |
| 2526 |
std::unordered_map<std::string, tree>::const_iterator it = |
| 2527 |
map_of_function_decls.find(key); |
| 2528 |
if( it != map_of_function_decls.end() ) |
| 2529 |
{ |
| 2530 |
// This function_decl has already been defined. Just return it; the caller |
| 2531 |
// is responsible for modifying it, if necessary. |
| 2532 |
function_decl = it->second; |
| 2533 |
} |
| 2534 |
else |
| 2535 |
{ |
| 2536 |
// When creating a never-seen function_decl, we default to the type used |
| 2537 |
// for calling a function defined elsewhere. It's up to our caller to |
| 2538 |
// modify the flags, for example if this is part of creating a function. |
| 2539 |
|
| 2540 |
function_decl = build_fn_decl(funcname, fndecl_type); |
| 2541 |
|
| 2542 |
// These are the bits shown in the table in the comment up above |
| 2543 |
TREE_ADDRESSABLE(function_decl) = 1; |
| 2544 |
TREE_USED(function_decl) = 1; |
| 2545 |
TREE_NOTHROW(function_decl) = 0; |
| 2546 |
TREE_STATIC(function_decl) = 0; |
| 2547 |
DECL_EXTERNAL (function_decl) = 1; |
| 2548 |
TREE_PUBLIC (function_decl) = 1; |
| 2549 |
DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT(function_decl) = 0; |
| 2550 |
|
| 2551 |
DECL_PRESERVE_P (function_decl) = 0; |
| 2552 |
DECL_ARTIFICIAL(function_decl) = 0; |
| 2553 |
map_of_function_decls[key] = function_decl; |
| 2554 |
} |
| 2555 |
return function_decl; |
| 2556 |
} |
| 2557 |
|
| 2558 |
tree |
| 2559 |
gg_define_function( tree return_type, |
| 2560 |
const char *funcname, |
| 2561 |
const char *unmangled_name, |
| 2562 |
...) |
| 2563 |
{ |
| 2564 |
// This routine builds a function_decl, puts it on the stack, and |
| 2565 |
// gives it a context. |
| 2566 |
|
| 2567 |
// At this time we don't know how many parameters this function expects, so |
| 2568 |
// we set things up and we'll tack on the parameters later. |
| 2569 |
|
| 2570 |
/* There is some bookkeeping we need to do to avoid crashing. |
| 2571 |
|
| 2572 |
It's possible for the source code to have two top-level functions with |
| 2573 |
the same name. This is a compile-time error, but the GCC processing gets |
| 2574 |
upset when it happens. We'll prevent it from happening here: |
| 2575 |
|
| 2576 |
*/ |
| 2577 |
|
| 2578 |
int nparams = 0; |
| 2579 |
|
| 2580 |
tree types[ARG_LIMIT]; |
| 2581 |
const char *names[ARG_LIMIT]; |
| 2582 |
|
| 2583 |
va_list params; |
| 2584 |
va_start(params, unmangled_name); |
| 2585 |
for(;;) |
| 2586 |
{ |
| 2587 |
tree var_type = va_arg(params, tree); |
| 2588 |
if( !var_type ) |
| 2589 |
{ |
| 2590 |
break; |
| 2591 |
} |
| 2592 |
|
| 2593 |
if( TREE_CODE(var_type) >= NUM_TREE_CODES) |
| 2594 |
{ |
| 2595 |
// Warning: This test is not completely reliable, because a garbage |
| 2596 |
// byte could have a valid TREE_CODE. But it does help. |
| 2597 |
yywarn("You forgot to put a %<NULL_TREE%> at the end of a " |
| 2598 |
"%<gg_define_function()%> again"); |
| 2599 |
gcc_unreachable(); |
| 2600 |
} |
| 2601 |
|
| 2602 |
const char *name = va_arg(params, const char *); |
| 2603 |
|
| 2604 |
types[nparams] = var_type; |
| 2605 |
names[nparams] = name; |
| 2606 |
nparams += 1; |
| 2607 |
if(nparams > ARG_LIMIT) |
| 2608 |
{ |
| 2609 |
yywarn("%d parameters? Really? Are you insane?", ARG_LIMIT+1); |
| 2610 |
gcc_unreachable(); |
| 2611 |
} |
| 2612 |
} |
| 2613 |
va_end(params); |
| 2614 |
|
| 2615 |
char ach[32]; |
| 2616 |
std::unordered_set<std::string>::const_iterator it = |
| 2617 |
names_we_have_seen.find(funcname); |
| 2618 |
if( it != names_we_have_seen.end() ) |
| 2619 |
{ |
| 2620 |
static int bum_counter = 1; |
| 2621 |
// We have seen this name before. Replace it with something unique: |
| 2622 |
sprintf(ach, "..no_dupes.%d", bum_counter++); |
| 2623 |
funcname = ach; |
| 2624 |
} |
| 2625 |
else |
| 2626 |
{ |
| 2627 |
names_we_have_seen.insert(funcname); |
| 2628 |
} |
| 2629 |
|
| 2630 |
tree fndecl_type = build_varargs_function_type_array( return_type, |
| 2631 |
nparams, |
| 2632 |
types); |
| 2633 |
|
| 2634 |
// Create the FUNCTION_DECL for that FUNCTION_TYPE |
| 2635 |
tree function_decl = gg_build_fn_decl (funcname, fndecl_type); |
| 2636 |
|
| 2637 |
// This code makes COBOL nested programs actual visible on the |
| 2638 |
// source code "trans_unit_decl" level, but with non-public "static" |
| 2639 |
// visibility. |
| 2640 |
if( gg_trans_unit.function_stack.size() == 0 ) |
| 2641 |
{ |
| 2642 |
// gg_trans_unit.function_stack is empty, so our context is |
| 2643 |
// the compilation module, and we need to be public because this is a |
| 2644 |
// top-level function with global scope: |
| 2645 |
|
| 2646 |
// These are the bits shown in the table for gg_build_fn_decl() |
| 2647 |
TREE_ADDRESSABLE(function_decl) = 1; |
| 2648 |
TREE_USED(function_decl) = 1; |
| 2649 |
TREE_NOTHROW(function_decl) = 1; |
| 2650 |
TREE_STATIC(function_decl) = 1; |
| 2651 |
DECL_EXTERNAL (function_decl) = 0; |
| 2652 |
TREE_PUBLIC (function_decl) = 1; |
| 2653 |
DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT(function_decl) = 1; |
| 2654 |
DECL_CONTEXT (function_decl) = gg_trans_unit.trans_unit_decl; |
| 2655 |
} |
| 2656 |
else |
| 2657 |
{ |
| 2658 |
// The stack has something in it, so we are building a contained |
| 2659 |
// program-id. Such function are implemented local static functions. |
| 2660 |
// |
| 2661 |
// It's not necessarily true that a static call to such a function will be |
| 2662 |
// part of the source code (the call can be through a variable), and so |
| 2663 |
// optimization routines can decide the function isn't used and can |
| 2664 |
// therefore be optimized away. The preserve flag prevents that. |
| 2665 |
|
| 2666 |
// These are the bits shown in the table for gg_build_fn_decl() |
| 2667 |
TREE_ADDRESSABLE(function_decl) = 1; |
| 2668 |
TREE_USED(function_decl) = 1; |
| 2669 |
TREE_NOTHROW(function_decl) = 1; |
| 2670 |
TREE_STATIC(function_decl) = 1; |
| 2671 |
DECL_EXTERNAL (function_decl) = 0; |
| 2672 |
TREE_PUBLIC (function_decl) = 0; |
| 2673 |
DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT(function_decl) = 1; |
| 2674 |
DECL_CONTEXT (function_decl) = gg_trans_unit.trans_unit_decl; |
| 2675 |
DECL_CONTEXT(function_decl) = gg_trans_unit.trans_unit_decl; |
| 2676 |
DECL_PRESERVE_P (function_decl) = 1; |
| 2677 |
gg_append_var_decl(function_decl); |
| 2678 |
} |
| 2679 |
|
| 2680 |
// Chain the names onto the variables list: |
| 2681 |
for(int i=0; i<nparams; i++) |
| 2682 |
{ |
| 2683 |
chain_parameter_to_function(function_decl, types[i], names[i]); |
| 2684 |
} |
| 2685 |
|
| 2686 |
// Establish the RESULT_DECL for the function: |
| 2687 |
tree resdecl = build_decl (gg_token_location(), RESULT_DECL, NULL_TREE, return_type); |
| 2688 |
DECL_CONTEXT (resdecl) = function_decl; |
| 2689 |
DECL_RESULT (function_decl) = resdecl; |
| 2690 |
|
| 2691 |
// The function_decl has a .function member, a pointer to struct_function. |
| 2692 |
// This is quietly, almost invisibly, extremely important. You need to |
| 2693 |
// call this routine after DECL_RESULT has been established: |
| 2694 |
|
| 2695 |
allocate_struct_function(function_decl, false); |
| 2696 |
|
| 2697 |
struct gg_function_t new_function = {}; |
| 2698 |
new_function.context_count = 0; |
| 2699 |
new_function.function_decl = function_decl; |
| 2700 |
new_function.our_name = IDENTIFIER_POINTER(DECL_NAME(function_decl)); |
| 2701 |
new_function.our_unmangled_name = xstrdup(unmangled_name); |
| 2702 |
new_function.function_address = gg_get_address_of(function_decl); |
| 2703 |
|
| 2704 |
// Each program on the stack gets a unique identifier. This is used, for |
| 2705 |
// example, to make sure that static variables have unique names. |
| 2706 |
static size_t program_id = 0; |
| 2707 |
new_function.program_id_number = program_id++; |
| 2708 |
|
| 2709 |
// With everything established, put this function_decl on the stack |
| 2710 |
gg_trans_unit.function_stack.push_back(new_function); |
| 2711 |
|
| 2712 |
// All we need is a context, and we are ready to go: |
| 2713 |
gg_push_context(); |
| 2714 |
return function_decl; |
| 2715 |
} |
| 2716 |
|
| 2717 |
void |
| 2718 |
gg_modify_function_type(tree function_decl, tree return_type) |
| 2719 |
{ |
| 2720 |
tree fndecl_type = build_varargs_function_type_array( return_type, |
| 2721 |
0, // No parameters yet |
| 2722 |
NULL); // And, hence, no types |
| 2723 |
TREE_TYPE(function_decl) = fndecl_type; |
| 2724 |
tree resdecl = build_decl (UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE, return_type); |
| 2725 |
DECL_CONTEXT (resdecl) = function_decl; |
| 2726 |
DECL_RESULT (function_decl) = resdecl; |
| 2727 |
} |
| 2728 |
|
| 2729 |
tree |
| 2730 |
gg_get_function_decl(tree return_type, const char *funcname, ...) |
| 2731 |
{ |
| 2732 |
// This very similar routine creates and returns the function_decl |
| 2733 |
|
| 2734 |
// It was designed for implementing nested functions, in particular |
| 2735 |
// in cases of forward references. Thus, you need to have the function_decl |
| 2736 |
// in order to create the call_expr, even though you don't yet have a body, |
| 2737 |
// and you aren't ready to create it at this time. |
| 2738 |
|
| 2739 |
int nparams = 0; |
| 2740 |
|
| 2741 |
tree types[ARG_LIMIT]; |
| 2742 |
const char *names[ARG_LIMIT]; |
| 2743 |
|
| 2744 |
va_list params; |
| 2745 |
va_start(params,funcname); |
| 2746 |
for(;;) |
| 2747 |
{ |
| 2748 |
tree var_type = va_arg(params, tree); |
| 2749 |
if( !var_type ) |
| 2750 |
{ |
| 2751 |
break; |
| 2752 |
} |
| 2753 |
|
| 2754 |
if( TREE_CODE(var_type) >= NUM_TREE_CODES) |
| 2755 |
{ |
| 2756 |
// Warning: This test is not completely reliable, because a garbage |
| 2757 |
// byte could have a valid TREE_CODE. But it does help. |
| 2758 |
yywarn("You forgot to put a %<NULL_TREE%> at the end of a " |
| 2759 |
"%<gg_define_function()%> again"); |
| 2760 |
gcc_unreachable(); |
| 2761 |
} |
| 2762 |
|
| 2763 |
const char *name = va_arg(params, const char *); |
| 2764 |
|
| 2765 |
types[nparams] = var_type; |
| 2766 |
names[nparams] = name; |
| 2767 |
nparams += 1; |
| 2768 |
if(nparams > ARG_LIMIT) |
| 2769 |
{ |
| 2770 |
yywarn("%d parameters? Really? Are you insane?", |
| 2771 |
ARG_LIMIT+1); |
| 2772 |
gcc_unreachable(); |
| 2773 |
} |
| 2774 |
} |
| 2775 |
va_end(params); |
| 2776 |
|
| 2777 |
// Create the FUNCTION_TYPE for that array: |
| 2778 |
tree fndecl_type = build_varargs_function_type_array( return_type, |
| 2779 |
nparams, |
| 2780 |
types); |
| 2781 |
|
| 2782 |
// Create the FUNCTION_DECL for that FUNCTION_TYPE |
| 2783 |
tree function_decl = build_fn_decl (funcname, fndecl_type); |
| 2784 |
|
| 2785 |
// Some of this stuff is magical, and is based on compiling C programs |
| 2786 |
// and just mimicking the results. |
| 2787 |
TREE_ADDRESSABLE(function_decl) = 1; |
| 2788 |
TREE_STATIC(function_decl) = 1; |
| 2789 |
DECL_EXTERNAL (function_decl) = 0; |
| 2790 |
DECL_PRESERVE_P (function_decl) = 0; |
| 2791 |
DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT(function_decl) = 1; |
| 2792 |
DECL_ARTIFICIAL(function_decl) = 0; |
| 2793 |
TREE_NOTHROW(function_decl) = 0; |
| 2794 |
TREE_USED(function_decl) = 1; |
| 2795 |
|
| 2796 |
if( gg_trans_unit.function_stack.size() == 0 ) |
| 2797 |
{ |
| 2798 |
// gg_trans_unit.function_stack is empty, so our context is |
| 2799 |
// the compilation module, and we need to be public: |
| 2800 |
DECL_CONTEXT (function_decl) = gg_trans_unit.trans_unit_decl; |
| 2801 |
TREE_PUBLIC(function_decl) = 1; |
| 2802 |
} |
| 2803 |
else |
| 2804 |
{ |
| 2805 |
// The stack has something in it, so we are building a nested function. |
| 2806 |
// Make the current function our context |
| 2807 |
DECL_CONTEXT (function_decl) = current_function->function_decl; |
| 2808 |
TREE_PUBLIC(function_decl) = 0; |
| 2809 |
DECL_STATIC_CHAIN(function_decl) = 1; |
| 2810 |
} |
| 2811 |
|
| 2812 |
// Chain the names onto the variables list: |
| 2813 |
for(int i=0; i<nparams; i++) |
| 2814 |
{ |
| 2815 |
chain_parameter_to_function(function_decl, types[i], names[i]); |
| 2816 |
} |
| 2817 |
|
| 2818 |
// Establish the RESULT_DECL for the function: |
| 2819 |
tree resdecl = build_decl (gg_token_location(), RESULT_DECL, NULL_TREE, return_type); |
| 2820 |
DECL_CONTEXT (resdecl) = function_decl; |
| 2821 |
DECL_RESULT (function_decl) = resdecl; |
| 2822 |
|
| 2823 |
// The function_decl has a .function member, a pointer to struct_function. |
| 2824 |
// This is quietly, almost invisibly, extremely important. You need to |
| 2825 |
// call this routine after DECL_RESULT has been established: |
| 2826 |
allocate_struct_function(function_decl, false); |
| 2827 |
|
| 2828 |
// It will be the caller's responsibility to push this function_decl onto |
| 2829 |
// the stack at the appropriate time, and create the appropriate context. |
| 2830 |
return function_decl; |
| 2831 |
} |
| 2832 |
|
| 2833 |
void |
| 2834 |
gg_finalize_function() |
| 2835 |
{ |
| 2836 |
// Unless it has already been handled: |
| 2837 |
if( !gg_trans_unit.function_stack.size() ) |
| 2838 |
{ |
| 2839 |
return ; |
| 2840 |
} |
| 2841 |
|
| 2842 |
// Finish off the context |
| 2843 |
gg_pop_context(); |
| 2844 |
|
| 2845 |
/* Because COBOL functions can be misleadingly referenced before they |
| 2846 |
defined, and because our compiler is single pass, we need to defer |
| 2847 |
actually passing the function_decls to the middle end until we are |
| 2848 |
done with the entire compilation unit. |
| 2849 |
|
| 2850 |
An actual example: |
| 2851 |
|
| 2852 |
IDENTIFICATION DIVISION. |
| 2853 |
PROGRAM-ID. A. |
| 2854 |
DATA DIVISION. |
| 2855 |
WORKING-STORAGE SECTION. |
| 2856 |
01 CWD PIC X(100). |
| 2857 |
01 LEN_OF_CWD PIC 999 VALUE 100. |
| 2858 |
PROCEDURE DIVISION. |
| 2859 |
CALL "getcwd" USING BY REFERENCE CWD BY VALUE LEN_OF_CWD |
| 2860 |
DISPLAY CWD |
| 2861 |
goback. |
| 2862 |
END PROGRAM A. |
| 2863 |
IDENTIFICATION DIVISION. |
| 2864 |
PROGRAM-ID. B. |
| 2865 |
DATA DIVISION. |
| 2866 |
WORKING-STORAGE SECTION. |
| 2867 |
01 CWD PIC X(100). |
| 2868 |
01 RETURNED-CWD PIC X(100). |
| 2869 |
01 LEN_OF_CWD PIC 999 VALUE 100. |
| 2870 |
PROCEDURE DIVISION. |
| 2871 |
CALL "getcwd" USING BY REFERENCE CWD BY VALUE LEN_OF_CWD RETURNING RETURNED-CWD |
| 2872 |
DISPLAY RETURNED-CWD |
| 2873 |
goback. |
| 2874 |
END PROGRAM B. |
| 2875 |
|
| 2876 |
When we encounter the first call to getcwd, we have no clue as to the |
| 2877 |
type of the return value, so we assume it is COBOL_FUNCTION_RETURN_TYPE |
| 2878 |
|
| 2879 |
When we encounter the second call, we learn that it returns CHAR_P. But |
| 2880 |
an attempt to change the return type of the function_decl will result |
| 2881 |
in problems if the function_decl of A is processed by the middle end |
| 2882 |
before we get a chance to change the getcwd functiona_decl. |
| 2883 |
|
| 2884 |
Hence the need for finalized_function_decls, which gets processed |
| 2885 |
at the end of the file. */ |
| 2886 |
|
| 2887 |
finalized_function_decls.push_back(current_function->function_decl); |
| 2888 |
|
| 2889 |
dump_function (TDI_original, current_function->function_decl); |
| 2890 |
|
| 2891 |
if( gg_trans_unit.function_stack.back().context_count ) |
| 2892 |
{ |
| 2893 |
cbl_internal_error("Residual context count"); |
| 2894 |
} |
| 2895 |
|
| 2896 |
gg_trans_unit.function_stack.pop_back(); |
| 2897 |
} |
| 2898 |
|
| 2899 |
void |
| 2900 |
gg_leaving_the_source_code_file() |
| 2901 |
{ |
| 2902 |
for( std::vector<tree>::const_iterator it=finalized_function_decls.begin(); |
| 2903 |
it != finalized_function_decls.end(); |
| 2904 |
it++ ) |
| 2905 |
{ |
| 2906 |
//This makes the function visible on the source code module level. |
| 2907 |
cgraph_node::finalize_function(*it, true); |
| 2908 |
} |
| 2909 |
} |
| 2910 |
|
| 2911 |
void |
| 2912 |
gg_push_context() |
| 2913 |
{ |
| 2914 |
// Sit back, relax, prepare to be amazed. |
| 2915 |
|
| 2916 |
// functions need a context in which they build variables and whatnot. |
| 2917 |
// they also need to be able to create subcontexts. |
| 2918 |
|
| 2919 |
// Functions have an DECL_INITIAL member that points to the first block. The |
| 2920 |
// first block has a BLOCK_VARS member that points to the first of a chain |
| 2921 |
// of var_decl entries. The first block has a BLOCK_SUBBLOCKS member that |
| 2922 |
// points to the block of the first subcontext. |
| 2923 |
|
| 2924 |
// Functions have a DECL_SAVED_TREE member that points to the first bind_expr |
| 2925 |
// That first bind_expr has a BIND_EXPR_BLOCK that points back to the first block |
| 2926 |
// has a BIND_EXPR_VARS that points back to the first block's first var_decl |
| 2927 |
// has a BIND_EXPR_BODY that points to the first statement_list |
| 2928 |
|
| 2929 |
// Each subsequent context gets a new block that is chained to the prior block through BLOCK_SUBBLOCKS |
| 2930 |
// Each subsequent context gets a new bind_expr which gets added to the parent context's statement list |
| 2931 |
|
| 2932 |
// Yes, it's confusing. Have a nice lie-down. |
| 2933 |
|
| 2934 |
// Here's what we need for this recipe: |
| 2935 |
|
| 2936 |
// We need a block: |
| 2937 |
tree block = make_node(BLOCK); |
| 2938 |
TREE_USED(block) = 1; |
| 2939 |
BLOCK_SUPERCONTEXT(block) = current_function->function_decl; |
| 2940 |
|
| 2941 |
// We need a statement list: |
| 2942 |
tree statement_list = alloc_stmt_list(); |
| 2943 |
|
| 2944 |
// We need a bind_expr: |
| 2945 |
tree bind_expr = build3(BIND_EXPR, |
| 2946 |
void_type_node, |
| 2947 |
NULL_TREE, // There are no vars yet. |
| 2948 |
statement_list, |
| 2949 |
block); |
| 2950 |
TREE_SIDE_EFFECTS(bind_expr) = 1; |
| 2951 |
|
| 2952 |
// At this point, we might be creating the initial context for a function, |
| 2953 |
// or we might be creating a sub-context. |
| 2954 |
|
| 2955 |
if( !DECL_INITIAL(current_function->function_decl) ) |
| 2956 |
{ |
| 2957 |
// We are creating the initial context of the function: |
| 2958 |
DECL_INITIAL(current_function->function_decl) = block; |
| 2959 |
DECL_SAVED_TREE(current_function->function_decl) = bind_expr; |
| 2960 |
|
| 2961 |
// To avoid an N-squared time complexity when chaining blocks, we save the |
| 2962 |
// current end of the chain of blocks: |
| 2963 |
current_function->current_block = block; |
| 2964 |
} |
| 2965 |
else |
| 2966 |
{ |
| 2967 |
// We are in the subtext business: |
| 2968 |
|
| 2969 |
// We need to tack on our new block to the end of the |
| 2970 |
// chain of existing blocks: |
| 2971 |
tree cblock = current_function->current_block; |
| 2972 |
BLOCK_SUBBLOCKS(cblock) = block; |
| 2973 |
current_function->current_block = block; |
| 2974 |
|
| 2975 |
// And we need to put our new bind_expr onto the end of the |
| 2976 |
// current active statement list: |
| 2977 |
gg_append_statement(bind_expr); |
| 2978 |
} |
| 2979 |
|
| 2980 |
// And now we make our statement_list and bind_expr the active ones: |
| 2981 |
current_function->statement_list_stack.push_back(statement_list); |
| 2982 |
current_function->bind_expr_stack.push_back(bind_expr); |
| 2983 |
|
| 2984 |
// And the new context is ready to rock and roll |
| 2985 |
gg_trans_unit.function_stack.back().context_count += 1; |
| 2986 |
} |
| 2987 |
|
| 2988 |
void |
| 2989 |
gg_pop_context() |
| 2990 |
{ |
| 2991 |
// Backing out is much easier: |
| 2992 |
current_function->bind_expr_stack.pop_back(); |
| 2993 |
current_function->statement_list_stack.pop_back(); |
| 2994 |
|
| 2995 |
gg_trans_unit.function_stack.back().context_count -= 1; |
| 2996 |
} |
| 2997 |
|
| 2998 |
static |
| 2999 |
std::unordered_map<std::string, tree> fndecl_from_name; |
| 3000 |
|
| 3001 |
static |
| 3002 |
tree |
| 3003 |
function_decl_from_name(tree return_type, |
| 3004 |
const char *function_name, |
| 3005 |
int nargs, |
| 3006 |
tree arg_types[]) |
| 3007 |
{ |
| 3008 |
tree fndecl; |
| 3009 |
std::unordered_map<std::string, tree>::const_iterator it = |
| 3010 |
fndecl_from_name.find(function_name); |
| 3011 |
if( it != fndecl_from_name.end() ) |
| 3012 |
{ |
| 3013 |
fndecl = it->second; |
| 3014 |
} |
| 3015 |
else |
| 3016 |
{ |
| 3017 |
tree fntype = build_function_type_array(return_type, nargs, arg_types); |
| 3018 |
fndecl = build_fn_decl (function_name, fntype); |
| 3019 |
fndecl_from_name[function_name] = fndecl; |
| 3020 |
} |
| 3021 |
return fndecl; |
| 3022 |
} |
| 3023 |
|
| 3024 |
tree |
| 3025 |
gg_call_expr(tree return_type, const char *function_name, ...) |
| 3026 |
{ |
| 3027 |
// Generalized caller. Params are terminated with NULL_TREE |
| 3028 |
|
| 3029 |
// Use this routine to call function_name when you need the return value. |
| 3030 |
// Typically you will do something like |
| 3031 |
|
| 3032 |
// tree call_expr = gg_call_expr(...); |
| 3033 |
// gg_assign( dest, call_expr ); |
| 3034 |
|
| 3035 |
// Note that everyt time call_expr is laid down, the function will be called, |
| 3036 |
// so you probably don't want to do things like |
| 3037 |
// gg_assign( dest1, call_expr ); |
| 3038 |
// gg_assign( dest2, call_expr ); |
| 3039 |
|
| 3040 |
int nargs = 0; |
| 3041 |
static tree arg_types[ARG_LIMIT+1]; |
| 3042 |
static tree args[ARG_LIMIT+1]; |
| 3043 |
|
| 3044 |
va_list ap; |
| 3045 |
va_start(ap, function_name); |
| 3046 |
for(;;) |
| 3047 |
{ |
| 3048 |
if(nargs >= ARG_LIMIT) |
| 3049 |
{ |
| 3050 |
yywarn("You *must* be joking"); |
| 3051 |
gcc_unreachable(); |
| 3052 |
} |
| 3053 |
|
| 3054 |
tree arg = va_arg(ap, tree); |
| 3055 |
|
| 3056 |
if( arg == NULL_TREE ) |
| 3057 |
{ |
| 3058 |
break; |
| 3059 |
} |
| 3060 |
|
| 3061 |
arg_types[nargs] = TREE_TYPE(arg); |
| 3062 |
args[nargs] = arg; |
| 3063 |
nargs += 1; |
| 3064 |
} |
| 3065 |
arg_types[nargs] = NULL_TREE; |
| 3066 |
args[nargs] = NULL_TREE; |
| 3067 |
va_end (ap); |
| 3068 |
|
| 3069 |
tree function_decl = function_decl_from_name( return_type, |
| 3070 |
function_name, |
| 3071 |
nargs, |
| 3072 |
arg_types); |
| 3073 |
DECL_EXTERNAL (function_decl) = 1; |
| 3074 |
tree the_func_addr = build1(ADDR_EXPR, |
| 3075 |
build_pointer_type (TREE_TYPE(function_decl)), |
| 3076 |
function_decl); |
| 3077 |
tree the_call = build_call_array_loc(gg_token_location(), |
| 3078 |
return_type, |
| 3079 |
the_func_addr, |
| 3080 |
nargs, |
| 3081 |
args); |
| 3082 |
// This routine returns the call_expr; the caller will have to deal with it |
| 3083 |
// as described up above |
| 3084 |
return the_call; |
| 3085 |
} |
| 3086 |
|
| 3087 |
void |
| 3088 |
gg_call(tree return_type, const char *function_name, ...) |
| 3089 |
{ |
| 3090 |
// Generalized caller. function_name is followed by a NULL_TREE-terminated |
| 3091 |
// list of formal parameters. |
| 3092 |
|
| 3093 |
// Use this routine when you don't care about the return value, and |
| 3094 |
// you want the subroutine to be invoked. |
| 3095 |
|
| 3096 |
int nargs = 0; |
| 3097 |
static tree arg_types[ARG_LIMIT+1]; |
| 3098 |
static tree args[ARG_LIMIT+1]; |
| 3099 |
|
| 3100 |
va_list ap; |
| 3101 |
va_start(ap, function_name); |
| 3102 |
for(;;) |
| 3103 |
{ |
| 3104 |
if(nargs >= ARG_LIMIT) |
| 3105 |
{ |
| 3106 |
yywarn("You *must* be joking"); |
| 3107 |
gcc_unreachable(); |
| 3108 |
} |
| 3109 |
|
| 3110 |
tree arg = va_arg(ap, tree); |
| 3111 |
|
| 3112 |
if( arg == NULL_TREE ) |
| 3113 |
{ |
| 3114 |
break; |
| 3115 |
} |
| 3116 |
|
| 3117 |
arg_types[nargs] = TREE_TYPE(arg); |
| 3118 |
args[nargs] = arg; |
| 3119 |
nargs += 1; |
| 3120 |
} |
| 3121 |
arg_types[nargs] = NULL_TREE; |
| 3122 |
args[nargs] = NULL_TREE; |
| 3123 |
va_end (ap); |
| 3124 |
|
| 3125 |
tree function_decl = function_decl_from_name( return_type, |
| 3126 |
function_name, |
| 3127 |
nargs, |
| 3128 |
arg_types); |
| 3129 |
DECL_EXTERNAL (function_decl) = 1; |
| 3130 |
tree the_func_addr = build1(ADDR_EXPR, |
| 3131 |
build_pointer_type (TREE_TYPE(function_decl)), |
| 3132 |
function_decl); |
| 3133 |
tree the_call = build_call_array_loc(gg_token_location(), |
| 3134 |
return_type, |
| 3135 |
the_func_addr, |
| 3136 |
nargs, |
| 3137 |
args); |
| 3138 |
// This simply executes the_call; any return value is ignored |
| 3139 |
gg_append_statement(the_call); |
| 3140 |
} |
| 3141 |
|
| 3142 |
tree |
| 3143 |
gg_call_expr_list(tree return_type, tree function_pointer, int param_count, tree args[]) |
| 3144 |
{ |
| 3145 |
// Generalized caller. param_count is the count of params in the arg[]] |
| 3146 |
|
| 3147 |
// Use this routine when you need the return value. Typically you |
| 3148 |
// will do something like |
| 3149 |
|
| 3150 |
// tree call_expr_Plist = gg_call_expr_list(...); |
| 3151 |
// gg_append_statement(call_expr); |
| 3152 |
|
| 3153 |
// Note that every time call_expr is invoked, the routine will run again. |
| 3154 |
|
| 3155 |
// Avoid that with something like |
| 3156 |
// gg_assign( dest, gg_call_expr_list(...) ); |
| 3157 |
|
| 3158 |
tree the_call = build_call_array_loc(gg_token_location(), |
| 3159 |
return_type, |
| 3160 |
function_pointer, |
| 3161 |
param_count, |
| 3162 |
args); |
| 3163 |
// This routine returns the call_expr; the caller will have to deal with it |
| 3164 |
// as described up above |
| 3165 |
return the_call; |
| 3166 |
} |
| 3167 |
|
| 3168 |
tree |
| 3169 |
gg_create_bind_expr() |
| 3170 |
{ |
| 3171 |
// In support of things like PERFORM paragraph, we need to create |
| 3172 |
// blocks of statements that can be executed. |
| 3173 |
|
| 3174 |
// This will be a naked bind_expr, like we use for WHILE construction. |
| 3175 |
// It's not defining a context, so it has no variable list, nor does |
| 3176 |
// it point to a block. |
| 3177 |
|
| 3178 |
tree statement_block = make_node(STATEMENT_LIST); |
| 3179 |
TREE_TYPE(statement_block) = void_type_node; |
| 3180 |
tree bind_expr = build3( BIND_EXPR, |
| 3181 |
void_type_node, |
| 3182 |
NULL_TREE, |
| 3183 |
statement_block, |
| 3184 |
NULL_TREE); |
| 3185 |
|
| 3186 |
return bind_expr; |
| 3187 |
} |
| 3188 |
|
| 3189 |
void |
| 3190 |
gg_exit(tree exit_code) |
| 3191 |
{ |
| 3192 |
tree the_call = |
| 3193 |
build_call_expr_loc(gg_token_location(), |
| 3194 |
builtin_decl_explicit (BUILT_IN_EXIT), |
| 3195 |
1, |
| 3196 |
exit_code); |
| 3197 |
gg_append_statement(the_call); |
| 3198 |
} |
| 3199 |
|
| 3200 |
void |
| 3201 |
gg_abort() |
| 3202 |
{ |
| 3203 |
tree the_call = |
| 3204 |
build_call_expr_loc(gg_token_location(), |
| 3205 |
builtin_decl_explicit (BUILT_IN_ABORT), |
| 3206 |
0); |
| 3207 |
gg_append_statement(the_call); |
| 3208 |
} |
| 3209 |
|
| 3210 |
tree |
| 3211 |
gg_strlen(tree psz) |
| 3212 |
{ |
| 3213 |
tree the_call = fold_convert( |
| 3214 |
size_type_node, |
| 3215 |
build_call_expr_loc(gg_token_location(), |
| 3216 |
builtin_decl_explicit (BUILT_IN_STRLEN), |
| 3217 |
1, |
| 3218 |
psz)); |
| 3219 |
return the_call; |
| 3220 |
} |
| 3221 |
|
| 3222 |
tree |
| 3223 |
gg_strdup(tree psz) |
| 3224 |
{ |
| 3225 |
tree the_call = fold_convert( |
| 3226 |
build_pointer_type(char_type_node), |
| 3227 |
build_call_expr_loc(gg_token_location(), |
| 3228 |
builtin_decl_explicit (BUILT_IN_STRDUP), |
| 3229 |
1, |
| 3230 |
psz)); |
| 3231 |
return the_call; |
| 3232 |
} |
| 3233 |
|
| 3234 |
/* built_in call to malloc() */ |
| 3235 |
|
| 3236 |
tree |
| 3237 |
gg_malloc(tree size) |
| 3238 |
{ |
| 3239 |
tree the_call = fold_convert( |
| 3240 |
pvoid_type_node, |
| 3241 |
build_call_expr_loc(gg_token_location(), |
| 3242 |
builtin_decl_explicit (BUILT_IN_MALLOC), |
| 3243 |
1, |
| 3244 |
size)); |
| 3245 |
return the_call; |
| 3246 |
} |
| 3247 |
|
| 3248 |
tree |
| 3249 |
gg_realloc(tree base, tree size) |
| 3250 |
{ |
| 3251 |
tree the_call = fold_convert( |
| 3252 |
pvoid_type_node, |
| 3253 |
build_call_expr_loc(gg_token_location(), |
| 3254 |
builtin_decl_explicit (BUILT_IN_REALLOC), |
| 3255 |
2, |
| 3256 |
base, |
| 3257 |
size)); |
| 3258 |
return the_call; |
| 3259 |
} |
| 3260 |
|
| 3261 |
tree |
| 3262 |
gg_realloc(tree base, size_t size) |
| 3263 |
{ |
| 3264 |
return gg_realloc(base, build_int_cst_type(SIZE_T, size)); |
| 3265 |
} |
| 3266 |
|
| 3267 |
tree |
| 3268 |
gg_malloc(size_t size) |
| 3269 |
{ |
| 3270 |
return gg_malloc(build_int_cst_type(SIZE_T, size)); |
| 3271 |
} |
| 3272 |
|
| 3273 |
void |
| 3274 |
gg_free(tree pointer) |
| 3275 |
{ |
| 3276 |
tree the_call = |
| 3277 |
build_call_expr_loc(gg_token_location(), |
| 3278 |
builtin_decl_explicit (BUILT_IN_FREE), |
| 3279 |
1, |
| 3280 |
pointer); |
| 3281 |
gg_append_statement(the_call); |
| 3282 |
} |
| 3283 |
|
| 3284 |
void |
| 3285 |
gg_record_statement_list_start() |
| 3286 |
{ |
| 3287 |
// We need a statement list: |
| 3288 |
tree statement_list = alloc_stmt_list(); |
| 3289 |
current_function->statement_list_stack.push_back(statement_list); |
| 3290 |
} |
| 3291 |
|
| 3292 |
tree |
| 3293 |
gg_record_statement_list_finish() |
| 3294 |
{ |
| 3295 |
tree retval = current_function->statement_list_stack.back(); |
| 3296 |
current_function->statement_list_stack.pop_back(); |
| 3297 |
return retval; |
| 3298 |
} |
| 3299 |
|
| 3300 |
size_t |
| 3301 |
gg_sizeof(tree node) |
| 3302 |
{ |
| 3303 |
size_t size_in_bytes; |
| 3304 |
if( DECL_P(node) ) |
| 3305 |
{ |
| 3306 |
size_in_bytes = TREE_INT_CST_LOW(TYPE_SIZE_UNIT(TREE_TYPE(node))); |
| 3307 |
} |
| 3308 |
else |
| 3309 |
{ |
| 3310 |
gcc_assert(TYPE_P(node)); |
| 3311 |
size_in_bytes = TREE_INT_CST_LOW(TYPE_SIZE_UNIT(node)); |
| 3312 |
} |
| 3313 |
return size_in_bytes; |
| 3314 |
} |
| 3315 |
|
| 3316 |
tree |
| 3317 |
gg_array_of_size_t( size_t N, size_t *values) |
| 3318 |
{ |
| 3319 |
tree retval = gg_define_variable(build_pointer_type(SIZE_T)); |
| 3320 |
tree sz = build_int_cst_type(SIZE_T, N * int_size_in_bytes(SIZE_T)); |
| 3321 |
gg_assign(retval, gg_cast(build_pointer_type(SIZE_T), gg_malloc(sz))); |
| 3322 |
for(size_t i=0; i<N; i++) |
| 3323 |
{ |
| 3324 |
gg_assign(gg_array_value(retval, i), build_int_cst_type(SIZE_T, values[i])); |
| 3325 |
} |
| 3326 |
return retval; |
| 3327 |
} |
| 3328 |
|
| 3329 |
tree |
| 3330 |
gg_array_of_bytes( size_t N, unsigned char *values) |
| 3331 |
{ |
| 3332 |
tree retval = gg_define_variable(UCHAR_P); |
| 3333 |
gg_assign(retval, gg_cast(UCHAR_P, gg_malloc( build_int_cst_type(SIZE_T, N)))); |
| 3334 |
for(size_t i=0; i<N; i++) |
| 3335 |
{ |
| 3336 |
gg_assign(gg_array_value(retval, i), build_int_cst_type(UCHAR, values[i])); |
| 3337 |
} |
| 3338 |
return retval; |
| 3339 |
} |
| 3340 |
|
| 3341 |
tree |
| 3342 |
gg_string_literal(const char *string) |
| 3343 |
{ |
| 3344 |
/* This is a message in a bottle. |
| 3345 |
|
| 3346 |
A genapi.cc program calling |
| 3347 |
|
| 3348 |
gg_call(VOID, |
| 3349 |
"puts", |
| 3350 |
build_string_literal(strlen(ach)+1, ach), |
| 3351 |
NULL_TREE); |
| 3352 |
|
| 3353 |
ten thousand times compiles about ten percent slower than a C program |
| 3354 |
calling |
| 3355 |
|
| 3356 |
puts(ach); |
| 3357 |
|
| 3358 |
ten thousand times. |
| 3359 |
|
| 3360 |
Trapping through the C front end reveals that they do not call |
| 3361 |
build_string_literal(). They instead use build_string() in a way that |
| 3362 |
I gave up trying to figure out that produces, apparently, more efficient |
| 3363 |
GENERIC. |
| 3364 |
|
| 3365 |
Their GENERIC: call_expr -> nop_expr -> addr_expr -> string_cst |
| 3366 |
|
| 3367 |
My GENERIC: call_expr -> addr_expr -> array_ref -> string_cst |
| 3368 |
|
| 3369 |
I tried for an hour to duplicate the C stuff, but made no headway. |
| 3370 |
|
| 3371 |
This comment is a reminder to myself to investigate this, someday, because |
| 3372 |
I eventually want that ten percent. |
| 3373 |
*/ |
| 3374 |
|
| 3375 |
return build_string_literal(strlen(string)+1, string); |
| 3376 |
} |
| 3377 |
|
| 3378 |
tree |
| 3379 |
gg_trans_unit_var_decl(const char *var_name) |
| 3380 |
{ |
| 3381 |
std::unordered_map<std::string, tree>::const_iterator it = |
| 3382 |
gg_trans_unit.trans_unit_var_decls.find(var_name); |
| 3383 |
if( it != gg_trans_unit.trans_unit_var_decls.end() ) |
| 3384 |
{ |
| 3385 |
return it->second; |
| 3386 |
} |
| 3387 |
return NULL_TREE; |
| 3388 |
} |
| 3389 |
|
| 3390 |
#pragma GCC diagnostic push |
| 3391 |
#pragma GCC diagnostic ignored "-Wsuggest-attribute=format" |
| 3392 |
|
| 3393 |
void |
| 3394 |
gg_insert_into_assembler(const char ach[]) |
| 3395 |
{ |
| 3396 |
if( !optimize ) |
| 3397 |
{ |
| 3398 |
// Create the required generic tag |
| 3399 |
tree asm_expr = build5_loc( gg_token_location(), |
| 3400 |
ASM_EXPR, |
| 3401 |
VOID, |
| 3402 |
build_string(strlen(ach), ach), |
| 3403 |
NULL_TREE, |
| 3404 |
NULL_TREE, |
| 3405 |
NULL_TREE, |
| 3406 |
NULL_TREE); |
| 3407 |
|
| 3408 |
// And insert it as a statement |
| 3409 |
gg_append_statement(asm_expr); |
| 3410 |
} |
| 3411 |
} |
| 3412 |
|
| 3413 |
void |
| 3414 |
gg_insert_into_assemblerf(const char *format, ...) |
| 3415 |
{ |
| 3416 |
// Temporarily defeat all ASM_EXPR for optimized code per PR119214 |
| 3417 |
// The correct solution using LABEL_DECL is forthcoming |
| 3418 |
if( !optimize ) |
| 3419 |
{ |
| 3420 |
// This routine inserts text directly into the assembly language stream. |
| 3421 |
|
| 3422 |
// Note that if for some reason your text has to have a '%' character, it |
| 3423 |
// needs to be doubled in the GENERIC tag. And that means if it is in the |
| 3424 |
// 'format' variable, it needs to be quadrupled. |
| 3425 |
|
| 3426 |
// Create the string to be inserted: |
| 3427 |
char ach[256]; |
| 3428 |
va_list ap; |
| 3429 |
va_start(ap, format); |
| 3430 |
vsnprintf(ach, sizeof(ach), format, ap); |
| 3431 |
va_end(ap); |
| 3432 |
|
| 3433 |
gg_insert_into_assembler(ach); |
| 3434 |
} |
| 3435 |
} |
| 3436 |
#pragma GCC diagnostic pop |
| 3437 |
|
| 3438 |
static location_t sv_token_location_override = 0; |
| 3439 |
|
| 3440 |
void |
| 3441 |
token_location_override(location_t loc) |
| 3442 |
{ |
| 3443 |
sv_token_location_override = loc; |
| 3444 |
} |
| 3445 |
|
| 3446 |
location_t |
| 3447 |
gg_token_location() |
| 3448 |
{ |
| 3449 |
location_t retval; |
| 3450 |
if( sv_token_location_override ) |
| 3451 |
{ |
| 3452 |
retval = sv_token_location_override; |
| 3453 |
sv_token_location_override = 0; |
| 3454 |
} |
| 3455 |
else |
| 3456 |
{ |
| 3457 |
retval = current_token_location(); |
| 3458 |
} |
| 3459 |
return retval; |
| 3460 |
} |