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);
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 /* We need to keep pointers live for the garbage collector. */
154 opts
->x_flag_keep_gc_roots_live
= 1;
156 /* Go programs expect runtime.Callers to work, and that uses
157 libbacktrace that uses debug info. Set the debug info level to 1
158 by default. In post_options we will set the debug type if the
159 debug info level was not set back to 0 on the command line. */
160 opts
->x_debug_info_level
= DINFO_LEVEL_TERSE
;
163 /* Infrastructure for a vector of char * pointers. */
165 typedef const char *go_char_p
;
167 /* The list of directories to search after all the Go specific
168 directories have been searched. */
170 static vec
<go_char_p
> go_search_dirs
;
172 /* Handle Go specific options. Return 0 if we didn't do anything. */
175 go_langhook_handle_option (
178 int value ATTRIBUTE_UNUSED
,
179 int kind ATTRIBUTE_UNUSED
,
180 location_t loc ATTRIBUTE_UNUSED
,
181 const struct cl_option_handlers
*handlers ATTRIBUTE_UNUSED
)
183 enum opt_code code
= (enum opt_code
) scode
;
189 go_add_search_path (arg
);
193 /* A -L option is assumed to come from the compiler driver.
194 This is a system directory. We search the following
195 directories, if they exist, before this one:
197 dir/go/VERSION/MACHINE
198 This is like include/c++. */
200 static const char dir_separator_str
[] = { DIR_SEPARATOR
, 0 };
206 p
= XALLOCAVEC (char,
207 (len
+ sizeof "go" + sizeof DEFAULT_TARGET_VERSION
208 + sizeof DEFAULT_TARGET_MACHINE
+ 3));
210 if (len
> 0 && !IS_DIR_SEPARATOR (p
[len
- 1]))
211 strcat (p
, dir_separator_str
);
213 strcat (p
, dir_separator_str
);
214 strcat (p
, DEFAULT_TARGET_VERSION
);
215 if (stat (p
, &st
) == 0 && S_ISDIR (st
.st_mode
))
217 go_add_search_path (p
);
218 strcat (p
, dir_separator_str
);
219 strcat (p
, DEFAULT_TARGET_MACHINE
);
220 if (stat (p
, &st
) == 0 && S_ISDIR (st
.st_mode
))
221 go_add_search_path (p
);
224 /* Search ARG too, but only after we've searched to Go
225 specific directories for all -L arguments. */
226 go_search_dirs
.safe_push (arg
);
231 ret
= go_enable_dump (arg
) ? true : false;
234 case OPT_fgo_optimize_
:
235 ret
= go_enable_optimize (arg
) ? true : false;
238 case OPT_fgo_pkgpath_
:
242 case OPT_fgo_prefix_
:
246 case OPT_fgo_relative_import_path_
:
247 go_relative_import_path
= arg
;
251 /* Just return 1 to indicate that the option is valid. */
258 /* Run after parsing options. */
261 go_langhook_post_options (const char **pfilename ATTRIBUTE_UNUSED
)
266 gcc_assert (num_in_fnames
> 0);
268 FOR_EACH_VEC_ELT (go_search_dirs
, ix
, dir
)
269 go_add_search_path (dir
);
270 go_search_dirs
.release ();
272 if (flag_excess_precision_cmdline
== EXCESS_PRECISION_DEFAULT
)
273 flag_excess_precision_cmdline
= EXCESS_PRECISION_STANDARD
;
275 /* Tail call optimizations can confuse uses of runtime.Callers. */
276 if (!global_options_set
.x_flag_optimize_sibling_calls
)
277 global_options
.x_flag_optimize_sibling_calls
= 0;
279 /* If the debug info level is still 1, as set in init_options, make
280 sure that some debugging type is selected. */
281 if (global_options
.x_debug_info_level
== DINFO_LEVEL_TERSE
282 && global_options
.x_write_symbols
== NO_DEBUG
)
283 global_options
.x_write_symbols
= PREFERRED_DEBUGGING_TYPE
;
285 /* We turn on stack splitting if we can. */
286 if (!global_options_set
.x_flag_split_stack
287 && targetm_common
.supports_split_stack (false, &global_options
))
288 global_options
.x_flag_split_stack
= 1;
290 /* Returning false means that the backend should be used. */
295 go_langhook_parse_file (void)
297 go_parse_input_files (in_fnames
, num_in_fnames
, flag_syntax_only
,
298 go_require_return_statement
);
300 /* Final processing of globals and early debug info generation. */
305 go_langhook_type_for_size (unsigned int bits
, int unsignedp
)
310 if (bits
== INT_TYPE_SIZE
)
311 type
= unsigned_type_node
;
312 else if (bits
== CHAR_TYPE_SIZE
)
313 type
= unsigned_char_type_node
;
314 else if (bits
== SHORT_TYPE_SIZE
)
315 type
= short_unsigned_type_node
;
316 else if (bits
== LONG_TYPE_SIZE
)
317 type
= long_unsigned_type_node
;
318 else if (bits
== LONG_LONG_TYPE_SIZE
)
319 type
= long_long_unsigned_type_node
;
321 type
= make_unsigned_type(bits
);
325 if (bits
== INT_TYPE_SIZE
)
326 type
= integer_type_node
;
327 else if (bits
== CHAR_TYPE_SIZE
)
328 type
= signed_char_type_node
;
329 else if (bits
== SHORT_TYPE_SIZE
)
330 type
= short_integer_type_node
;
331 else if (bits
== LONG_TYPE_SIZE
)
332 type
= long_integer_type_node
;
333 else if (bits
== LONG_LONG_TYPE_SIZE
)
334 type
= long_long_integer_type_node
;
336 type
= make_signed_type(bits
);
342 go_langhook_type_for_mode (machine_mode mode
, int unsignedp
)
345 /* Go has no vector types. Build them here. FIXME: It does not
346 make sense for the middle-end to ask the frontend for a type
347 which the frontend does not support. However, at least for now
348 it is required. See PR 46805. */
349 if (VECTOR_MODE_P (mode
))
353 inner
= go_langhook_type_for_mode (GET_MODE_INNER (mode
), unsignedp
);
354 if (inner
!= NULL_TREE
)
355 return build_vector_type_for_mode (inner
, mode
);
359 // FIXME: This static_cast should be in machmode.h.
360 enum mode_class mc
= static_cast<enum mode_class
>(GET_MODE_CLASS(mode
));
362 return go_langhook_type_for_size(GET_MODE_BITSIZE(mode
), unsignedp
);
363 else if (mc
== MODE_FLOAT
)
365 switch (GET_MODE_BITSIZE (mode
))
368 return float_type_node
;
370 return double_type_node
;
372 // We have to check for long double in order to support
373 // i386 excess precision.
374 if (mode
== TYPE_MODE(long_double_type_node
))
375 return long_double_type_node
;
378 else if (mc
== MODE_COMPLEX_FLOAT
)
380 switch (GET_MODE_BITSIZE (mode
))
383 return complex_float_type_node
;
385 return complex_double_type_node
;
387 // We have to check for long double in order to support
388 // i386 excess precision.
389 if (mode
== TYPE_MODE(complex_long_double_type_node
))
390 return complex_long_double_type_node
;
394 #if HOST_BITS_PER_WIDE_INT >= 64
395 /* The middle-end and some backends rely on TImode being supported
399 type
= build_nonstandard_integer_type (GET_MODE_BITSIZE (TImode
),
401 if (type
&& TYPE_MODE (type
) == TImode
)
408 /* Record a builtin function. We just ignore builtin functions. */
411 go_langhook_builtin_function (tree decl
)
416 /* Return true if we are in the global binding level. */
419 go_langhook_global_bindings_p (void)
421 return current_function_decl
== NULL_TREE
;
424 /* Push a declaration into the current binding level. We can't
425 usefully implement this since we don't want to convert from tree
426 back to one of our internal data structures. I think the only way
427 this is used is to record a decl which is to be returned by
428 getdecls, and we could implement it for that purpose if
432 go_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED
)
437 /* This hook is used to get the current list of declarations as trees.
438 We don't support that; instead we use the write_globals hook. This
439 can't simply crash because it is called by -gstabs. */
442 go_langhook_getdecls (void)
447 /* Go specific gimplification. We need to gimplify
448 CALL_EXPR_STATIC_CHAIN, because the gimplifier doesn't handle
452 go_langhook_gimplify_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
)
454 if (TREE_CODE (*expr_p
) == CALL_EXPR
455 && CALL_EXPR_STATIC_CHAIN (*expr_p
) != NULL_TREE
)
456 gimplify_expr (&CALL_EXPR_STATIC_CHAIN (*expr_p
), pre_p
, post_p
,
457 is_gimple_val
, fb_rvalue
);
461 /* Return a decl for the exception personality function. The function
462 itself is implemented in libgo/runtime/go-unwind.c. */
465 go_langhook_eh_personality (void)
467 static tree personality_decl
;
468 if (personality_decl
== NULL_TREE
)
470 personality_decl
= build_personality_function ("gccgo");
471 go_preserve_from_gc (personality_decl
);
473 return personality_decl
;
476 /* Functions called directly by the generic backend. */
479 convert (tree type
, tree expr
)
481 if (type
== error_mark_node
482 || expr
== error_mark_node
483 || TREE_TYPE (expr
) == error_mark_node
)
484 return error_mark_node
;
486 if (type
== TREE_TYPE (expr
))
489 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (TREE_TYPE (expr
)))
490 return fold_convert (type
, expr
);
492 switch (TREE_CODE (type
))
496 return fold_convert (type
, expr
);
498 return fold (convert_to_integer (type
, expr
));
500 return fold (convert_to_pointer (type
, expr
));
502 return fold (convert_to_real (type
, expr
));
504 return fold (convert_to_complex (type
, expr
));
512 /* FIXME: This is a hack to preserve trees that we create from the
513 garbage collector. */
515 static GTY(()) tree go_gc_root
;
518 go_preserve_from_gc (tree t
)
520 go_gc_root
= tree_cons (NULL_TREE
, t
, go_gc_root
);
523 /* Convert an identifier for use in an error message. */
526 go_localize_identifier (const char *ident
)
528 return identifier_to_locale (ident
);
531 #undef LANG_HOOKS_NAME
532 #undef LANG_HOOKS_INIT
533 #undef LANG_HOOKS_OPTION_LANG_MASK
534 #undef LANG_HOOKS_INIT_OPTIONS_STRUCT
535 #undef LANG_HOOKS_HANDLE_OPTION
536 #undef LANG_HOOKS_POST_OPTIONS
537 #undef LANG_HOOKS_PARSE_FILE
538 #undef LANG_HOOKS_TYPE_FOR_MODE
539 #undef LANG_HOOKS_TYPE_FOR_SIZE
540 #undef LANG_HOOKS_BUILTIN_FUNCTION
541 #undef LANG_HOOKS_GLOBAL_BINDINGS_P
542 #undef LANG_HOOKS_PUSHDECL
543 #undef LANG_HOOKS_GETDECLS
544 #undef LANG_HOOKS_GIMPLIFY_EXPR
545 #undef LANG_HOOKS_EH_PERSONALITY
547 #define LANG_HOOKS_NAME "GNU Go"
548 #define LANG_HOOKS_INIT go_langhook_init
549 #define LANG_HOOKS_OPTION_LANG_MASK go_langhook_option_lang_mask
550 #define LANG_HOOKS_INIT_OPTIONS_STRUCT go_langhook_init_options_struct
551 #define LANG_HOOKS_HANDLE_OPTION go_langhook_handle_option
552 #define LANG_HOOKS_POST_OPTIONS go_langhook_post_options
553 #define LANG_HOOKS_PARSE_FILE go_langhook_parse_file
554 #define LANG_HOOKS_TYPE_FOR_MODE go_langhook_type_for_mode
555 #define LANG_HOOKS_TYPE_FOR_SIZE go_langhook_type_for_size
556 #define LANG_HOOKS_BUILTIN_FUNCTION go_langhook_builtin_function
557 #define LANG_HOOKS_GLOBAL_BINDINGS_P go_langhook_global_bindings_p
558 #define LANG_HOOKS_PUSHDECL go_langhook_pushdecl
559 #define LANG_HOOKS_GETDECLS go_langhook_getdecls
560 #define LANG_HOOKS_GIMPLIFY_EXPR go_langhook_gimplify_expr
561 #define LANG_HOOKS_EH_PERSONALITY go_langhook_eh_personality
563 struct lang_hooks lang_hooks
= LANG_HOOKS_INITIALIZER
;
565 #include "gt-go-go-lang.h"
566 #include "gtype-go.h"