I can't believe the incompetence of the people I have to deal with around here.
[oota-llvm.git] / lib / Bytecode / Reader / ReaderInternals.h
index f08cedf8cfbc50045eaa8a30173230afd18a0c77..53f699e6d08714eec354e851519e6098c6d8b29d 100644 (file)
@@ -1,4 +1,11 @@
-//===-- ReaderInternals.h - Definitions internal to the reader ---*- C++ -*--=//
+//===-- ReaderInternals.h - Definitions internal to the reader --*- C++ -*-===//
+// 
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// 
+//===----------------------------------------------------------------------===//
 //
 //  This header file defines various stuff that is used by the bytecode reader.
 //
 #ifndef READER_INTERNALS_H
 #define READER_INTERNALS_H
 
-#include "llvm/Constant.h"
+#include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/Function.h"
 #include "llvm/ModuleProvider.h"
 #include "llvm/Bytecode/Primitives.h"
 #include <utility>
 #include <map>
-#include <memory>
-class Module;
+
+namespace llvm {
 
 // Enable to trace to figure out what the heck is going on when parsing fails
 //#define TRACE_LEVEL 10
+//#define DEBUG_OUTPUT
 
 #if TRACE_LEVEL    // ByteCodeReading_TRACEr
 #define BCR_TRACE(n, X) \
@@ -27,17 +35,6 @@ class Module;
 #define BCR_TRACE(n, X)
 #endif
 
-struct RawInst {       // The raw fields out of the bytecode stream...
-  unsigned NumOperands;
-  unsigned Opcode;
-  const Type *Ty;
-  unsigned Arg1, Arg2;
-  union {
-    unsigned Arg3;
-    std::vector<unsigned> *VarArgs; // Contains arg #3,4,5... if NumOperands > 3
-  };
-};
-
 struct LazyFunctionInfo {
   const unsigned char *Buf, *EndBuf;
   unsigned FunctionSlot;
@@ -57,17 +54,14 @@ public:
   }
   void freeState() {
     freeTable(Values);
-    freeTable(LateResolveValues);
     freeTable(ModuleValues);
   }
 
   Module* releaseModule() {
     // Since we're losing control of this Module, we must hand it back complete
-    materializeModule();
+    Module *M = ModuleProvider::releaseModule();
     freeState();
-    Module *tempM = TheModule; 
-    TheModule = 0; 
-    return tempM; 
+    return M;
   }
 
   void ParseBytecode(const unsigned char *Buf, unsigned Length,
@@ -77,11 +71,9 @@ public:
     std::cerr << "BytecodeParser instance!\n";
   }
 
-private:          // All of this data is transient across calls to ParseBytecode
+private:
   struct ValueList : public User {
-    ValueList() : User(Type::TypeTy, Value::TypeVal) {
-    }
-    ~ValueList() {}
+    ValueList() : User(Type::TypeTy, Value::TypeVal) {}
 
     // vector compatibility methods
     unsigned size() const { return getNumOperands(); }
@@ -98,12 +90,19 @@ private:          // All of this data is transient across calls to ParseBytecode
   // Information about the module, extracted from the bytecode revision number.
   unsigned char RevisionNum;        // The rev # itself
   unsigned char FirstDerivedTyID;   // First variable index to use for type
-  bool HasImplicitZeroInitializer;  // Is entry 0 of every slot implicity zeros?
   bool hasInternalMarkerOnly;       // Only types of linkage are intern/external
+  bool hasExtendedLinkageSpecs;     // Supports more than 4 linkage types
+  bool hasOldStyleVarargs;          // Has old version of varargs intrinsics?
+  bool hasVarArgCallPadding;        // Bytecode has extra padding in vararg call
+
+  bool usesOldStyleVarargs;         // Does this module USE old style varargs?
 
   typedef std::vector<ValueList*> ValueTable;
-  ValueTable Values, LateResolveValues;
+  ValueTable Values;
   ValueTable ModuleValues;
+  std::map<std::pair<unsigned,unsigned>, Value*> ForwardReferences;
+
+  std::vector<BasicBlock*> ParsedBasicBlocks;
 
   // GlobalRefs - This maintains a mapping between <Type, Slot #>'s and forward
   // references to global values or constants.  Such values may be referenced
@@ -155,17 +154,16 @@ private:
   void ParseVersionInfo   (const unsigned char *&Buf, const unsigned char *End);
   void ParseModuleGlobalInfo(const unsigned char *&Buf, const unsigned char *E);
   void ParseSymbolTable(const unsigned char *&Buf, const unsigned char *End,
-                        SymbolTable *);
+                        SymbolTable *, Function *CurrentFunction);
   void ParseFunction(const unsigned char *&Buf, const unsigned char *End);
   void ParseGlobalTypes(const unsigned char *&Buf, const unsigned char *EndBuf);
 
-  std::auto_ptr<BasicBlock>
-  ParseBasicBlock(const unsigned char *&Buf, const unsigned char *End);
+  BasicBlock *ParseBasicBlock(const unsigned char *&Buf,
+                              const unsigned char *End,
+                              unsigned BlockNo);
 
-  bool ParseInstruction   (const unsigned char *&Buf, const unsigned char *End,
-                           Instruction *&);
-  std::auto_ptr<RawInst> ParseRawInst(const unsigned char *&Buf,
-                                      const unsigned char *End);
+  void ParseInstruction(const unsigned char *&Buf, const unsigned char *End,
+                        std::vector<unsigned> &Args, BasicBlock *BB);
 
   void ParseConstantPool(const unsigned char *&Buf, const unsigned char *EndBuf,
                          ValueTable &Tab, TypeValuesListTy &TypeTab);
@@ -179,12 +177,13 @@ private:
                                 const unsigned char *EndBuf);
 
   Value      *getValue(const Type *Ty, unsigned num, bool Create = true);
