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