This class is no longer an annotation
[oota-llvm.git] / lib / Target / SparcV9 / EmitBytecodeToAssembly.cpp
index 3ca8b1658236aafee61d68a3c081cf300ab4c26c..0826f76284e7b4ab516c2e9daa56a2a523942012 100644 (file)
@@ -1,4 +1,11 @@
-//===-- EmitBytecodeToAssembly.cpp - Emit bytecode to Sparc .s File --------==//
+//===-- EmitBytecodeToAssembly.cpp - Emit bytecode to SparcV9 .s File ------==//
+// 
+//                     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.
+// 
+//===----------------------------------------------------------------------===//
 //
 // This file implements the pass that writes LLVM bytecode as data to a sparc
 // assembly file.  The bytecode gets assembled into a special bytecode section
 //
 //===----------------------------------------------------------------------===//
 
-#include "SparcInternals.h"
+#include "SparcV9Internals.h"
 #include "llvm/Pass.h"
 #include "llvm/Bytecode/Writer.h"
 #include <iostream>
-#include <sstream>
-#include <string>
+
+namespace llvm {
+
 using std::ostream;
 
 namespace {
@@ -41,7 +49,7 @@ namespace {
   // as the underlying streambuf to write the data to.  This streambuf formats
   // the output as .byte directives for sparc output.
   //
-  class osparcasmstream : public ostream {
+  class osparcasmstream : public std::ostream {
     sparcasmbuf sb;
   public:
     typedef char           char_type;
@@ -49,51 +57,63 @@ namespace {
     typedef std::streampos pos_type;
     typedef std::streamoff off_type;
 
-    explicit osparcasmstream(ostream &On) : ostream(&sb), sb(On) { }
+    explicit osparcasmstream(std::ostream &On) : std::ostream(&sb), sb(On) { }
 
     sparcasmbuf *rdbuf() const {
       return const_cast<sparcasmbuf*>(&sb);
     }
   };
 
-  // SparcBytecodeWriter - Write bytecode out to a stream that is sparc'ified
-  class SparcBytecodeWriter : public Pass {
+  static void writePrologue (std::ostream &Out, const std::string &comment,
+                            const std::string &symName) {
+    // Prologue:
+    // Output a comment describing the object.
+    Out << "!" << comment << "\n";   
+    // Switch the current section to .rodata in the assembly output:
+    Out << "\t.section \".rodata\"\n\t.align 8\n";  
+    // Output a global symbol naming the object:
+    Out << "\t.global " << symName << "\n";    
+    Out << "\t.type " << symName << ",#object\n"; 
+    Out << symName << ":\n"; 
+  }
+
+  static void writeEpilogue (std::ostream &Out, const std::string &symName) {
+    // Epilogue:
+    // Output a local symbol marking the end of the object:
+    Out << ".end_" << symName << ":\n";    
+    // Output size directive giving the size of the object:
+    Out << "\t.size " << symName << ", .end_" << symName << "-" << symName
+       << "\n";
+  }
+
+  // SparcV9BytecodeWriter - Write bytecode out to a stream that is sparc'ified
+  class SparcV9BytecodeWriter : public Pass {
     std::ostream &Out;
   public:
-    SparcBytecodeWriter(std::ostream &out) : Out(out) {}
-
-    const char *getPassName() const { return "Emit Bytecode to Sparc Assembly";}
+    SparcV9BytecodeWriter(std::ostream &out) : Out(out) {}
 
+    const char *getPassName() const { return "Emit Bytecode to SparcV9 Assembly";}
+    
     virtual bool run(Module &M) {
-      // Write bytecode out to the sparc assembly stream
-
-      Out << "\n\n!LLVM BYTECODE OUTPUT\n\t.section \".rodata\"\n\t.align 8\n";
-      Out << "\t.global LLVMBytecode\n\t.type LLVMBytecode,#object\n";
-      Out << "LLVMBytecode:\n";
-      //changed --anand, to get the size of bytecode
-      std::ostringstream Ostr;
-      osparcasmstream OS(Ostr);
+      // Write an object containing the bytecode to the SPARC assembly stream
+      writePrologue (Out, "LLVM BYTECODE OUTPUT", "LLVMBytecode");
+      osparcasmstream OS(Out);
       WriteBytecodeToFile(&M, OS);
+      writeEpilogue (Out, "LLVMBytecode");
+
+      // Write an object containing its length as an integer to the
+      // SPARC assembly stream
+      writePrologue (Out, "LLVM BYTECODE LENGTH", "llvm_length");
+      Out <<"\t.word\t.end_LLVMBytecode-LLVMBytecode\n"; 
+      writeEpilogue (Out, "llvm_length");
 
-      //compute length: count number of "."
-      std::string myString=Ostr.str();
-      int llvm_len=0;
-      for(std::string::iterator si=myString.begin(), se=myString.end(); si!=se; si++)
-       if(*si == '.')
-         llvm_len++;
-      
-      //now put Ostr into Out
-      //count no of 
-      Out<<Ostr.str();
-      Out << ".end_LLVMBytecode:\n";
-      Out << "\t.size LLVMBytecode, .end_LLVMBytecode-LLVMBytecode\n\n";
-
-      Out <<"\n\n!LLVM BYTECODE Length\n\t.section \".data\",#alloc,#write\n\t.global llvm_length\n\t.align 4\n\t.type llvm_length,#object\n\t.size llvm_length,4\nllvm_length:\n\t.word "<<llvm_len<<"\n"; 
       return false;
     }
   };
 }  // end anonymous namespace
 
-Pass *UltraSparc::getEmitBytecodeToAsmPass(std::ostream &Out) {
-  return new SparcBytecodeWriter(Out);
+Pass *createBytecodeAsmPrinterPass(std::ostream &Out) {
+  return new SparcV9BytecodeWriter(Out);
 }
+
+} // End llvm namespace