2012-04-13 01:08:20 +00:00
|
|
|
|
// -*- mode: C++; c-file-style: "cc-mode" -*-
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//*************************************************************************
|
|
|
|
|
// DESCRIPTION: Verilator: Emit C++ for 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
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
//
|
2018-01-02 23:05:06 +00:00
|
|
|
|
// Copyright 2003-2018 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.
|
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
|
|
|
|
|
#ifndef _V3EMITCBASE_H_
|
|
|
|
|
#define _V3EMITCBASE_H_ 1
|
|
|
|
|
|
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 "V3Global.h"
|
|
|
|
|
#include "V3File.h"
|
|
|
|
|
#include "V3Ast.h"
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Base Visitor class -- holds output file pointer
|
|
|
|
|
|
|
|
|
|
class EmitCBaseVisitor : public AstNVisitor {
|
|
|
|
|
public:
|
|
|
|
|
// STATE
|
|
|
|
|
V3OutCFile* m_ofp;
|
|
|
|
|
// METHODS
|
2009-07-22 18:38:20 +00:00
|
|
|
|
V3OutCFile* ofp() const { return m_ofp; }
|
2006-08-26 11:35:28 +00:00
|
|
|
|
void puts(const string& str) { ofp()->puts(str); }
|
|
|
|
|
void putbs(const string& str) { ofp()->putbs(str); }
|
2016-09-14 02:28:07 +00:00
|
|
|
|
void putsDecoration(const string& str) { if (v3Global.opt.decoration()) puts(str); }
|
2009-05-08 17:16:19 +00:00
|
|
|
|
void putsQuoted(const string& str) { ofp()->putsQuoted(str); }
|
2006-08-26 11:35:28 +00:00
|
|
|
|
bool optSystemC() { return v3Global.opt.systemC(); }
|
|
|
|
|
static string symClassName() { return v3Global.opt.prefix()+"__Syms"; }
|
2006-08-30 21:07:55 +00:00
|
|
|
|
static string symClassVar() { return symClassName()+"* __restrict vlSymsp"; }
|
2006-10-18 12:59:30 +00:00
|
|
|
|
static string symTopAssign() { return v3Global.opt.prefix()+"* __restrict vlTOPp VL_ATTR_UNUSED = vlSymsp->TOPp;"; }
|
2009-11-07 11:20:20 +00:00
|
|
|
|
static string modClassName(AstNodeModule* modp) { // Return name of current module being processed
|
2006-08-26 11:35:28 +00:00
|
|
|
|
if (modp->isTop()) {
|
|
|
|
|
return v3Global.opt.prefix();
|
|
|
|
|
} else {
|
|
|
|
|
return v3Global.opt.modPrefix() + "_" + modp->name();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
static string topClassName() { // Return name of top wrapper module
|
|
|
|
|
return v3Global.opt.prefix();
|
|
|
|
|
}
|
|
|
|
|
AstCFile* newCFile(const string& filename, bool slow, bool source) {
|
|
|
|
|
AstCFile* cfilep = new AstCFile(v3Global.rootp()->fileline(), filename);
|
|
|
|
|
cfilep->slow(slow);
|
|
|
|
|
cfilep->source(source);
|
|
|
|
|
v3Global.rootp()->addFilesp(cfilep);
|
|
|
|
|
return cfilep;
|
|
|
|
|
}
|
2018-02-02 02:32:58 +00:00
|
|
|
|
string cFuncArgs(const AstCFunc* nodep) {
|
2009-12-09 03:12:59 +00:00
|
|
|
|
// Return argument list for given C function
|
|
|
|
|
string args = nodep->argTypes();
|
|
|
|
|
// Might be a user function with argument list.
|
2018-02-02 02:32:58 +00:00
|
|
|
|
for (const AstNode* stmtp = nodep->argsp(); stmtp; stmtp=stmtp->nextp()) {
|
|
|
|
|
if (const AstVar* portp = VN_CAST_CONST(stmtp, Var)) {
|
2009-12-09 03:12:59 +00:00
|
|
|
|
if (portp->isIO() && !portp->isFuncReturn()) {
|
|
|
|
|
if (args != "") args+= ", ";
|
2009-12-20 13:27:00 +00:00
|
|
|
|
if (nodep->dpiImport() || nodep->dpiExportWrapper())
|
|
|
|
|
args += portp->dpiArgType(true,false);
|
|
|
|
|
else if (nodep->funcPublic())
|
|
|
|
|
args += portp->cPubArgType(true,false);
|
2014-04-15 22:50:04 +00:00
|
|
|
|
else args += portp->vlArgType(true,false,true);
|
2009-12-09 03:12:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return args;
|
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
|
|
// CONSTRUCTORS
|
|
|
|
|
EmitCBaseVisitor() {
|
|
|
|
|
m_ofp = NULL;
|
|
|
|
|
}
|
|
|
|
|
virtual ~EmitCBaseVisitor() {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
2008-11-17 22:13:57 +00:00
|
|
|
|
// Count operations under the given node, as a visitor of each AstNode
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
|
|
class EmitCBaseCounterVisitor : public AstNVisitor {
|
|
|
|
|
private:
|
|
|
|
|
// STATE
|
|
|
|
|
int m_count; // Number of statements
|
|
|
|
|
// VISITORS
|
2016-11-27 13:11:38 +00:00
|
|
|
|
virtual void visit(AstNode* nodep) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
m_count++;
|
2018-05-11 00:55:37 +00:00
|
|
|
|
iterateChildren(nodep);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
public:
|
|
|
|
|
// CONSTUCTORS
|
2015-10-04 02:33:06 +00:00
|
|
|
|
explicit EmitCBaseCounterVisitor(AstNode* nodep) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
m_count = 0;
|
2018-05-11 00:55:37 +00:00
|
|
|
|
iterate(nodep);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
virtual ~EmitCBaseCounterVisitor() {}
|
|
|
|
|
int count() const { return m_count; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif // guard
|