File: FAUhdlc.cpp

package info (click to toggle)
fauhdlc 20180504-3.1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, 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 (560 lines) | stat: -rw-r--r-- 12,992 bytes parent folder | download | duplicates (2)
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/* $Id$ 
 *
 * FAUhdlc: main class to setup and execute compilation.
 *
 * Copyright (C) 2007-2010 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 <cstring>
#include <fstream>
#include <iostream>
#include <cassert>
extern "C" {
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#include <getopt.h>
#undef _GNU_SOURCE
#else /* _GNU_SOURCE not externally defined */
#include <getopt.h>
#endif /* _GNU_SOURCE externally defined */
};
#include "compiler/FAUhdlc.hpp"
#include "frontend/reporting/ErrorRegistry.hpp"
#include "frontend/newparser/ParserDriver.hpp"
#include "util/MiscUtil.hpp"
#include "util/GarbageCollect.hpp"
#include "frontend/visitor/DotVisitor.hpp"
#include "frontend/misc/BuiltinSymbolTable.hpp"
#include "frontend/visitor/ResolveTypes.hpp"
#include "frontend/visitor/SetPathName.hpp"
#include "frontend/visitor/GatherImplicits.hpp"
#include "frontend/visitor/GenCode.hpp"
#include "frontend/visitor/ConstantPropagation.hpp"
#include "frontend/visitor/CheckLoops.hpp"
#include "frontend/visitor/CheckAccessMode.hpp"
#include "frontend/visitor/NormalizeAssocLists.hpp"
#include "frontend/visitor/WaitConditions.hpp"
#include "frontend/visitor/WarnUnused.hpp"
#include "frontend/visitor/TransformSigAssign.hpp"
#include "intermediate/visitor/PrintCode.hpp"
#include "intermediate/visitor/GenCCode.hpp"
#include "intermediate/visitor/LookupSymbols.hpp"

