Changes For Bug 352
[oota-llvm.git] / utils / TableGen / TableGen.cpp
index c05ccb0bb17cda99b5b2a86e679089284f5e25fa..15b313191e5c93238e11b4de932218f95f03bb9e 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #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 <algorithm>
 #include <cstdio>
 #include <fstream>
-
-namespace llvm {
+using namespace llvm;
 
 enum ActionType {
   PrintRecords,
   GenEmitter,
   GenRegisterEnums, GenRegister, GenRegisterHeader,
-  GenInstrEnums, GenInstrs, GenInstrSelector,
+  GenInstrEnums, GenInstrs, GenAsmWriter, GenInstrSelector,
   PrintEnums,
-  Parse,
+  Parse
 };
 
 namespace {
@@ -55,13 +55,15 @@ 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(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,10 +81,12 @@ 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();
@@ -97,7 +101,7 @@ static Init *getBit(Record *R, unsigned BitNo) {
     }
 
   std::cerr << "Cannot find requested bit!\n";
-  abort();
+  exit(1);
   return 0;
 }
 
@@ -273,7 +277,7 @@ 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
@@ -408,28 +412,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(OutputFilename);
   }
 
   try {
@@ -460,6 +457,11 @@ 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;
@@ -467,7 +469,7 @@ int main(int argc, char **argv) {
     {
       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;
     }
@@ -486,14 +488,6 @@ int main(int argc, char **argv) {
 
   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;
 }