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

ShStatement.cpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright (c) 2003 University of Waterloo Computer Graphics Laboratory
00004 // Project administrator: Michael D. McCool
00005 // Authors: Zheng Qin, Stefanus Du Toit, Kevin Moule, Tiberiu S. Popa,
00006 //          Michael D. McCool
00007 // 
00008 // This software is provided 'as-is', without any express or implied
00009 // warranty. In no event will the authors be held liable for any damages
00010 // arising from the use of this software.
00011 // 
00012 // Permission is granted to anyone to use this software for any purpose,
00013 // including commercial applications, and to alter it and redistribute it
00014 // freely, subject to the following restrictions:
00015 // 
00016 // 1. The origin of this software must not be misrepresented; you must
00017 // not claim that you wrote the original software. If you use this
00018 // software in a product, an acknowledgment in the product documentation
00019 // would be appreciated but is not required.
00020 // 
00021 // 2. Altered source versions must be plainly marked as such, and must
00022 // not be misrepresented as being the original software.
00023 // 
00024 // 3. This notice may not be removed or altered from any source
00025 // distribution.
00027 #include <iostream>
00028 #include "ShStatement.hpp"
00029 
00030 namespace SH {
00031 
00032 ShStatementInfo::ShStatementInfo()
00033 {
00034 }
00035 
00036 ShStatementInfo::~ShStatementInfo()
00037 {
00038 }
00039 
00040 ShStatement::ShStatement(ShVariable dest, ShOperation op)
00041   : dest(dest), op(op)
00042 {
00043 }
00044 
00045 ShStatement::ShStatement(ShVariable dest, ShOperation op, ShVariable src1)
00046   : dest(dest), op(op)
00047 {
00048   if(op == SH_OP_ASN && dest.size() != src1.size()) {
00049     SH_DEBUG_PRINT("SH_OP_ASN dest.size() != src.size() (" << dest.size() << " != " << src1.size());
00050     SH_DEBUG_PRINT("  dest=" << dest.name() << " src=" << src1.name());
00051     SH_DEBUG_ASSERT(dest.size() == src1.size());
00052   }
00053   src[0] = src1;
00054 }
00055 
00056 ShStatement::ShStatement(ShVariable dest, ShVariable src0, ShOperation op, ShVariable src1)
00057   : dest(dest), op(op)
00058 {
00059   src[0] = src0;
00060   src[1] = src1;
00061 }
00062 
00063 ShStatement::ShStatement(ShVariable dest, ShOperation op, ShVariable src0, ShVariable src1, ShVariable src2)
00064   : dest(dest), op(op)
00065 {
00066   src[0] = src0;
00067   src[1] = src1;
00068   src[2] = src2;
00069 }
00070 
00071 ShStatement::ShStatement(const ShStatement& other)
00072   : dest(other.dest),
00073     op(other.op),
00074     marked(other.marked)
00075 {
00076   for (int i = 0; i < 3; i++) src[i] = other.src[i];
00077   for (std::list<ShStatementInfo*>::const_iterator I = other.info.begin(); I != other.info.end(); ++I) {
00078     info.push_back((*I)->clone());
00079   }
00080 }
00081 
00082 
00083 ShStatement& ShStatement::operator=(const ShStatement& other)
00084 {
00085   if (&other == this) return *this;
00086   
00087   dest = other.dest;
00088   op = other.op;
00089   marked = other.marked;
00090   for (int i = 0; i < 3; i++) src[i] = other.src[i];
00091   
00092   for (std::list<ShStatementInfo*>::const_iterator I = info.begin();
00093        I != info.end(); ++I) {
00094     delete *I;
00095   }
00096   info.clear();
00097   
00098   for (std::list<ShStatementInfo*>::const_iterator I = other.info.begin();
00099        I != other.info.end(); ++I) {
00100     info.push_back((*I)->clone());
00101   }
00102 
00103   return *this;
00104 }
00105 
00106 ShStatement::~ShStatement()
00107 {
00108   for (std::list<ShStatementInfo*>::iterator I = info.begin(); I != info.end(); ++I) {
00109     delete *I;
00110   }
00111 }
00112 
00113 std::ostream& operator<<(std::ostream& out, const ShStatement& stmt)
00114 {
00115   if (stmt.op == SH::SH_OP_ASN) {
00116     // Special case for assignment
00117     out << (stmt.dest.neg() ? "-" : "") << stmt.dest.name() << stmt.dest.swizzle() << " := " << stmt.src[0].name() << stmt.src[0].swizzle();
00118     return out;
00119   }
00120   
00121   switch (SH::opInfo[stmt.op].arity) {
00122   case 0:
00123     out << SH::opInfo[stmt.op].name << " " << (stmt.dest.neg() ? "-" : "") << stmt.dest.name() << stmt.dest.swizzle();
00124     break;
00125   case 1:
00126     out << (stmt.dest.neg() ? "-" : "") << stmt.dest.name() << stmt.dest.swizzle() << " := " << SH::opInfo[stmt.op].name
00127         << " " << (stmt.src[0].neg() ? "-" : "") << stmt.src[0].name() << stmt.src[0].swizzle();
00128     break;
00129   case 2:
00130     out << (stmt.dest.neg() ? "-" : "") << stmt.dest.name() << stmt.dest.swizzle() << " := " << (stmt.src[0].neg() ? "-" : "") << stmt.src[0].name() << stmt.src[0].swizzle()
00131         << " " << SH::opInfo[stmt.op].name << " " <<(stmt.src[1].neg() ? "-" : "") <<  stmt.src[1].name() << stmt.src[1].swizzle();
00132     break;
00133   case 3:
00134     out << (stmt.dest.neg() ? "-" : "") << stmt.dest.name() << stmt.dest.swizzle() << " := " << SH::opInfo[stmt.op].name << " "
00135         << (stmt.src[0].neg() ? "-" : "") << stmt.src[0].name() << stmt.src[0].swizzle() << ", " 
00136         << (stmt.src[1].neg() ? "-" : "") << stmt.src[1].name() << stmt.src[1].swizzle() << ", "
00137         << (stmt.src[2].neg() ? "-" : "") << stmt.src[2].name() << stmt.src[2].swizzle();
00138     break;
00139   default:
00140     out << "<<<Unknown arity>>>";
00141     break;
00142   }
00143   return out;
00144 }
00145 
00146 void ShStatement::add_info(ShStatementInfo* new_info)
00147 {
00148   info.push_back(new_info);
00149 }
00150 
00151 void ShStatement::remove_info(ShStatementInfo* old_info)
00152 {
00153   info.remove(old_info);
00154 }
00155 
00156 } // namespace SH
00157 

Generated on Mon Jan 24 18:36:35 2005 for Sh by  doxygen 1.4.1