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/Pass.h"
11 #include "llvm/Bytecode/Writer.h"
16 // sparcasmbuf - stream buf for encoding output bytes as .byte directives for
17 // the sparc assembler.
19 class sparcasmbuf : public streambuf {
20 std::ostream &BaseStr;
22 typedef char char_type;
24 typedef std::streampos pos_type;
25 typedef std::streamoff off_type;
27 sparcasmbuf(std::ostream &On) : BaseStr(On) {}
29 virtual int_type overflow(int_type C) {
31 BaseStr << "\t.byte " << C << "\n"; // Output C;
37 // osparcasmstream - Define an ostream implementation that uses a sparcasmbuf
38 // as the underlying streambuf to write the data to. This streambuf formats
39 // the output as .byte directives for sparc output.
41 class osparcasmstream : public ostream {
44 typedef char char_type;
46 typedef std::streampos pos_type;
47 typedef std::streamoff off_type;
49 explicit osparcasmstream(ostream &On) : ostream(&sb), sb(On) { }
51 sparcasmbuf *rdbuf() const {
52 return const_cast<sparcasmbuf*>(&sb);
56 // SparcBytecodeWriter - Write bytecode out to a stream that is sparc'ified
57 class SparcBytecodeWriter : public Pass {
60 SparcBytecodeWriter(std::ostream &out) : Out(out) {}
62 virtual bool run(Module *M) {
63 // Write bytecode out to the sparc assembly stream
64 Out << "\n\n!LLVM BYTECODE OUTPUT\n\t.section \".rodata\"\n\t.align 8\n";
65 Out << "\t.global LLVMBytecode\n\t.type LLVMBytecode,#object\n";
66 Out << "LLVMBytecode:\n";
67 osparcasmstream OS(Out);
68 WriteBytecodeToFile(M, OS);
70 Out << ".end_LLVMBytecode:\n";
71 Out << "\t.size LLVMBytecode, .end_LLVMBytecode-LLVMBytecode\n\n";
75 } // end anonymous namespace
77 Pass *UltraSparc::getEmitBytecodeToAsmPass(std::ostream &Out) {
78 return new SparcBytecodeWriter(Out);