aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Bytecode/Reader/Reader.cpp
blob: 067b70b80625ef4341c44f1e336dc9887445cfef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
//===- Reader.cpp - Code to read bytecode files ---------------------------===//
//
// This library implements the functionality defined in llvm/Bytecode/Reader.h
//
// Note that this library should be as fast as possible, reentrant, and 
// threadsafe!!
//
// TODO: Return error messages to caller instead of printing them out directly.
// TODO: Allow passing in an option to ignore the symbol table
//
//===----------------------------------------------------------------------===//

#include "ReaderInternals.h"
#include "llvm/Bytecode/Reader.h"
#include "llvm/Bytecode/Format.h"
#include "llvm/Constants.h"
#include "llvm/iPHINode.h"
#include "llvm/iOther.h"
#include "llvm/Module.h"
#include "Support/StringExtras.h"
#include "Config/unistd.h"
#include "Config/sys/mman.h"
#include "Config/sys/stat.h"
#include "Config/sys/types.h"
#include <algorithm>
#include <memory>

static inline void ALIGN32(const unsigned char *&begin,
                           const unsigned char *end) {
  if (align32(begin, end))
    throw std::string("Alignment error in buffer: read past end of block.");
}

void
BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
  if (Ty->isPrimitiveType()) {
    Slot = Ty->getPrimitiveID();
  } else {
    // Check the function level types first...
    TypeValuesListTy::iterator I = find(FunctionTypeValues.begin(),
                                        FunctionTypeValues.end(), Ty);
    if (I != FunctionTypeValues.end()) {
      Slot = FirstDerivedTyID + ModuleTypeValues.size() +
        (&*I - &FunctionTypeValues[0]);
    } else {
      I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
      if (I == ModuleTypeValues.end())
        throw std::string("Didn't find type in ModuleTypeValues.");
      Slot = FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
    }
  }
  //cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << "\n";
}

const Type *BytecodeParser::getType(unsigned ID) {
  if (ID < Type::NumPrimitiveIDs) {
    const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
    if (T) return T;
  }
  
  //cerr << "Looking up Type ID: " << ID << "\n";
  const Value *V = getValue(Type::TypeTy, ID, false);
  return cast_or_null<Type>(V);
}

int BytecodeParser::insertValue(Value *Val, ValueTable &ValueTab) {
  assert((!HasImplicitZeroInitializer || !isa<Constant>(Val) ||
          Val->getType()->isPrimitiveType() ||
          !cast<Constant>(Val)->isNullValue()) &&
         "Cannot read null values from bytecode!");
  unsigned type;
  getTypeSlot(Val->getType(), type);
  assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
 
  if (ValueTab.size() <= type) {
    unsigned OldSize = ValueTab.size();
    ValueTab.resize(type+1);
    while (OldSize != type+1)
      ValueTab[OldSize++] = new ValueList();
  }

  //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size() 
  //   << "] = " << Val << "\n";
  ValueTab[type]->push_back(Val);

  bool HasOffset = HasImplicitZeroInitializer &&
    !Val->getType()->isPrimitiveType();

  return ValueTab[type]->size()-1 + HasOffset;
}


