File: GatherImplicits.cpp

package info (click to toggle)
fauhdlc 20180504-3.1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 3,064 kB
  • sloc: cpp: 23,188; ansic: 6,077; yacc: 3,764; lex: 763; makefile: 605; python: 412; xml: 403; sh: 61
file content (505 lines) | stat: -rw-r--r-- 11,453 bytes parent folder | download | duplicates (3)
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/* $Id$ 
 *
 * Gather implicitly needed variables (e.g. drivers).
 *
 * Copyright (C) 2008-2009 FAUmachine Team <info@faumachine.org>.
 * This program is free software. You can redistribute it and/or modify it
 * under the terms of the GNU General Public License, either version 2 of
 * the License, or (at your option) any later version. See COPYING.
 */


#include "frontend/visitor/GatherImplicits.hpp"
#include "frontend/ast/SigAssignStat.hpp"
#include "frontend/ast/VarAssignStat.hpp"
#include "frontend/ast/SimpleName.hpp"
#include "frontend/ast/SignalDeclaration.hpp"
#include "frontend/ast/Process.hpp"
#include "frontend/ast/Architecture.hpp"
#include "frontend/ast/ProcCallStat.hpp"
#include "frontend/ast/FunctionCall.hpp"
#include "frontend/ast/AssociationElement.hpp"
#include "frontend/ast/ProcedureDeclaration.hpp"
#include "frontend/ast/ValDeclaration.hpp"
#include "frontend/ast/LibUnit.hpp"
#include "frontend/visitor/ResolveTypes.hpp"
#include "frontend/reporting/CompileError.hpp"
#include "frontend/reporting/ErrorRegistry.hpp"

// subprogram: out, inout / signal -> driver to process
// signal gets assigned -> driver to process
//
// TODO it might be a more elegant idea to store ConstArrays 
//      as constants (and Aggregates as well).

