1 /* go-lang.c -- Go frontend gcc interface.
2 Copyright (C) 2009, 2010, 2011 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(()) 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
87 go_langhook_init (void)
89 build_common_tree_nodes (false, false);
91 /* We must create the gogo IR after calling build_common_tree_nodes
92 (because Gogo::define_builtin_function_trees refers indirectly
93 to, e.g., unsigned_char_type_node) but before calling
94 build_common_builtin_nodes (because it calls, indirectly,
96 go_create_gogo (INT_TYPE_SIZE, POINTER_SIZE);
98 build_common_builtin_nodes ();
100 /* I don't know why this is not done by any of the above. */
101 void_list_node = build_tree_list (NULL_TREE, void_type_node);
103 /* The default precision for floating point numbers. This is used
104 for floating point constants with abstract type. This may
105 eventually be controllable by a command line option. */
106 mpfr_set_default_prec (128);
108 /* Go uses exceptions. */
109 using_eh_for_cleanups ();
114 /* The option mask. */
117 go_langhook_option_lang_mask (void)
122 /* Initialize the options structure. */
125 go_langhook_init_options_struct (struct gcc_options *opts)
127 /* Go says that signed overflow is precisely defined. */
128 opts->x_flag_wrapv = 1;
130 /* We default to using strict aliasing, since Go pointers are safe.
131 This is turned off for code that imports the "unsafe" package,
132 because using unsafe.pointer violates C style aliasing
134 opts->x_flag_strict_aliasing = 1;
136 /* Default to avoiding range issues for complex multiply and
138 opts->x_flag_complex_method = 2;
140 /* The builtin math functions should not set errno. */
141 opts->x_flag_errno_math = 0;
142 opts->frontend_set_flag_errno_math = true;
144 /* We turn on stack splitting if we can. */
145 if (targetm_common.supports_split_stack (false, opts))
146 opts->x_flag_split_stack = 1;
148 /* Exceptions are used to handle recovering from panics. */
149 opts->x_flag_exceptions = 1;
150 opts->x_flag_non_call_exceptions = 1;
153 /* Infrastructure for a VEC of char * pointers. */
155 typedef const char *go_char_p;
156 DEF_VEC_P(go_char_p);
157 DEF_VEC_ALLOC_P(go_char_p, heap);
159 /* The list of directories to search after all the Go specific
160 directories have been searched. */
162 static VEC(go_char_p, heap) *go_search_dirs;
164 /* Handle Go specific options. Return 0 if we didn't do anything. */
167 go_langhook_handle_option (
170 int value ATTRIBUTE_UNUSED,
171 int kind ATTRIBUTE_UNUSED,
172 location_t loc ATTRIBUTE_UNUSED,
173 const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED)
175 enum opt_code code = (enum opt_code) scode;
181 go_add_search_path (arg);
185 /* A -L option is assumed to come from the compiler driver.
186 This is a system directory. We search the following
187 directories, if they exist, before this one:
189 dir/go/VERSION/MACHINE
190 This is like include/c++. */
192 static const char dir_separator_str[] = { DIR_SEPARATOR, 0 };
198 p = XALLOCAVEC (char,
199 (len + sizeof "go" + sizeof DEFAULT_TARGET_VERSION
200 + sizeof DEFAULT_TARGET_MACHINE + 3));
202 if (len > 0 && !IS_DIR_SEPARATOR (p[len - 1]))
203 strcat (p, dir_separator_str);
205 strcat (p, dir_separator_str);
206 strcat (p, DEFAULT_TARGET_VERSION);
207 if (stat (p, &st) == 0 && S_ISDIR (st.st_mode))
209 go_add_search_path (p);
210 strcat (p, dir_separator_str);
211 strcat (p, DEFAULT_TARGET_MACHINE);
212 if (stat (p, &st) == 0 && S_ISDIR (st.st_mode))
213 go_add_search_path (p);
216 /* Search ARG too, but only after we've searched to Go
217 specific directories for all -L arguments. */
218 VEC_safe_push (go_char_p, heap, go_search_dirs, arg);
223 ret = go_enable_dump (arg) ? true : false;
226 case OPT_fgo_prefix_:
231 /* Just return 1 to indicate that the option is valid. */
238 /* Run after parsing options. */
241 go_langhook_post_options (const char **pfilename ATTRIBUTE_UNUSED)
246 gcc_assert (num_in_fnames > 0);
248 FOR_EACH_VEC_ELT (go_char_p, go_search_dirs, ix, dir)
249 go_add_search_path (dir);
250 VEC_free (go_char_p, heap, go_search_dirs);
251 go_search_dirs = NULL;
253 if (flag_excess_precision_cmdline == EXCESS_PRECISION_DEFAULT)
254 flag_excess_precision_cmdline = EXCESS_PRECISION_STANDARD;
256 /* Returning false means that the backend should be used. */
261 go_langhook_parse_file (void)
263 go_parse_input_files (in_fnames, num_in_fnames, flag_syntax_only,
264 go_require_return_statement);
268 go_langhook_type_for_size (unsigned int bits, int unsignedp)
270 return go_type_for_size (bits, unsignedp);
274 go_langhook_type_for_mode (enum machine_mode mode, int unsignedp)
276 /* Go has no vector types. Build them here. FIXME: It does not
277 make sense for the middle-end to ask the frontend for a type
278 which the frontend does not support. However, at least for now
279 it is required. See PR 46805. */
280 if (VECTOR_MODE_P (mode))
284 inner = go_langhook_type_for_mode (GET_MODE_INNER (mode), unsignedp);
285 if (inner != NULL_TREE)
286 return build_vector_type_for_mode (inner, mode);
290 return go_type_for_mode (mode, unsignedp);
293 /* Record a builtin function. We just ignore builtin functions. */
296 go_langhook_builtin_function (tree decl)
301 /* Return true if we are in the global binding level. */
304 go_langhook_global_bindings_p (void)
306 return current_function_decl == NULL_TREE;
309 /* Push a declaration into the current binding level. We can't
310 usefully implement this since we don't want to convert from tree
311 back to one of our internal data structures. I think the only way
312 this is used is to record a decl which is to be returned by
313 getdecls, and we could implement it for that purpose if
317 go_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED)
322 /* This hook is used to get the current list of declarations as trees.
323 We don't support that; instead we use the write_globals hook. This
324 can't simply crash because it is called by -gstabs. */
327 go_langhook_getdecls (void)
332 /* Write out globals. */
335 go_langhook_write_globals (void)
340 /* Go specific gimplification. We need to gimplify
341 CALL_EXPR_STATIC_CHAIN, because the gimplifier doesn't handle
345 go_langhook_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
347 if (TREE_CODE (*expr_p) == CALL_EXPR
348 && CALL_EXPR_STATIC_CHAIN (*expr_p) != NULL_TREE)
349 gimplify_expr (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p, post_p,
350 is_gimple_val, fb_rvalue);
354 /* Return a decl for the exception personality function. The function
355 itself is implemented in libgo/runtime/go-unwind.c. */
358 go_langhook_eh_personality (void)
360 static tree personality_decl;
361 if (personality_decl == NULL_TREE)
363 personality_decl = build_personality_function ("gccgo");
364 go_preserve_from_gc (personality_decl);
366 return personality_decl;
369 /* Functions called directly by the generic backend. */
372 convert (tree type, tree expr)
374 if (type == error_mark_node
375 || expr == error_mark_node
376 || TREE_TYPE (expr) == error_mark_node)
377 return error_mark_node;
379 if (type == TREE_TYPE (expr))
382 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr)))
383 return fold_convert (type, expr);
385 switch (TREE_CODE (type))
389 return fold_convert (type, expr);
391 return fold (convert_to_integer (type, expr));
393 return fold (convert_to_pointer (type, expr));
395 return fold (convert_to_real (type, expr));
397 return fold (convert_to_complex (type, expr));
405 /* FIXME: This is a hack to preserve trees that we create from the
406 garbage collector. */
408 static GTY(()) tree go_gc_root;
411 go_preserve_from_gc (tree t)
413 go_gc_root = tree_cons (NULL_TREE, t, go_gc_root);
416 /* Convert an identifier for use in an error message. */
419 go_localize_identifier (const char *ident)
421 return identifier_to_locale (ident);
424 #undef LANG_HOOKS_NAME
425 #undef LANG_HOOKS_INIT
426 #undef LANG_HOOKS_OPTION_LANG_MASK
427 #undef LANG_HOOKS_INIT_OPTIONS_STRUCT
428 #undef LANG_HOOKS_HANDLE_OPTION
429 #undef LANG_HOOKS_POST_OPTIONS
430 #undef LANG_HOOKS_PARSE_FILE
431 #undef LANG_HOOKS_TYPE_FOR_MODE
432 #undef LANG_HOOKS_TYPE_FOR_SIZE
433 #undef LANG_HOOKS_BUILTIN_FUNCTION
434 #undef LANG_HOOKS_GLOBAL_BINDINGS_P
435 #undef LANG_HOOKS_PUSHDECL
436 #undef LANG_HOOKS_GETDECLS
437 #undef LANG_HOOKS_WRITE_GLOBALS
438 #undef LANG_HOOKS_GIMPLIFY_EXPR
439 #undef LANG_HOOKS_EH_PERSONALITY
441 #define LANG_HOOKS_NAME "GNU Go"
442 #define LANG_HOOKS_INIT go_langhook_init
443 #define LANG_HOOKS_OPTION_LANG_MASK go_langhook_option_lang_mask
444 #define LANG_HOOKS_INIT_OPTIONS_STRUCT go_langhook_init_options_struct
445 #define LANG_HOOKS_HANDLE_OPTION go_langhook_handle_option
446 #define LANG_HOOKS_POST_OPTIONS go_langhook_post_options
447 #define LANG_HOOKS_PARSE_FILE go_langhook_parse_file
448 #define LANG_HOOKS_TYPE_FOR_MODE go_langhook_type_for_mode
449 #define LANG_HOOKS_TYPE_FOR_SIZE go_langhook_type_for_size
450 #define LANG_HOOKS_BUILTIN_FUNCTION go_langhook_builtin_function
451 #define LANG_HOOKS_GLOBAL_BINDINGS_P go_langhook_global_bindings_p
452 #define LANG_HOOKS_PUSHDECL go_langhook_pushdecl
453 #define LANG_HOOKS_GETDECLS go_langhook_getdecls
454 #define LANG_HOOKS_WRITE_GLOBALS go_langhook_write_globals
455 #define LANG_HOOKS_GIMPLIFY_EXPR go_langhook_gimplify_expr
456 #define LANG_HOOKS_EH_PERSONALITY go_langhook_eh_personality
458 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
460 #include "gt-go-go-lang.h"
461 #include "gtype-go.h"