void BytecodeParser::setValueTo(ValueTable &ValueTab, unsigned Slot,
                                Value *Val) {
  assert(&ValueTab == &ModuleValues && "Can only setValueTo on Module values!");
  unsigned type;
  getTypeSlot(Val->getType(), type);

  assert((!HasImplicitZeroInitializer || Slot != 0) &&
         "Cannot change zero init");
  assert(type < ValueTab.size() && Slot <= ValueTab[type]->size());
  ValueTab[type]->setOperand(Slot-HasImplicitZeroInitializer, Val);
}

Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
  unsigned Num = oNum;
  unsigned type;   // The type plane it lives in...
  getTypeSlot(Ty, type);

  if (type == Type::TypeTyID) {   // The 'type' plane has implicit values
    assert(Create == false);
    if (Num < Type::NumPrimitiveIDs) {
      const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num);
      if (T) return (Value*)T;   // Asked for a primitive type...
    }

    // Otherwise, derived types need offset...
    Num -= FirstDerivedTyID;

    // Is it a module-level type?
    if (Num < ModuleTypeValues.size())
      return (Value*)ModuleTypeValues[Num].get();

    // Nope, is it a function-level type?
    Num -= ModuleTypeValues.size();
    if (Num < FunctionTypeValues.size())
      return (Value*)FunctionTypeValues[Num].get();

    return 0;
  }

  if (HasImplicitZeroInitializer && type >= FirstDerivedTyID) {
    if (Num == 0)
      return Constant::getNullValue(Ty);
    --Num;
  }

  if (type < ModuleValues.size()) {
    if (Num < ModuleValues[type]->size())
      return ModuleValues[type]->getOperand(Num);
    Num -= ModuleValues[type]->size();
  }

  if (Values.size() > type && Values[type]->size() > Num)
    return Values[type]->getOperand(Num);

  if (!Create) return 0;  // Do not create a placeholder?

  Value *d = 0;
  switch (Ty->getPrimitiveID()) {
  case Type::LabelTyID:
    d = new BBPHolder(Ty, oNum);
    break;
  default:
    d = new ValPHolder(Ty, oNum);
    break;
  }

  assert(d != 0 && "How did we not make something?");
  if (insertValue(d, LateResolveValues) == -1) return 0;
  return d;
}

/// getConstantValue - Just like getValue, except that it returns a null pointer
/// only on error.  It always returns a constant (meaning that if the value is
/// defined, but is not a constant, that is an error).  If the specified
/// constant hasn't been parsed yet, a placeholder is defined and used.  Later,
/// after the real value is parsed, the placeholder is eliminated.
///
Constant *BytecodeParser::getConstantValue(const Type *Ty, unsigned Slot) {
  if (Value *V = getValue(Ty, Slot, false))
    return dyn_cast<Constant>(V);      // If we already have the value parsed...

  std::pair<const Type*, unsigned> Key(Ty, Slot);
  GlobalRefsType::iterator I = GlobalRefs.lower_bound(Key);

  if (I != GlobalRefs.end() && I->first == Key) {
    BCR_TRACE(5, "Previous forward ref found!\n");
    return cast<Constant>(I->second);
  } else {
    // Create a placeholder for the constant reference and
    // keep track of the fact that we have a forward ref to recycle it
    BCR_TRACE(5, "Creating new forward ref to a constant!\n");
    Constant *C = new ConstPHolder(Ty, Slot);
    
    // Keep track of the fact that we have a forward ref to recycle it
    GlobalRefs.insert(I, std::make_pair(Key, C));
    return C;
  }
}


void BytecodeParser::postResolveValues(ValueTable &ValTab) {
  while (!ValTab.empty()) {
    ValueList &DL = *ValTab.back();
    ValTab.pop_back();    

    while (!DL.empty()) {
      Value *D = DL.back();
      unsigned IDNumber = getValueIDNumberFromPlaceHolder(D);
      DL.pop_back();

      Value *NewDef = getValue(D->getType(), IDNumber, false);
      if (NewDef == 0) {
        throw std::string("Unresolvable reference found: <" +
                          D->getType()->getDescription() + ">:" + 
                          utostr(IDNumber) + ".");
      } else {
        // Fixup all of the uses of this placeholder def...
        D->replaceAllUsesWith(NewDef);

        // Now that all the uses are gone, delete the placeholder...
        // If we couldn't find a def (error case), then leak a little
        delete D;  // memory, 'cause otherwise we can't remove all uses!
      }
    }
    delete &DL;
  }
}

std::auto_ptr<BasicBlock>
BytecodeParser::ParseBasicBlock(const unsigned char *&Buf,
                                const unsigned char *EndBuf) {
  std::auto_ptr<BasicBlock> BB(new BasicBlock());

  while (Buf < EndBuf) {
    Instruction *Inst;
    ParseInstruction(Buf, EndBuf, Inst);

    if (Inst == 0) { throw std::string("Could not parse Instruction."); }
    if (insertValue(Inst, Values) == -1) { 
      throw std::string("Could not insert value.");
    }

    BB->getInstList().push_back(Inst);
    BCR_TRACE(4, Inst);
  }

  return BB;
}

