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
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 // This bool controls whether attributes are ever added to function declarations
38 // definitions and calls.
39 static bool AddAttributes = false;
41 // This bool is used to communicate between the InstVal and Inst rules about
42 // whether or not a cast should be deleted. When the flag is set, InstVal has
43 // determined that the cast is a candidate. However, it can only be deleted if
44 // the value being casted is the same value name as the instruction. The Inst
45 // rule makes that comparison if the flag is set and comments out the
46 // instruction if they match.
47 static bool deleteUselessCastFlag = false;
48 static std::string* deleteUselessCastName = 0;
50 typedef std::vector<TypeInfo> TypeVector;
51 static TypeVector EnumeratedTypes;
52 typedef std::map<std::string,TypeInfo> TypeMap;
53 static TypeMap NamedTypes;
54 static TypeMap Globals;
56 void destroy(ValueList* VL) {
57 while (!VL->empty()) {
58 ValueInfo& VI = VL->back();
65 void UpgradeAssembly(const std::string &infile, std::istream& in,
66 std::ostream &out, bool debug, bool addAttrs)
72 AddAttributes = addAttrs;
76 std::cerr << "Parse failed.\n";
81 static void ResolveType(TypeInfo& Ty) {
82 if (Ty.oldTy == UnresolvedTy) {
83 TypeMap::iterator I = NamedTypes.find(*Ty.newTy);
84 if (I != NamedTypes.end()) {
85 Ty.oldTy = I->second.oldTy;
86 Ty.elemTy = I->second.elemTy;
88 std::string msg("Can't resolve type: ");
92 } else if (Ty.oldTy == NumericTy) {
93 unsigned ref = atoi(&((Ty.newTy->c_str())[1])); // Skip the '\\'
94 if (ref < EnumeratedTypes.size()) {
95 Ty.oldTy = EnumeratedTypes[ref].oldTy;
96 Ty.elemTy = EnumeratedTypes[ref].elemTy;
98 std::string msg("Can't resolve type: ");
100 yyerror(msg.c_str());
103 // otherwise its already resolved.
106 static const char* getCastOpcode(
107 std::string& Source, const TypeInfo& SrcTy, const TypeInfo& DstTy)
109 unsigned SrcBits = SrcTy.getBitWidth();
110 unsigned DstBits = DstTy.getBitWidth();
111 const char* opcode = "bitcast";
112 // Run through the possibilities ...
113 if (DstTy.isIntegral()) { // Casting to integral
114 if (SrcTy.isIntegral()) { // Casting from integral
115 if (DstBits < SrcBits)
117 else if (DstBits > SrcBits) { // its an extension
118 if (SrcTy.isSigned())
119 opcode ="sext"; // signed -> SEXT
121 opcode = "zext"; // unsigned -> ZEXT
123 opcode = "bitcast"; // Same size, No-op cast
125 } else if (SrcTy.isFloatingPoint()) { // Casting from floating pt
126 if (DstTy.isSigned())
127 opcode = "fptosi"; // FP -> sint
129 opcode = "fptoui"; // FP -> uint
130 } else if (SrcTy.isPacked()) {
131 assert(DstBits == SrcTy.getBitWidth() &&
132 "Casting packed to integer of different width");
133 opcode = "bitcast"; // same size, no-op cast
135 assert(SrcTy.isPointer() &&
136 "Casting from a value that is not first-class type");
137 opcode = "ptrtoint"; // ptr -> int
139 } else if (DstTy.isFloatingPoint()) { // Casting to floating pt
140 if (SrcTy.isIntegral()) { // Casting from integral
141 if (SrcTy.isSigned())
142 opcode = "sitofp"; // sint -> FP
144 opcode = "uitofp"; // uint -> FP
145 } else if (SrcTy.isFloatingPoint()) { // Casting from floating pt
146 if (DstBits < SrcBits) {
147 opcode = "fptrunc"; // FP -> smaller FP
148 } else if (DstBits > SrcBits) {
149 opcode = "fpext"; // FP -> larger FP
151 opcode ="bitcast"; // same size, no-op cast
153 } else if (SrcTy.isPacked()) {
154 assert(DstBits == SrcTy.getBitWidth() &&
155 "Casting packed to floating point of different width");
156 opcode = "bitcast"; // same size, no-op cast
158 assert(0 && "Casting pointer or non-first class to float");
160 } else if (DstTy.isPacked()) {
161 if (SrcTy.isPacked()) {
162 assert(DstTy.getBitWidth() == SrcTy.getBitWidth() &&
163 "Casting packed to packed of different widths");
164 opcode = "bitcast"; // packed -> packed
165 } else if (DstTy.getBitWidth() == SrcBits) {
166 opcode = "bitcast"; // float/int -> packed
168 assert(!"Illegal cast to packed (wrong type or size)");
170 } else if (DstTy.isPointer()) {
171 if (SrcTy.isPointer()) {
172 opcode = "bitcast"; // ptr -> ptr
173 } else if (SrcTy.isIntegral()) {
174 opcode = "inttoptr"; // int -> ptr
176 assert(!"Casting invalid type to pointer");
179 assert(!"Casting to type that is not first-class");
184 static std::string getCastUpgrade(
185 const std::string& Src, TypeInfo& SrcTy, TypeInfo& DstTy, bool isConst)
188 std::string Source = Src;
189 if (SrcTy.isFloatingPoint() && DstTy.isPointer()) {
190 // fp -> ptr cast is no longer supported but we must upgrade this
191 // by doing a double cast: fp -> int -> ptr
193 Source = "i64 fptoui(" + Source + " to i64)";
195 *O << " %cast_upgrade" << unique << " = fptoui " << Source
197 Source = "i64 %cast_upgrade" + llvm::utostr(unique);
199 // Update the SrcTy for the getCastOpcode call below
201 SrcTy.newTy = new std::string("i64");
202 SrcTy.oldTy = ULongTy;
203 } else if (DstTy.oldTy == BoolTy && SrcTy.oldTy != BoolTy) {
204 // cast ptr %x to bool was previously defined as setne ptr %x, null
205 // The ptrtoint semantic is to truncate, not compare so we must retain
206 // the original intent by replace the cast with a setne
207 const char* comparator = SrcTy.isPointer() ? ", null" :
208 (SrcTy.isFloatingPoint() ? ", 0.0" : ", 0");
209 const char* compareOp = SrcTy.isFloatingPoint() ? "fcmp one " : "icmp ne ";
211 Result = "(" + Source + comparator + ")";
212 Result = compareOp + Result;
214 Result = compareOp + Source + comparator;
215 return Result; // skip cast processing below
219 std::string Opcode(getCastOpcode(Source, SrcTy, DstTy));
221 Result += Opcode + "( " + Source + " to " + *DstTy.newTy + ")";
223 Result += Opcode + " " + Source + " to " + *DstTy.newTy;
227 const char* getDivRemOpcode(const std::string& opcode, const TypeInfo& TI) {
228 const char* op = opcode.c_str();
232 Ty.oldTy = Ty.getElementType();
234 if (Ty.isFloatingPoint())
236 else if (Ty.isUnsigned())
238 else if (Ty.isSigned())
241 yyerror("Invalid type for div instruction");
242 else if (opcode == "rem")
243 if (Ty.isFloatingPoint())
245 else if (Ty.isUnsigned())
247 else if (Ty.isSigned())
250 yyerror("Invalid type for rem instruction");
255 getCompareOp(const std::string& setcc, const TypeInfo& TI) {
256 assert(setcc.length() == 5);
259 assert(cc1 == 'e' || cc1 == 'n' || cc1 == 'l' || cc1 == 'g');
260 assert(cc2 == 'q' || cc2 == 'e' || cc2 == 'e' || cc2 == 't');
261 std::string result("xcmp xxx");
264 if (TI.isFloatingPoint()) {
268 result[5] = 'u'; // NE maps to unordered
270 result[5] = 'o'; // everything else maps to ordered
271 } else if (TI.isIntegral() || TI.isPointer()) {
273 if ((cc1 == 'e' && cc2 == 'q') || (cc1 == 'n' && cc2 == 'e'))
275 else if (TI.isSigned())
277 else if (TI.isUnsigned() || TI.isPointer() || TI.isBool())
280 yyerror("Invalid integral type for setcc");
287 // %file-prefix="UpgradeParser"
297 %token <Type> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
298 %token <Type> FLOAT DOUBLE LABEL
299 %token <String> OPAQUE ESINT64VAL EUINT64VAL SINTVAL UINTVAL FPVAL
300 %token <String> NULL_TOK UNDEF ZEROINITIALIZER TRUETOK FALSETOK
301 %token <String> TYPE VAR_ID LABELSTR STRINGCONSTANT
302 %token <String> IMPLEMENTATION BEGINTOK ENDTOK
303 %token <String> DECLARE GLOBAL CONSTANT SECTION VOLATILE
304 %token <String> TO DOTDOTDOT CONST INTERNAL LINKONCE WEAK
305 %token <String> DLLIMPORT DLLEXPORT EXTERN_WEAK APPENDING
306 %token <String> NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
307 %token <String> ALIGN UNINITIALIZED
308 %token <String> DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
309 %token <String> CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
310 %token <String> X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
311 %token <String> DATALAYOUT
312 %token <String> RET BR SWITCH INVOKE EXCEPT UNWIND UNREACHABLE
313 %token <String> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM AND OR XOR
314 %token <String> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
315 %token <String> ICMP FCMP EQ NE SLT SGT SLE SGE OEQ ONE OLT OGT OLE OGE
316 %token <String> ORD UNO UEQ UNE ULT UGT ULE UGE
317 %token <String> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
318 %token <String> PHI_TOK SELECT SHL SHR ASHR LSHR VAARG
319 %token <String> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
320 %token <String> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI UITOFP SITOFP
321 %token <String> PTRTOINT INTTOPTR BITCAST
323 %type <String> OptAssign OptLinkage OptCallingConv OptAlign OptCAlign
324 %type <String> SectionString OptSection GlobalVarAttributes GlobalVarAttribute
325 %type <String> ArgTypeListI ConstExpr DefinitionList
326 %type <String> ConstPool TargetDefinition LibrariesDefinition LibList OptName
327 %type <String> ArgVal ArgListH ArgList FunctionHeaderH BEGIN FunctionHeader END
328 %type <String> Function FunctionProto BasicBlock TypeListI
329 %type <String> InstructionList BBTerminatorInst JumpTable Inst PHIList
330 %type <String> OptTailCall InstVal OptVolatile Unwind
331 %type <String> MemoryInst SymbolicValueRef OptSideEffect GlobalType
332 %type <String> FnDeclareLinkage BasicBlockList BigOrLittle AsmBlock
333 %type <String> Name ConstValueRef ConstVector External
334 %type <String> ShiftOps SetCondOps LogicalOps ArithmeticOps CastOps
335 %type <String> IPredicates FPredicates
337 %type <ValList> ValueRefList ValueRefListE IndexList
339 %type <Type> IntType SIntType UIntType FPType TypesV Types
340 %type <Type> PrimType UpRTypesV UpRTypes
342 %type <String> IntVal EInt64Val
343 %type <Const> ConstVal
345 %type <Value> ValueRef ResolvedVal
351 // Handle constant integer size restriction and conversion...
352 IntVal : SINTVAL | UINTVAL ;
353 EInt64Val : ESINT64VAL | EUINT64VAL;
355 // Operations that are notably excluded from this list include:
356 // RET, BR, & SWITCH because they end basic blocks and are treated specially.
357 ArithmeticOps: ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV
358 | REM | UREM | SREM | FREM;
359 LogicalOps : AND | OR | XOR;
360 SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
361 IPredicates : EQ | NE | SLT | SGT | SLE | SGE | ULT | UGT | ULE | UGE;
362 FPredicates : OEQ | ONE | OLT | OGT | OLE | OGE | ORD | UNO | UEQ | UNE
363 | ULT | UGT | ULE | UGE | TRUETOK | FALSETOK;
364 ShiftOps : SHL | SHR | ASHR | LSHR;
365 CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI |
366 UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
369 // These are some types that allow classification if we only want a particular
370 // thing... for example, only a signed, unsigned, or integral type.
371 SIntType : LONG | INT | SHORT | SBYTE;
372 UIntType : ULONG | UINT | USHORT | UBYTE;
373 IntType : SIntType | UIntType;
374 FPType : FLOAT | DOUBLE;
376 // OptAssign - Value producing statements have an optional assignment component
377 OptAssign : Name '=' {
381 $$ = new std::string("");
385 : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT
387 | /*empty*/ { $$ = new std::string(""); } ;
390 : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK
392 | CC_TOK EUINT64VAL {
397 | /*empty*/ { $$ = new std::string(""); } ;
399 // OptAlign/OptCAlign - An optional alignment, and an optional alignment with
400 // a comma before it.
402 : /*empty*/ { $$ = new std::string(); }
403 | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
406 : /*empty*/ { $$ = new std::string(); }
407 | ',' ALIGN EUINT64VAL {
415 : SECTION STRINGCONSTANT {
421 OptSection : /*empty*/ { $$ = new std::string(); }
425 : /* empty */ { $$ = new std::string(); }
426 | ',' GlobalVarAttribute GlobalVarAttributes {
442 //===----------------------------------------------------------------------===//
443 // Types includes all predefined types... except void, because it can only be
444 // used in specific contexts (function returning void for example). To have
445 // access to it, a user must explicitly use TypesV.
448 // TypesV includes all of 'Types', but it also includes the void type.
449 TypesV : Types | VOID ;
450 UpRTypesV : UpRTypes | VOID ;
453 // Derived types are added later...
455 PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
456 PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
464 $$.oldTy = UnresolvedTy;
469 | '\\' EUINT64VAL { // Type UpReference
472 $$.oldTy = NumericTy;
474 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
475 *$1.newTy += "( " + *$3 + " )";
478 $$.oldTy = FunctionTy;
480 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
482 *$2 += " x " + *$4.newTy + " ]";
486 $$.elemTy = $4.oldTy;
488 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
490 *$2 += " x " + *$4.newTy + " >";
494 $$.elemTy = $4.oldTy;
496 | '{' TypeListI '}' { // Structure type?
502 | '{' '}' { // Empty structure type?
503 $$.newTy = new std::string("{}");
506 | '<' '{' TypeListI '}' '>' { // Packed Structure type?
507 $3->insert(0, "<{ ");
512 | '<' '{' '}' '>' { // Empty packed structure type?
513 $$.newTy = new std::string("<{}>");
516 | UpRTypes '*' { // Pointer type?
518 $$.elemTy = $1.oldTy;
519 $1.oldTy = PointerTy;
523 // TypeList - Used for struct declarations and as a basis for function type
524 // declaration type lists
530 | TypeListI ',' UpRTypes {
531 *$1 += ", " + *$3.newTy;
536 // ArgTypeList - List of types for a function type declaration...
539 | TypeListI ',' DOTDOTDOT {
548 $$ = new std::string();
551 // ConstVal - The various declarations that go into the constant pool. This
552 // production is used ONLY to represent constants that show up AFTER a 'const',
553 // 'constant' or 'global' token at global scope. Constants that can be inlined
554 // into other expressions (such as integers and constexprs) are handled by the
555 // ResolvedVal, ValueRef and ConstValueRef productions.
557 ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
559 $$.cnst = new std::string(*$1.newTy);
560 *$$.cnst += " [ " + *$3 + " ]";
565 $$.cnst = new std::string(*$1.newTy);
568 | Types 'c' STRINGCONSTANT {
570 $$.cnst = new std::string(*$1.newTy);
571 *$$.cnst += " c" + *$3;
574 | Types '<' ConstVector '>' { // Nonempty unsized arr
576 $$.cnst = new std::string(*$1.newTy);
577 *$$.cnst += " < " + *$3 + " >";
580 | Types '{' ConstVector '}' {
582 $$.cnst = new std::string(*$1.newTy);
583 *$$.cnst += " { " + *$3 + " }";
588 $$.cnst = new std::string(*$1.newTy);
593 $$.cnst = new std::string(*$1.newTy);
594 *$$.cnst += " " + *$2;
599 $$.cnst = new std::string(*$1.newTy);
600 *$$.cnst += " " + *$2;
603 | Types SymbolicValueRef {
605 $$.cnst = new std::string(*$1.newTy);
606 *$$.cnst += " " + *$2;
611 $$.cnst = new std::string(*$1.newTy);
612 *$$.cnst += " " + *$2;
615 | Types ZEROINITIALIZER {
617 $$.cnst = new std::string(*$1.newTy);
618 *$$.cnst += " " + *$2;
621 | SIntType EInt64Val { // integral constants
623 $$.cnst = new std::string(*$1.newTy);
624 *$$.cnst += " " + *$2;
627 | UIntType EUINT64VAL { // integral constants
629 $$.cnst = new std::string(*$1.newTy);
630 *$$.cnst += " " + *$2;
633 | BOOL TRUETOK { // Boolean constants
635 $$.cnst = new std::string(*$1.newTy);
636 *$$.cnst += " " + *$2;
639 | BOOL FALSETOK { // Boolean constants
641 $$.cnst = new std::string(*$1.newTy);
642 *$$.cnst += " " + *$2;
645 | FPType FPVAL { // Float & Double constants
647 $$.cnst = new std::string(*$1.newTy);
648 *$$.cnst += " " + *$2;
653 ConstExpr: CastOps '(' ConstVal TO Types ')' {
654 std::string source = *$3.cnst;
658 // Call getCastUpgrade to upgrade the old cast
659 $$ = new std::string(getCastUpgrade(source, $3.type, $5, true));
661 // Nothing to upgrade, just create the cast constant expr
662 $$ = new std::string(*$1);
663 *$$ += "( " + source + " to " + *$5.newTy + ")";
665 delete $1; $3.destroy(); delete $4; $5.destroy();
667 | GETELEMENTPTR '(' ConstVal IndexList ')' {
668 *$1 += "(" + *$3.cnst;
669 for (unsigned i = 0; i < $4->size(); ++i) {
670 ValueInfo& VI = (*$4)[i];
671 *$1 += ", " + *VI.val;
679 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
680 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
681 $3.destroy(); $5.destroy(); $7.destroy();
684 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
685 const char* op = getDivRemOpcode(*$1, $3.type);
686 $$ = new std::string(op);
687 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
688 delete $1; $3.destroy(); $5.destroy();
690 | LogicalOps '(' ConstVal ',' ConstVal ')' {
691 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
692 $3.destroy(); $5.destroy();
695 | SetCondOps '(' ConstVal ',' ConstVal ')' {
696 *$1 = getCompareOp(*$1, $3.type);
697 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
698 $3.destroy(); $5.destroy();
701 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
702 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
703 delete $2; $4.destroy(); $6.destroy();
706 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
707 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
708 delete $2; $4.destroy(); $6.destroy();
711 | ShiftOps '(' ConstVal ',' ConstVal ')' {
712 const char* shiftop = $1->c_str();
714 shiftop = ($3.type.isUnsigned()) ? "lshr" : "ashr";
715 $$ = new std::string(shiftop);
716 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
717 delete $1; $3.destroy(); $5.destroy();
719 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
720 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
721 $3.destroy(); $5.destroy();
724 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
725 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
726 $3.destroy(); $5.destroy(); $7.destroy();
729 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
730 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
731 $3.destroy(); $5.destroy(); $7.destroy();
736 // ConstVector - A list of comma separated constants.
739 : ConstVector ',' ConstVal {
740 *$1 += ", " + *$3.cnst;
744 | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
748 // GlobalType - Match either GLOBAL or CONSTANT for global declarations...
749 GlobalType : GLOBAL | CONSTANT ;
752 //===----------------------------------------------------------------------===//
753 // Rules to match Modules
754 //===----------------------------------------------------------------------===//
756 // Module rule: Capture the result of parsing the whole file into a result
759 Module : DefinitionList {
762 // DefinitionList - Top level definitions
764 DefinitionList : DefinitionList Function {
767 | DefinitionList FunctionProto {
772 | DefinitionList MODULE ASM_TOK AsmBlock {
773 *O << "module asm " << ' ' << *$4 << '\n';
776 | DefinitionList IMPLEMENTATION {
777 *O << "implementation\n";
780 | ConstPool { $$ = 0; }
782 External : EXTERNAL | UNINITIALIZED { $$ = $1; *$$ = "external"; }
784 // ConstPool - Constants with optional names assigned to them.
785 ConstPool : ConstPool OptAssign TYPE TypesV {
786 EnumeratedTypes.push_back($4);
788 NamedTypes[*$2].newTy = new std::string(*$4.newTy);
789 NamedTypes[*$2].oldTy = $4.oldTy;
790 NamedTypes[*$2].elemTy = $4.elemTy;
793 *O << "type " << *$4.newTy << '\n';
794 delete $2; delete $3; $4.destroy();
797 | ConstPool FunctionProto { // Function prototypes can be in const pool
802 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
803 *O << *$2 << ' ' << *$3 << ' ' << *$4 << '\n';
804 delete $2; delete $3; delete $4;
807 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
810 Globals[*$2] = $5.type.clone();
812 *O << *$3 << ' ' << *$4 << ' ' << *$5.cnst << ' ' << *$6 << '\n';
813 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
816 | ConstPool OptAssign External GlobalType Types GlobalVarAttributes {
819 Globals[*$2] = $5.clone();
821 *O << *$3 << ' ' << *$4 << ' ' << *$5.newTy << ' ' << *$6 << '\n';
822 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
825 | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
828 Globals[*$2] = $5.clone();
830 *O << *$3 << ' ' << *$4 << ' ' << *$5.newTy << ' ' << *$6 << '\n';
831 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
834 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
837 Globals[*$2] = $5.clone();
839 *O << *$3 << ' ' << *$4 << ' ' << *$5.newTy << ' ' << *$6 << '\n';
840 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
843 | ConstPool TARGET TargetDefinition {
844 *O << *$2 << ' ' << *$3 << '\n';
845 delete $2; delete $3;
848 | ConstPool DEPLIBS '=' LibrariesDefinition {
849 *O << *$2 << " = " << *$4 << '\n';
850 delete $2; delete $4;
853 | /* empty: end of list */ {
858 AsmBlock : STRINGCONSTANT ;
860 BigOrLittle : BIG | LITTLE
863 : ENDIAN '=' BigOrLittle {
868 | POINTERSIZE '=' EUINT64VAL {
875 | TRIPLE '=' STRINGCONSTANT {
880 | DATALAYOUT '=' STRINGCONSTANT {
894 : LibList ',' STRINGCONSTANT {
900 | /* empty: end of list */ {
901 $$ = new std::string();
904 //===----------------------------------------------------------------------===//
905 // Rules to match Function Headers
906 //===----------------------------------------------------------------------===//
908 Name : VAR_ID | STRINGCONSTANT;
909 OptName : Name | /*empty*/ { $$ = new std::string(); };
911 ArgVal : Types OptName {
918 ArgListH : ArgListH ',' ArgVal {
929 | ArgListH ',' DOTDOTDOT {
937 | /* empty */ { $$ = new std::string(); };
940 : OptCallingConv TypesV Name '(' ArgList ')' OptSection OptAlign {
944 *$1 += *$2.newTy + " " + *$3 + "(" + *$5 + ")";
959 BEGIN : BEGINTOK { $$ = new std::string("{"); delete $1; }
960 | '{' { $$ = new std::string ("{"); }
963 : OptLinkage FunctionHeaderH BEGIN {
968 *O << *$2 << ' ' << *$3 << '\n';
969 delete $1; delete $2; delete $3;
974 END : ENDTOK { $$ = new std::string("}"); delete $1; }
975 | '}' { $$ = new std::string("}"); };
977 Function : FunctionHeader BasicBlockList END {
985 : /*default*/ { $$ = new std::string(); }
991 : DECLARE FnDeclareLinkage FunctionHeaderH {
1000 //===----------------------------------------------------------------------===//
1001 // Rules to match Basic Blocks
1002 //===----------------------------------------------------------------------===//
1004 OptSideEffect : /* empty */ { $$ = new std::string(); }
1008 : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK | NULL_TOK | UNDEF
1010 | '<' ConstVector '>' {
1016 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
1020 *$1 += " " + *$3 + ", " + *$5;
1021 delete $2; delete $3; delete $5;
1025 SymbolicValueRef : IntVal | Name ;
1027 // ValueRef - A reference to a definition... either constant or symbolic
1029 : SymbolicValueRef {
1031 $$.constant = false;
1033 $$.type.oldTy = UnresolvedTy;
1039 $$.type.oldTy = UnresolvedTy;
1043 // ResolvedVal - a <type> <value> pair. This is used only in cases where the
1044 // type immediately preceeds the value reference, and allows complex constant
1045 // pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1046 ResolvedVal : Types ValueRef {
1049 $$.val->insert(0, *$1.newTy + " ");
1052 BasicBlockList : BasicBlockList BasicBlock {
1055 | BasicBlock { // Do not allow functions with 0 basic blocks
1060 // Basic blocks are terminated by branching instructions:
1061 // br, br/cc, switch, ret
1063 BasicBlock : InstructionList BBTerminatorInst {
1067 InstructionList : InstructionList Inst {
1068 *O << " " << *$2 << '\n';
1081 Unwind : UNWIND | EXCEPT { $$ = $1; *$$ = "unwind"; }
1083 BBTerminatorInst : RET ResolvedVal { // Return with a result...
1084 *O << " " << *$1 << ' ' << *$2.val << '\n';
1085 delete $1; $2.destroy();
1088 | RET VOID { // Return with no result...
1089 *O << " " << *$1 << ' ' << *$2.newTy << '\n';
1090 delete $1; $2.destroy();
1093 | BR LABEL ValueRef { // Unconditional Branch...
1094 *O << " " << *$1 << ' ' << *$2.newTy << ' ' << *$3.val << '\n';
1095 delete $1; $2.destroy(); $3.destroy();
1097 } // Conditional Branch...
1098 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
1099 *O << " " << *$1 << ' ' << *$2.newTy << ' ' << *$3.val << ", "
1100 << *$5.newTy << ' ' << *$6.val << ", " << *$8.newTy << ' '
1102 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1103 $8.destroy(); $9.destroy();
1106 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1107 *O << " " << *$1 << ' ' << *$2.newTy << ' ' << *$3.val << ", "
1108 << *$5.newTy << ' ' << *$6.val << " [" << *$8 << " ]\n";
1109 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1113 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1114 *O << " " << *$1 << ' ' << *$2.newTy << ' ' << *$3.val << ", "
1115 << *$5.newTy << ' ' << *$6.val << "[]\n";
1116 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1119 | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
1120 TO LABEL ValueRef Unwind LABEL ValueRef {
1124 *O << *$2 << ' ' << *$3 << ' ' << *$4.newTy << ' ' << *$5.val << " (";
1125 for (unsigned i = 0; i < $7->size(); ++i) {
1126 ValueInfo& VI = (*$7)[i];
1128 if (i+1 < $7->size())
1132 *O << ") " << *$9 << ' ' << *$10.newTy << ' ' << *$11.val << ' '
1133 << *$12 << ' ' << *$13.newTy << ' ' << *$14.val << '\n';
1134 delete $1; delete $2; delete $3; $4.destroy(); $5.destroy(); delete $7;
1135 delete $9; $10.destroy(); $11.destroy(); delete $12; $13.destroy();
1140 *O << " " << *$1 << '\n';
1145 *O << " " << *$1 << '\n';
1150 JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1151 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5.newTy + " " + *$6.val;
1152 $2.destroy(); delete $3; $5.destroy(); $6.destroy();
1155 | IntType ConstValueRef ',' LABEL ValueRef {
1156 $2->insert(0, *$1.newTy + " " );
1157 *$2 += ", " + *$4.newTy + " " + *$5.val;
1158 $1.destroy(); $4.destroy(); $5.destroy();
1163 : OptAssign InstVal {
1165 if (deleteUselessCastFlag && *deleteUselessCastName == *$1) {
1167 $1->insert(0, "; "); // don't actually delete it, just comment it out
1168 delete deleteUselessCastName;
1175 deleteUselessCastFlag = false;
1180 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1181 $3.val->insert(0, *$1.newTy + "[");
1182 *$3.val += "," + *$5.val + "]";
1183 $1.destroy(); $5.destroy();
1184 $$ = new std::string(*$3.val);
1187 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1188 *$1 += ", [" + *$4.val + "," + *$6.val + "]";
1189 $4.destroy(); $6.destroy();
1196 $$ = new ValueList();
1199 | ValueRefList ',' ResolvedVal {
1204 // ValueRefListE - Just like ValueRefList, except that it may also be empty!
1206 : ValueRefList { $$ = $1; }
1207 | /*empty*/ { $$ = new ValueList(); }
1219 InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1220 const char* op = getDivRemOpcode(*$1, $2);
1221 $$ = new std::string(op);
1222 *$$ += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1223 delete $1; $2.destroy(); $3.destroy(); $5.destroy();
1225 | LogicalOps Types ValueRef ',' ValueRef {
1226 *$1 += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1227 $2.destroy(); $3.destroy(); $5.destroy();
1230 | SetCondOps Types ValueRef ',' ValueRef {
1231 *$1 = getCompareOp(*$1, $2);
1232 *$1 += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1233 $2.destroy(); $3.destroy(); $5.destroy();
1236 | ICMP IPredicates Types ValueRef ',' ValueRef {
1237 *$1 += " " + *$2 + " " + *$3.newTy + " " + *$4.val + "," + *$6.val;
1238 delete $2; $4.destroy(); $6.destroy();
1241 | FCMP FPredicates Types ValueRef ',' ValueRef {
1242 *$1 += " " + *$2 + " " + *$3.newTy + " " + *$4.val + "," + *$6.val;
1243 delete $2; $4.destroy(); $6.destroy();
1247 *$1 += " " + *$2.val;
1251 | ShiftOps ResolvedVal ',' ResolvedVal {
1252 const char* shiftop = $1->c_str();
1254 shiftop = ($2.type.isUnsigned()) ? "lshr" : "ashr";
1255 $$ = new std::string(shiftop);
1256 *$$ += " " + *$2.val + ", " + *$4.val;
1257 delete $1; $2.destroy(); $4.destroy();
1259 | CastOps ResolvedVal TO Types {
1260 std::string source = *$2.val;
1261 TypeInfo SrcTy = $2.type;
1262 TypeInfo DstTy = $4;
1264 $$ = new std::string();
1265 if (*$1 == "cast") {
1266 *$$ += getCastUpgrade(source, SrcTy, DstTy, false);
1268 *$$ += *$1 + " " + source + " to " + *DstTy.newTy;
1270 // Check to see if this is a useless cast of a value to the same name
1271 // and the same type. Such casts will probably cause redefinition errors
1272 // when assembled and perform no code gen action so just remove them.
1273 if (*$1 == "cast" || *$1 == "bitcast")
1274 if ($2.type.isInteger() && $4.isInteger() &&
1275 $2.type.getBitWidth() == $4.getBitWidth()) {
1276 deleteUselessCastFlag = true; // Flag the "Inst" rule
1277 deleteUselessCastName = new std::string(*$2.val); // save the name
1278 size_t pos = deleteUselessCastName->find_first_of("%\"",0);
1279 if (pos != std::string::npos) {
1280 // remove the type portion before val
1281 deleteUselessCastName->erase(0, pos);
1284 delete $1; $2.destroy();
1285 delete $3; $4.destroy();
1287 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1288 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1289 $2.destroy(); $4.destroy(); $6.destroy();
1292 | VAARG ResolvedVal ',' Types {
1293 *$1 += " " + *$2.val + ", " + *$4.newTy;
1294 $2.destroy(); $4.destroy();
1297 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
1298 *$1 += " " + *$2.val + ", " + *$4.val;
1299 $2.destroy(); $4.destroy();
1302 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1303 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1304 $2.destroy(); $4.destroy(); $6.destroy();
1307 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1308 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1309 $2.destroy(); $4.destroy(); $6.destroy();
1317 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
1322 *$1 += *$3.newTy + " " + *$4.val + "(";
1323 for (unsigned i = 0; i < $6->size(); ++i) {
1324 ValueInfo& VI = (*$6)[i];
1326 if (i+1 < $6->size())
1331 delete $2; $3.destroy(); $4.destroy(); delete $6;
1337 // IndexList - List of indices for GEP based instructions...
1339 : ',' ValueRefList { $$ = $2; }
1340 | /* empty */ { $$ = new ValueList(); }
1345 | /* empty */ { $$ = new std::string(); }
1348 MemoryInst : MALLOC Types OptCAlign {
1349 *$1 += " " + *$2.newTy;
1352 $2.destroy(); delete $3;
1355 | MALLOC Types ',' UINT ValueRef OptCAlign {
1356 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5.val;
1359 $2.destroy(); $4.destroy(); $5.destroy(); delete $6;
1362 | ALLOCA Types OptCAlign {
1363 *$1 += " " + *$2.newTy;
1366 $2.destroy(); delete $3;
1369 | ALLOCA Types ',' UINT ValueRef OptCAlign {
1370 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5.val;
1373 $2.destroy(); $4.destroy(); $5.destroy(); delete $6;
1376 | FREE ResolvedVal {
1377 *$1 += " " + *$2.val;
1381 | OptVolatile LOAD Types ValueRef {
1384 *$1 += *$2 + " " + *$3.newTy + " " + *$4.val;
1385 delete $2; $3.destroy(); $4.destroy();
1388 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1391 *$1 += *$2 + " " + *$3.val + ", " + *$5.newTy + " " + *$6.val;
1392 delete $2; $3.destroy(); $5.destroy(); $6.destroy();
1395 | GETELEMENTPTR Types ValueRef IndexList {
1396 // Upgrade the indices
1397 for (unsigned i = 0; i < $4->size(); ++i) {
1398 ValueInfo& VI = (*$4)[i];
1399 if (VI.type.isUnsigned() && !VI.isConstant() &&
1400 VI.type.getBitWidth() < 64) {
1401 std::string* old = VI.val;
1402 *O << " %gep_upgrade" << unique << " = zext " << *old
1404 VI.val = new std::string("i64 %gep_upgrade" + llvm::utostr(unique++));
1405 VI.type.oldTy = ULongTy;
1409 *$1 += " " + *$2.newTy + " " + *$3.val;
1410 for (unsigned i = 0; i < $4->size(); ++i) {
1411 ValueInfo& VI = (*$4)[i];
1412 *$1 += ", " + *VI.val;
1415 $2.destroy(); $3.destroy(); delete $4;
1421 int yyerror(const char *ErrorMsg) {
1423 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1424 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
1425 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
1426 if (yychar == YYEMPTY || yychar == 0)
1427 errMsg += "end-of-file.";
1429 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
1430 std::cerr << "llvm-upgrade: " << errMsg << '\n';