Chris Lattner | cf3056d | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 1 | //===- WriterInternals.h - Data structures shared by the Writer -*- C++ -*-===// |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 2 | // |
John Criswell | 856ba76 | 2003-10-21 15:17:13 +0000 | [diff] [blame] | 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. |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 7 | // |
John Criswell | 856ba76 | 2003-10-21 15:17:13 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
| 10 | // This header defines the interface used between components of the bytecode |
| 11 | // writer. |
| 12 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_LIB_BYTECODE_WRITER_WRITERINTERNALS_H |
| 16 | #define LLVM_LIB_BYTECODE_WRITER_WRITERINTERNALS_H |
| 17 | |
Reid Spencer | d1fb1b7 | 2004-07-04 11:44:27 +0000 | [diff] [blame] | 18 | #include "SlotCalculator.h" |
| 19 | #include "llvm/Bytecode/Writer.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 20 | #include "llvm/Bytecode/Format.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 21 | #include "llvm/Instruction.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 22 | #include "llvm/Support/DataTypes.h" |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <vector> |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 25 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 26 | namespace llvm { |
Chris Lattner | 3bc5a60 | 2006-01-25 23:08:15 +0000 | [diff] [blame] | 27 | class InlineAsm; |
Reid Spencer | 78d033e | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 28 | class TypeSymbolTable; |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 29 | class ValueSymbolTable; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 30 | |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 31 | class BytecodeWriter { |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 32 | std::vector<unsigned char> &Out; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 33 | SlotCalculator Table; |
| 34 | public: |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 35 | BytecodeWriter(std::vector<unsigned char> &o, const Module *M); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 83bb3d2 | 2004-01-14 23:36:54 +0000 | [diff] [blame] | 37 | private: |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 38 | void outputConstants(bool isFunction); |
Chris Lattner | 83bb3d2 | 2004-01-14 23:36:54 +0000 | [diff] [blame] | 39 | void outputConstantStrings(); |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 40 | void outputFunction(const Function *F); |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 41 | void outputCompactionTable(); |
Reid Spencer | d1fb1b7 | 2004-07-04 11:44:27 +0000 | [diff] [blame] | 42 | void outputCompactionTypes(unsigned StartNo); |
Chris Lattner | cf3e67f | 2004-01-18 21:08:52 +0000 | [diff] [blame] | 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); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 48 | void outputInstructionFormat0(const Instruction *I, unsigned Opcode, |
Misha Brukman | 23c6d2c | 2005-04-21 21:48:46 +0000 | [diff] [blame] | 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) ; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 67 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 68 | void outputModuleInfoBlock(const Module *C); |
Reid Spencer | 78d033e | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 69 | void outputTypeSymbolTable(const TypeSymbolTable &TST); |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 70 | void outputValueSymbolTable(const ValueSymbolTable &ST); |
Reid Spencer | d1fb1b7 | 2004-07-04 11:44:27 +0000 | [diff] [blame] | 71 | void outputTypes(unsigned StartNo); |
Vikram S. Adve | 054bd68 | 2002-07-14 23:05:53 +0000 | [diff] [blame] | 72 | void outputConstantsInPlane(const std::vector<const Value*> &Plane, |
| 73 | unsigned StartNo); |
Chris Lattner | 83bb3d2 | 2004-01-14 23:36:54 +0000 | [diff] [blame] | 74 | void outputConstant(const Constant *CPV); |
Chris Lattner | 3bc5a60 | 2006-01-25 23:08:15 +0000 | [diff] [blame] | 75 | void outputInlineAsm(const InlineAsm *IA); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 76 | void outputType(const Type *T); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 3bc5a60 | 2006-01-25 23:08:15 +0000 | [diff] [blame] | 96 | inline void output(const std::string &s); |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 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; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
Chris Lattner | 0baa0af | 2004-01-15 21:06:57 +0000 | [diff] [blame] | 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 | /// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 114 | class BytecodeBlock { |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 115 | unsigned Id; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 116 | unsigned Loc; |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 117 | BytecodeWriter& Writer; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 118 | |
Chris Lattner | 0baa0af | 2004-01-15 21:06:57 +0000 | [diff] [blame] | 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 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 130 | BytecodeBlock(const BytecodeBlock &); // do not implement |
| 131 | void operator=(const BytecodeBlock &); // do not implement |
| 132 | public: |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 133 | inline BytecodeBlock(unsigned ID, BytecodeWriter& w, |
| 134 | bool elideIfEmpty = false, bool hasLongFormat = false); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 135 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 136 | inline ~BytecodeBlock(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 139 | } // End llvm namespace |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 140 | |
| 141 | #endif |