32 typedef typename SValuePtr::Pointee SValue;
33 typedef typename RegisterStatePtr::element_type RegisterState;
34 typedef typename MemoryStatePtr::element_type MemoryState;
35 typedef typename StatePtr::element_type State;
36 typedef typename RiscOperatorsPtr::element_type RiscOperators;
43 void require(
bool assertion,
const std::string &what_failed) {
45 throw Exception(
"failed assertion: "+what_failed);
48 template<
typename Po
inter>
49 void nonnull(
const Pointer &x,
const std::string &what_failed) {
51 throw Exception(
"must not be null: "+what_failed);
55 template<
class ToPtr,
class FromPtr>
56 void check_type(
const FromPtr &x,
const std::string &what_failed) {
57 typedef typename ToPtr::element_type To;
58 nonnull(x, what_failed);
61 throw Exception(
"wrong pointer type: "+what_failed);
66 nonnull(x, what_failed);
67 SValuePtr y = as<SValue>(x);
69 throw Exception(
"wrong pointer type: "+what_failed);
83 : RegisterState(protoval, regdict) {}
90 : MemoryState(protoval) {}
96 StateSubclass(
const RegisterStatePtr ®isters,
const MemoryStatePtr &memory)
97 : State(registers, memory) {}
106 : RiscOperators(protoval, solver) {}
108 : RiscOperators(state, solver) {}
115 test(ops->protoval(), ops->currentState(), ops);
117 test(ops->protoval(), ops->currentState(), ops);
118 ops->currentState()->memoryState()->set_byteOrder(savedByteOrder);
134 require(v0==NULL,
"default SValue constructor");
137 check_sval_type(SValue::promote(protoval),
"SValue::promote()");
141 check_sval_type(v1,
"SValue::undefined_()");
142 require(v1->nBits()==8,
"SValue::undefined_() width");
146 check_sval_type(v1b,
"SValue::unspecified_()");
147 require(v1b->nBits()==8,
"SValue::unspecified() width");
154 check_sval_type(v2,
"SValue::number_()");
155 require(v2->nBits()==32,
"SValue::number_() width");
159 check_sval_type(v3,
"SValue::boolean_()");
160 require(v3->nBits()==1,
"SValue::boolean_() width");
164 check_sval_type(v4,
"SValue::copy()");
165 require(v4!=v3,
"SValue::copy() should have returned a new object");
166 require(v4->nBits()==1,
"SValue::copy() width");
167 require(v4->isConcrete() == v3->isConcrete(),
"copies should be identical");
168 if (v4->isConcrete())
169 require(v4->toUnsigned().get() == v3->toUnsigned().get(),
"concrete copies should be identical");
170 std::ostringstream v3str, v4str;
173 require(v3str.str() == v4str.str(),
"copies should be identical");
176 require(v3->mayEqual(v3),
"a value may_equal itself");
177 require(v3->mayEqual(v4),
"a value may_equal a copy of itself");
178 require(v4->mayEqual(v3),
"a value may_equal a copy of itself");
181 require(v3->mustEqual(v3),
"a value must_equal itself");
182 require(v3->mustEqual(v4) == v4->mustEqual(v3),
"must_equal should be symmetric");
191 check_type<RegisterStatePtr>(RegisterState::promote(rs1),
"RegisterState::promote()");
194 check_sval_type(rs1v1,
"RegisterState::protoval()");
198 check_type<RegisterStatePtr>(rs3,
"create()");
199 require(rs3->registerDictionary()==regdict,
"RegisterState::create() register dictionary");
200 require(rs3 != rs1,
"RegisterState::create() must return a new object");
202 check_sval_type(rs3v1,
"RegisterState::protoval() after create()");
205 check_type<RegisterStatePtr>(rs4,
"clone()");
206 require(rs4 != rs1,
"RegisterState::clone() must return a new object");
207 require(rs4->registerDictionary()==rs1->registerDictionary(),
208 "RegisterState::clone() must use the register dictionary from the source state");
210 check_sval_type(rs4v1,
"RegisterState::protoval() after clone()");
218 check_type<MemoryStatePtr>(MemoryState::promote(ms1),
"MemoryState::promote()");
221 check_sval_type(ms1v1,
"MemoryState::get_addr_protoval()");
224 check_sval_type(ms1v2,
"MemoryState::get_val_protoval()");
228 require(ms2 != ms1,
"MemoryState::create() must return a new state");
229 check_type<MemoryStatePtr>(ms2,
"MemoryState::create(protoval)");
231 check_sval_type(ms2v1,
"MemoryState::get_addr_protoval() after create");
233 check_sval_type(ms2v2,
"MemoryState::get_val_protoval() after create");
236 require(ms3 != ms1,
"MemoryState::clone must return a new state");
237 check_type<MemoryStatePtr>(ms3,
"MemoryState::clone()");
239 check_sval_type(ms3v1,
"MemoryState::get_addr_protoval() after clone");
241 check_sval_type(ms3v2,
"MemoryState::get_val_protoval() after clone");
248 check_type<StatePtr>(State::promote(state),
"State::promote()");
251 check_sval_type(state_protoval,
"State::protoval()");
255 require(s1 != state,
"State::create() must return a new state");
256 check_type<StatePtr>(s1,
"State::create(regs,mem)");
257 require(s1->registerState()==rs1,
"State::create() must use supplied register state");
258 require(s1->memoryState()==ms1,
"State::create() must use supplied memory state");
261 require(s2 != state,
"State::clone() must return a new state");
262 check_type<StatePtr>(s2,
"State::clone()");
263 require(s2->registerState() != state->registerState(),
264 "State::clone() must deep-copy the register state");
265 require(s2->memoryState() != state->memoryState(),
266 "State::clone() must deep-copy the memory state");
273 check_type<RiscOperatorsPtr>(RiscOperators::promote(ops),
"RiscOperators::promote()");
276 check_sval_type(ops_protoval,
"RiscOperators::protoval()");
280 require(o1 != ops,
"RiscOperators::create(protoval,solver) should return a new object");
281 check_type<RiscOperatorsPtr>(o1,
"RiscOperators::create(protoval,solver)");
284 require(o2 != ops,
"RiscOperators::create(state,solver) should return a new object");
285 check_type<RiscOperatorsPtr>(o2,
"RiscOperators::create(state,solver)");
288 check_type<StatePtr>(ops_orig_state,
"RiscOperators::currentState()");
295 ops->currentState(our_state);
296 require(ops->currentState() == our_state,
"RiscOperators::currentState failed to change state");
298 for (
size_t i=0; i<4; ++i) {
303 v32a = ops->undefined_(32);
304 v32b = ops->undefined_(32);
305 v8 = ops->undefined_(8);
306 v1 = ops->undefined_(1);
309 v32a = ops->undefined_(32);
310 v32b = ops->number_(32, 3);
311 v8 = ops->number_(8, 3);
312 v1 = ops->boolean_(
false);
315 v32a = ops->number_(32, 4);
316 v32b = ops->undefined_(32);
317 v8 = ops->undefined_(8);
318 v1 = ops->undefined_(1);
321 v32a = ops->number_(32, 4);
322 v32b = ops->number_(32, 3);
323 v8 = ops->number_(8, 3);
324 v1 = ops->boolean_(
true);
327 check_sval_type(v32a,
"RiscOperators value constructor");
328 require(v32a->nBits()==32,
"RiscOperators value constructor width");
329 check_sval_type(v32b,
"RiscOperators value constructor");
330 require(v32b->nBits()==32,
"RiscOperators value constructor width");
331 check_sval_type(v8,
"RiscOperators value constructor");
332 require(v8->nBits()==8,
"RiscOperators value constructor width");
333 check_sval_type(v1,
"RiscOperators value constructor");
334 require(v1->nBits()==1,
"RiscOperators value constructor width");
338 check_sval_type(ops_v4,
"RiscOperators::filterCallTarget");
339 require(ops_v4->nBits()==32,
"RiscOperators::filterCallTarget width");
342 check_sval_type(ops_v5,
"RiscOperators::filterReturnTarget");
343 require(ops_v5->nBits()==32,
"RiscOperators::filterReturnTarget width");
346 check_sval_type(ops_v6,
"RiscOperators::filterIndirectJumpTarget");
347 require(ops_v6->nBits()==32,
"RiscOperators::filterIndirectJumpTarget width");
350 check_sval_type(ops_v7,
"RiscOperators::rdtsc");
351 require(ops_v7->nBits()==64,
"RiscOperators::rdtsc width");
354 check_sval_type(ops_v8,
"RiscOperators::and_");
355 require(ops_v8->nBits()==32,
"RiscOperators::and_ width");
358 check_sval_type(ops_v9,
"RiscOperators::or_");
359 require(ops_v9->nBits()==32,
"RiscOperators::or_ width");
362 check_sval_type(ops_v10,
"RiscOperators::xor_");
363 require(ops_v10->nBits()==32,
"RiscOperators::xor_ width");
366 check_sval_type(ops_v11,
"RiscOperators::invert");
367 require(ops_v11->nBits()==32,
"RiscOperators::invert width");
370 check_sval_type(ops_v12,
"RiscOperators::extract");
371 require(ops_v12->nBits()==3,
"RiscOperators::extract width");
374 check_sval_type(ops_v13,
"RiscOperators::concat");
375 require(ops_v13->nBits()==64,
"RiscOperators::concat width");
378 check_sval_type(ops_v14,
"RiscOperators::leastSignificantSetBit");
379 require(ops_v14->nBits()==32,
"RiscOperators::leastSignificantSetBit width");
382 check_sval_type(ops_v15,
"RiscOperators::mostSignificantSetBit");
383 require(ops_v15->nBits()==32,
"RiscOperators::mostSignificantSetBit width");
386 check_sval_type(ops_v16,
"RiscOperators::rotateLeft");
387 require(ops_v16->nBits()==32,
"RiscOperators::rotateLeft width");
390 check_sval_type(ops_v17,
"RiscOperators::rotateRight");
391 require(ops_v17->nBits()==32,
"RiscOperators::rotateRight width");
394 check_sval_type(ops_v18,
"RiscOperators::shiftLeft");
395 require(ops_v18->nBits()==32,
"RiscOperators::shiftLeft width");
398 check_sval_type(ops_v19,
"RiscOperators::shiftRight");
399 require(ops_v19->nBits()==32,
"RiscOperators::shiftRight width");
402 check_sval_type(ops_v20,
"RiscOperators::shiftRightArithmetic");
403 require(ops_v20->nBits()==32,
"RiscOperators::shiftRightArithmetic width");
406 check_sval_type(ops_v21,
"RiscOperators::equalToZero");
407 require(ops_v21->nBits()==1,
"RiscOperators::equalToZero width");
410 check_sval_type(ops_v22,
"RiscOperators::ite");
411 require(ops_v22->nBits()==32,
"RiscOperators::ite width");
415 check_sval_type(ops_v22b,
"RiscOperators::iteWithStatus");
416 require(ops_v22b->nBits() == 32,
"RiscOperators::iteWithStatus width");
419 check_sval_type(ops_v23,
"RiscOperators::unsignedExtend");
420 require(ops_v23->nBits()==32,
"RiscOperators::unsignedExtend width");
423 check_sval_type(ops_v24,
"RiscOperators::unsignedExtend truncate");
424 require(ops_v24->nBits()==8,
"RiscOperators::unsignedExtend truncate width");
427 check_sval_type(ops_v25,
"RiscOperators::signExtend");
428 require(ops_v25->nBits()==32,
"RiscOperators::signExtend width");
431 check_sval_type(ops_v26,
"RiscOperators::add");
432 require(ops_v26->nBits()==32,
"RiscOperators::add width");
436 check_sval_type(ops_v27,
"RiscOperators::addWithCarries");
437 require(ops_v27->nBits()==32,
"RiscOperators::addWithCarries width");
438 check_sval_type(carry_out,
"RiscOperators::addWithCarries carry_out");
439 require(carry_out->nBits()==32,
"RiscOperators::addWithCarries carry_out width");
442 check_sval_type(ops_v28,
"RiscOperators::negate");
443 require(ops_v28->nBits()==32,
"RiscOperators::negate width");
447 check_sval_type(ops_v29,
"RiscOperators::signedDivide");
448 require(ops_v29->nBits()==32,
"RiscOperators::signedDivide width");
455 check_sval_type(ops_v30,
"RiscOperators::signedModulo");
456 require(ops_v30->nBits()==8,
"RiscOperators::signedModulo width");
462 check_sval_type(ops_v31,
"RiscOperators::signedMultiply");
463 require(ops_v31->nBits()==40,
"RiscOperators::signedMultiply width");
467 check_sval_type(ops_v32,
"RiscOperators::unsignedDivide");
468 require(ops_v32->nBits()==32,
"RiscOperators::unsignedDivide width");
475 check_sval_type(ops_v33,
"RiscOperators::unsignedModulo");
476 require(ops_v33->nBits()==8,
"RiscOperators::unsignedModulo width");
482 check_sval_type(ops_v34,
"RiscOperators::unsignedMultiply");
483 require(ops_v34->nBits()==40,
"RiscOperators::unsignedMultiply width");
486 check_sval_type(ops_v35,
"RiscOperators::readRegister");
487 require(ops_v35->nBits()==32,
"RiscOperators::readRegister width");
495 check_sval_type(ops_v36,
"RiscOperators::readMemory byte");
496 require(ops_v36->nBits()==8,
"RiscOperators::readMemory byte width");
500 check_sval_type(ops_v37,
"RiscOperators::readMemory word");
501 require(ops_v37->nBits()==32,
"RiscOperators::readMemory word width");
508 ops->writeMemory(RegisterDescriptor(), v32a, dflt32, v1);
513 ops->currentState(ops_orig_state);