blob: 3eb00617f35e39a4000dd8ad2d79413eb968a0e2 [file] [log] [blame]
Reid Spencerefd53d52007-01-26 08:18:34 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
Reid Spencere7c3c602006-11-30 06:36:44 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4c06e0d2007-12-29 20:46:15 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencere7c3c602006-11-30 06:36:44 +00007//
8//===----------------------------------------------------------------------===//
9//
Reid Spencerefd53d52007-01-26 08:18:34 +000010// This file implements the bison parser for LLVM assembly languages files.
Reid Spencere7c3c602006-11-30 06:36:44 +000011//
12//===----------------------------------------------------------------------===//
13
14%{
Reid Spencer90eb4d62007-01-05 17:18:58 +000015#include "UpgradeInternals.h"
Reid Spencerefd53d52007-01-26 08:18:34 +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 Spencerefd53d52007-01-26 08:18:34 +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 Lattnerc02659f2007-02-11 21:39:35 +000026#include <map>
Reid Spencerefd53d52007-01-26 08:18:34 +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 Spencerefd53d52007-01-26 08:18:34 +000044int yylex();
Reid Spencere7c3c602006-11-30 06:36:44 +000045int yyparse();
46
Reid Spencerefd53d52007-01-26 08:18:34 +000047int yyerror(const char*);
48static void warning(const std::string& WarningMsg);
49
50namespace llvm {
51
Reid Spencerefd53d52007-01-26 08:18:34 +000052std::istream* LexInput;
Reid Spencere7c3c602006-11-30 06:36:44 +000053static std::string CurFilename;
Reid Spencer96839be2006-11-30 16:50:26 +000054
Reid Spencer05e52a12006-12-31 05:45:57 +000055// This bool controls whether attributes are ever added to function declarations
56// definitions and calls.
57static bool AddAttributes = false;
58
Reid Spencerefd53d52007-01-26 08:18:34 +000059static Module *ParserResult;
60static bool ObsoleteVarArgs;
61static bool NewVarArgs;
62static BasicBlock *CurBB;
63static GlobalVariable *CurGV;
Reid Spencer4f859aa2007-04-22 05:46:44 +000064static unsigned lastCallingConv;
Reid Spencera50d5962006-12-02 04:11:07 +000065
Reid Spencerefd53d52007-01-26 08:18:34 +000066// This contains info used when building the body of a function. It is
67// destroyed when the function is completed.
68//
69typedef std::vector<Value *> ValueList; // Numbered defs
70
Reid Spencer3e5affd2007-03-21 17:14:36 +000071typedef std::pair<std::string,TypeInfo> RenameMapKey;
Reid Spencerefd53d52007-01-26 08:18:34 +000072typedef std::map<RenameMapKey,std::string> RenameMapType;
73
74static void
75ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
76 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
77
78static struct PerModuleInfo {
79 Module *CurrentModule;
80 std::map<const Type *, ValueList> Values; // Module level numbered definitions
81 std::map<const Type *,ValueList> LateResolveValues;
Reid Spencer3e5affd2007-03-21 17:14:36 +000082 std::vector<PATypeHolder> Types;
83 std::vector<Signedness> TypeSigns;
84 std::map<std::string,Signedness> NamedTypeSigns;
85 std::map<std::string,Signedness> NamedValueSigns;
Reid Spencerefd53d52007-01-26 08:18:34 +000086 std::map<ValID, PATypeHolder> LateResolveTypes;
87 static Module::Endianness Endian;
88 static Module::PointerSize PointerSize;
89 RenameMapType RenameMap;
90
91 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
92 /// how they were referenced and on which line of the input they came from so
93 /// that we can resolve them later and print error messages as appropriate.
94 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
95
96 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
97 // references to global values. Global values may be referenced before they
98 // are defined, and if so, the temporary object that they represent is held
99 // here. This is used for forward references of GlobalValues.
100 //
101 typedef std::map<std::pair<const PointerType *, ValID>, GlobalValue*>
102 GlobalRefsType;
103 GlobalRefsType GlobalRefs;
104
105 void ModuleDone() {
106 // If we could not resolve some functions at function compilation time
107 // (calls to functions before they are defined), resolve them now... Types
108 // are resolved when the constant pool has been completely parsed.
109 //
110 ResolveDefinitions(LateResolveValues);
111
112 // Check to make sure that all global value forward references have been
113 // resolved!
114 //
115 if (!GlobalRefs.empty()) {
116 std::string UndefinedReferences = "Unresolved global references exist:\n";
117
118 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
119 I != E; ++I) {
120 UndefinedReferences += " " + I->first.first->getDescription() + " " +
121 I->first.second.getName() + "\n";
122 }
123 error(UndefinedReferences);
124 return;
125 }
126
127 if (CurrentModule->getDataLayout().empty()) {
128 std::string dataLayout;
129 if (Endian != Module::AnyEndianness)
130 dataLayout.append(Endian == Module::BigEndian ? "E" : "e");
131 if (PointerSize != Module::AnyPointerSize) {
132 if (!dataLayout.empty())
133 dataLayout += "-";
134 dataLayout.append(PointerSize == Module::Pointer64 ?
135 "p:64:64" : "p:32:32");
136 }
137 CurrentModule->setDataLayout(dataLayout);
138 }
139
140 Values.clear(); // Clear out function local definitions
141 Types.clear();
Reid Spencer3e5affd2007-03-21 17:14:36 +0000142 TypeSigns.clear();
143 NamedTypeSigns.clear();
144 NamedValueSigns.clear();
Reid Spencerefd53d52007-01-26 08:18:34 +0000145 CurrentModule = 0;
146 }
147
148 // GetForwardRefForGlobal - Check to see if there is a forward reference
149 // for this global. If so, remove it from the GlobalRefs map and return it.
150 // If not, just return null.
151 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
152 // Check to see if there is a forward reference to this global variable...
153 // if there is, eliminate it and patch the reference to use the new def'n.
154 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
155 GlobalValue *Ret = 0;
156 if (I != GlobalRefs.end()) {
157 Ret = I->second;
158 GlobalRefs.erase(I);
159 }
160 return Ret;
161 }
162 void setEndianness(Module::Endianness E) { Endian = E; }
163 void setPointerSize(Module::PointerSize sz) { PointerSize = sz; }
164} CurModule;
165
166Module::Endianness PerModuleInfo::Endian = Module::AnyEndianness;
167Module::PointerSize PerModuleInfo::PointerSize = Module::AnyPointerSize;
168
169static struct PerFunctionInfo {
170 Function *CurrentFunction; // Pointer to current function being created
171
172 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
173 std::map<const Type*, ValueList> LateResolveValues;
174 bool isDeclare; // Is this function a forward declararation?
175 GlobalValue::LinkageTypes Linkage;// Linkage for forward declaration.
176
177 /// BBForwardRefs - When we see forward references to basic blocks, keep
178 /// track of them here.
179 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
180 std::vector<BasicBlock*> NumberedBlocks;
181 RenameMapType RenameMap;
Reid Spencerefd53d52007-01-26 08:18:34 +0000182 unsigned NextBBNum;
183
184 inline PerFunctionInfo() {
185 CurrentFunction = 0;
186 isDeclare = false;
187 Linkage = GlobalValue::ExternalLinkage;
188 }
189
190 inline void FunctionStart(Function *M) {
191 CurrentFunction = M;
192 NextBBNum = 0;
193 }
194
195 void FunctionDone() {
196 NumberedBlocks.clear();
197
198 // Any forward referenced blocks left?
199 if (!BBForwardRefs.empty()) {
200 error("Undefined reference to label " +
201 BBForwardRefs.begin()->first->getName());
202 return;
203 }
204
205 // Resolve all forward references now.
206 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
207
208 Values.clear(); // Clear out function local definitions
209 RenameMap.clear();
Reid Spencerefd53d52007-01-26 08:18:34 +0000210 CurrentFunction = 0;
211 isDeclare = false;
212 Linkage = GlobalValue::ExternalLinkage;
213 }
214} CurFun; // Info for the current function...
215
216static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
217
Reid Spencer3e5affd2007-03-21 17:14:36 +0000218/// This function is just a utility to make a Key value for the rename map.
219/// The Key is a combination of the name, type, Signedness of the original
220/// value (global/function). This just constructs the key and ensures that
221/// named Signedness values are resolved to the actual Signedness.
222/// @brief Make a key for the RenameMaps
223static RenameMapKey makeRenameMapKey(const std::string &Name, const Type* Ty,
224 const Signedness &Sign) {
225 TypeInfo TI;
226 TI.T = Ty;
227 if (Sign.isNamed())
228 // Don't allow Named Signedness nodes because they won't match. The actual
229 // Signedness must be looked up in the NamedTypeSigns map.
230 TI.S.copy(CurModule.NamedTypeSigns[Sign.getName()]);
231 else
232 TI.S.copy(Sign);
233 return std::make_pair(Name, TI);
234}
235
Reid Spencerefd53d52007-01-26 08:18:34 +0000236
237//===----------------------------------------------------------------------===//
238// Code to handle definitions of all the types
239//===----------------------------------------------------------------------===//
240
241static int InsertValue(Value *V,
242 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
243 if (V->hasName()) return -1; // Is this a numbered definition?
244
245 // Yes, insert the value into the value table...
246 ValueList &List = ValueTab[V->getType()];
247 List.push_back(V);
248 return List.size()-1;
249}
250
Reid Spencer9373d272007-01-26 17:13:53 +0000251static const Type *getType(const ValID &D, bool DoNotImprovise = false) {
Reid Spencerefd53d52007-01-26 08:18:34 +0000252 switch (D.Type) {
253 case ValID::NumberVal: // Is it a numbered definition?
254 // Module constants occupy the lowest numbered slots...
255 if ((unsigned)D.Num < CurModule.Types.size()) {
256 return CurModule.Types[(unsigned)D.Num];
257 }
258 break;
259 case ValID::NameVal: // Is it a named definition?
260 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
Reid Spencerefd53d52007-01-26 08:18:34 +0000261 return N;
262 }
263 break;
264 default:
265 error("Internal parser error: Invalid symbol type reference");
266 return 0;
267 }
268
269 // If we reached here, we referenced either a symbol that we don't know about
270 // or an id number that hasn't been read yet. We may be referencing something
271 // forward, so just create an entry to be resolved later and get to it...
272 //
273 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
274
Reid Spencerefd53d52007-01-26 08:18:34 +0000275 if (inFunctionScope()) {
276 if (D.Type == ValID::NameVal) {
277 error("Reference to an undefined type: '" + D.getName() + "'");
278 return 0;
279 } else {
280 error("Reference to an undefined type: #" + itostr(D.Num));
281 return 0;
282 }
283 }
284
285 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
286 if (I != CurModule.LateResolveTypes.end())
287 return I->second;
288
289 Type *Typ = OpaqueType::get();
290 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
291 return Typ;
Reid Spencer3e5affd2007-03-21 17:14:36 +0000292}
293
294/// This is like the getType method except that instead of looking up the type
295/// for a given ID, it looks up that type's sign.
296/// @brief Get the signedness of a referenced type
297static Signedness getTypeSign(const ValID &D) {
298 switch (D.Type) {
299 case ValID::NumberVal: // Is it a numbered definition?
300 // Module constants occupy the lowest numbered slots...
301 if ((unsigned)D.Num < CurModule.TypeSigns.size()) {
302 return CurModule.TypeSigns[(unsigned)D.Num];
303 }
304 break;
305 case ValID::NameVal: { // Is it a named definition?
306 std::map<std::string,Signedness>::const_iterator I =
307 CurModule.NamedTypeSigns.find(D.Name);
308 if (I != CurModule.NamedTypeSigns.end())
309 return I->second;
310 // Perhaps its a named forward .. just cache the name
311 Signedness S;
312 S.makeNamed(D.Name);
313 return S;
314 }
315 default:
316 break;
317 }
318 // If we don't find it, its signless
319 Signedness S;
320 S.makeSignless();
321 return S;
322}
323
324/// This function is analagous to getElementType in LLVM. It provides the same
325/// function except that it looks up the Signedness instead of the type. This is
326/// used when processing GEP instructions that need to extract the type of an
327/// indexed struct/array/ptr member.
328/// @brief Look up an element's sign.
329static Signedness getElementSign(const ValueInfo& VI,
330 const std::vector<Value*> &Indices) {
331 const Type *Ptr = VI.V->getType();
332 assert(isa<PointerType>(Ptr) && "Need pointer type");
333
334 unsigned CurIdx = 0;
335 Signedness S(VI.S);
336 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
337 if (CurIdx == Indices.size())
338 break;
339
340 Value *Index = Indices[CurIdx++];
341 assert(!isa<PointerType>(CT) || CurIdx == 1 && "Invalid type");
342 Ptr = CT->getTypeAtIndex(Index);
343 if (const Type* Ty = Ptr->getForwardedType())
344 Ptr = Ty;
345 assert(S.isComposite() && "Bad Signedness type");
346 if (isa<StructType>(CT)) {
347 S = S.get(cast<ConstantInt>(Index)->getZExtValue());
348 } else {
349 S = S.get(0UL);
350 }
351 if (S.isNamed())
352 S = CurModule.NamedTypeSigns[S.getName()];
353 }
354 Signedness Result;
355 Result.makeComposite(S);
356 return Result;
357}
358
359/// This function just translates a ConstantInfo into a ValueInfo and calls
360/// getElementSign(ValueInfo,...). Its just a convenience.
361/// @brief ConstantInfo version of getElementSign.
362static Signedness getElementSign(const ConstInfo& CI,
363 const std::vector<Constant*> &Indices) {
364 ValueInfo VI;
365 VI.V = CI.C;
366 VI.S.copy(CI.S);
367 std::vector<Value*> Idx;
368 for (unsigned i = 0; i < Indices.size(); ++i)
369 Idx.push_back(Indices[i]);
370 Signedness result = getElementSign(VI, Idx);
371 VI.destroy();
372 return result;
373}
Reid Spencerefd53d52007-01-26 08:18:34 +0000374
375// getExistingValue - Look up the value specified by the provided type and
376// the provided ValID. If the value exists and has already been defined, return
377// it. Otherwise return null.
378//
379static Value *getExistingValue(const Type *Ty, const ValID &D) {
380 if (isa<FunctionType>(Ty)) {
381 error("Functions are not values and must be referenced as pointers");
382 }
383
384 switch (D.Type) {
385 case ValID::NumberVal: { // Is it a numbered definition?
386 unsigned Num = (unsigned)D.Num;
387
388 // Module constants occupy the lowest numbered slots...
389 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
390 if (VI != CurModule.Values.end()) {
391 if (Num < VI->second.size())
392 return VI->second[Num];
393 Num -= VI->second.size();
394 }
395
396 // Make sure that our type is within bounds
397 VI = CurFun.Values.find(Ty);
398 if (VI == CurFun.Values.end()) return 0;
399
400 // Check that the number is within bounds...
401 if (VI->second.size() <= Num) return 0;
402
403 return VI->second[Num];
404 }
405
406 case ValID::NameVal: { // Is it a named definition?
407 // Get the name out of the ID
Reid Spencer3e5affd2007-03-21 17:14:36 +0000408 RenameMapKey Key = makeRenameMapKey(D.Name, Ty, D.S);
409 Value *V = 0;
Reid Spencerefd53d52007-01-26 08:18:34 +0000410 if (inFunctionScope()) {
411 // See if the name was renamed
412 RenameMapType::const_iterator I = CurFun.RenameMap.find(Key);
413 std::string LookupName;
414 if (I != CurFun.RenameMap.end())
415 LookupName = I->second;
416 else
Reid Spencer3e5affd2007-03-21 17:14:36 +0000417 LookupName = D.Name;
Reid Spenceref9b9a72007-02-05 20:47:22 +0000418 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
419 V = SymTab.lookup(LookupName);
Reid Spencer3e5affd2007-03-21 17:14:36 +0000420 if (V && V->getType() != Ty)
Duncan Sandsdc024672007-11-27 13:23:08 +0000421 V = 0;
Reid Spencerefd53d52007-01-26 08:18:34 +0000422 }
423 if (!V) {
424 RenameMapType::const_iterator I = CurModule.RenameMap.find(Key);
425 std::string LookupName;
426 if (I != CurModule.RenameMap.end())
427 LookupName = I->second;
428 else
Reid Spencer3e5affd2007-03-21 17:14:36 +0000429 LookupName = D.Name;
Reid Spenceref9b9a72007-02-05 20:47:22 +0000430 V = CurModule.CurrentModule->getValueSymbolTable().lookup(LookupName);
Reid Spencer3e5affd2007-03-21 17:14:36 +0000431 if (V && V->getType() != Ty)
Duncan Sandsdc024672007-11-27 13:23:08 +0000432 V = 0;
Reid Spencerefd53d52007-01-26 08:18:34 +0000433 }
Reid Spenceref9b9a72007-02-05 20:47:22 +0000434 if (!V)
Reid Spencerefd53d52007-01-26 08:18:34 +0000435 return 0;
436
437 D.destroy(); // Free old strdup'd memory...
438 return V;
439 }
440
441 // Check to make sure that "Ty" is an integral type, and that our
442 // value will fit into the specified type...
443 case ValID::ConstSIntVal: // Is it a constant pool reference??
444 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
445 error("Signed integral constant '" + itostr(D.ConstPool64) +
446 "' is invalid for type '" + Ty->getDescription() + "'");
447 }
448 return ConstantInt::get(Ty, D.ConstPool64);
449
450 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
451 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
452 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64))
453 error("Integral constant '" + utostr(D.UConstPool64) +
454 "' is invalid or out of range");
455 else // This is really a signed reference. Transmogrify.
456 return ConstantInt::get(Ty, D.ConstPool64);
457 } else
458 return ConstantInt::get(Ty, D.UConstPool64);
459
460 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Dale Johannesen43421b32007-09-06 18:13:44 +0000461 if (!ConstantFP::isValueValidForType(Ty, *D.ConstPoolFP))
Reid Spencerefd53d52007-01-26 08:18:34 +0000462 error("FP constant invalid for type");
Dale Johannesen43421b32007-09-06 18:13:44 +0000463 // Lexer has no type info, so builds all FP constants as double.
464 // Fix this here.
465 if (Ty==Type::FloatTy)
466 D.ConstPoolFP->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
467 return ConstantFP::get(Ty, *D.ConstPoolFP);
Reid Spencerefd53d52007-01-26 08:18:34 +0000468
469 case ValID::ConstNullVal: // Is it a null value?
470 if (!isa<PointerType>(Ty))
471 error("Cannot create a a non pointer null");
472 return ConstantPointerNull::get(cast<PointerType>(Ty));
473
474 case ValID::ConstUndefVal: // Is it an undef value?
475 return UndefValue::get(Ty);
476
477 case ValID::ConstZeroVal: // Is it a zero value?
478 return Constant::getNullValue(Ty);
479
480 case ValID::ConstantVal: // Fully resolved constant?
481 if (D.ConstantValue->getType() != Ty)
482 error("Constant expression type different from required type");
483 return D.ConstantValue;
484
485 case ValID::InlineAsmVal: { // Inline asm expression
486 const PointerType *PTy = dyn_cast<PointerType>(Ty);
487 const FunctionType *FTy =
488 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
489 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints))
490 error("Invalid type for asm constraint string");
491 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
492 D.IAD->HasSideEffects);
493 D.destroy(); // Free InlineAsmDescriptor.
494 return IA;
495 }
496 default:
497 assert(0 && "Unhandled case");
498 return 0;
499 } // End of switch
500
501 assert(0 && "Unhandled case");
502 return 0;
503}
504
505// getVal - This function is identical to getExistingValue, except that if a
506// value is not already defined, it "improvises" by creating a placeholder var
507// that looks and acts just like the requested variable. When the value is
508// defined later, all uses of the placeholder variable are replaced with the
509// real thing.
510//
511static Value *getVal(const Type *Ty, const ValID &ID) {
512 if (Ty == Type::LabelTy)
513 error("Cannot use a basic block here");
514
515 // See if the value has already been defined.
516 Value *V = getExistingValue(Ty, ID);
517 if (V) return V;
518
519 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty))
520 error("Invalid use of a composite type");
521
522 // If we reached here, we referenced either a symbol that we don't know about
523 // or an id number that hasn't been read yet. We may be referencing something
524 // forward, so just create an entry to be resolved later and get to it...
Reid Spencerefd53d52007-01-26 08:18:34 +0000525 V = new Argument(Ty);
526
527 // Remember where this forward reference came from. FIXME, shouldn't we try
528 // to recycle these things??
529 CurModule.PlaceHolderInfo.insert(
Reid Spenceref9b9a72007-02-05 20:47:22 +0000530 std::make_pair(V, std::make_pair(ID, Upgradelineno)));
Reid Spencerefd53d52007-01-26 08:18:34 +0000531
532 if (inFunctionScope())
533 InsertValue(V, CurFun.LateResolveValues);
534 else
535 InsertValue(V, CurModule.LateResolveValues);
536 return V;
537}
538
Reid Spencerac6d6082007-02-08 09:07:25 +0000539/// @brief This just makes any name given to it unique, up to MAX_UINT times.
540static std::string makeNameUnique(const std::string& Name) {
541 static unsigned UniqueNameCounter = 1;
542 std::string Result(Name);
543 Result += ".upgrd." + llvm::utostr(UniqueNameCounter++);
544 return Result;
545}
546
Reid Spencerefd53d52007-01-26 08:18:34 +0000547/// getBBVal - This is used for two purposes:
548/// * If isDefinition is true, a new basic block with the specified ID is being
549/// defined.
550/// * If isDefinition is true, this is a reference to a basic block, which may
551/// or may not be a forward reference.
552///
553static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
554 assert(inFunctionScope() && "Can't get basic block at global scope");
555
556 std::string Name;
557 BasicBlock *BB = 0;
558 switch (ID.Type) {
559 default:
560 error("Illegal label reference " + ID.getName());
561 break;
562 case ValID::NumberVal: // Is it a numbered definition?
563 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
564 CurFun.NumberedBlocks.resize(ID.Num+1);
565 BB = CurFun.NumberedBlocks[ID.Num];
566 break;
567 case ValID::NameVal: // Is it a named definition?
568 Name = ID.Name;
Reid Spencer3e5affd2007-03-21 17:14:36 +0000569 if (Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name)) {
Reid Spencerac6d6082007-02-08 09:07:25 +0000570 if (N->getType() != Type::LabelTy) {
571 // Register names didn't use to conflict with basic block names
572 // because of type planes. Now they all have to be unique. So, we just
573 // rename the register and treat this name as if no basic block
574 // had been found.
Reid Spencer3e5affd2007-03-21 17:14:36 +0000575 RenameMapKey Key = makeRenameMapKey(ID.Name, N->getType(), ID.S);
Reid Spencerac6d6082007-02-08 09:07:25 +0000576 N->setName(makeNameUnique(N->getName()));
577 CurModule.RenameMap[Key] = N->getName();
578 BB = 0;
579 } else {
580 BB = cast<BasicBlock>(N);
581 }
Reid Spencerefd53d52007-01-26 08:18:34 +0000582 }
583 break;
584 }
585
586 // See if the block has already been defined.
587 if (BB) {
588 // If this is the definition of the block, make sure the existing value was
589 // just a forward reference. If it was a forward reference, there will be
590 // an entry for it in the PlaceHolderInfo map.
591 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
592 // The existing value was a definition, not a forward reference.
593 error("Redefinition of label " + ID.getName());
594
595 ID.destroy(); // Free strdup'd memory.
596 return BB;
597 }
598
599 // Otherwise this block has not been seen before.
600 BB = new BasicBlock("", CurFun.CurrentFunction);
601 if (ID.Type == ValID::NameVal) {
602 BB->setName(ID.Name);
603 } else {
604 CurFun.NumberedBlocks[ID.Num] = BB;
605 }
606
607 // If this is not a definition, keep track of it so we can use it as a forward
608 // reference.
609 if (!isDefinition) {
610 // Remember where this forward reference came from.
611 CurFun.BBForwardRefs[BB] = std::make_pair(ID, Upgradelineno);
612 } else {
613 // The forward declaration could have been inserted anywhere in the
614 // function: insert it into the correct place now.
615 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
616 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
617 }
618 ID.destroy();
619 return BB;
620}
621
622
623//===----------------------------------------------------------------------===//
624// Code to handle forward references in instructions
625//===----------------------------------------------------------------------===//
626//
627// This code handles the late binding needed with statements that reference
628// values not defined yet... for example, a forward branch, or the PHI node for
629// a loop body.
630//
631// This keeps a table (CurFun.LateResolveValues) of all such forward references
632// and back patchs after we are done.
633//
634
635// ResolveDefinitions - If we could not resolve some defs at parsing
636// time (forward branches, phi functions for loops, etc...) resolve the
637// defs now...
638//
639static void
640ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
641 std::map<const Type*,ValueList> *FutureLateResolvers) {
Reid Spencere7c326b2007-02-08 08:09:36 +0000642
Reid Spencerefd53d52007-01-26 08:18:34 +0000643 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
644 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
645 E = LateResolvers.end(); LRI != E; ++LRI) {
Reid Spencere7c326b2007-02-08 08:09:36 +0000646 const Type* Ty = LRI->first;
Reid Spencerefd53d52007-01-26 08:18:34 +0000647 ValueList &List = LRI->second;
648 while (!List.empty()) {
649 Value *V = List.back();
650 List.pop_back();
651
652 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
653 CurModule.PlaceHolderInfo.find(V);
654 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error");
655
656 ValID &DID = PHI->second.first;
657
Reid Spencere7c326b2007-02-08 08:09:36 +0000658 Value *TheRealValue = getExistingValue(Ty, DID);
Reid Spencerefd53d52007-01-26 08:18:34 +0000659 if (TheRealValue) {
660 V->replaceAllUsesWith(TheRealValue);
661 delete V;
662 CurModule.PlaceHolderInfo.erase(PHI);
663 } else if (FutureLateResolvers) {
664 // Functions have their unresolved items forwarded to the module late
665 // resolver table
666 InsertValue(V, *FutureLateResolvers);
667 } else {
668 if (DID.Type == ValID::NameVal) {
Reid Spencere7c326b2007-02-08 08:09:36 +0000669 error("Reference to an invalid definition: '" + DID.getName() +
670 "' of type '" + V->getType()->getDescription() + "'",
671 PHI->second.second);
Reid Spencerf049c672007-01-29 19:07:18 +0000672 return;
Reid Spencerefd53d52007-01-26 08:18:34 +0000673 } else {
674 error("Reference to an invalid definition: #" +
675 itostr(DID.Num) + " of type '" +
676 V->getType()->getDescription() + "'", PHI->second.second);
677 return;
678 }
679 }
680 }
681 }
682
683 LateResolvers.clear();
684}
685
Reid Spencer3e5affd2007-03-21 17:14:36 +0000686/// This function is used for type resolution and upref handling. When a type
687/// becomes concrete, this function is called to adjust the signedness for the
688/// concrete type.
689static void ResolveTypeSign(const Type* oldTy, const Signedness &Sign) {
690 std::string TyName = CurModule.CurrentModule->getTypeName(oldTy);
691 if (!TyName.empty())
692 CurModule.NamedTypeSigns[TyName] = Sign;
693}
694
695/// ResolveTypeTo - A brand new type was just declared. This means that (if
696/// name is not null) things referencing Name can be resolved. Otherwise,
697/// things refering to the number can be resolved. Do this now.
698static void ResolveTypeTo(char *Name, const Type *ToTy, const Signedness& Sign){
Reid Spencerefd53d52007-01-26 08:18:34 +0000699 ValID D;
Reid Spencer3e5affd2007-03-21 17:14:36 +0000700 if (Name)
701 D = ValID::create(Name);
702 else
703 D = ValID::create((int)CurModule.Types.size());
704 D.S.copy(Sign);
705
Reid Spencer84867442007-04-11 12:10:08 +0000706 if (Name)
707 CurModule.NamedTypeSigns[Name] = Sign;
Reid Spencerefd53d52007-01-26 08:18:34 +0000708
709 std::map<ValID, PATypeHolder>::iterator I =
710 CurModule.LateResolveTypes.find(D);
711 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencer3e5affd2007-03-21 17:14:36 +0000712 const Type *OldTy = I->second.get();
713 ((DerivedType*)OldTy)->refineAbstractTypeTo(ToTy);
Reid Spencerefd53d52007-01-26 08:18:34 +0000714 CurModule.LateResolveTypes.erase(I);
715 }
716}
717
Anton Korobeynikovce13b852007-01-28 15:25:24 +0000718/// This is the implementation portion of TypeHasInteger. It traverses the
719/// type given, avoiding recursive types, and returns true as soon as it finds
720/// an integer type. If no integer type is found, it returns false.
721static bool TypeHasIntegerI(const Type *Ty, std::vector<const Type*> Stack) {
722 // Handle some easy cases
723 if (Ty->isPrimitiveType() || (Ty->getTypeID() == Type::OpaqueTyID))
724 return false;
725 if (Ty->isInteger())
726 return true;
727 if (const SequentialType *STy = dyn_cast<SequentialType>(Ty))
728 return STy->getElementType()->isInteger();
729
730 // Avoid type structure recursion
731 for (std::vector<const Type*>::iterator I = Stack.begin(), E = Stack.end();
732 I != E; ++I)
733 if (Ty == *I)
734 return false;
735
736 // Push us on the type stack
737 Stack.push_back(Ty);
738
739 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
740 if (TypeHasIntegerI(FTy->getReturnType(), Stack))
741 return true;
742 FunctionType::param_iterator I = FTy->param_begin();
743 FunctionType::param_iterator E = FTy->param_end();
744 for (; I != E; ++I)
745 if (TypeHasIntegerI(*I, Stack))
746 return true;
747 return false;
748 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
749 StructType::element_iterator I = STy->element_begin();
750 StructType::element_iterator E = STy->element_end();
751 for (; I != E; ++I) {
752 if (TypeHasIntegerI(*I, Stack))
753 return true;
754 }
755 return false;
756 }
757 // There shouldn't be anything else, but its definitely not integer
758 assert(0 && "What type is this?");
759 return false;
760}
761
762/// This is the interface to TypeHasIntegerI. It just provides the type stack,
763/// to avoid recursion, and then calls TypeHasIntegerI.
764static inline bool TypeHasInteger(const Type *Ty) {
765 std::vector<const Type*> TyStack;
766 return TypeHasIntegerI(Ty, TyStack);
767}
768
Reid Spencerefd53d52007-01-26 08:18:34 +0000769// setValueName - Set the specified value to the name given. The name may be
770// null potentially, in which case this is a noop. The string passed in is
771// assumed to be a malloc'd string buffer, and is free'd by this function.
772//
Reid Spencer3e5affd2007-03-21 17:14:36 +0000773static void setValueName(const ValueInfo &V, char *NameStr) {
Reid Spencerefd53d52007-01-26 08:18:34 +0000774 if (NameStr) {
775 std::string Name(NameStr); // Copy string
776 free(NameStr); // Free old string
777
Reid Spencer3e5affd2007-03-21 17:14:36 +0000778 if (V.V->getType() == Type::VoidTy) {
Reid Spencerefd53d52007-01-26 08:18:34 +0000779 error("Can't assign name '" + Name + "' to value with void type");
780 return;
781 }
782
Reid Spencerefd53d52007-01-26 08:18:34 +0000783 assert(inFunctionScope() && "Must be in function scope");
784
785 // Search the function's symbol table for an existing value of this name
Reid Spenceref9b9a72007-02-05 20:47:22 +0000786 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
787 Value* Existing = ST.lookup(Name);
Reid Spencerefd53d52007-01-26 08:18:34 +0000788 if (Existing) {
Anton Korobeynikovce13b852007-01-28 15:25:24 +0000789 // An existing value of the same name was found. This might have happened
790 // because of the integer type planes collapsing in LLVM 2.0.
Reid Spencer3e5affd2007-03-21 17:14:36 +0000791 if (Existing->getType() == V.V->getType() &&
Anton Korobeynikovce13b852007-01-28 15:25:24 +0000792 !TypeHasInteger(Existing->getType())) {
793 // If the type does not contain any integers in them then this can't be
794 // a type plane collapsing issue. It truly is a redefinition and we
795 // should error out as the assembly is invalid.
796 error("Redefinition of value named '" + Name + "' of type '" +
Reid Spencer3e5affd2007-03-21 17:14:36 +0000797 V.V->getType()->getDescription() + "'");
Anton Korobeynikovce13b852007-01-28 15:25:24 +0000798 return;
Reid Spencerefd53d52007-01-26 08:18:34 +0000799 }
800 // In LLVM 2.0 we don't allow names to be re-used for any values in a
801 // function, regardless of Type. Previously re-use of names was okay as
802 // long as they were distinct types. With type planes collapsing because
803 // of the signedness change and because of PR411, this can no longer be
804 // supported. We must search the entire symbol table for a conflicting
805 // name and make the name unique. No warning is needed as this can't
806 // cause a problem.
807 std::string NewName = makeNameUnique(Name);
808 // We're changing the name but it will probably be used by other
809 // instructions as operands later on. Consequently we have to retain
810 // a mapping of the renaming that we're doing.
Reid Spencer3e5affd2007-03-21 17:14:36 +0000811 RenameMapKey Key = makeRenameMapKey(Name, V.V->getType(), V.S);
Reid Spencerefd53d52007-01-26 08:18:34 +0000812 CurFun.RenameMap[Key] = NewName;
813 Name = NewName;
814 }
815
816 // Set the name.
Reid Spencer3e5affd2007-03-21 17:14:36 +0000817 V.V->setName(Name);
Reid Spencerefd53d52007-01-26 08:18:34 +0000818 }
819}
820
821/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
822/// this is a declaration, otherwise it is a definition.
823static GlobalVariable *
824ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
825 bool isConstantGlobal, const Type *Ty,
Reid Spencer3e5affd2007-03-21 17:14:36 +0000826 Constant *Initializer,
827 const Signedness &Sign) {
Reid Spencerefd53d52007-01-26 08:18:34 +0000828 if (isa<FunctionType>(Ty))
829 error("Cannot declare global vars of function type");
830
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000831 const PointerType *PTy = PointerType::getUnqual(Ty);
Reid Spencerefd53d52007-01-26 08:18:34 +0000832
833 std::string Name;
834 if (NameStr) {
835 Name = NameStr; // Copy string
836 free(NameStr); // Free old string
837 }
838
839 // See if this global value was forward referenced. If so, recycle the
840 // object.
841 ValID ID;
842 if (!Name.empty()) {
Reid Spencer44f87ee2007-03-15 03:25:34 +0000843 ID = ValID::create((char*)Name.c_str());
Reid Spencerefd53d52007-01-26 08:18:34 +0000844 } else {
Reid Spencer44f87ee2007-03-15 03:25:34 +0000845 ID = ValID::create((int)CurModule.Values[PTy].size());
Reid Spencerefd53d52007-01-26 08:18:34 +0000846 }
Reid Spencer3e5affd2007-03-21 17:14:36 +0000847 ID.S.makeComposite(Sign);
Reid Spencerefd53d52007-01-26 08:18:34 +0000848
849 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
850 // Move the global to the end of the list, from whereever it was
851 // previously inserted.
852 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
853 CurModule.CurrentModule->getGlobalList().remove(GV);
854 CurModule.CurrentModule->getGlobalList().push_back(GV);
855 GV->setInitializer(Initializer);
856 GV->setLinkage(Linkage);
857 GV->setConstant(isConstantGlobal);
858 InsertValue(GV, CurModule.Values);
859 return GV;
860 }
861
862 // If this global has a name, check to see if there is already a definition
863 // of this global in the module and emit warnings if there are conflicts.
864 if (!Name.empty()) {
865 // The global has a name. See if there's an existing one of the same name.
Reid Spencer53bd7042007-04-16 02:56:33 +0000866 if (CurModule.CurrentModule->getNamedGlobal(Name) ||
867 CurModule.CurrentModule->getFunction(Name)) {
868 // We found an existing global of the same name. This isn't allowed
Reid Spencerefd53d52007-01-26 08:18:34 +0000869 // in LLVM 2.0. Consequently, we must alter the name of the global so it
870 // can at least compile. This can happen because of type planes
871 // There is alread a global of the same name which means there is a
872 // conflict. Let's see what we can do about it.
873 std::string NewName(makeNameUnique(Name));
Reid Spencer3e5affd2007-03-21 17:14:36 +0000874 if (Linkage != GlobalValue::InternalLinkage) {
Reid Spencerefd53d52007-01-26 08:18:34 +0000875 // The linkage of this gval is external so we can't reliably rename
876 // it because it could potentially create a linking problem.
877 // However, we can't leave the name conflict in the output either or
878 // it won't assemble with LLVM 2.0. So, all we can do is rename
879 // this one to something unique and emit a warning about the problem.
880 warning("Renaming global variable '" + Name + "' to '" + NewName +
881 "' may cause linkage errors");
882 }
883
884 // Put the renaming in the global rename map
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000885 RenameMapKey Key =
886 makeRenameMapKey(Name, PointerType::getUnqual(Ty), ID.S);
Reid Spencerefd53d52007-01-26 08:18:34 +0000887 CurModule.RenameMap[Key] = NewName;
888
889 // Rename it
890 Name = NewName;
891 }
892 }
893
894 // Otherwise there is no existing GV to use, create one now.
895 GlobalVariable *GV =
896 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
897 CurModule.CurrentModule);
898 InsertValue(GV, CurModule.Values);
Reid Spencer3e5affd2007-03-21 17:14:36 +0000899 // Remember the sign of this global.
900 CurModule.NamedValueSigns[Name] = ID.S;
Reid Spencerefd53d52007-01-26 08:18:34 +0000901 return GV;
902}
903
904// setTypeName - Set the specified type to the name given. The name may be
905// null potentially, in which case this is a noop. The string passed in is
906// assumed to be a malloc'd string buffer, and is freed by this function.
907//
908// This function returns true if the type has already been defined, but is
909// allowed to be redefined in the specified context. If the name is a new name
910// for the type plane, it is inserted and false is returned.
Reid Spencer3e5affd2007-03-21 17:14:36 +0000911static bool setTypeName(const PATypeInfo& TI, char *NameStr) {
Reid Spencerefd53d52007-01-26 08:18:34 +0000912 assert(!inFunctionScope() && "Can't give types function-local names");
913 if (NameStr == 0) return false;
914
915 std::string Name(NameStr); // Copy string
916 free(NameStr); // Free old string
917
Reid Spencer3e5affd2007-03-21 17:14:36 +0000918 const Type* Ty = TI.PAT->get();
919
Reid Spencerefd53d52007-01-26 08:18:34 +0000920 // We don't allow assigning names to void type
Reid Spencer3e5affd2007-03-21 17:14:36 +0000921 if (Ty == Type::VoidTy) {
Reid Spencerefd53d52007-01-26 08:18:34 +0000922 error("Can't assign name '" + Name + "' to the void type");
923 return false;
924 }
925
926 // Set the type name, checking for conflicts as we do so.
Reid Spencer3e5affd2007-03-21 17:14:36 +0000927 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, Ty);
928
929 // Save the sign information for later use
930 CurModule.NamedTypeSigns[Name] = TI.S;
Reid Spencerefd53d52007-01-26 08:18:34 +0000931
932 if (AlreadyExists) { // Inserting a name that is already defined???
933 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
934 assert(Existing && "Conflict but no matching type?");
935
936 // There is only one case where this is allowed: when we are refining an
937 // opaque type. In this case, Existing will be an opaque type.
938 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
939 // We ARE replacing an opaque type!
Reid Spencer3e5affd2007-03-21 17:14:36 +0000940 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(Ty);
Reid Spencerefd53d52007-01-26 08:18:34 +0000941 return true;
942 }
943
944 // Otherwise, this is an attempt to redefine a type. That's okay if
945 // the redefinition is identical to the original. This will be so if
946 // Existing and T point to the same Type object. In this one case we
947 // allow the equivalent redefinition.
Reid Spencer3e5affd2007-03-21 17:14:36 +0000948 if (Existing == Ty) return true; // Yes, it's equal.
Reid Spencerefd53d52007-01-26 08:18:34 +0000949
950 // Any other kind of (non-equivalent) redefinition is an error.
951 error("Redefinition of type named '" + Name + "' in the '" +
Reid Spencer3e5affd2007-03-21 17:14:36 +0000952 Ty->getDescription() + "' type plane");
Reid Spencerefd53d52007-01-26 08:18:34 +0000953 }
954
955 return false;
956}
957
958//===----------------------------------------------------------------------===//
959// Code for handling upreferences in type names...
960//
961
962// TypeContains - Returns true if Ty directly contains E in it.
963//
964static bool TypeContains(const Type *Ty, const Type *E) {
965 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
966 E) != Ty->subtype_end();
967}
968
969namespace {
970 struct UpRefRecord {
971 // NestingLevel - The number of nesting levels that need to be popped before
972 // this type is resolved.
973 unsigned NestingLevel;
974
975 // LastContainedTy - This is the type at the current binding level for the
976 // type. Every time we reduce the nesting level, this gets updated.
977 const Type *LastContainedTy;
978
979 // UpRefTy - This is the actual opaque type that the upreference is
980 // represented with.
981 OpaqueType *UpRefTy;
982
983 UpRefRecord(unsigned NL, OpaqueType *URTy)
Reid Spencer3e5affd2007-03-21 17:14:36 +0000984 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) { }
Reid Spencerefd53d52007-01-26 08:18:34 +0000985 };
986}
987
988// UpRefs - A list of the outstanding upreferences that need to be resolved.
989static std::vector<UpRefRecord> UpRefs;
990
991/// HandleUpRefs - Every time we finish a new layer of types, this function is
992/// called. It loops through the UpRefs vector, which is a list of the
993/// currently active types. For each type, if the up reference is contained in
994/// the newly completed type, we decrement the level count. When the level
995/// count reaches zero, the upreferenced type is the type that is passed in:
996/// thus we can complete the cycle.
997///
Reid Spencer3e5affd2007-03-21 17:14:36 +0000998static PATypeHolder HandleUpRefs(const Type *ty, const Signedness& Sign) {
Reid Spencerefd53d52007-01-26 08:18:34 +0000999 // If Ty isn't abstract, or if there are no up-references in it, then there is
1000 // nothing to resolve here.
1001 if (!ty->isAbstract() || UpRefs.empty()) return ty;
1002
1003 PATypeHolder Ty(ty);
1004 UR_OUT("Type '" << Ty->getDescription() <<
1005 "' newly formed. Resolving upreferences.\n" <<
1006 UpRefs.size() << " upreferences active!\n");
1007
1008 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
1009 // to zero), we resolve them all together before we resolve them to Ty. At
1010 // the end of the loop, if there is anything to resolve to Ty, it will be in
1011 // this variable.
1012 OpaqueType *TypeToResolve = 0;
1013
Reid Spencer3e5affd2007-03-21 17:14:36 +00001014 unsigned i = 0;
1015 for (; i != UpRefs.size(); ++i) {
Reid Spencerefd53d52007-01-26 08:18:34 +00001016 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Reid Spencer3e5affd2007-03-21 17:14:36 +00001017 << UpRefs[i].UpRefTy->getDescription() << ") = "
1018 << (TypeContains(Ty, UpRefs[i].UpRefTy) ? "true" : "false") << "\n");
Reid Spencerefd53d52007-01-26 08:18:34 +00001019 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
1020 // Decrement level of upreference
1021 unsigned Level = --UpRefs[i].NestingLevel;
1022 UpRefs[i].LastContainedTy = Ty;
1023 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
1024 if (Level == 0) { // Upreference should be resolved!
1025 if (!TypeToResolve) {
1026 TypeToResolve = UpRefs[i].UpRefTy;
1027 } else {
1028 UR_OUT(" * Resolving upreference for "
Reid Spencer3e5affd2007-03-21 17:14:36 +00001029 << UpRefs[i].UpRefTy->getDescription() << "\n";
1030 std::string OldName = UpRefs[i].UpRefTy->getDescription());
1031 ResolveTypeSign(UpRefs[i].UpRefTy, Sign);
Reid Spencerefd53d52007-01-26 08:18:34 +00001032 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
1033 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
1034 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
1035 }
1036 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
1037 --i; // Do not skip the next element...
1038 }
1039 }
1040 }
1041
1042 if (TypeToResolve) {
1043 UR_OUT(" * Resolving upreference for "
Reid Spencer3e5affd2007-03-21 17:14:36 +00001044 << UpRefs[i].UpRefTy->getDescription() << "\n";
Reid Spencerefd53d52007-01-26 08:18:34 +00001045 std::string OldName = TypeToResolve->getDescription());
Reid Spencer3e5affd2007-03-21 17:14:36 +00001046 ResolveTypeSign(TypeToResolve, Sign);
Reid Spencerefd53d52007-01-26 08:18:34 +00001047 TypeToResolve->refineAbstractTypeTo(Ty);
1048 }
1049
1050 return Ty;
1051}
1052
Reid Spencer3e5affd2007-03-21 17:14:36 +00001053bool Signedness::operator<(const Signedness &that) const {
1054 if (isNamed()) {
1055 if (that.isNamed())
1056 return *(this->name) < *(that.name);
1057 else
1058 return CurModule.NamedTypeSigns[*name] < that;
1059 } else if (that.isNamed()) {
1060 return *this < CurModule.NamedTypeSigns[*that.name];
1061 }
1062
1063 if (isComposite() && that.isComposite()) {
1064 if (sv->size() == that.sv->size()) {
1065 SignVector::const_iterator thisI = sv->begin(), thisE = sv->end();
1066 SignVector::const_iterator thatI = that.sv->begin(),
1067 thatE = that.sv->end();
1068 for (; thisI != thisE; ++thisI, ++thatI) {
1069 if (*thisI < *thatI)
1070 return true;
1071 else if (!(*thisI == *thatI))
1072 return false;
1073 }
1074 return false;
1075 }
1076 return sv->size() < that.sv->size();
1077 }
1078 return kind < that.kind;
1079}
1080
1081bool Signedness::operator==(const Signedness &that) const {
1082 if (isNamed())
1083 if (that.isNamed())
1084 return *(this->name) == *(that.name);
1085 else
1086 return CurModule.NamedTypeSigns[*(this->name)] == that;
1087 else if (that.isNamed())
1088 return *this == CurModule.NamedTypeSigns[*(that.name)];
1089 if (isComposite() && that.isComposite()) {
1090 if (sv->size() == that.sv->size()) {
1091 SignVector::const_iterator thisI = sv->begin(), thisE = sv->end();
1092 SignVector::const_iterator thatI = that.sv->begin(),
1093 thatE = that.sv->end();
1094 for (; thisI != thisE; ++thisI, ++thatI) {
1095 if (!(*thisI == *thatI))
1096 return false;
1097 }
1098 return true;
1099 }
1100 return false;
1101 }
1102 return kind == that.kind;
1103}
1104
1105void Signedness::copy(const Signedness &that) {
1106 if (that.isNamed()) {
1107 kind = Named;
1108 name = new std::string(*that.name);
1109 } else if (that.isComposite()) {
1110 kind = Composite;
1111 sv = new SignVector();
1112 *sv = *that.sv;
1113 } else {
1114 kind = that.kind;
1115 sv = 0;
1116 }
1117}
1118
1119void Signedness::destroy() {
1120 if (isNamed()) {
1121 delete name;
1122 } else if (isComposite()) {
1123 delete sv;
1124 }
1125}
1126
Evan Cheng2b484202007-03-22 07:43:51 +00001127#ifndef NDEBUG
Reid Spencer3e5affd2007-03-21 17:14:36 +00001128void Signedness::dump() const {
1129 if (isComposite()) {
1130 if (sv->size() == 1) {
1131 (*sv)[0].dump();
1132 std::cerr << "*";
1133 } else {
1134 std::cerr << "{ " ;
1135 for (unsigned i = 0; i < sv->size(); ++i) {
1136 if (i != 0)
1137 std::cerr << ", ";
1138 (*sv)[i].dump();
1139 }
1140 std::cerr << "} " ;
1141 }
1142 } else if (isNamed()) {
1143 std::cerr << *name;
1144 } else if (isSigned()) {
1145 std::cerr << "S";
1146 } else if (isUnsigned()) {
1147 std::cerr << "U";
1148 } else
1149 std::cerr << ".";
1150}
Evan Cheng2b484202007-03-22 07:43:51 +00001151#endif
Reid Spencer3e5affd2007-03-21 17:14:36 +00001152
Reid Spencerefd53d52007-01-26 08:18:34 +00001153static inline Instruction::TermOps
1154getTermOp(TermOps op) {
1155 switch (op) {
1156 default : assert(0 && "Invalid OldTermOp");
1157 case RetOp : return Instruction::Ret;
1158 case BrOp : return Instruction::Br;
1159 case SwitchOp : return Instruction::Switch;
1160 case InvokeOp : return Instruction::Invoke;
1161 case UnwindOp : return Instruction::Unwind;
1162 case UnreachableOp: return Instruction::Unreachable;
1163 }
1164}
1165
1166static inline Instruction::BinaryOps
Reid Spencer3e5affd2007-03-21 17:14:36 +00001167getBinaryOp(BinaryOps op, const Type *Ty, const Signedness& Sign) {
Reid Spencerefd53d52007-01-26 08:18:34 +00001168 switch (op) {
1169 default : assert(0 && "Invalid OldBinaryOps");
1170 case SetEQ :
1171 case SetNE :
1172 case SetLE :
1173 case SetGE :
1174 case SetLT :
1175 case SetGT : assert(0 && "Should use getCompareOp");
1176 case AddOp : return Instruction::Add;
1177 case SubOp : return Instruction::Sub;
1178 case MulOp : return Instruction::Mul;
1179 case DivOp : {
1180 // This is an obsolete instruction so we must upgrade it based on the
1181 // types of its operands.
1182 bool isFP = Ty->isFloatingPoint();
Reid Spencer9d6565a2007-02-15 02:26:10 +00001183 if (const VectorType* PTy = dyn_cast<VectorType>(Ty))
Reid Spencerac9dcb92007-02-15 03:39:18 +00001184 // If its a vector type we want to use the element type
Reid Spencerefd53d52007-01-26 08:18:34 +00001185 isFP = PTy->getElementType()->isFloatingPoint();
1186 if (isFP)
1187 return Instruction::FDiv;
Reid Spencer3e5affd2007-03-21 17:14:36 +00001188 else if (Sign.isSigned())
Reid Spencerefd53d52007-01-26 08:18:34 +00001189 return Instruction::SDiv;
1190 return Instruction::UDiv;
1191 }
1192 case UDivOp : return Instruction::UDiv;
1193 case SDivOp : return Instruction::SDiv;
1194 case FDivOp : return Instruction::FDiv;
1195 case RemOp : {
1196 // This is an obsolete instruction so we must upgrade it based on the
1197 // types of its operands.
1198 bool isFP = Ty->isFloatingPoint();
Reid Spencer9d6565a2007-02-15 02:26:10 +00001199 if (const VectorType* PTy = dyn_cast<VectorType>(Ty))
Reid Spencerac9dcb92007-02-15 03:39:18 +00001200 // If its a vector type we want to use the element type
Reid Spencerefd53d52007-01-26 08:18:34 +00001201 isFP = PTy->getElementType()->isFloatingPoint();
1202 // Select correct opcode
1203 if (isFP)
1204 return Instruction::FRem;
Reid Spencer3e5affd2007-03-21 17:14:36 +00001205 else if (Sign.isSigned())
Reid Spencerefd53d52007-01-26 08:18:34 +00001206 return Instruction::SRem;
1207 return Instruction::URem;
1208 }
1209 case URemOp : return Instruction::URem;
1210 case SRemOp : return Instruction::SRem;
1211 case FRemOp : return Instruction::FRem;
Reid Spencer832254e2007-02-02 02:16:23 +00001212 case LShrOp : return Instruction::LShr;
1213 case AShrOp : return Instruction::AShr;
1214 case ShlOp : return Instruction::Shl;
1215 case ShrOp :
Reid Spencer3e5affd2007-03-21 17:14:36 +00001216 if (Sign.isSigned())
Reid Spencer832254e2007-02-02 02:16:23 +00001217 return Instruction::AShr;
1218 return Instruction::LShr;
Reid Spencerefd53d52007-01-26 08:18:34 +00001219 case AndOp : return Instruction::And;
1220 case OrOp : return Instruction::Or;
1221 case XorOp : return Instruction::Xor;
1222 }
1223}
1224
1225static inline Instruction::OtherOps
1226getCompareOp(BinaryOps op, unsigned short &predicate, const Type* &Ty,
Reid Spencer3e5affd2007-03-21 17:14:36 +00001227 const Signedness &Sign) {
1228 bool isSigned = Sign.isSigned();
Reid Spencerefd53d52007-01-26 08:18:34 +00001229 bool isFP = Ty->isFloatingPoint();
1230 switch (op) {
1231 default : assert(0 && "Invalid OldSetCC");
1232 case SetEQ :
1233 if (isFP) {
1234 predicate = FCmpInst::FCMP_OEQ;
1235 return Instruction::FCmp;
1236 } else {
1237 predicate = ICmpInst::ICMP_EQ;
1238 return Instruction::ICmp;
1239 }
1240 case SetNE :
1241 if (isFP) {
1242 predicate = FCmpInst::FCMP_UNE;
1243 return Instruction::FCmp;
1244 } else {
1245 predicate = ICmpInst::ICMP_NE;
1246 return Instruction::ICmp;
1247 }
1248 case SetLE :
1249 if (isFP) {
1250 predicate = FCmpInst::FCMP_OLE;
1251 return Instruction::FCmp;
1252 } else {
1253 if (isSigned)
1254 predicate = ICmpInst::ICMP_SLE;
1255 else
1256 predicate = ICmpInst::ICMP_ULE;
1257 return Instruction::ICmp;
1258 }
1259 case SetGE :
1260 if (isFP) {
1261 predicate = FCmpInst::FCMP_OGE;
1262 return Instruction::FCmp;
1263 } else {
1264 if (isSigned)
1265 predicate = ICmpInst::ICMP_SGE;
1266 else
1267 predicate = ICmpInst::ICMP_UGE;
1268 return Instruction::ICmp;
1269 }
1270 case SetLT :
1271 if (isFP) {
1272 predicate = FCmpInst::FCMP_OLT;
1273 return Instruction::FCmp;
1274 } else {
1275 if (isSigned)
1276 predicate = ICmpInst::ICMP_SLT;
1277 else
1278 predicate = ICmpInst::ICMP_ULT;
1279 return Instruction::ICmp;
1280 }
1281 case SetGT :
1282 if (isFP) {
1283 predicate = FCmpInst::FCMP_OGT;
1284 return Instruction::FCmp;
1285 } else {
1286 if (isSigned)
1287 predicate = ICmpInst::ICMP_SGT;
1288 else
1289 predicate = ICmpInst::ICMP_UGT;
1290 return Instruction::ICmp;
1291 }
1292 }
1293}
1294
1295static inline Instruction::MemoryOps getMemoryOp(MemoryOps op) {
1296 switch (op) {
1297 default : assert(0 && "Invalid OldMemoryOps");
1298 case MallocOp : return Instruction::Malloc;
1299 case FreeOp : return Instruction::Free;
1300 case AllocaOp : return Instruction::Alloca;
1301 case LoadOp : return Instruction::Load;
1302 case StoreOp : return Instruction::Store;
1303 case GetElementPtrOp : return Instruction::GetElementPtr;
1304 }
1305}
1306
1307static inline Instruction::OtherOps
Reid Spencer3e5affd2007-03-21 17:14:36 +00001308getOtherOp(OtherOps op, const Signedness &Sign) {
Reid Spencerefd53d52007-01-26 08:18:34 +00001309 switch (op) {
1310 default : assert(0 && "Invalid OldOtherOps");
1311 case PHIOp : return Instruction::PHI;
1312 case CallOp : return Instruction::Call;
Reid Spencerefd53d52007-01-26 08:18:34 +00001313 case SelectOp : return Instruction::Select;
1314 case UserOp1 : return Instruction::UserOp1;
1315 case UserOp2 : return Instruction::UserOp2;
1316 case VAArg : return Instruction::VAArg;
1317 case ExtractElementOp : return Instruction::ExtractElement;
1318 case InsertElementOp : return Instruction::InsertElement;
1319 case ShuffleVectorOp : return Instruction::ShuffleVector;
1320 case ICmpOp : return Instruction::ICmp;
1321 case FCmpOp : return Instruction::FCmp;
Reid Spencerefd53d52007-01-26 08:18:34 +00001322 };
1323}
1324
1325static inline Value*
Reid Spencer3e5affd2007-03-21 17:14:36 +00001326getCast(CastOps op, Value *Src, const Signedness &SrcSign, const Type *DstTy,
1327 const Signedness &DstSign, bool ForceInstruction = false) {
Reid Spencerefd53d52007-01-26 08:18:34 +00001328 Instruction::CastOps Opcode;
1329 const Type* SrcTy = Src->getType();
1330 if (op == CastOp) {
1331 if (SrcTy->isFloatingPoint() && isa<PointerType>(DstTy)) {
1332 // fp -> ptr cast is no longer supported but we must upgrade this
1333 // by doing a double cast: fp -> int -> ptr
1334 SrcTy = Type::Int64Ty;
1335 Opcode = Instruction::IntToPtr;
1336 if (isa<Constant>(Src)) {
1337 Src = ConstantExpr::getCast(Instruction::FPToUI,
1338 cast<Constant>(Src), SrcTy);
1339 } else {
1340 std::string NewName(makeNameUnique(Src->getName()));
1341 Src = new FPToUIInst(Src, SrcTy, NewName, CurBB);
1342 }
1343 } else if (isa<IntegerType>(DstTy) &&
1344 cast<IntegerType>(DstTy)->getBitWidth() == 1) {
1345 // cast type %x to bool was previously defined as setne type %x, null
1346 // The cast semantic is now to truncate, not compare so we must retain
1347 // the original intent by replacing the cast with a setne
1348 Constant* Null = Constant::getNullValue(SrcTy);
1349 Instruction::OtherOps Opcode = Instruction::ICmp;
1350 unsigned short predicate = ICmpInst::ICMP_NE;
1351 if (SrcTy->isFloatingPoint()) {
1352 Opcode = Instruction::FCmp;
1353 predicate = FCmpInst::FCMP_ONE;
1354 } else if (!SrcTy->isInteger() && !isa<PointerType>(SrcTy)) {
1355 error("Invalid cast to bool");
1356 }
1357 if (isa<Constant>(Src) && !ForceInstruction)
1358 return ConstantExpr::getCompare(predicate, cast<Constant>(Src), Null);
1359 else
1360 return CmpInst::create(Opcode, predicate, Src, Null);
1361 }
1362 // Determine the opcode to use by calling CastInst::getCastOpcode
1363 Opcode =
Reid Spencer3e5affd2007-03-21 17:14:36 +00001364 CastInst::getCastOpcode(Src, SrcSign.isSigned(), DstTy,
1365 DstSign.isSigned());
Reid Spencerefd53d52007-01-26 08:18:34 +00001366
1367 } else switch (op) {
1368 default: assert(0 && "Invalid cast token");
1369 case TruncOp: Opcode = Instruction::Trunc; break;
1370 case ZExtOp: Opcode = Instruction::ZExt; break;
1371 case SExtOp: Opcode = Instruction::SExt; break;
1372 case FPTruncOp: Opcode = Instruction::FPTrunc; break;
1373 case FPExtOp: Opcode = Instruction::FPExt; break;
1374 case FPToUIOp: Opcode = Instruction::FPToUI; break;
1375 case FPToSIOp: Opcode = Instruction::FPToSI; break;
1376 case UIToFPOp: Opcode = Instruction::UIToFP; break;
1377 case SIToFPOp: Opcode = Instruction::SIToFP; break;
1378 case PtrToIntOp: Opcode = Instruction::PtrToInt; break;
1379 case IntToPtrOp: Opcode = Instruction::IntToPtr; break;
1380 case BitCastOp: Opcode = Instruction::BitCast; break;
1381 }
1382
1383 if (isa<Constant>(Src) && !ForceInstruction)
1384 return ConstantExpr::getCast(Opcode, cast<Constant>(Src), DstTy);
1385 return CastInst::create(Opcode, Src, DstTy);
1386}
1387
1388static Instruction *
1389upgradeIntrinsicCall(const Type* RetTy, const ValID &ID,
1390 std::vector<Value*>& Args) {
1391
1392 std::string Name = ID.Type == ValID::NameVal ? ID.Name : "";
Reid Spencer4f859aa2007-04-22 05:46:44 +00001393 if (Name.length() <= 5 || Name[0] != 'l' || Name[1] != 'l' ||
1394 Name[2] != 'v' || Name[3] != 'm' || Name[4] != '.')
1395 return 0;
1396
Reid Spencer1e70bb62007-04-02 00:50:28 +00001397 switch (Name[5]) {
1398 case 'i':
1399 if (Name == "llvm.isunordered.f32" || Name == "llvm.isunordered.f64") {
1400 if (Args.size() != 2)
1401 error("Invalid prototype for " + Name);
1402 return new FCmpInst(FCmpInst::FCMP_UNO, Args[0], Args[1]);
1403 }
1404 break;
Reid Spencer8918cb42007-04-02 02:08:05 +00001405
Reid Spencer1e70bb62007-04-02 00:50:28 +00001406 case 'v' : {
Christopher Lamb43ad6b32007-12-17 01:12:55 +00001407 const Type* PtrTy = PointerType::getUnqual(Type::Int8Ty);
Reid Spencer1e70bb62007-04-02 00:50:28 +00001408 std::vector<const Type*> Params;
1409 if (Name == "llvm.va_start" || Name == "llvm.va_end") {
1410 if (Args.size() != 1)
1411 error("Invalid prototype for " + Name + " prototype");
1412 Params.push_back(PtrTy);
1413 const FunctionType *FTy =
1414 FunctionType::get(Type::VoidTy, Params, false);
Christopher Lamb43ad6b32007-12-17 01:12:55 +00001415 const PointerType *PFTy = PointerType::getUnqual(FTy);
Reid Spencer1e70bb62007-04-02 00:50:28 +00001416 Value* Func = getVal(PFTy, ID);
1417 Args[0] = new BitCastInst(Args[0], PtrTy, makeNameUnique("va"), CurBB);
David Greene52eec542007-08-01 03:43:44 +00001418 return new CallInst(Func, Args.begin(), Args.end());
Reid Spencer1e70bb62007-04-02 00:50:28 +00001419 } else if (Name == "llvm.va_copy") {
1420 if (Args.size() != 2)
1421 error("Invalid prototype for " + Name + " prototype");
1422 Params.push_back(PtrTy);
1423 Params.push_back(PtrTy);
1424 const FunctionType *FTy =
1425 FunctionType::get(Type::VoidTy, Params, false);
Christopher Lamb43ad6b32007-12-17 01:12:55 +00001426 const PointerType *PFTy = PointerType::getUnqual(FTy);
Reid Spencer1e70bb62007-04-02 00:50:28 +00001427 Value* Func = getVal(PFTy, ID);
1428 std::string InstName0(makeNameUnique("va0"));
1429 std::string InstName1(makeNameUnique("va1"));
1430 Args[0] = new BitCastInst(Args[0], PtrTy, InstName0, CurBB);
1431 Args[1] = new BitCastInst(Args[1], PtrTy, InstName1, CurBB);
David Greene52eec542007-08-01 03:43:44 +00001432 return new CallInst(Func, Args.begin(), Args.end());
Reid Spencer1e70bb62007-04-02 00:50:28 +00001433 }
Reid Spencerefd53d52007-01-26 08:18:34 +00001434 }
1435 }
1436 return 0;
1437}
1438
Reid Spencerd23c4dd2007-04-16 00:39:39 +00001439const Type* upgradeGEPCEIndices(const Type* PTy,
1440 std::vector<ValueInfo> *Indices,
1441 std::vector<Constant*> &Result) {
1442 const Type *Ty = PTy;
1443 Result.clear();
1444 for (unsigned i = 0, e = Indices->size(); i != e ; ++i) {
1445 Constant *Index = cast<Constant>((*Indices)[i].V);
1446
1447 if (ConstantInt *CI = dyn_cast<ConstantInt>(Index)) {
1448 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte
1449 // struct indices to i32 struct indices with ZExt for compatibility.
1450 if (CI->getBitWidth() < 32)
1451 Index = ConstantExpr::getCast(Instruction::ZExt, CI, Type::Int32Ty);
1452 }
1453
1454 if (isa<SequentialType>(Ty)) {
1455 // Make sure that unsigned SequentialType indices are zext'd to
1456 // 64-bits if they were smaller than that because LLVM 2.0 will sext
1457 // all indices for SequentialType elements. We must retain the same
1458 // semantic (zext) for unsigned types.
1459 if (const IntegerType *Ity = dyn_cast<IntegerType>(Index->getType())) {
1460 if (Ity->getBitWidth() < 64 && (*Indices)[i].S.isUnsigned()) {
1461 Index = ConstantExpr::getCast(Instruction::ZExt, Index,Type::Int64Ty);
1462 }
1463 }
1464 }
1465 Result.push_back(Index);
David Greeneb8f74792007-09-04 15:46:09 +00001466 Ty = GetElementPtrInst::getIndexedType(PTy, Result.begin(),
1467 Result.end(),true);
Reid Spencerd23c4dd2007-04-16 00:39:39 +00001468 if (!Ty)
1469 error("Index list invalid for constant getelementptr");
1470 }
1471 return Ty;
1472}
1473
1474const Type* upgradeGEPInstIndices(const Type* PTy,
1475 std::vector<ValueInfo> *Indices,
1476 std::vector<Value*> &Result) {
1477 const Type *Ty = PTy;
1478 Result.clear();
1479 for (unsigned i = 0, e = Indices->size(); i != e ; ++i) {
1480 Value *Index = (*Indices)[i].V;
1481
1482 if (ConstantInt *CI = dyn_cast<ConstantInt>(Index)) {
1483 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte
1484 // struct indices to i32 struct indices with ZExt for compatibility.
1485 if (CI->getBitWidth() < 32)
1486 Index = ConstantExpr::getCast(Instruction::ZExt, CI, Type::Int32Ty);
1487 }
1488
1489
1490 if (isa<StructType>(Ty)) { // Only change struct indices
1491 if (!isa<Constant>(Index)) {
1492 error("Invalid non-constant structure index");
1493 return 0;
1494 }
Reid Spencerefd53d52007-01-26 08:18:34 +00001495 } else {
1496 // Make sure that unsigned SequentialType indices are zext'd to
1497 // 64-bits if they were smaller than that because LLVM 2.0 will sext
1498 // all indices for SequentialType elements. We must retain the same
1499 // semantic (zext) for unsigned types.
Reid Spencerd23c4dd2007-04-16 00:39:39 +00001500 if (const IntegerType *Ity = dyn_cast<IntegerType>(Index->getType())) {
Reid Spencer3e5affd2007-03-21 17:14:36 +00001501 if (Ity->getBitWidth() < 64 && (*Indices)[i].S.isUnsigned()) {
Reid Spencerd23c4dd2007-04-16 00:39:39 +00001502 if (isa<Constant>(Index))
Reid Spencerefd53d52007-01-26 08:18:34 +00001503 Index = ConstantExpr::getCast(Instruction::ZExt,
1504 cast<Constant>(Index), Type::Int64Ty);
1505 else
1506 Index = CastInst::create(Instruction::ZExt, Index, Type::Int64Ty,
Reid Spencer832254e2007-02-02 02:16:23 +00001507 makeNameUnique("gep"), CurBB);
Reid Spencer8f78af92007-01-26 20:29:52 +00001508 }
Reid Spencerd23c4dd2007-04-16 00:39:39 +00001509 }
Reid Spencerefd53d52007-01-26 08:18:34 +00001510 }
Reid Spencerd23c4dd2007-04-16 00:39:39 +00001511 Result.push_back(Index);
David Greeneb8f74792007-09-04 15:46:09 +00001512 Ty = GetElementPtrInst::getIndexedType(PTy, Result.begin(),
1513 Result.end(),true);
Reid Spencerd23c4dd2007-04-16 00:39:39 +00001514 if (!Ty)
Reid Spencerefd53d52007-01-26 08:18:34 +00001515 error("Index list invalid for constant getelementptr");
Reid Spencerd23c4dd2007-04-16 00:39:39 +00001516 }
1517 return Ty;
Reid Spencerefd53d52007-01-26 08:18:34 +00001518}
1519
Reid Spencer43f76c92007-01-29 05:41:09 +00001520unsigned upgradeCallingConv(unsigned CC) {
1521 switch (CC) {
1522 case OldCallingConv::C : return CallingConv::C;
1523 case OldCallingConv::CSRet : return CallingConv::C;
1524 case OldCallingConv::Fast : return CallingConv::Fast;
1525 case OldCallingConv::Cold : return CallingConv::Cold;
1526 case OldCallingConv::X86_StdCall : return CallingConv::X86_StdCall;
1527 case OldCallingConv::X86_FastCall: return CallingConv::X86_FastCall;
1528 default:
1529 return CC;
1530 }
1531}
1532
Reid Spencerefd53d52007-01-26 08:18:34 +00001533Module* UpgradeAssembly(const std::string &infile, std::istream& in,
1534 bool debug, bool addAttrs)
Reid Spencere7c3c602006-11-30 06:36:44 +00001535{
1536 Upgradelineno = 1;
1537 CurFilename = infile;
Reid Spencer96839be2006-11-30 16:50:26 +00001538 LexInput = &in;
Reid Spencere77e35e2006-12-01 20:26:20 +00001539 yydebug = debug;
Reid Spencer05e52a12006-12-31 05:45:57 +00001540 AddAttributes = addAttrs;
Reid Spencerefd53d52007-01-26 08:18:34 +00001541 ObsoleteVarArgs = false;
1542 NewVarArgs = false;
Reid Spencere7c3c602006-11-30 06:36:44 +00001543
Reid Spencerefd53d52007-01-26 08:18:34 +00001544 CurModule.CurrentModule = new Module(CurFilename);
1545
1546 // Check to make sure the parser succeeded
Reid Spencere7c3c602006-11-30 06:36:44 +00001547 if (yyparse()) {
Reid Spencerefd53d52007-01-26 08:18:34 +00001548 if (ParserResult)
1549 delete ParserResult;
Reid Spencere0a15bb2007-01-15 00:25:53 +00001550 std::cerr << "llvm-upgrade: parse failed.\n";
Reid Spencere0a15bb2007-01-15 00:25:53 +00001551 return 0;
1552 }
1553
Reid Spencerefd53d52007-01-26 08:18:34 +00001554 // Check to make sure that parsing produced a result
1555 if (!ParserResult) {
1556 std::cerr << "llvm-upgrade: no parse result.\n";
1557 return 0;
Reid Spencere0a15bb2007-01-15 00:25:53 +00001558 }
1559
Reid Spencerefd53d52007-01-26 08:18:34 +00001560 // Reset ParserResult variable while saving its value for the result.
1561 Module *Result = ParserResult;
1562 ParserResult = 0;
Reid Spencere0a15bb2007-01-15 00:25:53 +00001563
Reid Spencerefd53d52007-01-26 08:18:34 +00001564 //Not all functions use vaarg, so make a second check for ObsoleteVarArgs
Reid Spencere0a15bb2007-01-15 00:25:53 +00001565 {
Reid Spencerefd53d52007-01-26 08:18:34 +00001566 Function* F;
Reid Spencer688b0492007-02-05 21:19:13 +00001567 if ((F = Result->getFunction("llvm.va_start"))
Reid Spencerefd53d52007-01-26 08:18:34 +00001568 && F->getFunctionType()->getNumParams() == 0)
1569 ObsoleteVarArgs = true;
Reid Spencer688b0492007-02-05 21:19:13 +00001570 if((F = Result->getFunction("llvm.va_copy"))
Reid Spencerefd53d52007-01-26 08:18:34 +00001571 && F->getFunctionType()->getNumParams() == 1)
1572 ObsoleteVarArgs = true;
Reid Spencer280d8012006-12-01 23:40:53 +00001573 }
Reid Spencer90eb4d62007-01-05 17:18:58 +00001574
Reid Spencerefd53d52007-01-26 08:18:34 +00001575 if (ObsoleteVarArgs && NewVarArgs) {
1576 error("This file is corrupt: it uses both new and old style varargs");
1577 return 0;
Reid Spencer90eb4d62007-01-05 17:18:58 +00001578 }
Reid Spencer90eb4d62007-01-05 17:18:58 +00001579
Reid Spencerefd53d52007-01-26 08:18:34 +00001580 if(ObsoleteVarArgs) {
Reid Spencer688b0492007-02-05 21:19:13 +00001581 if(Function* F = Result->getFunction("llvm.va_start")) {
Reid Spencerefd53d52007-01-26 08:18:34 +00001582 if (F->arg_size() != 0) {
1583 error("Obsolete va_start takes 0 argument");
Reid Spencer90eb4d62007-01-05 17:18:58 +00001584 return 0;
1585 }
Reid Spencerefd53d52007-01-26 08:18:34 +00001586
1587 //foo = va_start()
1588 // ->
1589 //bar = alloca typeof(foo)
1590 //va_start(bar)
1591 //foo = load bar
Reid Spencer90eb4d62007-01-05 17:18:58 +00001592
Reid Spencerefd53d52007-01-26 08:18:34 +00001593 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
1594 const Type* ArgTy = F->getFunctionType()->getReturnType();
Christopher Lamb43ad6b32007-12-17 01:12:55 +00001595 const Type* ArgTyPtr = PointerType::getUnqual(ArgTy);
Duncan Sandse2c43042008-04-07 13:45:04 +00001596 Function* NF = Intrinsic::getDeclaration(Result, Intrinsic::va_start);
Reid Spencerefd53d52007-01-26 08:18:34 +00001597
1598 while (!F->use_empty()) {
1599 CallInst* CI = cast<CallInst>(F->use_back());
1600 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
1601 new CallInst(NF, bar, "", CI);
1602 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
1603 CI->replaceAllUsesWith(foo);
1604 CI->getParent()->getInstList().erase(CI);
Reid Spencercb03b5a2007-01-06 06:03:09 +00001605 }
Reid Spencerefd53d52007-01-26 08:18:34 +00001606 Result->getFunctionList().erase(F);
Reid Spencercb03b5a2007-01-06 06:03:09 +00001607 }
Reid Spencerefd53d52007-01-26 08:18:34 +00001608
Reid Spencer688b0492007-02-05 21:19:13 +00001609 if(Function* F = Result->getFunction("llvm.va_end")) {
Reid Spencerefd53d52007-01-26 08:18:34 +00001610 if(F->arg_size() != 1) {
1611 error("Obsolete va_end takes 1 argument");
1612 return 0;
Reid Spencercb03b5a2007-01-06 06:03:09 +00001613 }
Reid Spencercb03b5a2007-01-06 06:03:09 +00001614
Reid Spencerefd53d52007-01-26 08:18:34 +00001615 //vaend foo
1616 // ->
1617 //bar = alloca 1 of typeof(foo)
1618 //vaend bar
1619 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
1620 const Type* ArgTy = F->getFunctionType()->getParamType(0);
Christopher Lamb43ad6b32007-12-17 01:12:55 +00001621 const Type* ArgTyPtr = PointerType::getUnqual(ArgTy);
Duncan Sandse2c43042008-04-07 13:45:04 +00001622 Function* NF = Intrinsic::getDeclaration(Result, Intrinsic::va_end);
Reid Spencercb03b5a2007-01-06 06:03:09 +00001623
Reid Spencerefd53d52007-01-26 08:18:34 +00001624 while (!F->use_empty()) {
1625 CallInst* CI = cast<CallInst>(F->use_back());
1626 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
1627 new StoreInst(CI->getOperand(1), bar, CI);
1628 new CallInst(NF, bar, "", CI);
1629 CI->getParent()->getInstList().erase(CI);
Reid Spencere77e35e2006-12-01 20:26:20 +00001630 }
Reid Spencerefd53d52007-01-26 08:18:34 +00001631 Result->getFunctionList().erase(F);
Reid Spencere77e35e2006-12-01 20:26:20 +00001632 }
Reid Spencerefd53d52007-01-26 08:18:34 +00001633
Reid Spencer688b0492007-02-05 21:19:13 +00001634 if(Function* F = Result->getFunction("llvm.va_copy")) {
Reid Spencerefd53d52007-01-26 08:18:34 +00001635 if(F->arg_size() != 1) {
1636 error("Obsolete va_copy takes 1 argument");
1637 return 0;
Reid Spencere77e35e2006-12-01 20:26:20 +00001638 }
Reid Spencerefd53d52007-01-26 08:18:34 +00001639 //foo = vacopy(bar)
1640 // ->
1641 //a = alloca 1 of typeof(foo)
1642 //b = alloca 1 of typeof(foo)
1643 //store bar -> b
1644 //vacopy(a, b)
1645 //foo = load a
1646
1647 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
1648 const Type* ArgTy = F->getFunctionType()->getReturnType();
Christopher Lamb43ad6b32007-12-17 01:12:55 +00001649 const Type* ArgTyPtr = PointerType::getUnqual(ArgTy);
Duncan Sandse2c43042008-04-07 13:45:04 +00001650 Function* NF = Intrinsic::getDeclaration(Result, Intrinsic::va_copy);
Reid Spencere77e35e2006-12-01 20:26:20 +00001651
Reid Spencerefd53d52007-01-26 08:18:34 +00001652 while (!F->use_empty()) {
1653 CallInst* CI = cast<CallInst>(F->use_back());
David Greeneba1a7502007-08-07 16:57:55 +00001654 Value *Args[2] = {
1655 new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI),
1656 new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI)
1657 };
David Greene52eec542007-08-01 03:43:44 +00001658 new StoreInst(CI->getOperand(1), Args[1], CI);
David Greeneba1a7502007-08-07 16:57:55 +00001659 new CallInst(NF, Args, Args + 2, "", CI);
David Greene52eec542007-08-01 03:43:44 +00001660 Value* foo = new LoadInst(Args[0], "vacopy.fix.3", CI);
Reid Spencerefd53d52007-01-26 08:18:34 +00001661 CI->replaceAllUsesWith(foo);
1662 CI->getParent()->getInstList().erase(CI);
1663 }
1664 Result->getFunctionList().erase(F);
Reid Spencer90eb4d62007-01-05 17:18:58 +00001665 }
1666 }
1667
Reid Spencer2b8036e2007-01-02 05:44:33 +00001668 return Result;
1669}
1670
Reid Spencerefd53d52007-01-26 08:18:34 +00001671} // end llvm namespace
Reid Spencer90eb4d62007-01-05 17:18:58 +00001672
Reid Spencerefd53d52007-01-26 08:18:34 +00001673using namespace llvm;
Reid Spencere0a15bb2007-01-15 00:25:53 +00001674
Reid Spencere7c3c602006-11-30 06:36:44 +00001675%}
1676
Reid Spencere77e35e2006-12-01 20:26:20 +00001677%union {
Reid Spencerefd53d52007-01-26 08:18:34 +00001678 llvm::Module *ModuleVal;
1679 llvm::Function *FunctionVal;
1680 std::pair<llvm::PATypeInfo, char*> *ArgVal;
1681 llvm::BasicBlock *BasicBlockVal;
Reid Spencer3e5affd2007-03-21 17:14:36 +00001682 llvm::TermInstInfo TermInstVal;
Reid Spencerefd53d52007-01-26 08:18:34 +00001683 llvm::InstrInfo InstVal;
1684 llvm::ConstInfo ConstVal;
1685 llvm::ValueInfo ValueVal;
1686 llvm::PATypeInfo TypeVal;
1687 llvm::TypeInfo PrimType;
1688 llvm::PHIListInfo PHIList;
1689 std::list<llvm::PATypeInfo> *TypeList;
1690 std::vector<llvm::ValueInfo> *ValueList;
1691 std::vector<llvm::ConstInfo> *ConstVector;
1692
1693
1694 std::vector<std::pair<llvm::PATypeInfo,char*> > *ArgList;
1695 // Represent the RHS of PHI node
1696 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
1697
1698 llvm::GlobalValue::LinkageTypes Linkage;
1699 int64_t SInt64Val;
1700 uint64_t UInt64Val;
1701 int SIntVal;
1702 unsigned UIntVal;
Dale Johannesen43421b32007-09-06 18:13:44 +00001703 llvm::APFloat *FPVal;
Reid Spencerefd53d52007-01-26 08:18:34 +00001704 bool BoolVal;
1705
1706 char *StrVal; // This memory is strdup'd!
1707 llvm::ValID ValIDVal; // strdup'd memory maybe!
1708
1709 llvm::BinaryOps BinaryOpVal;
1710 llvm::TermOps TermOpVal;
1711 llvm::MemoryOps MemOpVal;
1712 llvm::OtherOps OtherOpVal;
1713 llvm::CastOps CastOpVal;
1714 llvm::ICmpInst::Predicate IPred;
1715 llvm::FCmpInst::Predicate FPred;
1716 llvm::Module::Endianness Endianness;
Reid Spencere77e35e2006-12-01 20:26:20 +00001717}
1718
Reid Spencerefd53d52007-01-26 08:18:34 +00001719%type <ModuleVal> Module FunctionList
1720%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
1721%type <BasicBlockVal> BasicBlock InstructionList
1722%type <TermInstVal> BBTerminatorInst
1723%type <InstVal> Inst InstVal MemoryInst
1724%type <ConstVal> ConstVal ConstExpr
1725%type <ConstVector> ConstVector
1726%type <ArgList> ArgList ArgListH
1727%type <ArgVal> ArgVal
1728%type <PHIList> PHIList
1729%type <ValueList> ValueRefList ValueRefListE // For call param lists
1730%type <ValueList> IndexList // For GEP derived indices
1731%type <TypeList> TypeListI ArgTypeListI
1732%type <JumpTable> JumpTable
1733%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
1734%type <BoolVal> OptVolatile // 'volatile' or not
1735%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1736%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencere7c326b2007-02-08 08:09:36 +00001737%type <Linkage> OptLinkage FnDeclareLinkage
Reid Spencerefd53d52007-01-26 08:18:34 +00001738%type <Endianness> BigOrLittle
Reid Spencere77e35e2006-12-01 20:26:20 +00001739
Reid Spencerefd53d52007-01-26 08:18:34 +00001740// ValueRef - Unresolved reference to a definition or BB
1741%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1742%type <ValueVal> ResolvedVal // <type> <valref> pair
Reid Spencer9373d272007-01-26 17:13:53 +00001743
Reid Spencerefd53d52007-01-26 08:18:34 +00001744// Tokens and types for handling constant integer values
1745//
1746// ESINT64VAL - A negative number within long long range
1747%token <SInt64Val> ESINT64VAL
Reid Spencere77e35e2006-12-01 20:26:20 +00001748
Reid Spencerefd53d52007-01-26 08:18:34 +00001749// EUINT64VAL - A positive number within uns. long long range
1750%token <UInt64Val> EUINT64VAL
1751%type <SInt64Val> EINT64VAL
Reid Spencere77e35e2006-12-01 20:26:20 +00001752
Reid Spencerefd53d52007-01-26 08:18:34 +00001753%token <SIntVal> SINTVAL // Signed 32 bit ints...
1754%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
1755%type <SIntVal> INTVAL
1756%token <FPVal> FPVAL // Float or Double constant
Reid Spencere77e35e2006-12-01 20:26:20 +00001757
Reid Spencerefd53d52007-01-26 08:18:34 +00001758// Built in types...
1759%type <TypeVal> Types TypesV UpRTypes UpRTypesV
1760%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
1761%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
1762%token <PrimType> FLOAT DOUBLE TYPE LABEL
Reid Spencere77e35e2006-12-01 20:26:20 +00001763
Reid Spencerefd53d52007-01-26 08:18:34 +00001764%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
1765%type <StrVal> Name OptName OptAssign
1766%type <UIntVal> OptAlign OptCAlign
1767%type <StrVal> OptSection SectionString
1768
1769%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
1770%token DECLARE GLOBAL CONSTANT SECTION VOLATILE
1771%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
1772%token DLLIMPORT DLLEXPORT EXTERN_WEAK
1773%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
1774%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
1775%token CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
1776%token X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
1777%token DATALAYOUT
1778%type <UIntVal> OptCallingConv
1779
1780// Basic Block Terminating Operators
1781%token <TermOpVal> RET BR SWITCH INVOKE UNREACHABLE
1782%token UNWIND EXCEPT
1783
1784// Binary Operators
1785%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Reid Spencer832254e2007-02-02 02:16:23 +00001786%type <BinaryOpVal> ShiftOps
Reid Spencerefd53d52007-01-26 08:18:34 +00001787%token <BinaryOpVal> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM
Reid Spencer832254e2007-02-02 02:16:23 +00001788%token <BinaryOpVal> AND OR XOR SHL SHR ASHR LSHR
Reid Spencerefd53d52007-01-26 08:18:34 +00001789%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
1790%token <OtherOpVal> ICMP FCMP
1791
1792// Memory Instructions
1793%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1794
1795// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001796%token <OtherOpVal> PHI_TOK SELECT VAARG
Reid Spencerefd53d52007-01-26 08:18:34 +00001797%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
1798%token VAARG_old VANEXT_old //OBSOLETE
1799
Reid Spencer9373d272007-01-26 17:13:53 +00001800// Support for ICmp/FCmp Predicates, which is 1.9++ but not 2.0
Reid Spencerefd53d52007-01-26 08:18:34 +00001801%type <IPred> IPredicates
1802%type <FPred> FPredicates
1803%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1804%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
1805
1806%token <CastOpVal> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI
1807%token <CastOpVal> UITOFP SITOFP PTRTOINT INTTOPTR BITCAST
1808%type <CastOpVal> CastOps
Reid Spencere7c3c602006-11-30 06:36:44 +00001809
1810%start Module
1811
1812%%
1813
1814// Handle constant integer size restriction and conversion...
Reid Spencerefd53d52007-01-26 08:18:34 +00001815//
1816INTVAL
Reid Spencer7e972882007-01-26 18:26:23 +00001817 : SINTVAL
Reid Spencerefd53d52007-01-26 08:18:34 +00001818 | UINTVAL {
1819 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
1820 error("Value too large for type");
1821 $$ = (int32_t)$1;
1822 }
1823 ;
1824
1825EINT64VAL
Reid Spencer7e972882007-01-26 18:26:23 +00001826 : ESINT64VAL // These have same type and can't cause problems...
Reid Spencerefd53d52007-01-26 08:18:34 +00001827 | EUINT64VAL {
1828 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
1829 error("Value too large for type");
1830 $$ = (int64_t)$1;
1831 };
Reid Spencere7c3c602006-11-30 06:36:44 +00001832
1833// Operations that are notably excluded from this list include:
1834// RET, BR, & SWITCH because they end basic blocks and are treated specially.
Reid Spencerefd53d52007-01-26 08:18:34 +00001835//
1836ArithmeticOps
1837 : ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV | REM | UREM | SREM | FREM
1838 ;
1839
1840LogicalOps
1841 : AND | OR | XOR
1842 ;
1843
1844SetCondOps
1845 : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
1846 ;
1847
1848IPredicates
1849 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
1850 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1851 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1852 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1853 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1854 ;
1855
1856FPredicates
1857 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1858 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1859 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1860 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1861 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1862 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1863 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1864 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1865 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1866 ;
1867ShiftOps
1868 : SHL | SHR | ASHR | LSHR
1869 ;
1870
1871CastOps
1872 : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI
1873 | UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
1874 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001875
1876// These are some types that allow classification if we only want a particular
1877// thing... for example, only a signed, unsigned, or integral type.
Reid Spencerefd53d52007-01-26 08:18:34 +00001878SIntType
1879 : LONG | INT | SHORT | SBYTE
1880 ;
1881
1882UIntType
1883 : ULONG | UINT | USHORT | UBYTE
1884 ;
1885
1886IntType
1887 : SIntType | UIntType
1888 ;
1889
1890FPType
1891 : FLOAT | DOUBLE
1892 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001893
1894// OptAssign - Value producing statements have an optional assignment component
Reid Spencerefd53d52007-01-26 08:18:34 +00001895OptAssign
1896 : Name '=' {
Reid Spencere7c3c602006-11-30 06:36:44 +00001897 $$ = $1;
1898 }
1899 | /*empty*/ {
Reid Spencerefd53d52007-01-26 08:18:34 +00001900 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001901 };
1902
1903OptLinkage
Reid Spencer0ec82002007-02-08 00:21:06 +00001904 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencerefd53d52007-01-26 08:18:34 +00001905 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1906 | WEAK { $$ = GlobalValue::WeakLinkage; }
1907 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1908 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1909 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencer0ec82002007-02-08 00:21:06 +00001910 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencerefd53d52007-01-26 08:18:34 +00001911 | /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1912 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001913
1914OptCallingConv
Reid Spencer4f859aa2007-04-22 05:46:44 +00001915 : /*empty*/ { $$ = lastCallingConv = OldCallingConv::C; }
1916 | CCC_TOK { $$ = lastCallingConv = OldCallingConv::C; }
1917 | CSRETCC_TOK { $$ = lastCallingConv = OldCallingConv::CSRet; }
1918 | FASTCC_TOK { $$ = lastCallingConv = OldCallingConv::Fast; }
1919 | COLDCC_TOK { $$ = lastCallingConv = OldCallingConv::Cold; }
1920 | X86_STDCALLCC_TOK { $$ = lastCallingConv = OldCallingConv::X86_StdCall; }
1921 | X86_FASTCALLCC_TOK { $$ = lastCallingConv = OldCallingConv::X86_FastCall; }
Reid Spencerefd53d52007-01-26 08:18:34 +00001922 | CC_TOK EUINT64VAL {
1923 if ((unsigned)$2 != $2)
1924 error("Calling conv too large");
Reid Spencer4f859aa2007-04-22 05:46:44 +00001925 $$ = lastCallingConv = $2;
Reid Spencerefd53d52007-01-26 08:18:34 +00001926 }
1927 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001928
1929// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1930// a comma before it.
1931OptAlign
Reid Spencerefd53d52007-01-26 08:18:34 +00001932 : /*empty*/ { $$ = 0; }
1933 | ALIGN EUINT64VAL {
1934 $$ = $2;
1935 if ($$ != 0 && !isPowerOf2_32($$))
1936 error("Alignment must be a power of two");
1937 }
1938 ;
Jim Laskey98ba5882006-12-06 10:57:33 +00001939
Reid Spencere7c3c602006-11-30 06:36:44 +00001940OptCAlign
Reid Spencerefd53d52007-01-26 08:18:34 +00001941 : /*empty*/ { $$ = 0; }
1942 | ',' ALIGN EUINT64VAL {
1943 $$ = $3;
1944 if ($$ != 0 && !isPowerOf2_32($$))
1945 error("Alignment must be a power of two");
1946 }
1947 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001948
1949SectionString
Reid Spencerefd53d52007-01-26 08:18:34 +00001950 : SECTION STRINGCONSTANT {
1951 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1952 if ($2[i] == '"' || $2[i] == '\\')
1953 error("Invalid character in section name");
1954 $$ = $2;
1955 }
1956 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001957
Reid Spencerefd53d52007-01-26 08:18:34 +00001958OptSection
1959 : /*empty*/ { $$ = 0; }
1960 | SectionString { $$ = $1; }
1961 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001962
Reid Spencerefd53d52007-01-26 08:18:34 +00001963// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1964// is set to be the global we are processing.
1965//
Reid Spencere7c3c602006-11-30 06:36:44 +00001966GlobalVarAttributes
Reid Spencerefd53d52007-01-26 08:18:34 +00001967 : /* empty */ {}
1968 | ',' GlobalVarAttribute GlobalVarAttributes {}
1969 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001970
Reid Spencerefd53d52007-01-26 08:18:34 +00001971GlobalVarAttribute
1972 : SectionString {
1973 CurGV->setSection($1);
1974 free($1);
1975 }
1976 | ALIGN EUINT64VAL {
1977 if ($2 != 0 && !isPowerOf2_32($2))
1978 error("Alignment must be a power of two");
1979 CurGV->setAlignment($2);
1980
1981 }
1982 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001983
1984//===----------------------------------------------------------------------===//
1985// Types includes all predefined types... except void, because it can only be
1986// used in specific contexts (function returning void for example). To have
1987// access to it, a user must explicitly use TypesV.
1988//
1989
1990// TypesV includes all of 'Types', but it also includes the void type.
Reid Spencerefd53d52007-01-26 08:18:34 +00001991TypesV
1992 : Types
1993 | VOID {
Reid Spencere7c326b2007-02-08 08:09:36 +00001994 $$.PAT = new PATypeHolder($1.T);
Reid Spencer3e5affd2007-03-21 17:14:36 +00001995 $$.S.makeSignless();
Reid Spencerefd53d52007-01-26 08:18:34 +00001996 }
1997 ;
1998
1999UpRTypesV
2000 : UpRTypes
2001 | VOID {
Reid Spencere7c326b2007-02-08 08:09:36 +00002002 $$.PAT = new PATypeHolder($1.T);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002003 $$.S.makeSignless();
Reid Spencerefd53d52007-01-26 08:18:34 +00002004 }
2005 ;
2006
2007Types
2008 : UpRTypes {
2009 if (!UpRefs.empty())
Reid Spencere7c326b2007-02-08 08:09:36 +00002010 error("Invalid upreference in type: " + (*$1.PAT)->getDescription());
Reid Spencerefd53d52007-01-26 08:18:34 +00002011 $$ = $1;
2012 }
2013 ;
2014
2015PrimType
2016 : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
2017 | LONG | ULONG | FLOAT | DOUBLE | LABEL
2018 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002019
2020// Derived types are added later...
Reid Spencera50d5962006-12-02 04:11:07 +00002021UpRTypes
Reid Spencerefd53d52007-01-26 08:18:34 +00002022 : PrimType {
Reid Spencere7c326b2007-02-08 08:09:36 +00002023 $$.PAT = new PATypeHolder($1.T);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002024 $$.S.copy($1.S);
Reid Spencera50d5962006-12-02 04:11:07 +00002025 }
Reid Spencerefd53d52007-01-26 08:18:34 +00002026 | OPAQUE {
Reid Spencere7c326b2007-02-08 08:09:36 +00002027 $$.PAT = new PATypeHolder(OpaqueType::get());
Reid Spencer3e5affd2007-03-21 17:14:36 +00002028 $$.S.makeSignless();
Reid Spencerefd53d52007-01-26 08:18:34 +00002029 }
2030 | SymbolicValueRef { // Named types are also simple types...
Reid Spencer3e5affd2007-03-21 17:14:36 +00002031 $$.S.copy(getTypeSign($1));
Reid Spencer9373d272007-01-26 17:13:53 +00002032 const Type* tmp = getType($1);
Reid Spencere7c326b2007-02-08 08:09:36 +00002033 $$.PAT = new PATypeHolder(tmp);
Reid Spencera8ca0902006-12-02 20:19:56 +00002034 }
2035 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerefd53d52007-01-26 08:18:34 +00002036 if ($2 > (uint64_t)~0U)
2037 error("Value out of range");
2038 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
2039 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencere7c326b2007-02-08 08:09:36 +00002040 $$.PAT = new PATypeHolder(OT);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002041 $$.S.makeSignless();
Reid Spencerefd53d52007-01-26 08:18:34 +00002042 UR_OUT("New Upreference!\n");
Reid Spencere7c3c602006-11-30 06:36:44 +00002043 }
2044 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Reid Spencer3e5affd2007-03-21 17:14:36 +00002045 $$.S.makeComposite($1.S);
Reid Spencerefd53d52007-01-26 08:18:34 +00002046 std::vector<const Type*> Params;
2047 for (std::list<llvm::PATypeInfo>::iterator I = $3->begin(),
2048 E = $3->end(); I != E; ++I) {
Reid Spencere7c326b2007-02-08 08:09:36 +00002049 Params.push_back(I->PAT->get());
Reid Spencer3e5affd2007-03-21 17:14:36 +00002050 $$.S.add(I->S);
Reid Spencer2b8036e2007-01-02 05:44:33 +00002051 }
Reid Spencerefd53d52007-01-26 08:18:34 +00002052 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
2053 if (isVarArg) Params.pop_back();
2054
Chris Lattner58d74912008-03-12 17:45:29 +00002055 PAListPtr PAL;
Reid Spencer4f859aa2007-04-22 05:46:44 +00002056 if (lastCallingConv == OldCallingConv::CSRet) {
Chris Lattner58d74912008-03-12 17:45:29 +00002057 ParamAttrsWithIndex PAWI =
2058 ParamAttrsWithIndex::get(1, ParamAttr::StructRet);
2059 PAL = PAListPtr::get(&PAWI, 1);
Reid Spencer4f859aa2007-04-22 05:46:44 +00002060 }
2061
Reid Spencer460dd9b2007-04-09 06:15:59 +00002062 const FunctionType *FTy =
Duncan Sandsdc024672007-11-27 13:23:08 +00002063 FunctionType::get($1.PAT->get(), Params, isVarArg);
Reid Spencer460dd9b2007-04-09 06:15:59 +00002064
2065 $$.PAT = new PATypeHolder( HandleUpRefs(FTy, $$.S) );
Reid Spencer3e5affd2007-03-21 17:14:36 +00002066 delete $1.PAT; // Delete the return type handle
Reid Spencerefd53d52007-01-26 08:18:34 +00002067 delete $3; // Delete the argument list
Reid Spencere7c3c602006-11-30 06:36:44 +00002068 }
2069 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencer3e5affd2007-03-21 17:14:36 +00002070 $$.S.makeComposite($4.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00002071 $$.PAT = new PATypeHolder(HandleUpRefs(ArrayType::get($4.PAT->get(),
Reid Spencer3e5affd2007-03-21 17:14:36 +00002072 (unsigned)$2), $$.S));
Reid Spencere7c326b2007-02-08 08:09:36 +00002073 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002074 }
Reid Spencerac9dcb92007-02-15 03:39:18 +00002075 | '<' EUINT64VAL 'x' UpRTypes '>' { // Vector type?
Reid Spencer3e5affd2007-03-21 17:14:36 +00002076 const llvm::Type* ElemTy = $4.PAT->get();
2077 if ((unsigned)$2 != $2)
2078 error("Unsigned result not equal to signed result");
2079 if (!(ElemTy->isInteger() || ElemTy->isFloatingPoint()))
2080 error("Elements of a VectorType must be integer or floating point");
2081 if (!isPowerOf2_32($2))
2082 error("VectorType length should be a power of 2");
2083 $$.S.makeComposite($4.S);
2084 $$.PAT = new PATypeHolder(HandleUpRefs(VectorType::get(ElemTy,
2085 (unsigned)$2), $$.S));
2086 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002087 }
2088 | '{' TypeListI '}' { // Structure type?
Reid Spencerefd53d52007-01-26 08:18:34 +00002089 std::vector<const Type*> Elements;
Reid Spencer3e5affd2007-03-21 17:14:36 +00002090 $$.S.makeComposite();
Reid Spencerefd53d52007-01-26 08:18:34 +00002091 for (std::list<llvm::PATypeInfo>::iterator I = $2->begin(),
Reid Spencer3e5affd2007-03-21 17:14:36 +00002092 E = $2->end(); I != E; ++I) {
Reid Spencere7c326b2007-02-08 08:09:36 +00002093 Elements.push_back(I->PAT->get());
Reid Spencer3e5affd2007-03-21 17:14:36 +00002094 $$.S.add(I->S);
2095 }
2096 $$.PAT = new PATypeHolder(HandleUpRefs(StructType::get(Elements), $$.S));
Reid Spencerefd53d52007-01-26 08:18:34 +00002097 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00002098 }
2099 | '{' '}' { // Empty structure type?
Reid Spencere7c326b2007-02-08 08:09:36 +00002100 $$.PAT = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer3e5affd2007-03-21 17:14:36 +00002101 $$.S.makeComposite();
Reid Spencere7c3c602006-11-30 06:36:44 +00002102 }
Reid Spencera9d7e892006-12-29 20:33:37 +00002103 | '<' '{' TypeListI '}' '>' { // Packed Structure type?
Reid Spencer3e5affd2007-03-21 17:14:36 +00002104 $$.S.makeComposite();
Reid Spencerefd53d52007-01-26 08:18:34 +00002105 std::vector<const Type*> Elements;
2106 for (std::list<llvm::PATypeInfo>::iterator I = $3->begin(),
2107 E = $3->end(); I != E; ++I) {
Reid Spencere7c326b2007-02-08 08:09:36 +00002108 Elements.push_back(I->PAT->get());
Reid Spencer3e5affd2007-03-21 17:14:36 +00002109 $$.S.add(I->S);
Reid Spencere7c326b2007-02-08 08:09:36 +00002110 delete I->PAT;
Reid Spencer2b8036e2007-01-02 05:44:33 +00002111 }
Reid Spencer3e5affd2007-03-21 17:14:36 +00002112 $$.PAT = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true),
2113 $$.S));
Reid Spencerefd53d52007-01-26 08:18:34 +00002114 delete $3;
Reid Spencera9d7e892006-12-29 20:33:37 +00002115 }
2116 | '<' '{' '}' '>' { // Empty packed structure type?
Reid Spencere7c326b2007-02-08 08:09:36 +00002117 $$.PAT = new PATypeHolder(StructType::get(std::vector<const Type*>(),true));
Reid Spencer3e5affd2007-03-21 17:14:36 +00002118 $$.S.makeComposite();
Reid Spencera9d7e892006-12-29 20:33:37 +00002119 }
Reid Spencere7c3c602006-11-30 06:36:44 +00002120 | UpRTypes '*' { // Pointer type?
Reid Spencere7c326b2007-02-08 08:09:36 +00002121 if ($1.PAT->get() == Type::LabelTy)
Reid Spencerefd53d52007-01-26 08:18:34 +00002122 error("Cannot form a pointer to a basic block");
Reid Spencer3e5affd2007-03-21 17:14:36 +00002123 $$.S.makeComposite($1.S);
Christopher Lamb43ad6b32007-12-17 01:12:55 +00002124 $$.PAT = new
2125 PATypeHolder(HandleUpRefs(PointerType::getUnqual($1.PAT->get()),
2126 $$.S));
Reid Spencere7c326b2007-02-08 08:09:36 +00002127 delete $1.PAT;
Reid Spencerefd53d52007-01-26 08:18:34 +00002128 }
2129 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002130
2131// TypeList - Used for struct declarations and as a basis for function type
2132// declaration type lists
2133//
Reid Spencere77e35e2006-12-01 20:26:20 +00002134TypeListI
2135 : UpRTypes {
Reid Spencerefd53d52007-01-26 08:18:34 +00002136 $$ = new std::list<PATypeInfo>();
2137 $$->push_back($1);
Reid Spencere77e35e2006-12-01 20:26:20 +00002138 }
2139 | TypeListI ',' UpRTypes {
Reid Spencerefd53d52007-01-26 08:18:34 +00002140 ($$=$1)->push_back($3);
2141 }
2142 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002143
2144// ArgTypeList - List of types for a function type declaration...
Reid Spencere77e35e2006-12-01 20:26:20 +00002145ArgTypeListI
Reid Spencerefd53d52007-01-26 08:18:34 +00002146 : TypeListI
Reid Spencere7c3c602006-11-30 06:36:44 +00002147 | TypeListI ',' DOTDOTDOT {
Reid Spencerefd53d52007-01-26 08:18:34 +00002148 PATypeInfo VoidTI;
Reid Spencere7c326b2007-02-08 08:09:36 +00002149 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002150 VoidTI.S.makeSignless();
Reid Spencerefd53d52007-01-26 08:18:34 +00002151 ($$=$1)->push_back(VoidTI);
Reid Spencere7c3c602006-11-30 06:36:44 +00002152 }
2153 | DOTDOTDOT {
Reid Spencerefd53d52007-01-26 08:18:34 +00002154 $$ = new std::list<PATypeInfo>();
2155 PATypeInfo VoidTI;
Reid Spencere7c326b2007-02-08 08:09:36 +00002156 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002157 VoidTI.S.makeSignless();
Reid Spencerefd53d52007-01-26 08:18:34 +00002158 $$->push_back(VoidTI);
Reid Spencere7c3c602006-11-30 06:36:44 +00002159 }
2160 | /*empty*/ {
Reid Spencerefd53d52007-01-26 08:18:34 +00002161 $$ = new std::list<PATypeInfo>();
2162 }
2163 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002164
2165// ConstVal - The various declarations that go into the constant pool. This
2166// production is used ONLY to represent constants that show up AFTER a 'const',
2167// 'constant' or 'global' token at global scope. Constants that can be inlined
2168// into other expressions (such as integers and constexprs) are handled by the
2169// ResolvedVal, ValueRef and ConstValueRef productions.
2170//
Reid Spencerefd53d52007-01-26 08:18:34 +00002171ConstVal
2172 : Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencere7c326b2007-02-08 08:09:36 +00002173 const ArrayType *ATy = dyn_cast<ArrayType>($1.PAT->get());
Reid Spencerefd53d52007-01-26 08:18:34 +00002174 if (ATy == 0)
2175 error("Cannot make array constant with type: '" +
Reid Spencere7c326b2007-02-08 08:09:36 +00002176 $1.PAT->get()->getDescription() + "'");
Reid Spencerefd53d52007-01-26 08:18:34 +00002177 const Type *ETy = ATy->getElementType();
2178 int NumElements = ATy->getNumElements();
2179
2180 // Verify that we have the correct size...
2181 if (NumElements != -1 && NumElements != (int)$3->size())
2182 error("Type mismatch: constant sized array initialized with " +
2183 utostr($3->size()) + " arguments, but has size of " +
2184 itostr(NumElements) + "");
2185
2186 // Verify all elements are correct type!
2187 std::vector<Constant*> Elems;
2188 for (unsigned i = 0; i < $3->size(); i++) {
2189 Constant *C = (*$3)[i].C;
2190 const Type* ValTy = C->getType();
2191 if (ETy != ValTy)
2192 error("Element #" + utostr(i) + " is not of type '" +
2193 ETy->getDescription() +"' as required!\nIt is of type '"+
2194 ValTy->getDescription() + "'");
2195 Elems.push_back(C);
2196 }
2197 $$.C = ConstantArray::get(ATy, Elems);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002198 $$.S.copy($1.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00002199 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002200 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002201 }
2202 | Types '[' ']' {
Reid Spencere7c326b2007-02-08 08:09:36 +00002203 const ArrayType *ATy = dyn_cast<ArrayType>($1.PAT->get());
Reid Spencerefd53d52007-01-26 08:18:34 +00002204 if (ATy == 0)
2205 error("Cannot make array constant with type: '" +
Reid Spencere7c326b2007-02-08 08:09:36 +00002206 $1.PAT->get()->getDescription() + "'");
Reid Spencerefd53d52007-01-26 08:18:34 +00002207 int NumElements = ATy->getNumElements();
2208 if (NumElements != -1 && NumElements != 0)
2209 error("Type mismatch: constant sized array initialized with 0"
2210 " arguments, but has size of " + itostr(NumElements) +"");
2211 $$.C = ConstantArray::get(ATy, std::vector<Constant*>());
Reid Spencer3e5affd2007-03-21 17:14:36 +00002212 $$.S.copy($1.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00002213 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002214 }
2215 | Types 'c' STRINGCONSTANT {
Reid Spencere7c326b2007-02-08 08:09:36 +00002216 const ArrayType *ATy = dyn_cast<ArrayType>($1.PAT->get());
Reid Spencerefd53d52007-01-26 08:18:34 +00002217 if (ATy == 0)
2218 error("Cannot make array constant with type: '" +
Reid Spencere7c326b2007-02-08 08:09:36 +00002219 $1.PAT->get()->getDescription() + "'");
Reid Spencerefd53d52007-01-26 08:18:34 +00002220 int NumElements = ATy->getNumElements();
2221 const Type *ETy = dyn_cast<IntegerType>(ATy->getElementType());
2222 if (!ETy || cast<IntegerType>(ETy)->getBitWidth() != 8)
2223 error("String arrays require type i8, not '" + ETy->getDescription() +
2224 "'");
2225 char *EndStr = UnEscapeLexed($3, true);
2226 if (NumElements != -1 && NumElements != (EndStr-$3))
2227 error("Can't build string constant of size " +
2228 itostr((int)(EndStr-$3)) + " when array has size " +
2229 itostr(NumElements) + "");
2230 std::vector<Constant*> Vals;
2231 for (char *C = (char *)$3; C != (char *)EndStr; ++C)
2232 Vals.push_back(ConstantInt::get(ETy, *C));
2233 free($3);
2234 $$.C = ConstantArray::get(ATy, Vals);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002235 $$.S.copy($1.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00002236 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002237 }
2238 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer9d6565a2007-02-15 02:26:10 +00002239 const VectorType *PTy = dyn_cast<VectorType>($1.PAT->get());
Reid Spencerefd53d52007-01-26 08:18:34 +00002240 if (PTy == 0)
2241 error("Cannot make packed constant with type: '" +
Reid Spencere7c326b2007-02-08 08:09:36 +00002242 $1.PAT->get()->getDescription() + "'");
Reid Spencerefd53d52007-01-26 08:18:34 +00002243 const Type *ETy = PTy->getElementType();
2244 int NumElements = PTy->getNumElements();
2245 // Verify that we have the correct size...
2246 if (NumElements != -1 && NumElements != (int)$3->size())
2247 error("Type mismatch: constant sized packed initialized with " +
2248 utostr($3->size()) + " arguments, but has size of " +
2249 itostr(NumElements) + "");
2250 // Verify all elements are correct type!
2251 std::vector<Constant*> Elems;
2252 for (unsigned i = 0; i < $3->size(); i++) {
2253 Constant *C = (*$3)[i].C;
2254 const Type* ValTy = C->getType();
2255 if (ETy != ValTy)
2256 error("Element #" + utostr(i) + " is not of type '" +
2257 ETy->getDescription() +"' as required!\nIt is of type '"+
2258 ValTy->getDescription() + "'");
2259 Elems.push_back(C);
2260 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00002261 $$.C = ConstantVector::get(PTy, Elems);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002262 $$.S.copy($1.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00002263 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002264 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002265 }
2266 | Types '{' ConstVector '}' {
Reid Spencere7c326b2007-02-08 08:09:36 +00002267 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencerefd53d52007-01-26 08:18:34 +00002268 if (STy == 0)
2269 error("Cannot make struct constant with type: '" +
Reid Spencere7c326b2007-02-08 08:09:36 +00002270 $1.PAT->get()->getDescription() + "'");
Reid Spencerefd53d52007-01-26 08:18:34 +00002271 if ($3->size() != STy->getNumContainedTypes())
2272 error("Illegal number of initializers for structure type");
2273
2274 // Check to ensure that constants are compatible with the type initializer!
2275 std::vector<Constant*> Fields;
2276 for (unsigned i = 0, e = $3->size(); i != e; ++i) {
2277 Constant *C = (*$3)[i].C;
2278 if (C->getType() != STy->getElementType(i))
2279 error("Expected type '" + STy->getElementType(i)->getDescription() +
2280 "' for element #" + utostr(i) + " of structure initializer");
2281 Fields.push_back(C);
2282 }
2283 $$.C = ConstantStruct::get(STy, Fields);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002284 $$.S.copy($1.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00002285 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002286 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002287 }
2288 | Types '{' '}' {
Reid Spencere7c326b2007-02-08 08:09:36 +00002289 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencerefd53d52007-01-26 08:18:34 +00002290 if (STy == 0)
2291 error("Cannot make struct constant with type: '" +
Reid Spencere7c326b2007-02-08 08:09:36 +00002292 $1.PAT->get()->getDescription() + "'");
Reid Spencerefd53d52007-01-26 08:18:34 +00002293 if (STy->getNumContainedTypes() != 0)
2294 error("Illegal number of initializers for structure type");
2295 $$.C = ConstantStruct::get(STy, std::vector<Constant*>());
Reid Spencer3e5affd2007-03-21 17:14:36 +00002296 $$.S.copy($1.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00002297 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002298 }
Reid Spencerefd53d52007-01-26 08:18:34 +00002299 | Types '<' '{' ConstVector '}' '>' {
Reid Spencere7c326b2007-02-08 08:09:36 +00002300 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencerefd53d52007-01-26 08:18:34 +00002301 if (STy == 0)
2302 error("Cannot make packed struct constant with type: '" +
Reid Spencere7c326b2007-02-08 08:09:36 +00002303 $1.PAT->get()->getDescription() + "'");
Reid Spencerefd53d52007-01-26 08:18:34 +00002304 if ($4->size() != STy->getNumContainedTypes())
2305 error("Illegal number of initializers for packed structure type");
Reid Spencere7c3c602006-11-30 06:36:44 +00002306
Reid Spencerefd53d52007-01-26 08:18:34 +00002307 // Check to ensure that constants are compatible with the type initializer!
2308 std::vector<Constant*> Fields;
2309 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
2310 Constant *C = (*$4)[i].C;
2311 if (C->getType() != STy->getElementType(i))
2312 error("Expected type '" + STy->getElementType(i)->getDescription() +
2313 "' for element #" + utostr(i) + " of packed struct initializer");
2314 Fields.push_back(C);
Reid Spencer280d8012006-12-01 23:40:53 +00002315 }
Reid Spencerefd53d52007-01-26 08:18:34 +00002316 $$.C = ConstantStruct::get(STy, Fields);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002317 $$.S.copy($1.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00002318 delete $1.PAT;
Reid Spencere77e35e2006-12-01 20:26:20 +00002319 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00002320 }
Reid Spencerefd53d52007-01-26 08:18:34 +00002321 | Types '<' '{' '}' '>' {
Reid Spencere7c326b2007-02-08 08:09:36 +00002322 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencerefd53d52007-01-26 08:18:34 +00002323 if (STy == 0)
2324 error("Cannot make packed struct constant with type: '" +
Reid Spencere7c326b2007-02-08 08:09:36 +00002325 $1.PAT->get()->getDescription() + "'");
Reid Spencerefd53d52007-01-26 08:18:34 +00002326 if (STy->getNumContainedTypes() != 0)
2327 error("Illegal number of initializers for packed structure type");
2328 $$.C = ConstantStruct::get(STy, std::vector<Constant*>());
Reid Spencer3e5affd2007-03-21 17:14:36 +00002329 $$.S.copy($1.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00002330 delete $1.PAT;
Reid Spencerefd53d52007-01-26 08:18:34 +00002331 }
2332 | Types NULL_TOK {
Reid Spencere7c326b2007-02-08 08:09:36 +00002333 const PointerType *PTy = dyn_cast<PointerType>($1.PAT->get());
Reid Spencerefd53d52007-01-26 08:18:34 +00002334 if (PTy == 0)
2335 error("Cannot make null pointer constant with type: '" +
Reid Spencere7c326b2007-02-08 08:09:36 +00002336 $1.PAT->get()->getDescription() + "'");
Reid Spencerefd53d52007-01-26 08:18:34 +00002337 $$.C = ConstantPointerNull::get(PTy);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002338 $$.S.copy($1.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00002339 delete $1.PAT;
Reid Spencerefd53d52007-01-26 08:18:34 +00002340 }
2341 | Types UNDEF {
Reid Spencere7c326b2007-02-08 08:09:36 +00002342 $$.C = UndefValue::get($1.PAT->get());
Reid Spencer3e5affd2007-03-21 17:14:36 +00002343 $$.S.copy($1.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00002344 delete $1.PAT;
Reid Spencerefd53d52007-01-26 08:18:34 +00002345 }
2346 | Types SymbolicValueRef {
Reid Spencere7c326b2007-02-08 08:09:36 +00002347 const PointerType *Ty = dyn_cast<PointerType>($1.PAT->get());
Reid Spencerefd53d52007-01-26 08:18:34 +00002348 if (Ty == 0)
2349 error("Global const reference must be a pointer type, not" +
Reid Spencere7c326b2007-02-08 08:09:36 +00002350 $1.PAT->get()->getDescription());
Reid Spencerefd53d52007-01-26 08:18:34 +00002351
2352 // ConstExprs can exist in the body of a function, thus creating
2353 // GlobalValues whenever they refer to a variable. Because we are in
2354 // the context of a function, getExistingValue will search the functions
2355 // symbol table instead of the module symbol table for the global symbol,
2356 // which throws things all off. To get around this, we just tell
2357 // getExistingValue that we are at global scope here.
2358 //
2359 Function *SavedCurFn = CurFun.CurrentFunction;
2360 CurFun.CurrentFunction = 0;
Reid Spencer3e5affd2007-03-21 17:14:36 +00002361 $2.S.copy($1.S);
Reid Spencerefd53d52007-01-26 08:18:34 +00002362 Value *V = getExistingValue(Ty, $2);
2363 CurFun.CurrentFunction = SavedCurFn;
2364
2365 // If this is an initializer for a constant pointer, which is referencing a
2366 // (currently) undefined variable, create a stub now that shall be replaced
2367 // in the future with the right type of variable.
2368 //
2369 if (V == 0) {
2370 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers");
2371 const PointerType *PT = cast<PointerType>(Ty);
2372
2373 // First check to see if the forward references value is already created!
2374 PerModuleInfo::GlobalRefsType::iterator I =
2375 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
2376
2377 if (I != CurModule.GlobalRefs.end()) {
2378 V = I->second; // Placeholder already exists, use it...
2379 $2.destroy();
2380 } else {
2381 std::string Name;
2382 if ($2.Type == ValID::NameVal) Name = $2.Name;
2383
2384 // Create the forward referenced global.
2385 GlobalValue *GV;
2386 if (const FunctionType *FTy =
2387 dyn_cast<FunctionType>(PT->getElementType())) {
2388 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
2389 CurModule.CurrentModule);
2390 } else {
2391 GV = new GlobalVariable(PT->getElementType(), false,
2392 GlobalValue::ExternalLinkage, 0,
2393 Name, CurModule.CurrentModule);
2394 }
2395
2396 // Keep track of the fact that we have a forward ref to recycle it
2397 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
2398 V = GV;
2399 }
2400 }
2401 $$.C = cast<GlobalValue>(V);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002402 $$.S.copy($1.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00002403 delete $1.PAT; // Free the type handle
Reid Spencerefd53d52007-01-26 08:18:34 +00002404 }
2405 | Types ConstExpr {
Reid Spencere7c326b2007-02-08 08:09:36 +00002406 if ($1.PAT->get() != $2.C->getType())
Reid Spencerefd53d52007-01-26 08:18:34 +00002407 error("Mismatched types for constant expression");
2408 $$ = $2;
Reid Spencer3e5affd2007-03-21 17:14:36 +00002409 $$.S.copy($1.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00002410 delete $1.PAT;
Reid Spencerefd53d52007-01-26 08:18:34 +00002411 }
2412 | Types ZEROINITIALIZER {
Reid Spencere7c326b2007-02-08 08:09:36 +00002413 const Type *Ty = $1.PAT->get();
Reid Spencerefd53d52007-01-26 08:18:34 +00002414 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
2415 error("Cannot create a null initialized value of this type");
2416 $$.C = Constant::getNullValue(Ty);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002417 $$.S.copy($1.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00002418 delete $1.PAT;
Reid Spencerefd53d52007-01-26 08:18:34 +00002419 }
2420 | SIntType EINT64VAL { // integral constants
2421 const Type *Ty = $1.T;
2422 if (!ConstantInt::isValueValidForType(Ty, $2))
2423 error("Constant value doesn't fit in type");
2424 $$.C = ConstantInt::get(Ty, $2);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002425 $$.S.makeSigned();
Reid Spencerefd53d52007-01-26 08:18:34 +00002426 }
2427 | UIntType EUINT64VAL { // integral constants
2428 const Type *Ty = $1.T;
2429 if (!ConstantInt::isValueValidForType(Ty, $2))
2430 error("Constant value doesn't fit in type");
2431 $$.C = ConstantInt::get(Ty, $2);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002432 $$.S.makeUnsigned();
Reid Spencerefd53d52007-01-26 08:18:34 +00002433 }
2434 | BOOL TRUETOK { // Boolean constants
2435 $$.C = ConstantInt::get(Type::Int1Ty, true);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002436 $$.S.makeUnsigned();
Reid Spencerefd53d52007-01-26 08:18:34 +00002437 }
2438 | BOOL FALSETOK { // Boolean constants
2439 $$.C = ConstantInt::get(Type::Int1Ty, false);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002440 $$.S.makeUnsigned();
Reid Spencerefd53d52007-01-26 08:18:34 +00002441 }
2442 | FPType FPVAL { // Float & Double constants
Dale Johannesen43421b32007-09-06 18:13:44 +00002443 if (!ConstantFP::isValueValidForType($1.T, *$2))
Reid Spencerefd53d52007-01-26 08:18:34 +00002444 error("Floating point constant invalid for type");
Dale Johannesen43421b32007-09-06 18:13:44 +00002445 // Lexer has no type info, so builds all FP constants as double.
2446 // Fix this here.
2447 if ($1.T==Type::FloatTy)
2448 $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
2449 $$.C = ConstantFP::get($1.T, *$2);
Dale Johannesencdd509a2007-09-07 21:07:57 +00002450 delete $2;
Reid Spencer3e5affd2007-03-21 17:14:36 +00002451 $$.S.makeSignless();
Reid Spencerefd53d52007-01-26 08:18:34 +00002452 }
2453 ;
2454
2455ConstExpr
2456 : CastOps '(' ConstVal TO Types ')' {
2457 const Type* SrcTy = $3.C->getType();
Reid Spencere7c326b2007-02-08 08:09:36 +00002458 const Type* DstTy = $5.PAT->get();
Reid Spencer3e5affd2007-03-21 17:14:36 +00002459 Signedness SrcSign($3.S);
2460 Signedness DstSign($5.S);
Reid Spencerefd53d52007-01-26 08:18:34 +00002461 if (!SrcTy->isFirstClassType())
2462 error("cast constant expression from a non-primitive type: '" +
2463 SrcTy->getDescription() + "'");
2464 if (!DstTy->isFirstClassType())
2465 error("cast constant expression to a non-primitive type: '" +
2466 DstTy->getDescription() + "'");
2467 $$.C = cast<Constant>(getCast($1, $3.C, SrcSign, DstTy, DstSign));
Reid Spencer3e5affd2007-03-21 17:14:36 +00002468 $$.S.copy(DstSign);
Reid Spencere7c326b2007-02-08 08:09:36 +00002469 delete $5.PAT;
Reid Spencerefd53d52007-01-26 08:18:34 +00002470 }
2471 | GETELEMENTPTR '(' ConstVal IndexList ')' {
2472 const Type *Ty = $3.C->getType();
2473 if (!isa<PointerType>(Ty))
2474 error("GetElementPtr requires a pointer operand");
2475
Reid Spencerefd53d52007-01-26 08:18:34 +00002476 std::vector<Constant*> CIndices;
Reid Spencerd23c4dd2007-04-16 00:39:39 +00002477 upgradeGEPCEIndices($3.C->getType(), $4, CIndices);
Reid Spencerefd53d52007-01-26 08:18:34 +00002478
2479 delete $4;
Chris Lattner4227bdb2007-02-19 07:34:02 +00002480 $$.C = ConstantExpr::getGetElementPtr($3.C, &CIndices[0], CIndices.size());
Reid Spencer3e5affd2007-03-21 17:14:36 +00002481 $$.S.copy(getElementSign($3, CIndices));
Reid Spencerefd53d52007-01-26 08:18:34 +00002482 }
Reid Spencere7c3c602006-11-30 06:36:44 +00002483 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencerefd53d52007-01-26 08:18:34 +00002484 if (!$3.C->getType()->isInteger() ||
2485 cast<IntegerType>($3.C->getType())->getBitWidth() != 1)
2486 error("Select condition must be bool type");
2487 if ($5.C->getType() != $7.C->getType())
2488 error("Select operand types must match");
2489 $$.C = ConstantExpr::getSelect($3.C, $5.C, $7.C);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002490 $$.S.copy($5.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002491 }
2492 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencerefd53d52007-01-26 08:18:34 +00002493 const Type *Ty = $3.C->getType();
2494 if (Ty != $5.C->getType())
2495 error("Binary operator types must match");
2496 // First, make sure we're dealing with the right opcode by upgrading from
2497 // obsolete versions.
2498 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $3.S);
2499
2500 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
2501 // To retain backward compatibility with these early compilers, we emit a
2502 // cast to the appropriate integer type automatically if we are in the
2503 // broken case. See PR424 for more information.
2504 if (!isa<PointerType>(Ty)) {
2505 $$.C = ConstantExpr::get(Opcode, $3.C, $5.C);
2506 } else {
2507 const Type *IntPtrTy = 0;
2508 switch (CurModule.CurrentModule->getPointerSize()) {
2509 case Module::Pointer32: IntPtrTy = Type::Int32Ty; break;
2510 case Module::Pointer64: IntPtrTy = Type::Int64Ty; break;
2511 default: error("invalid pointer binary constant expr");
2512 }
2513 $$.C = ConstantExpr::get(Opcode,
2514 ConstantExpr::getCast(Instruction::PtrToInt, $3.C, IntPtrTy),
2515 ConstantExpr::getCast(Instruction::PtrToInt, $5.C, IntPtrTy));
2516 $$.C = ConstantExpr::getCast(Instruction::IntToPtr, $$.C, Ty);
2517 }
Reid Spencer3e5affd2007-03-21 17:14:36 +00002518 $$.S.copy($3.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002519 }
2520 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencerefd53d52007-01-26 08:18:34 +00002521 const Type* Ty = $3.C->getType();
2522 if (Ty != $5.C->getType())
2523 error("Logical operator types must match");
2524 if (!Ty->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00002525 if (!isa<VectorType>(Ty) ||
2526 !cast<VectorType>(Ty)->getElementType()->isInteger())
Reid Spencerefd53d52007-01-26 08:18:34 +00002527 error("Logical operator requires integer operands");
2528 }
2529 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $3.S);
2530 $$.C = ConstantExpr::get(Opcode, $3.C, $5.C);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002531 $$.S.copy($3.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002532 }
2533 | SetCondOps '(' ConstVal ',' ConstVal ')' {
Reid Spencerefd53d52007-01-26 08:18:34 +00002534 const Type* Ty = $3.C->getType();
2535 if (Ty != $5.C->getType())
2536 error("setcc operand types must match");
2537 unsigned short pred;
2538 Instruction::OtherOps Opcode = getCompareOp($1, pred, Ty, $3.S);
2539 $$.C = ConstantExpr::getCompare(Opcode, $3.C, $5.C);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002540 $$.S.makeUnsigned();
Reid Spencere7c3c602006-11-30 06:36:44 +00002541 }
Reid Spencer57f28f92006-12-03 07:10:26 +00002542 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencerefd53d52007-01-26 08:18:34 +00002543 if ($4.C->getType() != $6.C->getType())
2544 error("icmp operand types must match");
2545 $$.C = ConstantExpr::getCompare($2, $4.C, $6.C);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002546 $$.S.makeUnsigned();
Reid Spencer57f28f92006-12-03 07:10:26 +00002547 }
2548 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencerefd53d52007-01-26 08:18:34 +00002549 if ($4.C->getType() != $6.C->getType())
2550 error("fcmp operand types must match");
2551 $$.C = ConstantExpr::getCompare($2, $4.C, $6.C);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002552 $$.S.makeUnsigned();
Reid Spencer2b404382006-12-02 22:09:27 +00002553 }
Reid Spencere7c3c602006-11-30 06:36:44 +00002554 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencerefd53d52007-01-26 08:18:34 +00002555 if (!$5.C->getType()->isInteger() ||
2556 cast<IntegerType>($5.C->getType())->getBitWidth() != 8)
2557 error("Shift count for shift constant must be unsigned byte");
Reid Spencer832254e2007-02-02 02:16:23 +00002558 const Type* Ty = $3.C->getType();
Reid Spencerefd53d52007-01-26 08:18:34 +00002559 if (!$3.C->getType()->isInteger())
2560 error("Shift constant expression requires integer operand");
Reid Spencer832254e2007-02-02 02:16:23 +00002561 Constant *ShiftAmt = ConstantExpr::getZExt($5.C, Ty);
2562 $$.C = ConstantExpr::get(getBinaryOp($1, Ty, $3.S), $3.C, ShiftAmt);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002563 $$.S.copy($3.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002564 }
2565 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencerefd53d52007-01-26 08:18:34 +00002566 if (!ExtractElementInst::isValidOperands($3.C, $5.C))
2567 error("Invalid extractelement operands");
2568 $$.C = ConstantExpr::getExtractElement($3.C, $5.C);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002569 $$.S.copy($3.S.get(0));
Reid Spencere7c3c602006-11-30 06:36:44 +00002570 }
2571 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencerefd53d52007-01-26 08:18:34 +00002572 if (!InsertElementInst::isValidOperands($3.C, $5.C, $7.C))
2573 error("Invalid insertelement operands");
2574 $$.C = ConstantExpr::getInsertElement($3.C, $5.C, $7.C);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002575 $$.S.copy($3.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002576 }
2577 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencerefd53d52007-01-26 08:18:34 +00002578 if (!ShuffleVectorInst::isValidOperands($3.C, $5.C, $7.C))
2579 error("Invalid shufflevector operands");
2580 $$.C = ConstantExpr::getShuffleVector($3.C, $5.C, $7.C);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002581 $$.S.copy($3.S);
Reid Spencerefd53d52007-01-26 08:18:34 +00002582 }
2583 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002584
2585
2586// ConstVector - A list of comma separated constants.
Reid Spencere77e35e2006-12-01 20:26:20 +00002587ConstVector
Reid Spencerefd53d52007-01-26 08:18:34 +00002588 : ConstVector ',' ConstVal { ($$ = $1)->push_back($3); }
2589 | ConstVal {
2590 $$ = new std::vector<ConstInfo>();
2591 $$->push_back($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00002592 }
Reid Spencere77e35e2006-12-01 20:26:20 +00002593 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002594
2595
2596// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Reid Spencerefd53d52007-01-26 08:18:34 +00002597GlobalType
2598 : GLOBAL { $$ = false; }
2599 | CONSTANT { $$ = true; }
2600 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002601
2602
2603//===----------------------------------------------------------------------===//
2604// Rules to match Modules
2605//===----------------------------------------------------------------------===//
2606
2607// Module rule: Capture the result of parsing the whole file into a result
2608// variable...
2609//
Reid Spencerefd53d52007-01-26 08:18:34 +00002610Module
2611 : FunctionList {
2612 $$ = ParserResult = $1;
2613 CurModule.ModuleDone();
Reid Spencere7c3c602006-11-30 06:36:44 +00002614 }
Jeff Cohenac2dca92007-01-21 19:30:52 +00002615 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002616
Reid Spencerefd53d52007-01-26 08:18:34 +00002617// FunctionList - A list of functions, preceeded by a constant pool.
2618//
2619FunctionList
2620 : FunctionList Function { $$ = $1; CurFun.FunctionDone(); }
2621 | FunctionList FunctionProto { $$ = $1; }
2622 | FunctionList MODULE ASM_TOK AsmBlock { $$ = $1; }
2623 | FunctionList IMPLEMENTATION { $$ = $1; }
2624 | ConstPool {
2625 $$ = CurModule.CurrentModule;
2626 // Emit an error if there are any unresolved types left.
2627 if (!CurModule.LateResolveTypes.empty()) {
2628 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
2629 if (DID.Type == ValID::NameVal) {
2630 error("Reference to an undefined type: '"+DID.getName() + "'");
2631 } else {
2632 error("Reference to an undefined type: #" + itostr(DID.Num));
2633 }
2634 }
2635 }
2636 ;
Reid Spencera8ca0902006-12-02 20:19:56 +00002637
Reid Spencere7c3c602006-11-30 06:36:44 +00002638// ConstPool - Constants with optional names assigned to them.
Reid Spencerefd53d52007-01-26 08:18:34 +00002639ConstPool
2640 : ConstPool OptAssign TYPE TypesV {
2641 // Eagerly resolve types. This is not an optimization, this is a
2642 // requirement that is due to the fact that we could have this:
2643 //
2644 // %list = type { %list * }
2645 // %list = type { %list * } ; repeated type decl
2646 //
2647 // If types are not resolved eagerly, then the two types will not be
2648 // determined to be the same type!
2649 //
Reid Spencer3e5affd2007-03-21 17:14:36 +00002650 ResolveTypeTo($2, $4.PAT->get(), $4.S);
Reid Spencerefd53d52007-01-26 08:18:34 +00002651
Reid Spencer3e5affd2007-03-21 17:14:36 +00002652 if (!setTypeName($4, $2) && !$2) {
2653 // If this is a numbered type that is not a redefinition, add it to the
2654 // slot table.
2655 CurModule.Types.push_back($4.PAT->get());
2656 CurModule.TypeSigns.push_back($4.S);
Reid Spencera50d5962006-12-02 04:11:07 +00002657 }
Reid Spencere7c326b2007-02-08 08:09:36 +00002658 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002659 }
2660 | ConstPool FunctionProto { // Function prototypes can be in const pool
Reid Spencere7c3c602006-11-30 06:36:44 +00002661 }
2662 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
Reid Spencere7c3c602006-11-30 06:36:44 +00002663 }
Reid Spencerefd53d52007-01-26 08:18:34 +00002664 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
2665 if ($5.C == 0)
2666 error("Global value initializer is not a constant");
Reid Spencer3e5affd2007-03-21 17:14:36 +00002667 CurGV = ParseGlobalVariable($2, $3, $4, $5.C->getType(), $5.C, $5.S);
Reid Spencerefd53d52007-01-26 08:18:34 +00002668 } GlobalVarAttributes {
2669 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002670 }
Reid Spencerefd53d52007-01-26 08:18:34 +00002671 | ConstPool OptAssign EXTERNAL GlobalType Types {
Reid Spencere7c326b2007-02-08 08:09:36 +00002672 const Type *Ty = $5.PAT->get();
Reid Spencer3e5affd2007-03-21 17:14:36 +00002673 CurGV = ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, Ty, 0,
2674 $5.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00002675 delete $5.PAT;
Reid Spencerefd53d52007-01-26 08:18:34 +00002676 } GlobalVarAttributes {
2677 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002678 }
Reid Spencerefd53d52007-01-26 08:18:34 +00002679 | ConstPool OptAssign DLLIMPORT GlobalType Types {
Reid Spencere7c326b2007-02-08 08:09:36 +00002680 const Type *Ty = $5.PAT->get();
Reid Spencer3e5affd2007-03-21 17:14:36 +00002681 CurGV = ParseGlobalVariable($2, GlobalValue::DLLImportLinkage, $4, Ty, 0,
2682 $5.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00002683 delete $5.PAT;
Reid Spencerefd53d52007-01-26 08:18:34 +00002684 } GlobalVarAttributes {
2685 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002686 }
Reid Spencerefd53d52007-01-26 08:18:34 +00002687 | ConstPool OptAssign EXTERN_WEAK GlobalType Types {
Reid Spencere7c326b2007-02-08 08:09:36 +00002688 const Type *Ty = $5.PAT->get();
Reid Spencerefd53d52007-01-26 08:18:34 +00002689 CurGV =
Reid Spencer3e5affd2007-03-21 17:14:36 +00002690 ParseGlobalVariable($2, GlobalValue::ExternalWeakLinkage, $4, Ty, 0,
2691 $5.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00002692 delete $5.PAT;
Reid Spencerefd53d52007-01-26 08:18:34 +00002693 } GlobalVarAttributes {
2694 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002695 }
2696 | ConstPool TARGET TargetDefinition {
Reid Spencere7c3c602006-11-30 06:36:44 +00002697 }
2698 | ConstPool DEPLIBS '=' LibrariesDefinition {
Reid Spencere7c3c602006-11-30 06:36:44 +00002699 }
2700 | /* empty: end of list */ {
Reid Spencerefd53d52007-01-26 08:18:34 +00002701 }
2702 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002703
Reid Spencerefd53d52007-01-26 08:18:34 +00002704AsmBlock
2705 : STRINGCONSTANT {
2706 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
2707 char *EndStr = UnEscapeLexed($1, true);
2708 std::string NewAsm($1, EndStr);
2709 free($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00002710
Reid Spencerefd53d52007-01-26 08:18:34 +00002711 if (AsmSoFar.empty())
2712 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
2713 else
2714 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
2715 }
2716 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002717
Reid Spencerefd53d52007-01-26 08:18:34 +00002718BigOrLittle
Reid Spencer7e972882007-01-26 18:26:23 +00002719 : BIG { $$ = Module::BigEndian; }
Reid Spencerefd53d52007-01-26 08:18:34 +00002720 | LITTLE { $$ = Module::LittleEndian; }
2721 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002722
2723TargetDefinition
2724 : ENDIAN '=' BigOrLittle {
Reid Spencerefd53d52007-01-26 08:18:34 +00002725 CurModule.setEndianness($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00002726 }
2727 | POINTERSIZE '=' EUINT64VAL {
Reid Spencerefd53d52007-01-26 08:18:34 +00002728 if ($3 == 32)
2729 CurModule.setPointerSize(Module::Pointer32);
2730 else if ($3 == 64)
2731 CurModule.setPointerSize(Module::Pointer64);
2732 else
2733 error("Invalid pointer size: '" + utostr($3) + "'");
Reid Spencere7c3c602006-11-30 06:36:44 +00002734 }
2735 | TRIPLE '=' STRINGCONSTANT {
Reid Spencerefd53d52007-01-26 08:18:34 +00002736 CurModule.CurrentModule->setTargetTriple($3);
2737 free($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00002738 }
2739 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencerefd53d52007-01-26 08:18:34 +00002740 CurModule.CurrentModule->setDataLayout($3);
2741 free($3);
2742 }
2743 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002744
2745LibrariesDefinition
Reid Spencerefd53d52007-01-26 08:18:34 +00002746 : '[' LibList ']'
2747 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002748
2749LibList
2750 : LibList ',' STRINGCONSTANT {
Reid Spencerefd53d52007-01-26 08:18:34 +00002751 CurModule.CurrentModule->addLibrary($3);
2752 free($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00002753 }
Reid Spencerefd53d52007-01-26 08:18:34 +00002754 | STRINGCONSTANT {
2755 CurModule.CurrentModule->addLibrary($1);
2756 free($1);
2757 }
2758 | /* empty: end of list */ { }
2759 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002760
2761//===----------------------------------------------------------------------===//
2762// Rules to match Function Headers
2763//===----------------------------------------------------------------------===//
2764
Reid Spencerefd53d52007-01-26 08:18:34 +00002765Name
2766 : VAR_ID | STRINGCONSTANT
2767 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002768
Reid Spencerefd53d52007-01-26 08:18:34 +00002769OptName
2770 : Name
2771 | /*empty*/ { $$ = 0; }
2772 ;
2773
2774ArgVal
2775 : Types OptName {
Reid Spencere7c326b2007-02-08 08:09:36 +00002776 if ($1.PAT->get() == Type::VoidTy)
Reid Spencerefd53d52007-01-26 08:18:34 +00002777 error("void typed arguments are invalid");
2778 $$ = new std::pair<PATypeInfo, char*>($1, $2);
Reid Spencer2b8036e2007-01-02 05:44:33 +00002779 }
Reid Spencerefd53d52007-01-26 08:18:34 +00002780 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002781
Reid Spencerefd53d52007-01-26 08:18:34 +00002782ArgListH
2783 : ArgListH ',' ArgVal {
2784 $$ = $1;
2785 $$->push_back(*$3);
Reid Spencere77e35e2006-12-01 20:26:20 +00002786 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002787 }
2788 | ArgVal {
Reid Spencerefd53d52007-01-26 08:18:34 +00002789 $$ = new std::vector<std::pair<PATypeInfo,char*> >();
2790 $$->push_back(*$1);
2791 delete $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00002792 }
Reid Spencerefd53d52007-01-26 08:18:34 +00002793 ;
2794
2795ArgList
2796 : ArgListH { $$ = $1; }
Reid Spencere7c3c602006-11-30 06:36:44 +00002797 | ArgListH ',' DOTDOTDOT {
Reid Spencere7c3c602006-11-30 06:36:44 +00002798 $$ = $1;
Reid Spencerefd53d52007-01-26 08:18:34 +00002799 PATypeInfo VoidTI;
Reid Spencere7c326b2007-02-08 08:09:36 +00002800 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002801 VoidTI.S.makeSignless();
Reid Spencerefd53d52007-01-26 08:18:34 +00002802 $$->push_back(std::pair<PATypeInfo, char*>(VoidTI, 0));
Reid Spencere7c3c602006-11-30 06:36:44 +00002803 }
2804 | DOTDOTDOT {
Reid Spencerefd53d52007-01-26 08:18:34 +00002805 $$ = new std::vector<std::pair<PATypeInfo,char*> >();
2806 PATypeInfo VoidTI;
Reid Spencere7c326b2007-02-08 08:09:36 +00002807 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002808 VoidTI.S.makeSignless();
Reid Spencerefd53d52007-01-26 08:18:34 +00002809 $$->push_back(std::pair<PATypeInfo, char*>(VoidTI, 0));
Reid Spencere7c3c602006-11-30 06:36:44 +00002810 }
Reid Spencerefd53d52007-01-26 08:18:34 +00002811 | /* empty */ { $$ = 0; }
2812 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002813
Reid Spencer05e52a12006-12-31 05:45:57 +00002814FunctionHeaderH
2815 : OptCallingConv TypesV Name '(' ArgList ')' OptSection OptAlign {
Reid Spencerefd53d52007-01-26 08:18:34 +00002816 UnEscapeLexed($3);
2817 std::string FunctionName($3);
2818 free($3); // Free strdup'd memory!
Reid Spencere7c3c602006-11-30 06:36:44 +00002819
Reid Spencere7c326b2007-02-08 08:09:36 +00002820 const Type* RetTy = $2.PAT->get();
Reid Spencerefd53d52007-01-26 08:18:34 +00002821
2822 if (!RetTy->isFirstClassType() && RetTy != Type::VoidTy)
2823 error("LLVM functions cannot return aggregate types");
2824
Reid Spencer3e5affd2007-03-21 17:14:36 +00002825 Signedness FTySign;
2826 FTySign.makeComposite($2.S);
Reid Spenceref9b9a72007-02-05 20:47:22 +00002827 std::vector<const Type*> ParamTyList;
Reid Spencerefd53d52007-01-26 08:18:34 +00002828
2829 // In LLVM 2.0 the signatures of three varargs intrinsics changed to take
2830 // i8*. We check here for those names and override the parameter list
2831 // types to ensure the prototype is correct.
2832 if (FunctionName == "llvm.va_start" || FunctionName == "llvm.va_end") {
Christopher Lamb43ad6b32007-12-17 01:12:55 +00002833 ParamTyList.push_back(PointerType::getUnqual(Type::Int8Ty));
Reid Spencerefd53d52007-01-26 08:18:34 +00002834 } else if (FunctionName == "llvm.va_copy") {
Christopher Lamb43ad6b32007-12-17 01:12:55 +00002835 ParamTyList.push_back(PointerType::getUnqual(Type::Int8Ty));
2836 ParamTyList.push_back(PointerType::getUnqual(Type::Int8Ty));
Reid Spencerefd53d52007-01-26 08:18:34 +00002837 } else if ($5) { // If there are arguments...
2838 for (std::vector<std::pair<PATypeInfo,char*> >::iterator
2839 I = $5->begin(), E = $5->end(); I != E; ++I) {
Reid Spencere7c326b2007-02-08 08:09:36 +00002840 const Type *Ty = I->first.PAT->get();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002841 ParamTyList.push_back(Ty);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002842 FTySign.add(I->first.S);
Reid Spencerefd53d52007-01-26 08:18:34 +00002843 }
2844 }
2845
Reid Spenceref9b9a72007-02-05 20:47:22 +00002846 bool isVarArg = ParamTyList.size() && ParamTyList.back() == Type::VoidTy;
2847 if (isVarArg)
2848 ParamTyList.pop_back();
Reid Spencerefd53d52007-01-26 08:18:34 +00002849
Duncan Sandsdc024672007-11-27 13:23:08 +00002850 const FunctionType *FT = FunctionType::get(RetTy, ParamTyList, isVarArg);
Christopher Lamb43ad6b32007-12-17 01:12:55 +00002851 const PointerType *PFT = PointerType::getUnqual(FT);
Reid Spencere7c326b2007-02-08 08:09:36 +00002852 delete $2.PAT;
Reid Spencerefd53d52007-01-26 08:18:34 +00002853
2854 ValID ID;
2855 if (!FunctionName.empty()) {
Reid Spencer44f87ee2007-03-15 03:25:34 +00002856 ID = ValID::create((char*)FunctionName.c_str());
Reid Spencerefd53d52007-01-26 08:18:34 +00002857 } else {
Reid Spencer44f87ee2007-03-15 03:25:34 +00002858 ID = ValID::create((int)CurModule.Values[PFT].size());
Reid Spencerefd53d52007-01-26 08:18:34 +00002859 }
Reid Spencer3e5affd2007-03-21 17:14:36 +00002860 ID.S.makeComposite(FTySign);
Reid Spencerefd53d52007-01-26 08:18:34 +00002861
2862 Function *Fn = 0;
Reid Spencere7c326b2007-02-08 08:09:36 +00002863 Module* M = CurModule.CurrentModule;
2864
Reid Spencerefd53d52007-01-26 08:18:34 +00002865 // See if this function was forward referenced. If so, recycle the object.
2866 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2867 // Move the function to the end of the list, from whereever it was
2868 // previously inserted.
2869 Fn = cast<Function>(FWRef);
Reid Spencere7c326b2007-02-08 08:09:36 +00002870 M->getFunctionList().remove(Fn);
2871 M->getFunctionList().push_back(Fn);
2872 } else if (!FunctionName.empty()) {
2873 GlobalValue *Conflict = M->getFunction(FunctionName);
2874 if (!Conflict)
2875 Conflict = M->getNamedGlobal(FunctionName);
2876 if (Conflict && PFT == Conflict->getType()) {
2877 if (!CurFun.isDeclare && !Conflict->isDeclaration()) {
2878 // We have two function definitions that conflict, same type, same
Reid Spencer39a97922007-02-08 08:47:38 +00002879 // name. We should really check to make sure that this is the result
2880 // of integer type planes collapsing and generate an error if it is
2881 // not, but we'll just rename on the assumption that it is. However,
2882 // let's do it intelligently and rename the internal linkage one
2883 // if there is one.
2884 std::string NewName(makeNameUnique(FunctionName));
2885 if (Conflict->hasInternalLinkage()) {
2886 Conflict->setName(NewName);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002887 RenameMapKey Key =
2888 makeRenameMapKey(FunctionName, Conflict->getType(), ID.S);
Reid Spencer39a97922007-02-08 08:47:38 +00002889 CurModule.RenameMap[Key] = NewName;
2890 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
2891 InsertValue(Fn, CurModule.Values);
2892 } else {
2893 Fn = new Function(FT, CurFun.Linkage, NewName, M);
2894 InsertValue(Fn, CurModule.Values);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002895 RenameMapKey Key =
2896 makeRenameMapKey(FunctionName, PFT, ID.S);
Reid Spencer39a97922007-02-08 08:47:38 +00002897 CurModule.RenameMap[Key] = NewName;
2898 }
Reid Spencere7c326b2007-02-08 08:09:36 +00002899 } else {
2900 // If they are not both definitions, then just use the function we
2901 // found since the types are the same.
2902 Fn = cast<Function>(Conflict);
Reid Spenceref9b9a72007-02-05 20:47:22 +00002903
Reid Spencere7c326b2007-02-08 08:09:36 +00002904 // Make sure to strip off any argument names so we can't get
2905 // conflicts.
2906 if (Fn->isDeclaration())
2907 for (Function::arg_iterator AI = Fn->arg_begin(),
2908 AE = Fn->arg_end(); AI != AE; ++AI)
2909 AI->setName("");
2910 }
2911 } else if (Conflict) {
Reid Spencer53bd7042007-04-16 02:56:33 +00002912 // We have two globals with the same name and different types.
Reid Spencere7c326b2007-02-08 08:09:36 +00002913 // Previously, this was permitted because the symbol table had
2914 // "type planes" and names only needed to be distinct within a
2915 // type plane. After PR411 was fixed, this is no loner the case.
2916 // To resolve this we must rename one of the two.
2917 if (Conflict->hasInternalLinkage()) {
Reid Spencer3e5affd2007-03-21 17:14:36 +00002918 // We can safely rename the Conflict.
2919 RenameMapKey Key =
2920 makeRenameMapKey(Conflict->getName(), Conflict->getType(),
2921 CurModule.NamedValueSigns[Conflict->getName()]);
Reid Spencere7c326b2007-02-08 08:09:36 +00002922 Conflict->setName(makeNameUnique(Conflict->getName()));
Reid Spencere7c326b2007-02-08 08:09:36 +00002923 CurModule.RenameMap[Key] = Conflict->getName();
2924 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
2925 InsertValue(Fn, CurModule.Values);
Reid Spencer91a9d542007-03-21 17:26:41 +00002926 } else {
Reid Spencere7c326b2007-02-08 08:09:36 +00002927 // We can't quietly rename either of these things, but we must
Reid Spencer91a9d542007-03-21 17:26:41 +00002928 // rename one of them. Only if the function's linkage is internal can
2929 // we forgo a warning message about the renamed function.
Reid Spencere7c326b2007-02-08 08:09:36 +00002930 std::string NewName = makeNameUnique(FunctionName);
Reid Spencer91a9d542007-03-21 17:26:41 +00002931 if (CurFun.Linkage != GlobalValue::InternalLinkage) {
2932 warning("Renaming function '" + FunctionName + "' as '" + NewName +
2933 "' may cause linkage errors");
2934 }
2935 // Elect to rename the thing we're now defining.
Reid Spencere7c326b2007-02-08 08:09:36 +00002936 Fn = new Function(FT, CurFun.Linkage, NewName, M);
2937 InsertValue(Fn, CurModule.Values);
Reid Spencer3e5affd2007-03-21 17:14:36 +00002938 RenameMapKey Key = makeRenameMapKey(FunctionName, PFT, ID.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00002939 CurModule.RenameMap[Key] = NewName;
Reid Spencer91a9d542007-03-21 17:26:41 +00002940 }
Reid Spenceref9b9a72007-02-05 20:47:22 +00002941 } else {
Reid Spencere7c326b2007-02-08 08:09:36 +00002942 // There's no conflict, just define the function
2943 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
2944 InsertValue(Fn, CurModule.Values);
Reid Spenceref9b9a72007-02-05 20:47:22 +00002945 }
Reid Spencer7055daf2007-04-16 03:04:13 +00002946 } else {
2947 // There's no conflict, just define the function
2948 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
2949 InsertValue(Fn, CurModule.Values);
Reid Spencerefd53d52007-01-26 08:18:34 +00002950 }
2951
Reid Spencer7055daf2007-04-16 03:04:13 +00002952
Reid Spencerefd53d52007-01-26 08:18:34 +00002953 CurFun.FunctionStart(Fn);
2954
2955 if (CurFun.isDeclare) {
2956 // If we have declaration, always overwrite linkage. This will allow us
2957 // to correctly handle cases, when pointer to function is passed as
2958 // argument to another function.
2959 Fn->setLinkage(CurFun.Linkage);
2960 }
Reid Spencer43f76c92007-01-29 05:41:09 +00002961 Fn->setCallingConv(upgradeCallingConv($1));
Reid Spencerefd53d52007-01-26 08:18:34 +00002962 Fn->setAlignment($8);
2963 if ($7) {
2964 Fn->setSection($7);
2965 free($7);
2966 }
2967
Duncan Sandsdc024672007-11-27 13:23:08 +00002968 // Convert the CSRet calling convention into the corresponding parameter
2969 // attribute.
2970 if ($1 == OldCallingConv::CSRet) {
Chris Lattner58d74912008-03-12 17:45:29 +00002971 ParamAttrsWithIndex PAWI =
2972 ParamAttrsWithIndex::get(1, ParamAttr::StructRet); // first arg
2973 Fn->setParamAttrs(PAListPtr::get(&PAWI, 1));
Duncan Sandsdc024672007-11-27 13:23:08 +00002974 }
2975
Reid Spencerefd53d52007-01-26 08:18:34 +00002976 // Add all of the arguments we parsed to the function...
2977 if ($5) { // Is null if empty...
2978 if (isVarArg) { // Nuke the last entry
Reid Spencere7c326b2007-02-08 08:09:36 +00002979 assert($5->back().first.PAT->get() == Type::VoidTy &&
Reid Spencerefd53d52007-01-26 08:18:34 +00002980 $5->back().second == 0 && "Not a varargs marker");
Reid Spencere7c326b2007-02-08 08:09:36 +00002981 delete $5->back().first.PAT;
Reid Spencerefd53d52007-01-26 08:18:34 +00002982 $5->pop_back(); // Delete the last entry
2983 }
2984 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002985 Function::arg_iterator ArgEnd = Fn->arg_end();
2986 std::vector<std::pair<PATypeInfo,char*> >::iterator I = $5->begin();
2987 std::vector<std::pair<PATypeInfo,char*> >::iterator E = $5->end();
2988 for ( ; I != E && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencere7c326b2007-02-08 08:09:36 +00002989 delete I->first.PAT; // Delete the typeholder...
Reid Spencer3e5affd2007-03-21 17:14:36 +00002990 ValueInfo VI; VI.V = ArgIt; VI.S.copy(I->first.S);
2991 setValueName(VI, I->second); // Insert arg into symtab...
Reid Spencerefd53d52007-01-26 08:18:34 +00002992 InsertValue(ArgIt);
2993 }
2994 delete $5; // We're now done with the argument list
2995 }
Reid Spencer4f859aa2007-04-22 05:46:44 +00002996 lastCallingConv = OldCallingConv::C;
Reid Spencerefd53d52007-01-26 08:18:34 +00002997 }
2998 ;
2999
3000BEGIN
3001 : BEGINTOK | '{' // Allow BEGIN or '{' to start a function
Jeff Cohenac2dca92007-01-21 19:30:52 +00003002 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003003
Reid Spencera9d7e892006-12-29 20:33:37 +00003004FunctionHeader
Reid Spencer91a9d542007-03-21 17:26:41 +00003005 : OptLinkage { CurFun.Linkage = $1; } FunctionHeaderH BEGIN {
Reid Spencerefd53d52007-01-26 08:18:34 +00003006 $$ = CurFun.CurrentFunction;
3007
3008 // Make sure that we keep track of the linkage type even if there was a
3009 // previous "declare".
3010 $$->setLinkage($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00003011 }
Reid Spencera9d7e892006-12-29 20:33:37 +00003012 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003013
Reid Spencerefd53d52007-01-26 08:18:34 +00003014END
3015 : ENDTOK | '}' // Allow end of '}' to end a function
3016 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003017
Reid Spencerefd53d52007-01-26 08:18:34 +00003018Function
3019 : BasicBlockList END {
3020 $$ = $1;
3021 };
Reid Spencere7c3c602006-11-30 06:36:44 +00003022
Reid Spencere77e35e2006-12-01 20:26:20 +00003023FnDeclareLinkage
Reid Spencere7c326b2007-02-08 08:09:36 +00003024 : /*default*/ { $$ = GlobalValue::ExternalLinkage; }
3025 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
3026 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencere7c3c602006-11-30 06:36:44 +00003027 ;
3028
3029FunctionProto
Reid Spencere7c326b2007-02-08 08:09:36 +00003030 : DECLARE { CurFun.isDeclare = true; }
3031 FnDeclareLinkage { CurFun.Linkage = $3; } FunctionHeaderH {
Reid Spencerefd53d52007-01-26 08:18:34 +00003032 $$ = CurFun.CurrentFunction;
3033 CurFun.FunctionDone();
3034
3035 }
3036 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003037
3038//===----------------------------------------------------------------------===//
3039// Rules to match Basic Blocks
3040//===----------------------------------------------------------------------===//
3041
Reid Spencerefd53d52007-01-26 08:18:34 +00003042OptSideEffect
3043 : /* empty */ { $$ = false; }
3044 | SIDEEFFECT { $$ = true; }
3045 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003046
Reid Spencere77e35e2006-12-01 20:26:20 +00003047ConstValueRef
Reid Spencerefd53d52007-01-26 08:18:34 +00003048 // A reference to a direct constant
Reid Spencer3e5affd2007-03-21 17:14:36 +00003049 : ESINT64VAL { $$ = ValID::create($1); }
Reid Spencerefd53d52007-01-26 08:18:34 +00003050 | EUINT64VAL { $$ = ValID::create($1); }
3051 | FPVAL { $$ = ValID::create($1); }
Reid Spencer3e5affd2007-03-21 17:14:36 +00003052 | TRUETOK {
3053 $$ = ValID::create(ConstantInt::get(Type::Int1Ty, true));
3054 $$.S.makeUnsigned();
3055 }
3056 | FALSETOK {
3057 $$ = ValID::create(ConstantInt::get(Type::Int1Ty, false));
3058 $$.S.makeUnsigned();
3059 }
Reid Spencerefd53d52007-01-26 08:18:34 +00003060 | NULL_TOK { $$ = ValID::createNull(); }
3061 | UNDEF { $$ = ValID::createUndef(); }
3062 | ZEROINITIALIZER { $$ = ValID::createZeroInit(); }
3063 | '<' ConstVector '>' { // Nonempty unsized packed vector
3064 const Type *ETy = (*$2)[0].C->getType();
3065 int NumElements = $2->size();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003066 VectorType* pt = VectorType::get(ETy, NumElements);
Reid Spencer3e5affd2007-03-21 17:14:36 +00003067 $$.S.makeComposite((*$2)[0].S);
3068 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(pt, $$.S));
Reid Spencerefd53d52007-01-26 08:18:34 +00003069
3070 // Verify all elements are correct type!
3071 std::vector<Constant*> Elems;
3072 for (unsigned i = 0; i < $2->size(); i++) {
3073 Constant *C = (*$2)[i].C;
3074 const Type *CTy = C->getType();
3075 if (ETy != CTy)
3076 error("Element #" + utostr(i) + " is not of type '" +
3077 ETy->getDescription() +"' as required!\nIt is of type '" +
3078 CTy->getDescription() + "'");
3079 Elems.push_back(C);
Reid Spencere7c3c602006-11-30 06:36:44 +00003080 }
Reid Spencer44f87ee2007-03-15 03:25:34 +00003081 $$ = ValID::create(ConstantVector::get(pt, Elems));
Reid Spencerefd53d52007-01-26 08:18:34 +00003082 delete PTy; delete $2;
3083 }
3084 | ConstExpr {
Reid Spencer44f87ee2007-03-15 03:25:34 +00003085 $$ = ValID::create($1.C);
Reid Spencer3e5affd2007-03-21 17:14:36 +00003086 $$.S.copy($1.S);
Reid Spencerefd53d52007-01-26 08:18:34 +00003087 }
3088 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
3089 char *End = UnEscapeLexed($3, true);
3090 std::string AsmStr = std::string($3, End);
3091 End = UnEscapeLexed($5, true);
3092 std::string Constraints = std::string($5, End);
3093 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
3094 free($3);
3095 free($5);
3096 }
3097 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003098
Christopher Lamb43ad6b32007-12-17 01:12:55 +00003099// SymbolicValueRef - Reference to one of two ways of symbolically refering to
3100// another value.
Reid Spencerefd53d52007-01-26 08:18:34 +00003101//
3102SymbolicValueRef
Reid Spencer3e5affd2007-03-21 17:14:36 +00003103 : INTVAL { $$ = ValID::create($1); $$.S.makeSignless(); }
3104 | Name { $$ = ValID::create($1); $$.S.makeSignless(); }
Reid Spencerefd53d52007-01-26 08:18:34 +00003105 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003106
3107// ValueRef - A reference to a definition... either constant or symbolic
Reid Spencer1d64a6c2006-12-02 16:19:28 +00003108ValueRef
Reid Spencerefd53d52007-01-26 08:18:34 +00003109 : SymbolicValueRef | ConstValueRef
Reid Spencer1d64a6c2006-12-02 16:19:28 +00003110 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003111
Reid Spencerefd53d52007-01-26 08:18:34 +00003112
Reid Spencere7c3c602006-11-30 06:36:44 +00003113// ResolvedVal - a <type> <value> pair. This is used only in cases where the
3114// type immediately preceeds the value reference, and allows complex constant
3115// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Reid Spencerefd53d52007-01-26 08:18:34 +00003116ResolvedVal
3117 : Types ValueRef {
Reid Spencere7c326b2007-02-08 08:09:36 +00003118 const Type *Ty = $1.PAT->get();
Reid Spencer3e5affd2007-03-21 17:14:36 +00003119 $2.S.copy($1.S);
Reid Spencer44f87ee2007-03-15 03:25:34 +00003120 $$.V = getVal(Ty, $2);
Reid Spencer3e5affd2007-03-21 17:14:36 +00003121 $$.S.copy($1.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00003122 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003123 }
Reid Spencerefd53d52007-01-26 08:18:34 +00003124 ;
3125
3126BasicBlockList
3127 : BasicBlockList BasicBlock {
3128 $$ = $1;
3129 }
3130 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
3131 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00003132 };
3133
3134
3135// Basic blocks are terminated by branching instructions:
3136// br, br/cc, switch, ret
3137//
Reid Spencerefd53d52007-01-26 08:18:34 +00003138BasicBlock
3139 : InstructionList OptAssign BBTerminatorInst {
Reid Spencer3e5affd2007-03-21 17:14:36 +00003140 ValueInfo VI; VI.V = $3.TI; VI.S.copy($3.S);
3141 setValueName(VI, $2);
3142 InsertValue($3.TI);
3143 $1->getInstList().push_back($3.TI);
Reid Spencerefd53d52007-01-26 08:18:34 +00003144 InsertValue($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00003145 $$ = $1;
3146 }
Reid Spencerefd53d52007-01-26 08:18:34 +00003147 ;
3148
3149InstructionList
3150 : InstructionList Inst {
3151 if ($2.I)
3152 $1->getInstList().push_back($2.I);
3153 $$ = $1;
3154 }
3155 | /* empty */ {
Reid Spencer3e5affd2007-03-21 17:14:36 +00003156 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++),true);
Reid Spencerefd53d52007-01-26 08:18:34 +00003157 // Make sure to move the basic block to the correct location in the
3158 // function, instead of leaving it inserted wherever it was first
3159 // referenced.
3160 Function::BasicBlockListType &BBL =
3161 CurFun.CurrentFunction->getBasicBlockList();
3162 BBL.splice(BBL.end(), BBL, $$);
3163 }
3164 | LABELSTR {
Reid Spencer44f87ee2007-03-15 03:25:34 +00003165 $$ = CurBB = getBBVal(ValID::create($1), true);
Reid Spencerefd53d52007-01-26 08:18:34 +00003166 // Make sure to move the basic block to the correct location in the
3167 // function, instead of leaving it inserted wherever it was first
3168 // referenced.
3169 Function::BasicBlockListType &BBL =
3170 CurFun.CurrentFunction->getBasicBlockList();
3171 BBL.splice(BBL.end(), BBL, $$);
3172 }
3173 ;
3174
3175Unwind : UNWIND | EXCEPT;
3176
3177BBTerminatorInst
3178 : RET ResolvedVal { // Return with a result...
Reid Spencer3e5affd2007-03-21 17:14:36 +00003179 $$.TI = new ReturnInst($2.V);
3180 $$.S.makeSignless();
Reid Spencerefd53d52007-01-26 08:18:34 +00003181 }
3182 | RET VOID { // Return with no result...
Reid Spencer3e5affd2007-03-21 17:14:36 +00003183 $$.TI = new ReturnInst();
3184 $$.S.makeSignless();
Reid Spencerefd53d52007-01-26 08:18:34 +00003185 }
3186 | BR LABEL ValueRef { // Unconditional Branch...
3187 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer3e5affd2007-03-21 17:14:36 +00003188 $$.TI = new BranchInst(tmpBB);
3189 $$.S.makeSignless();
Reid Spencerefd53d52007-01-26 08:18:34 +00003190 } // Conditional Branch...
3191 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencer3e5affd2007-03-21 17:14:36 +00003192 $6.S.makeSignless();
3193 $9.S.makeSignless();
Reid Spencerefd53d52007-01-26 08:18:34 +00003194 BasicBlock* tmpBBA = getBBVal($6);
3195 BasicBlock* tmpBBB = getBBVal($9);
Reid Spencer3e5affd2007-03-21 17:14:36 +00003196 $3.S.makeUnsigned();
Reid Spencerefd53d52007-01-26 08:18:34 +00003197 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer3e5affd2007-03-21 17:14:36 +00003198 $$.TI = new BranchInst(tmpBBA, tmpBBB, tmpVal);
3199 $$.S.makeSignless();
Reid Spencerefd53d52007-01-26 08:18:34 +00003200 }
3201 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencer3e5affd2007-03-21 17:14:36 +00003202 $3.S.copy($2.S);
Reid Spencerefd53d52007-01-26 08:18:34 +00003203 Value* tmpVal = getVal($2.T, $3);
Reid Spencer3e5affd2007-03-21 17:14:36 +00003204 $6.S.makeSignless();
Reid Spencerefd53d52007-01-26 08:18:34 +00003205 BasicBlock* tmpBB = getBBVal($6);
3206 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Reid Spencer3e5affd2007-03-21 17:14:36 +00003207 $$.TI = S;
3208 $$.S.makeSignless();
Reid Spencerefd53d52007-01-26 08:18:34 +00003209 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
3210 E = $8->end();
3211 for (; I != E; ++I) {
3212 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
3213 S->addCase(CI, I->second);
3214 else
3215 error("Switch case is constant, but not a simple integer");
3216 }
3217 delete $8;
3218 }
3219 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencer3e5affd2007-03-21 17:14:36 +00003220 $3.S.copy($2.S);
Reid Spencerefd53d52007-01-26 08:18:34 +00003221 Value* tmpVal = getVal($2.T, $3);
Reid Spencer3e5affd2007-03-21 17:14:36 +00003222 $6.S.makeSignless();
Reid Spencerefd53d52007-01-26 08:18:34 +00003223 BasicBlock* tmpBB = getBBVal($6);
3224 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Reid Spencer3e5affd2007-03-21 17:14:36 +00003225 $$.TI = S;
3226 $$.S.makeSignless();
Reid Spencerefd53d52007-01-26 08:18:34 +00003227 }
3228 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
3229 TO LABEL ValueRef Unwind LABEL ValueRef {
3230 const PointerType *PFTy;
3231 const FunctionType *Ty;
Reid Spencer3e5affd2007-03-21 17:14:36 +00003232 Signedness FTySign;
Reid Spencerefd53d52007-01-26 08:18:34 +00003233
Reid Spencere7c326b2007-02-08 08:09:36 +00003234 if (!(PFTy = dyn_cast<PointerType>($3.PAT->get())) ||
Reid Spencerefd53d52007-01-26 08:18:34 +00003235 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3236 // Pull out the types of all of the arguments...
3237 std::vector<const Type*> ParamTypes;
Reid Spencer3e5affd2007-03-21 17:14:36 +00003238 FTySign.makeComposite($3.S);
Reid Spencerefd53d52007-01-26 08:18:34 +00003239 if ($6) {
3240 for (std::vector<ValueInfo>::iterator I = $6->begin(), E = $6->end();
Reid Spencer3e5affd2007-03-21 17:14:36 +00003241 I != E; ++I) {
Reid Spencerefd53d52007-01-26 08:18:34 +00003242 ParamTypes.push_back((*I).V->getType());
Reid Spencer3e5affd2007-03-21 17:14:36 +00003243 FTySign.add(I->S);
3244 }
Reid Spencerefd53d52007-01-26 08:18:34 +00003245 }
3246 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
3247 if (isVarArg) ParamTypes.pop_back();
Duncan Sandsdc024672007-11-27 13:23:08 +00003248 Ty = FunctionType::get($3.PAT->get(), ParamTypes, isVarArg);
Christopher Lamb43ad6b32007-12-17 01:12:55 +00003249 PFTy = PointerType::getUnqual(Ty);
Reid Spencer3e5affd2007-03-21 17:14:36 +00003250 $$.S.copy($3.S);
3251 } else {
3252 FTySign = $3.S;
Reid Spencerb289f192007-04-07 16:10:37 +00003253 // Get the signedness of the result type. $3 is the pointer to the
3254 // function type so we get the 0th element to extract the function type,
3255 // and then the 0th element again to get the result type.
3256 $$.S.copy($3.S.get(0).get(0));
Reid Spencerefd53d52007-01-26 08:18:34 +00003257 }
Reid Spencer460dd9b2007-04-09 06:15:59 +00003258
Reid Spencer3e5affd2007-03-21 17:14:36 +00003259 $4.S.makeComposite(FTySign);
Reid Spencerefd53d52007-01-26 08:18:34 +00003260 Value *V = getVal(PFTy, $4); // Get the function we're calling...
3261 BasicBlock *Normal = getBBVal($10);
3262 BasicBlock *Except = getBBVal($13);
3263
3264 // Create the call node...
3265 if (!$6) { // Has no arguments?
David Greenef1355a52007-08-27 19:04:21 +00003266 std::vector<Value*> Args;
3267 $$.TI = new InvokeInst(V, Normal, Except, Args.begin(), Args.end());
Reid Spencerefd53d52007-01-26 08:18:34 +00003268 } else { // Has arguments?
3269 // Loop through FunctionType's arguments and ensure they are specified
3270 // correctly!
3271 //
3272 FunctionType::param_iterator I = Ty->param_begin();
3273 FunctionType::param_iterator E = Ty->param_end();
3274 std::vector<ValueInfo>::iterator ArgI = $6->begin(), ArgE = $6->end();
3275
3276 std::vector<Value*> Args;
3277 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
3278 if ((*ArgI).V->getType() != *I)
3279 error("Parameter " +(*ArgI).V->getName()+ " is not of type '" +
3280 (*I)->getDescription() + "'");
3281 Args.push_back((*ArgI).V);
3282 }
3283
3284 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
3285 error("Invalid number of parameters detected");
3286
David Greenef1355a52007-08-27 19:04:21 +00003287 $$.TI = new InvokeInst(V, Normal, Except, Args.begin(), Args.end());
Reid Spencerefd53d52007-01-26 08:18:34 +00003288 }
Reid Spencer3e5affd2007-03-21 17:14:36 +00003289 cast<InvokeInst>($$.TI)->setCallingConv(upgradeCallingConv($2));
Duncan Sandsdc024672007-11-27 13:23:08 +00003290 if ($2 == OldCallingConv::CSRet) {
Chris Lattner58d74912008-03-12 17:45:29 +00003291 ParamAttrsWithIndex PAWI =
3292 ParamAttrsWithIndex::get(1, ParamAttr::StructRet); // first arg
3293 cast<InvokeInst>($$.TI)->setParamAttrs(PAListPtr::get(&PAWI, 1));
Duncan Sandsdc024672007-11-27 13:23:08 +00003294 }
Reid Spencere7c326b2007-02-08 08:09:36 +00003295 delete $3.PAT;
Reid Spencerefd53d52007-01-26 08:18:34 +00003296 delete $6;
Reid Spencer4f859aa2007-04-22 05:46:44 +00003297 lastCallingConv = OldCallingConv::C;
Reid Spencerefd53d52007-01-26 08:18:34 +00003298 }
3299 | Unwind {
Reid Spencer3e5affd2007-03-21 17:14:36 +00003300 $$.TI = new UnwindInst();
3301 $$.S.makeSignless();
Reid Spencerefd53d52007-01-26 08:18:34 +00003302 }
3303 | UNREACHABLE {
Reid Spencer3e5affd2007-03-21 17:14:36 +00003304 $$.TI = new UnreachableInst();
3305 $$.S.makeSignless();
Reid Spencerefd53d52007-01-26 08:18:34 +00003306 }
3307 ;
3308
3309JumpTable
3310 : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
3311 $$ = $1;
Reid Spencer3e5affd2007-03-21 17:14:36 +00003312 $3.S.copy($2.S);
Reid Spencerefd53d52007-01-26 08:18:34 +00003313 Constant *V = cast<Constant>(getExistingValue($2.T, $3));
3314
3315 if (V == 0)
3316 error("May only switch on a constant pool value");
3317
Reid Spencer3e5affd2007-03-21 17:14:36 +00003318 $6.S.makeSignless();
Reid Spencerefd53d52007-01-26 08:18:34 +00003319 BasicBlock* tmpBB = getBBVal($6);
3320 $$->push_back(std::make_pair(V, tmpBB));
3321 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003322 | IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencerefd53d52007-01-26 08:18:34 +00003323 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencer3e5affd2007-03-21 17:14:36 +00003324 $2.S.copy($1.S);
Reid Spencerefd53d52007-01-26 08:18:34 +00003325 Constant *V = cast<Constant>(getExistingValue($1.T, $2));
3326
3327 if (V == 0)
3328 error("May only switch on a constant pool value");
3329
Reid Spencer3e5affd2007-03-21 17:14:36 +00003330 $5.S.makeSignless();
Reid Spencerefd53d52007-01-26 08:18:34 +00003331 BasicBlock* tmpBB = getBBVal($5);
3332 $$->push_back(std::make_pair(V, tmpBB));
3333 }
3334 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003335
3336Inst
3337 : OptAssign InstVal {
Reid Spencerefd53d52007-01-26 08:18:34 +00003338 bool omit = false;
3339 if ($1)
3340 if (BitCastInst *BCI = dyn_cast<BitCastInst>($2.I))
3341 if (BCI->getSrcTy() == BCI->getDestTy() &&
3342 BCI->getOperand(0)->getName() == $1)
3343 // This is a useless bit cast causing a name redefinition. It is
3344 // a bit cast from a type to the same type of an operand with the
3345 // same name as the name we would give this instruction. Since this
3346 // instruction results in no code generation, it is safe to omit
3347 // the instruction. This situation can occur because of collapsed
3348 // type planes. For example:
3349 // %X = add int %Y, %Z
3350 // %X = cast int %Y to uint
3351 // After upgrade, this looks like:
3352 // %X = add i32 %Y, %Z
3353 // %X = bitcast i32 to i32
3354 // The bitcast is clearly useless so we omit it.
3355 omit = true;
3356 if (omit) {
3357 $$.I = 0;
Reid Spencer3e5affd2007-03-21 17:14:36 +00003358 $$.S.makeSignless();
Reid Spencerefd53d52007-01-26 08:18:34 +00003359 } else {
Reid Spencer3e5affd2007-03-21 17:14:36 +00003360 ValueInfo VI; VI.V = $2.I; VI.S.copy($2.S);
3361 setValueName(VI, $1);
Reid Spencerefd53d52007-01-26 08:18:34 +00003362 InsertValue($2.I);
3363 $$ = $2;
Reid Spencer16222c02007-01-01 01:20:16 +00003364 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003365 };
3366
Reid Spencerefd53d52007-01-26 08:18:34 +00003367PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
3368 $$.P = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencer3e5affd2007-03-21 17:14:36 +00003369 $$.S.copy($1.S);
3370 $3.S.copy($1.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00003371 Value* tmpVal = getVal($1.PAT->get(), $3);
Reid Spencer3e5affd2007-03-21 17:14:36 +00003372 $5.S.makeSignless();
Reid Spencerefd53d52007-01-26 08:18:34 +00003373 BasicBlock* tmpBB = getBBVal($5);
3374 $$.P->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencere7c326b2007-02-08 08:09:36 +00003375 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003376 }
3377 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
Reid Spencere7c3c602006-11-30 06:36:44 +00003378 $$ = $1;
Reid Spencer3e5affd2007-03-21 17:14:36 +00003379 $4.S.copy($1.S);
Reid Spencerefd53d52007-01-26 08:18:34 +00003380 Value* tmpVal = getVal($1.P->front().first->getType(), $4);
Reid Spencer3e5affd2007-03-21 17:14:36 +00003381 $6.S.makeSignless();
Reid Spencerefd53d52007-01-26 08:18:34 +00003382 BasicBlock* tmpBB = getBBVal($6);
3383 $1.P->push_back(std::make_pair(tmpVal, tmpBB));
3384 }
3385 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003386
Reid Spencerefd53d52007-01-26 08:18:34 +00003387ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
3388 $$ = new std::vector<ValueInfo>();
Reid Spencerf8483652006-12-02 15:16:01 +00003389 $$->push_back($1);
3390 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003391 | ValueRefList ',' ResolvedVal {
Reid Spencere7c3c602006-11-30 06:36:44 +00003392 $$ = $1;
Reid Spencerefd53d52007-01-26 08:18:34 +00003393 $1->push_back($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00003394 };
3395
3396// ValueRefListE - Just like ValueRefList, except that it may also be empty!
3397ValueRefListE
Reid Spencerefd53d52007-01-26 08:18:34 +00003398 : ValueRefList
3399 | /*empty*/ { $$ = 0; }
Reid Spencere7c3c602006-11-30 06:36:44 +00003400 ;
3401
3402OptTailCall
3403 : TAIL CALL {
Reid Spencerefd53d52007-01-26 08:18:34 +00003404 $$ = true;
Reid Spencere7c3c602006-11-30 06:36:44 +00003405 }
Reid Spencerefd53d52007-01-26 08:18:34 +00003406 | CALL {
3407 $$ = false;
3408 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003409 ;
3410
Reid Spencerefd53d52007-01-26 08:18:34 +00003411InstVal
3412 : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer3e5affd2007-03-21 17:14:36 +00003413 $3.S.copy($2.S);
3414 $5.S.copy($2.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00003415 const Type* Ty = $2.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003416 if (!Ty->isInteger() && !Ty->isFloatingPoint() && !isa<VectorType>(Ty))
Reid Spencerefd53d52007-01-26 08:18:34 +00003417 error("Arithmetic operator requires integer, FP, or packed operands");
Reid Spencer9d6565a2007-02-15 02:26:10 +00003418 if (isa<VectorType>(Ty) &&
Reid Spencerefd53d52007-01-26 08:18:34 +00003419 ($1 == URemOp || $1 == SRemOp || $1 == FRemOp || $1 == RemOp))
Reid Spencerac9dcb92007-02-15 03:39:18 +00003420 error("Remainder not supported on vector types");
Reid Spencerefd53d52007-01-26 08:18:34 +00003421 // Upgrade the opcode from obsolete versions before we do anything with it.
3422 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $2.S);
3423 Value* val1 = getVal(Ty, $3);
3424 Value* val2 = getVal(Ty, $5);
3425 $$.I = BinaryOperator::create(Opcode, val1, val2);
3426 if ($$.I == 0)
3427 error("binary operator returned null");
Reid Spencer3e5affd2007-03-21 17:14:36 +00003428 $$.S.copy($2.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00003429 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003430 }
3431 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer3e5affd2007-03-21 17:14:36 +00003432 $3.S.copy($2.S);
3433 $5.S.copy($2.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00003434 const Type *Ty = $2.PAT->get();
Reid Spencerefd53d52007-01-26 08:18:34 +00003435 if (!Ty->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00003436 if (!isa<VectorType>(Ty) ||
3437 !cast<VectorType>(Ty)->getElementType()->isInteger())
Reid Spencerefd53d52007-01-26 08:18:34 +00003438 error("Logical operator requires integral operands");
3439 }
3440 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $2.S);
3441 Value* tmpVal1 = getVal(Ty, $3);
3442 Value* tmpVal2 = getVal(Ty, $5);
3443 $$.I = BinaryOperator::create(Opcode, tmpVal1, tmpVal2);
3444 if ($$.I == 0)
3445 error("binary operator returned null");
Reid Spencer3e5affd2007-03-21 17:14:36 +00003446 $$.S.copy($2.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00003447 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003448 }
3449 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer3e5affd2007-03-21 17:14:36 +00003450 $3.S.copy($2.S);
3451 $5.S.copy($2.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00003452 const Type* Ty = $2.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003453 if(isa<VectorType>(Ty))
3454 error("VectorTypes currently not supported in setcc instructions");
Reid Spencerefd53d52007-01-26 08:18:34 +00003455 unsigned short pred;
3456 Instruction::OtherOps Opcode = getCompareOp($1, pred, Ty, $2.S);
3457 Value* tmpVal1 = getVal(Ty, $3);
3458 Value* tmpVal2 = getVal(Ty, $5);
3459 $$.I = CmpInst::create(Opcode, pred, tmpVal1, tmpVal2);
3460 if ($$.I == 0)
3461 error("binary operator returned null");
Reid Spencer3e5affd2007-03-21 17:14:36 +00003462 $$.S.makeUnsigned();
Reid Spencere7c326b2007-02-08 08:09:36 +00003463 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003464 }
Reid Spencera9d7e892006-12-29 20:33:37 +00003465 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer3e5affd2007-03-21 17:14:36 +00003466 $4.S.copy($3.S);
3467 $6.S.copy($3.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00003468 const Type *Ty = $3.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003469 if (isa<VectorType>(Ty))
3470 error("VectorTypes currently not supported in icmp instructions");
Reid Spencerefd53d52007-01-26 08:18:34 +00003471 else if (!Ty->isInteger() && !isa<PointerType>(Ty))
3472 error("icmp requires integer or pointer typed operands");
3473 Value* tmpVal1 = getVal(Ty, $4);
3474 Value* tmpVal2 = getVal(Ty, $6);
3475 $$.I = new ICmpInst($2, tmpVal1, tmpVal2);
Reid Spencer3e5affd2007-03-21 17:14:36 +00003476 $$.S.makeUnsigned();
Reid Spencere7c326b2007-02-08 08:09:36 +00003477 delete $3.PAT;
Reid Spencer57f28f92006-12-03 07:10:26 +00003478 }
Reid Spencera9d7e892006-12-29 20:33:37 +00003479 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer3e5affd2007-03-21 17:14:36 +00003480 $4.S.copy($3.S);
3481 $6.S.copy($3.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00003482 const Type *Ty = $3.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003483 if (isa<VectorType>(Ty))
3484 error("VectorTypes currently not supported in fcmp instructions");
Reid Spencerefd53d52007-01-26 08:18:34 +00003485 else if (!Ty->isFloatingPoint())
3486 error("fcmp instruction requires floating point operands");
3487 Value* tmpVal1 = getVal(Ty, $4);
3488 Value* tmpVal2 = getVal(Ty, $6);
3489 $$.I = new FCmpInst($2, tmpVal1, tmpVal2);
Reid Spencer3e5affd2007-03-21 17:14:36 +00003490 $$.S.makeUnsigned();
Reid Spencere7c326b2007-02-08 08:09:36 +00003491 delete $3.PAT;
Reid Spencerefd53d52007-01-26 08:18:34 +00003492 }
3493 | NOT ResolvedVal {
3494 warning("Use of obsolete 'not' instruction: Replacing with 'xor");
3495 const Type *Ty = $2.V->getType();
3496 Value *Ones = ConstantInt::getAllOnesValue(Ty);
3497 if (Ones == 0)
3498 error("Expected integral type for not instruction");
3499 $$.I = BinaryOperator::create(Instruction::Xor, $2.V, Ones);
3500 if ($$.I == 0)
3501 error("Could not create a xor instruction");
Reid Spencer3e5affd2007-03-21 17:14:36 +00003502 $$.S.copy($2.S);
Reid Spencer2b404382006-12-02 22:09:27 +00003503 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003504 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencerefd53d52007-01-26 08:18:34 +00003505 if (!$4.V->getType()->isInteger() ||
3506 cast<IntegerType>($4.V->getType())->getBitWidth() != 8)
3507 error("Shift amount must be int8");
Reid Spencer832254e2007-02-02 02:16:23 +00003508 const Type* Ty = $2.V->getType();
3509 if (!Ty->isInteger())
Reid Spencerefd53d52007-01-26 08:18:34 +00003510 error("Shift constant expression requires integer operand");
Reid Spencer832254e2007-02-02 02:16:23 +00003511 Value* ShiftAmt = 0;
3512 if (cast<IntegerType>(Ty)->getBitWidth() > Type::Int8Ty->getBitWidth())
3513 if (Constant *C = dyn_cast<Constant>($4.V))
3514 ShiftAmt = ConstantExpr::getZExt(C, Ty);
3515 else
3516 ShiftAmt = new ZExtInst($4.V, Ty, makeNameUnique("shift"), CurBB);
3517 else
3518 ShiftAmt = $4.V;
3519 $$.I = BinaryOperator::create(getBinaryOp($1, Ty, $2.S), $2.V, ShiftAmt);
Reid Spencer3e5affd2007-03-21 17:14:36 +00003520 $$.S.copy($2.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00003521 }
Reid Spencerfcb5df82006-12-01 22:34:43 +00003522 | CastOps ResolvedVal TO Types {
Reid Spencere7c326b2007-02-08 08:09:36 +00003523 const Type *DstTy = $4.PAT->get();
Reid Spencerefd53d52007-01-26 08:18:34 +00003524 if (!DstTy->isFirstClassType())
3525 error("cast instruction to a non-primitive type: '" +
3526 DstTy->getDescription() + "'");
3527 $$.I = cast<Instruction>(getCast($1, $2.V, $2.S, DstTy, $4.S, true));
Reid Spencer3e5affd2007-03-21 17:14:36 +00003528 $$.S.copy($4.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00003529 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003530 }
3531 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencerefd53d52007-01-26 08:18:34 +00003532 if (!$2.V->getType()->isInteger() ||
3533 cast<IntegerType>($2.V->getType())->getBitWidth() != 1)
3534 error("select condition must be bool");
3535 if ($4.V->getType() != $6.V->getType())
3536 error("select value types should match");
3537 $$.I = new SelectInst($2.V, $4.V, $6.V);
Reid Spencer3e5affd2007-03-21 17:14:36 +00003538 $$.S.copy($4.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00003539 }
3540 | VAARG ResolvedVal ',' Types {
Reid Spencere7c326b2007-02-08 08:09:36 +00003541 const Type *Ty = $4.PAT->get();
Reid Spencerefd53d52007-01-26 08:18:34 +00003542 NewVarArgs = true;
3543 $$.I = new VAArgInst($2.V, Ty);
Reid Spencer3e5affd2007-03-21 17:14:36 +00003544 $$.S.copy($4.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00003545 delete $4.PAT;
Reid Spencerefd53d52007-01-26 08:18:34 +00003546 }
3547 | VAARG_old ResolvedVal ',' Types {
3548 const Type* ArgTy = $2.V->getType();
Reid Spencere7c326b2007-02-08 08:09:36 +00003549 const Type* DstTy = $4.PAT->get();
Reid Spencerefd53d52007-01-26 08:18:34 +00003550 ObsoleteVarArgs = true;
Duncan Sandse2c43042008-04-07 13:45:04 +00003551 Function* NF = Intrinsic::getDeclaration(CurModule.CurrentModule,
3552 Intrinsic::va_copy);
Reid Spencerefd53d52007-01-26 08:18:34 +00003553
3554 //b = vaarg a, t ->
3555 //foo = alloca 1 of t
3556 //bar = vacopy a
3557 //store bar -> foo
3558 //b = vaarg foo, t
3559 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix");
3560 CurBB->getInstList().push_back(foo);
3561 CallInst* bar = new CallInst(NF, $2.V);
3562 CurBB->getInstList().push_back(bar);
3563 CurBB->getInstList().push_back(new StoreInst(bar, foo));
3564 $$.I = new VAArgInst(foo, DstTy);
Reid Spencer3e5affd2007-03-21 17:14:36 +00003565 $$.S.copy($4.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00003566 delete $4.PAT;
Reid Spencerefd53d52007-01-26 08:18:34 +00003567 }
3568 | VANEXT_old ResolvedVal ',' Types {
3569 const Type* ArgTy = $2.V->getType();
Reid Spencere7c326b2007-02-08 08:09:36 +00003570 const Type* DstTy = $4.PAT->get();
Reid Spencerefd53d52007-01-26 08:18:34 +00003571 ObsoleteVarArgs = true;
Duncan Sandse2c43042008-04-07 13:45:04 +00003572 Function* NF = Intrinsic::getDeclaration(CurModule.CurrentModule,
3573 Intrinsic::va_copy);
Reid Spencerefd53d52007-01-26 08:18:34 +00003574
3575 //b = vanext a, t ->
3576 //foo = alloca 1 of t
3577 //bar = vacopy a
3578 //store bar -> foo
3579 //tmp = vaarg foo, t
3580 //b = load foo
3581 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix");
3582 CurBB->getInstList().push_back(foo);
3583 CallInst* bar = new CallInst(NF, $2.V);
3584 CurBB->getInstList().push_back(bar);
3585 CurBB->getInstList().push_back(new StoreInst(bar, foo));
3586 Instruction* tmp = new VAArgInst(foo, DstTy);
3587 CurBB->getInstList().push_back(tmp);
3588 $$.I = new LoadInst(foo);
Reid Spencer3e5affd2007-03-21 17:14:36 +00003589 $$.S.copy($4.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00003590 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003591 }
3592 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencerefd53d52007-01-26 08:18:34 +00003593 if (!ExtractElementInst::isValidOperands($2.V, $4.V))
3594 error("Invalid extractelement operands");
3595 $$.I = new ExtractElementInst($2.V, $4.V);
Reid Spencer3e5affd2007-03-21 17:14:36 +00003596 $$.S.copy($2.S.get(0));
Reid Spencere7c3c602006-11-30 06:36:44 +00003597 }
3598 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencerefd53d52007-01-26 08:18:34 +00003599 if (!InsertElementInst::isValidOperands($2.V, $4.V, $6.V))
3600 error("Invalid insertelement operands");
3601 $$.I = new InsertElementInst($2.V, $4.V, $6.V);
Reid Spencer3e5affd2007-03-21 17:14:36 +00003602 $$.S.copy($2.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00003603 }
3604 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencerefd53d52007-01-26 08:18:34 +00003605 if (!ShuffleVectorInst::isValidOperands($2.V, $4.V, $6.V))
3606 error("Invalid shufflevector operands");
3607 $$.I = new ShuffleVectorInst($2.V, $4.V, $6.V);
Reid Spencer3e5affd2007-03-21 17:14:36 +00003608 $$.S.copy($2.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00003609 }
3610 | PHI_TOK PHIList {
Reid Spencerefd53d52007-01-26 08:18:34 +00003611 const Type *Ty = $2.P->front().first->getType();
3612 if (!Ty->isFirstClassType())
3613 error("PHI node operands must be of first class type");
3614 PHINode *PHI = new PHINode(Ty);
3615 PHI->reserveOperandSpace($2.P->size());
3616 while ($2.P->begin() != $2.P->end()) {
3617 if ($2.P->front().first->getType() != Ty)
3618 error("All elements of a PHI node must be of the same type");
3619 PHI->addIncoming($2.P->front().first, $2.P->front().second);
3620 $2.P->pop_front();
3621 }
3622 $$.I = PHI;
Reid Spencer3e5affd2007-03-21 17:14:36 +00003623 $$.S.copy($2.S);
Reid Spencerefd53d52007-01-26 08:18:34 +00003624 delete $2.P; // Free the list...
Reid Spencere7c3c602006-11-30 06:36:44 +00003625 }
Reid Spencer460dd9b2007-04-09 06:15:59 +00003626 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Reid Spencerefd53d52007-01-26 08:18:34 +00003627 // Handle the short call syntax
3628 const PointerType *PFTy;
3629 const FunctionType *FTy;
Reid Spencer3e5affd2007-03-21 17:14:36 +00003630 Signedness FTySign;
Reid Spencere7c326b2007-02-08 08:09:36 +00003631 if (!(PFTy = dyn_cast<PointerType>($3.PAT->get())) ||
Reid Spencerefd53d52007-01-26 08:18:34 +00003632 !(FTy = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3633 // Pull out the types of all of the arguments...
3634 std::vector<const Type*> ParamTypes;
Reid Spencer3e5affd2007-03-21 17:14:36 +00003635 FTySign.makeComposite($3.S);
Reid Spencerefd53d52007-01-26 08:18:34 +00003636 if ($6) {
3637 for (std::vector<ValueInfo>::iterator I = $6->begin(), E = $6->end();
Reid Spencer3e5affd2007-03-21 17:14:36 +00003638 I != E; ++I) {
Reid Spencerefd53d52007-01-26 08:18:34 +00003639 ParamTypes.push_back((*I).V->getType());
Reid Spencer3e5affd2007-03-21 17:14:36 +00003640 FTySign.add(I->S);
3641 }
Reid Spencerfbb7b692007-01-13 00:02:00 +00003642 }
Reid Spencerefd53d52007-01-26 08:18:34 +00003643
3644 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
3645 if (isVarArg) ParamTypes.pop_back();
3646
Reid Spencere7c326b2007-02-08 08:09:36 +00003647 const Type *RetTy = $3.PAT->get();
Reid Spencerefd53d52007-01-26 08:18:34 +00003648 if (!RetTy->isFirstClassType() && RetTy != Type::VoidTy)
3649 error("Functions cannot return aggregate types");
3650
Duncan Sandsdc024672007-11-27 13:23:08 +00003651 FTy = FunctionType::get(RetTy, ParamTypes, isVarArg);
Christopher Lamb43ad6b32007-12-17 01:12:55 +00003652 PFTy = PointerType::getUnqual(FTy);
Reid Spencer3e5affd2007-03-21 17:14:36 +00003653 $$.S.copy($3.S);
3654 } else {
3655 FTySign = $3.S;
Reid Spencerb289f192007-04-07 16:10:37 +00003656 // Get the signedness of the result type. $3 is the pointer to the
3657 // function type so we get the 0th element to extract the function type,
3658 // and then the 0th element again to get the result type.
3659 $$.S.copy($3.S.get(0).get(0));
Reid Spencerf8483652006-12-02 15:16:01 +00003660 }
Reid Spencer3e5affd2007-03-21 17:14:36 +00003661 $4.S.makeComposite(FTySign);
Reid Spencerefd53d52007-01-26 08:18:34 +00003662
3663 // First upgrade any intrinsic calls.
3664 std::vector<Value*> Args;
3665 if ($6)
3666 for (unsigned i = 0, e = $6->size(); i < e; ++i)
3667 Args.push_back((*$6)[i].V);
Reid Spencer1e70bb62007-04-02 00:50:28 +00003668 Instruction *Inst = upgradeIntrinsicCall(FTy->getReturnType(), $4, Args);
Reid Spencerefd53d52007-01-26 08:18:34 +00003669
3670 // If we got an upgraded intrinsic
3671 if (Inst) {
3672 $$.I = Inst;
Reid Spencerefd53d52007-01-26 08:18:34 +00003673 } else {
3674 // Get the function we're calling
3675 Value *V = getVal(PFTy, $4);
3676
3677 // Check the argument values match
3678 if (!$6) { // Has no arguments?
3679 // Make sure no arguments is a good thing!
3680 if (FTy->getNumParams() != 0)
3681 error("No arguments passed to a function that expects arguments");
3682 } else { // Has arguments?
3683 // Loop through FunctionType's arguments and ensure they are specified
3684 // correctly!
3685 //
3686 FunctionType::param_iterator I = FTy->param_begin();
3687 FunctionType::param_iterator E = FTy->param_end();
3688 std::vector<ValueInfo>::iterator ArgI = $6->begin(), ArgE = $6->end();
3689
3690 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
3691 if ((*ArgI).V->getType() != *I)
3692 error("Parameter " +(*ArgI).V->getName()+ " is not of type '" +
3693 (*I)->getDescription() + "'");
3694
3695 if (I != E || (ArgI != ArgE && !FTy->isVarArg()))
3696 error("Invalid number of parameters detected");
3697 }
3698
3699 // Create the call instruction
David Greene52eec542007-08-01 03:43:44 +00003700 CallInst *CI = new CallInst(V, Args.begin(), Args.end());
Reid Spencerefd53d52007-01-26 08:18:34 +00003701 CI->setTailCall($1);
Reid Spencer43f76c92007-01-29 05:41:09 +00003702 CI->setCallingConv(upgradeCallingConv($2));
Duncan Sandsdc024672007-11-27 13:23:08 +00003703
Reid Spencerefd53d52007-01-26 08:18:34 +00003704 $$.I = CI;
Reid Spencerefd53d52007-01-26 08:18:34 +00003705 }
Duncan Sandsdc024672007-11-27 13:23:08 +00003706 // Deal with CSRetCC
3707 if ($2 == OldCallingConv::CSRet) {
Chris Lattner58d74912008-03-12 17:45:29 +00003708 ParamAttrsWithIndex PAWI =
3709 ParamAttrsWithIndex::get(1, ParamAttr::StructRet); // first arg
3710 cast<CallInst>($$.I)->setParamAttrs(PAListPtr::get(&PAWI, 1));
Duncan Sandsdc024672007-11-27 13:23:08 +00003711 }
Reid Spencere7c326b2007-02-08 08:09:36 +00003712 delete $3.PAT;
Reid Spencerefd53d52007-01-26 08:18:34 +00003713 delete $6;
Reid Spencer4f859aa2007-04-22 05:46:44 +00003714 lastCallingConv = OldCallingConv::C;
Reid Spencere7c3c602006-11-30 06:36:44 +00003715 }
Reid Spencerefd53d52007-01-26 08:18:34 +00003716 | MemoryInst {
3717 $$ = $1;
3718 }
3719 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003720
3721
3722// IndexList - List of indices for GEP based instructions...
3723IndexList
Reid Spencerefd53d52007-01-26 08:18:34 +00003724 : ',' ValueRefList { $$ = $2; }
3725 | /* empty */ { $$ = new std::vector<ValueInfo>(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00003726 ;
3727
3728OptVolatile
Reid Spencerefd53d52007-01-26 08:18:34 +00003729 : VOLATILE { $$ = true; }
3730 | /* empty */ { $$ = false; }
Reid Spencere7c3c602006-11-30 06:36:44 +00003731 ;
3732
Reid Spencerefd53d52007-01-26 08:18:34 +00003733MemoryInst
3734 : MALLOC Types OptCAlign {
Reid Spencere7c326b2007-02-08 08:09:36 +00003735 const Type *Ty = $2.PAT->get();
Reid Spencer3e5affd2007-03-21 17:14:36 +00003736 $$.S.makeComposite($2.S);
Reid Spencerefd53d52007-01-26 08:18:34 +00003737 $$.I = new MallocInst(Ty, 0, $3);
Reid Spencere7c326b2007-02-08 08:09:36 +00003738 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003739 }
3740 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencere7c326b2007-02-08 08:09:36 +00003741 const Type *Ty = $2.PAT->get();
Reid Spencer3e5affd2007-03-21 17:14:36 +00003742 $5.S.makeUnsigned();
3743 $$.S.makeComposite($2.S);
Reid Spencerefd53d52007-01-26 08:18:34 +00003744 $$.I = new MallocInst(Ty, getVal($4.T, $5), $6);
Reid Spencere7c326b2007-02-08 08:09:36 +00003745 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003746 }
3747 | ALLOCA Types OptCAlign {
Reid Spencere7c326b2007-02-08 08:09:36 +00003748 const Type *Ty = $2.PAT->get();
Reid Spencer3e5affd2007-03-21 17:14:36 +00003749 $$.S.makeComposite($2.S);
Reid Spencerefd53d52007-01-26 08:18:34 +00003750 $$.I = new AllocaInst(Ty, 0, $3);
Reid Spencere7c326b2007-02-08 08:09:36 +00003751 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003752 }
3753 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencere7c326b2007-02-08 08:09:36 +00003754 const Type *Ty = $2.PAT->get();
Reid Spencer3e5affd2007-03-21 17:14:36 +00003755 $5.S.makeUnsigned();
3756 $$.S.makeComposite($4.S);
Reid Spencerefd53d52007-01-26 08:18:34 +00003757 $$.I = new AllocaInst(Ty, getVal($4.T, $5), $6);
Reid Spencere7c326b2007-02-08 08:09:36 +00003758 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003759 }
3760 | FREE ResolvedVal {
Reid Spencerefd53d52007-01-26 08:18:34 +00003761 const Type *PTy = $2.V->getType();
3762 if (!isa<PointerType>(PTy))
3763 error("Trying to free nonpointer type '" + PTy->getDescription() + "'");
3764 $$.I = new FreeInst($2.V);
Reid Spencer3e5affd2007-03-21 17:14:36 +00003765 $$.S.makeSignless();
Reid Spencere7c3c602006-11-30 06:36:44 +00003766 }
3767 | OptVolatile LOAD Types ValueRef {
Reid Spencere7c326b2007-02-08 08:09:36 +00003768 const Type* Ty = $3.PAT->get();
Reid Spencer3e5affd2007-03-21 17:14:36 +00003769 $4.S.copy($3.S);
Reid Spencerefd53d52007-01-26 08:18:34 +00003770 if (!isa<PointerType>(Ty))
3771 error("Can't load from nonpointer type: " + Ty->getDescription());
3772 if (!cast<PointerType>(Ty)->getElementType()->isFirstClassType())
3773 error("Can't load from pointer of non-first-class type: " +
3774 Ty->getDescription());
3775 Value* tmpVal = getVal(Ty, $4);
3776 $$.I = new LoadInst(tmpVal, "", $1);
Reid Spencer3e5affd2007-03-21 17:14:36 +00003777 $$.S.copy($3.S.get(0));
Reid Spencere7c326b2007-02-08 08:09:36 +00003778 delete $3.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003779 }
3780 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
Reid Spencer3e5affd2007-03-21 17:14:36 +00003781 $6.S.copy($5.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00003782 const PointerType *PTy = dyn_cast<PointerType>($5.PAT->get());
Reid Spencerefd53d52007-01-26 08:18:34 +00003783 if (!PTy)
3784 error("Can't store to a nonpointer type: " +
Reid Spencere7c326b2007-02-08 08:09:36 +00003785 $5.PAT->get()->getDescription());
Reid Spencerefd53d52007-01-26 08:18:34 +00003786 const Type *ElTy = PTy->getElementType();
Reid Spencere7c326b2007-02-08 08:09:36 +00003787 Value *StoreVal = $3.V;
Reid Spencerefd53d52007-01-26 08:18:34 +00003788 Value* tmpVal = getVal(PTy, $6);
Reid Spencere7c326b2007-02-08 08:09:36 +00003789 if (ElTy != $3.V->getType()) {
Christopher Lamb43ad6b32007-12-17 01:12:55 +00003790 PTy = PointerType::getUnqual(StoreVal->getType());
Duncan Sandsdc024672007-11-27 13:23:08 +00003791 if (Constant *C = dyn_cast<Constant>(tmpVal))
3792 tmpVal = ConstantExpr::getBitCast(C, PTy);
3793 else
3794 tmpVal = new BitCastInst(tmpVal, PTy, "upgrd.cast", CurBB);
Reid Spencere7c326b2007-02-08 08:09:36 +00003795 }
3796 $$.I = new StoreInst(StoreVal, tmpVal, $1);
Reid Spencer3e5affd2007-03-21 17:14:36 +00003797 $$.S.makeSignless();
Reid Spencere7c326b2007-02-08 08:09:36 +00003798 delete $5.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003799 }
3800 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer3e5affd2007-03-21 17:14:36 +00003801 $3.S.copy($2.S);
Reid Spencere7c326b2007-02-08 08:09:36 +00003802 const Type* Ty = $2.PAT->get();
Reid Spencerefd53d52007-01-26 08:18:34 +00003803 if (!isa<PointerType>(Ty))
3804 error("getelementptr insn requires pointer operand");
3805
3806 std::vector<Value*> VIndices;
Reid Spencerd23c4dd2007-04-16 00:39:39 +00003807 upgradeGEPInstIndices(Ty, $4, VIndices);
Reid Spencerefd53d52007-01-26 08:18:34 +00003808
3809 Value* tmpVal = getVal(Ty, $3);
David Greeneb8f74792007-09-04 15:46:09 +00003810 $$.I = new GetElementPtrInst(tmpVal, VIndices.begin(), VIndices.end());
Reid Spencer3e5affd2007-03-21 17:14:36 +00003811 ValueInfo VI; VI.V = tmpVal; VI.S.copy($2.S);
3812 $$.S.copy(getElementSign(VI, VIndices));
Reid Spencere7c326b2007-02-08 08:09:36 +00003813 delete $2.PAT;
Reid Spencere0a15bb2007-01-15 00:25:53 +00003814 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00003815 };
3816
Reid Spencerefd53d52007-01-26 08:18:34 +00003817
Reid Spencere7c3c602006-11-30 06:36:44 +00003818%%
3819
3820int yyerror(const char *ErrorMsg) {
3821 std::string where
3822 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Reid Spencere7c326b2007-02-08 08:09:36 +00003823 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
Reid Spencerefd53d52007-01-26 08:18:34 +00003824 std::string errMsg = where + "error: " + std::string(ErrorMsg);
3825 if (yychar != YYEMPTY && yychar != 0)
3826 errMsg += " while reading token '" + std::string(Upgradetext, Upgradeleng) +
3827 "'.";
Reid Spencer05e52a12006-12-31 05:45:57 +00003828 std::cerr << "llvm-upgrade: " << errMsg << '\n';
Reid Spencerefd53d52007-01-26 08:18:34 +00003829 std::cout << "llvm-upgrade: parse failed.\n";
Reid Spencere7c3c602006-11-30 06:36:44 +00003830 exit(1);
3831}
Reid Spencer90eb4d62007-01-05 17:18:58 +00003832
Reid Spencere0a15bb2007-01-15 00:25:53 +00003833void warning(const std::string& ErrorMsg) {
Reid Spencer90eb4d62007-01-05 17:18:58 +00003834 std::string where
3835 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Reid Spencere7c326b2007-02-08 08:09:36 +00003836 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
Reid Spencerefd53d52007-01-26 08:18:34 +00003837 std::string errMsg = where + "warning: " + std::string(ErrorMsg);
3838 if (yychar != YYEMPTY && yychar != 0)
3839 errMsg += " while reading token '" + std::string(Upgradetext, Upgradeleng) +
3840 "'.";
Reid Spencer90eb4d62007-01-05 17:18:58 +00003841 std::cerr << "llvm-upgrade: " << errMsg << '\n';
3842}
Reid Spencerefd53d52007-01-26 08:18:34 +00003843
3844void error(const std::string& ErrorMsg, int LineNo) {
3845 if (LineNo == -1) LineNo = Upgradelineno;
3846 Upgradelineno = LineNo;
3847 yyerror(ErrorMsg.c_str());
3848}
3849