namespace ast {

GatherImplicits::GatherImplicits() : mode(VMODE_SOURCE), driverCounter(0),
					uhfCounter(0)
{
}

void
GatherImplicits::visit(SigAssignStat &node)
{
	this->mode = VMODE_TARGET_SIGNAL;
	node.target->accept(*this);
	this->mode = VMODE_SOURCE;
	assert(node.waveForm);
	this->listTraverse(*node.waveForm);
}

void
GatherImplicits::visit(VarAssignStat &node)
{
	this->mode = VMODE_TARGET_VAR;
	node.target->accept(*this);
	this->mode = VMODE_SOURCE;
	node.source->accept(*this);
}

void
GatherImplicits::visit(SimpleName &node)
{

	switch(this->mode) {
	case VMODE_TARGET_SIGNAL: {
		SignalDeclaration *sig 
			= dynamic_cast<SignalDeclaration*>(
						node.getDeclaration());
		if (sig == NULL) {
			CompileError *err = new CompileError(node, 
				"Target of signal assignment not a signal.");
			ErrorRegistry::addError(err);
			return;
		}
		node.driver = this->registerDriver(*sig, node);
		break;
	    }
	case VMODE_TARGET_VAR: {
		// only check, if it is in fact a VarDeclaration.
		const VarDeclaration *var = 
			dynamic_cast<const VarDeclaration*>(
						node.getDeclaration());
		if (var == NULL) {
			CompileError *err = new CompileError(node,
				"Target of a variable assignment not a "
				"variable.");
			ErrorRegistry::addError(err);
		}
		break;
	    }
	case VMODE_SOURCE:
		// nothing to check
		break;
	}
}

void
GatherImplicits::visit(Process &node)
{
	assert(this->drivers.empty());
	formalMapT formalBackup = this->formals;
	this->formals.clear();

	assert(node.declarations != NULL);

	if (node.seqStats != NULL) {
		this->listTraverse(*node.seqStats);
	}

	assert(node.drivers.empty());

	for (std::map<const SignalDeclaration*, Driver*>::const_iterator i = 
		this->drivers.begin(); i != this->drivers.end(); i++) {
		
		node.drivers.push_back(i->second);
	}

	for (formalMapT::const_iterator i = this->formals.begin(); 
		i != this->formals.end(); i++) {

		node.declarations->push_back(i->second.first);
	}

	this->formals = formalBackup;
	this->drivers.clear();
}

void
GatherImplicits::visit(FunctionCall &node)
{
	// type resolution must have happened before
	assert(node.definition != NULL);

	switch(this->mode) {
	case VMODE_TARGET_SIGNAL:
	case VMODE_TARGET_VAR: {
		CompileError *err = 
			new CompileError(node, 
					"Invalid lvalue in assignment");
		ErrorRegistry::addError(err);
		return;
	    }
	default:
		// nothing to do
		break;
	}

	if (node.arguments == NULL) {
		return;
	}

	if (node.definition->arguments == NULL) {
		return;
	}

	std::list<ValDeclaration *>::const_iterator i = 
				node.definition->arguments->begin();

	for (std::list<AssociationElement*>::const_iterator j = 
		node.arguments->begin(); j != node.arguments->end();
		i++, j++) {

		assert(i != node.definition->arguments->end());

		switch ((*i)->storageClass) {
		case ValDeclaration::OBJ_CLASS_CONSTANT:
			// need hidden formals for composite types only.
			assert((*i)->subtypeIndic != NULL);
			switch ((*i)->subtypeIndic->baseType) {
			case BASE_TYPE_ARRAY:
			case BASE_TYPE_RECORD:
				(*j)->hiddenFormal = 
					this->registerFormal(*i, 
							(*j)->actual->type);
				break;

			default:
				break;
			}
			break;

		default:
			break;
		}
	}
}

void
GatherImplicits::visit(ProcCallStat &node)
{
	assert(node.definition != NULL);

	if (node.definition->arguments == NULL) {
		return;
	}

	// TODO register implicit formals as necessary.
	if (node.arguments == NULL) {
		return;
	}

	assert(node.definition->arguments != NULL);
	std::list<ValDeclaration*>::const_iterator f = 
		node.definition->arguments->begin();

	// pickup drivers from actuals
	for (std::list<AssociationElement*>::iterator i = 
		node.arguments->begin();
		i != node.arguments->end(); i++, f++) {

		this->pickupImplicits(**f, **i);
	}
}

void
GatherImplicits::pickupImplicits(
	ValDeclaration &formalDecl,
	AssociationElement &assoc
)
{
	switch (formalDecl.mode) {
	case ValDeclaration::MODE_OUT:
	case ValDeclaration::MODE_INOUT:
		switch (formalDecl.storageClass) {
		case ValDeclaration::OBJ_CLASS_SIGNAL:
			this->mode = VMODE_TARGET_SIGNAL;
			break;

		case ValDeclaration::OBJ_CLASS_VARIABLE:
			this->mode = VMODE_TARGET_VAR;
			break;

		default:
			break;
		}
	default:
		break;
	}

	switch (formalDecl.storageClass) {
	case ValDeclaration::OBJ_CLASS_CONSTANT:
		assert(formalDecl.subtypeIndic != NULL);

		// already handled by NormalizeAssocs, if there's no
		// association in the first place.
		// (then an association must have been created anyway,
		// together with a hidden formal).
		if (assoc.hiddenFormal != NULL) {
			this->mode = VMODE_SOURCE;
			return;
		}

		switch (formalDecl.subtypeIndic->baseType) {
		case BASE_TYPE_RECORD:
			assoc.hiddenFormal = 
				this->registerFormal(&formalDecl, 
						assoc.actual->type);
			break;

		case BASE_TYPE_ARRAY: {
			// if the actual is an unconstraint array,
			// we have a pointer to storage space already.
			// Hence we'll only need a hiddenFormal, if 
			// there's a constraint array.
			bool isConstraint = 
				ResolveTypes::isConstraintArray(
							assoc.actual->type);

			if (! isConstraint) {
				break;
			}
			assoc.hiddenFormal = 
				this->registerFormal(&formalDecl, 
						assoc.actual->type);
			break;
		    }
		default:
			break;
		}
		break;

	default:
		break;
	}

	assoc.actual->accept(*this);
	this->mode = VMODE_SOURCE;
}

void
GatherImplicits::visit(FunctionDeclaration &node)
{
	this->processCallable(node);
}

void
GatherImplicits::visit(ProcedureDeclaration &node)
{
	this->processCallable(node);
}

void
GatherImplicits::processCallable(Callable &node)
{
	assert(this->drivers.empty());
	formalMapT formalBackup = this->formals;
	this->formals.clear();

	if (node.definition != NULL) {
		node.definition->accept(*this);
	}

	// FIXME that's still ugly and will fail for procedures that 
	//       are defined in a process and directly modify signals.

	if (node.arguments != NULL) {
		for (std::list<ValDeclaration*>::const_iterator i = 
			node.arguments->begin();
			i != node.arguments->end(); i++) {

			switch ((*i)->mode) {
			case ValDeclaration::MODE_IN:
				break;

			case ValDeclaration::MODE_OUT:
			case ValDeclaration::MODE_INOUT:
				switch ((*i)->storageClass) {
				case ValDeclaration::OBJ_CLASS_SIGNAL:
					this->registerDriver(**i,
							SYMBOL_PARAMETER);
					break;

				default:
					break;
				}
				break;
			}
		}
	}


	for (std::map<const SignalDeclaration*, Driver*>::const_iterator i = 
		this->drivers.begin(); i != this->drivers.end(); i++) {
		node.drivers.push_back(i->second);
	}

	// FIXME do s.th. with formals!

	this->formals = formalBackup;
	this->drivers.clear();
}

void
GatherImplicits::process(LibUnit &node)
{
	if (node.declarations != NULL) {
		this->listTraverse(*node.declarations);
	}
}

void
GatherImplicits::visit(Architecture &node)
{
	assert(this->drivers.empty());
	assert(this->formals.empty());

	this->process(node);

	if (node.concurrentStats != NULL) {
		this->listTraverse(*node.concurrentStats);
	}

	for (formalMapT::const_iterator i = this->formals.begin();
		i != this->formals.end(); i++) {

		node.declarations->push_back(i->second.first);
	}

	this->drivers.clear();
	this->formals.clear();
}

void
GatherImplicits::visit(CompInstStat &node)
{
	// at this point, association lists have already been normalized
	
	assert(node.genericMap != NULL);
	assert(node.entity != NULL);
	assert(node.entity->generics != NULL);

	std::list<ConstantDeclaration *>::const_iterator g = 
					node.entity->generics->begin();

	for (std::list<AssociationElement *>::iterator i = 
		node.genericMap->begin();
		i != node.genericMap->end();
		i++, g++) {

		this->pickupImplicits(**g, **i);
	}
		
}

Driver *
GatherImplicits::registerDriver(const SignalDeclaration &decl, SimpleName &n)
{

	std::map<const SignalDeclaration*, Driver*>::const_iterator i = 
		this->drivers.find(&decl);
	
	if (i != this->drivers.end()) {
		// driver registered already
		return i->second;
	}

	// driver not yet present.
	Driver *d = new Driver(decl, this->driverCounter, &n);
	this->driverCounter++;
	this->drivers[&decl] = d;
	return d;
}

void
GatherImplicits::registerDriver(ValDeclaration &decl, enum symType t)
{
	// must not throw an exception, otherwise logic error
	SignalDeclaration &d = dynamic_cast<SignalDeclaration&>(decl);

	std::map<const SignalDeclaration*, Driver*>::const_iterator i = 
		this->drivers.find(&d);
	
	if (i != this->drivers.end()) {
		// driver registered already
		return;
	}

	Symbol *sym = new Symbol(d.name, t, NULL, d);
	std::list<Symbol *> cands = std::list<Symbol*>();
	cands.push_back(sym);
	SimpleName *sn = new SimpleName(new std::string(*d.name), cands, 
					d.location);
	this->registerDriver(d, *sn);
}

SimpleName *
GatherImplicits::registerFormal(
	const ValDeclaration *formal,
	TypeDeclaration *actualType
)
{
	formalMapT::const_iterator i = this->formals.find(formal);
	if (i != this->formals.end()) {
		return i->second.second;
	}

	std::string *name = new std::string("__");
	*name += *formal->name;
	*name += "__hidden_formal__";

	SubtypeIndication *si;
	bool isUnconstraint = formal->isUnconstraint();
	if (isUnconstraint) {
		si = dynamic_cast<SubtypeIndication*>(actualType);
		assert(si != NULL);
		*name += util::MiscUtil::toString(this->uhfCounter);
		this->uhfCounter++;
	} else {
		si = formal->subtypeIndic;
	}

	// generate a ValDeclaration
	VarDeclaration *hidden = 
		new VarDeclaration(
					formal->mode, 
					name, 
					NULL, 
					si,
					Location("__hidden_formal__"));
	Symbol *sym = new Symbol(name, SYMBOL_VARIABLE, NULL, *hidden);
	std::list<Symbol *> cands = std::list<Symbol*>();
	cands.push_back(sym);
	
	SimpleName *ref = new SimpleName(new std::string(*name), 
					cands,
					Location("__builtin__"));

	if (isUnconstraint) {
		// need seperate entries for each actual of a 
		// unconstraint formal, since the storage size
		// can differs.
		this->formals[hidden] =
			std::pair<ValDeclaration*, SimpleName*>(hidden, ref);
	} else {
		// but only one entry for a constraint formal, since
		// the storage size is fixed.
		this->formals[formal] = 
			std::pair<ValDeclaration*, SimpleName*>(hidden, ref);
	}
	return ref;
}


}; /* namespace ast */