Remove spurious case. EXTLOAD is not one of the node opcodes.
[oota-llvm.git] / lib / Bytecode / Reader / ReaderWrappers.cpp
1 //===- ReaderWrappers.cpp - Parse bytecode from file or buffer  -----------===//
2 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements loading and parsing a bytecode file and parsing a
11 // bytecode module from a given buffer.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Bytecode/Analyzer.h"
16 #include "llvm/Bytecode/Reader.h"
17 #include "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"
23 #include <cerrno>
24 #include <iostream>
25 #include <memory>
26
27 using namespace llvm;
28
29 //===----------------------------------------------------------------------===//
30 // BytecodeFileReader - Read from an mmap'able file descriptor.
31 //
32
33 namespace {
34   /// BytecodeFileReader - parses a bytecode file from a file
35   ///
36   class BytecodeFileReader : public BytecodeReader {
37   private:
38     std::string fileName;
39     sys::MappedFile mapFile;
40
41     BytecodeFileReader(const BytecodeFileReader&); // Do not implement
42     void operator=(const BytecodeFileReader &BFR); // Do not implement
43
44   public:
45     BytecodeFileReader(const std::string &Filename, llvm::BytecodeHandler* H=0);
46     bool read(std::string* ErrMsg);
47     
48     void freeState() {
49       BytecodeReader::freeState();
50       mapFile.close();
51     }
52   };
53 }
54
55 BytecodeFileReader::BytecodeFileReader(const std::string &Filename,
56                                        llvm::BytecodeHandler* H)
57   : BytecodeReader(H), fileName(Filename) {
58 }
59
60 bool BytecodeFileReader::read(std::string* ErrMsg) {
61   if (mapFile.open(sys::Path(fileName), sys::MappedFile::READ_ACCESS, ErrMsg))
62     return true;
63   if (!mapFile.map(ErrMsg)) {
64     mapFile.close();
65     return true;
66   }
67   unsigned char* buffer = reinterpret_cast<unsigned char*>(mapFile.base());
68   return ParseBytecode(buffer, mapFile.size(), fileName, ErrMsg);
69 }
70
71 //===----------------------------------------------------------------------===//
72 // BytecodeBufferReader - Read from a memory buffer
73 //
74
75 namespace {
76   /// BytecodeBufferReader - parses a bytecode file from a buffer
77   ///
78   class BytecodeBufferReader : public BytecodeReader {
79   private:
80     const unsigned char *Buffer;
81     const unsigned char *Buf;
82     unsigned Length;
83     std::string ModuleID;
84     bool MustDelete;
85
86     BytecodeBufferReader(const BytecodeBufferReader&); // Do not implement
87     void operator=(const BytecodeBufferReader &BFR);   // Do not implement
88
89   public:
90     BytecodeBufferReader(const unsigned char *Buf, unsigned Length,
91                          const std::string &ModuleID,
92                          llvm::BytecodeHandler* Handler = 0);
93     ~BytecodeBufferReader();
94
95     bool read(std::string* ErrMsg);
96
97   };
98 }
99
100 BytecodeBufferReader::BytecodeBufferReader(const unsigned char *buf,
101                                            unsigned len,
102                                            const std::string &modID,
103                                            llvm::BytecodeHandler *H)
104   : BytecodeReader(H), Buffer(0), Buf(buf), Length(len), ModuleID(modID)
105   , MustDelete(false) {
106 }
107
108 BytecodeBufferReader::~BytecodeBufferReader() {
109   if (MustDelete) delete [] Buffer;
110 }
111
112 bool
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
121     MustDelete = true;
122   } else {
123     // If we don't need to copy it over, just use the caller's copy
124     ParseBegin = Buffer = Buf;
125     MustDelete = false;
126   }
127   if (ParseBytecode(ParseBegin, Length, ModuleID, ErrMsg)) {
128     if (MustDelete) delete [] Buffer;
129     return true;
130   }
131   return false;
132 }
133
134 //===----------------------------------------------------------------------===//
135 //  BytecodeStdinReader - Read bytecode from Standard Input
136 //
137
138 namespace {
139   /// BytecodeStdinReader - parses a bytecode file from stdin
140   ///
141   class BytecodeStdinReader : public BytecodeReader {
142   private:
143     std::vector<unsigned char> FileData;
144     unsigned char *FileBuf;
145
146     BytecodeStdinReader(const BytecodeStdinReader&); // Do not implement
147     void operator=(const BytecodeStdinReader &BFR);  // Do not implement
148
149   public:
150     BytecodeStdinReader( llvm::BytecodeHandler* H = 0 );
151     bool read(std::string* ErrMsg);
152   };
153 }
154
155 BytecodeStdinReader::BytecodeStdinReader( BytecodeHandler* H )
156   : BytecodeReader(H)
157 {
158 }
159
160 bool
161 BytecodeStdinReader::read(std::string* ErrMsg)
162 {
163   sys::Program::ChangeStdinToBinary();
164   char Buffer[4096*4];
165
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();
170     if (0 >= BlockSize)
171       break;
172     FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
173   }
174
175   if (FileData.empty()) {
176     if (ErrMsg)
177       *ErrMsg = "Standard Input is empty!";
178     return true;
179   }
180
181   FileBuf = &FileData[0];
182   if (ParseBytecode(FileBuf, FileData.size(), "<stdin>", ErrMsg))
183     return true;
184   return false;
185 }
186
187 //===----------------------------------------------------------------------===//
188 // Varargs transmogrification code...
189 //
190
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();
195
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.
199
200   if (F->getFunctionType()->getNumParams() == 1)
201     return MP; // Modern varargs processing, just return.
202
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)
206     return 0;
207
208   if(Function* F = M->getNamedFunction("llvm.va_start")) {
209     assert(F->arg_size() == 0 && "Obsolete va_start takes 0 argument!");
210
211     //foo = va_start()
212     // ->
213     //bar = alloca typeof(foo)
214     //va_start(bar)
215     //foo = load bar
216
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);
222
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);
230       }
231     F->setName("");
232   }
233
234   if(Function* F = M->getNamedFunction("llvm.va_end")) {
235     assert(F->arg_size() == 1 && "Obsolete va_end takes 1 argument!");
236     //vaend foo
237     // ->
238     //bar = alloca 1 of typeof(foo)
239     //vaend bar
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);
245
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);
252       }
253     F->setName("");
254   }
255
256   if(Function* F = M->getNamedFunction("llvm.va_copy")) {
257     assert(F->arg_size() == 1 && "Obsolete va_copy takes 1 argument!");
258     //foo = vacopy(bar)
259     // ->
260     //a = alloca 1 of typeof(foo)
261     //b = alloca 1 of typeof(foo)
262     //store bar -> b
263     //vacopy(a, b)
264     //foo = load a
265
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);
271
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);
281       }
282     F->setName("");
283   }
284   return MP;
285 }
286
287 //===----------------------------------------------------------------------===//
288 // Wrapper functions
289 //===----------------------------------------------------------------------===//
290
291 /// getBytecodeBufferModuleProvider - lazy function-at-a-time loading from a
292 /// buffer
293 ModuleProvider*
294 llvm::getBytecodeBufferModuleProvider(const unsigned char *Buffer,
295                                       unsigned Length,
296                                       const std::string &ModuleID,
297                                       std::string *ErrMsg, 
298                                       BytecodeHandler *H) {
299   BytecodeBufferReader* rdr = 
300     new BytecodeBufferReader(Buffer, Length, ModuleID, H);
301   if (rdr->read(ErrMsg))
302     return 0;
303   return CheckVarargs(rdr);
304 }
305
306 /// ParseBytecodeBuffer - Parse a given bytecode buffer
307 ///
308 Module *llvm::ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
309                                   const std::string &ModuleID,
310                                   std::string *ErrMsg) {
311   ModuleProvider *MP = 
312     getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, ErrMsg, 0);
313   if (!MP) return 0;
314   Module *M = MP->releaseModule();
315   delete MP;
316   return M;
317 }
318
319 /// getBytecodeModuleProvider - lazy function-at-a-time loading from a file
320 ///
321 ModuleProvider *
322 llvm::getBytecodeModuleProvider(const std::string &Filename,
323                                 std::string* ErrMsg,
324                                 BytecodeHandler* H) {
325   // Read from a file
326   if (Filename != std::string("-")) {
327     BytecodeFileReader* rdr = new BytecodeFileReader(Filename, H);
328     if (rdr->read(ErrMsg))
329       return 0;
330     return CheckVarargs(rdr);
331   }
332
333   // Read from stdin
334   BytecodeStdinReader* rdr = new BytecodeStdinReader(H);
335   if (rdr->read(ErrMsg))
336     return 0;
337   return CheckVarargs(rdr);
338 }
339
340 /// ParseBytecodeFile - Parse the given bytecode file
341 ///
342 Module *llvm::ParseBytecodeFile(const std::string &Filename,
343                                 std::string *ErrMsg) {
344   ModuleProvider* MP = getBytecodeModuleProvider(Filename, ErrMsg);
345   if (!MP) return 0;
346   Module *M = MP->releaseModule();
347   delete MP;
348   return M;
349 }
350
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
357 ) {
358   BytecodeHandler* AH = createBytecodeAnalyzerHandler(bca,output);
359   ModuleProvider* MP = getBytecodeModuleProvider(Filename, ErrMsg, AH);
360   if (!MP) return 0;
361   Module *M = MP->releaseModule();
362   delete MP;
363   return M;
364 }
365
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
374 )
375 {
376   BytecodeHandler* hdlr = createBytecodeAnalyzerHandler(bca, output);
377   ModuleProvider* MP = 
378     getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, ErrMsg, hdlr);
379   if (!MP) return 0;
380   Module *M = MP->releaseModule();
381   delete MP;
382   return M;
383 }
384
385 bool llvm::GetBytecodeDependentLibraries(const std::string &fname,
386                                          Module::LibraryListType& deplibs,
387                                          std::string* ErrMsg) {
388   ModuleProvider* MP = getBytecodeModuleProvider(fname, ErrMsg);
389   if (!MP) {
390     deplibs.clear();
391     return true;
392   }
393   Module* M = MP->releaseModule();
394   deplibs = M->getLibraries();
395   delete M;
396   delete MP;
397   return false;
398 }
399
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());
406
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());
412 }
413
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);
419   if (!MP)
420     return true;
421
422   // Get the module from the provider
423   Module* M = MP->materializeModule();
424   if (M == 0) {
425     delete MP;
426     return true;
427   }
428
429   // Get the symbols
430   getSymbols(M, symbols);
431
432   // Done with the module.
433   delete MP;
434   return true;
435 }
436
437 ModuleProvider*
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
443   ModuleProvider* MP = 
444     getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, ErrMsg, 0);
445   if (!MP)
446     return 0;
447
448   // Get the module from the provider
449   Module* M = MP->materializeModule();
450   if (M == 0) {
451     delete MP;
452     return 0;
453   }
454
455   // Get the symbols
456   getSymbols(M, symbols);
457
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.
461   return MP;
462 }