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