From 431bbac5a99abbccf33500e22aa353ec792eff94 Mon Sep 17 00:00:00 2001 From: manuel Date: Wed, 13 May 2009 15:29:51 +0200 Subject: * adding -Wno-long-long to all Makefiles * adding execute operator to cinstruction * added labels map for better performance --- ue1/imgsynth/Makefile | 2 +- ue2/imgsynth2/Makefile | 2 +- ue3/mycpu/ccpu.cpp | 2 +- ue3/mycpu/ccpu.h | 2 +- ue3/mycpu/cinstruction.h | 47 ++++++++++++--------------------- ue3/mycpu/cprogram.cpp | 21 +++++++++++++++ ue3/mycpu/cprogram.h | 27 +++++++++++++++++++ ue3/mycpu/instructions.cpp | 65 +++------------------------------------------- ue3/mycpu/instructions.h | 20 ++++---------- 9 files changed, 78 insertions(+), 110 deletions(-) diff --git a/ue1/imgsynth/Makefile b/ue1/imgsynth/Makefile index 6b52d60..71f57d7 100644 --- a/ue1/imgsynth/Makefile +++ b/ue1/imgsynth/Makefile @@ -5,7 +5,7 @@ CC= g++ LD= $(CC) DEBUGFLAGS= -CFLAGS= -O -ansi -pedantic-errors -Wall $(DEBUGFLAGS) +CFLAGS= -O -ansi -pedantic-errors -Wall -Wno-long-long $(DEBUGFLAGS) LDFLAGS= LIBS= -lboost_program_options diff --git a/ue2/imgsynth2/Makefile b/ue2/imgsynth2/Makefile index 481230c..932ae97 100644 --- a/ue2/imgsynth2/Makefile +++ b/ue2/imgsynth2/Makefile @@ -6,7 +6,7 @@ CXX= g++ LD= $(CXX) DEBUGFLAGS= -DNDEBUG INCLUDE_PATH= -I/usr/local/include -CXXFLAGS= -O -ansi -pedantic-errors -Wall $(INCLUDE_PATH) $(DEBUGFLAGS) +CXXFLAGS= -O -ansi -pedantic-errors -Wall -Wno-long-long $(INCLUDE_PATH) $(DEBUGFLAGS) LDFLAGS= LIBS= -L/usr/local/lib -lboost_program_options diff --git a/ue3/mycpu/ccpu.cpp b/ue3/mycpu/ccpu.cpp index b1539a4..16209e2 100644 --- a/ue3/mycpu/ccpu.cpp +++ b/ue3/mycpu/ccpu.cpp @@ -66,7 +66,7 @@ void CCPU::run() throw runtime_error("Programcounter is out of bound"); /* execute instruction */ - m_program->at(pc)->execute(this); + (*m_program->at(pc))(this); ++m_registers[0]; } } diff --git a/ue3/mycpu/ccpu.h b/ue3/mycpu/ccpu.h index 1ef1923..01c897f 100644 --- a/ue3/mycpu/ccpu.h +++ b/ue3/mycpu/ccpu.h @@ -205,7 +205,7 @@ class CCPU * @param - * @return - * @globalvars none - * @exception runtime_error + * @exception std::runtime_error * @conditions none */ void run(); diff --git a/ue3/mycpu/cinstruction.h b/ue3/mycpu/cinstruction.h index 57fcff1..40e9ddc 100644 --- a/ue3/mycpu/cinstruction.h +++ b/ue3/mycpu/cinstruction.h @@ -62,45 +62,32 @@ class CInstruction } /** - * @method getName - * @brief returns instruction name - * @param - - * @return name of instruction - * @globalvars none - * @exception none - * @conditions none - */ - virtual const std::string& getName() - { - return m_name; - } - - /** - * @method isLabel - * @brief returns true if the instruction defines a label - * @param - - * @return true if the instruction defines a label + * @method operator() + * @brief implementation of operator (CCPU) + * @param cpu pointer to cpu + * @return - * @globalvars none - * @exception none + * @exception std::runtime_error * @conditions none */ - virtual const bool isLabel() + virtual CInstruction& operator()(CCPU *cpu) { - return false; + execute(cpu); + return *this; } /** - * @method getLabelName - * @brief returns labelname if the instruction defines a label + * @method getName + * @brief returns instruction name * @param - - * @return labelname if the instruction defines a label + * @return name of instruction * @globalvars none * @exception none * @conditions none */ - virtual const std::string getLabelName() + virtual const std::string& getName() { - return ""; + return m_name; } /** @@ -139,7 +126,7 @@ class CInstruction * @param str register in assembler syntax * @return registernumber * @globalvars none - * @exception runtime_error + * @exception std::runtime_error * @conditions none */ virtual const unsigned parseRegister(const std::string& str); @@ -152,7 +139,7 @@ class CInstruction * @param regidx registernumber * @return - * @globalvars none - * @exception runtime_error + * @exception std::runtime_error * @conditions none */ virtual void checkRegister(CCPU *cpu, const unsigned regidx); @@ -175,7 +162,7 @@ class CInstruction * @param params list of parameters of this instruction * @return - * @globalvars none - * @exception runtime_error + * @exception std::runtime_error * @conditions none */ virtual void compile(std::list& params) = 0; @@ -186,7 +173,7 @@ class CInstruction * @param cpu pointer to cpu * @return - * @globalvars none - * @exception runtime_error + * @exception std::runtime_error * @conditions none */ virtual void execute(CCPU *cpu) = 0; diff --git a/ue3/mycpu/cprogram.cpp b/ue3/mycpu/cprogram.cpp index f6fc3cb..f904bce 100644 --- a/ue3/mycpu/cprogram.cpp +++ b/ue3/mycpu/cprogram.cpp @@ -107,6 +107,16 @@ void CProgram::compile(std::istream& in) /* let instruction parse the parameters. catch+throw exception */ try { + /* handle label instruction ourself, but still add a dummy instruction */ + if (instrname == "label") + { + if (instrparams.size() != 1) + throw runtime_error("Invalid paramater count - must be 1"); + string label = instrparams.front(); + if (label.length() < 2 || label[ label.length() - 1] != ':') + throw runtime_error("Label has invalid syntax"); + m_labels[ label.substr(0, label.length() - 1) ] = size(); + } instr->compile(instrparams); } catch(runtime_error& ex) @@ -123,6 +133,17 @@ void CProgram::compile(std::istream& in) /*----------------------------------------------------------------------------*/ +unsigned CProgram::findLabel(const std::string& label) const +{ + map::const_iterator it; + it = m_labels.find(label); + if (it == m_labels.end()) + throw runtime_error("Unknown label '" + label + "'"); + return it->second; +} + +/*----------------------------------------------------------------------------*/ + #if DEBUG void CProgram::dump(std::ostream& out) { diff --git a/ue3/mycpu/cprogram.h b/ue3/mycpu/cprogram.h index bf161b8..c145832 100644 --- a/ue3/mycpu/cprogram.h +++ b/ue3/mycpu/cprogram.h @@ -10,6 +10,7 @@ #include #include +#include #include "cinstruction.h" /** @@ -43,6 +44,31 @@ class CProgram */ ~CProgram(); + /** + * @method getLabels + * @brief get reference to labels map + * @param - + * @return reference to labels map + * @globalvars none + * @exception none + * @conditions none + */ + const std::map& getLabels() const + { + return m_labels; + } + + /** + * @method findLabel + * @brief search for label + * @param label name of label to search for + * @return index of found label in program + * @globalvars none + * @exception std::runtime_error + * @conditions none + */ + unsigned findLabel(const std::string& label) const; + /** * @method compile * @brief create instructions from parsing stream @@ -71,6 +97,7 @@ class CProgram /* members */ /** set of known instructions */ std::set m_instrset; + std::map m_labels; }; #endif diff --git a/ue3/mycpu/instructions.cpp b/ue3/mycpu/instructions.cpp index 609cd1c..a6ac611 100644 --- a/ue3/mycpu/instructions.cpp +++ b/ue3/mycpu/instructions.cpp @@ -5,6 +5,7 @@ * @date 10.05.2009 */ +#include #include "instructions.h" using namespace std; @@ -210,27 +211,6 @@ void CInstructionTest::execute(CCPU *cpu) /*============================================================================*/ -void CInstructionLabel::compile(std::list& params) -{ - if (params.size() != 1) - throw runtime_error("Invalid paramater count - must be 1"); - string label = params.front(); - params.pop_front(); - if (label.length() < 2 || label[ label.length() - 1] != ':') - throw runtime_error("Label has invalid syntax"); - m_label = label.substr(0, label.length() - 1); -} - -/*----------------------------------------------------------------------------*/ - -void CInstructionLabel::execute(CCPU *cpu) -{ - if (m_label.empty()) - throw runtime_error("Empty label"); -} - -/*============================================================================*/ - void CInstructionJumpA::compile(std::list& params) { if (params.size() != 1) @@ -245,18 +225,7 @@ void CInstructionJumpA::execute(CCPU *cpu) { if (m_addr.empty()) throw runtime_error("Empty address"); - - const CProgram *progam = cpu->getProgram(); - for(unsigned i = 0; i < progam->size(); i++) - { - if (progam->at(i)->isLabel() && progam->at(i)->getLabelName() == m_addr) - { - cpu->getRegisters()[ 0 ] = i; - return; - } - } - - throw runtime_error("Unknown label '" + m_addr + "'"); + cpu->getRegisters()[ 0 ] = cpu->getProgram()->findLabel(m_addr); } /*============================================================================*/ @@ -275,20 +244,7 @@ void CInstructionJumpZ::execute(CCPU *cpu) { if (!cpu->getFlagZero()) return; - if (m_addr.empty()) - throw runtime_error("Empty address"); - - const CProgram *progam = cpu->getProgram(); - for(unsigned i = 0; i < progam->size(); i++) - { - if (progam->at(i)->isLabel() && progam->at(i)->getLabelName() == m_addr) - { - cpu->getRegisters()[ 0 ] = i; - return; - } - } - - throw runtime_error("Unknown label '" + m_addr + "'"); + cpu->getRegisters()[ 0 ] = cpu->getProgram()->findLabel(m_addr); } /*============================================================================*/ @@ -307,20 +263,7 @@ void CInstructionJumpS::execute(CCPU *cpu) { if (!cpu->getFlagSign()) return; - if (m_addr.empty()) - throw runtime_error("Empty address"); - - const CProgram *progam = cpu->getProgram(); - for(unsigned i = 0; i < progam->size(); i++) - { - if (progam->at(i)->isLabel() && progam->at(i)->getLabelName() == m_addr) - { - cpu->getRegisters()[ 0 ] = i; - return; - } - } - - throw runtime_error("Unknown label '" + m_addr + "'"); + cpu->getRegisters()[ 0 ] = cpu->getProgram()->findLabel(m_addr); } /*============================================================================*/ diff --git a/ue3/mycpu/instructions.h b/ue3/mycpu/instructions.h index 8c62e0b..0e4d99c 100644 --- a/ue3/mycpu/instructions.h +++ b/ue3/mycpu/instructions.h @@ -274,29 +274,19 @@ class CInstructionLabel { public: CInstructionLabel() - : CInstruction("label"), m_label("") + : CInstruction("label") {} - const bool isLabel() - { - return true; - } - - const std::string getLabelName() - { - return m_label; - } - CInstructionLabel *factory() { return new CInstructionLabel; } - void compile(std::list& params); - void execute(CCPU *cpu); + void compile(std::list& params) + {} - protected: - std::string m_label; + void execute(CCPU *cpu) + {} }; /*============================================================================*/ -- cgit v1.2.3