00001 /* 00002 // $Id: //open/dev/fennel/calculator/InstructionFactory.cpp#2 $ 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 #include "fennel/common/CommonPreamble.h" 00023 #include "fennel/calculator/InstructionFactory.h" 00024 #include "fennel/calculator/InstructionCommon.h" 00025 00026 FENNEL_BEGIN_CPPFILE("$Id: //open/dev/fennel/calculator/InstructionFactory.cpp#2 $"); 00027 00028 StringToCreateFn InstructionFactory::instructionTable; 00029 ExtendedInstructionTable InstructionFactory::extendedTable; 00030 00031 // Create a typical instruction 00032 Instruction* 00033 InstructionFactory::createInstruction( 00034 string const & name, 00035 vector<RegisterReference*> const &operands) 00036 { 00037 InstructionSignature signature(name, operands); 00038 return createInstructionHelper(signature); 00039 } 00040 00041 // Create a Jump instruction 00042 Instruction* 00043 InstructionFactory::createInstruction( 00044 string const & name, 00045 TProgramCounter pc, 00046 RegisterReference* operand) 00047 { 00048 vector<RegisterReference*> v; 00049 if (operand) { 00050 v.push_back(operand); 00051 } 00052 00053 InstructionSignature signature(name, pc, v); 00054 return createInstructionHelper(signature); 00055 } 00056 00057 Instruction* 00058 InstructionFactory::createInstructionHelper(InstructionSignature const &sig) 00059 { 00060 InstructionCreateFunction createFn = instructionTable[sig.compute()]; 00061 00062 if (createFn) { 00063 return createFn(sig); 00064 } else { 00065 throw FennelExcn(sig.compute() + " is not a registered instruction"); 00066 } 00067 } 00068 00069 // Create an Extended instruction 00070 Instruction* 00071 InstructionFactory::createInstruction( 00072 string const & name, 00073 string const & function, 00074 vector<RegisterReference*> const &operands) 00075 { 00076 InstructionSignature signature(function, operands); 00077 ExtendedInstructionDef* instDef = extendedTable[signature.compute()]; 00078 if (instDef == NULL) { 00079 return NULL; 00080 } 00081 return instDef->createInstruction(operands); 00082 } 00083 00084 string 00085 InstructionFactory::signatures() 00086 { 00087 ostringstream s(""); 00088 00089 StringToCreateFnIter i = instructionTable.begin(); 00090 StringToCreateFnIter end = instructionTable.end(); 00091 00092 while (i != end) { 00093 s << (*i).first << endl; 00094 i++; 00095 } 00096 return s.str(); 00097 } 00098 00099 string 00100 InstructionFactory::extendedSignatures() 00101 { 00102 return extendedTable.signatures(); 00103 } 00104 00105 void 00106 InstructionFactory::registerInstructions() 00107 { 00108 BoolInstructionRegister::registerInstructions(); 00109 BoolNativeInstructionRegister::registerInstructions(); 00110 BoolPointerInstructionRegister::registerInstructions(); 00111 CastInstructionRegister::registerInstructions(); 00112 IntegralNativeInstructionRegister::registerInstructions(); 00113 IntegralPointerInstructionRegister::registerInstructions(); 00114 JumpInstructionRegister::registerInstructions(); 00115 NativeNativeInstructionRegister::registerInstructions(); 00116 PointerIntegralInstructionRegister::registerInstructions(); 00117 PointerPointerInstructionRegister::registerInstructions(); 00118 ReturnInstructionRegister::registerInstructions(); 00119 } 00120 00121 FENNEL_END_CPPFILE("$Id: //open/dev/fennel/calculator/InstructionFactory.cpp#2 $"); 00122 00123 // End InstructionFactory.cpp