- return Match;
-}
-
-static void PrintValue(Record *I, unsigned char *Ptr, const RecordVal &Val) {
- assert(dynamic_cast<BitsInit*>(Val.getValue()) &&
- "Can only handle undefined bits<> types!");
- BitsInit *BI = (BitsInit*)Val.getValue();
- assert(BI->getNumBits() <= 32 && "Can only handle fields up to 32 bits!");
-
- unsigned Value = 0;
- const std::vector<RecordVal> &Vals = I->getValues();
-
- // Start by filling in fixed values...
- for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
- if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(i)))
- Value |= B->getValue() << i;
-
- // Loop over all of the fields in the instruction adding in any
- // contributions to this value (due to bit references).
- //
- unsigned Offset = 0;
- for (unsigned f = 0, e = Vals.size(); f != e; ++f)
- if (Vals[f].getPrefix()) {
- BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue();
- if (&Vals[f] == &Val) {
- // Read the bits directly now...
- for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
- Value |= getMemoryBit(Ptr, Offset+i) << i;
- break;
- }
-
- // Scan through the field looking for bit initializers of the current
- // variable...
- for (unsigned i = 0, e = FieldInitializer->getNumBits(); i != e; ++i)
- if (VarBitInit *VBI =
- dynamic_cast<VarBitInit*>(FieldInitializer->getBit(i))) {
- TypedInit *TI = VBI->getVariable();
- if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
- if (VI->getName() == Val.getName())
- Value |= getMemoryBit(Ptr, Offset+i) << VBI->getBitNum();
- } else if (FieldInit *FI = dynamic_cast<FieldInit*>(TI)) {
- // FIXME: implement this!
- std::cerr << "FIELD INIT not implemented yet!\n";
- }
- }
- Offset += FieldInitializer->getNumBits();
- }
-
- std::cout << "0x" << std::hex << Value << std::dec;
-}
-
-static void PrintInstruction(Record *I, unsigned char *Ptr) {
- std::cout << "Inst " << getNumBits(I)/8 << " bytes: "
- << "\t" << I->getName() << "\t" << *I->getValue("Name")->getValue()
- << "\t";
-
- const std::vector<RecordVal> &Vals = I->getValues();
- for (unsigned i = 0, e = Vals.size(); i != e; ++i)
- if (!Vals[i].getValue()->isComplete()) {
- std::cout << Vals[i].getName() << "=";
- PrintValue(I, Ptr, Vals[i]);
- std::cout << "\t";
- }
-
- std::cout << "\n";// << *I;
-}
-
-static void ParseMachineCode() {
- // X86 code
- unsigned char Buffer[] = {
- 0x55, // push EBP
- 0x89, 0xE5, // mov EBP, ESP
- //0x83, 0xEC, 0x08, // sub ESP, 0x8
- 0xE8, 1, 2, 3, 4, // call +0x04030201
- 0x89, 0xEC, // mov ESP, EBP
- 0x5D, // pop EBP
- 0xC3, // ret
- 0x90, // nop
- 0xC9, // leave
- 0x89, 0xF6, // mov ESI, ESI
- 0x68, 1, 2, 3, 4, // push 0x04030201
- 0x5e, // pop ESI
- 0xFF, 0xD0, // call EAX
- 0xB8, 1, 2, 3, 4, // mov EAX, 0x04030201
- 0x85, 0xC0, // test EAX, EAX
- 0xF4, // hlt
- };
-
-#if 0
- // SparcV9 code
- unsigned char Buffer[] = { 0xbf, 0xe0, 0x20, 0x1f, 0x1, 0x0, 0x0, 0x1,
- 0x0, 0x0, 0x0, 0x0, 0xc1, 0x0, 0x20, 0x1, 0x1,
- 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
- 0x0, 0x0, 0x0, 0x0, 0x0, 0x40, 0x0, 0x0, 0x0, 0x1,
- 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
- 0x0, 0x0, 0xaf, 0xe8, 0x20, 0x17
- };
-#endif
-
- std::vector<Record*> Insts;
-
- const std::map<std::string, Record*> &Defs = Records.getDefs();
- Record *Inst = Records.getClass("Instruction");
- assert(Inst && "Couldn't find Instruction class!");
-
- for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
- E = Defs.end(); I != E; ++I)
- if (I->second->isSubClassOf(Inst))
- Insts.push_back(I->second);