void BytecodeParser::ParseSymbolTable(const unsigned char *&Buf,
                                      const unsigned char *EndBuf,
                                      SymbolTable *ST) {
  while (Buf < EndBuf) {
    // Symtab block header: [num entries][type id number]
    unsigned NumEntries, Typ;
    if (read_vbr(Buf, EndBuf, NumEntries) ||
        read_vbr(Buf, EndBuf, Typ)) throw Error_readvbr;
    const Type *Ty = getType(Typ);
    if (Ty == 0) throw std::string("Invalid type read in symbol table.");

    BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
                 " entries\n");

    for (unsigned i = 0; i < NumEntries; ++i) {
      // Symtab entry: [def slot #][name]
      unsigned slot;
      if (read_vbr(Buf, EndBuf, slot)) throw Error_readvbr;
      std::string Name;
      if (read(Buf, EndBuf, Name, false))  // Not aligned...
        throw std::string("Buffer not aligned.");

      Value *V = getValue(Ty, slot, false); // Find mapping...
      if (V == 0) {
        BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << "\n");
        throw std::string("Failed value look-up.");
      }
      BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << *V;
                if (!isa<Instruction>(V)) std::cerr << "\n");

      V->setName(Name, ST);
    }
  }

  if (Buf > EndBuf) throw std::string("Tried to read past end of buffer.");
}

void BytecodeParser::ResolveReferencesToValue(Value *NewV, unsigned Slot) {
  GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(NewV->getType(),
                                                              Slot));
  if (I == GlobalRefs.end()) return;   // Never forward referenced?

  BCR_TRACE(3, "Mutating forward refs!\n");
  Value *VPH = I->second;   // Get the placeholder...

  VPH->replaceAllUsesWith(NewV);

  // If this is a global variable being resolved, remove the placeholder from
  // the module...
  if (GlobalValue* GVal = dyn_cast<GlobalValue>(NewV))
    GVal->getParent()->getGlobalList().remove(cast<GlobalVariable>(VPH));

  delete VPH;                         // Delete the old placeholder
  GlobalRefs.erase(I);                // Remove the map entry for it
}

void
BytecodeParser::ParseFunction(const unsigned char *&Buf,
                              const unsigned char *EndBuf) {
  if (FunctionSignatureList.empty())
    throw std::string("FunctionSignatureList empty!");

  Function *F = FunctionSignatureList.back().first;
  unsigned FunctionSlot = FunctionSignatureList.back().second;
  FunctionSignatureList.pop_back();

  // Save the information for future reading of the function
  LazyFunctionInfo *LFI = new LazyFunctionInfo();
  LFI->Buf = Buf; LFI->EndBuf = EndBuf; LFI->FunctionSlot = FunctionSlot;
  LazyFunctionLoadMap[F] = LFI;
  // Pretend we've `parsed' this function
  Buf = EndBuf;
}

