2013-11-21 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / go / go-lang.c
blob93a0446efe620585817216f6cd2aebe80a4ed235
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
9 version.
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
14 for more details.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "ansidecl.h"
23 #include "coretypes.h"
24 #include "opts.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "gimplify.h"
28 #include "stor-layout.h"
29 #include "ggc.h"
30 #include "toplev.h"
31 #include "debug.h"
32 #include "options.h"
33 #include "flags.h"
34 #include "convert.h"
35 #include "diagnostic.h"
36 #include "langhooks.h"
37 #include "langhooks-def.h"
38 #include "target.h"
39 #include "common/common-target.h"
41 #include <mpfr.h>
43 #include "go-c.h"
45 /* Language-dependent contents of a type. */
47 struct GTY(()) lang_type
49 char dummy;
52 /* Language-dependent contents of a decl. */
54 struct GTY((variable_size)) lang_decl
56 char dummy;
59 /* Language-dependent contents of an identifier. This must include a
60 tree_identifier. */
62 struct GTY(()) lang_identifier
64 struct tree_identifier common;
67 /* The resulting tree type. */
69 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
70 chain_next ("CODE_CONTAINS_STRUCT (TREE_CODE (&%h.generic), TS_COMMON) ? ((union lang_tree_node *) TREE_CHAIN (&%h.generic)) : NULL")))
71 lang_tree_node
73 union tree_node GTY((tag ("0"),
74 desc ("tree_node_structure (&%h)"))) generic;
75 struct lang_identifier GTY((tag ("1"))) identifier;
78 /* We don't use language_function. */
80 struct GTY(()) language_function
82 int dummy;
85 /* Option information we need to pass to go_create_gogo. */
87 static const char *go_pkgpath = NULL;
88 static const char *go_prefix = NULL;
89 static const char *go_relative_import_path = NULL;
91 /* Language hooks. */
93 static bool
94 go_langhook_init (void)
96 build_common_tree_nodes (false, false);
98 /* I don't know why this has to be done explicitly. */
99 void_list_node = build_tree_list (NULL_TREE, void_type_node);
101 /* We must create the gogo IR after calling build_common_tree_nodes
102 (because Gogo::define_builtin_function_trees refers indirectly
103 to, e.g., unsigned_char_type_node) but before calling
104 build_common_builtin_nodes (because it calls, indirectly,
105 go_type_for_size). */
106 go_create_gogo (INT_TYPE_SIZE, POINTER_SIZE, go_pkgpath, go_prefix,
107 go_relative_import_path);
109 build_common_builtin_nodes ();
111 /* The default precision for floating point numbers. This is used
112 for floating point constants with abstract type. This may
113 eventually be controllable by a command line option. */
114 mpfr_set_default_prec (256);
116 /* Go uses exceptions. */
117 using_eh_for_cleanups ();
119 return true;
122 /* The option mask. */
124 static unsigned int
125 go_langhook_option_lang_mask (void)
127 return CL_Go;
130 /* Initialize the options structure. */
132 static void
133 go_langhook_init_options_struct (struct gcc_options *opts)
135 /* Go says that signed overflow is precisely defined. */
136 opts->x_flag_wrapv = 1;
138 /* We default to using strict aliasing, since Go pointers are safe.
139 This is turned off for code that imports the "unsafe" package,
140 because using unsafe.pointer violates C style aliasing
141 requirements. */
142 opts->x_flag_strict_aliasing = 1;
144 /* Default to avoiding range issues for complex multiply and
145 divide. */
146 opts->x_flag_complex_method = 2;
148 /* The builtin math functions should not set errno. */
149 opts->x_flag_errno_math = 0;
150 opts->frontend_set_flag_errno_math = true;
152 /* We turn on stack splitting if we can. */
153 if (targetm_common.supports_split_stack (false, opts))
154 opts->x_flag_split_stack = 1;
156 /* Exceptions are used to handle recovering from panics. */
157 opts->x_flag_exceptions = 1;
158 opts->x_flag_non_call_exceptions = 1;
161 /* Infrastructure for a vector of char * pointers. */
163 typedef const char *go_char_p;
165 /* The list of directories to search after all the Go specific
166 directories have been searched. */
168 static vec<go_char_p> go_search_dirs;
170 /* Handle Go specific options. Return 0 if we didn't do anything. */
172 static bool
173 go_langhook_handle_option (
174 size_t scode,
175 const char *arg,
176 int value ATTRIBUTE_UNUSED,
177 int kind ATTRIBUTE_UNUSED,
178 location_t loc ATTRIBUTE_UNUSED,
179 const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED)
181 enum opt_code code = (enum opt_code) scode;
182 bool ret = true;
184 switch (code)
186 case OPT_I:
187 go_add_search_path (arg);
188 break;
190 case OPT_L:
191 /* A -L option is assumed to come from the compiler driver.
192 This is a system directory. We search the following
193 directories, if they exist, before this one:
194 dir/go/VERSION
195 dir/go/VERSION/MACHINE
196 This is like include/c++. */
198 static const char dir_separator_str[] = { DIR_SEPARATOR, 0 };
199 size_t len;
200 char *p;
201 struct stat st;
203 len = strlen (arg);
204 p = XALLOCAVEC (char,
205 (len + sizeof "go" + sizeof DEFAULT_TARGET_VERSION
206 + sizeof DEFAULT_TARGET_MACHINE + 3));
207 strcpy (p, arg);
208 if (len > 0 && !IS_DIR_SEPARATOR (p[len - 1]))
209 strcat (p, dir_separator_str);
210 strcat (p, "go");
211 strcat (p, dir_separator_str);
212 strcat (p, DEFAULT_TARGET_VERSION);
213 if (stat (p, &st) == 0 && S_ISDIR (st.st_mode))
215 go_add_search_path (p);
216 strcat (p, dir_separator_str);
217 strcat (p, DEFAULT_TARGET_MACHINE);
218 if (stat (p, &st) == 0 && S_ISDIR (st.st_mode))
219 go_add_search_path (p);
222 /* Search ARG too, but only after we've searched to Go
223 specific directories for all -L arguments. */
224 go_search_dirs.safe_push (arg);
226 break;
228 case OPT_fgo_dump_:
229 ret = go_enable_dump (arg) ? true : false;
230 break;
232 case OPT_fgo_optimize_:
233 ret = go_enable_optimize (arg) ? true : false;
234 break;
236 case OPT_fgo_pkgpath_:
237 go_pkgpath = arg;
238 break;
240 case OPT_fgo_prefix_:
241 go_prefix = arg;
242 break;
244 case OPT_fgo_relative_import_path_:
245 go_relative_import_path = arg;
246 break;
248 default:
249 /* Just return 1 to indicate that the option is valid. */
250 break;
253 return ret;
256 /* Run after parsing options. */
258 static bool
259 go_langhook_post_options (const char **pfilename ATTRIBUTE_UNUSED)
261 unsigned int ix;
262 const char *dir;
264 gcc_assert (num_in_fnames > 0);
266 FOR_EACH_VEC_ELT (go_search_dirs, ix, dir)
267 go_add_search_path (dir);
268 go_search_dirs.release ();
270 if (flag_excess_precision_cmdline == EXCESS_PRECISION_DEFAULT)
271 flag_excess_precision_cmdline = EXCESS_PRECISION_STANDARD;
273 /* The isolate_erroneous_paths optimization can change a nil
274 dereference from a panic to a trap, so we have to disable it for
275 Go, even though it is normally enabled by -O2. */
276 if (!global_options_set.x_flag_isolate_erroneous_paths)
277 global_options.x_flag_isolate_erroneous_paths = 0;
279 /* Returning false means that the backend should be used. */
280 return false;
283 static void
284 go_langhook_parse_file (void)
286 go_parse_input_files (in_fnames, num_in_fnames, flag_syntax_only,
287 go_require_return_statement);
290 static tree
291 go_langhook_type_for_size (unsigned int bits, int unsignedp)
293 return go_type_for_size (bits, unsignedp);
296 static tree
297 go_langhook_type_for_mode (enum machine_mode mode, int unsignedp)
299 tree type;
300 /* Go has no vector types. Build them here. FIXME: It does not
301 make sense for the middle-end to ask the frontend for a type
302 which the frontend does not support. However, at least for now
303 it is required. See PR 46805. */
304 if (VECTOR_MODE_P (mode))
306 tree inner;
308 inner = go_langhook_type_for_mode (GET_MODE_INNER (mode), unsignedp);
309 if (inner != NULL_TREE)
310 return build_vector_type_for_mode (inner, mode);
311 return NULL_TREE;
314 type = go_type_for_mode (mode, unsignedp);
315 if (type)
316 return type;
318 #if HOST_BITS_PER_WIDE_INT >= 64
319 /* The middle-end and some backends rely on TImode being supported
320 for 64-bit HWI. */
321 if (mode == TImode)
323 type = build_nonstandard_integer_type (GET_MODE_BITSIZE (TImode),
324 unsignedp);
325 if (type && TYPE_MODE (type) == TImode)
326 return type;
328 #endif
329 return NULL_TREE;
332 /* Record a builtin function. We just ignore builtin functions. */
334 static tree
335 go_langhook_builtin_function (tree decl)
337 return decl;
340 /* Return true if we are in the global binding level. */
342 static bool
343 go_langhook_global_bindings_p (void)
345 return current_function_decl == NULL_TREE;
348 /* Push a declaration into the current binding level. We can't
349 usefully implement this since we don't want to convert from tree
350 back to one of our internal data structures. I think the only way
351 this is used is to record a decl which is to be returned by
352 getdecls, and we could implement it for that purpose if
353 necessary. */
355 static tree
356 go_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED)
358 gcc_unreachable ();
361 /* This hook is used to get the current list of declarations as trees.
362 We don't support that; instead we use the write_globals hook. This
363 can't simply crash because it is called by -gstabs. */
365 static tree
366 go_langhook_getdecls (void)
368 return NULL;
371 /* Write out globals. */
373 static void
374 go_langhook_write_globals (void)
376 go_write_globals ();
379 /* Go specific gimplification. We need to gimplify
380 CALL_EXPR_STATIC_CHAIN, because the gimplifier doesn't handle
381 it. */
383 static int
384 go_langhook_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
386 if (TREE_CODE (*expr_p) == CALL_EXPR
387 && CALL_EXPR_STATIC_CHAIN (*expr_p) != NULL_TREE)
388 gimplify_expr (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p, post_p,
389 is_gimple_val, fb_rvalue);
390 return GS_UNHANDLED;
393 /* Return a decl for the exception personality function. The function
394 itself is implemented in libgo/runtime/go-unwind.c. */
396 static tree
397 go_langhook_eh_personality (void)
399 static tree personality_decl;
400 if (personality_decl == NULL_TREE)
402 personality_decl = build_personality_function ("gccgo");
403 go_preserve_from_gc (personality_decl);
405 return personality_decl;
408 /* Functions called directly by the generic backend. */
410 tree
411 convert (tree type, tree expr)
413 if (type == error_mark_node
414 || expr == error_mark_node
415 || TREE_TYPE (expr) == error_mark_node)
416 return error_mark_node;
418 if (type == TREE_TYPE (expr))
419 return expr;
421 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr)))
422 return fold_convert (type, expr);
424 switch (TREE_CODE (type))
426 case VOID_TYPE:
427 case BOOLEAN_TYPE:
428 return fold_convert (type, expr);
429 case INTEGER_TYPE:
430 return fold (convert_to_integer (type, expr));
431 case POINTER_TYPE:
432 return fold (convert_to_pointer (type, expr));
433 case REAL_TYPE:
434 return fold (convert_to_real (type, expr));
435 case COMPLEX_TYPE:
436 return fold (convert_to_complex (type, expr));
437 default:
438 break;
441 gcc_unreachable ();
444 /* FIXME: This is a hack to preserve trees that we create from the
445 garbage collector. */
447 static GTY(()) tree go_gc_root;
449 void
450 go_preserve_from_gc (tree t)
452 go_gc_root = tree_cons (NULL_TREE, t, go_gc_root);
455 /* Convert an identifier for use in an error message. */
457 const char *
458 go_localize_identifier (const char *ident)
460 return identifier_to_locale (ident);
463 #undef LANG_HOOKS_NAME
464 #undef LANG_HOOKS_INIT
465 #undef LANG_HOOKS_OPTION_LANG_MASK
466 #undef LANG_HOOKS_INIT_OPTIONS_STRUCT
467 #undef LANG_HOOKS_HANDLE_OPTION
468 #undef LANG_HOOKS_POST_OPTIONS
469 #undef LANG_HOOKS_PARSE_FILE
470 #undef LANG_HOOKS_TYPE_FOR_MODE
471 #undef LANG_HOOKS_TYPE_FOR_SIZE
472 #undef LANG_HOOKS_BUILTIN_FUNCTION
473 #undef LANG_HOOKS_GLOBAL_BINDINGS_P
474 #undef LANG_HOOKS_PUSHDECL
475 #undef LANG_HOOKS_GETDECLS
476 #undef LANG_HOOKS_WRITE_GLOBALS
477 #undef LANG_HOOKS_GIMPLIFY_EXPR
478 #undef LANG_HOOKS_EH_PERSONALITY
480 #define LANG_HOOKS_NAME "GNU Go"
481 #define LANG_HOOKS_INIT go_langhook_init
482 #define LANG_HOOKS_OPTION_LANG_MASK go_langhook_option_lang_mask
483 #define LANG_HOOKS_INIT_OPTIONS_STRUCT go_langhook_init_options_struct
484 #define LANG_HOOKS_HANDLE_OPTION go_langhook_handle_option
485 #define LANG_HOOKS_POST_OPTIONS go_langhook_post_options
486 #define LANG_HOOKS_PARSE_FILE go_langhook_parse_file
487 #define LANG_HOOKS_TYPE_FOR_MODE go_langhook_type_for_mode
488 #define LANG_HOOKS_TYPE_FOR_SIZE go_langhook_type_for_size
489 #define LANG_HOOKS_BUILTIN_FUNCTION go_langhook_builtin_function
490 #define LANG_HOOKS_GLOBAL_BINDINGS_P go_langhook_global_bindings_p
491 #define LANG_HOOKS_PUSHDECL go_langhook_pushdecl
492 #define LANG_HOOKS_GETDECLS go_langhook_getdecls
493 #define LANG_HOOKS_WRITE_GLOBALS go_langhook_write_globals
494 #define LANG_HOOKS_GIMPLIFY_EXPR go_langhook_gimplify_expr
495 #define LANG_HOOKS_EH_PERSONALITY go_langhook_eh_personality
497 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
499 #include "gt-go-go-lang.h"
500 #include "gtype-go.h"