Add the new InstrSched directory.
[oota-llvm.git] / lib / Target / SparcV9 / EmitBytecodeToAssembly.cpp
1 //===-- EmitBytecodeToAssembly.cpp - Emit bytecode to SparcV9 .s File ------==//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the pass that writes LLVM bytecode as data to a sparc
11 // assembly file.  The bytecode gets assembled into a special bytecode section
12 // of the executable for use at runtime later.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "SparcV9Internals.h"
17 #include "llvm/Pass.h"
18 #include "llvm/Bytecode/Writer.h"
19 #include <iostream>
20 using namespace llvm;
21
22 namespace {
23
24   // sparcasmbuf - stream buf for encoding output bytes as .byte directives for
25   // the sparc assembler.
26   //
27   class sparcasmbuf : public std::streambuf {
28     std::ostream &BaseStr;
29   public:
30     typedef char           char_type;
31     typedef int            int_type;
32     typedef std::streampos pos_type;
33     typedef std::streamoff off_type;
34     
35     sparcasmbuf(std::ostream &On) : BaseStr(On) {}
36
37     virtual int_type overflow(int_type C) {
38       if (C != EOF)
39         BaseStr << "\t.byte " << C << "\n"; // Output C;
40       return C;
41     }
42   };
43
44
45   // osparcasmstream - Define an ostream implementation that uses a sparcasmbuf
46   // as the underlying streambuf to write the data to.  This streambuf formats
47   // the output as .byte directives for sparc output.
48   //
49   class osparcasmstream : public std::ostream {
50     sparcasmbuf sb;
51   public:
52     typedef char           char_type;
53     typedef int            int_type;
54     typedef std::streampos pos_type;
55     typedef std::streamoff off_type;
56
57     explicit osparcasmstream(std::ostream &On) : std::ostream(&sb), sb(On) { }
58
59     sparcasmbuf *rdbuf() const {
60       return const_cast<sparcasmbuf*>(&sb);
61     }
62   };
63
64   static void writePrologue (std::ostream &Out, const std::string &comment,
65                              const std::string &symName) {
66     // Prologue:
67     // Output a comment describing the object.
68     Out << "!" << comment << "\n";   
69     // Switch the current section to .rodata in the assembly output:
70     Out << "\t.section \".rodata\"\n\t.align 8\n";  
71     // Output a global symbol naming the object:
72     Out << "\t.global " << symName << "\n";    
73     Out << "\t.type " << symName << ",#object\n"; 
74     Out << symName << ":\n"; 
75   }
76
77   static void writeEpilogue (std::ostream &Out, const std::string &symName) {
78     // Epilogue:
79     // Output a local symbol marking the end of the object:
80     Out << ".end_" << symName << ":\n";    
81     // Output size directive giving the size of the object:
82     Out << "\t.size " << symName << ", .end_" << symName << "-" << symName
83         << "\n";
84   }
85
86   // SparcV9BytecodeWriter - Write bytecode out to a stream that is sparc'ified
87   class SparcV9BytecodeWriter : public ModulePass {
88     std::ostream &Out;
89   public:
90     SparcV9BytecodeWriter(std::ostream &out) : Out(out) {}
91
92     const char *getPassName() const { return "Emit Bytecode to SparcV9 Assembly";}
93     
94     virtual bool runOnModule(Module &M) {
95       // Write an object containing the bytecode to the SPARC assembly stream
96       writePrologue (Out, "LLVM BYTECODE OUTPUT", "LLVMBytecode");
97       osparcasmstream OS(Out);
98       WriteBytecodeToFile(&M, OS);
99       writeEpilogue (Out, "LLVMBytecode");
100
101       // Write an object containing its length as an integer to the
102       // SPARC assembly stream
103       writePrologue (Out, "LLVM BYTECODE LENGTH", "llvm_length");
104       Out <<"\t.word\t.end_LLVMBytecode-LLVMBytecode\n"; 
105       writeEpilogue (Out, "llvm_length");
106
107       return false;
108     }
109   };
110 }  // end anonymous namespace
111
112 ModulePass *llvm::createBytecodeAsmPrinterPass(std::ostream &Out) {
113   return new SparcV9BytecodeWriter(Out);
114 }
115