]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/go/go-lang.c
re PR go/64595 (go programs abort when debug info is stripped)
[thirdparty/gcc.git] / gcc / go / go-lang.c
1 /* go-lang.c -- Go frontend gcc interface.
2 Copyright (C) 2009-2015 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "ansidecl.h"
23 #include "coretypes.h"
24 #include "opts.h"
25 #include "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "options.h"
33 #include "wide-int.h"
34 #include "inchash.h"
35 #include "tree.h"
36 #include "fold-const.h"
37 #include "tm.h"
38 #include "hard-reg-set.h"
39 #include "input.h"
40 #include "function.h"
41 #include "gimple-expr.h"
42 #include "gimplify.h"
43 #include "stor-layout.h"
44 #include "toplev.h"
45 #include "debug.h"
46 #include "options.h"
47 #include "flags.h"
48 #include "convert.h"
49 #include "diagnostic.h"
50 #include "langhooks.h"
51 #include "langhooks-def.h"
52 #include "target.h"
53 #include "common/common-target.h"
54
55 #include <mpfr.h>
56
57 #include "go-c.h"
58
59 /* Language-dependent contents of a type. */
60
61 struct GTY(()) lang_type
62 {
63 char dummy;
64 };
65
66 /* Language-dependent contents of a decl. */
67
68 struct GTY(()) lang_decl
69 {
70 char dummy;
71 };
72
73 /* Language-dependent contents of an identifier. This must include a
74 tree_identifier. */
75
76 struct GTY(()) lang_identifier
77 {
78 struct tree_identifier common;
79 };
80
81 /* The resulting tree type. */
82
83 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
84 chain_next ("CODE_CONTAINS_STRUCT (TREE_CODE (&%h.generic), TS_COMMON) ? ((union lang_tree_node *) TREE_CHAIN (&%h.generic)) : NULL")))
85 lang_tree_node
86 {
87 union tree_node GTY((tag ("0"),
88 desc ("tree_node_structure (&%h)"))) generic;
89 struct lang_identifier GTY((tag ("1"))) identifier;
90 };
91
92 /* We don't use language_function. */
93
94 struct GTY(()) language_function
95 {
96 int dummy;
97 };
98
99 /* Option information we need to pass to go_create_gogo. */
100
101 static const char *go_pkgpath = NULL;
102 static const char *go_prefix = NULL;
103 static const char *go_relative_import_path = NULL;
104
105 /* Language hooks. */
106
107 static bool
108 go_langhook_init (void)
109 {
110 build_common_tree_nodes (false, false);
111
112 /* I don't know why this has to be done explicitly. */
113 void_list_node = build_tree_list (NULL_TREE, void_type_node);
114
115 /* We must create the gogo IR after calling build_common_tree_nodes
116 (because Gogo::define_builtin_function_trees refers indirectly
117 to, e.g., unsigned_char_type_node) but before calling
118 build_common_builtin_nodes (because it calls, indirectly,
119 go_type_for_size). */
120 go_create_gogo (INT_TYPE_SIZE, POINTER_SIZE, go_pkgpath, go_prefix,
121 go_relative_import_path, go_check_divide_zero,
122 go_check_divide_overflow);
123
124 build_common_builtin_nodes ();
125
126 /* The default precision for floating point numbers. This is used
127 for floating point constants with abstract type. This may
128 eventually be controllable by a command line option. */
129 mpfr_set_default_prec (256);
130
131 /* Go uses exceptions. */
132 using_eh_for_cleanups ();
133
134 return true;
135 }
136
137 /* The option mask. */
138
139 static unsigned int
140 go_langhook_option_lang_mask (void)
141 {
142 return CL_Go;
143 }
144
145 /* Initialize the options structure. */
146
147 static void
148 go_langhook_init_options_struct (struct gcc_options *opts)
149 {
150 /* Go says that signed overflow is precisely defined. */
151 opts->x_flag_wrapv = 1;
152
153 /* We default to using strict aliasing, since Go pointers are safe.
154 This is turned off for code that imports the "unsafe" package,
155 because using unsafe.pointer violates C style aliasing
156 requirements. */
157 opts->x_flag_strict_aliasing = 1;
158
159 /* Default to avoiding range issues for complex multiply and
160 divide. */
161 opts->x_flag_complex_method = 2;
162
163 /* The builtin math functions should not set errno. */
164 opts->x_flag_errno_math = 0;
165 opts->frontend_set_flag_errno_math = true;
166
167 /* We turn on stack splitting if we can. */
168 if (targetm_common.supports_split_stack (false, opts))
169 opts->x_flag_split_stack = 1;
170
171 /* Exceptions are used to handle recovering from panics. */
172 opts->x_flag_exceptions = 1;
173 opts->x_flag_non_call_exceptions = 1;
174
175 /* Go programs expect runtime.Callers to work, and that uses
176 libbacktrace that uses debug info. Set the debug info level to 1
177 by default. In post_options we will set the debug type if the
178 debug info level was not set back to 0 on the command line. */
179 opts->x_debug_info_level = DINFO_LEVEL_TERSE;
180 }
181
182 /* Infrastructure for a vector of char * pointers. */
183
184 typedef const char *go_char_p;
185
186 /* The list of directories to search after all the Go specific
187 directories have been searched. */
188
189 static vec<go_char_p> go_search_dirs;
190
191 /* Handle Go specific options. Return 0 if we didn't do anything. */
192
193 static bool
194 go_langhook_handle_option (
195 size_t scode,
196 const char *arg,
197 int value ATTRIBUTE_UNUSED,
198 int kind ATTRIBUTE_UNUSED,
199 location_t loc ATTRIBUTE_UNUSED,
200 const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED)
201 {
202 enum opt_code code = (enum opt_code) scode;
203 bool ret = true;
204
205 switch (code)
206 {
207 case OPT_I:
208 go_add_search_path (arg);
209 break;
210
211 case OPT_L:
212 /* A -L option is assumed to come from the compiler driver.
213 This is a system directory. We search the following
214 directories, if they exist, before this one:
215 dir/go/VERSION
216 dir/go/VERSION/MACHINE
217 This is like include/c++. */
218 {
219 static const char dir_separator_str[] = { DIR_SEPARATOR, 0 };
220 size_t len;
221 char *p;
222 struct stat st;
223
224 len = strlen (arg);
225 p = XALLOCAVEC (char,
226 (len + sizeof "go" + sizeof DEFAULT_TARGET_VERSION
227 + sizeof DEFAULT_TARGET_MACHINE + 3));
228 strcpy (p, arg);
229 if (len > 0 && !IS_DIR_SEPARATOR (p[len - 1]))
230 strcat (p, dir_separator_str);
231 strcat (p, "go");
232 strcat (p, dir_separator_str);
233 strcat (p, DEFAULT_TARGET_VERSION);
234 if (stat (p, &st) == 0 && S_ISDIR (st.st_mode))
235 {
236 go_add_search_path (p);
237 strcat (p, dir_separator_str);
238 strcat (p, DEFAULT_TARGET_MACHINE);
239 if (stat (p, &st) == 0 && S_ISDIR (st.st_mode))
240 go_add_search_path (p);
241 }
242
243 /* Search ARG too, but only after we've searched to Go
244 specific directories for all -L arguments. */
245 go_search_dirs.safe_push (arg);
246 }
247 break;
248
249 case OPT_fgo_dump_:
250 ret = go_enable_dump (arg) ? true : false;
251 break;
252
253 case OPT_fgo_optimize_:
254 ret = go_enable_optimize (arg) ? true : false;
255 break;
256
257 case OPT_fgo_pkgpath_:
258 go_pkgpath = arg;
259 break;
260
261 case OPT_fgo_prefix_:
262 go_prefix = arg;
263 break;
264
265 case OPT_fgo_relative_import_path_:
266 go_relative_import_path = arg;
267 break;
268
269 default:
270 /* Just return 1 to indicate that the option is valid. */
271 break;
272 }
273
274 return ret;
275 }
276
277 /* Run after parsing options. */
278
279 static bool
280 go_langhook_post_options (const char **pfilename ATTRIBUTE_UNUSED)
281 {
282 unsigned int ix;
283 const char *dir;
284
285 gcc_assert (num_in_fnames > 0);
286
287 FOR_EACH_VEC_ELT (go_search_dirs, ix, dir)
288 go_add_search_path (dir);
289 go_search_dirs.release ();
290
291 if (flag_excess_precision_cmdline == EXCESS_PRECISION_DEFAULT)
292 flag_excess_precision_cmdline = EXCESS_PRECISION_STANDARD;
293
294 /* Tail call optimizations can confuse uses of runtime.Callers. */
295 if (!global_options_set.x_flag_optimize_sibling_calls)
296 global_options.x_flag_optimize_sibling_calls = 0;
297
298 /* If the debug info level is still 1, as set in init_options, make
299 sure that some debugging type is selected. */
300 if (global_options.x_debug_info_level == DINFO_LEVEL_TERSE
301 && global_options.x_write_symbols == NO_DEBUG)
302 global_options.x_write_symbols = PREFERRED_DEBUGGING_TYPE;
303
304 /* Returning false means that the backend should be used. */
305 return false;
306 }
307
308 static void
309 go_langhook_parse_file (void)
310 {
311 go_parse_input_files (in_fnames, num_in_fnames, flag_syntax_only,
312 go_require_return_statement);
313 }
314
315 static tree
316 go_langhook_type_for_size (unsigned int bits, int unsignedp)
317 {
318 tree type;
319 if (unsignedp)
320 {
321 if (bits == INT_TYPE_SIZE)
322 type = unsigned_type_node;
323 else if (bits == CHAR_TYPE_SIZE)
324 type = unsigned_char_type_node;
325 else if (bits == SHORT_TYPE_SIZE)
326 type = short_unsigned_type_node;
327 else if (bits == LONG_TYPE_SIZE)
328 type = long_unsigned_type_node;
329 else if (bits == LONG_LONG_TYPE_SIZE)
330 type = long_long_unsigned_type_node;
331 else
332 type = make_unsigned_type(bits);
333 }
334 else
335 {
336 if (bits == INT_TYPE_SIZE)
337 type = integer_type_node;
338 else if (bits == CHAR_TYPE_SIZE)
339 type = signed_char_type_node;
340 else if (bits == SHORT_TYPE_SIZE)
341 type = short_integer_type_node;
342 else if (bits == LONG_TYPE_SIZE)
343 type = long_integer_type_node;
344 else if (bits == LONG_LONG_TYPE_SIZE)
345 type = long_long_integer_type_node;
346 else
347 type = make_signed_type(bits);
348 }
349 return type;
350 }
351
352 static tree
353 go_langhook_type_for_mode (machine_mode mode, int unsignedp)
354 {
355 tree type;
356 /* Go has no vector types. Build them here. FIXME: It does not
357 make sense for the middle-end to ask the frontend for a type
358 which the frontend does not support. However, at least for now
359 it is required. See PR 46805. */
360 if (VECTOR_MODE_P (mode))
361 {
362 tree inner;
363
364 inner = go_langhook_type_for_mode (GET_MODE_INNER (mode), unsignedp);
365 if (inner != NULL_TREE)
366 return build_vector_type_for_mode (inner, mode);
367 return NULL_TREE;
368 }
369
370 // FIXME: This static_cast should be in machmode.h.
371 enum mode_class mc = static_cast<enum mode_class>(GET_MODE_CLASS(mode));
372 if (mc == MODE_INT)
373 return go_langhook_type_for_size(GET_MODE_BITSIZE(mode), unsignedp);
374 else if (mc == MODE_FLOAT)
375 {
376 switch (GET_MODE_BITSIZE (mode))
377 {
378 case 32:
379 return float_type_node;
380 case 64:
381 return double_type_node;
382 default:
383 // We have to check for long double in order to support
384 // i386 excess precision.
385 if (mode == TYPE_MODE(long_double_type_node))
386 return long_double_type_node;
387 }
388 }
389 else if (mc == MODE_COMPLEX_FLOAT)
390 {
391 switch (GET_MODE_BITSIZE (mode))
392 {
393 case 64:
394 return complex_float_type_node;
395 case 128:
396 return complex_double_type_node;
397 default:
398 // We have to check for long double in order to support
399 // i386 excess precision.
400 if (mode == TYPE_MODE(complex_long_double_type_node))
401 return complex_long_double_type_node;
402 }
403 }
404
405 #if HOST_BITS_PER_WIDE_INT >= 64
406 /* The middle-end and some backends rely on TImode being supported
407 for 64-bit HWI. */
408 if (mode == TImode)
409 {
410 type = build_nonstandard_integer_type (GET_MODE_BITSIZE (TImode),
411 unsignedp);
412 if (type && TYPE_MODE (type) == TImode)
413 return type;
414 }
415 #endif
416 return NULL_TREE;
417 }
418
419 /* Record a builtin function. We just ignore builtin functions. */
420
421 static tree
422 go_langhook_builtin_function (tree decl)
423 {
424 return decl;
425 }
426
427 /* Return true if we are in the global binding level. */
428
429 static bool
430 go_langhook_global_bindings_p (void)
431 {
432 return current_function_decl == NULL_TREE;
433 }
434
435 /* Push a declaration into the current binding level. We can't
436 usefully implement this since we don't want to convert from tree
437 back to one of our internal data structures. I think the only way
438 this is used is to record a decl which is to be returned by
439 getdecls, and we could implement it for that purpose if
440 necessary. */
441
442 static tree
443 go_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED)
444 {
445 gcc_unreachable ();
446 }
447
448 /* This hook is used to get the current list of declarations as trees.
449 We don't support that; instead we use the write_globals hook. This
450 can't simply crash because it is called by -gstabs. */
451
452 static tree
453 go_langhook_getdecls (void)
454 {
455 return NULL;
456 }
457
458 /* Write out globals. */
459
460 static void
461 go_langhook_write_globals (void)
462 {
463 go_write_globals ();
464 }
465
466 /* Go specific gimplification. We need to gimplify
467 CALL_EXPR_STATIC_CHAIN, because the gimplifier doesn't handle
468 it. */
469
470 static int
471 go_langhook_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
472 {
473 if (TREE_CODE (*expr_p) == CALL_EXPR
474 && CALL_EXPR_STATIC_CHAIN (*expr_p) != NULL_TREE)
475 gimplify_expr (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p, post_p,
476 is_gimple_val, fb_rvalue);
477 return GS_UNHANDLED;
478 }
479
480 /* Return a decl for the exception personality function. The function
481 itself is implemented in libgo/runtime/go-unwind.c. */
482
483 static tree
484 go_langhook_eh_personality (void)
485 {
486 static tree personality_decl;
487 if (personality_decl == NULL_TREE)
488 {
489 personality_decl = build_personality_function ("gccgo");
490 go_preserve_from_gc (personality_decl);
491 }
492 return personality_decl;
493 }
494
495 /* Functions called directly by the generic backend. */
496
497 tree
498 convert (tree type, tree expr)
499 {
500 if (type == error_mark_node
501 || expr == error_mark_node
502 || TREE_TYPE (expr) == error_mark_node)
503 return error_mark_node;
504
505 if (type == TREE_TYPE (expr))
506 return expr;
507
508 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr)))
509 return fold_convert (type, expr);
510
511 switch (TREE_CODE (type))
512 {
513 case VOID_TYPE:
514 case BOOLEAN_TYPE:
515 return fold_convert (type, expr);
516 case INTEGER_TYPE:
517 return fold (convert_to_integer (type, expr));
518 case POINTER_TYPE:
519 return fold (convert_to_pointer (type, expr));
520 case REAL_TYPE:
521 return fold (convert_to_real (type, expr));
522 case COMPLEX_TYPE:
523 return fold (convert_to_complex (type, expr));
524 default:
525 break;
526 }
527
528 gcc_unreachable ();
529 }
530
531 /* FIXME: This is a hack to preserve trees that we create from the
532 garbage collector. */
533
534 static GTY(()) tree go_gc_root;
535
536 void
537 go_preserve_from_gc (tree t)
538 {
539 go_gc_root = tree_cons (NULL_TREE, t, go_gc_root);
540 }
541
542 /* Convert an identifier for use in an error message. */
543
544 const char *
545 go_localize_identifier (const char *ident)
546 {
547 return identifier_to_locale (ident);
548 }
549
550 #undef LANG_HOOKS_NAME
551 #undef LANG_HOOKS_INIT
552 #undef LANG_HOOKS_OPTION_LANG_MASK
553 #undef LANG_HOOKS_INIT_OPTIONS_STRUCT
554 #undef LANG_HOOKS_HANDLE_OPTION
555 #undef LANG_HOOKS_POST_OPTIONS
556 #undef LANG_HOOKS_PARSE_FILE
557 #undef LANG_HOOKS_TYPE_FOR_MODE
558 #undef LANG_HOOKS_TYPE_FOR_SIZE
559 #undef LANG_HOOKS_BUILTIN_FUNCTION
560 #undef LANG_HOOKS_GLOBAL_BINDINGS_P
561 #undef LANG_HOOKS_PUSHDECL
562 #undef LANG_HOOKS_GETDECLS
563 #undef LANG_HOOKS_WRITE_GLOBALS
564 #undef LANG_HOOKS_GIMPLIFY_EXPR
565 #undef LANG_HOOKS_EH_PERSONALITY
566
567 #define LANG_HOOKS_NAME "GNU Go"
568 #define LANG_HOOKS_INIT go_langhook_init
569 #define LANG_HOOKS_OPTION_LANG_MASK go_langhook_option_lang_mask
570 #define LANG_HOOKS_INIT_OPTIONS_STRUCT go_langhook_init_options_struct
571 #define LANG_HOOKS_HANDLE_OPTION go_langhook_handle_option
572 #define LANG_HOOKS_POST_OPTIONS go_langhook_post_options
573 #define LANG_HOOKS_PARSE_FILE go_langhook_parse_file
574 #define LANG_HOOKS_TYPE_FOR_MODE go_langhook_type_for_mode
575 #define LANG_HOOKS_TYPE_FOR_SIZE go_langhook_type_for_size
576 #define LANG_HOOKS_BUILTIN_FUNCTION go_langhook_builtin_function
577 #define LANG_HOOKS_GLOBAL_BINDINGS_P go_langhook_global_bindings_p
578 #define LANG_HOOKS_PUSHDECL go_langhook_pushdecl
579 #define LANG_HOOKS_GETDECLS go_langhook_getdecls
580 #define LANG_HOOKS_WRITE_GLOBALS go_langhook_write_globals
581 #define LANG_HOOKS_GIMPLIFY_EXPR go_langhook_gimplify_expr
582 #define LANG_HOOKS_EH_PERSONALITY go_langhook_eh_personality
583
584 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
585
586 #include "gt-go-go-lang.h"
587 #include "gtype-go.h"