blob: 45749777c63c02e38a600ce368ed7d2e1d5b7ea9 [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
Reid Spencerf2d55322006-12-01 21:52:30 +0000137%token <Type> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
138%token <Type> FLOAT DOUBLE LABEL OPAQUE
139%token <String> ESINT64VAL EUINT64VAL SINTVAL UINTVAL FPVAL
140%token <String> NULL_TOK UNDEF ZEROINITIALIZER TRUETOK FALSETOK
Reid Spencere77e35e2006-12-01 20:26:20 +0000141%token <String> TYPE VAR_ID LABELSTR STRINGCONSTANT
142%token <String> IMPLEMENTATION BEGINTOK ENDTOK
143%token <String> DECLARE GLOBAL CONSTANT SECTION VOLATILE
144%token <String> TO DOTDOTDOT CONST INTERNAL LINKONCE WEAK
145%token <String> DLLIMPORT DLLEXPORT EXTERN_WEAK APPENDING
146%token <String> NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
147%token <String> ALIGN
148%token <String> DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
149%token <String> CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
150%token <String> X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
151%token <String> DATALAYOUT
152%token <String> RET BR SWITCH INVOKE UNWIND UNREACHABLE
153%token <String> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
154%token <String> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
155%token <String> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Reid Spencerf7bde222006-12-01 22:26:37 +0000156%token <String> PHI_TOK SELECT SHL SHR ASHR LSHR VAARG
Reid Spencere77e35e2006-12-01 20:26:20 +0000157%token <String> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
158%token <String> CAST
159
160%type <String> OptAssign OptLinkage OptCallingConv OptAlign OptCAlign
161%type <String> SectionString OptSection GlobalVarAttributes GlobalVarAttribute
162%type <String> ArgTypeListI ConstExpr DefinitionList
163%type <String> ConstPool TargetDefinition LibrariesDefinition LibList OptName
164%type <String> ArgVal ArgListH ArgList FunctionHeaderH BEGIN FunctionHeader END
165%type <String> Function FunctionProto BasicBlock TypeListI
166%type <String> InstructionList BBTerminatorInst JumpTable Inst PHIList
167%type <String> ValueRefList OptTailCall InstVal IndexList OptVolatile
168%type <String> MemoryInst SymbolicValueRef OptSideEffect GlobalType
169%type <String> FnDeclareLinkage BasicBlockList BigOrLittle AsmBlock
170%type <String> Name ValueRef ValueRefListE
171%type <String> ShiftOps SetCondOps LogicalOps ArithmeticOps ConstValueRef
172
173%type <String> ConstVector
174
175%type <Type> IntType SIntType UIntType FPType TypesV Types
176%type <Type> PrimType UpRTypesV UpRTypes
177
Reid Spencerf2d55322006-12-01 21:52:30 +0000178%type <String> IntVal EInt64Val
179%type <Const> ConstVal
Reid Spencere77e35e2006-12-01 20:26:20 +0000180
181%type <Value> ResolvedVal
Reid Spencere7c3c602006-11-30 06:36:44 +0000182
183%start Module
184
185%%
186
187// Handle constant integer size restriction and conversion...
Reid Spencerf2d55322006-12-01 21:52:30 +0000188IntVal : SINTVAL | UINTVAL ;
Reid Spencere77e35e2006-12-01 20:26:20 +0000189EInt64Val : ESINT64VAL | EUINT64VAL;
Reid Spencere7c3c602006-11-30 06:36:44 +0000190
191// Operations that are notably excluded from this list include:
192// RET, BR, & SWITCH because they end basic blocks and are treated specially.
193ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
194LogicalOps : AND | OR | XOR;
195SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Reid Spencerf7bde222006-12-01 22:26:37 +0000196ShiftOps : SHL | SHR | ASHR | LSHR;
Reid Spencere7c3c602006-11-30 06:36:44 +0000197
198// These are some types that allow classification if we only want a particular
199// thing... for example, only a signed, unsigned, or integral type.
200SIntType : LONG | INT | SHORT | SBYTE;
201UIntType : ULONG | UINT | USHORT | UBYTE;
202IntType : SIntType | UIntType;
203FPType : FLOAT | DOUBLE;
204
205// OptAssign - Value producing statements have an optional assignment component
206OptAssign : Name '=' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000207 *$1 += " = ";
Reid Spencere7c3c602006-11-30 06:36:44 +0000208 $$ = $1;
209 }
210 | /*empty*/ {
211 $$ = new std::string("");
212 };
213
214OptLinkage
215 : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT
216 | EXTERN_WEAK
217 | /*empty*/ { $$ = new std::string(""); } ;
218
219OptCallingConv
220 : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK
Reid Spencer16244f42006-12-01 21:10:07 +0000221 | X86_FASTCALLCC_TOK
222 | CC_TOK EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000223 *$1 += *$2;
224 delete $2;
Reid Spencer16244f42006-12-01 21:10:07 +0000225 $$ = $1;
226 }
Reid Spencere7c3c602006-11-30 06:36:44 +0000227 | /*empty*/ { $$ = new std::string(""); } ;
228
229// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
230// a comma before it.
231OptAlign
232 : /*empty*/ { $$ = new std::string(); }
Reid Spencerf2d55322006-12-01 21:52:30 +0000233 | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
Reid Spencere7c3c602006-11-30 06:36:44 +0000234 ;
235OptCAlign
236 : /*empty*/ { $$ = new std::string(); }
237 | ',' ALIGN EUINT64VAL {
238 $2->insert(0, ", ");
Reid Spencerf2d55322006-12-01 21:52:30 +0000239 *$2 += " " + *$3;
240 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000241 $$ = $2;
242 };
243
244SectionString
245 : SECTION STRINGCONSTANT {
246 *$1 += " " + *$2;
247 delete $2;
248 $$ = $1;
249 };
250
251OptSection : /*empty*/ { $$ = new std::string(); }
252 | SectionString;
253
254GlobalVarAttributes
255 : /* empty */ { $$ = new std::string(); }
256 | ',' GlobalVarAttribute GlobalVarAttributes {
257 $2->insert(0, ", ");
258 if (!$3->empty())
259 *$2 += " " + *$3;
260 delete $3;
261 $$ = $2;
262 };
263
264GlobalVarAttribute
265 : SectionString
266 | ALIGN EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000267 *$1 += " " + *$2;
268 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000269 $$ = $1;
270 };
271
272//===----------------------------------------------------------------------===//
273// Types includes all predefined types... except void, because it can only be
274// used in specific contexts (function returning void for example). To have
275// access to it, a user must explicitly use TypesV.
276//
277
278// TypesV includes all of 'Types', but it also includes the void type.
279TypesV : Types | VOID ;
280UpRTypesV : UpRTypes | VOID ;
281Types : UpRTypes ;
282
283// Derived types are added later...
284//
285PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
Reid Spencere77e35e2006-12-01 20:26:20 +0000286PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
Reid Spencerd154b572006-12-01 20:36:40 +0000287UpRTypes : OPAQUE | PrimType
288 | SymbolicValueRef {
289 $$.newTy = $1; $$.oldTy = OpaqueTy;
290 };
Reid Spencere7c3c602006-11-30 06:36:44 +0000291
292// Include derived types in the Types production.
293//
294UpRTypes : '\\' EUINT64VAL { // Type UpReference
Reid Spencerf2d55322006-12-01 21:52:30 +0000295 $2->insert(0, "\\");
296 $$.newTy = $2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000297 $$.oldTy = OpaqueTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000298 }
299 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000300 *$1.newTy += "( " + *$3 + " )";
Reid Spencere7c3c602006-11-30 06:36:44 +0000301 delete $3;
Reid Spencere77e35e2006-12-01 20:26:20 +0000302 $$.newTy = $1.newTy;
303 $$.oldTy = FunctionTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000304 }
305 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencerf2d55322006-12-01 21:52:30 +0000306 $2->insert(0,"[ ");
307 *$2 += " x " + *$4.newTy + " ]";
Reid Spencere77e35e2006-12-01 20:26:20 +0000308 delete $4.newTy;
Reid Spencerf2d55322006-12-01 21:52:30 +0000309 $$.newTy = $2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000310 $$.oldTy = ArrayTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000311 }
312 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
Reid Spencerf2d55322006-12-01 21:52:30 +0000313 $2->insert(0,"< ");
314 *$2 += " x " + *$4.newTy + " >";
Reid Spencere77e35e2006-12-01 20:26:20 +0000315 delete $4.newTy;
Reid Spencerf2d55322006-12-01 21:52:30 +0000316 $$.newTy = $2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000317 $$.oldTy = PackedTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000318 }
319 | '{' TypeListI '}' { // Structure type?
320 $2->insert(0, "{ ");
321 *$2 += " }";
Reid Spencere77e35e2006-12-01 20:26:20 +0000322 $$.newTy = $2;
323 $$.oldTy = StructTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000324 }
325 | '{' '}' { // Empty structure type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000326 $$.newTy = new std::string("{ }");
327 $$.oldTy = StructTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000328 }
329 | UpRTypes '*' { // Pointer type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000330 *$1.newTy += '*';
331 $1.oldTy = PointerTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000332 $$ = $1;
333 };
334
335// TypeList - Used for struct declarations and as a basis for function type
336// declaration type lists
337//
Reid Spencere77e35e2006-12-01 20:26:20 +0000338TypeListI
339 : UpRTypes {
340 $$ = $1.newTy;
341 }
342 | TypeListI ',' UpRTypes {
343 *$1 += ", " + *$3.newTy;
344 delete $3.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000345 $$ = $1;
346 };
347
348// ArgTypeList - List of types for a function type declaration...
Reid Spencere77e35e2006-12-01 20:26:20 +0000349ArgTypeListI
350 : TypeListI
Reid Spencere7c3c602006-11-30 06:36:44 +0000351 | TypeListI ',' DOTDOTDOT {
352 *$1 += ", ...";
353 delete $3;
354 $$ = $1;
355 }
356 | DOTDOTDOT {
357 $$ = $1;
358 }
359 | /*empty*/ {
360 $$ = new std::string();
361 };
362
363// ConstVal - The various declarations that go into the constant pool. This
364// production is used ONLY to represent constants that show up AFTER a 'const',
365// 'constant' or 'global' token at global scope. Constants that can be inlined
366// into other expressions (such as integers and constexprs) are handled by the
367// ResolvedVal, ValueRef and ConstValueRef productions.
368//
369ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencere77e35e2006-12-01 20:26:20 +0000370 $$.type = $1;
371 $$.cnst = new std::string(*$1.newTy);
372 *$$.cnst += " [ " + *$3 + " ]";
Reid Spencere7c3c602006-11-30 06:36:44 +0000373 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000374 }
375 | Types '[' ']' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000376 $$.type = $1;
377 $$.cnst = new std::string(*$1.newTy);
378 *$$.cnst += "[ ]";
Reid Spencere7c3c602006-11-30 06:36:44 +0000379 }
380 | Types 'c' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000381 $$.type = $1;
382 $$.cnst = new std::string(*$1.newTy);
383 *$$.cnst += " c" + *$3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000384 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000385 }
386 | Types '<' ConstVector '>' { // Nonempty unsized arr
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 '{' ConstVector '}' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000393 $$.type = $1;
394 $$.cnst = new std::string(*$1.newTy);
395 *$$.cnst += " { " + *$3 + " }";
Reid Spencere7c3c602006-11-30 06:36:44 +0000396 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000397 }
398 | Types '{' '}' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000399 $$.type = $1;
400 $$.cnst = new std::string(*$1.newTy);
401 *$$.cnst += " [ ]";
Reid Spencere7c3c602006-11-30 06:36:44 +0000402 }
403 | Types NULL_TOK {
Reid Spencere77e35e2006-12-01 20:26:20 +0000404 $$.type = $1;
405 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000406 *$$.cnst += " " + *$2;
407 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000408 }
409 | Types UNDEF {
Reid Spencere77e35e2006-12-01 20:26:20 +0000410 $$.type = $1;
411 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000412 *$$.cnst += " " + *$2;
413 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000414 }
415 | Types SymbolicValueRef {
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 ConstExpr {
Reid Spencere77e35e2006-12-01 20:26:20 +0000422 $$.type = $1;
423 $$.cnst = new std::string(*$1.newTy);
424 *$$.cnst += " " + *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000425 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000426 }
427 | Types ZEROINITIALIZER {
Reid Spencere77e35e2006-12-01 20:26:20 +0000428 $$.type = $1;
429 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000430 *$$.cnst += " " + *$2;
431 delete $2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000432 }
433 | SIntType EInt64Val { // integral constants
434 $$.type = $1;
435 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000436 *$$.cnst += " " + *$2;
437 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000438 }
439 | UIntType EUINT64VAL { // integral constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000440 $$.type = $1;
441 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000442 *$$.cnst += " " + *$2;
443 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000444 }
445 | BOOL TRUETOK { // Boolean constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000446 $$.type = $1;
447 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000448 *$$.cnst += " " + *$2;
449 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000450 }
451 | BOOL FALSETOK { // Boolean constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000452 $$.type = $1;
453 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000454 *$$.cnst += " " + *$2;
455 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000456 }
457 | FPType FPVAL { // Float & Double constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000458 $$.type = $1;
459 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000460 *$$.cnst += " " + *$2;
461 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000462 };
463
464
Reid Spencere77e35e2006-12-01 20:26:20 +0000465ConstExpr: CAST '(' ConstVal TO Types ')' {
466 // We must infer the cast opcode from the types of the operands.
467 const char *opcode = getCastOpcode($3.type, $5);
468 $$ = new std::string(opcode);
469 *$$ += "(" + *$3.cnst + " " + *$4 + " " + *$5.newTy + ")";
470 delete $1; $3.destroy(); delete $4; $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000471 }
472 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000473 *$1 += "(" + *$3.cnst + " " + *$4 + ")";
474 $$ = $1;
475 $3.destroy();
476 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +0000477 }
478 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000479 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
480 $3.destroy(); $5.destroy(); $7.destroy();
481 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000482 }
483 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000484 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
485 $3.destroy(); $5.destroy();
486 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000487 }
488 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000489 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
490 $3.destroy(); $5.destroy();
491 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000492 }
493 | SetCondOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000494 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
495 $3.destroy(); $5.destroy();
496 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000497 }
498 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencerf7bde222006-12-01 22:26:37 +0000499 const char* shiftop = $1->c_str();
500 if (*$1 == "shr")
501 shiftop = ($3.type.isUnsigned()) ? "lshr" : "ashr";
502 $$ = new std::string(shiftop);
503 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
504 delete $1; $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000505 }
506 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000507 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
508 $3.destroy(); $5.destroy();
509 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000510 }
511 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000512 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
513 $3.destroy(); $5.destroy(); $7.destroy();
514 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000515 }
516 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000517 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
518 $3.destroy(); $5.destroy(); $7.destroy();
519 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000520 };
521
522
523// ConstVector - A list of comma separated constants.
Reid Spencere77e35e2006-12-01 20:26:20 +0000524
525ConstVector
526 : ConstVector ',' ConstVal {
527 *$1 += ", " + *$3.cnst;
528 $3.destroy();
529 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000530 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000531 | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
532 ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000533
534
535// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Reid Spencere77e35e2006-12-01 20:26:20 +0000536GlobalType : GLOBAL | CONSTANT ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000537
538
539//===----------------------------------------------------------------------===//
540// Rules to match Modules
541//===----------------------------------------------------------------------===//
542
543// Module rule: Capture the result of parsing the whole file into a result
544// variable...
545//
546Module : DefinitionList {
547};
548
549// DefinitionList - Top level definitions
550//
551DefinitionList : DefinitionList Function {
552 $$ = 0;
553 }
554 | DefinitionList FunctionProto {
555 *O << *$2 << "\n";
556 delete $2;
557 $$ = 0;
558 }
559 | DefinitionList MODULE ASM_TOK AsmBlock {
560 *O << "module asm " << " " << *$4 << "\n";
Reid Spencerd154b572006-12-01 20:36:40 +0000561 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000562 }
563 | DefinitionList IMPLEMENTATION {
564 *O << "implementation\n";
Reid Spencerd154b572006-12-01 20:36:40 +0000565 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000566 }
Reid Spencerd154b572006-12-01 20:36:40 +0000567 | ConstPool;
Reid Spencere7c3c602006-11-30 06:36:44 +0000568
569// ConstPool - Constants with optional names assigned to them.
570ConstPool : ConstPool OptAssign TYPE TypesV {
Reid Spencere77e35e2006-12-01 20:26:20 +0000571 *O << *$2 << " " << *$3 << " " << *$4.newTy << "\n";
572 // delete $2; delete $3; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000573 $$ = 0;
574 }
575 | ConstPool FunctionProto { // Function prototypes can be in const pool
576 *O << *$2 << "\n";
577 delete $2;
578 $$ = 0;
579 }
580 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
581 *O << *$2 << " " << *$3 << " " << *$4 << "\n";
582 delete $2; delete $3; delete $4;
583 $$ = 0;
584 }
585 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
Reid Spencere77e35e2006-12-01 20:26:20 +0000586 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.cnst << " "
587 << *$6 << "\n";
588 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000589 $$ = 0;
590 }
591 | ConstPool OptAssign EXTERNAL GlobalType Types GlobalVarAttributes {
Reid Spencere77e35e2006-12-01 20:26:20 +0000592 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.newTy
593 << " " << *$6 << "\n";
594 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000595 $$ = 0;
596 }
597 | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
Reid Spencere77e35e2006-12-01 20:26:20 +0000598 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.newTy
599 << " " << *$6 << "\n";
600 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000601 $$ = 0;
602 }
603 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
Reid Spencere77e35e2006-12-01 20:26:20 +0000604 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.newTy
605 << " " << *$6 << "\n";
606 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000607 $$ = 0;
608 }
609 | ConstPool TARGET TargetDefinition {
610 *O << *$2 << " " << *$3 << "\n";
611 delete $2; delete $3;
612 $$ = 0;
613 }
614 | ConstPool DEPLIBS '=' LibrariesDefinition {
615 *O << *$2 << " = " << *$4 << "\n";
616 delete $2; delete $4;
617 $$ = 0;
618 }
619 | /* empty: end of list */ {
620 $$ = 0;
621 };
622
623
624AsmBlock : STRINGCONSTANT ;
625
626BigOrLittle : BIG | LITTLE
627
628TargetDefinition
629 : ENDIAN '=' BigOrLittle {
Reid Spencere77e35e2006-12-01 20:26:20 +0000630 *$1 += " = " + *$3;
631 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000632 $$ = $1;
633 }
634 | POINTERSIZE '=' EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000635 *$1 += " = " + *$3;
636 if (*$3 == "64")
Reid Spencere77e35e2006-12-01 20:26:20 +0000637 SizeOfPointer = 64;
Reid Spencerf2d55322006-12-01 21:52:30 +0000638 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000639 $$ = $1;
640 }
641 | TRIPLE '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000642 *$1 += " = " + *$3;
643 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000644 $$ = $1;
645 }
646 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000647 *$1 += " = " + *$3;
648 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000649 $$ = $1;
650 };
651
652LibrariesDefinition
653 : '[' LibList ']' {
654 $2->insert(0, "[ ");
655 *$2 += " ]";
656 $$ = $2;
657 };
658
659LibList
660 : LibList ',' STRINGCONSTANT {
661 *$1 += ", " + *$3;
662 delete $3;
663 $$ = $1;
664 }
665 | STRINGCONSTANT
666 | /* empty: end of list */ {
667 $$ = new std::string();
668 };
669
670//===----------------------------------------------------------------------===//
671// Rules to match Function Headers
672//===----------------------------------------------------------------------===//
673
674Name : VAR_ID | STRINGCONSTANT;
675OptName : Name | /*empty*/ { $$ = new std::string(); };
676
677ArgVal : Types OptName {
Reid Spencere77e35e2006-12-01 20:26:20 +0000678 $$ = $1.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000679 if (!$2->empty())
680 *$$ += " " + *$2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000681 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000682};
683
684ArgListH : ArgListH ',' ArgVal {
685 *$1 += ", " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +0000686 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000687 }
688 | ArgVal {
689 $$ = $1;
690 };
691
692ArgList : ArgListH {
693 $$ = $1;
694 }
695 | ArgListH ',' DOTDOTDOT {
696 *$1 += ", ...";
697 $$ = $1;
Reid Spencere77e35e2006-12-01 20:26:20 +0000698 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000699 }
700 | DOTDOTDOT {
701 $$ = $1;
702 }
Reid Spencerd154b572006-12-01 20:36:40 +0000703 | /* empty */ { $$ = new std::string(); };
Reid Spencere7c3c602006-11-30 06:36:44 +0000704
705FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
706 OptSection OptAlign {
707 if (!$1->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000708 *$1 += " ";
Reid Spencere7c3c602006-11-30 06:36:44 +0000709 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000710 *$1 += *$2.newTy + " " + *$3 + "(" + *$5 + ")";
Reid Spencere7c3c602006-11-30 06:36:44 +0000711 if (!$7->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000712 *$1 += " " + *$7;
Reid Spencere7c3c602006-11-30 06:36:44 +0000713 }
714 if (!$8->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000715 *$1 += " " + *$8;
Reid Spencere7c3c602006-11-30 06:36:44 +0000716 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000717 $2.destroy();
718 delete $3;
719 delete $5;
720 delete $7;
721 delete $8;
722 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000723 };
724
725BEGIN : BEGINTOK {
726 $$ = new std::string("begin");
727 }
728 | '{' {
729 $$ = new std::string ("{");
730 }
731
732FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
733 if (!$1->empty()) {
734 *O << *$1 << " ";
735 }
736 *O << *$2 << " " << *$3 << "\n";
737 delete $1; delete $2; delete $3;
738 $$ = 0;
739};
740
741END : ENDTOK { $$ = new std::string("end"); }
742 | '}' { $$ = new std::string("}"); };
743
744Function : FunctionHeader BasicBlockList END {
745 if ($2)
746 *O << *$2;
747 *O << '\n' << *$3 << "\n";
Reid Spencere77e35e2006-12-01 20:26:20 +0000748 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000749};
750
Reid Spencere77e35e2006-12-01 20:26:20 +0000751FnDeclareLinkage
752 : /*default*/ { $$ = new std::string(); }
Reid Spencere7c3c602006-11-30 06:36:44 +0000753 | DLLIMPORT
754 | EXTERN_WEAK
755 ;
756
757FunctionProto
758 : DECLARE FnDeclareLinkage FunctionHeaderH {
Reid Spencere77e35e2006-12-01 20:26:20 +0000759 if (!$2->empty())
760 *$1 += " " + *$2;
761 *$1 += " " + *$3;
762 delete $2;
763 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000764 $$ = $1;
765 };
766
767//===----------------------------------------------------------------------===//
768// Rules to match Basic Blocks
769//===----------------------------------------------------------------------===//
770
Reid Spencerd154b572006-12-01 20:36:40 +0000771OptSideEffect : /* empty */ { $$ = new std::string(); }
772 | SIDEEFFECT;
Reid Spencere7c3c602006-11-30 06:36:44 +0000773
Reid Spencere77e35e2006-12-01 20:26:20 +0000774ConstValueRef
Reid Spencerf2d55322006-12-01 21:52:30 +0000775 : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK | NULL_TOK | UNDEF
776 | ZEROINITIALIZER
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 Spencerf2d55322006-12-01 21:52:30 +0000792SymbolicValueRef : IntVal | 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 {
Reid Spencerf2d55322006-12-01 21:52:30 +0000809 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000810 }
811 | BasicBlock { // Do not allow functions with 0 basic blocks
Reid Spencerf2d55322006-12-01 21:52:30 +0000812 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000813 };
814
815
816// Basic blocks are terminated by branching instructions:
817// br, br/cc, switch, ret
818//
Reid Spencer16244f42006-12-01 21:10:07 +0000819BasicBlock : InstructionList BBTerminatorInst {
Reid Spencerf2d55322006-12-01 21:52:30 +0000820 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000821 };
822
823InstructionList : InstructionList Inst {
824 *O << " " << *$2 << "\n";
825 delete $2;
826 $$ = 0;
827 }
828 | /* empty */ {
829 $$ = 0;
830 }
831 | LABELSTR {
832 *O << *$1 << "\n";
833 delete $1;
834 $$ = 0;
835 };
836
837BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencere77e35e2006-12-01 20:26:20 +0000838 *O << " " << *$1 << " " << *$2.val << "\n";
839 delete $1; $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000840 $$ = 0;
841 }
842 | RET VOID { // Return with no result...
Reid Spencere77e35e2006-12-01 20:26:20 +0000843 *O << " " << *$1 << " " << *$2.newTy << "\n";
844 delete $1; $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000845 $$ = 0;
846 }
847 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencere77e35e2006-12-01 20:26:20 +0000848 *O << " " << *$1 << " " << *$2.newTy << " " << *$3 << "\n";
849 delete $1; $2.destroy(); delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000850 $$ = 0;
851 } // Conditional Branch...
852 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000853 *O << " " << *$1 << " " << *$2.newTy << " " << *$3 << ", "
854 << *$5.newTy << " " << *$6 << ", " << *$8.newTy << " " << *$9 << "\n";
855 delete $1; $2.destroy(); delete $3; $5.destroy(); delete $6;
856 $8.destroy(); delete $9;
Reid Spencere7c3c602006-11-30 06:36:44 +0000857 $$ = 0;
858 }
859 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000860 *O << " " << *$1 << " " << *$2.newTy << " " << *$3 << ", " << *$5.newTy
861 << " " << *$6 << " [" << *$8 << " ]\n";
862 delete $1; $2.destroy(); delete $3; $5.destroy(); delete $6; delete $8;
Reid Spencere7c3c602006-11-30 06:36:44 +0000863 $$ = 0;
864 }
865 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000866 *O << " " << *$1 << " " << *$2.newTy << " " << *$3 << ", "
867 << *$5.newTy << " " << *$6 << "[]\n";
868 delete $1; $2.destroy(); delete $3; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000869 $$ = 0;
870 }
Reid Spencer16244f42006-12-01 21:10:07 +0000871 | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
Reid Spencere7c3c602006-11-30 06:36:44 +0000872 TO LABEL ValueRef UNWIND LABEL ValueRef {
Reid Spencer16244f42006-12-01 21:10:07 +0000873 *O << " ";
874 if (!$1->empty())
875 *O << *$1;
876 *O << *$2 << " " << *$3 << " " << *$4.newTy << " " << *$5 << " ("
877 << *$7 << ") " << *$9 << " " << *$10.newTy << " " << *$11 << " "
878 << *$12 << " " << *$13.newTy << " " << *$14 << "\n";
879 delete $1; delete $2; delete $3; $4.destroy(); delete $5; delete $7;
880 delete $9; $10.destroy(); delete $11; delete $12; $13.destroy();
881 delete $14;
Reid Spencere7c3c602006-11-30 06:36:44 +0000882 $$ = 0;
883 }
884 | UNWIND {
885 *O << " " << *$1 << "\n";
886 delete $1;
887 $$ = 0;
888 }
889 | UNREACHABLE {
890 *O << " " << *$1 << "\n";
891 delete $1;
892 $$ = 0;
893 };
894
895JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencer16244f42006-12-01 21:10:07 +0000896 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5.newTy + " " + *$6;
Reid Spencere77e35e2006-12-01 20:26:20 +0000897 $2.destroy(); delete $3; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000898 $$ = $1;
899 }
900 | IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000901 $2->insert(0, *$1.newTy + " " );
902 *$2 += ", " + *$4.newTy + " " + *$5;
903 $1.destroy(); $4.destroy(); delete $5;
904 $$ = $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000905 };
906
907Inst
908 : OptAssign InstVal {
909 *$1 += *$2;
910 delete $2;
911 $$ = $1;
912 };
913
914PHIList
915 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencere77e35e2006-12-01 20:26:20 +0000916 $3->insert(0, *$1.newTy + "[");
917 *$3 += "," + *$5 + "]";
918 $1.destroy(); delete $5;
919 $$ = $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000920 }
921 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
922 *$1 += ", [" + *$4 + "," + *$6 + "]";
923 delete $4; delete $6;
924 $$ = $1;
925 };
926
927
928ValueRefList
Reid Spencere77e35e2006-12-01 20:26:20 +0000929 : ResolvedVal { $$ = new std::string(*$1.val); $1.destroy(); }
Reid Spencere7c3c602006-11-30 06:36:44 +0000930 | ValueRefList ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +0000931 *$1 += ", " + *$3.val;
932 $3.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000933 $$ = $1;
934 };
935
936// ValueRefListE - Just like ValueRefList, except that it may also be empty!
937ValueRefListE
938 : ValueRefList
939 | /*empty*/ { $$ = new std::string(); }
940 ;
941
942OptTailCall
943 : TAIL CALL {
944 *$1 += " " + *$2;
945 delete $2;
946 $$ = $1;
947 }
948 | CALL
949 ;
950
951InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000952 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5;
953 $2.destroy(); delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +0000954 $$ = $1;
955 }
956 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000957 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5;
958 $2.destroy(); delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +0000959 $$ = $1;
960 }
961 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000962 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5;
963 $2.destroy(); delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +0000964 $$ = $1;
965 }
966 | NOT ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +0000967 *$1 += " " + *$2.val;
968 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000969 $$ = $1;
970 }
971 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencerf7bde222006-12-01 22:26:37 +0000972 const char* shiftop = $1->c_str();
973 if (*$1 == "shr")
974 shiftop = ($2.type.isUnsigned()) ? "lshr" : "ashr";
975 $$ = new std::string(shiftop);
976 *$$ += " " + *$2.val + ", " + *$4.val;
977 delete $1; $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000978 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000979 | CAST ResolvedVal TO Types {
980 const char *opcode = getCastOpcode($2.type, $4);
981 $$ = new std::string(opcode);
982 *$$ += *$2.val + " " + *$3 + " " + *$4.newTy;
983 delete $1; $2.destroy();
984 delete $3; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000985 }
986 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +0000987 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
988 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000989 $$ = $1;
990 }
991 | VAARG ResolvedVal ',' Types {
Reid Spencere77e35e2006-12-01 20:26:20 +0000992 *$1 += " " + *$2.val + ", " + *$4.newTy;
993 $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000994 $$ = $1;
995 }
996 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +0000997 *$1 += " " + *$2.val + ", " + *$4.val;
998 $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000999 $$ = $1;
1000 }
1001 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001002 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1003 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001004 $$ = $1;
1005 }
1006 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001007 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1008 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001009 $$ = $1;
1010 }
1011 | PHI_TOK PHIList {
1012 *$1 += " " + *$2;
1013 delete $2;
1014 $$ = $1;
1015 }
1016 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
1017 if (!$2->empty())
1018 *$1 += " " + *$2;
1019 if (!$1->empty())
1020 *$1 += " ";
Reid Spencere77e35e2006-12-01 20:26:20 +00001021 *$1 += *$3.newTy + " " + *$4 + "(" + *$6 + ")";
1022 delete $2; $3.destroy(); delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001023 $$ = $1;
1024 }
1025 | MemoryInst ;
1026
1027
1028// IndexList - List of indices for GEP based instructions...
1029IndexList
1030 : ',' ValueRefList {
1031 $2->insert(0, ", ");
1032 $$ = $2;
1033 }
1034 | /* empty */ { $$ = new std::string(); }
1035 ;
1036
1037OptVolatile
1038 : VOLATILE
1039 | /* empty */ { $$ = new std::string(); }
1040 ;
1041
1042MemoryInst : MALLOC Types OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001043 *$1 += " " + *$2.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +00001044 if (!$3->empty())
1045 *$1 += " " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001046 $2.destroy(); delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001047 $$ = $1;
1048 }
1049 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001050 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001051 if (!$6->empty())
1052 *$1 += " " + *$6;
Reid Spencere77e35e2006-12-01 20:26:20 +00001053 $2.destroy(); $4.destroy(); delete $5; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001054 $$ = $1;
1055 }
1056 | ALLOCA Types OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001057 *$1 += " " + *$2.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +00001058 if (!$3->empty())
1059 *$1 += " " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001060 $2.destroy(); delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001061 $$ = $1;
1062 }
1063 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001064 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001065 if (!$6->empty())
1066 *$1 += " " + *$6;
Reid Spencere77e35e2006-12-01 20:26:20 +00001067 $2.destroy(); $4.destroy(); delete $5; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001068 $$ = $1;
1069 }
1070 | FREE ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001071 *$1 += " " + *$2.val;
1072 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001073 $$ = $1;
1074 }
1075 | OptVolatile LOAD Types ValueRef {
1076 if (!$1->empty())
1077 *$1 += " ";
Reid Spencere77e35e2006-12-01 20:26:20 +00001078 *$1 += *$2 + " " + *$3.newTy + " " + *$4;
1079 delete $2; $3.destroy(); delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001080 $$ = $1;
1081 }
1082 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1083 if (!$1->empty())
1084 *$1 += " ";
Reid Spencere77e35e2006-12-01 20:26:20 +00001085 *$1 += *$2 + " " + *$3.val + ", " + *$5.newTy + " " + *$6;
1086 delete $2; $3.destroy(); $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001087 $$ = $1;
1088 }
1089 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencere77e35e2006-12-01 20:26:20 +00001090 *$1 += *$2.newTy + " " + *$3 + " " + *$4;
1091 $2.destroy(); delete $3; delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001092 $$ = $1;
1093 };
1094
1095%%
1096
1097int yyerror(const char *ErrorMsg) {
1098 std::string where
1099 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1100 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
1101 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
1102 if (yychar == YYEMPTY || yychar == 0)
1103 errMsg += "end-of-file.";
1104 else
1105 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
1106 std::cerr << errMsg << '\n';
1107 exit(1);
1108}