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" |
Chris Lattner | d50b671 | 2002-04-28 20:40:59 +0000 | [diff] [blame] | 10 | #include "llvm/Pass.h" |
Chris Lattner | 9530a6f | 2002-02-11 22:35:46 +0000 | [diff] [blame] | 11 | #include "llvm/Bytecode/Writer.h" |
Vikram S. Adve | 9ee9d71 | 2002-03-03 20:46:32 +0000 | [diff] [blame] | 12 | #include <iostream> |
Mehwish Nagda | 0009c2e | 2002-07-25 17:22:48 +0000 | [diff] [blame] | 13 | |
Anand Shukla | 3ee2ea8 | 2002-07-21 09:35:01 +0000 | [diff] [blame] | 14 | using std::ostream; |
Chris Lattner | 9530a6f | 2002-02-11 22:35:46 +0000 | [diff] [blame] | 15 | |
| 16 | namespace { |
| 17 | |
| 18 | // sparcasmbuf - stream buf for encoding output bytes as .byte directives for |
| 19 | // the sparc assembler. |
| 20 | // |
Anand Shukla | 3ee2ea8 | 2002-07-21 09:35:01 +0000 | [diff] [blame] | 21 | class sparcasmbuf : public std::streambuf { |
Chris Lattner | 9530a6f | 2002-02-11 22:35:46 +0000 | [diff] [blame] | 22 | std::ostream &BaseStr; |
| 23 | public: |
Chris Lattner | 49b8a9c | 2002-02-24 23:02:40 +0000 | [diff] [blame] | 24 | typedef char char_type; |
| 25 | typedef int int_type; |
| 26 | typedef std::streampos pos_type; |
| 27 | typedef std::streamoff off_type; |
Chris Lattner | 9530a6f | 2002-02-11 22:35:46 +0000 | [diff] [blame] | 28 | |
| 29 | sparcasmbuf(std::ostream &On) : BaseStr(On) {} |
| 30 | |
| 31 | virtual int_type overflow(int_type C) { |
| 32 | if (C != EOF) |
| 33 | BaseStr << "\t.byte " << C << "\n"; // Output C; |
| 34 | return C; |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | |
| 39 | // osparcasmstream - Define an ostream implementation that uses a sparcasmbuf |
| 40 | // as the underlying streambuf to write the data to. This streambuf formats |
| 41 | // the output as .byte directives for sparc output. |
| 42 | // |
Mehwish Nagda | 0009c2e | 2002-07-25 17:22:48 +0000 | [diff] [blame] | 43 | class osparcasmstream : public std::ostream { |
Chris Lattner | 9530a6f | 2002-02-11 22:35:46 +0000 | [diff] [blame] | 44 | sparcasmbuf sb; |
| 45 | public: |
Chris Lattner | 49b8a9c | 2002-02-24 23:02:40 +0000 | [diff] [blame] | 46 | typedef char char_type; |
| 47 | typedef int int_type; |
| 48 | typedef std::streampos pos_type; |
| 49 | typedef std::streamoff off_type; |
Chris Lattner | 9530a6f | 2002-02-11 22:35:46 +0000 | [diff] [blame] | 50 | |
Mehwish Nagda | 0009c2e | 2002-07-25 17:22:48 +0000 | [diff] [blame] | 51 | explicit osparcasmstream(std::ostream &On) : std::ostream(&sb), sb(On) { } |
Chris Lattner | 9530a6f | 2002-02-11 22:35:46 +0000 | [diff] [blame] | 52 | |
| 53 | sparcasmbuf *rdbuf() const { |
| 54 | return const_cast<sparcasmbuf*>(&sb); |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | // SparcBytecodeWriter - Write bytecode out to a stream that is sparc'ified |
| 59 | class SparcBytecodeWriter : public Pass { |
| 60 | std::ostream &Out; |
| 61 | public: |
| 62 | SparcBytecodeWriter(std::ostream &out) : Out(out) {} |
| 63 | |
Chris Lattner | 96c466b | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 64 | const char *getPassName() const { return "Emit Bytecode to Sparc Assembly";} |
Mehwish Nagda | 0009c2e | 2002-07-25 17:22:48 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 66 | virtual bool run(Module &M) { |
Chris Lattner | 9530a6f | 2002-02-11 22:35:46 +0000 | [diff] [blame] | 67 | // Write bytecode out to the sparc assembly stream |
Anand Shukla | 3ee2ea8 | 2002-07-21 09:35:01 +0000 | [diff] [blame] | 68 | |
Mehwish Nagda | 0009c2e | 2002-07-25 17:22:48 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 9530a6f | 2002-02-11 22:35:46 +0000 | [diff] [blame] | 70 | Out << "\n\n!LLVM BYTECODE OUTPUT\n\t.section \".rodata\"\n\t.align 8\n"; |
| 71 | Out << "\t.global LLVMBytecode\n\t.type LLVMBytecode,#object\n"; |
| 72 | Out << "LLVMBytecode:\n"; |
Anand Shukla | 3ee2ea8 | 2002-07-21 09:35:01 +0000 | [diff] [blame] | 73 | //changed --anand, to get the size of bytecode |
Mehwish Nagda | 0009c2e | 2002-07-25 17:22:48 +0000 | [diff] [blame] | 74 | osparcasmstream OS(Out); |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 75 | WriteBytecodeToFile(&M, OS); |
Chris Lattner | 9530a6f | 2002-02-11 22:35:46 +0000 | [diff] [blame] | 76 | |
Mehwish Nagda | 0009c2e | 2002-07-25 17:22:48 +0000 | [diff] [blame] | 77 | |
Chris Lattner | 9530a6f | 2002-02-11 22:35:46 +0000 | [diff] [blame] | 78 | Out << ".end_LLVMBytecode:\n"; |
| 79 | Out << "\t.size LLVMBytecode, .end_LLVMBytecode-LLVMBytecode\n\n"; |
Anand Shukla | 3ee2ea8 | 2002-07-21 09:35:01 +0000 | [diff] [blame] | 80 | |
Mehwish Nagda | 0009c2e | 2002-07-25 17:22:48 +0000 | [diff] [blame] | 81 | |
| 82 | Out <<"\n\n!LLVM BYTECODE Length\n"; |
| 83 | Out <<"\t.section \".data\",#alloc,#write\n"; |
| 84 | Out <<"\t.global llvm_length\n"; |
| 85 | Out <<"\t.align 4\n"; |
| 86 | Out <<"\t.type llvm_length,#object\n"; |
| 87 | Out <<"\t.size llvm_length,4\n"; |
| 88 | Out <<"llvm_length:\n"; |
| 89 | Out <<"\t.word\t.end_LLVMBytecode-LLVMBytecode\n"; |
Chris Lattner | 9530a6f | 2002-02-11 22:35:46 +0000 | [diff] [blame] | 90 | return false; |
| 91 | } |
| 92 | }; |
| 93 | } // end anonymous namespace |
| 94 | |
| 95 | Pass *UltraSparc::getEmitBytecodeToAsmPass(std::ostream &Out) { |
| 96 | return new SparcBytecodeWriter(Out); |
| 97 | } |