void BytecodeParser::materializeFunction(Function* F) {
  // Find {start, end} pointers and slot in the map. If not there, we're done.
  std::map<Function*, LazyFunctionInfo*>::iterator Fi =
    LazyFunctionLoadMap.find(F);
  if (Fi == LazyFunctionLoadMap.end()) return;
  
  LazyFunctionInfo *LFI = Fi->second;
  const unsigned char *Buf = LFI->Buf;
  const unsigned char *EndBuf = LFI->EndBuf;
  unsigned FunctionSlot = LFI->FunctionSlot;
  LazyFunctionLoadMap.erase(Fi);
  delete LFI;

  GlobalValue::LinkageTypes Linkage = GlobalValue::ExternalLinkage;

  if (!hasInternalMarkerOnly) {
    unsigned LinkageType;
    if (read_vbr(Buf, EndBuf, LinkageType)) 
      throw std::string("ParseFunction: Error reading from buffer.");
    if (LinkageType & ~0x3) 
      throw std::string("Invalid linkage type for Function.");
    Linkage = (GlobalValue::LinkageTypes)LinkageType;
  } else {
    // We used to only support two linkage models: internal and external
    unsigned isInternal;
    if (read_vbr(Buf, EndBuf, isInternal)) 
      throw std::string("ParseFunction: Error reading from buffer.");
    if (isInternal) Linkage = GlobalValue::InternalLinkage;
  }

  F->setLinkage(Linkage);

  const FunctionType::ParamTypes &Params =F->getFunctionType()->getParamTypes();
  Function::aiterator AI = F->abegin();
  for (FunctionType::ParamTypes::const_iterator It = Params.begin();
       It != Params.end(); ++It, ++AI) {
    if (insertValue(AI, Values) == -1)
      throw std::string("Error reading function arguments!");
  }

  while (Buf < EndBuf) {
    unsigned Type, Size;
    const unsigned char *OldBuf = Buf;
    readBlock(Buf, EndBuf, Type, Size);

    switch (Type) {
    case BytecodeFormat::ConstantPool: {
      BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
      ParseConstantPool(Buf, Buf+Size, Values, FunctionTypeValues);
      break;
    }

    case BytecodeFormat::BasicBlock: {
      BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
      std::auto_ptr<BasicBlock> BB = ParseBasicBlock(Buf, Buf+Size);
      if (!BB.get() || insertValue(BB.get(), Values) == -1)
        throw std::string("Parse error: BasicBlock");

      F->getBasicBlockList().push_back(BB.release());
      break;
    }

    case BytecodeFormat::SymbolTable: {
      BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
      ParseSymbolTable(Buf, Buf+Size, &F->getSymbolTable());
      break;
    }

    default:
      BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
      Buf += Size;
      if (OldBuf > Buf) 
        throw std::string("Wrapped around reading bytecode.");
      break;
    }
    BCR_TRACE(2, "} end block\n");

    // Malformed bc file if read past end of block.
    ALIGN32(Buf, EndBuf);
  }

  // Check for unresolvable references
  postResolveValues(LateResolveValues);

  //ResolveReferencesToValue(F, FunctionSlot);

  // Clear out function-level types...
  FunctionTypeValues.clear();

  freeTable(Values);
}

