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"
18 // sparcasmbuf - stream buf for encoding output bytes as .byte directives for
19 // the sparc assembler.
21 class sparcasmbuf : public std::streambuf {
22 std::ostream &BaseStr;
24 typedef char char_type;
26 typedef std::streampos pos_type;
27 typedef std::streamoff off_type;
29 sparcasmbuf(std::ostream &On) : BaseStr(On) {}
31 virtual int_type overflow(int_type C) {
33 BaseStr << "\t.byte " << C << "\n"; // Output C;
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.
43 class osparcasmstream : public std::ostream {
46 typedef char char_type;
48 typedef std::streampos pos_type;
49 typedef std::streamoff off_type;
51 explicit osparcasmstream(std::ostream &On) : std::ostream(&sb), sb(On) { }
53 sparcasmbuf *rdbuf() const {
54 return const_cast<sparcasmbuf*>(&sb);
58 // SparcBytecodeWriter - Write bytecode out to a stream that is sparc'ified
59 class SparcBytecodeWriter : public Pass {
62 SparcBytecodeWriter(std::ostream &out) : Out(out) {}
64 const char *getPassName() const { return "Emit Bytecode to Sparc Assembly";}
66 virtual bool run(Module &M) {
67 // Write bytecode out to the sparc assembly stream
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";
73 //changed --anand, to get the size of bytecode
74 osparcasmstream OS(Out);
75 WriteBytecodeToFile(&M, OS);
78 Out << ".end_LLVMBytecode:\n";
79 Out << "\t.size LLVMBytecode, .end_LLVMBytecode-LLVMBytecode\n\n";
82 Out <<"\n\n!LLVM BYTECODE Length\n";
83 Out <<"\t.section \".data\",#alloc,#write\n";
84 Out <<"\t.global llvm_length\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";
93 } // end anonymous namespace
95 Pass *UltraSparc::getEmitBytecodeToAsmPass(std::ostream &Out) {
96 return new SparcBytecodeWriter(Out);