Change references from Method to Function
[oota-llvm.git] / lib / Bytecode / Reader / Reader.cpp
index a2e72b2547deb465cb8b369cb2d498089d4c3917..0b2e935ed4e56bf7f741fda6253e95f1d5688c74 100644 (file)
 //
 //===----------------------------------------------------------------------===//
 
+#include "ReaderInternals.h"
 #include "llvm/Bytecode/Reader.h"
 #include "llvm/Bytecode/Format.h"
 #include "llvm/GlobalVariable.h"
 #include "llvm/Module.h"
 #include "llvm/BasicBlock.h"
-#include "llvm/DerivedTypes.h"
-#include "llvm/ConstPoolVals.h"
+#include "llvm/ConstantVals.h"
+#include "llvm/iPHINode.h"
 #include "llvm/iOther.h"
-#include "ReaderInternals.h"
 #include <sys/types.h>
-#include <sys/mman.h>
 #include <sys/stat.h>
+#include <sys/mman.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <algorithm>
+#include <iostream>
+using std::cerr;
+using std::make_pair;
 
 bool BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
   if (Ty->isPrimitiveType()) {
@@ -42,7 +45,7 @@ bool BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
       Slot = FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
     }
   }
-  //cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << endl;
+  //cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << "\n";
   return false;
 }
 
@@ -50,7 +53,7 @@ const Type *BytecodeParser::getType(unsigned ID) {
   const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
   if (T) return T;
   
-  //cerr << "Looking up Type ID: " << ID << endl;
+  //cerr << "Looking up Type ID: " << ID << "\n";
 
   const Value *D = getValue(Type::TypeTy, ID, false);
   if (D == 0) return failure<const Type*>(0);
@@ -58,7 +61,7 @@ const Type *BytecodeParser::getType(unsigned ID) {
   return cast<Type>(D);
 }
 
-int BytecodeParser::insertValue(Value *Val, vector<ValueList> &ValueTab) {
+int BytecodeParser::insertValue(Value *Val, std::vector<ValueList> &ValueTab) {
   unsigned type;
   if (getTypeSlot(Val->getType(), type)) return failure<int>(-1);
   assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
@@ -67,7 +70,7 @@ int BytecodeParser::insertValue(Value *Val, vector<ValueList> &ValueTab) {
     ValueTab.resize(type+1, ValueList());
 
   //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size() 
-  //     << "] = " << Val << endl;
+  //     << "] = " << Val << "\n";
   ValueTab[type].push_back(Val);
 
   return ValueTab[type].size()-1;
@@ -115,7 +118,7 @@ Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
   case Type::LabelTyID: d = new    BBPHolder(Ty, oNum); break;
   case Type::MethodTyID:
     cerr << "Creating method pholder! : " << type << ":" << oNum << " " 
-        << Ty->getName() << endl;
+        << Ty->getName() << "\n";
     d = new MethPHolder(Ty, oNum);
     if (insertValue(d, LateResolveModuleValues) ==-1) return failure<Value*>(0);
     return d;
@@ -196,17 +199,17 @@ bool BytecodeParser::ParseSymbolTable(const uchar *&Buf, const uchar *EndBuf,
       // Symtab entry: [def slot #][name]
       unsigned slot;
       if (read_vbr(Buf, EndBuf, slot)) return failure(true);
-      string Name;
+      std::string Name;
       if (read(Buf, EndBuf, Name, false))  // Not aligned...
        return failure(true);
 
       Value *D = getValue(Ty, slot, false); // Find mapping...
       if (D == 0) {
-       BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << endl);
+       BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << "\n");
        return failure(true);
       }
       BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << D;
-               if (!isa<Instruction>(D)) cerr << endl);
+               if (!isa<Instruction>(D)) cerr << "\n");
 
       D->setName(Name, ST);
     }