void BytecodeParser::ParseModuleGlobalInfo(const unsigned char *&Buf,
                                           const unsigned char *End) {
  if (!FunctionSignatureList.empty())
    throw std::string("Two ModuleGlobalInfo packets found!");

  // Read global variables...
  unsigned VarType;
  if (read_vbr(Buf, End, VarType)) throw Error_readvbr;
  while (VarType != Type::VoidTyID) { // List is terminated by Void
    unsigned SlotNo;
    GlobalValue::LinkageTypes Linkage;

    if (!hasInternalMarkerOnly) {
      // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
      // bit2,3 = Linkage, bit4+ = slot#
      SlotNo = VarType >> 4;
      Linkage = (GlobalValue::LinkageTypes)((VarType >> 2) & 3);
    } else {
      // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
      // bit2 = isInternal, bit3+ = slot#
      SlotNo = VarType >> 3;
      Linkage = (VarType & 4) ? GlobalValue::InternalLinkage :
        GlobalValue::ExternalLinkage;
    }

    const Type *Ty = getType(SlotNo);
    if (!Ty || !isa<PointerType>(Ty))
      throw std::string("Global not pointer type!  Ty = " + 
                        Ty->getDescription());

    const Type *ElTy = cast<PointerType>(Ty)->getElementType();

    // Create the global variable...
    GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, Linkage,
                                            0, "", TheModule);
    int DestSlot = insertValue(GV, ModuleValues);
    if (DestSlot == -1) throw Error_DestSlot;
    BCR_TRACE(2, "Global Variable of type: " << *Ty << "\n");
    ResolveReferencesToValue(GV, (unsigned)DestSlot);

    if (VarType & 2) { // Does it have an initializer?
      unsigned InitSlot;
      if (read_vbr(Buf, End, InitSlot)) throw Error_readvbr;
      GlobalInits.push_back(std::make_pair(GV, InitSlot));
    }
    if (read_vbr(Buf, End, VarType)) throw Error_readvbr;
  }

  // Read the function objects for all of the functions that are coming
  unsigned FnSignature;
  if (read_vbr(Buf, End, FnSignature)) throw Error_readvbr;
  while (FnSignature != Type::VoidTyID) { // List is terminated by Void
    const Type *Ty = getType(FnSignature);
    if (!Ty || !isa<PointerType>(Ty) ||
        !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) { 
      throw std::string("Function not ptr to func type!  Ty = " +
                        Ty->getDescription());
    }

    // We create functions by passing the underlying FunctionType to create...
    Ty = cast<PointerType>(Ty)->getElementType();

    // When the ModuleGlobalInfo section is read, we load the type of each
    // function and the 'ModuleValues' slot that it lands in.  We then load a
    // placeholder into its slot to reserve it.  When the function is loaded,
    // this placeholder is replaced.

    // Insert the placeholder...
    Function *Func = new Function(cast<FunctionType>(Ty),
                                  GlobalValue::InternalLinkage, "", TheModule);
    int DestSlot = insertValue(Func, ModuleValues);
    if (DestSlot == -1) throw Error_DestSlot;
    ResolveReferencesToValue(Func, (unsigned)DestSlot);

    // Keep track of this information in a list that is emptied as functions are
    // loaded...
    //
    FunctionSignatureList.push_back(std::make_pair(Func, DestSlot));

    if (read_vbr(Buf, End, FnSignature)) throw Error_readvbr;
    BCR_TRACE(2, "Function of type: " << Ty << "\n");
  }

  ALIGN32(Buf, End);

  // Now that the function signature list is set up, reverse it so that we can 
  // remove elements efficiently from the back of the vector.
  std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end());

  // This is for future proofing... in the future extra fields may be added that
  // we don't understand, so we transparently ignore them.
  //
  Buf = End;
}

void BytecodeParser::ParseVersionInfo(const unsigned char *&Buf,
                                      const unsigned char *EndBuf) {
  unsigned Version;
  if (read_vbr(Buf, EndBuf, Version)) throw Error_readvbr;

  // Unpack version number: low four bits are for flags, top bits = version
  Module::Endianness  Endianness;
  Module::PointerSize PointerSize;
  Endianness  = (Version & 1) ? Module::BigEndian : Module::LittleEndian;
  PointerSize = (Version & 2) ? Module::Pointer64 : Module::Pointer32;

  bool hasNoEndianness = Version & 4;
  bool hasNoPointerSize = Version & 8;
  
  RevisionNum = Version >> 4;

  // Default values for the current bytecode version
  HasImplicitZeroInitializer = true;
  hasInternalMarkerOnly = false;
  FirstDerivedTyID = 14;

  switch (RevisionNum) {
  case 0:                  // Initial revision
    // Version #0 didn't have any of the flags stored correctly, and in fact as
    // only valid with a 14 in the flags values.  Also, it does not support
    // encoding zero initializers for arrays compactly.
    //
    if (Version != 14) throw std::string("Unknown revision 0 flags?");
    HasImplicitZeroInitializer = false;
    Endianness  = Module::BigEndian;
    PointerSize = Module::Pointer64;
    hasInternalMarkerOnly = true;
    hasNoEndianness = hasNoPointerSize = false;
    break;
  case 1:
    // Version #1 has four bit fields: isBigEndian, hasLongPointers,
    // hasNoEndianness, and hasNoPointerSize.
    hasInternalMarkerOnly = true;
    break;
  case 2:
    // Version #2 added information about all 4 linkage types instead of just
    // having internal and external.
    break;
  default:
    throw std::string("Unknown bytecode version number!");
  }

  if (hasNoEndianness) Endianness  = Module::AnyEndianness;
  if (hasNoPointerSize) PointerSize = Module::AnyPointerSize;

  TheModule->setEndianness(Endianness);
  TheModule->setPointerSize(PointerSize);
  BCR_TRACE(1, "Bytecode Rev = " << (unsigned)RevisionNum << "\n");
  BCR_TRACE(1, "Endianness/PointerSize = " << Endianness << ","
               << PointerSize << "\n");
  BCR_TRACE(1, "HasImplicitZeroInit = " << HasImplicitZeroInitializer << "\n");
}

