1 //===-- EmitBytecodeToAssembly.cpp - Emit bytecode to Sparc .s File --------==//
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.
7 //===----------------------------------------------------------------------===//
9 #include "SparcInternals.h"
10 #include "llvm/Bytecode/Writer.h"
15 // sparcasmbuf - stream buf for encoding output bytes as .byte directives for
16 // the sparc assembler.
18 class sparcasmbuf : public streambuf {
19 std::ostream &BaseStr;
21 typedef char char_type;
23 typedef std::streampos pos_type;
24 typedef std::streamoff off_type;
26 sparcasmbuf(std::ostream &On) : BaseStr(On) {}
28 virtual int_type overflow(int_type C) {
30 BaseStr << "\t.byte " << C << "\n"; // Output C;
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.
40 class osparcasmstream : public ostream {
43 typedef char char_type;
45 typedef std::streampos pos_type;
46 typedef std::streamoff off_type;
48 explicit osparcasmstream(ostream &On) : ostream(&sb), sb(On) { }
50 sparcasmbuf *rdbuf() const {
51 return const_cast<sparcasmbuf*>(&sb);
55 // SparcBytecodeWriter - Write bytecode out to a stream that is sparc'ified
56 class SparcBytecodeWriter : public Pass {
59 SparcBytecodeWriter(std::ostream &out) : Out(out) {}
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);
69 Out << ".end_LLVMBytecode:\n";
70 Out << "\t.size LLVMBytecode, .end_LLVMBytecode-LLVMBytecode\n\n";
74 } // end anonymous namespace
76 Pass *UltraSparc::getEmitBytecodeToAsmPass(std::ostream &Out) {
77 return new SparcBytecodeWriter(Out);