* go-lang.c (go_langhook_init_options_struct): Don't set
[official-gcc.git] / gcc / go / go-lang.c
blob675b5828b298bd90b6fb67219c7017fc5a589d40
1 /* go-lang.c -- Go frontend gcc interface.
2 Copyright (C) 2009-2015 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 "alias.h"
26 #include "tree.h"
27 #include "options.h"
28 #include "fold-const.h"
29 #include "tm.h"
30 #include "hard-reg-set.h"
31 #include "function.h"
32 #include "gimple-expr.h"
33 #include "gimplify.h"
34 #include "stor-layout.h"
35 #include "toplev.h"
36 #include "debug.h"
37 #include "options.h"
38 #include "flags.h"
39 #include "convert.h"
40 #include "diagnostic.h"
41 #include "langhooks.h"
42 #include "langhooks-def.h"
43 #include "target.h"
44 #include "common/common-target.h"
46 #include <mpfr.h>
48 #include "go-c.h"
50 /* Language-dependent contents of a type. */
52 struct GTY(()) lang_type
54 char dummy;
57 /* Language-dependent contents of a decl. */
59 struct GTY(()) lang_decl
61 char dummy;
64 /* Language-dependent contents of an identifier. This must include a
65 tree_identifier. */
67 struct GTY(()) lang_identifier
69 struct tree_identifier common;
72 /* The resulting tree type. */
74 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
75 chain_next ("CODE_CONTAINS_STRUCT (TREE_CODE (&%h.generic), TS_COMMON) ? ((union lang_tree_node *) TREE_CHAIN (&%h.generic)) : NULL")))
76 lang_tree_node
78 union tree_node GTY((tag ("0"),
79 desc ("tree_node_structure (&%h)"))) generic;
80 struct lang_identifier GTY((tag ("1"))) identifier;
83 /* We don't use language_function. */
85 struct GTY(()) language_function
87 int dummy;
90 /* Option information we need to pass to go_create_gogo. */
92 static const char *go_pkgpath = NULL;
93 static const char *go_prefix = NULL;
94 static const char *go_relative_import_path = NULL;
96 /* Language hooks. */
98 static bool
99 go_langhook_init (void)
101 build_common_tree_nodes (false, false);
103 /* I don't know why this has to be done explicitly. */
104 void_list_node = build_tree_list (NULL_TREE, void_type_node);
106 /* We must create the gogo IR after calling build_common_tree_nodes
107 (because Gogo::define_builtin_function_trees refers indirectly
108 to, e.g., unsigned_char_type_node) but before calling
109 build_common_builtin_nodes (because it calls, indirectly,
110 go_type_for_size). */
111 go_create_gogo (INT_TYPE_SIZE, POINTER_SIZE, go_pkgpath, go_prefix,
112 go_relative_import_path, go_check_divide_zero,
113 go_check_divide_overflow);
115 build_common_builtin_nodes ();
117 /* The default precision for floating point numbers. This is used
118 for floating point constants with abstract type. This may
119 eventually be controllable by a command line option. */
120 mpfr_set_default_prec (256);
122 /* Go uses exceptions. */
123 using_eh_for_cleanups ();
125 return true;
128 /* The option mask. */
130 static unsigned int
131 go_langhook_option_lang_mask (void)
133 return CL_Go;
136 /* Initialize the options structure. */
138 static void
139 go_langhook_init_options_struct (struct gcc_options *opts)
141 /* Go says that signed overflow is precisely defined. */
142 opts->x_flag_wrapv = 1;
144 /* We default to using strict aliasing, since Go pointers are safe.
145 This is turned off for code that imports the "unsafe" package,
146 because using unsafe.pointer violates C style aliasing
147 requirements. */
148 opts->x_flag_strict_aliasing = 1;
150 /* Default to avoiding range issues for complex multiply and
151 divide. */
152 opts->x_flag_complex_method = 2;
154 /* The builtin math functions should not set errno. */
155 opts->x_flag_errno_math = 0;
156 opts->frontend_set_flag_errno_math = true;
158 /* Exceptions are used to handle recovering from panics. */
159 opts->x_flag_exceptions = 1;
160 opts->x_flag_non_call_exceptions = 1;
162 /* Go programs expect runtime.Callers to work, and that uses
163 libbacktrace that uses debug info. Set the debug info level to 1
164 by default. In post_options we will set the debug type if the
165 debug info level was not set back to 0 on the command line. */
166 opts->x_debug_info_level = DINFO_LEVEL_TERSE;
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. */
180 static bool
181 go_langhook_handle_option (
182 size_t scode,
183 const char *arg,
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;
190 bool ret = true;
192 switch (code)
194 case OPT_I:
195 go_add_search_path (arg);
196 break;
198 case OPT_L:
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:
202 dir/go/VERSION
203 dir/go/VERSION/MACHINE
204 This is like include/c++. */
206 static const char dir_separator_str[] = { DIR_SEPARATOR, 0 };
207 size_t len;
208 char *p;
209 struct stat st;
211 len = strlen (arg);
212 p = XALLOCAVEC (char,
213 (len + sizeof "go" + sizeof DEFAULT_TARGET_VERSION
214 + sizeof DEFAULT_TARGET_MACHINE + 3));
215 strcpy (p, arg);
216 if (len > 0 && !IS_DIR_SEPARATOR (p[len - 1]))
217 strcat (p, dir_separator_str);
218 strcat (p, "go");
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);
234 break;
236 case OPT_fgo_dump_:
237 ret = go_enable_dump (arg) ? true : false;
238 break;
240 case OPT_fgo_optimize_:
241 ret = go_enable_optimize (arg) ? true : false;
242 break;
244 case OPT_fgo_pkgpath_:
245 go_pkgpath = arg;
246 break;
248 case OPT_fgo_prefix_:
249 go_prefix = arg;
250 break;
252 case OPT_fgo_relative_import_path_:
253 go_relative_import_path = arg;
254 break;
256 default:
257 /* Just return 1 to indicate that the option is valid. */
258 break;
261 return ret;
264 /* Run after parsing options. */
266 static bool
267 go_langhook_post_options (const char **pfilename ATTRIBUTE_UNUSED)
269 unsigned int ix;
270 const char *dir;
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 /* If the debug info level is still 1, as set in init_options, make
286 sure that some debugging type is selected. */
287 if (global_options.x_debug_info_level == DINFO_LEVEL_TERSE
288 && global_options.x_write_symbols == NO_DEBUG)
289 global_options.x_write_symbols = PREFERRED_DEBUGGING_TYPE;
291 /* We turn on stack splitting if we can. */
292 if (!global_options_set.x_flag_split_stack
293 && targetm_common.supports_split_stack (false, &global_options))
294 global_options.x_flag_split_stack = 1;
296 /* Returning false means that the backend should be used. */
297 return false;
300 static void
301 go_langhook_parse_file (void)
303 go_parse_input_files (in_fnames, num_in_fnames, flag_syntax_only,
304 go_require_return_statement);
306 /* Final processing of globals and early debug info generation. */
307 go_write_globals ();
310 static tree
311 go_langhook_type_for_size (unsigned int bits, int unsignedp)
313 tree type;
314 if (unsignedp)
316 if (bits == INT_TYPE_SIZE)
317 type = unsigned_type_node;
318 else if (bits == CHAR_TYPE_SIZE)
319 type = unsigned_char_type_node;
320 else if (bits == SHORT_TYPE_SIZE)
321 type = short_unsigned_type_node;
322 else if (bits == LONG_TYPE_SIZE)
323 type = long_unsigned_type_node;
324 else if (bits == LONG_LONG_TYPE_SIZE)
325 type = long_long_unsigned_type_node;
326 else
327 type = make_unsigned_type(bits);
329 else
331 if (bits == INT_TYPE_SIZE)
332 type = integer_type_node;
333 else if (bits == CHAR_TYPE_SIZE)
334 type = signed_char_type_node;
335 else if (bits == SHORT_TYPE_SIZE)
336 type = short_integer_type_node;
337 else if (bits == LONG_TYPE_SIZE)
338 type = long_integer_type_node;
339 else if (bits == LONG_LONG_TYPE_SIZE)
340 type = long_long_integer_type_node;
341 else
342 type = make_signed_type(bits);
344 return type;
347 static tree
348 go_langhook_type_for_mode (machine_mode mode, int unsignedp)
350 tree type;
351 /* Go has no vector types. Build them here. FIXME: It does not
352 make sense for the middle-end to ask the frontend for a type
353 which the frontend does not support. However, at least for now
354 it is required. See PR 46805. */
355 if (VECTOR_MODE_P (mode))
357 tree inner;
359 inner = go_langhook_type_for_mode (GET_MODE_INNER (mode), unsignedp);
360 if (inner != NULL_TREE)
361 return build_vector_type_for_mode (inner, mode);
362 return NULL_TREE;
365 // FIXME: This static_cast should be in machmode.h.
366 enum mode_class mc = static_cast<enum mode_class>(GET_MODE_CLASS(mode));
367 if (mc == MODE_INT)
368 return go_langhook_type_for_size(GET_MODE_BITSIZE(mode), unsignedp);
369 else if (mc == MODE_FLOAT)
371 switch (GET_MODE_BITSIZE (mode))
373 case 32:
374 return float_type_node;
375 case 64:
376 return double_type_node;
377 default:
378 // We have to check for long double in order to support
379 // i386 excess precision.
380 if (mode == TYPE_MODE(long_double_type_node))
381 return long_double_type_node;
384 else if (mc == MODE_COMPLEX_FLOAT)
386 switch (GET_MODE_BITSIZE (mode))
388 case 64:
389 return complex_float_type_node;
390 case 128:
391 return complex_double_type_node;
392 default:
393 // We have to check for long double in order to support
394 // i386 excess precision.
395 if (mode == TYPE_MODE(complex_long_double_type_node))
396 return complex_long_double_type_node;
400 #if HOST_BITS_PER_WIDE_INT >= 64
401 /* The middle-end and some backends rely on TImode being supported
402 for 64-bit HWI. */
403 if (mode == TImode)
405 type = build_nonstandard_integer_type (GET_MODE_BITSIZE (TImode),
406 unsignedp);
407 if (type && TYPE_MODE (type) == TImode)
408 return type;
410 #endif
411 return NULL_TREE;
414 /* Record a builtin function. We just ignore builtin functions. */
416 static tree
417 go_langhook_builtin_function (tree decl)
419 return decl;
422 /* Return true if we are in the global binding level. */
424 static bool
425 go_langhook_global_bindings_p (void)
427 return current_function_decl == NULL_TREE;
430 /* Push a declaration into the current binding level. We can't
431 usefully implement this since we don't want to convert from tree
432 back to one of our internal data structures. I think the only way
433 this is used is to record a decl which is to be returned by
434 getdecls, and we could implement it for that purpose if
435 necessary. */
437 static tree
438 go_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED)
440 gcc_unreachable ();
443 /* This hook is used to get the current list of declarations as trees.
444 We don't support that; instead we use the write_globals hook. This
445 can't simply crash because it is called by -gstabs. */
447 static tree
448 go_langhook_getdecls (void)
450 return NULL;
453 /* Go specific gimplification. We need to gimplify
454 CALL_EXPR_STATIC_CHAIN, because the gimplifier doesn't handle
455 it. */
457 static int
458 go_langhook_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
460 if (TREE_CODE (*expr_p) == CALL_EXPR
461 && CALL_EXPR_STATIC_CHAIN (*expr_p) != NULL_TREE)
462 gimplify_expr (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p, post_p,
463 is_gimple_val, fb_rvalue);
464 return GS_UNHANDLED;
467 /* Return a decl for the exception personality function. The function
468 itself is implemented in libgo/runtime/go-unwind.c. */
470 static tree
471 go_langhook_eh_personality (void)
473 static tree personality_decl;
474 if (personality_decl == NULL_TREE)
476 personality_decl = build_personality_function ("gccgo");
477 go_preserve_from_gc (personality_decl);
479 return personality_decl;
482 /* Functions called directly by the generic backend. */
484 tree
485 convert (tree type, tree expr)
487 if (type == error_mark_node
488 || expr == error_mark_node
489 || TREE_TYPE (expr) == error_mark_node)
490 return error_mark_node;
492 if (type == TREE_TYPE (expr))
493 return expr;
495 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr)))
496 return fold_convert (type, expr);
498 switch (TREE_CODE (type))
500 case VOID_TYPE:
501 case BOOLEAN_TYPE:
502 return fold_convert (type, expr);
503 case INTEGER_TYPE:
504 return fold (convert_to_integer (type, expr));
505 case POINTER_TYPE:
506 return fold (convert_to_pointer (type, expr));
507 case REAL_TYPE:
508 return fold (convert_to_real (type, expr));
509 case COMPLEX_TYPE:
510 return fold (convert_to_complex (type, expr));
511 default:
512 break;
515 gcc_unreachable ();
518 /* FIXME: This is a hack to preserve trees that we create from the
519 garbage collector. */
521 static GTY(()) tree go_gc_root;
523 void
524 go_preserve_from_gc (tree t)
526 go_gc_root = tree_cons (NULL_TREE, t, go_gc_root);
529 /* Convert an identifier for use in an error message. */
531 const char *
532 go_localize_identifier (const char *ident)
534 return identifier_to_locale (ident);
537 #undef LANG_HOOKS_NAME
538 #undef LANG_HOOKS_INIT
539 #undef LANG_HOOKS_OPTION_LANG_MASK
540 #undef LANG_HOOKS_INIT_OPTIONS_STRUCT
541 #undef LANG_HOOKS_HANDLE_OPTION
542 #undef LANG_HOOKS_POST_OPTIONS
543 #undef LANG_HOOKS_PARSE_FILE
544 #undef LANG_HOOKS_TYPE_FOR_MODE
545 #undef LANG_HOOKS_TYPE_FOR_SIZE
546 #undef LANG_HOOKS_BUILTIN_FUNCTION
547 #undef LANG_HOOKS_GLOBAL_BINDINGS_P
548 #undef LANG_HOOKS_PUSHDECL
549 #undef LANG_HOOKS_GETDECLS
550 #undef LANG_HOOKS_GIMPLIFY_EXPR
551 #undef LANG_HOOKS_EH_PERSONALITY
553 #define LANG_HOOKS_NAME "GNU Go"
554 #define LANG_HOOKS_INIT go_langhook_init
555 #define LANG_HOOKS_OPTION_LANG_MASK go_langhook_option_lang_mask
556 #define LANG_HOOKS_INIT_OPTIONS_STRUCT go_langhook_init_options_struct
557 #define LANG_HOOKS_HANDLE_OPTION go_langhook_handle_option
558 #define LANG_HOOKS_POST_OPTIONS go_langhook_post_options
559 #define LANG_HOOKS_PARSE_FILE go_langhook_parse_file
560 #define LANG_HOOKS_TYPE_FOR_MODE go_langhook_type_for_mode
561 #define LANG_HOOKS_TYPE_FOR_SIZE go_langhook_type_for_size
562 #define LANG_HOOKS_BUILTIN_FUNCTION go_langhook_builtin_function
563 #define LANG_HOOKS_GLOBAL_BINDINGS_P go_langhook_global_bindings_p
564 #define LANG_HOOKS_PUSHDECL go_langhook_pushdecl
565 #define LANG_HOOKS_GETDECLS go_langhook_getdecls
566 #define LANG_HOOKS_GIMPLIFY_EXPR go_langhook_gimplify_expr
567 #define LANG_HOOKS_EH_PERSONALITY go_langhook_eh_personality
569 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
571 #include "gt-go-go-lang.h"
572 #include "gtype-go.h"