void BytecodeParser::ParseModule(const unsigned char *Buf,
                                 const unsigned char *EndBuf) {
  unsigned Type, Size;
  readBlock(Buf, EndBuf, Type, Size);
  if (Type != BytecodeFormat::Module || Buf+Size != EndBuf)
    throw std::string("Expected Module packet! B: "+
        utostr((unsigned)(intptr_t)Buf) + ", S: "+utostr(Size)+
        " E: "+utostr((unsigned)(intptr_t)EndBuf)); // Hrm, not a class?

  BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
  FunctionSignatureList.clear();                 // Just in case...

  // Read into instance variables...
  ParseVersionInfo(Buf, EndBuf);
  ALIGN32(Buf, EndBuf);

  while (Buf < EndBuf) {
    const unsigned char *OldBuf = Buf;
    readBlock(Buf, EndBuf, Type, Size);
    switch (Type) {
    case BytecodeFormat::GlobalTypePlane:
      BCR_TRACE(1, "BLOCK BytecodeFormat::GlobalTypePlane: {\n");
      ParseGlobalTypes(Buf, Buf+Size);
      break;

    case BytecodeFormat::ModuleGlobalInfo:
      BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
      ParseModuleGlobalInfo(Buf, Buf+Size);
      break;

    case BytecodeFormat::ConstantPool:
      BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
      ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues);
      break;

    case BytecodeFormat::Function: {
      BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
      ParseFunction(Buf, Buf+Size);
      break;
    }

    case BytecodeFormat::SymbolTable:
      BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
      ParseSymbolTable(Buf, Buf+Size, &TheModule->getSymbolTable());
      break;

    default:
      Buf += Size;
      if (OldBuf > Buf) throw std::string("Expected Module Block!");
      break;
    }
    BCR_TRACE(1, "} end block\n");
    ALIGN32(Buf, EndBuf);
  }

  // After the module constant pool has been read, we can safely initialize
  // global variables...
  while (!GlobalInits.empty()) {
    GlobalVariable *GV = GlobalInits.back().first;
    unsigned Slot = GlobalInits.back().second;
    GlobalInits.pop_back();

    // Look up the initializer value...
    if (Value *V = getValue(GV->getType()->getElementType(), Slot, false)) {
      if (GV->hasInitializer()) 
        throw std::string("Global *already* has an initializer?!");
      GV->setInitializer(cast<Constant>(V));
    } else
      throw std::string("Cannot find initializer value.");
  }

  if (!FunctionSignatureList.empty())
    throw std::string("Function expected, but bytecode stream ended!");

  BCR_TRACE(0, "} end block\n\n");
}

void
BytecodeParser::ParseBytecode(const unsigned char *Buf, unsigned Length,
                              const std::string &ModuleID) {

  unsigned char *EndBuf = (unsigned char*)(Buf + Length);

  // Read and check signature...
  unsigned Sig;
  if (read(Buf, EndBuf, Sig) ||
      Sig != ('l' | ('l' << 8) | ('v' << 16) | ('m' << 24)))
    throw std::string("Invalid bytecode signature!");

  TheModule = new Module(ModuleID);
  try { 
    ParseModule(Buf, EndBuf);
  } catch (std::string &Error) {
    freeState();       // Must destroy handles before deleting module!
    delete TheModule;
    TheModule = 0;
    throw Error;
  }
}