1 /* Generic plugin context
2    Copyright (C) 2020 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 #ifndef CC1_PLUGIN_CONTEXT_HH
21 #define CC1_PLUGIN_CONTEXT_HH
22 
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tree.h"
26 
27 #include "connection.hh"
28 
29 namespace cc1_plugin
30 {
31   static inline unsigned long long
convert_out(tree t)32   convert_out (tree t)
33   {
34     return (unsigned long long) (uintptr_t) t;
35   }
36 
37   static inline tree
convert_in(unsigned long long v)38   convert_in (unsigned long long v)
39   {
40     return (tree) (uintptr_t) v;
41   }
42 
43   struct decl_addr_value
44   {
45     tree decl;
46     tree address;
47   };
48 
49   struct decl_addr_hasher : free_ptr_hash<decl_addr_value>
50   {
hashcc1_plugin::decl_addr_hasher51     static hashval_t hash (const decl_addr_value *e)
52     {
53       return DECL_UID (e->decl);
54     }
55 
equalcc1_plugin::decl_addr_hasher56     static bool equal (const decl_addr_value *p1,
57 		       const decl_addr_value *p2)
58     {
59       return p1->decl == p2->decl;
60     }
61   };
62 
63   struct string_hasher : nofree_ptr_hash<const char>
64   {
hashcc1_plugin::string_hasher65     static inline hashval_t hash (const char *s)
66     {
67       return htab_hash_string (s);
68     }
69 
equalcc1_plugin::string_hasher70     static inline bool equal (const char *p1, const char *p2)
71     {
72       return strcmp (p1, p2) == 0;
73     }
74   };
75 
76   struct plugin_context : public cc1_plugin::connection
77   {
plugin_contextcc1_plugin::plugin_context78     plugin_context (int fd)
79       : cc1_plugin::connection (fd),
80 	address_map (30),
81 	preserved (30),
82 	file_names (30)
83     {
84     }
85 
86     // Map decls to addresses.
87     hash_table<decl_addr_hasher> address_map;
88 
89     // A collection of trees that are preserved for the GC.
90     hash_table< nofree_ptr_hash<tree_node> > preserved;
91 
92     // File name cache.
93     hash_table<string_hasher> file_names;
94 
95     // Perform GC marking.
96     void mark ();
97 
98     // Preserve a tree during the plugin's operation.
preservecc1_plugin::plugin_context99     tree preserve (tree t)
100     {
101       tree_node **slot = preserved.find_slot (t, INSERT);
102       *slot = t;
103       return t;
104     }
105 
106     location_t get_location_t (const char *filename,
107 			       unsigned int line_number);
108 
109   private:
110 
111     // Add a file name to FILE_NAMES and return the canonical copy.
112     const char *intern_filename (const char *filename);
113   };
114 
115   extern plugin_context *current_context;
116 
117   void generic_plugin_init (struct plugin_name_args *plugin_info,
118 			    unsigned int version);
119 }
120 
121 #endif // CC1_PLUGIN_CONTEXT_HH
122