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 "SlotCalculator.h"
23 #include "llvm/Bytecode/Writer.h"
24 #include "llvm/Bytecode/Format.h"
25 #include "llvm/Instruction.h"
26 #include "llvm/Support/DataTypes.h"
32 class BytecodeWriter {
33 std::vector<unsigned char> &Out;
36 BytecodeWriter(std::vector<unsigned char> &o, const Module *M);
39 void outputConstants(bool isFunction);
40 void outputConstantStrings();
41 void outputFunction(const Function *F);
42 void outputCompactionTable();
43 void outputCompactionTypes(unsigned StartNo);
44 void outputCompactionTablePlane(unsigned PlaneNo,
45 const std::vector<const Value*> &TypePlane,
47 void outputInstructions(const Function *F);
48 void outputInstruction(const Instruction &I);
49 void outputInstructionFormat0(const Instruction *I, unsigned Opcode,
50 const SlotCalculator &Table,
52 void outputInstrVarArgsCall(const Instruction *I,
54 const SlotCalculator &Table,
56 inline void outputInstructionFormat1(const Instruction *I,
60 inline void outputInstructionFormat2(const Instruction *I,
64 inline void outputInstructionFormat3(const Instruction *I,
69 void outputModuleInfoBlock(const Module *C);
70 void outputSymbolTable(const SymbolTable &ST);
71 void outputTypes(unsigned StartNo);
72 void outputConstantsInPlane(const std::vector<const Value*> &Plane,
74 void outputConstant(const Constant *CPV);
75 void outputType(const Type *T);
77 /// @brief Unsigned integer output primitive
78 inline void output(unsigned i, int pos = -1);
80 /// @brief Signed integer output primitive
81 inline void output(int i);
83 /// @brief 64-bit variable bit rate output primitive.
84 inline void output_vbr(uint64_t i);
86 /// @brief 32-bit variable bit rate output primitive.
87 inline void output_vbr(unsigned i);
89 /// @brief Signed 64-bit variable bit rate output primitive.
90 inline void output_vbr(int64_t i);
92 /// @brief Signed 32-bit variable bit rate output primitive.
93 inline void output_vbr(int i);
95 inline void output(const std::string &s );
97 inline void output_data(const void *Ptr, const void *End);
99 inline void output_float(float& FloatVal);
100 inline void output_double(double& DoubleVal);
102 inline void output_typeid(unsigned i);
104 inline size_t size() const { return Out.size(); }
105 inline void resize(size_t S) { Out.resize(S); }
106 friend class BytecodeBlock;
109 /// BytecodeBlock - Little helper class is used by the bytecode writer to help
110 /// do backpatching of bytecode block sizes really easily. It backpatches when
111 /// it goes out of scope.
113 class BytecodeBlock {
116 BytecodeWriter& Writer;
118 /// ElideIfEmpty - If this is true and the bytecode block ends up being empty,
119 /// the block can remove itself from the output stream entirely.
122 /// If this is true then the block is written with a long format header using
123 /// a uint (32-bits) for both the block id and size. Otherwise, it uses the
124 /// short format which is a single uint with 27 bits for size and 5 bits for
125 /// the block id. Both formats are used in a bc file with version 1.3.
126 /// Previously only the long format was used.
129 BytecodeBlock(const BytecodeBlock &); // do not implement
130 void operator=(const BytecodeBlock &); // do not implement
132 inline BytecodeBlock(unsigned ID, BytecodeWriter& w,
133 bool elideIfEmpty = false, bool hasLongFormat = false);
135 inline ~BytecodeBlock();
138 } // End llvm namespace