coarray_41.f90: Add "-latomic" option if libatomic_available.
[official-gcc.git] / gcc / go / go-lang.c
blob780d73799e6cf162b1eb575faf7e63043fbea765
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.linemap = go_get_linemap();
116 args.backend = go_get_backend();
117 go_create_gogo (&args);
119 build_common_builtin_nodes ();
121 /* The default precision for floating point numbers. This is used
122 for floating point constants with abstract type. This may
123 eventually be controllable by a command line option. */
124 mpfr_set_default_prec (256);
126 /* Go uses exceptions. */
127 using_eh_for_cleanups ();
129 return true;
132 /* The option mask. */
134 static unsigned int
135 go_langhook_option_lang_mask (void)
137 return CL_Go;
140 /* Initialize the options structure. */
142 static void
143 go_langhook_init_options_struct (struct gcc_options *opts)
145 /* Go says that signed overflow is precisely defined. */
146 opts->x_flag_wrapv = 1;
148 /* We default to using strict aliasing, since Go pointers are safe.
149 This is turned off for code that imports the "unsafe" package,
150 because using unsafe.pointer violates C style aliasing
151 requirements. */
152 opts->x_flag_strict_aliasing = 1;
154 /* Default to avoiding range issues for complex multiply and
155 divide. */
156 opts->x_flag_complex_method = 2;
158 /* The builtin math functions should not set errno. */
159 opts->x_flag_errno_math = 0;
160 opts->frontend_set_flag_errno_math = true;
162 /* Exceptions are used to handle recovering from panics. */
163 opts->x_flag_exceptions = 1;
164 opts->x_flag_non_call_exceptions = 1;
166 /* We need to keep pointers live for the garbage collector. */
167 opts->x_flag_keep_gc_roots_live = 1;
169 /* Go programs expect runtime.Callers to work, and that uses
170 libbacktrace that uses debug info. Set the debug info level to 1
171 by default. In post_options we will set the debug type if the
172 debug info level was not set back to 0 on the command line. */
173 opts->x_debug_info_level = DINFO_LEVEL_TERSE;
176 /* Infrastructure for a vector of char * pointers. */
178 typedef const char *go_char_p;
180 /* The list of directories to search after all the Go specific
181 directories have been searched. */
183 static vec<go_char_p> go_search_dirs;
185 /* Handle Go specific options. Return 0 if we didn't do anything. */
187 static bool
188 go_langhook_handle_option (
189 size_t scode,
190 const char *arg,
191 int value ATTRIBUTE_UNUSED,
192 int kind ATTRIBUTE_UNUSED,
193 location_t loc ATTRIBUTE_UNUSED,
194 const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED)
196 enum opt_code code = (enum opt_code) scode;
197 bool ret = true;
199 switch (code)
201 case OPT_I:
202 go_add_search_path (arg);
203 break;
205 case OPT_L:
206 /* A -L option is assumed to come from the compiler driver.
207 This is a system directory. We search the following
208 directories, if they exist, before this one:
209 dir/go/VERSION
210 dir/go/VERSION/MACHINE
211 This is like include/c++. */
213 static const char dir_separator_str[] = { DIR_SEPARATOR, 0 };
214 size_t len;
215 char *p;
216 struct stat st;
218 len = strlen (arg);
219 p = XALLOCAVEC (char,
220 (len + sizeof "go" + sizeof DEFAULT_TARGET_VERSION
221 + sizeof DEFAULT_TARGET_MACHINE + 3));
222 strcpy (p, arg);
223 if (len > 0 && !IS_DIR_SEPARATOR (p[len - 1]))
224 strcat (p, dir_separator_str);
225 strcat (p, "go");
226 strcat (p, dir_separator_str);
227 strcat (p, DEFAULT_TARGET_VERSION);
228 if (stat (p, &st) == 0 && S_ISDIR (st.st_mode))
230 go_add_search_path (p);
231 strcat (p, dir_separator_str);
232 strcat (p, DEFAULT_TARGET_MACHINE);
233 if (stat (p, &st) == 0 && S_ISDIR (st.st_mode))
234 go_add_search_path (p);
237 /* Search ARG too, but only after we've searched to Go
238 specific directories for all -L arguments. */
239 go_search_dirs.safe_push (arg);
241 break;
243 case OPT_fgo_dump_:
244 ret = go_enable_dump (arg) ? true : false;
245 break;
247 case OPT_fgo_optimize_:
248 ret = go_enable_optimize (arg) ? true : false;
249 break;
251 case OPT_fgo_pkgpath_:
252 go_pkgpath = arg;
253 break;
255 case OPT_fgo_prefix_:
256 go_prefix = arg;
257 break;
259 case OPT_fgo_relative_import_path_:
260 go_relative_import_path = arg;
261 break;
263 case OPT_fgo_c_header_:
264 go_c_header = arg;
265 break;
267 default:
268 /* Just return 1 to indicate that the option is valid. */
269 break;
272 return ret;
275 /* Run after parsing options. */
277 static bool
278 go_langhook_post_options (const char **pfilename ATTRIBUTE_UNUSED)
280 unsigned int ix;
281 const char *dir;
283 gcc_assert (num_in_fnames > 0);
285 FOR_EACH_VEC_ELT (go_search_dirs, ix, dir)
286 go_add_search_path (dir);
287 go_search_dirs.release ();
289 if (flag_excess_precision_cmdline == EXCESS_PRECISION_DEFAULT)
290 flag_excess_precision_cmdline = EXCESS_PRECISION_STANDARD;
292 /* Tail call optimizations can confuse uses of runtime.Callers. */
293 if (!global_options_set.x_flag_optimize_sibling_calls)
294 global_options.x_flag_optimize_sibling_calls = 0;
296 /* If the debug info level is still 1, as set in init_options, make
297 sure that some debugging type is selected. */
298 if (global_options.x_debug_info_level == DINFO_LEVEL_TERSE
299 && global_options.x_write_symbols == NO_DEBUG)
300 global_options.x_write_symbols = PREFERRED_DEBUGGING_TYPE;
302 /* We turn on stack splitting if we can. */
303 if (!global_options_set.x_flag_split_stack
304 && targetm_common.supports_split_stack (false, &global_options))
305 global_options.x_flag_split_stack = 1;
307 /* Returning false means that the backend should be used. */
308 return false;
311 static void
312 go_langhook_parse_file (void)
314 go_parse_input_files (in_fnames, num_in_fnames, flag_syntax_only,
315 go_require_return_statement);
317 /* Final processing of globals and early debug info generation. */
318 go_write_globals ();
321 static tree
322 go_langhook_type_for_size (unsigned int bits, int unsignedp)
324 tree type;
325 if (unsignedp)
327 if (bits == INT_TYPE_SIZE)
328 type = unsigned_type_node;
329 else if (bits == CHAR_TYPE_SIZE)
330 type = unsigned_char_type_node;
331 else if (bits == SHORT_TYPE_SIZE)
332 type = short_unsigned_type_node;
333 else if (bits == LONG_TYPE_SIZE)
334 type = long_unsigned_type_node;
335 else if (bits == LONG_LONG_TYPE_SIZE)
336 type = long_long_unsigned_type_node;
337 else
338 type = make_unsigned_type(bits);
340 else
342 if (bits == INT_TYPE_SIZE)
343 type = integer_type_node;
344 else if (bits == CHAR_TYPE_SIZE)
345 type = signed_char_type_node;
346 else if (bits == SHORT_TYPE_SIZE)
347 type = short_integer_type_node;
348 else if (bits == LONG_TYPE_SIZE)
349 type = long_integer_type_node;
350 else if (bits == LONG_LONG_TYPE_SIZE)
351 type = long_long_integer_type_node;
352 else
353 type = make_signed_type(bits);
355 return type;
358 static tree
359 go_langhook_type_for_mode (machine_mode mode, int unsignedp)
361 tree type;
362 /* Go has no vector types. Build them here. FIXME: It does not
363 make sense for the middle-end to ask the frontend for a type
364 which the frontend does not support. However, at least for now
365 it is required. See PR 46805. */
366 if (VECTOR_MODE_P (mode))
368 tree inner;
370 inner = go_langhook_type_for_mode (GET_MODE_INNER (mode), unsignedp);
371 if (inner != NULL_TREE)
372 return build_vector_type_for_mode (inner, mode);
373 return NULL_TREE;
376 enum mode_class mc = GET_MODE_CLASS (mode);
377 if (mc == MODE_INT)
378 return go_langhook_type_for_size (GET_MODE_BITSIZE (mode), unsignedp);
379 else if (mc == MODE_FLOAT)
381 switch (GET_MODE_BITSIZE (mode))
383 case 32:
384 return float_type_node;
385 case 64:
386 return double_type_node;
387 default:
388 // We have to check for long double in order to support
389 // i386 excess precision.
390 if (mode == TYPE_MODE(long_double_type_node))
391 return long_double_type_node;
394 else if (mc == MODE_COMPLEX_FLOAT)
396 switch (GET_MODE_BITSIZE (mode))
398 case 64:
399 return complex_float_type_node;
400 case 128:
401 return complex_double_type_node;
402 default:
403 // We have to check for long double in order to support
404 // i386 excess precision.
405 if (mode == TYPE_MODE(complex_long_double_type_node))
406 return complex_long_double_type_node;
410 #if HOST_BITS_PER_WIDE_INT >= 64
411 /* The middle-end and some backends rely on TImode being supported
412 for 64-bit HWI. */
413 if (mode == TImode)
415 type = build_nonstandard_integer_type (GET_MODE_BITSIZE (TImode),
416 unsignedp);
417 if (type && TYPE_MODE (type) == TImode)
418 return type;
420 #endif
421 return NULL_TREE;
424 /* Record a builtin function. We just ignore builtin functions. */
426 static tree
427 go_langhook_builtin_function (tree decl)
429 return decl;
432 /* Return true if we are in the global binding level. */
434 static bool
435 go_langhook_global_bindings_p (void)
437 return current_function_decl == NULL_TREE;
440 /* Push a declaration into the current binding level. We can't
441 usefully implement this since we don't want to convert from tree
442 back to one of our internal data structures. I think the only way
443 this is used is to record a decl which is to be returned by
444 getdecls, and we could implement it for that purpose if
445 necessary. */
447 static tree
448 go_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED)
450 gcc_unreachable ();
453 /* This hook is used to get the current list of declarations as trees.
454 We don't support that; instead we use the write_globals hook. This
455 can't simply crash because it is called by -gstabs. */
457 static tree
458 go_langhook_getdecls (void)
460 return NULL;
463 /* Go specific gimplification. We need to gimplify
464 CALL_EXPR_STATIC_CHAIN, because the gimplifier doesn't handle
465 it. */
467 static int
468 go_langhook_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
470 if (TREE_CODE (*expr_p) == CALL_EXPR
471 && CALL_EXPR_STATIC_CHAIN (*expr_p) != NULL_TREE)
472 gimplify_expr (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p, post_p,
473 is_gimple_val, fb_rvalue);
474 return GS_UNHANDLED;
477 /* Return a decl for the exception personality function. The function
478 itself is implemented in libgo/runtime/go-unwind.c. */
480 static tree
481 go_langhook_eh_personality (void)
483 static tree personality_decl;
484 if (personality_decl == NULL_TREE)
486 personality_decl = build_personality_function ("gccgo");
487 go_preserve_from_gc (personality_decl);
489 return personality_decl;
492 /* Functions called directly by the generic backend. */
494 tree
495 convert (tree type, tree expr)
497 if (type == error_mark_node
498 || expr == error_mark_node
499 || TREE_TYPE (expr) == error_mark_node)
500 return error_mark_node;
502 if (type == TREE_TYPE (expr))
503 return expr;
505 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr)))
506 return fold_convert (type, expr);
508 switch (TREE_CODE (type))
510 case VOID_TYPE:
511 case BOOLEAN_TYPE:
512 return fold_convert (type, expr);
513 case INTEGER_TYPE:
514 return fold (convert_to_integer (type, expr));
515 case POINTER_TYPE:
516 return fold (convert_to_pointer (type, expr));
517 case REAL_TYPE:
518 return fold (convert_to_real (type, expr));
519 case COMPLEX_TYPE:
520 return fold (convert_to_complex (type, expr));
521 default:
522 break;
525 gcc_unreachable ();
528 /* FIXME: This is a hack to preserve trees that we create from the
529 garbage collector. */
531 static GTY(()) tree go_gc_root;
533 void
534 go_preserve_from_gc (tree t)
536 go_gc_root = tree_cons (NULL_TREE, t, go_gc_root);
539 /* Convert an identifier for use in an error message. */
541 const char *
542 go_localize_identifier (const char *ident)
544 return identifier_to_locale (ident);
547 #undef LANG_HOOKS_NAME
548 #undef LANG_HOOKS_INIT
549 #undef LANG_HOOKS_OPTION_LANG_MASK
550 #undef LANG_HOOKS_INIT_OPTIONS_STRUCT
551 #undef LANG_HOOKS_HANDLE_OPTION
552 #undef LANG_HOOKS_POST_OPTIONS
553 #undef LANG_HOOKS_PARSE_FILE
554 #undef LANG_HOOKS_TYPE_FOR_MODE
555 #undef LANG_HOOKS_TYPE_FOR_SIZE
556 #undef LANG_HOOKS_BUILTIN_FUNCTION
557 #undef LANG_HOOKS_GLOBAL_BINDINGS_P
558 #undef LANG_HOOKS_PUSHDECL
559 #undef LANG_HOOKS_GETDECLS
560 #undef LANG_HOOKS_GIMPLIFY_EXPR
561 #undef LANG_HOOKS_EH_PERSONALITY
563 #define LANG_HOOKS_NAME "GNU Go"
564 #define LANG_HOOKS_INIT go_langhook_init
565 #define LANG_HOOKS_OPTION_LANG_MASK go_langhook_option_lang_mask
566 #define LANG_HOOKS_INIT_OPTIONS_STRUCT go_langhook_init_options_struct
567 #define LANG_HOOKS_HANDLE_OPTION go_langhook_handle_option
568 #define LANG_HOOKS_POST_OPTIONS go_langhook_post_options
569 #define LANG_HOOKS_PARSE_FILE go_langhook_parse_file
570 #define LANG_HOOKS_TYPE_FOR_MODE go_langhook_type_for_mode
571 #define LANG_HOOKS_TYPE_FOR_SIZE go_langhook_type_for_size
572 #define LANG_HOOKS_BUILTIN_FUNCTION go_langhook_builtin_function
573 #define LANG_HOOKS_GLOBAL_BINDINGS_P go_langhook_global_bindings_p
574 #define LANG_HOOKS_PUSHDECL go_langhook_pushdecl
575 #define LANG_HOOKS_GETDECLS go_langhook_getdecls
576 #define LANG_HOOKS_GIMPLIFY_EXPR go_langhook_gimplify_expr
577 #define LANG_HOOKS_EH_PERSONALITY go_langhook_eh_personality
579 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
581 #include "gt-go-go-lang.h"
582 #include "gtype-go.h"