blob: 9cc29fb61b4971bf284d1301ca41c09bf4d765ad [file] [log] [blame]
Reid Spencer950bf602007-01-26 08:19:09 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
Reid Spencere7c3c602006-11-30 06:36:44 +00002//
3// The LLVM Compiler Infrastructure
4//
Reid Spencer950bf602007-01-26 08:19:09 +00005// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Reid Spencere7c3c602006-11-30 06:36:44 +00007//
8//===----------------------------------------------------------------------===//
9//
Reid Spencer950bf602007-01-26 08:19:09 +000010// This file implements the bison parser for LLVM assembly languages files.
Reid Spencere7c3c602006-11-30 06:36:44 +000011//
12//===----------------------------------------------------------------------===//
13
14%{
Reid Spencer319a7302007-01-05 17:20:02 +000015#include "UpgradeInternals.h"
Reid Spencer950bf602007-01-26 08:19:09 +000016#include "llvm/CallingConv.h"
17#include "llvm/InlineAsm.h"
18#include "llvm/Instructions.h"
19#include "llvm/Module.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000020#include "llvm/ValueSymbolTable.h"
Reid Spencer950bf602007-01-26 08:19:09 +000021#include "llvm/Support/GetElementPtrTypeIterator.h"
22#include "llvm/ADT/STLExtras.h"
23#include "llvm/Support/MathExtras.h"
Reid Spencere7c3c602006-11-30 06:36:44 +000024#include <algorithm>
Reid Spencere7c3c602006-11-30 06:36:44 +000025#include <iostream>
Chris Lattner8adde282007-02-11 21:40:10 +000026#include <map>
Reid Spencer950bf602007-01-26 08:19:09 +000027#include <list>
28#include <utility>
29
30// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
31// relating to upreferences in the input stream.
32//
33//#define DEBUG_UPREFS 1
34#ifdef DEBUG_UPREFS
35#define UR_OUT(X) std::cerr << X
36#else
37#define UR_OUT(X)
38#endif
Reid Spencere7c3c602006-11-30 06:36:44 +000039
Reid Spencere77e35e2006-12-01 20:26:20 +000040#define YYERROR_VERBOSE 1
Reid Spencer96839be2006-11-30 16:50:26 +000041#define YYINCLUDED_STDLIB_H
Reid Spencere77e35e2006-12-01 20:26:20 +000042#define YYDEBUG 1
Reid Spencere7c3c602006-11-30 06:36:44 +000043
Reid Spencer950bf602007-01-26 08:19:09 +000044int yylex();
Reid Spencere7c3c602006-11-30 06:36:44 +000045int yyparse();
46
Reid Spencer950bf602007-01-26 08:19:09 +000047int yyerror(const char*);
48static void warning(const std::string& WarningMsg);
49
50namespace llvm {
51
Reid Spencer950bf602007-01-26 08:19:09 +000052std::istream* LexInput;
Reid Spencere7c3c602006-11-30 06:36:44 +000053static std::string CurFilename;
Reid Spencer96839be2006-11-30 16:50:26 +000054
Reid Spencer71d2ec92006-12-31 06:02:26 +000055// This bool controls whether attributes are ever added to function declarations
56// definitions and calls.
57static bool AddAttributes = false;
58
Reid Spencer950bf602007-01-26 08:19:09 +000059static Module *ParserResult;
60static bool ObsoleteVarArgs;
61static bool NewVarArgs;
62static BasicBlock *CurBB;
63static GlobalVariable *CurGV;
Reid Spencera50d5962006-12-02 04:11:07 +000064
Reid Spencer950bf602007-01-26 08:19:09 +000065// This contains info used when building the body of a function. It is
66// destroyed when the function is completed.
67//
68typedef std::vector<Value *> ValueList; // Numbered defs
69
Reid Spencerbb1fd572007-03-21 17:15:50 +000070typedef std::pair<std::string,TypeInfo> RenameMapKey;
Reid Spencer950bf602007-01-26 08:19:09 +000071typedef std::map<RenameMapKey,std::string> RenameMapType;
72
73static void
74ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
75 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
76
77static struct PerModuleInfo {
78 Module *CurrentModule;
79 std::map<const Type *, ValueList> Values; // Module level numbered definitions
80 std::map<const Type *,ValueList> LateResolveValues;
Reid Spencerbb1fd572007-03-21 17:15:50 +000081 std::vector<PATypeHolder> Types;
82 std::vector<Signedness> TypeSigns;
83 std::map<std::string,Signedness> NamedTypeSigns;
84 std::map<std::string,Signedness> NamedValueSigns;
Reid Spencer950bf602007-01-26 08:19:09 +000085 std::map<ValID, PATypeHolder> LateResolveTypes;
86 static Module::Endianness Endian;
87 static Module::PointerSize PointerSize;
88 RenameMapType RenameMap;
89
90 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
91 /// how they were referenced and on which line of the input they came from so
92 /// that we can resolve them later and print error messages as appropriate.
93 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
94
95 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
96 // references to global values. Global values may be referenced before they
97 // are defined, and if so, the temporary object that they represent is held
98 // here. This is used for forward references of GlobalValues.
99 //
100 typedef std::map<std::pair<const PointerType *, ValID>, GlobalValue*>
101 GlobalRefsType;
102 GlobalRefsType GlobalRefs;
103
104 void ModuleDone() {
105 // If we could not resolve some functions at function compilation time
106 // (calls to functions before they are defined), resolve them now... Types
107 // are resolved when the constant pool has been completely parsed.
108 //
109 ResolveDefinitions(LateResolveValues);
110
111 // Check to make sure that all global value forward references have been
112 // resolved!
113 //
114 if (!GlobalRefs.empty()) {
115 std::string UndefinedReferences = "Unresolved global references exist:\n";
116
117 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
118 I != E; ++I) {
119 UndefinedReferences += " " + I->first.first->getDescription() + " " +
120 I->first.second.getName() + "\n";
121 }
122 error(UndefinedReferences);
123 return;
124 }
125
126 if (CurrentModule->getDataLayout().empty()) {
127 std::string dataLayout;
128 if (Endian != Module::AnyEndianness)
129 dataLayout.append(Endian == Module::BigEndian ? "E" : "e");
130 if (PointerSize != Module::AnyPointerSize) {
131 if (!dataLayout.empty())
132 dataLayout += "-";
133 dataLayout.append(PointerSize == Module::Pointer64 ?
134 "p:64:64" : "p:32:32");
135 }
136 CurrentModule->setDataLayout(dataLayout);
137 }
138
139 Values.clear(); // Clear out function local definitions
140 Types.clear();
Reid Spencerbb1fd572007-03-21 17:15:50 +0000141 TypeSigns.clear();
142 NamedTypeSigns.clear();
143 NamedValueSigns.clear();
Reid Spencer950bf602007-01-26 08:19:09 +0000144 CurrentModule = 0;
145 }
146
147 // GetForwardRefForGlobal - Check to see if there is a forward reference
148 // for this global. If so, remove it from the GlobalRefs map and return it.
149 // If not, just return null.
150 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
151 // Check to see if there is a forward reference to this global variable...
152 // if there is, eliminate it and patch the reference to use the new def'n.
153 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
154 GlobalValue *Ret = 0;
155 if (I != GlobalRefs.end()) {
156 Ret = I->second;
157 GlobalRefs.erase(I);
158 }
159 return Ret;
160 }
161 void setEndianness(Module::Endianness E) { Endian = E; }
162 void setPointerSize(Module::PointerSize sz) { PointerSize = sz; }
163} CurModule;
164
165Module::Endianness PerModuleInfo::Endian = Module::AnyEndianness;
166Module::PointerSize PerModuleInfo::PointerSize = Module::AnyPointerSize;
167
168static struct PerFunctionInfo {
169 Function *CurrentFunction; // Pointer to current function being created
170
171 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
172 std::map<const Type*, ValueList> LateResolveValues;
173 bool isDeclare; // Is this function a forward declararation?
174 GlobalValue::LinkageTypes Linkage;// Linkage for forward declaration.
175
176 /// BBForwardRefs - When we see forward references to basic blocks, keep
177 /// track of them here.
178 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
179 std::vector<BasicBlock*> NumberedBlocks;
180 RenameMapType RenameMap;
Reid Spencer950bf602007-01-26 08:19:09 +0000181 unsigned NextBBNum;
182
183 inline PerFunctionInfo() {
184 CurrentFunction = 0;
185 isDeclare = false;
186 Linkage = GlobalValue::ExternalLinkage;
187 }
188
189 inline void FunctionStart(Function *M) {
190 CurrentFunction = M;
191 NextBBNum = 0;
192 }
193
194 void FunctionDone() {
195 NumberedBlocks.clear();
196
197 // Any forward referenced blocks left?
198 if (!BBForwardRefs.empty()) {
199 error("Undefined reference to label " +
200 BBForwardRefs.begin()->first->getName());
201 return;
202 }
203
204 // Resolve all forward references now.
205 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
206
207 Values.clear(); // Clear out function local definitions
208 RenameMap.clear();
Reid Spencer950bf602007-01-26 08:19:09 +0000209 CurrentFunction = 0;
210 isDeclare = false;
211 Linkage = GlobalValue::ExternalLinkage;
212 }
213} CurFun; // Info for the current function...
214
215static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
216
Reid Spencerbb1fd572007-03-21 17:15:50 +0000217/// This function is just a utility to make a Key value for the rename map.
218/// The Key is a combination of the name, type, Signedness of the original
219/// value (global/function). This just constructs the key and ensures that
220/// named Signedness values are resolved to the actual Signedness.
221/// @brief Make a key for the RenameMaps
222static RenameMapKey makeRenameMapKey(const std::string &Name, const Type* Ty,
223 const Signedness &Sign) {
224 TypeInfo TI;
225 TI.T = Ty;
226 if (Sign.isNamed())
227 // Don't allow Named Signedness nodes because they won't match. The actual
228 // Signedness must be looked up in the NamedTypeSigns map.
229 TI.S.copy(CurModule.NamedTypeSigns[Sign.getName()]);
230 else
231 TI.S.copy(Sign);
232 return std::make_pair(Name, TI);
233}
234
Reid Spencer950bf602007-01-26 08:19:09 +0000235
236//===----------------------------------------------------------------------===//
237// Code to handle definitions of all the types
238//===----------------------------------------------------------------------===//
239
240static int InsertValue(Value *V,
241 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
242 if (V->hasName()) return -1; // Is this a numbered definition?
243
244 // Yes, insert the value into the value table...
245 ValueList &List = ValueTab[V->getType()];
246 List.push_back(V);
247 return List.size()-1;
248}
249
Reid Spencerd7c4f8c2007-01-26 19:59:25 +0000250static const Type *getType(const ValID &D, bool DoNotImprovise = false) {
Reid Spencer950bf602007-01-26 08:19:09 +0000251 switch (D.Type) {
252 case ValID::NumberVal: // Is it a numbered definition?
253 // Module constants occupy the lowest numbered slots...
254 if ((unsigned)D.Num < CurModule.Types.size()) {
255 return CurModule.Types[(unsigned)D.Num];
256 }
257 break;
258 case ValID::NameVal: // Is it a named definition?
259 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
Reid Spencer950bf602007-01-26 08:19:09 +0000260 return N;
261 }
262 break;
263 default:
264 error("Internal parser error: Invalid symbol type reference");
265 return 0;
266 }
267
268 // If we reached here, we referenced either a symbol that we don't know about
269 // or an id number that hasn't been read yet. We may be referencing something
270 // forward, so just create an entry to be resolved later and get to it...
271 //
272 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
273
Reid Spencer950bf602007-01-26 08:19:09 +0000274 if (inFunctionScope()) {
275 if (D.Type == ValID::NameVal) {
276 error("Reference to an undefined type: '" + D.getName() + "'");
277 return 0;
278 } else {
279 error("Reference to an undefined type: #" + itostr(D.Num));
280 return 0;
281 }
282 }
283
284 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
285 if (I != CurModule.LateResolveTypes.end())
286 return I->second;
287
288 Type *Typ = OpaqueType::get();
289 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
290 return Typ;
Reid Spencerbb1fd572007-03-21 17:15:50 +0000291}
292
293/// This is like the getType method except that instead of looking up the type
294/// for a given ID, it looks up that type's sign.
295/// @brief Get the signedness of a referenced type
296static Signedness getTypeSign(const ValID &D) {
297 switch (D.Type) {
298 case ValID::NumberVal: // Is it a numbered definition?
299 // Module constants occupy the lowest numbered slots...
300 if ((unsigned)D.Num < CurModule.TypeSigns.size()) {
301 return CurModule.TypeSigns[(unsigned)D.Num];
302 }
303 break;
304 case ValID::NameVal: { // Is it a named definition?
305 std::map<std::string,Signedness>::const_iterator I =
306 CurModule.NamedTypeSigns.find(D.Name);
307 if (I != CurModule.NamedTypeSigns.end())
308 return I->second;
309 // Perhaps its a named forward .. just cache the name
310 Signedness S;
311 S.makeNamed(D.Name);
312 return S;
313 }
314 default:
315 break;
316 }
317 // If we don't find it, its signless
318 Signedness S;
319 S.makeSignless();
320 return S;
321}
322
323/// This function is analagous to getElementType in LLVM. It provides the same
324/// function except that it looks up the Signedness instead of the type. This is
325/// used when processing GEP instructions that need to extract the type of an
326/// indexed struct/array/ptr member.
327/// @brief Look up an element's sign.
328static Signedness getElementSign(const ValueInfo& VI,
329 const std::vector<Value*> &Indices) {
330 const Type *Ptr = VI.V->getType();
331 assert(isa<PointerType>(Ptr) && "Need pointer type");
332
333 unsigned CurIdx = 0;
334 Signedness S(VI.S);
335 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
336 if (CurIdx == Indices.size())
337 break;
338
339 Value *Index = Indices[CurIdx++];
340 assert(!isa<PointerType>(CT) || CurIdx == 1 && "Invalid type");
341 Ptr = CT->getTypeAtIndex(Index);
342 if (const Type* Ty = Ptr->getForwardedType())
343 Ptr = Ty;
344 assert(S.isComposite() && "Bad Signedness type");
345 if (isa<StructType>(CT)) {
346 S = S.get(cast<ConstantInt>(Index)->getZExtValue());
347 } else {
348 S = S.get(0UL);
349 }
350 if (S.isNamed())
351 S = CurModule.NamedTypeSigns[S.getName()];
352 }
353 Signedness Result;
354 Result.makeComposite(S);
355 return Result;
356}
357
358/// This function just translates a ConstantInfo into a ValueInfo and calls
359/// getElementSign(ValueInfo,...). Its just a convenience.
360/// @brief ConstantInfo version of getElementSign.
361static Signedness getElementSign(const ConstInfo& CI,
362 const std::vector<Constant*> &Indices) {
363 ValueInfo VI;
364 VI.V = CI.C;
365 VI.S.copy(CI.S);
366 std::vector<Value*> Idx;
367 for (unsigned i = 0; i < Indices.size(); ++i)
368 Idx.push_back(Indices[i]);
369 Signedness result = getElementSign(VI, Idx);
370 VI.destroy();
371 return result;
372}
Reid Spencer950bf602007-01-26 08:19:09 +0000373
Reid Spencered96d1e2007-02-08 09:08:52 +0000374/// This function determines if two function types differ only in their use of
375/// the sret parameter attribute in the first argument. If they are identical
376/// in all other respects, it returns true. Otherwise, it returns false.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000377static bool FuncTysDifferOnlyBySRet(const FunctionType *F1,
378 const FunctionType *F2) {
Reid Spencered96d1e2007-02-08 09:08:52 +0000379 if (F1->getReturnType() != F2->getReturnType() ||
380 F1->getNumParams() != F2->getNumParams() ||
381 F1->getParamAttrs(0) != F2->getParamAttrs(0))
382 return false;
383 unsigned SRetMask = ~unsigned(FunctionType::StructRetAttribute);
384 for (unsigned i = 0; i < F1->getNumParams(); ++i) {
385 if (F1->getParamType(i) != F2->getParamType(i) ||
386 unsigned(F1->getParamAttrs(i+1)) & SRetMask !=
387 unsigned(F2->getParamAttrs(i+1)) & SRetMask)
388 return false;
389 }
390 return true;
391}
392
Reid Spencerbb1fd572007-03-21 17:15:50 +0000393/// This function determines if the type of V and Ty differ only by the SRet
394/// parameter attribute. This is a more generalized case of
395/// FuncTysDIfferOnlyBySRet since it doesn't require FunctionType arguments.
396static bool TypesDifferOnlyBySRet(Value *V, const Type* Ty) {
397 if (V->getType() == Ty)
398 return true;
399 const PointerType *PF1 = dyn_cast<PointerType>(Ty);
400 const PointerType *PF2 = dyn_cast<PointerType>(V->getType());
401 if (PF1 && PF2) {
402 const FunctionType* FT1 = dyn_cast<FunctionType>(PF1->getElementType());
403 const FunctionType* FT2 = dyn_cast<FunctionType>(PF2->getElementType());
404 if (FT1 && FT2)
405 return FuncTysDifferOnlyBySRet(FT1, FT2);
406 }
407 return false;
408}
409
Reid Spencered96d1e2007-02-08 09:08:52 +0000410// The upgrade of csretcc to sret param attribute may have caused a function
411// to not be found because the param attribute changed the type of the called
412// function. This helper function, used in getExistingValue, detects that
Reid Spencerbb1fd572007-03-21 17:15:50 +0000413// situation and bitcasts the function to the correct type.
Reid Spencered96d1e2007-02-08 09:08:52 +0000414static Value* handleSRetFuncTypeMerge(Value *V, const Type* Ty) {
415 // Handle degenerate cases
416 if (!V)
417 return 0;
418 if (V->getType() == Ty)
419 return V;
420
Reid Spencered96d1e2007-02-08 09:08:52 +0000421 const PointerType *PF1 = dyn_cast<PointerType>(Ty);
422 const PointerType *PF2 = dyn_cast<PointerType>(V->getType());
423 if (PF1 && PF2) {
Reid Spencerbb1fd572007-03-21 17:15:50 +0000424 const FunctionType *FT1 = dyn_cast<FunctionType>(PF1->getElementType());
425 const FunctionType *FT2 = dyn_cast<FunctionType>(PF2->getElementType());
Reid Spencered96d1e2007-02-08 09:08:52 +0000426 if (FT1 && FT2 && FuncTysDifferOnlyBySRet(FT1, FT2))
427 if (FT2->paramHasAttr(1, FunctionType::StructRetAttribute))
Reid Spencerbb1fd572007-03-21 17:15:50 +0000428 return V;
Reid Spencered96d1e2007-02-08 09:08:52 +0000429 else if (Constant *C = dyn_cast<Constant>(V))
Reid Spencerbb1fd572007-03-21 17:15:50 +0000430 return ConstantExpr::getBitCast(C, PF1);
Reid Spencered96d1e2007-02-08 09:08:52 +0000431 else
Reid Spencerbb1fd572007-03-21 17:15:50 +0000432 return new BitCastInst(V, PF1, "upgrd.cast", CurBB);
433
Reid Spencered96d1e2007-02-08 09:08:52 +0000434 }
Reid Spencerbb1fd572007-03-21 17:15:50 +0000435 return 0;
Reid Spencered96d1e2007-02-08 09:08:52 +0000436}
437
Reid Spencer950bf602007-01-26 08:19:09 +0000438// getExistingValue - Look up the value specified by the provided type and
439// the provided ValID. If the value exists and has already been defined, return
440// it. Otherwise return null.
441//
442static Value *getExistingValue(const Type *Ty, const ValID &D) {
443 if (isa<FunctionType>(Ty)) {
444 error("Functions are not values and must be referenced as pointers");
445 }
446
447 switch (D.Type) {
448 case ValID::NumberVal: { // Is it a numbered definition?
449 unsigned Num = (unsigned)D.Num;
450
451 // Module constants occupy the lowest numbered slots...
452 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
453 if (VI != CurModule.Values.end()) {
454 if (Num < VI->second.size())
455 return VI->second[Num];
456 Num -= VI->second.size();
457 }
458
459 // Make sure that our type is within bounds
460 VI = CurFun.Values.find(Ty);
461 if (VI == CurFun.Values.end()) return 0;
462
463 // Check that the number is within bounds...
464 if (VI->second.size() <= Num) return 0;
465
466 return VI->second[Num];
467 }
468
469 case ValID::NameVal: { // Is it a named definition?
470 // Get the name out of the ID
Reid Spencerbb1fd572007-03-21 17:15:50 +0000471 RenameMapKey Key = makeRenameMapKey(D.Name, Ty, D.S);
472 Value *V = 0;
Reid Spencer950bf602007-01-26 08:19:09 +0000473 if (inFunctionScope()) {
474 // See if the name was renamed
475 RenameMapType::const_iterator I = CurFun.RenameMap.find(Key);
476 std::string LookupName;
477 if (I != CurFun.RenameMap.end())
478 LookupName = I->second;
479 else
Reid Spencerbb1fd572007-03-21 17:15:50 +0000480 LookupName = D.Name;
Reid Spenceref9b9a72007-02-05 20:47:22 +0000481 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
482 V = SymTab.lookup(LookupName);
Reid Spencerbb1fd572007-03-21 17:15:50 +0000483 if (V && V->getType() != Ty)
484 V = handleSRetFuncTypeMerge(V, Ty);
485 assert((!V || TypesDifferOnlyBySRet(V, Ty)) && "Found wrong type");
Reid Spencer950bf602007-01-26 08:19:09 +0000486 }
487 if (!V) {
488 RenameMapType::const_iterator I = CurModule.RenameMap.find(Key);
489 std::string LookupName;
490 if (I != CurModule.RenameMap.end())
491 LookupName = I->second;
492 else
Reid Spencerbb1fd572007-03-21 17:15:50 +0000493 LookupName = D.Name;
Reid Spenceref9b9a72007-02-05 20:47:22 +0000494 V = CurModule.CurrentModule->getValueSymbolTable().lookup(LookupName);
Reid Spencerbb1fd572007-03-21 17:15:50 +0000495 if (V && V->getType() != Ty)
496 V = handleSRetFuncTypeMerge(V, Ty);
497 assert((!V || TypesDifferOnlyBySRet(V, Ty)) && "Found wrong type");
Reid Spencer950bf602007-01-26 08:19:09 +0000498 }
Reid Spenceref9b9a72007-02-05 20:47:22 +0000499 if (!V)
Reid Spencer950bf602007-01-26 08:19:09 +0000500 return 0;
501
502 D.destroy(); // Free old strdup'd memory...
503 return V;
504 }
505
506 // Check to make sure that "Ty" is an integral type, and that our
507 // value will fit into the specified type...
508 case ValID::ConstSIntVal: // Is it a constant pool reference??
509 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
510 error("Signed integral constant '" + itostr(D.ConstPool64) +
511 "' is invalid for type '" + Ty->getDescription() + "'");
512 }
513 return ConstantInt::get(Ty, D.ConstPool64);
514
515 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
516 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
517 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64))
518 error("Integral constant '" + utostr(D.UConstPool64) +
519 "' is invalid or out of range");
520 else // This is really a signed reference. Transmogrify.
521 return ConstantInt::get(Ty, D.ConstPool64);
522 } else
523 return ConstantInt::get(Ty, D.UConstPool64);
524
525 case ValID::ConstFPVal: // Is it a floating point const pool reference?
526 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
527 error("FP constant invalid for type");
528 return ConstantFP::get(Ty, D.ConstPoolFP);
529
530 case ValID::ConstNullVal: // Is it a null value?
531 if (!isa<PointerType>(Ty))
532 error("Cannot create a a non pointer null");
533 return ConstantPointerNull::get(cast<PointerType>(Ty));
534
535 case ValID::ConstUndefVal: // Is it an undef value?
536 return UndefValue::get(Ty);
537
538 case ValID::ConstZeroVal: // Is it a zero value?
539 return Constant::getNullValue(Ty);
540
541 case ValID::ConstantVal: // Fully resolved constant?
542 if (D.ConstantValue->getType() != Ty)
543 error("Constant expression type different from required type");
544 return D.ConstantValue;
545
546 case ValID::InlineAsmVal: { // Inline asm expression
547 const PointerType *PTy = dyn_cast<PointerType>(Ty);
548 const FunctionType *FTy =
549 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
550 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints))
551 error("Invalid type for asm constraint string");
552 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
553 D.IAD->HasSideEffects);
554 D.destroy(); // Free InlineAsmDescriptor.
555 return IA;
556 }
557 default:
558 assert(0 && "Unhandled case");
559 return 0;
560 } // End of switch
561
562 assert(0 && "Unhandled case");
563 return 0;
564}
565
566// getVal - This function is identical to getExistingValue, except that if a
567// value is not already defined, it "improvises" by creating a placeholder var
568// that looks and acts just like the requested variable. When the value is
569// defined later, all uses of the placeholder variable are replaced with the
570// real thing.
571//
572static Value *getVal(const Type *Ty, const ValID &ID) {
573 if (Ty == Type::LabelTy)
574 error("Cannot use a basic block here");
575
576 // See if the value has already been defined.
577 Value *V = getExistingValue(Ty, ID);
578 if (V) return V;
579
580 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty))
581 error("Invalid use of a composite type");
582
583 // If we reached here, we referenced either a symbol that we don't know about
584 // or an id number that hasn't been read yet. We may be referencing something
585 // forward, so just create an entry to be resolved later and get to it...
Reid Spencer950bf602007-01-26 08:19:09 +0000586 V = new Argument(Ty);
587
588 // Remember where this forward reference came from. FIXME, shouldn't we try
589 // to recycle these things??
590 CurModule.PlaceHolderInfo.insert(
Reid Spenceref9b9a72007-02-05 20:47:22 +0000591 std::make_pair(V, std::make_pair(ID, Upgradelineno)));
Reid Spencer950bf602007-01-26 08:19:09 +0000592
593 if (inFunctionScope())
594 InsertValue(V, CurFun.LateResolveValues);
595 else
596 InsertValue(V, CurModule.LateResolveValues);
597 return V;
598}
599
Reid Spencered96d1e2007-02-08 09:08:52 +0000600/// @brief This just makes any name given to it unique, up to MAX_UINT times.
601static std::string makeNameUnique(const std::string& Name) {
602 static unsigned UniqueNameCounter = 1;
603 std::string Result(Name);
604 Result += ".upgrd." + llvm::utostr(UniqueNameCounter++);
605 return Result;
606}
607
Reid Spencer950bf602007-01-26 08:19:09 +0000608/// getBBVal - This is used for two purposes:
609/// * If isDefinition is true, a new basic block with the specified ID is being
610/// defined.
611/// * If isDefinition is true, this is a reference to a basic block, which may
612/// or may not be a forward reference.
613///
614static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
615 assert(inFunctionScope() && "Can't get basic block at global scope");
616
617 std::string Name;
618 BasicBlock *BB = 0;
619 switch (ID.Type) {
620 default:
621 error("Illegal label reference " + ID.getName());
622 break;
623 case ValID::NumberVal: // Is it a numbered definition?
624 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
625 CurFun.NumberedBlocks.resize(ID.Num+1);
626 BB = CurFun.NumberedBlocks[ID.Num];
627 break;
628 case ValID::NameVal: // Is it a named definition?
629 Name = ID.Name;
Reid Spencerbb1fd572007-03-21 17:15:50 +0000630 if (Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name)) {
Reid Spencered96d1e2007-02-08 09:08:52 +0000631 if (N->getType() != Type::LabelTy) {
632 // Register names didn't use to conflict with basic block names
633 // because of type planes. Now they all have to be unique. So, we just
634 // rename the register and treat this name as if no basic block
635 // had been found.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000636 RenameMapKey Key = makeRenameMapKey(ID.Name, N->getType(), ID.S);
Reid Spencered96d1e2007-02-08 09:08:52 +0000637 N->setName(makeNameUnique(N->getName()));
638 CurModule.RenameMap[Key] = N->getName();
639 BB = 0;
640 } else {
641 BB = cast<BasicBlock>(N);
642 }
Reid Spencer950bf602007-01-26 08:19:09 +0000643 }
644 break;
645 }
646
647 // See if the block has already been defined.
648 if (BB) {
649 // If this is the definition of the block, make sure the existing value was
650 // just a forward reference. If it was a forward reference, there will be
651 // an entry for it in the PlaceHolderInfo map.
652 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
653 // The existing value was a definition, not a forward reference.
654 error("Redefinition of label " + ID.getName());
655
656 ID.destroy(); // Free strdup'd memory.
657 return BB;
658 }
659
660 // Otherwise this block has not been seen before.
661 BB = new BasicBlock("", CurFun.CurrentFunction);
662 if (ID.Type == ValID::NameVal) {
663 BB->setName(ID.Name);
664 } else {
665 CurFun.NumberedBlocks[ID.Num] = BB;
666 }
667
668 // If this is not a definition, keep track of it so we can use it as a forward
669 // reference.
670 if (!isDefinition) {
671 // Remember where this forward reference came from.
672 CurFun.BBForwardRefs[BB] = std::make_pair(ID, Upgradelineno);
673 } else {
674 // The forward declaration could have been inserted anywhere in the
675 // function: insert it into the correct place now.
676 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
677 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
678 }
679 ID.destroy();
680 return BB;
681}
682
683
684//===----------------------------------------------------------------------===//
685// Code to handle forward references in instructions
686//===----------------------------------------------------------------------===//
687//
688// This code handles the late binding needed with statements that reference
689// values not defined yet... for example, a forward branch, or the PHI node for
690// a loop body.
691//
692// This keeps a table (CurFun.LateResolveValues) of all such forward references
693// and back patchs after we are done.
694//
695
696// ResolveDefinitions - If we could not resolve some defs at parsing
697// time (forward branches, phi functions for loops, etc...) resolve the
698// defs now...
699//
700static void
701ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
702 std::map<const Type*,ValueList> *FutureLateResolvers) {
Reid Spencered96d1e2007-02-08 09:08:52 +0000703
Reid Spencer950bf602007-01-26 08:19:09 +0000704 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
705 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
706 E = LateResolvers.end(); LRI != E; ++LRI) {
Reid Spencered96d1e2007-02-08 09:08:52 +0000707 const Type* Ty = LRI->first;
Reid Spencer950bf602007-01-26 08:19:09 +0000708 ValueList &List = LRI->second;
709 while (!List.empty()) {
710 Value *V = List.back();
711 List.pop_back();
712
713 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
714 CurModule.PlaceHolderInfo.find(V);
715 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error");
716
717 ValID &DID = PHI->second.first;
718
Reid Spencered96d1e2007-02-08 09:08:52 +0000719 Value *TheRealValue = getExistingValue(Ty, DID);
Reid Spencer950bf602007-01-26 08:19:09 +0000720 if (TheRealValue) {
721 V->replaceAllUsesWith(TheRealValue);
722 delete V;
723 CurModule.PlaceHolderInfo.erase(PHI);
724 } else if (FutureLateResolvers) {
725 // Functions have their unresolved items forwarded to the module late
726 // resolver table
727 InsertValue(V, *FutureLateResolvers);
728 } else {
729 if (DID.Type == ValID::NameVal) {
Reid Spencered96d1e2007-02-08 09:08:52 +0000730 error("Reference to an invalid definition: '" + DID.getName() +
731 "' of type '" + V->getType()->getDescription() + "'",
732 PHI->second.second);
Reid Spencer7de2e012007-01-29 19:08:46 +0000733 return;
Reid Spencer950bf602007-01-26 08:19:09 +0000734 } else {
735 error("Reference to an invalid definition: #" +
736 itostr(DID.Num) + " of type '" +
737 V->getType()->getDescription() + "'", PHI->second.second);
738 return;
739 }
740 }
741 }
742 }
743
744 LateResolvers.clear();
745}
746
Reid Spencerbb1fd572007-03-21 17:15:50 +0000747/// This function is used for type resolution and upref handling. When a type
748/// becomes concrete, this function is called to adjust the signedness for the
749/// concrete type.
750static void ResolveTypeSign(const Type* oldTy, const Signedness &Sign) {
751 std::string TyName = CurModule.CurrentModule->getTypeName(oldTy);
752 if (!TyName.empty())
753 CurModule.NamedTypeSigns[TyName] = Sign;
754}
755
756/// ResolveTypeTo - A brand new type was just declared. This means that (if
757/// name is not null) things referencing Name can be resolved. Otherwise,
758/// things refering to the number can be resolved. Do this now.
759static void ResolveTypeTo(char *Name, const Type *ToTy, const Signedness& Sign){
Reid Spencer950bf602007-01-26 08:19:09 +0000760 ValID D;
Reid Spencerbb1fd572007-03-21 17:15:50 +0000761 if (Name)
762 D = ValID::create(Name);
763 else
764 D = ValID::create((int)CurModule.Types.size());
765 D.S.copy(Sign);
766
767 CurModule.NamedTypeSigns[Name] = Sign;
Reid Spencer950bf602007-01-26 08:19:09 +0000768
769 std::map<ValID, PATypeHolder>::iterator I =
770 CurModule.LateResolveTypes.find(D);
771 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencerbb1fd572007-03-21 17:15:50 +0000772 const Type *OldTy = I->second.get();
773 ((DerivedType*)OldTy)->refineAbstractTypeTo(ToTy);
Reid Spencer950bf602007-01-26 08:19:09 +0000774 CurModule.LateResolveTypes.erase(I);
775 }
776}
777
Anton Korobeynikovce13b852007-01-28 15:25:24 +0000778/// This is the implementation portion of TypeHasInteger. It traverses the
779/// type given, avoiding recursive types, and returns true as soon as it finds
780/// an integer type. If no integer type is found, it returns false.
781static bool TypeHasIntegerI(const Type *Ty, std::vector<const Type*> Stack) {
782 // Handle some easy cases
783 if (Ty->isPrimitiveType() || (Ty->getTypeID() == Type::OpaqueTyID))
784 return false;
785 if (Ty->isInteger())
786 return true;
787 if (const SequentialType *STy = dyn_cast<SequentialType>(Ty))
788 return STy->getElementType()->isInteger();
789
790 // Avoid type structure recursion
791 for (std::vector<const Type*>::iterator I = Stack.begin(), E = Stack.end();
792 I != E; ++I)
793 if (Ty == *I)
794 return false;
795
796 // Push us on the type stack
797 Stack.push_back(Ty);
798
799 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
800 if (TypeHasIntegerI(FTy->getReturnType(), Stack))
801 return true;
802 FunctionType::param_iterator I = FTy->param_begin();
803 FunctionType::param_iterator E = FTy->param_end();
804 for (; I != E; ++I)
805 if (TypeHasIntegerI(*I, Stack))
806 return true;
807 return false;
808 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
809 StructType::element_iterator I = STy->element_begin();
810 StructType::element_iterator E = STy->element_end();
811 for (; I != E; ++I) {
812 if (TypeHasIntegerI(*I, Stack))
813 return true;
814 }
815 return false;
816 }
817 // There shouldn't be anything else, but its definitely not integer
818 assert(0 && "What type is this?");
819 return false;
820}
821
822/// This is the interface to TypeHasIntegerI. It just provides the type stack,
823/// to avoid recursion, and then calls TypeHasIntegerI.
824static inline bool TypeHasInteger(const Type *Ty) {
825 std::vector<const Type*> TyStack;
826 return TypeHasIntegerI(Ty, TyStack);
827}
828
Reid Spencer950bf602007-01-26 08:19:09 +0000829// setValueName - Set the specified value to the name given. The name may be
830// null potentially, in which case this is a noop. The string passed in is
831// assumed to be a malloc'd string buffer, and is free'd by this function.
832//
Reid Spencerbb1fd572007-03-21 17:15:50 +0000833static void setValueName(const ValueInfo &V, char *NameStr) {
Reid Spencer950bf602007-01-26 08:19:09 +0000834 if (NameStr) {
835 std::string Name(NameStr); // Copy string
836 free(NameStr); // Free old string
837
Reid Spencerbb1fd572007-03-21 17:15:50 +0000838 if (V.V->getType() == Type::VoidTy) {
Reid Spencer950bf602007-01-26 08:19:09 +0000839 error("Can't assign name '" + Name + "' to value with void type");
840 return;
841 }
842
Reid Spencer950bf602007-01-26 08:19:09 +0000843 assert(inFunctionScope() && "Must be in function scope");
844
845 // Search the function's symbol table for an existing value of this name
Reid Spenceref9b9a72007-02-05 20:47:22 +0000846 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
847 Value* Existing = ST.lookup(Name);
Reid Spencer950bf602007-01-26 08:19:09 +0000848 if (Existing) {
Anton Korobeynikovce13b852007-01-28 15:25:24 +0000849 // An existing value of the same name was found. This might have happened
850 // because of the integer type planes collapsing in LLVM 2.0.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000851 if (Existing->getType() == V.V->getType() &&
Anton Korobeynikovce13b852007-01-28 15:25:24 +0000852 !TypeHasInteger(Existing->getType())) {
853 // If the type does not contain any integers in them then this can't be
854 // a type plane collapsing issue. It truly is a redefinition and we
855 // should error out as the assembly is invalid.
856 error("Redefinition of value named '" + Name + "' of type '" +
Reid Spencerbb1fd572007-03-21 17:15:50 +0000857 V.V->getType()->getDescription() + "'");
Anton Korobeynikovce13b852007-01-28 15:25:24 +0000858 return;
Reid Spencer950bf602007-01-26 08:19:09 +0000859 }
860 // In LLVM 2.0 we don't allow names to be re-used for any values in a
861 // function, regardless of Type. Previously re-use of names was okay as
862 // long as they were distinct types. With type planes collapsing because
863 // of the signedness change and because of PR411, this can no longer be
864 // supported. We must search the entire symbol table for a conflicting
865 // name and make the name unique. No warning is needed as this can't
866 // cause a problem.
867 std::string NewName = makeNameUnique(Name);
868 // We're changing the name but it will probably be used by other
869 // instructions as operands later on. Consequently we have to retain
870 // a mapping of the renaming that we're doing.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000871 RenameMapKey Key = makeRenameMapKey(Name, V.V->getType(), V.S);
Reid Spencer950bf602007-01-26 08:19:09 +0000872 CurFun.RenameMap[Key] = NewName;
873 Name = NewName;
874 }
875
876 // Set the name.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000877 V.V->setName(Name);
Reid Spencer950bf602007-01-26 08:19:09 +0000878 }
879}
880
881/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
882/// this is a declaration, otherwise it is a definition.
883static GlobalVariable *
884ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
885 bool isConstantGlobal, const Type *Ty,
Reid Spencerbb1fd572007-03-21 17:15:50 +0000886 Constant *Initializer,
887 const Signedness &Sign) {
Reid Spencer950bf602007-01-26 08:19:09 +0000888 if (isa<FunctionType>(Ty))
889 error("Cannot declare global vars of function type");
890
891 const PointerType *PTy = PointerType::get(Ty);
892
893 std::string Name;
894 if (NameStr) {
895 Name = NameStr; // Copy string
896 free(NameStr); // Free old string
897 }
898
899 // See if this global value was forward referenced. If so, recycle the
900 // object.
901 ValID ID;
902 if (!Name.empty()) {
Reid Spencer5eb77c72007-03-15 03:26:42 +0000903 ID = ValID::create((char*)Name.c_str());
Reid Spencer950bf602007-01-26 08:19:09 +0000904 } else {
Reid Spencer5eb77c72007-03-15 03:26:42 +0000905 ID = ValID::create((int)CurModule.Values[PTy].size());
Reid Spencer950bf602007-01-26 08:19:09 +0000906 }
Reid Spencerbb1fd572007-03-21 17:15:50 +0000907 ID.S.makeComposite(Sign);
Reid Spencer950bf602007-01-26 08:19:09 +0000908
909 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
910 // Move the global to the end of the list, from whereever it was
911 // previously inserted.
912 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
913 CurModule.CurrentModule->getGlobalList().remove(GV);
914 CurModule.CurrentModule->getGlobalList().push_back(GV);
915 GV->setInitializer(Initializer);
916 GV->setLinkage(Linkage);
917 GV->setConstant(isConstantGlobal);
918 InsertValue(GV, CurModule.Values);
919 return GV;
920 }
921
922 // If this global has a name, check to see if there is already a definition
923 // of this global in the module and emit warnings if there are conflicts.
924 if (!Name.empty()) {
925 // The global has a name. See if there's an existing one of the same name.
926 if (CurModule.CurrentModule->getNamedGlobal(Name)) {
927 // We found an existing global ov the same name. This isn't allowed
928 // in LLVM 2.0. Consequently, we must alter the name of the global so it
929 // can at least compile. This can happen because of type planes
930 // There is alread a global of the same name which means there is a
931 // conflict. Let's see what we can do about it.
932 std::string NewName(makeNameUnique(Name));
Reid Spencerbb1fd572007-03-21 17:15:50 +0000933 if (Linkage != GlobalValue::InternalLinkage) {
Reid Spencer950bf602007-01-26 08:19:09 +0000934 // The linkage of this gval is external so we can't reliably rename
935 // it because it could potentially create a linking problem.
936 // However, we can't leave the name conflict in the output either or
937 // it won't assemble with LLVM 2.0. So, all we can do is rename
938 // this one to something unique and emit a warning about the problem.
939 warning("Renaming global variable '" + Name + "' to '" + NewName +
940 "' may cause linkage errors");
941 }
942
943 // Put the renaming in the global rename map
Reid Spencerbb1fd572007-03-21 17:15:50 +0000944 RenameMapKey Key = makeRenameMapKey(Name, PointerType::get(Ty), ID.S);
Reid Spencer950bf602007-01-26 08:19:09 +0000945 CurModule.RenameMap[Key] = NewName;
946
947 // Rename it
948 Name = NewName;
949 }
950 }
951
952 // Otherwise there is no existing GV to use, create one now.
953 GlobalVariable *GV =
954 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
955 CurModule.CurrentModule);
956 InsertValue(GV, CurModule.Values);
Reid Spencerbb1fd572007-03-21 17:15:50 +0000957 // Remember the sign of this global.
958 CurModule.NamedValueSigns[Name] = ID.S;
Reid Spencer950bf602007-01-26 08:19:09 +0000959 return GV;
960}
961
962// setTypeName - Set the specified type to the name given. The name may be
963// null potentially, in which case this is a noop. The string passed in is
964// assumed to be a malloc'd string buffer, and is freed by this function.
965//
966// This function returns true if the type has already been defined, but is
967// allowed to be redefined in the specified context. If the name is a new name
968// for the type plane, it is inserted and false is returned.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000969static bool setTypeName(const PATypeInfo& TI, char *NameStr) {
Reid Spencer950bf602007-01-26 08:19:09 +0000970 assert(!inFunctionScope() && "Can't give types function-local names");
971 if (NameStr == 0) return false;
972
973 std::string Name(NameStr); // Copy string
974 free(NameStr); // Free old string
975
Reid Spencerbb1fd572007-03-21 17:15:50 +0000976 const Type* Ty = TI.PAT->get();
977
Reid Spencer950bf602007-01-26 08:19:09 +0000978 // We don't allow assigning names to void type
Reid Spencerbb1fd572007-03-21 17:15:50 +0000979 if (Ty == Type::VoidTy) {
Reid Spencer950bf602007-01-26 08:19:09 +0000980 error("Can't assign name '" + Name + "' to the void type");
981 return false;
982 }
983
984 // Set the type name, checking for conflicts as we do so.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000985 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, Ty);
986
987 // Save the sign information for later use
988 CurModule.NamedTypeSigns[Name] = TI.S;
Reid Spencer950bf602007-01-26 08:19:09 +0000989
990 if (AlreadyExists) { // Inserting a name that is already defined???
991 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
992 assert(Existing && "Conflict but no matching type?");
993
994 // There is only one case where this is allowed: when we are refining an
995 // opaque type. In this case, Existing will be an opaque type.
996 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
997 // We ARE replacing an opaque type!
Reid Spencerbb1fd572007-03-21 17:15:50 +0000998 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(Ty);
Reid Spencer950bf602007-01-26 08:19:09 +0000999 return true;
1000 }
1001
1002 // Otherwise, this is an attempt to redefine a type. That's okay if
1003 // the redefinition is identical to the original. This will be so if
1004 // Existing and T point to the same Type object. In this one case we
1005 // allow the equivalent redefinition.
Reid Spencerbb1fd572007-03-21 17:15:50 +00001006 if (Existing == Ty) return true; // Yes, it's equal.
Reid Spencer950bf602007-01-26 08:19:09 +00001007
1008 // Any other kind of (non-equivalent) redefinition is an error.
1009 error("Redefinition of type named '" + Name + "' in the '" +
Reid Spencerbb1fd572007-03-21 17:15:50 +00001010 Ty->getDescription() + "' type plane");
Reid Spencer950bf602007-01-26 08:19:09 +00001011 }
1012
1013 return false;
1014}
1015
1016//===----------------------------------------------------------------------===//
1017// Code for handling upreferences in type names...
1018//
1019
1020// TypeContains - Returns true if Ty directly contains E in it.
1021//
1022static bool TypeContains(const Type *Ty, const Type *E) {
1023 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
1024 E) != Ty->subtype_end();
1025}
1026
1027namespace {
1028 struct UpRefRecord {
1029 // NestingLevel - The number of nesting levels that need to be popped before
1030 // this type is resolved.
1031 unsigned NestingLevel;
1032
1033 // LastContainedTy - This is the type at the current binding level for the
1034 // type. Every time we reduce the nesting level, this gets updated.
1035 const Type *LastContainedTy;
1036
1037 // UpRefTy - This is the actual opaque type that the upreference is
1038 // represented with.
1039 OpaqueType *UpRefTy;
1040
1041 UpRefRecord(unsigned NL, OpaqueType *URTy)
Reid Spencerbb1fd572007-03-21 17:15:50 +00001042 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) { }
Reid Spencer950bf602007-01-26 08:19:09 +00001043 };
1044}
1045
1046// UpRefs - A list of the outstanding upreferences that need to be resolved.
1047static std::vector<UpRefRecord> UpRefs;
1048
1049/// HandleUpRefs - Every time we finish a new layer of types, this function is
1050/// called. It loops through the UpRefs vector, which is a list of the
1051/// currently active types. For each type, if the up reference is contained in
1052/// the newly completed type, we decrement the level count. When the level
1053/// count reaches zero, the upreferenced type is the type that is passed in:
1054/// thus we can complete the cycle.
1055///
Reid Spencerbb1fd572007-03-21 17:15:50 +00001056static PATypeHolder HandleUpRefs(const Type *ty, const Signedness& Sign) {
Reid Spencer950bf602007-01-26 08:19:09 +00001057 // If Ty isn't abstract, or if there are no up-references in it, then there is
1058 // nothing to resolve here.
1059 if (!ty->isAbstract() || UpRefs.empty()) return ty;
1060
1061 PATypeHolder Ty(ty);
1062 UR_OUT("Type '" << Ty->getDescription() <<
1063 "' newly formed. Resolving upreferences.\n" <<
1064 UpRefs.size() << " upreferences active!\n");
1065
1066 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
1067 // to zero), we resolve them all together before we resolve them to Ty. At
1068 // the end of the loop, if there is anything to resolve to Ty, it will be in
1069 // this variable.
1070 OpaqueType *TypeToResolve = 0;
1071
Reid Spencerbb1fd572007-03-21 17:15:50 +00001072 unsigned i = 0;
1073 for (; i != UpRefs.size(); ++i) {
Reid Spencer950bf602007-01-26 08:19:09 +00001074 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Reid Spencerbb1fd572007-03-21 17:15:50 +00001075 << UpRefs[i].UpRefTy->getDescription() << ") = "
1076 << (TypeContains(Ty, UpRefs[i].UpRefTy) ? "true" : "false") << "\n");
Reid Spencer950bf602007-01-26 08:19:09 +00001077 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
1078 // Decrement level of upreference
1079 unsigned Level = --UpRefs[i].NestingLevel;
1080 UpRefs[i].LastContainedTy = Ty;
1081 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
1082 if (Level == 0) { // Upreference should be resolved!
1083 if (!TypeToResolve) {
1084 TypeToResolve = UpRefs[i].UpRefTy;
1085 } else {
1086 UR_OUT(" * Resolving upreference for "
Reid Spencerbb1fd572007-03-21 17:15:50 +00001087 << UpRefs[i].UpRefTy->getDescription() << "\n";
1088 std::string OldName = UpRefs[i].UpRefTy->getDescription());
1089 ResolveTypeSign(UpRefs[i].UpRefTy, Sign);
Reid Spencer950bf602007-01-26 08:19:09 +00001090 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
1091 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
1092 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
1093 }
1094 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
1095 --i; // Do not skip the next element...
1096 }
1097 }
1098 }
1099
1100 if (TypeToResolve) {
1101 UR_OUT(" * Resolving upreference for "
Reid Spencerbb1fd572007-03-21 17:15:50 +00001102 << UpRefs[i].UpRefTy->getDescription() << "\n";
Reid Spencer950bf602007-01-26 08:19:09 +00001103 std::string OldName = TypeToResolve->getDescription());
Reid Spencerbb1fd572007-03-21 17:15:50 +00001104 ResolveTypeSign(TypeToResolve, Sign);
Reid Spencer950bf602007-01-26 08:19:09 +00001105 TypeToResolve->refineAbstractTypeTo(Ty);
1106 }
1107
1108 return Ty;
1109}
1110
Reid Spencerbb1fd572007-03-21 17:15:50 +00001111bool Signedness::operator<(const Signedness &that) const {
1112 if (isNamed()) {
1113 if (that.isNamed())
1114 return *(this->name) < *(that.name);
1115 else
1116 return CurModule.NamedTypeSigns[*name] < that;
1117 } else if (that.isNamed()) {
1118 return *this < CurModule.NamedTypeSigns[*that.name];
1119 }
1120
1121 if (isComposite() && that.isComposite()) {
1122 if (sv->size() == that.sv->size()) {
1123 SignVector::const_iterator thisI = sv->begin(), thisE = sv->end();
1124 SignVector::const_iterator thatI = that.sv->begin(),
1125 thatE = that.sv->end();
1126 for (; thisI != thisE; ++thisI, ++thatI) {
1127 if (*thisI < *thatI)
1128 return true;
1129 else if (!(*thisI == *thatI))
1130 return false;
1131 }
1132 return false;
1133 }
1134 return sv->size() < that.sv->size();
1135 }
1136 return kind < that.kind;
1137}
1138
1139bool Signedness::operator==(const Signedness &that) const {
1140 if (isNamed())
1141 if (that.isNamed())
1142 return *(this->name) == *(that.name);
1143 else
1144 return CurModule.NamedTypeSigns[*(this->name)] == that;
1145 else if (that.isNamed())
1146 return *this == CurModule.NamedTypeSigns[*(that.name)];
1147 if (isComposite() && that.isComposite()) {
1148 if (sv->size() == that.sv->size()) {
1149 SignVector::const_iterator thisI = sv->begin(), thisE = sv->end();
1150 SignVector::const_iterator thatI = that.sv->begin(),
1151 thatE = that.sv->end();
1152 for (; thisI != thisE; ++thisI, ++thatI) {
1153 if (!(*thisI == *thatI))
1154 return false;
1155 }
1156 return true;
1157 }
1158 return false;
1159 }
1160 return kind == that.kind;
1161}
1162
1163void Signedness::copy(const Signedness &that) {
1164 if (that.isNamed()) {
1165 kind = Named;
1166 name = new std::string(*that.name);
1167 } else if (that.isComposite()) {
1168 kind = Composite;
1169 sv = new SignVector();
1170 *sv = *that.sv;
1171 } else {
1172 kind = that.kind;
1173 sv = 0;
1174 }
1175}
1176
1177void Signedness::destroy() {
1178 if (isNamed()) {
1179 delete name;
1180 } else if (isComposite()) {
1181 delete sv;
1182 }
1183}
1184
Evan Cheng2b484202007-03-22 07:43:51 +00001185#ifndef NDEBUG
Reid Spencerbb1fd572007-03-21 17:15:50 +00001186void Signedness::dump() const {
1187 if (isComposite()) {
1188 if (sv->size() == 1) {
1189 (*sv)[0].dump();
1190 std::cerr << "*";
1191 } else {
1192 std::cerr << "{ " ;
1193 for (unsigned i = 0; i < sv->size(); ++i) {
1194 if (i != 0)
1195 std::cerr << ", ";
1196 (*sv)[i].dump();
1197 }
1198 std::cerr << "} " ;
1199 }
1200 } else if (isNamed()) {
1201 std::cerr << *name;
1202 } else if (isSigned()) {
1203 std::cerr << "S";
1204 } else if (isUnsigned()) {
1205 std::cerr << "U";
1206 } else
1207 std::cerr << ".";
1208}
Evan Cheng2b484202007-03-22 07:43:51 +00001209#endif
Reid Spencerbb1fd572007-03-21 17:15:50 +00001210
Reid Spencer950bf602007-01-26 08:19:09 +00001211static inline Instruction::TermOps
1212getTermOp(TermOps op) {
1213 switch (op) {
1214 default : assert(0 && "Invalid OldTermOp");
1215 case RetOp : return Instruction::Ret;
1216 case BrOp : return Instruction::Br;
1217 case SwitchOp : return Instruction::Switch;
1218 case InvokeOp : return Instruction::Invoke;
1219 case UnwindOp : return Instruction::Unwind;
1220 case UnreachableOp: return Instruction::Unreachable;
1221 }
1222}
1223
1224static inline Instruction::BinaryOps
Reid Spencerbb1fd572007-03-21 17:15:50 +00001225getBinaryOp(BinaryOps op, const Type *Ty, const Signedness& Sign) {
Reid Spencer950bf602007-01-26 08:19:09 +00001226 switch (op) {
1227 default : assert(0 && "Invalid OldBinaryOps");
1228 case SetEQ :
1229 case SetNE :
1230 case SetLE :
1231 case SetGE :
1232 case SetLT :
1233 case SetGT : assert(0 && "Should use getCompareOp");
1234 case AddOp : return Instruction::Add;
1235 case SubOp : return Instruction::Sub;
1236 case MulOp : return Instruction::Mul;
1237 case DivOp : {
1238 // This is an obsolete instruction so we must upgrade it based on the
1239 // types of its operands.
1240 bool isFP = Ty->isFloatingPoint();
Reid Spencer9d6565a2007-02-15 02:26:10 +00001241 if (const VectorType* PTy = dyn_cast<VectorType>(Ty))
Chris Lattner4227bdb2007-02-19 07:34:02 +00001242 // If its a vector type we want to use the element type
Reid Spencer950bf602007-01-26 08:19:09 +00001243 isFP = PTy->getElementType()->isFloatingPoint();
1244 if (isFP)
1245 return Instruction::FDiv;
Reid Spencerbb1fd572007-03-21 17:15:50 +00001246 else if (Sign.isSigned())
Reid Spencer950bf602007-01-26 08:19:09 +00001247 return Instruction::SDiv;
1248 return Instruction::UDiv;
1249 }
1250 case UDivOp : return Instruction::UDiv;
1251 case SDivOp : return Instruction::SDiv;
1252 case FDivOp : return Instruction::FDiv;
1253 case RemOp : {
1254 // This is an obsolete instruction so we must upgrade it based on the
1255 // types of its operands.
1256 bool isFP = Ty->isFloatingPoint();
Reid Spencer9d6565a2007-02-15 02:26:10 +00001257 if (const VectorType* PTy = dyn_cast<VectorType>(Ty))
Chris Lattner4227bdb2007-02-19 07:34:02 +00001258 // If its a vector type we want to use the element type
Reid Spencer950bf602007-01-26 08:19:09 +00001259 isFP = PTy->getElementType()->isFloatingPoint();
1260 // Select correct opcode
1261 if (isFP)
1262 return Instruction::FRem;
Reid Spencerbb1fd572007-03-21 17:15:50 +00001263 else if (Sign.isSigned())
Reid Spencer950bf602007-01-26 08:19:09 +00001264 return Instruction::SRem;
1265 return Instruction::URem;
1266 }
1267 case URemOp : return Instruction::URem;
1268 case SRemOp : return Instruction::SRem;
1269 case FRemOp : return Instruction::FRem;
Reid Spencer832254e2007-02-02 02:16:23 +00001270 case LShrOp : return Instruction::LShr;
1271 case AShrOp : return Instruction::AShr;
1272 case ShlOp : return Instruction::Shl;
1273 case ShrOp :
Reid Spencerbb1fd572007-03-21 17:15:50 +00001274 if (Sign.isSigned())
Reid Spencer832254e2007-02-02 02:16:23 +00001275 return Instruction::AShr;
1276 return Instruction::LShr;
Reid Spencer950bf602007-01-26 08:19:09 +00001277 case AndOp : return Instruction::And;
1278 case OrOp : return Instruction::Or;
1279 case XorOp : return Instruction::Xor;
1280 }
1281}
1282
1283static inline Instruction::OtherOps
1284getCompareOp(BinaryOps op, unsigned short &predicate, const Type* &Ty,
Reid Spencerbb1fd572007-03-21 17:15:50 +00001285 const Signedness &Sign) {
1286 bool isSigned = Sign.isSigned();
Reid Spencer950bf602007-01-26 08:19:09 +00001287 bool isFP = Ty->isFloatingPoint();
1288 switch (op) {
1289 default : assert(0 && "Invalid OldSetCC");
1290 case SetEQ :
1291 if (isFP) {
1292 predicate = FCmpInst::FCMP_OEQ;
1293 return Instruction::FCmp;
1294 } else {
1295 predicate = ICmpInst::ICMP_EQ;
1296 return Instruction::ICmp;
1297 }
1298 case SetNE :
1299 if (isFP) {
1300 predicate = FCmpInst::FCMP_UNE;
1301 return Instruction::FCmp;
1302 } else {
1303 predicate = ICmpInst::ICMP_NE;
1304 return Instruction::ICmp;
1305 }
1306 case SetLE :
1307 if (isFP) {
1308 predicate = FCmpInst::FCMP_OLE;
1309 return Instruction::FCmp;
1310 } else {
1311 if (isSigned)
1312 predicate = ICmpInst::ICMP_SLE;
1313 else
1314 predicate = ICmpInst::ICMP_ULE;
1315 return Instruction::ICmp;
1316 }
1317 case SetGE :
1318 if (isFP) {
1319 predicate = FCmpInst::FCMP_OGE;
1320 return Instruction::FCmp;
1321 } else {
1322 if (isSigned)
1323 predicate = ICmpInst::ICMP_SGE;
1324 else
1325 predicate = ICmpInst::ICMP_UGE;
1326 return Instruction::ICmp;
1327 }
1328 case SetLT :
1329 if (isFP) {
1330 predicate = FCmpInst::FCMP_OLT;
1331 return Instruction::FCmp;
1332 } else {
1333 if (isSigned)
1334 predicate = ICmpInst::ICMP_SLT;
1335 else
1336 predicate = ICmpInst::ICMP_ULT;
1337 return Instruction::ICmp;
1338 }
1339 case SetGT :
1340 if (isFP) {
1341 predicate = FCmpInst::FCMP_OGT;
1342 return Instruction::FCmp;
1343 } else {
1344 if (isSigned)
1345 predicate = ICmpInst::ICMP_SGT;
1346 else
1347 predicate = ICmpInst::ICMP_UGT;
1348 return Instruction::ICmp;
1349 }
1350 }
1351}
1352
1353static inline Instruction::MemoryOps getMemoryOp(MemoryOps op) {
1354 switch (op) {
1355 default : assert(0 && "Invalid OldMemoryOps");
1356 case MallocOp : return Instruction::Malloc;
1357 case FreeOp : return Instruction::Free;
1358 case AllocaOp : return Instruction::Alloca;
1359 case LoadOp : return Instruction::Load;
1360 case StoreOp : return Instruction::Store;
1361 case GetElementPtrOp : return Instruction::GetElementPtr;
1362 }
1363}
1364
1365static inline Instruction::OtherOps
Reid Spencerbb1fd572007-03-21 17:15:50 +00001366getOtherOp(OtherOps op, const Signedness &Sign) {
Reid Spencer950bf602007-01-26 08:19:09 +00001367 switch (op) {
1368 default : assert(0 && "Invalid OldOtherOps");
1369 case PHIOp : return Instruction::PHI;
1370 case CallOp : return Instruction::Call;
Reid Spencer950bf602007-01-26 08:19:09 +00001371 case SelectOp : return Instruction::Select;
1372 case UserOp1 : return Instruction::UserOp1;
1373 case UserOp2 : return Instruction::UserOp2;
1374 case VAArg : return Instruction::VAArg;
1375 case ExtractElementOp : return Instruction::ExtractElement;
1376 case InsertElementOp : return Instruction::InsertElement;
1377 case ShuffleVectorOp : return Instruction::ShuffleVector;
1378 case ICmpOp : return Instruction::ICmp;
1379 case FCmpOp : return Instruction::FCmp;
Reid Spencer950bf602007-01-26 08:19:09 +00001380 };
1381}
1382
1383static inline Value*
Reid Spencerbb1fd572007-03-21 17:15:50 +00001384getCast(CastOps op, Value *Src, const Signedness &SrcSign, const Type *DstTy,
1385 const Signedness &DstSign, bool ForceInstruction = false) {
Reid Spencer950bf602007-01-26 08:19:09 +00001386 Instruction::CastOps Opcode;
1387 const Type* SrcTy = Src->getType();
1388 if (op == CastOp) {
1389 if (SrcTy->isFloatingPoint() && isa<PointerType>(DstTy)) {
1390 // fp -> ptr cast is no longer supported but we must upgrade this
1391 // by doing a double cast: fp -> int -> ptr
1392 SrcTy = Type::Int64Ty;
1393 Opcode = Instruction::IntToPtr;
1394 if (isa<Constant>(Src)) {
1395 Src = ConstantExpr::getCast(Instruction::FPToUI,
1396 cast<Constant>(Src), SrcTy);
1397 } else {
1398 std::string NewName(makeNameUnique(Src->getName()));
1399 Src = new FPToUIInst(Src, SrcTy, NewName, CurBB);
1400 }
1401 } else if (isa<IntegerType>(DstTy) &&
1402 cast<IntegerType>(DstTy)->getBitWidth() == 1) {
1403 // cast type %x to bool was previously defined as setne type %x, null
1404 // The cast semantic is now to truncate, not compare so we must retain
1405 // the original intent by replacing the cast with a setne
1406 Constant* Null = Constant::getNullValue(SrcTy);
1407 Instruction::OtherOps Opcode = Instruction::ICmp;
1408 unsigned short predicate = ICmpInst::ICMP_NE;
1409 if (SrcTy->isFloatingPoint()) {
1410 Opcode = Instruction::FCmp;
1411 predicate = FCmpInst::FCMP_ONE;
1412 } else if (!SrcTy->isInteger() && !isa<PointerType>(SrcTy)) {
1413 error("Invalid cast to bool");
1414 }
1415 if (isa<Constant>(Src) && !ForceInstruction)
1416 return ConstantExpr::getCompare(predicate, cast<Constant>(Src), Null);
1417 else
1418 return CmpInst::create(Opcode, predicate, Src, Null);
1419 }
1420 // Determine the opcode to use by calling CastInst::getCastOpcode
1421 Opcode =
Reid Spencerbb1fd572007-03-21 17:15:50 +00001422 CastInst::getCastOpcode(Src, SrcSign.isSigned(), DstTy,
1423 DstSign.isSigned());
Reid Spencer950bf602007-01-26 08:19:09 +00001424
1425 } else switch (op) {
1426 default: assert(0 && "Invalid cast token");
1427 case TruncOp: Opcode = Instruction::Trunc; break;
1428 case ZExtOp: Opcode = Instruction::ZExt; break;
1429 case SExtOp: Opcode = Instruction::SExt; break;
1430 case FPTruncOp: Opcode = Instruction::FPTrunc; break;
1431 case FPExtOp: Opcode = Instruction::FPExt; break;
1432 case FPToUIOp: Opcode = Instruction::FPToUI; break;
1433 case FPToSIOp: Opcode = Instruction::FPToSI; break;
1434 case UIToFPOp: Opcode = Instruction::UIToFP; break;
1435 case SIToFPOp: Opcode = Instruction::SIToFP; break;
1436 case PtrToIntOp: Opcode = Instruction::PtrToInt; break;
1437 case IntToPtrOp: Opcode = Instruction::IntToPtr; break;
1438 case BitCastOp: Opcode = Instruction::BitCast; break;
1439 }
1440
1441 if (isa<Constant>(Src) && !ForceInstruction)
1442 return ConstantExpr::getCast(Opcode, cast<Constant>(Src), DstTy);
1443 return CastInst::create(Opcode, Src, DstTy);
1444}
1445
1446static Instruction *
1447upgradeIntrinsicCall(const Type* RetTy, const ValID &ID,
1448 std::vector<Value*>& Args) {
1449
1450 std::string Name = ID.Type == ValID::NameVal ? ID.Name : "";
Reid Spencer41b213e2007-04-02 01:14:00 +00001451 switch (Name[5]) {
1452 case 'i':
1453 if (Name == "llvm.isunordered.f32" || Name == "llvm.isunordered.f64") {
1454 if (Args.size() != 2)
1455 error("Invalid prototype for " + Name);
1456 return new FCmpInst(FCmpInst::FCMP_UNO, Args[0], Args[1]);
1457 }
1458 break;
1459 case 'b':
1460 if (Name.length() == 14 && !memcmp(&Name[5], "bswap.i", 7)) {
1461 const Type* ArgTy = Args[0]->getType();
1462 Name += ".i" + utostr(cast<IntegerType>(ArgTy)->getBitWidth());
1463 Function *F = cast<Function>(
1464 CurModule.CurrentModule->getOrInsertFunction(Name, RetTy, ArgTy,
1465 (void*)0));
1466 return new CallInst(F, Args[0]);
1467 }
1468 break;
Reid Spencer8166a6c2007-04-02 02:08:35 +00001469 case 'c':
1470 if ((Name.length() <= 14 && !memcmp(&Name[5], "ctpop.i", 7)) ||
1471 (Name.length() <= 13 && !memcmp(&Name[5], "ctlz.i", 6)) ||
1472 (Name.length() <= 13 && !memcmp(&Name[5], "cttz.i", 6))) {
1473 // These intrinsics changed their result type.
1474 const Type* ArgTy = Args[0]->getType();
1475 Function *OldF = CurModule.CurrentModule->getFunction(Name);
1476 if (OldF)
1477 OldF->setName("upgrd.rm." + Name);
1478
1479 Function *NewF = cast<Function>(
1480 CurModule.CurrentModule->getOrInsertFunction(Name, Type::Int32Ty,
1481 ArgTy, (void*)0));
1482
1483 Instruction *Call = new CallInst(NewF, Args[0], "", CurBB);
1484 return CastInst::createIntegerCast(Call, RetTy, false);
1485 }
1486 break;
1487
Reid Spencer41b213e2007-04-02 01:14:00 +00001488 case 'v' : {
1489 const Type* PtrTy = PointerType::get(Type::Int8Ty);
1490 std::vector<const Type*> Params;
1491 if (Name == "llvm.va_start" || Name == "llvm.va_end") {
1492 if (Args.size() != 1)
1493 error("Invalid prototype for " + Name + " prototype");
1494 Params.push_back(PtrTy);
1495 const FunctionType *FTy =
1496 FunctionType::get(Type::VoidTy, Params, false);
1497 const PointerType *PFTy = PointerType::get(FTy);
1498 Value* Func = getVal(PFTy, ID);
1499 Args[0] = new BitCastInst(Args[0], PtrTy, makeNameUnique("va"), CurBB);
1500 return new CallInst(Func, &Args[0], Args.size());
1501 } else if (Name == "llvm.va_copy") {
1502 if (Args.size() != 2)
1503 error("Invalid prototype for " + Name + " prototype");
1504 Params.push_back(PtrTy);
1505 Params.push_back(PtrTy);
1506 const FunctionType *FTy =
1507 FunctionType::get(Type::VoidTy, Params, false);
1508 const PointerType *PFTy = PointerType::get(FTy);
1509 Value* Func = getVal(PFTy, ID);
1510 std::string InstName0(makeNameUnique("va0"));
1511 std::string InstName1(makeNameUnique("va1"));
1512 Args[0] = new BitCastInst(Args[0], PtrTy, InstName0, CurBB);
1513 Args[1] = new BitCastInst(Args[1], PtrTy, InstName1, CurBB);
1514 return new CallInst(Func, &Args[0], Args.size());
1515 }
Reid Spencer950bf602007-01-26 08:19:09 +00001516 }
1517 }
1518 return 0;
1519}
1520
1521const Type* upgradeGEPIndices(const Type* PTy,
1522 std::vector<ValueInfo> *Indices,
1523 std::vector<Value*> &VIndices,
1524 std::vector<Constant*> *CIndices = 0) {
1525 // Traverse the indices with a gep_type_iterator so we can build the list
1526 // of constant and value indices for use later. Also perform upgrades
1527 VIndices.clear();
1528 if (CIndices) CIndices->clear();
1529 for (unsigned i = 0, e = Indices->size(); i != e; ++i)
1530 VIndices.push_back((*Indices)[i].V);
1531 generic_gep_type_iterator<std::vector<Value*>::iterator>
1532 GTI = gep_type_begin(PTy, VIndices.begin(), VIndices.end()),
1533 GTE = gep_type_end(PTy, VIndices.begin(), VIndices.end());
1534 for (unsigned i = 0, e = Indices->size(); i != e && GTI != GTE; ++i, ++GTI) {
1535 Value *Index = VIndices[i];
1536 if (CIndices && !isa<Constant>(Index))
1537 error("Indices to constant getelementptr must be constants");
1538 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte
1539 // struct indices to i32 struct indices with ZExt for compatibility.
1540 else if (isa<StructType>(*GTI)) { // Only change struct indices
1541 if (ConstantInt *CUI = dyn_cast<ConstantInt>(Index))
1542 if (CUI->getType()->getBitWidth() == 8)
1543 Index =
1544 ConstantExpr::getCast(Instruction::ZExt, CUI, Type::Int32Ty);
1545 } else {
1546 // Make sure that unsigned SequentialType indices are zext'd to
1547 // 64-bits if they were smaller than that because LLVM 2.0 will sext
1548 // all indices for SequentialType elements. We must retain the same
1549 // semantic (zext) for unsigned types.
1550 if (const IntegerType *Ity = dyn_cast<IntegerType>(Index->getType()))
Reid Spencerbb1fd572007-03-21 17:15:50 +00001551 if (Ity->getBitWidth() < 64 && (*Indices)[i].S.isUnsigned()) {
Reid Spencer950bf602007-01-26 08:19:09 +00001552 if (CIndices)
1553 Index = ConstantExpr::getCast(Instruction::ZExt,
1554 cast<Constant>(Index), Type::Int64Ty);
1555 else
1556 Index = CastInst::create(Instruction::ZExt, Index, Type::Int64Ty,
Reid Spencer832254e2007-02-02 02:16:23 +00001557 makeNameUnique("gep"), CurBB);
Reid Spencer38f682b2007-01-26 20:31:18 +00001558 VIndices[i] = Index;
1559 }
Reid Spencer950bf602007-01-26 08:19:09 +00001560 }
1561 // Add to the CIndices list, if requested.
1562 if (CIndices)
1563 CIndices->push_back(cast<Constant>(Index));
1564 }
1565
1566 const Type *IdxTy =
Chris Lattner1bc3fa62007-02-12 22:58:38 +00001567 GetElementPtrInst::getIndexedType(PTy, &VIndices[0], VIndices.size(), true);
Reid Spencer950bf602007-01-26 08:19:09 +00001568 if (!IdxTy)
1569 error("Index list invalid for constant getelementptr");
1570 return IdxTy;
1571}
1572
Reid Spencerb7046c72007-01-29 05:41:34 +00001573unsigned upgradeCallingConv(unsigned CC) {
1574 switch (CC) {
1575 case OldCallingConv::C : return CallingConv::C;
1576 case OldCallingConv::CSRet : return CallingConv::C;
1577 case OldCallingConv::Fast : return CallingConv::Fast;
1578 case OldCallingConv::Cold : return CallingConv::Cold;
1579 case OldCallingConv::X86_StdCall : return CallingConv::X86_StdCall;
1580 case OldCallingConv::X86_FastCall: return CallingConv::X86_FastCall;
1581 default:
1582 return CC;
1583 }
1584}
1585
Reid Spencer950bf602007-01-26 08:19:09 +00001586Module* UpgradeAssembly(const std::string &infile, std::istream& in,
1587 bool debug, bool addAttrs)
Reid Spencere7c3c602006-11-30 06:36:44 +00001588{
1589 Upgradelineno = 1;
1590 CurFilename = infile;
Reid Spencer96839be2006-11-30 16:50:26 +00001591 LexInput = &in;
Reid Spencere77e35e2006-12-01 20:26:20 +00001592 yydebug = debug;
Reid Spencer71d2ec92006-12-31 06:02:26 +00001593 AddAttributes = addAttrs;
Reid Spencer950bf602007-01-26 08:19:09 +00001594 ObsoleteVarArgs = false;
1595 NewVarArgs = false;
Reid Spencere7c3c602006-11-30 06:36:44 +00001596
Reid Spencer950bf602007-01-26 08:19:09 +00001597 CurModule.CurrentModule = new Module(CurFilename);
1598
1599 // Check to make sure the parser succeeded
Reid Spencere7c3c602006-11-30 06:36:44 +00001600 if (yyparse()) {
Reid Spencer950bf602007-01-26 08:19:09 +00001601 if (ParserResult)
1602 delete ParserResult;
Reid Spencer30d0c582007-01-15 00:26:18 +00001603 std::cerr << "llvm-upgrade: parse failed.\n";
Reid Spencer30d0c582007-01-15 00:26:18 +00001604 return 0;
1605 }
1606
Reid Spencer950bf602007-01-26 08:19:09 +00001607 // Check to make sure that parsing produced a result
1608 if (!ParserResult) {
1609 std::cerr << "llvm-upgrade: no parse result.\n";
1610 return 0;
Reid Spencer30d0c582007-01-15 00:26:18 +00001611 }
1612
Reid Spencer950bf602007-01-26 08:19:09 +00001613 // Reset ParserResult variable while saving its value for the result.
1614 Module *Result = ParserResult;
1615 ParserResult = 0;
Reid Spencer30d0c582007-01-15 00:26:18 +00001616
Reid Spencer950bf602007-01-26 08:19:09 +00001617 //Not all functions use vaarg, so make a second check for ObsoleteVarArgs
Reid Spencer30d0c582007-01-15 00:26:18 +00001618 {
Reid Spencer950bf602007-01-26 08:19:09 +00001619 Function* F;
Reid Spencer688b0492007-02-05 21:19:13 +00001620 if ((F = Result->getFunction("llvm.va_start"))
Reid Spencer950bf602007-01-26 08:19:09 +00001621 && F->getFunctionType()->getNumParams() == 0)
1622 ObsoleteVarArgs = true;
Reid Spencer688b0492007-02-05 21:19:13 +00001623 if((F = Result->getFunction("llvm.va_copy"))
Reid Spencer950bf602007-01-26 08:19:09 +00001624 && F->getFunctionType()->getNumParams() == 1)
1625 ObsoleteVarArgs = true;
Reid Spencer280d8012006-12-01 23:40:53 +00001626 }
Reid Spencer319a7302007-01-05 17:20:02 +00001627
Reid Spencer950bf602007-01-26 08:19:09 +00001628 if (ObsoleteVarArgs && NewVarArgs) {
1629 error("This file is corrupt: it uses both new and old style varargs");
1630 return 0;
Reid Spencer319a7302007-01-05 17:20:02 +00001631 }
Reid Spencer319a7302007-01-05 17:20:02 +00001632
Reid Spencer950bf602007-01-26 08:19:09 +00001633 if(ObsoleteVarArgs) {
Reid Spencer688b0492007-02-05 21:19:13 +00001634 if(Function* F = Result->getFunction("llvm.va_start")) {
Reid Spencer950bf602007-01-26 08:19:09 +00001635 if (F->arg_size() != 0) {
1636 error("Obsolete va_start takes 0 argument");
Reid Spencer319a7302007-01-05 17:20:02 +00001637 return 0;
1638 }
Reid Spencer950bf602007-01-26 08:19:09 +00001639
1640 //foo = va_start()
1641 // ->
1642 //bar = alloca typeof(foo)
1643 //va_start(bar)
1644 //foo = load bar
Reid Spencer319a7302007-01-05 17:20:02 +00001645
Reid Spencer950bf602007-01-26 08:19:09 +00001646 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
1647 const Type* ArgTy = F->getFunctionType()->getReturnType();
1648 const Type* ArgTyPtr = PointerType::get(ArgTy);
1649 Function* NF = cast<Function>(Result->getOrInsertFunction(
1650 "llvm.va_start", RetTy, ArgTyPtr, (Type *)0));
1651
1652 while (!F->use_empty()) {
1653 CallInst* CI = cast<CallInst>(F->use_back());
1654 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
1655 new CallInst(NF, bar, "", CI);
1656 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
1657 CI->replaceAllUsesWith(foo);
1658 CI->getParent()->getInstList().erase(CI);
Reid Spencerf8383de2007-01-06 06:04:32 +00001659 }
Reid Spencer950bf602007-01-26 08:19:09 +00001660 Result->getFunctionList().erase(F);
Reid Spencerf8383de2007-01-06 06:04:32 +00001661 }
Reid Spencer950bf602007-01-26 08:19:09 +00001662
Reid Spencer688b0492007-02-05 21:19:13 +00001663 if(Function* F = Result->getFunction("llvm.va_end")) {
Reid Spencer950bf602007-01-26 08:19:09 +00001664 if(F->arg_size() != 1) {
1665 error("Obsolete va_end takes 1 argument");
1666 return 0;
Reid Spencerf8383de2007-01-06 06:04:32 +00001667 }
Reid Spencerf8383de2007-01-06 06:04:32 +00001668
Reid Spencer950bf602007-01-26 08:19:09 +00001669 //vaend foo
1670 // ->
1671 //bar = alloca 1 of typeof(foo)
1672 //vaend bar
1673 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
1674 const Type* ArgTy = F->getFunctionType()->getParamType(0);
1675 const Type* ArgTyPtr = PointerType::get(ArgTy);
1676 Function* NF = cast<Function>(Result->getOrInsertFunction(
1677 "llvm.va_end", RetTy, ArgTyPtr, (Type *)0));
Reid Spencerf8383de2007-01-06 06:04:32 +00001678
Reid Spencer950bf602007-01-26 08:19:09 +00001679 while (!F->use_empty()) {
1680 CallInst* CI = cast<CallInst>(F->use_back());
1681 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
1682 new StoreInst(CI->getOperand(1), bar, CI);
1683 new CallInst(NF, bar, "", CI);
1684 CI->getParent()->getInstList().erase(CI);
Reid Spencere77e35e2006-12-01 20:26:20 +00001685 }
Reid Spencer950bf602007-01-26 08:19:09 +00001686 Result->getFunctionList().erase(F);
Reid Spencere77e35e2006-12-01 20:26:20 +00001687 }
Reid Spencer950bf602007-01-26 08:19:09 +00001688
Reid Spencer688b0492007-02-05 21:19:13 +00001689 if(Function* F = Result->getFunction("llvm.va_copy")) {
Reid Spencer950bf602007-01-26 08:19:09 +00001690 if(F->arg_size() != 1) {
1691 error("Obsolete va_copy takes 1 argument");
1692 return 0;
Reid Spencere77e35e2006-12-01 20:26:20 +00001693 }
Reid Spencer950bf602007-01-26 08:19:09 +00001694 //foo = vacopy(bar)
1695 // ->
1696 //a = alloca 1 of typeof(foo)
1697 //b = alloca 1 of typeof(foo)
1698 //store bar -> b
1699 //vacopy(a, b)
1700 //foo = load a
1701
1702 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
1703 const Type* ArgTy = F->getFunctionType()->getReturnType();
1704 const Type* ArgTyPtr = PointerType::get(ArgTy);
1705 Function* NF = cast<Function>(Result->getOrInsertFunction(
1706 "llvm.va_copy", RetTy, ArgTyPtr, ArgTyPtr, (Type *)0));
Reid Spencere77e35e2006-12-01 20:26:20 +00001707
Reid Spencer950bf602007-01-26 08:19:09 +00001708 while (!F->use_empty()) {
1709 CallInst* CI = cast<CallInst>(F->use_back());
1710 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
1711 AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
1712 new StoreInst(CI->getOperand(1), b, CI);
1713 new CallInst(NF, a, b, "", CI);
1714 Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
1715 CI->replaceAllUsesWith(foo);
1716 CI->getParent()->getInstList().erase(CI);
1717 }
1718 Result->getFunctionList().erase(F);
Reid Spencer319a7302007-01-05 17:20:02 +00001719 }
1720 }
1721
Reid Spencer52402b02007-01-02 05:45:11 +00001722 return Result;
1723}
1724
Reid Spencer950bf602007-01-26 08:19:09 +00001725} // end llvm namespace
Reid Spencer319a7302007-01-05 17:20:02 +00001726
Reid Spencer950bf602007-01-26 08:19:09 +00001727using namespace llvm;
Reid Spencer30d0c582007-01-15 00:26:18 +00001728
Reid Spencere7c3c602006-11-30 06:36:44 +00001729%}
1730
Reid Spencere77e35e2006-12-01 20:26:20 +00001731%union {
Reid Spencer950bf602007-01-26 08:19:09 +00001732 llvm::Module *ModuleVal;
1733 llvm::Function *FunctionVal;
1734 std::pair<llvm::PATypeInfo, char*> *ArgVal;
1735 llvm::BasicBlock *BasicBlockVal;
Reid Spencerbb1fd572007-03-21 17:15:50 +00001736 llvm::TermInstInfo TermInstVal;
Reid Spencer950bf602007-01-26 08:19:09 +00001737 llvm::InstrInfo InstVal;
1738 llvm::ConstInfo ConstVal;
1739 llvm::ValueInfo ValueVal;
1740 llvm::PATypeInfo TypeVal;
1741 llvm::TypeInfo PrimType;
1742 llvm::PHIListInfo PHIList;
1743 std::list<llvm::PATypeInfo> *TypeList;
1744 std::vector<llvm::ValueInfo> *ValueList;
1745 std::vector<llvm::ConstInfo> *ConstVector;
1746
1747
1748 std::vector<std::pair<llvm::PATypeInfo,char*> > *ArgList;
1749 // Represent the RHS of PHI node
1750 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
1751
1752 llvm::GlobalValue::LinkageTypes Linkage;
1753 int64_t SInt64Val;
1754 uint64_t UInt64Val;
1755 int SIntVal;
1756 unsigned UIntVal;
1757 double FPVal;
1758 bool BoolVal;
1759
1760 char *StrVal; // This memory is strdup'd!
1761 llvm::ValID ValIDVal; // strdup'd memory maybe!
1762
1763 llvm::BinaryOps BinaryOpVal;
1764 llvm::TermOps TermOpVal;
1765 llvm::MemoryOps MemOpVal;
1766 llvm::OtherOps OtherOpVal;
1767 llvm::CastOps CastOpVal;
1768 llvm::ICmpInst::Predicate IPred;
1769 llvm::FCmpInst::Predicate FPred;
1770 llvm::Module::Endianness Endianness;
Reid Spencere77e35e2006-12-01 20:26:20 +00001771}
1772
Reid Spencer950bf602007-01-26 08:19:09 +00001773%type <ModuleVal> Module FunctionList
1774%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
1775%type <BasicBlockVal> BasicBlock InstructionList
1776%type <TermInstVal> BBTerminatorInst
1777%type <InstVal> Inst InstVal MemoryInst
1778%type <ConstVal> ConstVal ConstExpr
1779%type <ConstVector> ConstVector
1780%type <ArgList> ArgList ArgListH
1781%type <ArgVal> ArgVal
1782%type <PHIList> PHIList
1783%type <ValueList> ValueRefList ValueRefListE // For call param lists
1784%type <ValueList> IndexList // For GEP derived indices
1785%type <TypeList> TypeListI ArgTypeListI
1786%type <JumpTable> JumpTable
1787%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
1788%type <BoolVal> OptVolatile // 'volatile' or not
1789%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1790%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencered96d1e2007-02-08 09:08:52 +00001791%type <Linkage> OptLinkage FnDeclareLinkage
Reid Spencer950bf602007-01-26 08:19:09 +00001792%type <Endianness> BigOrLittle
Reid Spencere77e35e2006-12-01 20:26:20 +00001793
Reid Spencer950bf602007-01-26 08:19:09 +00001794// ValueRef - Unresolved reference to a definition or BB
1795%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1796%type <ValueVal> ResolvedVal // <type> <valref> pair
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001797
Reid Spencer950bf602007-01-26 08:19:09 +00001798// Tokens and types for handling constant integer values
1799//
1800// ESINT64VAL - A negative number within long long range
1801%token <SInt64Val> ESINT64VAL
Reid Spencere77e35e2006-12-01 20:26:20 +00001802
Reid Spencer950bf602007-01-26 08:19:09 +00001803// EUINT64VAL - A positive number within uns. long long range
1804%token <UInt64Val> EUINT64VAL
1805%type <SInt64Val> EINT64VAL
Reid Spencere77e35e2006-12-01 20:26:20 +00001806
Reid Spencer950bf602007-01-26 08:19:09 +00001807%token <SIntVal> SINTVAL // Signed 32 bit ints...
1808%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
1809%type <SIntVal> INTVAL
1810%token <FPVal> FPVAL // Float or Double constant
Reid Spencere77e35e2006-12-01 20:26:20 +00001811
Reid Spencer950bf602007-01-26 08:19:09 +00001812// Built in types...
1813%type <TypeVal> Types TypesV UpRTypes UpRTypesV
1814%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
1815%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
1816%token <PrimType> FLOAT DOUBLE TYPE LABEL
Reid Spencere77e35e2006-12-01 20:26:20 +00001817
Reid Spencer950bf602007-01-26 08:19:09 +00001818%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
1819%type <StrVal> Name OptName OptAssign
1820%type <UIntVal> OptAlign OptCAlign
1821%type <StrVal> OptSection SectionString
1822
1823%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
1824%token DECLARE GLOBAL CONSTANT SECTION VOLATILE
1825%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
1826%token DLLIMPORT DLLEXPORT EXTERN_WEAK
1827%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
1828%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
1829%token CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
1830%token X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
1831%token DATALAYOUT
1832%type <UIntVal> OptCallingConv
1833
1834// Basic Block Terminating Operators
1835%token <TermOpVal> RET BR SWITCH INVOKE UNREACHABLE
1836%token UNWIND EXCEPT
1837
1838// Binary Operators
1839%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Reid Spencer832254e2007-02-02 02:16:23 +00001840%type <BinaryOpVal> ShiftOps
Reid Spencer950bf602007-01-26 08:19:09 +00001841%token <BinaryOpVal> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM
Reid Spencer832254e2007-02-02 02:16:23 +00001842%token <BinaryOpVal> AND OR XOR SHL SHR ASHR LSHR
Reid Spencer950bf602007-01-26 08:19:09 +00001843%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
1844%token <OtherOpVal> ICMP FCMP
1845
1846// Memory Instructions
1847%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1848
1849// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001850%token <OtherOpVal> PHI_TOK SELECT VAARG
Reid Spencer950bf602007-01-26 08:19:09 +00001851%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
1852%token VAARG_old VANEXT_old //OBSOLETE
1853
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001854// Support for ICmp/FCmp Predicates, which is 1.9++ but not 2.0
Reid Spencer950bf602007-01-26 08:19:09 +00001855%type <IPred> IPredicates
1856%type <FPred> FPredicates
1857%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1858%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
1859
1860%token <CastOpVal> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI
1861%token <CastOpVal> UITOFP SITOFP PTRTOINT INTTOPTR BITCAST
1862%type <CastOpVal> CastOps
Reid Spencere7c3c602006-11-30 06:36:44 +00001863
1864%start Module
1865
1866%%
1867
1868// Handle constant integer size restriction and conversion...
Reid Spencer950bf602007-01-26 08:19:09 +00001869//
1870INTVAL
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001871 : SINTVAL
Reid Spencer950bf602007-01-26 08:19:09 +00001872 | UINTVAL {
1873 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
1874 error("Value too large for type");
1875 $$ = (int32_t)$1;
1876 }
1877 ;
1878
1879EINT64VAL
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001880 : ESINT64VAL // These have same type and can't cause problems...
Reid Spencer950bf602007-01-26 08:19:09 +00001881 | EUINT64VAL {
1882 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
1883 error("Value too large for type");
1884 $$ = (int64_t)$1;
1885 };
Reid Spencere7c3c602006-11-30 06:36:44 +00001886
1887// Operations that are notably excluded from this list include:
1888// RET, BR, & SWITCH because they end basic blocks and are treated specially.
Reid Spencer950bf602007-01-26 08:19:09 +00001889//
1890ArithmeticOps
1891 : ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV | REM | UREM | SREM | FREM
1892 ;
1893
1894LogicalOps
1895 : AND | OR | XOR
1896 ;
1897
1898SetCondOps
1899 : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
1900 ;
1901
1902IPredicates
1903 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
1904 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1905 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1906 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1907 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1908 ;
1909
1910FPredicates
1911 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1912 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1913 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1914 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1915 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1916 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1917 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1918 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1919 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1920 ;
1921ShiftOps
1922 : SHL | SHR | ASHR | LSHR
1923 ;
1924
1925CastOps
1926 : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI
1927 | UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
1928 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001929
1930// These are some types that allow classification if we only want a particular
1931// thing... for example, only a signed, unsigned, or integral type.
Reid Spencer950bf602007-01-26 08:19:09 +00001932SIntType
1933 : LONG | INT | SHORT | SBYTE
1934 ;
1935
1936UIntType
1937 : ULONG | UINT | USHORT | UBYTE
1938 ;
1939
1940IntType
1941 : SIntType | UIntType
1942 ;
1943
1944FPType
1945 : FLOAT | DOUBLE
1946 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001947
1948// OptAssign - Value producing statements have an optional assignment component
Reid Spencer950bf602007-01-26 08:19:09 +00001949OptAssign
1950 : Name '=' {
Reid Spencere7c3c602006-11-30 06:36:44 +00001951 $$ = $1;
1952 }
1953 | /*empty*/ {
Reid Spencer950bf602007-01-26 08:19:09 +00001954 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001955 };
1956
1957OptLinkage
Reid Spencer785a5ae2007-02-08 00:21:40 +00001958 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencer950bf602007-01-26 08:19:09 +00001959 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1960 | WEAK { $$ = GlobalValue::WeakLinkage; }
1961 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1962 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1963 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencer785a5ae2007-02-08 00:21:40 +00001964 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencer950bf602007-01-26 08:19:09 +00001965 | /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1966 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001967
1968OptCallingConv
Reid Spencered96d1e2007-02-08 09:08:52 +00001969 : /*empty*/ { $$ = OldCallingConv::C; }
1970 | CCC_TOK { $$ = OldCallingConv::C; }
1971 | CSRETCC_TOK { $$ = OldCallingConv::CSRet; }
1972 | FASTCC_TOK { $$ = OldCallingConv::Fast; }
1973 | COLDCC_TOK { $$ = OldCallingConv::Cold; }
1974 | X86_STDCALLCC_TOK { $$ = OldCallingConv::X86_StdCall; }
1975 | X86_FASTCALLCC_TOK { $$ = OldCallingConv::X86_FastCall; }
Reid Spencer950bf602007-01-26 08:19:09 +00001976 | CC_TOK EUINT64VAL {
1977 if ((unsigned)$2 != $2)
1978 error("Calling conv too large");
1979 $$ = $2;
1980 }
1981 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001982
1983// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1984// a comma before it.
1985OptAlign
Reid Spencer950bf602007-01-26 08:19:09 +00001986 : /*empty*/ { $$ = 0; }
1987 | ALIGN EUINT64VAL {
1988 $$ = $2;
1989 if ($$ != 0 && !isPowerOf2_32($$))
1990 error("Alignment must be a power of two");
1991 }
1992 ;
Reid Spencerf0cf1322006-12-07 04:23:03 +00001993
Reid Spencere7c3c602006-11-30 06:36:44 +00001994OptCAlign
Reid Spencer950bf602007-01-26 08:19:09 +00001995 : /*empty*/ { $$ = 0; }
1996 | ',' ALIGN EUINT64VAL {
1997 $$ = $3;
1998 if ($$ != 0 && !isPowerOf2_32($$))
1999 error("Alignment must be a power of two");
2000 }
2001 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002002
2003SectionString
Reid Spencer950bf602007-01-26 08:19:09 +00002004 : SECTION STRINGCONSTANT {
2005 for (unsigned i = 0, e = strlen($2); i != e; ++i)
2006 if ($2[i] == '"' || $2[i] == '\\')
2007 error("Invalid character in section name");
2008 $$ = $2;
2009 }
2010 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002011
Reid Spencer950bf602007-01-26 08:19:09 +00002012OptSection
2013 : /*empty*/ { $$ = 0; }
2014 | SectionString { $$ = $1; }
2015 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002016
Reid Spencer950bf602007-01-26 08:19:09 +00002017// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
2018// is set to be the global we are processing.
2019//
Reid Spencere7c3c602006-11-30 06:36:44 +00002020GlobalVarAttributes
Reid Spencer950bf602007-01-26 08:19:09 +00002021 : /* empty */ {}
2022 | ',' GlobalVarAttribute GlobalVarAttributes {}
2023 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002024
Reid Spencer950bf602007-01-26 08:19:09 +00002025GlobalVarAttribute
2026 : SectionString {
2027 CurGV->setSection($1);
2028 free($1);
2029 }
2030 | ALIGN EUINT64VAL {
2031 if ($2 != 0 && !isPowerOf2_32($2))
2032 error("Alignment must be a power of two");
2033 CurGV->setAlignment($2);
2034
2035 }
2036 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002037
2038//===----------------------------------------------------------------------===//
2039// Types includes all predefined types... except void, because it can only be
2040// used in specific contexts (function returning void for example). To have
2041// access to it, a user must explicitly use TypesV.
2042//
2043
2044// TypesV includes all of 'Types', but it also includes the void type.
Reid Spencer950bf602007-01-26 08:19:09 +00002045TypesV
2046 : Types
2047 | VOID {
Reid Spencered96d1e2007-02-08 09:08:52 +00002048 $$.PAT = new PATypeHolder($1.T);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002049 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002050 }
2051 ;
2052
2053UpRTypesV
2054 : UpRTypes
2055 | VOID {
Reid Spencered96d1e2007-02-08 09:08:52 +00002056 $$.PAT = new PATypeHolder($1.T);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002057 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002058 }
2059 ;
2060
2061Types
2062 : UpRTypes {
2063 if (!UpRefs.empty())
Reid Spencered96d1e2007-02-08 09:08:52 +00002064 error("Invalid upreference in type: " + (*$1.PAT)->getDescription());
Reid Spencer950bf602007-01-26 08:19:09 +00002065 $$ = $1;
2066 }
2067 ;
2068
2069PrimType
2070 : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
2071 | LONG | ULONG | FLOAT | DOUBLE | LABEL
2072 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002073
2074// Derived types are added later...
Reid Spencera50d5962006-12-02 04:11:07 +00002075UpRTypes
Reid Spencer950bf602007-01-26 08:19:09 +00002076 : PrimType {
Reid Spencered96d1e2007-02-08 09:08:52 +00002077 $$.PAT = new PATypeHolder($1.T);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002078 $$.S.copy($1.S);
Reid Spencera50d5962006-12-02 04:11:07 +00002079 }
Reid Spencer950bf602007-01-26 08:19:09 +00002080 | OPAQUE {
Reid Spencered96d1e2007-02-08 09:08:52 +00002081 $$.PAT = new PATypeHolder(OpaqueType::get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002082 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002083 }
2084 | SymbolicValueRef { // Named types are also simple types...
Reid Spencerbb1fd572007-03-21 17:15:50 +00002085 $$.S.copy(getTypeSign($1));
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00002086 const Type* tmp = getType($1);
Reid Spencered96d1e2007-02-08 09:08:52 +00002087 $$.PAT = new PATypeHolder(tmp);
Reid Spencer78720742006-12-02 20:21:22 +00002088 }
2089 | '\\' EUINT64VAL { // Type UpReference
Reid Spencer950bf602007-01-26 08:19:09 +00002090 if ($2 > (uint64_t)~0U)
2091 error("Value out of range");
2092 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
2093 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencered96d1e2007-02-08 09:08:52 +00002094 $$.PAT = new PATypeHolder(OT);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002095 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002096 UR_OUT("New Upreference!\n");
Reid Spencere7c3c602006-11-30 06:36:44 +00002097 }
2098 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Reid Spencerbb1fd572007-03-21 17:15:50 +00002099 $$.S.makeComposite($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002100 std::vector<const Type*> Params;
2101 for (std::list<llvm::PATypeInfo>::iterator I = $3->begin(),
2102 E = $3->end(); I != E; ++I) {
Reid Spencered96d1e2007-02-08 09:08:52 +00002103 Params.push_back(I->PAT->get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002104 $$.S.add(I->S);
Reid Spencer52402b02007-01-02 05:45:11 +00002105 }
Reid Spencerb7046c72007-01-29 05:41:34 +00002106 FunctionType::ParamAttrsList ParamAttrs;
Reid Spencer950bf602007-01-26 08:19:09 +00002107 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
2108 if (isVarArg) Params.pop_back();
2109
Reid Spencered96d1e2007-02-08 09:08:52 +00002110 $$.PAT = new PATypeHolder(
2111 HandleUpRefs(FunctionType::get($1.PAT->get(), Params, isVarArg,
Reid Spencerbb1fd572007-03-21 17:15:50 +00002112 ParamAttrs), $$.S));
2113 delete $1.PAT; // Delete the return type handle
Reid Spencer950bf602007-01-26 08:19:09 +00002114 delete $3; // Delete the argument list
Reid Spencere7c3c602006-11-30 06:36:44 +00002115 }
2116 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencerbb1fd572007-03-21 17:15:50 +00002117 $$.S.makeComposite($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002118 $$.PAT = new PATypeHolder(HandleUpRefs(ArrayType::get($4.PAT->get(),
Reid Spencerbb1fd572007-03-21 17:15:50 +00002119 (unsigned)$2), $$.S));
Reid Spencered96d1e2007-02-08 09:08:52 +00002120 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002121 }
Chris Lattner4227bdb2007-02-19 07:34:02 +00002122 | '<' EUINT64VAL 'x' UpRTypes '>' { // Vector type?
Reid Spencerbb1fd572007-03-21 17:15:50 +00002123 const llvm::Type* ElemTy = $4.PAT->get();
2124 if ((unsigned)$2 != $2)
2125 error("Unsigned result not equal to signed result");
2126 if (!(ElemTy->isInteger() || ElemTy->isFloatingPoint()))
2127 error("Elements of a VectorType must be integer or floating point");
2128 if (!isPowerOf2_32($2))
2129 error("VectorType length should be a power of 2");
2130 $$.S.makeComposite($4.S);
2131 $$.PAT = new PATypeHolder(HandleUpRefs(VectorType::get(ElemTy,
2132 (unsigned)$2), $$.S));
2133 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002134 }
2135 | '{' TypeListI '}' { // Structure type?
Reid Spencer950bf602007-01-26 08:19:09 +00002136 std::vector<const Type*> Elements;
Reid Spencerbb1fd572007-03-21 17:15:50 +00002137 $$.S.makeComposite();
Reid Spencer950bf602007-01-26 08:19:09 +00002138 for (std::list<llvm::PATypeInfo>::iterator I = $2->begin(),
Reid Spencerbb1fd572007-03-21 17:15:50 +00002139 E = $2->end(); I != E; ++I) {
Reid Spencered96d1e2007-02-08 09:08:52 +00002140 Elements.push_back(I->PAT->get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002141 $$.S.add(I->S);
2142 }
2143 $$.PAT = new PATypeHolder(HandleUpRefs(StructType::get(Elements), $$.S));
Reid Spencer950bf602007-01-26 08:19:09 +00002144 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00002145 }
2146 | '{' '}' { // Empty structure type?
Reid Spencered96d1e2007-02-08 09:08:52 +00002147 $$.PAT = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencerbb1fd572007-03-21 17:15:50 +00002148 $$.S.makeComposite();
Reid Spencere7c3c602006-11-30 06:36:44 +00002149 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002150 | '<' '{' TypeListI '}' '>' { // Packed Structure type?
Reid Spencerbb1fd572007-03-21 17:15:50 +00002151 $$.S.makeComposite();
Reid Spencer950bf602007-01-26 08:19:09 +00002152 std::vector<const Type*> Elements;
2153 for (std::list<llvm::PATypeInfo>::iterator I = $3->begin(),
2154 E = $3->end(); I != E; ++I) {
Reid Spencered96d1e2007-02-08 09:08:52 +00002155 Elements.push_back(I->PAT->get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002156 $$.S.add(I->S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002157 delete I->PAT;
Reid Spencer52402b02007-01-02 05:45:11 +00002158 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00002159 $$.PAT = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true),
2160 $$.S));
Reid Spencer950bf602007-01-26 08:19:09 +00002161 delete $3;
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002162 }
2163 | '<' '{' '}' '>' { // Empty packed structure type?
Reid Spencered96d1e2007-02-08 09:08:52 +00002164 $$.PAT = new PATypeHolder(StructType::get(std::vector<const Type*>(),true));
Reid Spencerbb1fd572007-03-21 17:15:50 +00002165 $$.S.makeComposite();
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002166 }
Reid Spencere7c3c602006-11-30 06:36:44 +00002167 | UpRTypes '*' { // Pointer type?
Reid Spencered96d1e2007-02-08 09:08:52 +00002168 if ($1.PAT->get() == Type::LabelTy)
Reid Spencer950bf602007-01-26 08:19:09 +00002169 error("Cannot form a pointer to a basic block");
Reid Spencerbb1fd572007-03-21 17:15:50 +00002170 $$.S.makeComposite($1.S);
2171 $$.PAT = new PATypeHolder(HandleUpRefs(PointerType::get($1.PAT->get()),
2172 $$.S));
Reid Spencered96d1e2007-02-08 09:08:52 +00002173 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002174 }
2175 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002176
2177// TypeList - Used for struct declarations and as a basis for function type
2178// declaration type lists
2179//
Reid Spencere77e35e2006-12-01 20:26:20 +00002180TypeListI
2181 : UpRTypes {
Reid Spencer950bf602007-01-26 08:19:09 +00002182 $$ = new std::list<PATypeInfo>();
2183 $$->push_back($1);
Reid Spencere77e35e2006-12-01 20:26:20 +00002184 }
2185 | TypeListI ',' UpRTypes {
Reid Spencer950bf602007-01-26 08:19:09 +00002186 ($$=$1)->push_back($3);
2187 }
2188 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002189
2190// ArgTypeList - List of types for a function type declaration...
Reid Spencere77e35e2006-12-01 20:26:20 +00002191ArgTypeListI
Reid Spencer950bf602007-01-26 08:19:09 +00002192 : TypeListI
Reid Spencere7c3c602006-11-30 06:36:44 +00002193 | TypeListI ',' DOTDOTDOT {
Reid Spencer950bf602007-01-26 08:19:09 +00002194 PATypeInfo VoidTI;
Reid Spencered96d1e2007-02-08 09:08:52 +00002195 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002196 VoidTI.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002197 ($$=$1)->push_back(VoidTI);
Reid Spencere7c3c602006-11-30 06:36:44 +00002198 }
2199 | DOTDOTDOT {
Reid Spencer950bf602007-01-26 08:19:09 +00002200 $$ = new std::list<PATypeInfo>();
2201 PATypeInfo VoidTI;
Reid Spencered96d1e2007-02-08 09:08:52 +00002202 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002203 VoidTI.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002204 $$->push_back(VoidTI);
Reid Spencere7c3c602006-11-30 06:36:44 +00002205 }
2206 | /*empty*/ {
Reid Spencer950bf602007-01-26 08:19:09 +00002207 $$ = new std::list<PATypeInfo>();
2208 }
2209 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002210
2211// ConstVal - The various declarations that go into the constant pool. This
2212// production is used ONLY to represent constants that show up AFTER a 'const',
2213// 'constant' or 'global' token at global scope. Constants that can be inlined
2214// into other expressions (such as integers and constexprs) are handled by the
2215// ResolvedVal, ValueRef and ConstValueRef productions.
2216//
Reid Spencer950bf602007-01-26 08:19:09 +00002217ConstVal
2218 : Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencered96d1e2007-02-08 09:08:52 +00002219 const ArrayType *ATy = dyn_cast<ArrayType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002220 if (ATy == 0)
2221 error("Cannot make array constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002222 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002223 const Type *ETy = ATy->getElementType();
2224 int NumElements = ATy->getNumElements();
2225
2226 // Verify that we have the correct size...
2227 if (NumElements != -1 && NumElements != (int)$3->size())
2228 error("Type mismatch: constant sized array initialized with " +
2229 utostr($3->size()) + " arguments, but has size of " +
2230 itostr(NumElements) + "");
2231
2232 // Verify all elements are correct type!
2233 std::vector<Constant*> Elems;
2234 for (unsigned i = 0; i < $3->size(); i++) {
2235 Constant *C = (*$3)[i].C;
2236 const Type* ValTy = C->getType();
2237 if (ETy != ValTy)
2238 error("Element #" + utostr(i) + " is not of type '" +
2239 ETy->getDescription() +"' as required!\nIt is of type '"+
2240 ValTy->getDescription() + "'");
2241 Elems.push_back(C);
2242 }
2243 $$.C = ConstantArray::get(ATy, Elems);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002244 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002245 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002246 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002247 }
2248 | Types '[' ']' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002249 const ArrayType *ATy = dyn_cast<ArrayType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002250 if (ATy == 0)
2251 error("Cannot make array constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002252 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002253 int NumElements = ATy->getNumElements();
2254 if (NumElements != -1 && NumElements != 0)
2255 error("Type mismatch: constant sized array initialized with 0"
2256 " arguments, but has size of " + itostr(NumElements) +"");
2257 $$.C = ConstantArray::get(ATy, std::vector<Constant*>());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002258 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002259 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002260 }
2261 | Types 'c' STRINGCONSTANT {
Reid Spencered96d1e2007-02-08 09:08:52 +00002262 const ArrayType *ATy = dyn_cast<ArrayType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002263 if (ATy == 0)
2264 error("Cannot make array constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002265 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002266 int NumElements = ATy->getNumElements();
2267 const Type *ETy = dyn_cast<IntegerType>(ATy->getElementType());
2268 if (!ETy || cast<IntegerType>(ETy)->getBitWidth() != 8)
2269 error("String arrays require type i8, not '" + ETy->getDescription() +
2270 "'");
2271 char *EndStr = UnEscapeLexed($3, true);
2272 if (NumElements != -1 && NumElements != (EndStr-$3))
2273 error("Can't build string constant of size " +
2274 itostr((int)(EndStr-$3)) + " when array has size " +
2275 itostr(NumElements) + "");
2276 std::vector<Constant*> Vals;
2277 for (char *C = (char *)$3; C != (char *)EndStr; ++C)
2278 Vals.push_back(ConstantInt::get(ETy, *C));
2279 free($3);
2280 $$.C = ConstantArray::get(ATy, Vals);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002281 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002282 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002283 }
2284 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer9d6565a2007-02-15 02:26:10 +00002285 const VectorType *PTy = dyn_cast<VectorType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002286 if (PTy == 0)
2287 error("Cannot make packed constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002288 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002289 const Type *ETy = PTy->getElementType();
2290 int NumElements = PTy->getNumElements();
2291 // Verify that we have the correct size...
2292 if (NumElements != -1 && NumElements != (int)$3->size())
2293 error("Type mismatch: constant sized packed initialized with " +
2294 utostr($3->size()) + " arguments, but has size of " +
2295 itostr(NumElements) + "");
2296 // Verify all elements are correct type!
2297 std::vector<Constant*> Elems;
2298 for (unsigned i = 0; i < $3->size(); i++) {
2299 Constant *C = (*$3)[i].C;
2300 const Type* ValTy = C->getType();
2301 if (ETy != ValTy)
2302 error("Element #" + utostr(i) + " is not of type '" +
2303 ETy->getDescription() +"' as required!\nIt is of type '"+
2304 ValTy->getDescription() + "'");
2305 Elems.push_back(C);
2306 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00002307 $$.C = ConstantVector::get(PTy, Elems);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002308 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002309 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002310 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002311 }
2312 | Types '{' ConstVector '}' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002313 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002314 if (STy == 0)
2315 error("Cannot make struct constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002316 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002317 if ($3->size() != STy->getNumContainedTypes())
2318 error("Illegal number of initializers for structure type");
2319
2320 // Check to ensure that constants are compatible with the type initializer!
2321 std::vector<Constant*> Fields;
2322 for (unsigned i = 0, e = $3->size(); i != e; ++i) {
2323 Constant *C = (*$3)[i].C;
2324 if (C->getType() != STy->getElementType(i))
2325 error("Expected type '" + STy->getElementType(i)->getDescription() +
2326 "' for element #" + utostr(i) + " of structure initializer");
2327 Fields.push_back(C);
2328 }
2329 $$.C = ConstantStruct::get(STy, Fields);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002330 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002331 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002332 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002333 }
2334 | Types '{' '}' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002335 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002336 if (STy == 0)
2337 error("Cannot make struct constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002338 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002339 if (STy->getNumContainedTypes() != 0)
2340 error("Illegal number of initializers for structure type");
2341 $$.C = ConstantStruct::get(STy, std::vector<Constant*>());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002342 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002343 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002344 }
Reid Spencer950bf602007-01-26 08:19:09 +00002345 | Types '<' '{' ConstVector '}' '>' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002346 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002347 if (STy == 0)
2348 error("Cannot make packed struct constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002349 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002350 if ($4->size() != STy->getNumContainedTypes())
2351 error("Illegal number of initializers for packed structure type");
Reid Spencere7c3c602006-11-30 06:36:44 +00002352
Reid Spencer950bf602007-01-26 08:19:09 +00002353 // Check to ensure that constants are compatible with the type initializer!
2354 std::vector<Constant*> Fields;
2355 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
2356 Constant *C = (*$4)[i].C;
2357 if (C->getType() != STy->getElementType(i))
2358 error("Expected type '" + STy->getElementType(i)->getDescription() +
2359 "' for element #" + utostr(i) + " of packed struct initializer");
2360 Fields.push_back(C);
Reid Spencer280d8012006-12-01 23:40:53 +00002361 }
Reid Spencer950bf602007-01-26 08:19:09 +00002362 $$.C = ConstantStruct::get(STy, Fields);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002363 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002364 delete $1.PAT;
Reid Spencere77e35e2006-12-01 20:26:20 +00002365 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00002366 }
Reid Spencer950bf602007-01-26 08:19:09 +00002367 | Types '<' '{' '}' '>' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002368 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002369 if (STy == 0)
2370 error("Cannot make packed struct constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002371 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002372 if (STy->getNumContainedTypes() != 0)
2373 error("Illegal number of initializers for packed structure type");
2374 $$.C = ConstantStruct::get(STy, std::vector<Constant*>());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002375 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002376 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002377 }
2378 | Types NULL_TOK {
Reid Spencered96d1e2007-02-08 09:08:52 +00002379 const PointerType *PTy = dyn_cast<PointerType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002380 if (PTy == 0)
2381 error("Cannot make null pointer constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002382 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002383 $$.C = ConstantPointerNull::get(PTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002384 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002385 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002386 }
2387 | Types UNDEF {
Reid Spencered96d1e2007-02-08 09:08:52 +00002388 $$.C = UndefValue::get($1.PAT->get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002389 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002390 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002391 }
2392 | Types SymbolicValueRef {
Reid Spencered96d1e2007-02-08 09:08:52 +00002393 const PointerType *Ty = dyn_cast<PointerType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002394 if (Ty == 0)
2395 error("Global const reference must be a pointer type, not" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002396 $1.PAT->get()->getDescription());
Reid Spencer950bf602007-01-26 08:19:09 +00002397
2398 // ConstExprs can exist in the body of a function, thus creating
2399 // GlobalValues whenever they refer to a variable. Because we are in
2400 // the context of a function, getExistingValue will search the functions
2401 // symbol table instead of the module symbol table for the global symbol,
2402 // which throws things all off. To get around this, we just tell
2403 // getExistingValue that we are at global scope here.
2404 //
2405 Function *SavedCurFn = CurFun.CurrentFunction;
2406 CurFun.CurrentFunction = 0;
Reid Spencerbb1fd572007-03-21 17:15:50 +00002407 $2.S.copy($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002408 Value *V = getExistingValue(Ty, $2);
2409 CurFun.CurrentFunction = SavedCurFn;
2410
2411 // If this is an initializer for a constant pointer, which is referencing a
2412 // (currently) undefined variable, create a stub now that shall be replaced
2413 // in the future with the right type of variable.
2414 //
2415 if (V == 0) {
2416 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers");
2417 const PointerType *PT = cast<PointerType>(Ty);
2418
2419 // First check to see if the forward references value is already created!
2420 PerModuleInfo::GlobalRefsType::iterator I =
2421 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
2422
2423 if (I != CurModule.GlobalRefs.end()) {
2424 V = I->second; // Placeholder already exists, use it...
2425 $2.destroy();
2426 } else {
2427 std::string Name;
2428 if ($2.Type == ValID::NameVal) Name = $2.Name;
2429
2430 // Create the forward referenced global.
2431 GlobalValue *GV;
2432 if (const FunctionType *FTy =
2433 dyn_cast<FunctionType>(PT->getElementType())) {
2434 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
2435 CurModule.CurrentModule);
2436 } else {
2437 GV = new GlobalVariable(PT->getElementType(), false,
2438 GlobalValue::ExternalLinkage, 0,
2439 Name, CurModule.CurrentModule);
2440 }
2441
2442 // Keep track of the fact that we have a forward ref to recycle it
2443 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
2444 V = GV;
2445 }
2446 }
2447 $$.C = cast<GlobalValue>(V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002448 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002449 delete $1.PAT; // Free the type handle
Reid Spencer950bf602007-01-26 08:19:09 +00002450 }
2451 | Types ConstExpr {
Reid Spencered96d1e2007-02-08 09:08:52 +00002452 if ($1.PAT->get() != $2.C->getType())
Reid Spencer950bf602007-01-26 08:19:09 +00002453 error("Mismatched types for constant expression");
2454 $$ = $2;
Reid Spencerbb1fd572007-03-21 17:15:50 +00002455 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002456 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002457 }
2458 | Types ZEROINITIALIZER {
Reid Spencered96d1e2007-02-08 09:08:52 +00002459 const Type *Ty = $1.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00002460 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
2461 error("Cannot create a null initialized value of this type");
2462 $$.C = Constant::getNullValue(Ty);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002463 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002464 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002465 }
2466 | SIntType EINT64VAL { // integral constants
2467 const Type *Ty = $1.T;
2468 if (!ConstantInt::isValueValidForType(Ty, $2))
2469 error("Constant value doesn't fit in type");
2470 $$.C = ConstantInt::get(Ty, $2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002471 $$.S.makeSigned();
Reid Spencer950bf602007-01-26 08:19:09 +00002472 }
2473 | UIntType EUINT64VAL { // integral constants
2474 const Type *Ty = $1.T;
2475 if (!ConstantInt::isValueValidForType(Ty, $2))
2476 error("Constant value doesn't fit in type");
2477 $$.C = ConstantInt::get(Ty, $2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002478 $$.S.makeUnsigned();
Reid Spencer950bf602007-01-26 08:19:09 +00002479 }
2480 | BOOL TRUETOK { // Boolean constants
2481 $$.C = ConstantInt::get(Type::Int1Ty, true);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002482 $$.S.makeUnsigned();
Reid Spencer950bf602007-01-26 08:19:09 +00002483 }
2484 | BOOL FALSETOK { // Boolean constants
2485 $$.C = ConstantInt::get(Type::Int1Ty, false);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002486 $$.S.makeUnsigned();
Reid Spencer950bf602007-01-26 08:19:09 +00002487 }
2488 | FPType FPVAL { // Float & Double constants
2489 if (!ConstantFP::isValueValidForType($1.T, $2))
2490 error("Floating point constant invalid for type");
2491 $$.C = ConstantFP::get($1.T, $2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002492 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002493 }
2494 ;
2495
2496ConstExpr
2497 : CastOps '(' ConstVal TO Types ')' {
2498 const Type* SrcTy = $3.C->getType();
Reid Spencered96d1e2007-02-08 09:08:52 +00002499 const Type* DstTy = $5.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00002500 Signedness SrcSign($3.S);
2501 Signedness DstSign($5.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002502 if (!SrcTy->isFirstClassType())
2503 error("cast constant expression from a non-primitive type: '" +
2504 SrcTy->getDescription() + "'");
2505 if (!DstTy->isFirstClassType())
2506 error("cast constant expression to a non-primitive type: '" +
2507 DstTy->getDescription() + "'");
2508 $$.C = cast<Constant>(getCast($1, $3.C, SrcSign, DstTy, DstSign));
Reid Spencerbb1fd572007-03-21 17:15:50 +00002509 $$.S.copy(DstSign);
Reid Spencered96d1e2007-02-08 09:08:52 +00002510 delete $5.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002511 }
2512 | GETELEMENTPTR '(' ConstVal IndexList ')' {
2513 const Type *Ty = $3.C->getType();
2514 if (!isa<PointerType>(Ty))
2515 error("GetElementPtr requires a pointer operand");
2516
2517 std::vector<Value*> VIndices;
2518 std::vector<Constant*> CIndices;
2519 upgradeGEPIndices($3.C->getType(), $4, VIndices, &CIndices);
2520
2521 delete $4;
Chris Lattner4227bdb2007-02-19 07:34:02 +00002522 $$.C = ConstantExpr::getGetElementPtr($3.C, &CIndices[0], CIndices.size());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002523 $$.S.copy(getElementSign($3, CIndices));
Reid Spencer950bf602007-01-26 08:19:09 +00002524 }
Reid Spencere7c3c602006-11-30 06:36:44 +00002525 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002526 if (!$3.C->getType()->isInteger() ||
2527 cast<IntegerType>($3.C->getType())->getBitWidth() != 1)
2528 error("Select condition must be bool type");
2529 if ($5.C->getType() != $7.C->getType())
2530 error("Select operand types must match");
2531 $$.C = ConstantExpr::getSelect($3.C, $5.C, $7.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002532 $$.S.copy($5.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002533 }
2534 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002535 const Type *Ty = $3.C->getType();
2536 if (Ty != $5.C->getType())
2537 error("Binary operator types must match");
2538 // First, make sure we're dealing with the right opcode by upgrading from
2539 // obsolete versions.
2540 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $3.S);
2541
2542 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
2543 // To retain backward compatibility with these early compilers, we emit a
2544 // cast to the appropriate integer type automatically if we are in the
2545 // broken case. See PR424 for more information.
2546 if (!isa<PointerType>(Ty)) {
2547 $$.C = ConstantExpr::get(Opcode, $3.C, $5.C);
2548 } else {
2549 const Type *IntPtrTy = 0;
2550 switch (CurModule.CurrentModule->getPointerSize()) {
2551 case Module::Pointer32: IntPtrTy = Type::Int32Ty; break;
2552 case Module::Pointer64: IntPtrTy = Type::Int64Ty; break;
2553 default: error("invalid pointer binary constant expr");
2554 }
2555 $$.C = ConstantExpr::get(Opcode,
2556 ConstantExpr::getCast(Instruction::PtrToInt, $3.C, IntPtrTy),
2557 ConstantExpr::getCast(Instruction::PtrToInt, $5.C, IntPtrTy));
2558 $$.C = ConstantExpr::getCast(Instruction::IntToPtr, $$.C, Ty);
2559 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00002560 $$.S.copy($3.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002561 }
2562 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002563 const Type* Ty = $3.C->getType();
2564 if (Ty != $5.C->getType())
2565 error("Logical operator types must match");
2566 if (!Ty->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00002567 if (!isa<VectorType>(Ty) ||
2568 !cast<VectorType>(Ty)->getElementType()->isInteger())
Reid Spencer950bf602007-01-26 08:19:09 +00002569 error("Logical operator requires integer operands");
2570 }
2571 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $3.S);
2572 $$.C = ConstantExpr::get(Opcode, $3.C, $5.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002573 $$.S.copy($3.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002574 }
2575 | SetCondOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002576 const Type* Ty = $3.C->getType();
2577 if (Ty != $5.C->getType())
2578 error("setcc operand types must match");
2579 unsigned short pred;
2580 Instruction::OtherOps Opcode = getCompareOp($1, pred, Ty, $3.S);
2581 $$.C = ConstantExpr::getCompare(Opcode, $3.C, $5.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002582 $$.S.makeUnsigned();
Reid Spencere7c3c602006-11-30 06:36:44 +00002583 }
Reid Spencer57f28f92006-12-03 07:10:26 +00002584 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002585 if ($4.C->getType() != $6.C->getType())
2586 error("icmp operand types must match");
2587 $$.C = ConstantExpr::getCompare($2, $4.C, $6.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002588 $$.S.makeUnsigned();
Reid Spencer57f28f92006-12-03 07:10:26 +00002589 }
2590 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002591 if ($4.C->getType() != $6.C->getType())
2592 error("fcmp operand types must match");
2593 $$.C = ConstantExpr::getCompare($2, $4.C, $6.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002594 $$.S.makeUnsigned();
Reid Spencer229e9362006-12-02 22:14:11 +00002595 }
Reid Spencere7c3c602006-11-30 06:36:44 +00002596 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002597 if (!$5.C->getType()->isInteger() ||
2598 cast<IntegerType>($5.C->getType())->getBitWidth() != 8)
2599 error("Shift count for shift constant must be unsigned byte");
Reid Spencer832254e2007-02-02 02:16:23 +00002600 const Type* Ty = $3.C->getType();
Reid Spencer950bf602007-01-26 08:19:09 +00002601 if (!$3.C->getType()->isInteger())
2602 error("Shift constant expression requires integer operand");
Reid Spencer832254e2007-02-02 02:16:23 +00002603 Constant *ShiftAmt = ConstantExpr::getZExt($5.C, Ty);
2604 $$.C = ConstantExpr::get(getBinaryOp($1, Ty, $3.S), $3.C, ShiftAmt);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002605 $$.S.copy($3.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002606 }
2607 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002608 if (!ExtractElementInst::isValidOperands($3.C, $5.C))
2609 error("Invalid extractelement operands");
2610 $$.C = ConstantExpr::getExtractElement($3.C, $5.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002611 $$.S.copy($3.S.get(0));
Reid Spencere7c3c602006-11-30 06:36:44 +00002612 }
2613 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002614 if (!InsertElementInst::isValidOperands($3.C, $5.C, $7.C))
2615 error("Invalid insertelement operands");
2616 $$.C = ConstantExpr::getInsertElement($3.C, $5.C, $7.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002617 $$.S.copy($3.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002618 }
2619 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002620 if (!ShuffleVectorInst::isValidOperands($3.C, $5.C, $7.C))
2621 error("Invalid shufflevector operands");
2622 $$.C = ConstantExpr::getShuffleVector($3.C, $5.C, $7.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002623 $$.S.copy($3.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002624 }
2625 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002626
2627
2628// ConstVector - A list of comma separated constants.
Reid Spencere77e35e2006-12-01 20:26:20 +00002629ConstVector
Reid Spencer950bf602007-01-26 08:19:09 +00002630 : ConstVector ',' ConstVal { ($$ = $1)->push_back($3); }
2631 | ConstVal {
2632 $$ = new std::vector<ConstInfo>();
2633 $$->push_back($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00002634 }
Reid Spencere77e35e2006-12-01 20:26:20 +00002635 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002636
2637
2638// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Reid Spencer950bf602007-01-26 08:19:09 +00002639GlobalType
2640 : GLOBAL { $$ = false; }
2641 | CONSTANT { $$ = true; }
2642 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002643
2644
2645//===----------------------------------------------------------------------===//
2646// Rules to match Modules
2647//===----------------------------------------------------------------------===//
2648
2649// Module rule: Capture the result of parsing the whole file into a result
2650// variable...
2651//
Reid Spencer950bf602007-01-26 08:19:09 +00002652Module
2653 : FunctionList {
2654 $$ = ParserResult = $1;
2655 CurModule.ModuleDone();
Reid Spencere7c3c602006-11-30 06:36:44 +00002656 }
Jeff Cohenac2dca92007-01-21 19:30:52 +00002657 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002658
Reid Spencer950bf602007-01-26 08:19:09 +00002659// FunctionList - A list of functions, preceeded by a constant pool.
2660//
2661FunctionList
2662 : FunctionList Function { $$ = $1; CurFun.FunctionDone(); }
2663 | FunctionList FunctionProto { $$ = $1; }
2664 | FunctionList MODULE ASM_TOK AsmBlock { $$ = $1; }
2665 | FunctionList IMPLEMENTATION { $$ = $1; }
2666 | ConstPool {
2667 $$ = CurModule.CurrentModule;
2668 // Emit an error if there are any unresolved types left.
2669 if (!CurModule.LateResolveTypes.empty()) {
2670 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
2671 if (DID.Type == ValID::NameVal) {
2672 error("Reference to an undefined type: '"+DID.getName() + "'");
2673 } else {
2674 error("Reference to an undefined type: #" + itostr(DID.Num));
2675 }
2676 }
2677 }
2678 ;
Reid Spencer78720742006-12-02 20:21:22 +00002679
Reid Spencere7c3c602006-11-30 06:36:44 +00002680// ConstPool - Constants with optional names assigned to them.
Reid Spencer950bf602007-01-26 08:19:09 +00002681ConstPool
2682 : ConstPool OptAssign TYPE TypesV {
2683 // Eagerly resolve types. This is not an optimization, this is a
2684 // requirement that is due to the fact that we could have this:
2685 //
2686 // %list = type { %list * }
2687 // %list = type { %list * } ; repeated type decl
2688 //
2689 // If types are not resolved eagerly, then the two types will not be
2690 // determined to be the same type!
2691 //
Reid Spencerbb1fd572007-03-21 17:15:50 +00002692 ResolveTypeTo($2, $4.PAT->get(), $4.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002693
Reid Spencerbb1fd572007-03-21 17:15:50 +00002694 if (!setTypeName($4, $2) && !$2) {
2695 // If this is a numbered type that is not a redefinition, add it to the
2696 // slot table.
2697 CurModule.Types.push_back($4.PAT->get());
2698 CurModule.TypeSigns.push_back($4.S);
Reid Spencera50d5962006-12-02 04:11:07 +00002699 }
Reid Spencered96d1e2007-02-08 09:08:52 +00002700 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002701 }
2702 | ConstPool FunctionProto { // Function prototypes can be in const pool
Reid Spencere7c3c602006-11-30 06:36:44 +00002703 }
2704 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
Reid Spencere7c3c602006-11-30 06:36:44 +00002705 }
Reid Spencer950bf602007-01-26 08:19:09 +00002706 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
2707 if ($5.C == 0)
2708 error("Global value initializer is not a constant");
Reid Spencerbb1fd572007-03-21 17:15:50 +00002709 CurGV = ParseGlobalVariable($2, $3, $4, $5.C->getType(), $5.C, $5.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002710 } GlobalVarAttributes {
2711 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002712 }
Reid Spencer950bf602007-01-26 08:19:09 +00002713 | ConstPool OptAssign EXTERNAL GlobalType Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00002714 const Type *Ty = $5.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00002715 CurGV = ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, Ty, 0,
2716 $5.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002717 delete $5.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002718 } GlobalVarAttributes {
2719 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002720 }
Reid Spencer950bf602007-01-26 08:19:09 +00002721 | ConstPool OptAssign DLLIMPORT GlobalType Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00002722 const Type *Ty = $5.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00002723 CurGV = ParseGlobalVariable($2, GlobalValue::DLLImportLinkage, $4, Ty, 0,
2724 $5.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002725 delete $5.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002726 } GlobalVarAttributes {
2727 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002728 }
Reid Spencer950bf602007-01-26 08:19:09 +00002729 | ConstPool OptAssign EXTERN_WEAK GlobalType Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00002730 const Type *Ty = $5.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00002731 CurGV =
Reid Spencerbb1fd572007-03-21 17:15:50 +00002732 ParseGlobalVariable($2, GlobalValue::ExternalWeakLinkage, $4, Ty, 0,
2733 $5.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002734 delete $5.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002735 } GlobalVarAttributes {
2736 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002737 }
2738 | ConstPool TARGET TargetDefinition {
Reid Spencere7c3c602006-11-30 06:36:44 +00002739 }
2740 | ConstPool DEPLIBS '=' LibrariesDefinition {
Reid Spencere7c3c602006-11-30 06:36:44 +00002741 }
2742 | /* empty: end of list */ {
Reid Spencer950bf602007-01-26 08:19:09 +00002743 }
2744 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002745
Reid Spencer950bf602007-01-26 08:19:09 +00002746AsmBlock
2747 : STRINGCONSTANT {
2748 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
2749 char *EndStr = UnEscapeLexed($1, true);
2750 std::string NewAsm($1, EndStr);
2751 free($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00002752
Reid Spencer950bf602007-01-26 08:19:09 +00002753 if (AsmSoFar.empty())
2754 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
2755 else
2756 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
2757 }
2758 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002759
Reid Spencer950bf602007-01-26 08:19:09 +00002760BigOrLittle
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00002761 : BIG { $$ = Module::BigEndian; }
Reid Spencer950bf602007-01-26 08:19:09 +00002762 | LITTLE { $$ = Module::LittleEndian; }
2763 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002764
2765TargetDefinition
2766 : ENDIAN '=' BigOrLittle {
Reid Spencer950bf602007-01-26 08:19:09 +00002767 CurModule.setEndianness($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00002768 }
2769 | POINTERSIZE '=' EUINT64VAL {
Reid Spencer950bf602007-01-26 08:19:09 +00002770 if ($3 == 32)
2771 CurModule.setPointerSize(Module::Pointer32);
2772 else if ($3 == 64)
2773 CurModule.setPointerSize(Module::Pointer64);
2774 else
2775 error("Invalid pointer size: '" + utostr($3) + "'");
Reid Spencere7c3c602006-11-30 06:36:44 +00002776 }
2777 | TRIPLE '=' STRINGCONSTANT {
Reid Spencer950bf602007-01-26 08:19:09 +00002778 CurModule.CurrentModule->setTargetTriple($3);
2779 free($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00002780 }
2781 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer950bf602007-01-26 08:19:09 +00002782 CurModule.CurrentModule->setDataLayout($3);
2783 free($3);
2784 }
2785 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002786
2787LibrariesDefinition
Reid Spencer950bf602007-01-26 08:19:09 +00002788 : '[' LibList ']'
2789 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002790
2791LibList
2792 : LibList ',' STRINGCONSTANT {
Reid Spencer950bf602007-01-26 08:19:09 +00002793 CurModule.CurrentModule->addLibrary($3);
2794 free($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00002795 }
Reid Spencer950bf602007-01-26 08:19:09 +00002796 | STRINGCONSTANT {
2797 CurModule.CurrentModule->addLibrary($1);
2798 free($1);
2799 }
2800 | /* empty: end of list */ { }
2801 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002802
2803//===----------------------------------------------------------------------===//
2804// Rules to match Function Headers
2805//===----------------------------------------------------------------------===//
2806
Reid Spencer950bf602007-01-26 08:19:09 +00002807Name
2808 : VAR_ID | STRINGCONSTANT
2809 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002810
Reid Spencer950bf602007-01-26 08:19:09 +00002811OptName
2812 : Name
2813 | /*empty*/ { $$ = 0; }
2814 ;
2815
2816ArgVal
2817 : Types OptName {
Reid Spencered96d1e2007-02-08 09:08:52 +00002818 if ($1.PAT->get() == Type::VoidTy)
Reid Spencer950bf602007-01-26 08:19:09 +00002819 error("void typed arguments are invalid");
2820 $$ = new std::pair<PATypeInfo, char*>($1, $2);
Reid Spencer52402b02007-01-02 05:45:11 +00002821 }
Reid Spencer950bf602007-01-26 08:19:09 +00002822 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002823
Reid Spencer950bf602007-01-26 08:19:09 +00002824ArgListH
2825 : ArgListH ',' ArgVal {
2826 $$ = $1;
2827 $$->push_back(*$3);
Reid Spencere77e35e2006-12-01 20:26:20 +00002828 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002829 }
2830 | ArgVal {
Reid Spencer950bf602007-01-26 08:19:09 +00002831 $$ = new std::vector<std::pair<PATypeInfo,char*> >();
2832 $$->push_back(*$1);
2833 delete $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00002834 }
Reid Spencer950bf602007-01-26 08:19:09 +00002835 ;
2836
2837ArgList
2838 : ArgListH { $$ = $1; }
Reid Spencere7c3c602006-11-30 06:36:44 +00002839 | ArgListH ',' DOTDOTDOT {
Reid Spencere7c3c602006-11-30 06:36:44 +00002840 $$ = $1;
Reid Spencer950bf602007-01-26 08:19:09 +00002841 PATypeInfo VoidTI;
Reid Spencered96d1e2007-02-08 09:08:52 +00002842 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002843 VoidTI.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002844 $$->push_back(std::pair<PATypeInfo, char*>(VoidTI, 0));
Reid Spencere7c3c602006-11-30 06:36:44 +00002845 }
2846 | DOTDOTDOT {
Reid Spencer950bf602007-01-26 08:19:09 +00002847 $$ = new std::vector<std::pair<PATypeInfo,char*> >();
2848 PATypeInfo VoidTI;
Reid Spencered96d1e2007-02-08 09:08:52 +00002849 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002850 VoidTI.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002851 $$->push_back(std::pair<PATypeInfo, char*>(VoidTI, 0));
Reid Spencere7c3c602006-11-30 06:36:44 +00002852 }
Reid Spencer950bf602007-01-26 08:19:09 +00002853 | /* empty */ { $$ = 0; }
2854 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002855
Reid Spencer71d2ec92006-12-31 06:02:26 +00002856FunctionHeaderH
2857 : OptCallingConv TypesV Name '(' ArgList ')' OptSection OptAlign {
Reid Spencer950bf602007-01-26 08:19:09 +00002858 UnEscapeLexed($3);
2859 std::string FunctionName($3);
2860 free($3); // Free strdup'd memory!
Reid Spencere7c3c602006-11-30 06:36:44 +00002861
Reid Spencered96d1e2007-02-08 09:08:52 +00002862 const Type* RetTy = $2.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00002863
2864 if (!RetTy->isFirstClassType() && RetTy != Type::VoidTy)
2865 error("LLVM functions cannot return aggregate types");
2866
Reid Spencerbb1fd572007-03-21 17:15:50 +00002867 Signedness FTySign;
2868 FTySign.makeComposite($2.S);
Reid Spenceref9b9a72007-02-05 20:47:22 +00002869 std::vector<const Type*> ParamTyList;
Reid Spencer950bf602007-01-26 08:19:09 +00002870
2871 // In LLVM 2.0 the signatures of three varargs intrinsics changed to take
2872 // i8*. We check here for those names and override the parameter list
2873 // types to ensure the prototype is correct.
2874 if (FunctionName == "llvm.va_start" || FunctionName == "llvm.va_end") {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002875 ParamTyList.push_back(PointerType::get(Type::Int8Ty));
Reid Spencer950bf602007-01-26 08:19:09 +00002876 } else if (FunctionName == "llvm.va_copy") {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002877 ParamTyList.push_back(PointerType::get(Type::Int8Ty));
2878 ParamTyList.push_back(PointerType::get(Type::Int8Ty));
Reid Spencer950bf602007-01-26 08:19:09 +00002879 } else if ($5) { // If there are arguments...
2880 for (std::vector<std::pair<PATypeInfo,char*> >::iterator
2881 I = $5->begin(), E = $5->end(); I != E; ++I) {
Reid Spencered96d1e2007-02-08 09:08:52 +00002882 const Type *Ty = I->first.PAT->get();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002883 ParamTyList.push_back(Ty);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002884 FTySign.add(I->first.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002885 }
2886 }
2887
Reid Spenceref9b9a72007-02-05 20:47:22 +00002888 bool isVarArg = ParamTyList.size() && ParamTyList.back() == Type::VoidTy;
2889 if (isVarArg)
2890 ParamTyList.pop_back();
Reid Spencer950bf602007-01-26 08:19:09 +00002891
Reid Spencerb7046c72007-01-29 05:41:34 +00002892 // Convert the CSRet calling convention into the corresponding parameter
2893 // attribute.
2894 FunctionType::ParamAttrsList ParamAttrs;
2895 if ($1 == OldCallingConv::CSRet) {
2896 ParamAttrs.push_back(FunctionType::NoAttributeSet); // result
2897 ParamAttrs.push_back(FunctionType::StructRetAttribute); // first arg
2898 }
2899
Reid Spenceref9b9a72007-02-05 20:47:22 +00002900 const FunctionType *FT = FunctionType::get(RetTy, ParamTyList, isVarArg,
Reid Spencerb7046c72007-01-29 05:41:34 +00002901 ParamAttrs);
Reid Spencer950bf602007-01-26 08:19:09 +00002902 const PointerType *PFT = PointerType::get(FT);
Reid Spencered96d1e2007-02-08 09:08:52 +00002903 delete $2.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002904
2905 ValID ID;
2906 if (!FunctionName.empty()) {
Reid Spencer5eb77c72007-03-15 03:26:42 +00002907 ID = ValID::create((char*)FunctionName.c_str());
Reid Spencer950bf602007-01-26 08:19:09 +00002908 } else {
Reid Spencer5eb77c72007-03-15 03:26:42 +00002909 ID = ValID::create((int)CurModule.Values[PFT].size());
Reid Spencer950bf602007-01-26 08:19:09 +00002910 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00002911 ID.S.makeComposite(FTySign);
Reid Spencer950bf602007-01-26 08:19:09 +00002912
2913 Function *Fn = 0;
Reid Spencered96d1e2007-02-08 09:08:52 +00002914 Module* M = CurModule.CurrentModule;
2915
Reid Spencer950bf602007-01-26 08:19:09 +00002916 // See if this function was forward referenced. If so, recycle the object.
2917 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2918 // Move the function to the end of the list, from whereever it was
2919 // previously inserted.
2920 Fn = cast<Function>(FWRef);
Reid Spencered96d1e2007-02-08 09:08:52 +00002921 M->getFunctionList().remove(Fn);
2922 M->getFunctionList().push_back(Fn);
2923 } else if (!FunctionName.empty()) {
2924 GlobalValue *Conflict = M->getFunction(FunctionName);
2925 if (!Conflict)
2926 Conflict = M->getNamedGlobal(FunctionName);
2927 if (Conflict && PFT == Conflict->getType()) {
2928 if (!CurFun.isDeclare && !Conflict->isDeclaration()) {
2929 // We have two function definitions that conflict, same type, same
2930 // name. We should really check to make sure that this is the result
2931 // of integer type planes collapsing and generate an error if it is
2932 // not, but we'll just rename on the assumption that it is. However,
2933 // let's do it intelligently and rename the internal linkage one
2934 // if there is one.
2935 std::string NewName(makeNameUnique(FunctionName));
2936 if (Conflict->hasInternalLinkage()) {
2937 Conflict->setName(NewName);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002938 RenameMapKey Key =
2939 makeRenameMapKey(FunctionName, Conflict->getType(), ID.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002940 CurModule.RenameMap[Key] = NewName;
2941 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
2942 InsertValue(Fn, CurModule.Values);
2943 } else {
2944 Fn = new Function(FT, CurFun.Linkage, NewName, M);
2945 InsertValue(Fn, CurModule.Values);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002946 RenameMapKey Key =
2947 makeRenameMapKey(FunctionName, PFT, ID.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002948 CurModule.RenameMap[Key] = NewName;
2949 }
2950 } else {
2951 // If they are not both definitions, then just use the function we
2952 // found since the types are the same.
2953 Fn = cast<Function>(Conflict);
Reid Spenceref9b9a72007-02-05 20:47:22 +00002954
Reid Spencered96d1e2007-02-08 09:08:52 +00002955 // Make sure to strip off any argument names so we can't get
2956 // conflicts.
2957 if (Fn->isDeclaration())
2958 for (Function::arg_iterator AI = Fn->arg_begin(),
2959 AE = Fn->arg_end(); AI != AE; ++AI)
2960 AI->setName("");
2961 }
2962 } else if (Conflict) {
2963 // We have two globals with the same name and different types.
2964 // Previously, this was permitted because the symbol table had
2965 // "type planes" and names only needed to be distinct within a
2966 // type plane. After PR411 was fixed, this is no loner the case.
2967 // To resolve this we must rename one of the two.
2968 if (Conflict->hasInternalLinkage()) {
Reid Spencerbb1fd572007-03-21 17:15:50 +00002969 // We can safely rename the Conflict.
2970 RenameMapKey Key =
2971 makeRenameMapKey(Conflict->getName(), Conflict->getType(),
2972 CurModule.NamedValueSigns[Conflict->getName()]);
Reid Spencered96d1e2007-02-08 09:08:52 +00002973 Conflict->setName(makeNameUnique(Conflict->getName()));
Reid Spencered96d1e2007-02-08 09:08:52 +00002974 CurModule.RenameMap[Key] = Conflict->getName();
2975 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
2976 InsertValue(Fn, CurModule.Values);
Reid Spencerd2920cd2007-03-21 17:27:53 +00002977 } else {
Reid Spencered96d1e2007-02-08 09:08:52 +00002978 // We can't quietly rename either of these things, but we must
Reid Spencerd2920cd2007-03-21 17:27:53 +00002979 // rename one of them. Only if the function's linkage is internal can
2980 // we forgo a warning message about the renamed function.
Reid Spencered96d1e2007-02-08 09:08:52 +00002981 std::string NewName = makeNameUnique(FunctionName);
Reid Spencerd2920cd2007-03-21 17:27:53 +00002982 if (CurFun.Linkage != GlobalValue::InternalLinkage) {
2983 warning("Renaming function '" + FunctionName + "' as '" + NewName +
2984 "' may cause linkage errors");
2985 }
2986 // Elect to rename the thing we're now defining.
Reid Spencered96d1e2007-02-08 09:08:52 +00002987 Fn = new Function(FT, CurFun.Linkage, NewName, M);
2988 InsertValue(Fn, CurModule.Values);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002989 RenameMapKey Key = makeRenameMapKey(FunctionName, PFT, ID.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002990 CurModule.RenameMap[Key] = NewName;
Reid Spencerd2920cd2007-03-21 17:27:53 +00002991 }
Reid Spenceref9b9a72007-02-05 20:47:22 +00002992 } else {
Reid Spencered96d1e2007-02-08 09:08:52 +00002993 // There's no conflict, just define the function
2994 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
2995 InsertValue(Fn, CurModule.Values);
Reid Spenceref9b9a72007-02-05 20:47:22 +00002996 }
Reid Spencer950bf602007-01-26 08:19:09 +00002997 }
2998
2999 CurFun.FunctionStart(Fn);
3000
3001 if (CurFun.isDeclare) {
3002 // If we have declaration, always overwrite linkage. This will allow us
3003 // to correctly handle cases, when pointer to function is passed as
3004 // argument to another function.
3005 Fn->setLinkage(CurFun.Linkage);
3006 }
Reid Spencerb7046c72007-01-29 05:41:34 +00003007 Fn->setCallingConv(upgradeCallingConv($1));
Reid Spencer950bf602007-01-26 08:19:09 +00003008 Fn->setAlignment($8);
3009 if ($7) {
3010 Fn->setSection($7);
3011 free($7);
3012 }
3013
3014 // Add all of the arguments we parsed to the function...
3015 if ($5) { // Is null if empty...
3016 if (isVarArg) { // Nuke the last entry
Reid Spencered96d1e2007-02-08 09:08:52 +00003017 assert($5->back().first.PAT->get() == Type::VoidTy &&
Reid Spencer950bf602007-01-26 08:19:09 +00003018 $5->back().second == 0 && "Not a varargs marker");
Reid Spencered96d1e2007-02-08 09:08:52 +00003019 delete $5->back().first.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003020 $5->pop_back(); // Delete the last entry
3021 }
3022 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00003023 Function::arg_iterator ArgEnd = Fn->arg_end();
3024 std::vector<std::pair<PATypeInfo,char*> >::iterator I = $5->begin();
3025 std::vector<std::pair<PATypeInfo,char*> >::iterator E = $5->end();
3026 for ( ; I != E && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencered96d1e2007-02-08 09:08:52 +00003027 delete I->first.PAT; // Delete the typeholder...
Reid Spencerbb1fd572007-03-21 17:15:50 +00003028 ValueInfo VI; VI.V = ArgIt; VI.S.copy(I->first.S);
3029 setValueName(VI, I->second); // Insert arg into symtab...
Reid Spencer950bf602007-01-26 08:19:09 +00003030 InsertValue(ArgIt);
3031 }
3032 delete $5; // We're now done with the argument list
3033 }
3034 }
3035 ;
3036
3037BEGIN
3038 : BEGINTOK | '{' // Allow BEGIN or '{' to start a function
Jeff Cohenac2dca92007-01-21 19:30:52 +00003039 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003040
Reid Spencer6fd36ab2006-12-29 20:35:03 +00003041FunctionHeader
Reid Spencerd2920cd2007-03-21 17:27:53 +00003042 : OptLinkage { CurFun.Linkage = $1; } FunctionHeaderH BEGIN {
Reid Spencer950bf602007-01-26 08:19:09 +00003043 $$ = CurFun.CurrentFunction;
3044
3045 // Make sure that we keep track of the linkage type even if there was a
3046 // previous "declare".
3047 $$->setLinkage($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00003048 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00003049 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003050
Reid Spencer950bf602007-01-26 08:19:09 +00003051END
3052 : ENDTOK | '}' // Allow end of '}' to end a function
3053 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003054
Reid Spencer950bf602007-01-26 08:19:09 +00003055Function
3056 : BasicBlockList END {
3057 $$ = $1;
3058 };
Reid Spencere7c3c602006-11-30 06:36:44 +00003059
Reid Spencere77e35e2006-12-01 20:26:20 +00003060FnDeclareLinkage
Reid Spencered96d1e2007-02-08 09:08:52 +00003061 : /*default*/ { $$ = GlobalValue::ExternalLinkage; }
3062 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
3063 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencere7c3c602006-11-30 06:36:44 +00003064 ;
3065
3066FunctionProto
Reid Spencered96d1e2007-02-08 09:08:52 +00003067 : DECLARE { CurFun.isDeclare = true; }
3068 FnDeclareLinkage { CurFun.Linkage = $3; } FunctionHeaderH {
Reid Spencer950bf602007-01-26 08:19:09 +00003069 $$ = CurFun.CurrentFunction;
3070 CurFun.FunctionDone();
3071
3072 }
3073 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003074
3075//===----------------------------------------------------------------------===//
3076// Rules to match Basic Blocks
3077//===----------------------------------------------------------------------===//
3078
Reid Spencer950bf602007-01-26 08:19:09 +00003079OptSideEffect
3080 : /* empty */ { $$ = false; }
3081 | SIDEEFFECT { $$ = true; }
3082 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003083
Reid Spencere77e35e2006-12-01 20:26:20 +00003084ConstValueRef
Reid Spencer950bf602007-01-26 08:19:09 +00003085 // A reference to a direct constant
Reid Spencerbb1fd572007-03-21 17:15:50 +00003086 : ESINT64VAL { $$ = ValID::create($1); }
Reid Spencer950bf602007-01-26 08:19:09 +00003087 | EUINT64VAL { $$ = ValID::create($1); }
3088 | FPVAL { $$ = ValID::create($1); }
Reid Spencerbb1fd572007-03-21 17:15:50 +00003089 | TRUETOK {
3090 $$ = ValID::create(ConstantInt::get(Type::Int1Ty, true));
3091 $$.S.makeUnsigned();
3092 }
3093 | FALSETOK {
3094 $$ = ValID::create(ConstantInt::get(Type::Int1Ty, false));
3095 $$.S.makeUnsigned();
3096 }
Reid Spencer950bf602007-01-26 08:19:09 +00003097 | NULL_TOK { $$ = ValID::createNull(); }
3098 | UNDEF { $$ = ValID::createUndef(); }
3099 | ZEROINITIALIZER { $$ = ValID::createZeroInit(); }
3100 | '<' ConstVector '>' { // Nonempty unsized packed vector
3101 const Type *ETy = (*$2)[0].C->getType();
3102 int NumElements = $2->size();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003103 VectorType* pt = VectorType::get(ETy, NumElements);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003104 $$.S.makeComposite((*$2)[0].S);
3105 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(pt, $$.S));
Reid Spencer950bf602007-01-26 08:19:09 +00003106
3107 // Verify all elements are correct type!
3108 std::vector<Constant*> Elems;
3109 for (unsigned i = 0; i < $2->size(); i++) {
3110 Constant *C = (*$2)[i].C;
3111 const Type *CTy = C->getType();
3112 if (ETy != CTy)
3113 error("Element #" + utostr(i) + " is not of type '" +
3114 ETy->getDescription() +"' as required!\nIt is of type '" +
3115 CTy->getDescription() + "'");
3116 Elems.push_back(C);
Reid Spencere7c3c602006-11-30 06:36:44 +00003117 }
Reid Spencer5eb77c72007-03-15 03:26:42 +00003118 $$ = ValID::create(ConstantVector::get(pt, Elems));
Reid Spencer950bf602007-01-26 08:19:09 +00003119 delete PTy; delete $2;
3120 }
3121 | ConstExpr {
Reid Spencer5eb77c72007-03-15 03:26:42 +00003122 $$ = ValID::create($1.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003123 $$.S.copy($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003124 }
3125 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
3126 char *End = UnEscapeLexed($3, true);
3127 std::string AsmStr = std::string($3, End);
3128 End = UnEscapeLexed($5, true);
3129 std::string Constraints = std::string($5, End);
3130 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
3131 free($3);
3132 free($5);
3133 }
3134 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003135
Reid Spencerbb1fd572007-03-21 17:15:50 +00003136// SymbolicValueRef - Reference to one of two ways of symbolically refering to // another value.
Reid Spencer950bf602007-01-26 08:19:09 +00003137//
3138SymbolicValueRef
Reid Spencerbb1fd572007-03-21 17:15:50 +00003139 : INTVAL { $$ = ValID::create($1); $$.S.makeSignless(); }
3140 | Name { $$ = ValID::create($1); $$.S.makeSignless(); }
Reid Spencer950bf602007-01-26 08:19:09 +00003141 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003142
3143// ValueRef - A reference to a definition... either constant or symbolic
Reid Spencerf459d392006-12-02 16:19:52 +00003144ValueRef
Reid Spencer950bf602007-01-26 08:19:09 +00003145 : SymbolicValueRef | ConstValueRef
Reid Spencerf459d392006-12-02 16:19:52 +00003146 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003147
Reid Spencer950bf602007-01-26 08:19:09 +00003148
Reid Spencere7c3c602006-11-30 06:36:44 +00003149// ResolvedVal - a <type> <value> pair. This is used only in cases where the
3150// type immediately preceeds the value reference, and allows complex constant
3151// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Reid Spencer950bf602007-01-26 08:19:09 +00003152ResolvedVal
3153 : Types ValueRef {
Reid Spencered96d1e2007-02-08 09:08:52 +00003154 const Type *Ty = $1.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003155 $2.S.copy($1.S);
Reid Spencer5eb77c72007-03-15 03:26:42 +00003156 $$.V = getVal(Ty, $2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003157 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003158 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003159 }
Reid Spencer950bf602007-01-26 08:19:09 +00003160 ;
3161
3162BasicBlockList
3163 : BasicBlockList BasicBlock {
3164 $$ = $1;
3165 }
3166 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
3167 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00003168 };
3169
3170
3171// Basic blocks are terminated by branching instructions:
3172// br, br/cc, switch, ret
3173//
Reid Spencer950bf602007-01-26 08:19:09 +00003174BasicBlock
3175 : InstructionList OptAssign BBTerminatorInst {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003176 ValueInfo VI; VI.V = $3.TI; VI.S.copy($3.S);
3177 setValueName(VI, $2);
3178 InsertValue($3.TI);
3179 $1->getInstList().push_back($3.TI);
Reid Spencer950bf602007-01-26 08:19:09 +00003180 InsertValue($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00003181 $$ = $1;
3182 }
Reid Spencer950bf602007-01-26 08:19:09 +00003183 ;
3184
3185InstructionList
3186 : InstructionList Inst {
3187 if ($2.I)
3188 $1->getInstList().push_back($2.I);
3189 $$ = $1;
3190 }
3191 | /* empty */ {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003192 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++),true);
Reid Spencer950bf602007-01-26 08:19:09 +00003193 // Make sure to move the basic block to the correct location in the
3194 // function, instead of leaving it inserted wherever it was first
3195 // referenced.
3196 Function::BasicBlockListType &BBL =
3197 CurFun.CurrentFunction->getBasicBlockList();
3198 BBL.splice(BBL.end(), BBL, $$);
3199 }
3200 | LABELSTR {
Reid Spencer5eb77c72007-03-15 03:26:42 +00003201 $$ = CurBB = getBBVal(ValID::create($1), true);
Reid Spencer950bf602007-01-26 08:19:09 +00003202 // Make sure to move the basic block to the correct location in the
3203 // function, instead of leaving it inserted wherever it was first
3204 // referenced.
3205 Function::BasicBlockListType &BBL =
3206 CurFun.CurrentFunction->getBasicBlockList();
3207 BBL.splice(BBL.end(), BBL, $$);
3208 }
3209 ;
3210
3211Unwind : UNWIND | EXCEPT;
3212
3213BBTerminatorInst
3214 : RET ResolvedVal { // Return with a result...
Reid Spencerbb1fd572007-03-21 17:15:50 +00003215 $$.TI = new ReturnInst($2.V);
3216 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003217 }
3218 | RET VOID { // Return with no result...
Reid Spencerbb1fd572007-03-21 17:15:50 +00003219 $$.TI = new ReturnInst();
3220 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003221 }
3222 | BR LABEL ValueRef { // Unconditional Branch...
3223 BasicBlock* tmpBB = getBBVal($3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003224 $$.TI = new BranchInst(tmpBB);
3225 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003226 } // Conditional Branch...
3227 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003228 $6.S.makeSignless();
3229 $9.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003230 BasicBlock* tmpBBA = getBBVal($6);
3231 BasicBlock* tmpBBB = getBBVal($9);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003232 $3.S.makeUnsigned();
Reid Spencer950bf602007-01-26 08:19:09 +00003233 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003234 $$.TI = new BranchInst(tmpBBA, tmpBBB, tmpVal);
3235 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003236 }
3237 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003238 $3.S.copy($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003239 Value* tmpVal = getVal($2.T, $3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003240 $6.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003241 BasicBlock* tmpBB = getBBVal($6);
3242 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Reid Spencerbb1fd572007-03-21 17:15:50 +00003243 $$.TI = S;
3244 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003245 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
3246 E = $8->end();
3247 for (; I != E; ++I) {
3248 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
3249 S->addCase(CI, I->second);
3250 else
3251 error("Switch case is constant, but not a simple integer");
3252 }
3253 delete $8;
3254 }
3255 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003256 $3.S.copy($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003257 Value* tmpVal = getVal($2.T, $3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003258 $6.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003259 BasicBlock* tmpBB = getBBVal($6);
3260 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003261 $$.TI = S;
3262 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003263 }
3264 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
3265 TO LABEL ValueRef Unwind LABEL ValueRef {
3266 const PointerType *PFTy;
3267 const FunctionType *Ty;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003268 Signedness FTySign;
Reid Spencer950bf602007-01-26 08:19:09 +00003269
Reid Spencered96d1e2007-02-08 09:08:52 +00003270 if (!(PFTy = dyn_cast<PointerType>($3.PAT->get())) ||
Reid Spencer950bf602007-01-26 08:19:09 +00003271 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3272 // Pull out the types of all of the arguments...
3273 std::vector<const Type*> ParamTypes;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003274 FTySign.makeComposite($3.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003275 if ($6) {
3276 for (std::vector<ValueInfo>::iterator I = $6->begin(), E = $6->end();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003277 I != E; ++I) {
Reid Spencer950bf602007-01-26 08:19:09 +00003278 ParamTypes.push_back((*I).V->getType());
Reid Spencerbb1fd572007-03-21 17:15:50 +00003279 FTySign.add(I->S);
3280 }
Reid Spencer950bf602007-01-26 08:19:09 +00003281 }
Reid Spencerb7046c72007-01-29 05:41:34 +00003282 FunctionType::ParamAttrsList ParamAttrs;
3283 if ($2 == OldCallingConv::CSRet) {
3284 ParamAttrs.push_back(FunctionType::NoAttributeSet);
3285 ParamAttrs.push_back(FunctionType::StructRetAttribute);
3286 }
Reid Spencer950bf602007-01-26 08:19:09 +00003287 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
3288 if (isVarArg) ParamTypes.pop_back();
Reid Spencered96d1e2007-02-08 09:08:52 +00003289 Ty = FunctionType::get($3.PAT->get(), ParamTypes, isVarArg, ParamAttrs);
Reid Spencer950bf602007-01-26 08:19:09 +00003290 PFTy = PointerType::get(Ty);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003291 $$.S.copy($3.S);
3292 } else {
3293 FTySign = $3.S;
3294 $$.S.copy($3.S.get(0)); // 0th element of FuncTy sign is result ty
Reid Spencer950bf602007-01-26 08:19:09 +00003295 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00003296 $4.S.makeComposite(FTySign);
Reid Spencer950bf602007-01-26 08:19:09 +00003297 Value *V = getVal(PFTy, $4); // Get the function we're calling...
3298 BasicBlock *Normal = getBBVal($10);
3299 BasicBlock *Except = getBBVal($13);
3300
3301 // Create the call node...
3302 if (!$6) { // Has no arguments?
Reid Spencerbb1fd572007-03-21 17:15:50 +00003303 $$.TI = new InvokeInst(V, Normal, Except, 0, 0);
Reid Spencer950bf602007-01-26 08:19:09 +00003304 } else { // Has arguments?
3305 // Loop through FunctionType's arguments and ensure they are specified
3306 // correctly!
3307 //
3308 FunctionType::param_iterator I = Ty->param_begin();
3309 FunctionType::param_iterator E = Ty->param_end();
3310 std::vector<ValueInfo>::iterator ArgI = $6->begin(), ArgE = $6->end();
3311
3312 std::vector<Value*> Args;
3313 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
3314 if ((*ArgI).V->getType() != *I)
3315 error("Parameter " +(*ArgI).V->getName()+ " is not of type '" +
3316 (*I)->getDescription() + "'");
3317 Args.push_back((*ArgI).V);
3318 }
3319
3320 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
3321 error("Invalid number of parameters detected");
3322
Reid Spencerbb1fd572007-03-21 17:15:50 +00003323 $$.TI = new InvokeInst(V, Normal, Except, &Args[0], Args.size());
Reid Spencer950bf602007-01-26 08:19:09 +00003324 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00003325 cast<InvokeInst>($$.TI)->setCallingConv(upgradeCallingConv($2));
Reid Spencered96d1e2007-02-08 09:08:52 +00003326 delete $3.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003327 delete $6;
3328 }
3329 | Unwind {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003330 $$.TI = new UnwindInst();
3331 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003332 }
3333 | UNREACHABLE {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003334 $$.TI = new UnreachableInst();
3335 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003336 }
3337 ;
3338
3339JumpTable
3340 : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
3341 $$ = $1;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003342 $3.S.copy($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003343 Constant *V = cast<Constant>(getExistingValue($2.T, $3));
3344
3345 if (V == 0)
3346 error("May only switch on a constant pool value");
3347
Reid Spencerbb1fd572007-03-21 17:15:50 +00003348 $6.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003349 BasicBlock* tmpBB = getBBVal($6);
3350 $$->push_back(std::make_pair(V, tmpBB));
3351 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003352 | IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencer950bf602007-01-26 08:19:09 +00003353 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003354 $2.S.copy($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003355 Constant *V = cast<Constant>(getExistingValue($1.T, $2));
3356
3357 if (V == 0)
3358 error("May only switch on a constant pool value");
3359
Reid Spencerbb1fd572007-03-21 17:15:50 +00003360 $5.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003361 BasicBlock* tmpBB = getBBVal($5);
3362 $$->push_back(std::make_pair(V, tmpBB));
3363 }
3364 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003365
3366Inst
3367 : OptAssign InstVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003368 bool omit = false;
3369 if ($1)
3370 if (BitCastInst *BCI = dyn_cast<BitCastInst>($2.I))
3371 if (BCI->getSrcTy() == BCI->getDestTy() &&
3372 BCI->getOperand(0)->getName() == $1)
3373 // This is a useless bit cast causing a name redefinition. It is
3374 // a bit cast from a type to the same type of an operand with the
3375 // same name as the name we would give this instruction. Since this
3376 // instruction results in no code generation, it is safe to omit
3377 // the instruction. This situation can occur because of collapsed
3378 // type planes. For example:
3379 // %X = add int %Y, %Z
3380 // %X = cast int %Y to uint
3381 // After upgrade, this looks like:
3382 // %X = add i32 %Y, %Z
3383 // %X = bitcast i32 to i32
3384 // The bitcast is clearly useless so we omit it.
3385 omit = true;
3386 if (omit) {
3387 $$.I = 0;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003388 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003389 } else {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003390 ValueInfo VI; VI.V = $2.I; VI.S.copy($2.S);
3391 setValueName(VI, $1);
Reid Spencer950bf602007-01-26 08:19:09 +00003392 InsertValue($2.I);
3393 $$ = $2;
Reid Spencerf5626a32007-01-01 01:20:41 +00003394 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003395 };
3396
Reid Spencer950bf602007-01-26 08:19:09 +00003397PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
3398 $$.P = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003399 $$.S.copy($1.S);
3400 $3.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003401 Value* tmpVal = getVal($1.PAT->get(), $3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003402 $5.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003403 BasicBlock* tmpBB = getBBVal($5);
3404 $$.P->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencered96d1e2007-02-08 09:08:52 +00003405 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003406 }
3407 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
Reid Spencere7c3c602006-11-30 06:36:44 +00003408 $$ = $1;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003409 $4.S.copy($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003410 Value* tmpVal = getVal($1.P->front().first->getType(), $4);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003411 $6.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003412 BasicBlock* tmpBB = getBBVal($6);
3413 $1.P->push_back(std::make_pair(tmpVal, tmpBB));
3414 }
3415 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003416
Reid Spencer950bf602007-01-26 08:19:09 +00003417ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
3418 $$ = new std::vector<ValueInfo>();
Reid Spencerf8483652006-12-02 15:16:01 +00003419 $$->push_back($1);
3420 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003421 | ValueRefList ',' ResolvedVal {
Reid Spencere7c3c602006-11-30 06:36:44 +00003422 $$ = $1;
Reid Spencer950bf602007-01-26 08:19:09 +00003423 $1->push_back($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00003424 };
3425
3426// ValueRefListE - Just like ValueRefList, except that it may also be empty!
3427ValueRefListE
Reid Spencer950bf602007-01-26 08:19:09 +00003428 : ValueRefList
3429 | /*empty*/ { $$ = 0; }
Reid Spencere7c3c602006-11-30 06:36:44 +00003430 ;
3431
3432OptTailCall
3433 : TAIL CALL {
Reid Spencer950bf602007-01-26 08:19:09 +00003434 $$ = true;
Reid Spencere7c3c602006-11-30 06:36:44 +00003435 }
Reid Spencer950bf602007-01-26 08:19:09 +00003436 | CALL {
3437 $$ = false;
3438 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003439 ;
3440
Reid Spencer950bf602007-01-26 08:19:09 +00003441InstVal
3442 : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003443 $3.S.copy($2.S);
3444 $5.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003445 const Type* Ty = $2.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003446 if (!Ty->isInteger() && !Ty->isFloatingPoint() && !isa<VectorType>(Ty))
Reid Spencer950bf602007-01-26 08:19:09 +00003447 error("Arithmetic operator requires integer, FP, or packed operands");
Reid Spencer9d6565a2007-02-15 02:26:10 +00003448 if (isa<VectorType>(Ty) &&
Reid Spencer950bf602007-01-26 08:19:09 +00003449 ($1 == URemOp || $1 == SRemOp || $1 == FRemOp || $1 == RemOp))
Chris Lattner4227bdb2007-02-19 07:34:02 +00003450 error("Remainder not supported on vector types");
Reid Spencer950bf602007-01-26 08:19:09 +00003451 // Upgrade the opcode from obsolete versions before we do anything with it.
3452 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $2.S);
3453 Value* val1 = getVal(Ty, $3);
3454 Value* val2 = getVal(Ty, $5);
3455 $$.I = BinaryOperator::create(Opcode, val1, val2);
3456 if ($$.I == 0)
3457 error("binary operator returned null");
Reid Spencerbb1fd572007-03-21 17:15:50 +00003458 $$.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003459 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003460 }
3461 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003462 $3.S.copy($2.S);
3463 $5.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003464 const Type *Ty = $2.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003465 if (!Ty->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00003466 if (!isa<VectorType>(Ty) ||
3467 !cast<VectorType>(Ty)->getElementType()->isInteger())
Reid Spencer950bf602007-01-26 08:19:09 +00003468 error("Logical operator requires integral operands");
3469 }
3470 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $2.S);
3471 Value* tmpVal1 = getVal(Ty, $3);
3472 Value* tmpVal2 = getVal(Ty, $5);
3473 $$.I = BinaryOperator::create(Opcode, tmpVal1, tmpVal2);
3474 if ($$.I == 0)
3475 error("binary operator returned null");
Reid Spencerbb1fd572007-03-21 17:15:50 +00003476 $$.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003477 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003478 }
3479 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003480 $3.S.copy($2.S);
3481 $5.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003482 const Type* Ty = $2.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003483 if(isa<VectorType>(Ty))
3484 error("VectorTypes currently not supported in setcc instructions");
Reid Spencer950bf602007-01-26 08:19:09 +00003485 unsigned short pred;
3486 Instruction::OtherOps Opcode = getCompareOp($1, pred, Ty, $2.S);
3487 Value* tmpVal1 = getVal(Ty, $3);
3488 Value* tmpVal2 = getVal(Ty, $5);
3489 $$.I = CmpInst::create(Opcode, pred, tmpVal1, tmpVal2);
3490 if ($$.I == 0)
3491 error("binary operator returned null");
Reid Spencerbb1fd572007-03-21 17:15:50 +00003492 $$.S.makeUnsigned();
Reid Spencered96d1e2007-02-08 09:08:52 +00003493 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003494 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00003495 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003496 $4.S.copy($3.S);
3497 $6.S.copy($3.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003498 const Type *Ty = $3.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003499 if (isa<VectorType>(Ty))
3500 error("VectorTypes currently not supported in icmp instructions");
Reid Spencer950bf602007-01-26 08:19:09 +00003501 else if (!Ty->isInteger() && !isa<PointerType>(Ty))
3502 error("icmp requires integer or pointer typed operands");
3503 Value* tmpVal1 = getVal(Ty, $4);
3504 Value* tmpVal2 = getVal(Ty, $6);
3505 $$.I = new ICmpInst($2, tmpVal1, tmpVal2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003506 $$.S.makeUnsigned();
Reid Spencered96d1e2007-02-08 09:08:52 +00003507 delete $3.PAT;
Reid Spencer57f28f92006-12-03 07:10:26 +00003508 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00003509 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003510 $4.S.copy($3.S);
3511 $6.S.copy($3.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003512 const Type *Ty = $3.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003513 if (isa<VectorType>(Ty))
3514 error("VectorTypes currently not supported in fcmp instructions");
Reid Spencer950bf602007-01-26 08:19:09 +00003515 else if (!Ty->isFloatingPoint())
3516 error("fcmp instruction requires floating point operands");
3517 Value* tmpVal1 = getVal(Ty, $4);
3518 Value* tmpVal2 = getVal(Ty, $6);
3519 $$.I = new FCmpInst($2, tmpVal1, tmpVal2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003520 $$.S.makeUnsigned();
Reid Spencered96d1e2007-02-08 09:08:52 +00003521 delete $3.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003522 }
3523 | NOT ResolvedVal {
3524 warning("Use of obsolete 'not' instruction: Replacing with 'xor");
3525 const Type *Ty = $2.V->getType();
3526 Value *Ones = ConstantInt::getAllOnesValue(Ty);
3527 if (Ones == 0)
3528 error("Expected integral type for not instruction");
3529 $$.I = BinaryOperator::create(Instruction::Xor, $2.V, Ones);
3530 if ($$.I == 0)
3531 error("Could not create a xor instruction");
Reid Spencerbb1fd572007-03-21 17:15:50 +00003532 $$.S.copy($2.S);
Reid Spencer229e9362006-12-02 22:14:11 +00003533 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003534 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003535 if (!$4.V->getType()->isInteger() ||
3536 cast<IntegerType>($4.V->getType())->getBitWidth() != 8)
3537 error("Shift amount must be int8");
Reid Spencer832254e2007-02-02 02:16:23 +00003538 const Type* Ty = $2.V->getType();
3539 if (!Ty->isInteger())
Reid Spencer950bf602007-01-26 08:19:09 +00003540 error("Shift constant expression requires integer operand");
Reid Spencer832254e2007-02-02 02:16:23 +00003541 Value* ShiftAmt = 0;
3542 if (cast<IntegerType>(Ty)->getBitWidth() > Type::Int8Ty->getBitWidth())
3543 if (Constant *C = dyn_cast<Constant>($4.V))
3544 ShiftAmt = ConstantExpr::getZExt(C, Ty);
3545 else
3546 ShiftAmt = new ZExtInst($4.V, Ty, makeNameUnique("shift"), CurBB);
3547 else
3548 ShiftAmt = $4.V;
3549 $$.I = BinaryOperator::create(getBinaryOp($1, Ty, $2.S), $2.V, ShiftAmt);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003550 $$.S.copy($2.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00003551 }
Reid Spencerfcb5df82006-12-01 22:34:43 +00003552 | CastOps ResolvedVal TO Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00003553 const Type *DstTy = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003554 if (!DstTy->isFirstClassType())
3555 error("cast instruction to a non-primitive type: '" +
3556 DstTy->getDescription() + "'");
3557 $$.I = cast<Instruction>(getCast($1, $2.V, $2.S, DstTy, $4.S, true));
Reid Spencerbb1fd572007-03-21 17:15:50 +00003558 $$.S.copy($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003559 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003560 }
3561 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003562 if (!$2.V->getType()->isInteger() ||
3563 cast<IntegerType>($2.V->getType())->getBitWidth() != 1)
3564 error("select condition must be bool");
3565 if ($4.V->getType() != $6.V->getType())
3566 error("select value types should match");
3567 $$.I = new SelectInst($2.V, $4.V, $6.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003568 $$.S.copy($4.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00003569 }
3570 | VAARG ResolvedVal ',' Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00003571 const Type *Ty = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003572 NewVarArgs = true;
3573 $$.I = new VAArgInst($2.V, Ty);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003574 $$.S.copy($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003575 delete $4.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003576 }
3577 | VAARG_old ResolvedVal ',' Types {
3578 const Type* ArgTy = $2.V->getType();
Reid Spencered96d1e2007-02-08 09:08:52 +00003579 const Type* DstTy = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003580 ObsoleteVarArgs = true;
3581 Function* NF = cast<Function>(CurModule.CurrentModule->
3582 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0));
3583
3584 //b = vaarg a, t ->
3585 //foo = alloca 1 of t
3586 //bar = vacopy a
3587 //store bar -> foo
3588 //b = vaarg foo, t
3589 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix");
3590 CurBB->getInstList().push_back(foo);
3591 CallInst* bar = new CallInst(NF, $2.V);
3592 CurBB->getInstList().push_back(bar);
3593 CurBB->getInstList().push_back(new StoreInst(bar, foo));
3594 $$.I = new VAArgInst(foo, DstTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003595 $$.S.copy($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003596 delete $4.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003597 }
3598 | VANEXT_old ResolvedVal ',' Types {
3599 const Type* ArgTy = $2.V->getType();
Reid Spencered96d1e2007-02-08 09:08:52 +00003600 const Type* DstTy = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003601 ObsoleteVarArgs = true;
3602 Function* NF = cast<Function>(CurModule.CurrentModule->
3603 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0));
3604
3605 //b = vanext a, t ->
3606 //foo = alloca 1 of t
3607 //bar = vacopy a
3608 //store bar -> foo
3609 //tmp = vaarg foo, t
3610 //b = load foo
3611 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix");
3612 CurBB->getInstList().push_back(foo);
3613 CallInst* bar = new CallInst(NF, $2.V);
3614 CurBB->getInstList().push_back(bar);
3615 CurBB->getInstList().push_back(new StoreInst(bar, foo));
3616 Instruction* tmp = new VAArgInst(foo, DstTy);
3617 CurBB->getInstList().push_back(tmp);
3618 $$.I = new LoadInst(foo);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003619 $$.S.copy($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003620 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003621 }
3622 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003623 if (!ExtractElementInst::isValidOperands($2.V, $4.V))
3624 error("Invalid extractelement operands");
3625 $$.I = new ExtractElementInst($2.V, $4.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003626 $$.S.copy($2.S.get(0));
Reid Spencere7c3c602006-11-30 06:36:44 +00003627 }
3628 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003629 if (!InsertElementInst::isValidOperands($2.V, $4.V, $6.V))
3630 error("Invalid insertelement operands");
3631 $$.I = new InsertElementInst($2.V, $4.V, $6.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003632 $$.S.copy($2.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00003633 }
3634 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003635 if (!ShuffleVectorInst::isValidOperands($2.V, $4.V, $6.V))
3636 error("Invalid shufflevector operands");
3637 $$.I = new ShuffleVectorInst($2.V, $4.V, $6.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003638 $$.S.copy($2.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00003639 }
3640 | PHI_TOK PHIList {
Reid Spencer950bf602007-01-26 08:19:09 +00003641 const Type *Ty = $2.P->front().first->getType();
3642 if (!Ty->isFirstClassType())
3643 error("PHI node operands must be of first class type");
3644 PHINode *PHI = new PHINode(Ty);
3645 PHI->reserveOperandSpace($2.P->size());
3646 while ($2.P->begin() != $2.P->end()) {
3647 if ($2.P->front().first->getType() != Ty)
3648 error("All elements of a PHI node must be of the same type");
3649 PHI->addIncoming($2.P->front().first, $2.P->front().second);
3650 $2.P->pop_front();
3651 }
3652 $$.I = PHI;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003653 $$.S.copy($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003654 delete $2.P; // Free the list...
Reid Spencere7c3c602006-11-30 06:36:44 +00003655 }
3656 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00003657 // Handle the short call syntax
3658 const PointerType *PFTy;
3659 const FunctionType *FTy;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003660 Signedness FTySign;
Reid Spencered96d1e2007-02-08 09:08:52 +00003661 if (!(PFTy = dyn_cast<PointerType>($3.PAT->get())) ||
Reid Spencer950bf602007-01-26 08:19:09 +00003662 !(FTy = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3663 // Pull out the types of all of the arguments...
3664 std::vector<const Type*> ParamTypes;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003665 FTySign.makeComposite($3.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003666 if ($6) {
3667 for (std::vector<ValueInfo>::iterator I = $6->begin(), E = $6->end();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003668 I != E; ++I) {
Reid Spencer950bf602007-01-26 08:19:09 +00003669 ParamTypes.push_back((*I).V->getType());
Reid Spencerbb1fd572007-03-21 17:15:50 +00003670 FTySign.add(I->S);
3671 }
Reid Spencerc4d96252007-01-13 00:03:30 +00003672 }
Reid Spencer950bf602007-01-26 08:19:09 +00003673
Reid Spencerb7046c72007-01-29 05:41:34 +00003674 FunctionType::ParamAttrsList ParamAttrs;
3675 if ($2 == OldCallingConv::CSRet) {
3676 ParamAttrs.push_back(FunctionType::NoAttributeSet);
3677 ParamAttrs.push_back(FunctionType::StructRetAttribute);
3678 }
Reid Spencer950bf602007-01-26 08:19:09 +00003679 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
3680 if (isVarArg) ParamTypes.pop_back();
3681
Reid Spencered96d1e2007-02-08 09:08:52 +00003682 const Type *RetTy = $3.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003683 if (!RetTy->isFirstClassType() && RetTy != Type::VoidTy)
3684 error("Functions cannot return aggregate types");
3685
Reid Spencerb7046c72007-01-29 05:41:34 +00003686 FTy = FunctionType::get(RetTy, ParamTypes, isVarArg, ParamAttrs);
Reid Spencer950bf602007-01-26 08:19:09 +00003687 PFTy = PointerType::get(FTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003688 $$.S.copy($3.S);
3689 } else {
3690 FTySign = $3.S;
3691 $$.S.copy($3.S.get(0)); // 0th element of FuncTy signedness is result sign
Reid Spencerf8483652006-12-02 15:16:01 +00003692 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00003693 $4.S.makeComposite(FTySign);
Reid Spencer950bf602007-01-26 08:19:09 +00003694
3695 // First upgrade any intrinsic calls.
3696 std::vector<Value*> Args;
3697 if ($6)
3698 for (unsigned i = 0, e = $6->size(); i < e; ++i)
3699 Args.push_back((*$6)[i].V);
Reid Spencer41b213e2007-04-02 01:14:00 +00003700 Instruction *Inst = upgradeIntrinsicCall(FTy->getReturnType(), $4, Args);
Reid Spencer950bf602007-01-26 08:19:09 +00003701
3702 // If we got an upgraded intrinsic
3703 if (Inst) {
3704 $$.I = Inst;
Reid Spencer950bf602007-01-26 08:19:09 +00003705 } else {
3706 // Get the function we're calling
3707 Value *V = getVal(PFTy, $4);
3708
3709 // Check the argument values match
3710 if (!$6) { // Has no arguments?
3711 // Make sure no arguments is a good thing!
3712 if (FTy->getNumParams() != 0)
3713 error("No arguments passed to a function that expects arguments");
3714 } else { // Has arguments?
3715 // Loop through FunctionType's arguments and ensure they are specified
3716 // correctly!
3717 //
3718 FunctionType::param_iterator I = FTy->param_begin();
3719 FunctionType::param_iterator E = FTy->param_end();
3720 std::vector<ValueInfo>::iterator ArgI = $6->begin(), ArgE = $6->end();
3721
3722 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
3723 if ((*ArgI).V->getType() != *I)
3724 error("Parameter " +(*ArgI).V->getName()+ " is not of type '" +
3725 (*I)->getDescription() + "'");
3726
3727 if (I != E || (ArgI != ArgE && !FTy->isVarArg()))
3728 error("Invalid number of parameters detected");
3729 }
3730
3731 // Create the call instruction
Chris Lattnercf3d0612007-02-13 06:04:17 +00003732 CallInst *CI = new CallInst(V, &Args[0], Args.size());
Reid Spencer950bf602007-01-26 08:19:09 +00003733 CI->setTailCall($1);
Reid Spencerb7046c72007-01-29 05:41:34 +00003734 CI->setCallingConv(upgradeCallingConv($2));
Reid Spencer950bf602007-01-26 08:19:09 +00003735 $$.I = CI;
Reid Spencer950bf602007-01-26 08:19:09 +00003736 }
Reid Spencered96d1e2007-02-08 09:08:52 +00003737 delete $3.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003738 delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00003739 }
Reid Spencer950bf602007-01-26 08:19:09 +00003740 | MemoryInst {
3741 $$ = $1;
3742 }
3743 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003744
3745
3746// IndexList - List of indices for GEP based instructions...
3747IndexList
Reid Spencer950bf602007-01-26 08:19:09 +00003748 : ',' ValueRefList { $$ = $2; }
3749 | /* empty */ { $$ = new std::vector<ValueInfo>(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00003750 ;
3751
3752OptVolatile
Reid Spencer950bf602007-01-26 08:19:09 +00003753 : VOLATILE { $$ = true; }
3754 | /* empty */ { $$ = false; }
Reid Spencere7c3c602006-11-30 06:36:44 +00003755 ;
3756
Reid Spencer950bf602007-01-26 08:19:09 +00003757MemoryInst
3758 : MALLOC Types OptCAlign {
Reid Spencered96d1e2007-02-08 09:08:52 +00003759 const Type *Ty = $2.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003760 $$.S.makeComposite($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003761 $$.I = new MallocInst(Ty, 0, $3);
Reid Spencered96d1e2007-02-08 09:08:52 +00003762 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003763 }
3764 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencered96d1e2007-02-08 09:08:52 +00003765 const Type *Ty = $2.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003766 $5.S.makeUnsigned();
3767 $$.S.makeComposite($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003768 $$.I = new MallocInst(Ty, getVal($4.T, $5), $6);
Reid Spencered96d1e2007-02-08 09:08:52 +00003769 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003770 }
3771 | ALLOCA Types OptCAlign {
Reid Spencered96d1e2007-02-08 09:08:52 +00003772 const Type *Ty = $2.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003773 $$.S.makeComposite($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003774 $$.I = new AllocaInst(Ty, 0, $3);
Reid Spencered96d1e2007-02-08 09:08:52 +00003775 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003776 }
3777 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencered96d1e2007-02-08 09:08:52 +00003778 const Type *Ty = $2.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003779 $5.S.makeUnsigned();
3780 $$.S.makeComposite($4.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003781 $$.I = new AllocaInst(Ty, getVal($4.T, $5), $6);
Reid Spencered96d1e2007-02-08 09:08:52 +00003782 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003783 }
3784 | FREE ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003785 const Type *PTy = $2.V->getType();
3786 if (!isa<PointerType>(PTy))
3787 error("Trying to free nonpointer type '" + PTy->getDescription() + "'");
3788 $$.I = new FreeInst($2.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003789 $$.S.makeSignless();
Reid Spencere7c3c602006-11-30 06:36:44 +00003790 }
3791 | OptVolatile LOAD Types ValueRef {
Reid Spencered96d1e2007-02-08 09:08:52 +00003792 const Type* Ty = $3.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003793 $4.S.copy($3.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003794 if (!isa<PointerType>(Ty))
3795 error("Can't load from nonpointer type: " + Ty->getDescription());
3796 if (!cast<PointerType>(Ty)->getElementType()->isFirstClassType())
3797 error("Can't load from pointer of non-first-class type: " +
3798 Ty->getDescription());
3799 Value* tmpVal = getVal(Ty, $4);
3800 $$.I = new LoadInst(tmpVal, "", $1);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003801 $$.S.copy($3.S.get(0));
Reid Spencered96d1e2007-02-08 09:08:52 +00003802 delete $3.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003803 }
3804 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003805 $6.S.copy($5.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003806 const PointerType *PTy = dyn_cast<PointerType>($5.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00003807 if (!PTy)
3808 error("Can't store to a nonpointer type: " +
Reid Spencered96d1e2007-02-08 09:08:52 +00003809 $5.PAT->get()->getDescription());
Reid Spencer950bf602007-01-26 08:19:09 +00003810 const Type *ElTy = PTy->getElementType();
Reid Spencered96d1e2007-02-08 09:08:52 +00003811 Value *StoreVal = $3.V;
Reid Spencer950bf602007-01-26 08:19:09 +00003812 Value* tmpVal = getVal(PTy, $6);
Reid Spencered96d1e2007-02-08 09:08:52 +00003813 if (ElTy != $3.V->getType()) {
3814 StoreVal = handleSRetFuncTypeMerge($3.V, ElTy);
3815 if (!StoreVal)
3816 error("Can't store '" + $3.V->getType()->getDescription() +
3817 "' into space of type '" + ElTy->getDescription() + "'");
3818 else {
3819 PTy = PointerType::get(StoreVal->getType());
3820 if (Constant *C = dyn_cast<Constant>(tmpVal))
3821 tmpVal = ConstantExpr::getBitCast(C, PTy);
3822 else
3823 tmpVal = new BitCastInst(tmpVal, PTy, "upgrd.cast", CurBB);
3824 }
3825 }
3826 $$.I = new StoreInst(StoreVal, tmpVal, $1);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003827 $$.S.makeSignless();
Reid Spencered96d1e2007-02-08 09:08:52 +00003828 delete $5.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003829 }
3830 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003831 $3.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003832 const Type* Ty = $2.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003833 if (!isa<PointerType>(Ty))
3834 error("getelementptr insn requires pointer operand");
3835
3836 std::vector<Value*> VIndices;
3837 upgradeGEPIndices(Ty, $4, VIndices);
3838
3839 Value* tmpVal = getVal(Ty, $3);
Chris Lattner1bc3fa62007-02-12 22:58:38 +00003840 $$.I = new GetElementPtrInst(tmpVal, &VIndices[0], VIndices.size());
Reid Spencerbb1fd572007-03-21 17:15:50 +00003841 ValueInfo VI; VI.V = tmpVal; VI.S.copy($2.S);
3842 $$.S.copy(getElementSign(VI, VIndices));
Reid Spencered96d1e2007-02-08 09:08:52 +00003843 delete $2.PAT;
Reid Spencer30d0c582007-01-15 00:26:18 +00003844 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00003845 };
3846
Reid Spencer950bf602007-01-26 08:19:09 +00003847
Reid Spencere7c3c602006-11-30 06:36:44 +00003848%%
3849
3850int yyerror(const char *ErrorMsg) {
3851 std::string where
3852 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Reid Spencered96d1e2007-02-08 09:08:52 +00003853 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
Reid Spencer950bf602007-01-26 08:19:09 +00003854 std::string errMsg = where + "error: " + std::string(ErrorMsg);
3855 if (yychar != YYEMPTY && yychar != 0)
3856 errMsg += " while reading token '" + std::string(Upgradetext, Upgradeleng) +
3857 "'.";
Reid Spencer71d2ec92006-12-31 06:02:26 +00003858 std::cerr << "llvm-upgrade: " << errMsg << '\n';
Reid Spencer950bf602007-01-26 08:19:09 +00003859 std::cout << "llvm-upgrade: parse failed.\n";
Reid Spencere7c3c602006-11-30 06:36:44 +00003860 exit(1);
3861}
Reid Spencer319a7302007-01-05 17:20:02 +00003862
Reid Spencer30d0c582007-01-15 00:26:18 +00003863void warning(const std::string& ErrorMsg) {
Reid Spencer319a7302007-01-05 17:20:02 +00003864 std::string where
3865 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Reid Spencered96d1e2007-02-08 09:08:52 +00003866 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
Reid Spencer950bf602007-01-26 08:19:09 +00003867 std::string errMsg = where + "warning: " + std::string(ErrorMsg);
3868 if (yychar != YYEMPTY && yychar != 0)
3869 errMsg += " while reading token '" + std::string(Upgradetext, Upgradeleng) +
3870 "'.";
Reid Spencer319a7302007-01-05 17:20:02 +00003871 std::cerr << "llvm-upgrade: " << errMsg << '\n';
3872}
Reid Spencer950bf602007-01-26 08:19:09 +00003873
3874void error(const std::string& ErrorMsg, int LineNo) {
3875 if (LineNo == -1) LineNo = Upgradelineno;
3876 Upgradelineno = LineNo;
3877 yyerror(ErrorMsg.c_str());
3878}
3879