[llvm-commits] CVS: llvm/tools/llvm2cpp/CppWriter.cpp

Reid Spencer reid at x10sys.com
Tue May 30 21:43:31 PDT 2006



Changes in directory llvm/tools/llvm2cpp:

CppWriter.cpp updated: 1.6 -> 1.7
---
Log message:

Don't generate module definitions when the -fragment option is given.


---
Diffs of the changes:  (+59 -51)

 CppWriter.cpp |  110 +++++++++++++++++++++++++++++++---------------------------
 1 files changed, 59 insertions(+), 51 deletions(-)


Index: llvm/tools/llvm2cpp/CppWriter.cpp
diff -u llvm/tools/llvm2cpp/CppWriter.cpp:1.6 llvm/tools/llvm2cpp/CppWriter.cpp:1.7
--- llvm/tools/llvm2cpp/CppWriter.cpp:1.6	Tue May 30 16:18:23 2006
+++ llvm/tools/llvm2cpp/CppWriter.cpp	Tue May 30 23:43:19 2006
@@ -71,7 +71,8 @@
 
   const Module* getModule() { return TheModule; }
 
-  void printModule(const Module *M);
+  void printModule();
+  void printFragment();
 
 private:
   void printTypes(const Module* M);
@@ -321,60 +322,23 @@
   return TypeNames[Ty] = name;
 }
 
