compiler: Store flags for division checks in Gogo object instead of using global...
[platform/upstream/gcc.git] / gcc / go / go-lang.c
1 /* go-lang.c -- Go frontend gcc interface.
2    Copyright (C) 2009-2014 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 "basic-block.h"
27 #include "gimple-expr.h"
28 #include "gimplify.h"
29 #include "stor-layout.h"
30 #include "toplev.h"
31 #include "debug.h"
32 #include "options.h"
33 #include "flags.h"
34 #include "convert.h"
35 #include "diagnostic.h"
36 #include "langhooks.h"
37 #include "langhooks-def.h"
38 #include "target.h"
39 #include "common/common-target.h"
40
41 #include <mpfr.h>
42
43 #include "go-c.h"
44
45 /* Language-dependent contents of a type.  */
46
47 struct GTY(()) lang_type
48 {
49   char dummy;
50 };
51
52 /* Language-dependent contents of a decl.  */
53
54 struct GTY((variable_size)) lang_decl
55 {
56   char dummy;
57 };
58
59 /* Language-dependent contents of an identifier.  This must include a
60    tree_identifier.  */
61
62 struct GTY(()) lang_identifier
63 {
64   struct tree_identifier common;
65 };
66
67 /* The resulting tree type.  */
68
69 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
70            chain_next ("CODE_CONTAINS_STRUCT (TREE_CODE (&%h.generic), TS_COMMON) ? ((union lang_tree_node *) TREE_CHAIN (&%h.generic)) : NULL")))
71 lang_tree_node
72 {
73   union tree_node GTY((tag ("0"),
74                        desc ("tree_node_structure (&%h)"))) generic;
75   struct lang_identifier GTY((tag ("1"))) identifier;
76 };
77
78 /* We don't use language_function.  */
79
80 struct GTY(()) language_function
81 {
82   int dummy;
83 };
84
85 /* Option information we need to pass to go_create_gogo.  */
86
87 static const char *go_pkgpath = NULL;
88 static const char *go_prefix = NULL;
89 static const char *go_relative_import_path = NULL;
90
91 /* Language hooks.  */
92
93 static bool
94 go_langhook_init (void)
95 {
96   build_common_tree_nodes (false, false);
97
98   /* I don't know why this has to be done explicitly.  */
99   void_list_node = build_tree_list (NULL_TREE, void_type_node);
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, go_pkgpath, go_prefix,
107                   go_relative_import_path, go_check_divide_zero,
108                   go_check_divide_overflow);
109
110   build_common_builtin_nodes ();
111
112   /* The default precision for floating point numbers.  This is used
113      for floating point constants with abstract type.  This may
114      eventually be controllable by a command line option.  */
115   mpfr_set_default_prec (256);
116
117   /* Go uses exceptions.  */
118   using_eh_for_cleanups ();
119
120   return true;
121 }
122
123 /* The option mask.  */
124
125 static unsigned int
126 go_langhook_option_lang_mask (void)
127 {
128   return CL_Go;
129 }
130
131 /* Initialize the options structure.  */
132
133 static void
134 go_langhook_init_options_struct (struct gcc_options *opts)
135 {
136   /* Go says that signed overflow is precisely defined.  */
137   opts->x_flag_wrapv = 1;
138
139   /* We default to using strict aliasing, since Go pointers are safe.
140      This is turned off for code that imports the "unsafe" package,
141      because using unsafe.pointer violates C style aliasing
142      requirements.  */
143   opts->x_flag_strict_aliasing = 1;
144
145   /* Default to avoiding range issues for complex multiply and
146      divide.  */
147   opts->x_flag_complex_method = 2;
148
149   /* The builtin math functions should not set errno.  */
150   opts->x_flag_errno_math = 0;
151   opts->frontend_set_flag_errno_math = true;
152
153   /* We turn on stack splitting if we can.  */
154   if (targetm_common.supports_split_stack (false, opts))
155     opts->x_flag_split_stack = 1;
156
157   /* Exceptions are used to handle recovering from panics.  */
158   opts->x_flag_exceptions = 1;
159   opts->x_flag_non_call_exceptions = 1;
160 }
161
162 /* Infrastructure for a vector of char * pointers.  */
163
164 typedef const char *go_char_p;
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> 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         go_search_dirs.safe_push (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_search_dirs, ix, dir)
268     go_add_search_path (dir);
269   go_search_dirs.release ();
270
271   if (flag_excess_precision_cmdline == EXCESS_PRECISION_DEFAULT)
272     flag_excess_precision_cmdline = EXCESS_PRECISION_STANDARD;
273
274   /* Tail call optimizations can confuse uses of runtime.Callers.  */
275   if (!global_options_set.x_flag_optimize_sibling_calls)
276     global_options.x_flag_optimize_sibling_calls = 0;
277
278   /* Returning false means that the backend should be used.  */
279   return false;
280 }
281
282 static void
283 go_langhook_parse_file (void)
284 {
285   go_parse_input_files (in_fnames, num_in_fnames, flag_syntax_only,
286                         go_require_return_statement);
287 }
288
289 static tree
290 go_langhook_type_for_size (unsigned int bits, int unsignedp)
291 {
292   tree type;
293   if (unsignedp)
294     {
295       if (bits == INT_TYPE_SIZE)
296         type = unsigned_type_node;
297       else if (bits == CHAR_TYPE_SIZE)
298         type = unsigned_char_type_node;
299       else if (bits == SHORT_TYPE_SIZE)
300         type = short_unsigned_type_node;
301       else if (bits == LONG_TYPE_SIZE)
302         type = long_unsigned_type_node;
303       else if (bits == LONG_LONG_TYPE_SIZE)
304         type = long_long_unsigned_type_node;
305       else
306         type = make_unsigned_type(bits);
307     }
308   else
309     {
310       if (bits == INT_TYPE_SIZE)
311         type = integer_type_node;
312       else if (bits == CHAR_TYPE_SIZE)
313         type = signed_char_type_node;
314       else if (bits == SHORT_TYPE_SIZE)
315         type = short_integer_type_node;
316       else if (bits == LONG_TYPE_SIZE)
317         type = long_integer_type_node;
318       else if (bits == LONG_LONG_TYPE_SIZE)
319         type = long_long_integer_type_node;
320       else
321         type = make_signed_type(bits);
322     }
323   return type;
324 }
325
326 static tree
327 go_langhook_type_for_mode (enum machine_mode mode, int unsignedp)
328 {
329   tree type;
330   /* Go has no vector types.  Build them here.  FIXME: It does not
331      make sense for the middle-end to ask the frontend for a type
332      which the frontend does not support.  However, at least for now
333      it is required.  See PR 46805.  */
334   if (VECTOR_MODE_P (mode))
335     {
336       tree inner;
337
338       inner = go_langhook_type_for_mode (GET_MODE_INNER (mode), unsignedp);
339       if (inner != NULL_TREE)
340         return build_vector_type_for_mode (inner, mode);
341       return NULL_TREE;
342     }
343
344   // FIXME: This static_cast should be in machmode.h.
345   enum mode_class mc = static_cast<enum mode_class>(GET_MODE_CLASS(mode));
346   if (mc == MODE_INT)
347     return go_langhook_type_for_size(GET_MODE_BITSIZE(mode), unsignedp);
348   else if (mc == MODE_FLOAT)
349     {
350       switch (GET_MODE_BITSIZE (mode))
351         {
352         case 32:
353           return float_type_node;
354         case 64:
355           return double_type_node;
356         default:
357           // We have to check for long double in order to support
358           // i386 excess precision.
359           if (mode == TYPE_MODE(long_double_type_node))
360             return long_double_type_node;
361         }
362     }
363   else if (mc == MODE_COMPLEX_FLOAT)
364     {
365       switch (GET_MODE_BITSIZE (mode))
366         {
367         case 64:
368           return complex_float_type_node;
369         case 128:
370           return complex_double_type_node;
371         default:
372           // We have to check for long double in order to support
373           // i386 excess precision.
374           if (mode == TYPE_MODE(complex_long_double_type_node))
375             return complex_long_double_type_node;
376         }
377     }
378
379 #if HOST_BITS_PER_WIDE_INT >= 64
380   /* The middle-end and some backends rely on TImode being supported
381      for 64-bit HWI.  */
382   if (mode == TImode)
383     {
384       type = build_nonstandard_integer_type (GET_MODE_BITSIZE (TImode),
385                                              unsignedp);
386       if (type && TYPE_MODE (type) == TImode)
387         return type;
388     }
389 #endif
390   return NULL_TREE;
391 }
392
393 /* Record a builtin function.  We just ignore builtin functions.  */
394
395 static tree
396 go_langhook_builtin_function (tree decl)
397 {
398   return decl;
399 }
400
401 /* Return true if we are in the global binding level.  */
402
403 static bool
404 go_langhook_global_bindings_p (void)
405 {
406   return current_function_decl == NULL_TREE;
407 }
408
409 /* Push a declaration into the current binding level.  We can't
410    usefully implement this since we don't want to convert from tree
411    back to one of our internal data structures.  I think the only way
412    this is used is to record a decl which is to be returned by
413    getdecls, and we could implement it for that purpose if
414    necessary.  */
415
416 static tree
417 go_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED)
418 {
419   gcc_unreachable ();
420 }
421
422 /* This hook is used to get the current list of declarations as trees.
423    We don't support that; instead we use the write_globals hook.  This
424    can't simply crash because it is called by -gstabs.  */
425
426 static tree
427 go_langhook_getdecls (void)
428 {
429   return NULL;
430 }
431
432 /* Write out globals.  */
433
434 static void
435 go_langhook_write_globals (void)
436 {
437   go_write_globals ();
438 }
439
440 /* Go specific gimplification.  We need to gimplify
441    CALL_EXPR_STATIC_CHAIN, because the gimplifier doesn't handle
442    it.  */
443
444 static int
445 go_langhook_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
446 {
447   if (TREE_CODE (*expr_p) == CALL_EXPR
448       && CALL_EXPR_STATIC_CHAIN (*expr_p) != NULL_TREE)
449     gimplify_expr (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p, post_p,
450                    is_gimple_val, fb_rvalue);
451   return GS_UNHANDLED;
452 }
453
454 /* Return a decl for the exception personality function.  The function
455    itself is implemented in libgo/runtime/go-unwind.c.  */
456
457 static tree
458 go_langhook_eh_personality (void)
459 {
460   static tree personality_decl;
461   if (personality_decl == NULL_TREE)
462     {
463       personality_decl = build_personality_function ("gccgo");
464       go_preserve_from_gc (personality_decl);
465     }
466   return personality_decl;
467 }
468
469 /* Functions called directly by the generic backend.  */
470
471 tree
472 convert (tree type, tree expr)
473 {
474   if (type == error_mark_node
475       || expr == error_mark_node
476       || TREE_TYPE (expr) == error_mark_node)
477     return error_mark_node;
478
479   if (type == TREE_TYPE (expr))
480     return expr;
481
482   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr)))
483     return fold_convert (type, expr);
484
485   switch (TREE_CODE (type))
486     {
487     case VOID_TYPE:
488     case BOOLEAN_TYPE:
489       return fold_convert (type, expr);
490     case INTEGER_TYPE:
491       return fold (convert_to_integer (type, expr));
492     case POINTER_TYPE:
493       return fold (convert_to_pointer (type, expr));
494     case REAL_TYPE:
495       return fold (convert_to_real (type, expr));
496     case COMPLEX_TYPE:
497       return fold (convert_to_complex (type, expr));
498     default:
499       break;
500     }
501
502   gcc_unreachable ();
503 }
504
505 /* FIXME: This is a hack to preserve trees that we create from the
506    garbage collector.  */
507
508 static GTY(()) tree go_gc_root;
509
510 void
511 go_preserve_from_gc (tree t)
512 {
513   go_gc_root = tree_cons (NULL_TREE, t, go_gc_root);
514 }
515
516 /* Convert an identifier for use in an error message.  */
517
518 const char *
519 go_localize_identifier (const char *ident)
520 {
521   return identifier_to_locale (ident);
522 }
523
524 #undef LANG_HOOKS_NAME
525 #undef LANG_HOOKS_INIT
526 #undef LANG_HOOKS_OPTION_LANG_MASK
527 #undef LANG_HOOKS_INIT_OPTIONS_STRUCT
528 #undef LANG_HOOKS_HANDLE_OPTION
529 #undef LANG_HOOKS_POST_OPTIONS
530 #undef LANG_HOOKS_PARSE_FILE
531 #undef LANG_HOOKS_TYPE_FOR_MODE
532 #undef LANG_HOOKS_TYPE_FOR_SIZE
533 #undef LANG_HOOKS_BUILTIN_FUNCTION
534 #undef LANG_HOOKS_GLOBAL_BINDINGS_P
535 #undef LANG_HOOKS_PUSHDECL
536 #undef LANG_HOOKS_GETDECLS
537 #undef LANG_HOOKS_WRITE_GLOBALS
538 #undef LANG_HOOKS_GIMPLIFY_EXPR
539 #undef LANG_HOOKS_EH_PERSONALITY
540
541 #define LANG_HOOKS_NAME                 "GNU Go"
542 #define LANG_HOOKS_INIT                 go_langhook_init
543 #define LANG_HOOKS_OPTION_LANG_MASK     go_langhook_option_lang_mask
544 #define LANG_HOOKS_INIT_OPTIONS_STRUCT  go_langhook_init_options_struct
545 #define LANG_HOOKS_HANDLE_OPTION        go_langhook_handle_option
546 #define LANG_HOOKS_POST_OPTIONS         go_langhook_post_options
547 #define LANG_HOOKS_PARSE_FILE           go_langhook_parse_file
548 #define LANG_HOOKS_TYPE_FOR_MODE        go_langhook_type_for_mode
549 #define LANG_HOOKS_TYPE_FOR_SIZE        go_langhook_type_for_size
550 #define LANG_HOOKS_BUILTIN_FUNCTION     go_langhook_builtin_function
551 #define LANG_HOOKS_GLOBAL_BINDINGS_P    go_langhook_global_bindings_p
552 #define LANG_HOOKS_PUSHDECL             go_langhook_pushdecl
553 #define LANG_HOOKS_GETDECLS             go_langhook_getdecls
554 #define LANG_HOOKS_WRITE_GLOBALS        go_langhook_write_globals
555 #define LANG_HOOKS_GIMPLIFY_EXPR        go_langhook_gimplify_expr
556 #define LANG_HOOKS_EH_PERSONALITY       go_langhook_eh_personality
557
558 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
559
560 #include "gt-go-go-lang.h"
561 #include "gtype-go.h"