00001 #ifndef CMA_OPERATIONS_DEFINED
00002 #define CMA_OPERATIONS_DEFINED
00003 #ifndef CMA_INTERPRETER_DEFINED
00004 #error operations.h must be included into the file interpreter.h
00005 #endif
00006
00118
00119 inline void inc(const Int& newSP)
00120 {
00121 ++heap.top();
00122 }
00123
00124 inline void dec(const Int& newSP)
00125 {
00126 --heap.top();
00127 }
00128
00129 inline void unary_minus(const Int& newSP)
00130 {
00131 heap.top() =- heap.top();
00132 }
00133
00134 inline void mul(const Int& newSP)
00135 {
00136 heap[newSP] *= heap.top();
00137 heap.setSP(newSP);
00138 }
00139
00140 inline void add(const Int& newSP)
00141 {
00142 heap[newSP] += heap.top();
00143 heap.setSP(newSP);
00144 }
00145
00146 inline void sub(const Int& newSP)
00147 {
00148 heap[newSP] -= heap.top();
00149 heap.setSP(newSP);
00150 }
00151
00152 inline void div(const Int& newSP)
00153 {
00154 heap[newSP] /= heap.top();
00155 heap.setSP(newSP);
00156 }
00157
00158 inline void mod(const Int& newSP)
00159 {
00160 heap[newSP] %= heap.top();
00161 heap.setSP(newSP);
00162 }
00163
00164
00165 inline void log_neg(const Int& newSP)
00166 {
00167 heap.top() = !heap.top();
00168 }
00169
00170 inline void log_and(const Int& newSP)
00171 {
00172 heap[newSP] = heap[newSP] && heap.top();
00173 heap.setSP(newSP);
00174 }
00175
00176 inline void log_or(const Int& newSP)
00177 {
00178 heap[newSP] = heap[newSP] || heap.top();
00179 heap.setSP(newSP);
00180 }
00181
00182
00183 inline void eq(const Int& newSP)
00184 {
00185 heap[newSP] = (Word)(heap[newSP] == heap.top());
00186 heap.setSP(newSP);
00187 }
00188
00189 inline void neq(const Int& newSP)
00190 {
00191 heap[newSP] = (Word)(heap[newSP] != heap.top());
00192 heap.setSP(newSP);
00193 }
00194
00195 inline void le(const Int& newSP)
00196 {
00197 heap[newSP] = (Word)(heap[newSP] < heap.top());
00198 heap.setSP(newSP);
00199 }
00200
00201 inline void leq(const Int& newSP)
00202 {
00203 heap[newSP] = (Word)(heap[newSP] <= heap.top());
00204 heap.setSP(newSP);
00205 }
00206
00207 inline void ge(const Int& newSP)
00208 {
00209 heap[newSP] = (Word)(heap[newSP] > heap.top());
00210 heap.setSP(newSP);
00211 }
00212
00213 inline void geq(const Int& newSP)
00214 {
00215 heap[newSP] = (Word)(heap[newSP] >= heap.top());
00216 heap.setSP(newSP);
00217 }
00218
00219
00220
00221 inline void bin_complement(const Int& newSP)
00222 {
00223 heap.top() = ~heap.top();
00224 }
00225
00226 inline void bin_and(const Int& newSP)
00227 {
00228 heap[newSP] &= heap.top();
00229 heap.setSP(newSP);
00230 }
00231
00232 inline void bin_or(const Int& newSP)
00233 {
00234 heap[newSP] |= heap.top();
00235 heap.setSP(newSP);
00236 }
00237
00238 inline void bin_xor(const Int& newSP)
00239 {
00240 heap[newSP] ^= heap.top();
00241 heap.setSP(newSP);
00242 }
00243
00244 inline void shift_left(const Int& newSP)
00245 {
00246
00247 heap[newSP]<<=heap.top();
00248 heap.setSP(newSP);
00249 }
00250
00251 inline void shift_right(const Int& newSP)
00252 {
00253
00254 heap[newSP]>>=heap.top();
00255 heap.setSP(newSP);
00256 }
00257
00258
00259 inline void jump(const Int& newSP, const Int& addr)
00260 {
00261 if(!code.verifyAddress(addr)) throw Error(Error::WRONG_JUMP_ADDRESS);
00262 PC = addr;
00263 }
00264
00265 inline void jumpz(const Int& newSP, const Int& addr)
00266 {
00267 if(heap.top()==(Word)0)
00268 {
00269 if(!code.verifyAddress(addr)) throw Error(Error::WRONG_JUMP_ADDRESS);
00270 PC = addr;
00271 }
00272 heap.setSP(newSP);
00273 }
00274
00275 inline void jumpi(const Int& newSP, const Int& addr)
00276 {
00277 if(!code.verifyAddress(addr+(Int)heap.top())) throw Error(Error::WRONG_JUMP_ADDRESS);
00278 PC = addr + (Int)heap.top();
00279 heap.setSP(newSP);
00280 }
00281
00282 inline void jumpzs(const Int& newSP, const Int& addr)
00283 {
00284 if(heap.top()==(Word)0)
00285 {
00286 if(!code.verifyAddress(addr)) throw Error(Error::WRONG_JUMP_ADDRESS);
00287 PC = addr;
00288 }
00289 }
00290
00291 inline void jumpnzs(const Int& newSP, const Int& addr)
00292 {
00293 if(heap.top()!=(Word)0)
00294 {
00295 if(!code.verifyAddress(addr)) throw Error(Error::WRONG_JUMP_ADDRESS);
00296 PC = addr;
00297 }
00298 }
00299
00300
00301 inline void push(const Int& newSP, const Word& value)
00302 {
00303 heap.setSP(newSP);
00304 heap[newSP] = value;
00305 }
00306
00307 inline void pop(const Int& newSP)
00308 {
00309 heap.setSP(newSP);
00310 }
00311
00312 inline void load(const Int& newSP)
00313 {
00314 if(heap[newSP]==(Word)0) throw Error(Error::READ_FROM_NULL_POINTER);
00315 if(!heap.verifyAddress((Int)heap[newSP])) throw Error(Error::WRONG_HEAP_ADDRESS);
00316 heap[newSP] = heap[(Int)heap[newSP]];
00317 }
00318
00319 inline void store(const Int& newSP)
00320 {
00321 if(heap.top()==(Word)0) throw Error(Error::WRITE_TO_NULL_POINTER);
00322 if(!heap.verifyAddress((Int)heap.top())) throw Error(Error::WRONG_HEAP_ADDRESS);
00323 heap[(Int)heap.top()] = heap[newSP];
00324 heap.setSP(newSP);
00325 }
00326
00327 inline void loada(const Int& newSP, const Int& addr)
00328 {
00329 if(addr==(Int)0) throw Error(Error::READ_FROM_NULL_POINTER);
00330 if(!heap.verifyAddress(addr)) throw Error(Error::WRONG_HEAP_ADDRESS);
00331 heap.setSP(newSP);
00332 heap[newSP] = heap[addr];
00333 }
00334
00335 inline void storea(const Int& newSP, const Int& addr)
00336 {
00337 if(addr==(Int)0) throw Error(Error::WRITE_TO_NULL_POINTER);
00338 if(!heap.verifyAddress(addr)) throw Error(Error::WRONG_HEAP_ADDRESS);
00339 heap[addr] = heap.top();
00340 heap.setSP(newSP);
00341 }
00342
00343 inline void loadrc(const Int& newSP,const Int& value)
00344 {
00345 heap.setSP(newSP);
00346 heap[newSP] =(Word)(FP + value);
00347 }
00348
00349 inline void loadr(const Int& newSP, const Int& addr)
00350 {
00351 if(addr==(Int)0) throw Error(Error::READ_FROM_NULL_POINTER);
00352 if(addr==(Int)-1 || addr==(Int)-2) throw Error(Error::READ_FROM_STACK_FRAME);
00353 if(!heap.verifyAddress(FP+addr)) throw Error(Error::WRONG_HEAP_ADDRESS);
00354 heap.setSP(newSP);
00355 heap[newSP] = heap[FP+addr];
00356 }
00357
00358 inline void storer(const Int& newSP, const Int& addr)
00359 {
00360 if(addr==(Int)0) throw Error(Error::WRITE_TO_NULL_POINTER);
00361 if(addr==(Int)-1 || addr==(Int)-2) throw Error(Error::WRITE_TO_STACK_FRAME);
00362 if(!heap.verifyAddress(FP+addr)) throw Error(Error::WRONG_HEAP_ADDRESS);
00363 heap[FP+addr] = heap.top();
00364 heap.setSP(newSP);
00365 }
00366
00367 inline void dup(const Int& newSP)
00368 {
00369 heap.setSP(newSP);
00370 heap[newSP] = heap[newSP-(Int)1];
00371 }
00372
00373 inline void move(const Int& newSP, const Int& number)
00374 {
00375 Int oldSP = heap.getSP();
00376 Int addr = (Int)heap.top();
00377 if(!heap.verifyInterval(addr, addr+number-(Int)1)) throw Error(Error::WRONG_HEAP_INTERVALL);
00378
00379 for(Int i=number-(Int)1;(Int)0<=i;--i) heap[oldSP+i] = heap[addr+i];
00380 }
00381
00382
00383 inline void alloc_memory(const Int& newSP)
00384 {
00385 if(heap.top()==(Word)0) return;
00386
00387 heap.top() = freeStore.malloc((Int)heap.top());
00388 }
00389
00390 inline void free_memory(const Int& newSP)
00391 {
00392 heap.setSP(newSP);
00393 }
00394
00395
00396 inline void mark(const Int& newSP)
00397 {
00398 heap.setSP(newSP);
00399 heap.top(1) = (Word)FP;
00400 heap.top(2) = (Word)heap.getEP();
00401 }
00402
00403 inline void call(const Int& newSP, const Int& value)
00404 {
00405 if(newSP<= value) throw Error(Error::CALL_UNDERFLOW);
00406 if(!code.verifyAddress((Int)heap.top())) throw Error(Error::WRONG_CALL_ADDRESS);
00407 FP = newSP - value;
00408 heap[FP] = (Word)PC;
00409 PC = (Int) heap.top();
00410 heap.setSP(newSP);
00411 }
00412
00413 inline void enter(const Int& newSP, const Int& value)
00414 {
00415 if(!heap.verifyEP(value+heap.getSP())) throw Error(Error::STACK_OVERFLOW);
00416 heap.setEP(value+heap.getSP());
00417 }
00418
00419 inline void alloc_stack(const Int& newSP)
00420 {
00421 heap.setSP(newSP);
00422 }
00423
00424 inline void return_value(const Int& newSP)
00425 {
00426 if(!heap.verifyAddress(FP)) throw Error(Error::CORRUPTED_FRAME);
00427 if(!code.verifyAddress((Int)heap[FP])) throw Error(Error::CORRUPTED_FRAME);
00428 if(!heap.verifyAddress(FP-(Int)2)) throw Error(Error::CORRUPTED_FRAME);
00429 if(!heap.verifyAddress(FP-(Int)1)) throw Error(Error::CORRUPTED_FRAME);
00430 if(!heap.verifyEP((Int)heap[FP-(Int)2])) throw Error(Error::STACK_OVERFLOW);
00431 if(!heap.verifyFP((Int)heap[FP-(Int)1],FP-(Int)3)) throw Error(Error::CORRUPTED_FRAME);
00432 PC = (Int)heap[FP];
00433 FP = (Int)heap[FP-(Int)1];
00434 heap.setSP(FP-(Int)3);
00435 }
00436
00437 void halt_machine(const Int& newSP, Control<Word>* pControl)
00438 {
00439
00440 if(heap.getSP()!=(Int)1) throw Error("Wrong stack state on HALT");
00441
00442 freeStore.verifyHalt();
00443 pControl->onComplete(heap[(Int)1]);
00444 freeStore.deInit();
00445 heap.deInit();
00446 code.deInit();
00447 }
00448
00449
00450 inline void write_int(const Int& newSP, Control<Word>* pControl)
00451 {
00452 pControl->onOutput_int(heap.top());
00453 heap.setSP(newSP);
00454 }
00455
00456 void read_int(const Int& newSP, Control<Word>* pControl)
00457 {
00458 heap.setSP(newSP);
00459 heap[newSP] = pControl->onInput();
00460 }
00461
00462 void write_char(const Int& newSP, Control<Word>* pControl, const Word& outchar)
00463 {
00464 pControl->onOutput_char(outchar);
00465 }
00466
00467 #endif