File: GCArrays.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 (423 lines) | stat: -rw-r--r-- 11,046 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
/* $Id$ 
 *
 * Generate intermediate code, array specific parts.
 *
 * 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/GCArrays.hpp"
#include <cassert>
#include "frontend/visitor/GenCode.hpp"
#include "frontend/visitor/ResolveTypes.hpp"
#include "frontend/visitor/GCTypes.hpp"
#include "frontend/ast/DiscreteRange.hpp"
#include "frontend/ast/UnconstrainedArrayType.hpp"
#include "intermediate/operands/RegisterFactory.hpp"
#include "intermediate/operands/ImmediateOperand.hpp"
#include "intermediate/operands/IndirectOperand.hpp"
#include "intermediate/container/LabelFactory.hpp"
#include "intermediate/container/TypeFactory.hpp"
#include "intermediate/opcodes/Mov.hpp"
#include "intermediate/opcodes/Je.hpp"
#include "intermediate/opcodes/Jb.hpp"
#include "intermediate/opcodes/Jbe.hpp"
#include "intermediate/opcodes/Jmp.hpp"
#include "intermediate/opcodes/Sub.hpp"
#include "intermediate/opcodes/IMul.hpp"
#include "intermediate/opcodes/Add.hpp"
#include "intermediate/opcodes/AOffset.hpp"

namespace ast {

using namespace intermediate;

/*
 * ===================== ARRAY HANDLING =======================
 */

ArrayHandling::ArrayHandling(
	TypeDeclaration *at,
	Operand *b,
	CodeContainer &container,
	std::list<Operand *> lbounds,
	std::list<Operand *> rbounds,
	std::list<Operand *> directs
) :		arrayType(at),
		base(b),
		cc(container),
		leftBounds(lbounds),
		rightBounds(rbounds),
		directions(directs)
{
	assert(at != NULL);
	assert(at->baseType == BASE_TYPE_ARRAY);

	GCTypes::GenTypeElements gte = 
		GCTypes::GenTypeElements(false, NULL, *this->arrayType, NULL);
	this->arrayType->accept(gte);

	this->indices = gte.getIndices();
	for (std::list<DiscreteRange*>::const_iterator i = 
		this->indices.begin();
		i != this->indices.end(); i++) {

		ImmediateOperand *lb = 
			new ImmediateOperand((*i)->getLeftBound());
		ImmediateOperand *rb = 
			new ImmediateOperand((*i)->getRightBound());
		ImmediateOperand *direct = 
			new ImmediateOperand((*i)->getDirection());
		this->leftBounds.push_back(lb);
		this->rightBounds.push_back(rb);
		this->directions.push_back(direct);
	}

	assert(gte.composite.size() == 1);
	assert(gte.referredTypes.size() == 1);

	TypeElement *elem = gte.composite.front();
	std::string name = elem->name;

	this->itype = TypeFactory::lookupType(name);
	assert(this->itype != NULL);

	this->elementType = gte.referredTypes.front();
}

ArrayHandling::~ArrayHandling()
{
}

void
ArrayHandling::factorize(void)
{
	// reverse dimension sizes
	// (..., d6, d5, d4, d3, d2)
	std::list<Operand *> rsizes;

	assert(this->leftBounds.size() == this->rightBounds.size());
	assert(this->leftBounds.size() > 0);

	std::list<Operand *>::const_reverse_iterator lbit = 
		this->leftBounds.rbegin();
	std::list<Operand *>::const_reverse_iterator rbit = 
		this->rightBounds.rbegin();
	std::list<Operand *>::const_reverse_iterator dirit = 
		this->directions.rbegin();

	std::list<Operand *>::const_reverse_iterator llast = 
		this->leftBounds.rend();
	llast--;
	// iterate over all but *first* entry (first is the actual dimension
	// that gets subscribed to with the first subscription element,
	// so the factor is always constant 1)
	for (; lbit != llast; lbit++, rbit++, dirit++) {
		// size := [(right - left) * direction] + 1
		
		Register *r1 = this->cc.createRegister(OP_TYPE_INTEGER);
		Sub *diff = new Sub(*rbit, *lbit, r1);
		this->cc.addCode(diff);

		Register *r2 = this->cc.createRegister(OP_TYPE_INTEGER);
		Register *r3 = this->cc.createRegister(OP_TYPE_INTEGER);
		
		IMul *m = new IMul(r1, *dirit, r2);

		Add *inc = new Add(r2, ImmediateOperand::getOne(), r3);
		this->cc.addCode(m);
		this->cc.addCode(inc);
		rsizes.push_back(r3);
	}

	// factors should be
	// (d2 * d3 * d4 * d5 * d6) 
	// (d3 * d4 * d5 * d6)
	// (d4 * d5 * d6)
	// (d5 * d6)
	// (d6)
	intermediate::Operand *factorial = ImmediateOperand::getOne();
	for (std::list<Operand *>::const_iterator i = rsizes.begin();
		i != rsizes.end(); i++) {

		Register *r1 = this->cc.createRegister(OP_TYPE_INTEGER);
		IMul *m = new IMul(factorial, *i, r1);
		this->cc.addCode(m);
		factorial = r1;
		this->dimensionFactors.push_front(factorial);
	}
	
	// also store 1 as factor for first dimension
	this->dimensionFactors.push_front(ImmediateOperand::getOne());
}

