1 //===- TableGen.cpp - Top-Level TableGen implementation -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // TableGen is a tool which can be used to build up a description of something,
11 // then invoke one or more "tablegen backends" to emit information about the
12 // description in some predefined format. In practice, this is used by the LLVM
13 // code generators to automate generation of a code generator through a
14 // high-level description of the target.
16 //===----------------------------------------------------------------------===//
19 #include "Support/CommandLine.h"
20 #include "Support/Signals.h"
21 #include "Support/FileUtilities.h"
22 #include "CodeEmitterGen.h"
23 #include "RegisterInfoEmitter.h"
24 #include "InstrInfoEmitter.h"
25 #include "InstrSelectorEmitter.h"
35 GenRegisterEnums, GenRegister, GenRegisterHeader,
36 GenInstrEnums, GenInstrs, GenInstrSelector,
43 Action(cl::desc("Action to perform:"),
44 cl::values(clEnumValN(PrintRecords, "print-records",
45 "Print all records to stdout (default)"),
46 clEnumValN(GenEmitter, "gen-emitter",
47 "Generate machine code emitter"),
48 clEnumValN(GenRegisterEnums, "gen-register-enums",
49 "Generate enum values for registers"),
50 clEnumValN(GenRegister, "gen-register-desc",
51 "Generate a register info description"),
52 clEnumValN(GenRegisterHeader, "gen-register-desc-header",
53 "Generate a register info description header"),
54 clEnumValN(GenInstrEnums, "gen-instr-enums",
55 "Generate enum values for instructions"),
56 clEnumValN(GenInstrs, "gen-instr-desc",
57 "Generate instruction descriptions"),
58 clEnumValN(GenInstrSelector, "gen-instr-selector",
59 "Generate an instruction selector"),
60 clEnumValN(PrintEnums, "print-enums",
61 "Print enum values for a class"),
62 clEnumValN(Parse, "parse",
63 "Interpret machine code (testing only)"),
67 Class("class", cl::desc("Print Enum list for this class"),
68 cl::value_desc("class name"));
71 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
75 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
78 IncludeDir("I", cl::desc("Directory of include files"),
79 cl::value_desc("directory"), cl::init(""));
83 void ParseFile(const std::string &Filename, const std::string & IncludeDir);
87 static Init *getBit(Record *R, unsigned BitNo) {
88 const std::vector<RecordVal> &V = R->getValues();
89 for (unsigned i = 0, e = V.size(); i != e; ++i)
90 if (V[i].getPrefix()) {
91 assert(dynamic_cast<BitsInit*>(V[i].getValue()) &&
92 "Can only handle fields of bits<> type!");
93 BitsInit *I = (BitsInit*)V[i].getValue();
94 if (BitNo < I->getNumBits())
95 return I->getBit(BitNo);
96 BitNo -= I->getNumBits();
99 std::cerr << "Cannot find requested bit!\n";
104 static unsigned getNumBits(Record *R) {
105 const std::vector<RecordVal> &V = R->getValues();
107 for (unsigned i = 0, e = V.size(); i != e; ++i)
108 if (V[i].getPrefix()) {
109 assert(dynamic_cast<BitsInit*>(V[i].getValue()) &&
110 "Can only handle fields of bits<> type!");
111 Num += ((BitsInit*)V[i].getValue())->getNumBits();
116 static bool BitsAreFixed(Record *I1, Record *I2, unsigned BitNo) {
117 return dynamic_cast<BitInit*>(getBit(I1, BitNo)) &&
118 dynamic_cast<BitInit*>(getBit(I2, BitNo));
121 static bool BitsAreEqual(Record *I1, Record *I2, unsigned BitNo) {
122 BitInit *Bit1 = dynamic_cast<BitInit*>(getBit(I1, BitNo));
123 BitInit *Bit2 = dynamic_cast<BitInit*>(getBit(I2, BitNo));
125 return Bit1 && Bit2 && Bit1->getValue() == Bit2->getValue();
128 static bool BitRangesEqual(Record *I1, Record *I2,
129 unsigned Start, unsigned End) {
130 for (unsigned i = Start; i != End; ++i)
131 if (!BitsAreEqual(I1, I2, i))
136 static unsigned getFirstFixedBit(Record *R, unsigned FirstFixedBit) {
137 // Look for the first bit of the pair that are required to be 0 or 1.
138 while (!dynamic_cast<BitInit*>(getBit(R, FirstFixedBit)))
140 return FirstFixedBit;
143 static void FindInstDifferences(Record *I1, Record *I2,
144 unsigned FirstFixedBit, unsigned MaxBits,
145 unsigned &FirstVaryingBitOverall,
146 unsigned &LastFixedBitOverall) {
147 // Compare the first instruction to the rest of the instructions, looking for
148 // fields that differ.
150 unsigned FirstVaryingBit = FirstFixedBit;
151 while (FirstVaryingBit < MaxBits && BitsAreEqual(I1, I2, FirstVaryingBit))
154 unsigned LastFixedBit = FirstVaryingBit;
155 while (LastFixedBit < MaxBits && BitsAreFixed(I1, I2, LastFixedBit))
158 if (FirstVaryingBit < FirstVaryingBitOverall)
159 FirstVaryingBitOverall = FirstVaryingBit;
160 if (LastFixedBit < LastFixedBitOverall)
161 LastFixedBitOverall = LastFixedBit;
164 static bool getBitValue(Record *R, unsigned BitNo) {
165 Init *I = getBit(R, BitNo);
166 assert(dynamic_cast<BitInit*>(I) && "Bit should be fixed!");
167 return ((BitInit*)I)->getValue();
170 struct BitComparator {
171 unsigned BitBegin, BitEnd;
172 BitComparator(unsigned B, unsigned E) : BitBegin(B), BitEnd(E) {}
174 bool operator()(Record *R1, Record *R2) { // Return true if R1 is less than R2
175 for (unsigned i = BitBegin; i != BitEnd; ++i) {
176 bool V1 = getBitValue(R1, i), V2 = getBitValue(R2, i);
186 static void PrintRange(std::vector<Record*>::iterator I,
187 std::vector<Record*>::iterator E) {
188 while (I != E) std::cerr << **I++;
191 static bool getMemoryBit(unsigned char *M, unsigned i) {
192 return (M[i/8] & (1 << (i&7))) != 0;
195 static unsigned getFirstFixedBitInSequence(std::vector<Record*>::iterator IB,
196 std::vector<Record*>::iterator IE,
198 unsigned FirstFixedBit = 0;
199 for (std::vector<Record*>::iterator I = IB; I != IE; ++I)
200 FirstFixedBit = std::max(FirstFixedBit, getFirstFixedBit(*I, StartBit));
201 return FirstFixedBit;
204 // ParseMachineCode - Try to split the vector of instructions (which is
205 // intentionally taken by-copy) in half, narrowing down the possible
206 // instructions that we may have found. Eventually, this list will get pared
207 // down to zero or one instruction, in which case we have a match or failure.
209 static Record *ParseMachineCode(std::vector<Record*>::iterator InstsB,
210 std::vector<Record*>::iterator InstsE,
212 assert(InstsB != InstsE && "Empty range?");
213 if (InstsB+1 == InstsE) {
214 // Only a single instruction, see if we match it...
215 Record *Inst = *InstsB;
216 for (unsigned i = 0, e = getNumBits(Inst); i != e; ++i)
217 if (BitInit *BI = dynamic_cast<BitInit*>(getBit(Inst, i)))
218 if (getMemoryBit(M, i) != BI->getValue())
219 throw std::string("Parse failed!\n");
223 unsigned MaxBits = ~0;
224 for (std::vector<Record*>::iterator I = InstsB; I != InstsE; ++I)
225 MaxBits = std::min(MaxBits, getNumBits(*I));
227 unsigned FirstFixedBit = getFirstFixedBitInSequence(InstsB, InstsE, 0);
228 unsigned FirstVaryingBit, LastFixedBit;
230 FirstVaryingBit = ~0;
232 for (std::vector<Record*>::iterator I = InstsB+1; I != InstsE; ++I)
233 FindInstDifferences(*InstsB, *I, FirstFixedBit, MaxBits,
234 FirstVaryingBit, LastFixedBit);
235 if (FirstVaryingBit == MaxBits) {
236 std::cerr << "ERROR: Could not find bit to distinguish between "
237 << "the following entries!\n";
238 PrintRange(InstsB, InstsE);
242 std::cerr << "FVB: " << FirstVaryingBit << " - " << LastFixedBit
243 << ": " << InstsE-InstsB << "\n";
246 FirstFixedBit = getFirstFixedBitInSequence(InstsB, InstsE, FirstVaryingBit);
247 } while (FirstVaryingBit != FirstFixedBit);
249 //std::cerr << "\n\nXXXXXXXXXXXXXXXXX\n\n";
250 //PrintRange(InstsB, InstsE);
252 // Sort the Insts list so that the entries have all of the bits in the range
253 // [FirstVaryingBit,LastFixedBit) sorted. These bits are all guaranteed to be
254 // set to either 0 or 1 (BitInit values), which simplifies things.
256 std::sort(InstsB, InstsE, BitComparator(FirstVaryingBit, LastFixedBit));
258 // Once the list is sorted by these bits, split the bit list into smaller
259 // lists, and recurse on each one.
261 std::vector<Record*>::iterator RangeBegin = InstsB;
263 while (RangeBegin != InstsE) {
264 std::vector<Record*>::iterator RangeEnd = RangeBegin+1;
265 while (RangeEnd != InstsE &&
266 BitRangesEqual(*RangeBegin, *RangeEnd, FirstVaryingBit, LastFixedBit))
269 // We just identified a range of equal instructions. If this range is the
270 // input range, we were not able to distinguish between the instructions in
271 // the set. Print an error and exit!
273 if (RangeBegin == InstsB && RangeEnd == InstsE) {
274 std::cerr << "Error: Could not distinguish among the following insts!:\n";
275 PrintRange(InstsB, InstsE);
280 std::cerr << "FVB: " << FirstVaryingBit << " - " << LastFixedBit
281 << ": [" << RangeEnd-RangeBegin << "] - ";
282 for (int i = LastFixedBit-1; i >= (int)FirstVaryingBit; --i)
283 std::cerr << (int)((BitInit*)getBit(*RangeBegin, i))->getValue() << " ";
287 if (Record *R = ParseMachineCode(RangeBegin, RangeEnd, M)) {
289 std::cerr << "Error: Multiple matches found:\n";
290 PrintRange(InstsB, InstsE);
293 assert(Match == 0 && "Multiple matches??");
296 RangeBegin = RangeEnd;
302 static void PrintValue(Record *I, unsigned char *Ptr, const RecordVal &Val) {
303 assert(dynamic_cast<BitsInit*>(Val.getValue()) &&
304 "Can only handle undefined bits<> types!");
305 BitsInit *BI = (BitsInit*)Val.getValue();
306 assert(BI->getNumBits() <= 32 && "Can only handle fields up to 32 bits!");
309 const std::vector<RecordVal> &Vals = I->getValues();
311 // Start by filling in fixed values...
312 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
313 if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(i)))
314 Value |= B->getValue() << i;
316 // Loop over all of the fields in the instruction adding in any
317 // contributions to this value (due to bit references).
320 for (unsigned f = 0, e = Vals.size(); f != e; ++f)
321 if (Vals[f].getPrefix()) {
322 BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue();
323 if (&Vals[f] == &Val) {
324 // Read the bits directly now...
325 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
326 Value |= getMemoryBit(Ptr, Offset+i) << i;
330 // Scan through the field looking for bit initializers of the current
332 for (unsigned i = 0, e = FieldInitializer->getNumBits(); i != e; ++i)
333 if (VarBitInit *VBI =
334 dynamic_cast<VarBitInit*>(FieldInitializer->getBit(i))) {
335 TypedInit *TI = VBI->getVariable();
336 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
337 if (VI->getName() == Val.getName())
338 Value |= getMemoryBit(Ptr, Offset+i) << VBI->getBitNum();
339 } else if (FieldInit *FI = dynamic_cast<FieldInit*>(TI)) {
340 // FIXME: implement this!
341 std::cerr << "FIELD INIT not implemented yet!\n";
344 Offset += FieldInitializer->getNumBits();
347 std::cout << "0x" << std::hex << Value << std::dec;
350 static void PrintInstruction(Record *I, unsigned char *Ptr) {
351 std::cout << "Inst " << getNumBits(I)/8 << " bytes: "
352 << "\t" << I->getName() << "\t" << *I->getValue("Name")->getValue()
355 const std::vector<RecordVal> &Vals = I->getValues();
356 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
357 if (!Vals[i].getValue()->isComplete()) {
358 std::cout << Vals[i].getName() << "=";
359 PrintValue(I, Ptr, Vals[i]);
363 std::cout << "\n";// << *I;
366 static void ParseMachineCode() {
368 unsigned char Buffer[] = {
370 0x89, 0xE5, // mov EBP, ESP
371 //0x83, 0xEC, 0x08, // sub ESP, 0x8
372 0xE8, 1, 2, 3, 4, // call +0x04030201
373 0x89, 0xEC, // mov ESP, EBP
378 0x89, 0xF6, // mov ESI, ESI
379 0x68, 1, 2, 3, 4, // push 0x04030201
381 0xFF, 0xD0, // call EAX
382 0xB8, 1, 2, 3, 4, // mov EAX, 0x04030201
383 0x85, 0xC0, // test EAX, EAX
389 unsigned char Buffer[] = { 0xbf, 0xe0, 0x20, 0x1f, 0x1, 0x0, 0x0, 0x1,
390 0x0, 0x0, 0x0, 0x0, 0xc1, 0x0, 0x20, 0x1, 0x1,
391 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
392 0x0, 0x0, 0x0, 0x0, 0x0, 0x40, 0x0, 0x0, 0x0, 0x1,
393 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
394 0x0, 0x0, 0xaf, 0xe8, 0x20, 0x17
398 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
400 unsigned char *BuffPtr = Buffer;
402 Record *R = ParseMachineCode(Insts.begin(), Insts.end(), BuffPtr);
403 PrintInstruction(R, BuffPtr);
405 unsigned Bits = getNumBits(R);
406 assert((Bits & 7) == 0 && "Instruction is not an even number of bytes!");
411 } // End llvm namespace
413 using namespace llvm;
415 int main(int argc, char **argv) {
416 cl::ParseCommandLineOptions(argc, argv);
417 ParseFile(InputFilename, IncludeDir);
419 std::ostream *Out = &std::cout;
420 if (OutputFilename != "-") {
421 // Output to a .tmp file, because we don't actually want to overwrite the
422 // output file unless the generated file is different or the specified file
424 Out = new std::ofstream((OutputFilename+".tmp").c_str());
427 std::cerr << argv[0] << ": error opening " << OutputFilename << ".tmp!\n";
431 // Make sure the file gets removed if *gasp* tablegen crashes...
432 RemoveFileOnSignal(OutputFilename+".tmp");
438 *Out << Records; // No argument, dump all contents
444 CodeEmitterGen(Records).run(*Out);
447 case GenRegisterEnums:
448 RegisterInfoEmitter(Records).runEnums(*Out);
451 RegisterInfoEmitter(Records).run(*Out);
453 case GenRegisterHeader:
454 RegisterInfoEmitter(Records).runHeader(*Out);
458 InstrInfoEmitter(Records).runEnums(*Out);
461 InstrInfoEmitter(Records).run(*Out);
463 case GenInstrSelector:
464 InstrSelectorEmitter(Records).run(*Out);
468 std::vector<Record*> Recs = Records.getAllDerivedDefinitions(Class);
469 for (unsigned i = 0, e = Recs.size(); i != e; ++i)
470 *Out << Recs[i]->getName() << ", ";
475 assert(1 && "Invalid Action");
478 } catch (const std::string &Error) {
479 std::cerr << Error << "\n";
480 if (Out != &std::cout) {
481 delete Out; // Close the file
482 std::remove(OutputFilename.c_str()); // Remove the file, it's broken
487 if (Out != &std::cout) {
488 delete Out; // Close the file
490 // Now that we have generated the result, check to see if we either don't
491 // have the requested file, or if the requested file is different than the
492 // file we generated. If so, move the generated file over the requested
493 // file. Otherwise, just remove the file we just generated, so 'make'
494 // doesn't try to regenerate tons of dependencies.
496 MoveFileOverIfUpdated(OutputFilename+".tmp", OutputFilename);