blob: 930325c1bd2d097297c63479cdb363158fb69dfe [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
222 | X86_FASTCALLCC_TOK | CC_TOK EUINT64VAL
223 | /*empty*/ { $$ = new std::string(""); } ;
224
225// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
226// a comma before it.
227OptAlign
228 : /*empty*/ { $$ = new std::string(); }
Reid Spencere77e35e2006-12-01 20:26:20 +0000229 | ALIGN EUINT64VAL { *$1 += " " + *$2.cnst; delete $2.cnst; $$ = $1; };
Reid Spencere7c3c602006-11-30 06:36:44 +0000230 ;
231OptCAlign
232 : /*empty*/ { $$ = new std::string(); }
233 | ',' ALIGN EUINT64VAL {
234 $2->insert(0, ", ");
Reid Spencere77e35e2006-12-01 20:26:20 +0000235 *$2 += " " + *$3.cnst;
236 delete $3.cnst;
Reid Spencere7c3c602006-11-30 06:36:44 +0000237 $$ = $2;
238 };
239
240SectionString
241 : SECTION STRINGCONSTANT {
242 *$1 += " " + *$2;
243 delete $2;
244 $$ = $1;
245 };
246
247OptSection : /*empty*/ { $$ = new std::string(); }
248 | SectionString;
249
250GlobalVarAttributes
251 : /* empty */ { $$ = new std::string(); }
252 | ',' GlobalVarAttribute GlobalVarAttributes {
253 $2->insert(0, ", ");
254 if (!$3->empty())
255 *$2 += " " + *$3;
256 delete $3;
257 $$ = $2;
258 };
259
260GlobalVarAttribute
261 : SectionString
262 | ALIGN EUINT64VAL {
Reid Spencere77e35e2006-12-01 20:26:20 +0000263 *$1 += " " + *$2.cnst;
264 delete $2.cnst;
Reid Spencere7c3c602006-11-30 06:36:44 +0000265 $$ = $1;
266 };
267
268//===----------------------------------------------------------------------===//
269// Types includes all predefined types... except void, because it can only be
270// used in specific contexts (function returning void for example). To have
271// access to it, a user must explicitly use TypesV.
272//
273
274// TypesV includes all of 'Types', but it also includes the void type.
275TypesV : Types | VOID ;
276UpRTypesV : UpRTypes | VOID ;
277Types : UpRTypes ;
278
279// Derived types are added later...
280//
281PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
Reid Spencere77e35e2006-12-01 20:26:20 +0000282PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
283UpRTypes : OPAQUE | PrimType | SymbolicValueRef {
284 $$.newTy = $1; };
Reid Spencere7c3c602006-11-30 06:36:44 +0000285
286// Include derived types in the Types production.
287//
288UpRTypes : '\\' EUINT64VAL { // Type UpReference
Reid Spencere77e35e2006-12-01 20:26:20 +0000289 $2.cnst->insert(0, "\\");
290 $$.newTy = $2.cnst;
291 $$.oldTy = OpaqueTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000292 }
293 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000294 *$1.newTy += "( " + *$3 + " )";
Reid Spencere7c3c602006-11-30 06:36:44 +0000295 delete $3;
Reid Spencere77e35e2006-12-01 20:26:20 +0000296 $$.newTy = $1.newTy;
297 $$.oldTy = FunctionTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000298 }
299 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000300 $2.cnst->insert(0,"[ ");
301 *$2.cnst += " x " + *$4.newTy + " ]";
302 delete $4.newTy;
303 $$.newTy = $2.cnst;
304 $$.oldTy = ArrayTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000305 }
306 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed 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 = PackedTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000312 }
313 | '{' TypeListI '}' { // Structure type?
314 $2->insert(0, "{ ");
315 *$2 += " }";
Reid Spencere77e35e2006-12-01 20:26:20 +0000316 $$.newTy = $2;
317 $$.oldTy = StructTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000318 }
319 | '{' '}' { // Empty structure type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000320 $$.newTy = new std::string("{ }");
321 $$.oldTy = StructTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000322 }
323 | UpRTypes '*' { // Pointer type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000324 *$1.newTy += '*';
325 $1.oldTy = PointerTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000326 $$ = $1;
327 };
328
329// TypeList - Used for struct declarations and as a basis for function type
330// declaration type lists
331//
Reid Spencere77e35e2006-12-01 20:26:20 +0000332TypeListI
333 : UpRTypes {
334 $$ = $1.newTy;
335 }
336 | TypeListI ',' UpRTypes {
337 *$1 += ", " + *$3.newTy;
338 delete $3.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000339 $$ = $1;
340 };
341
342// ArgTypeList - List of types for a function type declaration...
Reid Spencere77e35e2006-12-01 20:26:20 +0000343ArgTypeListI
344 : TypeListI
Reid Spencere7c3c602006-11-30 06:36:44 +0000345 | TypeListI ',' DOTDOTDOT {
346 *$1 += ", ...";
347 delete $3;
348 $$ = $1;
349 }
350 | DOTDOTDOT {
351 $$ = $1;
352 }
353 | /*empty*/ {
354 $$ = new std::string();
355 };
356
357// ConstVal - The various declarations that go into the constant pool. This
358// production is used ONLY to represent constants that show up AFTER a 'const',
359// 'constant' or 'global' token at global scope. Constants that can be inlined
360// into other expressions (such as integers and constexprs) are handled by the
361// ResolvedVal, ValueRef and ConstValueRef productions.
362//
363ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencere77e35e2006-12-01 20:26:20 +0000364 $$.type = $1;
365 $$.cnst = new std::string(*$1.newTy);
366 *$$.cnst += " [ " + *$3 + " ]";
Reid Spencere7c3c602006-11-30 06:36:44 +0000367 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000368 }
369 | Types '[' ']' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000370 $$.type = $1;
371 $$.cnst = new std::string(*$1.newTy);
372 *$$.cnst += "[ ]";
Reid Spencere7c3c602006-11-30 06:36:44 +0000373 }
374 | Types 'c' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000375 $$.type = $1;
376 $$.cnst = new std::string(*$1.newTy);
377 *$$.cnst += " c" + *$3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000378 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000379 }
380 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencere77e35e2006-12-01 20:26:20 +0000381 $$.type = $1;
382 $$.cnst = new std::string(*$1.newTy);
383 *$$.cnst += " < " + *$3 + " >";
Reid Spencere7c3c602006-11-30 06:36:44 +0000384 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000385 }
386 | Types '{' ConstVector '}' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000387 $$.type = $1;
388 $$.cnst = new std::string(*$1.newTy);
389 *$$.cnst += " { " + *$3 + " }";
Reid Spencere7c3c602006-11-30 06:36:44 +0000390 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000391 }
392 | Types '{' '}' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000393 $$.type = $1;
394 $$.cnst = new std::string(*$1.newTy);
395 *$$.cnst += " [ ]";
Reid Spencere7c3c602006-11-30 06:36:44 +0000396 }
397 | Types NULL_TOK {
Reid Spencere77e35e2006-12-01 20:26:20 +0000398 $$.type = $1;
399 $$.cnst = new std::string(*$1.newTy);
400 *$$.cnst += " " + *$2.cnst;
401 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000402 }
403 | Types UNDEF {
Reid Spencere77e35e2006-12-01 20:26:20 +0000404 $$.type = $1;
405 $$.cnst = new std::string(*$1.newTy);
406 *$$.cnst += " " + *$2.cnst;
407 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000408 }
409 | Types SymbolicValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000410 $$.type = $1;
411 $$.cnst = new std::string(*$1.newTy);
412 *$$.cnst += " " + *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000413 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000414 }
415 | Types ConstExpr {
Reid Spencere77e35e2006-12-01 20:26:20 +0000416 $$.type = $1;
417 $$.cnst = new std::string(*$1.newTy);
418 *$$.cnst += " " + *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000419 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000420 }
421 | Types ZEROINITIALIZER {
Reid Spencere77e35e2006-12-01 20:26:20 +0000422 $$.type = $1;
423 $$.cnst = new std::string(*$1.newTy);
424 *$$.cnst += " " + *$2.cnst;
425 $2.destroy();
426 }
427 | SIntType EInt64Val { // integral constants
428 $$.type = $1;
429 $$.cnst = new std::string(*$1.newTy);
430 *$$.cnst += " " + *$2.cnst;
431 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000432 }
433 | UIntType EUINT64VAL { // integral constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000434 $$.type = $1;
435 $$.cnst = new std::string(*$1.newTy);
436 *$$.cnst += " " + *$2.cnst;
437 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000438 }
439 | BOOL TRUETOK { // Boolean constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000440 $$.type = $1;
441 $$.cnst = new std::string(*$1.newTy);
442 *$$.cnst += " " + *$2.cnst;
443 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000444 }
445 | BOOL FALSETOK { // Boolean constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000446 $$.type = $1;
447 $$.cnst = new std::string(*$1.newTy);
448 *$$.cnst += " " + *$2.cnst;
449 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000450 }
451 | FPType FPVAL { // Float & Double constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000452 $$.type = $1;
453 $$.cnst = new std::string(*$1.newTy);
454 *$$.cnst += " " + *$2.cnst;
455 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000456 };
457
458
Reid Spencere77e35e2006-12-01 20:26:20 +0000459ConstExpr: CAST '(' ConstVal TO Types ')' {
460 // We must infer the cast opcode from the types of the operands.
461 const char *opcode = getCastOpcode($3.type, $5);
462 $$ = new std::string(opcode);
463 *$$ += "(" + *$3.cnst + " " + *$4 + " " + *$5.newTy + ")";
464 delete $1; $3.destroy(); delete $4; $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000465 }
466 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000467 *$1 += "(" + *$3.cnst + " " + *$4 + ")";
468 $$ = $1;
469 $3.destroy();
470 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +0000471 }
472 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000473 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
474 $3.destroy(); $5.destroy(); $7.destroy();
475 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000476 }
477 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000478 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
479 $3.destroy(); $5.destroy();
480 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000481 }
482 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000483 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
484 $3.destroy(); $5.destroy();
485 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000486 }
487 | SetCondOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000488 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
489 $3.destroy(); $5.destroy();
490 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000491 }
492 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000493 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
494 $3.destroy(); $5.destroy();
495 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000496 }
497 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000498 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
499 $3.destroy(); $5.destroy();
500 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000501 }
502 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000503 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
504 $3.destroy(); $5.destroy(); $7.destroy();
505 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000506 }
507 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000508 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
509 $3.destroy(); $5.destroy(); $7.destroy();
510 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000511 };
512
513
514// ConstVector - A list of comma separated constants.
Reid Spencere77e35e2006-12-01 20:26:20 +0000515
516ConstVector
517 : ConstVector ',' ConstVal {
518 *$1 += ", " + *$3.cnst;
519 $3.destroy();
520 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000521 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000522 | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
523 ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000524
525
526// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Reid Spencere77e35e2006-12-01 20:26:20 +0000527GlobalType : GLOBAL | CONSTANT ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000528
529
530//===----------------------------------------------------------------------===//
531// Rules to match Modules
532//===----------------------------------------------------------------------===//
533
534// Module rule: Capture the result of parsing the whole file into a result
535// variable...
536//
537Module : DefinitionList {
538};
539
540// DefinitionList - Top level definitions
541//
542DefinitionList : DefinitionList Function {
543 $$ = 0;
544 }
545 | DefinitionList FunctionProto {
546 *O << *$2 << "\n";
547 delete $2;
548 $$ = 0;
549 }
550 | DefinitionList MODULE ASM_TOK AsmBlock {
551 *O << "module asm " << " " << *$4 << "\n";
552 }
553 | DefinitionList IMPLEMENTATION {
554 *O << "implementation\n";
555 }
556 | ConstPool {
557 };
558
559// ConstPool - Constants with optional names assigned to them.
560ConstPool : ConstPool OptAssign TYPE TypesV {
Reid Spencere77e35e2006-12-01 20:26:20 +0000561 *O << *$2 << " " << *$3 << " " << *$4.newTy << "\n";
562 // delete $2; delete $3; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000563 $$ = 0;
564 }
565 | ConstPool FunctionProto { // Function prototypes can be in const pool
566 *O << *$2 << "\n";
567 delete $2;
568 $$ = 0;
569 }
570 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
571 *O << *$2 << " " << *$3 << " " << *$4 << "\n";
572 delete $2; delete $3; delete $4;
573 $$ = 0;
574 }
575 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
Reid Spencere77e35e2006-12-01 20:26:20 +0000576 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.cnst << " "
577 << *$6 << "\n";
578 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000579 $$ = 0;
580 }
581 | ConstPool OptAssign EXTERNAL GlobalType Types GlobalVarAttributes {
Reid Spencere77e35e2006-12-01 20:26:20 +0000582 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.newTy
583 << " " << *$6 << "\n";
584 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000585 $$ = 0;
586 }
587 | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
Reid Spencere77e35e2006-12-01 20:26:20 +0000588 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.newTy
589 << " " << *$6 << "\n";
590 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000591 $$ = 0;
592 }
593 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
Reid Spencere77e35e2006-12-01 20:26:20 +0000594 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.newTy
595 << " " << *$6 << "\n";
596 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000597 $$ = 0;
598 }
599 | ConstPool TARGET TargetDefinition {
600 *O << *$2 << " " << *$3 << "\n";
601 delete $2; delete $3;
602 $$ = 0;
603 }
604 | ConstPool DEPLIBS '=' LibrariesDefinition {
605 *O << *$2 << " = " << *$4 << "\n";
606 delete $2; delete $4;
607 $$ = 0;
608 }
609 | /* empty: end of list */ {
610 $$ = 0;
611 };
612
613
614AsmBlock : STRINGCONSTANT ;
615
616BigOrLittle : BIG | LITTLE
617
618TargetDefinition
619 : ENDIAN '=' BigOrLittle {
Reid Spencere77e35e2006-12-01 20:26:20 +0000620 *$1 += " = " + *$3;
621 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000622 $$ = $1;
623 }
624 | POINTERSIZE '=' EUINT64VAL {
Reid Spencere77e35e2006-12-01 20:26:20 +0000625 *$1 += " = " + *$3.cnst;
626 if (*$3.cnst == "64")
627 SizeOfPointer = 64;
628 $3.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000629 $$ = $1;
630 }
631 | TRIPLE '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000632 *$1 += " = " + *$3;
633 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000634 $$ = $1;
635 }
636 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000637 *$1 += " = " + *$3;
638 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000639 $$ = $1;
640 };
641
642LibrariesDefinition
643 : '[' LibList ']' {
644 $2->insert(0, "[ ");
645 *$2 += " ]";
646 $$ = $2;
647 };
648
649LibList
650 : LibList ',' STRINGCONSTANT {
651 *$1 += ", " + *$3;
652 delete $3;
653 $$ = $1;
654 }
655 | STRINGCONSTANT
656 | /* empty: end of list */ {
657 $$ = new std::string();
658 };
659
660//===----------------------------------------------------------------------===//
661// Rules to match Function Headers
662//===----------------------------------------------------------------------===//
663
664Name : VAR_ID | STRINGCONSTANT;
665OptName : Name | /*empty*/ { $$ = new std::string(); };
666
667ArgVal : Types OptName {
Reid Spencere77e35e2006-12-01 20:26:20 +0000668 $$ = $1.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000669 if (!$2->empty())
670 *$$ += " " + *$2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000671 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000672};
673
674ArgListH : ArgListH ',' ArgVal {
675 *$1 += ", " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +0000676 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000677 }
678 | ArgVal {
679 $$ = $1;
680 };
681
682ArgList : ArgListH {
683 $$ = $1;
684 }
685 | ArgListH ',' DOTDOTDOT {
686 *$1 += ", ...";
687 $$ = $1;
Reid Spencere77e35e2006-12-01 20:26:20 +0000688 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000689 }
690 | DOTDOTDOT {
691 $$ = $1;
692 }
693 | /* empty */ {
694 $$ = new std::string();
695 };
696
697FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
698 OptSection OptAlign {
699 if (!$1->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000700 *$1 += " ";
Reid Spencere7c3c602006-11-30 06:36:44 +0000701 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000702 *$1 += *$2.newTy + " " + *$3 + "(" + *$5 + ")";
Reid Spencere7c3c602006-11-30 06:36:44 +0000703 if (!$7->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000704 *$1 += " " + *$7;
Reid Spencere7c3c602006-11-30 06:36:44 +0000705 }
706 if (!$8->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000707 *$1 += " " + *$8;
Reid Spencere7c3c602006-11-30 06:36:44 +0000708 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000709 $2.destroy();
710 delete $3;
711 delete $5;
712 delete $7;
713 delete $8;
714 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000715 };
716
717BEGIN : BEGINTOK {
718 $$ = new std::string("begin");
719 }
720 | '{' {
721 $$ = new std::string ("{");
722 }
723
724FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
725 if (!$1->empty()) {
726 *O << *$1 << " ";
727 }
728 *O << *$2 << " " << *$3 << "\n";
729 delete $1; delete $2; delete $3;
730 $$ = 0;
731};
732
733END : ENDTOK { $$ = new std::string("end"); }
734 | '}' { $$ = new std::string("}"); };
735
736Function : FunctionHeader BasicBlockList END {
737 if ($2)
738 *O << *$2;
739 *O << '\n' << *$3 << "\n";
Reid Spencere77e35e2006-12-01 20:26:20 +0000740 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000741};
742
Reid Spencere77e35e2006-12-01 20:26:20 +0000743FnDeclareLinkage
744 : /*default*/ { $$ = new std::string(); }
Reid Spencere7c3c602006-11-30 06:36:44 +0000745 | DLLIMPORT
746 | EXTERN_WEAK
747 ;
748
749FunctionProto
750 : DECLARE FnDeclareLinkage FunctionHeaderH {
Reid Spencere77e35e2006-12-01 20:26:20 +0000751 if (!$2->empty())
752 *$1 += " " + *$2;
753 *$1 += " " + *$3;
754 delete $2;
755 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000756 $$ = $1;
757 };
758
759//===----------------------------------------------------------------------===//
760// Rules to match Basic Blocks
761//===----------------------------------------------------------------------===//
762
763OptSideEffect : /* empty */ {
764 }
765 | SIDEEFFECT {
766 };
767
Reid Spencere77e35e2006-12-01 20:26:20 +0000768ConstValueRef
769 : ESINT64VAL { $$ = $1.cnst; }
770 | EUINT64VAL { $$ = $1.cnst; }
771 | FPVAL { $$ = $1.cnst; }
772 | TRUETOK { $$ = $1.cnst; }
773 | FALSETOK { $$ = $1.cnst; }
774 | NULL_TOK { $$ = $1.cnst; }
775 | UNDEF { $$ = $1.cnst; }
776 | ZEROINITIALIZER { $$ = $1.cnst; }
Reid Spencere7c3c602006-11-30 06:36:44 +0000777 | '<' ConstVector '>' {
778 $2->insert(0, "<");
779 *$2 += ">";
780 $$ = $2;
781 }
782 | ConstExpr
783 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
784 if (!$2->empty()) {
785 *$1 += " " + *$2;
786 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000787 *$1 += " " + *$3 + ", " + *$5;
788 delete $2; delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +0000789 $$ = $1;
790 };
791
Reid Spencere77e35e2006-12-01 20:26:20 +0000792SymbolicValueRef : IntVal { $$ = $1.cnst; } | Name ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000793
794// ValueRef - A reference to a definition... either constant or symbolic
795ValueRef : SymbolicValueRef | ConstValueRef;
796
797
798// ResolvedVal - a <type> <value> pair. This is used only in cases where the
799// type immediately preceeds the value reference, and allows complex constant
800// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
801ResolvedVal : Types ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000802 $$.type = $1;
803 $$.val = new std::string(*$1.newTy + " ");
804 *$$.val += *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000805 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000806 };
807
808BasicBlockList : BasicBlockList BasicBlock {
809 }
810 | BasicBlock { // Do not allow functions with 0 basic blocks
811 };
812
813
814// Basic blocks are terminated by branching instructions:
815// br, br/cc, switch, ret
816//
817BasicBlock : InstructionList OptAssign BBTerminatorInst {
818 *O << *$2 ;
819 };
820
821InstructionList : InstructionList Inst {
822 *O << " " << *$2 << "\n";
823 delete $2;
824 $$ = 0;
825 }
826 | /* empty */ {
827 $$ = 0;
828 }
829 | LABELSTR {
830 *O << *$1 << "\n";
831 delete $1;
832 $$ = 0;
833 };
834
835BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencere77e35e2006-12-01 20:26:20 +0000836 *O << " " << *$1 << " " << *$2.val << "\n";
837 delete $1; $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000838 $$ = 0;
839 }
840 | RET VOID { // Return with no result...
Reid Spencere77e35e2006-12-01 20:26:20 +0000841 *O << " " << *$1 << " " << *$2.newTy << "\n";
842 delete $1; $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000843 $$ = 0;
844 }
845 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencere77e35e2006-12-01 20:26:20 +0000846 *O << " " << *$1 << " " << *$2.newTy << " " << *$3 << "\n";
847 delete $1; $2.destroy(); delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000848 $$ = 0;
849 } // Conditional Branch...
850 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000851 *O << " " << *$1 << " " << *$2.newTy << " " << *$3 << ", "
852 << *$5.newTy << " " << *$6 << ", " << *$8.newTy << " " << *$9 << "\n";
853 delete $1; $2.destroy(); delete $3; $5.destroy(); delete $6;
854 $8.destroy(); delete $9;
Reid Spencere7c3c602006-11-30 06:36:44 +0000855 $$ = 0;
856 }
857 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000858 *O << " " << *$1 << " " << *$2.newTy << " " << *$3 << ", " << *$5.newTy
859 << " " << *$6 << " [" << *$8 << " ]\n";
860 delete $1; $2.destroy(); delete $3; $5.destroy(); delete $6; delete $8;
Reid Spencere7c3c602006-11-30 06:36:44 +0000861 $$ = 0;
862 }
863 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000864 *O << " " << *$1 << " " << *$2.newTy << " " << *$3 << ", "
865 << *$5.newTy << " " << *$6 << "[]\n";
866 delete $1; $2.destroy(); delete $3; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000867 $$ = 0;
868 }
869 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
870 TO LABEL ValueRef UNWIND LABEL ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000871 *O << " " << *$1 << " " << *$2 << " " << *$3.newTy << " " << *$4 << " ("
872 << *$6 << ") " << *$8 << " " << *$9.newTy << " " << *$10 << " "
873 << *$11 << " " << *$12.newTy << " " << *$13 << "\n";
874 delete $1; delete $2; $3.destroy(); delete $4; delete $6; delete $8;
875 $9.destroy(); delete $10; delete $11; $12.destroy(); delete $13;
Reid Spencere7c3c602006-11-30 06:36:44 +0000876 $$ = 0;
877 }
878 | UNWIND {
879 *O << " " << *$1 << "\n";
880 delete $1;
881 $$ = 0;
882 }
883 | UNREACHABLE {
884 *O << " " << *$1 << "\n";
885 delete $1;
886 $$ = 0;
887 };
888
889JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000890 *$1 += *$2.newTy + " " + *$3 + ", " + *$5.newTy + " " + *$6;
891 $2.destroy(); delete $3; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000892 $$ = $1;
893 }
894 | IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000895 $2->insert(0, *$1.newTy + " " );
896 *$2 += ", " + *$4.newTy + " " + *$5;
897 $1.destroy(); $4.destroy(); delete $5;
898 $$ = $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000899 };
900
901Inst
902 : OptAssign InstVal {
903 *$1 += *$2;
904 delete $2;
905 $$ = $1;
906 };
907
908PHIList
909 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencere77e35e2006-12-01 20:26:20 +0000910 $3->insert(0, *$1.newTy + "[");
911 *$3 += "," + *$5 + "]";
912 $1.destroy(); delete $5;
913 $$ = $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000914 }
915 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
916 *$1 += ", [" + *$4 + "," + *$6 + "]";
917 delete $4; delete $6;
918 $$ = $1;
919 };
920
921
922ValueRefList
Reid Spencere77e35e2006-12-01 20:26:20 +0000923 : ResolvedVal { $$ = new std::string(*$1.val); $1.destroy(); }
Reid Spencere7c3c602006-11-30 06:36:44 +0000924 | ValueRefList ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +0000925 *$1 += ", " + *$3.val;
926 $3.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000927 $$ = $1;
928 };
929
930// ValueRefListE - Just like ValueRefList, except that it may also be empty!
931ValueRefListE
932 : ValueRefList
933 | /*empty*/ { $$ = new std::string(); }
934 ;
935
936OptTailCall
937 : TAIL CALL {
938 *$1 += " " + *$2;
939 delete $2;
940 $$ = $1;
941 }
942 | CALL
943 ;
944
945InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000946 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5;
947 $2.destroy(); delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +0000948 $$ = $1;
949 }
950 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000951 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5;
952 $2.destroy(); delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +0000953 $$ = $1;
954 }
955 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000956 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5;
957 $2.destroy(); delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +0000958 $$ = $1;
959 }
960 | NOT ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +0000961 *$1 += " " + *$2.val;
962 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000963 $$ = $1;
964 }
965 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +0000966 *$1 += " " + *$2.val + ", " + *$4.val;
967 $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000968 $$ = $1;
969 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000970 | CAST ResolvedVal TO Types {
971 const char *opcode = getCastOpcode($2.type, $4);
972 $$ = new std::string(opcode);
973 *$$ += *$2.val + " " + *$3 + " " + *$4.newTy;
974 delete $1; $2.destroy();
975 delete $3; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000976 }
977 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +0000978 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
979 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000980 $$ = $1;
981 }
982 | VAARG ResolvedVal ',' Types {
Reid Spencere77e35e2006-12-01 20:26:20 +0000983 *$1 += " " + *$2.val + ", " + *$4.newTy;
984 $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000985 $$ = $1;
986 }
987 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +0000988 *$1 += " " + *$2.val + ", " + *$4.val;
989 $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000990 $$ = $1;
991 }
992 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +0000993 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
994 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000995 $$ = $1;
996 }
997 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +0000998 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
999 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001000 $$ = $1;
1001 }
1002 | PHI_TOK PHIList {
1003 *$1 += " " + *$2;
1004 delete $2;
1005 $$ = $1;
1006 }
1007 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
1008 if (!$2->empty())
1009 *$1 += " " + *$2;
1010 if (!$1->empty())
1011 *$1 += " ";
Reid Spencere77e35e2006-12-01 20:26:20 +00001012 *$1 += *$3.newTy + " " + *$4 + "(" + *$6 + ")";
1013 delete $2; $3.destroy(); delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001014 $$ = $1;
1015 }
1016 | MemoryInst ;
1017
1018
1019// IndexList - List of indices for GEP based instructions...
1020IndexList
1021 : ',' ValueRefList {
1022 $2->insert(0, ", ");
1023 $$ = $2;
1024 }
1025 | /* empty */ { $$ = new std::string(); }
1026 ;
1027
1028OptVolatile
1029 : VOLATILE
1030 | /* empty */ { $$ = new std::string(); }
1031 ;
1032
1033MemoryInst : MALLOC Types OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001034 *$1 += " " + *$2.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +00001035 if (!$3->empty())
1036 *$1 += " " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001037 $2.destroy(); delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001038 $$ = $1;
1039 }
1040 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001041 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001042 if (!$6->empty())
1043 *$1 += " " + *$6;
Reid Spencere77e35e2006-12-01 20:26:20 +00001044 $2.destroy(); $4.destroy(); delete $5; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001045 $$ = $1;
1046 }
1047 | ALLOCA Types OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001048 *$1 += " " + *$2.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +00001049 if (!$3->empty())
1050 *$1 += " " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001051 $2.destroy(); delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001052 $$ = $1;
1053 }
1054 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001055 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001056 if (!$6->empty())
1057 *$1 += " " + *$6;
Reid Spencere77e35e2006-12-01 20:26:20 +00001058 $2.destroy(); $4.destroy(); delete $5; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001059 $$ = $1;
1060 }
1061 | FREE ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001062 *$1 += " " + *$2.val;
1063 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001064 $$ = $1;
1065 }
1066 | OptVolatile LOAD Types ValueRef {
1067 if (!$1->empty())
1068 *$1 += " ";
Reid Spencere77e35e2006-12-01 20:26:20 +00001069 *$1 += *$2 + " " + *$3.newTy + " " + *$4;
1070 delete $2; $3.destroy(); delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001071 $$ = $1;
1072 }
1073 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1074 if (!$1->empty())
1075 *$1 += " ";
Reid Spencere77e35e2006-12-01 20:26:20 +00001076 *$1 += *$2 + " " + *$3.val + ", " + *$5.newTy + " " + *$6;
1077 delete $2; $3.destroy(); $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001078 $$ = $1;
1079 }
1080 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencere77e35e2006-12-01 20:26:20 +00001081 *$1 += *$2.newTy + " " + *$3 + " " + *$4;
1082 $2.destroy(); delete $3; delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001083 $$ = $1;
1084 };
1085
1086%%
1087
1088int yyerror(const char *ErrorMsg) {
1089 std::string where
1090 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1091 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
1092 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
1093 if (yychar == YYEMPTY || yychar == 0)
1094 errMsg += "end-of-file.";
1095 else
1096 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
1097 std::cerr << errMsg << '\n';
1098 exit(1);
1099}