Register *
ArrayHandling::subscribe(std::list<Operand *> relativeIndices)
{
	// array(x, y, z) 
	// corresponds to (d1, d2, d3, d4, d5, d6)
	//
	// -->   (x - left(d1)) * direction1 * factor(d2, d3, d4, d5, d6)
	//     + (y - left(d2)) * direction2 * factor(d3, d4, d5, d6)
	//     + (y - left(d3)) * direction3 * factor(d4, d5, d6)
	//
	// the corresponding factors should be in dimensionFactors
	// or can be created with factorize().
	//
	if (this->dimensionFactors.empty()) {
		this->factorize();
	}

	assert(! this->dimensionFactors.empty());
	assert(this->dimensionFactors.size() >= relativeIndices.size());
	assert(this->itype != NULL);

	Operand *offset = ImmediateOperand::getZero();
	std::list<Operand *>::const_iterator f = 
		this->dimensionFactors.begin();
	std::list<Operand *>::const_iterator lb = 
		this->leftBounds.begin();
	std::list<Operand *>::const_iterator dir = 
		this->directions.begin();
	std::list<Operand *>::const_iterator ri = relativeIndices.begin();

	while (ri != relativeIndices.end()) {
		Register *r1 = this->cc.createRegister(OP_TYPE_INTEGER);
		Register *r2 = this->cc.createRegister(OP_TYPE_INTEGER);
		Register *r3 = this->cc.createRegister(OP_TYPE_INTEGER);
		Register *r4 = this->cc.createRegister(OP_TYPE_INTEGER);

		Sub *s1 = new Sub(*ri, *lb, r1);
		IMul *m1 = new IMul(r1, *f, r2);
		IMul *m2 = new IMul(r2, *dir, r3);
		Add *a1 = new Add(r3, offset, r4);
		offset = r4;

		this->cc.addCode(s1);
		this->cc.addCode(m1);
		this->cc.addCode(m2);
		this->cc.addCode(a1);
		f++; lb++; ri++; dir++;
	}

	Register *result = this->cc.createRegister(OP_TYPE_POINTER);
	AOffset *ao = new AOffset(this->base, offset, this->itype, result);
	this->cc.addCode(ao);

	return result;
}

universal_integer
ArrayHandling::transform_idx(
	const TypeDeclaration *arrayType,
	universal_integer idx
)
{
	std::list<DiscreteRange *> idcs;
	ResolveTypes::pickupIndexConstraint(arrayType, idcs);

	assert(idcs.size() == 1);
	DiscreteRange *dr = idcs.front();

	universal_integer ret = 
		(idx - dr->getLeftBound()) * dr->getDirection();
	return ret;
}

/*
 * =================== STATIC ARRAY ITERATE ===================
 */
void
StaticArrayIterate::iterate(void)
{
	std::list<universal_integer> lbounds = std::list<universal_integer>();
	std::list<universal_integer> rbounds = std::list<universal_integer>();
	std::list<universal_integer> ds = std::list<universal_integer>();
	std::list<universal_integer> i = std::list<universal_integer>();
	std::list<Operand*> offsetL = std::list<Operand*>();

	for (std::list<DiscreteRange*>::const_iterator d = 
		this->indices.begin();
		d != this->indices.end(); d++) {

		universal_integer lb = (*d)->getLeftBound();
		universal_integer rb = (*d)->getRightBound();
		universal_integer dir = (*d)->getDirection();

		lbounds.push_back(lb);
		i.push_back(lb);
		rbounds.push_back(rb);
		ds.push_back(dir);
	}

	while (StaticArrayIterate::checkLoop(i, rbounds, ds)) {

		for (std::list<universal_integer>::const_iterator i1 = 
			i.begin(); i1 != i.end(); i1++) {

			offsetL.push_back(new ImmediateOperand(*i1));
		}

		Register *element = this->subscribe(offsetL);
		this->iterateBody(element, i);

		StaticArrayIterate::incCounters(i, lbounds, rbounds, ds);
		offsetL.clear();
	}
}

