blob: ed84267d08a544a3c375283ae241c65c75048a5f [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?
536 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
537 error("FP constant invalid for type");
538 return ConstantFP::get(Ty, D.ConstPoolFP);
539
540 case ValID::ConstNullVal: // Is it a null value?
541 if (!isa<PointerType>(Ty))
542 error("Cannot create a a non pointer null");
543 return ConstantPointerNull::get(cast<PointerType>(Ty));
544
545 case ValID::ConstUndefVal: // Is it an undef value?
546 return UndefValue::get(Ty);
547
548 case ValID::ConstZeroVal: // Is it a zero value?
549 return Constant::getNullValue(Ty);
550
551 case ValID::ConstantVal: // Fully resolved constant?
552 if (D.ConstantValue->getType() != Ty)
553 error("Constant expression type different from required type");
554 return D.ConstantValue;
555
556 case ValID::InlineAsmVal: { // Inline asm expression
557 const PointerType *PTy = dyn_cast<PointerType>(Ty);
558 const FunctionType *FTy =
559 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
560 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints))
561 error("Invalid type for asm constraint string");
562 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
563 D.IAD->HasSideEffects);
564 D.destroy(); // Free InlineAsmDescriptor.
565 return IA;
566 }
567 default:
568 assert(0 && "Unhandled case");
569 return 0;
570 } // End of switch
571
572 assert(0 && "Unhandled case");
573 return 0;
574}
575
576// getVal - This function is identical to getExistingValue, except that if a
577// value is not already defined, it "improvises" by creating a placeholder var
578// that looks and acts just like the requested variable. When the value is
579// defined later, all uses of the placeholder variable are replaced with the
580// real thing.
581//
582static Value *getVal(const Type *Ty, const ValID &ID) {
583 if (Ty == Type::LabelTy)
584 error("Cannot use a basic block here");
585
586 // See if the value has already been defined.
587 Value *V = getExistingValue(Ty, ID);
588 if (V) return V;
589
590 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty))
591 error("Invalid use of a composite type");
592
593 // If we reached here, we referenced either a symbol that we don't know about
594 // or an id number that hasn't been read yet. We may be referencing something
595 // forward, so just create an entry to be resolved later and get to it...
Reid Spencer950bf602007-01-26 08:19:09 +0000596 V = new Argument(Ty);
597
598 // Remember where this forward reference came from. FIXME, shouldn't we try
599 // to recycle these things??
600 CurModule.PlaceHolderInfo.insert(
Reid Spenceref9b9a72007-02-05 20:47:22 +0000601 std::make_pair(V, std::make_pair(ID, Upgradelineno)));
Reid Spencer950bf602007-01-26 08:19:09 +0000602
603 if (inFunctionScope())
604 InsertValue(V, CurFun.LateResolveValues);
605 else
606 InsertValue(V, CurModule.LateResolveValues);
607 return V;
608}
609
Reid Spencered96d1e2007-02-08 09:08:52 +0000610/// @brief This just makes any name given to it unique, up to MAX_UINT times.
611static std::string makeNameUnique(const std::string& Name) {
612 static unsigned UniqueNameCounter = 1;
613 std::string Result(Name);
614 Result += ".upgrd." + llvm::utostr(UniqueNameCounter++);
615 return Result;
616}
617
Reid Spencer950bf602007-01-26 08:19:09 +0000618/// getBBVal - This is used for two purposes:
619/// * If isDefinition is true, a new basic block with the specified ID is being
620/// defined.
621/// * If isDefinition is true, this is a reference to a basic block, which may
622/// or may not be a forward reference.
623///
624static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
625 assert(inFunctionScope() && "Can't get basic block at global scope");
626
627 std::string Name;
628 BasicBlock *BB = 0;
629 switch (ID.Type) {
630 default:
631 error("Illegal label reference " + ID.getName());
632 break;
633 case ValID::NumberVal: // Is it a numbered definition?
634 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
635 CurFun.NumberedBlocks.resize(ID.Num+1);
636 BB = CurFun.NumberedBlocks[ID.Num];
637 break;
638 case ValID::NameVal: // Is it a named definition?
639 Name = ID.Name;
Reid Spencerbb1fd572007-03-21 17:15:50 +0000640 if (Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name)) {
Reid Spencered96d1e2007-02-08 09:08:52 +0000641 if (N->getType() != Type::LabelTy) {
642 // Register names didn't use to conflict with basic block names
643 // because of type planes. Now they all have to be unique. So, we just
644 // rename the register and treat this name as if no basic block
645 // had been found.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000646 RenameMapKey Key = makeRenameMapKey(ID.Name, N->getType(), ID.S);
Reid Spencered96d1e2007-02-08 09:08:52 +0000647 N->setName(makeNameUnique(N->getName()));
648 CurModule.RenameMap[Key] = N->getName();
649 BB = 0;
650 } else {
651 BB = cast<BasicBlock>(N);
652 }
Reid Spencer950bf602007-01-26 08:19:09 +0000653 }
654 break;
655 }
656
657 // See if the block has already been defined.
658 if (BB) {
659 // If this is the definition of the block, make sure the existing value was
660 // just a forward reference. If it was a forward reference, there will be
661 // an entry for it in the PlaceHolderInfo map.
662 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
663 // The existing value was a definition, not a forward reference.
664 error("Redefinition of label " + ID.getName());
665
666 ID.destroy(); // Free strdup'd memory.
667 return BB;
668 }
669
670 // Otherwise this block has not been seen before.
671 BB = new BasicBlock("", CurFun.CurrentFunction);
672 if (ID.Type == ValID::NameVal) {
673 BB->setName(ID.Name);
674 } else {
675 CurFun.NumberedBlocks[ID.Num] = BB;
676 }
677
678 // If this is not a definition, keep track of it so we can use it as a forward
679 // reference.
680 if (!isDefinition) {
681 // Remember where this forward reference came from.
682 CurFun.BBForwardRefs[BB] = std::make_pair(ID, Upgradelineno);
683 } else {
684 // The forward declaration could have been inserted anywhere in the
685 // function: insert it into the correct place now.
686 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
687 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
688 }
689 ID.destroy();
690 return BB;
691}
692
693
694//===----------------------------------------------------------------------===//
695// Code to handle forward references in instructions
696//===----------------------------------------------------------------------===//
697//
698// This code handles the late binding needed with statements that reference
699// values not defined yet... for example, a forward branch, or the PHI node for
700// a loop body.
701//
702// This keeps a table (CurFun.LateResolveValues) of all such forward references
703// and back patchs after we are done.
704//
705
706// ResolveDefinitions - If we could not resolve some defs at parsing
707// time (forward branches, phi functions for loops, etc...) resolve the
708// defs now...
709//
710static void
711ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
712 std::map<const Type*,ValueList> *FutureLateResolvers) {
Reid Spencered96d1e2007-02-08 09:08:52 +0000713
Reid Spencer950bf602007-01-26 08:19:09 +0000714 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
715 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
716 E = LateResolvers.end(); LRI != E; ++LRI) {
Reid Spencered96d1e2007-02-08 09:08:52 +0000717 const Type* Ty = LRI->first;
Reid Spencer950bf602007-01-26 08:19:09 +0000718 ValueList &List = LRI->second;
719 while (!List.empty()) {
720 Value *V = List.back();
721 List.pop_back();
722
723 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
724 CurModule.PlaceHolderInfo.find(V);
725 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error");
726
727 ValID &DID = PHI->second.first;
728
Reid Spencered96d1e2007-02-08 09:08:52 +0000729 Value *TheRealValue = getExistingValue(Ty, DID);
Reid Spencer950bf602007-01-26 08:19:09 +0000730 if (TheRealValue) {
731 V->replaceAllUsesWith(TheRealValue);
732 delete V;
733 CurModule.PlaceHolderInfo.erase(PHI);
734 } else if (FutureLateResolvers) {
735 // Functions have their unresolved items forwarded to the module late
736 // resolver table
737 InsertValue(V, *FutureLateResolvers);
738 } else {
739 if (DID.Type == ValID::NameVal) {
Reid Spencered96d1e2007-02-08 09:08:52 +0000740 error("Reference to an invalid definition: '" + DID.getName() +
741 "' of type '" + V->getType()->getDescription() + "'",
742 PHI->second.second);
Reid Spencer7de2e012007-01-29 19:08:46 +0000743 return;
Reid Spencer950bf602007-01-26 08:19:09 +0000744 } else {
745 error("Reference to an invalid definition: #" +
746 itostr(DID.Num) + " of type '" +
747 V->getType()->getDescription() + "'", PHI->second.second);
748 return;
749 }
750 }
751 }
752 }
753
754 LateResolvers.clear();
755}
756
Reid Spencerbb1fd572007-03-21 17:15:50 +0000757/// This function is used for type resolution and upref handling. When a type
758/// becomes concrete, this function is called to adjust the signedness for the
759/// concrete type.
760static void ResolveTypeSign(const Type* oldTy, const Signedness &Sign) {
761 std::string TyName = CurModule.CurrentModule->getTypeName(oldTy);
762 if (!TyName.empty())
763 CurModule.NamedTypeSigns[TyName] = Sign;
764}
765
766/// ResolveTypeTo - A brand new type was just declared. This means that (if
767/// name is not null) things referencing Name can be resolved. Otherwise,
768/// things refering to the number can be resolved. Do this now.
769static void ResolveTypeTo(char *Name, const Type *ToTy, const Signedness& Sign){
Reid Spencer950bf602007-01-26 08:19:09 +0000770 ValID D;
Reid Spencerbb1fd572007-03-21 17:15:50 +0000771 if (Name)
772 D = ValID::create(Name);
773 else
774 D = ValID::create((int)CurModule.Types.size());
775 D.S.copy(Sign);
776
Reid Spencerbaba98a2007-04-11 12:10:58 +0000777 if (Name)
778 CurModule.NamedTypeSigns[Name] = Sign;
Reid Spencer950bf602007-01-26 08:19:09 +0000779
780 std::map<ValID, PATypeHolder>::iterator I =
781 CurModule.LateResolveTypes.find(D);
782 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencerbb1fd572007-03-21 17:15:50 +0000783 const Type *OldTy = I->second.get();
784 ((DerivedType*)OldTy)->refineAbstractTypeTo(ToTy);
Reid Spencer950bf602007-01-26 08:19:09 +0000785 CurModule.LateResolveTypes.erase(I);
786 }
787}
788
Anton Korobeynikovce13b852007-01-28 15:25:24 +0000789/// This is the implementation portion of TypeHasInteger. It traverses the
790/// type given, avoiding recursive types, and returns true as soon as it finds
791/// an integer type. If no integer type is found, it returns false.
792static bool TypeHasIntegerI(const Type *Ty, std::vector<const Type*> Stack) {
793 // Handle some easy cases
794 if (Ty->isPrimitiveType() || (Ty->getTypeID() == Type::OpaqueTyID))
795 return false;
796 if (Ty->isInteger())
797 return true;
798 if (const SequentialType *STy = dyn_cast<SequentialType>(Ty))
799 return STy->getElementType()->isInteger();
800
801 // Avoid type structure recursion
802 for (std::vector<const Type*>::iterator I = Stack.begin(), E = Stack.end();
803 I != E; ++I)
804 if (Ty == *I)
805 return false;
806
807 // Push us on the type stack
808 Stack.push_back(Ty);
809
810 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
811 if (TypeHasIntegerI(FTy->getReturnType(), Stack))
812 return true;
813 FunctionType::param_iterator I = FTy->param_begin();
814 FunctionType::param_iterator E = FTy->param_end();
815 for (; I != E; ++I)
816 if (TypeHasIntegerI(*I, Stack))
817 return true;
818 return false;
819 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
820 StructType::element_iterator I = STy->element_begin();
821 StructType::element_iterator E = STy->element_end();
822 for (; I != E; ++I) {
823 if (TypeHasIntegerI(*I, Stack))
824 return true;
825 }
826 return false;
827 }
828 // There shouldn't be anything else, but its definitely not integer
829 assert(0 && "What type is this?");
830 return false;
831}
832
833/// This is the interface to TypeHasIntegerI. It just provides the type stack,
834/// to avoid recursion, and then calls TypeHasIntegerI.
835static inline bool TypeHasInteger(const Type *Ty) {
836 std::vector<const Type*> TyStack;
837 return TypeHasIntegerI(Ty, TyStack);
838}
839
Reid Spencer950bf602007-01-26 08:19:09 +0000840// setValueName - Set the specified value to the name given. The name may be
841// null potentially, in which case this is a noop. The string passed in is
842// assumed to be a malloc'd string buffer, and is free'd by this function.
843//
Reid Spencerbb1fd572007-03-21 17:15:50 +0000844static void setValueName(const ValueInfo &V, char *NameStr) {
Reid Spencer950bf602007-01-26 08:19:09 +0000845 if (NameStr) {
846 std::string Name(NameStr); // Copy string
847 free(NameStr); // Free old string
848
Reid Spencerbb1fd572007-03-21 17:15:50 +0000849 if (V.V->getType() == Type::VoidTy) {
Reid Spencer950bf602007-01-26 08:19:09 +0000850 error("Can't assign name '" + Name + "' to value with void type");
851 return;
852 }
853
Reid Spencer950bf602007-01-26 08:19:09 +0000854 assert(inFunctionScope() && "Must be in function scope");
855
856 // Search the function's symbol table for an existing value of this name
Reid Spenceref9b9a72007-02-05 20:47:22 +0000857 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
858 Value* Existing = ST.lookup(Name);
Reid Spencer950bf602007-01-26 08:19:09 +0000859 if (Existing) {
Anton Korobeynikovce13b852007-01-28 15:25:24 +0000860 // An existing value of the same name was found. This might have happened
861 // because of the integer type planes collapsing in LLVM 2.0.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000862 if (Existing->getType() == V.V->getType() &&
Anton Korobeynikovce13b852007-01-28 15:25:24 +0000863 !TypeHasInteger(Existing->getType())) {
864 // If the type does not contain any integers in them then this can't be
865 // a type plane collapsing issue. It truly is a redefinition and we
866 // should error out as the assembly is invalid.
867 error("Redefinition of value named '" + Name + "' of type '" +
Reid Spencerbb1fd572007-03-21 17:15:50 +0000868 V.V->getType()->getDescription() + "'");
Anton Korobeynikovce13b852007-01-28 15:25:24 +0000869 return;
Reid Spencer950bf602007-01-26 08:19:09 +0000870 }
871 // In LLVM 2.0 we don't allow names to be re-used for any values in a
872 // function, regardless of Type. Previously re-use of names was okay as
873 // long as they were distinct types. With type planes collapsing because
874 // of the signedness change and because of PR411, this can no longer be
875 // supported. We must search the entire symbol table for a conflicting
876 // name and make the name unique. No warning is needed as this can't
877 // cause a problem.
878 std::string NewName = makeNameUnique(Name);
879 // We're changing the name but it will probably be used by other
880 // instructions as operands later on. Consequently we have to retain
881 // a mapping of the renaming that we're doing.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000882 RenameMapKey Key = makeRenameMapKey(Name, V.V->getType(), V.S);
Reid Spencer950bf602007-01-26 08:19:09 +0000883 CurFun.RenameMap[Key] = NewName;
884 Name = NewName;
885 }
886
887 // Set the name.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000888 V.V->setName(Name);
Reid Spencer950bf602007-01-26 08:19:09 +0000889 }
890}
891
892/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
893/// this is a declaration, otherwise it is a definition.
894static GlobalVariable *
895ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
896 bool isConstantGlobal, const Type *Ty,
Reid Spencerbb1fd572007-03-21 17:15:50 +0000897 Constant *Initializer,
898 const Signedness &Sign) {
Reid Spencer950bf602007-01-26 08:19:09 +0000899 if (isa<FunctionType>(Ty))
900 error("Cannot declare global vars of function type");
901
902 const PointerType *PTy = PointerType::get(Ty);
903
904 std::string Name;
905 if (NameStr) {
906 Name = NameStr; // Copy string
907 free(NameStr); // Free old string
908 }
909
910 // See if this global value was forward referenced. If so, recycle the
911 // object.
912 ValID ID;
913 if (!Name.empty()) {
Reid Spencer5eb77c72007-03-15 03:26:42 +0000914 ID = ValID::create((char*)Name.c_str());
Reid Spencer950bf602007-01-26 08:19:09 +0000915 } else {
Reid Spencer5eb77c72007-03-15 03:26:42 +0000916 ID = ValID::create((int)CurModule.Values[PTy].size());
Reid Spencer950bf602007-01-26 08:19:09 +0000917 }
Reid Spencerbb1fd572007-03-21 17:15:50 +0000918 ID.S.makeComposite(Sign);
Reid Spencer950bf602007-01-26 08:19:09 +0000919
920 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
921 // Move the global to the end of the list, from whereever it was
922 // previously inserted.
923 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
924 CurModule.CurrentModule->getGlobalList().remove(GV);
925 CurModule.CurrentModule->getGlobalList().push_back(GV);
926 GV->setInitializer(Initializer);
927 GV->setLinkage(Linkage);
928 GV->setConstant(isConstantGlobal);
929 InsertValue(GV, CurModule.Values);
930 return GV;
931 }
932
933 // If this global has a name, check to see if there is already a definition
934 // of this global in the module and emit warnings if there are conflicts.
935 if (!Name.empty()) {
936 // The global has a name. See if there's an existing one of the same name.
Reid Spencere59f4932007-04-16 03:05:01 +0000937 if (CurModule.CurrentModule->getNamedGlobal(Name) ||
938 CurModule.CurrentModule->getFunction(Name)) {
939 // We found an existing global of the same name. This isn't allowed
Reid Spencer950bf602007-01-26 08:19:09 +0000940 // in LLVM 2.0. Consequently, we must alter the name of the global so it
941 // can at least compile. This can happen because of type planes
942 // There is alread a global of the same name which means there is a
943 // conflict. Let's see what we can do about it.
944 std::string NewName(makeNameUnique(Name));
Reid Spencerbb1fd572007-03-21 17:15:50 +0000945 if (Linkage != GlobalValue::InternalLinkage) {
Reid Spencer950bf602007-01-26 08:19:09 +0000946 // The linkage of this gval is external so we can't reliably rename
947 // it because it could potentially create a linking problem.
948 // However, we can't leave the name conflict in the output either or
949 // it won't assemble with LLVM 2.0. So, all we can do is rename
950 // this one to something unique and emit a warning about the problem.
951 warning("Renaming global variable '" + Name + "' to '" + NewName +
952 "' may cause linkage errors");
953 }
954
955 // Put the renaming in the global rename map
Reid Spencerbb1fd572007-03-21 17:15:50 +0000956 RenameMapKey Key = makeRenameMapKey(Name, PointerType::get(Ty), ID.S);
Reid Spencer950bf602007-01-26 08:19:09 +0000957 CurModule.RenameMap[Key] = NewName;
958
959 // Rename it
960 Name = NewName;
961 }
962 }
963
964 // Otherwise there is no existing GV to use, create one now.
965 GlobalVariable *GV =
966 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
967 CurModule.CurrentModule);
968 InsertValue(GV, CurModule.Values);
Reid Spencerbb1fd572007-03-21 17:15:50 +0000969 // Remember the sign of this global.
970 CurModule.NamedValueSigns[Name] = ID.S;
Reid Spencer950bf602007-01-26 08:19:09 +0000971 return GV;
972}
973
974// setTypeName - Set the specified type to the name given. The name may be
975// null potentially, in which case this is a noop. The string passed in is
976// assumed to be a malloc'd string buffer, and is freed by this function.
977//
978// This function returns true if the type has already been defined, but is
979// allowed to be redefined in the specified context. If the name is a new name
980// for the type plane, it is inserted and false is returned.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000981static bool setTypeName(const PATypeInfo& TI, char *NameStr) {
Reid Spencer950bf602007-01-26 08:19:09 +0000982 assert(!inFunctionScope() && "Can't give types function-local names");
983 if (NameStr == 0) return false;
984
985 std::string Name(NameStr); // Copy string
986 free(NameStr); // Free old string
987
Reid Spencerbb1fd572007-03-21 17:15:50 +0000988 const Type* Ty = TI.PAT->get();
989
Reid Spencer950bf602007-01-26 08:19:09 +0000990 // We don't allow assigning names to void type
Reid Spencerbb1fd572007-03-21 17:15:50 +0000991 if (Ty == Type::VoidTy) {
Reid Spencer950bf602007-01-26 08:19:09 +0000992 error("Can't assign name '" + Name + "' to the void type");
993 return false;
994 }
995
996 // Set the type name, checking for conflicts as we do so.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000997 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, Ty);
998
999 // Save the sign information for later use
1000 CurModule.NamedTypeSigns[Name] = TI.S;
Reid Spencer950bf602007-01-26 08:19:09 +00001001
1002 if (AlreadyExists) { // Inserting a name that is already defined???
1003 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
1004 assert(Existing && "Conflict but no matching type?");
1005
1006 // There is only one case where this is allowed: when we are refining an
1007 // opaque type. In this case, Existing will be an opaque type.
1008 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
1009 // We ARE replacing an opaque type!
Reid Spencerbb1fd572007-03-21 17:15:50 +00001010 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(Ty);
Reid Spencer950bf602007-01-26 08:19:09 +00001011 return true;
1012 }
1013
1014 // Otherwise, this is an attempt to redefine a type. That's okay if
1015 // the redefinition is identical to the original. This will be so if
1016 // Existing and T point to the same Type object. In this one case we
1017 // allow the equivalent redefinition.
Reid Spencerbb1fd572007-03-21 17:15:50 +00001018 if (Existing == Ty) return true; // Yes, it's equal.
Reid Spencer950bf602007-01-26 08:19:09 +00001019
1020 // Any other kind of (non-equivalent) redefinition is an error.
1021 error("Redefinition of type named '" + Name + "' in the '" +
Reid Spencerbb1fd572007-03-21 17:15:50 +00001022 Ty->getDescription() + "' type plane");
Reid Spencer950bf602007-01-26 08:19:09 +00001023 }
1024
1025 return false;
1026}
1027
1028//===----------------------------------------------------------------------===//
1029// Code for handling upreferences in type names...
1030//
1031
1032// TypeContains - Returns true if Ty directly contains E in it.
1033//
1034static bool TypeContains(const Type *Ty, const Type *E) {
1035 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
1036 E) != Ty->subtype_end();
1037}
1038
1039namespace {
1040 struct UpRefRecord {
1041 // NestingLevel - The number of nesting levels that need to be popped before
1042 // this type is resolved.
1043 unsigned NestingLevel;
1044
1045 // LastContainedTy - This is the type at the current binding level for the
1046 // type. Every time we reduce the nesting level, this gets updated.
1047 const Type *LastContainedTy;
1048
1049 // UpRefTy - This is the actual opaque type that the upreference is
1050 // represented with.
1051 OpaqueType *UpRefTy;
1052
1053 UpRefRecord(unsigned NL, OpaqueType *URTy)
Reid Spencerbb1fd572007-03-21 17:15:50 +00001054 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) { }
Reid Spencer950bf602007-01-26 08:19:09 +00001055 };
1056}
1057
1058// UpRefs - A list of the outstanding upreferences that need to be resolved.
1059static std::vector<UpRefRecord> UpRefs;
1060
1061/// HandleUpRefs - Every time we finish a new layer of types, this function is
1062/// called. It loops through the UpRefs vector, which is a list of the
1063/// currently active types. For each type, if the up reference is contained in
1064/// the newly completed type, we decrement the level count. When the level
1065/// count reaches zero, the upreferenced type is the type that is passed in:
1066/// thus we can complete the cycle.
1067///
Reid Spencerbb1fd572007-03-21 17:15:50 +00001068static PATypeHolder HandleUpRefs(const Type *ty, const Signedness& Sign) {
Reid Spencer950bf602007-01-26 08:19:09 +00001069 // If Ty isn't abstract, or if there are no up-references in it, then there is
1070 // nothing to resolve here.
1071 if (!ty->isAbstract() || UpRefs.empty()) return ty;
1072
1073 PATypeHolder Ty(ty);
1074 UR_OUT("Type '" << Ty->getDescription() <<
1075 "' newly formed. Resolving upreferences.\n" <<
1076 UpRefs.size() << " upreferences active!\n");
1077
1078 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
1079 // to zero), we resolve them all together before we resolve them to Ty. At
1080 // the end of the loop, if there is anything to resolve to Ty, it will be in
1081 // this variable.
1082 OpaqueType *TypeToResolve = 0;
1083
Reid Spencerbb1fd572007-03-21 17:15:50 +00001084 unsigned i = 0;
1085 for (; i != UpRefs.size(); ++i) {
Reid Spencer950bf602007-01-26 08:19:09 +00001086 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Reid Spencerbb1fd572007-03-21 17:15:50 +00001087 << UpRefs[i].UpRefTy->getDescription() << ") = "
1088 << (TypeContains(Ty, UpRefs[i].UpRefTy) ? "true" : "false") << "\n");
Reid Spencer950bf602007-01-26 08:19:09 +00001089 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
1090 // Decrement level of upreference
1091 unsigned Level = --UpRefs[i].NestingLevel;
1092 UpRefs[i].LastContainedTy = Ty;
1093 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
1094 if (Level == 0) { // Upreference should be resolved!
1095 if (!TypeToResolve) {
1096 TypeToResolve = UpRefs[i].UpRefTy;
1097 } else {
1098 UR_OUT(" * Resolving upreference for "
Reid Spencerbb1fd572007-03-21 17:15:50 +00001099 << UpRefs[i].UpRefTy->getDescription() << "\n";
1100 std::string OldName = UpRefs[i].UpRefTy->getDescription());
1101 ResolveTypeSign(UpRefs[i].UpRefTy, Sign);
Reid Spencer950bf602007-01-26 08:19:09 +00001102 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
1103 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
1104 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
1105 }
1106 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
1107 --i; // Do not skip the next element...
1108 }
1109 }
1110 }
1111
1112 if (TypeToResolve) {
1113 UR_OUT(" * Resolving upreference for "
Reid Spencerbb1fd572007-03-21 17:15:50 +00001114 << UpRefs[i].UpRefTy->getDescription() << "\n";
Reid Spencer950bf602007-01-26 08:19:09 +00001115 std::string OldName = TypeToResolve->getDescription());
Reid Spencerbb1fd572007-03-21 17:15:50 +00001116 ResolveTypeSign(TypeToResolve, Sign);
Reid Spencer950bf602007-01-26 08:19:09 +00001117 TypeToResolve->refineAbstractTypeTo(Ty);
1118 }
1119
1120 return Ty;
1121}
1122
Reid Spencerbb1fd572007-03-21 17:15:50 +00001123bool Signedness::operator<(const Signedness &that) const {
1124 if (isNamed()) {
1125 if (that.isNamed())
1126 return *(this->name) < *(that.name);
1127 else
1128 return CurModule.NamedTypeSigns[*name] < that;
1129 } else if (that.isNamed()) {
1130 return *this < CurModule.NamedTypeSigns[*that.name];
1131 }
1132
1133 if (isComposite() && that.isComposite()) {
1134 if (sv->size() == that.sv->size()) {
1135 SignVector::const_iterator thisI = sv->begin(), thisE = sv->end();
1136 SignVector::const_iterator thatI = that.sv->begin(),
1137 thatE = that.sv->end();
1138 for (; thisI != thisE; ++thisI, ++thatI) {
1139 if (*thisI < *thatI)
1140 return true;
1141 else if (!(*thisI == *thatI))
1142 return false;
1143 }
1144 return false;
1145 }
1146 return sv->size() < that.sv->size();
1147 }
1148 return kind < that.kind;
1149}
1150
1151bool Signedness::operator==(const Signedness &that) const {
1152 if (isNamed())
1153 if (that.isNamed())
1154 return *(this->name) == *(that.name);
1155 else
1156 return CurModule.NamedTypeSigns[*(this->name)] == that;
1157 else if (that.isNamed())
1158 return *this == CurModule.NamedTypeSigns[*(that.name)];
1159 if (isComposite() && that.isComposite()) {
1160 if (sv->size() == that.sv->size()) {
1161 SignVector::const_iterator thisI = sv->begin(), thisE = sv->end();
1162 SignVector::const_iterator thatI = that.sv->begin(),
1163 thatE = that.sv->end();
1164 for (; thisI != thisE; ++thisI, ++thatI) {
1165 if (!(*thisI == *thatI))
1166 return false;
1167 }
1168 return true;
1169 }
1170 return false;
1171 }
1172 return kind == that.kind;
1173}
1174
1175void Signedness::copy(const Signedness &that) {
1176 if (that.isNamed()) {
1177 kind = Named;
1178 name = new std::string(*that.name);
1179 } else if (that.isComposite()) {
1180 kind = Composite;
1181 sv = new SignVector();
1182 *sv = *that.sv;
1183 } else {
1184 kind = that.kind;
1185 sv = 0;
1186 }
1187}
1188
1189void Signedness::destroy() {
1190 if (isNamed()) {
1191 delete name;
1192 } else if (isComposite()) {
1193 delete sv;
1194 }
1195}
1196
Evan Cheng2b484202007-03-22 07:43:51 +00001197#ifndef NDEBUG
Reid Spencerbb1fd572007-03-21 17:15:50 +00001198void Signedness::dump() const {
1199 if (isComposite()) {
1200 if (sv->size() == 1) {
1201 (*sv)[0].dump();
1202 std::cerr << "*";
1203 } else {
1204 std::cerr << "{ " ;
1205 for (unsigned i = 0; i < sv->size(); ++i) {
1206 if (i != 0)
1207 std::cerr << ", ";
1208 (*sv)[i].dump();
1209 }
1210 std::cerr << "} " ;
1211 }
1212 } else if (isNamed()) {
1213 std::cerr << *name;
1214 } else if (isSigned()) {
1215 std::cerr << "S";
1216 } else if (isUnsigned()) {
1217 std::cerr << "U";
1218 } else
1219 std::cerr << ".";
1220}
Evan Cheng2b484202007-03-22 07:43:51 +00001221#endif
Reid Spencerbb1fd572007-03-21 17:15:50 +00001222
Reid Spencer950bf602007-01-26 08:19:09 +00001223static inline Instruction::TermOps
1224getTermOp(TermOps op) {
1225 switch (op) {
1226 default : assert(0 && "Invalid OldTermOp");
1227 case RetOp : return Instruction::Ret;
1228 case BrOp : return Instruction::Br;
1229 case SwitchOp : return Instruction::Switch;
1230 case InvokeOp : return Instruction::Invoke;
1231 case UnwindOp : return Instruction::Unwind;
1232 case UnreachableOp: return Instruction::Unreachable;
1233 }
1234}
1235
1236static inline Instruction::BinaryOps
Reid Spencerbb1fd572007-03-21 17:15:50 +00001237getBinaryOp(BinaryOps op, const Type *Ty, const Signedness& Sign) {
Reid Spencer950bf602007-01-26 08:19:09 +00001238 switch (op) {
1239 default : assert(0 && "Invalid OldBinaryOps");
1240 case SetEQ :
1241 case SetNE :
1242 case SetLE :
1243 case SetGE :
1244 case SetLT :
1245 case SetGT : assert(0 && "Should use getCompareOp");
1246 case AddOp : return Instruction::Add;
1247 case SubOp : return Instruction::Sub;
1248 case MulOp : return Instruction::Mul;
1249 case DivOp : {
1250 // This is an obsolete instruction so we must upgrade it based on the
1251 // types of its operands.
1252 bool isFP = Ty->isFloatingPoint();
Reid Spencer9d6565a2007-02-15 02:26:10 +00001253 if (const VectorType* PTy = dyn_cast<VectorType>(Ty))
Chris Lattner4227bdb2007-02-19 07:34:02 +00001254 // If its a vector type we want to use the element type
Reid Spencer950bf602007-01-26 08:19:09 +00001255 isFP = PTy->getElementType()->isFloatingPoint();
1256 if (isFP)
1257 return Instruction::FDiv;
Reid Spencerbb1fd572007-03-21 17:15:50 +00001258 else if (Sign.isSigned())
Reid Spencer950bf602007-01-26 08:19:09 +00001259 return Instruction::SDiv;
1260 return Instruction::UDiv;
1261 }
1262 case UDivOp : return Instruction::UDiv;
1263 case SDivOp : return Instruction::SDiv;
1264 case FDivOp : return Instruction::FDiv;
1265 case RemOp : {
1266 // This is an obsolete instruction so we must upgrade it based on the
1267 // types of its operands.
1268 bool isFP = Ty->isFloatingPoint();
Reid Spencer9d6565a2007-02-15 02:26:10 +00001269 if (const VectorType* PTy = dyn_cast<VectorType>(Ty))
Chris Lattner4227bdb2007-02-19 07:34:02 +00001270 // If its a vector type we want to use the element type
Reid Spencer950bf602007-01-26 08:19:09 +00001271 isFP = PTy->getElementType()->isFloatingPoint();
1272 // Select correct opcode
1273 if (isFP)
1274 return Instruction::FRem;
Reid Spencerbb1fd572007-03-21 17:15:50 +00001275 else if (Sign.isSigned())
Reid Spencer950bf602007-01-26 08:19:09 +00001276 return Instruction::SRem;
1277 return Instruction::URem;
1278 }
1279 case URemOp : return Instruction::URem;
1280 case SRemOp : return Instruction::SRem;
1281 case FRemOp : return Instruction::FRem;
Reid Spencer832254e2007-02-02 02:16:23 +00001282 case LShrOp : return Instruction::LShr;
1283 case AShrOp : return Instruction::AShr;
1284 case ShlOp : return Instruction::Shl;
1285 case ShrOp :
Reid Spencerbb1fd572007-03-21 17:15:50 +00001286 if (Sign.isSigned())
Reid Spencer832254e2007-02-02 02:16:23 +00001287 return Instruction::AShr;
1288 return Instruction::LShr;
Reid Spencer950bf602007-01-26 08:19:09 +00001289 case AndOp : return Instruction::And;
1290 case OrOp : return Instruction::Or;
1291 case XorOp : return Instruction::Xor;
1292 }
1293}
1294
1295static inline Instruction::OtherOps
1296getCompareOp(BinaryOps op, unsigned short &predicate, const Type* &Ty,
Reid Spencerbb1fd572007-03-21 17:15:50 +00001297 const Signedness &Sign) {
1298 bool isSigned = Sign.isSigned();
Reid Spencer950bf602007-01-26 08:19:09 +00001299 bool isFP = Ty->isFloatingPoint();
1300 switch (op) {
1301 default : assert(0 && "Invalid OldSetCC");
1302 case SetEQ :
1303 if (isFP) {
1304 predicate = FCmpInst::FCMP_OEQ;
1305 return Instruction::FCmp;
1306 } else {
1307 predicate = ICmpInst::ICMP_EQ;
1308 return Instruction::ICmp;
1309 }
1310 case SetNE :
1311 if (isFP) {
1312 predicate = FCmpInst::FCMP_UNE;
1313 return Instruction::FCmp;
1314 } else {
1315 predicate = ICmpInst::ICMP_NE;
1316 return Instruction::ICmp;
1317 }
1318 case SetLE :
1319 if (isFP) {
1320 predicate = FCmpInst::FCMP_OLE;
1321 return Instruction::FCmp;
1322 } else {
1323 if (isSigned)
1324 predicate = ICmpInst::ICMP_SLE;
1325 else
1326 predicate = ICmpInst::ICMP_ULE;
1327 return Instruction::ICmp;
1328 }
1329 case SetGE :
1330 if (isFP) {
1331 predicate = FCmpInst::FCMP_OGE;
1332 return Instruction::FCmp;
1333 } else {
1334 if (isSigned)
1335 predicate = ICmpInst::ICMP_SGE;
1336 else
1337 predicate = ICmpInst::ICMP_UGE;
1338 return Instruction::ICmp;
1339 }
1340 case SetLT :
1341 if (isFP) {
1342 predicate = FCmpInst::FCMP_OLT;
1343 return Instruction::FCmp;
1344 } else {
1345 if (isSigned)
1346 predicate = ICmpInst::ICMP_SLT;
1347 else
1348 predicate = ICmpInst::ICMP_ULT;
1349 return Instruction::ICmp;
1350 }
1351 case SetGT :
1352 if (isFP) {
1353 predicate = FCmpInst::FCMP_OGT;
1354 return Instruction::FCmp;
1355 } else {
1356 if (isSigned)
1357 predicate = ICmpInst::ICMP_SGT;
1358 else
1359 predicate = ICmpInst::ICMP_UGT;
1360 return Instruction::ICmp;
1361 }
1362 }
1363}
1364
1365static inline Instruction::MemoryOps getMemoryOp(MemoryOps op) {
1366 switch (op) {
1367 default : assert(0 && "Invalid OldMemoryOps");
1368 case MallocOp : return Instruction::Malloc;
1369 case FreeOp : return Instruction::Free;
1370 case AllocaOp : return Instruction::Alloca;
1371 case LoadOp : return Instruction::Load;
1372 case StoreOp : return Instruction::Store;
1373 case GetElementPtrOp : return Instruction::GetElementPtr;
1374 }
1375}
1376
1377static inline Instruction::OtherOps
Reid Spencerbb1fd572007-03-21 17:15:50 +00001378getOtherOp(OtherOps op, const Signedness &Sign) {
Reid Spencer950bf602007-01-26 08:19:09 +00001379 switch (op) {
1380 default : assert(0 && "Invalid OldOtherOps");
1381 case PHIOp : return Instruction::PHI;
1382 case CallOp : return Instruction::Call;
Reid Spencer950bf602007-01-26 08:19:09 +00001383 case SelectOp : return Instruction::Select;
1384 case UserOp1 : return Instruction::UserOp1;
1385 case UserOp2 : return Instruction::UserOp2;
1386 case VAArg : return Instruction::VAArg;
1387 case ExtractElementOp : return Instruction::ExtractElement;
1388 case InsertElementOp : return Instruction::InsertElement;
1389 case ShuffleVectorOp : return Instruction::ShuffleVector;
1390 case ICmpOp : return Instruction::ICmp;
1391 case FCmpOp : return Instruction::FCmp;
Reid Spencer950bf602007-01-26 08:19:09 +00001392 };
1393}
1394
1395static inline Value*
Reid Spencerbb1fd572007-03-21 17:15:50 +00001396getCast(CastOps op, Value *Src, const Signedness &SrcSign, const Type *DstTy,
1397 const Signedness &DstSign, bool ForceInstruction = false) {
Reid Spencer950bf602007-01-26 08:19:09 +00001398 Instruction::CastOps Opcode;
1399 const Type* SrcTy = Src->getType();
1400 if (op == CastOp) {
1401 if (SrcTy->isFloatingPoint() && isa<PointerType>(DstTy)) {
1402 // fp -> ptr cast is no longer supported but we must upgrade this
1403 // by doing a double cast: fp -> int -> ptr
1404 SrcTy = Type::Int64Ty;
1405 Opcode = Instruction::IntToPtr;
1406 if (isa<Constant>(Src)) {
1407 Src = ConstantExpr::getCast(Instruction::FPToUI,
1408 cast<Constant>(Src), SrcTy);
1409 } else {
1410 std::string NewName(makeNameUnique(Src->getName()));
1411 Src = new FPToUIInst(Src, SrcTy, NewName, CurBB);
1412 }
1413 } else if (isa<IntegerType>(DstTy) &&
1414 cast<IntegerType>(DstTy)->getBitWidth() == 1) {
1415 // cast type %x to bool was previously defined as setne type %x, null
1416 // The cast semantic is now to truncate, not compare so we must retain
1417 // the original intent by replacing the cast with a setne
1418 Constant* Null = Constant::getNullValue(SrcTy);
1419 Instruction::OtherOps Opcode = Instruction::ICmp;
1420 unsigned short predicate = ICmpInst::ICMP_NE;
1421 if (SrcTy->isFloatingPoint()) {
1422 Opcode = Instruction::FCmp;
1423 predicate = FCmpInst::FCMP_ONE;
1424 } else if (!SrcTy->isInteger() && !isa<PointerType>(SrcTy)) {
1425 error("Invalid cast to bool");
1426 }
1427 if (isa<Constant>(Src) && !ForceInstruction)
1428 return ConstantExpr::getCompare(predicate, cast<Constant>(Src), Null);
1429 else
1430 return CmpInst::create(Opcode, predicate, Src, Null);
1431 }
1432 // Determine the opcode to use by calling CastInst::getCastOpcode
1433 Opcode =
Reid Spencerbb1fd572007-03-21 17:15:50 +00001434 CastInst::getCastOpcode(Src, SrcSign.isSigned(), DstTy,
1435 DstSign.isSigned());
Reid Spencer950bf602007-01-26 08:19:09 +00001436
1437 } else switch (op) {
1438 default: assert(0 && "Invalid cast token");
1439 case TruncOp: Opcode = Instruction::Trunc; break;
1440 case ZExtOp: Opcode = Instruction::ZExt; break;
1441 case SExtOp: Opcode = Instruction::SExt; break;
1442 case FPTruncOp: Opcode = Instruction::FPTrunc; break;
1443 case FPExtOp: Opcode = Instruction::FPExt; break;
1444 case FPToUIOp: Opcode = Instruction::FPToUI; break;
1445 case FPToSIOp: Opcode = Instruction::FPToSI; break;
1446 case UIToFPOp: Opcode = Instruction::UIToFP; break;
1447 case SIToFPOp: Opcode = Instruction::SIToFP; break;
1448 case PtrToIntOp: Opcode = Instruction::PtrToInt; break;
1449 case IntToPtrOp: Opcode = Instruction::IntToPtr; break;
1450 case BitCastOp: Opcode = Instruction::BitCast; break;
1451 }
1452
1453 if (isa<Constant>(Src) && !ForceInstruction)
1454 return ConstantExpr::getCast(Opcode, cast<Constant>(Src), DstTy);
1455 return CastInst::create(Opcode, Src, DstTy);
1456}
1457
1458static Instruction *
1459upgradeIntrinsicCall(const Type* RetTy, const ValID &ID,
1460 std::vector<Value*>& Args) {
1461
1462 std::string Name = ID.Type == ValID::NameVal ? ID.Name : "";
Reid Spencer7eea8ff2007-05-18 05:48:07 +00001463 if (Name.length() <= 5 || Name[0] != 'l' || Name[1] != 'l' ||
1464 Name[2] != 'v' || Name[3] != 'm' || Name[4] != '.')
1465 return 0;
1466
Reid Spencer41b213e2007-04-02 01:14:00 +00001467 switch (Name[5]) {
1468 case 'i':
1469 if (Name == "llvm.isunordered.f32" || Name == "llvm.isunordered.f64") {
1470 if (Args.size() != 2)
1471 error("Invalid prototype for " + Name);
1472 return new FCmpInst(FCmpInst::FCMP_UNO, Args[0], Args[1]);
1473 }
1474 break;
1475 case 'b':
1476 if (Name.length() == 14 && !memcmp(&Name[5], "bswap.i", 7)) {
1477 const Type* ArgTy = Args[0]->getType();
1478 Name += ".i" + utostr(cast<IntegerType>(ArgTy)->getBitWidth());
1479 Function *F = cast<Function>(
1480 CurModule.CurrentModule->getOrInsertFunction(Name, RetTy, ArgTy,
1481 (void*)0));
1482 return new CallInst(F, Args[0]);
1483 }
1484 break;
Reid Spencer8166a6c2007-04-02 02:08:35 +00001485 case 'c':
1486 if ((Name.length() <= 14 && !memcmp(&Name[5], "ctpop.i", 7)) ||
1487 (Name.length() <= 13 && !memcmp(&Name[5], "ctlz.i", 6)) ||
1488 (Name.length() <= 13 && !memcmp(&Name[5], "cttz.i", 6))) {
1489 // These intrinsics changed their result type.
1490 const Type* ArgTy = Args[0]->getType();
1491 Function *OldF = CurModule.CurrentModule->getFunction(Name);
1492 if (OldF)
1493 OldF->setName("upgrd.rm." + Name);
1494
1495 Function *NewF = cast<Function>(
1496 CurModule.CurrentModule->getOrInsertFunction(Name, Type::Int32Ty,
1497 ArgTy, (void*)0));
1498
1499 Instruction *Call = new CallInst(NewF, Args[0], "", CurBB);
1500 return CastInst::createIntegerCast(Call, RetTy, false);
1501 }
1502 break;
1503
Reid Spencer41b213e2007-04-02 01:14:00 +00001504 case 'v' : {
1505 const Type* PtrTy = PointerType::get(Type::Int8Ty);
1506 std::vector<const Type*> Params;
1507 if (Name == "llvm.va_start" || Name == "llvm.va_end") {
1508 if (Args.size() != 1)
1509 error("Invalid prototype for " + Name + " prototype");
1510 Params.push_back(PtrTy);
1511 const FunctionType *FTy =
1512 FunctionType::get(Type::VoidTy, Params, false);
1513 const PointerType *PFTy = PointerType::get(FTy);
1514 Value* Func = getVal(PFTy, ID);
1515 Args[0] = new BitCastInst(Args[0], PtrTy, makeNameUnique("va"), CurBB);
1516 return new CallInst(Func, &Args[0], Args.size());
1517 } else if (Name == "llvm.va_copy") {
1518 if (Args.size() != 2)
1519 error("Invalid prototype for " + Name + " prototype");
1520 Params.push_back(PtrTy);
1521 Params.push_back(PtrTy);
1522 const FunctionType *FTy =
1523 FunctionType::get(Type::VoidTy, Params, false);
1524 const PointerType *PFTy = PointerType::get(FTy);
1525 Value* Func = getVal(PFTy, ID);
1526 std::string InstName0(makeNameUnique("va0"));
1527 std::string InstName1(makeNameUnique("va1"));
1528 Args[0] = new BitCastInst(Args[0], PtrTy, InstName0, CurBB);
1529 Args[1] = new BitCastInst(Args[1], PtrTy, InstName1, CurBB);
1530 return new CallInst(Func, &Args[0], Args.size());
1531 }
Reid Spencer950bf602007-01-26 08:19:09 +00001532 }
1533 }
1534 return 0;
1535}
1536
Reid Spencerff0e4482007-04-16 00:40:57 +00001537const Type* upgradeGEPCEIndices(const Type* PTy,
1538 std::vector<ValueInfo> *Indices,
1539 std::vector<Constant*> &Result) {
1540 const Type *Ty = PTy;
1541 Result.clear();
1542 for (unsigned i = 0, e = Indices->size(); i != e ; ++i) {
1543 Constant *Index = cast<Constant>((*Indices)[i].V);
1544
1545 if (ConstantInt *CI = dyn_cast<ConstantInt>(Index)) {
1546 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte
1547 // struct indices to i32 struct indices with ZExt for compatibility.
1548 if (CI->getBitWidth() < 32)
1549 Index = ConstantExpr::getCast(Instruction::ZExt, CI, Type::Int32Ty);
1550 }
1551
1552 if (isa<SequentialType>(Ty)) {
1553 // Make sure that unsigned SequentialType indices are zext'd to
1554 // 64-bits if they were smaller than that because LLVM 2.0 will sext
1555 // all indices for SequentialType elements. We must retain the same
1556 // semantic (zext) for unsigned types.
1557 if (const IntegerType *Ity = dyn_cast<IntegerType>(Index->getType())) {
1558 if (Ity->getBitWidth() < 64 && (*Indices)[i].S.isUnsigned()) {
1559 Index = ConstantExpr::getCast(Instruction::ZExt, Index,Type::Int64Ty);
1560 }
1561 }
1562 }
1563 Result.push_back(Index);
1564 Ty = GetElementPtrInst::getIndexedType(PTy, (Value**)&Result[0],
1565 Result.size(),true);
1566 if (!Ty)
1567 error("Index list invalid for constant getelementptr");
1568 }
1569 return Ty;
1570}
1571
1572const Type* upgradeGEPInstIndices(const Type* PTy,
1573 std::vector<ValueInfo> *Indices,
1574 std::vector<Value*> &Result) {
1575 const Type *Ty = PTy;
1576 Result.clear();
1577 for (unsigned i = 0, e = Indices->size(); i != e ; ++i) {
1578 Value *Index = (*Indices)[i].V;
1579
1580 if (ConstantInt *CI = dyn_cast<ConstantInt>(Index)) {
1581 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte
1582 // struct indices to i32 struct indices with ZExt for compatibility.
1583 if (CI->getBitWidth() < 32)
1584 Index = ConstantExpr::getCast(Instruction::ZExt, CI, Type::Int32Ty);
1585 }
1586
1587
1588 if (isa<StructType>(Ty)) { // Only change struct indices
1589 if (!isa<Constant>(Index)) {
1590 error("Invalid non-constant structure index");
1591 return 0;
1592 }
Reid Spencer950bf602007-01-26 08:19:09 +00001593 } else {
1594 // Make sure that unsigned SequentialType indices are zext'd to
1595 // 64-bits if they were smaller than that because LLVM 2.0 will sext
1596 // all indices for SequentialType elements. We must retain the same
1597 // semantic (zext) for unsigned types.
Reid Spencerff0e4482007-04-16 00:40:57 +00001598 if (const IntegerType *Ity = dyn_cast<IntegerType>(Index->getType())) {
Reid Spencerbb1fd572007-03-21 17:15:50 +00001599 if (Ity->getBitWidth() < 64 && (*Indices)[i].S.isUnsigned()) {
Reid Spencerff0e4482007-04-16 00:40:57 +00001600 if (isa<Constant>(Index))
Reid Spencer950bf602007-01-26 08:19:09 +00001601 Index = ConstantExpr::getCast(Instruction::ZExt,
1602 cast<Constant>(Index), Type::Int64Ty);
1603 else
1604 Index = CastInst::create(Instruction::ZExt, Index, Type::Int64Ty,
Reid Spencer832254e2007-02-02 02:16:23 +00001605 makeNameUnique("gep"), CurBB);
Reid Spencer38f682b2007-01-26 20:31:18 +00001606 }
Reid Spencerff0e4482007-04-16 00:40:57 +00001607 }
Reid Spencer950bf602007-01-26 08:19:09 +00001608 }
Reid Spencerff0e4482007-04-16 00:40:57 +00001609 Result.push_back(Index);
1610 Ty = GetElementPtrInst::getIndexedType(PTy, &Result[0], Result.size(),true);
1611 if (!Ty)
Reid Spencer950bf602007-01-26 08:19:09 +00001612 error("Index list invalid for constant getelementptr");
Reid Spencerff0e4482007-04-16 00:40:57 +00001613 }
1614 return Ty;
Reid Spencer950bf602007-01-26 08:19:09 +00001615}
1616
Reid Spencerb7046c72007-01-29 05:41:34 +00001617unsigned upgradeCallingConv(unsigned CC) {
1618 switch (CC) {
1619 case OldCallingConv::C : return CallingConv::C;
1620 case OldCallingConv::CSRet : return CallingConv::C;
1621 case OldCallingConv::Fast : return CallingConv::Fast;
1622 case OldCallingConv::Cold : return CallingConv::Cold;
1623 case OldCallingConv::X86_StdCall : return CallingConv::X86_StdCall;
1624 case OldCallingConv::X86_FastCall: return CallingConv::X86_FastCall;
1625 default:
1626 return CC;
1627 }
1628}
1629
Reid Spencer950bf602007-01-26 08:19:09 +00001630Module* UpgradeAssembly(const std::string &infile, std::istream& in,
1631 bool debug, bool addAttrs)
Reid Spencere7c3c602006-11-30 06:36:44 +00001632{
1633 Upgradelineno = 1;
1634 CurFilename = infile;
Reid Spencer96839be2006-11-30 16:50:26 +00001635 LexInput = &in;
Reid Spencere77e35e2006-12-01 20:26:20 +00001636 yydebug = debug;
Reid Spencer71d2ec92006-12-31 06:02:26 +00001637 AddAttributes = addAttrs;
Reid Spencer950bf602007-01-26 08:19:09 +00001638 ObsoleteVarArgs = false;
1639 NewVarArgs = false;
Reid Spencere7c3c602006-11-30 06:36:44 +00001640
Reid Spencer950bf602007-01-26 08:19:09 +00001641 CurModule.CurrentModule = new Module(CurFilename);
1642
1643 // Check to make sure the parser succeeded
Reid Spencere7c3c602006-11-30 06:36:44 +00001644 if (yyparse()) {
Reid Spencer950bf602007-01-26 08:19:09 +00001645 if (ParserResult)
1646 delete ParserResult;
Reid Spencer30d0c582007-01-15 00:26:18 +00001647 std::cerr << "llvm-upgrade: parse failed.\n";
Reid Spencer30d0c582007-01-15 00:26:18 +00001648 return 0;
1649 }
1650
Reid Spencer950bf602007-01-26 08:19:09 +00001651 // Check to make sure that parsing produced a result
1652 if (!ParserResult) {
1653 std::cerr << "llvm-upgrade: no parse result.\n";
1654 return 0;
Reid Spencer30d0c582007-01-15 00:26:18 +00001655 }
1656
Reid Spencer950bf602007-01-26 08:19:09 +00001657 // Reset ParserResult variable while saving its value for the result.
1658 Module *Result = ParserResult;
1659 ParserResult = 0;
Reid Spencer30d0c582007-01-15 00:26:18 +00001660
Reid Spencer950bf602007-01-26 08:19:09 +00001661 //Not all functions use vaarg, so make a second check for ObsoleteVarArgs
Reid Spencer30d0c582007-01-15 00:26:18 +00001662 {
Reid Spencer950bf602007-01-26 08:19:09 +00001663 Function* F;
Reid Spencer688b0492007-02-05 21:19:13 +00001664 if ((F = Result->getFunction("llvm.va_start"))
Reid Spencer950bf602007-01-26 08:19:09 +00001665 && F->getFunctionType()->getNumParams() == 0)
1666 ObsoleteVarArgs = true;
Reid Spencer688b0492007-02-05 21:19:13 +00001667 if((F = Result->getFunction("llvm.va_copy"))
Reid Spencer950bf602007-01-26 08:19:09 +00001668 && F->getFunctionType()->getNumParams() == 1)
1669 ObsoleteVarArgs = true;
Reid Spencer280d8012006-12-01 23:40:53 +00001670 }
Reid Spencer319a7302007-01-05 17:20:02 +00001671
Reid Spencer950bf602007-01-26 08:19:09 +00001672 if (ObsoleteVarArgs && NewVarArgs) {
1673 error("This file is corrupt: it uses both new and old style varargs");
1674 return 0;
Reid Spencer319a7302007-01-05 17:20:02 +00001675 }
Reid Spencer319a7302007-01-05 17:20:02 +00001676
Reid Spencer950bf602007-01-26 08:19:09 +00001677 if(ObsoleteVarArgs) {
Reid Spencer688b0492007-02-05 21:19:13 +00001678 if(Function* F = Result->getFunction("llvm.va_start")) {
Reid Spencer950bf602007-01-26 08:19:09 +00001679 if (F->arg_size() != 0) {
1680 error("Obsolete va_start takes 0 argument");
Reid Spencer319a7302007-01-05 17:20:02 +00001681 return 0;
1682 }
Reid Spencer950bf602007-01-26 08:19:09 +00001683
1684 //foo = va_start()
1685 // ->
1686 //bar = alloca typeof(foo)
1687 //va_start(bar)
1688 //foo = load bar
Reid Spencer319a7302007-01-05 17:20:02 +00001689
Reid Spencer950bf602007-01-26 08:19:09 +00001690 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
1691 const Type* ArgTy = F->getFunctionType()->getReturnType();
1692 const Type* ArgTyPtr = PointerType::get(ArgTy);
1693 Function* NF = cast<Function>(Result->getOrInsertFunction(
1694 "llvm.va_start", RetTy, ArgTyPtr, (Type *)0));
1695
1696 while (!F->use_empty()) {
1697 CallInst* CI = cast<CallInst>(F->use_back());
1698 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
1699 new CallInst(NF, bar, "", CI);
1700 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
1701 CI->replaceAllUsesWith(foo);
1702 CI->getParent()->getInstList().erase(CI);
Reid Spencerf8383de2007-01-06 06:04:32 +00001703 }
Reid Spencer950bf602007-01-26 08:19:09 +00001704 Result->getFunctionList().erase(F);
Reid Spencerf8383de2007-01-06 06:04:32 +00001705 }
Reid Spencer950bf602007-01-26 08:19:09 +00001706
Reid Spencer688b0492007-02-05 21:19:13 +00001707 if(Function* F = Result->getFunction("llvm.va_end")) {
Reid Spencer950bf602007-01-26 08:19:09 +00001708 if(F->arg_size() != 1) {
1709 error("Obsolete va_end takes 1 argument");
1710 return 0;
Reid Spencerf8383de2007-01-06 06:04:32 +00001711 }
Reid Spencerf8383de2007-01-06 06:04:32 +00001712
Reid Spencer950bf602007-01-26 08:19:09 +00001713 //vaend foo
1714 // ->
1715 //bar = alloca 1 of typeof(foo)
1716 //vaend bar
1717 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
1718 const Type* ArgTy = F->getFunctionType()->getParamType(0);
1719 const Type* ArgTyPtr = PointerType::get(ArgTy);
1720 Function* NF = cast<Function>(Result->getOrInsertFunction(
1721 "llvm.va_end", RetTy, ArgTyPtr, (Type *)0));
Reid Spencerf8383de2007-01-06 06:04:32 +00001722
Reid Spencer950bf602007-01-26 08:19:09 +00001723 while (!F->use_empty()) {
1724 CallInst* CI = cast<CallInst>(F->use_back());
1725 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
1726 new StoreInst(CI->getOperand(1), bar, CI);
1727 new CallInst(NF, bar, "", CI);
1728 CI->getParent()->getInstList().erase(CI);
Reid Spencere77e35e2006-12-01 20:26:20 +00001729 }
Reid Spencer950bf602007-01-26 08:19:09 +00001730 Result->getFunctionList().erase(F);
Reid Spencere77e35e2006-12-01 20:26:20 +00001731 }
Reid Spencer950bf602007-01-26 08:19:09 +00001732
Reid Spencer688b0492007-02-05 21:19:13 +00001733 if(Function* F = Result->getFunction("llvm.va_copy")) {
Reid Spencer950bf602007-01-26 08:19:09 +00001734 if(F->arg_size() != 1) {
1735 error("Obsolete va_copy takes 1 argument");
1736 return 0;
Reid Spencere77e35e2006-12-01 20:26:20 +00001737 }
Reid Spencer950bf602007-01-26 08:19:09 +00001738 //foo = vacopy(bar)
1739 // ->
1740 //a = alloca 1 of typeof(foo)
1741 //b = alloca 1 of typeof(foo)
1742 //store bar -> b
1743 //vacopy(a, b)
1744 //foo = load a
1745
1746 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
1747 const Type* ArgTy = F->getFunctionType()->getReturnType();
1748 const Type* ArgTyPtr = PointerType::get(ArgTy);
1749 Function* NF = cast<Function>(Result->getOrInsertFunction(
1750 "llvm.va_copy", RetTy, ArgTyPtr, ArgTyPtr, (Type *)0));
Reid Spencere77e35e2006-12-01 20:26:20 +00001751
Reid Spencer950bf602007-01-26 08:19:09 +00001752 while (!F->use_empty()) {
1753 CallInst* CI = cast<CallInst>(F->use_back());
1754 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
1755 AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
1756 new StoreInst(CI->getOperand(1), b, CI);
1757 new CallInst(NF, a, b, "", CI);
1758 Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
1759 CI->replaceAllUsesWith(foo);
1760 CI->getParent()->getInstList().erase(CI);
1761 }
1762 Result->getFunctionList().erase(F);
Reid Spencer319a7302007-01-05 17:20:02 +00001763 }
1764 }
1765
Reid Spencer52402b02007-01-02 05:45:11 +00001766 return Result;
1767}
1768
Reid Spencer950bf602007-01-26 08:19:09 +00001769} // end llvm namespace
Reid Spencer319a7302007-01-05 17:20:02 +00001770
Reid Spencer950bf602007-01-26 08:19:09 +00001771using namespace llvm;
Reid Spencer30d0c582007-01-15 00:26:18 +00001772
Reid Spencere7c3c602006-11-30 06:36:44 +00001773%}
1774
Reid Spencere77e35e2006-12-01 20:26:20 +00001775%union {
Reid Spencer950bf602007-01-26 08:19:09 +00001776 llvm::Module *ModuleVal;
1777 llvm::Function *FunctionVal;
1778 std::pair<llvm::PATypeInfo, char*> *ArgVal;
1779 llvm::BasicBlock *BasicBlockVal;
Reid Spencerbb1fd572007-03-21 17:15:50 +00001780 llvm::TermInstInfo TermInstVal;
Reid Spencer950bf602007-01-26 08:19:09 +00001781 llvm::InstrInfo InstVal;
1782 llvm::ConstInfo ConstVal;
1783 llvm::ValueInfo ValueVal;
1784 llvm::PATypeInfo TypeVal;
1785 llvm::TypeInfo PrimType;
1786 llvm::PHIListInfo PHIList;
1787 std::list<llvm::PATypeInfo> *TypeList;
1788 std::vector<llvm::ValueInfo> *ValueList;
1789 std::vector<llvm::ConstInfo> *ConstVector;
1790
1791
1792 std::vector<std::pair<llvm::PATypeInfo,char*> > *ArgList;
1793 // Represent the RHS of PHI node
1794 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
1795
1796 llvm::GlobalValue::LinkageTypes Linkage;
1797 int64_t SInt64Val;
1798 uint64_t UInt64Val;
1799 int SIntVal;
1800 unsigned UIntVal;
1801 double FPVal;
1802 bool BoolVal;
1803
1804 char *StrVal; // This memory is strdup'd!
1805 llvm::ValID ValIDVal; // strdup'd memory maybe!
1806
1807 llvm::BinaryOps BinaryOpVal;
1808 llvm::TermOps TermOpVal;
1809 llvm::MemoryOps MemOpVal;
1810 llvm::OtherOps OtherOpVal;
1811 llvm::CastOps CastOpVal;
1812 llvm::ICmpInst::Predicate IPred;
1813 llvm::FCmpInst::Predicate FPred;
1814 llvm::Module::Endianness Endianness;
Reid Spencere77e35e2006-12-01 20:26:20 +00001815}
1816
Reid Spencer950bf602007-01-26 08:19:09 +00001817%type <ModuleVal> Module FunctionList
1818%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
1819%type <BasicBlockVal> BasicBlock InstructionList
1820%type <TermInstVal> BBTerminatorInst
1821%type <InstVal> Inst InstVal MemoryInst
1822%type <ConstVal> ConstVal ConstExpr
1823%type <ConstVector> ConstVector
1824%type <ArgList> ArgList ArgListH
1825%type <ArgVal> ArgVal
1826%type <PHIList> PHIList
1827%type <ValueList> ValueRefList ValueRefListE // For call param lists
1828%type <ValueList> IndexList // For GEP derived indices
1829%type <TypeList> TypeListI ArgTypeListI
1830%type <JumpTable> JumpTable
1831%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
1832%type <BoolVal> OptVolatile // 'volatile' or not
1833%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1834%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencered96d1e2007-02-08 09:08:52 +00001835%type <Linkage> OptLinkage FnDeclareLinkage
Reid Spencer950bf602007-01-26 08:19:09 +00001836%type <Endianness> BigOrLittle
Reid Spencere77e35e2006-12-01 20:26:20 +00001837
Reid Spencer950bf602007-01-26 08:19:09 +00001838// ValueRef - Unresolved reference to a definition or BB
1839%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1840%type <ValueVal> ResolvedVal // <type> <valref> pair
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001841
Reid Spencer950bf602007-01-26 08:19:09 +00001842// Tokens and types for handling constant integer values
1843//
1844// ESINT64VAL - A negative number within long long range
1845%token <SInt64Val> ESINT64VAL
Reid Spencere77e35e2006-12-01 20:26:20 +00001846
Reid Spencer950bf602007-01-26 08:19:09 +00001847// EUINT64VAL - A positive number within uns. long long range
1848%token <UInt64Val> EUINT64VAL
1849%type <SInt64Val> EINT64VAL
Reid Spencere77e35e2006-12-01 20:26:20 +00001850
Reid Spencer950bf602007-01-26 08:19:09 +00001851%token <SIntVal> SINTVAL // Signed 32 bit ints...
1852%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
1853%type <SIntVal> INTVAL
1854%token <FPVal> FPVAL // Float or Double constant
Reid Spencere77e35e2006-12-01 20:26:20 +00001855
Reid Spencer950bf602007-01-26 08:19:09 +00001856// Built in types...
1857%type <TypeVal> Types TypesV UpRTypes UpRTypesV
1858%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
1859%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
1860%token <PrimType> FLOAT DOUBLE TYPE LABEL
Reid Spencere77e35e2006-12-01 20:26:20 +00001861
Reid Spencer950bf602007-01-26 08:19:09 +00001862%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
1863%type <StrVal> Name OptName OptAssign
1864%type <UIntVal> OptAlign OptCAlign
1865%type <StrVal> OptSection SectionString
1866
1867%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
1868%token DECLARE GLOBAL CONSTANT SECTION VOLATILE
1869%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
1870%token DLLIMPORT DLLEXPORT EXTERN_WEAK
1871%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
1872%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
1873%token CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
1874%token X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
1875%token DATALAYOUT
1876%type <UIntVal> OptCallingConv
1877
1878// Basic Block Terminating Operators
1879%token <TermOpVal> RET BR SWITCH INVOKE UNREACHABLE
1880%token UNWIND EXCEPT
1881
1882// Binary Operators
1883%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Reid Spencer832254e2007-02-02 02:16:23 +00001884%type <BinaryOpVal> ShiftOps
Reid Spencer950bf602007-01-26 08:19:09 +00001885%token <BinaryOpVal> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM
Reid Spencer832254e2007-02-02 02:16:23 +00001886%token <BinaryOpVal> AND OR XOR SHL SHR ASHR LSHR
Reid Spencer950bf602007-01-26 08:19:09 +00001887%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
1888%token <OtherOpVal> ICMP FCMP
1889
1890// Memory Instructions
1891%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1892
1893// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001894%token <OtherOpVal> PHI_TOK SELECT VAARG
Reid Spencer950bf602007-01-26 08:19:09 +00001895%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
1896%token VAARG_old VANEXT_old //OBSOLETE
1897
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001898// Support for ICmp/FCmp Predicates, which is 1.9++ but not 2.0
Reid Spencer950bf602007-01-26 08:19:09 +00001899%type <IPred> IPredicates
1900%type <FPred> FPredicates
1901%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1902%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
1903
1904%token <CastOpVal> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI
1905%token <CastOpVal> UITOFP SITOFP PTRTOINT INTTOPTR BITCAST
1906%type <CastOpVal> CastOps
Reid Spencere7c3c602006-11-30 06:36:44 +00001907
1908%start Module
1909
1910%%
1911
1912// Handle constant integer size restriction and conversion...
Reid Spencer950bf602007-01-26 08:19:09 +00001913//
1914INTVAL
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001915 : SINTVAL
Reid Spencer950bf602007-01-26 08:19:09 +00001916 | UINTVAL {
1917 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
1918 error("Value too large for type");
1919 $$ = (int32_t)$1;
1920 }
1921 ;
1922
1923EINT64VAL
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001924 : ESINT64VAL // These have same type and can't cause problems...
Reid Spencer950bf602007-01-26 08:19:09 +00001925 | EUINT64VAL {
1926 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
1927 error("Value too large for type");
1928 $$ = (int64_t)$1;
1929 };
Reid Spencere7c3c602006-11-30 06:36:44 +00001930
1931// Operations that are notably excluded from this list include:
1932// RET, BR, & SWITCH because they end basic blocks and are treated specially.
Reid Spencer950bf602007-01-26 08:19:09 +00001933//
1934ArithmeticOps
1935 : ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV | REM | UREM | SREM | FREM
1936 ;
1937
1938LogicalOps
1939 : AND | OR | XOR
1940 ;
1941
1942SetCondOps
1943 : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
1944 ;
1945
1946IPredicates
1947 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
1948 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1949 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1950 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1951 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1952 ;
1953
1954FPredicates
1955 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1956 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1957 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1958 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1959 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1960 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1961 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1962 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1963 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1964 ;
1965ShiftOps
1966 : SHL | SHR | ASHR | LSHR
1967 ;
1968
1969CastOps
1970 : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI
1971 | UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
1972 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001973
1974// These are some types that allow classification if we only want a particular
1975// thing... for example, only a signed, unsigned, or integral type.
Reid Spencer950bf602007-01-26 08:19:09 +00001976SIntType
1977 : LONG | INT | SHORT | SBYTE
1978 ;
1979
1980UIntType
1981 : ULONG | UINT | USHORT | UBYTE
1982 ;
1983
1984IntType
1985 : SIntType | UIntType
1986 ;
1987
1988FPType
1989 : FLOAT | DOUBLE
1990 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001991
1992// OptAssign - Value producing statements have an optional assignment component
Reid Spencer950bf602007-01-26 08:19:09 +00001993OptAssign
1994 : Name '=' {
Reid Spencere7c3c602006-11-30 06:36:44 +00001995 $$ = $1;
1996 }
1997 | /*empty*/ {
Reid Spencer950bf602007-01-26 08:19:09 +00001998 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001999 };
2000
2001OptLinkage
Reid Spencer785a5ae2007-02-08 00:21:40 +00002002 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencer950bf602007-01-26 08:19:09 +00002003 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
2004 | WEAK { $$ = GlobalValue::WeakLinkage; }
2005 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
2006 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
2007 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencer785a5ae2007-02-08 00:21:40 +00002008 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencer950bf602007-01-26 08:19:09 +00002009 | /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
2010 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002011
2012OptCallingConv
Reid Spencer7eea8ff2007-05-18 05:48:07 +00002013 : /*empty*/ { $$ = lastCallingConv = OldCallingConv::C; }
2014 | CCC_TOK { $$ = lastCallingConv = OldCallingConv::C; }
2015 | CSRETCC_TOK { $$ = lastCallingConv = OldCallingConv::CSRet; }
2016 | FASTCC_TOK { $$ = lastCallingConv = OldCallingConv::Fast; }
2017 | COLDCC_TOK { $$ = lastCallingConv = OldCallingConv::Cold; }
2018 | X86_STDCALLCC_TOK { $$ = lastCallingConv = OldCallingConv::X86_StdCall; }
2019 | X86_FASTCALLCC_TOK { $$ = lastCallingConv = OldCallingConv::X86_FastCall; }
Reid Spencer950bf602007-01-26 08:19:09 +00002020 | CC_TOK EUINT64VAL {
2021 if ((unsigned)$2 != $2)
2022 error("Calling conv too large");
Reid Spencer7eea8ff2007-05-18 05:48:07 +00002023 $$ = lastCallingConv = $2;
Reid Spencer950bf602007-01-26 08:19:09 +00002024 }
2025 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002026
2027// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
2028// a comma before it.
2029OptAlign
Reid Spencer950bf602007-01-26 08:19:09 +00002030 : /*empty*/ { $$ = 0; }
2031 | ALIGN EUINT64VAL {
2032 $$ = $2;
2033 if ($$ != 0 && !isPowerOf2_32($$))
2034 error("Alignment must be a power of two");
2035 }
2036 ;
Reid Spencerf0cf1322006-12-07 04:23:03 +00002037
Reid Spencere7c3c602006-11-30 06:36:44 +00002038OptCAlign
Reid Spencer950bf602007-01-26 08:19:09 +00002039 : /*empty*/ { $$ = 0; }
2040 | ',' ALIGN EUINT64VAL {
2041 $$ = $3;
2042 if ($$ != 0 && !isPowerOf2_32($$))
2043 error("Alignment must be a power of two");
2044 }
2045 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002046
2047SectionString
Reid Spencer950bf602007-01-26 08:19:09 +00002048 : SECTION STRINGCONSTANT {
2049 for (unsigned i = 0, e = strlen($2); i != e; ++i)
2050 if ($2[i] == '"' || $2[i] == '\\')
2051 error("Invalid character in section name");
2052 $$ = $2;
2053 }
2054 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002055
Reid Spencer950bf602007-01-26 08:19:09 +00002056OptSection
2057 : /*empty*/ { $$ = 0; }
2058 | SectionString { $$ = $1; }
2059 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002060
Reid Spencer950bf602007-01-26 08:19:09 +00002061// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
2062// is set to be the global we are processing.
2063//
Reid Spencere7c3c602006-11-30 06:36:44 +00002064GlobalVarAttributes
Reid Spencer950bf602007-01-26 08:19:09 +00002065 : /* empty */ {}
2066 | ',' GlobalVarAttribute GlobalVarAttributes {}
2067 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002068
Reid Spencer950bf602007-01-26 08:19:09 +00002069GlobalVarAttribute
2070 : SectionString {
2071 CurGV->setSection($1);
2072 free($1);
2073 }
2074 | ALIGN EUINT64VAL {
2075 if ($2 != 0 && !isPowerOf2_32($2))
2076 error("Alignment must be a power of two");
2077 CurGV->setAlignment($2);
2078
2079 }
2080 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002081
2082//===----------------------------------------------------------------------===//
2083// Types includes all predefined types... except void, because it can only be
2084// used in specific contexts (function returning void for example). To have
2085// access to it, a user must explicitly use TypesV.
2086//
2087
2088// TypesV includes all of 'Types', but it also includes the void type.
Reid Spencer950bf602007-01-26 08:19:09 +00002089TypesV
2090 : Types
2091 | VOID {
Reid Spencered96d1e2007-02-08 09:08:52 +00002092 $$.PAT = new PATypeHolder($1.T);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002093 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002094 }
2095 ;
2096
2097UpRTypesV
2098 : UpRTypes
2099 | VOID {
Reid Spencered96d1e2007-02-08 09:08:52 +00002100 $$.PAT = new PATypeHolder($1.T);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002101 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002102 }
2103 ;
2104
2105Types
2106 : UpRTypes {
2107 if (!UpRefs.empty())
Reid Spencered96d1e2007-02-08 09:08:52 +00002108 error("Invalid upreference in type: " + (*$1.PAT)->getDescription());
Reid Spencer950bf602007-01-26 08:19:09 +00002109 $$ = $1;
2110 }
2111 ;
2112
2113PrimType
2114 : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
2115 | LONG | ULONG | FLOAT | DOUBLE | LABEL
2116 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002117
2118// Derived types are added later...
Reid Spencera50d5962006-12-02 04:11:07 +00002119UpRTypes
Reid Spencer950bf602007-01-26 08:19:09 +00002120 : PrimType {
Reid Spencered96d1e2007-02-08 09:08:52 +00002121 $$.PAT = new PATypeHolder($1.T);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002122 $$.S.copy($1.S);
Reid Spencera50d5962006-12-02 04:11:07 +00002123 }
Reid Spencer950bf602007-01-26 08:19:09 +00002124 | OPAQUE {
Reid Spencered96d1e2007-02-08 09:08:52 +00002125 $$.PAT = new PATypeHolder(OpaqueType::get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002126 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002127 }
2128 | SymbolicValueRef { // Named types are also simple types...
Reid Spencerbb1fd572007-03-21 17:15:50 +00002129 $$.S.copy(getTypeSign($1));
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00002130 const Type* tmp = getType($1);
Reid Spencered96d1e2007-02-08 09:08:52 +00002131 $$.PAT = new PATypeHolder(tmp);
Reid Spencer78720742006-12-02 20:21:22 +00002132 }
2133 | '\\' EUINT64VAL { // Type UpReference
Reid Spencer950bf602007-01-26 08:19:09 +00002134 if ($2 > (uint64_t)~0U)
2135 error("Value out of range");
2136 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
2137 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencered96d1e2007-02-08 09:08:52 +00002138 $$.PAT = new PATypeHolder(OT);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002139 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002140 UR_OUT("New Upreference!\n");
Reid Spencere7c3c602006-11-30 06:36:44 +00002141 }
2142 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Reid Spencerbb1fd572007-03-21 17:15:50 +00002143 $$.S.makeComposite($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002144 std::vector<const Type*> Params;
2145 for (std::list<llvm::PATypeInfo>::iterator I = $3->begin(),
2146 E = $3->end(); I != E; ++I) {
Reid Spencered96d1e2007-02-08 09:08:52 +00002147 Params.push_back(I->PAT->get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002148 $$.S.add(I->S);
Reid Spencer52402b02007-01-02 05:45:11 +00002149 }
Reid Spencer950bf602007-01-26 08:19:09 +00002150 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
2151 if (isVarArg) Params.pop_back();
2152
Reid Spencer7eea8ff2007-05-18 05:48:07 +00002153 ParamAttrsList *PAL = 0;
2154 if (lastCallingConv == OldCallingConv::CSRet) {
2155 ParamAttrsVector Attrs;
2156 ParamAttrsWithIndex PAWI;
2157 PAWI.index = 1; PAWI.attrs = ParamAttr::StructRet; // first arg
2158 Attrs.push_back(PAWI);
2159 PAL = ParamAttrsList::get(Attrs);
2160 }
2161
Reid Spencer7b5d4662007-04-09 06:16:21 +00002162 const FunctionType *FTy =
Reid Spencer7eea8ff2007-05-18 05:48:07 +00002163 FunctionType::get($1.PAT->get(), Params, isVarArg, PAL);
Reid Spencer7b5d4662007-04-09 06:16:21 +00002164
2165 $$.PAT = new PATypeHolder( HandleUpRefs(FTy, $$.S) );
Reid Spencerbb1fd572007-03-21 17:15:50 +00002166 delete $1.PAT; // Delete the return type handle
Reid Spencer950bf602007-01-26 08:19:09 +00002167 delete $3; // Delete the argument list
Reid Spencere7c3c602006-11-30 06:36:44 +00002168 }
2169 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencerbb1fd572007-03-21 17:15:50 +00002170 $$.S.makeComposite($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002171 $$.PAT = new PATypeHolder(HandleUpRefs(ArrayType::get($4.PAT->get(),
Reid Spencerbb1fd572007-03-21 17:15:50 +00002172 (unsigned)$2), $$.S));
Reid Spencered96d1e2007-02-08 09:08:52 +00002173 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002174 }
Chris Lattner4227bdb2007-02-19 07:34:02 +00002175 | '<' EUINT64VAL 'x' UpRTypes '>' { // Vector type?
Reid Spencerbb1fd572007-03-21 17:15:50 +00002176 const llvm::Type* ElemTy = $4.PAT->get();
2177 if ((unsigned)$2 != $2)
2178 error("Unsigned result not equal to signed result");
2179 if (!(ElemTy->isInteger() || ElemTy->isFloatingPoint()))
2180 error("Elements of a VectorType must be integer or floating point");
2181 if (!isPowerOf2_32($2))
2182 error("VectorType length should be a power of 2");
2183 $$.S.makeComposite($4.S);
2184 $$.PAT = new PATypeHolder(HandleUpRefs(VectorType::get(ElemTy,
2185 (unsigned)$2), $$.S));
2186 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002187 }
2188 | '{' TypeListI '}' { // Structure type?
Reid Spencer950bf602007-01-26 08:19:09 +00002189 std::vector<const Type*> Elements;
Reid Spencerbb1fd572007-03-21 17:15:50 +00002190 $$.S.makeComposite();
Reid Spencer950bf602007-01-26 08:19:09 +00002191 for (std::list<llvm::PATypeInfo>::iterator I = $2->begin(),
Reid Spencerbb1fd572007-03-21 17:15:50 +00002192 E = $2->end(); I != E; ++I) {
Reid Spencered96d1e2007-02-08 09:08:52 +00002193 Elements.push_back(I->PAT->get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002194 $$.S.add(I->S);
2195 }
2196 $$.PAT = new PATypeHolder(HandleUpRefs(StructType::get(Elements), $$.S));
Reid Spencer950bf602007-01-26 08:19:09 +00002197 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00002198 }
2199 | '{' '}' { // Empty structure type?
Reid Spencered96d1e2007-02-08 09:08:52 +00002200 $$.PAT = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencerbb1fd572007-03-21 17:15:50 +00002201 $$.S.makeComposite();
Reid Spencere7c3c602006-11-30 06:36:44 +00002202 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002203 | '<' '{' TypeListI '}' '>' { // Packed Structure type?
Reid Spencerbb1fd572007-03-21 17:15:50 +00002204 $$.S.makeComposite();
Reid Spencer950bf602007-01-26 08:19:09 +00002205 std::vector<const Type*> Elements;
2206 for (std::list<llvm::PATypeInfo>::iterator I = $3->begin(),
2207 E = $3->end(); I != E; ++I) {
Reid Spencered96d1e2007-02-08 09:08:52 +00002208 Elements.push_back(I->PAT->get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002209 $$.S.add(I->S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002210 delete I->PAT;
Reid Spencer52402b02007-01-02 05:45:11 +00002211 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00002212 $$.PAT = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true),
2213 $$.S));
Reid Spencer950bf602007-01-26 08:19:09 +00002214 delete $3;
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002215 }
2216 | '<' '{' '}' '>' { // Empty packed structure type?
Reid Spencered96d1e2007-02-08 09:08:52 +00002217 $$.PAT = new PATypeHolder(StructType::get(std::vector<const Type*>(),true));
Reid Spencerbb1fd572007-03-21 17:15:50 +00002218 $$.S.makeComposite();
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002219 }
Reid Spencere7c3c602006-11-30 06:36:44 +00002220 | UpRTypes '*' { // Pointer type?
Reid Spencered96d1e2007-02-08 09:08:52 +00002221 if ($1.PAT->get() == Type::LabelTy)
Reid Spencer950bf602007-01-26 08:19:09 +00002222 error("Cannot form a pointer to a basic block");
Reid Spencerbb1fd572007-03-21 17:15:50 +00002223 $$.S.makeComposite($1.S);
2224 $$.PAT = new PATypeHolder(HandleUpRefs(PointerType::get($1.PAT->get()),
2225 $$.S));
Reid Spencered96d1e2007-02-08 09:08:52 +00002226 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002227 }
2228 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002229
2230// TypeList - Used for struct declarations and as a basis for function type
2231// declaration type lists
2232//
Reid Spencere77e35e2006-12-01 20:26:20 +00002233TypeListI
2234 : UpRTypes {
Reid Spencer950bf602007-01-26 08:19:09 +00002235 $$ = new std::list<PATypeInfo>();
2236 $$->push_back($1);
Reid Spencere77e35e2006-12-01 20:26:20 +00002237 }
2238 | TypeListI ',' UpRTypes {
Reid Spencer950bf602007-01-26 08:19:09 +00002239 ($$=$1)->push_back($3);
2240 }
2241 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002242
2243// ArgTypeList - List of types for a function type declaration...
Reid Spencere77e35e2006-12-01 20:26:20 +00002244ArgTypeListI
Reid Spencer950bf602007-01-26 08:19:09 +00002245 : TypeListI
Reid Spencere7c3c602006-11-30 06:36:44 +00002246 | TypeListI ',' DOTDOTDOT {
Reid Spencer950bf602007-01-26 08:19:09 +00002247 PATypeInfo VoidTI;
Reid Spencered96d1e2007-02-08 09:08:52 +00002248 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002249 VoidTI.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002250 ($$=$1)->push_back(VoidTI);
Reid Spencere7c3c602006-11-30 06:36:44 +00002251 }
2252 | DOTDOTDOT {
Reid Spencer950bf602007-01-26 08:19:09 +00002253 $$ = new std::list<PATypeInfo>();
2254 PATypeInfo VoidTI;
Reid Spencered96d1e2007-02-08 09:08:52 +00002255 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002256 VoidTI.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002257 $$->push_back(VoidTI);
Reid Spencere7c3c602006-11-30 06:36:44 +00002258 }
2259 | /*empty*/ {
Reid Spencer950bf602007-01-26 08:19:09 +00002260 $$ = new std::list<PATypeInfo>();
2261 }
2262 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002263
2264// ConstVal - The various declarations that go into the constant pool. This
2265// production is used ONLY to represent constants that show up AFTER a 'const',
2266// 'constant' or 'global' token at global scope. Constants that can be inlined
2267// into other expressions (such as integers and constexprs) are handled by the
2268// ResolvedVal, ValueRef and ConstValueRef productions.
2269//
Reid Spencer950bf602007-01-26 08:19:09 +00002270ConstVal
2271 : Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencered96d1e2007-02-08 09:08:52 +00002272 const ArrayType *ATy = dyn_cast<ArrayType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002273 if (ATy == 0)
2274 error("Cannot make array constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002275 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002276 const Type *ETy = ATy->getElementType();
2277 int NumElements = ATy->getNumElements();
2278
2279 // Verify that we have the correct size...
2280 if (NumElements != -1 && NumElements != (int)$3->size())
2281 error("Type mismatch: constant sized array initialized with " +
2282 utostr($3->size()) + " arguments, but has size of " +
2283 itostr(NumElements) + "");
2284
2285 // Verify all elements are correct type!
2286 std::vector<Constant*> Elems;
2287 for (unsigned i = 0; i < $3->size(); i++) {
2288 Constant *C = (*$3)[i].C;
2289 const Type* ValTy = C->getType();
2290 if (ETy != ValTy)
2291 error("Element #" + utostr(i) + " is not of type '" +
2292 ETy->getDescription() +"' as required!\nIt is of type '"+
2293 ValTy->getDescription() + "'");
2294 Elems.push_back(C);
2295 }
2296 $$.C = ConstantArray::get(ATy, Elems);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002297 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002298 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002299 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002300 }
2301 | Types '[' ']' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002302 const ArrayType *ATy = dyn_cast<ArrayType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002303 if (ATy == 0)
2304 error("Cannot make array constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002305 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002306 int NumElements = ATy->getNumElements();
2307 if (NumElements != -1 && NumElements != 0)
2308 error("Type mismatch: constant sized array initialized with 0"
2309 " arguments, but has size of " + itostr(NumElements) +"");
2310 $$.C = ConstantArray::get(ATy, std::vector<Constant*>());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002311 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002312 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002313 }
2314 | Types 'c' STRINGCONSTANT {
Reid Spencered96d1e2007-02-08 09:08:52 +00002315 const ArrayType *ATy = dyn_cast<ArrayType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002316 if (ATy == 0)
2317 error("Cannot make array constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002318 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002319 int NumElements = ATy->getNumElements();
2320 const Type *ETy = dyn_cast<IntegerType>(ATy->getElementType());
2321 if (!ETy || cast<IntegerType>(ETy)->getBitWidth() != 8)
2322 error("String arrays require type i8, not '" + ETy->getDescription() +
2323 "'");
2324 char *EndStr = UnEscapeLexed($3, true);
2325 if (NumElements != -1 && NumElements != (EndStr-$3))
2326 error("Can't build string constant of size " +
2327 itostr((int)(EndStr-$3)) + " when array has size " +
2328 itostr(NumElements) + "");
2329 std::vector<Constant*> Vals;
2330 for (char *C = (char *)$3; C != (char *)EndStr; ++C)
2331 Vals.push_back(ConstantInt::get(ETy, *C));
2332 free($3);
2333 $$.C = ConstantArray::get(ATy, Vals);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002334 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002335 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002336 }
2337 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer9d6565a2007-02-15 02:26:10 +00002338 const VectorType *PTy = dyn_cast<VectorType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002339 if (PTy == 0)
2340 error("Cannot make packed constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002341 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002342 const Type *ETy = PTy->getElementType();
2343 int NumElements = PTy->getNumElements();
2344 // Verify that we have the correct size...
2345 if (NumElements != -1 && NumElements != (int)$3->size())
2346 error("Type mismatch: constant sized packed initialized with " +
2347 utostr($3->size()) + " arguments, but has size of " +
2348 itostr(NumElements) + "");
2349 // Verify all elements are correct type!
2350 std::vector<Constant*> Elems;
2351 for (unsigned i = 0; i < $3->size(); i++) {
2352 Constant *C = (*$3)[i].C;
2353 const Type* ValTy = C->getType();
2354 if (ETy != ValTy)
2355 error("Element #" + utostr(i) + " is not of type '" +
2356 ETy->getDescription() +"' as required!\nIt is of type '"+
2357 ValTy->getDescription() + "'");
2358 Elems.push_back(C);
2359 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00002360 $$.C = ConstantVector::get(PTy, Elems);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002361 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002362 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002363 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002364 }
2365 | Types '{' ConstVector '}' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002366 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002367 if (STy == 0)
2368 error("Cannot make struct constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002369 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002370 if ($3->size() != STy->getNumContainedTypes())
2371 error("Illegal number of initializers for structure type");
2372
2373 // Check to ensure that constants are compatible with the type initializer!
2374 std::vector<Constant*> Fields;
2375 for (unsigned i = 0, e = $3->size(); i != e; ++i) {
2376 Constant *C = (*$3)[i].C;
2377 if (C->getType() != STy->getElementType(i))
2378 error("Expected type '" + STy->getElementType(i)->getDescription() +
2379 "' for element #" + utostr(i) + " of structure initializer");
2380 Fields.push_back(C);
2381 }
2382 $$.C = ConstantStruct::get(STy, Fields);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002383 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002384 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002385 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002386 }
2387 | Types '{' '}' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002388 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002389 if (STy == 0)
2390 error("Cannot make struct constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002391 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002392 if (STy->getNumContainedTypes() != 0)
2393 error("Illegal number of initializers for structure type");
2394 $$.C = ConstantStruct::get(STy, std::vector<Constant*>());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002395 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002396 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002397 }
Reid Spencer950bf602007-01-26 08:19:09 +00002398 | Types '<' '{' ConstVector '}' '>' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002399 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002400 if (STy == 0)
2401 error("Cannot make packed struct constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002402 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002403 if ($4->size() != STy->getNumContainedTypes())
2404 error("Illegal number of initializers for packed structure type");
Reid Spencere7c3c602006-11-30 06:36:44 +00002405
Reid Spencer950bf602007-01-26 08:19:09 +00002406 // Check to ensure that constants are compatible with the type initializer!
2407 std::vector<Constant*> Fields;
2408 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
2409 Constant *C = (*$4)[i].C;
2410 if (C->getType() != STy->getElementType(i))
2411 error("Expected type '" + STy->getElementType(i)->getDescription() +
2412 "' for element #" + utostr(i) + " of packed struct initializer");
2413 Fields.push_back(C);
Reid Spencer280d8012006-12-01 23:40:53 +00002414 }
Reid Spencer950bf602007-01-26 08:19:09 +00002415 $$.C = ConstantStruct::get(STy, Fields);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002416 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002417 delete $1.PAT;
Reid Spencere77e35e2006-12-01 20:26:20 +00002418 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00002419 }
Reid Spencer950bf602007-01-26 08:19:09 +00002420 | Types '<' '{' '}' '>' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002421 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002422 if (STy == 0)
2423 error("Cannot make packed struct constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002424 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002425 if (STy->getNumContainedTypes() != 0)
2426 error("Illegal number of initializers for packed structure type");
2427 $$.C = ConstantStruct::get(STy, std::vector<Constant*>());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002428 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002429 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002430 }
2431 | Types NULL_TOK {
Reid Spencered96d1e2007-02-08 09:08:52 +00002432 const PointerType *PTy = dyn_cast<PointerType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002433 if (PTy == 0)
2434 error("Cannot make null pointer constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002435 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002436 $$.C = ConstantPointerNull::get(PTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002437 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002438 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002439 }
2440 | Types UNDEF {
Reid Spencered96d1e2007-02-08 09:08:52 +00002441 $$.C = UndefValue::get($1.PAT->get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002442 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002443 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002444 }
2445 | Types SymbolicValueRef {
Reid Spencered96d1e2007-02-08 09:08:52 +00002446 const PointerType *Ty = dyn_cast<PointerType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002447 if (Ty == 0)
2448 error("Global const reference must be a pointer type, not" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002449 $1.PAT->get()->getDescription());
Reid Spencer950bf602007-01-26 08:19:09 +00002450
2451 // ConstExprs can exist in the body of a function, thus creating
2452 // GlobalValues whenever they refer to a variable. Because we are in
2453 // the context of a function, getExistingValue will search the functions
2454 // symbol table instead of the module symbol table for the global symbol,
2455 // which throws things all off. To get around this, we just tell
2456 // getExistingValue that we are at global scope here.
2457 //
2458 Function *SavedCurFn = CurFun.CurrentFunction;
2459 CurFun.CurrentFunction = 0;
Reid Spencerbb1fd572007-03-21 17:15:50 +00002460 $2.S.copy($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002461 Value *V = getExistingValue(Ty, $2);
2462 CurFun.CurrentFunction = SavedCurFn;
2463
2464 // If this is an initializer for a constant pointer, which is referencing a
2465 // (currently) undefined variable, create a stub now that shall be replaced
2466 // in the future with the right type of variable.
2467 //
2468 if (V == 0) {
2469 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers");
2470 const PointerType *PT = cast<PointerType>(Ty);
2471
2472 // First check to see if the forward references value is already created!
2473 PerModuleInfo::GlobalRefsType::iterator I =
2474 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
2475
2476 if (I != CurModule.GlobalRefs.end()) {
2477 V = I->second; // Placeholder already exists, use it...
2478 $2.destroy();
2479 } else {
2480 std::string Name;
2481 if ($2.Type == ValID::NameVal) Name = $2.Name;
2482
2483 // Create the forward referenced global.
2484 GlobalValue *GV;
2485 if (const FunctionType *FTy =
2486 dyn_cast<FunctionType>(PT->getElementType())) {
2487 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
2488 CurModule.CurrentModule);
2489 } else {
2490 GV = new GlobalVariable(PT->getElementType(), false,
2491 GlobalValue::ExternalLinkage, 0,
2492 Name, CurModule.CurrentModule);
2493 }
2494
2495 // Keep track of the fact that we have a forward ref to recycle it
2496 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
2497 V = GV;
2498 }
2499 }
2500 $$.C = cast<GlobalValue>(V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002501 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002502 delete $1.PAT; // Free the type handle
Reid Spencer950bf602007-01-26 08:19:09 +00002503 }
2504 | Types ConstExpr {
Reid Spencered96d1e2007-02-08 09:08:52 +00002505 if ($1.PAT->get() != $2.C->getType())
Reid Spencer950bf602007-01-26 08:19:09 +00002506 error("Mismatched types for constant expression");
2507 $$ = $2;
Reid Spencerbb1fd572007-03-21 17:15:50 +00002508 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002509 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002510 }
2511 | Types ZEROINITIALIZER {
Reid Spencered96d1e2007-02-08 09:08:52 +00002512 const Type *Ty = $1.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00002513 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
2514 error("Cannot create a null initialized value of this type");
2515 $$.C = Constant::getNullValue(Ty);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002516 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002517 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002518 }
2519 | SIntType EINT64VAL { // integral constants
2520 const Type *Ty = $1.T;
2521 if (!ConstantInt::isValueValidForType(Ty, $2))
2522 error("Constant value doesn't fit in type");
2523 $$.C = ConstantInt::get(Ty, $2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002524 $$.S.makeSigned();
Reid Spencer950bf602007-01-26 08:19:09 +00002525 }
2526 | UIntType EUINT64VAL { // integral constants
2527 const Type *Ty = $1.T;
2528 if (!ConstantInt::isValueValidForType(Ty, $2))
2529 error("Constant value doesn't fit in type");
2530 $$.C = ConstantInt::get(Ty, $2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002531 $$.S.makeUnsigned();
Reid Spencer950bf602007-01-26 08:19:09 +00002532 }
2533 | BOOL TRUETOK { // Boolean constants
2534 $$.C = ConstantInt::get(Type::Int1Ty, true);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002535 $$.S.makeUnsigned();
Reid Spencer950bf602007-01-26 08:19:09 +00002536 }
2537 | BOOL FALSETOK { // Boolean constants
2538 $$.C = ConstantInt::get(Type::Int1Ty, false);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002539 $$.S.makeUnsigned();
Reid Spencer950bf602007-01-26 08:19:09 +00002540 }
2541 | FPType FPVAL { // Float & Double constants
2542 if (!ConstantFP::isValueValidForType($1.T, $2))
2543 error("Floating point constant invalid for type");
2544 $$.C = ConstantFP::get($1.T, $2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002545 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002546 }
2547 ;
2548
2549ConstExpr
2550 : CastOps '(' ConstVal TO Types ')' {
2551 const Type* SrcTy = $3.C->getType();
Reid Spencered96d1e2007-02-08 09:08:52 +00002552 const Type* DstTy = $5.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00002553 Signedness SrcSign($3.S);
2554 Signedness DstSign($5.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002555 if (!SrcTy->isFirstClassType())
2556 error("cast constant expression from a non-primitive type: '" +
2557 SrcTy->getDescription() + "'");
2558 if (!DstTy->isFirstClassType())
2559 error("cast constant expression to a non-primitive type: '" +
2560 DstTy->getDescription() + "'");
2561 $$.C = cast<Constant>(getCast($1, $3.C, SrcSign, DstTy, DstSign));
Reid Spencerbb1fd572007-03-21 17:15:50 +00002562 $$.S.copy(DstSign);
Reid Spencered96d1e2007-02-08 09:08:52 +00002563 delete $5.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002564 }
2565 | GETELEMENTPTR '(' ConstVal IndexList ')' {
2566 const Type *Ty = $3.C->getType();
2567 if (!isa<PointerType>(Ty))
2568 error("GetElementPtr requires a pointer operand");
2569
Reid Spencer950bf602007-01-26 08:19:09 +00002570 std::vector<Constant*> CIndices;
Reid Spencerff0e4482007-04-16 00:40:57 +00002571 upgradeGEPCEIndices($3.C->getType(), $4, CIndices);
Reid Spencer950bf602007-01-26 08:19:09 +00002572
2573 delete $4;
Chris Lattner4227bdb2007-02-19 07:34:02 +00002574 $$.C = ConstantExpr::getGetElementPtr($3.C, &CIndices[0], CIndices.size());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002575 $$.S.copy(getElementSign($3, CIndices));
Reid Spencer950bf602007-01-26 08:19:09 +00002576 }
Reid Spencere7c3c602006-11-30 06:36:44 +00002577 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002578 if (!$3.C->getType()->isInteger() ||
2579 cast<IntegerType>($3.C->getType())->getBitWidth() != 1)
2580 error("Select condition must be bool type");
2581 if ($5.C->getType() != $7.C->getType())
2582 error("Select operand types must match");
2583 $$.C = ConstantExpr::getSelect($3.C, $5.C, $7.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002584 $$.S.copy($5.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002585 }
2586 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002587 const Type *Ty = $3.C->getType();
2588 if (Ty != $5.C->getType())
2589 error("Binary operator types must match");
2590 // First, make sure we're dealing with the right opcode by upgrading from
2591 // obsolete versions.
2592 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $3.S);
2593
2594 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
2595 // To retain backward compatibility with these early compilers, we emit a
2596 // cast to the appropriate integer type automatically if we are in the
2597 // broken case. See PR424 for more information.
2598 if (!isa<PointerType>(Ty)) {
2599 $$.C = ConstantExpr::get(Opcode, $3.C, $5.C);
2600 } else {
2601 const Type *IntPtrTy = 0;
2602 switch (CurModule.CurrentModule->getPointerSize()) {
2603 case Module::Pointer32: IntPtrTy = Type::Int32Ty; break;
2604 case Module::Pointer64: IntPtrTy = Type::Int64Ty; break;
2605 default: error("invalid pointer binary constant expr");
2606 }
2607 $$.C = ConstantExpr::get(Opcode,
2608 ConstantExpr::getCast(Instruction::PtrToInt, $3.C, IntPtrTy),
2609 ConstantExpr::getCast(Instruction::PtrToInt, $5.C, IntPtrTy));
2610 $$.C = ConstantExpr::getCast(Instruction::IntToPtr, $$.C, Ty);
2611 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00002612 $$.S.copy($3.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002613 }
2614 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002615 const Type* Ty = $3.C->getType();
2616 if (Ty != $5.C->getType())
2617 error("Logical operator types must match");
2618 if (!Ty->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00002619 if (!isa<VectorType>(Ty) ||
2620 !cast<VectorType>(Ty)->getElementType()->isInteger())
Reid Spencer950bf602007-01-26 08:19:09 +00002621 error("Logical operator requires integer operands");
2622 }
2623 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $3.S);
2624 $$.C = ConstantExpr::get(Opcode, $3.C, $5.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002625 $$.S.copy($3.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002626 }
2627 | SetCondOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002628 const Type* Ty = $3.C->getType();
2629 if (Ty != $5.C->getType())
2630 error("setcc operand types must match");
2631 unsigned short pred;
2632 Instruction::OtherOps Opcode = getCompareOp($1, pred, Ty, $3.S);
2633 $$.C = ConstantExpr::getCompare(Opcode, $3.C, $5.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002634 $$.S.makeUnsigned();
Reid Spencere7c3c602006-11-30 06:36:44 +00002635 }
Reid Spencer57f28f92006-12-03 07:10:26 +00002636 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002637 if ($4.C->getType() != $6.C->getType())
2638 error("icmp operand types must match");
2639 $$.C = ConstantExpr::getCompare($2, $4.C, $6.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002640 $$.S.makeUnsigned();
Reid Spencer57f28f92006-12-03 07:10:26 +00002641 }
2642 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002643 if ($4.C->getType() != $6.C->getType())
2644 error("fcmp operand types must match");
2645 $$.C = ConstantExpr::getCompare($2, $4.C, $6.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002646 $$.S.makeUnsigned();
Reid Spencer229e9362006-12-02 22:14:11 +00002647 }
Reid Spencere7c3c602006-11-30 06:36:44 +00002648 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002649 if (!$5.C->getType()->isInteger() ||
2650 cast<IntegerType>($5.C->getType())->getBitWidth() != 8)
2651 error("Shift count for shift constant must be unsigned byte");
Reid Spencer832254e2007-02-02 02:16:23 +00002652 const Type* Ty = $3.C->getType();
Reid Spencer950bf602007-01-26 08:19:09 +00002653 if (!$3.C->getType()->isInteger())
2654 error("Shift constant expression requires integer operand");
Reid Spencer832254e2007-02-02 02:16:23 +00002655 Constant *ShiftAmt = ConstantExpr::getZExt($5.C, Ty);
2656 $$.C = ConstantExpr::get(getBinaryOp($1, Ty, $3.S), $3.C, ShiftAmt);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002657 $$.S.copy($3.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002658 }
2659 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002660 if (!ExtractElementInst::isValidOperands($3.C, $5.C))
2661 error("Invalid extractelement operands");
2662 $$.C = ConstantExpr::getExtractElement($3.C, $5.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002663 $$.S.copy($3.S.get(0));
Reid Spencere7c3c602006-11-30 06:36:44 +00002664 }
2665 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002666 if (!InsertElementInst::isValidOperands($3.C, $5.C, $7.C))
2667 error("Invalid insertelement operands");
2668 $$.C = ConstantExpr::getInsertElement($3.C, $5.C, $7.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002669 $$.S.copy($3.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002670 }
2671 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002672 if (!ShuffleVectorInst::isValidOperands($3.C, $5.C, $7.C))
2673 error("Invalid shufflevector operands");
2674 $$.C = ConstantExpr::getShuffleVector($3.C, $5.C, $7.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002675 $$.S.copy($3.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002676 }
2677 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002678
2679
2680// ConstVector - A list of comma separated constants.
Reid Spencere77e35e2006-12-01 20:26:20 +00002681ConstVector
Reid Spencer950bf602007-01-26 08:19:09 +00002682 : ConstVector ',' ConstVal { ($$ = $1)->push_back($3); }
2683 | ConstVal {
2684 $$ = new std::vector<ConstInfo>();
2685 $$->push_back($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00002686 }
Reid Spencere77e35e2006-12-01 20:26:20 +00002687 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002688
2689
2690// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Reid Spencer950bf602007-01-26 08:19:09 +00002691GlobalType
2692 : GLOBAL { $$ = false; }
2693 | CONSTANT { $$ = true; }
2694 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002695
2696
2697//===----------------------------------------------------------------------===//
2698// Rules to match Modules
2699//===----------------------------------------------------------------------===//
2700
2701// Module rule: Capture the result of parsing the whole file into a result
2702// variable...
2703//
Reid Spencer950bf602007-01-26 08:19:09 +00002704Module
2705 : FunctionList {
2706 $$ = ParserResult = $1;
2707 CurModule.ModuleDone();
Reid Spencere7c3c602006-11-30 06:36:44 +00002708 }
Jeff Cohenac2dca92007-01-21 19:30:52 +00002709 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002710
Reid Spencer950bf602007-01-26 08:19:09 +00002711// FunctionList - A list of functions, preceeded by a constant pool.
2712//
2713FunctionList
2714 : FunctionList Function { $$ = $1; CurFun.FunctionDone(); }
2715 | FunctionList FunctionProto { $$ = $1; }
2716 | FunctionList MODULE ASM_TOK AsmBlock { $$ = $1; }
2717 | FunctionList IMPLEMENTATION { $$ = $1; }
2718 | ConstPool {
2719 $$ = CurModule.CurrentModule;
2720 // Emit an error if there are any unresolved types left.
2721 if (!CurModule.LateResolveTypes.empty()) {
2722 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
2723 if (DID.Type == ValID::NameVal) {
2724 error("Reference to an undefined type: '"+DID.getName() + "'");
2725 } else {
2726 error("Reference to an undefined type: #" + itostr(DID.Num));
2727 }
2728 }
2729 }
2730 ;
Reid Spencer78720742006-12-02 20:21:22 +00002731
Reid Spencere7c3c602006-11-30 06:36:44 +00002732// ConstPool - Constants with optional names assigned to them.
Reid Spencer950bf602007-01-26 08:19:09 +00002733ConstPool
2734 : ConstPool OptAssign TYPE TypesV {
2735 // Eagerly resolve types. This is not an optimization, this is a
2736 // requirement that is due to the fact that we could have this:
2737 //
2738 // %list = type { %list * }
2739 // %list = type { %list * } ; repeated type decl
2740 //
2741 // If types are not resolved eagerly, then the two types will not be
2742 // determined to be the same type!
2743 //
Reid Spencerbb1fd572007-03-21 17:15:50 +00002744 ResolveTypeTo($2, $4.PAT->get(), $4.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002745
Reid Spencerbb1fd572007-03-21 17:15:50 +00002746 if (!setTypeName($4, $2) && !$2) {
2747 // If this is a numbered type that is not a redefinition, add it to the
2748 // slot table.
2749 CurModule.Types.push_back($4.PAT->get());
2750 CurModule.TypeSigns.push_back($4.S);
Reid Spencera50d5962006-12-02 04:11:07 +00002751 }
Reid Spencered96d1e2007-02-08 09:08:52 +00002752 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002753 }
2754 | ConstPool FunctionProto { // Function prototypes can be in const pool
Reid Spencere7c3c602006-11-30 06:36:44 +00002755 }
2756 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
Reid Spencere7c3c602006-11-30 06:36:44 +00002757 }
Reid Spencer950bf602007-01-26 08:19:09 +00002758 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
2759 if ($5.C == 0)
2760 error("Global value initializer is not a constant");
Reid Spencerbb1fd572007-03-21 17:15:50 +00002761 CurGV = ParseGlobalVariable($2, $3, $4, $5.C->getType(), $5.C, $5.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002762 } GlobalVarAttributes {
2763 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002764 }
Reid Spencer950bf602007-01-26 08:19:09 +00002765 | ConstPool OptAssign EXTERNAL GlobalType Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00002766 const Type *Ty = $5.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00002767 CurGV = ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $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 }
Reid Spencer950bf602007-01-26 08:19:09 +00002773 | ConstPool OptAssign DLLIMPORT GlobalType Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00002774 const Type *Ty = $5.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00002775 CurGV = ParseGlobalVariable($2, GlobalValue::DLLImportLinkage, $4, Ty, 0,
2776 $5.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002777 delete $5.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002778 } GlobalVarAttributes {
2779 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002780 }
Reid Spencer950bf602007-01-26 08:19:09 +00002781 | ConstPool OptAssign EXTERN_WEAK GlobalType Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00002782 const Type *Ty = $5.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00002783 CurGV =
Reid Spencerbb1fd572007-03-21 17:15:50 +00002784 ParseGlobalVariable($2, GlobalValue::ExternalWeakLinkage, $4, Ty, 0,
2785 $5.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002786 delete $5.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002787 } GlobalVarAttributes {
2788 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002789 }
2790 | ConstPool TARGET TargetDefinition {
Reid Spencere7c3c602006-11-30 06:36:44 +00002791 }
2792 | ConstPool DEPLIBS '=' LibrariesDefinition {
Reid Spencere7c3c602006-11-30 06:36:44 +00002793 }
2794 | /* empty: end of list */ {
Reid Spencer950bf602007-01-26 08:19:09 +00002795 }
2796 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002797
Reid Spencer950bf602007-01-26 08:19:09 +00002798AsmBlock
2799 : STRINGCONSTANT {
2800 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
2801 char *EndStr = UnEscapeLexed($1, true);
2802 std::string NewAsm($1, EndStr);
2803 free($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00002804
Reid Spencer950bf602007-01-26 08:19:09 +00002805 if (AsmSoFar.empty())
2806 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
2807 else
2808 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
2809 }
2810 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002811
Reid Spencer950bf602007-01-26 08:19:09 +00002812BigOrLittle
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00002813 : BIG { $$ = Module::BigEndian; }
Reid Spencer950bf602007-01-26 08:19:09 +00002814 | LITTLE { $$ = Module::LittleEndian; }
2815 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002816
2817TargetDefinition
2818 : ENDIAN '=' BigOrLittle {
Reid Spencer950bf602007-01-26 08:19:09 +00002819 CurModule.setEndianness($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00002820 }
2821 | POINTERSIZE '=' EUINT64VAL {
Reid Spencer950bf602007-01-26 08:19:09 +00002822 if ($3 == 32)
2823 CurModule.setPointerSize(Module::Pointer32);
2824 else if ($3 == 64)
2825 CurModule.setPointerSize(Module::Pointer64);
2826 else
2827 error("Invalid pointer size: '" + utostr($3) + "'");
Reid Spencere7c3c602006-11-30 06:36:44 +00002828 }
2829 | TRIPLE '=' STRINGCONSTANT {
Reid Spencer950bf602007-01-26 08:19:09 +00002830 CurModule.CurrentModule->setTargetTriple($3);
2831 free($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00002832 }
2833 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer950bf602007-01-26 08:19:09 +00002834 CurModule.CurrentModule->setDataLayout($3);
2835 free($3);
2836 }
2837 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002838
2839LibrariesDefinition
Reid Spencer950bf602007-01-26 08:19:09 +00002840 : '[' LibList ']'
2841 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002842
2843LibList
2844 : LibList ',' STRINGCONSTANT {
Reid Spencer950bf602007-01-26 08:19:09 +00002845 CurModule.CurrentModule->addLibrary($3);
2846 free($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00002847 }
Reid Spencer950bf602007-01-26 08:19:09 +00002848 | STRINGCONSTANT {
2849 CurModule.CurrentModule->addLibrary($1);
2850 free($1);
2851 }
2852 | /* empty: end of list */ { }
2853 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002854
2855//===----------------------------------------------------------------------===//
2856// Rules to match Function Headers
2857//===----------------------------------------------------------------------===//
2858
Reid Spencer950bf602007-01-26 08:19:09 +00002859Name
2860 : VAR_ID | STRINGCONSTANT
2861 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002862
Reid Spencer950bf602007-01-26 08:19:09 +00002863OptName
2864 : Name
2865 | /*empty*/ { $$ = 0; }
2866 ;
2867
2868ArgVal
2869 : Types OptName {
Reid Spencered96d1e2007-02-08 09:08:52 +00002870 if ($1.PAT->get() == Type::VoidTy)
Reid Spencer950bf602007-01-26 08:19:09 +00002871 error("void typed arguments are invalid");
2872 $$ = new std::pair<PATypeInfo, char*>($1, $2);
Reid Spencer52402b02007-01-02 05:45:11 +00002873 }
Reid Spencer950bf602007-01-26 08:19:09 +00002874 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002875
Reid Spencer950bf602007-01-26 08:19:09 +00002876ArgListH
2877 : ArgListH ',' ArgVal {
2878 $$ = $1;
2879 $$->push_back(*$3);
Reid Spencere77e35e2006-12-01 20:26:20 +00002880 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002881 }
2882 | ArgVal {
Reid Spencer950bf602007-01-26 08:19:09 +00002883 $$ = new std::vector<std::pair<PATypeInfo,char*> >();
2884 $$->push_back(*$1);
2885 delete $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00002886 }
Reid Spencer950bf602007-01-26 08:19:09 +00002887 ;
2888
2889ArgList
2890 : ArgListH { $$ = $1; }
Reid Spencere7c3c602006-11-30 06:36:44 +00002891 | ArgListH ',' DOTDOTDOT {
Reid Spencere7c3c602006-11-30 06:36:44 +00002892 $$ = $1;
Reid Spencer950bf602007-01-26 08:19:09 +00002893 PATypeInfo VoidTI;
Reid Spencered96d1e2007-02-08 09:08:52 +00002894 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002895 VoidTI.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002896 $$->push_back(std::pair<PATypeInfo, char*>(VoidTI, 0));
Reid Spencere7c3c602006-11-30 06:36:44 +00002897 }
2898 | DOTDOTDOT {
Reid Spencer950bf602007-01-26 08:19:09 +00002899 $$ = new std::vector<std::pair<PATypeInfo,char*> >();
2900 PATypeInfo VoidTI;
Reid Spencered96d1e2007-02-08 09:08:52 +00002901 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002902 VoidTI.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002903 $$->push_back(std::pair<PATypeInfo, char*>(VoidTI, 0));
Reid Spencere7c3c602006-11-30 06:36:44 +00002904 }
Reid Spencer950bf602007-01-26 08:19:09 +00002905 | /* empty */ { $$ = 0; }
2906 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002907
Reid Spencer71d2ec92006-12-31 06:02:26 +00002908FunctionHeaderH
2909 : OptCallingConv TypesV Name '(' ArgList ')' OptSection OptAlign {
Reid Spencer950bf602007-01-26 08:19:09 +00002910 UnEscapeLexed($3);
2911 std::string FunctionName($3);
2912 free($3); // Free strdup'd memory!
Reid Spencere7c3c602006-11-30 06:36:44 +00002913
Reid Spencered96d1e2007-02-08 09:08:52 +00002914 const Type* RetTy = $2.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00002915
2916 if (!RetTy->isFirstClassType() && RetTy != Type::VoidTy)
2917 error("LLVM functions cannot return aggregate types");
2918
Reid Spencerbb1fd572007-03-21 17:15:50 +00002919 Signedness FTySign;
2920 FTySign.makeComposite($2.S);
Reid Spenceref9b9a72007-02-05 20:47:22 +00002921 std::vector<const Type*> ParamTyList;
Reid Spencer950bf602007-01-26 08:19:09 +00002922
2923 // In LLVM 2.0 the signatures of three varargs intrinsics changed to take
2924 // i8*. We check here for those names and override the parameter list
2925 // types to ensure the prototype is correct.
2926 if (FunctionName == "llvm.va_start" || FunctionName == "llvm.va_end") {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002927 ParamTyList.push_back(PointerType::get(Type::Int8Ty));
Reid Spencer950bf602007-01-26 08:19:09 +00002928 } else if (FunctionName == "llvm.va_copy") {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002929 ParamTyList.push_back(PointerType::get(Type::Int8Ty));
2930 ParamTyList.push_back(PointerType::get(Type::Int8Ty));
Reid Spencer950bf602007-01-26 08:19:09 +00002931 } else if ($5) { // If there are arguments...
2932 for (std::vector<std::pair<PATypeInfo,char*> >::iterator
2933 I = $5->begin(), E = $5->end(); I != E; ++I) {
Reid Spencered96d1e2007-02-08 09:08:52 +00002934 const Type *Ty = I->first.PAT->get();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002935 ParamTyList.push_back(Ty);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002936 FTySign.add(I->first.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002937 }
2938 }
2939
Reid Spenceref9b9a72007-02-05 20:47:22 +00002940 bool isVarArg = ParamTyList.size() && ParamTyList.back() == Type::VoidTy;
2941 if (isVarArg)
2942 ParamTyList.pop_back();
Reid Spencer950bf602007-01-26 08:19:09 +00002943
Reid Spencerb7046c72007-01-29 05:41:34 +00002944 // Convert the CSRet calling convention into the corresponding parameter
2945 // attribute.
Reid Spencer7eea8ff2007-05-18 05:48:07 +00002946 ParamAttrsList *PAL = 0;
Reid Spencerb7046c72007-01-29 05:41:34 +00002947 if ($1 == OldCallingConv::CSRet) {
Reid Spencer7eea8ff2007-05-18 05:48:07 +00002948 ParamAttrsVector Attrs;
2949 ParamAttrsWithIndex PAWI;
2950 PAWI.index = 1; PAWI.attrs = ParamAttr::StructRet; // first arg
2951 Attrs.push_back(PAWI);
2952 PAL = ParamAttrsList::get(Attrs);
Reid Spencerb7046c72007-01-29 05:41:34 +00002953 }
2954
Reid Spencer7b5d4662007-04-09 06:16:21 +00002955 const FunctionType *FT =
Reid Spencer7eea8ff2007-05-18 05:48:07 +00002956 FunctionType::get(RetTy, ParamTyList, isVarArg, PAL);
Reid Spencer950bf602007-01-26 08:19:09 +00002957 const PointerType *PFT = PointerType::get(FT);
Reid Spencered96d1e2007-02-08 09:08:52 +00002958 delete $2.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002959
2960 ValID ID;
2961 if (!FunctionName.empty()) {
Reid Spencer5eb77c72007-03-15 03:26:42 +00002962 ID = ValID::create((char*)FunctionName.c_str());
Reid Spencer950bf602007-01-26 08:19:09 +00002963 } else {
Reid Spencer5eb77c72007-03-15 03:26:42 +00002964 ID = ValID::create((int)CurModule.Values[PFT].size());
Reid Spencer950bf602007-01-26 08:19:09 +00002965 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00002966 ID.S.makeComposite(FTySign);
Reid Spencer950bf602007-01-26 08:19:09 +00002967
2968 Function *Fn = 0;
Reid Spencered96d1e2007-02-08 09:08:52 +00002969 Module* M = CurModule.CurrentModule;
2970
Reid Spencer950bf602007-01-26 08:19:09 +00002971 // See if this function was forward referenced. If so, recycle the object.
2972 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2973 // Move the function to the end of the list, from whereever it was
2974 // previously inserted.
2975 Fn = cast<Function>(FWRef);
Reid Spencered96d1e2007-02-08 09:08:52 +00002976 M->getFunctionList().remove(Fn);
2977 M->getFunctionList().push_back(Fn);
2978 } else if (!FunctionName.empty()) {
2979 GlobalValue *Conflict = M->getFunction(FunctionName);
2980 if (!Conflict)
2981 Conflict = M->getNamedGlobal(FunctionName);
2982 if (Conflict && PFT == Conflict->getType()) {
2983 if (!CurFun.isDeclare && !Conflict->isDeclaration()) {
2984 // We have two function definitions that conflict, same type, same
2985 // name. We should really check to make sure that this is the result
2986 // of integer type planes collapsing and generate an error if it is
2987 // not, but we'll just rename on the assumption that it is. However,
2988 // let's do it intelligently and rename the internal linkage one
2989 // if there is one.
2990 std::string NewName(makeNameUnique(FunctionName));
2991 if (Conflict->hasInternalLinkage()) {
2992 Conflict->setName(NewName);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002993 RenameMapKey Key =
2994 makeRenameMapKey(FunctionName, Conflict->getType(), ID.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002995 CurModule.RenameMap[Key] = NewName;
2996 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
2997 InsertValue(Fn, CurModule.Values);
2998 } else {
2999 Fn = new Function(FT, CurFun.Linkage, NewName, M);
3000 InsertValue(Fn, CurModule.Values);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003001 RenameMapKey Key =
3002 makeRenameMapKey(FunctionName, PFT, ID.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003003 CurModule.RenameMap[Key] = NewName;
3004 }
3005 } else {
3006 // If they are not both definitions, then just use the function we
3007 // found since the types are the same.
3008 Fn = cast<Function>(Conflict);
Reid Spenceref9b9a72007-02-05 20:47:22 +00003009
Reid Spencered96d1e2007-02-08 09:08:52 +00003010 // Make sure to strip off any argument names so we can't get
3011 // conflicts.
3012 if (Fn->isDeclaration())
3013 for (Function::arg_iterator AI = Fn->arg_begin(),
3014 AE = Fn->arg_end(); AI != AE; ++AI)
3015 AI->setName("");
3016 }
3017 } else if (Conflict) {
Reid Spencere59f4932007-04-16 03:05:01 +00003018 // We have two globals with the same name and different types.
Reid Spencered96d1e2007-02-08 09:08:52 +00003019 // Previously, this was permitted because the symbol table had
3020 // "type planes" and names only needed to be distinct within a
3021 // type plane. After PR411 was fixed, this is no loner the case.
3022 // To resolve this we must rename one of the two.
3023 if (Conflict->hasInternalLinkage()) {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003024 // We can safely rename the Conflict.
3025 RenameMapKey Key =
3026 makeRenameMapKey(Conflict->getName(), Conflict->getType(),
3027 CurModule.NamedValueSigns[Conflict->getName()]);
Reid Spencered96d1e2007-02-08 09:08:52 +00003028 Conflict->setName(makeNameUnique(Conflict->getName()));
Reid Spencered96d1e2007-02-08 09:08:52 +00003029 CurModule.RenameMap[Key] = Conflict->getName();
3030 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
3031 InsertValue(Fn, CurModule.Values);
Reid Spencerd2920cd2007-03-21 17:27:53 +00003032 } else {
Reid Spencered96d1e2007-02-08 09:08:52 +00003033 // We can't quietly rename either of these things, but we must
Reid Spencerd2920cd2007-03-21 17:27:53 +00003034 // rename one of them. Only if the function's linkage is internal can
3035 // we forgo a warning message about the renamed function.
Reid Spencered96d1e2007-02-08 09:08:52 +00003036 std::string NewName = makeNameUnique(FunctionName);
Reid Spencerd2920cd2007-03-21 17:27:53 +00003037 if (CurFun.Linkage != GlobalValue::InternalLinkage) {
3038 warning("Renaming function '" + FunctionName + "' as '" + NewName +
3039 "' may cause linkage errors");
3040 }
3041 // Elect to rename the thing we're now defining.
Reid Spencered96d1e2007-02-08 09:08:52 +00003042 Fn = new Function(FT, CurFun.Linkage, NewName, M);
3043 InsertValue(Fn, CurModule.Values);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003044 RenameMapKey Key = makeRenameMapKey(FunctionName, PFT, ID.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003045 CurModule.RenameMap[Key] = NewName;
Reid Spencerd2920cd2007-03-21 17:27:53 +00003046 }
Reid Spenceref9b9a72007-02-05 20:47:22 +00003047 } else {
Reid Spencered96d1e2007-02-08 09:08:52 +00003048 // There's no conflict, just define the function
3049 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
3050 InsertValue(Fn, CurModule.Values);
Reid Spenceref9b9a72007-02-05 20:47:22 +00003051 }
Reid Spencere59f4932007-04-16 03:05:01 +00003052 } else {
3053 // There's no conflict, just define the function
3054 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
3055 InsertValue(Fn, CurModule.Values);
Reid Spencer950bf602007-01-26 08:19:09 +00003056 }
3057
Reid Spencere59f4932007-04-16 03:05:01 +00003058
Reid Spencer950bf602007-01-26 08:19:09 +00003059 CurFun.FunctionStart(Fn);
3060
3061 if (CurFun.isDeclare) {
3062 // If we have declaration, always overwrite linkage. This will allow us
3063 // to correctly handle cases, when pointer to function is passed as
3064 // argument to another function.
3065 Fn->setLinkage(CurFun.Linkage);
3066 }
Reid Spencerb7046c72007-01-29 05:41:34 +00003067 Fn->setCallingConv(upgradeCallingConv($1));
Reid Spencer950bf602007-01-26 08:19:09 +00003068 Fn->setAlignment($8);
3069 if ($7) {
3070 Fn->setSection($7);
3071 free($7);
3072 }
3073
3074 // Add all of the arguments we parsed to the function...
3075 if ($5) { // Is null if empty...
3076 if (isVarArg) { // Nuke the last entry
Reid Spencered96d1e2007-02-08 09:08:52 +00003077 assert($5->back().first.PAT->get() == Type::VoidTy &&
Reid Spencer950bf602007-01-26 08:19:09 +00003078 $5->back().second == 0 && "Not a varargs marker");
Reid Spencered96d1e2007-02-08 09:08:52 +00003079 delete $5->back().first.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003080 $5->pop_back(); // Delete the last entry
3081 }
3082 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00003083 Function::arg_iterator ArgEnd = Fn->arg_end();
3084 std::vector<std::pair<PATypeInfo,char*> >::iterator I = $5->begin();
3085 std::vector<std::pair<PATypeInfo,char*> >::iterator E = $5->end();
3086 for ( ; I != E && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencered96d1e2007-02-08 09:08:52 +00003087 delete I->first.PAT; // Delete the typeholder...
Reid Spencerbb1fd572007-03-21 17:15:50 +00003088 ValueInfo VI; VI.V = ArgIt; VI.S.copy(I->first.S);
3089 setValueName(VI, I->second); // Insert arg into symtab...
Reid Spencer950bf602007-01-26 08:19:09 +00003090 InsertValue(ArgIt);
3091 }
3092 delete $5; // We're now done with the argument list
3093 }
Reid Spencer7eea8ff2007-05-18 05:48:07 +00003094 lastCallingConv = OldCallingConv::C;
Reid Spencer950bf602007-01-26 08:19:09 +00003095 }
3096 ;
3097
3098BEGIN
3099 : BEGINTOK | '{' // Allow BEGIN or '{' to start a function
Jeff Cohenac2dca92007-01-21 19:30:52 +00003100 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003101
Reid Spencer6fd36ab2006-12-29 20:35:03 +00003102FunctionHeader
Reid Spencerd2920cd2007-03-21 17:27:53 +00003103 : OptLinkage { CurFun.Linkage = $1; } FunctionHeaderH BEGIN {
Reid Spencer950bf602007-01-26 08:19:09 +00003104 $$ = CurFun.CurrentFunction;
3105
3106 // Make sure that we keep track of the linkage type even if there was a
3107 // previous "declare".
3108 $$->setLinkage($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00003109 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00003110 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003111
Reid Spencer950bf602007-01-26 08:19:09 +00003112END
3113 : ENDTOK | '}' // Allow end of '}' to end a function
3114 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003115
Reid Spencer950bf602007-01-26 08:19:09 +00003116Function
3117 : BasicBlockList END {
3118 $$ = $1;
3119 };
Reid Spencere7c3c602006-11-30 06:36:44 +00003120
Reid Spencere77e35e2006-12-01 20:26:20 +00003121FnDeclareLinkage
Reid Spencered96d1e2007-02-08 09:08:52 +00003122 : /*default*/ { $$ = GlobalValue::ExternalLinkage; }
3123 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
3124 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencere7c3c602006-11-30 06:36:44 +00003125 ;
3126
3127FunctionProto
Reid Spencered96d1e2007-02-08 09:08:52 +00003128 : DECLARE { CurFun.isDeclare = true; }
3129 FnDeclareLinkage { CurFun.Linkage = $3; } FunctionHeaderH {
Reid Spencer950bf602007-01-26 08:19:09 +00003130 $$ = CurFun.CurrentFunction;
3131 CurFun.FunctionDone();
3132
3133 }
3134 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003135
3136//===----------------------------------------------------------------------===//
3137// Rules to match Basic Blocks
3138//===----------------------------------------------------------------------===//
3139
Reid Spencer950bf602007-01-26 08:19:09 +00003140OptSideEffect
3141 : /* empty */ { $$ = false; }
3142 | SIDEEFFECT { $$ = true; }
3143 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003144
Reid Spencere77e35e2006-12-01 20:26:20 +00003145ConstValueRef
Reid Spencer950bf602007-01-26 08:19:09 +00003146 // A reference to a direct constant
Reid Spencerbb1fd572007-03-21 17:15:50 +00003147 : ESINT64VAL { $$ = ValID::create($1); }
Reid Spencer950bf602007-01-26 08:19:09 +00003148 | EUINT64VAL { $$ = ValID::create($1); }
3149 | FPVAL { $$ = ValID::create($1); }
Reid Spencerbb1fd572007-03-21 17:15:50 +00003150 | TRUETOK {
3151 $$ = ValID::create(ConstantInt::get(Type::Int1Ty, true));
3152 $$.S.makeUnsigned();
3153 }
3154 | FALSETOK {
3155 $$ = ValID::create(ConstantInt::get(Type::Int1Ty, false));
3156 $$.S.makeUnsigned();
3157 }
Reid Spencer950bf602007-01-26 08:19:09 +00003158 | NULL_TOK { $$ = ValID::createNull(); }
3159 | UNDEF { $$ = ValID::createUndef(); }
3160 | ZEROINITIALIZER { $$ = ValID::createZeroInit(); }
3161 | '<' ConstVector '>' { // Nonempty unsized packed vector
3162 const Type *ETy = (*$2)[0].C->getType();
3163 int NumElements = $2->size();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003164 VectorType* pt = VectorType::get(ETy, NumElements);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003165 $$.S.makeComposite((*$2)[0].S);
3166 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(pt, $$.S));
Reid Spencer950bf602007-01-26 08:19:09 +00003167
3168 // Verify all elements are correct type!
3169 std::vector<Constant*> Elems;
3170 for (unsigned i = 0; i < $2->size(); i++) {
3171 Constant *C = (*$2)[i].C;
3172 const Type *CTy = C->getType();
3173 if (ETy != CTy)
3174 error("Element #" + utostr(i) + " is not of type '" +
3175 ETy->getDescription() +"' as required!\nIt is of type '" +
3176 CTy->getDescription() + "'");
3177 Elems.push_back(C);
Reid Spencere7c3c602006-11-30 06:36:44 +00003178 }
Reid Spencer5eb77c72007-03-15 03:26:42 +00003179 $$ = ValID::create(ConstantVector::get(pt, Elems));
Reid Spencer950bf602007-01-26 08:19:09 +00003180 delete PTy; delete $2;
3181 }
3182 | ConstExpr {
Reid Spencer5eb77c72007-03-15 03:26:42 +00003183 $$ = ValID::create($1.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003184 $$.S.copy($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003185 }
3186 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
3187 char *End = UnEscapeLexed($3, true);
3188 std::string AsmStr = std::string($3, End);
3189 End = UnEscapeLexed($5, true);
3190 std::string Constraints = std::string($5, End);
3191 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
3192 free($3);
3193 free($5);
3194 }
3195 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003196
Reid Spencerbb1fd572007-03-21 17:15:50 +00003197// SymbolicValueRef - Reference to one of two ways of symbolically refering to // another value.
Reid Spencer950bf602007-01-26 08:19:09 +00003198//
3199SymbolicValueRef
Reid Spencerbb1fd572007-03-21 17:15:50 +00003200 : INTVAL { $$ = ValID::create($1); $$.S.makeSignless(); }
3201 | Name { $$ = ValID::create($1); $$.S.makeSignless(); }
Reid Spencer950bf602007-01-26 08:19:09 +00003202 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003203
3204// ValueRef - A reference to a definition... either constant or symbolic
Reid Spencerf459d392006-12-02 16:19:52 +00003205ValueRef
Reid Spencer950bf602007-01-26 08:19:09 +00003206 : SymbolicValueRef | ConstValueRef
Reid Spencerf459d392006-12-02 16:19:52 +00003207 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003208
Reid Spencer950bf602007-01-26 08:19:09 +00003209
Reid Spencere7c3c602006-11-30 06:36:44 +00003210// ResolvedVal - a <type> <value> pair. This is used only in cases where the
3211// type immediately preceeds the value reference, and allows complex constant
3212// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Reid Spencer950bf602007-01-26 08:19:09 +00003213ResolvedVal
3214 : Types ValueRef {
Reid Spencered96d1e2007-02-08 09:08:52 +00003215 const Type *Ty = $1.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003216 $2.S.copy($1.S);
Reid Spencer5eb77c72007-03-15 03:26:42 +00003217 $$.V = getVal(Ty, $2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003218 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003219 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003220 }
Reid Spencer950bf602007-01-26 08:19:09 +00003221 ;
3222
3223BasicBlockList
3224 : BasicBlockList BasicBlock {
3225 $$ = $1;
3226 }
3227 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
3228 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00003229 };
3230
3231
3232// Basic blocks are terminated by branching instructions:
3233// br, br/cc, switch, ret
3234//
Reid Spencer950bf602007-01-26 08:19:09 +00003235BasicBlock
3236 : InstructionList OptAssign BBTerminatorInst {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003237 ValueInfo VI; VI.V = $3.TI; VI.S.copy($3.S);
3238 setValueName(VI, $2);
3239 InsertValue($3.TI);
3240 $1->getInstList().push_back($3.TI);
Reid Spencer950bf602007-01-26 08:19:09 +00003241 InsertValue($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00003242 $$ = $1;
3243 }
Reid Spencer950bf602007-01-26 08:19:09 +00003244 ;
3245
3246InstructionList
3247 : InstructionList Inst {
3248 if ($2.I)
3249 $1->getInstList().push_back($2.I);
3250 $$ = $1;
3251 }
3252 | /* empty */ {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003253 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++),true);
Reid Spencer950bf602007-01-26 08:19:09 +00003254 // Make sure to move the basic block to the correct location in the
3255 // function, instead of leaving it inserted wherever it was first
3256 // referenced.
3257 Function::BasicBlockListType &BBL =
3258 CurFun.CurrentFunction->getBasicBlockList();
3259 BBL.splice(BBL.end(), BBL, $$);
3260 }
3261 | LABELSTR {
Reid Spencer5eb77c72007-03-15 03:26:42 +00003262 $$ = CurBB = getBBVal(ValID::create($1), true);
Reid Spencer950bf602007-01-26 08:19:09 +00003263 // Make sure to move the basic block to the correct location in the
3264 // function, instead of leaving it inserted wherever it was first
3265 // referenced.
3266 Function::BasicBlockListType &BBL =
3267 CurFun.CurrentFunction->getBasicBlockList();
3268 BBL.splice(BBL.end(), BBL, $$);
3269 }
3270 ;
3271
3272Unwind : UNWIND | EXCEPT;
3273
3274BBTerminatorInst
3275 : RET ResolvedVal { // Return with a result...
Reid Spencerbb1fd572007-03-21 17:15:50 +00003276 $$.TI = new ReturnInst($2.V);
3277 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003278 }
3279 | RET VOID { // Return with no result...
Reid Spencerbb1fd572007-03-21 17:15:50 +00003280 $$.TI = new ReturnInst();
3281 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003282 }
3283 | BR LABEL ValueRef { // Unconditional Branch...
3284 BasicBlock* tmpBB = getBBVal($3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003285 $$.TI = new BranchInst(tmpBB);
3286 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003287 } // Conditional Branch...
3288 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003289 $6.S.makeSignless();
3290 $9.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003291 BasicBlock* tmpBBA = getBBVal($6);
3292 BasicBlock* tmpBBB = getBBVal($9);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003293 $3.S.makeUnsigned();
Reid Spencer950bf602007-01-26 08:19:09 +00003294 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003295 $$.TI = new BranchInst(tmpBBA, tmpBBB, tmpVal);
3296 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003297 }
3298 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003299 $3.S.copy($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003300 Value* tmpVal = getVal($2.T, $3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003301 $6.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003302 BasicBlock* tmpBB = getBBVal($6);
3303 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Reid Spencerbb1fd572007-03-21 17:15:50 +00003304 $$.TI = S;
3305 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003306 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
3307 E = $8->end();
3308 for (; I != E; ++I) {
3309 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
3310 S->addCase(CI, I->second);
3311 else
3312 error("Switch case is constant, but not a simple integer");
3313 }
3314 delete $8;
3315 }
3316 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003317 $3.S.copy($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003318 Value* tmpVal = getVal($2.T, $3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003319 $6.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003320 BasicBlock* tmpBB = getBBVal($6);
3321 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003322 $$.TI = S;
3323 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003324 }
3325 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
3326 TO LABEL ValueRef Unwind LABEL ValueRef {
3327 const PointerType *PFTy;
3328 const FunctionType *Ty;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003329 Signedness FTySign;
Reid Spencer950bf602007-01-26 08:19:09 +00003330
Reid Spencered96d1e2007-02-08 09:08:52 +00003331 if (!(PFTy = dyn_cast<PointerType>($3.PAT->get())) ||
Reid Spencer950bf602007-01-26 08:19:09 +00003332 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3333 // Pull out the types of all of the arguments...
3334 std::vector<const Type*> ParamTypes;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003335 FTySign.makeComposite($3.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003336 if ($6) {
3337 for (std::vector<ValueInfo>::iterator I = $6->begin(), E = $6->end();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003338 I != E; ++I) {
Reid Spencer950bf602007-01-26 08:19:09 +00003339 ParamTypes.push_back((*I).V->getType());
Reid Spencerbb1fd572007-03-21 17:15:50 +00003340 FTySign.add(I->S);
3341 }
Reid Spencer950bf602007-01-26 08:19:09 +00003342 }
Reid Spencer7eea8ff2007-05-18 05:48:07 +00003343 ParamAttrsList *PAL = 0;
Reid Spencerb7046c72007-01-29 05:41:34 +00003344 if ($2 == OldCallingConv::CSRet) {
Reid Spencer7eea8ff2007-05-18 05:48:07 +00003345 ParamAttrsVector Attrs;
3346 ParamAttrsWithIndex PAWI;
3347 PAWI.index = 1; PAWI.attrs = ParamAttr::StructRet; // first arg
3348 Attrs.push_back(PAWI);
3349 PAL = ParamAttrsList::get(Attrs);
Reid Spencerb7046c72007-01-29 05:41:34 +00003350 }
Reid Spencer950bf602007-01-26 08:19:09 +00003351 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
3352 if (isVarArg) ParamTypes.pop_back();
Reid Spencer7eea8ff2007-05-18 05:48:07 +00003353 Ty = FunctionType::get($3.PAT->get(), ParamTypes, isVarArg, PAL);
Reid Spencer950bf602007-01-26 08:19:09 +00003354 PFTy = PointerType::get(Ty);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003355 $$.S.copy($3.S);
3356 } else {
3357 FTySign = $3.S;
Reid Spencera3b12dd2007-04-07 16:14:01 +00003358 // Get the signedness of the result type. $3 is the pointer to the
3359 // function type so we get the 0th element to extract the function type,
3360 // and then the 0th element again to get the result type.
3361 $$.S.copy($3.S.get(0).get(0));
Reid Spencer950bf602007-01-26 08:19:09 +00003362 }
Reid Spencer7b5d4662007-04-09 06:16:21 +00003363
Reid Spencerbb1fd572007-03-21 17:15:50 +00003364 $4.S.makeComposite(FTySign);
Reid Spencer950bf602007-01-26 08:19:09 +00003365 Value *V = getVal(PFTy, $4); // Get the function we're calling...
3366 BasicBlock *Normal = getBBVal($10);
3367 BasicBlock *Except = getBBVal($13);
3368
3369 // Create the call node...
3370 if (!$6) { // Has no arguments?
Reid Spencerbb1fd572007-03-21 17:15:50 +00003371 $$.TI = new InvokeInst(V, Normal, Except, 0, 0);
Reid Spencer950bf602007-01-26 08:19:09 +00003372 } else { // Has arguments?
3373 // Loop through FunctionType's arguments and ensure they are specified
3374 // correctly!
3375 //
3376 FunctionType::param_iterator I = Ty->param_begin();
3377 FunctionType::param_iterator E = Ty->param_end();
3378 std::vector<ValueInfo>::iterator ArgI = $6->begin(), ArgE = $6->end();
3379
3380 std::vector<Value*> Args;
3381 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
3382 if ((*ArgI).V->getType() != *I)
3383 error("Parameter " +(*ArgI).V->getName()+ " is not of type '" +
3384 (*I)->getDescription() + "'");
3385 Args.push_back((*ArgI).V);
3386 }
3387
3388 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
3389 error("Invalid number of parameters detected");
3390
Reid Spencerbb1fd572007-03-21 17:15:50 +00003391 $$.TI = new InvokeInst(V, Normal, Except, &Args[0], Args.size());
Reid Spencer950bf602007-01-26 08:19:09 +00003392 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00003393 cast<InvokeInst>($$.TI)->setCallingConv(upgradeCallingConv($2));
Reid Spencered96d1e2007-02-08 09:08:52 +00003394 delete $3.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003395 delete $6;
Reid Spencer7eea8ff2007-05-18 05:48:07 +00003396 lastCallingConv = OldCallingConv::C;
Reid Spencer950bf602007-01-26 08:19:09 +00003397 }
3398 | Unwind {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003399 $$.TI = new UnwindInst();
3400 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003401 }
3402 | UNREACHABLE {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003403 $$.TI = new UnreachableInst();
3404 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003405 }
3406 ;
3407
3408JumpTable
3409 : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
3410 $$ = $1;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003411 $3.S.copy($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003412 Constant *V = cast<Constant>(getExistingValue($2.T, $3));
3413
3414 if (V == 0)
3415 error("May only switch on a constant pool value");
3416
Reid Spencerbb1fd572007-03-21 17:15:50 +00003417 $6.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003418 BasicBlock* tmpBB = getBBVal($6);
3419 $$->push_back(std::make_pair(V, tmpBB));
3420 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003421 | IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencer950bf602007-01-26 08:19:09 +00003422 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003423 $2.S.copy($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003424 Constant *V = cast<Constant>(getExistingValue($1.T, $2));
3425
3426 if (V == 0)
3427 error("May only switch on a constant pool value");
3428
Reid Spencerbb1fd572007-03-21 17:15:50 +00003429 $5.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003430 BasicBlock* tmpBB = getBBVal($5);
3431 $$->push_back(std::make_pair(V, tmpBB));
3432 }
3433 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003434
3435Inst
3436 : OptAssign InstVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003437 bool omit = false;
3438 if ($1)
3439 if (BitCastInst *BCI = dyn_cast<BitCastInst>($2.I))
3440 if (BCI->getSrcTy() == BCI->getDestTy() &&
3441 BCI->getOperand(0)->getName() == $1)
3442 // This is a useless bit cast causing a name redefinition. It is
3443 // a bit cast from a type to the same type of an operand with the
3444 // same name as the name we would give this instruction. Since this
3445 // instruction results in no code generation, it is safe to omit
3446 // the instruction. This situation can occur because of collapsed
3447 // type planes. For example:
3448 // %X = add int %Y, %Z
3449 // %X = cast int %Y to uint
3450 // After upgrade, this looks like:
3451 // %X = add i32 %Y, %Z
3452 // %X = bitcast i32 to i32
3453 // The bitcast is clearly useless so we omit it.
3454 omit = true;
3455 if (omit) {
3456 $$.I = 0;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003457 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003458 } else {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003459 ValueInfo VI; VI.V = $2.I; VI.S.copy($2.S);
3460 setValueName(VI, $1);
Reid Spencer950bf602007-01-26 08:19:09 +00003461 InsertValue($2.I);
3462 $$ = $2;
Reid Spencerf5626a32007-01-01 01:20:41 +00003463 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003464 };
3465
Reid Spencer950bf602007-01-26 08:19:09 +00003466PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
3467 $$.P = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003468 $$.S.copy($1.S);
3469 $3.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003470 Value* tmpVal = getVal($1.PAT->get(), $3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003471 $5.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003472 BasicBlock* tmpBB = getBBVal($5);
3473 $$.P->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencered96d1e2007-02-08 09:08:52 +00003474 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003475 }
3476 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
Reid Spencere7c3c602006-11-30 06:36:44 +00003477 $$ = $1;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003478 $4.S.copy($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003479 Value* tmpVal = getVal($1.P->front().first->getType(), $4);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003480 $6.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003481 BasicBlock* tmpBB = getBBVal($6);
3482 $1.P->push_back(std::make_pair(tmpVal, tmpBB));
3483 }
3484 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003485
Reid Spencer950bf602007-01-26 08:19:09 +00003486ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
3487 $$ = new std::vector<ValueInfo>();
Reid Spencerf8483652006-12-02 15:16:01 +00003488 $$->push_back($1);
3489 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003490 | ValueRefList ',' ResolvedVal {
Reid Spencere7c3c602006-11-30 06:36:44 +00003491 $$ = $1;
Reid Spencer950bf602007-01-26 08:19:09 +00003492 $1->push_back($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00003493 };
3494
3495// ValueRefListE - Just like ValueRefList, except that it may also be empty!
3496ValueRefListE
Reid Spencer950bf602007-01-26 08:19:09 +00003497 : ValueRefList
3498 | /*empty*/ { $$ = 0; }
Reid Spencere7c3c602006-11-30 06:36:44 +00003499 ;
3500
3501OptTailCall
3502 : TAIL CALL {
Reid Spencer950bf602007-01-26 08:19:09 +00003503 $$ = true;
Reid Spencere7c3c602006-11-30 06:36:44 +00003504 }
Reid Spencer950bf602007-01-26 08:19:09 +00003505 | CALL {
3506 $$ = false;
3507 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003508 ;
3509
Reid Spencer950bf602007-01-26 08:19:09 +00003510InstVal
3511 : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003512 $3.S.copy($2.S);
3513 $5.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003514 const Type* Ty = $2.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003515 if (!Ty->isInteger() && !Ty->isFloatingPoint() && !isa<VectorType>(Ty))
Reid Spencer950bf602007-01-26 08:19:09 +00003516 error("Arithmetic operator requires integer, FP, or packed operands");
Reid Spencer9d6565a2007-02-15 02:26:10 +00003517 if (isa<VectorType>(Ty) &&
Reid Spencer950bf602007-01-26 08:19:09 +00003518 ($1 == URemOp || $1 == SRemOp || $1 == FRemOp || $1 == RemOp))
Chris Lattner4227bdb2007-02-19 07:34:02 +00003519 error("Remainder not supported on vector types");
Reid Spencer950bf602007-01-26 08:19:09 +00003520 // Upgrade the opcode from obsolete versions before we do anything with it.
3521 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $2.S);
3522 Value* val1 = getVal(Ty, $3);
3523 Value* val2 = getVal(Ty, $5);
3524 $$.I = BinaryOperator::create(Opcode, val1, val2);
3525 if ($$.I == 0)
3526 error("binary operator returned null");
Reid Spencerbb1fd572007-03-21 17:15:50 +00003527 $$.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003528 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003529 }
3530 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003531 $3.S.copy($2.S);
3532 $5.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003533 const Type *Ty = $2.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003534 if (!Ty->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00003535 if (!isa<VectorType>(Ty) ||
3536 !cast<VectorType>(Ty)->getElementType()->isInteger())
Reid Spencer950bf602007-01-26 08:19:09 +00003537 error("Logical operator requires integral operands");
3538 }
3539 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $2.S);
3540 Value* tmpVal1 = getVal(Ty, $3);
3541 Value* tmpVal2 = getVal(Ty, $5);
3542 $$.I = BinaryOperator::create(Opcode, tmpVal1, tmpVal2);
3543 if ($$.I == 0)
3544 error("binary operator returned null");
Reid Spencerbb1fd572007-03-21 17:15:50 +00003545 $$.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003546 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003547 }
3548 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003549 $3.S.copy($2.S);
3550 $5.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003551 const Type* Ty = $2.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003552 if(isa<VectorType>(Ty))
3553 error("VectorTypes currently not supported in setcc instructions");
Reid Spencer950bf602007-01-26 08:19:09 +00003554 unsigned short pred;
3555 Instruction::OtherOps Opcode = getCompareOp($1, pred, Ty, $2.S);
3556 Value* tmpVal1 = getVal(Ty, $3);
3557 Value* tmpVal2 = getVal(Ty, $5);
3558 $$.I = CmpInst::create(Opcode, pred, tmpVal1, tmpVal2);
3559 if ($$.I == 0)
3560 error("binary operator returned null");
Reid Spencerbb1fd572007-03-21 17:15:50 +00003561 $$.S.makeUnsigned();
Reid Spencered96d1e2007-02-08 09:08:52 +00003562 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003563 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00003564 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003565 $4.S.copy($3.S);
3566 $6.S.copy($3.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003567 const Type *Ty = $3.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003568 if (isa<VectorType>(Ty))
3569 error("VectorTypes currently not supported in icmp instructions");
Reid Spencer950bf602007-01-26 08:19:09 +00003570 else if (!Ty->isInteger() && !isa<PointerType>(Ty))
3571 error("icmp requires integer or pointer typed operands");
3572 Value* tmpVal1 = getVal(Ty, $4);
3573 Value* tmpVal2 = getVal(Ty, $6);
3574 $$.I = new ICmpInst($2, tmpVal1, tmpVal2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003575 $$.S.makeUnsigned();
Reid Spencered96d1e2007-02-08 09:08:52 +00003576 delete $3.PAT;
Reid Spencer57f28f92006-12-03 07:10:26 +00003577 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00003578 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003579 $4.S.copy($3.S);
3580 $6.S.copy($3.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003581 const Type *Ty = $3.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003582 if (isa<VectorType>(Ty))
3583 error("VectorTypes currently not supported in fcmp instructions");
Reid Spencer950bf602007-01-26 08:19:09 +00003584 else if (!Ty->isFloatingPoint())
3585 error("fcmp instruction requires floating point operands");
3586 Value* tmpVal1 = getVal(Ty, $4);
3587 Value* tmpVal2 = getVal(Ty, $6);
3588 $$.I = new FCmpInst($2, tmpVal1, tmpVal2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003589 $$.S.makeUnsigned();
Reid Spencered96d1e2007-02-08 09:08:52 +00003590 delete $3.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003591 }
3592 | NOT ResolvedVal {
3593 warning("Use of obsolete 'not' instruction: Replacing with 'xor");
3594 const Type *Ty = $2.V->getType();
3595 Value *Ones = ConstantInt::getAllOnesValue(Ty);
3596 if (Ones == 0)
3597 error("Expected integral type for not instruction");
3598 $$.I = BinaryOperator::create(Instruction::Xor, $2.V, Ones);
3599 if ($$.I == 0)
3600 error("Could not create a xor instruction");
Reid Spencerbb1fd572007-03-21 17:15:50 +00003601 $$.S.copy($2.S);
Reid Spencer229e9362006-12-02 22:14:11 +00003602 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003603 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003604 if (!$4.V->getType()->isInteger() ||
3605 cast<IntegerType>($4.V->getType())->getBitWidth() != 8)
3606 error("Shift amount must be int8");
Reid Spencer832254e2007-02-02 02:16:23 +00003607 const Type* Ty = $2.V->getType();
3608 if (!Ty->isInteger())
Reid Spencer950bf602007-01-26 08:19:09 +00003609 error("Shift constant expression requires integer operand");
Reid Spencer832254e2007-02-02 02:16:23 +00003610 Value* ShiftAmt = 0;
3611 if (cast<IntegerType>(Ty)->getBitWidth() > Type::Int8Ty->getBitWidth())
3612 if (Constant *C = dyn_cast<Constant>($4.V))
3613 ShiftAmt = ConstantExpr::getZExt(C, Ty);
3614 else
3615 ShiftAmt = new ZExtInst($4.V, Ty, makeNameUnique("shift"), CurBB);
3616 else
3617 ShiftAmt = $4.V;
3618 $$.I = BinaryOperator::create(getBinaryOp($1, Ty, $2.S), $2.V, ShiftAmt);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003619 $$.S.copy($2.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00003620 }
Reid Spencerfcb5df82006-12-01 22:34:43 +00003621 | CastOps ResolvedVal TO Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00003622 const Type *DstTy = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003623 if (!DstTy->isFirstClassType())
3624 error("cast instruction to a non-primitive type: '" +
3625 DstTy->getDescription() + "'");
3626 $$.I = cast<Instruction>(getCast($1, $2.V, $2.S, DstTy, $4.S, true));
Reid Spencerbb1fd572007-03-21 17:15:50 +00003627 $$.S.copy($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003628 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003629 }
3630 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003631 if (!$2.V->getType()->isInteger() ||
3632 cast<IntegerType>($2.V->getType())->getBitWidth() != 1)
3633 error("select condition must be bool");
3634 if ($4.V->getType() != $6.V->getType())
3635 error("select value types should match");
3636 $$.I = new SelectInst($2.V, $4.V, $6.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003637 $$.S.copy($4.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00003638 }
3639 | VAARG ResolvedVal ',' Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00003640 const Type *Ty = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003641 NewVarArgs = true;
3642 $$.I = new VAArgInst($2.V, Ty);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003643 $$.S.copy($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003644 delete $4.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003645 }
3646 | VAARG_old ResolvedVal ',' Types {
3647 const Type* ArgTy = $2.V->getType();
Reid Spencered96d1e2007-02-08 09:08:52 +00003648 const Type* DstTy = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003649 ObsoleteVarArgs = true;
3650 Function* NF = cast<Function>(CurModule.CurrentModule->
3651 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0));
3652
3653 //b = vaarg a, t ->
3654 //foo = alloca 1 of t
3655 //bar = vacopy a
3656 //store bar -> foo
3657 //b = vaarg foo, t
3658 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix");
3659 CurBB->getInstList().push_back(foo);
3660 CallInst* bar = new CallInst(NF, $2.V);
3661 CurBB->getInstList().push_back(bar);
3662 CurBB->getInstList().push_back(new StoreInst(bar, foo));
3663 $$.I = new VAArgInst(foo, DstTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003664 $$.S.copy($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003665 delete $4.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003666 }
3667 | VANEXT_old ResolvedVal ',' Types {
3668 const Type* ArgTy = $2.V->getType();
Reid Spencered96d1e2007-02-08 09:08:52 +00003669 const Type* DstTy = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003670 ObsoleteVarArgs = true;
3671 Function* NF = cast<Function>(CurModule.CurrentModule->
3672 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0));
3673
3674 //b = vanext a, t ->
3675 //foo = alloca 1 of t
3676 //bar = vacopy a
3677 //store bar -> foo
3678 //tmp = vaarg foo, t
3679 //b = load foo
3680 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix");
3681 CurBB->getInstList().push_back(foo);
3682 CallInst* bar = new CallInst(NF, $2.V);
3683 CurBB->getInstList().push_back(bar);
3684 CurBB->getInstList().push_back(new StoreInst(bar, foo));
3685 Instruction* tmp = new VAArgInst(foo, DstTy);
3686 CurBB->getInstList().push_back(tmp);
3687 $$.I = new LoadInst(foo);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003688 $$.S.copy($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003689 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003690 }
3691 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003692 if (!ExtractElementInst::isValidOperands($2.V, $4.V))
3693 error("Invalid extractelement operands");
3694 $$.I = new ExtractElementInst($2.V, $4.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003695 $$.S.copy($2.S.get(0));
Reid Spencere7c3c602006-11-30 06:36:44 +00003696 }
3697 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003698 if (!InsertElementInst::isValidOperands($2.V, $4.V, $6.V))
3699 error("Invalid insertelement operands");
3700 $$.I = new InsertElementInst($2.V, $4.V, $6.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003701 $$.S.copy($2.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00003702 }
3703 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003704 if (!ShuffleVectorInst::isValidOperands($2.V, $4.V, $6.V))
3705 error("Invalid shufflevector operands");
3706 $$.I = new ShuffleVectorInst($2.V, $4.V, $6.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003707 $$.S.copy($2.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00003708 }
3709 | PHI_TOK PHIList {
Reid Spencer950bf602007-01-26 08:19:09 +00003710 const Type *Ty = $2.P->front().first->getType();
3711 if (!Ty->isFirstClassType())
3712 error("PHI node operands must be of first class type");
3713 PHINode *PHI = new PHINode(Ty);
3714 PHI->reserveOperandSpace($2.P->size());
3715 while ($2.P->begin() != $2.P->end()) {
3716 if ($2.P->front().first->getType() != Ty)
3717 error("All elements of a PHI node must be of the same type");
3718 PHI->addIncoming($2.P->front().first, $2.P->front().second);
3719 $2.P->pop_front();
3720 }
3721 $$.I = PHI;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003722 $$.S.copy($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003723 delete $2.P; // Free the list...
Reid Spencere7c3c602006-11-30 06:36:44 +00003724 }
Reid Spencer7b5d4662007-04-09 06:16:21 +00003725 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00003726 // Handle the short call syntax
3727 const PointerType *PFTy;
3728 const FunctionType *FTy;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003729 Signedness FTySign;
Reid Spencered96d1e2007-02-08 09:08:52 +00003730 if (!(PFTy = dyn_cast<PointerType>($3.PAT->get())) ||
Reid Spencer950bf602007-01-26 08:19:09 +00003731 !(FTy = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3732 // Pull out the types of all of the arguments...
3733 std::vector<const Type*> ParamTypes;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003734 FTySign.makeComposite($3.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003735 if ($6) {
3736 for (std::vector<ValueInfo>::iterator I = $6->begin(), E = $6->end();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003737 I != E; ++I) {
Reid Spencer950bf602007-01-26 08:19:09 +00003738 ParamTypes.push_back((*I).V->getType());
Reid Spencerbb1fd572007-03-21 17:15:50 +00003739 FTySign.add(I->S);
3740 }
Reid Spencerc4d96252007-01-13 00:03:30 +00003741 }
Reid Spencer950bf602007-01-26 08:19:09 +00003742
3743 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
3744 if (isVarArg) ParamTypes.pop_back();
3745
Reid Spencered96d1e2007-02-08 09:08:52 +00003746 const Type *RetTy = $3.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003747 if (!RetTy->isFirstClassType() && RetTy != Type::VoidTy)
3748 error("Functions cannot return aggregate types");
3749
Reid Spencer7b5d4662007-04-09 06:16:21 +00003750 // Deal with CSRetCC
Reid Spencer7eea8ff2007-05-18 05:48:07 +00003751 ParamAttrsList *PAL = 0;
Reid Spencer7b5d4662007-04-09 06:16:21 +00003752 if ($2 == OldCallingConv::CSRet) {
Reid Spencer7eea8ff2007-05-18 05:48:07 +00003753 ParamAttrsVector Attrs;
3754 ParamAttrsWithIndex PAWI;
3755 PAWI.index = 1; PAWI.attrs = ParamAttr::StructRet; // first arg
3756 Attrs.push_back(PAWI);
3757 PAL = ParamAttrsList::get(Attrs);
Reid Spencer7b5d4662007-04-09 06:16:21 +00003758 }
3759
Reid Spencer7eea8ff2007-05-18 05:48:07 +00003760 FTy = FunctionType::get(RetTy, ParamTypes, isVarArg, PAL);
Reid Spencer950bf602007-01-26 08:19:09 +00003761 PFTy = PointerType::get(FTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003762 $$.S.copy($3.S);
3763 } else {
3764 FTySign = $3.S;
Reid Spencera3b12dd2007-04-07 16:14:01 +00003765 // Get the signedness of the result type. $3 is the pointer to the
3766 // function type so we get the 0th element to extract the function type,
3767 // and then the 0th element again to get the result type.
3768 $$.S.copy($3.S.get(0).get(0));
Reid Spencerf8483652006-12-02 15:16:01 +00003769 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00003770 $4.S.makeComposite(FTySign);
Reid Spencer950bf602007-01-26 08:19:09 +00003771
3772 // First upgrade any intrinsic calls.
3773 std::vector<Value*> Args;
3774 if ($6)
3775 for (unsigned i = 0, e = $6->size(); i < e; ++i)
3776 Args.push_back((*$6)[i].V);
Reid Spencer41b213e2007-04-02 01:14:00 +00003777 Instruction *Inst = upgradeIntrinsicCall(FTy->getReturnType(), $4, Args);
Reid Spencer950bf602007-01-26 08:19:09 +00003778
3779 // If we got an upgraded intrinsic
3780 if (Inst) {
3781 $$.I = Inst;
Reid Spencer950bf602007-01-26 08:19:09 +00003782 } else {
3783 // Get the function we're calling
3784 Value *V = getVal(PFTy, $4);
3785
3786 // Check the argument values match
3787 if (!$6) { // Has no arguments?
3788 // Make sure no arguments is a good thing!
3789 if (FTy->getNumParams() != 0)
3790 error("No arguments passed to a function that expects arguments");
3791 } else { // Has arguments?
3792 // Loop through FunctionType's arguments and ensure they are specified
3793 // correctly!
3794 //
3795 FunctionType::param_iterator I = FTy->param_begin();
3796 FunctionType::param_iterator E = FTy->param_end();
3797 std::vector<ValueInfo>::iterator ArgI = $6->begin(), ArgE = $6->end();
3798
3799 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
3800 if ((*ArgI).V->getType() != *I)
3801 error("Parameter " +(*ArgI).V->getName()+ " is not of type '" +
3802 (*I)->getDescription() + "'");
3803
3804 if (I != E || (ArgI != ArgE && !FTy->isVarArg()))
3805 error("Invalid number of parameters detected");
3806 }
3807
3808 // Create the call instruction
Chris Lattnercf3d0612007-02-13 06:04:17 +00003809 CallInst *CI = new CallInst(V, &Args[0], Args.size());
Reid Spencer950bf602007-01-26 08:19:09 +00003810 CI->setTailCall($1);
Reid Spencerb7046c72007-01-29 05:41:34 +00003811 CI->setCallingConv(upgradeCallingConv($2));
Reid Spencer950bf602007-01-26 08:19:09 +00003812 $$.I = CI;
Reid Spencer950bf602007-01-26 08:19:09 +00003813 }
Reid Spencered96d1e2007-02-08 09:08:52 +00003814 delete $3.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003815 delete $6;
Reid Spencer7eea8ff2007-05-18 05:48:07 +00003816 lastCallingConv = OldCallingConv::C;
Reid Spencere7c3c602006-11-30 06:36:44 +00003817 }
Reid Spencer950bf602007-01-26 08:19:09 +00003818 | MemoryInst {
3819 $$ = $1;
3820 }
3821 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003822
3823
3824// IndexList - List of indices for GEP based instructions...
3825IndexList
Reid Spencer950bf602007-01-26 08:19:09 +00003826 : ',' ValueRefList { $$ = $2; }
3827 | /* empty */ { $$ = new std::vector<ValueInfo>(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00003828 ;
3829
3830OptVolatile
Reid Spencer950bf602007-01-26 08:19:09 +00003831 : VOLATILE { $$ = true; }
3832 | /* empty */ { $$ = false; }
Reid Spencere7c3c602006-11-30 06:36:44 +00003833 ;
3834
Reid Spencer950bf602007-01-26 08:19:09 +00003835MemoryInst
3836 : MALLOC Types OptCAlign {
Reid Spencered96d1e2007-02-08 09:08:52 +00003837 const Type *Ty = $2.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003838 $$.S.makeComposite($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003839 $$.I = new MallocInst(Ty, 0, $3);
Reid Spencered96d1e2007-02-08 09:08:52 +00003840 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003841 }
3842 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencered96d1e2007-02-08 09:08:52 +00003843 const Type *Ty = $2.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003844 $5.S.makeUnsigned();
3845 $$.S.makeComposite($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003846 $$.I = new MallocInst(Ty, getVal($4.T, $5), $6);
Reid Spencered96d1e2007-02-08 09:08:52 +00003847 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003848 }
3849 | ALLOCA Types OptCAlign {
Reid Spencered96d1e2007-02-08 09:08:52 +00003850 const Type *Ty = $2.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003851 $$.S.makeComposite($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003852 $$.I = new AllocaInst(Ty, 0, $3);
Reid Spencered96d1e2007-02-08 09:08:52 +00003853 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003854 }
3855 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencered96d1e2007-02-08 09:08:52 +00003856 const Type *Ty = $2.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003857 $5.S.makeUnsigned();
3858 $$.S.makeComposite($4.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003859 $$.I = new AllocaInst(Ty, getVal($4.T, $5), $6);
Reid Spencered96d1e2007-02-08 09:08:52 +00003860 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003861 }
3862 | FREE ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003863 const Type *PTy = $2.V->getType();
3864 if (!isa<PointerType>(PTy))
3865 error("Trying to free nonpointer type '" + PTy->getDescription() + "'");
3866 $$.I = new FreeInst($2.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003867 $$.S.makeSignless();
Reid Spencere7c3c602006-11-30 06:36:44 +00003868 }
3869 | OptVolatile LOAD Types ValueRef {
Reid Spencered96d1e2007-02-08 09:08:52 +00003870 const Type* Ty = $3.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003871 $4.S.copy($3.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003872 if (!isa<PointerType>(Ty))
3873 error("Can't load from nonpointer type: " + Ty->getDescription());
3874 if (!cast<PointerType>(Ty)->getElementType()->isFirstClassType())
3875 error("Can't load from pointer of non-first-class type: " +
3876 Ty->getDescription());
3877 Value* tmpVal = getVal(Ty, $4);
3878 $$.I = new LoadInst(tmpVal, "", $1);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003879 $$.S.copy($3.S.get(0));
Reid Spencered96d1e2007-02-08 09:08:52 +00003880 delete $3.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003881 }
3882 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003883 $6.S.copy($5.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003884 const PointerType *PTy = dyn_cast<PointerType>($5.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00003885 if (!PTy)
3886 error("Can't store to a nonpointer type: " +
Reid Spencered96d1e2007-02-08 09:08:52 +00003887 $5.PAT->get()->getDescription());
Reid Spencer950bf602007-01-26 08:19:09 +00003888 const Type *ElTy = PTy->getElementType();
Reid Spencered96d1e2007-02-08 09:08:52 +00003889 Value *StoreVal = $3.V;
Reid Spencer950bf602007-01-26 08:19:09 +00003890 Value* tmpVal = getVal(PTy, $6);
Reid Spencered96d1e2007-02-08 09:08:52 +00003891 if (ElTy != $3.V->getType()) {
3892 StoreVal = handleSRetFuncTypeMerge($3.V, ElTy);
3893 if (!StoreVal)
3894 error("Can't store '" + $3.V->getType()->getDescription() +
3895 "' into space of type '" + ElTy->getDescription() + "'");
3896 else {
3897 PTy = PointerType::get(StoreVal->getType());
3898 if (Constant *C = dyn_cast<Constant>(tmpVal))
3899 tmpVal = ConstantExpr::getBitCast(C, PTy);
3900 else
3901 tmpVal = new BitCastInst(tmpVal, PTy, "upgrd.cast", CurBB);
3902 }
3903 }
3904 $$.I = new StoreInst(StoreVal, tmpVal, $1);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003905 $$.S.makeSignless();
Reid Spencered96d1e2007-02-08 09:08:52 +00003906 delete $5.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003907 }
3908 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003909 $3.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003910 const Type* Ty = $2.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003911 if (!isa<PointerType>(Ty))
3912 error("getelementptr insn requires pointer operand");
3913
3914 std::vector<Value*> VIndices;
Reid Spencerff0e4482007-04-16 00:40:57 +00003915 upgradeGEPInstIndices(Ty, $4, VIndices);
Reid Spencer950bf602007-01-26 08:19:09 +00003916
3917 Value* tmpVal = getVal(Ty, $3);
Chris Lattner1bc3fa62007-02-12 22:58:38 +00003918 $$.I = new GetElementPtrInst(tmpVal, &VIndices[0], VIndices.size());
Reid Spencerbb1fd572007-03-21 17:15:50 +00003919 ValueInfo VI; VI.V = tmpVal; VI.S.copy($2.S);
3920 $$.S.copy(getElementSign(VI, VIndices));
Reid Spencered96d1e2007-02-08 09:08:52 +00003921 delete $2.PAT;
Reid Spencer30d0c582007-01-15 00:26:18 +00003922 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00003923 };
3924
Reid Spencer950bf602007-01-26 08:19:09 +00003925
Reid Spencere7c3c602006-11-30 06:36:44 +00003926%%
3927
3928int yyerror(const char *ErrorMsg) {
3929 std::string where
3930 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Reid Spencered96d1e2007-02-08 09:08:52 +00003931 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
Reid Spencer950bf602007-01-26 08:19:09 +00003932 std::string errMsg = where + "error: " + std::string(ErrorMsg);
3933 if (yychar != YYEMPTY && yychar != 0)
3934 errMsg += " while reading token '" + std::string(Upgradetext, Upgradeleng) +
3935 "'.";
Reid Spencer71d2ec92006-12-31 06:02:26 +00003936 std::cerr << "llvm-upgrade: " << errMsg << '\n';
Reid Spencer950bf602007-01-26 08:19:09 +00003937 std::cout << "llvm-upgrade: parse failed.\n";
Reid Spencere7c3c602006-11-30 06:36:44 +00003938 exit(1);
3939}
Reid Spencer319a7302007-01-05 17:20:02 +00003940
Reid Spencer30d0c582007-01-15 00:26:18 +00003941void warning(const std::string& ErrorMsg) {
Reid Spencer319a7302007-01-05 17:20:02 +00003942 std::string where
3943 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Reid Spencered96d1e2007-02-08 09:08:52 +00003944 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
Reid Spencer950bf602007-01-26 08:19:09 +00003945 std::string errMsg = where + "warning: " + std::string(ErrorMsg);
3946 if (yychar != YYEMPTY && yychar != 0)
3947 errMsg += " while reading token '" + std::string(Upgradetext, Upgradeleng) +
3948 "'.";
Reid Spencer319a7302007-01-05 17:20:02 +00003949 std::cerr << "llvm-upgrade: " << errMsg << '\n';
3950}
Reid Spencer950bf602007-01-26 08:19:09 +00003951
3952void error(const std::string& ErrorMsg, int LineNo) {
3953 if (LineNo == -1) LineNo = Upgradelineno;
3954 Upgradelineno = LineNo;
3955 yyerror(ErrorMsg.c_str());
3956}
3957