]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/go/go-lang.c
poly_int: vect_permute_load/store_chain
[thirdparty/gcc.git] / gcc / go / go-lang.c
CommitLineData
7a938933 1/* go-lang.c -- Go frontend gcc interface.
85ec4feb 2 Copyright (C) 2009-2018 Free Software Foundation, Inc.
7a938933
ILT
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20#include "config.h"
21#include "system.h"
7a938933 22#include "coretypes.h"
2adfab87 23#include "target.h"
2adfab87
AM
24#include "tree.h"
25#include "gimple-expr.h"
26#include "diagnostic.h"
7a938933 27#include "opts.h"
40e23961 28#include "fold-const.h"
45b0be94 29#include "gimplify.h"
d8a2d370 30#include "stor-layout.h"
7a938933 31#include "debug.h"
7a938933 32#include "convert.h"
7a938933
ILT
33#include "langhooks.h"
34#include "langhooks-def.h"
677f3fa8 35#include "common/common-target.h"
7a938933
ILT
36
37#include <mpfr.h>
38
39#include "go-c.h"
3c061ac0 40#include "go-gcc.h"
7a938933 41
acdef7f8
TR
42#ifndef TARGET_AIX
43#define TARGET_AIX 0
44#endif
45
7a938933
ILT
46/* Language-dependent contents of a type. */
47
48struct GTY(()) lang_type
49{
50 char dummy;
51};
52
53/* Language-dependent contents of a decl. */
54
766090c2 55struct GTY(()) lang_decl
7a938933
ILT
56{
57 char dummy;
58};
59
60/* Language-dependent contents of an identifier. This must include a
61 tree_identifier. */
62
63struct GTY(()) lang_identifier
64{
65 struct tree_identifier common;
66};
67
68/* The resulting tree type. */
69
70union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
81f653d6 71 chain_next ("CODE_CONTAINS_STRUCT (TREE_CODE (&%h.generic), TS_COMMON) ? ((union lang_tree_node *) TREE_CHAIN (&%h.generic)) : NULL")))
7a938933
ILT
72lang_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
81struct GTY(()) language_function
82{
83 int dummy;
84};
85
097b12fb
ILT
86/* Option information we need to pass to go_create_gogo. */
87
88static const char *go_pkgpath = NULL;
89static const char *go_prefix = NULL;
4e1866fe 90static const char *go_relative_import_path = NULL;
2adb671d 91static const char *go_c_header = NULL;
097b12fb 92
7a938933
ILT
93/* Language hooks. */
94
95static bool
96go_langhook_init (void)
97{
a011cd92 98 build_common_tree_nodes (false);
7a938933 99
00733a00
ILT
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
7a938933
ILT
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). */
2adb671d
ILT
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;
acdef7f8 119 args.nil_check_size_threshold = TARGET_AIX ? -1 : 4096;
3c061ac0
TM
120 args.linemap = go_get_linemap();
121 args.backend = go_get_backend();
2adb671d 122 go_create_gogo (&args);
7a938933
ILT
123
124 build_common_builtin_nodes ();
125
7a938933
ILT
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. */
1ec20ea1 129 mpfr_set_default_prec (256);
7a938933
ILT
130
131 /* Go uses exceptions. */
132 using_eh_for_cleanups ();
133
134 return true;
135}
136
137/* The option mask. */
138
139static unsigned int
140go_langhook_option_lang_mask (void)
141{
142 return CL_Go;
143}
144
145/* Initialize the options structure. */
146
147static void
148go_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;
9b548472 165 opts->frontend_set_flag_errno_math = true;
7a938933 166
7a938933
ILT
167 /* Exceptions are used to handle recovering from panics. */
168 opts->x_flag_exceptions = 1;
169 opts->x_flag_non_call_exceptions = 1;
818424b2 170
ede67376
ILT
171 /* We need to keep pointers live for the garbage collector. */
172 opts->x_flag_keep_gc_roots_live = 1;
173
818424b2
ILT
174 /* Go programs expect runtime.Callers to work, and that uses
175 libbacktrace that uses debug info. Set the debug info level to 1
176 by default. In post_options we will set the debug type if the
177 debug info level was not set back to 0 on the command line. */
178 opts->x_debug_info_level = DINFO_LEVEL_TERSE;
7a938933
ILT
179}
180
9771b263 181/* Infrastructure for a vector of char * pointers. */
ac819ba5
ILT
182
183typedef const char *go_char_p;
ac819ba5
ILT
184
185/* The list of directories to search after all the Go specific
186 directories have been searched. */
187
9771b263 188static vec<go_char_p> go_search_dirs;
ac819ba5 189
7a938933
ILT
190/* Handle Go specific options. Return 0 if we didn't do anything. */
191
192static bool
193go_langhook_handle_option (
194 size_t scode,
195 const char *arg,
196 int value ATTRIBUTE_UNUSED,
197 int kind ATTRIBUTE_UNUSED,
198 location_t loc ATTRIBUTE_UNUSED,
199 const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED)
200{
201 enum opt_code code = (enum opt_code) scode;
202 bool ret = true;
203
204 switch (code)
205 {
206 case OPT_I:
7a938933
ILT
207 go_add_search_path (arg);
208 break;
209
ac819ba5
ILT
210 case OPT_L:
211 /* A -L option is assumed to come from the compiler driver.
212 This is a system directory. We search the following
213 directories, if they exist, before this one:
214 dir/go/VERSION
215 dir/go/VERSION/MACHINE
216 This is like include/c++. */
217 {
218 static const char dir_separator_str[] = { DIR_SEPARATOR, 0 };
219 size_t len;
220 char *p;
221 struct stat st;
222
223 len = strlen (arg);
224 p = XALLOCAVEC (char,
225 (len + sizeof "go" + sizeof DEFAULT_TARGET_VERSION
226 + sizeof DEFAULT_TARGET_MACHINE + 3));
227 strcpy (p, arg);
228 if (len > 0 && !IS_DIR_SEPARATOR (p[len - 1]))
229 strcat (p, dir_separator_str);
230 strcat (p, "go");
231 strcat (p, dir_separator_str);
232 strcat (p, DEFAULT_TARGET_VERSION);
233 if (stat (p, &st) == 0 && S_ISDIR (st.st_mode))
234 {
235 go_add_search_path (p);
236 strcat (p, dir_separator_str);
237 strcat (p, DEFAULT_TARGET_MACHINE);
238 if (stat (p, &st) == 0 && S_ISDIR (st.st_mode))
239 go_add_search_path (p);
240 }
241
242 /* Search ARG too, but only after we've searched to Go
243 specific directories for all -L arguments. */
9771b263 244 go_search_dirs.safe_push (arg);
ac819ba5
ILT
245 }
246 break;
247
7a938933
ILT
248 case OPT_fgo_dump_:
249 ret = go_enable_dump (arg) ? true : false;
250 break;
251
706cd57f
RL
252 case OPT_fgo_optimize_:
253 ret = go_enable_optimize (arg) ? true : false;
254 break;
255
097b12fb
ILT
256 case OPT_fgo_pkgpath_:
257 go_pkgpath = arg;
258 break;
259
7a938933 260 case OPT_fgo_prefix_:
097b12fb 261 go_prefix = arg;
7a938933
ILT
262 break;
263
4e1866fe
ILT
264 case OPT_fgo_relative_import_path_:
265 go_relative_import_path = arg;
266 break;
267
2adb671d
ILT
268 case OPT_fgo_c_header_:
269 go_c_header = arg;
270 break;
271
7a938933
ILT
272 default:
273 /* Just return 1 to indicate that the option is valid. */
274 break;
275 }
276
277 return ret;
278}
279
280/* Run after parsing options. */
281
282static bool
283go_langhook_post_options (const char **pfilename ATTRIBUTE_UNUSED)
284{
ac819ba5
ILT
285 unsigned int ix;
286 const char *dir;
287
7a938933
ILT
288 gcc_assert (num_in_fnames > 0);
289
9771b263 290 FOR_EACH_VEC_ELT (go_search_dirs, ix, dir)
ac819ba5 291 go_add_search_path (dir);
9771b263 292 go_search_dirs.release ();
ac819ba5 293
7a938933
ILT
294 if (flag_excess_precision_cmdline == EXCESS_PRECISION_DEFAULT)
295 flag_excess_precision_cmdline = EXCESS_PRECISION_STANDARD;
296
0fdd268f
ILT
297 /* Tail call optimizations can confuse uses of runtime.Callers. */
298 if (!global_options_set.x_flag_optimize_sibling_calls)
299 global_options.x_flag_optimize_sibling_calls = 0;
300
818424b2
ILT
301 /* If the debug info level is still 1, as set in init_options, make
302 sure that some debugging type is selected. */
303 if (global_options.x_debug_info_level == DINFO_LEVEL_TERSE
304 && global_options.x_write_symbols == NO_DEBUG)
305 global_options.x_write_symbols = PREFERRED_DEBUGGING_TYPE;
306
cb3306e5
AS
307 /* We turn on stack splitting if we can. */
308 if (!global_options_set.x_flag_split_stack
309 && targetm_common.supports_split_stack (false, &global_options))
310 global_options.x_flag_split_stack = 1;
311
a1f427e9
ILT
312 /* If stack splitting is turned on, and the user did not explicitly
313 request function partitioning, turn off partitioning, as it
314 confuses the linker when trying to handle partitioned split-stack
315 code that calls a non-split-stack function. */
316 if (global_options.x_flag_split_stack
317 && global_options.x_flag_reorder_blocks_and_partition
318 && !global_options_set.x_flag_reorder_blocks_and_partition)
319 global_options.x_flag_reorder_blocks_and_partition = 0;
320
7a938933
ILT
321 /* Returning false means that the backend should be used. */
322 return false;
323}
324
325static void
326go_langhook_parse_file (void)
327{
328 go_parse_input_files (in_fnames, num_in_fnames, flag_syntax_only,
329 go_require_return_statement);
d7438551
AH
330
331 /* Final processing of globals and early debug info generation. */
332 go_write_globals ();
7a938933
ILT
333}
334
335static tree
336go_langhook_type_for_size (unsigned int bits, int unsignedp)
337{
ad5b68e0
CM
338 tree type;
339 if (unsignedp)
340 {
341 if (bits == INT_TYPE_SIZE)
342 type = unsigned_type_node;
343 else if (bits == CHAR_TYPE_SIZE)
344 type = unsigned_char_type_node;
345 else if (bits == SHORT_TYPE_SIZE)
346 type = short_unsigned_type_node;
347 else if (bits == LONG_TYPE_SIZE)
348 type = long_unsigned_type_node;
349 else if (bits == LONG_LONG_TYPE_SIZE)
350 type = long_long_unsigned_type_node;
351 else
352 type = make_unsigned_type(bits);
353 }
354 else
355 {
356 if (bits == INT_TYPE_SIZE)
357 type = integer_type_node;
358 else if (bits == CHAR_TYPE_SIZE)
359 type = signed_char_type_node;
360 else if (bits == SHORT_TYPE_SIZE)
361 type = short_integer_type_node;
362 else if (bits == LONG_TYPE_SIZE)
363 type = long_integer_type_node;
364 else if (bits == LONG_LONG_TYPE_SIZE)
365 type = long_long_integer_type_node;
366 else
367 type = make_signed_type(bits);
368 }
369 return type;
7a938933
ILT
370}
371
372static tree
ef4bddc2 373go_langhook_type_for_mode (machine_mode mode, int unsignedp)
7a938933 374{
c2b13bb6 375 tree type;
0e29f7e5
ILT
376 /* Go has no vector types. Build them here. FIXME: It does not
377 make sense for the middle-end to ask the frontend for a type
378 which the frontend does not support. However, at least for now
379 it is required. See PR 46805. */
380 if (VECTOR_MODE_P (mode))
381 {
382 tree inner;
383
384 inner = go_langhook_type_for_mode (GET_MODE_INNER (mode), unsignedp);
385 if (inner != NULL_TREE)
386 return build_vector_type_for_mode (inner, mode);
387 return NULL_TREE;
388 }
389
b4206259 390 scalar_int_mode imode;
e386a52f 391 scalar_float_mode fmode;
a97390bf 392 complex_mode cmode;
b4206259
RS
393 if (is_int_mode (mode, &imode))
394 return go_langhook_type_for_size (GET_MODE_BITSIZE (imode), unsignedp);
e386a52f 395 else if (is_float_mode (mode, &fmode))
ad5b68e0 396 {
e386a52f 397 switch (GET_MODE_BITSIZE (fmode))
ad5b68e0
CM
398 {
399 case 32:
400 return float_type_node;
401 case 64:
402 return double_type_node;
403 default:
404 // We have to check for long double in order to support
405 // i386 excess precision.
e386a52f 406 if (fmode == TYPE_MODE(long_double_type_node))
ad5b68e0
CM
407 return long_double_type_node;
408 }
409 }
a97390bf 410 else if (is_complex_float_mode (mode, &cmode))
ad5b68e0 411 {
a97390bf 412 switch (GET_MODE_BITSIZE (cmode))
ad5b68e0
CM
413 {
414 case 64:
415 return complex_float_type_node;
416 case 128:
417 return complex_double_type_node;
418 default:
419 // We have to check for long double in order to support
420 // i386 excess precision.
a97390bf 421 if (cmode == TYPE_MODE(complex_long_double_type_node))
ad5b68e0
CM
422 return complex_long_double_type_node;
423 }
424 }
c2b13bb6
JJ
425
426#if HOST_BITS_PER_WIDE_INT >= 64
427 /* The middle-end and some backends rely on TImode being supported
428 for 64-bit HWI. */
429 if (mode == TImode)
430 {
431 type = build_nonstandard_integer_type (GET_MODE_BITSIZE (TImode),
432 unsignedp);
433 if (type && TYPE_MODE (type) == TImode)
434 return type;
435 }
436#endif
437 return NULL_TREE;
7a938933
ILT
438}
439
440/* Record a builtin function. We just ignore builtin functions. */
441
442static tree
443go_langhook_builtin_function (tree decl)
444{
445 return decl;
446}
447
c99c0026
EB
448/* Return true if we are in the global binding level. */
449
450static bool
7a938933
ILT
451go_langhook_global_bindings_p (void)
452{
c99c0026 453 return current_function_decl == NULL_TREE;
7a938933
ILT
454}
455
456/* Push a declaration into the current binding level. We can't
457 usefully implement this since we don't want to convert from tree
458 back to one of our internal data structures. I think the only way
459 this is used is to record a decl which is to be returned by
460 getdecls, and we could implement it for that purpose if
461 necessary. */
462
463static tree
464go_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED)
465{
466 gcc_unreachable ();
467}
468
469/* This hook is used to get the current list of declarations as trees.
470 We don't support that; instead we use the write_globals hook. This
471 can't simply crash because it is called by -gstabs. */
472
473static tree
474go_langhook_getdecls (void)
475{
476 return NULL;
477}
478
7a938933
ILT
479/* Go specific gimplification. We need to gimplify
480 CALL_EXPR_STATIC_CHAIN, because the gimplifier doesn't handle
481 it. */
482
483static int
484go_langhook_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
485{
486 if (TREE_CODE (*expr_p) == CALL_EXPR
487 && CALL_EXPR_STATIC_CHAIN (*expr_p) != NULL_TREE)
488 gimplify_expr (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p, post_p,
489 is_gimple_val, fb_rvalue);
490 return GS_UNHANDLED;
491}
492
493/* Return a decl for the exception personality function. The function
494 itself is implemented in libgo/runtime/go-unwind.c. */
495
496static tree
497go_langhook_eh_personality (void)
498{
499 static tree personality_decl;
500 if (personality_decl == NULL_TREE)
501 {
502 personality_decl = build_personality_function ("gccgo");
503 go_preserve_from_gc (personality_decl);
504 }
505 return personality_decl;
506}
507
508/* Functions called directly by the generic backend. */
509
510tree
511convert (tree type, tree expr)
512{
513 if (type == error_mark_node
514 || expr == error_mark_node
515 || TREE_TYPE (expr) == error_mark_node)
516 return error_mark_node;
517
518 if (type == TREE_TYPE (expr))
519 return expr;
520
521 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr)))
522 return fold_convert (type, expr);
523
524 switch (TREE_CODE (type))
525 {
526 case VOID_TYPE:
527 case BOOLEAN_TYPE:
528 return fold_convert (type, expr);
529 case INTEGER_TYPE:
530 return fold (convert_to_integer (type, expr));
531 case POINTER_TYPE:
532 return fold (convert_to_pointer (type, expr));
533 case REAL_TYPE:
534 return fold (convert_to_real (type, expr));
535 case COMPLEX_TYPE:
536 return fold (convert_to_complex (type, expr));
537 default:
538 break;
539 }
540
541 gcc_unreachable ();
542}
543
544/* FIXME: This is a hack to preserve trees that we create from the
545 garbage collector. */
546
547static GTY(()) tree go_gc_root;
548
549void
550go_preserve_from_gc (tree t)
551{
552 go_gc_root = tree_cons (NULL_TREE, t, go_gc_root);
553}
554
555/* Convert an identifier for use in an error message. */
556
557const char *
558go_localize_identifier (const char *ident)
559{
560 return identifier_to_locale (ident);
561}
562
563#undef LANG_HOOKS_NAME
564#undef LANG_HOOKS_INIT
565#undef LANG_HOOKS_OPTION_LANG_MASK
566#undef LANG_HOOKS_INIT_OPTIONS_STRUCT
567#undef LANG_HOOKS_HANDLE_OPTION
568#undef LANG_HOOKS_POST_OPTIONS
569#undef LANG_HOOKS_PARSE_FILE
570#undef LANG_HOOKS_TYPE_FOR_MODE
571#undef LANG_HOOKS_TYPE_FOR_SIZE
572#undef LANG_HOOKS_BUILTIN_FUNCTION
573#undef LANG_HOOKS_GLOBAL_BINDINGS_P
574#undef LANG_HOOKS_PUSHDECL
575#undef LANG_HOOKS_GETDECLS
7a938933
ILT
576#undef LANG_HOOKS_GIMPLIFY_EXPR
577#undef LANG_HOOKS_EH_PERSONALITY
578
579#define LANG_HOOKS_NAME "GNU Go"
580#define LANG_HOOKS_INIT go_langhook_init
581#define LANG_HOOKS_OPTION_LANG_MASK go_langhook_option_lang_mask
582#define LANG_HOOKS_INIT_OPTIONS_STRUCT go_langhook_init_options_struct
583#define LANG_HOOKS_HANDLE_OPTION go_langhook_handle_option
584#define LANG_HOOKS_POST_OPTIONS go_langhook_post_options
585#define LANG_HOOKS_PARSE_FILE go_langhook_parse_file
586#define LANG_HOOKS_TYPE_FOR_MODE go_langhook_type_for_mode
587#define LANG_HOOKS_TYPE_FOR_SIZE go_langhook_type_for_size
588#define LANG_HOOKS_BUILTIN_FUNCTION go_langhook_builtin_function
589#define LANG_HOOKS_GLOBAL_BINDINGS_P go_langhook_global_bindings_p
590#define LANG_HOOKS_PUSHDECL go_langhook_pushdecl
591#define LANG_HOOKS_GETDECLS go_langhook_getdecls
7a938933
ILT
592#define LANG_HOOKS_GIMPLIFY_EXPR go_langhook_gimplify_expr
593#define LANG_HOOKS_EH_PERSONALITY go_langhook_eh_personality
594
595struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
596
597#include "gt-go-go-lang.h"
598#include "gtype-go.h"