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"
21 #define YYERROR_VERBOSE 1
22 #define YYINCLUDED_STDLIB_H
25 int yylex(); // declaration" of xxx warnings.
29 static std::string CurFilename;
30 static std::ostream *O = 0;
31 std::istream* LexInput = 0;
32 unsigned SizeOfPointer = 32;
33 static uint64_t unique = 1;
35 // This bool controls whether attributes are ever added to function declarations
36 // definitions and calls.
37 static bool AddAttributes = false;
39 // This bool is used to communicate between the InstVal and Inst rules about
40 // whether or not a cast should be deleted. When the flag is set, InstVal has
41 // determined that the cast is a candidate. However, it can only be deleted if
42 // the value being casted is the same value name as the instruction. The Inst
43 // rule makes that comparison if the flag is set and comments out the
44 // instruction if they match.
45 static bool deleteUselessCastFlag = false;
46 static std::string* deleteUselessCastName = 0;
48 typedef std::vector<TypeInfo> TypeVector;
49 static TypeVector EnumeratedTypes;
50 typedef std::map<std::string,TypeInfo> TypeMap;
51 static TypeMap NamedTypes;
52 static TypeMap Globals;
54 void destroy(ValueList* VL) {
55 while (!VL->empty()) {
56 ValueInfo& VI = VL->back();
63 void UpgradeAssembly(const std::string &infile, std::istream& in,
64 std::ostream &out, bool debug, bool addAttrs)
70 AddAttributes = addAttrs;
74 std::cerr << "Parse failed.\n";
79 TypeInfo* ResolveType(TypeInfo*& Ty) {
80 if (Ty->isUnresolved()) {
81 TypeMap::iterator I = NamedTypes.find(Ty->getNewTy());
82 if (I != NamedTypes.end()) {
83 Ty = I->second.clone();
86 std::string msg("Cannot resolve type: ");
87 msg += Ty->getNewTy();
90 } else if (Ty->isNumeric()) {
91 unsigned ref = atoi(&((Ty->getNewTy().c_str())[1])); // Skip the '\\'
92 if (ref < EnumeratedTypes.size()) {
93 Ty = EnumeratedTypes[ref].clone();
96 std::string msg("Can't resolve type: ");
97 msg += Ty->getNewTy();
101 // otherwise its already resolved.
105 static const char* getCastOpcode(
106 std::string& Source, const TypeInfo* SrcTy, const TypeInfo* DstTy)
108 unsigned SrcBits = SrcTy->getBitWidth();
109 unsigned DstBits = DstTy->getBitWidth();
110 const char* opcode = "bitcast";
111 // Run through the possibilities ...
112 if (DstTy->isIntegral()) { // Casting to integral
113 if (SrcTy->isIntegral()) { // Casting from integral
114 if (DstBits < SrcBits)
116 else if (DstBits > SrcBits) { // its an extension
117 if (SrcTy->isSigned())
118 opcode ="sext"; // signed -> SEXT
120 opcode = "zext"; // unsigned -> ZEXT
122 opcode = "bitcast"; // Same size, No-op cast
124 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
125 if (DstTy->isSigned())
126 opcode = "fptosi"; // FP -> sint
128 opcode = "fptoui"; // FP -> uint
129 } else if (SrcTy->isPacked()) {
130 assert(DstBits == SrcTy->getBitWidth() &&
131 "Casting packed to integer of different width");
132 opcode = "bitcast"; // same size, no-op cast
134 assert(SrcTy->isPointer() &&
135 "Casting from a value that is not first-class type");
136 opcode = "ptrtoint"; // ptr -> int
138 } else if (DstTy->isFloatingPoint()) { // Casting to floating pt
139 if (SrcTy->isIntegral()) { // Casting from integral
140 if (SrcTy->isSigned())
141 opcode = "sitofp"; // sint -> FP
143 opcode = "uitofp"; // uint -> FP
144 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
145 if (DstBits < SrcBits) {
146 opcode = "fptrunc"; // FP -> smaller FP
147 } else if (DstBits > SrcBits) {
148 opcode = "fpext"; // FP -> larger FP
150 opcode ="bitcast"; // same size, no-op cast
152 } else if (SrcTy->isPacked()) {
153 assert(DstBits == SrcTy->getBitWidth() &&
154 "Casting packed to floating point of different width");
155 opcode = "bitcast"; // same size, no-op cast
157 assert(0 && "Casting pointer or non-first class to float");
159 } else if (DstTy->isPacked()) {
160 if (SrcTy->isPacked()) {
161 assert(DstTy->getBitWidth() == SrcTy->getBitWidth() &&
162 "Casting packed to packed of different widths");
163 opcode = "bitcast"; // packed -> packed
164 } else if (DstTy->getBitWidth() == SrcBits) {
165 opcode = "bitcast"; // float/int -> packed
167 assert(!"Illegal cast to packed (wrong type or size)");
169 } else if (DstTy->isPointer()) {
170 if (SrcTy->isPointer()) {
171 opcode = "bitcast"; // ptr -> ptr
172 } else if (SrcTy->isIntegral()) {
173 opcode = "inttoptr"; // int -> ptr
175 assert(!"Casting invalid type to pointer");
178 assert(!"Casting to type that is not first-class");
183 static std::string getCastUpgrade(const std::string& Src, TypeInfo* SrcTy,
184 TypeInfo* DstTy, bool isConst)
187 std::string Source = Src;
188 if (SrcTy->isFloatingPoint() && DstTy->isPointer()) {
189 // fp -> ptr cast is no longer supported but we must upgrade this
190 // by doing a double cast: fp -> int -> ptr
192 Source = "i64 fptoui(" + Source + " to i64)";
194 *O << " %cast_upgrade" << unique << " = fptoui " << Source
196 Source = "i64 %cast_upgrade" + llvm::utostr(unique);
198 // Update the SrcTy for the getCastOpcode call below
200 SrcTy = new TypeInfo("i64", ULongTy);
201 } else if (DstTy->isBool()) {
202 // cast type %x to bool was previously defined as setne type %x, null
203 // The cast semantic is now to truncate, not compare so we must retain
204 // the original intent by replacing the cast with a setne
205 const char* comparator = SrcTy->isPointer() ? ", null" :
206 (SrcTy->isFloatingPoint() ? ", 0.0" :
207 (SrcTy->isBool() ? ", false" : ", 0"));
208 const char* compareOp = SrcTy->isFloatingPoint() ? "fcmp one " : "icmp ne ";
210 Result = "(" + Source + comparator + ")";
211 Result = compareOp + Result;
213 Result = compareOp + Source + comparator;
214 return Result; // skip cast processing below
218 std::string Opcode(getCastOpcode(Source, SrcTy, DstTy));
220 Result += Opcode + "( " + Source + " to " + DstTy->getNewTy() + ")";
222 Result += Opcode + " " + Source + " to " + DstTy->getNewTy();
226 const char* getDivRemOpcode(const std::string& opcode, TypeInfo* TI) {
227 const char* op = opcode.c_str();
228 const TypeInfo* Ty = ResolveType(TI);
230 Ty = Ty->getElementType();
232 if (Ty->isFloatingPoint())
234 else if (Ty->isUnsigned())
236 else if (Ty->isSigned())
239 yyerror("Invalid type for div instruction");
240 else if (opcode == "rem")
241 if (Ty->isFloatingPoint())
243 else if (Ty->isUnsigned())
245 else if (Ty->isSigned())
248 yyerror("Invalid type for rem instruction");
253 getCompareOp(const std::string& setcc, const TypeInfo* TI) {
254 assert(setcc.length() == 5);
257 assert(cc1 == 'e' || cc1 == 'n' || cc1 == 'l' || cc1 == 'g');
258 assert(cc2 == 'q' || cc2 == 'e' || cc2 == 'e' || cc2 == 't');
259 std::string result("xcmp xxx");
262 if (TI->isFloatingPoint()) {
266 result[5] = 'u'; // NE maps to unordered
268 result[5] = 'o'; // everything else maps to ordered
269 } else if (TI->isIntegral() || TI->isPointer()) {
271 if ((cc1 == 'e' && cc2 == 'q') || (cc1 == 'n' && cc2 == 'e'))
273 else if (TI->isSigned())
275 else if (TI->isUnsigned() || TI->isPointer() || TI->isBool())
278 yyerror("Invalid integral type for setcc");
283 static TypeInfo* getFunctionReturnType(TypeInfo* PFTy) {
285 if (PFTy->isPointer()) {
286 TypeInfo* ElemTy = PFTy->getElementType();
288 if (ElemTy->isFunction())
289 return ElemTy->getResultType()->clone();
290 } else if (PFTy->isFunction()) {
291 return PFTy->getResultType()->clone();
293 return PFTy->clone();
296 static TypeInfo* getGEPIndexedType(TypeInfo* PTy, ValueList* idxs) {
298 assert(PTy->isPointer() && "GEP Operand is not a pointer?");
299 TypeInfo* Result = PTy->getElementType(); // just skip first index
301 for (unsigned i = 1; i < idxs->size(); ++i) {
302 if (Result->isComposite()) {
303 Result = Result->getIndexedType((*idxs)[i]);
306 yyerror("Invalid type for index");
308 return Result->getPointerType();
311 static std::string makeUniqueName(const std::string *Name, bool isSigned) {
312 const char *suffix = ".u";
315 if ((*Name)[Name->size()-1] == '"') {
316 std::string Result(*Name);
317 Result.insert(Name->size()-1, suffix);
320 return *Name + suffix;
323 // This function handles appending .u or .s to integer value names that
324 // were previously unsigned or signed, respectively. This avoids name
325 // collisions since the unsigned and signed type planes have collapsed
326 // into a single signless type plane.
327 static std::string getUniqueName(const std::string *Name, TypeInfo* Ty) {
328 // If its not a symbolic name, don't modify it, probably a constant val.
329 if ((*Name)[0] != '%' && (*Name)[0] != '"')
331 // If its a numeric reference, just leave it alone.
332 if (isdigit((*Name)[1]))
338 // Default the result to the current name
339 std::string Result = *Name;
341 if (Ty->isInteger()) {
342 // If its an integer type, make the name unique
343 Result = makeUniqueName(Name, Ty->isSigned());
344 } else if (Ty->isPointer()) {
345 while (Ty->isPointer())
346 Ty = Ty->getElementType();
348 Result = makeUniqueName(Name, Ty->isSigned());
355 // %file-prefix="UpgradeParser"
366 %token <Type> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
367 %token <Type> FLOAT DOUBLE LABEL
368 %token <String> OPAQUE ESINT64VAL EUINT64VAL SINTVAL UINTVAL FPVAL
369 %token <String> NULL_TOK UNDEF ZEROINITIALIZER TRUETOK FALSETOK
370 %token <String> TYPE VAR_ID LABELSTR STRINGCONSTANT
371 %token <String> IMPLEMENTATION BEGINTOK ENDTOK
372 %token <String> DECLARE GLOBAL CONSTANT SECTION VOLATILE
373 %token <String> TO DOTDOTDOT CONST INTERNAL LINKONCE WEAK
374 %token <String> DLLIMPORT DLLEXPORT EXTERN_WEAK APPENDING
375 %token <String> NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
376 %token <String> ALIGN UNINITIALIZED
377 %token <String> DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
378 %token <String> CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
379 %token <String> X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
380 %token <String> DATALAYOUT
381 %token <String> RET BR SWITCH INVOKE EXCEPT UNWIND UNREACHABLE
382 %token <String> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM AND OR XOR
383 %token <String> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
384 %token <String> ICMP FCMP EQ NE SLT SGT SLE SGE OEQ ONE OLT OGT OLE OGE
385 %token <String> ORD UNO UEQ UNE ULT UGT ULE UGE
386 %token <String> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
387 %token <String> PHI_TOK SELECT SHL SHR ASHR LSHR VAARG
388 %token <String> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
389 %token <String> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI UITOFP SITOFP
390 %token <String> PTRTOINT INTTOPTR BITCAST
392 %type <String> OptAssign OptLinkage OptCallingConv OptAlign OptCAlign
393 %type <String> SectionString OptSection GlobalVarAttributes GlobalVarAttribute
394 %type <String> ConstExpr DefinitionList
395 %type <String> ConstPool TargetDefinition LibrariesDefinition LibList OptName
396 %type <String> ArgVal ArgListH ArgList FunctionHeaderH BEGIN FunctionHeader END
397 %type <String> Function FunctionProto BasicBlock
398 %type <String> InstructionList BBTerminatorInst JumpTable Inst
399 %type <String> OptTailCall OptVolatile Unwind
400 %type <String> SymbolicValueRef OptSideEffect GlobalType
401 %type <String> FnDeclareLinkage BasicBlockList BigOrLittle AsmBlock
402 %type <String> Name ConstValueRef ConstVector External
403 %type <String> ShiftOps SetCondOps LogicalOps ArithmeticOps CastOps
404 %type <String> IPredicates FPredicates
406 %type <ValList> ValueRefList ValueRefListE IndexList
407 %type <TypeVec> TypeListI ArgTypeListI
409 %type <Type> IntType SIntType UIntType FPType TypesV Types
410 %type <Type> PrimType UpRTypesV UpRTypes
412 %type <String> IntVal EInt64Val
413 %type <Const> ConstVal
415 %type <Value> ValueRef ResolvedVal InstVal PHIList MemoryInst
421 // Handle constant integer size restriction and conversion...
422 IntVal : SINTVAL | UINTVAL ;
423 EInt64Val : ESINT64VAL | EUINT64VAL;
425 // Operations that are notably excluded from this list include:
426 // RET, BR, & SWITCH because they end basic blocks and are treated specially.
427 ArithmeticOps: ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV
428 | REM | UREM | SREM | FREM;
429 LogicalOps : AND | OR | XOR;
430 SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
431 IPredicates : EQ | NE | SLT | SGT | SLE | SGE | ULT | UGT | ULE | UGE;
432 FPredicates : OEQ | ONE | OLT | OGT | OLE | OGE | ORD | UNO | UEQ | UNE
433 | ULT | UGT | ULE | UGE | TRUETOK | FALSETOK;
434 ShiftOps : SHL | SHR | ASHR | LSHR;
435 CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI |
436 UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
439 // These are some types that allow classification if we only want a particular
440 // thing... for example, only a signed, unsigned, or integral type.
441 SIntType : LONG | INT | SHORT | SBYTE;
442 UIntType : ULONG | UINT | USHORT | UBYTE;
443 IntType : SIntType | UIntType;
444 FPType : FLOAT | DOUBLE;
446 // OptAssign - Value producing statements have an optional assignment component
447 OptAssign : Name '=' {
451 $$ = new std::string("");
455 : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT
457 | /*empty*/ { $$ = new std::string(""); } ;
460 : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK
462 | CC_TOK EUINT64VAL {
467 | /*empty*/ { $$ = new std::string(""); } ;
469 // OptAlign/OptCAlign - An optional alignment, and an optional alignment with
470 // a comma before it.
472 : /*empty*/ { $$ = new std::string(); }
473 | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
476 : /*empty*/ { $$ = new std::string(); }
477 | ',' ALIGN EUINT64VAL {
485 : SECTION STRINGCONSTANT {
491 OptSection : /*empty*/ { $$ = new std::string(); }
495 : /* empty */ { $$ = new std::string(); }
496 | ',' GlobalVarAttribute GlobalVarAttributes {
512 //===----------------------------------------------------------------------===//
513 // Types includes all predefined types... except void, because it can only be
514 // used in specific contexts (function returning void for example). To have
515 // access to it, a user must explicitly use TypesV.
518 // TypesV includes all of 'Types', but it also includes the void type.
519 TypesV : Types | VOID ;
520 UpRTypesV : UpRTypes | VOID ;
523 // Derived types are added later...
525 PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
526 PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
529 $$ = new TypeInfo($1, OpaqueTy);
532 $$ = new TypeInfo($1, UnresolvedTy);
537 | '\\' EUINT64VAL { // Type UpReference
539 $$ = new TypeInfo($2, NumericTy);
541 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
542 std::string newTy( $1->getNewTy() + "(");
543 for (unsigned i = 0; i < $3->size(); ++i) {
546 if ((*$3)[i]->isVoid())
549 newTy += (*$3)[i]->getNewTy();
552 $$ = new TypeInfo(new std::string(newTy), $1, $3);
553 EnumeratedTypes.push_back(*$$);
555 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
557 *$2 += " x " + $4->getNewTy() + " ]";
558 uint64_t elems = atoi($2->c_str());
559 $$ = new TypeInfo($2, ArrayTy, $4, elems);
560 EnumeratedTypes.push_back(*$$);
562 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
564 *$2 += " x " + $4->getNewTy() + " >";
565 uint64_t elems = atoi($2->c_str());
566 $$ = new TypeInfo($2, PackedTy, $4, elems);
567 EnumeratedTypes.push_back(*$$);
569 | '{' TypeListI '}' { // Structure type?
570 std::string newTy("{");
571 for (unsigned i = 0; i < $2->size(); ++i) {
574 newTy += (*$2)[i]->getNewTy();
577 $$ = new TypeInfo(new std::string(newTy), StructTy, $2);
578 EnumeratedTypes.push_back(*$$);
580 | '{' '}' { // Empty structure type?
581 $$ = new TypeInfo(new std::string("{}"), StructTy, new TypeList());
582 EnumeratedTypes.push_back(*$$);
584 | '<' '{' TypeListI '}' '>' { // Packed Structure type?
585 std::string newTy("<{");
586 for (unsigned i = 0; i < $3->size(); ++i) {
589 newTy += (*$3)[i]->getNewTy();
592 $$ = new TypeInfo(new std::string(newTy), PackedStructTy, $3);
593 EnumeratedTypes.push_back(*$$);
595 | '<' '{' '}' '>' { // Empty packed structure type?
596 $$ = new TypeInfo(new std::string("<{}>"), PackedStructTy, new TypeList());
597 EnumeratedTypes.push_back(*$$);
599 | UpRTypes '*' { // Pointer type?
600 $$ = $1->getPointerType();
601 EnumeratedTypes.push_back(*$$);
604 // TypeList - Used for struct declarations and as a basis for function type
605 // declaration type lists
612 | TypeListI ',' UpRTypes {
617 // ArgTypeList - List of types for a function type declaration...
620 | TypeListI ',' DOTDOTDOT {
622 $$->push_back(new TypeInfo("void",VoidTy));
627 $$->push_back(new TypeInfo("void",VoidTy));
634 // ConstVal - The various declarations that go into the constant pool. This
635 // production is used ONLY to represent constants that show up AFTER a 'const',
636 // 'constant' or 'global' token at global scope. Constants that can be inlined
637 // into other expressions (such as integers and constexprs) are handled by the
638 // ResolvedVal, ValueRef and ConstValueRef productions.
640 ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
642 $$.cnst = new std::string($1->getNewTy());
643 *$$.cnst += " [ " + *$3 + " ]";
648 $$.cnst = new std::string($1->getNewTy());
651 | Types 'c' STRINGCONSTANT {
653 $$.cnst = new std::string($1->getNewTy());
654 *$$.cnst += " c" + *$3;
657 | Types '<' ConstVector '>' { // Nonempty unsized arr
659 $$.cnst = new std::string($1->getNewTy());
660 *$$.cnst += " < " + *$3 + " >";
663 | Types '{' ConstVector '}' {
665 $$.cnst = new std::string($1->getNewTy());
666 *$$.cnst += " { " + *$3 + " }";
671 $$.cnst = new std::string($1->getNewTy());
676 $$.cnst = new std::string($1->getNewTy());
677 *$$.cnst += " " + *$2;
682 $$.cnst = new std::string($1->getNewTy());
683 *$$.cnst += " " + *$2;
686 | Types SymbolicValueRef {
687 std::string Name = getUniqueName($2,$1);
689 $$.cnst = new std::string($1->getNewTy());
690 *$$.cnst += " " + Name;
695 $$.cnst = new std::string($1->getNewTy());
696 *$$.cnst += " " + *$2;
699 | Types ZEROINITIALIZER {
701 $$.cnst = new std::string($1->getNewTy());
702 *$$.cnst += " " + *$2;
705 | SIntType EInt64Val { // integral constants
707 $$.cnst = new std::string($1->getNewTy());
708 *$$.cnst += " " + *$2;
711 | UIntType EUINT64VAL { // integral constants
713 $$.cnst = new std::string($1->getNewTy());
714 *$$.cnst += " " + *$2;
717 | BOOL TRUETOK { // Boolean constants
719 $$.cnst = new std::string($1->getNewTy());
720 *$$.cnst += " " + *$2;
723 | BOOL FALSETOK { // Boolean constants
725 $$.cnst = new std::string($1->getNewTy());
726 *$$.cnst += " " + *$2;
729 | FPType FPVAL { // Float & Double constants
731 $$.cnst = new std::string($1->getNewTy());
732 *$$.cnst += " " + *$2;
737 ConstExpr: CastOps '(' ConstVal TO Types ')' {
738 std::string source = *$3.cnst;
739 TypeInfo* DstTy = ResolveType($5);
741 // Call getCastUpgrade to upgrade the old cast
742 $$ = new std::string(getCastUpgrade(source, $3.type, DstTy, true));
744 // Nothing to upgrade, just create the cast constant expr
745 $$ = new std::string(*$1);
746 *$$ += "( " + source + " to " + $5->getNewTy() + ")";
748 delete $1; $3.destroy(); delete $4; delete $5;
750 | GETELEMENTPTR '(' ConstVal IndexList ')' {
751 *$1 += "(" + *$3.cnst;
752 for (unsigned i = 0; i < $4->size(); ++i) {
753 ValueInfo& VI = (*$4)[i];
754 *$1 += ", " + *VI.val;
762 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
763 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
764 $3.destroy(); $5.destroy(); $7.destroy();
767 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
768 const char* op = getDivRemOpcode(*$1, $3.type);
769 $$ = new std::string(op);
770 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
771 delete $1; $3.destroy(); $5.destroy();
773 | LogicalOps '(' ConstVal ',' ConstVal ')' {
774 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
775 $3.destroy(); $5.destroy();
778 | SetCondOps '(' ConstVal ',' ConstVal ')' {
779 *$1 = getCompareOp(*$1, $3.type);
780 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
781 $3.destroy(); $5.destroy();
784 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
785 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
786 delete $2; $4.destroy(); $6.destroy();
789 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
790 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
791 delete $2; $4.destroy(); $6.destroy();
794 | ShiftOps '(' ConstVal ',' ConstVal ')' {
795 const char* shiftop = $1->c_str();
797 shiftop = ($3.type->isUnsigned()) ? "lshr" : "ashr";
798 $$ = new std::string(shiftop);
799 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
800 delete $1; $3.destroy(); $5.destroy();
802 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
803 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
804 $3.destroy(); $5.destroy();
807 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
808 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
809 $3.destroy(); $5.destroy(); $7.destroy();
812 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
813 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
814 $3.destroy(); $5.destroy(); $7.destroy();
819 // ConstVector - A list of comma separated constants.
822 : ConstVector ',' ConstVal {
823 *$1 += ", " + *$3.cnst;
827 | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
831 // GlobalType - Match either GLOBAL or CONSTANT for global declarations...
832 GlobalType : GLOBAL | CONSTANT ;
835 //===----------------------------------------------------------------------===//
836 // Rules to match Modules
837 //===----------------------------------------------------------------------===//
839 // Module rule: Capture the result of parsing the whole file into a result
842 Module : DefinitionList {
845 // DefinitionList - Top level definitions
847 DefinitionList : DefinitionList Function {
850 | DefinitionList FunctionProto {
855 | DefinitionList MODULE ASM_TOK AsmBlock {
856 *O << "module asm " << ' ' << *$4 << '\n';
859 | DefinitionList IMPLEMENTATION {
860 *O << "implementation\n";
863 | ConstPool { $$ = 0; }
865 External : EXTERNAL | UNINITIALIZED { $$ = $1; *$$ = "external"; }
867 // ConstPool - Constants with optional names assigned to them.
868 ConstPool : ConstPool OptAssign TYPE TypesV {
869 EnumeratedTypes.push_back(*$4);
871 NamedTypes[*$2] = *$4;
874 *O << "type " << $4->getNewTy() << '\n';
875 delete $2; delete $3;
878 | ConstPool FunctionProto { // Function prototypes can be in const pool
883 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
884 *O << *$2 << ' ' << *$3 << ' ' << *$4 << '\n';
885 delete $2; delete $3; delete $4;
888 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
890 std::string Name = getUniqueName($2,$5.type);
892 Globals[Name] = *$5.type;
894 *O << *$3 << ' ' << *$4 << ' ' << *$5.cnst << ' ' << *$6 << '\n';
895 delete $2; delete $3; delete $4; delete $6;
898 | ConstPool OptAssign External GlobalType Types GlobalVarAttributes {
900 std::string Name = getUniqueName($2,$5);
904 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
905 delete $2; delete $3; delete $4; delete $6;
908 | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
910 std::string Name = getUniqueName($2,$5);
914 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
915 delete $2; delete $3; delete $4; delete $6;
918 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
920 std::string Name = getUniqueName($2,$5);
924 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
925 delete $2; delete $3; delete $4; delete $6;
928 | ConstPool TARGET TargetDefinition {
929 *O << *$2 << ' ' << *$3 << '\n';
930 delete $2; delete $3;
933 | ConstPool DEPLIBS '=' LibrariesDefinition {
934 *O << *$2 << " = " << *$4 << '\n';
935 delete $2; delete $4;
938 | /* empty: end of list */ {
943 AsmBlock : STRINGCONSTANT ;
945 BigOrLittle : BIG | LITTLE
948 : ENDIAN '=' BigOrLittle {
953 | POINTERSIZE '=' EUINT64VAL {
960 | TRIPLE '=' STRINGCONSTANT {
965 | DATALAYOUT '=' STRINGCONSTANT {
979 : LibList ',' STRINGCONSTANT {
985 | /* empty: end of list */ {
986 $$ = new std::string();
989 //===----------------------------------------------------------------------===//
990 // Rules to match Function Headers
991 //===----------------------------------------------------------------------===//
993 Name : VAR_ID | STRINGCONSTANT;
994 OptName : Name | /*empty*/ { $$ = new std::string(); };
996 ArgVal : Types OptName {
997 $$ = new std::string($1->getNewTy());
999 std::string Name = getUniqueName($2, $1);
1005 ArgListH : ArgListH ',' ArgVal {
1013 ArgList : ArgListH {
1016 | ArgListH ',' DOTDOTDOT {
1024 | /* empty */ { $$ = new std::string(); };
1027 : OptCallingConv TypesV Name '(' ArgList ')' OptSection OptAlign {
1031 *$1 += $2->getNewTy() + " " + *$3 + "(" + *$5 + ")";
1045 BEGIN : BEGINTOK { $$ = new std::string("{"); delete $1; }
1046 | '{' { $$ = new std::string ("{"); }
1049 : OptLinkage FunctionHeaderH BEGIN {
1054 *O << *$2 << ' ' << *$3 << '\n';
1055 delete $1; delete $2; delete $3;
1060 END : ENDTOK { $$ = new std::string("}"); delete $1; }
1061 | '}' { $$ = new std::string("}"); };
1063 Function : FunctionHeader BasicBlockList END {
1066 *O << *$3 << "\n\n";
1067 delete $1; delete $2; delete $3;
1072 : /*default*/ { $$ = new std::string(); }
1078 : DECLARE FnDeclareLinkage FunctionHeaderH {
1087 //===----------------------------------------------------------------------===//
1088 // Rules to match Basic Blocks
1089 //===----------------------------------------------------------------------===//
1091 OptSideEffect : /* empty */ { $$ = new std::string(); }
1095 : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK | NULL_TOK | UNDEF
1097 | '<' ConstVector '>' {
1103 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
1107 *$1 += " " + *$3 + ", " + *$5;
1108 delete $2; delete $3; delete $5;
1112 SymbolicValueRef : IntVal | Name ;
1114 // ValueRef - A reference to a definition... either constant or symbolic
1116 : SymbolicValueRef {
1118 $$.constant = false;
1119 $$.type = new TypeInfo();
1124 $$.type = new TypeInfo();
1128 // ResolvedVal - a <type> <value> pair. This is used only in cases where the
1129 // type immediately preceeds the value reference, and allows complex constant
1130 // pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1131 ResolvedVal : Types ValueRef {
1132 std::string Name = getUniqueName($2.val, $1);
1136 $$.val = new std::string($1->getNewTy() + " " + Name);
1140 BasicBlockList : BasicBlockList BasicBlock {
1143 | BasicBlock { // Do not allow functions with 0 basic blocks
1148 // Basic blocks are terminated by branching instructions:
1149 // br, br/cc, switch, ret
1151 BasicBlock : InstructionList BBTerminatorInst {
1155 InstructionList : InstructionList Inst {
1156 *O << " " << *$2 << '\n';
1169 Unwind : UNWIND | EXCEPT { $$ = $1; *$$ = "unwind"; }
1171 BBTerminatorInst : RET ResolvedVal { // Return with a result...
1172 *O << " " << *$1 << ' ' << *$2.val << '\n';
1173 delete $1; $2.destroy();
1176 | RET VOID { // Return with no result...
1177 *O << " " << *$1 << ' ' << $2->getNewTy() << '\n';
1178 delete $1; delete $2;
1181 | BR LABEL ValueRef { // Unconditional Branch...
1182 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << *$3.val << '\n';
1183 delete $1; delete $2; $3.destroy();
1185 } // Conditional Branch...
1186 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
1187 std::string Name = getUniqueName($3.val, $2);
1188 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", "
1189 << $5->getNewTy() << ' ' << *$6.val << ", " << $8->getNewTy() << ' '
1191 delete $1; delete $2; $3.destroy(); delete $5; $6.destroy();
1192 delete $8; $9.destroy();
1195 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1196 std::string Name = getUniqueName($3.val, $2);
1197 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", "
1198 << $5->getNewTy() << ' ' << *$6.val << " [" << *$8 << " ]\n";
1199 delete $1; delete $2; $3.destroy(); delete $5; $6.destroy();
1203 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1204 std::string Name = getUniqueName($3.val, $2);
1205 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", "
1206 << $5->getNewTy() << ' ' << *$6.val << "[]\n";
1207 delete $1; delete $2; $3.destroy(); delete $5; $6.destroy();
1210 | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
1211 TO LABEL ValueRef Unwind LABEL ValueRef {
1212 TypeInfo* ResTy = getFunctionReturnType($4);
1215 std::string Name = getUniqueName($1, ResTy);
1216 *O << Name << " = ";
1218 *O << *$2 << ' ' << *$3 << ' ' << $4->getNewTy() << ' ' << *$5.val << " (";
1219 for (unsigned i = 0; i < $7->size(); ++i) {
1220 ValueInfo& VI = (*$7)[i];
1222 if (i+1 < $7->size())
1226 *O << ") " << *$9 << ' ' << $10->getNewTy() << ' ' << *$11.val << ' '
1227 << *$12 << ' ' << $13->getNewTy() << ' ' << *$14.val << '\n';
1228 delete $1; delete $2; delete $3; delete $4; $5.destroy(); delete $7;
1229 delete $9; delete $10; $11.destroy(); delete $12; delete $13;
1234 *O << " " << *$1 << '\n';
1239 *O << " " << *$1 << '\n';
1244 JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1245 *$1 += " " + $2->getNewTy() + " " + *$3 + ", " + $5->getNewTy() + " " +
1247 delete $2; delete $3; delete $5; $6.destroy();
1250 | IntType ConstValueRef ',' LABEL ValueRef {
1251 $2->insert(0, $1->getNewTy() + " " );
1252 *$2 += ", " + $4->getNewTy() + " " + *$5.val;
1253 delete $1; delete $4; $5.destroy();
1258 : OptAssign InstVal {
1260 if (deleteUselessCastFlag && *deleteUselessCastName == *$1) {
1262 $1->insert(0, "; "); // don't actually delete it, just comment it out
1263 delete deleteUselessCastName;
1265 // Get a unique name for the name of this value, based on its type.
1266 *$1 = getUniqueName($1, $2.type) + " = ";
1271 deleteUselessCastFlag = false;
1276 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1277 std::string Name = getUniqueName($3.val, $1);
1278 Name.insert(0, $1->getNewTy() + "[");
1279 Name += "," + *$5.val + "]";
1280 $$.val = new std::string(Name);
1282 $3.destroy(); $5.destroy();
1284 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1285 std::string Name = getUniqueName($4.val, $1.type);
1286 *$1.val += ", [" + Name + "," + *$6.val + "]";
1287 $4.destroy(); $6.destroy();
1294 $$ = new ValueList();
1297 | ValueRefList ',' ResolvedVal {
1302 // ValueRefListE - Just like ValueRefList, except that it may also be empty!
1304 : ValueRefList { $$ = $1; }
1305 | /*empty*/ { $$ = new ValueList(); }
1317 InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1318 const char* op = getDivRemOpcode(*$1, $2);
1319 std::string Name1 = getUniqueName($3.val, $2);
1320 std::string Name2 = getUniqueName($5.val, $2);
1321 $$.val = new std::string(op);
1322 *$$.val += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
1324 delete $1; $3.destroy(); $5.destroy();
1326 | LogicalOps Types ValueRef ',' ValueRef {
1327 std::string Name1 = getUniqueName($3.val, $2);
1328 std::string Name2 = getUniqueName($5.val, $2);
1329 *$1 += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
1332 $3.destroy(); $5.destroy();
1334 | SetCondOps Types ValueRef ',' ValueRef {
1335 std::string Name1 = getUniqueName($3.val, $2);
1336 std::string Name2 = getUniqueName($5.val, $2);
1337 *$1 = getCompareOp(*$1, $2);
1338 *$1 += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
1340 $$.type = new TypeInfo("bool",BoolTy);
1341 $3.destroy(); $5.destroy();
1343 | ICMP IPredicates Types ValueRef ',' ValueRef {
1344 std::string Name1 = getUniqueName($4.val, $3);
1345 std::string Name2 = getUniqueName($6.val, $3);
1346 *$1 += " " + *$2 + " " + $3->getNewTy() + " " + Name1 + "," + Name2;
1348 $$.type = new TypeInfo("bool",BoolTy);
1349 delete $2; $4.destroy(); $6.destroy();
1351 | FCMP FPredicates Types ValueRef ',' ValueRef {
1352 std::string Name1 = getUniqueName($4.val, $3);
1353 std::string Name2 = getUniqueName($6.val, $3);
1354 *$1 += " " + *$2 + " " + $3->getNewTy() + " " + Name1 + "," + Name2;
1356 $$.type = new TypeInfo("bool",BoolTy);
1357 delete $2; $4.destroy(); $6.destroy();
1361 $$.val->insert(0, *$1 + " ");
1364 | ShiftOps ResolvedVal ',' ResolvedVal {
1365 const char* shiftop = $1->c_str();
1367 shiftop = ($2.type->isUnsigned()) ? "lshr" : "ashr";
1368 $$.val = new std::string(shiftop);
1369 *$$.val += " " + *$2.val + ", " + *$4.val;
1371 delete $1; delete $2.val; $4.destroy();
1373 | CastOps ResolvedVal TO Types {
1374 std::string source = *$2.val;
1375 TypeInfo* SrcTy = $2.type;
1376 TypeInfo* DstTy = ResolveType($4);
1377 $$.val = new std::string();
1378 if (*$1 == "cast") {
1379 *$$.val += getCastUpgrade(source, SrcTy, DstTy, false);
1381 *$$.val += *$1 + " " + source + " to " + DstTy->getNewTy();
1384 // Check to see if this is a useless cast of a value to the same name
1385 // and the same type. Such casts will probably cause redefinition errors
1386 // when assembled and perform no code gen action so just remove them.
1387 if (*$1 == "cast" || *$1 == "bitcast")
1388 if ($2.type->isInteger() && DstTy->isInteger() &&
1389 $2.type->getBitWidth() == DstTy->getBitWidth()) {
1390 deleteUselessCastFlag = true; // Flag the "Inst" rule
1391 deleteUselessCastName = new std::string(*$2.val); // save the name
1392 size_t pos = deleteUselessCastName->find_first_of("%\"",0);
1393 if (pos != std::string::npos) {
1394 // remove the type portion before val
1395 deleteUselessCastName->erase(0, pos);
1398 delete $1; $2.destroy();
1401 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1402 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1405 $2.destroy(); delete $4.val; $6.destroy();
1407 | VAARG ResolvedVal ',' Types {
1408 *$1 += " " + *$2.val + ", " + $4->getNewTy();
1413 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
1414 *$1 += " " + *$2.val + ", " + *$4.val;
1416 ResolveType($2.type);
1417 $$.type = $2.type->getElementType()->clone();
1418 delete $2.val; $4.destroy();
1420 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1421 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1424 delete $2.val; $4.destroy(); $6.destroy();
1426 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1427 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1430 delete $2.val; $4.destroy(); $6.destroy();
1433 *$1 += " " + *$2.val;
1438 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
1443 *$1 += $3->getNewTy() + " " + *$4.val + "(";
1444 for (unsigned i = 0; i < $6->size(); ++i) {
1445 ValueInfo& VI = (*$6)[i];
1447 if (i+1 < $6->size())
1453 $$.type = getFunctionReturnType($3);
1454 delete $2; delete $3; $4.destroy(); delete $6;
1459 // IndexList - List of indices for GEP based instructions...
1461 : ',' ValueRefList { $$ = $2; }
1462 | /* empty */ { $$ = new ValueList(); }
1467 | /* empty */ { $$ = new std::string(); }
1470 MemoryInst : MALLOC Types OptCAlign {
1471 *$1 += " " + $2->getNewTy();
1475 $$.type = $2->getPointerType();
1476 delete $2; delete $3;
1478 | MALLOC Types ',' UINT ValueRef OptCAlign {
1479 std::string Name = getUniqueName($5.val, $4);
1480 *$1 += " " + $2->getNewTy() + ", " + $4->getNewTy() + " " + Name;
1484 $$.type = $2->getPointerType();
1485 delete $2; delete $4; $5.destroy(); delete $6;
1487 | ALLOCA Types OptCAlign {
1488 *$1 += " " + $2->getNewTy();
1492 $$.type = $2->getPointerType();
1493 delete $2; delete $3;
1495 | ALLOCA Types ',' UINT ValueRef OptCAlign {
1496 std::string Name = getUniqueName($5.val, $4);
1497 *$1 += " " + $2->getNewTy() + ", " + $4->getNewTy() + " " + Name;
1501 $$.type = $2->getPointerType();
1502 delete $2; delete $4; $5.destroy(); delete $6;
1504 | FREE ResolvedVal {
1505 *$1 += " " + *$2.val;
1507 $$.type = new TypeInfo("void", VoidTy);
1510 | OptVolatile LOAD Types ValueRef {
1511 std::string Name = getUniqueName($4.val, $3);
1514 *$1 += *$2 + " " + $3->getNewTy() + " " + Name;
1516 $$.type = $3->getElementType()->clone();
1517 delete $2; delete $3; $4.destroy();
1519 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1520 std::string Name = getUniqueName($6.val, $5);
1523 *$1 += *$2 + " " + *$3.val + ", " + $5->getNewTy() + " " + Name;
1525 $$.type = new TypeInfo("void", VoidTy);
1526 delete $2; $3.destroy(); delete $5; $6.destroy();
1528 | GETELEMENTPTR Types ValueRef IndexList {
1529 std::string Name = getUniqueName($3.val, $2);
1530 // Upgrade the indices
1531 for (unsigned i = 0; i < $4->size(); ++i) {
1532 ValueInfo& VI = (*$4)[i];
1533 if (VI.type->isUnsigned() && !VI.isConstant() &&
1534 VI.type->getBitWidth() < 64) {
1535 std::string* old = VI.val;
1536 *O << " %gep_upgrade" << unique << " = zext " << *old
1538 VI.val = new std::string("i64 %gep_upgrade" + llvm::utostr(unique++));
1539 VI.type->setOldTy(ULongTy);
1542 *$1 += " " + $2->getNewTy() + " " + Name;
1543 for (unsigned i = 0; i < $4->size(); ++i) {
1544 ValueInfo& VI = (*$4)[i];
1545 *$1 += ", " + *VI.val;
1548 $$.type = getGEPIndexedType($2,$4);
1549 $3.destroy(); delete $4;
1554 int yyerror(const char *ErrorMsg) {
1556 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1557 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
1558 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
1559 if (yychar == YYEMPTY || yychar == 0)
1560 errMsg += "end-of-file.";
1562 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
1563 std::cerr << "llvm-upgrade: " << errMsg << '\n';