Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ShCastManager.hpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright 2003-2005 Serious Hack Inc.
00004 // 
00005 // This software is provided 'as-is', without any express or implied
00006 // warranty. In no event will the authors be held liable for any damages
00007 // arising from the use of this software.
00008 // 
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it
00011 // freely, subject to the following restrictions:
00012 // 
00013 // 1. The origin of this software must not be misrepresented; you must
00014 // not claim that you wrote the original software. If you use this
00015 // software in a product, an acknowledgment in the product documentation
00016 // would be appreciated but is not required.
00017 // 
00018 // 2. Altered source versions must be plainly marked as such, and must
00019 // not be misrepresented as being the original software.
00020 // 
00021 // 3. This notice may not be removed or altered from any source
00022 // distribution.
00024 #ifndef SHCASTMANAGER_HPP
00025 #define SHCASTMANAGER_HPP
00026 
00027 #include <map>
00028 #include "ShHashMap.hpp"
00029 #include "ShRefCount.hpp"
00030 #include "ShGraph.hpp"
00031 #include "ShTypeInfo.hpp"
00032 
00033 namespace SH {
00034 
00035 class ShVariant;
00036 class ShVariantCast;
00037 
00066 struct ShCastMgrEdge;
00067 struct ShCastMgrVertex;
00068 typedef ShGraphType<ShCastMgrVertex, ShCastMgrEdge> ShCastMgrGraphType;
00069 
00070 struct 
00071 SH_DLLEXPORT
00072 ShCastMgrEdge: public ShGraphEdge<ShCastMgrGraphType> 
00073 {
00074   // Creates an edges describing a relationship between two types.
00075   // automatic = true iff the cast is automatic
00076   // precedence = true iff this is an edge in the precedenced DAG 
00077   ShCastMgrEdge(const ShVariantCast *caster, bool automatic);
00078   ShCastMgrEdge(const ShCastMgrEdge &other);
00079 
00080   std::ostream& graphvizDump(std::ostream& out) const;
00081 
00082   const ShVariantCast *m_caster;
00083   bool m_auto; 
00084 };
00085 
00086 struct 
00087 SH_DLLEXPORT
00088 ShCastMgrVertex: public ShGraphVertex<ShCastMgrGraphType>
00089 {
00090   ShCastMgrVertex(ShValueType valueType, ShDataType dataType);
00091   ShCastMgrVertex(const ShCastMgrVertex &other);
00092 
00093   std::ostream& graphvizDump(std::ostream& out) const;
00094 
00095   ShValueType m_valueType;
00096   ShDataType m_dataType;
00097 };
00098 
00099 class 
00100 SH_DLLEXPORT
00101 ShCastMgrGraph: public ShGraph<ShCastMgrGraphType>
00102 {
00103   public:
00104     ShCastMgrGraph();
00105 
00106     // functions to use instead of default addVertex, addEdge
00107     ShCastMgrVertex *addVertex(ShValueType valueType, ShDataType dataType);
00108 
00109     // Use this function instead of the default addVertex/addEdge
00110     // automatically adds src/dest indices and sets start/end on the edge 
00111     void addEdge(ShCastMgrEdge* edge); 
00112 
00113   protected:
00114     typedef ShPairHashMap<ShValueType, ShDataType, ShCastMgrVertex*> VertexArray;
00115     VertexArray m_vert;
00116 };
00117 
00118 class 
00119 SH_DLLEXPORT
00120 ShCastManager {
00121   public:
00122     void addCast(const ShVariantCast *caster, bool automatic);
00123 
00124     // initializes caches, checks cast graph for errors 
00125     // (duplicate edges, cycles) 
00126     void init();
00127 
00135     void doCast(ShVariant *dest, const ShVariant *src);
00136     // @}
00137 
00145     bool doAllocCast(ShVariant *& dest, ShVariant *src,
00146         ShValueType valueType, ShDataType dataType);
00147     bool doAllocCast(const ShVariant *& dest, const ShVariant *src,
00148         ShValueType valueType, ShDataType dataType);
00149     // @}
00150 
00151     // returns distance of a cast using automatic promotions 
00152     // -1 if the cast is impossible
00153     int castDist(ShValueType destValueType, ShValueType srcValueType);
00154 
00155     std::ostream& graphvizDump(std::ostream& out) const;
00156 
00157     static ShCastManager* instance();
00158 
00159   protected:
00160     // graph of available ShVariantCast objects (explicit and automatic)
00161     ShCastMgrGraph m_casts;
00162 
00163     // TODO the two graphs should realy be one with a different 
00164     // distance functor 
00165 
00166     // add cached versions of cast order for different casts between indices
00167     // FirstCastMap[dest][destdt][src][srcdt] holds the first caster to use for 
00168     // getting from src to dest (or 0 if no cast path exists)
00169     typedef ShPairPairHashMap<ShValueType, ShDataType, ShValueType, ShDataType, 
00170               const ShVariantCast*> FirstCastMap;
00171 
00172     typedef ShPairHashMap<ShValueType, ShValueType, int> CastDistMap;
00173 
00174     // shortest paths using any kind of cast
00175     FirstCastMap m_castStep;
00176 
00177     // shortest distance only in precedence DAG 
00178     CastDistMap m_autoDist;
00179 
00180     static ShCastManager* m_instance;
00181 };
00182 
00183 }
00184 
00185 #endif

Generated on Wed Jun 15 18:12:38 2005 for Sh by  doxygen 1.4.3-20050530