1 //===-- EmitBytecodeToAssembly.cpp - Emit bytecode to Sparc .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 "SparcInternals.h"
17 #include "llvm/Pass.h"
18 #include "llvm/Bytecode/Writer.h"
25 // sparcasmbuf - stream buf for encoding output bytes as .byte directives for
26 // the sparc assembler.
28 class sparcasmbuf : public std::streambuf {
29 std::ostream &BaseStr;
31 typedef char char_type;
33 typedef std::streampos pos_type;
34 typedef std::streamoff off_type;
36 sparcasmbuf(std::ostream &On) : BaseStr(On) {}
38 virtual int_type overflow(int_type C) {
40 BaseStr << "\t.byte " << C << "\n"; // Output C;
46 // osparcasmstream - Define an ostream implementation that uses a sparcasmbuf
47 // as the underlying streambuf to write the data to. This streambuf formats
48 // the output as .byte directives for sparc output.
50 class osparcasmstream : public std::ostream {
53 typedef char char_type;
55 typedef std::streampos pos_type;
56 typedef std::streamoff off_type;
58 explicit osparcasmstream(std::ostream &On) : std::ostream(&sb), sb(On) { }
60 sparcasmbuf *rdbuf() const {
61 return const_cast<sparcasmbuf*>(&sb);
65 static void writePrologue (std::ostream &Out, const std::string &comment,
66 const std::string &symName) {
68 // Output a comment describing the object.
69 Out << "!" << comment << "\n";
70 // Switch the current section to .rodata in the assembly output:
71 Out << "\t.section \".rodata\"\n\t.align 8\n";
72 // Output a global symbol naming the object:
73 Out << "\t.global " << symName << "\n";
74 Out << "\t.type " << symName << ",#object\n";
75 Out << symName << ":\n";
78 static void writeEpilogue (std::ostream &Out, const std::string &symName) {
80 // Output a local symbol marking the end of the object:
81 Out << ".end_" << symName << ":\n";
82 // Output size directive giving the size of the object:
83 Out << "\t.size " << symName << ", .end_" << symName << "-" << symName
87 // SparcBytecodeWriter - Write bytecode out to a stream that is sparc'ified
88 class SparcBytecodeWriter : public Pass {
91 SparcBytecodeWriter(std::ostream &out) : Out(out) {}
93 const char *getPassName() const { return "Emit Bytecode to Sparc Assembly";}
95 virtual bool run(Module &M) {
96 // Write an object containing the bytecode to the SPARC assembly stream
97 writePrologue (Out, "LLVM BYTECODE OUTPUT", "LLVMBytecode");
98 osparcasmstream OS(Out);
99 WriteBytecodeToFile(&M, OS);
100 writeEpilogue (Out, "LLVMBytecode");
102 // Write an object containing its length as an integer to the
103 // SPARC assembly stream
104 writePrologue (Out, "LLVM BYTECODE LENGTH", "llvm_length");
105 Out <<"\t.word\t.end_LLVMBytecode-LLVMBytecode\n";
106 writeEpilogue (Out, "llvm_length");
111 } // end anonymous namespace
113 Pass *UltraSparc::getBytecodeAsmPrinterPass(std::ostream &Out) {
114 return new SparcBytecodeWriter(Out);