namespace compiler {

FAUhdlc::FAUhdlc() : topNode(NULL), 
			parseOnly(false), 
			dotParse(false), 
			resultCode(FAUHDLC_EXIT_SUCCESS),
			dotParseFile(NULL),
			outputFile(NULL),
			symbolTable(NULL),
			iTopNode(NULL),
			dotConst(false),
			dotConstFile(NULL),
			freeStanding(false),
			cFile(NULL)
{
}

FAUhdlc::~FAUhdlc() 
{
	// cleanup memory (only useful for leak checking)
	for (std::list<struct FAUhdlc::LibFile*>::iterator i =
		this->sourceFiles.begin(); i != this->sourceFiles.end(); 
		i++) {

		delete (*i)->filename;
		delete *i;
	}
	util::MiscUtil::terminate(this->topNode);
	delete this->symbolTable;
	util::MiscUtil::terminate(this->iTopNode);
}

void
FAUhdlc::parseCmdLine(int argc, char** argv) 
{
	const char *currentLib = "work";
	bool success = true;
	bool again = true;
	int c;
	struct option l_opts[] = {
		/* name, has_arg, *flag, val */
		{"parse-only", 0, NULL, 'p'},
		{"dot-parse", 1, NULL, 'd'},
		{"dot-const", 1, NULL, 'c'},
		{"lib", 1, NULL, 'l'},
		{"output", 1, NULL, 'o'},
		{"c-output", 1, NULL, 'C'},
		{"help", 0, NULL, 'h'},
		{"freestanding", 0, NULL, 'f'},
		{0, 0, 0, 0}
	};



	while (again) {
		c = getopt_long(argc, argv, "-pd:c:l:o:hW:C:f", l_opts, NULL);

		switch (c) {
		case -1: /* no more options */
			again = false;
			break;

		case 1: /* "non"-option, name of compile file */ {
			struct FAUhdlc::LibFile *f = new struct FAUhdlc::LibFile();
			f->filename = new std::string(optarg);
			f->library = currentLib;

			this->sourceFiles.push_back(f);
			break;
		    }
		case 'p': /* parse-only */
			this->parseOnly = true;
			break;

		case 'd': /* dot-parse=<filename> */
			this->dotParse = true;
			this->dotParseFile = optarg;
			break;

		case 'c': /* dot-const=<filename> */
			this->dotConst = true;
			this->dotConstFile = optarg;
			break;

		case 'l': /* lib=<libname> */
			currentLib = optarg;
			break;

		case 'o': /* output=<filename> */
			this->outputFile = optarg;
			break;

		case 'h': /* help */
			success = false;
			break;

		case 'W': /* warning options */
			if (strcmp("error", optarg) == 0) {
				ast::ErrorRegistry::setWerror(true);
			} else {
				success = false;
			}
			break;

		case 'f': /* freestanding */
			this->freeStanding = true;
			break;

		case 'C': /* C-Output */
			this->cFile = optarg;
			break;
		};
	}

	/** at least one file? */
	if (this->sourceFiles.size() == 0) {
		success = false;
	}

	if (! success) {
		this->resultCode = FAUHDLC_EXIT_COMMAND_ARGS;
		this->usage(std::cerr);
	}
}

int 
FAUhdlc::run(void)
{
	if (this->resultCode != FAUHDLC_EXIT_SUCCESS) {
		return this->resultCode;
	}

	this->doParse();

	if (   (this->resultCode != FAUHDLC_EXIT_SUCCESS) 
            || (this->topNode == NULL)
	    || ast::ErrorRegistry::hasErrors()) {

	    	this->putCompileMessages();
		return this->resultCode;
	}

	if (this->parseOnly) {
		return this->resultCode;
	}

	try {
		this->doSemanticAnalysis();
	} catch (std::runtime_error &f) {
		this->resultCode = FAUHDLC_EXIT_EXCEPTION;
		std::cerr << f.what() << std::endl;
	} catch (ast::CompileError &ce) {
		this->resultCode = FAUHDLC_EXIT_EXCEPTION;
		std::cerr << "ERROR> " << ce << std::endl;
	} catch (...) {
		this->resultCode = FAUHDLC_EXIT_EXCEPTION;
		std::cerr << "unknown exception occured." << std::endl;
	}

	this->putCompileMessages();

	if (this->resultCode != FAUHDLC_EXIT_SUCCESS) {
		return this->resultCode;
	}

	try {
		this->doIntermediateTransform();
	} catch (std::runtime_error &f) {
		this->resultCode = FAUHDLC_EXIT_EXCEPTION;
		std::cerr << f.what() << std::endl;

	} catch (...) {
		this->resultCode = FAUHDLC_EXIT_EXCEPTION;
		std::cerr << "unknown exception occured." << std::endl;
	}

	return this->resultCode;
}

void
FAUhdlc::doParse(void)
{
	this->symbolTable = new ast::BuiltinSymbolTable();
	yy::ParserDriver driver = yy::ParserDriver(*symbolTable);

	if (! this->freeStanding) {
		FAUhdlc::LibFile *lf = new FAUhdlc::LibFile();
		lf->filename = new std::string(
					VHDL_DATA_DIR "/std_logic_1164.vhdl");
		lf->library = "ieee";
		this->sourceFiles.push_front(lf);
	}

	for (std::list<struct LibFile*>::const_iterator i = 
		this->sourceFiles.begin(); i != this->sourceFiles.end();
		i++) {
		
		try {
			driver.parse(*((*i)->filename), (*i)->library);
		} catch (yy::SyntaxError &e) {
			if (ast::ErrorRegistry::hasErrors()) {
				// don't report syntax errors, there are 
				// other, probably more accute errors
				this->resultCode = FAUHDLC_EXIT_ERROR;
				return;
			}

			std::cerr << e.what() << std::endl;
			this->resultCode = FAUHDLC_EXIT_ERROR;
			return;
		} catch (std::runtime_error &g) {
			std::cerr << g.what() << std::endl;
			this->resultCode = FAUHDLC_EXIT_EXCEPTION;
			return;
		} catch (ast::CompileError &h) {
			std::cerr << "ERROR> " << h << std::endl;
		} catch (...) {
			std::cerr << "unknown exception occured." 
				  << std::endl;
			this->resultCode = FAUHDLC_EXIT_EXCEPTION;
			return;
		}
	}
	this->topNode = driver.topNode;
	if (this->topNode == NULL) {
		this->resultCode = FAUHDLC_EXIT_EXCEPTION;
		return;
	}

	// dot graph after parsing?
	if (this->dotParse) {
#ifdef DEBUG
		std::cerr << "========= DOT VISITOR ==========" << std::endl;
#endif
		ast::DotVisitor v = ast::DotVisitor();
		this->topNode->accept(v);
		
		std::ofstream stream;
		stream.open(this->dotParseFile, std::ofstream::out);
		v.put(stream);
		stream.close();
	}
}

void
FAUhdlc::putCompileMessages(void)
{
	// write warnings if any 
	if (ast::ErrorRegistry::hasWarnings()) {
		ast::ErrorRegistry::putWarnings(std::cerr);
	}

	// write errors if any
	if (ast::ErrorRegistry::hasErrors()) {
		ast::ErrorRegistry::putErrors(std::cerr);
		std::cerr << std::endl;
		this->resultCode = FAUHDLC_EXIT_ERROR;
	}
}

void
FAUhdlc::doSemanticAnalysis(void)
{
#ifdef DEBUG
	std::cerr << "======= TRANSFORM SIGASSIGNS =====" << std::endl;
#endif
	ast::TransformSigAssign tsa = ast::TransformSigAssign();
	this->topNode->accept(tsa);

	if (ast::ErrorRegistry::hasErrors()) {
		return;
	}

#ifdef DEBUG
	std::cerr << "========= RESOLVE TYPES ==========" << std::endl;
#endif
	ast::ResolveTypes rt = ast::ResolveTypes(*this->symbolTable);
	this->topNode->accept(rt);

	if (ast::ErrorRegistry::hasErrors()) {
		return;
	}

#ifdef DEBUG
	std::cerr << "========= PROPAGATE CONSTANT ========" << std::endl;
#endif
	ast::ConstantPropagation cp = ast::ConstantPropagation();
	this->topNode->accept(cp);

	if (ast::ErrorRegistry::hasErrors()) {
		return;
	}

	if (this->dotConst) {
#ifdef DEBUG
		std::cerr << "========= DOT VISITOR ==========" << std::endl;
#endif
		ast::DotVisitor v = ast::DotVisitor();
		this->topNode->accept(v);
		
		std::ofstream stream;
		stream.open(this->dotConstFile, std::ofstream::out);
		v.put(stream);
		stream.close();
	}

#ifdef DEBUG
	std::cerr << "========= CHECK LOOPS ========" << std::endl;
#endif
	ast::CheckLoops cl = ast::CheckLoops();
	this->topNode->accept(cl);

	if (ast::ErrorRegistry::hasErrors()) {
		return;
	}

#ifdef DEBUG
	std::cerr << "========= NORMALIZE ASSOCS ========" << std::endl;
#endif
	ast::NormalizeAssocLists na = ast::NormalizeAssocLists();
	this->topNode->accept(na);

	if (ast::ErrorRegistry::hasErrors()) {
		return;
	}

#ifdef DEBUG
	std::cerr << "========= CHECK ACCESS MODE ========" << std::endl;
#endif
	ast::CheckAccessMode cam = ast::CheckAccessMode();
	this->topNode->accept(cam);

	if (ast::ErrorRegistry::hasErrors()) {
		return;
	}

#ifdef DEBUG
	std::cerr << "=========== WARN UNUSED ===========" << std::endl;
#endif
	ast::WarnUnused wuu = ast::WarnUnused();
	this->topNode->accept(wuu);

	if (ast::ErrorRegistry::hasErrors()) {
		return;
	}

#ifdef DEBUG
	std::cerr << "========= GATHER IMPLICITS ========" << std::endl;
#endif
	ast::GatherImplicits giv = ast::GatherImplicits();
	this->topNode->accept(giv);

	if (ast::ErrorRegistry::hasErrors()) {
		return;
	}

#ifdef DEBUG
	std::cerr << "========= SET PATH NAMES ==========" << std::endl;
#endif
	ast::SetPathName spn = ast::SetPathName();
	this->topNode->accept(spn);

	if (ast::ErrorRegistry::hasErrors()) {
		return;
	}

#ifdef DEBUG
	std::cerr << "========= CHECK WAIT CONDS ========" << std::endl;
#endif
	ast::WaitConditions cwc = ast::WaitConditions();
	this->topNode->accept(cwc);

	if (ast::ErrorRegistry::hasErrors()) {
		return;
	}


#ifdef DEBUG
	std::cerr << "========= GENERATE ICODE =========" << std::endl;
#endif
	ast::GenCode gc = ast::GenCode();
	this->topNode->accept(gc);

	if (ast::ErrorRegistry::hasErrors()) {
		return;
	}

	this->iTopNode = gc.container;
}

void
FAUhdlc::doIntermediateTransform(void)
{
	intermediate::LookupSymbols ls = intermediate::LookupSymbols();
	assert(this->iTopNode != NULL);
	this->iTopNode->accept(ls);

	this->printICode();
	this->genCCode();
}

void
FAUhdlc::printICode(void)
{
	if (this->outputFile == NULL) {
		return;
	}

	std::ofstream out;
	out.open(this->outputFile, std::ofstream::out);
	if (! out.good()) {
		out.close();
		std::cerr << "Couldn't open " << this->outputFile 
			<< " for writing." << std::endl;
		return;
	}
	intermediate::PrintCode pc = intermediate::PrintCode(out);

	assert(this->iTopNode != NULL);
	this->iTopNode->accept(pc);
	out.close();
}

void
FAUhdlc::genCCode(void)
{
	if (this->cFile == NULL) {
		return;
	}

	std::ofstream out;
	out.open(this->cFile, std::ofstream::out);
	if (! out.good()) {
		out.close();
		std::cerr << "Couldn't open " << this->cFile
			<< " for writing." << std::endl;
		return;
	}

	intermediate::GenCCode gcv = intermediate::GenCCode(out);

	assert(this->iTopNode != NULL);
	this->iTopNode->accept(gcv);
	out.close();
}

void
FAUhdlc::usage(std::ostream &out) const
{
	out << "FAUhdlc a VHDL to C compiler" << std::endl;
	out << std::endl;
	out << "Usage: fauhdlc [OPTIONS] files" << std::endl;
	out << "OPTIONS:" << std::endl;

	out << "\t--help              \tDisplay this help." << std::endl;
	out << "\t-o filaneme       "
            << "\tOutput to file filename." 
            << std::endl;

	out << "\t--parse-only       "
            << "\tstop with semantic analysis after parsing." 
            << std::endl;

	out << "\t--dot-parse=filename"
            << "\tCreate a dot-file called filename after parsing." 
            << std::endl;

	out << "\t--dot-const=filename"
            << "\tCreate a dot-file called filename after const. propagation."
            << std::endl;

	out << "\t--lib=library       "
	    << "\tAll following files belong to library." 
            << std::endl;
	
	out << "\t-Werror            "
	    << "\tReport warnings as errors."
	    << std::endl;

	out << "\t--freestanding     "
	    << "\tFreestanding mode (don't load std_logic)."
	    << std::endl;

	out << "\t--c-output         "
	    << "\tCreate output filename in C language."
	    << std::endl;

	out << std::endl;

	out << "For each source file, a separate library may be specified."
            << std::endl;
	out << "If no library has been specified, 'work' will be used." 
	    << std::endl;

	out << std::endl;
	out << "Exit codes:" << std::endl;
	out << "\t1\twrong command line parameters." << std::endl;
	out << "\t3\tcompile error was found failed." << std::endl;
	out << "\t4\tOther exception occurred." << std::endl;
	out << std::endl;
}



}; /* namespace compiler */

int
main(int argc, char** argv) 
{
	util::GarbageCollect::initialize();
	compiler::FAUhdlc fauhdlc = compiler::FAUhdlc();
	fauhdlc.parseCmdLine(argc, argv);

	return fauhdlc.run();
}