1 /* go-lang.c -- Go frontend gcc interface.
2 Copyright (C) 2009-2013 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"
33 #include "diagnostic.h"
34 #include "langhooks.h"
35 #include "langhooks-def.h"
38 #include "common/common-target.h"
44 /* Language-dependent contents of a type. */
46 struct GTY(()) lang_type
51 /* Language-dependent contents of a decl. */
53 struct GTY((variable_size
)) lang_decl
58 /* Language-dependent contents of an identifier. This must include a
61 struct GTY(()) lang_identifier
63 struct tree_identifier common
;
66 /* The resulting tree type. */
68 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
69 chain_next ("CODE_CONTAINS_STRUCT (TREE_CODE (&%h.generic), TS_COMMON) ? ((union lang_tree_node *) TREE_CHAIN (&%h.generic)) : NULL")))
72 union tree_node
GTY((tag ("0"),
73 desc ("tree_node_structure (&%h)"))) generic
;
74 struct lang_identifier
GTY((tag ("1"))) identifier
;
77 /* We don't use language_function. */
79 struct GTY(()) language_function
84 /* Option information we need to pass to go_create_gogo. */
86 static const char *go_pkgpath
= NULL
;
87 static const char *go_prefix
= NULL
;
88 static const char *go_relative_import_path
= NULL
;
93 go_langhook_init (void)
95 build_common_tree_nodes (false, false);
97 /* I don't know why this has to be done explicitly. */
98 void_list_node
= build_tree_list (NULL_TREE
, void_type_node
);
100 /* We must create the gogo IR after calling build_common_tree_nodes
101 (because Gogo::define_builtin_function_trees refers indirectly
102 to, e.g., unsigned_char_type_node) but before calling
103 build_common_builtin_nodes (because it calls, indirectly,
104 go_type_for_size). */
105 go_create_gogo (INT_TYPE_SIZE
, POINTER_SIZE
, go_pkgpath
, go_prefix
,
106 go_relative_import_path
);
108 build_common_builtin_nodes ();
110 /* The default precision for floating point numbers. This is used
111 for floating point constants with abstract type. This may
112 eventually be controllable by a command line option. */
113 mpfr_set_default_prec (256);
115 /* Go uses exceptions. */
116 using_eh_for_cleanups ();
121 /* The option mask. */
124 go_langhook_option_lang_mask (void)
129 /* Initialize the options structure. */
132 go_langhook_init_options_struct (struct gcc_options
*opts
)
134 /* Go says that signed overflow is precisely defined. */
135 opts
->x_flag_wrapv
= 1;
137 /* We default to using strict aliasing, since Go pointers are safe.
138 This is turned off for code that imports the "unsafe" package,
139 because using unsafe.pointer violates C style aliasing
141 opts
->x_flag_strict_aliasing
= 1;
143 /* Default to avoiding range issues for complex multiply and
145 opts
->x_flag_complex_method
= 2;
147 /* The builtin math functions should not set errno. */
148 opts
->x_flag_errno_math
= 0;
149 opts
->frontend_set_flag_errno_math
= true;
151 /* We turn on stack splitting if we can. */
152 if (targetm_common
.supports_split_stack (false, opts
))
153 opts
->x_flag_split_stack
= 1;
155 /* Exceptions are used to handle recovering from panics. */
156 opts
->x_flag_exceptions
= 1;
157 opts
->x_flag_non_call_exceptions
= 1;
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 /* Returning false means that the backend should be used. */
277 go_langhook_parse_file (void)
279 go_parse_input_files (in_fnames
, num_in_fnames
, flag_syntax_only
,
280 go_require_return_statement
);
284 go_langhook_type_for_size (unsigned int bits
, int unsignedp
)
286 return go_type_for_size (bits
, unsignedp
);
290 go_langhook_type_for_mode (enum machine_mode mode
, int unsignedp
)
293 /* Go has no vector types. Build them here. FIXME: It does not
294 make sense for the middle-end to ask the frontend for a type
295 which the frontend does not support. However, at least for now
296 it is required. See PR 46805. */
297 if (VECTOR_MODE_P (mode
))
301 inner
= go_langhook_type_for_mode (GET_MODE_INNER (mode
), unsignedp
);
302 if (inner
!= NULL_TREE
)
303 return build_vector_type_for_mode (inner
, mode
);
307 type
= go_type_for_mode (mode
, unsignedp
);
311 #if HOST_BITS_PER_WIDE_INT >= 64
312 /* The middle-end and some backends rely on TImode being supported
316 type
= build_nonstandard_integer_type (GET_MODE_BITSIZE (TImode
),
318 if (type
&& TYPE_MODE (type
) == TImode
)
325 /* Record a builtin function. We just ignore builtin functions. */
328 go_langhook_builtin_function (tree decl
)
333 /* Return true if we are in the global binding level. */
336 go_langhook_global_bindings_p (void)
338 return current_function_decl
== NULL_TREE
;
341 /* Push a declaration into the current binding level. We can't
342 usefully implement this since we don't want to convert from tree
343 back to one of our internal data structures. I think the only way
344 this is used is to record a decl which is to be returned by
345 getdecls, and we could implement it for that purpose if
349 go_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED
)
354 /* This hook is used to get the current list of declarations as trees.
355 We don't support that; instead we use the write_globals hook. This
356 can't simply crash because it is called by -gstabs. */
359 go_langhook_getdecls (void)
364 /* Write out globals. */
367 go_langhook_write_globals (void)
372 /* Go specific gimplification. We need to gimplify
373 CALL_EXPR_STATIC_CHAIN, because the gimplifier doesn't handle
377 go_langhook_gimplify_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
)
379 if (TREE_CODE (*expr_p
) == CALL_EXPR
380 && CALL_EXPR_STATIC_CHAIN (*expr_p
) != NULL_TREE
)
381 gimplify_expr (&CALL_EXPR_STATIC_CHAIN (*expr_p
), pre_p
, post_p
,
382 is_gimple_val
, fb_rvalue
);
386 /* Return a decl for the exception personality function. The function
387 itself is implemented in libgo/runtime/go-unwind.c. */
390 go_langhook_eh_personality (void)
392 static tree personality_decl
;
393 if (personality_decl
== NULL_TREE
)
395 personality_decl
= build_personality_function ("gccgo");
396 go_preserve_from_gc (personality_decl
);
398 return personality_decl
;
401 /* Functions called directly by the generic backend. */
404 convert (tree type
, tree expr
)
406 if (type
== error_mark_node
407 || expr
== error_mark_node
408 || TREE_TYPE (expr
) == error_mark_node
)
409 return error_mark_node
;
411 if (type
== TREE_TYPE (expr
))
414 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (TREE_TYPE (expr
)))
415 return fold_convert (type
, expr
);
417 switch (TREE_CODE (type
))
421 return fold_convert (type
, expr
);
423 return fold (convert_to_integer (type
, expr
));
425 return fold (convert_to_pointer (type
, expr
));
427 return fold (convert_to_real (type
, expr
));
429 return fold (convert_to_complex (type
, expr
));
437 /* FIXME: This is a hack to preserve trees that we create from the
438 garbage collector. */
440 static GTY(()) tree go_gc_root
;
443 go_preserve_from_gc (tree t
)
445 go_gc_root
= tree_cons (NULL_TREE
, t
, go_gc_root
);
448 /* Convert an identifier for use in an error message. */
451 go_localize_identifier (const char *ident
)
453 return identifier_to_locale (ident
);
456 #undef LANG_HOOKS_NAME
457 #undef LANG_HOOKS_INIT
458 #undef LANG_HOOKS_OPTION_LANG_MASK
459 #undef LANG_HOOKS_INIT_OPTIONS_STRUCT
460 #undef LANG_HOOKS_HANDLE_OPTION
461 #undef LANG_HOOKS_POST_OPTIONS
462 #undef LANG_HOOKS_PARSE_FILE
463 #undef LANG_HOOKS_TYPE_FOR_MODE
464 #undef LANG_HOOKS_TYPE_FOR_SIZE
465 #undef LANG_HOOKS_BUILTIN_FUNCTION
466 #undef LANG_HOOKS_GLOBAL_BINDINGS_P
467 #undef LANG_HOOKS_PUSHDECL
468 #undef LANG_HOOKS_GETDECLS
469 #undef LANG_HOOKS_WRITE_GLOBALS
470 #undef LANG_HOOKS_GIMPLIFY_EXPR
471 #undef LANG_HOOKS_EH_PERSONALITY
473 #define LANG_HOOKS_NAME "GNU Go"
474 #define LANG_HOOKS_INIT go_langhook_init
475 #define LANG_HOOKS_OPTION_LANG_MASK go_langhook_option_lang_mask
476 #define LANG_HOOKS_INIT_OPTIONS_STRUCT go_langhook_init_options_struct
477 #define LANG_HOOKS_HANDLE_OPTION go_langhook_handle_option
478 #define LANG_HOOKS_POST_OPTIONS go_langhook_post_options
479 #define LANG_HOOKS_PARSE_FILE go_langhook_parse_file
480 #define LANG_HOOKS_TYPE_FOR_MODE go_langhook_type_for_mode
481 #define LANG_HOOKS_TYPE_FOR_SIZE go_langhook_type_for_size
482 #define LANG_HOOKS_BUILTIN_FUNCTION go_langhook_builtin_function
483 #define LANG_HOOKS_GLOBAL_BINDINGS_P go_langhook_global_bindings_p
484 #define LANG_HOOKS_PUSHDECL go_langhook_pushdecl
485 #define LANG_HOOKS_GETDECLS go_langhook_getdecls
486 #define LANG_HOOKS_WRITE_GLOBALS go_langhook_write_globals
487 #define LANG_HOOKS_GIMPLIFY_EXPR go_langhook_gimplify_expr
488 #define LANG_HOOKS_EH_PERSONALITY go_langhook_eh_personality
490 struct lang_hooks lang_hooks
= LANG_HOOKS_INITIALIZER
;
492 #include "gt-go-go-lang.h"
493 #include "gtype-go.h"