Skip to content

Commit 7ecb66d

Browse files
committed
Copy BitCodes.h to NaCl/NaClBitCodes.h
Create NaCl specific version of BitCodes.h so that we can custimize it for the PNaCl wire format. Moves enums to namespace naclbitc. Renames classes using NaCl prefix. Fixes references so that files compiles. BUG= https://code.google.com/p/nativeclient/issues/detail?id=3405 [email protected] Review URL: https://codereview.chromium.org/14682013
1 parent b56422f commit 7ecb66d

File tree

7 files changed

+422
-229
lines changed

7 files changed

+422
-229
lines changed
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
//===- NaClBitCodes.h - Enum values for the bitcode format ------*- C++ -*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// This header Bitcode enum values.
11+
//
12+
// The enum values defined in this file should be considered permanent. If
13+
// new features are added, they should have values added at the end of the
14+
// respective lists.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
18+
#ifndef LLVM_BITCODE_NACL_NACLBITCODES_H
19+
#define LLVM_BITCODE_NACL_NACLBITCODES_H
20+
21+
#include "llvm/ADT/SmallVector.h"
22+
#include "llvm/Support/DataTypes.h"
23+
#include "llvm/Support/ErrorHandling.h"
24+
#include <cassert>
25+
26+
namespace llvm {
27+
namespace naclbitc {
28+
enum StandardWidths {
29+
BlockIDWidth = 8, // We use VBR-8 for block IDs.
30+
CodeLenWidth = 4, // Codelen are VBR-4.
31+
BlockSizeWidth = 32 // BlockSize up to 2^32 32-bit words = 16GB per block.
32+
};
33+
34+
// The standard abbrev namespace always has a way to exit a block, enter a
35+
// nested block, define abbrevs, and define an unabbreviated record.
36+
enum FixedAbbrevIDs {
37+
END_BLOCK = 0, // Must be zero to guarantee termination for broken bitcode.
38+
ENTER_SUBBLOCK = 1,
39+
40+
/// DEFINE_ABBREV - Defines an abbrev for the current block. It consists
41+
/// of a vbr5 for # operand infos. Each operand info is emitted with a
42+
/// single bit to indicate if it is a literal encoding. If so, the value is
43+
/// emitted with a vbr8. If not, the encoding is emitted as 3 bits followed
44+
/// by the info value as a vbr5 if needed.
45+
DEFINE_ABBREV = 2,
46+
47+
// UNABBREV_RECORDs are emitted with a vbr6 for the record code, followed by
48+
// a vbr6 for the # operands, followed by vbr6's for each operand.
49+
UNABBREV_RECORD = 3,
50+
51+
// This is not a code, this is a marker for the first abbrev assignment.
52+
FIRST_APPLICATION_ABBREV = 4
53+
};
54+
55+
/// StandardBlockIDs - All bitcode files can optionally include a BLOCKINFO
56+
/// block, which contains metadata about other blocks in the file.
57+
enum StandardBlockIDs {
58+
/// BLOCKINFO_BLOCK is used to define metadata about blocks, for example,
59+
/// standard abbrevs that should be available to all blocks of a specified
60+
/// ID.
61+
BLOCKINFO_BLOCK_ID = 0,
62+
63+
// Block IDs 1-7 are reserved for future expansion.
64+
FIRST_APPLICATION_BLOCKID = 8
65+
};
66+
67+
/// BlockInfoCodes - The blockinfo block contains metadata about user-defined
68+
/// blocks.
69+
enum BlockInfoCodes {
70+
// DEFINE_ABBREV has magic semantics here, applying to the current SETBID'd
71+
// block, instead of the BlockInfo block.
72+
73+
BLOCKINFO_CODE_SETBID = 1, // SETBID: [blockid#]
74+
BLOCKINFO_CODE_BLOCKNAME = 2, // BLOCKNAME: [name]
75+
BLOCKINFO_CODE_SETRECORDNAME = 3 // BLOCKINFO_CODE_SETRECORDNAME:
76+
// [id, name]
77+
};
78+
79+
} // End naclbitc namespace
80+
81+
/// NaClBitCodeAbbrevOp - This describes one or more operands in an abbreviation.
82+
/// This is actually a union of two different things:
83+
/// 1. It could be a literal integer value ("the operand is always 17").
84+
/// 2. It could be an encoding specification ("this operand encoded like so").
85+
///
86+
class NaClBitCodeAbbrevOp {
87+
uint64_t Val; // A literal value or data for an encoding.
88+
bool IsLiteral : 1; // Indicate whether this is a literal value or not.
89+
unsigned Enc : 3; // The encoding to use.
90+
public:
91+
enum Encoding {
92+
Fixed = 1, // A fixed width field, Val specifies number of bits.
93+
VBR = 2, // A VBR field where Val specifies the width of each chunk.
94+
Array = 3, // A sequence of fields, next field species elt encoding.
95+
Char6 = 4, // A 6-bit fixed field which maps to [a-zA-Z0-9._].
96+
Blob = 5 // 32-bit aligned array of 8-bit characters.
97+
};
98+
99+
explicit NaClBitCodeAbbrevOp(uint64_t V) : Val(V), IsLiteral(true) {}
100+
explicit NaClBitCodeAbbrevOp(Encoding E, uint64_t Data = 0)
101+
: Val(Data), IsLiteral(false), Enc(E) {}
102+
103+
bool isLiteral() const { return IsLiteral; }
104+
bool isEncoding() const { return !IsLiteral; }
105+
106+
// Accessors for literals.
107+
uint64_t getLiteralValue() const { assert(isLiteral()); return Val; }
108+
109+
// Accessors for encoding info.
110+
Encoding getEncoding() const { assert(isEncoding()); return (Encoding)Enc; }
111+
uint64_t getEncodingData() const {
112+
assert(isEncoding() && hasEncodingData());
113+
return Val;
114+
}
115+
116+
bool hasEncodingData() const { return hasEncodingData(getEncoding()); }
117+
static bool hasEncodingData(Encoding E) {
118+
switch (E) {
119+
case Fixed:
120+
case VBR:
121+
return true;
122+
case Array:
123+
case Char6:
124+
case Blob:
125+
return false;
126+
}
127+
llvm_unreachable("Invalid encoding");
128+
}
129+
130+
/// isChar6 - Return true if this character is legal in the Char6 encoding.
131+
static bool isChar6(char C) {
132+
if (C >= 'a' && C <= 'z') return true;
133+
if (C >= 'A' && C <= 'Z') return true;
134+
if (C >= '0' && C <= '9') return true;
135+
if (C == '.' || C == '_') return true;
136+
return false;
137+
}
138+
static unsigned EncodeChar6(char C) {
139+
if (C >= 'a' && C <= 'z') return C-'a';
140+
if (C >= 'A' && C <= 'Z') return C-'A'+26;
141+
if (C >= '0' && C <= '9') return C-'0'+26+26;
142+
if (C == '.') return 62;
143+
if (C == '_') return 63;
144+
llvm_unreachable("Not a value Char6 character!");
145+
}
146+
147+
static char DecodeChar6(unsigned V) {
148+
assert((V & ~63) == 0 && "Not a Char6 encoded character!");
149+
if (V < 26) return V+'a';
150+
if (V < 26+26) return V-26+'A';
151+
if (V < 26+26+10) return V-26-26+'0';
152+
if (V == 62) return '.';
153+
if (V == 63) return '_';
154+
llvm_unreachable("Not a value Char6 character!");
155+
}
156+
157+
};
158+
159+
template <> struct isPodLike<NaClBitCodeAbbrevOp> {
160+
static const bool value=true;
161+
};
162+
163+
/// NaClBitCodeAbbrev - This class represents an abbreviation record. An
164+
/// abbreviation allows a complex record that has redundancy to be stored in a
165+
/// specialized format instead of the fully-general, fully-vbr, format.
166+
class NaClBitCodeAbbrev {
167+
SmallVector<NaClBitCodeAbbrevOp, 32> OperandList;
168+
unsigned char RefCount; // Number of things using this.
169+
~NaClBitCodeAbbrev() {}
170+
public:
171+
NaClBitCodeAbbrev() : RefCount(1) {}
172+
173+
void addRef() { ++RefCount; }
174+
void dropRef() { if (--RefCount == 0) delete this; }
175+
176+
unsigned getNumOperandInfos() const {
177+
return static_cast<unsigned>(OperandList.size());
178+
}
179+
const NaClBitCodeAbbrevOp &getOperandInfo(unsigned N) const {
180+
return OperandList[N];
181+
}
182+
183+
void Add(const NaClBitCodeAbbrevOp &OpInfo) {
184+
OperandList.push_back(OpInfo);
185+
}
186+
};
187+
} // End llvm namespace
188+
189+
#endif

