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 assembly language upgrade.
12 //===----------------------------------------------------------------------===//
15 #define YYERROR_VERBOSE 1
16 #define YYSTYPE std::string*
18 #include "ParserInternals.h"
19 #include <llvm/ADT/StringExtras.h>
20 #include <llvm/System/MappedFile.h>
27 int yylex(); // declaration" of xxx warnings.
30 static std::string CurFilename;
32 static std::ostream *O = 0;
34 void UpgradeAssembly(const std::string &infile, std::ostream &out)
38 llvm::sys::Path p(infile);
39 llvm::sys::MappedFile mf;
42 const char* base = mf.charBase();
43 size_t sz = mf.size();
45 set_scan_bytes(base, sz);
50 std::cerr << "Parse failed.\n";
59 %token SINTVAL // Signed 32 bit ints...
60 %token UINTVAL // Unsigned 32 bit ints...
61 %token FPVAL // Float or Double constant
62 %token VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
63 %token FLOAT DOUBLE TYPE LABEL
64 %token VAR_ID LABELSTR STRINGCONSTANT
65 %token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
66 %token DECLARE GLOBAL CONSTANT SECTION VOLATILE
67 %token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
68 %token DLLIMPORT DLLEXPORT EXTERN_WEAK
69 %token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
70 %token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
71 %token CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
72 %token X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
74 %token RET BR SWITCH INVOKE UNWIND UNREACHABLE
75 %token ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
76 %token SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
77 %token MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
78 %token TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
79 %token UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
80 %token PHI_TOK SELECT SHL LSHR ASHR VAARG
81 %token EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
88 // Handle constant integer size restriction and conversion...
89 INTVAL : SINTVAL | UINTVAL
90 EINT64VAL : ESINT64VAL | EUINT64VAL;
92 // Operations that are notably excluded from this list include:
93 // RET, BR, & SWITCH because they end basic blocks and are treated specially.
94 ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
95 LogicalOps : AND | OR | XOR;
96 SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
98 ShiftOps : SHL | LSHR | ASHR;
100 // These are some types that allow classification if we only want a particular
101 // thing... for example, only a signed, unsigned, or integral type.
102 SIntType : LONG | INT | SHORT | SBYTE;
103 UIntType : ULONG | UINT | USHORT | UBYTE;
104 IntType : SIntType | UIntType;
105 FPType : FLOAT | DOUBLE;
107 // OptAssign - Value producing statements have an optional assignment component
108 OptAssign : Name '=' {
113 $$ = new std::string("");
117 : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT
119 | /*empty*/ { $$ = new std::string(""); } ;
122 : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK
123 | X86_FASTCALLCC_TOK | CC_TOK EUINT64VAL
124 | /*empty*/ { $$ = new std::string(""); } ;
126 // OptAlign/OptCAlign - An optional alignment, and an optional alignment with
127 // a comma before it.
129 : /*empty*/ { $$ = new std::string(); }
130 | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
133 : /*empty*/ { $$ = new std::string(); }
134 | ',' ALIGN EUINT64VAL {
142 : SECTION STRINGCONSTANT {
148 OptSection : /*empty*/ { $$ = new std::string(); }
152 : /* empty */ { $$ = new std::string(); }
153 | ',' GlobalVarAttribute GlobalVarAttributes {
169 //===----------------------------------------------------------------------===//
170 // Types includes all predefined types... except void, because it can only be
171 // used in specific contexts (function returning void for example). To have
172 // access to it, a user must explicitly use TypesV.
175 // TypesV includes all of 'Types', but it also includes the void type.
176 TypesV : Types | VOID ;
177 UpRTypesV : UpRTypes | VOID ;
180 // Derived types are added later...
182 PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
183 PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
184 UpRTypes : OPAQUE | PrimType | SymbolicValueRef ;
186 // Include derived types in the Types production.
188 UpRTypes : '\\' EUINT64VAL { // Type UpReference
192 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
193 *$1 += "( " + *$3 + " )";
197 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
199 *$2 += " x " + *$4 + " ]";
203 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
205 *$2 += " x " + *$4 + " >";
209 | '{' TypeListI '}' { // Structure type?
214 | '{' '}' { // Empty structure type?
215 $$ = new std::string("{ }");
217 | UpRTypes '*' { // Pointer type?
222 // TypeList - Used for struct declarations and as a basis for function type
223 // declaration type lists
225 TypeListI : UpRTypes | TypeListI ',' UpRTypes {
231 // ArgTypeList - List of types for a function type declaration...
232 ArgTypeListI : TypeListI
233 | TypeListI ',' DOTDOTDOT {
242 $$ = new std::string();
245 // ConstVal - The various declarations that go into the constant pool. This
246 // production is used ONLY to represent constants that show up AFTER a 'const',
247 // 'constant' or 'global' token at global scope. Constants that can be inlined
248 // into other expressions (such as integers and constexprs) are handled by the
249 // ResolvedVal, ValueRef and ConstValueRef productions.
251 ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
252 *$1 += " [ " + *$3 + " ]";
257 $$ = new std::string("[ ]");
259 | Types 'c' STRINGCONSTANT {
264 | Types '<' ConstVector '>' { // Nonempty unsized arr
265 *$1 += " < " + *$3 + " >";
269 | Types '{' ConstVector '}' {
270 *$1 += " { " + *$3 + " }";
275 $$ = new std::string("[ ]");
287 | Types SymbolicValueRef {
297 | Types ZEROINITIALIZER {
303 ConstVal : SIntType EINT64VAL { // integral constants
308 | UIntType EUINT64VAL { // integral constants
313 | BOOL TRUETOK { // Boolean constants
318 | BOOL FALSETOK { // Boolean constants
323 | FPType FPVAL { // Float & Double constants
330 ConstExpr: CastOps '(' ConstVal TO Types ')' {
331 *$1 += " (" + *$3 + " " + *$4 + " " + *$5 + ")";
332 delete $3; delete $4; delete $5;
335 | GETELEMENTPTR '(' ConstVal IndexList ')' {
337 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
339 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
341 | LogicalOps '(' ConstVal ',' ConstVal ')' {
343 | SetCondOps '(' ConstVal ',' ConstVal ')' {
345 | ShiftOps '(' ConstVal ',' ConstVal ')' {
347 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
349 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
351 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
355 // ConstVector - A list of comma separated constants.
356 ConstVector : ConstVector ',' ConstVal {
362 // GlobalType - Match either GLOBAL or CONSTANT for global declarations...
363 GlobalType : GLOBAL { } | CONSTANT { };
366 //===----------------------------------------------------------------------===//
367 // Rules to match Modules
368 //===----------------------------------------------------------------------===//
370 // Module rule: Capture the result of parsing the whole file into a result
373 Module : DefinitionList {
376 // DefinitionList - Top level definitions
378 DefinitionList : DefinitionList Function {
381 | DefinitionList FunctionProto {
386 | DefinitionList MODULE ASM_TOK AsmBlock {
387 *O << "module asm " << " " << *$4 << "\n";
389 | DefinitionList IMPLEMENTATION {
390 *O << "implementation\n";
395 // ConstPool - Constants with optional names assigned to them.
396 ConstPool : ConstPool OptAssign TYPE TypesV {
397 *O << *$2 << " " << *$3 << " " << *$4 << "\n";
398 delete $2; delete $3; delete $4;
401 | ConstPool FunctionProto { // Function prototypes can be in const pool
406 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
407 *O << *$2 << " " << *$3 << " " << *$4 << "\n";
408 delete $2; delete $3; delete $4;
411 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
412 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5 << " " << *$6 << "\n";
413 delete $2; delete $3; delete $4; delete $5; delete $6;
416 | ConstPool OptAssign EXTERNAL 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 DLLIMPORT GlobalType Types GlobalVarAttributes {
422 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5 << " " << *$6 << "\n";
423 delete $2; delete $3; delete $4; delete $5; delete $6;
426 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
427 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5 << " " << *$6 << "\n";
428 delete $2; delete $3; delete $4; delete $5; delete $6;
431 | ConstPool TARGET TargetDefinition {
432 *O << *$2 << " " << *$3 << "\n";
433 delete $2; delete $3;
436 | ConstPool DEPLIBS '=' LibrariesDefinition {
437 *O << *$2 << " = " << *$4 << "\n";
438 delete $2; delete $4;
441 | /* empty: end of list */ {
446 AsmBlock : STRINGCONSTANT ;
448 BigOrLittle : BIG | LITTLE
451 : ENDIAN '=' BigOrLittle {
456 | POINTERSIZE '=' EUINT64VAL {
461 | TRIPLE '=' STRINGCONSTANT {
466 | DATALAYOUT '=' STRINGCONSTANT {
480 : LibList ',' STRINGCONSTANT {
486 | /* empty: end of list */ {
487 $$ = new std::string();
490 //===----------------------------------------------------------------------===//
491 // Rules to match Function Headers
492 //===----------------------------------------------------------------------===//
494 Name : VAR_ID | STRINGCONSTANT;
495 OptName : Name | /*empty*/ { $$ = new std::string(); };
497 ArgVal : Types OptName {
503 ArgListH : ArgListH ',' ArgVal {
513 | ArgListH ',' DOTDOTDOT {
521 $$ = new std::string();
524 FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
525 OptSection OptAlign {
527 $2->insert(0, *$1 + " ");
529 *$2 += " " + *$3 + "( " + *$5 + " )";
540 $$ = new std::string("begin");
543 $$ = new std::string ("{");
546 FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
550 *O << *$2 << " " << *$3 << "\n";
551 delete $1; delete $2; delete $3;
555 END : ENDTOK { $$ = new std::string("end"); }
556 | '}' { $$ = new std::string("}"); };
558 Function : FunctionHeader BasicBlockList END {
561 *O << '\n' << *$3 << "\n";
564 FnDeclareLinkage: /*default*/
570 : DECLARE FnDeclareLinkage FunctionHeaderH {
571 *$1 += " " + *$2 + " " + *$3;
572 delete $2; delete $3;
576 //===----------------------------------------------------------------------===//
577 // Rules to match Basic Blocks
578 //===----------------------------------------------------------------------===//
580 OptSideEffect : /* empty */ {
585 ConstValueRef : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK
586 | NULL_TOK | UNDEF | ZEROINITIALIZER
587 | '<' ConstVector '>' {
593 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
597 *$1 += " " + *$3 + ", " + *$4;
598 delete $2; delete $3; delete $4;
602 SymbolicValueRef : INTVAL | Name ;
604 // ValueRef - A reference to a definition... either constant or symbolic
605 ValueRef : SymbolicValueRef | ConstValueRef;
608 // ResolvedVal - a <type> <value> pair. This is used only in cases where the
609 // type immediately preceeds the value reference, and allows complex constant
610 // pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
611 ResolvedVal : Types ValueRef {
617 BasicBlockList : BasicBlockList BasicBlock {
619 | BasicBlock { // Do not allow functions with 0 basic blocks
623 // Basic blocks are terminated by branching instructions:
624 // br, br/cc, switch, ret
626 BasicBlock : InstructionList OptAssign BBTerminatorInst {
630 InstructionList : InstructionList Inst {
631 *O << " " << *$2 << "\n";
644 BBTerminatorInst : RET ResolvedVal { // Return with a result...
645 *O << " " << *$1 << " " << *$2 << "\n";
646 delete $1; delete $2;
649 | RET VOID { // Return with no result...
650 *O << " " << *$1 << " " << *$2 << "\n";
651 delete $1; delete $2;
654 | BR LABEL ValueRef { // Unconditional Branch...
655 *O << " " << *$1 << " " << *$2 << " " << *$3 << "\n";
656 delete $1; delete $2; delete $3;
658 } // Conditional Branch...
659 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
660 *O << " " << *$1 << " " << *$2 << " " << *$3 << ", " << *$5 << " "
661 << *$6 << ", " << *$8 << " " << *$9 << "\n";
662 delete $1; delete $2; delete $3; delete $5; delete $6; delete $8; delete $9;
665 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
666 *O << " " << *$1 << " " << *$2 << " " << *$3 << ", " << *$5 << " "
667 << *$6 << " [" << *$8 << " ]\n";
668 delete $1; delete $2; delete $3; delete $5; delete $6; delete $8;
671 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
672 *O << " " << *$1 << " " << *$2 << " " << *$3 << ", " << *$5 << " "
674 delete $1; delete $2; delete $3; delete $5; delete $6;
677 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
678 TO LABEL ValueRef UNWIND LABEL ValueRef {
679 *O << " " << *$1 << " " << *$2 << " " << *$3 << " " << *$4 << " ("
680 << *$6 << ") " << *$8 << " " << *$9 << " " << *$10 << " " << *$11 << " "
681 << *$12 << " " << *$13 << "\n";
682 delete $1; delete $2; delete $3; delete $4; delete $6; delete $8; delete $9;
683 delete $10; delete $11; delete $12; delete $13;
687 *O << " " << *$1 << "\n";
692 *O << " " << *$1 << "\n";
697 JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
698 *$1 += *$2 + " " + *$3 + ", " + *$5 + " " + *$6;
699 delete $2; delete $3; delete $5; delete $6;
702 | IntType ConstValueRef ',' LABEL ValueRef {
703 *$1 += *$2 + ", " + *$4 + " " + *$5;
704 delete $2; delete $4; delete $5;
709 : OptAssign InstVal {
716 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
717 *$1 += " [" + *$3 + "," + *$5 + "]";
718 delete $3; delete $5;
721 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
722 *$1 += ", [" + *$4 + "," + *$6 + "]";
723 delete $4; delete $6;
730 | ValueRefList ',' ResolvedVal {
736 // ValueRefListE - Just like ValueRefList, except that it may also be empty!
739 | /*empty*/ { $$ = new std::string(); }
751 InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
752 *$1 += " " + *$2 + " " + *$3 + ", " + *$5;
753 delete $2; delete $3; delete $5;
756 | LogicalOps Types ValueRef ',' ValueRef {
757 *$1 += " " + *$2 + " " + *$3 + ", " + *$5;
758 delete $2; delete $3; delete $5;
761 | SetCondOps Types ValueRef ',' ValueRef {
762 *$1 += " " + *$2 + " " + *$3 + ", " + *$5;
763 delete $2; delete $3; delete $5;
771 | ShiftOps ResolvedVal ',' ResolvedVal {
772 *$1 += " " + *$2 + ", " + *$4;
773 delete $2; delete $4;
776 | CastOps ResolvedVal TO Types {
777 *$1 += " " + *$2 + " " + *$3 + ", " + *$4;
778 delete $2; delete $3; delete $4;
781 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
782 *$1 += " " + *$2 + ", " + *$4 + ", " + *$6;
783 delete $2; delete $4; delete $6;
786 | VAARG ResolvedVal ',' Types {
787 *$1 += " " + *$2 + ", " + *$4;
788 delete $2; delete $4;
791 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
792 *$1 += " " + *$2 + ", " + *$4;
793 delete $2; delete $4;
796 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
797 *$1 += " " + *$2 + ", " + *$4 + ", " + *$6;
798 delete $2; delete $4; delete $6;
801 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
802 *$1 += " " + *$2 + ", " + *$4 + ", " + *$6;
803 delete $2; delete $4; delete $6;
811 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
816 *$1 += *$3 += " " + *$4 + "(" + *$5 + ")";
817 delete $2; delete $3; delete $4; delete $6;
823 // IndexList - List of indices for GEP based instructions...
829 | /* empty */ { $$ = new std::string(); }
834 | /* empty */ { $$ = new std::string(); }
837 MemoryInst : MALLOC Types OptCAlign {
841 delete $2; delete $3;
844 | MALLOC Types ',' UINT ValueRef OptCAlign {
845 *$1 += " " + *$2 + ", " + *$4 + " " + *$5;
848 delete $2; delete $4; delete $5; delete $6;
851 | ALLOCA Types OptCAlign {
855 delete $2; delete $3;
858 | ALLOCA Types ',' UINT ValueRef OptCAlign {
859 *$1 += " " + *$2 + ", " + *$4 + " " + *$5;
862 delete $2; delete $4; delete $5; delete $6;
870 | OptVolatile LOAD Types ValueRef {
873 *$1 += *$2 + " " + *$3 + " " + *$4;
874 delete $2; delete $3; delete $4;
877 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
880 *$1 += *$2 + " " + *$3 + ", " + *$5 + " " + *$6;
881 delete $2; delete $3; delete $5; delete $6;
884 | GETELEMENTPTR Types ValueRef IndexList {
885 *$1 += *$2 + " " + *$3 + " " + *$4;
886 delete $2; delete $3; delete $4;
892 int yyerror(const char *ErrorMsg) {
894 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
895 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
896 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
897 if (yychar == YYEMPTY || yychar == 0)
898 errMsg += "end-of-file.";
900 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
901 std::cerr << errMsg << '\n';