catch unnamed inputs
[oota-llvm.git] / utils / TableGen / TableGen.cpp
index c05ccb0bb17cda99b5b2a86e679089284f5e25fa..baa15eb6df459efaa7897750ba5573159e0ef329 100644 (file)
@@ -1,10 +1,10 @@
 //===- TableGen.cpp - Top-Level TableGen implementation -------------------===//
-// 
+//
 //                     The LLVM Compiler Infrastructure
 //
 // This file was developed by the LLVM research group and is distributed under
 // the University of Illinois Open Source License. See LICENSE.TXT for details.
-// 
+//
 //===----------------------------------------------------------------------===//
 //
 // TableGen is a tool which can be used to build up a description of something,
 //===----------------------------------------------------------------------===//
 
 #include "Record.h"
-#include "Support/CommandLine.h"
-#include "Support/Signals.h"
-#include "Support/FileUtilities.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/System/Signals.h"
+#include "llvm/Support/FileUtilities.h"
 #include "CodeEmitterGen.h"
 #include "RegisterInfoEmitter.h"
 #include "InstrInfoEmitter.h"
+#include "AsmWriterEmitter.h"
 #include "InstrSelectorEmitter.h"
+#include "DAGISelEmitter.h"
 #include <algorithm>
 #include <cstdio>
 #include <fstream>
-
-namespace llvm {
+using namespace llvm;
 
 enum ActionType {
   PrintRecords,
   GenEmitter,
   GenRegisterEnums, GenRegister, GenRegisterHeader,
-  GenInstrEnums, GenInstrs, GenInstrSelector,
+  GenInstrEnums, GenInstrs, GenAsmWriter, GenInstrSelector,
+  GenDAGISel,
   PrintEnums,
-  Parse,
+  Parse
 };
 
 namespace {
@@ -55,13 +57,17 @@ namespace {
                                "Generate enum values for instructions"),
                     clEnumValN(GenInstrs, "gen-instr-desc",
                                "Generate instruction descriptions"),
+                    clEnumValN(GenAsmWriter, "gen-asm-writer",
+                               "Generate assembly writer"),
                     clEnumValN(GenInstrSelector, "gen-instr-selector",
                                "Generate an instruction selector"),
+                    clEnumValN(GenDAGISel, "gen-dag-isel",
+                               "Generate a DAG instruction selector"),
                     clEnumValN(PrintEnums, "print-enums",
                                "Print enum values for a class"),
                     clEnumValN(Parse, "parse",
                                "Interpret machine code (testing only)"),
-                    0));
+                    clEnumValEnd));
 
   cl::opt<std::string>
   Class("class", cl::desc("Print Enum list for this class"),
@@ -79,25 +85,27 @@ namespace {
                   cl::value_desc("directory"), cl::init(""));
 }
 
+namespace llvm {
+  void ParseFile(const std::string &Filename,
+                 const std::string &IncludeDir);
+}
 
-void ParseFile(const std::string &Filename, const std::string & IncludeDir);
-
-RecordKeeper Records;
+RecordKeeper llvm::Records;
 
 static Init *getBit(Record *R, unsigned BitNo) {
   const std::vector<RecordVal> &V = R->getValues();
   for (unsigned i = 0, e = V.size(); i != e; ++i)
     if (V[i].getPrefix()) {
       assert(dynamic_cast<BitsInit*>(V[i].getValue()) &&
-            "Can only handle fields of bits<> type!");
+             "Can only handle fields of bits<> type!");
       BitsInit *I = (BitsInit*)V[i].getValue();
       if (BitNo < I->getNumBits())
-       return I->getBit(BitNo);
+        return I->getBit(BitNo);
       BitNo -= I->getNumBits();
     }
 
   std::cerr << "Cannot find requested bit!\n";
-  abort();
+  exit(1);
   return 0;
 }
 
@@ -107,7 +115,7 @@ static unsigned getNumBits(Record *R) {
   for (unsigned i = 0, e = V.size(); i != e; ++i)
     if (V[i].getPrefix()) {
       assert(dynamic_cast<BitsInit*>(V[i].getValue()) &&
-            "Can only handle fields of bits<> type!");
+             "Can only handle fields of bits<> type!");
       Num += ((BitsInit*)V[i].getValue())->getNumBits();
     }
   return Num;
