1 //===- Reader.cpp - Code to read bytecode files ---------------------------===//
3 // This library implements the functionality defined in llvm/Bytecode/Reader.h
5 // Note that this library should be as fast as possible, reentrant, and
8 // TODO: Return error messages to caller instead of printing them out directly.
9 // TODO: Allow passing in an option to ignore the symbol table
11 //===----------------------------------------------------------------------===//
13 #include "ReaderInternals.h"
14 #include "Config/sys/mman.h"
15 #include "llvm/Bytecode/Reader.h"
16 #include "llvm/Bytecode/Format.h"
17 #include "llvm/Module.h"
18 #include "llvm/Constants.h"
19 #include "llvm/iPHINode.h"
20 #include "llvm/iOther.h"
21 #include "Config/sys/types.h"
22 #include "Config/sys/stat.h"
23 #include "Config/fcntl.h"
24 #include "Config/unistd.h"
27 bool BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
28 if (Ty->isPrimitiveType()) {
29 Slot = Ty->getPrimitiveID();
31 // Check the function level types first...
32 TypeValuesListTy::iterator I = find(FunctionTypeValues.begin(),
33 FunctionTypeValues.end(), Ty);
34 if (I != FunctionTypeValues.end()) {
35 Slot = FirstDerivedTyID+ModuleTypeValues.size()+
36 (&*I - &FunctionTypeValues[0]);
38 I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
39 if (I == ModuleTypeValues.end()) return true; // Didn't find type!
40 Slot = FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
43 //cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << "\n";
47 const Type *BytecodeParser::getType(unsigned ID) {
48 if (ID < Type::NumPrimitiveIDs) {
49 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
53 //cerr << "Looking up Type ID: " << ID << "\n";
54 const Value *V = getValue(Type::TypeTy, ID, false);
55 return cast_or_null<Type>(V);
58 int BytecodeParser::insertValue(Value *Val, ValueTable &ValueTab) {
59 assert((!HasImplicitZeroInitializer || !isa<Constant>(Val) ||
60 Val->getType()->isPrimitiveType() ||
61 !cast<Constant>(Val)->isNullValue()) &&
62 "Cannot read null values from bytecode!");
64 if (getTypeSlot(Val->getType(), type)) return -1;
65 assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
67 if (ValueTab.size() <= type) {
68 unsigned OldSize = ValueTab.size();
69 ValueTab.resize(type+1);
70 while (OldSize != type+1)
71 ValueTab[OldSize++] = new ValueList();
74 //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size()
75 // << "] = " << Val << "\n";
76 ValueTab[type]->push_back(Val);
78 bool HasOffset = HasImplicitZeroInitializer &&
79 !Val->getType()->isPrimitiveType();
81 return ValueTab[type]->size()-1 + HasOffset;
85 void BytecodeParser::setValueTo(ValueTable &ValueTab, unsigned Slot,
87 assert(&ValueTab == &ModuleValues && "Can only setValueTo on Module values!");
89 if (getTypeSlot(Val->getType(), type))
90 assert(0 && "getTypeSlot failed!");
92 assert((!HasImplicitZeroInitializer || Slot != 0) &&
93 "Cannot change zero init");
94 assert(type < ValueTab.size() && Slot <= ValueTab[type]->size());
95 ValueTab[type]->setOperand(Slot-HasImplicitZeroInitializer, Val);
98 Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
100 unsigned type; // The type plane it lives in...
102 if (getTypeSlot(Ty, type)) return 0;
104 if (type == Type::TypeTyID) { // The 'type' plane has implicit values
105 assert(Create == false);
106 if (Num < Type::NumPrimitiveIDs) {
107 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num);
108 if (T) return (Value*)T; // Asked for a primitive type...
111 // Otherwise, derived types need offset...
112 Num -= FirstDerivedTyID;
114 // Is it a module level type?
115 if (Num < ModuleTypeValues.size())
116 return (Value*)ModuleTypeValues[Num].get();
118 // Nope, is it a function level type?
119 Num -= ModuleTypeValues.size();
120 if (Num < FunctionTypeValues.size())
121 return (Value*)FunctionTypeValues[Num].get();
126 if (HasImplicitZeroInitializer && type >= FirstDerivedTyID) {
128 return Constant::getNullValue(Ty);
132 if (type < ModuleValues.size()) {
133 if (Num < ModuleValues[type]->size())
134 return ModuleValues[type]->getOperand(Num);
135 Num -= ModuleValues[type]->size();
138 if (Values.size() > type && Values[type]->size() > Num)
139 return Values[type]->getOperand(Num);
141 if (!Create) return 0; // Do not create a placeholder?
144 switch (Ty->getPrimitiveID()) {
145 case Type::LabelTyID:
146 d = new BBPHolder(Ty, oNum);
149 d = new ValPHolder(Ty, oNum);
153 assert(d != 0 && "How did we not make something?");
154 if (insertValue(d, LateResolveValues) == -1) return 0;
158 /// getConstantValue - Just like getValue, except that it returns a null pointer
159 /// only on error. It always returns a constant (meaning that if the value is
160 /// defined, but is not a constant, that is an error). If the specified
161 /// constant hasn't been parsed yet, a placeholder is defined and used. Later,
162 /// after the real value is parsed, the placeholder is eliminated.
164 Constant *BytecodeParser::getConstantValue(const Type *Ty, unsigned Slot) {
165 if (Value *V = getValue(Ty, Slot, false))
166 return dyn_cast<Constant>(V); // If we already have the value parsed...
168 std::pair<const Type*, unsigned> Key(Ty, Slot);
169 GlobalRefsType::iterator I = GlobalRefs.lower_bound(Key);
171 if (I != GlobalRefs.end() && I->first == Key) {
172 BCR_TRACE(5, "Previous forward ref found!\n");
173 return cast<Constant>(I->second);
175 // Create a placeholder for the constant reference and
176 // keep track of the fact that we have a forward ref to recycle it
177 BCR_TRACE(5, "Creating new forward ref to a constant!\n");
178 Constant *C = new ConstPHolder(Ty, Slot);
180 // Keep track of the fact that we have a forward ref to recycle it
181 GlobalRefs.insert(I, std::make_pair(Key, C));
187 bool BytecodeParser::postResolveValues(ValueTable &ValTab) {
189 while (!ValTab.empty()) {
190 ValueList &DL = *ValTab.back();
193 while (!DL.empty()) {
194 Value *D = DL.back();
195 unsigned IDNumber = getValueIDNumberFromPlaceHolder(D);
198 Value *NewDef = getValue(D->getType(), IDNumber, false);
200 Error = true; // Unresolved thinger
201 std::cerr << "Unresolvable reference found: <"
202 << *D->getType() << ">:" << IDNumber <<"!\n";
204 // Fixup all of the uses of this placeholder def...
205 D->replaceAllUsesWith(NewDef);
207 // Now that all the uses are gone, delete the placeholder...
208 // If we couldn't find a def (error case), then leak a little
209 delete D; // memory, 'cause otherwise we can't remove all uses!
218 bool BytecodeParser::ParseBasicBlock(const unsigned char *&Buf,
219 const unsigned char *EndBuf,
221 BB = new BasicBlock();
223 while (Buf < EndBuf) {
225 if (ParseInstruction(Buf, EndBuf, Inst)) {
230 if (Inst == 0) { delete BB; return true; }
231 if (insertValue(Inst, Values) == -1) { delete BB; return true; }
233 BB->getInstList().push_back(Inst);
241 bool BytecodeParser::ParseSymbolTable(const unsigned char *&Buf,
242 const unsigned char *EndBuf,
244 while (Buf < EndBuf) {
245 // Symtab block header: [num entries][type id number]
246 unsigned NumEntries, Typ;
247 if (read_vbr(Buf, EndBuf, NumEntries) ||
248 read_vbr(Buf, EndBuf, Typ)) return true;
249 const Type *Ty = getType(Typ);
250 if (Ty == 0) return true;
252 BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
255 for (unsigned i = 0; i < NumEntries; ++i) {
256 // Symtab entry: [def slot #][name]
258 if (read_vbr(Buf, EndBuf, slot)) return true;
260 if (read(Buf, EndBuf, Name, false)) // Not aligned...
263 Value *V = getValue(Ty, slot, false); // Find mapping...
265 BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << "\n");
268 BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << *V;
269 if (!isa<Instruction>(V)) std::cerr << "\n");
271 V->setName(Name, ST);
275 if (Buf > EndBuf) return true;
279 void BytecodeParser::ResolveReferencesToValue(Value *NewV, unsigned Slot) {
280 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(NewV->getType(),
282 if (I == GlobalRefs.end()) return; // Never forward referenced?
284 BCR_TRACE(3, "Mutating forward refs!\n");
285 Value *VPH = I->second; // Get the placeholder...
287 VPH->replaceAllUsesWith(NewV);
289 // If this is a global variable being resolved, remove the placeholder from
291 if (GlobalValue* GVal = dyn_cast<GlobalValue>(NewV))
292 GVal->getParent()->getGlobalList().remove(cast<GlobalVariable>(VPH));
294 delete VPH; // Delete the old placeholder
295 GlobalRefs.erase(I); // Remove the map entry for it
299 bool BytecodeParser::ParseFunction(const unsigned char *&Buf,
300 const unsigned char *EndBuf) {
301 // Clear out the local values table...
302 if (FunctionSignatureList.empty()) {
303 Error = "Function found, but FunctionSignatureList empty!";
304 return true; // Unexpected function!
307 GlobalValue::LinkageTypes Linkage = GlobalValue::ExternalLinkage;
309 if (!hasInternalMarkerOnly) {
310 unsigned LinkageType;
311 if (read_vbr(Buf, EndBuf, LinkageType)) return true;
312 if (LinkageType & ~0x3) return true;
313 Linkage = (GlobalValue::LinkageTypes)LinkageType;
315 // We used to only support two linkage models: internal and external
317 if (read_vbr(Buf, EndBuf, isInternal)) return true;
318 if (isInternal) Linkage = GlobalValue::InternalLinkage;
321 Function *F = FunctionSignatureList.back().first;
322 unsigned FunctionSlot = FunctionSignatureList.back().second;
323 FunctionSignatureList.pop_back();
324 F->setLinkage(Linkage);
326 const FunctionType::ParamTypes &Params =F->getFunctionType()->getParamTypes();
327 Function::aiterator AI = F->abegin();
328 for (FunctionType::ParamTypes::const_iterator It = Params.begin();
329 It != Params.end(); ++It, ++AI) {
330 if (insertValue(AI, Values) == -1) {
331 Error = "Error reading function arguments!\n";
336 while (Buf < EndBuf) {
338 const unsigned char *OldBuf = Buf;
339 if (readBlock(Buf, EndBuf, Type, Size)) {
340 Error = "Error reading Function level block!";
345 case BytecodeFormat::ConstantPool:
346 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
347 if (ParseConstantPool(Buf, Buf+Size, Values, FunctionTypeValues))
351 case BytecodeFormat::BasicBlock: {
352 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
354 if (ParseBasicBlock(Buf, Buf+Size, BB) ||
355 insertValue(BB, Values) == -1)
356 return true; // Parse error... :(
358 F->getBasicBlockList().push_back(BB);
362 case BytecodeFormat::SymbolTable:
363 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
364 if (ParseSymbolTable(Buf, Buf+Size, &F->getSymbolTable()))
369 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
371 if (OldBuf > Buf) return true; // Wrap around!
374 BCR_TRACE(2, "} end block\n");
376 if (align32(Buf, EndBuf)) {
377 Error = "Error aligning Function level block!";
378 return true; // Malformed bc file, read past end of block.
382 if (postResolveValues(LateResolveValues)) {
383 Error = "Error resolving function values!";
384 return true; // Unresolvable references!
387 ResolveReferencesToValue(F, FunctionSlot);
389 // Clear out function level types...
390 FunctionTypeValues.clear();
396 bool BytecodeParser::ParseModuleGlobalInfo(const unsigned char *&Buf,
397 const unsigned char *End){
398 if (!FunctionSignatureList.empty()) {
399 Error = "Two ModuleGlobalInfo packets found!";
400 return true; // Two ModuleGlobal blocks?
403 // Read global variables...
405 if (read_vbr(Buf, End, VarType)) return true;
406 while (VarType != Type::VoidTyID) { // List is terminated by Void
408 GlobalValue::LinkageTypes Linkage;
410 if (!hasInternalMarkerOnly) {
411 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
412 // bit2,3 = Linkage, bit4+ = slot#
413 SlotNo = VarType >> 4;
414 Linkage = (GlobalValue::LinkageTypes)((VarType >> 2) & 3);
416 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
417 // bit2 = isInternal, bit3+ = slot#
418 SlotNo = VarType >> 3;
419 Linkage = (VarType & 4) ? GlobalValue::InternalLinkage :
420 GlobalValue::ExternalLinkage;
423 const Type *Ty = getType(SlotNo);
424 if (!Ty || !isa<PointerType>(Ty)) {
425 Error = "Global not pointer type! Ty = " + Ty->getDescription();
429 const Type *ElTy = cast<PointerType>(Ty)->getElementType();
431 // Create the global variable...
432 GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, Linkage,
434 int DestSlot = insertValue(GV, ModuleValues);
435 if (DestSlot == -1) return true;
436 BCR_TRACE(2, "Global Variable of type: " << *Ty << "\n");
437 ResolveReferencesToValue(GV, (unsigned)DestSlot);
439 if (VarType & 2) { // Does it have an initializer?
441 if (read_vbr(Buf, End, InitSlot)) return true;
442 GlobalInits.push_back(std::make_pair(GV, InitSlot));
444 if (read_vbr(Buf, End, VarType)) return true;
447 // Read the function objects for all of the functions that are coming
448 unsigned FnSignature;
449 if (read_vbr(Buf, End, FnSignature)) return true;
450 while (FnSignature != Type::VoidTyID) { // List is terminated by Void
451 const Type *Ty = getType(FnSignature);
452 if (!Ty || !isa<PointerType>(Ty) ||
453 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) {
454 Error = "Function not ptr to func type! Ty = " + Ty->getDescription();
458 // We create functions by passing the underlying FunctionType to create...
459 Ty = cast<PointerType>(Ty)->getElementType();
461 // When the ModuleGlobalInfo section is read, we load the type of each
462 // function and the 'ModuleValues' slot that it lands in. We then load a
463 // placeholder into its slot to reserve it. When the function is loaded,
464 // this placeholder is replaced.
466 // Insert the placeholder...
467 Function *Func = new Function(cast<FunctionType>(Ty),
468 GlobalValue::InternalLinkage, "", TheModule);
469 int DestSlot = insertValue(Func, ModuleValues);
470 if (DestSlot == -1) return true;
471 ResolveReferencesToValue(Func, (unsigned)DestSlot);
473 // Keep track of this information in a list that is emptied as functions are
476 FunctionSignatureList.push_back(std::make_pair(Func, DestSlot));
478 if (read_vbr(Buf, End, FnSignature)) return true;
479 BCR_TRACE(2, "Function of type: " << Ty << "\n");
482 if (align32(Buf, End)) return true;
484 // Now that the function signature list is set up, reverse it so that we can
485 // remove elements efficiently from the back of the vector.
486 std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end());
488 // This is for future proofing... in the future extra fields may be added that
489 // we don't understand, so we transparently ignore them.
495 bool BytecodeParser::ParseVersionInfo(const unsigned char *&Buf,
496 const unsigned char *EndBuf) {
498 if (read_vbr(Buf, EndBuf, Version)) return true;
500 // Unpack version number: low four bits are for flags, top bits = version
501 Module::Endianness Endianness;
502 Module::PointerSize PointerSize;
503 Endianness = (Version & 1) ? Module::BigEndian : Module::LittleEndian;
504 PointerSize = (Version & 2) ? Module::Pointer64 : Module::Pointer32;
506 bool hasNoEndianness = Version & 4;
507 bool hasNoPointerSize = Version & 8;
509 RevisionNum = Version >> 4;
511 // Default values for the current bytecode version
512 HasImplicitZeroInitializer = true;
513 hasInternalMarkerOnly = false;
514 FirstDerivedTyID = 14;
516 switch (RevisionNum) {
517 case 0: // Initial revision
518 // Version #0 didn't have any of the flags stored correctly, and in fact as
519 // only valid with a 14 in the flags values. Also, it does not support
520 // encoding zero initializers for arrays compactly.
522 if (Version != 14) return true; // Unknown revision 0 flags?
523 HasImplicitZeroInitializer = false;
524 Endianness = Module::BigEndian;
525 PointerSize = Module::Pointer64;
526 hasInternalMarkerOnly = true;
527 hasNoEndianness = hasNoPointerSize = false;
530 // Version #1 has four bit fields: isBigEndian, hasLongPointers,
531 // hasNoEndianness, and hasNoPointerSize.
532 hasInternalMarkerOnly = true;
535 // Version #2 added information about all 4 linkage types instead of just
536 // having internal and external.
539 Error = "Unknown bytecode version number!";
543 if (hasNoEndianness) Endianness = Module::AnyEndianness;
544 if (hasNoPointerSize) PointerSize = Module::AnyPointerSize;
546 TheModule->setEndianness(Endianness);
547 TheModule->setPointerSize(PointerSize);
548 BCR_TRACE(1, "Bytecode Rev = " << (unsigned)RevisionNum << "\n");
549 BCR_TRACE(1, "Endianness/PointerSize = " << Endianness << ","
550 << PointerSize << "\n");
551 BCR_TRACE(1, "HasImplicitZeroInit = " << HasImplicitZeroInitializer << "\n");
555 bool BytecodeParser::ParseModule(const unsigned char *Buf,
556 const unsigned char *EndBuf) {
558 if (readBlock(Buf, EndBuf, Type, Size)) return true;
559 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf) {
560 Error = "Expected Module packet!";
561 return true; // Hrm, not a class?
564 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
565 FunctionSignatureList.clear(); // Just in case...
567 // Read into instance variables...
568 if (ParseVersionInfo(Buf, EndBuf)) return true;
569 if (align32(Buf, EndBuf)) return true;
571 while (Buf < EndBuf) {
572 const unsigned char *OldBuf = Buf;
573 if (readBlock(Buf, EndBuf, Type, Size)) return true;
575 case BytecodeFormat::GlobalTypePlane:
576 BCR_TRACE(1, "BLOCK BytecodeFormat::GlobalTypePlane: {\n");
577 if (ParseGlobalTypes(Buf, Buf+Size)) return true;
580 case BytecodeFormat::ModuleGlobalInfo:
581 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
582 if (ParseModuleGlobalInfo(Buf, Buf+Size)) return true;
585 case BytecodeFormat::ConstantPool:
586 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
587 if (ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues))
591 case BytecodeFormat::Function: {
592 BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
593 if (ParseFunction(Buf, Buf+Size))
594 return true; // Error parsing function
598 case BytecodeFormat::SymbolTable:
599 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
600 if (ParseSymbolTable(Buf, Buf+Size, &TheModule->getSymbolTable()))
605 Error = "Expected Module Block!";
607 if (OldBuf > Buf) return true; // Wrap around!
610 BCR_TRACE(1, "} end block\n");
611 if (align32(Buf, EndBuf)) return true;
614 // After the module constant pool has been read, we can safely initialize
615 // global variables...
616 while (!GlobalInits.empty()) {
617 GlobalVariable *GV = GlobalInits.back().first;
618 unsigned Slot = GlobalInits.back().second;
619 GlobalInits.pop_back();
621 // Look up the initializer value...
622 if (Value *V = getValue(GV->getType()->getElementType(), Slot, false)) {
623 if (GV->hasInitializer()) return true;
624 GV->setInitializer(cast<Constant>(V));
629 if (!FunctionSignatureList.empty()) { // Expected more functions!
630 Error = "Function expected, but bytecode stream at end!";
634 BCR_TRACE(0, "} end block\n\n");
638 static inline Module *Error(std::string *ErrorStr, const char *Message) {
639 if (ErrorStr) *ErrorStr = Message;
643 Module *BytecodeParser::ParseBytecode(const unsigned char *Buf,
644 const unsigned char *EndBuf,
645 const std::string &ModuleID) {
647 // Read and check signature...
648 if (read(Buf, EndBuf, Sig) ||
649 Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24))
650 return ::Error(&Error, "Invalid bytecode signature!");
652 TheModule = new Module(ModuleID);
653 if (ParseModule(Buf, EndBuf)) {
654 freeState(); // Must destroy handles before deleting module!
662 Module *ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
663 const std::string &ModuleID, std::string *ErrorStr){
664 BytecodeParser Parser;
665 unsigned char *PtrToDelete = 0;
666 if ((intptr_t)Buffer & 3) { // If the buffer is not 4 byte aligned...
667 // Allocate a new buffer to hold the bytecode...
668 PtrToDelete = new unsigned char[Length+4];
669 unsigned Offset = 4-((intptr_t)PtrToDelete & 3); // Make sure it's aligned
670 memcpy(PtrToDelete+Offset, Buffer, Length); // Copy it over
671 Buffer = PtrToDelete+Offset;
674 Module *R = Parser.ParseBytecode(Buffer, Buffer+Length, ModuleID);
675 if (ErrorStr) *ErrorStr = Parser.getError();
677 delete [] PtrToDelete; // Delete alignment buffer if necessary
682 /// FDHandle - Simple handle class to make sure a file descriptor gets closed
683 /// when the object is destroyed.
687 FDHandle(int fd) : FD(fd) {}
688 operator int() const { return FD; }
690 if (FD != -1) close(FD);
694 // Parse and return a class file...
696 Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) {
699 if (Filename != std::string("-")) { // Read from a file...
700 FDHandle FD = open(Filename.c_str(), O_RDONLY);
702 return Error(ErrorStr, "Error opening file!");
704 // Stat the file to get its length...
706 if (fstat(FD, &StatBuf) == -1 || StatBuf.st_size == 0)
707 return Error(ErrorStr, "Error stat'ing file!");
709 // mmap in the file all at once...
710 int Length = StatBuf.st_size;
711 unsigned char *Buffer = (unsigned char*)mmap(0, Length, PROT_READ,
713 if (Buffer == (unsigned char*)MAP_FAILED)
714 return Error(ErrorStr, "Error mmapping file!");
716 // Parse the bytecode we mmapped in
717 Result = ParseBytecodeBuffer(Buffer, Length, Filename, ErrorStr);
719 // Unmmap the bytecode...
720 munmap((char*)Buffer, Length);
721 } else { // Read from stdin
723 unsigned char Buffer[4096*4];
724 std::vector<unsigned char> FileData;
726 // Read in all of the data from stdin, we cannot mmap stdin...
727 while ((BlockSize = read(0 /*stdin*/, Buffer, 4096*4))) {
729 return Error(ErrorStr, "Error reading from stdin!");
731 FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
734 if (FileData.empty())
735 return Error(ErrorStr, "Standard Input empty!");
740 (unsigned char*)mmap(0, FileData.size(), PROT_READ|PROT_WRITE,
741 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
742 assert((Buf != (unsigned char*)-1) && "mmap returned error!");
743 memcpy(Buf, &FileData[0], FileData.size());
745 unsigned char *Buf = &FileData[0];
748 Result = ParseBytecodeBuffer(Buf, FileData.size(), "<stdin>", ErrorStr);
751 munmap((char*)Buf, FileData.size()); // Free mmap'd data area