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: Hash AST trees to find duplicates
|
|
|
|
//
|
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 2005-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 _V3HASHED_H_
|
|
|
|
#define _V3HASHED_H_ 1
|
2006-12-18 19:20:45 +00:00
|
|
|
#include "config_build.h"
|
|
|
|
#include "verilatedos.h"
|
2018-10-14 17:43:24 +00:00
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
#include "V3Error.h"
|
|
|
|
#include "V3Ast.h"
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
|
2020-11-19 02:32:16 +00:00
|
|
|
class VHashedBase VL_NOT_FINAL {
|
2012-04-28 16:33:51 +00:00
|
|
|
public:
|
|
|
|
// CONSTRUCTORS
|
2020-11-17 00:56:16 +00:00
|
|
|
VHashedBase() = default;
|
|
|
|
~VHashedBase() = default;
|
2012-04-28 16:33:51 +00:00
|
|
|
|
|
|
|
// METHODS
|
2018-05-14 10:50:47 +00:00
|
|
|
VL_DEBUG_FUNC; // Declare debug()
|
2012-04-28 16:33:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
|
2019-08-03 23:05:59 +00:00
|
|
|
struct V3HashedUserSame {
|
2013-02-19 23:49:36 +00:00
|
|
|
// Functor for V3Hashed::findDuplicate
|
2019-08-03 23:05:59 +00:00
|
|
|
virtual bool isSame(AstNode*, AstNode*) = 0;
|
2020-11-17 00:56:16 +00:00
|
|
|
V3HashedUserSame() = default;
|
|
|
|
virtual ~V3HashedUserSame() = default;
|
2013-02-19 23:49:36 +00:00
|
|
|
};
|
|
|
|
|
2020-11-19 02:32:16 +00:00
|
|
|
class V3Hashed final : public VHashedBase {
|
2006-08-26 11:35:28 +00:00
|
|
|
// NODE STATE
|
2019-05-19 20:13:13 +00:00
|
|
|
// AstNode::user4() -> V3Hash. Hash value of this node (hash of 0 is illegal)
|
|
|
|
AstUser4InUse m_inuser4;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
// TYPES
|
|
|
|
public:
|
2020-04-14 02:51:35 +00:00
|
|
|
typedef std::multimap<V3Hash, AstNode*> HashMmap;
|
2006-08-26 11:35:28 +00:00
|
|
|
typedef HashMmap::iterator iterator;
|
2020-04-14 02:51:35 +00:00
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
private:
|
|
|
|
// MEMBERS
|
2019-05-19 20:13:13 +00:00
|
|
|
HashMmap m_hashMmap; // hashvalue -> nodes with that hash
|
2009-01-21 21:56:50 +00:00
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
public:
|
|
|
|
// CONSTRUCTORS
|
2012-04-02 01:04:28 +00:00
|
|
|
V3Hashed() { clear(); }
|
2020-11-17 00:56:16 +00:00
|
|
|
~V3Hashed() = default;
|
2012-04-02 01:04:28 +00:00
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
// ACCESSORS
|
2019-05-19 20:13:13 +00:00
|
|
|
HashMmap& mmap() { return m_hashMmap; } // Return map for iteration
|
2012-04-02 01:04:28 +00:00
|
|
|
iterator begin() { return m_hashMmap.begin(); }
|
|
|
|
iterator end() { return m_hashMmap.end(); }
|
2009-01-21 21:56:50 +00:00
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
// METHODS
|
2020-04-14 02:51:35 +00:00
|
|
|
void clear() {
|
|
|
|
m_hashMmap.clear();
|
|
|
|
AstNode::user4ClearTree();
|
|
|
|
}
|
2019-08-04 01:49:39 +00:00
|
|
|
void check(); // Check assertions on structure
|
2020-04-14 02:51:35 +00:00
|
|
|
// Hash the node, and insert into map. Return iterator to inserted
|
|
|
|
iterator hashAndInsert(AstNode* nodep);
|
2020-11-11 02:40:14 +00:00
|
|
|
static void hash(AstNode* nodep); // Only hash the node
|
|
|
|
// After hashing, and tell if identical
|
|
|
|
static bool sameNodes(AstNode* node1p, AstNode* node2p);
|
2019-05-19 20:13:13 +00:00
|
|
|
void erase(iterator it); // Remove node from structures
|
2019-08-03 23:05:59 +00:00
|
|
|
// Return duplicate in hash, if any, with optional user check for sameness
|
2020-08-15 14:12:55 +00:00
|
|
|
iterator findDuplicate(AstNode* nodep, V3HashedUserSame* checkp = nullptr);
|
2006-08-26 11:35:28 +00:00
|
|
|
AstNode* iteratorNodep(iterator it) { return it->second; }
|
2008-11-17 01:39:49 +00:00
|
|
|
void dumpFile(const string& filename, bool tree);
|
2020-04-14 02:51:35 +00:00
|
|
|
void dumpFilePrefixed(const string& nameComment, bool tree = false);
|
2012-04-02 01:04:28 +00:00
|
|
|
static V3Hash nodeHash(AstNode* nodep) { return V3Hash(nodep->user4p()); }
|
2018-06-12 02:05:45 +00:00
|
|
|
// Hash of the nodep tree, without caching in user4.
|
|
|
|
static V3Hash uncachedHash(const AstNode* nodep);
|
2006-08-26 11:35:28 +00:00
|
|
|
};
|
|
|
|
|
2019-05-19 20:13:13 +00:00
|
|
|
#endif // Guard
|