]>
Commit | Line | Data |
---|---|---|
ab103e33 | 1 | /* Library interface to C front end |
2 | Copyright (C) 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 <cc1plugin-config.h> | |
21 | ||
22 | #undef PACKAGE_NAME | |
23 | #undef PACKAGE_STRING | |
24 | #undef PACKAGE_TARNAME | |
25 | #undef PACKAGE_VERSION | |
26 | ||
27 | #include "../gcc/config.h" | |
28 | ||
29 | #undef PACKAGE_NAME | |
30 | #undef PACKAGE_STRING | |
31 | #undef PACKAGE_TARNAME | |
32 | #undef PACKAGE_VERSION | |
33 | ||
34 | #include "gcc-plugin.h" | |
35 | #include "system.h" | |
36 | #include "coretypes.h" | |
37 | #include "stringpool.h" | |
38 | ||
39 | #include "gcc-interface.h" | |
b20a8bb4 | 40 | #include "hash-set.h" |
41 | #include "machmode.h" | |
42 | #include "vec.h" | |
43 | #include "double-int.h" | |
44 | #include "input.h" | |
45 | #include "alias.h" | |
46 | #include "symtab.h" | |
47 | #include "options.h" | |
ab103e33 | 48 | #include "wide-int.h" |
b20a8bb4 | 49 | #include "inchash.h" |
50 | #include "tree.h" | |
51 | #include "fold-const.h" | |
ab103e33 | 52 | #include "stor-layout.h" |
53 | #include "c-tree.h" | |
54 | #include "toplev.h" | |
55 | #include "timevar.h" | |
56 | #include "hash-table.h" | |
57 | #include "tm.h" | |
58 | #include "c-family/c-pragma.h" | |
59 | #include "c-lang.h" | |
60 | #include "diagnostic.h" | |
61 | #include "langhooks.h" | |
62 | #include "langhooks-def.h" | |
63 | ||
64 | #include "callbacks.hh" | |
65 | #include "connection.hh" | |
66 | #include "rpc.hh" | |
67 | ||
ab103e33 | 68 | #ifdef __GNUC__ |
69 | #pragma GCC visibility push(default) | |
70 | #endif | |
71 | int plugin_is_GPL_compatible; | |
72 | #ifdef __GNUC__ | |
73 | #pragma GCC visibility pop | |
74 | #endif | |
75 | ||
76 | \f | |
77 | ||
78 | // This is put into the lang hooks when the plugin starts. | |
79 | ||
80 | static void | |
81 | plugin_print_error_function (diagnostic_context *context, const char *file, | |
82 | diagnostic_info *diagnostic) | |
83 | { | |
84 | if (current_function_decl != NULL_TREE | |
85 | && DECL_NAME (current_function_decl) != NULL_TREE | |
86 | && strcmp (IDENTIFIER_POINTER (DECL_NAME (current_function_decl)), | |
87 | GCC_FE_WRAPPER_FUNCTION) == 0) | |
88 | return; | |
89 | lhd_print_error_function (context, file, diagnostic); | |
90 | } | |
91 | ||
92 | \f | |
93 | ||
94 | static unsigned long long | |
95 | convert_out (tree t) | |
96 | { | |
97 | return (unsigned long long) (uintptr_t) t; | |
98 | } | |
99 | ||
100 | static tree | |
101 | convert_in (unsigned long long v) | |
102 | { | |
103 | return (tree) (uintptr_t) v; | |
104 | } | |
105 | ||
106 | \f | |
107 | ||
108 | struct decl_addr_value | |
109 | { | |
110 | tree decl; | |
111 | tree address; | |
112 | }; | |
113 | ||
114 | struct decl_addr_hasher : typed_free_remove<decl_addr_value> | |
115 | { | |
116 | typedef decl_addr_value value_type; | |
117 | typedef decl_addr_value compare_type; | |
118 | ||
119 | static inline hashval_t hash (const value_type *); | |
120 | static inline bool equal (const value_type *, const compare_type *); | |
121 | }; | |
122 | ||
123 | inline hashval_t | |
124 | decl_addr_hasher::hash (const value_type *e) | |
125 | { | |
126 | return IDENTIFIER_HASH_VALUE (DECL_NAME (e->decl)); | |
127 | } | |
128 | ||
129 | inline bool | |
130 | decl_addr_hasher::equal (const value_type *p1, const compare_type *p2) | |
131 | { | |
132 | return p1->decl == p2->decl; | |
133 | } | |
134 | ||
135 | \f | |
136 | ||
137 | struct string_hasher : typed_noop_remove<char> | |
138 | { | |
139 | typedef char value_type; | |
140 | typedef char compare_type; | |
141 | ||
142 | static inline hashval_t hash (const value_type *s) | |
143 | { | |
144 | return htab_hash_string (s); | |
145 | } | |
146 | ||
147 | static inline bool equal (const value_type *p1, const value_type *p2) | |
148 | { | |
149 | return strcmp (p1, p2) == 0; | |
150 | } | |
151 | }; | |
152 | ||
153 | \f | |
154 | ||
155 | // A wrapper for pushdecl that doesn't let gdb have a chance to | |
156 | // instantiate a symbol. | |
157 | ||
158 | static void | |
159 | pushdecl_safe (tree decl) | |
160 | { | |
161 | void (*save) (enum c_oracle_request, tree identifier); | |
162 | ||
163 | save = c_binding_oracle; | |
164 | c_binding_oracle = NULL; | |
165 | pushdecl (decl); | |
166 | c_binding_oracle = save; | |
167 | } | |
168 | ||
169 | \f | |
170 | ||
171 | struct plugin_context : public cc1_plugin::connection | |
172 | { | |
173 | plugin_context (int fd); | |
174 | ||
175 | // Map decls to addresses. | |
176 | hash_table<decl_addr_hasher> address_map; | |
177 | ||
178 | // A collection of trees that are preserved for the GC. | |
179 | hash_table< pointer_hash<tree_node> > preserved; | |
180 | ||
181 | // File name cache. | |
182 | hash_table<string_hasher> file_names; | |
183 | ||
184 | // Perform GC marking. | |
185 | void mark (); | |
186 | ||
187 | // Preserve a tree during the plugin's operation. | |
188 | tree preserve (tree t) | |
189 | { | |
190 | tree_node **slot = preserved.find_slot (t, INSERT); | |
191 | *slot = t; | |
192 | return t; | |
193 | } | |
194 | ||
195 | source_location get_source_location (const char *filename, | |
196 | unsigned int line_number) | |
197 | { | |
198 | if (filename == NULL) | |
199 | return UNKNOWN_LOCATION; | |
200 | ||
201 | filename = intern_filename (filename); | |
202 | linemap_add (line_table, LC_ENTER, false, filename, line_number); | |
203 | source_location loc = linemap_line_start (line_table, line_number, 0); | |
204 | linemap_add (line_table, LC_LEAVE, false, NULL, 0); | |
205 | return loc; | |
206 | } | |
207 | ||
208 | private: | |
209 | ||
210 | // Add a file name to FILE_NAMES and return the canonical copy. | |
211 | const char *intern_filename (const char *filename) | |
212 | { | |
213 | char **slot = file_names.find_slot (filename, INSERT); | |
214 | if (*slot == NULL) | |
215 | { | |
216 | /* The file name must live as long as the line map, which | |
217 | effectively means as long as this compilation. So, we copy | |
218 | the string here but never free it. */ | |
219 | *slot = xstrdup (filename); | |
220 | } | |
221 | return *slot; | |
222 | } | |
223 | }; | |
224 | ||
225 | static plugin_context *current_context; | |
226 | ||
227 | \f | |
228 | ||
229 | plugin_context::plugin_context (int fd) | |
230 | : cc1_plugin::connection (fd), | |
231 | address_map (30), | |
232 | preserved (30), | |
233 | file_names (30) | |
234 | { | |
235 | } | |
236 | ||
237 | void | |
238 | plugin_context::mark () | |
239 | { | |
240 | for (hash_table<decl_addr_hasher>::iterator it = address_map.begin (); | |
241 | it != address_map.end (); | |
242 | ++it) | |
243 | { | |
244 | ggc_mark ((*it)->decl); | |
245 | ggc_mark ((*it)->address); | |
246 | } | |
247 | ||
248 | for (hash_table< pointer_hash<tree_node> >::iterator it = preserved.begin (); | |
249 | it != preserved.end (); | |
250 | ++it) | |
251 | ggc_mark (&*it); | |
252 | } | |
253 | ||
254 | static void | |
255 | plugin_binding_oracle (enum c_oracle_request kind, tree identifier) | |
256 | { | |
257 | enum gcc_c_oracle_request request; | |
258 | ||
259 | gcc_assert (current_context != NULL); | |
260 | ||
261 | switch (kind) | |
262 | { | |
263 | case C_ORACLE_SYMBOL: | |
264 | request = GCC_C_ORACLE_SYMBOL; | |
265 | break; | |
266 | case C_ORACLE_TAG: | |
267 | request = GCC_C_ORACLE_TAG; | |
268 | break; | |
269 | case C_ORACLE_LABEL: | |
270 | request = GCC_C_ORACLE_LABEL; | |
271 | break; | |
272 | default: | |
273 | abort (); | |
274 | } | |
275 | ||
276 | int ignore; | |
277 | cc1_plugin::call (current_context, "binding_oracle", &ignore, | |
278 | request, IDENTIFIER_POINTER (identifier)); | |
279 | } | |
280 | ||
281 | static void | |
282 | plugin_pragma_user_expression (cpp_reader *) | |
283 | { | |
284 | c_binding_oracle = plugin_binding_oracle; | |
285 | } | |
286 | ||
287 | static void | |
288 | plugin_init_extra_pragmas (void *, void *) | |
289 | { | |
290 | c_register_pragma ("GCC", "user_expression", plugin_pragma_user_expression); | |
291 | } | |
292 | ||
293 | \f | |
294 | ||
295 | // Maybe rewrite a decl to its address. | |
296 | static tree | |
297 | address_rewriter (tree *in, int *walk_subtrees, void *arg) | |
298 | { | |
299 | plugin_context *ctx = (plugin_context *) arg; | |
300 | ||
301 | if (!DECL_P (*in) || DECL_NAME (*in) == NULL_TREE) | |
302 | return NULL_TREE; | |
303 | ||
304 | decl_addr_value value; | |
305 | value.decl = *in; | |
306 | decl_addr_value *found_value = ctx->address_map.find (&value); | |
307 | if (found_value != NULL) | |
308 | { | |
309 | // At this point we don't need VLA sizes for gdb-supplied | |
310 | // variables, and having them here confuses later passes, so we | |
311 | // drop them. | |
312 | if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (*in))) | |
313 | { | |
314 | TREE_TYPE (*in) | |
315 | = build_array_type_nelts (TREE_TYPE (TREE_TYPE (*in)), 1); | |
316 | DECL_SIZE (*in) = TYPE_SIZE (TREE_TYPE (*in)); | |
317 | DECL_SIZE_UNIT (*in) = TYPE_SIZE_UNIT (TREE_TYPE (*in)); | |
318 | } | |
319 | } | |
320 | else if (DECL_IS_BUILTIN (*in)) | |
321 | { | |
322 | gcc_address address; | |
323 | ||
324 | if (!cc1_plugin::call (ctx, "address_oracle", &address, | |
325 | IDENTIFIER_POINTER (DECL_NAME (*in)))) | |
326 | return NULL_TREE; | |
327 | if (address == 0) | |
328 | return NULL_TREE; | |
329 | ||
330 | // Insert the decl into the address map in case it is referenced | |
331 | // again. | |
332 | value.address = build_int_cst_type (ptr_type_node, address); | |
333 | decl_addr_value **slot = ctx->address_map.find_slot (&value, INSERT); | |
334 | gcc_assert (*slot == NULL); | |
335 | *slot | |
336 | = static_cast<decl_addr_value *> (xmalloc (sizeof (decl_addr_value))); | |
337 | **slot = value; | |
338 | found_value = *slot; | |
339 | } | |
340 | else | |
341 | return NULL_TREE; | |
342 | ||
343 | if (found_value->address != error_mark_node) | |
344 | { | |
345 | // We have an address for the decl, so rewrite the tree. | |
346 | tree ptr_type = build_pointer_type (TREE_TYPE (*in)); | |
347 | *in = fold_build1 (INDIRECT_REF, TREE_TYPE (*in), | |
348 | fold_build1 (CONVERT_EXPR, ptr_type, | |
349 | found_value->address)); | |
350 | } | |
351 | ||
352 | *walk_subtrees = 0; | |
353 | ||
354 | return NULL_TREE; | |
355 | } | |
356 | ||
357 | // When generating code for gdb, we want to be able to use absolute | |
358 | // addresses to refer to otherwise external objects that gdb knows | |
359 | // about. gdb passes in these addresses when building decls, and then | |
360 | // before gimplification we go through the trees, rewriting uses to | |
361 | // the equivalent of "*(TYPE *) ADDR". | |
362 | static void | |
363 | rewrite_decls_to_addresses (void *function_in, void *) | |
364 | { | |
365 | tree function = (tree) function_in; | |
366 | ||
367 | // Do nothing if we're not in gdb. | |
368 | if (current_context == NULL) | |
369 | return; | |
370 | ||
371 | walk_tree (&DECL_SAVED_TREE (function), address_rewriter, current_context, | |
372 | NULL); | |
373 | } | |
374 | ||
375 | \f | |
376 | ||
377 | gcc_decl | |
378 | plugin_build_decl (cc1_plugin::connection *self, | |
379 | const char *name, | |
380 | enum gcc_c_symbol_kind sym_kind, | |
381 | gcc_type sym_type_in, | |
382 | const char *substitution_name, | |
383 | gcc_address address, | |
384 | const char *filename, | |
385 | unsigned int line_number) | |
386 | { | |
387 | plugin_context *ctx = static_cast<plugin_context *> (self); | |
388 | tree identifier = get_identifier (name); | |
389 | enum tree_code code; | |
390 | tree decl; | |
391 | tree sym_type = convert_in (sym_type_in); | |
392 | ||
393 | switch (sym_kind) | |
394 | { | |
395 | case GCC_C_SYMBOL_FUNCTION: | |
396 | code = FUNCTION_DECL; | |
397 | break; | |
398 | ||
399 | case GCC_C_SYMBOL_VARIABLE: | |
400 | code = VAR_DECL; | |
401 | break; | |
402 | ||
403 | case GCC_C_SYMBOL_TYPEDEF: | |
404 | code = TYPE_DECL; | |
405 | break; | |
406 | ||
407 | case GCC_C_SYMBOL_LABEL: | |
408 | // FIXME: we aren't ready to handle labels yet. | |
409 | // It isn't clear how to translate them properly | |
410 | // and in any case a "goto" isn't likely to work. | |
411 | return convert_out (error_mark_node); | |
412 | ||
413 | default: | |
414 | abort (); | |
415 | } | |
416 | ||
417 | source_location loc = ctx->get_source_location (filename, line_number); | |
418 | ||
419 | decl = build_decl (loc, code, identifier, sym_type); | |
420 | TREE_USED (decl) = 1; | |
421 | TREE_ADDRESSABLE (decl) = 1; | |
422 | ||
423 | if (sym_kind != GCC_C_SYMBOL_TYPEDEF) | |
424 | { | |
425 | decl_addr_value value; | |
426 | ||
427 | value.decl = decl; | |
428 | if (substitution_name != NULL) | |
429 | { | |
430 | // If the translator gave us a name without a binding, | |
431 | // we can just substitute error_mark_node, since we know the | |
432 | // translator will be reporting an error anyhow. | |
433 | value.address | |
434 | = lookup_name (get_identifier (substitution_name)); | |
435 | if (value.address == NULL_TREE) | |
436 | value.address = error_mark_node; | |
437 | } | |
438 | else | |
439 | value.address = build_int_cst_type (ptr_type_node, address); | |
440 | decl_addr_value **slot = ctx->address_map.find_slot (&value, INSERT); | |
441 | gcc_assert (*slot == NULL); | |
442 | *slot | |
443 | = static_cast<decl_addr_value *> (xmalloc (sizeof (decl_addr_value))); | |
444 | **slot = value; | |
445 | } | |
446 | ||
447 | return convert_out (ctx->preserve (decl)); | |
448 | } | |
449 | ||
450 | int | |
451 | plugin_bind (cc1_plugin::connection *, | |
452 | gcc_decl decl_in, int is_global) | |
453 | { | |
454 | tree decl = convert_in (decl_in); | |
455 | c_bind (DECL_SOURCE_LOCATION (decl), decl, is_global); | |
456 | rest_of_decl_compilation (decl, is_global, 0); | |
457 | return 1; | |
458 | } | |
459 | ||
460 | int | |
461 | plugin_tagbind (cc1_plugin::connection *self, | |
462 | const char *name, gcc_type tagged_type, | |
463 | const char *filename, unsigned int line_number) | |
464 | { | |
465 | plugin_context *ctx = static_cast<plugin_context *> (self); | |
466 | c_pushtag (ctx->get_source_location (filename, line_number), | |
467 | get_identifier (name), convert_in (tagged_type)); | |
468 | return 1; | |
469 | } | |
470 | ||
471 | gcc_type | |
472 | plugin_build_pointer_type (cc1_plugin::connection *, | |
473 | gcc_type base_type) | |
474 | { | |
475 | // No need to preserve a pointer type as the base type is preserved. | |
476 | return convert_out (build_pointer_type (convert_in (base_type))); | |
477 | } | |
478 | ||
479 | gcc_type | |
480 | plugin_build_record_type (cc1_plugin::connection *self) | |
481 | { | |
482 | plugin_context *ctx = static_cast<plugin_context *> (self); | |
483 | return convert_out (ctx->preserve (make_node (RECORD_TYPE))); | |
484 | } | |
485 | ||
486 | gcc_type | |
487 | plugin_build_union_type (cc1_plugin::connection *self) | |
488 | { | |
489 | plugin_context *ctx = static_cast<plugin_context *> (self); | |
490 | return convert_out (ctx->preserve (make_node (UNION_TYPE))); | |
491 | } | |
492 | ||
493 | int | |
494 | plugin_build_add_field (cc1_plugin::connection *, | |
495 | gcc_type record_or_union_type_in, | |
496 | const char *field_name, | |
497 | gcc_type field_type_in, | |
498 | unsigned long bitsize, | |
499 | unsigned long bitpos) | |
500 | { | |
501 | tree record_or_union_type = convert_in (record_or_union_type_in); | |
502 | tree field_type = convert_in (field_type_in); | |
503 | ||
504 | gcc_assert (TREE_CODE (record_or_union_type) == RECORD_TYPE | |
505 | || TREE_CODE (record_or_union_type) == UNION_TYPE); | |
506 | ||
507 | /* Note that gdb does not preserve the location of field decls, so | |
508 | we can't provide a decent location here. */ | |
509 | tree decl = build_decl (BUILTINS_LOCATION, FIELD_DECL, | |
510 | get_identifier (field_name), field_type); | |
511 | DECL_FIELD_CONTEXT (decl) = record_or_union_type; | |
512 | ||
513 | if (TREE_CODE (field_type) == INTEGER_TYPE | |
514 | && TYPE_PRECISION (field_type) != bitsize) | |
515 | { | |
516 | DECL_BIT_FIELD_TYPE (decl) = field_type; | |
517 | TREE_TYPE (decl) | |
518 | = c_build_bitfield_integer_type (bitsize, TYPE_UNSIGNED (field_type)); | |
519 | } | |
520 | ||
521 | DECL_MODE (decl) = TYPE_MODE (TREE_TYPE (decl)); | |
522 | ||
523 | // There's no way to recover this from DWARF. | |
524 | SET_DECL_OFFSET_ALIGN (decl, TYPE_PRECISION (pointer_sized_int_node)); | |
525 | ||
526 | tree pos = bitsize_int (bitpos); | |
527 | pos_from_bit (&DECL_FIELD_OFFSET (decl), &DECL_FIELD_BIT_OFFSET (decl), | |
528 | DECL_OFFSET_ALIGN (decl), pos); | |
529 | ||
530 | DECL_SIZE (decl) = bitsize_int (bitsize); | |
531 | DECL_SIZE_UNIT (decl) = size_int ((bitsize + BITS_PER_UNIT - 1) | |
532 | / BITS_PER_UNIT); | |
533 | ||
534 | DECL_CHAIN (decl) = TYPE_FIELDS (record_or_union_type); | |
535 | TYPE_FIELDS (record_or_union_type) = decl; | |
536 | ||
537 | return 1; | |
538 | } | |
539 | ||
540 | int | |
541 | plugin_finish_record_or_union (cc1_plugin::connection *, | |
542 | gcc_type record_or_union_type_in, | |
543 | unsigned long size_in_bytes) | |
544 | { | |
545 | tree record_or_union_type = convert_in (record_or_union_type_in); | |
546 | ||
547 | gcc_assert (TREE_CODE (record_or_union_type) == RECORD_TYPE | |
548 | || TREE_CODE (record_or_union_type) == UNION_TYPE); | |
549 | ||
550 | /* We built the field list in reverse order, so fix it now. */ | |
551 | TYPE_FIELDS (record_or_union_type) | |
552 | = nreverse (TYPE_FIELDS (record_or_union_type)); | |
553 | ||
554 | if (TREE_CODE (record_or_union_type) == UNION_TYPE) | |
555 | { | |
556 | /* Unions can just be handled by the generic code. */ | |
557 | layout_type (record_or_union_type); | |
558 | } | |
559 | else | |
560 | { | |
561 | // FIXME there's no way to get this from DWARF, | |
562 | // or even, it seems, a particularly good way to deduce it. | |
563 | TYPE_ALIGN (record_or_union_type) | |
564 | = TYPE_PRECISION (pointer_sized_int_node); | |
565 | ||
566 | TYPE_SIZE (record_or_union_type) = bitsize_int (size_in_bytes | |
567 | * BITS_PER_UNIT); | |
568 | TYPE_SIZE_UNIT (record_or_union_type) = size_int (size_in_bytes); | |
569 | ||
570 | compute_record_mode (record_or_union_type); | |
571 | finish_bitfield_layout (record_or_union_type); | |
572 | // FIXME we have no idea about TYPE_PACKED | |
573 | } | |
574 | ||
575 | return 1; | |
576 | } | |
577 | ||
578 | gcc_type | |
579 | plugin_build_enum_type (cc1_plugin::connection *self, | |
580 | gcc_type underlying_int_type_in) | |
581 | { | |
582 | tree underlying_int_type = convert_in (underlying_int_type_in); | |
583 | ||
584 | if (underlying_int_type == error_mark_node) | |
585 | return convert_out (error_mark_node); | |
586 | ||
587 | tree result = make_node (ENUMERAL_TYPE); | |
588 | ||
589 | TYPE_PRECISION (result) = TYPE_PRECISION (underlying_int_type); | |
590 | TYPE_UNSIGNED (result) = TYPE_UNSIGNED (underlying_int_type); | |
591 | ||
592 | plugin_context *ctx = static_cast<plugin_context *> (self); | |
593 | return convert_out (ctx->preserve (result)); | |
594 | } | |
595 | ||
596 | int | |
597 | plugin_build_add_enum_constant (cc1_plugin::connection *, | |
598 | gcc_type enum_type_in, | |
599 | const char *name, | |
600 | unsigned long value) | |
601 | { | |
602 | tree cst, decl, cons; | |
603 | tree enum_type = convert_in (enum_type_in); | |
604 | ||
605 | gcc_assert (TREE_CODE (enum_type) == ENUMERAL_TYPE); | |
606 | ||
607 | cst = build_int_cst (enum_type, value); | |
608 | /* Note that gdb does not preserve the location of enum constants, | |
609 | so we can't provide a decent location here. */ | |
610 | decl = build_decl (BUILTINS_LOCATION, CONST_DECL, | |
611 | get_identifier (name), enum_type); | |
612 | DECL_INITIAL (decl) = cst; | |
613 | pushdecl_safe (decl); | |
614 | ||
615 | cons = tree_cons (DECL_NAME (decl), cst, TYPE_VALUES (enum_type)); | |
616 | TYPE_VALUES (enum_type) = cons; | |
617 | ||
618 | return 1; | |
619 | } | |
620 | ||
621 | int | |
622 | plugin_finish_enum_type (cc1_plugin::connection *, | |
623 | gcc_type enum_type_in) | |
624 | { | |
625 | tree enum_type = convert_in (enum_type_in); | |
626 | tree minnode, maxnode, iter; | |
627 | ||
628 | iter = TYPE_VALUES (enum_type); | |
629 | minnode = maxnode = TREE_VALUE (iter); | |
630 | for (iter = TREE_CHAIN (iter); | |
631 | iter != NULL_TREE; | |
632 | iter = TREE_CHAIN (iter)) | |
633 | { | |
634 | tree value = TREE_VALUE (iter); | |
635 | if (tree_int_cst_lt (maxnode, value)) | |
636 | maxnode = value; | |
637 | if (tree_int_cst_lt (value, minnode)) | |
638 | minnode = value; | |
639 | } | |
640 | TYPE_MIN_VALUE (enum_type) = minnode; | |
641 | TYPE_MAX_VALUE (enum_type) = maxnode; | |
642 | ||
643 | layout_type (enum_type); | |
644 | ||
645 | return 1; | |
646 | } | |
647 | ||
648 | gcc_type | |
649 | plugin_build_function_type (cc1_plugin::connection *self, | |
650 | gcc_type return_type_in, | |
651 | const struct gcc_type_array *argument_types_in, | |
652 | int is_varargs) | |
653 | { | |
654 | tree *argument_types; | |
655 | tree return_type = convert_in (return_type_in); | |
656 | tree result; | |
657 | ||
658 | argument_types = new tree[argument_types_in->n_elements]; | |
659 | for (int i = 0; i < argument_types_in->n_elements; ++i) | |
660 | argument_types[i] = convert_in (argument_types_in->elements[i]); | |
661 | ||
662 | if (is_varargs) | |
663 | result = build_varargs_function_type_array (return_type, | |
664 | argument_types_in->n_elements, | |
665 | argument_types); | |
666 | else | |
667 | result = build_function_type_array (return_type, | |
668 | argument_types_in->n_elements, | |
669 | argument_types); | |
670 | ||
671 | delete[] argument_types; | |
672 | ||
673 | plugin_context *ctx = static_cast<plugin_context *> (self); | |
674 | return convert_out (ctx->preserve (result)); | |
675 | } | |
676 | ||
677 | gcc_type | |
678 | plugin_int_type (cc1_plugin::connection *self, | |
679 | int is_unsigned, unsigned long size_in_bytes) | |
680 | { | |
681 | tree result = c_common_type_for_size (BITS_PER_UNIT * size_in_bytes, | |
682 | is_unsigned); | |
683 | if (result == NULL_TREE) | |
684 | result = error_mark_node; | |
685 | else | |
686 | { | |
687 | plugin_context *ctx = static_cast<plugin_context *> (self); | |
688 | ctx->preserve (result); | |
689 | } | |
690 | return convert_out (result); | |
691 | } | |
692 | ||
693 | gcc_type | |
694 | plugin_float_type (cc1_plugin::connection *, | |
695 | unsigned long size_in_bytes) | |
696 | { | |
697 | if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (float_type_node)) | |
698 | return convert_out (float_type_node); | |
699 | if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (double_type_node)) | |
700 | return convert_out (double_type_node); | |
701 | if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (long_double_type_node)) | |
702 | return convert_out (long_double_type_node); | |
703 | return convert_out (error_mark_node); | |
704 | } | |
705 | ||
706 | gcc_type | |
707 | plugin_void_type (cc1_plugin::connection *) | |
708 | { | |
709 | return convert_out (void_type_node); | |
710 | } | |
711 | ||
712 | gcc_type | |
713 | plugin_bool_type (cc1_plugin::connection *) | |
714 | { | |
715 | return convert_out (boolean_type_node); | |
716 | } | |
717 | ||
718 | gcc_type | |
719 | plugin_build_array_type (cc1_plugin::connection *self, | |
720 | gcc_type element_type_in, int num_elements) | |
721 | { | |
722 | tree element_type = convert_in (element_type_in); | |
723 | tree result; | |
724 | ||
725 | if (num_elements == -1) | |
726 | result = build_array_type (element_type, NULL_TREE); | |
727 | else | |
728 | result = build_array_type_nelts (element_type, num_elements); | |
729 | ||
730 | plugin_context *ctx = static_cast<plugin_context *> (self); | |
731 | return convert_out (ctx->preserve (result)); | |
732 | } | |
733 | ||
734 | gcc_type | |
735 | plugin_build_vla_array_type (cc1_plugin::connection *self, | |
736 | gcc_type element_type_in, | |
737 | const char *upper_bound_name) | |
738 | { | |
739 | tree element_type = convert_in (element_type_in); | |
740 | tree upper_bound = lookup_name (get_identifier (upper_bound_name)); | |
741 | tree range = build_index_type (upper_bound); | |
742 | ||
743 | tree result = build_array_type (element_type, range); | |
744 | C_TYPE_VARIABLE_SIZE (result) = 1; | |
745 | ||
746 | plugin_context *ctx = static_cast<plugin_context *> (self); | |
747 | return convert_out (ctx->preserve (result)); | |
748 | } | |
749 | ||
750 | gcc_type | |
751 | plugin_build_qualified_type (cc1_plugin::connection *, | |
752 | gcc_type unqualified_type_in, | |
753 | enum gcc_qualifiers qualifiers) | |
754 | { | |
755 | tree unqualified_type = convert_in (unqualified_type_in); | |
756 | int quals = 0; | |
757 | ||
758 | if ((qualifiers & GCC_QUALIFIER_CONST) != 0) | |
759 | quals |= TYPE_QUAL_CONST; | |
760 | if ((qualifiers & GCC_QUALIFIER_VOLATILE) != 0) | |
761 | quals |= TYPE_QUAL_VOLATILE; | |
762 | if ((qualifiers & GCC_QUALIFIER_RESTRICT) != 0) | |
763 | quals |= TYPE_QUAL_RESTRICT; | |
764 | ||
765 | return convert_out (build_qualified_type (unqualified_type, quals)); | |
766 | } | |
767 | ||
768 | gcc_type | |
769 | plugin_build_complex_type (cc1_plugin::connection *self, | |
770 | gcc_type base_type) | |
771 | { | |
772 | plugin_context *ctx = static_cast<plugin_context *> (self); | |
773 | return convert_out (ctx->preserve (build_complex_type (convert_in (base_type)))); | |
774 | } | |
775 | ||
776 | gcc_type | |
777 | plugin_build_vector_type (cc1_plugin::connection *self, | |
778 | gcc_type base_type, int nunits) | |
779 | { | |
780 | plugin_context *ctx = static_cast<plugin_context *> (self); | |
781 | return convert_out (ctx->preserve (build_vector_type (convert_in (base_type), | |
782 | nunits))); | |
783 | } | |
784 | ||
785 | int | |
786 | plugin_build_constant (cc1_plugin::connection *self, gcc_type type_in, | |
787 | const char *name, unsigned long value, | |
788 | const char *filename, unsigned int line_number) | |
789 | { | |
790 | plugin_context *ctx = static_cast<plugin_context *> (self); | |
791 | tree cst, decl; | |
792 | tree type = convert_in (type_in); | |
793 | ||
794 | cst = build_int_cst (type, value); | |
795 | decl = build_decl (ctx->get_source_location (filename, line_number), | |
796 | CONST_DECL, get_identifier (name), type); | |
797 | DECL_INITIAL (decl) = cst; | |
798 | pushdecl_safe (decl); | |
799 | ||
800 | return 1; | |
801 | } | |
802 | ||
803 | gcc_type | |
804 | plugin_error (cc1_plugin::connection *, | |
805 | const char *message) | |
806 | { | |
807 | error ("%s", message); | |
808 | return convert_out (error_mark_node); | |
809 | } | |
810 | ||
811 | \f | |
812 | ||
813 | // Perform GC marking. | |
814 | ||
815 | static void | |
816 | gc_mark (void *, void *) | |
817 | { | |
818 | if (current_context != NULL) | |
819 | current_context->mark (); | |
820 | } | |
821 | ||
822 | #ifdef __GNUC__ | |
823 | #pragma GCC visibility push(default) | |
824 | #endif | |
825 | ||
826 | int | |
827 | plugin_init (struct plugin_name_args *plugin_info, | |
828 | struct plugin_gcc_version *) | |
829 | { | |
830 | long fd = -1; | |
831 | for (int i = 0; i < plugin_info->argc; ++i) | |
832 | { | |
833 | if (strcmp (plugin_info->argv[i].key, "fd") == 0) | |
834 | { | |
835 | char *tail; | |
836 | errno = 0; | |
837 | fd = strtol (plugin_info->argv[i].value, &tail, 0); | |
838 | if (*tail != '\0' || errno != 0) | |
839 | fatal_error ("%s: invalid file descriptor argument to plugin", | |
840 | plugin_info->base_name); | |
841 | break; | |
842 | } | |
843 | } | |
844 | if (fd == -1) | |
845 | fatal_error ("%s: required plugin argument %<fd%> is missing", | |
846 | plugin_info->base_name); | |
847 | ||
848 | current_context = new plugin_context (fd); | |
849 | ||
850 | // Handshake. | |
851 | cc1_plugin::protocol_int version; | |
852 | if (!current_context->require ('H') | |
853 | || ! ::cc1_plugin::unmarshall (current_context, &version)) | |
854 | fatal_error ("%s: handshake failed", plugin_info->base_name); | |
855 | if (version != GCC_C_FE_VERSION_0) | |
856 | fatal_error ("%s: unknown version in handshake", plugin_info->base_name); | |
857 | ||
858 | register_callback (plugin_info->base_name, PLUGIN_PRAGMAS, | |
859 | plugin_init_extra_pragmas, NULL); | |
860 | register_callback (plugin_info->base_name, PLUGIN_PRE_GENERICIZE, | |
861 | rewrite_decls_to_addresses, NULL); | |
862 | register_callback (plugin_info->base_name, PLUGIN_GGC_MARKING, | |
863 | gc_mark, NULL); | |
864 | ||
865 | lang_hooks.print_error_function = plugin_print_error_function; | |
866 | ||
867 | #define GCC_METHOD0(R, N) \ | |
868 | { \ | |
869 | cc1_plugin::callback_ftype *fun \ | |
870 | = cc1_plugin::callback<R, plugin_ ## N>; \ | |
871 | current_context->add_callback (# N, fun); \ | |
872 | } | |
873 | #define GCC_METHOD1(R, N, A) \ | |
874 | { \ | |
875 | cc1_plugin::callback_ftype *fun \ | |
876 | = cc1_plugin::callback<R, A, plugin_ ## N>; \ | |
877 | current_context->add_callback (# N, fun); \ | |
878 | } | |
879 | #define GCC_METHOD2(R, N, A, B) \ | |
880 | { \ | |
881 | cc1_plugin::callback_ftype *fun \ | |
882 | = cc1_plugin::callback<R, A, B, plugin_ ## N>; \ | |
883 | current_context->add_callback (# N, fun); \ | |
884 | } | |
885 | #define GCC_METHOD3(R, N, A, B, C) \ | |
886 | { \ | |
887 | cc1_plugin::callback_ftype *fun \ | |
888 | = cc1_plugin::callback<R, A, B, C, plugin_ ## N>; \ | |
889 | current_context->add_callback (# N, fun); \ | |
890 | } | |
891 | #define GCC_METHOD4(R, N, A, B, C, D) \ | |
892 | { \ | |
893 | cc1_plugin::callback_ftype *fun \ | |
894 | = cc1_plugin::callback<R, A, B, C, D, \ | |
895 | plugin_ ## N>; \ | |
896 | current_context->add_callback (# N, fun); \ | |
897 | } | |
898 | #define GCC_METHOD5(R, N, A, B, C, D, E) \ | |
899 | { \ | |
900 | cc1_plugin::callback_ftype *fun \ | |
901 | = cc1_plugin::callback<R, A, B, C, D, E, \ | |
902 | plugin_ ## N>; \ | |
903 | current_context->add_callback (# N, fun); \ | |
904 | } | |
905 | #define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \ | |
906 | { \ | |
907 | cc1_plugin::callback_ftype *fun \ | |
908 | = cc1_plugin::callback<R, A, B, C, D, E, F, G, \ | |
909 | plugin_ ## N>; \ | |
910 | current_context->add_callback (# N, fun); \ | |
911 | } | |
912 | ||
913 | #include "gcc-c-fe.def" | |
914 | ||
915 | #undef GCC_METHOD0 | |
916 | #undef GCC_METHOD1 | |
917 | #undef GCC_METHOD2 | |
918 | #undef GCC_METHOD3 | |
919 | #undef GCC_METHOD4 | |
920 | #undef GCC_METHOD5 | |
921 | #undef GCC_METHOD7 | |
922 | ||
923 | return 0; | |
924 | } |