File: GCBuiltins.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 (365 lines) | stat: -rw-r--r-- 8,019 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
/* $Id$ 
 *
 * Generate intermediate code, intermediate code implementation of builtins.
 *
 * 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/GCBuiltins.hpp"
#include <cassert>
#include "frontend/ast/AssociationElement.hpp"
#include "frontend/misc/BuiltinSymbolTable.hpp"
#include "intermediate/container/LabelFactory.hpp"
#include "intermediate/operands/ImmediateOperand.hpp"
#include "intermediate/opcodes/Mov.hpp"
#include "intermediate/opcodes/Je.hpp"
#include "intermediate/opcodes/Jne.hpp"
#include "intermediate/opcodes/Jb.hpp"
#include "intermediate/opcodes/Jbe.hpp"

namespace ast {

using namespace intermediate;

RegisterSet
GCBuiltinsNoShortCircuit::emitCode(
	GenCode &gc, 
	std::list<AssociationElement*> ops
) const
{
	std::list<RegisterSet> cOps;
	std::list<enum BaseType> btls;

	for (std::list<AssociationElement*>::iterator i = ops.begin(); 
		i != ops.end(); i++) {

		GenCode lgc = GenCode(gc.container);
		assert((*i)->formal == NULL); // FIXME
		assert((*i)->actual != NULL);

		(*i)->actual->accept(lgc);
		cOps.push_back(lgc.sourceRegs);
		btls.push_back((*i)->actual->baseType);
	}

	return this->calculate(*gc.container, cOps, btls);
}

RegisterSet
GCBuiltinsShortCircuit::emitCode(
	GenCode &gc,
	std::list<AssociationElement*> ops
) const
{
	// FIXME this doesn't work for composite types, e.g. bit-vectors.
	assert(ops.size() == 2);

	std::list<AssociationElement*>::iterator i = ops.begin();
	Label *out = LabelFactory::getLabel("builtin_shortcut_result");

	// result = <default>
	Register *result = gc.container->createRegister(OP_TYPE_INTEGER);
	Mov *def = new Mov(new ImmediateOperand(this->getDefault()), result);
	gc.container->addCode(def);

	// leftop = (..)
	Operand *leftOp = this->evaluate(gc.container, **i);
	i++;
	
	// if leftop == <shortCut> goto out
	Je *shortCut = 
		new Je(leftOp, 
			new ImmediateOperand(this->getShortCut()), 
			out);
	gc.container->addCode(shortCut);

	// rightop = (..)
	Operand *rightOp = this->evaluate(gc.container, **i);

	// if rightop == <shortCut> goto out
	Je *shortCut2 = 
		new Je(rightOp, 
			new ImmediateOperand(this->getShortCut()),
			out);
	gc.container->addCode(shortCut2);
	
	// result = 1 - <default>
	Mov *altRes = 
		new Mov(new ImmediateOperand(1 - this->getDefault()), result);
	gc.container->addCode(altRes);
	
	// out:
	gc.container->addCode(out);

	RegisterSet ret = RegisterSet(*gc.container);
	ret.setValue(result);

	return ret;
}

Operand *
GCBuiltinsShortCircuit::evaluate(
	CodeContainer *cc,
	AssociationElement &assoc
)
{
	GenCode gc = GenCode(cc);
	// FIXME composites
	assert(assoc.formal == NULL);
	assert(assoc.actual != NULL);
	assoc.actual->accept(gc);

	Operand *ret = 
		gc.sourceRegs.getValue(assoc.actual->baseType);
	assert(ret != NULL);
	return ret;
}

RegisterSet
GCBuiltinsUnop::calculate(
	CodeContainer &cc,
	std::list<RegisterSet> ops,
	std::list<enum BaseType> btl
) const
{
	assert(ops.size() == 1);
	RegisterSet r = *ops.begin();

	Operand *value = r.getValue(btl.front());

	Register *result = this->calcUnOp(cc, value);
	RegisterSet rset = RegisterSet(cc);
	rset.setValue(result);
	return rset;
}

RegisterSet
GCBuiltinsBinOp::calculate(
	CodeContainer &cc,
	std::list<RegisterSet> ops,
	std::list<enum BaseType> btl
) const
{
	assert(ops.size() == 2);
	std::list<RegisterSet>::const_iterator i = ops.begin();
	std::list<enum BaseType>::const_iterator j = btl.begin();

	RegisterSet left = *i;
	enum BaseType leftBT = *j;

	i++; j++;
	RegisterSet right = *i;
	enum BaseType rightBT = *j;

	Operand *leftOp = left.getValue(leftBT);
	Operand *rightOp = right.getValue(rightBT);

	assert(leftOp != NULL);
	assert(rightOp != NULL);

	Register *result = this->calcBinOp(cc, leftOp, rightOp);
	RegisterSet rset = RegisterSet(cc);
	rset.setValue(result);

	return rset;
}


/* ********************** implementation of builtins ******************* */

