Chris Lattner | 9530a6f | 2002-02-11 22:35:46 +0000 | [diff] [blame^] | 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/Bytecode/Writer.h" |
| 11 | |
| 12 | namespace { |
| 13 | |
| 14 | // sparcasmbuf - stream buf for encoding output bytes as .byte directives for |
| 15 | // the sparc assembler. |
| 16 | // |
| 17 | class sparcasmbuf : public streambuf { |
| 18 | std::ostream &BaseStr; |
| 19 | public: |
| 20 | typedef char char_type; |
| 21 | typedef int int_type; |
| 22 | typedef streampos pos_type; |
| 23 | typedef streamoff off_type; |
| 24 | |
| 25 | sparcasmbuf(std::ostream &On) : BaseStr(On) {} |
| 26 | |
| 27 | virtual int_type overflow(int_type C) { |
| 28 | if (C != EOF) |
| 29 | BaseStr << "\t.byte " << C << "\n"; // Output C; |
| 30 | return C; |
| 31 | } |
| 32 | }; |
| 33 | |
| 34 | |
| 35 | // osparcasmstream - Define an ostream implementation that uses a sparcasmbuf |
| 36 | // as the underlying streambuf to write the data to. This streambuf formats |
| 37 | // the output as .byte directives for sparc output. |
| 38 | // |
| 39 | class osparcasmstream : public ostream { |
| 40 | sparcasmbuf sb; |
| 41 | public: |
| 42 | typedef char char_type; |
| 43 | typedef int int_type; |
| 44 | typedef streampos pos_type; |
| 45 | typedef streamoff off_type; |
| 46 | |
| 47 | explicit osparcasmstream(ostream &On) : ostream(&sb), sb(On) { } |
| 48 | |
| 49 | sparcasmbuf *rdbuf() const { |
| 50 | return const_cast<sparcasmbuf*>(&sb); |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | // SparcBytecodeWriter - Write bytecode out to a stream that is sparc'ified |
| 55 | class SparcBytecodeWriter : public Pass { |
| 56 | std::ostream &Out; |
| 57 | public: |
| 58 | SparcBytecodeWriter(std::ostream &out) : Out(out) {} |
| 59 | |
| 60 | virtual bool run(Module *M) { |
| 61 | // Write bytecode out to the sparc assembly stream |
| 62 | Out << "\n\n!LLVM BYTECODE OUTPUT\n\t.section \".rodata\"\n\t.align 8\n"; |
| 63 | Out << "\t.global LLVMBytecode\n\t.type LLVMBytecode,#object\n"; |
| 64 | Out << "LLVMBytecode:\n"; |
| 65 | osparcasmstream OS(Out); |
| 66 | WriteBytecodeToFile(M, OS); |
| 67 | |
| 68 | Out << ".end_LLVMBytecode:\n"; |
| 69 | Out << "\t.size LLVMBytecode, .end_LLVMBytecode-LLVMBytecode\n\n"; |
| 70 | return false; |
| 71 | } |
| 72 | }; |
| 73 | } // end anonymous namespace |
| 74 | |
| 75 | Pass *UltraSparc::getEmitBytecodeToAsmPass(std::ostream &Out) { |
| 76 | return new SparcBytecodeWriter(Out); |
| 77 | } |