[llvm-commits] CVS: llvm/lib/AsmParser/ParserInternals.h llvmAsmParser.y

Chris Lattner lattner at cs.uiuc.edu
Tue Jul 13 01:28:36 PDT 2004



Changes in directory llvm/lib/AsmParser:

ParserInternals.h updated: 1.35 -> 1.36
llvmAsmParser.y updated: 1.176 -> 1.177

---
Log message:

Eliminate some mega-cruft here.  There is no reason to DERIVE FROM IR CLASSES
just to keep track of some per-object state!  Gaah!  Whoever wrote this stuff...
oh wait, that would be me.  Never mind.



---
Diffs of the changes:  (+26 -67)

Index: llvm/lib/AsmParser/ParserInternals.h
diff -u llvm/lib/AsmParser/ParserInternals.h:1.35 llvm/lib/AsmParser/ParserInternals.h:1.36
--- llvm/lib/AsmParser/ParserInternals.h:1.35	Thu Jun 17 13:18:08 2004
+++ llvm/lib/AsmParser/ParserInternals.h	Tue Jul 13 03:28:21 2004
@@ -158,60 +158,6 @@
   }
 };
 
-
-
-template<class SuperType>
-class PlaceholderValue : public SuperType {
-  ValID D;
-  int LineNum;
-public:
-  PlaceholderValue(const Type *Ty, const ValID &d) : SuperType(Ty), D(d) {
-    LineNum = llvmAsmlineno;
-  }
-  ValID &getDef() { return D; }
-  int getLineNum() const { return LineNum; }
-};
-
-struct InstPlaceHolderHelper : public Instruction {
-  InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
-
-  virtual Instruction *clone() const { abort(); return 0; }
-  virtual const char *getOpcodeName() const { return "placeholder"; }
-};
-
-struct BBPlaceHolderHelper : public BasicBlock {
-  BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
-    assert(Ty == Type::LabelTy);
-  }
-};
-
-typedef PlaceholderValue<InstPlaceHolderHelper>  ValuePlaceHolder;
-typedef PlaceholderValue<BBPlaceHolderHelper>    BBPlaceHolder;
-
-static inline ValID &getValIDFromPlaceHolder(const Value *Val) {
-  const Type *Ty = Val->getType();
-  if (isa<PointerType>(Ty) &&
-      isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
-    Ty = cast<PointerType>(Ty)->getElementType();
-
-  switch (Ty->getTypeID()) {
-  case Type::LabelTyID:  return ((BBPlaceHolder*)Val)->getDef();
-  default:               return ((ValuePlaceHolder*)Val)->getDef();
-  }
-}
-
-static inline int getLineNumFromPlaceHolder(const Value *Val) {
-  const Type *Ty = Val->getType();
-  if (isa<PointerType>(Ty) &&
-      isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
-    Ty = cast<PointerType>(Ty)->getElementType();
-
-  switch (Ty->getTypeID()) {
-  case Type::LabelTyID:  return ((BBPlaceHolder*)Val)->getLineNum();
-  default:               return ((ValuePlaceHolder*)Val)->getLineNum();
-  }
-}
-
 } // End llvm namespace
 
 #endif


Index: llvm/lib/AsmParser/llvmAsmParser.y
diff -u llvm/lib/AsmParser/llvmAsmParser.y:1.176 llvm/lib/AsmParser/llvmAsmParser.y:1.177
--- llvm/lib/AsmParser/llvmAsmParser.y:1.176	Tue Jul 13 03:12:39 2004
+++ llvm/lib/AsmParser/llvmAsmParser.y	Tue Jul 13 03:28:21 2004
@@ -69,6 +69,11 @@
   std::vector<PATypeHolder>    Types;
   std::map<ValID, PATypeHolder> LateResolveTypes;
 
+  /// PlaceHolderInfo - When temporary placeholder objects are created, remember
+  /// how they were referenced and one which line of the input they came from so
+  /// that we can resolve them alter and print error messages as appropriate.
+  std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
+
   // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
   // references to global values.  Global values may be referenced before they
   // are defined, and if so, the temporary object that they represent is held
@@ -356,7 +361,6 @@
   return 0;
 }
 
-
 // getVal - This function is identical to getValNonImprovising, except that if a
 // value is not already defined, it "improvises" by creating a placeholder var
 // that looks and acts just like the requested variable.  When the value is
@@ -373,18 +377,21 @@
   // or an id number that hasn't been read yet.  We may be referencing something
   // forward, so just create an entry to be resolved later and get to it...
   //
-  Value *d = 0;
-  switch (Ty->getTypeID()) {
-  case Type::LabelTyID:  d = new   BBPlaceHolder(Ty, D); break;
-  default:               d = new ValuePlaceHolder(Ty, D); break;
-  }
+  if (Ty == Type::LabelTy)
+    V = new BasicBlock();
+  else
+    V = new Argument(Ty);
+
+  // Remember where this forward reference came from.  FIXME, shouldn't we try
+  // to recycle these things??
+  CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(D,
+                                                               llvmAsmlineno)));
 
-  assert(d != 0 && "How did we not make something?");
   if (inFunctionScope())
-    InsertValue(d, CurFun.LateResolveValues);
+    InsertValue(V, CurFun.LateResolveValues);
   else 
-    InsertValue(d, CurModule.LateResolveValues);
-  return d;
+    InsertValue(V, CurModule.LateResolveValues);
+  return V;
 }
 
 
@@ -413,12 +420,18 @@
     while (!List.empty()) {
       Value *V = List.back();
       List.pop_back();
-      ValID &DID = getValIDFromPlaceHolder(V);
+
+      std::map<Value*, std::pair<ValID, int> >::iterator PHI =
+        CurModule.PlaceHolderInfo.find(V);
+      assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
+
+      ValID &DID = PHI->second.first;
 
       Value *TheRealValue = getValNonImprovising(LRI->first, DID);
       if (TheRealValue) {
         V->replaceAllUsesWith(TheRealValue);
         delete V;
+        CurModule.PlaceHolderInfo.erase(PHI);
       } else if (FutureLateResolvers) {
         // Functions have their unresolved items forwarded to the module late
         // resolver table
@@ -427,12 +440,12 @@
 	if (DID.Type == ValID::NameVal)
 	  ThrowException("Reference to an invalid definition: '" +DID.getName()+
 			 "' of type '" + V->getType()->getDescription() + "'",
-			 getLineNumFromPlaceHolder(V));
+			 PHI->second.second);
 	else
 	  ThrowException("Reference to an invalid definition: #" +
 			 itostr(DID.Num) + " of type '" + 
 			 V->getType()->getDescription() + "'",
-			 getLineNumFromPlaceHolder(V));
+			 PHI->second.second);
       }
     }
   }





More information about the llvm-commits mailing list