llvm.org GIT mirror llvm / b35ca9d
Define the content-independent interfaces to read/write bitcode files and the high-level interface to read/write LLVM IR bitcode files. This is a work in progress. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36329 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 13 years ago
4 changed file(s) with 523 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 //===- BitCodes.h - Enum values for the bitcode format ----------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by Chris Lattner and is distributed under
5 // the University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This header Bitcode enum values.
10 //
11 // The enum values defined in this file should be considered permanent. If
12 // new features are added, they should have values added at the end of the
13 // respective lists.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_BITCODE_BITCODES_H
18 #define LLVM_BITCODE_BITCODES_H
19
20 namespace llvm {
21 namespace bitc {
22 enum StandardWidths {
23 BlockIDWidth = 8, // We use VBR-8 for block IDs.
24 CodeLenWidth = 4, // Codelen are VBR-4.
25 BlockSizeWidth = 32 // BlockSize up to 2^32 32-bit words = 32GB per block.
26 };
27
28 // The standard code namespace always has a way to exit a block, enter a
29 // nested block, define abbrevs, and define an unabbreviated record.
30 enum FixedCodes {
31 END_BLOCK = 0, // Must be zero to guarantee termination for broken bitcode.
32 ENTER_SUBBLOCK = 1,
33
34 // Two codes are reserved for defining abbrevs and for emitting an
35 // unabbreviated record.
36 DEFINE_ABBREVS = 2,
37 UNABBREV_RECORD = 3,
38
39 // This is not a code, this is a marker for the first abbrev assignment.
40 FIRST_ABBREV = 4
41 };
42 } // End bitc namespace
43 } // End llvm namespace
44
45 #endif
0 //===- BitstreamReader.h - Low-level bitstream reader interface -*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by Chris Lattner and is distributed under
5 // the University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This header defines the BitstreamReader class. This class can be used to
10 // read an arbitrary bitstream, regardless of its contents.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef BITSTREAM_READER_H
15 #define BITSTREAM_READER_H
16
17 #include "llvm/Bitcode/BitCodes.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include
20
21 namespace llvm {
22
23 class BitstreamReader {
24 const unsigned char *NextChar;
25 const unsigned char *LastChar;
26
27 /// CurWord - This is the current data we have pulled from the stream but have
28 /// not returned to the client.
29 uint32_t CurWord;
30
31 /// BitsInCurWord - This is the number of bits in CurWord that are valid. This
32 /// is always from [0...31] inclusive.
33 unsigned BitsInCurWord;
34
35 // CurCodeSize - This is the declared size of code values used for the current
36 // block, in bits.
37 unsigned CurCodeSize;
38
39 /// BlockScope - This tracks the codesize of parent blocks.
40 SmallVector BlockScope;
41
42 public:
43 BitstreamReader(const unsigned char *Start, const unsigned char *End)
44 : NextChar(Start), LastChar(End) {
45 assert(((End-Start) & 3) == 0 &&"Bitcode stream not a multiple of 4 bytes");
46 CurWord = 0;
47 BitsInCurWord = 0;
48 CurCodeSize = 2;
49 }
50
51 bool AtEndOfStream() const { return NextChar == LastChar; }
52
53 uint32_t Read(unsigned NumBits) {
54 // If the field is fully contained by CurWord, return it quickly.
55 if (BitsInCurWord >= NumBits) {
56 uint32_t R = CurWord & ((1U << NumBits)-1);
57 CurWord >>= NumBits;
58 BitsInCurWord -= NumBits;
59 return R;
60 }
61
62 // If we run out of data, stop at the end of the stream.
63 if (LastChar == NextChar) {
64 CurWord = 0;
65 BitsInCurWord = 0;
66 return 0;
67 }
68
69 unsigned R = CurWord;
70
71 // Read the next word from the stream.
72 CurWord = (NextChar[0] << 0) | (NextChar[1] << 8) |
73 (NextChar[2] << 16) | (NextChar[3] << 24);
74 NextChar += 4;
75
76 // Extract NumBits-BitsInCurWord from what we just read.
77 unsigned BitsLeft = NumBits-BitsInCurWord;
78
79 // Be careful here, BitsLeft is in the range [1..32] inclusive.
80 R |= (CurWord & (~0U >> (32-BitsLeft))) << BitsInCurWord;
81
82 // BitsLeft bits have just been used up from CurWord.
83 if (BitsLeft != 32)
84 CurWord >>= BitsLeft;
85 else
86 CurWord = 0;
87 BitsInCurWord = 32-BitsLeft;
88 return R;
89 }
90
91 uint32_t ReadVBR(unsigned NumBits) {
92 uint32_t Piece = Read(NumBits);
93 if ((Piece & (1U << NumBits-1)) == 0)
94 return Piece;
95
96 uint32_t Result = 0;
97 unsigned NextBit = 0;
98 while (1) {
99 Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit;
100
101 if ((Piece & (1U << NumBits-1)) == 0)
102 return Result;
103
104 NextBit += NumBits-1;
105 Piece = Read(NumBits);
106 }
107 }
108
109 uint64_t ReadVBR64(unsigned NumBits) {
110 uint64_t Piece = Read(NumBits);
111 if ((Piece & (1U << NumBits-1)) == 0)
112 return Piece;
113
114 uint64_t Result = 0;
115 unsigned NextBit = 0;
116 while (1) {
117 Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit;
118
119 if ((Piece & (1U << NumBits-1)) == 0)
120 return Result;
121
122 NextBit += NumBits-1;
123 Piece = Read(NumBits);
124 }
125 }
126
127 void SkipToWord() {
128 BitsInCurWord = 0;
129 CurWord = 0;
130 }
131
132
133 unsigned ReadCode() {
134 return Read(CurCodeSize);
135 }
136
137 //===--------------------------------------------------------------------===//
138 // Block Manipulation
139 //===--------------------------------------------------------------------===//
140
141 // Block header:
142 // [ENTER_SUBBLOCK, blockid, newcodelen, , blocklen]
143
144 /// ReadSubBlockID - Having read the ENTER_SUBBLOCK code, read the BlockID for
145 /// the block.
146 unsigned ReadSubBlockID() {
147 return ReadVBR(bitc::BlockIDWidth);
148 }
149
150 /// SkipBlock - Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip
151 /// over the body of this block. If the block record is malformed, return
152 /// true.
153 bool SkipBlock() {
154 // Read and ignore the codelen value. Since we are skipping this block, we
155 // don't care what code widths are used inside of it.
156 ReadVBR(bitc::CodeLenWidth);
157 SkipToWord();
158 unsigned NumWords = Read(bitc::BlockSizeWidth);
159
160 // Check that the block wasn't partially defined, and that the offset isn't
161 // bogus.
162 if (AtEndOfStream() || NextChar+NumWords*4 > LastChar)
163 return true;
164
165 NextChar += NumWords*4;
166 return false;
167 }
168
169 /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, read and enter
170 /// the block, returning the BlockID of the block we just entered.
171 bool EnterSubBlock() {
172 BlockScope.push_back(CurCodeSize);
173
174 // Get the codesize of this block.
175 CurCodeSize = ReadVBR(bitc::CodeLenWidth);
176 SkipToWord();
177 unsigned NumWords = Read(bitc::BlockSizeWidth);
178
179 // Validate that this block is sane.
180 if (CurCodeSize == 0 || AtEndOfStream() || NextChar+NumWords*4 > LastChar)
181 return true;
182
183 return false;
184 }
185
186 bool ReadBlockEnd() {
187 if (BlockScope.empty()) return true;
188
189 // Block tail:
190 // [END_BLOCK, ]
191 SkipToWord();
192 CurCodeSize = BlockScope.back();
193 BlockScope.pop_back();
194 return false;
195 }
196
197 //===--------------------------------------------------------------------===//
198 // Record Processing
199 //===--------------------------------------------------------------------===//
200
201 unsigned ReadRecord(unsigned AbbrevID, SmallVectorImpl &Vals) {
202 if (AbbrevID == bitc::UNABBREV_RECORD) {
203 unsigned Code = ReadVBR(6);
204 unsigned NumElts = ReadVBR(6);
205 for (unsigned i = 0; i != NumElts; ++i)
206 Vals.push_back(ReadVBR64(6));
207 return Code;
208 }
209
210 assert(0 && "Reading with abbrevs not implemented!");
211 }
212
213 };
214
215 } // End llvm namespace
216
217 #endif
218
219
0 //===- BitstreamWriter.h - Low-level bitstream writer interface -*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by Chris Lattner and is distributed under
5 // the University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This header defines the BitstreamWriter class. This class can be used to
10 // write an arbitrary bitstream, regardless of its contents.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef BITSTREAM_WRITER_H
15 #define BITSTREAM_WRITER_H
16
17 #include "llvm/Bitcode/BitCodes.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include
20 #include
21
22 namespace llvm {
23
24 class BitstreamWriter {
25 std::vector &Out;
26
27 /// CurBit - Always between 0 and 31 inclusive, specifies the next bit to use.
28 unsigned CurBit;
29
30 /// CurValue - The current value. Only bits < CurBit are valid.
31 uint32_t CurValue;
32
33 // CurCodeSize - This is the declared size of code values used for the current
34 // block, in bits.
35 unsigned CurCodeSize;
36
37 struct Block {
38 unsigned PrevCodeSize;
39 unsigned StartSizeWord;
40 Block(unsigned PCS, unsigned SSW) : PrevCodeSize(PCS), StartSizeWord(SSW) {}
41 };
42
43 /// BlockScope - This tracks the current blocks that we have entered.
44 std::vector BlockScope;
45 public:
46 BitstreamWriter(std::vector &O)
47 : Out(O), CurBit(0), CurValue(0), CurCodeSize(2) {}
48
49 ~BitstreamWriter() {
50 assert(CurBit == 0 && "Unflused data remaining");
51 assert(BlockScope.empty() && "Block imbalance");
52 }
53 //===--------------------------------------------------------------------===//
54 // Basic Primitives for emitting bits to the stream.
55 //===--------------------------------------------------------------------===//
56
57 void Emit(uint32_t Val, unsigned NumBits) {
58 assert(NumBits <= 32 && "Invalid value size!");
59 assert((Val & ~(~0U >> (32-NumBits))) == 0 && "High bits set!");
60 CurValue |= Val << CurBit;
61 if (CurBit + NumBits < 32) {
62 CurBit += NumBits;
63 return;
64 }
65
66 // Add the current word.
67 unsigned V = CurValue;
68 Out.push_back((unsigned char)(V >> 0));
69 Out.push_back((unsigned char)(V >> 8));
70 Out.push_back((unsigned char)(V >> 16));
71 Out.push_back((unsigned char)(V >> 24));
72
73 if (CurBit)
74 CurValue = Val >> 32-CurBit;
75 else
76 CurValue = 0;
77 CurBit = (CurBit+NumBits) & 31;
78 }
79
80 void Emit64(uint64_t Val, unsigned NumBits) {
81 if (NumBits <= 32)
82 Emit((uint32_t)Val, NumBits);
83 else {
84 Emit((uint32_t)Val, 32);
85 Emit((uint32_t)(Val >> 32), NumBits-32);
86 }
87 }
88
89 void FlushToWord() {
90 if (CurBit) {
91 unsigned V = CurValue;
92 Out.push_back((unsigned char)(V >> 0));
93 Out.push_back((unsigned char)(V >> 8));
94 Out.push_back((unsigned char)(V >> 16));
95 Out.push_back((unsigned char)(V >> 24));
96 CurBit = 0;
97 CurValue = 0;
98 }
99 }
100
101 void EmitVBR(uint32_t Val, unsigned NumBits) {
102 uint32_t Threshold = 1U << (NumBits-1);
103
104 // Emit the bits with VBR encoding, NumBits-1 bits at a time.
105 while (Val >= Threshold) {
106 Emit((Val & ((1 << (NumBits-1))-1)) | (1 << (NumBits-1)), NumBits);
107 Val >>= NumBits-1;
108 }
109
110 Emit(Val, NumBits);
111 }
112
113 void EmitVBR64(uint64_t Val, unsigned NumBits) {
114 if ((uint32_t)Val == Val)
115 return EmitVBR((uint32_t)Val, NumBits);
116
117 uint64_t Threshold = 1U << (NumBits-1);
118
119 // Emit the bits with VBR encoding, NumBits-1 bits at a time.
120 while (Val >= Threshold) {
121 Emit(((uint32_t)Val & ((1 << (NumBits-1))-1)) |
122 (1 << (NumBits-1)), NumBits);
123 Val >>= NumBits-1;
124 }
125
126 Emit((uint32_t)Val, NumBits);
127 }
128
129 /// EmitCode - Emit the specified code.
130 void EmitCode(unsigned Val) {
131 Emit(Val, CurCodeSize);
132 }
133
134 //===--------------------------------------------------------------------===//
135 // Block Manipulation
136 //===--------------------------------------------------------------------===//
137
138 void EnterSubblock(unsigned BlockID, unsigned CodeLen) {
139 // Block header:
140 // [ENTER_SUBBLOCK, blockid, newcodelen, , blocklen]
141 EmitCode(bitc::ENTER_SUBBLOCK);
142 EmitVBR(BlockID, bitc::BlockIDWidth);
143 EmitVBR(CodeLen, bitc::CodeLenWidth);
144 FlushToWord();
145 BlockScope.push_back(Block(CurCodeSize, Out.size()/4));
146 // Emit a placeholder, which will be replaced when the block is popped.
147 Emit(0, bitc::BlockSizeWidth);
148
149 CurCodeSize = CodeLen;
150 }
151
152 void ExitBlock() {
153 assert(!BlockScope.empty() && "Block scope imbalance!");
154 Block B = BlockScope.back();
155 BlockScope.pop_back();
156
157 // Block tail:
158 // [END_BLOCK, ]
159 EmitCode(bitc::END_BLOCK);
160 FlushToWord();
161
162 // Compute the size of the block, in words, not counting the size field.
163 unsigned SizeInWords = Out.size()/4-B.StartSizeWord - 1;
164 unsigned ByteNo = B.StartSizeWord*4;
165
166 // Update the block size field in the header of this sub-block.
167 Out[ByteNo++] = (unsigned char)(SizeInWords >> 0);
168 Out[ByteNo++] = (unsigned char)(SizeInWords >> 8);
169 Out[ByteNo++] = (unsigned char)(SizeInWords >> 16);
170 Out[ByteNo++] = (unsigned char)(SizeInWords >> 24);
171
172 // Restore the outer block's code size.
173 CurCodeSize = B.PrevCodeSize;
174 }
175
176 //===--------------------------------------------------------------------===//
177 // Record Emission
178 //===--------------------------------------------------------------------===//
179
180 /// EmitRecord - Emit the specified record to the stream, using an abbrev if
181 /// we have one to compress the output.
182 void EmitRecord(unsigned Code, SmallVectorImpl &Vals,
183 unsigned Abbrev = 0) {
184 if (Abbrev) {
185 assert(0 && "abbrevs not implemented yet!");
186 } else {
187 // If we don't have an abbrev to use, emit this in its fully unabbreviated
188 // form.
189 EmitCode(bitc::UNABBREV_RECORD);
190 EmitVBR(Code, 6);
191 EmitVBR(Vals.size(), 6);
192 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
193 EmitVBR64(Vals[i], 6);
194 }
195 }
196
197 /// EmitRecord - Emit the specified record to the stream, using an abbrev if
198 /// we have one to compress the output.
199 void EmitRecord(unsigned Code, SmallVectorImpl &Vals,
200 unsigned Abbrev = 0) {
201 if (Abbrev) {
202 assert(0 && "abbrevs not implemented yet!");
203 } else {
204 // If we don't have an abbrev to use, emit this in its fully unabbreviated
205 // form.
206 EmitCode(bitc::UNABBREV_RECORD);
207 EmitVBR(Code, 6);
208 EmitVBR(Vals.size(), 6);
209 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
210 EmitVBR(Vals[i], 6);
211 }
212 }
213 };
214
215
216 } // End llvm namespace
217
218 #endif
0 //===-- llvm/Bitcode/ReaderWriter.h - Bitcode reader/writers ----*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by Chris Lattner and is distributed under
5 // the University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This header defines interfaces to read and write LLVM bitcode files/streams.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_BITCODE_H
14 #define LLVM_BITCODE_H
15
16 #include
17 #include
18
19 namespace llvm {
20 class Module;
21 class ModuleProvider;
22
23 ModuleProvider *getBitcodeModuleProvider(const std::string &Filename,
24 std::string *ErrMsg = 0);
25
26
27 /// ParseBitcodeFile - Read the specified bitcode file, returning the module.
28 /// If an error occurs, return null and fill in *ErrMsg if non-null.
29 Module *ParseBitcodeFile(const std::string &Filename,
30 std::string *ErrMsg = 0);
31
32 /// WriteBitcodeToFile - Write the specified module to the specified output
33 /// stream.
34 void WriteBitcodeToFile(const Module *M, std::ostream &Out);
35 } // End llvm namespace
36
37 #endif