Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(119)

Side by Side Diff: lib/Bitcode/NaCl/TestUtils/NaClBitcodeMungeReader.cpp

Issue 1113023005: Add abilities to generate bitcode buffers from munged bitcode. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-llvm.git@master
Patch Set: Initial version of code. Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 //===- NaClBitcodeMungeReader.cpp - Read bitcode record list ----*- 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 // Implements bitcode reader for NaClBitcodeRecordList and NaClMungedBitcode.
jvoung (off chromium) 2015/05/05 00:27:23 extra space in " NaClMungedBitcode"
Karl 2015/05/05 22:38:03 Done.
11
12 #include "llvm/Bitcode/NaCl/NaClBitcodeMungeUtils.h"
13
14 #include "llvm/Bitcode/NaCl/NaClBitcodeHeader.h"
15 #include "llvm/Support/MemoryBuffer.h"
16
17 using namespace llvm;
18
19 namespace {
20
21 class BitcodeParser;
22
23 // \brief The state associated with parsing a bitcode buffer.
24 class BitcodeParseState {
25 BitcodeParseState(const BitcodeParseState&) = delete;
26 BitcodeParseState &operator=(const BitcodeParseState&) = delete;
27 public:
28 // \brief Construct the bitcode parse state.
29 //
30 // \param Parser The parser used to parse the bitcode.
31 // \param[out] Records Filled with parsed records.
32 BitcodeParseState(BitcodeParser *Parser,
33 NaClBitcodeRecordList &Records);
34
35 // List to read records into.
36 NaClBitcodeRecordList &Records;
37 // Listener use to get abbreviations as they are read.
jvoung (off chromium) 2015/05/05 00:27:23 "use to get" -> "used to get" ?
Karl 2015/05/05 22:38:04 Done.
38 NaClBitcodeParserListener AbbrevListener;
39 //
jvoung (off chromium) 2015/05/05 00:27:23 Dangling "//"
Karl 2015/05/05 22:38:03 Done.
40 };
41
42 // \brief The bitcode parser to extract bitcode records.
43 class BitcodeParser : public NaClBitcodeParser {
44 BitcodeParser(const BitcodeParser &) = delete;
45 BitcodeParser &operator=(const BitcodeParser&) = delete;
46 public:
47 // \brief Top-level constructor for a bitcode parser.
48 //
49 // \param Cursor The beginning position of the bitcode to parse.
50 // \param[out] Records Filled with parsed records.
51 BitcodeParser(NaClBitstreamCursor &Cursor,
52 NaClBitcodeRecordList &Records)
53 : NaClBitcodeParser(Cursor),
54 State(new BitcodeParseState(this, Records)) {
55 SetListener(&State->AbbrevListener);
56 }
57
58 virtual ~BitcodeParser() override {
jvoung (off chromium) 2015/05/05 00:27:23 Only need one "override" and don't need "virtual"
Karl 2015/05/05 22:38:03 Done.
59 if (EnclosingParser == nullptr)
60 delete State;
61 }
62
63 bool ParseBlock(unsigned BlockID) override {
64 BitcodeParser NestedParser(BlockID, this);
65 return NestedParser.ParseThisBlock();
66 }
67
68 void EnterBlock(unsigned NumWords) override {
69 NaClRecordVector Values;
70 Values.push_back(GetBlockID());
71 Values.push_back(Record.GetCursor().getAbbrevIDWidth());
72 std::unique_ptr<NaClBitcodeAbbrevRecord> AbbrevRec(
73 new NaClBitcodeAbbrevRecord(naclbitc::ENTER_SUBBLOCK,
74 naclbitc::BLK_CODE_ENTER,
75 Values));
76 State->Records.push_back(std::move(AbbrevRec));
77 }
78
79 void ExitBlock() override {
80 NaClRecordVector Values;
81 std::unique_ptr<NaClBitcodeAbbrevRecord> AbbrevRec(
82 new NaClBitcodeAbbrevRecord(naclbitc::END_BLOCK,
83 naclbitc::BLK_CODE_EXIT,
84 Values));
85 State->Records.push_back(std::move(AbbrevRec));
86 }
87
88 void ProcessRecord() override {
89 std::unique_ptr<NaClBitcodeAbbrevRecord> AbbrevRec(
90 new NaClBitcodeAbbrevRecord(Record.GetAbbreviationIndex(),
91 Record.GetCode(),
92 Record.GetValues()));
93 State->Records.push_back(std::move(AbbrevRec));
94 }
95
96 void SetBID() override {
97 ProcessRecord();
98 }
99
100 void ProcessAbbreviation(unsigned BlockID,
101 NaClBitCodeAbbrev *Abbrev,
102 bool IsLocal) override {
103 ProcessRecord();
104 }
105
106 private:
107 // \brief Nested constructor for blocks within the bitcode buffer.
108 //
109 // \param BlockID The identifying constant associated with the block.
110 // \param EnclosingParser The bitcode parser parsing the enclosing block.
111 BitcodeParser(unsigned BlockID, BitcodeParser *EnclosingParser)
112 : NaClBitcodeParser(BlockID, EnclosingParser),
113 State(EnclosingParser->State) {}
114
115 // The state of the bitcode parser.
116 BitcodeParseState* State;
117 };
118
119 BitcodeParseState::BitcodeParseState(BitcodeParser *Parser,
120 NaClBitcodeRecordList &Records)
121 : Records(Records), AbbrevListener(Parser) {}
122
123 } // end of anonymous namespace
124
125 void llvm::readNaClBitcodeRecordList(
126 NaClBitcodeRecordList &RecordList,
127 std::unique_ptr<MemoryBuffer> InputBuffer) {
128 if (InputBuffer->getBufferSize() % 4 != 0)
129 report_fatal_error("Bitcode stream must be a multiple of 4 bytes in length") ;
jvoung (off chromium) 2015/05/05 00:27:23 just a little too long for 80 cols
Karl 2015/05/05 22:38:03 Done.
130
131 const unsigned char *BufPtr =
132 (const unsigned char *) InputBuffer->getBufferStart();
133 const unsigned char *EndBufPtr = BufPtr + InputBuffer->getBufferSize();
134 const unsigned char *HeaderPtr = BufPtr;
135
136 // Read header and verify it is good.
137 NaClBitcodeHeader Header;
138 if (Header.Read(HeaderPtr, EndBufPtr) || !Header.IsSupported())
139 report_fatal_error("Invalid PNaCl bitcode header.\n");
140
141 NaClBitstreamReader Reader(BufPtr, EndBufPtr, Header.getHeaderSize());
142 NaClBitstreamCursor Cursor(Reader);
143
144 // Parse the bitcode buffer.
145 BitcodeParser Parser(Cursor, RecordList);
146
147 while (!Parser.AtEndOfStream()) {
jvoung (off chromium) 2015/05/05 00:27:22 There are a couple of instances of code that are v
Karl 2015/05/05 22:38:03 The problem I discovered was that this pattern was
148 if (Parser.Parse())
149 report_fatal_error("Malformed records founds, unable to continue");
150 }
151 }
152
153
154 NaClMungedBitcode::NaClMungedBitcode(std::unique_ptr<MemoryBuffer> InputBuffer)
155 : BaseRecords(new NaClBitcodeRecordList()) {
156 readNaClBitcodeRecordList(*BaseRecords, std::move(InputBuffer));
157 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698