Register *
GCBuiltinsNot::calcUnOp(CodeContainer &cc, Operand *op) const
{
	// not for bit, boolean: 1 - op
	// (which is only valid if '1'/true == 1 and '0'/false == 0)
	assert(VHDL_TRUE == 1);
	assert(VHDL_FALSE == 0);

	Register *result = cc.createRegister(OP_TYPE_INTEGER);
	Sub *sub = new Sub(ImmediateOperand::getOne(), op, result);
	cc.addCode(sub);

	return result;
}

Register *
GCBuiltinsXor::calcBinOp(
	CodeContainer &cc, 
	Operand *left, 
	Operand *right
) const
{
	//t = left + right.
	//Xor = t == 2 ? 0 : t;
	Register *lsumr = cc.createRegister(OP_TYPE_INTEGER);
	Add *add = new Add(left, right, lsumr);
	cc.addCode(add);

	ImmediateOperand *two = 
		new ImmediateOperand(static_cast<universal_integer>(2));

	Label *out = LabelFactory::getLabel("xor_out");

	Jb *jb = new Jb(lsumr, two, out);
	cc.addCode(jb);

	Mov *mov = new Mov(ImmediateOperand::getZero(), lsumr);
	cc.addCode(mov);

	cc.addCode(out);

	return lsumr;
}

Register *
GCBuiltinsXnor::calcBinOp(
	CodeContainer &cc, 
	Operand *left, 
	Operand *right
) const
{
	//xnor : not xor -> 1 - xor(left, right)
	Register *ret = GCBuiltinsXor::calcBinOp(cc, left, right);
	Register *res = cc.createRegister(OP_TYPE_INTEGER);

	Sub *sub = new Sub(ImmediateOperand::getOne(), ret, res);
	cc.addCode(sub);

	return res;
}

Register *
GCBuiltinsCompare::calcBinOp(
	CodeContainer &cc,
	Operand *left,
	Operand *right
) const
{
	Register *result = cc.createRegister(OP_TYPE_INTEGER);
	ImmediateOperand *default_op = 
		new ImmediateOperand(this->getDefaultValue());

	Mov *mov = new Mov(default_op, result);
	cc.addCode(mov);

	Label *out = LabelFactory::getLabel("compare_out");
	OpCode *branch = this->emitBranch(left, right, out);
	cc.addCode(branch);

	ImmediateOperand *non_default_op = 
		new ImmediateOperand(1 - this->getDefaultValue());
	Mov *mov2 = new Mov(non_default_op, result);
	cc.addCode(mov2);

	cc.addCode(out);

	return result;
}

OpCode *
GCBuiltinsEqual::emitBranch(Operand *left, Operand *right, Label *out) const
{
	return new Je(left, right, out);
}

OpCode *
GCBuiltinsInEqual::emitBranch(Operand *left, Operand *right, Label *out) const
{
	return new Jne(left, right, out);
}

OpCode *
GCBuiltinsLess::emitBranch(Operand *left, Operand *right, Label *out) const
{
	return new Jb(left, right, out);
}

OpCode *
GCBuiltinsLessEqual::emitBranch(
	Operand *left, 
	Operand *right, 
	Label *out
) const
{
	return new Jbe(left, right, out);
}

OpCode *
GCBuiltinsGreater::emitBranch(
	Operand *left, 
	Operand *right, 
	Label *out
) const
{
	return new Jbe(left, right, out);
}

OpCode *
GCBuiltinsGreaterEqual::emitBranch(
	Operand *left, 
	Operand *right, 
	Label *out
) const
{
	return new Jb(left, right, out);
}

Register *
GCBuiltinsUnaryMinus::calcUnOp(CodeContainer &cc, Operand *op) const
{
	// unary minus: 0 - <value>
	Register *result = cc.createRegister(op->type);
	Sub *sub = new Sub(ImmediateOperand::getZero(), op, result);
	cc.addCode(sub);

	return result;
}

RegisterSet
GCBuiltinsIdentity::emitCode(
	GenCode &gc, 
	std::list<AssociationElement*> ops
) const
{
	assert(ops.size() == 1);
	AssociationElement *elem = ops.front();

	assert(elem->formal == NULL);
	assert(elem->actual != NULL);

	GenCode lgc = GenCode(gc.container);
	elem->actual->accept(lgc);

	// just pass on the RegisterSet of the argument.
	return lgc.sourceRegs;
}

Register *
GCBuiltinsAbs::calcUnOp(CodeContainer &cc, Operand *op) const
{

	//    res = op
	//    if 0 < res goto out
	//    res = 1 - res
	// out:
	//
	Register *result = cc.createRegister(op->type);
	Mov *ir = new Mov(op, result);
	cc.addCode(ir);

	Label *absOut = LabelFactory::getLabel("abs_out");
	Jb *goOut = new Jb(ImmediateOperand::getZero(), op, absOut);
	cc.addCode(goOut);

	Sub *invert = new Sub(ImmediateOperand::getZero(), op, result);
	cc.addCode(invert);

	cc.addCode(absOut);

	return result;
}

}; /* namespace ast */