]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/go/go-lang.c
Add Go frontend, libgo library, and Go testsuite.
[thirdparty/gcc.git] / gcc / go / go-lang.c
1 /* go-lang.c -- Go frontend gcc interface.
2 Copyright (C) 2009, 2010 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 ("(union lang_tree_node *) TREE_CHAIN (&%h.generic)")))
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, FLOAT_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
153 /* By default assume that floating point math does not trap. */
154 opts->x_flag_trapping_math = 0;
155
156 /* We turn on stack splitting if we can. */
157 if (targetm.supports_split_stack (false, opts))
158 opts->x_flag_split_stack = 1;
159
160 /* Exceptions are used to handle recovering from panics. */
161 opts->x_flag_exceptions = 1;
162 opts->x_flag_non_call_exceptions = 1;
163 }
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 case OPT_L:
183 /* For the compiler, we currently handle -I and -L exactly the
184 same way: they give us a directory to search for import
185 statements. */
186 go_add_search_path (arg);
187 break;
188
189 case OPT_fgo_dump_:
190 ret = go_enable_dump (arg) ? true : false;
191 break;
192
193 case OPT_fgo_prefix_:
194 go_set_prefix (arg);
195 break;
196
197 default:
198 /* Just return 1 to indicate that the option is valid. */
199 break;
200 }
201
202 return ret;
203 }
204
205 /* Run after parsing options. */
206
207 static bool
208 go_langhook_post_options (const char **pfilename ATTRIBUTE_UNUSED)
209 {
210 gcc_assert (num_in_fnames > 0);
211
212 if (flag_excess_precision_cmdline == EXCESS_PRECISION_DEFAULT)
213 flag_excess_precision_cmdline = EXCESS_PRECISION_STANDARD;
214
215 /* Returning false means that the backend should be used. */
216 return false;
217 }
218
219 static void
220 go_langhook_parse_file (void)
221 {
222 go_parse_input_files (in_fnames, num_in_fnames, flag_syntax_only,
223 go_require_return_statement);
224 }
225
226 static tree
227 go_langhook_type_for_size (unsigned int bits, int unsignedp)
228 {
229 return go_type_for_size (bits, unsignedp);
230 }
231
232 static tree
233 go_langhook_type_for_mode (enum machine_mode mode, int unsignedp)
234 {
235 return go_type_for_mode (mode, unsignedp);
236 }
237
238 /* Record a builtin function. We just ignore builtin functions. */
239
240 static tree
241 go_langhook_builtin_function (tree decl)
242 {
243 return decl;
244 }
245
246 static int
247 go_langhook_global_bindings_p (void)
248 {
249 return current_function_decl == NULL ? 1 : 0;
250 }
251
252 /* Push a declaration into the current binding level. We can't
253 usefully implement this since we don't want to convert from tree
254 back to one of our internal data structures. I think the only way
255 this is used is to record a decl which is to be returned by
256 getdecls, and we could implement it for that purpose if
257 necessary. */
258
259 static tree
260 go_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED)
261 {
262 gcc_unreachable ();
263 }
264
265 /* This hook is used to get the current list of declarations as trees.
266 We don't support that; instead we use the write_globals hook. This
267 can't simply crash because it is called by -gstabs. */
268
269 static tree
270 go_langhook_getdecls (void)
271 {
272 return NULL;
273 }
274
275 /* Write out globals. */
276
277 static void
278 go_langhook_write_globals (void)
279 {
280 go_write_globals ();
281 }
282
283 /* Go specific gimplification. We need to gimplify
284 CALL_EXPR_STATIC_CHAIN, because the gimplifier doesn't handle
285 it. */
286
287 static int
288 go_langhook_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
289 {
290 if (TREE_CODE (*expr_p) == CALL_EXPR
291 && CALL_EXPR_STATIC_CHAIN (*expr_p) != NULL_TREE)
292 gimplify_expr (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p, post_p,
293 is_gimple_val, fb_rvalue);
294 return GS_UNHANDLED;
295 }
296
297 /* Return a decl for the exception personality function. The function
298 itself is implemented in libgo/runtime/go-unwind.c. */
299
300 static tree
301 go_langhook_eh_personality (void)
302 {
303 static tree personality_decl;
304 if (personality_decl == NULL_TREE)
305 {
306 personality_decl = build_personality_function ("gccgo");
307 go_preserve_from_gc (personality_decl);
308 }
309 return personality_decl;
310 }
311
312 /* Functions called directly by the generic backend. */
313
314 tree
315 convert (tree type, tree expr)
316 {
317 if (type == error_mark_node
318 || expr == error_mark_node
319 || TREE_TYPE (expr) == error_mark_node)
320 return error_mark_node;
321
322 if (type == TREE_TYPE (expr))
323 return expr;
324
325 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr)))
326 return fold_convert (type, expr);
327
328 switch (TREE_CODE (type))
329 {
330 case VOID_TYPE:
331 case BOOLEAN_TYPE:
332 return fold_convert (type, expr);
333 case INTEGER_TYPE:
334 return fold (convert_to_integer (type, expr));
335 case POINTER_TYPE:
336 return fold (convert_to_pointer (type, expr));
337 case REAL_TYPE:
338 return fold (convert_to_real (type, expr));
339 case COMPLEX_TYPE:
340 return fold (convert_to_complex (type, expr));
341 default:
342 break;
343 }
344
345 gcc_unreachable ();
346 }
347
348 /* FIXME: This is a hack to preserve trees that we create from the
349 garbage collector. */
350
351 static GTY(()) tree go_gc_root;
352
353 void
354 go_preserve_from_gc (tree t)
355 {
356 go_gc_root = tree_cons (NULL_TREE, t, go_gc_root);
357 }
358
359 /* Convert an identifier for use in an error message. */
360
361 const char *
362 go_localize_identifier (const char *ident)
363 {
364 return identifier_to_locale (ident);
365 }
366
367 #undef LANG_HOOKS_NAME
368 #undef LANG_HOOKS_INIT
369 #undef LANG_HOOKS_OPTION_LANG_MASK
370 #undef LANG_HOOKS_INIT_OPTIONS_STRUCT
371 #undef LANG_HOOKS_HANDLE_OPTION
372 #undef LANG_HOOKS_POST_OPTIONS
373 #undef LANG_HOOKS_PARSE_FILE
374 #undef LANG_HOOKS_TYPE_FOR_MODE
375 #undef LANG_HOOKS_TYPE_FOR_SIZE
376 #undef LANG_HOOKS_BUILTIN_FUNCTION
377 #undef LANG_HOOKS_GLOBAL_BINDINGS_P
378 #undef LANG_HOOKS_PUSHDECL
379 #undef LANG_HOOKS_GETDECLS
380 #undef LANG_HOOKS_WRITE_GLOBALS
381 #undef LANG_HOOKS_GIMPLIFY_EXPR
382 #undef LANG_HOOKS_EH_PERSONALITY
383
384 #define LANG_HOOKS_NAME "GNU Go"
385 #define LANG_HOOKS_INIT go_langhook_init
386 #define LANG_HOOKS_OPTION_LANG_MASK go_langhook_option_lang_mask
387 #define LANG_HOOKS_INIT_OPTIONS_STRUCT go_langhook_init_options_struct
388 #define LANG_HOOKS_HANDLE_OPTION go_langhook_handle_option
389 #define LANG_HOOKS_POST_OPTIONS go_langhook_post_options
390 #define LANG_HOOKS_PARSE_FILE go_langhook_parse_file
391 #define LANG_HOOKS_TYPE_FOR_MODE go_langhook_type_for_mode
392 #define LANG_HOOKS_TYPE_FOR_SIZE go_langhook_type_for_size
393 #define LANG_HOOKS_BUILTIN_FUNCTION go_langhook_builtin_function
394 #define LANG_HOOKS_GLOBAL_BINDINGS_P go_langhook_global_bindings_p
395 #define LANG_HOOKS_PUSHDECL go_langhook_pushdecl
396 #define LANG_HOOKS_GETDECLS go_langhook_getdecls
397 #define LANG_HOOKS_WRITE_GLOBALS go_langhook_write_globals
398 #define LANG_HOOKS_GIMPLIFY_EXPR go_langhook_gimplify_expr
399 #define LANG_HOOKS_EH_PERSONALITY go_langhook_eh_personality
400
401 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
402
403 #include "gt-go-go-lang.h"
404 #include "gtype-go.h"