First try at a horrible global value reference wrapper
authorChris Lattner <sabre@nondot.org>
Wed, 3 Oct 2001 06:12:09 +0000 (06:12 +0000)
committerChris Lattner <sabre@nondot.org>
Wed, 3 Oct 2001 06:12:09 +0000 (06:12 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@701 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ConstPoolVals.h
lib/AsmParser/llvmAsmParser.y
lib/VMCore/ConstPoolVals.cpp

index 4edfa807a181d27ffd55364c12946bf2dc39cb36..a2cd1431b2171ce41278842e826d69e7e3831e0e 100644 (file)
@@ -218,10 +218,38 @@ protected:
   ~ConstPoolPointer() {}
 public:
   static ConstPoolPointer *getNullPointer(const PointerType *T) {
+    // FIXME: These should all be shared!
     return new ConstPoolPointer(T);
   }
 
   virtual string getStrValue() const;
 };
 
+
+// ConstPoolPointerReference - a constant pointer value that is initialized to
+// point to a global value, which is a constant.
+//
+class ConstPoolPointerReference : public ConstPoolPointer {
+  ConstPoolPointerReference(const ConstPoolPointerReference &); // DNI!
+protected:
+  ConstPoolPointerReference(GlobalVariable *GV);
+  ~ConstPoolPointerReference() {}
+public:
+  static ConstPoolPointerReference *get(GlobalVariable *GV) {
+    // FIXME: These should all be shared!
+    return new ConstPoolPointerReference(GV);
+  }
+
+  virtual string getStrValue() const;
+
+  const GlobalVariable *getValue() const { 
+    return cast<GlobalVariable>(Operands[0].get());
+  }
+  GlobalVariable *getValue() {
+    return cast<GlobalVariable>(Operands[0].get());
+  }
+};
+
+
+
 #endif
index 5771318f6bf9aeff1e57845e23011bb558bbe358..6852df7dc6b93d8dc87fc5c7b54d47798f080732 100644 (file)
@@ -175,6 +175,23 @@ static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
   return Typ;
 }
 
+static Value *lookupInSymbolTable(const Type *Ty, const string &Name) {
+  SymbolTable *SymTab = 
+    CurMeth.CurrentMethod ? CurMeth.CurrentMethod->getSymbolTable() : 0;
+  Value *N = SymTab ? SymTab->lookup(Ty, Name) : 0;
+
+  if (N == 0) {
+    // Symbol table doesn't automatically chain yet... because the method
+    // hasn't been added to the module...
+    //
+    SymTab = CurModule.CurrentModule->getSymbolTable();
+    if (SymTab)
+      N = SymTab->lookup(Ty, Name);
+  }
+
+  return N;
+}
+
 static Value *getVal(const Type *Ty, const ValID &D, 
                      bool DoNotImprovise = false) {
   assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
@@ -204,20 +221,8 @@ static Value *getVal(const Type *Ty, const ValID &D,
   }
   case ValID::NameVal: {                // Is it a named definition?
     string Name(D.Name);
-    SymbolTable *SymTab = 0;
-    if (CurMeth.CurrentMethod) 
-      SymTab = CurMeth.CurrentMethod->getSymbolTable();
-    Value *N = SymTab ? SymTab->lookup(Ty, Name) : 0;
-
-    if (N == 0) {
-      // Symbol table doesn't automatically chain yet... because the method
-      // hasn't been added to the module...
-      //
-      SymTab = CurModule.CurrentModule->getSymbolTable();
-      if (SymTab)
-        N = SymTab->lookup(Ty, Name);
-      if (N == 0) break;
-    }
+    Value *N = lookupInSymbolTable(Ty, Name);
+    if (N == 0) break;
 
     D.destroy();  // Free old strdup'd memory...
     return N;
@@ -849,12 +854,28 @@ ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
     $$ = ConstPoolPointer::getNullPointer(PTy);
     delete $1;
   }
-/*
-  | Types '*' ConstVal {
-    assert(0);
-    $$ = 0;
+  | Types VAR_ID {
+    string Name($2); free($2);  // Change to a responsible mem manager
+    const PointerType *Ty = dyn_cast<const PointerType>($1->get());
+    if (Ty == 0)
+      ThrowException("Global const reference must be a pointer type!");
+
+    Value *N = lookupInSymbolTable(Ty, Name);
+    if (N == 0)
+      ThrowException("Global pointer reference '%" + Name +
+                     "' must be defined before use!");    
+
+    // TODO FIXME: This should also allow methods... when common baseclass
+    // exists
+    if (GlobalVariable *GV = dyn_cast<GlobalVariable>(N)) {
+      $$ = ConstPoolPointerReference::get(GV);
+    } else {
+      ThrowException("'%" + Name + "' is not a global value reference!");
+    }
+
+    delete $1;
   }
-*/
+
 
 ConstVal : SIntType EINT64VAL {     // integral constants
     if (!ConstPoolSInt::isValueValidForType($1, $2))
index f53c090f70b6a7df0295824df0a89c2900a6cea4..0e0da5e16e8ff5dda1ce47d947a50ec46b052386 100644 (file)
@@ -9,6 +9,7 @@
 #include "llvm/Support/StringExtras.h"  // itostr
 #include "llvm/DerivedTypes.h"
 #include "llvm/SymbolTable.h"
+#include "llvm/GlobalVariable.h"   // TODO make this GlobalValue.h
 #include <algorithm>
 #include <assert.h>
 
@@ -105,6 +106,12 @@ ConstPoolStruct::ConstPoolStruct(const StructType *T,
 
 ConstPoolPointer::ConstPoolPointer(const PointerType *T) : ConstPoolVal(T) {}
 
+ConstPoolPointerReference::ConstPoolPointerReference(GlobalVariable *GV)
+  : ConstPoolPointer(GV->getType()) {
+  Operands.push_back(Use(GV, this));
+}
+
+
 
 //===----------------------------------------------------------------------===//
 //                          getStrValue implementations
@@ -155,6 +162,10 @@ string ConstPoolPointer::getStrValue() const {
   return "null";
 }
 
+string ConstPoolPointerReference::getStrValue() const {
+  return "<pointer reference>";
+}
+
 //===----------------------------------------------------------------------===//
 //                      isValueValidForType implementations