blob: bd4a3288268e92dfe5c48c4f773016edca7340f2 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- WriterInternals.h - Data structures shared by the Writer -*- C++ -*--=//
2//
3// This header defines the interface used between components of the bytecode
4// writer.
5//
6// Note that the performance of this library is not terribly important, because
7// it shouldn't be used by JIT type applications... so it is not a huge focus
8// at least. :)
9//
10//===----------------------------------------------------------------------===//
11
12#ifndef LLVM_LIB_BYTECODE_WRITER_WRITERINTERNALS_H
13#define LLVM_LIB_BYTECODE_WRITER_WRITERINTERNALS_H
14
15#include "llvm/Bytecode/Writer.h"
16#include "llvm/Bytecode/Format.h"
Chris Lattnere8fdde12001-09-07 16:39:41 +000017#include "llvm/Bytecode/Primitives.h"
Chris Lattnerb5794002002-04-07 22:49:37 +000018#include "llvm/SlotCalculator.h"
Chris Lattner00950542001-06-06 20:29:01 +000019#include "llvm/Instruction.h"
20
Chris Lattnere8fdde12001-09-07 16:39:41 +000021class BytecodeWriter {
Chris Lattner697954c2002-01-20 22:54:45 +000022 std::deque<unsigned char> &Out;
Chris Lattner00950542001-06-06 20:29:01 +000023 SlotCalculator Table;
24public:
Chris Lattner697954c2002-01-20 22:54:45 +000025 BytecodeWriter(std::deque<unsigned char> &o, const Module *M);
Chris Lattner00950542001-06-06 20:29:01 +000026
27protected:
Chris Lattnere8fdde12001-09-07 16:39:41 +000028 void outputConstants(bool isMethod);
Chris Lattner4d669b52002-04-08 22:01:15 +000029 void processMethod(const Function *F);
Chris Lattnere8fdde12001-09-07 16:39:41 +000030 void processBasicBlock(const BasicBlock *BB);
31 void processInstruction(const Instruction *I);
Chris Lattner00950542001-06-06 20:29:01 +000032
33private :
34 inline void outputSignature() {
35 static const unsigned char *Sig = (const unsigned char*)"llvm";
36 Out.insert(Out.end(), Sig, Sig+4); // output the bytecode signature...
37 }
38
39 void outputModuleInfoBlock(const Module *C);
40 void outputSymbolTable(const SymbolTable &ST);
Chris Lattnere9bb2df2001-12-03 22:26:30 +000041 bool outputConstant(const Constant *CPV);
Chris Lattner00950542001-06-06 20:29:01 +000042 void outputType(const Type *T);
43};
44
45
46
47
48// BytecodeBlock - Little helper class that helps us do backpatching of bytecode
49// block sizes really easily. It backpatches when it goes out of scope.
50//
51class BytecodeBlock {
52 unsigned Loc;
Chris Lattner697954c2002-01-20 22:54:45 +000053 std::deque<unsigned char> &Out;
Chris Lattner00950542001-06-06 20:29:01 +000054
55 BytecodeBlock(const BytecodeBlock &); // do not implement
56 void operator=(const BytecodeBlock &); // do not implement
57public:
Chris Lattner697954c2002-01-20 22:54:45 +000058 inline BytecodeBlock(unsigned ID, std::deque<unsigned char> &o) : Out(o) {
Chris Lattner00950542001-06-06 20:29:01 +000059 output(ID, Out);
60 output((unsigned)0, Out); // Reserve the space for the block size...
61 Loc = Out.size();
62 }
63
64 inline ~BytecodeBlock() { // Do backpatch when block goes out
65 // of scope...
Chris Lattnere8fdde12001-09-07 16:39:41 +000066 //cerr << "OldLoc = " << Loc << " NewLoc = " << NewLoc << " diff = "
67 // << (NewLoc-Loc) << endl;
Chris Lattner00950542001-06-06 20:29:01 +000068 output((unsigned)(Out.size()-Loc), Out, (int)Loc-4);
69 align32(Out); // Blocks must ALWAYS be aligned
70 }
71};
72
73
74#endif