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