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 typedef std::vector<TypeInfo> TypeVector;
38 static TypeVector EnumeratedTypes;
39 typedef std::map<std::string,TypeInfo> TypeMap;
40 static TypeMap NamedTypes;
41 static TypeMap Globals;
43 void destroy(ValueList* VL) {
44 while (!VL->empty()) {
45 ValueInfo& VI = VL->back();
52 void UpgradeAssembly(const std::string &infile, std::istream& in,
53 std::ostream &out, bool debug)
62 std::cerr << "Parse failed.\n";
67 static void ResolveType(TypeInfo& Ty) {
68 if (Ty.oldTy == UnresolvedTy) {
69 TypeMap::iterator I = NamedTypes.find(*Ty.newTy);
70 if (I != NamedTypes.end()) {
71 Ty.oldTy = I->second.oldTy;
72 Ty.elemTy = I->second.elemTy;
74 std::string msg("Can't resolve type: ");
78 } else if (Ty.oldTy == NumericTy) {
79 unsigned ref = atoi(&((Ty.newTy->c_str())[1])); // Skip the '\\'
80 if (ref < EnumeratedTypes.size()) {
81 Ty.oldTy = EnumeratedTypes[ref].oldTy;
82 Ty.elemTy = EnumeratedTypes[ref].elemTy;
84 std::string msg("Can't resolve type: ");
89 // otherwise its already resolved.
92 static const char* getCastOpcode(
93 std::string& Source, const TypeInfo& SrcTy, const TypeInfo& DstTy)
95 unsigned SrcBits = SrcTy.getBitWidth();
96 unsigned DstBits = DstTy.getBitWidth();
97 const char* opcode = "bitcast";
98 // Run through the possibilities ...
99 if (DstTy.isIntegral()) { // Casting to integral
100 if (SrcTy.isIntegral()) { // Casting from integral
101 if (DstBits < SrcBits)
103 else if (DstBits > SrcBits) { // its an extension
104 if (SrcTy.isSigned())
105 opcode ="sext"; // signed -> SEXT
107 opcode = "zext"; // unsigned -> ZEXT
109 opcode = "bitcast"; // Same size, No-op cast
111 } else if (SrcTy.isFloatingPoint()) { // Casting from floating pt
112 if (DstTy.isSigned())
113 opcode = "fptosi"; // FP -> sint
115 opcode = "fptoui"; // FP -> uint
116 } else if (SrcTy.isPacked()) {
117 assert(DstBits == SrcTy.getBitWidth() &&
118 "Casting packed to integer of different width");
119 opcode = "bitcast"; // same size, no-op cast
121 assert(SrcTy.isPointer() &&
122 "Casting from a value that is not first-class type");
123 opcode = "ptrtoint"; // ptr -> int
125 } else if (DstTy.isFloatingPoint()) { // Casting to floating pt
126 if (SrcTy.isIntegral()) { // Casting from integral
127 if (SrcTy.isSigned())
128 opcode = "sitofp"; // sint -> FP
130 opcode = "uitofp"; // uint -> FP
131 } else if (SrcTy.isFloatingPoint()) { // Casting from floating pt
132 if (DstBits < SrcBits) {
133 opcode = "fptrunc"; // FP -> smaller FP
134 } else if (DstBits > SrcBits) {
135 opcode = "fpext"; // FP -> larger FP
137 opcode ="bitcast"; // same size, no-op cast
139 } else if (SrcTy.isPacked()) {
140 assert(DstBits == SrcTy.getBitWidth() &&
141 "Casting packed to floating point of different width");
142 opcode = "bitcast"; // same size, no-op cast
144 assert(0 && "Casting pointer or non-first class to float");
146 } else if (DstTy.isPacked()) {
147 if (SrcTy.isPacked()) {
148 assert(DstTy.getBitWidth() == SrcTy.getBitWidth() &&
149 "Casting packed to packed of different widths");
150 opcode = "bitcast"; // packed -> packed
151 } else if (DstTy.getBitWidth() == SrcBits) {
152 opcode = "bitcast"; // float/int -> packed
154 assert(!"Illegal cast to packed (wrong type or size)");
156 } else if (DstTy.isPointer()) {
157 if (SrcTy.isPointer()) {
158 opcode = "bitcast"; // ptr -> ptr
159 } else if (SrcTy.isIntegral()) {
160 opcode = "inttoptr"; // int -> ptr
162 assert(!"Casting invalid type to pointer");
165 assert(!"Casting to type that is not first-class");
170 static std::string getCastUpgrade(
171 const std::string& Src, TypeInfo& SrcTy, TypeInfo& DstTy, bool isConst)
174 std::string Source = Src;
175 if (SrcTy.isFloatingPoint() && DstTy.isPointer()) {
176 // fp -> ptr cast is no longer supported but we must upgrade this
177 // by doing a double cast: fp -> int -> ptr
179 Source = "ulong fptoui(" + Source + " to ulong)";
181 *O << " %cast_upgrade" << unique << " = fptoui " << Source
183 Source = "ulong %cast_upgrade" + llvm::utostr(unique);
185 // Update the SrcTy for the getCastOpcode call below
187 SrcTy.newTy = new std::string("ulong");
188 SrcTy.oldTy = ULongTy;
189 } else if (DstTy.oldTy == BoolTy && SrcTy.oldTy != BoolTy) {
190 // cast ptr %x to bool was previously defined as setne ptr %x, null
191 // The ptrtoint semantic is to truncate, not compare so we must retain
192 // the original intent by replace the cast with a setne
193 const char* comparator = SrcTy.isPointer() ? ", null" :
194 (SrcTy.isFloatingPoint() ? ", 0.0" : ", 0");
195 const char* compareOp = SrcTy.isFloatingPoint() ? "fcmp one " : "icmp ne ";
197 Result = "(" + Source + comparator + ")";
198 Result = compareOp + Result;
200 Result = compareOp + 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()) {
254 result[5] = 'u'; // NE maps to unordered
256 result[5] = 'o'; // everything else maps to ordered
257 } else if (TI.isIntegral() || TI.isPointer()) {
259 if ((cc1 == 'e' && cc2 == 'q') || (cc1 == 'n' && cc2 == 'e'))
261 else if (TI.isSigned())
263 else if (TI.isUnsigned() || TI.isPointer() || TI.isBool())
266 yyerror("Invalid integral type for setcc");
273 // %file-prefix="UpgradeParser"
283 %token <Type> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
284 %token <Type> FLOAT DOUBLE LABEL
285 %token <String> OPAQUE ESINT64VAL EUINT64VAL SINTVAL UINTVAL FPVAL
286 %token <String> NULL_TOK UNDEF ZEROINITIALIZER TRUETOK FALSETOK
287 %token <String> TYPE VAR_ID LABELSTR STRINGCONSTANT
288 %token <String> IMPLEMENTATION BEGINTOK ENDTOK
289 %token <String> DECLARE GLOBAL CONSTANT SECTION VOLATILE
290 %token <String> TO DOTDOTDOT CONST INTERNAL LINKONCE WEAK
291 %token <String> DLLIMPORT DLLEXPORT EXTERN_WEAK APPENDING
292 %token <String> NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
293 %token <String> ALIGN UNINITIALIZED
294 %token <String> DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
295 %token <String> CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
296 %token <String> X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
297 %token <String> DATALAYOUT
298 %token <String> RET BR SWITCH INVOKE EXCEPT UNWIND UNREACHABLE
299 %token <String> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM AND OR XOR
300 %token <String> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
301 %token <String> ICMP FCMP EQ NE SLT SGT SLE SGE OEQ ONE OLT OGT OLE OGE
302 %token <String> ORD UNO UEQ UNE ULT UGT ULE UGE
303 %token <String> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
304 %token <String> PHI_TOK SELECT SHL SHR ASHR LSHR VAARG
305 %token <String> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
306 %token <String> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI UITOFP SITOFP
307 %token <String> PTRTOINT INTTOPTR BITCAST
309 %type <String> OptAssign OptLinkage OptCallingConv OptAlign OptCAlign
310 %type <String> SectionString OptSection GlobalVarAttributes GlobalVarAttribute
311 %type <String> ArgTypeListI ConstExpr DefinitionList
312 %type <String> ConstPool TargetDefinition LibrariesDefinition LibList OptName
313 %type <String> ArgVal ArgListH ArgList FunctionHeaderH BEGIN FunctionHeader END
314 %type <String> Function FunctionProto BasicBlock TypeListI
315 %type <String> InstructionList BBTerminatorInst JumpTable Inst PHIList
316 %type <String> OptTailCall InstVal OptVolatile Unwind
317 %type <String> MemoryInst SymbolicValueRef OptSideEffect GlobalType
318 %type <String> FnDeclareLinkage BasicBlockList BigOrLittle AsmBlock
319 %type <String> Name ConstValueRef ConstVector External
320 %type <String> ShiftOps SetCondOps LogicalOps ArithmeticOps CastOps
321 %type <String> IPredicates FPredicates
323 %type <ValList> ValueRefList ValueRefListE IndexList
325 %type <Type> IntType SIntType UIntType FPType TypesV Types
326 %type <Type> PrimType UpRTypesV UpRTypes
328 %type <String> IntVal EInt64Val
329 %type <Const> ConstVal
331 %type <Value> ValueRef ResolvedVal
337 // Handle constant integer size restriction and conversion...
338 IntVal : SINTVAL | UINTVAL ;
339 EInt64Val : ESINT64VAL | EUINT64VAL;
341 // Operations that are notably excluded from this list include:
342 // RET, BR, & SWITCH because they end basic blocks and are treated specially.
343 ArithmeticOps: ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV
344 | REM | UREM | SREM | FREM;
345 LogicalOps : AND | OR | XOR;
346 SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
347 IPredicates : EQ | NE | SLT | SGT | SLE | SGE | ULT | UGT | ULE | UGE;
348 FPredicates : OEQ | ONE | OLT | OGT | OLE | OGE | ORD | UNO | UEQ | UNE
349 | ULT | UGT | ULE | UGE | TRUETOK | FALSETOK;
350 ShiftOps : SHL | SHR | ASHR | LSHR;
351 CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI |
352 UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
355 // These are some types that allow classification if we only want a particular
356 // thing... for example, only a signed, unsigned, or integral type.
357 SIntType : LONG | INT | SHORT | SBYTE;
358 UIntType : ULONG | UINT | USHORT | UBYTE;
359 IntType : SIntType | UIntType;
360 FPType : FLOAT | DOUBLE;
362 // OptAssign - Value producing statements have an optional assignment component
363 OptAssign : Name '=' {
367 $$ = new std::string("");
371 : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT
373 | /*empty*/ { $$ = new std::string(""); } ;
376 : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK
378 | CC_TOK EUINT64VAL {
383 | /*empty*/ { $$ = new std::string(""); } ;
385 // OptAlign/OptCAlign - An optional alignment, and an optional alignment with
386 // a comma before it.
388 : /*empty*/ { $$ = new std::string(); }
389 | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
392 : /*empty*/ { $$ = new std::string(); }
393 | ',' ALIGN EUINT64VAL {
401 : SECTION STRINGCONSTANT {
407 OptSection : /*empty*/ { $$ = new std::string(); }
411 : /* empty */ { $$ = new std::string(); }
412 | ',' GlobalVarAttribute GlobalVarAttributes {
428 //===----------------------------------------------------------------------===//
429 // Types includes all predefined types... except void, because it can only be
430 // used in specific contexts (function returning void for example). To have
431 // access to it, a user must explicitly use TypesV.
434 // TypesV includes all of 'Types', but it also includes the void type.
435 TypesV : Types | VOID ;
436 UpRTypesV : UpRTypes | VOID ;
439 // Derived types are added later...
441 PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
442 PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
450 $$.oldTy = UnresolvedTy;
455 | '\\' EUINT64VAL { // Type UpReference
458 $$.oldTy = NumericTy;
460 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
461 *$1.newTy += "( " + *$3 + " )";
464 $$.oldTy = FunctionTy;
466 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
468 *$2 += " x " + *$4.newTy + " ]";
472 $$.elemTy = $4.oldTy;
474 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
476 *$2 += " x " + *$4.newTy + " >";
480 $$.elemTy = $4.oldTy;
482 | '{' TypeListI '}' { // Structure type?
488 | '{' '}' { // Empty structure type?
489 $$.newTy = new std::string("{}");
492 | UpRTypes '*' { // Pointer type?
494 $$.elemTy = $1.oldTy;
495 $1.oldTy = PointerTy;
499 // TypeList - Used for struct declarations and as a basis for function type
500 // declaration type lists
506 | TypeListI ',' UpRTypes {
507 *$1 += ", " + *$3.newTy;
512 // ArgTypeList - List of types for a function type declaration...
515 | TypeListI ',' DOTDOTDOT {
524 $$ = new std::string();
527 // ConstVal - The various declarations that go into the constant pool. This
528 // production is used ONLY to represent constants that show up AFTER a 'const',
529 // 'constant' or 'global' token at global scope. Constants that can be inlined
530 // into other expressions (such as integers and constexprs) are handled by the
531 // ResolvedVal, ValueRef and ConstValueRef productions.
533 ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
535 $$.cnst = new std::string(*$1.newTy);
536 *$$.cnst += " [ " + *$3 + " ]";
541 $$.cnst = new std::string(*$1.newTy);
544 | Types 'c' STRINGCONSTANT {
546 $$.cnst = new std::string(*$1.newTy);
547 *$$.cnst += " c" + *$3;
550 | Types '<' ConstVector '>' { // Nonempty unsized arr
552 $$.cnst = new std::string(*$1.newTy);
553 *$$.cnst += " < " + *$3 + " >";
556 | Types '{' ConstVector '}' {
558 $$.cnst = new std::string(*$1.newTy);
559 *$$.cnst += " { " + *$3 + " }";
564 $$.cnst = new std::string(*$1.newTy);
569 $$.cnst = new std::string(*$1.newTy);
570 *$$.cnst += " " + *$2;
575 $$.cnst = new std::string(*$1.newTy);
576 *$$.cnst += " " + *$2;
579 | Types SymbolicValueRef {
581 $$.cnst = new std::string(*$1.newTy);
582 *$$.cnst += " " + *$2;
587 $$.cnst = new std::string(*$1.newTy);
588 *$$.cnst += " " + *$2;
591 | Types ZEROINITIALIZER {
593 $$.cnst = new std::string(*$1.newTy);
594 *$$.cnst += " " + *$2;
597 | SIntType EInt64Val { // integral constants
599 $$.cnst = new std::string(*$1.newTy);
600 *$$.cnst += " " + *$2;
603 | UIntType EUINT64VAL { // integral constants
605 $$.cnst = new std::string(*$1.newTy);
606 *$$.cnst += " " + *$2;
609 | BOOL TRUETOK { // Boolean constants
611 $$.cnst = new std::string(*$1.newTy);
612 *$$.cnst += " " + *$2;
615 | BOOL FALSETOK { // Boolean constants
617 $$.cnst = new std::string(*$1.newTy);
618 *$$.cnst += " " + *$2;
621 | FPType FPVAL { // Float & Double constants
623 $$.cnst = new std::string(*$1.newTy);
624 *$$.cnst += " " + *$2;
629 ConstExpr: CastOps '(' ConstVal TO Types ')' {
630 std::string source = *$3.cnst;
634 // Call getCastUpgrade to upgrade the old cast
635 $$ = new std::string(getCastUpgrade(source, $3.type, $5, true));
637 // Nothing to upgrade, just create the cast constant expr
638 $$ = new std::string(*$1);
639 *$$ += "( " + source + " to " + *$5.newTy + ")";
641 delete $1; $3.destroy(); delete $4; $5.destroy();
643 | GETELEMENTPTR '(' ConstVal IndexList ')' {
644 *$1 += "(" + *$3.cnst;
645 for (unsigned i = 0; i < $4->size(); ++i) {
646 ValueInfo& VI = (*$4)[i];
647 *$1 += ", " + *VI.val;
655 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
656 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
657 $3.destroy(); $5.destroy(); $7.destroy();
660 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
661 const char* op = getDivRemOpcode(*$1, $3.type);
662 $$ = new std::string(op);
663 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
664 delete $1; $3.destroy(); $5.destroy();
666 | LogicalOps '(' ConstVal ',' ConstVal ')' {
667 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
668 $3.destroy(); $5.destroy();
671 | SetCondOps '(' ConstVal ',' ConstVal ')' {
672 *$1 = getCompareOp(*$1, $3.type);
673 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
674 $3.destroy(); $5.destroy();
677 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
678 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
679 delete $2; $4.destroy(); $6.destroy();
682 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
683 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
684 delete $2; $4.destroy(); $6.destroy();
687 | ShiftOps '(' ConstVal ',' ConstVal ')' {
688 const char* shiftop = $1->c_str();
690 shiftop = ($3.type.isUnsigned()) ? "lshr" : "ashr";
691 $$ = new std::string(shiftop);
692 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
693 delete $1; $3.destroy(); $5.destroy();
695 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
696 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
697 $3.destroy(); $5.destroy();
700 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
701 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
702 $3.destroy(); $5.destroy(); $7.destroy();
705 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
706 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
707 $3.destroy(); $5.destroy(); $7.destroy();
712 // ConstVector - A list of comma separated constants.
715 : ConstVector ',' ConstVal {
716 *$1 += ", " + *$3.cnst;
720 | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
724 // GlobalType - Match either GLOBAL or CONSTANT for global declarations...
725 GlobalType : GLOBAL | CONSTANT ;
728 //===----------------------------------------------------------------------===//
729 // Rules to match Modules
730 //===----------------------------------------------------------------------===//
732 // Module rule: Capture the result of parsing the whole file into a result
735 Module : DefinitionList {
738 // DefinitionList - Top level definitions
740 DefinitionList : DefinitionList Function {
743 | DefinitionList FunctionProto {
748 | DefinitionList MODULE ASM_TOK AsmBlock {
749 *O << "module asm " << " " << *$4 << "\n";
752 | DefinitionList IMPLEMENTATION {
753 *O << "implementation\n";
756 | ConstPool { $$ = 0; }
758 External : EXTERNAL | UNINITIALIZED { $$ = $1; *$$ = "external"; }
760 // ConstPool - Constants with optional names assigned to them.
761 ConstPool : ConstPool OptAssign TYPE TypesV {
762 EnumeratedTypes.push_back($4);
764 NamedTypes[*$2].newTy = new std::string(*$4.newTy);
765 NamedTypes[*$2].oldTy = $4.oldTy;
766 NamedTypes[*$2].elemTy = $4.elemTy;
769 *O << "type " << *$4.newTy << "\n";
770 delete $2; delete $3; $4.destroy();
773 | ConstPool FunctionProto { // Function prototypes can be in const pool
778 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
779 *O << *$2 << " " << *$3 << " " << *$4 << "\n";
780 delete $2; delete $3; delete $4;
783 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
786 Globals[*$2] = $5.type.clone();
788 *O << *$3 << " " << *$4 << " " << *$5.cnst << " " << *$6 << "\n";
789 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
792 | ConstPool OptAssign External GlobalType Types GlobalVarAttributes {
795 Globals[*$2] = $5.clone();
797 *O << *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
798 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
801 | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
804 Globals[*$2] = $5.clone();
806 *O << *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
807 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
810 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
813 Globals[*$2] = $5.clone();
815 *O << *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
816 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
819 | ConstPool TARGET TargetDefinition {
820 *O << *$2 << " " << *$3 << "\n";
821 delete $2; delete $3;
824 | ConstPool DEPLIBS '=' LibrariesDefinition {
825 *O << *$2 << " = " << *$4 << "\n";
826 delete $2; delete $4;
829 | /* empty: end of list */ {
834 AsmBlock : STRINGCONSTANT ;
836 BigOrLittle : BIG | LITTLE
839 : ENDIAN '=' BigOrLittle {
844 | POINTERSIZE '=' EUINT64VAL {
851 | TRIPLE '=' STRINGCONSTANT {
856 | DATALAYOUT '=' STRINGCONSTANT {
870 : LibList ',' STRINGCONSTANT {
876 | /* empty: end of list */ {
877 $$ = new std::string();
880 //===----------------------------------------------------------------------===//
881 // Rules to match Function Headers
882 //===----------------------------------------------------------------------===//
884 Name : VAR_ID | STRINGCONSTANT;
885 OptName : Name | /*empty*/ { $$ = new std::string(); };
887 ArgVal : Types OptName {
894 ArgListH : ArgListH ',' ArgVal {
905 | ArgListH ',' DOTDOTDOT {
913 | /* empty */ { $$ = new std::string(); };
915 FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
916 OptSection OptAlign {
920 *$1 += *$2.newTy + " " + *$3 + "(" + *$5 + ")";
935 BEGIN : BEGINTOK { $$ = new std::string("{"); delete $1; }
936 | '{' { $$ = new std::string ("{"); }
938 FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
942 *O << *$2 << " " << *$3 << "\n";
943 delete $1; delete $2; delete $3;
947 END : ENDTOK { $$ = new std::string("}"); delete $1; }
948 | '}' { $$ = new std::string("}"); };
950 Function : FunctionHeader BasicBlockList END {
953 *O << '\n' << *$3 << "\n";
958 : /*default*/ { $$ = new std::string(); }
964 : DECLARE FnDeclareLinkage FunctionHeaderH {
973 //===----------------------------------------------------------------------===//
974 // Rules to match Basic Blocks
975 //===----------------------------------------------------------------------===//
977 OptSideEffect : /* empty */ { $$ = new std::string(); }
981 : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK | NULL_TOK | UNDEF
983 | '<' ConstVector '>' {
989 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
993 *$1 += " " + *$3 + ", " + *$5;
994 delete $2; delete $3; delete $5;
998 SymbolicValueRef : IntVal | Name ;
1000 // ValueRef - A reference to a definition... either constant or symbolic
1002 : SymbolicValueRef {
1004 $$.constant = false;
1006 $$.type.oldTy = UnresolvedTy;
1012 $$.type.oldTy = UnresolvedTy;
1016 // ResolvedVal - a <type> <value> pair. This is used only in cases where the
1017 // type immediately preceeds the value reference, and allows complex constant
1018 // pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1019 ResolvedVal : Types ValueRef {
1022 $$.val->insert(0, *$1.newTy + " ");
1025 BasicBlockList : BasicBlockList BasicBlock {
1028 | BasicBlock { // Do not allow functions with 0 basic blocks
1033 // Basic blocks are terminated by branching instructions:
1034 // br, br/cc, switch, ret
1036 BasicBlock : InstructionList BBTerminatorInst {
1040 InstructionList : InstructionList Inst {
1041 *O << " " << *$2 << "\n";
1054 Unwind : UNWIND | EXCEPT { $$ = $1; *$$ = "unwind"; }
1056 BBTerminatorInst : RET ResolvedVal { // Return with a result...
1057 *O << " " << *$1 << " " << *$2.val << "\n";
1058 delete $1; $2.destroy();
1061 | RET VOID { // Return with no result...
1062 *O << " " << *$1 << " " << *$2.newTy << "\n";
1063 delete $1; $2.destroy();
1066 | BR LABEL ValueRef { // Unconditional Branch...
1067 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << "\n";
1068 delete $1; $2.destroy(); $3.destroy();
1070 } // Conditional Branch...
1071 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
1072 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1073 << *$5.newTy << " " << *$6.val << ", " << *$8.newTy << " "
1075 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1076 $8.destroy(); $9.destroy();
1079 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1080 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1081 << *$5.newTy << " " << *$6.val << " [" << *$8 << " ]\n";
1082 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1086 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1087 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1088 << *$5.newTy << " " << *$6.val << "[]\n";
1089 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1092 | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
1093 TO LABEL ValueRef Unwind LABEL ValueRef {
1097 *O << *$2 << " " << *$3 << " " << *$4.newTy << " " << *$5.val << " (";
1098 for (unsigned i = 0; i < $7->size(); ++i) {
1099 ValueInfo& VI = (*$7)[i];
1101 if (i+1 < $7->size())
1105 *O << ") " << *$9 << " " << *$10.newTy << " " << *$11.val << " "
1106 << *$12 << " " << *$13.newTy << " " << *$14.val << "\n";
1107 delete $1; delete $2; delete $3; $4.destroy(); $5.destroy(); delete $7;
1108 delete $9; $10.destroy(); $11.destroy(); delete $12; $13.destroy();
1113 *O << " " << *$1 << "\n";
1118 *O << " " << *$1 << "\n";
1123 JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1124 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5.newTy + " " + *$6.val;
1125 $2.destroy(); delete $3; $5.destroy(); $6.destroy();
1128 | IntType ConstValueRef ',' LABEL ValueRef {
1129 $2->insert(0, *$1.newTy + " " );
1130 *$2 += ", " + *$4.newTy + " " + *$5.val;
1131 $1.destroy(); $4.destroy(); $5.destroy();
1136 : OptAssign InstVal {
1145 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1146 $3.val->insert(0, *$1.newTy + "[");
1147 *$3.val += "," + *$5.val + "]";
1148 $1.destroy(); $5.destroy();
1149 $$ = new std::string(*$3.val);
1152 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1153 *$1 += ", [" + *$4.val + "," + *$6.val + "]";
1154 $4.destroy(); $6.destroy();
1161 $$ = new ValueList();
1164 | ValueRefList ',' ResolvedVal {
1169 // ValueRefListE - Just like ValueRefList, except that it may also be empty!
1171 : ValueRefList { $$ = $1; }
1172 | /*empty*/ { $$ = new ValueList(); }
1184 InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1185 const char* op = getDivRemOpcode(*$1, $2);
1186 $$ = new std::string(op);
1187 *$$ += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1188 delete $1; $2.destroy(); $3.destroy(); $5.destroy();
1190 | LogicalOps Types ValueRef ',' ValueRef {
1191 *$1 += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1192 $2.destroy(); $3.destroy(); $5.destroy();
1195 | SetCondOps Types ValueRef ',' ValueRef {
1196 *$1 = getCompareOp(*$1, $2);
1197 *$1 += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1198 $2.destroy(); $3.destroy(); $5.destroy();
1201 | ICMP IPredicates Types ValueRef ',' ValueRef ')' {
1202 *$1 += " " + *$2 + " " + *$4.val + "," + *$6.val + ")";
1203 delete $2; $4.destroy(); $6.destroy();
1206 | FCMP FPredicates Types ValueRef ',' ValueRef ')' {
1207 *$1 += " " + *$2 + " " + *$4.val + "," + *$6.val + ")";
1208 delete $2; $4.destroy(); $6.destroy();
1212 *$1 += " " + *$2.val;
1216 | ShiftOps ResolvedVal ',' ResolvedVal {
1217 const char* shiftop = $1->c_str();
1219 shiftop = ($2.type.isUnsigned()) ? "lshr" : "ashr";
1220 $$ = new std::string(shiftop);
1221 *$$ += " " + *$2.val + ", " + *$4.val;
1222 delete $1; $2.destroy(); $4.destroy();
1224 | CastOps ResolvedVal TO Types {
1225 std::string source = *$2.val;
1226 TypeInfo SrcTy = $2.type;
1227 TypeInfo DstTy = $4;
1229 $$ = new std::string();
1230 if (*$1 == "cast") {
1231 *$$ += getCastUpgrade(source, SrcTy, DstTy, false);
1233 *$$ += *$1 + " " + source + " to " + *DstTy.newTy;
1235 delete $1; $2.destroy();
1236 delete $3; $4.destroy();
1238 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1239 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1240 $2.destroy(); $4.destroy(); $6.destroy();
1243 | VAARG ResolvedVal ',' Types {
1244 *$1 += " " + *$2.val + ", " + *$4.newTy;
1245 $2.destroy(); $4.destroy();
1248 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
1249 *$1 += " " + *$2.val + ", " + *$4.val;
1250 $2.destroy(); $4.destroy();
1253 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1254 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1255 $2.destroy(); $4.destroy(); $6.destroy();
1258 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1259 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1260 $2.destroy(); $4.destroy(); $6.destroy();
1268 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
1273 *$1 += *$3.newTy + " " + *$4.val + "(";
1274 for (unsigned i = 0; i < $6->size(); ++i) {
1275 ValueInfo& VI = (*$6)[i];
1277 if (i+1 < $6->size())
1282 delete $2; $3.destroy(); $4.destroy(); delete $6;
1288 // IndexList - List of indices for GEP based instructions...
1290 : ',' ValueRefList { $$ = $2; }
1291 | /* empty */ { $$ = new ValueList(); }
1296 | /* empty */ { $$ = new std::string(); }
1299 MemoryInst : MALLOC Types OptCAlign {
1300 *$1 += " " + *$2.newTy;
1303 $2.destroy(); delete $3;
1306 | MALLOC Types ',' UINT ValueRef OptCAlign {
1307 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5.val;
1310 $2.destroy(); $4.destroy(); $5.destroy(); delete $6;
1313 | ALLOCA Types OptCAlign {
1314 *$1 += " " + *$2.newTy;
1317 $2.destroy(); delete $3;
1320 | ALLOCA Types ',' UINT ValueRef OptCAlign {
1321 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5.val;
1324 $2.destroy(); $4.destroy(); $5.destroy(); delete $6;
1327 | FREE ResolvedVal {
1328 *$1 += " " + *$2.val;
1332 | OptVolatile LOAD Types ValueRef {
1335 *$1 += *$2 + " " + *$3.newTy + " " + *$4.val;
1336 delete $2; $3.destroy(); $4.destroy();
1339 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1342 *$1 += *$2 + " " + *$3.val + ", " + *$5.newTy + " " + *$6.val;
1343 delete $2; $3.destroy(); $5.destroy(); $6.destroy();
1346 | GETELEMENTPTR Types ValueRef IndexList {
1347 // Upgrade the indices
1348 for (unsigned i = 0; i < $4->size(); ++i) {
1349 ValueInfo& VI = (*$4)[i];
1350 if (VI.type.isUnsigned() && !VI.isConstant() &&
1351 VI.type.getBitWidth() < 64) {
1352 std::string* old = VI.val;
1353 *O << " %gep_upgrade" << unique << " = zext " << *old
1355 VI.val = new std::string("ulong %gep_upgrade" + llvm::utostr(unique++));
1356 VI.type.oldTy = ULongTy;
1360 *$1 += " " + *$2.newTy + " " + *$3.val;
1361 for (unsigned i = 0; i < $4->size(); ++i) {
1362 ValueInfo& VI = (*$4)[i];
1363 *$1 += ", " + *VI.val;
1366 $2.destroy(); $3.destroy(); delete $4;
1372 int yyerror(const char *ErrorMsg) {
1374 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1375 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
1376 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
1377 if (yychar == YYEMPTY || yychar == 0)
1378 errMsg += "end-of-file.";
1380 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
1381 std::cerr << errMsg << '\n';