File: v2cc-qid.cc

package info (click to toggle)
freehdl 0.0.8-2.2
  • links: PTS
  • area: main
  • in suites: buster, sid, stretch
  • size: 8,632 kB
  • ctags: 10,443
  • sloc: cpp: 45,275; sh: 11,405; yacc: 4,206; ansic: 2,026; lex: 486; perl: 430; makefile: 390; tcl: 100
file content (362 lines) | stat: -rw-r--r-- 11,604 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362

#include <freehdl/vaul.h>
#include "v2cc-chunk.h"
#include "v2cc-util.h"



// ******************************************************************************************
// Name: m_qid , generic function
//
// Description: Returns get a fully qualified identifier name plus an
// appropriate prefix. I.e., if a identifier is used at a location in
// the code where it is not directly visible then some prefix code is
// needed in order to access it. E.g., if a generic parameter is used
// within process then it is accessed using a architecture pointer as
// the parameter is not directly visible within the process. parameter
// rstack determines the code region where the identifier is to be
// used. Parameter obj_access defines how the identifier is accessed.
// 
// Return value: returns fully qualified string
//
// ******************************************************************************************

// Prepends \ before special characters \ : ( ) ". 
string
get_fully_escaped_string(const string &str)
{
  string result;

  for (unsigned int i = 0; i < str.length(); i++) {
    if (str[i] == '\\' ||
	str[i] == ':' ||
	str[i] == '"' ||
	str[i] == '(' ||
	str[i] == ')')
      result += '\\';
    result += str[i];
  }
  return result;
}

 
string
get_access_prefix(pIIR_Declaration d, RegionStack &rstack, id_type obj_access)
{
  if (obj_access.access() & NO_PREFIX)
    return "";

  string prefix;

  // If the object has been declared within a package or package body
  // then no prefix is needed as the corresponding C++ objects are
  // declared globally.
  if (d->declarative_region->is(IR_PACKAGE_DECLARATION) ||
      d->declarative_region->is(IR_PACKAGE_BODY_DECLARATION))
    return "";

  // If the identifier is globally declared then no prefix is required
  if (static_declarative_region(d) == RootDeclarativeRegion(rstack))
    return "";

  // Determine the "home" region where the object has been
  // declared. Note that this is not necessarily the original
  // declarative region but may have been modified during
  // optimization. E.g., if a constant is declared within a process
  // then the declarative region of the constant is the process
  // itself. Usually, the corresponding C++ constant will be declared
  // within the C++ process code.  However, if the constant is locally
  // static then it is declared globally in the C++ source.
  pIIR_DeclarativeRegion ident_home_region = 
    static_declarative_region(d) != NULL? static_declarative_region(d) : d->declarative_region;
  while (true) {
    // Search for the next concurrent statement, package, package
    // body, subprogram, architecture or entity.
    if (ident_home_region->is(IR_CONCURRENT_STATEMENT) ||
	ident_home_region->is(IR_PACKAGE_DECLARATION) ||
	ident_home_region->is(IR_PACKAGE_BODY_DECLARATION) ||
	ident_home_region->is(IR_SUBPROGRAM_DECLARATION) ||
	ident_home_region->is(IR_ARCHITECTURE_DECLARATION) ||
	ident_home_region->is(IR_ENTITY_DECLARATION))
      break;
    ident_home_region = ident_home_region->declarative_region;
  }
  // If the home region is equal to the current region then the
  // object is directly visible. Hence, no prefix is needed. Further,
  // all objects within an architecture or entity region are directly
  // visible as well.
  RegionStack::iterator r_iter = --rstack.end();
  if  (ident_home_region == (*r_iter) ||
       (*r_iter)->is(IR_ENTITY_DECLARATION) ||
       (*r_iter)->is(IR_ARCHITECTURE_DECLARATION))
    return prefix;
  // Analyze each region beginning with the current region an
  // proceeding to the very first region.
  while (true) {
    // If the identifier is used within a subprogram then we do not
    // need any additional prefix. Note that any identifier not
    // global to a subprogram is passed into the corresponding C++
    // function (generated from the VHDL subprogram code) as a
    // parameter.
    if ((*r_iter)->is(IR_SUBPROGRAM_DECLARATION))
      return prefix;
    // If the current region pointer points to an entity then the
    // object is accessed via the entity/architecture pointer.
    // Further, if the object is declared within an generate
    // statement, then it is accessed vi a special pointer.
    if ((*r_iter)->is(IR_ENTITY_DECLARATION) ||
	(*r_iter)->is(IR_ARCHITECTURE_DECLARATION) ||
	((*r_iter)->is(IR_CONCURRENT_GENERATE_STATEMENT) &&
	 ident_home_region == (*r_iter)))
      return qid(*r_iter, rstack, id_type()) + "_AP->" + prefix;
    // If the current region pointer is equal to the "home" region of
    // the object then we are done and the prefix is returned.
    if  (ident_home_region == (*r_iter))
      return prefix;
    if (r_iter == rstack.begin())
      {
	// This should never happen
	cout << "Internal problem: Could not determine access prefix for " << d->declarator->text.to_chars() << endl;
	cout << "Tested regions: ";
	r_iter = --rstack.end();
	while (r_iter != rstack.begin ())
	  {
	    if ((*r_iter)->declarator != NULL)
	      cout << " " << (*r_iter)->declarator->text.to_chars();
	    else
	      cout << " ???";
	    r_iter--;
	  }
	cout << endl;
	cout << "static_declarative_region: " << (long)static_declarative_region(d);
	if (static_declarative_region(d) != NULL &&
	    static_declarative_region(d)->declarator != NULL)
	  cout << " = " << static_declarative_region(d)->declarator->text.to_chars();
	cout << endl;
	cout << "RootDeclarativeRegion: " << (long)RootDeclarativeRegion(rstack) << endl;
	assert (false);
      }
    r_iter--;
  };
  
  return prefix;
}