+  Value      *getValue(unsigned TypeID, unsigned num, bool Create = true);
   const Type *getType(unsigned ID);
+  BasicBlock *getBasicBlock(unsigned ID);
   Constant   *getConstantValue(const Type *Ty, unsigned num);
 
-  int insertValue(Value *V, ValueTable &Table);  // -1 = Failure
-  void setValueTo(ValueTable &D, unsigned Slot, Value *V);
-  void postResolveValues(ValueTable &ValTab);
+  unsigned insertValue(Value *V, ValueTable &Table);
+  unsigned insertValue(Value *V, unsigned Type, ValueTable &Table);
 
   unsigned getTypeSlot(const Type *Ty);
 
@@ -202,27 +201,11 @@ public:
   unsigned getID() { return ID; }
 };
 
-struct InstPlaceHolderHelper : public Instruction {
-  InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
-  virtual const char *getOpcodeName() const { return "placeholder"; }
-
-  virtual Instruction *clone() const { abort(); return 0; }
-};
-
-struct BBPlaceHolderHelper : public BasicBlock {
-  BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
-    assert(Ty == Type::LabelTy);
-  }
-};
-
-struct ConstantPlaceHolderHelper : public Constant {
+struct ConstantPlaceHolderHelper : public ConstantExpr {
   ConstantPlaceHolderHelper(const Type *Ty)
-    : Constant(Ty) {}
-  virtual bool isNullValue() const { return false; }
+    : ConstantExpr(Instruction::UserOp1, Constant::getNullValue(Ty), Ty) {}
 };
 
-typedef PlaceholderDef<InstPlaceHolderHelper>  ValPHolder;
-typedef PlaceholderDef<BBPlaceHolderHelper>    BBPHolder;
 typedef PlaceholderDef<ConstantPlaceHolderHelper>  ConstPHolder;
 
 // Some common errors we find
@@ -231,28 +214,19 @@ static const std::string Error_read      = "read(): error reading.";
 static const std::string Error_inputdata = "input_data(): error reading.";
 static const std::string Error_DestSlot  = "No destination slot found.";
 
-static inline unsigned getValueIDNumberFromPlaceHolder(Value *Val) {
-  if (isa<Constant>(Val))
-    return ((ConstPHolder*)Val)->getID();
-  
-  // else discriminate by type
-  switch (Val->getType()->getPrimitiveID()) {
-  case Type::LabelTyID:    return ((BBPHolder*)Val)->getID();
-  default:                 return ((ValPHolder*)Val)->getID();
-  }
-}
-
 static inline void readBlock(const unsigned char *&Buf,
                              const unsigned char *EndBuf, 
                              unsigned &Type, unsigned &Size) {
-#if DEBUG_OUTPUT
+#ifdef DEBUG_OUTPUT
   bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
   std::cerr << "StartLoc = " << ((unsigned)Buf & 4095)
-       << " Type = " << Type << " Size = " << Size << endl;
+       << " Type = " << Type << " Size = " << Size << "\n";
   if (Result) throw Error_read;
 #else
   if (read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size)) throw Error_read;
 #endif
 }
 
+} // End llvm namespace
+
 #endif