blob: 9b2f09c196dc67ea0eabf040204774382da15294 [file] [log] [blame]
Reid Spencer950bf602007-01-26 08:19:09 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
Reid Spencere7c3c602006-11-30 06:36:44 +00002//
3// The LLVM Compiler Infrastructure
4//
Reid Spencer950bf602007-01-26 08:19:09 +00005// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Reid Spencere7c3c602006-11-30 06:36:44 +00007//
8//===----------------------------------------------------------------------===//
9//
Reid Spencer950bf602007-01-26 08:19:09 +000010// This file implements the bison parser for LLVM assembly languages files.
Reid Spencere7c3c602006-11-30 06:36:44 +000011//
12//===----------------------------------------------------------------------===//
13
14%{
Reid Spencer319a7302007-01-05 17:20:02 +000015#include "UpgradeInternals.h"
Reid Spencer950bf602007-01-26 08:19:09 +000016#include "llvm/CallingConv.h"
17#include "llvm/InlineAsm.h"
18#include "llvm/Instructions.h"
19#include "llvm/Module.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000020#include "llvm/ValueSymbolTable.h"
Reid Spencer950bf602007-01-26 08:19:09 +000021#include "llvm/Support/GetElementPtrTypeIterator.h"
22#include "llvm/ADT/STLExtras.h"
23#include "llvm/Support/MathExtras.h"
Reid Spencere7c3c602006-11-30 06:36:44 +000024#include <algorithm>
Reid Spencere7c3c602006-11-30 06:36:44 +000025#include <iostream>
Chris Lattner8adde282007-02-11 21:40:10 +000026#include <map>
Reid Spencer950bf602007-01-26 08:19:09 +000027#include <list>
28#include <utility>
29
30// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
31// relating to upreferences in the input stream.
32//
33//#define DEBUG_UPREFS 1
34#ifdef DEBUG_UPREFS
35#define UR_OUT(X) std::cerr << X
36#else
37#define UR_OUT(X)
38#endif
Reid Spencere7c3c602006-11-30 06:36:44 +000039
Reid Spencere77e35e2006-12-01 20:26:20 +000040#define YYERROR_VERBOSE 1
Reid Spencer96839be2006-11-30 16:50:26 +000041#define YYINCLUDED_STDLIB_H
Reid Spencere77e35e2006-12-01 20:26:20 +000042#define YYDEBUG 1
Reid Spencere7c3c602006-11-30 06:36:44 +000043
Reid Spencer950bf602007-01-26 08:19:09 +000044int yylex();
Reid Spencere7c3c602006-11-30 06:36:44 +000045int yyparse();
46
Reid Spencer950bf602007-01-26 08:19:09 +000047int yyerror(const char*);
48static void warning(const std::string& WarningMsg);
49
50namespace llvm {
51
Reid Spencer950bf602007-01-26 08:19:09 +000052std::istream* LexInput;
Reid Spencere7c3c602006-11-30 06:36:44 +000053static std::string CurFilename;
Reid Spencer96839be2006-11-30 16:50:26 +000054
Reid Spencer71d2ec92006-12-31 06:02:26 +000055// This bool controls whether attributes are ever added to function declarations
56// definitions and calls.
57static bool AddAttributes = false;
58
Reid Spencer950bf602007-01-26 08:19:09 +000059static Module *ParserResult;
60static bool ObsoleteVarArgs;
61static bool NewVarArgs;
62static BasicBlock *CurBB;
63static GlobalVariable *CurGV;
Reid Spencera50d5962006-12-02 04:11:07 +000064
Reid Spencer950bf602007-01-26 08:19:09 +000065// This contains info used when building the body of a function. It is
66// destroyed when the function is completed.
67//
68typedef std::vector<Value *> ValueList; // Numbered defs
69
70typedef std::pair<std::string,const Type*> RenameMapKey;
71typedef std::map<RenameMapKey,std::string> RenameMapType;
72
73static void
74ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
75 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
76
77static struct PerModuleInfo {
78 Module *CurrentModule;
79 std::map<const Type *, ValueList> Values; // Module level numbered definitions
80 std::map<const Type *,ValueList> LateResolveValues;
81 std::vector<PATypeHolder> Types;
82 std::map<ValID, PATypeHolder> LateResolveTypes;
83 static Module::Endianness Endian;
84 static Module::PointerSize PointerSize;
85 RenameMapType RenameMap;
86
87 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
88 /// how they were referenced and on which line of the input they came from so
89 /// that we can resolve them later and print error messages as appropriate.
90 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
91
92 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
93 // references to global values. Global values may be referenced before they
94 // are defined, and if so, the temporary object that they represent is held
95 // here. This is used for forward references of GlobalValues.
96 //
97 typedef std::map<std::pair<const PointerType *, ValID>, GlobalValue*>
98 GlobalRefsType;
99 GlobalRefsType GlobalRefs;
100
101 void ModuleDone() {
102 // If we could not resolve some functions at function compilation time
103 // (calls to functions before they are defined), resolve them now... Types
104 // are resolved when the constant pool has been completely parsed.
105 //
106 ResolveDefinitions(LateResolveValues);
107
108 // Check to make sure that all global value forward references have been
109 // resolved!
110 //
111 if (!GlobalRefs.empty()) {
112 std::string UndefinedReferences = "Unresolved global references exist:\n";
113
114 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
115 I != E; ++I) {
116 UndefinedReferences += " " + I->first.first->getDescription() + " " +
117 I->first.second.getName() + "\n";
118 }
119 error(UndefinedReferences);
120 return;
121 }
122
123 if (CurrentModule->getDataLayout().empty()) {
124 std::string dataLayout;
125 if (Endian != Module::AnyEndianness)
126 dataLayout.append(Endian == Module::BigEndian ? "E" : "e");
127 if (PointerSize != Module::AnyPointerSize) {
128 if (!dataLayout.empty())
129 dataLayout += "-";
130 dataLayout.append(PointerSize == Module::Pointer64 ?
131 "p:64:64" : "p:32:32");
132 }
133 CurrentModule->setDataLayout(dataLayout);
134 }
135
136 Values.clear(); // Clear out function local definitions
137 Types.clear();
138 CurrentModule = 0;
139 }
140
141 // GetForwardRefForGlobal - Check to see if there is a forward reference
142 // for this global. If so, remove it from the GlobalRefs map and return it.
143 // If not, just return null.
144 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
145 // Check to see if there is a forward reference to this global variable...
146 // if there is, eliminate it and patch the reference to use the new def'n.
147 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
148 GlobalValue *Ret = 0;
149 if (I != GlobalRefs.end()) {
150 Ret = I->second;
151 GlobalRefs.erase(I);
152 }
153 return Ret;
154 }
155 void setEndianness(Module::Endianness E) { Endian = E; }
156 void setPointerSize(Module::PointerSize sz) { PointerSize = sz; }
157} CurModule;
158
159Module::Endianness PerModuleInfo::Endian = Module::AnyEndianness;
160Module::PointerSize PerModuleInfo::PointerSize = Module::AnyPointerSize;
161
162static struct PerFunctionInfo {
163 Function *CurrentFunction; // Pointer to current function being created
164
165 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
166 std::map<const Type*, ValueList> LateResolveValues;
167 bool isDeclare; // Is this function a forward declararation?
168 GlobalValue::LinkageTypes Linkage;// Linkage for forward declaration.
169
170 /// BBForwardRefs - When we see forward references to basic blocks, keep
171 /// track of them here.
172 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
173 std::vector<BasicBlock*> NumberedBlocks;
174 RenameMapType RenameMap;
Reid Spencer950bf602007-01-26 08:19:09 +0000175 unsigned NextBBNum;
176
177 inline PerFunctionInfo() {
178 CurrentFunction = 0;
179 isDeclare = false;
180 Linkage = GlobalValue::ExternalLinkage;
181 }
182
183 inline void FunctionStart(Function *M) {
184 CurrentFunction = M;
185 NextBBNum = 0;
186 }
187
188 void FunctionDone() {
189 NumberedBlocks.clear();
190
191 // Any forward referenced blocks left?
192 if (!BBForwardRefs.empty()) {
193 error("Undefined reference to label " +
194 BBForwardRefs.begin()->first->getName());
195 return;
196 }
197
198 // Resolve all forward references now.
199 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
200
201 Values.clear(); // Clear out function local definitions
202 RenameMap.clear();
Reid Spencer950bf602007-01-26 08:19:09 +0000203 CurrentFunction = 0;
204 isDeclare = false;
205 Linkage = GlobalValue::ExternalLinkage;
206 }
207} CurFun; // Info for the current function...
208
209static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
210
211
212//===----------------------------------------------------------------------===//
213// Code to handle definitions of all the types
214//===----------------------------------------------------------------------===//
215
216static int InsertValue(Value *V,
217 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
218 if (V->hasName()) return -1; // Is this a numbered definition?
219
220 // Yes, insert the value into the value table...
221 ValueList &List = ValueTab[V->getType()];
222 List.push_back(V);
223 return List.size()-1;
224}
225
Reid Spencerd7c4f8c2007-01-26 19:59:25 +0000226static const Type *getType(const ValID &D, bool DoNotImprovise = false) {
Reid Spencer950bf602007-01-26 08:19:09 +0000227 switch (D.Type) {
228 case ValID::NumberVal: // Is it a numbered definition?
229 // Module constants occupy the lowest numbered slots...
230 if ((unsigned)D.Num < CurModule.Types.size()) {
231 return CurModule.Types[(unsigned)D.Num];
232 }
233 break;
234 case ValID::NameVal: // Is it a named definition?
235 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
236 D.destroy(); // Free old strdup'd memory...
237 return N;
238 }
239 break;
240 default:
241 error("Internal parser error: Invalid symbol type reference");
242 return 0;
243 }
244
245 // If we reached here, we referenced either a symbol that we don't know about
246 // or an id number that hasn't been read yet. We may be referencing something
247 // forward, so just create an entry to be resolved later and get to it...
248 //
249 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
250
251
252 if (inFunctionScope()) {
253 if (D.Type == ValID::NameVal) {
254 error("Reference to an undefined type: '" + D.getName() + "'");
255 return 0;
256 } else {
257 error("Reference to an undefined type: #" + itostr(D.Num));
258 return 0;
259 }
260 }
261
262 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
263 if (I != CurModule.LateResolveTypes.end())
264 return I->second;
265
266 Type *Typ = OpaqueType::get();
267 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
268 return Typ;
269 }
270
Reid Spencered96d1e2007-02-08 09:08:52 +0000271/// This function determines if two function types differ only in their use of
272/// the sret parameter attribute in the first argument. If they are identical
273/// in all other respects, it returns true. Otherwise, it returns false.
274bool FuncTysDifferOnlyBySRet(const FunctionType *F1,
275 const FunctionType *F2) {
276 if (F1->getReturnType() != F2->getReturnType() ||
277 F1->getNumParams() != F2->getNumParams() ||
278 F1->getParamAttrs(0) != F2->getParamAttrs(0))
279 return false;
280 unsigned SRetMask = ~unsigned(FunctionType::StructRetAttribute);
281 for (unsigned i = 0; i < F1->getNumParams(); ++i) {
282 if (F1->getParamType(i) != F2->getParamType(i) ||
283 unsigned(F1->getParamAttrs(i+1)) & SRetMask !=
284 unsigned(F2->getParamAttrs(i+1)) & SRetMask)
285 return false;
286 }
287 return true;
288}
289
290// The upgrade of csretcc to sret param attribute may have caused a function
291// to not be found because the param attribute changed the type of the called
292// function. This helper function, used in getExistingValue, detects that
293// situation and returns V if it occurs and 0 otherwise.
294static Value* handleSRetFuncTypeMerge(Value *V, const Type* Ty) {
295 // Handle degenerate cases
296 if (!V)
297 return 0;
298 if (V->getType() == Ty)
299 return V;
300
301 Value* Result = 0;
302 const PointerType *PF1 = dyn_cast<PointerType>(Ty);
303 const PointerType *PF2 = dyn_cast<PointerType>(V->getType());
304 if (PF1 && PF2) {
305 const FunctionType *FT1 =
306 dyn_cast<FunctionType>(PF1->getElementType());
307 const FunctionType *FT2 =
308 dyn_cast<FunctionType>(PF2->getElementType());
309 if (FT1 && FT2 && FuncTysDifferOnlyBySRet(FT1, FT2))
310 if (FT2->paramHasAttr(1, FunctionType::StructRetAttribute))
311 Result = V;
312 else if (Constant *C = dyn_cast<Constant>(V))
313 Result = ConstantExpr::getBitCast(C, PF1);
314 else
315 Result = new BitCastInst(V, PF1, "upgrd.cast", CurBB);
316 }
317 return Result;
318}
319
Reid Spencer950bf602007-01-26 08:19:09 +0000320// getExistingValue - Look up the value specified by the provided type and
321// the provided ValID. If the value exists and has already been defined, return
322// it. Otherwise return null.
323//
324static Value *getExistingValue(const Type *Ty, const ValID &D) {
325 if (isa<FunctionType>(Ty)) {
326 error("Functions are not values and must be referenced as pointers");
327 }
328
329 switch (D.Type) {
330 case ValID::NumberVal: { // Is it a numbered definition?
331 unsigned Num = (unsigned)D.Num;
332
333 // Module constants occupy the lowest numbered slots...
334 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
335 if (VI != CurModule.Values.end()) {
336 if (Num < VI->second.size())
337 return VI->second[Num];
338 Num -= VI->second.size();
339 }
340
341 // Make sure that our type is within bounds
342 VI = CurFun.Values.find(Ty);
343 if (VI == CurFun.Values.end()) return 0;
344
345 // Check that the number is within bounds...
346 if (VI->second.size() <= Num) return 0;
347
348 return VI->second[Num];
349 }
350
351 case ValID::NameVal: { // Is it a named definition?
352 // Get the name out of the ID
353 std::string Name(D.Name);
354 Value* V = 0;
355 RenameMapKey Key = std::make_pair(Name, Ty);
356 if (inFunctionScope()) {
357 // See if the name was renamed
358 RenameMapType::const_iterator I = CurFun.RenameMap.find(Key);
359 std::string LookupName;
360 if (I != CurFun.RenameMap.end())
361 LookupName = I->second;
362 else
363 LookupName = Name;
Reid Spenceref9b9a72007-02-05 20:47:22 +0000364 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
365 V = SymTab.lookup(LookupName);
Reid Spencered96d1e2007-02-08 09:08:52 +0000366 V = handleSRetFuncTypeMerge(V, Ty);
Reid Spencer950bf602007-01-26 08:19:09 +0000367 }
368 if (!V) {
369 RenameMapType::const_iterator I = CurModule.RenameMap.find(Key);
370 std::string LookupName;
371 if (I != CurModule.RenameMap.end())
372 LookupName = I->second;
373 else
374 LookupName = Name;
Reid Spenceref9b9a72007-02-05 20:47:22 +0000375 V = CurModule.CurrentModule->getValueSymbolTable().lookup(LookupName);
Reid Spencered96d1e2007-02-08 09:08:52 +0000376 V = handleSRetFuncTypeMerge(V, Ty);
Reid Spencer950bf602007-01-26 08:19:09 +0000377 }
Reid Spenceref9b9a72007-02-05 20:47:22 +0000378 if (!V)
Reid Spencer950bf602007-01-26 08:19:09 +0000379 return 0;
380
381 D.destroy(); // Free old strdup'd memory...
382 return V;
383 }
384
385 // Check to make sure that "Ty" is an integral type, and that our
386 // value will fit into the specified type...
387 case ValID::ConstSIntVal: // Is it a constant pool reference??
388 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
389 error("Signed integral constant '" + itostr(D.ConstPool64) +
390 "' is invalid for type '" + Ty->getDescription() + "'");
391 }
392 return ConstantInt::get(Ty, D.ConstPool64);
393
394 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
395 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
396 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64))
397 error("Integral constant '" + utostr(D.UConstPool64) +
398 "' is invalid or out of range");
399 else // This is really a signed reference. Transmogrify.
400 return ConstantInt::get(Ty, D.ConstPool64);
401 } else
402 return ConstantInt::get(Ty, D.UConstPool64);
403
404 case ValID::ConstFPVal: // Is it a floating point const pool reference?
405 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
406 error("FP constant invalid for type");
407 return ConstantFP::get(Ty, D.ConstPoolFP);
408
409 case ValID::ConstNullVal: // Is it a null value?
410 if (!isa<PointerType>(Ty))
411 error("Cannot create a a non pointer null");
412 return ConstantPointerNull::get(cast<PointerType>(Ty));
413
414 case ValID::ConstUndefVal: // Is it an undef value?
415 return UndefValue::get(Ty);
416
417 case ValID::ConstZeroVal: // Is it a zero value?
418 return Constant::getNullValue(Ty);
419
420 case ValID::ConstantVal: // Fully resolved constant?
421 if (D.ConstantValue->getType() != Ty)
422 error("Constant expression type different from required type");
423 return D.ConstantValue;
424
425 case ValID::InlineAsmVal: { // Inline asm expression
426 const PointerType *PTy = dyn_cast<PointerType>(Ty);
427 const FunctionType *FTy =
428 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
429 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints))
430 error("Invalid type for asm constraint string");
431 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
432 D.IAD->HasSideEffects);
433 D.destroy(); // Free InlineAsmDescriptor.
434 return IA;
435 }
436 default:
437 assert(0 && "Unhandled case");
438 return 0;
439 } // End of switch
440
441 assert(0 && "Unhandled case");
442 return 0;
443}
444
445// getVal - This function is identical to getExistingValue, except that if a
446// value is not already defined, it "improvises" by creating a placeholder var
447// that looks and acts just like the requested variable. When the value is
448// defined later, all uses of the placeholder variable are replaced with the
449// real thing.
450//
451static Value *getVal(const Type *Ty, const ValID &ID) {
452 if (Ty == Type::LabelTy)
453 error("Cannot use a basic block here");
454
455 // See if the value has already been defined.
456 Value *V = getExistingValue(Ty, ID);
457 if (V) return V;
458
459 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty))
460 error("Invalid use of a composite type");
461
462 // If we reached here, we referenced either a symbol that we don't know about
463 // or an id number that hasn't been read yet. We may be referencing something
464 // forward, so just create an entry to be resolved later and get to it...
Reid Spencer950bf602007-01-26 08:19:09 +0000465 V = new Argument(Ty);
466
467 // Remember where this forward reference came from. FIXME, shouldn't we try
468 // to recycle these things??
469 CurModule.PlaceHolderInfo.insert(
Reid Spenceref9b9a72007-02-05 20:47:22 +0000470 std::make_pair(V, std::make_pair(ID, Upgradelineno)));
Reid Spencer950bf602007-01-26 08:19:09 +0000471
472 if (inFunctionScope())
473 InsertValue(V, CurFun.LateResolveValues);
474 else
475 InsertValue(V, CurModule.LateResolveValues);
476 return V;
477}
478
Reid Spencered96d1e2007-02-08 09:08:52 +0000479/// @brief This just makes any name given to it unique, up to MAX_UINT times.
480static std::string makeNameUnique(const std::string& Name) {
481 static unsigned UniqueNameCounter = 1;
482 std::string Result(Name);
483 Result += ".upgrd." + llvm::utostr(UniqueNameCounter++);
484 return Result;
485}
486
Reid Spencer950bf602007-01-26 08:19:09 +0000487/// getBBVal - This is used for two purposes:
488/// * If isDefinition is true, a new basic block with the specified ID is being
489/// defined.
490/// * If isDefinition is true, this is a reference to a basic block, which may
491/// or may not be a forward reference.
492///
493static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
494 assert(inFunctionScope() && "Can't get basic block at global scope");
495
496 std::string Name;
497 BasicBlock *BB = 0;
498 switch (ID.Type) {
499 default:
500 error("Illegal label reference " + ID.getName());
501 break;
502 case ValID::NumberVal: // Is it a numbered definition?
503 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
504 CurFun.NumberedBlocks.resize(ID.Num+1);
505 BB = CurFun.NumberedBlocks[ID.Num];
506 break;
507 case ValID::NameVal: // Is it a named definition?
508 Name = ID.Name;
509 if (Value *N = CurFun.CurrentFunction->
Reid Spenceref9b9a72007-02-05 20:47:22 +0000510 getValueSymbolTable().lookup(Name)) {
Reid Spencered96d1e2007-02-08 09:08:52 +0000511 if (N->getType() != Type::LabelTy) {
512 // Register names didn't use to conflict with basic block names
513 // because of type planes. Now they all have to be unique. So, we just
514 // rename the register and treat this name as if no basic block
515 // had been found.
516 RenameMapKey Key = std::make_pair(N->getName(),N->getType());
517 N->setName(makeNameUnique(N->getName()));
518 CurModule.RenameMap[Key] = N->getName();
519 BB = 0;
520 } else {
521 BB = cast<BasicBlock>(N);
522 }
Reid Spencer950bf602007-01-26 08:19:09 +0000523 }
524 break;
525 }
526
527 // See if the block has already been defined.
528 if (BB) {
529 // If this is the definition of the block, make sure the existing value was
530 // just a forward reference. If it was a forward reference, there will be
531 // an entry for it in the PlaceHolderInfo map.
532 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
533 // The existing value was a definition, not a forward reference.
534 error("Redefinition of label " + ID.getName());
535
536 ID.destroy(); // Free strdup'd memory.
537 return BB;
538 }
539
540 // Otherwise this block has not been seen before.
541 BB = new BasicBlock("", CurFun.CurrentFunction);
542 if (ID.Type == ValID::NameVal) {
543 BB->setName(ID.Name);
544 } else {
545 CurFun.NumberedBlocks[ID.Num] = BB;
546 }
547
548 // If this is not a definition, keep track of it so we can use it as a forward
549 // reference.
550 if (!isDefinition) {
551 // Remember where this forward reference came from.
552 CurFun.BBForwardRefs[BB] = std::make_pair(ID, Upgradelineno);
553 } else {
554 // The forward declaration could have been inserted anywhere in the
555 // function: insert it into the correct place now.
556 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
557 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
558 }
559 ID.destroy();
560 return BB;
561}
562
563
564//===----------------------------------------------------------------------===//
565// Code to handle forward references in instructions
566//===----------------------------------------------------------------------===//
567//
568// This code handles the late binding needed with statements that reference
569// values not defined yet... for example, a forward branch, or the PHI node for
570// a loop body.
571//
572// This keeps a table (CurFun.LateResolveValues) of all such forward references
573// and back patchs after we are done.
574//
575
576// ResolveDefinitions - If we could not resolve some defs at parsing
577// time (forward branches, phi functions for loops, etc...) resolve the
578// defs now...
579//
580static void
581ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
582 std::map<const Type*,ValueList> *FutureLateResolvers) {
Reid Spencered96d1e2007-02-08 09:08:52 +0000583
Reid Spencer950bf602007-01-26 08:19:09 +0000584 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
585 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
586 E = LateResolvers.end(); LRI != E; ++LRI) {
Reid Spencered96d1e2007-02-08 09:08:52 +0000587 const Type* Ty = LRI->first;
Reid Spencer950bf602007-01-26 08:19:09 +0000588 ValueList &List = LRI->second;
589 while (!List.empty()) {
590 Value *V = List.back();
591 List.pop_back();
592
593 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
594 CurModule.PlaceHolderInfo.find(V);
595 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error");
596
597 ValID &DID = PHI->second.first;
598
Reid Spencered96d1e2007-02-08 09:08:52 +0000599 Value *TheRealValue = getExistingValue(Ty, DID);
Reid Spencer950bf602007-01-26 08:19:09 +0000600 if (TheRealValue) {
601 V->replaceAllUsesWith(TheRealValue);
602 delete V;
603 CurModule.PlaceHolderInfo.erase(PHI);
604 } else if (FutureLateResolvers) {
605 // Functions have their unresolved items forwarded to the module late
606 // resolver table
607 InsertValue(V, *FutureLateResolvers);
608 } else {
609 if (DID.Type == ValID::NameVal) {
Reid Spencered96d1e2007-02-08 09:08:52 +0000610 error("Reference to an invalid definition: '" + DID.getName() +
611 "' of type '" + V->getType()->getDescription() + "'",
612 PHI->second.second);
Reid Spencer7de2e012007-01-29 19:08:46 +0000613 return;
Reid Spencer950bf602007-01-26 08:19:09 +0000614 } else {
615 error("Reference to an invalid definition: #" +
616 itostr(DID.Num) + " of type '" +
617 V->getType()->getDescription() + "'", PHI->second.second);
618 return;
619 }
620 }
621 }
622 }
623
624 LateResolvers.clear();
625}
626
627// ResolveTypeTo - A brand new type was just declared. This means that (if
628// name is not null) things referencing Name can be resolved. Otherwise, things
629// refering to the number can be resolved. Do this now.
630//
631static void ResolveTypeTo(char *Name, const Type *ToTy) {
632 ValID D;
633 if (Name) D = ValID::create(Name);
634 else D = ValID::create((int)CurModule.Types.size());
635
636 std::map<ValID, PATypeHolder>::iterator I =
637 CurModule.LateResolveTypes.find(D);
638 if (I != CurModule.LateResolveTypes.end()) {
639 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
640 CurModule.LateResolveTypes.erase(I);
641 }
642}
643
Anton Korobeynikovce13b852007-01-28 15:25:24 +0000644/// This is the implementation portion of TypeHasInteger. It traverses the
645/// type given, avoiding recursive types, and returns true as soon as it finds
646/// an integer type. If no integer type is found, it returns false.
647static bool TypeHasIntegerI(const Type *Ty, std::vector<const Type*> Stack) {
648 // Handle some easy cases
649 if (Ty->isPrimitiveType() || (Ty->getTypeID() == Type::OpaqueTyID))
650 return false;
651 if (Ty->isInteger())
652 return true;
653 if (const SequentialType *STy = dyn_cast<SequentialType>(Ty))
654 return STy->getElementType()->isInteger();
655
656 // Avoid type structure recursion
657 for (std::vector<const Type*>::iterator I = Stack.begin(), E = Stack.end();
658 I != E; ++I)
659 if (Ty == *I)
660 return false;
661
662 // Push us on the type stack
663 Stack.push_back(Ty);
664
665 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
666 if (TypeHasIntegerI(FTy->getReturnType(), Stack))
667 return true;
668 FunctionType::param_iterator I = FTy->param_begin();
669 FunctionType::param_iterator E = FTy->param_end();
670 for (; I != E; ++I)
671 if (TypeHasIntegerI(*I, Stack))
672 return true;
673 return false;
674 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
675 StructType::element_iterator I = STy->element_begin();
676 StructType::element_iterator E = STy->element_end();
677 for (; I != E; ++I) {
678 if (TypeHasIntegerI(*I, Stack))
679 return true;
680 }
681 return false;
682 }
683 // There shouldn't be anything else, but its definitely not integer
684 assert(0 && "What type is this?");
685 return false;
686}
687
688/// This is the interface to TypeHasIntegerI. It just provides the type stack,
689/// to avoid recursion, and then calls TypeHasIntegerI.
690static inline bool TypeHasInteger(const Type *Ty) {
691 std::vector<const Type*> TyStack;
692 return TypeHasIntegerI(Ty, TyStack);
693}
694
Reid Spencer950bf602007-01-26 08:19:09 +0000695// setValueName - Set the specified value to the name given. The name may be
696// null potentially, in which case this is a noop. The string passed in is
697// assumed to be a malloc'd string buffer, and is free'd by this function.
698//
699static void setValueName(Value *V, char *NameStr) {
700 if (NameStr) {
701 std::string Name(NameStr); // Copy string
702 free(NameStr); // Free old string
703
704 if (V->getType() == Type::VoidTy) {
705 error("Can't assign name '" + Name + "' to value with void type");
706 return;
707 }
708
Reid Spencer950bf602007-01-26 08:19:09 +0000709 assert(inFunctionScope() && "Must be in function scope");
710
711 // Search the function's symbol table for an existing value of this name
Reid Spenceref9b9a72007-02-05 20:47:22 +0000712 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
713 Value* Existing = ST.lookup(Name);
Reid Spencer950bf602007-01-26 08:19:09 +0000714 if (Existing) {
Anton Korobeynikovce13b852007-01-28 15:25:24 +0000715 // An existing value of the same name was found. This might have happened
716 // because of the integer type planes collapsing in LLVM 2.0.
717 if (Existing->getType() == V->getType() &&
718 !TypeHasInteger(Existing->getType())) {
719 // If the type does not contain any integers in them then this can't be
720 // a type plane collapsing issue. It truly is a redefinition and we
721 // should error out as the assembly is invalid.
722 error("Redefinition of value named '" + Name + "' of type '" +
723 V->getType()->getDescription() + "'");
724 return;
Reid Spencer950bf602007-01-26 08:19:09 +0000725 }
726 // In LLVM 2.0 we don't allow names to be re-used for any values in a
727 // function, regardless of Type. Previously re-use of names was okay as
728 // long as they were distinct types. With type planes collapsing because
729 // of the signedness change and because of PR411, this can no longer be
730 // supported. We must search the entire symbol table for a conflicting
731 // name and make the name unique. No warning is needed as this can't
732 // cause a problem.
733 std::string NewName = makeNameUnique(Name);
734 // We're changing the name but it will probably be used by other
735 // instructions as operands later on. Consequently we have to retain
736 // a mapping of the renaming that we're doing.
737 RenameMapKey Key = std::make_pair(Name,V->getType());
738 CurFun.RenameMap[Key] = NewName;
739 Name = NewName;
740 }
741
742 // Set the name.
743 V->setName(Name);
744 }
745}
746
747/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
748/// this is a declaration, otherwise it is a definition.
749static GlobalVariable *
750ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
751 bool isConstantGlobal, const Type *Ty,
752 Constant *Initializer) {
753 if (isa<FunctionType>(Ty))
754 error("Cannot declare global vars of function type");
755
756 const PointerType *PTy = PointerType::get(Ty);
757
758 std::string Name;
759 if (NameStr) {
760 Name = NameStr; // Copy string
761 free(NameStr); // Free old string
762 }
763
764 // See if this global value was forward referenced. If so, recycle the
765 // object.
766 ValID ID;
767 if (!Name.empty()) {
768 ID = ValID::create((char*)Name.c_str());
769 } else {
770 ID = ValID::create((int)CurModule.Values[PTy].size());
771 }
772
773 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
774 // Move the global to the end of the list, from whereever it was
775 // previously inserted.
776 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
777 CurModule.CurrentModule->getGlobalList().remove(GV);
778 CurModule.CurrentModule->getGlobalList().push_back(GV);
779 GV->setInitializer(Initializer);
780 GV->setLinkage(Linkage);
781 GV->setConstant(isConstantGlobal);
782 InsertValue(GV, CurModule.Values);
783 return GV;
784 }
785
786 // If this global has a name, check to see if there is already a definition
787 // of this global in the module and emit warnings if there are conflicts.
788 if (!Name.empty()) {
789 // The global has a name. See if there's an existing one of the same name.
790 if (CurModule.CurrentModule->getNamedGlobal(Name)) {
791 // We found an existing global ov the same name. This isn't allowed
792 // in LLVM 2.0. Consequently, we must alter the name of the global so it
793 // can at least compile. This can happen because of type planes
794 // There is alread a global of the same name which means there is a
795 // conflict. Let's see what we can do about it.
796 std::string NewName(makeNameUnique(Name));
797 if (Linkage == GlobalValue::InternalLinkage) {
798 // The linkage type is internal so just warn about the rename without
799 // invoking "scarey language" about linkage failures. GVars with
800 // InternalLinkage can be renamed at will.
801 warning("Global variable '" + Name + "' was renamed to '"+
802 NewName + "'");
803 } else {
804 // The linkage of this gval is external so we can't reliably rename
805 // it because it could potentially create a linking problem.
806 // However, we can't leave the name conflict in the output either or
807 // it won't assemble with LLVM 2.0. So, all we can do is rename
808 // this one to something unique and emit a warning about the problem.
809 warning("Renaming global variable '" + Name + "' to '" + NewName +
810 "' may cause linkage errors");
811 }
812
813 // Put the renaming in the global rename map
814 RenameMapKey Key = std::make_pair(Name,PointerType::get(Ty));
815 CurModule.RenameMap[Key] = NewName;
816
817 // Rename it
818 Name = NewName;
819 }
820 }
821
822 // Otherwise there is no existing GV to use, create one now.
823 GlobalVariable *GV =
824 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
825 CurModule.CurrentModule);
826 InsertValue(GV, CurModule.Values);
827 return GV;
828}
829
830// setTypeName - Set the specified type to the name given. The name may be
831// null potentially, in which case this is a noop. The string passed in is
832// assumed to be a malloc'd string buffer, and is freed by this function.
833//
834// This function returns true if the type has already been defined, but is
835// allowed to be redefined in the specified context. If the name is a new name
836// for the type plane, it is inserted and false is returned.
837static bool setTypeName(const Type *T, char *NameStr) {
838 assert(!inFunctionScope() && "Can't give types function-local names");
839 if (NameStr == 0) return false;
840
841 std::string Name(NameStr); // Copy string
842 free(NameStr); // Free old string
843
844 // We don't allow assigning names to void type
845 if (T == Type::VoidTy) {
846 error("Can't assign name '" + Name + "' to the void type");
847 return false;
848 }
849
850 // Set the type name, checking for conflicts as we do so.
851 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
852
853 if (AlreadyExists) { // Inserting a name that is already defined???
854 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
855 assert(Existing && "Conflict but no matching type?");
856
857 // There is only one case where this is allowed: when we are refining an
858 // opaque type. In this case, Existing will be an opaque type.
859 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
860 // We ARE replacing an opaque type!
861 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
862 return true;
863 }
864
865 // Otherwise, this is an attempt to redefine a type. That's okay if
866 // the redefinition is identical to the original. This will be so if
867 // Existing and T point to the same Type object. In this one case we
868 // allow the equivalent redefinition.
869 if (Existing == T) return true; // Yes, it's equal.
870
871 // Any other kind of (non-equivalent) redefinition is an error.
872 error("Redefinition of type named '" + Name + "' in the '" +
873 T->getDescription() + "' type plane");
874 }
875
876 return false;
877}
878
879//===----------------------------------------------------------------------===//
880// Code for handling upreferences in type names...
881//
882
883// TypeContains - Returns true if Ty directly contains E in it.
884//
885static bool TypeContains(const Type *Ty, const Type *E) {
886 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
887 E) != Ty->subtype_end();
888}
889
890namespace {
891 struct UpRefRecord {
892 // NestingLevel - The number of nesting levels that need to be popped before
893 // this type is resolved.
894 unsigned NestingLevel;
895
896 // LastContainedTy - This is the type at the current binding level for the
897 // type. Every time we reduce the nesting level, this gets updated.
898 const Type *LastContainedTy;
899
900 // UpRefTy - This is the actual opaque type that the upreference is
901 // represented with.
902 OpaqueType *UpRefTy;
903
904 UpRefRecord(unsigned NL, OpaqueType *URTy)
905 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
906 };
907}
908
909// UpRefs - A list of the outstanding upreferences that need to be resolved.
910static std::vector<UpRefRecord> UpRefs;
911
912/// HandleUpRefs - Every time we finish a new layer of types, this function is
913/// called. It loops through the UpRefs vector, which is a list of the
914/// currently active types. For each type, if the up reference is contained in
915/// the newly completed type, we decrement the level count. When the level
916/// count reaches zero, the upreferenced type is the type that is passed in:
917/// thus we can complete the cycle.
918///
919static PATypeHolder HandleUpRefs(const Type *ty) {
920 // If Ty isn't abstract, or if there are no up-references in it, then there is
921 // nothing to resolve here.
922 if (!ty->isAbstract() || UpRefs.empty()) return ty;
923
924 PATypeHolder Ty(ty);
925 UR_OUT("Type '" << Ty->getDescription() <<
926 "' newly formed. Resolving upreferences.\n" <<
927 UpRefs.size() << " upreferences active!\n");
928
929 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
930 // to zero), we resolve them all together before we resolve them to Ty. At
931 // the end of the loop, if there is anything to resolve to Ty, it will be in
932 // this variable.
933 OpaqueType *TypeToResolve = 0;
934
935 for (unsigned i = 0; i != UpRefs.size(); ++i) {
936 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
937 << UpRefs[i].second->getDescription() << ") = "
938 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
939 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
940 // Decrement level of upreference
941 unsigned Level = --UpRefs[i].NestingLevel;
942 UpRefs[i].LastContainedTy = Ty;
943 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
944 if (Level == 0) { // Upreference should be resolved!
945 if (!TypeToResolve) {
946 TypeToResolve = UpRefs[i].UpRefTy;
947 } else {
948 UR_OUT(" * Resolving upreference for "
949 << UpRefs[i].second->getDescription() << "\n";
950 std::string OldName = UpRefs[i].UpRefTy->getDescription());
951 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
952 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
953 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
954 }
955 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
956 --i; // Do not skip the next element...
957 }
958 }
959 }
960
961 if (TypeToResolve) {
962 UR_OUT(" * Resolving upreference for "
963 << UpRefs[i].second->getDescription() << "\n";
964 std::string OldName = TypeToResolve->getDescription());
965 TypeToResolve->refineAbstractTypeTo(Ty);
966 }
967
968 return Ty;
969}
970
971static inline Instruction::TermOps
972getTermOp(TermOps op) {
973 switch (op) {
974 default : assert(0 && "Invalid OldTermOp");
975 case RetOp : return Instruction::Ret;
976 case BrOp : return Instruction::Br;
977 case SwitchOp : return Instruction::Switch;
978 case InvokeOp : return Instruction::Invoke;
979 case UnwindOp : return Instruction::Unwind;
980 case UnreachableOp: return Instruction::Unreachable;
981 }
982}
983
984static inline Instruction::BinaryOps
985getBinaryOp(BinaryOps op, const Type *Ty, Signedness Sign) {
986 switch (op) {
987 default : assert(0 && "Invalid OldBinaryOps");
988 case SetEQ :
989 case SetNE :
990 case SetLE :
991 case SetGE :
992 case SetLT :
993 case SetGT : assert(0 && "Should use getCompareOp");
994 case AddOp : return Instruction::Add;
995 case SubOp : return Instruction::Sub;
996 case MulOp : return Instruction::Mul;
997 case DivOp : {
998 // This is an obsolete instruction so we must upgrade it based on the
999 // types of its operands.
1000 bool isFP = Ty->isFloatingPoint();
Reid Spencer9d6565a2007-02-15 02:26:10 +00001001 if (const VectorType* PTy = dyn_cast<VectorType>(Ty))
Chris Lattner4227bdb2007-02-19 07:34:02 +00001002 // If its a vector type we want to use the element type
Reid Spencer950bf602007-01-26 08:19:09 +00001003 isFP = PTy->getElementType()->isFloatingPoint();
1004 if (isFP)
1005 return Instruction::FDiv;
1006 else if (Sign == Signed)
1007 return Instruction::SDiv;
1008 return Instruction::UDiv;
1009 }
1010 case UDivOp : return Instruction::UDiv;
1011 case SDivOp : return Instruction::SDiv;
1012 case FDivOp : return Instruction::FDiv;
1013 case RemOp : {
1014 // This is an obsolete instruction so we must upgrade it based on the
1015 // types of its operands.
1016 bool isFP = Ty->isFloatingPoint();
Reid Spencer9d6565a2007-02-15 02:26:10 +00001017 if (const VectorType* PTy = dyn_cast<VectorType>(Ty))
Chris Lattner4227bdb2007-02-19 07:34:02 +00001018 // If its a vector type we want to use the element type
Reid Spencer950bf602007-01-26 08:19:09 +00001019 isFP = PTy->getElementType()->isFloatingPoint();
1020 // Select correct opcode
1021 if (isFP)
1022 return Instruction::FRem;
1023 else if (Sign == Signed)
1024 return Instruction::SRem;
1025 return Instruction::URem;
1026 }
1027 case URemOp : return Instruction::URem;
1028 case SRemOp : return Instruction::SRem;
1029 case FRemOp : return Instruction::FRem;
Reid Spencer832254e2007-02-02 02:16:23 +00001030 case LShrOp : return Instruction::LShr;
1031 case AShrOp : return Instruction::AShr;
1032 case ShlOp : return Instruction::Shl;
1033 case ShrOp :
1034 if (Sign == Signed)
1035 return Instruction::AShr;
1036 return Instruction::LShr;
Reid Spencer950bf602007-01-26 08:19:09 +00001037 case AndOp : return Instruction::And;
1038 case OrOp : return Instruction::Or;
1039 case XorOp : return Instruction::Xor;
1040 }
1041}
1042
1043static inline Instruction::OtherOps
1044getCompareOp(BinaryOps op, unsigned short &predicate, const Type* &Ty,
1045 Signedness Sign) {
1046 bool isSigned = Sign == Signed;
1047 bool isFP = Ty->isFloatingPoint();
1048 switch (op) {
1049 default : assert(0 && "Invalid OldSetCC");
1050 case SetEQ :
1051 if (isFP) {
1052 predicate = FCmpInst::FCMP_OEQ;
1053 return Instruction::FCmp;
1054 } else {
1055 predicate = ICmpInst::ICMP_EQ;
1056 return Instruction::ICmp;
1057 }
1058 case SetNE :
1059 if (isFP) {
1060 predicate = FCmpInst::FCMP_UNE;
1061 return Instruction::FCmp;
1062 } else {
1063 predicate = ICmpInst::ICMP_NE;
1064 return Instruction::ICmp;
1065 }
1066 case SetLE :
1067 if (isFP) {
1068 predicate = FCmpInst::FCMP_OLE;
1069 return Instruction::FCmp;
1070 } else {
1071 if (isSigned)
1072 predicate = ICmpInst::ICMP_SLE;
1073 else
1074 predicate = ICmpInst::ICMP_ULE;
1075 return Instruction::ICmp;
1076 }
1077 case SetGE :
1078 if (isFP) {
1079 predicate = FCmpInst::FCMP_OGE;
1080 return Instruction::FCmp;
1081 } else {
1082 if (isSigned)
1083 predicate = ICmpInst::ICMP_SGE;
1084 else
1085 predicate = ICmpInst::ICMP_UGE;
1086 return Instruction::ICmp;
1087 }
1088 case SetLT :
1089 if (isFP) {
1090 predicate = FCmpInst::FCMP_OLT;
1091 return Instruction::FCmp;
1092 } else {
1093 if (isSigned)
1094 predicate = ICmpInst::ICMP_SLT;
1095 else
1096 predicate = ICmpInst::ICMP_ULT;
1097 return Instruction::ICmp;
1098 }
1099 case SetGT :
1100 if (isFP) {
1101 predicate = FCmpInst::FCMP_OGT;
1102 return Instruction::FCmp;
1103 } else {
1104 if (isSigned)
1105 predicate = ICmpInst::ICMP_SGT;
1106 else
1107 predicate = ICmpInst::ICMP_UGT;
1108 return Instruction::ICmp;
1109 }
1110 }
1111}
1112
1113static inline Instruction::MemoryOps getMemoryOp(MemoryOps op) {
1114 switch (op) {
1115 default : assert(0 && "Invalid OldMemoryOps");
1116 case MallocOp : return Instruction::Malloc;
1117 case FreeOp : return Instruction::Free;
1118 case AllocaOp : return Instruction::Alloca;
1119 case LoadOp : return Instruction::Load;
1120 case StoreOp : return Instruction::Store;
1121 case GetElementPtrOp : return Instruction::GetElementPtr;
1122 }
1123}
1124
1125static inline Instruction::OtherOps
1126getOtherOp(OtherOps op, Signedness Sign) {
1127 switch (op) {
1128 default : assert(0 && "Invalid OldOtherOps");
1129 case PHIOp : return Instruction::PHI;
1130 case CallOp : return Instruction::Call;
Reid Spencer950bf602007-01-26 08:19:09 +00001131 case SelectOp : return Instruction::Select;
1132 case UserOp1 : return Instruction::UserOp1;
1133 case UserOp2 : return Instruction::UserOp2;
1134 case VAArg : return Instruction::VAArg;
1135 case ExtractElementOp : return Instruction::ExtractElement;
1136 case InsertElementOp : return Instruction::InsertElement;
1137 case ShuffleVectorOp : return Instruction::ShuffleVector;
1138 case ICmpOp : return Instruction::ICmp;
1139 case FCmpOp : return Instruction::FCmp;
Reid Spencer950bf602007-01-26 08:19:09 +00001140 };
1141}
1142
1143static inline Value*
1144getCast(CastOps op, Value *Src, Signedness SrcSign, const Type *DstTy,
1145 Signedness DstSign, bool ForceInstruction = false) {
1146 Instruction::CastOps Opcode;
1147 const Type* SrcTy = Src->getType();
1148 if (op == CastOp) {
1149 if (SrcTy->isFloatingPoint() && isa<PointerType>(DstTy)) {
1150 // fp -> ptr cast is no longer supported but we must upgrade this
1151 // by doing a double cast: fp -> int -> ptr
1152 SrcTy = Type::Int64Ty;
1153 Opcode = Instruction::IntToPtr;
1154 if (isa<Constant>(Src)) {
1155 Src = ConstantExpr::getCast(Instruction::FPToUI,
1156 cast<Constant>(Src), SrcTy);
1157 } else {
1158 std::string NewName(makeNameUnique(Src->getName()));
1159 Src = new FPToUIInst(Src, SrcTy, NewName, CurBB);
1160 }
1161 } else if (isa<IntegerType>(DstTy) &&
1162 cast<IntegerType>(DstTy)->getBitWidth() == 1) {
1163 // cast type %x to bool was previously defined as setne type %x, null
1164 // The cast semantic is now to truncate, not compare so we must retain
1165 // the original intent by replacing the cast with a setne
1166 Constant* Null = Constant::getNullValue(SrcTy);
1167 Instruction::OtherOps Opcode = Instruction::ICmp;
1168 unsigned short predicate = ICmpInst::ICMP_NE;
1169 if (SrcTy->isFloatingPoint()) {
1170 Opcode = Instruction::FCmp;
1171 predicate = FCmpInst::FCMP_ONE;
1172 } else if (!SrcTy->isInteger() && !isa<PointerType>(SrcTy)) {
1173 error("Invalid cast to bool");
1174 }
1175 if (isa<Constant>(Src) && !ForceInstruction)
1176 return ConstantExpr::getCompare(predicate, cast<Constant>(Src), Null);
1177 else
1178 return CmpInst::create(Opcode, predicate, Src, Null);
1179 }
1180 // Determine the opcode to use by calling CastInst::getCastOpcode
1181 Opcode =
1182 CastInst::getCastOpcode(Src, SrcSign == Signed, DstTy, DstSign == Signed);
1183
1184 } else switch (op) {
1185 default: assert(0 && "Invalid cast token");
1186 case TruncOp: Opcode = Instruction::Trunc; break;
1187 case ZExtOp: Opcode = Instruction::ZExt; break;
1188 case SExtOp: Opcode = Instruction::SExt; break;
1189 case FPTruncOp: Opcode = Instruction::FPTrunc; break;
1190 case FPExtOp: Opcode = Instruction::FPExt; break;
1191 case FPToUIOp: Opcode = Instruction::FPToUI; break;
1192 case FPToSIOp: Opcode = Instruction::FPToSI; break;
1193 case UIToFPOp: Opcode = Instruction::UIToFP; break;
1194 case SIToFPOp: Opcode = Instruction::SIToFP; break;
1195 case PtrToIntOp: Opcode = Instruction::PtrToInt; break;
1196 case IntToPtrOp: Opcode = Instruction::IntToPtr; break;
1197 case BitCastOp: Opcode = Instruction::BitCast; break;
1198 }
1199
1200 if (isa<Constant>(Src) && !ForceInstruction)
1201 return ConstantExpr::getCast(Opcode, cast<Constant>(Src), DstTy);
1202 return CastInst::create(Opcode, Src, DstTy);
1203}
1204
1205static Instruction *
1206upgradeIntrinsicCall(const Type* RetTy, const ValID &ID,
1207 std::vector<Value*>& Args) {
1208
1209 std::string Name = ID.Type == ValID::NameVal ? ID.Name : "";
1210 if (Name == "llvm.isunordered.f32" || Name == "llvm.isunordered.f64") {
1211 if (Args.size() != 2)
1212 error("Invalid prototype for " + Name + " prototype");
1213 return new FCmpInst(FCmpInst::FCMP_UNO, Args[0], Args[1]);
1214 } else {
Reid Spencer950bf602007-01-26 08:19:09 +00001215 const Type* PtrTy = PointerType::get(Type::Int8Ty);
1216 std::vector<const Type*> Params;
1217 if (Name == "llvm.va_start" || Name == "llvm.va_end") {
1218 if (Args.size() != 1)
1219 error("Invalid prototype for " + Name + " prototype");
1220 Params.push_back(PtrTy);
1221 const FunctionType *FTy = FunctionType::get(Type::VoidTy, Params, false);
1222 const PointerType *PFTy = PointerType::get(FTy);
1223 Value* Func = getVal(PFTy, ID);
Reid Spencer832254e2007-02-02 02:16:23 +00001224 Args[0] = new BitCastInst(Args[0], PtrTy, makeNameUnique("va"), CurBB);
Chris Lattnercf3d0612007-02-13 06:04:17 +00001225 return new CallInst(Func, &Args[0], Args.size());
Reid Spencer950bf602007-01-26 08:19:09 +00001226 } else if (Name == "llvm.va_copy") {
1227 if (Args.size() != 2)
1228 error("Invalid prototype for " + Name + " prototype");
1229 Params.push_back(PtrTy);
1230 Params.push_back(PtrTy);
1231 const FunctionType *FTy = FunctionType::get(Type::VoidTy, Params, false);
1232 const PointerType *PFTy = PointerType::get(FTy);
1233 Value* Func = getVal(PFTy, ID);
Reid Spencer832254e2007-02-02 02:16:23 +00001234 std::string InstName0(makeNameUnique("va0"));
1235 std::string InstName1(makeNameUnique("va1"));
Reid Spencer950bf602007-01-26 08:19:09 +00001236 Args[0] = new BitCastInst(Args[0], PtrTy, InstName0, CurBB);
1237 Args[1] = new BitCastInst(Args[1], PtrTy, InstName1, CurBB);
Chris Lattnercf3d0612007-02-13 06:04:17 +00001238 return new CallInst(Func, &Args[0], Args.size());
Reid Spencer950bf602007-01-26 08:19:09 +00001239 }
1240 }
1241 return 0;
1242}
1243
1244const Type* upgradeGEPIndices(const Type* PTy,
1245 std::vector<ValueInfo> *Indices,
1246 std::vector<Value*> &VIndices,
1247 std::vector<Constant*> *CIndices = 0) {
1248 // Traverse the indices with a gep_type_iterator so we can build the list
1249 // of constant and value indices for use later. Also perform upgrades
1250 VIndices.clear();
1251 if (CIndices) CIndices->clear();
1252 for (unsigned i = 0, e = Indices->size(); i != e; ++i)
1253 VIndices.push_back((*Indices)[i].V);
1254 generic_gep_type_iterator<std::vector<Value*>::iterator>
1255 GTI = gep_type_begin(PTy, VIndices.begin(), VIndices.end()),
1256 GTE = gep_type_end(PTy, VIndices.begin(), VIndices.end());
1257 for (unsigned i = 0, e = Indices->size(); i != e && GTI != GTE; ++i, ++GTI) {
1258 Value *Index = VIndices[i];
1259 if (CIndices && !isa<Constant>(Index))
1260 error("Indices to constant getelementptr must be constants");
1261 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte
1262 // struct indices to i32 struct indices with ZExt for compatibility.
1263 else if (isa<StructType>(*GTI)) { // Only change struct indices
1264 if (ConstantInt *CUI = dyn_cast<ConstantInt>(Index))
1265 if (CUI->getType()->getBitWidth() == 8)
1266 Index =
1267 ConstantExpr::getCast(Instruction::ZExt, CUI, Type::Int32Ty);
1268 } else {
1269 // Make sure that unsigned SequentialType indices are zext'd to
1270 // 64-bits if they were smaller than that because LLVM 2.0 will sext
1271 // all indices for SequentialType elements. We must retain the same
1272 // semantic (zext) for unsigned types.
1273 if (const IntegerType *Ity = dyn_cast<IntegerType>(Index->getType()))
Reid Spencer38f682b2007-01-26 20:31:18 +00001274 if (Ity->getBitWidth() < 64 && (*Indices)[i].S == Unsigned) {
Reid Spencer950bf602007-01-26 08:19:09 +00001275 if (CIndices)
1276 Index = ConstantExpr::getCast(Instruction::ZExt,
1277 cast<Constant>(Index), Type::Int64Ty);
1278 else
1279 Index = CastInst::create(Instruction::ZExt, Index, Type::Int64Ty,
Reid Spencer832254e2007-02-02 02:16:23 +00001280 makeNameUnique("gep"), CurBB);
Reid Spencer38f682b2007-01-26 20:31:18 +00001281 VIndices[i] = Index;
1282 }
Reid Spencer950bf602007-01-26 08:19:09 +00001283 }
1284 // Add to the CIndices list, if requested.
1285 if (CIndices)
1286 CIndices->push_back(cast<Constant>(Index));
1287 }
1288
1289 const Type *IdxTy =
Chris Lattner1bc3fa62007-02-12 22:58:38 +00001290 GetElementPtrInst::getIndexedType(PTy, &VIndices[0], VIndices.size(), true);
Reid Spencer950bf602007-01-26 08:19:09 +00001291 if (!IdxTy)
1292 error("Index list invalid for constant getelementptr");
1293 return IdxTy;
1294}
1295
Reid Spencerb7046c72007-01-29 05:41:34 +00001296unsigned upgradeCallingConv(unsigned CC) {
1297 switch (CC) {
1298 case OldCallingConv::C : return CallingConv::C;
1299 case OldCallingConv::CSRet : return CallingConv::C;
1300 case OldCallingConv::Fast : return CallingConv::Fast;
1301 case OldCallingConv::Cold : return CallingConv::Cold;
1302 case OldCallingConv::X86_StdCall : return CallingConv::X86_StdCall;
1303 case OldCallingConv::X86_FastCall: return CallingConv::X86_FastCall;
1304 default:
1305 return CC;
1306 }
1307}
1308
Reid Spencer950bf602007-01-26 08:19:09 +00001309Module* UpgradeAssembly(const std::string &infile, std::istream& in,
1310 bool debug, bool addAttrs)
Reid Spencere7c3c602006-11-30 06:36:44 +00001311{
1312 Upgradelineno = 1;
1313 CurFilename = infile;
Reid Spencer96839be2006-11-30 16:50:26 +00001314 LexInput = &in;
Reid Spencere77e35e2006-12-01 20:26:20 +00001315 yydebug = debug;
Reid Spencer71d2ec92006-12-31 06:02:26 +00001316 AddAttributes = addAttrs;
Reid Spencer950bf602007-01-26 08:19:09 +00001317 ObsoleteVarArgs = false;
1318 NewVarArgs = false;
Reid Spencere7c3c602006-11-30 06:36:44 +00001319
Reid Spencer950bf602007-01-26 08:19:09 +00001320 CurModule.CurrentModule = new Module(CurFilename);
1321
1322 // Check to make sure the parser succeeded
Reid Spencere7c3c602006-11-30 06:36:44 +00001323 if (yyparse()) {
Reid Spencer950bf602007-01-26 08:19:09 +00001324 if (ParserResult)
1325 delete ParserResult;
Reid Spencer30d0c582007-01-15 00:26:18 +00001326 std::cerr << "llvm-upgrade: parse failed.\n";
Reid Spencer30d0c582007-01-15 00:26:18 +00001327 return 0;
1328 }
1329
Reid Spencer950bf602007-01-26 08:19:09 +00001330 // Check to make sure that parsing produced a result
1331 if (!ParserResult) {
1332 std::cerr << "llvm-upgrade: no parse result.\n";
1333 return 0;
Reid Spencer30d0c582007-01-15 00:26:18 +00001334 }
1335
Reid Spencer950bf602007-01-26 08:19:09 +00001336 // Reset ParserResult variable while saving its value for the result.
1337 Module *Result = ParserResult;
1338 ParserResult = 0;
Reid Spencer30d0c582007-01-15 00:26:18 +00001339
Reid Spencer950bf602007-01-26 08:19:09 +00001340 //Not all functions use vaarg, so make a second check for ObsoleteVarArgs
Reid Spencer30d0c582007-01-15 00:26:18 +00001341 {
Reid Spencer950bf602007-01-26 08:19:09 +00001342 Function* F;
Reid Spencer688b0492007-02-05 21:19:13 +00001343 if ((F = Result->getFunction("llvm.va_start"))
Reid Spencer950bf602007-01-26 08:19:09 +00001344 && F->getFunctionType()->getNumParams() == 0)
1345 ObsoleteVarArgs = true;
Reid Spencer688b0492007-02-05 21:19:13 +00001346 if((F = Result->getFunction("llvm.va_copy"))
Reid Spencer950bf602007-01-26 08:19:09 +00001347 && F->getFunctionType()->getNumParams() == 1)
1348 ObsoleteVarArgs = true;
Reid Spencer280d8012006-12-01 23:40:53 +00001349 }
Reid Spencer319a7302007-01-05 17:20:02 +00001350
Reid Spencer950bf602007-01-26 08:19:09 +00001351 if (ObsoleteVarArgs && NewVarArgs) {
1352 error("This file is corrupt: it uses both new and old style varargs");
1353 return 0;
Reid Spencer319a7302007-01-05 17:20:02 +00001354 }
Reid Spencer319a7302007-01-05 17:20:02 +00001355
Reid Spencer950bf602007-01-26 08:19:09 +00001356 if(ObsoleteVarArgs) {
Reid Spencer688b0492007-02-05 21:19:13 +00001357 if(Function* F = Result->getFunction("llvm.va_start")) {
Reid Spencer950bf602007-01-26 08:19:09 +00001358 if (F->arg_size() != 0) {
1359 error("Obsolete va_start takes 0 argument");
Reid Spencer319a7302007-01-05 17:20:02 +00001360 return 0;
1361 }
Reid Spencer950bf602007-01-26 08:19:09 +00001362
1363 //foo = va_start()
1364 // ->
1365 //bar = alloca typeof(foo)
1366 //va_start(bar)
1367 //foo = load bar
Reid Spencer319a7302007-01-05 17:20:02 +00001368
Reid Spencer950bf602007-01-26 08:19:09 +00001369 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
1370 const Type* ArgTy = F->getFunctionType()->getReturnType();
1371 const Type* ArgTyPtr = PointerType::get(ArgTy);
1372 Function* NF = cast<Function>(Result->getOrInsertFunction(
1373 "llvm.va_start", RetTy, ArgTyPtr, (Type *)0));
1374
1375 while (!F->use_empty()) {
1376 CallInst* CI = cast<CallInst>(F->use_back());
1377 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
1378 new CallInst(NF, bar, "", CI);
1379 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
1380 CI->replaceAllUsesWith(foo);
1381 CI->getParent()->getInstList().erase(CI);
Reid Spencerf8383de2007-01-06 06:04:32 +00001382 }
Reid Spencer950bf602007-01-26 08:19:09 +00001383 Result->getFunctionList().erase(F);
Reid Spencerf8383de2007-01-06 06:04:32 +00001384 }
Reid Spencer950bf602007-01-26 08:19:09 +00001385
Reid Spencer688b0492007-02-05 21:19:13 +00001386 if(Function* F = Result->getFunction("llvm.va_end")) {
Reid Spencer950bf602007-01-26 08:19:09 +00001387 if(F->arg_size() != 1) {
1388 error("Obsolete va_end takes 1 argument");
1389 return 0;
Reid Spencerf8383de2007-01-06 06:04:32 +00001390 }
Reid Spencerf8383de2007-01-06 06:04:32 +00001391
Reid Spencer950bf602007-01-26 08:19:09 +00001392 //vaend foo
1393 // ->
1394 //bar = alloca 1 of typeof(foo)
1395 //vaend bar
1396 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
1397 const Type* ArgTy = F->getFunctionType()->getParamType(0);
1398 const Type* ArgTyPtr = PointerType::get(ArgTy);
1399 Function* NF = cast<Function>(Result->getOrInsertFunction(
1400 "llvm.va_end", RetTy, ArgTyPtr, (Type *)0));
Reid Spencerf8383de2007-01-06 06:04:32 +00001401
Reid Spencer950bf602007-01-26 08:19:09 +00001402 while (!F->use_empty()) {
1403 CallInst* CI = cast<CallInst>(F->use_back());
1404 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
1405 new StoreInst(CI->getOperand(1), bar, CI);
1406 new CallInst(NF, bar, "", CI);
1407 CI->getParent()->getInstList().erase(CI);
Reid Spencere77e35e2006-12-01 20:26:20 +00001408 }
Reid Spencer950bf602007-01-26 08:19:09 +00001409 Result->getFunctionList().erase(F);
Reid Spencere77e35e2006-12-01 20:26:20 +00001410 }
Reid Spencer950bf602007-01-26 08:19:09 +00001411
Reid Spencer688b0492007-02-05 21:19:13 +00001412 if(Function* F = Result->getFunction("llvm.va_copy")) {
Reid Spencer950bf602007-01-26 08:19:09 +00001413 if(F->arg_size() != 1) {
1414 error("Obsolete va_copy takes 1 argument");
1415 return 0;
Reid Spencere77e35e2006-12-01 20:26:20 +00001416 }
Reid Spencer950bf602007-01-26 08:19:09 +00001417 //foo = vacopy(bar)
1418 // ->
1419 //a = alloca 1 of typeof(foo)
1420 //b = alloca 1 of typeof(foo)
1421 //store bar -> b
1422 //vacopy(a, b)
1423 //foo = load a
1424
1425 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
1426 const Type* ArgTy = F->getFunctionType()->getReturnType();
1427 const Type* ArgTyPtr = PointerType::get(ArgTy);
1428 Function* NF = cast<Function>(Result->getOrInsertFunction(
1429 "llvm.va_copy", RetTy, ArgTyPtr, ArgTyPtr, (Type *)0));
Reid Spencere77e35e2006-12-01 20:26:20 +00001430
Reid Spencer950bf602007-01-26 08:19:09 +00001431 while (!F->use_empty()) {
1432 CallInst* CI = cast<CallInst>(F->use_back());
1433 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
1434 AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
1435 new StoreInst(CI->getOperand(1), b, CI);
1436 new CallInst(NF, a, b, "", CI);
1437 Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
1438 CI->replaceAllUsesWith(foo);
1439 CI->getParent()->getInstList().erase(CI);
1440 }
1441 Result->getFunctionList().erase(F);
Reid Spencer319a7302007-01-05 17:20:02 +00001442 }
1443 }
1444
Reid Spencer52402b02007-01-02 05:45:11 +00001445 return Result;
1446}
1447
Reid Spencer950bf602007-01-26 08:19:09 +00001448} // end llvm namespace
Reid Spencer319a7302007-01-05 17:20:02 +00001449
Reid Spencer950bf602007-01-26 08:19:09 +00001450using namespace llvm;
Reid Spencer30d0c582007-01-15 00:26:18 +00001451
Reid Spencere7c3c602006-11-30 06:36:44 +00001452%}
1453
Reid Spencere77e35e2006-12-01 20:26:20 +00001454%union {
Reid Spencer950bf602007-01-26 08:19:09 +00001455 llvm::Module *ModuleVal;
1456 llvm::Function *FunctionVal;
1457 std::pair<llvm::PATypeInfo, char*> *ArgVal;
1458 llvm::BasicBlock *BasicBlockVal;
1459 llvm::TerminatorInst *TermInstVal;
1460 llvm::InstrInfo InstVal;
1461 llvm::ConstInfo ConstVal;
1462 llvm::ValueInfo ValueVal;
1463 llvm::PATypeInfo TypeVal;
1464 llvm::TypeInfo PrimType;
1465 llvm::PHIListInfo PHIList;
1466 std::list<llvm::PATypeInfo> *TypeList;
1467 std::vector<llvm::ValueInfo> *ValueList;
1468 std::vector<llvm::ConstInfo> *ConstVector;
1469
1470
1471 std::vector<std::pair<llvm::PATypeInfo,char*> > *ArgList;
1472 // Represent the RHS of PHI node
1473 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
1474
1475 llvm::GlobalValue::LinkageTypes Linkage;
1476 int64_t SInt64Val;
1477 uint64_t UInt64Val;
1478 int SIntVal;
1479 unsigned UIntVal;
1480 double FPVal;
1481 bool BoolVal;
1482
1483 char *StrVal; // This memory is strdup'd!
1484 llvm::ValID ValIDVal; // strdup'd memory maybe!
1485
1486 llvm::BinaryOps BinaryOpVal;
1487 llvm::TermOps TermOpVal;
1488 llvm::MemoryOps MemOpVal;
1489 llvm::OtherOps OtherOpVal;
1490 llvm::CastOps CastOpVal;
1491 llvm::ICmpInst::Predicate IPred;
1492 llvm::FCmpInst::Predicate FPred;
1493 llvm::Module::Endianness Endianness;
Reid Spencere77e35e2006-12-01 20:26:20 +00001494}
1495
Reid Spencer950bf602007-01-26 08:19:09 +00001496%type <ModuleVal> Module FunctionList
1497%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
1498%type <BasicBlockVal> BasicBlock InstructionList
1499%type <TermInstVal> BBTerminatorInst
1500%type <InstVal> Inst InstVal MemoryInst
1501%type <ConstVal> ConstVal ConstExpr
1502%type <ConstVector> ConstVector
1503%type <ArgList> ArgList ArgListH
1504%type <ArgVal> ArgVal
1505%type <PHIList> PHIList
1506%type <ValueList> ValueRefList ValueRefListE // For call param lists
1507%type <ValueList> IndexList // For GEP derived indices
1508%type <TypeList> TypeListI ArgTypeListI
1509%type <JumpTable> JumpTable
1510%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
1511%type <BoolVal> OptVolatile // 'volatile' or not
1512%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1513%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencered96d1e2007-02-08 09:08:52 +00001514%type <Linkage> OptLinkage FnDeclareLinkage
Reid Spencer950bf602007-01-26 08:19:09 +00001515%type <Endianness> BigOrLittle
Reid Spencere77e35e2006-12-01 20:26:20 +00001516
Reid Spencer950bf602007-01-26 08:19:09 +00001517// ValueRef - Unresolved reference to a definition or BB
1518%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1519%type <ValueVal> ResolvedVal // <type> <valref> pair
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001520
Reid Spencer950bf602007-01-26 08:19:09 +00001521// Tokens and types for handling constant integer values
1522//
1523// ESINT64VAL - A negative number within long long range
1524%token <SInt64Val> ESINT64VAL
Reid Spencere77e35e2006-12-01 20:26:20 +00001525
Reid Spencer950bf602007-01-26 08:19:09 +00001526// EUINT64VAL - A positive number within uns. long long range
1527%token <UInt64Val> EUINT64VAL
1528%type <SInt64Val> EINT64VAL
Reid Spencere77e35e2006-12-01 20:26:20 +00001529
Reid Spencer950bf602007-01-26 08:19:09 +00001530%token <SIntVal> SINTVAL // Signed 32 bit ints...
1531%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
1532%type <SIntVal> INTVAL
1533%token <FPVal> FPVAL // Float or Double constant
Reid Spencere77e35e2006-12-01 20:26:20 +00001534
Reid Spencer950bf602007-01-26 08:19:09 +00001535// Built in types...
1536%type <TypeVal> Types TypesV UpRTypes UpRTypesV
1537%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
1538%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
1539%token <PrimType> FLOAT DOUBLE TYPE LABEL
Reid Spencere77e35e2006-12-01 20:26:20 +00001540
Reid Spencer950bf602007-01-26 08:19:09 +00001541%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
1542%type <StrVal> Name OptName OptAssign
1543%type <UIntVal> OptAlign OptCAlign
1544%type <StrVal> OptSection SectionString
1545
1546%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
1547%token DECLARE GLOBAL CONSTANT SECTION VOLATILE
1548%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
1549%token DLLIMPORT DLLEXPORT EXTERN_WEAK
1550%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
1551%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
1552%token CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
1553%token X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
1554%token DATALAYOUT
1555%type <UIntVal> OptCallingConv
1556
1557// Basic Block Terminating Operators
1558%token <TermOpVal> RET BR SWITCH INVOKE UNREACHABLE
1559%token UNWIND EXCEPT
1560
1561// Binary Operators
1562%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Reid Spencer832254e2007-02-02 02:16:23 +00001563%type <BinaryOpVal> ShiftOps
Reid Spencer950bf602007-01-26 08:19:09 +00001564%token <BinaryOpVal> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM
Reid Spencer832254e2007-02-02 02:16:23 +00001565%token <BinaryOpVal> AND OR XOR SHL SHR ASHR LSHR
Reid Spencer950bf602007-01-26 08:19:09 +00001566%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
1567%token <OtherOpVal> ICMP FCMP
1568
1569// Memory Instructions
1570%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1571
1572// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001573%token <OtherOpVal> PHI_TOK SELECT VAARG
Reid Spencer950bf602007-01-26 08:19:09 +00001574%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
1575%token VAARG_old VANEXT_old //OBSOLETE
1576
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001577// Support for ICmp/FCmp Predicates, which is 1.9++ but not 2.0
Reid Spencer950bf602007-01-26 08:19:09 +00001578%type <IPred> IPredicates
1579%type <FPred> FPredicates
1580%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1581%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
1582
1583%token <CastOpVal> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI
1584%token <CastOpVal> UITOFP SITOFP PTRTOINT INTTOPTR BITCAST
1585%type <CastOpVal> CastOps
Reid Spencere7c3c602006-11-30 06:36:44 +00001586
1587%start Module
1588
1589%%
1590
1591// Handle constant integer size restriction and conversion...
Reid Spencer950bf602007-01-26 08:19:09 +00001592//
1593INTVAL
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001594 : SINTVAL
Reid Spencer950bf602007-01-26 08:19:09 +00001595 | UINTVAL {
1596 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
1597 error("Value too large for type");
1598 $$ = (int32_t)$1;
1599 }
1600 ;
1601
1602EINT64VAL
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001603 : ESINT64VAL // These have same type and can't cause problems...
Reid Spencer950bf602007-01-26 08:19:09 +00001604 | EUINT64VAL {
1605 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
1606 error("Value too large for type");
1607 $$ = (int64_t)$1;
1608 };
Reid Spencere7c3c602006-11-30 06:36:44 +00001609
1610// Operations that are notably excluded from this list include:
1611// RET, BR, & SWITCH because they end basic blocks and are treated specially.
Reid Spencer950bf602007-01-26 08:19:09 +00001612//
1613ArithmeticOps
1614 : ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV | REM | UREM | SREM | FREM
1615 ;
1616
1617LogicalOps
1618 : AND | OR | XOR
1619 ;
1620
1621SetCondOps
1622 : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
1623 ;
1624
1625IPredicates
1626 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
1627 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1628 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1629 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1630 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1631 ;
1632
1633FPredicates
1634 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1635 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1636 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1637 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1638 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1639 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1640 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1641 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1642 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1643 ;
1644ShiftOps
1645 : SHL | SHR | ASHR | LSHR
1646 ;
1647
1648CastOps
1649 : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI
1650 | UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
1651 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001652
1653// These are some types that allow classification if we only want a particular
1654// thing... for example, only a signed, unsigned, or integral type.
Reid Spencer950bf602007-01-26 08:19:09 +00001655SIntType
1656 : LONG | INT | SHORT | SBYTE
1657 ;
1658
1659UIntType
1660 : ULONG | UINT | USHORT | UBYTE
1661 ;
1662
1663IntType
1664 : SIntType | UIntType
1665 ;
1666
1667FPType
1668 : FLOAT | DOUBLE
1669 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001670
1671// OptAssign - Value producing statements have an optional assignment component
Reid Spencer950bf602007-01-26 08:19:09 +00001672OptAssign
1673 : Name '=' {
Reid Spencere7c3c602006-11-30 06:36:44 +00001674 $$ = $1;
1675 }
1676 | /*empty*/ {
Reid Spencer950bf602007-01-26 08:19:09 +00001677 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001678 };
1679
1680OptLinkage
Reid Spencer785a5ae2007-02-08 00:21:40 +00001681 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencer950bf602007-01-26 08:19:09 +00001682 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1683 | WEAK { $$ = GlobalValue::WeakLinkage; }
1684 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1685 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1686 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencer785a5ae2007-02-08 00:21:40 +00001687 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencer950bf602007-01-26 08:19:09 +00001688 | /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1689 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001690
1691OptCallingConv
Reid Spencered96d1e2007-02-08 09:08:52 +00001692 : /*empty*/ { $$ = OldCallingConv::C; }
1693 | CCC_TOK { $$ = OldCallingConv::C; }
1694 | CSRETCC_TOK { $$ = OldCallingConv::CSRet; }
1695 | FASTCC_TOK { $$ = OldCallingConv::Fast; }
1696 | COLDCC_TOK { $$ = OldCallingConv::Cold; }
1697 | X86_STDCALLCC_TOK { $$ = OldCallingConv::X86_StdCall; }
1698 | X86_FASTCALLCC_TOK { $$ = OldCallingConv::X86_FastCall; }
Reid Spencer950bf602007-01-26 08:19:09 +00001699 | CC_TOK EUINT64VAL {
1700 if ((unsigned)$2 != $2)
1701 error("Calling conv too large");
1702 $$ = $2;
1703 }
1704 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001705
1706// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1707// a comma before it.
1708OptAlign
Reid Spencer950bf602007-01-26 08:19:09 +00001709 : /*empty*/ { $$ = 0; }
1710 | ALIGN EUINT64VAL {
1711 $$ = $2;
1712 if ($$ != 0 && !isPowerOf2_32($$))
1713 error("Alignment must be a power of two");
1714 }
1715 ;
Reid Spencerf0cf1322006-12-07 04:23:03 +00001716
Reid Spencere7c3c602006-11-30 06:36:44 +00001717OptCAlign
Reid Spencer950bf602007-01-26 08:19:09 +00001718 : /*empty*/ { $$ = 0; }
1719 | ',' ALIGN EUINT64VAL {
1720 $$ = $3;
1721 if ($$ != 0 && !isPowerOf2_32($$))
1722 error("Alignment must be a power of two");
1723 }
1724 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001725
1726SectionString
Reid Spencer950bf602007-01-26 08:19:09 +00001727 : SECTION STRINGCONSTANT {
1728 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1729 if ($2[i] == '"' || $2[i] == '\\')
1730 error("Invalid character in section name");
1731 $$ = $2;
1732 }
1733 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001734
Reid Spencer950bf602007-01-26 08:19:09 +00001735OptSection
1736 : /*empty*/ { $$ = 0; }
1737 | SectionString { $$ = $1; }
1738 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001739
Reid Spencer950bf602007-01-26 08:19:09 +00001740// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1741// is set to be the global we are processing.
1742//
Reid Spencere7c3c602006-11-30 06:36:44 +00001743GlobalVarAttributes
Reid Spencer950bf602007-01-26 08:19:09 +00001744 : /* empty */ {}
1745 | ',' GlobalVarAttribute GlobalVarAttributes {}
1746 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001747
Reid Spencer950bf602007-01-26 08:19:09 +00001748GlobalVarAttribute
1749 : SectionString {
1750 CurGV->setSection($1);
1751 free($1);
1752 }
1753 | ALIGN EUINT64VAL {
1754 if ($2 != 0 && !isPowerOf2_32($2))
1755 error("Alignment must be a power of two");
1756 CurGV->setAlignment($2);
1757
1758 }
1759 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001760
1761//===----------------------------------------------------------------------===//
1762// Types includes all predefined types... except void, because it can only be
1763// used in specific contexts (function returning void for example). To have
1764// access to it, a user must explicitly use TypesV.
1765//
1766
1767// TypesV includes all of 'Types', but it also includes the void type.
Reid Spencer950bf602007-01-26 08:19:09 +00001768TypesV
1769 : Types
1770 | VOID {
Reid Spencered96d1e2007-02-08 09:08:52 +00001771 $$.PAT = new PATypeHolder($1.T);
Reid Spencer950bf602007-01-26 08:19:09 +00001772 $$.S = Signless;
1773 }
1774 ;
1775
1776UpRTypesV
1777 : UpRTypes
1778 | VOID {
Reid Spencered96d1e2007-02-08 09:08:52 +00001779 $$.PAT = new PATypeHolder($1.T);
Reid Spencer950bf602007-01-26 08:19:09 +00001780 $$.S = Signless;
1781 }
1782 ;
1783
1784Types
1785 : UpRTypes {
1786 if (!UpRefs.empty())
Reid Spencered96d1e2007-02-08 09:08:52 +00001787 error("Invalid upreference in type: " + (*$1.PAT)->getDescription());
Reid Spencer950bf602007-01-26 08:19:09 +00001788 $$ = $1;
1789 }
1790 ;
1791
1792PrimType
1793 : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
1794 | LONG | ULONG | FLOAT | DOUBLE | LABEL
1795 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001796
1797// Derived types are added later...
Reid Spencera50d5962006-12-02 04:11:07 +00001798UpRTypes
Reid Spencer950bf602007-01-26 08:19:09 +00001799 : PrimType {
Reid Spencered96d1e2007-02-08 09:08:52 +00001800 $$.PAT = new PATypeHolder($1.T);
Reid Spencer950bf602007-01-26 08:19:09 +00001801 $$.S = $1.S;
Reid Spencera50d5962006-12-02 04:11:07 +00001802 }
Reid Spencer950bf602007-01-26 08:19:09 +00001803 | OPAQUE {
Reid Spencered96d1e2007-02-08 09:08:52 +00001804 $$.PAT = new PATypeHolder(OpaqueType::get());
Reid Spencer950bf602007-01-26 08:19:09 +00001805 $$.S = Signless;
1806 }
1807 | SymbolicValueRef { // Named types are also simple types...
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001808 const Type* tmp = getType($1);
Reid Spencered96d1e2007-02-08 09:08:52 +00001809 $$.PAT = new PATypeHolder(tmp);
Reid Spencer950bf602007-01-26 08:19:09 +00001810 $$.S = Signless; // FIXME: what if its signed?
Reid Spencer78720742006-12-02 20:21:22 +00001811 }
1812 | '\\' EUINT64VAL { // Type UpReference
Reid Spencer950bf602007-01-26 08:19:09 +00001813 if ($2 > (uint64_t)~0U)
1814 error("Value out of range");
1815 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1816 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencered96d1e2007-02-08 09:08:52 +00001817 $$.PAT = new PATypeHolder(OT);
Reid Spencer950bf602007-01-26 08:19:09 +00001818 $$.S = Signless;
1819 UR_OUT("New Upreference!\n");
Reid Spencere7c3c602006-11-30 06:36:44 +00001820 }
1821 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Reid Spencer950bf602007-01-26 08:19:09 +00001822 std::vector<const Type*> Params;
1823 for (std::list<llvm::PATypeInfo>::iterator I = $3->begin(),
1824 E = $3->end(); I != E; ++I) {
Reid Spencered96d1e2007-02-08 09:08:52 +00001825 Params.push_back(I->PAT->get());
Reid Spencer52402b02007-01-02 05:45:11 +00001826 }
Reid Spencerb7046c72007-01-29 05:41:34 +00001827 FunctionType::ParamAttrsList ParamAttrs;
Reid Spencer950bf602007-01-26 08:19:09 +00001828 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1829 if (isVarArg) Params.pop_back();
1830
Reid Spencered96d1e2007-02-08 09:08:52 +00001831 $$.PAT = new PATypeHolder(
1832 HandleUpRefs(FunctionType::get($1.PAT->get(), Params, isVarArg,
1833 ParamAttrs)));
Reid Spencer950bf602007-01-26 08:19:09 +00001834 $$.S = $1.S;
Reid Spencered96d1e2007-02-08 09:08:52 +00001835 delete $1.PAT; // Delete the return type handle
Reid Spencer950bf602007-01-26 08:19:09 +00001836 delete $3; // Delete the argument list
Reid Spencere7c3c602006-11-30 06:36:44 +00001837 }
1838 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencered96d1e2007-02-08 09:08:52 +00001839 $$.PAT = new PATypeHolder(HandleUpRefs(ArrayType::get($4.PAT->get(),
Reid Spencer950bf602007-01-26 08:19:09 +00001840 (unsigned)$2)));
1841 $$.S = $4.S;
Reid Spencered96d1e2007-02-08 09:08:52 +00001842 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00001843 }
Chris Lattner4227bdb2007-02-19 07:34:02 +00001844 | '<' EUINT64VAL 'x' UpRTypes '>' { // Vector type?
Reid Spencered96d1e2007-02-08 09:08:52 +00001845 const llvm::Type* ElemTy = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00001846 if ((unsigned)$2 != $2)
1847 error("Unsigned result not equal to signed result");
1848 if (!(ElemTy->isInteger() || ElemTy->isFloatingPoint()))
Reid Spencer9d6565a2007-02-15 02:26:10 +00001849 error("Elements of a VectorType must be integer or floating point");
Reid Spencer950bf602007-01-26 08:19:09 +00001850 if (!isPowerOf2_32($2))
Reid Spencer9d6565a2007-02-15 02:26:10 +00001851 error("VectorType length should be a power of 2");
1852 $$.PAT = new PATypeHolder(HandleUpRefs(VectorType::get(ElemTy,
Reid Spencer950bf602007-01-26 08:19:09 +00001853 (unsigned)$2)));
1854 $$.S = $4.S;
Reid Spencered96d1e2007-02-08 09:08:52 +00001855 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00001856 }
1857 | '{' TypeListI '}' { // Structure type?
Reid Spencer950bf602007-01-26 08:19:09 +00001858 std::vector<const Type*> Elements;
1859 for (std::list<llvm::PATypeInfo>::iterator I = $2->begin(),
1860 E = $2->end(); I != E; ++I)
Reid Spencered96d1e2007-02-08 09:08:52 +00001861 Elements.push_back(I->PAT->get());
1862 $$.PAT = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Reid Spencer950bf602007-01-26 08:19:09 +00001863 $$.S = Signless;
1864 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001865 }
1866 | '{' '}' { // Empty structure type?
Reid Spencered96d1e2007-02-08 09:08:52 +00001867 $$.PAT = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer950bf602007-01-26 08:19:09 +00001868 $$.S = Signless;
Reid Spencere7c3c602006-11-30 06:36:44 +00001869 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001870 | '<' '{' TypeListI '}' '>' { // Packed Structure type?
Reid Spencer950bf602007-01-26 08:19:09 +00001871 std::vector<const Type*> Elements;
1872 for (std::list<llvm::PATypeInfo>::iterator I = $3->begin(),
1873 E = $3->end(); I != E; ++I) {
Reid Spencered96d1e2007-02-08 09:08:52 +00001874 Elements.push_back(I->PAT->get());
1875 delete I->PAT;
Reid Spencer52402b02007-01-02 05:45:11 +00001876 }
Reid Spencered96d1e2007-02-08 09:08:52 +00001877 $$.PAT = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
Reid Spencer950bf602007-01-26 08:19:09 +00001878 $$.S = Signless;
1879 delete $3;
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001880 }
1881 | '<' '{' '}' '>' { // Empty packed structure type?
Reid Spencered96d1e2007-02-08 09:08:52 +00001882 $$.PAT = new PATypeHolder(StructType::get(std::vector<const Type*>(),true));
Reid Spencer950bf602007-01-26 08:19:09 +00001883 $$.S = Signless;
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001884 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001885 | UpRTypes '*' { // Pointer type?
Reid Spencered96d1e2007-02-08 09:08:52 +00001886 if ($1.PAT->get() == Type::LabelTy)
Reid Spencer950bf602007-01-26 08:19:09 +00001887 error("Cannot form a pointer to a basic block");
Reid Spencered96d1e2007-02-08 09:08:52 +00001888 $$.PAT = new PATypeHolder(HandleUpRefs(PointerType::get($1.PAT->get())));
Reid Spencer950bf602007-01-26 08:19:09 +00001889 $$.S = $1.S;
Reid Spencered96d1e2007-02-08 09:08:52 +00001890 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00001891 }
1892 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001893
1894// TypeList - Used for struct declarations and as a basis for function type
1895// declaration type lists
1896//
Reid Spencere77e35e2006-12-01 20:26:20 +00001897TypeListI
1898 : UpRTypes {
Reid Spencer950bf602007-01-26 08:19:09 +00001899 $$ = new std::list<PATypeInfo>();
1900 $$->push_back($1);
Reid Spencere77e35e2006-12-01 20:26:20 +00001901 }
1902 | TypeListI ',' UpRTypes {
Reid Spencer950bf602007-01-26 08:19:09 +00001903 ($$=$1)->push_back($3);
1904 }
1905 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001906
1907// ArgTypeList - List of types for a function type declaration...
Reid Spencere77e35e2006-12-01 20:26:20 +00001908ArgTypeListI
Reid Spencer950bf602007-01-26 08:19:09 +00001909 : TypeListI
Reid Spencere7c3c602006-11-30 06:36:44 +00001910 | TypeListI ',' DOTDOTDOT {
Reid Spencer950bf602007-01-26 08:19:09 +00001911 PATypeInfo VoidTI;
Reid Spencered96d1e2007-02-08 09:08:52 +00001912 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencer950bf602007-01-26 08:19:09 +00001913 VoidTI.S = Signless;
1914 ($$=$1)->push_back(VoidTI);
Reid Spencere7c3c602006-11-30 06:36:44 +00001915 }
1916 | DOTDOTDOT {
Reid Spencer950bf602007-01-26 08:19:09 +00001917 $$ = new std::list<PATypeInfo>();
1918 PATypeInfo VoidTI;
Reid Spencered96d1e2007-02-08 09:08:52 +00001919 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencer950bf602007-01-26 08:19:09 +00001920 VoidTI.S = Signless;
1921 $$->push_back(VoidTI);
Reid Spencere7c3c602006-11-30 06:36:44 +00001922 }
1923 | /*empty*/ {
Reid Spencer950bf602007-01-26 08:19:09 +00001924 $$ = new std::list<PATypeInfo>();
1925 }
1926 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001927
1928// ConstVal - The various declarations that go into the constant pool. This
1929// production is used ONLY to represent constants that show up AFTER a 'const',
1930// 'constant' or 'global' token at global scope. Constants that can be inlined
1931// into other expressions (such as integers and constexprs) are handled by the
1932// ResolvedVal, ValueRef and ConstValueRef productions.
1933//
Reid Spencer950bf602007-01-26 08:19:09 +00001934ConstVal
1935 : Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencered96d1e2007-02-08 09:08:52 +00001936 const ArrayType *ATy = dyn_cast<ArrayType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00001937 if (ATy == 0)
1938 error("Cannot make array constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00001939 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00001940 const Type *ETy = ATy->getElementType();
1941 int NumElements = ATy->getNumElements();
1942
1943 // Verify that we have the correct size...
1944 if (NumElements != -1 && NumElements != (int)$3->size())
1945 error("Type mismatch: constant sized array initialized with " +
1946 utostr($3->size()) + " arguments, but has size of " +
1947 itostr(NumElements) + "");
1948
1949 // Verify all elements are correct type!
1950 std::vector<Constant*> Elems;
1951 for (unsigned i = 0; i < $3->size(); i++) {
1952 Constant *C = (*$3)[i].C;
1953 const Type* ValTy = C->getType();
1954 if (ETy != ValTy)
1955 error("Element #" + utostr(i) + " is not of type '" +
1956 ETy->getDescription() +"' as required!\nIt is of type '"+
1957 ValTy->getDescription() + "'");
1958 Elems.push_back(C);
1959 }
1960 $$.C = ConstantArray::get(ATy, Elems);
1961 $$.S = $1.S;
Reid Spencered96d1e2007-02-08 09:08:52 +00001962 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00001963 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001964 }
1965 | Types '[' ']' {
Reid Spencered96d1e2007-02-08 09:08:52 +00001966 const ArrayType *ATy = dyn_cast<ArrayType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00001967 if (ATy == 0)
1968 error("Cannot make array constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00001969 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00001970 int NumElements = ATy->getNumElements();
1971 if (NumElements != -1 && NumElements != 0)
1972 error("Type mismatch: constant sized array initialized with 0"
1973 " arguments, but has size of " + itostr(NumElements) +"");
1974 $$.C = ConstantArray::get(ATy, std::vector<Constant*>());
1975 $$.S = $1.S;
Reid Spencered96d1e2007-02-08 09:08:52 +00001976 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00001977 }
1978 | Types 'c' STRINGCONSTANT {
Reid Spencered96d1e2007-02-08 09:08:52 +00001979 const ArrayType *ATy = dyn_cast<ArrayType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00001980 if (ATy == 0)
1981 error("Cannot make array constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00001982 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00001983 int NumElements = ATy->getNumElements();
1984 const Type *ETy = dyn_cast<IntegerType>(ATy->getElementType());
1985 if (!ETy || cast<IntegerType>(ETy)->getBitWidth() != 8)
1986 error("String arrays require type i8, not '" + ETy->getDescription() +
1987 "'");
1988 char *EndStr = UnEscapeLexed($3, true);
1989 if (NumElements != -1 && NumElements != (EndStr-$3))
1990 error("Can't build string constant of size " +
1991 itostr((int)(EndStr-$3)) + " when array has size " +
1992 itostr(NumElements) + "");
1993 std::vector<Constant*> Vals;
1994 for (char *C = (char *)$3; C != (char *)EndStr; ++C)
1995 Vals.push_back(ConstantInt::get(ETy, *C));
1996 free($3);
1997 $$.C = ConstantArray::get(ATy, Vals);
1998 $$.S = $1.S;
Reid Spencered96d1e2007-02-08 09:08:52 +00001999 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002000 }
2001 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer9d6565a2007-02-15 02:26:10 +00002002 const VectorType *PTy = dyn_cast<VectorType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002003 if (PTy == 0)
2004 error("Cannot make packed constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002005 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002006 const Type *ETy = PTy->getElementType();
2007 int NumElements = PTy->getNumElements();
2008 // Verify that we have the correct size...
2009 if (NumElements != -1 && NumElements != (int)$3->size())
2010 error("Type mismatch: constant sized packed initialized with " +
2011 utostr($3->size()) + " arguments, but has size of " +
2012 itostr(NumElements) + "");
2013 // Verify all elements are correct type!
2014 std::vector<Constant*> Elems;
2015 for (unsigned i = 0; i < $3->size(); i++) {
2016 Constant *C = (*$3)[i].C;
2017 const Type* ValTy = C->getType();
2018 if (ETy != ValTy)
2019 error("Element #" + utostr(i) + " is not of type '" +
2020 ETy->getDescription() +"' as required!\nIt is of type '"+
2021 ValTy->getDescription() + "'");
2022 Elems.push_back(C);
2023 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00002024 $$.C = ConstantVector::get(PTy, Elems);
Reid Spencer950bf602007-01-26 08:19:09 +00002025 $$.S = $1.S;
Reid Spencered96d1e2007-02-08 09:08:52 +00002026 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002027 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002028 }
2029 | Types '{' ConstVector '}' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002030 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002031 if (STy == 0)
2032 error("Cannot make struct constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002033 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002034 if ($3->size() != STy->getNumContainedTypes())
2035 error("Illegal number of initializers for structure type");
2036
2037 // Check to ensure that constants are compatible with the type initializer!
2038 std::vector<Constant*> Fields;
2039 for (unsigned i = 0, e = $3->size(); i != e; ++i) {
2040 Constant *C = (*$3)[i].C;
2041 if (C->getType() != STy->getElementType(i))
2042 error("Expected type '" + STy->getElementType(i)->getDescription() +
2043 "' for element #" + utostr(i) + " of structure initializer");
2044 Fields.push_back(C);
2045 }
2046 $$.C = ConstantStruct::get(STy, Fields);
2047 $$.S = $1.S;
Reid Spencered96d1e2007-02-08 09:08:52 +00002048 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002049 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002050 }
2051 | Types '{' '}' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002052 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002053 if (STy == 0)
2054 error("Cannot make struct constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002055 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002056 if (STy->getNumContainedTypes() != 0)
2057 error("Illegal number of initializers for structure type");
2058 $$.C = ConstantStruct::get(STy, std::vector<Constant*>());
2059 $$.S = $1.S;
Reid Spencered96d1e2007-02-08 09:08:52 +00002060 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002061 }
Reid Spencer950bf602007-01-26 08:19:09 +00002062 | Types '<' '{' ConstVector '}' '>' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002063 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002064 if (STy == 0)
2065 error("Cannot make packed struct constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002066 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002067 if ($4->size() != STy->getNumContainedTypes())
2068 error("Illegal number of initializers for packed structure type");
Reid Spencere7c3c602006-11-30 06:36:44 +00002069
Reid Spencer950bf602007-01-26 08:19:09 +00002070 // Check to ensure that constants are compatible with the type initializer!
2071 std::vector<Constant*> Fields;
2072 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
2073 Constant *C = (*$4)[i].C;
2074 if (C->getType() != STy->getElementType(i))
2075 error("Expected type '" + STy->getElementType(i)->getDescription() +
2076 "' for element #" + utostr(i) + " of packed struct initializer");
2077 Fields.push_back(C);
Reid Spencer280d8012006-12-01 23:40:53 +00002078 }
Reid Spencer950bf602007-01-26 08:19:09 +00002079 $$.C = ConstantStruct::get(STy, Fields);
2080 $$.S = $1.S;
Reid Spencered96d1e2007-02-08 09:08:52 +00002081 delete $1.PAT;
Reid Spencere77e35e2006-12-01 20:26:20 +00002082 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00002083 }
Reid Spencer950bf602007-01-26 08:19:09 +00002084 | Types '<' '{' '}' '>' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002085 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002086 if (STy == 0)
2087 error("Cannot make packed struct constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002088 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002089 if (STy->getNumContainedTypes() != 0)
2090 error("Illegal number of initializers for packed structure type");
2091 $$.C = ConstantStruct::get(STy, std::vector<Constant*>());
2092 $$.S = $1.S;
Reid Spencered96d1e2007-02-08 09:08:52 +00002093 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002094 }
2095 | Types NULL_TOK {
Reid Spencered96d1e2007-02-08 09:08:52 +00002096 const PointerType *PTy = dyn_cast<PointerType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002097 if (PTy == 0)
2098 error("Cannot make null pointer constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002099 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002100 $$.C = ConstantPointerNull::get(PTy);
2101 $$.S = $1.S;
Reid Spencered96d1e2007-02-08 09:08:52 +00002102 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002103 }
2104 | Types UNDEF {
Reid Spencered96d1e2007-02-08 09:08:52 +00002105 $$.C = UndefValue::get($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002106 $$.S = $1.S;
Reid Spencered96d1e2007-02-08 09:08:52 +00002107 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002108 }
2109 | Types SymbolicValueRef {
Reid Spencered96d1e2007-02-08 09:08:52 +00002110 const PointerType *Ty = dyn_cast<PointerType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002111 if (Ty == 0)
2112 error("Global const reference must be a pointer type, not" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002113 $1.PAT->get()->getDescription());
Reid Spencer950bf602007-01-26 08:19:09 +00002114
2115 // ConstExprs can exist in the body of a function, thus creating
2116 // GlobalValues whenever they refer to a variable. Because we are in
2117 // the context of a function, getExistingValue will search the functions
2118 // symbol table instead of the module symbol table for the global symbol,
2119 // which throws things all off. To get around this, we just tell
2120 // getExistingValue that we are at global scope here.
2121 //
2122 Function *SavedCurFn = CurFun.CurrentFunction;
2123 CurFun.CurrentFunction = 0;
2124 Value *V = getExistingValue(Ty, $2);
2125 CurFun.CurrentFunction = SavedCurFn;
2126
2127 // If this is an initializer for a constant pointer, which is referencing a
2128 // (currently) undefined variable, create a stub now that shall be replaced
2129 // in the future with the right type of variable.
2130 //
2131 if (V == 0) {
2132 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers");
2133 const PointerType *PT = cast<PointerType>(Ty);
2134
2135 // First check to see if the forward references value is already created!
2136 PerModuleInfo::GlobalRefsType::iterator I =
2137 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
2138
2139 if (I != CurModule.GlobalRefs.end()) {
2140 V = I->second; // Placeholder already exists, use it...
2141 $2.destroy();
2142 } else {
2143 std::string Name;
2144 if ($2.Type == ValID::NameVal) Name = $2.Name;
2145
2146 // Create the forward referenced global.
2147 GlobalValue *GV;
2148 if (const FunctionType *FTy =
2149 dyn_cast<FunctionType>(PT->getElementType())) {
2150 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
2151 CurModule.CurrentModule);
2152 } else {
2153 GV = new GlobalVariable(PT->getElementType(), false,
2154 GlobalValue::ExternalLinkage, 0,
2155 Name, CurModule.CurrentModule);
2156 }
2157
2158 // Keep track of the fact that we have a forward ref to recycle it
2159 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
2160 V = GV;
2161 }
2162 }
2163 $$.C = cast<GlobalValue>(V);
2164 $$.S = $1.S;
Reid Spencered96d1e2007-02-08 09:08:52 +00002165 delete $1.PAT; // Free the type handle
Reid Spencer950bf602007-01-26 08:19:09 +00002166 }
2167 | Types ConstExpr {
Reid Spencered96d1e2007-02-08 09:08:52 +00002168 if ($1.PAT->get() != $2.C->getType())
Reid Spencer950bf602007-01-26 08:19:09 +00002169 error("Mismatched types for constant expression");
2170 $$ = $2;
2171 $$.S = $1.S;
Reid Spencered96d1e2007-02-08 09:08:52 +00002172 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002173 }
2174 | Types ZEROINITIALIZER {
Reid Spencered96d1e2007-02-08 09:08:52 +00002175 const Type *Ty = $1.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00002176 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
2177 error("Cannot create a null initialized value of this type");
2178 $$.C = Constant::getNullValue(Ty);
2179 $$.S = $1.S;
Reid Spencered96d1e2007-02-08 09:08:52 +00002180 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002181 }
2182 | SIntType EINT64VAL { // integral constants
2183 const Type *Ty = $1.T;
2184 if (!ConstantInt::isValueValidForType(Ty, $2))
2185 error("Constant value doesn't fit in type");
2186 $$.C = ConstantInt::get(Ty, $2);
2187 $$.S = Signed;
2188 }
2189 | UIntType EUINT64VAL { // integral constants
2190 const Type *Ty = $1.T;
2191 if (!ConstantInt::isValueValidForType(Ty, $2))
2192 error("Constant value doesn't fit in type");
2193 $$.C = ConstantInt::get(Ty, $2);
2194 $$.S = Unsigned;
2195 }
2196 | BOOL TRUETOK { // Boolean constants
2197 $$.C = ConstantInt::get(Type::Int1Ty, true);
2198 $$.S = Unsigned;
2199 }
2200 | BOOL FALSETOK { // Boolean constants
2201 $$.C = ConstantInt::get(Type::Int1Ty, false);
2202 $$.S = Unsigned;
2203 }
2204 | FPType FPVAL { // Float & Double constants
2205 if (!ConstantFP::isValueValidForType($1.T, $2))
2206 error("Floating point constant invalid for type");
2207 $$.C = ConstantFP::get($1.T, $2);
2208 $$.S = Signless;
2209 }
2210 ;
2211
2212ConstExpr
2213 : CastOps '(' ConstVal TO Types ')' {
2214 const Type* SrcTy = $3.C->getType();
Reid Spencered96d1e2007-02-08 09:08:52 +00002215 const Type* DstTy = $5.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00002216 Signedness SrcSign = $3.S;
2217 Signedness DstSign = $5.S;
2218 if (!SrcTy->isFirstClassType())
2219 error("cast constant expression from a non-primitive type: '" +
2220 SrcTy->getDescription() + "'");
2221 if (!DstTy->isFirstClassType())
2222 error("cast constant expression to a non-primitive type: '" +
2223 DstTy->getDescription() + "'");
2224 $$.C = cast<Constant>(getCast($1, $3.C, SrcSign, DstTy, DstSign));
2225 $$.S = DstSign;
Reid Spencered96d1e2007-02-08 09:08:52 +00002226 delete $5.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002227 }
2228 | GETELEMENTPTR '(' ConstVal IndexList ')' {
2229 const Type *Ty = $3.C->getType();
2230 if (!isa<PointerType>(Ty))
2231 error("GetElementPtr requires a pointer operand");
2232
2233 std::vector<Value*> VIndices;
2234 std::vector<Constant*> CIndices;
2235 upgradeGEPIndices($3.C->getType(), $4, VIndices, &CIndices);
2236
2237 delete $4;
Chris Lattner4227bdb2007-02-19 07:34:02 +00002238 $$.C = ConstantExpr::getGetElementPtr($3.C, &CIndices[0], CIndices.size());
Reid Spencer950bf602007-01-26 08:19:09 +00002239 $$.S = Signless;
2240 }
Reid Spencere7c3c602006-11-30 06:36:44 +00002241 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002242 if (!$3.C->getType()->isInteger() ||
2243 cast<IntegerType>($3.C->getType())->getBitWidth() != 1)
2244 error("Select condition must be bool type");
2245 if ($5.C->getType() != $7.C->getType())
2246 error("Select operand types must match");
2247 $$.C = ConstantExpr::getSelect($3.C, $5.C, $7.C);
2248 $$.S = Unsigned;
Reid Spencere7c3c602006-11-30 06:36:44 +00002249 }
2250 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002251 const Type *Ty = $3.C->getType();
2252 if (Ty != $5.C->getType())
2253 error("Binary operator types must match");
2254 // First, make sure we're dealing with the right opcode by upgrading from
2255 // obsolete versions.
2256 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $3.S);
2257
2258 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
2259 // To retain backward compatibility with these early compilers, we emit a
2260 // cast to the appropriate integer type automatically if we are in the
2261 // broken case. See PR424 for more information.
2262 if (!isa<PointerType>(Ty)) {
2263 $$.C = ConstantExpr::get(Opcode, $3.C, $5.C);
2264 } else {
2265 const Type *IntPtrTy = 0;
2266 switch (CurModule.CurrentModule->getPointerSize()) {
2267 case Module::Pointer32: IntPtrTy = Type::Int32Ty; break;
2268 case Module::Pointer64: IntPtrTy = Type::Int64Ty; break;
2269 default: error("invalid pointer binary constant expr");
2270 }
2271 $$.C = ConstantExpr::get(Opcode,
2272 ConstantExpr::getCast(Instruction::PtrToInt, $3.C, IntPtrTy),
2273 ConstantExpr::getCast(Instruction::PtrToInt, $5.C, IntPtrTy));
2274 $$.C = ConstantExpr::getCast(Instruction::IntToPtr, $$.C, Ty);
2275 }
2276 $$.S = $3.S;
Reid Spencere7c3c602006-11-30 06:36:44 +00002277 }
2278 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002279 const Type* Ty = $3.C->getType();
2280 if (Ty != $5.C->getType())
2281 error("Logical operator types must match");
2282 if (!Ty->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00002283 if (!isa<VectorType>(Ty) ||
2284 !cast<VectorType>(Ty)->getElementType()->isInteger())
Reid Spencer950bf602007-01-26 08:19:09 +00002285 error("Logical operator requires integer operands");
2286 }
2287 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $3.S);
2288 $$.C = ConstantExpr::get(Opcode, $3.C, $5.C);
2289 $$.S = $3.S;
Reid Spencere7c3c602006-11-30 06:36:44 +00002290 }
2291 | SetCondOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002292 const Type* Ty = $3.C->getType();
2293 if (Ty != $5.C->getType())
2294 error("setcc operand types must match");
2295 unsigned short pred;
2296 Instruction::OtherOps Opcode = getCompareOp($1, pred, Ty, $3.S);
2297 $$.C = ConstantExpr::getCompare(Opcode, $3.C, $5.C);
2298 $$.S = Unsigned;
Reid Spencere7c3c602006-11-30 06:36:44 +00002299 }
Reid Spencer57f28f92006-12-03 07:10:26 +00002300 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002301 if ($4.C->getType() != $6.C->getType())
2302 error("icmp operand types must match");
2303 $$.C = ConstantExpr::getCompare($2, $4.C, $6.C);
2304 $$.S = Unsigned;
Reid Spencer57f28f92006-12-03 07:10:26 +00002305 }
2306 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002307 if ($4.C->getType() != $6.C->getType())
2308 error("fcmp operand types must match");
2309 $$.C = ConstantExpr::getCompare($2, $4.C, $6.C);
2310 $$.S = Unsigned;
Reid Spencer229e9362006-12-02 22:14:11 +00002311 }
Reid Spencere7c3c602006-11-30 06:36:44 +00002312 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002313 if (!$5.C->getType()->isInteger() ||
2314 cast<IntegerType>($5.C->getType())->getBitWidth() != 8)
2315 error("Shift count for shift constant must be unsigned byte");
Reid Spencer832254e2007-02-02 02:16:23 +00002316 const Type* Ty = $3.C->getType();
Reid Spencer950bf602007-01-26 08:19:09 +00002317 if (!$3.C->getType()->isInteger())
2318 error("Shift constant expression requires integer operand");
Reid Spencer832254e2007-02-02 02:16:23 +00002319 Constant *ShiftAmt = ConstantExpr::getZExt($5.C, Ty);
2320 $$.C = ConstantExpr::get(getBinaryOp($1, Ty, $3.S), $3.C, ShiftAmt);
Reid Spencer950bf602007-01-26 08:19:09 +00002321 $$.S = $3.S;
Reid Spencere7c3c602006-11-30 06:36:44 +00002322 }
2323 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002324 if (!ExtractElementInst::isValidOperands($3.C, $5.C))
2325 error("Invalid extractelement operands");
2326 $$.C = ConstantExpr::getExtractElement($3.C, $5.C);
2327 $$.S = $3.S;
Reid Spencere7c3c602006-11-30 06:36:44 +00002328 }
2329 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002330 if (!InsertElementInst::isValidOperands($3.C, $5.C, $7.C))
2331 error("Invalid insertelement operands");
2332 $$.C = ConstantExpr::getInsertElement($3.C, $5.C, $7.C);
2333 $$.S = $3.S;
Reid Spencere7c3c602006-11-30 06:36:44 +00002334 }
2335 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002336 if (!ShuffleVectorInst::isValidOperands($3.C, $5.C, $7.C))
2337 error("Invalid shufflevector operands");
2338 $$.C = ConstantExpr::getShuffleVector($3.C, $5.C, $7.C);
2339 $$.S = $3.S;
2340 }
2341 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002342
2343
2344// ConstVector - A list of comma separated constants.
Reid Spencere77e35e2006-12-01 20:26:20 +00002345ConstVector
Reid Spencer950bf602007-01-26 08:19:09 +00002346 : ConstVector ',' ConstVal { ($$ = $1)->push_back($3); }
2347 | ConstVal {
2348 $$ = new std::vector<ConstInfo>();
2349 $$->push_back($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00002350 }
Reid Spencere77e35e2006-12-01 20:26:20 +00002351 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002352
2353
2354// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Reid Spencer950bf602007-01-26 08:19:09 +00002355GlobalType
2356 : GLOBAL { $$ = false; }
2357 | CONSTANT { $$ = true; }
2358 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002359
2360
2361//===----------------------------------------------------------------------===//
2362// Rules to match Modules
2363//===----------------------------------------------------------------------===//
2364
2365// Module rule: Capture the result of parsing the whole file into a result
2366// variable...
2367//
Reid Spencer950bf602007-01-26 08:19:09 +00002368Module
2369 : FunctionList {
2370 $$ = ParserResult = $1;
2371 CurModule.ModuleDone();
Reid Spencere7c3c602006-11-30 06:36:44 +00002372 }
Jeff Cohenac2dca92007-01-21 19:30:52 +00002373 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002374
Reid Spencer950bf602007-01-26 08:19:09 +00002375// FunctionList - A list of functions, preceeded by a constant pool.
2376//
2377FunctionList
2378 : FunctionList Function { $$ = $1; CurFun.FunctionDone(); }
2379 | FunctionList FunctionProto { $$ = $1; }
2380 | FunctionList MODULE ASM_TOK AsmBlock { $$ = $1; }
2381 | FunctionList IMPLEMENTATION { $$ = $1; }
2382 | ConstPool {
2383 $$ = CurModule.CurrentModule;
2384 // Emit an error if there are any unresolved types left.
2385 if (!CurModule.LateResolveTypes.empty()) {
2386 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
2387 if (DID.Type == ValID::NameVal) {
2388 error("Reference to an undefined type: '"+DID.getName() + "'");
2389 } else {
2390 error("Reference to an undefined type: #" + itostr(DID.Num));
2391 }
2392 }
2393 }
2394 ;
Reid Spencer78720742006-12-02 20:21:22 +00002395
Reid Spencere7c3c602006-11-30 06:36:44 +00002396// ConstPool - Constants with optional names assigned to them.
Reid Spencer950bf602007-01-26 08:19:09 +00002397ConstPool
2398 : ConstPool OptAssign TYPE TypesV {
2399 // Eagerly resolve types. This is not an optimization, this is a
2400 // requirement that is due to the fact that we could have this:
2401 //
2402 // %list = type { %list * }
2403 // %list = type { %list * } ; repeated type decl
2404 //
2405 // If types are not resolved eagerly, then the two types will not be
2406 // determined to be the same type!
2407 //
Reid Spencered96d1e2007-02-08 09:08:52 +00002408 const Type* Ty = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00002409 ResolveTypeTo($2, Ty);
2410
2411 if (!setTypeName(Ty, $2) && !$2) {
2412 // If this is a named type that is not a redefinition, add it to the slot
2413 // table.
2414 CurModule.Types.push_back(Ty);
Reid Spencera50d5962006-12-02 04:11:07 +00002415 }
Reid Spencered96d1e2007-02-08 09:08:52 +00002416 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002417 }
2418 | ConstPool FunctionProto { // Function prototypes can be in const pool
Reid Spencere7c3c602006-11-30 06:36:44 +00002419 }
2420 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
Reid Spencere7c3c602006-11-30 06:36:44 +00002421 }
Reid Spencer950bf602007-01-26 08:19:09 +00002422 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
2423 if ($5.C == 0)
2424 error("Global value initializer is not a constant");
2425 CurGV = ParseGlobalVariable($2, $3, $4, $5.C->getType(), $5.C);
2426 } GlobalVarAttributes {
2427 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002428 }
Reid Spencer950bf602007-01-26 08:19:09 +00002429 | ConstPool OptAssign EXTERNAL GlobalType Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00002430 const Type *Ty = $5.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00002431 CurGV = ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, Ty, 0);
Reid Spencered96d1e2007-02-08 09:08:52 +00002432 delete $5.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002433 } GlobalVarAttributes {
2434 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002435 }
Reid Spencer950bf602007-01-26 08:19:09 +00002436 | ConstPool OptAssign DLLIMPORT GlobalType Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00002437 const Type *Ty = $5.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00002438 CurGV = ParseGlobalVariable($2, GlobalValue::DLLImportLinkage, $4, Ty, 0);
Reid Spencered96d1e2007-02-08 09:08:52 +00002439 delete $5.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002440 } GlobalVarAttributes {
2441 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002442 }
Reid Spencer950bf602007-01-26 08:19:09 +00002443 | ConstPool OptAssign EXTERN_WEAK GlobalType Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00002444 const Type *Ty = $5.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00002445 CurGV =
2446 ParseGlobalVariable($2, GlobalValue::ExternalWeakLinkage, $4, Ty, 0);
Reid Spencered96d1e2007-02-08 09:08:52 +00002447 delete $5.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002448 } GlobalVarAttributes {
2449 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002450 }
2451 | ConstPool TARGET TargetDefinition {
Reid Spencere7c3c602006-11-30 06:36:44 +00002452 }
2453 | ConstPool DEPLIBS '=' LibrariesDefinition {
Reid Spencere7c3c602006-11-30 06:36:44 +00002454 }
2455 | /* empty: end of list */ {
Reid Spencer950bf602007-01-26 08:19:09 +00002456 }
2457 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002458
Reid Spencer950bf602007-01-26 08:19:09 +00002459AsmBlock
2460 : STRINGCONSTANT {
2461 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
2462 char *EndStr = UnEscapeLexed($1, true);
2463 std::string NewAsm($1, EndStr);
2464 free($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00002465
Reid Spencer950bf602007-01-26 08:19:09 +00002466 if (AsmSoFar.empty())
2467 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
2468 else
2469 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
2470 }
2471 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002472
Reid Spencer950bf602007-01-26 08:19:09 +00002473BigOrLittle
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00002474 : BIG { $$ = Module::BigEndian; }
Reid Spencer950bf602007-01-26 08:19:09 +00002475 | LITTLE { $$ = Module::LittleEndian; }
2476 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002477
2478TargetDefinition
2479 : ENDIAN '=' BigOrLittle {
Reid Spencer950bf602007-01-26 08:19:09 +00002480 CurModule.setEndianness($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00002481 }
2482 | POINTERSIZE '=' EUINT64VAL {
Reid Spencer950bf602007-01-26 08:19:09 +00002483 if ($3 == 32)
2484 CurModule.setPointerSize(Module::Pointer32);
2485 else if ($3 == 64)
2486 CurModule.setPointerSize(Module::Pointer64);
2487 else
2488 error("Invalid pointer size: '" + utostr($3) + "'");
Reid Spencere7c3c602006-11-30 06:36:44 +00002489 }
2490 | TRIPLE '=' STRINGCONSTANT {
Reid Spencer950bf602007-01-26 08:19:09 +00002491 CurModule.CurrentModule->setTargetTriple($3);
2492 free($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00002493 }
2494 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer950bf602007-01-26 08:19:09 +00002495 CurModule.CurrentModule->setDataLayout($3);
2496 free($3);
2497 }
2498 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002499
2500LibrariesDefinition
Reid Spencer950bf602007-01-26 08:19:09 +00002501 : '[' LibList ']'
2502 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002503
2504LibList
2505 : LibList ',' STRINGCONSTANT {
Reid Spencer950bf602007-01-26 08:19:09 +00002506 CurModule.CurrentModule->addLibrary($3);
2507 free($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00002508 }
Reid Spencer950bf602007-01-26 08:19:09 +00002509 | STRINGCONSTANT {
2510 CurModule.CurrentModule->addLibrary($1);
2511 free($1);
2512 }
2513 | /* empty: end of list */ { }
2514 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002515
2516//===----------------------------------------------------------------------===//
2517// Rules to match Function Headers
2518//===----------------------------------------------------------------------===//
2519
Reid Spencer950bf602007-01-26 08:19:09 +00002520Name
2521 : VAR_ID | STRINGCONSTANT
2522 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002523
Reid Spencer950bf602007-01-26 08:19:09 +00002524OptName
2525 : Name
2526 | /*empty*/ { $$ = 0; }
2527 ;
2528
2529ArgVal
2530 : Types OptName {
Reid Spencered96d1e2007-02-08 09:08:52 +00002531 if ($1.PAT->get() == Type::VoidTy)
Reid Spencer950bf602007-01-26 08:19:09 +00002532 error("void typed arguments are invalid");
2533 $$ = new std::pair<PATypeInfo, char*>($1, $2);
Reid Spencer52402b02007-01-02 05:45:11 +00002534 }
Reid Spencer950bf602007-01-26 08:19:09 +00002535 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002536
Reid Spencer950bf602007-01-26 08:19:09 +00002537ArgListH
2538 : ArgListH ',' ArgVal {
2539 $$ = $1;
2540 $$->push_back(*$3);
Reid Spencere77e35e2006-12-01 20:26:20 +00002541 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002542 }
2543 | ArgVal {
Reid Spencer950bf602007-01-26 08:19:09 +00002544 $$ = new std::vector<std::pair<PATypeInfo,char*> >();
2545 $$->push_back(*$1);
2546 delete $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00002547 }
Reid Spencer950bf602007-01-26 08:19:09 +00002548 ;
2549
2550ArgList
2551 : ArgListH { $$ = $1; }
Reid Spencere7c3c602006-11-30 06:36:44 +00002552 | ArgListH ',' DOTDOTDOT {
Reid Spencere7c3c602006-11-30 06:36:44 +00002553 $$ = $1;
Reid Spencer950bf602007-01-26 08:19:09 +00002554 PATypeInfo VoidTI;
Reid Spencered96d1e2007-02-08 09:08:52 +00002555 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencer950bf602007-01-26 08:19:09 +00002556 VoidTI.S = Signless;
2557 $$->push_back(std::pair<PATypeInfo, char*>(VoidTI, 0));
Reid Spencere7c3c602006-11-30 06:36:44 +00002558 }
2559 | DOTDOTDOT {
Reid Spencer950bf602007-01-26 08:19:09 +00002560 $$ = new std::vector<std::pair<PATypeInfo,char*> >();
2561 PATypeInfo VoidTI;
Reid Spencered96d1e2007-02-08 09:08:52 +00002562 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencer950bf602007-01-26 08:19:09 +00002563 VoidTI.S = Signless;
2564 $$->push_back(std::pair<PATypeInfo, char*>(VoidTI, 0));
Reid Spencere7c3c602006-11-30 06:36:44 +00002565 }
Reid Spencer950bf602007-01-26 08:19:09 +00002566 | /* empty */ { $$ = 0; }
2567 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002568
Reid Spencer71d2ec92006-12-31 06:02:26 +00002569FunctionHeaderH
2570 : OptCallingConv TypesV Name '(' ArgList ')' OptSection OptAlign {
Reid Spencer950bf602007-01-26 08:19:09 +00002571 UnEscapeLexed($3);
2572 std::string FunctionName($3);
2573 free($3); // Free strdup'd memory!
Reid Spencere7c3c602006-11-30 06:36:44 +00002574
Reid Spencered96d1e2007-02-08 09:08:52 +00002575 const Type* RetTy = $2.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00002576
2577 if (!RetTy->isFirstClassType() && RetTy != Type::VoidTy)
2578 error("LLVM functions cannot return aggregate types");
2579
Reid Spenceref9b9a72007-02-05 20:47:22 +00002580 std::vector<const Type*> ParamTyList;
Reid Spencer950bf602007-01-26 08:19:09 +00002581
2582 // In LLVM 2.0 the signatures of three varargs intrinsics changed to take
2583 // i8*. We check here for those names and override the parameter list
2584 // types to ensure the prototype is correct.
2585 if (FunctionName == "llvm.va_start" || FunctionName == "llvm.va_end") {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002586 ParamTyList.push_back(PointerType::get(Type::Int8Ty));
Reid Spencer950bf602007-01-26 08:19:09 +00002587 } else if (FunctionName == "llvm.va_copy") {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002588 ParamTyList.push_back(PointerType::get(Type::Int8Ty));
2589 ParamTyList.push_back(PointerType::get(Type::Int8Ty));
Reid Spencer950bf602007-01-26 08:19:09 +00002590 } else if ($5) { // If there are arguments...
2591 for (std::vector<std::pair<PATypeInfo,char*> >::iterator
2592 I = $5->begin(), E = $5->end(); I != E; ++I) {
Reid Spencered96d1e2007-02-08 09:08:52 +00002593 const Type *Ty = I->first.PAT->get();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002594 ParamTyList.push_back(Ty);
Reid Spencer950bf602007-01-26 08:19:09 +00002595 }
2596 }
2597
Reid Spenceref9b9a72007-02-05 20:47:22 +00002598 bool isVarArg = ParamTyList.size() && ParamTyList.back() == Type::VoidTy;
2599 if (isVarArg)
2600 ParamTyList.pop_back();
Reid Spencer950bf602007-01-26 08:19:09 +00002601
Reid Spencerb7046c72007-01-29 05:41:34 +00002602 // Convert the CSRet calling convention into the corresponding parameter
2603 // attribute.
2604 FunctionType::ParamAttrsList ParamAttrs;
2605 if ($1 == OldCallingConv::CSRet) {
2606 ParamAttrs.push_back(FunctionType::NoAttributeSet); // result
2607 ParamAttrs.push_back(FunctionType::StructRetAttribute); // first arg
2608 }
2609
Reid Spenceref9b9a72007-02-05 20:47:22 +00002610 const FunctionType *FT = FunctionType::get(RetTy, ParamTyList, isVarArg,
Reid Spencerb7046c72007-01-29 05:41:34 +00002611 ParamAttrs);
Reid Spencer950bf602007-01-26 08:19:09 +00002612 const PointerType *PFT = PointerType::get(FT);
Reid Spencered96d1e2007-02-08 09:08:52 +00002613 delete $2.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002614
2615 ValID ID;
2616 if (!FunctionName.empty()) {
2617 ID = ValID::create((char*)FunctionName.c_str());
2618 } else {
2619 ID = ValID::create((int)CurModule.Values[PFT].size());
2620 }
2621
2622 Function *Fn = 0;
Reid Spencered96d1e2007-02-08 09:08:52 +00002623 Module* M = CurModule.CurrentModule;
2624
Reid Spencer950bf602007-01-26 08:19:09 +00002625 // See if this function was forward referenced. If so, recycle the object.
2626 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2627 // Move the function to the end of the list, from whereever it was
2628 // previously inserted.
2629 Fn = cast<Function>(FWRef);
Reid Spencered96d1e2007-02-08 09:08:52 +00002630 M->getFunctionList().remove(Fn);
2631 M->getFunctionList().push_back(Fn);
2632 } else if (!FunctionName.empty()) {
2633 GlobalValue *Conflict = M->getFunction(FunctionName);
2634 if (!Conflict)
2635 Conflict = M->getNamedGlobal(FunctionName);
2636 if (Conflict && PFT == Conflict->getType()) {
2637 if (!CurFun.isDeclare && !Conflict->isDeclaration()) {
2638 // We have two function definitions that conflict, same type, same
2639 // name. We should really check to make sure that this is the result
2640 // of integer type planes collapsing and generate an error if it is
2641 // not, but we'll just rename on the assumption that it is. However,
2642 // let's do it intelligently and rename the internal linkage one
2643 // if there is one.
2644 std::string NewName(makeNameUnique(FunctionName));
2645 if (Conflict->hasInternalLinkage()) {
2646 Conflict->setName(NewName);
2647 RenameMapKey Key = std::make_pair(FunctionName,Conflict->getType());
2648 CurModule.RenameMap[Key] = NewName;
2649 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
2650 InsertValue(Fn, CurModule.Values);
2651 } else {
2652 Fn = new Function(FT, CurFun.Linkage, NewName, M);
2653 InsertValue(Fn, CurModule.Values);
2654 RenameMapKey Key = std::make_pair(FunctionName,PFT);
2655 CurModule.RenameMap[Key] = NewName;
2656 }
2657 } else {
2658 // If they are not both definitions, then just use the function we
2659 // found since the types are the same.
2660 Fn = cast<Function>(Conflict);
Reid Spenceref9b9a72007-02-05 20:47:22 +00002661
Reid Spencered96d1e2007-02-08 09:08:52 +00002662 // Make sure to strip off any argument names so we can't get
2663 // conflicts.
2664 if (Fn->isDeclaration())
2665 for (Function::arg_iterator AI = Fn->arg_begin(),
2666 AE = Fn->arg_end(); AI != AE; ++AI)
2667 AI->setName("");
2668 }
2669 } else if (Conflict) {
2670 // We have two globals with the same name and different types.
2671 // Previously, this was permitted because the symbol table had
2672 // "type planes" and names only needed to be distinct within a
2673 // type plane. After PR411 was fixed, this is no loner the case.
2674 // To resolve this we must rename one of the two.
2675 if (Conflict->hasInternalLinkage()) {
2676 // We can safely renamed the Conflict.
2677 Conflict->setName(makeNameUnique(Conflict->getName()));
2678 RenameMapKey Key = std::make_pair(FunctionName,Conflict->getType());
2679 CurModule.RenameMap[Key] = Conflict->getName();
2680 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
2681 InsertValue(Fn, CurModule.Values);
2682 } else if (CurFun.Linkage == GlobalValue::InternalLinkage) {
2683 // We can safely rename the function we're defining
2684 std::string NewName = makeNameUnique(FunctionName);
2685 Fn = new Function(FT, CurFun.Linkage, NewName, M);
2686 InsertValue(Fn, CurModule.Values);
2687 RenameMapKey Key = std::make_pair(FunctionName,PFT);
2688 CurModule.RenameMap[Key] = NewName;
2689 } else {
2690 // We can't quietly rename either of these things, but we must
2691 // rename one of them. Generate a warning about the renaming and
2692 // elect to rename the thing we're now defining.
2693 std::string NewName = makeNameUnique(FunctionName);
2694 warning("Renaming function '" + FunctionName + "' as '" + NewName +
2695 "' may cause linkage errors");
2696 Fn = new Function(FT, CurFun.Linkage, NewName, M);
2697 InsertValue(Fn, CurModule.Values);
2698 RenameMapKey Key = std::make_pair(FunctionName,PFT);
2699 CurModule.RenameMap[Key] = NewName;
2700 }
Reid Spenceref9b9a72007-02-05 20:47:22 +00002701 } else {
Reid Spencered96d1e2007-02-08 09:08:52 +00002702 // There's no conflict, just define the function
2703 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
2704 InsertValue(Fn, CurModule.Values);
Reid Spenceref9b9a72007-02-05 20:47:22 +00002705 }
Reid Spencer950bf602007-01-26 08:19:09 +00002706 }
2707
2708 CurFun.FunctionStart(Fn);
2709
2710 if (CurFun.isDeclare) {
2711 // If we have declaration, always overwrite linkage. This will allow us
2712 // to correctly handle cases, when pointer to function is passed as
2713 // argument to another function.
2714 Fn->setLinkage(CurFun.Linkage);
2715 }
Reid Spencerb7046c72007-01-29 05:41:34 +00002716 Fn->setCallingConv(upgradeCallingConv($1));
Reid Spencer950bf602007-01-26 08:19:09 +00002717 Fn->setAlignment($8);
2718 if ($7) {
2719 Fn->setSection($7);
2720 free($7);
2721 }
2722
2723 // Add all of the arguments we parsed to the function...
2724 if ($5) { // Is null if empty...
2725 if (isVarArg) { // Nuke the last entry
Reid Spencered96d1e2007-02-08 09:08:52 +00002726 assert($5->back().first.PAT->get() == Type::VoidTy &&
Reid Spencer950bf602007-01-26 08:19:09 +00002727 $5->back().second == 0 && "Not a varargs marker");
Reid Spencered96d1e2007-02-08 09:08:52 +00002728 delete $5->back().first.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002729 $5->pop_back(); // Delete the last entry
2730 }
2731 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002732 Function::arg_iterator ArgEnd = Fn->arg_end();
2733 std::vector<std::pair<PATypeInfo,char*> >::iterator I = $5->begin();
2734 std::vector<std::pair<PATypeInfo,char*> >::iterator E = $5->end();
2735 for ( ; I != E && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencered96d1e2007-02-08 09:08:52 +00002736 delete I->first.PAT; // Delete the typeholder...
Reid Spencer950bf602007-01-26 08:19:09 +00002737 setValueName(ArgIt, I->second); // Insert arg into symtab...
2738 InsertValue(ArgIt);
2739 }
2740 delete $5; // We're now done with the argument list
2741 }
2742 }
2743 ;
2744
2745BEGIN
2746 : BEGINTOK | '{' // Allow BEGIN or '{' to start a function
Jeff Cohenac2dca92007-01-21 19:30:52 +00002747 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002748
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002749FunctionHeader
2750 : OptLinkage FunctionHeaderH BEGIN {
Reid Spencer950bf602007-01-26 08:19:09 +00002751 $$ = CurFun.CurrentFunction;
2752
2753 // Make sure that we keep track of the linkage type even if there was a
2754 // previous "declare".
2755 $$->setLinkage($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00002756 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002757 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002758
Reid Spencer950bf602007-01-26 08:19:09 +00002759END
2760 : ENDTOK | '}' // Allow end of '}' to end a function
2761 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002762
Reid Spencer950bf602007-01-26 08:19:09 +00002763Function
2764 : BasicBlockList END {
2765 $$ = $1;
2766 };
Reid Spencere7c3c602006-11-30 06:36:44 +00002767
Reid Spencere77e35e2006-12-01 20:26:20 +00002768FnDeclareLinkage
Reid Spencered96d1e2007-02-08 09:08:52 +00002769 : /*default*/ { $$ = GlobalValue::ExternalLinkage; }
2770 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
2771 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencere7c3c602006-11-30 06:36:44 +00002772 ;
2773
2774FunctionProto
Reid Spencered96d1e2007-02-08 09:08:52 +00002775 : DECLARE { CurFun.isDeclare = true; }
2776 FnDeclareLinkage { CurFun.Linkage = $3; } FunctionHeaderH {
Reid Spencer950bf602007-01-26 08:19:09 +00002777 $$ = CurFun.CurrentFunction;
2778 CurFun.FunctionDone();
2779
2780 }
2781 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002782
2783//===----------------------------------------------------------------------===//
2784// Rules to match Basic Blocks
2785//===----------------------------------------------------------------------===//
2786
Reid Spencer950bf602007-01-26 08:19:09 +00002787OptSideEffect
2788 : /* empty */ { $$ = false; }
2789 | SIDEEFFECT { $$ = true; }
2790 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002791
Reid Spencere77e35e2006-12-01 20:26:20 +00002792ConstValueRef
Reid Spencer950bf602007-01-26 08:19:09 +00002793 // A reference to a direct constant
2794 : ESINT64VAL { $$ = ValID::create($1); }
2795 | EUINT64VAL { $$ = ValID::create($1); }
2796 | FPVAL { $$ = ValID::create($1); }
2797 | TRUETOK { $$ = ValID::create(ConstantInt::get(Type::Int1Ty, true)); }
2798 | FALSETOK { $$ = ValID::create(ConstantInt::get(Type::Int1Ty, false)); }
2799 | NULL_TOK { $$ = ValID::createNull(); }
2800 | UNDEF { $$ = ValID::createUndef(); }
2801 | ZEROINITIALIZER { $$ = ValID::createZeroInit(); }
2802 | '<' ConstVector '>' { // Nonempty unsized packed vector
2803 const Type *ETy = (*$2)[0].C->getType();
2804 int NumElements = $2->size();
Reid Spencer9d6565a2007-02-15 02:26:10 +00002805 VectorType* pt = VectorType::get(ETy, NumElements);
Reid Spencer950bf602007-01-26 08:19:09 +00002806 PATypeHolder* PTy = new PATypeHolder(
Reid Spencer9d6565a2007-02-15 02:26:10 +00002807 HandleUpRefs(VectorType::get(ETy, NumElements)));
Reid Spencer950bf602007-01-26 08:19:09 +00002808
2809 // Verify all elements are correct type!
2810 std::vector<Constant*> Elems;
2811 for (unsigned i = 0; i < $2->size(); i++) {
2812 Constant *C = (*$2)[i].C;
2813 const Type *CTy = C->getType();
2814 if (ETy != CTy)
2815 error("Element #" + utostr(i) + " is not of type '" +
2816 ETy->getDescription() +"' as required!\nIt is of type '" +
2817 CTy->getDescription() + "'");
2818 Elems.push_back(C);
Reid Spencere7c3c602006-11-30 06:36:44 +00002819 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00002820 $$ = ValID::create(ConstantVector::get(pt, Elems));
Reid Spencer950bf602007-01-26 08:19:09 +00002821 delete PTy; delete $2;
2822 }
2823 | ConstExpr {
2824 $$ = ValID::create($1.C);
2825 }
2826 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
2827 char *End = UnEscapeLexed($3, true);
2828 std::string AsmStr = std::string($3, End);
2829 End = UnEscapeLexed($5, true);
2830 std::string Constraints = std::string($5, End);
2831 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
2832 free($3);
2833 free($5);
2834 }
2835 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002836
Reid Spencer950bf602007-01-26 08:19:09 +00002837// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2838// another value.
2839//
2840SymbolicValueRef
2841 : INTVAL { $$ = ValID::create($1); }
2842 | Name { $$ = ValID::create($1); }
2843 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002844
2845// ValueRef - A reference to a definition... either constant or symbolic
Reid Spencerf459d392006-12-02 16:19:52 +00002846ValueRef
Reid Spencer950bf602007-01-26 08:19:09 +00002847 : SymbolicValueRef | ConstValueRef
Reid Spencerf459d392006-12-02 16:19:52 +00002848 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002849
Reid Spencer950bf602007-01-26 08:19:09 +00002850
Reid Spencere7c3c602006-11-30 06:36:44 +00002851// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2852// type immediately preceeds the value reference, and allows complex constant
2853// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Reid Spencer950bf602007-01-26 08:19:09 +00002854ResolvedVal
2855 : Types ValueRef {
Reid Spencered96d1e2007-02-08 09:08:52 +00002856 const Type *Ty = $1.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00002857 $$.S = $1.S;
2858 $$.V = getVal(Ty, $2);
Reid Spencered96d1e2007-02-08 09:08:52 +00002859 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002860 }
Reid Spencer950bf602007-01-26 08:19:09 +00002861 ;
2862
2863BasicBlockList
2864 : BasicBlockList BasicBlock {
2865 $$ = $1;
2866 }
2867 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
2868 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00002869 };
2870
2871
2872// Basic blocks are terminated by branching instructions:
2873// br, br/cc, switch, ret
2874//
Reid Spencer950bf602007-01-26 08:19:09 +00002875BasicBlock
2876 : InstructionList OptAssign BBTerminatorInst {
2877 setValueName($3, $2);
2878 InsertValue($3);
2879 $1->getInstList().push_back($3);
2880 InsertValue($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00002881 $$ = $1;
2882 }
Reid Spencer950bf602007-01-26 08:19:09 +00002883 ;
2884
2885InstructionList
2886 : InstructionList Inst {
2887 if ($2.I)
2888 $1->getInstList().push_back($2.I);
2889 $$ = $1;
2890 }
2891 | /* empty */ {
2892 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
2893 // Make sure to move the basic block to the correct location in the
2894 // function, instead of leaving it inserted wherever it was first
2895 // referenced.
2896 Function::BasicBlockListType &BBL =
2897 CurFun.CurrentFunction->getBasicBlockList();
2898 BBL.splice(BBL.end(), BBL, $$);
2899 }
2900 | LABELSTR {
2901 $$ = CurBB = getBBVal(ValID::create($1), true);
2902 // Make sure to move the basic block to the correct location in the
2903 // function, instead of leaving it inserted wherever it was first
2904 // referenced.
2905 Function::BasicBlockListType &BBL =
2906 CurFun.CurrentFunction->getBasicBlockList();
2907 BBL.splice(BBL.end(), BBL, $$);
2908 }
2909 ;
2910
2911Unwind : UNWIND | EXCEPT;
2912
2913BBTerminatorInst
2914 : RET ResolvedVal { // Return with a result...
2915 $$ = new ReturnInst($2.V);
2916 }
2917 | RET VOID { // Return with no result...
2918 $$ = new ReturnInst();
2919 }
2920 | BR LABEL ValueRef { // Unconditional Branch...
2921 BasicBlock* tmpBB = getBBVal($3);
2922 $$ = new BranchInst(tmpBB);
2923 } // Conditional Branch...
2924 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
2925 BasicBlock* tmpBBA = getBBVal($6);
2926 BasicBlock* tmpBBB = getBBVal($9);
2927 Value* tmpVal = getVal(Type::Int1Ty, $3);
2928 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
2929 }
2930 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
2931 Value* tmpVal = getVal($2.T, $3);
2932 BasicBlock* tmpBB = getBBVal($6);
2933 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
2934 $$ = S;
2935 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2936 E = $8->end();
2937 for (; I != E; ++I) {
2938 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2939 S->addCase(CI, I->second);
2940 else
2941 error("Switch case is constant, but not a simple integer");
2942 }
2943 delete $8;
2944 }
2945 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
2946 Value* tmpVal = getVal($2.T, $3);
2947 BasicBlock* tmpBB = getBBVal($6);
2948 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
2949 $$ = S;
2950 }
2951 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
2952 TO LABEL ValueRef Unwind LABEL ValueRef {
2953 const PointerType *PFTy;
2954 const FunctionType *Ty;
2955
Reid Spencered96d1e2007-02-08 09:08:52 +00002956 if (!(PFTy = dyn_cast<PointerType>($3.PAT->get())) ||
Reid Spencer950bf602007-01-26 08:19:09 +00002957 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2958 // Pull out the types of all of the arguments...
2959 std::vector<const Type*> ParamTypes;
2960 if ($6) {
2961 for (std::vector<ValueInfo>::iterator I = $6->begin(), E = $6->end();
2962 I != E; ++I)
2963 ParamTypes.push_back((*I).V->getType());
2964 }
Reid Spencerb7046c72007-01-29 05:41:34 +00002965 FunctionType::ParamAttrsList ParamAttrs;
2966 if ($2 == OldCallingConv::CSRet) {
2967 ParamAttrs.push_back(FunctionType::NoAttributeSet);
2968 ParamAttrs.push_back(FunctionType::StructRetAttribute);
2969 }
Reid Spencer950bf602007-01-26 08:19:09 +00002970 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2971 if (isVarArg) ParamTypes.pop_back();
Reid Spencered96d1e2007-02-08 09:08:52 +00002972 Ty = FunctionType::get($3.PAT->get(), ParamTypes, isVarArg, ParamAttrs);
Reid Spencer950bf602007-01-26 08:19:09 +00002973 PFTy = PointerType::get(Ty);
2974 }
2975 Value *V = getVal(PFTy, $4); // Get the function we're calling...
2976 BasicBlock *Normal = getBBVal($10);
2977 BasicBlock *Except = getBBVal($13);
2978
2979 // Create the call node...
2980 if (!$6) { // Has no arguments?
Chris Lattnercf3d0612007-02-13 06:04:17 +00002981 $$ = new InvokeInst(V, Normal, Except, 0, 0);
Reid Spencer950bf602007-01-26 08:19:09 +00002982 } else { // Has arguments?
2983 // Loop through FunctionType's arguments and ensure they are specified
2984 // correctly!
2985 //
2986 FunctionType::param_iterator I = Ty->param_begin();
2987 FunctionType::param_iterator E = Ty->param_end();
2988 std::vector<ValueInfo>::iterator ArgI = $6->begin(), ArgE = $6->end();
2989
2990 std::vector<Value*> Args;
2991 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2992 if ((*ArgI).V->getType() != *I)
2993 error("Parameter " +(*ArgI).V->getName()+ " is not of type '" +
2994 (*I)->getDescription() + "'");
2995 Args.push_back((*ArgI).V);
2996 }
2997
2998 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
2999 error("Invalid number of parameters detected");
3000
Chris Lattnercf3d0612007-02-13 06:04:17 +00003001 $$ = new InvokeInst(V, Normal, Except, &Args[0], Args.size());
Reid Spencer950bf602007-01-26 08:19:09 +00003002 }
Reid Spencerb7046c72007-01-29 05:41:34 +00003003 cast<InvokeInst>($$)->setCallingConv(upgradeCallingConv($2));
Reid Spencered96d1e2007-02-08 09:08:52 +00003004 delete $3.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003005 delete $6;
3006 }
3007 | Unwind {
3008 $$ = new UnwindInst();
3009 }
3010 | UNREACHABLE {
3011 $$ = new UnreachableInst();
3012 }
3013 ;
3014
3015JumpTable
3016 : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
3017 $$ = $1;
3018 Constant *V = cast<Constant>(getExistingValue($2.T, $3));
3019
3020 if (V == 0)
3021 error("May only switch on a constant pool value");
3022
3023 BasicBlock* tmpBB = getBBVal($6);
3024 $$->push_back(std::make_pair(V, tmpBB));
3025 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003026 | IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencer950bf602007-01-26 08:19:09 +00003027 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
3028 Constant *V = cast<Constant>(getExistingValue($1.T, $2));
3029
3030 if (V == 0)
3031 error("May only switch on a constant pool value");
3032
3033 BasicBlock* tmpBB = getBBVal($5);
3034 $$->push_back(std::make_pair(V, tmpBB));
3035 }
3036 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003037
3038Inst
3039 : OptAssign InstVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003040 bool omit = false;
3041 if ($1)
3042 if (BitCastInst *BCI = dyn_cast<BitCastInst>($2.I))
3043 if (BCI->getSrcTy() == BCI->getDestTy() &&
3044 BCI->getOperand(0)->getName() == $1)
3045 // This is a useless bit cast causing a name redefinition. It is
3046 // a bit cast from a type to the same type of an operand with the
3047 // same name as the name we would give this instruction. Since this
3048 // instruction results in no code generation, it is safe to omit
3049 // the instruction. This situation can occur because of collapsed
3050 // type planes. For example:
3051 // %X = add int %Y, %Z
3052 // %X = cast int %Y to uint
3053 // After upgrade, this looks like:
3054 // %X = add i32 %Y, %Z
3055 // %X = bitcast i32 to i32
3056 // The bitcast is clearly useless so we omit it.
3057 omit = true;
3058 if (omit) {
3059 $$.I = 0;
3060 $$.S = Signless;
3061 } else {
3062 setValueName($2.I, $1);
3063 InsertValue($2.I);
3064 $$ = $2;
Reid Spencerf5626a32007-01-01 01:20:41 +00003065 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003066 };
3067
Reid Spencer950bf602007-01-26 08:19:09 +00003068PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
3069 $$.P = new std::list<std::pair<Value*, BasicBlock*> >();
3070 $$.S = $1.S;
Reid Spencered96d1e2007-02-08 09:08:52 +00003071 Value* tmpVal = getVal($1.PAT->get(), $3);
Reid Spencer950bf602007-01-26 08:19:09 +00003072 BasicBlock* tmpBB = getBBVal($5);
3073 $$.P->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencered96d1e2007-02-08 09:08:52 +00003074 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003075 }
3076 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
Reid Spencere7c3c602006-11-30 06:36:44 +00003077 $$ = $1;
Reid Spencer950bf602007-01-26 08:19:09 +00003078 Value* tmpVal = getVal($1.P->front().first->getType(), $4);
3079 BasicBlock* tmpBB = getBBVal($6);
3080 $1.P->push_back(std::make_pair(tmpVal, tmpBB));
3081 }
3082 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003083
Reid Spencer950bf602007-01-26 08:19:09 +00003084ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
3085 $$ = new std::vector<ValueInfo>();
Reid Spencerf8483652006-12-02 15:16:01 +00003086 $$->push_back($1);
3087 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003088 | ValueRefList ',' ResolvedVal {
Reid Spencere7c3c602006-11-30 06:36:44 +00003089 $$ = $1;
Reid Spencer950bf602007-01-26 08:19:09 +00003090 $1->push_back($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00003091 };
3092
3093// ValueRefListE - Just like ValueRefList, except that it may also be empty!
3094ValueRefListE
Reid Spencer950bf602007-01-26 08:19:09 +00003095 : ValueRefList
3096 | /*empty*/ { $$ = 0; }
Reid Spencere7c3c602006-11-30 06:36:44 +00003097 ;
3098
3099OptTailCall
3100 : TAIL CALL {
Reid Spencer950bf602007-01-26 08:19:09 +00003101 $$ = true;
Reid Spencere7c3c602006-11-30 06:36:44 +00003102 }
Reid Spencer950bf602007-01-26 08:19:09 +00003103 | CALL {
3104 $$ = false;
3105 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003106 ;
3107
Reid Spencer950bf602007-01-26 08:19:09 +00003108InstVal
3109 : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencered96d1e2007-02-08 09:08:52 +00003110 const Type* Ty = $2.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003111 if (!Ty->isInteger() && !Ty->isFloatingPoint() && !isa<VectorType>(Ty))
Reid Spencer950bf602007-01-26 08:19:09 +00003112 error("Arithmetic operator requires integer, FP, or packed operands");
Reid Spencer9d6565a2007-02-15 02:26:10 +00003113 if (isa<VectorType>(Ty) &&
Reid Spencer950bf602007-01-26 08:19:09 +00003114 ($1 == URemOp || $1 == SRemOp || $1 == FRemOp || $1 == RemOp))
Chris Lattner4227bdb2007-02-19 07:34:02 +00003115 error("Remainder not supported on vector types");
Reid Spencer950bf602007-01-26 08:19:09 +00003116 // Upgrade the opcode from obsolete versions before we do anything with it.
3117 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $2.S);
3118 Value* val1 = getVal(Ty, $3);
3119 Value* val2 = getVal(Ty, $5);
3120 $$.I = BinaryOperator::create(Opcode, val1, val2);
3121 if ($$.I == 0)
3122 error("binary operator returned null");
3123 $$.S = $2.S;
Reid Spencered96d1e2007-02-08 09:08:52 +00003124 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003125 }
3126 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencered96d1e2007-02-08 09:08:52 +00003127 const Type *Ty = $2.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003128 if (!Ty->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00003129 if (!isa<VectorType>(Ty) ||
3130 !cast<VectorType>(Ty)->getElementType()->isInteger())
Reid Spencer950bf602007-01-26 08:19:09 +00003131 error("Logical operator requires integral operands");
3132 }
3133 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $2.S);
3134 Value* tmpVal1 = getVal(Ty, $3);
3135 Value* tmpVal2 = getVal(Ty, $5);
3136 $$.I = BinaryOperator::create(Opcode, tmpVal1, tmpVal2);
3137 if ($$.I == 0)
3138 error("binary operator returned null");
3139 $$.S = $2.S;
Reid Spencered96d1e2007-02-08 09:08:52 +00003140 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003141 }
3142 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencered96d1e2007-02-08 09:08:52 +00003143 const Type* Ty = $2.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003144 if(isa<VectorType>(Ty))
3145 error("VectorTypes currently not supported in setcc instructions");
Reid Spencer950bf602007-01-26 08:19:09 +00003146 unsigned short pred;
3147 Instruction::OtherOps Opcode = getCompareOp($1, pred, Ty, $2.S);
3148 Value* tmpVal1 = getVal(Ty, $3);
3149 Value* tmpVal2 = getVal(Ty, $5);
3150 $$.I = CmpInst::create(Opcode, pred, tmpVal1, tmpVal2);
3151 if ($$.I == 0)
3152 error("binary operator returned null");
3153 $$.S = Unsigned;
Reid Spencered96d1e2007-02-08 09:08:52 +00003154 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003155 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00003156 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencered96d1e2007-02-08 09:08:52 +00003157 const Type *Ty = $3.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003158 if (isa<VectorType>(Ty))
3159 error("VectorTypes currently not supported in icmp instructions");
Reid Spencer950bf602007-01-26 08:19:09 +00003160 else if (!Ty->isInteger() && !isa<PointerType>(Ty))
3161 error("icmp requires integer or pointer typed operands");
3162 Value* tmpVal1 = getVal(Ty, $4);
3163 Value* tmpVal2 = getVal(Ty, $6);
3164 $$.I = new ICmpInst($2, tmpVal1, tmpVal2);
3165 $$.S = Unsigned;
Reid Spencered96d1e2007-02-08 09:08:52 +00003166 delete $3.PAT;
Reid Spencer57f28f92006-12-03 07:10:26 +00003167 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00003168 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencered96d1e2007-02-08 09:08:52 +00003169 const Type *Ty = $3.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003170 if (isa<VectorType>(Ty))
3171 error("VectorTypes currently not supported in fcmp instructions");
Reid Spencer950bf602007-01-26 08:19:09 +00003172 else if (!Ty->isFloatingPoint())
3173 error("fcmp instruction requires floating point operands");
3174 Value* tmpVal1 = getVal(Ty, $4);
3175 Value* tmpVal2 = getVal(Ty, $6);
3176 $$.I = new FCmpInst($2, tmpVal1, tmpVal2);
3177 $$.S = Unsigned;
Reid Spencered96d1e2007-02-08 09:08:52 +00003178 delete $3.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003179 }
3180 | NOT ResolvedVal {
3181 warning("Use of obsolete 'not' instruction: Replacing with 'xor");
3182 const Type *Ty = $2.V->getType();
3183 Value *Ones = ConstantInt::getAllOnesValue(Ty);
3184 if (Ones == 0)
3185 error("Expected integral type for not instruction");
3186 $$.I = BinaryOperator::create(Instruction::Xor, $2.V, Ones);
3187 if ($$.I == 0)
3188 error("Could not create a xor instruction");
3189 $$.S = $2.S
Reid Spencer229e9362006-12-02 22:14:11 +00003190 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003191 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003192 if (!$4.V->getType()->isInteger() ||
3193 cast<IntegerType>($4.V->getType())->getBitWidth() != 8)
3194 error("Shift amount must be int8");
Reid Spencer832254e2007-02-02 02:16:23 +00003195 const Type* Ty = $2.V->getType();
3196 if (!Ty->isInteger())
Reid Spencer950bf602007-01-26 08:19:09 +00003197 error("Shift constant expression requires integer operand");
Reid Spencer832254e2007-02-02 02:16:23 +00003198 Value* ShiftAmt = 0;
3199 if (cast<IntegerType>(Ty)->getBitWidth() > Type::Int8Ty->getBitWidth())
3200 if (Constant *C = dyn_cast<Constant>($4.V))
3201 ShiftAmt = ConstantExpr::getZExt(C, Ty);
3202 else
3203 ShiftAmt = new ZExtInst($4.V, Ty, makeNameUnique("shift"), CurBB);
3204 else
3205 ShiftAmt = $4.V;
3206 $$.I = BinaryOperator::create(getBinaryOp($1, Ty, $2.S), $2.V, ShiftAmt);
Reid Spencer950bf602007-01-26 08:19:09 +00003207 $$.S = $2.S;
Reid Spencere7c3c602006-11-30 06:36:44 +00003208 }
Reid Spencerfcb5df82006-12-01 22:34:43 +00003209 | CastOps ResolvedVal TO Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00003210 const Type *DstTy = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003211 if (!DstTy->isFirstClassType())
3212 error("cast instruction to a non-primitive type: '" +
3213 DstTy->getDescription() + "'");
3214 $$.I = cast<Instruction>(getCast($1, $2.V, $2.S, DstTy, $4.S, true));
3215 $$.S = $4.S;
Reid Spencered96d1e2007-02-08 09:08:52 +00003216 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003217 }
3218 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003219 if (!$2.V->getType()->isInteger() ||
3220 cast<IntegerType>($2.V->getType())->getBitWidth() != 1)
3221 error("select condition must be bool");
3222 if ($4.V->getType() != $6.V->getType())
3223 error("select value types should match");
3224 $$.I = new SelectInst($2.V, $4.V, $6.V);
3225 $$.S = $2.S;
Reid Spencere7c3c602006-11-30 06:36:44 +00003226 }
3227 | VAARG ResolvedVal ',' Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00003228 const Type *Ty = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003229 NewVarArgs = true;
3230 $$.I = new VAArgInst($2.V, Ty);
3231 $$.S = $4.S;
Reid Spencered96d1e2007-02-08 09:08:52 +00003232 delete $4.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003233 }
3234 | VAARG_old ResolvedVal ',' Types {
3235 const Type* ArgTy = $2.V->getType();
Reid Spencered96d1e2007-02-08 09:08:52 +00003236 const Type* DstTy = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003237 ObsoleteVarArgs = true;
3238 Function* NF = cast<Function>(CurModule.CurrentModule->
3239 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0));
3240
3241 //b = vaarg a, t ->
3242 //foo = alloca 1 of t
3243 //bar = vacopy a
3244 //store bar -> foo
3245 //b = vaarg foo, t
3246 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix");
3247 CurBB->getInstList().push_back(foo);
3248 CallInst* bar = new CallInst(NF, $2.V);
3249 CurBB->getInstList().push_back(bar);
3250 CurBB->getInstList().push_back(new StoreInst(bar, foo));
3251 $$.I = new VAArgInst(foo, DstTy);
3252 $$.S = $4.S;
Reid Spencered96d1e2007-02-08 09:08:52 +00003253 delete $4.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003254 }
3255 | VANEXT_old ResolvedVal ',' Types {
3256 const Type* ArgTy = $2.V->getType();
Reid Spencered96d1e2007-02-08 09:08:52 +00003257 const Type* DstTy = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003258 ObsoleteVarArgs = true;
3259 Function* NF = cast<Function>(CurModule.CurrentModule->
3260 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0));
3261
3262 //b = vanext a, t ->
3263 //foo = alloca 1 of t
3264 //bar = vacopy a
3265 //store bar -> foo
3266 //tmp = vaarg foo, t
3267 //b = load foo
3268 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix");
3269 CurBB->getInstList().push_back(foo);
3270 CallInst* bar = new CallInst(NF, $2.V);
3271 CurBB->getInstList().push_back(bar);
3272 CurBB->getInstList().push_back(new StoreInst(bar, foo));
3273 Instruction* tmp = new VAArgInst(foo, DstTy);
3274 CurBB->getInstList().push_back(tmp);
3275 $$.I = new LoadInst(foo);
3276 $$.S = $4.S;
Reid Spencered96d1e2007-02-08 09:08:52 +00003277 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003278 }
3279 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003280 if (!ExtractElementInst::isValidOperands($2.V, $4.V))
3281 error("Invalid extractelement operands");
3282 $$.I = new ExtractElementInst($2.V, $4.V);
3283 $$.S = $2.S;
Reid Spencere7c3c602006-11-30 06:36:44 +00003284 }
3285 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003286 if (!InsertElementInst::isValidOperands($2.V, $4.V, $6.V))
3287 error("Invalid insertelement operands");
3288 $$.I = new InsertElementInst($2.V, $4.V, $6.V);
3289 $$.S = $2.S;
Reid Spencere7c3c602006-11-30 06:36:44 +00003290 }
3291 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003292 if (!ShuffleVectorInst::isValidOperands($2.V, $4.V, $6.V))
3293 error("Invalid shufflevector operands");
3294 $$.I = new ShuffleVectorInst($2.V, $4.V, $6.V);
3295 $$.S = $2.S;
Reid Spencere7c3c602006-11-30 06:36:44 +00003296 }
3297 | PHI_TOK PHIList {
Reid Spencer950bf602007-01-26 08:19:09 +00003298 const Type *Ty = $2.P->front().first->getType();
3299 if (!Ty->isFirstClassType())
3300 error("PHI node operands must be of first class type");
3301 PHINode *PHI = new PHINode(Ty);
3302 PHI->reserveOperandSpace($2.P->size());
3303 while ($2.P->begin() != $2.P->end()) {
3304 if ($2.P->front().first->getType() != Ty)
3305 error("All elements of a PHI node must be of the same type");
3306 PHI->addIncoming($2.P->front().first, $2.P->front().second);
3307 $2.P->pop_front();
3308 }
3309 $$.I = PHI;
3310 $$.S = $2.S;
3311 delete $2.P; // Free the list...
Reid Spencere7c3c602006-11-30 06:36:44 +00003312 }
3313 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00003314
3315 // Handle the short call syntax
3316 const PointerType *PFTy;
3317 const FunctionType *FTy;
Reid Spencered96d1e2007-02-08 09:08:52 +00003318 if (!(PFTy = dyn_cast<PointerType>($3.PAT->get())) ||
Reid Spencer950bf602007-01-26 08:19:09 +00003319 !(FTy = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3320 // Pull out the types of all of the arguments...
3321 std::vector<const Type*> ParamTypes;
3322 if ($6) {
3323 for (std::vector<ValueInfo>::iterator I = $6->begin(), E = $6->end();
3324 I != E; ++I)
3325 ParamTypes.push_back((*I).V->getType());
Reid Spencerc4d96252007-01-13 00:03:30 +00003326 }
Reid Spencer950bf602007-01-26 08:19:09 +00003327
Reid Spencerb7046c72007-01-29 05:41:34 +00003328 FunctionType::ParamAttrsList ParamAttrs;
3329 if ($2 == OldCallingConv::CSRet) {
3330 ParamAttrs.push_back(FunctionType::NoAttributeSet);
3331 ParamAttrs.push_back(FunctionType::StructRetAttribute);
3332 }
Reid Spencer950bf602007-01-26 08:19:09 +00003333 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
3334 if (isVarArg) ParamTypes.pop_back();
3335
Reid Spencered96d1e2007-02-08 09:08:52 +00003336 const Type *RetTy = $3.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003337 if (!RetTy->isFirstClassType() && RetTy != Type::VoidTy)
3338 error("Functions cannot return aggregate types");
3339
Reid Spencerb7046c72007-01-29 05:41:34 +00003340 FTy = FunctionType::get(RetTy, ParamTypes, isVarArg, ParamAttrs);
Reid Spencer950bf602007-01-26 08:19:09 +00003341 PFTy = PointerType::get(FTy);
Reid Spencerf8483652006-12-02 15:16:01 +00003342 }
Reid Spencer950bf602007-01-26 08:19:09 +00003343
3344 // First upgrade any intrinsic calls.
3345 std::vector<Value*> Args;
3346 if ($6)
3347 for (unsigned i = 0, e = $6->size(); i < e; ++i)
3348 Args.push_back((*$6)[i].V);
3349 Instruction *Inst = upgradeIntrinsicCall(FTy, $4, Args);
3350
3351 // If we got an upgraded intrinsic
3352 if (Inst) {
3353 $$.I = Inst;
3354 $$.S = Signless;
3355 } else {
3356 // Get the function we're calling
3357 Value *V = getVal(PFTy, $4);
3358
3359 // Check the argument values match
3360 if (!$6) { // Has no arguments?
3361 // Make sure no arguments is a good thing!
3362 if (FTy->getNumParams() != 0)
3363 error("No arguments passed to a function that expects arguments");
3364 } else { // Has arguments?
3365 // Loop through FunctionType's arguments and ensure they are specified
3366 // correctly!
3367 //
3368 FunctionType::param_iterator I = FTy->param_begin();
3369 FunctionType::param_iterator E = FTy->param_end();
3370 std::vector<ValueInfo>::iterator ArgI = $6->begin(), ArgE = $6->end();
3371
3372 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
3373 if ((*ArgI).V->getType() != *I)
3374 error("Parameter " +(*ArgI).V->getName()+ " is not of type '" +
3375 (*I)->getDescription() + "'");
3376
3377 if (I != E || (ArgI != ArgE && !FTy->isVarArg()))
3378 error("Invalid number of parameters detected");
3379 }
3380
3381 // Create the call instruction
Chris Lattnercf3d0612007-02-13 06:04:17 +00003382 CallInst *CI = new CallInst(V, &Args[0], Args.size());
Reid Spencer950bf602007-01-26 08:19:09 +00003383 CI->setTailCall($1);
Reid Spencerb7046c72007-01-29 05:41:34 +00003384 CI->setCallingConv(upgradeCallingConv($2));
Reid Spencer950bf602007-01-26 08:19:09 +00003385 $$.I = CI;
3386 $$.S = $3.S;
3387 }
Reid Spencered96d1e2007-02-08 09:08:52 +00003388 delete $3.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003389 delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00003390 }
Reid Spencer950bf602007-01-26 08:19:09 +00003391 | MemoryInst {
3392 $$ = $1;
3393 }
3394 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003395
3396
3397// IndexList - List of indices for GEP based instructions...
3398IndexList
Reid Spencer950bf602007-01-26 08:19:09 +00003399 : ',' ValueRefList { $$ = $2; }
3400 | /* empty */ { $$ = new std::vector<ValueInfo>(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00003401 ;
3402
3403OptVolatile
Reid Spencer950bf602007-01-26 08:19:09 +00003404 : VOLATILE { $$ = true; }
3405 | /* empty */ { $$ = false; }
Reid Spencere7c3c602006-11-30 06:36:44 +00003406 ;
3407
Reid Spencer950bf602007-01-26 08:19:09 +00003408MemoryInst
3409 : MALLOC Types OptCAlign {
Reid Spencered96d1e2007-02-08 09:08:52 +00003410 const Type *Ty = $2.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003411 $$.S = $2.S;
3412 $$.I = new MallocInst(Ty, 0, $3);
Reid Spencered96d1e2007-02-08 09:08:52 +00003413 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003414 }
3415 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencered96d1e2007-02-08 09:08:52 +00003416 const Type *Ty = $2.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003417 $$.S = $2.S;
3418 $$.I = new MallocInst(Ty, getVal($4.T, $5), $6);
Reid Spencered96d1e2007-02-08 09:08:52 +00003419 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003420 }
3421 | ALLOCA Types OptCAlign {
Reid Spencered96d1e2007-02-08 09:08:52 +00003422 const Type *Ty = $2.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003423 $$.S = $2.S;
3424 $$.I = new AllocaInst(Ty, 0, $3);
Reid Spencered96d1e2007-02-08 09:08:52 +00003425 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003426 }
3427 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencered96d1e2007-02-08 09:08:52 +00003428 const Type *Ty = $2.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003429 $$.S = $2.S;
3430 $$.I = new AllocaInst(Ty, getVal($4.T, $5), $6);
Reid Spencered96d1e2007-02-08 09:08:52 +00003431 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003432 }
3433 | FREE ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003434 const Type *PTy = $2.V->getType();
3435 if (!isa<PointerType>(PTy))
3436 error("Trying to free nonpointer type '" + PTy->getDescription() + "'");
3437 $$.I = new FreeInst($2.V);
3438 $$.S = Signless;
Reid Spencere7c3c602006-11-30 06:36:44 +00003439 }
3440 | OptVolatile LOAD Types ValueRef {
Reid Spencered96d1e2007-02-08 09:08:52 +00003441 const Type* Ty = $3.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003442 $$.S = $3.S;
3443 if (!isa<PointerType>(Ty))
3444 error("Can't load from nonpointer type: " + Ty->getDescription());
3445 if (!cast<PointerType>(Ty)->getElementType()->isFirstClassType())
3446 error("Can't load from pointer of non-first-class type: " +
3447 Ty->getDescription());
3448 Value* tmpVal = getVal(Ty, $4);
3449 $$.I = new LoadInst(tmpVal, "", $1);
Reid Spencered96d1e2007-02-08 09:08:52 +00003450 delete $3.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003451 }
3452 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
Reid Spencered96d1e2007-02-08 09:08:52 +00003453 const PointerType *PTy = dyn_cast<PointerType>($5.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00003454 if (!PTy)
3455 error("Can't store to a nonpointer type: " +
Reid Spencered96d1e2007-02-08 09:08:52 +00003456 $5.PAT->get()->getDescription());
Reid Spencer950bf602007-01-26 08:19:09 +00003457 const Type *ElTy = PTy->getElementType();
Reid Spencered96d1e2007-02-08 09:08:52 +00003458 Value *StoreVal = $3.V;
Reid Spencer950bf602007-01-26 08:19:09 +00003459 Value* tmpVal = getVal(PTy, $6);
Reid Spencered96d1e2007-02-08 09:08:52 +00003460 if (ElTy != $3.V->getType()) {
3461 StoreVal = handleSRetFuncTypeMerge($3.V, ElTy);
3462 if (!StoreVal)
3463 error("Can't store '" + $3.V->getType()->getDescription() +
3464 "' into space of type '" + ElTy->getDescription() + "'");
3465 else {
3466 PTy = PointerType::get(StoreVal->getType());
3467 if (Constant *C = dyn_cast<Constant>(tmpVal))
3468 tmpVal = ConstantExpr::getBitCast(C, PTy);
3469 else
3470 tmpVal = new BitCastInst(tmpVal, PTy, "upgrd.cast", CurBB);
3471 }
3472 }
3473 $$.I = new StoreInst(StoreVal, tmpVal, $1);
Reid Spencer950bf602007-01-26 08:19:09 +00003474 $$.S = Signless;
Reid Spencered96d1e2007-02-08 09:08:52 +00003475 delete $5.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003476 }
3477 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencered96d1e2007-02-08 09:08:52 +00003478 const Type* Ty = $2.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003479 if (!isa<PointerType>(Ty))
3480 error("getelementptr insn requires pointer operand");
3481
3482 std::vector<Value*> VIndices;
3483 upgradeGEPIndices(Ty, $4, VIndices);
3484
3485 Value* tmpVal = getVal(Ty, $3);
Chris Lattner1bc3fa62007-02-12 22:58:38 +00003486 $$.I = new GetElementPtrInst(tmpVal, &VIndices[0], VIndices.size());
Reid Spencer950bf602007-01-26 08:19:09 +00003487 $$.S = Signless;
Reid Spencered96d1e2007-02-08 09:08:52 +00003488 delete $2.PAT;
Reid Spencer30d0c582007-01-15 00:26:18 +00003489 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00003490 };
3491
Reid Spencer950bf602007-01-26 08:19:09 +00003492
Reid Spencere7c3c602006-11-30 06:36:44 +00003493%%
3494
3495int yyerror(const char *ErrorMsg) {
3496 std::string where
3497 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Reid Spencered96d1e2007-02-08 09:08:52 +00003498 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
Reid Spencer950bf602007-01-26 08:19:09 +00003499 std::string errMsg = where + "error: " + std::string(ErrorMsg);
3500 if (yychar != YYEMPTY && yychar != 0)
3501 errMsg += " while reading token '" + std::string(Upgradetext, Upgradeleng) +
3502 "'.";
Reid Spencer71d2ec92006-12-31 06:02:26 +00003503 std::cerr << "llvm-upgrade: " << errMsg << '\n';
Reid Spencer950bf602007-01-26 08:19:09 +00003504 std::cout << "llvm-upgrade: parse failed.\n";
Reid Spencere7c3c602006-11-30 06:36:44 +00003505 exit(1);
3506}
Reid Spencer319a7302007-01-05 17:20:02 +00003507
Reid Spencer30d0c582007-01-15 00:26:18 +00003508void warning(const std::string& ErrorMsg) {
Reid Spencer319a7302007-01-05 17:20:02 +00003509 std::string where
3510 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Reid Spencered96d1e2007-02-08 09:08:52 +00003511 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
Reid Spencer950bf602007-01-26 08:19:09 +00003512 std::string errMsg = where + "warning: " + std::string(ErrorMsg);
3513 if (yychar != YYEMPTY && yychar != 0)
3514 errMsg += " while reading token '" + std::string(Upgradetext, Upgradeleng) +
3515 "'.";
Reid Spencer319a7302007-01-05 17:20:02 +00003516 std::cerr << "llvm-upgrade: " << errMsg << '\n';
3517}
Reid Spencer950bf602007-01-26 08:19:09 +00003518
3519void error(const std::string& ErrorMsg, int LineNo) {
3520 if (LineNo == -1) LineNo = Upgradelineno;
3521 Upgradelineno = LineNo;
3522 yyerror(ErrorMsg.c_str());
3523}
3524