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 DEFINE 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 | '<' '{' TypeListI '}' '>' { // Packed Structure type?
493 $3->insert(0, "<{ ");
498 | '<' '{' '}' '>' { // Empty packed structure type?
499 $$.newTy = new std::string("<{}>");
502 | UpRTypes '*' { // Pointer type?
504 $$.elemTy = $1.oldTy;
505 $1.oldTy = PointerTy;
509 // TypeList - Used for struct declarations and as a basis for function type
510 // declaration type lists
516 | TypeListI ',' UpRTypes {
517 *$1 += ", " + *$3.newTy;
522 // ArgTypeList - List of types for a function type declaration...
525 | TypeListI ',' DOTDOTDOT {
534 $$ = new std::string();
537 // ConstVal - The various declarations that go into the constant pool. This
538 // production is used ONLY to represent constants that show up AFTER a 'const',
539 // 'constant' or 'global' token at global scope. Constants that can be inlined
540 // into other expressions (such as integers and constexprs) are handled by the
541 // ResolvedVal, ValueRef and ConstValueRef productions.
543 ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
545 $$.cnst = new std::string(*$1.newTy);
546 *$$.cnst += " [ " + *$3 + " ]";
551 $$.cnst = new std::string(*$1.newTy);
554 | Types 'c' STRINGCONSTANT {
556 $$.cnst = new std::string(*$1.newTy);
557 *$$.cnst += " c" + *$3;
560 | Types '<' ConstVector '>' { // Nonempty unsized arr
562 $$.cnst = new std::string(*$1.newTy);
563 *$$.cnst += " < " + *$3 + " >";
566 | Types '{' ConstVector '}' {
568 $$.cnst = new std::string(*$1.newTy);
569 *$$.cnst += " { " + *$3 + " }";
574 $$.cnst = new std::string(*$1.newTy);
579 $$.cnst = new std::string(*$1.newTy);
580 *$$.cnst += " " + *$2;
585 $$.cnst = new std::string(*$1.newTy);
586 *$$.cnst += " " + *$2;
589 | Types SymbolicValueRef {
591 $$.cnst = new std::string(*$1.newTy);
592 *$$.cnst += " " + *$2;
597 $$.cnst = new std::string(*$1.newTy);
598 *$$.cnst += " " + *$2;
601 | Types ZEROINITIALIZER {
603 $$.cnst = new std::string(*$1.newTy);
604 *$$.cnst += " " + *$2;
607 | SIntType EInt64Val { // integral constants
609 $$.cnst = new std::string(*$1.newTy);
610 *$$.cnst += " " + *$2;
613 | UIntType EUINT64VAL { // integral constants
615 $$.cnst = new std::string(*$1.newTy);
616 *$$.cnst += " " + *$2;
619 | BOOL TRUETOK { // Boolean constants
621 $$.cnst = new std::string(*$1.newTy);
622 *$$.cnst += " " + *$2;
625 | BOOL FALSETOK { // Boolean constants
627 $$.cnst = new std::string(*$1.newTy);
628 *$$.cnst += " " + *$2;
631 | FPType FPVAL { // Float & Double constants
633 $$.cnst = new std::string(*$1.newTy);
634 *$$.cnst += " " + *$2;
639 ConstExpr: CastOps '(' ConstVal TO Types ')' {
640 std::string source = *$3.cnst;
644 // Call getCastUpgrade to upgrade the old cast
645 $$ = new std::string(getCastUpgrade(source, $3.type, $5, true));
647 // Nothing to upgrade, just create the cast constant expr
648 $$ = new std::string(*$1);
649 *$$ += "( " + source + " to " + *$5.newTy + ")";
651 delete $1; $3.destroy(); delete $4; $5.destroy();
653 | GETELEMENTPTR '(' ConstVal IndexList ')' {
654 *$1 += "(" + *$3.cnst;
655 for (unsigned i = 0; i < $4->size(); ++i) {
656 ValueInfo& VI = (*$4)[i];
657 *$1 += ", " + *VI.val;
665 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
666 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
667 $3.destroy(); $5.destroy(); $7.destroy();
670 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
671 const char* op = getDivRemOpcode(*$1, $3.type);
672 $$ = new std::string(op);
673 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
674 delete $1; $3.destroy(); $5.destroy();
676 | LogicalOps '(' ConstVal ',' ConstVal ')' {
677 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
678 $3.destroy(); $5.destroy();
681 | SetCondOps '(' ConstVal ',' ConstVal ')' {
682 *$1 = getCompareOp(*$1, $3.type);
683 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
684 $3.destroy(); $5.destroy();
687 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
688 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
689 delete $2; $4.destroy(); $6.destroy();
692 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
693 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
694 delete $2; $4.destroy(); $6.destroy();
697 | ShiftOps '(' ConstVal ',' ConstVal ')' {
698 const char* shiftop = $1->c_str();
700 shiftop = ($3.type.isUnsigned()) ? "lshr" : "ashr";
701 $$ = new std::string(shiftop);
702 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
703 delete $1; $3.destroy(); $5.destroy();
705 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
706 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
707 $3.destroy(); $5.destroy();
710 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
711 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
712 $3.destroy(); $5.destroy(); $7.destroy();
715 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
716 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
717 $3.destroy(); $5.destroy(); $7.destroy();
722 // ConstVector - A list of comma separated constants.
725 : ConstVector ',' ConstVal {
726 *$1 += ", " + *$3.cnst;
730 | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
734 // GlobalType - Match either GLOBAL or CONSTANT for global declarations...
735 GlobalType : GLOBAL | CONSTANT ;
738 //===----------------------------------------------------------------------===//
739 // Rules to match Modules
740 //===----------------------------------------------------------------------===//
742 // Module rule: Capture the result of parsing the whole file into a result
745 Module : DefinitionList {
748 // DefinitionList - Top level definitions
750 DefinitionList : DefinitionList Function {
753 | DefinitionList FunctionProto {
758 | DefinitionList MODULE ASM_TOK AsmBlock {
759 *O << "module asm " << ' ' << *$4 << '\n';
762 | DefinitionList IMPLEMENTATION {
763 *O << "implementation\n";
766 | ConstPool { $$ = 0; }
768 External : EXTERNAL | UNINITIALIZED { $$ = $1; *$$ = "external"; }
770 // ConstPool - Constants with optional names assigned to them.
771 ConstPool : ConstPool OptAssign TYPE TypesV {
772 EnumeratedTypes.push_back($4);
774 NamedTypes[*$2].newTy = new std::string(*$4.newTy);
775 NamedTypes[*$2].oldTy = $4.oldTy;
776 NamedTypes[*$2].elemTy = $4.elemTy;
779 *O << "type " << *$4.newTy << '\n';
780 delete $2; delete $3; $4.destroy();
783 | ConstPool FunctionProto { // Function prototypes can be in const pool
788 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
789 *O << *$2 << ' ' << *$3 << ' ' << *$4 << '\n';
790 delete $2; delete $3; delete $4;
793 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
796 Globals[*$2] = $5.type.clone();
798 *O << *$3 << ' ' << *$4 << ' ' << *$5.cnst << ' ' << *$6 << '\n';
799 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
802 | ConstPool OptAssign External GlobalType Types GlobalVarAttributes {
805 Globals[*$2] = $5.clone();
807 *O << *$3 << ' ' << *$4 << ' ' << *$5.newTy << ' ' << *$6 << '\n';
808 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
811 | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
814 Globals[*$2] = $5.clone();
816 *O << *$3 << ' ' << *$4 << ' ' << *$5.newTy << ' ' << *$6 << '\n';
817 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
820 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
823 Globals[*$2] = $5.clone();
825 *O << *$3 << ' ' << *$4 << ' ' << *$5.newTy << ' ' << *$6 << '\n';
826 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
829 | ConstPool TARGET TargetDefinition {
830 *O << *$2 << ' ' << *$3 << '\n';
831 delete $2; delete $3;
834 | ConstPool DEPLIBS '=' LibrariesDefinition {
835 *O << *$2 << " = " << *$4 << '\n';
836 delete $2; delete $4;
839 | /* empty: end of list */ {
844 AsmBlock : STRINGCONSTANT ;
846 BigOrLittle : BIG | LITTLE
849 : ENDIAN '=' BigOrLittle {
854 | POINTERSIZE '=' EUINT64VAL {
861 | TRIPLE '=' STRINGCONSTANT {
866 | DATALAYOUT '=' STRINGCONSTANT {
880 : LibList ',' STRINGCONSTANT {
886 | /* empty: end of list */ {
887 $$ = new std::string();
890 //===----------------------------------------------------------------------===//
891 // Rules to match Function Headers
892 //===----------------------------------------------------------------------===//
894 Name : VAR_ID | STRINGCONSTANT;
895 OptName : Name | /*empty*/ { $$ = new std::string(); };
897 ArgVal : Types OptName {
904 ArgListH : ArgListH ',' ArgVal {
915 | ArgListH ',' DOTDOTDOT {
923 | /* empty */ { $$ = new std::string(); };
925 FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
926 OptSection OptAlign {
930 *$1 += *$2.newTy + " " + *$3 + "(" + *$5 + ")";
945 BEGIN : BEGINTOK { $$ = new std::string("{"); delete $1; }
946 | '{' { $$ = new std::string ("{"); }
949 : OptLinkage FunctionHeaderH BEGIN {
954 *O << *$2 << ' ' << *$3 << '\n';
955 delete $1; delete $2; delete $3;
958 | DEFINE OptLinkage FunctionHeaderH BEGIN {
963 *O << *$3 << ' ' << *$4 << '\n';
964 delete $1; delete $2; delete $3; delete $4;
969 END : ENDTOK { $$ = new std::string("}"); delete $1; }
970 | '}' { $$ = new std::string("}"); };
972 Function : FunctionHeader BasicBlockList END {
975 *O << '\n' << *$3 << '\n';
980 : /*default*/ { $$ = new std::string(); }
986 : DECLARE FnDeclareLinkage FunctionHeaderH {
995 //===----------------------------------------------------------------------===//
996 // Rules to match Basic Blocks
997 //===----------------------------------------------------------------------===//
999 OptSideEffect : /* empty */ { $$ = new std::string(); }
1003 : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK | NULL_TOK | UNDEF
1005 | '<' ConstVector '>' {
1011 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
1015 *$1 += " " + *$3 + ", " + *$5;
1016 delete $2; delete $3; delete $5;
1020 SymbolicValueRef : IntVal | Name ;
1022 // ValueRef - A reference to a definition... either constant or symbolic
1024 : SymbolicValueRef {
1026 $$.constant = false;
1028 $$.type.oldTy = UnresolvedTy;
1034 $$.type.oldTy = UnresolvedTy;
1038 // ResolvedVal - a <type> <value> pair. This is used only in cases where the
1039 // type immediately preceeds the value reference, and allows complex constant
1040 // pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1041 ResolvedVal : Types ValueRef {
1044 $$.val->insert(0, *$1.newTy + " ");
1047 BasicBlockList : BasicBlockList BasicBlock {
1050 | BasicBlock { // Do not allow functions with 0 basic blocks
1055 // Basic blocks are terminated by branching instructions:
1056 // br, br/cc, switch, ret
1058 BasicBlock : InstructionList BBTerminatorInst {
1062 InstructionList : InstructionList Inst {
1063 *O << " " << *$2 << '\n';
1076 Unwind : UNWIND | EXCEPT { $$ = $1; *$$ = "unwind"; }
1078 BBTerminatorInst : RET ResolvedVal { // Return with a result...
1079 *O << " " << *$1 << ' ' << *$2.val << '\n';
1080 delete $1; $2.destroy();
1083 | RET VOID { // Return with no result...
1084 *O << " " << *$1 << ' ' << *$2.newTy << '\n';
1085 delete $1; $2.destroy();
1088 | BR LABEL ValueRef { // Unconditional Branch...
1089 *O << " " << *$1 << ' ' << *$2.newTy << ' ' << *$3.val << '\n';
1090 delete $1; $2.destroy(); $3.destroy();
1092 } // Conditional Branch...
1093 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
1094 *O << " " << *$1 << ' ' << *$2.newTy << ' ' << *$3.val << ", "
1095 << *$5.newTy << ' ' << *$6.val << ", " << *$8.newTy << ' '
1097 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1098 $8.destroy(); $9.destroy();
1101 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1102 *O << " " << *$1 << ' ' << *$2.newTy << ' ' << *$3.val << ", "
1103 << *$5.newTy << ' ' << *$6.val << " [" << *$8 << " ]\n";
1104 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1108 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1109 *O << " " << *$1 << ' ' << *$2.newTy << ' ' << *$3.val << ", "
1110 << *$5.newTy << ' ' << *$6.val << "[]\n";
1111 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1114 | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
1115 TO LABEL ValueRef Unwind LABEL ValueRef {
1119 *O << *$2 << ' ' << *$3 << ' ' << *$4.newTy << ' ' << *$5.val << " (";
1120 for (unsigned i = 0; i < $7->size(); ++i) {
1121 ValueInfo& VI = (*$7)[i];
1123 if (i+1 < $7->size())
1127 *O << ") " << *$9 << ' ' << *$10.newTy << ' ' << *$11.val << ' '
1128 << *$12 << ' ' << *$13.newTy << ' ' << *$14.val << '\n';
1129 delete $1; delete $2; delete $3; $4.destroy(); $5.destroy(); delete $7;
1130 delete $9; $10.destroy(); $11.destroy(); delete $12; $13.destroy();
1135 *O << " " << *$1 << '\n';
1140 *O << " " << *$1 << '\n';
1145 JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1146 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5.newTy + " " + *$6.val;
1147 $2.destroy(); delete $3; $5.destroy(); $6.destroy();
1150 | IntType ConstValueRef ',' LABEL ValueRef {
1151 $2->insert(0, *$1.newTy + " " );
1152 *$2 += ", " + *$4.newTy + " " + *$5.val;
1153 $1.destroy(); $4.destroy(); $5.destroy();
1158 : OptAssign InstVal {
1167 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1168 $3.val->insert(0, *$1.newTy + "[");
1169 *$3.val += "," + *$5.val + "]";
1170 $1.destroy(); $5.destroy();
1171 $$ = new std::string(*$3.val);
1174 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1175 *$1 += ", [" + *$4.val + "," + *$6.val + "]";
1176 $4.destroy(); $6.destroy();
1183 $$ = new ValueList();
1186 | ValueRefList ',' ResolvedVal {
1191 // ValueRefListE - Just like ValueRefList, except that it may also be empty!
1193 : ValueRefList { $$ = $1; }
1194 | /*empty*/ { $$ = new ValueList(); }
1206 InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1207 const char* op = getDivRemOpcode(*$1, $2);
1208 $$ = new std::string(op);
1209 *$$ += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1210 delete $1; $2.destroy(); $3.destroy(); $5.destroy();
1212 | LogicalOps Types ValueRef ',' ValueRef {
1213 *$1 += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1214 $2.destroy(); $3.destroy(); $5.destroy();
1217 | SetCondOps Types ValueRef ',' ValueRef {
1218 *$1 = getCompareOp(*$1, $2);
1219 *$1 += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1220 $2.destroy(); $3.destroy(); $5.destroy();
1223 | ICMP IPredicates Types ValueRef ',' ValueRef {
1224 *$1 += " " + *$2 + " " + *$3.newTy + " " + *$4.val + "," + *$6.val;
1225 delete $2; $4.destroy(); $6.destroy();
1228 | FCMP FPredicates Types ValueRef ',' ValueRef {
1229 *$1 += " " + *$2 + " " + *$3.newTy + " " + *$4.val + "," + *$6.val;
1230 delete $2; $4.destroy(); $6.destroy();
1234 *$1 += " " + *$2.val;
1238 | ShiftOps ResolvedVal ',' ResolvedVal {
1239 const char* shiftop = $1->c_str();
1241 shiftop = ($2.type.isUnsigned()) ? "lshr" : "ashr";
1242 $$ = new std::string(shiftop);
1243 *$$ += " " + *$2.val + ", " + *$4.val;
1244 delete $1; $2.destroy(); $4.destroy();
1246 | CastOps ResolvedVal TO Types {
1247 std::string source = *$2.val;
1248 TypeInfo SrcTy = $2.type;
1249 TypeInfo DstTy = $4;
1251 $$ = new std::string();
1252 if (*$1 == "cast") {
1253 *$$ += getCastUpgrade(source, SrcTy, DstTy, false);
1255 *$$ += *$1 + " " + source + " to " + *DstTy.newTy;
1257 delete $1; $2.destroy();
1258 delete $3; $4.destroy();
1260 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1261 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1262 $2.destroy(); $4.destroy(); $6.destroy();
1265 | VAARG ResolvedVal ',' Types {
1266 *$1 += " " + *$2.val + ", " + *$4.newTy;
1267 $2.destroy(); $4.destroy();
1270 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
1271 *$1 += " " + *$2.val + ", " + *$4.val;
1272 $2.destroy(); $4.destroy();
1275 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1276 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1277 $2.destroy(); $4.destroy(); $6.destroy();
1280 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1281 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1282 $2.destroy(); $4.destroy(); $6.destroy();
1290 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
1295 *$1 += *$3.newTy + " " + *$4.val + "(";
1296 for (unsigned i = 0; i < $6->size(); ++i) {
1297 ValueInfo& VI = (*$6)[i];
1299 if (i+1 < $6->size())
1304 delete $2; $3.destroy(); $4.destroy(); delete $6;
1310 // IndexList - List of indices for GEP based instructions...
1312 : ',' ValueRefList { $$ = $2; }
1313 | /* empty */ { $$ = new ValueList(); }
1318 | /* empty */ { $$ = new std::string(); }
1321 MemoryInst : MALLOC Types OptCAlign {
1322 *$1 += " " + *$2.newTy;
1325 $2.destroy(); delete $3;
1328 | MALLOC Types ',' UINT ValueRef OptCAlign {
1329 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5.val;
1332 $2.destroy(); $4.destroy(); $5.destroy(); delete $6;
1335 | ALLOCA Types OptCAlign {
1336 *$1 += " " + *$2.newTy;
1339 $2.destroy(); delete $3;
1342 | ALLOCA Types ',' UINT ValueRef OptCAlign {
1343 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5.val;
1346 $2.destroy(); $4.destroy(); $5.destroy(); delete $6;
1349 | FREE ResolvedVal {
1350 *$1 += " " + *$2.val;
1354 | OptVolatile LOAD Types ValueRef {
1357 *$1 += *$2 + " " + *$3.newTy + " " + *$4.val;
1358 delete $2; $3.destroy(); $4.destroy();
1361 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1364 *$1 += *$2 + " " + *$3.val + ", " + *$5.newTy + " " + *$6.val;
1365 delete $2; $3.destroy(); $5.destroy(); $6.destroy();
1368 | GETELEMENTPTR Types ValueRef IndexList {
1369 // Upgrade the indices
1370 for (unsigned i = 0; i < $4->size(); ++i) {
1371 ValueInfo& VI = (*$4)[i];
1372 if (VI.type.isUnsigned() && !VI.isConstant() &&
1373 VI.type.getBitWidth() < 64) {
1374 std::string* old = VI.val;
1375 *O << " %gep_upgrade" << unique << " = zext " << *old
1377 VI.val = new std::string("ulong %gep_upgrade" + llvm::utostr(unique++));
1378 VI.type.oldTy = ULongTy;
1382 *$1 += " " + *$2.newTy + " " + *$3.val;
1383 for (unsigned i = 0; i < $4->size(); ++i) {
1384 ValueInfo& VI = (*$4)[i];
1385 *$1 += ", " + *VI.val;
1388 $2.destroy(); $3.destroy(); delete $4;
1394 int yyerror(const char *ErrorMsg) {
1396 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1397 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
1398 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
1399 if (yychar == YYEMPTY || yychar == 0)
1400 errMsg += "end-of-file.";
1402 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
1403 std::cerr << errMsg << '\n';