-void CppWriter::printModule(const Module *M) {
-  Out << "\n// Module Construction\n";
-  Out << "Module* mod = new Module(\"";
-  if (!ModName.empty())
-    printEscapedString(ModName);
-  else if (M->getModuleIdentifier() == "-")
-    printEscapedString("<stdin>");
-  else 
-    printEscapedString(M->getModuleIdentifier());
-  Out << "\");\n";
-  Out << "mod->setEndianness(";
-  switch (M->getEndianness()) {
-    case Module::LittleEndian: Out << "Module::LittleEndian);\n"; break;
-    case Module::BigEndian:    Out << "Module::BigEndian);\n";    break;
-    case Module::AnyEndianness:Out << "Module::AnyEndianness);\n";  break;
-  }
-  Out << "mod->setPointerSize(";
-  switch (M->getPointerSize()) {
-    case Module::Pointer32:      Out << "Module::Pointer32);\n"; break;
-    case Module::Pointer64:      Out << "Module::Pointer64);\n"; break;
-    case Module::AnyPointerSize: Out << "Module::AnyPointerSize);\n"; break;
-  }
-  if (!M->getTargetTriple().empty())
-    Out << "mod->setTargetTriple(\"" << M->getTargetTriple() << "\");\n";
-
-  if (!M->getModuleInlineAsm().empty()) {
-    Out << "mod->setModuleInlineAsm(\"";
-    printEscapedString(M->getModuleInlineAsm());
-    Out << "\");\n";
-  }
-  
-  // Loop over the dependent libraries and emit them.
-  Module::lib_iterator LI = M->lib_begin();
-  Module::lib_iterator LE = M->lib_end();
-  while (LI != LE) {
-    Out << "mod->addLibrary(\"" << *LI << "\");\n";
-    ++LI;
-  }
-
+void CppWriter::printFragment() {
   // Print out all the type definitions
   Out << "\n// Type Definitions\n";
-  printTypes(M);
+  printTypes(TheModule);
 
   // Functions can call each other and global variables can reference them so 
   // define all the functions first before emitting their function bodies.
   Out << "\n// Function Declarations\n";
-  for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
+  for (Module::const_iterator I = TheModule->begin(), E = TheModule->end(); 
+       I != E; ++I)
     printFunctionHead(I);
 
   // Process the global variables declarations. We can't initialze them until
   // after the constants are printed so just print a header for each global
   Out << "\n// Global Variable Declarations\n";
-  for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
-       I != E; ++I) {
+  for (Module::const_global_iterator I = TheModule->global_begin(), 
+       E = TheModule->global_end(); I != E; ++I) {
     printGlobalHead(I);
   }
 
@@ -382,20 +346,21 @@
   // through GlobalValues. All GlobalValues have been declared at this point
   // so we can proceed to generate the constants.
   Out << "\n// Constant Definitions\n";
-  printConstants(M);
+  printConstants(TheModule);
 
   // Process the global variables definitions now that all the constants have
   // been emitted. These definitions just couple the gvars with their constant
   // initializers.
   Out << "\n// Global Variable Definitions\n";
-  for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
-       I != E; ++I) {
+  for (Module::const_global_iterator I = TheModule->global_begin(), 
+       E = TheModule->global_end(); I != E; ++I) {
     printGlobalBody(I);
   }
 
   // Finally, we can safely put out all of the function bodies.
   Out << "\n// Function Definitions\n";
-  for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
+  for (Module::const_iterator I = TheModule->begin(), E = TheModule->end(); 
+       I != E; ++I) {
     if (!I->isExternal()) {
       Out << "\n// Function: " << I->getName() << " (" << getCppName(I) 
           << ")\n{\n";
@@ -405,6 +370,48 @@
   }
 }
 
+void CppWriter::printModule() {
+  Out << "\n// Module Construction\n";
+  Out << "Module* mod = new Module(\"";
+  if (!ModName.empty())
+    printEscapedString(ModName);
+  else if (TheModule->getModuleIdentifier() == "-")
+    printEscapedString("<stdin>");
+  else 
+    printEscapedString(TheModule->getModuleIdentifier());
+  Out << "\");\n";
+  Out << "mod->setEndianness(";
+  switch (TheModule->getEndianness()) {
+    case Module::LittleEndian: Out << "Module::LittleEndian);\n"; break;
+    case Module::BigEndian:    Out << "Module::BigEndian);\n";    break;
+    case Module::AnyEndianness:Out << "Module::AnyEndianness);\n";  break;
+  }
+  Out << "mod->setPointerSize(";
+  switch (TheModule->getPointerSize()) {
+    case Module::Pointer32:      Out << "Module::Pointer32);\n"; break;
+    case Module::Pointer64:      Out << "Module::Pointer64);\n"; break;
+    case Module::AnyPointerSize: Out << "Module::AnyPointerSize);\n"; break;
+  }
+  if (!TheModule->getTargetTriple().empty())
+    Out << "mod->setTargetTriple(\"" << TheModule->getTargetTriple() 
+        << "\");\n";
+
+  if (!TheModule->getModuleInlineAsm().empty()) {
+    Out << "mod->setModuleInlineAsm(\"";
+    printEscapedString(TheModule->getModuleInlineAsm());
+    Out << "\");\n";
+  }
+  
+  // Loop over the dependent libraries and emit them.
+  Module::lib_iterator LI = TheModule->lib_begin();
+  Module::lib_iterator LE = TheModule->lib_end();
+  while (LI != LE) {
+    Out << "mod->addLibrary(\"" << *LI << "\");\n";
+    ++LI;
+  }
+  printFragment();
+}
+
 void
 CppWriter::printCallingConv(unsigned cc){
   // Print the calling convention.
@@ -1303,13 +1310,14 @@
 namespace llvm {
 
 void WriteModuleToCppFile(Module* mod, std::ostream& o) {
+  o << "// Generated by llvm2cpp - DO NOT MODIFY!\n\n";
   std::string fname = FuncName.getValue();
   if (fname.empty())
     fname = "makeLLVMModule";
   if (Fragment) {
-    o << "Module* " << fname << "() {\n";
+    o << "Module* " << fname << "(Module *mod) {\n";
     CppWriter W(o, mod);
-    W.printModule(mod);
+    W.printFragment();
     o << "return mod;\n";
     o << "}\n";
   } else {
@@ -1342,7 +1350,7 @@
     o << "}\n\n";
     o << "Module* " << fname << "() {\n";
     CppWriter W(o, mod);
-    W.printModule(mod);
+    W.printModule();
     o << "return mod;\n";
     o << "}\n";
   }






More information about the llvm-commits mailing list