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