blob: ca8ee4f52e00a1b48d8094e3142b7ce64717fd5a [file] [log] [blame]
Reid Spencer96839be2006-11-30 16:50:26 +00001//===-- UpgradeParser.y - Upgrade parser for llvm assmbly -------*- C++ -*-===//
Reid Spencere7c3c602006-11-30 06:36:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Reid Spencer and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Reid Spencer96839be2006-11-30 16:50:26 +000010// This file implements the bison parser for LLVM 1.9 assembly language.
Reid Spencere7c3c602006-11-30 06:36:44 +000011//
12//===----------------------------------------------------------------------===//
13
14%{
Reid Spencer319a7302007-01-05 17:20:02 +000015#include "UpgradeInternals.h"
Reid Spencere7c3c602006-11-30 06:36:44 +000016#include <algorithm>
Reid Spencera50d5962006-12-02 04:11:07 +000017#include <map>
Reid Spencere7c3c602006-11-30 06:36:44 +000018#include <utility>
19#include <iostream>
20
Reid Spencere77e35e2006-12-01 20:26:20 +000021#define YYERROR_VERBOSE 1
Reid Spencer96839be2006-11-30 16:50:26 +000022#define YYINCLUDED_STDLIB_H
Reid Spencere77e35e2006-12-01 20:26:20 +000023#define YYDEBUG 1
Reid Spencere7c3c602006-11-30 06:36:44 +000024
25int yylex(); // declaration" of xxx warnings.
26int yyparse();
Reid Spencere77e35e2006-12-01 20:26:20 +000027extern int yydebug;
Reid Spencere7c3c602006-11-30 06:36:44 +000028
29static std::string CurFilename;
Reid Spencere7c3c602006-11-30 06:36:44 +000030static std::ostream *O = 0;
Reid Spencer96839be2006-11-30 16:50:26 +000031std::istream* LexInput = 0;
Reid Spencere77e35e2006-12-01 20:26:20 +000032unsigned SizeOfPointer = 32;
Reid Spencerf459d392006-12-02 16:19:52 +000033static uint64_t unique = 1;
Reid Spencer96839be2006-11-30 16:50:26 +000034
Reid Spencer71d2ec92006-12-31 06:02:26 +000035// This bool controls whether attributes are ever added to function declarations
36// definitions and calls.
37static bool AddAttributes = false;
38
Reid Spencerf5626a32007-01-01 01:20:41 +000039// This bool is used to communicate between the InstVal and Inst rules about
40// whether or not a cast should be deleted. When the flag is set, InstVal has
41// determined that the cast is a candidate. However, it can only be deleted if
42// the value being casted is the same value name as the instruction. The Inst
43// rule makes that comparison if the flag is set and comments out the
44// instruction if they match.
45static bool deleteUselessCastFlag = false;
46static std::string* deleteUselessCastName = 0;
47
Reid Spencer319a7302007-01-05 17:20:02 +000048typedef std::vector<const TypeInfo*> TypeVector;
Reid Spencera50d5962006-12-02 04:11:07 +000049static TypeVector EnumeratedTypes;
Reid Spencer319a7302007-01-05 17:20:02 +000050typedef std::map<std::string,const TypeInfo*> TypeMap;
Reid Spencera50d5962006-12-02 04:11:07 +000051static TypeMap NamedTypes;
Reid Spencer319a7302007-01-05 17:20:02 +000052typedef std::map<const TypeInfo*,std::string> TypePlaneMap;
53typedef std::map<std::string,TypePlaneMap> GlobalsTypeMap;
54static GlobalsTypeMap Globals;
55
56static void warning(const std::string& msg);
Reid Spencera50d5962006-12-02 04:11:07 +000057
Reid Spencerf8483652006-12-02 15:16:01 +000058void destroy(ValueList* VL) {
59 while (!VL->empty()) {
60 ValueInfo& VI = VL->back();
61 VI.destroy();
62 VL->pop_back();
63 }
64 delete VL;
65}
66
Reid Spencer96839be2006-11-30 16:50:26 +000067void UpgradeAssembly(const std::string &infile, std::istream& in,
Reid Spencer71d2ec92006-12-31 06:02:26 +000068 std::ostream &out, bool debug, bool addAttrs)
Reid Spencere7c3c602006-11-30 06:36:44 +000069{
70 Upgradelineno = 1;
71 CurFilename = infile;
Reid Spencer96839be2006-11-30 16:50:26 +000072 LexInput = &in;
Reid Spencere77e35e2006-12-01 20:26:20 +000073 yydebug = debug;
Reid Spencer71d2ec92006-12-31 06:02:26 +000074 AddAttributes = addAttrs;
Reid Spencere7c3c602006-11-30 06:36:44 +000075 O = &out;
76
77 if (yyparse()) {
78 std::cerr << "Parse failed.\n";
Chris Lattner37e01c52007-01-04 18:46:42 +000079 out << "llvm-upgrade parse failed.\n";
Reid Spencere7c3c602006-11-30 06:36:44 +000080 exit(1);
81 }
82}
83
Reid Spencer319a7302007-01-05 17:20:02 +000084TypeInfo::TypeRegMap TypeInfo::registry;
85
86const TypeInfo* TypeInfo::get(const std::string &newType, Types oldType) {
87 TypeInfo* Ty = new TypeInfo();
88 Ty->newTy = newType;
89 Ty->oldTy = oldType;
90 return add_new_type(Ty);
91}
92
93const TypeInfo* TypeInfo::get(const std::string& newType, Types oldType,
94 const TypeInfo* eTy, const TypeInfo* rTy) {
95 TypeInfo* Ty= new TypeInfo();
96 Ty->newTy = newType;
97 Ty->oldTy = oldType;
98 Ty->elemTy = const_cast<TypeInfo*>(eTy);
99 Ty->resultTy = const_cast<TypeInfo*>(rTy);
100 return add_new_type(Ty);
101}
102
103const TypeInfo* TypeInfo::get(const std::string& newType, Types oldType,
104 const TypeInfo *eTy, uint64_t elems) {
105 TypeInfo* Ty = new TypeInfo();
106 Ty->newTy = newType;
107 Ty->oldTy = oldType;
108 Ty->elemTy = const_cast<TypeInfo*>(eTy);
109 Ty->nelems = elems;
110 return add_new_type(Ty);
111}
112
113const TypeInfo* TypeInfo::get(const std::string& newType, Types oldType,
114 TypeList* TL) {
115 TypeInfo* Ty = new TypeInfo();
116 Ty->newTy = newType;
117 Ty->oldTy = oldType;
118 Ty->elements = TL;
119 return add_new_type(Ty);
120}
121
122const TypeInfo* TypeInfo::get(const std::string& newType, const TypeInfo* resTy,
123 TypeList* TL) {
124 TypeInfo* Ty = new TypeInfo();
125 Ty->newTy = newType;
126 Ty->oldTy = FunctionTy;
127 Ty->resultTy = const_cast<TypeInfo*>(resTy);
128 Ty->elements = TL;
129 return add_new_type(Ty);
130}
131
132const TypeInfo* TypeInfo::resolve() const {
133 if (isUnresolved()) {
134 if (getNewTy()[0] == '%' && isdigit(getNewTy()[1])) {
135 unsigned ref = atoi(&((getNewTy().c_str())[1])); // skip the %
Reid Spencereff838e2007-01-03 23:45:42 +0000136 if (ref < EnumeratedTypes.size()) {
Reid Spencer319a7302007-01-05 17:20:02 +0000137 return EnumeratedTypes[ref];
Reid Spencereff838e2007-01-03 23:45:42 +0000138 } else {
139 std::string msg("Can't resolve numbered type: ");
Reid Spencer319a7302007-01-05 17:20:02 +0000140 msg += getNewTy();
Reid Spencereff838e2007-01-03 23:45:42 +0000141 yyerror(msg.c_str());
142 }
Reid Spencer78720742006-12-02 20:21:22 +0000143 } else {
Reid Spencer319a7302007-01-05 17:20:02 +0000144 TypeMap::iterator I = NamedTypes.find(getNewTy());
Reid Spencereff838e2007-01-03 23:45:42 +0000145 if (I != NamedTypes.end()) {
Reid Spencer319a7302007-01-05 17:20:02 +0000146 return I->second;
Reid Spencereff838e2007-01-03 23:45:42 +0000147 } else {
148 std::string msg("Cannot resolve type: ");
Reid Spencer319a7302007-01-05 17:20:02 +0000149 msg += getNewTy();
Reid Spencereff838e2007-01-03 23:45:42 +0000150 yyerror(msg.c_str());
151 }
Reid Spencera50d5962006-12-02 04:11:07 +0000152 }
Reid Spencer280d8012006-12-01 23:40:53 +0000153 }
Reid Spencera50d5962006-12-02 04:11:07 +0000154 // otherwise its already resolved.
Reid Spencer319a7302007-01-05 17:20:02 +0000155 return this;
156}
157
158bool TypeInfo::operator<(const TypeInfo& that) const {
159 if (this == &that)
160 return false;
161 if (oldTy != that.oldTy)
162 return oldTy < that.oldTy;
163 switch (oldTy) {
164 case UpRefTy: {
165 unsigned thisUp = this->getUpRefNum();
166 unsigned thatUp = that.getUpRefNum();
167 return thisUp < thatUp;
168 }
169 case PackedTy:
170 case ArrayTy:
171 if (this->nelems != that.nelems)
172 return nelems < that.nelems;
173 case PointerTy: {
174 const TypeInfo* thisTy = this->elemTy;
175 const TypeInfo* thatTy = that.elemTy;
176 return *thisTy < *thatTy;
177 }
178 case FunctionTy: {
179 const TypeInfo* thisTy = this->resultTy;
180 const TypeInfo* thatTy = that.resultTy;
181 if (!thisTy->sameOldTyAs(thatTy))
182 return *thisTy < *thatTy;
183 /* FALL THROUGH */
184 }
185 case StructTy:
186 case PackedStructTy: {
187 if (elements->size() != that.elements->size())
188 return elements->size() < that.elements->size();
189 for (unsigned i = 0; i < elements->size(); i++) {
190 const TypeInfo* thisTy = (*this->elements)[i];
191 const TypeInfo* thatTy = (*that.elements)[i];
192 if (!thisTy->sameOldTyAs(thatTy))
193 return *thisTy < *thatTy;
194 }
195 break;
196 }
197 case UnresolvedTy:
198 return this->newTy < that.newTy;
199 default:
200 break;
201 }
202 return false;
203}
204
205bool TypeInfo::sameOldTyAs(const TypeInfo* that) const {
206 if (that == 0)
207 return false;
208 if ( this == that )
209 return true;
210 if (oldTy != that->oldTy)
211 return false;
212 switch (oldTy) {
213 case PackedTy:
214 case ArrayTy:
215 if (nelems != that->nelems)
216 return false;
217 /* FALL THROUGH */
218 case PointerTy: {
219 const TypeInfo* thisTy = this->elemTy;
220 const TypeInfo* thatTy = that->elemTy;
221 return thisTy->sameOldTyAs(thatTy);
222 }
223 case FunctionTy: {
224 const TypeInfo* thisTy = this->resultTy;
225 const TypeInfo* thatTy = that->resultTy;
226 if (!thisTy->sameOldTyAs(thatTy))
227 return false;
228 /* FALL THROUGH */
229 }
230 case StructTy:
231 case PackedStructTy: {
232 if (elements->size() != that->elements->size())
233 return false;
234 for (unsigned i = 0; i < elements->size(); i++) {
235 const TypeInfo* thisTy = (*this->elements)[i];
236 const TypeInfo* thatTy = (*that->elements)[i];
237 if (!thisTy->sameOldTyAs(thatTy))
238 return false;
239 }
240 return true;
241 }
242 case UnresolvedTy:
243 return this->newTy == that->newTy;
244 default:
245 return true; // for all others oldTy == that->oldTy is sufficient
246 }
247 return true;
248}
249
250bool TypeInfo::isUnresolvedDeep() const {
251 switch (oldTy) {
252 case UnresolvedTy:
253 return true;
254 case PackedTy:
255 case ArrayTy:
256 case PointerTy:
257 return elemTy->isUnresolvedDeep();
258 case PackedStructTy:
259 case StructTy:
260 for (unsigned i = 0; i < elements->size(); i++)
261 if ((*elements)[i]->isUnresolvedDeep())
262 return true;
263 return false;
264 default:
265 return false;
266 }
267}
268
269unsigned TypeInfo::getBitWidth() const {
270 switch (oldTy) {
271 default:
272 case LabelTy:
273 case VoidTy : return 0;
274 case BoolTy : return 1;
275 case SByteTy: case UByteTy : return 8;
276 case ShortTy: case UShortTy : return 16;
277 case IntTy: case UIntTy: case FloatTy: return 32;
278 case LongTy: case ULongTy: case DoubleTy : return 64;
279 case PointerTy: return SizeOfPointer; // global var
280 case PackedTy:
281 case ArrayTy:
282 return nelems * elemTy->getBitWidth();
283 case StructTy:
284 case PackedStructTy: {
285 uint64_t size = 0;
286 for (unsigned i = 0; i < elements->size(); i++) {
287 size += (*elements)[i]->getBitWidth();
288 }
289 return size;
290 }
291 }
292}
293
294const TypeInfo* TypeInfo::getIndexedType(const ValueInfo& VI) const {
295 if (isStruct()) {
296 if (VI.isConstant() && VI.type->isInteger()) {
297 size_t pos = VI.val->find(' ') + 1;
298 if (pos < VI.val->size()) {
299 uint64_t idx = atoi(VI.val->substr(pos).c_str());
300 return (*elements)[idx];
301 } else {
302 yyerror("Invalid value for constant integer");
303 return 0;
304 }
305 } else {
306 yyerror("Structure requires constant index");
307 return 0;
308 }
309 }
310 if (isArray() || isPacked() || isPointer())
311 return elemTy;
312 yyerror("Invalid type for getIndexedType");
313 return 0;
314}
315
316TypeInfo& TypeInfo::operator=(const TypeInfo& that) {
317 oldTy = that.oldTy;
318 nelems = that.nelems;
319 newTy = that.newTy;
320 elemTy = that.elemTy;
321 resultTy = that.resultTy;
322 if (that.elements) {
323 elements = new TypeList(that.elements->size());
324 *elements = *that.elements;
325 } else {
326 elements = 0;
327 }
328 return *this;
329}
330
331const TypeInfo* TypeInfo::add_new_type(TypeInfo* newTy) {
332 TypeRegMap::iterator I = registry.find(newTy);
333 if (I != registry.end()) {
334 delete newTy;
335 return *I;
336 }
337 registry.insert(newTy);
338 return newTy;
Reid Spencer280d8012006-12-01 23:40:53 +0000339}
340
Reid Spencera50d5962006-12-02 04:11:07 +0000341static const char* getCastOpcode(
Reid Spencer52402b02007-01-02 05:45:11 +0000342 std::string& Source, const TypeInfo* SrcTy, const TypeInfo* DstTy)
Reid Spencera50d5962006-12-02 04:11:07 +0000343{
Reid Spencer52402b02007-01-02 05:45:11 +0000344 unsigned SrcBits = SrcTy->getBitWidth();
345 unsigned DstBits = DstTy->getBitWidth();
Reid Spencere77e35e2006-12-01 20:26:20 +0000346 const char* opcode = "bitcast";
347 // Run through the possibilities ...
Reid Spencer52402b02007-01-02 05:45:11 +0000348 if (DstTy->isIntegral()) { // Casting to integral
349 if (SrcTy->isIntegral()) { // Casting from integral
Reid Spencere77e35e2006-12-01 20:26:20 +0000350 if (DstBits < SrcBits)
351 opcode = "trunc";
352 else if (DstBits > SrcBits) { // its an extension
Reid Spencer52402b02007-01-02 05:45:11 +0000353 if (SrcTy->isSigned())
Reid Spencere77e35e2006-12-01 20:26:20 +0000354 opcode ="sext"; // signed -> SEXT
355 else
356 opcode = "zext"; // unsigned -> ZEXT
357 } else {
358 opcode = "bitcast"; // Same size, No-op cast
359 }
Reid Spencer52402b02007-01-02 05:45:11 +0000360 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
361 if (DstTy->isSigned())
Reid Spencere77e35e2006-12-01 20:26:20 +0000362 opcode = "fptosi"; // FP -> sint
363 else
364 opcode = "fptoui"; // FP -> uint
Reid Spencer52402b02007-01-02 05:45:11 +0000365 } else if (SrcTy->isPacked()) {
366 assert(DstBits == SrcTy->getBitWidth() &&
Reid Spencere77e35e2006-12-01 20:26:20 +0000367 "Casting packed to integer of different width");
368 opcode = "bitcast"; // same size, no-op cast
369 } else {
Reid Spencer52402b02007-01-02 05:45:11 +0000370 assert(SrcTy->isPointer() &&
Reid Spencere77e35e2006-12-01 20:26:20 +0000371 "Casting from a value that is not first-class type");
372 opcode = "ptrtoint"; // ptr -> int
373 }
Reid Spencer52402b02007-01-02 05:45:11 +0000374 } else if (DstTy->isFloatingPoint()) { // Casting to floating pt
375 if (SrcTy->isIntegral()) { // Casting from integral
376 if (SrcTy->isSigned())
Reid Spencere77e35e2006-12-01 20:26:20 +0000377 opcode = "sitofp"; // sint -> FP
378 else
379 opcode = "uitofp"; // uint -> FP
Reid Spencer52402b02007-01-02 05:45:11 +0000380 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencere77e35e2006-12-01 20:26:20 +0000381 if (DstBits < SrcBits) {
382 opcode = "fptrunc"; // FP -> smaller FP
383 } else if (DstBits > SrcBits) {
384 opcode = "fpext"; // FP -> larger FP
385 } else {
386 opcode ="bitcast"; // same size, no-op cast
387 }
Reid Spencer52402b02007-01-02 05:45:11 +0000388 } else if (SrcTy->isPacked()) {
389 assert(DstBits == SrcTy->getBitWidth() &&
Reid Spencere77e35e2006-12-01 20:26:20 +0000390 "Casting packed to floating point of different width");
391 opcode = "bitcast"; // same size, no-op cast
392 } else {
393 assert(0 && "Casting pointer or non-first class to float");
394 }
Reid Spencer52402b02007-01-02 05:45:11 +0000395 } else if (DstTy->isPacked()) {
396 if (SrcTy->isPacked()) {
397 assert(DstTy->getBitWidth() == SrcTy->getBitWidth() &&
Reid Spencere77e35e2006-12-01 20:26:20 +0000398 "Casting packed to packed of different widths");
399 opcode = "bitcast"; // packed -> packed
Reid Spencer52402b02007-01-02 05:45:11 +0000400 } else if (DstTy->getBitWidth() == SrcBits) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000401 opcode = "bitcast"; // float/int -> packed
402 } else {
403 assert(!"Illegal cast to packed (wrong type or size)");
404 }
Reid Spencer52402b02007-01-02 05:45:11 +0000405 } else if (DstTy->isPointer()) {
406 if (SrcTy->isPointer()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000407 opcode = "bitcast"; // ptr -> ptr
Reid Spencer52402b02007-01-02 05:45:11 +0000408 } else if (SrcTy->isIntegral()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000409 opcode = "inttoptr"; // int -> ptr
410 } else {
Reid Spencera50d5962006-12-02 04:11:07 +0000411 assert(!"Casting invalid type to pointer");
Reid Spencere77e35e2006-12-01 20:26:20 +0000412 }
413 } else {
414 assert(!"Casting to type that is not first-class");
415 }
416 return opcode;
417}
418
Reid Spencer319a7302007-01-05 17:20:02 +0000419static std::string getCastUpgrade(const std::string& Src, const TypeInfo* SrcTy,
420 const TypeInfo* DstTy, bool isConst)
Reid Spencera50d5962006-12-02 04:11:07 +0000421{
422 std::string Result;
423 std::string Source = Src;
Reid Spencer52402b02007-01-02 05:45:11 +0000424 if (SrcTy->isFloatingPoint() && DstTy->isPointer()) {
Reid Spencera50d5962006-12-02 04:11:07 +0000425 // fp -> ptr cast is no longer supported but we must upgrade this
426 // by doing a double cast: fp -> int -> ptr
427 if (isConst)
Reid Spencer71d2ec92006-12-31 06:02:26 +0000428 Source = "i64 fptoui(" + Source + " to i64)";
Reid Spencera50d5962006-12-02 04:11:07 +0000429 else {
Reid Spencerf459d392006-12-02 16:19:52 +0000430 *O << " %cast_upgrade" << unique << " = fptoui " << Source
Reid Spencer71d2ec92006-12-31 06:02:26 +0000431 << " to i64\n";
432 Source = "i64 %cast_upgrade" + llvm::utostr(unique);
Reid Spencera50d5962006-12-02 04:11:07 +0000433 }
434 // Update the SrcTy for the getCastOpcode call below
Reid Spencer319a7302007-01-05 17:20:02 +0000435 SrcTy = TypeInfo::get("i64", ULongTy);
Reid Spencer52402b02007-01-02 05:45:11 +0000436 } else if (DstTy->isBool()) {
437 // cast type %x to bool was previously defined as setne type %x, null
438 // The cast semantic is now to truncate, not compare so we must retain
439 // the original intent by replacing the cast with a setne
440 const char* comparator = SrcTy->isPointer() ? ", null" :
441 (SrcTy->isFloatingPoint() ? ", 0.0" :
442 (SrcTy->isBool() ? ", false" : ", 0"));
443 const char* compareOp = SrcTy->isFloatingPoint() ? "fcmp one " : "icmp ne ";
Reid Spencer187ccf82006-12-09 16:57:22 +0000444 if (isConst) {
445 Result = "(" + Source + comparator + ")";
446 Result = compareOp + Result;
447 } else
448 Result = compareOp + Source + comparator;
Reid Spencera50d5962006-12-02 04:11:07 +0000449 return Result; // skip cast processing below
450 }
Reid Spencer319a7302007-01-05 17:20:02 +0000451 SrcTy = SrcTy->resolve();
452 DstTy = DstTy->resolve();
Reid Spencera50d5962006-12-02 04:11:07 +0000453 std::string Opcode(getCastOpcode(Source, SrcTy, DstTy));
454 if (isConst)
Reid Spencer52402b02007-01-02 05:45:11 +0000455 Result += Opcode + "( " + Source + " to " + DstTy->getNewTy() + ")";
Reid Spencera50d5962006-12-02 04:11:07 +0000456 else
Reid Spencer52402b02007-01-02 05:45:11 +0000457 Result += Opcode + " " + Source + " to " + DstTy->getNewTy();
Reid Spencera50d5962006-12-02 04:11:07 +0000458 return Result;
459}
460
Reid Spencer319a7302007-01-05 17:20:02 +0000461const char* getDivRemOpcode(const std::string& opcode, const TypeInfo* TI) {
Reid Spencer78720742006-12-02 20:21:22 +0000462 const char* op = opcode.c_str();
Reid Spencer319a7302007-01-05 17:20:02 +0000463 const TypeInfo* Ty = TI->resolve();
Reid Spencer52402b02007-01-02 05:45:11 +0000464 if (Ty->isPacked())
465 Ty = Ty->getElementType();
Reid Spencer78720742006-12-02 20:21:22 +0000466 if (opcode == "div")
Reid Spencer52402b02007-01-02 05:45:11 +0000467 if (Ty->isFloatingPoint())
Reid Spencer78720742006-12-02 20:21:22 +0000468 op = "fdiv";
Reid Spencer52402b02007-01-02 05:45:11 +0000469 else if (Ty->isUnsigned())
Reid Spencer78720742006-12-02 20:21:22 +0000470 op = "udiv";
Reid Spencer52402b02007-01-02 05:45:11 +0000471 else if (Ty->isSigned())
Reid Spencer78720742006-12-02 20:21:22 +0000472 op = "sdiv";
473 else
474 yyerror("Invalid type for div instruction");
475 else if (opcode == "rem")
Reid Spencer52402b02007-01-02 05:45:11 +0000476 if (Ty->isFloatingPoint())
Reid Spencer78720742006-12-02 20:21:22 +0000477 op = "frem";
Reid Spencer52402b02007-01-02 05:45:11 +0000478 else if (Ty->isUnsigned())
Reid Spencer78720742006-12-02 20:21:22 +0000479 op = "urem";
Reid Spencer52402b02007-01-02 05:45:11 +0000480 else if (Ty->isSigned())
Reid Spencer78720742006-12-02 20:21:22 +0000481 op = "srem";
482 else
483 yyerror("Invalid type for rem instruction");
484 return op;
485}
Reid Spencer229e9362006-12-02 22:14:11 +0000486
487std::string
Reid Spencer52402b02007-01-02 05:45:11 +0000488getCompareOp(const std::string& setcc, const TypeInfo* TI) {
Reid Spencer229e9362006-12-02 22:14:11 +0000489 assert(setcc.length() == 5);
490 char cc1 = setcc[3];
491 char cc2 = setcc[4];
492 assert(cc1 == 'e' || cc1 == 'n' || cc1 == 'l' || cc1 == 'g');
493 assert(cc2 == 'q' || cc2 == 'e' || cc2 == 'e' || cc2 == 't');
494 std::string result("xcmp xxx");
495 result[6] = cc1;
496 result[7] = cc2;
Reid Spencer52402b02007-01-02 05:45:11 +0000497 if (TI->isFloatingPoint()) {
Reid Spencer229e9362006-12-02 22:14:11 +0000498 result[0] = 'f';
Reid Spencere4d87aa2006-12-23 06:05:41 +0000499 result[5] = 'o';
Reid Spencerf0cf1322006-12-07 04:23:03 +0000500 if (cc1 == 'n')
501 result[5] = 'u'; // NE maps to unordered
502 else
503 result[5] = 'o'; // everything else maps to ordered
Reid Spencer52402b02007-01-02 05:45:11 +0000504 } else if (TI->isIntegral() || TI->isPointer()) {
Reid Spencer229e9362006-12-02 22:14:11 +0000505 result[0] = 'i';
506 if ((cc1 == 'e' && cc2 == 'q') || (cc1 == 'n' && cc2 == 'e'))
507 result.erase(5,1);
Reid Spencer52402b02007-01-02 05:45:11 +0000508 else if (TI->isSigned())
Reid Spencer229e9362006-12-02 22:14:11 +0000509 result[5] = 's';
Reid Spencer52402b02007-01-02 05:45:11 +0000510 else if (TI->isUnsigned() || TI->isPointer() || TI->isBool())
Reid Spencer229e9362006-12-02 22:14:11 +0000511 result[5] = 'u';
512 else
513 yyerror("Invalid integral type for setcc");
514 }
515 return result;
516}
517
Reid Spencer319a7302007-01-05 17:20:02 +0000518static const TypeInfo* getFunctionReturnType(const TypeInfo* PFTy) {
519 PFTy = PFTy->resolve();
Reid Spencer52402b02007-01-02 05:45:11 +0000520 if (PFTy->isPointer()) {
Reid Spencer319a7302007-01-05 17:20:02 +0000521 const TypeInfo* ElemTy = PFTy->getElementType();
522 ElemTy = ElemTy->resolve();
Reid Spencer52402b02007-01-02 05:45:11 +0000523 if (ElemTy->isFunction())
Reid Spencer319a7302007-01-05 17:20:02 +0000524 return ElemTy->getResultType();
Reid Spencer52402b02007-01-02 05:45:11 +0000525 } else if (PFTy->isFunction()) {
Reid Spencer319a7302007-01-05 17:20:02 +0000526 return PFTy->getResultType();
Reid Spencer52402b02007-01-02 05:45:11 +0000527 }
Reid Spencer319a7302007-01-05 17:20:02 +0000528 return PFTy;
Reid Spencer52402b02007-01-02 05:45:11 +0000529}
530
Reid Spencer319a7302007-01-05 17:20:02 +0000531typedef std::vector<const TypeInfo*> UpRefStack;
532static const TypeInfo* ResolveUpReference(const TypeInfo* Ty,
533 UpRefStack* stack) {
Reid Spencereff838e2007-01-03 23:45:42 +0000534 assert(Ty->isUpReference() && "Can't resolve a non-upreference");
Reid Spencer319a7302007-01-05 17:20:02 +0000535 unsigned upref = Ty->getUpRefNum();
Reid Spencereff838e2007-01-03 23:45:42 +0000536 assert(upref < stack->size() && "Invalid up reference");
537 return (*stack)[upref - stack->size() - 1];
538}
539
Reid Spencer319a7302007-01-05 17:20:02 +0000540static const TypeInfo* getGEPIndexedType(const TypeInfo* PTy, ValueList* idxs) {
541 const TypeInfo* Result = PTy = PTy->resolve();
Reid Spencer52402b02007-01-02 05:45:11 +0000542 assert(PTy->isPointer() && "GEP Operand is not a pointer?");
Reid Spencereff838e2007-01-03 23:45:42 +0000543 UpRefStack stack;
544 for (unsigned i = 0; i < idxs->size(); ++i) {
Reid Spencer52402b02007-01-02 05:45:11 +0000545 if (Result->isComposite()) {
546 Result = Result->getIndexedType((*idxs)[i]);
Reid Spencer319a7302007-01-05 17:20:02 +0000547 Result = Result->resolve();
Reid Spencereff838e2007-01-03 23:45:42 +0000548 stack.push_back(Result);
Reid Spencer52402b02007-01-02 05:45:11 +0000549 } else
550 yyerror("Invalid type for index");
551 }
Reid Spencereff838e2007-01-03 23:45:42 +0000552 // Resolve upreferences so we can return a more natural type
553 if (Result->isPointer()) {
554 if (Result->getElementType()->isUpReference()) {
555 stack.push_back(Result);
556 Result = ResolveUpReference(Result->getElementType(), &stack);
557 }
558 } else if (Result->isUpReference()) {
559 Result = ResolveUpReference(Result->getElementType(), &stack);
560 }
Reid Spencer52402b02007-01-02 05:45:11 +0000561 return Result->getPointerType();
562}
563
564static std::string makeUniqueName(const std::string *Name, bool isSigned) {
565 const char *suffix = ".u";
566 if (isSigned)
567 suffix = ".s";
568 if ((*Name)[Name->size()-1] == '"') {
569 std::string Result(*Name);
570 Result.insert(Name->size()-1, suffix);
571 return Result;
572 }
573 return *Name + suffix;
574}
575
576// This function handles appending .u or .s to integer value names that
577// were previously unsigned or signed, respectively. This avoids name
578// collisions since the unsigned and signed type planes have collapsed
579// into a single signless type plane.
Reid Spencer319a7302007-01-05 17:20:02 +0000580static std::string getUniqueName(const std::string *Name, const TypeInfo* Ty,
581 bool isGlobal = false) {
582
Reid Spencer52402b02007-01-02 05:45:11 +0000583 // If its not a symbolic name, don't modify it, probably a constant val.
584 if ((*Name)[0] != '%' && (*Name)[0] != '"')
585 return *Name;
Reid Spencer319a7302007-01-05 17:20:02 +0000586
Reid Spencer52402b02007-01-02 05:45:11 +0000587 // If its a numeric reference, just leave it alone.
588 if (isdigit((*Name)[1]))
589 return *Name;
590
591 // Resolve the type
Reid Spencer319a7302007-01-05 17:20:02 +0000592 Ty = Ty->resolve();
593
594 // If its a global name, get its uniquified name, if any
595 GlobalsTypeMap::iterator GI = Globals.find(*Name);
596 if (GI != Globals.end()) {
597 TypePlaneMap::iterator TPI = GI->second.begin();
598 TypePlaneMap::iterator TPE = GI->second.end();
599 for ( ; TPI != TPE ; ++TPI) {
600 if (TPI->first->sameNewTyAs(Ty))
601 return TPI->second;
602 }
603 }
604
605 if (isGlobal) {
606 // We didn't find a global name, but if its supposed to be global then all
607 // we can do is return the name. This is probably a forward reference of a
608 // global value that hasn't been defined yet. Since we have no definition
609 // we don't know its linkage class. Just assume its an external and the name
610 // shouldn't change.
611 return *Name;
612 }
Reid Spencer52402b02007-01-02 05:45:11 +0000613
Reid Spencereff838e2007-01-03 23:45:42 +0000614 // Remove as many levels of pointer nesting that we have.
615 if (Ty->isPointer()) {
616 // Avoid infinite loops in recursive types
Reid Spencer319a7302007-01-05 17:20:02 +0000617 const TypeInfo* Last = 0;
618 while (Ty->isPointer() && !Ty->sameOldTyAs(Last)) {
Reid Spencereff838e2007-01-03 23:45:42 +0000619 Last = Ty;
Reid Spencer319a7302007-01-05 17:20:02 +0000620 Ty = Ty->getElementType()->resolve();
Reid Spencereff838e2007-01-03 23:45:42 +0000621 }
622 }
623
Reid Spencer52402b02007-01-02 05:45:11 +0000624 // Default the result to the current name
625 std::string Result = *Name;
626
Reid Spencereff838e2007-01-03 23:45:42 +0000627 // Now deal with the underlying type
Reid Spencer52402b02007-01-02 05:45:11 +0000628 if (Ty->isInteger()) {
629 // If its an integer type, make the name unique
630 Result = makeUniqueName(Name, Ty->isSigned());
Reid Spencereff838e2007-01-03 23:45:42 +0000631 } else if (Ty->isArray() || Ty->isPacked()) {
632 Ty = Ty->getElementType();
Reid Spencer52402b02007-01-02 05:45:11 +0000633 if (Ty->isInteger())
634 Result = makeUniqueName(Name, Ty->isSigned());
Reid Spencereff838e2007-01-03 23:45:42 +0000635 } else if (Ty->isStruct()) {
636 // Scan the fields and count the signed and unsigned fields
637 int isSigned = 0;
638 for (unsigned i = 0; i < Ty->getNumStructElements(); ++i) {
Reid Spencer319a7302007-01-05 17:20:02 +0000639 const TypeInfo* Tmp = Ty->getElement(i);
Reid Spencereff838e2007-01-03 23:45:42 +0000640 if (Tmp->isInteger())
641 if (Tmp->isSigned())
642 isSigned++;
643 else
644 isSigned--;
645 }
646 if (isSigned != 0)
647 Result = makeUniqueName(Name, isSigned > 0);
Reid Spencer52402b02007-01-02 05:45:11 +0000648 }
649 return Result;
650}
651
Reid Spencer319a7302007-01-05 17:20:02 +0000652static unsigned UniqueNameCounter = 0;
653
654std::string getGlobalName(const std::string* Name, const std::string Linkage,
655 const TypeInfo* Ty, bool isConstant) {
656 // Default to given name
657 std::string Result = *Name;
658 // Look up the name in the Globals Map
659 GlobalsTypeMap::iterator GI = Globals.find(*Name);
660 // Did we see this global name before?
661 if (GI != Globals.end()) {
662 if (Ty->isUnresolvedDeep()) {
663 // The Gval's type is unresolved. Consequently, we can't disambiguate it
664 // by type. We'll just change its name and emit a warning.
665 warning("Cannot disambiguate global value '" + *Name +
666 "' because type '" + Ty->getNewTy() + "'is unresolved.\n");
667 Result = *Name + ".unique";
668 UniqueNameCounter++;
669 Result += llvm::utostr(UniqueNameCounter);
670 return Result;
671 } else {
672 TypePlaneMap::iterator TPI = GI->second.find(Ty);
673 if (TPI != GI->second.end()) {
674 // We found an existing name of the same old type. This isn't allowed
675 // in LLVM 2.0. Consequently, we must alter the name of the global so it
676 // can at least compile. References to the global will yield the first
677 // definition, which is okay. We also must warn about this.
678 Result = *Name + ".unique";
679 UniqueNameCounter++;
680 Result += llvm::utostr(UniqueNameCounter);
681 warning(std::string("Global variable '") + *Name + "' was renamed to '"+
682 Result + "'");
683 } else {
684 // There isn't an existing definition for this name according to the
685 // old types. Now search the TypePlanMap for types with the same new
686 // name.
687 TypePlaneMap::iterator TPI = GI->second.begin();
688 TypePlaneMap::iterator TPE = GI->second.end();
689 for ( ; TPI != TPE; ++TPI) {
690 if (TPI->first->sameNewTyAs(Ty)) {
691 // The new types are the same but the old types are different so
692 // this is a global name collision resulting from type planes
693 // collapsing.
694 if (Linkage == "external" || Linkage == "dllimport" ||
695 Linkage == "extern_weak" || Linkage == "") {
696 // The linkage of this gval is external so we can't reliably
697 // rename it because it could potentially create a linking
698 // problem. However, we can't leave the name conflict in the
699 // output either or it won't assemble with LLVM 2.0. So, all we
700 // can do is rename this one to something unique and emit a
701 // warning about the problem.
702 Result = *Name + ".unique";
703 UniqueNameCounter++;
704 Result += llvm::utostr(UniqueNameCounter);
705 warning("Renaming global value '" + *Name + "' to '" + Result +
706 "' may cause linkage errors.");
707 return Result;
708 } else {
709 // Its linkage is internal and its type is known so we can
710 // disambiguate the name collision successfully based on the type.
711 Result = getUniqueName(Name, Ty);
712 TPI->second = Result;
713 return Result;
714 }
715 }
716 }
717 // We didn't find an entry in the type plane with the same new type and
718 // the old types differ so this is a new type plane for this global
719 // variable. We just fall through to the logic below which inserts
720 // the global.
721 }
722 }
723 }
724
725 // Its a new global name, if it is external we can't change it
726 if (isConstant || Linkage == "external" || Linkage == "dllimport" ||
727 Linkage == "extern_weak" || Linkage == "") {
728 Globals[Result][Ty] = Result;
729 return Result;
730 }
731
732 // Its a new global name, and it is internal, change the name to make it
733 // unique for its type.
734 // Result = getUniqueName(Name, Ty);
735 Globals[*Name][Ty] = Result;
736 return Result;
737}
Reid Spencere7c3c602006-11-30 06:36:44 +0000738%}
739
Reid Spencerf0cf1322006-12-07 04:23:03 +0000740// %file-prefix="UpgradeParser"
Reid Spencere77e35e2006-12-01 20:26:20 +0000741
742%union {
743 std::string* String;
Reid Spencer319a7302007-01-05 17:20:02 +0000744 const TypeInfo* Type;
Reid Spencere77e35e2006-12-01 20:26:20 +0000745 ValueInfo Value;
746 ConstInfo Const;
Reid Spencerf8483652006-12-02 15:16:01 +0000747 ValueList* ValList;
Reid Spencer52402b02007-01-02 05:45:11 +0000748 TypeList* TypeVec;
Reid Spencere77e35e2006-12-01 20:26:20 +0000749}
750
Reid Spencerf2d55322006-12-01 21:52:30 +0000751%token <Type> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
Reid Spencera50d5962006-12-02 04:11:07 +0000752%token <Type> FLOAT DOUBLE LABEL
753%token <String> OPAQUE ESINT64VAL EUINT64VAL SINTVAL UINTVAL FPVAL
Reid Spencerf2d55322006-12-01 21:52:30 +0000754%token <String> NULL_TOK UNDEF ZEROINITIALIZER TRUETOK FALSETOK
Reid Spencere77e35e2006-12-01 20:26:20 +0000755%token <String> TYPE VAR_ID LABELSTR STRINGCONSTANT
756%token <String> IMPLEMENTATION BEGINTOK ENDTOK
Reid Spencer71d2ec92006-12-31 06:02:26 +0000757%token <String> DECLARE GLOBAL CONSTANT SECTION VOLATILE
Reid Spencere77e35e2006-12-01 20:26:20 +0000758%token <String> TO DOTDOTDOT CONST INTERNAL LINKONCE WEAK
759%token <String> DLLIMPORT DLLEXPORT EXTERN_WEAK APPENDING
760%token <String> NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
Reid Spencer78720742006-12-02 20:21:22 +0000761%token <String> ALIGN UNINITIALIZED
Reid Spencere77e35e2006-12-01 20:26:20 +0000762%token <String> DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
763%token <String> CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
764%token <String> X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
765%token <String> DATALAYOUT
Reid Spencer78720742006-12-02 20:21:22 +0000766%token <String> RET BR SWITCH INVOKE EXCEPT UNWIND UNREACHABLE
767%token <String> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM AND OR XOR
Reid Spencere77e35e2006-12-01 20:26:20 +0000768%token <String> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
Reid Spencer229e9362006-12-02 22:14:11 +0000769%token <String> ICMP FCMP EQ NE SLT SGT SLE SGE OEQ ONE OLT OGT OLE OGE
770%token <String> ORD UNO UEQ UNE ULT UGT ULE UGE
Reid Spencere77e35e2006-12-01 20:26:20 +0000771%token <String> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Reid Spencerf7bde222006-12-01 22:26:37 +0000772%token <String> PHI_TOK SELECT SHL SHR ASHR LSHR VAARG
Reid Spencere77e35e2006-12-01 20:26:20 +0000773%token <String> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Reid Spencerfcb5df82006-12-01 22:34:43 +0000774%token <String> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI UITOFP SITOFP
775%token <String> PTRTOINT INTTOPTR BITCAST
Reid Spencere77e35e2006-12-01 20:26:20 +0000776
777%type <String> OptAssign OptLinkage OptCallingConv OptAlign OptCAlign
778%type <String> SectionString OptSection GlobalVarAttributes GlobalVarAttribute
Reid Spencer52402b02007-01-02 05:45:11 +0000779%type <String> ConstExpr DefinitionList
Reid Spencere77e35e2006-12-01 20:26:20 +0000780%type <String> ConstPool TargetDefinition LibrariesDefinition LibList OptName
781%type <String> ArgVal ArgListH ArgList FunctionHeaderH BEGIN FunctionHeader END
Reid Spencer52402b02007-01-02 05:45:11 +0000782%type <String> Function FunctionProto BasicBlock
783%type <String> InstructionList BBTerminatorInst JumpTable Inst
784%type <String> OptTailCall OptVolatile Unwind
785%type <String> SymbolicValueRef OptSideEffect GlobalType
Reid Spencere77e35e2006-12-01 20:26:20 +0000786%type <String> FnDeclareLinkage BasicBlockList BigOrLittle AsmBlock
Reid Spencer78720742006-12-02 20:21:22 +0000787%type <String> Name ConstValueRef ConstVector External
Reid Spencer57f28f92006-12-03 07:10:26 +0000788%type <String> ShiftOps SetCondOps LogicalOps ArithmeticOps CastOps
789%type <String> IPredicates FPredicates
Reid Spencere77e35e2006-12-01 20:26:20 +0000790
Reid Spencerf8483652006-12-02 15:16:01 +0000791%type <ValList> ValueRefList ValueRefListE IndexList
Reid Spencer52402b02007-01-02 05:45:11 +0000792%type <TypeVec> TypeListI ArgTypeListI
Reid Spencere77e35e2006-12-01 20:26:20 +0000793
794%type <Type> IntType SIntType UIntType FPType TypesV Types
795%type <Type> PrimType UpRTypesV UpRTypes
796
Reid Spencerf2d55322006-12-01 21:52:30 +0000797%type <String> IntVal EInt64Val
798%type <Const> ConstVal
Reid Spencere77e35e2006-12-01 20:26:20 +0000799
Reid Spencer52402b02007-01-02 05:45:11 +0000800%type <Value> ValueRef ResolvedVal InstVal PHIList MemoryInst
Reid Spencere7c3c602006-11-30 06:36:44 +0000801
802%start Module
803
804%%
805
806// Handle constant integer size restriction and conversion...
Reid Spencerf2d55322006-12-01 21:52:30 +0000807IntVal : SINTVAL | UINTVAL ;
Reid Spencere77e35e2006-12-01 20:26:20 +0000808EInt64Val : ESINT64VAL | EUINT64VAL;
Reid Spencere7c3c602006-11-30 06:36:44 +0000809
810// Operations that are notably excluded from this list include:
811// RET, BR, & SWITCH because they end basic blocks and are treated specially.
Reid Spencer78720742006-12-02 20:21:22 +0000812ArithmeticOps: ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV
813 | REM | UREM | SREM | FREM;
Reid Spencere7c3c602006-11-30 06:36:44 +0000814LogicalOps : AND | OR | XOR;
815SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Reid Spencer57f28f92006-12-03 07:10:26 +0000816IPredicates : EQ | NE | SLT | SGT | SLE | SGE | ULT | UGT | ULE | UGE;
817FPredicates : OEQ | ONE | OLT | OGT | OLE | OGE | ORD | UNO | UEQ | UNE
818 | ULT | UGT | ULE | UGE | TRUETOK | FALSETOK;
Reid Spencerf7bde222006-12-01 22:26:37 +0000819ShiftOps : SHL | SHR | ASHR | LSHR;
Reid Spencerfcb5df82006-12-01 22:34:43 +0000820CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI |
821 UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
822 ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000823
824// These are some types that allow classification if we only want a particular
825// thing... for example, only a signed, unsigned, or integral type.
826SIntType : LONG | INT | SHORT | SBYTE;
827UIntType : ULONG | UINT | USHORT | UBYTE;
828IntType : SIntType | UIntType;
829FPType : FLOAT | DOUBLE;
830
831// OptAssign - Value producing statements have an optional assignment component
832OptAssign : Name '=' {
Reid Spencere7c3c602006-11-30 06:36:44 +0000833 $$ = $1;
834 }
835 | /*empty*/ {
836 $$ = new std::string("");
837 };
838
839OptLinkage
840 : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT
841 | EXTERN_WEAK
842 | /*empty*/ { $$ = new std::string(""); } ;
843
844OptCallingConv
845 : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK
Reid Spencer16244f42006-12-01 21:10:07 +0000846 | X86_FASTCALLCC_TOK
847 | CC_TOK EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000848 *$1 += *$2;
849 delete $2;
Reid Spencer16244f42006-12-01 21:10:07 +0000850 $$ = $1;
851 }
Reid Spencere7c3c602006-11-30 06:36:44 +0000852 | /*empty*/ { $$ = new std::string(""); } ;
853
854// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
855// a comma before it.
856OptAlign
857 : /*empty*/ { $$ = new std::string(); }
Reid Spencerf2d55322006-12-01 21:52:30 +0000858 | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
Reid Spencerf0cf1322006-12-07 04:23:03 +0000859
Reid Spencere7c3c602006-11-30 06:36:44 +0000860OptCAlign
861 : /*empty*/ { $$ = new std::string(); }
862 | ',' ALIGN EUINT64VAL {
863 $2->insert(0, ", ");
Reid Spencerf2d55322006-12-01 21:52:30 +0000864 *$2 += " " + *$3;
865 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000866 $$ = $2;
867 };
868
869SectionString
870 : SECTION STRINGCONSTANT {
871 *$1 += " " + *$2;
872 delete $2;
873 $$ = $1;
874 };
875
876OptSection : /*empty*/ { $$ = new std::string(); }
877 | SectionString;
878
879GlobalVarAttributes
880 : /* empty */ { $$ = new std::string(); }
881 | ',' GlobalVarAttribute GlobalVarAttributes {
882 $2->insert(0, ", ");
883 if (!$3->empty())
884 *$2 += " " + *$3;
885 delete $3;
886 $$ = $2;
887 };
888
889GlobalVarAttribute
890 : SectionString
891 | ALIGN EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000892 *$1 += " " + *$2;
893 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000894 $$ = $1;
895 };
896
897//===----------------------------------------------------------------------===//
898// Types includes all predefined types... except void, because it can only be
899// used in specific contexts (function returning void for example). To have
900// access to it, a user must explicitly use TypesV.
901//
902
903// TypesV includes all of 'Types', but it also includes the void type.
904TypesV : Types | VOID ;
905UpRTypesV : UpRTypes | VOID ;
906Types : UpRTypes ;
907
908// Derived types are added later...
909//
910PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
Reid Spencere77e35e2006-12-01 20:26:20 +0000911PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
Reid Spencera50d5962006-12-02 04:11:07 +0000912UpRTypes
913 : OPAQUE {
Reid Spencer319a7302007-01-05 17:20:02 +0000914 $$ = TypeInfo::get(*$1, OpaqueTy);
Reid Spencera50d5962006-12-02 04:11:07 +0000915 }
916 | SymbolicValueRef {
Reid Spencer319a7302007-01-05 17:20:02 +0000917 $$ = TypeInfo::get(*$1, UnresolvedTy);
Reid Spencera50d5962006-12-02 04:11:07 +0000918 }
Reid Spencer78720742006-12-02 20:21:22 +0000919 | PrimType {
920 $$ = $1;
921 }
922 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerf2d55322006-12-01 21:52:30 +0000923 $2->insert(0, "\\");
Reid Spencer319a7302007-01-05 17:20:02 +0000924 $$ = TypeInfo::get(*$2, UpRefTy);
Reid Spencere7c3c602006-11-30 06:36:44 +0000925 }
926 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Reid Spencer52402b02007-01-02 05:45:11 +0000927 std::string newTy( $1->getNewTy() + "(");
928 for (unsigned i = 0; i < $3->size(); ++i) {
929 if (i != 0)
930 newTy += ", ";
931 if ((*$3)[i]->isVoid())
932 newTy += "...";
933 else
934 newTy += (*$3)[i]->getNewTy();
935 }
936 newTy += ")";
Reid Spencer319a7302007-01-05 17:20:02 +0000937 $$ = TypeInfo::get(newTy, $1, $3);
Reid Spencere7c3c602006-11-30 06:36:44 +0000938 }
939 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencer319a7302007-01-05 17:20:02 +0000940 uint64_t elems = atoi($2->c_str());
Reid Spencerf2d55322006-12-01 21:52:30 +0000941 $2->insert(0,"[ ");
Reid Spencer52402b02007-01-02 05:45:11 +0000942 *$2 += " x " + $4->getNewTy() + " ]";
Reid Spencer319a7302007-01-05 17:20:02 +0000943 $$ = TypeInfo::get(*$2, ArrayTy, $4, elems);
Reid Spencere7c3c602006-11-30 06:36:44 +0000944 }
945 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
Reid Spencer319a7302007-01-05 17:20:02 +0000946 uint64_t elems = atoi($2->c_str());
Reid Spencerf2d55322006-12-01 21:52:30 +0000947 $2->insert(0,"< ");
Reid Spencer52402b02007-01-02 05:45:11 +0000948 *$2 += " x " + $4->getNewTy() + " >";
Reid Spencer319a7302007-01-05 17:20:02 +0000949 $$ = TypeInfo::get(*$2, PackedTy, $4, elems);
Reid Spencere7c3c602006-11-30 06:36:44 +0000950 }
951 | '{' TypeListI '}' { // Structure type?
Reid Spencer52402b02007-01-02 05:45:11 +0000952 std::string newTy("{");
953 for (unsigned i = 0; i < $2->size(); ++i) {
954 if (i != 0)
955 newTy += ", ";
956 newTy += (*$2)[i]->getNewTy();
957 }
958 newTy += "}";
Reid Spencer319a7302007-01-05 17:20:02 +0000959 $$ = TypeInfo::get(newTy, StructTy, $2);
Reid Spencere7c3c602006-11-30 06:36:44 +0000960 }
961 | '{' '}' { // Empty structure type?
Reid Spencer319a7302007-01-05 17:20:02 +0000962 $$ = TypeInfo::get("{}", StructTy, new TypeList());
Reid Spencere7c3c602006-11-30 06:36:44 +0000963 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000964 | '<' '{' TypeListI '}' '>' { // Packed Structure type?
Reid Spencer52402b02007-01-02 05:45:11 +0000965 std::string newTy("<{");
966 for (unsigned i = 0; i < $3->size(); ++i) {
967 if (i != 0)
968 newTy += ", ";
969 newTy += (*$3)[i]->getNewTy();
970 }
971 newTy += "}>";
Reid Spencer319a7302007-01-05 17:20:02 +0000972 $$ = TypeInfo::get(newTy, PackedStructTy, $3);
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000973 }
974 | '<' '{' '}' '>' { // Empty packed structure type?
Reid Spencer319a7302007-01-05 17:20:02 +0000975 $$ = TypeInfo::get("<{}>", PackedStructTy, new TypeList());
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000976 }
Reid Spencere7c3c602006-11-30 06:36:44 +0000977 | UpRTypes '*' { // Pointer type?
Reid Spencer52402b02007-01-02 05:45:11 +0000978 $$ = $1->getPointerType();
Reid Spencere7c3c602006-11-30 06:36:44 +0000979 };
980
981// TypeList - Used for struct declarations and as a basis for function type
982// declaration type lists
983//
Reid Spencere77e35e2006-12-01 20:26:20 +0000984TypeListI
985 : UpRTypes {
Reid Spencer52402b02007-01-02 05:45:11 +0000986 $$ = new TypeList();
987 $$->push_back($1);
Reid Spencere77e35e2006-12-01 20:26:20 +0000988 }
989 | TypeListI ',' UpRTypes {
Reid Spencere7c3c602006-11-30 06:36:44 +0000990 $$ = $1;
Reid Spencer52402b02007-01-02 05:45:11 +0000991 $$->push_back($3);
Reid Spencere7c3c602006-11-30 06:36:44 +0000992 };
993
994// ArgTypeList - List of types for a function type declaration...
Reid Spencere77e35e2006-12-01 20:26:20 +0000995ArgTypeListI
996 : TypeListI
Reid Spencere7c3c602006-11-30 06:36:44 +0000997 | TypeListI ',' DOTDOTDOT {
Reid Spencere7c3c602006-11-30 06:36:44 +0000998 $$ = $1;
Reid Spencer319a7302007-01-05 17:20:02 +0000999 $$->push_back(TypeInfo::get("void",VoidTy));
Reid Spencer52402b02007-01-02 05:45:11 +00001000 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001001 }
1002 | DOTDOTDOT {
Reid Spencer52402b02007-01-02 05:45:11 +00001003 $$ = new TypeList();
Reid Spencer319a7302007-01-05 17:20:02 +00001004 $$->push_back(TypeInfo::get("void",VoidTy));
Reid Spencer52402b02007-01-02 05:45:11 +00001005 delete $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001006 }
1007 | /*empty*/ {
Reid Spencer52402b02007-01-02 05:45:11 +00001008 $$ = new TypeList();
Reid Spencere7c3c602006-11-30 06:36:44 +00001009 };
1010
1011// ConstVal - The various declarations that go into the constant pool. This
1012// production is used ONLY to represent constants that show up AFTER a 'const',
1013// 'constant' or 'global' token at global scope. Constants that can be inlined
1014// into other expressions (such as integers and constexprs) are handled by the
1015// ResolvedVal, ValueRef and ConstValueRef productions.
1016//
1017ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencere77e35e2006-12-01 20:26:20 +00001018 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001019 $$.cnst = new std::string($1->getNewTy());
Reid Spencere77e35e2006-12-01 20:26:20 +00001020 *$$.cnst += " [ " + *$3 + " ]";
Reid Spencere7c3c602006-11-30 06:36:44 +00001021 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001022 }
1023 | Types '[' ']' {
Reid Spencere77e35e2006-12-01 20:26:20 +00001024 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001025 $$.cnst = new std::string($1->getNewTy());
Reid Spencere77e35e2006-12-01 20:26:20 +00001026 *$$.cnst += "[ ]";
Reid Spencere7c3c602006-11-30 06:36:44 +00001027 }
1028 | Types 'c' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +00001029 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001030 $$.cnst = new std::string($1->getNewTy());
Reid Spencere77e35e2006-12-01 20:26:20 +00001031 *$$.cnst += " c" + *$3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001032 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001033 }
1034 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencere77e35e2006-12-01 20:26:20 +00001035 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001036 $$.cnst = new std::string($1->getNewTy());
Reid Spencere77e35e2006-12-01 20:26:20 +00001037 *$$.cnst += " < " + *$3 + " >";
Reid Spencere7c3c602006-11-30 06:36:44 +00001038 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001039 }
1040 | Types '{' ConstVector '}' {
Reid Spencere77e35e2006-12-01 20:26:20 +00001041 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001042 $$.cnst = new std::string($1->getNewTy());
Reid Spencere77e35e2006-12-01 20:26:20 +00001043 *$$.cnst += " { " + *$3 + " }";
Reid Spencere7c3c602006-11-30 06:36:44 +00001044 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001045 }
1046 | Types '{' '}' {
Reid Spencere77e35e2006-12-01 20:26:20 +00001047 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001048 $$.cnst = new std::string($1->getNewTy());
Reid Spencer0b7e5072006-12-01 22:42:01 +00001049 *$$.cnst += " {}";
Reid Spencere7c3c602006-11-30 06:36:44 +00001050 }
1051 | Types NULL_TOK {
Reid Spencere77e35e2006-12-01 20:26:20 +00001052 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001053 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +00001054 *$$.cnst += " " + *$2;
1055 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001056 }
1057 | Types UNDEF {
Reid Spencere77e35e2006-12-01 20:26:20 +00001058 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001059 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +00001060 *$$.cnst += " " + *$2;
1061 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001062 }
1063 | Types SymbolicValueRef {
Reid Spencer319a7302007-01-05 17:20:02 +00001064 std::string Name = getUniqueName($2, $1->resolve(), true);
Reid Spencere77e35e2006-12-01 20:26:20 +00001065 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001066 $$.cnst = new std::string($1->getNewTy());
1067 *$$.cnst += " " + Name;
Reid Spencere7c3c602006-11-30 06:36:44 +00001068 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001069 }
1070 | Types ConstExpr {
Reid Spencere77e35e2006-12-01 20:26:20 +00001071 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001072 $$.cnst = new std::string($1->getNewTy());
Reid Spencere77e35e2006-12-01 20:26:20 +00001073 *$$.cnst += " " + *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001074 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001075 }
1076 | Types ZEROINITIALIZER {
Reid Spencere77e35e2006-12-01 20:26:20 +00001077 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001078 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +00001079 *$$.cnst += " " + *$2;
1080 delete $2;
Reid Spencere77e35e2006-12-01 20:26:20 +00001081 }
1082 | SIntType EInt64Val { // integral constants
1083 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001084 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +00001085 *$$.cnst += " " + *$2;
1086 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001087 }
Reid Spencer7356ae42007-01-02 06:34:08 +00001088 | UIntType EInt64Val { // integral constants
Reid Spencere77e35e2006-12-01 20:26:20 +00001089 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001090 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +00001091 *$$.cnst += " " + *$2;
1092 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001093 }
1094 | BOOL TRUETOK { // Boolean constants
Reid Spencere77e35e2006-12-01 20:26:20 +00001095 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001096 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +00001097 *$$.cnst += " " + *$2;
1098 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001099 }
1100 | BOOL FALSETOK { // Boolean constants
Reid Spencere77e35e2006-12-01 20:26:20 +00001101 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001102 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +00001103 *$$.cnst += " " + *$2;
1104 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001105 }
1106 | FPType FPVAL { // Float & Double constants
Reid Spencere77e35e2006-12-01 20:26:20 +00001107 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001108 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +00001109 *$$.cnst += " " + *$2;
1110 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001111 };
1112
1113
Reid Spencerfcb5df82006-12-01 22:34:43 +00001114ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer280d8012006-12-01 23:40:53 +00001115 std::string source = *$3.cnst;
Reid Spencer319a7302007-01-05 17:20:02 +00001116 const TypeInfo* SrcTy = $3.type->resolve();
1117 const TypeInfo* DstTy = $5->resolve();
Reid Spencer280d8012006-12-01 23:40:53 +00001118 if (*$1 == "cast") {
Reid Spencera50d5962006-12-02 04:11:07 +00001119 // Call getCastUpgrade to upgrade the old cast
Reid Spencer319a7302007-01-05 17:20:02 +00001120 $$ = new std::string(getCastUpgrade(source, SrcTy, DstTy, true));
Reid Spencera50d5962006-12-02 04:11:07 +00001121 } else {
1122 // Nothing to upgrade, just create the cast constant expr
1123 $$ = new std::string(*$1);
Reid Spencer52402b02007-01-02 05:45:11 +00001124 *$$ += "( " + source + " to " + $5->getNewTy() + ")";
Reid Spencer280d8012006-12-01 23:40:53 +00001125 }
Reid Spencereff838e2007-01-03 23:45:42 +00001126 delete $1; $3.destroy(); delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001127 }
1128 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencerf8483652006-12-02 15:16:01 +00001129 *$1 += "(" + *$3.cnst;
1130 for (unsigned i = 0; i < $4->size(); ++i) {
1131 ValueInfo& VI = (*$4)[i];
1132 *$1 += ", " + *VI.val;
1133 VI.destroy();
1134 }
1135 *$1 += ")";
Reid Spencere77e35e2006-12-01 20:26:20 +00001136 $$ = $1;
1137 $3.destroy();
1138 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001139 }
1140 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +00001141 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
1142 $3.destroy(); $5.destroy(); $7.destroy();
1143 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001144 }
1145 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer78720742006-12-02 20:21:22 +00001146 const char* op = getDivRemOpcode(*$1, $3.type);
1147 $$ = new std::string(op);
1148 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
1149 delete $1; $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001150 }
1151 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +00001152 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
1153 $3.destroy(); $5.destroy();
1154 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001155 }
1156 | SetCondOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer229e9362006-12-02 22:14:11 +00001157 *$1 = getCompareOp(*$1, $3.type);
Reid Spencere77e35e2006-12-01 20:26:20 +00001158 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
1159 $3.destroy(); $5.destroy();
1160 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001161 }
Reid Spencer57f28f92006-12-03 07:10:26 +00001162 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1163 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
1164 delete $2; $4.destroy(); $6.destroy();
1165 $$ = $1;
1166 }
1167 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencer229e9362006-12-02 22:14:11 +00001168 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
1169 delete $2; $4.destroy(); $6.destroy();
1170 $$ = $1;
1171 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001172 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencerf7bde222006-12-01 22:26:37 +00001173 const char* shiftop = $1->c_str();
1174 if (*$1 == "shr")
Reid Spencer52402b02007-01-02 05:45:11 +00001175 shiftop = ($3.type->isUnsigned()) ? "lshr" : "ashr";
Reid Spencerf7bde222006-12-01 22:26:37 +00001176 $$ = new std::string(shiftop);
1177 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
1178 delete $1; $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001179 }
1180 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +00001181 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
1182 $3.destroy(); $5.destroy();
1183 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001184 }
1185 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +00001186 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
1187 $3.destroy(); $5.destroy(); $7.destroy();
1188 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001189 }
1190 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +00001191 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
1192 $3.destroy(); $5.destroy(); $7.destroy();
1193 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001194 };
1195
1196
1197// ConstVector - A list of comma separated constants.
Reid Spencere77e35e2006-12-01 20:26:20 +00001198
1199ConstVector
1200 : ConstVector ',' ConstVal {
1201 *$1 += ", " + *$3.cnst;
1202 $3.destroy();
1203 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001204 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001205 | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
1206 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001207
1208
1209// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Reid Spencere77e35e2006-12-01 20:26:20 +00001210GlobalType : GLOBAL | CONSTANT ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001211
1212
1213//===----------------------------------------------------------------------===//
1214// Rules to match Modules
1215//===----------------------------------------------------------------------===//
1216
1217// Module rule: Capture the result of parsing the whole file into a result
1218// variable...
1219//
1220Module : DefinitionList {
1221};
1222
1223// DefinitionList - Top level definitions
1224//
1225DefinitionList : DefinitionList Function {
1226 $$ = 0;
1227 }
1228 | DefinitionList FunctionProto {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001229 *O << *$2 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001230 delete $2;
1231 $$ = 0;
1232 }
1233 | DefinitionList MODULE ASM_TOK AsmBlock {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001234 *O << "module asm " << ' ' << *$4 << '\n';
Reid Spencerd154b572006-12-01 20:36:40 +00001235 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001236 }
1237 | DefinitionList IMPLEMENTATION {
1238 *O << "implementation\n";
Reid Spencerd154b572006-12-01 20:36:40 +00001239 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001240 }
Reid Spencera50d5962006-12-02 04:11:07 +00001241 | ConstPool { $$ = 0; }
Reid Spencere7c3c602006-11-30 06:36:44 +00001242
Reid Spencer78720742006-12-02 20:21:22 +00001243External : EXTERNAL | UNINITIALIZED { $$ = $1; *$$ = "external"; }
1244
Reid Spencere7c3c602006-11-30 06:36:44 +00001245// ConstPool - Constants with optional names assigned to them.
1246ConstPool : ConstPool OptAssign TYPE TypesV {
Reid Spencer319a7302007-01-05 17:20:02 +00001247 EnumeratedTypes.push_back($4);
Reid Spencera50d5962006-12-02 04:11:07 +00001248 if (!$2->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001249 NamedTypes[*$2] = $4;
Reid Spencera50d5962006-12-02 04:11:07 +00001250 *O << *$2 << " = ";
1251 }
Reid Spencer52402b02007-01-02 05:45:11 +00001252 *O << "type " << $4->getNewTy() << '\n';
1253 delete $2; delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001254 $$ = 0;
1255 }
1256 | ConstPool FunctionProto { // Function prototypes can be in const pool
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001257 *O << *$2 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001258 delete $2;
1259 $$ = 0;
1260 }
1261 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001262 *O << *$2 << ' ' << *$3 << ' ' << *$4 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001263 delete $2; delete $3; delete $4;
1264 $$ = 0;
1265 }
1266 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +00001267 if (!$2->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001268 std::string Name = getGlobalName($2,*$3, $5.type->getPointerType(),
1269 *$4 == "constant");
Reid Spencer52402b02007-01-02 05:45:11 +00001270 *O << Name << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +00001271 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001272 *O << *$3 << ' ' << *$4 << ' ' << *$5.cnst << ' ' << *$6 << '\n';
Reid Spencer52402b02007-01-02 05:45:11 +00001273 delete $2; delete $3; delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001274 $$ = 0;
1275 }
Reid Spencer78720742006-12-02 20:21:22 +00001276 | ConstPool OptAssign External GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +00001277 if (!$2->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001278 std::string Name = getGlobalName($2,*$3,$5->getPointerType(),
1279 *$4 == "constant");
Reid Spencer52402b02007-01-02 05:45:11 +00001280 *O << Name << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +00001281 }
Reid Spencer52402b02007-01-02 05:45:11 +00001282 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
1283 delete $2; delete $3; delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001284 $$ = 0;
1285 }
Reid Spencer319a7302007-01-05 17:20:02 +00001286 | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +00001287 if (!$2->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001288 std::string Name = getGlobalName($2,*$3,$5->getPointerType(),
1289 *$4 == "constant");
Reid Spencer52402b02007-01-02 05:45:11 +00001290 *O << Name << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +00001291 }
Reid Spencer52402b02007-01-02 05:45:11 +00001292 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
1293 delete $2; delete $3; delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001294 $$ = 0;
1295 }
1296 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +00001297 if (!$2->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001298 std::string Name = getGlobalName($2,*$3,$5->getPointerType(),
1299 *$4 == "constant");
Reid Spencer52402b02007-01-02 05:45:11 +00001300 *O << Name << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +00001301 }
Reid Spencer52402b02007-01-02 05:45:11 +00001302 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
1303 delete $2; delete $3; delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001304 $$ = 0;
1305 }
1306 | ConstPool TARGET TargetDefinition {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001307 *O << *$2 << ' ' << *$3 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001308 delete $2; delete $3;
1309 $$ = 0;
1310 }
1311 | ConstPool DEPLIBS '=' LibrariesDefinition {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001312 *O << *$2 << " = " << *$4 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001313 delete $2; delete $4;
1314 $$ = 0;
1315 }
1316 | /* empty: end of list */ {
1317 $$ = 0;
1318 };
1319
1320
1321AsmBlock : STRINGCONSTANT ;
1322
1323BigOrLittle : BIG | LITTLE
1324
1325TargetDefinition
1326 : ENDIAN '=' BigOrLittle {
Reid Spencere77e35e2006-12-01 20:26:20 +00001327 *$1 += " = " + *$3;
1328 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001329 $$ = $1;
1330 }
1331 | POINTERSIZE '=' EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +00001332 *$1 += " = " + *$3;
1333 if (*$3 == "64")
Reid Spencere77e35e2006-12-01 20:26:20 +00001334 SizeOfPointer = 64;
Reid Spencerf2d55322006-12-01 21:52:30 +00001335 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001336 $$ = $1;
1337 }
1338 | TRIPLE '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +00001339 *$1 += " = " + *$3;
1340 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001341 $$ = $1;
1342 }
1343 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +00001344 *$1 += " = " + *$3;
1345 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001346 $$ = $1;
1347 };
1348
1349LibrariesDefinition
1350 : '[' LibList ']' {
1351 $2->insert(0, "[ ");
1352 *$2 += " ]";
1353 $$ = $2;
1354 };
1355
1356LibList
1357 : LibList ',' STRINGCONSTANT {
1358 *$1 += ", " + *$3;
1359 delete $3;
1360 $$ = $1;
1361 }
1362 | STRINGCONSTANT
1363 | /* empty: end of list */ {
1364 $$ = new std::string();
1365 };
1366
1367//===----------------------------------------------------------------------===//
1368// Rules to match Function Headers
1369//===----------------------------------------------------------------------===//
1370
1371Name : VAR_ID | STRINGCONSTANT;
1372OptName : Name | /*empty*/ { $$ = new std::string(); };
1373
1374ArgVal : Types OptName {
Reid Spencer52402b02007-01-02 05:45:11 +00001375 $$ = new std::string($1->getNewTy());
1376 if (!$2->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001377 std::string Name = getUniqueName($2, $1->resolve());
Reid Spencer52402b02007-01-02 05:45:11 +00001378 *$$ += " " + Name;
1379 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001380 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001381};
1382
1383ArgListH : ArgListH ',' ArgVal {
1384 *$1 += ", " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001385 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001386 }
1387 | ArgVal {
1388 $$ = $1;
1389 };
1390
1391ArgList : ArgListH {
1392 $$ = $1;
1393 }
1394 | ArgListH ',' DOTDOTDOT {
1395 *$1 += ", ...";
1396 $$ = $1;
Reid Spencere77e35e2006-12-01 20:26:20 +00001397 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001398 }
1399 | DOTDOTDOT {
1400 $$ = $1;
1401 }
Reid Spencerd154b572006-12-01 20:36:40 +00001402 | /* empty */ { $$ = new std::string(); };
Reid Spencere7c3c602006-11-30 06:36:44 +00001403
Reid Spencer71d2ec92006-12-31 06:02:26 +00001404FunctionHeaderH
1405 : OptCallingConv TypesV Name '(' ArgList ')' OptSection OptAlign {
Reid Spencere7c3c602006-11-30 06:36:44 +00001406 if (!$1->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +00001407 *$1 += " ";
Reid Spencere7c3c602006-11-30 06:36:44 +00001408 }
Reid Spencer52402b02007-01-02 05:45:11 +00001409 *$1 += $2->getNewTy() + " " + *$3 + "(" + *$5 + ")";
Reid Spencere7c3c602006-11-30 06:36:44 +00001410 if (!$7->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +00001411 *$1 += " " + *$7;
Reid Spencere7c3c602006-11-30 06:36:44 +00001412 }
1413 if (!$8->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +00001414 *$1 += " " + *$8;
Reid Spencere7c3c602006-11-30 06:36:44 +00001415 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001416 delete $3;
1417 delete $5;
1418 delete $7;
1419 delete $8;
1420 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001421 };
1422
Reid Spencer78720742006-12-02 20:21:22 +00001423BEGIN : BEGINTOK { $$ = new std::string("{"); delete $1; }
1424 | '{' { $$ = new std::string ("{"); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001425
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001426FunctionHeader
1427 : OptLinkage FunctionHeaderH BEGIN {
1428 *O << "define ";
1429 if (!$1->empty()) {
1430 *O << *$1 << ' ';
1431 }
1432 *O << *$2 << ' ' << *$3 << '\n';
1433 delete $1; delete $2; delete $3;
1434 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001435 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001436 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001437
Reid Spencer78720742006-12-02 20:21:22 +00001438END : ENDTOK { $$ = new std::string("}"); delete $1; }
Reid Spencere7c3c602006-11-30 06:36:44 +00001439 | '}' { $$ = new std::string("}"); };
1440
1441Function : FunctionHeader BasicBlockList END {
1442 if ($2)
1443 *O << *$2;
Reid Spencer71d2ec92006-12-31 06:02:26 +00001444 *O << *$3 << "\n\n";
Reid Spencer52402b02007-01-02 05:45:11 +00001445 delete $1; delete $2; delete $3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001446 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001447};
1448
Reid Spencere77e35e2006-12-01 20:26:20 +00001449FnDeclareLinkage
1450 : /*default*/ { $$ = new std::string(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001451 | DLLIMPORT
1452 | EXTERN_WEAK
1453 ;
1454
1455FunctionProto
1456 : DECLARE FnDeclareLinkage FunctionHeaderH {
Reid Spencere77e35e2006-12-01 20:26:20 +00001457 if (!$2->empty())
1458 *$1 += " " + *$2;
1459 *$1 += " " + *$3;
1460 delete $2;
1461 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001462 $$ = $1;
1463 };
1464
1465//===----------------------------------------------------------------------===//
1466// Rules to match Basic Blocks
1467//===----------------------------------------------------------------------===//
1468
Reid Spencerd154b572006-12-01 20:36:40 +00001469OptSideEffect : /* empty */ { $$ = new std::string(); }
1470 | SIDEEFFECT;
Reid Spencere7c3c602006-11-30 06:36:44 +00001471
Reid Spencere77e35e2006-12-01 20:26:20 +00001472ConstValueRef
Reid Spencerf2d55322006-12-01 21:52:30 +00001473 : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK | NULL_TOK | UNDEF
1474 | ZEROINITIALIZER
Reid Spencere7c3c602006-11-30 06:36:44 +00001475 | '<' ConstVector '>' {
1476 $2->insert(0, "<");
1477 *$2 += ">";
1478 $$ = $2;
1479 }
1480 | ConstExpr
1481 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
1482 if (!$2->empty()) {
1483 *$1 += " " + *$2;
1484 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001485 *$1 += " " + *$3 + ", " + *$5;
1486 delete $2; delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001487 $$ = $1;
1488 };
1489
Reid Spencerf2d55322006-12-01 21:52:30 +00001490SymbolicValueRef : IntVal | Name ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001491
1492// ValueRef - A reference to a definition... either constant or symbolic
Reid Spencerf459d392006-12-02 16:19:52 +00001493ValueRef
1494 : SymbolicValueRef {
1495 $$.val = $1;
1496 $$.constant = false;
Reid Spencer319a7302007-01-05 17:20:02 +00001497 $$.type = 0;
Reid Spencerf459d392006-12-02 16:19:52 +00001498 }
1499 | ConstValueRef {
1500 $$.val = $1;
1501 $$.constant = true;
Reid Spencer319a7302007-01-05 17:20:02 +00001502 $$.type = 0;
Reid Spencerf459d392006-12-02 16:19:52 +00001503 }
1504 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001505
1506// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1507// type immediately preceeds the value reference, and allows complex constant
1508// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1509ResolvedVal : Types ValueRef {
Reid Spencer319a7302007-01-05 17:20:02 +00001510 $1 = $1->resolve();
Reid Spencer52402b02007-01-02 05:45:11 +00001511 std::string Name = getUniqueName($2.val, $1);
Reid Spencerf459d392006-12-02 16:19:52 +00001512 $$ = $2;
Reid Spencer52402b02007-01-02 05:45:11 +00001513 delete $$.val;
Reid Spencer52402b02007-01-02 05:45:11 +00001514 $$.val = new std::string($1->getNewTy() + " " + Name);
Reid Spencere77e35e2006-12-01 20:26:20 +00001515 $$.type = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001516 };
1517
1518BasicBlockList : BasicBlockList BasicBlock {
Reid Spencerf2d55322006-12-01 21:52:30 +00001519 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001520 }
1521 | BasicBlock { // Do not allow functions with 0 basic blocks
Reid Spencerf2d55322006-12-01 21:52:30 +00001522 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001523 };
1524
1525
1526// Basic blocks are terminated by branching instructions:
1527// br, br/cc, switch, ret
1528//
Reid Spencer16244f42006-12-01 21:10:07 +00001529BasicBlock : InstructionList BBTerminatorInst {
Reid Spencerf2d55322006-12-01 21:52:30 +00001530 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001531 };
1532
1533InstructionList : InstructionList Inst {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001534 *O << " " << *$2 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001535 delete $2;
1536 $$ = 0;
1537 }
1538 | /* empty */ {
1539 $$ = 0;
1540 }
1541 | LABELSTR {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001542 *O << *$1 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001543 delete $1;
1544 $$ = 0;
1545 };
1546
Reid Spencer78720742006-12-02 20:21:22 +00001547Unwind : UNWIND | EXCEPT { $$ = $1; *$$ = "unwind"; }
1548
Reid Spencere7c3c602006-11-30 06:36:44 +00001549BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001550 *O << " " << *$1 << ' ' << *$2.val << '\n';
Reid Spencere77e35e2006-12-01 20:26:20 +00001551 delete $1; $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001552 $$ = 0;
1553 }
1554 | RET VOID { // Return with no result...
Reid Spencer52402b02007-01-02 05:45:11 +00001555 *O << " " << *$1 << ' ' << $2->getNewTy() << '\n';
Reid Spencer319a7302007-01-05 17:20:02 +00001556 delete $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001557 $$ = 0;
1558 }
1559 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer52402b02007-01-02 05:45:11 +00001560 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << *$3.val << '\n';
Reid Spencer319a7302007-01-05 17:20:02 +00001561 delete $1; $3.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001562 $$ = 0;
1563 } // Conditional Branch...
1564 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001565 std::string Name = getUniqueName($3.val, $2);
1566 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", "
1567 << $5->getNewTy() << ' ' << *$6.val << ", " << $8->getNewTy() << ' '
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001568 << *$9.val << '\n';
Reid Spencer319a7302007-01-05 17:20:02 +00001569 delete $1; $3.destroy(); $6.destroy(); $9.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001570 $$ = 0;
1571 }
1572 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencer52402b02007-01-02 05:45:11 +00001573 std::string Name = getUniqueName($3.val, $2);
1574 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", "
1575 << $5->getNewTy() << ' ' << *$6.val << " [" << *$8 << " ]\n";
Reid Spencer319a7302007-01-05 17:20:02 +00001576 delete $1; $3.destroy(); $6.destroy();
Reid Spencerf459d392006-12-02 16:19:52 +00001577 delete $8;
Reid Spencere7c3c602006-11-30 06:36:44 +00001578 $$ = 0;
1579 }
1580 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencer52402b02007-01-02 05:45:11 +00001581 std::string Name = getUniqueName($3.val, $2);
1582 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", "
1583 << $5->getNewTy() << ' ' << *$6.val << "[]\n";
Reid Spencer319a7302007-01-05 17:20:02 +00001584 delete $1; $3.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001585 $$ = 0;
1586 }
Reid Spencer16244f42006-12-01 21:10:07 +00001587 | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
Reid Spencer78720742006-12-02 20:21:22 +00001588 TO LABEL ValueRef Unwind LABEL ValueRef {
Reid Spencer319a7302007-01-05 17:20:02 +00001589 const TypeInfo* ResTy = getFunctionReturnType($4);
Reid Spencer16244f42006-12-01 21:10:07 +00001590 *O << " ";
Reid Spencer52402b02007-01-02 05:45:11 +00001591 if (!$1->empty()) {
1592 std::string Name = getUniqueName($1, ResTy);
1593 *O << Name << " = ";
1594 }
1595 *O << *$2 << ' ' << *$3 << ' ' << $4->getNewTy() << ' ' << *$5.val << " (";
Reid Spencerf8483652006-12-02 15:16:01 +00001596 for (unsigned i = 0; i < $7->size(); ++i) {
1597 ValueInfo& VI = (*$7)[i];
1598 *O << *VI.val;
1599 if (i+1 < $7->size())
1600 *O << ", ";
1601 VI.destroy();
1602 }
Reid Spencer52402b02007-01-02 05:45:11 +00001603 *O << ") " << *$9 << ' ' << $10->getNewTy() << ' ' << *$11.val << ' '
1604 << *$12 << ' ' << $13->getNewTy() << ' ' << *$14.val << '\n';
Reid Spencer319a7302007-01-05 17:20:02 +00001605 delete $1; delete $2; delete $3; $5.destroy(); delete $7;
1606 delete $9; $11.destroy(); delete $12; $14.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001607 $$ = 0;
1608 }
Reid Spencer78720742006-12-02 20:21:22 +00001609 | Unwind {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001610 *O << " " << *$1 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001611 delete $1;
1612 $$ = 0;
1613 }
1614 | UNREACHABLE {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001615 *O << " " << *$1 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001616 delete $1;
1617 $$ = 0;
1618 };
1619
1620JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001621 *$1 += " " + $2->getNewTy() + " " + *$3 + ", " + $5->getNewTy() + " " +
1622 *$6.val;
Reid Spencer319a7302007-01-05 17:20:02 +00001623 delete $3; $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001624 $$ = $1;
1625 }
1626 | IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001627 $2->insert(0, $1->getNewTy() + " " );
1628 *$2 += ", " + $4->getNewTy() + " " + *$5.val;
Reid Spencer319a7302007-01-05 17:20:02 +00001629 $5.destroy();
Reid Spencere77e35e2006-12-01 20:26:20 +00001630 $$ = $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001631 };
1632
1633Inst
1634 : OptAssign InstVal {
Reid Spencerf5626a32007-01-01 01:20:41 +00001635 if (!$1->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001636 // Get a unique name for this value, based on its type.
1637 std::string Name = getUniqueName($1, $2.type);
1638 *$1 = Name + " = ";
1639 if (deleteUselessCastFlag && *deleteUselessCastName == Name) {
1640 // don't actually delete it, just comment it out
1641 $1->insert(0, "; USELSS BITCAST: ");
Reid Spencerf5626a32007-01-01 01:20:41 +00001642 delete deleteUselessCastName;
Reid Spencerf5626a32007-01-01 01:20:41 +00001643 }
1644 }
Reid Spencer52402b02007-01-02 05:45:11 +00001645 *$1 += *$2.val;
1646 $2.destroy();
Reid Spencerf5626a32007-01-01 01:20:41 +00001647 deleteUselessCastFlag = false;
Reid Spencere7c3c602006-11-30 06:36:44 +00001648 $$ = $1;
1649 };
1650
1651PHIList
1652 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer52402b02007-01-02 05:45:11 +00001653 std::string Name = getUniqueName($3.val, $1);
1654 Name.insert(0, $1->getNewTy() + "[");
1655 Name += "," + *$5.val + "]";
1656 $$.val = new std::string(Name);
1657 $$.type = $1;
1658 $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001659 }
1660 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
Reid Spencer52402b02007-01-02 05:45:11 +00001661 std::string Name = getUniqueName($4.val, $1.type);
1662 *$1.val += ", [" + Name + "," + *$6.val + "]";
Reid Spencerf459d392006-12-02 16:19:52 +00001663 $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001664 $$ = $1;
1665 };
1666
1667
1668ValueRefList
Reid Spencer52402b02007-01-02 05:45:11 +00001669 : ResolvedVal {
Reid Spencerf8483652006-12-02 15:16:01 +00001670 $$ = new ValueList();
1671 $$->push_back($1);
1672 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001673 | ValueRefList ',' ResolvedVal {
Reid Spencere7c3c602006-11-30 06:36:44 +00001674 $$ = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001675 $$->push_back($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00001676 };
1677
1678// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1679ValueRefListE
Reid Spencerf8483652006-12-02 15:16:01 +00001680 : ValueRefList { $$ = $1; }
1681 | /*empty*/ { $$ = new ValueList(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001682 ;
1683
1684OptTailCall
1685 : TAIL CALL {
1686 *$1 += " " + *$2;
1687 delete $2;
1688 $$ = $1;
1689 }
1690 | CALL
1691 ;
1692
1693InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer78720742006-12-02 20:21:22 +00001694 const char* op = getDivRemOpcode(*$1, $2);
Reid Spencer52402b02007-01-02 05:45:11 +00001695 std::string Name1 = getUniqueName($3.val, $2);
1696 std::string Name2 = getUniqueName($5.val, $2);
1697 $$.val = new std::string(op);
1698 *$$.val += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
1699 $$.type = $2;
1700 delete $1; $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001701 }
1702 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001703 std::string Name1 = getUniqueName($3.val, $2);
1704 std::string Name2 = getUniqueName($5.val, $2);
1705 *$1 += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
1706 $$.val = $1;
1707 $$.type = $2;
1708 $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001709 }
1710 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001711 std::string Name1 = getUniqueName($3.val, $2);
1712 std::string Name2 = getUniqueName($5.val, $2);
Reid Spencer229e9362006-12-02 22:14:11 +00001713 *$1 = getCompareOp(*$1, $2);
Reid Spencer52402b02007-01-02 05:45:11 +00001714 *$1 += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
1715 $$.val = $1;
Reid Spencer319a7302007-01-05 17:20:02 +00001716 $$.type = TypeInfo::get("bool",BoolTy);
Reid Spencer52402b02007-01-02 05:45:11 +00001717 $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001718 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001719 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001720 std::string Name1 = getUniqueName($4.val, $3);
1721 std::string Name2 = getUniqueName($6.val, $3);
1722 *$1 += " " + *$2 + " " + $3->getNewTy() + " " + Name1 + "," + Name2;
1723 $$.val = $1;
Reid Spencer319a7302007-01-05 17:20:02 +00001724 $$.type = TypeInfo::get("bool",BoolTy);
Reid Spencer57f28f92006-12-03 07:10:26 +00001725 delete $2; $4.destroy(); $6.destroy();
Reid Spencer57f28f92006-12-03 07:10:26 +00001726 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001727 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001728 std::string Name1 = getUniqueName($4.val, $3);
1729 std::string Name2 = getUniqueName($6.val, $3);
1730 *$1 += " " + *$2 + " " + $3->getNewTy() + " " + Name1 + "," + Name2;
1731 $$.val = $1;
Reid Spencer319a7302007-01-05 17:20:02 +00001732 $$.type = TypeInfo::get("bool",BoolTy);
Reid Spencer229e9362006-12-02 22:14:11 +00001733 delete $2; $4.destroy(); $6.destroy();
Reid Spencer229e9362006-12-02 22:14:11 +00001734 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001735 | NOT ResolvedVal {
Reid Spencer52402b02007-01-02 05:45:11 +00001736 $$ = $2;
1737 $$.val->insert(0, *$1 + " ");
1738 delete $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001739 }
1740 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencerf7bde222006-12-01 22:26:37 +00001741 const char* shiftop = $1->c_str();
1742 if (*$1 == "shr")
Reid Spencer52402b02007-01-02 05:45:11 +00001743 shiftop = ($2.type->isUnsigned()) ? "lshr" : "ashr";
1744 $$.val = new std::string(shiftop);
1745 *$$.val += " " + *$2.val + ", " + *$4.val;
1746 $$.type = $2.type;
1747 delete $1; delete $2.val; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001748 }
Reid Spencerfcb5df82006-12-01 22:34:43 +00001749 | CastOps ResolvedVal TO Types {
Reid Spencer280d8012006-12-01 23:40:53 +00001750 std::string source = *$2.val;
Reid Spencer319a7302007-01-05 17:20:02 +00001751 const TypeInfo* SrcTy = $2.type->resolve();
1752 const TypeInfo* DstTy = $4->resolve();
Reid Spencer52402b02007-01-02 05:45:11 +00001753 $$.val = new std::string();
Reid Spencer319a7302007-01-05 17:20:02 +00001754 $$.type = DstTy;
Reid Spencer280d8012006-12-01 23:40:53 +00001755 if (*$1 == "cast") {
Reid Spencer319a7302007-01-05 17:20:02 +00001756 *$$.val += getCastUpgrade(source, SrcTy, DstTy, false);
Reid Spencera50d5962006-12-02 04:11:07 +00001757 } else {
Reid Spencer52402b02007-01-02 05:45:11 +00001758 *$$.val += *$1 + " " + source + " to " + DstTy->getNewTy();
Reid Spencer280d8012006-12-01 23:40:53 +00001759 }
Reid Spencerf5626a32007-01-01 01:20:41 +00001760 // Check to see if this is a useless cast of a value to the same name
1761 // and the same type. Such casts will probably cause redefinition errors
1762 // when assembled and perform no code gen action so just remove them.
1763 if (*$1 == "cast" || *$1 == "bitcast")
Reid Spencer319a7302007-01-05 17:20:02 +00001764 if (SrcTy->isInteger() && DstTy->isInteger() &&
1765 SrcTy->getBitWidth() == DstTy->getBitWidth()) {
Reid Spencerf5626a32007-01-01 01:20:41 +00001766 deleteUselessCastFlag = true; // Flag the "Inst" rule
1767 deleteUselessCastName = new std::string(*$2.val); // save the name
1768 size_t pos = deleteUselessCastName->find_first_of("%\"",0);
1769 if (pos != std::string::npos) {
1770 // remove the type portion before val
1771 deleteUselessCastName->erase(0, pos);
1772 }
1773 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001774 delete $1; $2.destroy();
Reid Spencer52402b02007-01-02 05:45:11 +00001775 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001776 }
1777 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001778 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
Reid Spencer52402b02007-01-02 05:45:11 +00001779 $$.val = $1;
1780 $$.type = $4.type;
1781 $2.destroy(); delete $4.val; $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001782 }
1783 | VAARG ResolvedVal ',' Types {
Reid Spencer52402b02007-01-02 05:45:11 +00001784 *$1 += " " + *$2.val + ", " + $4->getNewTy();
1785 $$.val = $1;
1786 $$.type = $4;
1787 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001788 }
1789 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001790 *$1 += " " + *$2.val + ", " + *$4.val;
Reid Spencer52402b02007-01-02 05:45:11 +00001791 $$.val = $1;
Reid Spencer319a7302007-01-05 17:20:02 +00001792 $2.type = $2.type->resolve();;
Reid Spencereff838e2007-01-03 23:45:42 +00001793 $$.type = $2.type->getElementType();
Reid Spencer52402b02007-01-02 05:45:11 +00001794 delete $2.val; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001795 }
1796 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001797 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
Reid Spencer52402b02007-01-02 05:45:11 +00001798 $$.val = $1;
1799 $$.type = $2.type;
1800 delete $2.val; $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001801 }
1802 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001803 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
Reid Spencer52402b02007-01-02 05:45:11 +00001804 $$.val = $1;
1805 $$.type = $2.type;
1806 delete $2.val; $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001807 }
1808 | PHI_TOK PHIList {
Reid Spencer52402b02007-01-02 05:45:11 +00001809 *$1 += " " + *$2.val;
1810 $$.val = $1;
1811 $$.type = $2.type;
1812 delete $2.val;
Reid Spencere7c3c602006-11-30 06:36:44 +00001813 }
1814 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
1815 if (!$2->empty())
1816 *$1 += " " + *$2;
1817 if (!$1->empty())
1818 *$1 += " ";
Reid Spencer52402b02007-01-02 05:45:11 +00001819 *$1 += $3->getNewTy() + " " + *$4.val + "(";
Reid Spencerf8483652006-12-02 15:16:01 +00001820 for (unsigned i = 0; i < $6->size(); ++i) {
1821 ValueInfo& VI = (*$6)[i];
1822 *$1 += *VI.val;
1823 if (i+1 < $6->size())
1824 *$1 += ", ";
1825 VI.destroy();
1826 }
1827 *$1 += ")";
Reid Spencer52402b02007-01-02 05:45:11 +00001828 $$.val = $1;
1829 $$.type = getFunctionReturnType($3);
Reid Spencer319a7302007-01-05 17:20:02 +00001830 delete $2; $4.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001831 }
1832 | MemoryInst ;
1833
1834
1835// IndexList - List of indices for GEP based instructions...
1836IndexList
Reid Spencerf8483652006-12-02 15:16:01 +00001837 : ',' ValueRefList { $$ = $2; }
1838 | /* empty */ { $$ = new ValueList(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001839 ;
1840
1841OptVolatile
1842 : VOLATILE
1843 | /* empty */ { $$ = new std::string(); }
1844 ;
1845
1846MemoryInst : MALLOC Types OptCAlign {
Reid Spencer52402b02007-01-02 05:45:11 +00001847 *$1 += " " + $2->getNewTy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001848 if (!$3->empty())
1849 *$1 += " " + *$3;
Reid Spencer52402b02007-01-02 05:45:11 +00001850 $$.val = $1;
1851 $$.type = $2->getPointerType();
Reid Spencer319a7302007-01-05 17:20:02 +00001852 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001853 }
1854 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencer52402b02007-01-02 05:45:11 +00001855 std::string Name = getUniqueName($5.val, $4);
1856 *$1 += " " + $2->getNewTy() + ", " + $4->getNewTy() + " " + Name;
Reid Spencere7c3c602006-11-30 06:36:44 +00001857 if (!$6->empty())
1858 *$1 += " " + *$6;
Reid Spencer52402b02007-01-02 05:45:11 +00001859 $$.val = $1;
1860 $$.type = $2->getPointerType();
Reid Spencer319a7302007-01-05 17:20:02 +00001861 $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001862 }
1863 | ALLOCA Types OptCAlign {
Reid Spencer52402b02007-01-02 05:45:11 +00001864 *$1 += " " + $2->getNewTy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001865 if (!$3->empty())
1866 *$1 += " " + *$3;
Reid Spencer52402b02007-01-02 05:45:11 +00001867 $$.val = $1;
1868 $$.type = $2->getPointerType();
Reid Spencer319a7302007-01-05 17:20:02 +00001869 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001870 }
1871 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencer52402b02007-01-02 05:45:11 +00001872 std::string Name = getUniqueName($5.val, $4);
1873 *$1 += " " + $2->getNewTy() + ", " + $4->getNewTy() + " " + Name;
Reid Spencere7c3c602006-11-30 06:36:44 +00001874 if (!$6->empty())
1875 *$1 += " " + *$6;
Reid Spencer52402b02007-01-02 05:45:11 +00001876 $$.val = $1;
1877 $$.type = $2->getPointerType();
Reid Spencer319a7302007-01-05 17:20:02 +00001878 $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001879 }
1880 | FREE ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001881 *$1 += " " + *$2.val;
Reid Spencer52402b02007-01-02 05:45:11 +00001882 $$.val = $1;
Reid Spencer319a7302007-01-05 17:20:02 +00001883 $$.type = TypeInfo::get("void", VoidTy);
Reid Spencere77e35e2006-12-01 20:26:20 +00001884 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001885 }
1886 | OptVolatile LOAD Types ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001887 std::string Name = getUniqueName($4.val, $3);
Reid Spencere7c3c602006-11-30 06:36:44 +00001888 if (!$1->empty())
1889 *$1 += " ";
Reid Spencer52402b02007-01-02 05:45:11 +00001890 *$1 += *$2 + " " + $3->getNewTy() + " " + Name;
1891 $$.val = $1;
Reid Spencer319a7302007-01-05 17:20:02 +00001892 $$.type = $3->getElementType();
1893 delete $2; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001894 }
1895 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001896 std::string Name = getUniqueName($6.val, $5);
Reid Spencere7c3c602006-11-30 06:36:44 +00001897 if (!$1->empty())
1898 *$1 += " ";
Reid Spencer52402b02007-01-02 05:45:11 +00001899 *$1 += *$2 + " " + *$3.val + ", " + $5->getNewTy() + " " + Name;
1900 $$.val = $1;
Reid Spencer319a7302007-01-05 17:20:02 +00001901 $$.type = TypeInfo::get("void", VoidTy);
1902 delete $2; $3.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001903 }
1904 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer52402b02007-01-02 05:45:11 +00001905 std::string Name = getUniqueName($3.val, $2);
Reid Spencerf459d392006-12-02 16:19:52 +00001906 // Upgrade the indices
1907 for (unsigned i = 0; i < $4->size(); ++i) {
1908 ValueInfo& VI = (*$4)[i];
Reid Spencer52402b02007-01-02 05:45:11 +00001909 if (VI.type->isUnsigned() && !VI.isConstant() &&
1910 VI.type->getBitWidth() < 64) {
Reid Spencerf459d392006-12-02 16:19:52 +00001911 std::string* old = VI.val;
1912 *O << " %gep_upgrade" << unique << " = zext " << *old
Reid Spencer71d2ec92006-12-31 06:02:26 +00001913 << " to i64\n";
1914 VI.val = new std::string("i64 %gep_upgrade" + llvm::utostr(unique++));
Reid Spencer319a7302007-01-05 17:20:02 +00001915 VI.type = TypeInfo::get("i64",ULongTy);
Reid Spencerf459d392006-12-02 16:19:52 +00001916 }
1917 }
Reid Spencer52402b02007-01-02 05:45:11 +00001918 *$1 += " " + $2->getNewTy() + " " + Name;
Reid Spencerf8483652006-12-02 15:16:01 +00001919 for (unsigned i = 0; i < $4->size(); ++i) {
1920 ValueInfo& VI = (*$4)[i];
1921 *$1 += ", " + *VI.val;
Reid Spencerf8483652006-12-02 15:16:01 +00001922 }
Reid Spencer52402b02007-01-02 05:45:11 +00001923 $$.val = $1;
1924 $$.type = getGEPIndexedType($2,$4);
1925 $3.destroy(); delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001926 };
1927
1928%%
1929
1930int yyerror(const char *ErrorMsg) {
1931 std::string where
1932 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1933 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
Reid Spencer319a7302007-01-05 17:20:02 +00001934 std::string errMsg = where + "error: " + std::string(ErrorMsg) +
1935 " while reading ";
Reid Spencere7c3c602006-11-30 06:36:44 +00001936 if (yychar == YYEMPTY || yychar == 0)
1937 errMsg += "end-of-file.";
1938 else
1939 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
Reid Spencer71d2ec92006-12-31 06:02:26 +00001940 std::cerr << "llvm-upgrade: " << errMsg << '\n';
Chris Lattner37e01c52007-01-04 18:46:42 +00001941 *O << "llvm-upgrade parse failed.\n";
Reid Spencere7c3c602006-11-30 06:36:44 +00001942 exit(1);
1943}
Reid Spencer319a7302007-01-05 17:20:02 +00001944
1945static void warning(const std::string& ErrorMsg) {
1946 std::string where
1947 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1948 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
1949 std::string errMsg = where + "warning: " + std::string(ErrorMsg) +
1950 " while reading ";
1951 if (yychar == YYEMPTY || yychar == 0)
1952 errMsg += "end-of-file.";
1953 else
1954 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
1955 std::cerr << "llvm-upgrade: " << errMsg << '\n';
1956}