blob: bc6342c94deca9aef47ce1000c58f2747f11357b [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 Spencer7eea8ff2007-05-18 05:48:07 +000065static unsigned lastCallingConv;
Reid Spencera50d5962006-12-02 04:11:07 +000066
Reid Spencer950bf602007-01-26 08:19:09 +000067// This contains info used when building the body of a function. It is
68// destroyed when the function is completed.
69//
70typedef std::vector<Value *> ValueList; // Numbered defs
71
Reid Spencerbb1fd572007-03-21 17:15:50 +000072typedef std::pair<std::string,TypeInfo> RenameMapKey;
Reid Spencer950bf602007-01-26 08:19:09 +000073typedef std::map<RenameMapKey,std::string> RenameMapType;
74
75static void
76ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
77 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
78
79static struct PerModuleInfo {
80 Module *CurrentModule;
81 std::map<const Type *, ValueList> Values; // Module level numbered definitions
82 std::map<const Type *,ValueList> LateResolveValues;
Reid Spencerbb1fd572007-03-21 17:15:50 +000083 std::vector<PATypeHolder> Types;
84 std::vector<Signedness> TypeSigns;
85 std::map<std::string,Signedness> NamedTypeSigns;
86 std::map<std::string,Signedness> NamedValueSigns;
Reid Spencer950bf602007-01-26 08:19:09 +000087 std::map<ValID, PATypeHolder> LateResolveTypes;
88 static Module::Endianness Endian;
89 static Module::PointerSize PointerSize;
90 RenameMapType RenameMap;
91
92 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
93 /// how they were referenced and on which line of the input they came from so
94 /// that we can resolve them later and print error messages as appropriate.
95 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
96
97 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
98 // references to global values. Global values may be referenced before they
99 // are defined, and if so, the temporary object that they represent is held
100 // here. This is used for forward references of GlobalValues.
101 //
102 typedef std::map<std::pair<const PointerType *, ValID>, GlobalValue*>
103 GlobalRefsType;
104 GlobalRefsType GlobalRefs;
105
106 void ModuleDone() {
107 // If we could not resolve some functions at function compilation time
108 // (calls to functions before they are defined), resolve them now... Types
109 // are resolved when the constant pool has been completely parsed.
110 //
111 ResolveDefinitions(LateResolveValues);
112
113 // Check to make sure that all global value forward references have been
114 // resolved!
115 //
116 if (!GlobalRefs.empty()) {
117 std::string UndefinedReferences = "Unresolved global references exist:\n";
118
119 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
120 I != E; ++I) {
121 UndefinedReferences += " " + I->first.first->getDescription() + " " +
122 I->first.second.getName() + "\n";
123 }
124 error(UndefinedReferences);
125 return;
126 }
127
128 if (CurrentModule->getDataLayout().empty()) {
129 std::string dataLayout;
130 if (Endian != Module::AnyEndianness)
131 dataLayout.append(Endian == Module::BigEndian ? "E" : "e");
132 if (PointerSize != Module::AnyPointerSize) {
133 if (!dataLayout.empty())
134 dataLayout += "-";
135 dataLayout.append(PointerSize == Module::Pointer64 ?
136 "p:64:64" : "p:32:32");
137 }
138 CurrentModule->setDataLayout(dataLayout);
139 }
140
141 Values.clear(); // Clear out function local definitions
142 Types.clear();
Reid Spencerbb1fd572007-03-21 17:15:50 +0000143 TypeSigns.clear();
144 NamedTypeSigns.clear();
145 NamedValueSigns.clear();
Reid Spencer950bf602007-01-26 08:19:09 +0000146 CurrentModule = 0;
147 }
148
149 // GetForwardRefForGlobal - Check to see if there is a forward reference
150 // for this global. If so, remove it from the GlobalRefs map and return it.
151 // If not, just return null.
152 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
153 // Check to see if there is a forward reference to this global variable...
154 // if there is, eliminate it and patch the reference to use the new def'n.
155 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
156 GlobalValue *Ret = 0;
157 if (I != GlobalRefs.end()) {
158 Ret = I->second;
159 GlobalRefs.erase(I);
160 }
161 return Ret;
162 }
163 void setEndianness(Module::Endianness E) { Endian = E; }
164 void setPointerSize(Module::PointerSize sz) { PointerSize = sz; }
165} CurModule;
166
167Module::Endianness PerModuleInfo::Endian = Module::AnyEndianness;
168Module::PointerSize PerModuleInfo::PointerSize = Module::AnyPointerSize;
169
170static struct PerFunctionInfo {
171 Function *CurrentFunction; // Pointer to current function being created
172
173 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
174 std::map<const Type*, ValueList> LateResolveValues;
175 bool isDeclare; // Is this function a forward declararation?
176 GlobalValue::LinkageTypes Linkage;// Linkage for forward declaration.
177
178 /// BBForwardRefs - When we see forward references to basic blocks, keep
179 /// track of them here.
180 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
181 std::vector<BasicBlock*> NumberedBlocks;
182 RenameMapType RenameMap;
Reid Spencer950bf602007-01-26 08:19:09 +0000183 unsigned NextBBNum;
184
185 inline PerFunctionInfo() {
186 CurrentFunction = 0;
187 isDeclare = false;
188 Linkage = GlobalValue::ExternalLinkage;
189 }
190
191 inline void FunctionStart(Function *M) {
192 CurrentFunction = M;
193 NextBBNum = 0;
194 }
195
196 void FunctionDone() {
197 NumberedBlocks.clear();
198
199 // Any forward referenced blocks left?
200 if (!BBForwardRefs.empty()) {
201 error("Undefined reference to label " +
202 BBForwardRefs.begin()->first->getName());
203 return;
204 }
205
206 // Resolve all forward references now.
207 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
208
209 Values.clear(); // Clear out function local definitions
210 RenameMap.clear();
Reid Spencer950bf602007-01-26 08:19:09 +0000211 CurrentFunction = 0;
212 isDeclare = false;
213 Linkage = GlobalValue::ExternalLinkage;
214 }
215} CurFun; // Info for the current function...
216
217static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
218
Reid Spencerbb1fd572007-03-21 17:15:50 +0000219/// This function is just a utility to make a Key value for the rename map.
220/// The Key is a combination of the name, type, Signedness of the original
221/// value (global/function). This just constructs the key and ensures that
222/// named Signedness values are resolved to the actual Signedness.
223/// @brief Make a key for the RenameMaps
224static RenameMapKey makeRenameMapKey(const std::string &Name, const Type* Ty,
225 const Signedness &Sign) {
226 TypeInfo TI;
227 TI.T = Ty;
228 if (Sign.isNamed())
229 // Don't allow Named Signedness nodes because they won't match. The actual
230 // Signedness must be looked up in the NamedTypeSigns map.
231 TI.S.copy(CurModule.NamedTypeSigns[Sign.getName()]);
232 else
233 TI.S.copy(Sign);
234 return std::make_pair(Name, TI);
235}
236
Reid Spencer950bf602007-01-26 08:19:09 +0000237
238//===----------------------------------------------------------------------===//
239// Code to handle definitions of all the types
240//===----------------------------------------------------------------------===//
241
242static int InsertValue(Value *V,
243 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
244 if (V->hasName()) return -1; // Is this a numbered definition?
245
246 // Yes, insert the value into the value table...
247 ValueList &List = ValueTab[V->getType()];
248 List.push_back(V);
249 return List.size()-1;
250}
251
Reid Spencerd7c4f8c2007-01-26 19:59:25 +0000252static const Type *getType(const ValID &D, bool DoNotImprovise = false) {
Reid Spencer950bf602007-01-26 08:19:09 +0000253 switch (D.Type) {
254 case ValID::NumberVal: // Is it a numbered definition?
255 // Module constants occupy the lowest numbered slots...
256 if ((unsigned)D.Num < CurModule.Types.size()) {
257 return CurModule.Types[(unsigned)D.Num];
258 }
259 break;
260 case ValID::NameVal: // Is it a named definition?
261 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
Reid Spencer950bf602007-01-26 08:19:09 +0000262 return N;
263 }
264 break;
265 default:
266 error("Internal parser error: Invalid symbol type reference");
267 return 0;
268 }
269
270 // If we reached here, we referenced either a symbol that we don't know about
271 // or an id number that hasn't been read yet. We may be referencing something
272 // forward, so just create an entry to be resolved later and get to it...
273 //
274 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
275
Reid Spencer950bf602007-01-26 08:19:09 +0000276 if (inFunctionScope()) {
277 if (D.Type == ValID::NameVal) {
278 error("Reference to an undefined type: '" + D.getName() + "'");
279 return 0;
280 } else {
281 error("Reference to an undefined type: #" + itostr(D.Num));
282 return 0;
283 }
284 }
285
286 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
287 if (I != CurModule.LateResolveTypes.end())
288 return I->second;
289
290 Type *Typ = OpaqueType::get();
291 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
292 return Typ;
Reid Spencerbb1fd572007-03-21 17:15:50 +0000293}
294
295/// This is like the getType method except that instead of looking up the type
296/// for a given ID, it looks up that type's sign.
297/// @brief Get the signedness of a referenced type
298static Signedness getTypeSign(const ValID &D) {
299 switch (D.Type) {
300 case ValID::NumberVal: // Is it a numbered definition?
301 // Module constants occupy the lowest numbered slots...
302 if ((unsigned)D.Num < CurModule.TypeSigns.size()) {
303 return CurModule.TypeSigns[(unsigned)D.Num];
304 }
305 break;
306 case ValID::NameVal: { // Is it a named definition?
307 std::map<std::string,Signedness>::const_iterator I =
308 CurModule.NamedTypeSigns.find(D.Name);
309 if (I != CurModule.NamedTypeSigns.end())
310 return I->second;
311 // Perhaps its a named forward .. just cache the name
312 Signedness S;
313 S.makeNamed(D.Name);
314 return S;
315 }
316 default:
317 break;
318 }
319 // If we don't find it, its signless
320 Signedness S;
321 S.makeSignless();
322 return S;
323}
324
325/// This function is analagous to getElementType in LLVM. It provides the same
326/// function except that it looks up the Signedness instead of the type. This is
327/// used when processing GEP instructions that need to extract the type of an
328/// indexed struct/array/ptr member.
329/// @brief Look up an element's sign.
330static Signedness getElementSign(const ValueInfo& VI,
331 const std::vector<Value*> &Indices) {
332 const Type *Ptr = VI.V->getType();
333 assert(isa<PointerType>(Ptr) && "Need pointer type");
334
335 unsigned CurIdx = 0;
336 Signedness S(VI.S);
337 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
338 if (CurIdx == Indices.size())
339 break;
340
341 Value *Index = Indices[CurIdx++];
342 assert(!isa<PointerType>(CT) || CurIdx == 1 && "Invalid type");
343 Ptr = CT->getTypeAtIndex(Index);
344 if (const Type* Ty = Ptr->getForwardedType())
345 Ptr = Ty;
346 assert(S.isComposite() && "Bad Signedness type");
347 if (isa<StructType>(CT)) {
348 S = S.get(cast<ConstantInt>(Index)->getZExtValue());
349 } else {
350 S = S.get(0UL);
351 }
352 if (S.isNamed())
353 S = CurModule.NamedTypeSigns[S.getName()];
354 }
355 Signedness Result;
356 Result.makeComposite(S);
357 return Result;
358}
359
360/// This function just translates a ConstantInfo into a ValueInfo and calls
361/// getElementSign(ValueInfo,...). Its just a convenience.
362/// @brief ConstantInfo version of getElementSign.
363static Signedness getElementSign(const ConstInfo& CI,
364 const std::vector<Constant*> &Indices) {
365 ValueInfo VI;
366 VI.V = CI.C;
367 VI.S.copy(CI.S);
368 std::vector<Value*> Idx;
369 for (unsigned i = 0; i < Indices.size(); ++i)
370 Idx.push_back(Indices[i]);
371 Signedness result = getElementSign(VI, Idx);
372 VI.destroy();
373 return result;
374}
Reid Spencer950bf602007-01-26 08:19:09 +0000375
Reid Spencered96d1e2007-02-08 09:08:52 +0000376/// This function determines if two function types differ only in their use of
377/// the sret parameter attribute in the first argument. If they are identical
378/// in all other respects, it returns true. Otherwise, it returns false.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000379static bool FuncTysDifferOnlyBySRet(const FunctionType *F1,
380 const FunctionType *F2) {
Reid Spencered96d1e2007-02-08 09:08:52 +0000381 if (F1->getReturnType() != F2->getReturnType() ||
Reid Spencer7b5d4662007-04-09 06:16:21 +0000382 F1->getNumParams() != F2->getNumParams())
Reid Spencered96d1e2007-02-08 09:08:52 +0000383 return false;
Reid Spencer7eea8ff2007-05-18 05:48:07 +0000384 const ParamAttrsList *PAL1 = F1->getParamAttrs();
385 const ParamAttrsList *PAL2 = F2->getParamAttrs();
386 if (PAL1 && !PAL2 || PAL2 && !PAL1)
387 return false;
388 if (PAL1 && PAL2 && ((PAL1->size() != PAL2->size()) ||
389 (PAL1->getParamAttrs(0) != PAL2->getParamAttrs(0))))
Reid Spencer7b5d4662007-04-09 06:16:21 +0000390 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) {
Reid Spencer7eea8ff2007-05-18 05:48:07 +0000393 if (F1->getParamType(i) != F2->getParamType(i) || (PAL1 && PAL2 &&
394 (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?
Dale Johannesen43421b32007-09-06 18:13:44 +0000536 if (!ConstantFP::isValueValidForType(Ty, *D.ConstPoolFP))
Reid Spencer950bf602007-01-26 08:19:09 +0000537 error("FP constant invalid for type");
Dale Johannesen43421b32007-09-06 18:13:44 +0000538 // Lexer has no type info, so builds all FP constants as double.
539 // Fix this here.
540 if (Ty==Type::FloatTy)
541 D.ConstPoolFP->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
542 return ConstantFP::get(Ty, *D.ConstPoolFP);
Reid Spencer950bf602007-01-26 08:19:09 +0000543
544 case ValID::ConstNullVal: // Is it a null value?
545 if (!isa<PointerType>(Ty))
546 error("Cannot create a a non pointer null");
547 return ConstantPointerNull::get(cast<PointerType>(Ty));
548
549 case ValID::ConstUndefVal: // Is it an undef value?
550 return UndefValue::get(Ty);
551
552 case ValID::ConstZeroVal: // Is it a zero value?
553 return Constant::getNullValue(Ty);
554
555 case ValID::ConstantVal: // Fully resolved constant?
556 if (D.ConstantValue->getType() != Ty)
557 error("Constant expression type different from required type");
558 return D.ConstantValue;
559
560 case ValID::InlineAsmVal: { // Inline asm expression
561 const PointerType *PTy = dyn_cast<PointerType>(Ty);
562 const FunctionType *FTy =
563 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
564 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints))
565 error("Invalid type for asm constraint string");
566 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
567 D.IAD->HasSideEffects);
568 D.destroy(); // Free InlineAsmDescriptor.
569 return IA;
570 }
571 default:
572 assert(0 && "Unhandled case");
573 return 0;
574 } // End of switch
575
576 assert(0 && "Unhandled case");
577 return 0;
578}
579
580// getVal - This function is identical to getExistingValue, except that if a
581// value is not already defined, it "improvises" by creating a placeholder var
582// that looks and acts just like the requested variable. When the value is
583// defined later, all uses of the placeholder variable are replaced with the
584// real thing.
585//
586static Value *getVal(const Type *Ty, const ValID &ID) {
587 if (Ty == Type::LabelTy)
588 error("Cannot use a basic block here");
589
590 // See if the value has already been defined.
591 Value *V = getExistingValue(Ty, ID);
592 if (V) return V;
593
594 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty))
595 error("Invalid use of a composite type");
596
597 // If we reached here, we referenced either a symbol that we don't know about
598 // or an id number that hasn't been read yet. We may be referencing something
599 // forward, so just create an entry to be resolved later and get to it...
Reid Spencer950bf602007-01-26 08:19:09 +0000600 V = new Argument(Ty);
601
602 // Remember where this forward reference came from. FIXME, shouldn't we try
603 // to recycle these things??
604 CurModule.PlaceHolderInfo.insert(
Reid Spenceref9b9a72007-02-05 20:47:22 +0000605 std::make_pair(V, std::make_pair(ID, Upgradelineno)));
Reid Spencer950bf602007-01-26 08:19:09 +0000606
607 if (inFunctionScope())
608 InsertValue(V, CurFun.LateResolveValues);
609 else
610 InsertValue(V, CurModule.LateResolveValues);
611 return V;
612}
613
Reid Spencered96d1e2007-02-08 09:08:52 +0000614/// @brief This just makes any name given to it unique, up to MAX_UINT times.
615static std::string makeNameUnique(const std::string& Name) {
616 static unsigned UniqueNameCounter = 1;
617 std::string Result(Name);
618 Result += ".upgrd." + llvm::utostr(UniqueNameCounter++);
619 return Result;
620}
621
Reid Spencer950bf602007-01-26 08:19:09 +0000622/// getBBVal - This is used for two purposes:
623/// * If isDefinition is true, a new basic block with the specified ID is being
624/// defined.
625/// * If isDefinition is true, this is a reference to a basic block, which may
626/// or may not be a forward reference.
627///
628static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
629 assert(inFunctionScope() && "Can't get basic block at global scope");
630
631 std::string Name;
632 BasicBlock *BB = 0;
633 switch (ID.Type) {
634 default:
635 error("Illegal label reference " + ID.getName());
636 break;
637 case ValID::NumberVal: // Is it a numbered definition?
638 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
639 CurFun.NumberedBlocks.resize(ID.Num+1);
640 BB = CurFun.NumberedBlocks[ID.Num];
641 break;
642 case ValID::NameVal: // Is it a named definition?
643 Name = ID.Name;
Reid Spencerbb1fd572007-03-21 17:15:50 +0000644 if (Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name)) {
Reid Spencered96d1e2007-02-08 09:08:52 +0000645 if (N->getType() != Type::LabelTy) {
646 // Register names didn't use to conflict with basic block names
647 // because of type planes. Now they all have to be unique. So, we just
648 // rename the register and treat this name as if no basic block
649 // had been found.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000650 RenameMapKey Key = makeRenameMapKey(ID.Name, N->getType(), ID.S);
Reid Spencered96d1e2007-02-08 09:08:52 +0000651 N->setName(makeNameUnique(N->getName()));
652 CurModule.RenameMap[Key] = N->getName();
653 BB = 0;
654 } else {
655 BB = cast<BasicBlock>(N);
656 }
Reid Spencer950bf602007-01-26 08:19:09 +0000657 }
658 break;
659 }
660
661 // See if the block has already been defined.
662 if (BB) {
663 // If this is the definition of the block, make sure the existing value was
664 // just a forward reference. If it was a forward reference, there will be
665 // an entry for it in the PlaceHolderInfo map.
666 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
667 // The existing value was a definition, not a forward reference.
668 error("Redefinition of label " + ID.getName());
669
670 ID.destroy(); // Free strdup'd memory.
671 return BB;
672 }
673
674 // Otherwise this block has not been seen before.
675 BB = new BasicBlock("", CurFun.CurrentFunction);
676 if (ID.Type == ValID::NameVal) {
677 BB->setName(ID.Name);
678 } else {
679 CurFun.NumberedBlocks[ID.Num] = BB;
680 }
681
682 // If this is not a definition, keep track of it so we can use it as a forward
683 // reference.
684 if (!isDefinition) {
685 // Remember where this forward reference came from.
686 CurFun.BBForwardRefs[BB] = std::make_pair(ID, Upgradelineno);
687 } else {
688 // The forward declaration could have been inserted anywhere in the
689 // function: insert it into the correct place now.
690 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
691 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
692 }
693 ID.destroy();
694 return BB;
695}
696
697
698//===----------------------------------------------------------------------===//
699// Code to handle forward references in instructions
700//===----------------------------------------------------------------------===//
701//
702// This code handles the late binding needed with statements that reference
703// values not defined yet... for example, a forward branch, or the PHI node for
704// a loop body.
705//
706// This keeps a table (CurFun.LateResolveValues) of all such forward references
707// and back patchs after we are done.
708//
709
710// ResolveDefinitions - If we could not resolve some defs at parsing
711// time (forward branches, phi functions for loops, etc...) resolve the
712// defs now...
713//
714static void
715ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
716 std::map<const Type*,ValueList> *FutureLateResolvers) {
Reid Spencered96d1e2007-02-08 09:08:52 +0000717
Reid Spencer950bf602007-01-26 08:19:09 +0000718 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
719 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
720 E = LateResolvers.end(); LRI != E; ++LRI) {
Reid Spencered96d1e2007-02-08 09:08:52 +0000721 const Type* Ty = LRI->first;
Reid Spencer950bf602007-01-26 08:19:09 +0000722 ValueList &List = LRI->second;
723 while (!List.empty()) {
724 Value *V = List.back();
725 List.pop_back();
726
727 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
728 CurModule.PlaceHolderInfo.find(V);
729 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error");
730
731 ValID &DID = PHI->second.first;
732
Reid Spencered96d1e2007-02-08 09:08:52 +0000733 Value *TheRealValue = getExistingValue(Ty, DID);
Reid Spencer950bf602007-01-26 08:19:09 +0000734 if (TheRealValue) {
735 V->replaceAllUsesWith(TheRealValue);
736 delete V;
737 CurModule.PlaceHolderInfo.erase(PHI);
738 } else if (FutureLateResolvers) {
739 // Functions have their unresolved items forwarded to the module late
740 // resolver table
741 InsertValue(V, *FutureLateResolvers);
742 } else {
743 if (DID.Type == ValID::NameVal) {
Reid Spencered96d1e2007-02-08 09:08:52 +0000744 error("Reference to an invalid definition: '" + DID.getName() +
745 "' of type '" + V->getType()->getDescription() + "'",
746 PHI->second.second);
Reid Spencer7de2e012007-01-29 19:08:46 +0000747 return;
Reid Spencer950bf602007-01-26 08:19:09 +0000748 } else {
749 error("Reference to an invalid definition: #" +
750 itostr(DID.Num) + " of type '" +
751 V->getType()->getDescription() + "'", PHI->second.second);
752 return;
753 }
754 }
755 }
756 }
757
758 LateResolvers.clear();
759}
760
Reid Spencerbb1fd572007-03-21 17:15:50 +0000761/// This function is used for type resolution and upref handling. When a type
762/// becomes concrete, this function is called to adjust the signedness for the
763/// concrete type.
764static void ResolveTypeSign(const Type* oldTy, const Signedness &Sign) {
765 std::string TyName = CurModule.CurrentModule->getTypeName(oldTy);
766 if (!TyName.empty())
767 CurModule.NamedTypeSigns[TyName] = Sign;
768}
769
770/// ResolveTypeTo - A brand new type was just declared. This means that (if
771/// name is not null) things referencing Name can be resolved. Otherwise,
772/// things refering to the number can be resolved. Do this now.
773static void ResolveTypeTo(char *Name, const Type *ToTy, const Signedness& Sign){
Reid Spencer950bf602007-01-26 08:19:09 +0000774 ValID D;
Reid Spencerbb1fd572007-03-21 17:15:50 +0000775 if (Name)
776 D = ValID::create(Name);
777 else
778 D = ValID::create((int)CurModule.Types.size());
779 D.S.copy(Sign);
780
Reid Spencerbaba98a2007-04-11 12:10:58 +0000781 if (Name)
782 CurModule.NamedTypeSigns[Name] = Sign;
Reid Spencer950bf602007-01-26 08:19:09 +0000783
784 std::map<ValID, PATypeHolder>::iterator I =
785 CurModule.LateResolveTypes.find(D);
786 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencerbb1fd572007-03-21 17:15:50 +0000787 const Type *OldTy = I->second.get();
788 ((DerivedType*)OldTy)->refineAbstractTypeTo(ToTy);
Reid Spencer950bf602007-01-26 08:19:09 +0000789 CurModule.LateResolveTypes.erase(I);
790 }
791}
792
Anton Korobeynikovce13b852007-01-28 15:25:24 +0000793/// This is the implementation portion of TypeHasInteger. It traverses the
794/// type given, avoiding recursive types, and returns true as soon as it finds
795/// an integer type. If no integer type is found, it returns false.
796static bool TypeHasIntegerI(const Type *Ty, std::vector<const Type*> Stack) {
797 // Handle some easy cases
798 if (Ty->isPrimitiveType() || (Ty->getTypeID() == Type::OpaqueTyID))
799 return false;
800 if (Ty->isInteger())
801 return true;
802 if (const SequentialType *STy = dyn_cast<SequentialType>(Ty))
803 return STy->getElementType()->isInteger();
804
805 // Avoid type structure recursion
806 for (std::vector<const Type*>::iterator I = Stack.begin(), E = Stack.end();
807 I != E; ++I)
808 if (Ty == *I)
809 return false;
810
811 // Push us on the type stack
812 Stack.push_back(Ty);
813
814 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
815 if (TypeHasIntegerI(FTy->getReturnType(), Stack))
816 return true;
817 FunctionType::param_iterator I = FTy->param_begin();
818 FunctionType::param_iterator E = FTy->param_end();
819 for (; I != E; ++I)
820 if (TypeHasIntegerI(*I, Stack))
821 return true;
822 return false;
823 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
824 StructType::element_iterator I = STy->element_begin();
825 StructType::element_iterator E = STy->element_end();
826 for (; I != E; ++I) {
827 if (TypeHasIntegerI(*I, Stack))
828 return true;
829 }
830 return false;
831 }
832 // There shouldn't be anything else, but its definitely not integer
833 assert(0 && "What type is this?");
834 return false;
835}
836
837/// This is the interface to TypeHasIntegerI. It just provides the type stack,
838/// to avoid recursion, and then calls TypeHasIntegerI.
839static inline bool TypeHasInteger(const Type *Ty) {
840 std::vector<const Type*> TyStack;
841 return TypeHasIntegerI(Ty, TyStack);
842}
843
Reid Spencer950bf602007-01-26 08:19:09 +0000844// setValueName - Set the specified value to the name given. The name may be
845// null potentially, in which case this is a noop. The string passed in is
846// assumed to be a malloc'd string buffer, and is free'd by this function.
847//
Reid Spencerbb1fd572007-03-21 17:15:50 +0000848static void setValueName(const ValueInfo &V, char *NameStr) {
Reid Spencer950bf602007-01-26 08:19:09 +0000849 if (NameStr) {
850 std::string Name(NameStr); // Copy string
851 free(NameStr); // Free old string
852
Reid Spencerbb1fd572007-03-21 17:15:50 +0000853 if (V.V->getType() == Type::VoidTy) {
Reid Spencer950bf602007-01-26 08:19:09 +0000854 error("Can't assign name '" + Name + "' to value with void type");
855 return;
856 }
857
Reid Spencer950bf602007-01-26 08:19:09 +0000858 assert(inFunctionScope() && "Must be in function scope");
859
860 // Search the function's symbol table for an existing value of this name
Reid Spenceref9b9a72007-02-05 20:47:22 +0000861 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
862 Value* Existing = ST.lookup(Name);
Reid Spencer950bf602007-01-26 08:19:09 +0000863 if (Existing) {
Anton Korobeynikovce13b852007-01-28 15:25:24 +0000864 // An existing value of the same name was found. This might have happened
865 // because of the integer type planes collapsing in LLVM 2.0.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000866 if (Existing->getType() == V.V->getType() &&
Anton Korobeynikovce13b852007-01-28 15:25:24 +0000867 !TypeHasInteger(Existing->getType())) {
868 // If the type does not contain any integers in them then this can't be
869 // a type plane collapsing issue. It truly is a redefinition and we
870 // should error out as the assembly is invalid.
871 error("Redefinition of value named '" + Name + "' of type '" +
Reid Spencerbb1fd572007-03-21 17:15:50 +0000872 V.V->getType()->getDescription() + "'");
Anton Korobeynikovce13b852007-01-28 15:25:24 +0000873 return;
Reid Spencer950bf602007-01-26 08:19:09 +0000874 }
875 // In LLVM 2.0 we don't allow names to be re-used for any values in a
876 // function, regardless of Type. Previously re-use of names was okay as
877 // long as they were distinct types. With type planes collapsing because
878 // of the signedness change and because of PR411, this can no longer be
879 // supported. We must search the entire symbol table for a conflicting
880 // name and make the name unique. No warning is needed as this can't
881 // cause a problem.
882 std::string NewName = makeNameUnique(Name);
883 // We're changing the name but it will probably be used by other
884 // instructions as operands later on. Consequently we have to retain
885 // a mapping of the renaming that we're doing.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000886 RenameMapKey Key = makeRenameMapKey(Name, V.V->getType(), V.S);
Reid Spencer950bf602007-01-26 08:19:09 +0000887 CurFun.RenameMap[Key] = NewName;
888 Name = NewName;
889 }
890
891 // Set the name.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000892 V.V->setName(Name);
Reid Spencer950bf602007-01-26 08:19:09 +0000893 }
894}
895
896/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
897/// this is a declaration, otherwise it is a definition.
898static GlobalVariable *
899ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
900 bool isConstantGlobal, const Type *Ty,
Reid Spencerbb1fd572007-03-21 17:15:50 +0000901 Constant *Initializer,
902 const Signedness &Sign) {
Reid Spencer950bf602007-01-26 08:19:09 +0000903 if (isa<FunctionType>(Ty))
904 error("Cannot declare global vars of function type");
905
906 const PointerType *PTy = PointerType::get(Ty);
907
908 std::string Name;
909 if (NameStr) {
910 Name = NameStr; // Copy string
911 free(NameStr); // Free old string
912 }
913
914 // See if this global value was forward referenced. If so, recycle the
915 // object.
916 ValID ID;
917 if (!Name.empty()) {
Reid Spencer5eb77c72007-03-15 03:26:42 +0000918 ID = ValID::create((char*)Name.c_str());
Reid Spencer950bf602007-01-26 08:19:09 +0000919 } else {
Reid Spencer5eb77c72007-03-15 03:26:42 +0000920 ID = ValID::create((int)CurModule.Values[PTy].size());
Reid Spencer950bf602007-01-26 08:19:09 +0000921 }
Reid Spencerbb1fd572007-03-21 17:15:50 +0000922 ID.S.makeComposite(Sign);
Reid Spencer950bf602007-01-26 08:19:09 +0000923
924 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
925 // Move the global to the end of the list, from whereever it was
926 // previously inserted.
927 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
928 CurModule.CurrentModule->getGlobalList().remove(GV);
929 CurModule.CurrentModule->getGlobalList().push_back(GV);
930 GV->setInitializer(Initializer);
931 GV->setLinkage(Linkage);
932 GV->setConstant(isConstantGlobal);
933 InsertValue(GV, CurModule.Values);
934 return GV;
935 }
936
937 // If this global has a name, check to see if there is already a definition
938 // of this global in the module and emit warnings if there are conflicts.
939 if (!Name.empty()) {
940 // The global has a name. See if there's an existing one of the same name.
Reid Spencere59f4932007-04-16 03:05:01 +0000941 if (CurModule.CurrentModule->getNamedGlobal(Name) ||
942 CurModule.CurrentModule->getFunction(Name)) {
943 // We found an existing global of the same name. This isn't allowed
Reid Spencer950bf602007-01-26 08:19:09 +0000944 // in LLVM 2.0. Consequently, we must alter the name of the global so it
945 // can at least compile. This can happen because of type planes
946 // There is alread a global of the same name which means there is a
947 // conflict. Let's see what we can do about it.
948 std::string NewName(makeNameUnique(Name));
Reid Spencerbb1fd572007-03-21 17:15:50 +0000949 if (Linkage != GlobalValue::InternalLinkage) {
Reid Spencer950bf602007-01-26 08:19:09 +0000950 // The linkage of this gval is external so we can't reliably rename
951 // it because it could potentially create a linking problem.
952 // However, we can't leave the name conflict in the output either or
953 // it won't assemble with LLVM 2.0. So, all we can do is rename
954 // this one to something unique and emit a warning about the problem.
955 warning("Renaming global variable '" + Name + "' to '" + NewName +
956 "' may cause linkage errors");
957 }
958
959 // Put the renaming in the global rename map
Reid Spencerbb1fd572007-03-21 17:15:50 +0000960 RenameMapKey Key = makeRenameMapKey(Name, PointerType::get(Ty), ID.S);
Reid Spencer950bf602007-01-26 08:19:09 +0000961 CurModule.RenameMap[Key] = NewName;
962
963 // Rename it
964 Name = NewName;
965 }
966 }
967
968 // Otherwise there is no existing GV to use, create one now.
969 GlobalVariable *GV =
970 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
971 CurModule.CurrentModule);
972 InsertValue(GV, CurModule.Values);
Reid Spencerbb1fd572007-03-21 17:15:50 +0000973 // Remember the sign of this global.
974 CurModule.NamedValueSigns[Name] = ID.S;
Reid Spencer950bf602007-01-26 08:19:09 +0000975 return GV;
976}
977
978// setTypeName - Set the specified type to the name given. The name may be
979// null potentially, in which case this is a noop. The string passed in is
980// assumed to be a malloc'd string buffer, and is freed by this function.
981//
982// This function returns true if the type has already been defined, but is
983// allowed to be redefined in the specified context. If the name is a new name
984// for the type plane, it is inserted and false is returned.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000985static bool setTypeName(const PATypeInfo& TI, char *NameStr) {
Reid Spencer950bf602007-01-26 08:19:09 +0000986 assert(!inFunctionScope() && "Can't give types function-local names");
987 if (NameStr == 0) return false;
988
989 std::string Name(NameStr); // Copy string
990 free(NameStr); // Free old string
991
Reid Spencerbb1fd572007-03-21 17:15:50 +0000992 const Type* Ty = TI.PAT->get();
993
Reid Spencer950bf602007-01-26 08:19:09 +0000994 // We don't allow assigning names to void type
Reid Spencerbb1fd572007-03-21 17:15:50 +0000995 if (Ty == Type::VoidTy) {
Reid Spencer950bf602007-01-26 08:19:09 +0000996 error("Can't assign name '" + Name + "' to the void type");
997 return false;
998 }
999
1000 // Set the type name, checking for conflicts as we do so.
Reid Spencerbb1fd572007-03-21 17:15:50 +00001001 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, Ty);
1002
1003 // Save the sign information for later use
1004 CurModule.NamedTypeSigns[Name] = TI.S;
Reid Spencer950bf602007-01-26 08:19:09 +00001005
1006 if (AlreadyExists) { // Inserting a name that is already defined???
1007 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
1008 assert(Existing && "Conflict but no matching type?");
1009
1010 // There is only one case where this is allowed: when we are refining an
1011 // opaque type. In this case, Existing will be an opaque type.
1012 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
1013 // We ARE replacing an opaque type!
Reid Spencerbb1fd572007-03-21 17:15:50 +00001014 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(Ty);
Reid Spencer950bf602007-01-26 08:19:09 +00001015 return true;
1016 }
1017
1018 // Otherwise, this is an attempt to redefine a type. That's okay if
1019 // the redefinition is identical to the original. This will be so if
1020 // Existing and T point to the same Type object. In this one case we
1021 // allow the equivalent redefinition.
Reid Spencerbb1fd572007-03-21 17:15:50 +00001022 if (Existing == Ty) return true; // Yes, it's equal.
Reid Spencer950bf602007-01-26 08:19:09 +00001023
1024 // Any other kind of (non-equivalent) redefinition is an error.
1025 error("Redefinition of type named '" + Name + "' in the '" +
Reid Spencerbb1fd572007-03-21 17:15:50 +00001026 Ty->getDescription() + "' type plane");
Reid Spencer950bf602007-01-26 08:19:09 +00001027 }
1028
1029 return false;
1030}
1031
1032//===----------------------------------------------------------------------===//
1033// Code for handling upreferences in type names...
1034//
1035
1036// TypeContains - Returns true if Ty directly contains E in it.
1037//
1038static bool TypeContains(const Type *Ty, const Type *E) {
1039 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
1040 E) != Ty->subtype_end();
1041}
1042
1043namespace {
1044 struct UpRefRecord {
1045 // NestingLevel - The number of nesting levels that need to be popped before
1046 // this type is resolved.
1047 unsigned NestingLevel;
1048
1049 // LastContainedTy - This is the type at the current binding level for the
1050 // type. Every time we reduce the nesting level, this gets updated.
1051 const Type *LastContainedTy;
1052
1053 // UpRefTy - This is the actual opaque type that the upreference is
1054 // represented with.
1055 OpaqueType *UpRefTy;
1056
1057 UpRefRecord(unsigned NL, OpaqueType *URTy)
Reid Spencerbb1fd572007-03-21 17:15:50 +00001058 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) { }
Reid Spencer950bf602007-01-26 08:19:09 +00001059 };
1060}
1061
1062// UpRefs - A list of the outstanding upreferences that need to be resolved.
1063static std::vector<UpRefRecord> UpRefs;
1064
1065/// HandleUpRefs - Every time we finish a new layer of types, this function is
1066/// called. It loops through the UpRefs vector, which is a list of the
1067/// currently active types. For each type, if the up reference is contained in
1068/// the newly completed type, we decrement the level count. When the level
1069/// count reaches zero, the upreferenced type is the type that is passed in:
1070/// thus we can complete the cycle.
1071///
Reid Spencerbb1fd572007-03-21 17:15:50 +00001072static PATypeHolder HandleUpRefs(const Type *ty, const Signedness& Sign) {
Reid Spencer950bf602007-01-26 08:19:09 +00001073 // If Ty isn't abstract, or if there are no up-references in it, then there is
1074 // nothing to resolve here.
1075 if (!ty->isAbstract() || UpRefs.empty()) return ty;
1076
1077 PATypeHolder Ty(ty);
1078 UR_OUT("Type '" << Ty->getDescription() <<
1079 "' newly formed. Resolving upreferences.\n" <<
1080 UpRefs.size() << " upreferences active!\n");
1081
1082 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
1083 // to zero), we resolve them all together before we resolve them to Ty. At
1084 // the end of the loop, if there is anything to resolve to Ty, it will be in
1085 // this variable.
1086 OpaqueType *TypeToResolve = 0;
1087
Reid Spencerbb1fd572007-03-21 17:15:50 +00001088 unsigned i = 0;
1089 for (; i != UpRefs.size(); ++i) {
Reid Spencer950bf602007-01-26 08:19:09 +00001090 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Reid Spencerbb1fd572007-03-21 17:15:50 +00001091 << UpRefs[i].UpRefTy->getDescription() << ") = "
1092 << (TypeContains(Ty, UpRefs[i].UpRefTy) ? "true" : "false") << "\n");
Reid Spencer950bf602007-01-26 08:19:09 +00001093 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
1094 // Decrement level of upreference
1095 unsigned Level = --UpRefs[i].NestingLevel;
1096 UpRefs[i].LastContainedTy = Ty;
1097 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
1098 if (Level == 0) { // Upreference should be resolved!
1099 if (!TypeToResolve) {
1100 TypeToResolve = UpRefs[i].UpRefTy;
1101 } else {
1102 UR_OUT(" * Resolving upreference for "
Reid Spencerbb1fd572007-03-21 17:15:50 +00001103 << UpRefs[i].UpRefTy->getDescription() << "\n";
1104 std::string OldName = UpRefs[i].UpRefTy->getDescription());
1105 ResolveTypeSign(UpRefs[i].UpRefTy, Sign);
Reid Spencer950bf602007-01-26 08:19:09 +00001106 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
1107 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
1108 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
1109 }
1110 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
1111 --i; // Do not skip the next element...
1112 }
1113 }
1114 }
1115
1116 if (TypeToResolve) {
1117 UR_OUT(" * Resolving upreference for "
Reid Spencerbb1fd572007-03-21 17:15:50 +00001118 << UpRefs[i].UpRefTy->getDescription() << "\n";
Reid Spencer950bf602007-01-26 08:19:09 +00001119 std::string OldName = TypeToResolve->getDescription());
Reid Spencerbb1fd572007-03-21 17:15:50 +00001120 ResolveTypeSign(TypeToResolve, Sign);
Reid Spencer950bf602007-01-26 08:19:09 +00001121 TypeToResolve->refineAbstractTypeTo(Ty);
1122 }
1123
1124 return Ty;
1125}
1126
Reid Spencerbb1fd572007-03-21 17:15:50 +00001127bool Signedness::operator<(const Signedness &that) const {
1128 if (isNamed()) {
1129 if (that.isNamed())
1130 return *(this->name) < *(that.name);
1131 else
1132 return CurModule.NamedTypeSigns[*name] < that;
1133 } else if (that.isNamed()) {
1134 return *this < CurModule.NamedTypeSigns[*that.name];
1135 }
1136
1137 if (isComposite() && that.isComposite()) {
1138 if (sv->size() == that.sv->size()) {
1139 SignVector::const_iterator thisI = sv->begin(), thisE = sv->end();
1140 SignVector::const_iterator thatI = that.sv->begin(),
1141 thatE = that.sv->end();
1142 for (; thisI != thisE; ++thisI, ++thatI) {
1143 if (*thisI < *thatI)
1144 return true;
1145 else if (!(*thisI == *thatI))
1146 return false;
1147 }
1148 return false;
1149 }
1150 return sv->size() < that.sv->size();
1151 }
1152 return kind < that.kind;
1153}
1154
1155bool Signedness::operator==(const Signedness &that) const {
1156 if (isNamed())
1157 if (that.isNamed())
1158 return *(this->name) == *(that.name);
1159 else
1160 return CurModule.NamedTypeSigns[*(this->name)] == that;
1161 else if (that.isNamed())
1162 return *this == CurModule.NamedTypeSigns[*(that.name)];
1163 if (isComposite() && that.isComposite()) {
1164 if (sv->size() == that.sv->size()) {
1165 SignVector::const_iterator thisI = sv->begin(), thisE = sv->end();
1166 SignVector::const_iterator thatI = that.sv->begin(),
1167 thatE = that.sv->end();
1168 for (; thisI != thisE; ++thisI, ++thatI) {
1169 if (!(*thisI == *thatI))
1170 return false;
1171 }
1172 return true;
1173 }
1174 return false;
1175 }
1176 return kind == that.kind;
1177}
1178
1179void Signedness::copy(const Signedness &that) {
1180 if (that.isNamed()) {
1181 kind = Named;
1182 name = new std::string(*that.name);
1183 } else if (that.isComposite()) {
1184 kind = Composite;
1185 sv = new SignVector();
1186 *sv = *that.sv;
1187 } else {
1188 kind = that.kind;
1189 sv = 0;
1190 }
1191}
1192
1193void Signedness::destroy() {
1194 if (isNamed()) {
1195 delete name;
1196 } else if (isComposite()) {
1197 delete sv;
1198 }
1199}
1200
Evan Cheng2b484202007-03-22 07:43:51 +00001201#ifndef NDEBUG
Reid Spencerbb1fd572007-03-21 17:15:50 +00001202void Signedness::dump() const {
1203 if (isComposite()) {
1204 if (sv->size() == 1) {
1205 (*sv)[0].dump();
1206 std::cerr << "*";
1207 } else {
1208 std::cerr << "{ " ;
1209 for (unsigned i = 0; i < sv->size(); ++i) {
1210 if (i != 0)
1211 std::cerr << ", ";
1212 (*sv)[i].dump();
1213 }
1214 std::cerr << "} " ;
1215 }
1216 } else if (isNamed()) {
1217 std::cerr << *name;
1218 } else if (isSigned()) {
1219 std::cerr << "S";
1220 } else if (isUnsigned()) {
1221 std::cerr << "U";
1222 } else
1223 std::cerr << ".";
1224}
Evan Cheng2b484202007-03-22 07:43:51 +00001225#endif
Reid Spencerbb1fd572007-03-21 17:15:50 +00001226
Reid Spencer950bf602007-01-26 08:19:09 +00001227static inline Instruction::TermOps
1228getTermOp(TermOps op) {
1229 switch (op) {
1230 default : assert(0 && "Invalid OldTermOp");
1231 case RetOp : return Instruction::Ret;
1232 case BrOp : return Instruction::Br;
1233 case SwitchOp : return Instruction::Switch;
1234 case InvokeOp : return Instruction::Invoke;
1235 case UnwindOp : return Instruction::Unwind;
1236 case UnreachableOp: return Instruction::Unreachable;
1237 }
1238}
1239
1240static inline Instruction::BinaryOps
Reid Spencerbb1fd572007-03-21 17:15:50 +00001241getBinaryOp(BinaryOps op, const Type *Ty, const Signedness& Sign) {
Reid Spencer950bf602007-01-26 08:19:09 +00001242 switch (op) {
1243 default : assert(0 && "Invalid OldBinaryOps");
1244 case SetEQ :
1245 case SetNE :
1246 case SetLE :
1247 case SetGE :
1248 case SetLT :
1249 case SetGT : assert(0 && "Should use getCompareOp");
1250 case AddOp : return Instruction::Add;
1251 case SubOp : return Instruction::Sub;
1252 case MulOp : return Instruction::Mul;
1253 case DivOp : {
1254 // This is an obsolete instruction so we must upgrade it based on the
1255 // types of its operands.
1256 bool isFP = Ty->isFloatingPoint();
Reid Spencer9d6565a2007-02-15 02:26:10 +00001257 if (const VectorType* PTy = dyn_cast<VectorType>(Ty))
Chris Lattner4227bdb2007-02-19 07:34:02 +00001258 // If its a vector type we want to use the element type
Reid Spencer950bf602007-01-26 08:19:09 +00001259 isFP = PTy->getElementType()->isFloatingPoint();
1260 if (isFP)
1261 return Instruction::FDiv;
Reid Spencerbb1fd572007-03-21 17:15:50 +00001262 else if (Sign.isSigned())
Reid Spencer950bf602007-01-26 08:19:09 +00001263 return Instruction::SDiv;
1264 return Instruction::UDiv;
1265 }
1266 case UDivOp : return Instruction::UDiv;
1267 case SDivOp : return Instruction::SDiv;
1268 case FDivOp : return Instruction::FDiv;
1269 case RemOp : {
1270 // This is an obsolete instruction so we must upgrade it based on the
1271 // types of its operands.
1272 bool isFP = Ty->isFloatingPoint();
Reid Spencer9d6565a2007-02-15 02:26:10 +00001273 if (const VectorType* PTy = dyn_cast<VectorType>(Ty))
Chris Lattner4227bdb2007-02-19 07:34:02 +00001274 // If its a vector type we want to use the element type
Reid Spencer950bf602007-01-26 08:19:09 +00001275 isFP = PTy->getElementType()->isFloatingPoint();
1276 // Select correct opcode
1277 if (isFP)
1278 return Instruction::FRem;
Reid Spencerbb1fd572007-03-21 17:15:50 +00001279 else if (Sign.isSigned())
Reid Spencer950bf602007-01-26 08:19:09 +00001280 return Instruction::SRem;
1281 return Instruction::URem;
1282 }
1283 case URemOp : return Instruction::URem;
1284 case SRemOp : return Instruction::SRem;
1285 case FRemOp : return Instruction::FRem;
Reid Spencer832254e2007-02-02 02:16:23 +00001286 case LShrOp : return Instruction::LShr;
1287 case AShrOp : return Instruction::AShr;
1288 case ShlOp : return Instruction::Shl;
1289 case ShrOp :
Reid Spencerbb1fd572007-03-21 17:15:50 +00001290 if (Sign.isSigned())
Reid Spencer832254e2007-02-02 02:16:23 +00001291 return Instruction::AShr;
1292 return Instruction::LShr;
Reid Spencer950bf602007-01-26 08:19:09 +00001293 case AndOp : return Instruction::And;
1294 case OrOp : return Instruction::Or;
1295 case XorOp : return Instruction::Xor;
1296 }
1297}
1298
1299static inline Instruction::OtherOps
1300getCompareOp(BinaryOps op, unsigned short &predicate, const Type* &Ty,
Reid Spencerbb1fd572007-03-21 17:15:50 +00001301 const Signedness &Sign) {
1302 bool isSigned = Sign.isSigned();
Reid Spencer950bf602007-01-26 08:19:09 +00001303 bool isFP = Ty->isFloatingPoint();
1304 switch (op) {
1305 default : assert(0 && "Invalid OldSetCC");
1306 case SetEQ :
1307 if (isFP) {
1308 predicate = FCmpInst::FCMP_OEQ;
1309 return Instruction::FCmp;
1310 } else {
1311 predicate = ICmpInst::ICMP_EQ;
1312 return Instruction::ICmp;
1313 }
1314 case SetNE :
1315 if (isFP) {
1316 predicate = FCmpInst::FCMP_UNE;
1317 return Instruction::FCmp;
1318 } else {
1319 predicate = ICmpInst::ICMP_NE;
1320 return Instruction::ICmp;
1321 }
1322 case SetLE :
1323 if (isFP) {
1324 predicate = FCmpInst::FCMP_OLE;
1325 return Instruction::FCmp;
1326 } else {
1327 if (isSigned)
1328 predicate = ICmpInst::ICMP_SLE;
1329 else
1330 predicate = ICmpInst::ICMP_ULE;
1331 return Instruction::ICmp;
1332 }
1333 case SetGE :
1334 if (isFP) {
1335 predicate = FCmpInst::FCMP_OGE;
1336 return Instruction::FCmp;
1337 } else {
1338 if (isSigned)
1339 predicate = ICmpInst::ICMP_SGE;
1340 else
1341 predicate = ICmpInst::ICMP_UGE;
1342 return Instruction::ICmp;
1343 }
1344 case SetLT :
1345 if (isFP) {
1346 predicate = FCmpInst::FCMP_OLT;
1347 return Instruction::FCmp;
1348 } else {
1349 if (isSigned)
1350 predicate = ICmpInst::ICMP_SLT;
1351 else
1352 predicate = ICmpInst::ICMP_ULT;
1353 return Instruction::ICmp;
1354 }
1355 case SetGT :
1356 if (isFP) {
1357 predicate = FCmpInst::FCMP_OGT;
1358 return Instruction::FCmp;
1359 } else {
1360 if (isSigned)
1361 predicate = ICmpInst::ICMP_SGT;
1362 else
1363 predicate = ICmpInst::ICMP_UGT;
1364 return Instruction::ICmp;
1365 }
1366 }
1367}
1368
1369static inline Instruction::MemoryOps getMemoryOp(MemoryOps op) {
1370 switch (op) {
1371 default : assert(0 && "Invalid OldMemoryOps");
1372 case MallocOp : return Instruction::Malloc;
1373 case FreeOp : return Instruction::Free;
1374 case AllocaOp : return Instruction::Alloca;
1375 case LoadOp : return Instruction::Load;
1376 case StoreOp : return Instruction::Store;
1377 case GetElementPtrOp : return Instruction::GetElementPtr;
1378 }
1379}
1380
1381static inline Instruction::OtherOps
Reid Spencerbb1fd572007-03-21 17:15:50 +00001382getOtherOp(OtherOps op, const Signedness &Sign) {
Reid Spencer950bf602007-01-26 08:19:09 +00001383 switch (op) {
1384 default : assert(0 && "Invalid OldOtherOps");
1385 case PHIOp : return Instruction::PHI;
1386 case CallOp : return Instruction::Call;
Reid Spencer950bf602007-01-26 08:19:09 +00001387 case SelectOp : return Instruction::Select;
1388 case UserOp1 : return Instruction::UserOp1;
1389 case UserOp2 : return Instruction::UserOp2;
1390 case VAArg : return Instruction::VAArg;
1391 case ExtractElementOp : return Instruction::ExtractElement;
1392 case InsertElementOp : return Instruction::InsertElement;
1393 case ShuffleVectorOp : return Instruction::ShuffleVector;
1394 case ICmpOp : return Instruction::ICmp;
1395 case FCmpOp : return Instruction::FCmp;
Reid Spencer950bf602007-01-26 08:19:09 +00001396 };
1397}
1398
1399static inline Value*
Reid Spencerbb1fd572007-03-21 17:15:50 +00001400getCast(CastOps op, Value *Src, const Signedness &SrcSign, const Type *DstTy,
1401 const Signedness &DstSign, bool ForceInstruction = false) {
Reid Spencer950bf602007-01-26 08:19:09 +00001402 Instruction::CastOps Opcode;
1403 const Type* SrcTy = Src->getType();
1404 if (op == CastOp) {
1405 if (SrcTy->isFloatingPoint() && isa<PointerType>(DstTy)) {
1406 // fp -> ptr cast is no longer supported but we must upgrade this
1407 // by doing a double cast: fp -> int -> ptr
1408 SrcTy = Type::Int64Ty;
1409 Opcode = Instruction::IntToPtr;
1410 if (isa<Constant>(Src)) {
1411 Src = ConstantExpr::getCast(Instruction::FPToUI,
1412 cast<Constant>(Src), SrcTy);
1413 } else {
1414 std::string NewName(makeNameUnique(Src->getName()));
1415 Src = new FPToUIInst(Src, SrcTy, NewName, CurBB);
1416 }
1417 } else if (isa<IntegerType>(DstTy) &&
1418 cast<IntegerType>(DstTy)->getBitWidth() == 1) {
1419 // cast type %x to bool was previously defined as setne type %x, null
1420 // The cast semantic is now to truncate, not compare so we must retain
1421 // the original intent by replacing the cast with a setne
1422 Constant* Null = Constant::getNullValue(SrcTy);
1423 Instruction::OtherOps Opcode = Instruction::ICmp;
1424 unsigned short predicate = ICmpInst::ICMP_NE;
1425 if (SrcTy->isFloatingPoint()) {
1426 Opcode = Instruction::FCmp;
1427 predicate = FCmpInst::FCMP_ONE;
1428 } else if (!SrcTy->isInteger() && !isa<PointerType>(SrcTy)) {
1429 error("Invalid cast to bool");
1430 }
1431 if (isa<Constant>(Src) && !ForceInstruction)
1432 return ConstantExpr::getCompare(predicate, cast<Constant>(Src), Null);
1433 else
1434 return CmpInst::create(Opcode, predicate, Src, Null);
1435 }
1436 // Determine the opcode to use by calling CastInst::getCastOpcode
1437 Opcode =
Reid Spencerbb1fd572007-03-21 17:15:50 +00001438 CastInst::getCastOpcode(Src, SrcSign.isSigned(), DstTy,
1439 DstSign.isSigned());
Reid Spencer950bf602007-01-26 08:19:09 +00001440
1441 } else switch (op) {
1442 default: assert(0 && "Invalid cast token");
1443 case TruncOp: Opcode = Instruction::Trunc; break;
1444 case ZExtOp: Opcode = Instruction::ZExt; break;
1445 case SExtOp: Opcode = Instruction::SExt; break;
1446 case FPTruncOp: Opcode = Instruction::FPTrunc; break;
1447 case FPExtOp: Opcode = Instruction::FPExt; break;
1448 case FPToUIOp: Opcode = Instruction::FPToUI; break;
1449 case FPToSIOp: Opcode = Instruction::FPToSI; break;
1450 case UIToFPOp: Opcode = Instruction::UIToFP; break;
1451 case SIToFPOp: Opcode = Instruction::SIToFP; break;
1452 case PtrToIntOp: Opcode = Instruction::PtrToInt; break;
1453 case IntToPtrOp: Opcode = Instruction::IntToPtr; break;
1454 case BitCastOp: Opcode = Instruction::BitCast; break;
1455 }
1456
1457 if (isa<Constant>(Src) && !ForceInstruction)
1458 return ConstantExpr::getCast(Opcode, cast<Constant>(Src), DstTy);
1459 return CastInst::create(Opcode, Src, DstTy);
1460}
1461
1462static Instruction *
1463upgradeIntrinsicCall(const Type* RetTy, const ValID &ID,
1464 std::vector<Value*>& Args) {
1465
1466 std::string Name = ID.Type == ValID::NameVal ? ID.Name : "";
Reid Spencer7eea8ff2007-05-18 05:48:07 +00001467 if (Name.length() <= 5 || Name[0] != 'l' || Name[1] != 'l' ||
1468 Name[2] != 'v' || Name[3] != 'm' || Name[4] != '.')
1469 return 0;
1470
Reid Spencer41b213e2007-04-02 01:14:00 +00001471 switch (Name[5]) {
1472 case 'i':
1473 if (Name == "llvm.isunordered.f32" || Name == "llvm.isunordered.f64") {
1474 if (Args.size() != 2)
1475 error("Invalid prototype for " + Name);
1476 return new FCmpInst(FCmpInst::FCMP_UNO, Args[0], Args[1]);
1477 }
1478 break;
Reid Spencer8166a6c2007-04-02 02:08:35 +00001479
Reid Spencer41b213e2007-04-02 01:14:00 +00001480 case 'v' : {
1481 const Type* PtrTy = PointerType::get(Type::Int8Ty);
1482 std::vector<const Type*> Params;
1483 if (Name == "llvm.va_start" || Name == "llvm.va_end") {
1484 if (Args.size() != 1)
1485 error("Invalid prototype for " + Name + " prototype");
1486 Params.push_back(PtrTy);
1487 const FunctionType *FTy =
1488 FunctionType::get(Type::VoidTy, Params, false);
1489 const PointerType *PFTy = PointerType::get(FTy);
1490 Value* Func = getVal(PFTy, ID);
1491 Args[0] = new BitCastInst(Args[0], PtrTy, makeNameUnique("va"), CurBB);
David Greene718fda32007-08-01 03:59:32 +00001492 return new CallInst(Func, Args.begin(), Args.end());
Reid Spencer41b213e2007-04-02 01:14:00 +00001493 } else if (Name == "llvm.va_copy") {
1494 if (Args.size() != 2)
1495 error("Invalid prototype for " + Name + " prototype");
1496 Params.push_back(PtrTy);
1497 Params.push_back(PtrTy);
1498 const FunctionType *FTy =
1499 FunctionType::get(Type::VoidTy, Params, false);
1500 const PointerType *PFTy = PointerType::get(FTy);
1501 Value* Func = getVal(PFTy, ID);
1502 std::string InstName0(makeNameUnique("va0"));
1503 std::string InstName1(makeNameUnique("va1"));
1504 Args[0] = new BitCastInst(Args[0], PtrTy, InstName0, CurBB);
1505 Args[1] = new BitCastInst(Args[1], PtrTy, InstName1, CurBB);
David Greene718fda32007-08-01 03:59:32 +00001506 return new CallInst(Func, Args.begin(), Args.end());
Reid Spencer41b213e2007-04-02 01:14:00 +00001507 }
Reid Spencer950bf602007-01-26 08:19:09 +00001508 }
1509 }
1510 return 0;
1511}
1512
Reid Spencerff0e4482007-04-16 00:40:57 +00001513const Type* upgradeGEPCEIndices(const Type* PTy,
1514 std::vector<ValueInfo> *Indices,
1515 std::vector<Constant*> &Result) {
1516 const Type *Ty = PTy;
1517 Result.clear();
1518 for (unsigned i = 0, e = Indices->size(); i != e ; ++i) {
1519 Constant *Index = cast<Constant>((*Indices)[i].V);
1520
1521 if (ConstantInt *CI = dyn_cast<ConstantInt>(Index)) {
1522 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte
1523 // struct indices to i32 struct indices with ZExt for compatibility.
1524 if (CI->getBitWidth() < 32)
1525 Index = ConstantExpr::getCast(Instruction::ZExt, CI, Type::Int32Ty);
1526 }
1527
1528 if (isa<SequentialType>(Ty)) {
1529 // Make sure that unsigned SequentialType indices are zext'd to
1530 // 64-bits if they were smaller than that because LLVM 2.0 will sext
1531 // all indices for SequentialType elements. We must retain the same
1532 // semantic (zext) for unsigned types.
1533 if (const IntegerType *Ity = dyn_cast<IntegerType>(Index->getType())) {
1534 if (Ity->getBitWidth() < 64 && (*Indices)[i].S.isUnsigned()) {
1535 Index = ConstantExpr::getCast(Instruction::ZExt, Index,Type::Int64Ty);
1536 }
1537 }
1538 }
1539 Result.push_back(Index);
David Greene5fd22a82007-09-04 18:46:50 +00001540 Ty = GetElementPtrInst::getIndexedType(PTy, Result.begin(),
1541 Result.end(),true);
Reid Spencerff0e4482007-04-16 00:40:57 +00001542 if (!Ty)
1543 error("Index list invalid for constant getelementptr");
1544 }
1545 return Ty;
1546}
1547
1548const Type* upgradeGEPInstIndices(const Type* PTy,
1549 std::vector<ValueInfo> *Indices,
1550 std::vector<Value*> &Result) {
1551 const Type *Ty = PTy;
1552 Result.clear();
1553 for (unsigned i = 0, e = Indices->size(); i != e ; ++i) {
1554 Value *Index = (*Indices)[i].V;
1555
1556 if (ConstantInt *CI = dyn_cast<ConstantInt>(Index)) {
1557 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte
1558 // struct indices to i32 struct indices with ZExt for compatibility.
1559 if (CI->getBitWidth() < 32)
1560 Index = ConstantExpr::getCast(Instruction::ZExt, CI, Type::Int32Ty);
1561 }
1562
1563
1564 if (isa<StructType>(Ty)) { // Only change struct indices
1565 if (!isa<Constant>(Index)) {
1566 error("Invalid non-constant structure index");
1567 return 0;
1568 }
Reid Spencer950bf602007-01-26 08:19:09 +00001569 } else {
1570 // Make sure that unsigned SequentialType indices are zext'd to
1571 // 64-bits if they were smaller than that because LLVM 2.0 will sext
1572 // all indices for SequentialType elements. We must retain the same
1573 // semantic (zext) for unsigned types.
Reid Spencerff0e4482007-04-16 00:40:57 +00001574 if (const IntegerType *Ity = dyn_cast<IntegerType>(Index->getType())) {
Reid Spencerbb1fd572007-03-21 17:15:50 +00001575 if (Ity->getBitWidth() < 64 && (*Indices)[i].S.isUnsigned()) {
Reid Spencerff0e4482007-04-16 00:40:57 +00001576 if (isa<Constant>(Index))
Reid Spencer950bf602007-01-26 08:19:09 +00001577 Index = ConstantExpr::getCast(Instruction::ZExt,
1578 cast<Constant>(Index), Type::Int64Ty);
1579 else
1580 Index = CastInst::create(Instruction::ZExt, Index, Type::Int64Ty,
Reid Spencer832254e2007-02-02 02:16:23 +00001581 makeNameUnique("gep"), CurBB);
Reid Spencer38f682b2007-01-26 20:31:18 +00001582 }
Reid Spencerff0e4482007-04-16 00:40:57 +00001583 }
Reid Spencer950bf602007-01-26 08:19:09 +00001584 }
Reid Spencerff0e4482007-04-16 00:40:57 +00001585 Result.push_back(Index);
David Greene5fd22a82007-09-04 18:46:50 +00001586 Ty = GetElementPtrInst::getIndexedType(PTy, Result.begin(),
1587 Result.end(),true);
Reid Spencerff0e4482007-04-16 00:40:57 +00001588 if (!Ty)
Reid Spencer950bf602007-01-26 08:19:09 +00001589 error("Index list invalid for constant getelementptr");
Reid Spencerff0e4482007-04-16 00:40:57 +00001590 }
1591 return Ty;
Reid Spencer950bf602007-01-26 08:19:09 +00001592}
1593
Reid Spencerb7046c72007-01-29 05:41:34 +00001594unsigned upgradeCallingConv(unsigned CC) {
1595 switch (CC) {
1596 case OldCallingConv::C : return CallingConv::C;
1597 case OldCallingConv::CSRet : return CallingConv::C;
1598 case OldCallingConv::Fast : return CallingConv::Fast;
1599 case OldCallingConv::Cold : return CallingConv::Cold;
1600 case OldCallingConv::X86_StdCall : return CallingConv::X86_StdCall;
1601 case OldCallingConv::X86_FastCall: return CallingConv::X86_FastCall;
1602 default:
1603 return CC;
1604 }
1605}
1606
Reid Spencer950bf602007-01-26 08:19:09 +00001607Module* UpgradeAssembly(const std::string &infile, std::istream& in,
1608 bool debug, bool addAttrs)
Reid Spencere7c3c602006-11-30 06:36:44 +00001609{
1610 Upgradelineno = 1;
1611 CurFilename = infile;
Reid Spencer96839be2006-11-30 16:50:26 +00001612 LexInput = &in;
Reid Spencere77e35e2006-12-01 20:26:20 +00001613 yydebug = debug;
Reid Spencer71d2ec92006-12-31 06:02:26 +00001614 AddAttributes = addAttrs;
Reid Spencer950bf602007-01-26 08:19:09 +00001615 ObsoleteVarArgs = false;
1616 NewVarArgs = false;
Reid Spencere7c3c602006-11-30 06:36:44 +00001617
Reid Spencer950bf602007-01-26 08:19:09 +00001618 CurModule.CurrentModule = new Module(CurFilename);
1619
1620 // Check to make sure the parser succeeded
Reid Spencere7c3c602006-11-30 06:36:44 +00001621 if (yyparse()) {
Reid Spencer950bf602007-01-26 08:19:09 +00001622 if (ParserResult)
1623 delete ParserResult;
Reid Spencer30d0c582007-01-15 00:26:18 +00001624 std::cerr << "llvm-upgrade: parse failed.\n";
Reid Spencer30d0c582007-01-15 00:26:18 +00001625 return 0;
1626 }
1627
Reid Spencer950bf602007-01-26 08:19:09 +00001628 // Check to make sure that parsing produced a result
1629 if (!ParserResult) {
1630 std::cerr << "llvm-upgrade: no parse result.\n";
1631 return 0;
Reid Spencer30d0c582007-01-15 00:26:18 +00001632 }
1633
Reid Spencer950bf602007-01-26 08:19:09 +00001634 // Reset ParserResult variable while saving its value for the result.
1635 Module *Result = ParserResult;
1636 ParserResult = 0;
Reid Spencer30d0c582007-01-15 00:26:18 +00001637
Reid Spencer950bf602007-01-26 08:19:09 +00001638 //Not all functions use vaarg, so make a second check for ObsoleteVarArgs
Reid Spencer30d0c582007-01-15 00:26:18 +00001639 {
Reid Spencer950bf602007-01-26 08:19:09 +00001640 Function* F;
Reid Spencer688b0492007-02-05 21:19:13 +00001641 if ((F = Result->getFunction("llvm.va_start"))
Reid Spencer950bf602007-01-26 08:19:09 +00001642 && F->getFunctionType()->getNumParams() == 0)
1643 ObsoleteVarArgs = true;
Reid Spencer688b0492007-02-05 21:19:13 +00001644 if((F = Result->getFunction("llvm.va_copy"))
Reid Spencer950bf602007-01-26 08:19:09 +00001645 && F->getFunctionType()->getNumParams() == 1)
1646 ObsoleteVarArgs = true;
Reid Spencer280d8012006-12-01 23:40:53 +00001647 }
Reid Spencer319a7302007-01-05 17:20:02 +00001648
Reid Spencer950bf602007-01-26 08:19:09 +00001649 if (ObsoleteVarArgs && NewVarArgs) {
1650 error("This file is corrupt: it uses both new and old style varargs");
1651 return 0;
Reid Spencer319a7302007-01-05 17:20:02 +00001652 }
Reid Spencer319a7302007-01-05 17:20:02 +00001653
Reid Spencer950bf602007-01-26 08:19:09 +00001654 if(ObsoleteVarArgs) {
Reid Spencer688b0492007-02-05 21:19:13 +00001655 if(Function* F = Result->getFunction("llvm.va_start")) {
Reid Spencer950bf602007-01-26 08:19:09 +00001656 if (F->arg_size() != 0) {
1657 error("Obsolete va_start takes 0 argument");
Reid Spencer319a7302007-01-05 17:20:02 +00001658 return 0;
1659 }
Reid Spencer950bf602007-01-26 08:19:09 +00001660
1661 //foo = va_start()
1662 // ->
1663 //bar = alloca typeof(foo)
1664 //va_start(bar)
1665 //foo = load bar
Reid Spencer319a7302007-01-05 17:20:02 +00001666
Reid Spencer950bf602007-01-26 08:19:09 +00001667 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
1668 const Type* ArgTy = F->getFunctionType()->getReturnType();
1669 const Type* ArgTyPtr = PointerType::get(ArgTy);
1670 Function* NF = cast<Function>(Result->getOrInsertFunction(
1671 "llvm.va_start", RetTy, ArgTyPtr, (Type *)0));
1672
1673 while (!F->use_empty()) {
1674 CallInst* CI = cast<CallInst>(F->use_back());
1675 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
1676 new CallInst(NF, bar, "", CI);
1677 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
1678 CI->replaceAllUsesWith(foo);
1679 CI->getParent()->getInstList().erase(CI);
Reid Spencerf8383de2007-01-06 06:04:32 +00001680 }
Reid Spencer950bf602007-01-26 08:19:09 +00001681 Result->getFunctionList().erase(F);
Reid Spencerf8383de2007-01-06 06:04:32 +00001682 }
Reid Spencer950bf602007-01-26 08:19:09 +00001683
Reid Spencer688b0492007-02-05 21:19:13 +00001684 if(Function* F = Result->getFunction("llvm.va_end")) {
Reid Spencer950bf602007-01-26 08:19:09 +00001685 if(F->arg_size() != 1) {
1686 error("Obsolete va_end takes 1 argument");
1687 return 0;
Reid Spencerf8383de2007-01-06 06:04:32 +00001688 }
Reid Spencerf8383de2007-01-06 06:04:32 +00001689
Reid Spencer950bf602007-01-26 08:19:09 +00001690 //vaend foo
1691 // ->
1692 //bar = alloca 1 of typeof(foo)
1693 //vaend bar
1694 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
1695 const Type* ArgTy = F->getFunctionType()->getParamType(0);
1696 const Type* ArgTyPtr = PointerType::get(ArgTy);
1697 Function* NF = cast<Function>(Result->getOrInsertFunction(
1698 "llvm.va_end", RetTy, ArgTyPtr, (Type *)0));
Reid Spencerf8383de2007-01-06 06:04:32 +00001699
Reid Spencer950bf602007-01-26 08:19:09 +00001700 while (!F->use_empty()) {
1701 CallInst* CI = cast<CallInst>(F->use_back());
1702 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
1703 new StoreInst(CI->getOperand(1), bar, CI);
1704 new CallInst(NF, bar, "", CI);
1705 CI->getParent()->getInstList().erase(CI);
Reid Spencere77e35e2006-12-01 20:26:20 +00001706 }
Reid Spencer950bf602007-01-26 08:19:09 +00001707 Result->getFunctionList().erase(F);
Reid Spencere77e35e2006-12-01 20:26:20 +00001708 }
Reid Spencer950bf602007-01-26 08:19:09 +00001709
Reid Spencer688b0492007-02-05 21:19:13 +00001710 if(Function* F = Result->getFunction("llvm.va_copy")) {
Reid Spencer950bf602007-01-26 08:19:09 +00001711 if(F->arg_size() != 1) {
1712 error("Obsolete va_copy takes 1 argument");
1713 return 0;
Reid Spencere77e35e2006-12-01 20:26:20 +00001714 }
Reid Spencer950bf602007-01-26 08:19:09 +00001715 //foo = vacopy(bar)
1716 // ->
1717 //a = alloca 1 of typeof(foo)
1718 //b = alloca 1 of typeof(foo)
1719 //store bar -> b
1720 //vacopy(a, b)
1721 //foo = load a
1722
1723 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
1724 const Type* ArgTy = F->getFunctionType()->getReturnType();
1725 const Type* ArgTyPtr = PointerType::get(ArgTy);
1726 Function* NF = cast<Function>(Result->getOrInsertFunction(
1727 "llvm.va_copy", RetTy, ArgTyPtr, ArgTyPtr, (Type *)0));
Reid Spencere77e35e2006-12-01 20:26:20 +00001728
Reid Spencer950bf602007-01-26 08:19:09 +00001729 while (!F->use_empty()) {
1730 CallInst* CI = cast<CallInst>(F->use_back());
David Greeneae5115d2007-08-15 17:58:51 +00001731 Value *Args[2] = {
1732 new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI),
1733 new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI)
1734 };
David Greene718fda32007-08-01 03:59:32 +00001735 new StoreInst(CI->getOperand(1), Args[1], CI);
David Greeneae5115d2007-08-15 17:58:51 +00001736 new CallInst(NF, Args, Args + 2, "", CI);
David Greene718fda32007-08-01 03:59:32 +00001737 Value* foo = new LoadInst(Args[0], "vacopy.fix.3", CI);
Reid Spencer950bf602007-01-26 08:19:09 +00001738 CI->replaceAllUsesWith(foo);
1739 CI->getParent()->getInstList().erase(CI);
1740 }
1741 Result->getFunctionList().erase(F);
Reid Spencer319a7302007-01-05 17:20:02 +00001742 }
1743 }
1744
Reid Spencer52402b02007-01-02 05:45:11 +00001745 return Result;
1746}
1747
Reid Spencer950bf602007-01-26 08:19:09 +00001748} // end llvm namespace
Reid Spencer319a7302007-01-05 17:20:02 +00001749
Reid Spencer950bf602007-01-26 08:19:09 +00001750using namespace llvm;
Reid Spencer30d0c582007-01-15 00:26:18 +00001751
Reid Spencere7c3c602006-11-30 06:36:44 +00001752%}
1753
Reid Spencere77e35e2006-12-01 20:26:20 +00001754%union {
Reid Spencer950bf602007-01-26 08:19:09 +00001755 llvm::Module *ModuleVal;
1756 llvm::Function *FunctionVal;
1757 std::pair<llvm::PATypeInfo, char*> *ArgVal;
1758 llvm::BasicBlock *BasicBlockVal;
Reid Spencerbb1fd572007-03-21 17:15:50 +00001759 llvm::TermInstInfo TermInstVal;
Reid Spencer950bf602007-01-26 08:19:09 +00001760 llvm::InstrInfo InstVal;
1761 llvm::ConstInfo ConstVal;
1762 llvm::ValueInfo ValueVal;
1763 llvm::PATypeInfo TypeVal;
1764 llvm::TypeInfo PrimType;
1765 llvm::PHIListInfo PHIList;
1766 std::list<llvm::PATypeInfo> *TypeList;
1767 std::vector<llvm::ValueInfo> *ValueList;
1768 std::vector<llvm::ConstInfo> *ConstVector;
1769
1770
1771 std::vector<std::pair<llvm::PATypeInfo,char*> > *ArgList;
1772 // Represent the RHS of PHI node
1773 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
1774
1775 llvm::GlobalValue::LinkageTypes Linkage;
1776 int64_t SInt64Val;
1777 uint64_t UInt64Val;
1778 int SIntVal;
1779 unsigned UIntVal;
Dale Johannesen43421b32007-09-06 18:13:44 +00001780 llvm::APFloat *FPVal;
Reid Spencer950bf602007-01-26 08:19:09 +00001781 bool BoolVal;
1782
1783 char *StrVal; // This memory is strdup'd!
1784 llvm::ValID ValIDVal; // strdup'd memory maybe!
1785
1786 llvm::BinaryOps BinaryOpVal;
1787 llvm::TermOps TermOpVal;
1788 llvm::MemoryOps MemOpVal;
1789 llvm::OtherOps OtherOpVal;
1790 llvm::CastOps CastOpVal;
1791 llvm::ICmpInst::Predicate IPred;
1792 llvm::FCmpInst::Predicate FPred;
1793 llvm::Module::Endianness Endianness;
Reid Spencere77e35e2006-12-01 20:26:20 +00001794}
1795
Reid Spencer950bf602007-01-26 08:19:09 +00001796%type <ModuleVal> Module FunctionList
1797%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
1798%type <BasicBlockVal> BasicBlock InstructionList
1799%type <TermInstVal> BBTerminatorInst
1800%type <InstVal> Inst InstVal MemoryInst
1801%type <ConstVal> ConstVal ConstExpr
1802%type <ConstVector> ConstVector
1803%type <ArgList> ArgList ArgListH
1804%type <ArgVal> ArgVal
1805%type <PHIList> PHIList
1806%type <ValueList> ValueRefList ValueRefListE // For call param lists
1807%type <ValueList> IndexList // For GEP derived indices
1808%type <TypeList> TypeListI ArgTypeListI
1809%type <JumpTable> JumpTable
1810%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
1811%type <BoolVal> OptVolatile // 'volatile' or not
1812%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1813%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencered96d1e2007-02-08 09:08:52 +00001814%type <Linkage> OptLinkage FnDeclareLinkage
Reid Spencer950bf602007-01-26 08:19:09 +00001815%type <Endianness> BigOrLittle
Reid Spencere77e35e2006-12-01 20:26:20 +00001816
Reid Spencer950bf602007-01-26 08:19:09 +00001817// ValueRef - Unresolved reference to a definition or BB
1818%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1819%type <ValueVal> ResolvedVal // <type> <valref> pair
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001820
Reid Spencer950bf602007-01-26 08:19:09 +00001821// Tokens and types for handling constant integer values
1822//
1823// ESINT64VAL - A negative number within long long range
1824%token <SInt64Val> ESINT64VAL
Reid Spencere77e35e2006-12-01 20:26:20 +00001825
Reid Spencer950bf602007-01-26 08:19:09 +00001826// EUINT64VAL - A positive number within uns. long long range
1827%token <UInt64Val> EUINT64VAL
1828%type <SInt64Val> EINT64VAL
Reid Spencere77e35e2006-12-01 20:26:20 +00001829
Reid Spencer950bf602007-01-26 08:19:09 +00001830%token <SIntVal> SINTVAL // Signed 32 bit ints...
1831%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
1832%type <SIntVal> INTVAL
1833%token <FPVal> FPVAL // Float or Double constant
Reid Spencere77e35e2006-12-01 20:26:20 +00001834
Reid Spencer950bf602007-01-26 08:19:09 +00001835// Built in types...
1836%type <TypeVal> Types TypesV UpRTypes UpRTypesV
1837%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
1838%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
1839%token <PrimType> FLOAT DOUBLE TYPE LABEL
Reid Spencere77e35e2006-12-01 20:26:20 +00001840
Reid Spencer950bf602007-01-26 08:19:09 +00001841%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
1842%type <StrVal> Name OptName OptAssign
1843%type <UIntVal> OptAlign OptCAlign
1844%type <StrVal> OptSection SectionString
1845
1846%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
1847%token DECLARE GLOBAL CONSTANT SECTION VOLATILE
1848%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
1849%token DLLIMPORT DLLEXPORT EXTERN_WEAK
1850%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
1851%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
1852%token CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
1853%token X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
1854%token DATALAYOUT
1855%type <UIntVal> OptCallingConv
1856
1857// Basic Block Terminating Operators
1858%token <TermOpVal> RET BR SWITCH INVOKE UNREACHABLE
1859%token UNWIND EXCEPT
1860
1861// Binary Operators
1862%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Reid Spencer832254e2007-02-02 02:16:23 +00001863%type <BinaryOpVal> ShiftOps
Reid Spencer950bf602007-01-26 08:19:09 +00001864%token <BinaryOpVal> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM
Reid Spencer832254e2007-02-02 02:16:23 +00001865%token <BinaryOpVal> AND OR XOR SHL SHR ASHR LSHR
Reid Spencer950bf602007-01-26 08:19:09 +00001866%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
1867%token <OtherOpVal> ICMP FCMP
1868
1869// Memory Instructions
1870%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1871
1872// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001873%token <OtherOpVal> PHI_TOK SELECT VAARG
Reid Spencer950bf602007-01-26 08:19:09 +00001874%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
1875%token VAARG_old VANEXT_old //OBSOLETE
1876
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001877// Support for ICmp/FCmp Predicates, which is 1.9++ but not 2.0
Reid Spencer950bf602007-01-26 08:19:09 +00001878%type <IPred> IPredicates
1879%type <FPred> FPredicates
1880%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1881%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
1882
1883%token <CastOpVal> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI
1884%token <CastOpVal> UITOFP SITOFP PTRTOINT INTTOPTR BITCAST
1885%type <CastOpVal> CastOps
Reid Spencere7c3c602006-11-30 06:36:44 +00001886
1887%start Module
1888
1889%%
1890
1891// Handle constant integer size restriction and conversion...
Reid Spencer950bf602007-01-26 08:19:09 +00001892//
1893INTVAL
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001894 : SINTVAL
Reid Spencer950bf602007-01-26 08:19:09 +00001895 | UINTVAL {
1896 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
1897 error("Value too large for type");
1898 $$ = (int32_t)$1;
1899 }
1900 ;
1901
1902EINT64VAL
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001903 : ESINT64VAL // These have same type and can't cause problems...
Reid Spencer950bf602007-01-26 08:19:09 +00001904 | EUINT64VAL {
1905 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
1906 error("Value too large for type");
1907 $$ = (int64_t)$1;
1908 };
Reid Spencere7c3c602006-11-30 06:36:44 +00001909
1910// Operations that are notably excluded from this list include:
1911// RET, BR, & SWITCH because they end basic blocks and are treated specially.
Reid Spencer950bf602007-01-26 08:19:09 +00001912//
1913ArithmeticOps
1914 : ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV | REM | UREM | SREM | FREM
1915 ;
1916
1917LogicalOps
1918 : AND | OR | XOR
1919 ;
1920
1921SetCondOps
1922 : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
1923 ;
1924
1925IPredicates
1926 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
1927 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1928 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1929 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1930 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1931 ;
1932
1933FPredicates
1934 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1935 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1936 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1937 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1938 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1939 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1940 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1941 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1942 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1943 ;
1944ShiftOps
1945 : SHL | SHR | ASHR | LSHR
1946 ;
1947
1948CastOps
1949 : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI
1950 | UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
1951 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001952
1953// These are some types that allow classification if we only want a particular
1954// thing... for example, only a signed, unsigned, or integral type.
Reid Spencer950bf602007-01-26 08:19:09 +00001955SIntType
1956 : LONG | INT | SHORT | SBYTE
1957 ;
1958
1959UIntType
1960 : ULONG | UINT | USHORT | UBYTE
1961 ;
1962
1963IntType
1964 : SIntType | UIntType
1965 ;
1966
1967FPType
1968 : FLOAT | DOUBLE
1969 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001970
1971// OptAssign - Value producing statements have an optional assignment component
Reid Spencer950bf602007-01-26 08:19:09 +00001972OptAssign
1973 : Name '=' {
Reid Spencere7c3c602006-11-30 06:36:44 +00001974 $$ = $1;
1975 }
1976 | /*empty*/ {
Reid Spencer950bf602007-01-26 08:19:09 +00001977 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001978 };
1979
1980OptLinkage
Reid Spencer785a5ae2007-02-08 00:21:40 +00001981 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencer950bf602007-01-26 08:19:09 +00001982 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1983 | WEAK { $$ = GlobalValue::WeakLinkage; }
1984 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1985 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1986 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencer785a5ae2007-02-08 00:21:40 +00001987 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencer950bf602007-01-26 08:19:09 +00001988 | /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1989 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001990
1991OptCallingConv
Reid Spencer7eea8ff2007-05-18 05:48:07 +00001992 : /*empty*/ { $$ = lastCallingConv = OldCallingConv::C; }
1993 | CCC_TOK { $$ = lastCallingConv = OldCallingConv::C; }
1994 | CSRETCC_TOK { $$ = lastCallingConv = OldCallingConv::CSRet; }
1995 | FASTCC_TOK { $$ = lastCallingConv = OldCallingConv::Fast; }
1996 | COLDCC_TOK { $$ = lastCallingConv = OldCallingConv::Cold; }
1997 | X86_STDCALLCC_TOK { $$ = lastCallingConv = OldCallingConv::X86_StdCall; }
1998 | X86_FASTCALLCC_TOK { $$ = lastCallingConv = OldCallingConv::X86_FastCall; }
Reid Spencer950bf602007-01-26 08:19:09 +00001999 | CC_TOK EUINT64VAL {
2000 if ((unsigned)$2 != $2)
2001 error("Calling conv too large");
Reid Spencer7eea8ff2007-05-18 05:48:07 +00002002 $$ = lastCallingConv = $2;
Reid Spencer950bf602007-01-26 08:19:09 +00002003 }
2004 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002005
2006// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
2007// a comma before it.
2008OptAlign
Reid Spencer950bf602007-01-26 08:19:09 +00002009 : /*empty*/ { $$ = 0; }
2010 | ALIGN EUINT64VAL {
2011 $$ = $2;
2012 if ($$ != 0 && !isPowerOf2_32($$))
2013 error("Alignment must be a power of two");
2014 }
2015 ;
Reid Spencerf0cf1322006-12-07 04:23:03 +00002016
Reid Spencere7c3c602006-11-30 06:36:44 +00002017OptCAlign
Reid Spencer950bf602007-01-26 08:19:09 +00002018 : /*empty*/ { $$ = 0; }
2019 | ',' ALIGN EUINT64VAL {
2020 $$ = $3;
2021 if ($$ != 0 && !isPowerOf2_32($$))
2022 error("Alignment must be a power of two");
2023 }
2024 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002025
2026SectionString
Reid Spencer950bf602007-01-26 08:19:09 +00002027 : SECTION STRINGCONSTANT {
2028 for (unsigned i = 0, e = strlen($2); i != e; ++i)
2029 if ($2[i] == '"' || $2[i] == '\\')
2030 error("Invalid character in section name");
2031 $$ = $2;
2032 }
2033 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002034
Reid Spencer950bf602007-01-26 08:19:09 +00002035OptSection
2036 : /*empty*/ { $$ = 0; }
2037 | SectionString { $$ = $1; }
2038 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002039
Reid Spencer950bf602007-01-26 08:19:09 +00002040// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
2041// is set to be the global we are processing.
2042//
Reid Spencere7c3c602006-11-30 06:36:44 +00002043GlobalVarAttributes
Reid Spencer950bf602007-01-26 08:19:09 +00002044 : /* empty */ {}
2045 | ',' GlobalVarAttribute GlobalVarAttributes {}
2046 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002047
Reid Spencer950bf602007-01-26 08:19:09 +00002048GlobalVarAttribute
2049 : SectionString {
2050 CurGV->setSection($1);
2051 free($1);
2052 }
2053 | ALIGN EUINT64VAL {
2054 if ($2 != 0 && !isPowerOf2_32($2))
2055 error("Alignment must be a power of two");
2056 CurGV->setAlignment($2);
2057
2058 }
2059 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002060
2061//===----------------------------------------------------------------------===//
2062// Types includes all predefined types... except void, because it can only be
2063// used in specific contexts (function returning void for example). To have
2064// access to it, a user must explicitly use TypesV.
2065//
2066
2067// TypesV includes all of 'Types', but it also includes the void type.
Reid Spencer950bf602007-01-26 08:19:09 +00002068TypesV
2069 : Types
2070 | VOID {
Reid Spencered96d1e2007-02-08 09:08:52 +00002071 $$.PAT = new PATypeHolder($1.T);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002072 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002073 }
2074 ;
2075
2076UpRTypesV
2077 : UpRTypes
2078 | VOID {
Reid Spencered96d1e2007-02-08 09:08:52 +00002079 $$.PAT = new PATypeHolder($1.T);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002080 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002081 }
2082 ;
2083
2084Types
2085 : UpRTypes {
2086 if (!UpRefs.empty())
Reid Spencered96d1e2007-02-08 09:08:52 +00002087 error("Invalid upreference in type: " + (*$1.PAT)->getDescription());
Reid Spencer950bf602007-01-26 08:19:09 +00002088 $$ = $1;
2089 }
2090 ;
2091
2092PrimType
2093 : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
2094 | LONG | ULONG | FLOAT | DOUBLE | LABEL
2095 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002096
2097// Derived types are added later...
Reid Spencera50d5962006-12-02 04:11:07 +00002098UpRTypes
Reid Spencer950bf602007-01-26 08:19:09 +00002099 : PrimType {
Reid Spencered96d1e2007-02-08 09:08:52 +00002100 $$.PAT = new PATypeHolder($1.T);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002101 $$.S.copy($1.S);
Reid Spencera50d5962006-12-02 04:11:07 +00002102 }
Reid Spencer950bf602007-01-26 08:19:09 +00002103 | OPAQUE {
Reid Spencered96d1e2007-02-08 09:08:52 +00002104 $$.PAT = new PATypeHolder(OpaqueType::get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002105 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002106 }
2107 | SymbolicValueRef { // Named types are also simple types...
Reid Spencerbb1fd572007-03-21 17:15:50 +00002108 $$.S.copy(getTypeSign($1));
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00002109 const Type* tmp = getType($1);
Reid Spencered96d1e2007-02-08 09:08:52 +00002110 $$.PAT = new PATypeHolder(tmp);
Reid Spencer78720742006-12-02 20:21:22 +00002111 }
2112 | '\\' EUINT64VAL { // Type UpReference
Reid Spencer950bf602007-01-26 08:19:09 +00002113 if ($2 > (uint64_t)~0U)
2114 error("Value out of range");
2115 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
2116 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencered96d1e2007-02-08 09:08:52 +00002117 $$.PAT = new PATypeHolder(OT);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002118 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002119 UR_OUT("New Upreference!\n");
Reid Spencere7c3c602006-11-30 06:36:44 +00002120 }
2121 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Reid Spencerbb1fd572007-03-21 17:15:50 +00002122 $$.S.makeComposite($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002123 std::vector<const Type*> Params;
2124 for (std::list<llvm::PATypeInfo>::iterator I = $3->begin(),
2125 E = $3->end(); I != E; ++I) {
Reid Spencered96d1e2007-02-08 09:08:52 +00002126 Params.push_back(I->PAT->get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002127 $$.S.add(I->S);
Reid Spencer52402b02007-01-02 05:45:11 +00002128 }
Reid Spencer950bf602007-01-26 08:19:09 +00002129 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
2130 if (isVarArg) Params.pop_back();
2131
Reid Spencer7eea8ff2007-05-18 05:48:07 +00002132 ParamAttrsList *PAL = 0;
2133 if (lastCallingConv == OldCallingConv::CSRet) {
2134 ParamAttrsVector Attrs;
2135 ParamAttrsWithIndex PAWI;
2136 PAWI.index = 1; PAWI.attrs = ParamAttr::StructRet; // first arg
2137 Attrs.push_back(PAWI);
2138 PAL = ParamAttrsList::get(Attrs);
2139 }
2140
Reid Spencer7b5d4662007-04-09 06:16:21 +00002141 const FunctionType *FTy =
Reid Spencer7eea8ff2007-05-18 05:48:07 +00002142 FunctionType::get($1.PAT->get(), Params, isVarArg, PAL);
Reid Spencer7b5d4662007-04-09 06:16:21 +00002143
2144 $$.PAT = new PATypeHolder( HandleUpRefs(FTy, $$.S) );
Reid Spencerbb1fd572007-03-21 17:15:50 +00002145 delete $1.PAT; // Delete the return type handle
Reid Spencer950bf602007-01-26 08:19:09 +00002146 delete $3; // Delete the argument list
Reid Spencere7c3c602006-11-30 06:36:44 +00002147 }
2148 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencerbb1fd572007-03-21 17:15:50 +00002149 $$.S.makeComposite($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002150 $$.PAT = new PATypeHolder(HandleUpRefs(ArrayType::get($4.PAT->get(),
Reid Spencerbb1fd572007-03-21 17:15:50 +00002151 (unsigned)$2), $$.S));
Reid Spencered96d1e2007-02-08 09:08:52 +00002152 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002153 }
Chris Lattner4227bdb2007-02-19 07:34:02 +00002154 | '<' EUINT64VAL 'x' UpRTypes '>' { // Vector type?
Reid Spencerbb1fd572007-03-21 17:15:50 +00002155 const llvm::Type* ElemTy = $4.PAT->get();
2156 if ((unsigned)$2 != $2)
2157 error("Unsigned result not equal to signed result");
2158 if (!(ElemTy->isInteger() || ElemTy->isFloatingPoint()))
2159 error("Elements of a VectorType must be integer or floating point");
2160 if (!isPowerOf2_32($2))
2161 error("VectorType length should be a power of 2");
2162 $$.S.makeComposite($4.S);
2163 $$.PAT = new PATypeHolder(HandleUpRefs(VectorType::get(ElemTy,
2164 (unsigned)$2), $$.S));
2165 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002166 }
2167 | '{' TypeListI '}' { // Structure type?
Reid Spencer950bf602007-01-26 08:19:09 +00002168 std::vector<const Type*> Elements;
Reid Spencerbb1fd572007-03-21 17:15:50 +00002169 $$.S.makeComposite();
Reid Spencer950bf602007-01-26 08:19:09 +00002170 for (std::list<llvm::PATypeInfo>::iterator I = $2->begin(),
Reid Spencerbb1fd572007-03-21 17:15:50 +00002171 E = $2->end(); I != E; ++I) {
Reid Spencered96d1e2007-02-08 09:08:52 +00002172 Elements.push_back(I->PAT->get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002173 $$.S.add(I->S);
2174 }
2175 $$.PAT = new PATypeHolder(HandleUpRefs(StructType::get(Elements), $$.S));
Reid Spencer950bf602007-01-26 08:19:09 +00002176 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00002177 }
2178 | '{' '}' { // Empty structure type?
Reid Spencered96d1e2007-02-08 09:08:52 +00002179 $$.PAT = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencerbb1fd572007-03-21 17:15:50 +00002180 $$.S.makeComposite();
Reid Spencere7c3c602006-11-30 06:36:44 +00002181 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002182 | '<' '{' TypeListI '}' '>' { // Packed Structure type?
Reid Spencerbb1fd572007-03-21 17:15:50 +00002183 $$.S.makeComposite();
Reid Spencer950bf602007-01-26 08:19:09 +00002184 std::vector<const Type*> Elements;
2185 for (std::list<llvm::PATypeInfo>::iterator I = $3->begin(),
2186 E = $3->end(); I != E; ++I) {
Reid Spencered96d1e2007-02-08 09:08:52 +00002187 Elements.push_back(I->PAT->get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002188 $$.S.add(I->S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002189 delete I->PAT;
Reid Spencer52402b02007-01-02 05:45:11 +00002190 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00002191 $$.PAT = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true),
2192 $$.S));
Reid Spencer950bf602007-01-26 08:19:09 +00002193 delete $3;
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002194 }
2195 | '<' '{' '}' '>' { // Empty packed structure type?
Reid Spencered96d1e2007-02-08 09:08:52 +00002196 $$.PAT = new PATypeHolder(StructType::get(std::vector<const Type*>(),true));
Reid Spencerbb1fd572007-03-21 17:15:50 +00002197 $$.S.makeComposite();
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002198 }
Reid Spencere7c3c602006-11-30 06:36:44 +00002199 | UpRTypes '*' { // Pointer type?
Reid Spencered96d1e2007-02-08 09:08:52 +00002200 if ($1.PAT->get() == Type::LabelTy)
Reid Spencer950bf602007-01-26 08:19:09 +00002201 error("Cannot form a pointer to a basic block");
Reid Spencerbb1fd572007-03-21 17:15:50 +00002202 $$.S.makeComposite($1.S);
2203 $$.PAT = new PATypeHolder(HandleUpRefs(PointerType::get($1.PAT->get()),
2204 $$.S));
Reid Spencered96d1e2007-02-08 09:08:52 +00002205 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002206 }
2207 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002208
2209// TypeList - Used for struct declarations and as a basis for function type
2210// declaration type lists
2211//
Reid Spencere77e35e2006-12-01 20:26:20 +00002212TypeListI
2213 : UpRTypes {
Reid Spencer950bf602007-01-26 08:19:09 +00002214 $$ = new std::list<PATypeInfo>();
2215 $$->push_back($1);
Reid Spencere77e35e2006-12-01 20:26:20 +00002216 }
2217 | TypeListI ',' UpRTypes {
Reid Spencer950bf602007-01-26 08:19:09 +00002218 ($$=$1)->push_back($3);
2219 }
2220 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002221
2222// ArgTypeList - List of types for a function type declaration...
Reid Spencere77e35e2006-12-01 20:26:20 +00002223ArgTypeListI
Reid Spencer950bf602007-01-26 08:19:09 +00002224 : TypeListI
Reid Spencere7c3c602006-11-30 06:36:44 +00002225 | TypeListI ',' DOTDOTDOT {
Reid Spencer950bf602007-01-26 08:19:09 +00002226 PATypeInfo VoidTI;
Reid Spencered96d1e2007-02-08 09:08:52 +00002227 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002228 VoidTI.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002229 ($$=$1)->push_back(VoidTI);
Reid Spencere7c3c602006-11-30 06:36:44 +00002230 }
2231 | DOTDOTDOT {
Reid Spencer950bf602007-01-26 08:19:09 +00002232 $$ = new std::list<PATypeInfo>();
2233 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 $$->push_back(VoidTI);
Reid Spencere7c3c602006-11-30 06:36:44 +00002237 }
2238 | /*empty*/ {
Reid Spencer950bf602007-01-26 08:19:09 +00002239 $$ = new std::list<PATypeInfo>();
2240 }
2241 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002242
2243// ConstVal - The various declarations that go into the constant pool. This
2244// production is used ONLY to represent constants that show up AFTER a 'const',
2245// 'constant' or 'global' token at global scope. Constants that can be inlined
2246// into other expressions (such as integers and constexprs) are handled by the
2247// ResolvedVal, ValueRef and ConstValueRef productions.
2248//
Reid Spencer950bf602007-01-26 08:19:09 +00002249ConstVal
2250 : Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencered96d1e2007-02-08 09:08:52 +00002251 const ArrayType *ATy = dyn_cast<ArrayType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002252 if (ATy == 0)
2253 error("Cannot make array constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002254 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002255 const Type *ETy = ATy->getElementType();
2256 int NumElements = ATy->getNumElements();
2257
2258 // Verify that we have the correct size...
2259 if (NumElements != -1 && NumElements != (int)$3->size())
2260 error("Type mismatch: constant sized array initialized with " +
2261 utostr($3->size()) + " arguments, but has size of " +
2262 itostr(NumElements) + "");
2263
2264 // Verify all elements are correct type!
2265 std::vector<Constant*> Elems;
2266 for (unsigned i = 0; i < $3->size(); i++) {
2267 Constant *C = (*$3)[i].C;
2268 const Type* ValTy = C->getType();
2269 if (ETy != ValTy)
2270 error("Element #" + utostr(i) + " is not of type '" +
2271 ETy->getDescription() +"' as required!\nIt is of type '"+
2272 ValTy->getDescription() + "'");
2273 Elems.push_back(C);
2274 }
2275 $$.C = ConstantArray::get(ATy, Elems);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002276 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002277 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002278 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002279 }
2280 | Types '[' ']' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002281 const ArrayType *ATy = dyn_cast<ArrayType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002282 if (ATy == 0)
2283 error("Cannot make array constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002284 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002285 int NumElements = ATy->getNumElements();
2286 if (NumElements != -1 && NumElements != 0)
2287 error("Type mismatch: constant sized array initialized with 0"
2288 " arguments, but has size of " + itostr(NumElements) +"");
2289 $$.C = ConstantArray::get(ATy, std::vector<Constant*>());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002290 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002291 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002292 }
2293 | Types 'c' STRINGCONSTANT {
Reid Spencered96d1e2007-02-08 09:08:52 +00002294 const ArrayType *ATy = dyn_cast<ArrayType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002295 if (ATy == 0)
2296 error("Cannot make array constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002297 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002298 int NumElements = ATy->getNumElements();
2299 const Type *ETy = dyn_cast<IntegerType>(ATy->getElementType());
2300 if (!ETy || cast<IntegerType>(ETy)->getBitWidth() != 8)
2301 error("String arrays require type i8, not '" + ETy->getDescription() +
2302 "'");
2303 char *EndStr = UnEscapeLexed($3, true);
2304 if (NumElements != -1 && NumElements != (EndStr-$3))
2305 error("Can't build string constant of size " +
2306 itostr((int)(EndStr-$3)) + " when array has size " +
2307 itostr(NumElements) + "");
2308 std::vector<Constant*> Vals;
2309 for (char *C = (char *)$3; C != (char *)EndStr; ++C)
2310 Vals.push_back(ConstantInt::get(ETy, *C));
2311 free($3);
2312 $$.C = ConstantArray::get(ATy, Vals);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002313 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002314 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002315 }
2316 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer9d6565a2007-02-15 02:26:10 +00002317 const VectorType *PTy = dyn_cast<VectorType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002318 if (PTy == 0)
2319 error("Cannot make packed constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002320 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002321 const Type *ETy = PTy->getElementType();
2322 int NumElements = PTy->getNumElements();
2323 // Verify that we have the correct size...
2324 if (NumElements != -1 && NumElements != (int)$3->size())
2325 error("Type mismatch: constant sized packed initialized with " +
2326 utostr($3->size()) + " arguments, but has size of " +
2327 itostr(NumElements) + "");
2328 // Verify all elements are correct type!
2329 std::vector<Constant*> Elems;
2330 for (unsigned i = 0; i < $3->size(); i++) {
2331 Constant *C = (*$3)[i].C;
2332 const Type* ValTy = C->getType();
2333 if (ETy != ValTy)
2334 error("Element #" + utostr(i) + " is not of type '" +
2335 ETy->getDescription() +"' as required!\nIt is of type '"+
2336 ValTy->getDescription() + "'");
2337 Elems.push_back(C);
2338 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00002339 $$.C = ConstantVector::get(PTy, Elems);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002340 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002341 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002342 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002343 }
2344 | Types '{' ConstVector '}' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002345 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002346 if (STy == 0)
2347 error("Cannot make struct constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002348 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002349 if ($3->size() != STy->getNumContainedTypes())
2350 error("Illegal number of initializers for structure type");
2351
2352 // Check to ensure that constants are compatible with the type initializer!
2353 std::vector<Constant*> Fields;
2354 for (unsigned i = 0, e = $3->size(); i != e; ++i) {
2355 Constant *C = (*$3)[i].C;
2356 if (C->getType() != STy->getElementType(i))
2357 error("Expected type '" + STy->getElementType(i)->getDescription() +
2358 "' for element #" + utostr(i) + " of structure initializer");
2359 Fields.push_back(C);
2360 }
2361 $$.C = ConstantStruct::get(STy, Fields);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002362 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002363 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002364 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002365 }
2366 | Types '{' '}' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002367 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002368 if (STy == 0)
2369 error("Cannot make struct constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002370 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002371 if (STy->getNumContainedTypes() != 0)
2372 error("Illegal number of initializers for structure type");
2373 $$.C = ConstantStruct::get(STy, std::vector<Constant*>());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002374 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002375 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002376 }
Reid Spencer950bf602007-01-26 08:19:09 +00002377 | Types '<' '{' ConstVector '}' '>' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002378 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002379 if (STy == 0)
2380 error("Cannot make packed struct constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002381 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002382 if ($4->size() != STy->getNumContainedTypes())
2383 error("Illegal number of initializers for packed structure type");
Reid Spencere7c3c602006-11-30 06:36:44 +00002384
Reid Spencer950bf602007-01-26 08:19:09 +00002385 // Check to ensure that constants are compatible with the type initializer!
2386 std::vector<Constant*> Fields;
2387 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
2388 Constant *C = (*$4)[i].C;
2389 if (C->getType() != STy->getElementType(i))
2390 error("Expected type '" + STy->getElementType(i)->getDescription() +
2391 "' for element #" + utostr(i) + " of packed struct initializer");
2392 Fields.push_back(C);
Reid Spencer280d8012006-12-01 23:40:53 +00002393 }
Reid Spencer950bf602007-01-26 08:19:09 +00002394 $$.C = ConstantStruct::get(STy, Fields);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002395 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002396 delete $1.PAT;
Reid Spencere77e35e2006-12-01 20:26:20 +00002397 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00002398 }
Reid Spencer950bf602007-01-26 08:19:09 +00002399 | Types '<' '{' '}' '>' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002400 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002401 if (STy == 0)
2402 error("Cannot make packed struct constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002403 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002404 if (STy->getNumContainedTypes() != 0)
2405 error("Illegal number of initializers for packed structure type");
2406 $$.C = ConstantStruct::get(STy, std::vector<Constant*>());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002407 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002408 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002409 }
2410 | Types NULL_TOK {
Reid Spencered96d1e2007-02-08 09:08:52 +00002411 const PointerType *PTy = dyn_cast<PointerType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002412 if (PTy == 0)
2413 error("Cannot make null pointer constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002414 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002415 $$.C = ConstantPointerNull::get(PTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002416 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002417 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002418 }
2419 | Types UNDEF {
Reid Spencered96d1e2007-02-08 09:08:52 +00002420 $$.C = UndefValue::get($1.PAT->get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002421 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002422 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002423 }
2424 | Types SymbolicValueRef {
Reid Spencered96d1e2007-02-08 09:08:52 +00002425 const PointerType *Ty = dyn_cast<PointerType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002426 if (Ty == 0)
2427 error("Global const reference must be a pointer type, not" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002428 $1.PAT->get()->getDescription());
Reid Spencer950bf602007-01-26 08:19:09 +00002429
2430 // ConstExprs can exist in the body of a function, thus creating
2431 // GlobalValues whenever they refer to a variable. Because we are in
2432 // the context of a function, getExistingValue will search the functions
2433 // symbol table instead of the module symbol table for the global symbol,
2434 // which throws things all off. To get around this, we just tell
2435 // getExistingValue that we are at global scope here.
2436 //
2437 Function *SavedCurFn = CurFun.CurrentFunction;
2438 CurFun.CurrentFunction = 0;
Reid Spencerbb1fd572007-03-21 17:15:50 +00002439 $2.S.copy($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002440 Value *V = getExistingValue(Ty, $2);
2441 CurFun.CurrentFunction = SavedCurFn;
2442
2443 // If this is an initializer for a constant pointer, which is referencing a
2444 // (currently) undefined variable, create a stub now that shall be replaced
2445 // in the future with the right type of variable.
2446 //
2447 if (V == 0) {
2448 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers");
2449 const PointerType *PT = cast<PointerType>(Ty);
2450
2451 // First check to see if the forward references value is already created!
2452 PerModuleInfo::GlobalRefsType::iterator I =
2453 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
2454
2455 if (I != CurModule.GlobalRefs.end()) {
2456 V = I->second; // Placeholder already exists, use it...
2457 $2.destroy();
2458 } else {
2459 std::string Name;
2460 if ($2.Type == ValID::NameVal) Name = $2.Name;
2461
2462 // Create the forward referenced global.
2463 GlobalValue *GV;
2464 if (const FunctionType *FTy =
2465 dyn_cast<FunctionType>(PT->getElementType())) {
2466 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
2467 CurModule.CurrentModule);
2468 } else {
2469 GV = new GlobalVariable(PT->getElementType(), false,
2470 GlobalValue::ExternalLinkage, 0,
2471 Name, CurModule.CurrentModule);
2472 }
2473
2474 // Keep track of the fact that we have a forward ref to recycle it
2475 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
2476 V = GV;
2477 }
2478 }
2479 $$.C = cast<GlobalValue>(V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002480 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002481 delete $1.PAT; // Free the type handle
Reid Spencer950bf602007-01-26 08:19:09 +00002482 }
2483 | Types ConstExpr {
Reid Spencered96d1e2007-02-08 09:08:52 +00002484 if ($1.PAT->get() != $2.C->getType())
Reid Spencer950bf602007-01-26 08:19:09 +00002485 error("Mismatched types for constant expression");
2486 $$ = $2;
Reid Spencerbb1fd572007-03-21 17:15:50 +00002487 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002488 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002489 }
2490 | Types ZEROINITIALIZER {
Reid Spencered96d1e2007-02-08 09:08:52 +00002491 const Type *Ty = $1.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00002492 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
2493 error("Cannot create a null initialized value of this type");
2494 $$.C = Constant::getNullValue(Ty);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002495 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002496 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002497 }
2498 | SIntType EINT64VAL { // integral constants
2499 const Type *Ty = $1.T;
2500 if (!ConstantInt::isValueValidForType(Ty, $2))
2501 error("Constant value doesn't fit in type");
2502 $$.C = ConstantInt::get(Ty, $2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002503 $$.S.makeSigned();
Reid Spencer950bf602007-01-26 08:19:09 +00002504 }
2505 | UIntType EUINT64VAL { // 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.makeUnsigned();
Reid Spencer950bf602007-01-26 08:19:09 +00002511 }
2512 | BOOL TRUETOK { // Boolean constants
2513 $$.C = ConstantInt::get(Type::Int1Ty, true);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002514 $$.S.makeUnsigned();
Reid Spencer950bf602007-01-26 08:19:09 +00002515 }
2516 | BOOL FALSETOK { // Boolean constants
2517 $$.C = ConstantInt::get(Type::Int1Ty, false);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002518 $$.S.makeUnsigned();
Reid Spencer950bf602007-01-26 08:19:09 +00002519 }
2520 | FPType FPVAL { // Float & Double constants
Dale Johannesen43421b32007-09-06 18:13:44 +00002521 if (!ConstantFP::isValueValidForType($1.T, *$2))
Reid Spencer950bf602007-01-26 08:19:09 +00002522 error("Floating point constant invalid for type");
Dale Johannesen43421b32007-09-06 18:13:44 +00002523 // Lexer has no type info, so builds all FP constants as double.
2524 // Fix this here.
2525 if ($1.T==Type::FloatTy)
2526 $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
2527 $$.C = ConstantFP::get($1.T, *$2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002528 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002529 }
2530 ;
2531
2532ConstExpr
2533 : CastOps '(' ConstVal TO Types ')' {
2534 const Type* SrcTy = $3.C->getType();
Reid Spencered96d1e2007-02-08 09:08:52 +00002535 const Type* DstTy = $5.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00002536 Signedness SrcSign($3.S);
2537 Signedness DstSign($5.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002538 if (!SrcTy->isFirstClassType())
2539 error("cast constant expression from a non-primitive type: '" +
2540 SrcTy->getDescription() + "'");
2541 if (!DstTy->isFirstClassType())
2542 error("cast constant expression to a non-primitive type: '" +
2543 DstTy->getDescription() + "'");
2544 $$.C = cast<Constant>(getCast($1, $3.C, SrcSign, DstTy, DstSign));
Reid Spencerbb1fd572007-03-21 17:15:50 +00002545 $$.S.copy(DstSign);
Reid Spencered96d1e2007-02-08 09:08:52 +00002546 delete $5.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002547 }
2548 | GETELEMENTPTR '(' ConstVal IndexList ')' {
2549 const Type *Ty = $3.C->getType();
2550 if (!isa<PointerType>(Ty))
2551 error("GetElementPtr requires a pointer operand");
2552
Reid Spencer950bf602007-01-26 08:19:09 +00002553 std::vector<Constant*> CIndices;
Reid Spencerff0e4482007-04-16 00:40:57 +00002554 upgradeGEPCEIndices($3.C->getType(), $4, CIndices);
Reid Spencer950bf602007-01-26 08:19:09 +00002555
2556 delete $4;
Chris Lattner4227bdb2007-02-19 07:34:02 +00002557 $$.C = ConstantExpr::getGetElementPtr($3.C, &CIndices[0], CIndices.size());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002558 $$.S.copy(getElementSign($3, CIndices));
Reid Spencer950bf602007-01-26 08:19:09 +00002559 }
Reid Spencere7c3c602006-11-30 06:36:44 +00002560 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002561 if (!$3.C->getType()->isInteger() ||
2562 cast<IntegerType>($3.C->getType())->getBitWidth() != 1)
2563 error("Select condition must be bool type");
2564 if ($5.C->getType() != $7.C->getType())
2565 error("Select operand types must match");
2566 $$.C = ConstantExpr::getSelect($3.C, $5.C, $7.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002567 $$.S.copy($5.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002568 }
2569 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002570 const Type *Ty = $3.C->getType();
2571 if (Ty != $5.C->getType())
2572 error("Binary operator types must match");
2573 // First, make sure we're dealing with the right opcode by upgrading from
2574 // obsolete versions.
2575 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $3.S);
2576
2577 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
2578 // To retain backward compatibility with these early compilers, we emit a
2579 // cast to the appropriate integer type automatically if we are in the
2580 // broken case. See PR424 for more information.
2581 if (!isa<PointerType>(Ty)) {
2582 $$.C = ConstantExpr::get(Opcode, $3.C, $5.C);
2583 } else {
2584 const Type *IntPtrTy = 0;
2585 switch (CurModule.CurrentModule->getPointerSize()) {
2586 case Module::Pointer32: IntPtrTy = Type::Int32Ty; break;
2587 case Module::Pointer64: IntPtrTy = Type::Int64Ty; break;
2588 default: error("invalid pointer binary constant expr");
2589 }
2590 $$.C = ConstantExpr::get(Opcode,
2591 ConstantExpr::getCast(Instruction::PtrToInt, $3.C, IntPtrTy),
2592 ConstantExpr::getCast(Instruction::PtrToInt, $5.C, IntPtrTy));
2593 $$.C = ConstantExpr::getCast(Instruction::IntToPtr, $$.C, Ty);
2594 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00002595 $$.S.copy($3.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002596 }
2597 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002598 const Type* Ty = $3.C->getType();
2599 if (Ty != $5.C->getType())
2600 error("Logical operator types must match");
2601 if (!Ty->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00002602 if (!isa<VectorType>(Ty) ||
2603 !cast<VectorType>(Ty)->getElementType()->isInteger())
Reid Spencer950bf602007-01-26 08:19:09 +00002604 error("Logical operator requires integer operands");
2605 }
2606 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $3.S);
2607 $$.C = ConstantExpr::get(Opcode, $3.C, $5.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002608 $$.S.copy($3.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002609 }
2610 | SetCondOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002611 const Type* Ty = $3.C->getType();
2612 if (Ty != $5.C->getType())
2613 error("setcc operand types must match");
2614 unsigned short pred;
2615 Instruction::OtherOps Opcode = getCompareOp($1, pred, Ty, $3.S);
2616 $$.C = ConstantExpr::getCompare(Opcode, $3.C, $5.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002617 $$.S.makeUnsigned();
Reid Spencere7c3c602006-11-30 06:36:44 +00002618 }
Reid Spencer57f28f92006-12-03 07:10:26 +00002619 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002620 if ($4.C->getType() != $6.C->getType())
2621 error("icmp operand types must match");
2622 $$.C = ConstantExpr::getCompare($2, $4.C, $6.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002623 $$.S.makeUnsigned();
Reid Spencer57f28f92006-12-03 07:10:26 +00002624 }
2625 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002626 if ($4.C->getType() != $6.C->getType())
2627 error("fcmp operand types must match");
2628 $$.C = ConstantExpr::getCompare($2, $4.C, $6.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002629 $$.S.makeUnsigned();
Reid Spencer229e9362006-12-02 22:14:11 +00002630 }
Reid Spencere7c3c602006-11-30 06:36:44 +00002631 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002632 if (!$5.C->getType()->isInteger() ||
2633 cast<IntegerType>($5.C->getType())->getBitWidth() != 8)
2634 error("Shift count for shift constant must be unsigned byte");
Reid Spencer832254e2007-02-02 02:16:23 +00002635 const Type* Ty = $3.C->getType();
Reid Spencer950bf602007-01-26 08:19:09 +00002636 if (!$3.C->getType()->isInteger())
2637 error("Shift constant expression requires integer operand");
Reid Spencer832254e2007-02-02 02:16:23 +00002638 Constant *ShiftAmt = ConstantExpr::getZExt($5.C, Ty);
2639 $$.C = ConstantExpr::get(getBinaryOp($1, Ty, $3.S), $3.C, ShiftAmt);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002640 $$.S.copy($3.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002641 }
2642 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002643 if (!ExtractElementInst::isValidOperands($3.C, $5.C))
2644 error("Invalid extractelement operands");
2645 $$.C = ConstantExpr::getExtractElement($3.C, $5.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002646 $$.S.copy($3.S.get(0));
Reid Spencere7c3c602006-11-30 06:36:44 +00002647 }
2648 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002649 if (!InsertElementInst::isValidOperands($3.C, $5.C, $7.C))
2650 error("Invalid insertelement operands");
2651 $$.C = ConstantExpr::getInsertElement($3.C, $5.C, $7.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002652 $$.S.copy($3.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002653 }
2654 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002655 if (!ShuffleVectorInst::isValidOperands($3.C, $5.C, $7.C))
2656 error("Invalid shufflevector operands");
2657 $$.C = ConstantExpr::getShuffleVector($3.C, $5.C, $7.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002658 $$.S.copy($3.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002659 }
2660 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002661
2662
2663// ConstVector - A list of comma separated constants.
Reid Spencere77e35e2006-12-01 20:26:20 +00002664ConstVector
Reid Spencer950bf602007-01-26 08:19:09 +00002665 : ConstVector ',' ConstVal { ($$ = $1)->push_back($3); }
2666 | ConstVal {
2667 $$ = new std::vector<ConstInfo>();
2668 $$->push_back($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00002669 }
Reid Spencere77e35e2006-12-01 20:26:20 +00002670 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002671
2672
2673// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Reid Spencer950bf602007-01-26 08:19:09 +00002674GlobalType
2675 : GLOBAL { $$ = false; }
2676 | CONSTANT { $$ = true; }
2677 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002678
2679
2680//===----------------------------------------------------------------------===//
2681// Rules to match Modules
2682//===----------------------------------------------------------------------===//
2683
2684// Module rule: Capture the result of parsing the whole file into a result
2685// variable...
2686//
Reid Spencer950bf602007-01-26 08:19:09 +00002687Module
2688 : FunctionList {
2689 $$ = ParserResult = $1;
2690 CurModule.ModuleDone();
Reid Spencere7c3c602006-11-30 06:36:44 +00002691 }
Jeff Cohenac2dca92007-01-21 19:30:52 +00002692 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002693
Reid Spencer950bf602007-01-26 08:19:09 +00002694// FunctionList - A list of functions, preceeded by a constant pool.
2695//
2696FunctionList
2697 : FunctionList Function { $$ = $1; CurFun.FunctionDone(); }
2698 | FunctionList FunctionProto { $$ = $1; }
2699 | FunctionList MODULE ASM_TOK AsmBlock { $$ = $1; }
2700 | FunctionList IMPLEMENTATION { $$ = $1; }
2701 | ConstPool {
2702 $$ = CurModule.CurrentModule;
2703 // Emit an error if there are any unresolved types left.
2704 if (!CurModule.LateResolveTypes.empty()) {
2705 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
2706 if (DID.Type == ValID::NameVal) {
2707 error("Reference to an undefined type: '"+DID.getName() + "'");
2708 } else {
2709 error("Reference to an undefined type: #" + itostr(DID.Num));
2710 }
2711 }
2712 }
2713 ;
Reid Spencer78720742006-12-02 20:21:22 +00002714
Reid Spencere7c3c602006-11-30 06:36:44 +00002715// ConstPool - Constants with optional names assigned to them.
Reid Spencer950bf602007-01-26 08:19:09 +00002716ConstPool
2717 : ConstPool OptAssign TYPE TypesV {
2718 // Eagerly resolve types. This is not an optimization, this is a
2719 // requirement that is due to the fact that we could have this:
2720 //
2721 // %list = type { %list * }
2722 // %list = type { %list * } ; repeated type decl
2723 //
2724 // If types are not resolved eagerly, then the two types will not be
2725 // determined to be the same type!
2726 //
Reid Spencerbb1fd572007-03-21 17:15:50 +00002727 ResolveTypeTo($2, $4.PAT->get(), $4.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002728
Reid Spencerbb1fd572007-03-21 17:15:50 +00002729 if (!setTypeName($4, $2) && !$2) {
2730 // If this is a numbered type that is not a redefinition, add it to the
2731 // slot table.
2732 CurModule.Types.push_back($4.PAT->get());
2733 CurModule.TypeSigns.push_back($4.S);
Reid Spencera50d5962006-12-02 04:11:07 +00002734 }
Reid Spencered96d1e2007-02-08 09:08:52 +00002735 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002736 }
2737 | ConstPool FunctionProto { // Function prototypes can be in const pool
Reid Spencere7c3c602006-11-30 06:36:44 +00002738 }
2739 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
Reid Spencere7c3c602006-11-30 06:36:44 +00002740 }
Reid Spencer950bf602007-01-26 08:19:09 +00002741 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
2742 if ($5.C == 0)
2743 error("Global value initializer is not a constant");
Reid Spencerbb1fd572007-03-21 17:15:50 +00002744 CurGV = ParseGlobalVariable($2, $3, $4, $5.C->getType(), $5.C, $5.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002745 } GlobalVarAttributes {
2746 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002747 }
Reid Spencer950bf602007-01-26 08:19:09 +00002748 | ConstPool OptAssign EXTERNAL GlobalType Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00002749 const Type *Ty = $5.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00002750 CurGV = ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, Ty, 0,
2751 $5.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002752 delete $5.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002753 } GlobalVarAttributes {
2754 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002755 }
Reid Spencer950bf602007-01-26 08:19:09 +00002756 | ConstPool OptAssign DLLIMPORT GlobalType Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00002757 const Type *Ty = $5.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00002758 CurGV = ParseGlobalVariable($2, GlobalValue::DLLImportLinkage, $4, Ty, 0,
2759 $5.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002760 delete $5.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002761 } GlobalVarAttributes {
2762 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002763 }
Reid Spencer950bf602007-01-26 08:19:09 +00002764 | ConstPool OptAssign EXTERN_WEAK GlobalType Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00002765 const Type *Ty = $5.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00002766 CurGV =
Reid Spencerbb1fd572007-03-21 17:15:50 +00002767 ParseGlobalVariable($2, GlobalValue::ExternalWeakLinkage, $4, Ty, 0,
2768 $5.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002769 delete $5.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002770 } GlobalVarAttributes {
2771 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002772 }
2773 | ConstPool TARGET TargetDefinition {
Reid Spencere7c3c602006-11-30 06:36:44 +00002774 }
2775 | ConstPool DEPLIBS '=' LibrariesDefinition {
Reid Spencere7c3c602006-11-30 06:36:44 +00002776 }
2777 | /* empty: end of list */ {
Reid Spencer950bf602007-01-26 08:19:09 +00002778 }
2779 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002780
Reid Spencer950bf602007-01-26 08:19:09 +00002781AsmBlock
2782 : STRINGCONSTANT {
2783 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
2784 char *EndStr = UnEscapeLexed($1, true);
2785 std::string NewAsm($1, EndStr);
2786 free($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00002787
Reid Spencer950bf602007-01-26 08:19:09 +00002788 if (AsmSoFar.empty())
2789 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
2790 else
2791 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
2792 }
2793 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002794
Reid Spencer950bf602007-01-26 08:19:09 +00002795BigOrLittle
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00002796 : BIG { $$ = Module::BigEndian; }
Reid Spencer950bf602007-01-26 08:19:09 +00002797 | LITTLE { $$ = Module::LittleEndian; }
2798 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002799
2800TargetDefinition
2801 : ENDIAN '=' BigOrLittle {
Reid Spencer950bf602007-01-26 08:19:09 +00002802 CurModule.setEndianness($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00002803 }
2804 | POINTERSIZE '=' EUINT64VAL {
Reid Spencer950bf602007-01-26 08:19:09 +00002805 if ($3 == 32)
2806 CurModule.setPointerSize(Module::Pointer32);
2807 else if ($3 == 64)
2808 CurModule.setPointerSize(Module::Pointer64);
2809 else
2810 error("Invalid pointer size: '" + utostr($3) + "'");
Reid Spencere7c3c602006-11-30 06:36:44 +00002811 }
2812 | TRIPLE '=' STRINGCONSTANT {
Reid Spencer950bf602007-01-26 08:19:09 +00002813 CurModule.CurrentModule->setTargetTriple($3);
2814 free($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00002815 }
2816 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer950bf602007-01-26 08:19:09 +00002817 CurModule.CurrentModule->setDataLayout($3);
2818 free($3);
2819 }
2820 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002821
2822LibrariesDefinition
Reid Spencer950bf602007-01-26 08:19:09 +00002823 : '[' LibList ']'
2824 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002825
2826LibList
2827 : LibList ',' STRINGCONSTANT {
Reid Spencer950bf602007-01-26 08:19:09 +00002828 CurModule.CurrentModule->addLibrary($3);
2829 free($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00002830 }
Reid Spencer950bf602007-01-26 08:19:09 +00002831 | STRINGCONSTANT {
2832 CurModule.CurrentModule->addLibrary($1);
2833 free($1);
2834 }
2835 | /* empty: end of list */ { }
2836 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002837
2838//===----------------------------------------------------------------------===//
2839// Rules to match Function Headers
2840//===----------------------------------------------------------------------===//
2841
Reid Spencer950bf602007-01-26 08:19:09 +00002842Name
2843 : VAR_ID | STRINGCONSTANT
2844 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002845
Reid Spencer950bf602007-01-26 08:19:09 +00002846OptName
2847 : Name
2848 | /*empty*/ { $$ = 0; }
2849 ;
2850
2851ArgVal
2852 : Types OptName {
Reid Spencered96d1e2007-02-08 09:08:52 +00002853 if ($1.PAT->get() == Type::VoidTy)
Reid Spencer950bf602007-01-26 08:19:09 +00002854 error("void typed arguments are invalid");
2855 $$ = new std::pair<PATypeInfo, char*>($1, $2);
Reid Spencer52402b02007-01-02 05:45:11 +00002856 }
Reid Spencer950bf602007-01-26 08:19:09 +00002857 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002858
Reid Spencer950bf602007-01-26 08:19:09 +00002859ArgListH
2860 : ArgListH ',' ArgVal {
2861 $$ = $1;
2862 $$->push_back(*$3);
Reid Spencere77e35e2006-12-01 20:26:20 +00002863 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002864 }
2865 | ArgVal {
Reid Spencer950bf602007-01-26 08:19:09 +00002866 $$ = new std::vector<std::pair<PATypeInfo,char*> >();
2867 $$->push_back(*$1);
2868 delete $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00002869 }
Reid Spencer950bf602007-01-26 08:19:09 +00002870 ;
2871
2872ArgList
2873 : ArgListH { $$ = $1; }
Reid Spencere7c3c602006-11-30 06:36:44 +00002874 | ArgListH ',' DOTDOTDOT {
Reid Spencere7c3c602006-11-30 06:36:44 +00002875 $$ = $1;
Reid Spencer950bf602007-01-26 08:19:09 +00002876 PATypeInfo VoidTI;
Reid Spencered96d1e2007-02-08 09:08:52 +00002877 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002878 VoidTI.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002879 $$->push_back(std::pair<PATypeInfo, char*>(VoidTI, 0));
Reid Spencere7c3c602006-11-30 06:36:44 +00002880 }
2881 | DOTDOTDOT {
Reid Spencer950bf602007-01-26 08:19:09 +00002882 $$ = new std::vector<std::pair<PATypeInfo,char*> >();
2883 PATypeInfo VoidTI;
Reid Spencered96d1e2007-02-08 09:08:52 +00002884 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002885 VoidTI.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002886 $$->push_back(std::pair<PATypeInfo, char*>(VoidTI, 0));
Reid Spencere7c3c602006-11-30 06:36:44 +00002887 }
Reid Spencer950bf602007-01-26 08:19:09 +00002888 | /* empty */ { $$ = 0; }
2889 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002890
Reid Spencer71d2ec92006-12-31 06:02:26 +00002891FunctionHeaderH
2892 : OptCallingConv TypesV Name '(' ArgList ')' OptSection OptAlign {
Reid Spencer950bf602007-01-26 08:19:09 +00002893 UnEscapeLexed($3);
2894 std::string FunctionName($3);
2895 free($3); // Free strdup'd memory!
Reid Spencere7c3c602006-11-30 06:36:44 +00002896
Reid Spencered96d1e2007-02-08 09:08:52 +00002897 const Type* RetTy = $2.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00002898
2899 if (!RetTy->isFirstClassType() && RetTy != Type::VoidTy)
2900 error("LLVM functions cannot return aggregate types");
2901
Reid Spencerbb1fd572007-03-21 17:15:50 +00002902 Signedness FTySign;
2903 FTySign.makeComposite($2.S);
Reid Spenceref9b9a72007-02-05 20:47:22 +00002904 std::vector<const Type*> ParamTyList;
Reid Spencer950bf602007-01-26 08:19:09 +00002905
2906 // In LLVM 2.0 the signatures of three varargs intrinsics changed to take
2907 // i8*. We check here for those names and override the parameter list
2908 // types to ensure the prototype is correct.
2909 if (FunctionName == "llvm.va_start" || FunctionName == "llvm.va_end") {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002910 ParamTyList.push_back(PointerType::get(Type::Int8Ty));
Reid Spencer950bf602007-01-26 08:19:09 +00002911 } else if (FunctionName == "llvm.va_copy") {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002912 ParamTyList.push_back(PointerType::get(Type::Int8Ty));
2913 ParamTyList.push_back(PointerType::get(Type::Int8Ty));
Reid Spencer950bf602007-01-26 08:19:09 +00002914 } else if ($5) { // If there are arguments...
2915 for (std::vector<std::pair<PATypeInfo,char*> >::iterator
2916 I = $5->begin(), E = $5->end(); I != E; ++I) {
Reid Spencered96d1e2007-02-08 09:08:52 +00002917 const Type *Ty = I->first.PAT->get();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002918 ParamTyList.push_back(Ty);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002919 FTySign.add(I->first.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002920 }
2921 }
2922
Reid Spenceref9b9a72007-02-05 20:47:22 +00002923 bool isVarArg = ParamTyList.size() && ParamTyList.back() == Type::VoidTy;
2924 if (isVarArg)
2925 ParamTyList.pop_back();
Reid Spencer950bf602007-01-26 08:19:09 +00002926
Reid Spencerb7046c72007-01-29 05:41:34 +00002927 // Convert the CSRet calling convention into the corresponding parameter
2928 // attribute.
Reid Spencer7eea8ff2007-05-18 05:48:07 +00002929 ParamAttrsList *PAL = 0;
Reid Spencerb7046c72007-01-29 05:41:34 +00002930 if ($1 == OldCallingConv::CSRet) {
Reid Spencer7eea8ff2007-05-18 05:48:07 +00002931 ParamAttrsVector Attrs;
2932 ParamAttrsWithIndex PAWI;
2933 PAWI.index = 1; PAWI.attrs = ParamAttr::StructRet; // first arg
2934 Attrs.push_back(PAWI);
2935 PAL = ParamAttrsList::get(Attrs);
Reid Spencerb7046c72007-01-29 05:41:34 +00002936 }
2937
Reid Spencer7b5d4662007-04-09 06:16:21 +00002938 const FunctionType *FT =
Reid Spencer7eea8ff2007-05-18 05:48:07 +00002939 FunctionType::get(RetTy, ParamTyList, isVarArg, PAL);
Reid Spencer950bf602007-01-26 08:19:09 +00002940 const PointerType *PFT = PointerType::get(FT);
Reid Spencered96d1e2007-02-08 09:08:52 +00002941 delete $2.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002942
2943 ValID ID;
2944 if (!FunctionName.empty()) {
Reid Spencer5eb77c72007-03-15 03:26:42 +00002945 ID = ValID::create((char*)FunctionName.c_str());
Reid Spencer950bf602007-01-26 08:19:09 +00002946 } else {
Reid Spencer5eb77c72007-03-15 03:26:42 +00002947 ID = ValID::create((int)CurModule.Values[PFT].size());
Reid Spencer950bf602007-01-26 08:19:09 +00002948 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00002949 ID.S.makeComposite(FTySign);
Reid Spencer950bf602007-01-26 08:19:09 +00002950
2951 Function *Fn = 0;
Reid Spencered96d1e2007-02-08 09:08:52 +00002952 Module* M = CurModule.CurrentModule;
2953
Reid Spencer950bf602007-01-26 08:19:09 +00002954 // See if this function was forward referenced. If so, recycle the object.
2955 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2956 // Move the function to the end of the list, from whereever it was
2957 // previously inserted.
2958 Fn = cast<Function>(FWRef);
Reid Spencered96d1e2007-02-08 09:08:52 +00002959 M->getFunctionList().remove(Fn);
2960 M->getFunctionList().push_back(Fn);
2961 } else if (!FunctionName.empty()) {
2962 GlobalValue *Conflict = M->getFunction(FunctionName);
2963 if (!Conflict)
2964 Conflict = M->getNamedGlobal(FunctionName);
2965 if (Conflict && PFT == Conflict->getType()) {
2966 if (!CurFun.isDeclare && !Conflict->isDeclaration()) {
2967 // We have two function definitions that conflict, same type, same
2968 // name. We should really check to make sure that this is the result
2969 // of integer type planes collapsing and generate an error if it is
2970 // not, but we'll just rename on the assumption that it is. However,
2971 // let's do it intelligently and rename the internal linkage one
2972 // if there is one.
2973 std::string NewName(makeNameUnique(FunctionName));
2974 if (Conflict->hasInternalLinkage()) {
2975 Conflict->setName(NewName);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002976 RenameMapKey Key =
2977 makeRenameMapKey(FunctionName, Conflict->getType(), ID.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002978 CurModule.RenameMap[Key] = NewName;
2979 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
2980 InsertValue(Fn, CurModule.Values);
2981 } else {
2982 Fn = new Function(FT, CurFun.Linkage, NewName, M);
2983 InsertValue(Fn, CurModule.Values);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002984 RenameMapKey Key =
2985 makeRenameMapKey(FunctionName, PFT, ID.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002986 CurModule.RenameMap[Key] = NewName;
2987 }
2988 } else {
2989 // If they are not both definitions, then just use the function we
2990 // found since the types are the same.
2991 Fn = cast<Function>(Conflict);
Reid Spenceref9b9a72007-02-05 20:47:22 +00002992
Reid Spencered96d1e2007-02-08 09:08:52 +00002993 // Make sure to strip off any argument names so we can't get
2994 // conflicts.
2995 if (Fn->isDeclaration())
2996 for (Function::arg_iterator AI = Fn->arg_begin(),
2997 AE = Fn->arg_end(); AI != AE; ++AI)
2998 AI->setName("");
2999 }
3000 } else if (Conflict) {
Reid Spencere59f4932007-04-16 03:05:01 +00003001 // We have two globals with the same name and different types.
Reid Spencered96d1e2007-02-08 09:08:52 +00003002 // Previously, this was permitted because the symbol table had
3003 // "type planes" and names only needed to be distinct within a
3004 // type plane. After PR411 was fixed, this is no loner the case.
3005 // To resolve this we must rename one of the two.
3006 if (Conflict->hasInternalLinkage()) {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003007 // We can safely rename the Conflict.
3008 RenameMapKey Key =
3009 makeRenameMapKey(Conflict->getName(), Conflict->getType(),
3010 CurModule.NamedValueSigns[Conflict->getName()]);
Reid Spencered96d1e2007-02-08 09:08:52 +00003011 Conflict->setName(makeNameUnique(Conflict->getName()));
Reid Spencered96d1e2007-02-08 09:08:52 +00003012 CurModule.RenameMap[Key] = Conflict->getName();
3013 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
3014 InsertValue(Fn, CurModule.Values);
Reid Spencerd2920cd2007-03-21 17:27:53 +00003015 } else {
Reid Spencered96d1e2007-02-08 09:08:52 +00003016 // We can't quietly rename either of these things, but we must
Reid Spencerd2920cd2007-03-21 17:27:53 +00003017 // rename one of them. Only if the function's linkage is internal can
3018 // we forgo a warning message about the renamed function.
Reid Spencered96d1e2007-02-08 09:08:52 +00003019 std::string NewName = makeNameUnique(FunctionName);
Reid Spencerd2920cd2007-03-21 17:27:53 +00003020 if (CurFun.Linkage != GlobalValue::InternalLinkage) {
3021 warning("Renaming function '" + FunctionName + "' as '" + NewName +
3022 "' may cause linkage errors");
3023 }
3024 // Elect to rename the thing we're now defining.
Reid Spencered96d1e2007-02-08 09:08:52 +00003025 Fn = new Function(FT, CurFun.Linkage, NewName, M);
3026 InsertValue(Fn, CurModule.Values);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003027 RenameMapKey Key = makeRenameMapKey(FunctionName, PFT, ID.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003028 CurModule.RenameMap[Key] = NewName;
Reid Spencerd2920cd2007-03-21 17:27:53 +00003029 }
Reid Spenceref9b9a72007-02-05 20:47:22 +00003030 } else {
Reid Spencered96d1e2007-02-08 09:08:52 +00003031 // There's no conflict, just define the function
3032 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
3033 InsertValue(Fn, CurModule.Values);
Reid Spenceref9b9a72007-02-05 20:47:22 +00003034 }
Reid Spencere59f4932007-04-16 03:05:01 +00003035 } else {
3036 // There's no conflict, just define the function
3037 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
3038 InsertValue(Fn, CurModule.Values);
Reid Spencer950bf602007-01-26 08:19:09 +00003039 }
3040
Reid Spencere59f4932007-04-16 03:05:01 +00003041
Reid Spencer950bf602007-01-26 08:19:09 +00003042 CurFun.FunctionStart(Fn);
3043
3044 if (CurFun.isDeclare) {
3045 // If we have declaration, always overwrite linkage. This will allow us
3046 // to correctly handle cases, when pointer to function is passed as
3047 // argument to another function.
3048 Fn->setLinkage(CurFun.Linkage);
3049 }
Reid Spencerb7046c72007-01-29 05:41:34 +00003050 Fn->setCallingConv(upgradeCallingConv($1));
Reid Spencer950bf602007-01-26 08:19:09 +00003051 Fn->setAlignment($8);
3052 if ($7) {
3053 Fn->setSection($7);
3054 free($7);
3055 }
3056
3057 // Add all of the arguments we parsed to the function...
3058 if ($5) { // Is null if empty...
3059 if (isVarArg) { // Nuke the last entry
Reid Spencered96d1e2007-02-08 09:08:52 +00003060 assert($5->back().first.PAT->get() == Type::VoidTy &&
Reid Spencer950bf602007-01-26 08:19:09 +00003061 $5->back().second == 0 && "Not a varargs marker");
Reid Spencered96d1e2007-02-08 09:08:52 +00003062 delete $5->back().first.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003063 $5->pop_back(); // Delete the last entry
3064 }
3065 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00003066 Function::arg_iterator ArgEnd = Fn->arg_end();
3067 std::vector<std::pair<PATypeInfo,char*> >::iterator I = $5->begin();
3068 std::vector<std::pair<PATypeInfo,char*> >::iterator E = $5->end();
3069 for ( ; I != E && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencered96d1e2007-02-08 09:08:52 +00003070 delete I->first.PAT; // Delete the typeholder...
Reid Spencerbb1fd572007-03-21 17:15:50 +00003071 ValueInfo VI; VI.V = ArgIt; VI.S.copy(I->first.S);
3072 setValueName(VI, I->second); // Insert arg into symtab...
Reid Spencer950bf602007-01-26 08:19:09 +00003073 InsertValue(ArgIt);
3074 }
3075 delete $5; // We're now done with the argument list
3076 }
Reid Spencer7eea8ff2007-05-18 05:48:07 +00003077 lastCallingConv = OldCallingConv::C;
Reid Spencer950bf602007-01-26 08:19:09 +00003078 }
3079 ;
3080
3081BEGIN
3082 : BEGINTOK | '{' // Allow BEGIN or '{' to start a function
Jeff Cohenac2dca92007-01-21 19:30:52 +00003083 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003084
Reid Spencer6fd36ab2006-12-29 20:35:03 +00003085FunctionHeader
Reid Spencerd2920cd2007-03-21 17:27:53 +00003086 : OptLinkage { CurFun.Linkage = $1; } FunctionHeaderH BEGIN {
Reid Spencer950bf602007-01-26 08:19:09 +00003087 $$ = CurFun.CurrentFunction;
3088
3089 // Make sure that we keep track of the linkage type even if there was a
3090 // previous "declare".
3091 $$->setLinkage($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00003092 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00003093 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003094
Reid Spencer950bf602007-01-26 08:19:09 +00003095END
3096 : ENDTOK | '}' // Allow end of '}' to end a function
3097 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003098
Reid Spencer950bf602007-01-26 08:19:09 +00003099Function
3100 : BasicBlockList END {
3101 $$ = $1;
3102 };
Reid Spencere7c3c602006-11-30 06:36:44 +00003103
Reid Spencere77e35e2006-12-01 20:26:20 +00003104FnDeclareLinkage
Reid Spencered96d1e2007-02-08 09:08:52 +00003105 : /*default*/ { $$ = GlobalValue::ExternalLinkage; }
3106 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
3107 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencere7c3c602006-11-30 06:36:44 +00003108 ;
3109
3110FunctionProto
Reid Spencered96d1e2007-02-08 09:08:52 +00003111 : DECLARE { CurFun.isDeclare = true; }
3112 FnDeclareLinkage { CurFun.Linkage = $3; } FunctionHeaderH {
Reid Spencer950bf602007-01-26 08:19:09 +00003113 $$ = CurFun.CurrentFunction;
3114 CurFun.FunctionDone();
3115
3116 }
3117 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003118
3119//===----------------------------------------------------------------------===//
3120// Rules to match Basic Blocks
3121//===----------------------------------------------------------------------===//
3122
Reid Spencer950bf602007-01-26 08:19:09 +00003123OptSideEffect
3124 : /* empty */ { $$ = false; }
3125 | SIDEEFFECT { $$ = true; }
3126 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003127
Reid Spencere77e35e2006-12-01 20:26:20 +00003128ConstValueRef
Reid Spencer950bf602007-01-26 08:19:09 +00003129 // A reference to a direct constant
Reid Spencerbb1fd572007-03-21 17:15:50 +00003130 : ESINT64VAL { $$ = ValID::create($1); }
Reid Spencer950bf602007-01-26 08:19:09 +00003131 | EUINT64VAL { $$ = ValID::create($1); }
3132 | FPVAL { $$ = ValID::create($1); }
Reid Spencerbb1fd572007-03-21 17:15:50 +00003133 | TRUETOK {
3134 $$ = ValID::create(ConstantInt::get(Type::Int1Ty, true));
3135 $$.S.makeUnsigned();
3136 }
3137 | FALSETOK {
3138 $$ = ValID::create(ConstantInt::get(Type::Int1Ty, false));
3139 $$.S.makeUnsigned();
3140 }
Reid Spencer950bf602007-01-26 08:19:09 +00003141 | NULL_TOK { $$ = ValID::createNull(); }
3142 | UNDEF { $$ = ValID::createUndef(); }
3143 | ZEROINITIALIZER { $$ = ValID::createZeroInit(); }
3144 | '<' ConstVector '>' { // Nonempty unsized packed vector
3145 const Type *ETy = (*$2)[0].C->getType();
3146 int NumElements = $2->size();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003147 VectorType* pt = VectorType::get(ETy, NumElements);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003148 $$.S.makeComposite((*$2)[0].S);
3149 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(pt, $$.S));
Reid Spencer950bf602007-01-26 08:19:09 +00003150
3151 // Verify all elements are correct type!
3152 std::vector<Constant*> Elems;
3153 for (unsigned i = 0; i < $2->size(); i++) {
3154 Constant *C = (*$2)[i].C;
3155 const Type *CTy = C->getType();
3156 if (ETy != CTy)
3157 error("Element #" + utostr(i) + " is not of type '" +
3158 ETy->getDescription() +"' as required!\nIt is of type '" +
3159 CTy->getDescription() + "'");
3160 Elems.push_back(C);
Reid Spencere7c3c602006-11-30 06:36:44 +00003161 }
Reid Spencer5eb77c72007-03-15 03:26:42 +00003162 $$ = ValID::create(ConstantVector::get(pt, Elems));
Reid Spencer950bf602007-01-26 08:19:09 +00003163 delete PTy; delete $2;
3164 }
3165 | ConstExpr {
Reid Spencer5eb77c72007-03-15 03:26:42 +00003166 $$ = ValID::create($1.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003167 $$.S.copy($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003168 }
3169 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
3170 char *End = UnEscapeLexed($3, true);
3171 std::string AsmStr = std::string($3, End);
3172 End = UnEscapeLexed($5, true);
3173 std::string Constraints = std::string($5, End);
3174 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
3175 free($3);
3176 free($5);
3177 }
3178 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003179
Reid Spencerbb1fd572007-03-21 17:15:50 +00003180// SymbolicValueRef - Reference to one of two ways of symbolically refering to // another value.
Reid Spencer950bf602007-01-26 08:19:09 +00003181//
3182SymbolicValueRef
Reid Spencerbb1fd572007-03-21 17:15:50 +00003183 : INTVAL { $$ = ValID::create($1); $$.S.makeSignless(); }
3184 | Name { $$ = ValID::create($1); $$.S.makeSignless(); }
Reid Spencer950bf602007-01-26 08:19:09 +00003185 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003186
3187// ValueRef - A reference to a definition... either constant or symbolic
Reid Spencerf459d392006-12-02 16:19:52 +00003188ValueRef
Reid Spencer950bf602007-01-26 08:19:09 +00003189 : SymbolicValueRef | ConstValueRef
Reid Spencerf459d392006-12-02 16:19:52 +00003190 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003191
Reid Spencer950bf602007-01-26 08:19:09 +00003192
Reid Spencere7c3c602006-11-30 06:36:44 +00003193// ResolvedVal - a <type> <value> pair. This is used only in cases where the
3194// type immediately preceeds the value reference, and allows complex constant
3195// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Reid Spencer950bf602007-01-26 08:19:09 +00003196ResolvedVal
3197 : Types ValueRef {
Reid Spencered96d1e2007-02-08 09:08:52 +00003198 const Type *Ty = $1.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003199 $2.S.copy($1.S);
Reid Spencer5eb77c72007-03-15 03:26:42 +00003200 $$.V = getVal(Ty, $2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003201 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003202 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003203 }
Reid Spencer950bf602007-01-26 08:19:09 +00003204 ;
3205
3206BasicBlockList
3207 : BasicBlockList BasicBlock {
3208 $$ = $1;
3209 }
3210 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
3211 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00003212 };
3213
3214
3215// Basic blocks are terminated by branching instructions:
3216// br, br/cc, switch, ret
3217//
Reid Spencer950bf602007-01-26 08:19:09 +00003218BasicBlock
3219 : InstructionList OptAssign BBTerminatorInst {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003220 ValueInfo VI; VI.V = $3.TI; VI.S.copy($3.S);
3221 setValueName(VI, $2);
3222 InsertValue($3.TI);
3223 $1->getInstList().push_back($3.TI);
Reid Spencer950bf602007-01-26 08:19:09 +00003224 InsertValue($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00003225 $$ = $1;
3226 }
Reid Spencer950bf602007-01-26 08:19:09 +00003227 ;
3228
3229InstructionList
3230 : InstructionList Inst {
3231 if ($2.I)
3232 $1->getInstList().push_back($2.I);
3233 $$ = $1;
3234 }
3235 | /* empty */ {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003236 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++),true);
Reid Spencer950bf602007-01-26 08:19:09 +00003237 // Make sure to move the basic block to the correct location in the
3238 // function, instead of leaving it inserted wherever it was first
3239 // referenced.
3240 Function::BasicBlockListType &BBL =
3241 CurFun.CurrentFunction->getBasicBlockList();
3242 BBL.splice(BBL.end(), BBL, $$);
3243 }
3244 | LABELSTR {
Reid Spencer5eb77c72007-03-15 03:26:42 +00003245 $$ = CurBB = getBBVal(ValID::create($1), true);
Reid Spencer950bf602007-01-26 08:19:09 +00003246 // Make sure to move the basic block to the correct location in the
3247 // function, instead of leaving it inserted wherever it was first
3248 // referenced.
3249 Function::BasicBlockListType &BBL =
3250 CurFun.CurrentFunction->getBasicBlockList();
3251 BBL.splice(BBL.end(), BBL, $$);
3252 }
3253 ;
3254
3255Unwind : UNWIND | EXCEPT;
3256
3257BBTerminatorInst
3258 : RET ResolvedVal { // Return with a result...
Reid Spencerbb1fd572007-03-21 17:15:50 +00003259 $$.TI = new ReturnInst($2.V);
3260 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003261 }
3262 | RET VOID { // Return with no result...
Reid Spencerbb1fd572007-03-21 17:15:50 +00003263 $$.TI = new ReturnInst();
3264 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003265 }
3266 | BR LABEL ValueRef { // Unconditional Branch...
3267 BasicBlock* tmpBB = getBBVal($3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003268 $$.TI = new BranchInst(tmpBB);
3269 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003270 } // Conditional Branch...
3271 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003272 $6.S.makeSignless();
3273 $9.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003274 BasicBlock* tmpBBA = getBBVal($6);
3275 BasicBlock* tmpBBB = getBBVal($9);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003276 $3.S.makeUnsigned();
Reid Spencer950bf602007-01-26 08:19:09 +00003277 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003278 $$.TI = new BranchInst(tmpBBA, tmpBBB, tmpVal);
3279 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003280 }
3281 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003282 $3.S.copy($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003283 Value* tmpVal = getVal($2.T, $3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003284 $6.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003285 BasicBlock* tmpBB = getBBVal($6);
3286 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Reid Spencerbb1fd572007-03-21 17:15:50 +00003287 $$.TI = S;
3288 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003289 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
3290 E = $8->end();
3291 for (; I != E; ++I) {
3292 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
3293 S->addCase(CI, I->second);
3294 else
3295 error("Switch case is constant, but not a simple integer");
3296 }
3297 delete $8;
3298 }
3299 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003300 $3.S.copy($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003301 Value* tmpVal = getVal($2.T, $3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003302 $6.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003303 BasicBlock* tmpBB = getBBVal($6);
3304 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003305 $$.TI = S;
3306 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003307 }
3308 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
3309 TO LABEL ValueRef Unwind LABEL ValueRef {
3310 const PointerType *PFTy;
3311 const FunctionType *Ty;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003312 Signedness FTySign;
Reid Spencer950bf602007-01-26 08:19:09 +00003313
Reid Spencered96d1e2007-02-08 09:08:52 +00003314 if (!(PFTy = dyn_cast<PointerType>($3.PAT->get())) ||
Reid Spencer950bf602007-01-26 08:19:09 +00003315 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3316 // Pull out the types of all of the arguments...
3317 std::vector<const Type*> ParamTypes;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003318 FTySign.makeComposite($3.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003319 if ($6) {
3320 for (std::vector<ValueInfo>::iterator I = $6->begin(), E = $6->end();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003321 I != E; ++I) {
Reid Spencer950bf602007-01-26 08:19:09 +00003322 ParamTypes.push_back((*I).V->getType());
Reid Spencerbb1fd572007-03-21 17:15:50 +00003323 FTySign.add(I->S);
3324 }
Reid Spencer950bf602007-01-26 08:19:09 +00003325 }
Reid Spencer7eea8ff2007-05-18 05:48:07 +00003326 ParamAttrsList *PAL = 0;
Reid Spencerb7046c72007-01-29 05:41:34 +00003327 if ($2 == OldCallingConv::CSRet) {
Reid Spencer7eea8ff2007-05-18 05:48:07 +00003328 ParamAttrsVector Attrs;
3329 ParamAttrsWithIndex PAWI;
3330 PAWI.index = 1; PAWI.attrs = ParamAttr::StructRet; // first arg
3331 Attrs.push_back(PAWI);
3332 PAL = ParamAttrsList::get(Attrs);
Reid Spencerb7046c72007-01-29 05:41:34 +00003333 }
Reid Spencer950bf602007-01-26 08:19:09 +00003334 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
3335 if (isVarArg) ParamTypes.pop_back();
Reid Spencer7eea8ff2007-05-18 05:48:07 +00003336 Ty = FunctionType::get($3.PAT->get(), ParamTypes, isVarArg, PAL);
Reid Spencer950bf602007-01-26 08:19:09 +00003337 PFTy = PointerType::get(Ty);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003338 $$.S.copy($3.S);
3339 } else {
3340 FTySign = $3.S;
Reid Spencera3b12dd2007-04-07 16:14:01 +00003341 // Get the signedness of the result type. $3 is the pointer to the
3342 // function type so we get the 0th element to extract the function type,
3343 // and then the 0th element again to get the result type.
3344 $$.S.copy($3.S.get(0).get(0));
Reid Spencer950bf602007-01-26 08:19:09 +00003345 }
Reid Spencer7b5d4662007-04-09 06:16:21 +00003346
Reid Spencerbb1fd572007-03-21 17:15:50 +00003347 $4.S.makeComposite(FTySign);
Reid Spencer950bf602007-01-26 08:19:09 +00003348 Value *V = getVal(PFTy, $4); // Get the function we're calling...
3349 BasicBlock *Normal = getBBVal($10);
3350 BasicBlock *Except = getBBVal($13);
3351
3352 // Create the call node...
3353 if (!$6) { // Has no arguments?
David Greene5fd22a82007-09-04 18:46:50 +00003354 std::vector<Value*> Args;
3355 $$.TI = new InvokeInst(V, Normal, Except, Args.begin(), Args.end());
Reid Spencer950bf602007-01-26 08:19:09 +00003356 } else { // Has arguments?
3357 // Loop through FunctionType's arguments and ensure they are specified
3358 // correctly!
3359 //
3360 FunctionType::param_iterator I = Ty->param_begin();
3361 FunctionType::param_iterator E = Ty->param_end();
3362 std::vector<ValueInfo>::iterator ArgI = $6->begin(), ArgE = $6->end();
3363
3364 std::vector<Value*> Args;
3365 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
3366 if ((*ArgI).V->getType() != *I)
3367 error("Parameter " +(*ArgI).V->getName()+ " is not of type '" +
3368 (*I)->getDescription() + "'");
3369 Args.push_back((*ArgI).V);
3370 }
3371
3372 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
3373 error("Invalid number of parameters detected");
3374
David Greene5fd22a82007-09-04 18:46:50 +00003375 $$.TI = new InvokeInst(V, Normal, Except, Args.begin(), Args.end());
Reid Spencer950bf602007-01-26 08:19:09 +00003376 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00003377 cast<InvokeInst>($$.TI)->setCallingConv(upgradeCallingConv($2));
Reid Spencered96d1e2007-02-08 09:08:52 +00003378 delete $3.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003379 delete $6;
Reid Spencer7eea8ff2007-05-18 05:48:07 +00003380 lastCallingConv = OldCallingConv::C;
Reid Spencer950bf602007-01-26 08:19:09 +00003381 }
3382 | Unwind {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003383 $$.TI = new UnwindInst();
3384 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003385 }
3386 | UNREACHABLE {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003387 $$.TI = new UnreachableInst();
3388 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003389 }
3390 ;
3391
3392JumpTable
3393 : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
3394 $$ = $1;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003395 $3.S.copy($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003396 Constant *V = cast<Constant>(getExistingValue($2.T, $3));
3397
3398 if (V == 0)
3399 error("May only switch on a constant pool value");
3400
Reid Spencerbb1fd572007-03-21 17:15:50 +00003401 $6.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003402 BasicBlock* tmpBB = getBBVal($6);
3403 $$->push_back(std::make_pair(V, tmpBB));
3404 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003405 | IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencer950bf602007-01-26 08:19:09 +00003406 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003407 $2.S.copy($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003408 Constant *V = cast<Constant>(getExistingValue($1.T, $2));
3409
3410 if (V == 0)
3411 error("May only switch on a constant pool value");
3412
Reid Spencerbb1fd572007-03-21 17:15:50 +00003413 $5.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003414 BasicBlock* tmpBB = getBBVal($5);
3415 $$->push_back(std::make_pair(V, tmpBB));
3416 }
3417 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003418
3419Inst
3420 : OptAssign InstVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003421 bool omit = false;
3422 if ($1)
3423 if (BitCastInst *BCI = dyn_cast<BitCastInst>($2.I))
3424 if (BCI->getSrcTy() == BCI->getDestTy() &&
3425 BCI->getOperand(0)->getName() == $1)
3426 // This is a useless bit cast causing a name redefinition. It is
3427 // a bit cast from a type to the same type of an operand with the
3428 // same name as the name we would give this instruction. Since this
3429 // instruction results in no code generation, it is safe to omit
3430 // the instruction. This situation can occur because of collapsed
3431 // type planes. For example:
3432 // %X = add int %Y, %Z
3433 // %X = cast int %Y to uint
3434 // After upgrade, this looks like:
3435 // %X = add i32 %Y, %Z
3436 // %X = bitcast i32 to i32
3437 // The bitcast is clearly useless so we omit it.
3438 omit = true;
3439 if (omit) {
3440 $$.I = 0;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003441 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003442 } else {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003443 ValueInfo VI; VI.V = $2.I; VI.S.copy($2.S);
3444 setValueName(VI, $1);
Reid Spencer950bf602007-01-26 08:19:09 +00003445 InsertValue($2.I);
3446 $$ = $2;
Reid Spencerf5626a32007-01-01 01:20:41 +00003447 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003448 };
3449
Reid Spencer950bf602007-01-26 08:19:09 +00003450PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
3451 $$.P = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003452 $$.S.copy($1.S);
3453 $3.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003454 Value* tmpVal = getVal($1.PAT->get(), $3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003455 $5.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003456 BasicBlock* tmpBB = getBBVal($5);
3457 $$.P->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencered96d1e2007-02-08 09:08:52 +00003458 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003459 }
3460 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
Reid Spencere7c3c602006-11-30 06:36:44 +00003461 $$ = $1;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003462 $4.S.copy($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003463 Value* tmpVal = getVal($1.P->front().first->getType(), $4);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003464 $6.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003465 BasicBlock* tmpBB = getBBVal($6);
3466 $1.P->push_back(std::make_pair(tmpVal, tmpBB));
3467 }
3468 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003469
Reid Spencer950bf602007-01-26 08:19:09 +00003470ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
3471 $$ = new std::vector<ValueInfo>();
Reid Spencerf8483652006-12-02 15:16:01 +00003472 $$->push_back($1);
3473 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003474 | ValueRefList ',' ResolvedVal {
Reid Spencere7c3c602006-11-30 06:36:44 +00003475 $$ = $1;
Reid Spencer950bf602007-01-26 08:19:09 +00003476 $1->push_back($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00003477 };
3478
3479// ValueRefListE - Just like ValueRefList, except that it may also be empty!
3480ValueRefListE
Reid Spencer950bf602007-01-26 08:19:09 +00003481 : ValueRefList
3482 | /*empty*/ { $$ = 0; }
Reid Spencere7c3c602006-11-30 06:36:44 +00003483 ;
3484
3485OptTailCall
3486 : TAIL CALL {
Reid Spencer950bf602007-01-26 08:19:09 +00003487 $$ = true;
Reid Spencere7c3c602006-11-30 06:36:44 +00003488 }
Reid Spencer950bf602007-01-26 08:19:09 +00003489 | CALL {
3490 $$ = false;
3491 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003492 ;
3493
Reid Spencer950bf602007-01-26 08:19:09 +00003494InstVal
3495 : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003496 $3.S.copy($2.S);
3497 $5.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003498 const Type* Ty = $2.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003499 if (!Ty->isInteger() && !Ty->isFloatingPoint() && !isa<VectorType>(Ty))
Reid Spencer950bf602007-01-26 08:19:09 +00003500 error("Arithmetic operator requires integer, FP, or packed operands");
Reid Spencer9d6565a2007-02-15 02:26:10 +00003501 if (isa<VectorType>(Ty) &&
Reid Spencer950bf602007-01-26 08:19:09 +00003502 ($1 == URemOp || $1 == SRemOp || $1 == FRemOp || $1 == RemOp))
Chris Lattner4227bdb2007-02-19 07:34:02 +00003503 error("Remainder not supported on vector types");
Reid Spencer950bf602007-01-26 08:19:09 +00003504 // Upgrade the opcode from obsolete versions before we do anything with it.
3505 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $2.S);
3506 Value* val1 = getVal(Ty, $3);
3507 Value* val2 = getVal(Ty, $5);
3508 $$.I = BinaryOperator::create(Opcode, val1, val2);
3509 if ($$.I == 0)
3510 error("binary operator returned null");
Reid Spencerbb1fd572007-03-21 17:15:50 +00003511 $$.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003512 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003513 }
3514 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003515 $3.S.copy($2.S);
3516 $5.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003517 const Type *Ty = $2.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003518 if (!Ty->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00003519 if (!isa<VectorType>(Ty) ||
3520 !cast<VectorType>(Ty)->getElementType()->isInteger())
Reid Spencer950bf602007-01-26 08:19:09 +00003521 error("Logical operator requires integral operands");
3522 }
3523 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $2.S);
3524 Value* tmpVal1 = getVal(Ty, $3);
3525 Value* tmpVal2 = getVal(Ty, $5);
3526 $$.I = BinaryOperator::create(Opcode, tmpVal1, tmpVal2);
3527 if ($$.I == 0)
3528 error("binary operator returned null");
Reid Spencerbb1fd572007-03-21 17:15:50 +00003529 $$.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003530 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003531 }
3532 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003533 $3.S.copy($2.S);
3534 $5.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003535 const Type* Ty = $2.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003536 if(isa<VectorType>(Ty))
3537 error("VectorTypes currently not supported in setcc instructions");
Reid Spencer950bf602007-01-26 08:19:09 +00003538 unsigned short pred;
3539 Instruction::OtherOps Opcode = getCompareOp($1, pred, Ty, $2.S);
3540 Value* tmpVal1 = getVal(Ty, $3);
3541 Value* tmpVal2 = getVal(Ty, $5);
3542 $$.I = CmpInst::create(Opcode, pred, tmpVal1, tmpVal2);
3543 if ($$.I == 0)
3544 error("binary operator returned null");
Reid Spencerbb1fd572007-03-21 17:15:50 +00003545 $$.S.makeUnsigned();
Reid Spencered96d1e2007-02-08 09:08:52 +00003546 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003547 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00003548 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003549 $4.S.copy($3.S);
3550 $6.S.copy($3.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003551 const Type *Ty = $3.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003552 if (isa<VectorType>(Ty))
3553 error("VectorTypes currently not supported in icmp instructions");
Reid Spencer950bf602007-01-26 08:19:09 +00003554 else if (!Ty->isInteger() && !isa<PointerType>(Ty))
3555 error("icmp requires integer or pointer typed operands");
3556 Value* tmpVal1 = getVal(Ty, $4);
3557 Value* tmpVal2 = getVal(Ty, $6);
3558 $$.I = new ICmpInst($2, tmpVal1, tmpVal2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003559 $$.S.makeUnsigned();
Reid Spencered96d1e2007-02-08 09:08:52 +00003560 delete $3.PAT;
Reid Spencer57f28f92006-12-03 07:10:26 +00003561 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00003562 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003563 $4.S.copy($3.S);
3564 $6.S.copy($3.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003565 const Type *Ty = $3.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003566 if (isa<VectorType>(Ty))
3567 error("VectorTypes currently not supported in fcmp instructions");
Reid Spencer950bf602007-01-26 08:19:09 +00003568 else if (!Ty->isFloatingPoint())
3569 error("fcmp instruction requires floating point operands");
3570 Value* tmpVal1 = getVal(Ty, $4);
3571 Value* tmpVal2 = getVal(Ty, $6);
3572 $$.I = new FCmpInst($2, tmpVal1, tmpVal2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003573 $$.S.makeUnsigned();
Reid Spencered96d1e2007-02-08 09:08:52 +00003574 delete $3.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003575 }
3576 | NOT ResolvedVal {
3577 warning("Use of obsolete 'not' instruction: Replacing with 'xor");
3578 const Type *Ty = $2.V->getType();
3579 Value *Ones = ConstantInt::getAllOnesValue(Ty);
3580 if (Ones == 0)
3581 error("Expected integral type for not instruction");
3582 $$.I = BinaryOperator::create(Instruction::Xor, $2.V, Ones);
3583 if ($$.I == 0)
3584 error("Could not create a xor instruction");
Reid Spencerbb1fd572007-03-21 17:15:50 +00003585 $$.S.copy($2.S);
Reid Spencer229e9362006-12-02 22:14:11 +00003586 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003587 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003588 if (!$4.V->getType()->isInteger() ||
3589 cast<IntegerType>($4.V->getType())->getBitWidth() != 8)
3590 error("Shift amount must be int8");
Reid Spencer832254e2007-02-02 02:16:23 +00003591 const Type* Ty = $2.V->getType();
3592 if (!Ty->isInteger())
Reid Spencer950bf602007-01-26 08:19:09 +00003593 error("Shift constant expression requires integer operand");
Reid Spencer832254e2007-02-02 02:16:23 +00003594 Value* ShiftAmt = 0;
3595 if (cast<IntegerType>(Ty)->getBitWidth() > Type::Int8Ty->getBitWidth())
3596 if (Constant *C = dyn_cast<Constant>($4.V))
3597 ShiftAmt = ConstantExpr::getZExt(C, Ty);
3598 else
3599 ShiftAmt = new ZExtInst($4.V, Ty, makeNameUnique("shift"), CurBB);
3600 else
3601 ShiftAmt = $4.V;
3602 $$.I = BinaryOperator::create(getBinaryOp($1, Ty, $2.S), $2.V, ShiftAmt);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003603 $$.S.copy($2.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00003604 }
Reid Spencerfcb5df82006-12-01 22:34:43 +00003605 | CastOps ResolvedVal TO Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00003606 const Type *DstTy = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003607 if (!DstTy->isFirstClassType())
3608 error("cast instruction to a non-primitive type: '" +
3609 DstTy->getDescription() + "'");
3610 $$.I = cast<Instruction>(getCast($1, $2.V, $2.S, DstTy, $4.S, true));
Reid Spencerbb1fd572007-03-21 17:15:50 +00003611 $$.S.copy($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003612 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003613 }
3614 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003615 if (!$2.V->getType()->isInteger() ||
3616 cast<IntegerType>($2.V->getType())->getBitWidth() != 1)
3617 error("select condition must be bool");
3618 if ($4.V->getType() != $6.V->getType())
3619 error("select value types should match");
3620 $$.I = new SelectInst($2.V, $4.V, $6.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003621 $$.S.copy($4.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00003622 }
3623 | VAARG ResolvedVal ',' Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00003624 const Type *Ty = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003625 NewVarArgs = true;
3626 $$.I = new VAArgInst($2.V, Ty);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003627 $$.S.copy($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003628 delete $4.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003629 }
3630 | VAARG_old ResolvedVal ',' Types {
3631 const Type* ArgTy = $2.V->getType();
Reid Spencered96d1e2007-02-08 09:08:52 +00003632 const Type* DstTy = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003633 ObsoleteVarArgs = true;
3634 Function* NF = cast<Function>(CurModule.CurrentModule->
3635 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0));
3636
3637 //b = vaarg a, t ->
3638 //foo = alloca 1 of t
3639 //bar = vacopy a
3640 //store bar -> foo
3641 //b = vaarg foo, t
3642 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix");
3643 CurBB->getInstList().push_back(foo);
3644 CallInst* bar = new CallInst(NF, $2.V);
3645 CurBB->getInstList().push_back(bar);
3646 CurBB->getInstList().push_back(new StoreInst(bar, foo));
3647 $$.I = new VAArgInst(foo, DstTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003648 $$.S.copy($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003649 delete $4.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003650 }
3651 | VANEXT_old ResolvedVal ',' Types {
3652 const Type* ArgTy = $2.V->getType();
Reid Spencered96d1e2007-02-08 09:08:52 +00003653 const Type* DstTy = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003654 ObsoleteVarArgs = true;
3655 Function* NF = cast<Function>(CurModule.CurrentModule->
3656 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0));
3657
3658 //b = vanext a, t ->
3659 //foo = alloca 1 of t
3660 //bar = vacopy a
3661 //store bar -> foo
3662 //tmp = vaarg foo, t
3663 //b = load foo
3664 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix");
3665 CurBB->getInstList().push_back(foo);
3666 CallInst* bar = new CallInst(NF, $2.V);
3667 CurBB->getInstList().push_back(bar);
3668 CurBB->getInstList().push_back(new StoreInst(bar, foo));
3669 Instruction* tmp = new VAArgInst(foo, DstTy);
3670 CurBB->getInstList().push_back(tmp);
3671 $$.I = new LoadInst(foo);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003672 $$.S.copy($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003673 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003674 }
3675 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003676 if (!ExtractElementInst::isValidOperands($2.V, $4.V))
3677 error("Invalid extractelement operands");
3678 $$.I = new ExtractElementInst($2.V, $4.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003679 $$.S.copy($2.S.get(0));
Reid Spencere7c3c602006-11-30 06:36:44 +00003680 }
3681 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003682 if (!InsertElementInst::isValidOperands($2.V, $4.V, $6.V))
3683 error("Invalid insertelement operands");
3684 $$.I = new InsertElementInst($2.V, $4.V, $6.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003685 $$.S.copy($2.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00003686 }
3687 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003688 if (!ShuffleVectorInst::isValidOperands($2.V, $4.V, $6.V))
3689 error("Invalid shufflevector operands");
3690 $$.I = new ShuffleVectorInst($2.V, $4.V, $6.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003691 $$.S.copy($2.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00003692 }
3693 | PHI_TOK PHIList {
Reid Spencer950bf602007-01-26 08:19:09 +00003694 const Type *Ty = $2.P->front().first->getType();
3695 if (!Ty->isFirstClassType())
3696 error("PHI node operands must be of first class type");
3697 PHINode *PHI = new PHINode(Ty);
3698 PHI->reserveOperandSpace($2.P->size());
3699 while ($2.P->begin() != $2.P->end()) {
3700 if ($2.P->front().first->getType() != Ty)
3701 error("All elements of a PHI node must be of the same type");
3702 PHI->addIncoming($2.P->front().first, $2.P->front().second);
3703 $2.P->pop_front();
3704 }
3705 $$.I = PHI;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003706 $$.S.copy($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003707 delete $2.P; // Free the list...
Reid Spencere7c3c602006-11-30 06:36:44 +00003708 }
Reid Spencer7b5d4662007-04-09 06:16:21 +00003709 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00003710 // Handle the short call syntax
3711 const PointerType *PFTy;
3712 const FunctionType *FTy;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003713 Signedness FTySign;
Reid Spencered96d1e2007-02-08 09:08:52 +00003714 if (!(PFTy = dyn_cast<PointerType>($3.PAT->get())) ||
Reid Spencer950bf602007-01-26 08:19:09 +00003715 !(FTy = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3716 // Pull out the types of all of the arguments...
3717 std::vector<const Type*> ParamTypes;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003718 FTySign.makeComposite($3.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003719 if ($6) {
3720 for (std::vector<ValueInfo>::iterator I = $6->begin(), E = $6->end();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003721 I != E; ++I) {
Reid Spencer950bf602007-01-26 08:19:09 +00003722 ParamTypes.push_back((*I).V->getType());
Reid Spencerbb1fd572007-03-21 17:15:50 +00003723 FTySign.add(I->S);
3724 }
Reid Spencerc4d96252007-01-13 00:03:30 +00003725 }
Reid Spencer950bf602007-01-26 08:19:09 +00003726
3727 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
3728 if (isVarArg) ParamTypes.pop_back();
3729
Reid Spencered96d1e2007-02-08 09:08:52 +00003730 const Type *RetTy = $3.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003731 if (!RetTy->isFirstClassType() && RetTy != Type::VoidTy)
3732 error("Functions cannot return aggregate types");
3733
Reid Spencer7b5d4662007-04-09 06:16:21 +00003734 // Deal with CSRetCC
Reid Spencer7eea8ff2007-05-18 05:48:07 +00003735 ParamAttrsList *PAL = 0;
Reid Spencer7b5d4662007-04-09 06:16:21 +00003736 if ($2 == OldCallingConv::CSRet) {
Reid Spencer7eea8ff2007-05-18 05:48:07 +00003737 ParamAttrsVector Attrs;
3738 ParamAttrsWithIndex PAWI;
3739 PAWI.index = 1; PAWI.attrs = ParamAttr::StructRet; // first arg
3740 Attrs.push_back(PAWI);
3741 PAL = ParamAttrsList::get(Attrs);
Reid Spencer7b5d4662007-04-09 06:16:21 +00003742 }
3743
Reid Spencer7eea8ff2007-05-18 05:48:07 +00003744 FTy = FunctionType::get(RetTy, ParamTypes, isVarArg, PAL);
Reid Spencer950bf602007-01-26 08:19:09 +00003745 PFTy = PointerType::get(FTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003746 $$.S.copy($3.S);
3747 } else {
3748 FTySign = $3.S;
Reid Spencera3b12dd2007-04-07 16:14:01 +00003749 // Get the signedness of the result type. $3 is the pointer to the
3750 // function type so we get the 0th element to extract the function type,
3751 // and then the 0th element again to get the result type.
3752 $$.S.copy($3.S.get(0).get(0));
Reid Spencerf8483652006-12-02 15:16:01 +00003753 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00003754 $4.S.makeComposite(FTySign);
Reid Spencer950bf602007-01-26 08:19:09 +00003755
3756 // First upgrade any intrinsic calls.
3757 std::vector<Value*> Args;
3758 if ($6)
3759 for (unsigned i = 0, e = $6->size(); i < e; ++i)
3760 Args.push_back((*$6)[i].V);
Reid Spencer41b213e2007-04-02 01:14:00 +00003761 Instruction *Inst = upgradeIntrinsicCall(FTy->getReturnType(), $4, Args);
Reid Spencer950bf602007-01-26 08:19:09 +00003762
3763 // If we got an upgraded intrinsic
3764 if (Inst) {
3765 $$.I = Inst;
Reid Spencer950bf602007-01-26 08:19:09 +00003766 } else {
3767 // Get the function we're calling
3768 Value *V = getVal(PFTy, $4);
3769
3770 // Check the argument values match
3771 if (!$6) { // Has no arguments?
3772 // Make sure no arguments is a good thing!
3773 if (FTy->getNumParams() != 0)
3774 error("No arguments passed to a function that expects arguments");
3775 } else { // Has arguments?
3776 // Loop through FunctionType's arguments and ensure they are specified
3777 // correctly!
3778 //
3779 FunctionType::param_iterator I = FTy->param_begin();
3780 FunctionType::param_iterator E = FTy->param_end();
3781 std::vector<ValueInfo>::iterator ArgI = $6->begin(), ArgE = $6->end();
3782
3783 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
3784 if ((*ArgI).V->getType() != *I)
3785 error("Parameter " +(*ArgI).V->getName()+ " is not of type '" +
3786 (*I)->getDescription() + "'");
3787
3788 if (I != E || (ArgI != ArgE && !FTy->isVarArg()))
3789 error("Invalid number of parameters detected");
3790 }
3791
3792 // Create the call instruction
David Greene718fda32007-08-01 03:59:32 +00003793 CallInst *CI = new CallInst(V, Args.begin(), Args.end());
Reid Spencer950bf602007-01-26 08:19:09 +00003794 CI->setTailCall($1);
Reid Spencerb7046c72007-01-29 05:41:34 +00003795 CI->setCallingConv(upgradeCallingConv($2));
Reid Spencer950bf602007-01-26 08:19:09 +00003796 $$.I = CI;
Reid Spencer950bf602007-01-26 08:19:09 +00003797 }
Reid Spencered96d1e2007-02-08 09:08:52 +00003798 delete $3.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003799 delete $6;
Reid Spencer7eea8ff2007-05-18 05:48:07 +00003800 lastCallingConv = OldCallingConv::C;
Reid Spencere7c3c602006-11-30 06:36:44 +00003801 }
Reid Spencer950bf602007-01-26 08:19:09 +00003802 | MemoryInst {
3803 $$ = $1;
3804 }
3805 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003806
3807
3808// IndexList - List of indices for GEP based instructions...
3809IndexList
Reid Spencer950bf602007-01-26 08:19:09 +00003810 : ',' ValueRefList { $$ = $2; }
3811 | /* empty */ { $$ = new std::vector<ValueInfo>(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00003812 ;
3813
3814OptVolatile
Reid Spencer950bf602007-01-26 08:19:09 +00003815 : VOLATILE { $$ = true; }
3816 | /* empty */ { $$ = false; }
Reid Spencere7c3c602006-11-30 06:36:44 +00003817 ;
3818
Reid Spencer950bf602007-01-26 08:19:09 +00003819MemoryInst
3820 : MALLOC Types OptCAlign {
Reid Spencered96d1e2007-02-08 09:08:52 +00003821 const Type *Ty = $2.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003822 $$.S.makeComposite($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003823 $$.I = new MallocInst(Ty, 0, $3);
Reid Spencered96d1e2007-02-08 09:08:52 +00003824 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003825 }
3826 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencered96d1e2007-02-08 09:08:52 +00003827 const Type *Ty = $2.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003828 $5.S.makeUnsigned();
3829 $$.S.makeComposite($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003830 $$.I = new MallocInst(Ty, getVal($4.T, $5), $6);
Reid Spencered96d1e2007-02-08 09:08:52 +00003831 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003832 }
3833 | ALLOCA Types OptCAlign {
Reid Spencered96d1e2007-02-08 09:08:52 +00003834 const Type *Ty = $2.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003835 $$.S.makeComposite($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003836 $$.I = new AllocaInst(Ty, 0, $3);
Reid Spencered96d1e2007-02-08 09:08:52 +00003837 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003838 }
3839 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencered96d1e2007-02-08 09:08:52 +00003840 const Type *Ty = $2.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003841 $5.S.makeUnsigned();
3842 $$.S.makeComposite($4.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003843 $$.I = new AllocaInst(Ty, getVal($4.T, $5), $6);
Reid Spencered96d1e2007-02-08 09:08:52 +00003844 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003845 }
3846 | FREE ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003847 const Type *PTy = $2.V->getType();
3848 if (!isa<PointerType>(PTy))
3849 error("Trying to free nonpointer type '" + PTy->getDescription() + "'");
3850 $$.I = new FreeInst($2.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003851 $$.S.makeSignless();
Reid Spencere7c3c602006-11-30 06:36:44 +00003852 }
3853 | OptVolatile LOAD Types ValueRef {
Reid Spencered96d1e2007-02-08 09:08:52 +00003854 const Type* Ty = $3.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003855 $4.S.copy($3.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003856 if (!isa<PointerType>(Ty))
3857 error("Can't load from nonpointer type: " + Ty->getDescription());
3858 if (!cast<PointerType>(Ty)->getElementType()->isFirstClassType())
3859 error("Can't load from pointer of non-first-class type: " +
3860 Ty->getDescription());
3861 Value* tmpVal = getVal(Ty, $4);
3862 $$.I = new LoadInst(tmpVal, "", $1);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003863 $$.S.copy($3.S.get(0));
Reid Spencered96d1e2007-02-08 09:08:52 +00003864 delete $3.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003865 }
3866 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003867 $6.S.copy($5.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003868 const PointerType *PTy = dyn_cast<PointerType>($5.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00003869 if (!PTy)
3870 error("Can't store to a nonpointer type: " +
Reid Spencered96d1e2007-02-08 09:08:52 +00003871 $5.PAT->get()->getDescription());
Reid Spencer950bf602007-01-26 08:19:09 +00003872 const Type *ElTy = PTy->getElementType();
Reid Spencered96d1e2007-02-08 09:08:52 +00003873 Value *StoreVal = $3.V;
Reid Spencer950bf602007-01-26 08:19:09 +00003874 Value* tmpVal = getVal(PTy, $6);
Reid Spencered96d1e2007-02-08 09:08:52 +00003875 if (ElTy != $3.V->getType()) {
3876 StoreVal = handleSRetFuncTypeMerge($3.V, ElTy);
3877 if (!StoreVal)
3878 error("Can't store '" + $3.V->getType()->getDescription() +
3879 "' into space of type '" + ElTy->getDescription() + "'");
3880 else {
3881 PTy = PointerType::get(StoreVal->getType());
3882 if (Constant *C = dyn_cast<Constant>(tmpVal))
3883 tmpVal = ConstantExpr::getBitCast(C, PTy);
3884 else
3885 tmpVal = new BitCastInst(tmpVal, PTy, "upgrd.cast", CurBB);
3886 }
3887 }
3888 $$.I = new StoreInst(StoreVal, tmpVal, $1);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003889 $$.S.makeSignless();
Reid Spencered96d1e2007-02-08 09:08:52 +00003890 delete $5.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003891 }
3892 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003893 $3.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003894 const Type* Ty = $2.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003895 if (!isa<PointerType>(Ty))
3896 error("getelementptr insn requires pointer operand");
3897
3898 std::vector<Value*> VIndices;
Reid Spencerff0e4482007-04-16 00:40:57 +00003899 upgradeGEPInstIndices(Ty, $4, VIndices);
Reid Spencer950bf602007-01-26 08:19:09 +00003900
3901 Value* tmpVal = getVal(Ty, $3);
David Greene5fd22a82007-09-04 18:46:50 +00003902 $$.I = new GetElementPtrInst(tmpVal, VIndices.begin(), VIndices.end());
Reid Spencerbb1fd572007-03-21 17:15:50 +00003903 ValueInfo VI; VI.V = tmpVal; VI.S.copy($2.S);
3904 $$.S.copy(getElementSign(VI, VIndices));
Reid Spencered96d1e2007-02-08 09:08:52 +00003905 delete $2.PAT;
Reid Spencer30d0c582007-01-15 00:26:18 +00003906 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00003907 };
3908
Reid Spencer950bf602007-01-26 08:19:09 +00003909
Reid Spencere7c3c602006-11-30 06:36:44 +00003910%%
3911
3912int yyerror(const char *ErrorMsg) {
3913 std::string where
3914 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Reid Spencered96d1e2007-02-08 09:08:52 +00003915 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
Reid Spencer950bf602007-01-26 08:19:09 +00003916 std::string errMsg = where + "error: " + std::string(ErrorMsg);
3917 if (yychar != YYEMPTY && yychar != 0)
3918 errMsg += " while reading token '" + std::string(Upgradetext, Upgradeleng) +
3919 "'.";
Reid Spencer71d2ec92006-12-31 06:02:26 +00003920 std::cerr << "llvm-upgrade: " << errMsg << '\n';
Reid Spencer950bf602007-01-26 08:19:09 +00003921 std::cout << "llvm-upgrade: parse failed.\n";
Reid Spencere7c3c602006-11-30 06:36:44 +00003922 exit(1);
3923}
Reid Spencer319a7302007-01-05 17:20:02 +00003924
Reid Spencer30d0c582007-01-15 00:26:18 +00003925void warning(const std::string& ErrorMsg) {
Reid Spencer319a7302007-01-05 17:20:02 +00003926 std::string where
3927 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Reid Spencered96d1e2007-02-08 09:08:52 +00003928 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
Reid Spencer950bf602007-01-26 08:19:09 +00003929 std::string errMsg = where + "warning: " + std::string(ErrorMsg);
3930 if (yychar != YYEMPTY && yychar != 0)
3931 errMsg += " while reading token '" + std::string(Upgradetext, Upgradeleng) +
3932 "'.";
Reid Spencer319a7302007-01-05 17:20:02 +00003933 std::cerr << "llvm-upgrade: " << errMsg << '\n';
3934}
Reid Spencer950bf602007-01-26 08:19:09 +00003935
3936void error(const std::string& ErrorMsg, int LineNo) {
3937 if (LineNo == -1) LineNo = Upgradelineno;
3938 Upgradelineno = LineNo;
3939 yyerror(ErrorMsg.c_str());
3940}
3941