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