1 /* go-lang.c -- Go frontend gcc interface.
2 Copyright (C) 2009-2016 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"
41 /* Language-dependent contents of a type. */
43 struct GTY(()) lang_type
48 /* Language-dependent contents of a decl. */
50 struct GTY(()) lang_decl
55 /* Language-dependent contents of an identifier. This must include a
58 struct GTY(()) lang_identifier
60 struct tree_identifier common
;
63 /* The resulting tree type. */
65 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
66 chain_next ("CODE_CONTAINS_STRUCT (TREE_CODE (&%h.generic), TS_COMMON) ? ((union lang_tree_node *) TREE_CHAIN (&%h.generic)) : NULL")))
69 union tree_node
GTY((tag ("0"),
70 desc ("tree_node_structure (&%h)"))) generic
;
71 struct lang_identifier
GTY((tag ("1"))) identifier
;
74 /* We don't use language_function. */
76 struct GTY(()) language_function
81 /* Option information we need to pass to go_create_gogo. */
83 static const char *go_pkgpath
= NULL
;
84 static const char *go_prefix
= NULL
;
85 static const char *go_relative_import_path
= NULL
;
90 go_langhook_init (void)
92 build_common_tree_nodes (false, false);
94 /* I don't know why this has to be done explicitly. */
95 void_list_node
= build_tree_list (NULL_TREE
, void_type_node
);
97 /* We must create the gogo IR after calling build_common_tree_nodes
98 (because Gogo::define_builtin_function_trees refers indirectly
99 to, e.g., unsigned_char_type_node) but before calling
100 build_common_builtin_nodes (because it calls, indirectly,
101 go_type_for_size). */
102 go_create_gogo (INT_TYPE_SIZE
, POINTER_SIZE
, go_pkgpath
, go_prefix
,
103 go_relative_import_path
, go_check_divide_zero
,
104 go_check_divide_overflow
);
106 build_common_builtin_nodes ();
108 /* The default precision for floating point numbers. This is used
109 for floating point constants with abstract type. This may
110 eventually be controllable by a command line option. */
111 mpfr_set_default_prec (256);
113 /* Go uses exceptions. */
114 using_eh_for_cleanups ();
119 /* The option mask. */
122 go_langhook_option_lang_mask (void)
127 /* Initialize the options structure. */
130 go_langhook_init_options_struct (struct gcc_options
*opts
)
132 /* Go says that signed overflow is precisely defined. */
133 opts
->x_flag_wrapv
= 1;
135 /* We default to using strict aliasing, since Go pointers are safe.
136 This is turned off for code that imports the "unsafe" package,
137 because using unsafe.pointer violates C style aliasing
139 opts
->x_flag_strict_aliasing
= 1;
141 /* Default to avoiding range issues for complex multiply and
143 opts
->x_flag_complex_method
= 2;
145 /* The builtin math functions should not set errno. */
146 opts
->x_flag_errno_math
= 0;
147 opts
->frontend_set_flag_errno_math
= true;
149 /* Exceptions are used to handle recovering from panics. */
150 opts
->x_flag_exceptions
= 1;
151 opts
->x_flag_non_call_exceptions
= 1;
153 /* Go programs expect runtime.Callers to work, and that uses
154 libbacktrace that uses debug info. Set the debug info level to 1
155 by default. In post_options we will set the debug type if the
156 debug info level was not set back to 0 on the command line. */
157 opts
->x_debug_info_level
= DINFO_LEVEL_TERSE
;
160 /* Infrastructure for a vector of char * pointers. */
162 typedef const char *go_char_p
;
164 /* The list of directories to search after all the Go specific
165 directories have been searched. */
167 static vec
<go_char_p
> go_search_dirs
;
169 /* Handle Go specific options. Return 0 if we didn't do anything. */
172 go_langhook_handle_option (
175 int value ATTRIBUTE_UNUSED
,
176 int kind ATTRIBUTE_UNUSED
,
177 location_t loc ATTRIBUTE_UNUSED
,
178 const struct cl_option_handlers
*handlers ATTRIBUTE_UNUSED
)
180 enum opt_code code
= (enum opt_code
) scode
;
186 go_add_search_path (arg
);
190 /* A -L option is assumed to come from the compiler driver.
191 This is a system directory. We search the following
192 directories, if they exist, before this one:
194 dir/go/VERSION/MACHINE
195 This is like include/c++. */
197 static const char dir_separator_str
[] = { DIR_SEPARATOR
, 0 };
203 p
= XALLOCAVEC (char,
204 (len
+ sizeof "go" + sizeof DEFAULT_TARGET_VERSION
205 + sizeof DEFAULT_TARGET_MACHINE
+ 3));
207 if (len
> 0 && !IS_DIR_SEPARATOR (p
[len
- 1]))
208 strcat (p
, dir_separator_str
);
210 strcat (p
, dir_separator_str
);
211 strcat (p
, DEFAULT_TARGET_VERSION
);
212 if (stat (p
, &st
) == 0 && S_ISDIR (st
.st_mode
))
214 go_add_search_path (p
);
215 strcat (p
, dir_separator_str
);
216 strcat (p
, DEFAULT_TARGET_MACHINE
);
217 if (stat (p
, &st
) == 0 && S_ISDIR (st
.st_mode
))
218 go_add_search_path (p
);
221 /* Search ARG too, but only after we've searched to Go
222 specific directories for all -L arguments. */
223 go_search_dirs
.safe_push (arg
);
228 ret
= go_enable_dump (arg
) ? true : false;
231 case OPT_fgo_optimize_
:
232 ret
= go_enable_optimize (arg
) ? true : false;
235 case OPT_fgo_pkgpath_
:
239 case OPT_fgo_prefix_
:
243 case OPT_fgo_relative_import_path_
:
244 go_relative_import_path
= arg
;
248 /* Just return 1 to indicate that the option is valid. */
255 /* Run after parsing options. */
258 go_langhook_post_options (const char **pfilename ATTRIBUTE_UNUSED
)
263 gcc_assert (num_in_fnames
> 0);
265 FOR_EACH_VEC_ELT (go_search_dirs
, ix
, dir
)
266 go_add_search_path (dir
);
267 go_search_dirs
.release ();
269 if (flag_excess_precision_cmdline
== EXCESS_PRECISION_DEFAULT
)
270 flag_excess_precision_cmdline
= EXCESS_PRECISION_STANDARD
;
272 /* Tail call optimizations can confuse uses of runtime.Callers. */
273 if (!global_options_set
.x_flag_optimize_sibling_calls
)
274 global_options
.x_flag_optimize_sibling_calls
= 0;
276 /* If the debug info level is still 1, as set in init_options, make
277 sure that some debugging type is selected. */
278 if (global_options
.x_debug_info_level
== DINFO_LEVEL_TERSE
279 && global_options
.x_write_symbols
== NO_DEBUG
)
280 global_options
.x_write_symbols
= PREFERRED_DEBUGGING_TYPE
;
282 /* We turn on stack splitting if we can. */
283 if (!global_options_set
.x_flag_split_stack
284 && targetm_common
.supports_split_stack (false, &global_options
))
285 global_options
.x_flag_split_stack
= 1;
287 /* Returning false means that the backend should be used. */
292 go_langhook_parse_file (void)
294 go_parse_input_files (in_fnames
, num_in_fnames
, flag_syntax_only
,
295 go_require_return_statement
);
297 /* Final processing of globals and early debug info generation. */
302 go_langhook_type_for_size (unsigned int bits
, int unsignedp
)
307 if (bits
== INT_TYPE_SIZE
)
308 type
= unsigned_type_node
;
309 else if (bits
== CHAR_TYPE_SIZE
)
310 type
= unsigned_char_type_node
;
311 else if (bits
== SHORT_TYPE_SIZE
)
312 type
= short_unsigned_type_node
;
313 else if (bits
== LONG_TYPE_SIZE
)
314 type
= long_unsigned_type_node
;
315 else if (bits
== LONG_LONG_TYPE_SIZE
)
316 type
= long_long_unsigned_type_node
;
318 type
= make_unsigned_type(bits
);
322 if (bits
== INT_TYPE_SIZE
)
323 type
= integer_type_node
;
324 else if (bits
== CHAR_TYPE_SIZE
)
325 type
= signed_char_type_node
;
326 else if (bits
== SHORT_TYPE_SIZE
)
327 type
= short_integer_type_node
;
328 else if (bits
== LONG_TYPE_SIZE
)
329 type
= long_integer_type_node
;
330 else if (bits
== LONG_LONG_TYPE_SIZE
)
331 type
= long_long_integer_type_node
;
333 type
= make_signed_type(bits
);
339 go_langhook_type_for_mode (machine_mode mode
, int unsignedp
)
342 /* Go has no vector types. Build them here. FIXME: It does not
343 make sense for the middle-end to ask the frontend for a type
344 which the frontend does not support. However, at least for now
345 it is required. See PR 46805. */
346 if (VECTOR_MODE_P (mode
))
350 inner
= go_langhook_type_for_mode (GET_MODE_INNER (mode
), unsignedp
);
351 if (inner
!= NULL_TREE
)
352 return build_vector_type_for_mode (inner
, mode
);
356 // FIXME: This static_cast should be in machmode.h.
357 enum mode_class mc
= static_cast<enum mode_class
>(GET_MODE_CLASS(mode
));
359 return go_langhook_type_for_size(GET_MODE_BITSIZE(mode
), unsignedp
);
360 else if (mc
== MODE_FLOAT
)
362 switch (GET_MODE_BITSIZE (mode
))
365 return float_type_node
;
367 return double_type_node
;
369 // We have to check for long double in order to support
370 // i386 excess precision.
371 if (mode
== TYPE_MODE(long_double_type_node
))
372 return long_double_type_node
;
375 else if (mc
== MODE_COMPLEX_FLOAT
)
377 switch (GET_MODE_BITSIZE (mode
))
380 return complex_float_type_node
;
382 return complex_double_type_node
;
384 // We have to check for long double in order to support
385 // i386 excess precision.
386 if (mode
== TYPE_MODE(complex_long_double_type_node
))
387 return complex_long_double_type_node
;
391 #if HOST_BITS_PER_WIDE_INT >= 64
392 /* The middle-end and some backends rely on TImode being supported
396 type
= build_nonstandard_integer_type (GET_MODE_BITSIZE (TImode
),
398 if (type
&& TYPE_MODE (type
) == TImode
)
405 /* Record a builtin function. We just ignore builtin functions. */
408 go_langhook_builtin_function (tree decl
)
413 /* Return true if we are in the global binding level. */
416 go_langhook_global_bindings_p (void)
418 return current_function_decl
== NULL_TREE
;
421 /* Push a declaration into the current binding level. We can't
422 usefully implement this since we don't want to convert from tree
423 back to one of our internal data structures. I think the only way
424 this is used is to record a decl which is to be returned by
425 getdecls, and we could implement it for that purpose if
429 go_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED
)
434 /* This hook is used to get the current list of declarations as trees.
435 We don't support that; instead we use the write_globals hook. This
436 can't simply crash because it is called by -gstabs. */
439 go_langhook_getdecls (void)
444 /* Go specific gimplification. We need to gimplify
445 CALL_EXPR_STATIC_CHAIN, because the gimplifier doesn't handle
449 go_langhook_gimplify_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
)
451 if (TREE_CODE (*expr_p
) == CALL_EXPR
452 && CALL_EXPR_STATIC_CHAIN (*expr_p
) != NULL_TREE
)
453 gimplify_expr (&CALL_EXPR_STATIC_CHAIN (*expr_p
), pre_p
, post_p
,
454 is_gimple_val
, fb_rvalue
);
458 /* Return a decl for the exception personality function. The function
459 itself is implemented in libgo/runtime/go-unwind.c. */
462 go_langhook_eh_personality (void)
464 static tree personality_decl
;
465 if (personality_decl
== NULL_TREE
)
467 personality_decl
= build_personality_function ("gccgo");
468 go_preserve_from_gc (personality_decl
);
470 return personality_decl
;
473 /* Functions called directly by the generic backend. */
476 convert (tree type
, tree expr
)
478 if (type
== error_mark_node
479 || expr
== error_mark_node
480 || TREE_TYPE (expr
) == error_mark_node
)
481 return error_mark_node
;
483 if (type
== TREE_TYPE (expr
))
486 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (TREE_TYPE (expr
)))
487 return fold_convert (type
, expr
);
489 switch (TREE_CODE (type
))
493 return fold_convert (type
, expr
);
495 return fold (convert_to_integer (type
, expr
));
497 return fold (convert_to_pointer (type
, expr
));
499 return fold (convert_to_real (type
, expr
));
501 return fold (convert_to_complex (type
, expr
));
509 /* FIXME: This is a hack to preserve trees that we create from the
510 garbage collector. */
512 static GTY(()) tree go_gc_root
;
515 go_preserve_from_gc (tree t
)
517 go_gc_root
= tree_cons (NULL_TREE
, t
, go_gc_root
);
520 /* Convert an identifier for use in an error message. */
523 go_localize_identifier (const char *ident
)
525 return identifier_to_locale (ident
);
528 #undef LANG_HOOKS_NAME
529 #undef LANG_HOOKS_INIT
530 #undef LANG_HOOKS_OPTION_LANG_MASK
531 #undef LANG_HOOKS_INIT_OPTIONS_STRUCT
532 #undef LANG_HOOKS_HANDLE_OPTION
533 #undef LANG_HOOKS_POST_OPTIONS
534 #undef LANG_HOOKS_PARSE_FILE
535 #undef LANG_HOOKS_TYPE_FOR_MODE
536 #undef LANG_HOOKS_TYPE_FOR_SIZE
537 #undef LANG_HOOKS_BUILTIN_FUNCTION
538 #undef LANG_HOOKS_GLOBAL_BINDINGS_P
539 #undef LANG_HOOKS_PUSHDECL
540 #undef LANG_HOOKS_GETDECLS
541 #undef LANG_HOOKS_GIMPLIFY_EXPR
542 #undef LANG_HOOKS_EH_PERSONALITY
544 #define LANG_HOOKS_NAME "GNU Go"
545 #define LANG_HOOKS_INIT go_langhook_init
546 #define LANG_HOOKS_OPTION_LANG_MASK go_langhook_option_lang_mask
547 #define LANG_HOOKS_INIT_OPTIONS_STRUCT go_langhook_init_options_struct
548 #define LANG_HOOKS_HANDLE_OPTION go_langhook_handle_option
549 #define LANG_HOOKS_POST_OPTIONS go_langhook_post_options
550 #define LANG_HOOKS_PARSE_FILE go_langhook_parse_file
551 #define LANG_HOOKS_TYPE_FOR_MODE go_langhook_type_for_mode
552 #define LANG_HOOKS_TYPE_FOR_SIZE go_langhook_type_for_size
553 #define LANG_HOOKS_BUILTIN_FUNCTION go_langhook_builtin_function
554 #define LANG_HOOKS_GLOBAL_BINDINGS_P go_langhook_global_bindings_p
555 #define LANG_HOOKS_PUSHDECL go_langhook_pushdecl
556 #define LANG_HOOKS_GETDECLS go_langhook_getdecls
557 #define LANG_HOOKS_GIMPLIFY_EXPR go_langhook_gimplify_expr
558 #define LANG_HOOKS_EH_PERSONALITY go_langhook_eh_personality
560 struct lang_hooks lang_hooks
= LANG_HOOKS_INITIALIZER
;
562 #include "gt-go-go-lang.h"
563 #include "gtype-go.h"