2 #include "Support/CommandLine.h"
3 #include "CodeEmitterGen.h"
6 static cl::opt<std::string> Class("class", cl::desc("Print Enum list for this class"));
7 static cl::opt<bool> Parse("parse");
8 static cl::opt<bool> GenEmitter("gen-emitter");
14 static Init *getBit(Record *R, unsigned BitNo) {
15 const std::vector<RecordVal> &V = R->getValues();
16 for (unsigned i = 0, e = V.size(); i != e; ++i)
17 if (V[i].getPrefix()) {
18 assert(dynamic_cast<BitsInit*>(V[i].getValue()) &&
19 "Can only handle fields of bits<> type!");
20 BitsInit *I = (BitsInit*)V[i].getValue();
21 if (BitNo < I->getNumBits())
22 return I->getBit(BitNo);
23 BitNo -= I->getNumBits();
26 std::cerr << "Cannot find requested bit!\n";
31 static unsigned getNumBits(Record *R) {
32 const std::vector<RecordVal> &V = R->getValues();
34 for (unsigned i = 0, e = V.size(); i != e; ++i)
35 if (V[i].getPrefix()) {
36 assert(dynamic_cast<BitsInit*>(V[i].getValue()) &&
37 "Can only handle fields of bits<> type!");
38 Num += ((BitsInit*)V[i].getValue())->getNumBits();
43 static bool BitsAreFixed(Record *I1, Record *I2, unsigned BitNo) {
44 return dynamic_cast<BitInit*>(getBit(I1, BitNo)) &&
45 dynamic_cast<BitInit*>(getBit(I2, BitNo));
48 static bool BitsAreEqual(Record *I1, Record *I2, unsigned BitNo) {
49 BitInit *Bit1 = dynamic_cast<BitInit*>(getBit(I1, BitNo));
50 BitInit *Bit2 = dynamic_cast<BitInit*>(getBit(I2, BitNo));
52 return Bit1 && Bit2 && Bit1->getValue() == Bit2->getValue();
55 static bool BitRangesEqual(Record *I1, Record *I2,
56 unsigned Start, unsigned End) {
57 for (unsigned i = Start; i != End; ++i)
58 if (!BitsAreEqual(I1, I2, i))
63 static unsigned getFirstFixedBit(Record *R, unsigned FirstFixedBit) {
64 // Look for the first bit of the pair that are required to be 0 or 1.
65 while (!dynamic_cast<BitInit*>(getBit(R, FirstFixedBit)))
70 static void FindInstDifferences(Record *I1, Record *I2,
71 unsigned FirstFixedBit, unsigned MaxBits,
72 unsigned &FirstVaryingBitOverall,
73 unsigned &LastFixedBitOverall) {
74 // Compare the first instruction to the rest of the instructions, looking for
75 // fields that differ.
77 unsigned FirstVaryingBit = FirstFixedBit;
78 while (FirstVaryingBit < MaxBits && BitsAreEqual(I1, I2, FirstVaryingBit))
81 unsigned LastFixedBit = FirstVaryingBit;
82 while (LastFixedBit < MaxBits && BitsAreFixed(I1, I2, LastFixedBit))
85 if (FirstVaryingBit < FirstVaryingBitOverall)
86 FirstVaryingBitOverall = FirstVaryingBit;
87 if (LastFixedBit < LastFixedBitOverall)
88 LastFixedBitOverall = LastFixedBit;
91 static bool getBitValue(Record *R, unsigned BitNo) {
92 Init *I = getBit(R, BitNo);
93 assert(dynamic_cast<BitInit*>(I) && "Bit should be fixed!");
94 return ((BitInit*)I)->getValue();
97 struct BitComparator {
98 unsigned BitBegin, BitEnd;
99 BitComparator(unsigned B, unsigned E) : BitBegin(B), BitEnd(E) {}
101 bool operator()(Record *R1, Record *R2) { // Return true if R1 is less than R2
102 for (unsigned i = BitBegin; i != BitEnd; ++i) {
103 bool V1 = getBitValue(R1, i), V2 = getBitValue(R2, i);
113 static void PrintRange(std::vector<Record*>::iterator I,
114 std::vector<Record*>::iterator E) {
115 while (I != E) std::cerr << **I++;
118 static bool getMemoryBit(unsigned char *M, unsigned i) {
119 return (M[i/8] & (1 << (i&7))) != 0;
122 static unsigned getFirstFixedBitInSequence(std::vector<Record*>::iterator IB,
123 std::vector<Record*>::iterator IE,
125 unsigned FirstFixedBit = 0;
126 for (std::vector<Record*>::iterator I = IB; I != IE; ++I)
127 FirstFixedBit = std::max(FirstFixedBit, getFirstFixedBit(*I, StartBit));
128 return FirstFixedBit;
131 // ParseMachineCode - Try to split the vector of instructions (which is
132 // intentially taken by-copy) in half, narrowing down the possible instructions
133 // that we may have found. Eventually, this list will get pared down to zero or
134 // one instruction, in which case we have a match or failure.
136 static Record *ParseMachineCode(std::vector<Record*>::iterator InstsB,
137 std::vector<Record*>::iterator InstsE,
139 assert(InstsB != InstsE && "Empty range?");
140 if (InstsB+1 == InstsE) {
141 // Only a single instruction, see if we match it...
142 Record *Inst = *InstsB;
143 for (unsigned i = 0, e = getNumBits(Inst); i != e; ++i)
144 if (BitInit *BI = dynamic_cast<BitInit*>(getBit(Inst, i)))
145 if (getMemoryBit(M, i) != BI->getValue())
150 unsigned MaxBits = ~0;
151 for (std::vector<Record*>::iterator I = InstsB; I != InstsE; ++I)
152 MaxBits = std::min(MaxBits, getNumBits(*I));
154 unsigned FirstFixedBit = getFirstFixedBitInSequence(InstsB, InstsE, 0);
155 unsigned FirstVaryingBit, LastFixedBit;
157 FirstVaryingBit = ~0;
159 for (std::vector<Record*>::iterator I = InstsB+1; I != InstsE; ++I)
160 FindInstDifferences(*InstsB, *I, FirstFixedBit, MaxBits,
161 FirstVaryingBit, LastFixedBit);
162 if (FirstVaryingBit == MaxBits) {
163 std::cerr << "ERROR: Could not find bit to distinguish between "
164 << "the following entries!\n";
165 PrintRange(InstsB, InstsE);
169 std::cerr << "FVB: " << FirstVaryingBit << " - " << LastFixedBit
170 << ": " << InstsE-InstsB << "\n";
173 FirstFixedBit = getFirstFixedBitInSequence(InstsB, InstsE, FirstVaryingBit);
174 } while (FirstVaryingBit != FirstFixedBit);
176 //std::cerr << "\n\nXXXXXXXXXXXXXXXXX\n\n";
177 //PrintRange(InstsB, InstsE);
179 // Sort the Insts list so that the entries have all of the bits in the range
180 // [FirstVaryingBit,LastFixedBit) sorted. These bits are all guaranteed to be
181 // set to either 0 or 1 (BitInit values), which simplifies things.
183 std::sort(InstsB, InstsE, BitComparator(FirstVaryingBit, LastFixedBit));
185 // Once the list is sorted by these bits, split the bit list into smaller
186 // lists, and recurse on each one.
188 std::vector<Record*>::iterator RangeBegin = InstsB;
190 while (RangeBegin != InstsE) {
191 std::vector<Record*>::iterator RangeEnd = RangeBegin+1;
192 while (RangeEnd != InstsE &&
193 BitRangesEqual(*RangeBegin, *RangeEnd, FirstVaryingBit, LastFixedBit))
196 // We just identified a range of equal instructions. If this range is the
197 // input range, we were not able to distinguish between the instructions in
198 // the set. Print an error and exit!
200 if (RangeBegin == InstsB && RangeEnd == InstsE) {
201 std::cerr << "Error: Could not distinguish among the following insts!:\n";
202 PrintRange(InstsB, InstsE);
207 std::cerr << "FVB: " << FirstVaryingBit << " - " << LastFixedBit
208 << ": [" << RangeEnd-RangeBegin << "] - ";
209 for (int i = LastFixedBit-1; i >= (int)FirstVaryingBit; --i)
210 std::cerr << (int)((BitInit*)getBit(*RangeBegin, i))->getValue() << " ";
214 if (Record *R = ParseMachineCode(RangeBegin, RangeEnd, M)) {
216 std::cerr << "Error: Multiple matches found:\n";
217 PrintRange(InstsB, InstsE);
220 assert(Match == 0 && "Multiple matches??");
223 RangeBegin = RangeEnd;
229 static void PrintValue(Record *I, unsigned char *Ptr, const RecordVal &Val) {
230 assert(dynamic_cast<BitsInit*>(Val.getValue()) &&
231 "Can only handle undefined bits<> types!");
232 BitsInit *BI = (BitsInit*)Val.getValue();
233 assert(BI->getNumBits() <= 32 && "Can only handle fields up to 32 bits!");
236 const std::vector<RecordVal> &Vals = I->getValues();
238 // Start by filling in fixed values...
239 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
240 if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(i)))
241 Value |= B->getValue() << i;
243 // Loop over all of the fields in the instruction adding in any
244 // contributions to this value (due to bit references).
247 for (unsigned f = 0, e = Vals.size(); f != e; ++f)
248 if (Vals[f].getPrefix()) {
249 BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue();
250 if (&Vals[f] == &Val) {
251 // Read the bits directly now...
252 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
253 Value |= getMemoryBit(Ptr, Offset+i) << i;
257 // Scan through the field looking for bit initializers of the current
259 for (unsigned i = 0, e = FieldInitializer->getNumBits(); i != e; ++i)
260 if (VarBitInit *VBI =
261 dynamic_cast<VarBitInit*>(FieldInitializer->getBit(i))) {
262 TypedInit *TI = VBI->getVariable();
263 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
264 if (VI->getName() == Val.getName())
265 Value |= getMemoryBit(Ptr, Offset+i) << VBI->getBitNum();
266 } else if (FieldInit *FI = dynamic_cast<FieldInit*>(TI)) {
267 // FIXME: implement this!
268 std::cerr << "FIELD INIT not implemented yet!\n";
271 Offset += FieldInitializer->getNumBits();
274 std::cout << "0x" << std::hex << Value << std::dec;
277 static void PrintInstruction(Record *I, unsigned char *Ptr) {
278 std::cout << "Inst " << getNumBits(I)/8 << " bytes: "
279 << "\t" << I->getName() << "\t" << *I->getValue("Name")->getValue()
282 const std::vector<RecordVal> &Vals = I->getValues();
283 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
284 if (!Vals[i].getValue()->isComplete()) {
285 std::cout << Vals[i].getName() << "=";
286 PrintValue(I, Ptr, Vals[i]);
290 std::cout << "\n";// << *I;
293 static void ParseMachineCode() {
295 unsigned char Buffer[] = {
297 0x89, 0xE5, // mov EBP, ESP
298 //0x83, 0xEC, 0x08, // sub ESP, 0x8
299 0xE8, 1, 2, 3, 4, // call +0x04030201
300 0x89, 0xEC, // mov ESP, EBP
305 0x89, 0xF6, // mov ESI, ESI
306 0x68, 1, 2, 3, 4, // push 0x04030201
308 0xFF, 0xD0, // call EAX
309 0xB8, 1, 2, 3, 4, // mov EAX, 0x04030201
310 0x85, 0xC0, // test EAX, EAX
316 unsigned char Buffer[] = { 0xbf, 0xe0, 0x20, 0x1f, 0x1, 0x0, 0x0, 0x1,
317 0x0, 0x0, 0x0, 0x0, 0xc1, 0x0, 0x20, 0x1, 0x1,
318 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
319 0x0, 0x0, 0x0, 0x0, 0x0, 0x40, 0x0, 0x0, 0x0, 0x1,
320 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
321 0x0, 0x0, 0xaf, 0xe8, 0x20, 0x17
325 std::vector<Record*> Insts;
327 const std::map<std::string, Record*> &Defs = Records.getDefs();
328 Record *Inst = Records.getClass("Instruction");
329 assert(Inst && "Couldn't find Instruction class!");
331 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
332 E = Defs.end(); I != E; ++I)
333 if (I->second->isSubClassOf(Inst))
334 Insts.push_back(I->second);
336 unsigned char *BuffPtr = Buffer;
338 Record *R = ParseMachineCode(Insts.begin(), Insts.end(), BuffPtr);
340 std::cout << "Parse failed!\n";
343 PrintInstruction(R, BuffPtr);
345 unsigned Bits = getNumBits(R);
346 assert((Bits & 7) == 0 && "Instruction is not an even number of bytes!");
352 int main(int argc, char **argv) {
353 cl::ParseCommandLineOptions(argc, argv);
362 CodeEmitterGen CEG(Records);
363 CEG.createEmitter(std::cout);
368 std::cout << Records; // No argument, dump all contents
370 Record *R = Records.getClass(Class);
372 std::cerr << "Cannot find class '" << Class << "'!\n";
376 const std::map<std::string, Record*> &Defs = Records.getDefs();
377 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
378 E = Defs.end(); I != E; ++I) {
379 if (I->second->isSubClassOf(R)) {
380 std::cout << I->first << ", ";