1 /* go-lang.c -- Go frontend gcc interface.
2 Copyright (C) 2009-2014 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/>. */
23 #include "coretypes.h"
31 #include "hard-reg-set.h"
34 #include "gimple-expr.h"
36 #include "stor-layout.h"
42 #include "diagnostic.h"
43 #include "langhooks.h"
44 #include "langhooks-def.h"
46 #include "common/common-target.h"
52 /* Language-dependent contents of a type. */
54 struct GTY(()) lang_type
59 /* Language-dependent contents of a decl. */
61 struct GTY(()) lang_decl
66 /* Language-dependent contents of an identifier. This must include a
69 struct GTY(()) lang_identifier
71 struct tree_identifier common
;
74 /* The resulting tree type. */
76 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
77 chain_next ("CODE_CONTAINS_STRUCT (TREE_CODE (&%h.generic), TS_COMMON) ? ((union lang_tree_node *) TREE_CHAIN (&%h.generic)) : NULL")))
80 union tree_node
GTY((tag ("0"),
81 desc ("tree_node_structure (&%h)"))) generic
;
82 struct lang_identifier
GTY((tag ("1"))) identifier
;
85 /* We don't use language_function. */
87 struct GTY(()) language_function
92 /* Option information we need to pass to go_create_gogo. */
94 static const char *go_pkgpath
= NULL
;
95 static const char *go_prefix
= NULL
;
96 static const char *go_relative_import_path
= NULL
;
101 go_langhook_init (void)
103 build_common_tree_nodes (false, false);
105 /* I don't know why this has to be done explicitly. */
106 void_list_node
= build_tree_list (NULL_TREE
, void_type_node
);
108 /* We must create the gogo IR after calling build_common_tree_nodes
109 (because Gogo::define_builtin_function_trees refers indirectly
110 to, e.g., unsigned_char_type_node) but before calling
111 build_common_builtin_nodes (because it calls, indirectly,
112 go_type_for_size). */
113 go_create_gogo (INT_TYPE_SIZE
, POINTER_SIZE
, go_pkgpath
, go_prefix
,
114 go_relative_import_path
, go_check_divide_zero
,
115 go_check_divide_overflow
);
117 build_common_builtin_nodes ();
119 /* The default precision for floating point numbers. This is used
120 for floating point constants with abstract type. This may
121 eventually be controllable by a command line option. */
122 mpfr_set_default_prec (256);
124 /* Go uses exceptions. */
125 using_eh_for_cleanups ();
130 /* The option mask. */
133 go_langhook_option_lang_mask (void)
138 /* Initialize the options structure. */
141 go_langhook_init_options_struct (struct gcc_options
*opts
)
143 /* Go says that signed overflow is precisely defined. */
144 opts
->x_flag_wrapv
= 1;
146 /* We default to using strict aliasing, since Go pointers are safe.
147 This is turned off for code that imports the "unsafe" package,
148 because using unsafe.pointer violates C style aliasing
150 opts
->x_flag_strict_aliasing
= 1;
152 /* Default to avoiding range issues for complex multiply and
154 opts
->x_flag_complex_method
= 2;
156 /* The builtin math functions should not set errno. */
157 opts
->x_flag_errno_math
= 0;
158 opts
->frontend_set_flag_errno_math
= true;
160 /* We turn on stack splitting if we can. */
161 if (targetm_common
.supports_split_stack (false, opts
))
162 opts
->x_flag_split_stack
= 1;
164 /* Exceptions are used to handle recovering from panics. */
165 opts
->x_flag_exceptions
= 1;
166 opts
->x_flag_non_call_exceptions
= 1;
169 /* Infrastructure for a vector of char * pointers. */
171 typedef const char *go_char_p
;
173 /* The list of directories to search after all the Go specific
174 directories have been searched. */
176 static vec
<go_char_p
> go_search_dirs
;
178 /* Handle Go specific options. Return 0 if we didn't do anything. */
181 go_langhook_handle_option (
184 int value ATTRIBUTE_UNUSED
,
185 int kind ATTRIBUTE_UNUSED
,
186 location_t loc ATTRIBUTE_UNUSED
,
187 const struct cl_option_handlers
*handlers ATTRIBUTE_UNUSED
)
189 enum opt_code code
= (enum opt_code
) scode
;
195 go_add_search_path (arg
);
199 /* A -L option is assumed to come from the compiler driver.
200 This is a system directory. We search the following
201 directories, if they exist, before this one:
203 dir/go/VERSION/MACHINE
204 This is like include/c++. */
206 static const char dir_separator_str
[] = { DIR_SEPARATOR
, 0 };
212 p
= XALLOCAVEC (char,
213 (len
+ sizeof "go" + sizeof DEFAULT_TARGET_VERSION
214 + sizeof DEFAULT_TARGET_MACHINE
+ 3));
216 if (len
> 0 && !IS_DIR_SEPARATOR (p
[len
- 1]))
217 strcat (p
, dir_separator_str
);
219 strcat (p
, dir_separator_str
);
220 strcat (p
, DEFAULT_TARGET_VERSION
);
221 if (stat (p
, &st
) == 0 && S_ISDIR (st
.st_mode
))
223 go_add_search_path (p
);
224 strcat (p
, dir_separator_str
);
225 strcat (p
, DEFAULT_TARGET_MACHINE
);
226 if (stat (p
, &st
) == 0 && S_ISDIR (st
.st_mode
))
227 go_add_search_path (p
);
230 /* Search ARG too, but only after we've searched to Go
231 specific directories for all -L arguments. */
232 go_search_dirs
.safe_push (arg
);
237 ret
= go_enable_dump (arg
) ? true : false;
240 case OPT_fgo_optimize_
:
241 ret
= go_enable_optimize (arg
) ? true : false;
244 case OPT_fgo_pkgpath_
:
248 case OPT_fgo_prefix_
:
252 case OPT_fgo_relative_import_path_
:
253 go_relative_import_path
= arg
;
257 /* Just return 1 to indicate that the option is valid. */
264 /* Run after parsing options. */
267 go_langhook_post_options (const char **pfilename ATTRIBUTE_UNUSED
)
272 gcc_assert (num_in_fnames
> 0);
274 FOR_EACH_VEC_ELT (go_search_dirs
, ix
, dir
)
275 go_add_search_path (dir
);
276 go_search_dirs
.release ();
278 if (flag_excess_precision_cmdline
== EXCESS_PRECISION_DEFAULT
)
279 flag_excess_precision_cmdline
= EXCESS_PRECISION_STANDARD
;
281 /* Tail call optimizations can confuse uses of runtime.Callers. */
282 if (!global_options_set
.x_flag_optimize_sibling_calls
)
283 global_options
.x_flag_optimize_sibling_calls
= 0;
285 /* Returning false means that the backend should be used. */
290 go_langhook_parse_file (void)
292 go_parse_input_files (in_fnames
, num_in_fnames
, flag_syntax_only
,
293 go_require_return_statement
);
297 go_langhook_type_for_size (unsigned int bits
, int unsignedp
)
302 if (bits
== INT_TYPE_SIZE
)
303 type
= unsigned_type_node
;
304 else if (bits
== CHAR_TYPE_SIZE
)
305 type
= unsigned_char_type_node
;
306 else if (bits
== SHORT_TYPE_SIZE
)
307 type
= short_unsigned_type_node
;
308 else if (bits
== LONG_TYPE_SIZE
)
309 type
= long_unsigned_type_node
;
310 else if (bits
== LONG_LONG_TYPE_SIZE
)
311 type
= long_long_unsigned_type_node
;
313 type
= make_unsigned_type(bits
);
317 if (bits
== INT_TYPE_SIZE
)
318 type
= integer_type_node
;
319 else if (bits
== CHAR_TYPE_SIZE
)
320 type
= signed_char_type_node
;
321 else if (bits
== SHORT_TYPE_SIZE
)
322 type
= short_integer_type_node
;
323 else if (bits
== LONG_TYPE_SIZE
)
324 type
= long_integer_type_node
;
325 else if (bits
== LONG_LONG_TYPE_SIZE
)
326 type
= long_long_integer_type_node
;
328 type
= make_signed_type(bits
);
334 go_langhook_type_for_mode (machine_mode mode
, int unsignedp
)
337 /* Go has no vector types. Build them here. FIXME: It does not
338 make sense for the middle-end to ask the frontend for a type
339 which the frontend does not support. However, at least for now
340 it is required. See PR 46805. */
341 if (VECTOR_MODE_P (mode
))
345 inner
= go_langhook_type_for_mode (GET_MODE_INNER (mode
), unsignedp
);
346 if (inner
!= NULL_TREE
)
347 return build_vector_type_for_mode (inner
, mode
);
351 // FIXME: This static_cast should be in machmode.h.
352 enum mode_class mc
= static_cast<enum mode_class
>(GET_MODE_CLASS(mode
));
354 return go_langhook_type_for_size(GET_MODE_BITSIZE(mode
), unsignedp
);
355 else if (mc
== MODE_FLOAT
)
357 switch (GET_MODE_BITSIZE (mode
))
360 return float_type_node
;
362 return double_type_node
;
364 // We have to check for long double in order to support
365 // i386 excess precision.
366 if (mode
== TYPE_MODE(long_double_type_node
))
367 return long_double_type_node
;
370 else if (mc
== MODE_COMPLEX_FLOAT
)
372 switch (GET_MODE_BITSIZE (mode
))
375 return complex_float_type_node
;
377 return complex_double_type_node
;
379 // We have to check for long double in order to support
380 // i386 excess precision.
381 if (mode
== TYPE_MODE(complex_long_double_type_node
))
382 return complex_long_double_type_node
;
386 #if HOST_BITS_PER_WIDE_INT >= 64
387 /* The middle-end and some backends rely on TImode being supported
391 type
= build_nonstandard_integer_type (GET_MODE_BITSIZE (TImode
),
393 if (type
&& TYPE_MODE (type
) == TImode
)
400 /* Record a builtin function. We just ignore builtin functions. */
403 go_langhook_builtin_function (tree decl
)
408 /* Return true if we are in the global binding level. */
411 go_langhook_global_bindings_p (void)
413 return current_function_decl
== NULL_TREE
;
416 /* Push a declaration into the current binding level. We can't
417 usefully implement this since we don't want to convert from tree
418 back to one of our internal data structures. I think the only way
419 this is used is to record a decl which is to be returned by
420 getdecls, and we could implement it for that purpose if
424 go_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED
)
429 /* This hook is used to get the current list of declarations as trees.
430 We don't support that; instead we use the write_globals hook. This
431 can't simply crash because it is called by -gstabs. */
434 go_langhook_getdecls (void)
439 /* Write out globals. */
442 go_langhook_write_globals (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_WRITE_GLOBALS
545 #undef LANG_HOOKS_GIMPLIFY_EXPR
546 #undef LANG_HOOKS_EH_PERSONALITY
548 #define LANG_HOOKS_NAME "GNU Go"
549 #define LANG_HOOKS_INIT go_langhook_init
550 #define LANG_HOOKS_OPTION_LANG_MASK go_langhook_option_lang_mask
551 #define LANG_HOOKS_INIT_OPTIONS_STRUCT go_langhook_init_options_struct
552 #define LANG_HOOKS_HANDLE_OPTION go_langhook_handle_option
553 #define LANG_HOOKS_POST_OPTIONS go_langhook_post_options
554 #define LANG_HOOKS_PARSE_FILE go_langhook_parse_file
555 #define LANG_HOOKS_TYPE_FOR_MODE go_langhook_type_for_mode
556 #define LANG_HOOKS_TYPE_FOR_SIZE go_langhook_type_for_size
557 #define LANG_HOOKS_BUILTIN_FUNCTION go_langhook_builtin_function
558 #define LANG_HOOKS_GLOBAL_BINDINGS_P go_langhook_global_bindings_p
559 #define LANG_HOOKS_PUSHDECL go_langhook_pushdecl
560 #define LANG_HOOKS_GETDECLS go_langhook_getdecls
561 #define LANG_HOOKS_WRITE_GLOBALS go_langhook_write_globals
562 #define LANG_HOOKS_GIMPLIFY_EXPR go_langhook_gimplify_expr
563 #define LANG_HOOKS_EH_PERSONALITY go_langhook_eh_personality
565 struct lang_hooks lang_hooks
= LANG_HOOKS_INITIALIZER
;
567 #include "gt-go-go-lang.h"
568 #include "gtype-go.h"