diff options
| author | manuel <manuel@clan-server.at> | 2009-05-13 15:29:51 +0200 |
|---|---|---|
| committer | manuel <manuel@clan-server.at> | 2009-05-13 15:29:51 +0200 |
| commit | 431bbac5a99abbccf33500e22aa353ec792eff94 (patch) | |
| tree | 0649a2833c7ee5f6cbce145b719c104c7efe19df | |
| parent | 89e202f49b9857dcd3627fbc4e0262125d729bbc (diff) | |
| download | ooprog-431bbac5a99abbccf33500e22aa353ec792eff94.tar.gz ooprog-431bbac5a99abbccf33500e22aa353ec792eff94.tar.bz2 ooprog-431bbac5a99abbccf33500e22aa353ec792eff94.zip | |
* adding -Wno-long-long to all Makefiles
* adding execute operator to cinstruction
* added labels map for better performance
| -rw-r--r-- | ue1/imgsynth/Makefile | 2 | ||||
| -rw-r--r-- | ue2/imgsynth2/Makefile | 2 | ||||
| -rw-r--r-- | ue3/mycpu/ccpu.cpp | 2 | ||||
| -rw-r--r-- | ue3/mycpu/ccpu.h | 2 | ||||
| -rw-r--r-- | ue3/mycpu/cinstruction.h | 47 | ||||
| -rw-r--r-- | ue3/mycpu/cprogram.cpp | 21 | ||||
| -rw-r--r-- | ue3/mycpu/cprogram.h | 27 | ||||
| -rw-r--r-- | ue3/mycpu/instructions.cpp | 65 | ||||
| -rw-r--r-- | 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 @@ | |||
| 5 | CC= g++ | 5 | CC= g++ |
| 6 | LD= $(CC) | 6 | LD= $(CC) |
| 7 | DEBUGFLAGS= | 7 | DEBUGFLAGS= |
| 8 | CFLAGS= -O -ansi -pedantic-errors -Wall $(DEBUGFLAGS) | 8 | CFLAGS= -O -ansi -pedantic-errors -Wall -Wno-long-long $(DEBUGFLAGS) |
| 9 | LDFLAGS= | 9 | LDFLAGS= |
| 10 | LIBS= -lboost_program_options | 10 | LIBS= -lboost_program_options |
| 11 | 11 | ||
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++ | |||
| 6 | LD= $(CXX) | 6 | LD= $(CXX) |
| 7 | DEBUGFLAGS= -DNDEBUG | 7 | DEBUGFLAGS= -DNDEBUG |
| 8 | INCLUDE_PATH= -I/usr/local/include | 8 | INCLUDE_PATH= -I/usr/local/include |
| 9 | CXXFLAGS= -O -ansi -pedantic-errors -Wall $(INCLUDE_PATH) $(DEBUGFLAGS) | 9 | CXXFLAGS= -O -ansi -pedantic-errors -Wall -Wno-long-long $(INCLUDE_PATH) $(DEBUGFLAGS) |
| 10 | LDFLAGS= | 10 | LDFLAGS= |
| 11 | LIBS= -L/usr/local/lib -lboost_program_options | 11 | LIBS= -L/usr/local/lib -lboost_program_options |
| 12 | 12 | ||
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() | |||
| 66 | throw runtime_error("Programcounter is out of bound"); | 66 | throw runtime_error("Programcounter is out of bound"); |
| 67 | 67 | ||
| 68 | /* execute instruction */ | 68 | /* execute instruction */ |
| 69 | m_program->at(pc)->execute(this); | 69 | (*m_program->at(pc))(this); |
| 70 | ++m_registers[0]; | 70 | ++m_registers[0]; |
| 71 | } | 71 | } |
| 72 | } | 72 | } |
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 | |||
| 205 | * @param - | 205 | * @param - |
| 206 | * @return - | 206 | * @return - |
| 207 | * @globalvars none | 207 | * @globalvars none |
| 208 | * @exception runtime_error | 208 | * @exception std::runtime_error |
| 209 | * @conditions none | 209 | * @conditions none |
| 210 | */ | 210 | */ |
| 211 | void run(); | 211 | 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 | |||
| 62 | } | 62 | } |
| 63 | 63 | ||
| 64 | /** | 64 | /** |
| 65 | * @method getName | 65 | * @method operator() |
| 66 | * @brief returns instruction name | 66 | * @brief implementation of operator (CCPU) |
| 67 | * @param - | 67 | * @param cpu pointer to cpu |
| 68 | * @return name of instruction | 68 | * @return - |
| 69 | * @globalvars none | ||
| 70 | * @exception none | ||
| 71 | * @conditions none | ||
| 72 | */ | ||
| 73 | virtual const std::string& getName() | ||
| 74 | { | ||
| 75 | return m_name; | ||
| 76 | } | ||
| 77 | |||
| 78 | /** | ||
| 79 | * @method isLabel | ||
| 80 | * @brief returns true if the instruction defines a label | ||
| 81 | * @param - | ||
| 82 | * @return true if the instruction defines a label | ||
| 83 | * @globalvars none | 69 | * @globalvars none |
| 84 | * @exception none | 70 | * @exception std::runtime_error |
| 85 | * @conditions none | 71 | * @conditions none |
| 86 | */ | 72 | */ |
| 87 | virtual const bool isLabel() | 73 | virtual CInstruction& operator()(CCPU *cpu) |
| 88 | { | 74 | { |
| 89 | return false; | 75 | execute(cpu); |
| 76 | return *this; | ||
| 90 | } | 77 | } |
| 91 | 78 | ||
| 92 | /** | 79 | /** |
| 93 | * @method getLabelName | 80 | * @method getName |
| 94 | * @brief returns labelname if the instruction defines a label | 81 | * @brief returns instruction name |
| 95 | * @param - | 82 | * @param - |
| 96 | * @return labelname if the instruction defines a label | 83 | * @return name of instruction |
| 97 | * @globalvars none | 84 | * @globalvars none |
| 98 | * @exception none | 85 | * @exception none |
| 99 | * @conditions none | 86 | * @conditions none |
| 100 | */ | 87 | */ |
| 101 | virtual const std::string getLabelName() | 88 | virtual const std::string& getName() |
| 102 | { | 89 | { |
| 103 | return ""; | 90 | return m_name; |
| 104 | } | 91 | } |
| 105 | 92 | ||
| 106 | /** | 93 | /** |
| @@ -139,7 +126,7 @@ class CInstruction | |||
| 139 | * @param str register in assembler syntax | 126 | * @param str register in assembler syntax |
| 140 | * @return registernumber | 127 | * @return registernumber |
| 141 | * @globalvars none | 128 | * @globalvars none |
| 142 | * @exception runtime_error | 129 | * @exception std::runtime_error |
| 143 | * @conditions none | 130 | * @conditions none |
| 144 | */ | 131 | */ |
| 145 | virtual const unsigned parseRegister(const std::string& str); | 132 | virtual const unsigned parseRegister(const std::string& str); |
| @@ -152,7 +139,7 @@ class CInstruction | |||
| 152 | * @param regidx registernumber | 139 | * @param regidx registernumber |
| 153 | * @return - | 140 | * @return - |
| 154 | * @globalvars none | 141 | * @globalvars none |
| 155 | * @exception runtime_error | 142 | * @exception std::runtime_error |
| 156 | * @conditions none | 143 | * @conditions none |
| 157 | */ | 144 | */ |
| 158 | virtual void checkRegister(CCPU *cpu, const unsigned regidx); | 145 | virtual void checkRegister(CCPU *cpu, const unsigned regidx); |
| @@ -175,7 +162,7 @@ class CInstruction | |||
| 175 | * @param params list of parameters of this instruction | 162 | * @param params list of parameters of this instruction |
| 176 | * @return - | 163 | * @return - |
| 177 | * @globalvars none | 164 | * @globalvars none |
| 178 | * @exception runtime_error | 165 | * @exception std::runtime_error |
| 179 | * @conditions none | 166 | * @conditions none |
| 180 | */ | 167 | */ |
| 181 | virtual void compile(std::list<std::string>& params) = 0; | 168 | virtual void compile(std::list<std::string>& params) = 0; |
| @@ -186,7 +173,7 @@ class CInstruction | |||
| 186 | * @param cpu pointer to cpu | 173 | * @param cpu pointer to cpu |
| 187 | * @return - | 174 | * @return - |
| 188 | * @globalvars none | 175 | * @globalvars none |
| 189 | * @exception runtime_error | 176 | * @exception std::runtime_error |
| 190 | * @conditions none | 177 | * @conditions none |
| 191 | */ | 178 | */ |
| 192 | virtual void execute(CCPU *cpu) = 0; | 179 | 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) | |||
| 107 | /* let instruction parse the parameters. catch+throw exception */ | 107 | /* let instruction parse the parameters. catch+throw exception */ |
| 108 | try | 108 | try |
| 109 | { | 109 | { |
| 110 | /* handle label instruction ourself, but still add a dummy instruction */ | ||
| 111 | if (instrname == "label") | ||
| 112 | { | ||
| 113 | if (instrparams.size() != 1) | ||
| 114 | throw runtime_error("Invalid paramater count - must be 1"); | ||
| 115 | string label = instrparams.front(); | ||
| 116 | if (label.length() < 2 || label[ label.length() - 1] != ':') | ||
| 117 | throw runtime_error("Label has invalid syntax"); | ||
| 118 | m_labels[ label.substr(0, label.length() - 1) ] = size(); | ||
| 119 | } | ||
| 110 | instr->compile(instrparams); | 120 | instr->compile(instrparams); |
| 111 | } | 121 | } |
| 112 | catch(runtime_error& ex) | 122 | catch(runtime_error& ex) |
| @@ -123,6 +133,17 @@ void CProgram::compile(std::istream& in) | |||
| 123 | 133 | ||
| 124 | /*----------------------------------------------------------------------------*/ | 134 | /*----------------------------------------------------------------------------*/ |
| 125 | 135 | ||
| 136 | unsigned CProgram::findLabel(const std::string& label) const | ||
| 137 | { | ||
| 138 | map<string, unsigned>::const_iterator it; | ||
| 139 | it = m_labels.find(label); | ||
| 140 | if (it == m_labels.end()) | ||
| 141 | throw runtime_error("Unknown label '" + label + "'"); | ||
| 142 | return it->second; | ||
| 143 | } | ||
| 144 | |||
| 145 | /*----------------------------------------------------------------------------*/ | ||
| 146 | |||
| 126 | #if DEBUG | 147 | #if DEBUG |
| 127 | void CProgram::dump(std::ostream& out) | 148 | void CProgram::dump(std::ostream& out) |
| 128 | { | 149 | { |
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 @@ | |||
| 10 | 10 | ||
| 11 | #include <vector> | 11 | #include <vector> |
| 12 | #include <set> | 12 | #include <set> |
| 13 | #include <map> | ||
| 13 | #include "cinstruction.h" | 14 | #include "cinstruction.h" |
| 14 | 15 | ||
| 15 | /** | 16 | /** |
| @@ -44,6 +45,31 @@ class CProgram | |||
| 44 | ~CProgram(); | 45 | ~CProgram(); |
| 45 | 46 | ||
| 46 | /** | 47 | /** |
| 48 | * @method getLabels | ||
| 49 | * @brief get reference to labels map | ||
| 50 | * @param - | ||
| 51 | * @return reference to labels map | ||
| 52 | * @globalvars none | ||
| 53 | * @exception none | ||
| 54 | * @conditions none | ||
| 55 | */ | ||
| 56 | const std::map<std::string, unsigned>& getLabels() const | ||
| 57 | { | ||
| 58 | return m_labels; | ||
| 59 | } | ||
| 60 | |||
| 61 | /** | ||
| 62 | * @method findLabel | ||
| 63 | * @brief search for label | ||
| 64 | * @param label name of label to search for | ||
| 65 | * @return index of found label in program | ||
| 66 | * @globalvars none | ||
| 67 | * @exception std::runtime_error | ||
| 68 | * @conditions none | ||
| 69 | */ | ||
| 70 | unsigned findLabel(const std::string& label) const; | ||
| 71 | |||
| 72 | /** | ||
| 47 | * @method compile | 73 | * @method compile |
| 48 | * @brief create instructions from parsing stream | 74 | * @brief create instructions from parsing stream |
| 49 | * @param in inputstream to read from | 75 | * @param in inputstream to read from |
| @@ -71,6 +97,7 @@ class CProgram | |||
| 71 | /* members */ | 97 | /* members */ |
| 72 | /** set of known instructions */ | 98 | /** set of known instructions */ |
| 73 | std::set<CInstruction *> m_instrset; | 99 | std::set<CInstruction *> m_instrset; |
| 100 | std::map<std::string, unsigned> m_labels; | ||
| 74 | }; | 101 | }; |
| 75 | 102 | ||
| 76 | #endif | 103 | #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 @@ | |||
| 5 | * @date 10.05.2009 | 5 | * @date 10.05.2009 |
| 6 | */ | 6 | */ |
| 7 | 7 | ||
| 8 | #include <map> | ||
| 8 | #include "instructions.h" | 9 | #include "instructions.h" |
| 9 | 10 | ||
| 10 | using namespace std; | 11 | using namespace std; |
| @@ -210,27 +211,6 @@ void CInstructionTest::execute(CCPU *cpu) | |||
| 210 | 211 | ||
| 211 | /*============================================================================*/ | 212 | /*============================================================================*/ |
| 212 | 213 | ||
| 213 | void CInstructionLabel::compile(std::list<std::string>& params) | ||
| 214 | { | ||
| 215 | if (params.size() != 1) | ||
| 216 | throw runtime_error("Invalid paramater count - must be 1"); | ||
| 217 | string label = params.front(); | ||
| 218 | params.pop_front(); | ||
| 219 | if (label.length() < 2 || label[ label.length() - 1] != ':') | ||
| 220 | throw runtime_error("Label has invalid syntax"); | ||
| 221 | m_label = label.substr(0, label.length() - 1); | ||
| 222 | } | ||
| 223 | |||
| 224 | /*----------------------------------------------------------------------------*/ | ||
| 225 | |||
| 226 | void CInstructionLabel::execute(CCPU *cpu) | ||
| 227 | { | ||
| 228 | if (m_label.empty()) | ||
| 229 | throw runtime_error("Empty label"); | ||
| 230 | } | ||
| 231 | |||
| 232 | /*============================================================================*/ | ||
| 233 | |||
| 234 | void CInstructionJumpA::compile(std::list<std::string>& params) | 214 | void CInstructionJumpA::compile(std::list<std::string>& params) |
| 235 | { | 215 | { |
| 236 | if (params.size() != 1) | 216 | if (params.size() != 1) |
| @@ -245,18 +225,7 @@ void CInstructionJumpA::execute(CCPU *cpu) | |||
| 245 | { | 225 | { |
| 246 | if (m_addr.empty()) | 226 | if (m_addr.empty()) |
| 247 | throw runtime_error("Empty address"); | 227 | throw runtime_error("Empty address"); |
| 248 | 228 | cpu->getRegisters()[ 0 ] = cpu->getProgram()->findLabel(m_addr); | |
| 249 | const CProgram *progam = cpu->getProgram(); | ||
| 250 | for(unsigned i = 0; i < progam->size(); i++) | ||
| 251 | { | ||
| 252 | if (progam->at(i)->isLabel() && progam->at(i)->getLabelName() == m_addr) | ||
| 253 | { | ||
| 254 | cpu->getRegisters()[ 0 ] = i; | ||
| 255 | return; | ||
| 256 | } | ||
| 257 | } | ||
| 258 | |||
| 259 | throw runtime_error("Unknown label '" + m_addr + "'"); | ||
| 260 | } | 229 | } |
| 261 | 230 | ||
| 262 | /*============================================================================*/ | 231 | /*============================================================================*/ |
| @@ -275,20 +244,7 @@ void CInstructionJumpZ::execute(CCPU *cpu) | |||
| 275 | { | 244 | { |
| 276 | if (!cpu->getFlagZero()) | 245 | if (!cpu->getFlagZero()) |
| 277 | return; | 246 | return; |
| 278 | if (m_addr.empty()) | 247 | cpu->getRegisters()[ 0 ] = cpu->getProgram()->findLabel(m_addr); |
| 279 | throw runtime_error("Empty address"); | ||
| 280 | |||
| 281 | const CProgram *progam = cpu->getProgram(); | ||
| 282 | for(unsigned i = 0; i < progam->size(); i++) | ||
| 283 | { | ||
| 284 | if (progam->at(i)->isLabel() && progam->at(i)->getLabelName() == m_addr) | ||
| 285 | { | ||
| 286 | cpu->getRegisters()[ 0 ] = i; | ||
| 287 | return; | ||
| 288 | } | ||
| 289 | } | ||
| 290 | |||
| 291 | throw runtime_error("Unknown label '" + m_addr + "'"); | ||
| 292 | } | 248 | } |
| 293 | 249 | ||
| 294 | /*============================================================================*/ | 250 | /*============================================================================*/ |
| @@ -307,20 +263,7 @@ void CInstructionJumpS::execute(CCPU *cpu) | |||
| 307 | { | 263 | { |
| 308 | if (!cpu->getFlagSign()) | 264 | if (!cpu->getFlagSign()) |
| 309 | return; | 265 | return; |
| 310 | if (m_addr.empty()) | 266 | cpu->getRegisters()[ 0 ] = cpu->getProgram()->findLabel(m_addr); |
| 311 | throw runtime_error("Empty address"); | ||
| 312 | |||
| 313 | const CProgram *progam = cpu->getProgram(); | ||
| 314 | for(unsigned i = 0; i < progam->size(); i++) | ||
| 315 | { | ||
| 316 | if (progam->at(i)->isLabel() && progam->at(i)->getLabelName() == m_addr) | ||
| 317 | { | ||
| 318 | cpu->getRegisters()[ 0 ] = i; | ||
| 319 | return; | ||
| 320 | } | ||
| 321 | } | ||
| 322 | |||
| 323 | throw runtime_error("Unknown label '" + m_addr + "'"); | ||
| 324 | } | 267 | } |
| 325 | 268 | ||
| 326 | /*============================================================================*/ | 269 | /*============================================================================*/ |
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 | |||
| 274 | { | 274 | { |
| 275 | public: | 275 | public: |
| 276 | CInstructionLabel() | 276 | CInstructionLabel() |
| 277 | : CInstruction("label"), m_label("") | 277 | : CInstruction("label") |
| 278 | {} | 278 | {} |
| 279 | 279 | ||
| 280 | const bool isLabel() | ||
| 281 | { | ||
| 282 | return true; | ||
| 283 | } | ||
| 284 | |||
| 285 | const std::string getLabelName() | ||
| 286 | { | ||
| 287 | return m_label; | ||
| 288 | } | ||
| 289 | |||
| 290 | CInstructionLabel *factory() | 280 | CInstructionLabel *factory() |
| 291 | { | 281 | { |
| 292 | return new CInstructionLabel; | 282 | return new CInstructionLabel; |
| 293 | } | 283 | } |
| 294 | 284 | ||
| 295 | void compile(std::list<std::string>& params); | 285 | void compile(std::list<std::string>& params) |
| 296 | void execute(CCPU *cpu); | 286 | {} |
| 297 | 287 | ||
| 298 | protected: | 288 | void execute(CCPU *cpu) |
| 299 | std::string m_label; | 289 | {} |
| 300 | }; | 290 | }; |
| 301 | 291 | ||
| 302 | /*============================================================================*/ | 292 | /*============================================================================*/ |
