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
|
|
|
//
|
|
|
|
//*************************************************************************
|
|
|
|
//
|
2021-01-01 15:29:54 +00:00
|
|
|
// Copyright 2003-2021 by Wilson Snyder. This program is free software; you
|
2020-03-21 15:24:24 +00:00
|
|
|
// 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
|
|
|
|
2021-03-04 02:57:07 +00:00
|
|
|
#ifndef VERILATOR_V3EMITCBASE_H_
|
|
|
|
#define VERILATOR_V3EMITCBASE_H_
|
2006-08-26 11:35:28 +00:00
|
|
|
|
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
|
|
|
|
|
2020-11-19 02:32:16 +00:00
|
|
|
class EmitCBaseVisitor VL_NOT_FINAL : public AstNVisitor {
|
2006-08-26 11:35:28 +00:00
|
|
|
public:
|
|
|
|
// STATE
|
2020-08-15 17:11:27 +00:00
|
|
|
V3OutCFile* m_ofp = nullptr;
|
|
|
|
bool m_trackText = false; // 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); }
|
2020-04-15 11:58:34 +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); }
|
2021-06-13 13:33:11 +00:00
|
|
|
void ensureNewLine() { ofp()->ensureNewLine(); }
|
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) {
|
2020-04-15 11:58:34 +00:00
|
|
|
return VIdProtect::protectIf(name, doIt);
|
|
|
|
}
|
2019-10-06 17:24:21 +00:00
|
|
|
static string protectWordsIf(const string& name, bool doIt) {
|
2020-04-15 11:58:34 +00:00
|
|
|
return VIdProtect::protectWordsIf(name, doIt);
|
|
|
|
}
|
2019-10-06 17:24:21 +00:00
|
|
|
static string ifNoProtect(const string& in) { return v3Global.opt.protectIds() ? "" : in; }
|
2020-04-15 11:58:34 +00:00
|
|
|
static string symClassName() { return v3Global.opt.prefix() + "_" + protect("_Syms"); }
|
|
|
|
static string symClassVar() { return symClassName() + "* __restrict vlSymsp"; }
|
2021-06-13 13:33:11 +00:00
|
|
|
static string symClassAssign() {
|
|
|
|
return symClassName() + "* const __restrict vlSymsp VL_ATTR_UNUSED = vlSelf->vlSymsp;\n";
|
2020-04-15 11:58:34 +00:00
|
|
|
}
|
2021-06-13 13:33:11 +00:00
|
|
|
static string funcNameProtect(const AstCFunc* nodep) {
|
|
|
|
AstNodeModule* modp = VN_CAST(nodep->user4p(), NodeModule);
|
|
|
|
string name;
|
2020-04-15 11:58:34 +00:00
|
|
|
if (nodep->isConstructor()) {
|
2021-06-13 13:33:11 +00:00
|
|
|
name += prefixNameProtect(modp);
|
2020-04-15 11:58:34 +00:00
|
|
|
} else if (nodep->isDestructor()) {
|
2021-06-13 13:33:11 +00:00
|
|
|
name += "~";
|
|
|
|
name += prefixNameProtect(modp);
|
2020-04-15 11:58:34 +00:00
|
|
|
} else {
|
2021-06-13 13:33:11 +00:00
|
|
|
if (nodep->isLoose()) {
|
|
|
|
name += prefixNameProtect(modp);
|
|
|
|
name += "__";
|
|
|
|
}
|
|
|
|
name += nodep->nameProtect();
|
2020-04-15 11:58:34 +00:00
|
|
|
}
|
2021-06-13 13:33:11 +00:00
|
|
|
return name;
|
2020-02-03 02:15:07 +00:00
|
|
|
}
|
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()) {
|
2021-06-13 13:33:11 +00:00
|
|
|
return topClassName();
|
2019-05-19 20:13:13 +00:00
|
|
|
} 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
|
2021-06-13 13:33:11 +00:00
|
|
|
string args;
|
|
|
|
if (nodep->isLoose() && nodep->isStatic().falseUnknown()) {
|
|
|
|
if (nodep->isConst().trueKnown()) args += "const ";
|
|
|
|
AstNodeModule* modp = VN_CAST(nodep->user4p(), NodeModule);
|
|
|
|
args += prefixNameProtect(modp);
|
|
|
|
args += "* vlSelf";
|
|
|
|
}
|
|
|
|
if (!nodep->argTypes().empty()) {
|
|
|
|
if (!args.empty()) args += ", ";
|
|
|
|
args += nodep->argTypes();
|
|
|
|
}
|
2019-05-19 20:13:13 +00:00
|
|
|
// Might be a user function with argument list.
|
2020-04-15 11:58:34 +00:00
|
|
|
for (const AstNode* stmtp = nodep->argsp(); stmtp; stmtp = stmtp->nextp()) {
|
2018-02-02 02:32:58 +00:00
|
|
|
if (const AstVar* portp = VN_CAST_CONST(stmtp, Var)) {
|
2019-05-19 20:13:13 +00:00
|
|
|
if (portp->isIO() && !portp->isFuncReturn()) {
|
2020-04-15 11:58:34 +00:00
|
|
|
if (args != "") args += ", ";
|
|
|
|
if (nodep->dpiImport() || nodep->dpiExportWrapper()) {
|
2019-05-19 20:13:13 +00:00
|
|
|
args += portp->dpiArgType(true, false);
|
2020-04-15 11:58:34 +00:00
|
|
|
} else if (nodep->funcPublic()) {
|
2019-05-19 20:13:13 +00:00
|
|
|
args += portp->cPubArgType(true, false);
|
2020-04-15 11:58:34 +00:00
|
|
|
} else {
|
|
|
|
args += portp->vlArgType(true, false, true);
|
|
|
|
}
|
2019-05-19 20:13:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return args;
|
2009-12-09 03:12:59 +00:00
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
2021-06-13 13:33:11 +00:00
|
|
|
void emitCFuncHeader(const AstCFunc* funcp, const AstNodeModule* modp, bool withScope) {
|
|
|
|
if (!funcp->isConstructor() && !funcp->isDestructor()) {
|
|
|
|
puts(funcp->rtnTypeVoid());
|
|
|
|
puts(" ");
|
|
|
|
}
|
|
|
|
if (withScope && funcp->isProperMethod()) puts(prefixNameProtect(modp) + "::");
|
|
|
|
puts(funcNameProtect(funcp));
|
|
|
|
puts("(" + cFuncArgs(funcp) + ")");
|
|
|
|
if (funcp->isConst().trueKnown() && funcp->isProperMethod()) puts(" const");
|
|
|
|
}
|
|
|
|
|
|
|
|
void emitCFuncDecl(const AstCFunc* funcp, const AstNodeModule* modp) {
|
|
|
|
ensureNewLine();
|
|
|
|
if (!funcp->ifdef().empty()) puts("#ifdef " + funcp->ifdef() + "\n");
|
|
|
|
if (funcp->isStatic().trueUnknown() && funcp->isProperMethod()) puts("static ");
|
|
|
|
if (funcp->isVirtual()) {
|
|
|
|
UASSERT_OBJ(funcp->isProperMethod(), funcp, "Virtual function is not a proper method");
|
|
|
|
puts("virtual ");
|
|
|
|
}
|
|
|
|
emitCFuncHeader(funcp, modp, /* withScope: */ false);
|
|
|
|
if (funcp->slow()) puts(" VL_ATTR_COLD");
|
|
|
|
puts(";\n");
|
|
|
|
if (!funcp->ifdef().empty()) puts("#endif // " + funcp->ifdef() + "\n");
|
|
|
|
}
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
// CONSTRUCTORS
|
2020-11-17 00:56:16 +00:00
|
|
|
EmitCBaseVisitor() = default;
|
|
|
|
virtual ~EmitCBaseVisitor() override = default;
|
2006-08-26 11:35:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//######################################################################
|
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
|
|
|
|
2020-11-19 02:32:16 +00:00
|
|
|
class EmitCBaseCounterVisitor final : public AstNVisitor {
|
2006-08-26 11:35:28 +00:00
|
|
|
private:
|
2019-05-19 20:13:13 +00:00
|
|
|
// MEMBERS
|
2020-08-16 13:55:36 +00:00
|
|
|
int m_count = 0; // Number of statements
|
2006-08-26 11:35:28 +00:00
|
|
|
// VISITORS
|
2020-08-15 14:03:34 +00:00
|
|
|
virtual void visit(AstNode* nodep) 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
|
|
|
}
|
2020-04-15 11:58:34 +00:00
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
public:
|
2019-09-12 11:22:22 +00:00
|
|
|
// CONSTRUCTORS
|
2020-08-16 13:55:36 +00:00
|
|
|
explicit EmitCBaseCounterVisitor(AstNode* nodep) { iterate(nodep); }
|
2020-11-17 00:56:16 +00:00
|
|
|
virtual ~EmitCBaseCounterVisitor() override = default;
|
2006-08-26 11:35:28 +00:00
|
|
|
int count() const { return m_count; }
|
|
|
|
};
|
|
|
|
|
2019-05-19 20:13:13 +00:00
|
|
|
#endif // guard
|