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
|
|
|
|
//
|
2019-11-08 03:33:59 +00:00
|
|
|
// Code available from: https://verilator.org
|
2006-08-26 11:35:28 +00:00
|
|
|
//
|
|
|
|
//*************************************************************************
|
|
|
|
//
|
2020-03-21 15:24:24 +00:00
|
|
|
// Copyright 2003-2020 by Wilson Snyder. This program is free software; you
|
|
|
|
// can 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.
|
2020-03-21 15:24:24 +00:00
|
|
|
// SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
|
2006-08-26 11:35:28 +00:00
|
|
|
//
|
|
|
|
//*************************************************************************
|
2019-10-05 00:17:11 +00:00
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
#ifndef _V3EMITCBASE_H_
|
|
|
|
#define _V3EMITCBASE_H_ 1
|
|
|
|
|
2006-12-18 19:20:45 +00:00
|
|
|
#include "config_build.h"
|
|
|
|
#include "verilatedos.h"
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
#include "V3Global.h"
|
|
|
|
#include "V3File.h"
|
|
|
|
#include "V3Ast.h"
|
|
|
|
|
2018-10-14 17:43:24 +00:00
|
|
|
#include <cstdarg>
|
|
|
|
#include <cmath>
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
//######################################################################
|
|
|
|
// Base Visitor class -- holds output file pointer
|
|
|
|
|
|
|
|
class EmitCBaseVisitor : public AstNVisitor {
|
|
|
|
public:
|
|
|
|
// STATE
|
2019-05-19 20:13:13 +00:00
|
|
|
V3OutCFile* m_ofp;
|
2019-09-27 07:44:23 +00:00
|
|
|
bool m_trackText; // Always track AstText nodes
|
2006-08-26 11:35:28 +00:00
|
|
|
// METHODS
|
2019-05-19 20:13:13 +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(); }
|
2019-10-06 17:24:21 +00:00
|
|
|
static string protect(const string& name) { return VIdProtect::protectIf(name, true); }
|
|
|
|
static string protectIf(const string& name, bool doIt) {
|
|
|
|
return VIdProtect::protectIf(name, doIt); }
|
|
|
|
static string protectWordsIf(const string& name, bool doIt) {
|
|
|
|
return VIdProtect::protectWordsIf(name, doIt); }
|
|
|
|
static string ifNoProtect(const string& in) { return v3Global.opt.protectIds() ? "" : in; }
|
|
|
|
static string symClassName() { return v3Global.opt.prefix()+"_"+protect("_Syms"); }
|
2006-08-30 21:07:55 +00:00
|
|
|
static string symClassVar() { return symClassName()+"* __restrict vlSymsp"; }
|
2019-05-19 20:13:13 +00:00
|
|
|
static string symTopAssign() {
|
|
|
|
return v3Global.opt.prefix()+"* __restrict vlTOPp VL_ATTR_UNUSED = vlSymsp->TOPp;"; }
|
2020-02-03 02:15:07 +00:00
|
|
|
static string funcNameProtect(const AstCFunc* nodep, const AstNodeModule* modp) {
|
|
|
|
if (nodep->isConstructor()) return prefixNameProtect(modp);
|
|
|
|
else if (nodep->isDestructor()) return string("~") + prefixNameProtect(modp);
|
|
|
|
else return nodep->nameProtect();
|
|
|
|
}
|
2020-01-25 14:16:00 +00:00
|
|
|
static string prefixNameProtect(const AstNode* nodep) { // C++ name with prefix
|
|
|
|
const AstNodeModule* modp = VN_CAST_CONST(nodep, NodeModule);
|
|
|
|
if (modp && modp->isTop()) {
|
2019-05-19 20:13:13 +00:00
|
|
|
return v3Global.opt.prefix();
|
|
|
|
} else {
|
2020-01-25 14:16:00 +00:00
|
|
|
return v3Global.opt.modPrefix() + "_" + protect(nodep->name());
|
2019-05-19 20:13:13 +00:00
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
2019-05-19 20:13:13 +00:00
|
|
|
static string topClassName() { // Return name of top wrapper module
|
|
|
|
return v3Global.opt.prefix();
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
2018-10-24 23:40:07 +00:00
|
|
|
static AstCFile* newCFile(const string& filename, bool slow, bool source) {
|
2019-05-19 20:13:13 +00:00
|
|
|
AstCFile* cfilep = new AstCFile(v3Global.rootp()->fileline(), filename);
|
|
|
|
cfilep->slow(slow);
|
|
|
|
cfilep->source(source);
|
|
|
|
v3Global.rootp()->addFilesp(cfilep);
|
|
|
|
return cfilep;
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
2018-02-02 02:32:58 +00:00
|
|
|
string cFuncArgs(const AstCFunc* nodep) {
|
2019-05-19 20:13:13 +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)) {
|
2019-05-19 20:13:13 +00:00
|
|
|
if (portp->isIO() && !portp->isFuncReturn()) {
|
|
|
|
if (args != "") args+= ", ";
|
|
|
|
if (nodep->dpiImport() || nodep->dpiExportWrapper())
|
|
|
|
args += portp->dpiArgType(true, false);
|
|
|
|
else if (nodep->funcPublic())
|
|
|
|
args += portp->cPubArgType(true, false);
|
|
|
|
else args += portp->vlArgType(true, false, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return args;
|
2009-12-09 03:12:59 +00:00
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
// CONSTRUCTORS
|
|
|
|
EmitCBaseVisitor() {
|
2019-05-19 20:13:13 +00:00
|
|
|
m_ofp = NULL;
|
2019-09-27 07:44:23 +00:00
|
|
|
m_trackText = false;
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
|
|
|
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:
|
2019-05-19 20:13:13 +00:00
|
|
|
// MEMBERS
|
|
|
|
int m_count; // Number of statements
|
2006-08-26 11:35:28 +00:00
|
|
|
// VISITORS
|
2020-01-21 22:35:56 +00:00
|
|
|
virtual void visit(AstNode* nodep) VL_OVERRIDE {
|
2019-05-19 20:13:13 +00:00
|
|
|
m_count++;
|
2018-05-11 00:55:37 +00:00
|
|
|
iterateChildren(nodep);
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
|
|
|
public:
|
2019-09-12 11:22:22 +00:00
|
|
|
// CONSTRUCTORS
|
2015-10-04 02:33:06 +00:00
|
|
|
explicit EmitCBaseCounterVisitor(AstNode* nodep) {
|
2019-05-19 20:13:13 +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; }
|
|
|
|
};
|
|
|
|
|
2019-05-19 20:13:13 +00:00
|
|
|
#endif // guard
|