blob: 0544d400e6ce4ce24ca6c75f7b6967c5fdf45931 [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()) {
Reid Spencerf8383de2007-01-06 06:04:32 +0000134 if (getNewTy()[0] == '%' && isdigit(newTy[1])) {
135 unsigned ref = atoi(&((newTy.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 Spencerf8383de2007-01-06 06:04:32 +0000144 TypeMap::iterator I = NamedTypes.find(newTy);
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
Reid Spencerf8383de2007-01-06 06:04:32 +0000316void TypeInfo::getSignedness(unsigned &sNum, unsigned &uNum,
317 UpRefStack& stack) const {
318 switch (oldTy) {
319 default:
320 case OpaqueTy: case LabelTy: case VoidTy: case BoolTy:
321 case FloatTy : case DoubleTy: case UpRefTy:
322 return;
323 case SByteTy: case ShortTy: case LongTy: case IntTy:
324 sNum++;
325 return;
326 case UByteTy: case UShortTy: case UIntTy: case ULongTy:
327 uNum++;
328 return;
329 case PointerTy:
330 case PackedTy:
331 case ArrayTy:
332 stack.push_back(this);
333 elemTy->getSignedness(sNum, uNum, stack);
334 return;
335 case StructTy:
336 case PackedStructTy: {
337 stack.push_back(this);
338 for (unsigned i = 0; i < elements->size(); i++) {
339 (*elements)[i]->getSignedness(sNum, uNum, stack);
340 }
341 return;
342 }
343 case UnresolvedTy: {
344 const TypeInfo* Ty = this->resolve();
345 // Let's not recurse.
346 UpRefStack::const_iterator I = stack.begin(), E = stack.end();
347 for ( ; I != E && *I != Ty; ++I)
348 ;
349 if (I == E)
350 Ty->getSignedness(sNum, uNum, stack);
351 return;
352 }
353 }
354}
355
356std::string AddSuffix(const std::string& Name, const std::string& Suffix) {
357 if (Name[Name.size()-1] == '"') {
358 std::string Result = Name;
359 Result.insert(Result.size()-1, Suffix);
360 return Result;
361 }
362 return Name + Suffix;
363}
364
365std::string TypeInfo::makeUniqueName(const std::string& BaseName) const {
366 if (BaseName == "\"alloca point\"")
367 return BaseName;
368 switch (oldTy) {
369 default:
370 break;
371 case OpaqueTy: case LabelTy: case VoidTy: case BoolTy: case UpRefTy:
372 case FloatTy : case DoubleTy: case UnresolvedTy:
373 return BaseName;
374 case SByteTy: case ShortTy: case LongTy: case IntTy:
375 return AddSuffix(BaseName, ".s");
376 case UByteTy: case UShortTy: case UIntTy: case ULongTy:
377 return AddSuffix(BaseName, ".u");
378 }
379
380 unsigned uNum = 0, sNum = 0;
381 std::string Suffix;
382 switch (oldTy) {
383 case PointerTy:
384 case PackedTy:
385 case ArrayTy: {
386 TypeInfo::UpRefStack stack;
387 elemTy->resolve()->getSignedness(sNum, uNum, stack);
388 break;
389 }
390 case StructTy:
391 case PackedStructTy: {
392 for (unsigned i = 0; i < elements->size(); i++) {
393 TypeInfo::UpRefStack stack;
394 (*elements)[i]->resolve()->getSignedness(sNum, uNum, stack);
395 }
396 break;
397 }
398 default:
399 assert(0 && "Invalid Type");
400 break;
401 }
402
403 if (sNum == 0 && uNum == 0)
404 return BaseName;
405
406 switch (oldTy) {
407 default: Suffix += ".nada"; break;
408 case PointerTy: Suffix += ".pntr"; break;
409 case PackedTy: Suffix += ".pckd"; break;
410 case ArrayTy: Suffix += ".arry"; break;
411 case StructTy: Suffix += ".strc"; break;
412 case PackedStructTy: Suffix += ".pstr"; break;
413 }
414
415 Suffix += ".s" + llvm::utostr(sNum);
416 Suffix += ".u" + llvm::utostr(uNum);
417 return AddSuffix(BaseName, Suffix);
418}
419
Reid Spencer319a7302007-01-05 17:20:02 +0000420TypeInfo& TypeInfo::operator=(const TypeInfo& that) {
421 oldTy = that.oldTy;
422 nelems = that.nelems;
423 newTy = that.newTy;
424 elemTy = that.elemTy;
425 resultTy = that.resultTy;
426 if (that.elements) {
427 elements = new TypeList(that.elements->size());
428 *elements = *that.elements;
429 } else {
430 elements = 0;
431 }
432 return *this;
433}
434
435const TypeInfo* TypeInfo::add_new_type(TypeInfo* newTy) {
436 TypeRegMap::iterator I = registry.find(newTy);
437 if (I != registry.end()) {
438 delete newTy;
439 return *I;
440 }
441 registry.insert(newTy);
442 return newTy;
Reid Spencer280d8012006-12-01 23:40:53 +0000443}
444
Reid Spencera50d5962006-12-02 04:11:07 +0000445static const char* getCastOpcode(
Reid Spencer52402b02007-01-02 05:45:11 +0000446 std::string& Source, const TypeInfo* SrcTy, const TypeInfo* DstTy)
Reid Spencera50d5962006-12-02 04:11:07 +0000447{
Reid Spencer52402b02007-01-02 05:45:11 +0000448 unsigned SrcBits = SrcTy->getBitWidth();
449 unsigned DstBits = DstTy->getBitWidth();
Reid Spencere77e35e2006-12-01 20:26:20 +0000450 const char* opcode = "bitcast";
451 // Run through the possibilities ...
Reid Spencer52402b02007-01-02 05:45:11 +0000452 if (DstTy->isIntegral()) { // Casting to integral
453 if (SrcTy->isIntegral()) { // Casting from integral
Reid Spencere77e35e2006-12-01 20:26:20 +0000454 if (DstBits < SrcBits)
455 opcode = "trunc";
456 else if (DstBits > SrcBits) { // its an extension
Reid Spencer52402b02007-01-02 05:45:11 +0000457 if (SrcTy->isSigned())
Reid Spencere77e35e2006-12-01 20:26:20 +0000458 opcode ="sext"; // signed -> SEXT
459 else
460 opcode = "zext"; // unsigned -> ZEXT
461 } else {
462 opcode = "bitcast"; // Same size, No-op cast
463 }
Reid Spencer52402b02007-01-02 05:45:11 +0000464 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
465 if (DstTy->isSigned())
Reid Spencere77e35e2006-12-01 20:26:20 +0000466 opcode = "fptosi"; // FP -> sint
467 else
468 opcode = "fptoui"; // FP -> uint
Reid Spencer52402b02007-01-02 05:45:11 +0000469 } else if (SrcTy->isPacked()) {
470 assert(DstBits == SrcTy->getBitWidth() &&
Reid Spencere77e35e2006-12-01 20:26:20 +0000471 "Casting packed to integer of different width");
472 opcode = "bitcast"; // same size, no-op cast
473 } else {
Reid Spencer52402b02007-01-02 05:45:11 +0000474 assert(SrcTy->isPointer() &&
Reid Spencere77e35e2006-12-01 20:26:20 +0000475 "Casting from a value that is not first-class type");
476 opcode = "ptrtoint"; // ptr -> int
477 }
Reid Spencer52402b02007-01-02 05:45:11 +0000478 } else if (DstTy->isFloatingPoint()) { // Casting to floating pt
479 if (SrcTy->isIntegral()) { // Casting from integral
480 if (SrcTy->isSigned())
Reid Spencere77e35e2006-12-01 20:26:20 +0000481 opcode = "sitofp"; // sint -> FP
482 else
483 opcode = "uitofp"; // uint -> FP
Reid Spencer52402b02007-01-02 05:45:11 +0000484 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencere77e35e2006-12-01 20:26:20 +0000485 if (DstBits < SrcBits) {
486 opcode = "fptrunc"; // FP -> smaller FP
487 } else if (DstBits > SrcBits) {
488 opcode = "fpext"; // FP -> larger FP
489 } else {
490 opcode ="bitcast"; // same size, no-op cast
491 }
Reid Spencer52402b02007-01-02 05:45:11 +0000492 } else if (SrcTy->isPacked()) {
493 assert(DstBits == SrcTy->getBitWidth() &&
Reid Spencere77e35e2006-12-01 20:26:20 +0000494 "Casting packed to floating point of different width");
495 opcode = "bitcast"; // same size, no-op cast
496 } else {
497 assert(0 && "Casting pointer or non-first class to float");
498 }
Reid Spencer52402b02007-01-02 05:45:11 +0000499 } else if (DstTy->isPacked()) {
500 if (SrcTy->isPacked()) {
501 assert(DstTy->getBitWidth() == SrcTy->getBitWidth() &&
Reid Spencere77e35e2006-12-01 20:26:20 +0000502 "Casting packed to packed of different widths");
503 opcode = "bitcast"; // packed -> packed
Reid Spencer52402b02007-01-02 05:45:11 +0000504 } else if (DstTy->getBitWidth() == SrcBits) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000505 opcode = "bitcast"; // float/int -> packed
506 } else {
507 assert(!"Illegal cast to packed (wrong type or size)");
508 }
Reid Spencer52402b02007-01-02 05:45:11 +0000509 } else if (DstTy->isPointer()) {
510 if (SrcTy->isPointer()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000511 opcode = "bitcast"; // ptr -> ptr
Reid Spencer52402b02007-01-02 05:45:11 +0000512 } else if (SrcTy->isIntegral()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000513 opcode = "inttoptr"; // int -> ptr
514 } else {
Reid Spencera50d5962006-12-02 04:11:07 +0000515 assert(!"Casting invalid type to pointer");
Reid Spencere77e35e2006-12-01 20:26:20 +0000516 }
517 } else {
518 assert(!"Casting to type that is not first-class");
519 }
520 return opcode;
521}
522
Reid Spencer319a7302007-01-05 17:20:02 +0000523static std::string getCastUpgrade(const std::string& Src, const TypeInfo* SrcTy,
524 const TypeInfo* DstTy, bool isConst)
Reid Spencera50d5962006-12-02 04:11:07 +0000525{
526 std::string Result;
527 std::string Source = Src;
Reid Spencer52402b02007-01-02 05:45:11 +0000528 if (SrcTy->isFloatingPoint() && DstTy->isPointer()) {
Reid Spencera50d5962006-12-02 04:11:07 +0000529 // fp -> ptr cast is no longer supported but we must upgrade this
530 // by doing a double cast: fp -> int -> ptr
531 if (isConst)
Reid Spencer71d2ec92006-12-31 06:02:26 +0000532 Source = "i64 fptoui(" + Source + " to i64)";
Reid Spencera50d5962006-12-02 04:11:07 +0000533 else {
Reid Spencerf459d392006-12-02 16:19:52 +0000534 *O << " %cast_upgrade" << unique << " = fptoui " << Source
Reid Spencer71d2ec92006-12-31 06:02:26 +0000535 << " to i64\n";
536 Source = "i64 %cast_upgrade" + llvm::utostr(unique);
Reid Spencera50d5962006-12-02 04:11:07 +0000537 }
538 // Update the SrcTy for the getCastOpcode call below
Reid Spencer319a7302007-01-05 17:20:02 +0000539 SrcTy = TypeInfo::get("i64", ULongTy);
Reid Spencer52402b02007-01-02 05:45:11 +0000540 } else if (DstTy->isBool()) {
541 // cast type %x to bool was previously defined as setne type %x, null
542 // The cast semantic is now to truncate, not compare so we must retain
543 // the original intent by replacing the cast with a setne
544 const char* comparator = SrcTy->isPointer() ? ", null" :
545 (SrcTy->isFloatingPoint() ? ", 0.0" :
546 (SrcTy->isBool() ? ", false" : ", 0"));
547 const char* compareOp = SrcTy->isFloatingPoint() ? "fcmp one " : "icmp ne ";
Reid Spencer187ccf82006-12-09 16:57:22 +0000548 if (isConst) {
549 Result = "(" + Source + comparator + ")";
550 Result = compareOp + Result;
551 } else
552 Result = compareOp + Source + comparator;
Reid Spencera50d5962006-12-02 04:11:07 +0000553 return Result; // skip cast processing below
554 }
Reid Spencer319a7302007-01-05 17:20:02 +0000555 SrcTy = SrcTy->resolve();
556 DstTy = DstTy->resolve();
Reid Spencera50d5962006-12-02 04:11:07 +0000557 std::string Opcode(getCastOpcode(Source, SrcTy, DstTy));
558 if (isConst)
Reid Spencer52402b02007-01-02 05:45:11 +0000559 Result += Opcode + "( " + Source + " to " + DstTy->getNewTy() + ")";
Reid Spencera50d5962006-12-02 04:11:07 +0000560 else
Reid Spencer52402b02007-01-02 05:45:11 +0000561 Result += Opcode + " " + Source + " to " + DstTy->getNewTy();
Reid Spencera50d5962006-12-02 04:11:07 +0000562 return Result;
563}
564
Reid Spencer319a7302007-01-05 17:20:02 +0000565const char* getDivRemOpcode(const std::string& opcode, const TypeInfo* TI) {
Reid Spencer78720742006-12-02 20:21:22 +0000566 const char* op = opcode.c_str();
Reid Spencer319a7302007-01-05 17:20:02 +0000567 const TypeInfo* Ty = TI->resolve();
Reid Spencer52402b02007-01-02 05:45:11 +0000568 if (Ty->isPacked())
569 Ty = Ty->getElementType();
Reid Spencer78720742006-12-02 20:21:22 +0000570 if (opcode == "div")
Reid Spencer52402b02007-01-02 05:45:11 +0000571 if (Ty->isFloatingPoint())
Reid Spencer78720742006-12-02 20:21:22 +0000572 op = "fdiv";
Reid Spencer52402b02007-01-02 05:45:11 +0000573 else if (Ty->isUnsigned())
Reid Spencer78720742006-12-02 20:21:22 +0000574 op = "udiv";
Reid Spencer52402b02007-01-02 05:45:11 +0000575 else if (Ty->isSigned())
Reid Spencer78720742006-12-02 20:21:22 +0000576 op = "sdiv";
577 else
578 yyerror("Invalid type for div instruction");
579 else if (opcode == "rem")
Reid Spencer52402b02007-01-02 05:45:11 +0000580 if (Ty->isFloatingPoint())
Reid Spencer78720742006-12-02 20:21:22 +0000581 op = "frem";
Reid Spencer52402b02007-01-02 05:45:11 +0000582 else if (Ty->isUnsigned())
Reid Spencer78720742006-12-02 20:21:22 +0000583 op = "urem";
Reid Spencer52402b02007-01-02 05:45:11 +0000584 else if (Ty->isSigned())
Reid Spencer78720742006-12-02 20:21:22 +0000585 op = "srem";
586 else
587 yyerror("Invalid type for rem instruction");
588 return op;
589}
Reid Spencer229e9362006-12-02 22:14:11 +0000590
591std::string
Reid Spencer52402b02007-01-02 05:45:11 +0000592getCompareOp(const std::string& setcc, const TypeInfo* TI) {
Reid Spencer229e9362006-12-02 22:14:11 +0000593 assert(setcc.length() == 5);
594 char cc1 = setcc[3];
595 char cc2 = setcc[4];
596 assert(cc1 == 'e' || cc1 == 'n' || cc1 == 'l' || cc1 == 'g');
597 assert(cc2 == 'q' || cc2 == 'e' || cc2 == 'e' || cc2 == 't');
598 std::string result("xcmp xxx");
599 result[6] = cc1;
600 result[7] = cc2;
Reid Spencer52402b02007-01-02 05:45:11 +0000601 if (TI->isFloatingPoint()) {
Reid Spencer229e9362006-12-02 22:14:11 +0000602 result[0] = 'f';
Reid Spencere4d87aa2006-12-23 06:05:41 +0000603 result[5] = 'o';
Reid Spencerf0cf1322006-12-07 04:23:03 +0000604 if (cc1 == 'n')
605 result[5] = 'u'; // NE maps to unordered
606 else
607 result[5] = 'o'; // everything else maps to ordered
Reid Spencer52402b02007-01-02 05:45:11 +0000608 } else if (TI->isIntegral() || TI->isPointer()) {
Reid Spencer229e9362006-12-02 22:14:11 +0000609 result[0] = 'i';
610 if ((cc1 == 'e' && cc2 == 'q') || (cc1 == 'n' && cc2 == 'e'))
611 result.erase(5,1);
Reid Spencer52402b02007-01-02 05:45:11 +0000612 else if (TI->isSigned())
Reid Spencer229e9362006-12-02 22:14:11 +0000613 result[5] = 's';
Reid Spencer52402b02007-01-02 05:45:11 +0000614 else if (TI->isUnsigned() || TI->isPointer() || TI->isBool())
Reid Spencer229e9362006-12-02 22:14:11 +0000615 result[5] = 'u';
616 else
617 yyerror("Invalid integral type for setcc");
618 }
619 return result;
620}
621
Reid Spencer319a7302007-01-05 17:20:02 +0000622static const TypeInfo* getFunctionReturnType(const TypeInfo* PFTy) {
623 PFTy = PFTy->resolve();
Reid Spencer52402b02007-01-02 05:45:11 +0000624 if (PFTy->isPointer()) {
Reid Spencer319a7302007-01-05 17:20:02 +0000625 const TypeInfo* ElemTy = PFTy->getElementType();
626 ElemTy = ElemTy->resolve();
Reid Spencer52402b02007-01-02 05:45:11 +0000627 if (ElemTy->isFunction())
Reid Spencer319a7302007-01-05 17:20:02 +0000628 return ElemTy->getResultType();
Reid Spencer52402b02007-01-02 05:45:11 +0000629 } else if (PFTy->isFunction()) {
Reid Spencer319a7302007-01-05 17:20:02 +0000630 return PFTy->getResultType();
Reid Spencer52402b02007-01-02 05:45:11 +0000631 }
Reid Spencer319a7302007-01-05 17:20:02 +0000632 return PFTy;
Reid Spencer52402b02007-01-02 05:45:11 +0000633}
634
Reid Spencer319a7302007-01-05 17:20:02 +0000635static const TypeInfo* ResolveUpReference(const TypeInfo* Ty,
Reid Spencerf8383de2007-01-06 06:04:32 +0000636 TypeInfo::UpRefStack* stack) {
Reid Spencereff838e2007-01-03 23:45:42 +0000637 assert(Ty->isUpReference() && "Can't resolve a non-upreference");
Reid Spencer319a7302007-01-05 17:20:02 +0000638 unsigned upref = Ty->getUpRefNum();
Reid Spencereff838e2007-01-03 23:45:42 +0000639 assert(upref < stack->size() && "Invalid up reference");
640 return (*stack)[upref - stack->size() - 1];
641}
642
Reid Spencer319a7302007-01-05 17:20:02 +0000643static const TypeInfo* getGEPIndexedType(const TypeInfo* PTy, ValueList* idxs) {
644 const TypeInfo* Result = PTy = PTy->resolve();
Reid Spencer52402b02007-01-02 05:45:11 +0000645 assert(PTy->isPointer() && "GEP Operand is not a pointer?");
Reid Spencerf8383de2007-01-06 06:04:32 +0000646 TypeInfo::UpRefStack stack;
Reid Spencereff838e2007-01-03 23:45:42 +0000647 for (unsigned i = 0; i < idxs->size(); ++i) {
Reid Spencer52402b02007-01-02 05:45:11 +0000648 if (Result->isComposite()) {
649 Result = Result->getIndexedType((*idxs)[i]);
Reid Spencer319a7302007-01-05 17:20:02 +0000650 Result = Result->resolve();
Reid Spencereff838e2007-01-03 23:45:42 +0000651 stack.push_back(Result);
Reid Spencer52402b02007-01-02 05:45:11 +0000652 } else
653 yyerror("Invalid type for index");
654 }
Reid Spencereff838e2007-01-03 23:45:42 +0000655 // Resolve upreferences so we can return a more natural type
656 if (Result->isPointer()) {
657 if (Result->getElementType()->isUpReference()) {
658 stack.push_back(Result);
659 Result = ResolveUpReference(Result->getElementType(), &stack);
660 }
661 } else if (Result->isUpReference()) {
662 Result = ResolveUpReference(Result->getElementType(), &stack);
663 }
Reid Spencer52402b02007-01-02 05:45:11 +0000664 return Result->getPointerType();
665}
666
Reid Spencer52402b02007-01-02 05:45:11 +0000667
668// This function handles appending .u or .s to integer value names that
669// were previously unsigned or signed, respectively. This avoids name
670// collisions since the unsigned and signed type planes have collapsed
671// into a single signless type plane.
Reid Spencer319a7302007-01-05 17:20:02 +0000672static std::string getUniqueName(const std::string *Name, const TypeInfo* Ty,
Reid Spencerf8383de2007-01-06 06:04:32 +0000673 bool isGlobal = false, bool isDef = false) {
Reid Spencer319a7302007-01-05 17:20:02 +0000674
Reid Spencer52402b02007-01-02 05:45:11 +0000675 // If its not a symbolic name, don't modify it, probably a constant val.
676 if ((*Name)[0] != '%' && (*Name)[0] != '"')
677 return *Name;
Reid Spencer319a7302007-01-05 17:20:02 +0000678
Reid Spencer52402b02007-01-02 05:45:11 +0000679 // If its a numeric reference, just leave it alone.
680 if (isdigit((*Name)[1]))
681 return *Name;
682
683 // Resolve the type
Reid Spencer319a7302007-01-05 17:20:02 +0000684 Ty = Ty->resolve();
685
686 // If its a global name, get its uniquified name, if any
687 GlobalsTypeMap::iterator GI = Globals.find(*Name);
688 if (GI != Globals.end()) {
689 TypePlaneMap::iterator TPI = GI->second.begin();
690 TypePlaneMap::iterator TPE = GI->second.end();
691 for ( ; TPI != TPE ; ++TPI) {
692 if (TPI->first->sameNewTyAs(Ty))
693 return TPI->second;
694 }
695 }
696
697 if (isGlobal) {
698 // We didn't find a global name, but if its supposed to be global then all
699 // we can do is return the name. This is probably a forward reference of a
700 // global value that hasn't been defined yet. Since we have no definition
701 // we don't know its linkage class. Just assume its an external and the name
702 // shouldn't change.
703 return *Name;
704 }
Reid Spencer52402b02007-01-02 05:45:11 +0000705
706 // Default the result to the current name
Reid Spencerf8383de2007-01-06 06:04:32 +0000707 std::string Result = Ty->makeUniqueName(*Name);
Reid Spencer52402b02007-01-02 05:45:11 +0000708
Reid Spencer52402b02007-01-02 05:45:11 +0000709 return Result;
710}
711
Reid Spencer319a7302007-01-05 17:20:02 +0000712static unsigned UniqueNameCounter = 0;
713
714std::string getGlobalName(const std::string* Name, const std::string Linkage,
715 const TypeInfo* Ty, bool isConstant) {
716 // Default to given name
717 std::string Result = *Name;
718 // Look up the name in the Globals Map
719 GlobalsTypeMap::iterator GI = Globals.find(*Name);
720 // Did we see this global name before?
721 if (GI != Globals.end()) {
722 if (Ty->isUnresolvedDeep()) {
723 // The Gval's type is unresolved. Consequently, we can't disambiguate it
724 // by type. We'll just change its name and emit a warning.
725 warning("Cannot disambiguate global value '" + *Name +
726 "' because type '" + Ty->getNewTy() + "'is unresolved.\n");
727 Result = *Name + ".unique";
728 UniqueNameCounter++;
729 Result += llvm::utostr(UniqueNameCounter);
730 return Result;
731 } else {
732 TypePlaneMap::iterator TPI = GI->second.find(Ty);
733 if (TPI != GI->second.end()) {
734 // We found an existing name of the same old type. This isn't allowed
735 // in LLVM 2.0. Consequently, we must alter the name of the global so it
736 // can at least compile. References to the global will yield the first
737 // definition, which is okay. We also must warn about this.
738 Result = *Name + ".unique";
739 UniqueNameCounter++;
740 Result += llvm::utostr(UniqueNameCounter);
741 warning(std::string("Global variable '") + *Name + "' was renamed to '"+
742 Result + "'");
743 } else {
744 // There isn't an existing definition for this name according to the
745 // old types. Now search the TypePlanMap for types with the same new
746 // name.
747 TypePlaneMap::iterator TPI = GI->second.begin();
748 TypePlaneMap::iterator TPE = GI->second.end();
749 for ( ; TPI != TPE; ++TPI) {
750 if (TPI->first->sameNewTyAs(Ty)) {
751 // The new types are the same but the old types are different so
752 // this is a global name collision resulting from type planes
753 // collapsing.
754 if (Linkage == "external" || Linkage == "dllimport" ||
755 Linkage == "extern_weak" || Linkage == "") {
756 // The linkage of this gval is external so we can't reliably
757 // rename it because it could potentially create a linking
758 // problem. However, we can't leave the name conflict in the
759 // output either or it won't assemble with LLVM 2.0. So, all we
760 // can do is rename this one to something unique and emit a
761 // warning about the problem.
762 Result = *Name + ".unique";
763 UniqueNameCounter++;
764 Result += llvm::utostr(UniqueNameCounter);
765 warning("Renaming global value '" + *Name + "' to '" + Result +
766 "' may cause linkage errors.");
767 return Result;
768 } else {
769 // Its linkage is internal and its type is known so we can
770 // disambiguate the name collision successfully based on the type.
771 Result = getUniqueName(Name, Ty);
772 TPI->second = Result;
773 return Result;
774 }
775 }
776 }
777 // We didn't find an entry in the type plane with the same new type and
778 // the old types differ so this is a new type plane for this global
779 // variable. We just fall through to the logic below which inserts
780 // the global.
781 }
782 }
783 }
784
785 // Its a new global name, if it is external we can't change it
786 if (isConstant || Linkage == "external" || Linkage == "dllimport" ||
787 Linkage == "extern_weak" || Linkage == "") {
788 Globals[Result][Ty] = Result;
789 return Result;
790 }
791
792 // Its a new global name, and it is internal, change the name to make it
793 // unique for its type.
794 // Result = getUniqueName(Name, Ty);
795 Globals[*Name][Ty] = Result;
796 return Result;
797}
Reid Spencere7c3c602006-11-30 06:36:44 +0000798%}
799
Reid Spencerf0cf1322006-12-07 04:23:03 +0000800// %file-prefix="UpgradeParser"
Reid Spencere77e35e2006-12-01 20:26:20 +0000801
802%union {
803 std::string* String;
Reid Spencer319a7302007-01-05 17:20:02 +0000804 const TypeInfo* Type;
Reid Spencere77e35e2006-12-01 20:26:20 +0000805 ValueInfo Value;
806 ConstInfo Const;
Reid Spencerf8483652006-12-02 15:16:01 +0000807 ValueList* ValList;
Reid Spencer52402b02007-01-02 05:45:11 +0000808 TypeList* TypeVec;
Reid Spencere77e35e2006-12-01 20:26:20 +0000809}
810
Reid Spencerf2d55322006-12-01 21:52:30 +0000811%token <Type> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
Reid Spencera50d5962006-12-02 04:11:07 +0000812%token <Type> FLOAT DOUBLE LABEL
813%token <String> OPAQUE ESINT64VAL EUINT64VAL SINTVAL UINTVAL FPVAL
Reid Spencerf2d55322006-12-01 21:52:30 +0000814%token <String> NULL_TOK UNDEF ZEROINITIALIZER TRUETOK FALSETOK
Reid Spencere77e35e2006-12-01 20:26:20 +0000815%token <String> TYPE VAR_ID LABELSTR STRINGCONSTANT
816%token <String> IMPLEMENTATION BEGINTOK ENDTOK
Reid Spencer71d2ec92006-12-31 06:02:26 +0000817%token <String> DECLARE GLOBAL CONSTANT SECTION VOLATILE
Reid Spencere77e35e2006-12-01 20:26:20 +0000818%token <String> TO DOTDOTDOT CONST INTERNAL LINKONCE WEAK
819%token <String> DLLIMPORT DLLEXPORT EXTERN_WEAK APPENDING
820%token <String> NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
Reid Spencer78720742006-12-02 20:21:22 +0000821%token <String> ALIGN UNINITIALIZED
Reid Spencere77e35e2006-12-01 20:26:20 +0000822%token <String> DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
823%token <String> CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
824%token <String> X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
825%token <String> DATALAYOUT
Reid Spencer78720742006-12-02 20:21:22 +0000826%token <String> RET BR SWITCH INVOKE EXCEPT UNWIND UNREACHABLE
827%token <String> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM AND OR XOR
Reid Spencere77e35e2006-12-01 20:26:20 +0000828%token <String> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
Reid Spencer229e9362006-12-02 22:14:11 +0000829%token <String> ICMP FCMP EQ NE SLT SGT SLE SGE OEQ ONE OLT OGT OLE OGE
830%token <String> ORD UNO UEQ UNE ULT UGT ULE UGE
Reid Spencere77e35e2006-12-01 20:26:20 +0000831%token <String> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Reid Spencerf7bde222006-12-01 22:26:37 +0000832%token <String> PHI_TOK SELECT SHL SHR ASHR LSHR VAARG
Reid Spencere77e35e2006-12-01 20:26:20 +0000833%token <String> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Reid Spencerfcb5df82006-12-01 22:34:43 +0000834%token <String> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI UITOFP SITOFP
835%token <String> PTRTOINT INTTOPTR BITCAST
Reid Spencere77e35e2006-12-01 20:26:20 +0000836
837%type <String> OptAssign OptLinkage OptCallingConv OptAlign OptCAlign
838%type <String> SectionString OptSection GlobalVarAttributes GlobalVarAttribute
Reid Spencer52402b02007-01-02 05:45:11 +0000839%type <String> ConstExpr DefinitionList
Reid Spencere77e35e2006-12-01 20:26:20 +0000840%type <String> ConstPool TargetDefinition LibrariesDefinition LibList OptName
841%type <String> ArgVal ArgListH ArgList FunctionHeaderH BEGIN FunctionHeader END
Reid Spencer52402b02007-01-02 05:45:11 +0000842%type <String> Function FunctionProto BasicBlock
843%type <String> InstructionList BBTerminatorInst JumpTable Inst
844%type <String> OptTailCall OptVolatile Unwind
845%type <String> SymbolicValueRef OptSideEffect GlobalType
Reid Spencere77e35e2006-12-01 20:26:20 +0000846%type <String> FnDeclareLinkage BasicBlockList BigOrLittle AsmBlock
Reid Spencer78720742006-12-02 20:21:22 +0000847%type <String> Name ConstValueRef ConstVector External
Reid Spencer57f28f92006-12-03 07:10:26 +0000848%type <String> ShiftOps SetCondOps LogicalOps ArithmeticOps CastOps
849%type <String> IPredicates FPredicates
Reid Spencere77e35e2006-12-01 20:26:20 +0000850
Reid Spencerf8483652006-12-02 15:16:01 +0000851%type <ValList> ValueRefList ValueRefListE IndexList
Reid Spencer52402b02007-01-02 05:45:11 +0000852%type <TypeVec> TypeListI ArgTypeListI
Reid Spencere77e35e2006-12-01 20:26:20 +0000853
854%type <Type> IntType SIntType UIntType FPType TypesV Types
855%type <Type> PrimType UpRTypesV UpRTypes
856
Reid Spencerf2d55322006-12-01 21:52:30 +0000857%type <String> IntVal EInt64Val
858%type <Const> ConstVal
Reid Spencere77e35e2006-12-01 20:26:20 +0000859
Reid Spencer52402b02007-01-02 05:45:11 +0000860%type <Value> ValueRef ResolvedVal InstVal PHIList MemoryInst
Reid Spencere7c3c602006-11-30 06:36:44 +0000861
862%start Module
863
864%%
865
866// Handle constant integer size restriction and conversion...
Reid Spencerf2d55322006-12-01 21:52:30 +0000867IntVal : SINTVAL | UINTVAL ;
Reid Spencere77e35e2006-12-01 20:26:20 +0000868EInt64Val : ESINT64VAL | EUINT64VAL;
Reid Spencere7c3c602006-11-30 06:36:44 +0000869
870// Operations that are notably excluded from this list include:
871// RET, BR, & SWITCH because they end basic blocks and are treated specially.
Reid Spencer78720742006-12-02 20:21:22 +0000872ArithmeticOps: ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV
873 | REM | UREM | SREM | FREM;
Reid Spencere7c3c602006-11-30 06:36:44 +0000874LogicalOps : AND | OR | XOR;
875SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Reid Spencer57f28f92006-12-03 07:10:26 +0000876IPredicates : EQ | NE | SLT | SGT | SLE | SGE | ULT | UGT | ULE | UGE;
877FPredicates : OEQ | ONE | OLT | OGT | OLE | OGE | ORD | UNO | UEQ | UNE
878 | ULT | UGT | ULE | UGE | TRUETOK | FALSETOK;
Reid Spencerf7bde222006-12-01 22:26:37 +0000879ShiftOps : SHL | SHR | ASHR | LSHR;
Reid Spencerfcb5df82006-12-01 22:34:43 +0000880CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI |
881 UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
882 ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000883
884// These are some types that allow classification if we only want a particular
885// thing... for example, only a signed, unsigned, or integral type.
886SIntType : LONG | INT | SHORT | SBYTE;
887UIntType : ULONG | UINT | USHORT | UBYTE;
888IntType : SIntType | UIntType;
889FPType : FLOAT | DOUBLE;
890
891// OptAssign - Value producing statements have an optional assignment component
892OptAssign : Name '=' {
Reid Spencere7c3c602006-11-30 06:36:44 +0000893 $$ = $1;
894 }
895 | /*empty*/ {
896 $$ = new std::string("");
897 };
898
899OptLinkage
900 : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT
901 | EXTERN_WEAK
902 | /*empty*/ { $$ = new std::string(""); } ;
903
904OptCallingConv
905 : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK
Reid Spencer16244f42006-12-01 21:10:07 +0000906 | X86_FASTCALLCC_TOK
907 | CC_TOK EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000908 *$1 += *$2;
909 delete $2;
Reid Spencer16244f42006-12-01 21:10:07 +0000910 $$ = $1;
911 }
Reid Spencere7c3c602006-11-30 06:36:44 +0000912 | /*empty*/ { $$ = new std::string(""); } ;
913
914// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
915// a comma before it.
916OptAlign
917 : /*empty*/ { $$ = new std::string(); }
Reid Spencerf2d55322006-12-01 21:52:30 +0000918 | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
Reid Spencerf0cf1322006-12-07 04:23:03 +0000919
Reid Spencere7c3c602006-11-30 06:36:44 +0000920OptCAlign
921 : /*empty*/ { $$ = new std::string(); }
922 | ',' ALIGN EUINT64VAL {
923 $2->insert(0, ", ");
Reid Spencerf2d55322006-12-01 21:52:30 +0000924 *$2 += " " + *$3;
925 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000926 $$ = $2;
927 };
928
929SectionString
930 : SECTION STRINGCONSTANT {
931 *$1 += " " + *$2;
932 delete $2;
933 $$ = $1;
934 };
935
936OptSection : /*empty*/ { $$ = new std::string(); }
937 | SectionString;
938
939GlobalVarAttributes
940 : /* empty */ { $$ = new std::string(); }
941 | ',' GlobalVarAttribute GlobalVarAttributes {
942 $2->insert(0, ", ");
943 if (!$3->empty())
944 *$2 += " " + *$3;
945 delete $3;
946 $$ = $2;
947 };
948
949GlobalVarAttribute
950 : SectionString
951 | ALIGN EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000952 *$1 += " " + *$2;
953 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000954 $$ = $1;
955 };
956
957//===----------------------------------------------------------------------===//
958// Types includes all predefined types... except void, because it can only be
959// used in specific contexts (function returning void for example). To have
960// access to it, a user must explicitly use TypesV.
961//
962
963// TypesV includes all of 'Types', but it also includes the void type.
964TypesV : Types | VOID ;
965UpRTypesV : UpRTypes | VOID ;
966Types : UpRTypes ;
967
968// Derived types are added later...
969//
970PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
Reid Spencere77e35e2006-12-01 20:26:20 +0000971PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
Reid Spencera50d5962006-12-02 04:11:07 +0000972UpRTypes
973 : OPAQUE {
Reid Spencer319a7302007-01-05 17:20:02 +0000974 $$ = TypeInfo::get(*$1, OpaqueTy);
Reid Spencera50d5962006-12-02 04:11:07 +0000975 }
976 | SymbolicValueRef {
Reid Spencer319a7302007-01-05 17:20:02 +0000977 $$ = TypeInfo::get(*$1, UnresolvedTy);
Reid Spencera50d5962006-12-02 04:11:07 +0000978 }
Reid Spencer78720742006-12-02 20:21:22 +0000979 | PrimType {
980 $$ = $1;
981 }
982 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerf2d55322006-12-01 21:52:30 +0000983 $2->insert(0, "\\");
Reid Spencer319a7302007-01-05 17:20:02 +0000984 $$ = TypeInfo::get(*$2, UpRefTy);
Reid Spencere7c3c602006-11-30 06:36:44 +0000985 }
986 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Reid Spencer52402b02007-01-02 05:45:11 +0000987 std::string newTy( $1->getNewTy() + "(");
988 for (unsigned i = 0; i < $3->size(); ++i) {
989 if (i != 0)
990 newTy += ", ";
991 if ((*$3)[i]->isVoid())
992 newTy += "...";
993 else
994 newTy += (*$3)[i]->getNewTy();
995 }
996 newTy += ")";
Reid Spencer319a7302007-01-05 17:20:02 +0000997 $$ = TypeInfo::get(newTy, $1, $3);
Reid Spencere7c3c602006-11-30 06:36:44 +0000998 }
999 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencer319a7302007-01-05 17:20:02 +00001000 uint64_t elems = atoi($2->c_str());
Reid Spencerf2d55322006-12-01 21:52:30 +00001001 $2->insert(0,"[ ");
Reid Spencer52402b02007-01-02 05:45:11 +00001002 *$2 += " x " + $4->getNewTy() + " ]";
Reid Spencer319a7302007-01-05 17:20:02 +00001003 $$ = TypeInfo::get(*$2, ArrayTy, $4, elems);
Reid Spencere7c3c602006-11-30 06:36:44 +00001004 }
1005 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
Reid Spencer319a7302007-01-05 17:20:02 +00001006 uint64_t elems = atoi($2->c_str());
Reid Spencerf2d55322006-12-01 21:52:30 +00001007 $2->insert(0,"< ");
Reid Spencer52402b02007-01-02 05:45:11 +00001008 *$2 += " x " + $4->getNewTy() + " >";
Reid Spencer319a7302007-01-05 17:20:02 +00001009 $$ = TypeInfo::get(*$2, PackedTy, $4, elems);
Reid Spencere7c3c602006-11-30 06:36:44 +00001010 }
1011 | '{' TypeListI '}' { // Structure type?
Reid Spencer52402b02007-01-02 05:45:11 +00001012 std::string newTy("{");
1013 for (unsigned i = 0; i < $2->size(); ++i) {
1014 if (i != 0)
1015 newTy += ", ";
1016 newTy += (*$2)[i]->getNewTy();
1017 }
1018 newTy += "}";
Reid Spencer319a7302007-01-05 17:20:02 +00001019 $$ = TypeInfo::get(newTy, StructTy, $2);
Reid Spencere7c3c602006-11-30 06:36:44 +00001020 }
1021 | '{' '}' { // Empty structure type?
Reid Spencer319a7302007-01-05 17:20:02 +00001022 $$ = TypeInfo::get("{}", StructTy, new TypeList());
Reid Spencere7c3c602006-11-30 06:36:44 +00001023 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001024 | '<' '{' TypeListI '}' '>' { // Packed Structure type?
Reid Spencer52402b02007-01-02 05:45:11 +00001025 std::string newTy("<{");
1026 for (unsigned i = 0; i < $3->size(); ++i) {
1027 if (i != 0)
1028 newTy += ", ";
1029 newTy += (*$3)[i]->getNewTy();
1030 }
1031 newTy += "}>";
Reid Spencer319a7302007-01-05 17:20:02 +00001032 $$ = TypeInfo::get(newTy, PackedStructTy, $3);
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001033 }
1034 | '<' '{' '}' '>' { // Empty packed structure type?
Reid Spencer319a7302007-01-05 17:20:02 +00001035 $$ = TypeInfo::get("<{}>", PackedStructTy, new TypeList());
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001036 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001037 | UpRTypes '*' { // Pointer type?
Reid Spencer52402b02007-01-02 05:45:11 +00001038 $$ = $1->getPointerType();
Reid Spencere7c3c602006-11-30 06:36:44 +00001039 };
1040
1041// TypeList - Used for struct declarations and as a basis for function type
1042// declaration type lists
1043//
Reid Spencere77e35e2006-12-01 20:26:20 +00001044TypeListI
1045 : UpRTypes {
Reid Spencer52402b02007-01-02 05:45:11 +00001046 $$ = new TypeList();
1047 $$->push_back($1);
Reid Spencere77e35e2006-12-01 20:26:20 +00001048 }
1049 | TypeListI ',' UpRTypes {
Reid Spencere7c3c602006-11-30 06:36:44 +00001050 $$ = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001051 $$->push_back($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00001052 };
1053
1054// ArgTypeList - List of types for a function type declaration...
Reid Spencere77e35e2006-12-01 20:26:20 +00001055ArgTypeListI
1056 : TypeListI
Reid Spencere7c3c602006-11-30 06:36:44 +00001057 | TypeListI ',' DOTDOTDOT {
Reid Spencere7c3c602006-11-30 06:36:44 +00001058 $$ = $1;
Reid Spencer319a7302007-01-05 17:20:02 +00001059 $$->push_back(TypeInfo::get("void",VoidTy));
Reid Spencer52402b02007-01-02 05:45:11 +00001060 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001061 }
1062 | DOTDOTDOT {
Reid Spencer52402b02007-01-02 05:45:11 +00001063 $$ = new TypeList();
Reid Spencer319a7302007-01-05 17:20:02 +00001064 $$->push_back(TypeInfo::get("void",VoidTy));
Reid Spencer52402b02007-01-02 05:45:11 +00001065 delete $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001066 }
1067 | /*empty*/ {
Reid Spencer52402b02007-01-02 05:45:11 +00001068 $$ = new TypeList();
Reid Spencere7c3c602006-11-30 06:36:44 +00001069 };
1070
1071// ConstVal - The various declarations that go into the constant pool. This
1072// production is used ONLY to represent constants that show up AFTER a 'const',
1073// 'constant' or 'global' token at global scope. Constants that can be inlined
1074// into other expressions (such as integers and constexprs) are handled by the
1075// ResolvedVal, ValueRef and ConstValueRef productions.
1076//
1077ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencere77e35e2006-12-01 20:26:20 +00001078 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001079 $$.cnst = new std::string($1->getNewTy());
Reid Spencere77e35e2006-12-01 20:26:20 +00001080 *$$.cnst += " [ " + *$3 + " ]";
Reid Spencere7c3c602006-11-30 06:36:44 +00001081 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001082 }
1083 | Types '[' ']' {
Reid Spencere77e35e2006-12-01 20:26:20 +00001084 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001085 $$.cnst = new std::string($1->getNewTy());
Reid Spencere77e35e2006-12-01 20:26:20 +00001086 *$$.cnst += "[ ]";
Reid Spencere7c3c602006-11-30 06:36:44 +00001087 }
1088 | Types 'c' STRINGCONSTANT {
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 Spencere77e35e2006-12-01 20:26:20 +00001091 *$$.cnst += " c" + *$3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001092 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001093 }
1094 | Types '<' ConstVector '>' { // Nonempty unsized arr
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 Spencere77e35e2006-12-01 20:26:20 +00001097 *$$.cnst += " < " + *$3 + " >";
Reid Spencere7c3c602006-11-30 06:36:44 +00001098 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001099 }
1100 | Types '{' ConstVector '}' {
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 Spencere77e35e2006-12-01 20:26:20 +00001103 *$$.cnst += " { " + *$3 + " }";
Reid Spencere7c3c602006-11-30 06:36:44 +00001104 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001105 }
1106 | Types '{' '}' {
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 Spencer0b7e5072006-12-01 22:42:01 +00001109 *$$.cnst += " {}";
Reid Spencere7c3c602006-11-30 06:36:44 +00001110 }
1111 | Types NULL_TOK {
Reid Spencere77e35e2006-12-01 20:26:20 +00001112 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001113 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +00001114 *$$.cnst += " " + *$2;
1115 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001116 }
1117 | Types UNDEF {
Reid Spencere77e35e2006-12-01 20:26:20 +00001118 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001119 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +00001120 *$$.cnst += " " + *$2;
1121 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001122 }
1123 | Types SymbolicValueRef {
Reid Spencer319a7302007-01-05 17:20:02 +00001124 std::string Name = getUniqueName($2, $1->resolve(), true);
Reid Spencere77e35e2006-12-01 20:26:20 +00001125 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001126 $$.cnst = new std::string($1->getNewTy());
1127 *$$.cnst += " " + Name;
Reid Spencere7c3c602006-11-30 06:36:44 +00001128 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001129 }
1130 | Types ConstExpr {
Reid Spencere77e35e2006-12-01 20:26:20 +00001131 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001132 $$.cnst = new std::string($1->getNewTy());
Reid Spencere77e35e2006-12-01 20:26:20 +00001133 *$$.cnst += " " + *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001134 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001135 }
1136 | Types ZEROINITIALIZER {
Reid Spencere77e35e2006-12-01 20:26:20 +00001137 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001138 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +00001139 *$$.cnst += " " + *$2;
1140 delete $2;
Reid Spencere77e35e2006-12-01 20:26:20 +00001141 }
1142 | SIntType EInt64Val { // integral constants
1143 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001144 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +00001145 *$$.cnst += " " + *$2;
1146 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001147 }
Reid Spencer7356ae42007-01-02 06:34:08 +00001148 | UIntType EInt64Val { // integral constants
Reid Spencere77e35e2006-12-01 20:26:20 +00001149 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001150 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +00001151 *$$.cnst += " " + *$2;
1152 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001153 }
1154 | BOOL TRUETOK { // Boolean constants
Reid Spencere77e35e2006-12-01 20:26:20 +00001155 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001156 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +00001157 *$$.cnst += " " + *$2;
1158 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001159 }
1160 | BOOL FALSETOK { // Boolean constants
Reid Spencere77e35e2006-12-01 20:26:20 +00001161 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001162 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +00001163 *$$.cnst += " " + *$2;
1164 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001165 }
1166 | FPType FPVAL { // Float & Double constants
Reid Spencere77e35e2006-12-01 20:26:20 +00001167 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001168 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +00001169 *$$.cnst += " " + *$2;
1170 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001171 };
1172
1173
Reid Spencerfcb5df82006-12-01 22:34:43 +00001174ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer280d8012006-12-01 23:40:53 +00001175 std::string source = *$3.cnst;
Reid Spencer319a7302007-01-05 17:20:02 +00001176 const TypeInfo* SrcTy = $3.type->resolve();
1177 const TypeInfo* DstTy = $5->resolve();
Reid Spencer280d8012006-12-01 23:40:53 +00001178 if (*$1 == "cast") {
Reid Spencera50d5962006-12-02 04:11:07 +00001179 // Call getCastUpgrade to upgrade the old cast
Reid Spencer319a7302007-01-05 17:20:02 +00001180 $$ = new std::string(getCastUpgrade(source, SrcTy, DstTy, true));
Reid Spencera50d5962006-12-02 04:11:07 +00001181 } else {
1182 // Nothing to upgrade, just create the cast constant expr
1183 $$ = new std::string(*$1);
Reid Spencer52402b02007-01-02 05:45:11 +00001184 *$$ += "( " + source + " to " + $5->getNewTy() + ")";
Reid Spencer280d8012006-12-01 23:40:53 +00001185 }
Reid Spencereff838e2007-01-03 23:45:42 +00001186 delete $1; $3.destroy(); delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001187 }
1188 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencerf8483652006-12-02 15:16:01 +00001189 *$1 += "(" + *$3.cnst;
1190 for (unsigned i = 0; i < $4->size(); ++i) {
1191 ValueInfo& VI = (*$4)[i];
1192 *$1 += ", " + *VI.val;
1193 VI.destroy();
1194 }
1195 *$1 += ")";
Reid Spencere77e35e2006-12-01 20:26:20 +00001196 $$ = $1;
1197 $3.destroy();
1198 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001199 }
1200 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +00001201 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
1202 $3.destroy(); $5.destroy(); $7.destroy();
1203 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001204 }
1205 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer78720742006-12-02 20:21:22 +00001206 const char* op = getDivRemOpcode(*$1, $3.type);
1207 $$ = new std::string(op);
1208 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
1209 delete $1; $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001210 }
1211 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +00001212 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
1213 $3.destroy(); $5.destroy();
1214 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001215 }
1216 | SetCondOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer229e9362006-12-02 22:14:11 +00001217 *$1 = getCompareOp(*$1, $3.type);
Reid Spencere77e35e2006-12-01 20:26:20 +00001218 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
1219 $3.destroy(); $5.destroy();
1220 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001221 }
Reid Spencer57f28f92006-12-03 07:10:26 +00001222 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencera7c46fa2007-01-06 00:23:53 +00001223 *$1 += " " + *$2 + " (" + *$4.cnst + "," + *$6.cnst + ")";
Reid Spencer57f28f92006-12-03 07:10:26 +00001224 delete $2; $4.destroy(); $6.destroy();
1225 $$ = $1;
1226 }
1227 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencera7c46fa2007-01-06 00:23:53 +00001228 *$1 += " " + *$2 + " (" + *$4.cnst + "," + *$6.cnst + ")";
Reid Spencer229e9362006-12-02 22:14:11 +00001229 delete $2; $4.destroy(); $6.destroy();
1230 $$ = $1;
1231 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001232 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencerf7bde222006-12-01 22:26:37 +00001233 const char* shiftop = $1->c_str();
1234 if (*$1 == "shr")
Reid Spencer52402b02007-01-02 05:45:11 +00001235 shiftop = ($3.type->isUnsigned()) ? "lshr" : "ashr";
Reid Spencerf7bde222006-12-01 22:26:37 +00001236 $$ = new std::string(shiftop);
1237 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
1238 delete $1; $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001239 }
1240 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +00001241 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
1242 $3.destroy(); $5.destroy();
1243 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001244 }
1245 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +00001246 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
1247 $3.destroy(); $5.destroy(); $7.destroy();
1248 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001249 }
1250 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +00001251 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
1252 $3.destroy(); $5.destroy(); $7.destroy();
1253 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001254 };
1255
1256
1257// ConstVector - A list of comma separated constants.
Reid Spencere77e35e2006-12-01 20:26:20 +00001258
1259ConstVector
1260 : ConstVector ',' ConstVal {
1261 *$1 += ", " + *$3.cnst;
1262 $3.destroy();
1263 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001264 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001265 | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
1266 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001267
1268
1269// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Reid Spencere77e35e2006-12-01 20:26:20 +00001270GlobalType : GLOBAL | CONSTANT ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001271
1272
1273//===----------------------------------------------------------------------===//
1274// Rules to match Modules
1275//===----------------------------------------------------------------------===//
1276
1277// Module rule: Capture the result of parsing the whole file into a result
1278// variable...
1279//
1280Module : DefinitionList {
1281};
1282
1283// DefinitionList - Top level definitions
1284//
1285DefinitionList : DefinitionList Function {
1286 $$ = 0;
1287 }
1288 | DefinitionList FunctionProto {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001289 *O << *$2 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001290 delete $2;
1291 $$ = 0;
1292 }
1293 | DefinitionList MODULE ASM_TOK AsmBlock {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001294 *O << "module asm " << ' ' << *$4 << '\n';
Reid Spencerd154b572006-12-01 20:36:40 +00001295 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001296 }
1297 | DefinitionList IMPLEMENTATION {
1298 *O << "implementation\n";
Reid Spencerd154b572006-12-01 20:36:40 +00001299 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001300 }
Reid Spencera50d5962006-12-02 04:11:07 +00001301 | ConstPool { $$ = 0; }
Reid Spencere7c3c602006-11-30 06:36:44 +00001302
Reid Spencer78720742006-12-02 20:21:22 +00001303External : EXTERNAL | UNINITIALIZED { $$ = $1; *$$ = "external"; }
1304
Reid Spencere7c3c602006-11-30 06:36:44 +00001305// ConstPool - Constants with optional names assigned to them.
1306ConstPool : ConstPool OptAssign TYPE TypesV {
Reid Spencer319a7302007-01-05 17:20:02 +00001307 EnumeratedTypes.push_back($4);
Reid Spencera50d5962006-12-02 04:11:07 +00001308 if (!$2->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001309 NamedTypes[*$2] = $4;
Reid Spencera50d5962006-12-02 04:11:07 +00001310 *O << *$2 << " = ";
1311 }
Reid Spencer52402b02007-01-02 05:45:11 +00001312 *O << "type " << $4->getNewTy() << '\n';
1313 delete $2; delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001314 $$ = 0;
1315 }
1316 | ConstPool FunctionProto { // Function prototypes can be in const pool
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001317 *O << *$2 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001318 delete $2;
1319 $$ = 0;
1320 }
1321 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001322 *O << *$2 << ' ' << *$3 << ' ' << *$4 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001323 delete $2; delete $3; delete $4;
1324 $$ = 0;
1325 }
1326 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +00001327 if (!$2->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001328 std::string Name = getGlobalName($2,*$3, $5.type->getPointerType(),
1329 *$4 == "constant");
Reid Spencer52402b02007-01-02 05:45:11 +00001330 *O << Name << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +00001331 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001332 *O << *$3 << ' ' << *$4 << ' ' << *$5.cnst << ' ' << *$6 << '\n';
Reid Spencer52402b02007-01-02 05:45:11 +00001333 delete $2; delete $3; delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001334 $$ = 0;
1335 }
Reid Spencer78720742006-12-02 20:21:22 +00001336 | ConstPool OptAssign External GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +00001337 if (!$2->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001338 std::string Name = getGlobalName($2,*$3,$5->getPointerType(),
1339 *$4 == "constant");
Reid Spencer52402b02007-01-02 05:45:11 +00001340 *O << Name << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +00001341 }
Reid Spencer52402b02007-01-02 05:45:11 +00001342 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
1343 delete $2; delete $3; delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001344 $$ = 0;
1345 }
Reid Spencer319a7302007-01-05 17:20:02 +00001346 | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +00001347 if (!$2->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001348 std::string Name = getGlobalName($2,*$3,$5->getPointerType(),
1349 *$4 == "constant");
Reid Spencer52402b02007-01-02 05:45:11 +00001350 *O << Name << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +00001351 }
Reid Spencer52402b02007-01-02 05:45:11 +00001352 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
1353 delete $2; delete $3; delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001354 $$ = 0;
1355 }
1356 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +00001357 if (!$2->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001358 std::string Name = getGlobalName($2,*$3,$5->getPointerType(),
1359 *$4 == "constant");
Reid Spencer52402b02007-01-02 05:45:11 +00001360 *O << Name << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +00001361 }
Reid Spencer52402b02007-01-02 05:45:11 +00001362 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
1363 delete $2; delete $3; delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001364 $$ = 0;
1365 }
1366 | ConstPool TARGET TargetDefinition {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001367 *O << *$2 << ' ' << *$3 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001368 delete $2; delete $3;
1369 $$ = 0;
1370 }
1371 | ConstPool DEPLIBS '=' LibrariesDefinition {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001372 *O << *$2 << " = " << *$4 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001373 delete $2; delete $4;
1374 $$ = 0;
1375 }
1376 | /* empty: end of list */ {
1377 $$ = 0;
1378 };
1379
1380
1381AsmBlock : STRINGCONSTANT ;
1382
1383BigOrLittle : BIG | LITTLE
1384
1385TargetDefinition
1386 : ENDIAN '=' BigOrLittle {
Reid Spencere77e35e2006-12-01 20:26:20 +00001387 *$1 += " = " + *$3;
1388 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001389 $$ = $1;
1390 }
1391 | POINTERSIZE '=' EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +00001392 *$1 += " = " + *$3;
1393 if (*$3 == "64")
Reid Spencere77e35e2006-12-01 20:26:20 +00001394 SizeOfPointer = 64;
Reid Spencerf2d55322006-12-01 21:52:30 +00001395 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001396 $$ = $1;
1397 }
1398 | TRIPLE '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +00001399 *$1 += " = " + *$3;
1400 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001401 $$ = $1;
1402 }
1403 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +00001404 *$1 += " = " + *$3;
1405 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001406 $$ = $1;
1407 };
1408
1409LibrariesDefinition
1410 : '[' LibList ']' {
1411 $2->insert(0, "[ ");
1412 *$2 += " ]";
1413 $$ = $2;
1414 };
1415
1416LibList
1417 : LibList ',' STRINGCONSTANT {
1418 *$1 += ", " + *$3;
1419 delete $3;
1420 $$ = $1;
1421 }
1422 | STRINGCONSTANT
1423 | /* empty: end of list */ {
1424 $$ = new std::string();
1425 };
1426
1427//===----------------------------------------------------------------------===//
1428// Rules to match Function Headers
1429//===----------------------------------------------------------------------===//
1430
1431Name : VAR_ID | STRINGCONSTANT;
1432OptName : Name | /*empty*/ { $$ = new std::string(); };
1433
1434ArgVal : Types OptName {
Reid Spencer52402b02007-01-02 05:45:11 +00001435 $$ = new std::string($1->getNewTy());
1436 if (!$2->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001437 std::string Name = getUniqueName($2, $1->resolve());
Reid Spencer52402b02007-01-02 05:45:11 +00001438 *$$ += " " + Name;
1439 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001440 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001441};
1442
1443ArgListH : ArgListH ',' ArgVal {
1444 *$1 += ", " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001445 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001446 }
1447 | ArgVal {
1448 $$ = $1;
1449 };
1450
1451ArgList : ArgListH {
1452 $$ = $1;
1453 }
1454 | ArgListH ',' DOTDOTDOT {
1455 *$1 += ", ...";
1456 $$ = $1;
Reid Spencere77e35e2006-12-01 20:26:20 +00001457 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001458 }
1459 | DOTDOTDOT {
1460 $$ = $1;
1461 }
Reid Spencerd154b572006-12-01 20:36:40 +00001462 | /* empty */ { $$ = new std::string(); };
Reid Spencere7c3c602006-11-30 06:36:44 +00001463
Reid Spencer71d2ec92006-12-31 06:02:26 +00001464FunctionHeaderH
1465 : OptCallingConv TypesV Name '(' ArgList ')' OptSection OptAlign {
Reid Spencere7c3c602006-11-30 06:36:44 +00001466 if (!$1->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +00001467 *$1 += " ";
Reid Spencere7c3c602006-11-30 06:36:44 +00001468 }
Reid Spencer52402b02007-01-02 05:45:11 +00001469 *$1 += $2->getNewTy() + " " + *$3 + "(" + *$5 + ")";
Reid Spencere7c3c602006-11-30 06:36:44 +00001470 if (!$7->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +00001471 *$1 += " " + *$7;
Reid Spencere7c3c602006-11-30 06:36:44 +00001472 }
1473 if (!$8->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +00001474 *$1 += " " + *$8;
Reid Spencere7c3c602006-11-30 06:36:44 +00001475 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001476 delete $3;
1477 delete $5;
1478 delete $7;
1479 delete $8;
1480 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001481 };
1482
Reid Spencer78720742006-12-02 20:21:22 +00001483BEGIN : BEGINTOK { $$ = new std::string("{"); delete $1; }
1484 | '{' { $$ = new std::string ("{"); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001485
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001486FunctionHeader
1487 : OptLinkage FunctionHeaderH BEGIN {
1488 *O << "define ";
1489 if (!$1->empty()) {
1490 *O << *$1 << ' ';
1491 }
1492 *O << *$2 << ' ' << *$3 << '\n';
1493 delete $1; delete $2; delete $3;
1494 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001495 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001496 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001497
Reid Spencer78720742006-12-02 20:21:22 +00001498END : ENDTOK { $$ = new std::string("}"); delete $1; }
Reid Spencere7c3c602006-11-30 06:36:44 +00001499 | '}' { $$ = new std::string("}"); };
1500
1501Function : FunctionHeader BasicBlockList END {
1502 if ($2)
1503 *O << *$2;
Reid Spencer71d2ec92006-12-31 06:02:26 +00001504 *O << *$3 << "\n\n";
Reid Spencer52402b02007-01-02 05:45:11 +00001505 delete $1; delete $2; delete $3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001506 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001507};
1508
Reid Spencere77e35e2006-12-01 20:26:20 +00001509FnDeclareLinkage
1510 : /*default*/ { $$ = new std::string(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001511 | DLLIMPORT
1512 | EXTERN_WEAK
1513 ;
1514
1515FunctionProto
1516 : DECLARE FnDeclareLinkage FunctionHeaderH {
Reid Spencere77e35e2006-12-01 20:26:20 +00001517 if (!$2->empty())
1518 *$1 += " " + *$2;
1519 *$1 += " " + *$3;
1520 delete $2;
1521 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001522 $$ = $1;
1523 };
1524
1525//===----------------------------------------------------------------------===//
1526// Rules to match Basic Blocks
1527//===----------------------------------------------------------------------===//
1528
Reid Spencerd154b572006-12-01 20:36:40 +00001529OptSideEffect : /* empty */ { $$ = new std::string(); }
1530 | SIDEEFFECT;
Reid Spencere7c3c602006-11-30 06:36:44 +00001531
Reid Spencere77e35e2006-12-01 20:26:20 +00001532ConstValueRef
Reid Spencerf2d55322006-12-01 21:52:30 +00001533 : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK | NULL_TOK | UNDEF
1534 | ZEROINITIALIZER
Reid Spencere7c3c602006-11-30 06:36:44 +00001535 | '<' ConstVector '>' {
1536 $2->insert(0, "<");
1537 *$2 += ">";
1538 $$ = $2;
1539 }
1540 | ConstExpr
1541 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
1542 if (!$2->empty()) {
1543 *$1 += " " + *$2;
1544 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001545 *$1 += " " + *$3 + ", " + *$5;
1546 delete $2; delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001547 $$ = $1;
1548 };
1549
Reid Spencerf2d55322006-12-01 21:52:30 +00001550SymbolicValueRef : IntVal | Name ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001551
1552// ValueRef - A reference to a definition... either constant or symbolic
Reid Spencerf459d392006-12-02 16:19:52 +00001553ValueRef
1554 : SymbolicValueRef {
1555 $$.val = $1;
1556 $$.constant = false;
Reid Spencer319a7302007-01-05 17:20:02 +00001557 $$.type = 0;
Reid Spencerf459d392006-12-02 16:19:52 +00001558 }
1559 | ConstValueRef {
1560 $$.val = $1;
1561 $$.constant = true;
Reid Spencer319a7302007-01-05 17:20:02 +00001562 $$.type = 0;
Reid Spencerf459d392006-12-02 16:19:52 +00001563 }
1564 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001565
1566// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1567// type immediately preceeds the value reference, and allows complex constant
1568// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1569ResolvedVal : Types ValueRef {
Reid Spencer319a7302007-01-05 17:20:02 +00001570 $1 = $1->resolve();
Reid Spencer52402b02007-01-02 05:45:11 +00001571 std::string Name = getUniqueName($2.val, $1);
Reid Spencerf459d392006-12-02 16:19:52 +00001572 $$ = $2;
Reid Spencer52402b02007-01-02 05:45:11 +00001573 delete $$.val;
Reid Spencer52402b02007-01-02 05:45:11 +00001574 $$.val = new std::string($1->getNewTy() + " " + Name);
Reid Spencere77e35e2006-12-01 20:26:20 +00001575 $$.type = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001576 };
1577
1578BasicBlockList : BasicBlockList BasicBlock {
Reid Spencerf2d55322006-12-01 21:52:30 +00001579 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001580 }
1581 | BasicBlock { // Do not allow functions with 0 basic blocks
Reid Spencerf2d55322006-12-01 21:52:30 +00001582 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001583 };
1584
1585
1586// Basic blocks are terminated by branching instructions:
1587// br, br/cc, switch, ret
1588//
Reid Spencer16244f42006-12-01 21:10:07 +00001589BasicBlock : InstructionList BBTerminatorInst {
Reid Spencerf2d55322006-12-01 21:52:30 +00001590 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001591 };
1592
1593InstructionList : InstructionList Inst {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001594 *O << " " << *$2 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001595 delete $2;
1596 $$ = 0;
1597 }
1598 | /* empty */ {
1599 $$ = 0;
1600 }
1601 | LABELSTR {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001602 *O << *$1 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001603 delete $1;
1604 $$ = 0;
1605 };
1606
Reid Spencer78720742006-12-02 20:21:22 +00001607Unwind : UNWIND | EXCEPT { $$ = $1; *$$ = "unwind"; }
1608
Reid Spencere7c3c602006-11-30 06:36:44 +00001609BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001610 *O << " " << *$1 << ' ' << *$2.val << '\n';
Reid Spencere77e35e2006-12-01 20:26:20 +00001611 delete $1; $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001612 $$ = 0;
1613 }
1614 | RET VOID { // Return with no result...
Reid Spencer52402b02007-01-02 05:45:11 +00001615 *O << " " << *$1 << ' ' << $2->getNewTy() << '\n';
Reid Spencer319a7302007-01-05 17:20:02 +00001616 delete $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001617 $$ = 0;
1618 }
1619 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer52402b02007-01-02 05:45:11 +00001620 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << *$3.val << '\n';
Reid Spencer319a7302007-01-05 17:20:02 +00001621 delete $1; $3.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001622 $$ = 0;
1623 } // Conditional Branch...
1624 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001625 std::string Name = getUniqueName($3.val, $2);
1626 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", "
1627 << $5->getNewTy() << ' ' << *$6.val << ", " << $8->getNewTy() << ' '
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001628 << *$9.val << '\n';
Reid Spencer319a7302007-01-05 17:20:02 +00001629 delete $1; $3.destroy(); $6.destroy(); $9.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001630 $$ = 0;
1631 }
1632 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencer52402b02007-01-02 05:45:11 +00001633 std::string Name = getUniqueName($3.val, $2);
1634 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", "
1635 << $5->getNewTy() << ' ' << *$6.val << " [" << *$8 << " ]\n";
Reid Spencer319a7302007-01-05 17:20:02 +00001636 delete $1; $3.destroy(); $6.destroy();
Reid Spencerf459d392006-12-02 16:19:52 +00001637 delete $8;
Reid Spencere7c3c602006-11-30 06:36:44 +00001638 $$ = 0;
1639 }
1640 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencer52402b02007-01-02 05:45:11 +00001641 std::string Name = getUniqueName($3.val, $2);
1642 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", "
1643 << $5->getNewTy() << ' ' << *$6.val << "[]\n";
Reid Spencer319a7302007-01-05 17:20:02 +00001644 delete $1; $3.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001645 $$ = 0;
1646 }
Reid Spencer16244f42006-12-01 21:10:07 +00001647 | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
Reid Spencer78720742006-12-02 20:21:22 +00001648 TO LABEL ValueRef Unwind LABEL ValueRef {
Reid Spencer319a7302007-01-05 17:20:02 +00001649 const TypeInfo* ResTy = getFunctionReturnType($4);
Reid Spencer16244f42006-12-01 21:10:07 +00001650 *O << " ";
Reid Spencer52402b02007-01-02 05:45:11 +00001651 if (!$1->empty()) {
1652 std::string Name = getUniqueName($1, ResTy);
1653 *O << Name << " = ";
1654 }
1655 *O << *$2 << ' ' << *$3 << ' ' << $4->getNewTy() << ' ' << *$5.val << " (";
Reid Spencerf8483652006-12-02 15:16:01 +00001656 for (unsigned i = 0; i < $7->size(); ++i) {
1657 ValueInfo& VI = (*$7)[i];
1658 *O << *VI.val;
1659 if (i+1 < $7->size())
1660 *O << ", ";
1661 VI.destroy();
1662 }
Reid Spencer52402b02007-01-02 05:45:11 +00001663 *O << ") " << *$9 << ' ' << $10->getNewTy() << ' ' << *$11.val << ' '
1664 << *$12 << ' ' << $13->getNewTy() << ' ' << *$14.val << '\n';
Reid Spencer319a7302007-01-05 17:20:02 +00001665 delete $1; delete $2; delete $3; $5.destroy(); delete $7;
1666 delete $9; $11.destroy(); delete $12; $14.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001667 $$ = 0;
1668 }
Reid Spencer78720742006-12-02 20:21:22 +00001669 | Unwind {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001670 *O << " " << *$1 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001671 delete $1;
1672 $$ = 0;
1673 }
1674 | UNREACHABLE {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001675 *O << " " << *$1 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001676 delete $1;
1677 $$ = 0;
1678 };
1679
1680JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001681 *$1 += " " + $2->getNewTy() + " " + *$3 + ", " + $5->getNewTy() + " " +
1682 *$6.val;
Reid Spencer319a7302007-01-05 17:20:02 +00001683 delete $3; $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001684 $$ = $1;
1685 }
1686 | IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001687 $2->insert(0, $1->getNewTy() + " " );
1688 *$2 += ", " + $4->getNewTy() + " " + *$5.val;
Reid Spencer319a7302007-01-05 17:20:02 +00001689 $5.destroy();
Reid Spencere77e35e2006-12-01 20:26:20 +00001690 $$ = $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001691 };
1692
1693Inst
1694 : OptAssign InstVal {
Reid Spencerf5626a32007-01-01 01:20:41 +00001695 if (!$1->empty()) {
Reid Spencer319a7302007-01-05 17:20:02 +00001696 // Get a unique name for this value, based on its type.
1697 std::string Name = getUniqueName($1, $2.type);
1698 *$1 = Name + " = ";
1699 if (deleteUselessCastFlag && *deleteUselessCastName == Name) {
1700 // don't actually delete it, just comment it out
1701 $1->insert(0, "; USELSS BITCAST: ");
Reid Spencerf5626a32007-01-01 01:20:41 +00001702 delete deleteUselessCastName;
Reid Spencerf5626a32007-01-01 01:20:41 +00001703 }
1704 }
Reid Spencer52402b02007-01-02 05:45:11 +00001705 *$1 += *$2.val;
1706 $2.destroy();
Reid Spencerf5626a32007-01-01 01:20:41 +00001707 deleteUselessCastFlag = false;
Reid Spencere7c3c602006-11-30 06:36:44 +00001708 $$ = $1;
1709 };
1710
1711PHIList
1712 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer52402b02007-01-02 05:45:11 +00001713 std::string Name = getUniqueName($3.val, $1);
1714 Name.insert(0, $1->getNewTy() + "[");
1715 Name += "," + *$5.val + "]";
1716 $$.val = new std::string(Name);
1717 $$.type = $1;
1718 $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001719 }
1720 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
Reid Spencer52402b02007-01-02 05:45:11 +00001721 std::string Name = getUniqueName($4.val, $1.type);
1722 *$1.val += ", [" + Name + "," + *$6.val + "]";
Reid Spencerf459d392006-12-02 16:19:52 +00001723 $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001724 $$ = $1;
1725 };
1726
1727
1728ValueRefList
Reid Spencer52402b02007-01-02 05:45:11 +00001729 : ResolvedVal {
Reid Spencerf8483652006-12-02 15:16:01 +00001730 $$ = new ValueList();
1731 $$->push_back($1);
1732 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001733 | ValueRefList ',' ResolvedVal {
Reid Spencere7c3c602006-11-30 06:36:44 +00001734 $$ = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001735 $$->push_back($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00001736 };
1737
1738// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1739ValueRefListE
Reid Spencerf8483652006-12-02 15:16:01 +00001740 : ValueRefList { $$ = $1; }
1741 | /*empty*/ { $$ = new ValueList(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001742 ;
1743
1744OptTailCall
1745 : TAIL CALL {
1746 *$1 += " " + *$2;
1747 delete $2;
1748 $$ = $1;
1749 }
1750 | CALL
1751 ;
1752
1753InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer78720742006-12-02 20:21:22 +00001754 const char* op = getDivRemOpcode(*$1, $2);
Reid Spencer52402b02007-01-02 05:45:11 +00001755 std::string Name1 = getUniqueName($3.val, $2);
1756 std::string Name2 = getUniqueName($5.val, $2);
1757 $$.val = new std::string(op);
1758 *$$.val += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
1759 $$.type = $2;
1760 delete $1; $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001761 }
1762 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001763 std::string Name1 = getUniqueName($3.val, $2);
1764 std::string Name2 = getUniqueName($5.val, $2);
1765 *$1 += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
1766 $$.val = $1;
1767 $$.type = $2;
1768 $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001769 }
1770 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001771 std::string Name1 = getUniqueName($3.val, $2);
1772 std::string Name2 = getUniqueName($5.val, $2);
Reid Spencer229e9362006-12-02 22:14:11 +00001773 *$1 = getCompareOp(*$1, $2);
Reid Spencer52402b02007-01-02 05:45:11 +00001774 *$1 += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
1775 $$.val = $1;
Reid Spencer319a7302007-01-05 17:20:02 +00001776 $$.type = TypeInfo::get("bool",BoolTy);
Reid Spencer52402b02007-01-02 05:45:11 +00001777 $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001778 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001779 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001780 std::string Name1 = getUniqueName($4.val, $3);
1781 std::string Name2 = getUniqueName($6.val, $3);
1782 *$1 += " " + *$2 + " " + $3->getNewTy() + " " + Name1 + "," + Name2;
1783 $$.val = $1;
Reid Spencer319a7302007-01-05 17:20:02 +00001784 $$.type = TypeInfo::get("bool",BoolTy);
Reid Spencer57f28f92006-12-03 07:10:26 +00001785 delete $2; $4.destroy(); $6.destroy();
Reid Spencer57f28f92006-12-03 07:10:26 +00001786 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001787 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001788 std::string Name1 = getUniqueName($4.val, $3);
1789 std::string Name2 = getUniqueName($6.val, $3);
1790 *$1 += " " + *$2 + " " + $3->getNewTy() + " " + Name1 + "," + Name2;
1791 $$.val = $1;
Reid Spencer319a7302007-01-05 17:20:02 +00001792 $$.type = TypeInfo::get("bool",BoolTy);
Reid Spencer229e9362006-12-02 22:14:11 +00001793 delete $2; $4.destroy(); $6.destroy();
Reid Spencer229e9362006-12-02 22:14:11 +00001794 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001795 | NOT ResolvedVal {
Reid Spencer52402b02007-01-02 05:45:11 +00001796 $$ = $2;
1797 $$.val->insert(0, *$1 + " ");
1798 delete $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001799 }
1800 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencerf7bde222006-12-01 22:26:37 +00001801 const char* shiftop = $1->c_str();
1802 if (*$1 == "shr")
Reid Spencer52402b02007-01-02 05:45:11 +00001803 shiftop = ($2.type->isUnsigned()) ? "lshr" : "ashr";
1804 $$.val = new std::string(shiftop);
1805 *$$.val += " " + *$2.val + ", " + *$4.val;
1806 $$.type = $2.type;
Reid Spencerf8383de2007-01-06 06:04:32 +00001807 delete $1; $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001808 }
Reid Spencerfcb5df82006-12-01 22:34:43 +00001809 | CastOps ResolvedVal TO Types {
Reid Spencer280d8012006-12-01 23:40:53 +00001810 std::string source = *$2.val;
Reid Spencer319a7302007-01-05 17:20:02 +00001811 const TypeInfo* SrcTy = $2.type->resolve();
1812 const TypeInfo* DstTy = $4->resolve();
Reid Spencer52402b02007-01-02 05:45:11 +00001813 $$.val = new std::string();
Reid Spencer319a7302007-01-05 17:20:02 +00001814 $$.type = DstTy;
Reid Spencer280d8012006-12-01 23:40:53 +00001815 if (*$1 == "cast") {
Reid Spencer319a7302007-01-05 17:20:02 +00001816 *$$.val += getCastUpgrade(source, SrcTy, DstTy, false);
Reid Spencera50d5962006-12-02 04:11:07 +00001817 } else {
Reid Spencer52402b02007-01-02 05:45:11 +00001818 *$$.val += *$1 + " " + source + " to " + DstTy->getNewTy();
Reid Spencer280d8012006-12-01 23:40:53 +00001819 }
Reid Spencerf5626a32007-01-01 01:20:41 +00001820 // Check to see if this is a useless cast of a value to the same name
1821 // and the same type. Such casts will probably cause redefinition errors
1822 // when assembled and perform no code gen action so just remove them.
1823 if (*$1 == "cast" || *$1 == "bitcast")
Reid Spencer319a7302007-01-05 17:20:02 +00001824 if (SrcTy->isInteger() && DstTy->isInteger() &&
1825 SrcTy->getBitWidth() == DstTy->getBitWidth()) {
Reid Spencerf5626a32007-01-01 01:20:41 +00001826 deleteUselessCastFlag = true; // Flag the "Inst" rule
1827 deleteUselessCastName = new std::string(*$2.val); // save the name
1828 size_t pos = deleteUselessCastName->find_first_of("%\"",0);
1829 if (pos != std::string::npos) {
1830 // remove the type portion before val
1831 deleteUselessCastName->erase(0, pos);
1832 }
1833 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001834 delete $1; $2.destroy();
Reid Spencer52402b02007-01-02 05:45:11 +00001835 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001836 }
1837 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001838 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
Reid Spencer52402b02007-01-02 05:45:11 +00001839 $$.val = $1;
1840 $$.type = $4.type;
Reid Spencerf8383de2007-01-06 06:04:32 +00001841 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001842 }
1843 | VAARG ResolvedVal ',' Types {
Reid Spencer52402b02007-01-02 05:45:11 +00001844 *$1 += " " + *$2.val + ", " + $4->getNewTy();
1845 $$.val = $1;
1846 $$.type = $4;
1847 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001848 }
1849 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001850 *$1 += " " + *$2.val + ", " + *$4.val;
Reid Spencer52402b02007-01-02 05:45:11 +00001851 $$.val = $1;
Reid Spencer319a7302007-01-05 17:20:02 +00001852 $2.type = $2.type->resolve();;
Reid Spencereff838e2007-01-03 23:45:42 +00001853 $$.type = $2.type->getElementType();
Reid Spencerf8383de2007-01-06 06:04:32 +00001854 $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001855 }
1856 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001857 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
Reid Spencer52402b02007-01-02 05:45:11 +00001858 $$.val = $1;
1859 $$.type = $2.type;
Reid Spencerf8383de2007-01-06 06:04:32 +00001860 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001861 }
1862 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001863 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
Reid Spencer52402b02007-01-02 05:45:11 +00001864 $$.val = $1;
1865 $$.type = $2.type;
Reid Spencerf8383de2007-01-06 06:04:32 +00001866 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001867 }
1868 | PHI_TOK PHIList {
Reid Spencer52402b02007-01-02 05:45:11 +00001869 *$1 += " " + *$2.val;
1870 $$.val = $1;
1871 $$.type = $2.type;
1872 delete $2.val;
Reid Spencere7c3c602006-11-30 06:36:44 +00001873 }
1874 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
1875 if (!$2->empty())
1876 *$1 += " " + *$2;
1877 if (!$1->empty())
1878 *$1 += " ";
Reid Spencer52402b02007-01-02 05:45:11 +00001879 *$1 += $3->getNewTy() + " " + *$4.val + "(";
Reid Spencerf8483652006-12-02 15:16:01 +00001880 for (unsigned i = 0; i < $6->size(); ++i) {
1881 ValueInfo& VI = (*$6)[i];
1882 *$1 += *VI.val;
1883 if (i+1 < $6->size())
1884 *$1 += ", ";
1885 VI.destroy();
1886 }
1887 *$1 += ")";
Reid Spencer52402b02007-01-02 05:45:11 +00001888 $$.val = $1;
1889 $$.type = getFunctionReturnType($3);
Reid Spencer319a7302007-01-05 17:20:02 +00001890 delete $2; $4.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001891 }
1892 | MemoryInst ;
1893
1894
1895// IndexList - List of indices for GEP based instructions...
1896IndexList
Reid Spencerf8483652006-12-02 15:16:01 +00001897 : ',' ValueRefList { $$ = $2; }
1898 | /* empty */ { $$ = new ValueList(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001899 ;
1900
1901OptVolatile
1902 : VOLATILE
1903 | /* empty */ { $$ = new std::string(); }
1904 ;
1905
1906MemoryInst : MALLOC Types OptCAlign {
Reid Spencer52402b02007-01-02 05:45:11 +00001907 *$1 += " " + $2->getNewTy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001908 if (!$3->empty())
1909 *$1 += " " + *$3;
Reid Spencer52402b02007-01-02 05:45:11 +00001910 $$.val = $1;
1911 $$.type = $2->getPointerType();
Reid Spencer319a7302007-01-05 17:20:02 +00001912 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001913 }
1914 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencer52402b02007-01-02 05:45:11 +00001915 std::string Name = getUniqueName($5.val, $4);
1916 *$1 += " " + $2->getNewTy() + ", " + $4->getNewTy() + " " + Name;
Reid Spencere7c3c602006-11-30 06:36:44 +00001917 if (!$6->empty())
1918 *$1 += " " + *$6;
Reid Spencer52402b02007-01-02 05:45:11 +00001919 $$.val = $1;
1920 $$.type = $2->getPointerType();
Reid Spencer319a7302007-01-05 17:20:02 +00001921 $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001922 }
1923 | ALLOCA Types OptCAlign {
Reid Spencer52402b02007-01-02 05:45:11 +00001924 *$1 += " " + $2->getNewTy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001925 if (!$3->empty())
1926 *$1 += " " + *$3;
Reid Spencer52402b02007-01-02 05:45:11 +00001927 $$.val = $1;
1928 $$.type = $2->getPointerType();
Reid Spencer319a7302007-01-05 17:20:02 +00001929 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001930 }
1931 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencer52402b02007-01-02 05:45:11 +00001932 std::string Name = getUniqueName($5.val, $4);
1933 *$1 += " " + $2->getNewTy() + ", " + $4->getNewTy() + " " + Name;
Reid Spencere7c3c602006-11-30 06:36:44 +00001934 if (!$6->empty())
1935 *$1 += " " + *$6;
Reid Spencer52402b02007-01-02 05:45:11 +00001936 $$.val = $1;
1937 $$.type = $2->getPointerType();
Reid Spencer319a7302007-01-05 17:20:02 +00001938 $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001939 }
1940 | FREE ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001941 *$1 += " " + *$2.val;
Reid Spencer52402b02007-01-02 05:45:11 +00001942 $$.val = $1;
Reid Spencer319a7302007-01-05 17:20:02 +00001943 $$.type = TypeInfo::get("void", VoidTy);
Reid Spencere77e35e2006-12-01 20:26:20 +00001944 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001945 }
1946 | OptVolatile LOAD Types ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001947 std::string Name = getUniqueName($4.val, $3);
Reid Spencere7c3c602006-11-30 06:36:44 +00001948 if (!$1->empty())
1949 *$1 += " ";
Reid Spencer52402b02007-01-02 05:45:11 +00001950 *$1 += *$2 + " " + $3->getNewTy() + " " + Name;
1951 $$.val = $1;
Reid Spencer319a7302007-01-05 17:20:02 +00001952 $$.type = $3->getElementType();
1953 delete $2; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001954 }
1955 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001956 std::string Name = getUniqueName($6.val, $5);
Reid Spencere7c3c602006-11-30 06:36:44 +00001957 if (!$1->empty())
1958 *$1 += " ";
Reid Spencer52402b02007-01-02 05:45:11 +00001959 *$1 += *$2 + " " + *$3.val + ", " + $5->getNewTy() + " " + Name;
1960 $$.val = $1;
Reid Spencer319a7302007-01-05 17:20:02 +00001961 $$.type = TypeInfo::get("void", VoidTy);
1962 delete $2; $3.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001963 }
1964 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer52402b02007-01-02 05:45:11 +00001965 std::string Name = getUniqueName($3.val, $2);
Reid Spencerf459d392006-12-02 16:19:52 +00001966 // Upgrade the indices
1967 for (unsigned i = 0; i < $4->size(); ++i) {
1968 ValueInfo& VI = (*$4)[i];
Reid Spencer52402b02007-01-02 05:45:11 +00001969 if (VI.type->isUnsigned() && !VI.isConstant() &&
1970 VI.type->getBitWidth() < 64) {
Reid Spencerf8383de2007-01-06 06:04:32 +00001971 *O << " %gep_upgrade" << unique << " = zext " << *VI.val
Reid Spencer71d2ec92006-12-31 06:02:26 +00001972 << " to i64\n";
Reid Spencerf8383de2007-01-06 06:04:32 +00001973 *VI.val = "i64 %gep_upgrade" + llvm::utostr(unique++);
Reid Spencer319a7302007-01-05 17:20:02 +00001974 VI.type = TypeInfo::get("i64",ULongTy);
Reid Spencerf459d392006-12-02 16:19:52 +00001975 }
1976 }
Reid Spencer52402b02007-01-02 05:45:11 +00001977 *$1 += " " + $2->getNewTy() + " " + Name;
Reid Spencerf8483652006-12-02 15:16:01 +00001978 for (unsigned i = 0; i < $4->size(); ++i) {
1979 ValueInfo& VI = (*$4)[i];
1980 *$1 += ", " + *VI.val;
Reid Spencerf8483652006-12-02 15:16:01 +00001981 }
Reid Spencer52402b02007-01-02 05:45:11 +00001982 $$.val = $1;
1983 $$.type = getGEPIndexedType($2,$4);
1984 $3.destroy(); delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001985 };
1986
1987%%
1988
1989int yyerror(const char *ErrorMsg) {
1990 std::string where
1991 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1992 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
Reid Spencer319a7302007-01-05 17:20:02 +00001993 std::string errMsg = where + "error: " + std::string(ErrorMsg) +
1994 " while reading ";
Reid Spencere7c3c602006-11-30 06:36:44 +00001995 if (yychar == YYEMPTY || yychar == 0)
1996 errMsg += "end-of-file.";
1997 else
1998 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
Reid Spencer71d2ec92006-12-31 06:02:26 +00001999 std::cerr << "llvm-upgrade: " << errMsg << '\n';
Chris Lattner37e01c52007-01-04 18:46:42 +00002000 *O << "llvm-upgrade parse failed.\n";
Reid Spencere7c3c602006-11-30 06:36:44 +00002001 exit(1);
2002}
Reid Spencer319a7302007-01-05 17:20:02 +00002003
2004static void warning(const std::string& ErrorMsg) {
2005 std::string where
2006 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2007 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
2008 std::string errMsg = where + "warning: " + std::string(ErrorMsg) +
2009 " while reading ";
2010 if (yychar == YYEMPTY || yychar == 0)
2011 errMsg += "end-of-file.";
2012 else
2013 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
2014 std::cerr << "llvm-upgrade: " << errMsg << '\n';
2015}