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