Create a new #include "Support/..." directory structure to move things
[oota-llvm.git] / lib / AsmParser / llvmAsmParser.y
index 60c966e1eb7d2fe9938f04c594c86fcf927f525c..0f5c11e1eae514ad55b904a0ba05884722b78e36 100644 (file)
@@ -21,8 +21,8 @@
 #include "llvm/DerivedTypes.h"
 #include "llvm/iTerminators.h"
 #include "llvm/iMemory.h"
-#include "llvm/Support/STLExtras.h"
-#include "llvm/Support/DepthFirstIterator.h"
+#include "Support/STLExtras.h"
+#include "Support/DepthFirstIterator.h"
 #include <list>
 #include <utility>            // Get definition of pair class
 #include <algorithm>
@@ -237,7 +237,7 @@ static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
     return I->second;
   }
 
-  Type *Typ = new TypePlaceHolder(Type::TypeTy, D);
+  Type *Typ = OpaqueType::get();
   LateResolver.insert(make_pair(D, Typ));
   return Typ;
 }
@@ -451,7 +451,7 @@ static bool ResolveType(PATypeHolder<Type> &T) {
 // refering to the number can be resolved.  Do this now.
 //
 static void ResolveTypeTo(char *Name, const Type *ToTy) {
-  vector<PATypeHolder<Type> > &Types = inMethodScope ? 
+  vector<PATypeHolder<Type> > &Types = inMethodScope() ? 
      CurMeth.Types : CurModule.Types;
 
    ValID D;
@@ -473,14 +473,12 @@ static void ResolveTypeTo(char *Name, const Type *ToTy) {
 //
 static void ResolveTypes(map<ValID, PATypeHolder<Type> > &LateResolveTypes) {
   if (!LateResolveTypes.empty()) {
-    ValID &DID = LateResolveTypes.begin()->first;
+    const ValID &DID = LateResolveTypes.begin()->first;
 
     if (DID.Type == ValID::NameVal)
-      ThrowException("Reference to an invalid type: '" +DID.getName() + "'",
-                     getLineNumFromPlaceHolder(Ty));
+      ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
     else
-      ThrowException("Reference to an invalid type: #" + itostr(DID.Num),
-                     getLineNumFromPlaceHolder(Ty));
+      ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
   }
 }
 
@@ -570,20 +568,23 @@ static vector<pair<unsigned, OpaqueType *> > UpRefs;
 
 static PATypeHolder<Type> HandleUpRefs(const Type *ty) {
   PATypeHolder<Type> Ty(ty);
-  UR_OUT(UpRefs.size() << " upreferences active!\n");
+  UR_OUT("Type '" << ty->getDescription() << 
+         "' newly formed.  Resolving upreferences.\n" <<
+         UpRefs.size() << " upreferences active!\n");
   for (unsigned i = 0; i < UpRefs.size(); ) {
-    UR_OUT("TypeContains(" << Ty->getDescription() << ", " 
+    UR_OUT("  UR#" << i << " - TypeContains(" << Ty->getDescription() << ", " 
           << UpRefs[i].second->getDescription() << ") = " 
-          << TypeContains(Ty, UpRefs[i].second) << endl);
+          << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << endl);
     if (TypeContains(Ty, UpRefs[i].second)) {
       unsigned Level = --UpRefs[i].first;   // Decrement level of upreference
-      UR_OUT("Uplevel Ref Level = " << Level << endl);
+      UR_OUT("  Uplevel Ref Level = " << Level << endl);
       if (Level == 0) {                     // Upreference should be resolved! 
-       UR_OUT("About to resolve upreference!\n";
+       UR_OUT("  * Resolving upreference for "
+               << UpRefs[i].second->getDescription() << endl;
               string OldName = UpRefs[i].second->getDescription());
        UpRefs[i].second->refineAbstractTypeTo(Ty);
        UpRefs.erase(UpRefs.begin()+i);     // Remove from upreference list...
-       UR_OUT("Type '" << OldName << "' refined upreference to: "
+       UR_OUT("  * Type '" << OldName << "' refined upreference to: "
               << (const void*)Ty << ", " << Ty->getDescription() << endl);
        continue;
       }
@@ -646,7 +647,7 @@ Module *RunVMAsmParser(const string &Filename, FILE *F) {
   Value                            *ValueVal;
 
   list<MethodArgument*>            *MethodArgList;
-  list<Value*>                     *ValueList;
+  vector<Value*>                   *ValueList;
   list<PATypeHolder<Type> >        *TypeList;
   list<pair<Value*, BasicBlock*> > *PHIList;   // Represent the RHS of PHI node
   list<pair<ConstPoolVal*, BasicBlock*> > *JumpTable;
@@ -675,14 +676,15 @@ Module *RunVMAsmParser(const string &Filename, FILE *F) {
 %type <TermInstVal>   BBTerminatorInst
 %type <InstVal>       Inst InstVal MemoryInst
 %type <ConstVal>      ConstVal
-%type <ConstVector>   ConstVector UByteList
+%type <ConstVector>   ConstVector
 %type <MethodArgList> ArgList ArgListH
 %type <MethArgVal>    ArgVal
 %type <PHIList>       PHIList
 %type <ValueList>     ValueRefList ValueRefListE  // For call param lists
+%type <ValueList>     IndexList                   // For GEP derived indices
 %type <TypeList>      TypeListI ArgTypeListI
 %type <JumpTable>     JumpTable
-%type <BoolVal>       GlobalType                  // GLOBAL or CONSTANT?
+%type <BoolVal>       GlobalType OptInternal      // GLOBAL or CONSTANT? Intern?
 
 // ValueRef - Unresolved reference to a definition or BB
 %type <ValIDVal>      ValueRef ConstValueRef SymbolicValueRef
@@ -713,7 +715,7 @@ Module *RunVMAsmParser(const string &Filename, FILE *F) {
 
 
 %token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE GLOBAL CONSTANT UNINIT
-%token TO EXCEPT DOTDOTDOT STRING NULL_TOK CONST
+%token TO EXCEPT DOTDOTDOT STRING NULL_TOK CONST INTERNAL
 
 // Basic Block Terminating Operators 
 %token <TermOpVal> RET BR SWITCH
@@ -778,6 +780,7 @@ OptAssign : VAR_ID '=' {
     $$ = 0; 
   }
 
+OptInternal : INTERNAL { $$ = true; } | /*empty*/ { $$ = false; }
 
 //===----------------------------------------------------------------------===//
 // Types includes all predefined types... except void, because it can only be
@@ -985,7 +988,7 @@ ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
        // TODO: GlobalVariable here that includes the said information!
        
        // Create a placeholder for the global variable reference...
-       GlobalVariable *GV = new GlobalVariable(PT->getValueType(), false);
+       GlobalVariable *GV = new GlobalVariable(PT->getValueType(), false,true);
        // Keep track of the fact that we have a forward ref to recycle it
        CurModule.GlobalRefs.insert(make_pair(make_pair(PT, $2), GV));
 
@@ -1065,14 +1068,14 @@ ConstPool : ConstPool OptAssign CONST ConstVal {
   }
   | ConstPool MethodProto {            // Method prototypes can be in const pool
   }
-  | ConstPool OptAssign GlobalType ConstVal {
-    const Type *Ty = $4->getType();
+  | ConstPool OptAssign OptInternal GlobalType ConstVal {
+    const Type *Ty = $5->getType();
     // Global declarations appear in Constant Pool
-    ConstPoolVal *Initializer = $4;
+    ConstPoolVal *Initializer = $5;
     if (Initializer == 0)
       ThrowException("Global value initializer is not a constant!");
         
-    GlobalVariable *GV = new GlobalVariable(Ty, $3, Initializer);
+    GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
     if (!setValueName(GV, $2)) {   // If not redefining...
       CurModule.CurrentModule->getGlobalList().push_back(GV);
       int Slot = InsertValue(GV, CurModule.Values);
@@ -1085,10 +1088,10 @@ ConstPool : ConstPool OptAssign CONST ConstVal {
       }
     }
   }
-  | ConstPool OptAssign UNINIT GlobalType Types {
-    const Type *Ty = *$5;
+  | ConstPool OptAssign OptInternal UNINIT GlobalType Types {
+    const Type *Ty = *$6;
     // Global declarations appear in Constant Pool
-    GlobalVariable *GV = new GlobalVariable(Ty, $4);
+    GlobalVariable *GV = new GlobalVariable(Ty, $5, $3);
     if (!setValueName(GV, $2)) {   // If not redefining...
       CurModule.CurrentModule->getGlobalList().push_back(GV);
       int Slot = InsertValue(GV, CurModule.Values);
@@ -1167,51 +1170,52 @@ ArgList : ArgListH {
     $$ = 0;
   }
 
-MethodHeaderH : TypesV STRINGCONSTANT '(' ArgList ')' {
-  UnEscapeLexed($2);
+MethodHeaderH : OptInternal TypesV STRINGCONSTANT '(' ArgList ')' {
+  UnEscapeLexed($3);
+  string MethodName($3);
+  
   vector<const Type*> ParamTypeList;
-  if ($4)
-    for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I)
+  if ($5)
+    for (list<MethodArgument*>::iterator I = $5->begin(); I != $5->end(); ++I)
       ParamTypeList.push_back((*I)->getType());
 
   bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
   if (isVarArg) ParamTypeList.pop_back();
 
-  const MethodType  *MT  = MethodType::get(*$1, ParamTypeList, isVarArg);
+  const MethodType  *MT  = MethodType::get(*$2, ParamTypeList, isVarArg);
   const PointerType *PMT = PointerType::get(MT);
-  delete $1;
+  delete $2;
 
   Method *M = 0;
   if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
-    if (Value *V = ST->lookup(PMT, $2)) {  // Method already in symtab?
+    if (Value *V = ST->lookup(PMT, MethodName)) {  // Method already in symtab?
       M = cast<Method>(V);
 
       // Yes it is.  If this is the case, either we need to be a forward decl,
       // or it needs to be.
       if (!CurMeth.isDeclare && !M->isExternal())
-       ThrowException("Redefinition of method '" + string($2) + "'!");      
+       ThrowException("Redefinition of method '" + MethodName + "'!");      
     }
   }
 
   if (M == 0) {  // Not already defined?
-    M = new Method(MT, $2);
+    M = new Method(MT, $1, MethodName);
     InsertValue(M, CurModule.Values);
-    CurModule.DeclareNewGlobalValue(M, ValID::create($2));
+    CurModule.DeclareNewGlobalValue(M, ValID::create($3));
   }
-
-  free($2);  // Free strdup'd memory!
+  free($3);  // Free strdup'd memory!
 
   CurMeth.MethodStart(M);
 
   // Add all of the arguments we parsed to the method...
-  if ($4 && !CurMeth.isDeclare) {        // Is null if empty...
+  if ($5 && !CurMeth.isDeclare) {        // Is null if empty...
     Method::ArgumentListType &ArgList = M->getArgumentList();
 
-    for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I) {
+    for (list<MethodArgument*>::iterator I = $5->begin(); I != $5->end(); ++I) {
       InsertValue(*I);
       ArgList.push_back(*I);
     }
-    delete $4;                     // We're now done with the argument list
+    delete $5;                     // We're now done with the argument list
   }
 }
 
@@ -1356,7 +1360,7 @@ BBTerminatorInst : RET ResolvedVal {              // Return with a result...
       // Pull out the types of all of the arguments...
       vector<const Type*> ParamTypes;
       if ($5) {
-        for (list<Value*>::iterator I = $5->begin(), E = $5->end(); I != E; ++I)
+        for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
           ParamTypes.push_back((*I)->getType());
       }
 
@@ -1385,7 +1389,7 @@ BBTerminatorInst : RET ResolvedVal {              // Return with a result...
       //
       MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
       MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
-      list<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
+      vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
 
       for (; ArgI != ArgE && I != E; ++ArgI, ++I)
        if ((*ArgI)->getType() != *I)
@@ -1395,8 +1399,7 @@ BBTerminatorInst : RET ResolvedVal {              // Return with a result...
       if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
        ThrowException("Invalid number of parameters detected!");
 
-      $$ = new InvokeInst(V, Normal, Except,
-                         vector<Value*>($5->begin(), $5->end()));
+      $$ = new InvokeInst(V, Normal, Except, *$5);
     }
     delete $5;
   }
@@ -1442,7 +1445,7 @@ PHIList : Types '[' ValueRef ',' ValueRef ']' {    // Used for PHI nodes
 
 
 ValueRefList : ResolvedVal {    // Used for call statements, and memory insts...
-    $$ = new list<Value*>();
+    $$ = new vector<Value*>();
     $$->push_back($1);
   }
   | ValueRefList ',' ResolvedVal {
@@ -1493,7 +1496,7 @@ InstVal : BinaryOps Types ValueRef ',' ValueRef {
       // Pull out the types of all of the arguments...
       vector<const Type*> ParamTypes;
       if ($5) {
-        for (list<Value*>::iterator I = $5->begin(), E = $5->end(); I != E; ++I)
+        for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
           ParamTypes.push_back((*I)->getType());
       }
 
@@ -1516,7 +1519,7 @@ InstVal : BinaryOps Types ValueRef ',' ValueRef {
       //
       MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
       MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
-      list<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
+      vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
 
       for (; ArgI != ArgE && I != E; ++ArgI, ++I)
        if ((*ArgI)->getType() != *I)
@@ -1526,7 +1529,7 @@ InstVal : BinaryOps Types ValueRef ',' ValueRef {
       if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
        ThrowException("Invalid number of parameters detected!");
 
-      $$ = new CallInst(V, vector<Value*>($5->begin(), $5->end()));
+      $$ = new CallInst(V, *$5);
     }
     delete $5;
   }
@@ -1534,11 +1537,12 @@ InstVal : BinaryOps Types ValueRef ',' ValueRef {
     $$ = $1;
   }
 
-// UByteList - List of ubyte values for load and store instructions
-UByteList : ',' ConstVector { 
+
+// IndexList - List of indices for GEP based instructions...
+IndexList : ',' ValueRefList { 
   $$ = $2; 
 } | /* empty */ { 
-  $$ = new vector<ConstPoolVal*>(); 
+  $$ = new vector<Value*>(); 
 }
 
 MemoryInst : MALLOC Types {
@@ -1573,7 +1577,7 @@ MemoryInst : MALLOC Types {
     $$ = new FreeInst($2);
   }
 
-  | LOAD Types ValueRef UByteList {
+  | LOAD Types ValueRef IndexList {
     if (!(*$2)->isPointerType())
       ThrowException("Can't load from nonpointer type: " +
                     (*$2)->getDescription());
@@ -1584,7 +1588,7 @@ MemoryInst : MALLOC Types {
     delete $4;   // Free the vector...
     delete $2;
   }
-  | STORE ResolvedVal ',' Types ValueRef UByteList {
+  | STORE ResolvedVal ',' Types ValueRef IndexList {
     if (!(*$4)->isPointerType())
       ThrowException("Can't store to a nonpointer type: " + (*$4)->getName());
     const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
@@ -1596,7 +1600,7 @@ MemoryInst : MALLOC Types {
     $$ = new StoreInst($2, getVal(*$4, $5), *$6);
     delete $4; delete $6;
   }
-  | GETELEMENTPTR Types ValueRef UByteList {
+  | GETELEMENTPTR Types ValueRef IndexList {
     if (!(*$2)->isPointerType())
       ThrowException("getelementptr insn requires pointer operand!");
     if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))