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