@@ -217,7 +220,7 @@ bool BytecodeParser::ParseSymbolTable(const uchar *&Buf, const uchar *EndBuf,
 }
 
 // DeclareNewGlobalValue - Patch up forward references to global values in the
-// form of ConstPoolPointerRef.
+// form of ConstantPointerRef.
 //
 void BytecodeParser::DeclareNewGlobalValue(GlobalValue *GV, unsigned Slot) {
   // Check to see if there is a forward reference to this global variable...
@@ -229,11 +232,11 @@ void BytecodeParser::DeclareNewGlobalValue(GlobalValue *GV, unsigned Slot) {
     BCR_TRACE(3, "Mutating CPPR Forward Ref!\n");
       
     // Loop over all of the uses of the GlobalValue.  The only thing they are
-    // allowed to be at this point is ConstPoolPointerRef's.
+    // allowed to be at this point is ConstantPointerRef's.
     assert(OldGV->use_size() == 1 && "Only one reference should exist!");
     while (!OldGV->use_empty()) {
-      User *U = OldGV->use_back();  // Must be a ConstPoolPointerRef...
-      ConstPoolPointerRef *CPPR = cast<ConstPoolPointerRef>(U);
+      User *U = OldGV->use_back();  // Must be a ConstantPointerRef...
+      ConstantPointerRef *CPPR = cast<ConstantPointerRef>(U);
       assert(CPPR->getValue() == OldGV && "Something isn't happy");
       
       BCR_TRACE(4, "Mutating Forward Ref!\n");
@@ -262,24 +265,27 @@ bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf,
   }
 
   const PointerType *PMTy = MethodSignatureList.front().first; // PtrMeth
-  const MethodType  *MTy  = dyn_cast<const MethodType>(PMTy->getValueType());
+  const MethodType  *MTy  = dyn_cast<const MethodType>(PMTy->getElementType());
   if (MTy == 0) return failure(true);  // Not ptr to method!
 
+  unsigned isInternal;
+  if (read_vbr(Buf, EndBuf, isInternal)) return failure(true);
+
   unsigned MethSlot = MethodSignatureList.front().second;
   MethodSignatureList.pop_front();
-  Method *M = new Method(MTy);
+  Method *M = new Method(MTy, isInternal != 0);
 
-  BCR_TRACE(2, "METHOD TYPE: " << MTy << endl);
+  BCR_TRACE(2, "METHOD TYPE: " << MTy << "\n");
 
   const MethodType::ParamTypes &Params = MTy->getParamTypes();
   for (MethodType::ParamTypes::const_iterator It = Params.begin();
        It != Params.end(); ++It) {
-    MethodArgument *MA = new MethodArgument(*It);
-    if (insertValue(MA, Values) == -1) {
+    FunctionArgument *FA = new FunctionArgument(*It);
+    if (insertValue(FA, Values) == -1) {
       Error = "Error reading method arguments!\n";
       delete M; return failure(true); 
     }
-    M->getArgumentList().push_back(MA);
+    M->getArgumentList().push_back(FA);
   }
 
   while (Buf < EndBuf) {
@@ -346,7 +352,7 @@ bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf,
   assert(!getTypeSlot(MTy, type) && "How can meth type not exist?");
   getTypeSlot(PMTy, type);
 
-  C->getMethodList().push_back(M);
+  C->getFunctionList().push_back(M);
 
   // Replace placeholder with the real method pointer...
   ModuleValues[type][MethSlot] = M;
@@ -380,17 +386,18 @@ bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
   unsigned VarType;
   if (read_vbr(Buf, End, VarType)) return failure(true);
   while (VarType != Type::VoidTyID) { // List is terminated by Void
-    // VarType Fields: bit0 = isConstant, bit1 = hasInitializer, bit2+ = slot#
-    const Type *Ty = getType(VarType >> 2);
+    // VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
+    // bit2 = isInternal, bit3+ = slot#
+    const Type *Ty = getType(VarType >> 3);
     if (!Ty || !Ty->isPointerType()) { 
       Error = "Global not pointer type!  Ty = " + Ty->getDescription();
       return failure(true); 
     }
 
     const PointerType *PTy = cast<const PointerType>(Ty);
-    const Type *ElTy = PTy->getValueType();
+    const Type *ElTy = PTy->getElementType();
 
-    ConstPoolVal *Initializer = 0;
+    Constant *Initializer = 0;
     if (VarType & 2) { // Does it have an initalizer?
       // Do not improvise... values must have been stored in the constant pool,
       // which should have been read before now.
@@ -400,11 +407,12 @@ bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
       
       Value *V = getValue(ElTy, InitSlot, false);
       if (V == 0) return failure(true);
-      Initializer = cast<ConstPoolVal>(V);
+      Initializer = cast<Constant>(V);
     }
 
     // Create the global variable...
-    GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, Initializer);
+    GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, VarType & 4,
+                                           Initializer);
     int DestSlot = insertValue(GV, ModuleValues);
     if (DestSlot == -1) return failure(true);
 
@@ -413,7 +421,7 @@ bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
     DeclareNewGlobalValue(GV, unsigned(DestSlot));
 
     BCR_TRACE(2, "Global Variable of type: " << PTy->getDescription() 
-             << " into slot #" << DestSlot << endl);
+             << " into slot #" << DestSlot << "\n");
 
     if (read_vbr(Buf, End, VarType)) return failure(true);
   }
@@ -425,13 +433,13 @@ bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
   while (MethSignature != Type::VoidTyID) { // List is terminated by Void
     const Type *Ty = getType(MethSignature);
     if (!Ty || !isa<PointerType>(Ty) ||
-        !isa<MethodType>(cast<PointerType>(Ty)->getValueType())) { 
+        !isa<MethodType>(cast<PointerType>(Ty)->getElementType())) { 
       Error = "Method not ptr to meth type!  Ty = " + Ty->getDescription();
       return failure(true); 
     }
     
     // We create methods by passing the underlying MethodType to create...
-    Ty = cast<PointerType>(Ty)->getValueType();
+    Ty = cast<PointerType>(Ty)->getElementType();
 
     // When the ModuleGlobalInfo section is read, we load the type of each 
     // method and the 'ModuleValues' slot that it lands in.  We then load a 
@@ -454,7 +462,7 @@ bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
     MethodSignatureList.push_back(
            make_pair(cast<const PointerType>(Val->getType()), SlotNo));
     if (read_vbr(Buf, End, MethSignature)) return failure(true);
-    BCR_TRACE(2, "Method of type: " << Ty << endl);
+    BCR_TRACE(2, "Method of type: " << Ty << "\n");
   }
 
   if (align32(Buf, End)) return failure(true);
@@ -561,11 +569,11 @@ Module *ParseBytecodeBuffer(const uchar *Buffer, unsigned Length) {
 
 // Parse and return a class file...
 //
-Module *ParseBytecodeFile(const string &Filename, string *ErrorStr) {
+Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) {
   struct stat StatBuf;
   Module *Result = 0;
 
-  if (Filename != string("-")) {        // Read from a file...
+  if (Filename != std::string("-")) {        // Read from a file...
     int FD = open(Filename.c_str(), O_RDONLY);
     if (FD == -1) {
       if (ErrorStr) *ErrorStr = "Error opening file!";
@@ -614,8 +622,8 @@ Module *ParseBytecodeFile(const string &Filename, string *ErrorStr) {
     uchar *Buf = (uchar*)mmap(0, FileSize, PROT_READ|PROT_WRITE, 
                              MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
     assert((Buf != (uchar*)-1) && "mmap returned error!");
-    free(FileData);
     memcpy(Buf, FileData, FileSize);
+    free(FileData);
 #else
     uchar *Buf = FileData;
 #endif