710c674a77109d4e249023aa5be085a8794ff176
[oota-llvm.git] / lib / AsmParser / llvmAsmParser.y
1 //===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file implements the bison parser for LLVM assembly languages files.
11 //
12 //===----------------------------------------------------------------------===//
13
14 %{
15 #include "ParserInternals.h"
16 #include "llvm/SymbolTable.h"
17 #include "llvm/Module.h"
18 #include "llvm/iTerminators.h"
19 #include "llvm/iMemory.h"
20 #include "llvm/iOperators.h"
21 #include "llvm/iPHINode.h"
22 #include "llvm/Support/GetElementPtrTypeIterator.h"
23 #include "Support/STLExtras.h"
24 #include <algorithm>
25 #include <iostream>
26 #include <list>
27 #include <utility>
28
29 int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
30 int yylex();                       // declaration" of xxx warnings.
31 int yyparse();
32
33 namespace llvm {
34   std::string CurFilename;
35 }
36 using namespace llvm;
37
38 static Module *ParserResult;
39
40 // DEBUG_UPREFS - Define this symbol if you want to enable debugging output
41 // relating to upreferences in the input stream.
42 //
43 //#define DEBUG_UPREFS 1
44 #ifdef DEBUG_UPREFS
45 #define UR_OUT(X) std::cerr << X
46 #else
47 #define UR_OUT(X)
48 #endif
49
50 #define YYERROR_VERBOSE 1
51
52 // HACK ALERT: This variable is used to implement the automatic conversion of
53 // variable argument instructions from their old to new forms.  When this
54 // compatiblity "Feature" is removed, this should be too.
55 //
56 static BasicBlock *CurBB;
57 static bool ObsoleteVarArgs;
58
59
60 // This contains info used when building the body of a function.  It is
61 // destroyed when the function is completed.
62 //
63 typedef std::vector<Value *> ValueList;           // Numbered defs
64 static void ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
65                                std::map<const Type *,ValueList> *FutureLateResolvers = 0);
66
67 static struct PerModuleInfo {
68   Module *CurrentModule;
69   std::map<const Type *, ValueList> Values; // Module level numbered definitions
70   std::map<const Type *,ValueList> LateResolveValues;
71   std::vector<PATypeHolder>    Types;
72   std::map<ValID, PATypeHolder> LateResolveTypes;
73
74   /// PlaceHolderInfo - When temporary placeholder objects are created, remember
75   /// how they were referenced and one which line of the input they came from so
76   /// that we can resolve them later and print error messages as appropriate.
77   std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
78
79   // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
80   // references to global values.  Global values may be referenced before they
81   // are defined, and if so, the temporary object that they represent is held
82   // here.  This is used for forward references of ConstantPointerRefs.
83   //
84   typedef std::map<std::pair<const PointerType *,
85                              ValID>, GlobalValue*> GlobalRefsType;
86   GlobalRefsType GlobalRefs;
87
88   void ModuleDone() {
89     // If we could not resolve some functions at function compilation time
90     // (calls to functions before they are defined), resolve them now...  Types
91     // are resolved when the constant pool has been completely parsed.
92     //
93     ResolveDefinitions(LateResolveValues);
94
95     // Check to make sure that all global value forward references have been
96     // resolved!
97     //
98     if (!GlobalRefs.empty()) {
99       std::string UndefinedReferences = "Unresolved global references exist:\n";
100       
101       for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
102            I != E; ++I) {
103         UndefinedReferences += "  " + I->first.first->getDescription() + " " +
104                                I->first.second.getName() + "\n";
105       }
106       ThrowException(UndefinedReferences);
107     }
108
109     Values.clear();         // Clear out function local definitions
110     Types.clear();
111     CurrentModule = 0;
112   }
113
114
115   // GetForwardRefForGlobal - Check to see if there is a forward reference
116   // for this global.  If so, remove it from the GlobalRefs map and return it.
117   // If not, just return null.
118   GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
119     // Check to see if there is a forward reference to this global variable...
120     // if there is, eliminate it and patch the reference to use the new def'n.
121     GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
122     GlobalValue *Ret = 0;
123     if (I != GlobalRefs.end()) {
124       Ret = I->second;
125       GlobalRefs.erase(I);
126     }
127     return Ret;
128   }
129 } CurModule;
130
131 static struct PerFunctionInfo {
132   Function *CurrentFunction;     // Pointer to current function being created
133
134   std::map<const Type*, ValueList> Values;   // Keep track of #'d definitions
135   std::map<const Type*, ValueList> LateResolveValues;
136   std::vector<PATypeHolder> Types;
137   std::map<ValID, PATypeHolder> LateResolveTypes;
138   bool isDeclare;                // Is this function a forward declararation?
139
140   /// BBForwardRefs - When we see forward references to basic blocks, keep
141   /// track of them here.
142   std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
143   std::vector<BasicBlock*> NumberedBlocks;
144   unsigned NextBBNum;
145
146   inline PerFunctionInfo() {
147     CurrentFunction = 0;
148     isDeclare = false;
149   }
150
151   inline void FunctionStart(Function *M) {
152     CurrentFunction = M;
153     NextBBNum = 0;
154   }
155
156   void FunctionDone() {
157     NumberedBlocks.clear();
158
159     // Any forward referenced blocks left?
160     if (!BBForwardRefs.empty())
161       ThrowException("Undefined reference to label " +
162                      BBForwardRefs.begin()->second.first.getName());
163
164     // Resolve all forward references now.
165     ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
166
167     // Make sure to resolve any constant expr references that might exist within
168     // the function we just declared itself.
169     ValID FID;
170     if (CurrentFunction->hasName()) {
171       FID = ValID::create((char*)CurrentFunction->getName().c_str());
172     } else {
173       // Figure out which slot number if is...
174       ValueList &List = CurModule.Values[CurrentFunction->getType()];
175       for (unsigned i = 0; ; ++i) {
176         assert(i < List.size() && "Function not found!");
177         if (List[i] == CurrentFunction) {
178           FID = ValID::create((int)i);
179           break;
180         }
181       }
182     }
183
184     Values.clear();         // Clear out function local definitions
185     Types.clear();          // Clear out function local types
186     CurrentFunction = 0;
187     isDeclare = false;
188   }
189 } CurFun;  // Info for the current function...
190
191 static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
192
193
194 //===----------------------------------------------------------------------===//
195 //               Code to handle definitions of all the types
196 //===----------------------------------------------------------------------===//
197
198 static int InsertValue(Value *V,
199                   std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
200   if (V->hasName()) return -1;           // Is this a numbered definition?
201
202   // Yes, insert the value into the value table...
203   ValueList &List = ValueTab[V->getType()];
204   List.push_back(V);
205   return List.size()-1;
206 }
207
208 static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
209   switch (D.Type) {
210   case ValID::NumberVal:               // Is it a numbered definition?
211     // Module constants occupy the lowest numbered slots...
212     if ((unsigned)D.Num < CurModule.Types.size()) 
213       return CurModule.Types[(unsigned)D.Num];
214     break;
215   case ValID::NameVal:                 // Is it a named definition?
216     if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
217       D.destroy();  // Free old strdup'd memory...
218       return N;
219     }
220     break;
221   default:
222     ThrowException("Internal parser error: Invalid symbol type reference!");
223   }
224
225   // If we reached here, we referenced either a symbol that we don't know about
226   // or an id number that hasn't been read yet.  We may be referencing something
227   // forward, so just create an entry to be resolved later and get to it...
228   //
229   if (DoNotImprovise) return 0;  // Do we just want a null to be returned?
230
231   std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ? 
232     CurFun.LateResolveTypes : CurModule.LateResolveTypes;
233   
234   std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
235   if (I != LateResolver.end()) {
236     return I->second;
237   }
238
239   Type *Typ = OpaqueType::get();
240   LateResolver.insert(std::make_pair(D, Typ));
241   return Typ;
242 }
243
244 static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
245   SymbolTable &SymTab = 
246     inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
247                         CurModule.CurrentModule->getSymbolTable();
248   return SymTab.lookup(Ty, Name);
249 }
250
251 // getValNonImprovising - Look up the value specified by the provided type and
252 // the provided ValID.  If the value exists and has already been defined, return
253 // it.  Otherwise return null.
254 //
255 static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
256   if (isa<FunctionType>(Ty))
257     ThrowException("Functions are not values and "
258                    "must be referenced as pointers");
259
260   switch (D.Type) {
261   case ValID::NumberVal: {                 // Is it a numbered definition?
262     unsigned Num = (unsigned)D.Num;
263
264     // Module constants occupy the lowest numbered slots...
265     std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
266     if (VI != CurModule.Values.end()) {
267       if (Num < VI->second.size()) 
268         return VI->second[Num];
269       Num -= VI->second.size();
270     }
271
272     // Make sure that our type is within bounds
273     VI = CurFun.Values.find(Ty);
274     if (VI == CurFun.Values.end()) return 0;
275
276     // Check that the number is within bounds...
277     if (VI->second.size() <= Num) return 0;
278   
279     return VI->second[Num];
280   }
281
282   case ValID::NameVal: {                // Is it a named definition?
283     Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
284     if (N == 0) return 0;
285
286     D.destroy();  // Free old strdup'd memory...
287     return N;
288   }
289
290   // Check to make sure that "Ty" is an integral type, and that our 
291   // value will fit into the specified type...
292   case ValID::ConstSIntVal:    // Is it a constant pool reference??
293     if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
294       ThrowException("Signed integral constant '" +
295                      itostr(D.ConstPool64) + "' is invalid for type '" + 
296                      Ty->getDescription() + "'!");
297     return ConstantSInt::get(Ty, D.ConstPool64);
298
299   case ValID::ConstUIntVal:     // Is it an unsigned const pool reference?
300     if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
301       if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
302         ThrowException("Integral constant '" + utostr(D.UConstPool64) +
303                        "' is invalid or out of range!");
304       } else {     // This is really a signed reference.  Transmogrify.
305         return ConstantSInt::get(Ty, D.ConstPool64);
306       }
307     } else {
308       return ConstantUInt::get(Ty, D.UConstPool64);
309     }
310
311   case ValID::ConstFPVal:        // Is it a floating point const pool reference?
312     if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
313       ThrowException("FP constant invalid for type!!");
314     return ConstantFP::get(Ty, D.ConstPoolFP);
315     
316   case ValID::ConstNullVal:      // Is it a null value?
317     if (!isa<PointerType>(Ty))
318       ThrowException("Cannot create a a non pointer null!");
319     return ConstantPointerNull::get(cast<PointerType>(Ty));
320     
321   case ValID::ConstantVal:       // Fully resolved constant?
322     if (D.ConstantValue->getType() != Ty)
323       ThrowException("Constant expression type different from required type!");
324     return D.ConstantValue;
325
326   default:
327     assert(0 && "Unhandled case!");
328     return 0;
329   }   // End of switch
330
331   assert(0 && "Unhandled case!");
332   return 0;
333 }
334
335 // getVal - This function is identical to getValNonImprovising, except that if a
336 // value is not already defined, it "improvises" by creating a placeholder var
337 // that looks and acts just like the requested variable.  When the value is
338 // defined later, all uses of the placeholder variable are replaced with the
339 // real thing.
340 //
341 static Value *getVal(const Type *Ty, const ValID &ID) {
342   if (Ty == Type::LabelTy)
343     ThrowException("Cannot use a basic block here");
344
345   // See if the value has already been defined.
346   Value *V = getValNonImprovising(Ty, ID);
347   if (V) return V;
348
349   // If we reached here, we referenced either a symbol that we don't know about
350   // or an id number that hasn't been read yet.  We may be referencing something
351   // forward, so just create an entry to be resolved later and get to it...
352   //
353   V = new Argument(Ty);
354
355   // Remember where this forward reference came from.  FIXME, shouldn't we try
356   // to recycle these things??
357   CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
358                                                                llvmAsmlineno)));
359
360   if (inFunctionScope())
361     InsertValue(V, CurFun.LateResolveValues);
362   else 
363     InsertValue(V, CurModule.LateResolveValues);
364   return V;
365 }
366
367 /// getBBVal - This is used for two purposes:
368 ///  * If isDefinition is true, a new basic block with the specified ID is being
369 ///    defined.
370 ///  * If isDefinition is true, this is a reference to a basic block, which may
371 ///    or may not be a forward reference.
372 ///
373 static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
374   assert(inFunctionScope() && "Can't get basic block at global scope!");
375
376   std::string Name;
377   BasicBlock *BB = 0;
378   switch (ID.Type) {
379   default: ThrowException("Illegal label reference " + ID.getName());
380   case ValID::NumberVal:                // Is it a numbered definition?
381     if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
382       CurFun.NumberedBlocks.resize(ID.Num+1);
383     BB = CurFun.NumberedBlocks[ID.Num];
384     break;
385   case ValID::NameVal:                  // Is it a named definition?
386     Name = ID.Name;
387     if (Value *N = CurFun.CurrentFunction->
388                    getSymbolTable().lookup(Type::LabelTy, Name))
389       BB = cast<BasicBlock>(N);
390     break;
391   }
392
393   // See if the block has already been defined.
394   if (BB) {
395     // If this is the definition of the block, make sure the existing value was
396     // just a forward reference.  If it was a forward reference, there will be
397     // an entry for it in the PlaceHolderInfo map.
398     if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
399       // The existing value was a definition, not a forward reference.
400       ThrowException("Redefinition of label " + ID.getName());
401
402     ID.destroy();                       // Free strdup'd memory.
403     return BB;
404   }
405
406   // Otherwise this block has not been seen before.
407   BB = new BasicBlock("", CurFun.CurrentFunction);
408   if (ID.Type == ValID::NameVal) {
409     BB->setName(ID.Name);
410   } else {
411     CurFun.NumberedBlocks[ID.Num] = BB;
412   }
413
414   // If this is not a definition, keep track of it so we can use it as a forward
415   // reference.
416   if (!isDefinition) {
417     // Remember where this forward reference came from.
418     CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
419   } else {
420     // The forward declaration could have been inserted anywhere in the
421     // function: insert it into the correct place now.
422     CurFun.CurrentFunction->getBasicBlockList().remove(BB);
423     CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
424   }
425
426   return BB;
427 }
428
429
430 //===----------------------------------------------------------------------===//
431 //              Code to handle forward references in instructions
432 //===----------------------------------------------------------------------===//
433 //
434 // This code handles the late binding needed with statements that reference
435 // values not defined yet... for example, a forward branch, or the PHI node for
436 // a loop body.
437 //
438 // This keeps a table (CurFun.LateResolveValues) of all such forward references
439 // and back patchs after we are done.
440 //
441
442 // ResolveDefinitions - If we could not resolve some defs at parsing 
443 // time (forward branches, phi functions for loops, etc...) resolve the 
444 // defs now...
445 //
446 static void ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
447                                std::map<const Type*,ValueList> *FutureLateResolvers) {
448   // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
449   for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
450          E = LateResolvers.end(); LRI != E; ++LRI) {
451     ValueList &List = LRI->second;
452     while (!List.empty()) {
453       Value *V = List.back();
454       List.pop_back();
455
456       std::map<Value*, std::pair<ValID, int> >::iterator PHI =
457         CurModule.PlaceHolderInfo.find(V);
458       assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
459
460       ValID &DID = PHI->second.first;
461
462       Value *TheRealValue = getValNonImprovising(LRI->first, DID);
463       if (TheRealValue) {
464         V->replaceAllUsesWith(TheRealValue);
465         delete V;
466         CurModule.PlaceHolderInfo.erase(PHI);
467       } else if (FutureLateResolvers) {
468         // Functions have their unresolved items forwarded to the module late
469         // resolver table
470         InsertValue(V, *FutureLateResolvers);
471       } else {
472         if (DID.Type == ValID::NameVal)
473           ThrowException("Reference to an invalid definition: '" +DID.getName()+
474                          "' of type '" + V->getType()->getDescription() + "'",
475                          PHI->second.second);
476         else
477           ThrowException("Reference to an invalid definition: #" +
478                          itostr(DID.Num) + " of type '" + 
479                          V->getType()->getDescription() + "'",
480                          PHI->second.second);
481       }
482     }
483   }
484
485   LateResolvers.clear();
486 }
487
488 // ResolveTypeTo - A brand new type was just declared.  This means that (if
489 // name is not null) things referencing Name can be resolved.  Otherwise, things
490 // refering to the number can be resolved.  Do this now.
491 //
492 static void ResolveTypeTo(char *Name, const Type *ToTy) {
493   std::vector<PATypeHolder> &Types = inFunctionScope() ? 
494      CurFun.Types : CurModule.Types;
495
496    ValID D;
497    if (Name) D = ValID::create(Name);
498    else      D = ValID::create((int)Types.size());
499
500    std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ? 
501      CurFun.LateResolveTypes : CurModule.LateResolveTypes;
502   
503    std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
504    if (I != LateResolver.end()) {
505      ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
506      LateResolver.erase(I);
507    }
508 }
509
510 // ResolveTypes - At this point, all types should be resolved.  Any that aren't
511 // are errors.
512 //
513 static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
514   if (!LateResolveTypes.empty()) {
515     const ValID &DID = LateResolveTypes.begin()->first;
516
517     if (DID.Type == ValID::NameVal)
518       ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
519     else
520       ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
521   }
522 }
523
524 // setValueName - Set the specified value to the name given.  The name may be
525 // null potentially, in which case this is a noop.  The string passed in is
526 // assumed to be a malloc'd string buffer, and is free'd by this function.
527 //
528 static void setValueName(Value *V, char *NameStr) {
529   if (NameStr) {
530     std::string Name(NameStr);      // Copy string
531     free(NameStr);                  // Free old string
532
533     if (V->getType() == Type::VoidTy) 
534       ThrowException("Can't assign name '" + Name+"' to value with void type!");
535     
536     assert(inFunctionScope() && "Must be in function scope!");
537     SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
538     if (ST.lookup(V->getType(), Name))
539       ThrowException("Redefinition of value named '" + Name + "' in the '" +
540                      V->getType()->getDescription() + "' type plane!");
541     
542     // Set the name.
543     V->setName(Name, &ST);
544   }
545 }
546
547 /// ParseGlobalVariable - Handle parsing of a global.  If Initializer is null,
548 /// this is a declaration, otherwise it is a definition.
549 static void ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
550                                 bool isConstantGlobal, const Type *Ty,
551                                 Constant *Initializer) {
552   if (isa<FunctionType>(Ty))
553     ThrowException("Cannot declare global vars of function type!");
554
555   const PointerType *PTy = PointerType::get(Ty); 
556
557   std::string Name;
558   if (NameStr) {
559     Name = NameStr;      // Copy string
560     free(NameStr);       // Free old string
561   }
562
563   // See if this global value was forward referenced.  If so, recycle the
564   // object.
565   ValID ID; 
566   if (!Name.empty()) {
567     ID = ValID::create((char*)Name.c_str());
568   } else {
569     ID = ValID::create((int)CurModule.Values[PTy].size());
570   }
571
572   if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
573     // Move the global to the end of the list, from whereever it was 
574     // previously inserted.
575     GlobalVariable *GV = cast<GlobalVariable>(FWGV);
576     CurModule.CurrentModule->getGlobalList().remove(GV);
577     CurModule.CurrentModule->getGlobalList().push_back(GV);
578     GV->setInitializer(Initializer);
579     GV->setLinkage(Linkage);
580     GV->setConstant(isConstantGlobal);
581     return;
582   }
583
584   // If this global has a name, check to see if there is already a definition
585   // of this global in the module.  If so, merge as appropriate.  Note that
586   // this is really just a hack around problems in the CFE.  :(
587   if (!Name.empty()) {
588     // We are a simple redefinition of a value, check to see if it is defined
589     // the same as the old one.
590     if (GlobalVariable *EGV = 
591                 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
592       // We are allowed to redefine a global variable in two circumstances:
593       // 1. If at least one of the globals is uninitialized or 
594       // 2. If both initializers have the same value.
595       //
596       if (!EGV->hasInitializer() || !Initializer ||
597           EGV->getInitializer() == Initializer) {
598
599         // Make sure the existing global version gets the initializer!  Make
600         // sure that it also gets marked const if the new version is.
601         if (Initializer && !EGV->hasInitializer())
602           EGV->setInitializer(Initializer);
603         if (isConstantGlobal)
604           EGV->setConstant(true);
605         EGV->setLinkage(Linkage);
606         return;
607       }
608
609       ThrowException("Redefinition of global variable named '" + Name + 
610                      "' in the '" + Ty->getDescription() + "' type plane!");
611     }
612   }
613
614   // Otherwise there is no existing GV to use, create one now.
615   new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name, 
616                      CurModule.CurrentModule);
617 }
618
619 // setTypeName - Set the specified type to the name given.  The name may be
620 // null potentially, in which case this is a noop.  The string passed in is
621 // assumed to be a malloc'd string buffer, and is freed by this function.
622 //
623 // This function returns true if the type has already been defined, but is
624 // allowed to be redefined in the specified context.  If the name is a new name
625 // for the type plane, it is inserted and false is returned.
626 static bool setTypeName(const Type *T, char *NameStr) {
627   assert(!inFunctionScope() && "Can't give types function-local names!");
628   if (NameStr == 0) return false;
629   
630   std::string Name(NameStr);      // Copy string
631   free(NameStr);                  // Free old string
632
633   // We don't allow assigning names to void type
634   if (T == Type::VoidTy) 
635     ThrowException("Can't assign name '" + Name + "' to the void type!");
636
637   // Set the type name, checking for conflicts as we do so.
638   bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
639
640   if (AlreadyExists) {   // Inserting a name that is already defined???
641     const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
642     assert(Existing && "Conflict but no matching type?");
643
644     // There is only one case where this is allowed: when we are refining an
645     // opaque type.  In this case, Existing will be an opaque type.
646     if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
647       // We ARE replacing an opaque type!
648       const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
649       return true;
650     }
651
652     // Otherwise, this is an attempt to redefine a type. That's okay if
653     // the redefinition is identical to the original. This will be so if
654     // Existing and T point to the same Type object. In this one case we
655     // allow the equivalent redefinition.
656     if (Existing == T) return true;  // Yes, it's equal.
657
658     // Any other kind of (non-equivalent) redefinition is an error.
659     ThrowException("Redefinition of type named '" + Name + "' in the '" +
660                    T->getDescription() + "' type plane!");
661   }
662
663   return false;
664 }
665
666 //===----------------------------------------------------------------------===//
667 // Code for handling upreferences in type names...
668 //
669
670 // TypeContains - Returns true if Ty directly contains E in it.
671 //
672 static bool TypeContains(const Type *Ty, const Type *E) {
673   return find(Ty->subtype_begin(), Ty->subtype_end(), E) != Ty->subtype_end();
674 }
675
676 namespace {
677   struct UpRefRecord {
678     // NestingLevel - The number of nesting levels that need to be popped before
679     // this type is resolved.
680     unsigned NestingLevel;
681     
682     // LastContainedTy - This is the type at the current binding level for the
683     // type.  Every time we reduce the nesting level, this gets updated.
684     const Type *LastContainedTy;
685
686     // UpRefTy - This is the actual opaque type that the upreference is
687     // represented with.
688     OpaqueType *UpRefTy;
689
690     UpRefRecord(unsigned NL, OpaqueType *URTy)
691       : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
692   };
693 }
694
695 // UpRefs - A list of the outstanding upreferences that need to be resolved.
696 static std::vector<UpRefRecord> UpRefs;
697
698 /// HandleUpRefs - Every time we finish a new layer of types, this function is
699 /// called.  It loops through the UpRefs vector, which is a list of the
700 /// currently active types.  For each type, if the up reference is contained in
701 /// the newly completed type, we decrement the level count.  When the level
702 /// count reaches zero, the upreferenced type is the type that is passed in:
703 /// thus we can complete the cycle.
704 ///
705 static PATypeHolder HandleUpRefs(const Type *ty) {
706   if (!ty->isAbstract()) return ty;
707   PATypeHolder Ty(ty);
708   UR_OUT("Type '" << Ty->getDescription() << 
709          "' newly formed.  Resolving upreferences.\n" <<
710          UpRefs.size() << " upreferences active!\n");
711
712   // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
713   // to zero), we resolve them all together before we resolve them to Ty.  At
714   // the end of the loop, if there is anything to resolve to Ty, it will be in
715   // this variable.
716   OpaqueType *TypeToResolve = 0;
717
718   for (unsigned i = 0; i != UpRefs.size(); ++i) {
719     UR_OUT("  UR#" << i << " - TypeContains(" << Ty->getDescription() << ", " 
720            << UpRefs[i].second->getDescription() << ") = " 
721            << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
722     if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
723       // Decrement level of upreference
724       unsigned Level = --UpRefs[i].NestingLevel;
725       UpRefs[i].LastContainedTy = Ty;
726       UR_OUT("  Uplevel Ref Level = " << Level << "\n");
727       if (Level == 0) {                     // Upreference should be resolved! 
728         if (!TypeToResolve) {
729           TypeToResolve = UpRefs[i].UpRefTy;
730         } else {
731           UR_OUT("  * Resolving upreference for "
732                  << UpRefs[i].second->getDescription() << "\n";
733                  std::string OldName = UpRefs[i].UpRefTy->getDescription());
734           UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
735           UR_OUT("  * Type '" << OldName << "' refined upreference to: "
736                  << (const void*)Ty << ", " << Ty->getDescription() << "\n");
737         }
738         UpRefs.erase(UpRefs.begin()+i);     // Remove from upreference list...
739         --i;                                // Do not skip the next element...
740       }
741     }
742   }
743
744   if (TypeToResolve) {
745     UR_OUT("  * Resolving upreference for "
746            << UpRefs[i].second->getDescription() << "\n";
747            std::string OldName = TypeToResolve->getDescription());
748     TypeToResolve->refineAbstractTypeTo(Ty);
749   }
750
751   return Ty;
752 }
753
754
755 //===----------------------------------------------------------------------===//
756 //            RunVMAsmParser - Define an interface to this parser
757 //===----------------------------------------------------------------------===//
758 //
759 Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
760   llvmAsmin = F;
761   CurFilename = Filename;
762   llvmAsmlineno = 1;      // Reset the current line number...
763   ObsoleteVarArgs = false;
764
765   // Allocate a new module to read
766   CurModule.CurrentModule = new Module(Filename);
767
768   yyparse();       // Parse the file, potentially throwing exception
769
770   Module *Result = ParserResult;
771
772   // Check to see if they called va_start but not va_arg..
773   if (!ObsoleteVarArgs)
774     if (Function *F = Result->getNamedFunction("llvm.va_start"))
775       if (F->asize() == 1) {
776         std::cerr << "WARNING: this file uses obsolete features.  "
777                   << "Assemble and disassemble to update it.\n";
778         ObsoleteVarArgs = true;
779       }
780
781   if (ObsoleteVarArgs) {
782     // If the user is making use of obsolete varargs intrinsics, adjust them for
783     // the user.
784     if (Function *F = Result->getNamedFunction("llvm.va_start")) {
785       assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
786
787       const Type *RetTy = F->getFunctionType()->getParamType(0);
788       RetTy = cast<PointerType>(RetTy)->getElementType();
789       Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
790       
791       while (!F->use_empty()) {
792         CallInst *CI = cast<CallInst>(F->use_back());
793         Value *V = new CallInst(NF, "", CI);
794         new StoreInst(V, CI->getOperand(1), CI);
795         CI->getParent()->getInstList().erase(CI);
796       }
797       Result->getFunctionList().erase(F);
798     }
799     
800     if (Function *F = Result->getNamedFunction("llvm.va_end")) {
801       assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
802       const Type *ArgTy = F->getFunctionType()->getParamType(0);
803       ArgTy = cast<PointerType>(ArgTy)->getElementType();
804       Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
805                                                  ArgTy, 0);
806
807       while (!F->use_empty()) {
808         CallInst *CI = cast<CallInst>(F->use_back());
809         Value *V = new LoadInst(CI->getOperand(1), "", CI);
810         new CallInst(NF, V, "", CI);
811         CI->getParent()->getInstList().erase(CI);
812       }
813       Result->getFunctionList().erase(F);
814     }
815
816     if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
817       assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
818       const Type *ArgTy = F->getFunctionType()->getParamType(0);
819       ArgTy = cast<PointerType>(ArgTy)->getElementType();
820       Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
821                                                  ArgTy, 0);
822
823       while (!F->use_empty()) {
824         CallInst *CI = cast<CallInst>(F->use_back());
825         Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
826         new StoreInst(V, CI->getOperand(1), CI);
827         CI->getParent()->getInstList().erase(CI);
828       }
829       Result->getFunctionList().erase(F);
830     }
831   }
832
833   llvmAsmin = stdin;    // F is about to go away, don't use it anymore...
834   ParserResult = 0;
835
836   return Result;
837 }
838
839 %}
840
841 %union {
842   llvm::Module                           *ModuleVal;
843   llvm::Function                         *FunctionVal;
844   std::pair<llvm::PATypeHolder*, char*>  *ArgVal;
845   llvm::BasicBlock                       *BasicBlockVal;
846   llvm::TerminatorInst                   *TermInstVal;
847   llvm::Instruction                      *InstVal;
848   llvm::Constant                         *ConstVal;
849
850   const llvm::Type                       *PrimType;
851   llvm::PATypeHolder                     *TypeVal;
852   llvm::Value                            *ValueVal;
853
854   std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
855   std::vector<llvm::Value*>              *ValueList;
856   std::list<llvm::PATypeHolder>          *TypeList;
857   std::list<std::pair<llvm::Value*,
858                       llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
859   std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
860   std::vector<llvm::Constant*>           *ConstVector;
861
862   llvm::GlobalValue::LinkageTypes         Linkage;
863   int64_t                           SInt64Val;
864   uint64_t                          UInt64Val;
865   int                               SIntVal;
866   unsigned                          UIntVal;
867   double                            FPVal;
868   bool                              BoolVal;
869
870   char                             *StrVal;   // This memory is strdup'd!
871   llvm::ValID                             ValIDVal; // strdup'd memory maybe!
872
873   llvm::Instruction::BinaryOps            BinaryOpVal;
874   llvm::Instruction::TermOps              TermOpVal;
875   llvm::Instruction::MemoryOps            MemOpVal;
876   llvm::Instruction::OtherOps             OtherOpVal;
877   llvm::Module::Endianness                Endianness;
878 }
879
880 %type <ModuleVal>     Module FunctionList
881 %type <FunctionVal>   Function FunctionProto FunctionHeader BasicBlockList
882 %type <BasicBlockVal> BasicBlock InstructionList
883 %type <TermInstVal>   BBTerminatorInst
884 %type <InstVal>       Inst InstVal MemoryInst
885 %type <ConstVal>      ConstVal ConstExpr
886 %type <ConstVector>   ConstVector
887 %type <ArgList>       ArgList ArgListH
888 %type <ArgVal>        ArgVal
889 %type <PHIList>       PHIList
890 %type <ValueList>     ValueRefList ValueRefListE  // For call param lists
891 %type <ValueList>     IndexList                   // For GEP derived indices
892 %type <TypeList>      TypeListI ArgTypeListI
893 %type <JumpTable>     JumpTable
894 %type <BoolVal>       GlobalType                  // GLOBAL or CONSTANT?
895 %type <BoolVal>       OptVolatile                 // 'volatile' or not
896 %type <Linkage>       OptLinkage
897 %type <Endianness>    BigOrLittle
898
899 // ValueRef - Unresolved reference to a definition or BB
900 %type <ValIDVal>      ValueRef ConstValueRef SymbolicValueRef
901 %type <ValueVal>      ResolvedVal            // <type> <valref> pair
902 // Tokens and types for handling constant integer values
903 //
904 // ESINT64VAL - A negative number within long long range
905 %token <SInt64Val> ESINT64VAL
906
907 // EUINT64VAL - A positive number within uns. long long range
908 %token <UInt64Val> EUINT64VAL
909 %type  <SInt64Val> EINT64VAL
910
911 %token  <SIntVal>   SINTVAL   // Signed 32 bit ints...
912 %token  <UIntVal>   UINTVAL   // Unsigned 32 bit ints...
913 %type   <SIntVal>   INTVAL
914 %token  <FPVal>     FPVAL     // Float or Double constant
915
916 // Built in types...
917 %type  <TypeVal> Types TypesV UpRTypes UpRTypesV
918 %type  <PrimType> SIntType UIntType IntType FPType PrimType   // Classifications
919 %token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
920 %token <PrimType> FLOAT DOUBLE TYPE LABEL
921
922 %token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
923 %type  <StrVal> Name OptName OptAssign
924
925
926 %token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
927 %token DECLARE GLOBAL CONSTANT VOLATILE
928 %token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK  APPENDING
929 %token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
930
931 // Basic Block Terminating Operators 
932 %token <TermOpVal> RET BR SWITCH INVOKE UNWIND
933
934 // Binary Operators 
935 %type  <BinaryOpVal> BinaryOps  // all the binary operators
936 %type  <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
937 %token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
938 %token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE  // Binary Comarators
939
940 // Memory Instructions
941 %token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
942
943 // Other Operators
944 %type  <OtherOpVal> ShiftOps
945 %token <OtherOpVal> PHI_TOK CALL CAST SELECT SHL SHR VAARG VANEXT
946 %token VA_ARG // FIXME: OBSOLETE
947
948 %start Module
949 %%
950
951 // Handle constant integer size restriction and conversion...
952 //
953 INTVAL : SINTVAL;
954 INTVAL : UINTVAL {
955   if ($1 > (uint32_t)INT32_MAX)     // Outside of my range!
956     ThrowException("Value too large for type!");
957   $$ = (int32_t)$1;
958 };
959
960
961 EINT64VAL : ESINT64VAL;      // These have same type and can't cause problems...
962 EINT64VAL : EUINT64VAL {
963   if ($1 > (uint64_t)INT64_MAX)     // Outside of my range!
964     ThrowException("Value too large for type!");
965   $$ = (int64_t)$1;
966 };
967
968 // Operations that are notably excluded from this list include: 
969 // RET, BR, & SWITCH because they end basic blocks and are treated specially.
970 //
971 ArithmeticOps: ADD | SUB | MUL | DIV | REM;
972 LogicalOps   : AND | OR | XOR;
973 SetCondOps   : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
974 BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
975
976 ShiftOps  : SHL | SHR;
977
978 // These are some types that allow classification if we only want a particular 
979 // thing... for example, only a signed, unsigned, or integral type.
980 SIntType :  LONG |  INT |  SHORT | SBYTE;
981 UIntType : ULONG | UINT | USHORT | UBYTE;
982 IntType  : SIntType | UIntType;
983 FPType   : FLOAT | DOUBLE;
984
985 // OptAssign - Value producing statements have an optional assignment component
986 OptAssign : Name '=' {
987     $$ = $1;
988   }
989   | /*empty*/ { 
990     $$ = 0; 
991   };
992
993 OptLinkage : INTERNAL  { $$ = GlobalValue::InternalLinkage; } |
994              LINKONCE  { $$ = GlobalValue::LinkOnceLinkage; } |
995              WEAK      { $$ = GlobalValue::WeakLinkage; } |
996              APPENDING { $$ = GlobalValue::AppendingLinkage; } |
997              /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
998
999 //===----------------------------------------------------------------------===//
1000 // Types includes all predefined types... except void, because it can only be
1001 // used in specific contexts (function returning void for example).  To have
1002 // access to it, a user must explicitly use TypesV.
1003 //
1004
1005 // TypesV includes all of 'Types', but it also includes the void type.
1006 TypesV    : Types    | VOID { $$ = new PATypeHolder($1); };
1007 UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
1008
1009 Types     : UpRTypes {
1010     if (!UpRefs.empty())
1011       ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
1012     $$ = $1;
1013   };
1014
1015
1016 // Derived types are added later...
1017 //
1018 PrimType : BOOL | SBYTE | UBYTE | SHORT  | USHORT | INT   | UINT ;
1019 PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE   | LABEL;
1020 UpRTypes : OPAQUE {
1021     $$ = new PATypeHolder(OpaqueType::get());
1022   }
1023   | PrimType {
1024     $$ = new PATypeHolder($1);
1025   };
1026 UpRTypes : SymbolicValueRef {            // Named types are also simple types...
1027   $$ = new PATypeHolder(getTypeVal($1));
1028 };
1029
1030 // Include derived types in the Types production.
1031 //
1032 UpRTypes : '\\' EUINT64VAL {                   // Type UpReference
1033     if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
1034     OpaqueType *OT = OpaqueType::get();        // Use temporary placeholder
1035     UpRefs.push_back(UpRefRecord((unsigned)$2, OT));  // Add to vector...
1036     $$ = new PATypeHolder(OT);
1037     UR_OUT("New Upreference!\n");
1038   }
1039   | UpRTypesV '(' ArgTypeListI ')' {           // Function derived type?
1040     std::vector<const Type*> Params;
1041     mapto($3->begin(), $3->end(), std::back_inserter(Params), 
1042           std::mem_fun_ref(&PATypeHolder::get));
1043     bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1044     if (isVarArg) Params.pop_back();
1045
1046     $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
1047     delete $3;      // Delete the argument list
1048     delete $1;      // Delete the return type handle
1049   }
1050   | '[' EUINT64VAL 'x' UpRTypes ']' {          // Sized array type?
1051     $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1052     delete $4;
1053   }
1054   | '{' TypeListI '}' {                        // Structure type?
1055     std::vector<const Type*> Elements;
1056     mapto($2->begin(), $2->end(), std::back_inserter(Elements), 
1057         std::mem_fun_ref(&PATypeHolder::get));
1058
1059     $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
1060     delete $2;
1061   }
1062   | '{' '}' {                                  // Empty structure type?
1063     $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
1064   }
1065   | UpRTypes '*' {                             // Pointer type?
1066     $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1067     delete $1;
1068   };
1069
1070 // TypeList - Used for struct declarations and as a basis for function type 
1071 // declaration type lists
1072 //
1073 TypeListI : UpRTypes {
1074     $$ = new std::list<PATypeHolder>();
1075     $$->push_back(*$1); delete $1;
1076   }
1077   | TypeListI ',' UpRTypes {
1078     ($$=$1)->push_back(*$3); delete $3;
1079   };
1080
1081 // ArgTypeList - List of types for a function type declaration...
1082 ArgTypeListI : TypeListI
1083   | TypeListI ',' DOTDOTDOT {
1084     ($$=$1)->push_back(Type::VoidTy);
1085   }
1086   | DOTDOTDOT {
1087     ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
1088   }
1089   | /*empty*/ {
1090     $$ = new std::list<PATypeHolder>();
1091   };
1092
1093 // ConstVal - The various declarations that go into the constant pool.  This
1094 // production is used ONLY to represent constants that show up AFTER a 'const',
1095 // 'constant' or 'global' token at global scope.  Constants that can be inlined
1096 // into other expressions (such as integers and constexprs) are handled by the
1097 // ResolvedVal, ValueRef and ConstValueRef productions.
1098 //
1099 ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
1100     const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
1101     if (ATy == 0)
1102       ThrowException("Cannot make array constant with type: '" + 
1103                      (*$1)->getDescription() + "'!");
1104     const Type *ETy = ATy->getElementType();
1105     int NumElements = ATy->getNumElements();
1106
1107     // Verify that we have the correct size...
1108     if (NumElements != -1 && NumElements != (int)$3->size())
1109       ThrowException("Type mismatch: constant sized array initialized with " +
1110                      utostr($3->size()) +  " arguments, but has size of " + 
1111                      itostr(NumElements) + "!");
1112
1113     // Verify all elements are correct type!
1114     for (unsigned i = 0; i < $3->size(); i++) {
1115       if (ETy != (*$3)[i]->getType())
1116         ThrowException("Element #" + utostr(i) + " is not of type '" + 
1117                        ETy->getDescription() +"' as required!\nIt is of type '"+
1118                        (*$3)[i]->getType()->getDescription() + "'.");
1119     }
1120
1121     $$ = ConstantArray::get(ATy, *$3);
1122     delete $1; delete $3;
1123   }
1124   | Types '[' ']' {
1125     const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
1126     if (ATy == 0)
1127       ThrowException("Cannot make array constant with type: '" + 
1128                      (*$1)->getDescription() + "'!");
1129
1130     int NumElements = ATy->getNumElements();
1131     if (NumElements != -1 && NumElements != 0) 
1132       ThrowException("Type mismatch: constant sized array initialized with 0"
1133                      " arguments, but has size of " + itostr(NumElements) +"!");
1134     $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1135     delete $1;
1136   }
1137   | Types 'c' STRINGCONSTANT {
1138     const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
1139     if (ATy == 0)
1140       ThrowException("Cannot make array constant with type: '" + 
1141                      (*$1)->getDescription() + "'!");
1142
1143     int NumElements = ATy->getNumElements();
1144     const Type *ETy = ATy->getElementType();
1145     char *EndStr = UnEscapeLexed($3, true);
1146     if (NumElements != -1 && NumElements != (EndStr-$3))
1147       ThrowException("Can't build string constant of size " + 
1148                      itostr((int)(EndStr-$3)) +
1149                      " when array has size " + itostr(NumElements) + "!");
1150     std::vector<Constant*> Vals;
1151     if (ETy == Type::SByteTy) {
1152       for (char *C = $3; C != EndStr; ++C)
1153         Vals.push_back(ConstantSInt::get(ETy, *C));
1154     } else if (ETy == Type::UByteTy) {
1155       for (char *C = $3; C != EndStr; ++C)
1156         Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
1157     } else {
1158       free($3);
1159       ThrowException("Cannot build string arrays of non byte sized elements!");
1160     }
1161     free($3);
1162     $$ = ConstantArray::get(ATy, Vals);
1163     delete $1;
1164   }
1165   | Types '{' ConstVector '}' {
1166     const StructType *STy = dyn_cast<StructType>($1->get());
1167     if (STy == 0)
1168       ThrowException("Cannot make struct constant with type: '" + 
1169                      (*$1)->getDescription() + "'!");
1170
1171     if ($3->size() != STy->getNumContainedTypes())
1172       ThrowException("Illegal number of initializers for structure type!");
1173
1174     // Check to ensure that constants are compatible with the type initializer!
1175     for (unsigned i = 0, e = $3->size(); i != e; ++i)
1176       if ((*$3)[i]->getType() != STy->getElementType(i))
1177         ThrowException("Expected type '" +
1178                        STy->getElementType(i)->getDescription() +
1179                        "' for element #" + utostr(i) +
1180                        " of structure initializer!");
1181
1182     $$ = ConstantStruct::get(STy, *$3);
1183     delete $1; delete $3;
1184   }
1185   | Types '{' '}' {
1186     const StructType *STy = dyn_cast<StructType>($1->get());
1187     if (STy == 0)
1188       ThrowException("Cannot make struct constant with type: '" + 
1189                      (*$1)->getDescription() + "'!");
1190
1191     if (STy->getNumContainedTypes() != 0)
1192       ThrowException("Illegal number of initializers for structure type!");
1193
1194     $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1195     delete $1;
1196   }
1197   | Types NULL_TOK {
1198     const PointerType *PTy = dyn_cast<PointerType>($1->get());
1199     if (PTy == 0)
1200       ThrowException("Cannot make null pointer constant with type: '" + 
1201                      (*$1)->getDescription() + "'!");
1202
1203     $$ = ConstantPointerNull::get(PTy);
1204     delete $1;
1205   }
1206   | Types SymbolicValueRef {
1207     const PointerType *Ty = dyn_cast<PointerType>($1->get());
1208     if (Ty == 0)
1209       ThrowException("Global const reference must be a pointer type!");
1210
1211     // ConstExprs can exist in the body of a function, thus creating
1212     // ConstantPointerRefs whenever they refer to a variable.  Because we are in
1213     // the context of a function, getValNonImprovising will search the functions
1214     // symbol table instead of the module symbol table for the global symbol,
1215     // which throws things all off.  To get around this, we just tell
1216     // getValNonImprovising that we are at global scope here.
1217     //
1218     Function *SavedCurFn = CurFun.CurrentFunction;
1219     CurFun.CurrentFunction = 0;
1220
1221     Value *V = getValNonImprovising(Ty, $2);
1222
1223     CurFun.CurrentFunction = SavedCurFn;
1224
1225     // If this is an initializer for a constant pointer, which is referencing a
1226     // (currently) undefined variable, create a stub now that shall be replaced
1227     // in the future with the right type of variable.
1228     //
1229     if (V == 0) {
1230       assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1231       const PointerType *PT = cast<PointerType>(Ty);
1232
1233       // First check to see if the forward references value is already created!
1234       PerModuleInfo::GlobalRefsType::iterator I =
1235         CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1236     
1237       if (I != CurModule.GlobalRefs.end()) {
1238         V = I->second;             // Placeholder already exists, use it...
1239         $2.destroy();
1240       } else {
1241         std::string Name;
1242         if ($2.Type == ValID::NameVal) Name = $2.Name;
1243
1244         // Create the forward referenced global.
1245         GlobalValue *GV;
1246         if (const FunctionType *FTy = 
1247                  dyn_cast<FunctionType>(PT->getElementType())) {
1248           GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1249                             CurModule.CurrentModule);
1250         } else {
1251           GV = new GlobalVariable(PT->getElementType(), false,
1252                                   GlobalValue::ExternalLinkage, 0,
1253                                   Name, CurModule.CurrentModule);
1254         }
1255
1256         // Keep track of the fact that we have a forward ref to recycle it
1257         CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1258         V = GV;
1259       }
1260     }
1261
1262     GlobalValue *GV = cast<GlobalValue>(V);
1263     $$ = ConstantPointerRef::get(GV);
1264     delete $1;            // Free the type handle
1265   }
1266   | Types ConstExpr {
1267     if ($1->get() != $2->getType())
1268       ThrowException("Mismatched types for constant expression!");
1269     $$ = $2;
1270     delete $1;
1271   }
1272   | Types ZEROINITIALIZER {
1273     $$ = Constant::getNullValue($1->get());
1274     delete $1;
1275   };
1276
1277 ConstVal : SIntType EINT64VAL {      // integral constants
1278     if (!ConstantSInt::isValueValidForType($1, $2))
1279       ThrowException("Constant value doesn't fit in type!");
1280     $$ = ConstantSInt::get($1, $2);
1281   }
1282   | UIntType EUINT64VAL {            // integral constants
1283     if (!ConstantUInt::isValueValidForType($1, $2))
1284       ThrowException("Constant value doesn't fit in type!");
1285     $$ = ConstantUInt::get($1, $2);
1286   }
1287   | BOOL TRUETOK {                      // Boolean constants
1288     $$ = ConstantBool::True;
1289   }
1290   | BOOL FALSETOK {                     // Boolean constants
1291     $$ = ConstantBool::False;
1292   }
1293   | FPType FPVAL {                   // Float & Double constants
1294     $$ = ConstantFP::get($1, $2);
1295   };
1296
1297
1298 ConstExpr: CAST '(' ConstVal TO Types ')' {
1299     if (!$3->getType()->isFirstClassType())
1300       ThrowException("cast constant expression from a non-primitive type: '" +
1301                      $3->getType()->getDescription() + "'!");
1302     if (!$5->get()->isFirstClassType())
1303       ThrowException("cast constant expression to a non-primitive type: '" +
1304                      $5->get()->getDescription() + "'!");
1305     $$ = ConstantExpr::getCast($3, $5->get());
1306     delete $5;
1307   }
1308   | GETELEMENTPTR '(' ConstVal IndexList ')' {
1309     if (!isa<PointerType>($3->getType()))
1310       ThrowException("GetElementPtr requires a pointer operand!");
1311
1312     // LLVM 1.2 and earlier used ubyte struct indices.  Convert any ubyte struct
1313     // indices to uint struct indices for compatibility.
1314     generic_gep_type_iterator<std::vector<Value*>::iterator>
1315       GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1316       GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1317     for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1318       if (isa<StructType>(*GTI))        // Only change struct indices
1319         if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1320           if (CUI->getType() == Type::UByteTy)
1321             (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1322
1323     const Type *IdxTy =
1324       GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
1325     if (!IdxTy)
1326       ThrowException("Index list invalid for constant getelementptr!");
1327
1328     std::vector<Constant*> IdxVec;
1329     for (unsigned i = 0, e = $4->size(); i != e; ++i)
1330       if (Constant *C = dyn_cast<Constant>((*$4)[i]))
1331         IdxVec.push_back(C);
1332       else
1333         ThrowException("Indices to constant getelementptr must be constants!");
1334
1335     delete $4;
1336
1337     $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
1338   }
1339   | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1340     if ($3->getType() != Type::BoolTy)
1341       ThrowException("Select condition must be of boolean type!");
1342     if ($5->getType() != $7->getType())
1343       ThrowException("Select operand types must match!");
1344     $$ = ConstantExpr::getSelect($3, $5, $7);
1345   }
1346   | BinaryOps '(' ConstVal ',' ConstVal ')' {
1347     if ($3->getType() != $5->getType())
1348       ThrowException("Binary operator types must match!");
1349     $$ = ConstantExpr::get($1, $3, $5);
1350   }
1351   | ShiftOps '(' ConstVal ',' ConstVal ')' {
1352     if ($5->getType() != Type::UByteTy)
1353       ThrowException("Shift count for shift constant must be unsigned byte!");
1354     if (!$3->getType()->isInteger())
1355       ThrowException("Shift constant expression requires integer operand!");
1356     $$ = ConstantExpr::get($1, $3, $5);
1357   };
1358
1359
1360 // ConstVector - A list of comma separated constants.
1361 ConstVector : ConstVector ',' ConstVal {
1362     ($$ = $1)->push_back($3);
1363   }
1364   | ConstVal {
1365     $$ = new std::vector<Constant*>();
1366     $$->push_back($1);
1367   };
1368
1369
1370 // GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1371 GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
1372
1373
1374 //===----------------------------------------------------------------------===//
1375 //                             Rules to match Modules
1376 //===----------------------------------------------------------------------===//
1377
1378 // Module rule: Capture the result of parsing the whole file into a result
1379 // variable...
1380 //
1381 Module : FunctionList {
1382   $$ = ParserResult = $1;
1383   CurModule.ModuleDone();
1384 };
1385
1386 // FunctionList - A list of functions, preceeded by a constant pool.
1387 //
1388 FunctionList : FunctionList Function {
1389     $$ = $1;
1390     CurFun.FunctionDone();
1391   } 
1392   | FunctionList FunctionProto {
1393     $$ = $1;
1394   }
1395   | FunctionList IMPLEMENTATION {
1396     $$ = $1;
1397   }
1398   | ConstPool {
1399     $$ = CurModule.CurrentModule;
1400     // Resolve circular types before we parse the body of the module
1401     ResolveTypes(CurModule.LateResolveTypes);
1402   };
1403
1404 // ConstPool - Constants with optional names assigned to them.
1405 ConstPool : ConstPool OptAssign TYPE TypesV {  // Types can be defined in the const pool
1406     // Eagerly resolve types.  This is not an optimization, this is a
1407     // requirement that is due to the fact that we could have this:
1408     //
1409     // %list = type { %list * }
1410     // %list = type { %list * }    ; repeated type decl
1411     //
1412     // If types are not resolved eagerly, then the two types will not be
1413     // determined to be the same type!
1414     //
1415     ResolveTypeTo($2, *$4);
1416
1417     if (!setTypeName(*$4, $2) && !$2) {
1418       // If this is a named type that is not a redefinition, add it to the slot
1419       // table.
1420       if (inFunctionScope())
1421         CurFun.Types.push_back(*$4);
1422       else
1423         CurModule.Types.push_back(*$4);
1424     }
1425
1426     delete $4;
1427   }
1428   | ConstPool FunctionProto {       // Function prototypes can be in const pool
1429   }
1430   | ConstPool OptAssign OptLinkage GlobalType ConstVal {
1431     if ($5 == 0) ThrowException("Global value initializer is not a constant!");
1432     ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
1433   }
1434   | ConstPool OptAssign EXTERNAL GlobalType Types {
1435     ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, *$5, 0);
1436     delete $5;
1437   }
1438   | ConstPool TARGET TargetDefinition { 
1439   }
1440   | /* empty: end of list */ { 
1441   };
1442
1443
1444
1445 BigOrLittle : BIG    { $$ = Module::BigEndian; };
1446 BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1447
1448 TargetDefinition : ENDIAN '=' BigOrLittle {
1449     CurModule.CurrentModule->setEndianness($3);
1450   }
1451   | POINTERSIZE '=' EUINT64VAL {
1452     if ($3 == 32)
1453       CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1454     else if ($3 == 64)
1455       CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1456     else
1457       ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1458   };
1459
1460
1461 //===----------------------------------------------------------------------===//
1462 //                       Rules to match Function Headers
1463 //===----------------------------------------------------------------------===//
1464
1465 Name : VAR_ID | STRINGCONSTANT;
1466 OptName : Name | /*empty*/ { $$ = 0; };
1467
1468 ArgVal : Types OptName {
1469   if (*$1 == Type::VoidTy)
1470     ThrowException("void typed arguments are invalid!");
1471   $$ = new std::pair<PATypeHolder*, char*>($1, $2);
1472 };
1473
1474 ArgListH : ArgListH ',' ArgVal {
1475     $$ = $1;
1476     $1->push_back(*$3);
1477     delete $3;
1478   }
1479   | ArgVal {
1480     $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1481     $$->push_back(*$1);
1482     delete $1;
1483   };
1484
1485 ArgList : ArgListH {
1486     $$ = $1;
1487   }
1488   | ArgListH ',' DOTDOTDOT {
1489     $$ = $1;
1490     $$->push_back(std::pair<PATypeHolder*,
1491                             char*>(new PATypeHolder(Type::VoidTy), 0));
1492   }
1493   | DOTDOTDOT {
1494     $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1495     $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
1496   }
1497   | /* empty */ {
1498     $$ = 0;
1499   };
1500
1501 FunctionHeaderH : TypesV Name '(' ArgList ')' {
1502   UnEscapeLexed($2);
1503   std::string FunctionName($2);
1504   free($2);  // Free strdup'd memory!
1505   
1506   if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1507     ThrowException("LLVM functions cannot return aggregate types!");
1508
1509   std::vector<const Type*> ParamTypeList;
1510   if ($4) {   // If there are arguments...
1511     for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
1512          I != $4->end(); ++I)
1513       ParamTypeList.push_back(I->first->get());
1514   }
1515
1516   bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1517   if (isVarArg) ParamTypeList.pop_back();
1518
1519   const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
1520   const PointerType *PFT = PointerType::get(FT);
1521   delete $1;
1522
1523   ValID ID;
1524   if (!FunctionName.empty()) {
1525     ID = ValID::create((char*)FunctionName.c_str());
1526   } else {
1527     ID = ValID::create((int)CurModule.Values[PFT].size());
1528   }
1529
1530   Function *Fn = 0;
1531   // See if this function was forward referenced.  If so, recycle the object.
1532   if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
1533     // Move the function to the end of the list, from whereever it was 
1534     // previously inserted.
1535     Fn = cast<Function>(FWRef);
1536     CurModule.CurrentModule->getFunctionList().remove(Fn);
1537     CurModule.CurrentModule->getFunctionList().push_back(Fn);
1538   } else if (!FunctionName.empty() &&     // Merge with an earlier prototype?
1539              (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1540     // If this is the case, either we need to be a forward decl, or it needs 
1541     // to be.
1542     if (!CurFun.isDeclare && !Fn->isExternal())
1543       ThrowException("Redefinition of function '" + FunctionName + "'!");
1544     
1545     // Make sure to strip off any argument names so we can't get conflicts.
1546     if (Fn->isExternal())
1547       for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend();
1548            AI != AE; ++AI)
1549         AI->setName("");
1550
1551   } else  {  // Not already defined?
1552     Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1553                       CurModule.CurrentModule);
1554     InsertValue(Fn, CurModule.Values);
1555   }
1556
1557   CurFun.FunctionStart(Fn);
1558
1559   // Add all of the arguments we parsed to the function...
1560   if ($4) {                     // Is null if empty...
1561     if (isVarArg) {  // Nuke the last entry
1562       assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
1563              "Not a varargs marker!");
1564       delete $4->back().first;
1565       $4->pop_back();  // Delete the last entry
1566     }
1567     Function::aiterator ArgIt = Fn->abegin();
1568     for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
1569          I != $4->end(); ++I, ++ArgIt) {
1570       delete I->first;                          // Delete the typeholder...
1571
1572       setValueName(ArgIt, I->second);           // Insert arg into symtab...
1573       InsertValue(ArgIt);
1574     }
1575
1576     delete $4;                     // We're now done with the argument list
1577   }
1578 };
1579
1580 BEGIN : BEGINTOK | '{';                // Allow BEGIN or '{' to start a function
1581
1582 FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
1583   $$ = CurFun.CurrentFunction;
1584
1585   // Make sure that we keep track of the linkage type even if there was a
1586   // previous "declare".
1587   $$->setLinkage($1);
1588
1589   // Resolve circular types before we parse the body of the function.
1590   ResolveTypes(CurFun.LateResolveTypes);
1591 };
1592
1593 END : ENDTOK | '}';                    // Allow end of '}' to end a function
1594
1595 Function : BasicBlockList END {
1596   $$ = $1;
1597 };
1598
1599 FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1600   $$ = CurFun.CurrentFunction;
1601   CurFun.FunctionDone();
1602 };
1603
1604 //===----------------------------------------------------------------------===//
1605 //                        Rules to match Basic Blocks
1606 //===----------------------------------------------------------------------===//
1607
1608 ConstValueRef : ESINT64VAL {    // A reference to a direct constant
1609     $$ = ValID::create($1);
1610   }
1611   | EUINT64VAL {
1612     $$ = ValID::create($1);
1613   }
1614   | FPVAL {                     // Perhaps it's an FP constant?
1615     $$ = ValID::create($1);
1616   }
1617   | TRUETOK {
1618     $$ = ValID::create(ConstantBool::True);
1619   } 
1620   | FALSETOK {
1621     $$ = ValID::create(ConstantBool::False);
1622   }
1623   | NULL_TOK {
1624     $$ = ValID::createNull();
1625   }
1626   | ConstExpr {
1627     $$ = ValID::create($1);
1628   };
1629
1630 // SymbolicValueRef - Reference to one of two ways of symbolically refering to
1631 // another value.
1632 //
1633 SymbolicValueRef : INTVAL {  // Is it an integer reference...?
1634     $$ = ValID::create($1);
1635   }
1636   | Name {                   // Is it a named reference...?
1637     $$ = ValID::create($1);
1638   };
1639
1640 // ValueRef - A reference to a definition... either constant or symbolic
1641 ValueRef : SymbolicValueRef | ConstValueRef;
1642
1643
1644 // ResolvedVal - a <type> <value> pair.  This is used only in cases where the
1645 // type immediately preceeds the value reference, and allows complex constant
1646 // pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1647 ResolvedVal : Types ValueRef {
1648     $$ = getVal(*$1, $2); delete $1;
1649   };
1650
1651 BasicBlockList : BasicBlockList BasicBlock {
1652     $$ = $1;
1653   }
1654   | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks   
1655     $$ = $1;
1656   };
1657
1658
1659 // Basic blocks are terminated by branching instructions: 
1660 // br, br/cc, switch, ret
1661 //
1662 BasicBlock : InstructionList OptAssign BBTerminatorInst  {
1663     setValueName($3, $2);
1664     InsertValue($3);
1665
1666     $1->getInstList().push_back($3);
1667     InsertValue($1);
1668     $$ = $1;
1669   };
1670
1671 InstructionList : InstructionList Inst {
1672     $1->getInstList().push_back($2);
1673     $$ = $1;
1674   }
1675   | /* empty */ {
1676     $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
1677   }
1678   | LABELSTR {
1679     $$ = CurBB = getBBVal(ValID::create($1), true);
1680   };
1681
1682 BBTerminatorInst : RET ResolvedVal {              // Return with a result...
1683     $$ = new ReturnInst($2);
1684   }
1685   | RET VOID {                                       // Return with no result...
1686     $$ = new ReturnInst();
1687   }
1688   | BR LABEL ValueRef {                         // Unconditional Branch...
1689     $$ = new BranchInst(getBBVal($3));
1690   }                                                  // Conditional Branch...
1691   | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {  
1692     $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
1693   }
1694   | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1695     SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6));
1696     $$ = S;
1697
1698     std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
1699       E = $8->end();
1700     for (; I != E; ++I)
1701       S->addCase(I->first, I->second);
1702     delete $8;
1703   }
1704   | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1705     SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6));
1706     $$ = S;
1707   }
1708   | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO LABEL ValueRef
1709     UNWIND LABEL ValueRef {
1710     const PointerType *PFTy;
1711     const FunctionType *Ty;
1712
1713     if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1714         !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
1715       // Pull out the types of all of the arguments...
1716       std::vector<const Type*> ParamTypes;
1717       if ($5) {
1718         for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1719              I != E; ++I)
1720           ParamTypes.push_back((*I)->getType());
1721       }
1722
1723       bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1724       if (isVarArg) ParamTypes.pop_back();
1725
1726       Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
1727       PFTy = PointerType::get(Ty);
1728     }
1729
1730     Value *V = getVal(PFTy, $3);   // Get the function we're calling...
1731
1732     BasicBlock *Normal = getBBVal($9);
1733     BasicBlock *Except = getBBVal($12);
1734
1735     // Create the call node...
1736     if (!$5) {                                   // Has no arguments?
1737       $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
1738     } else {                                     // Has arguments?
1739       // Loop through FunctionType's arguments and ensure they are specified
1740       // correctly!
1741       //
1742       FunctionType::param_iterator I = Ty->param_begin();
1743       FunctionType::param_iterator E = Ty->param_end();
1744       std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1745
1746       for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1747         if ((*ArgI)->getType() != *I)
1748           ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1749                          (*I)->getDescription() + "'!");
1750
1751       if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1752         ThrowException("Invalid number of parameters detected!");
1753
1754       $$ = new InvokeInst(V, Normal, Except, *$5);
1755     }
1756     delete $2;
1757     delete $5;
1758   }
1759   | UNWIND {
1760     $$ = new UnwindInst();
1761   };
1762
1763
1764
1765 JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1766     $$ = $1;
1767     Constant *V = cast<Constant>(getValNonImprovising($2, $3));
1768     if (V == 0)
1769       ThrowException("May only switch on a constant pool value!");
1770
1771     $$->push_back(std::make_pair(V, getBBVal($6)));
1772   }
1773   | IntType ConstValueRef ',' LABEL ValueRef {
1774     $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
1775     Constant *V = cast<Constant>(getValNonImprovising($1, $2));
1776
1777     if (V == 0)
1778       ThrowException("May only switch on a constant pool value!");
1779
1780     $$->push_back(std::make_pair(V, getBBVal($5)));
1781   };
1782
1783 Inst : OptAssign InstVal {
1784   // Is this definition named?? if so, assign the name...
1785   setValueName($2, $1);
1786   InsertValue($2);
1787   $$ = $2;
1788 };
1789
1790 PHIList : Types '[' ValueRef ',' ValueRef ']' {    // Used for PHI nodes
1791     $$ = new std::list<std::pair<Value*, BasicBlock*> >();
1792     $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
1793     delete $1;
1794   }
1795   | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1796     $$ = $1;
1797     $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
1798                                  getBBVal($6)));
1799   };
1800
1801
1802 ValueRefList : ResolvedVal {    // Used for call statements, and memory insts...
1803     $$ = new std::vector<Value*>();
1804     $$->push_back($1);
1805   }
1806   | ValueRefList ',' ResolvedVal {
1807     $$ = $1;
1808     $1->push_back($3);
1809   };
1810
1811 // ValueRefListE - Just like ValueRefList, except that it may also be empty!
1812 ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
1813
1814 InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1815     if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1816       ThrowException("Arithmetic operator requires integer or FP operands!");
1817     $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1818     if ($$ == 0)
1819       ThrowException("binary operator returned null!");
1820     delete $2;
1821   }
1822   | LogicalOps Types ValueRef ',' ValueRef {
1823     if (!(*$2)->isIntegral())
1824       ThrowException("Logical operator requires integral operands!");
1825     $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1826     if ($$ == 0)
1827       ThrowException("binary operator returned null!");
1828     delete $2;
1829   }
1830   | SetCondOps Types ValueRef ',' ValueRef {
1831     $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
1832     if ($$ == 0)
1833       ThrowException("binary operator returned null!");
1834     delete $2;
1835   }
1836   | NOT ResolvedVal {
1837     std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1838               << " Replacing with 'xor'.\n";
1839
1840     Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1841     if (Ones == 0)
1842       ThrowException("Expected integral type for not instruction!");
1843
1844     $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
1845     if ($$ == 0)
1846       ThrowException("Could not create a xor instruction!");
1847   }
1848   | ShiftOps ResolvedVal ',' ResolvedVal {
1849     if ($4->getType() != Type::UByteTy)
1850       ThrowException("Shift amount must be ubyte!");
1851     if (!$2->getType()->isInteger())
1852       ThrowException("Shift constant expression requires integer operand!");
1853     $$ = new ShiftInst($1, $2, $4);
1854   }
1855   | CAST ResolvedVal TO Types {
1856     if (!$4->get()->isFirstClassType())
1857       ThrowException("cast instruction to a non-primitive type: '" +
1858                      $4->get()->getDescription() + "'!");
1859     $$ = new CastInst($2, *$4);
1860     delete $4;
1861   }
1862   | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1863     if ($2->getType() != Type::BoolTy)
1864       ThrowException("select condition must be boolean!");
1865     if ($4->getType() != $6->getType())
1866       ThrowException("select value types should match!");
1867     $$ = new SelectInst($2, $4, $6);
1868   }
1869   | VA_ARG ResolvedVal ',' Types {
1870     // FIXME: This is emulation code for an obsolete syntax.  This should be
1871     // removed at some point.
1872     if (!ObsoleteVarArgs) {
1873       std::cerr << "WARNING: this file uses obsolete features.  "
1874                 << "Assemble and disassemble to update it.\n";
1875       ObsoleteVarArgs = true;
1876     }
1877
1878     // First, load the valist...
1879     Instruction *CurVAList = new LoadInst($2, "");
1880     CurBB->getInstList().push_back(CurVAList);
1881
1882     // Emit the vaarg instruction.
1883     $$ = new VAArgInst(CurVAList, *$4);
1884     
1885     // Now we must advance the pointer and update it in memory.
1886     Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1887     CurBB->getInstList().push_back(TheVANext);
1888
1889     CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1890     delete $4;
1891   }
1892   | VAARG ResolvedVal ',' Types {
1893     $$ = new VAArgInst($2, *$4);
1894     delete $4;
1895   }
1896   | VANEXT ResolvedVal ',' Types {
1897     $$ = new VANextInst($2, *$4);
1898     delete $4;
1899   }
1900   | PHI_TOK PHIList {
1901     const Type *Ty = $2->front().first->getType();
1902     if (!Ty->isFirstClassType())
1903       ThrowException("PHI node operands must be of first class type!");
1904     $$ = new PHINode(Ty);
1905     $$->op_reserve($2->size()*2);
1906     while ($2->begin() != $2->end()) {
1907       if ($2->front().first->getType() != Ty) 
1908         ThrowException("All elements of a PHI node must be of the same type!");
1909       cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
1910       $2->pop_front();
1911     }
1912     delete $2;  // Free the list...
1913   } 
1914   | CALL TypesV ValueRef '(' ValueRefListE ')' {
1915     const PointerType *PFTy;
1916     const FunctionType *Ty;
1917
1918     if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1919         !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
1920       // Pull out the types of all of the arguments...
1921       std::vector<const Type*> ParamTypes;
1922       if ($5) {
1923         for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1924              I != E; ++I)
1925           ParamTypes.push_back((*I)->getType());
1926       }
1927
1928       bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1929       if (isVarArg) ParamTypes.pop_back();
1930
1931       Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
1932       PFTy = PointerType::get(Ty);
1933     }
1934
1935     Value *V = getVal(PFTy, $3);   // Get the function we're calling...
1936
1937     // Create the call node...
1938     if (!$5) {                                   // Has no arguments?
1939       // Make sure no arguments is a good thing!
1940       if (Ty->getNumParams() != 0)
1941         ThrowException("No arguments passed to a function that "
1942                        "expects arguments!");
1943
1944       $$ = new CallInst(V, std::vector<Value*>());
1945     } else {                                     // Has arguments?
1946       // Loop through FunctionType's arguments and ensure they are specified
1947       // correctly!
1948       //
1949       FunctionType::param_iterator I = Ty->param_begin();
1950       FunctionType::param_iterator E = Ty->param_end();
1951       std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1952
1953       for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1954         if ((*ArgI)->getType() != *I)
1955           ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1956                          (*I)->getDescription() + "'!");
1957
1958       if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1959         ThrowException("Invalid number of parameters detected!");
1960
1961       $$ = new CallInst(V, *$5);
1962     }
1963     delete $2;
1964     delete $5;
1965   }
1966   | MemoryInst {
1967     $$ = $1;
1968   };
1969
1970
1971 // IndexList - List of indices for GEP based instructions...
1972 IndexList : ',' ValueRefList { 
1973     $$ = $2; 
1974   } | /* empty */ { 
1975     $$ = new std::vector<Value*>(); 
1976   };
1977
1978 OptVolatile : VOLATILE {
1979     $$ = true;
1980   }
1981   | /* empty */ {
1982     $$ = false;
1983   };
1984
1985
1986 MemoryInst : MALLOC Types {
1987     $$ = new MallocInst(*$2);
1988     delete $2;
1989   }
1990   | MALLOC Types ',' UINT ValueRef {
1991     $$ = new MallocInst(*$2, getVal($4, $5));
1992     delete $2;
1993   }
1994   | ALLOCA Types {
1995     $$ = new AllocaInst(*$2);
1996     delete $2;
1997   }
1998   | ALLOCA Types ',' UINT ValueRef {
1999     $$ = new AllocaInst(*$2, getVal($4, $5));
2000     delete $2;
2001   }
2002   | FREE ResolvedVal {
2003     if (!isa<PointerType>($2->getType()))
2004       ThrowException("Trying to free nonpointer type " + 
2005                      $2->getType()->getDescription() + "!");
2006     $$ = new FreeInst($2);
2007   }
2008
2009   | OptVolatile LOAD Types ValueRef {
2010     if (!isa<PointerType>($3->get()))
2011       ThrowException("Can't load from nonpointer type: " +
2012                      (*$3)->getDescription());
2013     $$ = new LoadInst(getVal(*$3, $4), "", $1);
2014     delete $3;
2015   }
2016   | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2017     const PointerType *PT = dyn_cast<PointerType>($5->get());
2018     if (!PT)
2019       ThrowException("Can't store to a nonpointer type: " +
2020                      (*$5)->getDescription());
2021     const Type *ElTy = PT->getElementType();
2022     if (ElTy != $3->getType())
2023       ThrowException("Can't store '" + $3->getType()->getDescription() +
2024                      "' into space of type '" + ElTy->getDescription() + "'!");
2025
2026     $$ = new StoreInst($3, getVal(*$5, $6), $1);
2027     delete $5;
2028   }
2029   | GETELEMENTPTR Types ValueRef IndexList {
2030     if (!isa<PointerType>($2->get()))
2031       ThrowException("getelementptr insn requires pointer operand!");
2032
2033     // LLVM 1.2 and earlier used ubyte struct indices.  Convert any ubyte struct
2034     // indices to uint struct indices for compatibility.
2035     generic_gep_type_iterator<std::vector<Value*>::iterator>
2036       GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2037       GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2038     for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2039       if (isa<StructType>(*GTI))        // Only change struct indices
2040         if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2041           if (CUI->getType() == Type::UByteTy)
2042             (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2043
2044     if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
2045       ThrowException("Invalid getelementptr indices for type '" +
2046                      (*$2)->getDescription()+ "'!");
2047     $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2048     delete $2; delete $4;
2049   };
2050
2051
2052 %%
2053 int yyerror(const char *ErrorMsg) {
2054   std::string where 
2055     = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2056                   + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
2057   std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
2058   if (yychar == YYEMPTY || yychar == 0)
2059     errMsg += "end-of-file.";
2060   else
2061     errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
2062   ThrowException(errMsg);
2063   return 0;
2064 }