bool
StaticArrayIterate::checkLoop(
	const std::list<universal_integer> &counters,
	const std::list<universal_integer> &rbounds,
	const std::list<universal_integer> &directions
)
{
	std::list<universal_integer>::const_iterator i = counters.begin();
	std::list<universal_integer>::const_iterator j = rbounds.begin();
	std::list<universal_integer>::const_iterator d = directions.begin();

	while (i != counters.end()) {
		bool ret = (((*i) * (*d)) <= ((*j) * (*d)));
		if (! ret) {
			return false;
		}

		i++;
		j++;
	}

	return true;
}

void
StaticArrayIterate::incCounters(
	std::list<universal_integer> &counters,
	const std::list<universal_integer> &lbounds,
	const std::list<universal_integer> &rbounds,
	const std::list<universal_integer> &directions
)
{
	bool carry = false;
	std::list<universal_integer>::iterator i = counters.begin();
	std::list<universal_integer>::const_iterator l = lbounds.begin();
	std::list<universal_integer>::const_iterator r = rbounds.begin();
	std::list<universal_integer>::const_iterator d = directions.begin();

	(*i) += (*d);

	while (i != counters.end()) {
		if (carry) {
			(*i) += (*d);
			carry = false;
		}

		if (((*r) * (*d)) < ((*i) * (*d))) {
			carry = true;
			(*i) = (*l);
		}

		i++;
		r++;
		l++;
		d++;
	}

	// make sure, that at least one index overflows if all 
	// values have been handled, otherwise checkLoop would
	// never yield false.
	if (carry) {
		i = counters.begin();
		r = rbounds.begin();
		d = directions.begin();
		(*i) = (*r) + (*d);
	}
}

/*
 * =================== ARRAY ITERATE ===================
 */

void
ArrayIterate::initCounters(void)
{
	for (std::list<Operand *>::const_iterator i = 
		this->leftBounds.begin();
		i != this->leftBounds.end();
		i++) {

		Register *b = this->cc.createRegister(OP_TYPE_INTEGER);
		Mov *m = new Mov(*i, b);
		this->cc.addCode(m);
		this->counters.push_back(b);
	}
}

void
ArrayIterate::incCounters(void)
{
	std::list<Operand*>::const_reverse_iterator rbi = 
		this->rightBounds.rbegin();
	std::list<Operand*>::const_reverse_iterator lbi = 
		this->leftBounds.rbegin();
	std::list<Operand*>::const_reverse_iterator di = 
		this->directions.rbegin();

	for (std::list<Register *>::reverse_iterator i = 
		this->counters.rbegin(); i != this->counters.rend();
		i++) {

		// c = c + direction
		Add *a = new Add(*di, *i, *i);
		this->cc.addCode(a);

		Register *r1 = this->cc.createRegister(OP_TYPE_INTEGER);
		Register *r2 = this->cc.createRegister(OP_TYPE_INTEGER);

		// r1 = c * direction
		// r2 = rightBound * direction
		IMul *m1 = new IMul(*i, *di, r1);
		IMul *m2 = new IMul(*rbi, *di, r2);

		this->cc.addCode(m1);
		this->cc.addCode(m2);

		// if r1 <= r2 goto loop
		Jbe *jbe = new Jbe(r1, r2, this->loop);
		this->cc.addCode(jbe);

		// c = lowerBound.
		Mov *m = new Mov(*lbi, *i);
		this->cc.addCode(m);

		rbi++;
		lbi++;
		di++;
	}

	// we're done with the iteration.
}

void
ArrayIterate::iterate(void)
{
	this->initCounters();
	this->cc.addCode(this->loop);
	std::list<Operand *> idx;

	for (std::list<Register *>::iterator i = this->counters.begin();
		i != this->counters.end(); i++) {

		idx.push_back(*i);
	}

	Register *elem = this->subscribe(idx);
	this->iterateBody(elem, this->counters);

	this->incCounters();
}

}; /* namespace ast */