blob: da0772c3bffdb9123afbe1c5f3d3f390e410e636 [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
Reid Spencerb6673a92007-01-15 02:41:46 +000062/// of creating llvm::Value directly, the parser will create Value which
Reid Spencer30d0c582007-01-15 00:26:18 +000063/// associates a Value* with a Signedness indication.
Reid Spencerb6673a92007-01-15 02:41:46 +000064struct Value {
Reid Spencer30d0c582007-01-15 00:26:18 +000065 std::string* val;
Reid Spencerb6673a92007-01-15 02:41:46 +000066 const Type* type;
Reid Spencer30d0c582007-01-15 00:26:18 +000067 bool constant;
68 bool isConstant() const { return constant; }
Reid Spencerb6673a92007-01-15 02:41:46 +000069 ~Value() { delete val; }
Reid Spencer30d0c582007-01-15 00:26:18 +000070};
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
Reid Spencerb6673a92007-01-15 02:41:46 +000075/// create instances of Type which retains the signedness indication so
Reid Spencer30d0c582007-01-15 00:26:18 +000076/// 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".
Reid Spencerb6673a92007-01-15 02:41:46 +000080class Type {
Reid Spencer30d0c582007-01-15 00:26:18 +000081public:
Reid Spencerb6673a92007-01-15 02:41:46 +000082 static const Type* get(const std::string &newType, TypeIDs oldType);
83 static const Type* get(const std::string& newType, TypeIDs oldType,
84 const Type* eTy, const Type* rTy);
Reid Spencer30d0c582007-01-15 00:26:18 +000085
Reid Spencerb6673a92007-01-15 02:41:46 +000086 static const Type* get(const std::string& newType, TypeIDs oldType,
87 const Type *eTy, uint64_t elems);
Reid Spencer30d0c582007-01-15 00:26:18 +000088
Reid Spencerb6673a92007-01-15 02:41:46 +000089 static const Type* get(const std::string& newType, TypeIDs oldType,
Reid Spencer30d0c582007-01-15 00:26:18 +000090 TypeList* TL);
91
Reid Spencerb6673a92007-01-15 02:41:46 +000092 static const Type* get(const std::string& newType, const Type* resTy,
Reid Spencer30d0c582007-01-15 00:26:18 +000093 TypeList* TL);
94
Reid Spencerb6673a92007-01-15 02:41:46 +000095 const Type* resolve() const;
96 bool operator<(const Type& that) const;
Reid Spencer30d0c582007-01-15 00:26:18 +000097
Reid Spencerb6673a92007-01-15 02:41:46 +000098 bool sameNewTyAs(const Type* that) const {
Reid Spencer30d0c582007-01-15 00:26:18 +000099 return this->newTy == that->newTy;
100 }
101
Reid Spencerb6673a92007-01-15 02:41:46 +0000102 bool sameOldTyAs(const Type* that) const;
Reid Spencer30d0c582007-01-15 00:26:18 +0000103
Reid Spencerb6673a92007-01-15 02:41:46 +0000104 TypeIDs getElementTy() const {
Reid Spencer30d0c582007-01-15 00:26:18 +0000105 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
Reid Spencerb6673a92007-01-15 02:41:46 +0000116 typedef std::vector<const Type*> UpRefStack;
Reid Spencer30d0c582007-01-15 00:26:18 +0000117 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; }
Reid Spencerb6673a92007-01-15 02:41:46 +0000121 const Type* getResultType() const { return resultTy; }
122 const Type* getElementType() const { return elemTy; }
Reid Spencer30d0c582007-01-15 00:26:18 +0000123
Reid Spencerb6673a92007-01-15 02:41:46 +0000124 const Type* getPointerType() const {
125 return get(newTy + "*", PointerTy, this, (Type*)0);
Reid Spencer30d0c582007-01-15 00:26:18 +0000126 }
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
Reid Spencerb6673a92007-01-15 02:41:46 +0000164 const Type* getIndexedType(const Value* V) const;
Reid Spencer30d0c582007-01-15 00:26:18 +0000165
166 unsigned getNumStructElements() const {
167 return (elements ? elements->size() : 0);
168 }
169
Reid Spencerb6673a92007-01-15 02:41:46 +0000170 const Type* getElement(unsigned idx) const {
Reid Spencer30d0c582007-01-15 00:26:18 +0000171 if (elements)
172 if (idx < elements->size())
173 return (*elements)[idx];
174 return 0;
175 }
176
177private:
Reid Spencerb6673a92007-01-15 02:41:46 +0000178 Type()
Reid Spencer30d0c582007-01-15 00:26:18 +0000179 : newTy(), oldTy(UnresolvedTy), elemTy(0), resultTy(0), elements(0),
180 nelems(0) {
181 }
182
Reid Spencerb6673a92007-01-15 02:41:46 +0000183 Type(const Type& that); // do not implement
184 Type& operator=(const Type& that); // do not implement
Reid Spencer30d0c582007-01-15 00:26:18 +0000185
Reid Spencerb6673a92007-01-15 02:41:46 +0000186 ~Type() { delete elements; }
Reid Spencer30d0c582007-01-15 00:26:18 +0000187
188 struct ltfunctor
189 {
Reid Spencerb6673a92007-01-15 02:41:46 +0000190 bool operator()(const Type* X, const Type* Y) const {
Reid Spencer30d0c582007-01-15 00:26:18 +0000191 assert(X && "Can't compare null pointer");
192 assert(Y && "Can't compare null pointer");
193 return *X < *Y;
194 }
195 };
196
Reid Spencerb6673a92007-01-15 02:41:46 +0000197 typedef std::set<const Type*, ltfunctor> TypeRegMap;
Reid Spencer30d0c582007-01-15 00:26:18 +0000198
Reid Spencerb6673a92007-01-15 02:41:46 +0000199 static const Type* add_new_type(Type* existing);
Reid Spencer30d0c582007-01-15 00:26:18 +0000200
201 std::string newTy;
Reid Spencerb6673a92007-01-15 02:41:46 +0000202 TypeIDs oldTy;
203 Type *elemTy;
204 Type *resultTy;
Reid Spencer30d0c582007-01-15 00:26:18 +0000205 TypeList *elements;
206 uint64_t nelems;
207 static TypeRegMap registry;
208public:
Reid Spencerb6673a92007-01-15 02:41:46 +0000209 typedef std::vector<const Type*> TypeVector;
210 typedef std::map<std::string,const Type*> TypeMap;
211 typedef std::map<const Type*,std::string> TypePlaneMap;
Reid Spencer30d0c582007-01-15 00:26:18 +0000212 typedef std::map<std::string,TypePlaneMap> GlobalsTypeMap;
213 static TypeVector EnumeratedTypes;
214 static TypeMap NamedTypes;
215 static GlobalsTypeMap Globals;
216};
217
Reid Spencerb6673a92007-01-15 02:41:46 +0000218Type::TypeRegMap Type::registry;
219Type::TypeVector Type::EnumeratedTypes;
220Type::TypeMap Type::NamedTypes;
221Type::GlobalsTypeMap Type::Globals;
Reid Spencer319a7302007-01-05 17:20:02 +0000222
Reid Spencerb6673a92007-01-15 02:41:46 +0000223const Type* Type::get(const std::string &newType, TypeIDs oldType) {
224 Type* Ty = new Type();
Reid Spencer319a7302007-01-05 17:20:02 +0000225 Ty->newTy = newType;
226 Ty->oldTy = oldType;
227 return add_new_type(Ty);
228}
229
Reid Spencerb6673a92007-01-15 02:41:46 +0000230const Type* Type::get(const std::string& newType, TypeIDs oldType,
231 const Type* eTy, const Type* rTy) {
232 Type* Ty= new Type();
Reid Spencer319a7302007-01-05 17:20:02 +0000233 Ty->newTy = newType;
234 Ty->oldTy = oldType;
Reid Spencerb6673a92007-01-15 02:41:46 +0000235 Ty->elemTy = const_cast<Type*>(eTy);
236 Ty->resultTy = const_cast<Type*>(rTy);
Reid Spencer319a7302007-01-05 17:20:02 +0000237 return add_new_type(Ty);
238}
239
Reid Spencerb6673a92007-01-15 02:41:46 +0000240const Type* Type::get(const std::string& newType, TypeIDs oldType,
241 const Type *eTy, uint64_t elems) {
242 Type* Ty = new Type();
Reid Spencer319a7302007-01-05 17:20:02 +0000243 Ty->newTy = newType;
244 Ty->oldTy = oldType;
Reid Spencerb6673a92007-01-15 02:41:46 +0000245 Ty->elemTy = const_cast<Type*>(eTy);
Reid Spencer319a7302007-01-05 17:20:02 +0000246 Ty->nelems = elems;
247 return add_new_type(Ty);
248}
249
Reid Spencerb6673a92007-01-15 02:41:46 +0000250const Type* Type::get(const std::string& newType, TypeIDs oldType,
Reid Spencer319a7302007-01-05 17:20:02 +0000251 TypeList* TL) {
Reid Spencerb6673a92007-01-15 02:41:46 +0000252 Type* Ty = new Type();
Reid Spencer319a7302007-01-05 17:20:02 +0000253 Ty->newTy = newType;
254 Ty->oldTy = oldType;
255 Ty->elements = TL;
256 return add_new_type(Ty);
257}
258
Reid Spencerb6673a92007-01-15 02:41:46 +0000259const Type* Type::get(const std::string& newType, const Type* resTy,
Reid Spencer319a7302007-01-05 17:20:02 +0000260 TypeList* TL) {
Reid Spencerb6673a92007-01-15 02:41:46 +0000261 Type* Ty = new Type();
Reid Spencer319a7302007-01-05 17:20:02 +0000262 Ty->newTy = newType;
263 Ty->oldTy = FunctionTy;
Reid Spencerb6673a92007-01-15 02:41:46 +0000264 Ty->resultTy = const_cast<Type*>(resTy);
Reid Spencer319a7302007-01-05 17:20:02 +0000265 Ty->elements = TL;
266 return add_new_type(Ty);
267}
268
Reid Spencerb6673a92007-01-15 02:41:46 +0000269const Type* Type::resolve() const {
Reid Spencer319a7302007-01-05 17:20:02 +0000270 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 Spencerb6673a92007-01-15 02:41:46 +0000281 Type::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
Reid Spencerb6673a92007-01-15 02:41:46 +0000295bool Type::operator<(const Type& that) const {
Reid Spencer319a7302007-01-05 17:20:02 +0000296 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: {
Reid Spencerb6673a92007-01-15 02:41:46 +0000311 const Type* thisTy = this->elemTy;
312 const Type* thatTy = that.elemTy;
Reid Spencer319a7302007-01-05 17:20:02 +0000313 return *thisTy < *thatTy;
314 }
315 case FunctionTy: {
Reid Spencerb6673a92007-01-15 02:41:46 +0000316 const Type* thisTy = this->resultTy;
317 const Type* thatTy = that.resultTy;
Reid Spencer319a7302007-01-05 17:20:02 +0000318 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++) {
Reid Spencerb6673a92007-01-15 02:41:46 +0000327 const Type* thisTy = (*this->elements)[i];
328 const Type* thatTy = (*that.elements)[i];
Reid Spencer319a7302007-01-05 17:20:02 +0000329 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
Reid Spencerb6673a92007-01-15 02:41:46 +0000342bool Type::sameOldTyAs(const Type* that) const {
Reid Spencer319a7302007-01-05 17:20:02 +0000343 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: {
Reid Spencerb6673a92007-01-15 02:41:46 +0000356 const Type* thisTy = this->elemTy;
357 const Type* thatTy = that->elemTy;
Reid Spencer319a7302007-01-05 17:20:02 +0000358 return thisTy->sameOldTyAs(thatTy);
359 }
360 case FunctionTy: {
Reid Spencerb6673a92007-01-15 02:41:46 +0000361 const Type* thisTy = this->resultTy;
362 const Type* thatTy = that->resultTy;
Reid Spencer319a7302007-01-05 17:20:02 +0000363 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++) {
Reid Spencerb6673a92007-01-15 02:41:46 +0000372 const Type* thisTy = (*this->elements)[i];
373 const Type* thatTy = (*that->elements)[i];
Reid Spencer319a7302007-01-05 17:20:02 +0000374 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
Reid Spencerb6673a92007-01-15 02:41:46 +0000387bool Type::isUnresolvedDeep() const {
Reid Spencer319a7302007-01-05 17:20:02 +0000388 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
Reid Spencerb6673a92007-01-15 02:41:46 +0000406unsigned Type::getBitWidth() const {
Reid Spencer319a7302007-01-05 17:20:02 +0000407 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 Spencerb6673a92007-01-15 02:41:46 +0000431const Type* Type::getIndexedType(const Value* V) const {
Reid Spencer319a7302007-01-05 17:20:02 +0000432 if (isStruct()) {
Reid Spencerb6673a92007-01-15 02:41:46 +0000433 if (V->isConstant() && V->type->isInteger()) {
434 size_t pos = V->val->find(' ') + 1;
435 if (pos < V->val->size()) {
436 uint64_t idx = atoi(V->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 Spencerb6673a92007-01-15 02:41:46 +0000453void Type::getSignedness(unsigned &sNum, unsigned &uNum,
Reid Spencerf8383de2007-01-06 06:04:32 +0000454 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: {
Reid Spencerb6673a92007-01-15 02:41:46 +0000481 const Type* Ty = this->resolve();
Reid Spencerf8383de2007-01-06 06:04:32 +0000482 // 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
Reid Spencerb6673a92007-01-15 02:41:46 +0000502std::string Type::makeUniqueName(const std::string& BaseName) const {
Reid Spencerf8383de2007-01-06 06:04:32 +0000503 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: {
Reid Spencerb6673a92007-01-15 02:41:46 +0000523 Type::UpRefStack stack;
Reid Spencerf8383de2007-01-06 06:04:32 +0000524 elemTy->resolve()->getSignedness(sNum, uNum, stack);
525 break;
526 }
527 case StructTy:
528 case PackedStructTy: {
529 for (unsigned i = 0; i < elements->size(); i++) {
Reid Spencerb6673a92007-01-15 02:41:46 +0000530 Type::UpRefStack stack;
Reid Spencerf8383de2007-01-06 06:04:32 +0000531 (*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 Spencerb6673a92007-01-15 02:41:46 +0000557Type& Type::operator=(const Type& that) {
Reid Spencer319a7302007-01-05 17:20:02 +0000558 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
Reid Spencerb6673a92007-01-15 02:41:46 +0000572const Type* Type::add_new_type(Type* newTy) {
Reid Spencer319a7302007-01-05 17:20:02 +0000573 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 Spencerb6673a92007-01-15 02:41:46 +0000582class Instruction {
583};
584
Reid Spencer30d0c582007-01-15 00:26:18 +0000585/// This type is used to keep track of the signedness of constants.
Reid Spencerb6673a92007-01-15 02:41:46 +0000586struct Constant {
Reid Spencer30d0c582007-01-15 00:26:18 +0000587 std::string *cnst;
Reid Spencerb6673a92007-01-15 02:41:46 +0000588 const Type *type;
589 ~Constant() { delete cnst; }
Reid Spencer30d0c582007-01-15 00:26:18 +0000590};
591
592/// This variable provides a counter for unique names. It is used in various
593/// productions to ensure a unique name is generated.
594static uint64_t UniqueNameCounter = 1;
595
596// This is set when a DECLARE keyword is recognized so that subsequent parsing
597// of a function prototype can know if its a declaration or definition.
598static bool isDeclare = false;
599
600// This bool is used to communicate between the InstVal and Inst rules about
601// whether or not a cast should be deleted. When the flag is set, InstVal has
602// determined that the cast is a candidate. However, it can only be deleted if
603// the value being casted is the same value name as the instruction. The Inst
604// rule makes that comparison if the flag is set and comments out the
605// instruction if they match.
606static bool deleteUselessCastFlag = false;
607static std::string* deleteUselessCastName = 0;
608
609
610
Reid Spencerb6673a92007-01-15 02:41:46 +0000611const char* getCastOpcode(std::string& Source, const Type* SrcTy,
612 const Type* DstTy) {
Reid Spencer52402b02007-01-02 05:45:11 +0000613 unsigned SrcBits = SrcTy->getBitWidth();
614 unsigned DstBits = DstTy->getBitWidth();
Reid Spencere77e35e2006-12-01 20:26:20 +0000615 const char* opcode = "bitcast";
616 // Run through the possibilities ...
Reid Spencer52402b02007-01-02 05:45:11 +0000617 if (DstTy->isIntegral()) { // Casting to integral
618 if (SrcTy->isIntegral()) { // Casting from integral
Reid Spencere77e35e2006-12-01 20:26:20 +0000619 if (DstBits < SrcBits)
620 opcode = "trunc";
621 else if (DstBits > SrcBits) { // its an extension
Reid Spencer52402b02007-01-02 05:45:11 +0000622 if (SrcTy->isSigned())
Reid Spencere77e35e2006-12-01 20:26:20 +0000623 opcode ="sext"; // signed -> SEXT
624 else
625 opcode = "zext"; // unsigned -> ZEXT
626 } else {
627 opcode = "bitcast"; // Same size, No-op cast
628 }
Reid Spencer52402b02007-01-02 05:45:11 +0000629 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
630 if (DstTy->isSigned())
Reid Spencere77e35e2006-12-01 20:26:20 +0000631 opcode = "fptosi"; // FP -> sint
632 else
633 opcode = "fptoui"; // FP -> uint
Reid Spencer52402b02007-01-02 05:45:11 +0000634 } else if (SrcTy->isPacked()) {
635 assert(DstBits == SrcTy->getBitWidth() &&
Reid Spencere77e35e2006-12-01 20:26:20 +0000636 "Casting packed to integer of different width");
637 opcode = "bitcast"; // same size, no-op cast
638 } else {
Reid Spencer52402b02007-01-02 05:45:11 +0000639 assert(SrcTy->isPointer() &&
Reid Spencere77e35e2006-12-01 20:26:20 +0000640 "Casting from a value that is not first-class type");
641 opcode = "ptrtoint"; // ptr -> int
642 }
Reid Spencer52402b02007-01-02 05:45:11 +0000643 } else if (DstTy->isFloatingPoint()) { // Casting to floating pt
644 if (SrcTy->isIntegral()) { // Casting from integral
645 if (SrcTy->isSigned())
Reid Spencere77e35e2006-12-01 20:26:20 +0000646 opcode = "sitofp"; // sint -> FP
647 else
648 opcode = "uitofp"; // uint -> FP
Reid Spencer52402b02007-01-02 05:45:11 +0000649 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencere77e35e2006-12-01 20:26:20 +0000650 if (DstBits < SrcBits) {
651 opcode = "fptrunc"; // FP -> smaller FP
652 } else if (DstBits > SrcBits) {
653 opcode = "fpext"; // FP -> larger FP
654 } else {
655 opcode ="bitcast"; // same size, no-op cast
656 }
Reid Spencer52402b02007-01-02 05:45:11 +0000657 } else if (SrcTy->isPacked()) {
658 assert(DstBits == SrcTy->getBitWidth() &&
Reid Spencere77e35e2006-12-01 20:26:20 +0000659 "Casting packed to floating point of different width");
660 opcode = "bitcast"; // same size, no-op cast
661 } else {
662 assert(0 && "Casting pointer or non-first class to float");
663 }
Reid Spencer52402b02007-01-02 05:45:11 +0000664 } else if (DstTy->isPacked()) {
665 if (SrcTy->isPacked()) {
666 assert(DstTy->getBitWidth() == SrcTy->getBitWidth() &&
Reid Spencere77e35e2006-12-01 20:26:20 +0000667 "Casting packed to packed of different widths");
668 opcode = "bitcast"; // packed -> packed
Reid Spencer52402b02007-01-02 05:45:11 +0000669 } else if (DstTy->getBitWidth() == SrcBits) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000670 opcode = "bitcast"; // float/int -> packed
671 } else {
672 assert(!"Illegal cast to packed (wrong type or size)");
673 }
Reid Spencer52402b02007-01-02 05:45:11 +0000674 } else if (DstTy->isPointer()) {
675 if (SrcTy->isPointer()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000676 opcode = "bitcast"; // ptr -> ptr
Reid Spencer52402b02007-01-02 05:45:11 +0000677 } else if (SrcTy->isIntegral()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000678 opcode = "inttoptr"; // int -> ptr
679 } else {
Reid Spencera50d5962006-12-02 04:11:07 +0000680 assert(!"Casting invalid type to pointer");
Reid Spencere77e35e2006-12-01 20:26:20 +0000681 }
682 } else {
683 assert(!"Casting to type that is not first-class");
684 }
685 return opcode;
686}
687
Reid Spencerb6673a92007-01-15 02:41:46 +0000688std::string getCastUpgrade(const std::string& Src, const Type* SrcTy,
689 const Type* DstTy, bool isConst) {
Reid Spencera50d5962006-12-02 04:11:07 +0000690 std::string Result;
691 std::string Source = Src;
Reid Spencer52402b02007-01-02 05:45:11 +0000692 if (SrcTy->isFloatingPoint() && DstTy->isPointer()) {
Reid Spencera50d5962006-12-02 04:11:07 +0000693 // fp -> ptr cast is no longer supported but we must upgrade this
694 // by doing a double cast: fp -> int -> ptr
695 if (isConst)
Reid Spencer71d2ec92006-12-31 06:02:26 +0000696 Source = "i64 fptoui(" + Source + " to i64)";
Reid Spencera50d5962006-12-02 04:11:07 +0000697 else {
Reid Spencerb6673a92007-01-15 02:41:46 +0000698 *O << " %cast_upgrade" << UniqueNameCounter << " = fptoui "
Reid Spencer30d0c582007-01-15 00:26:18 +0000699 << Source << " to i64\n";
Reid Spencerb6673a92007-01-15 02:41:46 +0000700 Source = "i64 %cast_upgrade" + llvm::utostr(UniqueNameCounter++);
Reid Spencera50d5962006-12-02 04:11:07 +0000701 }
702 // Update the SrcTy for the getCastOpcode call below
Reid Spencerb6673a92007-01-15 02:41:46 +0000703 SrcTy = Type::get("i64", ULongTy);
Reid Spencer52402b02007-01-02 05:45:11 +0000704 } else if (DstTy->isBool()) {
705 // cast type %x to bool was previously defined as setne type %x, null
706 // The cast semantic is now to truncate, not compare so we must retain
707 // the original intent by replacing the cast with a setne
708 const char* comparator = SrcTy->isPointer() ? ", null" :
709 (SrcTy->isFloatingPoint() ? ", 0.0" :
710 (SrcTy->isBool() ? ", false" : ", 0"));
711 const char* compareOp = SrcTy->isFloatingPoint() ? "fcmp one " : "icmp ne ";
Reid Spencer187ccf82006-12-09 16:57:22 +0000712 if (isConst) {
713 Result = "(" + Source + comparator + ")";
714 Result = compareOp + Result;
715 } else
716 Result = compareOp + Source + comparator;
Reid Spencera50d5962006-12-02 04:11:07 +0000717 return Result; // skip cast processing below
718 }
Reid Spencer319a7302007-01-05 17:20:02 +0000719 SrcTy = SrcTy->resolve();
720 DstTy = DstTy->resolve();
Reid Spencera50d5962006-12-02 04:11:07 +0000721 std::string Opcode(getCastOpcode(Source, SrcTy, DstTy));
722 if (isConst)
Reid Spencer52402b02007-01-02 05:45:11 +0000723 Result += Opcode + "( " + Source + " to " + DstTy->getNewTy() + ")";
Reid Spencera50d5962006-12-02 04:11:07 +0000724 else
Reid Spencer52402b02007-01-02 05:45:11 +0000725 Result += Opcode + " " + Source + " to " + DstTy->getNewTy();
Reid Spencera50d5962006-12-02 04:11:07 +0000726 return Result;
727}
728
Reid Spencerb6673a92007-01-15 02:41:46 +0000729const char* getDivRemOpcode(const std::string& opcode, const Type* TI) {
Reid Spencer78720742006-12-02 20:21:22 +0000730 const char* op = opcode.c_str();
Reid Spencerb6673a92007-01-15 02:41:46 +0000731 const Type* Ty = TI->resolve();
Reid Spencer52402b02007-01-02 05:45:11 +0000732 if (Ty->isPacked())
733 Ty = Ty->getElementType();
Reid Spencer78720742006-12-02 20:21:22 +0000734 if (opcode == "div")
Reid Spencer52402b02007-01-02 05:45:11 +0000735 if (Ty->isFloatingPoint())
Reid Spencer78720742006-12-02 20:21:22 +0000736 op = "fdiv";
Reid Spencer52402b02007-01-02 05:45:11 +0000737 else if (Ty->isUnsigned())
Reid Spencer78720742006-12-02 20:21:22 +0000738 op = "udiv";
Reid Spencer52402b02007-01-02 05:45:11 +0000739 else if (Ty->isSigned())
Reid Spencer78720742006-12-02 20:21:22 +0000740 op = "sdiv";
741 else
742 yyerror("Invalid type for div instruction");
743 else if (opcode == "rem")
Reid Spencer52402b02007-01-02 05:45:11 +0000744 if (Ty->isFloatingPoint())
Reid Spencer78720742006-12-02 20:21:22 +0000745 op = "frem";
Reid Spencer52402b02007-01-02 05:45:11 +0000746 else if (Ty->isUnsigned())
Reid Spencer78720742006-12-02 20:21:22 +0000747 op = "urem";
Reid Spencer52402b02007-01-02 05:45:11 +0000748 else if (Ty->isSigned())
Reid Spencer78720742006-12-02 20:21:22 +0000749 op = "srem";
750 else
751 yyerror("Invalid type for rem instruction");
752 return op;
753}
Reid Spencer229e9362006-12-02 22:14:11 +0000754
Reid Spencerb6673a92007-01-15 02:41:46 +0000755std::string getCompareOp(const std::string& setcc, const Type* TI) {
Reid Spencer229e9362006-12-02 22:14:11 +0000756 assert(setcc.length() == 5);
757 char cc1 = setcc[3];
758 char cc2 = setcc[4];
759 assert(cc1 == 'e' || cc1 == 'n' || cc1 == 'l' || cc1 == 'g');
760 assert(cc2 == 'q' || cc2 == 'e' || cc2 == 'e' || cc2 == 't');
761 std::string result("xcmp xxx");
762 result[6] = cc1;
763 result[7] = cc2;
Reid Spencer52402b02007-01-02 05:45:11 +0000764 if (TI->isFloatingPoint()) {
Reid Spencer229e9362006-12-02 22:14:11 +0000765 result[0] = 'f';
Reid Spencere4d87aa2006-12-23 06:05:41 +0000766 result[5] = 'o';
Reid Spencerf0cf1322006-12-07 04:23:03 +0000767 if (cc1 == 'n')
768 result[5] = 'u'; // NE maps to unordered
769 else
770 result[5] = 'o'; // everything else maps to ordered
Reid Spencer52402b02007-01-02 05:45:11 +0000771 } else if (TI->isIntegral() || TI->isPointer()) {
Reid Spencer229e9362006-12-02 22:14:11 +0000772 result[0] = 'i';
773 if ((cc1 == 'e' && cc2 == 'q') || (cc1 == 'n' && cc2 == 'e'))
774 result.erase(5,1);
Reid Spencer52402b02007-01-02 05:45:11 +0000775 else if (TI->isSigned())
Reid Spencer229e9362006-12-02 22:14:11 +0000776 result[5] = 's';
Reid Spencer52402b02007-01-02 05:45:11 +0000777 else if (TI->isUnsigned() || TI->isPointer() || TI->isBool())
Reid Spencer229e9362006-12-02 22:14:11 +0000778 result[5] = 'u';
779 else
780 yyerror("Invalid integral type for setcc");
781 }
782 return result;
783}
784
Reid Spencerb6673a92007-01-15 02:41:46 +0000785const Type* getFunctionReturnType(const Type* PFTy) {
Reid Spencer319a7302007-01-05 17:20:02 +0000786 PFTy = PFTy->resolve();
Reid Spencer52402b02007-01-02 05:45:11 +0000787 if (PFTy->isPointer()) {
Reid Spencerb6673a92007-01-15 02:41:46 +0000788 const Type* ElemTy = PFTy->getElementType();
Reid Spencer319a7302007-01-05 17:20:02 +0000789 ElemTy = ElemTy->resolve();
Reid Spencer52402b02007-01-02 05:45:11 +0000790 if (ElemTy->isFunction())
Reid Spencer319a7302007-01-05 17:20:02 +0000791 return ElemTy->getResultType();
Reid Spencer52402b02007-01-02 05:45:11 +0000792 } else if (PFTy->isFunction()) {
Reid Spencer319a7302007-01-05 17:20:02 +0000793 return PFTy->getResultType();
Reid Spencer52402b02007-01-02 05:45:11 +0000794 }
Reid Spencer319a7302007-01-05 17:20:02 +0000795 return PFTy;
Reid Spencer52402b02007-01-02 05:45:11 +0000796}
797
Reid Spencerb6673a92007-01-15 02:41:46 +0000798const Type* ResolveUpReference(const Type* Ty,
799 Type::UpRefStack* stack) {
Reid Spencereff838e2007-01-03 23:45:42 +0000800 assert(Ty->isUpReference() && "Can't resolve a non-upreference");
Reid Spencer319a7302007-01-05 17:20:02 +0000801 unsigned upref = Ty->getUpRefNum();
Reid Spencereff838e2007-01-03 23:45:42 +0000802 assert(upref < stack->size() && "Invalid up reference");
803 return (*stack)[upref - stack->size() - 1];
804}
805
Reid Spencerb6673a92007-01-15 02:41:46 +0000806const Type* getGEPIndexedType(const Type* PTy, ValueList* idxs) {
807 const Type* Result = PTy = PTy->resolve();
Reid Spencer52402b02007-01-02 05:45:11 +0000808 assert(PTy->isPointer() && "GEP Operand is not a pointer?");
Reid Spencerb6673a92007-01-15 02:41:46 +0000809 Type::UpRefStack stack;
Reid Spencereff838e2007-01-03 23:45:42 +0000810 for (unsigned i = 0; i < idxs->size(); ++i) {
Reid Spencer52402b02007-01-02 05:45:11 +0000811 if (Result->isComposite()) {
812 Result = Result->getIndexedType((*idxs)[i]);
Reid Spencer319a7302007-01-05 17:20:02 +0000813 Result = Result->resolve();
Reid Spencereff838e2007-01-03 23:45:42 +0000814 stack.push_back(Result);
Reid Spencer52402b02007-01-02 05:45:11 +0000815 } else
816 yyerror("Invalid type for index");
817 }
Reid Spencereff838e2007-01-03 23:45:42 +0000818 // Resolve upreferences so we can return a more natural type
819 if (Result->isPointer()) {
820 if (Result->getElementType()->isUpReference()) {
821 stack.push_back(Result);
822 Result = ResolveUpReference(Result->getElementType(), &stack);
823 }
824 } else if (Result->isUpReference()) {
825 Result = ResolveUpReference(Result->getElementType(), &stack);
826 }
Reid Spencer52402b02007-01-02 05:45:11 +0000827 return Result->getPointerType();
828}
829
Reid Spencer52402b02007-01-02 05:45:11 +0000830// This function handles appending .u or .s to integer value names that
831// were previously unsigned or signed, respectively. This avoids name
832// collisions since the unsigned and signed type planes have collapsed
833// into a single signless type plane.
Reid Spencerb6673a92007-01-15 02:41:46 +0000834std::string getUniqueName(const std::string *Name, const Type* Ty,
Reid Spencer30d0c582007-01-15 00:26:18 +0000835 bool isGlobal = false, bool isDef = false) {
Reid Spencer319a7302007-01-05 17:20:02 +0000836
Reid Spencer52402b02007-01-02 05:45:11 +0000837 // If its not a symbolic name, don't modify it, probably a constant val.
838 if ((*Name)[0] != '%' && (*Name)[0] != '"')
839 return *Name;
Reid Spencer319a7302007-01-05 17:20:02 +0000840
Reid Spencer52402b02007-01-02 05:45:11 +0000841 // If its a numeric reference, just leave it alone.
842 if (isdigit((*Name)[1]))
843 return *Name;
844
845 // Resolve the type
Reid Spencer319a7302007-01-05 17:20:02 +0000846 Ty = Ty->resolve();
847
848 // If its a global name, get its uniquified name, if any
Reid Spencerb6673a92007-01-15 02:41:46 +0000849 Type::GlobalsTypeMap::iterator GI = Type::Globals.find(*Name);
850 if (GI != Type::Globals.end()) {
851 Type::TypePlaneMap::iterator TPI = GI->second.begin();
852 Type::TypePlaneMap::iterator TPE = GI->second.end();
Reid Spencer319a7302007-01-05 17:20:02 +0000853 for ( ; TPI != TPE ; ++TPI) {
854 if (TPI->first->sameNewTyAs(Ty))
855 return TPI->second;
856 }
857 }
858
859 if (isGlobal) {
860 // We didn't find a global name, but if its supposed to be global then all
861 // we can do is return the name. This is probably a forward reference of a
862 // global value that hasn't been defined yet. Since we have no definition
863 // we don't know its linkage class. Just assume its an external and the name
864 // shouldn't change.
865 return *Name;
866 }
Reid Spencer52402b02007-01-02 05:45:11 +0000867
868 // Default the result to the current name
Reid Spencerf8383de2007-01-06 06:04:32 +0000869 std::string Result = Ty->makeUniqueName(*Name);
Reid Spencer52402b02007-01-02 05:45:11 +0000870
Reid Spencer52402b02007-01-02 05:45:11 +0000871 return Result;
872}
873
Reid Spencer319a7302007-01-05 17:20:02 +0000874std::string getGlobalName(const std::string* Name, const std::string Linkage,
Reid Spencerb6673a92007-01-15 02:41:46 +0000875 const Type* Ty, bool isConstant) {
Reid Spencer319a7302007-01-05 17:20:02 +0000876 // Default to given name
877 std::string Result = *Name;
878 // Look up the name in the Globals Map
Reid Spencerb6673a92007-01-15 02:41:46 +0000879 Type::GlobalsTypeMap::iterator GI = Type::Globals.find(*Name);
Reid Spencer319a7302007-01-05 17:20:02 +0000880 // Did we see this global name before?
Reid Spencerb6673a92007-01-15 02:41:46 +0000881 if (GI != Type::Globals.end()) {
Reid Spencer319a7302007-01-05 17:20:02 +0000882 if (Ty->isUnresolvedDeep()) {
883 // The Gval's type is unresolved. Consequently, we can't disambiguate it
884 // by type. We'll just change its name and emit a warning.
885 warning("Cannot disambiguate global value '" + *Name +
886 "' because type '" + Ty->getNewTy() + "'is unresolved.\n");
887 Result = *Name + ".unique";
888 UniqueNameCounter++;
889 Result += llvm::utostr(UniqueNameCounter);
890 return Result;
891 } else {
Reid Spencerb6673a92007-01-15 02:41:46 +0000892 Type::TypePlaneMap::iterator TPI = GI->second.find(Ty);
Reid Spencer319a7302007-01-05 17:20:02 +0000893 if (TPI != GI->second.end()) {
894 // We found an existing name of the same old type. This isn't allowed
895 // in LLVM 2.0. Consequently, we must alter the name of the global so it
896 // can at least compile. References to the global will yield the first
897 // definition, which is okay. We also must warn about this.
898 Result = *Name + ".unique";
899 UniqueNameCounter++;
900 Result += llvm::utostr(UniqueNameCounter);
901 warning(std::string("Global variable '") + *Name + "' was renamed to '"+
902 Result + "'");
903 } else {
904 // There isn't an existing definition for this name according to the
905 // old types. Now search the TypePlanMap for types with the same new
906 // name.
Reid Spencerb6673a92007-01-15 02:41:46 +0000907 Type::TypePlaneMap::iterator TPI = GI->second.begin();
908 Type::TypePlaneMap::iterator TPE = GI->second.end();
Reid Spencer319a7302007-01-05 17:20:02 +0000909 for ( ; TPI != TPE; ++TPI) {
910 if (TPI->first->sameNewTyAs(Ty)) {
911 // The new types are the same but the old types are different so
912 // this is a global name collision resulting from type planes
913 // collapsing.
914 if (Linkage == "external" || Linkage == "dllimport" ||
915 Linkage == "extern_weak" || Linkage == "") {
916 // The linkage of this gval is external so we can't reliably
917 // rename it because it could potentially create a linking
918 // problem. However, we can't leave the name conflict in the
919 // output either or it won't assemble with LLVM 2.0. So, all we
920 // can do is rename this one to something unique and emit a
921 // warning about the problem.
922 Result = *Name + ".unique";
923 UniqueNameCounter++;
924 Result += llvm::utostr(UniqueNameCounter);
925 warning("Renaming global value '" + *Name + "' to '" + Result +
926 "' may cause linkage errors.");
927 return Result;
928 } else {
929 // Its linkage is internal and its type is known so we can
930 // disambiguate the name collision successfully based on the type.
931 Result = getUniqueName(Name, Ty);
932 TPI->second = Result;
933 return Result;
934 }
935 }
936 }
937 // We didn't find an entry in the type plane with the same new type and
938 // the old types differ so this is a new type plane for this global
939 // variable. We just fall through to the logic below which inserts
940 // the global.
941 }
942 }
943 }
944
945 // Its a new global name, if it is external we can't change it
946 if (isConstant || Linkage == "external" || Linkage == "dllimport" ||
947 Linkage == "extern_weak" || Linkage == "") {
Reid Spencerb6673a92007-01-15 02:41:46 +0000948 Type::Globals[Result][Ty] = Result;
Reid Spencer319a7302007-01-05 17:20:02 +0000949 return Result;
950 }
951
952 // Its a new global name, and it is internal, change the name to make it
953 // unique for its type.
954 // Result = getUniqueName(Name, Ty);
Reid Spencerb6673a92007-01-15 02:41:46 +0000955 Type::Globals[*Name][Ty] = Result;
Reid Spencer319a7302007-01-05 17:20:02 +0000956 return Result;
957}
Reid Spencer30d0c582007-01-15 00:26:18 +0000958
959} // End anonymous namespace
960
Reid Spencerb6673a92007-01-15 02:41:46 +0000961// This function is used by the Lexer to create a Type. It can't be
Reid Spencer30d0c582007-01-15 00:26:18 +0000962// in the anonymous namespace.
Reid Spencerb6673a92007-01-15 02:41:46 +0000963const Type* getType(const std::string& newTy, TypeIDs oldTy) {
964 return Type::get(newTy, oldTy);
Reid Spencer30d0c582007-01-15 00:26:18 +0000965}
966
Reid Spencere7c3c602006-11-30 06:36:44 +0000967%}
968
Reid Spencerf0cf1322006-12-07 04:23:03 +0000969// %file-prefix="UpgradeParser"
Reid Spencere77e35e2006-12-01 20:26:20 +0000970
971%union {
972 std::string* String;
Reid Spencerb6673a92007-01-15 02:41:46 +0000973 const Type* Ty;
974 Value* Val;
975 Constant* Const;
Reid Spencerf8483652006-12-02 15:16:01 +0000976 ValueList* ValList;
Reid Spencer52402b02007-01-02 05:45:11 +0000977 TypeList* TypeVec;
Reid Spencere77e35e2006-12-01 20:26:20 +0000978}
979
Reid Spencerb6673a92007-01-15 02:41:46 +0000980%token <Ty> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
981%token <Ty> FLOAT DOUBLE LABEL
Reid Spencera50d5962006-12-02 04:11:07 +0000982%token <String> OPAQUE ESINT64VAL EUINT64VAL SINTVAL UINTVAL FPVAL
Reid Spencerf2d55322006-12-01 21:52:30 +0000983%token <String> NULL_TOK UNDEF ZEROINITIALIZER TRUETOK FALSETOK
Reid Spencere77e35e2006-12-01 20:26:20 +0000984%token <String> TYPE VAR_ID LABELSTR STRINGCONSTANT
985%token <String> IMPLEMENTATION BEGINTOK ENDTOK
Reid Spencer71d2ec92006-12-31 06:02:26 +0000986%token <String> DECLARE GLOBAL CONSTANT SECTION VOLATILE
Reid Spencere77e35e2006-12-01 20:26:20 +0000987%token <String> TO DOTDOTDOT CONST INTERNAL LINKONCE WEAK
988%token <String> DLLIMPORT DLLEXPORT EXTERN_WEAK APPENDING
Reid Spencerc4d96252007-01-13 00:03:30 +0000989%token <String> EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
Reid Spencer78720742006-12-02 20:21:22 +0000990%token <String> ALIGN UNINITIALIZED
Reid Spencere77e35e2006-12-01 20:26:20 +0000991%token <String> DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
992%token <String> CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
993%token <String> X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
994%token <String> DATALAYOUT
Reid Spencer78720742006-12-02 20:21:22 +0000995%token <String> RET BR SWITCH INVOKE EXCEPT UNWIND UNREACHABLE
996%token <String> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM AND OR XOR
Reid Spencere77e35e2006-12-01 20:26:20 +0000997%token <String> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
Reid Spencer229e9362006-12-02 22:14:11 +0000998%token <String> ICMP FCMP EQ NE SLT SGT SLE SGE OEQ ONE OLT OGT OLE OGE
999%token <String> ORD UNO UEQ UNE ULT UGT ULE UGE
Reid Spencere77e35e2006-12-01 20:26:20 +00001000%token <String> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Reid Spencerf7bde222006-12-01 22:26:37 +00001001%token <String> PHI_TOK SELECT SHL SHR ASHR LSHR VAARG
Reid Spencere77e35e2006-12-01 20:26:20 +00001002%token <String> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Reid Spencerfcb5df82006-12-01 22:34:43 +00001003%token <String> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI UITOFP SITOFP
1004%token <String> PTRTOINT INTTOPTR BITCAST
Reid Spencere77e35e2006-12-01 20:26:20 +00001005
1006%type <String> OptAssign OptLinkage OptCallingConv OptAlign OptCAlign
1007%type <String> SectionString OptSection GlobalVarAttributes GlobalVarAttribute
Reid Spencer52402b02007-01-02 05:45:11 +00001008%type <String> ConstExpr DefinitionList
Reid Spencere77e35e2006-12-01 20:26:20 +00001009%type <String> ConstPool TargetDefinition LibrariesDefinition LibList OptName
1010%type <String> ArgVal ArgListH ArgList FunctionHeaderH BEGIN FunctionHeader END
Reid Spencer52402b02007-01-02 05:45:11 +00001011%type <String> Function FunctionProto BasicBlock
1012%type <String> InstructionList BBTerminatorInst JumpTable Inst
1013%type <String> OptTailCall OptVolatile Unwind
1014%type <String> SymbolicValueRef OptSideEffect GlobalType
Reid Spencere77e35e2006-12-01 20:26:20 +00001015%type <String> FnDeclareLinkage BasicBlockList BigOrLittle AsmBlock
Reid Spencer78720742006-12-02 20:21:22 +00001016%type <String> Name ConstValueRef ConstVector External
Reid Spencer57f28f92006-12-03 07:10:26 +00001017%type <String> ShiftOps SetCondOps LogicalOps ArithmeticOps CastOps
1018%type <String> IPredicates FPredicates
Reid Spencere77e35e2006-12-01 20:26:20 +00001019
Reid Spencerf8483652006-12-02 15:16:01 +00001020%type <ValList> ValueRefList ValueRefListE IndexList
Reid Spencer52402b02007-01-02 05:45:11 +00001021%type <TypeVec> TypeListI ArgTypeListI
Reid Spencere77e35e2006-12-01 20:26:20 +00001022
Reid Spencerb6673a92007-01-15 02:41:46 +00001023%type <Ty> IntType SIntType UIntType FPType TypesV Types
1024%type <Ty> PrimType UpRTypesV UpRTypes
Reid Spencere77e35e2006-12-01 20:26:20 +00001025
Reid Spencerf2d55322006-12-01 21:52:30 +00001026%type <String> IntVal EInt64Val
1027%type <Const> ConstVal
Reid Spencere77e35e2006-12-01 20:26:20 +00001028
Reid Spencerb6673a92007-01-15 02:41:46 +00001029%type <Val> ValueRef ResolvedVal InstVal PHIList MemoryInst
Reid Spencere7c3c602006-11-30 06:36:44 +00001030
1031%start Module
1032
1033%%
1034
1035// Handle constant integer size restriction and conversion...
Reid Spencerf2d55322006-12-01 21:52:30 +00001036IntVal : SINTVAL | UINTVAL ;
Reid Spencere77e35e2006-12-01 20:26:20 +00001037EInt64Val : ESINT64VAL | EUINT64VAL;
Reid Spencere7c3c602006-11-30 06:36:44 +00001038
1039// Operations that are notably excluded from this list include:
1040// RET, BR, & SWITCH because they end basic blocks and are treated specially.
Reid Spencer78720742006-12-02 20:21:22 +00001041ArithmeticOps: ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV
1042 | REM | UREM | SREM | FREM;
Reid Spencere7c3c602006-11-30 06:36:44 +00001043LogicalOps : AND | OR | XOR;
1044SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Reid Spencer57f28f92006-12-03 07:10:26 +00001045IPredicates : EQ | NE | SLT | SGT | SLE | SGE | ULT | UGT | ULE | UGE;
1046FPredicates : OEQ | ONE | OLT | OGT | OLE | OGE | ORD | UNO | UEQ | UNE
1047 | ULT | UGT | ULE | UGE | TRUETOK | FALSETOK;
Reid Spencerf7bde222006-12-01 22:26:37 +00001048ShiftOps : SHL | SHR | ASHR | LSHR;
Reid Spencerfcb5df82006-12-01 22:34:43 +00001049CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI |
1050 UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
1051 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001052
1053// These are some types that allow classification if we only want a particular
1054// thing... for example, only a signed, unsigned, or integral type.
1055SIntType : LONG | INT | SHORT | SBYTE;
1056UIntType : ULONG | UINT | USHORT | UBYTE;
1057IntType : SIntType | UIntType;
1058FPType : FLOAT | DOUBLE;
1059
1060// OptAssign - Value producing statements have an optional assignment component
1061OptAssign : Name '=' {
Reid Spencere7c3c602006-11-30 06:36:44 +00001062 $$ = $1;
1063 }
1064 | /*empty*/ {
1065 $$ = new std::string("");
1066 };
1067
1068OptLinkage
1069 : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT
1070 | EXTERN_WEAK
1071 | /*empty*/ { $$ = new std::string(""); } ;
1072
1073OptCallingConv
1074 : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK
Reid Spencer16244f42006-12-01 21:10:07 +00001075 | X86_FASTCALLCC_TOK
1076 | CC_TOK EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +00001077 *$1 += *$2;
1078 delete $2;
Reid Spencer16244f42006-12-01 21:10:07 +00001079 $$ = $1;
1080 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001081 | /*empty*/ { $$ = new std::string(""); } ;
1082
1083// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1084// a comma before it.
1085OptAlign
1086 : /*empty*/ { $$ = new std::string(); }
Reid Spencerf2d55322006-12-01 21:52:30 +00001087 | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
Reid Spencerf0cf1322006-12-07 04:23:03 +00001088
Reid Spencere7c3c602006-11-30 06:36:44 +00001089OptCAlign
1090 : /*empty*/ { $$ = new std::string(); }
1091 | ',' ALIGN EUINT64VAL {
1092 $2->insert(0, ", ");
Reid Spencerf2d55322006-12-01 21:52:30 +00001093 *$2 += " " + *$3;
1094 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001095 $$ = $2;
1096 };
1097
1098SectionString
1099 : SECTION STRINGCONSTANT {
1100 *$1 += " " + *$2;
1101 delete $2;
1102 $$ = $1;
1103 };
1104
1105OptSection : /*empty*/ { $$ = new std::string(); }
1106 | SectionString;
1107
1108GlobalVarAttributes
1109 : /* empty */ { $$ = new std::string(); }
1110 | ',' GlobalVarAttribute GlobalVarAttributes {
1111 $2->insert(0, ", ");
1112 if (!$3->empty())
1113 *$2 += " " + *$3;
1114 delete $3;
1115 $$ = $2;
1116 };
1117
1118GlobalVarAttribute
1119 : SectionString
1120 | ALIGN EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +00001121 *$1 += " " + *$2;
1122 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001123 $$ = $1;
1124 };
1125
1126//===----------------------------------------------------------------------===//
1127// Types includes all predefined types... except void, because it can only be
1128// used in specific contexts (function returning void for example). To have
1129// access to it, a user must explicitly use TypesV.
1130//
1131
1132// TypesV includes all of 'Types', but it also includes the void type.
1133TypesV : Types | VOID ;
1134UpRTypesV : UpRTypes | VOID ;
1135Types : UpRTypes ;
1136
1137// Derived types are added later...
1138//
1139PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
Reid Spencere77e35e2006-12-01 20:26:20 +00001140PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
Reid Spencera50d5962006-12-02 04:11:07 +00001141UpRTypes
1142 : OPAQUE {
Reid Spencerb6673a92007-01-15 02:41:46 +00001143 $$ = Type::get(*$1, OpaqueTy);
Reid Spencera50d5962006-12-02 04:11:07 +00001144 }
1145 | SymbolicValueRef {
Reid Spencerb6673a92007-01-15 02:41:46 +00001146 $$ = Type::get(*$1, UnresolvedTy);
Reid Spencera50d5962006-12-02 04:11:07 +00001147 }
Reid Spencer78720742006-12-02 20:21:22 +00001148 | PrimType {
1149 $$ = $1;
1150 }
1151 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerf2d55322006-12-01 21:52:30 +00001152 $2->insert(0, "\\");
Reid Spencerb6673a92007-01-15 02:41:46 +00001153 $$ = Type::get(*$2, UpRefTy);
Reid Spencere7c3c602006-11-30 06:36:44 +00001154 }
1155 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Reid Spencer52402b02007-01-02 05:45:11 +00001156 std::string newTy( $1->getNewTy() + "(");
1157 for (unsigned i = 0; i < $3->size(); ++i) {
1158 if (i != 0)
1159 newTy += ", ";
1160 if ((*$3)[i]->isVoid())
1161 newTy += "...";
1162 else
1163 newTy += (*$3)[i]->getNewTy();
1164 }
1165 newTy += ")";
Reid Spencerb6673a92007-01-15 02:41:46 +00001166 $$ = Type::get(newTy, $1, $3);
Reid Spencere7c3c602006-11-30 06:36:44 +00001167 }
1168 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencer319a7302007-01-05 17:20:02 +00001169 uint64_t elems = atoi($2->c_str());
Reid Spencerf2d55322006-12-01 21:52:30 +00001170 $2->insert(0,"[ ");
Reid Spencer52402b02007-01-02 05:45:11 +00001171 *$2 += " x " + $4->getNewTy() + " ]";
Reid Spencerb6673a92007-01-15 02:41:46 +00001172 $$ = Type::get(*$2, ArrayTy, $4, elems);
Reid Spencere7c3c602006-11-30 06:36:44 +00001173 }
1174 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
Reid Spencer319a7302007-01-05 17:20:02 +00001175 uint64_t elems = atoi($2->c_str());
Reid Spencerf2d55322006-12-01 21:52:30 +00001176 $2->insert(0,"< ");
Reid Spencer52402b02007-01-02 05:45:11 +00001177 *$2 += " x " + $4->getNewTy() + " >";
Reid Spencerb6673a92007-01-15 02:41:46 +00001178 $$ = Type::get(*$2, PackedTy, $4, elems);
Reid Spencere7c3c602006-11-30 06:36:44 +00001179 }
1180 | '{' TypeListI '}' { // Structure type?
Reid Spencer52402b02007-01-02 05:45:11 +00001181 std::string newTy("{");
1182 for (unsigned i = 0; i < $2->size(); ++i) {
1183 if (i != 0)
1184 newTy += ", ";
1185 newTy += (*$2)[i]->getNewTy();
1186 }
1187 newTy += "}";
Reid Spencerb6673a92007-01-15 02:41:46 +00001188 $$ = Type::get(newTy, StructTy, $2);
Reid Spencere7c3c602006-11-30 06:36:44 +00001189 }
1190 | '{' '}' { // Empty structure type?
Reid Spencerb6673a92007-01-15 02:41:46 +00001191 $$ = Type::get("{}", StructTy, new TypeList());
Reid Spencere7c3c602006-11-30 06:36:44 +00001192 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001193 | '<' '{' TypeListI '}' '>' { // Packed Structure type?
Reid Spencer52402b02007-01-02 05:45:11 +00001194 std::string newTy("<{");
1195 for (unsigned i = 0; i < $3->size(); ++i) {
1196 if (i != 0)
1197 newTy += ", ";
1198 newTy += (*$3)[i]->getNewTy();
1199 }
1200 newTy += "}>";
Reid Spencerb6673a92007-01-15 02:41:46 +00001201 $$ = Type::get(newTy, PackedStructTy, $3);
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001202 }
1203 | '<' '{' '}' '>' { // Empty packed structure type?
Reid Spencerb6673a92007-01-15 02:41:46 +00001204 $$ = Type::get("<{}>", PackedStructTy, new TypeList());
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001205 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001206 | UpRTypes '*' { // Pointer type?
Reid Spencer52402b02007-01-02 05:45:11 +00001207 $$ = $1->getPointerType();
Reid Spencere7c3c602006-11-30 06:36:44 +00001208 };
1209
1210// TypeList - Used for struct declarations and as a basis for function type
1211// declaration type lists
1212//
Reid Spencere77e35e2006-12-01 20:26:20 +00001213TypeListI
1214 : UpRTypes {
Reid Spencer52402b02007-01-02 05:45:11 +00001215 $$ = new TypeList();
1216 $$->push_back($1);
Reid Spencere77e35e2006-12-01 20:26:20 +00001217 }
1218 | TypeListI ',' UpRTypes {
Reid Spencere7c3c602006-11-30 06:36:44 +00001219 $$ = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001220 $$->push_back($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00001221 };
1222
1223// ArgTypeList - List of types for a function type declaration...
Reid Spencere77e35e2006-12-01 20:26:20 +00001224ArgTypeListI
1225 : TypeListI
Reid Spencere7c3c602006-11-30 06:36:44 +00001226 | TypeListI ',' DOTDOTDOT {
Reid Spencere7c3c602006-11-30 06:36:44 +00001227 $$ = $1;
Reid Spencerb6673a92007-01-15 02:41:46 +00001228 $$->push_back(Type::get("void",VoidTy));
Reid Spencer52402b02007-01-02 05:45:11 +00001229 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001230 }
1231 | DOTDOTDOT {
Reid Spencer52402b02007-01-02 05:45:11 +00001232 $$ = new TypeList();
Reid Spencerb6673a92007-01-15 02:41:46 +00001233 $$->push_back(Type::get("void",VoidTy));
Reid Spencer52402b02007-01-02 05:45:11 +00001234 delete $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001235 }
1236 | /*empty*/ {
Reid Spencer52402b02007-01-02 05:45:11 +00001237 $$ = new TypeList();
Reid Spencere7c3c602006-11-30 06:36:44 +00001238 };
1239
1240// ConstVal - The various declarations that go into the constant pool. This
1241// production is used ONLY to represent constants that show up AFTER a 'const',
1242// 'constant' or 'global' token at global scope. Constants that can be inlined
1243// into other expressions (such as integers and constexprs) are handled by the
1244// ResolvedVal, ValueRef and ConstValueRef productions.
1245//
1246ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencerb6673a92007-01-15 02:41:46 +00001247 $$ = new Constant;
Reid Spencer30d0c582007-01-15 00:26:18 +00001248 $$->type = $1;
1249 $$->cnst = new std::string($1->getNewTy());
1250 *$$->cnst += " [ " + *$3 + " ]";
Reid Spencere7c3c602006-11-30 06:36:44 +00001251 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001252 }
1253 | Types '[' ']' {
Reid Spencerb6673a92007-01-15 02:41:46 +00001254 $$ = new Constant;
Reid Spencer30d0c582007-01-15 00:26:18 +00001255 $$->type = $1;
1256 $$->cnst = new std::string($1->getNewTy());
1257 *$$->cnst += "[ ]";
Reid Spencere7c3c602006-11-30 06:36:44 +00001258 }
1259 | Types 'c' STRINGCONSTANT {
Reid Spencerb6673a92007-01-15 02:41:46 +00001260 $$ = new Constant;
Reid Spencer30d0c582007-01-15 00:26:18 +00001261 $$->type = $1;
1262 $$->cnst = new std::string($1->getNewTy());
1263 *$$->cnst += " c" + *$3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001264 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001265 }
1266 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencerb6673a92007-01-15 02:41:46 +00001267 $$ = new Constant;
Reid Spencer30d0c582007-01-15 00:26:18 +00001268 $$->type = $1;
1269 $$->cnst = new std::string($1->getNewTy());
1270 *$$->cnst += " < " + *$3 + " >";
Reid Spencere7c3c602006-11-30 06:36:44 +00001271 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001272 }
1273 | Types '{' ConstVector '}' {
Reid Spencerb6673a92007-01-15 02:41:46 +00001274 $$ = new Constant;
Reid Spencer30d0c582007-01-15 00:26:18 +00001275 $$->type = $1;
1276 $$->cnst = new std::string($1->getNewTy());
1277 *$$->cnst += " { " + *$3 + " }";
Reid Spencere7c3c602006-11-30 06:36:44 +00001278 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001279 }
1280 | Types '{' '}' {
Reid Spencerb6673a92007-01-15 02:41:46 +00001281 $$ = new Constant;
Reid Spencer30d0c582007-01-15 00:26:18 +00001282 $$->type = $1;
1283 $$->cnst = new std::string($1->getNewTy());
1284 *$$->cnst += " {}";
Reid Spencere7c3c602006-11-30 06:36:44 +00001285 }
1286 | Types NULL_TOK {
Reid Spencerb6673a92007-01-15 02:41:46 +00001287 $$ = new Constant;
Reid Spencer30d0c582007-01-15 00:26:18 +00001288 $$->type = $1;
1289 $$->cnst = new std::string($1->getNewTy());
1290 *$$->cnst += " " + *$2;
Reid Spencerf2d55322006-12-01 21:52:30 +00001291 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001292 }
1293 | Types UNDEF {
Reid Spencerb6673a92007-01-15 02:41:46 +00001294 $$ = new Constant;
Reid Spencer30d0c582007-01-15 00:26:18 +00001295 $$->type = $1;
1296 $$->cnst = new std::string($1->getNewTy());
1297 *$$->cnst += " " + *$2;
Reid Spencerf2d55322006-12-01 21:52:30 +00001298 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001299 }
1300 | Types SymbolicValueRef {
Reid Spencerb6673a92007-01-15 02:41:46 +00001301 $$ = new Constant;
Reid Spencer319a7302007-01-05 17:20:02 +00001302 std::string Name = getUniqueName($2, $1->resolve(), true);
Reid Spencer30d0c582007-01-15 00:26:18 +00001303 $$->type = $1;
1304 $$->cnst = new std::string($1->getNewTy());
1305 *$$->cnst += " " + Name;
Reid Spencere7c3c602006-11-30 06:36:44 +00001306 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001307 }
1308 | Types ConstExpr {
Reid Spencerb6673a92007-01-15 02:41:46 +00001309 $$ = new Constant;
Reid Spencer30d0c582007-01-15 00:26:18 +00001310 $$->type = $1;
1311 $$->cnst = new std::string($1->getNewTy());
1312 *$$->cnst += " " + *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001313 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001314 }
1315 | Types ZEROINITIALIZER {
Reid Spencerb6673a92007-01-15 02:41:46 +00001316 $$ = new Constant;
Reid Spencer30d0c582007-01-15 00:26:18 +00001317 $$->type = $1;
1318 $$->cnst = new std::string($1->getNewTy());
1319 *$$->cnst += " " + *$2;
Reid Spencerf2d55322006-12-01 21:52:30 +00001320 delete $2;
Reid Spencere77e35e2006-12-01 20:26:20 +00001321 }
1322 | SIntType EInt64Val { // integral constants
Reid Spencerb6673a92007-01-15 02:41:46 +00001323 $$ = new Constant;
Reid Spencer30d0c582007-01-15 00:26:18 +00001324 $$->type = $1;
1325 $$->cnst = new std::string($1->getNewTy());
1326 *$$->cnst += " " + *$2;
Reid Spencerf2d55322006-12-01 21:52:30 +00001327 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001328 }
Reid Spencer7356ae42007-01-02 06:34:08 +00001329 | UIntType EInt64Val { // integral constants
Reid Spencerb6673a92007-01-15 02:41:46 +00001330 $$ = new Constant;
Reid Spencer30d0c582007-01-15 00:26:18 +00001331 $$->type = $1;
1332 $$->cnst = new std::string($1->getNewTy());
1333 *$$->cnst += " " + *$2;
Reid Spencerf2d55322006-12-01 21:52:30 +00001334 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001335 }
1336 | BOOL TRUETOK { // Boolean constants
Reid Spencerb6673a92007-01-15 02:41:46 +00001337 $$ = new Constant;
Reid Spencer30d0c582007-01-15 00:26:18 +00001338 $$->type = $1;
1339 $$->cnst = new std::string($1->getNewTy());
1340 *$$->cnst += " " + *$2;
Reid Spencerf2d55322006-12-01 21:52:30 +00001341 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001342 }
1343 | BOOL FALSETOK { // Boolean constants
Reid Spencerb6673a92007-01-15 02:41:46 +00001344 $$ = new Constant;
Reid Spencer30d0c582007-01-15 00:26:18 +00001345 $$->type = $1;
1346 $$->cnst = new std::string($1->getNewTy());
1347 *$$->cnst += " " + *$2;
Reid Spencerf2d55322006-12-01 21:52:30 +00001348 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001349 }
1350 | FPType FPVAL { // Float & Double constants
Reid Spencerb6673a92007-01-15 02:41:46 +00001351 $$ = new Constant;
Reid Spencer30d0c582007-01-15 00:26:18 +00001352 $$->type = $1;
1353 $$->cnst = new std::string($1->getNewTy());
1354 *$$->cnst += " " + *$2;
Reid Spencerf2d55322006-12-01 21:52:30 +00001355 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001356 };
1357
Reid Spencerfcb5df82006-12-01 22:34:43 +00001358ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001359 std::string source = *$3->cnst;
Reid Spencerb6673a92007-01-15 02:41:46 +00001360 const Type* SrcTy = $3->type->resolve();
1361 const Type* DstTy = $5->resolve();
Reid Spencer280d8012006-12-01 23:40:53 +00001362 if (*$1 == "cast") {
Reid Spencera50d5962006-12-02 04:11:07 +00001363 // Call getCastUpgrade to upgrade the old cast
Reid Spencer319a7302007-01-05 17:20:02 +00001364 $$ = new std::string(getCastUpgrade(source, SrcTy, DstTy, true));
Reid Spencera50d5962006-12-02 04:11:07 +00001365 } else {
1366 // Nothing to upgrade, just create the cast constant expr
1367 $$ = new std::string(*$1);
Reid Spencer52402b02007-01-02 05:45:11 +00001368 *$$ += "( " + source + " to " + $5->getNewTy() + ")";
Reid Spencer280d8012006-12-01 23:40:53 +00001369 }
Reid Spencer30d0c582007-01-15 00:26:18 +00001370 delete $1; delete $3; delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001371 }
1372 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001373 *$1 += "(" + *$3->cnst;
Reid Spencerf8483652006-12-02 15:16:01 +00001374 for (unsigned i = 0; i < $4->size(); ++i) {
Reid Spencerb6673a92007-01-15 02:41:46 +00001375 Value* V = (*$4)[i];
1376 *$1 += ", " + *V->val;
1377 delete V;
Reid Spencerf8483652006-12-02 15:16:01 +00001378 }
1379 *$1 += ")";
Reid Spencere77e35e2006-12-01 20:26:20 +00001380 $$ = $1;
Reid Spencer30d0c582007-01-15 00:26:18 +00001381 delete $3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001382 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001383 }
1384 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001385 *$1 += "(" + *$3->cnst + "," + *$5->cnst + "," + *$7->cnst + ")";
1386 delete $3; delete $5; delete $7;
Reid Spencere77e35e2006-12-01 20:26:20 +00001387 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001388 }
1389 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001390 const char* op = getDivRemOpcode(*$1, $3->type);
Reid Spencer78720742006-12-02 20:21:22 +00001391 $$ = new std::string(op);
Reid Spencer30d0c582007-01-15 00:26:18 +00001392 *$$ += "(" + *$3->cnst + "," + *$5->cnst + ")";
1393 delete $1; delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001394 }
1395 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001396 *$1 += "(" + *$3->cnst + "," + *$5->cnst + ")";
1397 delete $3; delete $5;
Reid Spencere77e35e2006-12-01 20:26:20 +00001398 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001399 }
1400 | SetCondOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001401 *$1 = getCompareOp(*$1, $3->type);
1402 *$1 += "(" + *$3->cnst + "," + *$5->cnst + ")";
1403 delete $3; delete $5;
Reid Spencere77e35e2006-12-01 20:26:20 +00001404 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001405 }
Reid Spencer57f28f92006-12-03 07:10:26 +00001406 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001407 *$1 += " " + *$2 + " (" + *$4->cnst + "," + *$6->cnst + ")";
1408 delete $2; delete $4; delete $6;
Reid Spencer57f28f92006-12-03 07:10:26 +00001409 $$ = $1;
1410 }
1411 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001412 *$1 += " " + *$2 + " (" + *$4->cnst + "," + *$6->cnst + ")";
1413 delete $2; delete $4; delete $6;
Reid Spencer229e9362006-12-02 22:14:11 +00001414 $$ = $1;
1415 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001416 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencerf7bde222006-12-01 22:26:37 +00001417 const char* shiftop = $1->c_str();
1418 if (*$1 == "shr")
Reid Spencer30d0c582007-01-15 00:26:18 +00001419 shiftop = ($3->type->isUnsigned()) ? "lshr" : "ashr";
Reid Spencerf7bde222006-12-01 22:26:37 +00001420 $$ = new std::string(shiftop);
Reid Spencer30d0c582007-01-15 00:26:18 +00001421 *$$ += "(" + *$3->cnst + "," + *$5->cnst + ")";
1422 delete $1; delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001423 }
1424 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001425 *$1 += "(" + *$3->cnst + "," + *$5->cnst + ")";
1426 delete $3; delete $5;
Reid Spencere77e35e2006-12-01 20:26:20 +00001427 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001428 }
1429 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001430 *$1 += "(" + *$3->cnst + "," + *$5->cnst + "," + *$7->cnst + ")";
1431 delete $3; delete $5; delete $7;
Reid Spencere77e35e2006-12-01 20:26:20 +00001432 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001433 }
1434 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001435 *$1 += "(" + *$3->cnst + "," + *$5->cnst + "," + *$7->cnst + ")";
1436 delete $3; delete $5; delete $7;
Reid Spencere77e35e2006-12-01 20:26:20 +00001437 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001438 };
1439
1440
1441// ConstVector - A list of comma separated constants.
Reid Spencere77e35e2006-12-01 20:26:20 +00001442
1443ConstVector
1444 : ConstVector ',' ConstVal {
Reid Spencer30d0c582007-01-15 00:26:18 +00001445 *$1 += ", " + *$3->cnst;
1446 delete $3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001447 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001448 }
Reid Spencer30d0c582007-01-15 00:26:18 +00001449 | ConstVal { $$ = new std::string(*$1->cnst); delete $1; }
Reid Spencere77e35e2006-12-01 20:26:20 +00001450 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001451
1452
1453// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Reid Spencere77e35e2006-12-01 20:26:20 +00001454GlobalType : GLOBAL | CONSTANT ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001455
1456
1457//===----------------------------------------------------------------------===//
1458// Rules to match Modules
1459//===----------------------------------------------------------------------===//
1460
1461// Module rule: Capture the result of parsing the whole file into a result
1462// variable...
1463//
1464Module : DefinitionList {
1465};
1466
1467// DefinitionList - Top level definitions
1468//
1469DefinitionList : DefinitionList Function {
1470 $$ = 0;
1471 }
1472 | DefinitionList FunctionProto {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001473 *O << *$2 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001474 delete $2;
1475 $$ = 0;
1476 }
1477 | DefinitionList MODULE ASM_TOK AsmBlock {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001478 *O << "module asm " << ' ' << *$4 << '\n';
Reid Spencerd154b572006-12-01 20:36:40 +00001479 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001480 }
1481 | DefinitionList IMPLEMENTATION {
1482 *O << "implementation\n";
Reid Spencerd154b572006-12-01 20:36:40 +00001483 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001484 }
Reid Spencera50d5962006-12-02 04:11:07 +00001485 | ConstPool { $$ = 0; }
Jeff Cohenac2dca92007-01-21 19:30:52 +00001486 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001487
Jeff Cohenac2dca92007-01-21 19:30:52 +00001488External : EXTERNAL | UNINITIALIZED { $$ = $1; *$$ = "external"; } ;
Reid Spencer78720742006-12-02 20:21:22 +00001489
Reid Spencere7c3c602006-11-30 06:36:44 +00001490// ConstPool - Constants with optional names assigned to them.
1491ConstPool : ConstPool OptAssign TYPE TypesV {
Reid Spencerb6673a92007-01-15 02:41:46 +00001492 Type::EnumeratedTypes.push_back($4);
Reid Spencera50d5962006-12-02 04:11:07 +00001493 if (!$2->empty()) {
Reid Spencerb6673a92007-01-15 02:41:46 +00001494 Type::NamedTypes[*$2] = $4;
Reid Spencera50d5962006-12-02 04:11:07 +00001495 *O << *$2 << " = ";
1496 }
Reid Spencer52402b02007-01-02 05:45:11 +00001497 *O << "type " << $4->getNewTy() << '\n';
1498 delete $2; delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001499 $$ = 0;
1500 }
1501 | ConstPool FunctionProto { // Function prototypes can be in const pool
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001502 *O << *$2 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001503 delete $2;
1504 $$ = 0;
1505 }
1506 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001507 *O << *$2 << ' ' << *$3 << ' ' << *$4 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001508 delete $2; delete $3; delete $4;
1509 $$ = 0;
1510 }
1511 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +00001512 if (!$2->empty()) {
Reid Spencer30d0c582007-01-15 00:26:18 +00001513 std::string Name = getGlobalName($2,*$3, $5->type->getPointerType(),
Reid Spencer319a7302007-01-05 17:20:02 +00001514 *$4 == "constant");
Reid Spencer52402b02007-01-02 05:45:11 +00001515 *O << Name << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +00001516 }
Reid Spencer30d0c582007-01-15 00:26:18 +00001517 *O << *$3 << ' ' << *$4 << ' ' << *$5->cnst << ' ' << *$6 << '\n';
Reid Spencer52402b02007-01-02 05:45:11 +00001518 delete $2; delete $3; delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001519 $$ = 0;
1520 }
Reid Spencer78720742006-12-02 20:21:22 +00001521 | ConstPool OptAssign External GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +00001522 if (!$2->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001523 std::string Name = getGlobalName($2,*$3,$5->getPointerType(),
1524 *$4 == "constant");
Reid Spencer52402b02007-01-02 05:45:11 +00001525 *O << Name << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +00001526 }
Reid Spencer52402b02007-01-02 05:45:11 +00001527 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
1528 delete $2; delete $3; delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001529 $$ = 0;
1530 }
Reid Spencer319a7302007-01-05 17:20:02 +00001531 | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +00001532 if (!$2->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001533 std::string Name = getGlobalName($2,*$3,$5->getPointerType(),
1534 *$4 == "constant");
Reid Spencer52402b02007-01-02 05:45:11 +00001535 *O << Name << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +00001536 }
Reid Spencer52402b02007-01-02 05:45:11 +00001537 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
1538 delete $2; delete $3; delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001539 $$ = 0;
1540 }
1541 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +00001542 if (!$2->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001543 std::string Name = getGlobalName($2,*$3,$5->getPointerType(),
1544 *$4 == "constant");
Reid Spencer52402b02007-01-02 05:45:11 +00001545 *O << Name << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +00001546 }
Reid Spencer52402b02007-01-02 05:45:11 +00001547 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
1548 delete $2; delete $3; delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001549 $$ = 0;
1550 }
1551 | ConstPool TARGET TargetDefinition {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001552 *O << *$2 << ' ' << *$3 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001553 delete $2; delete $3;
1554 $$ = 0;
1555 }
1556 | ConstPool DEPLIBS '=' LibrariesDefinition {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001557 *O << *$2 << " = " << *$4 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001558 delete $2; delete $4;
1559 $$ = 0;
1560 }
1561 | /* empty: end of list */ {
1562 $$ = 0;
1563 };
1564
1565
1566AsmBlock : STRINGCONSTANT ;
1567
Jeff Cohenac2dca92007-01-21 19:30:52 +00001568BigOrLittle : BIG | LITTLE ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001569
1570TargetDefinition
1571 : ENDIAN '=' BigOrLittle {
Reid Spencere77e35e2006-12-01 20:26:20 +00001572 *$1 += " = " + *$3;
1573 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001574 $$ = $1;
1575 }
1576 | POINTERSIZE '=' EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +00001577 *$1 += " = " + *$3;
1578 if (*$3 == "64")
Reid Spencere77e35e2006-12-01 20:26:20 +00001579 SizeOfPointer = 64;
Reid Spencerf2d55322006-12-01 21:52:30 +00001580 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001581 $$ = $1;
1582 }
1583 | TRIPLE '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +00001584 *$1 += " = " + *$3;
1585 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001586 $$ = $1;
1587 }
1588 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +00001589 *$1 += " = " + *$3;
1590 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001591 $$ = $1;
1592 };
1593
1594LibrariesDefinition
1595 : '[' LibList ']' {
1596 $2->insert(0, "[ ");
1597 *$2 += " ]";
1598 $$ = $2;
1599 };
1600
1601LibList
1602 : LibList ',' STRINGCONSTANT {
1603 *$1 += ", " + *$3;
1604 delete $3;
1605 $$ = $1;
1606 }
1607 | STRINGCONSTANT
1608 | /* empty: end of list */ {
1609 $$ = new std::string();
1610 };
1611
1612//===----------------------------------------------------------------------===//
1613// Rules to match Function Headers
1614//===----------------------------------------------------------------------===//
1615
1616Name : VAR_ID | STRINGCONSTANT;
1617OptName : Name | /*empty*/ { $$ = new std::string(); };
1618
1619ArgVal : Types OptName {
Reid Spencer52402b02007-01-02 05:45:11 +00001620 $$ = new std::string($1->getNewTy());
1621 if (!$2->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001622 std::string Name = getUniqueName($2, $1->resolve());
Reid Spencer52402b02007-01-02 05:45:11 +00001623 *$$ += " " + Name;
1624 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001625 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001626};
1627
1628ArgListH : ArgListH ',' ArgVal {
1629 *$1 += ", " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001630 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001631 }
1632 | ArgVal {
1633 $$ = $1;
1634 };
1635
1636ArgList : ArgListH {
1637 $$ = $1;
1638 }
1639 | ArgListH ',' DOTDOTDOT {
1640 *$1 += ", ...";
1641 $$ = $1;
Reid Spencere77e35e2006-12-01 20:26:20 +00001642 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001643 }
1644 | DOTDOTDOT {
1645 $$ = $1;
1646 }
Reid Spencerd154b572006-12-01 20:36:40 +00001647 | /* empty */ { $$ = new std::string(); };
Reid Spencere7c3c602006-11-30 06:36:44 +00001648
Reid Spencer71d2ec92006-12-31 06:02:26 +00001649FunctionHeaderH
1650 : OptCallingConv TypesV Name '(' ArgList ')' OptSection OptAlign {
Reid Spencerc4d96252007-01-13 00:03:30 +00001651 if (*$3 == "%llvm.va_start" || *$3 == "%llvm.va_end") {
Reid Spencerf0c9a652007-01-13 00:13:49 +00001652 *$5 = "i8*";
Reid Spencerc4d96252007-01-13 00:03:30 +00001653 } else if (*$3 == "%llvm.va_copy") {
1654 *$5 = "i8*, i8*";
1655 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001656 if (!$1->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +00001657 *$1 += " ";
Reid Spencere7c3c602006-11-30 06:36:44 +00001658 }
Reid Spencer52402b02007-01-02 05:45:11 +00001659 *$1 += $2->getNewTy() + " " + *$3 + "(" + *$5 + ")";
Reid Spencere7c3c602006-11-30 06:36:44 +00001660 if (!$7->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +00001661 *$1 += " " + *$7;
Reid Spencere7c3c602006-11-30 06:36:44 +00001662 }
1663 if (!$8->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +00001664 *$1 += " " + *$8;
Reid Spencere7c3c602006-11-30 06:36:44 +00001665 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001666 delete $3;
1667 delete $5;
1668 delete $7;
1669 delete $8;
1670 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001671 };
1672
Reid Spencer78720742006-12-02 20:21:22 +00001673BEGIN : BEGINTOK { $$ = new std::string("{"); delete $1; }
1674 | '{' { $$ = new std::string ("{"); }
Jeff Cohenac2dca92007-01-21 19:30:52 +00001675 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001676
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001677FunctionHeader
1678 : OptLinkage FunctionHeaderH BEGIN {
1679 *O << "define ";
1680 if (!$1->empty()) {
1681 *O << *$1 << ' ';
1682 }
1683 *O << *$2 << ' ' << *$3 << '\n';
1684 delete $1; delete $2; delete $3;
1685 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001686 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001687 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001688
Reid Spencer78720742006-12-02 20:21:22 +00001689END : ENDTOK { $$ = new std::string("}"); delete $1; }
Reid Spencere7c3c602006-11-30 06:36:44 +00001690 | '}' { $$ = new std::string("}"); };
1691
1692Function : FunctionHeader BasicBlockList END {
1693 if ($2)
1694 *O << *$2;
Reid Spencer71d2ec92006-12-31 06:02:26 +00001695 *O << *$3 << "\n\n";
Reid Spencer52402b02007-01-02 05:45:11 +00001696 delete $1; delete $2; delete $3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001697 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001698};
1699
Reid Spencere77e35e2006-12-01 20:26:20 +00001700FnDeclareLinkage
1701 : /*default*/ { $$ = new std::string(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001702 | DLLIMPORT
1703 | EXTERN_WEAK
1704 ;
1705
1706FunctionProto
Reid Spencerc4d96252007-01-13 00:03:30 +00001707 : DECLARE { isDeclare = true; } FnDeclareLinkage FunctionHeaderH {
1708 if (!$3->empty())
1709 *$1 += " " + *$3;
1710 *$1 += " " + *$4;
Reid Spencere77e35e2006-12-01 20:26:20 +00001711 delete $3;
Reid Spencerc4d96252007-01-13 00:03:30 +00001712 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001713 $$ = $1;
Reid Spencerc4d96252007-01-13 00:03:30 +00001714 isDeclare = false;
Reid Spencere7c3c602006-11-30 06:36:44 +00001715 };
1716
1717//===----------------------------------------------------------------------===//
1718// Rules to match Basic Blocks
1719//===----------------------------------------------------------------------===//
1720
Reid Spencerd154b572006-12-01 20:36:40 +00001721OptSideEffect : /* empty */ { $$ = new std::string(); }
1722 | SIDEEFFECT;
Reid Spencere7c3c602006-11-30 06:36:44 +00001723
Reid Spencere77e35e2006-12-01 20:26:20 +00001724ConstValueRef
Reid Spencerf2d55322006-12-01 21:52:30 +00001725 : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK | NULL_TOK | UNDEF
1726 | ZEROINITIALIZER
Reid Spencere7c3c602006-11-30 06:36:44 +00001727 | '<' ConstVector '>' {
1728 $2->insert(0, "<");
1729 *$2 += ">";
1730 $$ = $2;
1731 }
1732 | ConstExpr
1733 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
1734 if (!$2->empty()) {
1735 *$1 += " " + *$2;
1736 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001737 *$1 += " " + *$3 + ", " + *$5;
1738 delete $2; delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001739 $$ = $1;
1740 };
1741
Reid Spencerf2d55322006-12-01 21:52:30 +00001742SymbolicValueRef : IntVal | Name ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001743
1744// ValueRef - A reference to a definition... either constant or symbolic
Reid Spencerf459d392006-12-02 16:19:52 +00001745ValueRef
1746 : SymbolicValueRef {
Reid Spencerb6673a92007-01-15 02:41:46 +00001747 $$ = new Value;
Reid Spencer30d0c582007-01-15 00:26:18 +00001748 $$->val = $1;
1749 $$->constant = false;
1750 $$->type = 0;
Reid Spencerf459d392006-12-02 16:19:52 +00001751 }
1752 | ConstValueRef {
Reid Spencerb6673a92007-01-15 02:41:46 +00001753 $$ = new Value;
Reid Spencer30d0c582007-01-15 00:26:18 +00001754 $$->val = $1;
1755 $$->constant = true;
1756 $$->type = 0;
Reid Spencerf459d392006-12-02 16:19:52 +00001757 }
1758 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001759
1760// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1761// type immediately preceeds the value reference, and allows complex constant
1762// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1763ResolvedVal : Types ValueRef {
Reid Spencer319a7302007-01-05 17:20:02 +00001764 $1 = $1->resolve();
Reid Spencer30d0c582007-01-15 00:26:18 +00001765 std::string Name = getUniqueName($2->val, $1);
Reid Spencerf459d392006-12-02 16:19:52 +00001766 $$ = $2;
Reid Spencer30d0c582007-01-15 00:26:18 +00001767 delete $$->val;
1768 $$->val = new std::string($1->getNewTy() + " " + Name);
1769 $$->type = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001770 };
1771
1772BasicBlockList : BasicBlockList BasicBlock {
Reid Spencerf2d55322006-12-01 21:52:30 +00001773 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001774 }
1775 | BasicBlock { // Do not allow functions with 0 basic blocks
Reid Spencerf2d55322006-12-01 21:52:30 +00001776 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001777 };
1778
1779
1780// Basic blocks are terminated by branching instructions:
1781// br, br/cc, switch, ret
1782//
Reid Spencer16244f42006-12-01 21:10:07 +00001783BasicBlock : InstructionList BBTerminatorInst {
Reid Spencerf2d55322006-12-01 21:52:30 +00001784 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001785 };
1786
1787InstructionList : InstructionList Inst {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001788 *O << " " << *$2 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001789 delete $2;
1790 $$ = 0;
1791 }
1792 | /* empty */ {
1793 $$ = 0;
1794 }
1795 | LABELSTR {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001796 *O << *$1 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001797 delete $1;
1798 $$ = 0;
1799 };
1800
Jeff Cohenac2dca92007-01-21 19:30:52 +00001801Unwind : UNWIND | EXCEPT { $$ = $1; *$$ = "unwind"; } ;
Reid Spencer78720742006-12-02 20:21:22 +00001802
Reid Spencere7c3c602006-11-30 06:36:44 +00001803BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencer30d0c582007-01-15 00:26:18 +00001804 *O << " " << *$1 << ' ' << *$2->val << '\n';
1805 delete $1; delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001806 $$ = 0;
1807 }
1808 | RET VOID { // Return with no result...
Reid Spencer52402b02007-01-02 05:45:11 +00001809 *O << " " << *$1 << ' ' << $2->getNewTy() << '\n';
Reid Spencer319a7302007-01-05 17:20:02 +00001810 delete $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001811 $$ = 0;
1812 }
1813 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer30d0c582007-01-15 00:26:18 +00001814 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << *$3->val << '\n';
1815 delete $1; delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001816 $$ = 0;
1817 } // Conditional Branch...
1818 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencer30d0c582007-01-15 00:26:18 +00001819 std::string Name = getUniqueName($3->val, $2);
Reid Spencer52402b02007-01-02 05:45:11 +00001820 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", "
Reid Spencer30d0c582007-01-15 00:26:18 +00001821 << $5->getNewTy() << ' ' << *$6->val << ", " << $8->getNewTy() << ' '
1822 << *$9->val << '\n';
1823 delete $1; delete $3; delete $6; delete $9;
Reid Spencere7c3c602006-11-30 06:36:44 +00001824 $$ = 0;
1825 }
1826 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001827 std::string Name = getUniqueName($3->val, $2);
Reid Spencer52402b02007-01-02 05:45:11 +00001828 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", "
Reid Spencer30d0c582007-01-15 00:26:18 +00001829 << $5->getNewTy() << ' ' << *$6->val << " [" << *$8 << " ]\n";
1830 delete $1;
1831 delete $3;
1832 delete $6;
Reid Spencerf459d392006-12-02 16:19:52 +00001833 delete $8;
Reid Spencere7c3c602006-11-30 06:36:44 +00001834 $$ = 0;
1835 }
1836 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001837 std::string Name = getUniqueName($3->val, $2);
Reid Spencer52402b02007-01-02 05:45:11 +00001838 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", "
Reid Spencer30d0c582007-01-15 00:26:18 +00001839 << $5->getNewTy() << ' ' << *$6->val << "[]\n";
1840 delete $1;
1841 delete $3;
1842 delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001843 $$ = 0;
1844 }
Reid Spencer16244f42006-12-01 21:10:07 +00001845 | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
Reid Spencer78720742006-12-02 20:21:22 +00001846 TO LABEL ValueRef Unwind LABEL ValueRef {
Reid Spencerb6673a92007-01-15 02:41:46 +00001847 const Type* ResTy = getFunctionReturnType($4);
Reid Spencer16244f42006-12-01 21:10:07 +00001848 *O << " ";
Reid Spencer52402b02007-01-02 05:45:11 +00001849 if (!$1->empty()) {
1850 std::string Name = getUniqueName($1, ResTy);
1851 *O << Name << " = ";
1852 }
Reid Spencer30d0c582007-01-15 00:26:18 +00001853 *O << *$2 << ' ' << *$3 << ' ' << $4->getNewTy() << ' ' << *$5->val << " (";
Reid Spencerf8483652006-12-02 15:16:01 +00001854 for (unsigned i = 0; i < $7->size(); ++i) {
Reid Spencerb6673a92007-01-15 02:41:46 +00001855 Value* V = (*$7)[i];
1856 *O << *V->val;
Reid Spencerf8483652006-12-02 15:16:01 +00001857 if (i+1 < $7->size())
1858 *O << ", ";
Reid Spencerb6673a92007-01-15 02:41:46 +00001859 delete V;
Reid Spencerf8483652006-12-02 15:16:01 +00001860 }
Reid Spencer30d0c582007-01-15 00:26:18 +00001861 *O << ") " << *$9 << ' ' << $10->getNewTy() << ' ' << *$11->val << ' '
1862 << *$12 << ' ' << $13->getNewTy() << ' ' << *$14->val << '\n';
1863 delete $1; delete $2; delete $3; delete $5; delete $7;
1864 delete $9; delete $11; delete $12; delete $14;
Reid Spencere7c3c602006-11-30 06:36:44 +00001865 $$ = 0;
1866 }
Reid Spencer78720742006-12-02 20:21:22 +00001867 | Unwind {
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 | UNREACHABLE {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001873 *O << " " << *$1 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001874 delete $1;
1875 $$ = 0;
1876 };
1877
1878JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001879 *$1 += " " + $2->getNewTy() + " " + *$3 + ", " + $5->getNewTy() + " " +
Reid Spencer30d0c582007-01-15 00:26:18 +00001880 *$6->val;
1881 delete $3; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001882 $$ = $1;
1883 }
1884 | IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001885 $2->insert(0, $1->getNewTy() + " " );
Reid Spencer30d0c582007-01-15 00:26:18 +00001886 *$2 += ", " + $4->getNewTy() + " " + *$5->val;
1887 delete $5;
Reid Spencere77e35e2006-12-01 20:26:20 +00001888 $$ = $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001889 };
1890
1891Inst
1892 : OptAssign InstVal {
Reid Spencerf5626a32007-01-01 01:20:41 +00001893 if (!$1->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001894 // Get a unique name for this value, based on its type.
Reid Spencer30d0c582007-01-15 00:26:18 +00001895 std::string Name = getUniqueName($1, $2->type);
Reid Spencer319a7302007-01-05 17:20:02 +00001896 *$1 = Name + " = ";
1897 if (deleteUselessCastFlag && *deleteUselessCastName == Name) {
1898 // don't actually delete it, just comment it out
1899 $1->insert(0, "; USELSS BITCAST: ");
Reid Spencerf5626a32007-01-01 01:20:41 +00001900 delete deleteUselessCastName;
Reid Spencerf5626a32007-01-01 01:20:41 +00001901 }
1902 }
Reid Spencer30d0c582007-01-15 00:26:18 +00001903 *$1 += *$2->val;
1904 delete $2;
Reid Spencerf5626a32007-01-01 01:20:41 +00001905 deleteUselessCastFlag = false;
Reid Spencere7c3c602006-11-30 06:36:44 +00001906 $$ = $1;
1907 };
1908
1909PHIList
1910 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer30d0c582007-01-15 00:26:18 +00001911 std::string Name = getUniqueName($3->val, $1);
Reid Spencer52402b02007-01-02 05:45:11 +00001912 Name.insert(0, $1->getNewTy() + "[");
Reid Spencer30d0c582007-01-15 00:26:18 +00001913 Name += "," + *$5->val + "]";
Reid Spencerb6673a92007-01-15 02:41:46 +00001914 $$ = new Value;
Reid Spencer30d0c582007-01-15 00:26:18 +00001915 $$->val = new std::string(Name);
1916 $$->type = $1;
1917 delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001918 }
1919 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
Reid Spencer30d0c582007-01-15 00:26:18 +00001920 std::string Name = getUniqueName($4->val, $1->type);
1921 *$1->val += ", [" + Name + "," + *$6->val + "]";
1922 delete $4;
1923 delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001924 $$ = $1;
1925 };
1926
1927
1928ValueRefList
Reid Spencer52402b02007-01-02 05:45:11 +00001929 : ResolvedVal {
Reid Spencerf8483652006-12-02 15:16:01 +00001930 $$ = new ValueList();
1931 $$->push_back($1);
1932 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001933 | ValueRefList ',' ResolvedVal {
Reid Spencere7c3c602006-11-30 06:36:44 +00001934 $$ = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001935 $$->push_back($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00001936 };
1937
1938// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1939ValueRefListE
Reid Spencerf8483652006-12-02 15:16:01 +00001940 : ValueRefList { $$ = $1; }
1941 | /*empty*/ { $$ = new ValueList(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001942 ;
1943
1944OptTailCall
1945 : TAIL CALL {
1946 *$1 += " " + *$2;
1947 delete $2;
1948 $$ = $1;
1949 }
1950 | CALL
1951 ;
1952
1953InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer78720742006-12-02 20:21:22 +00001954 const char* op = getDivRemOpcode(*$1, $2);
Reid Spencer30d0c582007-01-15 00:26:18 +00001955 std::string Name1 = getUniqueName($3->val, $2);
1956 std::string Name2 = getUniqueName($5->val, $2);
1957 $$ = $3;
1958 delete $$->val;
1959 $$->val = new std::string(op);
1960 *$$->val += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
1961 $$->type = $2;
1962 delete $1; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001963 }
1964 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer30d0c582007-01-15 00:26:18 +00001965 std::string Name1 = getUniqueName($3->val, $2);
1966 std::string Name2 = getUniqueName($5->val, $2);
Reid Spencer52402b02007-01-02 05:45:11 +00001967 *$1 += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
Reid Spencer30d0c582007-01-15 00:26:18 +00001968 $$ = $3;
1969 delete $$->val;
1970 $$->val = $1;
1971 $$->type = $2;
1972 delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001973 }
1974 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer30d0c582007-01-15 00:26:18 +00001975 std::string Name1 = getUniqueName($3->val, $2);
1976 std::string Name2 = getUniqueName($5->val, $2);
Reid Spencer229e9362006-12-02 22:14:11 +00001977 *$1 = getCompareOp(*$1, $2);
Reid Spencer52402b02007-01-02 05:45:11 +00001978 *$1 += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
Reid Spencer30d0c582007-01-15 00:26:18 +00001979 $$ = $3;
1980 delete $$->val;
1981 $$->val = $1;
Reid Spencerb6673a92007-01-15 02:41:46 +00001982 $$->type = Type::get("i1",BoolTy);
Reid Spencer30d0c582007-01-15 00:26:18 +00001983 delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001984 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001985 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer30d0c582007-01-15 00:26:18 +00001986 std::string Name1 = getUniqueName($4->val, $3);
1987 std::string Name2 = getUniqueName($6->val, $3);
Reid Spencer52402b02007-01-02 05:45:11 +00001988 *$1 += " " + *$2 + " " + $3->getNewTy() + " " + Name1 + "," + Name2;
Reid Spencer30d0c582007-01-15 00:26:18 +00001989 $$ = $4;
1990 delete $$->val;
1991 $$->val = $1;
Reid Spencerb6673a92007-01-15 02:41:46 +00001992 $$->type = Type::get("i1",BoolTy);
Reid Spencer30d0c582007-01-15 00:26:18 +00001993 delete $2; delete $6;
Reid Spencer57f28f92006-12-03 07:10:26 +00001994 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001995 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer30d0c582007-01-15 00:26:18 +00001996 std::string Name1 = getUniqueName($4->val, $3);
1997 std::string Name2 = getUniqueName($6->val, $3);
Reid Spencer52402b02007-01-02 05:45:11 +00001998 *$1 += " " + *$2 + " " + $3->getNewTy() + " " + Name1 + "," + Name2;
Reid Spencer30d0c582007-01-15 00:26:18 +00001999 $$ = $4;
2000 delete $$->val;
2001 $$->val = $1;
Reid Spencerb6673a92007-01-15 02:41:46 +00002002 $$->type = Type::get("i1",BoolTy);
Reid Spencer30d0c582007-01-15 00:26:18 +00002003 delete $2; delete $6;
Reid Spencer229e9362006-12-02 22:14:11 +00002004 }
Reid Spencere7c3c602006-11-30 06:36:44 +00002005 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencerf7bde222006-12-01 22:26:37 +00002006 const char* shiftop = $1->c_str();
2007 if (*$1 == "shr")
Reid Spencer30d0c582007-01-15 00:26:18 +00002008 shiftop = ($2->type->isUnsigned()) ? "lshr" : "ashr";
2009 std::string *val = new std::string(shiftop);
2010 *val += " " + *$2->val + ", " + *$4->val;
2011 $$ = $2;
2012 delete $$->val;
2013 $$->val = val;
2014 delete $1; delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00002015 }
Reid Spencerfcb5df82006-12-01 22:34:43 +00002016 | CastOps ResolvedVal TO Types {
Reid Spencer30d0c582007-01-15 00:26:18 +00002017 std::string source = *$2->val;
Reid Spencerb6673a92007-01-15 02:41:46 +00002018 const Type* SrcTy = $2->type->resolve();
2019 const Type* DstTy = $4->resolve();
Reid Spencer30d0c582007-01-15 00:26:18 +00002020 $$ = $2;
2021 delete $$->val;
2022 $$->val = new std::string();
2023 $$->type = DstTy;
Reid Spencer280d8012006-12-01 23:40:53 +00002024 if (*$1 == "cast") {
Reid Spencer30d0c582007-01-15 00:26:18 +00002025 *$$->val += getCastUpgrade(source, SrcTy, DstTy, false);
Reid Spencera50d5962006-12-02 04:11:07 +00002026 } else {
Reid Spencer30d0c582007-01-15 00:26:18 +00002027 *$$->val += *$1 + " " + source + " to " + DstTy->getNewTy();
Reid Spencer280d8012006-12-01 23:40:53 +00002028 }
Reid Spencerf5626a32007-01-01 01:20:41 +00002029 // Check to see if this is a useless cast of a value to the same name
2030 // and the same type. Such casts will probably cause redefinition errors
2031 // when assembled and perform no code gen action so just remove them.
2032 if (*$1 == "cast" || *$1 == "bitcast")
Reid Spencer319a7302007-01-05 17:20:02 +00002033 if (SrcTy->isInteger() && DstTy->isInteger() &&
2034 SrcTy->getBitWidth() == DstTy->getBitWidth()) {
Reid Spencerf5626a32007-01-01 01:20:41 +00002035 deleteUselessCastFlag = true; // Flag the "Inst" rule
Reid Spencer30d0c582007-01-15 00:26:18 +00002036 deleteUselessCastName = new std::string(*$2->val); // save the name
Reid Spencerf5626a32007-01-01 01:20:41 +00002037 size_t pos = deleteUselessCastName->find_first_of("%\"",0);
2038 if (pos != std::string::npos) {
2039 // remove the type portion before val
2040 deleteUselessCastName->erase(0, pos);
2041 }
2042 }
Reid Spencer30d0c582007-01-15 00:26:18 +00002043 delete $1;
Reid Spencer52402b02007-01-02 05:45:11 +00002044 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002045 }
2046 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer30d0c582007-01-15 00:26:18 +00002047 *$1 += " " + *$2->val + ", " + *$4->val + ", " + *$6->val;
2048 $$ = $2;
2049 delete $$->val;
2050 $$->val = $1;
2051 $$->type = $4->type;
2052 delete $4;
2053 delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00002054 }
2055 | VAARG ResolvedVal ',' Types {
Reid Spencer30d0c582007-01-15 00:26:18 +00002056 *$1 += " " + *$2->val + ", " + $4->getNewTy();
2057 $$ = $2;
2058 delete $$->val;
2059 $$->val = $1;
2060 $$->type = $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00002061 }
2062 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencer30d0c582007-01-15 00:26:18 +00002063 *$1 += " " + *$2->val + ", " + *$4->val;
2064 $$ = $2;
2065 delete $$->val;
2066 $$->val = $1;
2067 $$->type = $$->type->resolve();
2068 $$->type = $$->type->getElementType();
2069 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00002070 }
2071 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer30d0c582007-01-15 00:26:18 +00002072 *$1 += " " + *$2->val + ", " + *$4->val + ", " + *$6->val;
2073 $$ = $2;
2074 delete $$->val;
2075 $$->val = $1;
2076 delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00002077 }
2078 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer30d0c582007-01-15 00:26:18 +00002079 *$1 += " " + *$2->val + ", " + *$4->val + ", " + *$6->val;
2080 $$ = $2;
2081 delete $$->val;
2082 $$->val = $1;
2083 delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00002084 }
2085 | PHI_TOK PHIList {
Reid Spencer30d0c582007-01-15 00:26:18 +00002086 *$1 += " " + *$2->val;
2087 $$ = $2;
2088 delete $2->val;
2089 $$->val = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00002090 }
2091 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Reid Spencer12969882007-01-07 08:07:39 +00002092 // map llvm.isunordered to "fcmp uno"
Reid Spencerb6673a92007-01-15 02:41:46 +00002093 $$ = new Value;
Reid Spencer30d0c582007-01-15 00:26:18 +00002094 if (*$4->val == "%llvm.isunordered.f32" ||
2095 *$4->val == "%llvm.isunordered.f64") {
2096 $$->val = new std::string( "fcmp uno " + *(*$6)[0]->val + ", ");
2097 size_t pos = (*$6)[1]->val->find(' ');
Reid Spencer12969882007-01-07 08:07:39 +00002098 assert(pos != std::string::npos && "no space?");
Reid Spencer30d0c582007-01-15 00:26:18 +00002099 *$$->val += (*$6)[1]->val->substr(pos+1);
Reid Spencerb6673a92007-01-15 02:41:46 +00002100 $$->type = Type::get("i1", BoolTy);
Reid Spencer12969882007-01-07 08:07:39 +00002101 } else {
Reid Spencerc4d96252007-01-13 00:03:30 +00002102 static unsigned upgradeCount = 1;
Reid Spencer30d0c582007-01-15 00:26:18 +00002103 if (*$4->val == "%llvm.va_start" || *$4->val == "%llvm.va_end") {
Reid Spencerf6e54592007-01-13 00:23:06 +00002104 if (!$6->empty()) {
2105 std::string name("%va_upgrade");
2106 name += llvm::utostr(upgradeCount++);
Reid Spencer30d0c582007-01-15 00:26:18 +00002107 $1->insert(0, name + " = bitcast " + *(*$6)[0]->val + " to i8*\n ");
2108 *(*$6)[0]->val = "i8* " + name;
Reid Spencerb6673a92007-01-15 02:41:46 +00002109 (*$6)[0]->type = Type::get("i8", UByteTy)->getPointerType();
Reid Spencerf6e54592007-01-13 00:23:06 +00002110 }
Reid Spencer30d0c582007-01-15 00:26:18 +00002111 } else if (*$4->val == "%llvm.va_copy") {
Reid Spencerc4d96252007-01-13 00:03:30 +00002112 std::string name0("%va_upgrade");
2113 name0 += llvm::utostr(upgradeCount++);
2114 std::string name1("%va_upgrade");
2115 name1 += llvm::utostr(upgradeCount++);
Reid Spencer30d0c582007-01-15 00:26:18 +00002116 $1->insert(0, name0 + " = bitcast " + *(*$6)[0]->val + " to i8*\n " +
2117 name1 + " = bitcast " + *(*$6)[1]->val + " to i8*\n ");
2118 *(*$6)[0]->val = "i8* " + name0;
Reid Spencerb6673a92007-01-15 02:41:46 +00002119 (*$6)[0]->type = Type::get("i8", UByteTy)->getPointerType();
Reid Spencer30d0c582007-01-15 00:26:18 +00002120 *(*$6)[1]->val = "i8* " + name1;
Reid Spencerb6673a92007-01-15 02:41:46 +00002121 (*$6)[0]->type = Type::get("i8", UByteTy)->getPointerType();
Reid Spencerc4d96252007-01-13 00:03:30 +00002122 }
Reid Spencer12969882007-01-07 08:07:39 +00002123 if (!$2->empty())
2124 *$1 += " " + *$2;
2125 if (!$1->empty())
2126 *$1 += " ";
Reid Spencer30d0c582007-01-15 00:26:18 +00002127 *$1 += $3->getNewTy() + " " + *$4->val + "(";
Reid Spencer12969882007-01-07 08:07:39 +00002128 for (unsigned i = 0; i < $6->size(); ++i) {
Reid Spencerb6673a92007-01-15 02:41:46 +00002129 Value* V = (*$6)[i];
2130 *$1 += *V->val;
Reid Spencer12969882007-01-07 08:07:39 +00002131 if (i+1 < $6->size())
2132 *$1 += ", ";
Reid Spencerb6673a92007-01-15 02:41:46 +00002133 delete V;
Reid Spencer12969882007-01-07 08:07:39 +00002134 }
2135 *$1 += ")";
Reid Spencerb6673a92007-01-15 02:41:46 +00002136 $$ = new Value;
Reid Spencer30d0c582007-01-15 00:26:18 +00002137 $$->val = $1;
2138 $$->type = getFunctionReturnType($3);
Reid Spencerf8483652006-12-02 15:16:01 +00002139 }
Reid Spencer30d0c582007-01-15 00:26:18 +00002140 delete $2; delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00002141 }
2142 | MemoryInst ;
2143
2144
2145// IndexList - List of indices for GEP based instructions...
2146IndexList
Reid Spencerf8483652006-12-02 15:16:01 +00002147 : ',' ValueRefList { $$ = $2; }
2148 | /* empty */ { $$ = new ValueList(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00002149 ;
2150
2151OptVolatile
2152 : VOLATILE
2153 | /* empty */ { $$ = new std::string(); }
2154 ;
2155
2156MemoryInst : MALLOC Types OptCAlign {
Reid Spencer52402b02007-01-02 05:45:11 +00002157 *$1 += " " + $2->getNewTy();
Reid Spencere7c3c602006-11-30 06:36:44 +00002158 if (!$3->empty())
2159 *$1 += " " + *$3;
Reid Spencerb6673a92007-01-15 02:41:46 +00002160 $$ = new Value;
Reid Spencer30d0c582007-01-15 00:26:18 +00002161 $$->val = $1;
2162 $$->type = $2->getPointerType();
Reid Spencer319a7302007-01-05 17:20:02 +00002163 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002164 }
2165 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencer30d0c582007-01-15 00:26:18 +00002166 std::string Name = getUniqueName($5->val, $4);
Reid Spencer52402b02007-01-02 05:45:11 +00002167 *$1 += " " + $2->getNewTy() + ", " + $4->getNewTy() + " " + Name;
Reid Spencere7c3c602006-11-30 06:36:44 +00002168 if (!$6->empty())
2169 *$1 += " " + *$6;
Reid Spencerb6673a92007-01-15 02:41:46 +00002170 $$ = new Value;
Reid Spencer30d0c582007-01-15 00:26:18 +00002171 $$->val = $1;
2172 $$->type = $2->getPointerType();
2173 delete $5; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00002174 }
2175 | ALLOCA Types OptCAlign {
Reid Spencer52402b02007-01-02 05:45:11 +00002176 *$1 += " " + $2->getNewTy();
Reid Spencere7c3c602006-11-30 06:36:44 +00002177 if (!$3->empty())
2178 *$1 += " " + *$3;
Reid Spencerb6673a92007-01-15 02:41:46 +00002179 $$ = new Value;
Reid Spencer30d0c582007-01-15 00:26:18 +00002180 $$->val = $1;
2181 $$->type = $2->getPointerType();
Reid Spencer319a7302007-01-05 17:20:02 +00002182 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002183 }
2184 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencer30d0c582007-01-15 00:26:18 +00002185 std::string Name = getUniqueName($5->val, $4);
Reid Spencer52402b02007-01-02 05:45:11 +00002186 *$1 += " " + $2->getNewTy() + ", " + $4->getNewTy() + " " + Name;
Reid Spencere7c3c602006-11-30 06:36:44 +00002187 if (!$6->empty())
2188 *$1 += " " + *$6;
Reid Spencer30d0c582007-01-15 00:26:18 +00002189 $$ = $5;
2190 delete $$->val;
2191 $$->val = $1;
2192 $$->type = $2->getPointerType();
2193 delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00002194 }
2195 | FREE ResolvedVal {
Reid Spencer30d0c582007-01-15 00:26:18 +00002196 *$1 += " " + *$2->val;
2197 $$ = $2;
2198 delete $2->val;
2199 $$->val = $1;
Reid Spencerb6673a92007-01-15 02:41:46 +00002200 $$->type = Type::get("void", VoidTy);
Reid Spencere7c3c602006-11-30 06:36:44 +00002201 }
2202 | OptVolatile LOAD Types ValueRef {
Reid Spencer30d0c582007-01-15 00:26:18 +00002203 std::string Name = getUniqueName($4->val, $3);
Reid Spencere7c3c602006-11-30 06:36:44 +00002204 if (!$1->empty())
2205 *$1 += " ";
Reid Spencer52402b02007-01-02 05:45:11 +00002206 *$1 += *$2 + " " + $3->getNewTy() + " " + Name;
Reid Spencer30d0c582007-01-15 00:26:18 +00002207 $$ = $4;
2208 delete $$->val;
2209 $$->val = $1;
2210 $$->type = $3->getElementType();
2211 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00002212 }
2213 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
Reid Spencer30d0c582007-01-15 00:26:18 +00002214 std::string Name = getUniqueName($6->val, $5);
Reid Spencere7c3c602006-11-30 06:36:44 +00002215 if (!$1->empty())
2216 *$1 += " ";
Reid Spencer30d0c582007-01-15 00:26:18 +00002217 *$1 += *$2 + " " + *$3->val + ", " + $5->getNewTy() + " " + Name;
2218 $$ = $3;
2219 delete $$->val;
2220 $$->val = $1;
Reid Spencerb6673a92007-01-15 02:41:46 +00002221 $$->type = Type::get("void", VoidTy);
Reid Spencer30d0c582007-01-15 00:26:18 +00002222 delete $2; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00002223 }
2224 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer30d0c582007-01-15 00:26:18 +00002225 std::string Name = getUniqueName($3->val, $2);
Reid Spencerf459d392006-12-02 16:19:52 +00002226 // Upgrade the indices
2227 for (unsigned i = 0; i < $4->size(); ++i) {
Reid Spencerb6673a92007-01-15 02:41:46 +00002228 Value* V = (*$4)[i];
2229 if (V->type->isUnsigned() && !V->isConstant() &&
2230 V->type->getBitWidth() < 64) {
2231 *O << " %gep_upgrade" << UniqueNameCounter << " = zext " << *V->val
Reid Spencer71d2ec92006-12-31 06:02:26 +00002232 << " to i64\n";
Reid Spencerb6673a92007-01-15 02:41:46 +00002233 *V->val = "i64 %gep_upgrade" + llvm::utostr(UniqueNameCounter++);
2234 V->type = Type::get("i64",ULongTy);
Reid Spencerf459d392006-12-02 16:19:52 +00002235 }
2236 }
Reid Spencer52402b02007-01-02 05:45:11 +00002237 *$1 += " " + $2->getNewTy() + " " + Name;
Reid Spencerf8483652006-12-02 15:16:01 +00002238 for (unsigned i = 0; i < $4->size(); ++i) {
Reid Spencerb6673a92007-01-15 02:41:46 +00002239 Value* V = (*$4)[i];
2240 *$1 += ", " + *V->val;
Reid Spencerf8483652006-12-02 15:16:01 +00002241 }
Reid Spencer30d0c582007-01-15 00:26:18 +00002242 $$ = $3;
2243 delete $$->val;
2244 $$->val = $1;
2245 $$->type = getGEPIndexedType($2,$4);
2246 for (unsigned i = 0; i < $4->size(); ++i)
2247 delete (*$4)[i];
2248 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00002249 };
2250
2251%%
2252
2253int yyerror(const char *ErrorMsg) {
2254 std::string where
2255 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2256 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
Reid Spencer319a7302007-01-05 17:20:02 +00002257 std::string errMsg = where + "error: " + std::string(ErrorMsg) +
2258 " while reading ";
Reid Spencere7c3c602006-11-30 06:36:44 +00002259 if (yychar == YYEMPTY || yychar == 0)
2260 errMsg += "end-of-file.";
2261 else
2262 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
Reid Spencer71d2ec92006-12-31 06:02:26 +00002263 std::cerr << "llvm-upgrade: " << errMsg << '\n';
Chris Lattner37e01c52007-01-04 18:46:42 +00002264 *O << "llvm-upgrade parse failed.\n";
Reid Spencere7c3c602006-11-30 06:36:44 +00002265 exit(1);
2266}
Reid Spencer319a7302007-01-05 17:20:02 +00002267
Reid Spencer30d0c582007-01-15 00:26:18 +00002268void warning(const std::string& ErrorMsg) {
Reid Spencer319a7302007-01-05 17:20:02 +00002269 std::string where
2270 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2271 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
2272 std::string errMsg = where + "warning: " + std::string(ErrorMsg) +
2273 " while reading ";
2274 if (yychar == YYEMPTY || yychar == 0)
2275 errMsg += "end-of-file.";
2276 else
2277 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
2278 std::cerr << "llvm-upgrade: " << errMsg << '\n';
2279}