1 /* go-lang.c -- Go frontend gcc interface.
2 Copyright (C) 2009-2021 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "target.h"
24 #include "tree.h"
25 #include "gimple-expr.h"
26 #include "diagnostic.h"
27 #include "opts.h"
28 #include "fold-const.h"
29 #include "gimplify.h"
30 #include "stor-layout.h"
31 #include "debug.h"
32 #include "convert.h"
33 #include "langhooks.h"
34 #include "langhooks-def.h"
35 #include "common/common-target.h"
36
37 #include <mpfr.h>
38
39 #include "go-c.h"
40 #include "go-gcc.h"
41
42 #ifndef TARGET_AIX
43 #define TARGET_AIX 0
44 #endif
45
46 /* Language-dependent contents of a type. */
47
48 struct GTY(()) lang_type
49 {
50 char dummy;
51 };
52
53 /* Language-dependent contents of a decl. */
54
55 struct GTY(()) lang_decl
56 {
57 char dummy;
58 };
59
60 /* Language-dependent contents of an identifier. This must include a
61 tree_identifier. */
62
63 struct GTY(()) lang_identifier
64 {
65 struct tree_identifier common;
66 };
67
68 /* The resulting tree type. */
69
70 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
71 chain_next ("CODE_CONTAINS_STRUCT (TREE_CODE (&%h.generic), TS_COMMON) ? ((union lang_tree_node *) TREE_CHAIN (&%h.generic)) : NULL")))
72 lang_tree_node
73 {
74 union tree_node GTY((tag ("0"),
75 desc ("tree_node_structure (&%h)"))) generic;
76 struct lang_identifier GTY((tag ("1"))) identifier;
77 };
78
79 /* We don't use language_function. */
80
81 struct GTY(()) language_function
82 {
83 int dummy;
84 };
85
86 /* Option information we need to pass to go_create_gogo. */
87
88 static const char *go_pkgpath = NULL;
89 static const char *go_prefix = NULL;
90 static const char *go_relative_import_path = NULL;
91 static const char *go_c_header = NULL;
92 static const char *go_embedcfg = NULL;
93
94 /* Language hooks. */
95
96 static bool
go_langhook_init(void)97 go_langhook_init (void)
98 {
99 build_common_tree_nodes (false);
100
101 /* I don't know why this has to be done explicitly. */
102 void_list_node = build_tree_list (NULL_TREE, void_type_node);
103
104 /* We must create the gogo IR after calling build_common_tree_nodes
105 (because Gogo::define_builtin_function_trees refers indirectly
106 to, e.g., unsigned_char_type_node) but before calling
107 build_common_builtin_nodes (because it calls, indirectly,
108 go_type_for_size). */
109 struct go_create_gogo_args args;
110 args.int_type_size = INT_TYPE_SIZE;
111 args.pointer_size = POINTER_SIZE;
112 args.pkgpath = go_pkgpath;
113 args.prefix = go_prefix;
114 args.relative_import_path = go_relative_import_path;
115 args.c_header = go_c_header;
116 args.embedcfg = go_embedcfg;
117 args.check_divide_by_zero = go_check_divide_zero;
118 args.check_divide_overflow = go_check_divide_overflow;
119 args.compiling_runtime = go_compiling_runtime;
120 args.debug_escape_level = go_debug_escape_level;
121 args.debug_escape_hash = go_debug_escape_hash;
122 args.nil_check_size_threshold = TARGET_AIX ? -1 : 4096;
123 args.debug_optimization = go_debug_optimization;
124 args.need_eqtype = TARGET_AIX ? true : false;
125 args.linemap = go_get_linemap();
126 args.backend = go_get_backend();
127 go_create_gogo (&args);
128
129 build_common_builtin_nodes ();
130
131 /* The default precision for floating point numbers. This is used
132 for floating point constants with abstract type. This may
133 eventually be controllable by a command line option. */
134 mpfr_set_default_prec (256);
135
136 /* If necessary, override GCC's choice of minimum and maximum
137 exponents. This should only affect GCC middle-end
138 compilation-time, not correctness. */
139 mpfr_exp_t exp = mpfr_get_emax ();
140 if (exp < (1 << 16) - 1)
141 mpfr_set_emax ((1 << 16) - 1);
142 exp = mpfr_get_emin ();
143 if (exp > - ((1 << 16) - 1))
144 mpfr_set_emin (- ((1 << 16) - 1));
145
146 /* Go uses exceptions. */
147 using_eh_for_cleanups ();
148
149 return true;
150 }
151
152 /* The option mask. */
153
154 static unsigned int
go_langhook_option_lang_mask(void)155 go_langhook_option_lang_mask (void)
156 {
157 return CL_Go;
158 }
159
160 /* Initialize the options structure. */
161
162 static void
go_langhook_init_options_struct(struct gcc_options * opts)163 go_langhook_init_options_struct (struct gcc_options *opts)
164 {
165 /* Go says that signed overflow is precisely defined. */
166 opts->x_flag_wrapv = 1;
167
168 /* We default to using strict aliasing, since Go pointers are safe.
169 This is turned off for code that imports the "unsafe" package,
170 because using unsafe.pointer violates C style aliasing
171 requirements. */
172 opts->x_flag_strict_aliasing = 1;
173
174 /* Default to avoiding range issues for complex multiply and
175 divide. */
176 opts->x_flag_complex_method = 2;
177
178 /* The builtin math functions should not set errno. */
179 opts->x_flag_errno_math = 0;
180 opts->frontend_set_flag_errno_math = true;
181
182 /* Exceptions are used to handle recovering from panics. */
183 opts->x_flag_exceptions = 1;
184 opts->x_flag_non_call_exceptions = 1;
185
186 /* We need to keep pointers live for the garbage collector. */
187 opts->x_flag_keep_gc_roots_live = 1;
188
189 /* Go programs expect runtime.Callers to work, and that uses
190 libbacktrace that uses debug info. Set the debug info level to 1
191 by default. In post_options we will set the debug type if the
192 debug info level was not set back to 0 on the command line. */
193 opts->x_debug_info_level = DINFO_LEVEL_TERSE;
194 }
195
196 /* Infrastructure for a vector of char * pointers. */
197
198 typedef const char *go_char_p;
199
200 /* The list of directories to search after all the Go specific
201 directories have been searched. */
202
203 static vec<go_char_p> go_search_dirs;
204
205 /* Handle Go specific options. Return 0 if we didn't do anything. */
206
207 static bool
go_langhook_handle_option(size_t scode,const char * arg,HOST_WIDE_INT value,int kind ATTRIBUTE_UNUSED,location_t loc ATTRIBUTE_UNUSED,const struct cl_option_handlers * handlers ATTRIBUTE_UNUSED)208 go_langhook_handle_option (
209 size_t scode,
210 const char *arg,
211 HOST_WIDE_INT value,
212 int kind ATTRIBUTE_UNUSED,
213 location_t loc ATTRIBUTE_UNUSED,
214 const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED)
215 {
216 enum opt_code code = (enum opt_code) scode;
217 bool ret = true;
218
219 switch (code)
220 {
221 case OPT_I:
222 go_add_search_path (arg);
223 break;
224
225 case OPT_L:
226 /* A -L option is assumed to come from the compiler driver.
227 This is a system directory. We search the following
228 directories, if they exist, before this one:
229 dir/go/VERSION
230 dir/go/VERSION/MACHINE
231 This is like include/c++. */
232 {
233 static const char dir_separator_str[] = { DIR_SEPARATOR, 0 };
234 size_t len;
235 char *p;
236 struct stat st;
237
238 len = strlen (arg);
239 p = XALLOCAVEC (char,
240 (len + sizeof "go" + sizeof DEFAULT_TARGET_VERSION
241 + sizeof DEFAULT_TARGET_MACHINE + 3));
242 strcpy (p, arg);
243 if (len > 0 && !IS_DIR_SEPARATOR (p[len - 1]))
244 strcat (p, dir_separator_str);
245 strcat (p, "go");
246 strcat (p, dir_separator_str);
247 strcat (p, DEFAULT_TARGET_VERSION);
248 if (stat (p, &st) == 0 && S_ISDIR (st.st_mode))
249 {
250 go_add_search_path (p);
251 strcat (p, dir_separator_str);
252 strcat (p, DEFAULT_TARGET_MACHINE);
253 if (stat (p, &st) == 0 && S_ISDIR (st.st_mode))
254 go_add_search_path (p);
255 }
256
257 /* Search ARG too, but only after we've searched to Go
258 specific directories for all -L arguments. */
259 go_search_dirs.safe_push (arg);
260 }
261 break;
262
263 case OPT_fgo_dump_:
264 ret = go_enable_dump (arg) ? true : false;
265 break;
266
267 case OPT_fgo_optimize_:
268 ret = go_enable_optimize (arg, value) ? true : false;
269 break;
270
271 case OPT_fgo_pkgpath_:
272 go_pkgpath = arg;
273 break;
274
275 case OPT_fgo_prefix_:
276 go_prefix = arg;
277 break;
278
279 case OPT_fgo_relative_import_path_:
280 go_relative_import_path = arg;
281 break;
282
283 case OPT_fgo_c_header_:
284 go_c_header = arg;
285 break;
286
287 case OPT_fgo_embedcfg_:
288 go_embedcfg = arg;
289 break;
290
291 default:
292 /* Just return 1 to indicate that the option is valid. */
293 break;
294 }
295
296 return ret;
297 }
298
299 /* Run after parsing options. */
300
301 static bool
go_langhook_post_options(const char ** pfilename ATTRIBUTE_UNUSED)302 go_langhook_post_options (const char **pfilename ATTRIBUTE_UNUSED)
303 {
304 unsigned int ix;
305 const char *dir;
306
307 gcc_assert (num_in_fnames > 0);
308
309 FOR_EACH_VEC_ELT (go_search_dirs, ix, dir)
310 go_add_search_path (dir);
311 go_search_dirs.release ();
312
313 if (flag_excess_precision == EXCESS_PRECISION_DEFAULT)
314 flag_excess_precision = EXCESS_PRECISION_STANDARD;
315
316 /* Tail call optimizations can confuse uses of runtime.Callers. */
317 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
318 flag_optimize_sibling_calls, 0);
319
320 /* Partial inlining can confuses uses of runtime.Callers.
321 See https://gcc.gnu.org/PR91663. */
322 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
323 flag_partial_inlining, 0);
324
325 /* Go programs expect runtime.Callers to give the right answers,
326 which means that we can't combine functions even if they look the
327 same. */
328 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
329 flag_ipa_icf_functions, 0);
330
331 /* If the debug info level is still 1, as set in init_options, make
332 sure that some debugging type is selected. */
333 if (global_options.x_debug_info_level == DINFO_LEVEL_TERSE
334 && global_options.x_write_symbols == NO_DEBUG)
335 global_options.x_write_symbols = PREFERRED_DEBUGGING_TYPE;
336
337 /* We turn on stack splitting if we can. */
338 if (targetm_common.supports_split_stack (false, &global_options))
339 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
340 flag_split_stack, 1);
341
342 /* If stack splitting is turned on, and the user did not explicitly
343 request function partitioning, turn off partitioning, as it
344 confuses the linker when trying to handle partitioned split-stack
345 code that calls a non-split-stack function. */
346 if (global_options.x_flag_split_stack
347 && global_options.x_flag_reorder_blocks_and_partition)
348 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
349 flag_reorder_blocks_and_partition, 0);
350
351 /* Returning false means that the backend should be used. */
352 return false;
353 }
354
355 static void
go_langhook_parse_file(void)356 go_langhook_parse_file (void)
357 {
358 go_parse_input_files (in_fnames, num_in_fnames, flag_syntax_only,
359 go_require_return_statement);
360
361 /* Final processing of globals and early debug info generation. */
362 go_write_globals ();
363 }
364
365 static tree
go_langhook_type_for_size(unsigned int bits,int unsignedp)366 go_langhook_type_for_size (unsigned int bits, int unsignedp)
367 {
368 tree type;
369 if (unsignedp)
370 {
371 if (bits == INT_TYPE_SIZE)
372 type = unsigned_type_node;
373 else if (bits == CHAR_TYPE_SIZE)
374 type = unsigned_char_type_node;
375 else if (bits == SHORT_TYPE_SIZE)
376 type = short_unsigned_type_node;
377 else if (bits == LONG_TYPE_SIZE)
378 type = long_unsigned_type_node;
379 else if (bits == LONG_LONG_TYPE_SIZE)
380 type = long_long_unsigned_type_node;
381 else
382 type = make_unsigned_type(bits);
383 }
384 else
385 {
386 if (bits == INT_TYPE_SIZE)
387 type = integer_type_node;
388 else if (bits == CHAR_TYPE_SIZE)
389 type = signed_char_type_node;
390 else if (bits == SHORT_TYPE_SIZE)
391 type = short_integer_type_node;
392 else if (bits == LONG_TYPE_SIZE)
393 type = long_integer_type_node;
394 else if (bits == LONG_LONG_TYPE_SIZE)
395 type = long_long_integer_type_node;
396 else
397 type = make_signed_type(bits);
398 }
399 return type;
400 }
401
402 static tree
go_langhook_type_for_mode(machine_mode mode,int unsignedp)403 go_langhook_type_for_mode (machine_mode mode, int unsignedp)
404 {
405 tree type;
406 /* Go has no vector types. Build them here. FIXME: It does not
407 make sense for the middle-end to ask the frontend for a type
408 which the frontend does not support. However, at least for now
409 it is required. See PR 46805. */
410 if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL
411 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
412 {
413 unsigned int elem_bits = vector_element_size (GET_MODE_BITSIZE (mode),
414 GET_MODE_NUNITS (mode));
415 tree bool_type = build_nonstandard_boolean_type (elem_bits);
416 return build_vector_type_for_mode (bool_type, mode);
417 }
418 else if (VECTOR_MODE_P (mode)
419 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
420 {
421 tree inner;
422
423 inner = go_langhook_type_for_mode (GET_MODE_INNER (mode), unsignedp);
424 if (inner != NULL_TREE)
425 return build_vector_type_for_mode (inner, mode);
426 return NULL_TREE;
427 }
428
429 scalar_int_mode imode;
430 scalar_float_mode fmode;
431 complex_mode cmode;
432 if (is_int_mode (mode, &imode))
433 return go_langhook_type_for_size (GET_MODE_BITSIZE (imode), unsignedp);
434 else if (is_float_mode (mode, &fmode))
435 {
436 switch (GET_MODE_BITSIZE (fmode))
437 {
438 case 32:
439 return float_type_node;
440 case 64:
441 return double_type_node;
442 default:
443 // We have to check for long double in order to support
444 // i386 excess precision.
445 if (fmode == TYPE_MODE(long_double_type_node))
446 return long_double_type_node;
447 }
448 }
449 else if (is_complex_float_mode (mode, &cmode))
450 {
451 switch (GET_MODE_BITSIZE (cmode))
452 {
453 case 64:
454 return complex_float_type_node;
455 case 128:
456 return complex_double_type_node;
457 default:
458 // We have to check for long double in order to support
459 // i386 excess precision.
460 if (cmode == TYPE_MODE(complex_long_double_type_node))
461 return complex_long_double_type_node;
462 }
463 }
464
465 #if HOST_BITS_PER_WIDE_INT >= 64
466 /* The middle-end and some backends rely on TImode being supported
467 for 64-bit HWI. */
468 if (mode == TImode)
469 {
470 type = build_nonstandard_integer_type (GET_MODE_BITSIZE (TImode),
471 unsignedp);
472 if (type && TYPE_MODE (type) == TImode)
473 return type;
474 }
475 #endif
476 return NULL_TREE;
477 }
478
479 /* Record a builtin function. We just ignore builtin functions. */
480
481 static tree
go_langhook_builtin_function(tree decl)482 go_langhook_builtin_function (tree decl)
483 {
484 return decl;
485 }
486
487 /* Return true if we are in the global binding level. */
488
489 static bool
go_langhook_global_bindings_p(void)490 go_langhook_global_bindings_p (void)
491 {
492 return current_function_decl == NULL_TREE;
493 }
494
495 /* Push a declaration into the current binding level. We can't
496 usefully implement this since we don't want to convert from tree
497 back to one of our internal data structures. I think the only way
498 this is used is to record a decl which is to be returned by
499 getdecls, and we could implement it for that purpose if
500 necessary. */
501
502 static tree
go_langhook_pushdecl(tree decl ATTRIBUTE_UNUSED)503 go_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED)
504 {
505 gcc_unreachable ();
506 }
507
508 /* This hook is used to get the current list of declarations as trees.
509 We don't support that; instead we use the write_globals hook. This
510 can't simply crash because it is called by -gstabs. */
511
512 static tree
go_langhook_getdecls(void)513 go_langhook_getdecls (void)
514 {
515 return NULL;
516 }
517
518 /* Go specific gimplification. We need to gimplify
519 CALL_EXPR_STATIC_CHAIN, because the gimplifier doesn't handle
520 it. */
521
522 static int
go_langhook_gimplify_expr(tree * expr_p,gimple_seq * pre_p,gimple_seq * post_p)523 go_langhook_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
524 {
525 if (TREE_CODE (*expr_p) == CALL_EXPR
526 && CALL_EXPR_STATIC_CHAIN (*expr_p) != NULL_TREE)
527 gimplify_expr (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p, post_p,
528 is_gimple_val, fb_rvalue);
529 return GS_UNHANDLED;
530 }
531
532 /* Return a decl for the exception personality function. The function
533 itself is implemented in libgo/runtime/go-unwind.c. */
534
535 static tree
go_langhook_eh_personality(void)536 go_langhook_eh_personality (void)
537 {
538 static tree personality_decl;
539 if (personality_decl == NULL_TREE)
540 {
541 personality_decl = build_personality_function ("gccgo");
542 go_preserve_from_gc (personality_decl);
543 }
544 return personality_decl;
545 }
546
547 /* Functions called directly by the generic backend. */
548
549 tree
convert(tree type,tree expr)550 convert (tree type, tree expr)
551 {
552 if (type == error_mark_node
553 || expr == error_mark_node
554 || TREE_TYPE (expr) == error_mark_node)
555 return error_mark_node;
556
557 if (type == TREE_TYPE (expr))
558 return expr;
559
560 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr)))
561 return fold_convert (type, expr);
562
563 switch (TREE_CODE (type))
564 {
565 case VOID_TYPE:
566 case BOOLEAN_TYPE:
567 return fold_convert (type, expr);
568 case INTEGER_TYPE:
569 return fold (convert_to_integer (type, expr));
570 case POINTER_TYPE:
571 return fold (convert_to_pointer (type, expr));
572 case REAL_TYPE:
573 return fold (convert_to_real (type, expr));
574 case COMPLEX_TYPE:
575 return fold (convert_to_complex (type, expr));
576 default:
577 break;
578 }
579
580 gcc_unreachable ();
581 }
582
583 /* FIXME: This is a hack to preserve trees that we create from the
584 garbage collector. */
585
586 static GTY(()) tree go_gc_root;
587
588 void
go_preserve_from_gc(tree t)589 go_preserve_from_gc (tree t)
590 {
591 go_gc_root = tree_cons (NULL_TREE, t, go_gc_root);
592 }
593
594 /* Convert an identifier for use in an error message. */
595
596 const char *
go_localize_identifier(const char * ident)597 go_localize_identifier (const char *ident)
598 {
599 return identifier_to_locale (ident);
600 }
601
602 #undef LANG_HOOKS_NAME
603 #undef LANG_HOOKS_INIT
604 #undef LANG_HOOKS_OPTION_LANG_MASK
605 #undef LANG_HOOKS_INIT_OPTIONS_STRUCT
606 #undef LANG_HOOKS_HANDLE_OPTION
607 #undef LANG_HOOKS_POST_OPTIONS
608 #undef LANG_HOOKS_PARSE_FILE
609 #undef LANG_HOOKS_TYPE_FOR_MODE
610 #undef LANG_HOOKS_TYPE_FOR_SIZE
611 #undef LANG_HOOKS_BUILTIN_FUNCTION
612 #undef LANG_HOOKS_GLOBAL_BINDINGS_P
613 #undef LANG_HOOKS_PUSHDECL
614 #undef LANG_HOOKS_GETDECLS
615 #undef LANG_HOOKS_GIMPLIFY_EXPR
616 #undef LANG_HOOKS_EH_PERSONALITY
617
618 #define LANG_HOOKS_NAME "GNU Go"
619 #define LANG_HOOKS_INIT go_langhook_init
620 #define LANG_HOOKS_OPTION_LANG_MASK go_langhook_option_lang_mask
621 #define LANG_HOOKS_INIT_OPTIONS_STRUCT go_langhook_init_options_struct
622 #define LANG_HOOKS_HANDLE_OPTION go_langhook_handle_option
623 #define LANG_HOOKS_POST_OPTIONS go_langhook_post_options
624 #define LANG_HOOKS_PARSE_FILE go_langhook_parse_file
625 #define LANG_HOOKS_TYPE_FOR_MODE go_langhook_type_for_mode
626 #define LANG_HOOKS_TYPE_FOR_SIZE go_langhook_type_for_size
627 #define LANG_HOOKS_BUILTIN_FUNCTION go_langhook_builtin_function
628 #define LANG_HOOKS_GLOBAL_BINDINGS_P go_langhook_global_bindings_p
629 #define LANG_HOOKS_PUSHDECL go_langhook_pushdecl
630 #define LANG_HOOKS_GETDECLS go_langhook_getdecls
631 #define LANG_HOOKS_GIMPLIFY_EXPR go_langhook_gimplify_expr
632 #define LANG_HOOKS_EH_PERSONALITY go_langhook_eh_personality
633
634 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
635
636 #include "gt-go-go-lang.h"
637 #include "gtype-go.h"
638