OSDN Git Service

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