Initial, non-functional, version of llvm-upgrade. This version just echos
[oota-llvm.git] / tools / llvm-upgrade / UpgradeParser.y
1 //===-- UpgradeParser.y - Upgrade parser for llvm assmbly -------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Reid Spencer and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file implements the bison parser for LLVM 1.9 assembly language.
11 //
12 //===----------------------------------------------------------------------===//
13
14 %{
15 #define YYERROR_VERBOSE 1
16 #define YYSTYPE std::string*
17
18 #include "ParserInternals.h"
19 #include <llvm/ADT/StringExtras.h>
20 #include <llvm/System/MappedFile.h>
21 #include <algorithm>
22 #include <list>
23 #include <utility>
24 #include <iostream>
25
26
27 int yylex();                       // declaration" of xxx warnings.
28 int yyparse();
29
30 static std::string CurFilename;
31
32 static std::ostream *O = 0;
33
34 void UpgradeAssembly(const std::string &infile, std::ostream &out)
35 {
36   Upgradelineno = 1; 
37   CurFilename = infile;
38   llvm::sys::Path p(infile);
39   llvm::sys::MappedFile mf;
40   mf.open(p);
41   mf.map();
42   const char* base = mf.charBase();
43   size_t sz = mf.size();
44
45   set_scan_bytes(base, sz);
46
47   O = &out;
48
49   if (yyparse()) {
50     std::cerr << "Parse failed.\n";
51     exit(1);
52   }
53 }
54
55 %}
56
57 %token ESINT64VAL
58 %token EUINT64VAL
59 %token SINTVAL   // Signed 32 bit ints...
60 %token UINTVAL   // Unsigned 32 bit ints...
61 %token FPVAL     // Float or Double constant
62 %token VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
63 %token FLOAT DOUBLE TYPE LABEL
64 %token VAR_ID LABELSTR STRINGCONSTANT
65 %token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
66 %token DECLARE GLOBAL CONSTANT SECTION VOLATILE
67 %token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
68 %token DLLIMPORT DLLEXPORT EXTERN_WEAK
69 %token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
70 %token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
71 %token CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
72 %token X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
73 %token DATALAYOUT
74 %token RET BR SWITCH INVOKE UNWIND UNREACHABLE
75 %token ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
76 %token SETLE SETGE SETLT SETGT SETEQ SETNE  // Binary Comparators
77 %token MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
78 %token TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
79 %token UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
80 %token PHI_TOK SELECT SHL LSHR ASHR VAARG
81 %token EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
82 %token CAST
83
84 %start Module
85
86 %%
87
88 // Handle constant integer size restriction and conversion...
89 INTVAL : SINTVAL | UINTVAL 
90 EINT64VAL : ESINT64VAL | EUINT64VAL;
91
92 // Operations that are notably excluded from this list include:
93 // RET, BR, & SWITCH because they end basic blocks and are treated specially.
94 ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
95 LogicalOps   : AND | OR | XOR;
96 SetCondOps   : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
97 CastOps      : CAST;
98 ShiftOps     : SHL | LSHR | ASHR;
99
100 // These are some types that allow classification if we only want a particular 
101 // thing... for example, only a signed, unsigned, or integral type.
102 SIntType :  LONG |  INT |  SHORT | SBYTE;
103 UIntType : ULONG | UINT | USHORT | UBYTE;
104 IntType  : SIntType | UIntType;
105 FPType   : FLOAT | DOUBLE;
106
107 // OptAssign - Value producing statements have an optional assignment component
108 OptAssign : Name '=' {
109     $1->append(" = ");
110     $$ = $1;
111   }
112   | /*empty*/ {
113     $$ = new std::string(""); 
114   };
115
116 OptLinkage 
117   : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT 
118   | EXTERN_WEAK 
119   | /*empty*/   { $$ = new std::string(""); } ;
120
121 OptCallingConv 
122   : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK 
123   | X86_FASTCALLCC_TOK | CC_TOK EUINT64VAL
124   | /*empty*/ { $$ = new std::string(""); } ;
125
126 // OptAlign/OptCAlign - An optional alignment, and an optional alignment with
127 // a comma before it.
128 OptAlign 
129   : /*empty*/        { $$ = new std::string(); }
130   | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
131          ;
132 OptCAlign 
133   : /*empty*/            { $$ = new std::string(); } 
134   | ',' ALIGN EUINT64VAL { 
135     $2->insert(0, ", "); 
136     *$2 += " " + *$3;
137     delete $3;
138     $$ = $2;
139   };
140
141 SectionString 
142   : SECTION STRINGCONSTANT { 
143     *$1 += " " + *$2;
144     delete $2;
145     $$ = $1;
146   };
147
148 OptSection : /*empty*/     { $$ = new std::string(); } 
149            | SectionString;
150
151 GlobalVarAttributes 
152     : /* empty */ { $$ = new std::string(); } 
153     | ',' GlobalVarAttribute GlobalVarAttributes  {
154       $2->insert(0, ", ");
155       if (!$3->empty())
156         *$2 += " " + *$3;
157       delete $3;
158       $$ = $2;
159     };
160
161 GlobalVarAttribute 
162     : SectionString 
163     | ALIGN EUINT64VAL {
164       *$1 += " " + *$2;
165       delete $2;
166       $$ = $1;
167     };
168
169 //===----------------------------------------------------------------------===//
170 // Types includes all predefined types... except void, because it can only be
171 // used in specific contexts (function returning void for example).  To have
172 // access to it, a user must explicitly use TypesV.
173 //
174
175 // TypesV includes all of 'Types', but it also includes the void type.
176 TypesV    : Types    | VOID ;
177 UpRTypesV : UpRTypes | VOID ; 
178 Types     : UpRTypes ;
179
180 // Derived types are added later...
181 //
182 PrimType : BOOL | SBYTE | UBYTE | SHORT  | USHORT | INT   | UINT ;
183 PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE   | LABEL;
184 UpRTypes : OPAQUE | PrimType | SymbolicValueRef ;
185
186 // Include derived types in the Types production.
187 //
188 UpRTypes : '\\' EUINT64VAL {                   // Type UpReference
189     $2->insert(0, "\\");
190     $$ = $2;
191   }
192   | UpRTypesV '(' ArgTypeListI ')' {           // Function derived type?
193     *$1 += "( " + *$3 + " )";
194     delete $3;
195     $$ = $1;
196   }
197   | '[' EUINT64VAL 'x' UpRTypes ']' {          // Sized array type?
198     $2->insert(0,"[ ");
199     *$2 += " x " + *$4 + " ]";
200     delete $4;
201     $$ = $2;
202   }
203   | '<' EUINT64VAL 'x' UpRTypes '>' {          // Packed array type?
204     $2->insert(0,"< ");
205     *$2 += " x " + *$4 + " >";
206     delete $4;
207     $$ = $2;
208   }
209   | '{' TypeListI '}' {                        // Structure type?
210     $2->insert(0, "{ ");
211     *$2 += " }";
212     $$ = $2;
213   }
214   | '{' '}' {                                  // Empty structure type?
215     $$ = new std::string("{ }");
216   }
217   | UpRTypes '*' {                             // Pointer type?
218     *$1 += '*';
219     $$ = $1;
220   };
221
222 // TypeList - Used for struct declarations and as a basis for function type 
223 // declaration type lists
224 //
225 TypeListI : UpRTypes | TypeListI ',' UpRTypes {
226     *$1 += ", " + *$3;
227     delete $3;
228     $$ = $1;
229   };
230
231 // ArgTypeList - List of types for a function type declaration...
232 ArgTypeListI : TypeListI
233   | TypeListI ',' DOTDOTDOT {
234     *$1 += ", ...";
235     delete $3;
236     $$ = $1;
237   }
238   | DOTDOTDOT {
239     $$ = $1;
240   }
241   | /*empty*/ {
242     $$ = new std::string();
243   };
244
245 // ConstVal - The various declarations that go into the constant pool.  This
246 // production is used ONLY to represent constants that show up AFTER a 'const',
247 // 'constant' or 'global' token at global scope.  Constants that can be inlined
248 // into other expressions (such as integers and constexprs) are handled by the
249 // ResolvedVal, ValueRef and ConstValueRef productions.
250 //
251 ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
252     *$1 += " [ " + *$3 + " ]";
253     delete $3;
254     $$ = $1;
255   }
256   | Types '[' ']' {
257     $$ = new std::string("[ ]");
258   }
259   | Types 'c' STRINGCONSTANT {
260     *$1 += " c" + *$3;
261     delete $3;
262     $$ = $1;
263   }
264   | Types '<' ConstVector '>' { // Nonempty unsized arr
265     *$1 += " < " + *$3 + " >";
266     delete $3;
267     $$ = $1;
268   }
269   | Types '{' ConstVector '}' {
270     *$1 += " { " + *$3 + " }";
271     delete $3;
272     $$ = $1;
273   }
274   | Types '{' '}' {
275     $$ = new std::string("[ ]");
276   }
277   | Types NULL_TOK {
278     *$1 += " " + *$2; 
279     delete $2;
280     $$ = $1;
281   }
282   | Types UNDEF {
283     *$1 += " " + *$2;
284     delete $2;
285     $$ = $1;
286   }
287   | Types SymbolicValueRef {
288     *$1 += " " + *$2;
289     delete $2;
290     $$ = $1;
291   }
292   | Types ConstExpr {
293     *$1 += " " + *$2;
294     delete $2;
295     $$ = $1;
296   }
297   | Types ZEROINITIALIZER {
298     *$1 += " " + *$2;
299     delete $2;
300     $$ = $1;
301   };
302
303 ConstVal : SIntType EINT64VAL {      // integral constants
304     *$1 += " " + *$2;
305     delete $2;
306     $$ = $1;
307   }
308   | UIntType EUINT64VAL {            // integral constants
309     *$1 += " " + *$2;
310     delete $2;
311     $$ = $1;
312   }
313   | BOOL TRUETOK {                      // Boolean constants
314     *$1 += " " + *$2;
315     delete $2;
316     $$ = $1;
317   }
318   | BOOL FALSETOK {                     // Boolean constants
319     *$1 += " " + *$2;
320     delete $2;
321     $$ = $1;
322   }
323   | FPType FPVAL {                   // Float & Double constants
324     *$1 += " " + *$2;
325     delete $2;
326     $$ = $1;
327   };
328
329
330 ConstExpr: CastOps '(' ConstVal TO Types ')' {
331     *$1 += " (" + *$3 + " " + *$4 + " " + *$5 + ")";
332     delete $3; delete $4; delete $5;
333     $$ = $1;
334   }
335   | GETELEMENTPTR '(' ConstVal IndexList ')' {
336   }
337   | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
338   }
339   | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
340   }
341   | LogicalOps '(' ConstVal ',' ConstVal ')' {
342   }
343   | SetCondOps '(' ConstVal ',' ConstVal ')' {
344   }
345   | ShiftOps '(' ConstVal ',' ConstVal ')' {
346   }
347   | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
348   }
349   | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
350   }
351   | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
352   };
353
354
355 // ConstVector - A list of comma separated constants.
356 ConstVector : ConstVector ',' ConstVal {
357   }
358   | ConstVal {
359   };
360
361
362 // GlobalType - Match either GLOBAL or CONSTANT for global declarations...
363 GlobalType : GLOBAL { } | CONSTANT { };
364
365
366 //===----------------------------------------------------------------------===//
367 //                             Rules to match Modules
368 //===----------------------------------------------------------------------===//
369
370 // Module rule: Capture the result of parsing the whole file into a result
371 // variable...
372 //
373 Module : DefinitionList {
374 };
375
376 // DefinitionList - Top level definitions
377 //
378 DefinitionList : DefinitionList Function {
379     $$ = 0;
380   } 
381   | DefinitionList FunctionProto {
382     *O << *$2 << "\n";
383     delete $2;
384     $$ = 0;
385   }
386   | DefinitionList MODULE ASM_TOK AsmBlock {
387     *O << "module asm " << " " << *$4 << "\n";
388   }  
389   | DefinitionList IMPLEMENTATION {
390     *O << "implementation\n";
391   }
392   | ConstPool {
393   };
394
395 // ConstPool - Constants with optional names assigned to them.
396 ConstPool : ConstPool OptAssign TYPE TypesV {
397     *O << *$2 << " " << *$3 << " " << *$4 << "\n";
398     delete $2; delete $3; delete $4;
399     $$ = 0;
400   }
401   | ConstPool FunctionProto {       // Function prototypes can be in const pool
402     *O << *$2 << "\n";
403     delete $2;
404     $$ = 0;
405   }
406   | ConstPool MODULE ASM_TOK AsmBlock {  // Asm blocks can be in the const pool
407     *O << *$2 << " " << *$3 << " " << *$4 << "\n";
408     delete $2; delete $3; delete $4; 
409     $$ = 0;
410   }
411   | ConstPool OptAssign OptLinkage GlobalType ConstVal  GlobalVarAttributes {
412     *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5 << " " << *$6 << "\n";
413     delete $2; delete $3; delete $4; delete $5; delete $6;
414     $$ = 0;
415   }
416   | ConstPool OptAssign EXTERNAL GlobalType Types  GlobalVarAttributes {
417     *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5 << " " << *$6 << "\n";
418     delete $2; delete $3; delete $4; delete $5; delete $6;
419     $$ = 0;
420   }
421   | ConstPool OptAssign DLLIMPORT GlobalType Types  GlobalVarAttributes {
422     *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5 << " " << *$6 << "\n";
423     delete $2; delete $3; delete $4; delete $5; delete $6;
424     $$ = 0;
425   }
426   | ConstPool OptAssign EXTERN_WEAK GlobalType Types  GlobalVarAttributes {
427     *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5 << " " << *$6 << "\n";
428     delete $2; delete $3; delete $4; delete $5; delete $6;
429     $$ = 0;
430   }
431   | ConstPool TARGET TargetDefinition { 
432     *O << *$2 << " " << *$3 << "\n";
433     delete $2; delete $3;
434     $$ = 0;
435   }
436   | ConstPool DEPLIBS '=' LibrariesDefinition {
437     *O << *$2 << " = " << *$4 << "\n";
438     delete $2; delete $4;
439     $$ = 0;
440   }
441   | /* empty: end of list */ { 
442     $$ = 0;
443   };
444
445
446 AsmBlock : STRINGCONSTANT ;
447
448 BigOrLittle : BIG | LITTLE 
449
450 TargetDefinition 
451   : ENDIAN '=' BigOrLittle {
452     *$1 += " = " + *$2;
453     delete $2;
454     $$ = $1;
455   }
456   | POINTERSIZE '=' EUINT64VAL {
457     *$1 += " = " + *$2;
458     delete $2;
459     $$ = $1;
460   }
461   | TRIPLE '=' STRINGCONSTANT {
462     *$1 += " = " + *$2;
463     delete $2;
464     $$ = $1;
465   }
466   | DATALAYOUT '=' STRINGCONSTANT {
467     *$1 += " = " + *$2;
468     delete $2;
469     $$ = $1;
470   };
471
472 LibrariesDefinition 
473   : '[' LibList ']' {
474     $2->insert(0, "[ ");
475     *$2 += " ]";
476     $$ = $2;
477   };
478
479 LibList 
480   : LibList ',' STRINGCONSTANT {
481     *$1 += ", " + *$3;
482     delete $3;
483     $$ = $1;
484   }
485   | STRINGCONSTANT 
486   | /* empty: end of list */ {
487     $$ = new std::string();
488   };
489
490 //===----------------------------------------------------------------------===//
491 //                       Rules to match Function Headers
492 //===----------------------------------------------------------------------===//
493
494 Name : VAR_ID | STRINGCONSTANT;
495 OptName : Name | /*empty*/ { $$ = new std::string(); };
496
497 ArgVal : Types OptName {
498   $$ = $1;
499   if (!$2->empty())
500     *$$ += " " + *$2;
501 };
502
503 ArgListH : ArgListH ',' ArgVal {
504     *$1 += ", " + *$3;
505   }
506   | ArgVal {
507     $$ = $1;
508   };
509
510 ArgList : ArgListH {
511     $$ = $1;
512   }
513   | ArgListH ',' DOTDOTDOT {
514     *$1 += ", ...";
515     $$ = $1;
516   }
517   | DOTDOTDOT {
518     $$ = $1;
519   }
520   | /* empty */ {
521     $$ = new std::string();
522   };
523
524 FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')' 
525                   OptSection OptAlign {
526     if (!$1->empty()) {
527       $2->insert(0, *$1 + " ");
528     }
529     *$2 += " " + *$3 + "( " + *$5 + " )";
530     if (!$7->empty()) {
531       *$2 += " " + *$7;
532     }
533     if (!$8->empty()) {
534       *$2 += " " + *$8;
535     }
536     $$ = $2;
537   };
538
539 BEGIN : BEGINTOK {
540     $$ = new std::string("begin");
541   }
542   | '{' { 
543     $$ = new std::string ("{");
544   }
545
546 FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
547   if (!$1->empty()) {
548     *O << *$1 << " ";
549   }
550   *O << *$2 << " " << *$3 << "\n";
551   delete $1; delete $2; delete $3;
552   $$ = 0;
553 };
554
555 END : ENDTOK { $$ = new std::string("end"); }
556     | '}' { $$ = new std::string("}"); };
557
558 Function : FunctionHeader BasicBlockList END {
559   if ($2)
560     *O << *$2;
561   *O << '\n' << *$3 << "\n";
562 };
563
564 FnDeclareLinkage: /*default*/ 
565   | DLLIMPORT    
566   | EXTERN_WEAK 
567   ;
568   
569 FunctionProto 
570   : DECLARE FnDeclareLinkage FunctionHeaderH { 
571     *$1 += " " + *$2 + " " + *$3;
572     delete $2; delete $3;
573     $$ = $1;
574   };
575
576 //===----------------------------------------------------------------------===//
577 //                        Rules to match Basic Blocks
578 //===----------------------------------------------------------------------===//
579
580 OptSideEffect : /* empty */ {
581   }
582   | SIDEEFFECT {
583   };
584
585 ConstValueRef : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK  | FALSETOK 
586   | NULL_TOK | UNDEF | ZEROINITIALIZER 
587   | '<' ConstVector '>' { 
588     $2->insert(0, "<");
589     *$2 += ">";
590     $$ = $2;
591   }
592   | ConstExpr 
593   | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
594     if (!$2->empty()) {
595       *$1 += " " + *$2;
596     }
597     *$1 += " " + *$3 + ", " + *$4;
598     delete $2; delete $3; delete $4;
599     $$ = $1;
600   };
601
602 SymbolicValueRef : INTVAL | Name ;
603
604 // ValueRef - A reference to a definition... either constant or symbolic
605 ValueRef : SymbolicValueRef | ConstValueRef;
606
607
608 // ResolvedVal - a <type> <value> pair.  This is used only in cases where the
609 // type immediately preceeds the value reference, and allows complex constant
610 // pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
611 ResolvedVal : Types ValueRef {
612     *$1 += " " + *$2;
613     delete $2;
614     $$ = $1;
615   };
616
617 BasicBlockList : BasicBlockList BasicBlock {
618   }
619   | BasicBlock { // Do not allow functions with 0 basic blocks   
620   };
621
622
623 // Basic blocks are terminated by branching instructions: 
624 // br, br/cc, switch, ret
625 //
626 BasicBlock : InstructionList OptAssign BBTerminatorInst  {
627     *O << *$2 ;
628   };
629
630 InstructionList : InstructionList Inst {
631     *O << "    " << *$2 << "\n";
632     delete $2;
633     $$ = 0;
634   }
635   | /* empty */ {
636     $$ = 0;
637   }
638   | LABELSTR {
639     *O << *$1 << "\n";
640     delete $1;
641     $$ = 0;
642   };
643
644 BBTerminatorInst : RET ResolvedVal {              // Return with a result...
645     *O << "    " << *$1 << " " << *$2 << "\n";
646     delete $1; delete $2;
647     $$ = 0;
648   }
649   | RET VOID {                                       // Return with no result...
650     *O << "    " << *$1 << " " << *$2 << "\n";
651     delete $1; delete $2;
652     $$ = 0;
653   }
654   | BR LABEL ValueRef {                         // Unconditional Branch...
655     *O << "    " << *$1 << " " << *$2 << " " << *$3 << "\n";
656     delete $1; delete $2; delete $3;
657     $$ = 0;
658   }                                                  // Conditional Branch...
659   | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {  
660     *O << "    " << *$1 << " " << *$2 << " " << *$3 << ", " << *$5 << " "
661        << *$6 << ", " << *$8 << " " << *$9 << "\n";
662     delete $1; delete $2; delete $3; delete $5; delete $6; delete $8; delete $9;
663     $$ = 0;
664   }
665   | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
666     *O << "    " << *$1 << " " << *$2 << " " << *$3 << ", " << *$5 << " " 
667        << *$6 << " [" << *$8 << " ]\n";
668     delete $1; delete $2; delete $3; delete $5; delete $6; delete $8;
669     $$ = 0;
670   }
671   | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
672     *O << "    " << *$1 << " " << *$2 << " " << *$3 << ", " << *$5 << " " 
673        << *$6 << "[]\n";
674     delete $1; delete $2; delete $3; delete $5; delete $6;
675     $$ = 0;
676   }
677   | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
678     TO LABEL ValueRef UNWIND LABEL ValueRef {
679     *O << "    " << *$1 << " " << *$2 << " " << *$3 << " " << *$4 << " ("
680        << *$6 << ") " << *$8 << " " << *$9 << " " << *$10 << " " << *$11 << " "
681        << *$12 << " " << *$13 << "\n";
682     delete $1; delete $2; delete $3; delete $4; delete $6; delete $8; delete $9;
683     delete $10; delete $11; delete $12; delete $13; 
684     $$ = 0;
685   }
686   | UNWIND {
687     *O << "    " << *$1 << "\n";
688     delete $1;
689     $$ = 0;
690   }
691   | UNREACHABLE {
692     *O << "    " << *$1 << "\n";
693     delete $1;
694     $$ = 0;
695   };
696
697 JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
698     *$1 += *$2 + " " + *$3 + ", " + *$5 + " " + *$6;
699     delete $2; delete $3; delete $5; delete $6;
700     $$ = $1;
701   }
702   | IntType ConstValueRef ',' LABEL ValueRef {
703     *$1 += *$2 + ", " + *$4 + " " + *$5;
704     delete $2; delete $4; delete $5;
705     $$ = $1;
706   };
707
708 Inst 
709   : OptAssign InstVal {
710     *$1 += *$2;
711     delete $2;
712     $$ = $1; 
713   };
714
715 PHIList 
716   : Types '[' ValueRef ',' ValueRef ']' {    // Used for PHI nodes
717     *$1 += " [" + *$3 + "," + *$5 + "]";
718     delete $3; delete $5;
719     $$ = $1;
720   }
721   | PHIList ',' '[' ValueRef ',' ValueRef ']' {
722     *$1 += ", [" + *$4 + "," + *$6 + "]";
723     delete $4; delete $6;
724     $$ = $1;
725   };
726
727
728 ValueRefList 
729   : ResolvedVal 
730   | ValueRefList ',' ResolvedVal {
731     *$1 += ", " + *$3;
732     delete $3;
733     $$ = $1;
734   };
735
736 // ValueRefListE - Just like ValueRefList, except that it may also be empty!
737 ValueRefListE 
738   : ValueRefList 
739   | /*empty*/ { $$ = new std::string(); }
740   ;
741
742 OptTailCall 
743   : TAIL CALL {
744     *$1 += " " + *$2;
745     delete $2;
746     $$ = $1;
747   }
748   | CALL 
749   ;
750
751 InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
752     *$1 += " " + *$2 + " " + *$3 + ", " + *$5;
753     delete $2; delete $3; delete $5;
754     $$ = $1;
755   }
756   | LogicalOps Types ValueRef ',' ValueRef {
757     *$1 += " " + *$2 + " " + *$3 + ", " + *$5;
758     delete $2; delete $3; delete $5;
759     $$ = $1;
760   }
761   | SetCondOps Types ValueRef ',' ValueRef {
762     *$1 += " " + *$2 + " " + *$3 + ", " + *$5;
763     delete $2; delete $3; delete $5;
764     $$ = $1;
765   }
766   | NOT ResolvedVal {
767     *$1 += " " + *$2;
768     delete $2;
769     $$ = $1;
770   }
771   | ShiftOps ResolvedVal ',' ResolvedVal {
772     *$1 += " " + *$2 + ", " + *$4;
773     delete $2; delete $4;
774     $$ = $1;
775   }
776   | CastOps ResolvedVal TO Types {
777     *$1 += " " + *$2 + " " + *$3 + ", " + *$4;
778     delete $2; delete $3; delete $4;
779     $$ = $1;
780   }
781   | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
782     *$1 += " " + *$2 + ", " + *$4 + ", " + *$6;
783     delete $2; delete $4; delete $6;
784     $$ = $1;
785   }
786   | VAARG ResolvedVal ',' Types {
787     *$1 += " " + *$2 + ", " + *$4;
788     delete $2; delete $4;
789     $$ = $1;
790   }
791   | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
792     *$1 += " " + *$2 + ", " + *$4;
793     delete $2; delete $4;
794     $$ = $1;
795   }
796   | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
797     *$1 += " " + *$2 + ", " + *$4 + ", " + *$6;
798     delete $2; delete $4; delete $6;
799     $$ = $1;
800   }
801   | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
802     *$1 += " " + *$2 + ", " + *$4 + ", " + *$6;
803     delete $2; delete $4; delete $6;
804     $$ = $1;
805   }
806   | PHI_TOK PHIList {
807     *$1 += " " + *$2;
808     delete $2;
809     $$ = $1;
810   }
811   | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')'  {
812     if (!$2->empty())
813       *$1 += " " + *$2;
814     if (!$1->empty())
815       *$1 += " ";
816     *$1 += *$3 += " " + *$4 + "(" + *$5 + ")";
817     delete $2; delete $3; delete $4; delete $6;
818     $$ = $1;
819   }
820   | MemoryInst ;
821
822
823 // IndexList - List of indices for GEP based instructions...
824 IndexList 
825   : ',' ValueRefList { 
826     $2->insert(0, ", ");
827     $$ = $2;
828   } 
829   | /* empty */ {  $$ = new std::string(); }
830   ;
831
832 OptVolatile 
833   : VOLATILE 
834   | /* empty */ { $$ = new std::string(); }
835   ;
836
837 MemoryInst : MALLOC Types OptCAlign {
838     *$1 += " " + *$2;
839     if (!$3->empty())
840       *$1 += " " + *$3;
841     delete $2; delete $3;
842     $$ = $1;
843   }
844   | MALLOC Types ',' UINT ValueRef OptCAlign {
845     *$1 += " " + *$2 + ", " + *$4 + " " + *$5;
846     if (!$6->empty())
847       *$1 += " " + *$6;
848     delete $2; delete $4; delete $5; delete $6;
849     $$ = $1;
850   }
851   | ALLOCA Types OptCAlign {
852     *$1 += " " + *$2;
853     if (!$3->empty())
854       *$1 += " " + *$3;
855     delete $2; delete $3;
856     $$ = $1;
857   }
858   | ALLOCA Types ',' UINT ValueRef OptCAlign {
859     *$1 += " " + *$2 + ", " + *$4 + " " + *$5;
860     if (!$6->empty())
861       *$1 += " " + *$6;
862     delete $2; delete $4; delete $5; delete $6;
863     $$ = $1;
864   }
865   | FREE ResolvedVal {
866     *$1 += " " + *$2;
867     delete $2;
868     $$ = $1;
869   }
870   | OptVolatile LOAD Types ValueRef {
871     if (!$1->empty())
872       *$1 += " ";
873     *$1 += *$2 + " " + *$3 + " " + *$4;
874     delete $2; delete $3; delete $4;
875     $$ = $1;
876   }
877   | OptVolatile STORE ResolvedVal ',' Types ValueRef {
878     if (!$1->empty())
879       *$1 += " ";
880     *$1 += *$2 + " " + *$3 + ", " + *$5 + " " + *$6;
881     delete $2; delete $3; delete $5; delete $6;
882     $$ = $1;
883   }
884   | GETELEMENTPTR Types ValueRef IndexList {
885     *$1 += *$2 + " " + *$3 + " " + *$4;
886     delete $2; delete $3; delete $4;
887     $$ = $1;
888   };
889
890 %%
891
892 int yyerror(const char *ErrorMsg) {
893   std::string where 
894     = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
895                   + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
896   std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
897   if (yychar == YYEMPTY || yychar == 0)
898     errMsg += "end-of-file.";
899   else
900     errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
901   std::cerr << errMsg << '\n';
902   exit(1);
903 }