1 //===-- EmitBytecodeToAssembly.cpp - Emit bytecode to SparcV9 .s File ------==//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #include "SparcV9Internals.h"
17 #include "llvm/Pass.h"
18 #include "llvm/Bytecode/Writer.h"
24 // sparcasmbuf - stream buf for encoding output bytes as .byte directives for
25 // the sparc assembler.
27 class sparcasmbuf : public std::streambuf {
28 std::ostream &BaseStr;
30 typedef char char_type;
32 typedef std::streampos pos_type;
33 typedef std::streamoff off_type;
35 sparcasmbuf(std::ostream &On) : BaseStr(On) {}
37 virtual int_type overflow(int_type C) {
39 BaseStr << "\t.byte " << C << "\n"; // Output C;
45 // osparcasmstream - Define an ostream implementation that uses a sparcasmbuf
46 // as the underlying streambuf to write the data to. This streambuf formats
47 // the output as .byte directives for sparc output.
49 class osparcasmstream : public std::ostream {
52 typedef char char_type;
54 typedef std::streampos pos_type;
55 typedef std::streamoff off_type;
57 explicit osparcasmstream(std::ostream &On) : std::ostream(&sb), sb(On) { }
59 sparcasmbuf *rdbuf() const {
60 return const_cast<sparcasmbuf*>(&sb);
64 static void writePrologue (std::ostream &Out, const std::string &comment,
65 const std::string &symName) {
67 // Output a comment describing the object.
68 Out << "!" << comment << "\n";
69 // Switch the current section to .rodata in the assembly output:
70 Out << "\t.section \".rodata\"\n\t.align 8\n";
71 // Output a global symbol naming the object:
72 Out << "\t.global " << symName << "\n";
73 Out << "\t.type " << symName << ",#object\n";
74 Out << symName << ":\n";
77 static void writeEpilogue (std::ostream &Out, const std::string &symName) {
79 // Output a local symbol marking the end of the object:
80 Out << ".end_" << symName << ":\n";
81 // Output size directive giving the size of the object:
82 Out << "\t.size " << symName << ", .end_" << symName << "-" << symName
86 // SparcV9BytecodeWriter - Write bytecode out to a stream that is sparc'ified
87 class SparcV9BytecodeWriter : public ModulePass {
90 SparcV9BytecodeWriter(std::ostream &out) : Out(out) {}
92 const char *getPassName() const { return "Emit Bytecode to SparcV9 Assembly";}
94 virtual bool runOnModule(Module &M) {
95 // Write an object containing the bytecode to the SPARC assembly stream
96 writePrologue (Out, "LLVM BYTECODE OUTPUT", "LLVMBytecode");
97 osparcasmstream OS(Out);
98 WriteBytecodeToFile(&M, OS);
99 writeEpilogue (Out, "LLVMBytecode");
101 // Write an object containing its length as an integer to the
102 // SPARC assembly stream
103 writePrologue (Out, "LLVM BYTECODE LENGTH", "llvm_length");
104 Out <<"\t.word\t.end_LLVMBytecode-LLVMBytecode\n";
105 writeEpilogue (Out, "llvm_length");
110 } // end anonymous namespace
112 ModulePass *llvm::createBytecodeAsmPrinterPass(std::ostream &Out) {
113 return new SparcV9BytecodeWriter(Out);