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