blob: f09f426c6b1f371c8d2d43438eff1ee1f863837c [file] [log] [blame]
Reid Spencer96839be2006-11-30 16:50:26 +00001//===-- UpgradeParser.y - Upgrade parser for llvm assmbly -------*- C++ -*-===//
Reid Spencere7c3c602006-11-30 06:36:44 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
Reid Spencer96839be2006-11-30 16:50:26 +000010// This file implements the bison parser for LLVM 1.9 assembly language.
Reid Spencere7c3c602006-11-30 06:36:44 +000011//
12//===----------------------------------------------------------------------===//
13
14%{
Reid Spencere7c3c602006-11-30 06:36:44 +000015#include "ParserInternals.h"
16#include <llvm/ADT/StringExtras.h>
Reid Spencere7c3c602006-11-30 06:36:44 +000017#include <algorithm>
18#include <list>
19#include <utility>
20#include <iostream>
21
Reid Spencere77e35e2006-12-01 20:26:20 +000022#define YYERROR_VERBOSE 1
Reid Spencer96839be2006-11-30 16:50:26 +000023#define YYINCLUDED_STDLIB_H
Reid Spencere77e35e2006-12-01 20:26:20 +000024#define YYDEBUG 1
Reid Spencere7c3c602006-11-30 06:36:44 +000025
26int yylex(); // declaration" of xxx warnings.
27int yyparse();
Reid Spencere77e35e2006-12-01 20:26:20 +000028extern int yydebug;
Reid Spencere7c3c602006-11-30 06:36:44 +000029
30static std::string CurFilename;
Reid Spencere7c3c602006-11-30 06:36:44 +000031static std::ostream *O = 0;
Reid Spencer96839be2006-11-30 16:50:26 +000032std::istream* LexInput = 0;
Reid Spencere77e35e2006-12-01 20:26:20 +000033unsigned SizeOfPointer = 32;
Reid Spencer96839be2006-11-30 16:50:26 +000034
35void UpgradeAssembly(const std::string &infile, std::istream& in,
Reid Spencere77e35e2006-12-01 20:26:20 +000036 std::ostream &out, bool debug)
Reid Spencere7c3c602006-11-30 06:36:44 +000037{
38 Upgradelineno = 1;
39 CurFilename = infile;
Reid Spencer96839be2006-11-30 16:50:26 +000040 LexInput = &in;
Reid Spencere77e35e2006-12-01 20:26:20 +000041 yydebug = debug;
Reid Spencere7c3c602006-11-30 06:36:44 +000042 O = &out;
43
44 if (yyparse()) {
45 std::cerr << "Parse failed.\n";
46 exit(1);
47 }
48}
49
Reid Spencer280d8012006-12-01 23:40:53 +000050std::string getCastUpgrade(std::string& Source, TypeInfo& SrcTy,
51 TypeInfo&DstTy, bool isConst = false)
52{
53 std::string Result;
54 if (SrcTy.isFloatingPoint() && DstTy.isPointer()) {
55 if (isConst)
56 Source = "ulong fptoui(" + Source + " to ulong)";
57 else {
58 Result = "%cast_upgrade = fptoui " + Source + " to ulong";
59 Source = "ulong %cast_upgrade";
60 }
61 SrcTy.destroy();
62 SrcTy.newTy = new std::string("ulong");
63 SrcTy.oldTy = ULongTy;
64 }
65 return Result;
66}
67
68const char* getCastOpcode(std::string& Source, TypeInfo& SrcTy,
69 TypeInfo&DstTy) {
Reid Spencere77e35e2006-12-01 20:26:20 +000070 unsigned SrcBits = SrcTy.getBitWidth();
71 unsigned DstBits = DstTy.getBitWidth();
72 const char* opcode = "bitcast";
73 // Run through the possibilities ...
74 if (DstTy.isIntegral()) { // Casting to integral
75 if (SrcTy.isIntegral()) { // Casting from integral
76 if (DstBits < SrcBits)
77 opcode = "trunc";
78 else if (DstBits > SrcBits) { // its an extension
79 if (SrcTy.isSigned())
80 opcode ="sext"; // signed -> SEXT
81 else
82 opcode = "zext"; // unsigned -> ZEXT
83 } else {
84 opcode = "bitcast"; // Same size, No-op cast
85 }
86 } else if (SrcTy.isFloatingPoint()) { // Casting from floating pt
87 if (DstTy.isSigned())
88 opcode = "fptosi"; // FP -> sint
89 else
90 opcode = "fptoui"; // FP -> uint
91 } else if (SrcTy.isPacked()) {
92 assert(DstBits == SrcTy.getBitWidth() &&
93 "Casting packed to integer of different width");
94 opcode = "bitcast"; // same size, no-op cast
95 } else {
96 assert(SrcTy.isPointer() &&
97 "Casting from a value that is not first-class type");
98 opcode = "ptrtoint"; // ptr -> int
99 }
100 } else if (DstTy.isFloatingPoint()) { // Casting to floating pt
101 if (SrcTy.isIntegral()) { // Casting from integral
102 if (SrcTy.isSigned())
103 opcode = "sitofp"; // sint -> FP
104 else
105 opcode = "uitofp"; // uint -> FP
106 } else if (SrcTy.isFloatingPoint()) { // Casting from floating pt
107 if (DstBits < SrcBits) {
108 opcode = "fptrunc"; // FP -> smaller FP
109 } else if (DstBits > SrcBits) {
110 opcode = "fpext"; // FP -> larger FP
111 } else {
112 opcode ="bitcast"; // same size, no-op cast
113 }
114 } else if (SrcTy.isPacked()) {
115 assert(DstBits == SrcTy.getBitWidth() &&
116 "Casting packed to floating point of different width");
117 opcode = "bitcast"; // same size, no-op cast
118 } else {
119 assert(0 && "Casting pointer or non-first class to float");
120 }
121 } else if (DstTy.isPacked()) {
122 if (SrcTy.isPacked()) {
123 assert(DstTy.getBitWidth() == SrcTy.getBitWidth() &&
124 "Casting packed to packed of different widths");
125 opcode = "bitcast"; // packed -> packed
126 } else if (DstTy.getBitWidth() == SrcBits) {
127 opcode = "bitcast"; // float/int -> packed
128 } else {
129 assert(!"Illegal cast to packed (wrong type or size)");
130 }
131 } else if (DstTy.isPointer()) {
132 if (SrcTy.isPointer()) {
133 opcode = "bitcast"; // ptr -> ptr
134 } else if (SrcTy.isIntegral()) {
135 opcode = "inttoptr"; // int -> ptr
Reid Spencer280d8012006-12-01 23:40:53 +0000136 } else if (SrcTy.isFloatingPoint()) { // float/double -> ptr
137 // Cast to int first
138 *O << " %upgrade_cast = fptoui " << Source << " to ulong\n";
139 opcode = "inttoptr";
140 Source = "ulong %upgrade_cast";
Reid Spencere77e35e2006-12-01 20:26:20 +0000141 } else {
142 assert(!"Casting pointer to other than pointer or int");
143 }
144 } else {
145 assert(!"Casting to type that is not first-class");
146 }
147 return opcode;
148}
149
Reid Spencere7c3c602006-11-30 06:36:44 +0000150%}
151
Reid Spencere77e35e2006-12-01 20:26:20 +0000152%file-prefix="UpgradeParser"
153
154%union {
155 std::string* String;
156 TypeInfo Type;
157 ValueInfo Value;
158 ConstInfo Const;
159}
160
Reid Spencerf2d55322006-12-01 21:52:30 +0000161%token <Type> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
162%token <Type> FLOAT DOUBLE LABEL OPAQUE
163%token <String> ESINT64VAL EUINT64VAL SINTVAL UINTVAL FPVAL
164%token <String> NULL_TOK UNDEF ZEROINITIALIZER TRUETOK FALSETOK
Reid Spencere77e35e2006-12-01 20:26:20 +0000165%token <String> TYPE VAR_ID LABELSTR STRINGCONSTANT
166%token <String> IMPLEMENTATION BEGINTOK ENDTOK
167%token <String> DECLARE GLOBAL CONSTANT SECTION VOLATILE
168%token <String> TO DOTDOTDOT CONST INTERNAL LINKONCE WEAK
169%token <String> DLLIMPORT DLLEXPORT EXTERN_WEAK APPENDING
170%token <String> NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
171%token <String> ALIGN
172%token <String> DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
173%token <String> CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
174%token <String> X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
175%token <String> DATALAYOUT
176%token <String> RET BR SWITCH INVOKE UNWIND UNREACHABLE
177%token <String> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
178%token <String> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
179%token <String> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Reid Spencerf7bde222006-12-01 22:26:37 +0000180%token <String> PHI_TOK SELECT SHL SHR ASHR LSHR VAARG
Reid Spencere77e35e2006-12-01 20:26:20 +0000181%token <String> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Reid Spencerfcb5df82006-12-01 22:34:43 +0000182%token <String> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI UITOFP SITOFP
183%token <String> PTRTOINT INTTOPTR BITCAST
Reid Spencere77e35e2006-12-01 20:26:20 +0000184
185%type <String> OptAssign OptLinkage OptCallingConv OptAlign OptCAlign
186%type <String> SectionString OptSection GlobalVarAttributes GlobalVarAttribute
187%type <String> ArgTypeListI ConstExpr DefinitionList
188%type <String> ConstPool TargetDefinition LibrariesDefinition LibList OptName
189%type <String> ArgVal ArgListH ArgList FunctionHeaderH BEGIN FunctionHeader END
190%type <String> Function FunctionProto BasicBlock TypeListI
191%type <String> InstructionList BBTerminatorInst JumpTable Inst PHIList
192%type <String> ValueRefList OptTailCall InstVal IndexList OptVolatile
193%type <String> MemoryInst SymbolicValueRef OptSideEffect GlobalType
194%type <String> FnDeclareLinkage BasicBlockList BigOrLittle AsmBlock
Reid Spencerfcb5df82006-12-01 22:34:43 +0000195%type <String> Name ValueRef ValueRefListE ConstValueRef
196%type <String> ShiftOps SetCondOps LogicalOps ArithmeticOps CastOps
Reid Spencere77e35e2006-12-01 20:26:20 +0000197
198%type <String> ConstVector
199
200%type <Type> IntType SIntType UIntType FPType TypesV Types
201%type <Type> PrimType UpRTypesV UpRTypes
202
Reid Spencerf2d55322006-12-01 21:52:30 +0000203%type <String> IntVal EInt64Val
204%type <Const> ConstVal
Reid Spencere77e35e2006-12-01 20:26:20 +0000205
206%type <Value> ResolvedVal
Reid Spencere7c3c602006-11-30 06:36:44 +0000207
208%start Module
209
210%%
211
212// Handle constant integer size restriction and conversion...
Reid Spencerf2d55322006-12-01 21:52:30 +0000213IntVal : SINTVAL | UINTVAL ;
Reid Spencere77e35e2006-12-01 20:26:20 +0000214EInt64Val : ESINT64VAL | EUINT64VAL;
Reid Spencere7c3c602006-11-30 06:36:44 +0000215
216// Operations that are notably excluded from this list include:
217// RET, BR, & SWITCH because they end basic blocks and are treated specially.
218ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
219LogicalOps : AND | OR | XOR;
220SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Reid Spencerf7bde222006-12-01 22:26:37 +0000221ShiftOps : SHL | SHR | ASHR | LSHR;
Reid Spencerfcb5df82006-12-01 22:34:43 +0000222CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI |
223 UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
224 ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000225
226// These are some types that allow classification if we only want a particular
227// thing... for example, only a signed, unsigned, or integral type.
228SIntType : LONG | INT | SHORT | SBYTE;
229UIntType : ULONG | UINT | USHORT | UBYTE;
230IntType : SIntType | UIntType;
231FPType : FLOAT | DOUBLE;
232
233// OptAssign - Value producing statements have an optional assignment component
234OptAssign : Name '=' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000235 *$1 += " = ";
Reid Spencere7c3c602006-11-30 06:36:44 +0000236 $$ = $1;
237 }
238 | /*empty*/ {
239 $$ = new std::string("");
240 };
241
242OptLinkage
243 : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT
244 | EXTERN_WEAK
245 | /*empty*/ { $$ = new std::string(""); } ;
246
247OptCallingConv
248 : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK
Reid Spencer16244f42006-12-01 21:10:07 +0000249 | X86_FASTCALLCC_TOK
250 | CC_TOK EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000251 *$1 += *$2;
252 delete $2;
Reid Spencer16244f42006-12-01 21:10:07 +0000253 $$ = $1;
254 }
Reid Spencere7c3c602006-11-30 06:36:44 +0000255 | /*empty*/ { $$ = new std::string(""); } ;
256
257// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
258// a comma before it.
259OptAlign
260 : /*empty*/ { $$ = new std::string(); }
Reid Spencerf2d55322006-12-01 21:52:30 +0000261 | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
Reid Spencere7c3c602006-11-30 06:36:44 +0000262 ;
263OptCAlign
264 : /*empty*/ { $$ = new std::string(); }
265 | ',' ALIGN EUINT64VAL {
266 $2->insert(0, ", ");
Reid Spencerf2d55322006-12-01 21:52:30 +0000267 *$2 += " " + *$3;
268 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000269 $$ = $2;
270 };
271
272SectionString
273 : SECTION STRINGCONSTANT {
274 *$1 += " " + *$2;
275 delete $2;
276 $$ = $1;
277 };
278
279OptSection : /*empty*/ { $$ = new std::string(); }
280 | SectionString;
281
282GlobalVarAttributes
283 : /* empty */ { $$ = new std::string(); }
284 | ',' GlobalVarAttribute GlobalVarAttributes {
285 $2->insert(0, ", ");
286 if (!$3->empty())
287 *$2 += " " + *$3;
288 delete $3;
289 $$ = $2;
290 };
291
292GlobalVarAttribute
293 : SectionString
294 | ALIGN EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000295 *$1 += " " + *$2;
296 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000297 $$ = $1;
298 };
299
300//===----------------------------------------------------------------------===//
301// Types includes all predefined types... except void, because it can only be
302// used in specific contexts (function returning void for example). To have
303// access to it, a user must explicitly use TypesV.
304//
305
306// TypesV includes all of 'Types', but it also includes the void type.
307TypesV : Types | VOID ;
308UpRTypesV : UpRTypes | VOID ;
309Types : UpRTypes ;
310
311// Derived types are added later...
312//
313PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
Reid Spencere77e35e2006-12-01 20:26:20 +0000314PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
Reid Spencerd154b572006-12-01 20:36:40 +0000315UpRTypes : OPAQUE | PrimType
316 | SymbolicValueRef {
317 $$.newTy = $1; $$.oldTy = OpaqueTy;
318 };
Reid Spencere7c3c602006-11-30 06:36:44 +0000319
320// Include derived types in the Types production.
321//
322UpRTypes : '\\' EUINT64VAL { // Type UpReference
Reid Spencerf2d55322006-12-01 21:52:30 +0000323 $2->insert(0, "\\");
324 $$.newTy = $2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000325 $$.oldTy = OpaqueTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000326 }
327 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000328 *$1.newTy += "( " + *$3 + " )";
Reid Spencere7c3c602006-11-30 06:36:44 +0000329 delete $3;
Reid Spencere77e35e2006-12-01 20:26:20 +0000330 $$.newTy = $1.newTy;
331 $$.oldTy = FunctionTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000332 }
333 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencerf2d55322006-12-01 21:52:30 +0000334 $2->insert(0,"[ ");
335 *$2 += " x " + *$4.newTy + " ]";
Reid Spencere77e35e2006-12-01 20:26:20 +0000336 delete $4.newTy;
Reid Spencerf2d55322006-12-01 21:52:30 +0000337 $$.newTy = $2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000338 $$.oldTy = ArrayTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000339 }
340 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
Reid Spencerf2d55322006-12-01 21:52:30 +0000341 $2->insert(0,"< ");
342 *$2 += " x " + *$4.newTy + " >";
Reid Spencere77e35e2006-12-01 20:26:20 +0000343 delete $4.newTy;
Reid Spencerf2d55322006-12-01 21:52:30 +0000344 $$.newTy = $2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000345 $$.oldTy = PackedTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000346 }
347 | '{' TypeListI '}' { // Structure type?
348 $2->insert(0, "{ ");
349 *$2 += " }";
Reid Spencere77e35e2006-12-01 20:26:20 +0000350 $$.newTy = $2;
351 $$.oldTy = StructTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000352 }
353 | '{' '}' { // Empty structure type?
Reid Spencer0b7e5072006-12-01 22:42:01 +0000354 $$.newTy = new std::string("{}");
Reid Spencere77e35e2006-12-01 20:26:20 +0000355 $$.oldTy = StructTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000356 }
357 | UpRTypes '*' { // Pointer type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000358 *$1.newTy += '*';
359 $1.oldTy = PointerTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000360 $$ = $1;
361 };
362
363// TypeList - Used for struct declarations and as a basis for function type
364// declaration type lists
365//
Reid Spencere77e35e2006-12-01 20:26:20 +0000366TypeListI
367 : UpRTypes {
368 $$ = $1.newTy;
369 }
370 | TypeListI ',' UpRTypes {
371 *$1 += ", " + *$3.newTy;
372 delete $3.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000373 $$ = $1;
374 };
375
376// ArgTypeList - List of types for a function type declaration...
Reid Spencere77e35e2006-12-01 20:26:20 +0000377ArgTypeListI
378 : TypeListI
Reid Spencere7c3c602006-11-30 06:36:44 +0000379 | TypeListI ',' DOTDOTDOT {
380 *$1 += ", ...";
381 delete $3;
382 $$ = $1;
383 }
384 | DOTDOTDOT {
385 $$ = $1;
386 }
387 | /*empty*/ {
388 $$ = new std::string();
389 };
390
391// ConstVal - The various declarations that go into the constant pool. This
392// production is used ONLY to represent constants that show up AFTER a 'const',
393// 'constant' or 'global' token at global scope. Constants that can be inlined
394// into other expressions (such as integers and constexprs) are handled by the
395// ResolvedVal, ValueRef and ConstValueRef productions.
396//
397ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencere77e35e2006-12-01 20:26:20 +0000398 $$.type = $1;
399 $$.cnst = new std::string(*$1.newTy);
400 *$$.cnst += " [ " + *$3 + " ]";
Reid Spencere7c3c602006-11-30 06:36:44 +0000401 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000402 }
403 | Types '[' ']' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000404 $$.type = $1;
405 $$.cnst = new std::string(*$1.newTy);
406 *$$.cnst += "[ ]";
Reid Spencere7c3c602006-11-30 06:36:44 +0000407 }
408 | Types 'c' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000409 $$.type = $1;
410 $$.cnst = new std::string(*$1.newTy);
411 *$$.cnst += " c" + *$3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000412 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000413 }
414 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencere77e35e2006-12-01 20:26:20 +0000415 $$.type = $1;
416 $$.cnst = new std::string(*$1.newTy);
417 *$$.cnst += " < " + *$3 + " >";
Reid Spencere7c3c602006-11-30 06:36:44 +0000418 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000419 }
420 | Types '{' ConstVector '}' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000421 $$.type = $1;
422 $$.cnst = new std::string(*$1.newTy);
423 *$$.cnst += " { " + *$3 + " }";
Reid Spencere7c3c602006-11-30 06:36:44 +0000424 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000425 }
426 | Types '{' '}' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000427 $$.type = $1;
428 $$.cnst = new std::string(*$1.newTy);
Reid Spencer0b7e5072006-12-01 22:42:01 +0000429 *$$.cnst += " {}";
Reid Spencere7c3c602006-11-30 06:36:44 +0000430 }
431 | Types NULL_TOK {
Reid Spencere77e35e2006-12-01 20:26:20 +0000432 $$.type = $1;
433 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000434 *$$.cnst += " " + *$2;
435 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000436 }
437 | Types UNDEF {
Reid Spencere77e35e2006-12-01 20:26:20 +0000438 $$.type = $1;
439 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000440 *$$.cnst += " " + *$2;
441 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000442 }
443 | Types SymbolicValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000444 $$.type = $1;
445 $$.cnst = new std::string(*$1.newTy);
446 *$$.cnst += " " + *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000447 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000448 }
449 | Types ConstExpr {
Reid Spencere77e35e2006-12-01 20:26:20 +0000450 $$.type = $1;
451 $$.cnst = new std::string(*$1.newTy);
452 *$$.cnst += " " + *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000453 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000454 }
455 | Types ZEROINITIALIZER {
Reid Spencere77e35e2006-12-01 20:26:20 +0000456 $$.type = $1;
457 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000458 *$$.cnst += " " + *$2;
459 delete $2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000460 }
461 | SIntType EInt64Val { // integral constants
462 $$.type = $1;
463 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000464 *$$.cnst += " " + *$2;
465 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000466 }
467 | UIntType EUINT64VAL { // integral constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000468 $$.type = $1;
469 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000470 *$$.cnst += " " + *$2;
471 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000472 }
473 | BOOL TRUETOK { // Boolean constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000474 $$.type = $1;
475 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000476 *$$.cnst += " " + *$2;
477 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000478 }
479 | BOOL FALSETOK { // Boolean constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000480 $$.type = $1;
481 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000482 *$$.cnst += " " + *$2;
483 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000484 }
485 | FPType FPVAL { // Float & Double constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000486 $$.type = $1;
487 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000488 *$$.cnst += " " + *$2;
489 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000490 };
491
492
Reid Spencerfcb5df82006-12-01 22:34:43 +0000493ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000494 // We must infer the cast opcode from the types of the operands.
Reid Spencerfcb5df82006-12-01 22:34:43 +0000495 const char *opcode = $1->c_str();
Reid Spencer280d8012006-12-01 23:40:53 +0000496 std::string source = *$3.cnst;
497 if (*$1 == "cast") {
498 std::string upgrade = getCastUpgrade(source, $3.type, $5, true);
499 opcode = getCastOpcode(source, $3.type, $5);
500 if (!upgrade.empty())
501 source = upgrade;
502 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000503 $$ = new std::string(opcode);
Reid Spencer280d8012006-12-01 23:40:53 +0000504 *$$ += "( " + source + " " + *$4 + " " + *$5.newTy + ")";
Reid Spencere77e35e2006-12-01 20:26:20 +0000505 delete $1; $3.destroy(); delete $4; $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000506 }
507 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000508 *$1 += "(" + *$3.cnst + " " + *$4 + ")";
509 $$ = $1;
510 $3.destroy();
511 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +0000512 }
513 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000514 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
515 $3.destroy(); $5.destroy(); $7.destroy();
516 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000517 }
518 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000519 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
520 $3.destroy(); $5.destroy();
521 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000522 }
523 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000524 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
525 $3.destroy(); $5.destroy();
526 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000527 }
528 | SetCondOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000529 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
530 $3.destroy(); $5.destroy();
531 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000532 }
533 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencerf7bde222006-12-01 22:26:37 +0000534 const char* shiftop = $1->c_str();
535 if (*$1 == "shr")
536 shiftop = ($3.type.isUnsigned()) ? "lshr" : "ashr";
537 $$ = new std::string(shiftop);
538 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
539 delete $1; $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000540 }
541 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000542 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
543 $3.destroy(); $5.destroy();
544 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000545 }
546 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000547 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
548 $3.destroy(); $5.destroy(); $7.destroy();
549 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000550 }
551 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000552 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
553 $3.destroy(); $5.destroy(); $7.destroy();
554 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000555 };
556
557
558// ConstVector - A list of comma separated constants.
Reid Spencere77e35e2006-12-01 20:26:20 +0000559
560ConstVector
561 : ConstVector ',' ConstVal {
562 *$1 += ", " + *$3.cnst;
563 $3.destroy();
564 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000565 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000566 | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
567 ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000568
569
570// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Reid Spencere77e35e2006-12-01 20:26:20 +0000571GlobalType : GLOBAL | CONSTANT ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000572
573
574//===----------------------------------------------------------------------===//
575// Rules to match Modules
576//===----------------------------------------------------------------------===//
577
578// Module rule: Capture the result of parsing the whole file into a result
579// variable...
580//
581Module : DefinitionList {
582};
583
584// DefinitionList - Top level definitions
585//
586DefinitionList : DefinitionList Function {
587 $$ = 0;
588 }
589 | DefinitionList FunctionProto {
590 *O << *$2 << "\n";
591 delete $2;
592 $$ = 0;
593 }
594 | DefinitionList MODULE ASM_TOK AsmBlock {
595 *O << "module asm " << " " << *$4 << "\n";
Reid Spencerd154b572006-12-01 20:36:40 +0000596 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000597 }
598 | DefinitionList IMPLEMENTATION {
599 *O << "implementation\n";
Reid Spencerd154b572006-12-01 20:36:40 +0000600 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000601 }
Reid Spencerd154b572006-12-01 20:36:40 +0000602 | ConstPool;
Reid Spencere7c3c602006-11-30 06:36:44 +0000603
604// ConstPool - Constants with optional names assigned to them.
605ConstPool : ConstPool OptAssign TYPE TypesV {
Reid Spencere77e35e2006-12-01 20:26:20 +0000606 *O << *$2 << " " << *$3 << " " << *$4.newTy << "\n";
607 // delete $2; delete $3; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000608 $$ = 0;
609 }
610 | ConstPool FunctionProto { // Function prototypes can be in const pool
611 *O << *$2 << "\n";
612 delete $2;
613 $$ = 0;
614 }
615 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
616 *O << *$2 << " " << *$3 << " " << *$4 << "\n";
617 delete $2; delete $3; delete $4;
618 $$ = 0;
619 }
620 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
Reid Spencere77e35e2006-12-01 20:26:20 +0000621 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.cnst << " "
622 << *$6 << "\n";
623 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000624 $$ = 0;
625 }
626 | ConstPool OptAssign EXTERNAL GlobalType Types GlobalVarAttributes {
Reid Spencere77e35e2006-12-01 20:26:20 +0000627 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.newTy
628 << " " << *$6 << "\n";
629 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000630 $$ = 0;
631 }
632 | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
Reid Spencere77e35e2006-12-01 20:26:20 +0000633 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.newTy
634 << " " << *$6 << "\n";
635 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000636 $$ = 0;
637 }
638 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
Reid Spencere77e35e2006-12-01 20:26:20 +0000639 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.newTy
640 << " " << *$6 << "\n";
641 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000642 $$ = 0;
643 }
644 | ConstPool TARGET TargetDefinition {
645 *O << *$2 << " " << *$3 << "\n";
646 delete $2; delete $3;
647 $$ = 0;
648 }
649 | ConstPool DEPLIBS '=' LibrariesDefinition {
650 *O << *$2 << " = " << *$4 << "\n";
651 delete $2; delete $4;
652 $$ = 0;
653 }
654 | /* empty: end of list */ {
655 $$ = 0;
656 };
657
658
659AsmBlock : STRINGCONSTANT ;
660
661BigOrLittle : BIG | LITTLE
662
663TargetDefinition
664 : ENDIAN '=' BigOrLittle {
Reid Spencere77e35e2006-12-01 20:26:20 +0000665 *$1 += " = " + *$3;
666 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000667 $$ = $1;
668 }
669 | POINTERSIZE '=' EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000670 *$1 += " = " + *$3;
671 if (*$3 == "64")
Reid Spencere77e35e2006-12-01 20:26:20 +0000672 SizeOfPointer = 64;
Reid Spencerf2d55322006-12-01 21:52:30 +0000673 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000674 $$ = $1;
675 }
676 | TRIPLE '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000677 *$1 += " = " + *$3;
678 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000679 $$ = $1;
680 }
681 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000682 *$1 += " = " + *$3;
683 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000684 $$ = $1;
685 };
686
687LibrariesDefinition
688 : '[' LibList ']' {
689 $2->insert(0, "[ ");
690 *$2 += " ]";
691 $$ = $2;
692 };
693
694LibList
695 : LibList ',' STRINGCONSTANT {
696 *$1 += ", " + *$3;
697 delete $3;
698 $$ = $1;
699 }
700 | STRINGCONSTANT
701 | /* empty: end of list */ {
702 $$ = new std::string();
703 };
704
705//===----------------------------------------------------------------------===//
706// Rules to match Function Headers
707//===----------------------------------------------------------------------===//
708
709Name : VAR_ID | STRINGCONSTANT;
710OptName : Name | /*empty*/ { $$ = new std::string(); };
711
712ArgVal : Types OptName {
Reid Spencere77e35e2006-12-01 20:26:20 +0000713 $$ = $1.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000714 if (!$2->empty())
715 *$$ += " " + *$2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000716 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000717};
718
719ArgListH : ArgListH ',' ArgVal {
720 *$1 += ", " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +0000721 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000722 }
723 | ArgVal {
724 $$ = $1;
725 };
726
727ArgList : ArgListH {
728 $$ = $1;
729 }
730 | ArgListH ',' DOTDOTDOT {
731 *$1 += ", ...";
732 $$ = $1;
Reid Spencere77e35e2006-12-01 20:26:20 +0000733 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000734 }
735 | DOTDOTDOT {
736 $$ = $1;
737 }
Reid Spencerd154b572006-12-01 20:36:40 +0000738 | /* empty */ { $$ = new std::string(); };
Reid Spencere7c3c602006-11-30 06:36:44 +0000739
740FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
741 OptSection OptAlign {
742 if (!$1->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000743 *$1 += " ";
Reid Spencere7c3c602006-11-30 06:36:44 +0000744 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000745 *$1 += *$2.newTy + " " + *$3 + "(" + *$5 + ")";
Reid Spencere7c3c602006-11-30 06:36:44 +0000746 if (!$7->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000747 *$1 += " " + *$7;
Reid Spencere7c3c602006-11-30 06:36:44 +0000748 }
749 if (!$8->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000750 *$1 += " " + *$8;
Reid Spencere7c3c602006-11-30 06:36:44 +0000751 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000752 $2.destroy();
753 delete $3;
754 delete $5;
755 delete $7;
756 delete $8;
757 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000758 };
759
760BEGIN : BEGINTOK {
761 $$ = new std::string("begin");
762 }
763 | '{' {
764 $$ = new std::string ("{");
765 }
766
767FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
768 if (!$1->empty()) {
769 *O << *$1 << " ";
770 }
771 *O << *$2 << " " << *$3 << "\n";
772 delete $1; delete $2; delete $3;
773 $$ = 0;
774};
775
776END : ENDTOK { $$ = new std::string("end"); }
777 | '}' { $$ = new std::string("}"); };
778
779Function : FunctionHeader BasicBlockList END {
780 if ($2)
781 *O << *$2;
782 *O << '\n' << *$3 << "\n";
Reid Spencere77e35e2006-12-01 20:26:20 +0000783 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000784};
785
Reid Spencere77e35e2006-12-01 20:26:20 +0000786FnDeclareLinkage
787 : /*default*/ { $$ = new std::string(); }
Reid Spencere7c3c602006-11-30 06:36:44 +0000788 | DLLIMPORT
789 | EXTERN_WEAK
790 ;
791
792FunctionProto
793 : DECLARE FnDeclareLinkage FunctionHeaderH {
Reid Spencere77e35e2006-12-01 20:26:20 +0000794 if (!$2->empty())
795 *$1 += " " + *$2;
796 *$1 += " " + *$3;
797 delete $2;
798 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000799 $$ = $1;
800 };
801
802//===----------------------------------------------------------------------===//
803// Rules to match Basic Blocks
804//===----------------------------------------------------------------------===//
805
Reid Spencerd154b572006-12-01 20:36:40 +0000806OptSideEffect : /* empty */ { $$ = new std::string(); }
807 | SIDEEFFECT;
Reid Spencere7c3c602006-11-30 06:36:44 +0000808
Reid Spencere77e35e2006-12-01 20:26:20 +0000809ConstValueRef
Reid Spencerf2d55322006-12-01 21:52:30 +0000810 : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK | NULL_TOK | UNDEF
811 | ZEROINITIALIZER
Reid Spencere7c3c602006-11-30 06:36:44 +0000812 | '<' ConstVector '>' {
813 $2->insert(0, "<");
814 *$2 += ">";
815 $$ = $2;
816 }
817 | ConstExpr
818 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
819 if (!$2->empty()) {
820 *$1 += " " + *$2;
821 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000822 *$1 += " " + *$3 + ", " + *$5;
823 delete $2; delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +0000824 $$ = $1;
825 };
826
Reid Spencerf2d55322006-12-01 21:52:30 +0000827SymbolicValueRef : IntVal | Name ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000828
829// ValueRef - A reference to a definition... either constant or symbolic
830ValueRef : SymbolicValueRef | ConstValueRef;
831
832
833// ResolvedVal - a <type> <value> pair. This is used only in cases where the
834// type immediately preceeds the value reference, and allows complex constant
835// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
836ResolvedVal : Types ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000837 $$.type = $1;
838 $$.val = new std::string(*$1.newTy + " ");
839 *$$.val += *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000840 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000841 };
842
843BasicBlockList : BasicBlockList BasicBlock {
Reid Spencerf2d55322006-12-01 21:52:30 +0000844 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000845 }
846 | BasicBlock { // Do not allow functions with 0 basic blocks
Reid Spencerf2d55322006-12-01 21:52:30 +0000847 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000848 };
849
850
851// Basic blocks are terminated by branching instructions:
852// br, br/cc, switch, ret
853//
Reid Spencer16244f42006-12-01 21:10:07 +0000854BasicBlock : InstructionList BBTerminatorInst {
Reid Spencerf2d55322006-12-01 21:52:30 +0000855 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000856 };
857
858InstructionList : InstructionList Inst {
859 *O << " " << *$2 << "\n";
860 delete $2;
861 $$ = 0;
862 }
863 | /* empty */ {
864 $$ = 0;
865 }
866 | LABELSTR {
867 *O << *$1 << "\n";
868 delete $1;
869 $$ = 0;
870 };
871
872BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencere77e35e2006-12-01 20:26:20 +0000873 *O << " " << *$1 << " " << *$2.val << "\n";
874 delete $1; $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000875 $$ = 0;
876 }
877 | RET VOID { // Return with no result...
Reid Spencere77e35e2006-12-01 20:26:20 +0000878 *O << " " << *$1 << " " << *$2.newTy << "\n";
879 delete $1; $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000880 $$ = 0;
881 }
882 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencere77e35e2006-12-01 20:26:20 +0000883 *O << " " << *$1 << " " << *$2.newTy << " " << *$3 << "\n";
884 delete $1; $2.destroy(); delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000885 $$ = 0;
886 } // Conditional Branch...
887 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000888 *O << " " << *$1 << " " << *$2.newTy << " " << *$3 << ", "
889 << *$5.newTy << " " << *$6 << ", " << *$8.newTy << " " << *$9 << "\n";
890 delete $1; $2.destroy(); delete $3; $5.destroy(); delete $6;
891 $8.destroy(); delete $9;
Reid Spencere7c3c602006-11-30 06:36:44 +0000892 $$ = 0;
893 }
894 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000895 *O << " " << *$1 << " " << *$2.newTy << " " << *$3 << ", " << *$5.newTy
896 << " " << *$6 << " [" << *$8 << " ]\n";
897 delete $1; $2.destroy(); delete $3; $5.destroy(); delete $6; delete $8;
Reid Spencere7c3c602006-11-30 06:36:44 +0000898 $$ = 0;
899 }
900 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000901 *O << " " << *$1 << " " << *$2.newTy << " " << *$3 << ", "
902 << *$5.newTy << " " << *$6 << "[]\n";
903 delete $1; $2.destroy(); delete $3; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000904 $$ = 0;
905 }
Reid Spencer16244f42006-12-01 21:10:07 +0000906 | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
Reid Spencere7c3c602006-11-30 06:36:44 +0000907 TO LABEL ValueRef UNWIND LABEL ValueRef {
Reid Spencer16244f42006-12-01 21:10:07 +0000908 *O << " ";
909 if (!$1->empty())
910 *O << *$1;
911 *O << *$2 << " " << *$3 << " " << *$4.newTy << " " << *$5 << " ("
912 << *$7 << ") " << *$9 << " " << *$10.newTy << " " << *$11 << " "
913 << *$12 << " " << *$13.newTy << " " << *$14 << "\n";
914 delete $1; delete $2; delete $3; $4.destroy(); delete $5; delete $7;
915 delete $9; $10.destroy(); delete $11; delete $12; $13.destroy();
916 delete $14;
Reid Spencere7c3c602006-11-30 06:36:44 +0000917 $$ = 0;
918 }
919 | UNWIND {
920 *O << " " << *$1 << "\n";
921 delete $1;
922 $$ = 0;
923 }
924 | UNREACHABLE {
925 *O << " " << *$1 << "\n";
926 delete $1;
927 $$ = 0;
928 };
929
930JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencer16244f42006-12-01 21:10:07 +0000931 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5.newTy + " " + *$6;
Reid Spencere77e35e2006-12-01 20:26:20 +0000932 $2.destroy(); delete $3; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000933 $$ = $1;
934 }
935 | IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000936 $2->insert(0, *$1.newTy + " " );
937 *$2 += ", " + *$4.newTy + " " + *$5;
938 $1.destroy(); $4.destroy(); delete $5;
939 $$ = $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000940 };
941
942Inst
943 : OptAssign InstVal {
944 *$1 += *$2;
945 delete $2;
946 $$ = $1;
947 };
948
949PHIList
950 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencere77e35e2006-12-01 20:26:20 +0000951 $3->insert(0, *$1.newTy + "[");
952 *$3 += "," + *$5 + "]";
953 $1.destroy(); delete $5;
954 $$ = $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000955 }
956 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
957 *$1 += ", [" + *$4 + "," + *$6 + "]";
958 delete $4; delete $6;
959 $$ = $1;
960 };
961
962
963ValueRefList
Reid Spencere77e35e2006-12-01 20:26:20 +0000964 : ResolvedVal { $$ = new std::string(*$1.val); $1.destroy(); }
Reid Spencere7c3c602006-11-30 06:36:44 +0000965 | ValueRefList ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +0000966 *$1 += ", " + *$3.val;
967 $3.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000968 $$ = $1;
969 };
970
971// ValueRefListE - Just like ValueRefList, except that it may also be empty!
972ValueRefListE
973 : ValueRefList
974 | /*empty*/ { $$ = new std::string(); }
975 ;
976
977OptTailCall
978 : TAIL CALL {
979 *$1 += " " + *$2;
980 delete $2;
981 $$ = $1;
982 }
983 | CALL
984 ;
985
986InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000987 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5;
988 $2.destroy(); delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +0000989 $$ = $1;
990 }
991 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000992 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5;
993 $2.destroy(); delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +0000994 $$ = $1;
995 }
996 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000997 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5;
998 $2.destroy(); delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +0000999 $$ = $1;
1000 }
1001 | NOT ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001002 *$1 += " " + *$2.val;
1003 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001004 $$ = $1;
1005 }
1006 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencerf7bde222006-12-01 22:26:37 +00001007 const char* shiftop = $1->c_str();
1008 if (*$1 == "shr")
1009 shiftop = ($2.type.isUnsigned()) ? "lshr" : "ashr";
1010 $$ = new std::string(shiftop);
1011 *$$ += " " + *$2.val + ", " + *$4.val;
1012 delete $1; $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001013 }
Reid Spencerfcb5df82006-12-01 22:34:43 +00001014 | CastOps ResolvedVal TO Types {
1015 const char *opcode = $1->c_str();
Reid Spencer280d8012006-12-01 23:40:53 +00001016 std::string source = *$2.val;
1017 if (*$1 == "cast") {
1018 std::string upgrade = getCastUpgrade(source, $2.type, $4, false);
1019 if (!upgrade.empty())
1020 *O << " " << upgrade << "\n";
1021 opcode = getCastOpcode(source, $2.type, $4);
1022 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001023 $$ = new std::string(opcode);
Reid Spencer280d8012006-12-01 23:40:53 +00001024 *$$ += " " + source + " " + *$3 + " " + *$4.newTy;
Reid Spencere77e35e2006-12-01 20:26:20 +00001025 delete $1; $2.destroy();
1026 delete $3; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001027 }
1028 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001029 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1030 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001031 $$ = $1;
1032 }
1033 | VAARG ResolvedVal ',' Types {
Reid Spencere77e35e2006-12-01 20:26:20 +00001034 *$1 += " " + *$2.val + ", " + *$4.newTy;
1035 $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001036 $$ = $1;
1037 }
1038 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001039 *$1 += " " + *$2.val + ", " + *$4.val;
1040 $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001041 $$ = $1;
1042 }
1043 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001044 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1045 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001046 $$ = $1;
1047 }
1048 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001049 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1050 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001051 $$ = $1;
1052 }
1053 | PHI_TOK PHIList {
1054 *$1 += " " + *$2;
1055 delete $2;
1056 $$ = $1;
1057 }
1058 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
1059 if (!$2->empty())
1060 *$1 += " " + *$2;
1061 if (!$1->empty())
1062 *$1 += " ";
Reid Spencere77e35e2006-12-01 20:26:20 +00001063 *$1 += *$3.newTy + " " + *$4 + "(" + *$6 + ")";
1064 delete $2; $3.destroy(); delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001065 $$ = $1;
1066 }
1067 | MemoryInst ;
1068
1069
1070// IndexList - List of indices for GEP based instructions...
1071IndexList
1072 : ',' ValueRefList {
1073 $2->insert(0, ", ");
1074 $$ = $2;
1075 }
1076 | /* empty */ { $$ = new std::string(); }
1077 ;
1078
1079OptVolatile
1080 : VOLATILE
1081 | /* empty */ { $$ = new std::string(); }
1082 ;
1083
1084MemoryInst : MALLOC Types OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001085 *$1 += " " + *$2.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +00001086 if (!$3->empty())
1087 *$1 += " " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001088 $2.destroy(); delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001089 $$ = $1;
1090 }
1091 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001092 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001093 if (!$6->empty())
1094 *$1 += " " + *$6;
Reid Spencere77e35e2006-12-01 20:26:20 +00001095 $2.destroy(); $4.destroy(); delete $5; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001096 $$ = $1;
1097 }
1098 | ALLOCA Types OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001099 *$1 += " " + *$2.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +00001100 if (!$3->empty())
1101 *$1 += " " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001102 $2.destroy(); delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001103 $$ = $1;
1104 }
1105 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001106 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001107 if (!$6->empty())
1108 *$1 += " " + *$6;
Reid Spencere77e35e2006-12-01 20:26:20 +00001109 $2.destroy(); $4.destroy(); delete $5; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001110 $$ = $1;
1111 }
1112 | FREE ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001113 *$1 += " " + *$2.val;
1114 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001115 $$ = $1;
1116 }
1117 | OptVolatile LOAD Types ValueRef {
1118 if (!$1->empty())
1119 *$1 += " ";
Reid Spencere77e35e2006-12-01 20:26:20 +00001120 *$1 += *$2 + " " + *$3.newTy + " " + *$4;
1121 delete $2; $3.destroy(); delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001122 $$ = $1;
1123 }
1124 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1125 if (!$1->empty())
1126 *$1 += " ";
Reid Spencere77e35e2006-12-01 20:26:20 +00001127 *$1 += *$2 + " " + *$3.val + ", " + *$5.newTy + " " + *$6;
1128 delete $2; $3.destroy(); $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001129 $$ = $1;
1130 }
1131 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencere77e35e2006-12-01 20:26:20 +00001132 *$1 += *$2.newTy + " " + *$3 + " " + *$4;
1133 $2.destroy(); delete $3; delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001134 $$ = $1;
1135 };
1136
1137%%
1138
1139int yyerror(const char *ErrorMsg) {
1140 std::string where
1141 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1142 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
1143 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
1144 if (yychar == YYEMPTY || yychar == 0)
1145 errMsg += "end-of-file.";
1146 else
1147 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
1148 std::cerr << errMsg << '\n';
1149 exit(1);
1150}