2012-04-13 03:08:20 +02:00
|
|
|
// -*- mode: C++; c-file-style: "cc-mode" -*-
|
2006-08-26 13:35:28 +02:00
|
|
|
//*************************************************************************
|
|
|
|
|
// DESCRIPTION: Verilator: Emit C++ for tree
|
|
|
|
|
//
|
2019-11-08 04:33:59 +01:00
|
|
|
// Code available from: https://verilator.org
|
2006-08-26 13:35:28 +02:00
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
//
|
2020-03-21 16:24:24 +01: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 23:07:57 +02:00
|
|
|
// Lesser General Public License Version 3 or the Perl Artistic License
|
|
|
|
|
// Version 2.0.
|
2020-03-21 16:24:24 +01:00
|
|
|
// SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
|
2006-08-26 13:35:28 +02:00
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
2019-10-05 02:17:11 +02:00
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
#ifndef _V3EMITCBASE_H_
|
|
|
|
|
#define _V3EMITCBASE_H_ 1
|
|
|
|
|
|
2006-12-18 20:20:45 +01:00
|
|
|
#include "config_build.h"
|
|
|
|
|
#include "verilatedos.h"
|
2006-08-26 13:35:28 +02:00
|
|
|
|
|
|
|
|
#include "V3Global.h"
|
|
|
|
|
#include "V3File.h"
|
|
|
|
|
#include "V3Ast.h"
|
|
|
|
|
|
2018-10-14 19:43:24 +02:00
|
|
|
#include <cstdarg>
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
//######################################################################
|
|
|
|
|
// Base Visitor class -- holds output file pointer
|
|
|
|
|
|
|
|
|
|
class EmitCBaseVisitor : public AstNVisitor {
|
|
|
|
|
public:
|
|
|
|
|
// STATE
|
2020-08-15 19:11:27 +02:00
|
|
|
V3OutCFile* m_ofp = nullptr;
|
|
|
|
|
bool m_trackText = false; // Always track AstText nodes
|
2006-08-26 13:35:28 +02:00
|
|
|
// METHODS
|
2019-05-19 22:13:13 +02:00
|
|
|
V3OutCFile* ofp() const { return m_ofp; }
|
2006-08-26 13:35:28 +02:00
|
|
|
void puts(const string& str) { ofp()->puts(str); }
|
|
|
|
|
void putbs(const string& str) { ofp()->putbs(str); }
|
2020-04-15 13:58:34 +02:00
|
|
|
void putsDecoration(const string& str) {
|
|
|
|
|
if (v3Global.opt.decoration()) puts(str);
|
|
|
|
|
}
|
2009-05-08 19:16:19 +02:00
|
|
|
void putsQuoted(const string& str) { ofp()->putsQuoted(str); }
|
2006-08-26 13:35:28 +02:00
|
|
|
bool optSystemC() { return v3Global.opt.systemC(); }
|
2019-10-06 19:24:21 +02:00
|
|
|
static string protect(const string& name) { return VIdProtect::protectIf(name, true); }
|
|
|
|
|
static string protectIf(const string& name, bool doIt) {
|
2020-04-15 13:58:34 +02:00
|
|
|
return VIdProtect::protectIf(name, doIt);
|
|
|
|
|
}
|
2019-10-06 19:24:21 +02:00
|
|
|
static string protectWordsIf(const string& name, bool doIt) {
|
2020-04-15 13:58:34 +02:00
|
|
|
return VIdProtect::protectWordsIf(name, doIt);
|
|
|
|
|
}
|
2019-10-06 19:24:21 +02:00
|
|
|
static string ifNoProtect(const string& in) { return v3Global.opt.protectIds() ? "" : in; }
|
2020-04-15 13:58:34 +02:00
|
|
|
static string symClassName() { return v3Global.opt.prefix() + "_" + protect("_Syms"); }
|
|
|
|
|
static string symClassVar() { return symClassName() + "* __restrict vlSymsp"; }
|
2019-05-19 22:13:13 +02:00
|
|
|
static string symTopAssign() {
|
2020-05-09 00:42:34 +02:00
|
|
|
return v3Global.opt.prefix() + "* const __restrict vlTOPp VL_ATTR_UNUSED = vlSymsp->TOPp;";
|
2020-04-15 13:58:34 +02:00
|
|
|
}
|
2020-02-03 03:15:07 +01:00
|
|
|
static string funcNameProtect(const AstCFunc* nodep, const AstNodeModule* modp) {
|
2020-04-15 13:58:34 +02:00
|
|
|
if (nodep->isConstructor()) {
|
|
|
|
|
return prefixNameProtect(modp);
|
|
|
|
|
} else if (nodep->isDestructor()) {
|
|
|
|
|
return string("~") + prefixNameProtect(modp);
|
|
|
|
|
} else {
|
|
|
|
|
return nodep->nameProtect();
|
|
|
|
|
}
|
2020-02-03 03:15:07 +01:00
|
|
|
}
|
2020-01-25 15:16:00 +01: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 22:13:13 +02:00
|
|
|
return v3Global.opt.prefix();
|
|
|
|
|
} else {
|
2020-01-25 15:16:00 +01:00
|
|
|
return v3Global.opt.modPrefix() + "_" + protect(nodep->name());
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2019-05-19 22:13:13 +02:00
|
|
|
static string topClassName() { // Return name of top wrapper module
|
|
|
|
|
return v3Global.opt.prefix();
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2018-10-25 01:40:07 +02:00
|
|
|
static AstCFile* newCFile(const string& filename, bool slow, bool source) {
|
2019-05-19 22:13:13 +02:00
|
|
|
AstCFile* cfilep = new AstCFile(v3Global.rootp()->fileline(), filename);
|
|
|
|
|
cfilep->slow(slow);
|
|
|
|
|
cfilep->source(source);
|
|
|
|
|
v3Global.rootp()->addFilesp(cfilep);
|
|
|
|
|
return cfilep;
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2018-02-02 03:32:58 +01:00
|
|
|
string cFuncArgs(const AstCFunc* nodep) {
|
2019-05-19 22:13:13 +02:00
|
|
|
// Return argument list for given C function
|
|
|
|
|
string args = nodep->argTypes();
|
|
|
|
|
// Might be a user function with argument list.
|
2020-04-15 13:58:34 +02:00
|
|
|
for (const AstNode* stmtp = nodep->argsp(); stmtp; stmtp = stmtp->nextp()) {
|
2018-02-02 03:32:58 +01:00
|
|
|
if (const AstVar* portp = VN_CAST_CONST(stmtp, Var)) {
|
2019-05-19 22:13:13 +02:00
|
|
|
if (portp->isIO() && !portp->isFuncReturn()) {
|
2020-04-15 13:58:34 +02:00
|
|
|
if (args != "") args += ", ";
|
|
|
|
|
if (nodep->dpiImport() || nodep->dpiExportWrapper()) {
|
2019-05-19 22:13:13 +02:00
|
|
|
args += portp->dpiArgType(true, false);
|
2020-04-15 13:58:34 +02:00
|
|
|
} else if (nodep->funcPublic()) {
|
2019-05-19 22:13:13 +02:00
|
|
|
args += portp->cPubArgType(true, false);
|
2020-04-15 13:58:34 +02:00
|
|
|
} else {
|
|
|
|
|
args += portp->vlArgType(true, false, true);
|
|
|
|
|
}
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return args;
|
2009-12-09 04:12:59 +01:00
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
|
|
|
|
|
// CONSTRUCTORS
|
2020-11-17 01:56:16 +01:00
|
|
|
EmitCBaseVisitor() = default;
|
|
|
|
|
virtual ~EmitCBaseVisitor() override = default;
|
2006-08-26 13:35:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
2008-11-17 23:13:57 +01:00
|
|
|
// Count operations under the given node, as a visitor of each AstNode
|
2006-08-26 13:35:28 +02:00
|
|
|
|
|
|
|
|
class EmitCBaseCounterVisitor : public AstNVisitor {
|
|
|
|
|
private:
|
2019-05-19 22:13:13 +02:00
|
|
|
// MEMBERS
|
2020-08-16 15:55:36 +02:00
|
|
|
int m_count = 0; // Number of statements
|
2006-08-26 13:35:28 +02:00
|
|
|
// VISITORS
|
2020-08-15 16:03:34 +02:00
|
|
|
virtual void visit(AstNode* nodep) override {
|
2019-05-19 22:13:13 +02:00
|
|
|
m_count++;
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateChildren(nodep);
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2020-04-15 13:58:34 +02:00
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
public:
|
2019-09-12 13:22:22 +02:00
|
|
|
// CONSTRUCTORS
|
2020-08-16 15:55:36 +02:00
|
|
|
explicit EmitCBaseCounterVisitor(AstNode* nodep) { iterate(nodep); }
|
2020-11-17 01:56:16 +01:00
|
|
|
virtual ~EmitCBaseCounterVisitor() override = default;
|
2006-08-26 13:35:28 +02:00
|
|
|
int count() const { return m_count; }
|
|
|
|
|
};
|
|
|
|
|
|
2019-05-19 22:13:13 +02:00
|
|
|
#endif // guard
|