blob: f82b9c6d4bf9fb8fbad93e7eb2804b56a763e8e2 [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 Spencere77e35e2006-12-01 20:26:20 +000050const char* getCastOpcode(TypeInfo& SrcTy, TypeInfo&DstTy) {
51 unsigned SrcBits = SrcTy.getBitWidth();
52 unsigned DstBits = DstTy.getBitWidth();
53 const char* opcode = "bitcast";
54 // Run through the possibilities ...
55 if (DstTy.isIntegral()) { // Casting to integral
56 if (SrcTy.isIntegral()) { // Casting from integral
57 if (DstBits < SrcBits)
58 opcode = "trunc";
59 else if (DstBits > SrcBits) { // its an extension
60 if (SrcTy.isSigned())
61 opcode ="sext"; // signed -> SEXT
62 else
63 opcode = "zext"; // unsigned -> ZEXT
64 } else {
65 opcode = "bitcast"; // Same size, No-op cast
66 }
67 } else if (SrcTy.isFloatingPoint()) { // Casting from floating pt
68 if (DstTy.isSigned())
69 opcode = "fptosi"; // FP -> sint
70 else
71 opcode = "fptoui"; // FP -> uint
72 } else if (SrcTy.isPacked()) {
73 assert(DstBits == SrcTy.getBitWidth() &&
74 "Casting packed to integer of different width");
75 opcode = "bitcast"; // same size, no-op cast
76 } else {
77 assert(SrcTy.isPointer() &&
78 "Casting from a value that is not first-class type");
79 opcode = "ptrtoint"; // ptr -> int
80 }
81 } else if (DstTy.isFloatingPoint()) { // Casting to floating pt
82 if (SrcTy.isIntegral()) { // Casting from integral
83 if (SrcTy.isSigned())
84 opcode = "sitofp"; // sint -> FP
85 else
86 opcode = "uitofp"; // uint -> FP
87 } else if (SrcTy.isFloatingPoint()) { // Casting from floating pt
88 if (DstBits < SrcBits) {
89 opcode = "fptrunc"; // FP -> smaller FP
90 } else if (DstBits > SrcBits) {
91 opcode = "fpext"; // FP -> larger FP
92 } else {
93 opcode ="bitcast"; // same size, no-op cast
94 }
95 } else if (SrcTy.isPacked()) {
96 assert(DstBits == SrcTy.getBitWidth() &&
97 "Casting packed to floating point of different width");
98 opcode = "bitcast"; // same size, no-op cast
99 } else {
100 assert(0 && "Casting pointer or non-first class to float");
101 }
102 } else if (DstTy.isPacked()) {
103 if (SrcTy.isPacked()) {
104 assert(DstTy.getBitWidth() == SrcTy.getBitWidth() &&
105 "Casting packed to packed of different widths");
106 opcode = "bitcast"; // packed -> packed
107 } else if (DstTy.getBitWidth() == SrcBits) {
108 opcode = "bitcast"; // float/int -> packed
109 } else {
110 assert(!"Illegal cast to packed (wrong type or size)");
111 }
112 } else if (DstTy.isPointer()) {
113 if (SrcTy.isPointer()) {
114 opcode = "bitcast"; // ptr -> ptr
115 } else if (SrcTy.isIntegral()) {
116 opcode = "inttoptr"; // int -> ptr
117 } else {
118 assert(!"Casting pointer to other than pointer or int");
119 }
120 } else {
121 assert(!"Casting to type that is not first-class");
122 }
123 return opcode;
124}
125
Reid Spencere7c3c602006-11-30 06:36:44 +0000126%}
127
Reid Spencere77e35e2006-12-01 20:26:20 +0000128%file-prefix="UpgradeParser"
129
130%union {
131 std::string* String;
132 TypeInfo Type;
133 ValueInfo Value;
134 ConstInfo Const;
135}
136
137%token <Const> ESINT64VAL EUINT64VAL SINTVAL UINTVAL FPVAL TRUETOK FALSETOK
138%token <Const> NULL_TOK UNDEF ZEROINITIALIZER
139
140%token <Type> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
141%token <Type> FLOAT DOUBLE LABEL OPAQUE
142
143%token <String> TYPE VAR_ID LABELSTR STRINGCONSTANT
144%token <String> IMPLEMENTATION BEGINTOK ENDTOK
145%token <String> DECLARE GLOBAL CONSTANT SECTION VOLATILE
146%token <String> TO DOTDOTDOT CONST INTERNAL LINKONCE WEAK
147%token <String> DLLIMPORT DLLEXPORT EXTERN_WEAK APPENDING
148%token <String> NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
149%token <String> ALIGN
150%token <String> DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
151%token <String> CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
152%token <String> X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
153%token <String> DATALAYOUT
154%token <String> RET BR SWITCH INVOKE UNWIND UNREACHABLE
155%token <String> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
156%token <String> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
157%token <String> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
158%token <String> PHI_TOK SELECT SHL LSHR ASHR VAARG
159%token <String> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
160%token <String> CAST
161
162%type <String> OptAssign OptLinkage OptCallingConv OptAlign OptCAlign
163%type <String> SectionString OptSection GlobalVarAttributes GlobalVarAttribute
164%type <String> ArgTypeListI ConstExpr DefinitionList
165%type <String> ConstPool TargetDefinition LibrariesDefinition LibList OptName
166%type <String> ArgVal ArgListH ArgList FunctionHeaderH BEGIN FunctionHeader END
167%type <String> Function FunctionProto BasicBlock TypeListI
168%type <String> InstructionList BBTerminatorInst JumpTable Inst PHIList
169%type <String> ValueRefList OptTailCall InstVal IndexList OptVolatile
170%type <String> MemoryInst SymbolicValueRef OptSideEffect GlobalType
171%type <String> FnDeclareLinkage BasicBlockList BigOrLittle AsmBlock
172%type <String> Name ValueRef ValueRefListE
173%type <String> ShiftOps SetCondOps LogicalOps ArithmeticOps ConstValueRef
174
175%type <String> ConstVector
176
177%type <Type> IntType SIntType UIntType FPType TypesV Types
178%type <Type> PrimType UpRTypesV UpRTypes
179
180%type <Const> IntVal EInt64Val ConstVal
181
182%type <Value> ResolvedVal
Reid Spencere7c3c602006-11-30 06:36:44 +0000183
184%start Module
185
186%%
187
188// Handle constant integer size restriction and conversion...
Reid Spencere77e35e2006-12-01 20:26:20 +0000189IntVal : SINTVAL | UINTVAL
190EInt64Val : ESINT64VAL | EUINT64VAL;
Reid Spencere7c3c602006-11-30 06:36:44 +0000191
192// Operations that are notably excluded from this list include:
193// RET, BR, & SWITCH because they end basic blocks and are treated specially.
194ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
195LogicalOps : AND | OR | XOR;
196SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Reid Spencere7c3c602006-11-30 06:36:44 +0000197ShiftOps : SHL | LSHR | ASHR;
198
199// These are some types that allow classification if we only want a particular
200// thing... for example, only a signed, unsigned, or integral type.
201SIntType : LONG | INT | SHORT | SBYTE;
202UIntType : ULONG | UINT | USHORT | UBYTE;
203IntType : SIntType | UIntType;
204FPType : FLOAT | DOUBLE;
205
206// OptAssign - Value producing statements have an optional assignment component
207OptAssign : Name '=' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000208 *$1 += " = ";
Reid Spencere7c3c602006-11-30 06:36:44 +0000209 $$ = $1;
210 }
211 | /*empty*/ {
212 $$ = new std::string("");
213 };
214
215OptLinkage
216 : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT
217 | EXTERN_WEAK
218 | /*empty*/ { $$ = new std::string(""); } ;
219
220OptCallingConv
221 : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK
Reid Spencer16244f42006-12-01 21:10:07 +0000222 | X86_FASTCALLCC_TOK
223 | CC_TOK EUINT64VAL {
224 *$1 += *$2.cnst;
225 $2.destroy();
226 $$ = $1;
227 }
Reid Spencere7c3c602006-11-30 06:36:44 +0000228 | /*empty*/ { $$ = new std::string(""); } ;
229
230// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
231// a comma before it.
232OptAlign
233 : /*empty*/ { $$ = new std::string(); }
Reid Spencere77e35e2006-12-01 20:26:20 +0000234 | ALIGN EUINT64VAL { *$1 += " " + *$2.cnst; delete $2.cnst; $$ = $1; };
Reid Spencere7c3c602006-11-30 06:36:44 +0000235 ;
236OptCAlign
237 : /*empty*/ { $$ = new std::string(); }
238 | ',' ALIGN EUINT64VAL {
239 $2->insert(0, ", ");
Reid Spencere77e35e2006-12-01 20:26:20 +0000240 *$2 += " " + *$3.cnst;
241 delete $3.cnst;
Reid Spencere7c3c602006-11-30 06:36:44 +0000242 $$ = $2;
243 };
244
245SectionString
246 : SECTION STRINGCONSTANT {
247 *$1 += " " + *$2;
248 delete $2;
249 $$ = $1;
250 };
251
252OptSection : /*empty*/ { $$ = new std::string(); }
253 | SectionString;
254
255GlobalVarAttributes
256 : /* empty */ { $$ = new std::string(); }
257 | ',' GlobalVarAttribute GlobalVarAttributes {
258 $2->insert(0, ", ");
259 if (!$3->empty())
260 *$2 += " " + *$3;
261 delete $3;
262 $$ = $2;
263 };
264
265GlobalVarAttribute
266 : SectionString
267 | ALIGN EUINT64VAL {
Reid Spencere77e35e2006-12-01 20:26:20 +0000268 *$1 += " " + *$2.cnst;
269 delete $2.cnst;
Reid Spencere7c3c602006-11-30 06:36:44 +0000270 $$ = $1;
271 };
272
273//===----------------------------------------------------------------------===//
274// Types includes all predefined types... except void, because it can only be
275// used in specific contexts (function returning void for example). To have
276// access to it, a user must explicitly use TypesV.
277//
278
279// TypesV includes all of 'Types', but it also includes the void type.
280TypesV : Types | VOID ;
281UpRTypesV : UpRTypes | VOID ;
282Types : UpRTypes ;
283
284// Derived types are added later...
285//
286PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
Reid Spencere77e35e2006-12-01 20:26:20 +0000287PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
Reid Spencerd154b572006-12-01 20:36:40 +0000288UpRTypes : OPAQUE | PrimType
289 | SymbolicValueRef {
290 $$.newTy = $1; $$.oldTy = OpaqueTy;
291 };
Reid Spencere7c3c602006-11-30 06:36:44 +0000292
293// Include derived types in the Types production.
294//
295UpRTypes : '\\' EUINT64VAL { // Type UpReference
Reid Spencere77e35e2006-12-01 20:26:20 +0000296 $2.cnst->insert(0, "\\");
297 $$.newTy = $2.cnst;
298 $$.oldTy = OpaqueTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000299 }
300 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000301 *$1.newTy += "( " + *$3 + " )";
Reid Spencere7c3c602006-11-30 06:36:44 +0000302 delete $3;
Reid Spencere77e35e2006-12-01 20:26:20 +0000303 $$.newTy = $1.newTy;
304 $$.oldTy = FunctionTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000305 }
306 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000307 $2.cnst->insert(0,"[ ");
308 *$2.cnst += " x " + *$4.newTy + " ]";
309 delete $4.newTy;
310 $$.newTy = $2.cnst;
311 $$.oldTy = ArrayTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000312 }
313 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000314 $2.cnst->insert(0,"< ");
315 *$2.cnst += " x " + *$4.newTy + " >";
316 delete $4.newTy;
317 $$.newTy = $2.cnst;
318 $$.oldTy = PackedTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000319 }
320 | '{' TypeListI '}' { // Structure type?
321 $2->insert(0, "{ ");
322 *$2 += " }";
Reid Spencere77e35e2006-12-01 20:26:20 +0000323 $$.newTy = $2;
324 $$.oldTy = StructTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000325 }
326 | '{' '}' { // Empty structure type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000327 $$.newTy = new std::string("{ }");
328 $$.oldTy = StructTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000329 }
330 | UpRTypes '*' { // Pointer type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000331 *$1.newTy += '*';
332 $1.oldTy = PointerTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000333 $$ = $1;
334 };
335
336// TypeList - Used for struct declarations and as a basis for function type
337// declaration type lists
338//
Reid Spencere77e35e2006-12-01 20:26:20 +0000339TypeListI
340 : UpRTypes {
341 $$ = $1.newTy;
342 }
343 | TypeListI ',' UpRTypes {
344 *$1 += ", " + *$3.newTy;
345 delete $3.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000346 $$ = $1;
347 };
348
349// ArgTypeList - List of types for a function type declaration...
Reid Spencere77e35e2006-12-01 20:26:20 +0000350ArgTypeListI
351 : TypeListI
Reid Spencere7c3c602006-11-30 06:36:44 +0000352 | TypeListI ',' DOTDOTDOT {
353 *$1 += ", ...";
354 delete $3;
355 $$ = $1;
356 }
357 | DOTDOTDOT {
358 $$ = $1;
359 }
360 | /*empty*/ {
361 $$ = new std::string();
362 };
363
364// ConstVal - The various declarations that go into the constant pool. This
365// production is used ONLY to represent constants that show up AFTER a 'const',
366// 'constant' or 'global' token at global scope. Constants that can be inlined
367// into other expressions (such as integers and constexprs) are handled by the
368// ResolvedVal, ValueRef and ConstValueRef productions.
369//
370ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencere77e35e2006-12-01 20:26:20 +0000371 $$.type = $1;
372 $$.cnst = new std::string(*$1.newTy);
373 *$$.cnst += " [ " + *$3 + " ]";
Reid Spencere7c3c602006-11-30 06:36:44 +0000374 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000375 }
376 | Types '[' ']' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000377 $$.type = $1;
378 $$.cnst = new std::string(*$1.newTy);
379 *$$.cnst += "[ ]";
Reid Spencere7c3c602006-11-30 06:36:44 +0000380 }
381 | Types 'c' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000382 $$.type = $1;
383 $$.cnst = new std::string(*$1.newTy);
384 *$$.cnst += " c" + *$3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000385 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000386 }
387 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencere77e35e2006-12-01 20:26:20 +0000388 $$.type = $1;
389 $$.cnst = new std::string(*$1.newTy);
390 *$$.cnst += " < " + *$3 + " >";
Reid Spencere7c3c602006-11-30 06:36:44 +0000391 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000392 }
393 | Types '{' ConstVector '}' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000394 $$.type = $1;
395 $$.cnst = new std::string(*$1.newTy);
396 *$$.cnst += " { " + *$3 + " }";
Reid Spencere7c3c602006-11-30 06:36:44 +0000397 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000398 }
399 | Types '{' '}' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000400 $$.type = $1;
401 $$.cnst = new std::string(*$1.newTy);
402 *$$.cnst += " [ ]";
Reid Spencere7c3c602006-11-30 06:36:44 +0000403 }
404 | Types NULL_TOK {
Reid Spencere77e35e2006-12-01 20:26:20 +0000405 $$.type = $1;
406 $$.cnst = new std::string(*$1.newTy);
407 *$$.cnst += " " + *$2.cnst;
408 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000409 }
410 | Types UNDEF {
Reid Spencere77e35e2006-12-01 20:26:20 +0000411 $$.type = $1;
412 $$.cnst = new std::string(*$1.newTy);
413 *$$.cnst += " " + *$2.cnst;
414 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000415 }
416 | Types SymbolicValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000417 $$.type = $1;
418 $$.cnst = new std::string(*$1.newTy);
419 *$$.cnst += " " + *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000420 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000421 }
422 | Types ConstExpr {
Reid Spencere77e35e2006-12-01 20:26:20 +0000423 $$.type = $1;
424 $$.cnst = new std::string(*$1.newTy);
425 *$$.cnst += " " + *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000426 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000427 }
428 | Types ZEROINITIALIZER {
Reid Spencere77e35e2006-12-01 20:26:20 +0000429 $$.type = $1;
430 $$.cnst = new std::string(*$1.newTy);
431 *$$.cnst += " " + *$2.cnst;
432 $2.destroy();
433 }
434 | SIntType EInt64Val { // integral constants
435 $$.type = $1;
436 $$.cnst = new std::string(*$1.newTy);
437 *$$.cnst += " " + *$2.cnst;
438 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000439 }
440 | UIntType EUINT64VAL { // integral constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000441 $$.type = $1;
442 $$.cnst = new std::string(*$1.newTy);
443 *$$.cnst += " " + *$2.cnst;
444 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000445 }
446 | BOOL TRUETOK { // Boolean constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000447 $$.type = $1;
448 $$.cnst = new std::string(*$1.newTy);
449 *$$.cnst += " " + *$2.cnst;
450 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000451 }
452 | BOOL FALSETOK { // Boolean constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000453 $$.type = $1;
454 $$.cnst = new std::string(*$1.newTy);
455 *$$.cnst += " " + *$2.cnst;
456 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000457 }
458 | FPType FPVAL { // Float & Double constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000459 $$.type = $1;
460 $$.cnst = new std::string(*$1.newTy);
461 *$$.cnst += " " + *$2.cnst;
462 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000463 };
464
465
Reid Spencere77e35e2006-12-01 20:26:20 +0000466ConstExpr: CAST '(' ConstVal TO Types ')' {
467 // We must infer the cast opcode from the types of the operands.
468 const char *opcode = getCastOpcode($3.type, $5);
469 $$ = new std::string(opcode);
470 *$$ += "(" + *$3.cnst + " " + *$4 + " " + *$5.newTy + ")";
471 delete $1; $3.destroy(); delete $4; $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000472 }
473 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000474 *$1 += "(" + *$3.cnst + " " + *$4 + ")";
475 $$ = $1;
476 $3.destroy();
477 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +0000478 }
479 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000480 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
481 $3.destroy(); $5.destroy(); $7.destroy();
482 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000483 }
484 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000485 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
486 $3.destroy(); $5.destroy();
487 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000488 }
489 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000490 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
491 $3.destroy(); $5.destroy();
492 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000493 }
494 | SetCondOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000495 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
496 $3.destroy(); $5.destroy();
497 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000498 }
499 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000500 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
501 $3.destroy(); $5.destroy();
502 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000503 }
504 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000505 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
506 $3.destroy(); $5.destroy();
507 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000508 }
509 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000510 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
511 $3.destroy(); $5.destroy(); $7.destroy();
512 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000513 }
514 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000515 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
516 $3.destroy(); $5.destroy(); $7.destroy();
517 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000518 };
519
520
521// ConstVector - A list of comma separated constants.
Reid Spencere77e35e2006-12-01 20:26:20 +0000522
523ConstVector
524 : ConstVector ',' ConstVal {
525 *$1 += ", " + *$3.cnst;
526 $3.destroy();
527 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000528 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000529 | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
530 ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000531
532
533// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Reid Spencere77e35e2006-12-01 20:26:20 +0000534GlobalType : GLOBAL | CONSTANT ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000535
536
537//===----------------------------------------------------------------------===//
538// Rules to match Modules
539//===----------------------------------------------------------------------===//
540
541// Module rule: Capture the result of parsing the whole file into a result
542// variable...
543//
544Module : DefinitionList {
545};
546
547// DefinitionList - Top level definitions
548//
549DefinitionList : DefinitionList Function {
550 $$ = 0;
551 }
552 | DefinitionList FunctionProto {
553 *O << *$2 << "\n";
554 delete $2;
555 $$ = 0;
556 }
557 | DefinitionList MODULE ASM_TOK AsmBlock {
558 *O << "module asm " << " " << *$4 << "\n";
Reid Spencerd154b572006-12-01 20:36:40 +0000559 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000560 }
561 | DefinitionList IMPLEMENTATION {
562 *O << "implementation\n";
Reid Spencerd154b572006-12-01 20:36:40 +0000563 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000564 }
Reid Spencerd154b572006-12-01 20:36:40 +0000565 | ConstPool;
Reid Spencere7c3c602006-11-30 06:36:44 +0000566
567// ConstPool - Constants with optional names assigned to them.
568ConstPool : ConstPool OptAssign TYPE TypesV {
Reid Spencere77e35e2006-12-01 20:26:20 +0000569 *O << *$2 << " " << *$3 << " " << *$4.newTy << "\n";
570 // delete $2; delete $3; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000571 $$ = 0;
572 }
573 | ConstPool FunctionProto { // Function prototypes can be in const pool
574 *O << *$2 << "\n";
575 delete $2;
576 $$ = 0;
577 }
578 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
579 *O << *$2 << " " << *$3 << " " << *$4 << "\n";
580 delete $2; delete $3; delete $4;
581 $$ = 0;
582 }
583 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
Reid Spencere77e35e2006-12-01 20:26:20 +0000584 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.cnst << " "
585 << *$6 << "\n";
586 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000587 $$ = 0;
588 }
589 | ConstPool OptAssign EXTERNAL GlobalType Types GlobalVarAttributes {
Reid Spencere77e35e2006-12-01 20:26:20 +0000590 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.newTy
591 << " " << *$6 << "\n";
592 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000593 $$ = 0;
594 }
595 | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
Reid Spencere77e35e2006-12-01 20:26:20 +0000596 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.newTy
597 << " " << *$6 << "\n";
598 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000599 $$ = 0;
600 }
601 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
Reid Spencere77e35e2006-12-01 20:26:20 +0000602 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.newTy
603 << " " << *$6 << "\n";
604 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000605 $$ = 0;
606 }
607 | ConstPool TARGET TargetDefinition {
608 *O << *$2 << " " << *$3 << "\n";
609 delete $2; delete $3;
610 $$ = 0;
611 }
612 | ConstPool DEPLIBS '=' LibrariesDefinition {
613 *O << *$2 << " = " << *$4 << "\n";
614 delete $2; delete $4;
615 $$ = 0;
616 }
617 | /* empty: end of list */ {
618 $$ = 0;
619 };
620
621
622AsmBlock : STRINGCONSTANT ;
623
624BigOrLittle : BIG | LITTLE
625
626TargetDefinition
627 : ENDIAN '=' BigOrLittle {
Reid Spencere77e35e2006-12-01 20:26:20 +0000628 *$1 += " = " + *$3;
629 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000630 $$ = $1;
631 }
632 | POINTERSIZE '=' EUINT64VAL {
Reid Spencere77e35e2006-12-01 20:26:20 +0000633 *$1 += " = " + *$3.cnst;
634 if (*$3.cnst == "64")
635 SizeOfPointer = 64;
636 $3.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000637 $$ = $1;
638 }
639 | TRIPLE '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000640 *$1 += " = " + *$3;
641 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000642 $$ = $1;
643 }
644 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000645 *$1 += " = " + *$3;
646 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000647 $$ = $1;
648 };
649
650LibrariesDefinition
651 : '[' LibList ']' {
652 $2->insert(0, "[ ");
653 *$2 += " ]";
654 $$ = $2;
655 };
656
657LibList
658 : LibList ',' STRINGCONSTANT {
659 *$1 += ", " + *$3;
660 delete $3;
661 $$ = $1;
662 }
663 | STRINGCONSTANT
664 | /* empty: end of list */ {
665 $$ = new std::string();
666 };
667
668//===----------------------------------------------------------------------===//
669// Rules to match Function Headers
670//===----------------------------------------------------------------------===//
671
672Name : VAR_ID | STRINGCONSTANT;
673OptName : Name | /*empty*/ { $$ = new std::string(); };
674
675ArgVal : Types OptName {
Reid Spencere77e35e2006-12-01 20:26:20 +0000676 $$ = $1.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000677 if (!$2->empty())
678 *$$ += " " + *$2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000679 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000680};
681
682ArgListH : ArgListH ',' ArgVal {
683 *$1 += ", " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +0000684 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000685 }
686 | ArgVal {
687 $$ = $1;
688 };
689
690ArgList : ArgListH {
691 $$ = $1;
692 }
693 | ArgListH ',' DOTDOTDOT {
694 *$1 += ", ...";
695 $$ = $1;
Reid Spencere77e35e2006-12-01 20:26:20 +0000696 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000697 }
698 | DOTDOTDOT {
699 $$ = $1;
700 }
Reid Spencerd154b572006-12-01 20:36:40 +0000701 | /* empty */ { $$ = new std::string(); };
Reid Spencere7c3c602006-11-30 06:36:44 +0000702
703FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
704 OptSection OptAlign {
705 if (!$1->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000706 *$1 += " ";
Reid Spencere7c3c602006-11-30 06:36:44 +0000707 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000708 *$1 += *$2.newTy + " " + *$3 + "(" + *$5 + ")";
Reid Spencere7c3c602006-11-30 06:36:44 +0000709 if (!$7->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000710 *$1 += " " + *$7;
Reid Spencere7c3c602006-11-30 06:36:44 +0000711 }
712 if (!$8->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000713 *$1 += " " + *$8;
Reid Spencere7c3c602006-11-30 06:36:44 +0000714 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000715 $2.destroy();
716 delete $3;
717 delete $5;
718 delete $7;
719 delete $8;
720 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000721 };
722
723BEGIN : BEGINTOK {
724 $$ = new std::string("begin");
725 }
726 | '{' {
727 $$ = new std::string ("{");
728 }
729
730FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
731 if (!$1->empty()) {
732 *O << *$1 << " ";
733 }
734 *O << *$2 << " " << *$3 << "\n";
735 delete $1; delete $2; delete $3;
736 $$ = 0;
737};
738
739END : ENDTOK { $$ = new std::string("end"); }
740 | '}' { $$ = new std::string("}"); };
741
742Function : FunctionHeader BasicBlockList END {
743 if ($2)
744 *O << *$2;
745 *O << '\n' << *$3 << "\n";
Reid Spencere77e35e2006-12-01 20:26:20 +0000746 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000747};
748
Reid Spencere77e35e2006-12-01 20:26:20 +0000749FnDeclareLinkage
750 : /*default*/ { $$ = new std::string(); }
Reid Spencere7c3c602006-11-30 06:36:44 +0000751 | DLLIMPORT
752 | EXTERN_WEAK
753 ;
754
755FunctionProto
756 : DECLARE FnDeclareLinkage FunctionHeaderH {
Reid Spencere77e35e2006-12-01 20:26:20 +0000757 if (!$2->empty())
758 *$1 += " " + *$2;
759 *$1 += " " + *$3;
760 delete $2;
761 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000762 $$ = $1;
763 };
764
765//===----------------------------------------------------------------------===//
766// Rules to match Basic Blocks
767//===----------------------------------------------------------------------===//
768
Reid Spencerd154b572006-12-01 20:36:40 +0000769OptSideEffect : /* empty */ { $$ = new std::string(); }
770 | SIDEEFFECT;
Reid Spencere7c3c602006-11-30 06:36:44 +0000771
Reid Spencere77e35e2006-12-01 20:26:20 +0000772ConstValueRef
773 : ESINT64VAL { $$ = $1.cnst; }
774 | EUINT64VAL { $$ = $1.cnst; }
775 | FPVAL { $$ = $1.cnst; }
776 | TRUETOK { $$ = $1.cnst; }
777 | FALSETOK { $$ = $1.cnst; }
778 | NULL_TOK { $$ = $1.cnst; }
779 | UNDEF { $$ = $1.cnst; }
780 | ZEROINITIALIZER { $$ = $1.cnst; }
Reid Spencere7c3c602006-11-30 06:36:44 +0000781 | '<' ConstVector '>' {
782 $2->insert(0, "<");
783 *$2 += ">";
784 $$ = $2;
785 }
786 | ConstExpr
787 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
788 if (!$2->empty()) {
789 *$1 += " " + *$2;
790 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000791 *$1 += " " + *$3 + ", " + *$5;
792 delete $2; delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +0000793 $$ = $1;
794 };
795
Reid Spencere77e35e2006-12-01 20:26:20 +0000796SymbolicValueRef : IntVal { $$ = $1.cnst; } | Name ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000797
798// ValueRef - A reference to a definition... either constant or symbolic
799ValueRef : SymbolicValueRef | ConstValueRef;
800
801
802// ResolvedVal - a <type> <value> pair. This is used only in cases where the
803// type immediately preceeds the value reference, and allows complex constant
804// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
805ResolvedVal : Types ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000806 $$.type = $1;
807 $$.val = new std::string(*$1.newTy + " ");
808 *$$.val += *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000809 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000810 };
811
812BasicBlockList : BasicBlockList BasicBlock {
813 }
814 | BasicBlock { // Do not allow functions with 0 basic blocks
815 };
816
817
818// Basic blocks are terminated by branching instructions:
819// br, br/cc, switch, ret
820//
Reid Spencer16244f42006-12-01 21:10:07 +0000821BasicBlock : InstructionList BBTerminatorInst {
Reid Spencere7c3c602006-11-30 06:36:44 +0000822 *O << *$2 ;
823 };
824
825InstructionList : InstructionList Inst {
826 *O << " " << *$2 << "\n";
827 delete $2;
828 $$ = 0;
829 }
830 | /* empty */ {
831 $$ = 0;
832 }
833 | LABELSTR {
834 *O << *$1 << "\n";
835 delete $1;
836 $$ = 0;
837 };
838
839BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencere77e35e2006-12-01 20:26:20 +0000840 *O << " " << *$1 << " " << *$2.val << "\n";
841 delete $1; $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000842 $$ = 0;
843 }
844 | RET VOID { // Return with no result...
Reid Spencere77e35e2006-12-01 20:26:20 +0000845 *O << " " << *$1 << " " << *$2.newTy << "\n";
846 delete $1; $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000847 $$ = 0;
848 }
849 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencere77e35e2006-12-01 20:26:20 +0000850 *O << " " << *$1 << " " << *$2.newTy << " " << *$3 << "\n";
851 delete $1; $2.destroy(); delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000852 $$ = 0;
853 } // Conditional Branch...
854 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000855 *O << " " << *$1 << " " << *$2.newTy << " " << *$3 << ", "
856 << *$5.newTy << " " << *$6 << ", " << *$8.newTy << " " << *$9 << "\n";
857 delete $1; $2.destroy(); delete $3; $5.destroy(); delete $6;
858 $8.destroy(); delete $9;
Reid Spencere7c3c602006-11-30 06:36:44 +0000859 $$ = 0;
860 }
861 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000862 *O << " " << *$1 << " " << *$2.newTy << " " << *$3 << ", " << *$5.newTy
863 << " " << *$6 << " [" << *$8 << " ]\n";
864 delete $1; $2.destroy(); delete $3; $5.destroy(); delete $6; delete $8;
Reid Spencere7c3c602006-11-30 06:36:44 +0000865 $$ = 0;
866 }
867 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000868 *O << " " << *$1 << " " << *$2.newTy << " " << *$3 << ", "
869 << *$5.newTy << " " << *$6 << "[]\n";
870 delete $1; $2.destroy(); delete $3; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000871 $$ = 0;
872 }
Reid Spencer16244f42006-12-01 21:10:07 +0000873 | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
Reid Spencere7c3c602006-11-30 06:36:44 +0000874 TO LABEL ValueRef UNWIND LABEL ValueRef {
Reid Spencer16244f42006-12-01 21:10:07 +0000875 *O << " ";
876 if (!$1->empty())
877 *O << *$1;
878 *O << *$2 << " " << *$3 << " " << *$4.newTy << " " << *$5 << " ("
879 << *$7 << ") " << *$9 << " " << *$10.newTy << " " << *$11 << " "
880 << *$12 << " " << *$13.newTy << " " << *$14 << "\n";
881 delete $1; delete $2; delete $3; $4.destroy(); delete $5; delete $7;
882 delete $9; $10.destroy(); delete $11; delete $12; $13.destroy();
883 delete $14;
Reid Spencere7c3c602006-11-30 06:36:44 +0000884 $$ = 0;
885 }
886 | UNWIND {
887 *O << " " << *$1 << "\n";
888 delete $1;
889 $$ = 0;
890 }
891 | UNREACHABLE {
892 *O << " " << *$1 << "\n";
893 delete $1;
894 $$ = 0;
895 };
896
897JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencer16244f42006-12-01 21:10:07 +0000898 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5.newTy + " " + *$6;
Reid Spencere77e35e2006-12-01 20:26:20 +0000899 $2.destroy(); delete $3; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000900 $$ = $1;
901 }
902 | IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000903 $2->insert(0, *$1.newTy + " " );
904 *$2 += ", " + *$4.newTy + " " + *$5;
905 $1.destroy(); $4.destroy(); delete $5;
906 $$ = $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000907 };
908
909Inst
910 : OptAssign InstVal {
911 *$1 += *$2;
912 delete $2;
913 $$ = $1;
914 };
915
916PHIList
917 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencere77e35e2006-12-01 20:26:20 +0000918 $3->insert(0, *$1.newTy + "[");
919 *$3 += "," + *$5 + "]";
920 $1.destroy(); delete $5;
921 $$ = $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000922 }
923 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
924 *$1 += ", [" + *$4 + "," + *$6 + "]";
925 delete $4; delete $6;
926 $$ = $1;
927 };
928
929
930ValueRefList
Reid Spencere77e35e2006-12-01 20:26:20 +0000931 : ResolvedVal { $$ = new std::string(*$1.val); $1.destroy(); }
Reid Spencere7c3c602006-11-30 06:36:44 +0000932 | ValueRefList ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +0000933 *$1 += ", " + *$3.val;
934 $3.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000935 $$ = $1;
936 };
937
938// ValueRefListE - Just like ValueRefList, except that it may also be empty!
939ValueRefListE
940 : ValueRefList
941 | /*empty*/ { $$ = new std::string(); }
942 ;
943
944OptTailCall
945 : TAIL CALL {
946 *$1 += " " + *$2;
947 delete $2;
948 $$ = $1;
949 }
950 | CALL
951 ;
952
953InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000954 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5;
955 $2.destroy(); delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +0000956 $$ = $1;
957 }
958 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000959 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5;
960 $2.destroy(); delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +0000961 $$ = $1;
962 }
963 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000964 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5;
965 $2.destroy(); delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +0000966 $$ = $1;
967 }
968 | NOT ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +0000969 *$1 += " " + *$2.val;
970 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000971 $$ = $1;
972 }
973 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +0000974 *$1 += " " + *$2.val + ", " + *$4.val;
975 $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000976 $$ = $1;
977 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000978 | CAST ResolvedVal TO Types {
979 const char *opcode = getCastOpcode($2.type, $4);
980 $$ = new std::string(opcode);
981 *$$ += *$2.val + " " + *$3 + " " + *$4.newTy;
982 delete $1; $2.destroy();
983 delete $3; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000984 }
985 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +0000986 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
987 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000988 $$ = $1;
989 }
990 | VAARG ResolvedVal ',' Types {
Reid Spencere77e35e2006-12-01 20:26:20 +0000991 *$1 += " " + *$2.val + ", " + *$4.newTy;
992 $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000993 $$ = $1;
994 }
995 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +0000996 *$1 += " " + *$2.val + ", " + *$4.val;
997 $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000998 $$ = $1;
999 }
1000 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001001 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1002 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001003 $$ = $1;
1004 }
1005 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001006 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1007 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001008 $$ = $1;
1009 }
1010 | PHI_TOK PHIList {
1011 *$1 += " " + *$2;
1012 delete $2;
1013 $$ = $1;
1014 }
1015 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
1016 if (!$2->empty())
1017 *$1 += " " + *$2;
1018 if (!$1->empty())
1019 *$1 += " ";
Reid Spencere77e35e2006-12-01 20:26:20 +00001020 *$1 += *$3.newTy + " " + *$4 + "(" + *$6 + ")";
1021 delete $2; $3.destroy(); delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001022 $$ = $1;
1023 }
1024 | MemoryInst ;
1025
1026
1027// IndexList - List of indices for GEP based instructions...
1028IndexList
1029 : ',' ValueRefList {
1030 $2->insert(0, ", ");
1031 $$ = $2;
1032 }
1033 | /* empty */ { $$ = new std::string(); }
1034 ;
1035
1036OptVolatile
1037 : VOLATILE
1038 | /* empty */ { $$ = new std::string(); }
1039 ;
1040
1041MemoryInst : MALLOC Types OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001042 *$1 += " " + *$2.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +00001043 if (!$3->empty())
1044 *$1 += " " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001045 $2.destroy(); delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001046 $$ = $1;
1047 }
1048 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001049 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001050 if (!$6->empty())
1051 *$1 += " " + *$6;
Reid Spencere77e35e2006-12-01 20:26:20 +00001052 $2.destroy(); $4.destroy(); delete $5; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001053 $$ = $1;
1054 }
1055 | ALLOCA Types OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001056 *$1 += " " + *$2.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +00001057 if (!$3->empty())
1058 *$1 += " " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001059 $2.destroy(); delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001060 $$ = $1;
1061 }
1062 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001063 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001064 if (!$6->empty())
1065 *$1 += " " + *$6;
Reid Spencere77e35e2006-12-01 20:26:20 +00001066 $2.destroy(); $4.destroy(); delete $5; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001067 $$ = $1;
1068 }
1069 | FREE ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001070 *$1 += " " + *$2.val;
1071 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001072 $$ = $1;
1073 }
1074 | OptVolatile LOAD Types ValueRef {
1075 if (!$1->empty())
1076 *$1 += " ";
Reid Spencere77e35e2006-12-01 20:26:20 +00001077 *$1 += *$2 + " " + *$3.newTy + " " + *$4;
1078 delete $2; $3.destroy(); delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001079 $$ = $1;
1080 }
1081 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1082 if (!$1->empty())
1083 *$1 += " ";
Reid Spencere77e35e2006-12-01 20:26:20 +00001084 *$1 += *$2 + " " + *$3.val + ", " + *$5.newTy + " " + *$6;
1085 delete $2; $3.destroy(); $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001086 $$ = $1;
1087 }
1088 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencere77e35e2006-12-01 20:26:20 +00001089 *$1 += *$2.newTy + " " + *$3 + " " + *$4;
1090 $2.destroy(); delete $3; delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001091 $$ = $1;
1092 };
1093
1094%%
1095
1096int yyerror(const char *ErrorMsg) {
1097 std::string where
1098 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1099 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
1100 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
1101 if (yychar == YYEMPTY || yychar == 0)
1102 errMsg += "end-of-file.";
1103 else
1104 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
1105 std::cerr << errMsg << '\n';
1106 exit(1);
1107}