OSDN Git Service

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