LLVM API Documentation
00001 //===- WriterInternals.h - Data structures shared by the Writer -*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This header defines the interface used between components of the bytecode 00011 // writer. 00012 // 00013 // Note that the performance of this library is not terribly important, because 00014 // it shouldn't be used by JIT type applications... so it is not a huge focus 00015 // at least. :) 00016 // 00017 //===----------------------------------------------------------------------===// 00018 00019 #ifndef LLVM_LIB_BYTECODE_WRITER_WRITERINTERNALS_H 00020 #define LLVM_LIB_BYTECODE_WRITER_WRITERINTERNALS_H 00021 00022 #include "SlotCalculator.h" 00023 #include "llvm/Bytecode/Writer.h" 00024 #include "llvm/Bytecode/Format.h" 00025 #include "llvm/Instruction.h" 00026 #include "llvm/Support/DataTypes.h" 00027 #include <string> 00028 #include <vector> 00029 00030 namespace llvm { 00031 00032 class BytecodeWriter { 00033 std::vector<unsigned char> &Out; 00034 SlotCalculator Table; 00035 public: 00036 BytecodeWriter(std::vector<unsigned char> &o, const Module *M); 00037 00038 private: 00039 void outputConstants(bool isFunction); 00040 void outputConstantStrings(); 00041 void outputFunction(const Function *F); 00042 void outputCompactionTable(); 00043 void outputCompactionTypes(unsigned StartNo); 00044 void outputCompactionTablePlane(unsigned PlaneNo, 00045 const std::vector<const Value*> &TypePlane, 00046 unsigned StartNo); 00047 void outputInstructions(const Function *F); 00048 void outputInstruction(const Instruction &I); 00049 void outputInstructionFormat0(const Instruction *I, unsigned Opcode, 00050 const SlotCalculator &Table, 00051 unsigned Type); 00052 void outputInstrVarArgsCall(const Instruction *I, 00053 unsigned Opcode, 00054 const SlotCalculator &Table, 00055 unsigned Type) ; 00056 inline void outputInstructionFormat1(const Instruction *I, 00057 unsigned Opcode, 00058 unsigned *Slots, 00059 unsigned Type) ; 00060 inline void outputInstructionFormat2(const Instruction *I, 00061 unsigned Opcode, 00062 unsigned *Slots, 00063 unsigned Type) ; 00064 inline void outputInstructionFormat3(const Instruction *I, 00065 unsigned Opcode, 00066 unsigned *Slots, 00067 unsigned Type) ; 00068 00069 void outputModuleInfoBlock(const Module *C); 00070 void outputSymbolTable(const SymbolTable &ST); 00071 void outputTypes(unsigned StartNo); 00072 void outputConstantsInPlane(const std::vector<const Value*> &Plane, 00073 unsigned StartNo); 00074 void outputConstant(const Constant *CPV); 00075 void outputType(const Type *T); 00076 00077 /// @brief Unsigned integer output primitive 00078 inline void output(unsigned i, int pos = -1); 00079 00080 /// @brief Signed integer output primitive 00081 inline void output(int i); 00082 00083 /// @brief 64-bit variable bit rate output primitive. 00084 inline void output_vbr(uint64_t i); 00085 00086 /// @brief 32-bit variable bit rate output primitive. 00087 inline void output_vbr(unsigned i); 00088 00089 /// @brief Signed 64-bit variable bit rate output primitive. 00090 inline void output_vbr(int64_t i); 00091 00092 /// @brief Signed 32-bit variable bit rate output primitive. 00093 inline void output_vbr(int i); 00094 00095 inline void output(const std::string &s ); 00096 00097 inline void output_data(const void *Ptr, const void *End); 00098 00099 inline void output_float(float& FloatVal); 00100 inline void output_double(double& DoubleVal); 00101 00102 inline void output_typeid(unsigned i); 00103 00104 inline size_t size() const { return Out.size(); } 00105 inline void resize(size_t S) { Out.resize(S); } 00106 friend class BytecodeBlock; 00107 }; 00108 00109 /// BytecodeBlock - Little helper class is used by the bytecode writer to help 00110 /// do backpatching of bytecode block sizes really easily. It backpatches when 00111 /// it goes out of scope. 00112 /// 00113 class BytecodeBlock { 00114 unsigned Id; 00115 unsigned Loc; 00116 BytecodeWriter& Writer; 00117 00118 /// ElideIfEmpty - If this is true and the bytecode block ends up being empty, 00119 /// the block can remove itself from the output stream entirely. 00120 bool ElideIfEmpty; 00121 00122 /// If this is true then the block is written with a long format header using 00123 /// a uint (32-bits) for both the block id and size. Otherwise, it uses the 00124 /// short format which is a single uint with 27 bits for size and 5 bits for 00125 /// the block id. Both formats are used in a bc file with version 1.3. 00126 /// Previously only the long format was used. 00127 bool HasLongFormat; 00128 00129 BytecodeBlock(const BytecodeBlock &); // do not implement 00130 void operator=(const BytecodeBlock &); // do not implement 00131 public: 00132 inline BytecodeBlock(unsigned ID, BytecodeWriter& w, 00133 bool elideIfEmpty = false, bool hasLongFormat = false); 00134 00135 inline ~BytecodeBlock(); 00136 }; 00137 00138 } // End llvm namespace 00139 00140 #endif