2012-04-13 03:08:20 +02:00
|
|
|
|
// -*- mode: C++; c-file-style: "cc-mode" -*-
|
2012-03-20 21:13:10 +01:00
|
|
|
|
//*************************************************************************
|
|
|
|
|
|
// DESCRIPTION: Verilator: Emit Verilog from tree
|
|
|
|
|
|
//
|
|
|
|
|
|
// Code available from: http://www.veripool.org/verilator
|
|
|
|
|
|
//
|
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
|
//
|
2018-01-03 00:05:06 +01:00
|
|
|
|
// Copyright 2004-2018 by Wilson Snyder. This program is free software; you can
|
2012-03-20 21:13:10 +01:00
|
|
|
|
// redistribute it and/or modify it under the terms of either the GNU
|
|
|
|
|
|
// Lesser General Public License Version 3 or the Perl Artistic License
|
|
|
|
|
|
// Version 2.0.
|
|
|
|
|
|
//
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
//
|
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
|
|
|
|
|
|
|
#include "config_build.h"
|
|
|
|
|
|
#include "verilatedos.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include "V3Global.h"
|
2012-08-27 03:13:47 +02:00
|
|
|
|
#include "V3String.h"
|
2012-03-20 21:13:10 +01:00
|
|
|
|
#include "V3EmitXml.h"
|
|
|
|
|
|
#include "V3EmitCBase.h"
|
|
|
|
|
|
|
2018-10-14 19:43:24 +02:00
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
#include <cstdarg>
|
|
|
|
|
|
#include <map>
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
2012-03-20 21:13:10 +01:00
|
|
|
|
//######################################################################
|
|
|
|
|
|
// Emit statements and math operators
|
|
|
|
|
|
|
2016-02-05 00:30:21 +01:00
|
|
|
|
class EmitXmlFileVisitor : public AstNVisitor {
|
2017-11-09 03:27:15 +01:00
|
|
|
|
// NODE STATE
|
|
|
|
|
|
//Entire netlist:
|
|
|
|
|
|
// AstNode::user1 -> uint64_t, number to connect crossrefs
|
|
|
|
|
|
|
2012-03-20 21:13:10 +01:00
|
|
|
|
// MEMBERS
|
|
|
|
|
|
V3OutFile* m_ofp;
|
2017-11-09 03:27:15 +01:00
|
|
|
|
uint64_t m_id;
|
2012-03-20 21:13:10 +01:00
|
|
|
|
|
|
|
|
|
|
// METHODS
|
2018-05-14 12:50:47 +02:00
|
|
|
|
VL_DEBUG_FUNC; // Declare debug()
|
2012-03-20 21:13:10 +01:00
|
|
|
|
|
|
|
|
|
|
// Outfile methods
|
|
|
|
|
|
V3OutFile* ofp() const { return m_ofp; }
|
|
|
|
|
|
virtual void puts(const string& str) { ofp()->puts(str); }
|
|
|
|
|
|
virtual void putbs(const string& str) { ofp()->putbs(str); }
|
|
|
|
|
|
virtual void putfs(AstNode*, const string& str) { putbs(str); }
|
|
|
|
|
|
virtual void putqs(AstNode*, const string& str) { putbs(str); }
|
|
|
|
|
|
virtual void putsNoTracking(const string& str) { ofp()->putsNoTracking(str); }
|
|
|
|
|
|
virtual void putsQuoted(const string& str) {
|
|
|
|
|
|
// Quote \ and " for use inside C programs
|
|
|
|
|
|
// Don't use to quote a filename for #include - #include doesn't \ escape.
|
|
|
|
|
|
// Duplicate in V3File - here so we can print to string
|
|
|
|
|
|
putsNoTracking("\"");
|
2014-11-28 18:40:46 +01:00
|
|
|
|
putsNoTracking(V3Number::quoteNameControls(str));
|
2012-03-20 21:13:10 +01:00
|
|
|
|
putsNoTracking("\"");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// XML methods
|
2017-11-09 03:27:15 +01:00
|
|
|
|
void outputId(AstNode* nodep) {
|
|
|
|
|
|
if (!nodep->user1()) { nodep->user1(++m_id); }
|
|
|
|
|
|
puts("\""+cvtToStr(nodep->user1())+"\"");
|
|
|
|
|
|
}
|
2012-03-20 21:13:10 +01:00
|
|
|
|
void outputTag(AstNode* nodep, string tag) {
|
2012-08-27 03:13:47 +02:00
|
|
|
|
if (tag=="") tag = VString::downcase(nodep->typeName());
|
2012-03-20 21:13:10 +01:00
|
|
|
|
puts("<"+tag+" "+nodep->fileline()->xml());
|
2018-02-02 03:32:58 +01:00
|
|
|
|
if (VN_IS(nodep, NodeDType)) { puts(" id="); outputId(nodep); }
|
2012-03-20 21:13:10 +01:00
|
|
|
|
if (nodep->name()!="") { puts(" name="); putsQuoted(nodep->prettyName()); }
|
2017-10-06 13:33:52 +02:00
|
|
|
|
if (nodep->tag()!="") { puts(" tag="); putsQuoted(nodep->tag()); }
|
2018-02-02 03:32:58 +01:00
|
|
|
|
if (AstNodeDType* dtp = VN_CAST(nodep, NodeDType)) {
|
2017-11-10 00:04:16 +01:00
|
|
|
|
if (dtp->subDTypep()) { puts(" sub_dtype_id="); outputId(dtp->subDTypep()->skipRefp()); }
|
2017-11-09 03:27:15 +01:00
|
|
|
|
} else {
|
2017-11-10 00:04:16 +01:00
|
|
|
|
if (nodep->dtypep()) { puts(" dtype_id="); outputId(nodep->dtypep()->skipRefp()); }
|
2017-11-09 03:27:15 +01:00
|
|
|
|
}
|
2012-03-20 21:13:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
void outputChildrenEnd(AstNode* nodep, string tag) {
|
2012-08-27 03:13:47 +02:00
|
|
|
|
if (tag=="") tag = VString::downcase(nodep->typeName());
|
2012-03-20 21:13:10 +01:00
|
|
|
|
if (nodep->op1p() || nodep->op2p() || nodep->op3p() || nodep->op4p()) {
|
|
|
|
|
|
puts(">\n");
|
2018-05-11 02:55:37 +02:00
|
|
|
|
iterateChildren(nodep);
|
2012-03-20 21:13:10 +01:00
|
|
|
|
puts("</"+tag+">\n");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
puts("/>\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// VISITORS
|
2018-01-31 13:25:10 +01:00
|
|
|
|
virtual void visit(AstAssignW* nodep) {
|
|
|
|
|
|
outputTag(nodep, "contassign"); // IEEE: vpiContAssign
|
|
|
|
|
|
outputChildrenEnd(nodep, "contassign");
|
|
|
|
|
|
}
|
|
|
|
|
|
virtual void visit(AstCell* nodep) {
|
|
|
|
|
|
outputTag(nodep, "instance"); // IEEE: vpiInstance
|
|
|
|
|
|
puts(" defName="); putsQuoted(nodep->modName()); // IEEE vpiDefName
|
2018-10-30 23:17:37 +01:00
|
|
|
|
puts(" origName="); putsQuoted(nodep->origName());
|
2018-01-31 13:25:10 +01:00
|
|
|
|
outputChildrenEnd(nodep, "instance");
|
|
|
|
|
|
}
|
2016-11-27 14:11:38 +01:00
|
|
|
|
virtual void visit(AstNetlist* nodep) {
|
2012-03-20 21:13:10 +01:00
|
|
|
|
puts("<netlist>\n");
|
2018-05-11 02:55:37 +02:00
|
|
|
|
iterateChildren(nodep);
|
2012-03-20 21:13:10 +01:00
|
|
|
|
puts("</netlist>\n");
|
|
|
|
|
|
}
|
2016-11-27 14:11:38 +01:00
|
|
|
|
virtual void visit(AstNodeModule* nodep) {
|
2012-03-20 21:13:10 +01:00
|
|
|
|
outputTag(nodep, "");
|
2018-10-30 23:17:37 +01:00
|
|
|
|
puts(" origName="); putsQuoted(nodep->origName());
|
2012-03-20 21:13:10 +01:00
|
|
|
|
if (nodep->level()==1 || nodep->level()==2) // ==2 because we don't add wrapper when in XML mode
|
|
|
|
|
|
puts(" topModule=\"1\""); // IEEE vpiTopModule
|
|
|
|
|
|
outputChildrenEnd(nodep, "");
|
|
|
|
|
|
}
|
2018-10-30 23:17:37 +01:00
|
|
|
|
virtual void visit(AstVar* nodep) {
|
2018-12-06 13:12:39 +01:00
|
|
|
|
AstVarType typ = nodep->varType();
|
|
|
|
|
|
string kw = nodep->verilogKwd();
|
|
|
|
|
|
string vt = nodep->dtypep()->name();
|
|
|
|
|
|
outputTag(nodep, "");
|
|
|
|
|
|
if (nodep->isIO()) {
|
|
|
|
|
|
puts(" dir="); putsQuoted(kw);
|
|
|
|
|
|
puts(" vartype="); putsQuoted(!vt.empty()
|
2018-12-07 01:06:20 +01:00
|
|
|
|
? vt : typ == AstVarType::PORT ? "port" : "unknown");
|
2018-12-06 13:12:39 +01:00
|
|
|
|
} else {
|
|
|
|
|
|
puts(" vartype="); putsQuoted(!vt.empty() ? vt : kw);
|
|
|
|
|
|
}
|
2018-10-30 23:17:37 +01:00
|
|
|
|
puts(" origName="); putsQuoted(nodep->origName());
|
|
|
|
|
|
outputChildrenEnd(nodep, "");
|
|
|
|
|
|
}
|
2016-11-27 14:11:38 +01:00
|
|
|
|
virtual void visit(AstPin* nodep) {
|
2018-10-27 23:29:00 +02:00
|
|
|
|
// What we call a pin in verilator is a port in the IEEE spec.
|
|
|
|
|
|
outputTag(nodep, "port"); // IEEE: vpiPort
|
|
|
|
|
|
if (nodep->modVarp()->isIO()) {
|
|
|
|
|
|
puts(" direction=\""+nodep->modVarp()->direction().xmlKwd()+"\"");
|
|
|
|
|
|
}
|
|
|
|
|
|
puts(" portIndex=\""+cvtToStr(nodep->pinNum())+"\""); // IEEE: vpiPortIndex
|
|
|
|
|
|
// Children includes vpiHighConn and vpiLowConn; we don't support port bits (yet?)
|
|
|
|
|
|
outputChildrenEnd(nodep, "port");
|
2012-03-20 21:13:10 +01:00
|
|
|
|
}
|
2018-01-31 13:29:14 +01:00
|
|
|
|
virtual void visit(AstSenItem* nodep) {
|
|
|
|
|
|
outputTag(nodep, "");
|
|
|
|
|
|
puts(" edgeType=\""+cvtToStr(nodep->edgeType().ascii())+"\""); // IEEE vpiTopModule
|
|
|
|
|
|
outputChildrenEnd(nodep, "");
|
|
|
|
|
|
}
|
2012-03-20 21:13:10 +01:00
|
|
|
|
|
|
|
|
|
|
// Data types
|
2016-11-27 14:11:38 +01:00
|
|
|
|
virtual void visit(AstBasicDType* nodep) {
|
2017-11-09 03:27:15 +01:00
|
|
|
|
outputTag(nodep, "basicdtype ");
|
2012-03-20 21:13:10 +01:00
|
|
|
|
if (nodep->isRanged()) {
|
|
|
|
|
|
puts(" left=\""+cvtToStr(nodep->left())+"\"");
|
|
|
|
|
|
puts(" right=\""+cvtToStr(nodep->right())+"\"");
|
|
|
|
|
|
}
|
|
|
|
|
|
puts("/>\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Default
|
2016-11-27 14:11:38 +01:00
|
|
|
|
virtual void visit(AstNode* nodep) {
|
2012-03-20 21:13:10 +01:00
|
|
|
|
outputTag(nodep, "");
|
|
|
|
|
|
outputChildrenEnd(nodep, "");
|
|
|
|
|
|
}
|
|
|
|
|
|
public:
|
|
|
|
|
|
EmitXmlFileVisitor(AstNode* nodep, V3OutFile* ofp) {
|
|
|
|
|
|
m_ofp = ofp;
|
2017-11-09 03:27:15 +01:00
|
|
|
|
m_id = 0;
|
2018-05-11 02:55:37 +02:00
|
|
|
|
iterate(nodep);
|
2012-03-20 21:13:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
virtual ~EmitXmlFileVisitor() {}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2018-11-02 00:53:26 +01:00
|
|
|
|
//######################################################################
|
|
|
|
|
|
// List of module files xml visitor
|
|
|
|
|
|
|
|
|
|
|
|
class ModuleFilesXmlVisitor : public AstNVisitor {
|
|
|
|
|
|
private:
|
|
|
|
|
|
// MEMBERS
|
|
|
|
|
|
std::ostream& m_os;
|
|
|
|
|
|
std::set<std::string> m_modulesCovered;
|
|
|
|
|
|
std::deque<FileLine*> m_nodeModules;
|
|
|
|
|
|
|
|
|
|
|
|
// METHODS
|
|
|
|
|
|
VL_DEBUG_FUNC; // Declare debug()
|
|
|
|
|
|
|
|
|
|
|
|
// VISITORS
|
|
|
|
|
|
virtual void visit(AstNetlist* nodep) {
|
|
|
|
|
|
// Children are iterated backwards to ensure correct compilation order
|
|
|
|
|
|
iterateChildrenBackwards(nodep);
|
|
|
|
|
|
}
|
|
|
|
|
|
virtual void visit(AstNodeModule* nodep) {
|
|
|
|
|
|
// Only list modules and interfaces
|
|
|
|
|
|
// Assumes modules and interfaces list is already sorted level wise
|
|
|
|
|
|
if (!nodep->dead()
|
|
|
|
|
|
&& (VN_IS(nodep, Module) || VN_IS(nodep, Iface))
|
|
|
|
|
|
&& m_modulesCovered.insert(nodep->fileline()->filename()).second) {
|
|
|
|
|
|
m_nodeModules.push_front(nodep->fileline());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
//-----
|
|
|
|
|
|
virtual void visit(AstNode* nodep) {
|
|
|
|
|
|
// All modules are present at root so no need to iterate on children
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
// CONSTRUCTORS
|
|
|
|
|
|
ModuleFilesXmlVisitor(AstNetlist* nodep, std::ostream& os)
|
|
|
|
|
|
: m_os(os), m_modulesCovered(), m_nodeModules() {
|
|
|
|
|
|
// Operate on whole netlist
|
|
|
|
|
|
nodep->accept(*this);
|
|
|
|
|
|
// Xml output
|
|
|
|
|
|
m_os<<"<module_files>\n";
|
|
|
|
|
|
for (std::deque<FileLine*>::iterator it = m_nodeModules.begin();
|
|
|
|
|
|
it != m_nodeModules.end(); it++) {
|
|
|
|
|
|
m_os<<"<file id=\""<<(*it)->filenameLetters()
|
|
|
|
|
|
<<"\" filename=\""<<(*it)->filename()
|
|
|
|
|
|
<<"\" language=\""<<(*it)->language().ascii()<<"\"/>\n";
|
|
|
|
|
|
}
|
|
|
|
|
|
m_os<<"</module_files>\n";
|
|
|
|
|
|
}
|
|
|
|
|
|
virtual ~ModuleFilesXmlVisitor() {}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
|
// Hierarchy of Cells visitor
|
|
|
|
|
|
|
|
|
|
|
|
class HierCellsXmlVisitor : public AstNVisitor {
|
|
|
|
|
|
private:
|
|
|
|
|
|
// MEMBERS
|
|
|
|
|
|
std::ostream& m_os;
|
|
|
|
|
|
std::string m_hier;
|
|
|
|
|
|
bool m_hasChildren;
|
|
|
|
|
|
|
|
|
|
|
|
// METHODS
|
|
|
|
|
|
VL_DEBUG_FUNC; // Declare debug()
|
|
|
|
|
|
|
|
|
|
|
|
// VISITORS
|
|
|
|
|
|
virtual void visit(AstNodeModule* nodep) {
|
|
|
|
|
|
if (nodep->level() >= 0
|
|
|
|
|
|
&& nodep->level() <=2 ) { // ==2 because we don't add wrapper when in XML mode
|
|
|
|
|
|
m_os<<"<cells>\n";
|
|
|
|
|
|
m_os<<"<cell "<<nodep->fileline()->xml()
|
|
|
|
|
|
<<" name=\""<<nodep->name()<<"\""
|
|
|
|
|
|
<<" submodname=\""<<nodep->name()<<"\""
|
|
|
|
|
|
<<" hier=\""<<nodep->name()<<"\"";
|
|
|
|
|
|
m_hier = nodep->name() + ".";
|
|
|
|
|
|
m_hasChildren = false;
|
|
|
|
|
|
iterateChildren(nodep);
|
|
|
|
|
|
if (m_hasChildren) {
|
|
|
|
|
|
m_os<<"</cell>\n";
|
|
|
|
|
|
} else {
|
|
|
|
|
|
m_os<<"/>\n";
|
|
|
|
|
|
}
|
|
|
|
|
|
m_os<<"</cells>\n";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
virtual void visit(AstCell* nodep) {
|
|
|
|
|
|
if (nodep->modp()->dead()) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!m_hasChildren) m_os<<">\n";
|
|
|
|
|
|
m_os<<"<cell "<<nodep->fileline()->xml()
|
|
|
|
|
|
<<" name=\""<<nodep->name()<<"\""
|
|
|
|
|
|
<<" submodname=\""<<nodep->modName()<<"\""
|
|
|
|
|
|
<<" hier=\""<<m_hier+nodep->name()<<"\"";
|
|
|
|
|
|
std::string hier = m_hier;
|
|
|
|
|
|
m_hier += nodep->name() + ".";
|
|
|
|
|
|
m_hasChildren = false;
|
|
|
|
|
|
iterateChildren(nodep->modp());
|
|
|
|
|
|
if (m_hasChildren) {
|
|
|
|
|
|
m_os<<"</cell>\n";
|
|
|
|
|
|
} else {
|
|
|
|
|
|
m_os<<"/>\n";
|
|
|
|
|
|
}
|
|
|
|
|
|
m_hier = hier;
|
|
|
|
|
|
m_hasChildren = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
//-----
|
|
|
|
|
|
virtual void visit(AstNode* nodep) {
|
|
|
|
|
|
iterateChildren(nodep);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
// CONSTRUCTORS
|
|
|
|
|
|
HierCellsXmlVisitor(AstNetlist* nodep, std::ostream& os)
|
|
|
|
|
|
: m_os(os), m_hier(""), m_hasChildren(false) {
|
|
|
|
|
|
// Operate on whole netlist
|
|
|
|
|
|
nodep->accept(*this);
|
|
|
|
|
|
}
|
|
|
|
|
|
virtual ~HierCellsXmlVisitor() {}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2012-03-20 21:13:10 +01:00
|
|
|
|
//######################################################################
|
|
|
|
|
|
// EmitXml class functions
|
|
|
|
|
|
|
|
|
|
|
|
void V3EmitXml::emitxml() {
|
|
|
|
|
|
UINFO(2,__FUNCTION__<<": "<<endl);
|
|
|
|
|
|
// All-in-one file
|
|
|
|
|
|
V3OutXmlFile of (v3Global.opt.makeDir()+"/"+v3Global.opt.prefix()+".xml");
|
|
|
|
|
|
of.putsHeader();
|
|
|
|
|
|
of.puts("<!-- DESCR" "IPTION: Verilator output: XML representation of netlist -->\n");
|
|
|
|
|
|
of.puts("<verilator_xml>\n");
|
|
|
|
|
|
{
|
2018-01-31 13:25:10 +01:00
|
|
|
|
std::stringstream sstr;
|
2012-03-20 21:13:10 +01:00
|
|
|
|
FileLine::fileNameNumMapDumpXml(sstr);
|
|
|
|
|
|
of.puts(sstr.str());
|
|
|
|
|
|
}
|
2018-11-02 00:53:26 +01:00
|
|
|
|
{
|
|
|
|
|
|
std::stringstream sstr;
|
|
|
|
|
|
ModuleFilesXmlVisitor moduleFilesVisitor (v3Global.rootp(), sstr);
|
|
|
|
|
|
HierCellsXmlVisitor cellsVisitor (v3Global.rootp(), sstr);
|
|
|
|
|
|
of.puts(sstr.str());
|
|
|
|
|
|
}
|
2012-03-20 21:13:10 +01:00
|
|
|
|
EmitXmlFileVisitor visitor (v3Global.rootp(), &of);
|
|
|
|
|
|
of.puts("</verilator_xml>\n");
|
|
|
|
|
|
}
|