1 //===- TableGen.cpp - Top-Level TableGen implementation -------------------===//
3 // TableGen is a tool which can be used to build up a description of something,
4 // then invoke one or more "tablegen backends" to emit information about the
5 // description in some predefined format. In practice, this is used by the LLVM
6 // code generators to automate generation of a code generator through a
7 // high-level description of the target.
9 //===----------------------------------------------------------------------===//
12 #include "Support/CommandLine.h"
13 #include "Support/Signals.h"
14 #include "Support/FileUtilities.h"
15 #include "CodeEmitterGen.h"
16 #include "RegisterInfoEmitter.h"
17 #include "InstrInfoEmitter.h"
18 #include "InstrSelectorEmitter.h"
25 GenRegisterEnums, GenRegister, GenRegisterHeader,
26 GenInstrEnums, GenInstrs, GenInstrSelector,
33 Action(cl::desc("Action to perform:"),
34 cl::values(clEnumValN(PrintRecords, "print-records",
35 "Print all records to stdout (default)"),
36 clEnumValN(GenEmitter, "gen-emitter",
37 "Generate machine code emitter"),
38 clEnumValN(GenRegisterEnums, "gen-register-enums",
39 "Generate enum values for registers"),
40 clEnumValN(GenRegister, "gen-register-desc",
41 "Generate a register info description"),
42 clEnumValN(GenRegisterHeader, "gen-register-desc-header",
43 "Generate a register info description header"),
44 clEnumValN(GenInstrEnums, "gen-instr-enums",
45 "Generate enum values for instructions"),
46 clEnumValN(GenInstrs, "gen-instr-desc",
47 "Generate instruction descriptions"),
48 clEnumValN(GenInstrSelector, "gen-instr-selector",
49 "Generate an instruction selector"),
50 clEnumValN(PrintEnums, "print-enums",
51 "Print enum values for a class"),
52 clEnumValN(Parse, "parse",
53 "Interpret machine code (testing only)"),
57 Class("class", cl::desc("Print Enum list for this class"),
58 cl::value_desc("class name"));
61 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
65 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
69 void ParseFile(const std::string &Filename);
73 static Init *getBit(Record *R, unsigned BitNo) {
74 const std::vector<RecordVal> &V = R->getValues();
75 for (unsigned i = 0, e = V.size(); i != e; ++i)
76 if (V[i].getPrefix()) {
77 assert(dynamic_cast<BitsInit*>(V[i].getValue()) &&
78 "Can only handle fields of bits<> type!");
79 BitsInit *I = (BitsInit*)V[i].getValue();
80 if (BitNo < I->getNumBits())
81 return I->getBit(BitNo);
82 BitNo -= I->getNumBits();
85 std::cerr << "Cannot find requested bit!\n";
90 static unsigned getNumBits(Record *R) {
91 const std::vector<RecordVal> &V = R->getValues();
93 for (unsigned i = 0, e = V.size(); i != e; ++i)
94 if (V[i].getPrefix()) {
95 assert(dynamic_cast<BitsInit*>(V[i].getValue()) &&
96 "Can only handle fields of bits<> type!");
97 Num += ((BitsInit*)V[i].getValue())->getNumBits();
102 static bool BitsAreFixed(Record *I1, Record *I2, unsigned BitNo) {
103 return dynamic_cast<BitInit*>(getBit(I1, BitNo)) &&
104 dynamic_cast<BitInit*>(getBit(I2, BitNo));
107 static bool BitsAreEqual(Record *I1, Record *I2, unsigned BitNo) {
108 BitInit *Bit1 = dynamic_cast<BitInit*>(getBit(I1, BitNo));
109 BitInit *Bit2 = dynamic_cast<BitInit*>(getBit(I2, BitNo));
111 return Bit1 && Bit2 && Bit1->getValue() == Bit2->getValue();
114 static bool BitRangesEqual(Record *I1, Record *I2,
115 unsigned Start, unsigned End) {
116 for (unsigned i = Start; i != End; ++i)
117 if (!BitsAreEqual(I1, I2, i))
122 static unsigned getFirstFixedBit(Record *R, unsigned FirstFixedBit) {
123 // Look for the first bit of the pair that are required to be 0 or 1.
124 while (!dynamic_cast<BitInit*>(getBit(R, FirstFixedBit)))
126 return FirstFixedBit;
129 static void FindInstDifferences(Record *I1, Record *I2,
130 unsigned FirstFixedBit, unsigned MaxBits,
131 unsigned &FirstVaryingBitOverall,
132 unsigned &LastFixedBitOverall) {
133 // Compare the first instruction to the rest of the instructions, looking for
134 // fields that differ.
136 unsigned FirstVaryingBit = FirstFixedBit;
137 while (FirstVaryingBit < MaxBits && BitsAreEqual(I1, I2, FirstVaryingBit))
140 unsigned LastFixedBit = FirstVaryingBit;
141 while (LastFixedBit < MaxBits && BitsAreFixed(I1, I2, LastFixedBit))
144 if (FirstVaryingBit < FirstVaryingBitOverall)
145 FirstVaryingBitOverall = FirstVaryingBit;
146 if (LastFixedBit < LastFixedBitOverall)
147 LastFixedBitOverall = LastFixedBit;
150 static bool getBitValue(Record *R, unsigned BitNo) {
151 Init *I = getBit(R, BitNo);
152 assert(dynamic_cast<BitInit*>(I) && "Bit should be fixed!");
153 return ((BitInit*)I)->getValue();
156 struct BitComparator {
157 unsigned BitBegin, BitEnd;
158 BitComparator(unsigned B, unsigned E) : BitBegin(B), BitEnd(E) {}
160 bool operator()(Record *R1, Record *R2) { // Return true if R1 is less than R2
161 for (unsigned i = BitBegin; i != BitEnd; ++i) {
162 bool V1 = getBitValue(R1, i), V2 = getBitValue(R2, i);
172 static void PrintRange(std::vector<Record*>::iterator I,
173 std::vector<Record*>::iterator E) {
174 while (I != E) std::cerr << **I++;
177 static bool getMemoryBit(unsigned char *M, unsigned i) {
178 return (M[i/8] & (1 << (i&7))) != 0;
181 static unsigned getFirstFixedBitInSequence(std::vector<Record*>::iterator IB,
182 std::vector<Record*>::iterator IE,
184 unsigned FirstFixedBit = 0;
185 for (std::vector<Record*>::iterator I = IB; I != IE; ++I)
186 FirstFixedBit = std::max(FirstFixedBit, getFirstFixedBit(*I, StartBit));
187 return FirstFixedBit;
190 // ParseMachineCode - Try to split the vector of instructions (which is
191 // intentially taken by-copy) in half, narrowing down the possible instructions
192 // that we may have found. Eventually, this list will get pared down to zero or
193 // one instruction, in which case we have a match or failure.
195 static Record *ParseMachineCode(std::vector<Record*>::iterator InstsB,
196 std::vector<Record*>::iterator InstsE,
198 assert(InstsB != InstsE && "Empty range?");
199 if (InstsB+1 == InstsE) {
200 // Only a single instruction, see if we match it...
201 Record *Inst = *InstsB;
202 for (unsigned i = 0, e = getNumBits(Inst); i != e; ++i)
203 if (BitInit *BI = dynamic_cast<BitInit*>(getBit(Inst, i)))
204 if (getMemoryBit(M, i) != BI->getValue())
205 throw std::string("Parse failed!\n");
209 unsigned MaxBits = ~0;
210 for (std::vector<Record*>::iterator I = InstsB; I != InstsE; ++I)
211 MaxBits = std::min(MaxBits, getNumBits(*I));
213 unsigned FirstFixedBit = getFirstFixedBitInSequence(InstsB, InstsE, 0);
214 unsigned FirstVaryingBit, LastFixedBit;
216 FirstVaryingBit = ~0;
218 for (std::vector<Record*>::iterator I = InstsB+1; I != InstsE; ++I)
219 FindInstDifferences(*InstsB, *I, FirstFixedBit, MaxBits,
220 FirstVaryingBit, LastFixedBit);
221 if (FirstVaryingBit == MaxBits) {
222 std::cerr << "ERROR: Could not find bit to distinguish between "
223 << "the following entries!\n";
224 PrintRange(InstsB, InstsE);
228 std::cerr << "FVB: " << FirstVaryingBit << " - " << LastFixedBit
229 << ": " << InstsE-InstsB << "\n";
232 FirstFixedBit = getFirstFixedBitInSequence(InstsB, InstsE, FirstVaryingBit);
233 } while (FirstVaryingBit != FirstFixedBit);
235 //std::cerr << "\n\nXXXXXXXXXXXXXXXXX\n\n";
236 //PrintRange(InstsB, InstsE);
238 // Sort the Insts list so that the entries have all of the bits in the range
239 // [FirstVaryingBit,LastFixedBit) sorted. These bits are all guaranteed to be
240 // set to either 0 or 1 (BitInit values), which simplifies things.
242 std::sort(InstsB, InstsE, BitComparator(FirstVaryingBit, LastFixedBit));
244 // Once the list is sorted by these bits, split the bit list into smaller
245 // lists, and recurse on each one.
247 std::vector<Record*>::iterator RangeBegin = InstsB;
249 while (RangeBegin != InstsE) {
250 std::vector<Record*>::iterator RangeEnd = RangeBegin+1;
251 while (RangeEnd != InstsE &&
252 BitRangesEqual(*RangeBegin, *RangeEnd, FirstVaryingBit, LastFixedBit))
255 // We just identified a range of equal instructions. If this range is the
256 // input range, we were not able to distinguish between the instructions in
257 // the set. Print an error and exit!
259 if (RangeBegin == InstsB && RangeEnd == InstsE) {
260 std::cerr << "Error: Could not distinguish among the following insts!:\n";
261 PrintRange(InstsB, InstsE);
266 std::cerr << "FVB: " << FirstVaryingBit << " - " << LastFixedBit
267 << ": [" << RangeEnd-RangeBegin << "] - ";
268 for (int i = LastFixedBit-1; i >= (int)FirstVaryingBit; --i)
269 std::cerr << (int)((BitInit*)getBit(*RangeBegin, i))->getValue() << " ";
273 if (Record *R = ParseMachineCode(RangeBegin, RangeEnd, M)) {
275 std::cerr << "Error: Multiple matches found:\n";
276 PrintRange(InstsB, InstsE);
279 assert(Match == 0 && "Multiple matches??");
282 RangeBegin = RangeEnd;
288 static void PrintValue(Record *I, unsigned char *Ptr, const RecordVal &Val) {
289 assert(dynamic_cast<BitsInit*>(Val.getValue()) &&
290 "Can only handle undefined bits<> types!");
291 BitsInit *BI = (BitsInit*)Val.getValue();
292 assert(BI->getNumBits() <= 32 && "Can only handle fields up to 32 bits!");
295 const std::vector<RecordVal> &Vals = I->getValues();
297 // Start by filling in fixed values...
298 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
299 if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(i)))
300 Value |= B->getValue() << i;
302 // Loop over all of the fields in the instruction adding in any
303 // contributions to this value (due to bit references).
306 for (unsigned f = 0, e = Vals.size(); f != e; ++f)
307 if (Vals[f].getPrefix()) {
308 BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue();
309 if (&Vals[f] == &Val) {
310 // Read the bits directly now...
311 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
312 Value |= getMemoryBit(Ptr, Offset+i) << i;
316 // Scan through the field looking for bit initializers of the current
318 for (unsigned i = 0, e = FieldInitializer->getNumBits(); i != e; ++i)
319 if (VarBitInit *VBI =
320 dynamic_cast<VarBitInit*>(FieldInitializer->getBit(i))) {
321 TypedInit *TI = VBI->getVariable();
322 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
323 if (VI->getName() == Val.getName())
324 Value |= getMemoryBit(Ptr, Offset+i) << VBI->getBitNum();
325 } else if (FieldInit *FI = dynamic_cast<FieldInit*>(TI)) {
326 // FIXME: implement this!
327 std::cerr << "FIELD INIT not implemented yet!\n";
330 Offset += FieldInitializer->getNumBits();
333 std::cout << "0x" << std::hex << Value << std::dec;
336 static void PrintInstruction(Record *I, unsigned char *Ptr) {
337 std::cout << "Inst " << getNumBits(I)/8 << " bytes: "
338 << "\t" << I->getName() << "\t" << *I->getValue("Name")->getValue()
341 const std::vector<RecordVal> &Vals = I->getValues();
342 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
343 if (!Vals[i].getValue()->isComplete()) {
344 std::cout << Vals[i].getName() << "=";
345 PrintValue(I, Ptr, Vals[i]);
349 std::cout << "\n";// << *I;
352 static void ParseMachineCode() {
354 unsigned char Buffer[] = {
356 0x89, 0xE5, // mov EBP, ESP
357 //0x83, 0xEC, 0x08, // sub ESP, 0x8
358 0xE8, 1, 2, 3, 4, // call +0x04030201
359 0x89, 0xEC, // mov ESP, EBP
364 0x89, 0xF6, // mov ESI, ESI
365 0x68, 1, 2, 3, 4, // push 0x04030201
367 0xFF, 0xD0, // call EAX
368 0xB8, 1, 2, 3, 4, // mov EAX, 0x04030201
369 0x85, 0xC0, // test EAX, EAX
375 unsigned char Buffer[] = { 0xbf, 0xe0, 0x20, 0x1f, 0x1, 0x0, 0x0, 0x1,
376 0x0, 0x0, 0x0, 0x0, 0xc1, 0x0, 0x20, 0x1, 0x1,
377 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
378 0x0, 0x0, 0x0, 0x0, 0x0, 0x40, 0x0, 0x0, 0x0, 0x1,
379 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
380 0x0, 0x0, 0xaf, 0xe8, 0x20, 0x17
384 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
386 unsigned char *BuffPtr = Buffer;
388 Record *R = ParseMachineCode(Insts.begin(), Insts.end(), BuffPtr);
389 PrintInstruction(R, BuffPtr);
391 unsigned Bits = getNumBits(R);
392 assert((Bits & 7) == 0 && "Instruction is not an even number of bytes!");
398 int main(int argc, char **argv) {
399 cl::ParseCommandLineOptions(argc, argv);
400 ParseFile(InputFilename);
402 std::ostream *Out = &std::cout;
403 if (OutputFilename != "-") {
404 // Output to a .tmp file, because we don't actually want to overwrite the
405 // output file unless the generated file is different or the specified file
407 Out = new std::ofstream((OutputFilename+".tmp").c_str());
410 std::cerr << argv[0] << ": error opening " << OutputFilename << ".tmp!\n";
414 // Make sure the file gets removed if *gasp* tablegen crashes...
415 RemoveFileOnSignal(OutputFilename+".tmp");
421 *Out << Records; // No argument, dump all contents
427 CodeEmitterGen(Records).run(*Out);
430 case GenRegisterEnums:
431 RegisterInfoEmitter(Records).runEnums(*Out);
434 RegisterInfoEmitter(Records).run(*Out);
436 case GenRegisterHeader:
437 RegisterInfoEmitter(Records).runHeader(*Out);
441 InstrInfoEmitter(Records).runEnums(*Out);
444 InstrInfoEmitter(Records).run(*Out);
446 case GenInstrSelector:
447 InstrSelectorEmitter(Records).run(*Out);
450 std::vector<Record*> Recs = Records.getAllDerivedDefinitions(Class);
451 for (unsigned i = 0, e = Recs.size(); i != e; ++i)
452 *Out << Recs[i] << ", ";
456 } catch (const std::string &Error) {
457 std::cerr << Error << "\n";
458 if (Out != &std::cout) {
459 delete Out; // Close the file
460 std::remove(OutputFilename.c_str()); // Remove the file, it's broken
465 if (Out != &std::cout) {
466 delete Out; // Close the file
468 // Now that we have generated the result, check to see if we either don't
469 // have the requested file, or if the requested file is different than the
470 // file we generated. If so, move the generated file over the requested
471 // file. Otherwise, just remove the file we just generated, so 'make'
472 // doesn't try to regenerate tons of dependencies.
474 MoveFileOverIfUpdated(OutputFilename+".tmp", OutputFilename);