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; }
|
2021-06-16 11:18:56 +00:00
|
|
|
static string voidSelfAssign() {
|
|
|
|
return topClassName() + "* const __restrict vlSelf VL_ATTR_UNUSED = static_cast<"
|
|
|
|
+ topClassName() + "*>(voidSelf);\n";
|
|
|
|
}
|
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-24 15:58:30 +00:00
|
|
|
static string funcNameProtect(const AstCFunc* nodep, const AstNodeModule* modp = nullptr);
|
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
|
|
|
}
|
2021-06-13 14:05:55 +00:00
|
|
|
|
|
|
|
static bool isConstPoolMod(AstNode* modp) {
|
|
|
|
return modp == v3Global.rootp()->constPoolp()->modp();
|
|
|
|
}
|
|
|
|
|
2021-06-24 15:58:30 +00:00
|
|
|
static AstCFile* newCFile(const string& filename, bool slow, bool source);
|
|
|
|
string cFuncArgs(const AstCFunc* nodep);
|
|
|
|
void emitCFuncHeader(const AstCFunc* funcp, const AstNodeModule* modp, bool withScope);
|
|
|
|
void emitCFuncDecl(const AstCFunc* funcp, const AstNodeModule* modp, bool cLinkage = false);
|
2021-06-24 16:08:07 +00:00
|
|
|
void emitVarDecl(const AstVar* nodep, const string& prefixIfImp);
|
|
|
|
void emitModCUse(AstNodeModule* modp, VUseType useType);
|
2021-06-13 13:33:11 +00:00
|
|
|
|
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
|