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) {
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");
198 Result = "setne (" + Source + comparator + ")";
200 Result = "setne " + Source + comparator;
201 return Result; // skip cast processing below
205 std::string Opcode(getCastOpcode(Source, SrcTy, DstTy));
207 Result += Opcode + "( " + Source + " to " + *DstTy.newTy + ")";
209 Result += Opcode + " " + Source + " to " + *DstTy.newTy;
213 const char* getDivRemOpcode(const std::string& opcode, const TypeInfo& TI) {
214 const char* op = opcode.c_str();
218 Ty.oldTy = Ty.getElementType();
220 if (Ty.isFloatingPoint())
222 else if (Ty.isUnsigned())
224 else if (Ty.isSigned())
227 yyerror("Invalid type for div instruction");
228 else if (opcode == "rem")
229 if (Ty.isFloatingPoint())
231 else if (Ty.isUnsigned())
233 else if (Ty.isSigned())
236 yyerror("Invalid type for rem instruction");
241 getCompareOp(const std::string& setcc, const TypeInfo& TI) {
242 assert(setcc.length() == 5);
245 assert(cc1 == 'e' || cc1 == 'n' || cc1 == 'l' || cc1 == 'g');
246 assert(cc2 == 'q' || cc2 == 'e' || cc2 == 'e' || cc2 == 't');
247 std::string result("xcmp xxx");
250 if (TI.isFloatingPoint()) {
251 #if GENERATE_FCMP_INSTS
253 result[5] = 'o'; // FIXME: Always map to ordered comparison ?
255 result[5] = 'u'; // NE maps to unordered
257 result[5] = 'o'; // everything else maps to ordered
261 } else if (TI.isIntegral() || TI.isPointer()) {
263 if ((cc1 == 'e' && cc2 == 'q') || (cc1 == 'n' && cc2 == 'e'))
265 else if (TI.isSigned())
267 else if (TI.isUnsigned() || TI.isPointer() || TI.isBool())
270 yyerror("Invalid integral type for setcc");
277 // %file-prefix="UpgradeParser"
287 %token <Type> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
288 %token <Type> FLOAT DOUBLE LABEL
289 %token <String> OPAQUE ESINT64VAL EUINT64VAL SINTVAL UINTVAL FPVAL
290 %token <String> NULL_TOK UNDEF ZEROINITIALIZER TRUETOK FALSETOK
291 %token <String> TYPE VAR_ID LABELSTR STRINGCONSTANT
292 %token <String> IMPLEMENTATION BEGINTOK ENDTOK
293 %token <String> DECLARE GLOBAL CONSTANT SECTION VOLATILE
294 %token <String> TO DOTDOTDOT CONST INTERNAL LINKONCE WEAK
295 %token <String> DLLIMPORT DLLEXPORT EXTERN_WEAK APPENDING
296 %token <String> NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
297 %token <String> ALIGN UNINITIALIZED
298 %token <String> DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
299 %token <String> CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
300 %token <String> X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
301 %token <String> DATALAYOUT
302 %token <String> RET BR SWITCH INVOKE EXCEPT UNWIND UNREACHABLE
303 %token <String> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM AND OR XOR
304 %token <String> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
305 %token <String> ICMP FCMP EQ NE SLT SGT SLE SGE OEQ ONE OLT OGT OLE OGE
306 %token <String> ORD UNO UEQ UNE ULT UGT ULE UGE
307 %token <String> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
308 %token <String> PHI_TOK SELECT SHL SHR ASHR LSHR VAARG
309 %token <String> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
310 %token <String> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI UITOFP SITOFP
311 %token <String> PTRTOINT INTTOPTR BITCAST
313 %type <String> OptAssign OptLinkage OptCallingConv OptAlign OptCAlign
314 %type <String> SectionString OptSection GlobalVarAttributes GlobalVarAttribute
315 %type <String> ArgTypeListI ConstExpr DefinitionList
316 %type <String> ConstPool TargetDefinition LibrariesDefinition LibList OptName
317 %type <String> ArgVal ArgListH ArgList FunctionHeaderH BEGIN FunctionHeader END
318 %type <String> Function FunctionProto BasicBlock TypeListI
319 %type <String> InstructionList BBTerminatorInst JumpTable Inst PHIList
320 %type <String> OptTailCall InstVal OptVolatile Unwind
321 %type <String> MemoryInst SymbolicValueRef OptSideEffect GlobalType
322 %type <String> FnDeclareLinkage BasicBlockList BigOrLittle AsmBlock
323 %type <String> Name ConstValueRef ConstVector External
324 %type <String> ShiftOps SetCondOps LogicalOps ArithmeticOps CastOps
325 %type <String> IPredicates FPredicates
327 %type <ValList> ValueRefList ValueRefListE IndexList
329 %type <Type> IntType SIntType UIntType FPType TypesV Types
330 %type <Type> PrimType UpRTypesV UpRTypes
332 %type <String> IntVal EInt64Val
333 %type <Const> ConstVal
335 %type <Value> ValueRef ResolvedVal
341 // Handle constant integer size restriction and conversion...
342 IntVal : SINTVAL | UINTVAL ;
343 EInt64Val : ESINT64VAL | EUINT64VAL;
345 // Operations that are notably excluded from this list include:
346 // RET, BR, & SWITCH because they end basic blocks and are treated specially.
347 ArithmeticOps: ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV
348 | REM | UREM | SREM | FREM;
349 LogicalOps : AND | OR | XOR;
350 SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
351 IPredicates : EQ | NE | SLT | SGT | SLE | SGE | ULT | UGT | ULE | UGE;
352 FPredicates : OEQ | ONE | OLT | OGT | OLE | OGE | ORD | UNO | UEQ | UNE
353 | ULT | UGT | ULE | UGE | TRUETOK | FALSETOK;
354 ShiftOps : SHL | SHR | ASHR | LSHR;
355 CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI |
356 UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
359 // These are some types that allow classification if we only want a particular
360 // thing... for example, only a signed, unsigned, or integral type.
361 SIntType : LONG | INT | SHORT | SBYTE;
362 UIntType : ULONG | UINT | USHORT | UBYTE;
363 IntType : SIntType | UIntType;
364 FPType : FLOAT | DOUBLE;
366 // OptAssign - Value producing statements have an optional assignment component
367 OptAssign : Name '=' {
371 $$ = new std::string("");
375 : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT
377 | /*empty*/ { $$ = new std::string(""); } ;
380 : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK
382 | CC_TOK EUINT64VAL {
387 | /*empty*/ { $$ = new std::string(""); } ;
389 // OptAlign/OptCAlign - An optional alignment, and an optional alignment with
390 // a comma before it.
392 : /*empty*/ { $$ = new std::string(); }
393 | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
396 : /*empty*/ { $$ = new std::string(); }
397 | ',' ALIGN EUINT64VAL {
405 : SECTION STRINGCONSTANT {
411 OptSection : /*empty*/ { $$ = new std::string(); }
415 : /* empty */ { $$ = new std::string(); }
416 | ',' GlobalVarAttribute GlobalVarAttributes {
432 //===----------------------------------------------------------------------===//
433 // Types includes all predefined types... except void, because it can only be
434 // used in specific contexts (function returning void for example). To have
435 // access to it, a user must explicitly use TypesV.
438 // TypesV includes all of 'Types', but it also includes the void type.
439 TypesV : Types | VOID ;
440 UpRTypesV : UpRTypes | VOID ;
443 // Derived types are added later...
445 PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
446 PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
454 $$.oldTy = UnresolvedTy;
459 | '\\' EUINT64VAL { // Type UpReference
462 $$.oldTy = NumericTy;
464 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
465 *$1.newTy += "( " + *$3 + " )";
468 $$.oldTy = FunctionTy;
470 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
472 *$2 += " x " + *$4.newTy + " ]";
476 $$.elemTy = $4.oldTy;
478 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
480 *$2 += " x " + *$4.newTy + " >";
484 $$.elemTy = $4.oldTy;
486 | '{' TypeListI '}' { // Structure type?
492 | '{' '}' { // Empty structure type?
493 $$.newTy = new std::string("{}");
496 | UpRTypes '*' { // Pointer type?
498 $$.elemTy = $1.oldTy;
499 $1.oldTy = PointerTy;
503 // TypeList - Used for struct declarations and as a basis for function type
504 // declaration type lists
510 | TypeListI ',' UpRTypes {
511 *$1 += ", " + *$3.newTy;
516 // ArgTypeList - List of types for a function type declaration...
519 | TypeListI ',' DOTDOTDOT {
528 $$ = new std::string();
531 // ConstVal - The various declarations that go into the constant pool. This
532 // production is used ONLY to represent constants that show up AFTER a 'const',
533 // 'constant' or 'global' token at global scope. Constants that can be inlined
534 // into other expressions (such as integers and constexprs) are handled by the
535 // ResolvedVal, ValueRef and ConstValueRef productions.
537 ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
539 $$.cnst = new std::string(*$1.newTy);
540 *$$.cnst += " [ " + *$3 + " ]";
545 $$.cnst = new std::string(*$1.newTy);
548 | Types 'c' STRINGCONSTANT {
550 $$.cnst = new std::string(*$1.newTy);
551 *$$.cnst += " c" + *$3;
554 | Types '<' ConstVector '>' { // Nonempty unsized arr
556 $$.cnst = new std::string(*$1.newTy);
557 *$$.cnst += " < " + *$3 + " >";
560 | Types '{' ConstVector '}' {
562 $$.cnst = new std::string(*$1.newTy);
563 *$$.cnst += " { " + *$3 + " }";
568 $$.cnst = new std::string(*$1.newTy);
573 $$.cnst = new std::string(*$1.newTy);
574 *$$.cnst += " " + *$2;
579 $$.cnst = new std::string(*$1.newTy);
580 *$$.cnst += " " + *$2;
583 | Types SymbolicValueRef {
585 $$.cnst = new std::string(*$1.newTy);
586 *$$.cnst += " " + *$2;
591 $$.cnst = new std::string(*$1.newTy);
592 *$$.cnst += " " + *$2;
595 | Types ZEROINITIALIZER {
597 $$.cnst = new std::string(*$1.newTy);
598 *$$.cnst += " " + *$2;
601 | SIntType EInt64Val { // integral constants
603 $$.cnst = new std::string(*$1.newTy);
604 *$$.cnst += " " + *$2;
607 | UIntType EUINT64VAL { // integral constants
609 $$.cnst = new std::string(*$1.newTy);
610 *$$.cnst += " " + *$2;
613 | BOOL TRUETOK { // Boolean constants
615 $$.cnst = new std::string(*$1.newTy);
616 *$$.cnst += " " + *$2;
619 | BOOL FALSETOK { // Boolean constants
621 $$.cnst = new std::string(*$1.newTy);
622 *$$.cnst += " " + *$2;
625 | FPType FPVAL { // Float & Double constants
627 $$.cnst = new std::string(*$1.newTy);
628 *$$.cnst += " " + *$2;
633 ConstExpr: CastOps '(' ConstVal TO Types ')' {
634 std::string source = *$3.cnst;
638 // Call getCastUpgrade to upgrade the old cast
639 $$ = new std::string(getCastUpgrade(source, $3.type, $5, true));
641 // Nothing to upgrade, just create the cast constant expr
642 $$ = new std::string(*$1);
643 *$$ += "( " + source + " to " + *$5.newTy + ")";
645 delete $1; $3.destroy(); delete $4; $5.destroy();
647 | GETELEMENTPTR '(' ConstVal IndexList ')' {
648 *$1 += "(" + *$3.cnst;
649 for (unsigned i = 0; i < $4->size(); ++i) {
650 ValueInfo& VI = (*$4)[i];
651 *$1 += ", " + *VI.val;
659 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
660 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
661 $3.destroy(); $5.destroy(); $7.destroy();
664 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
665 const char* op = getDivRemOpcode(*$1, $3.type);
666 $$ = new std::string(op);
667 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
668 delete $1; $3.destroy(); $5.destroy();
670 | LogicalOps '(' ConstVal ',' ConstVal ')' {
671 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
672 $3.destroy(); $5.destroy();
675 | SetCondOps '(' ConstVal ',' ConstVal ')' {
676 #if UPGRADE_SETCOND_OPS
677 *$1 = getCompareOp(*$1, $3.type);
679 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
680 $3.destroy(); $5.destroy();
683 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
684 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
685 delete $2; $4.destroy(); $6.destroy();
688 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
689 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
690 delete $2; $4.destroy(); $6.destroy();
693 | ShiftOps '(' ConstVal ',' ConstVal ')' {
694 const char* shiftop = $1->c_str();
696 shiftop = ($3.type.isUnsigned()) ? "lshr" : "ashr";
697 $$ = new std::string(shiftop);
698 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
699 delete $1; $3.destroy(); $5.destroy();
701 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
702 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
703 $3.destroy(); $5.destroy();
706 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
707 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
708 $3.destroy(); $5.destroy(); $7.destroy();
711 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
712 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
713 $3.destroy(); $5.destroy(); $7.destroy();
718 // ConstVector - A list of comma separated constants.
721 : ConstVector ',' ConstVal {
722 *$1 += ", " + *$3.cnst;
726 | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
730 // GlobalType - Match either GLOBAL or CONSTANT for global declarations...
731 GlobalType : GLOBAL | CONSTANT ;
734 //===----------------------------------------------------------------------===//
735 // Rules to match Modules
736 //===----------------------------------------------------------------------===//
738 // Module rule: Capture the result of parsing the whole file into a result
741 Module : DefinitionList {
744 // DefinitionList - Top level definitions
746 DefinitionList : DefinitionList Function {
749 | DefinitionList FunctionProto {
754 | DefinitionList MODULE ASM_TOK AsmBlock {
755 *O << "module asm " << " " << *$4 << "\n";
758 | DefinitionList IMPLEMENTATION {
759 *O << "implementation\n";
762 | ConstPool { $$ = 0; }
764 External : EXTERNAL | UNINITIALIZED { $$ = $1; *$$ = "external"; }
766 // ConstPool - Constants with optional names assigned to them.
767 ConstPool : ConstPool OptAssign TYPE TypesV {
768 EnumeratedTypes.push_back($4);
770 NamedTypes[*$2].newTy = new std::string(*$4.newTy);
771 NamedTypes[*$2].oldTy = $4.oldTy;
772 NamedTypes[*$2].elemTy = $4.elemTy;
775 *O << "type " << *$4.newTy << "\n";
776 delete $2; delete $3; $4.destroy();
779 | ConstPool FunctionProto { // Function prototypes can be in const pool
784 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
785 *O << *$2 << " " << *$3 << " " << *$4 << "\n";
786 delete $2; delete $3; delete $4;
789 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
792 Globals[*$2] = $5.type.clone();
794 *O << *$3 << " " << *$4 << " " << *$5.cnst << " " << *$6 << "\n";
795 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
798 | ConstPool OptAssign External 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 DLLIMPORT 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 OptAssign EXTERN_WEAK 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 TARGET TargetDefinition {
826 *O << *$2 << " " << *$3 << "\n";
827 delete $2; delete $3;
830 | ConstPool DEPLIBS '=' LibrariesDefinition {
831 *O << *$2 << " = " << *$4 << "\n";
832 delete $2; delete $4;
835 | /* empty: end of list */ {
840 AsmBlock : STRINGCONSTANT ;
842 BigOrLittle : BIG | LITTLE
845 : ENDIAN '=' BigOrLittle {
850 | POINTERSIZE '=' EUINT64VAL {
857 | TRIPLE '=' STRINGCONSTANT {
862 | DATALAYOUT '=' STRINGCONSTANT {
876 : LibList ',' STRINGCONSTANT {
882 | /* empty: end of list */ {
883 $$ = new std::string();
886 //===----------------------------------------------------------------------===//
887 // Rules to match Function Headers
888 //===----------------------------------------------------------------------===//
890 Name : VAR_ID | STRINGCONSTANT;
891 OptName : Name | /*empty*/ { $$ = new std::string(); };
893 ArgVal : Types OptName {
900 ArgListH : ArgListH ',' ArgVal {
911 | ArgListH ',' DOTDOTDOT {
919 | /* empty */ { $$ = new std::string(); };
921 FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
922 OptSection OptAlign {
926 *$1 += *$2.newTy + " " + *$3 + "(" + *$5 + ")";
941 BEGIN : BEGINTOK { $$ = new std::string("{"); delete $1; }
942 | '{' { $$ = new std::string ("{"); }
944 FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
948 *O << *$2 << " " << *$3 << "\n";
949 delete $1; delete $2; delete $3;
953 END : ENDTOK { $$ = new std::string("}"); delete $1; }
954 | '}' { $$ = new std::string("}"); };
956 Function : FunctionHeader BasicBlockList END {
959 *O << '\n' << *$3 << "\n";
964 : /*default*/ { $$ = new std::string(); }
970 : DECLARE FnDeclareLinkage FunctionHeaderH {
979 //===----------------------------------------------------------------------===//
980 // Rules to match Basic Blocks
981 //===----------------------------------------------------------------------===//
983 OptSideEffect : /* empty */ { $$ = new std::string(); }
987 : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK | NULL_TOK | UNDEF
989 | '<' ConstVector '>' {
995 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
999 *$1 += " " + *$3 + ", " + *$5;
1000 delete $2; delete $3; delete $5;
1004 SymbolicValueRef : IntVal | Name ;
1006 // ValueRef - A reference to a definition... either constant or symbolic
1008 : SymbolicValueRef {
1010 $$.constant = false;
1012 $$.type.oldTy = UnresolvedTy;
1018 $$.type.oldTy = UnresolvedTy;
1022 // ResolvedVal - a <type> <value> pair. This is used only in cases where the
1023 // type immediately preceeds the value reference, and allows complex constant
1024 // pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1025 ResolvedVal : Types ValueRef {
1028 $$.val->insert(0, *$1.newTy + " ");
1031 BasicBlockList : BasicBlockList BasicBlock {
1034 | BasicBlock { // Do not allow functions with 0 basic blocks
1039 // Basic blocks are terminated by branching instructions:
1040 // br, br/cc, switch, ret
1042 BasicBlock : InstructionList BBTerminatorInst {
1046 InstructionList : InstructionList Inst {
1047 *O << " " << *$2 << "\n";
1060 Unwind : UNWIND | EXCEPT { $$ = $1; *$$ = "unwind"; }
1062 BBTerminatorInst : RET ResolvedVal { // Return with a result...
1063 *O << " " << *$1 << " " << *$2.val << "\n";
1064 delete $1; $2.destroy();
1067 | RET VOID { // Return with no result...
1068 *O << " " << *$1 << " " << *$2.newTy << "\n";
1069 delete $1; $2.destroy();
1072 | BR LABEL ValueRef { // Unconditional Branch...
1073 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << "\n";
1074 delete $1; $2.destroy(); $3.destroy();
1076 } // Conditional Branch...
1077 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
1078 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1079 << *$5.newTy << " " << *$6.val << ", " << *$8.newTy << " "
1081 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1082 $8.destroy(); $9.destroy();
1085 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1086 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1087 << *$5.newTy << " " << *$6.val << " [" << *$8 << " ]\n";
1088 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1092 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1093 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1094 << *$5.newTy << " " << *$6.val << "[]\n";
1095 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1098 | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
1099 TO LABEL ValueRef Unwind LABEL ValueRef {
1103 *O << *$2 << " " << *$3 << " " << *$4.newTy << " " << *$5.val << " (";
1104 for (unsigned i = 0; i < $7->size(); ++i) {
1105 ValueInfo& VI = (*$7)[i];
1107 if (i+1 < $7->size())
1111 *O << ") " << *$9 << " " << *$10.newTy << " " << *$11.val << " "
1112 << *$12 << " " << *$13.newTy << " " << *$14.val << "\n";
1113 delete $1; delete $2; delete $3; $4.destroy(); $5.destroy(); delete $7;
1114 delete $9; $10.destroy(); $11.destroy(); delete $12; $13.destroy();
1119 *O << " " << *$1 << "\n";
1124 *O << " " << *$1 << "\n";
1129 JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1130 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5.newTy + " " + *$6.val;
1131 $2.destroy(); delete $3; $5.destroy(); $6.destroy();
1134 | IntType ConstValueRef ',' LABEL ValueRef {
1135 $2->insert(0, *$1.newTy + " " );
1136 *$2 += ", " + *$4.newTy + " " + *$5.val;
1137 $1.destroy(); $4.destroy(); $5.destroy();
1142 : OptAssign InstVal {
1151 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1152 $3.val->insert(0, *$1.newTy + "[");
1153 *$3.val += "," + *$5.val + "]";
1154 $1.destroy(); $5.destroy();
1155 $$ = new std::string(*$3.val);
1158 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1159 *$1 += ", [" + *$4.val + "," + *$6.val + "]";
1160 $4.destroy(); $6.destroy();
1167 $$ = new ValueList();
1170 | ValueRefList ',' ResolvedVal {
1175 // ValueRefListE - Just like ValueRefList, except that it may also be empty!
1177 : ValueRefList { $$ = $1; }
1178 | /*empty*/ { $$ = new ValueList(); }
1190 InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1191 const char* op = getDivRemOpcode(*$1, $2);
1192 $$ = new std::string(op);
1193 *$$ += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1194 delete $1; $2.destroy(); $3.destroy(); $5.destroy();
1196 | LogicalOps Types ValueRef ',' ValueRef {
1197 *$1 += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1198 $2.destroy(); $3.destroy(); $5.destroy();
1201 | SetCondOps Types ValueRef ',' ValueRef {
1202 #if UPGRADE_SETCOND_OPS
1203 *$1 = getCompareOp(*$1, $2);
1205 *$1 += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1206 $2.destroy(); $3.destroy(); $5.destroy();
1209 | ICMP IPredicates Types ValueRef ',' ValueRef ')' {
1210 *$1 += " " + *$2 + " " + *$4.val + "," + *$6.val + ")";
1211 delete $2; $4.destroy(); $6.destroy();
1214 | FCMP FPredicates Types ValueRef ',' ValueRef ')' {
1215 *$1 += " " + *$2 + " " + *$4.val + "," + *$6.val + ")";
1216 delete $2; $4.destroy(); $6.destroy();
1220 *$1 += " " + *$2.val;
1224 | ShiftOps ResolvedVal ',' ResolvedVal {
1225 const char* shiftop = $1->c_str();
1227 shiftop = ($2.type.isUnsigned()) ? "lshr" : "ashr";
1228 $$ = new std::string(shiftop);
1229 *$$ += " " + *$2.val + ", " + *$4.val;
1230 delete $1; $2.destroy(); $4.destroy();
1232 | CastOps ResolvedVal TO Types {
1233 std::string source = *$2.val;
1234 TypeInfo SrcTy = $2.type;
1235 TypeInfo DstTy = $4;
1237 $$ = new std::string();
1238 if (*$1 == "cast") {
1239 *$$ += getCastUpgrade(source, SrcTy, DstTy, false);
1241 *$$ += *$1 + " " + source + " to " + *DstTy.newTy;
1243 delete $1; $2.destroy();
1244 delete $3; $4.destroy();
1246 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1247 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1248 $2.destroy(); $4.destroy(); $6.destroy();
1251 | VAARG ResolvedVal ',' Types {
1252 *$1 += " " + *$2.val + ", " + *$4.newTy;
1253 $2.destroy(); $4.destroy();
1256 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
1257 *$1 += " " + *$2.val + ", " + *$4.val;
1258 $2.destroy(); $4.destroy();
1261 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1262 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1263 $2.destroy(); $4.destroy(); $6.destroy();
1266 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1267 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1268 $2.destroy(); $4.destroy(); $6.destroy();
1276 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
1281 *$1 += *$3.newTy + " " + *$4.val + "(";
1282 for (unsigned i = 0; i < $6->size(); ++i) {
1283 ValueInfo& VI = (*$6)[i];
1285 if (i+1 < $6->size())
1290 delete $2; $3.destroy(); $4.destroy(); delete $6;
1296 // IndexList - List of indices for GEP based instructions...
1298 : ',' ValueRefList { $$ = $2; }
1299 | /* empty */ { $$ = new ValueList(); }
1304 | /* empty */ { $$ = new std::string(); }
1307 MemoryInst : MALLOC Types OptCAlign {
1308 *$1 += " " + *$2.newTy;
1311 $2.destroy(); delete $3;
1314 | MALLOC Types ',' UINT ValueRef OptCAlign {
1315 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5.val;
1318 $2.destroy(); $4.destroy(); $5.destroy(); delete $6;
1321 | ALLOCA Types OptCAlign {
1322 *$1 += " " + *$2.newTy;
1325 $2.destroy(); delete $3;
1328 | ALLOCA Types ',' UINT ValueRef OptCAlign {
1329 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5.val;
1332 $2.destroy(); $4.destroy(); $5.destroy(); delete $6;
1335 | FREE ResolvedVal {
1336 *$1 += " " + *$2.val;
1340 | OptVolatile LOAD Types ValueRef {
1343 *$1 += *$2 + " " + *$3.newTy + " " + *$4.val;
1344 delete $2; $3.destroy(); $4.destroy();
1347 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1350 *$1 += *$2 + " " + *$3.val + ", " + *$5.newTy + " " + *$6.val;
1351 delete $2; $3.destroy(); $5.destroy(); $6.destroy();
1354 | GETELEMENTPTR Types ValueRef IndexList {
1355 // Upgrade the indices
1356 for (unsigned i = 0; i < $4->size(); ++i) {
1357 ValueInfo& VI = (*$4)[i];
1358 if (VI.type.isUnsigned() && !VI.isConstant() &&
1359 VI.type.getBitWidth() < 64) {
1360 std::string* old = VI.val;
1361 *O << " %gep_upgrade" << unique << " = zext " << *old
1363 VI.val = new std::string("ulong %gep_upgrade" + llvm::utostr(unique++));
1364 VI.type.oldTy = ULongTy;
1368 *$1 += " " + *$2.newTy + " " + *$3.val;
1369 for (unsigned i = 0; i < $4->size(); ++i) {
1370 ValueInfo& VI = (*$4)[i];
1371 *$1 += ", " + *VI.val;
1374 $2.destroy(); $3.destroy(); delete $4;
1380 int yyerror(const char *ErrorMsg) {
1382 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1383 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
1384 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
1385 if (yychar == YYEMPTY || yychar == 0)
1386 errMsg += "end-of-file.";
1388 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
1389 std::cerr << errMsg << '\n';