1 //===- ReaderWrappers.cpp - Parse bytecode from file or buffer -----------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements loading and parsing a bytecode file and parsing a
11 // bytecode module from a given buffer.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Bytecode/Analyzer.h"
16 #include "llvm/Bytecode/Reader.h"
18 #include "llvm/Module.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/System/MappedFile.h"
22 #include "llvm/System/Program.h"
29 //===----------------------------------------------------------------------===//
30 // BytecodeFileReader - Read from an mmap'able file descriptor.
34 /// BytecodeFileReader - parses a bytecode file from a file
36 class BytecodeFileReader : public BytecodeReader {
39 sys::MappedFile mapFile;
41 BytecodeFileReader(const BytecodeFileReader&); // Do not implement
42 void operator=(const BytecodeFileReader &BFR); // Do not implement
45 BytecodeFileReader(const std::string &Filename, llvm::BytecodeHandler* H=0);
46 bool read(std::string* ErrMsg);
49 BytecodeReader::freeState();
55 BytecodeFileReader::BytecodeFileReader(const std::string &Filename,
56 llvm::BytecodeHandler* H)
57 : BytecodeReader(H), fileName(Filename) {
60 bool BytecodeFileReader::read(std::string* ErrMsg) {
61 if (mapFile.open(sys::Path(fileName), sys::MappedFile::READ_ACCESS, ErrMsg))
63 if (!mapFile.map(ErrMsg)) {
67 unsigned char* buffer = reinterpret_cast<unsigned char*>(mapFile.base());
68 return ParseBytecode(buffer, mapFile.size(), fileName, ErrMsg);
71 //===----------------------------------------------------------------------===//
72 // BytecodeBufferReader - Read from a memory buffer
76 /// BytecodeBufferReader - parses a bytecode file from a buffer
78 class BytecodeBufferReader : public BytecodeReader {
80 const unsigned char *Buffer;
81 const unsigned char *Buf;
86 BytecodeBufferReader(const BytecodeBufferReader&); // Do not implement
87 void operator=(const BytecodeBufferReader &BFR); // Do not implement
90 BytecodeBufferReader(const unsigned char *Buf, unsigned Length,
91 const std::string &ModuleID,
92 llvm::BytecodeHandler* Handler = 0);
93 ~BytecodeBufferReader();
95 bool read(std::string* ErrMsg);
100 BytecodeBufferReader::BytecodeBufferReader(const unsigned char *buf,
102 const std::string &modID,
103 llvm::BytecodeHandler *H)
104 : BytecodeReader(H), Buffer(0), Buf(buf), Length(len), ModuleID(modID)
105 , MustDelete(false) {
108 BytecodeBufferReader::~BytecodeBufferReader() {
109 if (MustDelete) delete [] Buffer;
113 BytecodeBufferReader::read(std::string* ErrMsg) {
114 // If not aligned, allocate a new buffer to hold the bytecode...
115 const unsigned char *ParseBegin = 0;
116 if (reinterpret_cast<uint64_t>(Buf) & 3) {
117 Buffer = new unsigned char[Length+4];
118 unsigned Offset = 4 - ((intptr_t)Buffer & 3); // Make sure it's aligned
119 ParseBegin = Buffer + Offset;
120 memcpy((unsigned char*)ParseBegin, Buf, Length); // Copy it over
123 // If we don't need to copy it over, just use the caller's copy
124 ParseBegin = Buffer = Buf;
127 if (ParseBytecode(ParseBegin, Length, ModuleID, ErrMsg)) {
128 if (MustDelete) delete [] Buffer;
134 //===----------------------------------------------------------------------===//
135 // BytecodeStdinReader - Read bytecode from Standard Input
139 /// BytecodeStdinReader - parses a bytecode file from stdin
141 class BytecodeStdinReader : public BytecodeReader {
143 std::vector<unsigned char> FileData;
144 unsigned char *FileBuf;
146 BytecodeStdinReader(const BytecodeStdinReader&); // Do not implement
147 void operator=(const BytecodeStdinReader &BFR); // Do not implement
150 BytecodeStdinReader( llvm::BytecodeHandler* H = 0 );
151 bool read(std::string* ErrMsg);
155 BytecodeStdinReader::BytecodeStdinReader( BytecodeHandler* H )
161 BytecodeStdinReader::read(std::string* ErrMsg)
163 sys::Program::ChangeStdinToBinary();
166 // Read in all of the data from stdin, we cannot mmap stdin...
167 while (std::cin.good()) {
168 std::cin.read(Buffer, 4096*4);
169 int BlockSize = std::cin.gcount();
172 FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
175 if (FileData.empty()) {
177 *ErrMsg = "Standard Input is empty!";
181 FileBuf = &FileData[0];
182 if (ParseBytecode(FileBuf, FileData.size(), "<stdin>", ErrMsg))
187 //===----------------------------------------------------------------------===//
188 // Varargs transmogrification code...
191 // CheckVarargs - This is used to automatically translate old-style varargs to
192 // new style varargs for backwards compatibility.
193 static ModuleProvider* CheckVarargs(ModuleProvider* MP) {
194 Module* M = MP->getModule();
196 // check to see if va_start takes arguements...
197 Function* F = M->getNamedFunction("llvm.va_start");
198 if(F == 0) return MP; //No varargs use, just return.
200 if (F->getFunctionType()->getNumParams() == 1)
201 return MP; // Modern varargs processing, just return.
203 // If we get to this point, we know that we have an old-style module.
204 // Materialize the whole thing to perform the rewriting.
205 if (MP->materializeModule() == 0)
208 if(Function* F = M->getNamedFunction("llvm.va_start")) {
209 assert(F->arg_size() == 0 && "Obsolete va_start takes 0 argument!");
213 //bar = alloca typeof(foo)
217 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
218 const Type* ArgTy = F->getFunctionType()->getReturnType();
219 const Type* ArgTyPtr = PointerType::get(ArgTy);
220 Function* NF = M->getOrInsertFunction("llvm.va_start",
221 RetTy, ArgTyPtr, (Type *)0);
223 for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
224 if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
225 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
226 new CallInst(NF, bar, "", CI);
227 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
228 CI->replaceAllUsesWith(foo);
229 CI->getParent()->getInstList().erase(CI);
234 if(Function* F = M->getNamedFunction("llvm.va_end")) {
235 assert(F->arg_size() == 1 && "Obsolete va_end takes 1 argument!");
238 //bar = alloca 1 of typeof(foo)
240 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
241 const Type* ArgTy = F->getFunctionType()->getParamType(0);
242 const Type* ArgTyPtr = PointerType::get(ArgTy);
243 Function* NF = M->getOrInsertFunction("llvm.va_end",
244 RetTy, ArgTyPtr, (Type *)0);
246 for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
247 if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
248 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
249 new StoreInst(CI->getOperand(1), bar, CI);
250 new CallInst(NF, bar, "", CI);
251 CI->getParent()->getInstList().erase(CI);
256 if(Function* F = M->getNamedFunction("llvm.va_copy")) {
257 assert(F->arg_size() == 1 && "Obsolete va_copy takes 1 argument!");
260 //a = alloca 1 of typeof(foo)
261 //b = alloca 1 of typeof(foo)
266 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
267 const Type* ArgTy = F->getFunctionType()->getReturnType();
268 const Type* ArgTyPtr = PointerType::get(ArgTy);
269 Function* NF = M->getOrInsertFunction("llvm.va_copy",
270 RetTy, ArgTyPtr, ArgTyPtr, (Type *)0);
272 for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
273 if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
274 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
275 AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
276 new StoreInst(CI->getOperand(1), b, CI);
277 new CallInst(NF, a, b, "", CI);
278 Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
279 CI->replaceAllUsesWith(foo);
280 CI->getParent()->getInstList().erase(CI);
287 //===----------------------------------------------------------------------===//
289 //===----------------------------------------------------------------------===//
291 /// getBytecodeBufferModuleProvider - lazy function-at-a-time loading from a
294 llvm::getBytecodeBufferModuleProvider(const unsigned char *Buffer,
296 const std::string &ModuleID,
298 BytecodeHandler *H) {
299 BytecodeBufferReader* rdr =
300 new BytecodeBufferReader(Buffer, Length, ModuleID, H);
301 if (rdr->read(ErrMsg))
303 return CheckVarargs(rdr);
306 /// ParseBytecodeBuffer - Parse a given bytecode buffer
308 Module *llvm::ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
309 const std::string &ModuleID,
310 std::string *ErrMsg) {
312 getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, ErrMsg, 0);
314 Module *M = MP->releaseModule();
319 /// getBytecodeModuleProvider - lazy function-at-a-time loading from a file
322 llvm::getBytecodeModuleProvider(const std::string &Filename,
324 BytecodeHandler* H) {
326 if (Filename != std::string("-")) {
327 BytecodeFileReader* rdr = new BytecodeFileReader(Filename, H);
328 if (rdr->read(ErrMsg))
330 return CheckVarargs(rdr);
334 BytecodeStdinReader* rdr = new BytecodeStdinReader(H);
335 if (rdr->read(ErrMsg))
337 return CheckVarargs(rdr);
340 /// ParseBytecodeFile - Parse the given bytecode file
342 Module *llvm::ParseBytecodeFile(const std::string &Filename,
343 std::string *ErrMsg) {
344 ModuleProvider* MP = getBytecodeModuleProvider(Filename, ErrMsg);
346 Module *M = MP->releaseModule();
351 // AnalyzeBytecodeFile - analyze one file
352 Module* llvm::AnalyzeBytecodeFile(
353 const std::string &Filename, ///< File to analyze
354 BytecodeAnalysis& bca, ///< Statistical output
355 std::string *ErrMsg, ///< Error output
356 std::ostream* output ///< Dump output
358 BytecodeHandler* AH = createBytecodeAnalyzerHandler(bca,output);
359 ModuleProvider* MP = getBytecodeModuleProvider(Filename, ErrMsg, AH);
361 Module *M = MP->releaseModule();
366 // AnalyzeBytecodeBuffer - analyze a buffer
367 Module* llvm::AnalyzeBytecodeBuffer(
368 const unsigned char* Buffer, ///< Pointer to start of bytecode buffer
369 unsigned Length, ///< Size of the bytecode buffer
370 const std::string& ModuleID, ///< Identifier for the module
371 BytecodeAnalysis& bca, ///< The results of the analysis
372 std::string* ErrMsg, ///< Errors, if any.
373 std::ostream* output ///< Dump output, if any
376 BytecodeHandler* hdlr = createBytecodeAnalyzerHandler(bca, output);
378 getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, ErrMsg, hdlr);
380 Module *M = MP->releaseModule();
385 bool llvm::GetBytecodeDependentLibraries(const std::string &fname,
386 Module::LibraryListType& deplibs,
387 std::string* ErrMsg) {
388 ModuleProvider* MP = getBytecodeModuleProvider(fname, ErrMsg);
393 Module* M = MP->releaseModule();
394 deplibs = M->getLibraries();
400 static void getSymbols(Module*M, std::vector<std::string>& symbols) {
401 // Loop over global variables
402 for (Module::global_iterator GI = M->global_begin(), GE=M->global_end(); GI != GE; ++GI)
403 if (!GI->isExternal() && !GI->hasInternalLinkage())
404 if (!GI->getName().empty())
405 symbols.push_back(GI->getName());
407 // Loop over functions.
408 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
409 if (!FI->isExternal() && !FI->hasInternalLinkage())
410 if (!FI->getName().empty())
411 symbols.push_back(FI->getName());
414 // Get just the externally visible defined symbols from the bytecode
415 bool llvm::GetBytecodeSymbols(const sys::Path& fName,
416 std::vector<std::string>& symbols,
417 std::string* ErrMsg) {
418 ModuleProvider *MP = getBytecodeModuleProvider(fName.toString(), ErrMsg);
422 // Get the module from the provider
423 Module* M = MP->materializeModule();
430 getSymbols(M, symbols);
432 // Done with the module.
438 llvm::GetBytecodeSymbols(const unsigned char*Buffer, unsigned Length,
439 const std::string& ModuleID,
440 std::vector<std::string>& symbols,
441 std::string* ErrMsg) {
442 // Get the module provider
444 getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, ErrMsg, 0);
448 // Get the module from the provider
449 Module* M = MP->materializeModule();
456 getSymbols(M, symbols);
458 // Done with the module. Note that ModuleProvider will delete the
459 // Module when it is deleted. Also note that its the caller's responsibility
460 // to delete the ModuleProvider.