valgrind clean version of llvm-upgrade
[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 
284          | SymbolicValueRef { 
285            $$.newTy = $1; $$.oldTy = OpaqueTy;
286          };
287
288 // Include derived types in the Types production.
289 //
290 UpRTypes : '\\' EUINT64VAL {                   // Type UpReference
291     $2.cnst->insert(0, "\\");
292     $$.newTy = $2.cnst;
293     $$.oldTy = OpaqueTy;
294   }
295   | UpRTypesV '(' ArgTypeListI ')' {           // Function derived type?
296     *$1.newTy += "( " + *$3 + " )";
297     delete $3;
298     $$.newTy = $1.newTy;
299     $$.oldTy = FunctionTy;
300   }
301   | '[' EUINT64VAL 'x' UpRTypes ']' {          // Sized array type?
302     $2.cnst->insert(0,"[ ");
303     *$2.cnst += " x " + *$4.newTy + " ]";
304     delete $4.newTy;
305     $$.newTy = $2.cnst;
306     $$.oldTy = ArrayTy;
307   }
308   | '<' EUINT64VAL 'x' UpRTypes '>' {          // Packed array type?
309     $2.cnst->insert(0,"< ");
310     *$2.cnst += " x " + *$4.newTy + " >";
311     delete $4.newTy;
312     $$.newTy = $2.cnst;
313     $$.oldTy = PackedTy;
314   }
315   | '{' TypeListI '}' {                        // Structure type?
316     $2->insert(0, "{ ");
317     *$2 += " }";
318     $$.newTy = $2;
319     $$.oldTy = StructTy;
320   }
321   | '{' '}' {                                  // Empty structure type?
322     $$.newTy = new std::string("{ }");
323     $$.oldTy = StructTy;
324   }
325   | UpRTypes '*' {                             // Pointer type?
326     *$1.newTy += '*';
327     $1.oldTy = PointerTy;
328     $$ = $1;
329   };
330
331 // TypeList - Used for struct declarations and as a basis for function type 
332 // declaration type lists
333 //
334 TypeListI 
335   : UpRTypes {
336     $$ = $1.newTy;
337   }
338   | TypeListI ',' UpRTypes {
339     *$1 += ", " + *$3.newTy;
340     delete $3.newTy;
341     $$ = $1;
342   };
343
344 // ArgTypeList - List of types for a function type declaration...
345 ArgTypeListI 
346   : TypeListI 
347   | TypeListI ',' DOTDOTDOT {
348     *$1 += ", ...";
349     delete $3;
350     $$ = $1;
351   }
352   | DOTDOTDOT {
353     $$ = $1;
354   }
355   | /*empty*/ {
356     $$ = new std::string();
357   };
358
359 // ConstVal - The various declarations that go into the constant pool.  This
360 // production is used ONLY to represent constants that show up AFTER a 'const',
361 // 'constant' or 'global' token at global scope.  Constants that can be inlined
362 // into other expressions (such as integers and constexprs) are handled by the
363 // ResolvedVal, ValueRef and ConstValueRef productions.
364 //
365 ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
366     $$.type = $1;
367     $$.cnst = new std::string(*$1.newTy);
368     *$$.cnst += " [ " + *$3 + " ]";
369     delete $3;
370   }
371   | Types '[' ']' {
372     $$.type = $1;
373     $$.cnst = new std::string(*$1.newTy);
374     *$$.cnst += "[ ]";
375   }
376   | Types 'c' STRINGCONSTANT {
377     $$.type = $1;
378     $$.cnst = new std::string(*$1.newTy);
379     *$$.cnst += " c" + *$3;
380     delete $3;
381   }
382   | Types '<' ConstVector '>' { // Nonempty unsized arr
383     $$.type = $1;
384     $$.cnst = new std::string(*$1.newTy);
385     *$$.cnst += " < " + *$3 + " >";
386     delete $3;
387   }
388   | Types '{' ConstVector '}' {
389     $$.type = $1;
390     $$.cnst = new std::string(*$1.newTy);
391     *$$.cnst += " { " + *$3 + " }";
392     delete $3;
393   }
394   | Types '{' '}' {
395     $$.type = $1;
396     $$.cnst = new std::string(*$1.newTy);
397     *$$.cnst += " [ ]";
398   }
399   | Types NULL_TOK {
400     $$.type = $1;
401     $$.cnst = new std::string(*$1.newTy);
402     *$$.cnst +=  " " + *$2.cnst;
403     $2.destroy();
404   }
405   | Types UNDEF {
406     $$.type = $1;
407     $$.cnst = new std::string(*$1.newTy);
408     *$$.cnst += " " + *$2.cnst;
409     $2.destroy();
410   }
411   | Types SymbolicValueRef {
412     $$.type = $1;
413     $$.cnst = new std::string(*$1.newTy);
414     *$$.cnst += " " + *$2;
415     delete $2;
416   }
417   | Types ConstExpr {
418     $$.type = $1;
419     $$.cnst = new std::string(*$1.newTy);
420     *$$.cnst += " " + *$2;
421     delete $2;
422   }
423   | Types ZEROINITIALIZER {
424     $$.type = $1;
425     $$.cnst = new std::string(*$1.newTy);
426     *$$.cnst += " " + *$2.cnst;
427     $2.destroy();
428   }
429   | SIntType EInt64Val {      // integral constants
430     $$.type = $1;
431     $$.cnst = new std::string(*$1.newTy);
432     *$$.cnst += " " + *$2.cnst;
433     $2.destroy();
434   }
435   | UIntType EUINT64VAL {            // integral constants
436     $$.type = $1;
437     $$.cnst = new std::string(*$1.newTy);
438     *$$.cnst += " " + *$2.cnst;
439     $2.destroy();
440   }
441   | BOOL TRUETOK {                      // Boolean constants
442     $$.type = $1;
443     $$.cnst = new std::string(*$1.newTy);
444     *$$.cnst += " " + *$2.cnst;
445     $2.destroy();
446   }
447   | BOOL FALSETOK {                     // Boolean constants
448     $$.type = $1;
449     $$.cnst = new std::string(*$1.newTy);
450     *$$.cnst += " " + *$2.cnst;
451     $2.destroy();
452   }
453   | FPType FPVAL {                   // Float & Double constants
454     $$.type = $1;
455     $$.cnst = new std::string(*$1.newTy);
456     *$$.cnst += " " + *$2.cnst;
457     $2.destroy();
458   };
459
460
461 ConstExpr: CAST '(' ConstVal TO Types ')' {
462     // We must infer the cast opcode from the types of the operands. 
463     const char *opcode = getCastOpcode($3.type, $5);
464     $$ = new std::string(opcode);
465     *$$ += "(" + *$3.cnst + " " + *$4 + " " + *$5.newTy + ")";
466     delete $1; $3.destroy(); delete $4; $5.destroy();
467   }
468   | GETELEMENTPTR '(' ConstVal IndexList ')' {
469     *$1 += "(" + *$3.cnst + " " + *$4 + ")";
470     $$ = $1;
471     $3.destroy();
472     delete $4;
473   }
474   | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
475     *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
476     $3.destroy(); $5.destroy(); $7.destroy();
477     $$ = $1;
478   }
479   | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
480     *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
481     $3.destroy(); $5.destroy();
482     $$ = $1;
483   }
484   | LogicalOps '(' ConstVal ',' ConstVal ')' {
485     *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
486     $3.destroy(); $5.destroy();
487     $$ = $1;
488   }
489   | SetCondOps '(' ConstVal ',' ConstVal ')' {
490     *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
491     $3.destroy(); $5.destroy();
492     $$ = $1;
493   }
494   | ShiftOps '(' ConstVal ',' ConstVal ')' {
495     *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
496     $3.destroy(); $5.destroy();
497     $$ = $1;
498   }
499   | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
500     *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
501     $3.destroy(); $5.destroy();
502     $$ = $1;
503   }
504   | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
505     *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
506     $3.destroy(); $5.destroy(); $7.destroy();
507     $$ = $1;
508   }
509   | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
510     *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
511     $3.destroy(); $5.destroy(); $7.destroy();
512     $$ = $1;
513   };
514
515
516 // ConstVector - A list of comma separated constants.
517
518 ConstVector 
519   : ConstVector ',' ConstVal {
520     *$1 += ", " + *$3.cnst;
521     $3.destroy();
522     $$ = $1;
523   }
524   | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
525   ;
526
527
528 // GlobalType - Match either GLOBAL or CONSTANT for global declarations...
529 GlobalType : GLOBAL | CONSTANT ;
530
531
532 //===----------------------------------------------------------------------===//
533 //                             Rules to match Modules
534 //===----------------------------------------------------------------------===//
535
536 // Module rule: Capture the result of parsing the whole file into a result
537 // variable...
538 //
539 Module : DefinitionList {
540 };
541
542 // DefinitionList - Top level definitions
543 //
544 DefinitionList : DefinitionList Function {
545     $$ = 0;
546   } 
547   | DefinitionList FunctionProto {
548     *O << *$2 << "\n";
549     delete $2;
550     $$ = 0;
551   }
552   | DefinitionList MODULE ASM_TOK AsmBlock {
553     *O << "module asm " << " " << *$4 << "\n";
554     $$ = 0;
555   }  
556   | DefinitionList IMPLEMENTATION {
557     *O << "implementation\n";
558     $$ = 0;
559   }
560   | ConstPool;
561
562 // ConstPool - Constants with optional names assigned to them.
563 ConstPool : ConstPool OptAssign TYPE TypesV {
564     *O << *$2 << " " << *$3 << " " << *$4.newTy << "\n";
565     // delete $2; delete $3; $4.destroy();
566     $$ = 0;
567   }
568   | ConstPool FunctionProto {       // Function prototypes can be in const pool
569     *O << *$2 << "\n";
570     delete $2;
571     $$ = 0;
572   }
573   | ConstPool MODULE ASM_TOK AsmBlock {  // Asm blocks can be in the const pool
574     *O << *$2 << " " << *$3 << " " << *$4 << "\n";
575     delete $2; delete $3; delete $4; 
576     $$ = 0;
577   }
578   | ConstPool OptAssign OptLinkage GlobalType ConstVal  GlobalVarAttributes {
579     *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.cnst << " " 
580        << *$6 << "\n";
581     delete $2; delete $3; delete $4; $5.destroy(); delete $6; 
582     $$ = 0;
583   }
584   | ConstPool OptAssign EXTERNAL GlobalType Types  GlobalVarAttributes {
585     *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.newTy 
586        << " " << *$6 << "\n";
587     delete $2; delete $3; delete $4; $5.destroy(); delete $6;
588     $$ = 0;
589   }
590   | ConstPool OptAssign DLLIMPORT GlobalType Types  GlobalVarAttributes {
591     *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.newTy 
592        << " " << *$6 << "\n";
593     delete $2; delete $3; delete $4; $5.destroy(); delete $6;
594     $$ = 0;
595   }
596   | ConstPool OptAssign EXTERN_WEAK GlobalType Types  GlobalVarAttributes {
597     *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.newTy 
598        << " " << *$6 << "\n";
599     delete $2; delete $3; delete $4; $5.destroy(); delete $6;
600     $$ = 0;
601   }
602   | ConstPool TARGET TargetDefinition { 
603     *O << *$2 << " " << *$3 << "\n";
604     delete $2; delete $3;
605     $$ = 0;
606   }
607   | ConstPool DEPLIBS '=' LibrariesDefinition {
608     *O << *$2 << " = " << *$4 << "\n";
609     delete $2; delete $4;
610     $$ = 0;
611   }
612   | /* empty: end of list */ { 
613     $$ = 0;
614   };
615
616
617 AsmBlock : STRINGCONSTANT ;
618
619 BigOrLittle : BIG | LITTLE 
620
621 TargetDefinition 
622   : ENDIAN '=' BigOrLittle {
623     *$1 += " = " + *$3;
624     delete $3;
625     $$ = $1;
626   }
627   | POINTERSIZE '=' EUINT64VAL {
628     *$1 += " = " + *$3.cnst;
629     if (*$3.cnst == "64")
630       SizeOfPointer = 64;
631     $3.destroy();
632     $$ = $1;
633   }
634   | TRIPLE '=' STRINGCONSTANT {
635     *$1 += " = " + *$3;
636     delete $3;
637     $$ = $1;
638   }
639   | DATALAYOUT '=' STRINGCONSTANT {
640     *$1 += " = " + *$3;
641     delete $3;
642     $$ = $1;
643   };
644
645 LibrariesDefinition 
646   : '[' LibList ']' {
647     $2->insert(0, "[ ");
648     *$2 += " ]";
649     $$ = $2;
650   };
651
652 LibList 
653   : LibList ',' STRINGCONSTANT {
654     *$1 += ", " + *$3;
655     delete $3;
656     $$ = $1;
657   }
658   | STRINGCONSTANT 
659   | /* empty: end of list */ {
660     $$ = new std::string();
661   };
662
663 //===----------------------------------------------------------------------===//
664 //                       Rules to match Function Headers
665 //===----------------------------------------------------------------------===//
666
667 Name : VAR_ID | STRINGCONSTANT;
668 OptName : Name | /*empty*/ { $$ = new std::string(); };
669
670 ArgVal : Types OptName {
671   $$ = $1.newTy;
672   if (!$2->empty())
673     *$$ += " " + *$2;
674   delete $2;
675 };
676
677 ArgListH : ArgListH ',' ArgVal {
678     *$1 += ", " + *$3;
679     delete $3;
680   }
681   | ArgVal {
682     $$ = $1;
683   };
684
685 ArgList : ArgListH {
686     $$ = $1;
687   }
688   | ArgListH ',' DOTDOTDOT {
689     *$1 += ", ...";
690     $$ = $1;
691     delete $3;
692   }
693   | DOTDOTDOT {
694     $$ = $1;
695   }
696   | /* empty */ { $$ = new std::string(); };
697
698 FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')' 
699                   OptSection OptAlign {
700     if (!$1->empty()) {
701       *$1 += " ";
702     }
703     *$1 += *$2.newTy + " " + *$3 + "(" + *$5 + ")";
704     if (!$7->empty()) {
705       *$1 += " " + *$7;
706     }
707     if (!$8->empty()) {
708       *$1 += " " + *$8;
709     }
710     $2.destroy();
711     delete $3;
712     delete $5;
713     delete $7;
714     delete $8;
715     $$ = $1;
716   };
717
718 BEGIN : BEGINTOK {
719     $$ = new std::string("begin");
720   }
721   | '{' { 
722     $$ = new std::string ("{");
723   }
724
725 FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
726   if (!$1->empty()) {
727     *O << *$1 << " ";
728   }
729   *O << *$2 << " " << *$3 << "\n";
730   delete $1; delete $2; delete $3;
731   $$ = 0;
732 };
733
734 END : ENDTOK { $$ = new std::string("end"); }
735     | '}' { $$ = new std::string("}"); };
736
737 Function : FunctionHeader BasicBlockList END {
738   if ($2)
739     *O << *$2;
740   *O << '\n' << *$3 << "\n";
741   $$ = 0;
742 };
743
744 FnDeclareLinkage
745   : /*default*/ { $$ = new std::string(); }
746   | DLLIMPORT    
747   | EXTERN_WEAK 
748   ;
749   
750 FunctionProto 
751   : DECLARE FnDeclareLinkage FunctionHeaderH { 
752     if (!$2->empty())
753       *$1 += " " + *$2;
754     *$1 += " " + *$3;
755     delete $2;
756     delete $3;
757     $$ = $1;
758   };
759
760 //===----------------------------------------------------------------------===//
761 //                        Rules to match Basic Blocks
762 //===----------------------------------------------------------------------===//
763
764 OptSideEffect : /* empty */ { $$ = new std::string(); }
765   | SIDEEFFECT;
766
767 ConstValueRef 
768   : ESINT64VAL       { $$ = $1.cnst; }
769   | EUINT64VAL       { $$ = $1.cnst; }
770   | FPVAL            { $$ = $1.cnst; }
771   | TRUETOK          { $$ = $1.cnst; }
772   | FALSETOK         { $$ = $1.cnst; }
773   | NULL_TOK         { $$ = $1.cnst; }
774   | UNDEF            { $$ = $1.cnst; }
775   | ZEROINITIALIZER  { $$ = $1.cnst; }
776   | '<' ConstVector '>' { 
777     $2->insert(0, "<");
778     *$2 += ">";
779     $$ = $2;
780   }
781   | ConstExpr 
782   | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
783     if (!$2->empty()) {
784       *$1 += " " + *$2;
785     }
786     *$1 += " " + *$3 + ", " + *$5;
787     delete $2; delete $3; delete $5;
788     $$ = $1;
789   };
790
791 SymbolicValueRef : IntVal { $$ = $1.cnst; } | Name ;
792
793 // ValueRef - A reference to a definition... either constant or symbolic
794 ValueRef : SymbolicValueRef | ConstValueRef;
795
796
797 // ResolvedVal - a <type> <value> pair.  This is used only in cases where the
798 // type immediately preceeds the value reference, and allows complex constant
799 // pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
800 ResolvedVal : Types ValueRef {
801     $$.type = $1;
802     $$.val = new std::string(*$1.newTy + " ");
803     *$$.val += *$2;
804     delete $2;
805   };
806
807 BasicBlockList : BasicBlockList BasicBlock {
808   }
809   | BasicBlock { // Do not allow functions with 0 basic blocks   
810   };
811
812
813 // Basic blocks are terminated by branching instructions: 
814 // br, br/cc, switch, ret
815 //
816 BasicBlock : InstructionList OptAssign BBTerminatorInst  {
817     *O << *$2 ;
818   };
819
820 InstructionList : InstructionList Inst {
821     *O << "    " << *$2 << "\n";
822     delete $2;
823     $$ = 0;
824   }
825   | /* empty */ {
826     $$ = 0;
827   }
828   | LABELSTR {
829     *O << *$1 << "\n";
830     delete $1;
831     $$ = 0;
832   };
833
834 BBTerminatorInst : RET ResolvedVal {              // Return with a result...
835     *O << "    " << *$1 << " " << *$2.val << "\n";
836     delete $1; $2.destroy();
837     $$ = 0;
838   }
839   | RET VOID {                                       // Return with no result...
840     *O << "    " << *$1 << " " << *$2.newTy << "\n";
841     delete $1; $2.destroy();
842     $$ = 0;
843   }
844   | BR LABEL ValueRef {                         // Unconditional Branch...
845     *O << "    " << *$1 << " " << *$2.newTy << " " << *$3 << "\n";
846     delete $1; $2.destroy(); delete $3;
847     $$ = 0;
848   }                                                  // Conditional Branch...
849   | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {  
850     *O << "    " << *$1 << " " << *$2.newTy << " " << *$3 << ", " 
851        << *$5.newTy << " " << *$6 << ", " << *$8.newTy << " " << *$9 << "\n";
852     delete $1; $2.destroy(); delete $3; $5.destroy(); delete $6; 
853     $8.destroy(); delete $9;
854     $$ = 0;
855   }
856   | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
857     *O << "    " << *$1 << " " << *$2.newTy << " " << *$3 << ", " << *$5.newTy 
858        << " " << *$6 << " [" << *$8 << " ]\n";
859     delete $1; $2.destroy(); delete $3; $5.destroy(); delete $6; delete $8;
860     $$ = 0;
861   }
862   | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
863     *O << "    " << *$1 << " " << *$2.newTy << " " << *$3 << ", " 
864        << *$5.newTy << " " << *$6 << "[]\n";
865     delete $1; $2.destroy(); delete $3; $5.destroy(); delete $6;
866     $$ = 0;
867   }
868   | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
869     TO LABEL ValueRef UNWIND LABEL ValueRef {
870     *O << "    " << *$1 << " " << *$2 << " " << *$3.newTy << " " << *$4 << " ("
871        << *$6 << ") " << *$8 << " " << *$9.newTy << " " << *$10 << " " 
872        << *$11 << " " << *$12.newTy << " " << *$13 << "\n";
873     delete $1; delete $2; $3.destroy(); delete $4; delete $6; delete $8; 
874     $9.destroy(); delete $10; delete $11; $12.destroy(); delete $13; 
875     $$ = 0;
876   }
877   | UNWIND {
878     *O << "    " << *$1 << "\n";
879     delete $1;
880     $$ = 0;
881   }
882   | UNREACHABLE {
883     *O << "    " << *$1 << "\n";
884     delete $1;
885     $$ = 0;
886   };
887
888 JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
889     *$1 += *$2.newTy + " " + *$3 + ", " + *$5.newTy + " " + *$6;
890     $2.destroy(); delete $3; $5.destroy(); delete $6;
891     $$ = $1;
892   }
893   | IntType ConstValueRef ',' LABEL ValueRef {
894     $2->insert(0, *$1.newTy + " " );
895     *$2 += ", " + *$4.newTy + " " + *$5;
896     $1.destroy(); $4.destroy(); delete $5;
897     $$ = $2;
898   };
899
900 Inst 
901   : OptAssign InstVal {
902     *$1 += *$2;
903     delete $2;
904     $$ = $1; 
905   };
906
907 PHIList 
908   : Types '[' ValueRef ',' ValueRef ']' {    // Used for PHI nodes
909     $3->insert(0, *$1.newTy + "[");
910     *$3 += "," + *$5 + "]";
911     $1.destroy(); delete $5;
912     $$ = $3;
913   }
914   | PHIList ',' '[' ValueRef ',' ValueRef ']' {
915     *$1 += ", [" + *$4 + "," + *$6 + "]";
916     delete $4; delete $6;
917     $$ = $1;
918   };
919
920
921 ValueRefList 
922   : ResolvedVal { $$ = new std::string(*$1.val); $1.destroy(); }
923   | ValueRefList ',' ResolvedVal {
924     *$1 += ", " + *$3.val;
925     $3.destroy();
926     $$ = $1;
927   };
928
929 // ValueRefListE - Just like ValueRefList, except that it may also be empty!
930 ValueRefListE 
931   : ValueRefList 
932   | /*empty*/ { $$ = new std::string(); }
933   ;
934
935 OptTailCall 
936   : TAIL CALL {
937     *$1 += " " + *$2;
938     delete $2;
939     $$ = $1;
940   }
941   | CALL 
942   ;
943
944 InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
945     *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5;
946     $2.destroy(); delete $3; delete $5;
947     $$ = $1;
948   }
949   | LogicalOps Types ValueRef ',' ValueRef {
950     *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5;
951     $2.destroy(); delete $3; delete $5;
952     $$ = $1;
953   }
954   | SetCondOps Types ValueRef ',' ValueRef {
955     *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5;
956     $2.destroy(); delete $3; delete $5;
957     $$ = $1;
958   }
959   | NOT ResolvedVal {
960     *$1 += " " + *$2.val;
961     $2.destroy();
962     $$ = $1;
963   }
964   | ShiftOps ResolvedVal ',' ResolvedVal {
965     *$1 += " " + *$2.val + ", " + *$4.val;
966     $2.destroy(); $4.destroy();
967     $$ = $1;
968   }
969   | CAST ResolvedVal TO Types {
970     const char *opcode = getCastOpcode($2.type, $4);
971     $$ = new std::string(opcode);
972     *$$ += *$2.val + " " + *$3 + " " + *$4.newTy; 
973     delete $1; $2.destroy();
974     delete $3; $4.destroy();
975   }
976   | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
977     *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
978     $2.destroy(); $4.destroy(); $6.destroy();
979     $$ = $1;
980   }
981   | VAARG ResolvedVal ',' Types {
982     *$1 += " " + *$2.val + ", " + *$4.newTy;
983     $2.destroy(); $4.destroy();
984     $$ = $1;
985   }
986   | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
987     *$1 += " " + *$2.val + ", " + *$4.val;
988     $2.destroy(); $4.destroy();
989     $$ = $1;
990   }
991   | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
992     *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
993     $2.destroy(); $4.destroy(); $6.destroy();
994     $$ = $1;
995   }
996   | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
997     *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
998     $2.destroy(); $4.destroy(); $6.destroy();
999     $$ = $1;
1000   }
1001   | PHI_TOK PHIList {
1002     *$1 += " " + *$2;
1003     delete $2;
1004     $$ = $1;
1005   }
1006   | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')'  {
1007     if (!$2->empty())
1008       *$1 += " " + *$2;
1009     if (!$1->empty())
1010       *$1 += " ";
1011     *$1 += *$3.newTy + " " + *$4 + "(" + *$6 + ")";
1012     delete $2; $3.destroy(); delete $4; delete $6;
1013     $$ = $1;
1014   }
1015   | MemoryInst ;
1016
1017
1018 // IndexList - List of indices for GEP based instructions...
1019 IndexList 
1020   : ',' ValueRefList { 
1021     $2->insert(0, ", ");
1022     $$ = $2;
1023   } 
1024   | /* empty */ {  $$ = new std::string(); }
1025   ;
1026
1027 OptVolatile 
1028   : VOLATILE 
1029   | /* empty */ { $$ = new std::string(); }
1030   ;
1031
1032 MemoryInst : MALLOC Types OptCAlign {
1033     *$1 += " " + *$2.newTy;
1034     if (!$3->empty())
1035       *$1 += " " + *$3;
1036     $2.destroy(); delete $3;
1037     $$ = $1;
1038   }
1039   | MALLOC Types ',' UINT ValueRef OptCAlign {
1040     *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5;
1041     if (!$6->empty())
1042       *$1 += " " + *$6;
1043     $2.destroy(); $4.destroy(); delete $5; delete $6;
1044     $$ = $1;
1045   }
1046   | ALLOCA Types OptCAlign {
1047     *$1 += " " + *$2.newTy;
1048     if (!$3->empty())
1049       *$1 += " " + *$3;
1050     $2.destroy(); delete $3;
1051     $$ = $1;
1052   }
1053   | ALLOCA Types ',' UINT ValueRef OptCAlign {
1054     *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5;
1055     if (!$6->empty())
1056       *$1 += " " + *$6;
1057     $2.destroy(); $4.destroy(); delete $5; delete $6;
1058     $$ = $1;
1059   }
1060   | FREE ResolvedVal {
1061     *$1 += " " + *$2.val;
1062     $2.destroy();
1063     $$ = $1;
1064   }
1065   | OptVolatile LOAD Types ValueRef {
1066     if (!$1->empty())
1067       *$1 += " ";
1068     *$1 += *$2 + " " + *$3.newTy + " " + *$4;
1069     delete $2; $3.destroy(); delete $4;
1070     $$ = $1;
1071   }
1072   | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1073     if (!$1->empty())
1074       *$1 += " ";
1075     *$1 += *$2 + " " + *$3.val + ", " + *$5.newTy + " " + *$6;
1076     delete $2; $3.destroy(); $5.destroy(); delete $6;
1077     $$ = $1;
1078   }
1079   | GETELEMENTPTR Types ValueRef IndexList {
1080     *$1 += *$2.newTy + " " + *$3 + " " + *$4;
1081     $2.destroy(); delete $3; delete $4;
1082     $$ = $1;
1083   };
1084
1085 %%
1086
1087 int yyerror(const char *ErrorMsg) {
1088   std::string where 
1089     = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1090                   + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
1091   std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
1092   if (yychar == YYEMPTY || yychar == 0)
1093     errMsg += "end-of-file.";
1094   else
1095     errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
1096   std::cerr << errMsg << '\n';
1097   exit(1);
1098 }