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
27 #define GENERATE_FCMP_INSTS 0
29 int yylex(); // declaration" of xxx warnings.
33 static std::string CurFilename;
34 static std::ostream *O = 0;
35 std::istream* LexInput = 0;
36 unsigned SizeOfPointer = 32;
37 static uint64_t unique = 1;
39 typedef std::vector<TypeInfo> TypeVector;
40 static TypeVector EnumeratedTypes;
41 typedef std::map<std::string,TypeInfo> TypeMap;
42 static TypeMap NamedTypes;
43 static TypeMap Globals;
45 void destroy(ValueList* VL) {
46 while (!VL->empty()) {
47 ValueInfo& VI = VL->back();
54 void UpgradeAssembly(const std::string &infile, std::istream& in,
55 std::ostream &out, bool debug)
64 std::cerr << "Parse failed.\n";
69 static void ResolveType(TypeInfo& Ty) {
70 if (Ty.oldTy == UnresolvedTy) {
71 TypeMap::iterator I = NamedTypes.find(*Ty.newTy);
72 if (I != NamedTypes.end()) {
73 Ty.oldTy = I->second.oldTy;
74 Ty.elemTy = I->second.elemTy;
76 std::string msg("Can't resolve type: ");
80 } else if (Ty.oldTy == NumericTy) {
81 unsigned ref = atoi(&((Ty.newTy->c_str())[1])); // Skip the '\\'
82 if (ref < EnumeratedTypes.size()) {
83 Ty.oldTy = EnumeratedTypes[ref].oldTy;
84 Ty.elemTy = EnumeratedTypes[ref].elemTy;
86 std::string msg("Can't resolve type: ");
91 // otherwise its already resolved.
94 static const char* getCastOpcode(
95 std::string& Source, const TypeInfo& SrcTy, const TypeInfo& DstTy)
97 unsigned SrcBits = SrcTy.getBitWidth();
98 unsigned DstBits = DstTy.getBitWidth();
99 const char* opcode = "bitcast";
100 // Run through the possibilities ...
101 if (DstTy.isIntegral()) { // Casting to integral
102 if (SrcTy.isIntegral()) { // Casting from integral
103 if (DstBits < SrcBits)
105 else if (DstBits > SrcBits) { // its an extension
106 if (SrcTy.isSigned())
107 opcode ="sext"; // signed -> SEXT
109 opcode = "zext"; // unsigned -> ZEXT
111 opcode = "bitcast"; // Same size, No-op cast
113 } else if (SrcTy.isFloatingPoint()) { // Casting from floating pt
114 if (DstTy.isSigned())
115 opcode = "fptosi"; // FP -> sint
117 opcode = "fptoui"; // FP -> uint
118 } else if (SrcTy.isPacked()) {
119 assert(DstBits == SrcTy.getBitWidth() &&
120 "Casting packed to integer of different width");
121 opcode = "bitcast"; // same size, no-op cast
123 assert(SrcTy.isPointer() &&
124 "Casting from a value that is not first-class type");
125 opcode = "ptrtoint"; // ptr -> int
127 } else if (DstTy.isFloatingPoint()) { // Casting to floating pt
128 if (SrcTy.isIntegral()) { // Casting from integral
129 if (SrcTy.isSigned())
130 opcode = "sitofp"; // sint -> FP
132 opcode = "uitofp"; // uint -> FP
133 } else if (SrcTy.isFloatingPoint()) { // Casting from floating pt
134 if (DstBits < SrcBits) {
135 opcode = "fptrunc"; // FP -> smaller FP
136 } else if (DstBits > SrcBits) {
137 opcode = "fpext"; // FP -> larger FP
139 opcode ="bitcast"; // same size, no-op cast
141 } else if (SrcTy.isPacked()) {
142 assert(DstBits == SrcTy.getBitWidth() &&
143 "Casting packed to floating point of different width");
144 opcode = "bitcast"; // same size, no-op cast
146 assert(0 && "Casting pointer or non-first class to float");
148 } else if (DstTy.isPacked()) {
149 if (SrcTy.isPacked()) {
150 assert(DstTy.getBitWidth() == SrcTy.getBitWidth() &&
151 "Casting packed to packed of different widths");
152 opcode = "bitcast"; // packed -> packed
153 } else if (DstTy.getBitWidth() == SrcBits) {
154 opcode = "bitcast"; // float/int -> packed
156 assert(!"Illegal cast to packed (wrong type or size)");
158 } else if (DstTy.isPointer()) {
159 if (SrcTy.isPointer()) {
160 opcode = "bitcast"; // ptr -> ptr
161 } else if (SrcTy.isIntegral()) {
162 opcode = "inttoptr"; // int -> ptr
164 assert(!"Casting invalid type to pointer");
167 assert(!"Casting to type that is not first-class");
172 static std::string getCastUpgrade(
173 const std::string& Src, TypeInfo& SrcTy, TypeInfo& DstTy, bool isConst)
176 std::string Source = Src;
177 if (SrcTy.isFloatingPoint() && DstTy.isPointer()) {
178 // fp -> ptr cast is no longer supported but we must upgrade this
179 // by doing a double cast: fp -> int -> ptr
181 Source = "ulong fptoui(" + Source + " to ulong)";
183 *O << " %cast_upgrade" << unique << " = fptoui " << Source
185 Source = "ulong %cast_upgrade" + llvm::utostr(unique);
187 // Update the SrcTy for the getCastOpcode call below
189 SrcTy.newTy = new std::string("ulong");
190 SrcTy.oldTy = ULongTy;
191 } else if (DstTy.oldTy == BoolTy && SrcTy.oldTy != BoolTy) {
192 // cast ptr %x to bool was previously defined as setne ptr %x, null
193 // The ptrtoint semantic is to truncate, not compare so we must retain
194 // the original intent by replace the cast with a setne
195 const char* comparator = SrcTy.isPointer() ? ", null" :
196 (SrcTy.isFloatingPoint() ? ", 0.0" : ", 0");
197 #if UPGRADE_SETCOND_OPS
198 const char* compareOp = SrcTy.isFloatingPoint() ? "setne " : "icmp ne ";
200 const char* compareOp = "setne";
203 Result = "(" + Source + comparator + ")";
204 Result = compareOp + Result;
206 Result = compareOp + Source + comparator;
207 return Result; // skip cast processing below
211 std::string Opcode(getCastOpcode(Source, SrcTy, DstTy));
213 Result += Opcode + "( " + Source + " to " + *DstTy.newTy + ")";
215 Result += Opcode + " " + Source + " to " + *DstTy.newTy;
219 const char* getDivRemOpcode(const std::string& opcode, const TypeInfo& TI) {
220 const char* op = opcode.c_str();
224 Ty.oldTy = Ty.getElementType();
226 if (Ty.isFloatingPoint())
228 else if (Ty.isUnsigned())
230 else if (Ty.isSigned())
233 yyerror("Invalid type for div instruction");
234 else if (opcode == "rem")
235 if (Ty.isFloatingPoint())
237 else if (Ty.isUnsigned())
239 else if (Ty.isSigned())
242 yyerror("Invalid type for rem instruction");
247 getCompareOp(const std::string& setcc, const TypeInfo& TI) {
248 assert(setcc.length() == 5);
251 assert(cc1 == 'e' || cc1 == 'n' || cc1 == 'l' || cc1 == 'g');
252 assert(cc2 == 'q' || cc2 == 'e' || cc2 == 'e' || cc2 == 't');
253 std::string result("xcmp xxx");
256 if (TI.isFloatingPoint()) {
257 #if GENERATE_FCMP_INSTS
259 result[5] = 'o'; // FIXME: Always map to ordered comparison ?
261 result[5] = 'u'; // NE maps to unordered
263 result[5] = 'o'; // everything else maps to ordered
267 } else if (TI.isIntegral() || TI.isPointer()) {
269 if ((cc1 == 'e' && cc2 == 'q') || (cc1 == 'n' && cc2 == 'e'))
271 else if (TI.isSigned())
273 else if (TI.isUnsigned() || TI.isPointer() || TI.isBool())
276 yyerror("Invalid integral type for setcc");
283 // %file-prefix="UpgradeParser"
293 %token <Type> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
294 %token <Type> FLOAT DOUBLE LABEL
295 %token <String> OPAQUE ESINT64VAL EUINT64VAL SINTVAL UINTVAL FPVAL
296 %token <String> NULL_TOK UNDEF ZEROINITIALIZER TRUETOK FALSETOK
297 %token <String> TYPE VAR_ID LABELSTR STRINGCONSTANT
298 %token <String> IMPLEMENTATION BEGINTOK ENDTOK
299 %token <String> DECLARE GLOBAL CONSTANT SECTION VOLATILE
300 %token <String> TO DOTDOTDOT CONST INTERNAL LINKONCE WEAK
301 %token <String> DLLIMPORT DLLEXPORT EXTERN_WEAK APPENDING
302 %token <String> NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
303 %token <String> ALIGN UNINITIALIZED
304 %token <String> DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
305 %token <String> CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
306 %token <String> X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
307 %token <String> DATALAYOUT
308 %token <String> RET BR SWITCH INVOKE EXCEPT UNWIND UNREACHABLE
309 %token <String> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM AND OR XOR
310 %token <String> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
311 %token <String> ICMP FCMP EQ NE SLT SGT SLE SGE OEQ ONE OLT OGT OLE OGE
312 %token <String> ORD UNO UEQ UNE ULT UGT ULE UGE
313 %token <String> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
314 %token <String> PHI_TOK SELECT SHL SHR ASHR LSHR VAARG
315 %token <String> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
316 %token <String> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI UITOFP SITOFP
317 %token <String> PTRTOINT INTTOPTR BITCAST
319 %type <String> OptAssign OptLinkage OptCallingConv OptAlign OptCAlign
320 %type <String> SectionString OptSection GlobalVarAttributes GlobalVarAttribute
321 %type <String> ArgTypeListI ConstExpr DefinitionList
322 %type <String> ConstPool TargetDefinition LibrariesDefinition LibList OptName
323 %type <String> ArgVal ArgListH ArgList FunctionHeaderH BEGIN FunctionHeader END
324 %type <String> Function FunctionProto BasicBlock TypeListI
325 %type <String> InstructionList BBTerminatorInst JumpTable Inst PHIList
326 %type <String> OptTailCall InstVal OptVolatile Unwind
327 %type <String> MemoryInst SymbolicValueRef OptSideEffect GlobalType
328 %type <String> FnDeclareLinkage BasicBlockList BigOrLittle AsmBlock
329 %type <String> Name ConstValueRef ConstVector External
330 %type <String> ShiftOps SetCondOps LogicalOps ArithmeticOps CastOps
331 %type <String> IPredicates FPredicates
333 %type <ValList> ValueRefList ValueRefListE IndexList
335 %type <Type> IntType SIntType UIntType FPType TypesV Types
336 %type <Type> PrimType UpRTypesV UpRTypes
338 %type <String> IntVal EInt64Val
339 %type <Const> ConstVal
341 %type <Value> ValueRef ResolvedVal
347 // Handle constant integer size restriction and conversion...
348 IntVal : SINTVAL | UINTVAL ;
349 EInt64Val : ESINT64VAL | EUINT64VAL;
351 // Operations that are notably excluded from this list include:
352 // RET, BR, & SWITCH because they end basic blocks and are treated specially.
353 ArithmeticOps: ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV
354 | REM | UREM | SREM | FREM;
355 LogicalOps : AND | OR | XOR;
356 SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
357 IPredicates : EQ | NE | SLT | SGT | SLE | SGE | ULT | UGT | ULE | UGE;
358 FPredicates : OEQ | ONE | OLT | OGT | OLE | OGE | ORD | UNO | UEQ | UNE
359 | ULT | UGT | ULE | UGE | TRUETOK | FALSETOK;
360 ShiftOps : SHL | SHR | ASHR | LSHR;
361 CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI |
362 UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
365 // These are some types that allow classification if we only want a particular
366 // thing... for example, only a signed, unsigned, or integral type.
367 SIntType : LONG | INT | SHORT | SBYTE;
368 UIntType : ULONG | UINT | USHORT | UBYTE;
369 IntType : SIntType | UIntType;
370 FPType : FLOAT | DOUBLE;
372 // OptAssign - Value producing statements have an optional assignment component
373 OptAssign : Name '=' {
377 $$ = new std::string("");
381 : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT
383 | /*empty*/ { $$ = new std::string(""); } ;
386 : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK
388 | CC_TOK EUINT64VAL {
393 | /*empty*/ { $$ = new std::string(""); } ;
395 // OptAlign/OptCAlign - An optional alignment, and an optional alignment with
396 // a comma before it.
398 : /*empty*/ { $$ = new std::string(); }
399 | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
402 : /*empty*/ { $$ = new std::string(); }
403 | ',' ALIGN EUINT64VAL {
411 : SECTION STRINGCONSTANT {
417 OptSection : /*empty*/ { $$ = new std::string(); }
421 : /* empty */ { $$ = new std::string(); }
422 | ',' GlobalVarAttribute GlobalVarAttributes {
438 //===----------------------------------------------------------------------===//
439 // Types includes all predefined types... except void, because it can only be
440 // used in specific contexts (function returning void for example). To have
441 // access to it, a user must explicitly use TypesV.
444 // TypesV includes all of 'Types', but it also includes the void type.
445 TypesV : Types | VOID ;
446 UpRTypesV : UpRTypes | VOID ;
449 // Derived types are added later...
451 PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
452 PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
460 $$.oldTy = UnresolvedTy;
465 | '\\' EUINT64VAL { // Type UpReference
468 $$.oldTy = NumericTy;
470 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
471 *$1.newTy += "( " + *$3 + " )";
474 $$.oldTy = FunctionTy;
476 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
478 *$2 += " x " + *$4.newTy + " ]";
482 $$.elemTy = $4.oldTy;
484 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
486 *$2 += " x " + *$4.newTy + " >";
490 $$.elemTy = $4.oldTy;
492 | '{' TypeListI '}' { // Structure type?
498 | '{' '}' { // Empty structure type?
499 $$.newTy = new std::string("{}");
502 | UpRTypes '*' { // Pointer type?
504 $$.elemTy = $1.oldTy;
505 $1.oldTy = PointerTy;
509 // TypeList - Used for struct declarations and as a basis for function type
510 // declaration type lists
516 | TypeListI ',' UpRTypes {
517 *$1 += ", " + *$3.newTy;
522 // ArgTypeList - List of types for a function type declaration...
525 | TypeListI ',' DOTDOTDOT {
534 $$ = new std::string();
537 // ConstVal - The various declarations that go into the constant pool. This
538 // production is used ONLY to represent constants that show up AFTER a 'const',
539 // 'constant' or 'global' token at global scope. Constants that can be inlined
540 // into other expressions (such as integers and constexprs) are handled by the
541 // ResolvedVal, ValueRef and ConstValueRef productions.
543 ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
545 $$.cnst = new std::string(*$1.newTy);
546 *$$.cnst += " [ " + *$3 + " ]";
551 $$.cnst = new std::string(*$1.newTy);
554 | Types 'c' STRINGCONSTANT {
556 $$.cnst = new std::string(*$1.newTy);
557 *$$.cnst += " c" + *$3;
560 | Types '<' ConstVector '>' { // Nonempty unsized arr
562 $$.cnst = new std::string(*$1.newTy);
563 *$$.cnst += " < " + *$3 + " >";
566 | Types '{' ConstVector '}' {
568 $$.cnst = new std::string(*$1.newTy);
569 *$$.cnst += " { " + *$3 + " }";
574 $$.cnst = new std::string(*$1.newTy);
579 $$.cnst = new std::string(*$1.newTy);
580 *$$.cnst += " " + *$2;
585 $$.cnst = new std::string(*$1.newTy);
586 *$$.cnst += " " + *$2;
589 | Types SymbolicValueRef {
591 $$.cnst = new std::string(*$1.newTy);
592 *$$.cnst += " " + *$2;
597 $$.cnst = new std::string(*$1.newTy);
598 *$$.cnst += " " + *$2;
601 | Types ZEROINITIALIZER {
603 $$.cnst = new std::string(*$1.newTy);
604 *$$.cnst += " " + *$2;
607 | SIntType EInt64Val { // integral constants
609 $$.cnst = new std::string(*$1.newTy);
610 *$$.cnst += " " + *$2;
613 | UIntType EUINT64VAL { // integral constants
615 $$.cnst = new std::string(*$1.newTy);
616 *$$.cnst += " " + *$2;
619 | BOOL TRUETOK { // Boolean constants
621 $$.cnst = new std::string(*$1.newTy);
622 *$$.cnst += " " + *$2;
625 | BOOL FALSETOK { // Boolean constants
627 $$.cnst = new std::string(*$1.newTy);
628 *$$.cnst += " " + *$2;
631 | FPType FPVAL { // Float & Double constants
633 $$.cnst = new std::string(*$1.newTy);
634 *$$.cnst += " " + *$2;
639 ConstExpr: CastOps '(' ConstVal TO Types ')' {
640 std::string source = *$3.cnst;
644 // Call getCastUpgrade to upgrade the old cast
645 $$ = new std::string(getCastUpgrade(source, $3.type, $5, true));
647 // Nothing to upgrade, just create the cast constant expr
648 $$ = new std::string(*$1);
649 *$$ += "( " + source + " to " + *$5.newTy + ")";
651 delete $1; $3.destroy(); delete $4; $5.destroy();
653 | GETELEMENTPTR '(' ConstVal IndexList ')' {
654 *$1 += "(" + *$3.cnst;
655 for (unsigned i = 0; i < $4->size(); ++i) {
656 ValueInfo& VI = (*$4)[i];
657 *$1 += ", " + *VI.val;
665 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
666 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
667 $3.destroy(); $5.destroy(); $7.destroy();
670 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
671 const char* op = getDivRemOpcode(*$1, $3.type);
672 $$ = new std::string(op);
673 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
674 delete $1; $3.destroy(); $5.destroy();
676 | LogicalOps '(' ConstVal ',' ConstVal ')' {
677 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
678 $3.destroy(); $5.destroy();
681 | SetCondOps '(' ConstVal ',' ConstVal ')' {
682 #if UPGRADE_SETCOND_OPS
683 *$1 = getCompareOp(*$1, $3.type);
685 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
686 $3.destroy(); $5.destroy();
689 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
690 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
691 delete $2; $4.destroy(); $6.destroy();
694 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
695 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
696 delete $2; $4.destroy(); $6.destroy();
699 | ShiftOps '(' ConstVal ',' ConstVal ')' {
700 const char* shiftop = $1->c_str();
702 shiftop = ($3.type.isUnsigned()) ? "lshr" : "ashr";
703 $$ = new std::string(shiftop);
704 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
705 delete $1; $3.destroy(); $5.destroy();
707 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
708 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
709 $3.destroy(); $5.destroy();
712 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
713 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
714 $3.destroy(); $5.destroy(); $7.destroy();
717 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
718 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
719 $3.destroy(); $5.destroy(); $7.destroy();
724 // ConstVector - A list of comma separated constants.
727 : ConstVector ',' ConstVal {
728 *$1 += ", " + *$3.cnst;
732 | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
736 // GlobalType - Match either GLOBAL or CONSTANT for global declarations...
737 GlobalType : GLOBAL | CONSTANT ;
740 //===----------------------------------------------------------------------===//
741 // Rules to match Modules
742 //===----------------------------------------------------------------------===//
744 // Module rule: Capture the result of parsing the whole file into a result
747 Module : DefinitionList {
750 // DefinitionList - Top level definitions
752 DefinitionList : DefinitionList Function {
755 | DefinitionList FunctionProto {
760 | DefinitionList MODULE ASM_TOK AsmBlock {
761 *O << "module asm " << " " << *$4 << "\n";
764 | DefinitionList IMPLEMENTATION {
765 *O << "implementation\n";
768 | ConstPool { $$ = 0; }
770 External : EXTERNAL | UNINITIALIZED { $$ = $1; *$$ = "external"; }
772 // ConstPool - Constants with optional names assigned to them.
773 ConstPool : ConstPool OptAssign TYPE TypesV {
774 EnumeratedTypes.push_back($4);
776 NamedTypes[*$2].newTy = new std::string(*$4.newTy);
777 NamedTypes[*$2].oldTy = $4.oldTy;
778 NamedTypes[*$2].elemTy = $4.elemTy;
781 *O << "type " << *$4.newTy << "\n";
782 delete $2; delete $3; $4.destroy();
785 | ConstPool FunctionProto { // Function prototypes can be in const pool
790 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
791 *O << *$2 << " " << *$3 << " " << *$4 << "\n";
792 delete $2; delete $3; delete $4;
795 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
798 Globals[*$2] = $5.type.clone();
800 *O << *$3 << " " << *$4 << " " << *$5.cnst << " " << *$6 << "\n";
801 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
804 | ConstPool OptAssign External GlobalType Types GlobalVarAttributes {
807 Globals[*$2] = $5.clone();
809 *O << *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
810 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
813 | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
816 Globals[*$2] = $5.clone();
818 *O << *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
819 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
822 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
825 Globals[*$2] = $5.clone();
827 *O << *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
828 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
831 | ConstPool TARGET TargetDefinition {
832 *O << *$2 << " " << *$3 << "\n";
833 delete $2; delete $3;
836 | ConstPool DEPLIBS '=' LibrariesDefinition {
837 *O << *$2 << " = " << *$4 << "\n";
838 delete $2; delete $4;
841 | /* empty: end of list */ {
846 AsmBlock : STRINGCONSTANT ;
848 BigOrLittle : BIG | LITTLE
851 : ENDIAN '=' BigOrLittle {
856 | POINTERSIZE '=' EUINT64VAL {
863 | TRIPLE '=' STRINGCONSTANT {
868 | DATALAYOUT '=' STRINGCONSTANT {
882 : LibList ',' STRINGCONSTANT {
888 | /* empty: end of list */ {
889 $$ = new std::string();
892 //===----------------------------------------------------------------------===//
893 // Rules to match Function Headers
894 //===----------------------------------------------------------------------===//
896 Name : VAR_ID | STRINGCONSTANT;
897 OptName : Name | /*empty*/ { $$ = new std::string(); };
899 ArgVal : Types OptName {
906 ArgListH : ArgListH ',' ArgVal {
917 | ArgListH ',' DOTDOTDOT {
925 | /* empty */ { $$ = new std::string(); };
927 FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
928 OptSection OptAlign {
932 *$1 += *$2.newTy + " " + *$3 + "(" + *$5 + ")";
947 BEGIN : BEGINTOK { $$ = new std::string("{"); delete $1; }
948 | '{' { $$ = new std::string ("{"); }
950 FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
954 *O << *$2 << " " << *$3 << "\n";
955 delete $1; delete $2; delete $3;
959 END : ENDTOK { $$ = new std::string("}"); delete $1; }
960 | '}' { $$ = new std::string("}"); };
962 Function : FunctionHeader BasicBlockList END {
965 *O << '\n' << *$3 << "\n";
970 : /*default*/ { $$ = new std::string(); }
976 : DECLARE FnDeclareLinkage FunctionHeaderH {
985 //===----------------------------------------------------------------------===//
986 // Rules to match Basic Blocks
987 //===----------------------------------------------------------------------===//
989 OptSideEffect : /* empty */ { $$ = new std::string(); }
993 : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK | NULL_TOK | UNDEF
995 | '<' ConstVector '>' {
1001 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
1005 *$1 += " " + *$3 + ", " + *$5;
1006 delete $2; delete $3; delete $5;
1010 SymbolicValueRef : IntVal | Name ;
1012 // ValueRef - A reference to a definition... either constant or symbolic
1014 : SymbolicValueRef {
1016 $$.constant = false;
1018 $$.type.oldTy = UnresolvedTy;
1024 $$.type.oldTy = UnresolvedTy;
1028 // ResolvedVal - a <type> <value> pair. This is used only in cases where the
1029 // type immediately preceeds the value reference, and allows complex constant
1030 // pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1031 ResolvedVal : Types ValueRef {
1034 $$.val->insert(0, *$1.newTy + " ");
1037 BasicBlockList : BasicBlockList BasicBlock {
1040 | BasicBlock { // Do not allow functions with 0 basic blocks
1045 // Basic blocks are terminated by branching instructions:
1046 // br, br/cc, switch, ret
1048 BasicBlock : InstructionList BBTerminatorInst {
1052 InstructionList : InstructionList Inst {
1053 *O << " " << *$2 << "\n";
1066 Unwind : UNWIND | EXCEPT { $$ = $1; *$$ = "unwind"; }
1068 BBTerminatorInst : RET ResolvedVal { // Return with a result...
1069 *O << " " << *$1 << " " << *$2.val << "\n";
1070 delete $1; $2.destroy();
1073 | RET VOID { // Return with no result...
1074 *O << " " << *$1 << " " << *$2.newTy << "\n";
1075 delete $1; $2.destroy();
1078 | BR LABEL ValueRef { // Unconditional Branch...
1079 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << "\n";
1080 delete $1; $2.destroy(); $3.destroy();
1082 } // Conditional Branch...
1083 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
1084 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1085 << *$5.newTy << " " << *$6.val << ", " << *$8.newTy << " "
1087 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1088 $8.destroy(); $9.destroy();
1091 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1092 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1093 << *$5.newTy << " " << *$6.val << " [" << *$8 << " ]\n";
1094 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1098 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1099 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1100 << *$5.newTy << " " << *$6.val << "[]\n";
1101 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1104 | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
1105 TO LABEL ValueRef Unwind LABEL ValueRef {
1109 *O << *$2 << " " << *$3 << " " << *$4.newTy << " " << *$5.val << " (";
1110 for (unsigned i = 0; i < $7->size(); ++i) {
1111 ValueInfo& VI = (*$7)[i];
1113 if (i+1 < $7->size())
1117 *O << ") " << *$9 << " " << *$10.newTy << " " << *$11.val << " "
1118 << *$12 << " " << *$13.newTy << " " << *$14.val << "\n";
1119 delete $1; delete $2; delete $3; $4.destroy(); $5.destroy(); delete $7;
1120 delete $9; $10.destroy(); $11.destroy(); delete $12; $13.destroy();
1125 *O << " " << *$1 << "\n";
1130 *O << " " << *$1 << "\n";
1135 JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1136 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5.newTy + " " + *$6.val;
1137 $2.destroy(); delete $3; $5.destroy(); $6.destroy();
1140 | IntType ConstValueRef ',' LABEL ValueRef {
1141 $2->insert(0, *$1.newTy + " " );
1142 *$2 += ", " + *$4.newTy + " " + *$5.val;
1143 $1.destroy(); $4.destroy(); $5.destroy();
1148 : OptAssign InstVal {
1157 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1158 $3.val->insert(0, *$1.newTy + "[");
1159 *$3.val += "," + *$5.val + "]";
1160 $1.destroy(); $5.destroy();
1161 $$ = new std::string(*$3.val);
1164 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1165 *$1 += ", [" + *$4.val + "," + *$6.val + "]";
1166 $4.destroy(); $6.destroy();
1173 $$ = new ValueList();
1176 | ValueRefList ',' ResolvedVal {
1181 // ValueRefListE - Just like ValueRefList, except that it may also be empty!
1183 : ValueRefList { $$ = $1; }
1184 | /*empty*/ { $$ = new ValueList(); }
1196 InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1197 const char* op = getDivRemOpcode(*$1, $2);
1198 $$ = new std::string(op);
1199 *$$ += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1200 delete $1; $2.destroy(); $3.destroy(); $5.destroy();
1202 | LogicalOps Types ValueRef ',' ValueRef {
1203 *$1 += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1204 $2.destroy(); $3.destroy(); $5.destroy();
1207 | SetCondOps Types ValueRef ',' ValueRef {
1208 #if UPGRADE_SETCOND_OPS
1209 *$1 = getCompareOp(*$1, $2);
1211 *$1 += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1212 $2.destroy(); $3.destroy(); $5.destroy();
1215 | ICMP IPredicates Types ValueRef ',' ValueRef ')' {
1216 *$1 += " " + *$2 + " " + *$4.val + "," + *$6.val + ")";
1217 delete $2; $4.destroy(); $6.destroy();
1220 | FCMP FPredicates Types ValueRef ',' ValueRef ')' {
1221 *$1 += " " + *$2 + " " + *$4.val + "," + *$6.val + ")";
1222 delete $2; $4.destroy(); $6.destroy();
1226 *$1 += " " + *$2.val;
1230 | ShiftOps ResolvedVal ',' ResolvedVal {
1231 const char* shiftop = $1->c_str();
1233 shiftop = ($2.type.isUnsigned()) ? "lshr" : "ashr";
1234 $$ = new std::string(shiftop);
1235 *$$ += " " + *$2.val + ", " + *$4.val;
1236 delete $1; $2.destroy(); $4.destroy();
1238 | CastOps ResolvedVal TO Types {
1239 std::string source = *$2.val;
1240 TypeInfo SrcTy = $2.type;
1241 TypeInfo DstTy = $4;
1243 $$ = new std::string();
1244 if (*$1 == "cast") {
1245 *$$ += getCastUpgrade(source, SrcTy, DstTy, false);
1247 *$$ += *$1 + " " + source + " to " + *DstTy.newTy;
1249 delete $1; $2.destroy();
1250 delete $3; $4.destroy();
1252 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1253 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1254 $2.destroy(); $4.destroy(); $6.destroy();
1257 | VAARG ResolvedVal ',' Types {
1258 *$1 += " " + *$2.val + ", " + *$4.newTy;
1259 $2.destroy(); $4.destroy();
1262 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
1263 *$1 += " " + *$2.val + ", " + *$4.val;
1264 $2.destroy(); $4.destroy();
1267 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1268 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1269 $2.destroy(); $4.destroy(); $6.destroy();
1272 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1273 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1274 $2.destroy(); $4.destroy(); $6.destroy();
1282 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
1287 *$1 += *$3.newTy + " " + *$4.val + "(";
1288 for (unsigned i = 0; i < $6->size(); ++i) {
1289 ValueInfo& VI = (*$6)[i];
1291 if (i+1 < $6->size())
1296 delete $2; $3.destroy(); $4.destroy(); delete $6;
1302 // IndexList - List of indices for GEP based instructions...
1304 : ',' ValueRefList { $$ = $2; }
1305 | /* empty */ { $$ = new ValueList(); }
1310 | /* empty */ { $$ = new std::string(); }
1313 MemoryInst : MALLOC Types OptCAlign {
1314 *$1 += " " + *$2.newTy;
1317 $2.destroy(); delete $3;
1320 | MALLOC Types ',' UINT ValueRef OptCAlign {
1321 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5.val;
1324 $2.destroy(); $4.destroy(); $5.destroy(); delete $6;
1327 | ALLOCA Types OptCAlign {
1328 *$1 += " " + *$2.newTy;
1331 $2.destroy(); delete $3;
1334 | ALLOCA Types ',' UINT ValueRef OptCAlign {
1335 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5.val;
1338 $2.destroy(); $4.destroy(); $5.destroy(); delete $6;
1341 | FREE ResolvedVal {
1342 *$1 += " " + *$2.val;
1346 | OptVolatile LOAD Types ValueRef {
1349 *$1 += *$2 + " " + *$3.newTy + " " + *$4.val;
1350 delete $2; $3.destroy(); $4.destroy();
1353 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1356 *$1 += *$2 + " " + *$3.val + ", " + *$5.newTy + " " + *$6.val;
1357 delete $2; $3.destroy(); $5.destroy(); $6.destroy();
1360 | GETELEMENTPTR Types ValueRef IndexList {
1361 // Upgrade the indices
1362 for (unsigned i = 0; i < $4->size(); ++i) {
1363 ValueInfo& VI = (*$4)[i];
1364 if (VI.type.isUnsigned() && !VI.isConstant() &&
1365 VI.type.getBitWidth() < 64) {
1366 std::string* old = VI.val;
1367 *O << " %gep_upgrade" << unique << " = zext " << *old
1369 VI.val = new std::string("ulong %gep_upgrade" + llvm::utostr(unique++));
1370 VI.type.oldTy = ULongTy;
1374 *$1 += " " + *$2.newTy + " " + *$3.val;
1375 for (unsigned i = 0; i < $4->size(); ++i) {
1376 ValueInfo& VI = (*$4)[i];
1377 *$1 += ", " + *VI.val;
1380 $2.destroy(); $3.destroy(); delete $4;
1386 int yyerror(const char *ErrorMsg) {
1388 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1389 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
1390 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
1391 if (yychar == YYEMPTY || yychar == 0)
1392 errMsg += "end-of-file.";
1394 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
1395 std::cerr << errMsg << '\n';