blob: 697c7af2f884a261b5bead752d008c2c6b00ecd2 [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 Spencer7b5d4662007-04-09 06:16:21 +000020#include "llvm/ParameterAttributes.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000021#include "llvm/ValueSymbolTable.h"
Reid Spencer950bf602007-01-26 08:19:09 +000022#include "llvm/Support/GetElementPtrTypeIterator.h"
23#include "llvm/ADT/STLExtras.h"
24#include "llvm/Support/MathExtras.h"
Reid Spencere7c3c602006-11-30 06:36:44 +000025#include <algorithm>
Reid Spencere7c3c602006-11-30 06:36:44 +000026#include <iostream>
Chris Lattner8adde282007-02-11 21:40:10 +000027#include <map>
Reid Spencer950bf602007-01-26 08:19:09 +000028#include <list>
29#include <utility>
30
31// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
32// relating to upreferences in the input stream.
33//
34//#define DEBUG_UPREFS 1
35#ifdef DEBUG_UPREFS
36#define UR_OUT(X) std::cerr << X
37#else
38#define UR_OUT(X)
39#endif
Reid Spencere7c3c602006-11-30 06:36:44 +000040
Reid Spencere77e35e2006-12-01 20:26:20 +000041#define YYERROR_VERBOSE 1
Reid Spencer96839be2006-11-30 16:50:26 +000042#define YYINCLUDED_STDLIB_H
Reid Spencere77e35e2006-12-01 20:26:20 +000043#define YYDEBUG 1
Reid Spencere7c3c602006-11-30 06:36:44 +000044
Reid Spencer950bf602007-01-26 08:19:09 +000045int yylex();
Reid Spencere7c3c602006-11-30 06:36:44 +000046int yyparse();
47
Reid Spencer950bf602007-01-26 08:19:09 +000048int yyerror(const char*);
49static void warning(const std::string& WarningMsg);
50
51namespace llvm {
52
Reid Spencer950bf602007-01-26 08:19:09 +000053std::istream* LexInput;
Reid Spencere7c3c602006-11-30 06:36:44 +000054static std::string CurFilename;
Reid Spencer96839be2006-11-30 16:50:26 +000055
Reid Spencer71d2ec92006-12-31 06:02:26 +000056// This bool controls whether attributes are ever added to function declarations
57// definitions and calls.
58static bool AddAttributes = false;
59
Reid Spencer950bf602007-01-26 08:19:09 +000060static Module *ParserResult;
61static bool ObsoleteVarArgs;
62static bool NewVarArgs;
63static BasicBlock *CurBB;
64static GlobalVariable *CurGV;
Reid Spencera50d5962006-12-02 04:11:07 +000065
Reid Spencer950bf602007-01-26 08:19:09 +000066// This contains info used when building the body of a function. It is
67// destroyed when the function is completed.
68//
69typedef std::vector<Value *> ValueList; // Numbered defs
70
Reid Spencerbb1fd572007-03-21 17:15:50 +000071typedef std::pair<std::string,TypeInfo> RenameMapKey;
Reid Spencer950bf602007-01-26 08:19:09 +000072typedef std::map<RenameMapKey,std::string> RenameMapType;
73
74static void
75ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
76 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
77
78static struct PerModuleInfo {
79 Module *CurrentModule;
80 std::map<const Type *, ValueList> Values; // Module level numbered definitions
81 std::map<const Type *,ValueList> LateResolveValues;
Reid Spencerbb1fd572007-03-21 17:15:50 +000082 std::vector<PATypeHolder> Types;
83 std::vector<Signedness> TypeSigns;
84 std::map<std::string,Signedness> NamedTypeSigns;
85 std::map<std::string,Signedness> NamedValueSigns;
Reid Spencer950bf602007-01-26 08:19:09 +000086 std::map<ValID, PATypeHolder> LateResolveTypes;
87 static Module::Endianness Endian;
88 static Module::PointerSize PointerSize;
89 RenameMapType RenameMap;
90
91 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
92 /// how they were referenced and on which line of the input they came from so
93 /// that we can resolve them later and print error messages as appropriate.
94 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
95
96 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
97 // references to global values. Global values may be referenced before they
98 // are defined, and if so, the temporary object that they represent is held
99 // here. This is used for forward references of GlobalValues.
100 //
101 typedef std::map<std::pair<const PointerType *, ValID>, GlobalValue*>
102 GlobalRefsType;
103 GlobalRefsType GlobalRefs;
104
105 void ModuleDone() {
106 // If we could not resolve some functions at function compilation time
107 // (calls to functions before they are defined), resolve them now... Types
108 // are resolved when the constant pool has been completely parsed.
109 //
110 ResolveDefinitions(LateResolveValues);
111
112 // Check to make sure that all global value forward references have been
113 // resolved!
114 //
115 if (!GlobalRefs.empty()) {
116 std::string UndefinedReferences = "Unresolved global references exist:\n";
117
118 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
119 I != E; ++I) {
120 UndefinedReferences += " " + I->first.first->getDescription() + " " +
121 I->first.second.getName() + "\n";
122 }
123 error(UndefinedReferences);
124 return;
125 }
126
127 if (CurrentModule->getDataLayout().empty()) {
128 std::string dataLayout;
129 if (Endian != Module::AnyEndianness)
130 dataLayout.append(Endian == Module::BigEndian ? "E" : "e");
131 if (PointerSize != Module::AnyPointerSize) {
132 if (!dataLayout.empty())
133 dataLayout += "-";
134 dataLayout.append(PointerSize == Module::Pointer64 ?
135 "p:64:64" : "p:32:32");
136 }
137 CurrentModule->setDataLayout(dataLayout);
138 }
139
140 Values.clear(); // Clear out function local definitions
141 Types.clear();
Reid Spencerbb1fd572007-03-21 17:15:50 +0000142 TypeSigns.clear();
143 NamedTypeSigns.clear();
144 NamedValueSigns.clear();
Reid Spencer950bf602007-01-26 08:19:09 +0000145 CurrentModule = 0;
146 }
147
148 // GetForwardRefForGlobal - Check to see if there is a forward reference
149 // for this global. If so, remove it from the GlobalRefs map and return it.
150 // If not, just return null.
151 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
152 // Check to see if there is a forward reference to this global variable...
153 // if there is, eliminate it and patch the reference to use the new def'n.
154 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
155 GlobalValue *Ret = 0;
156 if (I != GlobalRefs.end()) {
157 Ret = I->second;
158 GlobalRefs.erase(I);
159 }
160 return Ret;
161 }
162 void setEndianness(Module::Endianness E) { Endian = E; }
163 void setPointerSize(Module::PointerSize sz) { PointerSize = sz; }
164} CurModule;
165
166Module::Endianness PerModuleInfo::Endian = Module::AnyEndianness;
167Module::PointerSize PerModuleInfo::PointerSize = Module::AnyPointerSize;
168
169static struct PerFunctionInfo {
170 Function *CurrentFunction; // Pointer to current function being created
171
172 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
173 std::map<const Type*, ValueList> LateResolveValues;
174 bool isDeclare; // Is this function a forward declararation?
175 GlobalValue::LinkageTypes Linkage;// Linkage for forward declaration.
176
177 /// BBForwardRefs - When we see forward references to basic blocks, keep
178 /// track of them here.
179 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
180 std::vector<BasicBlock*> NumberedBlocks;
181 RenameMapType RenameMap;
Reid Spencer950bf602007-01-26 08:19:09 +0000182 unsigned NextBBNum;
183
184 inline PerFunctionInfo() {
185 CurrentFunction = 0;
186 isDeclare = false;
187 Linkage = GlobalValue::ExternalLinkage;
188 }
189
190 inline void FunctionStart(Function *M) {
191 CurrentFunction = M;
192 NextBBNum = 0;
193 }
194
195 void FunctionDone() {
196 NumberedBlocks.clear();
197
198 // Any forward referenced blocks left?
199 if (!BBForwardRefs.empty()) {
200 error("Undefined reference to label " +
201 BBForwardRefs.begin()->first->getName());
202 return;
203 }
204
205 // Resolve all forward references now.
206 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
207
208 Values.clear(); // Clear out function local definitions
209 RenameMap.clear();
Reid Spencer950bf602007-01-26 08:19:09 +0000210 CurrentFunction = 0;
211 isDeclare = false;
212 Linkage = GlobalValue::ExternalLinkage;
213 }
214} CurFun; // Info for the current function...
215
216static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
217
Reid Spencerbb1fd572007-03-21 17:15:50 +0000218/// This function is just a utility to make a Key value for the rename map.
219/// The Key is a combination of the name, type, Signedness of the original
220/// value (global/function). This just constructs the key and ensures that
221/// named Signedness values are resolved to the actual Signedness.
222/// @brief Make a key for the RenameMaps
223static RenameMapKey makeRenameMapKey(const std::string &Name, const Type* Ty,
224 const Signedness &Sign) {
225 TypeInfo TI;
226 TI.T = Ty;
227 if (Sign.isNamed())
228 // Don't allow Named Signedness nodes because they won't match. The actual
229 // Signedness must be looked up in the NamedTypeSigns map.
230 TI.S.copy(CurModule.NamedTypeSigns[Sign.getName()]);
231 else
232 TI.S.copy(Sign);
233 return std::make_pair(Name, TI);
234}
235
Reid Spencer950bf602007-01-26 08:19:09 +0000236
237//===----------------------------------------------------------------------===//
238// Code to handle definitions of all the types
239//===----------------------------------------------------------------------===//
240
241static int InsertValue(Value *V,
242 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
243 if (V->hasName()) return -1; // Is this a numbered definition?
244
245 // Yes, insert the value into the value table...
246 ValueList &List = ValueTab[V->getType()];
247 List.push_back(V);
248 return List.size()-1;
249}
250
Reid Spencerd7c4f8c2007-01-26 19:59:25 +0000251static const Type *getType(const ValID &D, bool DoNotImprovise = false) {
Reid Spencer950bf602007-01-26 08:19:09 +0000252 switch (D.Type) {
253 case ValID::NumberVal: // Is it a numbered definition?
254 // Module constants occupy the lowest numbered slots...
255 if ((unsigned)D.Num < CurModule.Types.size()) {
256 return CurModule.Types[(unsigned)D.Num];
257 }
258 break;
259 case ValID::NameVal: // Is it a named definition?
260 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
Reid Spencer950bf602007-01-26 08:19:09 +0000261 return N;
262 }
263 break;
264 default:
265 error("Internal parser error: Invalid symbol type reference");
266 return 0;
267 }
268
269 // If we reached here, we referenced either a symbol that we don't know about
270 // or an id number that hasn't been read yet. We may be referencing something
271 // forward, so just create an entry to be resolved later and get to it...
272 //
273 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
274
Reid Spencer950bf602007-01-26 08:19:09 +0000275 if (inFunctionScope()) {
276 if (D.Type == ValID::NameVal) {
277 error("Reference to an undefined type: '" + D.getName() + "'");
278 return 0;
279 } else {
280 error("Reference to an undefined type: #" + itostr(D.Num));
281 return 0;
282 }
283 }
284
285 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
286 if (I != CurModule.LateResolveTypes.end())
287 return I->second;
288
289 Type *Typ = OpaqueType::get();
290 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
291 return Typ;
Reid Spencerbb1fd572007-03-21 17:15:50 +0000292}
293
294/// This is like the getType method except that instead of looking up the type
295/// for a given ID, it looks up that type's sign.
296/// @brief Get the signedness of a referenced type
297static Signedness getTypeSign(const ValID &D) {
298 switch (D.Type) {
299 case ValID::NumberVal: // Is it a numbered definition?
300 // Module constants occupy the lowest numbered slots...
301 if ((unsigned)D.Num < CurModule.TypeSigns.size()) {
302 return CurModule.TypeSigns[(unsigned)D.Num];
303 }
304 break;
305 case ValID::NameVal: { // Is it a named definition?
306 std::map<std::string,Signedness>::const_iterator I =
307 CurModule.NamedTypeSigns.find(D.Name);
308 if (I != CurModule.NamedTypeSigns.end())
309 return I->second;
310 // Perhaps its a named forward .. just cache the name
311 Signedness S;
312 S.makeNamed(D.Name);
313 return S;
314 }
315 default:
316 break;
317 }
318 // If we don't find it, its signless
319 Signedness S;
320 S.makeSignless();
321 return S;
322}
323
324/// This function is analagous to getElementType in LLVM. It provides the same
325/// function except that it looks up the Signedness instead of the type. This is
326/// used when processing GEP instructions that need to extract the type of an
327/// indexed struct/array/ptr member.
328/// @brief Look up an element's sign.
329static Signedness getElementSign(const ValueInfo& VI,
330 const std::vector<Value*> &Indices) {
331 const Type *Ptr = VI.V->getType();
332 assert(isa<PointerType>(Ptr) && "Need pointer type");
333
334 unsigned CurIdx = 0;
335 Signedness S(VI.S);
336 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
337 if (CurIdx == Indices.size())
338 break;
339
340 Value *Index = Indices[CurIdx++];
341 assert(!isa<PointerType>(CT) || CurIdx == 1 && "Invalid type");
342 Ptr = CT->getTypeAtIndex(Index);
343 if (const Type* Ty = Ptr->getForwardedType())
344 Ptr = Ty;
345 assert(S.isComposite() && "Bad Signedness type");
346 if (isa<StructType>(CT)) {
347 S = S.get(cast<ConstantInt>(Index)->getZExtValue());
348 } else {
349 S = S.get(0UL);
350 }
351 if (S.isNamed())
352 S = CurModule.NamedTypeSigns[S.getName()];
353 }
354 Signedness Result;
355 Result.makeComposite(S);
356 return Result;
357}
358
359/// This function just translates a ConstantInfo into a ValueInfo and calls
360/// getElementSign(ValueInfo,...). Its just a convenience.
361/// @brief ConstantInfo version of getElementSign.
362static Signedness getElementSign(const ConstInfo& CI,
363 const std::vector<Constant*> &Indices) {
364 ValueInfo VI;
365 VI.V = CI.C;
366 VI.S.copy(CI.S);
367 std::vector<Value*> Idx;
368 for (unsigned i = 0; i < Indices.size(); ++i)
369 Idx.push_back(Indices[i]);
370 Signedness result = getElementSign(VI, Idx);
371 VI.destroy();
372 return result;
373}
Reid Spencer950bf602007-01-26 08:19:09 +0000374
Reid Spencered96d1e2007-02-08 09:08:52 +0000375/// This function determines if two function types differ only in their use of
376/// the sret parameter attribute in the first argument. If they are identical
377/// in all other respects, it returns true. Otherwise, it returns false.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000378static bool FuncTysDifferOnlyBySRet(const FunctionType *F1,
379 const FunctionType *F2) {
Reid Spencered96d1e2007-02-08 09:08:52 +0000380 if (F1->getReturnType() != F2->getReturnType() ||
Reid Spencer7b5d4662007-04-09 06:16:21 +0000381 F1->getNumParams() != F2->getNumParams())
Reid Spencered96d1e2007-02-08 09:08:52 +0000382 return false;
Reid Spencer7b5d4662007-04-09 06:16:21 +0000383 ParamAttrsList PAL1;
384 if (F1->getParamAttrs())
385 PAL1 = *F1->getParamAttrs();
386 ParamAttrsList PAL2;
387 if (F2->getParamAttrs())
388 PAL2 = *F2->getParamAttrs();
389 if (PAL1.getParamAttrs(0) != PAL2.getParamAttrs(0))
390 return false;
Reid Spencer18da0722007-04-11 02:44:20 +0000391 unsigned SRetMask = ~unsigned(ParamAttr::StructRet);
Reid Spencered96d1e2007-02-08 09:08:52 +0000392 for (unsigned i = 0; i < F1->getNumParams(); ++i) {
393 if (F1->getParamType(i) != F2->getParamType(i) ||
Reid Spencer7b5d4662007-04-09 06:16:21 +0000394 unsigned(PAL1.getParamAttrs(i+1)) & SRetMask !=
395 unsigned(PAL2.getParamAttrs(i+1)) & SRetMask)
Reid Spencered96d1e2007-02-08 09:08:52 +0000396 return false;
397 }
398 return true;
399}
400
Reid Spencerbb1fd572007-03-21 17:15:50 +0000401/// This function determines if the type of V and Ty differ only by the SRet
402/// parameter attribute. This is a more generalized case of
403/// FuncTysDIfferOnlyBySRet since it doesn't require FunctionType arguments.
404static bool TypesDifferOnlyBySRet(Value *V, const Type* Ty) {
405 if (V->getType() == Ty)
406 return true;
407 const PointerType *PF1 = dyn_cast<PointerType>(Ty);
408 const PointerType *PF2 = dyn_cast<PointerType>(V->getType());
409 if (PF1 && PF2) {
410 const FunctionType* FT1 = dyn_cast<FunctionType>(PF1->getElementType());
411 const FunctionType* FT2 = dyn_cast<FunctionType>(PF2->getElementType());
412 if (FT1 && FT2)
413 return FuncTysDifferOnlyBySRet(FT1, FT2);
414 }
415 return false;
416}
417
Reid Spencered96d1e2007-02-08 09:08:52 +0000418// The upgrade of csretcc to sret param attribute may have caused a function
419// to not be found because the param attribute changed the type of the called
420// function. This helper function, used in getExistingValue, detects that
Reid Spencerbb1fd572007-03-21 17:15:50 +0000421// situation and bitcasts the function to the correct type.
Reid Spencered96d1e2007-02-08 09:08:52 +0000422static Value* handleSRetFuncTypeMerge(Value *V, const Type* Ty) {
423 // Handle degenerate cases
424 if (!V)
425 return 0;
426 if (V->getType() == Ty)
427 return V;
428
Reid Spencered96d1e2007-02-08 09:08:52 +0000429 const PointerType *PF1 = dyn_cast<PointerType>(Ty);
430 const PointerType *PF2 = dyn_cast<PointerType>(V->getType());
431 if (PF1 && PF2) {
Reid Spencerbb1fd572007-03-21 17:15:50 +0000432 const FunctionType *FT1 = dyn_cast<FunctionType>(PF1->getElementType());
433 const FunctionType *FT2 = dyn_cast<FunctionType>(PF2->getElementType());
Reid Spencer7b5d4662007-04-09 06:16:21 +0000434 if (FT1 && FT2 && FuncTysDifferOnlyBySRet(FT1, FT2)) {
435 const ParamAttrsList *PAL2 = FT2->getParamAttrs();
Reid Spencer18da0722007-04-11 02:44:20 +0000436 if (PAL2 && PAL2->paramHasAttr(1, ParamAttr::StructRet))
Reid Spencerbb1fd572007-03-21 17:15:50 +0000437 return V;
Reid Spencered96d1e2007-02-08 09:08:52 +0000438 else if (Constant *C = dyn_cast<Constant>(V))
Reid Spencerbb1fd572007-03-21 17:15:50 +0000439 return ConstantExpr::getBitCast(C, PF1);
Reid Spencered96d1e2007-02-08 09:08:52 +0000440 else
Reid Spencerbb1fd572007-03-21 17:15:50 +0000441 return new BitCastInst(V, PF1, "upgrd.cast", CurBB);
Reid Spencer7b5d4662007-04-09 06:16:21 +0000442 }
Reid Spencerbb1fd572007-03-21 17:15:50 +0000443
Reid Spencered96d1e2007-02-08 09:08:52 +0000444 }
Reid Spencerbb1fd572007-03-21 17:15:50 +0000445 return 0;
Reid Spencered96d1e2007-02-08 09:08:52 +0000446}
447
Reid Spencer950bf602007-01-26 08:19:09 +0000448// getExistingValue - Look up the value specified by the provided type and
449// the provided ValID. If the value exists and has already been defined, return
450// it. Otherwise return null.
451//
452static Value *getExistingValue(const Type *Ty, const ValID &D) {
453 if (isa<FunctionType>(Ty)) {
454 error("Functions are not values and must be referenced as pointers");
455 }
456
457 switch (D.Type) {
458 case ValID::NumberVal: { // Is it a numbered definition?
459 unsigned Num = (unsigned)D.Num;
460
461 // Module constants occupy the lowest numbered slots...
462 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
463 if (VI != CurModule.Values.end()) {
464 if (Num < VI->second.size())
465 return VI->second[Num];
466 Num -= VI->second.size();
467 }
468
469 // Make sure that our type is within bounds
470 VI = CurFun.Values.find(Ty);
471 if (VI == CurFun.Values.end()) return 0;
472
473 // Check that the number is within bounds...
474 if (VI->second.size() <= Num) return 0;
475
476 return VI->second[Num];
477 }
478
479 case ValID::NameVal: { // Is it a named definition?
480 // Get the name out of the ID
Reid Spencerbb1fd572007-03-21 17:15:50 +0000481 RenameMapKey Key = makeRenameMapKey(D.Name, Ty, D.S);
482 Value *V = 0;
Reid Spencer950bf602007-01-26 08:19:09 +0000483 if (inFunctionScope()) {
484 // See if the name was renamed
485 RenameMapType::const_iterator I = CurFun.RenameMap.find(Key);
486 std::string LookupName;
487 if (I != CurFun.RenameMap.end())
488 LookupName = I->second;
489 else
Reid Spencerbb1fd572007-03-21 17:15:50 +0000490 LookupName = D.Name;
Reid Spenceref9b9a72007-02-05 20:47:22 +0000491 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
492 V = SymTab.lookup(LookupName);
Reid Spencerbb1fd572007-03-21 17:15:50 +0000493 if (V && V->getType() != Ty)
494 V = handleSRetFuncTypeMerge(V, Ty);
495 assert((!V || TypesDifferOnlyBySRet(V, Ty)) && "Found wrong type");
Reid Spencer950bf602007-01-26 08:19:09 +0000496 }
497 if (!V) {
498 RenameMapType::const_iterator I = CurModule.RenameMap.find(Key);
499 std::string LookupName;
500 if (I != CurModule.RenameMap.end())
501 LookupName = I->second;
502 else
Reid Spencerbb1fd572007-03-21 17:15:50 +0000503 LookupName = D.Name;
Reid Spenceref9b9a72007-02-05 20:47:22 +0000504 V = CurModule.CurrentModule->getValueSymbolTable().lookup(LookupName);
Reid Spencerbb1fd572007-03-21 17:15:50 +0000505 if (V && V->getType() != Ty)
506 V = handleSRetFuncTypeMerge(V, Ty);
507 assert((!V || TypesDifferOnlyBySRet(V, Ty)) && "Found wrong type");
Reid Spencer950bf602007-01-26 08:19:09 +0000508 }
Reid Spenceref9b9a72007-02-05 20:47:22 +0000509 if (!V)
Reid Spencer950bf602007-01-26 08:19:09 +0000510 return 0;
511
512 D.destroy(); // Free old strdup'd memory...
513 return V;
514 }
515
516 // Check to make sure that "Ty" is an integral type, and that our
517 // value will fit into the specified type...
518 case ValID::ConstSIntVal: // Is it a constant pool reference??
519 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
520 error("Signed integral constant '" + itostr(D.ConstPool64) +
521 "' is invalid for type '" + Ty->getDescription() + "'");
522 }
523 return ConstantInt::get(Ty, D.ConstPool64);
524
525 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
526 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
527 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64))
528 error("Integral constant '" + utostr(D.UConstPool64) +
529 "' is invalid or out of range");
530 else // This is really a signed reference. Transmogrify.
531 return ConstantInt::get(Ty, D.ConstPool64);
532 } else
533 return ConstantInt::get(Ty, D.UConstPool64);
534
535 case ValID::ConstFPVal: // Is it a floating point const pool reference?
536 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
537 error("FP constant invalid for type");
538 return ConstantFP::get(Ty, D.ConstPoolFP);
539
540 case ValID::ConstNullVal: // Is it a null value?
541 if (!isa<PointerType>(Ty))
542 error("Cannot create a a non pointer null");
543 return ConstantPointerNull::get(cast<PointerType>(Ty));
544
545 case ValID::ConstUndefVal: // Is it an undef value?
546 return UndefValue::get(Ty);
547
548 case ValID::ConstZeroVal: // Is it a zero value?
549 return Constant::getNullValue(Ty);
550
551 case ValID::ConstantVal: // Fully resolved constant?
552 if (D.ConstantValue->getType() != Ty)
553 error("Constant expression type different from required type");
554 return D.ConstantValue;
555
556 case ValID::InlineAsmVal: { // Inline asm expression
557 const PointerType *PTy = dyn_cast<PointerType>(Ty);
558 const FunctionType *FTy =
559 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
560 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints))
561 error("Invalid type for asm constraint string");
562 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
563 D.IAD->HasSideEffects);
564 D.destroy(); // Free InlineAsmDescriptor.
565 return IA;
566 }
567 default:
568 assert(0 && "Unhandled case");
569 return 0;
570 } // End of switch
571
572 assert(0 && "Unhandled case");
573 return 0;
574}
575
576// getVal - This function is identical to getExistingValue, except that if a
577// value is not already defined, it "improvises" by creating a placeholder var
578// that looks and acts just like the requested variable. When the value is
579// defined later, all uses of the placeholder variable are replaced with the
580// real thing.
581//
582static Value *getVal(const Type *Ty, const ValID &ID) {
583 if (Ty == Type::LabelTy)
584 error("Cannot use a basic block here");
585
586 // See if the value has already been defined.
587 Value *V = getExistingValue(Ty, ID);
588 if (V) return V;
589
590 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty))
591 error("Invalid use of a composite type");
592
593 // If we reached here, we referenced either a symbol that we don't know about
594 // or an id number that hasn't been read yet. We may be referencing something
595 // forward, so just create an entry to be resolved later and get to it...
Reid Spencer950bf602007-01-26 08:19:09 +0000596 V = new Argument(Ty);
597
598 // Remember where this forward reference came from. FIXME, shouldn't we try
599 // to recycle these things??
600 CurModule.PlaceHolderInfo.insert(
Reid Spenceref9b9a72007-02-05 20:47:22 +0000601 std::make_pair(V, std::make_pair(ID, Upgradelineno)));
Reid Spencer950bf602007-01-26 08:19:09 +0000602
603 if (inFunctionScope())
604 InsertValue(V, CurFun.LateResolveValues);
605 else
606 InsertValue(V, CurModule.LateResolveValues);
607 return V;
608}
609
Reid Spencered96d1e2007-02-08 09:08:52 +0000610/// @brief This just makes any name given to it unique, up to MAX_UINT times.
611static std::string makeNameUnique(const std::string& Name) {
612 static unsigned UniqueNameCounter = 1;
613 std::string Result(Name);
614 Result += ".upgrd." + llvm::utostr(UniqueNameCounter++);
615 return Result;
616}
617
Reid Spencer950bf602007-01-26 08:19:09 +0000618/// getBBVal - This is used for two purposes:
619/// * If isDefinition is true, a new basic block with the specified ID is being
620/// defined.
621/// * If isDefinition is true, this is a reference to a basic block, which may
622/// or may not be a forward reference.
623///
624static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
625 assert(inFunctionScope() && "Can't get basic block at global scope");
626
627 std::string Name;
628 BasicBlock *BB = 0;
629 switch (ID.Type) {
630 default:
631 error("Illegal label reference " + ID.getName());
632 break;
633 case ValID::NumberVal: // Is it a numbered definition?
634 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
635 CurFun.NumberedBlocks.resize(ID.Num+1);
636 BB = CurFun.NumberedBlocks[ID.Num];
637 break;
638 case ValID::NameVal: // Is it a named definition?
639 Name = ID.Name;
Reid Spencerbb1fd572007-03-21 17:15:50 +0000640 if (Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name)) {
Reid Spencered96d1e2007-02-08 09:08:52 +0000641 if (N->getType() != Type::LabelTy) {
642 // Register names didn't use to conflict with basic block names
643 // because of type planes. Now they all have to be unique. So, we just
644 // rename the register and treat this name as if no basic block
645 // had been found.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000646 RenameMapKey Key = makeRenameMapKey(ID.Name, N->getType(), ID.S);
Reid Spencered96d1e2007-02-08 09:08:52 +0000647 N->setName(makeNameUnique(N->getName()));
648 CurModule.RenameMap[Key] = N->getName();
649 BB = 0;
650 } else {
651 BB = cast<BasicBlock>(N);
652 }
Reid Spencer950bf602007-01-26 08:19:09 +0000653 }
654 break;
655 }
656
657 // See if the block has already been defined.
658 if (BB) {
659 // If this is the definition of the block, make sure the existing value was
660 // just a forward reference. If it was a forward reference, there will be
661 // an entry for it in the PlaceHolderInfo map.
662 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
663 // The existing value was a definition, not a forward reference.
664 error("Redefinition of label " + ID.getName());
665
666 ID.destroy(); // Free strdup'd memory.
667 return BB;
668 }
669
670 // Otherwise this block has not been seen before.
671 BB = new BasicBlock("", CurFun.CurrentFunction);
672 if (ID.Type == ValID::NameVal) {
673 BB->setName(ID.Name);
674 } else {
675 CurFun.NumberedBlocks[ID.Num] = BB;
676 }
677
678 // If this is not a definition, keep track of it so we can use it as a forward
679 // reference.
680 if (!isDefinition) {
681 // Remember where this forward reference came from.
682 CurFun.BBForwardRefs[BB] = std::make_pair(ID, Upgradelineno);
683 } else {
684 // The forward declaration could have been inserted anywhere in the
685 // function: insert it into the correct place now.
686 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
687 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
688 }
689 ID.destroy();
690 return BB;
691}
692
693
694//===----------------------------------------------------------------------===//
695// Code to handle forward references in instructions
696//===----------------------------------------------------------------------===//
697//
698// This code handles the late binding needed with statements that reference
699// values not defined yet... for example, a forward branch, or the PHI node for
700// a loop body.
701//
702// This keeps a table (CurFun.LateResolveValues) of all such forward references
703// and back patchs after we are done.
704//
705
706// ResolveDefinitions - If we could not resolve some defs at parsing
707// time (forward branches, phi functions for loops, etc...) resolve the
708// defs now...
709//
710static void
711ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
712 std::map<const Type*,ValueList> *FutureLateResolvers) {
Reid Spencered96d1e2007-02-08 09:08:52 +0000713
Reid Spencer950bf602007-01-26 08:19:09 +0000714 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
715 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
716 E = LateResolvers.end(); LRI != E; ++LRI) {
Reid Spencered96d1e2007-02-08 09:08:52 +0000717 const Type* Ty = LRI->first;
Reid Spencer950bf602007-01-26 08:19:09 +0000718 ValueList &List = LRI->second;
719 while (!List.empty()) {
720 Value *V = List.back();
721 List.pop_back();
722
723 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
724 CurModule.PlaceHolderInfo.find(V);
725 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error");
726
727 ValID &DID = PHI->second.first;
728
Reid Spencered96d1e2007-02-08 09:08:52 +0000729 Value *TheRealValue = getExistingValue(Ty, DID);
Reid Spencer950bf602007-01-26 08:19:09 +0000730 if (TheRealValue) {
731 V->replaceAllUsesWith(TheRealValue);
732 delete V;
733 CurModule.PlaceHolderInfo.erase(PHI);
734 } else if (FutureLateResolvers) {
735 // Functions have their unresolved items forwarded to the module late
736 // resolver table
737 InsertValue(V, *FutureLateResolvers);
738 } else {
739 if (DID.Type == ValID::NameVal) {
Reid Spencered96d1e2007-02-08 09:08:52 +0000740 error("Reference to an invalid definition: '" + DID.getName() +
741 "' of type '" + V->getType()->getDescription() + "'",
742 PHI->second.second);
Reid Spencer7de2e012007-01-29 19:08:46 +0000743 return;
Reid Spencer950bf602007-01-26 08:19:09 +0000744 } else {
745 error("Reference to an invalid definition: #" +
746 itostr(DID.Num) + " of type '" +
747 V->getType()->getDescription() + "'", PHI->second.second);
748 return;
749 }
750 }
751 }
752 }
753
754 LateResolvers.clear();
755}
756
Reid Spencerbb1fd572007-03-21 17:15:50 +0000757/// This function is used for type resolution and upref handling. When a type
758/// becomes concrete, this function is called to adjust the signedness for the
759/// concrete type.
760static void ResolveTypeSign(const Type* oldTy, const Signedness &Sign) {
761 std::string TyName = CurModule.CurrentModule->getTypeName(oldTy);
762 if (!TyName.empty())
763 CurModule.NamedTypeSigns[TyName] = Sign;
764}
765
766/// ResolveTypeTo - A brand new type was just declared. This means that (if
767/// name is not null) things referencing Name can be resolved. Otherwise,
768/// things refering to the number can be resolved. Do this now.
769static void ResolveTypeTo(char *Name, const Type *ToTy, const Signedness& Sign){
Reid Spencer950bf602007-01-26 08:19:09 +0000770 ValID D;
Reid Spencerbb1fd572007-03-21 17:15:50 +0000771 if (Name)
772 D = ValID::create(Name);
773 else
774 D = ValID::create((int)CurModule.Types.size());
775 D.S.copy(Sign);
776
Reid Spencerbaba98a2007-04-11 12:10:58 +0000777 if (Name)
778 CurModule.NamedTypeSigns[Name] = Sign;
Reid Spencer950bf602007-01-26 08:19:09 +0000779
780 std::map<ValID, PATypeHolder>::iterator I =
781 CurModule.LateResolveTypes.find(D);
782 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencerbb1fd572007-03-21 17:15:50 +0000783 const Type *OldTy = I->second.get();
784 ((DerivedType*)OldTy)->refineAbstractTypeTo(ToTy);
Reid Spencer950bf602007-01-26 08:19:09 +0000785 CurModule.LateResolveTypes.erase(I);
786 }
787}
788
Anton Korobeynikovce13b852007-01-28 15:25:24 +0000789/// This is the implementation portion of TypeHasInteger. It traverses the
790/// type given, avoiding recursive types, and returns true as soon as it finds
791/// an integer type. If no integer type is found, it returns false.
792static bool TypeHasIntegerI(const Type *Ty, std::vector<const Type*> Stack) {
793 // Handle some easy cases
794 if (Ty->isPrimitiveType() || (Ty->getTypeID() == Type::OpaqueTyID))
795 return false;
796 if (Ty->isInteger())
797 return true;
798 if (const SequentialType *STy = dyn_cast<SequentialType>(Ty))
799 return STy->getElementType()->isInteger();
800
801 // Avoid type structure recursion
802 for (std::vector<const Type*>::iterator I = Stack.begin(), E = Stack.end();
803 I != E; ++I)
804 if (Ty == *I)
805 return false;
806
807 // Push us on the type stack
808 Stack.push_back(Ty);
809
810 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
811 if (TypeHasIntegerI(FTy->getReturnType(), Stack))
812 return true;
813 FunctionType::param_iterator I = FTy->param_begin();
814 FunctionType::param_iterator E = FTy->param_end();
815 for (; I != E; ++I)
816 if (TypeHasIntegerI(*I, Stack))
817 return true;
818 return false;
819 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
820 StructType::element_iterator I = STy->element_begin();
821 StructType::element_iterator E = STy->element_end();
822 for (; I != E; ++I) {
823 if (TypeHasIntegerI(*I, Stack))
824 return true;
825 }
826 return false;
827 }
828 // There shouldn't be anything else, but its definitely not integer
829 assert(0 && "What type is this?");
830 return false;
831}
832
833/// This is the interface to TypeHasIntegerI. It just provides the type stack,
834/// to avoid recursion, and then calls TypeHasIntegerI.
835static inline bool TypeHasInteger(const Type *Ty) {
836 std::vector<const Type*> TyStack;
837 return TypeHasIntegerI(Ty, TyStack);
838}
839
Reid Spencer950bf602007-01-26 08:19:09 +0000840// setValueName - Set the specified value to the name given. The name may be
841// null potentially, in which case this is a noop. The string passed in is
842// assumed to be a malloc'd string buffer, and is free'd by this function.
843//
Reid Spencerbb1fd572007-03-21 17:15:50 +0000844static void setValueName(const ValueInfo &V, char *NameStr) {
Reid Spencer950bf602007-01-26 08:19:09 +0000845 if (NameStr) {
846 std::string Name(NameStr); // Copy string
847 free(NameStr); // Free old string
848
Reid Spencerbb1fd572007-03-21 17:15:50 +0000849 if (V.V->getType() == Type::VoidTy) {
Reid Spencer950bf602007-01-26 08:19:09 +0000850 error("Can't assign name '" + Name + "' to value with void type");
851 return;
852 }
853
Reid Spencer950bf602007-01-26 08:19:09 +0000854 assert(inFunctionScope() && "Must be in function scope");
855
856 // Search the function's symbol table for an existing value of this name
Reid Spenceref9b9a72007-02-05 20:47:22 +0000857 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
858 Value* Existing = ST.lookup(Name);
Reid Spencer950bf602007-01-26 08:19:09 +0000859 if (Existing) {
Anton Korobeynikovce13b852007-01-28 15:25:24 +0000860 // An existing value of the same name was found. This might have happened
861 // because of the integer type planes collapsing in LLVM 2.0.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000862 if (Existing->getType() == V.V->getType() &&
Anton Korobeynikovce13b852007-01-28 15:25:24 +0000863 !TypeHasInteger(Existing->getType())) {
864 // If the type does not contain any integers in them then this can't be
865 // a type plane collapsing issue. It truly is a redefinition and we
866 // should error out as the assembly is invalid.
867 error("Redefinition of value named '" + Name + "' of type '" +
Reid Spencerbb1fd572007-03-21 17:15:50 +0000868 V.V->getType()->getDescription() + "'");
Anton Korobeynikovce13b852007-01-28 15:25:24 +0000869 return;
Reid Spencer950bf602007-01-26 08:19:09 +0000870 }
871 // In LLVM 2.0 we don't allow names to be re-used for any values in a
872 // function, regardless of Type. Previously re-use of names was okay as
873 // long as they were distinct types. With type planes collapsing because
874 // of the signedness change and because of PR411, this can no longer be
875 // supported. We must search the entire symbol table for a conflicting
876 // name and make the name unique. No warning is needed as this can't
877 // cause a problem.
878 std::string NewName = makeNameUnique(Name);
879 // We're changing the name but it will probably be used by other
880 // instructions as operands later on. Consequently we have to retain
881 // a mapping of the renaming that we're doing.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000882 RenameMapKey Key = makeRenameMapKey(Name, V.V->getType(), V.S);
Reid Spencer950bf602007-01-26 08:19:09 +0000883 CurFun.RenameMap[Key] = NewName;
884 Name = NewName;
885 }
886
887 // Set the name.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000888 V.V->setName(Name);
Reid Spencer950bf602007-01-26 08:19:09 +0000889 }
890}
891
892/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
893/// this is a declaration, otherwise it is a definition.
894static GlobalVariable *
895ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
896 bool isConstantGlobal, const Type *Ty,
Reid Spencerbb1fd572007-03-21 17:15:50 +0000897 Constant *Initializer,
898 const Signedness &Sign) {
Reid Spencer950bf602007-01-26 08:19:09 +0000899 if (isa<FunctionType>(Ty))
900 error("Cannot declare global vars of function type");
901
902 const PointerType *PTy = PointerType::get(Ty);
903
904 std::string Name;
905 if (NameStr) {
906 Name = NameStr; // Copy string
907 free(NameStr); // Free old string
908 }
909
910 // See if this global value was forward referenced. If so, recycle the
911 // object.
912 ValID ID;
913 if (!Name.empty()) {
Reid Spencer5eb77c72007-03-15 03:26:42 +0000914 ID = ValID::create((char*)Name.c_str());
Reid Spencer950bf602007-01-26 08:19:09 +0000915 } else {
Reid Spencer5eb77c72007-03-15 03:26:42 +0000916 ID = ValID::create((int)CurModule.Values[PTy].size());
Reid Spencer950bf602007-01-26 08:19:09 +0000917 }
Reid Spencerbb1fd572007-03-21 17:15:50 +0000918 ID.S.makeComposite(Sign);
Reid Spencer950bf602007-01-26 08:19:09 +0000919
920 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
921 // Move the global to the end of the list, from whereever it was
922 // previously inserted.
923 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
924 CurModule.CurrentModule->getGlobalList().remove(GV);
925 CurModule.CurrentModule->getGlobalList().push_back(GV);
926 GV->setInitializer(Initializer);
927 GV->setLinkage(Linkage);
928 GV->setConstant(isConstantGlobal);
929 InsertValue(GV, CurModule.Values);
930 return GV;
931 }
932
933 // If this global has a name, check to see if there is already a definition
934 // of this global in the module and emit warnings if there are conflicts.
935 if (!Name.empty()) {
936 // The global has a name. See if there's an existing one of the same name.
937 if (CurModule.CurrentModule->getNamedGlobal(Name)) {
938 // We found an existing global ov the same name. This isn't allowed
939 // in LLVM 2.0. Consequently, we must alter the name of the global so it
940 // can at least compile. This can happen because of type planes
941 // There is alread a global of the same name which means there is a
942 // conflict. Let's see what we can do about it.
943 std::string NewName(makeNameUnique(Name));
Reid Spencerbb1fd572007-03-21 17:15:50 +0000944 if (Linkage != GlobalValue::InternalLinkage) {
Reid Spencer950bf602007-01-26 08:19:09 +0000945 // The linkage of this gval is external so we can't reliably rename
946 // it because it could potentially create a linking problem.
947 // However, we can't leave the name conflict in the output either or
948 // it won't assemble with LLVM 2.0. So, all we can do is rename
949 // this one to something unique and emit a warning about the problem.
950 warning("Renaming global variable '" + Name + "' to '" + NewName +
951 "' may cause linkage errors");
952 }
953
954 // Put the renaming in the global rename map
Reid Spencerbb1fd572007-03-21 17:15:50 +0000955 RenameMapKey Key = makeRenameMapKey(Name, PointerType::get(Ty), ID.S);
Reid Spencer950bf602007-01-26 08:19:09 +0000956 CurModule.RenameMap[Key] = NewName;
957
958 // Rename it
959 Name = NewName;
960 }
961 }
962
963 // Otherwise there is no existing GV to use, create one now.
964 GlobalVariable *GV =
965 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
966 CurModule.CurrentModule);
967 InsertValue(GV, CurModule.Values);
Reid Spencerbb1fd572007-03-21 17:15:50 +0000968 // Remember the sign of this global.
969 CurModule.NamedValueSigns[Name] = ID.S;
Reid Spencer950bf602007-01-26 08:19:09 +0000970 return GV;
971}
972
973// setTypeName - Set the specified type to the name given. The name may be
974// null potentially, in which case this is a noop. The string passed in is
975// assumed to be a malloc'd string buffer, and is freed by this function.
976//
977// This function returns true if the type has already been defined, but is
978// allowed to be redefined in the specified context. If the name is a new name
979// for the type plane, it is inserted and false is returned.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000980static bool setTypeName(const PATypeInfo& TI, char *NameStr) {
Reid Spencer950bf602007-01-26 08:19:09 +0000981 assert(!inFunctionScope() && "Can't give types function-local names");
982 if (NameStr == 0) return false;
983
984 std::string Name(NameStr); // Copy string
985 free(NameStr); // Free old string
986
Reid Spencerbb1fd572007-03-21 17:15:50 +0000987 const Type* Ty = TI.PAT->get();
988
Reid Spencer950bf602007-01-26 08:19:09 +0000989 // We don't allow assigning names to void type
Reid Spencerbb1fd572007-03-21 17:15:50 +0000990 if (Ty == Type::VoidTy) {
Reid Spencer950bf602007-01-26 08:19:09 +0000991 error("Can't assign name '" + Name + "' to the void type");
992 return false;
993 }
994
995 // Set the type name, checking for conflicts as we do so.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000996 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, Ty);
997
998 // Save the sign information for later use
999 CurModule.NamedTypeSigns[Name] = TI.S;
Reid Spencer950bf602007-01-26 08:19:09 +00001000
1001 if (AlreadyExists) { // Inserting a name that is already defined???
1002 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
1003 assert(Existing && "Conflict but no matching type?");
1004
1005 // There is only one case where this is allowed: when we are refining an
1006 // opaque type. In this case, Existing will be an opaque type.
1007 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
1008 // We ARE replacing an opaque type!
Reid Spencerbb1fd572007-03-21 17:15:50 +00001009 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(Ty);
Reid Spencer950bf602007-01-26 08:19:09 +00001010 return true;
1011 }
1012
1013 // Otherwise, this is an attempt to redefine a type. That's okay if
1014 // the redefinition is identical to the original. This will be so if
1015 // Existing and T point to the same Type object. In this one case we
1016 // allow the equivalent redefinition.
Reid Spencerbb1fd572007-03-21 17:15:50 +00001017 if (Existing == Ty) return true; // Yes, it's equal.
Reid Spencer950bf602007-01-26 08:19:09 +00001018
1019 // Any other kind of (non-equivalent) redefinition is an error.
1020 error("Redefinition of type named '" + Name + "' in the '" +
Reid Spencerbb1fd572007-03-21 17:15:50 +00001021 Ty->getDescription() + "' type plane");
Reid Spencer950bf602007-01-26 08:19:09 +00001022 }
1023
1024 return false;
1025}
1026
1027//===----------------------------------------------------------------------===//
1028// Code for handling upreferences in type names...
1029//
1030
1031// TypeContains - Returns true if Ty directly contains E in it.
1032//
1033static bool TypeContains(const Type *Ty, const Type *E) {
1034 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
1035 E) != Ty->subtype_end();
1036}
1037
1038namespace {
1039 struct UpRefRecord {
1040 // NestingLevel - The number of nesting levels that need to be popped before
1041 // this type is resolved.
1042 unsigned NestingLevel;
1043
1044 // LastContainedTy - This is the type at the current binding level for the
1045 // type. Every time we reduce the nesting level, this gets updated.
1046 const Type *LastContainedTy;
1047
1048 // UpRefTy - This is the actual opaque type that the upreference is
1049 // represented with.
1050 OpaqueType *UpRefTy;
1051
1052 UpRefRecord(unsigned NL, OpaqueType *URTy)
Reid Spencerbb1fd572007-03-21 17:15:50 +00001053 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) { }
Reid Spencer950bf602007-01-26 08:19:09 +00001054 };
1055}
1056
1057// UpRefs - A list of the outstanding upreferences that need to be resolved.
1058static std::vector<UpRefRecord> UpRefs;
1059
1060/// HandleUpRefs - Every time we finish a new layer of types, this function is
1061/// called. It loops through the UpRefs vector, which is a list of the
1062/// currently active types. For each type, if the up reference is contained in
1063/// the newly completed type, we decrement the level count. When the level
1064/// count reaches zero, the upreferenced type is the type that is passed in:
1065/// thus we can complete the cycle.
1066///
Reid Spencerbb1fd572007-03-21 17:15:50 +00001067static PATypeHolder HandleUpRefs(const Type *ty, const Signedness& Sign) {
Reid Spencer950bf602007-01-26 08:19:09 +00001068 // If Ty isn't abstract, or if there are no up-references in it, then there is
1069 // nothing to resolve here.
1070 if (!ty->isAbstract() || UpRefs.empty()) return ty;
1071
1072 PATypeHolder Ty(ty);
1073 UR_OUT("Type '" << Ty->getDescription() <<
1074 "' newly formed. Resolving upreferences.\n" <<
1075 UpRefs.size() << " upreferences active!\n");
1076
1077 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
1078 // to zero), we resolve them all together before we resolve them to Ty. At
1079 // the end of the loop, if there is anything to resolve to Ty, it will be in
1080 // this variable.
1081 OpaqueType *TypeToResolve = 0;
1082
Reid Spencerbb1fd572007-03-21 17:15:50 +00001083 unsigned i = 0;
1084 for (; i != UpRefs.size(); ++i) {
Reid Spencer950bf602007-01-26 08:19:09 +00001085 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Reid Spencerbb1fd572007-03-21 17:15:50 +00001086 << UpRefs[i].UpRefTy->getDescription() << ") = "
1087 << (TypeContains(Ty, UpRefs[i].UpRefTy) ? "true" : "false") << "\n");
Reid Spencer950bf602007-01-26 08:19:09 +00001088 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
1089 // Decrement level of upreference
1090 unsigned Level = --UpRefs[i].NestingLevel;
1091 UpRefs[i].LastContainedTy = Ty;
1092 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
1093 if (Level == 0) { // Upreference should be resolved!
1094 if (!TypeToResolve) {
1095 TypeToResolve = UpRefs[i].UpRefTy;
1096 } else {
1097 UR_OUT(" * Resolving upreference for "
Reid Spencerbb1fd572007-03-21 17:15:50 +00001098 << UpRefs[i].UpRefTy->getDescription() << "\n";
1099 std::string OldName = UpRefs[i].UpRefTy->getDescription());
1100 ResolveTypeSign(UpRefs[i].UpRefTy, Sign);
Reid Spencer950bf602007-01-26 08:19:09 +00001101 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
1102 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
1103 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
1104 }
1105 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
1106 --i; // Do not skip the next element...
1107 }
1108 }
1109 }
1110
1111 if (TypeToResolve) {
1112 UR_OUT(" * Resolving upreference for "
Reid Spencerbb1fd572007-03-21 17:15:50 +00001113 << UpRefs[i].UpRefTy->getDescription() << "\n";
Reid Spencer950bf602007-01-26 08:19:09 +00001114 std::string OldName = TypeToResolve->getDescription());
Reid Spencerbb1fd572007-03-21 17:15:50 +00001115 ResolveTypeSign(TypeToResolve, Sign);
Reid Spencer950bf602007-01-26 08:19:09 +00001116 TypeToResolve->refineAbstractTypeTo(Ty);
1117 }
1118
1119 return Ty;
1120}
1121
Reid Spencerbb1fd572007-03-21 17:15:50 +00001122bool Signedness::operator<(const Signedness &that) const {
1123 if (isNamed()) {
1124 if (that.isNamed())
1125 return *(this->name) < *(that.name);
1126 else
1127 return CurModule.NamedTypeSigns[*name] < that;
1128 } else if (that.isNamed()) {
1129 return *this < CurModule.NamedTypeSigns[*that.name];
1130 }
1131
1132 if (isComposite() && that.isComposite()) {
1133 if (sv->size() == that.sv->size()) {
1134 SignVector::const_iterator thisI = sv->begin(), thisE = sv->end();
1135 SignVector::const_iterator thatI = that.sv->begin(),
1136 thatE = that.sv->end();
1137 for (; thisI != thisE; ++thisI, ++thatI) {
1138 if (*thisI < *thatI)
1139 return true;
1140 else if (!(*thisI == *thatI))
1141 return false;
1142 }
1143 return false;
1144 }
1145 return sv->size() < that.sv->size();
1146 }
1147 return kind < that.kind;
1148}
1149
1150bool Signedness::operator==(const Signedness &that) const {
1151 if (isNamed())
1152 if (that.isNamed())
1153 return *(this->name) == *(that.name);
1154 else
1155 return CurModule.NamedTypeSigns[*(this->name)] == that;
1156 else if (that.isNamed())
1157 return *this == CurModule.NamedTypeSigns[*(that.name)];
1158 if (isComposite() && that.isComposite()) {
1159 if (sv->size() == that.sv->size()) {
1160 SignVector::const_iterator thisI = sv->begin(), thisE = sv->end();
1161 SignVector::const_iterator thatI = that.sv->begin(),
1162 thatE = that.sv->end();
1163 for (; thisI != thisE; ++thisI, ++thatI) {
1164 if (!(*thisI == *thatI))
1165 return false;
1166 }
1167 return true;
1168 }
1169 return false;
1170 }
1171 return kind == that.kind;
1172}
1173
1174void Signedness::copy(const Signedness &that) {
1175 if (that.isNamed()) {
1176 kind = Named;
1177 name = new std::string(*that.name);
1178 } else if (that.isComposite()) {
1179 kind = Composite;
1180 sv = new SignVector();
1181 *sv = *that.sv;
1182 } else {
1183 kind = that.kind;
1184 sv = 0;
1185 }
1186}
1187
1188void Signedness::destroy() {
1189 if (isNamed()) {
1190 delete name;
1191 } else if (isComposite()) {
1192 delete sv;
1193 }
1194}
1195
Evan Cheng2b484202007-03-22 07:43:51 +00001196#ifndef NDEBUG
Reid Spencerbb1fd572007-03-21 17:15:50 +00001197void Signedness::dump() const {
1198 if (isComposite()) {
1199 if (sv->size() == 1) {
1200 (*sv)[0].dump();
1201 std::cerr << "*";
1202 } else {
1203 std::cerr << "{ " ;
1204 for (unsigned i = 0; i < sv->size(); ++i) {
1205 if (i != 0)
1206 std::cerr << ", ";
1207 (*sv)[i].dump();
1208 }
1209 std::cerr << "} " ;
1210 }
1211 } else if (isNamed()) {
1212 std::cerr << *name;
1213 } else if (isSigned()) {
1214 std::cerr << "S";
1215 } else if (isUnsigned()) {
1216 std::cerr << "U";
1217 } else
1218 std::cerr << ".";
1219}
Evan Cheng2b484202007-03-22 07:43:51 +00001220#endif
Reid Spencerbb1fd572007-03-21 17:15:50 +00001221
Reid Spencer950bf602007-01-26 08:19:09 +00001222static inline Instruction::TermOps
1223getTermOp(TermOps op) {
1224 switch (op) {
1225 default : assert(0 && "Invalid OldTermOp");
1226 case RetOp : return Instruction::Ret;
1227 case BrOp : return Instruction::Br;
1228 case SwitchOp : return Instruction::Switch;
1229 case InvokeOp : return Instruction::Invoke;
1230 case UnwindOp : return Instruction::Unwind;
1231 case UnreachableOp: return Instruction::Unreachable;
1232 }
1233}
1234
1235static inline Instruction::BinaryOps
Reid Spencerbb1fd572007-03-21 17:15:50 +00001236getBinaryOp(BinaryOps op, const Type *Ty, const Signedness& Sign) {
Reid Spencer950bf602007-01-26 08:19:09 +00001237 switch (op) {
1238 default : assert(0 && "Invalid OldBinaryOps");
1239 case SetEQ :
1240 case SetNE :
1241 case SetLE :
1242 case SetGE :
1243 case SetLT :
1244 case SetGT : assert(0 && "Should use getCompareOp");
1245 case AddOp : return Instruction::Add;
1246 case SubOp : return Instruction::Sub;
1247 case MulOp : return Instruction::Mul;
1248 case DivOp : {
1249 // This is an obsolete instruction so we must upgrade it based on the
1250 // types of its operands.
1251 bool isFP = Ty->isFloatingPoint();
Reid Spencer9d6565a2007-02-15 02:26:10 +00001252 if (const VectorType* PTy = dyn_cast<VectorType>(Ty))
Chris Lattner4227bdb2007-02-19 07:34:02 +00001253 // If its a vector type we want to use the element type
Reid Spencer950bf602007-01-26 08:19:09 +00001254 isFP = PTy->getElementType()->isFloatingPoint();
1255 if (isFP)
1256 return Instruction::FDiv;
Reid Spencerbb1fd572007-03-21 17:15:50 +00001257 else if (Sign.isSigned())
Reid Spencer950bf602007-01-26 08:19:09 +00001258 return Instruction::SDiv;
1259 return Instruction::UDiv;
1260 }
1261 case UDivOp : return Instruction::UDiv;
1262 case SDivOp : return Instruction::SDiv;
1263 case FDivOp : return Instruction::FDiv;
1264 case RemOp : {
1265 // This is an obsolete instruction so we must upgrade it based on the
1266 // types of its operands.
1267 bool isFP = Ty->isFloatingPoint();
Reid Spencer9d6565a2007-02-15 02:26:10 +00001268 if (const VectorType* PTy = dyn_cast<VectorType>(Ty))
Chris Lattner4227bdb2007-02-19 07:34:02 +00001269 // If its a vector type we want to use the element type
Reid Spencer950bf602007-01-26 08:19:09 +00001270 isFP = PTy->getElementType()->isFloatingPoint();
1271 // Select correct opcode
1272 if (isFP)
1273 return Instruction::FRem;
Reid Spencerbb1fd572007-03-21 17:15:50 +00001274 else if (Sign.isSigned())
Reid Spencer950bf602007-01-26 08:19:09 +00001275 return Instruction::SRem;
1276 return Instruction::URem;
1277 }
1278 case URemOp : return Instruction::URem;
1279 case SRemOp : return Instruction::SRem;
1280 case FRemOp : return Instruction::FRem;
Reid Spencer832254e2007-02-02 02:16:23 +00001281 case LShrOp : return Instruction::LShr;
1282 case AShrOp : return Instruction::AShr;
1283 case ShlOp : return Instruction::Shl;
1284 case ShrOp :
Reid Spencerbb1fd572007-03-21 17:15:50 +00001285 if (Sign.isSigned())
Reid Spencer832254e2007-02-02 02:16:23 +00001286 return Instruction::AShr;
1287 return Instruction::LShr;
Reid Spencer950bf602007-01-26 08:19:09 +00001288 case AndOp : return Instruction::And;
1289 case OrOp : return Instruction::Or;
1290 case XorOp : return Instruction::Xor;
1291 }
1292}
1293
1294static inline Instruction::OtherOps
1295getCompareOp(BinaryOps op, unsigned short &predicate, const Type* &Ty,
Reid Spencerbb1fd572007-03-21 17:15:50 +00001296 const Signedness &Sign) {
1297 bool isSigned = Sign.isSigned();
Reid Spencer950bf602007-01-26 08:19:09 +00001298 bool isFP = Ty->isFloatingPoint();
1299 switch (op) {
1300 default : assert(0 && "Invalid OldSetCC");
1301 case SetEQ :
1302 if (isFP) {
1303 predicate = FCmpInst::FCMP_OEQ;
1304 return Instruction::FCmp;
1305 } else {
1306 predicate = ICmpInst::ICMP_EQ;
1307 return Instruction::ICmp;
1308 }
1309 case SetNE :
1310 if (isFP) {
1311 predicate = FCmpInst::FCMP_UNE;
1312 return Instruction::FCmp;
1313 } else {
1314 predicate = ICmpInst::ICMP_NE;
1315 return Instruction::ICmp;
1316 }
1317 case SetLE :
1318 if (isFP) {
1319 predicate = FCmpInst::FCMP_OLE;
1320 return Instruction::FCmp;
1321 } else {
1322 if (isSigned)
1323 predicate = ICmpInst::ICMP_SLE;
1324 else
1325 predicate = ICmpInst::ICMP_ULE;
1326 return Instruction::ICmp;
1327 }
1328 case SetGE :
1329 if (isFP) {
1330 predicate = FCmpInst::FCMP_OGE;
1331 return Instruction::FCmp;
1332 } else {
1333 if (isSigned)
1334 predicate = ICmpInst::ICMP_SGE;
1335 else
1336 predicate = ICmpInst::ICMP_UGE;
1337 return Instruction::ICmp;
1338 }
1339 case SetLT :
1340 if (isFP) {
1341 predicate = FCmpInst::FCMP_OLT;
1342 return Instruction::FCmp;
1343 } else {
1344 if (isSigned)
1345 predicate = ICmpInst::ICMP_SLT;
1346 else
1347 predicate = ICmpInst::ICMP_ULT;
1348 return Instruction::ICmp;
1349 }
1350 case SetGT :
1351 if (isFP) {
1352 predicate = FCmpInst::FCMP_OGT;
1353 return Instruction::FCmp;
1354 } else {
1355 if (isSigned)
1356 predicate = ICmpInst::ICMP_SGT;
1357 else
1358 predicate = ICmpInst::ICMP_UGT;
1359 return Instruction::ICmp;
1360 }
1361 }
1362}
1363
1364static inline Instruction::MemoryOps getMemoryOp(MemoryOps op) {
1365 switch (op) {
1366 default : assert(0 && "Invalid OldMemoryOps");
1367 case MallocOp : return Instruction::Malloc;
1368 case FreeOp : return Instruction::Free;
1369 case AllocaOp : return Instruction::Alloca;
1370 case LoadOp : return Instruction::Load;
1371 case StoreOp : return Instruction::Store;
1372 case GetElementPtrOp : return Instruction::GetElementPtr;
1373 }
1374}
1375
1376static inline Instruction::OtherOps
Reid Spencerbb1fd572007-03-21 17:15:50 +00001377getOtherOp(OtherOps op, const Signedness &Sign) {
Reid Spencer950bf602007-01-26 08:19:09 +00001378 switch (op) {
1379 default : assert(0 && "Invalid OldOtherOps");
1380 case PHIOp : return Instruction::PHI;
1381 case CallOp : return Instruction::Call;
Reid Spencer950bf602007-01-26 08:19:09 +00001382 case SelectOp : return Instruction::Select;
1383 case UserOp1 : return Instruction::UserOp1;
1384 case UserOp2 : return Instruction::UserOp2;
1385 case VAArg : return Instruction::VAArg;
1386 case ExtractElementOp : return Instruction::ExtractElement;
1387 case InsertElementOp : return Instruction::InsertElement;
1388 case ShuffleVectorOp : return Instruction::ShuffleVector;
1389 case ICmpOp : return Instruction::ICmp;
1390 case FCmpOp : return Instruction::FCmp;
Reid Spencer950bf602007-01-26 08:19:09 +00001391 };
1392}
1393
1394static inline Value*
Reid Spencerbb1fd572007-03-21 17:15:50 +00001395getCast(CastOps op, Value *Src, const Signedness &SrcSign, const Type *DstTy,
1396 const Signedness &DstSign, bool ForceInstruction = false) {
Reid Spencer950bf602007-01-26 08:19:09 +00001397 Instruction::CastOps Opcode;
1398 const Type* SrcTy = Src->getType();
1399 if (op == CastOp) {
1400 if (SrcTy->isFloatingPoint() && isa<PointerType>(DstTy)) {
1401 // fp -> ptr cast is no longer supported but we must upgrade this
1402 // by doing a double cast: fp -> int -> ptr
1403 SrcTy = Type::Int64Ty;
1404 Opcode = Instruction::IntToPtr;
1405 if (isa<Constant>(Src)) {
1406 Src = ConstantExpr::getCast(Instruction::FPToUI,
1407 cast<Constant>(Src), SrcTy);
1408 } else {
1409 std::string NewName(makeNameUnique(Src->getName()));
1410 Src = new FPToUIInst(Src, SrcTy, NewName, CurBB);
1411 }
1412 } else if (isa<IntegerType>(DstTy) &&
1413 cast<IntegerType>(DstTy)->getBitWidth() == 1) {
1414 // cast type %x to bool was previously defined as setne type %x, null
1415 // The cast semantic is now to truncate, not compare so we must retain
1416 // the original intent by replacing the cast with a setne
1417 Constant* Null = Constant::getNullValue(SrcTy);
1418 Instruction::OtherOps Opcode = Instruction::ICmp;
1419 unsigned short predicate = ICmpInst::ICMP_NE;
1420 if (SrcTy->isFloatingPoint()) {
1421 Opcode = Instruction::FCmp;
1422 predicate = FCmpInst::FCMP_ONE;
1423 } else if (!SrcTy->isInteger() && !isa<PointerType>(SrcTy)) {
1424 error("Invalid cast to bool");
1425 }
1426 if (isa<Constant>(Src) && !ForceInstruction)
1427 return ConstantExpr::getCompare(predicate, cast<Constant>(Src), Null);
1428 else
1429 return CmpInst::create(Opcode, predicate, Src, Null);
1430 }
1431 // Determine the opcode to use by calling CastInst::getCastOpcode
1432 Opcode =
Reid Spencerbb1fd572007-03-21 17:15:50 +00001433 CastInst::getCastOpcode(Src, SrcSign.isSigned(), DstTy,
1434 DstSign.isSigned());
Reid Spencer950bf602007-01-26 08:19:09 +00001435
1436 } else switch (op) {
1437 default: assert(0 && "Invalid cast token");
1438 case TruncOp: Opcode = Instruction::Trunc; break;
1439 case ZExtOp: Opcode = Instruction::ZExt; break;
1440 case SExtOp: Opcode = Instruction::SExt; break;
1441 case FPTruncOp: Opcode = Instruction::FPTrunc; break;
1442 case FPExtOp: Opcode = Instruction::FPExt; break;
1443 case FPToUIOp: Opcode = Instruction::FPToUI; break;
1444 case FPToSIOp: Opcode = Instruction::FPToSI; break;
1445 case UIToFPOp: Opcode = Instruction::UIToFP; break;
1446 case SIToFPOp: Opcode = Instruction::SIToFP; break;
1447 case PtrToIntOp: Opcode = Instruction::PtrToInt; break;
1448 case IntToPtrOp: Opcode = Instruction::IntToPtr; break;
1449 case BitCastOp: Opcode = Instruction::BitCast; break;
1450 }
1451
1452 if (isa<Constant>(Src) && !ForceInstruction)
1453 return ConstantExpr::getCast(Opcode, cast<Constant>(Src), DstTy);
1454 return CastInst::create(Opcode, Src, DstTy);
1455}
1456
1457static Instruction *
1458upgradeIntrinsicCall(const Type* RetTy, const ValID &ID,
1459 std::vector<Value*>& Args) {
1460
1461 std::string Name = ID.Type == ValID::NameVal ? ID.Name : "";
Reid Spencer41b213e2007-04-02 01:14:00 +00001462 switch (Name[5]) {
1463 case 'i':
1464 if (Name == "llvm.isunordered.f32" || Name == "llvm.isunordered.f64") {
1465 if (Args.size() != 2)
1466 error("Invalid prototype for " + Name);
1467 return new FCmpInst(FCmpInst::FCMP_UNO, Args[0], Args[1]);
1468 }
1469 break;
1470 case 'b':
1471 if (Name.length() == 14 && !memcmp(&Name[5], "bswap.i", 7)) {
1472 const Type* ArgTy = Args[0]->getType();
1473 Name += ".i" + utostr(cast<IntegerType>(ArgTy)->getBitWidth());
1474 Function *F = cast<Function>(
1475 CurModule.CurrentModule->getOrInsertFunction(Name, RetTy, ArgTy,
1476 (void*)0));
1477 return new CallInst(F, Args[0]);
1478 }
1479 break;
Reid Spencer8166a6c2007-04-02 02:08:35 +00001480 case 'c':
1481 if ((Name.length() <= 14 && !memcmp(&Name[5], "ctpop.i", 7)) ||
1482 (Name.length() <= 13 && !memcmp(&Name[5], "ctlz.i", 6)) ||
1483 (Name.length() <= 13 && !memcmp(&Name[5], "cttz.i", 6))) {
1484 // These intrinsics changed their result type.
1485 const Type* ArgTy = Args[0]->getType();
1486 Function *OldF = CurModule.CurrentModule->getFunction(Name);
1487 if (OldF)
1488 OldF->setName("upgrd.rm." + Name);
1489
1490 Function *NewF = cast<Function>(
1491 CurModule.CurrentModule->getOrInsertFunction(Name, Type::Int32Ty,
1492 ArgTy, (void*)0));
1493
1494 Instruction *Call = new CallInst(NewF, Args[0], "", CurBB);
1495 return CastInst::createIntegerCast(Call, RetTy, false);
1496 }
1497 break;
1498
Reid Spencer41b213e2007-04-02 01:14:00 +00001499 case 'v' : {
1500 const Type* PtrTy = PointerType::get(Type::Int8Ty);
1501 std::vector<const Type*> Params;
1502 if (Name == "llvm.va_start" || Name == "llvm.va_end") {
1503 if (Args.size() != 1)
1504 error("Invalid prototype for " + Name + " prototype");
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 Args[0] = new BitCastInst(Args[0], PtrTy, makeNameUnique("va"), CurBB);
1511 return new CallInst(Func, &Args[0], Args.size());
1512 } else if (Name == "llvm.va_copy") {
1513 if (Args.size() != 2)
1514 error("Invalid prototype for " + Name + " prototype");
1515 Params.push_back(PtrTy);
1516 Params.push_back(PtrTy);
1517 const FunctionType *FTy =
1518 FunctionType::get(Type::VoidTy, Params, false);
1519 const PointerType *PFTy = PointerType::get(FTy);
1520 Value* Func = getVal(PFTy, ID);
1521 std::string InstName0(makeNameUnique("va0"));
1522 std::string InstName1(makeNameUnique("va1"));
1523 Args[0] = new BitCastInst(Args[0], PtrTy, InstName0, CurBB);
1524 Args[1] = new BitCastInst(Args[1], PtrTy, InstName1, CurBB);
1525 return new CallInst(Func, &Args[0], Args.size());
1526 }
Reid Spencer950bf602007-01-26 08:19:09 +00001527 }
1528 }
1529 return 0;
1530}
1531
Reid Spencerff0e4482007-04-16 00:40:57 +00001532const Type* upgradeGEPCEIndices(const Type* PTy,
1533 std::vector<ValueInfo> *Indices,
1534 std::vector<Constant*> &Result) {
1535 const Type *Ty = PTy;
1536 Result.clear();
1537 for (unsigned i = 0, e = Indices->size(); i != e ; ++i) {
1538 Constant *Index = cast<Constant>((*Indices)[i].V);
1539
1540 if (ConstantInt *CI = dyn_cast<ConstantInt>(Index)) {
1541 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte
1542 // struct indices to i32 struct indices with ZExt for compatibility.
1543 if (CI->getBitWidth() < 32)
1544 Index = ConstantExpr::getCast(Instruction::ZExt, CI, Type::Int32Ty);
1545 }
1546
1547 if (isa<SequentialType>(Ty)) {
1548 // Make sure that unsigned SequentialType indices are zext'd to
1549 // 64-bits if they were smaller than that because LLVM 2.0 will sext
1550 // all indices for SequentialType elements. We must retain the same
1551 // semantic (zext) for unsigned types.
1552 if (const IntegerType *Ity = dyn_cast<IntegerType>(Index->getType())) {
1553 if (Ity->getBitWidth() < 64 && (*Indices)[i].S.isUnsigned()) {
1554 Index = ConstantExpr::getCast(Instruction::ZExt, Index,Type::Int64Ty);
1555 }
1556 }
1557 }
1558 Result.push_back(Index);
1559 Ty = GetElementPtrInst::getIndexedType(PTy, (Value**)&Result[0],
1560 Result.size(),true);
1561 if (!Ty)
1562 error("Index list invalid for constant getelementptr");
1563 }
1564 return Ty;
1565}
1566
1567const Type* upgradeGEPInstIndices(const Type* PTy,
1568 std::vector<ValueInfo> *Indices,
1569 std::vector<Value*> &Result) {
1570 const Type *Ty = PTy;
1571 Result.clear();
1572 for (unsigned i = 0, e = Indices->size(); i != e ; ++i) {
1573 Value *Index = (*Indices)[i].V;
1574
1575 if (ConstantInt *CI = dyn_cast<ConstantInt>(Index)) {
1576 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte
1577 // struct indices to i32 struct indices with ZExt for compatibility.
1578 if (CI->getBitWidth() < 32)
1579 Index = ConstantExpr::getCast(Instruction::ZExt, CI, Type::Int32Ty);
1580 }
1581
1582
1583 if (isa<StructType>(Ty)) { // Only change struct indices
1584 if (!isa<Constant>(Index)) {
1585 error("Invalid non-constant structure index");
1586 return 0;
1587 }
Reid Spencer950bf602007-01-26 08:19:09 +00001588 } else {
1589 // Make sure that unsigned SequentialType indices are zext'd to
1590 // 64-bits if they were smaller than that because LLVM 2.0 will sext
1591 // all indices for SequentialType elements. We must retain the same
1592 // semantic (zext) for unsigned types.
Reid Spencerff0e4482007-04-16 00:40:57 +00001593 if (const IntegerType *Ity = dyn_cast<IntegerType>(Index->getType())) {
Reid Spencerbb1fd572007-03-21 17:15:50 +00001594 if (Ity->getBitWidth() < 64 && (*Indices)[i].S.isUnsigned()) {
Reid Spencerff0e4482007-04-16 00:40:57 +00001595 if (isa<Constant>(Index))
Reid Spencer950bf602007-01-26 08:19:09 +00001596 Index = ConstantExpr::getCast(Instruction::ZExt,
1597 cast<Constant>(Index), Type::Int64Ty);
1598 else
1599 Index = CastInst::create(Instruction::ZExt, Index, Type::Int64Ty,
Reid Spencer832254e2007-02-02 02:16:23 +00001600 makeNameUnique("gep"), CurBB);
Reid Spencer38f682b2007-01-26 20:31:18 +00001601 }
Reid Spencerff0e4482007-04-16 00:40:57 +00001602 }
Reid Spencer950bf602007-01-26 08:19:09 +00001603 }
Reid Spencerff0e4482007-04-16 00:40:57 +00001604 Result.push_back(Index);
1605 Ty = GetElementPtrInst::getIndexedType(PTy, &Result[0], Result.size(),true);
1606 if (!Ty)
Reid Spencer950bf602007-01-26 08:19:09 +00001607 error("Index list invalid for constant getelementptr");
Reid Spencerff0e4482007-04-16 00:40:57 +00001608 }
1609 return Ty;
Reid Spencer950bf602007-01-26 08:19:09 +00001610}
1611
Reid Spencerb7046c72007-01-29 05:41:34 +00001612unsigned upgradeCallingConv(unsigned CC) {
1613 switch (CC) {
1614 case OldCallingConv::C : return CallingConv::C;
1615 case OldCallingConv::CSRet : return CallingConv::C;
1616 case OldCallingConv::Fast : return CallingConv::Fast;
1617 case OldCallingConv::Cold : return CallingConv::Cold;
1618 case OldCallingConv::X86_StdCall : return CallingConv::X86_StdCall;
1619 case OldCallingConv::X86_FastCall: return CallingConv::X86_FastCall;
1620 default:
1621 return CC;
1622 }
1623}
1624
Reid Spencer950bf602007-01-26 08:19:09 +00001625Module* UpgradeAssembly(const std::string &infile, std::istream& in,
1626 bool debug, bool addAttrs)
Reid Spencere7c3c602006-11-30 06:36:44 +00001627{
1628 Upgradelineno = 1;
1629 CurFilename = infile;
Reid Spencer96839be2006-11-30 16:50:26 +00001630 LexInput = &in;
Reid Spencere77e35e2006-12-01 20:26:20 +00001631 yydebug = debug;
Reid Spencer71d2ec92006-12-31 06:02:26 +00001632 AddAttributes = addAttrs;
Reid Spencer950bf602007-01-26 08:19:09 +00001633 ObsoleteVarArgs = false;
1634 NewVarArgs = false;
Reid Spencere7c3c602006-11-30 06:36:44 +00001635
Reid Spencer950bf602007-01-26 08:19:09 +00001636 CurModule.CurrentModule = new Module(CurFilename);
1637
1638 // Check to make sure the parser succeeded
Reid Spencere7c3c602006-11-30 06:36:44 +00001639 if (yyparse()) {
Reid Spencer950bf602007-01-26 08:19:09 +00001640 if (ParserResult)
1641 delete ParserResult;
Reid Spencer30d0c582007-01-15 00:26:18 +00001642 std::cerr << "llvm-upgrade: parse failed.\n";
Reid Spencer30d0c582007-01-15 00:26:18 +00001643 return 0;
1644 }
1645
Reid Spencer950bf602007-01-26 08:19:09 +00001646 // Check to make sure that parsing produced a result
1647 if (!ParserResult) {
1648 std::cerr << "llvm-upgrade: no parse result.\n";
1649 return 0;
Reid Spencer30d0c582007-01-15 00:26:18 +00001650 }
1651
Reid Spencer950bf602007-01-26 08:19:09 +00001652 // Reset ParserResult variable while saving its value for the result.
1653 Module *Result = ParserResult;
1654 ParserResult = 0;
Reid Spencer30d0c582007-01-15 00:26:18 +00001655
Reid Spencer950bf602007-01-26 08:19:09 +00001656 //Not all functions use vaarg, so make a second check for ObsoleteVarArgs
Reid Spencer30d0c582007-01-15 00:26:18 +00001657 {
Reid Spencer950bf602007-01-26 08:19:09 +00001658 Function* F;
Reid Spencer688b0492007-02-05 21:19:13 +00001659 if ((F = Result->getFunction("llvm.va_start"))
Reid Spencer950bf602007-01-26 08:19:09 +00001660 && F->getFunctionType()->getNumParams() == 0)
1661 ObsoleteVarArgs = true;
Reid Spencer688b0492007-02-05 21:19:13 +00001662 if((F = Result->getFunction("llvm.va_copy"))
Reid Spencer950bf602007-01-26 08:19:09 +00001663 && F->getFunctionType()->getNumParams() == 1)
1664 ObsoleteVarArgs = true;
Reid Spencer280d8012006-12-01 23:40:53 +00001665 }
Reid Spencer319a7302007-01-05 17:20:02 +00001666
Reid Spencer950bf602007-01-26 08:19:09 +00001667 if (ObsoleteVarArgs && NewVarArgs) {
1668 error("This file is corrupt: it uses both new and old style varargs");
1669 return 0;
Reid Spencer319a7302007-01-05 17:20:02 +00001670 }
Reid Spencer319a7302007-01-05 17:20:02 +00001671
Reid Spencer950bf602007-01-26 08:19:09 +00001672 if(ObsoleteVarArgs) {
Reid Spencer688b0492007-02-05 21:19:13 +00001673 if(Function* F = Result->getFunction("llvm.va_start")) {
Reid Spencer950bf602007-01-26 08:19:09 +00001674 if (F->arg_size() != 0) {
1675 error("Obsolete va_start takes 0 argument");
Reid Spencer319a7302007-01-05 17:20:02 +00001676 return 0;
1677 }
Reid Spencer950bf602007-01-26 08:19:09 +00001678
1679 //foo = va_start()
1680 // ->
1681 //bar = alloca typeof(foo)
1682 //va_start(bar)
1683 //foo = load bar
Reid Spencer319a7302007-01-05 17:20:02 +00001684
Reid Spencer950bf602007-01-26 08:19:09 +00001685 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
1686 const Type* ArgTy = F->getFunctionType()->getReturnType();
1687 const Type* ArgTyPtr = PointerType::get(ArgTy);
1688 Function* NF = cast<Function>(Result->getOrInsertFunction(
1689 "llvm.va_start", RetTy, ArgTyPtr, (Type *)0));
1690
1691 while (!F->use_empty()) {
1692 CallInst* CI = cast<CallInst>(F->use_back());
1693 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
1694 new CallInst(NF, bar, "", CI);
1695 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
1696 CI->replaceAllUsesWith(foo);
1697 CI->getParent()->getInstList().erase(CI);
Reid Spencerf8383de2007-01-06 06:04:32 +00001698 }
Reid Spencer950bf602007-01-26 08:19:09 +00001699 Result->getFunctionList().erase(F);
Reid Spencerf8383de2007-01-06 06:04:32 +00001700 }
Reid Spencer950bf602007-01-26 08:19:09 +00001701
Reid Spencer688b0492007-02-05 21:19:13 +00001702 if(Function* F = Result->getFunction("llvm.va_end")) {
Reid Spencer950bf602007-01-26 08:19:09 +00001703 if(F->arg_size() != 1) {
1704 error("Obsolete va_end takes 1 argument");
1705 return 0;
Reid Spencerf8383de2007-01-06 06:04:32 +00001706 }
Reid Spencerf8383de2007-01-06 06:04:32 +00001707
Reid Spencer950bf602007-01-26 08:19:09 +00001708 //vaend foo
1709 // ->
1710 //bar = alloca 1 of typeof(foo)
1711 //vaend bar
1712 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
1713 const Type* ArgTy = F->getFunctionType()->getParamType(0);
1714 const Type* ArgTyPtr = PointerType::get(ArgTy);
1715 Function* NF = cast<Function>(Result->getOrInsertFunction(
1716 "llvm.va_end", RetTy, ArgTyPtr, (Type *)0));
Reid Spencerf8383de2007-01-06 06:04:32 +00001717
Reid Spencer950bf602007-01-26 08:19:09 +00001718 while (!F->use_empty()) {
1719 CallInst* CI = cast<CallInst>(F->use_back());
1720 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
1721 new StoreInst(CI->getOperand(1), bar, CI);
1722 new CallInst(NF, bar, "", CI);
1723 CI->getParent()->getInstList().erase(CI);
Reid Spencere77e35e2006-12-01 20:26:20 +00001724 }
Reid Spencer950bf602007-01-26 08:19:09 +00001725 Result->getFunctionList().erase(F);
Reid Spencere77e35e2006-12-01 20:26:20 +00001726 }
Reid Spencer950bf602007-01-26 08:19:09 +00001727
Reid Spencer688b0492007-02-05 21:19:13 +00001728 if(Function* F = Result->getFunction("llvm.va_copy")) {
Reid Spencer950bf602007-01-26 08:19:09 +00001729 if(F->arg_size() != 1) {
1730 error("Obsolete va_copy takes 1 argument");
1731 return 0;
Reid Spencere77e35e2006-12-01 20:26:20 +00001732 }
Reid Spencer950bf602007-01-26 08:19:09 +00001733 //foo = vacopy(bar)
1734 // ->
1735 //a = alloca 1 of typeof(foo)
1736 //b = alloca 1 of typeof(foo)
1737 //store bar -> b
1738 //vacopy(a, b)
1739 //foo = load a
1740
1741 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
1742 const Type* ArgTy = F->getFunctionType()->getReturnType();
1743 const Type* ArgTyPtr = PointerType::get(ArgTy);
1744 Function* NF = cast<Function>(Result->getOrInsertFunction(
1745 "llvm.va_copy", RetTy, ArgTyPtr, ArgTyPtr, (Type *)0));
Reid Spencere77e35e2006-12-01 20:26:20 +00001746
Reid Spencer950bf602007-01-26 08:19:09 +00001747 while (!F->use_empty()) {
1748 CallInst* CI = cast<CallInst>(F->use_back());
1749 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
1750 AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
1751 new StoreInst(CI->getOperand(1), b, CI);
1752 new CallInst(NF, a, b, "", CI);
1753 Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
1754 CI->replaceAllUsesWith(foo);
1755 CI->getParent()->getInstList().erase(CI);
1756 }
1757 Result->getFunctionList().erase(F);
Reid Spencer319a7302007-01-05 17:20:02 +00001758 }
1759 }
1760
Reid Spencer52402b02007-01-02 05:45:11 +00001761 return Result;
1762}
1763
Reid Spencer950bf602007-01-26 08:19:09 +00001764} // end llvm namespace
Reid Spencer319a7302007-01-05 17:20:02 +00001765
Reid Spencer950bf602007-01-26 08:19:09 +00001766using namespace llvm;
Reid Spencer30d0c582007-01-15 00:26:18 +00001767
Reid Spencere7c3c602006-11-30 06:36:44 +00001768%}
1769
Reid Spencere77e35e2006-12-01 20:26:20 +00001770%union {
Reid Spencer950bf602007-01-26 08:19:09 +00001771 llvm::Module *ModuleVal;
1772 llvm::Function *FunctionVal;
1773 std::pair<llvm::PATypeInfo, char*> *ArgVal;
1774 llvm::BasicBlock *BasicBlockVal;
Reid Spencerbb1fd572007-03-21 17:15:50 +00001775 llvm::TermInstInfo TermInstVal;
Reid Spencer950bf602007-01-26 08:19:09 +00001776 llvm::InstrInfo InstVal;
1777 llvm::ConstInfo ConstVal;
1778 llvm::ValueInfo ValueVal;
1779 llvm::PATypeInfo TypeVal;
1780 llvm::TypeInfo PrimType;
1781 llvm::PHIListInfo PHIList;
1782 std::list<llvm::PATypeInfo> *TypeList;
1783 std::vector<llvm::ValueInfo> *ValueList;
1784 std::vector<llvm::ConstInfo> *ConstVector;
1785
1786
1787 std::vector<std::pair<llvm::PATypeInfo,char*> > *ArgList;
1788 // Represent the RHS of PHI node
1789 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
1790
1791 llvm::GlobalValue::LinkageTypes Linkage;
1792 int64_t SInt64Val;
1793 uint64_t UInt64Val;
1794 int SIntVal;
1795 unsigned UIntVal;
1796 double FPVal;
1797 bool BoolVal;
1798
1799 char *StrVal; // This memory is strdup'd!
1800 llvm::ValID ValIDVal; // strdup'd memory maybe!
1801
1802 llvm::BinaryOps BinaryOpVal;
1803 llvm::TermOps TermOpVal;
1804 llvm::MemoryOps MemOpVal;
1805 llvm::OtherOps OtherOpVal;
1806 llvm::CastOps CastOpVal;
1807 llvm::ICmpInst::Predicate IPred;
1808 llvm::FCmpInst::Predicate FPred;
1809 llvm::Module::Endianness Endianness;
Reid Spencere77e35e2006-12-01 20:26:20 +00001810}
1811
Reid Spencer950bf602007-01-26 08:19:09 +00001812%type <ModuleVal> Module FunctionList
1813%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
1814%type <BasicBlockVal> BasicBlock InstructionList
1815%type <TermInstVal> BBTerminatorInst
1816%type <InstVal> Inst InstVal MemoryInst
1817%type <ConstVal> ConstVal ConstExpr
1818%type <ConstVector> ConstVector
1819%type <ArgList> ArgList ArgListH
1820%type <ArgVal> ArgVal
1821%type <PHIList> PHIList
1822%type <ValueList> ValueRefList ValueRefListE // For call param lists
1823%type <ValueList> IndexList // For GEP derived indices
1824%type <TypeList> TypeListI ArgTypeListI
1825%type <JumpTable> JumpTable
1826%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
1827%type <BoolVal> OptVolatile // 'volatile' or not
1828%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1829%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencered96d1e2007-02-08 09:08:52 +00001830%type <Linkage> OptLinkage FnDeclareLinkage
Reid Spencer950bf602007-01-26 08:19:09 +00001831%type <Endianness> BigOrLittle
Reid Spencere77e35e2006-12-01 20:26:20 +00001832
Reid Spencer950bf602007-01-26 08:19:09 +00001833// ValueRef - Unresolved reference to a definition or BB
1834%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1835%type <ValueVal> ResolvedVal // <type> <valref> pair
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001836
Reid Spencer950bf602007-01-26 08:19:09 +00001837// Tokens and types for handling constant integer values
1838//
1839// ESINT64VAL - A negative number within long long range
1840%token <SInt64Val> ESINT64VAL
Reid Spencere77e35e2006-12-01 20:26:20 +00001841
Reid Spencer950bf602007-01-26 08:19:09 +00001842// EUINT64VAL - A positive number within uns. long long range
1843%token <UInt64Val> EUINT64VAL
1844%type <SInt64Val> EINT64VAL
Reid Spencere77e35e2006-12-01 20:26:20 +00001845
Reid Spencer950bf602007-01-26 08:19:09 +00001846%token <SIntVal> SINTVAL // Signed 32 bit ints...
1847%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
1848%type <SIntVal> INTVAL
1849%token <FPVal> FPVAL // Float or Double constant
Reid Spencere77e35e2006-12-01 20:26:20 +00001850
Reid Spencer950bf602007-01-26 08:19:09 +00001851// Built in types...
1852%type <TypeVal> Types TypesV UpRTypes UpRTypesV
1853%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
1854%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
1855%token <PrimType> FLOAT DOUBLE TYPE LABEL
Reid Spencere77e35e2006-12-01 20:26:20 +00001856
Reid Spencer950bf602007-01-26 08:19:09 +00001857%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
1858%type <StrVal> Name OptName OptAssign
1859%type <UIntVal> OptAlign OptCAlign
1860%type <StrVal> OptSection SectionString
1861
1862%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
1863%token DECLARE GLOBAL CONSTANT SECTION VOLATILE
1864%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
1865%token DLLIMPORT DLLEXPORT EXTERN_WEAK
1866%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
1867%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
1868%token CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
1869%token X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
1870%token DATALAYOUT
1871%type <UIntVal> OptCallingConv
1872
1873// Basic Block Terminating Operators
1874%token <TermOpVal> RET BR SWITCH INVOKE UNREACHABLE
1875%token UNWIND EXCEPT
1876
1877// Binary Operators
1878%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Reid Spencer832254e2007-02-02 02:16:23 +00001879%type <BinaryOpVal> ShiftOps
Reid Spencer950bf602007-01-26 08:19:09 +00001880%token <BinaryOpVal> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM
Reid Spencer832254e2007-02-02 02:16:23 +00001881%token <BinaryOpVal> AND OR XOR SHL SHR ASHR LSHR
Reid Spencer950bf602007-01-26 08:19:09 +00001882%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
1883%token <OtherOpVal> ICMP FCMP
1884
1885// Memory Instructions
1886%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1887
1888// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001889%token <OtherOpVal> PHI_TOK SELECT VAARG
Reid Spencer950bf602007-01-26 08:19:09 +00001890%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
1891%token VAARG_old VANEXT_old //OBSOLETE
1892
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001893// Support for ICmp/FCmp Predicates, which is 1.9++ but not 2.0
Reid Spencer950bf602007-01-26 08:19:09 +00001894%type <IPred> IPredicates
1895%type <FPred> FPredicates
1896%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1897%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
1898
1899%token <CastOpVal> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI
1900%token <CastOpVal> UITOFP SITOFP PTRTOINT INTTOPTR BITCAST
1901%type <CastOpVal> CastOps
Reid Spencere7c3c602006-11-30 06:36:44 +00001902
1903%start Module
1904
1905%%
1906
1907// Handle constant integer size restriction and conversion...
Reid Spencer950bf602007-01-26 08:19:09 +00001908//
1909INTVAL
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001910 : SINTVAL
Reid Spencer950bf602007-01-26 08:19:09 +00001911 | UINTVAL {
1912 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
1913 error("Value too large for type");
1914 $$ = (int32_t)$1;
1915 }
1916 ;
1917
1918EINT64VAL
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001919 : ESINT64VAL // These have same type and can't cause problems...
Reid Spencer950bf602007-01-26 08:19:09 +00001920 | EUINT64VAL {
1921 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
1922 error("Value too large for type");
1923 $$ = (int64_t)$1;
1924 };
Reid Spencere7c3c602006-11-30 06:36:44 +00001925
1926// Operations that are notably excluded from this list include:
1927// RET, BR, & SWITCH because they end basic blocks and are treated specially.
Reid Spencer950bf602007-01-26 08:19:09 +00001928//
1929ArithmeticOps
1930 : ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV | REM | UREM | SREM | FREM
1931 ;
1932
1933LogicalOps
1934 : AND | OR | XOR
1935 ;
1936
1937SetCondOps
1938 : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
1939 ;
1940
1941IPredicates
1942 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
1943 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1944 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1945 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1946 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1947 ;
1948
1949FPredicates
1950 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1951 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1952 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1953 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1954 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1955 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1956 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1957 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1958 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1959 ;
1960ShiftOps
1961 : SHL | SHR | ASHR | LSHR
1962 ;
1963
1964CastOps
1965 : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI
1966 | UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
1967 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001968
1969// These are some types that allow classification if we only want a particular
1970// thing... for example, only a signed, unsigned, or integral type.
Reid Spencer950bf602007-01-26 08:19:09 +00001971SIntType
1972 : LONG | INT | SHORT | SBYTE
1973 ;
1974
1975UIntType
1976 : ULONG | UINT | USHORT | UBYTE
1977 ;
1978
1979IntType
1980 : SIntType | UIntType
1981 ;
1982
1983FPType
1984 : FLOAT | DOUBLE
1985 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001986
1987// OptAssign - Value producing statements have an optional assignment component
Reid Spencer950bf602007-01-26 08:19:09 +00001988OptAssign
1989 : Name '=' {
Reid Spencere7c3c602006-11-30 06:36:44 +00001990 $$ = $1;
1991 }
1992 | /*empty*/ {
Reid Spencer950bf602007-01-26 08:19:09 +00001993 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001994 };
1995
1996OptLinkage
Reid Spencer785a5ae2007-02-08 00:21:40 +00001997 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencer950bf602007-01-26 08:19:09 +00001998 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1999 | WEAK { $$ = GlobalValue::WeakLinkage; }
2000 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
2001 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
2002 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencer785a5ae2007-02-08 00:21:40 +00002003 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencer950bf602007-01-26 08:19:09 +00002004 | /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
2005 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002006
2007OptCallingConv
Reid Spencered96d1e2007-02-08 09:08:52 +00002008 : /*empty*/ { $$ = OldCallingConv::C; }
2009 | CCC_TOK { $$ = OldCallingConv::C; }
2010 | CSRETCC_TOK { $$ = OldCallingConv::CSRet; }
2011 | FASTCC_TOK { $$ = OldCallingConv::Fast; }
2012 | COLDCC_TOK { $$ = OldCallingConv::Cold; }
2013 | X86_STDCALLCC_TOK { $$ = OldCallingConv::X86_StdCall; }
2014 | X86_FASTCALLCC_TOK { $$ = OldCallingConv::X86_FastCall; }
Reid Spencer950bf602007-01-26 08:19:09 +00002015 | CC_TOK EUINT64VAL {
2016 if ((unsigned)$2 != $2)
2017 error("Calling conv too large");
2018 $$ = $2;
2019 }
2020 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002021
2022// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
2023// a comma before it.
2024OptAlign
Reid Spencer950bf602007-01-26 08:19:09 +00002025 : /*empty*/ { $$ = 0; }
2026 | ALIGN EUINT64VAL {
2027 $$ = $2;
2028 if ($$ != 0 && !isPowerOf2_32($$))
2029 error("Alignment must be a power of two");
2030 }
2031 ;
Reid Spencerf0cf1322006-12-07 04:23:03 +00002032
Reid Spencere7c3c602006-11-30 06:36:44 +00002033OptCAlign
Reid Spencer950bf602007-01-26 08:19:09 +00002034 : /*empty*/ { $$ = 0; }
2035 | ',' ALIGN EUINT64VAL {
2036 $$ = $3;
2037 if ($$ != 0 && !isPowerOf2_32($$))
2038 error("Alignment must be a power of two");
2039 }
2040 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002041
2042SectionString
Reid Spencer950bf602007-01-26 08:19:09 +00002043 : SECTION STRINGCONSTANT {
2044 for (unsigned i = 0, e = strlen($2); i != e; ++i)
2045 if ($2[i] == '"' || $2[i] == '\\')
2046 error("Invalid character in section name");
2047 $$ = $2;
2048 }
2049 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002050
Reid Spencer950bf602007-01-26 08:19:09 +00002051OptSection
2052 : /*empty*/ { $$ = 0; }
2053 | SectionString { $$ = $1; }
2054 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002055
Reid Spencer950bf602007-01-26 08:19:09 +00002056// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
2057// is set to be the global we are processing.
2058//
Reid Spencere7c3c602006-11-30 06:36:44 +00002059GlobalVarAttributes
Reid Spencer950bf602007-01-26 08:19:09 +00002060 : /* empty */ {}
2061 | ',' GlobalVarAttribute GlobalVarAttributes {}
2062 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002063
Reid Spencer950bf602007-01-26 08:19:09 +00002064GlobalVarAttribute
2065 : SectionString {
2066 CurGV->setSection($1);
2067 free($1);
2068 }
2069 | ALIGN EUINT64VAL {
2070 if ($2 != 0 && !isPowerOf2_32($2))
2071 error("Alignment must be a power of two");
2072 CurGV->setAlignment($2);
2073
2074 }
2075 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002076
2077//===----------------------------------------------------------------------===//
2078// Types includes all predefined types... except void, because it can only be
2079// used in specific contexts (function returning void for example). To have
2080// access to it, a user must explicitly use TypesV.
2081//
2082
2083// TypesV includes all of 'Types', but it also includes the void type.
Reid Spencer950bf602007-01-26 08:19:09 +00002084TypesV
2085 : Types
2086 | VOID {
Reid Spencered96d1e2007-02-08 09:08:52 +00002087 $$.PAT = new PATypeHolder($1.T);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002088 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002089 }
2090 ;
2091
2092UpRTypesV
2093 : UpRTypes
2094 | VOID {
Reid Spencered96d1e2007-02-08 09:08:52 +00002095 $$.PAT = new PATypeHolder($1.T);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002096 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002097 }
2098 ;
2099
2100Types
2101 : UpRTypes {
2102 if (!UpRefs.empty())
Reid Spencered96d1e2007-02-08 09:08:52 +00002103 error("Invalid upreference in type: " + (*$1.PAT)->getDescription());
Reid Spencer950bf602007-01-26 08:19:09 +00002104 $$ = $1;
2105 }
2106 ;
2107
2108PrimType
2109 : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
2110 | LONG | ULONG | FLOAT | DOUBLE | LABEL
2111 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002112
2113// Derived types are added later...
Reid Spencera50d5962006-12-02 04:11:07 +00002114UpRTypes
Reid Spencer950bf602007-01-26 08:19:09 +00002115 : PrimType {
Reid Spencered96d1e2007-02-08 09:08:52 +00002116 $$.PAT = new PATypeHolder($1.T);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002117 $$.S.copy($1.S);
Reid Spencera50d5962006-12-02 04:11:07 +00002118 }
Reid Spencer950bf602007-01-26 08:19:09 +00002119 | OPAQUE {
Reid Spencered96d1e2007-02-08 09:08:52 +00002120 $$.PAT = new PATypeHolder(OpaqueType::get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002121 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002122 }
2123 | SymbolicValueRef { // Named types are also simple types...
Reid Spencerbb1fd572007-03-21 17:15:50 +00002124 $$.S.copy(getTypeSign($1));
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00002125 const Type* tmp = getType($1);
Reid Spencered96d1e2007-02-08 09:08:52 +00002126 $$.PAT = new PATypeHolder(tmp);
Reid Spencer78720742006-12-02 20:21:22 +00002127 }
2128 | '\\' EUINT64VAL { // Type UpReference
Reid Spencer950bf602007-01-26 08:19:09 +00002129 if ($2 > (uint64_t)~0U)
2130 error("Value out of range");
2131 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
2132 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencered96d1e2007-02-08 09:08:52 +00002133 $$.PAT = new PATypeHolder(OT);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002134 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002135 UR_OUT("New Upreference!\n");
Reid Spencere7c3c602006-11-30 06:36:44 +00002136 }
2137 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Reid Spencerbb1fd572007-03-21 17:15:50 +00002138 $$.S.makeComposite($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002139 std::vector<const Type*> Params;
2140 for (std::list<llvm::PATypeInfo>::iterator I = $3->begin(),
2141 E = $3->end(); I != E; ++I) {
Reid Spencered96d1e2007-02-08 09:08:52 +00002142 Params.push_back(I->PAT->get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002143 $$.S.add(I->S);
Reid Spencer52402b02007-01-02 05:45:11 +00002144 }
Reid Spencer950bf602007-01-26 08:19:09 +00002145 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
2146 if (isVarArg) Params.pop_back();
2147
Reid Spencer7b5d4662007-04-09 06:16:21 +00002148 const FunctionType *FTy =
2149 FunctionType::get($1.PAT->get(), Params, isVarArg, 0);
2150
2151 $$.PAT = new PATypeHolder( HandleUpRefs(FTy, $$.S) );
Reid Spencerbb1fd572007-03-21 17:15:50 +00002152 delete $1.PAT; // Delete the return type handle
Reid Spencer950bf602007-01-26 08:19:09 +00002153 delete $3; // Delete the argument list
Reid Spencere7c3c602006-11-30 06:36:44 +00002154 }
2155 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencerbb1fd572007-03-21 17:15:50 +00002156 $$.S.makeComposite($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002157 $$.PAT = new PATypeHolder(HandleUpRefs(ArrayType::get($4.PAT->get(),
Reid Spencerbb1fd572007-03-21 17:15:50 +00002158 (unsigned)$2), $$.S));
Reid Spencered96d1e2007-02-08 09:08:52 +00002159 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002160 }
Chris Lattner4227bdb2007-02-19 07:34:02 +00002161 | '<' EUINT64VAL 'x' UpRTypes '>' { // Vector type?
Reid Spencerbb1fd572007-03-21 17:15:50 +00002162 const llvm::Type* ElemTy = $4.PAT->get();
2163 if ((unsigned)$2 != $2)
2164 error("Unsigned result not equal to signed result");
2165 if (!(ElemTy->isInteger() || ElemTy->isFloatingPoint()))
2166 error("Elements of a VectorType must be integer or floating point");
2167 if (!isPowerOf2_32($2))
2168 error("VectorType length should be a power of 2");
2169 $$.S.makeComposite($4.S);
2170 $$.PAT = new PATypeHolder(HandleUpRefs(VectorType::get(ElemTy,
2171 (unsigned)$2), $$.S));
2172 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002173 }
2174 | '{' TypeListI '}' { // Structure type?
Reid Spencer950bf602007-01-26 08:19:09 +00002175 std::vector<const Type*> Elements;
Reid Spencerbb1fd572007-03-21 17:15:50 +00002176 $$.S.makeComposite();
Reid Spencer950bf602007-01-26 08:19:09 +00002177 for (std::list<llvm::PATypeInfo>::iterator I = $2->begin(),
Reid Spencerbb1fd572007-03-21 17:15:50 +00002178 E = $2->end(); I != E; ++I) {
Reid Spencered96d1e2007-02-08 09:08:52 +00002179 Elements.push_back(I->PAT->get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002180 $$.S.add(I->S);
2181 }
2182 $$.PAT = new PATypeHolder(HandleUpRefs(StructType::get(Elements), $$.S));
Reid Spencer950bf602007-01-26 08:19:09 +00002183 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00002184 }
2185 | '{' '}' { // Empty structure type?
Reid Spencered96d1e2007-02-08 09:08:52 +00002186 $$.PAT = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencerbb1fd572007-03-21 17:15:50 +00002187 $$.S.makeComposite();
Reid Spencere7c3c602006-11-30 06:36:44 +00002188 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002189 | '<' '{' TypeListI '}' '>' { // Packed Structure type?
Reid Spencerbb1fd572007-03-21 17:15:50 +00002190 $$.S.makeComposite();
Reid Spencer950bf602007-01-26 08:19:09 +00002191 std::vector<const Type*> Elements;
2192 for (std::list<llvm::PATypeInfo>::iterator I = $3->begin(),
2193 E = $3->end(); I != E; ++I) {
Reid Spencered96d1e2007-02-08 09:08:52 +00002194 Elements.push_back(I->PAT->get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002195 $$.S.add(I->S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002196 delete I->PAT;
Reid Spencer52402b02007-01-02 05:45:11 +00002197 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00002198 $$.PAT = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true),
2199 $$.S));
Reid Spencer950bf602007-01-26 08:19:09 +00002200 delete $3;
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002201 }
2202 | '<' '{' '}' '>' { // Empty packed structure type?
Reid Spencered96d1e2007-02-08 09:08:52 +00002203 $$.PAT = new PATypeHolder(StructType::get(std::vector<const Type*>(),true));
Reid Spencerbb1fd572007-03-21 17:15:50 +00002204 $$.S.makeComposite();
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002205 }
Reid Spencere7c3c602006-11-30 06:36:44 +00002206 | UpRTypes '*' { // Pointer type?
Reid Spencered96d1e2007-02-08 09:08:52 +00002207 if ($1.PAT->get() == Type::LabelTy)
Reid Spencer950bf602007-01-26 08:19:09 +00002208 error("Cannot form a pointer to a basic block");
Reid Spencerbb1fd572007-03-21 17:15:50 +00002209 $$.S.makeComposite($1.S);
2210 $$.PAT = new PATypeHolder(HandleUpRefs(PointerType::get($1.PAT->get()),
2211 $$.S));
Reid Spencered96d1e2007-02-08 09:08:52 +00002212 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002213 }
2214 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002215
2216// TypeList - Used for struct declarations and as a basis for function type
2217// declaration type lists
2218//
Reid Spencere77e35e2006-12-01 20:26:20 +00002219TypeListI
2220 : UpRTypes {
Reid Spencer950bf602007-01-26 08:19:09 +00002221 $$ = new std::list<PATypeInfo>();
2222 $$->push_back($1);
Reid Spencere77e35e2006-12-01 20:26:20 +00002223 }
2224 | TypeListI ',' UpRTypes {
Reid Spencer950bf602007-01-26 08:19:09 +00002225 ($$=$1)->push_back($3);
2226 }
2227 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002228
2229// ArgTypeList - List of types for a function type declaration...
Reid Spencere77e35e2006-12-01 20:26:20 +00002230ArgTypeListI
Reid Spencer950bf602007-01-26 08:19:09 +00002231 : TypeListI
Reid Spencere7c3c602006-11-30 06:36:44 +00002232 | TypeListI ',' DOTDOTDOT {
Reid Spencer950bf602007-01-26 08:19:09 +00002233 PATypeInfo VoidTI;
Reid Spencered96d1e2007-02-08 09:08:52 +00002234 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002235 VoidTI.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002236 ($$=$1)->push_back(VoidTI);
Reid Spencere7c3c602006-11-30 06:36:44 +00002237 }
2238 | DOTDOTDOT {
Reid Spencer950bf602007-01-26 08:19:09 +00002239 $$ = new std::list<PATypeInfo>();
2240 PATypeInfo VoidTI;
Reid Spencered96d1e2007-02-08 09:08:52 +00002241 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002242 VoidTI.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002243 $$->push_back(VoidTI);
Reid Spencere7c3c602006-11-30 06:36:44 +00002244 }
2245 | /*empty*/ {
Reid Spencer950bf602007-01-26 08:19:09 +00002246 $$ = new std::list<PATypeInfo>();
2247 }
2248 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002249
2250// ConstVal - The various declarations that go into the constant pool. This
2251// production is used ONLY to represent constants that show up AFTER a 'const',
2252// 'constant' or 'global' token at global scope. Constants that can be inlined
2253// into other expressions (such as integers and constexprs) are handled by the
2254// ResolvedVal, ValueRef and ConstValueRef productions.
2255//
Reid Spencer950bf602007-01-26 08:19:09 +00002256ConstVal
2257 : Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencered96d1e2007-02-08 09:08:52 +00002258 const ArrayType *ATy = dyn_cast<ArrayType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002259 if (ATy == 0)
2260 error("Cannot make array constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002261 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002262 const Type *ETy = ATy->getElementType();
2263 int NumElements = ATy->getNumElements();
2264
2265 // Verify that we have the correct size...
2266 if (NumElements != -1 && NumElements != (int)$3->size())
2267 error("Type mismatch: constant sized array initialized with " +
2268 utostr($3->size()) + " arguments, but has size of " +
2269 itostr(NumElements) + "");
2270
2271 // Verify all elements are correct type!
2272 std::vector<Constant*> Elems;
2273 for (unsigned i = 0; i < $3->size(); i++) {
2274 Constant *C = (*$3)[i].C;
2275 const Type* ValTy = C->getType();
2276 if (ETy != ValTy)
2277 error("Element #" + utostr(i) + " is not of type '" +
2278 ETy->getDescription() +"' as required!\nIt is of type '"+
2279 ValTy->getDescription() + "'");
2280 Elems.push_back(C);
2281 }
2282 $$.C = ConstantArray::get(ATy, Elems);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002283 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002284 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002285 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002286 }
2287 | Types '[' ']' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002288 const ArrayType *ATy = dyn_cast<ArrayType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002289 if (ATy == 0)
2290 error("Cannot make array constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002291 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002292 int NumElements = ATy->getNumElements();
2293 if (NumElements != -1 && NumElements != 0)
2294 error("Type mismatch: constant sized array initialized with 0"
2295 " arguments, but has size of " + itostr(NumElements) +"");
2296 $$.C = ConstantArray::get(ATy, std::vector<Constant*>());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002297 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002298 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002299 }
2300 | Types 'c' STRINGCONSTANT {
Reid Spencered96d1e2007-02-08 09:08:52 +00002301 const ArrayType *ATy = dyn_cast<ArrayType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002302 if (ATy == 0)
2303 error("Cannot make array constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002304 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002305 int NumElements = ATy->getNumElements();
2306 const Type *ETy = dyn_cast<IntegerType>(ATy->getElementType());
2307 if (!ETy || cast<IntegerType>(ETy)->getBitWidth() != 8)
2308 error("String arrays require type i8, not '" + ETy->getDescription() +
2309 "'");
2310 char *EndStr = UnEscapeLexed($3, true);
2311 if (NumElements != -1 && NumElements != (EndStr-$3))
2312 error("Can't build string constant of size " +
2313 itostr((int)(EndStr-$3)) + " when array has size " +
2314 itostr(NumElements) + "");
2315 std::vector<Constant*> Vals;
2316 for (char *C = (char *)$3; C != (char *)EndStr; ++C)
2317 Vals.push_back(ConstantInt::get(ETy, *C));
2318 free($3);
2319 $$.C = ConstantArray::get(ATy, Vals);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002320 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002321 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002322 }
2323 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer9d6565a2007-02-15 02:26:10 +00002324 const VectorType *PTy = dyn_cast<VectorType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002325 if (PTy == 0)
2326 error("Cannot make packed constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002327 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002328 const Type *ETy = PTy->getElementType();
2329 int NumElements = PTy->getNumElements();
2330 // Verify that we have the correct size...
2331 if (NumElements != -1 && NumElements != (int)$3->size())
2332 error("Type mismatch: constant sized packed initialized with " +
2333 utostr($3->size()) + " arguments, but has size of " +
2334 itostr(NumElements) + "");
2335 // Verify all elements are correct type!
2336 std::vector<Constant*> Elems;
2337 for (unsigned i = 0; i < $3->size(); i++) {
2338 Constant *C = (*$3)[i].C;
2339 const Type* ValTy = C->getType();
2340 if (ETy != ValTy)
2341 error("Element #" + utostr(i) + " is not of type '" +
2342 ETy->getDescription() +"' as required!\nIt is of type '"+
2343 ValTy->getDescription() + "'");
2344 Elems.push_back(C);
2345 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00002346 $$.C = ConstantVector::get(PTy, Elems);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002347 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002348 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002349 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002350 }
2351 | Types '{' ConstVector '}' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002352 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002353 if (STy == 0)
2354 error("Cannot make struct constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002355 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002356 if ($3->size() != STy->getNumContainedTypes())
2357 error("Illegal number of initializers for structure type");
2358
2359 // Check to ensure that constants are compatible with the type initializer!
2360 std::vector<Constant*> Fields;
2361 for (unsigned i = 0, e = $3->size(); i != e; ++i) {
2362 Constant *C = (*$3)[i].C;
2363 if (C->getType() != STy->getElementType(i))
2364 error("Expected type '" + STy->getElementType(i)->getDescription() +
2365 "' for element #" + utostr(i) + " of structure initializer");
2366 Fields.push_back(C);
2367 }
2368 $$.C = ConstantStruct::get(STy, Fields);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002369 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002370 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002371 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002372 }
2373 | Types '{' '}' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002374 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002375 if (STy == 0)
2376 error("Cannot make struct constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002377 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002378 if (STy->getNumContainedTypes() != 0)
2379 error("Illegal number of initializers for structure type");
2380 $$.C = ConstantStruct::get(STy, std::vector<Constant*>());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002381 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002382 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002383 }
Reid Spencer950bf602007-01-26 08:19:09 +00002384 | Types '<' '{' ConstVector '}' '>' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002385 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002386 if (STy == 0)
2387 error("Cannot make packed struct constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002388 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002389 if ($4->size() != STy->getNumContainedTypes())
2390 error("Illegal number of initializers for packed structure type");
Reid Spencere7c3c602006-11-30 06:36:44 +00002391
Reid Spencer950bf602007-01-26 08:19:09 +00002392 // Check to ensure that constants are compatible with the type initializer!
2393 std::vector<Constant*> Fields;
2394 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
2395 Constant *C = (*$4)[i].C;
2396 if (C->getType() != STy->getElementType(i))
2397 error("Expected type '" + STy->getElementType(i)->getDescription() +
2398 "' for element #" + utostr(i) + " of packed struct initializer");
2399 Fields.push_back(C);
Reid Spencer280d8012006-12-01 23:40:53 +00002400 }
Reid Spencer950bf602007-01-26 08:19:09 +00002401 $$.C = ConstantStruct::get(STy, Fields);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002402 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002403 delete $1.PAT;
Reid Spencere77e35e2006-12-01 20:26:20 +00002404 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00002405 }
Reid Spencer950bf602007-01-26 08:19:09 +00002406 | Types '<' '{' '}' '>' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002407 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002408 if (STy == 0)
2409 error("Cannot make packed struct constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002410 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002411 if (STy->getNumContainedTypes() != 0)
2412 error("Illegal number of initializers for packed structure type");
2413 $$.C = ConstantStruct::get(STy, std::vector<Constant*>());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002414 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002415 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002416 }
2417 | Types NULL_TOK {
Reid Spencered96d1e2007-02-08 09:08:52 +00002418 const PointerType *PTy = dyn_cast<PointerType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002419 if (PTy == 0)
2420 error("Cannot make null pointer constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002421 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002422 $$.C = ConstantPointerNull::get(PTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002423 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002424 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002425 }
2426 | Types UNDEF {
Reid Spencered96d1e2007-02-08 09:08:52 +00002427 $$.C = UndefValue::get($1.PAT->get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002428 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002429 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002430 }
2431 | Types SymbolicValueRef {
Reid Spencered96d1e2007-02-08 09:08:52 +00002432 const PointerType *Ty = dyn_cast<PointerType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002433 if (Ty == 0)
2434 error("Global const reference must be a pointer type, not" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002435 $1.PAT->get()->getDescription());
Reid Spencer950bf602007-01-26 08:19:09 +00002436
2437 // ConstExprs can exist in the body of a function, thus creating
2438 // GlobalValues whenever they refer to a variable. Because we are in
2439 // the context of a function, getExistingValue will search the functions
2440 // symbol table instead of the module symbol table for the global symbol,
2441 // which throws things all off. To get around this, we just tell
2442 // getExistingValue that we are at global scope here.
2443 //
2444 Function *SavedCurFn = CurFun.CurrentFunction;
2445 CurFun.CurrentFunction = 0;
Reid Spencerbb1fd572007-03-21 17:15:50 +00002446 $2.S.copy($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002447 Value *V = getExistingValue(Ty, $2);
2448 CurFun.CurrentFunction = SavedCurFn;
2449
2450 // If this is an initializer for a constant pointer, which is referencing a
2451 // (currently) undefined variable, create a stub now that shall be replaced
2452 // in the future with the right type of variable.
2453 //
2454 if (V == 0) {
2455 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers");
2456 const PointerType *PT = cast<PointerType>(Ty);
2457
2458 // First check to see if the forward references value is already created!
2459 PerModuleInfo::GlobalRefsType::iterator I =
2460 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
2461
2462 if (I != CurModule.GlobalRefs.end()) {
2463 V = I->second; // Placeholder already exists, use it...
2464 $2.destroy();
2465 } else {
2466 std::string Name;
2467 if ($2.Type == ValID::NameVal) Name = $2.Name;
2468
2469 // Create the forward referenced global.
2470 GlobalValue *GV;
2471 if (const FunctionType *FTy =
2472 dyn_cast<FunctionType>(PT->getElementType())) {
2473 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
2474 CurModule.CurrentModule);
2475 } else {
2476 GV = new GlobalVariable(PT->getElementType(), false,
2477 GlobalValue::ExternalLinkage, 0,
2478 Name, CurModule.CurrentModule);
2479 }
2480
2481 // Keep track of the fact that we have a forward ref to recycle it
2482 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
2483 V = GV;
2484 }
2485 }
2486 $$.C = cast<GlobalValue>(V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002487 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002488 delete $1.PAT; // Free the type handle
Reid Spencer950bf602007-01-26 08:19:09 +00002489 }
2490 | Types ConstExpr {
Reid Spencered96d1e2007-02-08 09:08:52 +00002491 if ($1.PAT->get() != $2.C->getType())
Reid Spencer950bf602007-01-26 08:19:09 +00002492 error("Mismatched types for constant expression");
2493 $$ = $2;
Reid Spencerbb1fd572007-03-21 17:15:50 +00002494 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002495 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002496 }
2497 | Types ZEROINITIALIZER {
Reid Spencered96d1e2007-02-08 09:08:52 +00002498 const Type *Ty = $1.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00002499 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
2500 error("Cannot create a null initialized value of this type");
2501 $$.C = Constant::getNullValue(Ty);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002502 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002503 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002504 }
2505 | SIntType EINT64VAL { // integral constants
2506 const Type *Ty = $1.T;
2507 if (!ConstantInt::isValueValidForType(Ty, $2))
2508 error("Constant value doesn't fit in type");
2509 $$.C = ConstantInt::get(Ty, $2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002510 $$.S.makeSigned();
Reid Spencer950bf602007-01-26 08:19:09 +00002511 }
2512 | UIntType EUINT64VAL { // integral constants
2513 const Type *Ty = $1.T;
2514 if (!ConstantInt::isValueValidForType(Ty, $2))
2515 error("Constant value doesn't fit in type");
2516 $$.C = ConstantInt::get(Ty, $2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002517 $$.S.makeUnsigned();
Reid Spencer950bf602007-01-26 08:19:09 +00002518 }
2519 | BOOL TRUETOK { // Boolean constants
2520 $$.C = ConstantInt::get(Type::Int1Ty, true);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002521 $$.S.makeUnsigned();
Reid Spencer950bf602007-01-26 08:19:09 +00002522 }
2523 | BOOL FALSETOK { // Boolean constants
2524 $$.C = ConstantInt::get(Type::Int1Ty, false);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002525 $$.S.makeUnsigned();
Reid Spencer950bf602007-01-26 08:19:09 +00002526 }
2527 | FPType FPVAL { // Float & Double constants
2528 if (!ConstantFP::isValueValidForType($1.T, $2))
2529 error("Floating point constant invalid for type");
2530 $$.C = ConstantFP::get($1.T, $2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002531 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002532 }
2533 ;
2534
2535ConstExpr
2536 : CastOps '(' ConstVal TO Types ')' {
2537 const Type* SrcTy = $3.C->getType();
Reid Spencered96d1e2007-02-08 09:08:52 +00002538 const Type* DstTy = $5.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00002539 Signedness SrcSign($3.S);
2540 Signedness DstSign($5.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002541 if (!SrcTy->isFirstClassType())
2542 error("cast constant expression from a non-primitive type: '" +
2543 SrcTy->getDescription() + "'");
2544 if (!DstTy->isFirstClassType())
2545 error("cast constant expression to a non-primitive type: '" +
2546 DstTy->getDescription() + "'");
2547 $$.C = cast<Constant>(getCast($1, $3.C, SrcSign, DstTy, DstSign));
Reid Spencerbb1fd572007-03-21 17:15:50 +00002548 $$.S.copy(DstSign);
Reid Spencered96d1e2007-02-08 09:08:52 +00002549 delete $5.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002550 }
2551 | GETELEMENTPTR '(' ConstVal IndexList ')' {
2552 const Type *Ty = $3.C->getType();
2553 if (!isa<PointerType>(Ty))
2554 error("GetElementPtr requires a pointer operand");
2555
Reid Spencer950bf602007-01-26 08:19:09 +00002556 std::vector<Constant*> CIndices;
Reid Spencerff0e4482007-04-16 00:40:57 +00002557 upgradeGEPCEIndices($3.C->getType(), $4, CIndices);
Reid Spencer950bf602007-01-26 08:19:09 +00002558
2559 delete $4;
Chris Lattner4227bdb2007-02-19 07:34:02 +00002560 $$.C = ConstantExpr::getGetElementPtr($3.C, &CIndices[0], CIndices.size());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002561 $$.S.copy(getElementSign($3, CIndices));
Reid Spencer950bf602007-01-26 08:19:09 +00002562 }
Reid Spencere7c3c602006-11-30 06:36:44 +00002563 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002564 if (!$3.C->getType()->isInteger() ||
2565 cast<IntegerType>($3.C->getType())->getBitWidth() != 1)
2566 error("Select condition must be bool type");
2567 if ($5.C->getType() != $7.C->getType())
2568 error("Select operand types must match");
2569 $$.C = ConstantExpr::getSelect($3.C, $5.C, $7.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002570 $$.S.copy($5.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002571 }
2572 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002573 const Type *Ty = $3.C->getType();
2574 if (Ty != $5.C->getType())
2575 error("Binary operator types must match");
2576 // First, make sure we're dealing with the right opcode by upgrading from
2577 // obsolete versions.
2578 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $3.S);
2579
2580 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
2581 // To retain backward compatibility with these early compilers, we emit a
2582 // cast to the appropriate integer type automatically if we are in the
2583 // broken case. See PR424 for more information.
2584 if (!isa<PointerType>(Ty)) {
2585 $$.C = ConstantExpr::get(Opcode, $3.C, $5.C);
2586 } else {
2587 const Type *IntPtrTy = 0;
2588 switch (CurModule.CurrentModule->getPointerSize()) {
2589 case Module::Pointer32: IntPtrTy = Type::Int32Ty; break;
2590 case Module::Pointer64: IntPtrTy = Type::Int64Ty; break;
2591 default: error("invalid pointer binary constant expr");
2592 }
2593 $$.C = ConstantExpr::get(Opcode,
2594 ConstantExpr::getCast(Instruction::PtrToInt, $3.C, IntPtrTy),
2595 ConstantExpr::getCast(Instruction::PtrToInt, $5.C, IntPtrTy));
2596 $$.C = ConstantExpr::getCast(Instruction::IntToPtr, $$.C, Ty);
2597 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00002598 $$.S.copy($3.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002599 }
2600 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002601 const Type* Ty = $3.C->getType();
2602 if (Ty != $5.C->getType())
2603 error("Logical operator types must match");
2604 if (!Ty->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00002605 if (!isa<VectorType>(Ty) ||
2606 !cast<VectorType>(Ty)->getElementType()->isInteger())
Reid Spencer950bf602007-01-26 08:19:09 +00002607 error("Logical operator requires integer operands");
2608 }
2609 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $3.S);
2610 $$.C = ConstantExpr::get(Opcode, $3.C, $5.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002611 $$.S.copy($3.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002612 }
2613 | SetCondOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002614 const Type* Ty = $3.C->getType();
2615 if (Ty != $5.C->getType())
2616 error("setcc operand types must match");
2617 unsigned short pred;
2618 Instruction::OtherOps Opcode = getCompareOp($1, pred, Ty, $3.S);
2619 $$.C = ConstantExpr::getCompare(Opcode, $3.C, $5.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002620 $$.S.makeUnsigned();
Reid Spencere7c3c602006-11-30 06:36:44 +00002621 }
Reid Spencer57f28f92006-12-03 07:10:26 +00002622 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002623 if ($4.C->getType() != $6.C->getType())
2624 error("icmp operand types must match");
2625 $$.C = ConstantExpr::getCompare($2, $4.C, $6.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002626 $$.S.makeUnsigned();
Reid Spencer57f28f92006-12-03 07:10:26 +00002627 }
2628 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002629 if ($4.C->getType() != $6.C->getType())
2630 error("fcmp operand types must match");
2631 $$.C = ConstantExpr::getCompare($2, $4.C, $6.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002632 $$.S.makeUnsigned();
Reid Spencer229e9362006-12-02 22:14:11 +00002633 }
Reid Spencere7c3c602006-11-30 06:36:44 +00002634 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002635 if (!$5.C->getType()->isInteger() ||
2636 cast<IntegerType>($5.C->getType())->getBitWidth() != 8)
2637 error("Shift count for shift constant must be unsigned byte");
Reid Spencer832254e2007-02-02 02:16:23 +00002638 const Type* Ty = $3.C->getType();
Reid Spencer950bf602007-01-26 08:19:09 +00002639 if (!$3.C->getType()->isInteger())
2640 error("Shift constant expression requires integer operand");
Reid Spencer832254e2007-02-02 02:16:23 +00002641 Constant *ShiftAmt = ConstantExpr::getZExt($5.C, Ty);
2642 $$.C = ConstantExpr::get(getBinaryOp($1, Ty, $3.S), $3.C, ShiftAmt);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002643 $$.S.copy($3.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002644 }
2645 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002646 if (!ExtractElementInst::isValidOperands($3.C, $5.C))
2647 error("Invalid extractelement operands");
2648 $$.C = ConstantExpr::getExtractElement($3.C, $5.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002649 $$.S.copy($3.S.get(0));
Reid Spencere7c3c602006-11-30 06:36:44 +00002650 }
2651 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002652 if (!InsertElementInst::isValidOperands($3.C, $5.C, $7.C))
2653 error("Invalid insertelement operands");
2654 $$.C = ConstantExpr::getInsertElement($3.C, $5.C, $7.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002655 $$.S.copy($3.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002656 }
2657 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002658 if (!ShuffleVectorInst::isValidOperands($3.C, $5.C, $7.C))
2659 error("Invalid shufflevector operands");
2660 $$.C = ConstantExpr::getShuffleVector($3.C, $5.C, $7.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002661 $$.S.copy($3.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002662 }
2663 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002664
2665
2666// ConstVector - A list of comma separated constants.
Reid Spencere77e35e2006-12-01 20:26:20 +00002667ConstVector
Reid Spencer950bf602007-01-26 08:19:09 +00002668 : ConstVector ',' ConstVal { ($$ = $1)->push_back($3); }
2669 | ConstVal {
2670 $$ = new std::vector<ConstInfo>();
2671 $$->push_back($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00002672 }
Reid Spencere77e35e2006-12-01 20:26:20 +00002673 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002674
2675
2676// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Reid Spencer950bf602007-01-26 08:19:09 +00002677GlobalType
2678 : GLOBAL { $$ = false; }
2679 | CONSTANT { $$ = true; }
2680 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002681
2682
2683//===----------------------------------------------------------------------===//
2684// Rules to match Modules
2685//===----------------------------------------------------------------------===//
2686
2687// Module rule: Capture the result of parsing the whole file into a result
2688// variable...
2689//
Reid Spencer950bf602007-01-26 08:19:09 +00002690Module
2691 : FunctionList {
2692 $$ = ParserResult = $1;
2693 CurModule.ModuleDone();
Reid Spencere7c3c602006-11-30 06:36:44 +00002694 }
Jeff Cohenac2dca92007-01-21 19:30:52 +00002695 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002696
Reid Spencer950bf602007-01-26 08:19:09 +00002697// FunctionList - A list of functions, preceeded by a constant pool.
2698//
2699FunctionList
2700 : FunctionList Function { $$ = $1; CurFun.FunctionDone(); }
2701 | FunctionList FunctionProto { $$ = $1; }
2702 | FunctionList MODULE ASM_TOK AsmBlock { $$ = $1; }
2703 | FunctionList IMPLEMENTATION { $$ = $1; }
2704 | ConstPool {
2705 $$ = CurModule.CurrentModule;
2706 // Emit an error if there are any unresolved types left.
2707 if (!CurModule.LateResolveTypes.empty()) {
2708 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
2709 if (DID.Type == ValID::NameVal) {
2710 error("Reference to an undefined type: '"+DID.getName() + "'");
2711 } else {
2712 error("Reference to an undefined type: #" + itostr(DID.Num));
2713 }
2714 }
2715 }
2716 ;
Reid Spencer78720742006-12-02 20:21:22 +00002717
Reid Spencere7c3c602006-11-30 06:36:44 +00002718// ConstPool - Constants with optional names assigned to them.
Reid Spencer950bf602007-01-26 08:19:09 +00002719ConstPool
2720 : ConstPool OptAssign TYPE TypesV {
2721 // Eagerly resolve types. This is not an optimization, this is a
2722 // requirement that is due to the fact that we could have this:
2723 //
2724 // %list = type { %list * }
2725 // %list = type { %list * } ; repeated type decl
2726 //
2727 // If types are not resolved eagerly, then the two types will not be
2728 // determined to be the same type!
2729 //
Reid Spencerbb1fd572007-03-21 17:15:50 +00002730 ResolveTypeTo($2, $4.PAT->get(), $4.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002731
Reid Spencerbb1fd572007-03-21 17:15:50 +00002732 if (!setTypeName($4, $2) && !$2) {
2733 // If this is a numbered type that is not a redefinition, add it to the
2734 // slot table.
2735 CurModule.Types.push_back($4.PAT->get());
2736 CurModule.TypeSigns.push_back($4.S);
Reid Spencera50d5962006-12-02 04:11:07 +00002737 }
Reid Spencered96d1e2007-02-08 09:08:52 +00002738 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002739 }
2740 | ConstPool FunctionProto { // Function prototypes can be in const pool
Reid Spencere7c3c602006-11-30 06:36:44 +00002741 }
2742 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
Reid Spencere7c3c602006-11-30 06:36:44 +00002743 }
Reid Spencer950bf602007-01-26 08:19:09 +00002744 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
2745 if ($5.C == 0)
2746 error("Global value initializer is not a constant");
Reid Spencerbb1fd572007-03-21 17:15:50 +00002747 CurGV = ParseGlobalVariable($2, $3, $4, $5.C->getType(), $5.C, $5.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002748 } GlobalVarAttributes {
2749 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002750 }
Reid Spencer950bf602007-01-26 08:19:09 +00002751 | ConstPool OptAssign EXTERNAL GlobalType Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00002752 const Type *Ty = $5.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00002753 CurGV = ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, Ty, 0,
2754 $5.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002755 delete $5.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002756 } GlobalVarAttributes {
2757 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002758 }
Reid Spencer950bf602007-01-26 08:19:09 +00002759 | ConstPool OptAssign DLLIMPORT GlobalType Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00002760 const Type *Ty = $5.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00002761 CurGV = ParseGlobalVariable($2, GlobalValue::DLLImportLinkage, $4, Ty, 0,
2762 $5.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002763 delete $5.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002764 } GlobalVarAttributes {
2765 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002766 }
Reid Spencer950bf602007-01-26 08:19:09 +00002767 | ConstPool OptAssign EXTERN_WEAK GlobalType Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00002768 const Type *Ty = $5.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00002769 CurGV =
Reid Spencerbb1fd572007-03-21 17:15:50 +00002770 ParseGlobalVariable($2, GlobalValue::ExternalWeakLinkage, $4, Ty, 0,
2771 $5.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002772 delete $5.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002773 } GlobalVarAttributes {
2774 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002775 }
2776 | ConstPool TARGET TargetDefinition {
Reid Spencere7c3c602006-11-30 06:36:44 +00002777 }
2778 | ConstPool DEPLIBS '=' LibrariesDefinition {
Reid Spencere7c3c602006-11-30 06:36:44 +00002779 }
2780 | /* empty: end of list */ {
Reid Spencer950bf602007-01-26 08:19:09 +00002781 }
2782 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002783
Reid Spencer950bf602007-01-26 08:19:09 +00002784AsmBlock
2785 : STRINGCONSTANT {
2786 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
2787 char *EndStr = UnEscapeLexed($1, true);
2788 std::string NewAsm($1, EndStr);
2789 free($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00002790
Reid Spencer950bf602007-01-26 08:19:09 +00002791 if (AsmSoFar.empty())
2792 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
2793 else
2794 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
2795 }
2796 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002797
Reid Spencer950bf602007-01-26 08:19:09 +00002798BigOrLittle
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00002799 : BIG { $$ = Module::BigEndian; }
Reid Spencer950bf602007-01-26 08:19:09 +00002800 | LITTLE { $$ = Module::LittleEndian; }
2801 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002802
2803TargetDefinition
2804 : ENDIAN '=' BigOrLittle {
Reid Spencer950bf602007-01-26 08:19:09 +00002805 CurModule.setEndianness($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00002806 }
2807 | POINTERSIZE '=' EUINT64VAL {
Reid Spencer950bf602007-01-26 08:19:09 +00002808 if ($3 == 32)
2809 CurModule.setPointerSize(Module::Pointer32);
2810 else if ($3 == 64)
2811 CurModule.setPointerSize(Module::Pointer64);
2812 else
2813 error("Invalid pointer size: '" + utostr($3) + "'");
Reid Spencere7c3c602006-11-30 06:36:44 +00002814 }
2815 | TRIPLE '=' STRINGCONSTANT {
Reid Spencer950bf602007-01-26 08:19:09 +00002816 CurModule.CurrentModule->setTargetTriple($3);
2817 free($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00002818 }
2819 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer950bf602007-01-26 08:19:09 +00002820 CurModule.CurrentModule->setDataLayout($3);
2821 free($3);
2822 }
2823 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002824
2825LibrariesDefinition
Reid Spencer950bf602007-01-26 08:19:09 +00002826 : '[' LibList ']'
2827 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002828
2829LibList
2830 : LibList ',' STRINGCONSTANT {
Reid Spencer950bf602007-01-26 08:19:09 +00002831 CurModule.CurrentModule->addLibrary($3);
2832 free($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00002833 }
Reid Spencer950bf602007-01-26 08:19:09 +00002834 | STRINGCONSTANT {
2835 CurModule.CurrentModule->addLibrary($1);
2836 free($1);
2837 }
2838 | /* empty: end of list */ { }
2839 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002840
2841//===----------------------------------------------------------------------===//
2842// Rules to match Function Headers
2843//===----------------------------------------------------------------------===//
2844
Reid Spencer950bf602007-01-26 08:19:09 +00002845Name
2846 : VAR_ID | STRINGCONSTANT
2847 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002848
Reid Spencer950bf602007-01-26 08:19:09 +00002849OptName
2850 : Name
2851 | /*empty*/ { $$ = 0; }
2852 ;
2853
2854ArgVal
2855 : Types OptName {
Reid Spencered96d1e2007-02-08 09:08:52 +00002856 if ($1.PAT->get() == Type::VoidTy)
Reid Spencer950bf602007-01-26 08:19:09 +00002857 error("void typed arguments are invalid");
2858 $$ = new std::pair<PATypeInfo, char*>($1, $2);
Reid Spencer52402b02007-01-02 05:45:11 +00002859 }
Reid Spencer950bf602007-01-26 08:19:09 +00002860 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002861
Reid Spencer950bf602007-01-26 08:19:09 +00002862ArgListH
2863 : ArgListH ',' ArgVal {
2864 $$ = $1;
2865 $$->push_back(*$3);
Reid Spencere77e35e2006-12-01 20:26:20 +00002866 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002867 }
2868 | ArgVal {
Reid Spencer950bf602007-01-26 08:19:09 +00002869 $$ = new std::vector<std::pair<PATypeInfo,char*> >();
2870 $$->push_back(*$1);
2871 delete $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00002872 }
Reid Spencer950bf602007-01-26 08:19:09 +00002873 ;
2874
2875ArgList
2876 : ArgListH { $$ = $1; }
Reid Spencere7c3c602006-11-30 06:36:44 +00002877 | ArgListH ',' DOTDOTDOT {
Reid Spencere7c3c602006-11-30 06:36:44 +00002878 $$ = $1;
Reid Spencer950bf602007-01-26 08:19:09 +00002879 PATypeInfo VoidTI;
Reid Spencered96d1e2007-02-08 09:08:52 +00002880 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002881 VoidTI.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002882 $$->push_back(std::pair<PATypeInfo, char*>(VoidTI, 0));
Reid Spencere7c3c602006-11-30 06:36:44 +00002883 }
2884 | DOTDOTDOT {
Reid Spencer950bf602007-01-26 08:19:09 +00002885 $$ = new std::vector<std::pair<PATypeInfo,char*> >();
2886 PATypeInfo VoidTI;
Reid Spencered96d1e2007-02-08 09:08:52 +00002887 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002888 VoidTI.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002889 $$->push_back(std::pair<PATypeInfo, char*>(VoidTI, 0));
Reid Spencere7c3c602006-11-30 06:36:44 +00002890 }
Reid Spencer950bf602007-01-26 08:19:09 +00002891 | /* empty */ { $$ = 0; }
2892 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002893
Reid Spencer71d2ec92006-12-31 06:02:26 +00002894FunctionHeaderH
2895 : OptCallingConv TypesV Name '(' ArgList ')' OptSection OptAlign {
Reid Spencer950bf602007-01-26 08:19:09 +00002896 UnEscapeLexed($3);
2897 std::string FunctionName($3);
2898 free($3); // Free strdup'd memory!
Reid Spencere7c3c602006-11-30 06:36:44 +00002899
Reid Spencered96d1e2007-02-08 09:08:52 +00002900 const Type* RetTy = $2.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00002901
2902 if (!RetTy->isFirstClassType() && RetTy != Type::VoidTy)
2903 error("LLVM functions cannot return aggregate types");
2904
Reid Spencerbb1fd572007-03-21 17:15:50 +00002905 Signedness FTySign;
2906 FTySign.makeComposite($2.S);
Reid Spenceref9b9a72007-02-05 20:47:22 +00002907 std::vector<const Type*> ParamTyList;
Reid Spencer950bf602007-01-26 08:19:09 +00002908
2909 // In LLVM 2.0 the signatures of three varargs intrinsics changed to take
2910 // i8*. We check here for those names and override the parameter list
2911 // types to ensure the prototype is correct.
2912 if (FunctionName == "llvm.va_start" || FunctionName == "llvm.va_end") {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002913 ParamTyList.push_back(PointerType::get(Type::Int8Ty));
Reid Spencer950bf602007-01-26 08:19:09 +00002914 } else if (FunctionName == "llvm.va_copy") {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002915 ParamTyList.push_back(PointerType::get(Type::Int8Ty));
2916 ParamTyList.push_back(PointerType::get(Type::Int8Ty));
Reid Spencer950bf602007-01-26 08:19:09 +00002917 } else if ($5) { // If there are arguments...
2918 for (std::vector<std::pair<PATypeInfo,char*> >::iterator
2919 I = $5->begin(), E = $5->end(); I != E; ++I) {
Reid Spencered96d1e2007-02-08 09:08:52 +00002920 const Type *Ty = I->first.PAT->get();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002921 ParamTyList.push_back(Ty);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002922 FTySign.add(I->first.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002923 }
2924 }
2925
Reid Spenceref9b9a72007-02-05 20:47:22 +00002926 bool isVarArg = ParamTyList.size() && ParamTyList.back() == Type::VoidTy;
2927 if (isVarArg)
2928 ParamTyList.pop_back();
Reid Spencer950bf602007-01-26 08:19:09 +00002929
Reid Spencerb7046c72007-01-29 05:41:34 +00002930 // Convert the CSRet calling convention into the corresponding parameter
2931 // attribute.
Reid Spencer7b5d4662007-04-09 06:16:21 +00002932 ParamAttrsList *ParamAttrs = 0;
Reid Spencerb7046c72007-01-29 05:41:34 +00002933 if ($1 == OldCallingConv::CSRet) {
Reid Spencer7b5d4662007-04-09 06:16:21 +00002934 ParamAttrs = new ParamAttrsList();
Reid Spencer18da0722007-04-11 02:44:20 +00002935 ParamAttrs->addAttributes(0, ParamAttr::None); // result
2936 ParamAttrs->addAttributes(1, ParamAttr::StructRet); // first arg
Reid Spencerb7046c72007-01-29 05:41:34 +00002937 }
2938
Reid Spencer7b5d4662007-04-09 06:16:21 +00002939 const FunctionType *FT =
2940 FunctionType::get(RetTy, ParamTyList, isVarArg, ParamAttrs);
Reid Spencer950bf602007-01-26 08:19:09 +00002941 const PointerType *PFT = PointerType::get(FT);
Reid Spencered96d1e2007-02-08 09:08:52 +00002942 delete $2.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002943
2944 ValID ID;
2945 if (!FunctionName.empty()) {
Reid Spencer5eb77c72007-03-15 03:26:42 +00002946 ID = ValID::create((char*)FunctionName.c_str());
Reid Spencer950bf602007-01-26 08:19:09 +00002947 } else {
Reid Spencer5eb77c72007-03-15 03:26:42 +00002948 ID = ValID::create((int)CurModule.Values[PFT].size());
Reid Spencer950bf602007-01-26 08:19:09 +00002949 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00002950 ID.S.makeComposite(FTySign);
Reid Spencer950bf602007-01-26 08:19:09 +00002951
2952 Function *Fn = 0;
Reid Spencered96d1e2007-02-08 09:08:52 +00002953 Module* M = CurModule.CurrentModule;
2954
Reid Spencer950bf602007-01-26 08:19:09 +00002955 // See if this function was forward referenced. If so, recycle the object.
2956 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2957 // Move the function to the end of the list, from whereever it was
2958 // previously inserted.
2959 Fn = cast<Function>(FWRef);
Reid Spencered96d1e2007-02-08 09:08:52 +00002960 M->getFunctionList().remove(Fn);
2961 M->getFunctionList().push_back(Fn);
2962 } else if (!FunctionName.empty()) {
2963 GlobalValue *Conflict = M->getFunction(FunctionName);
2964 if (!Conflict)
2965 Conflict = M->getNamedGlobal(FunctionName);
2966 if (Conflict && PFT == Conflict->getType()) {
2967 if (!CurFun.isDeclare && !Conflict->isDeclaration()) {
2968 // We have two function definitions that conflict, same type, same
2969 // name. We should really check to make sure that this is the result
2970 // of integer type planes collapsing and generate an error if it is
2971 // not, but we'll just rename on the assumption that it is. However,
2972 // let's do it intelligently and rename the internal linkage one
2973 // if there is one.
2974 std::string NewName(makeNameUnique(FunctionName));
2975 if (Conflict->hasInternalLinkage()) {
2976 Conflict->setName(NewName);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002977 RenameMapKey Key =
2978 makeRenameMapKey(FunctionName, Conflict->getType(), ID.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002979 CurModule.RenameMap[Key] = NewName;
2980 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
2981 InsertValue(Fn, CurModule.Values);
2982 } else {
2983 Fn = new Function(FT, CurFun.Linkage, NewName, M);
2984 InsertValue(Fn, CurModule.Values);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002985 RenameMapKey Key =
2986 makeRenameMapKey(FunctionName, PFT, ID.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002987 CurModule.RenameMap[Key] = NewName;
2988 }
2989 } else {
2990 // If they are not both definitions, then just use the function we
2991 // found since the types are the same.
2992 Fn = cast<Function>(Conflict);
Reid Spenceref9b9a72007-02-05 20:47:22 +00002993
Reid Spencered96d1e2007-02-08 09:08:52 +00002994 // Make sure to strip off any argument names so we can't get
2995 // conflicts.
2996 if (Fn->isDeclaration())
2997 for (Function::arg_iterator AI = Fn->arg_begin(),
2998 AE = Fn->arg_end(); AI != AE; ++AI)
2999 AI->setName("");
3000 }
3001 } else if (Conflict) {
3002 // We have two globals with the same name and different types.
3003 // Previously, this was permitted because the symbol table had
3004 // "type planes" and names only needed to be distinct within a
3005 // type plane. After PR411 was fixed, this is no loner the case.
3006 // To resolve this we must rename one of the two.
3007 if (Conflict->hasInternalLinkage()) {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003008 // We can safely rename the Conflict.
3009 RenameMapKey Key =
3010 makeRenameMapKey(Conflict->getName(), Conflict->getType(),
3011 CurModule.NamedValueSigns[Conflict->getName()]);
Reid Spencered96d1e2007-02-08 09:08:52 +00003012 Conflict->setName(makeNameUnique(Conflict->getName()));
Reid Spencered96d1e2007-02-08 09:08:52 +00003013 CurModule.RenameMap[Key] = Conflict->getName();
3014 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
3015 InsertValue(Fn, CurModule.Values);
Reid Spencerd2920cd2007-03-21 17:27:53 +00003016 } else {
Reid Spencered96d1e2007-02-08 09:08:52 +00003017 // We can't quietly rename either of these things, but we must
Reid Spencerd2920cd2007-03-21 17:27:53 +00003018 // rename one of them. Only if the function's linkage is internal can
3019 // we forgo a warning message about the renamed function.
Reid Spencered96d1e2007-02-08 09:08:52 +00003020 std::string NewName = makeNameUnique(FunctionName);
Reid Spencerd2920cd2007-03-21 17:27:53 +00003021 if (CurFun.Linkage != GlobalValue::InternalLinkage) {
3022 warning("Renaming function '" + FunctionName + "' as '" + NewName +
3023 "' may cause linkage errors");
3024 }
3025 // Elect to rename the thing we're now defining.
Reid Spencered96d1e2007-02-08 09:08:52 +00003026 Fn = new Function(FT, CurFun.Linkage, NewName, M);
3027 InsertValue(Fn, CurModule.Values);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003028 RenameMapKey Key = makeRenameMapKey(FunctionName, PFT, ID.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003029 CurModule.RenameMap[Key] = NewName;
Reid Spencerd2920cd2007-03-21 17:27:53 +00003030 }
Reid Spenceref9b9a72007-02-05 20:47:22 +00003031 } else {
Reid Spencered96d1e2007-02-08 09:08:52 +00003032 // There's no conflict, just define the function
3033 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
3034 InsertValue(Fn, CurModule.Values);
Reid Spenceref9b9a72007-02-05 20:47:22 +00003035 }
Reid Spencer950bf602007-01-26 08:19:09 +00003036 }
3037
3038 CurFun.FunctionStart(Fn);
3039
3040 if (CurFun.isDeclare) {
3041 // If we have declaration, always overwrite linkage. This will allow us
3042 // to correctly handle cases, when pointer to function is passed as
3043 // argument to another function.
3044 Fn->setLinkage(CurFun.Linkage);
3045 }
Reid Spencerb7046c72007-01-29 05:41:34 +00003046 Fn->setCallingConv(upgradeCallingConv($1));
Reid Spencer950bf602007-01-26 08:19:09 +00003047 Fn->setAlignment($8);
3048 if ($7) {
3049 Fn->setSection($7);
3050 free($7);
3051 }
3052
3053 // Add all of the arguments we parsed to the function...
3054 if ($5) { // Is null if empty...
3055 if (isVarArg) { // Nuke the last entry
Reid Spencered96d1e2007-02-08 09:08:52 +00003056 assert($5->back().first.PAT->get() == Type::VoidTy &&
Reid Spencer950bf602007-01-26 08:19:09 +00003057 $5->back().second == 0 && "Not a varargs marker");
Reid Spencered96d1e2007-02-08 09:08:52 +00003058 delete $5->back().first.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003059 $5->pop_back(); // Delete the last entry
3060 }
3061 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00003062 Function::arg_iterator ArgEnd = Fn->arg_end();
3063 std::vector<std::pair<PATypeInfo,char*> >::iterator I = $5->begin();
3064 std::vector<std::pair<PATypeInfo,char*> >::iterator E = $5->end();
3065 for ( ; I != E && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencered96d1e2007-02-08 09:08:52 +00003066 delete I->first.PAT; // Delete the typeholder...
Reid Spencerbb1fd572007-03-21 17:15:50 +00003067 ValueInfo VI; VI.V = ArgIt; VI.S.copy(I->first.S);
3068 setValueName(VI, I->second); // Insert arg into symtab...
Reid Spencer950bf602007-01-26 08:19:09 +00003069 InsertValue(ArgIt);
3070 }
3071 delete $5; // We're now done with the argument list
3072 }
3073 }
3074 ;
3075
3076BEGIN
3077 : BEGINTOK | '{' // Allow BEGIN or '{' to start a function
Jeff Cohenac2dca92007-01-21 19:30:52 +00003078 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003079
Reid Spencer6fd36ab2006-12-29 20:35:03 +00003080FunctionHeader
Reid Spencerd2920cd2007-03-21 17:27:53 +00003081 : OptLinkage { CurFun.Linkage = $1; } FunctionHeaderH BEGIN {
Reid Spencer950bf602007-01-26 08:19:09 +00003082 $$ = CurFun.CurrentFunction;
3083
3084 // Make sure that we keep track of the linkage type even if there was a
3085 // previous "declare".
3086 $$->setLinkage($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00003087 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00003088 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003089
Reid Spencer950bf602007-01-26 08:19:09 +00003090END
3091 : ENDTOK | '}' // Allow end of '}' to end a function
3092 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003093
Reid Spencer950bf602007-01-26 08:19:09 +00003094Function
3095 : BasicBlockList END {
3096 $$ = $1;
3097 };
Reid Spencere7c3c602006-11-30 06:36:44 +00003098
Reid Spencere77e35e2006-12-01 20:26:20 +00003099FnDeclareLinkage
Reid Spencered96d1e2007-02-08 09:08:52 +00003100 : /*default*/ { $$ = GlobalValue::ExternalLinkage; }
3101 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
3102 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencere7c3c602006-11-30 06:36:44 +00003103 ;
3104
3105FunctionProto
Reid Spencered96d1e2007-02-08 09:08:52 +00003106 : DECLARE { CurFun.isDeclare = true; }
3107 FnDeclareLinkage { CurFun.Linkage = $3; } FunctionHeaderH {
Reid Spencer950bf602007-01-26 08:19:09 +00003108 $$ = CurFun.CurrentFunction;
3109 CurFun.FunctionDone();
3110
3111 }
3112 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003113
3114//===----------------------------------------------------------------------===//
3115// Rules to match Basic Blocks
3116//===----------------------------------------------------------------------===//
3117
Reid Spencer950bf602007-01-26 08:19:09 +00003118OptSideEffect
3119 : /* empty */ { $$ = false; }
3120 | SIDEEFFECT { $$ = true; }
3121 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003122
Reid Spencere77e35e2006-12-01 20:26:20 +00003123ConstValueRef
Reid Spencer950bf602007-01-26 08:19:09 +00003124 // A reference to a direct constant
Reid Spencerbb1fd572007-03-21 17:15:50 +00003125 : ESINT64VAL { $$ = ValID::create($1); }
Reid Spencer950bf602007-01-26 08:19:09 +00003126 | EUINT64VAL { $$ = ValID::create($1); }
3127 | FPVAL { $$ = ValID::create($1); }
Reid Spencerbb1fd572007-03-21 17:15:50 +00003128 | TRUETOK {
3129 $$ = ValID::create(ConstantInt::get(Type::Int1Ty, true));
3130 $$.S.makeUnsigned();
3131 }
3132 | FALSETOK {
3133 $$ = ValID::create(ConstantInt::get(Type::Int1Ty, false));
3134 $$.S.makeUnsigned();
3135 }
Reid Spencer950bf602007-01-26 08:19:09 +00003136 | NULL_TOK { $$ = ValID::createNull(); }
3137 | UNDEF { $$ = ValID::createUndef(); }
3138 | ZEROINITIALIZER { $$ = ValID::createZeroInit(); }
3139 | '<' ConstVector '>' { // Nonempty unsized packed vector
3140 const Type *ETy = (*$2)[0].C->getType();
3141 int NumElements = $2->size();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003142 VectorType* pt = VectorType::get(ETy, NumElements);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003143 $$.S.makeComposite((*$2)[0].S);
3144 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(pt, $$.S));
Reid Spencer950bf602007-01-26 08:19:09 +00003145
3146 // Verify all elements are correct type!
3147 std::vector<Constant*> Elems;
3148 for (unsigned i = 0; i < $2->size(); i++) {
3149 Constant *C = (*$2)[i].C;
3150 const Type *CTy = C->getType();
3151 if (ETy != CTy)
3152 error("Element #" + utostr(i) + " is not of type '" +
3153 ETy->getDescription() +"' as required!\nIt is of type '" +
3154 CTy->getDescription() + "'");
3155 Elems.push_back(C);
Reid Spencere7c3c602006-11-30 06:36:44 +00003156 }
Reid Spencer5eb77c72007-03-15 03:26:42 +00003157 $$ = ValID::create(ConstantVector::get(pt, Elems));
Reid Spencer950bf602007-01-26 08:19:09 +00003158 delete PTy; delete $2;
3159 }
3160 | ConstExpr {
Reid Spencer5eb77c72007-03-15 03:26:42 +00003161 $$ = ValID::create($1.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003162 $$.S.copy($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003163 }
3164 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
3165 char *End = UnEscapeLexed($3, true);
3166 std::string AsmStr = std::string($3, End);
3167 End = UnEscapeLexed($5, true);
3168 std::string Constraints = std::string($5, End);
3169 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
3170 free($3);
3171 free($5);
3172 }
3173 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003174
Reid Spencerbb1fd572007-03-21 17:15:50 +00003175// SymbolicValueRef - Reference to one of two ways of symbolically refering to // another value.
Reid Spencer950bf602007-01-26 08:19:09 +00003176//
3177SymbolicValueRef
Reid Spencerbb1fd572007-03-21 17:15:50 +00003178 : INTVAL { $$ = ValID::create($1); $$.S.makeSignless(); }
3179 | Name { $$ = ValID::create($1); $$.S.makeSignless(); }
Reid Spencer950bf602007-01-26 08:19:09 +00003180 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003181
3182// ValueRef - A reference to a definition... either constant or symbolic
Reid Spencerf459d392006-12-02 16:19:52 +00003183ValueRef
Reid Spencer950bf602007-01-26 08:19:09 +00003184 : SymbolicValueRef | ConstValueRef
Reid Spencerf459d392006-12-02 16:19:52 +00003185 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003186
Reid Spencer950bf602007-01-26 08:19:09 +00003187
Reid Spencere7c3c602006-11-30 06:36:44 +00003188// ResolvedVal - a <type> <value> pair. This is used only in cases where the
3189// type immediately preceeds the value reference, and allows complex constant
3190// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Reid Spencer950bf602007-01-26 08:19:09 +00003191ResolvedVal
3192 : Types ValueRef {
Reid Spencered96d1e2007-02-08 09:08:52 +00003193 const Type *Ty = $1.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003194 $2.S.copy($1.S);
Reid Spencer5eb77c72007-03-15 03:26:42 +00003195 $$.V = getVal(Ty, $2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003196 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003197 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003198 }
Reid Spencer950bf602007-01-26 08:19:09 +00003199 ;
3200
3201BasicBlockList
3202 : BasicBlockList BasicBlock {
3203 $$ = $1;
3204 }
3205 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
3206 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00003207 };
3208
3209
3210// Basic blocks are terminated by branching instructions:
3211// br, br/cc, switch, ret
3212//
Reid Spencer950bf602007-01-26 08:19:09 +00003213BasicBlock
3214 : InstructionList OptAssign BBTerminatorInst {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003215 ValueInfo VI; VI.V = $3.TI; VI.S.copy($3.S);
3216 setValueName(VI, $2);
3217 InsertValue($3.TI);
3218 $1->getInstList().push_back($3.TI);
Reid Spencer950bf602007-01-26 08:19:09 +00003219 InsertValue($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00003220 $$ = $1;
3221 }
Reid Spencer950bf602007-01-26 08:19:09 +00003222 ;
3223
3224InstructionList
3225 : InstructionList Inst {
3226 if ($2.I)
3227 $1->getInstList().push_back($2.I);
3228 $$ = $1;
3229 }
3230 | /* empty */ {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003231 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++),true);
Reid Spencer950bf602007-01-26 08:19:09 +00003232 // Make sure to move the basic block to the correct location in the
3233 // function, instead of leaving it inserted wherever it was first
3234 // referenced.
3235 Function::BasicBlockListType &BBL =
3236 CurFun.CurrentFunction->getBasicBlockList();
3237 BBL.splice(BBL.end(), BBL, $$);
3238 }
3239 | LABELSTR {
Reid Spencer5eb77c72007-03-15 03:26:42 +00003240 $$ = CurBB = getBBVal(ValID::create($1), true);
Reid Spencer950bf602007-01-26 08:19:09 +00003241 // Make sure to move the basic block to the correct location in the
3242 // function, instead of leaving it inserted wherever it was first
3243 // referenced.
3244 Function::BasicBlockListType &BBL =
3245 CurFun.CurrentFunction->getBasicBlockList();
3246 BBL.splice(BBL.end(), BBL, $$);
3247 }
3248 ;
3249
3250Unwind : UNWIND | EXCEPT;
3251
3252BBTerminatorInst
3253 : RET ResolvedVal { // Return with a result...
Reid Spencerbb1fd572007-03-21 17:15:50 +00003254 $$.TI = new ReturnInst($2.V);
3255 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003256 }
3257 | RET VOID { // Return with no result...
Reid Spencerbb1fd572007-03-21 17:15:50 +00003258 $$.TI = new ReturnInst();
3259 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003260 }
3261 | BR LABEL ValueRef { // Unconditional Branch...
3262 BasicBlock* tmpBB = getBBVal($3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003263 $$.TI = new BranchInst(tmpBB);
3264 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003265 } // Conditional Branch...
3266 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003267 $6.S.makeSignless();
3268 $9.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003269 BasicBlock* tmpBBA = getBBVal($6);
3270 BasicBlock* tmpBBB = getBBVal($9);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003271 $3.S.makeUnsigned();
Reid Spencer950bf602007-01-26 08:19:09 +00003272 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003273 $$.TI = new BranchInst(tmpBBA, tmpBBB, tmpVal);
3274 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003275 }
3276 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003277 $3.S.copy($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003278 Value* tmpVal = getVal($2.T, $3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003279 $6.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003280 BasicBlock* tmpBB = getBBVal($6);
3281 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Reid Spencerbb1fd572007-03-21 17:15:50 +00003282 $$.TI = S;
3283 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003284 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
3285 E = $8->end();
3286 for (; I != E; ++I) {
3287 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
3288 S->addCase(CI, I->second);
3289 else
3290 error("Switch case is constant, but not a simple integer");
3291 }
3292 delete $8;
3293 }
3294 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003295 $3.S.copy($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003296 Value* tmpVal = getVal($2.T, $3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003297 $6.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003298 BasicBlock* tmpBB = getBBVal($6);
3299 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003300 $$.TI = S;
3301 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003302 }
3303 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
3304 TO LABEL ValueRef Unwind LABEL ValueRef {
3305 const PointerType *PFTy;
3306 const FunctionType *Ty;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003307 Signedness FTySign;
Reid Spencer950bf602007-01-26 08:19:09 +00003308
Reid Spencered96d1e2007-02-08 09:08:52 +00003309 if (!(PFTy = dyn_cast<PointerType>($3.PAT->get())) ||
Reid Spencer950bf602007-01-26 08:19:09 +00003310 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3311 // Pull out the types of all of the arguments...
3312 std::vector<const Type*> ParamTypes;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003313 FTySign.makeComposite($3.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003314 if ($6) {
3315 for (std::vector<ValueInfo>::iterator I = $6->begin(), E = $6->end();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003316 I != E; ++I) {
Reid Spencer950bf602007-01-26 08:19:09 +00003317 ParamTypes.push_back((*I).V->getType());
Reid Spencerbb1fd572007-03-21 17:15:50 +00003318 FTySign.add(I->S);
3319 }
Reid Spencer950bf602007-01-26 08:19:09 +00003320 }
Reid Spencer7b5d4662007-04-09 06:16:21 +00003321 ParamAttrsList *ParamAttrs = 0;
Reid Spencerb7046c72007-01-29 05:41:34 +00003322 if ($2 == OldCallingConv::CSRet) {
Reid Spencer7b5d4662007-04-09 06:16:21 +00003323 ParamAttrs = new ParamAttrsList();
Reid Spencer18da0722007-04-11 02:44:20 +00003324 ParamAttrs->addAttributes(0, ParamAttr::None); // Function result
3325 ParamAttrs->addAttributes(1, ParamAttr::StructRet); // first param
Reid Spencerb7046c72007-01-29 05:41:34 +00003326 }
Reid Spencer950bf602007-01-26 08:19:09 +00003327 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
3328 if (isVarArg) ParamTypes.pop_back();
Reid Spencered96d1e2007-02-08 09:08:52 +00003329 Ty = FunctionType::get($3.PAT->get(), ParamTypes, isVarArg, ParamAttrs);
Reid Spencer950bf602007-01-26 08:19:09 +00003330 PFTy = PointerType::get(Ty);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003331 $$.S.copy($3.S);
3332 } else {
3333 FTySign = $3.S;
Reid Spencera3b12dd2007-04-07 16:14:01 +00003334 // Get the signedness of the result type. $3 is the pointer to the
3335 // function type so we get the 0th element to extract the function type,
3336 // and then the 0th element again to get the result type.
3337 $$.S.copy($3.S.get(0).get(0));
Reid Spencer950bf602007-01-26 08:19:09 +00003338 }
Reid Spencer7b5d4662007-04-09 06:16:21 +00003339
Reid Spencerbb1fd572007-03-21 17:15:50 +00003340 $4.S.makeComposite(FTySign);
Reid Spencer950bf602007-01-26 08:19:09 +00003341 Value *V = getVal(PFTy, $4); // Get the function we're calling...
3342 BasicBlock *Normal = getBBVal($10);
3343 BasicBlock *Except = getBBVal($13);
3344
3345 // Create the call node...
3346 if (!$6) { // Has no arguments?
Reid Spencerbb1fd572007-03-21 17:15:50 +00003347 $$.TI = new InvokeInst(V, Normal, Except, 0, 0);
Reid Spencer950bf602007-01-26 08:19:09 +00003348 } else { // Has arguments?
3349 // Loop through FunctionType's arguments and ensure they are specified
3350 // correctly!
3351 //
3352 FunctionType::param_iterator I = Ty->param_begin();
3353 FunctionType::param_iterator E = Ty->param_end();
3354 std::vector<ValueInfo>::iterator ArgI = $6->begin(), ArgE = $6->end();
3355
3356 std::vector<Value*> Args;
3357 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
3358 if ((*ArgI).V->getType() != *I)
3359 error("Parameter " +(*ArgI).V->getName()+ " is not of type '" +
3360 (*I)->getDescription() + "'");
3361 Args.push_back((*ArgI).V);
3362 }
3363
3364 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
3365 error("Invalid number of parameters detected");
3366
Reid Spencerbb1fd572007-03-21 17:15:50 +00003367 $$.TI = new InvokeInst(V, Normal, Except, &Args[0], Args.size());
Reid Spencer950bf602007-01-26 08:19:09 +00003368 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00003369 cast<InvokeInst>($$.TI)->setCallingConv(upgradeCallingConv($2));
Reid Spencered96d1e2007-02-08 09:08:52 +00003370 delete $3.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003371 delete $6;
3372 }
3373 | Unwind {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003374 $$.TI = new UnwindInst();
3375 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003376 }
3377 | UNREACHABLE {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003378 $$.TI = new UnreachableInst();
3379 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003380 }
3381 ;
3382
3383JumpTable
3384 : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
3385 $$ = $1;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003386 $3.S.copy($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003387 Constant *V = cast<Constant>(getExistingValue($2.T, $3));
3388
3389 if (V == 0)
3390 error("May only switch on a constant pool value");
3391
Reid Spencerbb1fd572007-03-21 17:15:50 +00003392 $6.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003393 BasicBlock* tmpBB = getBBVal($6);
3394 $$->push_back(std::make_pair(V, tmpBB));
3395 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003396 | IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencer950bf602007-01-26 08:19:09 +00003397 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003398 $2.S.copy($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003399 Constant *V = cast<Constant>(getExistingValue($1.T, $2));
3400
3401 if (V == 0)
3402 error("May only switch on a constant pool value");
3403
Reid Spencerbb1fd572007-03-21 17:15:50 +00003404 $5.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003405 BasicBlock* tmpBB = getBBVal($5);
3406 $$->push_back(std::make_pair(V, tmpBB));
3407 }
3408 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003409
3410Inst
3411 : OptAssign InstVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003412 bool omit = false;
3413 if ($1)
3414 if (BitCastInst *BCI = dyn_cast<BitCastInst>($2.I))
3415 if (BCI->getSrcTy() == BCI->getDestTy() &&
3416 BCI->getOperand(0)->getName() == $1)
3417 // This is a useless bit cast causing a name redefinition. It is
3418 // a bit cast from a type to the same type of an operand with the
3419 // same name as the name we would give this instruction. Since this
3420 // instruction results in no code generation, it is safe to omit
3421 // the instruction. This situation can occur because of collapsed
3422 // type planes. For example:
3423 // %X = add int %Y, %Z
3424 // %X = cast int %Y to uint
3425 // After upgrade, this looks like:
3426 // %X = add i32 %Y, %Z
3427 // %X = bitcast i32 to i32
3428 // The bitcast is clearly useless so we omit it.
3429 omit = true;
3430 if (omit) {
3431 $$.I = 0;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003432 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003433 } else {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003434 ValueInfo VI; VI.V = $2.I; VI.S.copy($2.S);
3435 setValueName(VI, $1);
Reid Spencer950bf602007-01-26 08:19:09 +00003436 InsertValue($2.I);
3437 $$ = $2;
Reid Spencerf5626a32007-01-01 01:20:41 +00003438 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003439 };
3440
Reid Spencer950bf602007-01-26 08:19:09 +00003441PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
3442 $$.P = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003443 $$.S.copy($1.S);
3444 $3.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003445 Value* tmpVal = getVal($1.PAT->get(), $3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003446 $5.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003447 BasicBlock* tmpBB = getBBVal($5);
3448 $$.P->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencered96d1e2007-02-08 09:08:52 +00003449 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003450 }
3451 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
Reid Spencere7c3c602006-11-30 06:36:44 +00003452 $$ = $1;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003453 $4.S.copy($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003454 Value* tmpVal = getVal($1.P->front().first->getType(), $4);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003455 $6.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003456 BasicBlock* tmpBB = getBBVal($6);
3457 $1.P->push_back(std::make_pair(tmpVal, tmpBB));
3458 }
3459 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003460
Reid Spencer950bf602007-01-26 08:19:09 +00003461ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
3462 $$ = new std::vector<ValueInfo>();
Reid Spencerf8483652006-12-02 15:16:01 +00003463 $$->push_back($1);
3464 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003465 | ValueRefList ',' ResolvedVal {
Reid Spencere7c3c602006-11-30 06:36:44 +00003466 $$ = $1;
Reid Spencer950bf602007-01-26 08:19:09 +00003467 $1->push_back($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00003468 };
3469
3470// ValueRefListE - Just like ValueRefList, except that it may also be empty!
3471ValueRefListE
Reid Spencer950bf602007-01-26 08:19:09 +00003472 : ValueRefList
3473 | /*empty*/ { $$ = 0; }
Reid Spencere7c3c602006-11-30 06:36:44 +00003474 ;
3475
3476OptTailCall
3477 : TAIL CALL {
Reid Spencer950bf602007-01-26 08:19:09 +00003478 $$ = true;
Reid Spencere7c3c602006-11-30 06:36:44 +00003479 }
Reid Spencer950bf602007-01-26 08:19:09 +00003480 | CALL {
3481 $$ = false;
3482 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003483 ;
3484
Reid Spencer950bf602007-01-26 08:19:09 +00003485InstVal
3486 : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003487 $3.S.copy($2.S);
3488 $5.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003489 const Type* Ty = $2.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003490 if (!Ty->isInteger() && !Ty->isFloatingPoint() && !isa<VectorType>(Ty))
Reid Spencer950bf602007-01-26 08:19:09 +00003491 error("Arithmetic operator requires integer, FP, or packed operands");
Reid Spencer9d6565a2007-02-15 02:26:10 +00003492 if (isa<VectorType>(Ty) &&
Reid Spencer950bf602007-01-26 08:19:09 +00003493 ($1 == URemOp || $1 == SRemOp || $1 == FRemOp || $1 == RemOp))
Chris Lattner4227bdb2007-02-19 07:34:02 +00003494 error("Remainder not supported on vector types");
Reid Spencer950bf602007-01-26 08:19:09 +00003495 // Upgrade the opcode from obsolete versions before we do anything with it.
3496 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $2.S);
3497 Value* val1 = getVal(Ty, $3);
3498 Value* val2 = getVal(Ty, $5);
3499 $$.I = BinaryOperator::create(Opcode, val1, val2);
3500 if ($$.I == 0)
3501 error("binary operator returned null");
Reid Spencerbb1fd572007-03-21 17:15:50 +00003502 $$.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003503 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003504 }
3505 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003506 $3.S.copy($2.S);
3507 $5.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003508 const Type *Ty = $2.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003509 if (!Ty->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00003510 if (!isa<VectorType>(Ty) ||
3511 !cast<VectorType>(Ty)->getElementType()->isInteger())
Reid Spencer950bf602007-01-26 08:19:09 +00003512 error("Logical operator requires integral operands");
3513 }
3514 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $2.S);
3515 Value* tmpVal1 = getVal(Ty, $3);
3516 Value* tmpVal2 = getVal(Ty, $5);
3517 $$.I = BinaryOperator::create(Opcode, tmpVal1, tmpVal2);
3518 if ($$.I == 0)
3519 error("binary operator returned null");
Reid Spencerbb1fd572007-03-21 17:15:50 +00003520 $$.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003521 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003522 }
3523 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003524 $3.S.copy($2.S);
3525 $5.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003526 const Type* Ty = $2.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003527 if(isa<VectorType>(Ty))
3528 error("VectorTypes currently not supported in setcc instructions");
Reid Spencer950bf602007-01-26 08:19:09 +00003529 unsigned short pred;
3530 Instruction::OtherOps Opcode = getCompareOp($1, pred, Ty, $2.S);
3531 Value* tmpVal1 = getVal(Ty, $3);
3532 Value* tmpVal2 = getVal(Ty, $5);
3533 $$.I = CmpInst::create(Opcode, pred, tmpVal1, tmpVal2);
3534 if ($$.I == 0)
3535 error("binary operator returned null");
Reid Spencerbb1fd572007-03-21 17:15:50 +00003536 $$.S.makeUnsigned();
Reid Spencered96d1e2007-02-08 09:08:52 +00003537 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003538 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00003539 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003540 $4.S.copy($3.S);
3541 $6.S.copy($3.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003542 const Type *Ty = $3.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003543 if (isa<VectorType>(Ty))
3544 error("VectorTypes currently not supported in icmp instructions");
Reid Spencer950bf602007-01-26 08:19:09 +00003545 else if (!Ty->isInteger() && !isa<PointerType>(Ty))
3546 error("icmp requires integer or pointer typed operands");
3547 Value* tmpVal1 = getVal(Ty, $4);
3548 Value* tmpVal2 = getVal(Ty, $6);
3549 $$.I = new ICmpInst($2, tmpVal1, tmpVal2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003550 $$.S.makeUnsigned();
Reid Spencered96d1e2007-02-08 09:08:52 +00003551 delete $3.PAT;
Reid Spencer57f28f92006-12-03 07:10:26 +00003552 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00003553 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003554 $4.S.copy($3.S);
3555 $6.S.copy($3.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003556 const Type *Ty = $3.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003557 if (isa<VectorType>(Ty))
3558 error("VectorTypes currently not supported in fcmp instructions");
Reid Spencer950bf602007-01-26 08:19:09 +00003559 else if (!Ty->isFloatingPoint())
3560 error("fcmp instruction requires floating point operands");
3561 Value* tmpVal1 = getVal(Ty, $4);
3562 Value* tmpVal2 = getVal(Ty, $6);
3563 $$.I = new FCmpInst($2, tmpVal1, tmpVal2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003564 $$.S.makeUnsigned();
Reid Spencered96d1e2007-02-08 09:08:52 +00003565 delete $3.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003566 }
3567 | NOT ResolvedVal {
3568 warning("Use of obsolete 'not' instruction: Replacing with 'xor");
3569 const Type *Ty = $2.V->getType();
3570 Value *Ones = ConstantInt::getAllOnesValue(Ty);
3571 if (Ones == 0)
3572 error("Expected integral type for not instruction");
3573 $$.I = BinaryOperator::create(Instruction::Xor, $2.V, Ones);
3574 if ($$.I == 0)
3575 error("Could not create a xor instruction");
Reid Spencerbb1fd572007-03-21 17:15:50 +00003576 $$.S.copy($2.S);
Reid Spencer229e9362006-12-02 22:14:11 +00003577 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003578 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003579 if (!$4.V->getType()->isInteger() ||
3580 cast<IntegerType>($4.V->getType())->getBitWidth() != 8)
3581 error("Shift amount must be int8");
Reid Spencer832254e2007-02-02 02:16:23 +00003582 const Type* Ty = $2.V->getType();
3583 if (!Ty->isInteger())
Reid Spencer950bf602007-01-26 08:19:09 +00003584 error("Shift constant expression requires integer operand");
Reid Spencer832254e2007-02-02 02:16:23 +00003585 Value* ShiftAmt = 0;
3586 if (cast<IntegerType>(Ty)->getBitWidth() > Type::Int8Ty->getBitWidth())
3587 if (Constant *C = dyn_cast<Constant>($4.V))
3588 ShiftAmt = ConstantExpr::getZExt(C, Ty);
3589 else
3590 ShiftAmt = new ZExtInst($4.V, Ty, makeNameUnique("shift"), CurBB);
3591 else
3592 ShiftAmt = $4.V;
3593 $$.I = BinaryOperator::create(getBinaryOp($1, Ty, $2.S), $2.V, ShiftAmt);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003594 $$.S.copy($2.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00003595 }
Reid Spencerfcb5df82006-12-01 22:34:43 +00003596 | CastOps ResolvedVal TO Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00003597 const Type *DstTy = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003598 if (!DstTy->isFirstClassType())
3599 error("cast instruction to a non-primitive type: '" +
3600 DstTy->getDescription() + "'");
3601 $$.I = cast<Instruction>(getCast($1, $2.V, $2.S, DstTy, $4.S, true));
Reid Spencerbb1fd572007-03-21 17:15:50 +00003602 $$.S.copy($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003603 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003604 }
3605 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003606 if (!$2.V->getType()->isInteger() ||
3607 cast<IntegerType>($2.V->getType())->getBitWidth() != 1)
3608 error("select condition must be bool");
3609 if ($4.V->getType() != $6.V->getType())
3610 error("select value types should match");
3611 $$.I = new SelectInst($2.V, $4.V, $6.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003612 $$.S.copy($4.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00003613 }
3614 | VAARG ResolvedVal ',' Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00003615 const Type *Ty = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003616 NewVarArgs = true;
3617 $$.I = new VAArgInst($2.V, Ty);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003618 $$.S.copy($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003619 delete $4.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003620 }
3621 | VAARG_old ResolvedVal ',' Types {
3622 const Type* ArgTy = $2.V->getType();
Reid Spencered96d1e2007-02-08 09:08:52 +00003623 const Type* DstTy = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003624 ObsoleteVarArgs = true;
3625 Function* NF = cast<Function>(CurModule.CurrentModule->
3626 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0));
3627
3628 //b = vaarg a, t ->
3629 //foo = alloca 1 of t
3630 //bar = vacopy a
3631 //store bar -> foo
3632 //b = vaarg foo, t
3633 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix");
3634 CurBB->getInstList().push_back(foo);
3635 CallInst* bar = new CallInst(NF, $2.V);
3636 CurBB->getInstList().push_back(bar);
3637 CurBB->getInstList().push_back(new StoreInst(bar, foo));
3638 $$.I = new VAArgInst(foo, DstTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003639 $$.S.copy($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003640 delete $4.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003641 }
3642 | VANEXT_old ResolvedVal ',' Types {
3643 const Type* ArgTy = $2.V->getType();
Reid Spencered96d1e2007-02-08 09:08:52 +00003644 const Type* DstTy = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003645 ObsoleteVarArgs = true;
3646 Function* NF = cast<Function>(CurModule.CurrentModule->
3647 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0));
3648
3649 //b = vanext a, t ->
3650 //foo = alloca 1 of t
3651 //bar = vacopy a
3652 //store bar -> foo
3653 //tmp = vaarg foo, t
3654 //b = load foo
3655 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix");
3656 CurBB->getInstList().push_back(foo);
3657 CallInst* bar = new CallInst(NF, $2.V);
3658 CurBB->getInstList().push_back(bar);
3659 CurBB->getInstList().push_back(new StoreInst(bar, foo));
3660 Instruction* tmp = new VAArgInst(foo, DstTy);
3661 CurBB->getInstList().push_back(tmp);
3662 $$.I = new LoadInst(foo);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003663 $$.S.copy($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003664 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003665 }
3666 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003667 if (!ExtractElementInst::isValidOperands($2.V, $4.V))
3668 error("Invalid extractelement operands");
3669 $$.I = new ExtractElementInst($2.V, $4.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003670 $$.S.copy($2.S.get(0));
Reid Spencere7c3c602006-11-30 06:36:44 +00003671 }
3672 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003673 if (!InsertElementInst::isValidOperands($2.V, $4.V, $6.V))
3674 error("Invalid insertelement operands");
3675 $$.I = new InsertElementInst($2.V, $4.V, $6.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003676 $$.S.copy($2.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00003677 }
3678 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003679 if (!ShuffleVectorInst::isValidOperands($2.V, $4.V, $6.V))
3680 error("Invalid shufflevector operands");
3681 $$.I = new ShuffleVectorInst($2.V, $4.V, $6.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003682 $$.S.copy($2.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00003683 }
3684 | PHI_TOK PHIList {
Reid Spencer950bf602007-01-26 08:19:09 +00003685 const Type *Ty = $2.P->front().first->getType();
3686 if (!Ty->isFirstClassType())
3687 error("PHI node operands must be of first class type");
3688 PHINode *PHI = new PHINode(Ty);
3689 PHI->reserveOperandSpace($2.P->size());
3690 while ($2.P->begin() != $2.P->end()) {
3691 if ($2.P->front().first->getType() != Ty)
3692 error("All elements of a PHI node must be of the same type");
3693 PHI->addIncoming($2.P->front().first, $2.P->front().second);
3694 $2.P->pop_front();
3695 }
3696 $$.I = PHI;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003697 $$.S.copy($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003698 delete $2.P; // Free the list...
Reid Spencere7c3c602006-11-30 06:36:44 +00003699 }
Reid Spencer7b5d4662007-04-09 06:16:21 +00003700 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00003701 // Handle the short call syntax
3702 const PointerType *PFTy;
3703 const FunctionType *FTy;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003704 Signedness FTySign;
Reid Spencered96d1e2007-02-08 09:08:52 +00003705 if (!(PFTy = dyn_cast<PointerType>($3.PAT->get())) ||
Reid Spencer950bf602007-01-26 08:19:09 +00003706 !(FTy = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3707 // Pull out the types of all of the arguments...
3708 std::vector<const Type*> ParamTypes;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003709 FTySign.makeComposite($3.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003710 if ($6) {
3711 for (std::vector<ValueInfo>::iterator I = $6->begin(), E = $6->end();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003712 I != E; ++I) {
Reid Spencer950bf602007-01-26 08:19:09 +00003713 ParamTypes.push_back((*I).V->getType());
Reid Spencerbb1fd572007-03-21 17:15:50 +00003714 FTySign.add(I->S);
3715 }
Reid Spencerc4d96252007-01-13 00:03:30 +00003716 }
Reid Spencer950bf602007-01-26 08:19:09 +00003717
3718 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
3719 if (isVarArg) ParamTypes.pop_back();
3720
Reid Spencered96d1e2007-02-08 09:08:52 +00003721 const Type *RetTy = $3.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003722 if (!RetTy->isFirstClassType() && RetTy != Type::VoidTy)
3723 error("Functions cannot return aggregate types");
3724
Reid Spencer7b5d4662007-04-09 06:16:21 +00003725 // Deal with CSRetCC
3726 ParamAttrsList *ParamAttrs = 0;
3727 if ($2 == OldCallingConv::CSRet) {
3728 ParamAttrs = new ParamAttrsList();
Reid Spencer18da0722007-04-11 02:44:20 +00003729 ParamAttrs->addAttributes(0, ParamAttr::None); // function result
3730 ParamAttrs->addAttributes(1, ParamAttr::StructRet); // first parameter
Reid Spencer7b5d4662007-04-09 06:16:21 +00003731 }
3732
Reid Spencerb7046c72007-01-29 05:41:34 +00003733 FTy = FunctionType::get(RetTy, ParamTypes, isVarArg, ParamAttrs);
Reid Spencer950bf602007-01-26 08:19:09 +00003734 PFTy = PointerType::get(FTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003735 $$.S.copy($3.S);
3736 } else {
3737 FTySign = $3.S;
Reid Spencera3b12dd2007-04-07 16:14:01 +00003738 // Get the signedness of the result type. $3 is the pointer to the
3739 // function type so we get the 0th element to extract the function type,
3740 // and then the 0th element again to get the result type.
3741 $$.S.copy($3.S.get(0).get(0));
Reid Spencerf8483652006-12-02 15:16:01 +00003742 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00003743 $4.S.makeComposite(FTySign);
Reid Spencer950bf602007-01-26 08:19:09 +00003744
3745 // First upgrade any intrinsic calls.
3746 std::vector<Value*> Args;
3747 if ($6)
3748 for (unsigned i = 0, e = $6->size(); i < e; ++i)
3749 Args.push_back((*$6)[i].V);
Reid Spencer41b213e2007-04-02 01:14:00 +00003750 Instruction *Inst = upgradeIntrinsicCall(FTy->getReturnType(), $4, Args);
Reid Spencer950bf602007-01-26 08:19:09 +00003751
3752 // If we got an upgraded intrinsic
3753 if (Inst) {
3754 $$.I = Inst;
Reid Spencer950bf602007-01-26 08:19:09 +00003755 } else {
3756 // Get the function we're calling
3757 Value *V = getVal(PFTy, $4);
3758
3759 // Check the argument values match
3760 if (!$6) { // Has no arguments?
3761 // Make sure no arguments is a good thing!
3762 if (FTy->getNumParams() != 0)
3763 error("No arguments passed to a function that expects arguments");
3764 } else { // Has arguments?
3765 // Loop through FunctionType's arguments and ensure they are specified
3766 // correctly!
3767 //
3768 FunctionType::param_iterator I = FTy->param_begin();
3769 FunctionType::param_iterator E = FTy->param_end();
3770 std::vector<ValueInfo>::iterator ArgI = $6->begin(), ArgE = $6->end();
3771
3772 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
3773 if ((*ArgI).V->getType() != *I)
3774 error("Parameter " +(*ArgI).V->getName()+ " is not of type '" +
3775 (*I)->getDescription() + "'");
3776
3777 if (I != E || (ArgI != ArgE && !FTy->isVarArg()))
3778 error("Invalid number of parameters detected");
3779 }
3780
3781 // Create the call instruction
Chris Lattnercf3d0612007-02-13 06:04:17 +00003782 CallInst *CI = new CallInst(V, &Args[0], Args.size());
Reid Spencer950bf602007-01-26 08:19:09 +00003783 CI->setTailCall($1);
Reid Spencerb7046c72007-01-29 05:41:34 +00003784 CI->setCallingConv(upgradeCallingConv($2));
Reid Spencer950bf602007-01-26 08:19:09 +00003785 $$.I = CI;
Reid Spencer950bf602007-01-26 08:19:09 +00003786 }
Reid Spencered96d1e2007-02-08 09:08:52 +00003787 delete $3.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003788 delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00003789 }
Reid Spencer950bf602007-01-26 08:19:09 +00003790 | MemoryInst {
3791 $$ = $1;
3792 }
3793 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003794
3795
3796// IndexList - List of indices for GEP based instructions...
3797IndexList
Reid Spencer950bf602007-01-26 08:19:09 +00003798 : ',' ValueRefList { $$ = $2; }
3799 | /* empty */ { $$ = new std::vector<ValueInfo>(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00003800 ;
3801
3802OptVolatile
Reid Spencer950bf602007-01-26 08:19:09 +00003803 : VOLATILE { $$ = true; }
3804 | /* empty */ { $$ = false; }
Reid Spencere7c3c602006-11-30 06:36:44 +00003805 ;
3806
Reid Spencer950bf602007-01-26 08:19:09 +00003807MemoryInst
3808 : MALLOC Types OptCAlign {
Reid Spencered96d1e2007-02-08 09:08:52 +00003809 const Type *Ty = $2.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003810 $$.S.makeComposite($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003811 $$.I = new MallocInst(Ty, 0, $3);
Reid Spencered96d1e2007-02-08 09:08:52 +00003812 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003813 }
3814 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencered96d1e2007-02-08 09:08:52 +00003815 const Type *Ty = $2.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003816 $5.S.makeUnsigned();
3817 $$.S.makeComposite($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003818 $$.I = new MallocInst(Ty, getVal($4.T, $5), $6);
Reid Spencered96d1e2007-02-08 09:08:52 +00003819 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003820 }
3821 | ALLOCA Types OptCAlign {
Reid Spencered96d1e2007-02-08 09:08:52 +00003822 const Type *Ty = $2.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003823 $$.S.makeComposite($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003824 $$.I = new AllocaInst(Ty, 0, $3);
Reid Spencered96d1e2007-02-08 09:08:52 +00003825 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003826 }
3827 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencered96d1e2007-02-08 09:08:52 +00003828 const Type *Ty = $2.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003829 $5.S.makeUnsigned();
3830 $$.S.makeComposite($4.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003831 $$.I = new AllocaInst(Ty, getVal($4.T, $5), $6);
Reid Spencered96d1e2007-02-08 09:08:52 +00003832 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003833 }
3834 | FREE ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003835 const Type *PTy = $2.V->getType();
3836 if (!isa<PointerType>(PTy))
3837 error("Trying to free nonpointer type '" + PTy->getDescription() + "'");
3838 $$.I = new FreeInst($2.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003839 $$.S.makeSignless();
Reid Spencere7c3c602006-11-30 06:36:44 +00003840 }
3841 | OptVolatile LOAD Types ValueRef {
Reid Spencered96d1e2007-02-08 09:08:52 +00003842 const Type* Ty = $3.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003843 $4.S.copy($3.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003844 if (!isa<PointerType>(Ty))
3845 error("Can't load from nonpointer type: " + Ty->getDescription());
3846 if (!cast<PointerType>(Ty)->getElementType()->isFirstClassType())
3847 error("Can't load from pointer of non-first-class type: " +
3848 Ty->getDescription());
3849 Value* tmpVal = getVal(Ty, $4);
3850 $$.I = new LoadInst(tmpVal, "", $1);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003851 $$.S.copy($3.S.get(0));
Reid Spencered96d1e2007-02-08 09:08:52 +00003852 delete $3.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003853 }
3854 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003855 $6.S.copy($5.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003856 const PointerType *PTy = dyn_cast<PointerType>($5.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00003857 if (!PTy)
3858 error("Can't store to a nonpointer type: " +
Reid Spencered96d1e2007-02-08 09:08:52 +00003859 $5.PAT->get()->getDescription());
Reid Spencer950bf602007-01-26 08:19:09 +00003860 const Type *ElTy = PTy->getElementType();
Reid Spencered96d1e2007-02-08 09:08:52 +00003861 Value *StoreVal = $3.V;
Reid Spencer950bf602007-01-26 08:19:09 +00003862 Value* tmpVal = getVal(PTy, $6);
Reid Spencered96d1e2007-02-08 09:08:52 +00003863 if (ElTy != $3.V->getType()) {
3864 StoreVal = handleSRetFuncTypeMerge($3.V, ElTy);
3865 if (!StoreVal)
3866 error("Can't store '" + $3.V->getType()->getDescription() +
3867 "' into space of type '" + ElTy->getDescription() + "'");
3868 else {
3869 PTy = PointerType::get(StoreVal->getType());
3870 if (Constant *C = dyn_cast<Constant>(tmpVal))
3871 tmpVal = ConstantExpr::getBitCast(C, PTy);
3872 else
3873 tmpVal = new BitCastInst(tmpVal, PTy, "upgrd.cast", CurBB);
3874 }
3875 }
3876 $$.I = new StoreInst(StoreVal, tmpVal, $1);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003877 $$.S.makeSignless();
Reid Spencered96d1e2007-02-08 09:08:52 +00003878 delete $5.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003879 }
3880 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003881 $3.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003882 const Type* Ty = $2.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003883 if (!isa<PointerType>(Ty))
3884 error("getelementptr insn requires pointer operand");
3885
3886 std::vector<Value*> VIndices;
Reid Spencerff0e4482007-04-16 00:40:57 +00003887 upgradeGEPInstIndices(Ty, $4, VIndices);
Reid Spencer950bf602007-01-26 08:19:09 +00003888
3889 Value* tmpVal = getVal(Ty, $3);
Chris Lattner1bc3fa62007-02-12 22:58:38 +00003890 $$.I = new GetElementPtrInst(tmpVal, &VIndices[0], VIndices.size());
Reid Spencerbb1fd572007-03-21 17:15:50 +00003891 ValueInfo VI; VI.V = tmpVal; VI.S.copy($2.S);
3892 $$.S.copy(getElementSign(VI, VIndices));
Reid Spencered96d1e2007-02-08 09:08:52 +00003893 delete $2.PAT;
Reid Spencer30d0c582007-01-15 00:26:18 +00003894 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00003895 };
3896
Reid Spencer950bf602007-01-26 08:19:09 +00003897
Reid Spencere7c3c602006-11-30 06:36:44 +00003898%%
3899
3900int yyerror(const char *ErrorMsg) {
3901 std::string where
3902 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Reid Spencered96d1e2007-02-08 09:08:52 +00003903 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
Reid Spencer950bf602007-01-26 08:19:09 +00003904 std::string errMsg = where + "error: " + std::string(ErrorMsg);
3905 if (yychar != YYEMPTY && yychar != 0)
3906 errMsg += " while reading token '" + std::string(Upgradetext, Upgradeleng) +
3907 "'.";
Reid Spencer71d2ec92006-12-31 06:02:26 +00003908 std::cerr << "llvm-upgrade: " << errMsg << '\n';
Reid Spencer950bf602007-01-26 08:19:09 +00003909 std::cout << "llvm-upgrade: parse failed.\n";
Reid Spencere7c3c602006-11-30 06:36:44 +00003910 exit(1);
3911}
Reid Spencer319a7302007-01-05 17:20:02 +00003912
Reid Spencer30d0c582007-01-15 00:26:18 +00003913void warning(const std::string& ErrorMsg) {
Reid Spencer319a7302007-01-05 17:20:02 +00003914 std::string where
3915 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Reid Spencered96d1e2007-02-08 09:08:52 +00003916 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
Reid Spencer950bf602007-01-26 08:19:09 +00003917 std::string errMsg = where + "warning: " + std::string(ErrorMsg);
3918 if (yychar != YYEMPTY && yychar != 0)
3919 errMsg += " while reading token '" + std::string(Upgradetext, Upgradeleng) +
3920 "'.";
Reid Spencer319a7302007-01-05 17:20:02 +00003921 std::cerr << "llvm-upgrade: " << errMsg << '\n';
3922}
Reid Spencer950bf602007-01-26 08:19:09 +00003923
3924void error(const std::string& ErrorMsg, int LineNo) {
3925 if (LineNo == -1) LineNo = Upgradelineno;
3926 Upgradelineno = LineNo;
3927 yyerror(ErrorMsg.c_str());
3928}
3929