llvm.org GIT mirror llvm / 3b23972
remove a bunch of long-dead testing code git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@26497 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 13 years ago
1 changed file(s) with 1 addition(s) and 331 deletion(s). Raw diff Collapse all Expand all
3939 GenDAGISel,
4040 GenSubtarget,
4141 GenIntrinsic,
42 PrintEnums,
43 Parse
42 PrintEnums
4443 };
4544
4645 namespace {
7069 "Generate intrinsic information"),
7170 clEnumValN(PrintEnums, "print-enums",
7271 "Print enum values for a class"),
73 clEnumValN(Parse, "parse",
74 "Interpret machine code (testing only)"),
7572 clEnumValEnd));
7673
7774 cl::opt
9794
9895 RecordKeeper llvm::Records;
9996
100 static Init *getBit(Record *R, unsigned BitNo) {
101 const std::vector &V = R->getValues();
102 for (unsigned i = 0, e = V.size(); i != e; ++i)
103 if (V[i].getPrefix()) {
104 assert(dynamic_cast(V[i].getValue()) &&
105 "Can only handle fields of bits<> type!");
106 BitsInit *I = (BitsInit*)V[i].getValue();
107 if (BitNo < I->getNumBits())
108 return I->getBit(BitNo);
109 BitNo -= I->getNumBits();
110 }
111
112 std::cerr << "Cannot find requested bit!\n";
113 exit(1);
114 return 0;
115 }
116
117 static unsigned getNumBits(Record *R) {
118 const std::vector &V = R->getValues();
119 unsigned Num = 0;
120 for (unsigned i = 0, e = V.size(); i != e; ++i)
121 if (V[i].getPrefix()) {
122 assert(dynamic_cast(V[i].getValue()) &&
123 "Can only handle fields of bits<> type!");
124 Num += ((BitsInit*)V[i].getValue())->getNumBits();
125 }
126 return Num;
127 }
128
129 static bool BitsAreFixed(Record *I1, Record *I2, unsigned BitNo) {
130 return dynamic_cast(getBit(I1, BitNo)) &&
131 dynamic_cast(getBit(I2, BitNo));
132 }
133
134 static bool BitsAreEqual(Record *I1, Record *I2, unsigned BitNo) {
135 BitInit *Bit1 = dynamic_cast(getBit(I1, BitNo));
136 BitInit *Bit2 = dynamic_cast(getBit(I2, BitNo));
137
138 return Bit1 && Bit2 && Bit1->getValue() == Bit2->getValue();
139 }
140
141 static bool BitRangesEqual(Record *I1, Record *I2,
142 unsigned Start, unsigned End) {
143 for (unsigned i = Start; i != End; ++i)
144 if (!BitsAreEqual(I1, I2, i))
145 return false;
146 return true;
147 }
148
149 static unsigned getFirstFixedBit(Record *R, unsigned FirstFixedBit) {
150 // Look for the first bit of the pair that are required to be 0 or 1.
151 while (!dynamic_cast(getBit(R, FirstFixedBit)))
152 ++FirstFixedBit;
153 return FirstFixedBit;
154 }
155
156 static void FindInstDifferences(Record *I1, Record *I2,
157 unsigned FirstFixedBit, unsigned MaxBits,
158 unsigned &FirstVaryingBitOverall,
159 unsigned &LastFixedBitOverall) {
160 // Compare the first instruction to the rest of the instructions, looking for
161 // fields that differ.
162 //
163 unsigned FirstVaryingBit = FirstFixedBit;
164 while (FirstVaryingBit < MaxBits && BitsAreEqual(I1, I2, FirstVaryingBit))
165 ++FirstVaryingBit;
166
167 unsigned LastFixedBit = FirstVaryingBit;
168 while (LastFixedBit < MaxBits && BitsAreFixed(I1, I2, LastFixedBit))
169 ++LastFixedBit;
170
171 if (FirstVaryingBit < FirstVaryingBitOverall)
172 FirstVaryingBitOverall = FirstVaryingBit;
173 if (LastFixedBit < LastFixedBitOverall)
174 LastFixedBitOverall = LastFixedBit;
175 }
176
177 static bool getBitValue(Record *R, unsigned BitNo) {
178 Init *I = getBit(R, BitNo);
179 assert(dynamic_cast(I) && "Bit should be fixed!");
180 return ((BitInit*)I)->getValue();
181 }
182
183 struct BitComparator {
184 unsigned BitBegin, BitEnd;
185 BitComparator(unsigned B, unsigned E) : BitBegin(B), BitEnd(E) {}
186
187 bool operator()(Record *R1, Record *R2) { // Return true if R1 is less than R2
188 for (unsigned i = BitBegin; i != BitEnd; ++i) {
189 bool V1 = getBitValue(R1, i), V2 = getBitValue(R2, i);
190 if (V1 < V2)
191 return true;
192 else if (V2 < V1)
193 return false;
194 }
195 return false;
196 }
197 };
198
199 static void PrintRange(std::vector::iterator I,
200 std::vector::iterator E) {
201 while (I != E) std::cerr << **I++;
202 }
203
204 static bool getMemoryBit(unsigned char *M, unsigned i) {
205 return (M[i/8] & (1 << (i&7))) != 0;
206 }
207
208 static unsigned getFirstFixedBitInSequence(std::vector::iterator IB,
209 std::vector::iterator IE,
210 unsigned StartBit) {
211 unsigned FirstFixedBit = 0;
212 for (std::vector::iterator I = IB; I != IE; ++I)
213 FirstFixedBit = std::max(FirstFixedBit, getFirstFixedBit(*I, StartBit));
214 return FirstFixedBit;
215 }
216
217 // ParseMachineCode - Try to split the vector of instructions (which is
218 // intentionally taken by-copy) in half, narrowing down the possible
219 // instructions that we may have found. Eventually, this list will get pared
220 // down to zero or one instruction, in which case we have a match or failure.
221 //
222 static Record *ParseMachineCode(std::vector::iterator InstsB,
223 std::vector::iterator InstsE,
224 unsigned char *M) {
225 assert(InstsB != InstsE && "Empty range?");
226 if (InstsB+1 == InstsE) {
227 // Only a single instruction, see if we match it...
228 Record *Inst = *InstsB;
229 for (unsigned i = 0, e = getNumBits(Inst); i != e; ++i)
230 if (BitInit *BI = dynamic_cast(getBit(Inst, i)))
231 if (getMemoryBit(M, i) != BI->getValue())
232 throw std::string("Parse failed!\n");
233 return Inst;
234 }
235
236 unsigned MaxBits = ~0;
237 for (std::vector::iterator I = InstsB; I != InstsE; ++I)
238 MaxBits = std::min(MaxBits, getNumBits(*I));
239
240 unsigned FirstFixedBit = getFirstFixedBitInSequence(InstsB, InstsE, 0);
241 unsigned FirstVaryingBit, LastFixedBit;
242 do {
243 FirstVaryingBit = ~0;
244 LastFixedBit = ~0;
245 for (std::vector::iterator I = InstsB+1; I != InstsE; ++I)
246 FindInstDifferences(*InstsB, *I, FirstFixedBit, MaxBits,
247 FirstVaryingBit, LastFixedBit);
248 if (FirstVaryingBit == MaxBits) {
249 std::cerr << "ERROR: Could not find bit to distinguish between "
250 << "the following entries!\n";
251 PrintRange(InstsB, InstsE);
252 }
253
254 #if 0
255 std::cerr << "FVB: " << FirstVaryingBit << " - " << LastFixedBit
256 << ": " << InstsE-InstsB << "\n";
257 #endif
258
259 FirstFixedBit = getFirstFixedBitInSequence(InstsB, InstsE, FirstVaryingBit);
260 } while (FirstVaryingBit != FirstFixedBit);
261
262 //std::cerr << "\n\nXXXXXXXXXXXXXXXXX\n\n";
263 //PrintRange(InstsB, InstsE);
264
265 // Sort the Insts list so that the entries have all of the bits in the range
266 // [FirstVaryingBit,LastFixedBit) sorted. These bits are all guaranteed to be
267 // set to either 0 or 1 (BitInit values), which simplifies things.
268 //
269 std::sort(InstsB, InstsE, BitComparator(FirstVaryingBit, LastFixedBit));
270
271 // Once the list is sorted by these bits, split the bit list into smaller
272 // lists, and recurse on each one.
273 //
274 std::vector::iterator RangeBegin = InstsB;
275 Record *Match = 0;
276 while (RangeBegin != InstsE) {
277 std::vector::iterator RangeEnd = RangeBegin+1;
278 while (RangeEnd != InstsE &&
279 BitRangesEqual(*RangeBegin, *RangeEnd, FirstVaryingBit, LastFixedBit))
280 ++RangeEnd;
281
282 // We just identified a range of equal instructions. If this range is the
283 // input range, we were not able to distinguish between the instructions in
284 // the set. Print an error and exit!
285 //
286 if (RangeBegin == InstsB && RangeEnd == InstsE) {
287 std::cerr << "Error: Could not distinguish among the following insts!:\n";
288 PrintRange(InstsB, InstsE);
289 exit(1);
290 }
291
292 #if 0
293 std::cerr << "FVB: " << FirstVaryingBit << " - " << LastFixedBit
294 << ": [" << RangeEnd-RangeBegin << "] - ";
295 for (int i = LastFixedBit-1; i >= (int)FirstVaryingBit; --i)
296 std::cerr << (int)((BitInit*)getBit(*RangeBegin, i))->getValue() << " ";
297 std::cerr << "\n";
298 #endif
299
300 if (Record *R = ParseMachineCode(RangeBegin, RangeEnd, M)) {
301 if (Match) {
302 std::cerr << "Error: Multiple matches found:\n";
303 PrintRange(InstsB, InstsE);
304 }
305
306 assert(Match == 0 && "Multiple matches??");
307 Match = R;
308 }
309 RangeBegin = RangeEnd;
310 }
311
312 return Match;
313 }
314
315 static void PrintValue(Record *I, unsigned char *Ptr, const RecordVal &Val) {
316 assert(dynamic_cast(Val.getValue()) &&
317 "Can only handle undefined bits<> types!");
318 BitsInit *BI = (BitsInit*)Val.getValue();
319 assert(BI->getNumBits() <= 32 && "Can only handle fields up to 32 bits!");
320
321 unsigned Value = 0;
322 const std::vector &Vals = I->getValues();
323
324 // Start by filling in fixed values...
325 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
326 if (BitInit *B = dynamic_cast(BI->getBit(i)))
327 Value |= B->getValue() << i;
328
329 // Loop over all of the fields in the instruction adding in any
330 // contributions to this value (due to bit references).
331 //
332 unsigned Offset = 0;
333 for (unsigned f = 0, e = Vals.size(); f != e; ++f)
334 if (Vals[f].getPrefix()) {
335 BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue();
336 if (&Vals[f] == &Val) {
337 // Read the bits directly now...
338 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
339 Value |= getMemoryBit(Ptr, Offset+i) << i;
340 break;
341 }
342
343 // Scan through the field looking for bit initializers of the current
344 // variable...
345 for (unsigned i = 0, e = FieldInitializer->getNumBits(); i != e; ++i)
346 if (VarBitInit *VBI =
347 dynamic_cast(FieldInitializer->getBit(i))) {
348 TypedInit *TI = VBI->getVariable();
349 if (VarInit *VI = dynamic_cast(TI)) {
350 if (VI->getName() == Val.getName())
351 Value |= getMemoryBit(Ptr, Offset+i) << VBI->getBitNum();
352 } else if (FieldInit *FI = dynamic_cast(TI)) {
353 // FIXME: implement this!
354 std::cerr << "FIELD INIT not implemented yet!\n";
355 }
356 }
357 Offset += FieldInitializer->getNumBits();
358 }
359
360 std::cout << "0x" << std::hex << Value << std::dec;
361 }
362
363 static void PrintInstruction(Record *I, unsigned char *Ptr) {
364 std::cout << "Inst " << getNumBits(I)/8 << " bytes: "
365 << "\t" << I->getName() << "\t" << *I->getValue("Name")->getValue()
366 << "\t";
367
368 const std::vector &Vals = I->getValues();
369 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
370 if (!Vals[i].getValue()->isComplete()) {
371 std::cout << Vals[i].getName() << "=";
372 PrintValue(I, Ptr, Vals[i]);
373 std::cout << "\t";
374 }
375
376 std::cout << "\n";// << *I;
377 }
378
379 static void ParseMachineCode() {
380 // X86 code
381 unsigned char Buffer[] = {
382 0x55, // push EBP
383 0x89, 0xE5, // mov EBP, ESP
384 //0x83, 0xEC, 0x08, // sub ESP, 0x8
385 0xE8, 1, 2, 3, 4, // call +0x04030201
386 0x89, 0xEC, // mov ESP, EBP
387 0x5D, // pop EBP
388 0xC3, // ret
389 0x90, // nop
390 0xC9, // leave
391 0x89, 0xF6, // mov ESI, ESI
392 0x68, 1, 2, 3, 4, // push 0x04030201
393 0x5e, // pop ESI
394 0xFF, 0xD0, // call EAX
395 0xB8, 1, 2, 3, 4, // mov EAX, 0x04030201
396 0x85, 0xC0, // test EAX, EAX
397 0xF4, // hlt
398 };
399
400 #if 0
401 // SparcV9 code
402 unsigned char Buffer[] = { 0xbf, 0xe0, 0x20, 0x1f, 0x1, 0x0, 0x0, 0x1,
403 0x0, 0x0, 0x0, 0x0, 0xc1, 0x0, 0x20, 0x1, 0x1,
404 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
405 0x0, 0x0, 0x0, 0x0, 0x0, 0x40, 0x0, 0x0, 0x0, 0x1,
406 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
407 0x0, 0x0, 0xaf, 0xe8, 0x20, 0x17
408 };
409 #endif
410
411 std::vector Insts = Records.getAllDerivedDefinitions("Instruction");
412
413 unsigned char *BuffPtr = Buffer;
414 while (1) {
415 Record *R = ParseMachineCode(Insts.begin(), Insts.end(), BuffPtr);
416 PrintInstruction(R, BuffPtr);
417
418 unsigned Bits = getNumBits(R);
419 assert((Bits & 7) == 0 && "Instruction is not an even number of bytes!");
420 BuffPtr += Bits/8;
421 }
422 }
423
42497 int main(int argc, char **argv) {
42598 cl::ParseCommandLineOptions(argc, argv);
42699 ParseFile(InputFilename, IncludeDirs);
442115 switch (Action) {
443116 case PrintRecords:
444117 *Out << Records; // No argument, dump all contents
445 break;
446 case Parse:
447 ParseMachineCode();
448118 break;
449119 case GenEmitter:
450120 CodeEmitterGen(Records).run(*Out);