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>
23 #define YYERROR_VERBOSE 1
24 #define YYINCLUDED_STDLIB_H
26 #define UPGRADE_SETCOND_OPS 0
28 int yylex(); // declaration" of xxx warnings.
32 static std::string CurFilename;
33 static std::ostream *O = 0;
34 std::istream* LexInput = 0;
35 unsigned SizeOfPointer = 32;
36 static uint64_t unique = 1;
38 typedef std::vector<TypeInfo> TypeVector;
39 static TypeVector EnumeratedTypes;
40 typedef std::map<std::string,TypeInfo> TypeMap;
41 static TypeMap NamedTypes;
43 void destroy(ValueList* VL) {
44 while (!VL->empty()) {
45 ValueInfo& VI = VL->back();
52 void UpgradeAssembly(const std::string &infile, std::istream& in,
53 std::ostream &out, bool debug)
62 std::cerr << "Parse failed.\n";
67 static void ResolveType(TypeInfo& Ty) {
68 if (Ty.oldTy == UnresolvedTy) {
69 TypeMap::iterator I = NamedTypes.find(*Ty.newTy);
70 if (I != NamedTypes.end()) {
71 Ty.oldTy = I->second.oldTy;
72 Ty.elemTy = I->second.elemTy;
74 std::string msg("Can't resolve type: ");
78 } else if (Ty.oldTy == NumericTy) {
79 unsigned ref = atoi(&((Ty.newTy->c_str())[1])); // Skip the '\\'
80 if (ref < EnumeratedTypes.size()) {
81 Ty.oldTy = EnumeratedTypes[ref].oldTy;
82 Ty.elemTy = EnumeratedTypes[ref].elemTy;
84 std::string msg("Can't resolve type: ");
89 // otherwise its already resolved.
92 static const char* getCastOpcode(
93 std::string& Source, const TypeInfo& SrcTy, const TypeInfo& DstTy)
95 unsigned SrcBits = SrcTy.getBitWidth();
96 unsigned DstBits = DstTy.getBitWidth();
97 const char* opcode = "bitcast";
98 // Run through the possibilities ...
99 if (DstTy.isIntegral()) { // Casting to integral
100 if (SrcTy.isIntegral()) { // Casting from integral
101 if (DstBits < SrcBits)
103 else if (DstBits > SrcBits) { // its an extension
104 if (SrcTy.isSigned())
105 opcode ="sext"; // signed -> SEXT
107 opcode = "zext"; // unsigned -> ZEXT
109 opcode = "bitcast"; // Same size, No-op cast
111 } else if (SrcTy.isFloatingPoint()) { // Casting from floating pt
112 if (DstTy.isSigned())
113 opcode = "fptosi"; // FP -> sint
115 opcode = "fptoui"; // FP -> uint
116 } else if (SrcTy.isPacked()) {
117 assert(DstBits == SrcTy.getBitWidth() &&
118 "Casting packed to integer of different width");
119 opcode = "bitcast"; // same size, no-op cast
121 assert(SrcTy.isPointer() &&
122 "Casting from a value that is not first-class type");
123 opcode = "ptrtoint"; // ptr -> int
125 } else if (DstTy.isFloatingPoint()) { // Casting to floating pt
126 if (SrcTy.isIntegral()) { // Casting from integral
127 if (SrcTy.isSigned())
128 opcode = "sitofp"; // sint -> FP
130 opcode = "uitofp"; // uint -> FP
131 } else if (SrcTy.isFloatingPoint()) { // Casting from floating pt
132 if (DstBits < SrcBits) {
133 opcode = "fptrunc"; // FP -> smaller FP
134 } else if (DstBits > SrcBits) {
135 opcode = "fpext"; // FP -> larger FP
137 opcode ="bitcast"; // same size, no-op cast
139 } else if (SrcTy.isPacked()) {
140 assert(DstBits == SrcTy.getBitWidth() &&
141 "Casting packed to floating point of different width");
142 opcode = "bitcast"; // same size, no-op cast
144 assert(0 && "Casting pointer or non-first class to float");
146 } else if (DstTy.isPacked()) {
147 if (SrcTy.isPacked()) {
148 assert(DstTy.getBitWidth() == SrcTy.getBitWidth() &&
149 "Casting packed to packed of different widths");
150 opcode = "bitcast"; // packed -> packed
151 } else if (DstTy.getBitWidth() == SrcBits) {
152 opcode = "bitcast"; // float/int -> packed
154 assert(!"Illegal cast to packed (wrong type or size)");
156 } else if (DstTy.isPointer()) {
157 if (SrcTy.isPointer()) {
158 opcode = "bitcast"; // ptr -> ptr
159 } else if (SrcTy.isIntegral()) {
160 opcode = "inttoptr"; // int -> ptr
162 assert(!"Casting invalid type to pointer");
165 assert(!"Casting to type that is not first-class");
170 static std::string getCastUpgrade(
171 const std::string& Src, TypeInfo& SrcTy, TypeInfo& DstTy, bool isConst)
174 std::string Source = Src;
175 if (SrcTy.isFloatingPoint() && DstTy.isPointer()) {
176 // fp -> ptr cast is no longer supported but we must upgrade this
177 // by doing a double cast: fp -> int -> ptr
179 Source = "ulong fptoui(" + Source + " to ulong)";
181 *O << " %cast_upgrade" << unique << " = fptoui " << Source
183 Source = "ulong %cast_upgrade" + llvm::utostr(unique);
185 // Update the SrcTy for the getCastOpcode call below
187 SrcTy.newTy = new std::string("ulong");
188 SrcTy.oldTy = ULongTy;
189 } else if (DstTy.oldTy == BoolTy) {
190 // cast ptr %x to bool was previously defined as setne ptr %x, null
191 // The ptrtoint semantic is to truncate, not compare so we must retain
192 // the original intent by replace the cast with a setne
193 const char* comparator = SrcTy.isPointer() ? ", null" :
194 (SrcTy.isFloatingPoint() ? ", 0.0" : ", 0");
196 Result = "setne (" + Source + comparator + ")";
198 Result = "setne " + Source + comparator;
199 return Result; // skip cast processing below
203 std::string Opcode(getCastOpcode(Source, SrcTy, DstTy));
205 Result += Opcode + "( " + Source + " to " + *DstTy.newTy + ")";
207 Result += Opcode + " " + Source + " to " + *DstTy.newTy;
211 const char* getDivRemOpcode(const std::string& opcode, const TypeInfo& TI) {
212 const char* op = opcode.c_str();
216 Ty.oldTy = Ty.getElementType();
218 if (Ty.isFloatingPoint())
220 else if (Ty.isUnsigned())
222 else if (Ty.isSigned())
225 yyerror("Invalid type for div instruction");
226 else if (opcode == "rem")
227 if (Ty.isFloatingPoint())
229 else if (Ty.isUnsigned())
231 else if (Ty.isSigned())
234 yyerror("Invalid type for rem instruction");
239 getCompareOp(const std::string& setcc, const TypeInfo& TI) {
240 assert(setcc.length() == 5);
243 assert(cc1 == 'e' || cc1 == 'n' || cc1 == 'l' || cc1 == 'g');
244 assert(cc2 == 'q' || cc2 == 'e' || cc2 == 'e' || cc2 == 't');
245 std::string result("xcmp xxx");
248 if (TI.isFloatingPoint()) {
250 result[5] = 'o'; // FIXME: Always map to ordered comparison ?
251 } else if (TI.isIntegral() || TI.isPointer()) {
253 if ((cc1 == 'e' && cc2 == 'q') || (cc1 == 'n' && cc2 == 'e'))
255 else if (TI.isSigned())
257 else if (TI.isUnsigned() || TI.isPointer())
260 yyerror("Invalid integral type for setcc");
267 %file-prefix="UpgradeParser"
277 %token <Type> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
278 %token <Type> FLOAT DOUBLE LABEL
279 %token <String> OPAQUE ESINT64VAL EUINT64VAL SINTVAL UINTVAL FPVAL
280 %token <String> NULL_TOK UNDEF ZEROINITIALIZER TRUETOK FALSETOK
281 %token <String> TYPE VAR_ID LABELSTR STRINGCONSTANT
282 %token <String> IMPLEMENTATION BEGINTOK ENDTOK
283 %token <String> DECLARE GLOBAL CONSTANT SECTION VOLATILE
284 %token <String> TO DOTDOTDOT CONST INTERNAL LINKONCE WEAK
285 %token <String> DLLIMPORT DLLEXPORT EXTERN_WEAK APPENDING
286 %token <String> NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
287 %token <String> ALIGN UNINITIALIZED
288 %token <String> DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
289 %token <String> CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
290 %token <String> X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
291 %token <String> DATALAYOUT
292 %token <String> RET BR SWITCH INVOKE EXCEPT UNWIND UNREACHABLE
293 %token <String> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM AND OR XOR
294 %token <String> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
295 %token <String> ICMP FCMP EQ NE SLT SGT SLE SGE OEQ ONE OLT OGT OLE OGE
296 %token <String> ORD UNO UEQ UNE ULT UGT ULE UGE
297 %token <String> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
298 %token <String> PHI_TOK SELECT SHL SHR ASHR LSHR VAARG
299 %token <String> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
300 %token <String> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI UITOFP SITOFP
301 %token <String> PTRTOINT INTTOPTR BITCAST
303 %type <String> OptAssign OptLinkage OptCallingConv OptAlign OptCAlign
304 %type <String> SectionString OptSection GlobalVarAttributes GlobalVarAttribute
305 %type <String> ArgTypeListI ConstExpr DefinitionList
306 %type <String> ConstPool TargetDefinition LibrariesDefinition LibList OptName
307 %type <String> ArgVal ArgListH ArgList FunctionHeaderH BEGIN FunctionHeader END
308 %type <String> Function FunctionProto BasicBlock TypeListI
309 %type <String> InstructionList BBTerminatorInst JumpTable Inst PHIList
310 %type <String> OptTailCall InstVal OptVolatile Unwind
311 %type <String> MemoryInst SymbolicValueRef OptSideEffect GlobalType
312 %type <String> FnDeclareLinkage BasicBlockList BigOrLittle AsmBlock
313 %type <String> Name ConstValueRef ConstVector External
314 %type <String> ShiftOps SetCondOps LogicalOps ArithmeticOps CastOps
315 %type <String> IPredicates FPredicates
317 %type <ValList> ValueRefList ValueRefListE IndexList
319 %type <Type> IntType SIntType UIntType FPType TypesV Types
320 %type <Type> PrimType UpRTypesV UpRTypes
322 %type <String> IntVal EInt64Val
323 %type <Const> ConstVal
325 %type <Value> ValueRef ResolvedVal
331 // Handle constant integer size restriction and conversion...
332 IntVal : SINTVAL | UINTVAL ;
333 EInt64Val : ESINT64VAL | EUINT64VAL;
335 // Operations that are notably excluded from this list include:
336 // RET, BR, & SWITCH because they end basic blocks and are treated specially.
337 ArithmeticOps: ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV
338 | REM | UREM | SREM | FREM;
339 LogicalOps : AND | OR | XOR;
340 SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
341 IPredicates : EQ | NE | SLT | SGT | SLE | SGE | ULT | UGT | ULE | UGE;
342 FPredicates : OEQ | ONE | OLT | OGT | OLE | OGE | ORD | UNO | UEQ | UNE
343 | ULT | UGT | ULE | UGE | TRUETOK | FALSETOK;
344 ShiftOps : SHL | SHR | ASHR | LSHR;
345 CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI |
346 UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
349 // These are some types that allow classification if we only want a particular
350 // thing... for example, only a signed, unsigned, or integral type.
351 SIntType : LONG | INT | SHORT | SBYTE;
352 UIntType : ULONG | UINT | USHORT | UBYTE;
353 IntType : SIntType | UIntType;
354 FPType : FLOAT | DOUBLE;
356 // OptAssign - Value producing statements have an optional assignment component
357 OptAssign : Name '=' {
361 $$ = new std::string("");
365 : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT
367 | /*empty*/ { $$ = new std::string(""); } ;
370 : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK
372 | CC_TOK EUINT64VAL {
377 | /*empty*/ { $$ = new std::string(""); } ;
379 // OptAlign/OptCAlign - An optional alignment, and an optional alignment with
380 // a comma before it.
382 : /*empty*/ { $$ = new std::string(); }
383 | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
386 : /*empty*/ { $$ = new std::string(); }
387 | ',' ALIGN EUINT64VAL {
395 : SECTION STRINGCONSTANT {
401 OptSection : /*empty*/ { $$ = new std::string(); }
405 : /* empty */ { $$ = new std::string(); }
406 | ',' GlobalVarAttribute GlobalVarAttributes {
422 //===----------------------------------------------------------------------===//
423 // Types includes all predefined types... except void, because it can only be
424 // used in specific contexts (function returning void for example). To have
425 // access to it, a user must explicitly use TypesV.
428 // TypesV includes all of 'Types', but it also includes the void type.
429 TypesV : Types | VOID ;
430 UpRTypesV : UpRTypes | VOID ;
433 // Derived types are added later...
435 PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
436 PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
444 $$.oldTy = UnresolvedTy;
449 | '\\' EUINT64VAL { // Type UpReference
452 $$.oldTy = NumericTy;
454 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
455 *$1.newTy += "( " + *$3 + " )";
458 $$.oldTy = FunctionTy;
460 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
462 *$2 += " x " + *$4.newTy + " ]";
466 $$.elemTy = $4.oldTy;
468 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
470 *$2 += " x " + *$4.newTy + " >";
474 $$.elemTy = $4.oldTy;
476 | '{' TypeListI '}' { // Structure type?
482 | '{' '}' { // Empty structure type?
483 $$.newTy = new std::string("{}");
486 | UpRTypes '*' { // Pointer type?
488 $$.elemTy = $1.oldTy;
489 $1.oldTy = PointerTy;
493 // TypeList - Used for struct declarations and as a basis for function type
494 // declaration type lists
500 | TypeListI ',' UpRTypes {
501 *$1 += ", " + *$3.newTy;
506 // ArgTypeList - List of types for a function type declaration...
509 | TypeListI ',' DOTDOTDOT {
518 $$ = new std::string();
521 // ConstVal - The various declarations that go into the constant pool. This
522 // production is used ONLY to represent constants that show up AFTER a 'const',
523 // 'constant' or 'global' token at global scope. Constants that can be inlined
524 // into other expressions (such as integers and constexprs) are handled by the
525 // ResolvedVal, ValueRef and ConstValueRef productions.
527 ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
529 $$.cnst = new std::string(*$1.newTy);
530 *$$.cnst += " [ " + *$3 + " ]";
535 $$.cnst = new std::string(*$1.newTy);
538 | Types 'c' STRINGCONSTANT {
540 $$.cnst = new std::string(*$1.newTy);
541 *$$.cnst += " c" + *$3;
544 | Types '<' ConstVector '>' { // Nonempty unsized arr
546 $$.cnst = new std::string(*$1.newTy);
547 *$$.cnst += " < " + *$3 + " >";
550 | Types '{' ConstVector '}' {
552 $$.cnst = new std::string(*$1.newTy);
553 *$$.cnst += " { " + *$3 + " }";
558 $$.cnst = new std::string(*$1.newTy);
563 $$.cnst = new std::string(*$1.newTy);
564 *$$.cnst += " " + *$2;
569 $$.cnst = new std::string(*$1.newTy);
570 *$$.cnst += " " + *$2;
573 | Types SymbolicValueRef {
575 $$.cnst = new std::string(*$1.newTy);
576 *$$.cnst += " " + *$2;
581 $$.cnst = new std::string(*$1.newTy);
582 *$$.cnst += " " + *$2;
585 | Types ZEROINITIALIZER {
587 $$.cnst = new std::string(*$1.newTy);
588 *$$.cnst += " " + *$2;
591 | SIntType EInt64Val { // integral constants
593 $$.cnst = new std::string(*$1.newTy);
594 *$$.cnst += " " + *$2;
597 | UIntType EUINT64VAL { // integral constants
599 $$.cnst = new std::string(*$1.newTy);
600 *$$.cnst += " " + *$2;
603 | BOOL TRUETOK { // Boolean constants
605 $$.cnst = new std::string(*$1.newTy);
606 *$$.cnst += " " + *$2;
609 | BOOL FALSETOK { // Boolean constants
611 $$.cnst = new std::string(*$1.newTy);
612 *$$.cnst += " " + *$2;
615 | FPType FPVAL { // Float & Double constants
617 $$.cnst = new std::string(*$1.newTy);
618 *$$.cnst += " " + *$2;
623 ConstExpr: CastOps '(' ConstVal TO Types ')' {
624 std::string source = *$3.cnst;
628 // Call getCastUpgrade to upgrade the old cast
629 $$ = new std::string(getCastUpgrade(source, $3.type, $5, true));
631 // Nothing to upgrade, just create the cast constant expr
632 $$ = new std::string(*$1);
633 *$$ += "( " + source + " to " + *$5.newTy + ")";
635 delete $1; $3.destroy(); delete $4; $5.destroy();
637 | GETELEMENTPTR '(' ConstVal IndexList ')' {
638 *$1 += "(" + *$3.cnst;
639 for (unsigned i = 0; i < $4->size(); ++i) {
640 ValueInfo& VI = (*$4)[i];
641 *$1 += ", " + *VI.val;
649 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
650 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
651 $3.destroy(); $5.destroy(); $7.destroy();
654 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
655 const char* op = getDivRemOpcode(*$1, $3.type);
656 $$ = new std::string(op);
657 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
658 delete $1; $3.destroy(); $5.destroy();
660 | LogicalOps '(' ConstVal ',' ConstVal ')' {
661 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
662 $3.destroy(); $5.destroy();
665 | SetCondOps '(' ConstVal ',' ConstVal ')' {
666 #if UPGRADE_SETCOND_OPS
667 *$1 = getCompareOp(*$1, $3.type);
669 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
670 $3.destroy(); $5.destroy();
673 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
674 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
675 delete $2; $4.destroy(); $6.destroy();
678 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
679 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
680 delete $2; $4.destroy(); $6.destroy();
683 | ShiftOps '(' ConstVal ',' ConstVal ')' {
684 const char* shiftop = $1->c_str();
686 shiftop = ($3.type.isUnsigned()) ? "lshr" : "ashr";
687 $$ = new std::string(shiftop);
688 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
689 delete $1; $3.destroy(); $5.destroy();
691 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
692 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
693 $3.destroy(); $5.destroy();
696 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
697 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
698 $3.destroy(); $5.destroy(); $7.destroy();
701 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
702 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
703 $3.destroy(); $5.destroy(); $7.destroy();
708 // ConstVector - A list of comma separated constants.
711 : ConstVector ',' ConstVal {
712 *$1 += ", " + *$3.cnst;
716 | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
720 // GlobalType - Match either GLOBAL or CONSTANT for global declarations...
721 GlobalType : GLOBAL | CONSTANT ;
724 //===----------------------------------------------------------------------===//
725 // Rules to match Modules
726 //===----------------------------------------------------------------------===//
728 // Module rule: Capture the result of parsing the whole file into a result
731 Module : DefinitionList {
734 // DefinitionList - Top level definitions
736 DefinitionList : DefinitionList Function {
739 | DefinitionList FunctionProto {
744 | DefinitionList MODULE ASM_TOK AsmBlock {
745 *O << "module asm " << " " << *$4 << "\n";
748 | DefinitionList IMPLEMENTATION {
749 *O << "implementation\n";
752 | ConstPool { $$ = 0; }
754 External : EXTERNAL | UNINITIALIZED { $$ = $1; *$$ = "external"; }
756 // ConstPool - Constants with optional names assigned to them.
757 ConstPool : ConstPool OptAssign TYPE TypesV {
758 EnumeratedTypes.push_back($4);
760 NamedTypes[*$2].newTy = new std::string(*$4.newTy);
761 NamedTypes[*$2].oldTy = $4.oldTy;
762 NamedTypes[*$2].elemTy = $4.elemTy;
765 *O << "type " << *$4.newTy << "\n";
766 delete $2; delete $3; $4.destroy();
769 | ConstPool FunctionProto { // Function prototypes can be in const pool
774 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
775 *O << *$2 << " " << *$3 << " " << *$4 << "\n";
776 delete $2; delete $3; delete $4;
779 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
782 *O << *$3 << " " << *$4 << " " << *$5.cnst << " " << *$6 << "\n";
783 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
786 | ConstPool OptAssign External GlobalType Types GlobalVarAttributes {
789 *O << *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
790 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
793 | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
796 *O << *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
797 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
800 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
803 *O << *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
804 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
807 | ConstPool TARGET TargetDefinition {
808 *O << *$2 << " " << *$3 << "\n";
809 delete $2; delete $3;
812 | ConstPool DEPLIBS '=' LibrariesDefinition {
813 *O << *$2 << " = " << *$4 << "\n";
814 delete $2; delete $4;
817 | /* empty: end of list */ {
822 AsmBlock : STRINGCONSTANT ;
824 BigOrLittle : BIG | LITTLE
827 : ENDIAN '=' BigOrLittle {
832 | POINTERSIZE '=' EUINT64VAL {
839 | TRIPLE '=' STRINGCONSTANT {
844 | DATALAYOUT '=' STRINGCONSTANT {
858 : LibList ',' STRINGCONSTANT {
864 | /* empty: end of list */ {
865 $$ = new std::string();
868 //===----------------------------------------------------------------------===//
869 // Rules to match Function Headers
870 //===----------------------------------------------------------------------===//
872 Name : VAR_ID | STRINGCONSTANT;
873 OptName : Name | /*empty*/ { $$ = new std::string(); };
875 ArgVal : Types OptName {
882 ArgListH : ArgListH ',' ArgVal {
893 | ArgListH ',' DOTDOTDOT {
901 | /* empty */ { $$ = new std::string(); };
903 FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
904 OptSection OptAlign {
908 *$1 += *$2.newTy + " " + *$3 + "(" + *$5 + ")";
923 BEGIN : BEGINTOK { $$ = new std::string("{"); delete $1; }
924 | '{' { $$ = new std::string ("{"); }
926 FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
930 *O << *$2 << " " << *$3 << "\n";
931 delete $1; delete $2; delete $3;
935 END : ENDTOK { $$ = new std::string("}"); delete $1; }
936 | '}' { $$ = new std::string("}"); };
938 Function : FunctionHeader BasicBlockList END {
941 *O << '\n' << *$3 << "\n";
946 : /*default*/ { $$ = new std::string(); }
952 : DECLARE FnDeclareLinkage FunctionHeaderH {
961 //===----------------------------------------------------------------------===//
962 // Rules to match Basic Blocks
963 //===----------------------------------------------------------------------===//
965 OptSideEffect : /* empty */ { $$ = new std::string(); }
969 : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK | NULL_TOK | UNDEF
971 | '<' ConstVector '>' {
977 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
981 *$1 += " " + *$3 + ", " + *$5;
982 delete $2; delete $3; delete $5;
986 SymbolicValueRef : IntVal | Name ;
988 // ValueRef - A reference to a definition... either constant or symbolic
994 $$.type.oldTy = UnresolvedTy;
1000 $$.type.oldTy = UnresolvedTy;
1004 // ResolvedVal - a <type> <value> pair. This is used only in cases where the
1005 // type immediately preceeds the value reference, and allows complex constant
1006 // pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1007 ResolvedVal : Types ValueRef {
1010 $$.val->insert(0, *$1.newTy + " ");
1013 BasicBlockList : BasicBlockList BasicBlock {
1016 | BasicBlock { // Do not allow functions with 0 basic blocks
1021 // Basic blocks are terminated by branching instructions:
1022 // br, br/cc, switch, ret
1024 BasicBlock : InstructionList BBTerminatorInst {
1028 InstructionList : InstructionList Inst {
1029 *O << " " << *$2 << "\n";
1042 Unwind : UNWIND | EXCEPT { $$ = $1; *$$ = "unwind"; }
1044 BBTerminatorInst : RET ResolvedVal { // Return with a result...
1045 *O << " " << *$1 << " " << *$2.val << "\n";
1046 delete $1; $2.destroy();
1049 | RET VOID { // Return with no result...
1050 *O << " " << *$1 << " " << *$2.newTy << "\n";
1051 delete $1; $2.destroy();
1054 | BR LABEL ValueRef { // Unconditional Branch...
1055 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << "\n";
1056 delete $1; $2.destroy(); $3.destroy();
1058 } // Conditional Branch...
1059 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
1060 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1061 << *$5.newTy << " " << *$6.val << ", " << *$8.newTy << " "
1063 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1064 $8.destroy(); $9.destroy();
1067 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1068 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1069 << *$5.newTy << " " << *$6.val << " [" << *$8 << " ]\n";
1070 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1074 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1075 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1076 << *$5.newTy << " " << *$6.val << "[]\n";
1077 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1080 | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
1081 TO LABEL ValueRef Unwind LABEL ValueRef {
1085 *O << *$2 << " " << *$3 << " " << *$4.newTy << " " << *$5.val << " (";
1086 for (unsigned i = 0; i < $7->size(); ++i) {
1087 ValueInfo& VI = (*$7)[i];
1089 if (i+1 < $7->size())
1093 *O << ") " << *$9 << " " << *$10.newTy << " " << *$11.val << " "
1094 << *$12 << " " << *$13.newTy << " " << *$14.val << "\n";
1095 delete $1; delete $2; delete $3; $4.destroy(); $5.destroy(); delete $7;
1096 delete $9; $10.destroy(); $11.destroy(); delete $12; $13.destroy();
1101 *O << " " << *$1 << "\n";
1106 *O << " " << *$1 << "\n";
1111 JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1112 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5.newTy + " " + *$6.val;
1113 $2.destroy(); delete $3; $5.destroy(); $6.destroy();
1116 | IntType ConstValueRef ',' LABEL ValueRef {
1117 $2->insert(0, *$1.newTy + " " );
1118 *$2 += ", " + *$4.newTy + " " + *$5.val;
1119 $1.destroy(); $4.destroy(); $5.destroy();
1124 : OptAssign InstVal {
1133 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1134 $3.val->insert(0, *$1.newTy + "[");
1135 *$3.val += "," + *$5.val + "]";
1136 $1.destroy(); $5.destroy();
1137 $$ = new std::string(*$3.val);
1140 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1141 *$1 += ", [" + *$4.val + "," + *$6.val + "]";
1142 $4.destroy(); $6.destroy();
1149 $$ = new ValueList();
1152 | ValueRefList ',' ResolvedVal {
1157 // ValueRefListE - Just like ValueRefList, except that it may also be empty!
1159 : ValueRefList { $$ = $1; }
1160 | /*empty*/ { $$ = new ValueList(); }
1172 InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1173 const char* op = getDivRemOpcode(*$1, $2);
1174 $$ = new std::string(op);
1175 *$$ += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1176 delete $1; $2.destroy(); $3.destroy(); $5.destroy();
1178 | LogicalOps Types ValueRef ',' ValueRef {
1179 *$1 += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1180 $2.destroy(); $3.destroy(); $5.destroy();
1183 | SetCondOps Types ValueRef ',' ValueRef {
1184 #if UPGRADE_SETCOND_OPS
1185 *$1 = getCompareOp(*$1, $2);
1187 *$1 += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1188 $2.destroy(); $3.destroy(); $5.destroy();
1191 | ICMP IPredicates Types ValueRef ',' ValueRef ')' {
1192 *$1 += " " + *$2 + " " + *$4.val + "," + *$6.val + ")";
1193 delete $2; $4.destroy(); $6.destroy();
1196 | FCMP FPredicates Types ValueRef ',' ValueRef ')' {
1197 *$1 += " " + *$2 + " " + *$4.val + "," + *$6.val + ")";
1198 delete $2; $4.destroy(); $6.destroy();
1202 *$1 += " " + *$2.val;
1206 | ShiftOps ResolvedVal ',' ResolvedVal {
1207 const char* shiftop = $1->c_str();
1209 shiftop = ($2.type.isUnsigned()) ? "lshr" : "ashr";
1210 $$ = new std::string(shiftop);
1211 *$$ += " " + *$2.val + ", " + *$4.val;
1212 delete $1; $2.destroy(); $4.destroy();
1214 | CastOps ResolvedVal TO Types {
1215 std::string source = *$2.val;
1216 TypeInfo SrcTy = $2.type;
1217 TypeInfo DstTy = $4;
1219 $$ = new std::string();
1220 if (*$1 == "cast") {
1221 *$$ += getCastUpgrade(source, SrcTy, DstTy, false);
1223 *$$ += *$1 + " " + source + " to " + *DstTy.newTy;
1225 delete $1; $2.destroy();
1226 delete $3; $4.destroy();
1228 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1229 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1230 $2.destroy(); $4.destroy(); $6.destroy();
1233 | VAARG ResolvedVal ',' Types {
1234 *$1 += " " + *$2.val + ", " + *$4.newTy;
1235 $2.destroy(); $4.destroy();
1238 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
1239 *$1 += " " + *$2.val + ", " + *$4.val;
1240 $2.destroy(); $4.destroy();
1243 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1244 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1245 $2.destroy(); $4.destroy(); $6.destroy();
1248 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1249 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1250 $2.destroy(); $4.destroy(); $6.destroy();
1258 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
1263 *$1 += *$3.newTy + " " + *$4.val + "(";
1264 for (unsigned i = 0; i < $6->size(); ++i) {
1265 ValueInfo& VI = (*$6)[i];
1267 if (i+1 < $6->size())
1272 delete $2; $3.destroy(); $4.destroy(); delete $6;
1278 // IndexList - List of indices for GEP based instructions...
1280 : ',' ValueRefList { $$ = $2; }
1281 | /* empty */ { $$ = new ValueList(); }
1286 | /* empty */ { $$ = new std::string(); }
1289 MemoryInst : MALLOC Types OptCAlign {
1290 *$1 += " " + *$2.newTy;
1293 $2.destroy(); delete $3;
1296 | MALLOC Types ',' UINT ValueRef OptCAlign {
1297 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5.val;
1300 $2.destroy(); $4.destroy(); $5.destroy(); delete $6;
1303 | ALLOCA Types OptCAlign {
1304 *$1 += " " + *$2.newTy;
1307 $2.destroy(); delete $3;
1310 | ALLOCA Types ',' UINT ValueRef OptCAlign {
1311 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5.val;
1314 $2.destroy(); $4.destroy(); $5.destroy(); delete $6;
1317 | FREE ResolvedVal {
1318 *$1 += " " + *$2.val;
1322 | OptVolatile LOAD Types ValueRef {
1325 *$1 += *$2 + " " + *$3.newTy + " " + *$4.val;
1326 delete $2; $3.destroy(); $4.destroy();
1329 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1332 *$1 += *$2 + " " + *$3.val + ", " + *$5.newTy + " " + *$6.val;
1333 delete $2; $3.destroy(); $5.destroy(); $6.destroy();
1336 | GETELEMENTPTR Types ValueRef IndexList {
1337 // Upgrade the indices
1338 for (unsigned i = 0; i < $4->size(); ++i) {
1339 ValueInfo& VI = (*$4)[i];
1340 if (VI.type.isUnsigned() && !VI.isConstant() &&
1341 VI.type.getBitWidth() < 64) {
1342 std::string* old = VI.val;
1343 *O << " %gep_upgrade" << unique << " = zext " << *old
1345 VI.val = new std::string("ulong %gep_upgrade" + llvm::utostr(unique++));
1346 VI.type.oldTy = ULongTy;
1350 *$1 += " " + *$2.newTy + " " + *$3.val;
1351 for (unsigned i = 0; i < $4->size(); ++i) {
1352 ValueInfo& VI = (*$4)[i];
1353 *$1 += ", " + *VI.val;
1356 $2.destroy(); $3.destroy(); delete $4;
1362 int yyerror(const char *ErrorMsg) {
1364 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1365 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
1366 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
1367 if (yychar == YYEMPTY || yychar == 0)
1368 errMsg += "end-of-file.";
1370 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
1371 std::cerr << errMsg << '\n';