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