1 /*
2 * Managing global state for the VHDL code generator.
3 *
4 * Copyright (C) 2009 Nick Gasson (nick@nickg.me.uk)
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "state.hh"
22 #include "vhdl_syntax.hh"
23 #include "vhdl_target.h"
24
25 #include <algorithm>
26 #include <string>
27 #include <map>
28 #include <vector>
29 #include <cstring>
30 #include <iostream>
31
32 using namespace std;
33
34 /*
35 * This file stores all the global state required during VHDL code
36 * generation. At present we store the following:
37 *
38 * - A mapping from Verilog signals to the VHDL scope (entity, etc.)
39 * where it is found, and the name of the corresponding VHDL signal.
40 * This allows us to support renaming invalid Verilog signal names
41 * to valid VHDL ones.
42 *
43 * - The set of all VHDL entities generated.
44 *
45 * - The currently active entity. "Active" here means that we are
46 * currently generating code for a process inside the corresponding
47 * scope. This is useful, for example, if a statement or expression
48 * in a process needs to add are referencing something in the containing
49 * architecture object.
50 */
51
52 /*
53 * Maps a signal to the scope it is defined within. Also
54 * provides a mechanism for renaming signals -- i.e. when
55 * an output has the same name as register: valid in Verilog
56 * but not in VHDL, so two separate signals need to be
57 * defined.
58 */
59 struct signal_defn_t {
60 std::string renamed; // The name of the VHDL signal
61 vhdl_scope *scope; // The scope where it is defined
62 };
63
64 // All entities to emit.
65 // These are stored in a list rather than a set so the first
66 // entity added will correspond to the first (top) Verilog module
67 // encountered and hence it will appear first in the output file.
68 static entity_list_t g_entities;
69
70 // Store the mapping of ivl scope names to entity names
71 typedef map<ivl_scope_t, string> scope_name_map_t;
72 static scope_name_map_t g_scope_names;
73
74 typedef std::map<ivl_signal_t, signal_defn_t> signal_defn_map_t;
75 static signal_defn_map_t g_known_signals;
76
77 static vhdl_entity *g_active_entity = NULL;
78
79 // Set of scopes that are treated as the default examples of
80 // that type. Any other scopes of the same type are ignored.
81 typedef vector<ivl_scope_t> default_scopes_t;
82 static default_scopes_t g_default_scopes;
83
84 // True if signal `sig' has already been encountered by the code
85 // generator. This means we have already assigned it to a VHDL code
86 // object and possibly renamed it.
seen_signal_before(ivl_signal_t sig)87 bool seen_signal_before(ivl_signal_t sig)
88 {
89 return g_known_signals.find(sig) != g_known_signals.end();
90 }
91
92 // Remember the association of signal to a VHDL code object (typically
93 // an entity).
remember_signal(ivl_signal_t sig,vhdl_scope * scope)94 void remember_signal(ivl_signal_t sig, vhdl_scope *scope)
95 {
96 assert(!seen_signal_before(sig));
97
98 signal_defn_t defn = { ivl_signal_basename(sig), scope };
99 g_known_signals[sig] = defn;
100 }
101
102 // Change the VHDL name of a Verilog signal.
rename_signal(ivl_signal_t sig,const std::string & renamed)103 void rename_signal(ivl_signal_t sig, const std::string &renamed)
104 {
105 assert(seen_signal_before(sig));
106
107 g_known_signals[sig].renamed = renamed;
108 }
109
110 // Given a Verilog signal, return the VHDL code object where it should
111 // be defined. Note that this can return a NULL pointer if `sig' hasn't
112 // be encountered yet.
find_scope_for_signal(ivl_signal_t sig)113 vhdl_scope *find_scope_for_signal(ivl_signal_t sig)
114 {
115 if (seen_signal_before(sig))
116 return g_known_signals[sig].scope;
117 else
118 return NULL;
119 }
120
121 // Get the name of the VHDL signal corresponding to Verilog signal `sig'.
get_renamed_signal(ivl_signal_t sig)122 const std::string &get_renamed_signal(ivl_signal_t sig)
123 {
124 assert(seen_signal_before(sig));
125
126 return g_known_signals[sig].renamed;
127 }
128
129 // TODO: Can we dispose of this???
130 // -> This is only used in logic.cc to get the type of a signal connected
131 // to a logic device -> we should be able to get this from the nexus
find_signal_named(const std::string & name,const vhdl_scope * scope)132 ivl_signal_t find_signal_named(const std::string &name, const vhdl_scope *scope)
133 {
134 signal_defn_map_t::const_iterator it;
135 for (it = g_known_signals.begin(); it != g_known_signals.end(); ++it) {
136 if (((*it).second.scope == scope
137 || (*it).second.scope == scope->get_parent())
138 && (*it).second.renamed == name)
139 return (*it).first;
140 }
141 assert(false);
142 return NULL;
143 }
144
145 // Compare the name of an entity against a string
146 struct cmp_ent_name {
cmp_ent_namecmp_ent_name147 explicit cmp_ent_name(const string& n) : name_(n) {}
148
operator ()cmp_ent_name149 bool operator()(const vhdl_entity* ent) const
150 {
151 return ent->get_name() == name_;
152 }
153
154 const string& name_;
155 };
156
157 // Find an entity given its name.
find_entity(const string & name)158 vhdl_entity* find_entity(const string& name)
159 {
160 entity_list_t::const_iterator it
161 = find_if(g_entities.begin(), g_entities.end(),
162 cmp_ent_name(name));
163
164 if (it != g_entities.end())
165 return *it;
166 else
167 return NULL;
168 }
169
170 // Find a VHDL entity given a Verilog module scope. The VHDL entity
171 // name should be the same as the Verilog module type name.
172 // Note that this will return NULL if no entity has been recorded
173 // for this scope type.
find_entity(ivl_scope_t scope)174 vhdl_entity* find_entity(ivl_scope_t scope)
175 {
176 // Skip over generate scopes
177 while (ivl_scope_type(scope) == IVL_SCT_GENERATE)
178 scope = ivl_scope_parent(scope);
179
180 assert(ivl_scope_type(scope) == IVL_SCT_MODULE);
181
182 if (is_default_scope_instance(scope)) {
183 scope_name_map_t::iterator it = g_scope_names.find(scope);
184 if (it != g_scope_names.end())
185 return find_entity((*it).second);
186 else
187 return NULL;
188 }
189 else {
190 const char *tname = ivl_scope_tname(scope);
191
192 for (scope_name_map_t::iterator it = g_scope_names.begin();
193 it != g_scope_names.end(); ++it) {
194 if (strcmp(tname, ivl_scope_tname((*it).first)) == 0)
195 return find_entity((*it).second);
196 }
197
198 return NULL;
199 }
200 }
201
202 // Add an entity/architecture pair to the list of entities to emit.
remember_entity(vhdl_entity * ent,ivl_scope_t scope)203 void remember_entity(vhdl_entity* ent, ivl_scope_t scope)
204 {
205 g_entities.push_back(ent);
206 g_scope_names[scope] = ent->get_name();
207 }
208
209 // Print all VHDL entities, in order, to the specified output stream.
emit_all_entities(std::ostream & os,int max_depth)210 void emit_all_entities(std::ostream& os, int max_depth)
211 {
212 for (entity_list_t::iterator it = g_entities.begin();
213 it != g_entities.end();
214 ++it) {
215 if ((max_depth == 0 || (*it)->depth < max_depth))
216 (*it)->emit(os);
217 }
218 }
219
220 // Release all memory for the VHDL objects. No vhdl_element pointers
221 // will be valid after this call.
free_all_vhdl_objects()222 void free_all_vhdl_objects()
223 {
224 int freed = vhdl_element::free_all_objects();
225 debug_msg("Deallocated %d VHDL syntax objects", freed);
226
227 size_t total = vhdl_element::total_allocated();
228 debug_msg("%d total bytes used for VHDL syntax objects", total);
229
230 g_entities.clear();
231 }
232
233 // Return the currently active entity
get_active_entity()234 vhdl_entity *get_active_entity()
235 {
236 return g_active_entity;
237 }
238
239 // Change the currently active entity
set_active_entity(vhdl_entity * ent)240 void set_active_entity(vhdl_entity *ent)
241 {
242 g_active_entity = ent;
243 }
244
245 /*
246 * True if two scopes have the same type name.
247 */
same_scope_type_name(ivl_scope_t a,ivl_scope_t b)248 static bool same_scope_type_name(ivl_scope_t a, ivl_scope_t b)
249 {
250 if (strcmp(ivl_scope_tname(a), ivl_scope_tname(b)) != 0)
251 return false;
252
253 unsigned nparams_a = ivl_scope_params(a);
254 unsigned nparams_b = ivl_scope_params(b);
255
256 if (nparams_a != nparams_b)
257 return false;
258
259 for (unsigned i = 0; i < nparams_a; i++) {
260 ivl_parameter_t param_a = ivl_scope_param(a, i);
261 ivl_parameter_t param_b = ivl_scope_param(b, i);
262
263 if (strcmp(ivl_parameter_basename(param_a),
264 ivl_parameter_basename(param_b)) != 0)
265 return false;
266
267 ivl_expr_t value_a = ivl_parameter_expr(param_a);
268 ivl_expr_t value_b = ivl_parameter_expr(param_b);
269
270 if (ivl_expr_type(value_a) != ivl_expr_type(value_b))
271 return false;
272
273 switch (ivl_expr_type(value_a)) {
274 case IVL_EX_STRING:
275 if (strcmp(ivl_expr_string(value_a), ivl_expr_string(value_b)) != 0)
276 return false;
277 break;
278
279 case IVL_EX_NUMBER:
280 if (ivl_expr_uvalue(value_a) != ivl_expr_uvalue(value_b))
281 return false;
282 break;
283
284 default:
285 assert(false);
286 }
287 }
288
289 return true;
290 }
291
292 /*
293 * True if we have already seen a scope with this type before.
294 * If the result is `false' then s is stored in the set of seen
295 * scopes.
296 */
seen_this_scope_type(ivl_scope_t s)297 bool seen_this_scope_type(ivl_scope_t s)
298 {
299 if (find_if(g_default_scopes.begin(), g_default_scopes.end(),
300 bind1st(ptr_fun(same_scope_type_name), s))
301 == g_default_scopes.end()) {
302 g_default_scopes.push_back(s);
303 return false;
304 }
305 else
306 return true;
307 }
308
309 /*
310 * True if this scope is the default example of this scope type.
311 * All other instances of this scope type are ignored.
312 */
is_default_scope_instance(ivl_scope_t s)313 bool is_default_scope_instance(ivl_scope_t s)
314 {
315 return find(g_default_scopes.begin(), g_default_scopes.end(), s)
316 != g_default_scopes.end();
317 }
318