1 //===- WriterInternals.h - Data structures shared by the Writer -*- C++ -*-===//
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 header defines the interface used between components of the bytecode
13 // Note that the performance of this library is not terribly important, because
14 // it shouldn't be used by JIT type applications... so it is not a huge focus
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_LIB_BYTECODE_WRITER_WRITERINTERNALS_H
20 #define LLVM_LIB_BYTECODE_WRITER_WRITERINTERNALS_H
22 #include "llvm/Bytecode/Writer.h"
23 #include "WriterPrimitives.h"
24 #include "llvm/Bytecode/Format.h"
25 #include "llvm/Analysis/SlotCalculator.h"
26 #include "llvm/Instruction.h"
30 class BytecodeWriter {
31 std::deque<unsigned char> &Out;
34 BytecodeWriter(std::deque<unsigned char> &o, const Module *M);
37 void outputConstants(bool isFunction);
38 void outputConstantStrings();
39 void outputFunction(const Function *F);
40 void outputCompactionTable();
41 void outputCompactionTablePlane(unsigned PlaneNo,
42 const std::vector<const Value*> &TypePlane,
44 void outputInstructions(const Function *F);
45 void outputInstruction(const Instruction &I);
47 void outputModuleInfoBlock(const Module *C);
48 void outputSymbolTable(const SymbolTable &ST);
49 void outputConstantsInPlane(const std::vector<const Value*> &Plane,
51 void outputConstant(const Constant *CPV);
52 void outputType(const Type *T);
58 /// BytecodeBlock - Little helper class is used by the bytecode writer to help
59 /// do backpatching of bytecode block sizes really easily. It backpatches when
60 /// it goes out of scope.
64 std::deque<unsigned char> &Out;
66 /// ElideIfEmpty - If this is true and the bytecode block ends up being empty,
67 /// the block can remove itself from the output stream entirely.
70 BytecodeBlock(const BytecodeBlock &); // do not implement
71 void operator=(const BytecodeBlock &); // do not implement
73 inline BytecodeBlock(unsigned ID, std::deque<unsigned char> &o,
74 bool elideIfEmpty = false)
75 : Out(o), ElideIfEmpty(elideIfEmpty) {
77 output(0U, Out); // Reserve the space for the block size...
81 inline ~BytecodeBlock() { // Do backpatch when block goes out
83 if (Loc == Out.size() && ElideIfEmpty) {
84 // If the block is empty, and we are allowed to, do not emit the block at
86 Out.resize(Out.size()-8);
90 //cerr << "OldLoc = " << Loc << " NewLoc = " << NewLoc << " diff = "
91 // << (NewLoc-Loc) << endl;
92 output(unsigned(Out.size()-Loc), Out, int(Loc-4));
93 align32(Out); // Blocks must ALWAYS be aligned
97 } // End llvm namespace