compiler: use package path with embedded builtin type
[official-gcc.git] / gcc / go / go-lang.cc
blob84cd623bbf88811f10e959007854bf94852ece2a
1 /* go-lang.cc -- Go frontend gcc interface.
2 Copyright (C) 2009-2022 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
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"
37 #include <mpfr.h>
39 #include "go-c.h"
40 #include "go-gcc.h"
42 #ifndef TARGET_AIX
43 #define TARGET_AIX 0
44 #endif
46 /* Language-dependent contents of a type. */
48 struct GTY(()) lang_type
50 char dummy;
53 /* Language-dependent contents of a decl. */
55 struct GTY(()) lang_decl
57 char dummy;
60 /* Language-dependent contents of an identifier. This must include a
61 tree_identifier. */
63 struct GTY(()) lang_identifier
65 struct tree_identifier common;
68 /* The resulting tree type. */
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
74 union tree_node GTY((tag ("0"),
75 desc ("tree_node_structure (&%h)"))) generic;
76 struct lang_identifier GTY((tag ("1"))) identifier;
79 /* We don't use language_function. */
81 struct GTY(()) language_function
83 int dummy;
86 /* Option information we need to pass to go_create_gogo. */
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;
94 /* Language hooks. */
96 static bool
97 go_langhook_init (void)
99 build_common_tree_nodes (false);
101 /* I don't know why this has to be done explicitly. */
102 void_list_node = build_tree_list (NULL_TREE, void_type_node);
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);
129 build_common_builtin_nodes ();
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);
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));
146 /* Go uses exceptions. */
147 using_eh_for_cleanups ();
149 return true;
152 /* The option mask. */
154 static unsigned int
155 go_langhook_option_lang_mask (void)
157 return CL_Go;
160 /* Initialize the options structure. */
162 static void
163 go_langhook_init_options_struct (struct gcc_options *opts)
165 /* Go says that signed overflow is precisely defined. */
166 opts->x_flag_wrapv = 1;
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;
174 /* Default to avoiding range issues for complex multiply and
175 divide. */
176 opts->x_flag_complex_method = 2;
177 opts->x_flag_default_complex_method = opts->x_flag_complex_method;
179 /* The builtin math functions should not set errno. */
180 opts->x_flag_errno_math = 0;
181 opts->frontend_set_flag_errno_math = true;
183 /* Exceptions are used to handle recovering from panics. */
184 opts->x_flag_exceptions = 1;
185 opts->x_flag_non_call_exceptions = 1;
187 /* We need to keep pointers live for the garbage collector. */
188 opts->x_flag_keep_gc_roots_live = 1;
190 /* Go programs expect runtime.Callers to work, and that uses
191 libbacktrace that uses debug info. Set the debug info level to 1
192 by default. In post_options we will set the debug type if the
193 debug info level was not set back to 0 on the command line. */
194 opts->x_debug_info_level = DINFO_LEVEL_TERSE;
197 /* Infrastructure for a vector of char * pointers. */
199 typedef const char *go_char_p;
201 /* The list of directories to search after all the Go specific
202 directories have been searched. */
204 static vec<go_char_p> go_search_dirs;
206 /* Handle Go specific options. Return 0 if we didn't do anything. */
208 static bool
209 go_langhook_handle_option (
210 size_t scode,
211 const char *arg,
212 HOST_WIDE_INT value,
213 int kind ATTRIBUTE_UNUSED,
214 location_t loc ATTRIBUTE_UNUSED,
215 const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED)
217 enum opt_code code = (enum opt_code) scode;
218 bool ret = true;
220 switch (code)
222 case OPT_I:
223 go_add_search_path (arg);
224 break;
226 case OPT_L:
227 /* A -L option is assumed to come from the compiler driver.
228 This is a system directory. We search the following
229 directories, if they exist, before this one:
230 dir/go/VERSION
231 dir/go/VERSION/MACHINE
232 This is like include/c++. */
234 static const char dir_separator_str[] = { DIR_SEPARATOR, 0 };
235 size_t len;
236 char *p;
237 struct stat st;
239 len = strlen (arg);
240 p = XALLOCAVEC (char,
241 (len + sizeof "go" + sizeof DEFAULT_TARGET_VERSION
242 + sizeof DEFAULT_TARGET_MACHINE + 3));
243 strcpy (p, arg);
244 if (len > 0 && !IS_DIR_SEPARATOR (p[len - 1]))
245 strcat (p, dir_separator_str);
246 strcat (p, "go");
247 strcat (p, dir_separator_str);
248 strcat (p, DEFAULT_TARGET_VERSION);
249 if (stat (p, &st) == 0 && S_ISDIR (st.st_mode))
251 go_add_search_path (p);
252 strcat (p, dir_separator_str);
253 strcat (p, DEFAULT_TARGET_MACHINE);
254 if (stat (p, &st) == 0 && S_ISDIR (st.st_mode))
255 go_add_search_path (p);
258 /* Search ARG too, but only after we've searched to Go
259 specific directories for all -L arguments. */
260 go_search_dirs.safe_push (arg);
262 break;
264 case OPT_fgo_dump_:
265 ret = go_enable_dump (arg) ? true : false;
266 break;
268 case OPT_fgo_optimize_:
269 ret = go_enable_optimize (arg, value) ? true : false;
270 break;
272 case OPT_fgo_pkgpath_:
273 go_pkgpath = arg;
274 break;
276 case OPT_fgo_prefix_:
277 go_prefix = arg;
278 break;
280 case OPT_fgo_relative_import_path_:
281 go_relative_import_path = arg;
282 break;
284 case OPT_fgo_c_header_:
285 go_c_header = arg;
286 break;
288 case OPT_fgo_embedcfg_:
289 go_embedcfg = arg;
290 break;
292 default:
293 /* Just return 1 to indicate that the option is valid. */
294 break;
297 return ret;
300 /* Run after parsing options. */
302 static bool
303 go_langhook_post_options (const char **pfilename ATTRIBUTE_UNUSED)
305 unsigned int ix;
306 const char *dir;
308 gcc_assert (num_in_fnames > 0);
310 FOR_EACH_VEC_ELT (go_search_dirs, ix, dir)
311 go_add_search_path (dir);
312 go_search_dirs.release ();
314 if (flag_excess_precision == EXCESS_PRECISION_DEFAULT)
315 flag_excess_precision = EXCESS_PRECISION_STANDARD;
317 /* Tail call optimizations can confuse uses of runtime.Callers. */
318 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
319 flag_optimize_sibling_calls, 0);
321 /* Partial inlining can confuses uses of runtime.Callers.
322 See https://gcc.gnu.org/PR91663. */
323 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
324 flag_partial_inlining, 0);
326 /* Go programs expect runtime.Callers to give the right answers,
327 which means that we can't combine functions even if they look the
328 same. */
329 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
330 flag_ipa_icf_functions, 0);
332 /* If the debug info level is still 1, as set in init_options, make
333 sure that some debugging type is selected. */
334 if (global_options.x_debug_info_level == DINFO_LEVEL_TERSE
335 && global_options.x_write_symbols == NO_DEBUG)
336 global_options.x_write_symbols = PREFERRED_DEBUGGING_TYPE;
338 /* We turn on stack splitting if we can. */
339 if (targetm_common.supports_split_stack (false, &global_options))
340 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
341 flag_split_stack, 1);
343 /* If stack splitting is turned on, and the user did not explicitly
344 request function partitioning, turn off partitioning, as it
345 confuses the linker when trying to handle partitioned split-stack
346 code that calls a non-split-stack function. */
347 if (global_options.x_flag_split_stack
348 && global_options.x_flag_reorder_blocks_and_partition)
349 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
350 flag_reorder_blocks_and_partition, 0);
352 /* Returning false means that the backend should be used. */
353 return false;
356 static void
357 go_langhook_parse_file (void)
359 go_parse_input_files (in_fnames, num_in_fnames, flag_syntax_only,
360 go_require_return_statement);
362 /* Final processing of globals and early debug info generation. */
363 go_write_globals ();
366 static tree
367 go_langhook_type_for_size (unsigned int bits, int unsignedp)
369 tree type;
370 if (unsignedp)
372 if (bits == INT_TYPE_SIZE)
373 type = unsigned_type_node;
374 else if (bits == CHAR_TYPE_SIZE)
375 type = unsigned_char_type_node;
376 else if (bits == SHORT_TYPE_SIZE)
377 type = short_unsigned_type_node;
378 else if (bits == LONG_TYPE_SIZE)
379 type = long_unsigned_type_node;
380 else if (bits == LONG_LONG_TYPE_SIZE)
381 type = long_long_unsigned_type_node;
382 else
383 type = make_unsigned_type(bits);
385 else
387 if (bits == INT_TYPE_SIZE)
388 type = integer_type_node;
389 else if (bits == CHAR_TYPE_SIZE)
390 type = signed_char_type_node;
391 else if (bits == SHORT_TYPE_SIZE)
392 type = short_integer_type_node;
393 else if (bits == LONG_TYPE_SIZE)
394 type = long_integer_type_node;
395 else if (bits == LONG_LONG_TYPE_SIZE)
396 type = long_long_integer_type_node;
397 else
398 type = make_signed_type(bits);
400 return type;
403 static tree
404 go_langhook_type_for_mode (machine_mode mode, int unsignedp)
406 tree type;
407 /* Go has no vector types. Build them here. FIXME: It does not
408 make sense for the middle-end to ask the frontend for a type
409 which the frontend does not support. However, at least for now
410 it is required. See PR 46805. */
411 if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL
412 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
414 unsigned int elem_bits = vector_element_size (GET_MODE_BITSIZE (mode),
415 GET_MODE_NUNITS (mode));
416 tree bool_type = build_nonstandard_boolean_type (elem_bits);
417 return build_vector_type_for_mode (bool_type, mode);
419 else if (VECTOR_MODE_P (mode)
420 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
422 tree inner;
424 inner = go_langhook_type_for_mode (GET_MODE_INNER (mode), unsignedp);
425 if (inner != NULL_TREE)
426 return build_vector_type_for_mode (inner, mode);
427 return NULL_TREE;
430 scalar_int_mode imode;
431 scalar_float_mode fmode;
432 complex_mode cmode;
433 if (is_int_mode (mode, &imode))
434 return go_langhook_type_for_size (GET_MODE_BITSIZE (imode), unsignedp);
435 else if (is_float_mode (mode, &fmode))
437 switch (GET_MODE_BITSIZE (fmode))
439 case 32:
440 return float_type_node;
441 case 64:
442 return double_type_node;
443 default:
444 // We have to check for long double in order to support
445 // i386 excess precision.
446 if (fmode == TYPE_MODE(long_double_type_node))
447 return long_double_type_node;
450 else if (is_complex_float_mode (mode, &cmode))
452 switch (GET_MODE_BITSIZE (cmode))
454 case 64:
455 return complex_float_type_node;
456 case 128:
457 return complex_double_type_node;
458 default:
459 // We have to check for long double in order to support
460 // i386 excess precision.
461 if (cmode == TYPE_MODE(complex_long_double_type_node))
462 return complex_long_double_type_node;
466 #if HOST_BITS_PER_WIDE_INT >= 64
467 /* The middle-end and some backends rely on TImode being supported
468 for 64-bit HWI. */
469 if (mode == TImode)
471 type = build_nonstandard_integer_type (GET_MODE_BITSIZE (TImode),
472 unsignedp);
473 if (type && TYPE_MODE (type) == TImode)
474 return type;
476 #endif
477 return NULL_TREE;
480 /* Record a builtin function. We just ignore builtin functions. */
482 static tree
483 go_langhook_builtin_function (tree decl)
485 return decl;
488 /* Return true if we are in the global binding level. */
490 static bool
491 go_langhook_global_bindings_p (void)
493 return current_function_decl == NULL_TREE;
496 /* Push a declaration into the current binding level. We can't
497 usefully implement this since we don't want to convert from tree
498 back to one of our internal data structures. I think the only way
499 this is used is to record a decl which is to be returned by
500 getdecls, and we could implement it for that purpose if
501 necessary. */
503 static tree
504 go_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED)
506 gcc_unreachable ();
509 /* This hook is used to get the current list of declarations as trees.
510 We don't support that; instead we use the write_globals hook. This
511 can't simply crash because it is called by -gstabs. */
513 static tree
514 go_langhook_getdecls (void)
516 return NULL;
519 /* Go specific gimplification. We need to gimplify
520 CALL_EXPR_STATIC_CHAIN, because the gimplifier doesn't handle
521 it. */
523 static int
524 go_langhook_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
526 if (TREE_CODE (*expr_p) == CALL_EXPR
527 && CALL_EXPR_STATIC_CHAIN (*expr_p) != NULL_TREE)
528 gimplify_expr (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p, post_p,
529 is_gimple_val, fb_rvalue);
530 return GS_UNHANDLED;
533 /* Return a decl for the exception personality function. The function
534 itself is implemented in libgo/runtime/go-unwind.c. */
536 static tree
537 go_langhook_eh_personality (void)
539 static tree personality_decl;
540 if (personality_decl == NULL_TREE)
542 personality_decl = build_personality_function ("gccgo");
543 go_preserve_from_gc (personality_decl);
545 return personality_decl;
548 /* Get a value for the SARIF v2.1.0 "artifact.sourceLanguage" property,
549 based on the list in SARIF v2.1.0 Appendix J. */
551 static const char *
552 go_get_sarif_source_language (const char *)
554 return "go";
557 /* Functions called directly by the generic backend. */
559 tree
560 convert (tree type, tree expr)
562 if (type == error_mark_node
563 || expr == error_mark_node
564 || TREE_TYPE (expr) == error_mark_node)
565 return error_mark_node;
567 if (type == TREE_TYPE (expr))
568 return expr;
570 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr)))
571 return fold_convert (type, expr);
573 switch (TREE_CODE (type))
575 case VOID_TYPE:
576 case BOOLEAN_TYPE:
577 return fold_convert (type, expr);
578 case INTEGER_TYPE:
579 return fold (convert_to_integer (type, expr));
580 case POINTER_TYPE:
581 return fold (convert_to_pointer (type, expr));
582 case REAL_TYPE:
583 return fold (convert_to_real (type, expr));
584 case COMPLEX_TYPE:
585 return fold (convert_to_complex (type, expr));
586 default:
587 break;
590 gcc_unreachable ();
593 /* FIXME: This is a hack to preserve trees that we create from the
594 garbage collector. */
596 static GTY(()) tree go_gc_root;
598 void
599 go_preserve_from_gc (tree t)
601 go_gc_root = tree_cons (NULL_TREE, t, go_gc_root);
604 /* Convert an identifier for use in an error message. */
606 const char *
607 go_localize_identifier (const char *ident)
609 return identifier_to_locale (ident);
612 #undef LANG_HOOKS_NAME
613 #undef LANG_HOOKS_INIT
614 #undef LANG_HOOKS_OPTION_LANG_MASK
615 #undef LANG_HOOKS_INIT_OPTIONS_STRUCT
616 #undef LANG_HOOKS_HANDLE_OPTION
617 #undef LANG_HOOKS_POST_OPTIONS
618 #undef LANG_HOOKS_PARSE_FILE
619 #undef LANG_HOOKS_TYPE_FOR_MODE
620 #undef LANG_HOOKS_TYPE_FOR_SIZE
621 #undef LANG_HOOKS_BUILTIN_FUNCTION
622 #undef LANG_HOOKS_GLOBAL_BINDINGS_P
623 #undef LANG_HOOKS_PUSHDECL
624 #undef LANG_HOOKS_GETDECLS
625 #undef LANG_HOOKS_GIMPLIFY_EXPR
626 #undef LANG_HOOKS_EH_PERSONALITY
627 #undef LANG_HOOKS_GET_SARIF_SOURCE_LANGUAGE
629 #define LANG_HOOKS_NAME "GNU Go"
630 #define LANG_HOOKS_INIT go_langhook_init
631 #define LANG_HOOKS_OPTION_LANG_MASK go_langhook_option_lang_mask
632 #define LANG_HOOKS_INIT_OPTIONS_STRUCT go_langhook_init_options_struct
633 #define LANG_HOOKS_HANDLE_OPTION go_langhook_handle_option
634 #define LANG_HOOKS_POST_OPTIONS go_langhook_post_options
635 #define LANG_HOOKS_PARSE_FILE go_langhook_parse_file
636 #define LANG_HOOKS_TYPE_FOR_MODE go_langhook_type_for_mode
637 #define LANG_HOOKS_TYPE_FOR_SIZE go_langhook_type_for_size
638 #define LANG_HOOKS_BUILTIN_FUNCTION go_langhook_builtin_function
639 #define LANG_HOOKS_GLOBAL_BINDINGS_P go_langhook_global_bindings_p
640 #define LANG_HOOKS_PUSHDECL go_langhook_pushdecl
641 #define LANG_HOOKS_GETDECLS go_langhook_getdecls
642 #define LANG_HOOKS_GIMPLIFY_EXPR go_langhook_gimplify_expr
643 #define LANG_HOOKS_EH_PERSONALITY go_langhook_eh_personality
644 #define LANG_HOOKS_GET_SARIF_SOURCE_LANGUAGE go_get_sarif_source_language
646 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
648 #include "gt-go-go-lang.h"
649 #include "gtype-go.h"