[llvm-commits] [hlvm] r37982 - in /hlvm/trunk/hlvm/AST: Node.cpp Node.h SymbolTable.cpp SymbolTable.h Type.h
Reid Spencer
reid at x10sys.com
Sat Jul 7 16:58:37 PDT 2007
Author: reid
Date: Sat Jul 7 18:58:37 2007
New Revision: 37982
URL: http://llvm.org/viewvc/llvm-project?rev=37982&view=rev
Log:
Implement the symbol table and add some new Type subclasses.
Added:
hlvm/trunk/hlvm/AST/Node.cpp
hlvm/trunk/hlvm/AST/SymbolTable.cpp
hlvm/trunk/hlvm/AST/SymbolTable.h
Modified:
hlvm/trunk/hlvm/AST/Node.h
hlvm/trunk/hlvm/AST/Type.h
Added: hlvm/trunk/hlvm/AST/Node.cpp
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/AST/Node.cpp?rev=37982&view=auto
==============================================================================
--- hlvm/trunk/hlvm/AST/Node.cpp (added)
+++ hlvm/trunk/hlvm/AST/Node.cpp Sat Jul 7 18:58:37 2007
@@ -0,0 +1,36 @@
+//
+// Copyright (C) 2006 HLVM Group. All Rights Reserved.
+//
+// This program is open source software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License (GPL) as published by
+// the Free Software Foundation; either version 2 of the License, or (at your
+// option) any later version. You should have received a copy of the GPL in a
+// file named COPYING that was included with this program; if not, you can
+// obtain a copy of the license through the Internet at http://www.fsf.org/
+//
+// This program 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.
+//
+////////////////////////////////////////////////////////////////////////////////
+/// @file hlvm/AST/Node.cpp
+/// @author Reid Spencer <reid at hlvm.org> (original author)
+/// @date 2006/05/04
+/// @since 0.1.0
+/// @brief Implements the functions of class hlvm::AST::Node.
+////////////////////////////////////////////////////////////////////////////////
+
+#include <hlvm/AST/Node.h>
+#include <hlvm/AST/Bundle.h>
+#include <hlvm/AST/Function.h>
+#include <hlvm/AST/Variable.h>
+#include <hlvm/AST/Type.h>
+
+namespace hlvm
+{
+namespace AST
+{
+
+}
+}
Modified: hlvm/trunk/hlvm/AST/Node.h
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/AST/Node.h?rev=37982&r1=37981&r2=37982&view=diff
==============================================================================
--- hlvm/trunk/hlvm/AST/Node.h (original)
+++ hlvm/trunk/hlvm/AST/Node.h Sat Jul 7 18:58:37 2007
@@ -53,6 +53,9 @@
Node(Node* parent, const std::string& name)
: name_(name), parent_(parent), kids_() {}
virtual ~Node();
+#ifndef _NDEBUG
+ virtual void dump() const;
+#endif
/// @}
/// @name Data
Added: hlvm/trunk/hlvm/AST/SymbolTable.cpp
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/AST/SymbolTable.cpp?rev=37982&view=auto
==============================================================================
--- hlvm/trunk/hlvm/AST/SymbolTable.cpp (added)
+++ hlvm/trunk/hlvm/AST/SymbolTable.cpp Sat Jul 7 18:58:37 2007
@@ -0,0 +1,114 @@
+//
+// Copyright (C) 2006 HLVM Group. All Rights Reserved.
+//
+// This program is open source software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License (GPL) as published by
+// the Free Software Foundation; either version 2 of the License, or (at your
+// option) any later version. You should have received a copy of the GPL in a
+// file named COPYING that was included with this program; if not, you can
+// obtain a copy of the license through the Internet at http://www.fsf.org/
+//
+// This program 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.
+//
+////////////////////////////////////////////////////////////////////////////////
+/// @file hlvm/AST/SymbolTable.cpp
+/// @author Reid Spencer <reid at hlvm.org> (original author)
+/// @date 2006/05/04
+/// @since 0.1.0
+/// @brief Implements the functions of class hlvm::AST::SymbolTable.
+////////////////////////////////////////////////////////////////////////////////
+
+#include "hlvm/AST/SymbolTable.h"
+#include "llvm/ADT/StringExtras.h"
+#include <iostream>
+#include <algorithm>
+#include <cassert>
+
+using namespace hlvm::AST;
+
+std::string
+SymbolTable::getUniqueName(const std::string &base_name) const {
+ std::string try_name = base_name;
+ const_iterator end = map_.end();
+
+ // See if the name exists. Loop until we find a free name in the symbol table
+ // by incrementing the last_unique_ counter.
+ while (map_.find(try_name) != end)
+ try_name = base_name +
+ llvm::utostr(++last_unique_);
+ return try_name;
+}
+
+// lookup a node by name - returns null on failure
+Node* SymbolTable::lookup(const std::string& name) const {
+ const_iterator TI = map_.find(name);
+ if (TI != map_.end())
+ return const_cast<Node*>(TI->second);
+ return 0;
+}
+
+// Erase a specific type from the symbol table
+bool SymbolTable::erase(Node *N) {
+ for (iterator TI = map_.begin(), TE = map_.end(); TI != TE; ++TI) {
+ if (TI->second == N) {
+ this->erase(TI);
+ return true;
+ }
+ }
+ return false;
+}
+
+// remove - Remove a node from the symbol table...
+Node* SymbolTable::erase(iterator Entry) {
+ assert(Entry != map_.end() && "Invalid entry to remove!");
+ const Node* Result = Entry->second;
+ map_.erase(Entry);
+ return const_cast<Node*>(Result);
+}
+
+// insert - Insert a node into the symbol table with the specified name...
+void SymbolTable::insert(const std::string& Name, const Node* N) {
+ assert(N && "Can't insert null node into symbol table!");
+
+ // Check to see if there is a naming conflict. If so, rename this type!
+ std::string unique_name = Name;
+ if (lookup(Name))
+ unique_name = getUniqueName(Name);
+
+ // Insert the map entry
+ map_.insert(make_pair(unique_name, N));
+}
+
+/// rename - Given a value with a non-empty name, remove its existing entry
+/// from the symbol table and insert a new one for Name. This is equivalent to
+/// doing "remove(V), V->Name = Name, insert(V)", but is faster, and will not
+/// temporarily remove the symbol table plane if V is the last value in the
+/// symtab with that name (which could invalidate iterators to that plane).
+bool SymbolTable::rename(Node *N, const std::string &name) {
+ for (iterator NI = map_.begin(), NE = map_.end(); NI != NE; ++NI) {
+ if (NI->second == N) {
+ // Remove the old entry.
+ map_.erase(NI);
+ // Add the new entry.
+ this->insert(name,N);
+ return true;
+ }
+ }
+ return false;
+}
+
+#ifndef _NDEBUG
+static void DumpNodes(const std::pair<const std::string, const Node*>& I ) {
+ std::cerr << " '" << I.first << "' = ";
+ I.second->dump();
+ std::cerr << "\n";
+}
+
+void SymbolTable::dump() const {
+ std::cerr << "SymbolTable: ";
+ for_each(map_.begin(), map_.end(), DumpNodes);
+}
+#endif
Added: hlvm/trunk/hlvm/AST/SymbolTable.h
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/AST/SymbolTable.h?rev=37982&view=auto
==============================================================================
--- hlvm/trunk/hlvm/AST/SymbolTable.h (added)
+++ hlvm/trunk/hlvm/AST/SymbolTable.h Sat Jul 7 18:58:37 2007
@@ -0,0 +1,136 @@
+//
+// Copyright (C) 2006 HLVM Group. All Rights Reserved.
+//
+// This program is open source software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License (GPL) as published by
+// the Free Software Foundation; either version 2 of the License, or (at your
+// option) any later version. You should have received a copy of the GPL in a
+// file named COPYING that was included with this program; if not, you can
+// obtain a copy of the license through the Internet at http://www.fsf.org/
+//
+// This program 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.
+//
+////////////////////////////////////////////////////////////////////////////////
+/// @file hlvm/AST/SymbolTable.h
+/// @author Reid Spencer <reid at hlvm.org> (original author)
+/// @date 2006/05/04
+/// @since 0.1.0
+/// @brief Declares the class hlvm::AST::SymbolTable
+////////////////////////////////////////////////////////////////////////////////
+
+#ifndef HLVM_AST_SYMBOL_TABLE_H
+#define HLVM_AST_SYMBOL_TABLE_H
+
+#include <hlvm/AST/Node.h>
+#include <map>
+
+namespace hlvm {
+namespace AST {
+
+ /// This class provides a symbol table of name/node pairs with operations to
+ /// support constructing, searching and iterating over the symbol table.
+ class SymbolTable
+ {
+ /// @name Types
+ /// @{
+ public:
+ /// @brief A mapping of names to nodes.
+ typedef std::map<const std::string, const Node*> NodeMap;
+
+ /// @brief An iterator over the NodeMap.
+ typedef NodeMap::iterator iterator;
+
+ /// @brief A const_iterator over the NodeMap.
+ typedef NodeMap::const_iterator const_iterator;
+
+ /// @}
+ /// @name Constructors
+ /// @{
+ public:
+ SymbolTable() {}
+ ~SymbolTable() {}
+
+ /// @}
+ /// @name Accessors
+ /// @{
+ public:
+ /// Generates a unique name for a node based on the \p BaseName by
+ /// incrementing an integer and appending it to the name, if necessary
+ /// @returns the unique name
+ /// @brief Get a unique name for a node
+ std::string getUniqueName(const std::string &BaseName) const;
+
+ /// This method finds the node with the given \p name in the node map
+ /// and returns it.
+ /// @returns null if the name is not found, otherwise the Node
+ /// associated with the \p name.
+ /// @brief Lookup a node by name.
+ Node* lookup(const std::string& name) const;
+
+ /// @returns true iff the symbol table is empty.
+ /// @brief Determine if the symbol table is empty
+ inline bool empty() const { return map_.empty(); }
+
+ /// @returns the size of the symbol table
+ /// @brief The number of name/node pairs is returned.
+ inline unsigned size() const { return unsigned(map_.size()); }
+
+ /// This function can be used from the debugger to display the
+ /// content of the symbol table while debugging.
+ /// @brief Print out symbol table on stderr
+ void dump() const;
+
+ /// @}
+ /// @name Iteration
+ /// @{
+ public:
+ /// Get an iterator to the start of the symbol table
+ inline iterator begin() { return map_.begin(); }
+
+ /// @brief Get a const_iterator to the start of the symbol table
+ inline const_iterator begin() const { return map_.begin(); }
+
+ /// Get an iterator to the end of the symbol talbe.
+ inline iterator end() { return map_.end(); }
+
+ /// Get a const_iterator to the end of the symbol table.
+ inline const_iterator end() const { return map_.end(); }
+
+ /// @}
+ /// @name Mutators
+ /// @{
+ public:
+ /// Inserts a node into the symbol table with the specified name. There can
+ /// be a many-to-one mapping between names and nodes. This method allows a
+ /// node with an existing entry in the symbol table to get a new name.
+ /// @brief Insert a node under a new name.
+ void insert(const std::string &Name, const Node *N);
+
+ /// Remove a node at the specified position in the symbol table.
+ /// @returns the removed Node.
+ /// @returns the Node that was erased from the symbol table.
+ Node* erase(iterator TI);
+
+ /// Remove a specific Node from the symbol table. This isn't fast, linear
+ /// search, O(n), algorithm.
+ /// @returns true if the erase was successful (TI was found)
+ bool erase(Node* TI);
+
+ /// Rename a node. This ain't fast, we have to linearly search for it first.
+ /// @returns true if the rename was successful (node was found)
+ bool rename(Node* T, const std::string& new_name);
+
+ /// @}
+ /// @name Internal Data
+ /// @{
+ private:
+ NodeMap map_; ///< This is the mapping of names to types.
+ mutable unsigned long last_unique_; ///< Counter for tracking unique names
+ /// @}
+ };
+} // End AST namespace
+} // End hlvm namespace
+#endif
Modified: hlvm/trunk/hlvm/AST/Type.h
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/AST/Type.h?rev=37982&r1=37981&r2=37982&view=diff
==============================================================================
--- hlvm/trunk/hlvm/AST/Type.h (original)
+++ hlvm/trunk/hlvm/AST/Type.h Sat Jul 7 18:58:37 2007
@@ -26,10 +26,8 @@
#include <hlvm/AST/Node.h>
-namespace hlvm
-{
-namespace AST
-{
+namespace hlvm {
+namespace AST {
/// This class represents a Type in the HLVM Abstract Syntax Tree.
/// A Type defines the format of storage.
/// @brief HLVM AST Type Node
@@ -51,6 +49,103 @@
Type* type_; ///< The type of the variable
/// @}
};
+
+ /// This class represents all HLVM integer types. An integer type declares the
+ /// the minimum number of bits that are required to store the integer type.
+ /// HLVM will convert this specification to the most appropriate sized
+ /// machine type for computation. If the number of bits is specified as zero
+ /// it implies infinite precision integer arithmetic.
+ class IntegerType : public Type
+ {
+ /// @name Constructors
+ /// @{
+ public:
+ IntegerType(
+ Node* parent, ///< The bundle in which the function is defined
+ const std::string& name ///< The name of the function
+ ) : Type(parent,name) {}
+ virtual ~IntegerType();
+
+ /// @}
+ /// @name Data
+ /// @{
+ protected:
+ uint32_t numBits; ///< Minimum number of bits
+ /// @}
+ };
+
+ /// A RangeType is an IntegerType that allows the range of values to be
+ /// constricted. The use of RangeType implies range checking whenever the
+ /// value of a RangeType variable is assigned.
+ class RangeType: public IntegerType
+ {
+ /// @name Constructors
+ /// @{
+ public:
+ RangeType(
+ Node* parent, ///< The bundle in which the function is defined
+ const std::string& name ///< The name of the function
+ ) : IntegerType(parent,name) {}
+ virtual ~RangeType();
+
+ /// @}
+ /// @name Data
+ /// @{
+ protected:
+ uint64_t min_value_; ///< Lowest value accepted
+ uint64_t max_value_; ///< Highest value accepted
+ /// @}
+ };
+
+ /// This class represents all HLVM real number types. The precision and
+ /// mantissa are specified as a number of decimal digits to be provided as a
+ /// minimum. HLVM will use the machine's natural floating point
+ /// representation for those real types that can fit within the requested
+ /// precision and mantissa lengths. If not, infinite precision floating point
+ /// arithmetic will be utilized.
+ class RealType : public Type
+ {
+ /// @name Constructors
+ /// @{
+ public:
+ RealType(
+ Node* parent, ///< The bundle in which the function is defined
+ const std::string& name ///< The name of the function
+ ) : Type(parent,name) {}
+ virtual ~RealType();
+
+ /// @}
+ /// @name Data
+ /// @{
+ protected:
+ uint32_t precision_; ///< Number of decimal digits of precision
+ uint32_t mantissa_; ///< Number of decimal digits in mantissa
+ /// @}
+ };
+
+ /// This class represents a fixed size, packed vector of some other type.
+ /// Where possible, HLVM will attempt to generate code that makes use of a
+ /// machines vector instructions to process such types. If not possible, HLVM
+ /// will treat the vector the same as an Array.
+ class VectorType : public Type
+ {
+ /// @name Constructors
+ /// @{
+ public:
+ VectorType(
+ Node* parent, ///< The bundle in which the function is defined
+ const std::string& name ///< The name of the function
+ ) : Type(parent,name) {}
+ virtual ~VectorType();
+
+ /// @}
+ /// @name Data
+ /// @{
+ protected:
+ uint32_t precision_; ///< Number of decimal digits of precision
+ uint32_t mantissa_; ///< Number of decimal digits in mantissa
+ /// @}
+ };
} // AST
} // hlvm
#endif
More information about the llvm-commits
mailing list