@@ -126,7 +134,7 @@ static bool BitsAreEqual(Record *I1, Record *I2, unsigned BitNo) {
 }
 
 static bool BitRangesEqual(Record *I1, Record *I2,
-                          unsigned Start, unsigned End) {
+                           unsigned Start, unsigned End) {
   for (unsigned i = Start; i != End; ++i)
     if (!BitsAreEqual(I1, I2, i))
       return false;
@@ -141,9 +149,9 @@ static unsigned getFirstFixedBit(Record *R, unsigned FirstFixedBit) {
 }
 
 static void FindInstDifferences(Record *I1, Record *I2,
-                               unsigned FirstFixedBit, unsigned MaxBits,
-                               unsigned &FirstVaryingBitOverall,
-                               unsigned &LastFixedBitOverall) {
+                                unsigned FirstFixedBit, unsigned MaxBits,
+                                unsigned &FirstVaryingBitOverall,
+                                unsigned &LastFixedBitOverall) {
   // Compare the first instruction to the rest of the instructions, looking for
   // fields that differ.
   //
@@ -154,7 +162,7 @@ static void FindInstDifferences(Record *I1, Record *I2,
   unsigned LastFixedBit = FirstVaryingBit;
   while (LastFixedBit < MaxBits && BitsAreFixed(I1, I2, LastFixedBit))
     ++LastFixedBit;
-  
+
   if (FirstVaryingBit < FirstVaryingBitOverall)
     FirstVaryingBitOverall = FirstVaryingBit;
   if (LastFixedBit < LastFixedBitOverall)
@@ -175,16 +183,16 @@ struct BitComparator {
     for (unsigned i = BitBegin; i != BitEnd; ++i) {
       bool V1 = getBitValue(R1, i), V2 = getBitValue(R2, i);
       if (V1 < V2)
-       return true;
+        return true;
       else if (V2 < V1)
-       return false;
+        return false;
     }
     return false;
   }
 };
 
-static void PrintRange(std::vector<Record*>::iterator I, 
-                      std::vector<Record*>::iterator E) {
+static void PrintRange(std::vector<Record*>::iterator I,
+                       std::vector<Record*>::iterator E) {
   while (I != E) std::cerr << **I++;
 }
 
@@ -193,8 +201,8 @@ static bool getMemoryBit(unsigned char *M, unsigned i) {
 }
 
 static unsigned getFirstFixedBitInSequence(std::vector<Record*>::iterator IB,
-                                          std::vector<Record*>::iterator IE,
-                                          unsigned StartBit) {
+                                           std::vector<Record*>::iterator IE,
+                                           unsigned StartBit) {
   unsigned FirstFixedBit = 0;
   for (std::vector<Record*>::iterator I = IB; I != IE; ++I)
     FirstFixedBit = std::max(FirstFixedBit, getFirstFixedBit(*I, StartBit));
@@ -206,17 +214,17 @@ static unsigned getFirstFixedBitInSequence(std::vector<Record*>::iterator IB,
 // instructions that we may have found.  Eventually, this list will get pared
 // down to zero or one instruction, in which case we have a match or failure.
 //
-static Record *ParseMachineCode(std::vector<Record*>::iterator InstsB, 
-                               std::vector<Record*>::iterator InstsE,
-                               unsigned char *M) {
+static Record *ParseMachineCode(std::vector<Record*>::iterator InstsB,
+                                std::vector<Record*>::iterator InstsE,
+                                unsigned char *M) {
   assert(InstsB != InstsE && "Empty range?");
   if (InstsB+1 == InstsE) {
     // Only a single instruction, see if we match it...
     Record *Inst = *InstsB;
     for (unsigned i = 0, e = getNumBits(Inst); i != e; ++i)
       if (BitInit *BI = dynamic_cast<BitInit*>(getBit(Inst, i)))
-       if (getMemoryBit(M, i) != BI->getValue())
-         throw std::string("Parse failed!\n");
+        if (getMemoryBit(M, i) != BI->getValue())
+          throw std::string("Parse failed!\n");
     return Inst;
   }
 
@@ -231,16 +239,16 @@ static Record *ParseMachineCode(std::vector<Record*>::iterator InstsB,
     LastFixedBit = ~0;
     for (std::vector<Record*>::iterator I = InstsB+1; I != InstsE; ++I)
       FindInstDifferences(*InstsB, *I, FirstFixedBit, MaxBits,
-                         FirstVaryingBit, LastFixedBit);
+                          FirstVaryingBit, LastFixedBit);
     if (FirstVaryingBit == MaxBits) {
       std::cerr << "ERROR: Could not find bit to distinguish between "
-               << "the following entries!\n";
+                << "the following entries!\n";
       PrintRange(InstsB, InstsE);
     }
 
 #if 0
     std::cerr << "FVB: " << FirstVaryingBit << " - " << LastFixedBit
-             << ": " << InstsE-InstsB << "\n";
+              << ": " << InstsE-InstsB << "\n";
 #endif
 
     FirstFixedBit = getFirstFixedBitInSequence(InstsB, InstsE, FirstVaryingBit);
@@ -265,7 +273,7 @@ static Record *ParseMachineCode(std::vector<Record*>::iterator InstsB,
     while (RangeEnd != InstsE &&
           BitRangesEqual(*RangeBegin, *RangeEnd, FirstVaryingBit, LastFixedBit))
       ++RangeEnd;
-    
+
     // We just identified a range of equal instructions.  If this range is the
     // input range, we were not able to distinguish between the instructions in
     // the set.  Print an error and exit!
@@ -273,12 +281,12 @@ static Record *ParseMachineCode(std::vector<Record*>::iterator InstsB,
     if (RangeBegin == InstsB && RangeEnd == InstsE) {
       std::cerr << "Error: Could not distinguish among the following insts!:\n";
       PrintRange(InstsB, InstsE);
-      abort();
+      exit(1);
     }
-    
+
 #if 0
     std::cerr << "FVB: " << FirstVaryingBit << " - " << LastFixedBit
-             << ": [" << RangeEnd-RangeBegin << "] - ";
+              << ": [" << RangeEnd-RangeBegin << "] - ";
     for (int i = LastFixedBit-1; i >= (int)FirstVaryingBit; --i)
       std::cerr << (int)((BitInit*)getBit(*RangeBegin, i))->getValue() << " ";
     std::cerr << "\n";
@@ -286,8 +294,8 @@ static Record *ParseMachineCode(std::vector<Record*>::iterator InstsB,
 
     if (Record *R = ParseMachineCode(RangeBegin, RangeEnd, M)) {
       if (Match) {
-       std::cerr << "Error: Multiple matches found:\n";
-       PrintRange(InstsB, InstsE);
+        std::cerr << "Error: Multiple matches found:\n";
+        PrintRange(InstsB, InstsE);
       }
 
       assert(Match == 0 && "Multiple matches??");
@@ -301,7 +309,7 @@ static Record *ParseMachineCode(std::vector<Record*>::iterator InstsB,
 
 static void PrintValue(Record *I, unsigned char *Ptr, const RecordVal &Val) {
   assert(dynamic_cast<BitsInit*>(Val.getValue()) &&
-        "Can only handle undefined bits<> types!");
+         "Can only handle undefined bits<> types!");
   BitsInit *BI = (BitsInit*)Val.getValue();
   assert(BI->getNumBits() <= 32 && "Can only handle fields up to 32 bits!");
 
@@ -312,7 +320,7 @@ static void PrintValue(Record *I, unsigned char *Ptr, const RecordVal &Val) {
   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).
   //
@@ -321,17 +329,17 @@ static void PrintValue(Record *I, unsigned char *Ptr, const RecordVal &Val) {
     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;
+        // 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))) {
+        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())
@@ -340,7 +348,7 @@ static void PrintValue(Record *I, unsigned char *Ptr, const RecordVal &Val) {
             // FIXME: implement this!
             std::cerr << "FIELD INIT not implemented yet!\n";
           }
-       }       
+        }
       Offset += FieldInitializer->getNumBits();
     }
 
@@ -349,9 +357,9 @@ static void PrintValue(Record *I, unsigned char *Ptr, const RecordVal &Val) {
 
 static void PrintInstruction(Record *I, unsigned char *Ptr) {
   std::cout << "Inst " << getNumBits(I)/8 << " bytes: "
-           << "\t" << I->getName() << "\t" << *I->getValue("Name")->getValue()
-           << "\t";
-  
+            << "\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()) {
@@ -359,7 +367,7 @@ static void PrintInstruction(Record *I, unsigned char *Ptr) {
       PrintValue(I, Ptr, Vals[i]);
       std::cout << "\t";
     }
-  
+
   std::cout << "\n";// << *I;
 }
 
@@ -367,26 +375,26 @@ 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
+                             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, 
+  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,
@@ -408,28 +416,21 @@ static void ParseMachineCode() {
   }
 }
 
-} // End llvm namespace
-
-using namespace llvm;
-
 int main(int argc, char **argv) {
   cl::ParseCommandLineOptions(argc, argv);
   ParseFile(InputFilename, IncludeDir);
 
   std::ostream *Out = &std::cout;
   if (OutputFilename != "-") {
-    // Output to a .tmp file, because we don't actually want to overwrite the
-    // output file unless the generated file is different or the specified file
-    // does not exist.
-    Out = new std::ofstream((OutputFilename+".tmp").c_str());
+    Out = new std::ofstream(OutputFilename.c_str());
 
     if (!Out->good()) {
-      std::cerr << argv[0] << ": error opening " << OutputFilename << ".tmp!\n";
+      std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
       return 1;
     }
 
     // Make sure the file gets removed if *gasp* tablegen crashes...
-    RemoveFileOnSignal(OutputFilename+".tmp");
+    sys::RemoveFileOnSignal(sys::Path(OutputFilename));
   }
 
   try {
@@ -460,14 +461,22 @@ int main(int argc, char **argv) {
     case GenInstrs:
       InstrInfoEmitter(Records).run(*Out);
       break;
+
+    case GenAsmWriter:
+      AsmWriterEmitter(Records).run(*Out);
+      break;
+
     case GenInstrSelector:
       InstrSelectorEmitter(Records).run(*Out);
       break;
+    case GenDAGISel:
+      DAGISelEmitter(Records).run(*Out);
+      break;
     case PrintEnums:
     {
       std::vector<Record*> Recs = Records.getAllDerivedDefinitions(Class);
       for (unsigned i = 0, e = Recs.size(); i != e; ++i)
-        *Out << Recs[i] << ", ";
+        *Out << Recs[i]->getName() << ", ";
       *Out << "\n";
       break;
     }
@@ -476,24 +485,23 @@ int main(int argc, char **argv) {
       return 1;
     }
   } catch (const std::string &Error) {
-    std::cerr << Error << "\n";
+    std::cerr << argv[0] << ": " << Error << "\n";
     if (Out != &std::cout) {
       delete Out;                             // Close the file
       std::remove(OutputFilename.c_str());    // Remove the file, it's broken
     }
     return 1;
+  } catch (...) {
+    std::cerr << argv[0] << ": Unknown unexpected exception occurred.\n";
+    if (Out != &std::cout) {
+      delete Out;                             // Close the file
+      std::remove(OutputFilename.c_str());    // Remove the file, it's broken
+    }
+    return 2;
   }
 
   if (Out != &std::cout) {
     delete Out;                               // Close the file
-    
-    // Now that we have generated the result, check to see if we either don't
-    // have the requested file, or if the requested file is different than the
-    // file we generated.  If so, move the generated file over the requested
-    // file.  Otherwise, just remove the file we just generated, so 'make'
-    // doesn't try to regenerate tons of dependencies.
-    //
-    MoveFileOverIfUpdated(OutputFilename+".tmp", OutputFilename);
   }
   return 0;
 }