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;
42 static TypeMap Globals;
44 void destroy(ValueList* VL) {
45 while (!VL->empty()) {
46 ValueInfo& VI = VL->back();
53 void UpgradeAssembly(const std::string &infile, std::istream& in,
54 std::ostream &out, bool debug)
63 std::cerr << "Parse failed.\n";
68 static void ResolveType(TypeInfo& Ty) {
69 if (Ty.oldTy == UnresolvedTy) {
70 TypeMap::iterator I = NamedTypes.find(*Ty.newTy);
71 if (I != NamedTypes.end()) {
72 Ty.oldTy = I->second.oldTy;
73 Ty.elemTy = I->second.elemTy;
75 std::string msg("Can't resolve type: ");
79 } else if (Ty.oldTy == NumericTy) {
80 unsigned ref = atoi(&((Ty.newTy->c_str())[1])); // Skip the '\\'
81 if (ref < EnumeratedTypes.size()) {
82 Ty.oldTy = EnumeratedTypes[ref].oldTy;
83 Ty.elemTy = EnumeratedTypes[ref].elemTy;
85 std::string msg("Can't resolve type: ");
90 // otherwise its already resolved.
93 static const char* getCastOpcode(
94 std::string& Source, const TypeInfo& SrcTy, const TypeInfo& DstTy)
96 unsigned SrcBits = SrcTy.getBitWidth();
97 unsigned DstBits = DstTy.getBitWidth();
98 const char* opcode = "bitcast";
99 // Run through the possibilities ...
100 if (DstTy.isIntegral()) { // Casting to integral
101 if (SrcTy.isIntegral()) { // Casting from integral
102 if (DstBits < SrcBits)
104 else if (DstBits > SrcBits) { // its an extension
105 if (SrcTy.isSigned())
106 opcode ="sext"; // signed -> SEXT
108 opcode = "zext"; // unsigned -> ZEXT
110 opcode = "bitcast"; // Same size, No-op cast
112 } else if (SrcTy.isFloatingPoint()) { // Casting from floating pt
113 if (DstTy.isSigned())
114 opcode = "fptosi"; // FP -> sint
116 opcode = "fptoui"; // FP -> uint
117 } else if (SrcTy.isPacked()) {
118 assert(DstBits == SrcTy.getBitWidth() &&
119 "Casting packed to integer of different width");
120 opcode = "bitcast"; // same size, no-op cast
122 assert(SrcTy.isPointer() &&
123 "Casting from a value that is not first-class type");
124 opcode = "ptrtoint"; // ptr -> int
126 } else if (DstTy.isFloatingPoint()) { // Casting to floating pt
127 if (SrcTy.isIntegral()) { // Casting from integral
128 if (SrcTy.isSigned())
129 opcode = "sitofp"; // sint -> FP
131 opcode = "uitofp"; // uint -> FP
132 } else if (SrcTy.isFloatingPoint()) { // Casting from floating pt
133 if (DstBits < SrcBits) {
134 opcode = "fptrunc"; // FP -> smaller FP
135 } else if (DstBits > SrcBits) {
136 opcode = "fpext"; // FP -> larger FP
138 opcode ="bitcast"; // same size, no-op cast
140 } else if (SrcTy.isPacked()) {
141 assert(DstBits == SrcTy.getBitWidth() &&
142 "Casting packed to floating point of different width");
143 opcode = "bitcast"; // same size, no-op cast
145 assert(0 && "Casting pointer or non-first class to float");
147 } else if (DstTy.isPacked()) {
148 if (SrcTy.isPacked()) {
149 assert(DstTy.getBitWidth() == SrcTy.getBitWidth() &&
150 "Casting packed to packed of different widths");
151 opcode = "bitcast"; // packed -> packed
152 } else if (DstTy.getBitWidth() == SrcBits) {
153 opcode = "bitcast"; // float/int -> packed
155 assert(!"Illegal cast to packed (wrong type or size)");
157 } else if (DstTy.isPointer()) {
158 if (SrcTy.isPointer()) {
159 opcode = "bitcast"; // ptr -> ptr
160 } else if (SrcTy.isIntegral()) {
161 opcode = "inttoptr"; // int -> ptr
163 assert(!"Casting invalid type to pointer");
166 assert(!"Casting to type that is not first-class");
171 static std::string getCastUpgrade(
172 const std::string& Src, TypeInfo& SrcTy, TypeInfo& DstTy, bool isConst)
175 std::string Source = Src;
176 if (SrcTy.isFloatingPoint() && DstTy.isPointer()) {
177 // fp -> ptr cast is no longer supported but we must upgrade this
178 // by doing a double cast: fp -> int -> ptr
180 Source = "ulong fptoui(" + Source + " to ulong)";
182 *O << " %cast_upgrade" << unique << " = fptoui " << Source
184 Source = "ulong %cast_upgrade" + llvm::utostr(unique);
186 // Update the SrcTy for the getCastOpcode call below
188 SrcTy.newTy = new std::string("ulong");
189 SrcTy.oldTy = ULongTy;
190 } else if (DstTy.oldTy == BoolTy) {
191 // cast ptr %x to bool was previously defined as setne ptr %x, null
192 // The ptrtoint semantic is to truncate, not compare so we must retain
193 // the original intent by replace the cast with a setne
194 const char* comparator = SrcTy.isPointer() ? ", null" :
195 (SrcTy.isFloatingPoint() ? ", 0.0" : ", 0");
197 Result = "setne (" + Source + comparator + ")";
199 Result = "setne " + Source + comparator;
200 return Result; // skip cast processing below
204 std::string Opcode(getCastOpcode(Source, SrcTy, DstTy));
206 Result += Opcode + "( " + Source + " to " + *DstTy.newTy + ")";
208 Result += Opcode + " " + Source + " to " + *DstTy.newTy;
212 const char* getDivRemOpcode(const std::string& opcode, const TypeInfo& TI) {
213 const char* op = opcode.c_str();
217 Ty.oldTy = Ty.getElementType();
219 if (Ty.isFloatingPoint())
221 else if (Ty.isUnsigned())
223 else if (Ty.isSigned())
226 yyerror("Invalid type for div instruction");
227 else if (opcode == "rem")
228 if (Ty.isFloatingPoint())
230 else if (Ty.isUnsigned())
232 else if (Ty.isSigned())
235 yyerror("Invalid type for rem instruction");
240 getCompareOp(const std::string& setcc, const TypeInfo& TI) {
241 assert(setcc.length() == 5);
244 assert(cc1 == 'e' || cc1 == 'n' || cc1 == 'l' || cc1 == 'g');
245 assert(cc2 == 'q' || cc2 == 'e' || cc2 == 'e' || cc2 == 't');
246 std::string result("xcmp xxx");
249 if (TI.isFloatingPoint()) {
251 result[5] = 'o'; // FIXME: Always map to ordered comparison ?
252 } else if (TI.isIntegral() || TI.isPointer()) {
254 if ((cc1 == 'e' && cc2 == 'q') || (cc1 == 'n' && cc2 == 'e'))
256 else if (TI.isSigned())
258 else if (TI.isUnsigned() || TI.isPointer())
261 yyerror("Invalid integral type for setcc");
268 %file-prefix="UpgradeParser"
278 %token <Type> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
279 %token <Type> FLOAT DOUBLE LABEL
280 %token <String> OPAQUE ESINT64VAL EUINT64VAL SINTVAL UINTVAL FPVAL
281 %token <String> NULL_TOK UNDEF ZEROINITIALIZER TRUETOK FALSETOK
282 %token <String> TYPE VAR_ID LABELSTR STRINGCONSTANT
283 %token <String> IMPLEMENTATION BEGINTOK ENDTOK
284 %token <String> DECLARE GLOBAL CONSTANT SECTION VOLATILE
285 %token <String> TO DOTDOTDOT CONST INTERNAL LINKONCE WEAK
286 %token <String> DLLIMPORT DLLEXPORT EXTERN_WEAK APPENDING
287 %token <String> NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
288 %token <String> ALIGN UNINITIALIZED
289 %token <String> DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
290 %token <String> CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
291 %token <String> X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
292 %token <String> DATALAYOUT
293 %token <String> RET BR SWITCH INVOKE EXCEPT UNWIND UNREACHABLE
294 %token <String> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM AND OR XOR
295 %token <String> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
296 %token <String> ICMP FCMP EQ NE SLT SGT SLE SGE OEQ ONE OLT OGT OLE OGE
297 %token <String> ORD UNO UEQ UNE ULT UGT ULE UGE
298 %token <String> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
299 %token <String> PHI_TOK SELECT SHL SHR ASHR LSHR VAARG
300 %token <String> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
301 %token <String> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI UITOFP SITOFP
302 %token <String> PTRTOINT INTTOPTR BITCAST
304 %type <String> OptAssign OptLinkage OptCallingConv OptAlign OptCAlign
305 %type <String> SectionString OptSection GlobalVarAttributes GlobalVarAttribute
306 %type <String> ArgTypeListI ConstExpr DefinitionList
307 %type <String> ConstPool TargetDefinition LibrariesDefinition LibList OptName
308 %type <String> ArgVal ArgListH ArgList FunctionHeaderH BEGIN FunctionHeader END
309 %type <String> Function FunctionProto BasicBlock TypeListI
310 %type <String> InstructionList BBTerminatorInst JumpTable Inst PHIList
311 %type <String> OptTailCall InstVal OptVolatile Unwind
312 %type <String> MemoryInst SymbolicValueRef OptSideEffect GlobalType
313 %type <String> FnDeclareLinkage BasicBlockList BigOrLittle AsmBlock
314 %type <String> Name ConstValueRef ConstVector External
315 %type <String> ShiftOps SetCondOps LogicalOps ArithmeticOps CastOps
316 %type <String> IPredicates FPredicates
318 %type <ValList> ValueRefList ValueRefListE IndexList
320 %type <Type> IntType SIntType UIntType FPType TypesV Types
321 %type <Type> PrimType UpRTypesV UpRTypes
323 %type <String> IntVal EInt64Val
324 %type <Const> ConstVal
326 %type <Value> ValueRef ResolvedVal
332 // Handle constant integer size restriction and conversion...
333 IntVal : SINTVAL | UINTVAL ;
334 EInt64Val : ESINT64VAL | EUINT64VAL;
336 // Operations that are notably excluded from this list include:
337 // RET, BR, & SWITCH because they end basic blocks and are treated specially.
338 ArithmeticOps: ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV
339 | REM | UREM | SREM | FREM;
340 LogicalOps : AND | OR | XOR;
341 SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
342 IPredicates : EQ | NE | SLT | SGT | SLE | SGE | ULT | UGT | ULE | UGE;
343 FPredicates : OEQ | ONE | OLT | OGT | OLE | OGE | ORD | UNO | UEQ | UNE
344 | ULT | UGT | ULE | UGE | TRUETOK | FALSETOK;
345 ShiftOps : SHL | SHR | ASHR | LSHR;
346 CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI |
347 UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
350 // These are some types that allow classification if we only want a particular
351 // thing... for example, only a signed, unsigned, or integral type.
352 SIntType : LONG | INT | SHORT | SBYTE;
353 UIntType : ULONG | UINT | USHORT | UBYTE;
354 IntType : SIntType | UIntType;
355 FPType : FLOAT | DOUBLE;
357 // OptAssign - Value producing statements have an optional assignment component
358 OptAssign : Name '=' {
362 $$ = new std::string("");
366 : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT
368 | /*empty*/ { $$ = new std::string(""); } ;
371 : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK
373 | CC_TOK EUINT64VAL {
378 | /*empty*/ { $$ = new std::string(""); } ;
380 // OptAlign/OptCAlign - An optional alignment, and an optional alignment with
381 // a comma before it.
383 : /*empty*/ { $$ = new std::string(); }
384 | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
387 : /*empty*/ { $$ = new std::string(); }
388 | ',' ALIGN EUINT64VAL {
396 : SECTION STRINGCONSTANT {
402 OptSection : /*empty*/ { $$ = new std::string(); }
406 : /* empty */ { $$ = new std::string(); }
407 | ',' GlobalVarAttribute GlobalVarAttributes {
423 //===----------------------------------------------------------------------===//
424 // Types includes all predefined types... except void, because it can only be
425 // used in specific contexts (function returning void for example). To have
426 // access to it, a user must explicitly use TypesV.
429 // TypesV includes all of 'Types', but it also includes the void type.
430 TypesV : Types | VOID ;
431 UpRTypesV : UpRTypes | VOID ;
434 // Derived types are added later...
436 PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
437 PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
445 $$.oldTy = UnresolvedTy;
450 | '\\' EUINT64VAL { // Type UpReference
453 $$.oldTy = NumericTy;
455 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
456 *$1.newTy += "( " + *$3 + " )";
459 $$.oldTy = FunctionTy;
461 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
463 *$2 += " x " + *$4.newTy + " ]";
467 $$.elemTy = $4.oldTy;
469 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
471 *$2 += " x " + *$4.newTy + " >";
475 $$.elemTy = $4.oldTy;
477 | '{' TypeListI '}' { // Structure type?
483 | '{' '}' { // Empty structure type?
484 $$.newTy = new std::string("{}");
487 | UpRTypes '*' { // Pointer type?
489 $$.elemTy = $1.oldTy;
490 $1.oldTy = PointerTy;
494 // TypeList - Used for struct declarations and as a basis for function type
495 // declaration type lists
501 | TypeListI ',' UpRTypes {
502 *$1 += ", " + *$3.newTy;
507 // ArgTypeList - List of types for a function type declaration...
510 | TypeListI ',' DOTDOTDOT {
519 $$ = new std::string();
522 // ConstVal - The various declarations that go into the constant pool. This
523 // production is used ONLY to represent constants that show up AFTER a 'const',
524 // 'constant' or 'global' token at global scope. Constants that can be inlined
525 // into other expressions (such as integers and constexprs) are handled by the
526 // ResolvedVal, ValueRef and ConstValueRef productions.
528 ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
530 $$.cnst = new std::string(*$1.newTy);
531 *$$.cnst += " [ " + *$3 + " ]";
536 $$.cnst = new std::string(*$1.newTy);
539 | Types 'c' STRINGCONSTANT {
541 $$.cnst = new std::string(*$1.newTy);
542 *$$.cnst += " c" + *$3;
545 | Types '<' ConstVector '>' { // Nonempty unsized arr
547 $$.cnst = new std::string(*$1.newTy);
548 *$$.cnst += " < " + *$3 + " >";
551 | Types '{' ConstVector '}' {
553 $$.cnst = new std::string(*$1.newTy);
554 *$$.cnst += " { " + *$3 + " }";
559 $$.cnst = new std::string(*$1.newTy);
564 $$.cnst = new std::string(*$1.newTy);
565 *$$.cnst += " " + *$2;
570 $$.cnst = new std::string(*$1.newTy);
571 *$$.cnst += " " + *$2;
574 | Types SymbolicValueRef {
576 $$.cnst = new std::string(*$1.newTy);
577 *$$.cnst += " " + *$2;
582 $$.cnst = new std::string(*$1.newTy);
583 *$$.cnst += " " + *$2;
586 | Types ZEROINITIALIZER {
588 $$.cnst = new std::string(*$1.newTy);
589 *$$.cnst += " " + *$2;
592 | SIntType EInt64Val { // integral constants
594 $$.cnst = new std::string(*$1.newTy);
595 *$$.cnst += " " + *$2;
598 | UIntType EUINT64VAL { // integral constants
600 $$.cnst = new std::string(*$1.newTy);
601 *$$.cnst += " " + *$2;
604 | BOOL TRUETOK { // Boolean constants
606 $$.cnst = new std::string(*$1.newTy);
607 *$$.cnst += " " + *$2;
610 | BOOL FALSETOK { // Boolean constants
612 $$.cnst = new std::string(*$1.newTy);
613 *$$.cnst += " " + *$2;
616 | FPType FPVAL { // Float & Double constants
618 $$.cnst = new std::string(*$1.newTy);
619 *$$.cnst += " " + *$2;
624 ConstExpr: CastOps '(' ConstVal TO Types ')' {
625 std::string source = *$3.cnst;
629 // Call getCastUpgrade to upgrade the old cast
630 $$ = new std::string(getCastUpgrade(source, $3.type, $5, true));
632 // Nothing to upgrade, just create the cast constant expr
633 $$ = new std::string(*$1);
634 *$$ += "( " + source + " to " + *$5.newTy + ")";
636 delete $1; $3.destroy(); delete $4; $5.destroy();
638 | GETELEMENTPTR '(' ConstVal IndexList ')' {
639 *$1 += "(" + *$3.cnst;
640 for (unsigned i = 0; i < $4->size(); ++i) {
641 ValueInfo& VI = (*$4)[i];
642 *$1 += ", " + *VI.val;
650 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
651 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
652 $3.destroy(); $5.destroy(); $7.destroy();
655 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
656 const char* op = getDivRemOpcode(*$1, $3.type);
657 $$ = new std::string(op);
658 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
659 delete $1; $3.destroy(); $5.destroy();
661 | LogicalOps '(' ConstVal ',' ConstVal ')' {
662 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
663 $3.destroy(); $5.destroy();
666 | SetCondOps '(' ConstVal ',' ConstVal ')' {
667 #if UPGRADE_SETCOND_OPS
668 *$1 = getCompareOp(*$1, $3.type);
670 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
671 $3.destroy(); $5.destroy();
674 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
675 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
676 delete $2; $4.destroy(); $6.destroy();
679 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
680 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
681 delete $2; $4.destroy(); $6.destroy();
684 | ShiftOps '(' ConstVal ',' ConstVal ')' {
685 const char* shiftop = $1->c_str();
687 shiftop = ($3.type.isUnsigned()) ? "lshr" : "ashr";
688 $$ = new std::string(shiftop);
689 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
690 delete $1; $3.destroy(); $5.destroy();
692 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
693 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
694 $3.destroy(); $5.destroy();
697 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
698 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
699 $3.destroy(); $5.destroy(); $7.destroy();
702 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
703 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
704 $3.destroy(); $5.destroy(); $7.destroy();
709 // ConstVector - A list of comma separated constants.
712 : ConstVector ',' ConstVal {
713 *$1 += ", " + *$3.cnst;
717 | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
721 // GlobalType - Match either GLOBAL or CONSTANT for global declarations...
722 GlobalType : GLOBAL | CONSTANT ;
725 //===----------------------------------------------------------------------===//
726 // Rules to match Modules
727 //===----------------------------------------------------------------------===//
729 // Module rule: Capture the result of parsing the whole file into a result
732 Module : DefinitionList {
735 // DefinitionList - Top level definitions
737 DefinitionList : DefinitionList Function {
740 | DefinitionList FunctionProto {
745 | DefinitionList MODULE ASM_TOK AsmBlock {
746 *O << "module asm " << " " << *$4 << "\n";
749 | DefinitionList IMPLEMENTATION {
750 *O << "implementation\n";
753 | ConstPool { $$ = 0; }
755 External : EXTERNAL | UNINITIALIZED { $$ = $1; *$$ = "external"; }
757 // ConstPool - Constants with optional names assigned to them.
758 ConstPool : ConstPool OptAssign TYPE TypesV {
759 EnumeratedTypes.push_back($4);
761 NamedTypes[*$2].newTy = new std::string(*$4.newTy);
762 NamedTypes[*$2].oldTy = $4.oldTy;
763 NamedTypes[*$2].elemTy = $4.elemTy;
766 *O << "type " << *$4.newTy << "\n";
767 delete $2; delete $3; $4.destroy();
770 | ConstPool FunctionProto { // Function prototypes can be in const pool
775 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
776 *O << *$2 << " " << *$3 << " " << *$4 << "\n";
777 delete $2; delete $3; delete $4;
780 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
783 Globals[*$2] = $5.type.clone();
785 *O << *$3 << " " << *$4 << " " << *$5.cnst << " " << *$6 << "\n";
786 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
789 | ConstPool OptAssign External GlobalType Types GlobalVarAttributes {
792 Globals[*$2] = $5.clone();
794 *O << *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
795 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
798 | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
801 Globals[*$2] = $5.clone();
803 *O << *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
804 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
807 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
810 Globals[*$2] = $5.clone();
812 *O << *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
813 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
816 | ConstPool TARGET TargetDefinition {
817 *O << *$2 << " " << *$3 << "\n";
818 delete $2; delete $3;
821 | ConstPool DEPLIBS '=' LibrariesDefinition {
822 *O << *$2 << " = " << *$4 << "\n";
823 delete $2; delete $4;
826 | /* empty: end of list */ {
831 AsmBlock : STRINGCONSTANT ;
833 BigOrLittle : BIG | LITTLE
836 : ENDIAN '=' BigOrLittle {
841 | POINTERSIZE '=' EUINT64VAL {
848 | TRIPLE '=' STRINGCONSTANT {
853 | DATALAYOUT '=' STRINGCONSTANT {
867 : LibList ',' STRINGCONSTANT {
873 | /* empty: end of list */ {
874 $$ = new std::string();
877 //===----------------------------------------------------------------------===//
878 // Rules to match Function Headers
879 //===----------------------------------------------------------------------===//
881 Name : VAR_ID | STRINGCONSTANT;
882 OptName : Name | /*empty*/ { $$ = new std::string(); };
884 ArgVal : Types OptName {
891 ArgListH : ArgListH ',' ArgVal {
902 | ArgListH ',' DOTDOTDOT {
910 | /* empty */ { $$ = new std::string(); };
912 FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
913 OptSection OptAlign {
917 *$1 += *$2.newTy + " " + *$3 + "(" + *$5 + ")";
932 BEGIN : BEGINTOK { $$ = new std::string("{"); delete $1; }
933 | '{' { $$ = new std::string ("{"); }
935 FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
939 *O << *$2 << " " << *$3 << "\n";
940 delete $1; delete $2; delete $3;
944 END : ENDTOK { $$ = new std::string("}"); delete $1; }
945 | '}' { $$ = new std::string("}"); };
947 Function : FunctionHeader BasicBlockList END {
950 *O << '\n' << *$3 << "\n";
955 : /*default*/ { $$ = new std::string(); }
961 : DECLARE FnDeclareLinkage FunctionHeaderH {
970 //===----------------------------------------------------------------------===//
971 // Rules to match Basic Blocks
972 //===----------------------------------------------------------------------===//
974 OptSideEffect : /* empty */ { $$ = new std::string(); }
978 : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK | NULL_TOK | UNDEF
980 | '<' ConstVector '>' {
986 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
990 *$1 += " " + *$3 + ", " + *$5;
991 delete $2; delete $3; delete $5;
995 SymbolicValueRef : IntVal | Name ;
997 // ValueRef - A reference to a definition... either constant or symbolic
1001 $$.constant = false;
1003 $$.type.oldTy = UnresolvedTy;
1009 $$.type.oldTy = UnresolvedTy;
1013 // ResolvedVal - a <type> <value> pair. This is used only in cases where the
1014 // type immediately preceeds the value reference, and allows complex constant
1015 // pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1016 ResolvedVal : Types ValueRef {
1019 $$.val->insert(0, *$1.newTy + " ");
1022 BasicBlockList : BasicBlockList BasicBlock {
1025 | BasicBlock { // Do not allow functions with 0 basic blocks
1030 // Basic blocks are terminated by branching instructions:
1031 // br, br/cc, switch, ret
1033 BasicBlock : InstructionList BBTerminatorInst {
1037 InstructionList : InstructionList Inst {
1038 *O << " " << *$2 << "\n";
1051 Unwind : UNWIND | EXCEPT { $$ = $1; *$$ = "unwind"; }
1053 BBTerminatorInst : RET ResolvedVal { // Return with a result...
1054 *O << " " << *$1 << " " << *$2.val << "\n";
1055 delete $1; $2.destroy();
1058 | RET VOID { // Return with no result...
1059 *O << " " << *$1 << " " << *$2.newTy << "\n";
1060 delete $1; $2.destroy();
1063 | BR LABEL ValueRef { // Unconditional Branch...
1064 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << "\n";
1065 delete $1; $2.destroy(); $3.destroy();
1067 } // Conditional Branch...
1068 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
1069 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1070 << *$5.newTy << " " << *$6.val << ", " << *$8.newTy << " "
1072 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1073 $8.destroy(); $9.destroy();
1076 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1077 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1078 << *$5.newTy << " " << *$6.val << " [" << *$8 << " ]\n";
1079 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1083 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1084 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1085 << *$5.newTy << " " << *$6.val << "[]\n";
1086 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1089 | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
1090 TO LABEL ValueRef Unwind LABEL ValueRef {
1094 *O << *$2 << " " << *$3 << " " << *$4.newTy << " " << *$5.val << " (";
1095 for (unsigned i = 0; i < $7->size(); ++i) {
1096 ValueInfo& VI = (*$7)[i];
1098 if (i+1 < $7->size())
1102 *O << ") " << *$9 << " " << *$10.newTy << " " << *$11.val << " "
1103 << *$12 << " " << *$13.newTy << " " << *$14.val << "\n";
1104 delete $1; delete $2; delete $3; $4.destroy(); $5.destroy(); delete $7;
1105 delete $9; $10.destroy(); $11.destroy(); delete $12; $13.destroy();
1110 *O << " " << *$1 << "\n";
1115 *O << " " << *$1 << "\n";
1120 JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1121 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5.newTy + " " + *$6.val;
1122 $2.destroy(); delete $3; $5.destroy(); $6.destroy();
1125 | IntType ConstValueRef ',' LABEL ValueRef {
1126 $2->insert(0, *$1.newTy + " " );
1127 *$2 += ", " + *$4.newTy + " " + *$5.val;
1128 $1.destroy(); $4.destroy(); $5.destroy();
1133 : OptAssign InstVal {
1142 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1143 $3.val->insert(0, *$1.newTy + "[");
1144 *$3.val += "," + *$5.val + "]";
1145 $1.destroy(); $5.destroy();
1146 $$ = new std::string(*$3.val);
1149 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1150 *$1 += ", [" + *$4.val + "," + *$6.val + "]";
1151 $4.destroy(); $6.destroy();
1158 $$ = new ValueList();
1161 | ValueRefList ',' ResolvedVal {
1166 // ValueRefListE - Just like ValueRefList, except that it may also be empty!
1168 : ValueRefList { $$ = $1; }
1169 | /*empty*/ { $$ = new ValueList(); }
1181 InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1182 const char* op = getDivRemOpcode(*$1, $2);
1183 $$ = new std::string(op);
1184 *$$ += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1185 delete $1; $2.destroy(); $3.destroy(); $5.destroy();
1187 | LogicalOps Types ValueRef ',' ValueRef {
1188 *$1 += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1189 $2.destroy(); $3.destroy(); $5.destroy();
1192 | SetCondOps Types ValueRef ',' ValueRef {
1193 #if UPGRADE_SETCOND_OPS
1194 *$1 = getCompareOp(*$1, $2);
1196 *$1 += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1197 $2.destroy(); $3.destroy(); $5.destroy();
1200 | ICMP IPredicates Types ValueRef ',' ValueRef ')' {
1201 *$1 += " " + *$2 + " " + *$4.val + "," + *$6.val + ")";
1202 delete $2; $4.destroy(); $6.destroy();
1205 | FCMP FPredicates Types ValueRef ',' ValueRef ')' {
1206 *$1 += " " + *$2 + " " + *$4.val + "," + *$6.val + ")";
1207 delete $2; $4.destroy(); $6.destroy();
1211 *$1 += " " + *$2.val;
1215 | ShiftOps ResolvedVal ',' ResolvedVal {
1216 const char* shiftop = $1->c_str();
1218 shiftop = ($2.type.isUnsigned()) ? "lshr" : "ashr";
1219 $$ = new std::string(shiftop);
1220 *$$ += " " + *$2.val + ", " + *$4.val;
1221 delete $1; $2.destroy(); $4.destroy();
1223 | CastOps ResolvedVal TO Types {
1224 std::string source = *$2.val;
1225 TypeInfo SrcTy = $2.type;
1226 TypeInfo DstTy = $4;
1228 $$ = new std::string();
1229 if (*$1 == "cast") {
1230 *$$ += getCastUpgrade(source, SrcTy, DstTy, false);
1232 *$$ += *$1 + " " + source + " to " + *DstTy.newTy;
1234 delete $1; $2.destroy();
1235 delete $3; $4.destroy();
1237 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1238 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1239 $2.destroy(); $4.destroy(); $6.destroy();
1242 | VAARG ResolvedVal ',' Types {
1243 *$1 += " " + *$2.val + ", " + *$4.newTy;
1244 $2.destroy(); $4.destroy();
1247 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
1248 *$1 += " " + *$2.val + ", " + *$4.val;
1249 $2.destroy(); $4.destroy();
1252 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1253 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1254 $2.destroy(); $4.destroy(); $6.destroy();
1257 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1258 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1259 $2.destroy(); $4.destroy(); $6.destroy();
1267 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
1272 *$1 += *$3.newTy + " " + *$4.val + "(";
1273 for (unsigned i = 0; i < $6->size(); ++i) {
1274 ValueInfo& VI = (*$6)[i];
1276 if (i+1 < $6->size())
1281 delete $2; $3.destroy(); $4.destroy(); delete $6;
1287 // IndexList - List of indices for GEP based instructions...
1289 : ',' ValueRefList { $$ = $2; }
1290 | /* empty */ { $$ = new ValueList(); }
1295 | /* empty */ { $$ = new std::string(); }
1298 MemoryInst : MALLOC Types OptCAlign {
1299 *$1 += " " + *$2.newTy;
1302 $2.destroy(); delete $3;
1305 | MALLOC Types ',' UINT ValueRef OptCAlign {
1306 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5.val;
1309 $2.destroy(); $4.destroy(); $5.destroy(); delete $6;
1312 | ALLOCA Types OptCAlign {
1313 *$1 += " " + *$2.newTy;
1316 $2.destroy(); delete $3;
1319 | ALLOCA Types ',' UINT ValueRef OptCAlign {
1320 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5.val;
1323 $2.destroy(); $4.destroy(); $5.destroy(); delete $6;
1326 | FREE ResolvedVal {
1327 *$1 += " " + *$2.val;
1331 | OptVolatile LOAD Types ValueRef {
1334 *$1 += *$2 + " " + *$3.newTy + " " + *$4.val;
1335 delete $2; $3.destroy(); $4.destroy();
1338 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1341 *$1 += *$2 + " " + *$3.val + ", " + *$5.newTy + " " + *$6.val;
1342 delete $2; $3.destroy(); $5.destroy(); $6.destroy();
1345 | GETELEMENTPTR Types ValueRef IndexList {
1346 // Upgrade the indices
1347 for (unsigned i = 0; i < $4->size(); ++i) {
1348 ValueInfo& VI = (*$4)[i];
1349 if (VI.type.isUnsigned() && !VI.isConstant() &&
1350 VI.type.getBitWidth() < 64) {
1351 std::string* old = VI.val;
1352 *O << " %gep_upgrade" << unique << " = zext " << *old
1354 VI.val = new std::string("ulong %gep_upgrade" + llvm::utostr(unique++));
1355 VI.type.oldTy = ULongTy;
1359 *$1 += " " + *$2.newTy + " " + *$3.val;
1360 for (unsigned i = 0; i < $4->size(); ++i) {
1361 ValueInfo& VI = (*$4)[i];
1362 *$1 += ", " + *VI.val;
1365 $2.destroy(); $3.destroy(); delete $4;
1371 int yyerror(const char *ErrorMsg) {
1373 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1374 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
1375 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
1376 if (yychar == YYEMPTY || yychar == 0)
1377 errMsg += "end-of-file.";
1379 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
1380 std::cerr << errMsg << '\n';