* sr.po: Update.
[official-gcc.git] / gcc / go / go-lang.c
blob9c95c8e0bb16462a314f9bad7a1c34229858cdb7
1 /* go-lang.c -- Go frontend gcc interface.
2 Copyright (C) 2009-2016 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 "coretypes.h"
23 #include "target.h"
24 #include "tree.h"
25 #include "gimple-expr.h"
26 #include "diagnostic.h"
27 #include "opts.h"
28 #include "fold-const.h"
29 #include "gimplify.h"
30 #include "stor-layout.h"
31 #include "debug.h"
32 #include "convert.h"
33 #include "langhooks.h"
34 #include "langhooks-def.h"
35 #include "common/common-target.h"
37 #include <mpfr.h>
39 #include "go-c.h"
41 /* Language-dependent contents of a type. */
43 struct GTY(()) lang_type
45 char dummy;
48 /* Language-dependent contents of a decl. */
50 struct GTY(()) lang_decl
52 char dummy;
55 /* Language-dependent contents of an identifier. This must include a
56 tree_identifier. */
58 struct GTY(()) lang_identifier
60 struct tree_identifier common;
63 /* The resulting tree type. */
65 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
66 chain_next ("CODE_CONTAINS_STRUCT (TREE_CODE (&%h.generic), TS_COMMON) ? ((union lang_tree_node *) TREE_CHAIN (&%h.generic)) : NULL")))
67 lang_tree_node
69 union tree_node GTY((tag ("0"),
70 desc ("tree_node_structure (&%h)"))) generic;
71 struct lang_identifier GTY((tag ("1"))) identifier;
74 /* We don't use language_function. */
76 struct GTY(()) language_function
78 int dummy;
81 /* Option information we need to pass to go_create_gogo. */
83 static const char *go_pkgpath = NULL;
84 static const char *go_prefix = NULL;
85 static const char *go_relative_import_path = NULL;
87 /* Language hooks. */
89 static bool
90 go_langhook_init (void)
92 build_common_tree_nodes (false);
94 /* I don't know why this has to be done explicitly. */
95 void_list_node = build_tree_list (NULL_TREE, void_type_node);
97 /* We must create the gogo IR after calling build_common_tree_nodes
98 (because Gogo::define_builtin_function_trees refers indirectly
99 to, e.g., unsigned_char_type_node) but before calling
100 build_common_builtin_nodes (because it calls, indirectly,
101 go_type_for_size). */
102 go_create_gogo (INT_TYPE_SIZE, POINTER_SIZE, go_pkgpath, go_prefix,
103 go_relative_import_path, go_check_divide_zero,
104 go_check_divide_overflow);
106 build_common_builtin_nodes ();
108 /* The default precision for floating point numbers. This is used
109 for floating point constants with abstract type. This may
110 eventually be controllable by a command line option. */
111 mpfr_set_default_prec (256);
113 /* Go uses exceptions. */
114 using_eh_for_cleanups ();
116 return true;
119 /* The option mask. */
121 static unsigned int
122 go_langhook_option_lang_mask (void)
124 return CL_Go;
127 /* Initialize the options structure. */
129 static void
130 go_langhook_init_options_struct (struct gcc_options *opts)
132 /* Go says that signed overflow is precisely defined. */
133 opts->x_flag_wrapv = 1;
135 /* We default to using strict aliasing, since Go pointers are safe.
136 This is turned off for code that imports the "unsafe" package,
137 because using unsafe.pointer violates C style aliasing
138 requirements. */
139 opts->x_flag_strict_aliasing = 1;
141 /* Default to avoiding range issues for complex multiply and
142 divide. */
143 opts->x_flag_complex_method = 2;
145 /* The builtin math functions should not set errno. */
146 opts->x_flag_errno_math = 0;
147 opts->frontend_set_flag_errno_math = true;
149 /* Exceptions are used to handle recovering from panics. */
150 opts->x_flag_exceptions = 1;
151 opts->x_flag_non_call_exceptions = 1;
153 /* We need to keep pointers live for the garbage collector. */
154 opts->x_flag_keep_gc_roots_live = 1;
156 /* Go programs expect runtime.Callers to work, and that uses
157 libbacktrace that uses debug info. Set the debug info level to 1
158 by default. In post_options we will set the debug type if the
159 debug info level was not set back to 0 on the command line. */
160 opts->x_debug_info_level = DINFO_LEVEL_TERSE;
163 /* Infrastructure for a vector of char * pointers. */
165 typedef const char *go_char_p;
167 /* The list of directories to search after all the Go specific
168 directories have been searched. */
170 static vec<go_char_p> go_search_dirs;
172 /* Handle Go specific options. Return 0 if we didn't do anything. */
174 static bool
175 go_langhook_handle_option (
176 size_t scode,
177 const char *arg,
178 int value ATTRIBUTE_UNUSED,
179 int kind ATTRIBUTE_UNUSED,
180 location_t loc ATTRIBUTE_UNUSED,
181 const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED)
183 enum opt_code code = (enum opt_code) scode;
184 bool ret = true;
186 switch (code)
188 case OPT_I:
189 go_add_search_path (arg);
190 break;
192 case OPT_L:
193 /* A -L option is assumed to come from the compiler driver.
194 This is a system directory. We search the following
195 directories, if they exist, before this one:
196 dir/go/VERSION
197 dir/go/VERSION/MACHINE
198 This is like include/c++. */
200 static const char dir_separator_str[] = { DIR_SEPARATOR, 0 };
201 size_t len;
202 char *p;
203 struct stat st;
205 len = strlen (arg);
206 p = XALLOCAVEC (char,
207 (len + sizeof "go" + sizeof DEFAULT_TARGET_VERSION
208 + sizeof DEFAULT_TARGET_MACHINE + 3));
209 strcpy (p, arg);
210 if (len > 0 && !IS_DIR_SEPARATOR (p[len - 1]))
211 strcat (p, dir_separator_str);
212 strcat (p, "go");
213 strcat (p, dir_separator_str);
214 strcat (p, DEFAULT_TARGET_VERSION);
215 if (stat (p, &st) == 0 && S_ISDIR (st.st_mode))
217 go_add_search_path (p);
218 strcat (p, dir_separator_str);
219 strcat (p, DEFAULT_TARGET_MACHINE);
220 if (stat (p, &st) == 0 && S_ISDIR (st.st_mode))
221 go_add_search_path (p);
224 /* Search ARG too, but only after we've searched to Go
225 specific directories for all -L arguments. */
226 go_search_dirs.safe_push (arg);
228 break;
230 case OPT_fgo_dump_:
231 ret = go_enable_dump (arg) ? true : false;
232 break;
234 case OPT_fgo_optimize_:
235 ret = go_enable_optimize (arg) ? true : false;
236 break;
238 case OPT_fgo_pkgpath_:
239 go_pkgpath = arg;
240 break;
242 case OPT_fgo_prefix_:
243 go_prefix = arg;
244 break;
246 case OPT_fgo_relative_import_path_:
247 go_relative_import_path = arg;
248 break;
250 default:
251 /* Just return 1 to indicate that the option is valid. */
252 break;
255 return ret;
258 /* Run after parsing options. */
260 static bool
261 go_langhook_post_options (const char **pfilename ATTRIBUTE_UNUSED)
263 unsigned int ix;
264 const char *dir;
266 gcc_assert (num_in_fnames > 0);
268 FOR_EACH_VEC_ELT (go_search_dirs, ix, dir)
269 go_add_search_path (dir);
270 go_search_dirs.release ();
272 if (flag_excess_precision_cmdline == EXCESS_PRECISION_DEFAULT)
273 flag_excess_precision_cmdline = EXCESS_PRECISION_STANDARD;
275 /* Tail call optimizations can confuse uses of runtime.Callers. */
276 if (!global_options_set.x_flag_optimize_sibling_calls)
277 global_options.x_flag_optimize_sibling_calls = 0;
279 /* If the debug info level is still 1, as set in init_options, make
280 sure that some debugging type is selected. */
281 if (global_options.x_debug_info_level == DINFO_LEVEL_TERSE
282 && global_options.x_write_symbols == NO_DEBUG)
283 global_options.x_write_symbols = PREFERRED_DEBUGGING_TYPE;
285 /* We turn on stack splitting if we can. */
286 if (!global_options_set.x_flag_split_stack
287 && targetm_common.supports_split_stack (false, &global_options))
288 global_options.x_flag_split_stack = 1;
290 /* Returning false means that the backend should be used. */
291 return false;
294 static void
295 go_langhook_parse_file (void)
297 go_parse_input_files (in_fnames, num_in_fnames, flag_syntax_only,
298 go_require_return_statement);
300 /* Final processing of globals and early debug info generation. */
301 go_write_globals ();
304 static tree
305 go_langhook_type_for_size (unsigned int bits, int unsignedp)
307 tree type;
308 if (unsignedp)
310 if (bits == INT_TYPE_SIZE)
311 type = unsigned_type_node;
312 else if (bits == CHAR_TYPE_SIZE)
313 type = unsigned_char_type_node;
314 else if (bits == SHORT_TYPE_SIZE)
315 type = short_unsigned_type_node;
316 else if (bits == LONG_TYPE_SIZE)
317 type = long_unsigned_type_node;
318 else if (bits == LONG_LONG_TYPE_SIZE)
319 type = long_long_unsigned_type_node;
320 else
321 type = make_unsigned_type(bits);
323 else
325 if (bits == INT_TYPE_SIZE)
326 type = integer_type_node;
327 else if (bits == CHAR_TYPE_SIZE)
328 type = signed_char_type_node;
329 else if (bits == SHORT_TYPE_SIZE)
330 type = short_integer_type_node;
331 else if (bits == LONG_TYPE_SIZE)
332 type = long_integer_type_node;
333 else if (bits == LONG_LONG_TYPE_SIZE)
334 type = long_long_integer_type_node;
335 else
336 type = make_signed_type(bits);
338 return type;
341 static tree
342 go_langhook_type_for_mode (machine_mode mode, int unsignedp)
344 tree type;
345 /* Go has no vector types. Build them here. FIXME: It does not
346 make sense for the middle-end to ask the frontend for a type
347 which the frontend does not support. However, at least for now
348 it is required. See PR 46805. */
349 if (VECTOR_MODE_P (mode))
351 tree inner;
353 inner = go_langhook_type_for_mode (GET_MODE_INNER (mode), unsignedp);
354 if (inner != NULL_TREE)
355 return build_vector_type_for_mode (inner, mode);
356 return NULL_TREE;
359 // FIXME: This static_cast should be in machmode.h.
360 enum mode_class mc = static_cast<enum mode_class>(GET_MODE_CLASS(mode));
361 if (mc == MODE_INT)
362 return go_langhook_type_for_size(GET_MODE_BITSIZE(mode), unsignedp);
363 else if (mc == MODE_FLOAT)
365 switch (GET_MODE_BITSIZE (mode))
367 case 32:
368 return float_type_node;
369 case 64:
370 return double_type_node;
371 default:
372 // We have to check for long double in order to support
373 // i386 excess precision.
374 if (mode == TYPE_MODE(long_double_type_node))
375 return long_double_type_node;
378 else if (mc == MODE_COMPLEX_FLOAT)
380 switch (GET_MODE_BITSIZE (mode))
382 case 64:
383 return complex_float_type_node;
384 case 128:
385 return complex_double_type_node;
386 default:
387 // We have to check for long double in order to support
388 // i386 excess precision.
389 if (mode == TYPE_MODE(complex_long_double_type_node))
390 return complex_long_double_type_node;
394 #if HOST_BITS_PER_WIDE_INT >= 64
395 /* The middle-end and some backends rely on TImode being supported
396 for 64-bit HWI. */
397 if (mode == TImode)
399 type = build_nonstandard_integer_type (GET_MODE_BITSIZE (TImode),
400 unsignedp);
401 if (type && TYPE_MODE (type) == TImode)
402 return type;
404 #endif
405 return NULL_TREE;
408 /* Record a builtin function. We just ignore builtin functions. */
410 static tree
411 go_langhook_builtin_function (tree decl)
413 return decl;
416 /* Return true if we are in the global binding level. */
418 static bool
419 go_langhook_global_bindings_p (void)
421 return current_function_decl == NULL_TREE;
424 /* Push a declaration into the current binding level. We can't
425 usefully implement this since we don't want to convert from tree
426 back to one of our internal data structures. I think the only way
427 this is used is to record a decl which is to be returned by
428 getdecls, and we could implement it for that purpose if
429 necessary. */
431 static tree
432 go_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED)
434 gcc_unreachable ();
437 /* This hook is used to get the current list of declarations as trees.
438 We don't support that; instead we use the write_globals hook. This
439 can't simply crash because it is called by -gstabs. */
441 static tree
442 go_langhook_getdecls (void)
444 return NULL;
447 /* Go specific gimplification. We need to gimplify
448 CALL_EXPR_STATIC_CHAIN, because the gimplifier doesn't handle
449 it. */
451 static int
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);
458 return GS_UNHANDLED;
461 /* Return a decl for the exception personality function. The function
462 itself is implemented in libgo/runtime/go-unwind.c. */
464 static tree
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. */
478 tree
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))
487 return expr;
489 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr)))
490 return fold_convert (type, expr);
492 switch (TREE_CODE (type))
494 case VOID_TYPE:
495 case BOOLEAN_TYPE:
496 return fold_convert (type, expr);
497 case INTEGER_TYPE:
498 return fold (convert_to_integer (type, expr));
499 case POINTER_TYPE:
500 return fold (convert_to_pointer (type, expr));
501 case REAL_TYPE:
502 return fold (convert_to_real (type, expr));
503 case COMPLEX_TYPE:
504 return fold (convert_to_complex (type, expr));
505 default:
506 break;
509 gcc_unreachable ();
512 /* FIXME: This is a hack to preserve trees that we create from the
513 garbage collector. */
515 static GTY(()) tree go_gc_root;
517 void
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. */
525 const char *
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_GIMPLIFY_EXPR
545 #undef LANG_HOOKS_EH_PERSONALITY
547 #define LANG_HOOKS_NAME "GNU Go"
548 #define LANG_HOOKS_INIT go_langhook_init
549 #define LANG_HOOKS_OPTION_LANG_MASK go_langhook_option_lang_mask
550 #define LANG_HOOKS_INIT_OPTIONS_STRUCT go_langhook_init_options_struct
551 #define LANG_HOOKS_HANDLE_OPTION go_langhook_handle_option
552 #define LANG_HOOKS_POST_OPTIONS go_langhook_post_options
553 #define LANG_HOOKS_PARSE_FILE go_langhook_parse_file
554 #define LANG_HOOKS_TYPE_FOR_MODE go_langhook_type_for_mode
555 #define LANG_HOOKS_TYPE_FOR_SIZE go_langhook_type_for_size
556 #define LANG_HOOKS_BUILTIN_FUNCTION go_langhook_builtin_function
557 #define LANG_HOOKS_GLOBAL_BINDINGS_P go_langhook_global_bindings_p
558 #define LANG_HOOKS_PUSHDECL go_langhook_pushdecl
559 #define LANG_HOOKS_GETDECLS go_langhook_getdecls
560 #define LANG_HOOKS_GIMPLIFY_EXPR go_langhook_gimplify_expr
561 #define LANG_HOOKS_EH_PERSONALITY go_langhook_eh_personality
563 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
565 #include "gt-go-go-lang.h"
566 #include "gtype-go.h"