Implement global variable support
[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 // TODO: Parse comments and add them to an internal node... so that they may
9 // be saved in the bytecode format as well as everything else.  Very important
10 // for a general IR format.
11 //
12
13 %{
14 #include "ParserInternals.h"
15 #include "llvm/Assembly/Parser.h"
16 #include "llvm/SymbolTable.h"
17 #include "llvm/Module.h"
18 #include "llvm/GlobalVariable.h"
19 #include "llvm/Method.h"
20 #include "llvm/BasicBlock.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/iTerminators.h"
23 #include "llvm/iMemory.h"
24 #include "llvm/CFG.h"         // TODO: Change this when we have a DF.h
25 #include "llvm/Support/STLExtras.h"
26 #include <list>
27 #include <utility>            // Get definition of pair class
28 #include <algorithm>
29 #include <stdio.h>            // This embarasment is due to our flex lexer...
30
31 int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit 
32 int yylex();                       // declaration" of xxx warnings.
33 int yyparse();
34
35 static Module *ParserResult;
36 string CurFilename;
37
38 // DEBUG_UPREFS - Define this symbol if you want to enable debugging output
39 // relating to upreferences in the input stream.
40 //
41 //#define DEBUG_UPREFS 1
42 #ifdef DEBUG_UPREFS
43 #define UR_OUT(X) cerr << X
44 #else
45 #define UR_OUT(X)
46 #endif
47
48 // This contains info used when building the body of a method.  It is destroyed
49 // when the method is completed.
50 //
51 typedef vector<Value *> ValueList;           // Numbered defs
52 static void ResolveDefinitions(vector<ValueList> &LateResolvers);
53 static void ResolveTypes      (vector<PATypeHolder<Type> > &LateResolveTypes);
54
55 static struct PerModuleInfo {
56   Module *CurrentModule;
57   vector<ValueList>    Values;     // Module level numbered definitions
58   vector<ValueList>    LateResolveValues;
59   vector<PATypeHolder<Type> > Types, LateResolveTypes;
60
61   void ModuleDone() {
62     // If we could not resolve some methods at method compilation time (calls to
63     // methods before they are defined), resolve them now...  Types are resolved
64     // when the constant pool has been completely parsed.
65     //
66     ResolveDefinitions(LateResolveValues);
67
68     Values.clear();         // Clear out method local definitions
69     Types.clear();
70     CurrentModule = 0;
71   }
72 } CurModule;
73
74 static struct PerMethodInfo {
75   Method *CurrentMethod;         // Pointer to current method being created
76
77   vector<ValueList> Values;      // Keep track of numbered definitions
78   vector<ValueList> LateResolveValues;
79   vector<PATypeHolder<Type> > Types, LateResolveTypes;
80   bool isDeclare;                // Is this method a forward declararation?
81
82   inline PerMethodInfo() {
83     CurrentMethod = 0;
84     isDeclare = false;
85   }
86
87   inline ~PerMethodInfo() {}
88
89   inline void MethodStart(Method *M) {
90     CurrentMethod = M;
91   }
92
93   void MethodDone() {
94     // If we could not resolve some blocks at parsing time (forward branches)
95     // resolve the branches now...
96     ResolveDefinitions(LateResolveValues);
97
98     Values.clear();         // Clear out method local definitions
99     Types.clear();
100     CurrentMethod = 0;
101     isDeclare = false;
102   }
103 } CurMeth;  // Info for the current method...
104
105
106 //===----------------------------------------------------------------------===//
107 //               Code to handle definitions of all the types
108 //===----------------------------------------------------------------------===//
109
110 static void InsertValue(Value *D, vector<ValueList> &ValueTab = CurMeth.Values){
111   if (!D->hasName()) {             // Is this a numbered definition?
112     unsigned type = D->getType()->getUniqueID();
113     if (ValueTab.size() <= type)
114       ValueTab.resize(type+1, ValueList());
115     //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
116     ValueTab[type].push_back(D);
117   }
118 }
119
120 // TODO: FIXME when Type are not const
121 static void InsertType(const Type *Ty, vector<PATypeHolder<Type> > &Types) {
122   Types.push_back(Ty);
123 }
124
125 static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
126   switch (D.Type) {
127   case 0: {                 // Is it a numbered definition?
128     unsigned Num = (unsigned)D.Num;
129
130     // Module constants occupy the lowest numbered slots...
131     if (Num < CurModule.Types.size()) 
132       return CurModule.Types[Num];
133
134     Num -= CurModule.Types.size();
135
136     // Check that the number is within bounds...
137     if (Num <= CurMeth.Types.size())
138       return CurMeth.Types[Num];
139   }
140   case 1: {                // Is it a named definition?
141     string Name(D.Name);
142     SymbolTable *SymTab = 0;
143     if (CurMeth.CurrentMethod) 
144       SymTab = CurMeth.CurrentMethod->getSymbolTable();
145     Value *N = SymTab ? SymTab->lookup(Type::TypeTy, Name) : 0;
146
147     if (N == 0) {
148       // Symbol table doesn't automatically chain yet... because the method
149       // hasn't been added to the module...
150       //
151       SymTab = CurModule.CurrentModule->getSymbolTable();
152       if (SymTab)
153         N = SymTab->lookup(Type::TypeTy, Name);
154       if (N == 0) break;
155     }
156
157     D.destroy();  // Free old strdup'd memory...
158     return N->castTypeAsserting();
159   }
160   default:
161     ThrowException("Invalid symbol type reference!");
162   }
163
164   // If we reached here, we referenced either a symbol that we don't know about
165   // or an id number that hasn't been read yet.  We may be referencing something
166   // forward, so just create an entry to be resolved later and get to it...
167   //
168   if (DoNotImprovise) return 0;  // Do we just want a null to be returned?
169
170   vector<PATypeHolder<Type> > *LateResolver = CurMeth.CurrentMethod ? 
171     &CurMeth.LateResolveTypes : &CurModule.LateResolveTypes;
172
173   Type *Typ = new TypePlaceHolder(Type::TypeTy, D);
174   InsertType(Typ, *LateResolver);
175   return Typ;
176 }
177
178 static Value *getVal(const Type *Ty, const ValID &D, 
179                      bool DoNotImprovise = false) {
180   assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
181
182   switch (D.Type) {
183   case 0: {                 // Is it a numbered definition?
184     unsigned type = Ty->getUniqueID();
185     unsigned Num = (unsigned)D.Num;
186
187     // Module constants occupy the lowest numbered slots...
188     if (type < CurModule.Values.size()) {
189       if (Num < CurModule.Values[type].size()) 
190         return CurModule.Values[type][Num];
191
192       Num -= CurModule.Values[type].size();
193     }
194
195     // Make sure that our type is within bounds
196     if (CurMeth.Values.size() <= type)
197       break;
198
199     // Check that the number is within bounds...
200     if (CurMeth.Values[type].size() <= Num)
201       break;
202   
203     return CurMeth.Values[type][Num];
204   }
205   case 1: {                // Is it a named definition?
206     string Name(D.Name);
207     SymbolTable *SymTab = 0;
208     if (CurMeth.CurrentMethod) 
209       SymTab = CurMeth.CurrentMethod->getSymbolTable();
210     Value *N = SymTab ? SymTab->lookup(Ty, Name) : 0;
211
212     if (N == 0) {
213       // Symbol table doesn't automatically chain yet... because the method
214       // hasn't been added to the module...
215       //
216       SymTab = CurModule.CurrentModule->getSymbolTable();
217       if (SymTab)
218         N = SymTab->lookup(Ty, Name);
219       if (N == 0) break;
220     }
221
222     D.destroy();  // Free old strdup'd memory...
223     return N;
224   }
225
226   case 2:                 // Is it a constant pool reference??
227   case 3:                 // Is it an unsigned const pool reference?
228   case 4:                 // Is it a string const pool reference?
229   case 5:{                // Is it a floating point const pool reference?
230     ConstPoolVal *CPV = 0;
231
232     // Check to make sure that "Ty" is an integral type, and that our 
233     // value will fit into the specified type...
234     switch (D.Type) {
235     case 2:
236       if (Ty == Type::BoolTy) {  // Special handling for boolean data
237         CPV = ConstPoolBool::get(D.ConstPool64 != 0);
238       } else {
239         if (!ConstPoolSInt::isValueValidForType(Ty, D.ConstPool64))
240           ThrowException("Symbolic constant pool value '" +
241                          itostr(D.ConstPool64) + "' is invalid for type '" + 
242                          Ty->getName() + "'!");
243         CPV = ConstPoolSInt::get(Ty, D.ConstPool64);
244       }
245       break;
246     case 3:
247       if (!ConstPoolUInt::isValueValidForType(Ty, D.UConstPool64)) {
248         if (!ConstPoolSInt::isValueValidForType(Ty, D.ConstPool64)) {
249           ThrowException("Integral constant pool reference is invalid!");
250         } else {     // This is really a signed reference.  Transmogrify.
251           CPV = ConstPoolSInt::get(Ty, D.ConstPool64);
252         }
253       } else {
254         CPV = ConstPoolUInt::get(Ty, D.UConstPool64);
255       }
256       break;
257     case 4:
258       cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n";
259       abort();
260       break;
261     case 5:
262       if (!ConstPoolFP::isValueValidForType(Ty, D.ConstPoolFP))
263         ThrowException("FP constant invalid for type!!");
264       else
265         CPV = ConstPoolFP::get(Ty, D.ConstPoolFP);
266       break;
267     }
268     assert(CPV && "How did we escape creating a constant??");
269     return CPV;
270   }   // End of case 2,3,4
271   default:
272     assert(0 && "Unhandled case!");
273   }   // End of switch
274
275
276   // If we reached here, we referenced either a symbol that we don't know about
277   // or an id number that hasn't been read yet.  We may be referencing something
278   // forward, so just create an entry to be resolved later and get to it...
279   //
280   if (DoNotImprovise) return 0;  // Do we just want a null to be returned?
281
282   Value *d = 0;
283   vector<ValueList> *LateResolver =  (CurMeth.CurrentMethod) ? 
284     &CurMeth.LateResolveValues : &CurModule.LateResolveValues;
285
286   switch (Ty->getPrimitiveID()) {
287   case Type::LabelTyID:  d = new   BBPlaceHolder(Ty, D); break;
288   case Type::MethodTyID: d = new MethPlaceHolder(Ty, D); 
289                          LateResolver = &CurModule.LateResolveValues; break;
290   default:               d = new ValuePlaceHolder(Ty, D); break;
291   }
292
293   assert(d != 0 && "How did we not make something?");
294   InsertValue(d, *LateResolver);
295   return d;
296 }
297
298
299 //===----------------------------------------------------------------------===//
300 //              Code to handle forward references in instructions
301 //===----------------------------------------------------------------------===//
302 //
303 // This code handles the late binding needed with statements that reference
304 // values not defined yet... for example, a forward branch, or the PHI node for
305 // a loop body.
306 //
307 // This keeps a table (CurMeth.LateResolveValues) of all such forward references
308 // and back patchs after we are done.
309 //
310
311 // ResolveDefinitions - If we could not resolve some defs at parsing 
312 // time (forward branches, phi functions for loops, etc...) resolve the 
313 // defs now...
314 //
315 static void ResolveDefinitions(vector<ValueList> &LateResolvers) {
316   // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
317   for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
318     while (!LateResolvers[ty].empty()) {
319       Value *V = LateResolvers[ty].back();
320       LateResolvers[ty].pop_back();
321       ValID &DID = getValIDFromPlaceHolder(V);
322
323       Value *TheRealValue = getVal(Type::getUniqueIDType(ty), DID, true);
324
325       if (TheRealValue == 0) {
326         if (DID.Type == 1)
327           ThrowException("Reference to an invalid definition: '" +DID.getName()+
328                          "' of type '" + V->getType()->getDescription() + "'",
329                          getLineNumFromPlaceHolder(V));
330         else
331           ThrowException("Reference to an invalid definition: #" +
332                          itostr(DID.Num) + " of type '" + 
333                          V->getType()->getDescription() + "'",
334                          getLineNumFromPlaceHolder(V));
335       }
336
337       assert(!V->isType() && "Types should be in LateResolveTypes!");
338
339       V->replaceAllUsesWith(TheRealValue);
340       delete V;
341     }
342   }
343
344   LateResolvers.clear();
345 }
346
347
348 // ResolveTypes - This goes through the forward referenced type table and makes
349 // sure that all type references are complete.  This code is executed after the
350 // constant pool of a method or module is completely parsed.
351 //
352 static void ResolveTypes(vector<PATypeHolder<Type> > &LateResolveTypes) {
353   while (!LateResolveTypes.empty()) {
354     const Type *Ty = LateResolveTypes.back();
355     ValID &DID = getValIDFromPlaceHolder(Ty);
356
357     const Type *TheRealType = getTypeVal(DID, true);
358     if (TheRealType == 0) {
359       if (DID.Type == 1)
360         ThrowException("Reference to an invalid type: '" +DID.getName(),
361                        getLineNumFromPlaceHolder(Ty));
362       else
363         ThrowException("Reference to an invalid type: #" + itostr(DID.Num),
364                        getLineNumFromPlaceHolder(Ty));
365     }
366
367     // FIXME: When types are not const
368     DerivedType *DTy = const_cast<DerivedType*>(Ty->castDerivedTypeAsserting());
369     
370     // Refine the opaque type we had to the new type we are getting.
371     DTy->refineAbstractTypeTo(TheRealType);
372
373     // No need to delete type, refine does that for us.
374     LateResolveTypes.pop_back();
375   }
376 }
377
378 static void setValueName(Value *V, const string &Name) {
379   SymbolTable *ST = CurMeth.CurrentMethod ? 
380     CurMeth.CurrentMethod->getSymbolTableSure() : 
381     CurModule.CurrentModule->getSymbolTableSure();
382
383   Value *Existing = ST->lookup(V->getType(), Name);
384   if (Existing) {    // Inserting a name that is already defined???
385     // There is only one case where this is allowed: when we are refining an
386     // opaque type.  In this case, Existing will be an opaque type.
387     if (const Type *Ty = Existing->castType())
388       if (Ty->isOpaqueType()) {
389         // We ARE replacing an opaque type!
390
391         // TODO: FIXME when types are not const!
392         const_cast<DerivedType*>(Ty->castDerivedTypeAsserting())->refineAbstractTypeTo(V->castTypeAsserting());
393         return;
394       }
395
396     // Otherwise, we are a simple redefinition of a value, baaad
397     ThrowException("Redefinition of value name '" + Name + "' in the '" +
398                    V->getType()->getDescription() + "' type plane!");
399   }
400
401   V->setName(Name, ST);
402 }
403
404
405 //===----------------------------------------------------------------------===//
406 // Code for handling upreferences in type names...
407 //
408
409 // TypeContains - Returns true if Ty contains E in it.
410 //
411 static bool TypeContains(const Type *Ty, const Type *E) {
412   return find(cfg::tdf_begin(Ty), cfg::tdf_end(Ty), E) != cfg::tdf_end(Ty);
413 }
414
415
416 static vector<pair<unsigned, OpaqueType *> > UpRefs;
417
418 static PATypeHolder<Type> HandleUpRefs(const Type *ty) {
419   PATypeHolder<Type> Ty(ty);
420   UR_OUT(UpRefs.size() << " upreferences active!\n");
421   for (unsigned i = 0; i < UpRefs.size(); ) {
422     UR_OUT("TypeContains(" << Ty->getDescription() << ", " 
423            << UpRefs[i].second->getDescription() << ") = " 
424            << TypeContains(Ty, UpRefs[i].second) << endl);
425     if (TypeContains(Ty, UpRefs[i].second)) {
426       unsigned Level = --UpRefs[i].first;   // Decrement level of upreference
427       UR_OUT("Uplevel Ref Level = " << Level << endl);
428       if (Level == 0) {                     // Upreference should be resolved! 
429         UR_OUT("About to resolve upreference!\n";
430                string OldName = UpRefs[i].second->getDescription());
431         UpRefs[i].second->refineAbstractTypeTo(Ty);
432         UpRefs.erase(UpRefs.begin()+i);     // Remove from upreference list...
433         UR_OUT("Type '" << OldName << "' refined upreference to: "
434                << (const void*)Ty << ", " << Ty->getDescription() << endl);
435         continue;
436       }
437     }
438
439     ++i;                                  // Otherwise, no resolve, move on...
440   }
441   // FIXME: TODO: this should return the updated type
442   return Ty;
443 }
444
445 template <class TypeTy>
446 inline static void TypeDone(PATypeHolder<TypeTy> *Ty) {
447   if (UpRefs.size())
448     ThrowException("Invalid upreference in type: " + (*Ty)->getDescription());
449 }
450
451 // newTH - Allocate a new type holder for the specified type
452 template <class TypeTy>
453 inline static PATypeHolder<TypeTy> *newTH(const TypeTy *Ty) {
454   return new PATypeHolder<TypeTy>(Ty);
455 }
456 template <class TypeTy>
457 inline static PATypeHolder<TypeTy> *newTH(const PATypeHolder<TypeTy> &TH) {
458   return new PATypeHolder<TypeTy>(TH);
459 }
460
461
462 // newTHC - Allocate a new type holder for the specified type that can be
463 // casted to a new Type type.
464 template <class TypeTy, class OldTy>
465 inline static PATypeHolder<TypeTy> *newTHC(const PATypeHolder<OldTy> &Old) {
466   return new PATypeHolder<TypeTy>((const TypeTy*)Old.get());
467 }
468
469
470 //===----------------------------------------------------------------------===//
471 //            RunVMAsmParser - Define an interface to this parser
472 //===----------------------------------------------------------------------===//
473 //
474 Module *RunVMAsmParser(const string &Filename, FILE *F) {
475   llvmAsmin = F;
476   CurFilename = Filename;
477   llvmAsmlineno = 1;      // Reset the current line number...
478
479   CurModule.CurrentModule = new Module();  // Allocate a new module to read
480   yyparse();       // Parse the file.
481   Module *Result = ParserResult;
482   llvmAsmin = stdin;    // F is about to go away, don't use it anymore...
483   ParserResult = 0;
484
485   return Result;
486 }
487
488 %}
489
490 %union {
491   Module                           *ModuleVal;
492   Method                           *MethodVal;
493   MethodArgument                   *MethArgVal;
494   BasicBlock                       *BasicBlockVal;
495   TerminatorInst                   *TermInstVal;
496   Instruction                      *InstVal;
497   ConstPoolVal                     *ConstVal;
498
499   const Type                       *PrimType;
500   PATypeHolder<Type>               *TypeVal;
501   PATypeHolder<ArrayType>          *ArrayTypeTy;
502   PATypeHolder<StructType>         *StructTypeTy;
503   Value                            *ValueVal;
504
505   list<MethodArgument*>            *MethodArgList;
506   list<Value*>                     *ValueList;
507   list<PATypeHolder<Type> >        *TypeList;
508   list<pair<Value*, BasicBlock*> > *PHIList;   // Represent the RHS of PHI node
509   list<pair<ConstPoolVal*, BasicBlock*> > *JumpTable;
510   vector<ConstPoolVal*>            *ConstVector;
511
512   int64_t                           SInt64Val;
513   uint64_t                          UInt64Val;
514   int                               SIntVal;
515   unsigned                          UIntVal;
516   double                            FPVal;
517
518   char                             *StrVal;   // This memory is strdup'd!
519   ValID                             ValIDVal; // strdup'd memory maybe!
520
521   Instruction::UnaryOps             UnaryOpVal;
522   Instruction::BinaryOps            BinaryOpVal;
523   Instruction::TermOps              TermOpVal;
524   Instruction::MemoryOps            MemOpVal;
525   Instruction::OtherOps             OtherOpVal;
526 }
527
528 %type <ModuleVal>     Module MethodList
529 %type <MethodVal>     Method MethodProto MethodHeader BasicBlockList
530 %type <BasicBlockVal> BasicBlock InstructionList
531 %type <TermInstVal>   BBTerminatorInst
532 %type <InstVal>       Inst InstVal MemoryInst
533 %type <ConstVal>      ConstVal ExtendedConstVal
534 %type <ConstVector>   ConstVector UByteList
535 %type <MethodArgList> ArgList ArgListH
536 %type <MethArgVal>    ArgVal
537 %type <PHIList>       PHIList
538 %type <ValueList>     ValueRefList ValueRefListE  // For call param lists
539 %type <TypeList>      TypeListI ArgTypeListI
540 %type <JumpTable>     JumpTable
541
542 %type <ValIDVal>      ValueRef ConstValueRef // Reference to a definition or BB
543 %type <ValueVal>      ResolvedVal            // <type> <valref> pair
544 // Tokens and types for handling constant integer values
545 //
546 // ESINT64VAL - A negative number within long long range
547 %token <SInt64Val> ESINT64VAL
548
549 // EUINT64VAL - A positive number within uns. long long range
550 %token <UInt64Val> EUINT64VAL
551 %type  <SInt64Val> EINT64VAL
552
553 %token  <SIntVal>   SINTVAL   // Signed 32 bit ints...
554 %token  <UIntVal>   UINTVAL   // Unsigned 32 bit ints...
555 %type   <SIntVal>   INTVAL
556 %token  <FPVal>     FPVAL     // Float or Double constant
557
558 // Built in types...
559 %type  <TypeVal> Types TypesV UpRTypes UpRTypesV
560 %type  <PrimType> SIntType UIntType IntType FPType PrimType   // Classifications
561 %token <TypeVal>  OPAQUE
562 %token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
563 %token <PrimType> FLOAT DOUBLE TYPE LABEL
564 %type  <ArrayTypeTy> ArrayType ArrayTypeI
565 %type  <StructTypeTy> StructType StructTypeI
566
567 %token <StrVal>     VAR_ID LABELSTR STRINGCONSTANT
568 %type  <StrVal>  OptVAR_ID OptAssign
569
570
571 %token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE GLOBAL TO DOTDOTDOT STRING
572
573 // Basic Block Terminating Operators 
574 %token <TermOpVal> RET BR SWITCH
575
576 // Unary Operators 
577 %type  <UnaryOpVal> UnaryOps  // all the unary operators
578 %token <UnaryOpVal> NOT
579
580 // Binary Operators 
581 %type  <BinaryOpVal> BinaryOps  // all the binary operators
582 %token <BinaryOpVal> ADD SUB MUL DIV REM
583 %token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE  // Binary Comarators
584
585 // Memory Instructions
586 %token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
587
588 // Other Operators
589 %type  <OtherOpVal> ShiftOps
590 %token <OtherOpVal> PHI CALL CAST SHL SHR
591
592 %start Module
593 %%
594
595 // Handle constant integer size restriction and conversion...
596 //
597
598 INTVAL : SINTVAL
599 INTVAL : UINTVAL {
600   if ($1 > (uint32_t)INT32_MAX)     // Outside of my range!
601     ThrowException("Value too large for type!");
602   $$ = (int32_t)$1;
603 }
604
605
606 EINT64VAL : ESINT64VAL       // These have same type and can't cause problems...
607 EINT64VAL : EUINT64VAL {
608   if ($1 > (uint64_t)INT64_MAX)     // Outside of my range!
609     ThrowException("Value too large for type!");
610   $$ = (int64_t)$1;
611 }
612
613 // Operations that are notably excluded from this list include: 
614 // RET, BR, & SWITCH because they end basic blocks and are treated specially.
615 //
616 UnaryOps  : NOT
617 BinaryOps : ADD | SUB | MUL | DIV | REM
618 BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
619 ShiftOps  : SHL | SHR
620
621 // These are some types that allow classification if we only want a particular 
622 // thing... for example, only a signed, unsigned, or integral type.
623 SIntType :  LONG |  INT |  SHORT | SBYTE
624 UIntType : ULONG | UINT | USHORT | UBYTE
625 IntType  : SIntType | UIntType
626 FPType   : FLOAT | DOUBLE
627
628 // OptAssign - Value producing statements have an optional assignment component
629 OptAssign : VAR_ID '=' {
630     $$ = $1;
631   }
632   | /*empty*/ { 
633     $$ = 0; 
634   }
635
636
637 //===----------------------------------------------------------------------===//
638 // Types includes all predefined types... except void, because it can only be
639 // used in specific contexts (method returning void for example).  To have
640 // access to it, a user must explicitly use TypesV.
641 //
642
643 // TypesV includes all of 'Types', but it also includes the void type.
644 TypesV    : Types    | VOID { $$ = newTH($1); }
645 UpRTypesV : UpRTypes | VOID { $$ = newTH($1); }
646
647 Types     : UpRTypes {
648     TypeDone($$ = $1);
649   }
650
651
652 // Derived types are added later...
653 //
654 PrimType : BOOL | SBYTE | UBYTE | SHORT  | USHORT | INT   | UINT 
655 PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE   | LABEL
656 UpRTypes : OPAQUE | PrimType { $$ = newTH($1); }
657 UpRTypes : ValueRef {                    // Named types are also simple types...
658   $$ = newTH(getTypeVal($1));
659 }
660
661 // ArrayTypeI - Internal version of ArrayType that can have incomplete uprefs
662 //
663 ArrayTypeI : '[' UpRTypesV ']' {               // Unsized array type?
664     $$ = newTHC<ArrayType>(HandleUpRefs(ArrayType::get(*$2)));
665     delete $2;
666   }
667   | '[' EUINT64VAL 'x' UpRTypes ']' {          // Sized array type?
668     $$ = newTHC<ArrayType>(HandleUpRefs(ArrayType::get(*$4, (int)$2)));
669     delete $4;
670   }
671
672 StructTypeI : '{' TypeListI '}' {              // Structure type?
673     vector<const Type*> Elements;
674     mapto($2->begin(), $2->end(), back_inserter(Elements), 
675         mem_fun_ref(&PATypeHandle<Type>::get));
676
677     $$ = newTHC<StructType>(HandleUpRefs(StructType::get(Elements)));
678     delete $2;
679   }
680   | '{' '}' {                                  // Empty structure type?
681     $$ = newTH(StructType::get(vector<const Type*>()));
682   }
683
684
685 // Include derived types in the Types production.
686 //
687 UpRTypes : '\\' EUINT64VAL {                   // Type UpReference
688     if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
689     OpaqueType *OT = OpaqueType::get();        // Use temporary placeholder
690     UpRefs.push_back(make_pair((unsigned)$2, OT));  // Add to vector...
691     $$ = newTH<Type>(OT);
692     UR_OUT("New Upreference!\n");
693   }
694   | UpRTypesV '(' ArgTypeListI ')' {           // Method derived type?
695     vector<const Type*> Params;
696     mapto($3->begin(), $3->end(), back_inserter(Params), 
697           mem_fun_ref(&PATypeHandle<Type>::get));
698     $$ = newTH(HandleUpRefs(MethodType::get(*$1, Params)));
699     delete $3;      // Delete the argument list
700     delete $1;      // Delete the old type handle
701   }
702   | ArrayTypeI {                               // [Un]sized array type?
703     $$ = newTHC<Type>(*$1); delete $1;
704   }
705   | StructTypeI {                              // Structure type?
706     $$ = newTHC<Type>(*$1); delete $1;
707   }
708   | UpRTypes '*' {                             // Pointer type?
709     $$ = newTH(HandleUpRefs(PointerType::get(*$1)));
710     delete $1;  // Delete the type handle
711   }
712
713 // Define some helpful top level types that do not allow UpReferences to escape
714 //
715 ArrayType  : ArrayTypeI  { TypeDone($$ = $1); }
716 StructType : StructTypeI { TypeDone($$ = $1); }
717
718
719
720 // TypeList - Used for struct declarations and as a basis for method type 
721 // declaration type lists
722 //
723 TypeListI : UpRTypes {
724     $$ = new list<PATypeHolder<Type> >();
725     $$->push_back(*$1); delete $1;
726   }
727   | TypeListI ',' UpRTypes {
728     ($$=$1)->push_back(*$3); delete $3;
729   }
730
731 // ArgTypeList - List of types for a method type declaration...
732 ArgTypeListI : TypeListI
733   | TypeListI ',' DOTDOTDOT {
734     ($$=$1)->push_back(Type::VoidTy);
735   }
736   | DOTDOTDOT {
737     ($$ = new list<PATypeHolder<Type> >())->push_back(Type::VoidTy);
738   }
739   | /*empty*/ {
740     $$ = new list<PATypeHolder<Type> >();
741   }
742
743
744 // ConstVal - The various declarations that go into the constant pool.  This
745 // includes all forward declarations of types, constants, and functions.
746 //
747 // This is broken into two sections: ExtendedConstVal and ConstVal
748 //
749 ExtendedConstVal: ArrayType '[' ConstVector ']' { // Nonempty unsized arr
750     const ArrayType *ATy = *$1;
751     const Type *ETy = ATy->getElementType();
752     int NumElements = ATy->getNumElements();
753
754     // Verify that we have the correct size...
755     if (NumElements != -1 && NumElements != (int)$3->size())
756       ThrowException("Type mismatch: constant sized array initialized with " +
757                      utostr($3->size()) +  " arguments, but has size of " + 
758                      itostr(NumElements) + "!");
759
760     // Verify all elements are correct type!
761     for (unsigned i = 0; i < $3->size(); i++) {
762       if (ETy != (*$3)[i]->getType())
763         ThrowException("Element #" + utostr(i) + " is not of type '" + 
764                        ETy->getName() + "' as required!\nIt is of type '" +
765                        (*$3)[i]->getType()->getName() + "'.");
766     }
767
768     $$ = ConstPoolArray::get(ATy, *$3);
769     delete $1; delete $3;
770   }
771   | ArrayType '[' ']' {
772     int NumElements = (*$1)->getNumElements();
773     if (NumElements != -1 && NumElements != 0) 
774       ThrowException("Type mismatch: constant sized array initialized with 0"
775                      " arguments, but has size of " + itostr(NumElements) +"!");
776     $$ = ConstPoolArray::get((*$1), vector<ConstPoolVal*>());
777     delete $1;
778   }
779   | ArrayType 'c' STRINGCONSTANT {
780     const ArrayType *ATy = *$1;
781     int NumElements = ATy->getNumElements();
782     const Type *ETy = ATy->getElementType();
783     char *EndStr = UnEscapeLexed($3, true);
784     if (NumElements != -1 && NumElements != (EndStr-$3))
785       ThrowException("Can't build string constant of size " + 
786                      itostr((int)(EndStr-$3)) +
787                      " when array has size " + itostr(NumElements) + "!");
788     vector<ConstPoolVal*> Vals;
789     if (ETy == Type::SByteTy) {
790       for (char *C = $3; C != EndStr; ++C)
791         Vals.push_back(ConstPoolSInt::get(ETy, *C));
792     } else if (ETy == Type::UByteTy) {
793       for (char *C = $3; C != EndStr; ++C)
794         Vals.push_back(ConstPoolUInt::get(ETy, *C));
795     } else {
796       free($3);
797       ThrowException("Cannot build string arrays of non byte sized elements!");
798     }
799     free($3);
800     $$ = ConstPoolArray::get(ATy, Vals);
801     delete $1;
802   }
803   | StructType '{' ConstVector '}' {
804     // FIXME: TODO: Check to see that the constants are compatible with the type
805     // initializer!
806     $$ = ConstPoolStruct::get(*$1, *$3);
807     delete $1; delete $3;
808   }
809 /*
810   | Types '*' ConstVal {
811     assert(0);
812     $$ = 0;
813   }
814 */
815
816 ConstVal : ExtendedConstVal {
817     $$ = $1;
818   }
819   | SIntType EINT64VAL {     // integral constants
820     if (!ConstPoolSInt::isValueValidForType($1, $2))
821       ThrowException("Constant value doesn't fit in type!");
822     $$ = ConstPoolSInt::get($1, $2);
823   } 
824   | UIntType EUINT64VAL {           // integral constants
825     if (!ConstPoolUInt::isValueValidForType($1, $2))
826       ThrowException("Constant value doesn't fit in type!");
827     $$ = ConstPoolUInt::get($1, $2);
828   } 
829   | BOOL TRUE {                     // Boolean constants
830     $$ = ConstPoolBool::True;
831   }
832   | BOOL FALSE {                    // Boolean constants
833     $$ = ConstPoolBool::False;
834   }
835   | FPType FPVAL {                   // Float & Double constants
836     $$ = ConstPoolFP::get($1, $2);
837   }
838
839 // ConstVector - A list of comma seperated constants.
840 ConstVector : ConstVector ',' ConstVal {
841     ($$ = $1)->push_back($3);
842   }
843   | ConstVal {
844     $$ = new vector<ConstPoolVal*>();
845     $$->push_back($1);
846   }
847
848
849 //ExternMethodDecl : EXTERNAL TypesV '(' TypeList ')' {
850 //  }
851 //ExternVarDecl : 
852
853 // ConstPool - Constants with optional names assigned to them.
854 ConstPool : ConstPool OptAssign ConstVal { 
855     if ($2) {
856       setValueName($3, $2);
857       free($2);
858     }
859     InsertValue($3);
860   }
861   | ConstPool OptAssign TYPE TypesV {  // Types can be defined in the const pool
862     if ($2) {
863       // TODO: FIXME when Type are not const
864       setValueName(const_cast<Type*>($4->get()), $2);
865       free($2);
866     } else {
867       InsertType($4->get(),
868                  CurMeth.CurrentMethod ? CurMeth.Types : CurModule.Types);
869     }
870     delete $4;
871   }
872   | ConstPool MethodProto {            // Method prototypes can be in const pool
873   }
874   | ConstPool GLOBAL OptAssign Types { // Global declarations appear in CP
875     if (!$4->get()->isPointerType() || 
876         (((PointerType*)$4->get())->isArrayType() && 
877          ((PointerType*)$4->get())->isArrayType()->isUnsized())) {
878       ThrowException("Type '" + $4->get()->getDescription() +
879                      "' is not a pointer to a sized type!");
880     }
881
882     GlobalVariable *GV = new GlobalVariable(*$4);
883     delete $4;
884     if ($3) {
885       setValueName(GV, $3);
886       free($3);
887     }
888     CurModule.CurrentModule->getGlobalList().push_back(GV);
889     InsertValue(GV, CurModule.Values);
890   }
891   | /* empty: end of list */ { 
892   }
893
894
895 //===----------------------------------------------------------------------===//
896 //                             Rules to match Modules
897 //===----------------------------------------------------------------------===//
898
899 // Module rule: Capture the result of parsing the whole file into a result
900 // variable...
901 //
902 Module : MethodList {
903   $$ = ParserResult = $1;
904   CurModule.ModuleDone();
905 }
906
907 // MethodList - A list of methods, preceeded by a constant pool.
908 //
909 MethodList : MethodList Method {
910     $$ = $1;
911     if (!$2->getParent())
912       $1->getMethodList().push_back($2);
913     CurMeth.MethodDone();
914   } 
915   | MethodList MethodProto {
916     $$ = $1;
917   }
918   | ConstPool IMPLEMENTATION {
919     $$ = CurModule.CurrentModule;
920     // Resolve circular types before we parse the body of the module
921     ResolveTypes(CurModule.LateResolveTypes);
922   }
923
924
925 //===----------------------------------------------------------------------===//
926 //                       Rules to match Method Headers
927 //===----------------------------------------------------------------------===//
928
929 OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; }
930
931 ArgVal : Types OptVAR_ID {
932   $$ = new MethodArgument(*$1); delete $1;
933   if ($2) {      // Was the argument named?
934     setValueName($$, $2);
935     free($2);    // The string was strdup'd, so free it now.
936   }
937 }
938
939 ArgListH : ArgVal ',' ArgListH {
940     $$ = $3;
941     $3->push_front($1);
942   }
943   | ArgVal {
944     $$ = new list<MethodArgument*>();
945     $$->push_front($1);
946   }
947   | DOTDOTDOT {
948     $$ = new list<MethodArgument*>();
949     $$->push_back(new MethodArgument(Type::VoidTy));
950   }
951
952 ArgList : ArgListH {
953     $$ = $1;
954   }
955   | /* empty */ {
956     $$ = 0;
957   }
958
959 MethodHeaderH : TypesV STRINGCONSTANT '(' ArgList ')' {
960   UnEscapeLexed($2);
961   vector<const Type*> ParamTypeList;
962   if ($4)
963     for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I)
964       ParamTypeList.push_back((*I)->getType());
965
966   const MethodType *MT = MethodType::get(*$1, ParamTypeList);
967   delete $1;
968
969   Method *M = 0;
970   if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
971     if (Value *V = ST->lookup(MT, $2)) {  // Method already in symtab?
972       M = V->castMethodAsserting();
973
974       // Yes it is.  If this is the case, either we need to be a forward decl,
975       // or it needs to be.
976       if (!CurMeth.isDeclare && !M->isExternal())
977         ThrowException("Redefinition of method '" + string($2) + "'!");      
978     }
979   }
980
981   if (M == 0) {  // Not already defined?
982     M = new Method(MT, $2);
983     InsertValue(M, CurModule.Values);
984   }
985
986   free($2);  // Free strdup'd memory!
987
988   CurMeth.MethodStart(M);
989
990   // Add all of the arguments we parsed to the method...
991   if ($4 && !CurMeth.isDeclare) {        // Is null if empty...
992     Method::ArgumentListType &ArgList = M->getArgumentList();
993
994     for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I) {
995       InsertValue(*I);
996       ArgList.push_back(*I);
997     }
998     delete $4;                     // We're now done with the argument list
999   }
1000 }
1001
1002 MethodHeader : MethodHeaderH ConstPool BEGINTOK {
1003   $$ = CurMeth.CurrentMethod;
1004
1005   // Resolve circular types before we parse the body of the method.
1006   ResolveTypes(CurMeth.LateResolveTypes);
1007 }
1008
1009 Method : BasicBlockList END {
1010   $$ = $1;
1011 }
1012
1013 MethodProto : DECLARE { CurMeth.isDeclare = true; } MethodHeaderH {
1014   $$ = CurMeth.CurrentMethod;
1015   if (!$$->getParent())
1016     CurModule.CurrentModule->getMethodList().push_back($$);
1017   CurMeth.MethodDone();
1018 }
1019
1020 //===----------------------------------------------------------------------===//
1021 //                        Rules to match Basic Blocks
1022 //===----------------------------------------------------------------------===//
1023
1024 ConstValueRef : ESINT64VAL {    // A reference to a direct constant
1025     $$ = ValID::create($1);
1026   }
1027   | EUINT64VAL {
1028     $$ = ValID::create($1);
1029   }
1030   | FPVAL {                     // Perhaps it's an FP constant?
1031     $$ = ValID::create($1);
1032   }
1033   | TRUE {
1034     $$ = ValID::create((int64_t)1);
1035   } 
1036   | FALSE {
1037     $$ = ValID::create((int64_t)0);
1038   }
1039 /*
1040   | STRINGCONSTANT {        // Quoted strings work too... especially for methods
1041     $$ = ValID::create_conststr($1);
1042   }
1043 */
1044
1045 // ValueRef - A reference to a definition... 
1046 ValueRef : INTVAL {           // Is it an integer reference...?
1047     $$ = ValID::create($1);
1048   }
1049   | VAR_ID {                 // Is it a named reference...?
1050     $$ = ValID::create($1);
1051   }
1052   | ConstValueRef {
1053     $$ = $1;
1054   }
1055
1056 // ResolvedVal - a <type> <value> pair.  This is used only in cases where the
1057 // type immediately preceeds the value reference, and allows complex constant
1058 // pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1059 ResolvedVal : ExtendedConstVal {
1060     $$ = $1;
1061   }
1062   | Types ValueRef {
1063     $$ = getVal(*$1, $2); delete $1;
1064   }
1065
1066
1067 BasicBlockList : BasicBlockList BasicBlock {
1068     $1->getBasicBlocks().push_back($2);
1069     $$ = $1;
1070   }
1071   | MethodHeader BasicBlock { // Do not allow methods with 0 basic blocks   
1072     $$ = $1;                  // in them...
1073     $1->getBasicBlocks().push_back($2);
1074   }
1075
1076
1077 // Basic blocks are terminated by branching instructions: 
1078 // br, br/cc, switch, ret
1079 //
1080 BasicBlock : InstructionList BBTerminatorInst  {
1081     $1->getInstList().push_back($2);
1082     InsertValue($1);
1083     $$ = $1;
1084   }
1085   | LABELSTR InstructionList BBTerminatorInst  {
1086     $2->getInstList().push_back($3);
1087     setValueName($2, $1);
1088     free($1);         // Free the strdup'd memory...
1089
1090     InsertValue($2);
1091     $$ = $2;
1092   }
1093
1094 InstructionList : InstructionList Inst {
1095     $1->getInstList().push_back($2);
1096     $$ = $1;
1097   }
1098   | /* empty */ {
1099     $$ = new BasicBlock();
1100   }
1101
1102 BBTerminatorInst : RET ResolvedVal {              // Return with a result...
1103     $$ = new ReturnInst($2);
1104   }
1105   | RET VOID {                                       // Return with no result...
1106     $$ = new ReturnInst();
1107   }
1108   | BR LABEL ValueRef {                         // Unconditional Branch...
1109     $$ = new BranchInst(getVal(Type::LabelTy, $3)->castBasicBlockAsserting());
1110   }                                                  // Conditional Branch...
1111   | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {  
1112     $$ = new BranchInst(getVal(Type::LabelTy, $6)->castBasicBlockAsserting(), 
1113                         getVal(Type::LabelTy, $9)->castBasicBlockAsserting(),
1114                         getVal(Type::BoolTy, $3));
1115   }
1116   | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1117     SwitchInst *S = new SwitchInst(getVal($2, $3), 
1118                           getVal(Type::LabelTy, $6)->castBasicBlockAsserting());
1119     $$ = S;
1120
1121     list<pair<ConstPoolVal*, BasicBlock*> >::iterator I = $8->begin(), 
1122                                                       end = $8->end();
1123     for (; I != end; ++I)
1124       S->dest_push_back(I->first, I->second);
1125   }
1126
1127 JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1128     $$ = $1;
1129     ConstPoolVal *V = getVal($2, $3, true)->castConstantAsserting();
1130     if (V == 0)
1131       ThrowException("May only switch on a constant pool value!");
1132
1133     $$->push_back(make_pair(V, getVal($5, $6)->castBasicBlockAsserting()));
1134   }
1135   | IntType ConstValueRef ',' LABEL ValueRef {
1136     $$ = new list<pair<ConstPoolVal*, BasicBlock*> >();
1137     ConstPoolVal *V = getVal($1, $2, true)->castConstantAsserting();
1138
1139     if (V == 0)
1140       ThrowException("May only switch on a constant pool value!");
1141
1142     $$->push_back(make_pair(V, getVal($4, $5)->castBasicBlockAsserting()));
1143   }
1144
1145 Inst : OptAssign InstVal {
1146   if ($1)                   // Is this definition named??
1147     setValueName($2, $1);   // if so, assign the name...
1148
1149   InsertValue($2);
1150   $$ = $2;
1151 }
1152
1153 PHIList : Types '[' ValueRef ',' ValueRef ']' {    // Used for PHI nodes
1154     $$ = new list<pair<Value*, BasicBlock*> >();
1155     $$->push_back(make_pair(getVal(*$1, $3), 
1156                          getVal(Type::LabelTy, $5)->castBasicBlockAsserting()));
1157     delete $1;
1158   }
1159   | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1160     $$ = $1;
1161     $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
1162                          getVal(Type::LabelTy, $6)->castBasicBlockAsserting()));
1163   }
1164
1165
1166 ValueRefList : ResolvedVal {    // Used for call statements, and memory insts...
1167     $$ = new list<Value*>();
1168     $$->push_back($1);
1169   }
1170   | ValueRefList ',' ResolvedVal {
1171     $$ = $1;
1172     $1->push_back($3);
1173   }
1174
1175 // ValueRefListE - Just like ValueRefList, except that it may also be empty!
1176 ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; }
1177
1178 InstVal : BinaryOps Types ValueRef ',' ValueRef {
1179     $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1180     if ($$ == 0)
1181       ThrowException("binary operator returned null!");
1182     delete $2;
1183   }
1184   | UnaryOps ResolvedVal {
1185     $$ = UnaryOperator::create($1, $2);
1186     if ($$ == 0)
1187       ThrowException("unary operator returned null!");
1188   }
1189   | ShiftOps ResolvedVal ',' ResolvedVal {
1190     if ($4->getType() != Type::UByteTy)
1191       ThrowException("Shift amount must be ubyte!");
1192     $$ = new ShiftInst($1, $2, $4);
1193   }
1194   | CAST ResolvedVal TO Types {
1195     $$ = new CastInst($2, *$4);
1196     delete $4;
1197   }
1198   | PHI PHIList {
1199     const Type *Ty = $2->front().first->getType();
1200     $$ = new PHINode(Ty);
1201     while ($2->begin() != $2->end()) {
1202       if ($2->front().first->getType() != Ty) 
1203         ThrowException("All elements of a PHI node must be of the same type!");
1204       ((PHINode*)$$)->addIncoming($2->front().first, $2->front().second);
1205       $2->pop_front();
1206     }
1207     delete $2;  // Free the list...
1208   } 
1209   | CALL TypesV ValueRef '(' ValueRefListE ')' {
1210     const MethodType *Ty;
1211
1212     if (!(Ty = (*$2)->isMethodType())) {
1213       // Pull out the types of all of the arguments...
1214       vector<const Type*> ParamTypes;
1215       for (list<Value*>::iterator I = $5->begin(), E = $5->end(); I != E; ++I)
1216         ParamTypes.push_back((*I)->getType());
1217       Ty = MethodType::get(*$2, ParamTypes);
1218     }
1219     delete $2;
1220
1221     Value *V = getVal(Ty, $3);   // Get the method we're calling...
1222
1223     // Create the call node...
1224     if (!$5) {                                   // Has no arguments?
1225       $$ = new CallInst(V->castMethodAsserting(), vector<Value*>());
1226     } else {                                     // Has arguments?
1227       // Loop through MethodType's arguments and ensure they are specified
1228       // correctly!
1229       //
1230       MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1231       MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
1232       list<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1233
1234       for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1235         if ((*ArgI)->getType() != *I)
1236           ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1237                          (*I)->getName() + "'!");
1238
1239       if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1240         ThrowException("Invalid number of parameters detected!");
1241
1242       $$ = new CallInst(V->castMethodAsserting(),
1243                         vector<Value*>($5->begin(), $5->end()));
1244     }
1245     delete $5;
1246   }
1247   | MemoryInst {
1248     $$ = $1;
1249   }
1250
1251 // UByteList - List of ubyte values for load and store instructions
1252 UByteList : ',' ConstVector { 
1253   $$ = $2; 
1254 } | /* empty */ { 
1255   $$ = new vector<ConstPoolVal*>(); 
1256 }
1257
1258 MemoryInst : MALLOC Types {
1259     $$ = new MallocInst(PointerType::get(*$2));
1260     delete $2;
1261   }
1262   | MALLOC Types ',' UINT ValueRef {
1263     if (!(*$2)->isArrayType() || ((const ArrayType*)$2->get())->isSized())
1264       ThrowException("Trying to allocate " + (*$2)->getName() + 
1265                      " as unsized array!");
1266     const Type *Ty = PointerType::get(*$2);
1267     $$ = new MallocInst(Ty, getVal($4, $5));
1268     delete $2;
1269   }
1270   | ALLOCA Types {
1271     $$ = new AllocaInst(PointerType::get(*$2));
1272     delete $2;
1273   }
1274   | ALLOCA Types ',' UINT ValueRef {
1275     if (!(*$2)->isArrayType() || ((const ArrayType*)$2->get())->isSized())
1276       ThrowException("Trying to allocate " + (*$2)->getName() + 
1277                      " as unsized array!");
1278     const Type *Ty = PointerType::get(*$2);
1279     Value *ArrSize = getVal($4, $5);
1280     $$ = new AllocaInst(Ty, ArrSize);
1281     delete $2;
1282   }
1283   | FREE ResolvedVal {
1284     if (!$2->getType()->isPointerType())
1285       ThrowException("Trying to free nonpointer type " + 
1286                      $2->getType()->getName() + "!");
1287     $$ = new FreeInst($2);
1288   }
1289
1290   | LOAD Types ValueRef UByteList {
1291     if (!(*$2)->isPointerType())
1292       ThrowException("Can't load from nonpointer type: " + (*$2)->getName());
1293     if (LoadInst::getIndexedType(*$2, *$4) == 0)
1294       ThrowException("Invalid indices for load instruction!");
1295
1296     $$ = new LoadInst(getVal(*$2, $3), *$4);
1297     delete $4;   // Free the vector...
1298     delete $2;
1299   }
1300   | STORE ResolvedVal ',' Types ValueRef UByteList {
1301     if (!(*$4)->isPointerType())
1302       ThrowException("Can't store to a nonpointer type: " + (*$4)->getName());
1303     const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
1304     if (ElTy == 0)
1305       ThrowException("Can't store into that field list!");
1306     if (ElTy != $2->getType())
1307       ThrowException("Can't store '" + $2->getType()->getName() +
1308                      "' into space of type '" + ElTy->getName() + "'!");
1309     $$ = new StoreInst($2, getVal(*$4, $5), *$6);
1310     delete $4; delete $6;
1311   }
1312   | GETELEMENTPTR Types ValueRef UByteList {
1313     if (!(*$2)->isPointerType())
1314       ThrowException("getelementptr insn requires pointer operand!");
1315     if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
1316       ThrowException("Can't get element ptr '" + (*$2)->getName() + "'!");
1317     $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1318     delete $2; delete $4;
1319   }
1320
1321 %%
1322 int yyerror(const char *ErrorMsg) {
1323   ThrowException(string("Parse error: ") + ErrorMsg);
1324   return 0;
1325 }