31 typedef typename SValuePtr::Pointee SValue;
32 typedef typename RegisterStatePtr::element_type RegisterState;
33 typedef typename MemoryStatePtr::element_type MemoryState;
34 typedef typename StatePtr::element_type State;
35 typedef typename RiscOperatorsPtr::element_type RiscOperators;
42 void require(
bool assertion,
const std::string &what_failed) {
44 throw Exception(
"failed assertion: "+what_failed);
47 template<
typename Po
inter>
48 void nonnull(
const Pointer &x,
const std::string &what_failed) {
50 throw Exception(
"must not be null: "+what_failed);
54 template<
class ToPtr,
class FromPtr>
55 void check_type(
const FromPtr &x,
const std::string &what_failed) {
56 typedef typename ToPtr::element_type To;
57 nonnull(x, what_failed);
58 ToPtr y = boost::dynamic_pointer_cast<To>(x);
60 throw Exception(
"wrong pointer type: "+what_failed);
65 nonnull(x, what_failed);
66 SValuePtr y = BaseSemantics::dynamic_pointer_cast<SValue>(x);
68 throw Exception(
"wrong pointer type: "+what_failed);
82 : RegisterState(protoval, regdict) {}
89 : MemoryState(protoval) {}
95 StateSubclass(
const RegisterStatePtr ®isters,
const MemoryStatePtr &memory)
96 : State(registers, memory) {}
105 : RiscOperators(protoval, solver) {}
107 : RiscOperators(state, solver) {}
114 test(ops->protoval(), ops->currentState(), ops);
116 test(ops->protoval(), ops->currentState(), ops);
117 ops->currentState()->memoryState()->set_byteOrder(savedByteOrder);
133 require(v0==NULL,
"default SValue constructor");
136 check_sval_type(SValue::promote(protoval),
"SValue::promote()");
140 check_sval_type(v1,
"SValue::undefined_()");
141 require(v1->nBits()==8,
"SValue::undefined_() width");
145 check_sval_type(v1b,
"SValue::unspecified_()");
146 require(v1b->nBits()==8,
"SValue::unspecified() width");
153 check_sval_type(v2,
"SValue::number_()");
154 require(v2->nBits()==32,
"SValue::number_() width");
158 check_sval_type(v3,
"SValue::boolean_()");
159 require(v3->nBits()==1,
"SValue::boolean_() width");
163 check_sval_type(v4,
"SValue::copy()");
164 require(v4!=v3,
"SValue::copy() should have returned a new object");
165 require(v4->nBits()==1,
"SValue::copy() width");
166 require(v4->isConcrete() == v3->isConcrete(),
"copies should be identical");
167 if (v4->isConcrete())
168 require(v4->toUnsigned().get() == v3->toUnsigned().get(),
"concrete copies should be identical");
169 std::ostringstream v3str, v4str;
172 require(v3str.str() == v4str.str(),
"copies should be identical");
175 require(v3->mayEqual(v3),
"a value may_equal itself");
176 require(v3->mayEqual(v4),
"a value may_equal a copy of itself");
177 require(v4->mayEqual(v3),
"a value may_equal a copy of itself");
180 require(v3->mustEqual(v3),
"a value must_equal itself");
181 require(v3->mustEqual(v4) == v4->mustEqual(v3),
"must_equal should be symmetric");
190 check_type<RegisterStatePtr>(RegisterState::promote(rs1),
"RegisterState::promote()");
193 check_sval_type(rs1v1,
"RegisterState::protoval()");
197 check_type<RegisterStatePtr>(rs3,
"create()");
198 require(rs3->registerDictionary()==regdict,
"RegisterState::create() register dictionary");
199 require(rs3 != rs1,
"RegisterState::create() must return a new object");
201 check_sval_type(rs3v1,
"RegisterState::protoval() after create()");
204 check_type<RegisterStatePtr>(rs4,
"clone()");
205 require(rs4 != rs1,
"RegisterState::clone() must return a new object");
206 require(rs4->registerDictionary()==rs1->registerDictionary(),
207 "RegisterState::clone() must use the register dictionary from the source state");
209 check_sval_type(rs4v1,
"RegisterState::protoval() after clone()");
217 check_type<MemoryStatePtr>(MemoryState::promote(ms1),
"MemoryState::promote()");
220 check_sval_type(ms1v1,
"MemoryState::get_addr_protoval()");
223 check_sval_type(ms1v2,
"MemoryState::get_val_protoval()");
227 require(ms2 != ms1,
"MemoryState::create() must return a new state");
228 check_type<MemoryStatePtr>(ms2,
"MemoryState::create(protoval)");
230 check_sval_type(ms2v1,
"MemoryState::get_addr_protoval() after create");
232 check_sval_type(ms2v2,
"MemoryState::get_val_protoval() after create");
235 require(ms3 != ms1,
"MemoryState::clone must return a new state");
236 check_type<MemoryStatePtr>(ms3,
"MemoryState::clone()");
238 check_sval_type(ms3v1,
"MemoryState::get_addr_protoval() after clone");
240 check_sval_type(ms3v2,
"MemoryState::get_val_protoval() after clone");
247 check_type<StatePtr>(State::promote(state),
"State::promote()");
250 check_sval_type(state_protoval,
"State::protoval()");
254 require(s1 != state,
"State::create() must return a new state");
255 check_type<StatePtr>(s1,
"State::create(regs,mem)");
256 require(s1->registerState()==rs1,
"State::create() must use supplied register state");
257 require(s1->memoryState()==ms1,
"State::create() must use supplied memory state");
260 require(s2 != state,
"State::clone() must return a new state");
261 check_type<StatePtr>(s2,
"State::clone()");
262 require(s2->registerState() != state->registerState(),
263 "State::clone() must deep-copy the register state");
264 require(s2->memoryState() != state->memoryState(),
265 "State::clone() must deep-copy the memory state");
272 check_type<RiscOperatorsPtr>(RiscOperators::promote(ops),
"RiscOperators::promote()");
275 check_sval_type(ops_protoval,
"RiscOperators::protoval()");
279 require(o1 != ops,
"RiscOperators::create(protoval,solver) should return a new object");
280 check_type<RiscOperatorsPtr>(o1,
"RiscOperators::create(protoval,solver)");
283 require(o2 != ops,
"RiscOperators::create(state,solver) should return a new object");
284 check_type<RiscOperatorsPtr>(o2,
"RiscOperators::create(state,solver)");
287 check_type<StatePtr>(ops_orig_state,
"RiscOperators::currentState()");
294 ops->currentState(our_state);
295 require(ops->currentState() == our_state,
"RiscOperators::currentState failed to change state");
297 for (
size_t i=0; i<4; ++i) {
302 v32a = ops->undefined_(32);
303 v32b = ops->undefined_(32);
304 v8 = ops->undefined_(8);
305 v1 = ops->undefined_(1);
308 v32a = ops->undefined_(32);
309 v32b = ops->number_(32, 3);
310 v8 = ops->number_(8, 3);
311 v1 = ops->boolean_(
false);
314 v32a = ops->number_(32, 4);
315 v32b = ops->undefined_(32);
316 v8 = ops->undefined_(8);
317 v1 = ops->undefined_(1);
320 v32a = ops->number_(32, 4);
321 v32b = ops->number_(32, 3);
322 v8 = ops->number_(8, 3);
323 v1 = ops->boolean_(
true);
326 check_sval_type(v32a,
"RiscOperators value constructor");
327 require(v32a->nBits()==32,
"RiscOperators value constructor width");
328 check_sval_type(v32b,
"RiscOperators value constructor");
329 require(v32b->nBits()==32,
"RiscOperators value constructor width");
330 check_sval_type(v8,
"RiscOperators value constructor");
331 require(v8->nBits()==8,
"RiscOperators value constructor width");
332 check_sval_type(v1,
"RiscOperators value constructor");
333 require(v1->nBits()==1,
"RiscOperators value constructor width");
337 check_sval_type(ops_v4,
"RiscOperators::filterCallTarget");
338 require(ops_v4->nBits()==32,
"RiscOperators::filterCallTarget width");
341 check_sval_type(ops_v5,
"RiscOperators::filterReturnTarget");
342 require(ops_v5->nBits()==32,
"RiscOperators::filterReturnTarget width");
345 check_sval_type(ops_v6,
"RiscOperators::filterIndirectJumpTarget");
346 require(ops_v6->nBits()==32,
"RiscOperators::filterIndirectJumpTarget width");
349 check_sval_type(ops_v7,
"RiscOperators::rdtsc");
350 require(ops_v7->nBits()==64,
"RiscOperators::rdtsc width");
353 check_sval_type(ops_v8,
"RiscOperators::and_");
354 require(ops_v8->nBits()==32,
"RiscOperators::and_ width");
357 check_sval_type(ops_v9,
"RiscOperators::or_");
358 require(ops_v9->nBits()==32,
"RiscOperators::or_ width");
361 check_sval_type(ops_v10,
"RiscOperators::xor_");
362 require(ops_v10->nBits()==32,
"RiscOperators::xor_ width");
365 check_sval_type(ops_v11,
"RiscOperators::invert");
366 require(ops_v11->nBits()==32,
"RiscOperators::invert width");
369 check_sval_type(ops_v12,
"RiscOperators::extract");
370 require(ops_v12->nBits()==3,
"RiscOperators::extract width");
373 check_sval_type(ops_v13,
"RiscOperators::concat");
374 require(ops_v13->nBits()==64,
"RiscOperators::concat width");
377 check_sval_type(ops_v14,
"RiscOperators::leastSignificantSetBit");
378 require(ops_v14->nBits()==32,
"RiscOperators::leastSignificantSetBit width");
381 check_sval_type(ops_v15,
"RiscOperators::mostSignificantSetBit");
382 require(ops_v15->nBits()==32,
"RiscOperators::mostSignificantSetBit width");
385 check_sval_type(ops_v16,
"RiscOperators::rotateLeft");
386 require(ops_v16->nBits()==32,
"RiscOperators::rotateLeft width");
389 check_sval_type(ops_v17,
"RiscOperators::rotateRight");
390 require(ops_v17->nBits()==32,
"RiscOperators::rotateRight width");
393 check_sval_type(ops_v18,
"RiscOperators::shiftLeft");
394 require(ops_v18->nBits()==32,
"RiscOperators::shiftLeft width");
397 check_sval_type(ops_v19,
"RiscOperators::shiftRight");
398 require(ops_v19->nBits()==32,
"RiscOperators::shiftRight width");
401 check_sval_type(ops_v20,
"RiscOperators::shiftRightArithmetic");
402 require(ops_v20->nBits()==32,
"RiscOperators::shiftRightArithmetic width");
405 check_sval_type(ops_v21,
"RiscOperators::equalToZero");
406 require(ops_v21->nBits()==1,
"RiscOperators::equalToZero width");
409 check_sval_type(ops_v22,
"RiscOperators::ite");
410 require(ops_v22->nBits()==32,
"RiscOperators::ite width");
414 check_sval_type(ops_v22b,
"RiscOperators::iteWithStatus");
415 require(ops_v22b->nBits() == 32,
"RiscOperators::iteWithStatus width");
418 check_sval_type(ops_v23,
"RiscOperators::unsignedExtend");
419 require(ops_v23->nBits()==32,
"RiscOperators::unsignedExtend width");
422 check_sval_type(ops_v24,
"RiscOperators::unsignedExtend truncate");
423 require(ops_v24->nBits()==8,
"RiscOperators::unsignedExtend truncate width");
426 check_sval_type(ops_v25,
"RiscOperators::signExtend");
427 require(ops_v25->nBits()==32,
"RiscOperators::signExtend width");
430 check_sval_type(ops_v26,
"RiscOperators::add");
431 require(ops_v26->nBits()==32,
"RiscOperators::add width");
435 check_sval_type(ops_v27,
"RiscOperators::addWithCarries");
436 require(ops_v27->nBits()==32,
"RiscOperators::addWithCarries width");
437 check_sval_type(carry_out,
"RiscOperators::addWithCarries carry_out");
438 require(carry_out->nBits()==32,
"RiscOperators::addWithCarries carry_out width");
441 check_sval_type(ops_v28,
"RiscOperators::negate");
442 require(ops_v28->nBits()==32,
"RiscOperators::negate width");
446 check_sval_type(ops_v29,
"RiscOperators::signedDivide");
447 require(ops_v29->nBits()==32,
"RiscOperators::signedDivide width");
454 check_sval_type(ops_v30,
"RiscOperators::signedModulo");
455 require(ops_v30->nBits()==8,
"RiscOperators::signedModulo width");
461 check_sval_type(ops_v31,
"RiscOperators::signedMultiply");
462 require(ops_v31->nBits()==40,
"RiscOperators::signedMultiply width");
466 check_sval_type(ops_v32,
"RiscOperators::unsignedDivide");
467 require(ops_v32->nBits()==32,
"RiscOperators::unsignedDivide width");
474 check_sval_type(ops_v33,
"RiscOperators::unsignedModulo");
475 require(ops_v33->nBits()==8,
"RiscOperators::unsignedModulo width");
481 check_sval_type(ops_v34,
"RiscOperators::unsignedMultiply");
482 require(ops_v34->nBits()==40,
"RiscOperators::unsignedMultiply width");
485 check_sval_type(ops_v35,
"RiscOperators::readRegister");
486 require(ops_v35->nBits()==32,
"RiscOperators::readRegister width");
494 check_sval_type(ops_v36,
"RiscOperators::readMemory byte");
495 require(ops_v36->nBits()==8,
"RiscOperators::readMemory byte width");
499 check_sval_type(ops_v37,
"RiscOperators::readMemory word");
500 require(ops_v37->nBits()==32,
"RiscOperators::readMemory word width");
507 ops->writeMemory(RegisterDescriptor(), v32a, dflt32, v1);
512 ops->currentState(ops_orig_state);