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