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