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