string
m_qid(pIIR_LibraryUnit d, RegionStack &rstack, id_type obj_access)
{
  string id;

  if (d->is (IR_ARCHITECTURE_DECLARATION))			// Ent_Arch
    id = qid(pIIR_ArchitectureDeclaration(d)->entity, rstack, obj_access);
  else if (!(obj_access.object() == BARE))
    id = nid(pIIR_LibraryUnit(d),LIBRARY);
  else
    id = pIIR_LibraryUnit(d)->library_name->text.to_chars();		

  id += "_" + nid(d, obj_access);

  return id;
}


string
m_qid(pIIR_SubprogramDeclaration d, RegionStack &rstack, id_type obj_access)
{
  string id;
  if (d->prototype != NULL)
    // If a subprogram shall be printed and if this subprogram has a
    // prototype then print the name of the prototype delcaration!
    id = qid(d->prototype, rstack, obj_access);
  else
    id = qid(d->declarative_region, rstack, obj_access) + "_" + nid(d, obj_access);

  return id;
}


string
m_qid(pIIR_Declaration d, RegionStack &rstack, id_type obj_access)
{
  return qid(d->declarative_region, rstack, obj_access) + "_" + nid(d, obj_access);
}


string
m_qid(pIIR_InterfaceDeclaration d, RegionStack &rstack, id_type obj_access)
{
  string id = get_access_prefix(d, rstack, obj_access) + 
    qid(d->declarative_region, rstack, obj_access) + "_" + nid(d, obj_access);

  if (d->alias_base != NULL && 
      !is_array_type(d->subtype) && 
      (obj_access.access() & DEREF))
      // If an object alias (other than an array alias) shall be
      // printed and the DEREF flag is set then the entire string is
      // prepended with "*"
      id = "(*" + id + ")";

  return id;
}


string
m_qid(pIIR_SignalInterfaceDeclaration d, RegionStack &rstack, id_type obj_access)
{
  string id = qid(d->declarative_region, rstack, obj_access) + "_";

  // if the reader is requested and ARCHREF is set then the reader is
  // accessed via the sig_info instance.
  if ((obj_access.object() == READER) &&
      (obj_access.access() & ARCHREF)) {
    id += nid(d, id_type(DEFAULT, obj_access.access()));
    id = get_access_prefix(d, rstack, obj_access) + id;
    if (!(obj_access.access() & DEREF))
      id = "&" + id;
    else
      id += "->reader()";

  } else {
    // Reader and driver are delclared locally. Hence, no access prefix
    // is required.
    id += nid(d, obj_access);
  
    if ((obj_access.object() != READER) && 
	(obj_access.object() != DRIVER) &&
	(obj_access.access() & ARCHREF))
      id = get_access_prefix(d, rstack, obj_access) + id;
    
    if (obj_access.access() & DEREF)
      // If a signal shall be printed and the DEREF flag is set then the
      // entire string is prepended with "*"
      id = "(*" + id + ")";
  }

  return id;
}


