blob: 7c0e6d438978147c8aae4207951aafb4424c5dca [file] [log] [blame]
Chris Lattnerd6b65252001-10-24 01:15:12 +00001//===- Reader.cpp - Code to read bytecode files ---------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00002//
3// This library implements the functionality defined in llvm/Bytecode/Reader.h
4//
5// Note that this library should be as fast as possible, reentrant, and
6// threadsafe!!
7//
Chris Lattner74734132002-08-17 22:01:27 +00008// TODO: Return error messages to caller instead of printing them out directly.
Chris Lattner00950542001-06-06 20:29:01 +00009// TODO: Allow passing in an option to ignore the symbol table
10//
Chris Lattnerd6b65252001-10-24 01:15:12 +000011//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000012
Chris Lattner7061dc52001-12-03 18:02:31 +000013#include "ReaderInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +000014#include "llvm/Bytecode/Reader.h"
15#include "llvm/Bytecode/Format.h"
16#include "llvm/Module.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000017#include "llvm/Constants.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000018#include "llvm/iPHINode.h"
Chris Lattner00950542001-06-06 20:29:01 +000019#include "llvm/iOther.h"
Chris Lattner00950542001-06-06 20:29:01 +000020#include <sys/types.h>
Chris Lattner00950542001-06-06 20:29:01 +000021#include <sys/stat.h>
Chris Lattner697954c2002-01-20 22:54:45 +000022#include <sys/mman.h>
Chris Lattner00950542001-06-06 20:29:01 +000023#include <fcntl.h>
24#include <unistd.h>
25#include <algorithm>
26
27bool BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
28 if (Ty->isPrimitiveType()) {
29 Slot = Ty->getPrimitiveID();
30 } else {
Chris Lattner2a7b6ba2003-03-06 17:15:19 +000031 // Check the function level types first...
Chris Lattner6e5a0e42003-03-06 17:18:14 +000032 TypeValuesListTy::iterator I = find(FunctionTypeValues.begin(),
33 FunctionTypeValues.end(), Ty);
34 if (I != FunctionTypeValues.end()) {
Chris Lattner1d670cc2001-09-07 16:37:43 +000035 Slot = FirstDerivedTyID+ModuleTypeValues.size()+
Chris Lattner6e5a0e42003-03-06 17:18:14 +000036 (&*I - &FunctionTypeValues[0]);
Chris Lattner1d670cc2001-09-07 16:37:43 +000037 } else {
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]);
41 }
Chris Lattner00950542001-06-06 20:29:01 +000042 }
Chris Lattner697954c2002-01-20 22:54:45 +000043 //cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << "\n";
Chris Lattner00950542001-06-06 20:29:01 +000044 return false;
45}
46
47const Type *BytecodeParser::getType(unsigned ID) {
Chris Lattner8cdc6b72002-10-23 00:51:54 +000048 if (ID < Type::NumPrimitiveIDs) {
49 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
50 if (T) return T;
51 }
Chris Lattner00950542001-06-06 20:29:01 +000052
Chris Lattner697954c2002-01-20 22:54:45 +000053 //cerr << "Looking up Type ID: " << ID << "\n";
Chris Lattner8cdc6b72002-10-23 00:51:54 +000054 const Value *V = getValue(Type::TypeTy, ID, false);
55 return cast_or_null<Type>(V);
Chris Lattner00950542001-06-06 20:29:01 +000056}
57
Chris Lattner52e20b02003-03-19 20:54:26 +000058int 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!");
Chris Lattner00950542001-06-06 20:29:01 +000063 unsigned type;
Chris Lattner74734132002-08-17 22:01:27 +000064 if (getTypeSlot(Val->getType(), type)) return -1;
Chris Lattner1d670cc2001-09-07 16:37:43 +000065 assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
Chris Lattner00950542001-06-06 20:29:01 +000066
Chris Lattner52e20b02003-03-19 20:54:26 +000067 if (ValueTab.size() <= type) {
68 unsigned OldSize = ValueTab.size();
69 ValueTab.resize(type+1);
70 while (OldSize != type+1)
71 ValueTab[OldSize++] = new ValueList();
Chris Lattner036b8aa2003-03-06 17:55:45 +000072 }
Chris Lattner00950542001-06-06 20:29:01 +000073
74 //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size()
Chris Lattner697954c2002-01-20 22:54:45 +000075 // << "] = " << Val << "\n";
Chris Lattner52e20b02003-03-19 20:54:26 +000076 ValueTab[type]->push_back(Val);
Chris Lattner00950542001-06-06 20:29:01 +000077
Chris Lattner52e20b02003-03-19 20:54:26 +000078 bool HasOffset = HasImplicitZeroInitializer &&
79 !Val->getType()->isPrimitiveType();
80
81 return ValueTab[type]->size()-1 + HasOffset;
82}
83
84
85void BytecodeParser::setValueTo(ValueTable &ValueTab, unsigned Slot,
86 Value *Val) {
87 assert(&ValueTab == &ModuleValues && "Can only setValueTo on Module values!");
88 unsigned type;
89 if (getTypeSlot(Val->getType(), type))
90 assert(0 && "getTypeSlot failed!");
91
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);
Chris Lattner00950542001-06-06 20:29:01 +000096}
97
98Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
99 unsigned Num = oNum;
100 unsigned type; // The type plane it lives in...
101
Chris Lattner74734132002-08-17 22:01:27 +0000102 if (getTypeSlot(Ty, type)) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000103
104 if (type == Type::TypeTyID) { // The 'type' plane has implicit values
Chris Lattner1d670cc2001-09-07 16:37:43 +0000105 assert(Create == false);
Chris Lattner8cdc6b72002-10-23 00:51:54 +0000106 if (Num < Type::NumPrimitiveIDs) {
107 const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num);
108 if (T) return (Value*)T; // Asked for a primitive type...
109 }
Chris Lattner00950542001-06-06 20:29:01 +0000110
111 // Otherwise, derived types need offset...
112 Num -= FirstDerivedTyID;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000113
114 // Is it a module level type?
115 if (Num < ModuleTypeValues.size())
Chris Lattner05950c32001-10-13 06:47:01 +0000116 return (Value*)ModuleTypeValues[Num].get();
Chris Lattner1d670cc2001-09-07 16:37:43 +0000117
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000118 // Nope, is it a function level type?
Chris Lattner1d670cc2001-09-07 16:37:43 +0000119 Num -= ModuleTypeValues.size();
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000120 if (Num < FunctionTypeValues.size())
121 return (Value*)FunctionTypeValues[Num].get();
Chris Lattner1d670cc2001-09-07 16:37:43 +0000122
123 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000124 }
125
Chris Lattner52e20b02003-03-19 20:54:26 +0000126 if (HasImplicitZeroInitializer && type >= FirstDerivedTyID) {
127 if (Num == 0)
128 return Constant::getNullValue(Ty);
129 --Num;
Chris Lattner00950542001-06-06 20:29:01 +0000130 }
131
Chris Lattner52e20b02003-03-19 20:54:26 +0000132 if (type < ModuleValues.size()) {
133 if (Num < ModuleValues[type]->size())
134 return ModuleValues[type]->getOperand(Num);
135 Num -= ModuleValues[type]->size();
136 }
137
138 if (Values.size() > type && Values[type]->size() > Num)
139 return Values[type]->getOperand(Num);
Chris Lattner00950542001-06-06 20:29:01 +0000140
Chris Lattner74734132002-08-17 22:01:27 +0000141 if (!Create) return 0; // Do not create a placeholder?
Chris Lattner00950542001-06-06 20:29:01 +0000142
143 Value *d = 0;
144 switch (Ty->getPrimitiveID()) {
Chris Lattner74734132002-08-17 22:01:27 +0000145 case Type::LabelTyID:
146 d = new BBPHolder(Ty, oNum);
147 break;
148 default:
149 d = new ValPHolder(Ty, oNum);
150 break;
Chris Lattner00950542001-06-06 20:29:01 +0000151 }
152
153 assert(d != 0 && "How did we not make something?");
Chris Lattner74734132002-08-17 22:01:27 +0000154 if (insertValue(d, LateResolveValues) == -1) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000155 return d;
156}
157
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000158/// 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.
163///
164Constant *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...
167
Chris Lattner52e20b02003-03-19 20:54:26 +0000168 std::pair<const Type*, unsigned> Key(Ty, Slot);
169 GlobalRefsType::iterator I = GlobalRefs.lower_bound(Key);
170
171 if (I != GlobalRefs.end() && I->first == Key) {
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000172 BCR_TRACE(5, "Previous forward ref found!\n");
173 return cast<Constant>(I->second);
174 } else {
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);
179
180 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner52e20b02003-03-19 20:54:26 +0000181 GlobalRefs.insert(I, std::make_pair(Key, C));
Chris Lattnerbbd4b302002-10-14 03:33:02 +0000182 return C;
183 }
184}
185
186
Chris Lattner00950542001-06-06 20:29:01 +0000187bool BytecodeParser::postResolveValues(ValueTable &ValTab) {
188 bool Error = false;
Chris Lattner52e20b02003-03-19 20:54:26 +0000189 while (!ValTab.empty()) {
190 ValueList &DL = *ValTab.back();
191 ValTab.pop_back();
Chris Lattner00950542001-06-06 20:29:01 +0000192
Chris Lattner52e20b02003-03-19 20:54:26 +0000193 while (!DL.empty()) {
194 Value *D = DL.back();
195 unsigned IDNumber = getValueIDNumberFromPlaceHolder(D);
Chris Lattner00950542001-06-06 20:29:01 +0000196 DL.pop_back();
197
198 Value *NewDef = getValue(D->getType(), IDNumber, false);
199 if (NewDef == 0) {
200 Error = true; // Unresolved thinger
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000201 std::cerr << "Unresolvable reference found: <"
Chris Lattner52e20b02003-03-19 20:54:26 +0000202 << *D->getType() << ">:" << IDNumber <<"!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000203 } else {
204 // Fixup all of the uses of this placeholder def...
205 D->replaceAllUsesWith(NewDef);
206
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!
210 }
211 }
Chris Lattner52e20b02003-03-19 20:54:26 +0000212 delete &DL;
Chris Lattner00950542001-06-06 20:29:01 +0000213 }
214
215 return Error;
216}
217
Chris Lattner12e64652003-05-22 18:08:30 +0000218bool BytecodeParser::ParseBasicBlock(const unsigned char *&Buf,
219 const unsigned char *EndBuf,
Chris Lattner00950542001-06-06 20:29:01 +0000220 BasicBlock *&BB) {
221 BB = new BasicBlock();
222
223 while (Buf < EndBuf) {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000224 Instruction *Inst;
Chris Lattner036b8aa2003-03-06 17:55:45 +0000225 if (ParseInstruction(Buf, EndBuf, Inst, /*HACK*/BB)) {
Chris Lattner00950542001-06-06 20:29:01 +0000226 delete BB;
Chris Lattner74734132002-08-17 22:01:27 +0000227 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000228 }
229
Chris Lattner74734132002-08-17 22:01:27 +0000230 if (Inst == 0) { delete BB; return true; }
231 if (insertValue(Inst, Values) == -1) { delete BB; return true; }
Chris Lattner00950542001-06-06 20:29:01 +0000232
Chris Lattner1d670cc2001-09-07 16:37:43 +0000233 BB->getInstList().push_back(Inst);
234
235 BCR_TRACE(4, Inst);
Chris Lattner00950542001-06-06 20:29:01 +0000236 }
237
238 return false;
239}
240
Chris Lattner12e64652003-05-22 18:08:30 +0000241bool BytecodeParser::ParseSymbolTable(const unsigned char *&Buf,
242 const unsigned char *EndBuf,
Chris Lattner1d670cc2001-09-07 16:37:43 +0000243 SymbolTable *ST) {
Chris Lattner00950542001-06-06 20:29:01 +0000244 while (Buf < EndBuf) {
245 // Symtab block header: [num entries][type id number]
246 unsigned NumEntries, Typ;
247 if (read_vbr(Buf, EndBuf, NumEntries) ||
Chris Lattner74734132002-08-17 22:01:27 +0000248 read_vbr(Buf, EndBuf, Typ)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000249 const Type *Ty = getType(Typ);
Chris Lattner74734132002-08-17 22:01:27 +0000250 if (Ty == 0) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000251
Chris Lattner1d670cc2001-09-07 16:37:43 +0000252 BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
253 " entries\n");
254
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000255 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000256 // Symtab entry: [def slot #][name]
257 unsigned slot;
Chris Lattner74734132002-08-17 22:01:27 +0000258 if (read_vbr(Buf, EndBuf, slot)) return true;
Chris Lattner697954c2002-01-20 22:54:45 +0000259 std::string Name;
Chris Lattner00950542001-06-06 20:29:01 +0000260 if (read(Buf, EndBuf, Name, false)) // Not aligned...
Chris Lattner74734132002-08-17 22:01:27 +0000261 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000262
Chris Lattner52e20b02003-03-19 20:54:26 +0000263 Value *V = getValue(Ty, slot, false); // Find mapping...
264 if (V == 0) {
Chris Lattner697954c2002-01-20 22:54:45 +0000265 BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << "\n");
Chris Lattner74734132002-08-17 22:01:27 +0000266 return true;
Chris Lattner1d670cc2001-09-07 16:37:43 +0000267 }
Chris Lattner52e20b02003-03-19 20:54:26 +0000268 BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << *V;
269 if (!isa<Instruction>(V)) std::cerr << "\n");
Chris Lattner1d670cc2001-09-07 16:37:43 +0000270
Chris Lattner52e20b02003-03-19 20:54:26 +0000271 V->setName(Name, ST);
Chris Lattner00950542001-06-06 20:29:01 +0000272 }
273 }
274
Chris Lattner74734132002-08-17 22:01:27 +0000275 if (Buf > EndBuf) return true;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000276 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000277}
278
Chris Lattner74734132002-08-17 22:01:27 +0000279void BytecodeParser::ResolveReferencesToValue(Value *NewV, unsigned Slot) {
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000280 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(NewV->getType(),
281 Slot));
Chris Lattner74734132002-08-17 22:01:27 +0000282 if (I == GlobalRefs.end()) return; // Never forward referenced?
Chris Lattner00950542001-06-06 20:29:01 +0000283
Chris Lattner74734132002-08-17 22:01:27 +0000284 BCR_TRACE(3, "Mutating forward refs!\n");
285 Value *VPH = I->second; // Get the placeholder...
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000286
Chris Lattner52e20b02003-03-19 20:54:26 +0000287 VPH->replaceAllUsesWith(NewV);
288
289 // If this is a global variable being resolved, remove the placeholder from
290 // the module...
291 if (GlobalValue* GVal = dyn_cast<GlobalValue>(NewV))
292 GVal->getParent()->getGlobalList().remove(cast<GlobalVariable>(VPH));
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000293
Chris Lattner74734132002-08-17 22:01:27 +0000294 delete VPH; // Delete the old placeholder
295 GlobalRefs.erase(I); // Remove the map entry for it
Vikram S. Advec1e4a812002-07-14 23:04:18 +0000296}
297
Chris Lattner52e20b02003-03-19 20:54:26 +0000298
Chris Lattner12e64652003-05-22 18:08:30 +0000299bool BytecodeParser::ParseFunction(const unsigned char *&Buf,
300 const unsigned char *EndBuf) {
Chris Lattner00950542001-06-06 20:29:01 +0000301 // Clear out the local values table...
Chris Lattner74734132002-08-17 22:01:27 +0000302 if (FunctionSignatureList.empty()) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000303 Error = "Function found, but FunctionSignatureList empty!";
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000304 return true; // Unexpected function!
Chris Lattnerd6b65252001-10-24 01:15:12 +0000305 }
Chris Lattner00950542001-06-06 20:29:01 +0000306
Chris Lattnere3869c82003-04-16 21:16:05 +0000307 GlobalValue::LinkageTypes Linkage = GlobalValue::ExternalLinkage;
308
309 if (!hasInternalMarkerOnly) {
310 unsigned LinkageType;
311 if (read_vbr(Buf, EndBuf, LinkageType)) return true;
Chris Lattner869de252003-04-18 04:11:56 +0000312 if (LinkageType & ~0x3) return true;
Chris Lattnere3869c82003-04-16 21:16:05 +0000313 Linkage = (GlobalValue::LinkageTypes)LinkageType;
314 } else {
315 // We used to only support two linkage models: internal and external
316 unsigned isInternal;
317 if (read_vbr(Buf, EndBuf, isInternal)) return true;
318 if (isInternal) Linkage = GlobalValue::InternalLinkage;
319 }
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000320
Chris Lattner52e20b02003-03-19 20:54:26 +0000321 Function *F = FunctionSignatureList.back().first;
322 unsigned FunctionSlot = FunctionSignatureList.back().second;
Chris Lattner74734132002-08-17 22:01:27 +0000323 FunctionSignatureList.pop_back();
Chris Lattnere3869c82003-04-16 21:16:05 +0000324 F->setLinkage(Linkage);
Chris Lattner00950542001-06-06 20:29:01 +0000325
Chris Lattner52e20b02003-03-19 20:54:26 +0000326 const FunctionType::ParamTypes &Params =F->getFunctionType()->getParamTypes();
327 Function::aiterator AI = F->abegin();
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000328 for (FunctionType::ParamTypes::const_iterator It = Params.begin();
Chris Lattner69da5cf2002-10-13 20:57:00 +0000329 It != Params.end(); ++It, ++AI) {
330 if (insertValue(AI, Values) == -1) {
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000331 Error = "Error reading function arguments!\n";
Chris Lattner52e20b02003-03-19 20:54:26 +0000332 return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000333 }
Chris Lattner00950542001-06-06 20:29:01 +0000334 }
335
336 while (Buf < EndBuf) {
337 unsigned Type, Size;
Chris Lattnerb6c46952003-03-06 17:03:28 +0000338 const unsigned char *OldBuf = Buf;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000339 if (readBlock(Buf, EndBuf, Type, Size)) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000340 Error = "Error reading Function level block!";
Chris Lattner52e20b02003-03-19 20:54:26 +0000341 return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000342 }
Chris Lattner00950542001-06-06 20:29:01 +0000343
344 switch (Type) {
345 case BytecodeFormat::ConstantPool:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000346 BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
Chris Lattner52e20b02003-03-19 20:54:26 +0000347 if (ParseConstantPool(Buf, Buf+Size, Values, FunctionTypeValues))
348 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000349 break;
350
351 case BytecodeFormat::BasicBlock: {
Chris Lattner1d670cc2001-09-07 16:37:43 +0000352 BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000353 BasicBlock *BB;
354 if (ParseBasicBlock(Buf, Buf+Size, BB) ||
Chris Lattner52e20b02003-03-19 20:54:26 +0000355 insertValue(BB, Values) == -1)
356 return true; // Parse error... :(
Chris Lattner00950542001-06-06 20:29:01 +0000357
Chris Lattner52e20b02003-03-19 20:54:26 +0000358 F->getBasicBlockList().push_back(BB);
Chris Lattner00950542001-06-06 20:29:01 +0000359 break;
360 }
361
362 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000363 BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
Chris Lattner52e20b02003-03-19 20:54:26 +0000364 if (ParseSymbolTable(Buf, Buf+Size, &F->getSymbolTable()))
365 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000366 break;
367
368 default:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000369 BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
Chris Lattner00950542001-06-06 20:29:01 +0000370 Buf += Size;
Chris Lattner74734132002-08-17 22:01:27 +0000371 if (OldBuf > Buf) return true; // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000372 break;
373 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000374 BCR_TRACE(2, "} end block\n");
375
Chris Lattner00950542001-06-06 20:29:01 +0000376 if (align32(Buf, EndBuf)) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000377 Error = "Error aligning Function level block!";
Chris Lattner52e20b02003-03-19 20:54:26 +0000378 return true; // Malformed bc file, read past end of block.
Chris Lattner00950542001-06-06 20:29:01 +0000379 }
380 }
381
Chris Lattner52e20b02003-03-19 20:54:26 +0000382 if (postResolveValues(LateResolveValues)) {
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000383 Error = "Error resolving function values!";
Chris Lattner52e20b02003-03-19 20:54:26 +0000384 return true; // Unresolvable references!
Chris Lattner00950542001-06-06 20:29:01 +0000385 }
386
Chris Lattner52e20b02003-03-19 20:54:26 +0000387 ResolveReferencesToValue(F, FunctionSlot);
Chris Lattner00950542001-06-06 20:29:01 +0000388
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000389 // Clear out function level types...
Chris Lattner6e5a0e42003-03-06 17:18:14 +0000390 FunctionTypeValues.clear();
Chris Lattnere4d71a12001-09-14 22:03:42 +0000391
Chris Lattner52e20b02003-03-19 20:54:26 +0000392 freeTable(Values);
Chris Lattner00950542001-06-06 20:29:01 +0000393 return false;
394}
395
Chris Lattner12e64652003-05-22 18:08:30 +0000396bool BytecodeParser::ParseModuleGlobalInfo(const unsigned char *&Buf,
397 const unsigned char *End){
Chris Lattner74734132002-08-17 22:01:27 +0000398 if (!FunctionSignatureList.empty()) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000399 Error = "Two ModuleGlobalInfo packets found!";
Chris Lattner74734132002-08-17 22:01:27 +0000400 return true; // Two ModuleGlobal blocks?
Chris Lattnerd6b65252001-10-24 01:15:12 +0000401 }
Chris Lattner00950542001-06-06 20:29:01 +0000402
Chris Lattner70cc3392001-09-10 07:58:01 +0000403 // Read global variables...
404 unsigned VarType;
Chris Lattner74734132002-08-17 22:01:27 +0000405 if (read_vbr(Buf, End, VarType)) return true;
Chris Lattner70cc3392001-09-10 07:58:01 +0000406 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattnere3869c82003-04-16 21:16:05 +0000407 unsigned SlotNo;
408 GlobalValue::LinkageTypes Linkage;
409
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);
415 } else {
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;
421 }
422
423 const Type *Ty = getType(SlotNo);
Chris Lattner9b625032002-05-06 16:15:30 +0000424 if (!Ty || !isa<PointerType>(Ty)) {
Chris Lattnerd6b65252001-10-24 01:15:12 +0000425 Error = "Global not pointer type! Ty = " + Ty->getDescription();
Chris Lattner74734132002-08-17 22:01:27 +0000426 return true;
Chris Lattner70cc3392001-09-10 07:58:01 +0000427 }
428
Chris Lattner52e20b02003-03-19 20:54:26 +0000429 const Type *ElTy = cast<PointerType>(Ty)->getElementType();
Chris Lattnerd70684f2001-09-18 04:01:05 +0000430
Chris Lattner70cc3392001-09-10 07:58:01 +0000431 // Create the global variable...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000432 GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, Linkage,
Chris Lattner52e20b02003-03-19 20:54:26 +0000433 0, "", TheModule);
Chris Lattner05950c32001-10-13 06:47:01 +0000434 int DestSlot = insertValue(GV, ModuleValues);
Chris Lattner74734132002-08-17 22:01:27 +0000435 if (DestSlot == -1) return true;
Chris Lattner52e20b02003-03-19 20:54:26 +0000436 BCR_TRACE(2, "Global Variable of type: " << *Ty << "\n");
Chris Lattner74734132002-08-17 22:01:27 +0000437 ResolveReferencesToValue(GV, (unsigned)DestSlot);
Chris Lattner05950c32001-10-13 06:47:01 +0000438
Chris Lattner52e20b02003-03-19 20:54:26 +0000439 if (VarType & 2) { // Does it have an initalizer?
440 unsigned InitSlot;
441 if (read_vbr(Buf, End, InitSlot)) return true;
442 GlobalInits.push_back(std::make_pair(GV, InitSlot));
443 }
Chris Lattner74734132002-08-17 22:01:27 +0000444 if (read_vbr(Buf, End, VarType)) return true;
Chris Lattner70cc3392001-09-10 07:58:01 +0000445 }
446
Chris Lattner52e20b02003-03-19 20:54:26 +0000447 // Read the function objects for all of the functions that are coming
Chris Lattner74734132002-08-17 22:01:27 +0000448 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);
Chris Lattneref9c23f2001-10-03 14:53:21 +0000452 if (!Ty || !isa<PointerType>(Ty) ||
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000453 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) {
454 Error = "Function not ptr to func type! Ty = " + Ty->getDescription();
Chris Lattner74734132002-08-17 22:01:27 +0000455 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000456 }
Chris Lattner8cdc6b72002-10-23 00:51:54 +0000457
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000458 // We create functions by passing the underlying FunctionType to create...
Chris Lattner7a176752001-12-04 00:03:30 +0000459 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattner00950542001-06-06 20:29:01 +0000460
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000461 // 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.
Chris Lattner00950542001-06-06 20:29:01 +0000465
466 // Insert the placeholder...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000467 Function *Func = new Function(cast<FunctionType>(Ty),
468 GlobalValue::InternalLinkage, "", TheModule);
Chris Lattner52e20b02003-03-19 20:54:26 +0000469 int DestSlot = insertValue(Func, ModuleValues);
470 if (DestSlot == -1) return true;
471 ResolveReferencesToValue(Func, (unsigned)DestSlot);
Chris Lattner00950542001-06-06 20:29:01 +0000472
Chris Lattner52e20b02003-03-19 20:54:26 +0000473 // Keep track of this information in a list that is emptied as functions are
474 // loaded...
Chris Lattner00950542001-06-06 20:29:01 +0000475 //
Chris Lattner52e20b02003-03-19 20:54:26 +0000476 FunctionSignatureList.push_back(std::make_pair(Func, DestSlot));
477
Chris Lattner74734132002-08-17 22:01:27 +0000478 if (read_vbr(Buf, End, FnSignature)) return true;
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000479 BCR_TRACE(2, "Function of type: " << Ty << "\n");
Chris Lattner00950542001-06-06 20:29:01 +0000480 }
481
Chris Lattner74734132002-08-17 22:01:27 +0000482 if (align32(Buf, End)) return true;
483
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());
Chris Lattner00950542001-06-06 20:29:01 +0000487
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.
490 //
491 Buf = End;
492 return false;
493}
494
Chris Lattner12e64652003-05-22 18:08:30 +0000495bool BytecodeParser::ParseVersionInfo(const unsigned char *&Buf,
496 const unsigned char *EndBuf) {
Chris Lattner036b8aa2003-03-06 17:55:45 +0000497 unsigned Version;
498 if (read_vbr(Buf, EndBuf, Version)) return true;
499
500 // Unpack version number: low four bits are for flags, top bits = version
501 isBigEndian = Version & 1;
502 hasLongPointers = Version & 2;
503 RevisionNum = Version >> 4;
Chris Lattnere3869c82003-04-16 21:16:05 +0000504
505 // Default values for the current bytecode version
Chris Lattner036b8aa2003-03-06 17:55:45 +0000506 HasImplicitZeroInitializer = true;
Chris Lattnere3869c82003-04-16 21:16:05 +0000507 hasInternalMarkerOnly = false;
508 FirstDerivedTyID = 14;
Chris Lattner036b8aa2003-03-06 17:55:45 +0000509
510 switch (RevisionNum) {
511 case 0: // Initial revision
Chris Lattner52e20b02003-03-19 20:54:26 +0000512 // Version #0 didn't have any of the flags stored correctly, and in fact as
513 // only valid with a 14 in the flags values. Also, it does not support
514 // encoding zero initializers for arrays compactly.
515 //
Chris Lattner036b8aa2003-03-06 17:55:45 +0000516 if (Version != 14) return true; // Unknown revision 0 flags?
Chris Lattner036b8aa2003-03-06 17:55:45 +0000517 HasImplicitZeroInitializer = false;
518 isBigEndian = hasLongPointers = true;
Chris Lattnere3869c82003-04-16 21:16:05 +0000519 hasInternalMarkerOnly = true;
Chris Lattner036b8aa2003-03-06 17:55:45 +0000520 break;
521 case 1:
Chris Lattner52e20b02003-03-19 20:54:26 +0000522 // Version #1 has two bit fields: isBigEndian and hasLongPointers
Chris Lattnere3869c82003-04-16 21:16:05 +0000523 hasInternalMarkerOnly = true;
524 break;
525 case 2:
526 // Version #2 added information about all 4 linkage types instead of just
527 // having internal and external.
Chris Lattner036b8aa2003-03-06 17:55:45 +0000528 break;
529 default:
530 Error = "Unknown bytecode version number!";
531 return true;
532 }
533
Chris Lattner76e38962003-04-22 18:15:10 +0000534 TheModule->setEndianness(isBigEndian ? Module::BigEndian :
535 Module::LittleEndian);
536 TheModule->setPointerSize(hasLongPointers ? Module::Pointer64 :
537 Module::Pointer32);
538
Chris Lattner036b8aa2003-03-06 17:55:45 +0000539 BCR_TRACE(1, "Bytecode Rev = " << (unsigned)RevisionNum << "\n");
540 BCR_TRACE(1, "BigEndian/LongPointers = " << isBigEndian << ","
541 << hasLongPointers << "\n");
542 BCR_TRACE(1, "HasImplicitZeroInit = " << HasImplicitZeroInitializer << "\n");
543 return false;
544}
545
Chris Lattner12e64652003-05-22 18:08:30 +0000546bool BytecodeParser::ParseModule(const unsigned char *Buf,
547 const unsigned char *EndBuf) {
Chris Lattner00950542001-06-06 20:29:01 +0000548 unsigned Type, Size;
Chris Lattner74734132002-08-17 22:01:27 +0000549 if (readBlock(Buf, EndBuf, Type, Size)) return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000550 if (Type != BytecodeFormat::Module || Buf+Size != EndBuf) {
551 Error = "Expected Module packet!";
Chris Lattner74734132002-08-17 22:01:27 +0000552 return true; // Hrm, not a class?
Chris Lattnerd6b65252001-10-24 01:15:12 +0000553 }
Chris Lattner00950542001-06-06 20:29:01 +0000554
Chris Lattner1d670cc2001-09-07 16:37:43 +0000555 BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
Chris Lattner74734132002-08-17 22:01:27 +0000556 FunctionSignatureList.clear(); // Just in case...
Chris Lattner00950542001-06-06 20:29:01 +0000557
558 // Read into instance variables...
Chris Lattner036b8aa2003-03-06 17:55:45 +0000559 if (ParseVersionInfo(Buf, EndBuf)) return true;
Chris Lattner74734132002-08-17 22:01:27 +0000560 if (align32(Buf, EndBuf)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000561
Chris Lattner00950542001-06-06 20:29:01 +0000562 while (Buf < EndBuf) {
Chris Lattnerb6c46952003-03-06 17:03:28 +0000563 const unsigned char *OldBuf = Buf;
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000564 if (readBlock(Buf, EndBuf, Type, Size)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000565 switch (Type) {
Chris Lattner52e20b02003-03-19 20:54:26 +0000566 case BytecodeFormat::GlobalTypePlane:
567 BCR_TRACE(1, "BLOCK BytecodeFormat::GlobalTypePlane: {\n");
568 if (ParseGlobalTypes(Buf, Buf+Size)) return true;
569 break;
570
571 case BytecodeFormat::ModuleGlobalInfo:
572 BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
573 if (ParseModuleGlobalInfo(Buf, Buf+Size)) return true;
574 break;
575
Chris Lattner1d670cc2001-09-07 16:37:43 +0000576 case BytecodeFormat::ConstantPool:
577 BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000578 if (ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues))
579 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000580 break;
581
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000582 case BytecodeFormat::Function: {
583 BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
Chris Lattner52e20b02003-03-19 20:54:26 +0000584 if (ParseFunction(Buf, Buf+Size))
585 return true; // Error parsing function
Chris Lattner00950542001-06-06 20:29:01 +0000586 break;
587 }
588
589 case BytecodeFormat::SymbolTable:
Chris Lattner1d670cc2001-09-07 16:37:43 +0000590 BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000591 if (ParseSymbolTable(Buf, Buf+Size, &TheModule->getSymbolTable()))
592 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000593 break;
594
595 default:
Chris Lattnerd6b65252001-10-24 01:15:12 +0000596 Error = "Expected Module Block!";
Chris Lattner00950542001-06-06 20:29:01 +0000597 Buf += Size;
Chris Lattner74734132002-08-17 22:01:27 +0000598 if (OldBuf > Buf) return true; // Wrap around!
Chris Lattner00950542001-06-06 20:29:01 +0000599 break;
600 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000601 BCR_TRACE(1, "} end block\n");
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000602 if (align32(Buf, EndBuf)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000603 }
604
Chris Lattner52e20b02003-03-19 20:54:26 +0000605 // After the module constant pool has been read, we can safely initialize
606 // global variables...
607 while (!GlobalInits.empty()) {
608 GlobalVariable *GV = GlobalInits.back().first;
609 unsigned Slot = GlobalInits.back().second;
610 GlobalInits.pop_back();
611
612 // Look up the initializer value...
613 if (Value *V = getValue(GV->getType()->getElementType(), Slot, false)) {
614 if (GV->hasInitializer()) return true;
615 GV->setInitializer(cast<Constant>(V));
616 } else
617 return true;
618 }
619
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000620 if (!FunctionSignatureList.empty()) { // Expected more functions!
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000621 Error = "Function expected, but bytecode stream at end!";
Chris Lattner74734132002-08-17 22:01:27 +0000622 return true;
Chris Lattnerd6b65252001-10-24 01:15:12 +0000623 }
Chris Lattner1d670cc2001-09-07 16:37:43 +0000624
625 BCR_TRACE(0, "} end block\n\n");
Chris Lattner00950542001-06-06 20:29:01 +0000626 return false;
627}
628
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000629static inline Module *Error(std::string *ErrorStr, const char *Message) {
630 if (ErrorStr) *ErrorStr = Message;
631 return 0;
632}
633
Chris Lattner12e64652003-05-22 18:08:30 +0000634Module *BytecodeParser::ParseBytecode(const unsigned char *Buf,
635 const unsigned char *EndBuf,
Chris Lattner75f20532003-04-22 18:02:52 +0000636 const std::string &ModuleID) {
Chris Lattner00950542001-06-06 20:29:01 +0000637 unsigned Sig;
638 // Read and check signature...
639 if (read(Buf, EndBuf, Sig) ||
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000640 Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24))
641 return ::Error(&Error, "Invalid bytecode signature!");
Chris Lattner00950542001-06-06 20:29:01 +0000642
Chris Lattner75f20532003-04-22 18:02:52 +0000643 TheModule = new Module(ModuleID);
Chris Lattner2a7b6ba2003-03-06 17:15:19 +0000644 if (ParseModule(Buf, EndBuf)) {
645 delete TheModule;
646 TheModule = 0;
647 }
648 return TheModule;
Chris Lattner00950542001-06-06 20:29:01 +0000649}
650
651
Chris Lattner09abe6a2003-03-06 16:50:32 +0000652Module *ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
Chris Lattner75f20532003-04-22 18:02:52 +0000653 const std::string &ModuleID, std::string *ErrorStr){
Chris Lattner00950542001-06-06 20:29:01 +0000654 BytecodeParser Parser;
Chris Lattnerf6099df2003-04-19 21:45:17 +0000655 unsigned char *PtrToDelete = 0;
656 if ((intptr_t)Buffer & 3) { // If the buffer is not 4 byte aligned...
657 // Allocate a new buffer to hold the bytecode...
658 PtrToDelete = new unsigned char[Length+4];
659 unsigned Offset = 4-((intptr_t)PtrToDelete & 3); // Make sure it's aligned
660 memcpy(PtrToDelete+Offset, Buffer, Length); // Copy it over
661 Buffer = PtrToDelete+Offset;
662 }
663
Chris Lattner75f20532003-04-22 18:02:52 +0000664 Module *R = Parser.ParseBytecode(Buffer, Buffer+Length, ModuleID);
Chris Lattner09abe6a2003-03-06 16:50:32 +0000665 if (ErrorStr) *ErrorStr = Parser.getError();
Chris Lattnerf6099df2003-04-19 21:45:17 +0000666
667 delete [] PtrToDelete; // Delete alignment buffer if neccesary
Chris Lattner09abe6a2003-03-06 16:50:32 +0000668 return R;
Chris Lattner00950542001-06-06 20:29:01 +0000669}
670
Chris Lattnerb6c46952003-03-06 17:03:28 +0000671
672/// FDHandle - Simple handle class to make sure a file descriptor gets closed
673/// when the object is destroyed.
674class FDHandle {
675 int FD;
676public:
677 FDHandle(int fd) : FD(fd) {}
678 operator int() const { return FD; }
679 ~FDHandle() {
680 if (FD != -1) close(FD);
681 }
682};
683
Chris Lattner00950542001-06-06 20:29:01 +0000684// Parse and return a class file...
685//
Chris Lattner697954c2002-01-20 22:54:45 +0000686Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) {
Chris Lattner00950542001-06-06 20:29:01 +0000687 Module *Result = 0;
688
Chris Lattner697954c2002-01-20 22:54:45 +0000689 if (Filename != std::string("-")) { // Read from a file...
Chris Lattnerb6c46952003-03-06 17:03:28 +0000690 FDHandle FD = open(Filename.c_str(), O_RDONLY);
691 if (FD == -1)
692 return Error(ErrorStr, "Error opening file!");
Chris Lattner00950542001-06-06 20:29:01 +0000693
Chris Lattnerb6c46952003-03-06 17:03:28 +0000694 // Stat the file to get its length...
Chris Lattner09abe6a2003-03-06 16:50:32 +0000695 struct stat StatBuf;
Chris Lattnerb6c46952003-03-06 17:03:28 +0000696 if (fstat(FD, &StatBuf) == -1 || StatBuf.st_size == 0)
697 return Error(ErrorStr, "Error stat'ing file!");
Chris Lattner09abe6a2003-03-06 16:50:32 +0000698
Chris Lattnerb6c46952003-03-06 17:03:28 +0000699 // mmap in the file all at once...
Chris Lattner09abe6a2003-03-06 16:50:32 +0000700 int Length = StatBuf.st_size;
701 unsigned char *Buffer = (unsigned char*)mmap(0, Length, PROT_READ,
702 MAP_PRIVATE, FD, 0);
Chris Lattnerb6c46952003-03-06 17:03:28 +0000703 if (Buffer == (unsigned char*)MAP_FAILED)
704 return Error(ErrorStr, "Error mmapping file!");
Chris Lattner00950542001-06-06 20:29:01 +0000705
Chris Lattnerb6c46952003-03-06 17:03:28 +0000706 // Parse the bytecode we mmapped in
Chris Lattner75f20532003-04-22 18:02:52 +0000707 Result = ParseBytecodeBuffer(Buffer, Length, Filename, ErrorStr);
Chris Lattner00950542001-06-06 20:29:01 +0000708
Chris Lattnerb6c46952003-03-06 17:03:28 +0000709 // Unmmap the bytecode...
Chris Lattner00950542001-06-06 20:29:01 +0000710 munmap((char*)Buffer, Length);
Chris Lattner00950542001-06-06 20:29:01 +0000711 } else { // Read from stdin
Chris Lattner00950542001-06-06 20:29:01 +0000712 int BlockSize;
Chris Lattner12e64652003-05-22 18:08:30 +0000713 unsigned char Buffer[4096*4];
Chris Lattnerb6c46952003-03-06 17:03:28 +0000714 std::vector<unsigned char> FileData;
Chris Lattner00950542001-06-06 20:29:01 +0000715
Chris Lattnerb6c46952003-03-06 17:03:28 +0000716 // Read in all of the data from stdin, we cannot mmap stdin...
717 while ((BlockSize = read(0 /*stdin*/, Buffer, 4096*4))) {
718 if (BlockSize == -1)
719 return Error(ErrorStr, "Error reading from stdin!");
720
721 FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
Chris Lattner00950542001-06-06 20:29:01 +0000722 }
723
Chris Lattnerb6c46952003-03-06 17:03:28 +0000724 if (FileData.empty())
725 return Error(ErrorStr, "Standard Input empty!");
Chris Lattner00950542001-06-06 20:29:01 +0000726
Chris Lattner915ce8a2002-08-18 00:38:32 +0000727#define ALIGN_PTRS 0
Chris Lattner00950542001-06-06 20:29:01 +0000728#if ALIGN_PTRS
Chris Lattner12e64652003-05-22 18:08:30 +0000729 unsigned char *Buf =
730 (unsigned char*)mmap(0, FileData.size(), PROT_READ|PROT_WRITE,
731 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
732 assert((Buf != (unsigned char*)-1) && "mmap returned error!");
Chris Lattnerb6c46952003-03-06 17:03:28 +0000733 memcpy(Buf, &FileData[0], FileData.size());
Chris Lattner00950542001-06-06 20:29:01 +0000734#else
Chris Lattnerb6c46952003-03-06 17:03:28 +0000735 unsigned char *Buf = &FileData[0];
Chris Lattner00950542001-06-06 20:29:01 +0000736#endif
737
Chris Lattner75f20532003-04-22 18:02:52 +0000738 Result = ParseBytecodeBuffer(Buf, FileData.size(), "<stdin>", ErrorStr);
Chris Lattner00950542001-06-06 20:29:01 +0000739
740#if ALIGN_PTRS
Chris Lattnerb6c46952003-03-06 17:03:28 +0000741 munmap((char*)Buf, FileData.size()); // Free mmap'd data area
Chris Lattner00950542001-06-06 20:29:01 +0000742#endif
Chris Lattner00950542001-06-06 20:29:01 +0000743 }
744
745 return Result;
746}