]>
Commit | Line | Data |
---|---|---|
4e263cd9 | 1 | /* |
6cfea11b TJ |
2 | |
3 | TREELANG Compiler back end interface (treetree.c) | |
4 | Called by the parser. | |
5 | ||
6 | If you want a working example of how to write a front end to GCC, | |
7 | you are in the right place. | |
8 | ||
27f94314 TJ |
9 | Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, |
10 | 1999, 2000, 2001, 2002, 2003, Free Software Foundation, Inc. | |
6cfea11b | 11 | |
4e263cd9 AJ |
12 | This code is based on toy.c written by Richard Kenner. |
13 | ||
6cfea11b TJ |
14 | It was later modified by Jonathan Bartlett whose changes have all |
15 | been removed (by Tim Josling). | |
16 | ||
17 | Various bits and pieces were cloned from the GCC main tree, as | |
18 | GCC evolved, for COBOLForGCC, by Tim Josling. | |
19 | ||
20 | It was adapted to TREELANG by Tim Josling 2001. | |
21 | ||
22 | --------------------------------------------------------------------------- | |
23 | ||
24 | This program is free software; you can redistribute it and/or modify it | |
25 | under the terms of the GNU General Public License as published by the | |
26 | Free Software Foundation; either version 2, or (at your option) any | |
27 | later version. | |
28 | ||
29 | This program is distributed in the hope that it will be useful, | |
30 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
31 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
32 | GNU General Public License for more details. | |
33 | ||
34 | You should have received a copy of the GNU General Public License | |
35 | along with this program; if not, write to the Free Software | |
36 | Foundation, 59 Temple Place - Suite 330, | |
37 | Boston, MA 02111-1307, USA. | |
38 | ||
39 | In other words, you are welcome to use, share and improve this program. | |
40 | You are forbidden to forbid anyone else to use, share and improve | |
4e263cd9 | 41 | what you give them. Help stamp out software-hoarding! |
6cfea11b TJ |
42 | |
43 | --------------------------------------------------------------------------- | |
44 | ||
45 | */ | |
46 | ||
47 | /* | |
48 | Assumption: garbage collection is never called implicitly. It will | |
49 | not be called 'at any time' when short of memory. It will only be | |
50 | called explicitly at the end of each function. This removes the | |
51 | need for a *lot* of bother to ensure everything is in the mark trees | |
52 | at all times. */ | |
53 | ||
54 | /* Note it is OK to use GCC extensions such as long long in a compiler front end. | |
55 | This is because the GCC front ends are built using GCC. */ | |
56 | ||
6cfea11b TJ |
57 | /* GCC headers. */ |
58 | ||
6cfea11b TJ |
59 | #include "config.h" |
60 | #include "system.h" | |
4977bab6 ZW |
61 | #include "coretypes.h" |
62 | #include "tm.h" | |
6cfea11b TJ |
63 | #include "tree.h" |
64 | #include "flags.h" | |
65 | #include "output.h" | |
6cfea11b | 66 | #include "rtl.h" |
6cfea11b TJ |
67 | #include "ggc.h" |
68 | #include "toplev.h" | |
69 | #include "varray.h" | |
70 | #include "langhooks-def.h" | |
71 | #include "langhooks.h" | |
31c56a8b | 72 | #include "target.h" |
6cfea11b | 73 | |
96e3ac4f | 74 | #include "treelang.h" |
6cfea11b | 75 | #include "treetree.h" |
d79d6fea | 76 | #include "opts.h" |
6cfea11b TJ |
77 | |
78 | extern int option_main; | |
79 | extern char **file_names; | |
80 | ||
31c56a8b FH |
81 | /* Types expected by gcc's garbage collector. |
82 | These types exist to allow language front-ends to | |
83 | add extra information in gcc's parse tree data structure. | |
84 | But the treelang front end doesn't use them -- it has | |
85 | its own parse tree data structure. | |
86 | We define them here only to satisfy gcc's garbage collector. */ | |
87 | ||
88 | /* Language-specific identifier information. */ | |
89 | ||
90 | struct lang_identifier GTY(()) | |
91 | { | |
92 | struct tree_identifier common; | |
93 | }; | |
94 | ||
95 | /* Language-specific tree node information. */ | |
96 | ||
97 | union lang_tree_node | |
98 | GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"))) | |
99 | { | |
100 | union tree_node GTY ((tag ("0"), | |
101 | desc ("tree_node_structure (&%h)"))) | |
102 | generic; | |
103 | struct lang_identifier GTY ((tag ("1"))) identifier; | |
104 | }; | |
105 | ||
106 | /* Language-specific type information. */ | |
107 | ||
108 | struct lang_type GTY(()) | |
109 | { | |
110 | char junk; /* dummy field to ensure struct is not empty */ | |
111 | }; | |
112 | ||
113 | /* Language-specific declaration information. */ | |
114 | ||
115 | struct lang_decl GTY(()) | |
116 | { | |
117 | char junk; /* dummy field to ensure struct is not empty */ | |
118 | }; | |
119 | ||
120 | struct language_function GTY(()) | |
121 | { | |
122 | char junk; /* dummy field to ensure struct is not empty */ | |
123 | }; | |
124 | ||
8376cf3d AJ |
125 | static tree tree_lang_truthvalue_conversion (tree expr); |
126 | static bool tree_mark_addressable (tree exp); | |
127 | static tree tree_lang_type_for_size (unsigned precision, int unsignedp); | |
128 | static tree tree_lang_type_for_mode (enum machine_mode mode, int unsignedp); | |
129 | static tree tree_lang_unsigned_type (tree type_node); | |
130 | static tree tree_lang_signed_type (tree type_node); | |
131 | static tree tree_lang_signed_or_unsigned_type (int unsignedp, tree type); | |
31c56a8b FH |
132 | |
133 | /* XXX these should be static */ | |
8376cf3d AJ |
134 | void pushlevel (int ignore); |
135 | tree poplevel (int keep, int reverse, int functionbody); | |
136 | int global_bindings_p (void); | |
137 | void insert_block (tree block); | |
138 | void set_block (tree block); | |
139 | tree pushdecl (tree decl); | |
140 | tree getdecls (void); | |
141 | int kept_level_p (void); | |
142 | ||
143 | static void tree_push_type_decl (tree id, tree type_node); | |
144 | static void tree_push_atomic_type_decl (tree id, tree type_node); | |
31c56a8b | 145 | |
6cfea11b | 146 | /* The front end language hooks (addresses of code for this front |
31c56a8b FH |
147 | end). These are not really very language-dependent, i.e. |
148 | treelang, C, Mercury, etc. can all use almost the same definitions. */ | |
6cfea11b TJ |
149 | |
150 | #undef LANG_HOOKS_TRUTHVALUE_CONVERSION | |
31c56a8b | 151 | #define LANG_HOOKS_TRUTHVALUE_CONVERSION tree_lang_truthvalue_conversion |
6cfea11b | 152 | #undef LANG_HOOKS_MARK_ADDRESSABLE |
31c56a8b | 153 | #define LANG_HOOKS_MARK_ADDRESSABLE tree_mark_addressable |
6cfea11b | 154 | #undef LANG_HOOKS_SIGNED_TYPE |
31c56a8b | 155 | #define LANG_HOOKS_SIGNED_TYPE tree_lang_signed_type |
6cfea11b | 156 | #undef LANG_HOOKS_UNSIGNED_TYPE |
31c56a8b | 157 | #define LANG_HOOKS_UNSIGNED_TYPE tree_lang_unsigned_type |
6cfea11b | 158 | #undef LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE |
31c56a8b | 159 | #define LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE tree_lang_signed_or_unsigned_type |
6cfea11b | 160 | #undef LANG_HOOKS_TYPE_FOR_MODE |
31c56a8b | 161 | #define LANG_HOOKS_TYPE_FOR_MODE tree_lang_type_for_mode |
6cfea11b | 162 | #undef LANG_HOOKS_TYPE_FOR_SIZE |
31c56a8b | 163 | #define LANG_HOOKS_TYPE_FOR_SIZE tree_lang_type_for_size |
6cfea11b TJ |
164 | #undef LANG_HOOKS_PARSE_FILE |
165 | #define LANG_HOOKS_PARSE_FILE treelang_parse_file | |
166 | ||
167 | /* Hook routines and data unique to treelang. */ | |
168 | ||
169 | #undef LANG_HOOKS_INIT | |
170 | #define LANG_HOOKS_INIT treelang_init | |
171 | #undef LANG_HOOKS_NAME | |
172 | #define LANG_HOOKS_NAME "GNU treelang" | |
4e263cd9 | 173 | #undef LANG_HOOKS_FINISH |
6cfea11b | 174 | #define LANG_HOOKS_FINISH treelang_finish |
d7b42618 NB |
175 | #undef LANG_HOOKS_INIT_OPTIONS |
176 | #define LANG_HOOKS_INIT_OPTIONS treelang_init_options | |
7fb26bb0 NB |
177 | #undef LANG_HOOKS_HANDLE_OPTION |
178 | #define LANG_HOOKS_HANDLE_OPTION treelang_handle_option | |
6cfea11b TJ |
179 | const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER; |
180 | ||
181 | /* Tree code type/name/code tables. */ | |
182 | ||
183 | #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE, | |
184 | ||
185 | const char tree_code_type[] = { | |
186 | #include "tree.def" | |
187 | 'x' | |
188 | }; | |
189 | #undef DEFTREECODE | |
190 | ||
191 | #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH, | |
192 | ||
193 | const unsigned char tree_code_length[] = { | |
194 | #include "tree.def" | |
195 | 0 | |
196 | }; | |
197 | #undef DEFTREECODE | |
198 | ||
199 | #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME, | |
200 | ||
201 | const char *const tree_code_name[] = { | |
202 | #include "tree.def" | |
203 | "@@dummy" | |
204 | }; | |
205 | #undef DEFTREECODE | |
206 | ||
207 | /* Number of bits in int and char - accessed by front end. */ | |
208 | ||
886c07bc TJ |
209 | unsigned int tree_code_int_size = SIZEOF_INT * HOST_BITS_PER_CHAR; |
210 | ||
211 | unsigned int tree_code_char_size = HOST_BITS_PER_CHAR; | |
6cfea11b | 212 | |
6cfea11b TJ |
213 | /* Return the tree stuff for this type TYPE_NUM. */ |
214 | ||
4e263cd9 AJ |
215 | tree |
216 | tree_code_get_type (int type_num) | |
6cfea11b TJ |
217 | { |
218 | switch (type_num) | |
219 | { | |
220 | case SIGNED_CHAR: | |
221 | return signed_char_type_node; | |
4e263cd9 | 222 | |
6cfea11b TJ |
223 | case UNSIGNED_CHAR: |
224 | return unsigned_char_type_node; | |
225 | ||
226 | case SIGNED_INT: | |
227 | return integer_type_node; | |
228 | ||
229 | case UNSIGNED_INT: | |
4e263cd9 | 230 | return unsigned_type_node; |
6cfea11b TJ |
231 | |
232 | case VOID_TYPE: | |
4e263cd9 | 233 | return void_type_node; |
6cfea11b TJ |
234 | |
235 | default: | |
236 | abort (); | |
237 | } | |
238 | } | |
239 | ||
240 | /* Output the code for the start of an if statement. The test | |
241 | expression is EXP (true if not zero), and the stmt occurred at line | |
242 | LINENO in file FILENAME. */ | |
243 | ||
244 | void | |
c2e39602 | 245 | tree_code_if_start (tree exp, location_t loc) |
6cfea11b TJ |
246 | { |
247 | tree cond_exp; | |
4e263cd9 AJ |
248 | cond_exp = build (NE_EXPR, |
249 | TREE_TYPE (exp), | |
250 | exp, | |
6cfea11b | 251 | build1 (CONVERT_EXPR, TREE_TYPE (exp), integer_zero_node)); |
0cea056b | 252 | emit_line_note (loc); /* Output the line number information. */ |
0e9e1e0a | 253 | expand_start_cond (cond_exp, /* Exit-able if nonzero. */ 0); |
6cfea11b TJ |
254 | } |
255 | ||
256 | /* Output the code for the else of an if statement. The else occurred | |
257 | at line LINENO in file FILENAME. */ | |
258 | ||
4e263cd9 | 259 | void |
c2e39602 | 260 | tree_code_if_else (location_t loc) |
6cfea11b | 261 | { |
0cea056b | 262 | emit_line_note (loc); /* Output the line number information. */ |
6cfea11b TJ |
263 | expand_start_else (); |
264 | } | |
265 | ||
266 | /* Output the code for the end_if an if statement. The end_if (final brace) occurred | |
267 | at line LINENO in file FILENAME. */ | |
268 | ||
4e263cd9 | 269 | void |
c2e39602 | 270 | tree_code_if_end (location_t loc) |
6cfea11b | 271 | { |
0cea056b | 272 | emit_line_note (loc); /* Output the line number information. */ |
6cfea11b TJ |
273 | expand_end_cond (); |
274 | } | |
275 | ||
276 | /* Create a function. The prototype name is NAME, storage class is | |
277 | STORAGE_CLASS, type of return variable is RET_TYPE, parameter lists | |
278 | is PARMS, returns decl for this function. */ | |
279 | ||
4e263cd9 | 280 | tree |
6cfea11b | 281 | tree_code_create_function_prototype (unsigned char* chars, |
c2e39602 NS |
282 | unsigned int storage_class, |
283 | unsigned int ret_type, | |
284 | struct prod_token_parm_item* parms, | |
285 | location_t loc) | |
6cfea11b TJ |
286 | { |
287 | ||
288 | tree id; | |
96e3ac4f | 289 | struct prod_token_parm_item* parm; |
6cfea11b TJ |
290 | tree type_list = NULL_TREE; |
291 | tree type_node; | |
292 | tree fn_type; | |
293 | tree fn_decl; | |
294 | ||
295 | /* Build the type. */ | |
296 | id = get_identifier ((const char*)chars); | |
96e3ac4f | 297 | for (parm = parms; parm; parm = parm->tp.par.next) |
6cfea11b | 298 | { |
27f94314 TJ |
299 | if (parm->category != parameter_category) |
300 | abort (); | |
6cfea11b TJ |
301 | type_node = get_type_for_numeric_type (parm->type); |
302 | type_list = tree_cons (NULL_TREE, type_node, type_list); | |
303 | } | |
a3b5decf | 304 | /* Last parm if void indicates fixed length list (as opposed to |
6cfea11b TJ |
305 | printf style va_* list). */ |
306 | type_list = tree_cons (NULL_TREE, void_type_node, type_list); | |
307 | /* The back end needs them in reverse order. */ | |
308 | type_list = nreverse (type_list); | |
309 | ||
310 | type_node = get_type_for_numeric_type (ret_type); | |
311 | fn_type = build_function_type (type_node, type_list); | |
312 | ||
313 | id = get_identifier ((const char*)chars); | |
314 | fn_decl = build_decl (FUNCTION_DECL, id, fn_type); | |
315 | ||
f31686a3 RH |
316 | DECL_CONTEXT (fn_decl) = NULL_TREE; /* Nested functions not supported here. */ |
317 | DECL_SOURCE_LOCATION (fn_decl) = loc; | |
6cfea11b TJ |
318 | |
319 | TREE_USED (fn_decl) = 1; | |
320 | ||
321 | /* Real name (optional). */ | |
322 | SET_DECL_ASSEMBLER_NAME (fn_decl, DECL_NAME (fn_decl)); | |
4e263cd9 | 323 | |
6cfea11b | 324 | TREE_PUBLIC (fn_decl) = 0; |
4e263cd9 AJ |
325 | DECL_EXTERNAL (fn_decl) = 0; |
326 | TREE_STATIC (fn_decl) = 0; | |
6cfea11b TJ |
327 | switch (storage_class) |
328 | { | |
329 | case STATIC_STORAGE: | |
4e263cd9 | 330 | TREE_PUBLIC (fn_decl) = 0; |
6cfea11b TJ |
331 | break; |
332 | ||
333 | case EXTERNAL_DEFINITION_STORAGE: | |
334 | TREE_PUBLIC (fn_decl) = 1; | |
4e263cd9 | 335 | TREE_STATIC (fn_decl) = 0; |
6cfea11b TJ |
336 | DECL_EXTERNAL (fn_decl) = 0; |
337 | break; | |
4e263cd9 | 338 | |
6cfea11b | 339 | case EXTERNAL_REFERENCE_STORAGE: |
4e263cd9 | 340 | TREE_PUBLIC (fn_decl) = 0; |
6cfea11b TJ |
341 | DECL_EXTERNAL (fn_decl) = 1; |
342 | break; | |
343 | ||
344 | ||
345 | case AUTOMATIC_STORAGE: | |
346 | default: | |
347 | abort (); | |
348 | } | |
349 | ||
350 | /* Process declaration of function defined elsewhere. */ | |
351 | rest_of_decl_compilation (fn_decl, NULL, 1, 0); | |
352 | ||
353 | return fn_decl; | |
354 | } | |
355 | ||
356 | ||
357 | /* Output code for start of function; the decl of the function is in | |
358 | PREV_SAVED (as created by tree_code_create_function_prototype), | |
359 | the function is at line number LINENO in file FILENAME. The | |
360 | parameter details are in the lists PARMS. Returns nothing. */ | |
4e263cd9 AJ |
361 | void |
362 | tree_code_create_function_initial (tree prev_saved, | |
c2e39602 NS |
363 | location_t loc, |
364 | struct prod_token_parm_item* parms) | |
6cfea11b TJ |
365 | { |
366 | tree fn_decl; | |
367 | tree param_decl; | |
368 | tree next_param; | |
369 | tree first_param; | |
370 | tree parm_decl; | |
371 | tree parm_list; | |
372 | tree resultdecl; | |
4e263cd9 | 373 | struct prod_token_parm_item* this_parm; |
96e3ac4f | 374 | struct prod_token_parm_item* parm; |
6cfea11b TJ |
375 | |
376 | fn_decl = prev_saved; | |
377 | if (!fn_decl) | |
378 | abort (); | |
379 | ||
380 | /* Output message if not -quiet. */ | |
381 | announce_function (fn_decl); | |
382 | ||
383 | /* This has something to do with forcing output also. */ | |
384 | pushdecl (fn_decl); | |
385 | ||
386 | /* Set current function for error msgs etc. */ | |
387 | current_function_decl = fn_decl; | |
388 | DECL_INITIAL (fn_decl) = error_mark_node; | |
389 | ||
f31686a3 | 390 | DECL_SOURCE_LOCATION (fn_decl) = loc; |
6cfea11b TJ |
391 | |
392 | /* Prepare creation of rtl for a new function. */ | |
4e263cd9 | 393 | |
886c07bc TJ |
394 | resultdecl = DECL_RESULT (fn_decl) |
395 | = build_decl (RESULT_DECL, NULL_TREE, TREE_TYPE (TREE_TYPE (fn_decl))); | |
6cfea11b | 396 | DECL_CONTEXT (DECL_RESULT (fn_decl)) = fn_decl; |
f31686a3 | 397 | DECL_SOURCE_LOCATION (resultdecl) = loc; |
c2e39602 | 398 | |
6cfea11b TJ |
399 | /* Work out the size. ??? is this needed. */ |
400 | layout_decl (DECL_RESULT (fn_decl), 0); | |
401 | ||
402 | /* Make the argument variable decls. */ | |
403 | parm_list = NULL_TREE; | |
96e3ac4f | 404 | for (parm = parms; parm; parm = parm->tp.par.next) |
6cfea11b | 405 | { |
4e263cd9 AJ |
406 | parm_decl = build_decl (PARM_DECL, get_identifier |
407 | ((const char*) (parm->tp.par.variable_name)), | |
96e3ac4f | 408 | get_type_for_numeric_type (parm->type)); |
4e263cd9 | 409 | |
6cfea11b TJ |
410 | /* Some languages have different nominal and real types. */ |
411 | DECL_ARG_TYPE (parm_decl) = TREE_TYPE (parm_decl); | |
412 | if (!DECL_ARG_TYPE (parm_decl)) | |
413 | abort (); | |
414 | if (!fn_decl) | |
415 | abort (); | |
416 | DECL_CONTEXT (parm_decl) = fn_decl; | |
f31686a3 | 417 | DECL_SOURCE_LOCATION (parm_decl) = loc; |
6cfea11b TJ |
418 | parm_list = chainon (parm_decl, parm_list); |
419 | } | |
420 | ||
421 | /* Back into reverse order as the back end likes them. */ | |
422 | parm_list = nreverse (parm_list); | |
4e263cd9 | 423 | |
6cfea11b TJ |
424 | DECL_ARGUMENTS (fn_decl) = parm_list; |
425 | ||
426 | /* Save the decls for use when the args are referred to. */ | |
427 | for (param_decl = DECL_ARGUMENTS (fn_decl), | |
428 | this_parm = parms; | |
429 | param_decl; | |
430 | param_decl = TREE_CHAIN (param_decl), | |
96e3ac4f | 431 | this_parm = this_parm->tp.par.next) |
6cfea11b TJ |
432 | { |
433 | if (!this_parm) | |
434 | abort (); /* Too few. */ | |
96e3ac4f | 435 | *this_parm->tp.par.where_to_put_var_tree = param_decl; |
6cfea11b TJ |
436 | } |
437 | if (this_parm) | |
438 | abort (); /* Too many. */ | |
439 | ||
440 | /* Output the decl rtl (not the rtl for the function code). ???. | |
441 | If the function is not defined in this file, when should you | |
442 | execute this? */ | |
443 | make_decl_rtl (fn_decl, NULL); | |
444 | ||
ee6b0296 | 445 | init_function_start (fn_decl); |
4e263cd9 | 446 | |
6cfea11b | 447 | /* Create rtl for startup code of function, such as saving registers. */ |
4e263cd9 | 448 | |
6cfea11b | 449 | expand_function_start (fn_decl, 0); |
4e263cd9 | 450 | |
6cfea11b TJ |
451 | /* Function.c requires a push at the start of the function. that |
452 | looks like a bug to me but let's make it happy. */ | |
4e263cd9 | 453 | |
6cfea11b | 454 | (*lang_hooks.decls.pushlevel) (0); |
4e263cd9 | 455 | |
6cfea11b | 456 | /* Create rtl for the start of a new scope. */ |
4e263cd9 | 457 | |
6cfea11b TJ |
458 | expand_start_bindings (2); |
459 | ||
460 | /* Put the parameters into the symbol table. */ | |
4e263cd9 | 461 | |
6cfea11b TJ |
462 | for (first_param = param_decl = nreverse (DECL_ARGUMENTS (fn_decl)); |
463 | param_decl; | |
464 | param_decl = next_param) | |
465 | { | |
466 | next_param = TREE_CHAIN (param_decl); | |
467 | TREE_CHAIN (param_decl) = NULL; | |
468 | /* layout_decl (param_decl, 0); Already done in build_decl tej 13/4/2002. */ | |
469 | pushdecl (param_decl); | |
470 | if (DECL_CONTEXT (param_decl) != current_function_decl) | |
471 | abort (); | |
472 | } | |
473 | ||
474 | /* Store back the PARM_DECL nodes. They appear in the right order. */ | |
475 | DECL_ARGUMENTS (fn_decl) = getdecls (); | |
476 | ||
477 | /* Force it to be output, else may be solely inlined. */ | |
478 | TREE_ADDRESSABLE (fn_decl) = 1; | |
4e263cd9 | 479 | |
6cfea11b TJ |
480 | /* Stop -O3 from deleting it. */ |
481 | TREE_USED (fn_decl) = 1; | |
482 | ||
483 | /* Add a new level to the debugger symbol table. */ | |
4e263cd9 | 484 | |
6cfea11b | 485 | (*lang_hooks.decls.pushlevel) (0); |
4e263cd9 | 486 | |
6cfea11b | 487 | /* Create rtl for the start of a new scope. */ |
4e263cd9 | 488 | |
6cfea11b | 489 | expand_start_bindings (0); |
4e263cd9 | 490 | |
0cea056b | 491 | emit_line_note (loc); /* Output the line number information. */ |
6cfea11b TJ |
492 | } |
493 | ||
494 | /* Wrapup a function contained in file FILENAME, ending at line LINENO. */ | |
4e263cd9 | 495 | void |
c2e39602 | 496 | tree_code_create_function_wrapup (location_t loc) |
6cfea11b TJ |
497 | { |
498 | tree block; | |
499 | tree fn_decl; | |
500 | ||
501 | fn_decl = current_function_decl; | |
4e263cd9 | 502 | |
0cea056b | 503 | emit_line_note (loc); /* Output the line number information. */ |
6cfea11b TJ |
504 | |
505 | /* Get completely built level from debugger symbol table. */ | |
4e263cd9 | 506 | |
6cfea11b | 507 | block = (*lang_hooks.decls.poplevel) (1, 0, 0); |
4e263cd9 | 508 | |
6cfea11b | 509 | /* Emit rtl for end of scope. */ |
4e263cd9 | 510 | |
6cfea11b | 511 | expand_end_bindings (block, 0, 1); |
4e263cd9 | 512 | |
6cfea11b | 513 | /* Emit rtl for end of function. */ |
4e263cd9 | 514 | |
1f9cc6db | 515 | expand_function_end (); |
4e263cd9 | 516 | |
6cfea11b TJ |
517 | /* Pop the level. */ |
518 | ||
519 | block = (*lang_hooks.decls.poplevel) (1, 0, 1); | |
520 | ||
521 | /* And attach it to the function. */ | |
4e263cd9 | 522 | |
6cfea11b | 523 | DECL_INITIAL (fn_decl) = block; |
4e263cd9 | 524 | |
6cfea11b | 525 | /* Emit rtl for end of scope. */ |
4e263cd9 | 526 | |
6cfea11b | 527 | expand_end_bindings (block, 0, 1); |
4e263cd9 | 528 | |
6cfea11b | 529 | /* Call optimization and convert optimized rtl to assembly code. */ |
4e263cd9 | 530 | |
6cfea11b | 531 | rest_of_compilation (fn_decl); |
4e263cd9 | 532 | |
6cfea11b | 533 | /* We are not inside of any scope now. */ |
4e263cd9 | 534 | |
6cfea11b TJ |
535 | current_function_decl = NULL_TREE; |
536 | } | |
537 | ||
4e263cd9 AJ |
538 | /* |
539 | Create a variable. | |
540 | ||
541 | The storage class is STORAGE_CLASS (eg LOCAL). | |
542 | The name is CHARS/LENGTH. | |
543 | The type is EXPRESSION_TYPE (eg UNSIGNED_TYPE). | |
544 | The init tree is INIT. | |
6cfea11b TJ |
545 | */ |
546 | ||
4e263cd9 | 547 | tree |
6cfea11b | 548 | tree_code_create_variable (unsigned int storage_class, |
c2e39602 NS |
549 | unsigned char* chars, |
550 | unsigned int length, | |
551 | unsigned int expression_type, | |
552 | tree init, | |
553 | location_t loc) | |
6cfea11b TJ |
554 | { |
555 | tree var_type; | |
556 | tree var_id; | |
557 | tree var_decl; | |
558 | ||
559 | /* 1. Build the type. */ | |
560 | var_type = get_type_for_numeric_type (expression_type); | |
561 | ||
562 | /* 2. Build the name. */ | |
563 | if (chars[length] != 0) | |
564 | abort (); /* Should be null terminated. */ | |
565 | ||
566 | var_id = get_identifier ((const char*)chars); | |
567 | ||
568 | /* 3. Build the decl and set up init. */ | |
569 | var_decl = build_decl (VAR_DECL, var_id, var_type); | |
570 | ||
571 | /* 3a. Initialization. */ | |
572 | if (init) | |
573 | DECL_INITIAL (var_decl) = build1 (CONVERT_EXPR, var_type, init); | |
574 | else | |
575 | DECL_INITIAL (var_decl) = NULL_TREE; | |
4e263cd9 | 576 | |
6cfea11b TJ |
577 | /* 4. Compute size etc. */ |
578 | layout_decl (var_decl, 0); | |
4e263cd9 | 579 | |
6cfea11b TJ |
580 | if (TYPE_SIZE (var_type) == 0) |
581 | abort (); /* Did not calculate size. */ | |
582 | ||
583 | DECL_CONTEXT (var_decl) = current_function_decl; | |
584 | ||
f31686a3 | 585 | DECL_SOURCE_LOCATION (var_decl) = loc; |
6cfea11b TJ |
586 | |
587 | /* Set the storage mode and whether only visible in the same file. */ | |
588 | switch (storage_class) | |
589 | { | |
590 | case STATIC_STORAGE: | |
591 | TREE_STATIC (var_decl) = 1; | |
592 | TREE_PUBLIC (var_decl) = 0; | |
593 | break; | |
594 | ||
595 | case AUTOMATIC_STORAGE: | |
596 | TREE_STATIC (var_decl) = 0; | |
597 | TREE_PUBLIC (var_decl) = 0; | |
598 | break; | |
4e263cd9 | 599 | |
6cfea11b | 600 | case EXTERNAL_DEFINITION_STORAGE: |
4e263cd9 | 601 | TREE_STATIC (var_decl) = 0; |
6cfea11b TJ |
602 | TREE_PUBLIC (var_decl) = 1; |
603 | break; | |
4e263cd9 | 604 | |
6cfea11b TJ |
605 | case EXTERNAL_REFERENCE_STORAGE: |
606 | DECL_EXTERNAL (var_decl) = 1; | |
607 | TREE_PUBLIC (var_decl) = 0; | |
608 | break; | |
4e263cd9 | 609 | |
6cfea11b TJ |
610 | default: |
611 | abort (); | |
612 | } | |
4e263cd9 | 613 | |
6cfea11b TJ |
614 | /* This should really only be set if the variable is used. */ |
615 | TREE_USED (var_decl) = 1; | |
4e263cd9 | 616 | |
6cfea11b | 617 | /* Expand declaration and initial value if any. */ |
4e263cd9 AJ |
618 | |
619 | if (TREE_STATIC (var_decl)) | |
6cfea11b TJ |
620 | rest_of_decl_compilation (var_decl, 0, 0, 0); |
621 | else | |
622 | { | |
623 | expand_decl (var_decl); | |
624 | if (DECL_INITIAL (var_decl)) | |
625 | expand_decl_init (var_decl); | |
626 | } | |
4e263cd9 | 627 | |
6cfea11b | 628 | return pushdecl (copy_node (var_decl)); |
4e263cd9 | 629 | |
6cfea11b TJ |
630 | } |
631 | ||
632 | ||
633 | /* Generate code for return statement. Type is in TYPE, expression | |
634 | is in EXP if present. */ | |
635 | ||
636 | void | |
637 | tree_code_generate_return (tree type, tree exp) | |
638 | { | |
639 | tree setret; | |
640 | tree param; | |
641 | ||
642 | for (param = DECL_ARGUMENTS (current_function_decl); | |
643 | param; | |
644 | param = TREE_CHAIN (param)) | |
645 | { | |
646 | if (DECL_CONTEXT (param) != current_function_decl) | |
647 | abort (); | |
648 | } | |
649 | ||
650 | if (exp) | |
651 | { | |
4e263cd9 | 652 | setret = build (MODIFY_EXPR, type, DECL_RESULT (current_function_decl), |
6cfea11b TJ |
653 | build1 (CONVERT_EXPR, type, exp)); |
654 | TREE_SIDE_EFFECTS (setret) = 1; | |
655 | TREE_USED (setret) = 1; | |
656 | expand_expr_stmt (setret); | |
657 | } | |
658 | expand_return (DECL_RESULT (current_function_decl)); | |
659 | } | |
660 | ||
661 | /* Output the code for this expression statement CODE. */ | |
662 | ||
663 | ||
4e263cd9 | 664 | void |
0cea056b | 665 | tree_code_output_expression_statement (tree code, location_t loc) |
6cfea11b TJ |
666 | { |
667 | /* Output the line number information. */ | |
0cea056b | 668 | emit_line_note (loc); |
6cfea11b TJ |
669 | TREE_USED (code) = 1; |
670 | TREE_SIDE_EFFECTS (code) = 1; | |
671 | expand_expr_stmt (code); | |
672 | } | |
673 | ||
674 | /* Return a tree for a constant integer value in the token TOK. No | |
675 | size checking is done. */ | |
676 | ||
4e263cd9 | 677 | tree |
6cfea11b TJ |
678 | tree_code_get_integer_value (unsigned char* chars, unsigned int length) |
679 | { | |
680 | long long int val = 0; | |
681 | unsigned int ix; | |
682 | unsigned int start = 0; | |
683 | int negative = 1; | |
684 | switch (chars[0]) | |
685 | { | |
686 | case (unsigned char)'-': | |
687 | negative = -1; | |
688 | start = 1; | |
689 | break; | |
690 | ||
691 | case (unsigned char)'+': | |
692 | start = 1; | |
693 | break; | |
694 | ||
695 | default: | |
696 | break; | |
697 | } | |
698 | for (ix = start; ix < length; ix++) | |
699 | val = val * 10 + chars[ix] - (unsigned char)'0'; | |
700 | val = val*negative; | |
701 | return build_int_2 (val & 0xffffffff, (val >> 32) & 0xffffffff); | |
702 | } | |
703 | ||
704 | /* Return the tree for an expresssion, type EXP_TYPE (see treetree.h) | |
705 | with tree type TYPE and with operands1 OP1, OP2 (maybe), OP3 (maybe). */ | |
4e263cd9 AJ |
706 | tree |
707 | tree_code_get_expression (unsigned int exp_type, | |
8376cf3d AJ |
708 | tree type, tree op1, tree op2, |
709 | tree op3 ATTRIBUTE_UNUSED) | |
6cfea11b TJ |
710 | { |
711 | tree ret1; | |
712 | int operator; | |
713 | ||
714 | switch (exp_type) | |
715 | { | |
716 | case EXP_ASSIGN: | |
717 | if (!op1 || !op2) | |
718 | abort (); | |
719 | operator = MODIFY_EXPR; | |
4e263cd9 AJ |
720 | ret1 = build (operator, type, |
721 | op1, | |
6cfea11b TJ |
722 | build1 (CONVERT_EXPR, type, op2)); |
723 | ||
724 | break; | |
725 | ||
726 | case EXP_PLUS: | |
727 | operator = PLUS_EXPR; | |
728 | goto binary_expression; | |
4e263cd9 | 729 | |
6cfea11b TJ |
730 | case EXP_MINUS: |
731 | operator = MINUS_EXPR; | |
732 | goto binary_expression; | |
4e263cd9 | 733 | |
6cfea11b TJ |
734 | case EXP_EQUALS: |
735 | operator = EQ_EXPR; | |
736 | goto binary_expression; | |
4e263cd9 | 737 | |
6cfea11b TJ |
738 | /* Expand a binary expression. Ensure the operands are the right type. */ |
739 | binary_expression: | |
740 | if (!op1 || !op2) | |
741 | abort (); | |
4e263cd9 AJ |
742 | ret1 = build (operator, type, |
743 | build1 (CONVERT_EXPR, type, op1), | |
6cfea11b TJ |
744 | build1 (CONVERT_EXPR, type, op2)); |
745 | break; | |
746 | ||
747 | /* Reference to a variable. This is dead easy, just return the | |
748 | decl for the variable. If the TYPE is different than the | |
749 | variable type, convert it. */ | |
750 | case EXP_REFERENCE: | |
751 | if (!op1) | |
752 | abort (); | |
753 | if (type == TREE_TYPE (op1)) | |
754 | ret1 = op1; | |
755 | else | |
756 | ret1 = build1 (CONVERT_EXPR, type, op1); | |
757 | break; | |
4e263cd9 | 758 | |
6cfea11b TJ |
759 | case EXP_FUNCTION_INVOCATION: |
760 | if (!op1 || !op2) | |
761 | abort (); | |
762 | { | |
763 | tree fun_ptr; | |
764 | fun_ptr = build1 (ADDR_EXPR, build_pointer_type (type), op1); | |
765 | ret1 = build (CALL_EXPR, type, fun_ptr, nreverse (op2)); | |
766 | } | |
767 | break; | |
768 | ||
769 | default: | |
770 | abort (); | |
771 | } | |
4e263cd9 | 772 | |
6cfea11b TJ |
773 | return ret1; |
774 | } | |
775 | ||
776 | /* Init parameter list and return empty list. */ | |
777 | ||
4e263cd9 | 778 | tree |
6cfea11b TJ |
779 | tree_code_init_parameters (void) |
780 | { | |
781 | return NULL_TREE; | |
782 | } | |
783 | ||
784 | /* Add a parameter EXP whose expression type is EXP_PROTO to list | |
785 | LIST, returning the new list. */ | |
786 | ||
4e263cd9 | 787 | tree |
6cfea11b TJ |
788 | tree_code_add_parameter (tree list, tree proto_exp, tree exp) |
789 | { | |
790 | tree new_exp; | |
4e263cd9 | 791 | new_exp = tree_cons (NULL_TREE, |
6cfea11b TJ |
792 | build1 (CONVERT_EXPR, TREE_TYPE (proto_exp), exp), |
793 | NULL_TREE); | |
794 | if (!list) | |
795 | return new_exp; | |
796 | return chainon (new_exp, list); | |
797 | } | |
798 | ||
799 | /* Get the tree type for this type whose number is NUMERIC_TYPE. */ | |
800 | ||
801 | tree | |
802 | get_type_for_numeric_type (unsigned int numeric_type) | |
803 | { | |
4e263cd9 | 804 | |
6cfea11b TJ |
805 | int size1; |
806 | int sign1; | |
807 | switch (numeric_type) | |
808 | { | |
809 | case VOID_TYPE: | |
810 | return void_type_node; | |
4e263cd9 | 811 | |
6cfea11b TJ |
812 | case SIGNED_INT: |
813 | size1 = tree_code_int_size; | |
814 | sign1 = 1; | |
815 | break; | |
4e263cd9 | 816 | |
6cfea11b TJ |
817 | case UNSIGNED_INT: |
818 | size1 = tree_code_int_size; | |
819 | sign1 = 0; | |
820 | break; | |
4e263cd9 | 821 | |
6cfea11b TJ |
822 | case SIGNED_CHAR: |
823 | size1 = tree_code_char_size; | |
824 | sign1 = 1; | |
825 | break; | |
4e263cd9 | 826 | |
6cfea11b TJ |
827 | case UNSIGNED_CHAR: |
828 | size1 = tree_code_char_size; | |
829 | sign1 = 0; | |
830 | break; | |
4e263cd9 | 831 | |
6cfea11b TJ |
832 | default: |
833 | abort (); | |
834 | } | |
835 | ||
836 | return tree_code_get_numeric_type (size1, sign1); | |
4e263cd9 | 837 | |
6cfea11b TJ |
838 | } |
839 | ||
840 | /* Return tree representing a numeric type of size SIZE1 bits and | |
841 | signed if SIGN1 != 0. */ | |
4e263cd9 | 842 | tree |
6cfea11b TJ |
843 | tree_code_get_numeric_type (unsigned int size1, unsigned int sign1) |
844 | { | |
845 | tree ret1; | |
886c07bc TJ |
846 | if (!size1) |
847 | abort (); | |
6cfea11b TJ |
848 | if (size1 == tree_code_int_size) |
849 | { | |
850 | if (sign1) | |
851 | ret1 = integer_type_node; | |
852 | else | |
853 | ret1 = unsigned_type_node; | |
854 | } | |
855 | else | |
856 | if (size1 == tree_code_char_size) | |
857 | { | |
858 | if (sign1) | |
859 | ret1 = signed_char_type_node; | |
860 | else | |
861 | ret1 = unsigned_char_type_node; | |
862 | } | |
4e263cd9 | 863 | else |
6cfea11b | 864 | abort (); |
4e263cd9 AJ |
865 | |
866 | return ret1; | |
6cfea11b TJ |
867 | } |
868 | ||
31c56a8b FH |
869 | /* Get a stringpool entry for a string S of length L. This is needed |
870 | because the GTY routines don't mark strings, forcing you to put | |
871 | them into stringpool, which is never freed. */ | |
6cfea11b | 872 | |
31c56a8b FH |
873 | const char* |
874 | get_string (const char *s, size_t l) | |
6cfea11b | 875 | { |
31c56a8b FH |
876 | tree t; |
877 | t = get_identifier_with_length (s, l); | |
878 | return IDENTIFIER_POINTER(t); | |
6cfea11b | 879 | } |
31c56a8b FH |
880 | |
881 | /* Save typing debug_tree all the time. Dump a tree T pretty and | |
882 | concise. */ | |
6cfea11b | 883 | |
31c56a8b | 884 | void dt (tree t); |
e5bc58d3 | 885 | |
6cfea11b | 886 | void |
31c56a8b | 887 | dt (tree t) |
6cfea11b | 888 | { |
31c56a8b | 889 | debug_tree (t); |
6cfea11b TJ |
890 | } |
891 | ||
31c56a8b | 892 | /* Routines Expected by gcc: */ |
6cfea11b | 893 | |
31c56a8b FH |
894 | /* These are used to build types for various sizes. The code below |
895 | is a simplified version of that of GNAT. */ | |
a3b5decf | 896 | |
31c56a8b FH |
897 | #ifndef MAX_BITS_PER_WORD |
898 | #define MAX_BITS_PER_WORD BITS_PER_WORD | |
899 | #endif | |
22362755 | 900 | |
31c56a8b FH |
901 | /* This variable keeps a table for types for each precision so that we only |
902 | allocate each of them once. Signed and unsigned types are kept separate. */ | |
903 | static GTY(()) tree signed_and_unsigned_types[MAX_BITS_PER_WORD + 1][2]; | |
22362755 | 904 | |
31c56a8b FH |
905 | /* XXX is this definition OK? */ |
906 | static tree | |
8376cf3d | 907 | tree_lang_truthvalue_conversion (tree expr) |
22362755 | 908 | { |
31c56a8b | 909 | return expr; |
22362755 TJ |
910 | } |
911 | ||
31c56a8b FH |
912 | /* Mark EXP saying that we need to be able to take the |
913 | address of it; it should not be allocated in a register. | |
914 | Value is 1 if successful. | |
915 | ||
916 | This implementation was copied from c-decl.c. */ | |
6cfea11b | 917 | |
31c56a8b | 918 | static bool |
8376cf3d | 919 | tree_mark_addressable (tree exp) |
6cfea11b | 920 | { |
31c56a8b FH |
921 | register tree x = exp; |
922 | while (1) | |
923 | switch (TREE_CODE (x)) | |
924 | { | |
925 | case COMPONENT_REF: | |
926 | case ADDR_EXPR: | |
927 | case ARRAY_REF: | |
928 | case REALPART_EXPR: | |
929 | case IMAGPART_EXPR: | |
930 | x = TREE_OPERAND (x, 0); | |
931 | break; | |
932 | ||
933 | case CONSTRUCTOR: | |
934 | TREE_ADDRESSABLE (x) = 1; | |
935 | return 1; | |
936 | ||
937 | case VAR_DECL: | |
938 | case CONST_DECL: | |
939 | case PARM_DECL: | |
940 | case RESULT_DECL: | |
941 | if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x) | |
942 | && DECL_NONLOCAL (x)) | |
943 | { | |
944 | if (TREE_PUBLIC (x)) | |
945 | { | |
946 | error ("global register variable `%s' used in nested function", | |
947 | IDENTIFIER_POINTER (DECL_NAME (x))); | |
948 | return 0; | |
949 | } | |
950 | pedwarn ("register variable `%s' used in nested function", | |
951 | IDENTIFIER_POINTER (DECL_NAME (x))); | |
952 | } | |
953 | else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)) | |
954 | { | |
955 | if (TREE_PUBLIC (x)) | |
956 | { | |
957 | error ("address of global register variable `%s' requested", | |
958 | IDENTIFIER_POINTER (DECL_NAME (x))); | |
959 | return 0; | |
960 | } | |
961 | ||
962 | pedwarn ("address of register variable `%s' requested", | |
963 | IDENTIFIER_POINTER (DECL_NAME (x))); | |
964 | } | |
31c56a8b FH |
965 | |
966 | /* drops in */ | |
967 | case FUNCTION_DECL: | |
968 | TREE_ADDRESSABLE (x) = 1; | |
969 | ||
970 | default: | |
971 | return 1; | |
972 | } | |
6cfea11b | 973 | } |
31c56a8b FH |
974 | |
975 | /* Return an integer type with the number of bits of precision given by | |
976 | PRECISION. UNSIGNEDP is nonzero if the type is unsigned; otherwise | |
977 | it is a signed type. */ | |
978 | ||
979 | static tree | |
8376cf3d | 980 | tree_lang_type_for_size (unsigned precision, int unsignedp) |
6cfea11b | 981 | { |
31c56a8b | 982 | tree t; |
6cfea11b | 983 | |
31c56a8b FH |
984 | if (precision <= MAX_BITS_PER_WORD |
985 | && signed_and_unsigned_types[precision][unsignedp] != 0) | |
986 | return signed_and_unsigned_types[precision][unsignedp]; | |
6cfea11b | 987 | |
31c56a8b FH |
988 | if (unsignedp) |
989 | t = signed_and_unsigned_types[precision][1] | |
990 | = make_unsigned_type (precision); | |
991 | else | |
992 | t = signed_and_unsigned_types[precision][0] | |
993 | = make_signed_type (precision); | |
994 | ||
995 | return t; | |
6cfea11b TJ |
996 | } |
997 | ||
31c56a8b FH |
998 | /* Return a data type that has machine mode MODE. UNSIGNEDP selects |
999 | an unsigned type; otherwise a signed type is returned. */ | |
6cfea11b | 1000 | |
31c56a8b | 1001 | static tree |
8376cf3d | 1002 | tree_lang_type_for_mode (enum machine_mode mode, int unsignedp) |
6cfea11b | 1003 | { |
31c56a8b | 1004 | return tree_lang_type_for_size (GET_MODE_BITSIZE (mode), unsignedp); |
6cfea11b TJ |
1005 | } |
1006 | ||
31c56a8b | 1007 | /* Return the unsigned version of a TYPE_NODE, a scalar type. */ |
6cfea11b | 1008 | |
31c56a8b | 1009 | static tree |
8376cf3d | 1010 | tree_lang_unsigned_type (tree type_node) |
6cfea11b | 1011 | { |
31c56a8b | 1012 | return tree_lang_type_for_size (TYPE_PRECISION (type_node), 1); |
6cfea11b TJ |
1013 | } |
1014 | ||
31c56a8b | 1015 | /* Return the signed version of a TYPE_NODE, a scalar type. */ |
6cfea11b | 1016 | |
31c56a8b | 1017 | static tree |
8376cf3d | 1018 | tree_lang_signed_type (tree type_node) |
6cfea11b | 1019 | { |
31c56a8b | 1020 | return tree_lang_type_for_size (TYPE_PRECISION (type_node), 0); |
6cfea11b TJ |
1021 | } |
1022 | ||
31c56a8b FH |
1023 | /* Return a type the same as TYPE except unsigned or signed according to |
1024 | UNSIGNEDP. */ | |
6cfea11b | 1025 | |
31c56a8b | 1026 | static tree |
8376cf3d | 1027 | tree_lang_signed_or_unsigned_type (int unsignedp, tree type) |
6cfea11b | 1028 | { |
8df83eae | 1029 | if (! INTEGRAL_TYPE_P (type) || TYPE_UNSIGNED (type) == unsignedp) |
31c56a8b FH |
1030 | return type; |
1031 | else | |
1032 | return tree_lang_type_for_size (TYPE_PRECISION (type), unsignedp); | |
6cfea11b | 1033 | } |
31c56a8b FH |
1034 | \f |
1035 | /* These functions and variables deal with binding contours. We only | |
1036 | need these functions for the list of PARM_DECLs, but we leave the | |
1037 | functions more general; these are a simplified version of the | |
1038 | functions from GNAT. */ | |
6cfea11b | 1039 | |
31c56a8b FH |
1040 | /* For each binding contour we allocate a binding_level structure which records |
1041 | the entities defined or declared in that contour. Contours include: | |
6cfea11b | 1042 | |
31c56a8b FH |
1043 | the global one |
1044 | one for each subprogram definition | |
1045 | one for each compound statement (declare block) | |
6cfea11b | 1046 | |
31c56a8b | 1047 | Binding contours are used to create GCC tree BLOCK nodes. */ |
6cfea11b | 1048 | |
31c56a8b | 1049 | struct binding_level |
6cfea11b | 1050 | { |
31c56a8b FH |
1051 | /* A chain of ..._DECL nodes for all variables, constants, functions, |
1052 | parameters and type declarations. These ..._DECL nodes are chained | |
1053 | through the TREE_CHAIN field. Note that these ..._DECL nodes are stored | |
1054 | in the reverse of the order supplied to be compatible with the | |
1055 | back-end. */ | |
1056 | tree names; | |
1057 | /* For each level (except the global one), a chain of BLOCK nodes for all | |
1058 | the levels that were entered and exited one level down from this one. */ | |
1059 | tree blocks; | |
1060 | /* The back end may need, for its own internal processing, to create a BLOCK | |
1061 | node. This field is set aside for this purpose. If this field is non-null | |
1062 | when the level is popped, i.e. when poplevel is invoked, we will use such | |
1063 | block instead of creating a new one from the 'names' field, that is the | |
1064 | ..._DECL nodes accumulated so far. Typically the routine 'pushlevel' | |
1065 | will be called before setting this field, so that if the front-end had | |
1066 | inserted ..._DECL nodes in the current block they will not be lost. */ | |
1067 | tree block_created_by_back_end; | |
1068 | /* The binding level containing this one (the enclosing binding level). */ | |
1069 | struct binding_level *level_chain; | |
1070 | }; | |
6cfea11b | 1071 | |
31c56a8b FH |
1072 | /* The binding level currently in effect. */ |
1073 | static struct binding_level *current_binding_level = NULL; | |
6cfea11b | 1074 | |
31c56a8b FH |
1075 | /* The outermost binding level. This binding level is created when the |
1076 | compiler is started and it will exist through the entire compilation. */ | |
1077 | static struct binding_level *global_binding_level; | |
6cfea11b | 1078 | |
31c56a8b FH |
1079 | /* Binding level structures are initialized by copying this one. */ |
1080 | static struct binding_level clear_binding_level = {NULL, NULL, NULL, NULL}; | |
1081 | \f | |
1082 | /* Return non-zero if we are currently in the global binding level. */ | |
6cfea11b | 1083 | |
a3b5decf | 1084 | int |
8376cf3d | 1085 | global_bindings_p (void) |
6cfea11b | 1086 | { |
31c56a8b | 1087 | return current_binding_level == global_binding_level ? -1 : 0; |
6cfea11b TJ |
1088 | } |
1089 | ||
31c56a8b FH |
1090 | /* Return the list of declarations in the current level. Note that this list |
1091 | is in reverse order (it has to be so for back-end compatibility). */ | |
6cfea11b TJ |
1092 | |
1093 | tree | |
8376cf3d | 1094 | getdecls (void) |
6cfea11b | 1095 | { |
31c56a8b | 1096 | return current_binding_level->names; |
6cfea11b TJ |
1097 | } |
1098 | ||
31c56a8b | 1099 | /* Nonzero if the current level needs to have a BLOCK made. */ |
6cfea11b | 1100 | |
31c56a8b | 1101 | int |
8376cf3d | 1102 | kept_level_p (void) |
6cfea11b | 1103 | { |
31c56a8b | 1104 | return (current_binding_level->names != 0); |
6cfea11b TJ |
1105 | } |
1106 | ||
31c56a8b FH |
1107 | /* Enter a new binding level. The input parameter is ignored, but has to be |
1108 | specified for back-end compatibility. */ | |
6cfea11b | 1109 | |
4e263cd9 | 1110 | void |
8376cf3d | 1111 | pushlevel (int ignore ATTRIBUTE_UNUSED) |
6cfea11b | 1112 | { |
c68b0a84 | 1113 | struct binding_level *newlevel = xmalloc (sizeof (struct binding_level)); |
6cfea11b | 1114 | |
31c56a8b | 1115 | *newlevel = clear_binding_level; |
6cfea11b | 1116 | |
31c56a8b FH |
1117 | /* Add this level to the front of the chain (stack) of levels that are |
1118 | active. */ | |
1119 | newlevel->level_chain = current_binding_level; | |
1120 | current_binding_level = newlevel; | |
6cfea11b TJ |
1121 | } |
1122 | ||
31c56a8b FH |
1123 | /* Exit a binding level. |
1124 | Pop the level off, and restore the state of the identifier-decl mappings | |
1125 | that were in effect when this level was entered. | |
a3b5decf | 1126 | |
31c56a8b FH |
1127 | If KEEP is nonzero, this level had explicit declarations, so |
1128 | and create a "block" (a BLOCK node) for the level | |
1129 | to record its declarations and subblocks for symbol table output. | |
a3b5decf | 1130 | |
31c56a8b FH |
1131 | If FUNCTIONBODY is nonzero, this level is the body of a function, |
1132 | so create a block as if KEEP were set and also clear out all | |
1133 | label names. | |
a3b5decf | 1134 | |
31c56a8b FH |
1135 | If REVERSE is nonzero, reverse the order of decls before putting |
1136 | them into the BLOCK. */ | |
a3b5decf TJ |
1137 | |
1138 | tree | |
8376cf3d | 1139 | poplevel (int keep, int reverse, int functionbody) |
a3b5decf | 1140 | { |
31c56a8b FH |
1141 | /* Points to a BLOCK tree node. This is the BLOCK node construted for the |
1142 | binding level that we are about to exit and which is returned by this | |
1143 | routine. */ | |
1144 | tree block_node = NULL_TREE; | |
1145 | tree decl_chain; | |
1146 | tree subblock_chain = current_binding_level->blocks; | |
1147 | tree subblock_node; | |
1148 | tree block_created_by_back_end; | |
1149 | ||
1150 | /* Reverse the list of *_DECL nodes if desired. Note that the ..._DECL | |
1151 | nodes chained through the `names' field of current_binding_level are in | |
1152 | reverse order except for PARM_DECL node, which are explicitely stored in | |
1153 | the right order. */ | |
1154 | decl_chain = (reverse) ? nreverse (current_binding_level->names) | |
1155 | : current_binding_level->names; | |
1156 | ||
1157 | block_created_by_back_end = current_binding_level->block_created_by_back_end; | |
1158 | if (block_created_by_back_end != 0) | |
1159 | { | |
1160 | block_node = block_created_by_back_end; | |
1161 | ||
1162 | /* Check if we are about to discard some information that was gathered | |
1163 | by the front-end. Nameley check if the back-end created a new block | |
1164 | without calling pushlevel first. To understand why things are lost | |
1165 | just look at the next case (i.e. no block created by back-end. */ | |
1166 | if ((keep || functionbody) && (decl_chain || subblock_chain)) | |
1167 | abort (); | |
1168 | } | |
6cfea11b | 1169 | |
31c56a8b FH |
1170 | /* If there were any declarations in the current binding level, or if this |
1171 | binding level is a function body, or if there are any nested blocks then | |
1172 | create a BLOCK node to record them for the life of this function. */ | |
1173 | else if (keep || functionbody) | |
1174 | block_node = build_block (keep ? decl_chain : 0, 0, subblock_chain, 0, 0); | |
1175 | ||
1176 | /* Record the BLOCK node just built as the subblock its enclosing scope. */ | |
1177 | for (subblock_node = subblock_chain; subblock_node; | |
1178 | subblock_node = TREE_CHAIN (subblock_node)) | |
1179 | BLOCK_SUPERCONTEXT (subblock_node) = block_node; | |
1180 | ||
1181 | /* Clear out the meanings of the local variables of this level. */ | |
1182 | ||
1183 | for (subblock_node = decl_chain; subblock_node; | |
1184 | subblock_node = TREE_CHAIN (subblock_node)) | |
1185 | if (DECL_NAME (subblock_node) != 0) | |
1186 | /* If the identifier was used or addressed via a local extern decl, | |
1187 | don't forget that fact. */ | |
1188 | if (DECL_EXTERNAL (subblock_node)) | |
1189 | { | |
1190 | if (TREE_USED (subblock_node)) | |
1191 | TREE_USED (DECL_NAME (subblock_node)) = 1; | |
1192 | if (TREE_ADDRESSABLE (subblock_node)) | |
1193 | TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (subblock_node)) = 1; | |
1194 | } | |
1195 | ||
1196 | /* Pop the current level. */ | |
1197 | current_binding_level = current_binding_level->level_chain; | |
1198 | ||
1199 | if (functionbody) | |
1200 | { | |
1201 | /* This is the top level block of a function. The ..._DECL chain stored | |
1202 | in BLOCK_VARS are the function's parameters (PARM_DECL nodes). Don't | |
1203 | leave them in the BLOCK because they are found in the FUNCTION_DECL | |
1204 | instead. */ | |
1205 | DECL_INITIAL (current_function_decl) = block_node; | |
1206 | BLOCK_VARS (block_node) = 0; | |
1207 | } | |
1208 | else if (block_node) | |
1209 | { | |
1210 | if (block_created_by_back_end == NULL) | |
1211 | current_binding_level->blocks | |
1212 | = chainon (current_binding_level->blocks, block_node); | |
1213 | } | |
6cfea11b | 1214 | |
31c56a8b FH |
1215 | /* If we did not make a block for the level just exited, any blocks made for |
1216 | inner levels (since they cannot be recorded as subblocks in that level) | |
1217 | must be carried forward so they will later become subblocks of something | |
1218 | else. */ | |
1219 | else if (subblock_chain) | |
1220 | current_binding_level->blocks | |
1221 | = chainon (current_binding_level->blocks, subblock_chain); | |
1222 | if (block_node) | |
1223 | TREE_USED (block_node) = 1; | |
1224 | ||
1225 | return block_node; | |
6cfea11b | 1226 | } |
31c56a8b FH |
1227 | \f |
1228 | /* Insert BLOCK at the end of the list of subblocks of the | |
1229 | current binding level. This is used when a BIND_EXPR is expanded, | |
1230 | to handle the BLOCK node inside the BIND_EXPR. */ | |
6cfea11b TJ |
1231 | |
1232 | void | |
8376cf3d | 1233 | insert_block (tree block) |
6cfea11b | 1234 | { |
31c56a8b FH |
1235 | TREE_USED (block) = 1; |
1236 | current_binding_level->blocks | |
1237 | = chainon (current_binding_level->blocks, block); | |
6cfea11b TJ |
1238 | } |
1239 | ||
31c56a8b FH |
1240 | /* Set the BLOCK node for the innermost scope |
1241 | (the one we are currently in). */ | |
6cfea11b TJ |
1242 | |
1243 | void | |
8376cf3d | 1244 | set_block (tree block) |
6cfea11b | 1245 | { |
31c56a8b | 1246 | current_binding_level->block_created_by_back_end = block; |
6cfea11b TJ |
1247 | } |
1248 | ||
31c56a8b FH |
1249 | /* Records a ..._DECL node DECL as belonging to the current lexical scope. |
1250 | Returns the ..._DECL node. */ | |
6cfea11b TJ |
1251 | |
1252 | tree | |
8376cf3d | 1253 | pushdecl (tree decl) |
6cfea11b | 1254 | { |
31c56a8b FH |
1255 | /* External objects aren't nested, other objects may be. */ |
1256 | ||
1257 | if ((DECL_EXTERNAL (decl)) || (decl==current_function_decl)) | |
1258 | DECL_CONTEXT (decl) = 0; | |
1259 | else | |
1260 | DECL_CONTEXT (decl) = current_function_decl; | |
6cfea11b | 1261 | |
31c56a8b FH |
1262 | /* Put the declaration on the list. The list of declarations is in reverse |
1263 | order. The list will be reversed later if necessary. This needs to be | |
1264 | this way for compatibility with the back-end. */ | |
6cfea11b | 1265 | |
31c56a8b FH |
1266 | TREE_CHAIN (decl) = current_binding_level->names; |
1267 | current_binding_level->names = decl; | |
6cfea11b | 1268 | |
31c56a8b | 1269 | /* For the declartion of a type, set its name if it is not already set. */ |
6cfea11b | 1270 | |
31c56a8b FH |
1271 | if (TREE_CODE (decl) == TYPE_DECL |
1272 | && TYPE_NAME (TREE_TYPE (decl)) == 0) | |
1273 | TYPE_NAME (TREE_TYPE (decl)) = DECL_NAME (decl); | |
6cfea11b | 1274 | |
31c56a8b | 1275 | return decl; |
6cfea11b | 1276 | } |
31c56a8b | 1277 | \f |
6cfea11b | 1278 | |
31c56a8b | 1279 | static void |
8376cf3d | 1280 | tree_push_type_decl(tree id, tree type_node) |
d7ee9f9f | 1281 | { |
31c56a8b FH |
1282 | tree decl = build_decl (TYPE_DECL, id, type_node); |
1283 | TYPE_NAME (type_node) = decl; | |
1284 | TYPE_STUB_DECL (type_node) = decl; | |
1285 | pushdecl (decl); | |
d7ee9f9f | 1286 | } |
6cfea11b | 1287 | |
31c56a8b FH |
1288 | /* push_atomic_type_decl() ensures that the type's type is itself. |
1289 | Needed for DBX. Must only be used for atomic types, | |
1290 | not for e.g. pointer or array types. */ | |
44e55536 | 1291 | |
31c56a8b | 1292 | static void |
8376cf3d | 1293 | tree_push_atomic_type_decl(tree id, tree type_node) |
44e55536 | 1294 | { |
31c56a8b FH |
1295 | TREE_TYPE (type_node) = type_node; |
1296 | tree_push_type_decl (id, type_node); | |
44e55536 TJ |
1297 | } |
1298 | ||
31c56a8b | 1299 | #define NULL_BINDING_LEVEL (struct binding_level *) NULL |
44e55536 | 1300 | |
6cfea11b TJ |
1301 | /* Create the predefined scalar types of C, |
1302 | and some nodes representing standard constants (0, 1, (void *) 0). | |
1303 | Initialize the global binding level. | |
1304 | Make definitions for built-in primitive functions. */ | |
1305 | ||
6cfea11b | 1306 | void |
8376cf3d | 1307 | treelang_init_decl_processing (void) |
6cfea11b | 1308 | { |
31c56a8b FH |
1309 | current_function_decl = NULL; |
1310 | current_binding_level = NULL_BINDING_LEVEL; | |
1311 | pushlevel (0); /* make the binding_level structure for global names */ | |
1312 | global_binding_level = current_binding_level; | |
1313 | ||
1314 | build_common_tree_nodes (flag_signed_char); | |
1315 | ||
1316 | /* set standard type names */ | |
1317 | ||
1318 | /* Define `int' and `char' first so that dbx will output them first. */ | |
1319 | ||
1320 | tree_push_atomic_type_decl (get_identifier ("int"), integer_type_node); | |
1321 | tree_push_atomic_type_decl (get_identifier ("char"), char_type_node); | |
1322 | tree_push_atomic_type_decl (get_identifier ("long int"), | |
1323 | long_integer_type_node); | |
1324 | tree_push_atomic_type_decl (get_identifier ("unsigned int"), | |
1325 | unsigned_type_node); | |
1326 | tree_push_atomic_type_decl (get_identifier ("long unsigned int"), | |
1327 | long_unsigned_type_node); | |
1328 | tree_push_atomic_type_decl (get_identifier ("long long int"), | |
1329 | long_long_integer_type_node); | |
1330 | tree_push_atomic_type_decl (get_identifier ("long long unsigned int"), | |
1331 | long_long_unsigned_type_node); | |
1332 | tree_push_atomic_type_decl (get_identifier ("short int"), | |
1333 | short_integer_type_node); | |
1334 | tree_push_atomic_type_decl (get_identifier ("short unsigned int"), | |
1335 | short_unsigned_type_node); | |
1336 | tree_push_atomic_type_decl (get_identifier ("signed char"), | |
1337 | signed_char_type_node); | |
1338 | tree_push_atomic_type_decl (get_identifier ("unsigned char"), | |
1339 | unsigned_char_type_node); | |
1340 | tree_push_atomic_type_decl (NULL_TREE, intQI_type_node); | |
1341 | tree_push_atomic_type_decl (NULL_TREE, intHI_type_node); | |
1342 | tree_push_atomic_type_decl (NULL_TREE, intSI_type_node); | |
1343 | tree_push_atomic_type_decl (NULL_TREE, intDI_type_node); | |
1344 | #if HOST_BITS_PER_WIDE_INT >= 64 | |
1345 | tree_push_atomic_type_decl (NULL_TREE, intTI_type_node); | |
1346 | #endif | |
1347 | tree_push_atomic_type_decl (NULL_TREE, unsigned_intQI_type_node); | |
1348 | tree_push_atomic_type_decl (NULL_TREE, unsigned_intHI_type_node); | |
1349 | tree_push_atomic_type_decl (NULL_TREE, unsigned_intSI_type_node); | |
1350 | tree_push_atomic_type_decl (NULL_TREE, unsigned_intDI_type_node); | |
1351 | #if HOST_BITS_PER_WIDE_INT >= 64 | |
1352 | tree_push_atomic_type_decl (NULL_TREE, unsigned_intTI_type_node); | |
1353 | #endif | |
1354 | ||
1355 | size_type_node = make_unsigned_type (POINTER_SIZE); | |
1356 | tree_push_atomic_type_decl (get_identifier ("size_t"), size_type_node); | |
1357 | set_sizetype (size_type_node); | |
1358 | ||
1359 | build_common_tree_nodes_2 (/* short_double= */ 0); | |
1360 | ||
1361 | tree_push_atomic_type_decl (get_identifier ("float"), float_type_node); | |
1362 | tree_push_atomic_type_decl (get_identifier ("double"), double_type_node); | |
1363 | tree_push_atomic_type_decl (get_identifier ("long double"), long_double_type_node); | |
1364 | tree_push_atomic_type_decl (get_identifier ("void"), void_type_node); | |
1365 | ||
1366 | /* Add any target-specific builtin functions. */ | |
1367 | (*targetm.init_builtins) (); | |
1368 | ||
1369 | pedantic_lvalues = pedantic; | |
6cfea11b TJ |
1370 | } |
1371 | ||
31c56a8b FH |
1372 | /* Return a definition for a builtin function named NAME and whose data type |
1373 | is TYPE. TYPE should be a function type with argument types. | |
1374 | FUNCTION_CODE tells later passes how to compile calls to this function. | |
1375 | See tree.h for its possible values. | |
6cfea11b | 1376 | |
31c56a8b FH |
1377 | If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME, |
1378 | the name to be called if we can't opencode the function. If | |
1379 | ATTRS is nonzero, use that for the function's attribute list. | |
6cfea11b | 1380 | |
31c56a8b FH |
1381 | copied from gcc/c-decl.c |
1382 | */ | |
27f94314 | 1383 | |
31c56a8b | 1384 | tree |
8376cf3d AJ |
1385 | builtin_function (const char *name, tree type, int function_code, |
1386 | enum built_in_class class, const char *library_name, | |
1387 | tree attrs) | |
27f94314 | 1388 | { |
31c56a8b FH |
1389 | tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type); |
1390 | DECL_EXTERNAL (decl) = 1; | |
1391 | TREE_PUBLIC (decl) = 1; | |
1392 | if (library_name) | |
1393 | SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name)); | |
1394 | make_decl_rtl (decl, NULL); | |
1395 | pushdecl (decl); | |
1396 | DECL_BUILT_IN_CLASS (decl) = class; | |
1397 | DECL_FUNCTION_CODE (decl) = function_code; | |
1398 | ||
1399 | /* Possibly apply some default attributes to this built-in function. */ | |
1400 | if (attrs) | |
1401 | decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN); | |
1402 | else | |
1403 | decl_attributes (&decl, NULL_TREE, 0); | |
1404 | ||
1405 | return decl; | |
27f94314 | 1406 | } |
22362755 | 1407 | |
31c56a8b FH |
1408 | #include "debug.h" /* for debug_hooks, needed by gt-treelang-treetree.h */ |
1409 | #include "gt-treelang-treetree.h" |