blob: 92420f9421f4c68e1a82bb654daa841078eed8f1 [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"
Chris Lattnerd50b6712002-04-28 20:40:59 +000010#include "llvm/Pass.h"
Chris Lattner9530a6f2002-02-11 22:35:46 +000011#include "llvm/Bytecode/Writer.h"
Vikram S. Adve9ee9d712002-03-03 20:46:32 +000012#include <iostream>
Chris Lattner9530a6f2002-02-11 22:35:46 +000013
14namespace {
15
16 // sparcasmbuf - stream buf for encoding output bytes as .byte directives for
17 // the sparc assembler.
18 //
19 class sparcasmbuf : public streambuf {
20 std::ostream &BaseStr;
21 public:
Chris Lattner49b8a9c2002-02-24 23:02:40 +000022 typedef char char_type;
23 typedef int int_type;
24 typedef std::streampos pos_type;
25 typedef std::streamoff off_type;
Chris Lattner9530a6f2002-02-11 22:35:46 +000026
27 sparcasmbuf(std::ostream &On) : BaseStr(On) {}
28
29 virtual int_type overflow(int_type C) {
30 if (C != EOF)
31 BaseStr << "\t.byte " << C << "\n"; // Output C;
32 return C;
33 }
34 };
35
36
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.
40 //
41 class osparcasmstream : public ostream {
42 sparcasmbuf sb;
43 public:
Chris Lattner49b8a9c2002-02-24 23:02:40 +000044 typedef char char_type;
45 typedef int int_type;
46 typedef std::streampos pos_type;
47 typedef std::streamoff off_type;
Chris Lattner9530a6f2002-02-11 22:35:46 +000048
49 explicit osparcasmstream(ostream &On) : ostream(&sb), sb(On) { }
50
51 sparcasmbuf *rdbuf() const {
52 return const_cast<sparcasmbuf*>(&sb);
53 }
54 };
55
56 // SparcBytecodeWriter - Write bytecode out to a stream that is sparc'ified
57 class SparcBytecodeWriter : public Pass {
58 std::ostream &Out;
59 public:
60 SparcBytecodeWriter(std::ostream &out) : Out(out) {}
61
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);
69
70 Out << ".end_LLVMBytecode:\n";
71 Out << "\t.size LLVMBytecode, .end_LLVMBytecode-LLVMBytecode\n\n";
72 return false;
73 }
74 };
75} // end anonymous namespace
76
77Pass *UltraSparc::getEmitBytecodeToAsmPass(std::ostream &Out) {
78 return new SparcBytecodeWriter(Out);
79}