Add #include that was removed from TargetMachine.h
[oota-llvm.git] / lib / Target / SparcV9 / EmitBytecodeToAssembly.cpp
1 //===-- EmitBytecodeToAssembly.cpp - Emit bytecode to Sparc .s File --------==//
2 //
3 // This file implements the pass that writes LLVM bytecode as data to a sparc
4 // assembly file.  The bytecode gets assembled into a special bytecode section
5 // of the executable for use at runtime later.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "SparcInternals.h"
10 #include "llvm/Pass.h"
11 #include "llvm/Bytecode/Writer.h"
12 #include <iostream>
13
14 namespace {
15
16   // sparcasmbuf - stream buf for encoding output bytes as .byte directives for
17   // the sparc assembler.
18   //
19   class sparcasmbuf : public streambuf {
20     std::ostream &BaseStr;
21   public:
22     typedef char           char_type;
23     typedef int            int_type;
24     typedef std::streampos pos_type;
25     typedef std::streamoff off_type;
26     
27     sparcasmbuf(std::ostream &On) : BaseStr(On) {}
28
29     virtual int_type overflow(int_type C) {
30       if (C != EOF)
31         BaseStr << "\t.byte " << C << "\n"; // Output C;
32       return C;
33     }
34   };
35
36
37   // osparcasmstream - Define an ostream implementation that uses a sparcasmbuf
38   // as the underlying streambuf to write the data to.  This streambuf formats
39   // the output as .byte directives for sparc output.
40   //
41   class osparcasmstream : public ostream {
42     sparcasmbuf sb;
43   public:
44     typedef char           char_type;
45     typedef int            int_type;
46     typedef std::streampos pos_type;
47     typedef std::streamoff off_type;
48
49     explicit osparcasmstream(ostream &On) : ostream(&sb), sb(On) { }
50
51     sparcasmbuf *rdbuf() const {
52       return const_cast<sparcasmbuf*>(&sb);
53     }
54   };
55
56   // SparcBytecodeWriter - Write bytecode out to a stream that is sparc'ified
57   class SparcBytecodeWriter : public Pass {
58     std::ostream &Out;
59   public:
60     SparcBytecodeWriter(std::ostream &out) : Out(out) {}
61
62     virtual bool run(Module *M) {
63       // Write bytecode out to the sparc assembly stream
64       Out << "\n\n!LLVM BYTECODE OUTPUT\n\t.section \".rodata\"\n\t.align 8\n";
65       Out << "\t.global LLVMBytecode\n\t.type LLVMBytecode,#object\n";
66       Out << "LLVMBytecode:\n";
67       osparcasmstream OS(Out);
68       WriteBytecodeToFile(M, OS);
69
70       Out << ".end_LLVMBytecode:\n";
71       Out << "\t.size LLVMBytecode, .end_LLVMBytecode-LLVMBytecode\n\n";
72       return false;
73     }
74   };
75 }  // end anonymous namespace
76
77 Pass *UltraSparc::getEmitBytecodeToAsmPass(std::ostream &Out) {
78   return new SparcBytecodeWriter(Out);
79 }