include/llvm/Bitcode/NaCl/NaClBitstreamReader.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class NaClBitstreamReader {
3939
/// These describe abbreviations that all blocks of the specified ID inherit.
4040
struct BlockInfo {
4141
unsigned BlockID;
42-
std::vector<BitCodeAbbrev*> Abbrevs;
42+
std::vector<NaClBitCodeAbbrev*> Abbrevs;
4343
std::string Name;
4444

4545
std::vector<std::pair<unsigned, std::string> > RecordNames;
@@ -189,11 +189,11 @@ class NaClBitstreamCursor {
189189
unsigned CurCodeSize;
190190

191191
/// CurAbbrevs - Abbrevs installed at in this block.
192-
std::vector<BitCodeAbbrev*> CurAbbrevs;
192+
std::vector<NaClBitCodeAbbrev*> CurAbbrevs;
193193

194194
struct Block {
195195
unsigned PrevCodeSize;
196-
std::vector<BitCodeAbbrev*> PrevAbbrevs;
196+
std::vector<NaClBitCodeAbbrev*> PrevAbbrevs;
197197
explicit Block(unsigned PCS) : PrevCodeSize(PCS) {}
198198
};
199199

@@ -286,17 +286,17 @@ class NaClBitstreamCursor {
286286
NaClBitstreamEntry advance(unsigned Flags = 0) {
287287
while (1) {
288288
unsigned Code = ReadCode();
289-
if (Code == bitc::END_BLOCK) {
289+
if (Code == naclbitc::END_BLOCK) {
290290
// Pop the end of the block unless Flags tells us not to.
291291
if (!(Flags & AF_DontPopBlockAtEnd) && ReadBlockEnd())
292292
return NaClBitstreamEntry::getError();
293293
return NaClBitstreamEntry::getEndBlock();
294294
}
295295

296-
if (Code == bitc::ENTER_SUBBLOCK)
296+
if (Code == naclbitc::ENTER_SUBBLOCK)
297297
return NaClBitstreamEntry::getSubBlock(ReadSubBlockID());
298298

299-
if (Code == bitc::DEFINE_ABBREV &&
299+
if (Code == naclbitc::DEFINE_ABBREV &&
300300
!(Flags & AF_DontAutoprocessAbbrevs)) {
301301
// We read and accumulate abbrev's, the client can't do anything with
302302
// them anyway.
@@ -469,7 +469,7 @@ class NaClBitstreamCursor {
469469
/// ReadSubBlockID - Having read the ENTER_SUBBLOCK code, read the BlockID for
470470
/// the block.
471471
unsigned ReadSubBlockID() {
472-
return ReadVBR(bitc::BlockIDWidth);
472+
return ReadVBR(naclbitc::BlockIDWidth);
473473
}
474474

475475
/// SkipBlock - Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip
@@ -478,9 +478,9 @@ class NaClBitstreamCursor {
478478
bool SkipBlock() {
479479
// Read and ignore the codelen value. Since we are skipping this block, we
480480
// don't care what code widths are used inside of it.
481-
ReadVBR(bitc::CodeLenWidth);
481+
ReadVBR(naclbitc::CodeLenWidth);
482482
SkipToFourByteBoundary();
483-
unsigned NumFourBytes = Read(bitc::BlockSizeWidth);
483+
unsigned NumFourBytes = Read(naclbitc::BlockSizeWidth);
484484

485485
// Check that the block wasn't partially defined, and that the offset isn't
486486
// bogus.
@@ -526,17 +526,17 @@ class NaClBitstreamCursor {
526526
//===--------------------------------------------------------------------===//
527527

528528
private:
529-
void readAbbreviatedLiteral(const BitCodeAbbrevOp &Op,
529+
void readAbbreviatedLiteral(const NaClBitCodeAbbrevOp &Op,
530530
SmallVectorImpl<uint64_t> &Vals);
531-
void readAbbreviatedField(const BitCodeAbbrevOp &Op,
531+
void readAbbreviatedField(const NaClBitCodeAbbrevOp &Op,
532532
SmallVectorImpl<uint64_t> &Vals);
533-
void skipAbbreviatedField(const BitCodeAbbrevOp &Op);
533+
void skipAbbreviatedField(const NaClBitCodeAbbrevOp &Op);
534534

535535
public:
536536

537537
/// getAbbrev - Return the abbreviation for the specified AbbrevId.
538-
const BitCodeAbbrev *getAbbrev(unsigned AbbrevID) {
539-
unsigned AbbrevNo = AbbrevID-bitc::FIRST_APPLICATION_ABBREV;
538+
const NaClBitCodeAbbrev *getAbbrev(unsigned AbbrevID) {
539+
unsigned AbbrevNo = AbbrevID-naclbitc::FIRST_APPLICATION_ABBREV;
540540
assert(AbbrevNo < CurAbbrevs.size() && "Invalid abbrev #!");
541541
return CurAbbrevs[AbbrevNo];
542542
}

0 commit comments

Comments
 (0)