blob: 1a14a5300b7f23fbbd954059e277a8a60cf969de [file] [log] [blame]
Chris Lattner9530a6f2002-02-11 22:35:46 +00001//===-- EmitBytecodeToAssembly.cpp - Emit bytecode to Sparc .s File --------==//
2//
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.
6//
7//===----------------------------------------------------------------------===//
8
9#include "SparcInternals.h"
10#include "llvm/Bytecode/Writer.h"
Chris Lattner49b8a9c2002-02-24 23:02:40 +000011#include <ostream>
Chris Lattner9530a6f2002-02-11 22:35:46 +000012
13namespace {
14
15 // sparcasmbuf - stream buf for encoding output bytes as .byte directives for
16 // the sparc assembler.
17 //
18 class sparcasmbuf : public streambuf {
19 std::ostream &BaseStr;
20 public:
Chris Lattner49b8a9c2002-02-24 23:02:40 +000021 typedef char char_type;
22 typedef int int_type;
23 typedef std::streampos pos_type;
24 typedef std::streamoff off_type;
Chris Lattner9530a6f2002-02-11 22:35:46 +000025
26 sparcasmbuf(std::ostream &On) : BaseStr(On) {}
27
28 virtual int_type overflow(int_type C) {
29 if (C != EOF)
30 BaseStr << "\t.byte " << C << "\n"; // Output C;
31 return C;
32 }
33 };
34
35
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.
39 //
40 class osparcasmstream : public ostream {
41 sparcasmbuf sb;
42 public:
Chris Lattner49b8a9c2002-02-24 23:02:40 +000043 typedef char char_type;
44 typedef int int_type;
45 typedef std::streampos pos_type;
46 typedef std::streamoff off_type;
Chris Lattner9530a6f2002-02-11 22:35:46 +000047
48 explicit osparcasmstream(ostream &On) : ostream(&sb), sb(On) { }
49
50 sparcasmbuf *rdbuf() const {
51 return const_cast<sparcasmbuf*>(&sb);
52 }
53 };
54
55 // SparcBytecodeWriter - Write bytecode out to a stream that is sparc'ified
56 class SparcBytecodeWriter : public Pass {
57 std::ostream &Out;
58 public:
59 SparcBytecodeWriter(std::ostream &out) : Out(out) {}
60
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);
68
69 Out << ".end_LLVMBytecode:\n";
70 Out << "\t.size LLVMBytecode, .end_LLVMBytecode-LLVMBytecode\n\n";
71 return false;
72 }
73 };
74} // end anonymous namespace
75
76Pass *UltraSparc::getEmitBytecodeToAsmPass(std::ostream &Out) {
77 return new SparcBytecodeWriter(Out);
78}