ShCastManager.hpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright 2003-2006 Serious Hack Inc.
00004 // 
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
00018 // MA  02110-1301, USA
00020 #ifndef SHCASTMANAGER_HPP
00021 #define SHCASTMANAGER_HPP
00022 
00023 #include <map>
00024 #include "ShHashMap.hpp"
00025 #include "ShRefCount.hpp"
00026 #include "ShGraph.hpp"
00027 #include "ShTypeInfo.hpp"
00028 
00029 namespace SH {
00030 
00031 class ShVariant;
00032 class ShVariantCast;
00033 
00062 struct ShCastMgrEdge;
00063 struct ShCastMgrVertex;
00064 typedef ShGraphType<ShCastMgrVertex, ShCastMgrEdge> ShCastMgrGraphType;
00065 
00066 struct 
00067 SH_DLLEXPORT
00068 ShCastMgrEdge: public ShGraphEdge<ShCastMgrGraphType> 
00069 {
00070   // Creates an edges describing a relationship between two types.
00071   // automatic = true iff the cast is automatic
00072   // precedence = true iff this is an edge in the precedenced DAG 
00073   ShCastMgrEdge(const ShVariantCast *caster, bool automatic);
00074   ShCastMgrEdge(const ShCastMgrEdge &other);
00075 
00076   std::ostream& graphvizDump(std::ostream& out) const;
00077 
00078   const ShVariantCast *m_caster;
00079   bool m_auto; 
00080 };
00081 
00082 struct 
00083 SH_DLLEXPORT
00084 ShCastMgrVertex: public ShGraphVertex<ShCastMgrGraphType>
00085 {
00086   ShCastMgrVertex(ShValueType valueType, ShDataType dataType);
00087   ShCastMgrVertex(const ShCastMgrVertex &other);
00088 
00089   std::ostream& graphvizDump(std::ostream& out) const;
00090 
00091   ShValueType m_valueType;
00092   ShDataType m_dataType;
00093 };
00094 
00095 class 
00096 SH_DLLEXPORT
00097 ShCastMgrGraph: public ShGraph<ShCastMgrGraphType>
00098 {
00099   public:
00100     ShCastMgrGraph();
00101 
00102     // functions to use instead of default addVertex, addEdge
00103     ShCastMgrVertex *addVertex(ShValueType valueType, ShDataType dataType);
00104 
00105     // Use this function instead of the default addVertex/addEdge
00106     // automatically adds src/dest indices and sets start/end on the edge 
00107     void addEdge(ShCastMgrEdge* edge); 
00108 
00109   protected:
00110     typedef ShPairHashMap<ShValueType, ShDataType, ShCastMgrVertex*> VertexArray;
00111     VertexArray m_vert;
00112 };
00113 
00114 class 
00115 SH_DLLEXPORT
00116 ShCastManager {
00117   public:
00118     void addCast(const ShVariantCast *caster, bool automatic);
00119 
00120     // initializes caches, checks cast graph for errors 
00121     // (duplicate edges, cycles) 
00122     void init();
00123 
00131     void doCast(ShVariant *dest, const ShVariant *src);
00132     // @}
00133 
00141     bool doAllocCast(ShVariant *& dest, ShVariant *src,
00142         ShValueType valueType, ShDataType dataType);
00143     bool doAllocCast(const ShVariant *& dest, const ShVariant *src,
00144         ShValueType valueType, ShDataType dataType);
00145     // @}
00146 
00147     // returns distance of a cast using automatic promotions 
00148     // -1 if the cast is impossible
00149     int castDist(ShValueType destValueType, ShValueType srcValueType);
00150 
00151     std::ostream& graphvizDump(std::ostream& out) const;
00152 
00153     static ShCastManager* instance();
00154 
00155   protected:
00156     // graph of available ShVariantCast objects (explicit and automatic)
00157     ShCastMgrGraph m_casts;
00158 
00159     // TODO the two graphs should realy be one with a different 
00160     // distance functor 
00161 
00162     // add cached versions of cast order for different casts between indices
00163     // FirstCastMap[dest][destdt][src][srcdt] holds the first caster to use for 
00164     // getting from src to dest (or 0 if no cast path exists)
00165     typedef ShPairPairHashMap<ShValueType, ShDataType, ShValueType, ShDataType, 
00166               const ShVariantCast*> FirstCastMap;
00167 
00168     typedef ShPairHashMap<ShValueType, ShValueType, int> CastDistMap;
00169 
00170     // shortest paths using any kind of cast
00171     FirstCastMap m_castStep;
00172 
00173     // shortest distance only in precedence DAG 
00174     CastDistMap m_autoDist;
00175 
00176     static ShCastManager* m_instance;
00177 };
00178 
00179 }
00180 
00181 #endif

Generated on Thu Feb 16 14:51:31 2006 for Sh by  doxygen 1.4.6