1 /* go-lang.c -- Go frontend gcc interface.
2 Copyright (C) 2009-2018 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
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
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/>. */
22 #include "coretypes.h"
25 #include "gimple-expr.h"
26 #include "diagnostic.h"
28 #include "fold-const.h"
30 #include "stor-layout.h"
33 #include "langhooks.h"
34 #include "langhooks-def.h"
35 #include "common/common-target.h"
46 /* Language-dependent contents of a type. */
48 struct GTY(()) lang_type
53 /* Language-dependent contents of a decl. */
55 struct GTY(()) lang_decl
60 /* Language-dependent contents of an identifier. This must include a
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")))
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
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
;
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
.linemap
= go_get_linemap();
122 args
.backend
= go_get_backend();
123 go_create_gogo (&args
);
125 build_common_builtin_nodes ();
127 /* The default precision for floating point numbers. This is used
128 for floating point constants with abstract type. This may
129 eventually be controllable by a command line option. */
130 mpfr_set_default_prec (256);
132 /* Go uses exceptions. */
133 using_eh_for_cleanups ();
138 /* The option mask. */
141 go_langhook_option_lang_mask (void)
146 /* Initialize the options structure. */
149 go_langhook_init_options_struct (struct gcc_options
*opts
)
151 /* Go says that signed overflow is precisely defined. */
152 opts
->x_flag_wrapv
= 1;
154 /* We default to using strict aliasing, since Go pointers are safe.
155 This is turned off for code that imports the "unsafe" package,
156 because using unsafe.pointer violates C style aliasing
158 opts
->x_flag_strict_aliasing
= 1;
160 /* Default to avoiding range issues for complex multiply and
162 opts
->x_flag_complex_method
= 2;
164 /* The builtin math functions should not set errno. */
165 opts
->x_flag_errno_math
= 0;
166 opts
->frontend_set_flag_errno_math
= true;
168 /* Exceptions are used to handle recovering from panics. */
169 opts
->x_flag_exceptions
= 1;
170 opts
->x_flag_non_call_exceptions
= 1;
172 /* We need to keep pointers live for the garbage collector. */
173 opts
->x_flag_keep_gc_roots_live
= 1;
175 /* Go programs expect runtime.Callers to work, and that uses
176 libbacktrace that uses debug info. Set the debug info level to 1
177 by default. In post_options we will set the debug type if the
178 debug info level was not set back to 0 on the command line. */
179 opts
->x_debug_info_level
= DINFO_LEVEL_TERSE
;
182 /* Infrastructure for a vector of char * pointers. */
184 typedef const char *go_char_p
;
186 /* The list of directories to search after all the Go specific
187 directories have been searched. */
189 static vec
<go_char_p
> go_search_dirs
;
191 /* Handle Go specific options. Return 0 if we didn't do anything. */
194 go_langhook_handle_option (
197 int value ATTRIBUTE_UNUSED
,
198 int kind ATTRIBUTE_UNUSED
,
199 location_t loc ATTRIBUTE_UNUSED
,
200 const struct cl_option_handlers
*handlers ATTRIBUTE_UNUSED
)
202 enum opt_code code
= (enum opt_code
) scode
;
208 go_add_search_path (arg
);
212 /* A -L option is assumed to come from the compiler driver.
213 This is a system directory. We search the following
214 directories, if they exist, before this one:
216 dir/go/VERSION/MACHINE
217 This is like include/c++. */
219 static const char dir_separator_str
[] = { DIR_SEPARATOR
, 0 };
225 p
= XALLOCAVEC (char,
226 (len
+ sizeof "go" + sizeof DEFAULT_TARGET_VERSION
227 + sizeof DEFAULT_TARGET_MACHINE
+ 3));
229 if (len
> 0 && !IS_DIR_SEPARATOR (p
[len
- 1]))
230 strcat (p
, dir_separator_str
);
232 strcat (p
, dir_separator_str
);
233 strcat (p
, DEFAULT_TARGET_VERSION
);
234 if (stat (p
, &st
) == 0 && S_ISDIR (st
.st_mode
))
236 go_add_search_path (p
);
237 strcat (p
, dir_separator_str
);
238 strcat (p
, DEFAULT_TARGET_MACHINE
);
239 if (stat (p
, &st
) == 0 && S_ISDIR (st
.st_mode
))
240 go_add_search_path (p
);
243 /* Search ARG too, but only after we've searched to Go
244 specific directories for all -L arguments. */
245 go_search_dirs
.safe_push (arg
);
250 ret
= go_enable_dump (arg
) ? true : false;
253 case OPT_fgo_optimize_
:
254 ret
= go_enable_optimize (arg
) ? true : false;
257 case OPT_fgo_pkgpath_
:
261 case OPT_fgo_prefix_
:
265 case OPT_fgo_relative_import_path_
:
266 go_relative_import_path
= arg
;
269 case OPT_fgo_c_header_
:
274 /* Just return 1 to indicate that the option is valid. */
281 /* Run after parsing options. */
284 go_langhook_post_options (const char **pfilename ATTRIBUTE_UNUSED
)
289 gcc_assert (num_in_fnames
> 0);
291 FOR_EACH_VEC_ELT (go_search_dirs
, ix
, dir
)
292 go_add_search_path (dir
);
293 go_search_dirs
.release ();
295 if (flag_excess_precision_cmdline
== EXCESS_PRECISION_DEFAULT
)
296 flag_excess_precision_cmdline
= EXCESS_PRECISION_STANDARD
;
298 /* Tail call optimizations can confuse uses of runtime.Callers. */
299 if (!global_options_set
.x_flag_optimize_sibling_calls
)
300 global_options
.x_flag_optimize_sibling_calls
= 0;
302 /* If the debug info level is still 1, as set in init_options, make
303 sure that some debugging type is selected. */
304 if (global_options
.x_debug_info_level
== DINFO_LEVEL_TERSE
305 && global_options
.x_write_symbols
== NO_DEBUG
)
306 global_options
.x_write_symbols
= PREFERRED_DEBUGGING_TYPE
;
308 /* We turn on stack splitting if we can. */
309 if (!global_options_set
.x_flag_split_stack
310 && targetm_common
.supports_split_stack (false, &global_options
))
311 global_options
.x_flag_split_stack
= 1;
313 /* If stack splitting is turned on, and the user did not explicitly
314 request function partitioning, turn off partitioning, as it
315 confuses the linker when trying to handle partitioned split-stack
316 code that calls a non-split-stack function. */
317 if (global_options
.x_flag_split_stack
318 && global_options
.x_flag_reorder_blocks_and_partition
319 && !global_options_set
.x_flag_reorder_blocks_and_partition
)
320 global_options
.x_flag_reorder_blocks_and_partition
= 0;
322 /* Returning false means that the backend should be used. */
327 go_langhook_parse_file (void)
329 go_parse_input_files (in_fnames
, num_in_fnames
, flag_syntax_only
,
330 go_require_return_statement
);
332 /* Final processing of globals and early debug info generation. */
337 go_langhook_type_for_size (unsigned int bits
, int unsignedp
)
342 if (bits
== INT_TYPE_SIZE
)
343 type
= unsigned_type_node
;
344 else if (bits
== CHAR_TYPE_SIZE
)
345 type
= unsigned_char_type_node
;
346 else if (bits
== SHORT_TYPE_SIZE
)
347 type
= short_unsigned_type_node
;
348 else if (bits
== LONG_TYPE_SIZE
)
349 type
= long_unsigned_type_node
;
350 else if (bits
== LONG_LONG_TYPE_SIZE
)
351 type
= long_long_unsigned_type_node
;
353 type
= make_unsigned_type(bits
);
357 if (bits
== INT_TYPE_SIZE
)
358 type
= integer_type_node
;
359 else if (bits
== CHAR_TYPE_SIZE
)
360 type
= signed_char_type_node
;
361 else if (bits
== SHORT_TYPE_SIZE
)
362 type
= short_integer_type_node
;
363 else if (bits
== LONG_TYPE_SIZE
)
364 type
= long_integer_type_node
;
365 else if (bits
== LONG_LONG_TYPE_SIZE
)
366 type
= long_long_integer_type_node
;
368 type
= make_signed_type(bits
);
374 go_langhook_type_for_mode (machine_mode mode
, int unsignedp
)
377 /* Go has no vector types. Build them here. FIXME: It does not
378 make sense for the middle-end to ask the frontend for a type
379 which the frontend does not support. However, at least for now
380 it is required. See PR 46805. */
381 if (GET_MODE_CLASS (mode
) == MODE_VECTOR_BOOL
382 && valid_vector_subparts_p (GET_MODE_NUNITS (mode
)))
384 unsigned int elem_bits
= vector_element_size (GET_MODE_BITSIZE (mode
),
385 GET_MODE_NUNITS (mode
));
386 tree bool_type
= build_nonstandard_boolean_type (elem_bits
);
387 return build_vector_type_for_mode (bool_type
, mode
);
389 else if (VECTOR_MODE_P (mode
)
390 && valid_vector_subparts_p (GET_MODE_NUNITS (mode
)))
394 inner
= go_langhook_type_for_mode (GET_MODE_INNER (mode
), unsignedp
);
395 if (inner
!= NULL_TREE
)
396 return build_vector_type_for_mode (inner
, mode
);
400 scalar_int_mode imode
;
401 scalar_float_mode fmode
;
403 if (is_int_mode (mode
, &imode
))
404 return go_langhook_type_for_size (GET_MODE_BITSIZE (imode
), unsignedp
);
405 else if (is_float_mode (mode
, &fmode
))
407 switch (GET_MODE_BITSIZE (fmode
))
410 return float_type_node
;
412 return double_type_node
;
414 // We have to check for long double in order to support
415 // i386 excess precision.
416 if (fmode
== TYPE_MODE(long_double_type_node
))
417 return long_double_type_node
;
420 else if (is_complex_float_mode (mode
, &cmode
))
422 switch (GET_MODE_BITSIZE (cmode
))
425 return complex_float_type_node
;
427 return complex_double_type_node
;
429 // We have to check for long double in order to support
430 // i386 excess precision.
431 if (cmode
== TYPE_MODE(complex_long_double_type_node
))
432 return complex_long_double_type_node
;
436 #if HOST_BITS_PER_WIDE_INT >= 64
437 /* The middle-end and some backends rely on TImode being supported
441 type
= build_nonstandard_integer_type (GET_MODE_BITSIZE (TImode
),
443 if (type
&& TYPE_MODE (type
) == TImode
)
450 /* Record a builtin function. We just ignore builtin functions. */
453 go_langhook_builtin_function (tree decl
)
458 /* Return true if we are in the global binding level. */
461 go_langhook_global_bindings_p (void)
463 return current_function_decl
== NULL_TREE
;
466 /* Push a declaration into the current binding level. We can't
467 usefully implement this since we don't want to convert from tree
468 back to one of our internal data structures. I think the only way
469 this is used is to record a decl which is to be returned by
470 getdecls, and we could implement it for that purpose if
474 go_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED
)
479 /* This hook is used to get the current list of declarations as trees.
480 We don't support that; instead we use the write_globals hook. This
481 can't simply crash because it is called by -gstabs. */
484 go_langhook_getdecls (void)
489 /* Go specific gimplification. We need to gimplify
490 CALL_EXPR_STATIC_CHAIN, because the gimplifier doesn't handle
494 go_langhook_gimplify_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
)
496 if (TREE_CODE (*expr_p
) == CALL_EXPR
497 && CALL_EXPR_STATIC_CHAIN (*expr_p
) != NULL_TREE
)
498 gimplify_expr (&CALL_EXPR_STATIC_CHAIN (*expr_p
), pre_p
, post_p
,
499 is_gimple_val
, fb_rvalue
);
503 /* Return a decl for the exception personality function. The function
504 itself is implemented in libgo/runtime/go-unwind.c. */
507 go_langhook_eh_personality (void)
509 static tree personality_decl
;
510 if (personality_decl
== NULL_TREE
)
512 personality_decl
= build_personality_function ("gccgo");
513 go_preserve_from_gc (personality_decl
);
515 return personality_decl
;
518 /* Functions called directly by the generic backend. */
521 convert (tree type
, tree expr
)
523 if (type
== error_mark_node
524 || expr
== error_mark_node
525 || TREE_TYPE (expr
) == error_mark_node
)
526 return error_mark_node
;
528 if (type
== TREE_TYPE (expr
))
531 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (TREE_TYPE (expr
)))
532 return fold_convert (type
, expr
);
534 switch (TREE_CODE (type
))
538 return fold_convert (type
, expr
);
540 return fold (convert_to_integer (type
, expr
));
542 return fold (convert_to_pointer (type
, expr
));
544 return fold (convert_to_real (type
, expr
));
546 return fold (convert_to_complex (type
, expr
));
554 /* FIXME: This is a hack to preserve trees that we create from the
555 garbage collector. */
557 static GTY(()) tree go_gc_root
;
560 go_preserve_from_gc (tree t
)
562 go_gc_root
= tree_cons (NULL_TREE
, t
, go_gc_root
);
565 /* Convert an identifier for use in an error message. */
568 go_localize_identifier (const char *ident
)
570 return identifier_to_locale (ident
);
573 #undef LANG_HOOKS_NAME
574 #undef LANG_HOOKS_INIT
575 #undef LANG_HOOKS_OPTION_LANG_MASK
576 #undef LANG_HOOKS_INIT_OPTIONS_STRUCT
577 #undef LANG_HOOKS_HANDLE_OPTION
578 #undef LANG_HOOKS_POST_OPTIONS
579 #undef LANG_HOOKS_PARSE_FILE
580 #undef LANG_HOOKS_TYPE_FOR_MODE
581 #undef LANG_HOOKS_TYPE_FOR_SIZE
582 #undef LANG_HOOKS_BUILTIN_FUNCTION
583 #undef LANG_HOOKS_GLOBAL_BINDINGS_P
584 #undef LANG_HOOKS_PUSHDECL
585 #undef LANG_HOOKS_GETDECLS
586 #undef LANG_HOOKS_GIMPLIFY_EXPR
587 #undef LANG_HOOKS_EH_PERSONALITY
589 #define LANG_HOOKS_NAME "GNU Go"
590 #define LANG_HOOKS_INIT go_langhook_init
591 #define LANG_HOOKS_OPTION_LANG_MASK go_langhook_option_lang_mask
592 #define LANG_HOOKS_INIT_OPTIONS_STRUCT go_langhook_init_options_struct
593 #define LANG_HOOKS_HANDLE_OPTION go_langhook_handle_option
594 #define LANG_HOOKS_POST_OPTIONS go_langhook_post_options
595 #define LANG_HOOKS_PARSE_FILE go_langhook_parse_file
596 #define LANG_HOOKS_TYPE_FOR_MODE go_langhook_type_for_mode
597 #define LANG_HOOKS_TYPE_FOR_SIZE go_langhook_type_for_size
598 #define LANG_HOOKS_BUILTIN_FUNCTION go_langhook_builtin_function
599 #define LANG_HOOKS_GLOBAL_BINDINGS_P go_langhook_global_bindings_p
600 #define LANG_HOOKS_PUSHDECL go_langhook_pushdecl
601 #define LANG_HOOKS_GETDECLS go_langhook_getdecls
602 #define LANG_HOOKS_GIMPLIFY_EXPR go_langhook_gimplify_expr
603 #define LANG_HOOKS_EH_PERSONALITY go_langhook_eh_personality
605 struct lang_hooks lang_hooks
= LANG_HOOKS_INITIALIZER
;
607 #include "gt-go-go-lang.h"
608 #include "gtype-go.h"