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