First version of llvm-upgrade that can correctly upgrade a large test
[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 #include "ParserInternals.h"
16 #include <llvm/ADT/StringExtras.h>
17 #include <algorithm>
18 #include <list>
19 #include <utility>
20 #include <iostream>
21
22 #define YYERROR_VERBOSE 1
23 #define YYINCLUDED_STDLIB_H
24 #define YYDEBUG 1
25
26 int yylex();                       // declaration" of xxx warnings.
27 int yyparse();
28 extern int yydebug;
29
30 static std::string CurFilename;
31 static std::ostream *O = 0;
32 std::istream* LexInput = 0;
33 unsigned SizeOfPointer = 32;
34
35 void UpgradeAssembly(const std::string &infile, std::istream& in, 
36                      std::ostream &out, bool debug)
37 {
38   Upgradelineno = 1; 
39   CurFilename = infile;
40   LexInput = &in;
41   yydebug = debug;
42   O = &out;
43
44   if (yyparse()) {
45     std::cerr << "Parse failed.\n";
46     exit(1);
47   }
48 }
49
50 const char* getCastOpcode(TypeInfo& SrcTy, TypeInfo&DstTy) {
51   unsigned SrcBits = SrcTy.getBitWidth();
52   unsigned DstBits = DstTy.getBitWidth();
53   const char* opcode = "bitcast";
54   // Run through the possibilities ...
55   if (DstTy.isIntegral()) {                        // Casting to integral
56     if (SrcTy.isIntegral()) {                      // Casting from integral
57       if (DstBits < SrcBits)
58         opcode = "trunc";
59       else if (DstBits > SrcBits) {                // its an extension
60         if (SrcTy.isSigned())
61           opcode ="sext";                          // signed -> SEXT
62         else
63           opcode = "zext";                         // unsigned -> ZEXT
64       } else {
65         opcode = "bitcast";                        // Same size, No-op cast
66       }
67     } else if (SrcTy.isFloatingPoint()) {          // Casting from floating pt
68       if (DstTy.isSigned()) 
69         opcode = "fptosi";                         // FP -> sint
70       else
71         opcode = "fptoui";                         // FP -> uint 
72     } else if (SrcTy.isPacked()) {
73       assert(DstBits == SrcTy.getBitWidth() &&
74                "Casting packed to integer of different width");
75         opcode = "bitcast";                        // same size, no-op cast
76     } else {
77       assert(SrcTy.isPointer() &&
78              "Casting from a value that is not first-class type");
79       opcode = "ptrtoint";                         // ptr -> int
80     }
81   } else if (DstTy.isFloatingPoint()) {           // Casting to floating pt
82     if (SrcTy.isIntegral()) {                     // Casting from integral
83       if (SrcTy.isSigned())
84         opcode = "sitofp";                         // sint -> FP
85       else
86         opcode = "uitofp";                         // uint -> FP
87     } else if (SrcTy.isFloatingPoint()) {         // Casting from floating pt
88       if (DstBits < SrcBits) {
89         opcode = "fptrunc";                        // FP -> smaller FP
90       } else if (DstBits > SrcBits) {
91         opcode = "fpext";                          // FP -> larger FP
92       } else  {
93         opcode ="bitcast";                         // same size, no-op cast
94       }
95     } else if (SrcTy.isPacked()) {
96       assert(DstBits == SrcTy.getBitWidth() &&
97              "Casting packed to floating point of different width");
98         opcode = "bitcast";                        // same size, no-op cast
99     } else {
100       assert(0 && "Casting pointer or non-first class to float");
101     }
102   } else if (DstTy.isPacked()) {
103     if (SrcTy.isPacked()) {
104       assert(DstTy.getBitWidth() == SrcTy.getBitWidth() &&
105              "Casting packed to packed of different widths");
106       opcode = "bitcast";                          // packed -> packed
107     } else if (DstTy.getBitWidth() == SrcBits) {
108       opcode = "bitcast";                          // float/int -> packed
109     } else {
110       assert(!"Illegal cast to packed (wrong type or size)");
111     }
112   } else if (DstTy.isPointer()) {
113     if (SrcTy.isPointer()) {
114       opcode = "bitcast";                          // ptr -> ptr
115     } else if (SrcTy.isIntegral()) {
116       opcode = "inttoptr";                         // int -> ptr
117     } else {
118       assert(!"Casting pointer to other than pointer or int");
119     }
120   } else {
121     assert(!"Casting to type that is not first-class");
122   }
123   return opcode;
124 }
125
126 %}
127
128 %file-prefix="UpgradeParser"
129
130 %union {
131   std::string*    String;
132   TypeInfo        Type;
133   ValueInfo       Value;
134   ConstInfo       Const;
135 }
136
137 %token <Const> ESINT64VAL EUINT64VAL SINTVAL UINTVAL FPVAL TRUETOK FALSETOK 
138 %token <Const> NULL_TOK UNDEF ZEROINITIALIZER 
139
140 %token <Type>  VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
141 %token <Type> FLOAT DOUBLE LABEL OPAQUE
142
143 %token <String> TYPE VAR_ID LABELSTR STRINGCONSTANT
144 %token <String> IMPLEMENTATION BEGINTOK ENDTOK
145 %token <String> DECLARE GLOBAL CONSTANT SECTION VOLATILE
146 %token <String> TO DOTDOTDOT CONST INTERNAL LINKONCE WEAK 
147 %token <String> DLLIMPORT DLLEXPORT EXTERN_WEAK APPENDING
148 %token <String> NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
149 %token <String> ALIGN
150 %token <String> DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
151 %token <String> CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
152 %token <String> X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
153 %token <String> DATALAYOUT
154 %token <String> RET BR SWITCH INVOKE UNWIND UNREACHABLE
155 %token <String> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
156 %token <String> SETLE SETGE SETLT SETGT SETEQ SETNE  // Binary Comparators
157 %token <String> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
158 %token <String> PHI_TOK SELECT SHL LSHR ASHR VAARG
159 %token <String> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
160 %token <String> CAST
161
162 %type <String> OptAssign OptLinkage OptCallingConv OptAlign OptCAlign 
163 %type <String> SectionString OptSection GlobalVarAttributes GlobalVarAttribute
164 %type <String> ArgTypeListI ConstExpr DefinitionList
165 %type <String> ConstPool TargetDefinition LibrariesDefinition LibList OptName
166 %type <String> ArgVal ArgListH ArgList FunctionHeaderH BEGIN FunctionHeader END
167 %type <String> Function FunctionProto BasicBlock TypeListI
168 %type <String> InstructionList BBTerminatorInst JumpTable Inst PHIList
169 %type <String> ValueRefList OptTailCall InstVal IndexList OptVolatile
170 %type <String> MemoryInst SymbolicValueRef OptSideEffect GlobalType
171 %type <String> FnDeclareLinkage BasicBlockList BigOrLittle AsmBlock
172 %type <String> Name ValueRef ValueRefListE
173 %type <String> ShiftOps SetCondOps LogicalOps ArithmeticOps ConstValueRef 
174
175 %type <String> ConstVector
176
177 %type <Type> IntType SIntType UIntType FPType TypesV Types 
178 %type <Type> PrimType UpRTypesV UpRTypes
179
180 %type <Const> IntVal EInt64Val ConstVal
181
182 %type <Value> ResolvedVal
183
184 %start Module
185
186 %%
187
188 // Handle constant integer size restriction and conversion...
189 IntVal : SINTVAL | UINTVAL 
190 EInt64Val : ESINT64VAL | EUINT64VAL;
191
192 // Operations that are notably excluded from this list include:
193 // RET, BR, & SWITCH because they end basic blocks and are treated specially.
194 ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
195 LogicalOps   : AND | OR | XOR;
196 SetCondOps   : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
197 ShiftOps     : SHL | LSHR | ASHR;
198
199 // These are some types that allow classification if we only want a particular 
200 // thing... for example, only a signed, unsigned, or integral type.
201 SIntType :  LONG |  INT |  SHORT | SBYTE;
202 UIntType : ULONG | UINT | USHORT | UBYTE;
203 IntType  : SIntType | UIntType;
204 FPType   : FLOAT | DOUBLE;
205
206 // OptAssign - Value producing statements have an optional assignment component
207 OptAssign : Name '=' {
208     *$1 += " = ";
209     $$ = $1;
210   }
211   | /*empty*/ {
212     $$ = new std::string(""); 
213   };
214
215 OptLinkage 
216   : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT 
217   | EXTERN_WEAK 
218   | /*empty*/   { $$ = new std::string(""); } ;
219
220 OptCallingConv 
221   : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK 
222   | X86_FASTCALLCC_TOK | CC_TOK EUINT64VAL
223   | /*empty*/ { $$ = new std::string(""); } ;
224
225 // OptAlign/OptCAlign - An optional alignment, and an optional alignment with
226 // a comma before it.
227 OptAlign 
228   : /*empty*/        { $$ = new std::string(); }
229   | ALIGN EUINT64VAL { *$1 += " " + *$2.cnst; delete $2.cnst; $$ = $1; };
230          ;
231 OptCAlign 
232   : /*empty*/            { $$ = new std::string(); } 
233   | ',' ALIGN EUINT64VAL { 
234     $2->insert(0, ", "); 
235     *$2 += " " + *$3.cnst;
236     delete $3.cnst;
237     $$ = $2;
238   };
239
240 SectionString 
241   : SECTION STRINGCONSTANT { 
242     *$1 += " " + *$2;
243     delete $2;
244     $$ = $1;
245   };
246
247 OptSection : /*empty*/     { $$ = new std::string(); } 
248            | SectionString;
249
250 GlobalVarAttributes 
251     : /* empty */ { $$ = new std::string(); } 
252     | ',' GlobalVarAttribute GlobalVarAttributes  {
253       $2->insert(0, ", ");
254       if (!$3->empty())
255         *$2 += " " + *$3;
256       delete $3;
257       $$ = $2;
258     };
259
260 GlobalVarAttribute 
261     : SectionString 
262     | ALIGN EUINT64VAL {
263       *$1 += " " + *$2.cnst;
264       delete $2.cnst;
265       $$ = $1;
266     };
267
268 //===----------------------------------------------------------------------===//
269 // Types includes all predefined types... except void, because it can only be
270 // used in specific contexts (function returning void for example).  To have
271 // access to it, a user must explicitly use TypesV.
272 //
273
274 // TypesV includes all of 'Types', but it also includes the void type.
275 TypesV    : Types    | VOID ;
276 UpRTypesV : UpRTypes | VOID ; 
277 Types     : UpRTypes ;
278
279 // Derived types are added later...
280 //
281 PrimType : BOOL | SBYTE | UBYTE | SHORT  | USHORT | INT   | UINT ;
282 PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
283 UpRTypes : OPAQUE | PrimType | SymbolicValueRef   { 
284     $$.newTy = $1; };
285
286 // Include derived types in the Types production.
287 //
288 UpRTypes : '\\' EUINT64VAL {                   // Type UpReference
289     $2.cnst->insert(0, "\\");
290     $$.newTy = $2.cnst;
291     $$.oldTy = OpaqueTy;
292   }
293   | UpRTypesV '(' ArgTypeListI ')' {           // Function derived type?
294     *$1.newTy += "( " + *$3 + " )";
295     delete $3;
296     $$.newTy = $1.newTy;
297     $$.oldTy = FunctionTy;
298   }
299   | '[' EUINT64VAL 'x' UpRTypes ']' {          // Sized array type?
300     $2.cnst->insert(0,"[ ");
301     *$2.cnst += " x " + *$4.newTy + " ]";
302     delete $4.newTy;
303     $$.newTy = $2.cnst;
304     $$.oldTy = ArrayTy;
305   }
306   | '<' EUINT64VAL 'x' UpRTypes '>' {          // Packed array type?
307     $2.cnst->insert(0,"< ");
308     *$2.cnst += " x " + *$4.newTy + " >";
309     delete $4.newTy;
310     $$.newTy = $2.cnst;
311     $$.oldTy = PackedTy;
312   }
313   | '{' TypeListI '}' {                        // Structure type?
314     $2->insert(0, "{ ");
315     *$2 += " }";
316     $$.newTy = $2;
317     $$.oldTy = StructTy;
318   }
319   | '{' '}' {                                  // Empty structure type?
320     $$.newTy = new std::string("{ }");
321     $$.oldTy = StructTy;
322   }
323   | UpRTypes '*' {                             // Pointer type?
324     *$1.newTy += '*';
325     $1.oldTy = PointerTy;
326     $$ = $1;
327   };
328
329 // TypeList - Used for struct declarations and as a basis for function type 
330 // declaration type lists
331 //
332 TypeListI 
333   : UpRTypes {
334     $$ = $1.newTy;
335   }
336   | TypeListI ',' UpRTypes {
337     *$1 += ", " + *$3.newTy;
338     delete $3.newTy;
339     $$ = $1;
340   };
341
342 // ArgTypeList - List of types for a function type declaration...
343 ArgTypeListI 
344   : TypeListI 
345   | TypeListI ',' DOTDOTDOT {
346     *$1 += ", ...";
347     delete $3;
348     $$ = $1;
349   }
350   | DOTDOTDOT {
351     $$ = $1;
352   }
353   | /*empty*/ {
354     $$ = new std::string();
355   };
356
357 // ConstVal - The various declarations that go into the constant pool.  This
358 // production is used ONLY to represent constants that show up AFTER a 'const',
359 // 'constant' or 'global' token at global scope.  Constants that can be inlined
360 // into other expressions (such as integers and constexprs) are handled by the
361 // ResolvedVal, ValueRef and ConstValueRef productions.
362 //
363 ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
364     $$.type = $1;
365     $$.cnst = new std::string(*$1.newTy);
366     *$$.cnst += " [ " + *$3 + " ]";
367     delete $3;
368   }
369   | Types '[' ']' {
370     $$.type = $1;
371     $$.cnst = new std::string(*$1.newTy);
372     *$$.cnst += "[ ]";
373   }
374   | Types 'c' STRINGCONSTANT {
375     $$.type = $1;
376     $$.cnst = new std::string(*$1.newTy);
377     *$$.cnst += " c" + *$3;
378     delete $3;
379   }
380   | Types '<' ConstVector '>' { // Nonempty unsized arr
381     $$.type = $1;
382     $$.cnst = new std::string(*$1.newTy);
383     *$$.cnst += " < " + *$3 + " >";
384     delete $3;
385   }
386   | Types '{' ConstVector '}' {
387     $$.type = $1;
388     $$.cnst = new std::string(*$1.newTy);
389     *$$.cnst += " { " + *$3 + " }";
390     delete $3;
391   }
392   | Types '{' '}' {
393     $$.type = $1;
394     $$.cnst = new std::string(*$1.newTy);
395     *$$.cnst += " [ ]";
396   }
397   | Types NULL_TOK {
398     $$.type = $1;
399     $$.cnst = new std::string(*$1.newTy);
400     *$$.cnst +=  " " + *$2.cnst;
401     $2.destroy();
402   }
403   | Types UNDEF {
404     $$.type = $1;
405     $$.cnst = new std::string(*$1.newTy);
406     *$$.cnst += " " + *$2.cnst;
407     $2.destroy();
408   }
409   | Types SymbolicValueRef {
410     $$.type = $1;
411     $$.cnst = new std::string(*$1.newTy);
412     *$$.cnst += " " + *$2;
413     delete $2;
414   }
415   | Types ConstExpr {
416     $$.type = $1;
417     $$.cnst = new std::string(*$1.newTy);
418     *$$.cnst += " " + *$2;
419     delete $2;
420   }
421   | Types ZEROINITIALIZER {
422     $$.type = $1;
423     $$.cnst = new std::string(*$1.newTy);
424     *$$.cnst += " " + *$2.cnst;
425     $2.destroy();
426   }
427   | SIntType EInt64Val {      // integral constants
428     $$.type = $1;
429     $$.cnst = new std::string(*$1.newTy);
430     *$$.cnst += " " + *$2.cnst;
431     $2.destroy();
432   }
433   | UIntType EUINT64VAL {            // integral constants
434     $$.type = $1;
435     $$.cnst = new std::string(*$1.newTy);
436     *$$.cnst += " " + *$2.cnst;
437     $2.destroy();
438   }
439   | BOOL TRUETOK {                      // Boolean constants
440     $$.type = $1;
441     $$.cnst = new std::string(*$1.newTy);
442     *$$.cnst += " " + *$2.cnst;
443     $2.destroy();
444   }
445   | BOOL FALSETOK {                     // Boolean constants
446     $$.type = $1;
447     $$.cnst = new std::string(*$1.newTy);
448     *$$.cnst += " " + *$2.cnst;
449     $2.destroy();
450   }
451   | FPType FPVAL {                   // Float & Double constants
452     $$.type = $1;
453     $$.cnst = new std::string(*$1.newTy);
454     *$$.cnst += " " + *$2.cnst;
455     $2.destroy();
456   };
457
458
459 ConstExpr: CAST '(' ConstVal TO Types ')' {
460     // We must infer the cast opcode from the types of the operands. 
461     const char *opcode = getCastOpcode($3.type, $5);
462     $$ = new std::string(opcode);
463     *$$ += "(" + *$3.cnst + " " + *$4 + " " + *$5.newTy + ")";
464     delete $1; $3.destroy(); delete $4; $5.destroy();
465   }
466   | GETELEMENTPTR '(' ConstVal IndexList ')' {
467     *$1 += "(" + *$3.cnst + " " + *$4 + ")";
468     $$ = $1;
469     $3.destroy();
470     delete $4;
471   }
472   | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
473     *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
474     $3.destroy(); $5.destroy(); $7.destroy();
475     $$ = $1;
476   }
477   | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
478     *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
479     $3.destroy(); $5.destroy();
480     $$ = $1;
481   }
482   | LogicalOps '(' ConstVal ',' ConstVal ')' {
483     *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
484     $3.destroy(); $5.destroy();
485     $$ = $1;
486   }
487   | SetCondOps '(' ConstVal ',' ConstVal ')' {
488     *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
489     $3.destroy(); $5.destroy();
490     $$ = $1;
491   }
492   | ShiftOps '(' ConstVal ',' ConstVal ')' {
493     *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
494     $3.destroy(); $5.destroy();
495     $$ = $1;
496   }
497   | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
498     *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
499     $3.destroy(); $5.destroy();
500     $$ = $1;
501   }
502   | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
503     *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
504     $3.destroy(); $5.destroy(); $7.destroy();
505     $$ = $1;
506   }
507   | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
508     *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
509     $3.destroy(); $5.destroy(); $7.destroy();
510     $$ = $1;
511   };
512
513
514 // ConstVector - A list of comma separated constants.
515
516 ConstVector 
517   : ConstVector ',' ConstVal {
518     *$1 += ", " + *$3.cnst;
519     $3.destroy();
520     $$ = $1;
521   }
522   | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
523   ;
524
525
526 // GlobalType - Match either GLOBAL or CONSTANT for global declarations...
527 GlobalType : GLOBAL | CONSTANT ;
528
529
530 //===----------------------------------------------------------------------===//
531 //                             Rules to match Modules
532 //===----------------------------------------------------------------------===//
533
534 // Module rule: Capture the result of parsing the whole file into a result
535 // variable...
536 //
537 Module : DefinitionList {
538 };
539
540 // DefinitionList - Top level definitions
541 //
542 DefinitionList : DefinitionList Function {
543     $$ = 0;
544   } 
545   | DefinitionList FunctionProto {
546     *O << *$2 << "\n";
547     delete $2;
548     $$ = 0;
549   }
550   | DefinitionList MODULE ASM_TOK AsmBlock {
551     *O << "module asm " << " " << *$4 << "\n";
552   }  
553   | DefinitionList IMPLEMENTATION {
554     *O << "implementation\n";
555   }
556   | ConstPool {
557   };
558
559 // ConstPool - Constants with optional names assigned to them.
560 ConstPool : ConstPool OptAssign TYPE TypesV {
561     *O << *$2 << " " << *$3 << " " << *$4.newTy << "\n";
562     // delete $2; delete $3; $4.destroy();
563     $$ = 0;
564   }
565   | ConstPool FunctionProto {       // Function prototypes can be in const pool
566     *O << *$2 << "\n";
567     delete $2;
568     $$ = 0;
569   }
570   | ConstPool MODULE ASM_TOK AsmBlock {  // Asm blocks can be in the const pool
571     *O << *$2 << " " << *$3 << " " << *$4 << "\n";
572     delete $2; delete $3; delete $4; 
573     $$ = 0;
574   }
575   | ConstPool OptAssign OptLinkage GlobalType ConstVal  GlobalVarAttributes {
576     *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.cnst << " " 
577        << *$6 << "\n";
578     delete $2; delete $3; delete $4; $5.destroy(); delete $6; 
579     $$ = 0;
580   }
581   | ConstPool OptAssign EXTERNAL GlobalType Types  GlobalVarAttributes {
582     *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.newTy 
583        << " " << *$6 << "\n";
584     delete $2; delete $3; delete $4; $5.destroy(); delete $6;
585     $$ = 0;
586   }
587   | ConstPool OptAssign DLLIMPORT GlobalType Types  GlobalVarAttributes {
588     *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.newTy 
589        << " " << *$6 << "\n";
590     delete $2; delete $3; delete $4; $5.destroy(); delete $6;
591     $$ = 0;
592   }
593   | ConstPool OptAssign EXTERN_WEAK GlobalType Types  GlobalVarAttributes {
594     *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.newTy 
595        << " " << *$6 << "\n";
596     delete $2; delete $3; delete $4; $5.destroy(); delete $6;
597     $$ = 0;
598   }
599   | ConstPool TARGET TargetDefinition { 
600     *O << *$2 << " " << *$3 << "\n";
601     delete $2; delete $3;
602     $$ = 0;
603   }
604   | ConstPool DEPLIBS '=' LibrariesDefinition {
605     *O << *$2 << " = " << *$4 << "\n";
606     delete $2; delete $4;
607     $$ = 0;
608   }
609   | /* empty: end of list */ { 
610     $$ = 0;
611   };
612
613
614 AsmBlock : STRINGCONSTANT ;
615
616 BigOrLittle : BIG | LITTLE 
617
618 TargetDefinition 
619   : ENDIAN '=' BigOrLittle {
620     *$1 += " = " + *$3;
621     delete $3;
622     $$ = $1;
623   }
624   | POINTERSIZE '=' EUINT64VAL {
625     *$1 += " = " + *$3.cnst;
626     if (*$3.cnst == "64")
627       SizeOfPointer = 64;
628     $3.destroy();
629     $$ = $1;
630   }
631   | TRIPLE '=' STRINGCONSTANT {
632     *$1 += " = " + *$3;
633     delete $3;
634     $$ = $1;
635   }
636   | DATALAYOUT '=' STRINGCONSTANT {
637     *$1 += " = " + *$3;
638     delete $3;
639     $$ = $1;
640   };
641
642 LibrariesDefinition 
643   : '[' LibList ']' {
644     $2->insert(0, "[ ");
645     *$2 += " ]";
646     $$ = $2;
647   };
648
649 LibList 
650   : LibList ',' STRINGCONSTANT {
651     *$1 += ", " + *$3;
652     delete $3;
653     $$ = $1;
654   }
655   | STRINGCONSTANT 
656   | /* empty: end of list */ {
657     $$ = new std::string();
658   };
659
660 //===----------------------------------------------------------------------===//
661 //                       Rules to match Function Headers
662 //===----------------------------------------------------------------------===//
663
664 Name : VAR_ID | STRINGCONSTANT;
665 OptName : Name | /*empty*/ { $$ = new std::string(); };
666
667 ArgVal : Types OptName {
668   $$ = $1.newTy;
669   if (!$2->empty())
670     *$$ += " " + *$2;
671   delete $2;
672 };
673
674 ArgListH : ArgListH ',' ArgVal {
675     *$1 += ", " + *$3;
676     delete $3;
677   }
678   | ArgVal {
679     $$ = $1;
680   };
681
682 ArgList : ArgListH {
683     $$ = $1;
684   }
685   | ArgListH ',' DOTDOTDOT {
686     *$1 += ", ...";
687     $$ = $1;
688     delete $3;
689   }
690   | DOTDOTDOT {
691     $$ = $1;
692   }
693   | /* empty */ {
694     $$ = new std::string();
695   };
696
697 FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')' 
698                   OptSection OptAlign {
699     if (!$1->empty()) {
700       *$1 += " ";
701     }
702     *$1 += *$2.newTy + " " + *$3 + "(" + *$5 + ")";
703     if (!$7->empty()) {
704       *$1 += " " + *$7;
705     }
706     if (!$8->empty()) {
707       *$1 += " " + *$8;
708     }
709     $2.destroy();
710     delete $3;
711     delete $5;
712     delete $7;
713     delete $8;
714     $$ = $1;
715   };
716
717 BEGIN : BEGINTOK {
718     $$ = new std::string("begin");
719   }
720   | '{' { 
721     $$ = new std::string ("{");
722   }
723
724 FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
725   if (!$1->empty()) {
726     *O << *$1 << " ";
727   }
728   *O << *$2 << " " << *$3 << "\n";
729   delete $1; delete $2; delete $3;
730   $$ = 0;
731 };
732
733 END : ENDTOK { $$ = new std::string("end"); }
734     | '}' { $$ = new std::string("}"); };
735
736 Function : FunctionHeader BasicBlockList END {
737   if ($2)
738     *O << *$2;
739   *O << '\n' << *$3 << "\n";
740   $$ = 0;
741 };
742
743 FnDeclareLinkage
744   : /*default*/ { $$ = new std::string(); }
745   | DLLIMPORT    
746   | EXTERN_WEAK 
747   ;
748   
749 FunctionProto 
750   : DECLARE FnDeclareLinkage FunctionHeaderH { 
751     if (!$2->empty())
752       *$1 += " " + *$2;
753     *$1 += " " + *$3;
754     delete $2;
755     delete $3;
756     $$ = $1;
757   };
758
759 //===----------------------------------------------------------------------===//
760 //                        Rules to match Basic Blocks
761 //===----------------------------------------------------------------------===//
762
763 OptSideEffect : /* empty */ {
764   }
765   | SIDEEFFECT {
766   };
767
768 ConstValueRef 
769   : ESINT64VAL       { $$ = $1.cnst; }
770   | EUINT64VAL       { $$ = $1.cnst; }
771   | FPVAL            { $$ = $1.cnst; }
772   | TRUETOK          { $$ = $1.cnst; }
773   | FALSETOK         { $$ = $1.cnst; }
774   | NULL_TOK         { $$ = $1.cnst; }
775   | UNDEF            { $$ = $1.cnst; }
776   | ZEROINITIALIZER  { $$ = $1.cnst; }
777   | '<' ConstVector '>' { 
778     $2->insert(0, "<");
779     *$2 += ">";
780     $$ = $2;
781   }
782   | ConstExpr 
783   | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
784     if (!$2->empty()) {
785       *$1 += " " + *$2;
786     }
787     *$1 += " " + *$3 + ", " + *$5;
788     delete $2; delete $3; delete $5;
789     $$ = $1;
790   };
791
792 SymbolicValueRef : IntVal { $$ = $1.cnst; } | Name ;
793
794 // ValueRef - A reference to a definition... either constant or symbolic
795 ValueRef : SymbolicValueRef | ConstValueRef;
796
797
798 // ResolvedVal - a <type> <value> pair.  This is used only in cases where the
799 // type immediately preceeds the value reference, and allows complex constant
800 // pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
801 ResolvedVal : Types ValueRef {
802     $$.type = $1;
803     $$.val = new std::string(*$1.newTy + " ");
804     *$$.val += *$2;
805     delete $2;
806   };
807
808 BasicBlockList : BasicBlockList BasicBlock {
809   }
810   | BasicBlock { // Do not allow functions with 0 basic blocks   
811   };
812
813
814 // Basic blocks are terminated by branching instructions: 
815 // br, br/cc, switch, ret
816 //
817 BasicBlock : InstructionList OptAssign BBTerminatorInst  {
818     *O << *$2 ;
819   };
820
821 InstructionList : InstructionList Inst {
822     *O << "    " << *$2 << "\n";
823     delete $2;
824     $$ = 0;
825   }
826   | /* empty */ {
827     $$ = 0;
828   }
829   | LABELSTR {
830     *O << *$1 << "\n";
831     delete $1;
832     $$ = 0;
833   };
834
835 BBTerminatorInst : RET ResolvedVal {              // Return with a result...
836     *O << "    " << *$1 << " " << *$2.val << "\n";
837     delete $1; $2.destroy();
838     $$ = 0;
839   }
840   | RET VOID {                                       // Return with no result...
841     *O << "    " << *$1 << " " << *$2.newTy << "\n";
842     delete $1; $2.destroy();
843     $$ = 0;
844   }
845   | BR LABEL ValueRef {                         // Unconditional Branch...
846     *O << "    " << *$1 << " " << *$2.newTy << " " << *$3 << "\n";
847     delete $1; $2.destroy(); delete $3;
848     $$ = 0;
849   }                                                  // Conditional Branch...
850   | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {  
851     *O << "    " << *$1 << " " << *$2.newTy << " " << *$3 << ", " 
852        << *$5.newTy << " " << *$6 << ", " << *$8.newTy << " " << *$9 << "\n";
853     delete $1; $2.destroy(); delete $3; $5.destroy(); delete $6; 
854     $8.destroy(); delete $9;
855     $$ = 0;
856   }
857   | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
858     *O << "    " << *$1 << " " << *$2.newTy << " " << *$3 << ", " << *$5.newTy 
859        << " " << *$6 << " [" << *$8 << " ]\n";
860     delete $1; $2.destroy(); delete $3; $5.destroy(); delete $6; delete $8;
861     $$ = 0;
862   }
863   | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
864     *O << "    " << *$1 << " " << *$2.newTy << " " << *$3 << ", " 
865        << *$5.newTy << " " << *$6 << "[]\n";
866     delete $1; $2.destroy(); delete $3; $5.destroy(); delete $6;
867     $$ = 0;
868   }
869   | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
870     TO LABEL ValueRef UNWIND LABEL ValueRef {
871     *O << "    " << *$1 << " " << *$2 << " " << *$3.newTy << " " << *$4 << " ("
872        << *$6 << ") " << *$8 << " " << *$9.newTy << " " << *$10 << " " 
873        << *$11 << " " << *$12.newTy << " " << *$13 << "\n";
874     delete $1; delete $2; $3.destroy(); delete $4; delete $6; delete $8; 
875     $9.destroy(); delete $10; delete $11; $12.destroy(); delete $13; 
876     $$ = 0;
877   }
878   | UNWIND {
879     *O << "    " << *$1 << "\n";
880     delete $1;
881     $$ = 0;
882   }
883   | UNREACHABLE {
884     *O << "    " << *$1 << "\n";
885     delete $1;
886     $$ = 0;
887   };
888
889 JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
890     *$1 += *$2.newTy + " " + *$3 + ", " + *$5.newTy + " " + *$6;
891     $2.destroy(); delete $3; $5.destroy(); delete $6;
892     $$ = $1;
893   }
894   | IntType ConstValueRef ',' LABEL ValueRef {
895     $2->insert(0, *$1.newTy + " " );
896     *$2 += ", " + *$4.newTy + " " + *$5;
897     $1.destroy(); $4.destroy(); delete $5;
898     $$ = $2;
899   };
900
901 Inst 
902   : OptAssign InstVal {
903     *$1 += *$2;
904     delete $2;
905     $$ = $1; 
906   };
907
908 PHIList 
909   : Types '[' ValueRef ',' ValueRef ']' {    // Used for PHI nodes
910     $3->insert(0, *$1.newTy + "[");
911     *$3 += "," + *$5 + "]";
912     $1.destroy(); delete $5;
913     $$ = $3;
914   }
915   | PHIList ',' '[' ValueRef ',' ValueRef ']' {
916     *$1 += ", [" + *$4 + "," + *$6 + "]";
917     delete $4; delete $6;
918     $$ = $1;
919   };
920
921
922 ValueRefList 
923   : ResolvedVal { $$ = new std::string(*$1.val); $1.destroy(); }
924   | ValueRefList ',' ResolvedVal {
925     *$1 += ", " + *$3.val;
926     $3.destroy();
927     $$ = $1;
928   };
929
930 // ValueRefListE - Just like ValueRefList, except that it may also be empty!
931 ValueRefListE 
932   : ValueRefList 
933   | /*empty*/ { $$ = new std::string(); }
934   ;
935
936 OptTailCall 
937   : TAIL CALL {
938     *$1 += " " + *$2;
939     delete $2;
940     $$ = $1;
941   }
942   | CALL 
943   ;
944
945 InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
946     *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5;
947     $2.destroy(); delete $3; delete $5;
948     $$ = $1;
949   }
950   | LogicalOps Types ValueRef ',' ValueRef {
951     *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5;
952     $2.destroy(); delete $3; delete $5;
953     $$ = $1;
954   }
955   | SetCondOps Types ValueRef ',' ValueRef {
956     *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5;
957     $2.destroy(); delete $3; delete $5;
958     $$ = $1;
959   }
960   | NOT ResolvedVal {
961     *$1 += " " + *$2.val;
962     $2.destroy();
963     $$ = $1;
964   }
965   | ShiftOps ResolvedVal ',' ResolvedVal {
966     *$1 += " " + *$2.val + ", " + *$4.val;
967     $2.destroy(); $4.destroy();
968     $$ = $1;
969   }
970   | CAST ResolvedVal TO Types {
971     const char *opcode = getCastOpcode($2.type, $4);
972     $$ = new std::string(opcode);
973     *$$ += *$2.val + " " + *$3 + " " + *$4.newTy; 
974     delete $1; $2.destroy();
975     delete $3; $4.destroy();
976   }
977   | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
978     *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
979     $2.destroy(); $4.destroy(); $6.destroy();
980     $$ = $1;
981   }
982   | VAARG ResolvedVal ',' Types {
983     *$1 += " " + *$2.val + ", " + *$4.newTy;
984     $2.destroy(); $4.destroy();
985     $$ = $1;
986   }
987   | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
988     *$1 += " " + *$2.val + ", " + *$4.val;
989     $2.destroy(); $4.destroy();
990     $$ = $1;
991   }
992   | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
993     *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
994     $2.destroy(); $4.destroy(); $6.destroy();
995     $$ = $1;
996   }
997   | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
998     *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
999     $2.destroy(); $4.destroy(); $6.destroy();
1000     $$ = $1;
1001   }
1002   | PHI_TOK PHIList {
1003     *$1 += " " + *$2;
1004     delete $2;
1005     $$ = $1;
1006   }
1007   | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')'  {
1008     if (!$2->empty())
1009       *$1 += " " + *$2;
1010     if (!$1->empty())
1011       *$1 += " ";
1012     *$1 += *$3.newTy + " " + *$4 + "(" + *$6 + ")";
1013     delete $2; $3.destroy(); delete $4; delete $6;
1014     $$ = $1;
1015   }
1016   | MemoryInst ;
1017
1018
1019 // IndexList - List of indices for GEP based instructions...
1020 IndexList 
1021   : ',' ValueRefList { 
1022     $2->insert(0, ", ");
1023     $$ = $2;
1024   } 
1025   | /* empty */ {  $$ = new std::string(); }
1026   ;
1027
1028 OptVolatile 
1029   : VOLATILE 
1030   | /* empty */ { $$ = new std::string(); }
1031   ;
1032
1033 MemoryInst : MALLOC Types OptCAlign {
1034     *$1 += " " + *$2.newTy;
1035     if (!$3->empty())
1036       *$1 += " " + *$3;
1037     $2.destroy(); delete $3;
1038     $$ = $1;
1039   }
1040   | MALLOC Types ',' UINT ValueRef OptCAlign {
1041     *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5;
1042     if (!$6->empty())
1043       *$1 += " " + *$6;
1044     $2.destroy(); $4.destroy(); delete $5; delete $6;
1045     $$ = $1;
1046   }
1047   | ALLOCA Types OptCAlign {
1048     *$1 += " " + *$2.newTy;
1049     if (!$3->empty())
1050       *$1 += " " + *$3;
1051     $2.destroy(); delete $3;
1052     $$ = $1;
1053   }
1054   | ALLOCA Types ',' UINT ValueRef OptCAlign {
1055     *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5;
1056     if (!$6->empty())
1057       *$1 += " " + *$6;
1058     $2.destroy(); $4.destroy(); delete $5; delete $6;
1059     $$ = $1;
1060   }
1061   | FREE ResolvedVal {
1062     *$1 += " " + *$2.val;
1063     $2.destroy();
1064     $$ = $1;
1065   }
1066   | OptVolatile LOAD Types ValueRef {
1067     if (!$1->empty())
1068       *$1 += " ";
1069     *$1 += *$2 + " " + *$3.newTy + " " + *$4;
1070     delete $2; $3.destroy(); delete $4;
1071     $$ = $1;
1072   }
1073   | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1074     if (!$1->empty())
1075       *$1 += " ";
1076     *$1 += *$2 + " " + *$3.val + ", " + *$5.newTy + " " + *$6;
1077     delete $2; $3.destroy(); $5.destroy(); delete $6;
1078     $$ = $1;
1079   }
1080   | GETELEMENTPTR Types ValueRef IndexList {
1081     *$1 += *$2.newTy + " " + *$3 + " " + *$4;
1082     $2.destroy(); delete $3; delete $4;
1083     $$ = $1;
1084   };
1085
1086 %%
1087
1088 int yyerror(const char *ErrorMsg) {
1089   std::string where 
1090     = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1091                   + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
1092   std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
1093   if (yychar == YYEMPTY || yychar == 0)
1094     errMsg += "end-of-file.";
1095   else
1096     errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
1097   std::cerr << errMsg << '\n';
1098   exit(1);
1099 }