OSDN Git Service

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