For PR411:
[oota-llvm.git] / lib / Bytecode / Writer / WriterInternals.h
1 //===- WriterInternals.h - Data structures shared by the Writer -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This header defines the interface used between components of the bytecode
11 // writer.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_LIB_BYTECODE_WRITER_WRITERINTERNALS_H
16 #define LLVM_LIB_BYTECODE_WRITER_WRITERINTERNALS_H
17
18 #include "SlotCalculator.h"
19 #include "llvm/Bytecode/Writer.h"
20 #include "llvm/Bytecode/Format.h"
21 #include "llvm/Instruction.h"
22 #include "llvm/Support/DataTypes.h"
23 #include <string>
24 #include <vector>
25
26 namespace llvm {
27   class InlineAsm;
28   class TypeSymbolTable;
29   class ValueSymbolTable;
30
31 class BytecodeWriter {
32   std::vector<unsigned char> &Out;
33   SlotCalculator Table;
34 public:
35   BytecodeWriter(std::vector<unsigned char> &o, const Module *M);
36
37 private:
38   void outputConstants(bool isFunction);
39   void outputConstantStrings();
40   void outputFunction(const Function *F);
41   void outputCompactionTable();
42   void outputCompactionTypes(unsigned StartNo);
43   void outputCompactionTablePlane(unsigned PlaneNo,
44                                   const std::vector<const Value*> &TypePlane,
45                                   unsigned StartNo);
46   void outputInstructions(const Function *F);
47   void outputInstruction(const Instruction &I);
48   void outputInstructionFormat0(const Instruction *I, unsigned Opcode,
49                                 const SlotCalculator &Table,
50                                 unsigned Type);
51   void outputInstrVarArgsCall(const Instruction *I,
52                               unsigned Opcode,
53                               const SlotCalculator &Table,
54                               unsigned Type) ;
55   inline void outputInstructionFormat1(const Instruction *I,
56                                        unsigned Opcode,
57                                        unsigned *Slots,
58                                        unsigned Type) ;
59   inline void outputInstructionFormat2(const Instruction *I,
60                                        unsigned Opcode,
61                                        unsigned *Slots,
62                                        unsigned Type) ;
63   inline void outputInstructionFormat3(const Instruction *I,
64                                        unsigned Opcode,
65                                        unsigned *Slots,
66                                        unsigned Type) ;
67
68   void outputModuleInfoBlock(const Module *C);
69   void outputTypeSymbolTable(const TypeSymbolTable &TST);
70   void outputValueSymbolTable(const ValueSymbolTable &ST);
71   void outputTypes(unsigned StartNo);
72   void outputConstantsInPlane(const std::vector<const Value*> &Plane,
73                               unsigned StartNo);
74   void outputConstant(const Constant *CPV);
75   void outputInlineAsm(const InlineAsm *IA);
76   void outputType(const Type *T);
77
78   /// @brief Unsigned integer output primitive
79   inline void output(unsigned i, int pos = -1);
80
81   /// @brief Signed integer output primitive
82   inline void output(int i);
83
84   /// @brief 64-bit variable bit rate output primitive.
85   inline void output_vbr(uint64_t i);
86
87   /// @brief 32-bit variable bit rate output primitive.
88   inline void output_vbr(unsigned i);
89
90   /// @brief Signed 64-bit variable bit rate output primitive.
91   inline void output_vbr(int64_t i);
92
93   /// @brief Signed 32-bit variable bit rate output primitive.
94   inline void output_vbr(int i);
95
96   inline void output(const std::string &s);
97
98   inline void output_data(const void *Ptr, const void *End);
99
100   inline void output_float(float& FloatVal);
101   inline void output_double(double& DoubleVal);
102
103   inline void output_typeid(unsigned i);
104
105   inline size_t size() const { return Out.size(); }
106   inline void resize(size_t S) { Out.resize(S); }
107   friend class BytecodeBlock;
108 };
109
110 /// BytecodeBlock - Little helper class is used by the bytecode writer to help
111 /// do backpatching of bytecode block sizes really easily.  It backpatches when
112 /// it goes out of scope.
113 ///
114 class BytecodeBlock {
115   unsigned Id;
116   unsigned Loc;
117   BytecodeWriter& Writer;
118
119   /// ElideIfEmpty - If this is true and the bytecode block ends up being empty,
120   /// the block can remove itself from the output stream entirely.
121   bool ElideIfEmpty;
122
123   /// If this is true then the block is written with a long format header using
124   /// a uint (32-bits) for both the block id and size. Otherwise, it uses the
125   /// short format which is a single uint with 27 bits for size and 5 bits for
126   /// the block id. Both formats are used in a bc file with version 1.3.
127   /// Previously only the long format was used.
128   bool HasLongFormat;
129
130   BytecodeBlock(const BytecodeBlock &);   // do not implement
131   void operator=(const BytecodeBlock &);  // do not implement
132 public:
133   inline BytecodeBlock(unsigned ID, BytecodeWriter& w,
134                        bool elideIfEmpty = false, bool hasLongFormat = false);
135
136   inline ~BytecodeBlock();
137 };
138
139 } // End llvm namespace
140
141 #endif