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