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 #define YYERROR_VERBOSE 1
16 #define YYSTYPE std::string*
18 #include "ParserInternals.h"
19 #include <llvm/ADT/StringExtras.h>
25 #define YYINCLUDED_STDLIB_H
27 int yylex(); // declaration" of xxx warnings.
30 static std::string CurFilename;
32 static std::ostream *O = 0;
34 std::istream* LexInput = 0;
36 void UpgradeAssembly(const std::string &infile, std::istream& in,
45 std::cerr << "Parse failed.\n";
54 %token SINTVAL // Signed 32 bit ints...
55 %token UINTVAL // Unsigned 32 bit ints...
56 %token FPVAL // Float or Double constant
57 %token VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
58 %token FLOAT DOUBLE TYPE LABEL
59 %token VAR_ID LABELSTR STRINGCONSTANT
60 %token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
61 %token DECLARE GLOBAL CONSTANT SECTION VOLATILE
62 %token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
63 %token DLLIMPORT DLLEXPORT EXTERN_WEAK
64 %token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
65 %token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
66 %token CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
67 %token X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
69 %token RET BR SWITCH INVOKE UNWIND UNREACHABLE
70 %token ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
71 %token SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
72 %token MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
73 %token TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
74 %token UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
75 %token PHI_TOK SELECT SHL LSHR ASHR VAARG
76 %token EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
83 // Handle constant integer size restriction and conversion...
84 INTVAL : SINTVAL | UINTVAL
85 EINT64VAL : ESINT64VAL | EUINT64VAL;
87 // Operations that are notably excluded from this list include:
88 // RET, BR, & SWITCH because they end basic blocks and are treated specially.
89 ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
90 LogicalOps : AND | OR | XOR;
91 SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
93 ShiftOps : SHL | LSHR | ASHR;
95 // These are some types that allow classification if we only want a particular
96 // thing... for example, only a signed, unsigned, or integral type.
97 SIntType : LONG | INT | SHORT | SBYTE;
98 UIntType : ULONG | UINT | USHORT | UBYTE;
99 IntType : SIntType | UIntType;
100 FPType : FLOAT | DOUBLE;
102 // OptAssign - Value producing statements have an optional assignment component
103 OptAssign : Name '=' {
108 $$ = new std::string("");
112 : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT
114 | /*empty*/ { $$ = new std::string(""); } ;
117 : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK
118 | X86_FASTCALLCC_TOK | CC_TOK EUINT64VAL
119 | /*empty*/ { $$ = new std::string(""); } ;
121 // OptAlign/OptCAlign - An optional alignment, and an optional alignment with
122 // a comma before it.
124 : /*empty*/ { $$ = new std::string(); }
125 | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
128 : /*empty*/ { $$ = new std::string(); }
129 | ',' ALIGN EUINT64VAL {
137 : SECTION STRINGCONSTANT {
143 OptSection : /*empty*/ { $$ = new std::string(); }
147 : /* empty */ { $$ = new std::string(); }
148 | ',' GlobalVarAttribute GlobalVarAttributes {
164 //===----------------------------------------------------------------------===//
165 // Types includes all predefined types... except void, because it can only be
166 // used in specific contexts (function returning void for example). To have
167 // access to it, a user must explicitly use TypesV.
170 // TypesV includes all of 'Types', but it also includes the void type.
171 TypesV : Types | VOID ;
172 UpRTypesV : UpRTypes | VOID ;
175 // Derived types are added later...
177 PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
178 PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
179 UpRTypes : OPAQUE | PrimType | SymbolicValueRef ;
181 // Include derived types in the Types production.
183 UpRTypes : '\\' EUINT64VAL { // Type UpReference
187 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
188 *$1 += "( " + *$3 + " )";
192 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
194 *$2 += " x " + *$4 + " ]";
198 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
200 *$2 += " x " + *$4 + " >";
204 | '{' TypeListI '}' { // Structure type?
209 | '{' '}' { // Empty structure type?
210 $$ = new std::string("{ }");
212 | UpRTypes '*' { // Pointer type?
217 // TypeList - Used for struct declarations and as a basis for function type
218 // declaration type lists
220 TypeListI : UpRTypes | TypeListI ',' UpRTypes {
226 // ArgTypeList - List of types for a function type declaration...
227 ArgTypeListI : TypeListI
228 | TypeListI ',' DOTDOTDOT {
237 $$ = new std::string();
240 // ConstVal - The various declarations that go into the constant pool. This
241 // production is used ONLY to represent constants that show up AFTER a 'const',
242 // 'constant' or 'global' token at global scope. Constants that can be inlined
243 // into other expressions (such as integers and constexprs) are handled by the
244 // ResolvedVal, ValueRef and ConstValueRef productions.
246 ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
247 *$1 += " [ " + *$3 + " ]";
252 $$ = new std::string("[ ]");
254 | Types 'c' STRINGCONSTANT {
259 | Types '<' ConstVector '>' { // Nonempty unsized arr
260 *$1 += " < " + *$3 + " >";
264 | Types '{' ConstVector '}' {
265 *$1 += " { " + *$3 + " }";
270 $$ = new std::string("[ ]");
282 | Types SymbolicValueRef {
292 | Types ZEROINITIALIZER {
298 ConstVal : SIntType EINT64VAL { // integral constants
303 | UIntType EUINT64VAL { // integral constants
308 | BOOL TRUETOK { // Boolean constants
313 | BOOL FALSETOK { // Boolean constants
318 | FPType FPVAL { // Float & Double constants
325 ConstExpr: CastOps '(' ConstVal TO Types ')' {
326 *$1 += " (" + *$3 + " " + *$4 + " " + *$5 + ")";
327 delete $3; delete $4; delete $5;
330 | GETELEMENTPTR '(' ConstVal IndexList ')' {
332 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
334 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
336 | LogicalOps '(' ConstVal ',' ConstVal ')' {
338 | SetCondOps '(' ConstVal ',' ConstVal ')' {
340 | ShiftOps '(' ConstVal ',' ConstVal ')' {
342 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
344 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
346 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
350 // ConstVector - A list of comma separated constants.
351 ConstVector : ConstVector ',' ConstVal {
357 // GlobalType - Match either GLOBAL or CONSTANT for global declarations...
358 GlobalType : GLOBAL { } | CONSTANT { };
361 //===----------------------------------------------------------------------===//
362 // Rules to match Modules
363 //===----------------------------------------------------------------------===//
365 // Module rule: Capture the result of parsing the whole file into a result
368 Module : DefinitionList {
371 // DefinitionList - Top level definitions
373 DefinitionList : DefinitionList Function {
376 | DefinitionList FunctionProto {
381 | DefinitionList MODULE ASM_TOK AsmBlock {
382 *O << "module asm " << " " << *$4 << "\n";
384 | DefinitionList IMPLEMENTATION {
385 *O << "implementation\n";
390 // ConstPool - Constants with optional names assigned to them.
391 ConstPool : ConstPool OptAssign TYPE TypesV {
392 *O << *$2 << " " << *$3 << " " << *$4 << "\n";
393 delete $2; delete $3; delete $4;
396 | ConstPool FunctionProto { // Function prototypes can be in const pool
401 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
402 *O << *$2 << " " << *$3 << " " << *$4 << "\n";
403 delete $2; delete $3; delete $4;
406 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
407 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5 << " " << *$6 << "\n";
408 delete $2; delete $3; delete $4; delete $5; delete $6;
411 | ConstPool OptAssign EXTERNAL GlobalType Types GlobalVarAttributes {
412 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5 << " " << *$6 << "\n";
413 delete $2; delete $3; delete $4; delete $5; delete $6;
416 | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
417 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5 << " " << *$6 << "\n";
418 delete $2; delete $3; delete $4; delete $5; delete $6;
421 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
422 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5 << " " << *$6 << "\n";
423 delete $2; delete $3; delete $4; delete $5; delete $6;
426 | ConstPool TARGET TargetDefinition {
427 *O << *$2 << " " << *$3 << "\n";
428 delete $2; delete $3;
431 | ConstPool DEPLIBS '=' LibrariesDefinition {
432 *O << *$2 << " = " << *$4 << "\n";
433 delete $2; delete $4;
436 | /* empty: end of list */ {
441 AsmBlock : STRINGCONSTANT ;
443 BigOrLittle : BIG | LITTLE
446 : ENDIAN '=' BigOrLittle {
451 | POINTERSIZE '=' EUINT64VAL {
456 | TRIPLE '=' STRINGCONSTANT {
461 | DATALAYOUT '=' STRINGCONSTANT {
475 : LibList ',' STRINGCONSTANT {
481 | /* empty: end of list */ {
482 $$ = new std::string();
485 //===----------------------------------------------------------------------===//
486 // Rules to match Function Headers
487 //===----------------------------------------------------------------------===//
489 Name : VAR_ID | STRINGCONSTANT;
490 OptName : Name | /*empty*/ { $$ = new std::string(); };
492 ArgVal : Types OptName {
498 ArgListH : ArgListH ',' ArgVal {
508 | ArgListH ',' DOTDOTDOT {
516 $$ = new std::string();
519 FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
520 OptSection OptAlign {
522 $2->insert(0, *$1 + " ");
524 *$2 += " " + *$3 + "( " + *$5 + " )";
535 $$ = new std::string("begin");
538 $$ = new std::string ("{");
541 FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
545 *O << *$2 << " " << *$3 << "\n";
546 delete $1; delete $2; delete $3;
550 END : ENDTOK { $$ = new std::string("end"); }
551 | '}' { $$ = new std::string("}"); };
553 Function : FunctionHeader BasicBlockList END {
556 *O << '\n' << *$3 << "\n";
559 FnDeclareLinkage: /*default*/
565 : DECLARE FnDeclareLinkage FunctionHeaderH {
566 *$1 += " " + *$2 + " " + *$3;
567 delete $2; delete $3;
571 //===----------------------------------------------------------------------===//
572 // Rules to match Basic Blocks
573 //===----------------------------------------------------------------------===//
575 OptSideEffect : /* empty */ {
580 ConstValueRef : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK
581 | NULL_TOK | UNDEF | ZEROINITIALIZER
582 | '<' ConstVector '>' {
588 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
592 *$1 += " " + *$3 + ", " + *$4;
593 delete $2; delete $3; delete $4;
597 SymbolicValueRef : INTVAL | Name ;
599 // ValueRef - A reference to a definition... either constant or symbolic
600 ValueRef : SymbolicValueRef | ConstValueRef;
603 // ResolvedVal - a <type> <value> pair. This is used only in cases where the
604 // type immediately preceeds the value reference, and allows complex constant
605 // pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
606 ResolvedVal : Types ValueRef {
612 BasicBlockList : BasicBlockList BasicBlock {
614 | BasicBlock { // Do not allow functions with 0 basic blocks
618 // Basic blocks are terminated by branching instructions:
619 // br, br/cc, switch, ret
621 BasicBlock : InstructionList OptAssign BBTerminatorInst {
625 InstructionList : InstructionList Inst {
626 *O << " " << *$2 << "\n";
639 BBTerminatorInst : RET ResolvedVal { // Return with a result...
640 *O << " " << *$1 << " " << *$2 << "\n";
641 delete $1; delete $2;
644 | RET VOID { // Return with no result...
645 *O << " " << *$1 << " " << *$2 << "\n";
646 delete $1; delete $2;
649 | BR LABEL ValueRef { // Unconditional Branch...
650 *O << " " << *$1 << " " << *$2 << " " << *$3 << "\n";
651 delete $1; delete $2; delete $3;
653 } // Conditional Branch...
654 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
655 *O << " " << *$1 << " " << *$2 << " " << *$3 << ", " << *$5 << " "
656 << *$6 << ", " << *$8 << " " << *$9 << "\n";
657 delete $1; delete $2; delete $3; delete $5; delete $6; delete $8; delete $9;
660 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
661 *O << " " << *$1 << " " << *$2 << " " << *$3 << ", " << *$5 << " "
662 << *$6 << " [" << *$8 << " ]\n";
663 delete $1; delete $2; delete $3; delete $5; delete $6; delete $8;
666 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
667 *O << " " << *$1 << " " << *$2 << " " << *$3 << ", " << *$5 << " "
669 delete $1; delete $2; delete $3; delete $5; delete $6;
672 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
673 TO LABEL ValueRef UNWIND LABEL ValueRef {
674 *O << " " << *$1 << " " << *$2 << " " << *$3 << " " << *$4 << " ("
675 << *$6 << ") " << *$8 << " " << *$9 << " " << *$10 << " " << *$11 << " "
676 << *$12 << " " << *$13 << "\n";
677 delete $1; delete $2; delete $3; delete $4; delete $6; delete $8; delete $9;
678 delete $10; delete $11; delete $12; delete $13;
682 *O << " " << *$1 << "\n";
687 *O << " " << *$1 << "\n";
692 JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
693 *$1 += *$2 + " " + *$3 + ", " + *$5 + " " + *$6;
694 delete $2; delete $3; delete $5; delete $6;
697 | IntType ConstValueRef ',' LABEL ValueRef {
698 *$1 += *$2 + ", " + *$4 + " " + *$5;
699 delete $2; delete $4; delete $5;
704 : OptAssign InstVal {
711 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
712 *$1 += " [" + *$3 + "," + *$5 + "]";
713 delete $3; delete $5;
716 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
717 *$1 += ", [" + *$4 + "," + *$6 + "]";
718 delete $4; delete $6;
725 | ValueRefList ',' ResolvedVal {
731 // ValueRefListE - Just like ValueRefList, except that it may also be empty!
734 | /*empty*/ { $$ = new std::string(); }
746 InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
747 *$1 += " " + *$2 + " " + *$3 + ", " + *$5;
748 delete $2; delete $3; delete $5;
751 | LogicalOps Types ValueRef ',' ValueRef {
752 *$1 += " " + *$2 + " " + *$3 + ", " + *$5;
753 delete $2; delete $3; delete $5;
756 | SetCondOps Types ValueRef ',' ValueRef {
757 *$1 += " " + *$2 + " " + *$3 + ", " + *$5;
758 delete $2; delete $3; delete $5;
766 | ShiftOps ResolvedVal ',' ResolvedVal {
767 *$1 += " " + *$2 + ", " + *$4;
768 delete $2; delete $4;
771 | CastOps ResolvedVal TO Types {
772 *$1 += " " + *$2 + " " + *$3 + ", " + *$4;
773 delete $2; delete $3; delete $4;
776 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
777 *$1 += " " + *$2 + ", " + *$4 + ", " + *$6;
778 delete $2; delete $4; delete $6;
781 | VAARG ResolvedVal ',' Types {
782 *$1 += " " + *$2 + ", " + *$4;
783 delete $2; delete $4;
786 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
787 *$1 += " " + *$2 + ", " + *$4;
788 delete $2; delete $4;
791 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
792 *$1 += " " + *$2 + ", " + *$4 + ", " + *$6;
793 delete $2; delete $4; delete $6;
796 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
797 *$1 += " " + *$2 + ", " + *$4 + ", " + *$6;
798 delete $2; delete $4; delete $6;
806 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
811 *$1 += *$3 += " " + *$4 + "(" + *$5 + ")";
812 delete $2; delete $3; delete $4; delete $6;
818 // IndexList - List of indices for GEP based instructions...
824 | /* empty */ { $$ = new std::string(); }
829 | /* empty */ { $$ = new std::string(); }
832 MemoryInst : MALLOC Types OptCAlign {
836 delete $2; delete $3;
839 | MALLOC Types ',' UINT ValueRef OptCAlign {
840 *$1 += " " + *$2 + ", " + *$4 + " " + *$5;
843 delete $2; delete $4; delete $5; delete $6;
846 | ALLOCA Types OptCAlign {
850 delete $2; delete $3;
853 | ALLOCA Types ',' UINT ValueRef OptCAlign {
854 *$1 += " " + *$2 + ", " + *$4 + " " + *$5;
857 delete $2; delete $4; delete $5; delete $6;
865 | OptVolatile LOAD Types ValueRef {
868 *$1 += *$2 + " " + *$3 + " " + *$4;
869 delete $2; delete $3; delete $4;
872 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
875 *$1 += *$2 + " " + *$3 + ", " + *$5 + " " + *$6;
876 delete $2; delete $3; delete $5; delete $6;
879 | GETELEMENTPTR Types ValueRef IndexList {
880 *$1 += *$2 + " " + *$3 + " " + *$4;
881 delete $2; delete $3; delete $4;
887 int yyerror(const char *ErrorMsg) {
889 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
890 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
891 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
892 if (yychar == YYEMPTY || yychar == 0)
893 errMsg += "end-of-file.";
895 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
896 std::cerr << errMsg << '\n';