| 1 |
/* gcobol backend interface |
| 2 |
Copyright (C) 2021-2025 Free Software Foundation, Inc. |
| 3 |
Contributed by Robert J. Dubner and James K. Lowden |
| 4 |
|
| 5 |
This file is part of GCC. |
| 6 |
|
| 7 |
GCC is free software; you can redistribute it and/or modify it under |
| 8 |
the terms of the GNU General Public License as published by the Free |
| 9 |
Software Foundation; either version 3, or (at your option) any later |
| 10 |
version. |
| 11 |
|
| 12 |
GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 |
for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with GCC; see the file COPYING3. If not see |
| 19 |
<http://www.gnu.org/licenses/>. */ |
| 20 |
|
| 21 |
|
| 22 |
#include "cobol-system.h" |
| 23 |
#include <coretypes.h> |
| 24 |
#include <tree.h> |
| 25 |
#include <diagnostic.h> |
| 26 |
#include <opts.h> |
| 27 |
#include <debug.h> |
| 28 |
#include <langhooks.h> |
| 29 |
#include <langhooks-def.h> |
| 30 |
#include <target.h> |
| 31 |
#include <stringpool.h> |
| 32 |
#include "../../libgcobol/ec.h" |
| 33 |
#include "../../libgcobol/common-defs.h" |
| 34 |
#include "util.h" |
| 35 |
#include "cbldiag.h" |
| 36 |
#include "symbols.h" |
| 37 |
#include "inspect.h" |
| 38 |
#include "../../libgcobol/io.h" |
| 39 |
#include "genapi.h" |
| 40 |
#include "../../libgcobol/exceptl.h" |
| 41 |
#include "exceptg.h" |
| 42 |
#include "gengen.h" // This has some GTY(()) markers |
| 43 |
#include "structs.h" // This has some GTY(()) markers |
| 44 |
|
| 45 |
/* Required language-dependent contents of a type. |
| 46 |
|
| 47 |
Without it, we get |
| 48 |
|
| 49 |
gt-cobol-cobol1.h:858: undefined reference to `gt_pch_nx_lang_type(void *) |
| 50 |
|
| 51 |
*/ |
| 52 |
|
| 53 |
struct GTY (()) lang_type |
| 54 |
{ |
| 55 |
char dummy; |
| 56 |
}; |
| 57 |
|
| 58 |
/* Language-dependent contents of a decl. |
| 59 |
Without it, we get |
| 60 |
|
| 61 |
gt-cobol-cobol1.h:674: more undefined references to `gt_pch_nx_lang_decl |
| 62 |
|
| 63 |
*/ |
| 64 |
|
| 65 |
struct GTY (()) lang_decl |
| 66 |
{ |
| 67 |
char dummy; |
| 68 |
}; |
| 69 |
|
| 70 |
/* |
| 71 |
* Language-dependent contents of an identifier. |
| 72 |
* This must include a tree_identifier. |
| 73 |
*/ |
| 74 |
struct GTY (()) lang_identifier |
| 75 |
{ |
| 76 |
struct tree_identifier common; |
| 77 |
}; |
| 78 |
|
| 79 |
/* The resulting tree type. */ |
| 80 |
|
| 81 |
union GTY ((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"), |
| 82 |
chain_next ("CODE_CONTAINS_STRUCT (TREE_CODE (&%h.generic), " |
| 83 |
"TS_COMMON) ? ((union lang_tree_node *) TREE_CHAIN " |
| 84 |
"(&%h.generic)) : NULL"))) lang_tree_node |
| 85 |
{ |
| 86 |
union tree_node GTY ((tag ("0"), desc ("tree_node_structure (&%h)"))) generic; |
| 87 |
struct lang_identifier GTY ((tag ("1"))) identifier; |
| 88 |
}; |
| 89 |
|
| 90 |
/* We don't use language_function. |
| 91 |
|
| 92 |
But without the placeholder: |
| 93 |
|
| 94 |
/usr/bin/ld: gtype-desc.o: in function `gt_ggc_mx_function(void*)': |
| 95 |
../build/gcc/gtype-desc.cc:1763: undefined reference to `gt_ggc_mx_language_function(void*)' |
| 96 |
/usr/bin/ld: gtype-desc.o: in function `gt_pch_nx_function(void*)': |
| 97 |
../build/gcc/gtype-desc.cc:5727: undefined reference to `gt_pch_nx_language_function(void*)' |
| 98 |
|
| 99 |
*/ |
| 100 |
|
| 101 |
struct GTY (()) language_function |
| 102 |
{ |
| 103 |
int dummy; |
| 104 |
}; |
| 105 |
|
| 106 |
/* |
| 107 |
* Language hooks. |
| 108 |
*/ |
| 109 |
|
| 110 |
#define ATTR_NULL 0 |
| 111 |
#define ATTR_LEAF_LIST (ECF_LEAF) |
| 112 |
#define ATTR_NOTHROW_LEAF_LIST (ECF_NOTHROW | ECF_LEAF) |
| 113 |
#define ATTR_NOTHROW_LEAF_MALLOC_LIST (ECF_NOTHROW | ECF_LEAF | ECF_MALLOC) |
| 114 |
#define ATTR_CONST_NOTHROW_LEAF_LIST (ECF_NOTHROW | ECF_LEAF | ECF_CONST) |
| 115 |
#define ATTR_PURE_NOTHROW_LEAF_LIST (ECF_NOTHROW | ECF_LEAF | ECF_PURE) |
| 116 |
#define ATTR_NOTHROW_LIST (ECF_NOTHROW) |
| 117 |
#define ATTR_CONST_NOTHROW_LIST (ECF_NOTHROW | ECF_CONST) |
| 118 |
#define ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_NOTHROW_LIST \ |
| 119 |
(ECF_NOTHROW | ECF_LEAF | ECF_MALLOC) |
| 120 |
#define ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_NOTHROW_LEAF_LIST \ |
| 121 |
(ECF_NOTHROW | ECF_LEAF) |
| 122 |
#define ATTR_COLD_NORETURN_NOTHROW_LEAF_LIST \ |
| 123 |
(ECF_COLD | ECF_NORETURN | \ |
| 124 |
ECF_NOTHROW | ECF_LEAF) |
| 125 |
#define ATTR_PURE_NOTHROW_NONNULL_LEAF (ECF_PURE|ECF_NOTHROW|ECF_LEAF) |
| 126 |
#define ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF (ECF_MALLOC|ECF_NOTHROW|ECF_LEAF) |
| 127 |
#define ATTR_TMPURE_NORETURN_NOTHROW_LEAF_COLD_LIST (ECF_TM_PURE|ECF_NORETURN|ECF_NOTHROW|ECF_LEAF|ECF_COLD) |
| 128 |
#define ATTR_NORETURN_NOTHROW_LIST (ECF_NORETURN|ECF_NOTHROW) |
| 129 |
#define ATTR_NOTHROW_NONNULL_LEAF (ECF_NOTHROW|ECF_LEAF) |
| 130 |
|
| 131 |
static void |
| 132 |
gfc_define_builtin (const char *name, tree type, enum built_in_function code, |
| 133 |
const char *library_name, int attr) |
| 134 |
{ |
| 135 |
tree decl; |
| 136 |
|
| 137 |
decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL, |
| 138 |
library_name, NULL_TREE); |
| 139 |
set_call_expr_flags (decl, attr); |
| 140 |
|
| 141 |
set_builtin_decl (code, decl, true); |
| 142 |
} |
| 143 |
|
| 144 |
static void |
| 145 |
create_our_type_nodes_init() |
| 146 |
{ |
| 147 |
for(int i=0; i<256; i++) |
| 148 |
{ |
| 149 |
char_nodes[i] = build_int_cst_type(CHAR, i); |
| 150 |
} |
| 151 |
|
| 152 |
// Create some useful constants to avoid cluttering up the code |
| 153 |
// build_int_cst_type() calls |
| 154 |
pvoid_type_node = build_pointer_type(void_type_node); |
| 155 |
integer_minusone_node = build_int_cst_type(INT, -1); |
| 156 |
integer_two_node = build_int_cst_type(INT, 2); |
| 157 |
integer_eight_node = build_int_cst_type(INT, 8); |
| 158 |
size_t_zero_node = build_int_cst_type(SIZE_T, 0); |
| 159 |
int128_zero_node = build_int_cst_type(INT128, 0); |
| 160 |
int128_five_node = build_int_cst_type(INT128, 5); |
| 161 |
int128_ten_node = build_int_cst_type(INT128, 10); |
| 162 |
char_ptr_type_node = build_pointer_type(CHAR); |
| 163 |
uchar_ptr_type_node = build_pointer_type(UCHAR); |
| 164 |
wchar_ptr_type_node = build_pointer_type(WCHAR); |
| 165 |
long_double_ten_node = build_real_from_int_cst( |
| 166 |
LONGDOUBLE, |
| 167 |
build_int_cst_type(INT,10)); |
| 168 |
sizeof_size_t = build_int_cst_type(SIZE_T, int_size_in_bytes(SIZE_T)); |
| 169 |
sizeof_pointer = build_int_cst_type(SIZE_T, int_size_in_bytes(VOID_P)); |
| 170 |
|
| 171 |
bool_true_node = build2(EQ_EXPR, |
| 172 |
integer_type_node, |
| 173 |
integer_one_node, |
| 174 |
integer_one_node); |
| 175 |
|
| 176 |
bool_false_node = build2( EQ_EXPR, |
| 177 |
integer_type_node, |
| 178 |
integer_one_node, |
| 179 |
integer_zero_node); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
static bool |
| 184 |
cobol_langhook_init (void) |
| 185 |
{ |
| 186 |
build_common_tree_nodes (true); |
| 187 |
|
| 188 |
create_our_type_nodes_init(); |
| 189 |
|
| 190 |
tree char_pointer_type_node = build_pointer_type (char_type_node); |
| 191 |
tree const_char_pointer_type_node |
| 192 |
= build_pointer_type (build_type_variant (char_pointer_type_node, 1, 0)); |
| 193 |
|
| 194 |
tree ftype; |
| 195 |
|
| 196 |
ftype = build_function_type_list (pvoid_type_node, |
| 197 |
size_type_node, |
| 198 |
NULL_TREE); |
| 199 |
gfc_define_builtin ("__builtin_malloc", |
| 200 |
ftype, |
| 201 |
BUILT_IN_MALLOC, |
| 202 |
"malloc", |
| 203 |
ATTR_NOTHROW_LEAF_MALLOC_LIST); |
| 204 |
|
| 205 |
ftype = build_function_type_list (pvoid_type_node, pvoid_type_node, |
| 206 |
size_type_node, NULL_TREE); |
| 207 |
gfc_define_builtin ("__builtin_realloc", ftype, BUILT_IN_REALLOC, |
| 208 |
"realloc", ATTR_NOTHROW_LEAF_LIST); |
| 209 |
|
| 210 |
ftype = build_function_type_list (void_type_node, |
| 211 |
pvoid_type_node, NULL_TREE); |
| 212 |
gfc_define_builtin ("__builtin_free", ftype, BUILT_IN_FREE, |
| 213 |
"free", ATTR_NOTHROW_LEAF_LIST); |
| 214 |
|
| 215 |
ftype = build_function_type_list (pvoid_type_node, |
| 216 |
const_ptr_type_node, |
| 217 |
integer_type_node, |
| 218 |
size_type_node, |
| 219 |
NULL_TREE); |
| 220 |
gfc_define_builtin ("__builtin_memchr", ftype, BUILT_IN_MEMCHR, |
| 221 |
"memchr", ATTR_PURE_NOTHROW_NONNULL_LEAF); |
| 222 |
|
| 223 |
|
| 224 |
ftype = build_function_type_list (size_type_node, |
| 225 |
const_char_pointer_type_node, |
| 226 |
NULL_TREE); |
| 227 |
gfc_define_builtin ("__builtin_strlen", ftype, BUILT_IN_STRLEN, |
| 228 |
"strlen", ATTR_PURE_NOTHROW_NONNULL_LEAF); |
| 229 |
|
| 230 |
|
| 231 |
ftype = build_function_type_list (char_pointer_type_node, |
| 232 |
const_char_pointer_type_node, |
| 233 |
NULL_TREE); |
| 234 |
gfc_define_builtin ("__builtin_strdup", ftype, BUILT_IN_STRDUP, |
| 235 |
"strdup", ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF); |
| 236 |
|
| 237 |
ftype = build_function_type_list (void_type_node, NULL_TREE); |
| 238 |
gfc_define_builtin ("__builtin_abort", ftype, BUILT_IN_ABORT, |
| 239 |
"abort", ATTR_TMPURE_NORETURN_NOTHROW_LEAF_COLD_LIST); |
| 240 |
|
| 241 |
ftype = build_function_type_list (void_type_node, |
| 242 |
integer_type_node, |
| 243 |
NULL_TREE); |
| 244 |
gfc_define_builtin ("__builtin_exit", ftype, BUILT_IN_EXIT, |
| 245 |
"exit", ATTR_TMPURE_NORETURN_NOTHROW_LEAF_COLD_LIST); |
| 246 |
|
| 247 |
ftype = build_function_type_list (integer_type_node, |
| 248 |
const_char_pointer_type_node, |
| 249 |
const_char_pointer_type_node, |
| 250 |
size_type_node, |
| 251 |
NULL_TREE); |
| 252 |
gfc_define_builtin ("__builtin_strncmp", ftype, BUILT_IN_STRNCMP, |
| 253 |
"strncmp", ATTR_PURE_NOTHROW_NONNULL_LEAF); |
| 254 |
|
| 255 |
ftype = build_function_type_list (integer_type_node, |
| 256 |
const_char_pointer_type_node, |
| 257 |
const_char_pointer_type_node, |
| 258 |
NULL_TREE); |
| 259 |
gfc_define_builtin ("__builtin_strcmp", ftype, BUILT_IN_STRCMP, |
| 260 |
"strcmp", ATTR_PURE_NOTHROW_NONNULL_LEAF); |
| 261 |
|
| 262 |
ftype = build_function_type_list (char_pointer_type_node, |
| 263 |
char_pointer_type_node, |
| 264 |
const_char_pointer_type_node, |
| 265 |
NULL_TREE); |
| 266 |
gfc_define_builtin ("__builtin_strcpy", ftype, BUILT_IN_STRCPY, |
| 267 |
"strcpy", ATTR_NOTHROW_NONNULL_LEAF); |
| 268 |
|
| 269 |
build_common_builtin_nodes (); |
| 270 |
|
| 271 |
// Make sure this is a supported configuration. |
| 272 |
if( !targetm.scalar_mode_supported_p (TImode) || !float128_type_node ) |
| 273 |
{ |
| 274 |
sorry ("COBOL requires a 64-bit configuration"); |
| 275 |
} |
| 276 |
|
| 277 |
return true; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
void cobol_set_debugging( bool flex, bool yacc, bool parser ); |
| 282 |
void cobol_set_indicator_column( int column ); |
| 283 |
void copybook_directory_add( const char gcob_copybook[] ); |
| 284 |
void copybook_extension_add( const char ext[] ); |
| 285 |
bool defined_cmd( const char arg[] ); |
| 286 |
void lexer_echo( bool tf ); |
| 287 |
|
| 288 |
static void |
| 289 |
cobol_langhook_init_options_struct (struct gcc_options *opts) { |
| 290 |
opts->x_yy_flex_debug = 0; |
| 291 |
opts->x_yy_debug = 0; |
| 292 |
opts->x_cobol_trace_debug = 0; |
| 293 |
|
| 294 |
cobol_set_debugging( false, false, false ); |
| 295 |
|
| 296 |
copybook_directory_add( getenv("GCOBOL_COPYBOOK") ); |
| 297 |
} |
| 298 |
|
| 299 |
static unsigned int |
| 300 |
cobol_option_lang_mask (void) { |
| 301 |
return CL_Cobol; |
| 302 |
} |
| 303 |
|
| 304 |
bool use_static_call( bool yn ); |
| 305 |
void add_cobol_exception( ec_type_t type, bool ); |
| 306 |
|
| 307 |
bool include_file_add(const char input[]); |
| 308 |
bool preprocess_filter_add( const char filter[] ); |
| 309 |
|
| 310 |
bool max_errors_exceeded( int nerr ) { |
| 311 |
return flag_max_errors != 0 && flag_max_errors <= nerr; |
| 312 |
} |
| 313 |
|
| 314 |
static void |
| 315 |
enable_exceptions( bool enable ) { |
| 316 |
for( char * name = xstrdup(cobol_exceptions); |
| 317 |
NULL != (name = strtok(name, ",")); name = NULL ) { |
| 318 |
ec_type_t type = ec_type_of(name); |
| 319 |
if( type == ec_none_e ) { |
| 320 |
yywarn("unrecognized exception '%s' was ignored", name); |
| 321 |
continue; |
| 322 |
} |
| 323 |
ec_disposition_t disposition = ec_type_disposition(type); |
| 324 |
if( disposition != ec_implemented(disposition) ) { |
| 325 |
cbl_unimplemented("exception '%s'", name); |
| 326 |
} |
| 327 |
add_cobol_exception(type, enable ); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
static bool |
| 332 |
cobol_langhook_handle_option (size_t scode, |
| 333 |
const char *arg ATTRIBUTE_UNUSED, |
| 334 |
HOST_WIDE_INT value, |
| 335 |
int kind ATTRIBUTE_UNUSED, |
| 336 |
location_t loc ATTRIBUTE_UNUSED, |
| 337 |
const struct |
| 338 |
cl_option_handlers *handlers ATTRIBUTE_UNUSED) |
| 339 |
{ |
| 340 |
// process_command (decoded_options_count, decoded_options); |
| 341 |
enum opt_code code = (enum opt_code) scode; |
| 342 |
|
| 343 |
switch(code) |
| 344 |
{ |
| 345 |
case OPT_D: |
| 346 |
defined_cmd(arg); |
| 347 |
return true; |
| 348 |
case OPT_E: |
| 349 |
lexer_echo(true); |
| 350 |
return true; |
| 351 |
|
| 352 |
case OPT_I: |
| 353 |
copybook_directory_add(arg); |
| 354 |
return true; |
| 355 |
case OPT_copyext: |
| 356 |
copybook_extension_add(cobol_copyext); |
| 357 |
return true; |
| 358 |
|
| 359 |
case OPT_M: |
| 360 |
cobol_set_pp_option('M'); |
| 361 |
return true; |
| 362 |
|
| 363 |
case OPT_fstatic_call: |
| 364 |
use_static_call( arg? true : false ); |
| 365 |
return true; |
| 366 |
|
| 367 |
case OPT_fdefaultbyte: |
| 368 |
wsclear(cobol_default_byte); |
| 369 |
return true; |
| 370 |
|
| 371 |
case OPT_fflex_debug: // cppcheck-suppress syntaxError // The need for this is a mystery |
| 372 |
yy_flex_debug = 1; |
| 373 |
cobol_set_debugging( true, yy_debug == 1, cobol_trace_debug == 1 ); |
| 374 |
return true; |
| 375 |
|
| 376 |
case OPT_fyacc_debug: |
| 377 |
yy_debug = 1; |
| 378 |
cobol_set_debugging(yy_flex_debug == 1, |
| 379 |
true, |
| 380 |
cobol_trace_debug == 1 ); |
| 381 |
return true; |
| 382 |
|
| 383 |
case OPT_ftrace_debug: |
| 384 |
cobol_set_debugging( yy_flex_debug == 1, yy_debug == 1, true ); |
| 385 |
return true; |
| 386 |
|
| 387 |
case OPT_fcobol_exceptions: { |
| 388 |
if( cobol_exceptions[0] == '=' ) cobol_exceptions++; |
| 389 |
enable_exceptions(value == 1); |
| 390 |
return true; |
| 391 |
} |
| 392 |
|
| 393 |
case OPT_ffixed_form: |
| 394 |
cobol_set_indicator_column(-7); |
| 395 |
return true; |
| 396 |
case OPT_ffree_form: |
| 397 |
cobol_set_indicator_column(0); |
| 398 |
return true; |
| 399 |
|
| 400 |
case OPT_findicator_column: |
| 401 |
cobol_set_indicator_column( indicator_column ); |
| 402 |
return true; |
| 403 |
|
| 404 |
case OPT_dialect: |
| 405 |
cobol_dialect_set(cbl_dialect_t(cobol_dialect)); |
| 406 |
return true; |
| 407 |
|
| 408 |
case OPT_fsyntax_only: |
| 409 |
mode_syntax_only(identification_div_e); |
| 410 |
break; |
| 411 |
|
| 412 |
case OPT_preprocess: |
| 413 |
if( ! preprocess_filter_add(arg) ) { |
| 414 |
cbl_errx( "could not execute preprocessor %s", arg); |
| 415 |
} |
| 416 |
return true; |
| 417 |
|
| 418 |
case OPT_include: |
| 419 |
if( ! include_file_add(arg) ) { |
| 420 |
cbl_errx( "could not include %s", arg); |
| 421 |
} |
| 422 |
return true; |
| 423 |
|
| 424 |
case OPT_main: |
| 425 |
// This isn't right. All OPT_main should be replaced |
| 426 |
error("We should never see a non-equal dash-main in cobol1.c"); |
| 427 |
exit(1); |
| 428 |
return true; |
| 429 |
|
| 430 |
case OPT_main_: |
| 431 |
register_main_switch(cobol_main_string); |
| 432 |
return true; |
| 433 |
|
| 434 |
case OPT_nomain: |
| 435 |
return true; |
| 436 |
|
| 437 |
case OPT_finternal_ebcdic: |
| 438 |
cobol_gcobol_feature_set(feature_internal_ebcdic_e); |
| 439 |
return true; |
| 440 |
|
| 441 |
default: |
| 442 |
break; |
| 443 |
} |
| 444 |
|
| 445 |
Cobol_handle_option_auto (&global_options, &global_options_set, |
| 446 |
scode, arg, value, |
| 447 |
cobol_option_lang_mask (), kind, |
| 448 |
loc, handlers, global_dc); |
| 449 |
|
| 450 |
return true; |
| 451 |
} |
| 452 |
|
| 453 |
void |
| 454 |
cobol_parse_files (int nfile, const char **files); |
| 455 |
|
| 456 |
static void |
| 457 |
cobol_langhook_parse_file (void) |
| 458 |
{ |
| 459 |
cobol_parse_files (num_in_fnames, in_fnames); |
| 460 |
} |
| 461 |
|
| 462 |
static tree |
| 463 |
cobol_langhook_type_for_mode (enum machine_mode mode, int unsignedp) |
| 464 |
{ |
| 465 |
if (mode == TYPE_MODE (float_type_node)) |
| 466 |
return float_type_node; |
| 467 |
|
| 468 |
if (mode == TYPE_MODE (double_type_node)) |
| 469 |
return double_type_node; |
| 470 |
|
| 471 |
if (mode == TYPE_MODE (float32_type_node)) |
| 472 |
return float32_type_node; |
| 473 |
|
| 474 |
if (mode == TYPE_MODE (float64_type_node)) |
| 475 |
return float64_type_node; |
| 476 |
|
| 477 |
if (mode == TYPE_MODE (float128_type_node)) |
| 478 |
return float128_type_node; |
| 479 |
|
| 480 |
if (mode == TYPE_MODE (intQI_type_node)) |
| 481 |
return unsignedp ? unsigned_intQI_type_node : intQI_type_node; |
| 482 |
if (mode == TYPE_MODE (intHI_type_node)) |
| 483 |
return unsignedp ? unsigned_intHI_type_node : intHI_type_node; |
| 484 |
if (mode == TYPE_MODE (intSI_type_node)) |
| 485 |
return unsignedp ? unsigned_intSI_type_node : intSI_type_node; |
| 486 |
if (mode == TYPE_MODE (intDI_type_node)) |
| 487 |
return unsignedp ? unsigned_intDI_type_node : intDI_type_node; |
| 488 |
if (mode == TYPE_MODE (intTI_type_node)) |
| 489 |
return unsignedp ? unsigned_intTI_type_node : intTI_type_node; |
| 490 |
|
| 491 |
if (mode == TYPE_MODE (integer_type_node)) |
| 492 |
return unsignedp ? unsigned_type_node : integer_type_node; |
| 493 |
|
| 494 |
if (mode == TYPE_MODE (long_integer_type_node)) |
| 495 |
return unsignedp ? long_unsigned_type_node : long_integer_type_node; |
| 496 |
|
| 497 |
if (mode == TYPE_MODE (long_long_integer_type_node)) |
| 498 |
return unsignedp ? long_long_unsigned_type_node |
| 499 |
: long_long_integer_type_node; |
| 500 |
|
| 501 |
if (COMPLEX_MODE_P (mode)) |
| 502 |
{ |
| 503 |
if (mode == TYPE_MODE (complex_float_type_node)) |
| 504 |
return complex_float_type_node; |
| 505 |
if (mode == TYPE_MODE (complex_double_type_node)) |
| 506 |
return complex_double_type_node; |
| 507 |
if (mode == TYPE_MODE (complex_long_double_type_node)) |
| 508 |
return complex_long_double_type_node; |
| 509 |
if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp) |
| 510 |
return complex_integer_type_node; |
| 511 |
} |
| 512 |
|
| 513 |
return NULL; |
| 514 |
} |
| 515 |
|
| 516 |
////static tree |
| 517 |
////cobol_langhook_type_for_size (unsigned int bits ATTRIBUTE_UNUSED, |
| 518 |
//// int unsignedp ATTRIBUTE_UNUSED) |
| 519 |
//// { |
| 520 |
//// gcc_unreachable (); |
| 521 |
//// return NULL; |
| 522 |
//// } |
| 523 |
|
| 524 |
/* Record a builtin function. We just ignore builtin functions. */ |
| 525 |
|
| 526 |
static tree |
| 527 |
cobol_langhook_builtin_function (tree decl) |
| 528 |
{ |
| 529 |
return decl; |
| 530 |
} |
| 531 |
|
| 532 |
static bool |
| 533 |
cobol_langhook_global_bindings_p (void) |
| 534 |
{ |
| 535 |
return false; |
| 536 |
} |
| 537 |
|
| 538 |
static tree |
| 539 |
cobol_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED) |
| 540 |
{ |
| 541 |
// This function is necessary, but is apparently never being called |
| 542 |
gcc_unreachable (); |
| 543 |
} |
| 544 |
|
| 545 |
static tree |
| 546 |
cobol_langhook_getdecls (void) |
| 547 |
{ |
| 548 |
return NULL; |
| 549 |
} |
| 550 |
|
| 551 |
char * |
| 552 |
cobol_name_mangler(const char *cobol_name_) |
| 553 |
{ |
| 554 |
// The caller should free the returned string. |
| 555 |
|
| 556 |
// This is a solution to the problem of hyphens and the fact that COBOL |
| 557 |
// names can start with digits. |
| 558 |
// |
| 559 |
// COBOL names can't start with underscore; GNU assembler names can. |
| 560 |
// Assembler names can't start with a digit 0-9; COBOL names can. |
| 561 |
// |
| 562 |
// We convert all COBOL names to lowercase, so uppercase characters aren't |
| 563 |
// seen. |
| 564 |
// |
| 565 |
// COBOL names can have hyphens; assembler names can't. |
| 566 |
// |
| 567 |
// So if a name starts with a digit, we prepend an underscore. |
| 568 |
// We convert the whole name to lowercase. |
| 569 |
// We replace hyphens with '$' |
| 570 |
// |
| 571 |
|
| 572 |
if( !cobol_name_ ) |
| 573 |
{ |
| 574 |
return nullptr; |
| 575 |
} |
| 576 |
|
| 577 |
// Allocate enough space for a prepended underscore and a final '\0' |
| 578 |
char *cobol_name = static_cast<char *>(xmalloc(strlen(cobol_name_)+2)); |
| 579 |
size_t n = 0; |
| 580 |
if( cobol_name_[0] >= '0' && cobol_name_[0] <= '9' ) |
| 581 |
{ |
| 582 |
// The name starts with 0-9, so we are going to lead it |
| 583 |
// with an underscore |
| 584 |
cobol_name[n++] = '_'; |
| 585 |
} |
| 586 |
for(size_t i=0; i<strlen(cobol_name_); i++) |
| 587 |
{ |
| 588 |
// Convert to lowercase, replacing '-' with '$' |
| 589 |
int ch = cobol_name_[i]; |
| 590 |
if( ch == '-' ) |
| 591 |
{ |
| 592 |
cobol_name[n++] = '$'; |
| 593 |
} |
| 594 |
else |
| 595 |
{ |
| 596 |
cobol_name[n++] = TOLOWER(ch); |
| 597 |
} |
| 598 |
} |
| 599 |
cobol_name[n++] = '\0'; |
| 600 |
|
| 601 |
return cobol_name; |
| 602 |
} |
| 603 |
|
| 604 |
cbl_call_convention_t parser_call_target_convention( tree func ); |
| 605 |
|
| 606 |
static |
| 607 |
void |
| 608 |
cobol_set_decl_assembler_name (tree decl) |
| 609 |
{ |
| 610 |
tree id; |
| 611 |
|
| 612 |
/* set_decl_assembler_name may be called on TYPE_DECL to record ODR |
| 613 |
name for C++ types. By default types have no ODR names. */ |
| 614 |
if (TREE_CODE (decl) == TYPE_DECL) |
| 615 |
{ |
| 616 |
return; |
| 617 |
} |
| 618 |
|
| 619 |
/* The language-independent code should never use the |
| 620 |
DECL_ASSEMBLER_NAME for lots of DECLs. Only FUNCTION_DECLs and |
| 621 |
VAR_DECLs for variables with static storage duration need a real |
| 622 |
DECL_ASSEMBLER_NAME. */ |
| 623 |
gcc_assert (TREE_CODE (decl) == FUNCTION_DECL |
| 624 |
|| (VAR_P (decl) && (TREE_STATIC (decl) |
| 625 |
|| DECL_EXTERNAL (decl) |
| 626 |
|| TREE_PUBLIC (decl)))); |
| 627 |
|
| 628 |
const char *name = IDENTIFIER_POINTER (DECL_NAME (decl)); |
| 629 |
char *mangled_name = cobol_name_mangler(name); |
| 630 |
|
| 631 |
// A verbatim CALL does not get mangled. |
| 632 |
if( cbl_call_verbatim_e == parser_call_target_convention(decl) ) |
| 633 |
{ |
| 634 |
strcpy(mangled_name, name); |
| 635 |
} |
| 636 |
|
| 637 |
id = get_identifier(mangled_name); |
| 638 |
free(mangled_name); |
| 639 |
|
| 640 |
SET_DECL_ASSEMBLER_NAME (decl, id); |
| 641 |
} |
| 642 |
|
| 643 |
/* Get a value for the SARIF v2.1.0 "artifact.sourceLanguage" property, |
| 644 |
based on the list in SARIF v2.1.0 Appendix J. */ |
| 645 |
|
| 646 |
const char * |
| 647 |
cobol_get_sarif_source_language(const char *) |
| 648 |
{ |
| 649 |
return "cobol"; |
| 650 |
} |
| 651 |
|
| 652 |
#undef LANG_HOOKS_BUILTIN_FUNCTION |
| 653 |
#undef LANG_HOOKS_GETDECLS |
| 654 |
#undef LANG_HOOKS_GLOBAL_BINDINGS_P |
| 655 |
#undef LANG_HOOKS_HANDLE_OPTION |
| 656 |
#undef LANG_HOOKS_INIT |
| 657 |
#undef LANG_HOOKS_INIT_OPTIONS_STRUCT |
| 658 |
#undef LANG_HOOKS_NAME |
| 659 |
#undef LANG_HOOKS_OPTION_LANG_MASK |
| 660 |
#undef LANG_HOOKS_PARSE_FILE |
| 661 |
#undef LANG_HOOKS_PUSHDECL |
| 662 |
#undef LANG_HOOKS_TYPE_FOR_MODE |
| 663 |
////#undef LANG_HOOKS_TYPE_FOR_SIZE |
| 664 |
#undef LANG_HOOKS_SET_DECL_ASSEMBLER_NAME |
| 665 |
#undef LANG_HOOKS_GET_SARIF_SOURCE_LANGUAGE |
| 666 |
|
| 667 |
// We use GCC in the name, not GNU, as others do, |
| 668 |
// because "GnuCOBOL" refers to a different GNU project. |
| 669 |
// https://www.gnu.org/software/software.html |
| 670 |
#define LANG_HOOKS_NAME "GCC COBOL" |
| 671 |
|
| 672 |
#define LANG_HOOKS_INIT cobol_langhook_init |
| 673 |
#define LANG_HOOKS_OPTION_LANG_MASK cobol_option_lang_mask |
| 674 |
|
| 675 |
#define LANG_HOOKS_INIT_OPTIONS_STRUCT cobol_langhook_init_options_struct |
| 676 |
#define LANG_HOOKS_HANDLE_OPTION cobol_langhook_handle_option |
| 677 |
|
| 678 |
#define LANG_HOOKS_BUILTIN_FUNCTION cobol_langhook_builtin_function |
| 679 |
#define LANG_HOOKS_GETDECLS cobol_langhook_getdecls |
| 680 |
#define LANG_HOOKS_GLOBAL_BINDINGS_P cobol_langhook_global_bindings_p |
| 681 |
#define LANG_HOOKS_PARSE_FILE cobol_langhook_parse_file |
| 682 |
#define LANG_HOOKS_PUSHDECL cobol_langhook_pushdecl |
| 683 |
|
| 684 |
#define LANG_HOOKS_TYPE_FOR_MODE cobol_langhook_type_for_mode |
| 685 |
////#define LANG_HOOKS_TYPE_FOR_SIZE cobol_langhook_type_for_size |
| 686 |
|
| 687 |
#define LANG_HOOKS_SET_DECL_ASSEMBLER_NAME cobol_set_decl_assembler_name |
| 688 |
|
| 689 |
#define LANG_HOOKS_GET_SARIF_SOURCE_LANGUAGE cobol_get_sarif_source_language |
| 690 |
|
| 691 |
struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER; |
| 692 |
|
| 693 |
#include "gt-cobol-cobol1.h" |
| 694 |
#include "gtype-cobol.h" |