IntegralPointerInstruction.h

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/calculator/IntegralPointerInstruction.h#3 $
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_IntegralPointerInstruction_Included
00023 #define Fennel_IntegralPointerInstruction_Included
00024 
00025 #include "fennel/calculator/PointerInstruction.h"
00026 
00027 FENNEL_BEGIN_NAMESPACE
00028 
00031 
00032 
00033 template<typename PTR_TYPE>
00034 class IntegralPointerInstruction
00035     : public PointerInstruction
00036 {
00037 public:
00038     explicit
00039     IntegralPointerInstruction(
00040         RegisterRef<PointerSizeT>* result,
00041         RegisterRef<PTR_TYPE>* op1,
00042         StandardTypeDescriptorOrdinal pointerType)
00043         : mResult(result),
00044           mOp1(op1),
00045           mPointerType(pointerType)
00046     {}
00047 
00048     ~IntegralPointerInstruction() {
00049 #ifndef __MSVC__
00050         // If (0) to reduce performance impact of template type checking
00051         if (0) {
00052             PointerInstruction_NotAPointerType<PTR_TYPE>();
00053         }
00054 #endif
00055     }
00056 
00057 protected:
00058     RegisterRef<PointerSizeT>* mResult;
00059     RegisterRef<PTR_TYPE>* mOp1;
00060     StandardTypeDescriptorOrdinal mPointerType;
00061 };
00062 
00063 template <typename PTR_TYPE>
00064 class PointerGetSize : public IntegralPointerInstruction<PTR_TYPE>
00065 {
00066 public:
00067     explicit
00068     PointerGetSize(
00069         RegisterRef<PointerSizeT>* result,
00070         RegisterRef<PTR_TYPE>* op1,
00071         StandardTypeDescriptorOrdinal pointerType)
00072         : IntegralPointerInstruction<PTR_TYPE>(result, op1, pointerType)
00073     {}
00074 
00075     virtual
00076     ~PointerGetSize() {}
00077 
00078     virtual void exec(TProgramCounter& pc) const {
00079         pc++;
00080 
00081         if (IntegralPointerInstruction<PTR_TYPE>::mOp1->isNull()) {
00082             IntegralPointerInstruction<PTR_TYPE>::mResult->toNull();
00083         } else {
00084             // get size, put value
00085             IntegralPointerInstruction<PTR_TYPE>::mResult->value(
00086                 IntegralPointerInstruction<PTR_TYPE>::mOp1->length());
00087         }
00088     }
00089 
00090     static const char * longName()
00091     {
00092         return "PointerGetSize";
00093     }
00094 
00095     static const char * shortName()
00096     {
00097         return "GETS";
00098     }
00099 
00100     static int numArgs()
00101     {
00102         return 2;
00103     }
00104 
00105     void describe(string& out, bool values) const {
00106         RegisterRef<PTR_TYPE> mOp2; // create invalid regref
00107         describeHelper(
00108             out, values, longName(), shortName(),
00109             IntegralPointerInstruction<PTR_TYPE>::mResult,
00110             IntegralPointerInstruction<PTR_TYPE>::mOp1, &mOp2);
00111     }
00112 
00113     static InstructionSignature
00114     signature(StandardTypeDescriptorOrdinal type) {
00115         return InstructionSignature(
00116             shortName(),
00117             regDesc(1, numArgs() - 1, type, 0));
00118     }
00119 
00120     static Instruction*
00121     create(InstructionSignature const & sig)
00122     {
00123         assert(sig.size() == numArgs());
00124         assert((sig[0])->type() == POINTERSIZET_STANDARD_TYPE);
00125         return new
00126             PointerGetSize(
00127                 static_cast<RegisterRef<PointerSizeT>*> (sig[0]),
00128                 static_cast<RegisterRef<PTR_TYPE>*> (sig[1]),
00129                 (sig[1])->type());
00130     }
00131 };
00132 
00133 template <typename PTR_TYPE>
00134 class PointerGetMaxSize : public IntegralPointerInstruction<PTR_TYPE>
00135 {
00136 public:
00137     explicit
00138     PointerGetMaxSize(
00139         RegisterRef<PointerSizeT>* result,
00140         RegisterRef<PTR_TYPE>* op1,
00141         StandardTypeDescriptorOrdinal pointerType)
00142         : IntegralPointerInstruction<PTR_TYPE>(result, op1, pointerType)
00143     {}
00144 
00145     virtual
00146     ~PointerGetMaxSize() {}
00147 
00148     virtual void exec(TProgramCounter& pc) const {
00149         pc++;
00150 
00151         if (IntegralPointerInstruction<PTR_TYPE>::mOp1->isNull()) {
00152             IntegralPointerInstruction<PTR_TYPE>::mResult->toNull();
00153         } else {
00154             // get size, put value
00155             IntegralPointerInstruction<PTR_TYPE>::mResult->value(
00156                 IntegralPointerInstruction<PTR_TYPE>::mOp1->storage());
00157         }
00158     }
00159 
00160     static const char * longName()
00161     {
00162         return "PointerGetMaxSize";
00163     }
00164 
00165     static const char * shortName()
00166     {
00167         return "GETMS";
00168     }
00169 
00170     static int numArgs()
00171     {
00172         return 2;
00173     }
00174 
00175     void describe(string& out, bool values) const {
00176         RegisterRef<PTR_TYPE> mOp2; // create invalid regref
00177         describeHelper(
00178             out, values, longName(), shortName(),
00179             IntegralPointerInstruction<PTR_TYPE>::mResult,
00180             IntegralPointerInstruction<PTR_TYPE>::mOp1, &mOp2);
00181     }
00182 
00183     static InstructionSignature
00184     signature(StandardTypeDescriptorOrdinal type) {
00185         return InstructionSignature(
00186             shortName(),
00187             regDesc(1, numArgs() - 1, type, 0));
00188     }
00189 
00190     static Instruction*
00191     create(InstructionSignature const & sig)
00192     {
00193         assert(sig.size() == numArgs());
00194         assert((sig[0])->type() == POINTERSIZET_STANDARD_TYPE);
00195         return new
00196             PointerGetMaxSize(
00197                 static_cast<RegisterRef<PointerSizeT>*> (sig[0]),
00198                 static_cast<RegisterRef<PTR_TYPE>*> (sig[1]),
00199                 (sig[1])->type());
00200     }
00201 };
00202 
00203 
00204 class FENNEL_CALCULATOR_EXPORT IntegralPointerInstructionRegister
00205     : InstructionRegister
00206 {
00207     // TODO: Refactor registerTypes to class InstructionRegister
00208     template < template <typename> class INSTCLASS2 >
00209     static void
00210     registerTypes(vector<StandardTypeDescriptorOrdinal> const &t) {
00211 
00212         for (uint i = 0; i < t.size(); i++) {
00213             StandardTypeDescriptorOrdinal type = t[i];
00214             // Type <char> below is a placeholder and is ignored.
00215             InstructionSignature sig = INSTCLASS2<char>::signature(type);
00216             switch (type) {
00217                 // Array_Text, below, does not allow assembly programs
00218                 // of to have say, pointer to int16s, but the language
00219                 // does not have pointers defined other than
00220                 // c,vc,b,vb, so this is OK for now.
00221 #define Fennel_InstructionRegisterSwitch_Array 1
00222 #include "fennel/calculator/InstructionRegisterSwitch.h"
00223             default:
00224                 throw std::logic_error("Default InstructionRegister");
00225             }
00226         }
00227     }
00228 
00229 public:
00230     static void
00231     registerInstructions() {
00232         vector<StandardTypeDescriptorOrdinal> t;
00233         // isArray, below, does not allow assembly programs of to
00234         // have say, pointer to int16s, but the language does not have
00235         // pointers defined other than c,vc,b,vb, so this is OK for now.
00236         t = InstructionSignature::typeVector(StandardTypeDescriptor::isArray);
00237 
00238         // Have to do full fennel:: qualification of template
00239         // arguments below to prevent template argument 'TMPLT', of
00240         // this encapsulating class, from perverting NativeAdd into
00241         // NativeAdd<TMPLT> or something like
00242         // that. Anyway. Fennel::NativeAdd works just fine.
00243         registerTypes<fennel::PointerGetSize>(t);
00244         registerTypes<fennel::PointerGetMaxSize>(t);
00245     }
00246 };
00247 
00248 FENNEL_END_NAMESPACE
00249 
00250 #endif
00251 
00252 // End IntegralPointerInstruction.h
00253 

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