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