2006-08-26 11:35:28 +00:00
|
|
|
|
//*************************************************************************
|
|
|
|
|
// DESCRIPTION: Verilator: Emit Verilog from tree
|
|
|
|
|
//
|
2008-04-25 12:14:27 +00:00
|
|
|
|
// Code available from: http://www.veripool.org/verilator
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//
|
|
|
|
|
// AUTHORS: Wilson Snyder with Paul Wasson, Duane Gabli
|
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
//
|
2009-01-02 16:47:39 +00:00
|
|
|
|
// Copyright 2004-2009 by Wilson Snyder. This program is free software; you can
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// redistribute it and/or modify it under the terms of either the GNU
|
2009-05-04 21:07:57 +00:00
|
|
|
|
// Lesser General Public License Version 3 or the Perl Artistic License
|
|
|
|
|
// Version 2.0.
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//
|
|
|
|
|
// Verilator is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
|
2006-12-18 19:20:45 +00:00
|
|
|
|
#include "config_build.h"
|
|
|
|
|
#include "verilatedos.h"
|
2008-06-30 17:11:25 +00:00
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstdarg>
|
2006-08-26 11:35:28 +00:00
|
|
|
|
#include <unistd.h>
|
2008-06-30 17:11:25 +00:00
|
|
|
|
#include <cmath>
|
2006-08-26 11:35:28 +00:00
|
|
|
|
#include <map>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
#include "V3Global.h"
|
|
|
|
|
#include "V3EmitV.h"
|
|
|
|
|
#include "V3EmitCBase.h"
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Emit statements and math operators
|
|
|
|
|
|
2008-11-20 12:55:54 +00:00
|
|
|
|
class EmitVBaseVisitor : public EmitCBaseVisitor {
|
|
|
|
|
// MEMBERS
|
2006-08-26 11:35:28 +00:00
|
|
|
|
bool m_suppressSemi;
|
|
|
|
|
|
|
|
|
|
// METHODS
|
2009-01-21 21:56:50 +00:00
|
|
|
|
static int debug() {
|
|
|
|
|
static int level = -1;
|
|
|
|
|
if (VL_UNLIKELY(level < 0)) level = v3Global.opt.debugSrcLevel(__FILE__);
|
|
|
|
|
return level;
|
|
|
|
|
}
|
|
|
|
|
|
2008-11-20 12:55:54 +00:00
|
|
|
|
virtual void puts(const string& str) = 0;
|
|
|
|
|
virtual void putbs(const string& str) = 0;
|
|
|
|
|
virtual void putsNoTracking(const string& str) = 0;
|
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// VISITORS
|
|
|
|
|
virtual void visit(AstNetlist* nodep, AstNUser*) {
|
|
|
|
|
nodep->iterateChildren(*this);
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstModule* nodep, AstNUser*) {
|
|
|
|
|
putbs("module "+modClassName(nodep)+";\n");
|
|
|
|
|
nodep->iterateChildren(*this);
|
|
|
|
|
puts("endmodule\n");
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstNodeFTask* nodep, AstNUser*) {
|
|
|
|
|
putbs(nodep->castTask() ? "task ":"function ");
|
|
|
|
|
puts(nodep->name());
|
|
|
|
|
puts(";\n");
|
|
|
|
|
putbs("begin\n");
|
|
|
|
|
nodep->stmtsp()->iterateAndNext(*this);
|
|
|
|
|
puts("end\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void visit(AstBegin* nodep, AstNUser*) {
|
|
|
|
|
putbs("begin\n");
|
|
|
|
|
nodep->iterateChildren(*this);
|
|
|
|
|
puts("end\n");
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstGenerate* nodep, AstNUser*) {
|
|
|
|
|
putbs("generate\n");
|
|
|
|
|
nodep->iterateChildren(*this);
|
|
|
|
|
puts("end\n");
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstFinal* nodep, AstNUser*) {
|
|
|
|
|
putbs("final begin\n");
|
|
|
|
|
nodep->iterateChildren(*this);
|
|
|
|
|
puts("end\n");
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstInitial* nodep, AstNUser*) {
|
|
|
|
|
putbs("initial begin\n");
|
|
|
|
|
nodep->iterateChildren(*this);
|
|
|
|
|
puts("end\n");
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstAlways* nodep, AstNUser*) {
|
|
|
|
|
putbs("always ");
|
|
|
|
|
nodep->sensesp()->iterateAndNext(*this);
|
2008-11-20 12:55:54 +00:00
|
|
|
|
putbs(" begin\n");
|
2006-08-26 11:35:28 +00:00
|
|
|
|
nodep->bodysp()->iterateAndNext(*this);
|
|
|
|
|
puts("end\n");
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstNodeAssign* nodep, AstNUser*) {
|
|
|
|
|
nodep->lhsp()->iterateAndNext(*this);
|
|
|
|
|
putbs(" "+nodep->verilogKwd()+" ");
|
|
|
|
|
nodep->rhsp()->iterateAndNext(*this);
|
|
|
|
|
if (!m_suppressSemi) puts(";\n");
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstAssignDly* nodep, AstNUser*) {
|
|
|
|
|
nodep->lhsp()->iterateAndNext(*this);
|
|
|
|
|
putbs(" <= ");
|
|
|
|
|
nodep->rhsp()->iterateAndNext(*this);
|
|
|
|
|
puts(";\n");
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstAssignAlias* nodep, AstNUser*) {
|
2009-10-07 01:46:24 +00:00
|
|
|
|
putbs("alias ");
|
2006-08-26 11:35:28 +00:00
|
|
|
|
nodep->lhsp()->iterateAndNext(*this);
|
|
|
|
|
putbs(" = ");
|
|
|
|
|
nodep->rhsp()->iterateAndNext(*this);
|
|
|
|
|
if (!m_suppressSemi) puts(";\n");
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstAssignW* nodep, AstNUser*) {
|
|
|
|
|
putbs("assign ");
|
|
|
|
|
nodep->lhsp()->iterateAndNext(*this);
|
|
|
|
|
putbs(" = ");
|
|
|
|
|
nodep->rhsp()->iterateAndNext(*this);
|
|
|
|
|
if (!m_suppressSemi) puts(";\n");
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstSenTree* nodep, AstNUser*) {
|
2008-11-20 12:55:54 +00:00
|
|
|
|
// AstSenItem is called for dumping in isolation by V3Order
|
2006-08-26 11:35:28 +00:00
|
|
|
|
putbs("@(");
|
2009-01-07 14:37:59 +00:00
|
|
|
|
for (AstNode* expp=nodep->sensesp(); expp; expp = expp->nextp()) {
|
|
|
|
|
nodep->iterateChildren(*this);
|
|
|
|
|
if (expp->nextp()) puts(" or ");
|
|
|
|
|
}
|
2008-11-20 12:55:54 +00:00
|
|
|
|
puts(")");
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
2009-01-07 14:37:59 +00:00
|
|
|
|
virtual void visit(AstSenGate* nodep, AstNUser*) {
|
|
|
|
|
emitVerilogFormat(nodep, nodep->emitVerilog(), nodep->sensesp(), nodep->rhsp());
|
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
virtual void visit(AstSenItem* nodep, AstNUser*) {
|
|
|
|
|
putbs("");
|
|
|
|
|
puts(nodep->edgeType().verilogKwd());
|
2008-11-20 12:55:54 +00:00
|
|
|
|
if (nodep->sensp()) puts(" ");
|
2006-08-26 11:35:28 +00:00
|
|
|
|
nodep->iterateChildren(*this);
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstNodeCase* nodep, AstNUser*) {
|
|
|
|
|
putbs(nodep->verilogKwd());
|
|
|
|
|
puts(" (");
|
|
|
|
|
nodep->exprp()->iterateAndNext(*this);
|
|
|
|
|
puts(")\n");
|
|
|
|
|
if (AstCase* casep = nodep->castCase()) {
|
|
|
|
|
if (casep->fullPragma() || casep->parallelPragma()) {
|
|
|
|
|
puts(" // synopsys");
|
|
|
|
|
if (casep->fullPragma()) puts(" full_case");
|
|
|
|
|
if (casep->parallelPragma()) puts(" parallel_case");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
nodep->itemsp()->iterateAndNext(*this);
|
|
|
|
|
putbs("endcase");
|
|
|
|
|
puts("\n");
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstCaseItem* nodep, AstNUser*) {
|
|
|
|
|
if (nodep->condsp()) {
|
|
|
|
|
nodep->condsp()->iterateAndNext(*this);
|
|
|
|
|
} else putbs("default");
|
|
|
|
|
putbs(": begin ");
|
|
|
|
|
nodep->bodysp()->iterateAndNext(*this);
|
|
|
|
|
puts("end\n");
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstComment* nodep, AstNUser*) {
|
|
|
|
|
puts((string)"// "+nodep->name()+"\n");
|
|
|
|
|
nodep->iterateChildren(*this);
|
|
|
|
|
}
|
2008-12-10 22:10:03 +00:00
|
|
|
|
virtual void visit(AstCoverDecl*, AstNUser*) {} // N/A
|
|
|
|
|
virtual void visit(AstCoverInc*, AstNUser*) {} // N/A
|
2008-12-12 20:34:02 +00:00
|
|
|
|
virtual void visit(AstCoverToggle*, AstNUser*) {} // N/A
|
2008-07-01 18:15:10 +00:00
|
|
|
|
|
|
|
|
|
void visitNodeDisplay(AstNode* nodep, AstNode* filep, const string& text, AstNode* exprsp) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
putbs(nodep->verilogKwd());
|
|
|
|
|
putbs(" (");
|
2008-07-01 18:15:10 +00:00
|
|
|
|
if (filep) { filep->iterateAndNext(*this); putbs(","); }
|
2006-08-26 11:35:28 +00:00
|
|
|
|
puts("\"");
|
2009-05-08 17:16:19 +00:00
|
|
|
|
putsNoTracking(text); // Not putsQuoted, as display text contains \ already
|
2006-08-26 11:35:28 +00:00
|
|
|
|
puts("\"");
|
2008-07-01 18:15:10 +00:00
|
|
|
|
for (AstNode* expp=exprsp; expp; expp = expp->nextp()) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
puts(",");
|
|
|
|
|
expp->iterateAndNext(*this);
|
|
|
|
|
}
|
|
|
|
|
puts(");\n");
|
|
|
|
|
}
|
2008-07-01 18:15:10 +00:00
|
|
|
|
virtual void visit(AstDisplay* nodep, AstNUser*) {
|
|
|
|
|
visitNodeDisplay(nodep, nodep->filep(), nodep->text(), nodep->exprsp());
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstFScanF* nodep, AstNUser*) {
|
|
|
|
|
visitNodeDisplay(nodep, nodep->filep(), nodep->text(), nodep->exprsp());
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstSScanF* nodep, AstNUser*) {
|
|
|
|
|
visitNodeDisplay(nodep, nodep->fromp(), nodep->text(), nodep->exprsp());
|
|
|
|
|
}
|
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
|
virtual void visit(AstFOpen* nodep, AstNUser*) {
|
|
|
|
|
putbs(nodep->verilogKwd());
|
|
|
|
|
putbs(" (");
|
|
|
|
|
if (nodep->filep()) nodep->filep()->iterateChildren(*this);
|
|
|
|
|
putbs(",");
|
|
|
|
|
if (nodep->filenamep()) nodep->filenamep()->iterateChildren(*this);
|
|
|
|
|
putbs(",");
|
|
|
|
|
if (nodep->modep()) nodep->modep()->iterateChildren(*this);
|
|
|
|
|
puts(");\n");
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstFClose* nodep, AstNUser*) {
|
|
|
|
|
putbs(nodep->verilogKwd());
|
|
|
|
|
putbs(" (");
|
|
|
|
|
if (nodep->filep()) nodep->filep()->iterateChildren(*this);
|
|
|
|
|
puts(");\n");
|
|
|
|
|
}
|
2008-06-27 12:45:05 +00:00
|
|
|
|
virtual void visit(AstFFlush* nodep, AstNUser*) {
|
|
|
|
|
putbs(nodep->verilogKwd());
|
|
|
|
|
putbs(" (");
|
|
|
|
|
if (nodep->filep()) nodep->filep()->iterateChildren(*this);
|
|
|
|
|
puts(");\n");
|
|
|
|
|
}
|
2006-12-19 14:09:57 +00:00
|
|
|
|
virtual void visit(AstReadMem* nodep, AstNUser*) {
|
|
|
|
|
putbs(nodep->verilogKwd());
|
|
|
|
|
putbs(" (");
|
|
|
|
|
if (nodep->filenamep()) nodep->filenamep()->iterateChildren(*this);
|
|
|
|
|
putbs(",");
|
|
|
|
|
if (nodep->memp()) nodep->memp()->iterateChildren(*this);
|
|
|
|
|
if (nodep->lsbp()) { putbs(","); nodep->lsbp()->iterateChildren(*this); }
|
|
|
|
|
if (nodep->msbp()) { putbs(","); nodep->msbp()->iterateChildren(*this); }
|
|
|
|
|
puts(");\n");
|
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
virtual void visit(AstNodeFor* nodep, AstNUser*) {
|
|
|
|
|
puts("for (");
|
|
|
|
|
m_suppressSemi = true;
|
|
|
|
|
nodep->initsp()->iterateAndNext(*this);
|
|
|
|
|
puts(";");
|
|
|
|
|
nodep->condp()->iterateAndNext(*this);
|
|
|
|
|
puts(";");
|
2006-09-05 20:06:23 +00:00
|
|
|
|
nodep->incsp()->iterateAndNext(*this);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
m_suppressSemi = false;
|
|
|
|
|
puts(") {\n");
|
|
|
|
|
nodep->bodysp()->iterateAndNext(*this);
|
2009-02-26 03:06:59 +00:00
|
|
|
|
puts("}\n");
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstRepeat* nodep, AstNUser*) {
|
|
|
|
|
puts("repeat (");
|
|
|
|
|
nodep->countp()->iterateAndNext(*this);
|
|
|
|
|
puts(") {\n");
|
|
|
|
|
nodep->bodysp()->iterateAndNext(*this);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
puts("}\n");
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstWhile* nodep, AstNUser*) {
|
|
|
|
|
nodep->precondsp()->iterateAndNext(*this);
|
|
|
|
|
puts("while (");
|
|
|
|
|
nodep->condp()->iterateAndNext(*this);
|
|
|
|
|
puts(") {\n");
|
|
|
|
|
nodep->bodysp()->iterateAndNext(*this);
|
|
|
|
|
nodep->precondsp()->iterateAndNext(*this); // Need to recompute before next loop
|
|
|
|
|
puts("}\n");
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstNodeIf* nodep, AstNUser*) {
|
|
|
|
|
puts("if (");
|
|
|
|
|
nodep->condp()->iterateAndNext(*this);
|
|
|
|
|
puts(") begin\n");
|
|
|
|
|
nodep->ifsp()->iterateAndNext(*this);
|
|
|
|
|
if (nodep->elsesp()) {
|
|
|
|
|
puts("end\n");
|
|
|
|
|
puts("else begin\n");
|
|
|
|
|
nodep->elsesp()->iterateAndNext(*this);
|
|
|
|
|
}
|
|
|
|
|
puts("end\n");
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstStop*, AstNUser*) {
|
|
|
|
|
putbs("$stop;\n");
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstFinish*, AstNUser*) {
|
|
|
|
|
putbs("$finish;\n");
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstText* nodep, AstNUser*) {
|
2008-11-20 12:55:54 +00:00
|
|
|
|
putsNoTracking(nodep->text());
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
2007-06-14 16:41:32 +00:00
|
|
|
|
virtual void visit(AstScopeName* nodep, AstNUser*) {
|
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
virtual void visit(AstCStmt* nodep, AstNUser*) {
|
|
|
|
|
putbs("$_CSTMT(");
|
|
|
|
|
nodep->bodysp()->iterateAndNext(*this);
|
|
|
|
|
puts(");\n");
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstCMath* nodep, AstNUser*) {
|
|
|
|
|
putbs("$_CMATH(");
|
|
|
|
|
nodep->bodysp()->iterateAndNext(*this);
|
|
|
|
|
puts(");\n");
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstUCStmt* nodep, AstNUser*) {
|
|
|
|
|
putbs("$c(");
|
|
|
|
|
nodep->bodysp()->iterateAndNext(*this);
|
|
|
|
|
puts(");\n");
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstUCFunc* nodep, AstNUser*) {
|
|
|
|
|
putbs("$c(");
|
|
|
|
|
nodep->bodysp()->iterateAndNext(*this); puts(")\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Operators
|
|
|
|
|
virtual void emitVerilogFormat(AstNode* nodep, const string& format,
|
|
|
|
|
AstNode* lhsp=NULL, AstNode* rhsp=NULL, AstNode* thsp=NULL) {
|
|
|
|
|
// Look at emitVerilog() format for term/uni/dual/triops,
|
|
|
|
|
// and write out appropriate text.
|
|
|
|
|
// %l lhsp - if appropriate
|
|
|
|
|
// %r rhsp - if appropriate
|
|
|
|
|
// %t thsp - if appropriate
|
|
|
|
|
// %k Potential line break
|
|
|
|
|
bool inPct = false;
|
|
|
|
|
putbs("");
|
|
|
|
|
for (string::const_iterator pos = format.begin(); pos != format.end(); ++pos) {
|
|
|
|
|
if (pos[0]=='%') {
|
|
|
|
|
inPct = true;
|
|
|
|
|
} else if (!inPct) { // Normal text
|
|
|
|
|
string s; s+=pos[0]; puts(s);
|
|
|
|
|
} else { // Format character
|
|
|
|
|
inPct = false;
|
|
|
|
|
switch (*pos) {
|
|
|
|
|
case '%': puts("%"); break;
|
|
|
|
|
case 'k': putbs(""); break;
|
|
|
|
|
case 'l': {
|
|
|
|
|
if (!lhsp) { nodep->v3fatalSrc("emitVerilog() references undef node"); }
|
|
|
|
|
else lhsp->iterateAndNext(*this);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'r': {
|
|
|
|
|
if (!rhsp) { nodep->v3fatalSrc("emitVerilog() references undef node"); }
|
|
|
|
|
else rhsp->iterateAndNext(*this);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 't': {
|
|
|
|
|
if (!thsp) { nodep->v3fatalSrc("emitVerilog() references undef node"); }
|
|
|
|
|
else thsp->iterateAndNext(*this);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
nodep->v3fatalSrc("Unknown emitVerilog format code: %"<<pos[0]);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void visit(AstNodeTermop* nodep, AstNUser*) {
|
|
|
|
|
emitVerilogFormat(nodep, nodep->emitVerilog());
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstNodeUniop* nodep, AstNUser*) {
|
|
|
|
|
emitVerilogFormat(nodep, nodep->emitVerilog(), nodep->lhsp());
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstNodeBiop* nodep, AstNUser*) {
|
|
|
|
|
emitVerilogFormat(nodep, nodep->emitVerilog(), nodep->lhsp(), nodep->rhsp());
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstNodeTriop* nodep, AstNUser*) {
|
|
|
|
|
emitVerilogFormat(nodep, nodep->emitVerilog(), nodep->lhsp(), nodep->rhsp(), nodep->thsp());
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstAttrOf* nodep, AstNUser*) {
|
|
|
|
|
puts("$_ATTROF(");
|
|
|
|
|
nodep->fromp()->iterateAndNext(*this);
|
|
|
|
|
puts(")");
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstNodeCond* nodep, AstNUser*) {
|
|
|
|
|
putbs("(");
|
|
|
|
|
nodep->condp()->iterateAndNext(*this); putbs(" ? ");
|
|
|
|
|
nodep->expr1p()->iterateAndNext(*this); putbs(" : ");
|
|
|
|
|
nodep->expr2p()->iterateAndNext(*this); puts(")");
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstRange* nodep, AstNUser*) {
|
|
|
|
|
puts("[");
|
2009-10-25 20:53:55 +00:00
|
|
|
|
nodep->msbEndianedp()->iterateAndNext(*this); puts(":");
|
|
|
|
|
nodep->lsbEndianedp()->iterateAndNext(*this); puts("]");
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstSel* nodep, AstNUser*) {
|
|
|
|
|
nodep->fromp()->iterateAndNext(*this); puts("[");
|
|
|
|
|
if (nodep->lsbp()->castConst()) {
|
|
|
|
|
if (nodep->widthp()->isOne()) {
|
|
|
|
|
nodep->lsbp()->iterateAndNext(*this);
|
|
|
|
|
} else {
|
2008-10-06 13:59:22 +00:00
|
|
|
|
puts(cvtToStr(nodep->lsbp()->castConst()->toSInt()
|
|
|
|
|
+nodep->widthp()->castConst()->toSInt()
|
2006-08-26 11:35:28 +00:00
|
|
|
|
-1));
|
|
|
|
|
puts(":");
|
2008-10-06 13:59:22 +00:00
|
|
|
|
puts(cvtToStr(nodep->lsbp()->castConst()->toSInt()));
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
nodep->lsbp()->iterateAndNext(*this); puts("+:");
|
|
|
|
|
nodep->widthp()->iterateAndNext(*this); puts("]");
|
|
|
|
|
}
|
|
|
|
|
puts("]");
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstNodeFTaskRef* nodep, AstNUser*) {
|
|
|
|
|
if (nodep->dotted()!="") { puts(nodep->dotted()); puts("."); }
|
|
|
|
|
puts(nodep->name());
|
|
|
|
|
puts("(");
|
|
|
|
|
nodep->pinsp()->iterateAndNext(*this);
|
|
|
|
|
puts(")");
|
|
|
|
|
}
|
|
|
|
|
// Terminals
|
|
|
|
|
virtual void visit(AstVarRef* nodep, AstNUser*) {
|
|
|
|
|
puts(nodep->hiername());
|
2008-06-12 16:03:47 +00:00
|
|
|
|
puts(nodep->varp()->name());
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstVarXRef* nodep, AstNUser*) {
|
|
|
|
|
puts(nodep->dotted());
|
|
|
|
|
puts(".");
|
2008-06-12 16:03:47 +00:00
|
|
|
|
puts(nodep->varp()->name());
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstConst* nodep, AstNUser*) {
|
|
|
|
|
puts(nodep->num().ascii(true,true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Just iterate
|
|
|
|
|
virtual void visit(AstTopScope* nodep, AstNUser*) {
|
|
|
|
|
nodep->iterateChildren(*this);
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstScope* nodep, AstNUser*) {
|
|
|
|
|
nodep->iterateChildren(*this);
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstVar* nodep, AstNUser*) {
|
|
|
|
|
puts(nodep->verilogKwd());
|
|
|
|
|
puts(" ");
|
|
|
|
|
if (nodep->isSigned()) puts("signed ");
|
2009-11-02 13:06:04 +00:00
|
|
|
|
nodep->dtypep()->iterateChildren(*this);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
puts(nodep->name());
|
|
|
|
|
puts(";\n");
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstNodeText*, AstNUser*) {}
|
|
|
|
|
virtual void visit(AstTraceDecl*, AstNUser*) {}
|
|
|
|
|
virtual void visit(AstTraceInc*, AstNUser*) {}
|
|
|
|
|
// NOPs
|
|
|
|
|
virtual void visit(AstPragma*, AstNUser*) {}
|
|
|
|
|
virtual void visit(AstCell*, AstNUser*) {} // Handled outside the Visit class
|
|
|
|
|
// Default
|
|
|
|
|
virtual void visit(AstNode* nodep, AstNUser*) {
|
2009-07-09 21:39:24 +00:00
|
|
|
|
puts((string)"\n???? // "+nodep->prettyTypeName()+"\n");
|
2006-08-26 11:35:28 +00:00
|
|
|
|
nodep->iterateChildren(*this);
|
2009-07-09 21:39:24 +00:00
|
|
|
|
nodep->v3fatalSrc("Unknown node type reached emitter: "<<nodep->prettyTypeName());
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
2008-11-20 12:55:54 +00:00
|
|
|
|
EmitVBaseVisitor() {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
m_suppressSemi = false;
|
|
|
|
|
}
|
2008-11-20 12:55:54 +00:00
|
|
|
|
virtual ~EmitVBaseVisitor() {}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
2008-11-20 12:55:54 +00:00
|
|
|
|
// Emit to an output file
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
2008-11-20 12:55:54 +00:00
|
|
|
|
class EmitVFileVisitor : public EmitVBaseVisitor {
|
|
|
|
|
// MEMBERS
|
|
|
|
|
V3OutFile* m_ofp;
|
|
|
|
|
// METHODS
|
2009-07-22 18:38:20 +00:00
|
|
|
|
V3OutFile* ofp() const { return m_ofp; }
|
2008-11-20 12:55:54 +00:00
|
|
|
|
void puts(const string& str) { ofp()->puts(str); }
|
|
|
|
|
void putbs(const string& str) { ofp()->putbs(str); }
|
|
|
|
|
void putsNoTracking(const string& str) { ofp()->putsNoTracking(str); }
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
EmitVFileVisitor(AstNode* nodep, V3OutFile* ofp) {
|
|
|
|
|
m_ofp = ofp;
|
|
|
|
|
nodep->accept(*this);
|
|
|
|
|
}
|
|
|
|
|
virtual ~EmitVFileVisitor() {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Emit to a stream (perhaps stringstream)
|
|
|
|
|
|
|
|
|
|
class EmitVStreamVisitor : public EmitVBaseVisitor {
|
|
|
|
|
// MEMBERS
|
|
|
|
|
ostream& m_os;
|
|
|
|
|
// METHODS
|
|
|
|
|
void puts(const string& str) { m_os<<str; }
|
|
|
|
|
void putbs(const string& str) { m_os<<str; }
|
|
|
|
|
void putsNoTracking(const string& str) { m_os<<str; }
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
EmitVStreamVisitor(AstNode* nodep, ostream& os)
|
|
|
|
|
: m_os(os) {
|
|
|
|
|
nodep->accept(*this);
|
|
|
|
|
}
|
|
|
|
|
virtual ~EmitVStreamVisitor() {}
|
|
|
|
|
};
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// EmitV class functions
|
|
|
|
|
|
|
|
|
|
void V3EmitV::emitv() {
|
|
|
|
|
UINFO(2,__FUNCTION__<<": "<<endl);
|
|
|
|
|
if (1) {
|
|
|
|
|
// All-in-one file
|
|
|
|
|
V3OutVFile of (v3Global.opt.makeDir()+"/"+v3Global.opt.prefix()+"__Vout.v");
|
|
|
|
|
of.putsHeader();
|
2008-11-20 12:55:54 +00:00
|
|
|
|
EmitVFileVisitor visitor (v3Global.rootp(), &of);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
} else {
|
|
|
|
|
// Process each module in turn
|
|
|
|
|
for (AstModule* modp = v3Global.rootp()->modulesp(); modp; modp=modp->nextp()->castModule()) {
|
2008-11-20 12:55:54 +00:00
|
|
|
|
V3OutVFile of (v3Global.opt.makeDir()
|
|
|
|
|
+"/"+EmitCBaseVisitor::modClassName(modp)+"__Vout.v");
|
2006-08-26 11:35:28 +00:00
|
|
|
|
of.putsHeader();
|
2008-11-20 12:55:54 +00:00
|
|
|
|
EmitVFileVisitor visitor (modp, &of);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-11-20 12:55:54 +00:00
|
|
|
|
|
|
|
|
|
void V3EmitV::verilogForTree(AstNode* nodep, ostream& os) {
|
|
|
|
|
EmitVStreamVisitor(nodep, os);
|
|
|
|
|
}
|