testsuite: Correct vec-rlmi-rlnm.c testsuite expected result
[official-gcc.git] / gcc / go / go-lang.c
blob2cfb41042bddfa7bcdc3c33bc92a805e120fdf26
1 /* go-lang.c -- Go frontend gcc interface.
2 Copyright (C) 2009-2020 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;
93 /* Language hooks. */
95 static bool
96 go_langhook_init (void)
98 build_common_tree_nodes (false);
100 /* I don't know why this has to be done explicitly. */
101 void_list_node = build_tree_list (NULL_TREE, void_type_node);
103 /* We must create the gogo IR after calling build_common_tree_nodes
104 (because Gogo::define_builtin_function_trees refers indirectly
105 to, e.g., unsigned_char_type_node) but before calling
106 build_common_builtin_nodes (because it calls, indirectly,
107 go_type_for_size). */
108 struct go_create_gogo_args args;
109 args.int_type_size = INT_TYPE_SIZE;
110 args.pointer_size = POINTER_SIZE;
111 args.pkgpath = go_pkgpath;
112 args.prefix = go_prefix;
113 args.relative_import_path = go_relative_import_path;
114 args.c_header = go_c_header;
115 args.check_divide_by_zero = go_check_divide_zero;
116 args.check_divide_overflow = go_check_divide_overflow;
117 args.compiling_runtime = go_compiling_runtime;
118 args.debug_escape_level = go_debug_escape_level;
119 args.debug_escape_hash = go_debug_escape_hash;
120 args.nil_check_size_threshold = TARGET_AIX ? -1 : 4096;
121 args.debug_optimization = go_debug_optimization;
122 args.need_eqtype = TARGET_AIX ? true : false;
123 args.linemap = go_get_linemap();
124 args.backend = go_get_backend();
125 go_create_gogo (&args);
127 build_common_builtin_nodes ();
129 /* The default precision for floating point numbers. This is used
130 for floating point constants with abstract type. This may
131 eventually be controllable by a command line option. */
132 mpfr_set_default_prec (256);
134 /* Go uses exceptions. */
135 using_eh_for_cleanups ();
137 return true;
140 /* The option mask. */
142 static unsigned int
143 go_langhook_option_lang_mask (void)
145 return CL_Go;
148 /* Initialize the options structure. */
150 static void
151 go_langhook_init_options_struct (struct gcc_options *opts)
153 /* Go says that signed overflow is precisely defined. */
154 opts->x_flag_wrapv = 1;
156 /* We default to using strict aliasing, since Go pointers are safe.
157 This is turned off for code that imports the "unsafe" package,
158 because using unsafe.pointer violates C style aliasing
159 requirements. */
160 opts->x_flag_strict_aliasing = 1;
162 /* Default to avoiding range issues for complex multiply and
163 divide. */
164 opts->x_flag_complex_method = 2;
166 /* The builtin math functions should not set errno. */
167 opts->x_flag_errno_math = 0;
168 opts->frontend_set_flag_errno_math = true;
170 /* Exceptions are used to handle recovering from panics. */
171 opts->x_flag_exceptions = 1;
172 opts->x_flag_non_call_exceptions = 1;
174 /* We need to keep pointers live for the garbage collector. */
175 opts->x_flag_keep_gc_roots_live = 1;
177 /* Go programs expect runtime.Callers to work, and that uses
178 libbacktrace that uses debug info. Set the debug info level to 1
179 by default. In post_options we will set the debug type if the
180 debug info level was not set back to 0 on the command line. */
181 opts->x_debug_info_level = DINFO_LEVEL_TERSE;
184 /* Infrastructure for a vector of char * pointers. */
186 typedef const char *go_char_p;
188 /* The list of directories to search after all the Go specific
189 directories have been searched. */
191 static vec<go_char_p> go_search_dirs;
193 /* Handle Go specific options. Return 0 if we didn't do anything. */
195 static bool
196 go_langhook_handle_option (
197 size_t scode,
198 const char *arg,
199 HOST_WIDE_INT value,
200 int kind ATTRIBUTE_UNUSED,
201 location_t loc ATTRIBUTE_UNUSED,
202 const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED)
204 enum opt_code code = (enum opt_code) scode;
205 bool ret = true;
207 switch (code)
209 case OPT_I:
210 go_add_search_path (arg);
211 break;
213 case OPT_L:
214 /* A -L option is assumed to come from the compiler driver.
215 This is a system directory. We search the following
216 directories, if they exist, before this one:
217 dir/go/VERSION
218 dir/go/VERSION/MACHINE
219 This is like include/c++. */
221 static const char dir_separator_str[] = { DIR_SEPARATOR, 0 };
222 size_t len;
223 char *p;
224 struct stat st;
226 len = strlen (arg);
227 p = XALLOCAVEC (char,
228 (len + sizeof "go" + sizeof DEFAULT_TARGET_VERSION
229 + sizeof DEFAULT_TARGET_MACHINE + 3));
230 strcpy (p, arg);
231 if (len > 0 && !IS_DIR_SEPARATOR (p[len - 1]))
232 strcat (p, dir_separator_str);
233 strcat (p, "go");
234 strcat (p, dir_separator_str);
235 strcat (p, DEFAULT_TARGET_VERSION);
236 if (stat (p, &st) == 0 && S_ISDIR (st.st_mode))
238 go_add_search_path (p);
239 strcat (p, dir_separator_str);
240 strcat (p, DEFAULT_TARGET_MACHINE);
241 if (stat (p, &st) == 0 && S_ISDIR (st.st_mode))
242 go_add_search_path (p);
245 /* Search ARG too, but only after we've searched to Go
246 specific directories for all -L arguments. */
247 go_search_dirs.safe_push (arg);
249 break;
251 case OPT_fgo_dump_:
252 ret = go_enable_dump (arg) ? true : false;
253 break;
255 case OPT_fgo_optimize_:
256 ret = go_enable_optimize (arg, value) ? true : false;
257 break;
259 case OPT_fgo_pkgpath_:
260 go_pkgpath = arg;
261 break;
263 case OPT_fgo_prefix_:
264 go_prefix = arg;
265 break;
267 case OPT_fgo_relative_import_path_:
268 go_relative_import_path = arg;
269 break;
271 case OPT_fgo_c_header_:
272 go_c_header = arg;
273 break;
275 default:
276 /* Just return 1 to indicate that the option is valid. */
277 break;
280 return ret;
283 /* Run after parsing options. */
285 static bool
286 go_langhook_post_options (const char **pfilename ATTRIBUTE_UNUSED)
288 unsigned int ix;
289 const char *dir;
291 gcc_assert (num_in_fnames > 0);
293 FOR_EACH_VEC_ELT (go_search_dirs, ix, dir)
294 go_add_search_path (dir);
295 go_search_dirs.release ();
297 if (flag_excess_precision == EXCESS_PRECISION_DEFAULT)
298 flag_excess_precision = EXCESS_PRECISION_STANDARD;
300 /* Tail call optimizations can confuse uses of runtime.Callers. */
301 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
302 flag_optimize_sibling_calls, 0);
304 /* Partial inlining can confuses uses of runtime.Callers.
305 See https://gcc.gnu.org/PR91663. */
306 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
307 flag_partial_inlining, 0);
309 /* If the debug info level is still 1, as set in init_options, make
310 sure that some debugging type is selected. */
311 if (global_options.x_debug_info_level == DINFO_LEVEL_TERSE
312 && global_options.x_write_symbols == NO_DEBUG)
313 global_options.x_write_symbols = PREFERRED_DEBUGGING_TYPE;
315 /* We turn on stack splitting if we can. */
316 if (targetm_common.supports_split_stack (false, &global_options))
317 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
318 flag_split_stack, 1);
320 /* If stack splitting is turned on, and the user did not explicitly
321 request function partitioning, turn off partitioning, as it
322 confuses the linker when trying to handle partitioned split-stack
323 code that calls a non-split-stack function. */
324 if (global_options.x_flag_split_stack
325 && global_options.x_flag_reorder_blocks_and_partition)
326 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
327 flag_reorder_blocks_and_partition, 0);
329 /* Returning false means that the backend should be used. */
330 return false;
333 static void
334 go_langhook_parse_file (void)
336 go_parse_input_files (in_fnames, num_in_fnames, flag_syntax_only,
337 go_require_return_statement);
339 /* Final processing of globals and early debug info generation. */
340 go_write_globals ();
343 static tree
344 go_langhook_type_for_size (unsigned int bits, int unsignedp)
346 tree type;
347 if (unsignedp)
349 if (bits == INT_TYPE_SIZE)
350 type = unsigned_type_node;
351 else if (bits == CHAR_TYPE_SIZE)
352 type = unsigned_char_type_node;
353 else if (bits == SHORT_TYPE_SIZE)
354 type = short_unsigned_type_node;
355 else if (bits == LONG_TYPE_SIZE)
356 type = long_unsigned_type_node;
357 else if (bits == LONG_LONG_TYPE_SIZE)
358 type = long_long_unsigned_type_node;
359 else
360 type = make_unsigned_type(bits);
362 else
364 if (bits == INT_TYPE_SIZE)
365 type = integer_type_node;
366 else if (bits == CHAR_TYPE_SIZE)
367 type = signed_char_type_node;
368 else if (bits == SHORT_TYPE_SIZE)
369 type = short_integer_type_node;
370 else if (bits == LONG_TYPE_SIZE)
371 type = long_integer_type_node;
372 else if (bits == LONG_LONG_TYPE_SIZE)
373 type = long_long_integer_type_node;
374 else
375 type = make_signed_type(bits);
377 return type;
380 static tree
381 go_langhook_type_for_mode (machine_mode mode, int unsignedp)
383 tree type;
384 /* Go has no vector types. Build them here. FIXME: It does not
385 make sense for the middle-end to ask the frontend for a type
386 which the frontend does not support. However, at least for now
387 it is required. See PR 46805. */
388 if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL
389 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
391 unsigned int elem_bits = vector_element_size (GET_MODE_BITSIZE (mode),
392 GET_MODE_NUNITS (mode));
393 tree bool_type = build_nonstandard_boolean_type (elem_bits);
394 return build_vector_type_for_mode (bool_type, mode);
396 else if (VECTOR_MODE_P (mode)
397 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
399 tree inner;
401 inner = go_langhook_type_for_mode (GET_MODE_INNER (mode), unsignedp);
402 if (inner != NULL_TREE)
403 return build_vector_type_for_mode (inner, mode);
404 return NULL_TREE;
407 scalar_int_mode imode;
408 scalar_float_mode fmode;
409 complex_mode cmode;
410 if (is_int_mode (mode, &imode))
411 return go_langhook_type_for_size (GET_MODE_BITSIZE (imode), unsignedp);
412 else if (is_float_mode (mode, &fmode))
414 switch (GET_MODE_BITSIZE (fmode))
416 case 32:
417 return float_type_node;
418 case 64:
419 return double_type_node;
420 default:
421 // We have to check for long double in order to support
422 // i386 excess precision.
423 if (fmode == TYPE_MODE(long_double_type_node))
424 return long_double_type_node;
427 else if (is_complex_float_mode (mode, &cmode))
429 switch (GET_MODE_BITSIZE (cmode))
431 case 64:
432 return complex_float_type_node;
433 case 128:
434 return complex_double_type_node;
435 default:
436 // We have to check for long double in order to support
437 // i386 excess precision.
438 if (cmode == TYPE_MODE(complex_long_double_type_node))
439 return complex_long_double_type_node;
443 #if HOST_BITS_PER_WIDE_INT >= 64
444 /* The middle-end and some backends rely on TImode being supported
445 for 64-bit HWI. */
446 if (mode == TImode)
448 type = build_nonstandard_integer_type (GET_MODE_BITSIZE (TImode),
449 unsignedp);
450 if (type && TYPE_MODE (type) == TImode)
451 return type;
453 #endif
454 return NULL_TREE;
457 /* Record a builtin function. We just ignore builtin functions. */
459 static tree
460 go_langhook_builtin_function (tree decl)
462 return decl;
465 /* Return true if we are in the global binding level. */
467 static bool
468 go_langhook_global_bindings_p (void)
470 return current_function_decl == NULL_TREE;
473 /* Push a declaration into the current binding level. We can't
474 usefully implement this since we don't want to convert from tree
475 back to one of our internal data structures. I think the only way
476 this is used is to record a decl which is to be returned by
477 getdecls, and we could implement it for that purpose if
478 necessary. */
480 static tree
481 go_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED)
483 gcc_unreachable ();
486 /* This hook is used to get the current list of declarations as trees.
487 We don't support that; instead we use the write_globals hook. This
488 can't simply crash because it is called by -gstabs. */
490 static tree
491 go_langhook_getdecls (void)
493 return NULL;
496 /* Go specific gimplification. We need to gimplify
497 CALL_EXPR_STATIC_CHAIN, because the gimplifier doesn't handle
498 it. */
500 static int
501 go_langhook_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
503 if (TREE_CODE (*expr_p) == CALL_EXPR
504 && CALL_EXPR_STATIC_CHAIN (*expr_p) != NULL_TREE)
505 gimplify_expr (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p, post_p,
506 is_gimple_val, fb_rvalue);
507 return GS_UNHANDLED;
510 /* Return a decl for the exception personality function. The function
511 itself is implemented in libgo/runtime/go-unwind.c. */
513 static tree
514 go_langhook_eh_personality (void)
516 static tree personality_decl;
517 if (personality_decl == NULL_TREE)
519 personality_decl = build_personality_function ("gccgo");
520 go_preserve_from_gc (personality_decl);
522 return personality_decl;
525 /* Functions called directly by the generic backend. */
527 tree
528 convert (tree type, tree expr)
530 if (type == error_mark_node
531 || expr == error_mark_node
532 || TREE_TYPE (expr) == error_mark_node)
533 return error_mark_node;
535 if (type == TREE_TYPE (expr))
536 return expr;
538 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr)))
539 return fold_convert (type, expr);
541 switch (TREE_CODE (type))
543 case VOID_TYPE:
544 case BOOLEAN_TYPE:
545 return fold_convert (type, expr);
546 case INTEGER_TYPE:
547 return fold (convert_to_integer (type, expr));
548 case POINTER_TYPE:
549 return fold (convert_to_pointer (type, expr));
550 case REAL_TYPE:
551 return fold (convert_to_real (type, expr));
552 case COMPLEX_TYPE:
553 return fold (convert_to_complex (type, expr));
554 default:
555 break;
558 gcc_unreachable ();
561 /* FIXME: This is a hack to preserve trees that we create from the
562 garbage collector. */
564 static GTY(()) tree go_gc_root;
566 void
567 go_preserve_from_gc (tree t)
569 go_gc_root = tree_cons (NULL_TREE, t, go_gc_root);
572 /* Convert an identifier for use in an error message. */
574 const char *
575 go_localize_identifier (const char *ident)
577 return identifier_to_locale (ident);
580 #undef LANG_HOOKS_NAME
581 #undef LANG_HOOKS_INIT
582 #undef LANG_HOOKS_OPTION_LANG_MASK
583 #undef LANG_HOOKS_INIT_OPTIONS_STRUCT
584 #undef LANG_HOOKS_HANDLE_OPTION
585 #undef LANG_HOOKS_POST_OPTIONS
586 #undef LANG_HOOKS_PARSE_FILE
587 #undef LANG_HOOKS_TYPE_FOR_MODE
588 #undef LANG_HOOKS_TYPE_FOR_SIZE
589 #undef LANG_HOOKS_BUILTIN_FUNCTION
590 #undef LANG_HOOKS_GLOBAL_BINDINGS_P
591 #undef LANG_HOOKS_PUSHDECL
592 #undef LANG_HOOKS_GETDECLS
593 #undef LANG_HOOKS_GIMPLIFY_EXPR
594 #undef LANG_HOOKS_EH_PERSONALITY
596 #define LANG_HOOKS_NAME "GNU Go"
597 #define LANG_HOOKS_INIT go_langhook_init
598 #define LANG_HOOKS_OPTION_LANG_MASK go_langhook_option_lang_mask
599 #define LANG_HOOKS_INIT_OPTIONS_STRUCT go_langhook_init_options_struct
600 #define LANG_HOOKS_HANDLE_OPTION go_langhook_handle_option
601 #define LANG_HOOKS_POST_OPTIONS go_langhook_post_options
602 #define LANG_HOOKS_PARSE_FILE go_langhook_parse_file
603 #define LANG_HOOKS_TYPE_FOR_MODE go_langhook_type_for_mode
604 #define LANG_HOOKS_TYPE_FOR_SIZE go_langhook_type_for_size
605 #define LANG_HOOKS_BUILTIN_FUNCTION go_langhook_builtin_function
606 #define LANG_HOOKS_GLOBAL_BINDINGS_P go_langhook_global_bindings_p
607 #define LANG_HOOKS_PUSHDECL go_langhook_pushdecl
608 #define LANG_HOOKS_GETDECLS go_langhook_getdecls
609 #define LANG_HOOKS_GIMPLIFY_EXPR go_langhook_gimplify_expr
610 #define LANG_HOOKS_EH_PERSONALITY go_langhook_eh_personality
612 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
614 #include "gt-go-go-lang.h"
615 #include "gtype-go.h"