string
m_qid(pIIR_SignalDeclaration d, RegionStack &rstack, id_type obj_access)
{
  string id = qid(d->declarative_region, rstack, obj_access) + "_";

  // if the reader is requested and ARCHREF is set then the reader is
  // accessed via the sig_info instance.
  if ((obj_access.object() == READER) &&
      (obj_access.access() & ARCHREF)) {
    id += nid(d, id_type(DEFAULT, obj_access.access()));
    id = get_access_prefix(d, rstack, obj_access) + id;
    if (!(obj_access.access() & DEREF))
      id = "&" + id;
    else
      id += "->reader()";

  } else {
    // Reader and driver are delclared locally. Hence, no access prefix
    // is required.
    id += nid(d, obj_access);
    
    if ((obj_access.object() != READER) && 
	(obj_access.object() != DRIVER) &&
	(obj_access.access() & ARCHREF))
      id = get_access_prefix(d, rstack, obj_access) + id;
    
    if (obj_access.access() & DEREF)
      // If a signal shall be printed and the DEREF flag is set then the
      // entire string is prepended with "*"
      id = "(*" + id + ")";
  }

  return id;
}


string
m_qid(pIIR_ObjectDeclaration d, RegionStack &rstack, id_type obj_access)
{
  string id = get_access_prefix(d, rstack, obj_access) + 
    qid(d->declarative_region, rstack, obj_access) + "_" + nid(d, obj_access);

  if (d->alias_base != NULL &&
      !is_array_type(d->subtype) && 
      (obj_access.access() & DEREF))
    // If an object alias (other than an array alias) shall be printed
    // and the DEREF flag is set then the entire string is prepended
    // with "*"
    id = "(*" + id + ")";
  
  return id;
}


string
m_qid(pV2CC_ImplicitSignalDeclaration_WaitFor d, RegionStack &rstack, id_type obj_access)
{
  string id = nid(d, obj_access);

  if (obj_access.access() & DEREF)
    // If a signal shall be printed and the DEREF flag is set then the
    // entire string is prepended with "*"
    id = "(*" + id + ")";

  return id;
}


string
m_qid(pIIR_TypeDeclaration d, RegionStack &rstack, id_type obj_access)
{
  string id;

  if ((obj_access.object() == TYPE) && is_scalar_type(d->type)) {
    pIIR_Type base_type = get_base_type(d->type);
    
    if (base_type->is(IR_INTEGER_TYPE))
      return "integer";
    else if (base_type->is(IR_ENUMERATION_TYPE))
      return "enumeration";
    else if (base_type->is(IR_PHYSICAL_TYPE))
      return "physical";
    else if (base_type->is(IR_FLOATING_TYPE))
      return "floatingpoint";
    else
      assert(false);

  } else if ((obj_access.object() == TYPE) && d->type->is(IR_ACCESS_TYPE)) {
    return "vhdlaccess";

  } else if ((obj_access.object() == TYPE) && d->type->is(IR_FILE_TYPE)) {
    return "vhdlfile";
  
  } else
    id = qid(d->declarative_region, rstack, obj_access) + "_" + nid(d, obj_access);

  if (obj_access.object() == INFO)
    id = get_access_prefix (d, rstack, obj_access) + id;

  if ((obj_access.object() == (ALIAS | TYPE)) &&
      (d->type->is(IR_ARRAY_SUBTYPE) || (d->type->is(IR_ARRAY_TYPE))))
    // Create an array alias type name
    id = "array_alias<" + id + " >";

  return id;
}


string
m_qid(pIIR_Type t, RegionStack &rstack, id_type obj_access)
{
  return qid(get_declaration(t), rstack, obj_access);
}


string
m_qid(pIIR_Subtype t, RegionStack &rstack, id_type obj_access)
{
  if (implicit_subtype_declaration(t) != NULL)
    return qid(implicit_subtype_declaration(t), rstack, obj_access);
  else
    return qid(get_declaration(t), rstack, obj_access);
}