blob: 7501c0c4f5707c3dc166c5bf0bda3b5ed0244bf4 [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 Spencer30d0c582007-01-15 00:26:18 +000033
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 Spencer319a7302007-01-05 17:20:02 +000039static void warning(const std::string& msg);
Reid Spencera50d5962006-12-02 04:11:07 +000040
Reid Spencer96839be2006-11-30 16:50:26 +000041void UpgradeAssembly(const std::string &infile, std::istream& in,
Reid Spencer71d2ec92006-12-31 06:02:26 +000042 std::ostream &out, bool debug, bool addAttrs)
Reid Spencere7c3c602006-11-30 06:36:44 +000043{
44 Upgradelineno = 1;
45 CurFilename = infile;
Reid Spencer96839be2006-11-30 16:50:26 +000046 LexInput = &in;
Reid Spencere77e35e2006-12-01 20:26:20 +000047 yydebug = debug;
Reid Spencer71d2ec92006-12-31 06:02:26 +000048 AddAttributes = addAttrs;
Reid Spencere7c3c602006-11-30 06:36:44 +000049 O = &out;
50
51 if (yyparse()) {
Reid Spencer30d0c582007-01-15 00:26:18 +000052 std::cerr << "llvm-upgrade: parse failed.\n";
53 out << "llvm-upgrade: parse failed.\n";
Reid Spencere7c3c602006-11-30 06:36:44 +000054 exit(1);
55 }
56}
57
Reid Spencer30d0c582007-01-15 00:26:18 +000058namespace { // Anonymous namespace to keep our implementation local
59
60
61/// This type is used to keep track of the signedness of values. Instead
62/// of creating llvm::Value directly, the parser will create ValueInfo which
63/// associates a Value* with a Signedness indication.
64struct ValueInfo {
65 std::string* val;
66 const TypeInfo* type;
67 bool constant;
68 bool isConstant() const { return constant; }
69 ~ValueInfo() { delete val; }
70};
71
72
73/// This type is used to keep track of the signedness of the obsolete
74/// integer types. Instead of creating an llvm::Type directly, the Lexer will
75/// create instances of TypeInfo which retains the signedness indication so
76/// it can be used by the parser for upgrade decisions.
77/// For example if "uint" is encountered then the "first" field will be set
78/// to "int32" and the "second" field will be set to "isUnsigned". If the
79/// type is not obsolete then "second" will be set to "isSignless".
80class TypeInfo {
81public:
82 static const TypeInfo* get(const std::string &newType, Types oldType);
83 static const TypeInfo* get(const std::string& newType, Types oldType,
84 const TypeInfo* eTy, const TypeInfo* rTy);
85
86 static const TypeInfo* get(const std::string& newType, Types oldType,
87 const TypeInfo *eTy, uint64_t elems);
88
89 static const TypeInfo* get(const std::string& newType, Types oldType,
90 TypeList* TL);
91
92 static const TypeInfo* get(const std::string& newType, const TypeInfo* resTy,
93 TypeList* TL);
94
95 const TypeInfo* resolve() const;
96 bool operator<(const TypeInfo& that) const;
97
98 bool sameNewTyAs(const TypeInfo* that) const {
99 return this->newTy == that->newTy;
100 }
101
102 bool sameOldTyAs(const TypeInfo* that) const;
103
104 Types getElementTy() const {
105 if (elemTy) {
106 return elemTy->oldTy;
107 }
108 return UnresolvedTy;
109 }
110
111 unsigned getUpRefNum() const {
112 assert(oldTy == UpRefTy && "Can't getUpRefNum on non upreference");
113 return atoi(&((getNewTy().c_str())[1])); // skip the slash
114 }
115
116 typedef std::vector<const TypeInfo*> UpRefStack;
117 void getSignedness(unsigned &sNum, unsigned &uNum, UpRefStack& stk) const;
118 std::string makeUniqueName(const std::string& BaseName) const;
119
120 const std::string& getNewTy() const { return newTy; }
121 const TypeInfo* getResultType() const { return resultTy; }
122 const TypeInfo* getElementType() const { return elemTy; }
123
124 const TypeInfo* getPointerType() const {
125 return get(newTy + "*", PointerTy, this, (TypeInfo*)0);
126 }
127
128 bool isUnresolved() const { return oldTy == UnresolvedTy; }
129 bool isUpReference() const { return oldTy == UpRefTy; }
130 bool isVoid() const { return oldTy == VoidTy; }
131 bool isBool() const { return oldTy == BoolTy; }
132 bool isSigned() const {
133 return oldTy == SByteTy || oldTy == ShortTy ||
134 oldTy == IntTy || oldTy == LongTy;
135 }
136
137 bool isUnsigned() const {
138 return oldTy == UByteTy || oldTy == UShortTy ||
139 oldTy == UIntTy || oldTy == ULongTy;
140 }
141 bool isSignless() const { return !isSigned() && !isUnsigned(); }
142 bool isInteger() const { return isSigned() || isUnsigned(); }
143 bool isIntegral() const { return oldTy == BoolTy || isInteger(); }
144 bool isFloatingPoint() const { return oldTy == DoubleTy || oldTy == FloatTy; }
145 bool isPacked() const { return oldTy == PackedTy; }
146 bool isPointer() const { return oldTy == PointerTy; }
147 bool isStruct() const { return oldTy == StructTy || oldTy == PackedStructTy; }
148 bool isArray() const { return oldTy == ArrayTy; }
149 bool isOther() const {
150 return !isPacked() && !isPointer() && !isFloatingPoint() && !isIntegral(); }
151 bool isFunction() const { return oldTy == FunctionTy; }
152 bool isComposite() const {
153 return isStruct() || isPointer() || isArray() || isPacked();
154 }
155
156 bool isAttributeCandidate() const {
157 return isIntegral() && getBitWidth() < 32;
158 }
159
160 bool isUnresolvedDeep() const;
161
162 unsigned getBitWidth() const;
163
164 const TypeInfo* getIndexedType(const ValueInfo* VI) const;
165
166 unsigned getNumStructElements() const {
167 return (elements ? elements->size() : 0);
168 }
169
170 const TypeInfo* getElement(unsigned idx) const {
171 if (elements)
172 if (idx < elements->size())
173 return (*elements)[idx];
174 return 0;
175 }
176
177private:
178 TypeInfo()
179 : newTy(), oldTy(UnresolvedTy), elemTy(0), resultTy(0), elements(0),
180 nelems(0) {
181 }
182
183 TypeInfo(const TypeInfo& that); // do not implement
184 TypeInfo& operator=(const TypeInfo& that); // do not implement
185
186 ~TypeInfo() { delete elements; }
187
188 struct ltfunctor
189 {
190 bool operator()(const TypeInfo* X, const TypeInfo* Y) const {
191 assert(X && "Can't compare null pointer");
192 assert(Y && "Can't compare null pointer");
193 return *X < *Y;
194 }
195 };
196
197 typedef std::set<const TypeInfo*, ltfunctor> TypeRegMap;
198
199 static const TypeInfo* add_new_type(TypeInfo* existing);
200
201 std::string newTy;
202 Types oldTy;
203 TypeInfo *elemTy;
204 TypeInfo *resultTy;
205 TypeList *elements;
206 uint64_t nelems;
207 static TypeRegMap registry;
208public:
209 typedef std::vector<const TypeInfo*> TypeVector;
210 typedef std::map<std::string,const TypeInfo*> TypeMap;
211 typedef std::map<const TypeInfo*,std::string> TypePlaneMap;
212 typedef std::map<std::string,TypePlaneMap> GlobalsTypeMap;
213 static TypeVector EnumeratedTypes;
214 static TypeMap NamedTypes;
215 static GlobalsTypeMap Globals;
216};
217
218TypeInfo::TypeRegMap TypeInfo::registry;
219TypeInfo::TypeVector TypeInfo::EnumeratedTypes;
220TypeInfo::TypeMap TypeInfo::NamedTypes;
221TypeInfo::GlobalsTypeMap TypeInfo::Globals;
Reid Spencer319a7302007-01-05 17:20:02 +0000222
223const TypeInfo* TypeInfo::get(const std::string &newType, Types oldType) {
224 TypeInfo* Ty = new TypeInfo();
225 Ty->newTy = newType;
226 Ty->oldTy = oldType;
227 return add_new_type(Ty);
228}
229
230const TypeInfo* TypeInfo::get(const std::string& newType, Types oldType,
231 const TypeInfo* eTy, const TypeInfo* rTy) {
232 TypeInfo* Ty= new TypeInfo();
233 Ty->newTy = newType;
234 Ty->oldTy = oldType;
235 Ty->elemTy = const_cast<TypeInfo*>(eTy);
236 Ty->resultTy = const_cast<TypeInfo*>(rTy);
237 return add_new_type(Ty);
238}
239
240const TypeInfo* TypeInfo::get(const std::string& newType, Types oldType,
241 const TypeInfo *eTy, uint64_t elems) {
242 TypeInfo* Ty = new TypeInfo();
243 Ty->newTy = newType;
244 Ty->oldTy = oldType;
245 Ty->elemTy = const_cast<TypeInfo*>(eTy);
246 Ty->nelems = elems;
247 return add_new_type(Ty);
248}
249
250const TypeInfo* TypeInfo::get(const std::string& newType, Types oldType,
251 TypeList* TL) {
252 TypeInfo* Ty = new TypeInfo();
253 Ty->newTy = newType;
254 Ty->oldTy = oldType;
255 Ty->elements = TL;
256 return add_new_type(Ty);
257}
258
259const TypeInfo* TypeInfo::get(const std::string& newType, const TypeInfo* resTy,
260 TypeList* TL) {
261 TypeInfo* Ty = new TypeInfo();
262 Ty->newTy = newType;
263 Ty->oldTy = FunctionTy;
264 Ty->resultTy = const_cast<TypeInfo*>(resTy);
265 Ty->elements = TL;
266 return add_new_type(Ty);
267}
268
269const TypeInfo* TypeInfo::resolve() const {
270 if (isUnresolved()) {
Reid Spencerf8383de2007-01-06 06:04:32 +0000271 if (getNewTy()[0] == '%' && isdigit(newTy[1])) {
272 unsigned ref = atoi(&((newTy.c_str())[1])); // skip the %
Reid Spencereff838e2007-01-03 23:45:42 +0000273 if (ref < EnumeratedTypes.size()) {
Reid Spencer319a7302007-01-05 17:20:02 +0000274 return EnumeratedTypes[ref];
Reid Spencereff838e2007-01-03 23:45:42 +0000275 } else {
276 std::string msg("Can't resolve numbered type: ");
Reid Spencer319a7302007-01-05 17:20:02 +0000277 msg += getNewTy();
Reid Spencereff838e2007-01-03 23:45:42 +0000278 yyerror(msg.c_str());
279 }
Reid Spencer78720742006-12-02 20:21:22 +0000280 } else {
Reid Spencer30d0c582007-01-15 00:26:18 +0000281 TypeInfo::TypeMap::iterator I = NamedTypes.find(newTy);
Reid Spencereff838e2007-01-03 23:45:42 +0000282 if (I != NamedTypes.end()) {
Reid Spencer319a7302007-01-05 17:20:02 +0000283 return I->second;
Reid Spencereff838e2007-01-03 23:45:42 +0000284 } else {
285 std::string msg("Cannot resolve type: ");
Reid Spencer319a7302007-01-05 17:20:02 +0000286 msg += getNewTy();
Reid Spencereff838e2007-01-03 23:45:42 +0000287 yyerror(msg.c_str());
288 }
Reid Spencera50d5962006-12-02 04:11:07 +0000289 }
Reid Spencer280d8012006-12-01 23:40:53 +0000290 }
Reid Spencera50d5962006-12-02 04:11:07 +0000291 // otherwise its already resolved.
Reid Spencer319a7302007-01-05 17:20:02 +0000292 return this;
293}
294
295bool TypeInfo::operator<(const TypeInfo& that) const {
296 if (this == &that)
297 return false;
298 if (oldTy != that.oldTy)
299 return oldTy < that.oldTy;
300 switch (oldTy) {
301 case UpRefTy: {
302 unsigned thisUp = this->getUpRefNum();
303 unsigned thatUp = that.getUpRefNum();
304 return thisUp < thatUp;
305 }
306 case PackedTy:
307 case ArrayTy:
308 if (this->nelems != that.nelems)
309 return nelems < that.nelems;
310 case PointerTy: {
311 const TypeInfo* thisTy = this->elemTy;
312 const TypeInfo* thatTy = that.elemTy;
313 return *thisTy < *thatTy;
314 }
315 case FunctionTy: {
316 const TypeInfo* thisTy = this->resultTy;
317 const TypeInfo* thatTy = that.resultTy;
318 if (!thisTy->sameOldTyAs(thatTy))
319 return *thisTy < *thatTy;
320 /* FALL THROUGH */
321 }
322 case StructTy:
323 case PackedStructTy: {
324 if (elements->size() != that.elements->size())
325 return elements->size() < that.elements->size();
326 for (unsigned i = 0; i < elements->size(); i++) {
327 const TypeInfo* thisTy = (*this->elements)[i];
328 const TypeInfo* thatTy = (*that.elements)[i];
329 if (!thisTy->sameOldTyAs(thatTy))
330 return *thisTy < *thatTy;
331 }
332 break;
333 }
334 case UnresolvedTy:
335 return this->newTy < that.newTy;
336 default:
337 break;
338 }
339 return false;
340}
341
342bool TypeInfo::sameOldTyAs(const TypeInfo* that) const {
343 if (that == 0)
344 return false;
345 if ( this == that )
346 return true;
347 if (oldTy != that->oldTy)
348 return false;
349 switch (oldTy) {
350 case PackedTy:
351 case ArrayTy:
352 if (nelems != that->nelems)
353 return false;
354 /* FALL THROUGH */
355 case PointerTy: {
356 const TypeInfo* thisTy = this->elemTy;
357 const TypeInfo* thatTy = that->elemTy;
358 return thisTy->sameOldTyAs(thatTy);
359 }
360 case FunctionTy: {
361 const TypeInfo* thisTy = this->resultTy;
362 const TypeInfo* thatTy = that->resultTy;
363 if (!thisTy->sameOldTyAs(thatTy))
364 return false;
365 /* FALL THROUGH */
366 }
367 case StructTy:
368 case PackedStructTy: {
369 if (elements->size() != that->elements->size())
370 return false;
371 for (unsigned i = 0; i < elements->size(); i++) {
372 const TypeInfo* thisTy = (*this->elements)[i];
373 const TypeInfo* thatTy = (*that->elements)[i];
374 if (!thisTy->sameOldTyAs(thatTy))
375 return false;
376 }
377 return true;
378 }
379 case UnresolvedTy:
380 return this->newTy == that->newTy;
381 default:
382 return true; // for all others oldTy == that->oldTy is sufficient
383 }
384 return true;
385}
386
387bool TypeInfo::isUnresolvedDeep() const {
388 switch (oldTy) {
389 case UnresolvedTy:
390 return true;
391 case PackedTy:
392 case ArrayTy:
393 case PointerTy:
394 return elemTy->isUnresolvedDeep();
395 case PackedStructTy:
396 case StructTy:
397 for (unsigned i = 0; i < elements->size(); i++)
398 if ((*elements)[i]->isUnresolvedDeep())
399 return true;
400 return false;
401 default:
402 return false;
403 }
404}
405
406unsigned TypeInfo::getBitWidth() const {
407 switch (oldTy) {
408 default:
409 case LabelTy:
410 case VoidTy : return 0;
411 case BoolTy : return 1;
412 case SByteTy: case UByteTy : return 8;
413 case ShortTy: case UShortTy : return 16;
414 case IntTy: case UIntTy: case FloatTy: return 32;
415 case LongTy: case ULongTy: case DoubleTy : return 64;
416 case PointerTy: return SizeOfPointer; // global var
417 case PackedTy:
418 case ArrayTy:
419 return nelems * elemTy->getBitWidth();
420 case StructTy:
421 case PackedStructTy: {
422 uint64_t size = 0;
423 for (unsigned i = 0; i < elements->size(); i++) {
424 size += (*elements)[i]->getBitWidth();
425 }
426 return size;
427 }
428 }
429}
430
Reid Spencer30d0c582007-01-15 00:26:18 +0000431const TypeInfo* TypeInfo::getIndexedType(const ValueInfo* VI) const {
Reid Spencer319a7302007-01-05 17:20:02 +0000432 if (isStruct()) {
Reid Spencer30d0c582007-01-15 00:26:18 +0000433 if (VI->isConstant() && VI->type->isInteger()) {
434 size_t pos = VI->val->find(' ') + 1;
435 if (pos < VI->val->size()) {
436 uint64_t idx = atoi(VI->val->substr(pos).c_str());
Reid Spencer319a7302007-01-05 17:20:02 +0000437 return (*elements)[idx];
438 } else {
439 yyerror("Invalid value for constant integer");
440 return 0;
441 }
442 } else {
443 yyerror("Structure requires constant index");
444 return 0;
445 }
446 }
447 if (isArray() || isPacked() || isPointer())
448 return elemTy;
449 yyerror("Invalid type for getIndexedType");
450 return 0;
451}
452
Reid Spencerf8383de2007-01-06 06:04:32 +0000453void TypeInfo::getSignedness(unsigned &sNum, unsigned &uNum,
454 UpRefStack& stack) const {
455 switch (oldTy) {
456 default:
457 case OpaqueTy: case LabelTy: case VoidTy: case BoolTy:
458 case FloatTy : case DoubleTy: case UpRefTy:
459 return;
460 case SByteTy: case ShortTy: case LongTy: case IntTy:
461 sNum++;
462 return;
463 case UByteTy: case UShortTy: case UIntTy: case ULongTy:
464 uNum++;
465 return;
466 case PointerTy:
467 case PackedTy:
468 case ArrayTy:
469 stack.push_back(this);
470 elemTy->getSignedness(sNum, uNum, stack);
471 return;
472 case StructTy:
473 case PackedStructTy: {
474 stack.push_back(this);
475 for (unsigned i = 0; i < elements->size(); i++) {
476 (*elements)[i]->getSignedness(sNum, uNum, stack);
477 }
478 return;
479 }
480 case UnresolvedTy: {
481 const TypeInfo* Ty = this->resolve();
482 // Let's not recurse.
483 UpRefStack::const_iterator I = stack.begin(), E = stack.end();
484 for ( ; I != E && *I != Ty; ++I)
485 ;
486 if (I == E)
487 Ty->getSignedness(sNum, uNum, stack);
488 return;
489 }
490 }
491}
492
493std::string AddSuffix(const std::string& Name, const std::string& Suffix) {
494 if (Name[Name.size()-1] == '"') {
495 std::string Result = Name;
496 Result.insert(Result.size()-1, Suffix);
497 return Result;
498 }
499 return Name + Suffix;
500}
501
502std::string TypeInfo::makeUniqueName(const std::string& BaseName) const {
503 if (BaseName == "\"alloca point\"")
504 return BaseName;
505 switch (oldTy) {
506 default:
507 break;
508 case OpaqueTy: case LabelTy: case VoidTy: case BoolTy: case UpRefTy:
509 case FloatTy : case DoubleTy: case UnresolvedTy:
510 return BaseName;
511 case SByteTy: case ShortTy: case LongTy: case IntTy:
512 return AddSuffix(BaseName, ".s");
513 case UByteTy: case UShortTy: case UIntTy: case ULongTy:
514 return AddSuffix(BaseName, ".u");
515 }
516
517 unsigned uNum = 0, sNum = 0;
518 std::string Suffix;
519 switch (oldTy) {
520 case PointerTy:
521 case PackedTy:
522 case ArrayTy: {
523 TypeInfo::UpRefStack stack;
524 elemTy->resolve()->getSignedness(sNum, uNum, stack);
525 break;
526 }
527 case StructTy:
528 case PackedStructTy: {
529 for (unsigned i = 0; i < elements->size(); i++) {
530 TypeInfo::UpRefStack stack;
531 (*elements)[i]->resolve()->getSignedness(sNum, uNum, stack);
532 }
533 break;
534 }
535 default:
536 assert(0 && "Invalid Type");
537 break;
538 }
539
540 if (sNum == 0 && uNum == 0)
541 return BaseName;
542
543 switch (oldTy) {
544 default: Suffix += ".nada"; break;
545 case PointerTy: Suffix += ".pntr"; break;
546 case PackedTy: Suffix += ".pckd"; break;
547 case ArrayTy: Suffix += ".arry"; break;
548 case StructTy: Suffix += ".strc"; break;
549 case PackedStructTy: Suffix += ".pstr"; break;
550 }
551
552 Suffix += ".s" + llvm::utostr(sNum);
553 Suffix += ".u" + llvm::utostr(uNum);
554 return AddSuffix(BaseName, Suffix);
555}
556
Reid Spencer319a7302007-01-05 17:20:02 +0000557TypeInfo& TypeInfo::operator=(const TypeInfo& that) {
558 oldTy = that.oldTy;
559 nelems = that.nelems;
560 newTy = that.newTy;
561 elemTy = that.elemTy;
562 resultTy = that.resultTy;
563 if (that.elements) {
564 elements = new TypeList(that.elements->size());
565 *elements = *that.elements;
566 } else {
567 elements = 0;
568 }
569 return *this;
570}
571
572const TypeInfo* TypeInfo::add_new_type(TypeInfo* newTy) {
573 TypeRegMap::iterator I = registry.find(newTy);
574 if (I != registry.end()) {
575 delete newTy;
576 return *I;
577 }
578 registry.insert(newTy);
579 return newTy;
Reid Spencer280d8012006-12-01 23:40:53 +0000580}
581
Reid Spencer30d0c582007-01-15 00:26:18 +0000582/// This type is used to keep track of the signedness of constants.
583struct ConstInfo {
584 std::string *cnst;
585 const TypeInfo *type;
586 ~ConstInfo() { delete cnst; }
587};
588
589/// This variable provides a counter for unique names. It is used in various
590/// productions to ensure a unique name is generated.
591static uint64_t UniqueNameCounter = 1;
592
593// This is set when a DECLARE keyword is recognized so that subsequent parsing
594// of a function prototype can know if its a declaration or definition.
595static bool isDeclare = false;
596
597// This bool is used to communicate between the InstVal and Inst rules about
598// whether or not a cast should be deleted. When the flag is set, InstVal has
599// determined that the cast is a candidate. However, it can only be deleted if
600// the value being casted is the same value name as the instruction. The Inst
601// rule makes that comparison if the flag is set and comments out the
602// instruction if they match.
603static bool deleteUselessCastFlag = false;
604static std::string* deleteUselessCastName = 0;
605
606
607
608const char* getCastOpcode(std::string& Source, const TypeInfo* SrcTy,
609 const TypeInfo* DstTy) {
Reid Spencer52402b02007-01-02 05:45:11 +0000610 unsigned SrcBits = SrcTy->getBitWidth();
611 unsigned DstBits = DstTy->getBitWidth();
Reid Spencere77e35e2006-12-01 20:26:20 +0000612 const char* opcode = "bitcast";
613 // Run through the possibilities ...
Reid Spencer52402b02007-01-02 05:45:11 +0000614 if (DstTy->isIntegral()) { // Casting to integral
615 if (SrcTy->isIntegral()) { // Casting from integral
Reid Spencere77e35e2006-12-01 20:26:20 +0000616 if (DstBits < SrcBits)
617 opcode = "trunc";
618 else if (DstBits > SrcBits) { // its an extension
Reid Spencer52402b02007-01-02 05:45:11 +0000619 if (SrcTy->isSigned())
Reid Spencere77e35e2006-12-01 20:26:20 +0000620 opcode ="sext"; // signed -> SEXT
621 else
622 opcode = "zext"; // unsigned -> ZEXT
623 } else {
624 opcode = "bitcast"; // Same size, No-op cast
625 }
Reid Spencer52402b02007-01-02 05:45:11 +0000626 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
627 if (DstTy->isSigned())
Reid Spencere77e35e2006-12-01 20:26:20 +0000628 opcode = "fptosi"; // FP -> sint
629 else
630 opcode = "fptoui"; // FP -> uint
Reid Spencer52402b02007-01-02 05:45:11 +0000631 } else if (SrcTy->isPacked()) {
632 assert(DstBits == SrcTy->getBitWidth() &&
Reid Spencere77e35e2006-12-01 20:26:20 +0000633 "Casting packed to integer of different width");
634 opcode = "bitcast"; // same size, no-op cast
635 } else {
Reid Spencer52402b02007-01-02 05:45:11 +0000636 assert(SrcTy->isPointer() &&
Reid Spencere77e35e2006-12-01 20:26:20 +0000637 "Casting from a value that is not first-class type");
638 opcode = "ptrtoint"; // ptr -> int
639 }
Reid Spencer52402b02007-01-02 05:45:11 +0000640 } else if (DstTy->isFloatingPoint()) { // Casting to floating pt
641 if (SrcTy->isIntegral()) { // Casting from integral
642 if (SrcTy->isSigned())
Reid Spencere77e35e2006-12-01 20:26:20 +0000643 opcode = "sitofp"; // sint -> FP
644 else
645 opcode = "uitofp"; // uint -> FP
Reid Spencer52402b02007-01-02 05:45:11 +0000646 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencere77e35e2006-12-01 20:26:20 +0000647 if (DstBits < SrcBits) {
648 opcode = "fptrunc"; // FP -> smaller FP
649 } else if (DstBits > SrcBits) {
650 opcode = "fpext"; // FP -> larger FP
651 } else {
652 opcode ="bitcast"; // same size, no-op cast
653 }
Reid Spencer52402b02007-01-02 05:45:11 +0000654 } else if (SrcTy->isPacked()) {
655 assert(DstBits == SrcTy->getBitWidth() &&
Reid Spencere77e35e2006-12-01 20:26:20 +0000656 "Casting packed to floating point of different width");
657 opcode = "bitcast"; // same size, no-op cast
658 } else {
659 assert(0 && "Casting pointer or non-first class to float");
660 }
Reid Spencer52402b02007-01-02 05:45:11 +0000661 } else if (DstTy->isPacked()) {
662 if (SrcTy->isPacked()) {
663 assert(DstTy->getBitWidth() == SrcTy->getBitWidth() &&
Reid Spencere77e35e2006-12-01 20:26:20 +0000664 "Casting packed to packed of different widths");
665 opcode = "bitcast"; // packed -> packed
Reid Spencer52402b02007-01-02 05:45:11 +0000666 } else if (DstTy->getBitWidth() == SrcBits) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000667 opcode = "bitcast"; // float/int -> packed
668 } else {
669 assert(!"Illegal cast to packed (wrong type or size)");
670 }
Reid Spencer52402b02007-01-02 05:45:11 +0000671 } else if (DstTy->isPointer()) {
672 if (SrcTy->isPointer()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000673 opcode = "bitcast"; // ptr -> ptr
Reid Spencer52402b02007-01-02 05:45:11 +0000674 } else if (SrcTy->isIntegral()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000675 opcode = "inttoptr"; // int -> ptr
676 } else {
Reid Spencera50d5962006-12-02 04:11:07 +0000677 assert(!"Casting invalid type to pointer");
Reid Spencere77e35e2006-12-01 20:26:20 +0000678 }
679 } else {
680 assert(!"Casting to type that is not first-class");
681 }
682 return opcode;
683}
684
Reid Spencer30d0c582007-01-15 00:26:18 +0000685std::string getCastUpgrade(const std::string& Src, const TypeInfo* SrcTy,
686 const TypeInfo* DstTy, bool isConst) {
Reid Spencera50d5962006-12-02 04:11:07 +0000687 std::string Result;
688 std::string Source = Src;
Reid Spencer52402b02007-01-02 05:45:11 +0000689 if (SrcTy->isFloatingPoint() && DstTy->isPointer()) {
Reid Spencera50d5962006-12-02 04:11:07 +0000690 // fp -> ptr cast is no longer supported but we must upgrade this
691 // by doing a double cast: fp -> int -> ptr
692 if (isConst)
Reid Spencer71d2ec92006-12-31 06:02:26 +0000693 Source = "i64 fptoui(" + Source + " to i64)";
Reid Spencera50d5962006-12-02 04:11:07 +0000694 else {
Reid Spencer30d0c582007-01-15 00:26:18 +0000695 *O << " %cast_upgrade" << UniqueNameCounter++ << " = fptoui "
696 << Source << " to i64\n";
697 Source = "i64 %cast_upgrade" + llvm::utostr(UniqueNameCounter);
Reid Spencera50d5962006-12-02 04:11:07 +0000698 }
699 // Update the SrcTy for the getCastOpcode call below
Reid Spencer319a7302007-01-05 17:20:02 +0000700 SrcTy = TypeInfo::get("i64", ULongTy);
Reid Spencer52402b02007-01-02 05:45:11 +0000701 } else if (DstTy->isBool()) {
702 // cast type %x to bool was previously defined as setne type %x, null
703 // The cast semantic is now to truncate, not compare so we must retain
704 // the original intent by replacing the cast with a setne
705 const char* comparator = SrcTy->isPointer() ? ", null" :
706 (SrcTy->isFloatingPoint() ? ", 0.0" :
707 (SrcTy->isBool() ? ", false" : ", 0"));
708 const char* compareOp = SrcTy->isFloatingPoint() ? "fcmp one " : "icmp ne ";
Reid Spencer187ccf82006-12-09 16:57:22 +0000709 if (isConst) {
710 Result = "(" + Source + comparator + ")";
711 Result = compareOp + Result;
712 } else
713 Result = compareOp + Source + comparator;
Reid Spencera50d5962006-12-02 04:11:07 +0000714 return Result; // skip cast processing below
715 }
Reid Spencer319a7302007-01-05 17:20:02 +0000716 SrcTy = SrcTy->resolve();
717 DstTy = DstTy->resolve();
Reid Spencera50d5962006-12-02 04:11:07 +0000718 std::string Opcode(getCastOpcode(Source, SrcTy, DstTy));
719 if (isConst)
Reid Spencer52402b02007-01-02 05:45:11 +0000720 Result += Opcode + "( " + Source + " to " + DstTy->getNewTy() + ")";
Reid Spencera50d5962006-12-02 04:11:07 +0000721 else
Reid Spencer52402b02007-01-02 05:45:11 +0000722 Result += Opcode + " " + Source + " to " + DstTy->getNewTy();
Reid Spencera50d5962006-12-02 04:11:07 +0000723 return Result;
724}
725
Reid Spencer319a7302007-01-05 17:20:02 +0000726const char* getDivRemOpcode(const std::string& opcode, const TypeInfo* TI) {
Reid Spencer78720742006-12-02 20:21:22 +0000727 const char* op = opcode.c_str();
Reid Spencer319a7302007-01-05 17:20:02 +0000728 const TypeInfo* Ty = TI->resolve();
Reid Spencer52402b02007-01-02 05:45:11 +0000729 if (Ty->isPacked())
730 Ty = Ty->getElementType();
Reid Spencer78720742006-12-02 20:21:22 +0000731 if (opcode == "div")
Reid Spencer52402b02007-01-02 05:45:11 +0000732 if (Ty->isFloatingPoint())
Reid Spencer78720742006-12-02 20:21:22 +0000733 op = "fdiv";
Reid Spencer52402b02007-01-02 05:45:11 +0000734 else if (Ty->isUnsigned())
Reid Spencer78720742006-12-02 20:21:22 +0000735 op = "udiv";
Reid Spencer52402b02007-01-02 05:45:11 +0000736 else if (Ty->isSigned())
Reid Spencer78720742006-12-02 20:21:22 +0000737 op = "sdiv";
738 else
739 yyerror("Invalid type for div instruction");
740 else if (opcode == "rem")
Reid Spencer52402b02007-01-02 05:45:11 +0000741 if (Ty->isFloatingPoint())
Reid Spencer78720742006-12-02 20:21:22 +0000742 op = "frem";
Reid Spencer52402b02007-01-02 05:45:11 +0000743 else if (Ty->isUnsigned())
Reid Spencer78720742006-12-02 20:21:22 +0000744 op = "urem";
Reid Spencer52402b02007-01-02 05:45:11 +0000745 else if (Ty->isSigned())
Reid Spencer78720742006-12-02 20:21:22 +0000746 op = "srem";
747 else
748 yyerror("Invalid type for rem instruction");
749 return op;
750}
Reid Spencer229e9362006-12-02 22:14:11 +0000751
Reid Spencer30d0c582007-01-15 00:26:18 +0000752std::string getCompareOp(const std::string& setcc, const TypeInfo* TI) {
Reid Spencer229e9362006-12-02 22:14:11 +0000753 assert(setcc.length() == 5);
754 char cc1 = setcc[3];
755 char cc2 = setcc[4];
756 assert(cc1 == 'e' || cc1 == 'n' || cc1 == 'l' || cc1 == 'g');
757 assert(cc2 == 'q' || cc2 == 'e' || cc2 == 'e' || cc2 == 't');
758 std::string result("xcmp xxx");
759 result[6] = cc1;
760 result[7] = cc2;
Reid Spencer52402b02007-01-02 05:45:11 +0000761 if (TI->isFloatingPoint()) {
Reid Spencer229e9362006-12-02 22:14:11 +0000762 result[0] = 'f';
Reid Spencere4d87aa2006-12-23 06:05:41 +0000763 result[5] = 'o';
Reid Spencerf0cf1322006-12-07 04:23:03 +0000764 if (cc1 == 'n')
765 result[5] = 'u'; // NE maps to unordered
766 else
767 result[5] = 'o'; // everything else maps to ordered
Reid Spencer52402b02007-01-02 05:45:11 +0000768 } else if (TI->isIntegral() || TI->isPointer()) {
Reid Spencer229e9362006-12-02 22:14:11 +0000769 result[0] = 'i';
770 if ((cc1 == 'e' && cc2 == 'q') || (cc1 == 'n' && cc2 == 'e'))
771 result.erase(5,1);
Reid Spencer52402b02007-01-02 05:45:11 +0000772 else if (TI->isSigned())
Reid Spencer229e9362006-12-02 22:14:11 +0000773 result[5] = 's';
Reid Spencer52402b02007-01-02 05:45:11 +0000774 else if (TI->isUnsigned() || TI->isPointer() || TI->isBool())
Reid Spencer229e9362006-12-02 22:14:11 +0000775 result[5] = 'u';
776 else
777 yyerror("Invalid integral type for setcc");
778 }
779 return result;
780}
781
Reid Spencer30d0c582007-01-15 00:26:18 +0000782const TypeInfo* getFunctionReturnType(const TypeInfo* PFTy) {
Reid Spencer319a7302007-01-05 17:20:02 +0000783 PFTy = PFTy->resolve();
Reid Spencer52402b02007-01-02 05:45:11 +0000784 if (PFTy->isPointer()) {
Reid Spencer319a7302007-01-05 17:20:02 +0000785 const TypeInfo* ElemTy = PFTy->getElementType();
786 ElemTy = ElemTy->resolve();
Reid Spencer52402b02007-01-02 05:45:11 +0000787 if (ElemTy->isFunction())
Reid Spencer319a7302007-01-05 17:20:02 +0000788 return ElemTy->getResultType();
Reid Spencer52402b02007-01-02 05:45:11 +0000789 } else if (PFTy->isFunction()) {
Reid Spencer319a7302007-01-05 17:20:02 +0000790 return PFTy->getResultType();
Reid Spencer52402b02007-01-02 05:45:11 +0000791 }
Reid Spencer319a7302007-01-05 17:20:02 +0000792 return PFTy;
Reid Spencer52402b02007-01-02 05:45:11 +0000793}
794
Reid Spencer30d0c582007-01-15 00:26:18 +0000795const TypeInfo* ResolveUpReference(const TypeInfo* Ty,
796 TypeInfo::UpRefStack* stack) {
Reid Spencereff838e2007-01-03 23:45:42 +0000797 assert(Ty->isUpReference() && "Can't resolve a non-upreference");
Reid Spencer319a7302007-01-05 17:20:02 +0000798 unsigned upref = Ty->getUpRefNum();
Reid Spencereff838e2007-01-03 23:45:42 +0000799 assert(upref < stack->size() && "Invalid up reference");
800 return (*stack)[upref - stack->size() - 1];
801}
802
Reid Spencer30d0c582007-01-15 00:26:18 +0000803const TypeInfo* getGEPIndexedType(const TypeInfo* PTy, ValueList* idxs) {
Reid Spencer319a7302007-01-05 17:20:02 +0000804 const TypeInfo* Result = PTy = PTy->resolve();
Reid Spencer52402b02007-01-02 05:45:11 +0000805 assert(PTy->isPointer() && "GEP Operand is not a pointer?");
Reid Spencerf8383de2007-01-06 06:04:32 +0000806 TypeInfo::UpRefStack stack;
Reid Spencereff838e2007-01-03 23:45:42 +0000807 for (unsigned i = 0; i < idxs->size(); ++i) {
Reid Spencer52402b02007-01-02 05:45:11 +0000808 if (Result->isComposite()) {
809 Result = Result->getIndexedType((*idxs)[i]);
Reid Spencer319a7302007-01-05 17:20:02 +0000810 Result = Result->resolve();
Reid Spencereff838e2007-01-03 23:45:42 +0000811 stack.push_back(Result);
Reid Spencer52402b02007-01-02 05:45:11 +0000812 } else
813 yyerror("Invalid type for index");
814 }
Reid Spencereff838e2007-01-03 23:45:42 +0000815 // Resolve upreferences so we can return a more natural type
816 if (Result->isPointer()) {
817 if (Result->getElementType()->isUpReference()) {
818 stack.push_back(Result);
819 Result = ResolveUpReference(Result->getElementType(), &stack);
820 }
821 } else if (Result->isUpReference()) {
822 Result = ResolveUpReference(Result->getElementType(), &stack);
823 }
Reid Spencer52402b02007-01-02 05:45:11 +0000824 return Result->getPointerType();
825}
826
Reid Spencer52402b02007-01-02 05:45:11 +0000827// This function handles appending .u or .s to integer value names that
828// were previously unsigned or signed, respectively. This avoids name
829// collisions since the unsigned and signed type planes have collapsed
830// into a single signless type plane.
Reid Spencer30d0c582007-01-15 00:26:18 +0000831std::string getUniqueName(const std::string *Name, const TypeInfo* Ty,
832 bool isGlobal = false, bool isDef = false) {
Reid Spencer319a7302007-01-05 17:20:02 +0000833
Reid Spencer52402b02007-01-02 05:45:11 +0000834 // If its not a symbolic name, don't modify it, probably a constant val.
835 if ((*Name)[0] != '%' && (*Name)[0] != '"')
836 return *Name;
Reid Spencer319a7302007-01-05 17:20:02 +0000837
Reid Spencer52402b02007-01-02 05:45:11 +0000838 // If its a numeric reference, just leave it alone.
839 if (isdigit((*Name)[1]))
840 return *Name;
841
842 // Resolve the type
Reid Spencer319a7302007-01-05 17:20:02 +0000843 Ty = Ty->resolve();
844
845 // If its a global name, get its uniquified name, if any
Reid Spencer30d0c582007-01-15 00:26:18 +0000846 TypeInfo::GlobalsTypeMap::iterator GI = TypeInfo::Globals.find(*Name);
847 if (GI != TypeInfo::Globals.end()) {
848 TypeInfo::TypePlaneMap::iterator TPI = GI->second.begin();
849 TypeInfo::TypePlaneMap::iterator TPE = GI->second.end();
Reid Spencer319a7302007-01-05 17:20:02 +0000850 for ( ; TPI != TPE ; ++TPI) {
851 if (TPI->first->sameNewTyAs(Ty))
852 return TPI->second;
853 }
854 }
855
856 if (isGlobal) {
857 // We didn't find a global name, but if its supposed to be global then all
858 // we can do is return the name. This is probably a forward reference of a
859 // global value that hasn't been defined yet. Since we have no definition
860 // we don't know its linkage class. Just assume its an external and the name
861 // shouldn't change.
862 return *Name;
863 }
Reid Spencer52402b02007-01-02 05:45:11 +0000864
865 // Default the result to the current name
Reid Spencerf8383de2007-01-06 06:04:32 +0000866 std::string Result = Ty->makeUniqueName(*Name);
Reid Spencer52402b02007-01-02 05:45:11 +0000867
Reid Spencer52402b02007-01-02 05:45:11 +0000868 return Result;
869}
870
Reid Spencer319a7302007-01-05 17:20:02 +0000871std::string getGlobalName(const std::string* Name, const std::string Linkage,
872 const TypeInfo* Ty, bool isConstant) {
873 // Default to given name
874 std::string Result = *Name;
875 // Look up the name in the Globals Map
Reid Spencer30d0c582007-01-15 00:26:18 +0000876 TypeInfo::GlobalsTypeMap::iterator GI = TypeInfo::Globals.find(*Name);
Reid Spencer319a7302007-01-05 17:20:02 +0000877 // Did we see this global name before?
Reid Spencer30d0c582007-01-15 00:26:18 +0000878 if (GI != TypeInfo::Globals.end()) {
Reid Spencer319a7302007-01-05 17:20:02 +0000879 if (Ty->isUnresolvedDeep()) {
880 // The Gval's type is unresolved. Consequently, we can't disambiguate it
881 // by type. We'll just change its name and emit a warning.
882 warning("Cannot disambiguate global value '" + *Name +
883 "' because type '" + Ty->getNewTy() + "'is unresolved.\n");
884 Result = *Name + ".unique";
885 UniqueNameCounter++;
886 Result += llvm::utostr(UniqueNameCounter);
887 return Result;
888 } else {
Reid Spencer30d0c582007-01-15 00:26:18 +0000889 TypeInfo::TypePlaneMap::iterator TPI = GI->second.find(Ty);
Reid Spencer319a7302007-01-05 17:20:02 +0000890 if (TPI != GI->second.end()) {
891 // We found an existing name of the same old type. This isn't allowed
892 // in LLVM 2.0. Consequently, we must alter the name of the global so it
893 // can at least compile. References to the global will yield the first
894 // definition, which is okay. We also must warn about this.
895 Result = *Name + ".unique";
896 UniqueNameCounter++;
897 Result += llvm::utostr(UniqueNameCounter);
898 warning(std::string("Global variable '") + *Name + "' was renamed to '"+
899 Result + "'");
900 } else {
901 // There isn't an existing definition for this name according to the
902 // old types. Now search the TypePlanMap for types with the same new
903 // name.
Reid Spencer30d0c582007-01-15 00:26:18 +0000904 TypeInfo::TypePlaneMap::iterator TPI = GI->second.begin();
905 TypeInfo::TypePlaneMap::iterator TPE = GI->second.end();
Reid Spencer319a7302007-01-05 17:20:02 +0000906 for ( ; TPI != TPE; ++TPI) {
907 if (TPI->first->sameNewTyAs(Ty)) {
908 // The new types are the same but the old types are different so
909 // this is a global name collision resulting from type planes
910 // collapsing.
911 if (Linkage == "external" || Linkage == "dllimport" ||
912 Linkage == "extern_weak" || Linkage == "") {
913 // The linkage of this gval is external so we can't reliably
914 // rename it because it could potentially create a linking
915 // problem. However, we can't leave the name conflict in the
916 // output either or it won't assemble with LLVM 2.0. So, all we
917 // can do is rename this one to something unique and emit a
918 // warning about the problem.
919 Result = *Name + ".unique";
920 UniqueNameCounter++;
921 Result += llvm::utostr(UniqueNameCounter);
922 warning("Renaming global value '" + *Name + "' to '" + Result +
923 "' may cause linkage errors.");
924 return Result;
925 } else {
926 // Its linkage is internal and its type is known so we can
927 // disambiguate the name collision successfully based on the type.
928 Result = getUniqueName(Name, Ty);
929 TPI->second = Result;
930 return Result;
931 }
932 }
933 }
934 // We didn't find an entry in the type plane with the same new type and
935 // the old types differ so this is a new type plane for this global
936 // variable. We just fall through to the logic below which inserts
937 // the global.
938 }
939 }
940 }
941
942 // Its a new global name, if it is external we can't change it
943 if (isConstant || Linkage == "external" || Linkage == "dllimport" ||
944 Linkage == "extern_weak" || Linkage == "") {
Reid Spencer30d0c582007-01-15 00:26:18 +0000945 TypeInfo::Globals[Result][Ty] = Result;
Reid Spencer319a7302007-01-05 17:20:02 +0000946 return Result;
947 }
948
949 // Its a new global name, and it is internal, change the name to make it
950 // unique for its type.
951 // Result = getUniqueName(Name, Ty);
Reid Spencer30d0c582007-01-15 00:26:18 +0000952 TypeInfo::Globals[*Name][Ty] = Result;
Reid Spencer319a7302007-01-05 17:20:02 +0000953 return Result;
954}
Reid Spencer30d0c582007-01-15 00:26:18 +0000955
956} // End anonymous namespace
957
958// This function is used by the Lexer to create a TypeInfo. It can't be
959// in the anonymous namespace.
960const TypeInfo* getTypeInfo(const std::string& newTy, Types oldTy) {
961 return TypeInfo::get(newTy, oldTy);
962}
963
Reid Spencere7c3c602006-11-30 06:36:44 +0000964%}
965
Reid Spencerf0cf1322006-12-07 04:23:03 +0000966// %file-prefix="UpgradeParser"
Reid Spencere77e35e2006-12-01 20:26:20 +0000967
968%union {
969 std::string* String;
Reid Spencer319a7302007-01-05 17:20:02 +0000970 const TypeInfo* Type;
Reid Spencer30d0c582007-01-15 00:26:18 +0000971 ValueInfo* Value;
972 ConstInfo* Const;
Reid Spencerf8483652006-12-02 15:16:01 +0000973 ValueList* ValList;
Reid Spencer52402b02007-01-02 05:45:11 +0000974 TypeList* TypeVec;
Reid Spencere77e35e2006-12-01 20:26:20 +0000975}
976
Reid Spencerf2d55322006-12-01 21:52:30 +0000977%token <Type> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
Reid Spencera50d5962006-12-02 04:11:07 +0000978%token <Type> FLOAT DOUBLE LABEL
979%token <String> OPAQUE ESINT64VAL EUINT64VAL SINTVAL UINTVAL FPVAL
Reid Spencerf2d55322006-12-01 21:52:30 +0000980%token <String> NULL_TOK UNDEF ZEROINITIALIZER TRUETOK FALSETOK
Reid Spencere77e35e2006-12-01 20:26:20 +0000981%token <String> TYPE VAR_ID LABELSTR STRINGCONSTANT
982%token <String> IMPLEMENTATION BEGINTOK ENDTOK
Reid Spencer71d2ec92006-12-31 06:02:26 +0000983%token <String> DECLARE GLOBAL CONSTANT SECTION VOLATILE
Reid Spencere77e35e2006-12-01 20:26:20 +0000984%token <String> TO DOTDOTDOT CONST INTERNAL LINKONCE WEAK
985%token <String> DLLIMPORT DLLEXPORT EXTERN_WEAK APPENDING
Reid Spencerc4d96252007-01-13 00:03:30 +0000986%token <String> EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
Reid Spencer78720742006-12-02 20:21:22 +0000987%token <String> ALIGN UNINITIALIZED
Reid Spencere77e35e2006-12-01 20:26:20 +0000988%token <String> DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
989%token <String> CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
990%token <String> X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
991%token <String> DATALAYOUT
Reid Spencer78720742006-12-02 20:21:22 +0000992%token <String> RET BR SWITCH INVOKE EXCEPT UNWIND UNREACHABLE
993%token <String> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM AND OR XOR
Reid Spencere77e35e2006-12-01 20:26:20 +0000994%token <String> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
Reid Spencer229e9362006-12-02 22:14:11 +0000995%token <String> ICMP FCMP EQ NE SLT SGT SLE SGE OEQ ONE OLT OGT OLE OGE
996%token <String> ORD UNO UEQ UNE ULT UGT ULE UGE
Reid Spencere77e35e2006-12-01 20:26:20 +0000997%token <String> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Reid Spencerf7bde222006-12-01 22:26:37 +0000998%token <String> PHI_TOK SELECT SHL SHR ASHR LSHR VAARG
Reid Spencere77e35e2006-12-01 20:26:20 +0000999%token <String> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Reid Spencerfcb5df82006-12-01 22:34:43 +00001000%token <String> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI UITOFP SITOFP
1001%token <String> PTRTOINT INTTOPTR BITCAST
Reid Spencere77e35e2006-12-01 20:26:20 +00001002
1003%type <String> OptAssign OptLinkage OptCallingConv OptAlign OptCAlign
1004%type <String> SectionString OptSection GlobalVarAttributes GlobalVarAttribute
Reid Spencer52402b02007-01-02 05:45:11 +00001005%type <String> ConstExpr DefinitionList
Reid Spencere77e35e2006-12-01 20:26:20 +00001006%type <String> ConstPool TargetDefinition LibrariesDefinition LibList OptName
1007%type <String> ArgVal ArgListH ArgList FunctionHeaderH BEGIN FunctionHeader END
Reid Spencer52402b02007-01-02 05:45:11 +00001008%type <String> Function FunctionProto BasicBlock
1009%type <String> InstructionList BBTerminatorInst JumpTable Inst
1010%type <String> OptTailCall OptVolatile Unwind
1011%type <String> SymbolicValueRef OptSideEffect GlobalType
Reid Spencere77e35e2006-12-01 20:26:20 +00001012%type <String> FnDeclareLinkage BasicBlockList BigOrLittle AsmBlock
Reid Spencer78720742006-12-02 20:21:22 +00001013%type <String> Name ConstValueRef ConstVector External
Reid Spencer57f28f92006-12-03 07:10:26 +00001014%type <String> ShiftOps SetCondOps LogicalOps ArithmeticOps CastOps
1015%type <String> IPredicates FPredicates
Reid Spencere77e35e2006-12-01 20:26:20 +00001016
Reid Spencerf8483652006-12-02 15:16:01 +00001017%type <ValList> ValueRefList ValueRefListE IndexList
Reid Spencer52402b02007-01-02 05:45:11 +00001018%type <TypeVec> TypeListI ArgTypeListI
Reid Spencere77e35e2006-12-01 20:26:20 +00001019
1020%type <Type> IntType SIntType UIntType FPType TypesV Types
1021%type <Type> PrimType UpRTypesV UpRTypes
1022
Reid Spencerf2d55322006-12-01 21:52:30 +00001023%type <String> IntVal EInt64Val
1024%type <Const> ConstVal
Reid Spencere77e35e2006-12-01 20:26:20 +00001025
Reid Spencer52402b02007-01-02 05:45:11 +00001026%type <Value> ValueRef ResolvedVal InstVal PHIList MemoryInst
Reid Spencere7c3c602006-11-30 06:36:44 +00001027
1028%start Module
1029
1030%%
1031
1032// Handle constant integer size restriction and conversion...
Reid Spencerf2d55322006-12-01 21:52:30 +00001033IntVal : SINTVAL | UINTVAL ;
Reid Spencere77e35e2006-12-01 20:26:20 +00001034EInt64Val : ESINT64VAL | EUINT64VAL;
Reid Spencere7c3c602006-11-30 06:36:44 +00001035
1036// Operations that are notably excluded from this list include:
1037// RET, BR, & SWITCH because they end basic blocks and are treated specially.
Reid Spencer78720742006-12-02 20:21:22 +00001038ArithmeticOps: ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV
1039 | REM | UREM | SREM | FREM;
Reid Spencere7c3c602006-11-30 06:36:44 +00001040LogicalOps : AND | OR | XOR;
1041SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Reid Spencer57f28f92006-12-03 07:10:26 +00001042IPredicates : EQ | NE | SLT | SGT | SLE | SGE | ULT | UGT | ULE | UGE;
1043FPredicates : OEQ | ONE | OLT | OGT | OLE | OGE | ORD | UNO | UEQ | UNE
1044 | ULT | UGT | ULE | UGE | TRUETOK | FALSETOK;
Reid Spencerf7bde222006-12-01 22:26:37 +00001045ShiftOps : SHL | SHR | ASHR | LSHR;
Reid Spencerfcb5df82006-12-01 22:34:43 +00001046CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI |
1047 UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
1048 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001049
1050// These are some types that allow classification if we only want a particular
1051// thing... for example, only a signed, unsigned, or integral type.
1052SIntType : LONG | INT | SHORT | SBYTE;
1053UIntType : ULONG | UINT | USHORT | UBYTE;
1054IntType : SIntType | UIntType;
1055FPType : FLOAT | DOUBLE;
1056
1057// OptAssign - Value producing statements have an optional assignment component
1058OptAssign : Name '=' {
Reid Spencere7c3c602006-11-30 06:36:44 +00001059 $$ = $1;
1060 }
1061 | /*empty*/ {
1062 $$ = new std::string("");
1063 };
1064
1065OptLinkage
1066 : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT
1067 | EXTERN_WEAK
1068 | /*empty*/ { $$ = new std::string(""); } ;
1069
1070OptCallingConv
1071 : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK
Reid Spencer16244f42006-12-01 21:10:07 +00001072 | X86_FASTCALLCC_TOK
1073 | CC_TOK EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +00001074 *$1 += *$2;
1075 delete $2;
Reid Spencer16244f42006-12-01 21:10:07 +00001076 $$ = $1;
1077 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001078 | /*empty*/ { $$ = new std::string(""); } ;
1079
1080// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1081// a comma before it.
1082OptAlign
1083 : /*empty*/ { $$ = new std::string(); }
Reid Spencerf2d55322006-12-01 21:52:30 +00001084 | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
Reid Spencerf0cf1322006-12-07 04:23:03 +00001085
Reid Spencere7c3c602006-11-30 06:36:44 +00001086OptCAlign
1087 : /*empty*/ { $$ = new std::string(); }
1088 | ',' ALIGN EUINT64VAL {
1089 $2->insert(0, ", ");
Reid Spencerf2d55322006-12-01 21:52:30 +00001090 *$2 += " " + *$3;
1091 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001092 $$ = $2;
1093 };
1094
1095SectionString
1096 : SECTION STRINGCONSTANT {
1097 *$1 += " " + *$2;
1098 delete $2;
1099 $$ = $1;
1100 };
1101
1102OptSection : /*empty*/ { $$ = new std::string(); }
1103 | SectionString;
1104
1105GlobalVarAttributes
1106 : /* empty */ { $$ = new std::string(); }
1107 | ',' GlobalVarAttribute GlobalVarAttributes {
1108 $2->insert(0, ", ");
1109 if (!$3->empty())
1110 *$2 += " " + *$3;
1111 delete $3;
1112 $$ = $2;
1113 };
1114
1115GlobalVarAttribute
1116 : SectionString
1117 | ALIGN EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +00001118 *$1 += " " + *$2;
1119 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001120 $$ = $1;
1121 };
1122
1123//===----------------------------------------------------------------------===//
1124// Types includes all predefined types... except void, because it can only be
1125// used in specific contexts (function returning void for example). To have
1126// access to it, a user must explicitly use TypesV.
1127//
1128
1129// TypesV includes all of 'Types', but it also includes the void type.
1130TypesV : Types | VOID ;
1131UpRTypesV : UpRTypes | VOID ;
1132Types : UpRTypes ;
1133
1134// Derived types are added later...
1135//
1136PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
Reid Spencere77e35e2006-12-01 20:26:20 +00001137PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
Reid Spencera50d5962006-12-02 04:11:07 +00001138UpRTypes
1139 : OPAQUE {
Reid Spencer319a7302007-01-05 17:20:02 +00001140 $$ = TypeInfo::get(*$1, OpaqueTy);
Reid Spencera50d5962006-12-02 04:11:07 +00001141 }
1142 | SymbolicValueRef {
Reid Spencer319a7302007-01-05 17:20:02 +00001143 $$ = TypeInfo::get(*$1, UnresolvedTy);
Reid Spencera50d5962006-12-02 04:11:07 +00001144 }
Reid Spencer78720742006-12-02 20:21:22 +00001145 | PrimType {
1146 $$ = $1;
1147 }
1148 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerf2d55322006-12-01 21:52:30 +00001149 $2->insert(0, "\\");
Reid Spencer319a7302007-01-05 17:20:02 +00001150 $$ = TypeInfo::get(*$2, UpRefTy);
Reid Spencere7c3c602006-11-30 06:36:44 +00001151 }
1152 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Reid Spencer52402b02007-01-02 05:45:11 +00001153 std::string newTy( $1->getNewTy() + "(");
1154 for (unsigned i = 0; i < $3->size(); ++i) {
1155 if (i != 0)
1156 newTy += ", ";
1157 if ((*$3)[i]->isVoid())
1158 newTy += "...";
1159 else
1160 newTy += (*$3)[i]->getNewTy();
1161 }
1162 newTy += ")";
Reid Spencer319a7302007-01-05 17:20:02 +00001163 $$ = TypeInfo::get(newTy, $1, $3);
Reid Spencere7c3c602006-11-30 06:36:44 +00001164 }
1165 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencer319a7302007-01-05 17:20:02 +00001166 uint64_t elems = atoi($2->c_str());
Reid Spencerf2d55322006-12-01 21:52:30 +00001167 $2->insert(0,"[ ");
Reid Spencer52402b02007-01-02 05:45:11 +00001168 *$2 += " x " + $4->getNewTy() + " ]";
Reid Spencer319a7302007-01-05 17:20:02 +00001169 $$ = TypeInfo::get(*$2, ArrayTy, $4, elems);
Reid Spencere7c3c602006-11-30 06:36:44 +00001170 }
1171 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
Reid Spencer319a7302007-01-05 17:20:02 +00001172 uint64_t elems = atoi($2->c_str());
Reid Spencerf2d55322006-12-01 21:52:30 +00001173 $2->insert(0,"< ");
Reid Spencer52402b02007-01-02 05:45:11 +00001174 *$2 += " x " + $4->getNewTy() + " >";
Reid Spencer319a7302007-01-05 17:20:02 +00001175 $$ = TypeInfo::get(*$2, PackedTy, $4, elems);
Reid Spencere7c3c602006-11-30 06:36:44 +00001176 }
1177 | '{' TypeListI '}' { // Structure type?
Reid Spencer52402b02007-01-02 05:45:11 +00001178 std::string newTy("{");
1179 for (unsigned i = 0; i < $2->size(); ++i) {
1180 if (i != 0)
1181 newTy += ", ";
1182 newTy += (*$2)[i]->getNewTy();
1183 }
1184 newTy += "}";
Reid Spencer319a7302007-01-05 17:20:02 +00001185 $$ = TypeInfo::get(newTy, StructTy, $2);
Reid Spencere7c3c602006-11-30 06:36:44 +00001186 }
1187 | '{' '}' { // Empty structure type?
Reid Spencer319a7302007-01-05 17:20:02 +00001188 $$ = TypeInfo::get("{}", StructTy, new TypeList());
Reid Spencere7c3c602006-11-30 06:36:44 +00001189 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001190 | '<' '{' TypeListI '}' '>' { // Packed Structure type?
Reid Spencer52402b02007-01-02 05:45:11 +00001191 std::string newTy("<{");
1192 for (unsigned i = 0; i < $3->size(); ++i) {
1193 if (i != 0)
1194 newTy += ", ";
1195 newTy += (*$3)[i]->getNewTy();
1196 }
1197 newTy += "}>";
Reid Spencer319a7302007-01-05 17:20:02 +00001198 $$ = TypeInfo::get(newTy, PackedStructTy, $3);
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001199 }
1200 | '<' '{' '}' '>' { // Empty packed structure type?
Reid Spencer319a7302007-01-05 17:20:02 +00001201 $$ = TypeInfo::get("<{}>", PackedStructTy, new TypeList());
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001202 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001203 | UpRTypes '*' { // Pointer type?
Reid Spencer52402b02007-01-02 05:45:11 +00001204 $$ = $1->getPointerType();
Reid Spencere7c3c602006-11-30 06:36:44 +00001205 };
1206
1207// TypeList - Used for struct declarations and as a basis for function type
1208// declaration type lists
1209//
Reid Spencere77e35e2006-12-01 20:26:20 +00001210TypeListI
1211 : UpRTypes {
Reid Spencer52402b02007-01-02 05:45:11 +00001212 $$ = new TypeList();
1213 $$->push_back($1);
Reid Spencere77e35e2006-12-01 20:26:20 +00001214 }
1215 | TypeListI ',' UpRTypes {
Reid Spencere7c3c602006-11-30 06:36:44 +00001216 $$ = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001217 $$->push_back($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00001218 };
1219
1220// ArgTypeList - List of types for a function type declaration...
Reid Spencere77e35e2006-12-01 20:26:20 +00001221ArgTypeListI
1222 : TypeListI
Reid Spencere7c3c602006-11-30 06:36:44 +00001223 | TypeListI ',' DOTDOTDOT {
Reid Spencere7c3c602006-11-30 06:36:44 +00001224 $$ = $1;
Reid Spencer319a7302007-01-05 17:20:02 +00001225 $$->push_back(TypeInfo::get("void",VoidTy));
Reid Spencer52402b02007-01-02 05:45:11 +00001226 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001227 }
1228 | DOTDOTDOT {
Reid Spencer52402b02007-01-02 05:45:11 +00001229 $$ = new TypeList();
Reid Spencer319a7302007-01-05 17:20:02 +00001230 $$->push_back(TypeInfo::get("void",VoidTy));
Reid Spencer52402b02007-01-02 05:45:11 +00001231 delete $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001232 }
1233 | /*empty*/ {
Reid Spencer52402b02007-01-02 05:45:11 +00001234 $$ = new TypeList();
Reid Spencere7c3c602006-11-30 06:36:44 +00001235 };
1236
1237// ConstVal - The various declarations that go into the constant pool. This
1238// production is used ONLY to represent constants that show up AFTER a 'const',
1239// 'constant' or 'global' token at global scope. Constants that can be inlined
1240// into other expressions (such as integers and constexprs) are handled by the
1241// ResolvedVal, ValueRef and ConstValueRef productions.
1242//
1243ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencer30d0c582007-01-15 00:26:18 +00001244 $$ = new ConstInfo;
1245 $$->type = $1;
1246 $$->cnst = new std::string($1->getNewTy());
1247 *$$->cnst += " [ " + *$3 + " ]";
Reid Spencere7c3c602006-11-30 06:36:44 +00001248 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001249 }
1250 | Types '[' ']' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001251 $$ = new ConstInfo;
1252 $$->type = $1;
1253 $$->cnst = new std::string($1->getNewTy());
1254 *$$->cnst += "[ ]";
Reid Spencere7c3c602006-11-30 06:36:44 +00001255 }
1256 | Types 'c' STRINGCONSTANT {
Reid Spencer30d0c582007-01-15 00:26:18 +00001257 $$ = new ConstInfo;
1258 $$->type = $1;
1259 $$->cnst = new std::string($1->getNewTy());
1260 *$$->cnst += " c" + *$3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001261 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001262 }
1263 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer30d0c582007-01-15 00:26:18 +00001264 $$ = new ConstInfo;
1265 $$->type = $1;
1266 $$->cnst = new std::string($1->getNewTy());
1267 *$$->cnst += " < " + *$3 + " >";
Reid Spencere7c3c602006-11-30 06:36:44 +00001268 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001269 }
1270 | Types '{' ConstVector '}' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001271 $$ = new ConstInfo;
1272 $$->type = $1;
1273 $$->cnst = new std::string($1->getNewTy());
1274 *$$->cnst += " { " + *$3 + " }";
Reid Spencere7c3c602006-11-30 06:36:44 +00001275 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001276 }
1277 | Types '{' '}' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001278 $$ = new ConstInfo;
1279 $$->type = $1;
1280 $$->cnst = new std::string($1->getNewTy());
1281 *$$->cnst += " {}";
Reid Spencere7c3c602006-11-30 06:36:44 +00001282 }
1283 | Types NULL_TOK {
Reid Spencer30d0c582007-01-15 00:26:18 +00001284 $$ = new ConstInfo;
1285 $$->type = $1;
1286 $$->cnst = new std::string($1->getNewTy());
1287 *$$->cnst += " " + *$2;
Reid Spencerf2d55322006-12-01 21:52:30 +00001288 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001289 }
1290 | Types UNDEF {
Reid Spencer30d0c582007-01-15 00:26:18 +00001291 $$ = new ConstInfo;
1292 $$->type = $1;
1293 $$->cnst = new std::string($1->getNewTy());
1294 *$$->cnst += " " + *$2;
Reid Spencerf2d55322006-12-01 21:52:30 +00001295 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001296 }
1297 | Types SymbolicValueRef {
Reid Spencer30d0c582007-01-15 00:26:18 +00001298 $$ = new ConstInfo;
Reid Spencer319a7302007-01-05 17:20:02 +00001299 std::string Name = getUniqueName($2, $1->resolve(), true);
Reid Spencer30d0c582007-01-15 00:26:18 +00001300 $$->type = $1;
1301 $$->cnst = new std::string($1->getNewTy());
1302 *$$->cnst += " " + Name;
Reid Spencere7c3c602006-11-30 06:36:44 +00001303 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001304 }
1305 | Types ConstExpr {
Reid Spencer30d0c582007-01-15 00:26:18 +00001306 $$ = new ConstInfo;
1307 $$->type = $1;
1308 $$->cnst = new std::string($1->getNewTy());
1309 *$$->cnst += " " + *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001310 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001311 }
1312 | Types ZEROINITIALIZER {
Reid Spencer30d0c582007-01-15 00:26:18 +00001313 $$ = new ConstInfo;
1314 $$->type = $1;
1315 $$->cnst = new std::string($1->getNewTy());
1316 *$$->cnst += " " + *$2;
Reid Spencerf2d55322006-12-01 21:52:30 +00001317 delete $2;
Reid Spencere77e35e2006-12-01 20:26:20 +00001318 }
1319 | SIntType EInt64Val { // integral constants
Reid Spencer30d0c582007-01-15 00:26:18 +00001320 $$ = new ConstInfo;
1321 $$->type = $1;
1322 $$->cnst = new std::string($1->getNewTy());
1323 *$$->cnst += " " + *$2;
Reid Spencerf2d55322006-12-01 21:52:30 +00001324 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001325 }
Reid Spencer7356ae42007-01-02 06:34:08 +00001326 | UIntType EInt64Val { // integral constants
Reid Spencer30d0c582007-01-15 00:26:18 +00001327 $$ = new ConstInfo;
1328 $$->type = $1;
1329 $$->cnst = new std::string($1->getNewTy());
1330 *$$->cnst += " " + *$2;
Reid Spencerf2d55322006-12-01 21:52:30 +00001331 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001332 }
1333 | BOOL TRUETOK { // Boolean constants
Reid Spencer30d0c582007-01-15 00:26:18 +00001334 $$ = new ConstInfo;
1335 $$->type = $1;
1336 $$->cnst = new std::string($1->getNewTy());
1337 *$$->cnst += " " + *$2;
Reid Spencerf2d55322006-12-01 21:52:30 +00001338 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001339 }
1340 | BOOL FALSETOK { // Boolean constants
Reid Spencer30d0c582007-01-15 00:26:18 +00001341 $$ = new ConstInfo;
1342 $$->type = $1;
1343 $$->cnst = new std::string($1->getNewTy());
1344 *$$->cnst += " " + *$2;
Reid Spencerf2d55322006-12-01 21:52:30 +00001345 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001346 }
1347 | FPType FPVAL { // Float & Double constants
Reid Spencer30d0c582007-01-15 00:26:18 +00001348 $$ = new ConstInfo;
1349 $$->type = $1;
1350 $$->cnst = new std::string($1->getNewTy());
1351 *$$->cnst += " " + *$2;
Reid Spencerf2d55322006-12-01 21:52:30 +00001352 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001353 };
1354
Reid Spencerfcb5df82006-12-01 22:34:43 +00001355ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001356 std::string source = *$3->cnst;
1357 const TypeInfo* SrcTy = $3->type->resolve();
Reid Spencer319a7302007-01-05 17:20:02 +00001358 const TypeInfo* DstTy = $5->resolve();
Reid Spencer280d8012006-12-01 23:40:53 +00001359 if (*$1 == "cast") {
Reid Spencera50d5962006-12-02 04:11:07 +00001360 // Call getCastUpgrade to upgrade the old cast
Reid Spencer319a7302007-01-05 17:20:02 +00001361 $$ = new std::string(getCastUpgrade(source, SrcTy, DstTy, true));
Reid Spencera50d5962006-12-02 04:11:07 +00001362 } else {
1363 // Nothing to upgrade, just create the cast constant expr
1364 $$ = new std::string(*$1);
Reid Spencer52402b02007-01-02 05:45:11 +00001365 *$$ += "( " + source + " to " + $5->getNewTy() + ")";
Reid Spencer280d8012006-12-01 23:40:53 +00001366 }
Reid Spencer30d0c582007-01-15 00:26:18 +00001367 delete $1; delete $3; delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001368 }
1369 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001370 *$1 += "(" + *$3->cnst;
Reid Spencerf8483652006-12-02 15:16:01 +00001371 for (unsigned i = 0; i < $4->size(); ++i) {
Reid Spencer30d0c582007-01-15 00:26:18 +00001372 ValueInfo* VI = (*$4)[i];
1373 *$1 += ", " + *VI->val;
1374 delete VI;
Reid Spencerf8483652006-12-02 15:16:01 +00001375 }
1376 *$1 += ")";
Reid Spencere77e35e2006-12-01 20:26:20 +00001377 $$ = $1;
Reid Spencer30d0c582007-01-15 00:26:18 +00001378 delete $3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001379 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001380 }
1381 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001382 *$1 += "(" + *$3->cnst + "," + *$5->cnst + "," + *$7->cnst + ")";
1383 delete $3; delete $5; delete $7;
Reid Spencere77e35e2006-12-01 20:26:20 +00001384 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001385 }
1386 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001387 const char* op = getDivRemOpcode(*$1, $3->type);
Reid Spencer78720742006-12-02 20:21:22 +00001388 $$ = new std::string(op);
Reid Spencer30d0c582007-01-15 00:26:18 +00001389 *$$ += "(" + *$3->cnst + "," + *$5->cnst + ")";
1390 delete $1; delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001391 }
1392 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001393 *$1 += "(" + *$3->cnst + "," + *$5->cnst + ")";
1394 delete $3; delete $5;
Reid Spencere77e35e2006-12-01 20:26:20 +00001395 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001396 }
1397 | SetCondOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001398 *$1 = getCompareOp(*$1, $3->type);
1399 *$1 += "(" + *$3->cnst + "," + *$5->cnst + ")";
1400 delete $3; delete $5;
Reid Spencere77e35e2006-12-01 20:26:20 +00001401 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001402 }
Reid Spencer57f28f92006-12-03 07:10:26 +00001403 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001404 *$1 += " " + *$2 + " (" + *$4->cnst + "," + *$6->cnst + ")";
1405 delete $2; delete $4; delete $6;
Reid Spencer57f28f92006-12-03 07:10:26 +00001406 $$ = $1;
1407 }
1408 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001409 *$1 += " " + *$2 + " (" + *$4->cnst + "," + *$6->cnst + ")";
1410 delete $2; delete $4; delete $6;
Reid Spencer229e9362006-12-02 22:14:11 +00001411 $$ = $1;
1412 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001413 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencerf7bde222006-12-01 22:26:37 +00001414 const char* shiftop = $1->c_str();
1415 if (*$1 == "shr")
Reid Spencer30d0c582007-01-15 00:26:18 +00001416 shiftop = ($3->type->isUnsigned()) ? "lshr" : "ashr";
Reid Spencerf7bde222006-12-01 22:26:37 +00001417 $$ = new std::string(shiftop);
Reid Spencer30d0c582007-01-15 00:26:18 +00001418 *$$ += "(" + *$3->cnst + "," + *$5->cnst + ")";
1419 delete $1; delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001420 }
1421 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001422 *$1 += "(" + *$3->cnst + "," + *$5->cnst + ")";
1423 delete $3; delete $5;
Reid Spencere77e35e2006-12-01 20:26:20 +00001424 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001425 }
1426 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001427 *$1 += "(" + *$3->cnst + "," + *$5->cnst + "," + *$7->cnst + ")";
1428 delete $3; delete $5; delete $7;
Reid Spencere77e35e2006-12-01 20:26:20 +00001429 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001430 }
1431 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001432 *$1 += "(" + *$3->cnst + "," + *$5->cnst + "," + *$7->cnst + ")";
1433 delete $3; delete $5; delete $7;
Reid Spencere77e35e2006-12-01 20:26:20 +00001434 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001435 };
1436
1437
1438// ConstVector - A list of comma separated constants.
Reid Spencere77e35e2006-12-01 20:26:20 +00001439
1440ConstVector
1441 : ConstVector ',' ConstVal {
Reid Spencer30d0c582007-01-15 00:26:18 +00001442 *$1 += ", " + *$3->cnst;
1443 delete $3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001444 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001445 }
Reid Spencer30d0c582007-01-15 00:26:18 +00001446 | ConstVal { $$ = new std::string(*$1->cnst); delete $1; }
Reid Spencere77e35e2006-12-01 20:26:20 +00001447 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001448
1449
1450// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Reid Spencere77e35e2006-12-01 20:26:20 +00001451GlobalType : GLOBAL | CONSTANT ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001452
1453
1454//===----------------------------------------------------------------------===//
1455// Rules to match Modules
1456//===----------------------------------------------------------------------===//
1457
1458// Module rule: Capture the result of parsing the whole file into a result
1459// variable...
1460//
1461Module : DefinitionList {
1462};
1463
1464// DefinitionList - Top level definitions
1465//
1466DefinitionList : DefinitionList Function {
1467 $$ = 0;
1468 }
1469 | DefinitionList FunctionProto {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001470 *O << *$2 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001471 delete $2;
1472 $$ = 0;
1473 }
1474 | DefinitionList MODULE ASM_TOK AsmBlock {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001475 *O << "module asm " << ' ' << *$4 << '\n';
Reid Spencerd154b572006-12-01 20:36:40 +00001476 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001477 }
1478 | DefinitionList IMPLEMENTATION {
1479 *O << "implementation\n";
Reid Spencerd154b572006-12-01 20:36:40 +00001480 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001481 }
Reid Spencera50d5962006-12-02 04:11:07 +00001482 | ConstPool { $$ = 0; }
Reid Spencere7c3c602006-11-30 06:36:44 +00001483
Reid Spencer78720742006-12-02 20:21:22 +00001484External : EXTERNAL | UNINITIALIZED { $$ = $1; *$$ = "external"; }
1485
Reid Spencere7c3c602006-11-30 06:36:44 +00001486// ConstPool - Constants with optional names assigned to them.
1487ConstPool : ConstPool OptAssign TYPE TypesV {
Reid Spencer30d0c582007-01-15 00:26:18 +00001488 TypeInfo::EnumeratedTypes.push_back($4);
Reid Spencera50d5962006-12-02 04:11:07 +00001489 if (!$2->empty()) {
Reid Spencer30d0c582007-01-15 00:26:18 +00001490 TypeInfo::NamedTypes[*$2] = $4;
Reid Spencera50d5962006-12-02 04:11:07 +00001491 *O << *$2 << " = ";
1492 }
Reid Spencer52402b02007-01-02 05:45:11 +00001493 *O << "type " << $4->getNewTy() << '\n';
1494 delete $2; delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001495 $$ = 0;
1496 }
1497 | ConstPool FunctionProto { // Function prototypes can be in const pool
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001498 *O << *$2 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001499 delete $2;
1500 $$ = 0;
1501 }
1502 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001503 *O << *$2 << ' ' << *$3 << ' ' << *$4 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001504 delete $2; delete $3; delete $4;
1505 $$ = 0;
1506 }
1507 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +00001508 if (!$2->empty()) {
Reid Spencer30d0c582007-01-15 00:26:18 +00001509 std::string Name = getGlobalName($2,*$3, $5->type->getPointerType(),
Reid Spencer319a7302007-01-05 17:20:02 +00001510 *$4 == "constant");
Reid Spencer52402b02007-01-02 05:45:11 +00001511 *O << Name << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +00001512 }
Reid Spencer30d0c582007-01-15 00:26:18 +00001513 *O << *$3 << ' ' << *$4 << ' ' << *$5->cnst << ' ' << *$6 << '\n';
Reid Spencer52402b02007-01-02 05:45:11 +00001514 delete $2; delete $3; delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001515 $$ = 0;
1516 }
Reid Spencer78720742006-12-02 20:21:22 +00001517 | ConstPool OptAssign External GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +00001518 if (!$2->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001519 std::string Name = getGlobalName($2,*$3,$5->getPointerType(),
1520 *$4 == "constant");
Reid Spencer52402b02007-01-02 05:45:11 +00001521 *O << Name << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +00001522 }
Reid Spencer52402b02007-01-02 05:45:11 +00001523 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
1524 delete $2; delete $3; delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001525 $$ = 0;
1526 }
Reid Spencer319a7302007-01-05 17:20:02 +00001527 | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +00001528 if (!$2->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001529 std::string Name = getGlobalName($2,*$3,$5->getPointerType(),
1530 *$4 == "constant");
Reid Spencer52402b02007-01-02 05:45:11 +00001531 *O << Name << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +00001532 }
Reid Spencer52402b02007-01-02 05:45:11 +00001533 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
1534 delete $2; delete $3; delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001535 $$ = 0;
1536 }
1537 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +00001538 if (!$2->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001539 std::string Name = getGlobalName($2,*$3,$5->getPointerType(),
1540 *$4 == "constant");
Reid Spencer52402b02007-01-02 05:45:11 +00001541 *O << Name << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +00001542 }
Reid Spencer52402b02007-01-02 05:45:11 +00001543 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
1544 delete $2; delete $3; delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001545 $$ = 0;
1546 }
1547 | ConstPool TARGET TargetDefinition {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001548 *O << *$2 << ' ' << *$3 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001549 delete $2; delete $3;
1550 $$ = 0;
1551 }
1552 | ConstPool DEPLIBS '=' LibrariesDefinition {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001553 *O << *$2 << " = " << *$4 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001554 delete $2; delete $4;
1555 $$ = 0;
1556 }
1557 | /* empty: end of list */ {
1558 $$ = 0;
1559 };
1560
1561
1562AsmBlock : STRINGCONSTANT ;
1563
1564BigOrLittle : BIG | LITTLE
1565
1566TargetDefinition
1567 : ENDIAN '=' BigOrLittle {
Reid Spencere77e35e2006-12-01 20:26:20 +00001568 *$1 += " = " + *$3;
1569 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001570 $$ = $1;
1571 }
1572 | POINTERSIZE '=' EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +00001573 *$1 += " = " + *$3;
1574 if (*$3 == "64")
Reid Spencere77e35e2006-12-01 20:26:20 +00001575 SizeOfPointer = 64;
Reid Spencerf2d55322006-12-01 21:52:30 +00001576 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001577 $$ = $1;
1578 }
1579 | TRIPLE '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +00001580 *$1 += " = " + *$3;
1581 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001582 $$ = $1;
1583 }
1584 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +00001585 *$1 += " = " + *$3;
1586 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001587 $$ = $1;
1588 };
1589
1590LibrariesDefinition
1591 : '[' LibList ']' {
1592 $2->insert(0, "[ ");
1593 *$2 += " ]";
1594 $$ = $2;
1595 };
1596
1597LibList
1598 : LibList ',' STRINGCONSTANT {
1599 *$1 += ", " + *$3;
1600 delete $3;
1601 $$ = $1;
1602 }
1603 | STRINGCONSTANT
1604 | /* empty: end of list */ {
1605 $$ = new std::string();
1606 };
1607
1608//===----------------------------------------------------------------------===//
1609// Rules to match Function Headers
1610//===----------------------------------------------------------------------===//
1611
1612Name : VAR_ID | STRINGCONSTANT;
1613OptName : Name | /*empty*/ { $$ = new std::string(); };
1614
1615ArgVal : Types OptName {
Reid Spencer52402b02007-01-02 05:45:11 +00001616 $$ = new std::string($1->getNewTy());
1617 if (!$2->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001618 std::string Name = getUniqueName($2, $1->resolve());
Reid Spencer52402b02007-01-02 05:45:11 +00001619 *$$ += " " + Name;
1620 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001621 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001622};
1623
1624ArgListH : ArgListH ',' ArgVal {
1625 *$1 += ", " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001626 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001627 }
1628 | ArgVal {
1629 $$ = $1;
1630 };
1631
1632ArgList : ArgListH {
1633 $$ = $1;
1634 }
1635 | ArgListH ',' DOTDOTDOT {
1636 *$1 += ", ...";
1637 $$ = $1;
Reid Spencere77e35e2006-12-01 20:26:20 +00001638 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001639 }
1640 | DOTDOTDOT {
1641 $$ = $1;
1642 }
Reid Spencerd154b572006-12-01 20:36:40 +00001643 | /* empty */ { $$ = new std::string(); };
Reid Spencere7c3c602006-11-30 06:36:44 +00001644
Reid Spencer71d2ec92006-12-31 06:02:26 +00001645FunctionHeaderH
1646 : OptCallingConv TypesV Name '(' ArgList ')' OptSection OptAlign {
Reid Spencerc4d96252007-01-13 00:03:30 +00001647 if (*$3 == "%llvm.va_start" || *$3 == "%llvm.va_end") {
Reid Spencerf0c9a652007-01-13 00:13:49 +00001648 *$5 = "i8*";
Reid Spencerc4d96252007-01-13 00:03:30 +00001649 } else if (*$3 == "%llvm.va_copy") {
1650 *$5 = "i8*, i8*";
1651 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001652 if (!$1->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +00001653 *$1 += " ";
Reid Spencere7c3c602006-11-30 06:36:44 +00001654 }
Reid Spencer52402b02007-01-02 05:45:11 +00001655 *$1 += $2->getNewTy() + " " + *$3 + "(" + *$5 + ")";
Reid Spencere7c3c602006-11-30 06:36:44 +00001656 if (!$7->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +00001657 *$1 += " " + *$7;
Reid Spencere7c3c602006-11-30 06:36:44 +00001658 }
1659 if (!$8->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +00001660 *$1 += " " + *$8;
Reid Spencere7c3c602006-11-30 06:36:44 +00001661 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001662 delete $3;
1663 delete $5;
1664 delete $7;
1665 delete $8;
1666 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001667 };
1668
Reid Spencer78720742006-12-02 20:21:22 +00001669BEGIN : BEGINTOK { $$ = new std::string("{"); delete $1; }
1670 | '{' { $$ = new std::string ("{"); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001671
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001672FunctionHeader
1673 : OptLinkage FunctionHeaderH BEGIN {
1674 *O << "define ";
1675 if (!$1->empty()) {
1676 *O << *$1 << ' ';
1677 }
1678 *O << *$2 << ' ' << *$3 << '\n';
1679 delete $1; delete $2; delete $3;
1680 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001681 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001682 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001683
Reid Spencer78720742006-12-02 20:21:22 +00001684END : ENDTOK { $$ = new std::string("}"); delete $1; }
Reid Spencere7c3c602006-11-30 06:36:44 +00001685 | '}' { $$ = new std::string("}"); };
1686
1687Function : FunctionHeader BasicBlockList END {
1688 if ($2)
1689 *O << *$2;
Reid Spencer71d2ec92006-12-31 06:02:26 +00001690 *O << *$3 << "\n\n";
Reid Spencer52402b02007-01-02 05:45:11 +00001691 delete $1; delete $2; delete $3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001692 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001693};
1694
Reid Spencere77e35e2006-12-01 20:26:20 +00001695FnDeclareLinkage
1696 : /*default*/ { $$ = new std::string(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001697 | DLLIMPORT
1698 | EXTERN_WEAK
1699 ;
1700
1701FunctionProto
Reid Spencerc4d96252007-01-13 00:03:30 +00001702 : DECLARE { isDeclare = true; } FnDeclareLinkage FunctionHeaderH {
1703 if (!$3->empty())
1704 *$1 += " " + *$3;
1705 *$1 += " " + *$4;
Reid Spencere77e35e2006-12-01 20:26:20 +00001706 delete $3;
Reid Spencerc4d96252007-01-13 00:03:30 +00001707 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001708 $$ = $1;
Reid Spencerc4d96252007-01-13 00:03:30 +00001709 isDeclare = false;
Reid Spencere7c3c602006-11-30 06:36:44 +00001710 };
1711
1712//===----------------------------------------------------------------------===//
1713// Rules to match Basic Blocks
1714//===----------------------------------------------------------------------===//
1715
Reid Spencerd154b572006-12-01 20:36:40 +00001716OptSideEffect : /* empty */ { $$ = new std::string(); }
1717 | SIDEEFFECT;
Reid Spencere7c3c602006-11-30 06:36:44 +00001718
Reid Spencere77e35e2006-12-01 20:26:20 +00001719ConstValueRef
Reid Spencerf2d55322006-12-01 21:52:30 +00001720 : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK | NULL_TOK | UNDEF
1721 | ZEROINITIALIZER
Reid Spencere7c3c602006-11-30 06:36:44 +00001722 | '<' ConstVector '>' {
1723 $2->insert(0, "<");
1724 *$2 += ">";
1725 $$ = $2;
1726 }
1727 | ConstExpr
1728 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
1729 if (!$2->empty()) {
1730 *$1 += " " + *$2;
1731 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001732 *$1 += " " + *$3 + ", " + *$5;
1733 delete $2; delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001734 $$ = $1;
1735 };
1736
Reid Spencerf2d55322006-12-01 21:52:30 +00001737SymbolicValueRef : IntVal | Name ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001738
1739// ValueRef - A reference to a definition... either constant or symbolic
Reid Spencerf459d392006-12-02 16:19:52 +00001740ValueRef
1741 : SymbolicValueRef {
Reid Spencer30d0c582007-01-15 00:26:18 +00001742 $$ = new ValueInfo;
1743 $$->val = $1;
1744 $$->constant = false;
1745 $$->type = 0;
Reid Spencerf459d392006-12-02 16:19:52 +00001746 }
1747 | ConstValueRef {
Reid Spencer30d0c582007-01-15 00:26:18 +00001748 $$ = new ValueInfo;
1749 $$->val = $1;
1750 $$->constant = true;
1751 $$->type = 0;
Reid Spencerf459d392006-12-02 16:19:52 +00001752 }
1753 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001754
1755// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1756// type immediately preceeds the value reference, and allows complex constant
1757// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1758ResolvedVal : Types ValueRef {
Reid Spencer319a7302007-01-05 17:20:02 +00001759 $1 = $1->resolve();
Reid Spencer30d0c582007-01-15 00:26:18 +00001760 std::string Name = getUniqueName($2->val, $1);
Reid Spencerf459d392006-12-02 16:19:52 +00001761 $$ = $2;
Reid Spencer30d0c582007-01-15 00:26:18 +00001762 delete $$->val;
1763 $$->val = new std::string($1->getNewTy() + " " + Name);
1764 $$->type = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001765 };
1766
1767BasicBlockList : BasicBlockList BasicBlock {
Reid Spencerf2d55322006-12-01 21:52:30 +00001768 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001769 }
1770 | BasicBlock { // Do not allow functions with 0 basic blocks
Reid Spencerf2d55322006-12-01 21:52:30 +00001771 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001772 };
1773
1774
1775// Basic blocks are terminated by branching instructions:
1776// br, br/cc, switch, ret
1777//
Reid Spencer16244f42006-12-01 21:10:07 +00001778BasicBlock : InstructionList BBTerminatorInst {
Reid Spencerf2d55322006-12-01 21:52:30 +00001779 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001780 };
1781
1782InstructionList : InstructionList Inst {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001783 *O << " " << *$2 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001784 delete $2;
1785 $$ = 0;
1786 }
1787 | /* empty */ {
1788 $$ = 0;
1789 }
1790 | LABELSTR {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001791 *O << *$1 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001792 delete $1;
1793 $$ = 0;
1794 };
1795
Reid Spencer78720742006-12-02 20:21:22 +00001796Unwind : UNWIND | EXCEPT { $$ = $1; *$$ = "unwind"; }
1797
Reid Spencere7c3c602006-11-30 06:36:44 +00001798BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencer30d0c582007-01-15 00:26:18 +00001799 *O << " " << *$1 << ' ' << *$2->val << '\n';
1800 delete $1; delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001801 $$ = 0;
1802 }
1803 | RET VOID { // Return with no result...
Reid Spencer52402b02007-01-02 05:45:11 +00001804 *O << " " << *$1 << ' ' << $2->getNewTy() << '\n';
Reid Spencer319a7302007-01-05 17:20:02 +00001805 delete $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001806 $$ = 0;
1807 }
1808 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer30d0c582007-01-15 00:26:18 +00001809 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << *$3->val << '\n';
1810 delete $1; delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001811 $$ = 0;
1812 } // Conditional Branch...
1813 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencer30d0c582007-01-15 00:26:18 +00001814 std::string Name = getUniqueName($3->val, $2);
Reid Spencer52402b02007-01-02 05:45:11 +00001815 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", "
Reid Spencer30d0c582007-01-15 00:26:18 +00001816 << $5->getNewTy() << ' ' << *$6->val << ", " << $8->getNewTy() << ' '
1817 << *$9->val << '\n';
1818 delete $1; delete $3; delete $6; delete $9;
Reid Spencere7c3c602006-11-30 06:36:44 +00001819 $$ = 0;
1820 }
1821 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001822 std::string Name = getUniqueName($3->val, $2);
Reid Spencer52402b02007-01-02 05:45:11 +00001823 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", "
Reid Spencer30d0c582007-01-15 00:26:18 +00001824 << $5->getNewTy() << ' ' << *$6->val << " [" << *$8 << " ]\n";
1825 delete $1;
1826 delete $3;
1827 delete $6;
Reid Spencerf459d392006-12-02 16:19:52 +00001828 delete $8;
Reid Spencere7c3c602006-11-30 06:36:44 +00001829 $$ = 0;
1830 }
1831 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001832 std::string Name = getUniqueName($3->val, $2);
Reid Spencer52402b02007-01-02 05:45:11 +00001833 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", "
Reid Spencer30d0c582007-01-15 00:26:18 +00001834 << $5->getNewTy() << ' ' << *$6->val << "[]\n";
1835 delete $1;
1836 delete $3;
1837 delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001838 $$ = 0;
1839 }
Reid Spencer16244f42006-12-01 21:10:07 +00001840 | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
Reid Spencer78720742006-12-02 20:21:22 +00001841 TO LABEL ValueRef Unwind LABEL ValueRef {
Reid Spencer319a7302007-01-05 17:20:02 +00001842 const TypeInfo* ResTy = getFunctionReturnType($4);
Reid Spencer16244f42006-12-01 21:10:07 +00001843 *O << " ";
Reid Spencer52402b02007-01-02 05:45:11 +00001844 if (!$1->empty()) {
1845 std::string Name = getUniqueName($1, ResTy);
1846 *O << Name << " = ";
1847 }
Reid Spencer30d0c582007-01-15 00:26:18 +00001848 *O << *$2 << ' ' << *$3 << ' ' << $4->getNewTy() << ' ' << *$5->val << " (";
Reid Spencerf8483652006-12-02 15:16:01 +00001849 for (unsigned i = 0; i < $7->size(); ++i) {
Reid Spencer30d0c582007-01-15 00:26:18 +00001850 ValueInfo* VI = (*$7)[i];
1851 *O << *VI->val;
Reid Spencerf8483652006-12-02 15:16:01 +00001852 if (i+1 < $7->size())
1853 *O << ", ";
Reid Spencer30d0c582007-01-15 00:26:18 +00001854 delete VI;
Reid Spencerf8483652006-12-02 15:16:01 +00001855 }
Reid Spencer30d0c582007-01-15 00:26:18 +00001856 *O << ") " << *$9 << ' ' << $10->getNewTy() << ' ' << *$11->val << ' '
1857 << *$12 << ' ' << $13->getNewTy() << ' ' << *$14->val << '\n';
1858 delete $1; delete $2; delete $3; delete $5; delete $7;
1859 delete $9; delete $11; delete $12; delete $14;
Reid Spencere7c3c602006-11-30 06:36:44 +00001860 $$ = 0;
1861 }
Reid Spencer78720742006-12-02 20:21:22 +00001862 | Unwind {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001863 *O << " " << *$1 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001864 delete $1;
1865 $$ = 0;
1866 }
1867 | UNREACHABLE {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001868 *O << " " << *$1 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001869 delete $1;
1870 $$ = 0;
1871 };
1872
1873JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001874 *$1 += " " + $2->getNewTy() + " " + *$3 + ", " + $5->getNewTy() + " " +
Reid Spencer30d0c582007-01-15 00:26:18 +00001875 *$6->val;
1876 delete $3; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001877 $$ = $1;
1878 }
1879 | IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001880 $2->insert(0, $1->getNewTy() + " " );
Reid Spencer30d0c582007-01-15 00:26:18 +00001881 *$2 += ", " + $4->getNewTy() + " " + *$5->val;
1882 delete $5;
Reid Spencere77e35e2006-12-01 20:26:20 +00001883 $$ = $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001884 };
1885
1886Inst
1887 : OptAssign InstVal {
Reid Spencerf5626a32007-01-01 01:20:41 +00001888 if (!$1->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001889 // Get a unique name for this value, based on its type.
Reid Spencer30d0c582007-01-15 00:26:18 +00001890 std::string Name = getUniqueName($1, $2->type);
Reid Spencer319a7302007-01-05 17:20:02 +00001891 *$1 = Name + " = ";
1892 if (deleteUselessCastFlag && *deleteUselessCastName == Name) {
1893 // don't actually delete it, just comment it out
1894 $1->insert(0, "; USELSS BITCAST: ");
Reid Spencerf5626a32007-01-01 01:20:41 +00001895 delete deleteUselessCastName;
Reid Spencerf5626a32007-01-01 01:20:41 +00001896 }
1897 }
Reid Spencer30d0c582007-01-15 00:26:18 +00001898 *$1 += *$2->val;
1899 delete $2;
Reid Spencerf5626a32007-01-01 01:20:41 +00001900 deleteUselessCastFlag = false;
Reid Spencere7c3c602006-11-30 06:36:44 +00001901 $$ = $1;
1902 };
1903
1904PHIList
1905 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer30d0c582007-01-15 00:26:18 +00001906 std::string Name = getUniqueName($3->val, $1);
Reid Spencer52402b02007-01-02 05:45:11 +00001907 Name.insert(0, $1->getNewTy() + "[");
Reid Spencer30d0c582007-01-15 00:26:18 +00001908 Name += "," + *$5->val + "]";
1909 $$ = new ValueInfo;
1910 $$->val = new std::string(Name);
1911 $$->type = $1;
1912 delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001913 }
1914 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001915 std::string Name = getUniqueName($4->val, $1->type);
1916 *$1->val += ", [" + Name + "," + *$6->val + "]";
1917 delete $4;
1918 delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001919 $$ = $1;
1920 };
1921
1922
1923ValueRefList
Reid Spencer52402b02007-01-02 05:45:11 +00001924 : ResolvedVal {
Reid Spencerf8483652006-12-02 15:16:01 +00001925 $$ = new ValueList();
1926 $$->push_back($1);
1927 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001928 | ValueRefList ',' ResolvedVal {
Reid Spencere7c3c602006-11-30 06:36:44 +00001929 $$ = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001930 $$->push_back($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00001931 };
1932
1933// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1934ValueRefListE
Reid Spencerf8483652006-12-02 15:16:01 +00001935 : ValueRefList { $$ = $1; }
1936 | /*empty*/ { $$ = new ValueList(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001937 ;
1938
1939OptTailCall
1940 : TAIL CALL {
1941 *$1 += " " + *$2;
1942 delete $2;
1943 $$ = $1;
1944 }
1945 | CALL
1946 ;
1947
1948InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer78720742006-12-02 20:21:22 +00001949 const char* op = getDivRemOpcode(*$1, $2);
Reid Spencer30d0c582007-01-15 00:26:18 +00001950 std::string Name1 = getUniqueName($3->val, $2);
1951 std::string Name2 = getUniqueName($5->val, $2);
1952 $$ = $3;
1953 delete $$->val;
1954 $$->val = new std::string(op);
1955 *$$->val += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
1956 $$->type = $2;
1957 delete $1; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001958 }
1959 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer30d0c582007-01-15 00:26:18 +00001960 std::string Name1 = getUniqueName($3->val, $2);
1961 std::string Name2 = getUniqueName($5->val, $2);
Reid Spencer52402b02007-01-02 05:45:11 +00001962 *$1 += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
Reid Spencer30d0c582007-01-15 00:26:18 +00001963 $$ = $3;
1964 delete $$->val;
1965 $$->val = $1;
1966 $$->type = $2;
1967 delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001968 }
1969 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer30d0c582007-01-15 00:26:18 +00001970 std::string Name1 = getUniqueName($3->val, $2);
1971 std::string Name2 = getUniqueName($5->val, $2);
Reid Spencer229e9362006-12-02 22:14:11 +00001972 *$1 = getCompareOp(*$1, $2);
Reid Spencer52402b02007-01-02 05:45:11 +00001973 *$1 += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
Reid Spencer30d0c582007-01-15 00:26:18 +00001974 $$ = $3;
1975 delete $$->val;
1976 $$->val = $1;
1977 $$->type = TypeInfo::get("i1",BoolTy);
1978 delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001979 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001980 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer30d0c582007-01-15 00:26:18 +00001981 std::string Name1 = getUniqueName($4->val, $3);
1982 std::string Name2 = getUniqueName($6->val, $3);
Reid Spencer52402b02007-01-02 05:45:11 +00001983 *$1 += " " + *$2 + " " + $3->getNewTy() + " " + Name1 + "," + Name2;
Reid Spencer30d0c582007-01-15 00:26:18 +00001984 $$ = $4;
1985 delete $$->val;
1986 $$->val = $1;
1987 $$->type = TypeInfo::get("i1",BoolTy);
1988 delete $2; delete $6;
Reid Spencer57f28f92006-12-03 07:10:26 +00001989 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001990 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer30d0c582007-01-15 00:26:18 +00001991 std::string Name1 = getUniqueName($4->val, $3);
1992 std::string Name2 = getUniqueName($6->val, $3);
Reid Spencer52402b02007-01-02 05:45:11 +00001993 *$1 += " " + *$2 + " " + $3->getNewTy() + " " + Name1 + "," + Name2;
Reid Spencer30d0c582007-01-15 00:26:18 +00001994 $$ = $4;
1995 delete $$->val;
1996 $$->val = $1;
1997 $$->type = TypeInfo::get("i1",BoolTy);
1998 delete $2; delete $6;
Reid Spencer229e9362006-12-02 22:14:11 +00001999 }
Reid Spencere7c3c602006-11-30 06:36:44 +00002000 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencerf7bde222006-12-01 22:26:37 +00002001 const char* shiftop = $1->c_str();
2002 if (*$1 == "shr")
Reid Spencer30d0c582007-01-15 00:26:18 +00002003 shiftop = ($2->type->isUnsigned()) ? "lshr" : "ashr";
2004 std::string *val = new std::string(shiftop);
2005 *val += " " + *$2->val + ", " + *$4->val;
2006 $$ = $2;
2007 delete $$->val;
2008 $$->val = val;
2009 delete $1; delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00002010 }
Reid Spencerfcb5df82006-12-01 22:34:43 +00002011 | CastOps ResolvedVal TO Types {
Reid Spencer30d0c582007-01-15 00:26:18 +00002012 std::string source = *$2->val;
2013 const TypeInfo* SrcTy = $2->type->resolve();
Reid Spencer319a7302007-01-05 17:20:02 +00002014 const TypeInfo* DstTy = $4->resolve();
Reid Spencer30d0c582007-01-15 00:26:18 +00002015 $$ = $2;
2016 delete $$->val;
2017 $$->val = new std::string();
2018 $$->type = DstTy;
Reid Spencer280d8012006-12-01 23:40:53 +00002019 if (*$1 == "cast") {
Reid Spencer30d0c582007-01-15 00:26:18 +00002020 *$$->val += getCastUpgrade(source, SrcTy, DstTy, false);
Reid Spencera50d5962006-12-02 04:11:07 +00002021 } else {
Reid Spencer30d0c582007-01-15 00:26:18 +00002022 *$$->val += *$1 + " " + source + " to " + DstTy->getNewTy();
Reid Spencer280d8012006-12-01 23:40:53 +00002023 }
Reid Spencerf5626a32007-01-01 01:20:41 +00002024 // Check to see if this is a useless cast of a value to the same name
2025 // and the same type. Such casts will probably cause redefinition errors
2026 // when assembled and perform no code gen action so just remove them.
2027 if (*$1 == "cast" || *$1 == "bitcast")
Reid Spencer319a7302007-01-05 17:20:02 +00002028 if (SrcTy->isInteger() && DstTy->isInteger() &&
2029 SrcTy->getBitWidth() == DstTy->getBitWidth()) {
Reid Spencerf5626a32007-01-01 01:20:41 +00002030 deleteUselessCastFlag = true; // Flag the "Inst" rule
Reid Spencer30d0c582007-01-15 00:26:18 +00002031 deleteUselessCastName = new std::string(*$2->val); // save the name
Reid Spencerf5626a32007-01-01 01:20:41 +00002032 size_t pos = deleteUselessCastName->find_first_of("%\"",0);
2033 if (pos != std::string::npos) {
2034 // remove the type portion before val
2035 deleteUselessCastName->erase(0, pos);
2036 }
2037 }
Reid Spencer30d0c582007-01-15 00:26:18 +00002038 delete $1;
Reid Spencer52402b02007-01-02 05:45:11 +00002039 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002040 }
2041 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer30d0c582007-01-15 00:26:18 +00002042 *$1 += " " + *$2->val + ", " + *$4->val + ", " + *$6->val;
2043 $$ = $2;
2044 delete $$->val;
2045 $$->val = $1;
2046 $$->type = $4->type;
2047 delete $4;
2048 delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00002049 }
2050 | VAARG ResolvedVal ',' Types {
Reid Spencer30d0c582007-01-15 00:26:18 +00002051 *$1 += " " + *$2->val + ", " + $4->getNewTy();
2052 $$ = $2;
2053 delete $$->val;
2054 $$->val = $1;
2055 $$->type = $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00002056 }
2057 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencer30d0c582007-01-15 00:26:18 +00002058 *$1 += " " + *$2->val + ", " + *$4->val;
2059 $$ = $2;
2060 delete $$->val;
2061 $$->val = $1;
2062 $$->type = $$->type->resolve();
2063 $$->type = $$->type->getElementType();
2064 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00002065 }
2066 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer30d0c582007-01-15 00:26:18 +00002067 *$1 += " " + *$2->val + ", " + *$4->val + ", " + *$6->val;
2068 $$ = $2;
2069 delete $$->val;
2070 $$->val = $1;
2071 delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00002072 }
2073 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer30d0c582007-01-15 00:26:18 +00002074 *$1 += " " + *$2->val + ", " + *$4->val + ", " + *$6->val;
2075 $$ = $2;
2076 delete $$->val;
2077 $$->val = $1;
2078 delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00002079 }
2080 | PHI_TOK PHIList {
Reid Spencer30d0c582007-01-15 00:26:18 +00002081 *$1 += " " + *$2->val;
2082 $$ = $2;
2083 delete $2->val;
2084 $$->val = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00002085 }
2086 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Reid Spencer12969882007-01-07 08:07:39 +00002087 // map llvm.isunordered to "fcmp uno"
Reid Spencer30d0c582007-01-15 00:26:18 +00002088 $$ = new ValueInfo;
2089 if (*$4->val == "%llvm.isunordered.f32" ||
2090 *$4->val == "%llvm.isunordered.f64") {
2091 $$->val = new std::string( "fcmp uno " + *(*$6)[0]->val + ", ");
2092 size_t pos = (*$6)[1]->val->find(' ');
Reid Spencer12969882007-01-07 08:07:39 +00002093 assert(pos != std::string::npos && "no space?");
Reid Spencer30d0c582007-01-15 00:26:18 +00002094 *$$->val += (*$6)[1]->val->substr(pos+1);
2095 $$->type = TypeInfo::get("i1", BoolTy);
Reid Spencer12969882007-01-07 08:07:39 +00002096 } else {
Reid Spencerc4d96252007-01-13 00:03:30 +00002097 static unsigned upgradeCount = 1;
Reid Spencer30d0c582007-01-15 00:26:18 +00002098 if (*$4->val == "%llvm.va_start" || *$4->val == "%llvm.va_end") {
Reid Spencerf6e54592007-01-13 00:23:06 +00002099 if (!$6->empty()) {
2100 std::string name("%va_upgrade");
2101 name += llvm::utostr(upgradeCount++);
Reid Spencer30d0c582007-01-15 00:26:18 +00002102 $1->insert(0, name + " = bitcast " + *(*$6)[0]->val + " to i8*\n ");
2103 *(*$6)[0]->val = "i8* " + name;
2104 (*$6)[0]->type = TypeInfo::get("i8", UByteTy)->getPointerType();
Reid Spencerf6e54592007-01-13 00:23:06 +00002105 }
Reid Spencer30d0c582007-01-15 00:26:18 +00002106 } else if (*$4->val == "%llvm.va_copy") {
Reid Spencerc4d96252007-01-13 00:03:30 +00002107 std::string name0("%va_upgrade");
2108 name0 += llvm::utostr(upgradeCount++);
2109 std::string name1("%va_upgrade");
2110 name1 += llvm::utostr(upgradeCount++);
Reid Spencer30d0c582007-01-15 00:26:18 +00002111 $1->insert(0, name0 + " = bitcast " + *(*$6)[0]->val + " to i8*\n " +
2112 name1 + " = bitcast " + *(*$6)[1]->val + " to i8*\n ");
2113 *(*$6)[0]->val = "i8* " + name0;
2114 (*$6)[0]->type = TypeInfo::get("i8", UByteTy)->getPointerType();
2115 *(*$6)[1]->val = "i8* " + name1;
2116 (*$6)[0]->type = TypeInfo::get("i8", UByteTy)->getPointerType();
Reid Spencerc4d96252007-01-13 00:03:30 +00002117 }
Reid Spencer12969882007-01-07 08:07:39 +00002118 if (!$2->empty())
2119 *$1 += " " + *$2;
2120 if (!$1->empty())
2121 *$1 += " ";
Reid Spencer30d0c582007-01-15 00:26:18 +00002122 *$1 += $3->getNewTy() + " " + *$4->val + "(";
Reid Spencer12969882007-01-07 08:07:39 +00002123 for (unsigned i = 0; i < $6->size(); ++i) {
Reid Spencer30d0c582007-01-15 00:26:18 +00002124 ValueInfo* VI = (*$6)[i];
2125 *$1 += *VI->val;
Reid Spencer12969882007-01-07 08:07:39 +00002126 if (i+1 < $6->size())
2127 *$1 += ", ";
Reid Spencer30d0c582007-01-15 00:26:18 +00002128 delete VI;
Reid Spencer12969882007-01-07 08:07:39 +00002129 }
2130 *$1 += ")";
Reid Spencer30d0c582007-01-15 00:26:18 +00002131 $$ = new ValueInfo;
2132 $$->val = $1;
2133 $$->type = getFunctionReturnType($3);
Reid Spencerf8483652006-12-02 15:16:01 +00002134 }
Reid Spencer30d0c582007-01-15 00:26:18 +00002135 delete $2; delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00002136 }
2137 | MemoryInst ;
2138
2139
2140// IndexList - List of indices for GEP based instructions...
2141IndexList
Reid Spencerf8483652006-12-02 15:16:01 +00002142 : ',' ValueRefList { $$ = $2; }
2143 | /* empty */ { $$ = new ValueList(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00002144 ;
2145
2146OptVolatile
2147 : VOLATILE
2148 | /* empty */ { $$ = new std::string(); }
2149 ;
2150
2151MemoryInst : MALLOC Types OptCAlign {
Reid Spencer52402b02007-01-02 05:45:11 +00002152 *$1 += " " + $2->getNewTy();
Reid Spencere7c3c602006-11-30 06:36:44 +00002153 if (!$3->empty())
2154 *$1 += " " + *$3;
Reid Spencer30d0c582007-01-15 00:26:18 +00002155 $$ = new ValueInfo;
2156 $$->val = $1;
2157 $$->type = $2->getPointerType();
Reid Spencer319a7302007-01-05 17:20:02 +00002158 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002159 }
2160 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencer30d0c582007-01-15 00:26:18 +00002161 std::string Name = getUniqueName($5->val, $4);
Reid Spencer52402b02007-01-02 05:45:11 +00002162 *$1 += " " + $2->getNewTy() + ", " + $4->getNewTy() + " " + Name;
Reid Spencere7c3c602006-11-30 06:36:44 +00002163 if (!$6->empty())
2164 *$1 += " " + *$6;
Reid Spencer30d0c582007-01-15 00:26:18 +00002165 $$ = new ValueInfo;
2166 $$->val = $1;
2167 $$->type = $2->getPointerType();
2168 delete $5; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00002169 }
2170 | ALLOCA Types OptCAlign {
Reid Spencer52402b02007-01-02 05:45:11 +00002171 *$1 += " " + $2->getNewTy();
Reid Spencere7c3c602006-11-30 06:36:44 +00002172 if (!$3->empty())
2173 *$1 += " " + *$3;
Reid Spencer30d0c582007-01-15 00:26:18 +00002174 $$ = new ValueInfo;
2175 $$->val = $1;
2176 $$->type = $2->getPointerType();
Reid Spencer319a7302007-01-05 17:20:02 +00002177 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002178 }
2179 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencer30d0c582007-01-15 00:26:18 +00002180 std::string Name = getUniqueName($5->val, $4);
Reid Spencer52402b02007-01-02 05:45:11 +00002181 *$1 += " " + $2->getNewTy() + ", " + $4->getNewTy() + " " + Name;
Reid Spencere7c3c602006-11-30 06:36:44 +00002182 if (!$6->empty())
2183 *$1 += " " + *$6;
Reid Spencer30d0c582007-01-15 00:26:18 +00002184 $$ = $5;
2185 delete $$->val;
2186 $$->val = $1;
2187 $$->type = $2->getPointerType();
2188 delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00002189 }
2190 | FREE ResolvedVal {
Reid Spencer30d0c582007-01-15 00:26:18 +00002191 *$1 += " " + *$2->val;
2192 $$ = $2;
2193 delete $2->val;
2194 $$->val = $1;
2195 $$->type = TypeInfo::get("void", VoidTy);
Reid Spencere7c3c602006-11-30 06:36:44 +00002196 }
2197 | OptVolatile LOAD Types ValueRef {
Reid Spencer30d0c582007-01-15 00:26:18 +00002198 std::string Name = getUniqueName($4->val, $3);
Reid Spencere7c3c602006-11-30 06:36:44 +00002199 if (!$1->empty())
2200 *$1 += " ";
Reid Spencer52402b02007-01-02 05:45:11 +00002201 *$1 += *$2 + " " + $3->getNewTy() + " " + Name;
Reid Spencer30d0c582007-01-15 00:26:18 +00002202 $$ = $4;
2203 delete $$->val;
2204 $$->val = $1;
2205 $$->type = $3->getElementType();
2206 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00002207 }
2208 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
Reid Spencer30d0c582007-01-15 00:26:18 +00002209 std::string Name = getUniqueName($6->val, $5);
Reid Spencere7c3c602006-11-30 06:36:44 +00002210 if (!$1->empty())
2211 *$1 += " ";
Reid Spencer30d0c582007-01-15 00:26:18 +00002212 *$1 += *$2 + " " + *$3->val + ", " + $5->getNewTy() + " " + Name;
2213 $$ = $3;
2214 delete $$->val;
2215 $$->val = $1;
2216 $$->type = TypeInfo::get("void", VoidTy);
2217 delete $2; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00002218 }
2219 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer30d0c582007-01-15 00:26:18 +00002220 std::string Name = getUniqueName($3->val, $2);
Reid Spencerf459d392006-12-02 16:19:52 +00002221 // Upgrade the indices
2222 for (unsigned i = 0; i < $4->size(); ++i) {
Reid Spencer30d0c582007-01-15 00:26:18 +00002223 ValueInfo* VI = (*$4)[i];
2224 if (VI->type->isUnsigned() && !VI->isConstant() &&
2225 VI->type->getBitWidth() < 64) {
2226 *O << " %gep_upgrade" << UniqueNameCounter << " = zext " << *VI->val
Reid Spencer71d2ec92006-12-31 06:02:26 +00002227 << " to i64\n";
Reid Spencer30d0c582007-01-15 00:26:18 +00002228 *VI->val = "i64 %gep_upgrade" + llvm::utostr(UniqueNameCounter++);
2229 VI->type = TypeInfo::get("i64",ULongTy);
Reid Spencerf459d392006-12-02 16:19:52 +00002230 }
2231 }
Reid Spencer52402b02007-01-02 05:45:11 +00002232 *$1 += " " + $2->getNewTy() + " " + Name;
Reid Spencerf8483652006-12-02 15:16:01 +00002233 for (unsigned i = 0; i < $4->size(); ++i) {
Reid Spencer30d0c582007-01-15 00:26:18 +00002234 ValueInfo* VI = (*$4)[i];
2235 *$1 += ", " + *VI->val;
Reid Spencerf8483652006-12-02 15:16:01 +00002236 }
Reid Spencer30d0c582007-01-15 00:26:18 +00002237 $$ = $3;
2238 delete $$->val;
2239 $$->val = $1;
2240 $$->type = getGEPIndexedType($2,$4);
2241 for (unsigned i = 0; i < $4->size(); ++i)
2242 delete (*$4)[i];
2243 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00002244 };
2245
2246%%
2247
2248int yyerror(const char *ErrorMsg) {
2249 std::string where
2250 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2251 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
Reid Spencer319a7302007-01-05 17:20:02 +00002252 std::string errMsg = where + "error: " + std::string(ErrorMsg) +
2253 " while reading ";
Reid Spencere7c3c602006-11-30 06:36:44 +00002254 if (yychar == YYEMPTY || yychar == 0)
2255 errMsg += "end-of-file.";
2256 else
2257 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
Reid Spencer71d2ec92006-12-31 06:02:26 +00002258 std::cerr << "llvm-upgrade: " << errMsg << '\n';
Chris Lattner37e01c52007-01-04 18:46:42 +00002259 *O << "llvm-upgrade parse failed.\n";
Reid Spencere7c3c602006-11-30 06:36:44 +00002260 exit(1);
2261}
Reid Spencer319a7302007-01-05 17:20:02 +00002262
Reid Spencer30d0c582007-01-15 00:26:18 +00002263void warning(const std::string& ErrorMsg) {
Reid Spencer319a7302007-01-05 17:20:02 +00002264 std::string where
2265 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2266 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
2267 std::string errMsg = where + "warning: " + std::string(ErrorMsg) +
2268 " while reading ";
2269 if (yychar == YYEMPTY || yychar == 0)
2270 errMsg += "end-of-file.";
2271 else
2272 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
2273 std::cerr << "llvm-upgrade: " << errMsg << '\n';
2274}