blob: f44804e45397236051176bb6ac7e9376777dab1a [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 Spencer319a7302007-01-05 17:20:02 +000015#include "UpgradeInternals.h"
Reid Spencere7c3c602006-11-30 06:36:44 +000016#include <algorithm>
Reid Spencera50d5962006-12-02 04:11:07 +000017#include <map>
Reid Spencere7c3c602006-11-30 06:36:44 +000018#include <utility>
19#include <iostream>
20
Reid Spencere77e35e2006-12-01 20:26:20 +000021#define YYERROR_VERBOSE 1
Reid Spencer96839be2006-11-30 16:50:26 +000022#define YYINCLUDED_STDLIB_H
Reid Spencere77e35e2006-12-01 20:26:20 +000023#define YYDEBUG 1
Reid Spencere7c3c602006-11-30 06:36:44 +000024
25int yylex(); // declaration" of xxx warnings.
26int yyparse();
Reid Spencere77e35e2006-12-01 20:26:20 +000027extern int yydebug;
Reid Spencere7c3c602006-11-30 06:36:44 +000028
29static std::string CurFilename;
Reid Spencere7c3c602006-11-30 06:36:44 +000030static std::ostream *O = 0;
Reid Spencer96839be2006-11-30 16:50:26 +000031std::istream* LexInput = 0;
Reid Spencere77e35e2006-12-01 20:26:20 +000032unsigned SizeOfPointer = 32;
Reid Spencerf459d392006-12-02 16:19:52 +000033static uint64_t unique = 1;
Reid Spencer96839be2006-11-30 16:50:26 +000034
Reid Spencer71d2ec92006-12-31 06:02:26 +000035// This bool controls whether attributes are ever added to function declarations
36// definitions and calls.
37static bool AddAttributes = false;
38
Reid Spencerc4d96252007-01-13 00:03:30 +000039// This is set when a DECLARE keyword is recognized so that subsequent parsing
40// of a function prototype can know if its a declaration or definition.
41static bool isDeclare = false;
42
Reid Spencerf5626a32007-01-01 01:20:41 +000043// This bool is used to communicate between the InstVal and Inst rules about
44// whether or not a cast should be deleted. When the flag is set, InstVal has
45// determined that the cast is a candidate. However, it can only be deleted if
46// the value being casted is the same value name as the instruction. The Inst
47// rule makes that comparison if the flag is set and comments out the
48// instruction if they match.
49static bool deleteUselessCastFlag = false;
50static std::string* deleteUselessCastName = 0;
51
Reid Spencer319a7302007-01-05 17:20:02 +000052typedef std::vector<const TypeInfo*> TypeVector;
Reid Spencera50d5962006-12-02 04:11:07 +000053static TypeVector EnumeratedTypes;
Reid Spencer319a7302007-01-05 17:20:02 +000054typedef std::map<std::string,const TypeInfo*> TypeMap;
Reid Spencera50d5962006-12-02 04:11:07 +000055static TypeMap NamedTypes;
Reid Spencer319a7302007-01-05 17:20:02 +000056typedef std::map<const TypeInfo*,std::string> TypePlaneMap;
57typedef std::map<std::string,TypePlaneMap> GlobalsTypeMap;
58static GlobalsTypeMap Globals;
59
60static void warning(const std::string& msg);
Reid Spencera50d5962006-12-02 04:11:07 +000061
Reid Spencerf8483652006-12-02 15:16:01 +000062void destroy(ValueList* VL) {
63 while (!VL->empty()) {
64 ValueInfo& VI = VL->back();
65 VI.destroy();
66 VL->pop_back();
67 }
68 delete VL;
69}
70
Reid Spencer96839be2006-11-30 16:50:26 +000071void UpgradeAssembly(const std::string &infile, std::istream& in,
Reid Spencer71d2ec92006-12-31 06:02:26 +000072 std::ostream &out, bool debug, bool addAttrs)
Reid Spencere7c3c602006-11-30 06:36:44 +000073{
74 Upgradelineno = 1;
75 CurFilename = infile;
Reid Spencer96839be2006-11-30 16:50:26 +000076 LexInput = &in;
Reid Spencere77e35e2006-12-01 20:26:20 +000077 yydebug = debug;
Reid Spencer71d2ec92006-12-31 06:02:26 +000078 AddAttributes = addAttrs;
Reid Spencere7c3c602006-11-30 06:36:44 +000079 O = &out;
80
81 if (yyparse()) {
82 std::cerr << "Parse failed.\n";
Chris Lattner37e01c52007-01-04 18:46:42 +000083 out << "llvm-upgrade parse failed.\n";
Reid Spencere7c3c602006-11-30 06:36:44 +000084 exit(1);
85 }
86}
87
Reid Spencer319a7302007-01-05 17:20:02 +000088TypeInfo::TypeRegMap TypeInfo::registry;
89
90const TypeInfo* TypeInfo::get(const std::string &newType, Types oldType) {
91 TypeInfo* Ty = new TypeInfo();
92 Ty->newTy = newType;
93 Ty->oldTy = oldType;
94 return add_new_type(Ty);
95}
96
97const TypeInfo* TypeInfo::get(const std::string& newType, Types oldType,
98 const TypeInfo* eTy, const TypeInfo* rTy) {
99 TypeInfo* Ty= new TypeInfo();
100 Ty->newTy = newType;
101 Ty->oldTy = oldType;
102 Ty->elemTy = const_cast<TypeInfo*>(eTy);
103 Ty->resultTy = const_cast<TypeInfo*>(rTy);
104 return add_new_type(Ty);
105}
106
107const TypeInfo* TypeInfo::get(const std::string& newType, Types oldType,
108 const TypeInfo *eTy, uint64_t elems) {
109 TypeInfo* Ty = new TypeInfo();
110 Ty->newTy = newType;
111 Ty->oldTy = oldType;
112 Ty->elemTy = const_cast<TypeInfo*>(eTy);
113 Ty->nelems = elems;
114 return add_new_type(Ty);
115}
116
117const TypeInfo* TypeInfo::get(const std::string& newType, Types oldType,
118 TypeList* TL) {
119 TypeInfo* Ty = new TypeInfo();
120 Ty->newTy = newType;
121 Ty->oldTy = oldType;
122 Ty->elements = TL;
123 return add_new_type(Ty);
124}
125
126const TypeInfo* TypeInfo::get(const std::string& newType, const TypeInfo* resTy,
127 TypeList* TL) {
128 TypeInfo* Ty = new TypeInfo();
129 Ty->newTy = newType;
130 Ty->oldTy = FunctionTy;
131 Ty->resultTy = const_cast<TypeInfo*>(resTy);
132 Ty->elements = TL;
133 return add_new_type(Ty);
134}
135
136const TypeInfo* TypeInfo::resolve() const {
137 if (isUnresolved()) {
Reid Spencerf8383de2007-01-06 06:04:32 +0000138 if (getNewTy()[0] == '%' && isdigit(newTy[1])) {
139 unsigned ref = atoi(&((newTy.c_str())[1])); // skip the %
Reid Spencereff838e2007-01-03 23:45:42 +0000140 if (ref < EnumeratedTypes.size()) {
Reid Spencer319a7302007-01-05 17:20:02 +0000141 return EnumeratedTypes[ref];
Reid Spencereff838e2007-01-03 23:45:42 +0000142 } else {
143 std::string msg("Can't resolve numbered type: ");
Reid Spencer319a7302007-01-05 17:20:02 +0000144 msg += getNewTy();
Reid Spencereff838e2007-01-03 23:45:42 +0000145 yyerror(msg.c_str());
146 }
Reid Spencer78720742006-12-02 20:21:22 +0000147 } else {
Reid Spencerf8383de2007-01-06 06:04:32 +0000148 TypeMap::iterator I = NamedTypes.find(newTy);
Reid Spencereff838e2007-01-03 23:45:42 +0000149 if (I != NamedTypes.end()) {
Reid Spencer319a7302007-01-05 17:20:02 +0000150 return I->second;
Reid Spencereff838e2007-01-03 23:45:42 +0000151 } else {
152 std::string msg("Cannot resolve type: ");
Reid Spencer319a7302007-01-05 17:20:02 +0000153 msg += getNewTy();
Reid Spencereff838e2007-01-03 23:45:42 +0000154 yyerror(msg.c_str());
155 }
Reid Spencera50d5962006-12-02 04:11:07 +0000156 }
Reid Spencer280d8012006-12-01 23:40:53 +0000157 }
Reid Spencera50d5962006-12-02 04:11:07 +0000158 // otherwise its already resolved.
Reid Spencer319a7302007-01-05 17:20:02 +0000159 return this;
160}
161
162bool TypeInfo::operator<(const TypeInfo& that) const {
163 if (this == &that)
164 return false;
165 if (oldTy != that.oldTy)
166 return oldTy < that.oldTy;
167 switch (oldTy) {
168 case UpRefTy: {
169 unsigned thisUp = this->getUpRefNum();
170 unsigned thatUp = that.getUpRefNum();
171 return thisUp < thatUp;
172 }
173 case PackedTy:
174 case ArrayTy:
175 if (this->nelems != that.nelems)
176 return nelems < that.nelems;
177 case PointerTy: {
178 const TypeInfo* thisTy = this->elemTy;
179 const TypeInfo* thatTy = that.elemTy;
180 return *thisTy < *thatTy;
181 }
182 case FunctionTy: {
183 const TypeInfo* thisTy = this->resultTy;
184 const TypeInfo* thatTy = that.resultTy;
185 if (!thisTy->sameOldTyAs(thatTy))
186 return *thisTy < *thatTy;
187 /* FALL THROUGH */
188 }
189 case StructTy:
190 case PackedStructTy: {
191 if (elements->size() != that.elements->size())
192 return elements->size() < that.elements->size();
193 for (unsigned i = 0; i < elements->size(); i++) {
194 const TypeInfo* thisTy = (*this->elements)[i];
195 const TypeInfo* thatTy = (*that.elements)[i];
196 if (!thisTy->sameOldTyAs(thatTy))
197 return *thisTy < *thatTy;
198 }
199 break;
200 }
201 case UnresolvedTy:
202 return this->newTy < that.newTy;
203 default:
204 break;
205 }
206 return false;
207}
208
209bool TypeInfo::sameOldTyAs(const TypeInfo* that) const {
210 if (that == 0)
211 return false;
212 if ( this == that )
213 return true;
214 if (oldTy != that->oldTy)
215 return false;
216 switch (oldTy) {
217 case PackedTy:
218 case ArrayTy:
219 if (nelems != that->nelems)
220 return false;
221 /* FALL THROUGH */
222 case PointerTy: {
223 const TypeInfo* thisTy = this->elemTy;
224 const TypeInfo* thatTy = that->elemTy;
225 return thisTy->sameOldTyAs(thatTy);
226 }
227 case FunctionTy: {
228 const TypeInfo* thisTy = this->resultTy;
229 const TypeInfo* thatTy = that->resultTy;
230 if (!thisTy->sameOldTyAs(thatTy))
231 return false;
232 /* FALL THROUGH */
233 }
234 case StructTy:
235 case PackedStructTy: {
236 if (elements->size() != that->elements->size())
237 return false;
238 for (unsigned i = 0; i < elements->size(); i++) {
239 const TypeInfo* thisTy = (*this->elements)[i];
240 const TypeInfo* thatTy = (*that->elements)[i];
241 if (!thisTy->sameOldTyAs(thatTy))
242 return false;
243 }
244 return true;
245 }
246 case UnresolvedTy:
247 return this->newTy == that->newTy;
248 default:
249 return true; // for all others oldTy == that->oldTy is sufficient
250 }
251 return true;
252}
253
254bool TypeInfo::isUnresolvedDeep() const {
255 switch (oldTy) {
256 case UnresolvedTy:
257 return true;
258 case PackedTy:
259 case ArrayTy:
260 case PointerTy:
261 return elemTy->isUnresolvedDeep();
262 case PackedStructTy:
263 case StructTy:
264 for (unsigned i = 0; i < elements->size(); i++)
265 if ((*elements)[i]->isUnresolvedDeep())
266 return true;
267 return false;
268 default:
269 return false;
270 }
271}
272
273unsigned TypeInfo::getBitWidth() const {
274 switch (oldTy) {
275 default:
276 case LabelTy:
277 case VoidTy : return 0;
278 case BoolTy : return 1;
279 case SByteTy: case UByteTy : return 8;
280 case ShortTy: case UShortTy : return 16;
281 case IntTy: case UIntTy: case FloatTy: return 32;
282 case LongTy: case ULongTy: case DoubleTy : return 64;
283 case PointerTy: return SizeOfPointer; // global var
284 case PackedTy:
285 case ArrayTy:
286 return nelems * elemTy->getBitWidth();
287 case StructTy:
288 case PackedStructTy: {
289 uint64_t size = 0;
290 for (unsigned i = 0; i < elements->size(); i++) {
291 size += (*elements)[i]->getBitWidth();
292 }
293 return size;
294 }
295 }
296}
297
298const TypeInfo* TypeInfo::getIndexedType(const ValueInfo& VI) const {
299 if (isStruct()) {
300 if (VI.isConstant() && VI.type->isInteger()) {
301 size_t pos = VI.val->find(' ') + 1;
302 if (pos < VI.val->size()) {
303 uint64_t idx = atoi(VI.val->substr(pos).c_str());
304 return (*elements)[idx];
305 } else {
306 yyerror("Invalid value for constant integer");
307 return 0;
308 }
309 } else {
310 yyerror("Structure requires constant index");
311 return 0;
312 }
313 }
314 if (isArray() || isPacked() || isPointer())
315 return elemTy;
316 yyerror("Invalid type for getIndexedType");
317 return 0;
318}
319
Reid Spencerf8383de2007-01-06 06:04:32 +0000320void TypeInfo::getSignedness(unsigned &sNum, unsigned &uNum,
321 UpRefStack& stack) const {
322 switch (oldTy) {
323 default:
324 case OpaqueTy: case LabelTy: case VoidTy: case BoolTy:
325 case FloatTy : case DoubleTy: case UpRefTy:
326 return;
327 case SByteTy: case ShortTy: case LongTy: case IntTy:
328 sNum++;
329 return;
330 case UByteTy: case UShortTy: case UIntTy: case ULongTy:
331 uNum++;
332 return;
333 case PointerTy:
334 case PackedTy:
335 case ArrayTy:
336 stack.push_back(this);
337 elemTy->getSignedness(sNum, uNum, stack);
338 return;
339 case StructTy:
340 case PackedStructTy: {
341 stack.push_back(this);
342 for (unsigned i = 0; i < elements->size(); i++) {
343 (*elements)[i]->getSignedness(sNum, uNum, stack);
344 }
345 return;
346 }
347 case UnresolvedTy: {
348 const TypeInfo* Ty = this->resolve();
349 // Let's not recurse.
350 UpRefStack::const_iterator I = stack.begin(), E = stack.end();
351 for ( ; I != E && *I != Ty; ++I)
352 ;
353 if (I == E)
354 Ty->getSignedness(sNum, uNum, stack);
355 return;
356 }
357 }
358}
359
360std::string AddSuffix(const std::string& Name, const std::string& Suffix) {
361 if (Name[Name.size()-1] == '"') {
362 std::string Result = Name;
363 Result.insert(Result.size()-1, Suffix);
364 return Result;
365 }
366 return Name + Suffix;
367}
368
369std::string TypeInfo::makeUniqueName(const std::string& BaseName) const {
370 if (BaseName == "\"alloca point\"")
371 return BaseName;
372 switch (oldTy) {
373 default:
374 break;
375 case OpaqueTy: case LabelTy: case VoidTy: case BoolTy: case UpRefTy:
376 case FloatTy : case DoubleTy: case UnresolvedTy:
377 return BaseName;
378 case SByteTy: case ShortTy: case LongTy: case IntTy:
379 return AddSuffix(BaseName, ".s");
380 case UByteTy: case UShortTy: case UIntTy: case ULongTy:
381 return AddSuffix(BaseName, ".u");
382 }
383
384 unsigned uNum = 0, sNum = 0;
385 std::string Suffix;
386 switch (oldTy) {
387 case PointerTy:
388 case PackedTy:
389 case ArrayTy: {
390 TypeInfo::UpRefStack stack;
391 elemTy->resolve()->getSignedness(sNum, uNum, stack);
392 break;
393 }
394 case StructTy:
395 case PackedStructTy: {
396 for (unsigned i = 0; i < elements->size(); i++) {
397 TypeInfo::UpRefStack stack;
398 (*elements)[i]->resolve()->getSignedness(sNum, uNum, stack);
399 }
400 break;
401 }
402 default:
403 assert(0 && "Invalid Type");
404 break;
405 }
406
407 if (sNum == 0 && uNum == 0)
408 return BaseName;
409
410 switch (oldTy) {
411 default: Suffix += ".nada"; break;
412 case PointerTy: Suffix += ".pntr"; break;
413 case PackedTy: Suffix += ".pckd"; break;
414 case ArrayTy: Suffix += ".arry"; break;
415 case StructTy: Suffix += ".strc"; break;
416 case PackedStructTy: Suffix += ".pstr"; break;
417 }
418
419 Suffix += ".s" + llvm::utostr(sNum);
420 Suffix += ".u" + llvm::utostr(uNum);
421 return AddSuffix(BaseName, Suffix);
422}
423
Reid Spencer319a7302007-01-05 17:20:02 +0000424TypeInfo& TypeInfo::operator=(const TypeInfo& that) {
425 oldTy = that.oldTy;
426 nelems = that.nelems;
427 newTy = that.newTy;
428 elemTy = that.elemTy;
429 resultTy = that.resultTy;
430 if (that.elements) {
431 elements = new TypeList(that.elements->size());
432 *elements = *that.elements;
433 } else {
434 elements = 0;
435 }
436 return *this;
437}
438
439const TypeInfo* TypeInfo::add_new_type(TypeInfo* newTy) {
440 TypeRegMap::iterator I = registry.find(newTy);
441 if (I != registry.end()) {
442 delete newTy;
443 return *I;
444 }
445 registry.insert(newTy);
446 return newTy;
Reid Spencer280d8012006-12-01 23:40:53 +0000447}
448
Reid Spencera50d5962006-12-02 04:11:07 +0000449static const char* getCastOpcode(
Reid Spencer52402b02007-01-02 05:45:11 +0000450 std::string& Source, const TypeInfo* SrcTy, const TypeInfo* DstTy)
Reid Spencera50d5962006-12-02 04:11:07 +0000451{
Reid Spencer52402b02007-01-02 05:45:11 +0000452 unsigned SrcBits = SrcTy->getBitWidth();
453 unsigned DstBits = DstTy->getBitWidth();
Reid Spencere77e35e2006-12-01 20:26:20 +0000454 const char* opcode = "bitcast";
455 // Run through the possibilities ...
Reid Spencer52402b02007-01-02 05:45:11 +0000456 if (DstTy->isIntegral()) { // Casting to integral
457 if (SrcTy->isIntegral()) { // Casting from integral
Reid Spencere77e35e2006-12-01 20:26:20 +0000458 if (DstBits < SrcBits)
459 opcode = "trunc";
460 else if (DstBits > SrcBits) { // its an extension
Reid Spencer52402b02007-01-02 05:45:11 +0000461 if (SrcTy->isSigned())
Reid Spencere77e35e2006-12-01 20:26:20 +0000462 opcode ="sext"; // signed -> SEXT
463 else
464 opcode = "zext"; // unsigned -> ZEXT
465 } else {
466 opcode = "bitcast"; // Same size, No-op cast
467 }
Reid Spencer52402b02007-01-02 05:45:11 +0000468 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
469 if (DstTy->isSigned())
Reid Spencere77e35e2006-12-01 20:26:20 +0000470 opcode = "fptosi"; // FP -> sint
471 else
472 opcode = "fptoui"; // FP -> uint
Reid Spencer52402b02007-01-02 05:45:11 +0000473 } else if (SrcTy->isPacked()) {
474 assert(DstBits == SrcTy->getBitWidth() &&
Reid Spencere77e35e2006-12-01 20:26:20 +0000475 "Casting packed to integer of different width");
476 opcode = "bitcast"; // same size, no-op cast
477 } else {
Reid Spencer52402b02007-01-02 05:45:11 +0000478 assert(SrcTy->isPointer() &&
Reid Spencere77e35e2006-12-01 20:26:20 +0000479 "Casting from a value that is not first-class type");
480 opcode = "ptrtoint"; // ptr -> int
481 }
Reid Spencer52402b02007-01-02 05:45:11 +0000482 } else if (DstTy->isFloatingPoint()) { // Casting to floating pt
483 if (SrcTy->isIntegral()) { // Casting from integral
484 if (SrcTy->isSigned())
Reid Spencere77e35e2006-12-01 20:26:20 +0000485 opcode = "sitofp"; // sint -> FP
486 else
487 opcode = "uitofp"; // uint -> FP
Reid Spencer52402b02007-01-02 05:45:11 +0000488 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencere77e35e2006-12-01 20:26:20 +0000489 if (DstBits < SrcBits) {
490 opcode = "fptrunc"; // FP -> smaller FP
491 } else if (DstBits > SrcBits) {
492 opcode = "fpext"; // FP -> larger FP
493 } else {
494 opcode ="bitcast"; // same size, no-op cast
495 }
Reid Spencer52402b02007-01-02 05:45:11 +0000496 } else if (SrcTy->isPacked()) {
497 assert(DstBits == SrcTy->getBitWidth() &&
Reid Spencere77e35e2006-12-01 20:26:20 +0000498 "Casting packed to floating point of different width");
499 opcode = "bitcast"; // same size, no-op cast
500 } else {
501 assert(0 && "Casting pointer or non-first class to float");
502 }
Reid Spencer52402b02007-01-02 05:45:11 +0000503 } else if (DstTy->isPacked()) {
504 if (SrcTy->isPacked()) {
505 assert(DstTy->getBitWidth() == SrcTy->getBitWidth() &&
Reid Spencere77e35e2006-12-01 20:26:20 +0000506 "Casting packed to packed of different widths");
507 opcode = "bitcast"; // packed -> packed
Reid Spencer52402b02007-01-02 05:45:11 +0000508 } else if (DstTy->getBitWidth() == SrcBits) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000509 opcode = "bitcast"; // float/int -> packed
510 } else {
511 assert(!"Illegal cast to packed (wrong type or size)");
512 }
Reid Spencer52402b02007-01-02 05:45:11 +0000513 } else if (DstTy->isPointer()) {
514 if (SrcTy->isPointer()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000515 opcode = "bitcast"; // ptr -> ptr
Reid Spencer52402b02007-01-02 05:45:11 +0000516 } else if (SrcTy->isIntegral()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000517 opcode = "inttoptr"; // int -> ptr
518 } else {
Reid Spencera50d5962006-12-02 04:11:07 +0000519 assert(!"Casting invalid type to pointer");
Reid Spencere77e35e2006-12-01 20:26:20 +0000520 }
521 } else {
522 assert(!"Casting to type that is not first-class");
523 }
524 return opcode;
525}
526
Reid Spencer319a7302007-01-05 17:20:02 +0000527static std::string getCastUpgrade(const std::string& Src, const TypeInfo* SrcTy,
528 const TypeInfo* DstTy, bool isConst)
Reid Spencera50d5962006-12-02 04:11:07 +0000529{
530 std::string Result;
531 std::string Source = Src;
Reid Spencer52402b02007-01-02 05:45:11 +0000532 if (SrcTy->isFloatingPoint() && DstTy->isPointer()) {
Reid Spencera50d5962006-12-02 04:11:07 +0000533 // fp -> ptr cast is no longer supported but we must upgrade this
534 // by doing a double cast: fp -> int -> ptr
535 if (isConst)
Reid Spencer71d2ec92006-12-31 06:02:26 +0000536 Source = "i64 fptoui(" + Source + " to i64)";
Reid Spencera50d5962006-12-02 04:11:07 +0000537 else {
Reid Spencerf459d392006-12-02 16:19:52 +0000538 *O << " %cast_upgrade" << unique << " = fptoui " << Source
Reid Spencer71d2ec92006-12-31 06:02:26 +0000539 << " to i64\n";
540 Source = "i64 %cast_upgrade" + llvm::utostr(unique);
Reid Spencera50d5962006-12-02 04:11:07 +0000541 }
542 // Update the SrcTy for the getCastOpcode call below
Reid Spencer319a7302007-01-05 17:20:02 +0000543 SrcTy = TypeInfo::get("i64", ULongTy);
Reid Spencer52402b02007-01-02 05:45:11 +0000544 } else if (DstTy->isBool()) {
545 // cast type %x to bool was previously defined as setne type %x, null
546 // The cast semantic is now to truncate, not compare so we must retain
547 // the original intent by replacing the cast with a setne
548 const char* comparator = SrcTy->isPointer() ? ", null" :
549 (SrcTy->isFloatingPoint() ? ", 0.0" :
550 (SrcTy->isBool() ? ", false" : ", 0"));
551 const char* compareOp = SrcTy->isFloatingPoint() ? "fcmp one " : "icmp ne ";
Reid Spencer187ccf82006-12-09 16:57:22 +0000552 if (isConst) {
553 Result = "(" + Source + comparator + ")";
554 Result = compareOp + Result;
555 } else
556 Result = compareOp + Source + comparator;
Reid Spencera50d5962006-12-02 04:11:07 +0000557 return Result; // skip cast processing below
558 }
Reid Spencer319a7302007-01-05 17:20:02 +0000559 SrcTy = SrcTy->resolve();
560 DstTy = DstTy->resolve();
Reid Spencera50d5962006-12-02 04:11:07 +0000561 std::string Opcode(getCastOpcode(Source, SrcTy, DstTy));
562 if (isConst)
Reid Spencer52402b02007-01-02 05:45:11 +0000563 Result += Opcode + "( " + Source + " to " + DstTy->getNewTy() + ")";
Reid Spencera50d5962006-12-02 04:11:07 +0000564 else
Reid Spencer52402b02007-01-02 05:45:11 +0000565 Result += Opcode + " " + Source + " to " + DstTy->getNewTy();
Reid Spencera50d5962006-12-02 04:11:07 +0000566 return Result;
567}
568
Reid Spencer319a7302007-01-05 17:20:02 +0000569const char* getDivRemOpcode(const std::string& opcode, const TypeInfo* TI) {
Reid Spencer78720742006-12-02 20:21:22 +0000570 const char* op = opcode.c_str();
Reid Spencer319a7302007-01-05 17:20:02 +0000571 const TypeInfo* Ty = TI->resolve();
Reid Spencer52402b02007-01-02 05:45:11 +0000572 if (Ty->isPacked())
573 Ty = Ty->getElementType();
Reid Spencer78720742006-12-02 20:21:22 +0000574 if (opcode == "div")
Reid Spencer52402b02007-01-02 05:45:11 +0000575 if (Ty->isFloatingPoint())
Reid Spencer78720742006-12-02 20:21:22 +0000576 op = "fdiv";
Reid Spencer52402b02007-01-02 05:45:11 +0000577 else if (Ty->isUnsigned())
Reid Spencer78720742006-12-02 20:21:22 +0000578 op = "udiv";
Reid Spencer52402b02007-01-02 05:45:11 +0000579 else if (Ty->isSigned())
Reid Spencer78720742006-12-02 20:21:22 +0000580 op = "sdiv";
581 else
582 yyerror("Invalid type for div instruction");
583 else if (opcode == "rem")
Reid Spencer52402b02007-01-02 05:45:11 +0000584 if (Ty->isFloatingPoint())
Reid Spencer78720742006-12-02 20:21:22 +0000585 op = "frem";
Reid Spencer52402b02007-01-02 05:45:11 +0000586 else if (Ty->isUnsigned())
Reid Spencer78720742006-12-02 20:21:22 +0000587 op = "urem";
Reid Spencer52402b02007-01-02 05:45:11 +0000588 else if (Ty->isSigned())
Reid Spencer78720742006-12-02 20:21:22 +0000589 op = "srem";
590 else
591 yyerror("Invalid type for rem instruction");
592 return op;
593}
Reid Spencer229e9362006-12-02 22:14:11 +0000594
595std::string
Reid Spencer52402b02007-01-02 05:45:11 +0000596getCompareOp(const std::string& setcc, const TypeInfo* TI) {
Reid Spencer229e9362006-12-02 22:14:11 +0000597 assert(setcc.length() == 5);
598 char cc1 = setcc[3];
599 char cc2 = setcc[4];
600 assert(cc1 == 'e' || cc1 == 'n' || cc1 == 'l' || cc1 == 'g');
601 assert(cc2 == 'q' || cc2 == 'e' || cc2 == 'e' || cc2 == 't');
602 std::string result("xcmp xxx");
603 result[6] = cc1;
604 result[7] = cc2;
Reid Spencer52402b02007-01-02 05:45:11 +0000605 if (TI->isFloatingPoint()) {
Reid Spencer229e9362006-12-02 22:14:11 +0000606 result[0] = 'f';
Reid Spencere4d87aa2006-12-23 06:05:41 +0000607 result[5] = 'o';
Reid Spencerf0cf1322006-12-07 04:23:03 +0000608 if (cc1 == 'n')
609 result[5] = 'u'; // NE maps to unordered
610 else
611 result[5] = 'o'; // everything else maps to ordered
Reid Spencer52402b02007-01-02 05:45:11 +0000612 } else if (TI->isIntegral() || TI->isPointer()) {
Reid Spencer229e9362006-12-02 22:14:11 +0000613 result[0] = 'i';
614 if ((cc1 == 'e' && cc2 == 'q') || (cc1 == 'n' && cc2 == 'e'))
615 result.erase(5,1);
Reid Spencer52402b02007-01-02 05:45:11 +0000616 else if (TI->isSigned())
Reid Spencer229e9362006-12-02 22:14:11 +0000617 result[5] = 's';
Reid Spencer52402b02007-01-02 05:45:11 +0000618 else if (TI->isUnsigned() || TI->isPointer() || TI->isBool())
Reid Spencer229e9362006-12-02 22:14:11 +0000619 result[5] = 'u';
620 else
621 yyerror("Invalid integral type for setcc");
622 }
623 return result;
624}
625
Reid Spencer319a7302007-01-05 17:20:02 +0000626static const TypeInfo* getFunctionReturnType(const TypeInfo* PFTy) {
627 PFTy = PFTy->resolve();
Reid Spencer52402b02007-01-02 05:45:11 +0000628 if (PFTy->isPointer()) {
Reid Spencer319a7302007-01-05 17:20:02 +0000629 const TypeInfo* ElemTy = PFTy->getElementType();
630 ElemTy = ElemTy->resolve();
Reid Spencer52402b02007-01-02 05:45:11 +0000631 if (ElemTy->isFunction())
Reid Spencer319a7302007-01-05 17:20:02 +0000632 return ElemTy->getResultType();
Reid Spencer52402b02007-01-02 05:45:11 +0000633 } else if (PFTy->isFunction()) {
Reid Spencer319a7302007-01-05 17:20:02 +0000634 return PFTy->getResultType();
Reid Spencer52402b02007-01-02 05:45:11 +0000635 }
Reid Spencer319a7302007-01-05 17:20:02 +0000636 return PFTy;
Reid Spencer52402b02007-01-02 05:45:11 +0000637}
638
Reid Spencer319a7302007-01-05 17:20:02 +0000639static const TypeInfo* ResolveUpReference(const TypeInfo* Ty,
Reid Spencerf8383de2007-01-06 06:04:32 +0000640 TypeInfo::UpRefStack* stack) {
Reid Spencereff838e2007-01-03 23:45:42 +0000641 assert(Ty->isUpReference() && "Can't resolve a non-upreference");
Reid Spencer319a7302007-01-05 17:20:02 +0000642 unsigned upref = Ty->getUpRefNum();
Reid Spencereff838e2007-01-03 23:45:42 +0000643 assert(upref < stack->size() && "Invalid up reference");
644 return (*stack)[upref - stack->size() - 1];
645}
646
Reid Spencer319a7302007-01-05 17:20:02 +0000647static const TypeInfo* getGEPIndexedType(const TypeInfo* PTy, ValueList* idxs) {
648 const TypeInfo* Result = PTy = PTy->resolve();
Reid Spencer52402b02007-01-02 05:45:11 +0000649 assert(PTy->isPointer() && "GEP Operand is not a pointer?");
Reid Spencerf8383de2007-01-06 06:04:32 +0000650 TypeInfo::UpRefStack stack;
Reid Spencereff838e2007-01-03 23:45:42 +0000651 for (unsigned i = 0; i < idxs->size(); ++i) {
Reid Spencer52402b02007-01-02 05:45:11 +0000652 if (Result->isComposite()) {
653 Result = Result->getIndexedType((*idxs)[i]);
Reid Spencer319a7302007-01-05 17:20:02 +0000654 Result = Result->resolve();
Reid Spencereff838e2007-01-03 23:45:42 +0000655 stack.push_back(Result);
Reid Spencer52402b02007-01-02 05:45:11 +0000656 } else
657 yyerror("Invalid type for index");
658 }
Reid Spencereff838e2007-01-03 23:45:42 +0000659 // Resolve upreferences so we can return a more natural type
660 if (Result->isPointer()) {
661 if (Result->getElementType()->isUpReference()) {
662 stack.push_back(Result);
663 Result = ResolveUpReference(Result->getElementType(), &stack);
664 }
665 } else if (Result->isUpReference()) {
666 Result = ResolveUpReference(Result->getElementType(), &stack);
667 }
Reid Spencer52402b02007-01-02 05:45:11 +0000668 return Result->getPointerType();
669}
670
Reid Spencer52402b02007-01-02 05:45:11 +0000671
672// This function handles appending .u or .s to integer value names that
673// were previously unsigned or signed, respectively. This avoids name
674// collisions since the unsigned and signed type planes have collapsed
675// into a single signless type plane.
Reid Spencer319a7302007-01-05 17:20:02 +0000676static std::string getUniqueName(const std::string *Name, const TypeInfo* Ty,
Reid Spencerf8383de2007-01-06 06:04:32 +0000677 bool isGlobal = false, bool isDef = false) {
Reid Spencer319a7302007-01-05 17:20:02 +0000678
Reid Spencer52402b02007-01-02 05:45:11 +0000679 // If its not a symbolic name, don't modify it, probably a constant val.
680 if ((*Name)[0] != '%' && (*Name)[0] != '"')
681 return *Name;
Reid Spencer319a7302007-01-05 17:20:02 +0000682
Reid Spencer52402b02007-01-02 05:45:11 +0000683 // If its a numeric reference, just leave it alone.
684 if (isdigit((*Name)[1]))
685 return *Name;
686
687 // Resolve the type
Reid Spencer319a7302007-01-05 17:20:02 +0000688 Ty = Ty->resolve();
689
690 // If its a global name, get its uniquified name, if any
691 GlobalsTypeMap::iterator GI = Globals.find(*Name);
692 if (GI != Globals.end()) {
693 TypePlaneMap::iterator TPI = GI->second.begin();
694 TypePlaneMap::iterator TPE = GI->second.end();
695 for ( ; TPI != TPE ; ++TPI) {
696 if (TPI->first->sameNewTyAs(Ty))
697 return TPI->second;
698 }
699 }
700
701 if (isGlobal) {
702 // We didn't find a global name, but if its supposed to be global then all
703 // we can do is return the name. This is probably a forward reference of a
704 // global value that hasn't been defined yet. Since we have no definition
705 // we don't know its linkage class. Just assume its an external and the name
706 // shouldn't change.
707 return *Name;
708 }
Reid Spencer52402b02007-01-02 05:45:11 +0000709
710 // Default the result to the current name
Reid Spencerf8383de2007-01-06 06:04:32 +0000711 std::string Result = Ty->makeUniqueName(*Name);
Reid Spencer52402b02007-01-02 05:45:11 +0000712
Reid Spencer52402b02007-01-02 05:45:11 +0000713 return Result;
714}
715
Reid Spencer319a7302007-01-05 17:20:02 +0000716static unsigned UniqueNameCounter = 0;
717
718std::string getGlobalName(const std::string* Name, const std::string Linkage,
719 const TypeInfo* Ty, bool isConstant) {
720 // Default to given name
721 std::string Result = *Name;
722 // Look up the name in the Globals Map
723 GlobalsTypeMap::iterator GI = Globals.find(*Name);
724 // Did we see this global name before?
725 if (GI != Globals.end()) {
726 if (Ty->isUnresolvedDeep()) {
727 // The Gval's type is unresolved. Consequently, we can't disambiguate it
728 // by type. We'll just change its name and emit a warning.
729 warning("Cannot disambiguate global value '" + *Name +
730 "' because type '" + Ty->getNewTy() + "'is unresolved.\n");
731 Result = *Name + ".unique";
732 UniqueNameCounter++;
733 Result += llvm::utostr(UniqueNameCounter);
734 return Result;
735 } else {
736 TypePlaneMap::iterator TPI = GI->second.find(Ty);
737 if (TPI != GI->second.end()) {
738 // We found an existing name of the same old type. This isn't allowed
739 // in LLVM 2.0. Consequently, we must alter the name of the global so it
740 // can at least compile. References to the global will yield the first
741 // definition, which is okay. We also must warn about this.
742 Result = *Name + ".unique";
743 UniqueNameCounter++;
744 Result += llvm::utostr(UniqueNameCounter);
745 warning(std::string("Global variable '") + *Name + "' was renamed to '"+
746 Result + "'");
747 } else {
748 // There isn't an existing definition for this name according to the
749 // old types. Now search the TypePlanMap for types with the same new
750 // name.
751 TypePlaneMap::iterator TPI = GI->second.begin();
752 TypePlaneMap::iterator TPE = GI->second.end();
753 for ( ; TPI != TPE; ++TPI) {
754 if (TPI->first->sameNewTyAs(Ty)) {
755 // The new types are the same but the old types are different so
756 // this is a global name collision resulting from type planes
757 // collapsing.
758 if (Linkage == "external" || Linkage == "dllimport" ||
759 Linkage == "extern_weak" || Linkage == "") {
760 // The linkage of this gval is external so we can't reliably
761 // rename it because it could potentially create a linking
762 // problem. However, we can't leave the name conflict in the
763 // output either or it won't assemble with LLVM 2.0. So, all we
764 // can do is rename this one to something unique and emit a
765 // warning about the problem.
766 Result = *Name + ".unique";
767 UniqueNameCounter++;
768 Result += llvm::utostr(UniqueNameCounter);
769 warning("Renaming global value '" + *Name + "' to '" + Result +
770 "' may cause linkage errors.");
771 return Result;
772 } else {
773 // Its linkage is internal and its type is known so we can
774 // disambiguate the name collision successfully based on the type.
775 Result = getUniqueName(Name, Ty);
776 TPI->second = Result;
777 return Result;
778 }
779 }
780 }
781 // We didn't find an entry in the type plane with the same new type and
782 // the old types differ so this is a new type plane for this global
783 // variable. We just fall through to the logic below which inserts
784 // the global.
785 }
786 }
787 }
788
789 // Its a new global name, if it is external we can't change it
790 if (isConstant || Linkage == "external" || Linkage == "dllimport" ||
791 Linkage == "extern_weak" || Linkage == "") {
792 Globals[Result][Ty] = Result;
793 return Result;
794 }
795
796 // Its a new global name, and it is internal, change the name to make it
797 // unique for its type.
798 // Result = getUniqueName(Name, Ty);
799 Globals[*Name][Ty] = Result;
800 return Result;
801}
Reid Spencere7c3c602006-11-30 06:36:44 +0000802%}
803
Reid Spencerf0cf1322006-12-07 04:23:03 +0000804// %file-prefix="UpgradeParser"
Reid Spencere77e35e2006-12-01 20:26:20 +0000805
806%union {
807 std::string* String;
Reid Spencer319a7302007-01-05 17:20:02 +0000808 const TypeInfo* Type;
Reid Spencere77e35e2006-12-01 20:26:20 +0000809 ValueInfo Value;
810 ConstInfo Const;
Reid Spencerf8483652006-12-02 15:16:01 +0000811 ValueList* ValList;
Reid Spencer52402b02007-01-02 05:45:11 +0000812 TypeList* TypeVec;
Reid Spencere77e35e2006-12-01 20:26:20 +0000813}
814
Reid Spencerf2d55322006-12-01 21:52:30 +0000815%token <Type> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
Reid Spencera50d5962006-12-02 04:11:07 +0000816%token <Type> FLOAT DOUBLE LABEL
817%token <String> OPAQUE ESINT64VAL EUINT64VAL SINTVAL UINTVAL FPVAL
Reid Spencerf2d55322006-12-01 21:52:30 +0000818%token <String> NULL_TOK UNDEF ZEROINITIALIZER TRUETOK FALSETOK
Reid Spencere77e35e2006-12-01 20:26:20 +0000819%token <String> TYPE VAR_ID LABELSTR STRINGCONSTANT
820%token <String> IMPLEMENTATION BEGINTOK ENDTOK
Reid Spencer71d2ec92006-12-31 06:02:26 +0000821%token <String> DECLARE GLOBAL CONSTANT SECTION VOLATILE
Reid Spencere77e35e2006-12-01 20:26:20 +0000822%token <String> TO DOTDOTDOT CONST INTERNAL LINKONCE WEAK
823%token <String> DLLIMPORT DLLEXPORT EXTERN_WEAK APPENDING
Reid Spencerc4d96252007-01-13 00:03:30 +0000824%token <String> EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
Reid Spencer78720742006-12-02 20:21:22 +0000825%token <String> ALIGN UNINITIALIZED
Reid Spencere77e35e2006-12-01 20:26:20 +0000826%token <String> DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
827%token <String> CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
828%token <String> X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
829%token <String> DATALAYOUT
Reid Spencer78720742006-12-02 20:21:22 +0000830%token <String> RET BR SWITCH INVOKE EXCEPT UNWIND UNREACHABLE
831%token <String> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM AND OR XOR
Reid Spencere77e35e2006-12-01 20:26:20 +0000832%token <String> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
Reid Spencer229e9362006-12-02 22:14:11 +0000833%token <String> ICMP FCMP EQ NE SLT SGT SLE SGE OEQ ONE OLT OGT OLE OGE
834%token <String> ORD UNO UEQ UNE ULT UGT ULE UGE
Reid Spencere77e35e2006-12-01 20:26:20 +0000835%token <String> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Reid Spencerf7bde222006-12-01 22:26:37 +0000836%token <String> PHI_TOK SELECT SHL SHR ASHR LSHR VAARG
Reid Spencere77e35e2006-12-01 20:26:20 +0000837%token <String> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Reid Spencerfcb5df82006-12-01 22:34:43 +0000838%token <String> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI UITOFP SITOFP
839%token <String> PTRTOINT INTTOPTR BITCAST
Reid Spencere77e35e2006-12-01 20:26:20 +0000840
841%type <String> OptAssign OptLinkage OptCallingConv OptAlign OptCAlign
842%type <String> SectionString OptSection GlobalVarAttributes GlobalVarAttribute
Reid Spencer52402b02007-01-02 05:45:11 +0000843%type <String> ConstExpr DefinitionList
Reid Spencere77e35e2006-12-01 20:26:20 +0000844%type <String> ConstPool TargetDefinition LibrariesDefinition LibList OptName
845%type <String> ArgVal ArgListH ArgList FunctionHeaderH BEGIN FunctionHeader END
Reid Spencer52402b02007-01-02 05:45:11 +0000846%type <String> Function FunctionProto BasicBlock
847%type <String> InstructionList BBTerminatorInst JumpTable Inst
848%type <String> OptTailCall OptVolatile Unwind
849%type <String> SymbolicValueRef OptSideEffect GlobalType
Reid Spencere77e35e2006-12-01 20:26:20 +0000850%type <String> FnDeclareLinkage BasicBlockList BigOrLittle AsmBlock
Reid Spencer78720742006-12-02 20:21:22 +0000851%type <String> Name ConstValueRef ConstVector External
Reid Spencer57f28f92006-12-03 07:10:26 +0000852%type <String> ShiftOps SetCondOps LogicalOps ArithmeticOps CastOps
853%type <String> IPredicates FPredicates
Reid Spencere77e35e2006-12-01 20:26:20 +0000854
Reid Spencerf8483652006-12-02 15:16:01 +0000855%type <ValList> ValueRefList ValueRefListE IndexList
Reid Spencer52402b02007-01-02 05:45:11 +0000856%type <TypeVec> TypeListI ArgTypeListI
Reid Spencere77e35e2006-12-01 20:26:20 +0000857
858%type <Type> IntType SIntType UIntType FPType TypesV Types
859%type <Type> PrimType UpRTypesV UpRTypes
860
Reid Spencerf2d55322006-12-01 21:52:30 +0000861%type <String> IntVal EInt64Val
862%type <Const> ConstVal
Reid Spencere77e35e2006-12-01 20:26:20 +0000863
Reid Spencer52402b02007-01-02 05:45:11 +0000864%type <Value> ValueRef ResolvedVal InstVal PHIList MemoryInst
Reid Spencere7c3c602006-11-30 06:36:44 +0000865
866%start Module
867
868%%
869
870// Handle constant integer size restriction and conversion...
Reid Spencerf2d55322006-12-01 21:52:30 +0000871IntVal : SINTVAL | UINTVAL ;
Reid Spencere77e35e2006-12-01 20:26:20 +0000872EInt64Val : ESINT64VAL | EUINT64VAL;
Reid Spencere7c3c602006-11-30 06:36:44 +0000873
874// Operations that are notably excluded from this list include:
875// RET, BR, & SWITCH because they end basic blocks and are treated specially.
Reid Spencer78720742006-12-02 20:21:22 +0000876ArithmeticOps: ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV
877 | REM | UREM | SREM | FREM;
Reid Spencere7c3c602006-11-30 06:36:44 +0000878LogicalOps : AND | OR | XOR;
879SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Reid Spencer57f28f92006-12-03 07:10:26 +0000880IPredicates : EQ | NE | SLT | SGT | SLE | SGE | ULT | UGT | ULE | UGE;
881FPredicates : OEQ | ONE | OLT | OGT | OLE | OGE | ORD | UNO | UEQ | UNE
882 | ULT | UGT | ULE | UGE | TRUETOK | FALSETOK;
Reid Spencerf7bde222006-12-01 22:26:37 +0000883ShiftOps : SHL | SHR | ASHR | LSHR;
Reid Spencerfcb5df82006-12-01 22:34:43 +0000884CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI |
885 UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
886 ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000887
888// These are some types that allow classification if we only want a particular
889// thing... for example, only a signed, unsigned, or integral type.
890SIntType : LONG | INT | SHORT | SBYTE;
891UIntType : ULONG | UINT | USHORT | UBYTE;
892IntType : SIntType | UIntType;
893FPType : FLOAT | DOUBLE;
894
895// OptAssign - Value producing statements have an optional assignment component
896OptAssign : Name '=' {
Reid Spencere7c3c602006-11-30 06:36:44 +0000897 $$ = $1;
898 }
899 | /*empty*/ {
900 $$ = new std::string("");
901 };
902
903OptLinkage
904 : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT
905 | EXTERN_WEAK
906 | /*empty*/ { $$ = new std::string(""); } ;
907
908OptCallingConv
909 : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK
Reid Spencer16244f42006-12-01 21:10:07 +0000910 | X86_FASTCALLCC_TOK
911 | CC_TOK EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000912 *$1 += *$2;
913 delete $2;
Reid Spencer16244f42006-12-01 21:10:07 +0000914 $$ = $1;
915 }
Reid Spencere7c3c602006-11-30 06:36:44 +0000916 | /*empty*/ { $$ = new std::string(""); } ;
917
918// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
919// a comma before it.
920OptAlign
921 : /*empty*/ { $$ = new std::string(); }
Reid Spencerf2d55322006-12-01 21:52:30 +0000922 | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
Reid Spencerf0cf1322006-12-07 04:23:03 +0000923
Reid Spencere7c3c602006-11-30 06:36:44 +0000924OptCAlign
925 : /*empty*/ { $$ = new std::string(); }
926 | ',' ALIGN EUINT64VAL {
927 $2->insert(0, ", ");
Reid Spencerf2d55322006-12-01 21:52:30 +0000928 *$2 += " " + *$3;
929 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000930 $$ = $2;
931 };
932
933SectionString
934 : SECTION STRINGCONSTANT {
935 *$1 += " " + *$2;
936 delete $2;
937 $$ = $1;
938 };
939
940OptSection : /*empty*/ { $$ = new std::string(); }
941 | SectionString;
942
943GlobalVarAttributes
944 : /* empty */ { $$ = new std::string(); }
945 | ',' GlobalVarAttribute GlobalVarAttributes {
946 $2->insert(0, ", ");
947 if (!$3->empty())
948 *$2 += " " + *$3;
949 delete $3;
950 $$ = $2;
951 };
952
953GlobalVarAttribute
954 : SectionString
955 | ALIGN EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000956 *$1 += " " + *$2;
957 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000958 $$ = $1;
959 };
960
961//===----------------------------------------------------------------------===//
962// Types includes all predefined types... except void, because it can only be
963// used in specific contexts (function returning void for example). To have
964// access to it, a user must explicitly use TypesV.
965//
966
967// TypesV includes all of 'Types', but it also includes the void type.
968TypesV : Types | VOID ;
969UpRTypesV : UpRTypes | VOID ;
970Types : UpRTypes ;
971
972// Derived types are added later...
973//
974PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
Reid Spencere77e35e2006-12-01 20:26:20 +0000975PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
Reid Spencera50d5962006-12-02 04:11:07 +0000976UpRTypes
977 : OPAQUE {
Reid Spencer319a7302007-01-05 17:20:02 +0000978 $$ = TypeInfo::get(*$1, OpaqueTy);
Reid Spencera50d5962006-12-02 04:11:07 +0000979 }
980 | SymbolicValueRef {
Reid Spencer319a7302007-01-05 17:20:02 +0000981 $$ = TypeInfo::get(*$1, UnresolvedTy);
Reid Spencera50d5962006-12-02 04:11:07 +0000982 }
Reid Spencer78720742006-12-02 20:21:22 +0000983 | PrimType {
984 $$ = $1;
985 }
986 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerf2d55322006-12-01 21:52:30 +0000987 $2->insert(0, "\\");
Reid Spencer319a7302007-01-05 17:20:02 +0000988 $$ = TypeInfo::get(*$2, UpRefTy);
Reid Spencere7c3c602006-11-30 06:36:44 +0000989 }
990 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Reid Spencer52402b02007-01-02 05:45:11 +0000991 std::string newTy( $1->getNewTy() + "(");
992 for (unsigned i = 0; i < $3->size(); ++i) {
993 if (i != 0)
994 newTy += ", ";
995 if ((*$3)[i]->isVoid())
996 newTy += "...";
997 else
998 newTy += (*$3)[i]->getNewTy();
999 }
1000 newTy += ")";
Reid Spencer319a7302007-01-05 17:20:02 +00001001 $$ = TypeInfo::get(newTy, $1, $3);
Reid Spencere7c3c602006-11-30 06:36:44 +00001002 }
1003 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencer319a7302007-01-05 17:20:02 +00001004 uint64_t elems = atoi($2->c_str());
Reid Spencerf2d55322006-12-01 21:52:30 +00001005 $2->insert(0,"[ ");
Reid Spencer52402b02007-01-02 05:45:11 +00001006 *$2 += " x " + $4->getNewTy() + " ]";
Reid Spencer319a7302007-01-05 17:20:02 +00001007 $$ = TypeInfo::get(*$2, ArrayTy, $4, elems);
Reid Spencere7c3c602006-11-30 06:36:44 +00001008 }
1009 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
Reid Spencer319a7302007-01-05 17:20:02 +00001010 uint64_t elems = atoi($2->c_str());
Reid Spencerf2d55322006-12-01 21:52:30 +00001011 $2->insert(0,"< ");
Reid Spencer52402b02007-01-02 05:45:11 +00001012 *$2 += " x " + $4->getNewTy() + " >";
Reid Spencer319a7302007-01-05 17:20:02 +00001013 $$ = TypeInfo::get(*$2, PackedTy, $4, elems);
Reid Spencere7c3c602006-11-30 06:36:44 +00001014 }
1015 | '{' TypeListI '}' { // Structure type?
Reid Spencer52402b02007-01-02 05:45:11 +00001016 std::string newTy("{");
1017 for (unsigned i = 0; i < $2->size(); ++i) {
1018 if (i != 0)
1019 newTy += ", ";
1020 newTy += (*$2)[i]->getNewTy();
1021 }
1022 newTy += "}";
Reid Spencer319a7302007-01-05 17:20:02 +00001023 $$ = TypeInfo::get(newTy, StructTy, $2);
Reid Spencere7c3c602006-11-30 06:36:44 +00001024 }
1025 | '{' '}' { // Empty structure type?
Reid Spencer319a7302007-01-05 17:20:02 +00001026 $$ = TypeInfo::get("{}", StructTy, new TypeList());
Reid Spencere7c3c602006-11-30 06:36:44 +00001027 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001028 | '<' '{' TypeListI '}' '>' { // Packed Structure type?
Reid Spencer52402b02007-01-02 05:45:11 +00001029 std::string newTy("<{");
1030 for (unsigned i = 0; i < $3->size(); ++i) {
1031 if (i != 0)
1032 newTy += ", ";
1033 newTy += (*$3)[i]->getNewTy();
1034 }
1035 newTy += "}>";
Reid Spencer319a7302007-01-05 17:20:02 +00001036 $$ = TypeInfo::get(newTy, PackedStructTy, $3);
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001037 }
1038 | '<' '{' '}' '>' { // Empty packed structure type?
Reid Spencer319a7302007-01-05 17:20:02 +00001039 $$ = TypeInfo::get("<{}>", PackedStructTy, new TypeList());
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001040 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001041 | UpRTypes '*' { // Pointer type?
Reid Spencer52402b02007-01-02 05:45:11 +00001042 $$ = $1->getPointerType();
Reid Spencere7c3c602006-11-30 06:36:44 +00001043 };
1044
1045// TypeList - Used for struct declarations and as a basis for function type
1046// declaration type lists
1047//
Reid Spencere77e35e2006-12-01 20:26:20 +00001048TypeListI
1049 : UpRTypes {
Reid Spencer52402b02007-01-02 05:45:11 +00001050 $$ = new TypeList();
1051 $$->push_back($1);
Reid Spencere77e35e2006-12-01 20:26:20 +00001052 }
1053 | TypeListI ',' UpRTypes {
Reid Spencere7c3c602006-11-30 06:36:44 +00001054 $$ = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001055 $$->push_back($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00001056 };
1057
1058// ArgTypeList - List of types for a function type declaration...
Reid Spencere77e35e2006-12-01 20:26:20 +00001059ArgTypeListI
1060 : TypeListI
Reid Spencere7c3c602006-11-30 06:36:44 +00001061 | TypeListI ',' DOTDOTDOT {
Reid Spencere7c3c602006-11-30 06:36:44 +00001062 $$ = $1;
Reid Spencer319a7302007-01-05 17:20:02 +00001063 $$->push_back(TypeInfo::get("void",VoidTy));
Reid Spencer52402b02007-01-02 05:45:11 +00001064 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001065 }
1066 | DOTDOTDOT {
Reid Spencer52402b02007-01-02 05:45:11 +00001067 $$ = new TypeList();
Reid Spencer319a7302007-01-05 17:20:02 +00001068 $$->push_back(TypeInfo::get("void",VoidTy));
Reid Spencer52402b02007-01-02 05:45:11 +00001069 delete $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001070 }
1071 | /*empty*/ {
Reid Spencer52402b02007-01-02 05:45:11 +00001072 $$ = new TypeList();
Reid Spencere7c3c602006-11-30 06:36:44 +00001073 };
1074
1075// ConstVal - The various declarations that go into the constant pool. This
1076// production is used ONLY to represent constants that show up AFTER a 'const',
1077// 'constant' or 'global' token at global scope. Constants that can be inlined
1078// into other expressions (such as integers and constexprs) are handled by the
1079// ResolvedVal, ValueRef and ConstValueRef productions.
1080//
1081ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencere77e35e2006-12-01 20:26:20 +00001082 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001083 $$.cnst = new std::string($1->getNewTy());
Reid Spencere77e35e2006-12-01 20:26:20 +00001084 *$$.cnst += " [ " + *$3 + " ]";
Reid Spencere7c3c602006-11-30 06:36:44 +00001085 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001086 }
1087 | Types '[' ']' {
Reid Spencere77e35e2006-12-01 20:26:20 +00001088 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001089 $$.cnst = new std::string($1->getNewTy());
Reid Spencere77e35e2006-12-01 20:26:20 +00001090 *$$.cnst += "[ ]";
Reid Spencere7c3c602006-11-30 06:36:44 +00001091 }
1092 | Types 'c' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +00001093 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001094 $$.cnst = new std::string($1->getNewTy());
Reid Spencere77e35e2006-12-01 20:26:20 +00001095 *$$.cnst += " c" + *$3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001096 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001097 }
1098 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencere77e35e2006-12-01 20:26:20 +00001099 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001100 $$.cnst = new std::string($1->getNewTy());
Reid Spencere77e35e2006-12-01 20:26:20 +00001101 *$$.cnst += " < " + *$3 + " >";
Reid Spencere7c3c602006-11-30 06:36:44 +00001102 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001103 }
1104 | Types '{' ConstVector '}' {
Reid Spencere77e35e2006-12-01 20:26:20 +00001105 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001106 $$.cnst = new std::string($1->getNewTy());
Reid Spencere77e35e2006-12-01 20:26:20 +00001107 *$$.cnst += " { " + *$3 + " }";
Reid Spencere7c3c602006-11-30 06:36:44 +00001108 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001109 }
1110 | Types '{' '}' {
Reid Spencere77e35e2006-12-01 20:26:20 +00001111 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001112 $$.cnst = new std::string($1->getNewTy());
Reid Spencer0b7e5072006-12-01 22:42:01 +00001113 *$$.cnst += " {}";
Reid Spencere7c3c602006-11-30 06:36:44 +00001114 }
1115 | Types NULL_TOK {
Reid Spencere77e35e2006-12-01 20:26:20 +00001116 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001117 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +00001118 *$$.cnst += " " + *$2;
1119 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001120 }
1121 | Types UNDEF {
Reid Spencere77e35e2006-12-01 20:26:20 +00001122 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001123 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +00001124 *$$.cnst += " " + *$2;
1125 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001126 }
1127 | Types SymbolicValueRef {
Reid Spencer319a7302007-01-05 17:20:02 +00001128 std::string Name = getUniqueName($2, $1->resolve(), true);
Reid Spencere77e35e2006-12-01 20:26:20 +00001129 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001130 $$.cnst = new std::string($1->getNewTy());
1131 *$$.cnst += " " + Name;
Reid Spencere7c3c602006-11-30 06:36:44 +00001132 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001133 }
1134 | Types ConstExpr {
Reid Spencere77e35e2006-12-01 20:26:20 +00001135 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001136 $$.cnst = new std::string($1->getNewTy());
Reid Spencere77e35e2006-12-01 20:26:20 +00001137 *$$.cnst += " " + *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001138 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001139 }
1140 | Types ZEROINITIALIZER {
Reid Spencere77e35e2006-12-01 20:26:20 +00001141 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001142 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +00001143 *$$.cnst += " " + *$2;
1144 delete $2;
Reid Spencere77e35e2006-12-01 20:26:20 +00001145 }
1146 | SIntType EInt64Val { // integral constants
1147 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001148 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +00001149 *$$.cnst += " " + *$2;
1150 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001151 }
Reid Spencer7356ae42007-01-02 06:34:08 +00001152 | UIntType EInt64Val { // integral constants
Reid Spencere77e35e2006-12-01 20:26:20 +00001153 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001154 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +00001155 *$$.cnst += " " + *$2;
1156 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001157 }
1158 | BOOL TRUETOK { // Boolean constants
Reid Spencere77e35e2006-12-01 20:26:20 +00001159 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001160 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +00001161 *$$.cnst += " " + *$2;
1162 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001163 }
1164 | BOOL FALSETOK { // Boolean constants
Reid Spencere77e35e2006-12-01 20:26:20 +00001165 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001166 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +00001167 *$$.cnst += " " + *$2;
1168 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001169 }
1170 | FPType FPVAL { // Float & Double constants
Reid Spencere77e35e2006-12-01 20:26:20 +00001171 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001172 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +00001173 *$$.cnst += " " + *$2;
1174 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001175 };
1176
1177
Reid Spencerfcb5df82006-12-01 22:34:43 +00001178ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer280d8012006-12-01 23:40:53 +00001179 std::string source = *$3.cnst;
Reid Spencer319a7302007-01-05 17:20:02 +00001180 const TypeInfo* SrcTy = $3.type->resolve();
1181 const TypeInfo* DstTy = $5->resolve();
Reid Spencer280d8012006-12-01 23:40:53 +00001182 if (*$1 == "cast") {
Reid Spencera50d5962006-12-02 04:11:07 +00001183 // Call getCastUpgrade to upgrade the old cast
Reid Spencer319a7302007-01-05 17:20:02 +00001184 $$ = new std::string(getCastUpgrade(source, SrcTy, DstTy, true));
Reid Spencera50d5962006-12-02 04:11:07 +00001185 } else {
1186 // Nothing to upgrade, just create the cast constant expr
1187 $$ = new std::string(*$1);
Reid Spencer52402b02007-01-02 05:45:11 +00001188 *$$ += "( " + source + " to " + $5->getNewTy() + ")";
Reid Spencer280d8012006-12-01 23:40:53 +00001189 }
Reid Spencereff838e2007-01-03 23:45:42 +00001190 delete $1; $3.destroy(); delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001191 }
1192 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencerf8483652006-12-02 15:16:01 +00001193 *$1 += "(" + *$3.cnst;
1194 for (unsigned i = 0; i < $4->size(); ++i) {
1195 ValueInfo& VI = (*$4)[i];
1196 *$1 += ", " + *VI.val;
1197 VI.destroy();
1198 }
1199 *$1 += ")";
Reid Spencere77e35e2006-12-01 20:26:20 +00001200 $$ = $1;
1201 $3.destroy();
1202 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001203 }
1204 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +00001205 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
1206 $3.destroy(); $5.destroy(); $7.destroy();
1207 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001208 }
1209 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer78720742006-12-02 20:21:22 +00001210 const char* op = getDivRemOpcode(*$1, $3.type);
1211 $$ = new std::string(op);
1212 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
1213 delete $1; $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001214 }
1215 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +00001216 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
1217 $3.destroy(); $5.destroy();
1218 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001219 }
1220 | SetCondOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer229e9362006-12-02 22:14:11 +00001221 *$1 = getCompareOp(*$1, $3.type);
Reid Spencere77e35e2006-12-01 20:26:20 +00001222 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
1223 $3.destroy(); $5.destroy();
1224 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001225 }
Reid Spencer57f28f92006-12-03 07:10:26 +00001226 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencera7c46fa2007-01-06 00:23:53 +00001227 *$1 += " " + *$2 + " (" + *$4.cnst + "," + *$6.cnst + ")";
Reid Spencer57f28f92006-12-03 07:10:26 +00001228 delete $2; $4.destroy(); $6.destroy();
1229 $$ = $1;
1230 }
1231 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencera7c46fa2007-01-06 00:23:53 +00001232 *$1 += " " + *$2 + " (" + *$4.cnst + "," + *$6.cnst + ")";
Reid Spencer229e9362006-12-02 22:14:11 +00001233 delete $2; $4.destroy(); $6.destroy();
1234 $$ = $1;
1235 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001236 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencerf7bde222006-12-01 22:26:37 +00001237 const char* shiftop = $1->c_str();
1238 if (*$1 == "shr")
Reid Spencer52402b02007-01-02 05:45:11 +00001239 shiftop = ($3.type->isUnsigned()) ? "lshr" : "ashr";
Reid Spencerf7bde222006-12-01 22:26:37 +00001240 $$ = new std::string(shiftop);
1241 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
1242 delete $1; $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001243 }
1244 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +00001245 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
1246 $3.destroy(); $5.destroy();
1247 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001248 }
1249 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +00001250 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
1251 $3.destroy(); $5.destroy(); $7.destroy();
1252 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001253 }
1254 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +00001255 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
1256 $3.destroy(); $5.destroy(); $7.destroy();
1257 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001258 };
1259
1260
1261// ConstVector - A list of comma separated constants.
Reid Spencere77e35e2006-12-01 20:26:20 +00001262
1263ConstVector
1264 : ConstVector ',' ConstVal {
1265 *$1 += ", " + *$3.cnst;
1266 $3.destroy();
1267 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001268 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001269 | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
1270 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001271
1272
1273// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Reid Spencere77e35e2006-12-01 20:26:20 +00001274GlobalType : GLOBAL | CONSTANT ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001275
1276
1277//===----------------------------------------------------------------------===//
1278// Rules to match Modules
1279//===----------------------------------------------------------------------===//
1280
1281// Module rule: Capture the result of parsing the whole file into a result
1282// variable...
1283//
1284Module : DefinitionList {
1285};
1286
1287// DefinitionList - Top level definitions
1288//
1289DefinitionList : DefinitionList Function {
1290 $$ = 0;
1291 }
1292 | DefinitionList FunctionProto {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001293 *O << *$2 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001294 delete $2;
1295 $$ = 0;
1296 }
1297 | DefinitionList MODULE ASM_TOK AsmBlock {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001298 *O << "module asm " << ' ' << *$4 << '\n';
Reid Spencerd154b572006-12-01 20:36:40 +00001299 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001300 }
1301 | DefinitionList IMPLEMENTATION {
1302 *O << "implementation\n";
Reid Spencerd154b572006-12-01 20:36:40 +00001303 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001304 }
Reid Spencera50d5962006-12-02 04:11:07 +00001305 | ConstPool { $$ = 0; }
Reid Spencere7c3c602006-11-30 06:36:44 +00001306
Reid Spencer78720742006-12-02 20:21:22 +00001307External : EXTERNAL | UNINITIALIZED { $$ = $1; *$$ = "external"; }
1308
Reid Spencere7c3c602006-11-30 06:36:44 +00001309// ConstPool - Constants with optional names assigned to them.
1310ConstPool : ConstPool OptAssign TYPE TypesV {
Reid Spencer319a7302007-01-05 17:20:02 +00001311 EnumeratedTypes.push_back($4);
Reid Spencera50d5962006-12-02 04:11:07 +00001312 if (!$2->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001313 NamedTypes[*$2] = $4;
Reid Spencera50d5962006-12-02 04:11:07 +00001314 *O << *$2 << " = ";
1315 }
Reid Spencer52402b02007-01-02 05:45:11 +00001316 *O << "type " << $4->getNewTy() << '\n';
1317 delete $2; delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001318 $$ = 0;
1319 }
1320 | ConstPool FunctionProto { // Function prototypes can be in const pool
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001321 *O << *$2 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001322 delete $2;
1323 $$ = 0;
1324 }
1325 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001326 *O << *$2 << ' ' << *$3 << ' ' << *$4 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001327 delete $2; delete $3; delete $4;
1328 $$ = 0;
1329 }
1330 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +00001331 if (!$2->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001332 std::string Name = getGlobalName($2,*$3, $5.type->getPointerType(),
1333 *$4 == "constant");
Reid Spencer52402b02007-01-02 05:45:11 +00001334 *O << Name << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +00001335 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001336 *O << *$3 << ' ' << *$4 << ' ' << *$5.cnst << ' ' << *$6 << '\n';
Reid Spencer52402b02007-01-02 05:45:11 +00001337 delete $2; delete $3; delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001338 $$ = 0;
1339 }
Reid Spencer78720742006-12-02 20:21:22 +00001340 | ConstPool OptAssign External GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +00001341 if (!$2->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001342 std::string Name = getGlobalName($2,*$3,$5->getPointerType(),
1343 *$4 == "constant");
Reid Spencer52402b02007-01-02 05:45:11 +00001344 *O << Name << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +00001345 }
Reid Spencer52402b02007-01-02 05:45:11 +00001346 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
1347 delete $2; delete $3; delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001348 $$ = 0;
1349 }
Reid Spencer319a7302007-01-05 17:20:02 +00001350 | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +00001351 if (!$2->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001352 std::string Name = getGlobalName($2,*$3,$5->getPointerType(),
1353 *$4 == "constant");
Reid Spencer52402b02007-01-02 05:45:11 +00001354 *O << Name << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +00001355 }
Reid Spencer52402b02007-01-02 05:45:11 +00001356 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
1357 delete $2; delete $3; delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001358 $$ = 0;
1359 }
1360 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +00001361 if (!$2->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001362 std::string Name = getGlobalName($2,*$3,$5->getPointerType(),
1363 *$4 == "constant");
Reid Spencer52402b02007-01-02 05:45:11 +00001364 *O << Name << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +00001365 }
Reid Spencer52402b02007-01-02 05:45:11 +00001366 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
1367 delete $2; delete $3; delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001368 $$ = 0;
1369 }
1370 | ConstPool TARGET TargetDefinition {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001371 *O << *$2 << ' ' << *$3 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001372 delete $2; delete $3;
1373 $$ = 0;
1374 }
1375 | ConstPool DEPLIBS '=' LibrariesDefinition {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001376 *O << *$2 << " = " << *$4 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001377 delete $2; delete $4;
1378 $$ = 0;
1379 }
1380 | /* empty: end of list */ {
1381 $$ = 0;
1382 };
1383
1384
1385AsmBlock : STRINGCONSTANT ;
1386
1387BigOrLittle : BIG | LITTLE
1388
1389TargetDefinition
1390 : ENDIAN '=' BigOrLittle {
Reid Spencere77e35e2006-12-01 20:26:20 +00001391 *$1 += " = " + *$3;
1392 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001393 $$ = $1;
1394 }
1395 | POINTERSIZE '=' EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +00001396 *$1 += " = " + *$3;
1397 if (*$3 == "64")
Reid Spencere77e35e2006-12-01 20:26:20 +00001398 SizeOfPointer = 64;
Reid Spencerf2d55322006-12-01 21:52:30 +00001399 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001400 $$ = $1;
1401 }
1402 | TRIPLE '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +00001403 *$1 += " = " + *$3;
1404 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001405 $$ = $1;
1406 }
1407 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +00001408 *$1 += " = " + *$3;
1409 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001410 $$ = $1;
1411 };
1412
1413LibrariesDefinition
1414 : '[' LibList ']' {
1415 $2->insert(0, "[ ");
1416 *$2 += " ]";
1417 $$ = $2;
1418 };
1419
1420LibList
1421 : LibList ',' STRINGCONSTANT {
1422 *$1 += ", " + *$3;
1423 delete $3;
1424 $$ = $1;
1425 }
1426 | STRINGCONSTANT
1427 | /* empty: end of list */ {
1428 $$ = new std::string();
1429 };
1430
1431//===----------------------------------------------------------------------===//
1432// Rules to match Function Headers
1433//===----------------------------------------------------------------------===//
1434
1435Name : VAR_ID | STRINGCONSTANT;
1436OptName : Name | /*empty*/ { $$ = new std::string(); };
1437
1438ArgVal : Types OptName {
Reid Spencer52402b02007-01-02 05:45:11 +00001439 $$ = new std::string($1->getNewTy());
1440 if (!$2->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001441 std::string Name = getUniqueName($2, $1->resolve());
Reid Spencer52402b02007-01-02 05:45:11 +00001442 *$$ += " " + Name;
1443 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001444 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001445};
1446
1447ArgListH : ArgListH ',' ArgVal {
1448 *$1 += ", " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001449 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001450 }
1451 | ArgVal {
1452 $$ = $1;
1453 };
1454
1455ArgList : ArgListH {
1456 $$ = $1;
1457 }
1458 | ArgListH ',' DOTDOTDOT {
1459 *$1 += ", ...";
1460 $$ = $1;
Reid Spencere77e35e2006-12-01 20:26:20 +00001461 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001462 }
1463 | DOTDOTDOT {
1464 $$ = $1;
1465 }
Reid Spencerd154b572006-12-01 20:36:40 +00001466 | /* empty */ { $$ = new std::string(); };
Reid Spencere7c3c602006-11-30 06:36:44 +00001467
Reid Spencer71d2ec92006-12-31 06:02:26 +00001468FunctionHeaderH
1469 : OptCallingConv TypesV Name '(' ArgList ')' OptSection OptAlign {
Reid Spencerc4d96252007-01-13 00:03:30 +00001470 if (*$3 == "%llvm.va_start" || *$3 == "%llvm.va_end") {
1471 *$5 = "i8* ";
1472 } else if (*$3 == "%llvm.va_copy") {
1473 *$5 = "i8*, i8*";
1474 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001475 if (!$1->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +00001476 *$1 += " ";
Reid Spencere7c3c602006-11-30 06:36:44 +00001477 }
Reid Spencer52402b02007-01-02 05:45:11 +00001478 *$1 += $2->getNewTy() + " " + *$3 + "(" + *$5 + ")";
Reid Spencere7c3c602006-11-30 06:36:44 +00001479 if (!$7->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +00001480 *$1 += " " + *$7;
Reid Spencere7c3c602006-11-30 06:36:44 +00001481 }
1482 if (!$8->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +00001483 *$1 += " " + *$8;
Reid Spencere7c3c602006-11-30 06:36:44 +00001484 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001485 delete $3;
1486 delete $5;
1487 delete $7;
1488 delete $8;
1489 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001490 };
1491
Reid Spencer78720742006-12-02 20:21:22 +00001492BEGIN : BEGINTOK { $$ = new std::string("{"); delete $1; }
1493 | '{' { $$ = new std::string ("{"); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001494
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001495FunctionHeader
1496 : OptLinkage FunctionHeaderH BEGIN {
1497 *O << "define ";
1498 if (!$1->empty()) {
1499 *O << *$1 << ' ';
1500 }
1501 *O << *$2 << ' ' << *$3 << '\n';
1502 delete $1; delete $2; delete $3;
1503 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001504 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001505 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001506
Reid Spencer78720742006-12-02 20:21:22 +00001507END : ENDTOK { $$ = new std::string("}"); delete $1; }
Reid Spencere7c3c602006-11-30 06:36:44 +00001508 | '}' { $$ = new std::string("}"); };
1509
1510Function : FunctionHeader BasicBlockList END {
1511 if ($2)
1512 *O << *$2;
Reid Spencer71d2ec92006-12-31 06:02:26 +00001513 *O << *$3 << "\n\n";
Reid Spencer52402b02007-01-02 05:45:11 +00001514 delete $1; delete $2; delete $3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001515 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001516};
1517
Reid Spencere77e35e2006-12-01 20:26:20 +00001518FnDeclareLinkage
1519 : /*default*/ { $$ = new std::string(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001520 | DLLIMPORT
1521 | EXTERN_WEAK
1522 ;
1523
1524FunctionProto
Reid Spencerc4d96252007-01-13 00:03:30 +00001525 : DECLARE { isDeclare = true; } FnDeclareLinkage FunctionHeaderH {
1526 if (!$3->empty())
1527 *$1 += " " + *$3;
1528 *$1 += " " + *$4;
Reid Spencere77e35e2006-12-01 20:26:20 +00001529 delete $3;
Reid Spencerc4d96252007-01-13 00:03:30 +00001530 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001531 $$ = $1;
Reid Spencerc4d96252007-01-13 00:03:30 +00001532 isDeclare = false;
Reid Spencere7c3c602006-11-30 06:36:44 +00001533 };
1534
1535//===----------------------------------------------------------------------===//
1536// Rules to match Basic Blocks
1537//===----------------------------------------------------------------------===//
1538
Reid Spencerd154b572006-12-01 20:36:40 +00001539OptSideEffect : /* empty */ { $$ = new std::string(); }
1540 | SIDEEFFECT;
Reid Spencere7c3c602006-11-30 06:36:44 +00001541
Reid Spencere77e35e2006-12-01 20:26:20 +00001542ConstValueRef
Reid Spencerf2d55322006-12-01 21:52:30 +00001543 : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK | NULL_TOK | UNDEF
1544 | ZEROINITIALIZER
Reid Spencere7c3c602006-11-30 06:36:44 +00001545 | '<' ConstVector '>' {
1546 $2->insert(0, "<");
1547 *$2 += ">";
1548 $$ = $2;
1549 }
1550 | ConstExpr
1551 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
1552 if (!$2->empty()) {
1553 *$1 += " " + *$2;
1554 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001555 *$1 += " " + *$3 + ", " + *$5;
1556 delete $2; delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001557 $$ = $1;
1558 };
1559
Reid Spencerf2d55322006-12-01 21:52:30 +00001560SymbolicValueRef : IntVal | Name ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001561
1562// ValueRef - A reference to a definition... either constant or symbolic
Reid Spencerf459d392006-12-02 16:19:52 +00001563ValueRef
1564 : SymbolicValueRef {
1565 $$.val = $1;
1566 $$.constant = false;
Reid Spencer319a7302007-01-05 17:20:02 +00001567 $$.type = 0;
Reid Spencerf459d392006-12-02 16:19:52 +00001568 }
1569 | ConstValueRef {
1570 $$.val = $1;
1571 $$.constant = true;
Reid Spencer319a7302007-01-05 17:20:02 +00001572 $$.type = 0;
Reid Spencerf459d392006-12-02 16:19:52 +00001573 }
1574 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001575
1576// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1577// type immediately preceeds the value reference, and allows complex constant
1578// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1579ResolvedVal : Types ValueRef {
Reid Spencer319a7302007-01-05 17:20:02 +00001580 $1 = $1->resolve();
Reid Spencer52402b02007-01-02 05:45:11 +00001581 std::string Name = getUniqueName($2.val, $1);
Reid Spencerf459d392006-12-02 16:19:52 +00001582 $$ = $2;
Reid Spencer52402b02007-01-02 05:45:11 +00001583 delete $$.val;
Reid Spencer52402b02007-01-02 05:45:11 +00001584 $$.val = new std::string($1->getNewTy() + " " + Name);
Reid Spencere77e35e2006-12-01 20:26:20 +00001585 $$.type = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001586 };
1587
1588BasicBlockList : BasicBlockList BasicBlock {
Reid Spencerf2d55322006-12-01 21:52:30 +00001589 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001590 }
1591 | BasicBlock { // Do not allow functions with 0 basic blocks
Reid Spencerf2d55322006-12-01 21:52:30 +00001592 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001593 };
1594
1595
1596// Basic blocks are terminated by branching instructions:
1597// br, br/cc, switch, ret
1598//
Reid Spencer16244f42006-12-01 21:10:07 +00001599BasicBlock : InstructionList BBTerminatorInst {
Reid Spencerf2d55322006-12-01 21:52:30 +00001600 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001601 };
1602
1603InstructionList : InstructionList Inst {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001604 *O << " " << *$2 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001605 delete $2;
1606 $$ = 0;
1607 }
1608 | /* empty */ {
1609 $$ = 0;
1610 }
1611 | LABELSTR {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001612 *O << *$1 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001613 delete $1;
1614 $$ = 0;
1615 };
1616
Reid Spencer78720742006-12-02 20:21:22 +00001617Unwind : UNWIND | EXCEPT { $$ = $1; *$$ = "unwind"; }
1618
Reid Spencere7c3c602006-11-30 06:36:44 +00001619BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001620 *O << " " << *$1 << ' ' << *$2.val << '\n';
Reid Spencere77e35e2006-12-01 20:26:20 +00001621 delete $1; $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001622 $$ = 0;
1623 }
1624 | RET VOID { // Return with no result...
Reid Spencer52402b02007-01-02 05:45:11 +00001625 *O << " " << *$1 << ' ' << $2->getNewTy() << '\n';
Reid Spencer319a7302007-01-05 17:20:02 +00001626 delete $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001627 $$ = 0;
1628 }
1629 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer52402b02007-01-02 05:45:11 +00001630 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << *$3.val << '\n';
Reid Spencer319a7302007-01-05 17:20:02 +00001631 delete $1; $3.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001632 $$ = 0;
1633 } // Conditional Branch...
1634 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001635 std::string Name = getUniqueName($3.val, $2);
1636 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", "
1637 << $5->getNewTy() << ' ' << *$6.val << ", " << $8->getNewTy() << ' '
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001638 << *$9.val << '\n';
Reid Spencer319a7302007-01-05 17:20:02 +00001639 delete $1; $3.destroy(); $6.destroy(); $9.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001640 $$ = 0;
1641 }
1642 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencer52402b02007-01-02 05:45:11 +00001643 std::string Name = getUniqueName($3.val, $2);
1644 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", "
1645 << $5->getNewTy() << ' ' << *$6.val << " [" << *$8 << " ]\n";
Reid Spencer319a7302007-01-05 17:20:02 +00001646 delete $1; $3.destroy(); $6.destroy();
Reid Spencerf459d392006-12-02 16:19:52 +00001647 delete $8;
Reid Spencere7c3c602006-11-30 06:36:44 +00001648 $$ = 0;
1649 }
1650 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencer52402b02007-01-02 05:45:11 +00001651 std::string Name = getUniqueName($3.val, $2);
1652 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", "
1653 << $5->getNewTy() << ' ' << *$6.val << "[]\n";
Reid Spencer319a7302007-01-05 17:20:02 +00001654 delete $1; $3.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001655 $$ = 0;
1656 }
Reid Spencer16244f42006-12-01 21:10:07 +00001657 | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
Reid Spencer78720742006-12-02 20:21:22 +00001658 TO LABEL ValueRef Unwind LABEL ValueRef {
Reid Spencer319a7302007-01-05 17:20:02 +00001659 const TypeInfo* ResTy = getFunctionReturnType($4);
Reid Spencer16244f42006-12-01 21:10:07 +00001660 *O << " ";
Reid Spencer52402b02007-01-02 05:45:11 +00001661 if (!$1->empty()) {
1662 std::string Name = getUniqueName($1, ResTy);
1663 *O << Name << " = ";
1664 }
1665 *O << *$2 << ' ' << *$3 << ' ' << $4->getNewTy() << ' ' << *$5.val << " (";
Reid Spencerf8483652006-12-02 15:16:01 +00001666 for (unsigned i = 0; i < $7->size(); ++i) {
1667 ValueInfo& VI = (*$7)[i];
1668 *O << *VI.val;
1669 if (i+1 < $7->size())
1670 *O << ", ";
1671 VI.destroy();
1672 }
Reid Spencer52402b02007-01-02 05:45:11 +00001673 *O << ") " << *$9 << ' ' << $10->getNewTy() << ' ' << *$11.val << ' '
1674 << *$12 << ' ' << $13->getNewTy() << ' ' << *$14.val << '\n';
Reid Spencer319a7302007-01-05 17:20:02 +00001675 delete $1; delete $2; delete $3; $5.destroy(); delete $7;
1676 delete $9; $11.destroy(); delete $12; $14.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001677 $$ = 0;
1678 }
Reid Spencer78720742006-12-02 20:21:22 +00001679 | Unwind {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001680 *O << " " << *$1 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001681 delete $1;
1682 $$ = 0;
1683 }
1684 | UNREACHABLE {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001685 *O << " " << *$1 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001686 delete $1;
1687 $$ = 0;
1688 };
1689
1690JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001691 *$1 += " " + $2->getNewTy() + " " + *$3 + ", " + $5->getNewTy() + " " +
1692 *$6.val;
Reid Spencer319a7302007-01-05 17:20:02 +00001693 delete $3; $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001694 $$ = $1;
1695 }
1696 | IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001697 $2->insert(0, $1->getNewTy() + " " );
1698 *$2 += ", " + $4->getNewTy() + " " + *$5.val;
Reid Spencer319a7302007-01-05 17:20:02 +00001699 $5.destroy();
Reid Spencere77e35e2006-12-01 20:26:20 +00001700 $$ = $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001701 };
1702
1703Inst
1704 : OptAssign InstVal {
Reid Spencerf5626a32007-01-01 01:20:41 +00001705 if (!$1->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001706 // Get a unique name for this value, based on its type.
1707 std::string Name = getUniqueName($1, $2.type);
1708 *$1 = Name + " = ";
1709 if (deleteUselessCastFlag && *deleteUselessCastName == Name) {
1710 // don't actually delete it, just comment it out
1711 $1->insert(0, "; USELSS BITCAST: ");
Reid Spencerf5626a32007-01-01 01:20:41 +00001712 delete deleteUselessCastName;
Reid Spencerf5626a32007-01-01 01:20:41 +00001713 }
1714 }
Reid Spencer52402b02007-01-02 05:45:11 +00001715 *$1 += *$2.val;
1716 $2.destroy();
Reid Spencerf5626a32007-01-01 01:20:41 +00001717 deleteUselessCastFlag = false;
Reid Spencere7c3c602006-11-30 06:36:44 +00001718 $$ = $1;
1719 };
1720
1721PHIList
1722 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer52402b02007-01-02 05:45:11 +00001723 std::string Name = getUniqueName($3.val, $1);
1724 Name.insert(0, $1->getNewTy() + "[");
1725 Name += "," + *$5.val + "]";
1726 $$.val = new std::string(Name);
1727 $$.type = $1;
1728 $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001729 }
1730 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
Reid Spencer52402b02007-01-02 05:45:11 +00001731 std::string Name = getUniqueName($4.val, $1.type);
1732 *$1.val += ", [" + Name + "," + *$6.val + "]";
Reid Spencerf459d392006-12-02 16:19:52 +00001733 $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001734 $$ = $1;
1735 };
1736
1737
1738ValueRefList
Reid Spencer52402b02007-01-02 05:45:11 +00001739 : ResolvedVal {
Reid Spencerf8483652006-12-02 15:16:01 +00001740 $$ = new ValueList();
1741 $$->push_back($1);
1742 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001743 | ValueRefList ',' ResolvedVal {
Reid Spencere7c3c602006-11-30 06:36:44 +00001744 $$ = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001745 $$->push_back($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00001746 };
1747
1748// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1749ValueRefListE
Reid Spencerf8483652006-12-02 15:16:01 +00001750 : ValueRefList { $$ = $1; }
1751 | /*empty*/ { $$ = new ValueList(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001752 ;
1753
1754OptTailCall
1755 : TAIL CALL {
1756 *$1 += " " + *$2;
1757 delete $2;
1758 $$ = $1;
1759 }
1760 | CALL
1761 ;
1762
1763InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer78720742006-12-02 20:21:22 +00001764 const char* op = getDivRemOpcode(*$1, $2);
Reid Spencer52402b02007-01-02 05:45:11 +00001765 std::string Name1 = getUniqueName($3.val, $2);
1766 std::string Name2 = getUniqueName($5.val, $2);
1767 $$.val = new std::string(op);
1768 *$$.val += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
1769 $$.type = $2;
1770 delete $1; $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001771 }
1772 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001773 std::string Name1 = getUniqueName($3.val, $2);
1774 std::string Name2 = getUniqueName($5.val, $2);
1775 *$1 += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
1776 $$.val = $1;
1777 $$.type = $2;
1778 $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001779 }
1780 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001781 std::string Name1 = getUniqueName($3.val, $2);
1782 std::string Name2 = getUniqueName($5.val, $2);
Reid Spencer229e9362006-12-02 22:14:11 +00001783 *$1 = getCompareOp(*$1, $2);
Reid Spencer52402b02007-01-02 05:45:11 +00001784 *$1 += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
1785 $$.val = $1;
Reid Spencer319a7302007-01-05 17:20:02 +00001786 $$.type = TypeInfo::get("bool",BoolTy);
Reid Spencer52402b02007-01-02 05:45:11 +00001787 $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001788 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001789 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001790 std::string Name1 = getUniqueName($4.val, $3);
1791 std::string Name2 = getUniqueName($6.val, $3);
1792 *$1 += " " + *$2 + " " + $3->getNewTy() + " " + Name1 + "," + Name2;
1793 $$.val = $1;
Reid Spencer319a7302007-01-05 17:20:02 +00001794 $$.type = TypeInfo::get("bool",BoolTy);
Reid Spencer57f28f92006-12-03 07:10:26 +00001795 delete $2; $4.destroy(); $6.destroy();
Reid Spencer57f28f92006-12-03 07:10:26 +00001796 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001797 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001798 std::string Name1 = getUniqueName($4.val, $3);
1799 std::string Name2 = getUniqueName($6.val, $3);
1800 *$1 += " " + *$2 + " " + $3->getNewTy() + " " + Name1 + "," + Name2;
1801 $$.val = $1;
Reid Spencer319a7302007-01-05 17:20:02 +00001802 $$.type = TypeInfo::get("bool",BoolTy);
Reid Spencer229e9362006-12-02 22:14:11 +00001803 delete $2; $4.destroy(); $6.destroy();
Reid Spencer229e9362006-12-02 22:14:11 +00001804 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001805 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencerf7bde222006-12-01 22:26:37 +00001806 const char* shiftop = $1->c_str();
1807 if (*$1 == "shr")
Reid Spencer52402b02007-01-02 05:45:11 +00001808 shiftop = ($2.type->isUnsigned()) ? "lshr" : "ashr";
1809 $$.val = new std::string(shiftop);
1810 *$$.val += " " + *$2.val + ", " + *$4.val;
1811 $$.type = $2.type;
Reid Spencerf8383de2007-01-06 06:04:32 +00001812 delete $1; $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001813 }
Reid Spencerfcb5df82006-12-01 22:34:43 +00001814 | CastOps ResolvedVal TO Types {
Reid Spencer280d8012006-12-01 23:40:53 +00001815 std::string source = *$2.val;
Reid Spencer319a7302007-01-05 17:20:02 +00001816 const TypeInfo* SrcTy = $2.type->resolve();
1817 const TypeInfo* DstTy = $4->resolve();
Reid Spencer52402b02007-01-02 05:45:11 +00001818 $$.val = new std::string();
Reid Spencer319a7302007-01-05 17:20:02 +00001819 $$.type = DstTy;
Reid Spencer280d8012006-12-01 23:40:53 +00001820 if (*$1 == "cast") {
Reid Spencer319a7302007-01-05 17:20:02 +00001821 *$$.val += getCastUpgrade(source, SrcTy, DstTy, false);
Reid Spencera50d5962006-12-02 04:11:07 +00001822 } else {
Reid Spencer52402b02007-01-02 05:45:11 +00001823 *$$.val += *$1 + " " + source + " to " + DstTy->getNewTy();
Reid Spencer280d8012006-12-01 23:40:53 +00001824 }
Reid Spencerf5626a32007-01-01 01:20:41 +00001825 // Check to see if this is a useless cast of a value to the same name
1826 // and the same type. Such casts will probably cause redefinition errors
1827 // when assembled and perform no code gen action so just remove them.
1828 if (*$1 == "cast" || *$1 == "bitcast")
Reid Spencer319a7302007-01-05 17:20:02 +00001829 if (SrcTy->isInteger() && DstTy->isInteger() &&
1830 SrcTy->getBitWidth() == DstTy->getBitWidth()) {
Reid Spencerf5626a32007-01-01 01:20:41 +00001831 deleteUselessCastFlag = true; // Flag the "Inst" rule
1832 deleteUselessCastName = new std::string(*$2.val); // save the name
1833 size_t pos = deleteUselessCastName->find_first_of("%\"",0);
1834 if (pos != std::string::npos) {
1835 // remove the type portion before val
1836 deleteUselessCastName->erase(0, pos);
1837 }
1838 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001839 delete $1; $2.destroy();
Reid Spencer52402b02007-01-02 05:45:11 +00001840 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001841 }
1842 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001843 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
Reid Spencer52402b02007-01-02 05:45:11 +00001844 $$.val = $1;
1845 $$.type = $4.type;
Reid Spencerf8383de2007-01-06 06:04:32 +00001846 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001847 }
1848 | VAARG ResolvedVal ',' Types {
Reid Spencer52402b02007-01-02 05:45:11 +00001849 *$1 += " " + *$2.val + ", " + $4->getNewTy();
1850 $$.val = $1;
1851 $$.type = $4;
1852 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001853 }
1854 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001855 *$1 += " " + *$2.val + ", " + *$4.val;
Reid Spencer52402b02007-01-02 05:45:11 +00001856 $$.val = $1;
Reid Spencer319a7302007-01-05 17:20:02 +00001857 $2.type = $2.type->resolve();;
Reid Spencereff838e2007-01-03 23:45:42 +00001858 $$.type = $2.type->getElementType();
Reid Spencerf8383de2007-01-06 06:04:32 +00001859 $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001860 }
1861 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001862 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
Reid Spencer52402b02007-01-02 05:45:11 +00001863 $$.val = $1;
1864 $$.type = $2.type;
Reid Spencerf8383de2007-01-06 06:04:32 +00001865 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001866 }
1867 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001868 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
Reid Spencer52402b02007-01-02 05:45:11 +00001869 $$.val = $1;
1870 $$.type = $2.type;
Reid Spencerf8383de2007-01-06 06:04:32 +00001871 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001872 }
1873 | PHI_TOK PHIList {
Reid Spencer52402b02007-01-02 05:45:11 +00001874 *$1 += " " + *$2.val;
1875 $$.val = $1;
1876 $$.type = $2.type;
1877 delete $2.val;
Reid Spencere7c3c602006-11-30 06:36:44 +00001878 }
1879 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Reid Spencer12969882007-01-07 08:07:39 +00001880 // map llvm.isunordered to "fcmp uno"
1881 if (*$4.val == "%llvm.isunordered.f32" ||
1882 *$4.val == "%llvm.isunordered.f64") {
1883 $$.val = new std::string( "fcmp uno " + *(*$6)[0].val + ", ");
1884 size_t pos = (*$6)[1].val->find(' ');
1885 assert(pos != std::string::npos && "no space?");
1886 *$$.val += (*$6)[1].val->substr(pos+1);
1887 $$.type = TypeInfo::get("bool", BoolTy);
1888 } else {
Reid Spencerc4d96252007-01-13 00:03:30 +00001889 static unsigned upgradeCount = 1;
1890 if (*$4.val == "%llvm.va_start" || *$4.val == "%llvm.va_end") {
1891 std::string name("%va_upgrade");
1892 name += llvm::utostr(upgradeCount++);
1893 $1->insert(0, name + " = bitcast " + *(*$6)[0].val + " to i8*\n ");
1894 *(*$6)[0].val = "i8* " + name;
1895 (*$6)[0].type = TypeInfo::get("i8", UByteTy)->getPointerType();
1896 } else if (*$4.val == "%llvm.va_copy") {
1897 std::string name0("%va_upgrade");
1898 name0 += llvm::utostr(upgradeCount++);
1899 std::string name1("%va_upgrade");
1900 name1 += llvm::utostr(upgradeCount++);
1901 $1->insert(0, name0 + " = bitcast " + *(*$6)[0].val + " to i8*\n " +
1902 name1 + " = bitcast " + *(*$6)[1].val + " to i8*\n ");
1903 *(*$6)[0].val = "i8* " + name0;
1904 (*$6)[0].type = TypeInfo::get("i8", UByteTy)->getPointerType();
1905 *(*$6)[1].val = "i8* " + name1;
1906 (*$6)[0].type = TypeInfo::get("i8", UByteTy)->getPointerType();
1907 }
Reid Spencer12969882007-01-07 08:07:39 +00001908 if (!$2->empty())
1909 *$1 += " " + *$2;
1910 if (!$1->empty())
1911 *$1 += " ";
1912 *$1 += $3->getNewTy() + " " + *$4.val + "(";
1913 for (unsigned i = 0; i < $6->size(); ++i) {
1914 ValueInfo& VI = (*$6)[i];
1915 *$1 += *VI.val;
1916 if (i+1 < $6->size())
1917 *$1 += ", ";
1918 VI.destroy();
1919 }
1920 *$1 += ")";
1921 $$.val = $1;
1922 $$.type = getFunctionReturnType($3);
Reid Spencerf8483652006-12-02 15:16:01 +00001923 }
Reid Spencer319a7302007-01-05 17:20:02 +00001924 delete $2; $4.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001925 }
1926 | MemoryInst ;
1927
1928
1929// IndexList - List of indices for GEP based instructions...
1930IndexList
Reid Spencerf8483652006-12-02 15:16:01 +00001931 : ',' ValueRefList { $$ = $2; }
1932 | /* empty */ { $$ = new ValueList(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001933 ;
1934
1935OptVolatile
1936 : VOLATILE
1937 | /* empty */ { $$ = new std::string(); }
1938 ;
1939
1940MemoryInst : MALLOC Types OptCAlign {
Reid Spencer52402b02007-01-02 05:45:11 +00001941 *$1 += " " + $2->getNewTy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001942 if (!$3->empty())
1943 *$1 += " " + *$3;
Reid Spencer52402b02007-01-02 05:45:11 +00001944 $$.val = $1;
1945 $$.type = $2->getPointerType();
Reid Spencer319a7302007-01-05 17:20:02 +00001946 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001947 }
1948 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencer52402b02007-01-02 05:45:11 +00001949 std::string Name = getUniqueName($5.val, $4);
1950 *$1 += " " + $2->getNewTy() + ", " + $4->getNewTy() + " " + Name;
Reid Spencere7c3c602006-11-30 06:36:44 +00001951 if (!$6->empty())
1952 *$1 += " " + *$6;
Reid Spencer52402b02007-01-02 05:45:11 +00001953 $$.val = $1;
1954 $$.type = $2->getPointerType();
Reid Spencer319a7302007-01-05 17:20:02 +00001955 $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001956 }
1957 | ALLOCA Types OptCAlign {
Reid Spencer52402b02007-01-02 05:45:11 +00001958 *$1 += " " + $2->getNewTy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001959 if (!$3->empty())
1960 *$1 += " " + *$3;
Reid Spencer52402b02007-01-02 05:45:11 +00001961 $$.val = $1;
1962 $$.type = $2->getPointerType();
Reid Spencer319a7302007-01-05 17:20:02 +00001963 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001964 }
1965 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencer52402b02007-01-02 05:45:11 +00001966 std::string Name = getUniqueName($5.val, $4);
1967 *$1 += " " + $2->getNewTy() + ", " + $4->getNewTy() + " " + Name;
Reid Spencere7c3c602006-11-30 06:36:44 +00001968 if (!$6->empty())
1969 *$1 += " " + *$6;
Reid Spencer52402b02007-01-02 05:45:11 +00001970 $$.val = $1;
1971 $$.type = $2->getPointerType();
Reid Spencer319a7302007-01-05 17:20:02 +00001972 $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001973 }
1974 | FREE ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001975 *$1 += " " + *$2.val;
Reid Spencer52402b02007-01-02 05:45:11 +00001976 $$.val = $1;
Reid Spencer319a7302007-01-05 17:20:02 +00001977 $$.type = TypeInfo::get("void", VoidTy);
Reid Spencere77e35e2006-12-01 20:26:20 +00001978 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001979 }
1980 | OptVolatile LOAD Types ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001981 std::string Name = getUniqueName($4.val, $3);
Reid Spencere7c3c602006-11-30 06:36:44 +00001982 if (!$1->empty())
1983 *$1 += " ";
Reid Spencer52402b02007-01-02 05:45:11 +00001984 *$1 += *$2 + " " + $3->getNewTy() + " " + Name;
1985 $$.val = $1;
Reid Spencer319a7302007-01-05 17:20:02 +00001986 $$.type = $3->getElementType();
1987 delete $2; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001988 }
1989 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001990 std::string Name = getUniqueName($6.val, $5);
Reid Spencere7c3c602006-11-30 06:36:44 +00001991 if (!$1->empty())
1992 *$1 += " ";
Reid Spencer52402b02007-01-02 05:45:11 +00001993 *$1 += *$2 + " " + *$3.val + ", " + $5->getNewTy() + " " + Name;
1994 $$.val = $1;
Reid Spencer319a7302007-01-05 17:20:02 +00001995 $$.type = TypeInfo::get("void", VoidTy);
1996 delete $2; $3.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001997 }
1998 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer52402b02007-01-02 05:45:11 +00001999 std::string Name = getUniqueName($3.val, $2);
Reid Spencerf459d392006-12-02 16:19:52 +00002000 // Upgrade the indices
2001 for (unsigned i = 0; i < $4->size(); ++i) {
2002 ValueInfo& VI = (*$4)[i];
Reid Spencer52402b02007-01-02 05:45:11 +00002003 if (VI.type->isUnsigned() && !VI.isConstant() &&
2004 VI.type->getBitWidth() < 64) {
Reid Spencerf8383de2007-01-06 06:04:32 +00002005 *O << " %gep_upgrade" << unique << " = zext " << *VI.val
Reid Spencer71d2ec92006-12-31 06:02:26 +00002006 << " to i64\n";
Reid Spencerf8383de2007-01-06 06:04:32 +00002007 *VI.val = "i64 %gep_upgrade" + llvm::utostr(unique++);
Reid Spencer319a7302007-01-05 17:20:02 +00002008 VI.type = TypeInfo::get("i64",ULongTy);
Reid Spencerf459d392006-12-02 16:19:52 +00002009 }
2010 }
Reid Spencer52402b02007-01-02 05:45:11 +00002011 *$1 += " " + $2->getNewTy() + " " + Name;
Reid Spencerf8483652006-12-02 15:16:01 +00002012 for (unsigned i = 0; i < $4->size(); ++i) {
2013 ValueInfo& VI = (*$4)[i];
2014 *$1 += ", " + *VI.val;
Reid Spencerf8483652006-12-02 15:16:01 +00002015 }
Reid Spencer52402b02007-01-02 05:45:11 +00002016 $$.val = $1;
2017 $$.type = getGEPIndexedType($2,$4);
2018 $3.destroy(); delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00002019 };
2020
2021%%
2022
2023int yyerror(const char *ErrorMsg) {
2024 std::string where
2025 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2026 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
Reid Spencer319a7302007-01-05 17:20:02 +00002027 std::string errMsg = where + "error: " + std::string(ErrorMsg) +
2028 " while reading ";
Reid Spencere7c3c602006-11-30 06:36:44 +00002029 if (yychar == YYEMPTY || yychar == 0)
2030 errMsg += "end-of-file.";
2031 else
2032 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
Reid Spencer71d2ec92006-12-31 06:02:26 +00002033 std::cerr << "llvm-upgrade: " << errMsg << '\n';
Chris Lattner37e01c52007-01-04 18:46:42 +00002034 *O << "llvm-upgrade parse failed.\n";
Reid Spencere7c3c602006-11-30 06:36:44 +00002035 exit(1);
2036}
Reid Spencer319a7302007-01-05 17:20:02 +00002037
2038static void warning(const std::string& ErrorMsg) {
2039 std::string where
2040 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2041 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
2042 std::string errMsg = where + "warning: " + std::string(ErrorMsg) +
2043 " while reading ";
2044 if (yychar == YYEMPTY || yychar == 0)
2045 errMsg += "end-of-file.";
2046 else
2047 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
2048 std::cerr << "llvm-upgrade: " << errMsg << '\n';
2049}