2004-02-25 18:44:15 +00:00
|
|
|
//===-- EmitBytecodeToAssembly.cpp - Emit bytecode to SparcV9 .s File --------==//
|
2003-10-20 19:43:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-02-11 22:35:46 +00:00
|
|
|
//
|
|
|
|
// This file implements the pass that writes LLVM bytecode as data to a sparc
|
|
|
|
// assembly file. The bytecode gets assembled into a special bytecode section
|
|
|
|
// of the executable for use at runtime later.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-02-25 18:44:15 +00:00
|
|
|
#include "SparcV9Internals.h"
|
2002-04-28 20:40:59 +00:00
|
|
|
#include "llvm/Pass.h"
|
2002-02-11 22:35:46 +00:00
|
|
|
#include "llvm/Bytecode/Writer.h"
|
2002-03-03 20:46:32 +00:00
|
|
|
#include <iostream>
|
2002-07-25 17:22:48 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2002-07-21 09:35:01 +00:00
|
|
|
using std::ostream;
|
2002-02-11 22:35:46 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// sparcasmbuf - stream buf for encoding output bytes as .byte directives for
|
|
|
|
// the sparc assembler.
|
|
|
|
//
|
2002-07-21 09:35:01 +00:00
|
|
|
class sparcasmbuf : public std::streambuf {
|
2002-02-11 22:35:46 +00:00
|
|
|
std::ostream &BaseStr;
|
|
|
|
public:
|
2002-02-24 23:02:40 +00:00
|
|
|
typedef char char_type;
|
|
|
|
typedef int int_type;
|
|
|
|
typedef std::streampos pos_type;
|
|
|
|
typedef std::streamoff off_type;
|
2002-02-11 22:35:46 +00:00
|
|
|
|
|
|
|
sparcasmbuf(std::ostream &On) : BaseStr(On) {}
|
|
|
|
|
|
|
|
virtual int_type overflow(int_type C) {
|
|
|
|
if (C != EOF)
|
|
|
|
BaseStr << "\t.byte " << C << "\n"; // Output C;
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// osparcasmstream - Define an ostream implementation that uses a sparcasmbuf
|
|
|
|
// as the underlying streambuf to write the data to. This streambuf formats
|
|
|
|
// the output as .byte directives for sparc output.
|
|
|
|
//
|
2002-07-25 17:22:48 +00:00
|
|
|
class osparcasmstream : public std::ostream {
|
2002-02-11 22:35:46 +00:00
|
|
|
sparcasmbuf sb;
|
|
|
|
public:
|
2002-02-24 23:02:40 +00:00
|
|
|
typedef char char_type;
|
|
|
|
typedef int int_type;
|
|
|
|
typedef std::streampos pos_type;
|
|
|
|
typedef std::streamoff off_type;
|
2002-02-11 22:35:46 +00:00
|
|
|
|
2002-07-25 17:22:48 +00:00
|
|
|
explicit osparcasmstream(std::ostream &On) : std::ostream(&sb), sb(On) { }
|
2002-02-11 22:35:46 +00:00
|
|
|
|
|
|
|
sparcasmbuf *rdbuf() const {
|
|
|
|
return const_cast<sparcasmbuf*>(&sb);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2003-09-18 17:37:14 +00:00
|
|
|
static void writePrologue (std::ostream &Out, const std::string &comment,
|
|
|
|
const std::string &symName) {
|
|
|
|
// Prologue:
|
|
|
|
// Output a comment describing the object.
|
|
|
|
Out << "!" << comment << "\n";
|
|
|
|
// Switch the current section to .rodata in the assembly output:
|
|
|
|
Out << "\t.section \".rodata\"\n\t.align 8\n";
|
|
|
|
// Output a global symbol naming the object:
|
|
|
|
Out << "\t.global " << symName << "\n";
|
|
|
|
Out << "\t.type " << symName << ",#object\n";
|
|
|
|
Out << symName << ":\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void writeEpilogue (std::ostream &Out, const std::string &symName) {
|
|
|
|
// Epilogue:
|
|
|
|
// Output a local symbol marking the end of the object:
|
|
|
|
Out << ".end_" << symName << ":\n";
|
|
|
|
// Output size directive giving the size of the object:
|
|
|
|
Out << "\t.size " << symName << ", .end_" << symName << "-" << symName
|
|
|
|
<< "\n";
|
|
|
|
}
|
|
|
|
|
2004-02-25 18:44:15 +00:00
|
|
|
// SparcV9BytecodeWriter - Write bytecode out to a stream that is sparc'ified
|
|
|
|
class SparcV9BytecodeWriter : public Pass {
|
2002-02-11 22:35:46 +00:00
|
|
|
std::ostream &Out;
|
|
|
|
public:
|
2004-02-25 18:44:15 +00:00
|
|
|
SparcV9BytecodeWriter(std::ostream &out) : Out(out) {}
|
2002-02-11 22:35:46 +00:00
|
|
|
|
2004-02-25 18:44:15 +00:00
|
|
|
const char *getPassName() const { return "Emit Bytecode to SparcV9 Assembly";}
|
2002-07-25 17:22:48 +00:00
|
|
|
|
2002-06-25 16:13:21 +00:00
|
|
|
virtual bool run(Module &M) {
|
2003-09-18 17:37:14 +00:00
|
|
|
// Write an object containing the bytecode to the SPARC assembly stream
|
|
|
|
writePrologue (Out, "LLVM BYTECODE OUTPUT", "LLVMBytecode");
|
2002-07-25 17:22:48 +00:00
|
|
|
osparcasmstream OS(Out);
|
2002-06-25 16:13:21 +00:00
|
|
|
WriteBytecodeToFile(&M, OS);
|
2003-09-18 17:37:14 +00:00
|
|
|
writeEpilogue (Out, "LLVMBytecode");
|
2002-02-11 22:35:46 +00:00
|
|
|
|
2003-09-18 17:37:14 +00:00
|
|
|
// Write an object containing its length as an integer to the
|
|
|
|
// SPARC assembly stream
|
|
|
|
writePrologue (Out, "LLVM BYTECODE LENGTH", "llvm_length");
|
2002-07-25 17:22:48 +00:00
|
|
|
Out <<"\t.word\t.end_LLVMBytecode-LLVMBytecode\n";
|
2003-09-18 17:37:14 +00:00
|
|
|
writeEpilogue (Out, "llvm_length");
|
|
|
|
|
2002-02-11 22:35:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2003-11-13 00:19:02 +00:00
|
|
|
Pass *createBytecodeAsmPrinterPass(std::ostream &Out) {
|
2004-02-25 18:44:15 +00:00
|
|
|
return new SparcV9BytecodeWriter(Out);
|
2002-02-11 22:35:46 +00:00
|
|
|
}
|
2003-11-11 22:41:34 +00:00
|
|
|
|
|
|
|
} // End llvm namespace
|