Improve fstack_protector effective target
[official-gcc.git] / gcc / go / go-lang.c
blobfda069aee6dbe0f823e33d8b590531339a11fa03
1 /* go-lang.c -- Go frontend gcc interface.
2 Copyright (C) 2009-2017 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 /* Language-dependent contents of a type. */
44 struct GTY(()) lang_type
46 char dummy;
49 /* Language-dependent contents of a decl. */
51 struct GTY(()) lang_decl
53 char dummy;
56 /* Language-dependent contents of an identifier. This must include a
57 tree_identifier. */
59 struct GTY(()) lang_identifier
61 struct tree_identifier common;
64 /* The resulting tree type. */
66 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
67 chain_next ("CODE_CONTAINS_STRUCT (TREE_CODE (&%h.generic), TS_COMMON) ? ((union lang_tree_node *) TREE_CHAIN (&%h.generic)) : NULL")))
68 lang_tree_node
70 union tree_node GTY((tag ("0"),
71 desc ("tree_node_structure (&%h)"))) generic;
72 struct lang_identifier GTY((tag ("1"))) identifier;
75 /* We don't use language_function. */
77 struct GTY(()) language_function
79 int dummy;
82 /* Option information we need to pass to go_create_gogo. */
84 static const char *go_pkgpath = NULL;
85 static const char *go_prefix = NULL;
86 static const char *go_relative_import_path = NULL;
87 static const char *go_c_header = NULL;
89 /* Language hooks. */
91 static bool
92 go_langhook_init (void)
94 build_common_tree_nodes (false);
96 /* I don't know why this has to be done explicitly. */
97 void_list_node = build_tree_list (NULL_TREE, void_type_node);
99 /* We must create the gogo IR after calling build_common_tree_nodes
100 (because Gogo::define_builtin_function_trees refers indirectly
101 to, e.g., unsigned_char_type_node) but before calling
102 build_common_builtin_nodes (because it calls, indirectly,
103 go_type_for_size). */
104 struct go_create_gogo_args args;
105 args.int_type_size = INT_TYPE_SIZE;
106 args.pointer_size = POINTER_SIZE;
107 args.pkgpath = go_pkgpath;
108 args.prefix = go_prefix;
109 args.relative_import_path = go_relative_import_path;
110 args.c_header = go_c_header;
111 args.check_divide_by_zero = go_check_divide_zero;
112 args.check_divide_overflow = go_check_divide_overflow;
113 args.compiling_runtime = go_compiling_runtime;
114 args.debug_escape_level = go_debug_escape_level;
115 args.nil_check_size_threshold = 4096;
116 args.linemap = go_get_linemap();
117 args.backend = go_get_backend();
118 go_create_gogo (&args);
120 build_common_builtin_nodes ();
122 /* The default precision for floating point numbers. This is used
123 for floating point constants with abstract type. This may
124 eventually be controllable by a command line option. */
125 mpfr_set_default_prec (256);
127 /* Go uses exceptions. */
128 using_eh_for_cleanups ();
130 return true;
133 /* The option mask. */
135 static unsigned int
136 go_langhook_option_lang_mask (void)
138 return CL_Go;
141 /* Initialize the options structure. */
143 static void
144 go_langhook_init_options_struct (struct gcc_options *opts)
146 /* Go says that signed overflow is precisely defined. */
147 opts->x_flag_wrapv = 1;
149 /* We default to using strict aliasing, since Go pointers are safe.
150 This is turned off for code that imports the "unsafe" package,
151 because using unsafe.pointer violates C style aliasing
152 requirements. */
153 opts->x_flag_strict_aliasing = 1;
155 /* Default to avoiding range issues for complex multiply and
156 divide. */
157 opts->x_flag_complex_method = 2;
159 /* The builtin math functions should not set errno. */
160 opts->x_flag_errno_math = 0;
161 opts->frontend_set_flag_errno_math = true;
163 /* Exceptions are used to handle recovering from panics. */
164 opts->x_flag_exceptions = 1;
165 opts->x_flag_non_call_exceptions = 1;
167 /* We need to keep pointers live for the garbage collector. */
168 opts->x_flag_keep_gc_roots_live = 1;
170 /* Go programs expect runtime.Callers to work, and that uses
171 libbacktrace that uses debug info. Set the debug info level to 1
172 by default. In post_options we will set the debug type if the
173 debug info level was not set back to 0 on the command line. */
174 opts->x_debug_info_level = DINFO_LEVEL_TERSE;
177 /* Infrastructure for a vector of char * pointers. */
179 typedef const char *go_char_p;
181 /* The list of directories to search after all the Go specific
182 directories have been searched. */
184 static vec<go_char_p> go_search_dirs;
186 /* Handle Go specific options. Return 0 if we didn't do anything. */
188 static bool
189 go_langhook_handle_option (
190 size_t scode,
191 const char *arg,
192 int value ATTRIBUTE_UNUSED,
193 int kind ATTRIBUTE_UNUSED,
194 location_t loc ATTRIBUTE_UNUSED,
195 const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED)
197 enum opt_code code = (enum opt_code) scode;
198 bool ret = true;
200 switch (code)
202 case OPT_I:
203 go_add_search_path (arg);
204 break;
206 case OPT_L:
207 /* A -L option is assumed to come from the compiler driver.
208 This is a system directory. We search the following
209 directories, if they exist, before this one:
210 dir/go/VERSION
211 dir/go/VERSION/MACHINE
212 This is like include/c++. */
214 static const char dir_separator_str[] = { DIR_SEPARATOR, 0 };
215 size_t len;
216 char *p;
217 struct stat st;
219 len = strlen (arg);
220 p = XALLOCAVEC (char,
221 (len + sizeof "go" + sizeof DEFAULT_TARGET_VERSION
222 + sizeof DEFAULT_TARGET_MACHINE + 3));
223 strcpy (p, arg);
224 if (len > 0 && !IS_DIR_SEPARATOR (p[len - 1]))
225 strcat (p, dir_separator_str);
226 strcat (p, "go");
227 strcat (p, dir_separator_str);
228 strcat (p, DEFAULT_TARGET_VERSION);
229 if (stat (p, &st) == 0 && S_ISDIR (st.st_mode))
231 go_add_search_path (p);
232 strcat (p, dir_separator_str);
233 strcat (p, DEFAULT_TARGET_MACHINE);
234 if (stat (p, &st) == 0 && S_ISDIR (st.st_mode))
235 go_add_search_path (p);
238 /* Search ARG too, but only after we've searched to Go
239 specific directories for all -L arguments. */
240 go_search_dirs.safe_push (arg);
242 break;
244 case OPT_fgo_dump_:
245 ret = go_enable_dump (arg) ? true : false;
246 break;
248 case OPT_fgo_optimize_:
249 ret = go_enable_optimize (arg) ? true : false;
250 break;
252 case OPT_fgo_pkgpath_:
253 go_pkgpath = arg;
254 break;
256 case OPT_fgo_prefix_:
257 go_prefix = arg;
258 break;
260 case OPT_fgo_relative_import_path_:
261 go_relative_import_path = arg;
262 break;
264 case OPT_fgo_c_header_:
265 go_c_header = arg;
266 break;
268 default:
269 /* Just return 1 to indicate that the option is valid. */
270 break;
273 return ret;
276 /* Run after parsing options. */
278 static bool
279 go_langhook_post_options (const char **pfilename ATTRIBUTE_UNUSED)
281 unsigned int ix;
282 const char *dir;
284 gcc_assert (num_in_fnames > 0);
286 FOR_EACH_VEC_ELT (go_search_dirs, ix, dir)
287 go_add_search_path (dir);
288 go_search_dirs.release ();
290 if (flag_excess_precision_cmdline == EXCESS_PRECISION_DEFAULT)
291 flag_excess_precision_cmdline = EXCESS_PRECISION_STANDARD;
293 /* Tail call optimizations can confuse uses of runtime.Callers. */
294 if (!global_options_set.x_flag_optimize_sibling_calls)
295 global_options.x_flag_optimize_sibling_calls = 0;
297 /* If the debug info level is still 1, as set in init_options, make
298 sure that some debugging type is selected. */
299 if (global_options.x_debug_info_level == DINFO_LEVEL_TERSE
300 && global_options.x_write_symbols == NO_DEBUG)
301 global_options.x_write_symbols = PREFERRED_DEBUGGING_TYPE;
303 /* We turn on stack splitting if we can. */
304 if (!global_options_set.x_flag_split_stack
305 && targetm_common.supports_split_stack (false, &global_options))
306 global_options.x_flag_split_stack = 1;
308 /* If stack splitting is turned on, and the user did not explicitly
309 request function partitioning, turn off partitioning, as it
310 confuses the linker when trying to handle partitioned split-stack
311 code that calls a non-split-stack function. */
312 if (global_options.x_flag_split_stack
313 && global_options.x_flag_reorder_blocks_and_partition
314 && !global_options_set.x_flag_reorder_blocks_and_partition)
315 global_options.x_flag_reorder_blocks_and_partition = 0;
317 /* Returning false means that the backend should be used. */
318 return false;
321 static void
322 go_langhook_parse_file (void)
324 go_parse_input_files (in_fnames, num_in_fnames, flag_syntax_only,
325 go_require_return_statement);
327 /* Final processing of globals and early debug info generation. */
328 go_write_globals ();
331 static tree
332 go_langhook_type_for_size (unsigned int bits, int unsignedp)
334 tree type;
335 if (unsignedp)
337 if (bits == INT_TYPE_SIZE)
338 type = unsigned_type_node;
339 else if (bits == CHAR_TYPE_SIZE)
340 type = unsigned_char_type_node;
341 else if (bits == SHORT_TYPE_SIZE)
342 type = short_unsigned_type_node;
343 else if (bits == LONG_TYPE_SIZE)
344 type = long_unsigned_type_node;
345 else if (bits == LONG_LONG_TYPE_SIZE)
346 type = long_long_unsigned_type_node;
347 else
348 type = make_unsigned_type(bits);
350 else
352 if (bits == INT_TYPE_SIZE)
353 type = integer_type_node;
354 else if (bits == CHAR_TYPE_SIZE)
355 type = signed_char_type_node;
356 else if (bits == SHORT_TYPE_SIZE)
357 type = short_integer_type_node;
358 else if (bits == LONG_TYPE_SIZE)
359 type = long_integer_type_node;
360 else if (bits == LONG_LONG_TYPE_SIZE)
361 type = long_long_integer_type_node;
362 else
363 type = make_signed_type(bits);
365 return type;
368 static tree
369 go_langhook_type_for_mode (machine_mode mode, int unsignedp)
371 tree type;
372 /* Go has no vector types. Build them here. FIXME: It does not
373 make sense for the middle-end to ask the frontend for a type
374 which the frontend does not support. However, at least for now
375 it is required. See PR 46805. */
376 if (VECTOR_MODE_P (mode))
378 tree inner;
380 inner = go_langhook_type_for_mode (GET_MODE_INNER (mode), unsignedp);
381 if (inner != NULL_TREE)
382 return build_vector_type_for_mode (inner, mode);
383 return NULL_TREE;
386 scalar_int_mode imode;
387 scalar_float_mode fmode;
388 complex_mode cmode;
389 if (is_int_mode (mode, &imode))
390 return go_langhook_type_for_size (GET_MODE_BITSIZE (imode), unsignedp);
391 else if (is_float_mode (mode, &fmode))
393 switch (GET_MODE_BITSIZE (fmode))
395 case 32:
396 return float_type_node;
397 case 64:
398 return double_type_node;
399 default:
400 // We have to check for long double in order to support
401 // i386 excess precision.
402 if (fmode == TYPE_MODE(long_double_type_node))
403 return long_double_type_node;
406 else if (is_complex_float_mode (mode, &cmode))
408 switch (GET_MODE_BITSIZE (cmode))
410 case 64:
411 return complex_float_type_node;
412 case 128:
413 return complex_double_type_node;
414 default:
415 // We have to check for long double in order to support
416 // i386 excess precision.
417 if (cmode == TYPE_MODE(complex_long_double_type_node))
418 return complex_long_double_type_node;
422 #if HOST_BITS_PER_WIDE_INT >= 64
423 /* The middle-end and some backends rely on TImode being supported
424 for 64-bit HWI. */
425 if (mode == TImode)
427 type = build_nonstandard_integer_type (GET_MODE_BITSIZE (TImode),
428 unsignedp);
429 if (type && TYPE_MODE (type) == TImode)
430 return type;
432 #endif
433 return NULL_TREE;
436 /* Record a builtin function. We just ignore builtin functions. */
438 static tree
439 go_langhook_builtin_function (tree decl)
441 return decl;
444 /* Return true if we are in the global binding level. */
446 static bool
447 go_langhook_global_bindings_p (void)
449 return current_function_decl == NULL_TREE;
452 /* Push a declaration into the current binding level. We can't
453 usefully implement this since we don't want to convert from tree
454 back to one of our internal data structures. I think the only way
455 this is used is to record a decl which is to be returned by
456 getdecls, and we could implement it for that purpose if
457 necessary. */
459 static tree
460 go_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED)
462 gcc_unreachable ();
465 /* This hook is used to get the current list of declarations as trees.
466 We don't support that; instead we use the write_globals hook. This
467 can't simply crash because it is called by -gstabs. */
469 static tree
470 go_langhook_getdecls (void)
472 return NULL;
475 /* Go specific gimplification. We need to gimplify
476 CALL_EXPR_STATIC_CHAIN, because the gimplifier doesn't handle
477 it. */
479 static int
480 go_langhook_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
482 if (TREE_CODE (*expr_p) == CALL_EXPR
483 && CALL_EXPR_STATIC_CHAIN (*expr_p) != NULL_TREE)
484 gimplify_expr (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p, post_p,
485 is_gimple_val, fb_rvalue);
486 return GS_UNHANDLED;
489 /* Return a decl for the exception personality function. The function
490 itself is implemented in libgo/runtime/go-unwind.c. */
492 static tree
493 go_langhook_eh_personality (void)
495 static tree personality_decl;
496 if (personality_decl == NULL_TREE)
498 personality_decl = build_personality_function ("gccgo");
499 go_preserve_from_gc (personality_decl);
501 return personality_decl;
504 /* Functions called directly by the generic backend. */
506 tree
507 convert (tree type, tree expr)
509 if (type == error_mark_node
510 || expr == error_mark_node
511 || TREE_TYPE (expr) == error_mark_node)
512 return error_mark_node;
514 if (type == TREE_TYPE (expr))
515 return expr;
517 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr)))
518 return fold_convert (type, expr);
520 switch (TREE_CODE (type))
522 case VOID_TYPE:
523 case BOOLEAN_TYPE:
524 return fold_convert (type, expr);
525 case INTEGER_TYPE:
526 return fold (convert_to_integer (type, expr));
527 case POINTER_TYPE:
528 return fold (convert_to_pointer (type, expr));
529 case REAL_TYPE:
530 return fold (convert_to_real (type, expr));
531 case COMPLEX_TYPE:
532 return fold (convert_to_complex (type, expr));
533 default:
534 break;
537 gcc_unreachable ();
540 /* FIXME: This is a hack to preserve trees that we create from the
541 garbage collector. */
543 static GTY(()) tree go_gc_root;
545 void
546 go_preserve_from_gc (tree t)
548 go_gc_root = tree_cons (NULL_TREE, t, go_gc_root);
551 /* Convert an identifier for use in an error message. */
553 const char *
554 go_localize_identifier (const char *ident)
556 return identifier_to_locale (ident);
559 #undef LANG_HOOKS_NAME
560 #undef LANG_HOOKS_INIT
561 #undef LANG_HOOKS_OPTION_LANG_MASK
562 #undef LANG_HOOKS_INIT_OPTIONS_STRUCT
563 #undef LANG_HOOKS_HANDLE_OPTION
564 #undef LANG_HOOKS_POST_OPTIONS
565 #undef LANG_HOOKS_PARSE_FILE
566 #undef LANG_HOOKS_TYPE_FOR_MODE
567 #undef LANG_HOOKS_TYPE_FOR_SIZE
568 #undef LANG_HOOKS_BUILTIN_FUNCTION
569 #undef LANG_HOOKS_GLOBAL_BINDINGS_P
570 #undef LANG_HOOKS_PUSHDECL
571 #undef LANG_HOOKS_GETDECLS
572 #undef LANG_HOOKS_GIMPLIFY_EXPR
573 #undef LANG_HOOKS_EH_PERSONALITY
575 #define LANG_HOOKS_NAME "GNU Go"
576 #define LANG_HOOKS_INIT go_langhook_init
577 #define LANG_HOOKS_OPTION_LANG_MASK go_langhook_option_lang_mask
578 #define LANG_HOOKS_INIT_OPTIONS_STRUCT go_langhook_init_options_struct
579 #define LANG_HOOKS_HANDLE_OPTION go_langhook_handle_option
580 #define LANG_HOOKS_POST_OPTIONS go_langhook_post_options
581 #define LANG_HOOKS_PARSE_FILE go_langhook_parse_file
582 #define LANG_HOOKS_TYPE_FOR_MODE go_langhook_type_for_mode
583 #define LANG_HOOKS_TYPE_FOR_SIZE go_langhook_type_for_size
584 #define LANG_HOOKS_BUILTIN_FUNCTION go_langhook_builtin_function
585 #define LANG_HOOKS_GLOBAL_BINDINGS_P go_langhook_global_bindings_p
586 #define LANG_HOOKS_PUSHDECL go_langhook_pushdecl
587 #define LANG_HOOKS_GETDECLS go_langhook_getdecls
588 #define LANG_HOOKS_GIMPLIFY_EXPR go_langhook_gimplify_expr
589 #define LANG_HOOKS_EH_PERSONALITY go_langhook_eh_personality
591 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
593 #include "gt-go-go-lang.h"
594 #include "gtype-go.h"