File: UnconstraintBounds.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 (177 lines) | stat: -rw-r--r-- 4,017 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
/* $Id$
 *
 * Resolve ranges of positional and named aggregates, where the type
 * is an unconstraint array.
 *
 * Copyright (C) 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.
 */

#if 1 /* that is obsolete... use <cstdint> instead. */
#ifndef __STDC_LIMIT_MACROS
#define __STDC_LIMIT_MACROS
#endif /* __STDC_LIMIT_MACROS */
extern "C" {
#include "stdint.h"
};
#endif

#include "frontend/visitor/UnconstraintBounds.hpp"
#include "frontend/ast/Aggregate.hpp"
#include "frontend/ast/ConstInteger.hpp"
#include "frontend/ast/UnconstrainedArrayType.hpp"
#include "frontend/ast/RangeConstraintType.hpp"
#include "frontend/visitor/ConstantPropagation.hpp"
#include "frontend/visitor/ResolveTypes.hpp"

namespace ast {

UnconstraintBounds::UnconstraintBounds() : bounds(NULL),
					low(INT64_MAX), 
					high(INT64_MIN),
					numElements(0),
					positional(true) 
{
}


void
UnconstraintBounds::visit(Aggregate &node)
{
	// first fold constants (to reduce locally static expressions
	// in choices)
#if 0 /* FIXME */
	ConstantPropagation cp;
	node.accept(cp);
#endif
	assert(node.associations != NULL);
	this->listTraverse(*node.associations);

	if (this->positional) {
		// lrm 7.3.2.2: need to determine bounds
		// by base type.
		std::list<DiscreteRange*> drl;

		const UnconstrainedArrayType *ua =
			ResolveTypes::pickupIndexConstraint(node.type, drl);

		// constraint mustn't be set yet by type.
		assert(drl.empty());
		DiscreteRange *dr = UnconstraintBounds::findIndexRange(ua);

		// FIXME downto...
		switch (dr->direction) {
		case DiscreteRange::DIRECTION_DOWN:
			assert(false);
			break;

		default:
			break;
		}

		this->low = dr->getLowerBound();
		this->high = this->low - 1 + this->numElements;

		// check for overflow of NULL array
		if (this->numElements == 0) {
			assert(this->high < this->low);
		}
	}

	ConstInteger *lowBound = new ConstInteger(this->low, node.location);
	ConstInteger *upBound = new ConstInteger(this->high, node.location);
	this->bounds = new DiscreteRange(
				lowBound, 
				upBound,
				DiscreteRange::DIRECTION_UP,
				node.location);
}

void
UnconstraintBounds::visit(ElementAssociation &node)
{
	if (node.choices != NULL) {
		// named association
		this->positional = false;
		this->listTraverse(*node.choices);
	} else {
		this->numElements++;
	}
}

void
UnconstraintBounds::visit(Others &node)
{
	//TODO register error
	assert(false);
}

void
UnconstraintBounds::visit(ConstInteger &node)
{
	// must have come from a formal choice.
	if (node.value < this->low) {
		this->low = node.value;
	}

	if (this->high < node.value) {
		this->high = node.value;
	}
}

void
UnconstraintBounds::visit(DiscreteRange &node)
{
	assert(node.from != NULL);
	assert(node.to != NULL);

	node.from->accept(*this);
	node.to->accept(*this);
}

void
UnconstraintBounds::process(AstNode &node)
{
	// FIXME register error? or is it a logic error?
	assert(false);
}

DiscreteRange *
UnconstraintBounds::findIndexRange(const UnconstrainedArrayType *at)
{
	// FIXME specification not yet clear to me about 
	// multidimensional arrays... ignore for now.
	assert(at->indexTypes->size() == 1);

	const TypeDeclaration *it = at->indexTypes->front();
	assert(it != NULL);

	/* try to determine constraint from SubtypeIndication */
	const SubtypeIndication *sit = 
		dynamic_cast<const SubtypeIndication*>(it);
	
	while (sit->constraint == NULL) {
		const SubtypeIndication *sit2 = 
				dynamic_cast<const SubtypeIndication*>(
							sit->declaration);
		if (sit2 == NULL) {
			break;
		}
		sit = sit2;
	}
	if (sit->constraint != NULL) {
		return sit->constraint;
	}

	// not a subtype
	const RangeConstraintType *rt = 
		dynamic_cast<const RangeConstraintType*>(sit->declaration);

	assert(rt != NULL);
	assert(rt->constraint != NULL);
	return rt->constraint;
}

}; /* namespace ast */