1 //===-- UpgradeParser.y - Upgrade parser for llvm assmbly -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the bison parser for LLVM 1.9 assembly language.
12 //===----------------------------------------------------------------------===//
15 #include "ParserInternals.h"
16 #include <llvm/ADT/StringExtras.h>
22 #define YYERROR_VERBOSE 1
23 #define YYINCLUDED_STDLIB_H
25 #define UPGRADE_SETCOND_OPS 0
27 int yylex(); // declaration" of xxx warnings.
31 static std::string CurFilename;
32 static std::ostream *O = 0;
33 std::istream* LexInput = 0;
34 unsigned SizeOfPointer = 32;
35 static uint64_t unique = 1;
37 typedef std::vector<TypeInfo> TypeVector;
38 static TypeVector EnumeratedTypes;
39 typedef std::map<std::string,TypeInfo> TypeMap;
40 static TypeMap NamedTypes;
42 void destroy(ValueList* VL) {
43 while (!VL->empty()) {
44 ValueInfo& VI = VL->back();
51 void UpgradeAssembly(const std::string &infile, std::istream& in,
52 std::ostream &out, bool debug)
61 std::cerr << "Parse failed.\n";
66 static void ResolveType(TypeInfo& Ty) {
67 if (Ty.oldTy == UnresolvedTy) {
68 TypeMap::iterator I = NamedTypes.find(*Ty.newTy);
69 if (I != NamedTypes.end()) {
70 Ty.oldTy = I->second.oldTy;
71 Ty.elemTy = I->second.elemTy;
73 std::string msg("Can't resolve type: ");
77 } else if (Ty.oldTy == NumericTy) {
78 unsigned ref = atoi(&((Ty.newTy->c_str())[1])); // Skip the '\\'
79 if (ref < EnumeratedTypes.size()) {
80 Ty.oldTy = EnumeratedTypes[ref].oldTy;
81 Ty.elemTy = EnumeratedTypes[ref].elemTy;
83 std::string msg("Can't resolve type: ");
88 // otherwise its already resolved.
91 static const char* getCastOpcode(
92 std::string& Source, const TypeInfo& SrcTy, const TypeInfo& DstTy)
94 unsigned SrcBits = SrcTy.getBitWidth();
95 unsigned DstBits = DstTy.getBitWidth();
96 const char* opcode = "bitcast";
97 // Run through the possibilities ...
98 if (DstTy.isIntegral()) { // Casting to integral
99 if (SrcTy.isIntegral()) { // Casting from integral
100 if (DstBits < SrcBits)
102 else if (DstBits > SrcBits) { // its an extension
103 if (SrcTy.isSigned())
104 opcode ="sext"; // signed -> SEXT
106 opcode = "zext"; // unsigned -> ZEXT
108 opcode = "bitcast"; // Same size, No-op cast
110 } else if (SrcTy.isFloatingPoint()) { // Casting from floating pt
111 if (DstTy.isSigned())
112 opcode = "fptosi"; // FP -> sint
114 opcode = "fptoui"; // FP -> uint
115 } else if (SrcTy.isPacked()) {
116 assert(DstBits == SrcTy.getBitWidth() &&
117 "Casting packed to integer of different width");
118 opcode = "bitcast"; // same size, no-op cast
120 assert(SrcTy.isPointer() &&
121 "Casting from a value that is not first-class type");
122 opcode = "ptrtoint"; // ptr -> int
124 } else if (DstTy.isFloatingPoint()) { // Casting to floating pt
125 if (SrcTy.isIntegral()) { // Casting from integral
126 if (SrcTy.isSigned())
127 opcode = "sitofp"; // sint -> FP
129 opcode = "uitofp"; // uint -> FP
130 } else if (SrcTy.isFloatingPoint()) { // Casting from floating pt
131 if (DstBits < SrcBits) {
132 opcode = "fptrunc"; // FP -> smaller FP
133 } else if (DstBits > SrcBits) {
134 opcode = "fpext"; // FP -> larger FP
136 opcode ="bitcast"; // same size, no-op cast
138 } else if (SrcTy.isPacked()) {
139 assert(DstBits == SrcTy.getBitWidth() &&
140 "Casting packed to floating point of different width");
141 opcode = "bitcast"; // same size, no-op cast
143 assert(0 && "Casting pointer or non-first class to float");
145 } else if (DstTy.isPacked()) {
146 if (SrcTy.isPacked()) {
147 assert(DstTy.getBitWidth() == SrcTy.getBitWidth() &&
148 "Casting packed to packed of different widths");
149 opcode = "bitcast"; // packed -> packed
150 } else if (DstTy.getBitWidth() == SrcBits) {
151 opcode = "bitcast"; // float/int -> packed
153 assert(!"Illegal cast to packed (wrong type or size)");
155 } else if (DstTy.isPointer()) {
156 if (SrcTy.isPointer()) {
157 opcode = "bitcast"; // ptr -> ptr
158 } else if (SrcTy.isIntegral()) {
159 opcode = "inttoptr"; // int -> ptr
161 assert(!"Casting invalid type to pointer");
164 assert(!"Casting to type that is not first-class");
169 static std::string getCastUpgrade(
170 const std::string& Src, TypeInfo& SrcTy, TypeInfo& DstTy, bool isConst)
173 std::string Source = Src;
174 if (SrcTy.isFloatingPoint() && DstTy.isPointer()) {
175 // fp -> ptr cast is no longer supported but we must upgrade this
176 // by doing a double cast: fp -> int -> ptr
178 Source = "ulong fptoui(" + Source + " to ulong)";
180 *O << " %cast_upgrade" << unique << " = fptoui " << Source
182 Source = "ulong %cast_upgrade" + llvm::utostr(unique);
184 // Update the SrcTy for the getCastOpcode call below
186 SrcTy.newTy = new std::string("ulong");
187 SrcTy.oldTy = ULongTy;
188 } else if (DstTy.oldTy == BoolTy) {
189 // cast ptr %x to bool was previously defined as setne ptr %x, null
190 // The ptrtoint semantic is to truncate, not compare so we must retain
191 // the original intent by replace the cast with a setne
192 const char* comparator = SrcTy.isPointer() ? ", null" :
193 (SrcTy.isFloatingPoint() ? ", 0.0" : ", 0");
195 Result = "setne (" + Source + comparator + ")";
197 Result = "setne " + Source + comparator;
198 return Result; // skip cast processing below
202 std::string Opcode(getCastOpcode(Source, SrcTy, DstTy));
204 Result += Opcode + "( " + Source + " to " + *DstTy.newTy + ")";
206 Result += Opcode + " " + Source + " to " + *DstTy.newTy;
210 const char* getDivRemOpcode(const std::string& opcode, const TypeInfo& TI) {
211 const char* op = opcode.c_str();
215 Ty.oldTy = Ty.getElementType();
217 if (Ty.isFloatingPoint())
219 else if (Ty.isUnsigned())
221 else if (Ty.isSigned())
224 yyerror("Invalid type for div instruction");
225 else if (opcode == "rem")
226 if (Ty.isFloatingPoint())
228 else if (Ty.isUnsigned())
230 else if (Ty.isSigned())
233 yyerror("Invalid type for rem instruction");
238 getCompareOp(const std::string& setcc, const TypeInfo& TI) {
239 assert(setcc.length() == 5);
242 assert(cc1 == 'e' || cc1 == 'n' || cc1 == 'l' || cc1 == 'g');
243 assert(cc2 == 'q' || cc2 == 'e' || cc2 == 'e' || cc2 == 't');
244 std::string result("xcmp xxx");
247 if (TI.isFloatingPoint()) {
249 result[5] = 'o'; // FIXME: Always map to ordered comparison ?
250 } else if (TI.isIntegral() || TI.isPointer()) {
252 if ((cc1 == 'e' && cc2 == 'q') || (cc1 == 'n' && cc2 == 'e'))
254 else if (TI.isSigned())
256 else if (TI.isUnsigned() || TI.isPointer())
259 yyerror("Invalid integral type for setcc");
266 %file-prefix="UpgradeParser"
276 %token <Type> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
277 %token <Type> FLOAT DOUBLE LABEL
278 %token <String> OPAQUE ESINT64VAL EUINT64VAL SINTVAL UINTVAL FPVAL
279 %token <String> NULL_TOK UNDEF ZEROINITIALIZER TRUETOK FALSETOK
280 %token <String> TYPE VAR_ID LABELSTR STRINGCONSTANT
281 %token <String> IMPLEMENTATION BEGINTOK ENDTOK
282 %token <String> DECLARE GLOBAL CONSTANT SECTION VOLATILE
283 %token <String> TO DOTDOTDOT CONST INTERNAL LINKONCE WEAK
284 %token <String> DLLIMPORT DLLEXPORT EXTERN_WEAK APPENDING
285 %token <String> NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
286 %token <String> ALIGN UNINITIALIZED
287 %token <String> DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
288 %token <String> CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
289 %token <String> X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
290 %token <String> DATALAYOUT
291 %token <String> RET BR SWITCH INVOKE EXCEPT UNWIND UNREACHABLE
292 %token <String> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM AND OR XOR
293 %token <String> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
294 %token <String> ICMP FCMP EQ NE SLT SGT SLE SGE OEQ ONE OLT OGT OLE OGE
295 %token <String> ORD UNO UEQ UNE ULT UGT ULE UGE
296 %token <String> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
297 %token <String> PHI_TOK SELECT SHL SHR ASHR LSHR VAARG
298 %token <String> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
299 %token <String> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI UITOFP SITOFP
300 %token <String> PTRTOINT INTTOPTR BITCAST
302 %type <String> OptAssign OptLinkage OptCallingConv OptAlign OptCAlign
303 %type <String> SectionString OptSection GlobalVarAttributes GlobalVarAttribute
304 %type <String> ArgTypeListI ConstExpr DefinitionList
305 %type <String> ConstPool TargetDefinition LibrariesDefinition LibList OptName
306 %type <String> ArgVal ArgListH ArgList FunctionHeaderH BEGIN FunctionHeader END
307 %type <String> Function FunctionProto BasicBlock TypeListI
308 %type <String> InstructionList BBTerminatorInst JumpTable Inst PHIList
309 %type <String> OptTailCall InstVal OptVolatile Unwind
310 %type <String> MemoryInst SymbolicValueRef OptSideEffect GlobalType
311 %type <String> FnDeclareLinkage BasicBlockList BigOrLittle AsmBlock
312 %type <String> Name ConstValueRef ConstVector External
313 %type <String> ShiftOps SetCondOps LogicalOps ArithmeticOps CastOps CompareOps
314 %type <String> Predicates
316 %type <ValList> ValueRefList ValueRefListE IndexList
318 %type <Type> IntType SIntType UIntType FPType TypesV Types
319 %type <Type> PrimType UpRTypesV UpRTypes
321 %type <String> IntVal EInt64Val
322 %type <Const> ConstVal
324 %type <Value> ValueRef ResolvedVal
330 // Handle constant integer size restriction and conversion...
331 IntVal : SINTVAL | UINTVAL ;
332 EInt64Val : ESINT64VAL | EUINT64VAL;
334 // Operations that are notably excluded from this list include:
335 // RET, BR, & SWITCH because they end basic blocks and are treated specially.
336 ArithmeticOps: ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV
337 | REM | UREM | SREM | FREM;
338 LogicalOps : AND | OR | XOR;
339 SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
340 CompareOps : ICMP | FCMP;
341 Predicates : EQ | NE | SLT | SGT | SLE | SGE | ULT | UGT | ULE | UGE
342 | OEQ | ONE | OLT | OGT | OLE | OGE | ORD | UNO | UEQ | UNE ;
343 ShiftOps : SHL | SHR | ASHR | LSHR;
344 CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI |
345 UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
348 // These are some types that allow classification if we only want a particular
349 // thing... for example, only a signed, unsigned, or integral type.
350 SIntType : LONG | INT | SHORT | SBYTE;
351 UIntType : ULONG | UINT | USHORT | UBYTE;
352 IntType : SIntType | UIntType;
353 FPType : FLOAT | DOUBLE;
355 // OptAssign - Value producing statements have an optional assignment component
356 OptAssign : Name '=' {
360 $$ = new std::string("");
364 : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT
366 | /*empty*/ { $$ = new std::string(""); } ;
369 : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK
371 | CC_TOK EUINT64VAL {
376 | /*empty*/ { $$ = new std::string(""); } ;
378 // OptAlign/OptCAlign - An optional alignment, and an optional alignment with
379 // a comma before it.
381 : /*empty*/ { $$ = new std::string(); }
382 | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
385 : /*empty*/ { $$ = new std::string(); }
386 | ',' ALIGN EUINT64VAL {
394 : SECTION STRINGCONSTANT {
400 OptSection : /*empty*/ { $$ = new std::string(); }
404 : /* empty */ { $$ = new std::string(); }
405 | ',' GlobalVarAttribute GlobalVarAttributes {
421 //===----------------------------------------------------------------------===//
422 // Types includes all predefined types... except void, because it can only be
423 // used in specific contexts (function returning void for example). To have
424 // access to it, a user must explicitly use TypesV.
427 // TypesV includes all of 'Types', but it also includes the void type.
428 TypesV : Types | VOID ;
429 UpRTypesV : UpRTypes | VOID ;
432 // Derived types are added later...
434 PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
435 PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
443 $$.oldTy = UnresolvedTy;
448 | '\\' EUINT64VAL { // Type UpReference
451 $$.oldTy = NumericTy;
453 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
454 *$1.newTy += "( " + *$3 + " )";
457 $$.oldTy = FunctionTy;
459 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
461 *$2 += " x " + *$4.newTy + " ]";
465 $$.elemTy = $4.oldTy;
467 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
469 *$2 += " x " + *$4.newTy + " >";
473 $$.elemTy = $4.oldTy;
475 | '{' TypeListI '}' { // Structure type?
481 | '{' '}' { // Empty structure type?
482 $$.newTy = new std::string("{}");
485 | UpRTypes '*' { // Pointer type?
487 $$.elemTy = $1.oldTy;
488 $1.oldTy = PointerTy;
492 // TypeList - Used for struct declarations and as a basis for function type
493 // declaration type lists
499 | TypeListI ',' UpRTypes {
500 *$1 += ", " + *$3.newTy;
505 // ArgTypeList - List of types for a function type declaration...
508 | TypeListI ',' DOTDOTDOT {
517 $$ = new std::string();
520 // ConstVal - The various declarations that go into the constant pool. This
521 // production is used ONLY to represent constants that show up AFTER a 'const',
522 // 'constant' or 'global' token at global scope. Constants that can be inlined
523 // into other expressions (such as integers and constexprs) are handled by the
524 // ResolvedVal, ValueRef and ConstValueRef productions.
526 ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
528 $$.cnst = new std::string(*$1.newTy);
529 *$$.cnst += " [ " + *$3 + " ]";
534 $$.cnst = new std::string(*$1.newTy);
537 | Types 'c' STRINGCONSTANT {
539 $$.cnst = new std::string(*$1.newTy);
540 *$$.cnst += " c" + *$3;
543 | Types '<' ConstVector '>' { // Nonempty unsized arr
545 $$.cnst = new std::string(*$1.newTy);
546 *$$.cnst += " < " + *$3 + " >";
549 | Types '{' ConstVector '}' {
551 $$.cnst = new std::string(*$1.newTy);
552 *$$.cnst += " { " + *$3 + " }";
557 $$.cnst = new std::string(*$1.newTy);
562 $$.cnst = new std::string(*$1.newTy);
563 *$$.cnst += " " + *$2;
568 $$.cnst = new std::string(*$1.newTy);
569 *$$.cnst += " " + *$2;
572 | Types SymbolicValueRef {
574 $$.cnst = new std::string(*$1.newTy);
575 *$$.cnst += " " + *$2;
580 $$.cnst = new std::string(*$1.newTy);
581 *$$.cnst += " " + *$2;
584 | Types ZEROINITIALIZER {
586 $$.cnst = new std::string(*$1.newTy);
587 *$$.cnst += " " + *$2;
590 | SIntType EInt64Val { // integral constants
592 $$.cnst = new std::string(*$1.newTy);
593 *$$.cnst += " " + *$2;
596 | UIntType EUINT64VAL { // integral constants
598 $$.cnst = new std::string(*$1.newTy);
599 *$$.cnst += " " + *$2;
602 | BOOL TRUETOK { // Boolean constants
604 $$.cnst = new std::string(*$1.newTy);
605 *$$.cnst += " " + *$2;
608 | BOOL FALSETOK { // Boolean constants
610 $$.cnst = new std::string(*$1.newTy);
611 *$$.cnst += " " + *$2;
614 | FPType FPVAL { // Float & Double constants
616 $$.cnst = new std::string(*$1.newTy);
617 *$$.cnst += " " + *$2;
622 ConstExpr: CastOps '(' ConstVal TO Types ')' {
623 std::string source = *$3.cnst;
627 // Call getCastUpgrade to upgrade the old cast
628 $$ = new std::string(getCastUpgrade(source, $3.type, $5, true));
630 // Nothing to upgrade, just create the cast constant expr
631 $$ = new std::string(*$1);
632 *$$ += "( " + source + " to " + *$5.newTy + ")";
634 delete $1; $3.destroy(); delete $4; $5.destroy();
636 | GETELEMENTPTR '(' ConstVal IndexList ')' {
637 *$1 += "(" + *$3.cnst;
638 for (unsigned i = 0; i < $4->size(); ++i) {
639 ValueInfo& VI = (*$4)[i];
640 *$1 += ", " + *VI.val;
648 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
649 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
650 $3.destroy(); $5.destroy(); $7.destroy();
653 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
654 const char* op = getDivRemOpcode(*$1, $3.type);
655 $$ = new std::string(op);
656 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
657 delete $1; $3.destroy(); $5.destroy();
659 | LogicalOps '(' ConstVal ',' ConstVal ')' {
660 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
661 $3.destroy(); $5.destroy();
664 | SetCondOps '(' ConstVal ',' ConstVal ')' {
665 #if UPGRADE_SETCOND_OPS
666 *$1 = getCompareOp(*$1, $3.type);
668 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
669 $3.destroy(); $5.destroy();
672 | CompareOps Predicates '(' ConstVal ',' ConstVal ')' {
673 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
674 delete $2; $4.destroy(); $6.destroy();
677 | ShiftOps '(' ConstVal ',' ConstVal ')' {
678 const char* shiftop = $1->c_str();
680 shiftop = ($3.type.isUnsigned()) ? "lshr" : "ashr";
681 $$ = new std::string(shiftop);
682 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
683 delete $1; $3.destroy(); $5.destroy();
685 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
686 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
687 $3.destroy(); $5.destroy();
690 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
691 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
692 $3.destroy(); $5.destroy(); $7.destroy();
695 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
696 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
697 $3.destroy(); $5.destroy(); $7.destroy();
702 // ConstVector - A list of comma separated constants.
705 : ConstVector ',' ConstVal {
706 *$1 += ", " + *$3.cnst;
710 | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
714 // GlobalType - Match either GLOBAL or CONSTANT for global declarations...
715 GlobalType : GLOBAL | CONSTANT ;
718 //===----------------------------------------------------------------------===//
719 // Rules to match Modules
720 //===----------------------------------------------------------------------===//
722 // Module rule: Capture the result of parsing the whole file into a result
725 Module : DefinitionList {
728 // DefinitionList - Top level definitions
730 DefinitionList : DefinitionList Function {
733 | DefinitionList FunctionProto {
738 | DefinitionList MODULE ASM_TOK AsmBlock {
739 *O << "module asm " << " " << *$4 << "\n";
742 | DefinitionList IMPLEMENTATION {
743 *O << "implementation\n";
746 | ConstPool { $$ = 0; }
748 External : EXTERNAL | UNINITIALIZED { $$ = $1; *$$ = "external"; }
750 // ConstPool - Constants with optional names assigned to them.
751 ConstPool : ConstPool OptAssign TYPE TypesV {
752 EnumeratedTypes.push_back($4);
754 NamedTypes[*$2].newTy = new std::string(*$4.newTy);
755 NamedTypes[*$2].oldTy = $4.oldTy;
756 NamedTypes[*$2].elemTy = $4.elemTy;
759 *O << "type " << *$4.newTy << "\n";
760 delete $2; delete $3; $4.destroy();
763 | ConstPool FunctionProto { // Function prototypes can be in const pool
768 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
769 *O << *$2 << " " << *$3 << " " << *$4 << "\n";
770 delete $2; delete $3; delete $4;
773 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
776 *O << *$3 << " " << *$4 << " " << *$5.cnst << " " << *$6 << "\n";
777 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
780 | ConstPool OptAssign External GlobalType Types GlobalVarAttributes {
783 *O << *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
784 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
787 | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
790 *O << *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
791 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
794 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
797 *O << *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
798 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
801 | ConstPool TARGET TargetDefinition {
802 *O << *$2 << " " << *$3 << "\n";
803 delete $2; delete $3;
806 | ConstPool DEPLIBS '=' LibrariesDefinition {
807 *O << *$2 << " = " << *$4 << "\n";
808 delete $2; delete $4;
811 | /* empty: end of list */ {
816 AsmBlock : STRINGCONSTANT ;
818 BigOrLittle : BIG | LITTLE
821 : ENDIAN '=' BigOrLittle {
826 | POINTERSIZE '=' EUINT64VAL {
833 | TRIPLE '=' STRINGCONSTANT {
838 | DATALAYOUT '=' STRINGCONSTANT {
852 : LibList ',' STRINGCONSTANT {
858 | /* empty: end of list */ {
859 $$ = new std::string();
862 //===----------------------------------------------------------------------===//
863 // Rules to match Function Headers
864 //===----------------------------------------------------------------------===//
866 Name : VAR_ID | STRINGCONSTANT;
867 OptName : Name | /*empty*/ { $$ = new std::string(); };
869 ArgVal : Types OptName {
876 ArgListH : ArgListH ',' ArgVal {
887 | ArgListH ',' DOTDOTDOT {
895 | /* empty */ { $$ = new std::string(); };
897 FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
898 OptSection OptAlign {
902 *$1 += *$2.newTy + " " + *$3 + "(" + *$5 + ")";
917 BEGIN : BEGINTOK { $$ = new std::string("{"); delete $1; }
918 | '{' { $$ = new std::string ("{"); }
920 FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
924 *O << *$2 << " " << *$3 << "\n";
925 delete $1; delete $2; delete $3;
929 END : ENDTOK { $$ = new std::string("}"); delete $1; }
930 | '}' { $$ = new std::string("}"); };
932 Function : FunctionHeader BasicBlockList END {
935 *O << '\n' << *$3 << "\n";
940 : /*default*/ { $$ = new std::string(); }
946 : DECLARE FnDeclareLinkage FunctionHeaderH {
955 //===----------------------------------------------------------------------===//
956 // Rules to match Basic Blocks
957 //===----------------------------------------------------------------------===//
959 OptSideEffect : /* empty */ { $$ = new std::string(); }
963 : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK | NULL_TOK | UNDEF
965 | '<' ConstVector '>' {
971 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
975 *$1 += " " + *$3 + ", " + *$5;
976 delete $2; delete $3; delete $5;
980 SymbolicValueRef : IntVal | Name ;
982 // ValueRef - A reference to a definition... either constant or symbolic
988 $$.type.oldTy = UnresolvedTy;
994 $$.type.oldTy = UnresolvedTy;
998 // ResolvedVal - a <type> <value> pair. This is used only in cases where the
999 // type immediately preceeds the value reference, and allows complex constant
1000 // pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1001 ResolvedVal : Types ValueRef {
1004 $$.val->insert(0, *$1.newTy + " ");
1007 BasicBlockList : BasicBlockList BasicBlock {
1010 | BasicBlock { // Do not allow functions with 0 basic blocks
1015 // Basic blocks are terminated by branching instructions:
1016 // br, br/cc, switch, ret
1018 BasicBlock : InstructionList BBTerminatorInst {
1022 InstructionList : InstructionList Inst {
1023 *O << " " << *$2 << "\n";
1036 Unwind : UNWIND | EXCEPT { $$ = $1; *$$ = "unwind"; }
1038 BBTerminatorInst : RET ResolvedVal { // Return with a result...
1039 *O << " " << *$1 << " " << *$2.val << "\n";
1040 delete $1; $2.destroy();
1043 | RET VOID { // Return with no result...
1044 *O << " " << *$1 << " " << *$2.newTy << "\n";
1045 delete $1; $2.destroy();
1048 | BR LABEL ValueRef { // Unconditional Branch...
1049 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << "\n";
1050 delete $1; $2.destroy(); $3.destroy();
1052 } // Conditional Branch...
1053 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
1054 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1055 << *$5.newTy << " " << *$6.val << ", " << *$8.newTy << " "
1057 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1058 $8.destroy(); $9.destroy();
1061 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1062 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1063 << *$5.newTy << " " << *$6.val << " [" << *$8 << " ]\n";
1064 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1068 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1069 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1070 << *$5.newTy << " " << *$6.val << "[]\n";
1071 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1074 | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
1075 TO LABEL ValueRef Unwind LABEL ValueRef {
1079 *O << *$2 << " " << *$3 << " " << *$4.newTy << " " << *$5.val << " (";
1080 for (unsigned i = 0; i < $7->size(); ++i) {
1081 ValueInfo& VI = (*$7)[i];
1083 if (i+1 < $7->size())
1087 *O << ") " << *$9 << " " << *$10.newTy << " " << *$11.val << " "
1088 << *$12 << " " << *$13.newTy << " " << *$14.val << "\n";
1089 delete $1; delete $2; delete $3; $4.destroy(); $5.destroy(); delete $7;
1090 delete $9; $10.destroy(); $11.destroy(); delete $12; $13.destroy();
1095 *O << " " << *$1 << "\n";
1100 *O << " " << *$1 << "\n";
1105 JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1106 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5.newTy + " " + *$6.val;
1107 $2.destroy(); delete $3; $5.destroy(); $6.destroy();
1110 | IntType ConstValueRef ',' LABEL ValueRef {
1111 $2->insert(0, *$1.newTy + " " );
1112 *$2 += ", " + *$4.newTy + " " + *$5.val;
1113 $1.destroy(); $4.destroy(); $5.destroy();
1118 : OptAssign InstVal {
1127 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1128 $3.val->insert(0, *$1.newTy + "[");
1129 *$3.val += "," + *$5.val + "]";
1130 $1.destroy(); $5.destroy();
1131 $$ = new std::string(*$3.val);
1134 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1135 *$1 += ", [" + *$4.val + "," + *$6.val + "]";
1136 $4.destroy(); $6.destroy();
1143 $$ = new ValueList();
1146 | ValueRefList ',' ResolvedVal {
1151 // ValueRefListE - Just like ValueRefList, except that it may also be empty!
1153 : ValueRefList { $$ = $1; }
1154 | /*empty*/ { $$ = new ValueList(); }
1166 InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1167 const char* op = getDivRemOpcode(*$1, $2);
1168 $$ = new std::string(op);
1169 *$$ += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1170 delete $1; $2.destroy(); $3.destroy(); $5.destroy();
1172 | LogicalOps Types ValueRef ',' ValueRef {
1173 *$1 += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1174 $2.destroy(); $3.destroy(); $5.destroy();
1177 | SetCondOps Types ValueRef ',' ValueRef {
1178 #if UPGRADE_SETCOND_OPS
1179 *$1 = getCompareOp(*$1, $2);
1181 *$1 += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1182 $2.destroy(); $3.destroy(); $5.destroy();
1185 | CompareOps Predicates Types ValueRef ',' ValueRef ')' {
1186 *$1 += " " + *$2 + " " + *$4.val + "," + *$6.val + ")";
1187 delete $2; $4.destroy(); $6.destroy();
1191 *$1 += " " + *$2.val;
1195 | ShiftOps ResolvedVal ',' ResolvedVal {
1196 const char* shiftop = $1->c_str();
1198 shiftop = ($2.type.isUnsigned()) ? "lshr" : "ashr";
1199 $$ = new std::string(shiftop);
1200 *$$ += " " + *$2.val + ", " + *$4.val;
1201 delete $1; $2.destroy(); $4.destroy();
1203 | CastOps ResolvedVal TO Types {
1204 std::string source = *$2.val;
1205 TypeInfo SrcTy = $2.type;
1206 TypeInfo DstTy = $4;
1208 $$ = new std::string();
1209 if (*$1 == "cast") {
1210 *$$ += getCastUpgrade(source, SrcTy, DstTy, false);
1212 *$$ += *$1 + " " + source + " to " + *DstTy.newTy;
1214 delete $1; $2.destroy();
1215 delete $3; $4.destroy();
1217 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1218 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1219 $2.destroy(); $4.destroy(); $6.destroy();
1222 | VAARG ResolvedVal ',' Types {
1223 *$1 += " " + *$2.val + ", " + *$4.newTy;
1224 $2.destroy(); $4.destroy();
1227 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
1228 *$1 += " " + *$2.val + ", " + *$4.val;
1229 $2.destroy(); $4.destroy();
1232 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1233 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1234 $2.destroy(); $4.destroy(); $6.destroy();
1237 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1238 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1239 $2.destroy(); $4.destroy(); $6.destroy();
1247 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
1252 *$1 += *$3.newTy + " " + *$4.val + "(";
1253 for (unsigned i = 0; i < $6->size(); ++i) {
1254 ValueInfo& VI = (*$6)[i];
1256 if (i+1 < $6->size())
1261 delete $2; $3.destroy(); $4.destroy(); delete $6;
1267 // IndexList - List of indices for GEP based instructions...
1269 : ',' ValueRefList { $$ = $2; }
1270 | /* empty */ { $$ = new ValueList(); }
1275 | /* empty */ { $$ = new std::string(); }
1278 MemoryInst : MALLOC Types OptCAlign {
1279 *$1 += " " + *$2.newTy;
1282 $2.destroy(); delete $3;
1285 | MALLOC Types ',' UINT ValueRef OptCAlign {
1286 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5.val;
1289 $2.destroy(); $4.destroy(); $5.destroy(); delete $6;
1292 | ALLOCA Types OptCAlign {
1293 *$1 += " " + *$2.newTy;
1296 $2.destroy(); delete $3;
1299 | ALLOCA Types ',' UINT ValueRef OptCAlign {
1300 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5.val;
1303 $2.destroy(); $4.destroy(); $5.destroy(); delete $6;
1306 | FREE ResolvedVal {
1307 *$1 += " " + *$2.val;
1311 | OptVolatile LOAD Types ValueRef {
1314 *$1 += *$2 + " " + *$3.newTy + " " + *$4.val;
1315 delete $2; $3.destroy(); $4.destroy();
1318 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1321 *$1 += *$2 + " " + *$3.val + ", " + *$5.newTy + " " + *$6.val;
1322 delete $2; $3.destroy(); $5.destroy(); $6.destroy();
1325 | GETELEMENTPTR Types ValueRef IndexList {
1326 // Upgrade the indices
1327 for (unsigned i = 0; i < $4->size(); ++i) {
1328 ValueInfo& VI = (*$4)[i];
1329 if (VI.type.isUnsigned() && !VI.isConstant() &&
1330 VI.type.getBitWidth() < 64) {
1331 std::string* old = VI.val;
1332 *O << " %gep_upgrade" << unique << " = zext " << *old
1334 VI.val = new std::string("ulong %gep_upgrade" + llvm::utostr(unique++));
1335 VI.type.oldTy = ULongTy;
1339 *$1 += " " + *$2.newTy + " " + *$3.val;
1340 for (unsigned i = 0; i < $4->size(); ++i) {
1341 ValueInfo& VI = (*$4)[i];
1342 *$1 += ", " + *VI.val;
1345 $2.destroy(); $3.destroy(); delete $4;
1351 int yyerror(const char *ErrorMsg) {
1353 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1354 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
1355 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
1356 if (yychar == YYEMPTY || yychar == 0)
1357 errMsg += "end-of-file.";
1359 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
1360 std::cerr << errMsg << '\n';