Chris Lattner | 9530a6f | 2002-02-11 22:35:46 +0000 | [diff] [blame] | 1 | //===-- EmitBytecodeToAssembly.cpp - Emit bytecode to Sparc .s File --------==// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame^] | 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 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9530a6f | 2002-02-11 22:35:46 +0000 | [diff] [blame] | 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 "SparcInternals.h" |
Chris Lattner | d50b671 | 2002-04-28 20:40:59 +0000 | [diff] [blame] | 17 | #include "llvm/Pass.h" |
Chris Lattner | 9530a6f | 2002-02-11 22:35:46 +0000 | [diff] [blame] | 18 | #include "llvm/Bytecode/Writer.h" |
Vikram S. Adve | 9ee9d71 | 2002-03-03 20:46:32 +0000 | [diff] [blame] | 19 | #include <iostream> |
Mehwish Nagda | 0009c2e | 2002-07-25 17:22:48 +0000 | [diff] [blame] | 20 | |
Anand Shukla | 3ee2ea8 | 2002-07-21 09:35:01 +0000 | [diff] [blame] | 21 | using std::ostream; |
Chris Lattner | 9530a6f | 2002-02-11 22:35:46 +0000 | [diff] [blame] | 22 | |
| 23 | namespace { |
| 24 | |
| 25 | // sparcasmbuf - stream buf for encoding output bytes as .byte directives for |
| 26 | // the sparc assembler. |
| 27 | // |
Anand Shukla | 3ee2ea8 | 2002-07-21 09:35:01 +0000 | [diff] [blame] | 28 | class sparcasmbuf : public std::streambuf { |
Chris Lattner | 9530a6f | 2002-02-11 22:35:46 +0000 | [diff] [blame] | 29 | std::ostream &BaseStr; |
| 30 | public: |
Chris Lattner | 49b8a9c | 2002-02-24 23:02:40 +0000 | [diff] [blame] | 31 | typedef char char_type; |
| 32 | typedef int int_type; |
| 33 | typedef std::streampos pos_type; |
| 34 | typedef std::streamoff off_type; |
Chris Lattner | 9530a6f | 2002-02-11 22:35:46 +0000 | [diff] [blame] | 35 | |
| 36 | sparcasmbuf(std::ostream &On) : BaseStr(On) {} |
| 37 | |
| 38 | virtual int_type overflow(int_type C) { |
| 39 | if (C != EOF) |
| 40 | BaseStr << "\t.byte " << C << "\n"; // Output C; |
| 41 | return C; |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | |
| 46 | // osparcasmstream - Define an ostream implementation that uses a sparcasmbuf |
| 47 | // as the underlying streambuf to write the data to. This streambuf formats |
| 48 | // the output as .byte directives for sparc output. |
| 49 | // |
Mehwish Nagda | 0009c2e | 2002-07-25 17:22:48 +0000 | [diff] [blame] | 50 | class osparcasmstream : public std::ostream { |
Chris Lattner | 9530a6f | 2002-02-11 22:35:46 +0000 | [diff] [blame] | 51 | sparcasmbuf sb; |
| 52 | public: |
Chris Lattner | 49b8a9c | 2002-02-24 23:02:40 +0000 | [diff] [blame] | 53 | typedef char char_type; |
| 54 | typedef int int_type; |
| 55 | typedef std::streampos pos_type; |
| 56 | typedef std::streamoff off_type; |
Chris Lattner | 9530a6f | 2002-02-11 22:35:46 +0000 | [diff] [blame] | 57 | |
Mehwish Nagda | 0009c2e | 2002-07-25 17:22:48 +0000 | [diff] [blame] | 58 | explicit osparcasmstream(std::ostream &On) : std::ostream(&sb), sb(On) { } |
Chris Lattner | 9530a6f | 2002-02-11 22:35:46 +0000 | [diff] [blame] | 59 | |
| 60 | sparcasmbuf *rdbuf() const { |
| 61 | return const_cast<sparcasmbuf*>(&sb); |
| 62 | } |
| 63 | }; |
| 64 | |
Brian Gaeke | bb7cd97 | 2003-09-18 17:37:14 +0000 | [diff] [blame] | 65 | static void writePrologue (std::ostream &Out, const std::string &comment, |
| 66 | const std::string &symName) { |
| 67 | // Prologue: |
| 68 | // Output a comment describing the object. |
| 69 | Out << "!" << comment << "\n"; |
| 70 | // Switch the current section to .rodata in the assembly output: |
| 71 | Out << "\t.section \".rodata\"\n\t.align 8\n"; |
| 72 | // Output a global symbol naming the object: |
| 73 | Out << "\t.global " << symName << "\n"; |
| 74 | Out << "\t.type " << symName << ",#object\n"; |
| 75 | Out << symName << ":\n"; |
| 76 | } |
| 77 | |
| 78 | static void writeEpilogue (std::ostream &Out, const std::string &symName) { |
| 79 | // Epilogue: |
| 80 | // Output a local symbol marking the end of the object: |
| 81 | Out << ".end_" << symName << ":\n"; |
| 82 | // Output size directive giving the size of the object: |
| 83 | Out << "\t.size " << symName << ", .end_" << symName << "-" << symName |
| 84 | << "\n"; |
| 85 | } |
| 86 | |
Chris Lattner | 9530a6f | 2002-02-11 22:35:46 +0000 | [diff] [blame] | 87 | // SparcBytecodeWriter - Write bytecode out to a stream that is sparc'ified |
| 88 | class SparcBytecodeWriter : public Pass { |
| 89 | std::ostream &Out; |
| 90 | public: |
| 91 | SparcBytecodeWriter(std::ostream &out) : Out(out) {} |
| 92 | |
Chris Lattner | 96c466b | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 93 | const char *getPassName() const { return "Emit Bytecode to Sparc Assembly";} |
Mehwish Nagda | 0009c2e | 2002-07-25 17:22:48 +0000 | [diff] [blame] | 94 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 95 | virtual bool run(Module &M) { |
Brian Gaeke | bb7cd97 | 2003-09-18 17:37:14 +0000 | [diff] [blame] | 96 | // Write an object containing the bytecode to the SPARC assembly stream |
| 97 | writePrologue (Out, "LLVM BYTECODE OUTPUT", "LLVMBytecode"); |
Mehwish Nagda | 0009c2e | 2002-07-25 17:22:48 +0000 | [diff] [blame] | 98 | osparcasmstream OS(Out); |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 99 | WriteBytecodeToFile(&M, OS); |
Brian Gaeke | bb7cd97 | 2003-09-18 17:37:14 +0000 | [diff] [blame] | 100 | writeEpilogue (Out, "LLVMBytecode"); |
Chris Lattner | 9530a6f | 2002-02-11 22:35:46 +0000 | [diff] [blame] | 101 | |
Brian Gaeke | bb7cd97 | 2003-09-18 17:37:14 +0000 | [diff] [blame] | 102 | // Write an object containing its length as an integer to the |
| 103 | // SPARC assembly stream |
| 104 | writePrologue (Out, "LLVM BYTECODE LENGTH", "llvm_length"); |
Mehwish Nagda | 0009c2e | 2002-07-25 17:22:48 +0000 | [diff] [blame] | 105 | Out <<"\t.word\t.end_LLVMBytecode-LLVMBytecode\n"; |
Brian Gaeke | bb7cd97 | 2003-09-18 17:37:14 +0000 | [diff] [blame] | 106 | writeEpilogue (Out, "llvm_length"); |
| 107 | |
Chris Lattner | 9530a6f | 2002-02-11 22:35:46 +0000 | [diff] [blame] | 108 | return false; |
| 109 | } |
| 110 | }; |
| 111 | } // end anonymous namespace |
| 112 | |
Brian Gaeke | bb7cd97 | 2003-09-18 17:37:14 +0000 | [diff] [blame] | 113 | Pass *UltraSparc::getBytecodeAsmPrinterPass(std::ostream &Out) { |
Chris Lattner | 9530a6f | 2002-02-11 22:35:46 +0000 | [diff] [blame] | 114 | return new SparcBytecodeWriter(Out); |
| 115 | } |