blob: 79c1d947f6f40c05903a403b87a72e905d318c43 [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
Reid Spencerfcb5df82006-12-01 22:34:43 +0000158%token <String> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI UITOFP SITOFP
159%token <String> PTRTOINT INTTOPTR BITCAST
Reid Spencere77e35e2006-12-01 20:26:20 +0000160
161%type <String> OptAssign OptLinkage OptCallingConv OptAlign OptCAlign
162%type <String> SectionString OptSection GlobalVarAttributes GlobalVarAttribute
163%type <String> ArgTypeListI ConstExpr DefinitionList
164%type <String> ConstPool TargetDefinition LibrariesDefinition LibList OptName
165%type <String> ArgVal ArgListH ArgList FunctionHeaderH BEGIN FunctionHeader END
166%type <String> Function FunctionProto BasicBlock TypeListI
167%type <String> InstructionList BBTerminatorInst JumpTable Inst PHIList
168%type <String> ValueRefList OptTailCall InstVal IndexList OptVolatile
169%type <String> MemoryInst SymbolicValueRef OptSideEffect GlobalType
170%type <String> FnDeclareLinkage BasicBlockList BigOrLittle AsmBlock
Reid Spencerfcb5df82006-12-01 22:34:43 +0000171%type <String> Name ValueRef ValueRefListE ConstValueRef
172%type <String> ShiftOps SetCondOps LogicalOps ArithmeticOps CastOps
Reid Spencere77e35e2006-12-01 20:26:20 +0000173
174%type <String> ConstVector
175
176%type <Type> IntType SIntType UIntType FPType TypesV Types
177%type <Type> PrimType UpRTypesV UpRTypes
178
Reid Spencerf2d55322006-12-01 21:52:30 +0000179%type <String> IntVal EInt64Val
180%type <Const> ConstVal
Reid Spencere77e35e2006-12-01 20:26:20 +0000181
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 Spencerf2d55322006-12-01 21:52:30 +0000189IntVal : SINTVAL | UINTVAL ;
Reid Spencere77e35e2006-12-01 20:26:20 +0000190EInt64Val : 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 Spencerf7bde222006-12-01 22:26:37 +0000197ShiftOps : SHL | SHR | ASHR | LSHR;
Reid Spencerfcb5df82006-12-01 22:34:43 +0000198CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI |
199 UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
200 ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000201
202// These are some types that allow classification if we only want a particular
203// thing... for example, only a signed, unsigned, or integral type.
204SIntType : LONG | INT | SHORT | SBYTE;
205UIntType : ULONG | UINT | USHORT | UBYTE;
206IntType : SIntType | UIntType;
207FPType : FLOAT | DOUBLE;
208
209// OptAssign - Value producing statements have an optional assignment component
210OptAssign : Name '=' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000211 *$1 += " = ";
Reid Spencere7c3c602006-11-30 06:36:44 +0000212 $$ = $1;
213 }
214 | /*empty*/ {
215 $$ = new std::string("");
216 };
217
218OptLinkage
219 : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT
220 | EXTERN_WEAK
221 | /*empty*/ { $$ = new std::string(""); } ;
222
223OptCallingConv
224 : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK
Reid Spencer16244f42006-12-01 21:10:07 +0000225 | X86_FASTCALLCC_TOK
226 | CC_TOK EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000227 *$1 += *$2;
228 delete $2;
Reid Spencer16244f42006-12-01 21:10:07 +0000229 $$ = $1;
230 }
Reid Spencere7c3c602006-11-30 06:36:44 +0000231 | /*empty*/ { $$ = new std::string(""); } ;
232
233// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
234// a comma before it.
235OptAlign
236 : /*empty*/ { $$ = new std::string(); }
Reid Spencerf2d55322006-12-01 21:52:30 +0000237 | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
Reid Spencere7c3c602006-11-30 06:36:44 +0000238 ;
239OptCAlign
240 : /*empty*/ { $$ = new std::string(); }
241 | ',' ALIGN EUINT64VAL {
242 $2->insert(0, ", ");
Reid Spencerf2d55322006-12-01 21:52:30 +0000243 *$2 += " " + *$3;
244 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000245 $$ = $2;
246 };
247
248SectionString
249 : SECTION STRINGCONSTANT {
250 *$1 += " " + *$2;
251 delete $2;
252 $$ = $1;
253 };
254
255OptSection : /*empty*/ { $$ = new std::string(); }
256 | SectionString;
257
258GlobalVarAttributes
259 : /* empty */ { $$ = new std::string(); }
260 | ',' GlobalVarAttribute GlobalVarAttributes {
261 $2->insert(0, ", ");
262 if (!$3->empty())
263 *$2 += " " + *$3;
264 delete $3;
265 $$ = $2;
266 };
267
268GlobalVarAttribute
269 : SectionString
270 | ALIGN EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000271 *$1 += " " + *$2;
272 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000273 $$ = $1;
274 };
275
276//===----------------------------------------------------------------------===//
277// Types includes all predefined types... except void, because it can only be
278// used in specific contexts (function returning void for example). To have
279// access to it, a user must explicitly use TypesV.
280//
281
282// TypesV includes all of 'Types', but it also includes the void type.
283TypesV : Types | VOID ;
284UpRTypesV : UpRTypes | VOID ;
285Types : UpRTypes ;
286
287// Derived types are added later...
288//
289PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
Reid Spencere77e35e2006-12-01 20:26:20 +0000290PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
Reid Spencerd154b572006-12-01 20:36:40 +0000291UpRTypes : OPAQUE | PrimType
292 | SymbolicValueRef {
293 $$.newTy = $1; $$.oldTy = OpaqueTy;
294 };
Reid Spencere7c3c602006-11-30 06:36:44 +0000295
296// Include derived types in the Types production.
297//
298UpRTypes : '\\' EUINT64VAL { // Type UpReference
Reid Spencerf2d55322006-12-01 21:52:30 +0000299 $2->insert(0, "\\");
300 $$.newTy = $2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000301 $$.oldTy = OpaqueTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000302 }
303 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000304 *$1.newTy += "( " + *$3 + " )";
Reid Spencere7c3c602006-11-30 06:36:44 +0000305 delete $3;
Reid Spencere77e35e2006-12-01 20:26:20 +0000306 $$.newTy = $1.newTy;
307 $$.oldTy = FunctionTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000308 }
309 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencerf2d55322006-12-01 21:52:30 +0000310 $2->insert(0,"[ ");
311 *$2 += " x " + *$4.newTy + " ]";
Reid Spencere77e35e2006-12-01 20:26:20 +0000312 delete $4.newTy;
Reid Spencerf2d55322006-12-01 21:52:30 +0000313 $$.newTy = $2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000314 $$.oldTy = ArrayTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000315 }
316 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
Reid Spencerf2d55322006-12-01 21:52:30 +0000317 $2->insert(0,"< ");
318 *$2 += " x " + *$4.newTy + " >";
Reid Spencere77e35e2006-12-01 20:26:20 +0000319 delete $4.newTy;
Reid Spencerf2d55322006-12-01 21:52:30 +0000320 $$.newTy = $2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000321 $$.oldTy = PackedTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000322 }
323 | '{' TypeListI '}' { // Structure type?
324 $2->insert(0, "{ ");
325 *$2 += " }";
Reid Spencere77e35e2006-12-01 20:26:20 +0000326 $$.newTy = $2;
327 $$.oldTy = StructTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000328 }
329 | '{' '}' { // Empty structure type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000330 $$.newTy = new std::string("{ }");
331 $$.oldTy = StructTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000332 }
333 | UpRTypes '*' { // Pointer type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000334 *$1.newTy += '*';
335 $1.oldTy = PointerTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000336 $$ = $1;
337 };
338
339// TypeList - Used for struct declarations and as a basis for function type
340// declaration type lists
341//
Reid Spencere77e35e2006-12-01 20:26:20 +0000342TypeListI
343 : UpRTypes {
344 $$ = $1.newTy;
345 }
346 | TypeListI ',' UpRTypes {
347 *$1 += ", " + *$3.newTy;
348 delete $3.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000349 $$ = $1;
350 };
351
352// ArgTypeList - List of types for a function type declaration...
Reid Spencere77e35e2006-12-01 20:26:20 +0000353ArgTypeListI
354 : TypeListI
Reid Spencere7c3c602006-11-30 06:36:44 +0000355 | TypeListI ',' DOTDOTDOT {
356 *$1 += ", ...";
357 delete $3;
358 $$ = $1;
359 }
360 | DOTDOTDOT {
361 $$ = $1;
362 }
363 | /*empty*/ {
364 $$ = new std::string();
365 };
366
367// ConstVal - The various declarations that go into the constant pool. This
368// production is used ONLY to represent constants that show up AFTER a 'const',
369// 'constant' or 'global' token at global scope. Constants that can be inlined
370// into other expressions (such as integers and constexprs) are handled by the
371// ResolvedVal, ValueRef and ConstValueRef productions.
372//
373ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencere77e35e2006-12-01 20:26:20 +0000374 $$.type = $1;
375 $$.cnst = new std::string(*$1.newTy);
376 *$$.cnst += " [ " + *$3 + " ]";
Reid Spencere7c3c602006-11-30 06:36:44 +0000377 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000378 }
379 | Types '[' ']' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000380 $$.type = $1;
381 $$.cnst = new std::string(*$1.newTy);
382 *$$.cnst += "[ ]";
Reid Spencere7c3c602006-11-30 06:36:44 +0000383 }
384 | Types 'c' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000385 $$.type = $1;
386 $$.cnst = new std::string(*$1.newTy);
387 *$$.cnst += " c" + *$3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000388 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000389 }
390 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencere77e35e2006-12-01 20:26:20 +0000391 $$.type = $1;
392 $$.cnst = new std::string(*$1.newTy);
393 *$$.cnst += " < " + *$3 + " >";
Reid Spencere7c3c602006-11-30 06:36:44 +0000394 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000395 }
396 | Types '{' ConstVector '}' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000397 $$.type = $1;
398 $$.cnst = new std::string(*$1.newTy);
399 *$$.cnst += " { " + *$3 + " }";
Reid Spencere7c3c602006-11-30 06:36:44 +0000400 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000401 }
402 | Types '{' '}' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000403 $$.type = $1;
404 $$.cnst = new std::string(*$1.newTy);
405 *$$.cnst += " [ ]";
Reid Spencere7c3c602006-11-30 06:36:44 +0000406 }
407 | Types NULL_TOK {
Reid Spencere77e35e2006-12-01 20:26:20 +0000408 $$.type = $1;
409 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000410 *$$.cnst += " " + *$2;
411 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000412 }
413 | Types UNDEF {
Reid Spencere77e35e2006-12-01 20:26:20 +0000414 $$.type = $1;
415 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000416 *$$.cnst += " " + *$2;
417 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000418 }
419 | Types SymbolicValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000420 $$.type = $1;
421 $$.cnst = new std::string(*$1.newTy);
422 *$$.cnst += " " + *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000423 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000424 }
425 | Types ConstExpr {
Reid Spencere77e35e2006-12-01 20:26:20 +0000426 $$.type = $1;
427 $$.cnst = new std::string(*$1.newTy);
428 *$$.cnst += " " + *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000429 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000430 }
431 | Types ZEROINITIALIZER {
Reid Spencere77e35e2006-12-01 20:26:20 +0000432 $$.type = $1;
433 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000434 *$$.cnst += " " + *$2;
435 delete $2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000436 }
437 | SIntType EInt64Val { // integral constants
438 $$.type = $1;
439 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000440 *$$.cnst += " " + *$2;
441 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000442 }
443 | UIntType EUINT64VAL { // integral constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000444 $$.type = $1;
445 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000446 *$$.cnst += " " + *$2;
447 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000448 }
449 | BOOL TRUETOK { // Boolean constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000450 $$.type = $1;
451 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000452 *$$.cnst += " " + *$2;
453 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000454 }
455 | BOOL FALSETOK { // Boolean constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000456 $$.type = $1;
457 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000458 *$$.cnst += " " + *$2;
459 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000460 }
461 | FPType FPVAL { // Float & Double constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000462 $$.type = $1;
463 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000464 *$$.cnst += " " + *$2;
465 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000466 };
467
468
Reid Spencerfcb5df82006-12-01 22:34:43 +0000469ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000470 // We must infer the cast opcode from the types of the operands.
Reid Spencerfcb5df82006-12-01 22:34:43 +0000471 const char *opcode = $1->c_str();
472 if (*$1 == "cast")
473 opcode = getCastOpcode($3.type, $5);
Reid Spencere77e35e2006-12-01 20:26:20 +0000474 $$ = new std::string(opcode);
475 *$$ += "(" + *$3.cnst + " " + *$4 + " " + *$5.newTy + ")";
476 delete $1; $3.destroy(); delete $4; $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000477 }
478 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000479 *$1 += "(" + *$3.cnst + " " + *$4 + ")";
480 $$ = $1;
481 $3.destroy();
482 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +0000483 }
484 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000485 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
486 $3.destroy(); $5.destroy(); $7.destroy();
487 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000488 }
489 | ArithmeticOps '(' 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 | LogicalOps '(' 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 | SetCondOps '(' 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 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencerf7bde222006-12-01 22:26:37 +0000505 const char* shiftop = $1->c_str();
506 if (*$1 == "shr")
507 shiftop = ($3.type.isUnsigned()) ? "lshr" : "ashr";
508 $$ = new std::string(shiftop);
509 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
510 delete $1; $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000511 }
512 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000513 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
514 $3.destroy(); $5.destroy();
515 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000516 }
517 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000518 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
519 $3.destroy(); $5.destroy(); $7.destroy();
520 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000521 }
522 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000523 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
524 $3.destroy(); $5.destroy(); $7.destroy();
525 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000526 };
527
528
529// ConstVector - A list of comma separated constants.
Reid Spencere77e35e2006-12-01 20:26:20 +0000530
531ConstVector
532 : ConstVector ',' ConstVal {
533 *$1 += ", " + *$3.cnst;
534 $3.destroy();
535 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000536 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000537 | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
538 ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000539
540
541// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Reid Spencere77e35e2006-12-01 20:26:20 +0000542GlobalType : GLOBAL | CONSTANT ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000543
544
545//===----------------------------------------------------------------------===//
546// Rules to match Modules
547//===----------------------------------------------------------------------===//
548
549// Module rule: Capture the result of parsing the whole file into a result
550// variable...
551//
552Module : DefinitionList {
553};
554
555// DefinitionList - Top level definitions
556//
557DefinitionList : DefinitionList Function {
558 $$ = 0;
559 }
560 | DefinitionList FunctionProto {
561 *O << *$2 << "\n";
562 delete $2;
563 $$ = 0;
564 }
565 | DefinitionList MODULE ASM_TOK AsmBlock {
566 *O << "module asm " << " " << *$4 << "\n";
Reid Spencerd154b572006-12-01 20:36:40 +0000567 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000568 }
569 | DefinitionList IMPLEMENTATION {
570 *O << "implementation\n";
Reid Spencerd154b572006-12-01 20:36:40 +0000571 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000572 }
Reid Spencerd154b572006-12-01 20:36:40 +0000573 | ConstPool;
Reid Spencere7c3c602006-11-30 06:36:44 +0000574
575// ConstPool - Constants with optional names assigned to them.
576ConstPool : ConstPool OptAssign TYPE TypesV {
Reid Spencere77e35e2006-12-01 20:26:20 +0000577 *O << *$2 << " " << *$3 << " " << *$4.newTy << "\n";
578 // delete $2; delete $3; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000579 $$ = 0;
580 }
581 | ConstPool FunctionProto { // Function prototypes can be in const pool
582 *O << *$2 << "\n";
583 delete $2;
584 $$ = 0;
585 }
586 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
587 *O << *$2 << " " << *$3 << " " << *$4 << "\n";
588 delete $2; delete $3; delete $4;
589 $$ = 0;
590 }
591 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
Reid Spencere77e35e2006-12-01 20:26:20 +0000592 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.cnst << " "
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 EXTERNAL 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 DLLIMPORT 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 OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
Reid Spencere77e35e2006-12-01 20:26:20 +0000610 *O << *$2 << " " << *$3 << " " << *$4 << " " << *$5.newTy
611 << " " << *$6 << "\n";
612 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000613 $$ = 0;
614 }
615 | ConstPool TARGET TargetDefinition {
616 *O << *$2 << " " << *$3 << "\n";
617 delete $2; delete $3;
618 $$ = 0;
619 }
620 | ConstPool DEPLIBS '=' LibrariesDefinition {
621 *O << *$2 << " = " << *$4 << "\n";
622 delete $2; delete $4;
623 $$ = 0;
624 }
625 | /* empty: end of list */ {
626 $$ = 0;
627 };
628
629
630AsmBlock : STRINGCONSTANT ;
631
632BigOrLittle : BIG | LITTLE
633
634TargetDefinition
635 : ENDIAN '=' BigOrLittle {
Reid Spencere77e35e2006-12-01 20:26:20 +0000636 *$1 += " = " + *$3;
637 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000638 $$ = $1;
639 }
640 | POINTERSIZE '=' EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000641 *$1 += " = " + *$3;
642 if (*$3 == "64")
Reid Spencere77e35e2006-12-01 20:26:20 +0000643 SizeOfPointer = 64;
Reid Spencerf2d55322006-12-01 21:52:30 +0000644 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000645 $$ = $1;
646 }
647 | TRIPLE '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000648 *$1 += " = " + *$3;
649 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000650 $$ = $1;
651 }
652 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000653 *$1 += " = " + *$3;
654 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000655 $$ = $1;
656 };
657
658LibrariesDefinition
659 : '[' LibList ']' {
660 $2->insert(0, "[ ");
661 *$2 += " ]";
662 $$ = $2;
663 };
664
665LibList
666 : LibList ',' STRINGCONSTANT {
667 *$1 += ", " + *$3;
668 delete $3;
669 $$ = $1;
670 }
671 | STRINGCONSTANT
672 | /* empty: end of list */ {
673 $$ = new std::string();
674 };
675
676//===----------------------------------------------------------------------===//
677// Rules to match Function Headers
678//===----------------------------------------------------------------------===//
679
680Name : VAR_ID | STRINGCONSTANT;
681OptName : Name | /*empty*/ { $$ = new std::string(); };
682
683ArgVal : Types OptName {
Reid Spencere77e35e2006-12-01 20:26:20 +0000684 $$ = $1.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000685 if (!$2->empty())
686 *$$ += " " + *$2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000687 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000688};
689
690ArgListH : ArgListH ',' ArgVal {
691 *$1 += ", " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +0000692 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000693 }
694 | ArgVal {
695 $$ = $1;
696 };
697
698ArgList : ArgListH {
699 $$ = $1;
700 }
701 | ArgListH ',' DOTDOTDOT {
702 *$1 += ", ...";
703 $$ = $1;
Reid Spencere77e35e2006-12-01 20:26:20 +0000704 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000705 }
706 | DOTDOTDOT {
707 $$ = $1;
708 }
Reid Spencerd154b572006-12-01 20:36:40 +0000709 | /* empty */ { $$ = new std::string(); };
Reid Spencere7c3c602006-11-30 06:36:44 +0000710
711FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
712 OptSection OptAlign {
713 if (!$1->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000714 *$1 += " ";
Reid Spencere7c3c602006-11-30 06:36:44 +0000715 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000716 *$1 += *$2.newTy + " " + *$3 + "(" + *$5 + ")";
Reid Spencere7c3c602006-11-30 06:36:44 +0000717 if (!$7->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000718 *$1 += " " + *$7;
Reid Spencere7c3c602006-11-30 06:36:44 +0000719 }
720 if (!$8->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000721 *$1 += " " + *$8;
Reid Spencere7c3c602006-11-30 06:36:44 +0000722 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000723 $2.destroy();
724 delete $3;
725 delete $5;
726 delete $7;
727 delete $8;
728 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000729 };
730
731BEGIN : BEGINTOK {
732 $$ = new std::string("begin");
733 }
734 | '{' {
735 $$ = new std::string ("{");
736 }
737
738FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
739 if (!$1->empty()) {
740 *O << *$1 << " ";
741 }
742 *O << *$2 << " " << *$3 << "\n";
743 delete $1; delete $2; delete $3;
744 $$ = 0;
745};
746
747END : ENDTOK { $$ = new std::string("end"); }
748 | '}' { $$ = new std::string("}"); };
749
750Function : FunctionHeader BasicBlockList END {
751 if ($2)
752 *O << *$2;
753 *O << '\n' << *$3 << "\n";
Reid Spencere77e35e2006-12-01 20:26:20 +0000754 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000755};
756
Reid Spencere77e35e2006-12-01 20:26:20 +0000757FnDeclareLinkage
758 : /*default*/ { $$ = new std::string(); }
Reid Spencere7c3c602006-11-30 06:36:44 +0000759 | DLLIMPORT
760 | EXTERN_WEAK
761 ;
762
763FunctionProto
764 : DECLARE FnDeclareLinkage FunctionHeaderH {
Reid Spencere77e35e2006-12-01 20:26:20 +0000765 if (!$2->empty())
766 *$1 += " " + *$2;
767 *$1 += " " + *$3;
768 delete $2;
769 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000770 $$ = $1;
771 };
772
773//===----------------------------------------------------------------------===//
774// Rules to match Basic Blocks
775//===----------------------------------------------------------------------===//
776
Reid Spencerd154b572006-12-01 20:36:40 +0000777OptSideEffect : /* empty */ { $$ = new std::string(); }
778 | SIDEEFFECT;
Reid Spencere7c3c602006-11-30 06:36:44 +0000779
Reid Spencere77e35e2006-12-01 20:26:20 +0000780ConstValueRef
Reid Spencerf2d55322006-12-01 21:52:30 +0000781 : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK | NULL_TOK | UNDEF
782 | ZEROINITIALIZER
Reid Spencere7c3c602006-11-30 06:36:44 +0000783 | '<' ConstVector '>' {
784 $2->insert(0, "<");
785 *$2 += ">";
786 $$ = $2;
787 }
788 | ConstExpr
789 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
790 if (!$2->empty()) {
791 *$1 += " " + *$2;
792 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000793 *$1 += " " + *$3 + ", " + *$5;
794 delete $2; delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +0000795 $$ = $1;
796 };
797
Reid Spencerf2d55322006-12-01 21:52:30 +0000798SymbolicValueRef : IntVal | Name ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000799
800// ValueRef - A reference to a definition... either constant or symbolic
801ValueRef : SymbolicValueRef | ConstValueRef;
802
803
804// ResolvedVal - a <type> <value> pair. This is used only in cases where the
805// type immediately preceeds the value reference, and allows complex constant
806// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
807ResolvedVal : Types ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000808 $$.type = $1;
809 $$.val = new std::string(*$1.newTy + " ");
810 *$$.val += *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000811 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000812 };
813
814BasicBlockList : BasicBlockList BasicBlock {
Reid Spencerf2d55322006-12-01 21:52:30 +0000815 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000816 }
817 | BasicBlock { // Do not allow functions with 0 basic blocks
Reid Spencerf2d55322006-12-01 21:52:30 +0000818 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000819 };
820
821
822// Basic blocks are terminated by branching instructions:
823// br, br/cc, switch, ret
824//
Reid Spencer16244f42006-12-01 21:10:07 +0000825BasicBlock : InstructionList BBTerminatorInst {
Reid Spencerf2d55322006-12-01 21:52:30 +0000826 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000827 };
828
829InstructionList : InstructionList Inst {
830 *O << " " << *$2 << "\n";
831 delete $2;
832 $$ = 0;
833 }
834 | /* empty */ {
835 $$ = 0;
836 }
837 | LABELSTR {
838 *O << *$1 << "\n";
839 delete $1;
840 $$ = 0;
841 };
842
843BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencere77e35e2006-12-01 20:26:20 +0000844 *O << " " << *$1 << " " << *$2.val << "\n";
845 delete $1; $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000846 $$ = 0;
847 }
848 | RET VOID { // Return with no result...
Reid Spencere77e35e2006-12-01 20:26:20 +0000849 *O << " " << *$1 << " " << *$2.newTy << "\n";
850 delete $1; $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000851 $$ = 0;
852 }
853 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencere77e35e2006-12-01 20:26:20 +0000854 *O << " " << *$1 << " " << *$2.newTy << " " << *$3 << "\n";
855 delete $1; $2.destroy(); delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000856 $$ = 0;
857 } // Conditional Branch...
858 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000859 *O << " " << *$1 << " " << *$2.newTy << " " << *$3 << ", "
860 << *$5.newTy << " " << *$6 << ", " << *$8.newTy << " " << *$9 << "\n";
861 delete $1; $2.destroy(); delete $3; $5.destroy(); delete $6;
862 $8.destroy(); delete $9;
Reid Spencere7c3c602006-11-30 06:36:44 +0000863 $$ = 0;
864 }
865 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000866 *O << " " << *$1 << " " << *$2.newTy << " " << *$3 << ", " << *$5.newTy
867 << " " << *$6 << " [" << *$8 << " ]\n";
868 delete $1; $2.destroy(); delete $3; $5.destroy(); delete $6; delete $8;
Reid Spencere7c3c602006-11-30 06:36:44 +0000869 $$ = 0;
870 }
871 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000872 *O << " " << *$1 << " " << *$2.newTy << " " << *$3 << ", "
873 << *$5.newTy << " " << *$6 << "[]\n";
874 delete $1; $2.destroy(); delete $3; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000875 $$ = 0;
876 }
Reid Spencer16244f42006-12-01 21:10:07 +0000877 | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
Reid Spencere7c3c602006-11-30 06:36:44 +0000878 TO LABEL ValueRef UNWIND LABEL ValueRef {
Reid Spencer16244f42006-12-01 21:10:07 +0000879 *O << " ";
880 if (!$1->empty())
881 *O << *$1;
882 *O << *$2 << " " << *$3 << " " << *$4.newTy << " " << *$5 << " ("
883 << *$7 << ") " << *$9 << " " << *$10.newTy << " " << *$11 << " "
884 << *$12 << " " << *$13.newTy << " " << *$14 << "\n";
885 delete $1; delete $2; delete $3; $4.destroy(); delete $5; delete $7;
886 delete $9; $10.destroy(); delete $11; delete $12; $13.destroy();
887 delete $14;
Reid Spencere7c3c602006-11-30 06:36:44 +0000888 $$ = 0;
889 }
890 | UNWIND {
891 *O << " " << *$1 << "\n";
892 delete $1;
893 $$ = 0;
894 }
895 | UNREACHABLE {
896 *O << " " << *$1 << "\n";
897 delete $1;
898 $$ = 0;
899 };
900
901JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencer16244f42006-12-01 21:10:07 +0000902 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5.newTy + " " + *$6;
Reid Spencere77e35e2006-12-01 20:26:20 +0000903 $2.destroy(); delete $3; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000904 $$ = $1;
905 }
906 | IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000907 $2->insert(0, *$1.newTy + " " );
908 *$2 += ", " + *$4.newTy + " " + *$5;
909 $1.destroy(); $4.destroy(); delete $5;
910 $$ = $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000911 };
912
913Inst
914 : OptAssign InstVal {
915 *$1 += *$2;
916 delete $2;
917 $$ = $1;
918 };
919
920PHIList
921 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencere77e35e2006-12-01 20:26:20 +0000922 $3->insert(0, *$1.newTy + "[");
923 *$3 += "," + *$5 + "]";
924 $1.destroy(); delete $5;
925 $$ = $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000926 }
927 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
928 *$1 += ", [" + *$4 + "," + *$6 + "]";
929 delete $4; delete $6;
930 $$ = $1;
931 };
932
933
934ValueRefList
Reid Spencere77e35e2006-12-01 20:26:20 +0000935 : ResolvedVal { $$ = new std::string(*$1.val); $1.destroy(); }
Reid Spencere7c3c602006-11-30 06:36:44 +0000936 | ValueRefList ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +0000937 *$1 += ", " + *$3.val;
938 $3.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000939 $$ = $1;
940 };
941
942// ValueRefListE - Just like ValueRefList, except that it may also be empty!
943ValueRefListE
944 : ValueRefList
945 | /*empty*/ { $$ = new std::string(); }
946 ;
947
948OptTailCall
949 : TAIL CALL {
950 *$1 += " " + *$2;
951 delete $2;
952 $$ = $1;
953 }
954 | CALL
955 ;
956
957InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000958 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5;
959 $2.destroy(); delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +0000960 $$ = $1;
961 }
962 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000963 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5;
964 $2.destroy(); delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +0000965 $$ = $1;
966 }
967 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000968 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5;
969 $2.destroy(); delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +0000970 $$ = $1;
971 }
972 | NOT ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +0000973 *$1 += " " + *$2.val;
974 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000975 $$ = $1;
976 }
977 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencerf7bde222006-12-01 22:26:37 +0000978 const char* shiftop = $1->c_str();
979 if (*$1 == "shr")
980 shiftop = ($2.type.isUnsigned()) ? "lshr" : "ashr";
981 $$ = new std::string(shiftop);
982 *$$ += " " + *$2.val + ", " + *$4.val;
983 delete $1; $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000984 }
Reid Spencerfcb5df82006-12-01 22:34:43 +0000985 | CastOps ResolvedVal TO Types {
986 const char *opcode = $1->c_str();
987 if (*$1 == "cast")
988 opcode = getCastOpcode($2.type, $4);
Reid Spencere77e35e2006-12-01 20:26:20 +0000989 $$ = new std::string(opcode);
990 *$$ += *$2.val + " " + *$3 + " " + *$4.newTy;
991 delete $1; $2.destroy();
992 delete $3; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000993 }
994 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +0000995 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
996 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000997 $$ = $1;
998 }
999 | VAARG ResolvedVal ',' Types {
Reid Spencere77e35e2006-12-01 20:26:20 +00001000 *$1 += " " + *$2.val + ", " + *$4.newTy;
1001 $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001002 $$ = $1;
1003 }
1004 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001005 *$1 += " " + *$2.val + ", " + *$4.val;
1006 $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001007 $$ = $1;
1008 }
1009 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001010 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1011 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001012 $$ = $1;
1013 }
1014 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001015 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1016 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001017 $$ = $1;
1018 }
1019 | PHI_TOK PHIList {
1020 *$1 += " " + *$2;
1021 delete $2;
1022 $$ = $1;
1023 }
1024 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
1025 if (!$2->empty())
1026 *$1 += " " + *$2;
1027 if (!$1->empty())
1028 *$1 += " ";
Reid Spencere77e35e2006-12-01 20:26:20 +00001029 *$1 += *$3.newTy + " " + *$4 + "(" + *$6 + ")";
1030 delete $2; $3.destroy(); delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001031 $$ = $1;
1032 }
1033 | MemoryInst ;
1034
1035
1036// IndexList - List of indices for GEP based instructions...
1037IndexList
1038 : ',' ValueRefList {
1039 $2->insert(0, ", ");
1040 $$ = $2;
1041 }
1042 | /* empty */ { $$ = new std::string(); }
1043 ;
1044
1045OptVolatile
1046 : VOLATILE
1047 | /* empty */ { $$ = new std::string(); }
1048 ;
1049
1050MemoryInst : MALLOC Types OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001051 *$1 += " " + *$2.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +00001052 if (!$3->empty())
1053 *$1 += " " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001054 $2.destroy(); delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001055 $$ = $1;
1056 }
1057 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001058 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001059 if (!$6->empty())
1060 *$1 += " " + *$6;
Reid Spencere77e35e2006-12-01 20:26:20 +00001061 $2.destroy(); $4.destroy(); delete $5; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001062 $$ = $1;
1063 }
1064 | ALLOCA Types OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001065 *$1 += " " + *$2.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +00001066 if (!$3->empty())
1067 *$1 += " " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001068 $2.destroy(); delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001069 $$ = $1;
1070 }
1071 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001072 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001073 if (!$6->empty())
1074 *$1 += " " + *$6;
Reid Spencere77e35e2006-12-01 20:26:20 +00001075 $2.destroy(); $4.destroy(); delete $5; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001076 $$ = $1;
1077 }
1078 | FREE ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001079 *$1 += " " + *$2.val;
1080 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001081 $$ = $1;
1082 }
1083 | OptVolatile LOAD Types ValueRef {
1084 if (!$1->empty())
1085 *$1 += " ";
Reid Spencere77e35e2006-12-01 20:26:20 +00001086 *$1 += *$2 + " " + *$3.newTy + " " + *$4;
1087 delete $2; $3.destroy(); delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001088 $$ = $1;
1089 }
1090 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1091 if (!$1->empty())
1092 *$1 += " ";
Reid Spencere77e35e2006-12-01 20:26:20 +00001093 *$1 += *$2 + " " + *$3.val + ", " + *$5.newTy + " " + *$6;
1094 delete $2; $3.destroy(); $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001095 $$ = $1;
1096 }
1097 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencere77e35e2006-12-01 20:26:20 +00001098 *$1 += *$2.newTy + " " + *$3 + " " + *$4;
1099 $2.destroy(); delete $3; delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001100 $$ = $1;
1101 };
1102
1103%%
1104
1105int yyerror(const char *ErrorMsg) {
1106 std::string where
1107 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1108 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
1109 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
1110 if (yychar == YYEMPTY || yychar == 0)
1111 errMsg += "end-of-file.";
1112 else
1113 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
1114 std::cerr << errMsg << '\n';
1115 exit(1);
1116}