PointerIntegralInstruction.h

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/calculator/PointerIntegralInstruction.h#5 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2005-2009 The Eigenbase Project
00005 // Copyright (C) 2004-2009 SQLstream, Inc.
00006 // Copyright (C) 2009-2009 LucidEra, Inc.
00007 //
00008 // This program is free software; you can redistribute it and/or modify it
00009 // under the terms of the GNU General Public License as published by the Free
00010 // Software Foundation; either version 2 of the License, or (at your option)
00011 // any later version approved by The Eigenbase Project.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 */
00022 #ifndef Fennel_PointerIntegralInstruction_Included
00023 #define Fennel_PointerIntegralInstruction_Included
00024 
00025 #include "fennel/calculator/PointerInstruction.h"
00026 
00027 FENNEL_BEGIN_NAMESPACE
00028 
00029 
00030 template<typename PTR_TYPE>
00031 class PointerIntegralInstruction : public PointerInstruction
00032 {
00033 public:
00034     explicit
00035     PointerIntegralInstruction(
00036         RegisterRef<PTR_TYPE>* result,
00037         RegisterRef<PointerSizeT>* op1,
00038         StandardTypeDescriptorOrdinal pointerType)
00039         : mResult(result),
00040           mOp1(op1),
00041           mPointerType(pointerType)
00042     {}
00043 
00044     ~PointerIntegralInstruction() {
00045 #ifndef __MSVC__
00046         // If (0) to reduce performance impact of template type checking
00047         if (0) {
00048             PointerInstruction_NotAPointerType<PTR_TYPE>();
00049         }
00050 #endif
00051     }
00052 
00053 protected:
00054     RegisterRef<PTR_TYPE>* mResult;
00055     RegisterRef<PointerSizeT>* mOp1;
00056     StandardTypeDescriptorOrdinal mPointerType;
00057 };
00058 
00059 // TODO: Rename to PointerPutLength to be consistent with RegisterReference
00060 // TODO: accessors.
00061 template <typename PTR_TYPE>
00062 class PointerPutSize : public PointerIntegralInstruction<PTR_TYPE>
00063 {
00064 public:
00065     explicit
00066     PointerPutSize(
00067         RegisterRef<PTR_TYPE>* result,
00068         RegisterRef<PointerSizeT>* op1,
00069         StandardTypeDescriptorOrdinal pointerType)
00070         : PointerIntegralInstruction<PTR_TYPE>(result, op1, pointerType)
00071     {}
00072 
00073     virtual
00074     ~PointerPutSize() {}
00075 
00076     virtual void exec(TProgramCounter& pc) const {
00077         pc++;
00078 
00079         if (PointerIntegralInstruction<PTR_TYPE>::mOp1->isNull()) {
00080             PointerIntegralInstruction<PTR_TYPE>::mResult->toNull();
00081             PointerIntegralInstruction<PTR_TYPE>::mResult->length(0);
00082         } else {
00083             // get value, put size
00084             PointerIntegralInstruction<PTR_TYPE>::mResult->length
00085                (PointerIntegralInstruction<PTR_TYPE>::mOp1->value());
00086         }
00087     }
00088 
00089     static const char * longName()
00090     {
00091         return "PointerPutSize";
00092     }
00093 
00094     static const char * shortName()
00095     {
00096         return "PUTS";
00097     }
00098 
00099     static int numArgs()
00100     {
00101         return 2;
00102     }
00103 
00104     void describe(string& out, bool values) const {
00105         RegisterRef<PTR_TYPE> mOp2; // create invalid regref
00106         describeHelper(
00107             out, values, longName(), shortName(),
00108             PointerIntegralInstruction<PTR_TYPE>::mResult,
00109             PointerIntegralInstruction<PTR_TYPE>::mOp1, &mOp2);
00110     }
00111 
00112     static InstructionSignature
00113     signature(StandardTypeDescriptorOrdinal type) {
00114         vector<StandardTypeDescriptorOrdinal> v;
00115         v.push_back(type);
00116         v.push_back(POINTERSIZET_STANDARD_TYPE);
00117         return InstructionSignature(shortName(), v);
00118     }
00119 
00120     static Instruction*
00121     create(InstructionSignature const & sig)
00122     {
00123         assert(sig.size() == numArgs());
00124         assert((sig[1])->type() == POINTERSIZET_STANDARD_TYPE);
00125         return new
00126             PointerPutSize(
00127                 static_cast<RegisterRef<PTR_TYPE>*> (sig[0]),
00128                 static_cast<RegisterRef<PointerSizeT>*> (sig[1]),
00129                 (sig[0])->type());
00130     }
00131 };
00132 
00135 
00136 class FENNEL_CALCULATOR_EXPORT PointerIntegralInstructionRegister
00137     : InstructionRegister {
00138 
00139     // TODO: Refactor registerTypes to class InstructionRegister
00140     template < template <typename> class INSTCLASS2 >
00141     static void
00142     registerTypes(vector<StandardTypeDescriptorOrdinal> const &t) {
00143 
00144         for (uint i = 0; i < t.size(); i++) {
00145             StandardTypeDescriptorOrdinal type = t[i];
00146             // Type <char> below is a placeholder and is ignored.
00147             InstructionSignature sig = INSTCLASS2<char>::signature(type);
00148             switch (type) {
00149                 // Array_Text, below, does not allow assembly programs
00150                 // of to have say, pointer to int16s, but the language
00151                 // does not have pointers defined other than
00152                 // c,vc,b,vb, so this is OK for now.
00153 #define Fennel_InstructionRegisterSwitch_Array 1
00154 #include "fennel/calculator/InstructionRegisterSwitch.h"
00155             default:
00156                 throw std::logic_error("Default InstructionRegister");
00157             }
00158         }
00159     }
00160 
00161 public:
00162     static void
00163     registerInstructions() {
00164         vector<StandardTypeDescriptorOrdinal> t;
00165         // isArray, below, does not allow assembly programs of to
00166         // have say, pointer to int16s, but the language does not have
00167         // pointers defined other than c,vc,b,vb, so this is OK for now.
00168         t = InstructionSignature::typeVector(StandardTypeDescriptor::isArray);
00169 
00170         // Have to do full fennel:: qualification of template
00171         // arguments below to prevent template argument 'TMPLT', of
00172         // this encapsulating class, from perverting NativeAdd into
00173         // NativeAdd<TMPLT> or something like
00174         // that. Anyway. Fennel::NativeAdd works just fine.
00175         registerTypes<fennel::PointerPutSize>(t);
00176         // Note: Cannot have PointerPutSizeMax. See comment above
00177     }
00178 };
00179 
00180 
00181 FENNEL_END_NAMESPACE
00182 
00183 #endif
00184 
00185 // End PointerIntegralInstruction.h
00186 

Generated on Mon Jun 22 04:00:17 2009 for Fennel by  doxygen 1.5.1