blob: bd877403383f1886bc09a543294283577a15cf4f [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;
Reid Spencerd154b572006-12-01 20:36:40 +0000283UpRTypes : OPAQUE | PrimType
284 | SymbolicValueRef {
285 $$.newTy = $1; $$.oldTy = OpaqueTy;
286 };
Reid Spencere7c3c602006-11-30 06:36:44 +0000287
288// Include derived types in the Types production.
289//
290UpRTypes : '\\' EUINT64VAL { // Type UpReference
Reid Spencere77e35e2006-12-01 20:26:20 +0000291 $2.cnst->insert(0, "\\");
292 $$.newTy = $2.cnst;
293 $$.oldTy = OpaqueTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000294 }
295 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000296 *$1.newTy += "( " + *$3 + " )";
Reid Spencere7c3c602006-11-30 06:36:44 +0000297 delete $3;
Reid Spencere77e35e2006-12-01 20:26:20 +0000298 $$.newTy = $1.newTy;
299 $$.oldTy = FunctionTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000300 }
301 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000302 $2.cnst->insert(0,"[ ");
303 *$2.cnst += " x " + *$4.newTy + " ]";
304 delete $4.newTy;
305 $$.newTy = $2.cnst;
306 $$.oldTy = ArrayTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000307 }
308 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000309 $2.cnst->insert(0,"< ");
310 *$2.cnst += " x " + *$4.newTy + " >";
311 delete $4.newTy;
312 $$.newTy = $2.cnst;
313 $$.oldTy = PackedTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000314 }
315 | '{' TypeListI '}' { // Structure type?
316 $2->insert(0, "{ ");
317 *$2 += " }";
Reid Spencere77e35e2006-12-01 20:26:20 +0000318 $$.newTy = $2;
319 $$.oldTy = StructTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000320 }
321 | '{' '}' { // Empty structure type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000322 $$.newTy = new std::string("{ }");
323 $$.oldTy = StructTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000324 }
325 | UpRTypes '*' { // Pointer type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000326 *$1.newTy += '*';
327 $1.oldTy = PointerTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000328 $$ = $1;
329 };
330
331// TypeList - Used for struct declarations and as a basis for function type
332// declaration type lists
333//
Reid Spencere77e35e2006-12-01 20:26:20 +0000334TypeListI
335 : UpRTypes {
336 $$ = $1.newTy;
337 }
338 | TypeListI ',' UpRTypes {
339 *$1 += ", " + *$3.newTy;
340 delete $3.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000341 $$ = $1;
342 };
343
344// ArgTypeList - List of types for a function type declaration...
Reid Spencere77e35e2006-12-01 20:26:20 +0000345ArgTypeListI
346 : TypeListI
Reid Spencere7c3c602006-11-30 06:36:44 +0000347 | TypeListI ',' DOTDOTDOT {
348 *$1 += ", ...";
349 delete $3;
350 $$ = $1;
351 }
352 | DOTDOTDOT {
353 $$ = $1;
354 }
355 | /*empty*/ {
356 $$ = new std::string();
357 };
358
359// ConstVal - The various declarations that go into the constant pool. This
360// production is used ONLY to represent constants that show up AFTER a 'const',
361// 'constant' or 'global' token at global scope. Constants that can be inlined
362// into other expressions (such as integers and constexprs) are handled by the
363// ResolvedVal, ValueRef and ConstValueRef productions.
364//
365ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencere77e35e2006-12-01 20:26:20 +0000366 $$.type = $1;
367 $$.cnst = new std::string(*$1.newTy);
368 *$$.cnst += " [ " + *$3 + " ]";
Reid Spencere7c3c602006-11-30 06:36:44 +0000369 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000370 }
371 | Types '[' ']' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000372 $$.type = $1;
373 $$.cnst = new std::string(*$1.newTy);
374 *$$.cnst += "[ ]";
Reid Spencere7c3c602006-11-30 06:36:44 +0000375 }
376 | Types 'c' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000377 $$.type = $1;
378 $$.cnst = new std::string(*$1.newTy);
379 *$$.cnst += " c" + *$3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000380 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000381 }
382 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencere77e35e2006-12-01 20:26:20 +0000383 $$.type = $1;
384 $$.cnst = new std::string(*$1.newTy);
385 *$$.cnst += " < " + *$3 + " >";
Reid Spencere7c3c602006-11-30 06:36:44 +0000386 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000387 }
388 | Types '{' ConstVector '}' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000389 $$.type = $1;
390 $$.cnst = new std::string(*$1.newTy);
391 *$$.cnst += " { " + *$3 + " }";
Reid Spencere7c3c602006-11-30 06:36:44 +0000392 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000393 }
394 | Types '{' '}' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000395 $$.type = $1;
396 $$.cnst = new std::string(*$1.newTy);
397 *$$.cnst += " [ ]";
Reid Spencere7c3c602006-11-30 06:36:44 +0000398 }
399 | Types NULL_TOK {
Reid Spencere77e35e2006-12-01 20:26:20 +0000400 $$.type = $1;
401 $$.cnst = new std::string(*$1.newTy);
402 *$$.cnst += " " + *$2.cnst;
403 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000404 }
405 | Types UNDEF {
Reid Spencere77e35e2006-12-01 20:26:20 +0000406 $$.type = $1;
407 $$.cnst = new std::string(*$1.newTy);
408 *$$.cnst += " " + *$2.cnst;
409 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000410 }
411 | Types SymbolicValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000412 $$.type = $1;
413 $$.cnst = new std::string(*$1.newTy);
414 *$$.cnst += " " + *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000415 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000416 }
417 | Types ConstExpr {
Reid Spencere77e35e2006-12-01 20:26:20 +0000418 $$.type = $1;
419 $$.cnst = new std::string(*$1.newTy);
420 *$$.cnst += " " + *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000421 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000422 }
423 | Types ZEROINITIALIZER {
Reid Spencere77e35e2006-12-01 20:26:20 +0000424 $$.type = $1;
425 $$.cnst = new std::string(*$1.newTy);
426 *$$.cnst += " " + *$2.cnst;
427 $2.destroy();
428 }
429 | SIntType EInt64Val { // integral constants
430 $$.type = $1;
431 $$.cnst = new std::string(*$1.newTy);
432 *$$.cnst += " " + *$2.cnst;
433 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000434 }
435 | UIntType EUINT64VAL { // integral constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000436 $$.type = $1;
437 $$.cnst = new std::string(*$1.newTy);
438 *$$.cnst += " " + *$2.cnst;
439 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000440 }
441 | BOOL TRUETOK { // Boolean constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000442 $$.type = $1;
443 $$.cnst = new std::string(*$1.newTy);
444 *$$.cnst += " " + *$2.cnst;
445 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000446 }
447 | BOOL FALSETOK { // Boolean constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000448 $$.type = $1;
449 $$.cnst = new std::string(*$1.newTy);
450 *$$.cnst += " " + *$2.cnst;
451 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000452 }
453 | FPType FPVAL { // Float & Double constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000454 $$.type = $1;
455 $$.cnst = new std::string(*$1.newTy);
456 *$$.cnst += " " + *$2.cnst;
457 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000458 };
459
460
Reid Spencere77e35e2006-12-01 20:26:20 +0000461ConstExpr: CAST '(' ConstVal TO Types ')' {
462 // We must infer the cast opcode from the types of the operands.
463 const char *opcode = getCastOpcode($3.type, $5);
464 $$ = new std::string(opcode);
465 *$$ += "(" + *$3.cnst + " " + *$4 + " " + *$5.newTy + ")";
466 delete $1; $3.destroy(); delete $4; $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000467 }
468 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000469 *$1 += "(" + *$3.cnst + " " + *$4 + ")";
470 $$ = $1;
471 $3.destroy();
472 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +0000473 }
474 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000475 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
476 $3.destroy(); $5.destroy(); $7.destroy();
477 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000478 }
479 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000480 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
481 $3.destroy(); $5.destroy();
482 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000483 }
484 | LogicalOps '(' 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 | SetCondOps '(' 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 | ShiftOps '(' 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 | EXTRACTELEMENT '(' 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 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000505 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
506 $3.destroy(); $5.destroy(); $7.destroy();
507 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000508 }
509 | SHUFFLEVECTOR '(' 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
515
516// ConstVector - A list of comma separated constants.
Reid Spencere77e35e2006-12-01 20:26:20 +0000517
518ConstVector
519 : ConstVector ',' ConstVal {
520 *$1 += ", " + *$3.cnst;
521 $3.destroy();
522 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000523 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000524 | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
525 ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000526
527
528// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Reid Spencere77e35e2006-12-01 20:26:20 +0000529GlobalType : GLOBAL | CONSTANT ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000530
531
532//===----------------------------------------------------------------------===//
533// Rules to match Modules
534//===----------------------------------------------------------------------===//
535
536// Module rule: Capture the result of parsing the whole file into a result
537// variable...
538//
539Module : DefinitionList {
540};
541
542// DefinitionList - Top level definitions
543//
544DefinitionList : DefinitionList Function {
545 $$ = 0;
546 }
547 | DefinitionList FunctionProto {
548 *O << *$2 << "\n";
549 delete $2;
550 $$ = 0;
551 }
552 | DefinitionList MODULE ASM_TOK AsmBlock {
553 *O << "module asm " << " " << *$4 << "\n";
Reid Spencerd154b572006-12-01 20:36:40 +0000554 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000555 }
556 | DefinitionList IMPLEMENTATION {
557 *O << "implementation\n";
Reid Spencerd154b572006-12-01 20:36:40 +0000558 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000559 }
Reid Spencerd154b572006-12-01 20:36:40 +0000560 | ConstPool;
Reid Spencere7c3c602006-11-30 06:36:44 +0000561
562// ConstPool - Constants with optional names assigned to them.
563ConstPool : ConstPool OptAssign TYPE TypesV {
Reid Spencere77e35e2006-12-01 20:26:20 +0000564 *O << *$2 << " " << *$3 << " " << *$4.newTy << "\n";
565 // delete $2; delete $3; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000566 $$ = 0;
567 }
568 | ConstPool FunctionProto { // Function prototypes can be in const pool
569 *O << *$2 << "\n";
570 delete $2;
571 $$ = 0;
572 }
573 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
574 *O << *$2 << " " << *$3 << " " << *$4 << "\n";
575 delete $2; delete $3; delete $4;
576 $$ = 0;
577 }
578 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
Reid Spencere77e35e2006-12-01 20:26:20 +0000579 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.cnst << " "
580 << *$6 << "\n";
581 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000582 $$ = 0;
583 }
584 | ConstPool OptAssign EXTERNAL GlobalType Types GlobalVarAttributes {
Reid Spencere77e35e2006-12-01 20:26:20 +0000585 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.newTy
586 << " " << *$6 << "\n";
587 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000588 $$ = 0;
589 }
590 | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
Reid Spencere77e35e2006-12-01 20:26:20 +0000591 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.newTy
592 << " " << *$6 << "\n";
593 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000594 $$ = 0;
595 }
596 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
Reid Spencere77e35e2006-12-01 20:26:20 +0000597 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.newTy
598 << " " << *$6 << "\n";
599 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000600 $$ = 0;
601 }
602 | ConstPool TARGET TargetDefinition {
603 *O << *$2 << " " << *$3 << "\n";
604 delete $2; delete $3;
605 $$ = 0;
606 }
607 | ConstPool DEPLIBS '=' LibrariesDefinition {
608 *O << *$2 << " = " << *$4 << "\n";
609 delete $2; delete $4;
610 $$ = 0;
611 }
612 | /* empty: end of list */ {
613 $$ = 0;
614 };
615
616
617AsmBlock : STRINGCONSTANT ;
618
619BigOrLittle : BIG | LITTLE
620
621TargetDefinition
622 : ENDIAN '=' BigOrLittle {
Reid Spencere77e35e2006-12-01 20:26:20 +0000623 *$1 += " = " + *$3;
624 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000625 $$ = $1;
626 }
627 | POINTERSIZE '=' EUINT64VAL {
Reid Spencere77e35e2006-12-01 20:26:20 +0000628 *$1 += " = " + *$3.cnst;
629 if (*$3.cnst == "64")
630 SizeOfPointer = 64;
631 $3.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000632 $$ = $1;
633 }
634 | TRIPLE '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000635 *$1 += " = " + *$3;
636 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000637 $$ = $1;
638 }
639 | DATALAYOUT '=' 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
645LibrariesDefinition
646 : '[' LibList ']' {
647 $2->insert(0, "[ ");
648 *$2 += " ]";
649 $$ = $2;
650 };
651
652LibList
653 : LibList ',' STRINGCONSTANT {
654 *$1 += ", " + *$3;
655 delete $3;
656 $$ = $1;
657 }
658 | STRINGCONSTANT
659 | /* empty: end of list */ {
660 $$ = new std::string();
661 };
662
663//===----------------------------------------------------------------------===//
664// Rules to match Function Headers
665//===----------------------------------------------------------------------===//
666
667Name : VAR_ID | STRINGCONSTANT;
668OptName : Name | /*empty*/ { $$ = new std::string(); };
669
670ArgVal : Types OptName {
Reid Spencere77e35e2006-12-01 20:26:20 +0000671 $$ = $1.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000672 if (!$2->empty())
673 *$$ += " " + *$2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000674 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000675};
676
677ArgListH : ArgListH ',' ArgVal {
678 *$1 += ", " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +0000679 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000680 }
681 | ArgVal {
682 $$ = $1;
683 };
684
685ArgList : ArgListH {
686 $$ = $1;
687 }
688 | ArgListH ',' DOTDOTDOT {
689 *$1 += ", ...";
690 $$ = $1;
Reid Spencere77e35e2006-12-01 20:26:20 +0000691 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000692 }
693 | DOTDOTDOT {
694 $$ = $1;
695 }
Reid Spencerd154b572006-12-01 20:36:40 +0000696 | /* empty */ { $$ = new std::string(); };
Reid Spencere7c3c602006-11-30 06:36:44 +0000697
698FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
699 OptSection OptAlign {
700 if (!$1->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000701 *$1 += " ";
Reid Spencere7c3c602006-11-30 06:36:44 +0000702 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000703 *$1 += *$2.newTy + " " + *$3 + "(" + *$5 + ")";
Reid Spencere7c3c602006-11-30 06:36:44 +0000704 if (!$7->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000705 *$1 += " " + *$7;
Reid Spencere7c3c602006-11-30 06:36:44 +0000706 }
707 if (!$8->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000708 *$1 += " " + *$8;
Reid Spencere7c3c602006-11-30 06:36:44 +0000709 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000710 $2.destroy();
711 delete $3;
712 delete $5;
713 delete $7;
714 delete $8;
715 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000716 };
717
718BEGIN : BEGINTOK {
719 $$ = new std::string("begin");
720 }
721 | '{' {
722 $$ = new std::string ("{");
723 }
724
725FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
726 if (!$1->empty()) {
727 *O << *$1 << " ";
728 }
729 *O << *$2 << " " << *$3 << "\n";
730 delete $1; delete $2; delete $3;
731 $$ = 0;
732};
733
734END : ENDTOK { $$ = new std::string("end"); }
735 | '}' { $$ = new std::string("}"); };
736
737Function : FunctionHeader BasicBlockList END {
738 if ($2)
739 *O << *$2;
740 *O << '\n' << *$3 << "\n";
Reid Spencere77e35e2006-12-01 20:26:20 +0000741 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000742};
743
Reid Spencere77e35e2006-12-01 20:26:20 +0000744FnDeclareLinkage
745 : /*default*/ { $$ = new std::string(); }
Reid Spencere7c3c602006-11-30 06:36:44 +0000746 | DLLIMPORT
747 | EXTERN_WEAK
748 ;
749
750FunctionProto
751 : DECLARE FnDeclareLinkage FunctionHeaderH {
Reid Spencere77e35e2006-12-01 20:26:20 +0000752 if (!$2->empty())
753 *$1 += " " + *$2;
754 *$1 += " " + *$3;
755 delete $2;
756 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000757 $$ = $1;
758 };
759
760//===----------------------------------------------------------------------===//
761// Rules to match Basic Blocks
762//===----------------------------------------------------------------------===//
763
Reid Spencerd154b572006-12-01 20:36:40 +0000764OptSideEffect : /* empty */ { $$ = new std::string(); }
765 | SIDEEFFECT;
Reid Spencere7c3c602006-11-30 06:36:44 +0000766
Reid Spencere77e35e2006-12-01 20:26:20 +0000767ConstValueRef
768 : ESINT64VAL { $$ = $1.cnst; }
769 | EUINT64VAL { $$ = $1.cnst; }
770 | FPVAL { $$ = $1.cnst; }
771 | TRUETOK { $$ = $1.cnst; }
772 | FALSETOK { $$ = $1.cnst; }
773 | NULL_TOK { $$ = $1.cnst; }
774 | UNDEF { $$ = $1.cnst; }
775 | ZEROINITIALIZER { $$ = $1.cnst; }
Reid Spencere7c3c602006-11-30 06:36:44 +0000776 | '<' ConstVector '>' {
777 $2->insert(0, "<");
778 *$2 += ">";
779 $$ = $2;
780 }
781 | ConstExpr
782 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
783 if (!$2->empty()) {
784 *$1 += " " + *$2;
785 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000786 *$1 += " " + *$3 + ", " + *$5;
787 delete $2; delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +0000788 $$ = $1;
789 };
790
Reid Spencere77e35e2006-12-01 20:26:20 +0000791SymbolicValueRef : IntVal { $$ = $1.cnst; } | Name ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000792
793// ValueRef - A reference to a definition... either constant or symbolic
794ValueRef : SymbolicValueRef | ConstValueRef;
795
796
797// ResolvedVal - a <type> <value> pair. This is used only in cases where the
798// type immediately preceeds the value reference, and allows complex constant
799// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
800ResolvedVal : Types ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000801 $$.type = $1;
802 $$.val = new std::string(*$1.newTy + " ");
803 *$$.val += *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000804 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000805 };
806
807BasicBlockList : BasicBlockList BasicBlock {
808 }
809 | BasicBlock { // Do not allow functions with 0 basic blocks
810 };
811
812
813// Basic blocks are terminated by branching instructions:
814// br, br/cc, switch, ret
815//
816BasicBlock : InstructionList OptAssign BBTerminatorInst {
817 *O << *$2 ;
818 };
819
820InstructionList : InstructionList Inst {
821 *O << " " << *$2 << "\n";
822 delete $2;
823 $$ = 0;
824 }
825 | /* empty */ {
826 $$ = 0;
827 }
828 | LABELSTR {
829 *O << *$1 << "\n";
830 delete $1;
831 $$ = 0;
832 };
833
834BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencere77e35e2006-12-01 20:26:20 +0000835 *O << " " << *$1 << " " << *$2.val << "\n";
836 delete $1; $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000837 $$ = 0;
838 }
839 | RET VOID { // Return with no result...
Reid Spencere77e35e2006-12-01 20:26:20 +0000840 *O << " " << *$1 << " " << *$2.newTy << "\n";
841 delete $1; $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000842 $$ = 0;
843 }
844 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencere77e35e2006-12-01 20:26:20 +0000845 *O << " " << *$1 << " " << *$2.newTy << " " << *$3 << "\n";
846 delete $1; $2.destroy(); delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000847 $$ = 0;
848 } // Conditional Branch...
849 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000850 *O << " " << *$1 << " " << *$2.newTy << " " << *$3 << ", "
851 << *$5.newTy << " " << *$6 << ", " << *$8.newTy << " " << *$9 << "\n";
852 delete $1; $2.destroy(); delete $3; $5.destroy(); delete $6;
853 $8.destroy(); delete $9;
Reid Spencere7c3c602006-11-30 06:36:44 +0000854 $$ = 0;
855 }
856 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000857 *O << " " << *$1 << " " << *$2.newTy << " " << *$3 << ", " << *$5.newTy
858 << " " << *$6 << " [" << *$8 << " ]\n";
859 delete $1; $2.destroy(); delete $3; $5.destroy(); delete $6; delete $8;
Reid Spencere7c3c602006-11-30 06:36:44 +0000860 $$ = 0;
861 }
862 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000863 *O << " " << *$1 << " " << *$2.newTy << " " << *$3 << ", "
864 << *$5.newTy << " " << *$6 << "[]\n";
865 delete $1; $2.destroy(); delete $3; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000866 $$ = 0;
867 }
868 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
869 TO LABEL ValueRef UNWIND LABEL ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000870 *O << " " << *$1 << " " << *$2 << " " << *$3.newTy << " " << *$4 << " ("
871 << *$6 << ") " << *$8 << " " << *$9.newTy << " " << *$10 << " "
872 << *$11 << " " << *$12.newTy << " " << *$13 << "\n";
873 delete $1; delete $2; $3.destroy(); delete $4; delete $6; delete $8;
874 $9.destroy(); delete $10; delete $11; $12.destroy(); delete $13;
Reid Spencere7c3c602006-11-30 06:36:44 +0000875 $$ = 0;
876 }
877 | UNWIND {
878 *O << " " << *$1 << "\n";
879 delete $1;
880 $$ = 0;
881 }
882 | UNREACHABLE {
883 *O << " " << *$1 << "\n";
884 delete $1;
885 $$ = 0;
886 };
887
888JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000889 *$1 += *$2.newTy + " " + *$3 + ", " + *$5.newTy + " " + *$6;
890 $2.destroy(); delete $3; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000891 $$ = $1;
892 }
893 | IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000894 $2->insert(0, *$1.newTy + " " );
895 *$2 += ", " + *$4.newTy + " " + *$5;
896 $1.destroy(); $4.destroy(); delete $5;
897 $$ = $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000898 };
899
900Inst
901 : OptAssign InstVal {
902 *$1 += *$2;
903 delete $2;
904 $$ = $1;
905 };
906
907PHIList
908 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencere77e35e2006-12-01 20:26:20 +0000909 $3->insert(0, *$1.newTy + "[");
910 *$3 += "," + *$5 + "]";
911 $1.destroy(); delete $5;
912 $$ = $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000913 }
914 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
915 *$1 += ", [" + *$4 + "," + *$6 + "]";
916 delete $4; delete $6;
917 $$ = $1;
918 };
919
920
921ValueRefList
Reid Spencere77e35e2006-12-01 20:26:20 +0000922 : ResolvedVal { $$ = new std::string(*$1.val); $1.destroy(); }
Reid Spencere7c3c602006-11-30 06:36:44 +0000923 | ValueRefList ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +0000924 *$1 += ", " + *$3.val;
925 $3.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000926 $$ = $1;
927 };
928
929// ValueRefListE - Just like ValueRefList, except that it may also be empty!
930ValueRefListE
931 : ValueRefList
932 | /*empty*/ { $$ = new std::string(); }
933 ;
934
935OptTailCall
936 : TAIL CALL {
937 *$1 += " " + *$2;
938 delete $2;
939 $$ = $1;
940 }
941 | CALL
942 ;
943
944InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000945 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5;
946 $2.destroy(); delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +0000947 $$ = $1;
948 }
949 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000950 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5;
951 $2.destroy(); delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +0000952 $$ = $1;
953 }
954 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000955 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5;
956 $2.destroy(); delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +0000957 $$ = $1;
958 }
959 | NOT ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +0000960 *$1 += " " + *$2.val;
961 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000962 $$ = $1;
963 }
964 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +0000965 *$1 += " " + *$2.val + ", " + *$4.val;
966 $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000967 $$ = $1;
968 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000969 | CAST ResolvedVal TO Types {
970 const char *opcode = getCastOpcode($2.type, $4);
971 $$ = new std::string(opcode);
972 *$$ += *$2.val + " " + *$3 + " " + *$4.newTy;
973 delete $1; $2.destroy();
974 delete $3; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000975 }
976 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +0000977 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
978 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000979 $$ = $1;
980 }
981 | VAARG ResolvedVal ',' Types {
Reid Spencere77e35e2006-12-01 20:26:20 +0000982 *$1 += " " + *$2.val + ", " + *$4.newTy;
983 $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000984 $$ = $1;
985 }
986 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +0000987 *$1 += " " + *$2.val + ", " + *$4.val;
988 $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000989 $$ = $1;
990 }
991 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +0000992 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
993 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000994 $$ = $1;
995 }
996 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +0000997 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
998 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000999 $$ = $1;
1000 }
1001 | PHI_TOK PHIList {
1002 *$1 += " " + *$2;
1003 delete $2;
1004 $$ = $1;
1005 }
1006 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
1007 if (!$2->empty())
1008 *$1 += " " + *$2;
1009 if (!$1->empty())
1010 *$1 += " ";
Reid Spencere77e35e2006-12-01 20:26:20 +00001011 *$1 += *$3.newTy + " " + *$4 + "(" + *$6 + ")";
1012 delete $2; $3.destroy(); delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001013 $$ = $1;
1014 }
1015 | MemoryInst ;
1016
1017
1018// IndexList - List of indices for GEP based instructions...
1019IndexList
1020 : ',' ValueRefList {
1021 $2->insert(0, ", ");
1022 $$ = $2;
1023 }
1024 | /* empty */ { $$ = new std::string(); }
1025 ;
1026
1027OptVolatile
1028 : VOLATILE
1029 | /* empty */ { $$ = new std::string(); }
1030 ;
1031
1032MemoryInst : MALLOC Types OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001033 *$1 += " " + *$2.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +00001034 if (!$3->empty())
1035 *$1 += " " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001036 $2.destroy(); delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001037 $$ = $1;
1038 }
1039 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001040 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001041 if (!$6->empty())
1042 *$1 += " " + *$6;
Reid Spencere77e35e2006-12-01 20:26:20 +00001043 $2.destroy(); $4.destroy(); delete $5; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001044 $$ = $1;
1045 }
1046 | ALLOCA Types OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001047 *$1 += " " + *$2.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +00001048 if (!$3->empty())
1049 *$1 += " " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001050 $2.destroy(); delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001051 $$ = $1;
1052 }
1053 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001054 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001055 if (!$6->empty())
1056 *$1 += " " + *$6;
Reid Spencere77e35e2006-12-01 20:26:20 +00001057 $2.destroy(); $4.destroy(); delete $5; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001058 $$ = $1;
1059 }
1060 | FREE ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001061 *$1 += " " + *$2.val;
1062 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001063 $$ = $1;
1064 }
1065 | OptVolatile LOAD Types ValueRef {
1066 if (!$1->empty())
1067 *$1 += " ";
Reid Spencere77e35e2006-12-01 20:26:20 +00001068 *$1 += *$2 + " " + *$3.newTy + " " + *$4;
1069 delete $2; $3.destroy(); delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001070 $$ = $1;
1071 }
1072 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1073 if (!$1->empty())
1074 *$1 += " ";
Reid Spencere77e35e2006-12-01 20:26:20 +00001075 *$1 += *$2 + " " + *$3.val + ", " + *$5.newTy + " " + *$6;
1076 delete $2; $3.destroy(); $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001077 $$ = $1;
1078 }
1079 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencere77e35e2006-12-01 20:26:20 +00001080 *$1 += *$2.newTy + " " + *$3 + " " + *$4;
1081 $2.destroy(); delete $3; delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001082 $$ = $1;
1083 };
1084
1085%%
1086
1087int yyerror(const char *ErrorMsg) {
1088 std::string where
1089 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1090 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
1091 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
1092 if (yychar == YYEMPTY || yychar == 0)
1093 errMsg += "end-of-file.";
1094 else
1095 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
1096 std::cerr << errMsg << '\n';
1097 exit(1);
1098}