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
|
|
|
|
|
//
|
2008-04-25 12:14:27 +00:00
|
|
|
|
// Code available from: http://www.veripool.org/verilator
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
//
|
2018-01-02 23:05:06 +00:00
|
|
|
|
// Copyright 2005-2018 by Wilson Snyder. This program is free software; you can
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// 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.
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//
|
|
|
|
|
// Verilator is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
|
|
|
|
|
#ifndef _V3HASHED_H_
|
|
|
|
|
#define _V3HASHED_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 "V3Error.h"
|
|
|
|
|
#include "V3Ast.h"
|
|
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
|
|
2012-04-28 16:33:51 +00:00
|
|
|
|
class VHashedBase {
|
|
|
|
|
public:
|
|
|
|
|
// CONSTRUCTORS
|
|
|
|
|
VHashedBase() {}
|
|
|
|
|
~VHashedBase() {}
|
|
|
|
|
|
|
|
|
|
// METHODS
|
2018-05-14 10:50:47 +00:00
|
|
|
|
VL_DEBUG_FUNC; // Declare debug()
|
2012-04-28 16:33:51 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
|
|
2013-02-19 23:49:36 +00:00
|
|
|
|
struct V3HashedUserCheck {
|
|
|
|
|
// Functor for V3Hashed::findDuplicate
|
|
|
|
|
virtual bool check(AstNode*,AstNode*) =0;
|
2013-05-01 02:55:03 +00:00
|
|
|
|
V3HashedUserCheck() {}
|
|
|
|
|
virtual ~V3HashedUserCheck() {}
|
2013-02-19 23:49:36 +00:00
|
|
|
|
};
|
|
|
|
|
|
2012-04-28 16:33:51 +00:00
|
|
|
|
class V3Hashed : public VHashedBase {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// NODE STATE
|
|
|
|
|
// AstNode::user4() -> V3Hash. Hash value of this node (hash of 0 is illegal)
|
2008-11-25 14:03:49 +00:00
|
|
|
|
AstUser4InUse m_inuser4;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
|
|
// TYPES
|
2018-02-02 02:24:41 +00:00
|
|
|
|
typedef std::multimap<V3Hash,AstNode*> HashMmap;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
public:
|
|
|
|
|
typedef HashMmap::iterator iterator;
|
|
|
|
|
private:
|
|
|
|
|
// MEMBERS
|
|
|
|
|
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(); }
|
2006-08-26 11:35:28 +00:00
|
|
|
|
~V3Hashed() {}
|
2012-04-02 01:04:28 +00:00
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// ACCESSORS
|
|
|
|
|
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
|
2012-04-02 01:04:28 +00:00
|
|
|
|
void clear() { m_hashMmap.clear(); AstNode::user4ClearTree(); }
|
2013-02-19 23:49:36 +00:00
|
|
|
|
iterator hashAndInsert(AstNode* nodep); // Hash the node, and insert into map. Return iterator to inserted
|
|
|
|
|
void hash(AstNode* nodep); // Only hash the node
|
2006-08-26 11:35:28 +00:00
|
|
|
|
bool sameNodes(AstNode* node1p, AstNode* node2p); // After hashing, and tell if identical
|
|
|
|
|
void erase(iterator it); // Remove node from structures
|
|
|
|
|
iterator findDuplicate(AstNode* nodep); // Return duplicate in hash, if any
|
2013-02-19 23:49:36 +00:00
|
|
|
|
iterator findDuplicate(AstNode* nodep, V3HashedUserCheck* checkp); // Extra user checks for sameness
|
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);
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif // Guard
|