blob: 6340d86487a38544402e2df19d6253850f4042df [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//
Chris Lattnercf786592007-12-29 20:47:37 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencere7c3c602006-11-30 06:36:44 +00007//
8//===----------------------------------------------------------------------===//
9//
Reid 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
376// getExistingValue - Look up the value specified by the provided type and
377// the provided ValID. If the value exists and has already been defined, return
378// it. Otherwise return null.
379//
380static Value *getExistingValue(const Type *Ty, const ValID &D) {
381 if (isa<FunctionType>(Ty)) {
382 error("Functions are not values and must be referenced as pointers");
383 }
384
385 switch (D.Type) {
386 case ValID::NumberVal: { // Is it a numbered definition?
387 unsigned Num = (unsigned)D.Num;
388
389 // Module constants occupy the lowest numbered slots...
390 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
391 if (VI != CurModule.Values.end()) {
392 if (Num < VI->second.size())
393 return VI->second[Num];
394 Num -= VI->second.size();
395 }
396
397 // Make sure that our type is within bounds
398 VI = CurFun.Values.find(Ty);
399 if (VI == CurFun.Values.end()) return 0;
400
401 // Check that the number is within bounds...
402 if (VI->second.size() <= Num) return 0;
403
404 return VI->second[Num];
405 }
406
407 case ValID::NameVal: { // Is it a named definition?
408 // Get the name out of the ID
Reid Spencerbb1fd572007-03-21 17:15:50 +0000409 RenameMapKey Key = makeRenameMapKey(D.Name, Ty, D.S);
410 Value *V = 0;
Reid Spencer950bf602007-01-26 08:19:09 +0000411 if (inFunctionScope()) {
412 // See if the name was renamed
413 RenameMapType::const_iterator I = CurFun.RenameMap.find(Key);
414 std::string LookupName;
415 if (I != CurFun.RenameMap.end())
416 LookupName = I->second;
417 else
Reid Spencerbb1fd572007-03-21 17:15:50 +0000418 LookupName = D.Name;
Reid Spenceref9b9a72007-02-05 20:47:22 +0000419 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
420 V = SymTab.lookup(LookupName);
Reid Spencerbb1fd572007-03-21 17:15:50 +0000421 if (V && V->getType() != Ty)
Duncan Sandsdc024672007-11-27 13:23:08 +0000422 V = 0;
Reid Spencer950bf602007-01-26 08:19:09 +0000423 }
424 if (!V) {
425 RenameMapType::const_iterator I = CurModule.RenameMap.find(Key);
426 std::string LookupName;
427 if (I != CurModule.RenameMap.end())
428 LookupName = I->second;
429 else
Reid Spencerbb1fd572007-03-21 17:15:50 +0000430 LookupName = D.Name;
Reid Spenceref9b9a72007-02-05 20:47:22 +0000431 V = CurModule.CurrentModule->getValueSymbolTable().lookup(LookupName);
Reid Spencerbb1fd572007-03-21 17:15:50 +0000432 if (V && V->getType() != Ty)
Duncan Sandsdc024672007-11-27 13:23:08 +0000433 V = 0;
Reid Spencer950bf602007-01-26 08:19:09 +0000434 }
Reid Spenceref9b9a72007-02-05 20:47:22 +0000435 if (!V)
Reid Spencer950bf602007-01-26 08:19:09 +0000436 return 0;
437
438 D.destroy(); // Free old strdup'd memory...
439 return V;
440 }
441
442 // Check to make sure that "Ty" is an integral type, and that our
443 // value will fit into the specified type...
444 case ValID::ConstSIntVal: // Is it a constant pool reference??
445 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
446 error("Signed integral constant '" + itostr(D.ConstPool64) +
447 "' is invalid for type '" + Ty->getDescription() + "'");
448 }
449 return ConstantInt::get(Ty, D.ConstPool64);
450
451 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
452 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
453 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64))
454 error("Integral constant '" + utostr(D.UConstPool64) +
455 "' is invalid or out of range");
456 else // This is really a signed reference. Transmogrify.
457 return ConstantInt::get(Ty, D.ConstPool64);
458 } else
459 return ConstantInt::get(Ty, D.UConstPool64);
460
461 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Dale Johannesen43421b32007-09-06 18:13:44 +0000462 if (!ConstantFP::isValueValidForType(Ty, *D.ConstPoolFP))
Reid Spencer950bf602007-01-26 08:19:09 +0000463 error("FP constant invalid for type");
Dale Johannesen43421b32007-09-06 18:13:44 +0000464 // Lexer has no type info, so builds all FP constants as double.
465 // Fix this here.
466 if (Ty==Type::FloatTy)
467 D.ConstPoolFP->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
468 return ConstantFP::get(Ty, *D.ConstPoolFP);
Reid Spencer950bf602007-01-26 08:19:09 +0000469
470 case ValID::ConstNullVal: // Is it a null value?
471 if (!isa<PointerType>(Ty))
472 error("Cannot create a a non pointer null");
473 return ConstantPointerNull::get(cast<PointerType>(Ty));
474
475 case ValID::ConstUndefVal: // Is it an undef value?
476 return UndefValue::get(Ty);
477
478 case ValID::ConstZeroVal: // Is it a zero value?
479 return Constant::getNullValue(Ty);
480
481 case ValID::ConstantVal: // Fully resolved constant?
482 if (D.ConstantValue->getType() != Ty)
483 error("Constant expression type different from required type");
484 return D.ConstantValue;
485
486 case ValID::InlineAsmVal: { // Inline asm expression
487 const PointerType *PTy = dyn_cast<PointerType>(Ty);
488 const FunctionType *FTy =
489 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
490 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints))
491 error("Invalid type for asm constraint string");
492 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
493 D.IAD->HasSideEffects);
494 D.destroy(); // Free InlineAsmDescriptor.
495 return IA;
496 }
497 default:
498 assert(0 && "Unhandled case");
499 return 0;
500 } // End of switch
501
502 assert(0 && "Unhandled case");
503 return 0;
504}
505
506// getVal - This function is identical to getExistingValue, except that if a
507// value is not already defined, it "improvises" by creating a placeholder var
508// that looks and acts just like the requested variable. When the value is
509// defined later, all uses of the placeholder variable are replaced with the
510// real thing.
511//
512static Value *getVal(const Type *Ty, const ValID &ID) {
513 if (Ty == Type::LabelTy)
514 error("Cannot use a basic block here");
515
516 // See if the value has already been defined.
517 Value *V = getExistingValue(Ty, ID);
518 if (V) return V;
519
520 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty))
521 error("Invalid use of a composite type");
522
523 // If we reached here, we referenced either a symbol that we don't know about
524 // or an id number that hasn't been read yet. We may be referencing something
525 // forward, so just create an entry to be resolved later and get to it...
Reid Spencer950bf602007-01-26 08:19:09 +0000526 V = new Argument(Ty);
527
528 // Remember where this forward reference came from. FIXME, shouldn't we try
529 // to recycle these things??
530 CurModule.PlaceHolderInfo.insert(
Reid Spenceref9b9a72007-02-05 20:47:22 +0000531 std::make_pair(V, std::make_pair(ID, Upgradelineno)));
Reid Spencer950bf602007-01-26 08:19:09 +0000532
533 if (inFunctionScope())
534 InsertValue(V, CurFun.LateResolveValues);
535 else
536 InsertValue(V, CurModule.LateResolveValues);
537 return V;
538}
539
Reid Spencered96d1e2007-02-08 09:08:52 +0000540/// @brief This just makes any name given to it unique, up to MAX_UINT times.
541static std::string makeNameUnique(const std::string& Name) {
542 static unsigned UniqueNameCounter = 1;
543 std::string Result(Name);
544 Result += ".upgrd." + llvm::utostr(UniqueNameCounter++);
545 return Result;
546}
547
Reid Spencer950bf602007-01-26 08:19:09 +0000548/// getBBVal - This is used for two purposes:
549/// * If isDefinition is true, a new basic block with the specified ID is being
550/// defined.
551/// * If isDefinition is true, this is a reference to a basic block, which may
552/// or may not be a forward reference.
553///
554static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
555 assert(inFunctionScope() && "Can't get basic block at global scope");
556
557 std::string Name;
558 BasicBlock *BB = 0;
559 switch (ID.Type) {
560 default:
561 error("Illegal label reference " + ID.getName());
562 break;
563 case ValID::NumberVal: // Is it a numbered definition?
564 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
565 CurFun.NumberedBlocks.resize(ID.Num+1);
566 BB = CurFun.NumberedBlocks[ID.Num];
567 break;
568 case ValID::NameVal: // Is it a named definition?
569 Name = ID.Name;
Reid Spencerbb1fd572007-03-21 17:15:50 +0000570 if (Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name)) {
Reid Spencered96d1e2007-02-08 09:08:52 +0000571 if (N->getType() != Type::LabelTy) {
572 // Register names didn't use to conflict with basic block names
573 // because of type planes. Now they all have to be unique. So, we just
574 // rename the register and treat this name as if no basic block
575 // had been found.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000576 RenameMapKey Key = makeRenameMapKey(ID.Name, N->getType(), ID.S);
Reid Spencered96d1e2007-02-08 09:08:52 +0000577 N->setName(makeNameUnique(N->getName()));
578 CurModule.RenameMap[Key] = N->getName();
579 BB = 0;
580 } else {
581 BB = cast<BasicBlock>(N);
582 }
Reid Spencer950bf602007-01-26 08:19:09 +0000583 }
584 break;
585 }
586
587 // See if the block has already been defined.
588 if (BB) {
589 // If this is the definition of the block, make sure the existing value was
590 // just a forward reference. If it was a forward reference, there will be
591 // an entry for it in the PlaceHolderInfo map.
592 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
593 // The existing value was a definition, not a forward reference.
594 error("Redefinition of label " + ID.getName());
595
596 ID.destroy(); // Free strdup'd memory.
597 return BB;
598 }
599
600 // Otherwise this block has not been seen before.
601 BB = new BasicBlock("", CurFun.CurrentFunction);
602 if (ID.Type == ValID::NameVal) {
603 BB->setName(ID.Name);
604 } else {
605 CurFun.NumberedBlocks[ID.Num] = BB;
606 }
607
608 // If this is not a definition, keep track of it so we can use it as a forward
609 // reference.
610 if (!isDefinition) {
611 // Remember where this forward reference came from.
612 CurFun.BBForwardRefs[BB] = std::make_pair(ID, Upgradelineno);
613 } else {
614 // The forward declaration could have been inserted anywhere in the
615 // function: insert it into the correct place now.
616 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
617 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
618 }
619 ID.destroy();
620 return BB;
621}
622
623
624//===----------------------------------------------------------------------===//
625// Code to handle forward references in instructions
626//===----------------------------------------------------------------------===//
627//
628// This code handles the late binding needed with statements that reference
629// values not defined yet... for example, a forward branch, or the PHI node for
630// a loop body.
631//
632// This keeps a table (CurFun.LateResolveValues) of all such forward references
633// and back patchs after we are done.
634//
635
636// ResolveDefinitions - If we could not resolve some defs at parsing
637// time (forward branches, phi functions for loops, etc...) resolve the
638// defs now...
639//
640static void
641ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
642 std::map<const Type*,ValueList> *FutureLateResolvers) {
Reid Spencered96d1e2007-02-08 09:08:52 +0000643
Reid Spencer950bf602007-01-26 08:19:09 +0000644 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
645 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
646 E = LateResolvers.end(); LRI != E; ++LRI) {
Reid Spencered96d1e2007-02-08 09:08:52 +0000647 const Type* Ty = LRI->first;
Reid Spencer950bf602007-01-26 08:19:09 +0000648 ValueList &List = LRI->second;
649 while (!List.empty()) {
650 Value *V = List.back();
651 List.pop_back();
652
653 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
654 CurModule.PlaceHolderInfo.find(V);
655 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error");
656
657 ValID &DID = PHI->second.first;
658
Reid Spencered96d1e2007-02-08 09:08:52 +0000659 Value *TheRealValue = getExistingValue(Ty, DID);
Reid Spencer950bf602007-01-26 08:19:09 +0000660 if (TheRealValue) {
661 V->replaceAllUsesWith(TheRealValue);
662 delete V;
663 CurModule.PlaceHolderInfo.erase(PHI);
664 } else if (FutureLateResolvers) {
665 // Functions have their unresolved items forwarded to the module late
666 // resolver table
667 InsertValue(V, *FutureLateResolvers);
668 } else {
669 if (DID.Type == ValID::NameVal) {
Reid Spencered96d1e2007-02-08 09:08:52 +0000670 error("Reference to an invalid definition: '" + DID.getName() +
671 "' of type '" + V->getType()->getDescription() + "'",
672 PHI->second.second);
Reid Spencer7de2e012007-01-29 19:08:46 +0000673 return;
Reid Spencer950bf602007-01-26 08:19:09 +0000674 } else {
675 error("Reference to an invalid definition: #" +
676 itostr(DID.Num) + " of type '" +
677 V->getType()->getDescription() + "'", PHI->second.second);
678 return;
679 }
680 }
681 }
682 }
683
684 LateResolvers.clear();
685}
686
Reid Spencerbb1fd572007-03-21 17:15:50 +0000687/// This function is used for type resolution and upref handling. When a type
688/// becomes concrete, this function is called to adjust the signedness for the
689/// concrete type.
690static void ResolveTypeSign(const Type* oldTy, const Signedness &Sign) {
691 std::string TyName = CurModule.CurrentModule->getTypeName(oldTy);
692 if (!TyName.empty())
693 CurModule.NamedTypeSigns[TyName] = Sign;
694}
695
696/// ResolveTypeTo - A brand new type was just declared. This means that (if
697/// name is not null) things referencing Name can be resolved. Otherwise,
698/// things refering to the number can be resolved. Do this now.
699static void ResolveTypeTo(char *Name, const Type *ToTy, const Signedness& Sign){
Reid Spencer950bf602007-01-26 08:19:09 +0000700 ValID D;
Reid Spencerbb1fd572007-03-21 17:15:50 +0000701 if (Name)
702 D = ValID::create(Name);
703 else
704 D = ValID::create((int)CurModule.Types.size());
705 D.S.copy(Sign);
706
Reid Spencerbaba98a2007-04-11 12:10:58 +0000707 if (Name)
708 CurModule.NamedTypeSigns[Name] = Sign;
Reid Spencer950bf602007-01-26 08:19:09 +0000709
710 std::map<ValID, PATypeHolder>::iterator I =
711 CurModule.LateResolveTypes.find(D);
712 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencerbb1fd572007-03-21 17:15:50 +0000713 const Type *OldTy = I->second.get();
714 ((DerivedType*)OldTy)->refineAbstractTypeTo(ToTy);
Reid Spencer950bf602007-01-26 08:19:09 +0000715 CurModule.LateResolveTypes.erase(I);
716 }
717}
718
Anton Korobeynikovce13b852007-01-28 15:25:24 +0000719/// This is the implementation portion of TypeHasInteger. It traverses the
720/// type given, avoiding recursive types, and returns true as soon as it finds
721/// an integer type. If no integer type is found, it returns false.
722static bool TypeHasIntegerI(const Type *Ty, std::vector<const Type*> Stack) {
723 // Handle some easy cases
724 if (Ty->isPrimitiveType() || (Ty->getTypeID() == Type::OpaqueTyID))
725 return false;
726 if (Ty->isInteger())
727 return true;
728 if (const SequentialType *STy = dyn_cast<SequentialType>(Ty))
729 return STy->getElementType()->isInteger();
730
731 // Avoid type structure recursion
732 for (std::vector<const Type*>::iterator I = Stack.begin(), E = Stack.end();
733 I != E; ++I)
734 if (Ty == *I)
735 return false;
736
737 // Push us on the type stack
738 Stack.push_back(Ty);
739
740 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
741 if (TypeHasIntegerI(FTy->getReturnType(), Stack))
742 return true;
743 FunctionType::param_iterator I = FTy->param_begin();
744 FunctionType::param_iterator E = FTy->param_end();
745 for (; I != E; ++I)
746 if (TypeHasIntegerI(*I, Stack))
747 return true;
748 return false;
749 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
750 StructType::element_iterator I = STy->element_begin();
751 StructType::element_iterator E = STy->element_end();
752 for (; I != E; ++I) {
753 if (TypeHasIntegerI(*I, Stack))
754 return true;
755 }
756 return false;
757 }
758 // There shouldn't be anything else, but its definitely not integer
759 assert(0 && "What type is this?");
760 return false;
761}
762
763/// This is the interface to TypeHasIntegerI. It just provides the type stack,
764/// to avoid recursion, and then calls TypeHasIntegerI.
765static inline bool TypeHasInteger(const Type *Ty) {
766 std::vector<const Type*> TyStack;
767 return TypeHasIntegerI(Ty, TyStack);
768}
769
Reid Spencer950bf602007-01-26 08:19:09 +0000770// setValueName - Set the specified value to the name given. The name may be
771// null potentially, in which case this is a noop. The string passed in is
772// assumed to be a malloc'd string buffer, and is free'd by this function.
773//
Reid Spencerbb1fd572007-03-21 17:15:50 +0000774static void setValueName(const ValueInfo &V, char *NameStr) {
Reid Spencer950bf602007-01-26 08:19:09 +0000775 if (NameStr) {
776 std::string Name(NameStr); // Copy string
777 free(NameStr); // Free old string
778
Reid Spencerbb1fd572007-03-21 17:15:50 +0000779 if (V.V->getType() == Type::VoidTy) {
Reid Spencer950bf602007-01-26 08:19:09 +0000780 error("Can't assign name '" + Name + "' to value with void type");
781 return;
782 }
783
Reid Spencer950bf602007-01-26 08:19:09 +0000784 assert(inFunctionScope() && "Must be in function scope");
785
786 // Search the function's symbol table for an existing value of this name
Reid Spenceref9b9a72007-02-05 20:47:22 +0000787 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
788 Value* Existing = ST.lookup(Name);
Reid Spencer950bf602007-01-26 08:19:09 +0000789 if (Existing) {
Anton Korobeynikovce13b852007-01-28 15:25:24 +0000790 // An existing value of the same name was found. This might have happened
791 // because of the integer type planes collapsing in LLVM 2.0.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000792 if (Existing->getType() == V.V->getType() &&
Anton Korobeynikovce13b852007-01-28 15:25:24 +0000793 !TypeHasInteger(Existing->getType())) {
794 // If the type does not contain any integers in them then this can't be
795 // a type plane collapsing issue. It truly is a redefinition and we
796 // should error out as the assembly is invalid.
797 error("Redefinition of value named '" + Name + "' of type '" +
Reid Spencerbb1fd572007-03-21 17:15:50 +0000798 V.V->getType()->getDescription() + "'");
Anton Korobeynikovce13b852007-01-28 15:25:24 +0000799 return;
Reid Spencer950bf602007-01-26 08:19:09 +0000800 }
801 // In LLVM 2.0 we don't allow names to be re-used for any values in a
802 // function, regardless of Type. Previously re-use of names was okay as
803 // long as they were distinct types. With type planes collapsing because
804 // of the signedness change and because of PR411, this can no longer be
805 // supported. We must search the entire symbol table for a conflicting
806 // name and make the name unique. No warning is needed as this can't
807 // cause a problem.
808 std::string NewName = makeNameUnique(Name);
809 // We're changing the name but it will probably be used by other
810 // instructions as operands later on. Consequently we have to retain
811 // a mapping of the renaming that we're doing.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000812 RenameMapKey Key = makeRenameMapKey(Name, V.V->getType(), V.S);
Reid Spencer950bf602007-01-26 08:19:09 +0000813 CurFun.RenameMap[Key] = NewName;
814 Name = NewName;
815 }
816
817 // Set the name.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000818 V.V->setName(Name);
Reid Spencer950bf602007-01-26 08:19:09 +0000819 }
820}
821
822/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
823/// this is a declaration, otherwise it is a definition.
824static GlobalVariable *
825ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
826 bool isConstantGlobal, const Type *Ty,
Reid Spencerbb1fd572007-03-21 17:15:50 +0000827 Constant *Initializer,
828 const Signedness &Sign) {
Reid Spencer950bf602007-01-26 08:19:09 +0000829 if (isa<FunctionType>(Ty))
830 error("Cannot declare global vars of function type");
831
Christopher Lamb4374f8e2007-12-17 01:17:35 +0000832 const PointerType *PTy = PointerType::getUnqual(Ty);
Reid Spencer950bf602007-01-26 08:19:09 +0000833
834 std::string Name;
835 if (NameStr) {
836 Name = NameStr; // Copy string
837 free(NameStr); // Free old string
838 }
839
840 // See if this global value was forward referenced. If so, recycle the
841 // object.
842 ValID ID;
843 if (!Name.empty()) {
Reid Spencer5eb77c72007-03-15 03:26:42 +0000844 ID = ValID::create((char*)Name.c_str());
Reid Spencer950bf602007-01-26 08:19:09 +0000845 } else {
Reid Spencer5eb77c72007-03-15 03:26:42 +0000846 ID = ValID::create((int)CurModule.Values[PTy].size());
Reid Spencer950bf602007-01-26 08:19:09 +0000847 }
Reid Spencerbb1fd572007-03-21 17:15:50 +0000848 ID.S.makeComposite(Sign);
Reid Spencer950bf602007-01-26 08:19:09 +0000849
850 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
851 // Move the global to the end of the list, from whereever it was
852 // previously inserted.
853 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
854 CurModule.CurrentModule->getGlobalList().remove(GV);
855 CurModule.CurrentModule->getGlobalList().push_back(GV);
856 GV->setInitializer(Initializer);
857 GV->setLinkage(Linkage);
858 GV->setConstant(isConstantGlobal);
859 InsertValue(GV, CurModule.Values);
860 return GV;
861 }
862
863 // If this global has a name, check to see if there is already a definition
864 // of this global in the module and emit warnings if there are conflicts.
865 if (!Name.empty()) {
866 // The global has a name. See if there's an existing one of the same name.
Reid Spencere59f4932007-04-16 03:05:01 +0000867 if (CurModule.CurrentModule->getNamedGlobal(Name) ||
868 CurModule.CurrentModule->getFunction(Name)) {
869 // We found an existing global of the same name. This isn't allowed
Reid Spencer950bf602007-01-26 08:19:09 +0000870 // in LLVM 2.0. Consequently, we must alter the name of the global so it
871 // can at least compile. This can happen because of type planes
872 // There is alread a global of the same name which means there is a
873 // conflict. Let's see what we can do about it.
874 std::string NewName(makeNameUnique(Name));
Reid Spencerbb1fd572007-03-21 17:15:50 +0000875 if (Linkage != GlobalValue::InternalLinkage) {
Reid Spencer950bf602007-01-26 08:19:09 +0000876 // The linkage of this gval is external so we can't reliably rename
877 // it because it could potentially create a linking problem.
878 // However, we can't leave the name conflict in the output either or
879 // it won't assemble with LLVM 2.0. So, all we can do is rename
880 // this one to something unique and emit a warning about the problem.
881 warning("Renaming global variable '" + Name + "' to '" + NewName +
882 "' may cause linkage errors");
883 }
884
885 // Put the renaming in the global rename map
Christopher Lamb4374f8e2007-12-17 01:17:35 +0000886 RenameMapKey Key =
887 makeRenameMapKey(Name, PointerType::getUnqual(Ty), ID.S);
Reid Spencer950bf602007-01-26 08:19:09 +0000888 CurModule.RenameMap[Key] = NewName;
889
890 // Rename it
891 Name = NewName;
892 }
893 }
894
895 // Otherwise there is no existing GV to use, create one now.
896 GlobalVariable *GV =
897 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
898 CurModule.CurrentModule);
899 InsertValue(GV, CurModule.Values);
Reid Spencerbb1fd572007-03-21 17:15:50 +0000900 // Remember the sign of this global.
901 CurModule.NamedValueSigns[Name] = ID.S;
Reid Spencer950bf602007-01-26 08:19:09 +0000902 return GV;
903}
904
905// setTypeName - Set the specified type to the name given. The name may be
906// null potentially, in which case this is a noop. The string passed in is
907// assumed to be a malloc'd string buffer, and is freed by this function.
908//
909// This function returns true if the type has already been defined, but is
910// allowed to be redefined in the specified context. If the name is a new name
911// for the type plane, it is inserted and false is returned.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000912static bool setTypeName(const PATypeInfo& TI, char *NameStr) {
Reid Spencer950bf602007-01-26 08:19:09 +0000913 assert(!inFunctionScope() && "Can't give types function-local names");
914 if (NameStr == 0) return false;
915
916 std::string Name(NameStr); // Copy string
917 free(NameStr); // Free old string
918
Reid Spencerbb1fd572007-03-21 17:15:50 +0000919 const Type* Ty = TI.PAT->get();
920
Reid Spencer950bf602007-01-26 08:19:09 +0000921 // We don't allow assigning names to void type
Reid Spencerbb1fd572007-03-21 17:15:50 +0000922 if (Ty == Type::VoidTy) {
Reid Spencer950bf602007-01-26 08:19:09 +0000923 error("Can't assign name '" + Name + "' to the void type");
924 return false;
925 }
926
927 // Set the type name, checking for conflicts as we do so.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000928 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, Ty);
929
930 // Save the sign information for later use
931 CurModule.NamedTypeSigns[Name] = TI.S;
Reid Spencer950bf602007-01-26 08:19:09 +0000932
933 if (AlreadyExists) { // Inserting a name that is already defined???
934 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
935 assert(Existing && "Conflict but no matching type?");
936
937 // There is only one case where this is allowed: when we are refining an
938 // opaque type. In this case, Existing will be an opaque type.
939 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
940 // We ARE replacing an opaque type!
Reid Spencerbb1fd572007-03-21 17:15:50 +0000941 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(Ty);
Reid Spencer950bf602007-01-26 08:19:09 +0000942 return true;
943 }
944
945 // Otherwise, this is an attempt to redefine a type. That's okay if
946 // the redefinition is identical to the original. This will be so if
947 // Existing and T point to the same Type object. In this one case we
948 // allow the equivalent redefinition.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000949 if (Existing == Ty) return true; // Yes, it's equal.
Reid Spencer950bf602007-01-26 08:19:09 +0000950
951 // Any other kind of (non-equivalent) redefinition is an error.
952 error("Redefinition of type named '" + Name + "' in the '" +
Reid Spencerbb1fd572007-03-21 17:15:50 +0000953 Ty->getDescription() + "' type plane");
Reid Spencer950bf602007-01-26 08:19:09 +0000954 }
955
956 return false;
957}
958
959//===----------------------------------------------------------------------===//
960// Code for handling upreferences in type names...
961//
962
963// TypeContains - Returns true if Ty directly contains E in it.
964//
965static bool TypeContains(const Type *Ty, const Type *E) {
966 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
967 E) != Ty->subtype_end();
968}
969
970namespace {
971 struct UpRefRecord {
972 // NestingLevel - The number of nesting levels that need to be popped before
973 // this type is resolved.
974 unsigned NestingLevel;
975
976 // LastContainedTy - This is the type at the current binding level for the
977 // type. Every time we reduce the nesting level, this gets updated.
978 const Type *LastContainedTy;
979
980 // UpRefTy - This is the actual opaque type that the upreference is
981 // represented with.
982 OpaqueType *UpRefTy;
983
984 UpRefRecord(unsigned NL, OpaqueType *URTy)
Reid Spencerbb1fd572007-03-21 17:15:50 +0000985 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) { }
Reid Spencer950bf602007-01-26 08:19:09 +0000986 };
987}
988
989// UpRefs - A list of the outstanding upreferences that need to be resolved.
990static std::vector<UpRefRecord> UpRefs;
991
992/// HandleUpRefs - Every time we finish a new layer of types, this function is
993/// called. It loops through the UpRefs vector, which is a list of the
994/// currently active types. For each type, if the up reference is contained in
995/// the newly completed type, we decrement the level count. When the level
996/// count reaches zero, the upreferenced type is the type that is passed in:
997/// thus we can complete the cycle.
998///
Reid Spencerbb1fd572007-03-21 17:15:50 +0000999static PATypeHolder HandleUpRefs(const Type *ty, const Signedness& Sign) {
Reid Spencer950bf602007-01-26 08:19:09 +00001000 // If Ty isn't abstract, or if there are no up-references in it, then there is
1001 // nothing to resolve here.
1002 if (!ty->isAbstract() || UpRefs.empty()) return ty;
1003
1004 PATypeHolder Ty(ty);
1005 UR_OUT("Type '" << Ty->getDescription() <<
1006 "' newly formed. Resolving upreferences.\n" <<
1007 UpRefs.size() << " upreferences active!\n");
1008
1009 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
1010 // to zero), we resolve them all together before we resolve them to Ty. At
1011 // the end of the loop, if there is anything to resolve to Ty, it will be in
1012 // this variable.
1013 OpaqueType *TypeToResolve = 0;
1014
Reid Spencerbb1fd572007-03-21 17:15:50 +00001015 unsigned i = 0;
1016 for (; i != UpRefs.size(); ++i) {
Reid Spencer950bf602007-01-26 08:19:09 +00001017 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Reid Spencerbb1fd572007-03-21 17:15:50 +00001018 << UpRefs[i].UpRefTy->getDescription() << ") = "
1019 << (TypeContains(Ty, UpRefs[i].UpRefTy) ? "true" : "false") << "\n");
Reid Spencer950bf602007-01-26 08:19:09 +00001020 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
1021 // Decrement level of upreference
1022 unsigned Level = --UpRefs[i].NestingLevel;
1023 UpRefs[i].LastContainedTy = Ty;
1024 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
1025 if (Level == 0) { // Upreference should be resolved!
1026 if (!TypeToResolve) {
1027 TypeToResolve = UpRefs[i].UpRefTy;
1028 } else {
1029 UR_OUT(" * Resolving upreference for "
Reid Spencerbb1fd572007-03-21 17:15:50 +00001030 << UpRefs[i].UpRefTy->getDescription() << "\n";
1031 std::string OldName = UpRefs[i].UpRefTy->getDescription());
1032 ResolveTypeSign(UpRefs[i].UpRefTy, Sign);
Reid Spencer950bf602007-01-26 08:19:09 +00001033 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
1034 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
1035 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
1036 }
1037 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
1038 --i; // Do not skip the next element...
1039 }
1040 }
1041 }
1042
1043 if (TypeToResolve) {
1044 UR_OUT(" * Resolving upreference for "
Reid Spencerbb1fd572007-03-21 17:15:50 +00001045 << UpRefs[i].UpRefTy->getDescription() << "\n";
Reid Spencer950bf602007-01-26 08:19:09 +00001046 std::string OldName = TypeToResolve->getDescription());
Reid Spencerbb1fd572007-03-21 17:15:50 +00001047 ResolveTypeSign(TypeToResolve, Sign);
Reid Spencer950bf602007-01-26 08:19:09 +00001048 TypeToResolve->refineAbstractTypeTo(Ty);
1049 }
1050
1051 return Ty;
1052}
1053
Reid Spencerbb1fd572007-03-21 17:15:50 +00001054bool Signedness::operator<(const Signedness &that) const {
1055 if (isNamed()) {
1056 if (that.isNamed())
1057 return *(this->name) < *(that.name);
1058 else
1059 return CurModule.NamedTypeSigns[*name] < that;
1060 } else if (that.isNamed()) {
1061 return *this < CurModule.NamedTypeSigns[*that.name];
1062 }
1063
1064 if (isComposite() && that.isComposite()) {
1065 if (sv->size() == that.sv->size()) {
1066 SignVector::const_iterator thisI = sv->begin(), thisE = sv->end();
1067 SignVector::const_iterator thatI = that.sv->begin(),
1068 thatE = that.sv->end();
1069 for (; thisI != thisE; ++thisI, ++thatI) {
1070 if (*thisI < *thatI)
1071 return true;
1072 else if (!(*thisI == *thatI))
1073 return false;
1074 }
1075 return false;
1076 }
1077 return sv->size() < that.sv->size();
1078 }
1079 return kind < that.kind;
1080}
1081
1082bool Signedness::operator==(const Signedness &that) const {
1083 if (isNamed())
1084 if (that.isNamed())
1085 return *(this->name) == *(that.name);
1086 else
1087 return CurModule.NamedTypeSigns[*(this->name)] == that;
1088 else if (that.isNamed())
1089 return *this == CurModule.NamedTypeSigns[*(that.name)];
1090 if (isComposite() && that.isComposite()) {
1091 if (sv->size() == that.sv->size()) {
1092 SignVector::const_iterator thisI = sv->begin(), thisE = sv->end();
1093 SignVector::const_iterator thatI = that.sv->begin(),
1094 thatE = that.sv->end();
1095 for (; thisI != thisE; ++thisI, ++thatI) {
1096 if (!(*thisI == *thatI))
1097 return false;
1098 }
1099 return true;
1100 }
1101 return false;
1102 }
1103 return kind == that.kind;
1104}
1105
1106void Signedness::copy(const Signedness &that) {
1107 if (that.isNamed()) {
1108 kind = Named;
1109 name = new std::string(*that.name);
1110 } else if (that.isComposite()) {
1111 kind = Composite;
1112 sv = new SignVector();
1113 *sv = *that.sv;
1114 } else {
1115 kind = that.kind;
1116 sv = 0;
1117 }
1118}
1119
1120void Signedness::destroy() {
1121 if (isNamed()) {
1122 delete name;
1123 } else if (isComposite()) {
1124 delete sv;
1125 }
1126}
1127
Evan Cheng2b484202007-03-22 07:43:51 +00001128#ifndef NDEBUG
Reid Spencerbb1fd572007-03-21 17:15:50 +00001129void Signedness::dump() const {
1130 if (isComposite()) {
1131 if (sv->size() == 1) {
1132 (*sv)[0].dump();
1133 std::cerr << "*";
1134 } else {
1135 std::cerr << "{ " ;
1136 for (unsigned i = 0; i < sv->size(); ++i) {
1137 if (i != 0)
1138 std::cerr << ", ";
1139 (*sv)[i].dump();
1140 }
1141 std::cerr << "} " ;
1142 }
1143 } else if (isNamed()) {
1144 std::cerr << *name;
1145 } else if (isSigned()) {
1146 std::cerr << "S";
1147 } else if (isUnsigned()) {
1148 std::cerr << "U";
1149 } else
1150 std::cerr << ".";
1151}
Evan Cheng2b484202007-03-22 07:43:51 +00001152#endif
Reid Spencerbb1fd572007-03-21 17:15:50 +00001153
Reid Spencer950bf602007-01-26 08:19:09 +00001154static inline Instruction::TermOps
1155getTermOp(TermOps op) {
1156 switch (op) {
1157 default : assert(0 && "Invalid OldTermOp");
1158 case RetOp : return Instruction::Ret;
1159 case BrOp : return Instruction::Br;
1160 case SwitchOp : return Instruction::Switch;
1161 case InvokeOp : return Instruction::Invoke;
1162 case UnwindOp : return Instruction::Unwind;
1163 case UnreachableOp: return Instruction::Unreachable;
1164 }
1165}
1166
1167static inline Instruction::BinaryOps
Reid Spencerbb1fd572007-03-21 17:15:50 +00001168getBinaryOp(BinaryOps op, const Type *Ty, const Signedness& Sign) {
Reid Spencer950bf602007-01-26 08:19:09 +00001169 switch (op) {
1170 default : assert(0 && "Invalid OldBinaryOps");
1171 case SetEQ :
1172 case SetNE :
1173 case SetLE :
1174 case SetGE :
1175 case SetLT :
1176 case SetGT : assert(0 && "Should use getCompareOp");
1177 case AddOp : return Instruction::Add;
1178 case SubOp : return Instruction::Sub;
1179 case MulOp : return Instruction::Mul;
1180 case DivOp : {
1181 // This is an obsolete instruction so we must upgrade it based on the
1182 // types of its operands.
1183 bool isFP = Ty->isFloatingPoint();
Reid Spencer9d6565a2007-02-15 02:26:10 +00001184 if (const VectorType* PTy = dyn_cast<VectorType>(Ty))
Chris Lattner4227bdb2007-02-19 07:34:02 +00001185 // If its a vector type we want to use the element type
Reid Spencer950bf602007-01-26 08:19:09 +00001186 isFP = PTy->getElementType()->isFloatingPoint();
1187 if (isFP)
1188 return Instruction::FDiv;
Reid Spencerbb1fd572007-03-21 17:15:50 +00001189 else if (Sign.isSigned())
Reid Spencer950bf602007-01-26 08:19:09 +00001190 return Instruction::SDiv;
1191 return Instruction::UDiv;
1192 }
1193 case UDivOp : return Instruction::UDiv;
1194 case SDivOp : return Instruction::SDiv;
1195 case FDivOp : return Instruction::FDiv;
1196 case RemOp : {
1197 // This is an obsolete instruction so we must upgrade it based on the
1198 // types of its operands.
1199 bool isFP = Ty->isFloatingPoint();
Reid Spencer9d6565a2007-02-15 02:26:10 +00001200 if (const VectorType* PTy = dyn_cast<VectorType>(Ty))
Chris Lattner4227bdb2007-02-19 07:34:02 +00001201 // If its a vector type we want to use the element type
Reid Spencer950bf602007-01-26 08:19:09 +00001202 isFP = PTy->getElementType()->isFloatingPoint();
1203 // Select correct opcode
1204 if (isFP)
1205 return Instruction::FRem;
Reid Spencerbb1fd572007-03-21 17:15:50 +00001206 else if (Sign.isSigned())
Reid Spencer950bf602007-01-26 08:19:09 +00001207 return Instruction::SRem;
1208 return Instruction::URem;
1209 }
1210 case URemOp : return Instruction::URem;
1211 case SRemOp : return Instruction::SRem;
1212 case FRemOp : return Instruction::FRem;
Reid Spencer832254e2007-02-02 02:16:23 +00001213 case LShrOp : return Instruction::LShr;
1214 case AShrOp : return Instruction::AShr;
1215 case ShlOp : return Instruction::Shl;
1216 case ShrOp :
Reid Spencerbb1fd572007-03-21 17:15:50 +00001217 if (Sign.isSigned())
Reid Spencer832254e2007-02-02 02:16:23 +00001218 return Instruction::AShr;
1219 return Instruction::LShr;
Reid Spencer950bf602007-01-26 08:19:09 +00001220 case AndOp : return Instruction::And;
1221 case OrOp : return Instruction::Or;
1222 case XorOp : return Instruction::Xor;
1223 }
1224}
1225
1226static inline Instruction::OtherOps
1227getCompareOp(BinaryOps op, unsigned short &predicate, const Type* &Ty,
Reid Spencerbb1fd572007-03-21 17:15:50 +00001228 const Signedness &Sign) {
1229 bool isSigned = Sign.isSigned();
Reid Spencer950bf602007-01-26 08:19:09 +00001230 bool isFP = Ty->isFloatingPoint();
1231 switch (op) {
1232 default : assert(0 && "Invalid OldSetCC");
1233 case SetEQ :
1234 if (isFP) {
1235 predicate = FCmpInst::FCMP_OEQ;
1236 return Instruction::FCmp;
1237 } else {
1238 predicate = ICmpInst::ICMP_EQ;
1239 return Instruction::ICmp;
1240 }
1241 case SetNE :
1242 if (isFP) {
1243 predicate = FCmpInst::FCMP_UNE;
1244 return Instruction::FCmp;
1245 } else {
1246 predicate = ICmpInst::ICMP_NE;
1247 return Instruction::ICmp;
1248 }
1249 case SetLE :
1250 if (isFP) {
1251 predicate = FCmpInst::FCMP_OLE;
1252 return Instruction::FCmp;
1253 } else {
1254 if (isSigned)
1255 predicate = ICmpInst::ICMP_SLE;
1256 else
1257 predicate = ICmpInst::ICMP_ULE;
1258 return Instruction::ICmp;
1259 }
1260 case SetGE :
1261 if (isFP) {
1262 predicate = FCmpInst::FCMP_OGE;
1263 return Instruction::FCmp;
1264 } else {
1265 if (isSigned)
1266 predicate = ICmpInst::ICMP_SGE;
1267 else
1268 predicate = ICmpInst::ICMP_UGE;
1269 return Instruction::ICmp;
1270 }
1271 case SetLT :
1272 if (isFP) {
1273 predicate = FCmpInst::FCMP_OLT;
1274 return Instruction::FCmp;
1275 } else {
1276 if (isSigned)
1277 predicate = ICmpInst::ICMP_SLT;
1278 else
1279 predicate = ICmpInst::ICMP_ULT;
1280 return Instruction::ICmp;
1281 }
1282 case SetGT :
1283 if (isFP) {
1284 predicate = FCmpInst::FCMP_OGT;
1285 return Instruction::FCmp;
1286 } else {
1287 if (isSigned)
1288 predicate = ICmpInst::ICMP_SGT;
1289 else
1290 predicate = ICmpInst::ICMP_UGT;
1291 return Instruction::ICmp;
1292 }
1293 }
1294}
1295
1296static inline Instruction::MemoryOps getMemoryOp(MemoryOps op) {
1297 switch (op) {
1298 default : assert(0 && "Invalid OldMemoryOps");
1299 case MallocOp : return Instruction::Malloc;
1300 case FreeOp : return Instruction::Free;
1301 case AllocaOp : return Instruction::Alloca;
1302 case LoadOp : return Instruction::Load;
1303 case StoreOp : return Instruction::Store;
1304 case GetElementPtrOp : return Instruction::GetElementPtr;
1305 }
1306}
1307
1308static inline Instruction::OtherOps
Reid Spencerbb1fd572007-03-21 17:15:50 +00001309getOtherOp(OtherOps op, const Signedness &Sign) {
Reid Spencer950bf602007-01-26 08:19:09 +00001310 switch (op) {
1311 default : assert(0 && "Invalid OldOtherOps");
1312 case PHIOp : return Instruction::PHI;
1313 case CallOp : return Instruction::Call;
Reid Spencer950bf602007-01-26 08:19:09 +00001314 case SelectOp : return Instruction::Select;
1315 case UserOp1 : return Instruction::UserOp1;
1316 case UserOp2 : return Instruction::UserOp2;
1317 case VAArg : return Instruction::VAArg;
1318 case ExtractElementOp : return Instruction::ExtractElement;
1319 case InsertElementOp : return Instruction::InsertElement;
1320 case ShuffleVectorOp : return Instruction::ShuffleVector;
1321 case ICmpOp : return Instruction::ICmp;
1322 case FCmpOp : return Instruction::FCmp;
Reid Spencer950bf602007-01-26 08:19:09 +00001323 };
1324}
1325
1326static inline Value*
Reid Spencerbb1fd572007-03-21 17:15:50 +00001327getCast(CastOps op, Value *Src, const Signedness &SrcSign, const Type *DstTy,
1328 const Signedness &DstSign, bool ForceInstruction = false) {
Reid Spencer950bf602007-01-26 08:19:09 +00001329 Instruction::CastOps Opcode;
1330 const Type* SrcTy = Src->getType();
1331 if (op == CastOp) {
1332 if (SrcTy->isFloatingPoint() && isa<PointerType>(DstTy)) {
1333 // fp -> ptr cast is no longer supported but we must upgrade this
1334 // by doing a double cast: fp -> int -> ptr
1335 SrcTy = Type::Int64Ty;
1336 Opcode = Instruction::IntToPtr;
1337 if (isa<Constant>(Src)) {
1338 Src = ConstantExpr::getCast(Instruction::FPToUI,
1339 cast<Constant>(Src), SrcTy);
1340 } else {
1341 std::string NewName(makeNameUnique(Src->getName()));
1342 Src = new FPToUIInst(Src, SrcTy, NewName, CurBB);
1343 }
1344 } else if (isa<IntegerType>(DstTy) &&
1345 cast<IntegerType>(DstTy)->getBitWidth() == 1) {
1346 // cast type %x to bool was previously defined as setne type %x, null
1347 // The cast semantic is now to truncate, not compare so we must retain
1348 // the original intent by replacing the cast with a setne
1349 Constant* Null = Constant::getNullValue(SrcTy);
1350 Instruction::OtherOps Opcode = Instruction::ICmp;
1351 unsigned short predicate = ICmpInst::ICMP_NE;
1352 if (SrcTy->isFloatingPoint()) {
1353 Opcode = Instruction::FCmp;
1354 predicate = FCmpInst::FCMP_ONE;
1355 } else if (!SrcTy->isInteger() && !isa<PointerType>(SrcTy)) {
1356 error("Invalid cast to bool");
1357 }
1358 if (isa<Constant>(Src) && !ForceInstruction)
1359 return ConstantExpr::getCompare(predicate, cast<Constant>(Src), Null);
1360 else
1361 return CmpInst::create(Opcode, predicate, Src, Null);
1362 }
1363 // Determine the opcode to use by calling CastInst::getCastOpcode
1364 Opcode =
Reid Spencerbb1fd572007-03-21 17:15:50 +00001365 CastInst::getCastOpcode(Src, SrcSign.isSigned(), DstTy,
1366 DstSign.isSigned());
Reid Spencer950bf602007-01-26 08:19:09 +00001367
1368 } else switch (op) {
1369 default: assert(0 && "Invalid cast token");
1370 case TruncOp: Opcode = Instruction::Trunc; break;
1371 case ZExtOp: Opcode = Instruction::ZExt; break;
1372 case SExtOp: Opcode = Instruction::SExt; break;
1373 case FPTruncOp: Opcode = Instruction::FPTrunc; break;
1374 case FPExtOp: Opcode = Instruction::FPExt; break;
1375 case FPToUIOp: Opcode = Instruction::FPToUI; break;
1376 case FPToSIOp: Opcode = Instruction::FPToSI; break;
1377 case UIToFPOp: Opcode = Instruction::UIToFP; break;
1378 case SIToFPOp: Opcode = Instruction::SIToFP; break;
1379 case PtrToIntOp: Opcode = Instruction::PtrToInt; break;
1380 case IntToPtrOp: Opcode = Instruction::IntToPtr; break;
1381 case BitCastOp: Opcode = Instruction::BitCast; break;
1382 }
1383
1384 if (isa<Constant>(Src) && !ForceInstruction)
1385 return ConstantExpr::getCast(Opcode, cast<Constant>(Src), DstTy);
1386 return CastInst::create(Opcode, Src, DstTy);
1387}
1388
1389static Instruction *
1390upgradeIntrinsicCall(const Type* RetTy, const ValID &ID,
1391 std::vector<Value*>& Args) {
1392
1393 std::string Name = ID.Type == ValID::NameVal ? ID.Name : "";
Reid Spencer7eea8ff2007-05-18 05:48:07 +00001394 if (Name.length() <= 5 || Name[0] != 'l' || Name[1] != 'l' ||
1395 Name[2] != 'v' || Name[3] != 'm' || Name[4] != '.')
1396 return 0;
1397
Reid Spencer41b213e2007-04-02 01:14:00 +00001398 switch (Name[5]) {
1399 case 'i':
1400 if (Name == "llvm.isunordered.f32" || Name == "llvm.isunordered.f64") {
1401 if (Args.size() != 2)
1402 error("Invalid prototype for " + Name);
1403 return new FCmpInst(FCmpInst::FCMP_UNO, Args[0], Args[1]);
1404 }
1405 break;
Reid Spencer8166a6c2007-04-02 02:08:35 +00001406
Reid Spencer41b213e2007-04-02 01:14:00 +00001407 case 'v' : {
Christopher Lamb4374f8e2007-12-17 01:17:35 +00001408 const Type* PtrTy = PointerType::getUnqual(Type::Int8Ty);
Reid Spencer41b213e2007-04-02 01:14:00 +00001409 std::vector<const Type*> Params;
1410 if (Name == "llvm.va_start" || Name == "llvm.va_end") {
1411 if (Args.size() != 1)
1412 error("Invalid prototype for " + Name + " prototype");
1413 Params.push_back(PtrTy);
1414 const FunctionType *FTy =
1415 FunctionType::get(Type::VoidTy, Params, false);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00001416 const PointerType *PFTy = PointerType::getUnqual(FTy);
Reid Spencer41b213e2007-04-02 01:14:00 +00001417 Value* Func = getVal(PFTy, ID);
1418 Args[0] = new BitCastInst(Args[0], PtrTy, makeNameUnique("va"), CurBB);
David Greene718fda32007-08-01 03:59:32 +00001419 return new CallInst(Func, Args.begin(), Args.end());
Reid Spencer41b213e2007-04-02 01:14:00 +00001420 } else if (Name == "llvm.va_copy") {
1421 if (Args.size() != 2)
1422 error("Invalid prototype for " + Name + " prototype");
1423 Params.push_back(PtrTy);
1424 Params.push_back(PtrTy);
1425 const FunctionType *FTy =
1426 FunctionType::get(Type::VoidTy, Params, false);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00001427 const PointerType *PFTy = PointerType::getUnqual(FTy);
Reid Spencer41b213e2007-04-02 01:14:00 +00001428 Value* Func = getVal(PFTy, ID);
1429 std::string InstName0(makeNameUnique("va0"));
1430 std::string InstName1(makeNameUnique("va1"));
1431 Args[0] = new BitCastInst(Args[0], PtrTy, InstName0, CurBB);
1432 Args[1] = new BitCastInst(Args[1], PtrTy, InstName1, CurBB);
David Greene718fda32007-08-01 03:59:32 +00001433 return new CallInst(Func, Args.begin(), Args.end());
Reid Spencer41b213e2007-04-02 01:14:00 +00001434 }
Reid Spencer950bf602007-01-26 08:19:09 +00001435 }
1436 }
1437 return 0;
1438}
1439
Reid Spencerff0e4482007-04-16 00:40:57 +00001440const Type* upgradeGEPCEIndices(const Type* PTy,
1441 std::vector<ValueInfo> *Indices,
1442 std::vector<Constant*> &Result) {
1443 const Type *Ty = PTy;
1444 Result.clear();
1445 for (unsigned i = 0, e = Indices->size(); i != e ; ++i) {
1446 Constant *Index = cast<Constant>((*Indices)[i].V);
1447
1448 if (ConstantInt *CI = dyn_cast<ConstantInt>(Index)) {
1449 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte
1450 // struct indices to i32 struct indices with ZExt for compatibility.
1451 if (CI->getBitWidth() < 32)
1452 Index = ConstantExpr::getCast(Instruction::ZExt, CI, Type::Int32Ty);
1453 }
1454
1455 if (isa<SequentialType>(Ty)) {
1456 // Make sure that unsigned SequentialType indices are zext'd to
1457 // 64-bits if they were smaller than that because LLVM 2.0 will sext
1458 // all indices for SequentialType elements. We must retain the same
1459 // semantic (zext) for unsigned types.
1460 if (const IntegerType *Ity = dyn_cast<IntegerType>(Index->getType())) {
1461 if (Ity->getBitWidth() < 64 && (*Indices)[i].S.isUnsigned()) {
1462 Index = ConstantExpr::getCast(Instruction::ZExt, Index,Type::Int64Ty);
1463 }
1464 }
1465 }
1466 Result.push_back(Index);
David Greene5fd22a82007-09-04 18:46:50 +00001467 Ty = GetElementPtrInst::getIndexedType(PTy, Result.begin(),
1468 Result.end(),true);
Reid Spencerff0e4482007-04-16 00:40:57 +00001469 if (!Ty)
1470 error("Index list invalid for constant getelementptr");
1471 }
1472 return Ty;
1473}
1474
1475const Type* upgradeGEPInstIndices(const Type* PTy,
1476 std::vector<ValueInfo> *Indices,
1477 std::vector<Value*> &Result) {
1478 const Type *Ty = PTy;
1479 Result.clear();
1480 for (unsigned i = 0, e = Indices->size(); i != e ; ++i) {
1481 Value *Index = (*Indices)[i].V;
1482
1483 if (ConstantInt *CI = dyn_cast<ConstantInt>(Index)) {
1484 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte
1485 // struct indices to i32 struct indices with ZExt for compatibility.
1486 if (CI->getBitWidth() < 32)
1487 Index = ConstantExpr::getCast(Instruction::ZExt, CI, Type::Int32Ty);
1488 }
1489
1490
1491 if (isa<StructType>(Ty)) { // Only change struct indices
1492 if (!isa<Constant>(Index)) {
1493 error("Invalid non-constant structure index");
1494 return 0;
1495 }
Reid Spencer950bf602007-01-26 08:19:09 +00001496 } else {
1497 // Make sure that unsigned SequentialType indices are zext'd to
1498 // 64-bits if they were smaller than that because LLVM 2.0 will sext
1499 // all indices for SequentialType elements. We must retain the same
1500 // semantic (zext) for unsigned types.
Reid Spencerff0e4482007-04-16 00:40:57 +00001501 if (const IntegerType *Ity = dyn_cast<IntegerType>(Index->getType())) {
Reid Spencerbb1fd572007-03-21 17:15:50 +00001502 if (Ity->getBitWidth() < 64 && (*Indices)[i].S.isUnsigned()) {
Reid Spencerff0e4482007-04-16 00:40:57 +00001503 if (isa<Constant>(Index))
Reid Spencer950bf602007-01-26 08:19:09 +00001504 Index = ConstantExpr::getCast(Instruction::ZExt,
1505 cast<Constant>(Index), Type::Int64Ty);
1506 else
1507 Index = CastInst::create(Instruction::ZExt, Index, Type::Int64Ty,
Reid Spencer832254e2007-02-02 02:16:23 +00001508 makeNameUnique("gep"), CurBB);
Reid Spencer38f682b2007-01-26 20:31:18 +00001509 }
Reid Spencerff0e4482007-04-16 00:40:57 +00001510 }
Reid Spencer950bf602007-01-26 08:19:09 +00001511 }
Reid Spencerff0e4482007-04-16 00:40:57 +00001512 Result.push_back(Index);
David Greene5fd22a82007-09-04 18:46:50 +00001513 Ty = GetElementPtrInst::getIndexedType(PTy, Result.begin(),
1514 Result.end(),true);
Reid Spencerff0e4482007-04-16 00:40:57 +00001515 if (!Ty)
Reid Spencer950bf602007-01-26 08:19:09 +00001516 error("Index list invalid for constant getelementptr");
Reid Spencerff0e4482007-04-16 00:40:57 +00001517 }
1518 return Ty;
Reid Spencer950bf602007-01-26 08:19:09 +00001519}
1520
Reid Spencerb7046c72007-01-29 05:41:34 +00001521unsigned upgradeCallingConv(unsigned CC) {
1522 switch (CC) {
1523 case OldCallingConv::C : return CallingConv::C;
1524 case OldCallingConv::CSRet : return CallingConv::C;
1525 case OldCallingConv::Fast : return CallingConv::Fast;
1526 case OldCallingConv::Cold : return CallingConv::Cold;
1527 case OldCallingConv::X86_StdCall : return CallingConv::X86_StdCall;
1528 case OldCallingConv::X86_FastCall: return CallingConv::X86_FastCall;
1529 default:
1530 return CC;
1531 }
1532}
1533
Reid Spencer950bf602007-01-26 08:19:09 +00001534Module* UpgradeAssembly(const std::string &infile, std::istream& in,
1535 bool debug, bool addAttrs)
Reid Spencere7c3c602006-11-30 06:36:44 +00001536{
1537 Upgradelineno = 1;
1538 CurFilename = infile;
Reid Spencer96839be2006-11-30 16:50:26 +00001539 LexInput = &in;
Reid Spencere77e35e2006-12-01 20:26:20 +00001540 yydebug = debug;
Reid Spencer71d2ec92006-12-31 06:02:26 +00001541 AddAttributes = addAttrs;
Reid Spencer950bf602007-01-26 08:19:09 +00001542 ObsoleteVarArgs = false;
1543 NewVarArgs = false;
Reid Spencere7c3c602006-11-30 06:36:44 +00001544
Reid Spencer950bf602007-01-26 08:19:09 +00001545 CurModule.CurrentModule = new Module(CurFilename);
1546
1547 // Check to make sure the parser succeeded
Reid Spencere7c3c602006-11-30 06:36:44 +00001548 if (yyparse()) {
Reid Spencer950bf602007-01-26 08:19:09 +00001549 if (ParserResult)
1550 delete ParserResult;
Reid Spencer30d0c582007-01-15 00:26:18 +00001551 std::cerr << "llvm-upgrade: parse failed.\n";
Reid Spencer30d0c582007-01-15 00:26:18 +00001552 return 0;
1553 }
1554
Reid Spencer950bf602007-01-26 08:19:09 +00001555 // Check to make sure that parsing produced a result
1556 if (!ParserResult) {
1557 std::cerr << "llvm-upgrade: no parse result.\n";
1558 return 0;
Reid Spencer30d0c582007-01-15 00:26:18 +00001559 }
1560
Reid Spencer950bf602007-01-26 08:19:09 +00001561 // Reset ParserResult variable while saving its value for the result.
1562 Module *Result = ParserResult;
1563 ParserResult = 0;
Reid Spencer30d0c582007-01-15 00:26:18 +00001564
Reid Spencer950bf602007-01-26 08:19:09 +00001565 //Not all functions use vaarg, so make a second check for ObsoleteVarArgs
Reid Spencer30d0c582007-01-15 00:26:18 +00001566 {
Reid Spencer950bf602007-01-26 08:19:09 +00001567 Function* F;
Reid Spencer688b0492007-02-05 21:19:13 +00001568 if ((F = Result->getFunction("llvm.va_start"))
Reid Spencer950bf602007-01-26 08:19:09 +00001569 && F->getFunctionType()->getNumParams() == 0)
1570 ObsoleteVarArgs = true;
Reid Spencer688b0492007-02-05 21:19:13 +00001571 if((F = Result->getFunction("llvm.va_copy"))
Reid Spencer950bf602007-01-26 08:19:09 +00001572 && F->getFunctionType()->getNumParams() == 1)
1573 ObsoleteVarArgs = true;
Reid Spencer280d8012006-12-01 23:40:53 +00001574 }
Reid Spencer319a7302007-01-05 17:20:02 +00001575
Reid Spencer950bf602007-01-26 08:19:09 +00001576 if (ObsoleteVarArgs && NewVarArgs) {
1577 error("This file is corrupt: it uses both new and old style varargs");
1578 return 0;
Reid Spencer319a7302007-01-05 17:20:02 +00001579 }
Reid Spencer319a7302007-01-05 17:20:02 +00001580
Reid Spencer950bf602007-01-26 08:19:09 +00001581 if(ObsoleteVarArgs) {
Reid Spencer688b0492007-02-05 21:19:13 +00001582 if(Function* F = Result->getFunction("llvm.va_start")) {
Reid Spencer950bf602007-01-26 08:19:09 +00001583 if (F->arg_size() != 0) {
1584 error("Obsolete va_start takes 0 argument");
Reid Spencer319a7302007-01-05 17:20:02 +00001585 return 0;
1586 }
Reid Spencer950bf602007-01-26 08:19:09 +00001587
1588 //foo = va_start()
1589 // ->
1590 //bar = alloca typeof(foo)
1591 //va_start(bar)
1592 //foo = load bar
Reid Spencer319a7302007-01-05 17:20:02 +00001593
Reid Spencer950bf602007-01-26 08:19:09 +00001594 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
1595 const Type* ArgTy = F->getFunctionType()->getReturnType();
Christopher Lamb4374f8e2007-12-17 01:17:35 +00001596 const Type* ArgTyPtr = PointerType::getUnqual(ArgTy);
Reid Spencer950bf602007-01-26 08:19:09 +00001597 Function* NF = cast<Function>(Result->getOrInsertFunction(
1598 "llvm.va_start", RetTy, ArgTyPtr, (Type *)0));
1599
1600 while (!F->use_empty()) {
1601 CallInst* CI = cast<CallInst>(F->use_back());
1602 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
1603 new CallInst(NF, bar, "", CI);
1604 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
1605 CI->replaceAllUsesWith(foo);
1606 CI->getParent()->getInstList().erase(CI);
Reid Spencerf8383de2007-01-06 06:04:32 +00001607 }
Reid Spencer950bf602007-01-26 08:19:09 +00001608 Result->getFunctionList().erase(F);
Reid Spencerf8383de2007-01-06 06:04:32 +00001609 }
Reid Spencer950bf602007-01-26 08:19:09 +00001610
Reid Spencer688b0492007-02-05 21:19:13 +00001611 if(Function* F = Result->getFunction("llvm.va_end")) {
Reid Spencer950bf602007-01-26 08:19:09 +00001612 if(F->arg_size() != 1) {
1613 error("Obsolete va_end takes 1 argument");
1614 return 0;
Reid Spencerf8383de2007-01-06 06:04:32 +00001615 }
Reid Spencerf8383de2007-01-06 06:04:32 +00001616
Reid Spencer950bf602007-01-26 08:19:09 +00001617 //vaend foo
1618 // ->
1619 //bar = alloca 1 of typeof(foo)
1620 //vaend bar
1621 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
1622 const Type* ArgTy = F->getFunctionType()->getParamType(0);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00001623 const Type* ArgTyPtr = PointerType::getUnqual(ArgTy);
Reid Spencer950bf602007-01-26 08:19:09 +00001624 Function* NF = cast<Function>(Result->getOrInsertFunction(
1625 "llvm.va_end", RetTy, ArgTyPtr, (Type *)0));
Reid Spencerf8383de2007-01-06 06:04:32 +00001626
Reid Spencer950bf602007-01-26 08:19:09 +00001627 while (!F->use_empty()) {
1628 CallInst* CI = cast<CallInst>(F->use_back());
1629 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
1630 new StoreInst(CI->getOperand(1), bar, CI);
1631 new CallInst(NF, bar, "", CI);
1632 CI->getParent()->getInstList().erase(CI);
Reid Spencere77e35e2006-12-01 20:26:20 +00001633 }
Reid Spencer950bf602007-01-26 08:19:09 +00001634 Result->getFunctionList().erase(F);
Reid Spencere77e35e2006-12-01 20:26:20 +00001635 }
Reid Spencer950bf602007-01-26 08:19:09 +00001636
Reid Spencer688b0492007-02-05 21:19:13 +00001637 if(Function* F = Result->getFunction("llvm.va_copy")) {
Reid Spencer950bf602007-01-26 08:19:09 +00001638 if(F->arg_size() != 1) {
1639 error("Obsolete va_copy takes 1 argument");
1640 return 0;
Reid Spencere77e35e2006-12-01 20:26:20 +00001641 }
Reid Spencer950bf602007-01-26 08:19:09 +00001642 //foo = vacopy(bar)
1643 // ->
1644 //a = alloca 1 of typeof(foo)
1645 //b = alloca 1 of typeof(foo)
1646 //store bar -> b
1647 //vacopy(a, b)
1648 //foo = load a
1649
1650 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
1651 const Type* ArgTy = F->getFunctionType()->getReturnType();
Christopher Lamb4374f8e2007-12-17 01:17:35 +00001652 const Type* ArgTyPtr = PointerType::getUnqual(ArgTy);
Reid Spencer950bf602007-01-26 08:19:09 +00001653 Function* NF = cast<Function>(Result->getOrInsertFunction(
1654 "llvm.va_copy", RetTy, ArgTyPtr, ArgTyPtr, (Type *)0));
Reid Spencere77e35e2006-12-01 20:26:20 +00001655
Reid Spencer950bf602007-01-26 08:19:09 +00001656 while (!F->use_empty()) {
1657 CallInst* CI = cast<CallInst>(F->use_back());
David Greeneae5115d2007-08-15 17:58:51 +00001658 Value *Args[2] = {
1659 new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI),
1660 new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI)
1661 };
David Greene718fda32007-08-01 03:59:32 +00001662 new StoreInst(CI->getOperand(1), Args[1], CI);
David Greeneae5115d2007-08-15 17:58:51 +00001663 new CallInst(NF, Args, Args + 2, "", CI);
David Greene718fda32007-08-01 03:59:32 +00001664 Value* foo = new LoadInst(Args[0], "vacopy.fix.3", CI);
Reid Spencer950bf602007-01-26 08:19:09 +00001665 CI->replaceAllUsesWith(foo);
1666 CI->getParent()->getInstList().erase(CI);
1667 }
1668 Result->getFunctionList().erase(F);
Reid Spencer319a7302007-01-05 17:20:02 +00001669 }
1670 }
1671
Reid Spencer52402b02007-01-02 05:45:11 +00001672 return Result;
1673}
1674
Reid Spencer950bf602007-01-26 08:19:09 +00001675} // end llvm namespace
Reid Spencer319a7302007-01-05 17:20:02 +00001676
Reid Spencer950bf602007-01-26 08:19:09 +00001677using namespace llvm;
Reid Spencer30d0c582007-01-15 00:26:18 +00001678
Reid Spencere7c3c602006-11-30 06:36:44 +00001679%}
1680
Reid Spencere77e35e2006-12-01 20:26:20 +00001681%union {
Reid Spencer950bf602007-01-26 08:19:09 +00001682 llvm::Module *ModuleVal;
1683 llvm::Function *FunctionVal;
1684 std::pair<llvm::PATypeInfo, char*> *ArgVal;
1685 llvm::BasicBlock *BasicBlockVal;
Reid Spencerbb1fd572007-03-21 17:15:50 +00001686 llvm::TermInstInfo TermInstVal;
Reid Spencer950bf602007-01-26 08:19:09 +00001687 llvm::InstrInfo InstVal;
1688 llvm::ConstInfo ConstVal;
1689 llvm::ValueInfo ValueVal;
1690 llvm::PATypeInfo TypeVal;
1691 llvm::TypeInfo PrimType;
1692 llvm::PHIListInfo PHIList;
1693 std::list<llvm::PATypeInfo> *TypeList;
1694 std::vector<llvm::ValueInfo> *ValueList;
1695 std::vector<llvm::ConstInfo> *ConstVector;
1696
1697
1698 std::vector<std::pair<llvm::PATypeInfo,char*> > *ArgList;
1699 // Represent the RHS of PHI node
1700 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
1701
1702 llvm::GlobalValue::LinkageTypes Linkage;
1703 int64_t SInt64Val;
1704 uint64_t UInt64Val;
1705 int SIntVal;
1706 unsigned UIntVal;
Dale Johannesen43421b32007-09-06 18:13:44 +00001707 llvm::APFloat *FPVal;
Reid Spencer950bf602007-01-26 08:19:09 +00001708 bool BoolVal;
1709
1710 char *StrVal; // This memory is strdup'd!
1711 llvm::ValID ValIDVal; // strdup'd memory maybe!
1712
1713 llvm::BinaryOps BinaryOpVal;
1714 llvm::TermOps TermOpVal;
1715 llvm::MemoryOps MemOpVal;
1716 llvm::OtherOps OtherOpVal;
1717 llvm::CastOps CastOpVal;
1718 llvm::ICmpInst::Predicate IPred;
1719 llvm::FCmpInst::Predicate FPred;
1720 llvm::Module::Endianness Endianness;
Reid Spencere77e35e2006-12-01 20:26:20 +00001721}
1722
Reid Spencer950bf602007-01-26 08:19:09 +00001723%type <ModuleVal> Module FunctionList
1724%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
1725%type <BasicBlockVal> BasicBlock InstructionList
1726%type <TermInstVal> BBTerminatorInst
1727%type <InstVal> Inst InstVal MemoryInst
1728%type <ConstVal> ConstVal ConstExpr
1729%type <ConstVector> ConstVector
1730%type <ArgList> ArgList ArgListH
1731%type <ArgVal> ArgVal
1732%type <PHIList> PHIList
1733%type <ValueList> ValueRefList ValueRefListE // For call param lists
1734%type <ValueList> IndexList // For GEP derived indices
1735%type <TypeList> TypeListI ArgTypeListI
1736%type <JumpTable> JumpTable
1737%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
1738%type <BoolVal> OptVolatile // 'volatile' or not
1739%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1740%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencered96d1e2007-02-08 09:08:52 +00001741%type <Linkage> OptLinkage FnDeclareLinkage
Reid Spencer950bf602007-01-26 08:19:09 +00001742%type <Endianness> BigOrLittle
Reid Spencere77e35e2006-12-01 20:26:20 +00001743
Reid Spencer950bf602007-01-26 08:19:09 +00001744// ValueRef - Unresolved reference to a definition or BB
1745%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1746%type <ValueVal> ResolvedVal // <type> <valref> pair
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001747
Reid Spencer950bf602007-01-26 08:19:09 +00001748// Tokens and types for handling constant integer values
1749//
1750// ESINT64VAL - A negative number within long long range
1751%token <SInt64Val> ESINT64VAL
Reid Spencere77e35e2006-12-01 20:26:20 +00001752
Reid Spencer950bf602007-01-26 08:19:09 +00001753// EUINT64VAL - A positive number within uns. long long range
1754%token <UInt64Val> EUINT64VAL
1755%type <SInt64Val> EINT64VAL
Reid Spencere77e35e2006-12-01 20:26:20 +00001756
Reid Spencer950bf602007-01-26 08:19:09 +00001757%token <SIntVal> SINTVAL // Signed 32 bit ints...
1758%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
1759%type <SIntVal> INTVAL
1760%token <FPVal> FPVAL // Float or Double constant
Reid Spencere77e35e2006-12-01 20:26:20 +00001761
Reid Spencer950bf602007-01-26 08:19:09 +00001762// Built in types...
1763%type <TypeVal> Types TypesV UpRTypes UpRTypesV
1764%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
1765%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
1766%token <PrimType> FLOAT DOUBLE TYPE LABEL
Reid Spencere77e35e2006-12-01 20:26:20 +00001767
Reid Spencer950bf602007-01-26 08:19:09 +00001768%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
1769%type <StrVal> Name OptName OptAssign
1770%type <UIntVal> OptAlign OptCAlign
1771%type <StrVal> OptSection SectionString
1772
1773%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
1774%token DECLARE GLOBAL CONSTANT SECTION VOLATILE
1775%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
1776%token DLLIMPORT DLLEXPORT EXTERN_WEAK
1777%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
1778%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
1779%token CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
1780%token X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
1781%token DATALAYOUT
1782%type <UIntVal> OptCallingConv
1783
1784// Basic Block Terminating Operators
1785%token <TermOpVal> RET BR SWITCH INVOKE UNREACHABLE
1786%token UNWIND EXCEPT
1787
1788// Binary Operators
1789%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Reid Spencer832254e2007-02-02 02:16:23 +00001790%type <BinaryOpVal> ShiftOps
Reid Spencer950bf602007-01-26 08:19:09 +00001791%token <BinaryOpVal> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM
Reid Spencer832254e2007-02-02 02:16:23 +00001792%token <BinaryOpVal> AND OR XOR SHL SHR ASHR LSHR
Reid Spencer950bf602007-01-26 08:19:09 +00001793%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
1794%token <OtherOpVal> ICMP FCMP
1795
1796// Memory Instructions
1797%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1798
1799// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001800%token <OtherOpVal> PHI_TOK SELECT VAARG
Reid Spencer950bf602007-01-26 08:19:09 +00001801%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
1802%token VAARG_old VANEXT_old //OBSOLETE
1803
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001804// Support for ICmp/FCmp Predicates, which is 1.9++ but not 2.0
Reid Spencer950bf602007-01-26 08:19:09 +00001805%type <IPred> IPredicates
1806%type <FPred> FPredicates
1807%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1808%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
1809
1810%token <CastOpVal> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI
1811%token <CastOpVal> UITOFP SITOFP PTRTOINT INTTOPTR BITCAST
1812%type <CastOpVal> CastOps
Reid Spencere7c3c602006-11-30 06:36:44 +00001813
1814%start Module
1815
1816%%
1817
1818// Handle constant integer size restriction and conversion...
Reid Spencer950bf602007-01-26 08:19:09 +00001819//
1820INTVAL
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001821 : SINTVAL
Reid Spencer950bf602007-01-26 08:19:09 +00001822 | UINTVAL {
1823 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
1824 error("Value too large for type");
1825 $$ = (int32_t)$1;
1826 }
1827 ;
1828
1829EINT64VAL
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001830 : ESINT64VAL // These have same type and can't cause problems...
Reid Spencer950bf602007-01-26 08:19:09 +00001831 | EUINT64VAL {
1832 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
1833 error("Value too large for type");
1834 $$ = (int64_t)$1;
1835 };
Reid Spencere7c3c602006-11-30 06:36:44 +00001836
1837// Operations that are notably excluded from this list include:
1838// RET, BR, & SWITCH because they end basic blocks and are treated specially.
Reid Spencer950bf602007-01-26 08:19:09 +00001839//
1840ArithmeticOps
1841 : ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV | REM | UREM | SREM | FREM
1842 ;
1843
1844LogicalOps
1845 : AND | OR | XOR
1846 ;
1847
1848SetCondOps
1849 : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
1850 ;
1851
1852IPredicates
1853 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
1854 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1855 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1856 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1857 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1858 ;
1859
1860FPredicates
1861 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1862 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1863 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1864 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1865 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1866 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1867 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1868 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1869 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1870 ;
1871ShiftOps
1872 : SHL | SHR | ASHR | LSHR
1873 ;
1874
1875CastOps
1876 : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI
1877 | UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
1878 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001879
1880// These are some types that allow classification if we only want a particular
1881// thing... for example, only a signed, unsigned, or integral type.
Reid Spencer950bf602007-01-26 08:19:09 +00001882SIntType
1883 : LONG | INT | SHORT | SBYTE
1884 ;
1885
1886UIntType
1887 : ULONG | UINT | USHORT | UBYTE
1888 ;
1889
1890IntType
1891 : SIntType | UIntType
1892 ;
1893
1894FPType
1895 : FLOAT | DOUBLE
1896 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001897
1898// OptAssign - Value producing statements have an optional assignment component
Reid Spencer950bf602007-01-26 08:19:09 +00001899OptAssign
1900 : Name '=' {
Reid Spencere7c3c602006-11-30 06:36:44 +00001901 $$ = $1;
1902 }
1903 | /*empty*/ {
Reid Spencer950bf602007-01-26 08:19:09 +00001904 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001905 };
1906
1907OptLinkage
Reid Spencer785a5ae2007-02-08 00:21:40 +00001908 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencer950bf602007-01-26 08:19:09 +00001909 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1910 | WEAK { $$ = GlobalValue::WeakLinkage; }
1911 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1912 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1913 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencer785a5ae2007-02-08 00:21:40 +00001914 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencer950bf602007-01-26 08:19:09 +00001915 | /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1916 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001917
1918OptCallingConv
Reid Spencer7eea8ff2007-05-18 05:48:07 +00001919 : /*empty*/ { $$ = lastCallingConv = OldCallingConv::C; }
1920 | CCC_TOK { $$ = lastCallingConv = OldCallingConv::C; }
1921 | CSRETCC_TOK { $$ = lastCallingConv = OldCallingConv::CSRet; }
1922 | FASTCC_TOK { $$ = lastCallingConv = OldCallingConv::Fast; }
1923 | COLDCC_TOK { $$ = lastCallingConv = OldCallingConv::Cold; }
1924 | X86_STDCALLCC_TOK { $$ = lastCallingConv = OldCallingConv::X86_StdCall; }
1925 | X86_FASTCALLCC_TOK { $$ = lastCallingConv = OldCallingConv::X86_FastCall; }
Reid Spencer950bf602007-01-26 08:19:09 +00001926 | CC_TOK EUINT64VAL {
1927 if ((unsigned)$2 != $2)
1928 error("Calling conv too large");
Reid Spencer7eea8ff2007-05-18 05:48:07 +00001929 $$ = lastCallingConv = $2;
Reid Spencer950bf602007-01-26 08:19:09 +00001930 }
1931 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001932
1933// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1934// a comma before it.
1935OptAlign
Reid Spencer950bf602007-01-26 08:19:09 +00001936 : /*empty*/ { $$ = 0; }
1937 | ALIGN EUINT64VAL {
1938 $$ = $2;
1939 if ($$ != 0 && !isPowerOf2_32($$))
1940 error("Alignment must be a power of two");
1941 }
1942 ;
Reid Spencerf0cf1322006-12-07 04:23:03 +00001943
Reid Spencere7c3c602006-11-30 06:36:44 +00001944OptCAlign
Reid Spencer950bf602007-01-26 08:19:09 +00001945 : /*empty*/ { $$ = 0; }
1946 | ',' ALIGN EUINT64VAL {
1947 $$ = $3;
1948 if ($$ != 0 && !isPowerOf2_32($$))
1949 error("Alignment must be a power of two");
1950 }
1951 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001952
1953SectionString
Reid Spencer950bf602007-01-26 08:19:09 +00001954 : SECTION STRINGCONSTANT {
1955 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1956 if ($2[i] == '"' || $2[i] == '\\')
1957 error("Invalid character in section name");
1958 $$ = $2;
1959 }
1960 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001961
Reid Spencer950bf602007-01-26 08:19:09 +00001962OptSection
1963 : /*empty*/ { $$ = 0; }
1964 | SectionString { $$ = $1; }
1965 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001966
Reid Spencer950bf602007-01-26 08:19:09 +00001967// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1968// is set to be the global we are processing.
1969//
Reid Spencere7c3c602006-11-30 06:36:44 +00001970GlobalVarAttributes
Reid Spencer950bf602007-01-26 08:19:09 +00001971 : /* empty */ {}
1972 | ',' GlobalVarAttribute GlobalVarAttributes {}
1973 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001974
Reid Spencer950bf602007-01-26 08:19:09 +00001975GlobalVarAttribute
1976 : SectionString {
1977 CurGV->setSection($1);
1978 free($1);
1979 }
1980 | ALIGN EUINT64VAL {
1981 if ($2 != 0 && !isPowerOf2_32($2))
1982 error("Alignment must be a power of two");
1983 CurGV->setAlignment($2);
1984
1985 }
1986 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001987
1988//===----------------------------------------------------------------------===//
1989// Types includes all predefined types... except void, because it can only be
1990// used in specific contexts (function returning void for example). To have
1991// access to it, a user must explicitly use TypesV.
1992//
1993
1994// TypesV includes all of 'Types', but it also includes the void type.
Reid Spencer950bf602007-01-26 08:19:09 +00001995TypesV
1996 : Types
1997 | VOID {
Reid Spencered96d1e2007-02-08 09:08:52 +00001998 $$.PAT = new PATypeHolder($1.T);
Reid Spencerbb1fd572007-03-21 17:15:50 +00001999 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002000 }
2001 ;
2002
2003UpRTypesV
2004 : UpRTypes
2005 | VOID {
Reid Spencered96d1e2007-02-08 09:08:52 +00002006 $$.PAT = new PATypeHolder($1.T);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002007 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002008 }
2009 ;
2010
2011Types
2012 : UpRTypes {
2013 if (!UpRefs.empty())
Reid Spencered96d1e2007-02-08 09:08:52 +00002014 error("Invalid upreference in type: " + (*$1.PAT)->getDescription());
Reid Spencer950bf602007-01-26 08:19:09 +00002015 $$ = $1;
2016 }
2017 ;
2018
2019PrimType
2020 : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
2021 | LONG | ULONG | FLOAT | DOUBLE | LABEL
2022 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002023
2024// Derived types are added later...
Reid Spencera50d5962006-12-02 04:11:07 +00002025UpRTypes
Reid Spencer950bf602007-01-26 08:19:09 +00002026 : PrimType {
Reid Spencered96d1e2007-02-08 09:08:52 +00002027 $$.PAT = new PATypeHolder($1.T);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002028 $$.S.copy($1.S);
Reid Spencera50d5962006-12-02 04:11:07 +00002029 }
Reid Spencer950bf602007-01-26 08:19:09 +00002030 | OPAQUE {
Reid Spencered96d1e2007-02-08 09:08:52 +00002031 $$.PAT = new PATypeHolder(OpaqueType::get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002032 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002033 }
2034 | SymbolicValueRef { // Named types are also simple types...
Reid Spencerbb1fd572007-03-21 17:15:50 +00002035 $$.S.copy(getTypeSign($1));
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00002036 const Type* tmp = getType($1);
Reid Spencered96d1e2007-02-08 09:08:52 +00002037 $$.PAT = new PATypeHolder(tmp);
Reid Spencer78720742006-12-02 20:21:22 +00002038 }
2039 | '\\' EUINT64VAL { // Type UpReference
Reid Spencer950bf602007-01-26 08:19:09 +00002040 if ($2 > (uint64_t)~0U)
2041 error("Value out of range");
2042 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
2043 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencered96d1e2007-02-08 09:08:52 +00002044 $$.PAT = new PATypeHolder(OT);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002045 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002046 UR_OUT("New Upreference!\n");
Reid Spencere7c3c602006-11-30 06:36:44 +00002047 }
2048 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Reid Spencerbb1fd572007-03-21 17:15:50 +00002049 $$.S.makeComposite($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002050 std::vector<const Type*> Params;
2051 for (std::list<llvm::PATypeInfo>::iterator I = $3->begin(),
2052 E = $3->end(); I != E; ++I) {
Reid Spencered96d1e2007-02-08 09:08:52 +00002053 Params.push_back(I->PAT->get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002054 $$.S.add(I->S);
Reid Spencer52402b02007-01-02 05:45:11 +00002055 }
Reid Spencer950bf602007-01-26 08:19:09 +00002056 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
2057 if (isVarArg) Params.pop_back();
2058
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00002059 const ParamAttrsList *PAL = 0;
Reid Spencer7eea8ff2007-05-18 05:48:07 +00002060 if (lastCallingConv == OldCallingConv::CSRet) {
2061 ParamAttrsVector Attrs;
2062 ParamAttrsWithIndex PAWI;
2063 PAWI.index = 1; PAWI.attrs = ParamAttr::StructRet; // first arg
2064 Attrs.push_back(PAWI);
2065 PAL = ParamAttrsList::get(Attrs);
2066 }
2067
Reid Spencer7b5d4662007-04-09 06:16:21 +00002068 const FunctionType *FTy =
Duncan Sandsdc024672007-11-27 13:23:08 +00002069 FunctionType::get($1.PAT->get(), Params, isVarArg);
Reid Spencer7b5d4662007-04-09 06:16:21 +00002070
2071 $$.PAT = new PATypeHolder( HandleUpRefs(FTy, $$.S) );
Reid Spencerbb1fd572007-03-21 17:15:50 +00002072 delete $1.PAT; // Delete the return type handle
Reid Spencer950bf602007-01-26 08:19:09 +00002073 delete $3; // Delete the argument list
Reid Spencere7c3c602006-11-30 06:36:44 +00002074 }
2075 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencerbb1fd572007-03-21 17:15:50 +00002076 $$.S.makeComposite($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002077 $$.PAT = new PATypeHolder(HandleUpRefs(ArrayType::get($4.PAT->get(),
Reid Spencerbb1fd572007-03-21 17:15:50 +00002078 (unsigned)$2), $$.S));
Reid Spencered96d1e2007-02-08 09:08:52 +00002079 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002080 }
Chris Lattner4227bdb2007-02-19 07:34:02 +00002081 | '<' EUINT64VAL 'x' UpRTypes '>' { // Vector type?
Reid Spencerbb1fd572007-03-21 17:15:50 +00002082 const llvm::Type* ElemTy = $4.PAT->get();
2083 if ((unsigned)$2 != $2)
2084 error("Unsigned result not equal to signed result");
2085 if (!(ElemTy->isInteger() || ElemTy->isFloatingPoint()))
2086 error("Elements of a VectorType must be integer or floating point");
2087 if (!isPowerOf2_32($2))
2088 error("VectorType length should be a power of 2");
2089 $$.S.makeComposite($4.S);
2090 $$.PAT = new PATypeHolder(HandleUpRefs(VectorType::get(ElemTy,
2091 (unsigned)$2), $$.S));
2092 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002093 }
2094 | '{' TypeListI '}' { // Structure type?
Reid Spencer950bf602007-01-26 08:19:09 +00002095 std::vector<const Type*> Elements;
Reid Spencerbb1fd572007-03-21 17:15:50 +00002096 $$.S.makeComposite();
Reid Spencer950bf602007-01-26 08:19:09 +00002097 for (std::list<llvm::PATypeInfo>::iterator I = $2->begin(),
Reid Spencerbb1fd572007-03-21 17:15:50 +00002098 E = $2->end(); I != E; ++I) {
Reid Spencered96d1e2007-02-08 09:08:52 +00002099 Elements.push_back(I->PAT->get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002100 $$.S.add(I->S);
2101 }
2102 $$.PAT = new PATypeHolder(HandleUpRefs(StructType::get(Elements), $$.S));
Reid Spencer950bf602007-01-26 08:19:09 +00002103 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00002104 }
2105 | '{' '}' { // Empty structure type?
Reid Spencered96d1e2007-02-08 09:08:52 +00002106 $$.PAT = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencerbb1fd572007-03-21 17:15:50 +00002107 $$.S.makeComposite();
Reid Spencere7c3c602006-11-30 06:36:44 +00002108 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002109 | '<' '{' TypeListI '}' '>' { // Packed Structure type?
Reid Spencerbb1fd572007-03-21 17:15:50 +00002110 $$.S.makeComposite();
Reid Spencer950bf602007-01-26 08:19:09 +00002111 std::vector<const Type*> Elements;
2112 for (std::list<llvm::PATypeInfo>::iterator I = $3->begin(),
2113 E = $3->end(); I != E; ++I) {
Reid Spencered96d1e2007-02-08 09:08:52 +00002114 Elements.push_back(I->PAT->get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002115 $$.S.add(I->S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002116 delete I->PAT;
Reid Spencer52402b02007-01-02 05:45:11 +00002117 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00002118 $$.PAT = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true),
2119 $$.S));
Reid Spencer950bf602007-01-26 08:19:09 +00002120 delete $3;
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002121 }
2122 | '<' '{' '}' '>' { // Empty packed structure type?
Reid Spencered96d1e2007-02-08 09:08:52 +00002123 $$.PAT = new PATypeHolder(StructType::get(std::vector<const Type*>(),true));
Reid Spencerbb1fd572007-03-21 17:15:50 +00002124 $$.S.makeComposite();
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002125 }
Reid Spencere7c3c602006-11-30 06:36:44 +00002126 | UpRTypes '*' { // Pointer type?
Reid Spencered96d1e2007-02-08 09:08:52 +00002127 if ($1.PAT->get() == Type::LabelTy)
Reid Spencer950bf602007-01-26 08:19:09 +00002128 error("Cannot form a pointer to a basic block");
Reid Spencerbb1fd572007-03-21 17:15:50 +00002129 $$.S.makeComposite($1.S);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002130 $$.PAT = new
2131 PATypeHolder(HandleUpRefs(PointerType::getUnqual($1.PAT->get()),
2132 $$.S));
Reid Spencered96d1e2007-02-08 09:08:52 +00002133 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002134 }
2135 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002136
2137// TypeList - Used for struct declarations and as a basis for function type
2138// declaration type lists
2139//
Reid Spencere77e35e2006-12-01 20:26:20 +00002140TypeListI
2141 : UpRTypes {
Reid Spencer950bf602007-01-26 08:19:09 +00002142 $$ = new std::list<PATypeInfo>();
2143 $$->push_back($1);
Reid Spencere77e35e2006-12-01 20:26:20 +00002144 }
2145 | TypeListI ',' UpRTypes {
Reid Spencer950bf602007-01-26 08:19:09 +00002146 ($$=$1)->push_back($3);
2147 }
2148 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002149
2150// ArgTypeList - List of types for a function type declaration...
Reid Spencere77e35e2006-12-01 20:26:20 +00002151ArgTypeListI
Reid Spencer950bf602007-01-26 08:19:09 +00002152 : TypeListI
Reid Spencere7c3c602006-11-30 06:36:44 +00002153 | TypeListI ',' DOTDOTDOT {
Reid Spencer950bf602007-01-26 08:19:09 +00002154 PATypeInfo VoidTI;
Reid Spencered96d1e2007-02-08 09:08:52 +00002155 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002156 VoidTI.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002157 ($$=$1)->push_back(VoidTI);
Reid Spencere7c3c602006-11-30 06:36:44 +00002158 }
2159 | DOTDOTDOT {
Reid Spencer950bf602007-01-26 08:19:09 +00002160 $$ = new std::list<PATypeInfo>();
2161 PATypeInfo VoidTI;
Reid Spencered96d1e2007-02-08 09:08:52 +00002162 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002163 VoidTI.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002164 $$->push_back(VoidTI);
Reid Spencere7c3c602006-11-30 06:36:44 +00002165 }
2166 | /*empty*/ {
Reid Spencer950bf602007-01-26 08:19:09 +00002167 $$ = new std::list<PATypeInfo>();
2168 }
2169 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002170
2171// ConstVal - The various declarations that go into the constant pool. This
2172// production is used ONLY to represent constants that show up AFTER a 'const',
2173// 'constant' or 'global' token at global scope. Constants that can be inlined
2174// into other expressions (such as integers and constexprs) are handled by the
2175// ResolvedVal, ValueRef and ConstValueRef productions.
2176//
Reid Spencer950bf602007-01-26 08:19:09 +00002177ConstVal
2178 : Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencered96d1e2007-02-08 09:08:52 +00002179 const ArrayType *ATy = dyn_cast<ArrayType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002180 if (ATy == 0)
2181 error("Cannot make array constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002182 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002183 const Type *ETy = ATy->getElementType();
2184 int NumElements = ATy->getNumElements();
2185
2186 // Verify that we have the correct size...
2187 if (NumElements != -1 && NumElements != (int)$3->size())
2188 error("Type mismatch: constant sized array initialized with " +
2189 utostr($3->size()) + " arguments, but has size of " +
2190 itostr(NumElements) + "");
2191
2192 // Verify all elements are correct type!
2193 std::vector<Constant*> Elems;
2194 for (unsigned i = 0; i < $3->size(); i++) {
2195 Constant *C = (*$3)[i].C;
2196 const Type* ValTy = C->getType();
2197 if (ETy != ValTy)
2198 error("Element #" + utostr(i) + " is not of type '" +
2199 ETy->getDescription() +"' as required!\nIt is of type '"+
2200 ValTy->getDescription() + "'");
2201 Elems.push_back(C);
2202 }
2203 $$.C = ConstantArray::get(ATy, Elems);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002204 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002205 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002206 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002207 }
2208 | Types '[' ']' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002209 const ArrayType *ATy = dyn_cast<ArrayType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002210 if (ATy == 0)
2211 error("Cannot make array constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002212 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002213 int NumElements = ATy->getNumElements();
2214 if (NumElements != -1 && NumElements != 0)
2215 error("Type mismatch: constant sized array initialized with 0"
2216 " arguments, but has size of " + itostr(NumElements) +"");
2217 $$.C = ConstantArray::get(ATy, std::vector<Constant*>());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002218 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002219 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002220 }
2221 | Types 'c' STRINGCONSTANT {
Reid Spencered96d1e2007-02-08 09:08:52 +00002222 const ArrayType *ATy = dyn_cast<ArrayType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002223 if (ATy == 0)
2224 error("Cannot make array constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002225 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002226 int NumElements = ATy->getNumElements();
2227 const Type *ETy = dyn_cast<IntegerType>(ATy->getElementType());
2228 if (!ETy || cast<IntegerType>(ETy)->getBitWidth() != 8)
2229 error("String arrays require type i8, not '" + ETy->getDescription() +
2230 "'");
2231 char *EndStr = UnEscapeLexed($3, true);
2232 if (NumElements != -1 && NumElements != (EndStr-$3))
2233 error("Can't build string constant of size " +
2234 itostr((int)(EndStr-$3)) + " when array has size " +
2235 itostr(NumElements) + "");
2236 std::vector<Constant*> Vals;
2237 for (char *C = (char *)$3; C != (char *)EndStr; ++C)
2238 Vals.push_back(ConstantInt::get(ETy, *C));
2239 free($3);
2240 $$.C = ConstantArray::get(ATy, Vals);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002241 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002242 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002243 }
2244 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer9d6565a2007-02-15 02:26:10 +00002245 const VectorType *PTy = dyn_cast<VectorType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002246 if (PTy == 0)
2247 error("Cannot make packed constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002248 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002249 const Type *ETy = PTy->getElementType();
2250 int NumElements = PTy->getNumElements();
2251 // Verify that we have the correct size...
2252 if (NumElements != -1 && NumElements != (int)$3->size())
2253 error("Type mismatch: constant sized packed initialized with " +
2254 utostr($3->size()) + " arguments, but has size of " +
2255 itostr(NumElements) + "");
2256 // Verify all elements are correct type!
2257 std::vector<Constant*> Elems;
2258 for (unsigned i = 0; i < $3->size(); i++) {
2259 Constant *C = (*$3)[i].C;
2260 const Type* ValTy = C->getType();
2261 if (ETy != ValTy)
2262 error("Element #" + utostr(i) + " is not of type '" +
2263 ETy->getDescription() +"' as required!\nIt is of type '"+
2264 ValTy->getDescription() + "'");
2265 Elems.push_back(C);
2266 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00002267 $$.C = ConstantVector::get(PTy, Elems);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002268 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002269 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002270 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002271 }
2272 | Types '{' ConstVector '}' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002273 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002274 if (STy == 0)
2275 error("Cannot make struct constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002276 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002277 if ($3->size() != STy->getNumContainedTypes())
2278 error("Illegal number of initializers for structure type");
2279
2280 // Check to ensure that constants are compatible with the type initializer!
2281 std::vector<Constant*> Fields;
2282 for (unsigned i = 0, e = $3->size(); i != e; ++i) {
2283 Constant *C = (*$3)[i].C;
2284 if (C->getType() != STy->getElementType(i))
2285 error("Expected type '" + STy->getElementType(i)->getDescription() +
2286 "' for element #" + utostr(i) + " of structure initializer");
2287 Fields.push_back(C);
2288 }
2289 $$.C = ConstantStruct::get(STy, Fields);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002290 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002291 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002292 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002293 }
2294 | Types '{' '}' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002295 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002296 if (STy == 0)
2297 error("Cannot make struct constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002298 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002299 if (STy->getNumContainedTypes() != 0)
2300 error("Illegal number of initializers for structure type");
2301 $$.C = ConstantStruct::get(STy, std::vector<Constant*>());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002302 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002303 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002304 }
Reid Spencer950bf602007-01-26 08:19:09 +00002305 | Types '<' '{' ConstVector '}' '>' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002306 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002307 if (STy == 0)
2308 error("Cannot make packed struct constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002309 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002310 if ($4->size() != STy->getNumContainedTypes())
2311 error("Illegal number of initializers for packed structure type");
Reid Spencere7c3c602006-11-30 06:36:44 +00002312
Reid Spencer950bf602007-01-26 08:19:09 +00002313 // Check to ensure that constants are compatible with the type initializer!
2314 std::vector<Constant*> Fields;
2315 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
2316 Constant *C = (*$4)[i].C;
2317 if (C->getType() != STy->getElementType(i))
2318 error("Expected type '" + STy->getElementType(i)->getDescription() +
2319 "' for element #" + utostr(i) + " of packed struct initializer");
2320 Fields.push_back(C);
Reid Spencer280d8012006-12-01 23:40:53 +00002321 }
Reid Spencer950bf602007-01-26 08:19:09 +00002322 $$.C = ConstantStruct::get(STy, Fields);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002323 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002324 delete $1.PAT;
Reid Spencere77e35e2006-12-01 20:26:20 +00002325 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00002326 }
Reid Spencer950bf602007-01-26 08:19:09 +00002327 | Types '<' '{' '}' '>' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002328 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002329 if (STy == 0)
2330 error("Cannot make packed struct constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002331 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002332 if (STy->getNumContainedTypes() != 0)
2333 error("Illegal number of initializers for packed structure type");
2334 $$.C = ConstantStruct::get(STy, std::vector<Constant*>());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002335 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002336 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002337 }
2338 | Types NULL_TOK {
Reid Spencered96d1e2007-02-08 09:08:52 +00002339 const PointerType *PTy = dyn_cast<PointerType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002340 if (PTy == 0)
2341 error("Cannot make null pointer constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002342 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002343 $$.C = ConstantPointerNull::get(PTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002344 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002345 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002346 }
2347 | Types UNDEF {
Reid Spencered96d1e2007-02-08 09:08:52 +00002348 $$.C = UndefValue::get($1.PAT->get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002349 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002350 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002351 }
2352 | Types SymbolicValueRef {
Reid Spencered96d1e2007-02-08 09:08:52 +00002353 const PointerType *Ty = dyn_cast<PointerType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002354 if (Ty == 0)
2355 error("Global const reference must be a pointer type, not" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002356 $1.PAT->get()->getDescription());
Reid Spencer950bf602007-01-26 08:19:09 +00002357
2358 // ConstExprs can exist in the body of a function, thus creating
2359 // GlobalValues whenever they refer to a variable. Because we are in
2360 // the context of a function, getExistingValue will search the functions
2361 // symbol table instead of the module symbol table for the global symbol,
2362 // which throws things all off. To get around this, we just tell
2363 // getExistingValue that we are at global scope here.
2364 //
2365 Function *SavedCurFn = CurFun.CurrentFunction;
2366 CurFun.CurrentFunction = 0;
Reid Spencerbb1fd572007-03-21 17:15:50 +00002367 $2.S.copy($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002368 Value *V = getExistingValue(Ty, $2);
2369 CurFun.CurrentFunction = SavedCurFn;
2370
2371 // If this is an initializer for a constant pointer, which is referencing a
2372 // (currently) undefined variable, create a stub now that shall be replaced
2373 // in the future with the right type of variable.
2374 //
2375 if (V == 0) {
2376 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers");
2377 const PointerType *PT = cast<PointerType>(Ty);
2378
2379 // First check to see if the forward references value is already created!
2380 PerModuleInfo::GlobalRefsType::iterator I =
2381 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
2382
2383 if (I != CurModule.GlobalRefs.end()) {
2384 V = I->second; // Placeholder already exists, use it...
2385 $2.destroy();
2386 } else {
2387 std::string Name;
2388 if ($2.Type == ValID::NameVal) Name = $2.Name;
2389
2390 // Create the forward referenced global.
2391 GlobalValue *GV;
2392 if (const FunctionType *FTy =
2393 dyn_cast<FunctionType>(PT->getElementType())) {
2394 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
2395 CurModule.CurrentModule);
2396 } else {
2397 GV = new GlobalVariable(PT->getElementType(), false,
2398 GlobalValue::ExternalLinkage, 0,
2399 Name, CurModule.CurrentModule);
2400 }
2401
2402 // Keep track of the fact that we have a forward ref to recycle it
2403 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
2404 V = GV;
2405 }
2406 }
2407 $$.C = cast<GlobalValue>(V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002408 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002409 delete $1.PAT; // Free the type handle
Reid Spencer950bf602007-01-26 08:19:09 +00002410 }
2411 | Types ConstExpr {
Reid Spencered96d1e2007-02-08 09:08:52 +00002412 if ($1.PAT->get() != $2.C->getType())
Reid Spencer950bf602007-01-26 08:19:09 +00002413 error("Mismatched types for constant expression");
2414 $$ = $2;
Reid Spencerbb1fd572007-03-21 17:15:50 +00002415 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002416 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002417 }
2418 | Types ZEROINITIALIZER {
Reid Spencered96d1e2007-02-08 09:08:52 +00002419 const Type *Ty = $1.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00002420 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
2421 error("Cannot create a null initialized value of this type");
2422 $$.C = Constant::getNullValue(Ty);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002423 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002424 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002425 }
2426 | SIntType EINT64VAL { // integral constants
2427 const Type *Ty = $1.T;
2428 if (!ConstantInt::isValueValidForType(Ty, $2))
2429 error("Constant value doesn't fit in type");
2430 $$.C = ConstantInt::get(Ty, $2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002431 $$.S.makeSigned();
Reid Spencer950bf602007-01-26 08:19:09 +00002432 }
2433 | UIntType EUINT64VAL { // integral constants
2434 const Type *Ty = $1.T;
2435 if (!ConstantInt::isValueValidForType(Ty, $2))
2436 error("Constant value doesn't fit in type");
2437 $$.C = ConstantInt::get(Ty, $2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002438 $$.S.makeUnsigned();
Reid Spencer950bf602007-01-26 08:19:09 +00002439 }
2440 | BOOL TRUETOK { // Boolean constants
2441 $$.C = ConstantInt::get(Type::Int1Ty, true);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002442 $$.S.makeUnsigned();
Reid Spencer950bf602007-01-26 08:19:09 +00002443 }
2444 | BOOL FALSETOK { // Boolean constants
2445 $$.C = ConstantInt::get(Type::Int1Ty, false);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002446 $$.S.makeUnsigned();
Reid Spencer950bf602007-01-26 08:19:09 +00002447 }
2448 | FPType FPVAL { // Float & Double constants
Dale Johannesen43421b32007-09-06 18:13:44 +00002449 if (!ConstantFP::isValueValidForType($1.T, *$2))
Reid Spencer950bf602007-01-26 08:19:09 +00002450 error("Floating point constant invalid for type");
Dale Johannesen43421b32007-09-06 18:13:44 +00002451 // Lexer has no type info, so builds all FP constants as double.
2452 // Fix this here.
2453 if ($1.T==Type::FloatTy)
2454 $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
2455 $$.C = ConstantFP::get($1.T, *$2);
Dale Johannesencdd509a2007-09-07 21:07:57 +00002456 delete $2;
Reid Spencerbb1fd572007-03-21 17:15:50 +00002457 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002458 }
2459 ;
2460
2461ConstExpr
2462 : CastOps '(' ConstVal TO Types ')' {
2463 const Type* SrcTy = $3.C->getType();
Reid Spencered96d1e2007-02-08 09:08:52 +00002464 const Type* DstTy = $5.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00002465 Signedness SrcSign($3.S);
2466 Signedness DstSign($5.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002467 if (!SrcTy->isFirstClassType())
2468 error("cast constant expression from a non-primitive type: '" +
2469 SrcTy->getDescription() + "'");
2470 if (!DstTy->isFirstClassType())
2471 error("cast constant expression to a non-primitive type: '" +
2472 DstTy->getDescription() + "'");
2473 $$.C = cast<Constant>(getCast($1, $3.C, SrcSign, DstTy, DstSign));
Reid Spencerbb1fd572007-03-21 17:15:50 +00002474 $$.S.copy(DstSign);
Reid Spencered96d1e2007-02-08 09:08:52 +00002475 delete $5.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002476 }
2477 | GETELEMENTPTR '(' ConstVal IndexList ')' {
2478 const Type *Ty = $3.C->getType();
2479 if (!isa<PointerType>(Ty))
2480 error("GetElementPtr requires a pointer operand");
2481
Reid Spencer950bf602007-01-26 08:19:09 +00002482 std::vector<Constant*> CIndices;
Reid Spencerff0e4482007-04-16 00:40:57 +00002483 upgradeGEPCEIndices($3.C->getType(), $4, CIndices);
Reid Spencer950bf602007-01-26 08:19:09 +00002484
2485 delete $4;
Chris Lattner4227bdb2007-02-19 07:34:02 +00002486 $$.C = ConstantExpr::getGetElementPtr($3.C, &CIndices[0], CIndices.size());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002487 $$.S.copy(getElementSign($3, CIndices));
Reid Spencer950bf602007-01-26 08:19:09 +00002488 }
Reid Spencere7c3c602006-11-30 06:36:44 +00002489 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002490 if (!$3.C->getType()->isInteger() ||
2491 cast<IntegerType>($3.C->getType())->getBitWidth() != 1)
2492 error("Select condition must be bool type");
2493 if ($5.C->getType() != $7.C->getType())
2494 error("Select operand types must match");
2495 $$.C = ConstantExpr::getSelect($3.C, $5.C, $7.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002496 $$.S.copy($5.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002497 }
2498 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002499 const Type *Ty = $3.C->getType();
2500 if (Ty != $5.C->getType())
2501 error("Binary operator types must match");
2502 // First, make sure we're dealing with the right opcode by upgrading from
2503 // obsolete versions.
2504 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $3.S);
2505
2506 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
2507 // To retain backward compatibility with these early compilers, we emit a
2508 // cast to the appropriate integer type automatically if we are in the
2509 // broken case. See PR424 for more information.
2510 if (!isa<PointerType>(Ty)) {
2511 $$.C = ConstantExpr::get(Opcode, $3.C, $5.C);
2512 } else {
2513 const Type *IntPtrTy = 0;
2514 switch (CurModule.CurrentModule->getPointerSize()) {
2515 case Module::Pointer32: IntPtrTy = Type::Int32Ty; break;
2516 case Module::Pointer64: IntPtrTy = Type::Int64Ty; break;
2517 default: error("invalid pointer binary constant expr");
2518 }
2519 $$.C = ConstantExpr::get(Opcode,
2520 ConstantExpr::getCast(Instruction::PtrToInt, $3.C, IntPtrTy),
2521 ConstantExpr::getCast(Instruction::PtrToInt, $5.C, IntPtrTy));
2522 $$.C = ConstantExpr::getCast(Instruction::IntToPtr, $$.C, Ty);
2523 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00002524 $$.S.copy($3.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002525 }
2526 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002527 const Type* Ty = $3.C->getType();
2528 if (Ty != $5.C->getType())
2529 error("Logical operator types must match");
2530 if (!Ty->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00002531 if (!isa<VectorType>(Ty) ||
2532 !cast<VectorType>(Ty)->getElementType()->isInteger())
Reid Spencer950bf602007-01-26 08:19:09 +00002533 error("Logical operator requires integer operands");
2534 }
2535 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $3.S);
2536 $$.C = ConstantExpr::get(Opcode, $3.C, $5.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002537 $$.S.copy($3.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002538 }
2539 | SetCondOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002540 const Type* Ty = $3.C->getType();
2541 if (Ty != $5.C->getType())
2542 error("setcc operand types must match");
2543 unsigned short pred;
2544 Instruction::OtherOps Opcode = getCompareOp($1, pred, Ty, $3.S);
2545 $$.C = ConstantExpr::getCompare(Opcode, $3.C, $5.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002546 $$.S.makeUnsigned();
Reid Spencere7c3c602006-11-30 06:36:44 +00002547 }
Reid Spencer57f28f92006-12-03 07:10:26 +00002548 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002549 if ($4.C->getType() != $6.C->getType())
2550 error("icmp operand types must match");
2551 $$.C = ConstantExpr::getCompare($2, $4.C, $6.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002552 $$.S.makeUnsigned();
Reid Spencer57f28f92006-12-03 07:10:26 +00002553 }
2554 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002555 if ($4.C->getType() != $6.C->getType())
2556 error("fcmp operand types must match");
2557 $$.C = ConstantExpr::getCompare($2, $4.C, $6.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002558 $$.S.makeUnsigned();
Reid Spencer229e9362006-12-02 22:14:11 +00002559 }
Reid Spencere7c3c602006-11-30 06:36:44 +00002560 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002561 if (!$5.C->getType()->isInteger() ||
2562 cast<IntegerType>($5.C->getType())->getBitWidth() != 8)
2563 error("Shift count for shift constant must be unsigned byte");
Reid Spencer832254e2007-02-02 02:16:23 +00002564 const Type* Ty = $3.C->getType();
Reid Spencer950bf602007-01-26 08:19:09 +00002565 if (!$3.C->getType()->isInteger())
2566 error("Shift constant expression requires integer operand");
Reid Spencer832254e2007-02-02 02:16:23 +00002567 Constant *ShiftAmt = ConstantExpr::getZExt($5.C, Ty);
2568 $$.C = ConstantExpr::get(getBinaryOp($1, Ty, $3.S), $3.C, ShiftAmt);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002569 $$.S.copy($3.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002570 }
2571 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002572 if (!ExtractElementInst::isValidOperands($3.C, $5.C))
2573 error("Invalid extractelement operands");
2574 $$.C = ConstantExpr::getExtractElement($3.C, $5.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002575 $$.S.copy($3.S.get(0));
Reid Spencere7c3c602006-11-30 06:36:44 +00002576 }
2577 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002578 if (!InsertElementInst::isValidOperands($3.C, $5.C, $7.C))
2579 error("Invalid insertelement operands");
2580 $$.C = ConstantExpr::getInsertElement($3.C, $5.C, $7.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002581 $$.S.copy($3.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002582 }
2583 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002584 if (!ShuffleVectorInst::isValidOperands($3.C, $5.C, $7.C))
2585 error("Invalid shufflevector operands");
2586 $$.C = ConstantExpr::getShuffleVector($3.C, $5.C, $7.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002587 $$.S.copy($3.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002588 }
2589 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002590
2591
2592// ConstVector - A list of comma separated constants.
Reid Spencere77e35e2006-12-01 20:26:20 +00002593ConstVector
Reid Spencer950bf602007-01-26 08:19:09 +00002594 : ConstVector ',' ConstVal { ($$ = $1)->push_back($3); }
2595 | ConstVal {
2596 $$ = new std::vector<ConstInfo>();
2597 $$->push_back($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00002598 }
Reid Spencere77e35e2006-12-01 20:26:20 +00002599 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002600
2601
2602// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Reid Spencer950bf602007-01-26 08:19:09 +00002603GlobalType
2604 : GLOBAL { $$ = false; }
2605 | CONSTANT { $$ = true; }
2606 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002607
2608
2609//===----------------------------------------------------------------------===//
2610// Rules to match Modules
2611//===----------------------------------------------------------------------===//
2612
2613// Module rule: Capture the result of parsing the whole file into a result
2614// variable...
2615//
Reid Spencer950bf602007-01-26 08:19:09 +00002616Module
2617 : FunctionList {
2618 $$ = ParserResult = $1;
2619 CurModule.ModuleDone();
Reid Spencere7c3c602006-11-30 06:36:44 +00002620 }
Jeff Cohenac2dca92007-01-21 19:30:52 +00002621 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002622
Reid Spencer950bf602007-01-26 08:19:09 +00002623// FunctionList - A list of functions, preceeded by a constant pool.
2624//
2625FunctionList
2626 : FunctionList Function { $$ = $1; CurFun.FunctionDone(); }
2627 | FunctionList FunctionProto { $$ = $1; }
2628 | FunctionList MODULE ASM_TOK AsmBlock { $$ = $1; }
2629 | FunctionList IMPLEMENTATION { $$ = $1; }
2630 | ConstPool {
2631 $$ = CurModule.CurrentModule;
2632 // Emit an error if there are any unresolved types left.
2633 if (!CurModule.LateResolveTypes.empty()) {
2634 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
2635 if (DID.Type == ValID::NameVal) {
2636 error("Reference to an undefined type: '"+DID.getName() + "'");
2637 } else {
2638 error("Reference to an undefined type: #" + itostr(DID.Num));
2639 }
2640 }
2641 }
2642 ;
Reid Spencer78720742006-12-02 20:21:22 +00002643
Reid Spencere7c3c602006-11-30 06:36:44 +00002644// ConstPool - Constants with optional names assigned to them.
Reid Spencer950bf602007-01-26 08:19:09 +00002645ConstPool
2646 : ConstPool OptAssign TYPE TypesV {
2647 // Eagerly resolve types. This is not an optimization, this is a
2648 // requirement that is due to the fact that we could have this:
2649 //
2650 // %list = type { %list * }
2651 // %list = type { %list * } ; repeated type decl
2652 //
2653 // If types are not resolved eagerly, then the two types will not be
2654 // determined to be the same type!
2655 //
Reid Spencerbb1fd572007-03-21 17:15:50 +00002656 ResolveTypeTo($2, $4.PAT->get(), $4.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002657
Reid Spencerbb1fd572007-03-21 17:15:50 +00002658 if (!setTypeName($4, $2) && !$2) {
2659 // If this is a numbered type that is not a redefinition, add it to the
2660 // slot table.
2661 CurModule.Types.push_back($4.PAT->get());
2662 CurModule.TypeSigns.push_back($4.S);
Reid Spencera50d5962006-12-02 04:11:07 +00002663 }
Reid Spencered96d1e2007-02-08 09:08:52 +00002664 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002665 }
2666 | ConstPool FunctionProto { // Function prototypes can be in const pool
Reid Spencere7c3c602006-11-30 06:36:44 +00002667 }
2668 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
Reid Spencere7c3c602006-11-30 06:36:44 +00002669 }
Reid Spencer950bf602007-01-26 08:19:09 +00002670 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
2671 if ($5.C == 0)
2672 error("Global value initializer is not a constant");
Reid Spencerbb1fd572007-03-21 17:15:50 +00002673 CurGV = ParseGlobalVariable($2, $3, $4, $5.C->getType(), $5.C, $5.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002674 } GlobalVarAttributes {
2675 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002676 }
Reid Spencer950bf602007-01-26 08:19:09 +00002677 | ConstPool OptAssign EXTERNAL GlobalType Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00002678 const Type *Ty = $5.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00002679 CurGV = ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, Ty, 0,
2680 $5.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002681 delete $5.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002682 } GlobalVarAttributes {
2683 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002684 }
Reid Spencer950bf602007-01-26 08:19:09 +00002685 | ConstPool OptAssign DLLIMPORT GlobalType Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00002686 const Type *Ty = $5.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00002687 CurGV = ParseGlobalVariable($2, GlobalValue::DLLImportLinkage, $4, Ty, 0,
2688 $5.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002689 delete $5.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002690 } GlobalVarAttributes {
2691 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002692 }
Reid Spencer950bf602007-01-26 08:19:09 +00002693 | ConstPool OptAssign EXTERN_WEAK GlobalType Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00002694 const Type *Ty = $5.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00002695 CurGV =
Reid Spencerbb1fd572007-03-21 17:15:50 +00002696 ParseGlobalVariable($2, GlobalValue::ExternalWeakLinkage, $4, Ty, 0,
2697 $5.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002698 delete $5.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002699 } GlobalVarAttributes {
2700 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002701 }
2702 | ConstPool TARGET TargetDefinition {
Reid Spencere7c3c602006-11-30 06:36:44 +00002703 }
2704 | ConstPool DEPLIBS '=' LibrariesDefinition {
Reid Spencere7c3c602006-11-30 06:36:44 +00002705 }
2706 | /* empty: end of list */ {
Reid Spencer950bf602007-01-26 08:19:09 +00002707 }
2708 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002709
Reid Spencer950bf602007-01-26 08:19:09 +00002710AsmBlock
2711 : STRINGCONSTANT {
2712 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
2713 char *EndStr = UnEscapeLexed($1, true);
2714 std::string NewAsm($1, EndStr);
2715 free($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00002716
Reid Spencer950bf602007-01-26 08:19:09 +00002717 if (AsmSoFar.empty())
2718 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
2719 else
2720 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
2721 }
2722 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002723
Reid Spencer950bf602007-01-26 08:19:09 +00002724BigOrLittle
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00002725 : BIG { $$ = Module::BigEndian; }
Reid Spencer950bf602007-01-26 08:19:09 +00002726 | LITTLE { $$ = Module::LittleEndian; }
2727 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002728
2729TargetDefinition
2730 : ENDIAN '=' BigOrLittle {
Reid Spencer950bf602007-01-26 08:19:09 +00002731 CurModule.setEndianness($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00002732 }
2733 | POINTERSIZE '=' EUINT64VAL {
Reid Spencer950bf602007-01-26 08:19:09 +00002734 if ($3 == 32)
2735 CurModule.setPointerSize(Module::Pointer32);
2736 else if ($3 == 64)
2737 CurModule.setPointerSize(Module::Pointer64);
2738 else
2739 error("Invalid pointer size: '" + utostr($3) + "'");
Reid Spencere7c3c602006-11-30 06:36:44 +00002740 }
2741 | TRIPLE '=' STRINGCONSTANT {
Reid Spencer950bf602007-01-26 08:19:09 +00002742 CurModule.CurrentModule->setTargetTriple($3);
2743 free($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00002744 }
2745 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer950bf602007-01-26 08:19:09 +00002746 CurModule.CurrentModule->setDataLayout($3);
2747 free($3);
2748 }
2749 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002750
2751LibrariesDefinition
Reid Spencer950bf602007-01-26 08:19:09 +00002752 : '[' LibList ']'
2753 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002754
2755LibList
2756 : LibList ',' STRINGCONSTANT {
Reid Spencer950bf602007-01-26 08:19:09 +00002757 CurModule.CurrentModule->addLibrary($3);
2758 free($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00002759 }
Reid Spencer950bf602007-01-26 08:19:09 +00002760 | STRINGCONSTANT {
2761 CurModule.CurrentModule->addLibrary($1);
2762 free($1);
2763 }
2764 | /* empty: end of list */ { }
2765 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002766
2767//===----------------------------------------------------------------------===//
2768// Rules to match Function Headers
2769//===----------------------------------------------------------------------===//
2770
Reid Spencer950bf602007-01-26 08:19:09 +00002771Name
2772 : VAR_ID | STRINGCONSTANT
2773 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002774
Reid Spencer950bf602007-01-26 08:19:09 +00002775OptName
2776 : Name
2777 | /*empty*/ { $$ = 0; }
2778 ;
2779
2780ArgVal
2781 : Types OptName {
Reid Spencered96d1e2007-02-08 09:08:52 +00002782 if ($1.PAT->get() == Type::VoidTy)
Reid Spencer950bf602007-01-26 08:19:09 +00002783 error("void typed arguments are invalid");
2784 $$ = new std::pair<PATypeInfo, char*>($1, $2);
Reid Spencer52402b02007-01-02 05:45:11 +00002785 }
Reid Spencer950bf602007-01-26 08:19:09 +00002786 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002787
Reid Spencer950bf602007-01-26 08:19:09 +00002788ArgListH
2789 : ArgListH ',' ArgVal {
2790 $$ = $1;
2791 $$->push_back(*$3);
Reid Spencere77e35e2006-12-01 20:26:20 +00002792 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002793 }
2794 | ArgVal {
Reid Spencer950bf602007-01-26 08:19:09 +00002795 $$ = new std::vector<std::pair<PATypeInfo,char*> >();
2796 $$->push_back(*$1);
2797 delete $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00002798 }
Reid Spencer950bf602007-01-26 08:19:09 +00002799 ;
2800
2801ArgList
2802 : ArgListH { $$ = $1; }
Reid Spencere7c3c602006-11-30 06:36:44 +00002803 | ArgListH ',' DOTDOTDOT {
Reid Spencere7c3c602006-11-30 06:36:44 +00002804 $$ = $1;
Reid Spencer950bf602007-01-26 08:19:09 +00002805 PATypeInfo VoidTI;
Reid Spencered96d1e2007-02-08 09:08:52 +00002806 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002807 VoidTI.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002808 $$->push_back(std::pair<PATypeInfo, char*>(VoidTI, 0));
Reid Spencere7c3c602006-11-30 06:36:44 +00002809 }
2810 | DOTDOTDOT {
Reid Spencer950bf602007-01-26 08:19:09 +00002811 $$ = new std::vector<std::pair<PATypeInfo,char*> >();
2812 PATypeInfo VoidTI;
Reid Spencered96d1e2007-02-08 09:08:52 +00002813 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002814 VoidTI.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002815 $$->push_back(std::pair<PATypeInfo, char*>(VoidTI, 0));
Reid Spencere7c3c602006-11-30 06:36:44 +00002816 }
Reid Spencer950bf602007-01-26 08:19:09 +00002817 | /* empty */ { $$ = 0; }
2818 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002819
Reid Spencer71d2ec92006-12-31 06:02:26 +00002820FunctionHeaderH
2821 : OptCallingConv TypesV Name '(' ArgList ')' OptSection OptAlign {
Reid Spencer950bf602007-01-26 08:19:09 +00002822 UnEscapeLexed($3);
2823 std::string FunctionName($3);
2824 free($3); // Free strdup'd memory!
Reid Spencere7c3c602006-11-30 06:36:44 +00002825
Reid Spencered96d1e2007-02-08 09:08:52 +00002826 const Type* RetTy = $2.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00002827
2828 if (!RetTy->isFirstClassType() && RetTy != Type::VoidTy)
2829 error("LLVM functions cannot return aggregate types");
2830
Reid Spencerbb1fd572007-03-21 17:15:50 +00002831 Signedness FTySign;
2832 FTySign.makeComposite($2.S);
Reid Spenceref9b9a72007-02-05 20:47:22 +00002833 std::vector<const Type*> ParamTyList;
Reid Spencer950bf602007-01-26 08:19:09 +00002834
2835 // In LLVM 2.0 the signatures of three varargs intrinsics changed to take
2836 // i8*. We check here for those names and override the parameter list
2837 // types to ensure the prototype is correct.
2838 if (FunctionName == "llvm.va_start" || FunctionName == "llvm.va_end") {
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002839 ParamTyList.push_back(PointerType::getUnqual(Type::Int8Ty));
Reid Spencer950bf602007-01-26 08:19:09 +00002840 } else if (FunctionName == "llvm.va_copy") {
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002841 ParamTyList.push_back(PointerType::getUnqual(Type::Int8Ty));
2842 ParamTyList.push_back(PointerType::getUnqual(Type::Int8Ty));
Reid Spencer950bf602007-01-26 08:19:09 +00002843 } else if ($5) { // If there are arguments...
2844 for (std::vector<std::pair<PATypeInfo,char*> >::iterator
2845 I = $5->begin(), E = $5->end(); I != E; ++I) {
Reid Spencered96d1e2007-02-08 09:08:52 +00002846 const Type *Ty = I->first.PAT->get();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002847 ParamTyList.push_back(Ty);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002848 FTySign.add(I->first.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002849 }
2850 }
2851
Reid Spenceref9b9a72007-02-05 20:47:22 +00002852 bool isVarArg = ParamTyList.size() && ParamTyList.back() == Type::VoidTy;
2853 if (isVarArg)
2854 ParamTyList.pop_back();
Reid Spencer950bf602007-01-26 08:19:09 +00002855
Duncan Sandsdc024672007-11-27 13:23:08 +00002856 const FunctionType *FT = FunctionType::get(RetTy, ParamTyList, isVarArg);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002857 const PointerType *PFT = PointerType::getUnqual(FT);
Reid Spencered96d1e2007-02-08 09:08:52 +00002858 delete $2.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002859
2860 ValID ID;
2861 if (!FunctionName.empty()) {
Reid Spencer5eb77c72007-03-15 03:26:42 +00002862 ID = ValID::create((char*)FunctionName.c_str());
Reid Spencer950bf602007-01-26 08:19:09 +00002863 } else {
Reid Spencer5eb77c72007-03-15 03:26:42 +00002864 ID = ValID::create((int)CurModule.Values[PFT].size());
Reid Spencer950bf602007-01-26 08:19:09 +00002865 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00002866 ID.S.makeComposite(FTySign);
Reid Spencer950bf602007-01-26 08:19:09 +00002867
2868 Function *Fn = 0;
Reid Spencered96d1e2007-02-08 09:08:52 +00002869 Module* M = CurModule.CurrentModule;
2870
Reid Spencer950bf602007-01-26 08:19:09 +00002871 // See if this function was forward referenced. If so, recycle the object.
2872 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2873 // Move the function to the end of the list, from whereever it was
2874 // previously inserted.
2875 Fn = cast<Function>(FWRef);
Reid Spencered96d1e2007-02-08 09:08:52 +00002876 M->getFunctionList().remove(Fn);
2877 M->getFunctionList().push_back(Fn);
2878 } else if (!FunctionName.empty()) {
2879 GlobalValue *Conflict = M->getFunction(FunctionName);
2880 if (!Conflict)
2881 Conflict = M->getNamedGlobal(FunctionName);
2882 if (Conflict && PFT == Conflict->getType()) {
2883 if (!CurFun.isDeclare && !Conflict->isDeclaration()) {
2884 // We have two function definitions that conflict, same type, same
2885 // name. We should really check to make sure that this is the result
2886 // of integer type planes collapsing and generate an error if it is
2887 // not, but we'll just rename on the assumption that it is. However,
2888 // let's do it intelligently and rename the internal linkage one
2889 // if there is one.
2890 std::string NewName(makeNameUnique(FunctionName));
2891 if (Conflict->hasInternalLinkage()) {
2892 Conflict->setName(NewName);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002893 RenameMapKey Key =
2894 makeRenameMapKey(FunctionName, Conflict->getType(), ID.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002895 CurModule.RenameMap[Key] = NewName;
2896 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
2897 InsertValue(Fn, CurModule.Values);
2898 } else {
2899 Fn = new Function(FT, CurFun.Linkage, NewName, M);
2900 InsertValue(Fn, CurModule.Values);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002901 RenameMapKey Key =
2902 makeRenameMapKey(FunctionName, PFT, ID.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002903 CurModule.RenameMap[Key] = NewName;
2904 }
2905 } else {
2906 // If they are not both definitions, then just use the function we
2907 // found since the types are the same.
2908 Fn = cast<Function>(Conflict);
Reid Spenceref9b9a72007-02-05 20:47:22 +00002909
Reid Spencered96d1e2007-02-08 09:08:52 +00002910 // Make sure to strip off any argument names so we can't get
2911 // conflicts.
2912 if (Fn->isDeclaration())
2913 for (Function::arg_iterator AI = Fn->arg_begin(),
2914 AE = Fn->arg_end(); AI != AE; ++AI)
2915 AI->setName("");
2916 }
2917 } else if (Conflict) {
Reid Spencere59f4932007-04-16 03:05:01 +00002918 // We have two globals with the same name and different types.
Reid Spencered96d1e2007-02-08 09:08:52 +00002919 // Previously, this was permitted because the symbol table had
2920 // "type planes" and names only needed to be distinct within a
2921 // type plane. After PR411 was fixed, this is no loner the case.
2922 // To resolve this we must rename one of the two.
2923 if (Conflict->hasInternalLinkage()) {
Reid Spencerbb1fd572007-03-21 17:15:50 +00002924 // We can safely rename the Conflict.
2925 RenameMapKey Key =
2926 makeRenameMapKey(Conflict->getName(), Conflict->getType(),
2927 CurModule.NamedValueSigns[Conflict->getName()]);
Reid Spencered96d1e2007-02-08 09:08:52 +00002928 Conflict->setName(makeNameUnique(Conflict->getName()));
Reid Spencered96d1e2007-02-08 09:08:52 +00002929 CurModule.RenameMap[Key] = Conflict->getName();
2930 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
2931 InsertValue(Fn, CurModule.Values);
Reid Spencerd2920cd2007-03-21 17:27:53 +00002932 } else {
Reid Spencered96d1e2007-02-08 09:08:52 +00002933 // We can't quietly rename either of these things, but we must
Reid Spencerd2920cd2007-03-21 17:27:53 +00002934 // rename one of them. Only if the function's linkage is internal can
2935 // we forgo a warning message about the renamed function.
Reid Spencered96d1e2007-02-08 09:08:52 +00002936 std::string NewName = makeNameUnique(FunctionName);
Reid Spencerd2920cd2007-03-21 17:27:53 +00002937 if (CurFun.Linkage != GlobalValue::InternalLinkage) {
2938 warning("Renaming function '" + FunctionName + "' as '" + NewName +
2939 "' may cause linkage errors");
2940 }
2941 // Elect to rename the thing we're now defining.
Reid Spencered96d1e2007-02-08 09:08:52 +00002942 Fn = new Function(FT, CurFun.Linkage, NewName, M);
2943 InsertValue(Fn, CurModule.Values);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002944 RenameMapKey Key = makeRenameMapKey(FunctionName, PFT, ID.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002945 CurModule.RenameMap[Key] = NewName;
Reid Spencerd2920cd2007-03-21 17:27:53 +00002946 }
Reid Spenceref9b9a72007-02-05 20:47:22 +00002947 } else {
Reid Spencered96d1e2007-02-08 09:08:52 +00002948 // There's no conflict, just define the function
2949 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
2950 InsertValue(Fn, CurModule.Values);
Reid Spenceref9b9a72007-02-05 20:47:22 +00002951 }
Reid Spencere59f4932007-04-16 03:05:01 +00002952 } else {
2953 // There's no conflict, just define the function
2954 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
2955 InsertValue(Fn, CurModule.Values);
Reid Spencer950bf602007-01-26 08:19:09 +00002956 }
2957
Reid Spencere59f4932007-04-16 03:05:01 +00002958
Reid Spencer950bf602007-01-26 08:19:09 +00002959 CurFun.FunctionStart(Fn);
2960
2961 if (CurFun.isDeclare) {
2962 // If we have declaration, always overwrite linkage. This will allow us
2963 // to correctly handle cases, when pointer to function is passed as
2964 // argument to another function.
2965 Fn->setLinkage(CurFun.Linkage);
2966 }
Reid Spencerb7046c72007-01-29 05:41:34 +00002967 Fn->setCallingConv(upgradeCallingConv($1));
Reid Spencer950bf602007-01-26 08:19:09 +00002968 Fn->setAlignment($8);
2969 if ($7) {
2970 Fn->setSection($7);
2971 free($7);
2972 }
2973
Duncan Sandsdc024672007-11-27 13:23:08 +00002974 // Convert the CSRet calling convention into the corresponding parameter
2975 // attribute.
2976 if ($1 == OldCallingConv::CSRet) {
2977 ParamAttrsVector Attrs;
2978 ParamAttrsWithIndex PAWI;
2979 PAWI.index = 1; PAWI.attrs = ParamAttr::StructRet; // first arg
2980 Attrs.push_back(PAWI);
2981 Fn->setParamAttrs(ParamAttrsList::get(Attrs));
2982 }
2983
Reid Spencer950bf602007-01-26 08:19:09 +00002984 // Add all of the arguments we parsed to the function...
2985 if ($5) { // Is null if empty...
2986 if (isVarArg) { // Nuke the last entry
Reid Spencered96d1e2007-02-08 09:08:52 +00002987 assert($5->back().first.PAT->get() == Type::VoidTy &&
Reid Spencer950bf602007-01-26 08:19:09 +00002988 $5->back().second == 0 && "Not a varargs marker");
Reid Spencered96d1e2007-02-08 09:08:52 +00002989 delete $5->back().first.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002990 $5->pop_back(); // Delete the last entry
2991 }
2992 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002993 Function::arg_iterator ArgEnd = Fn->arg_end();
2994 std::vector<std::pair<PATypeInfo,char*> >::iterator I = $5->begin();
2995 std::vector<std::pair<PATypeInfo,char*> >::iterator E = $5->end();
2996 for ( ; I != E && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencered96d1e2007-02-08 09:08:52 +00002997 delete I->first.PAT; // Delete the typeholder...
Reid Spencerbb1fd572007-03-21 17:15:50 +00002998 ValueInfo VI; VI.V = ArgIt; VI.S.copy(I->first.S);
2999 setValueName(VI, I->second); // Insert arg into symtab...
Reid Spencer950bf602007-01-26 08:19:09 +00003000 InsertValue(ArgIt);
3001 }
3002 delete $5; // We're now done with the argument list
3003 }
Reid Spencer7eea8ff2007-05-18 05:48:07 +00003004 lastCallingConv = OldCallingConv::C;
Reid Spencer950bf602007-01-26 08:19:09 +00003005 }
3006 ;
3007
3008BEGIN
3009 : BEGINTOK | '{' // Allow BEGIN or '{' to start a function
Jeff Cohenac2dca92007-01-21 19:30:52 +00003010 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003011
Reid Spencer6fd36ab2006-12-29 20:35:03 +00003012FunctionHeader
Reid Spencerd2920cd2007-03-21 17:27:53 +00003013 : OptLinkage { CurFun.Linkage = $1; } FunctionHeaderH BEGIN {
Reid Spencer950bf602007-01-26 08:19:09 +00003014 $$ = CurFun.CurrentFunction;
3015
3016 // Make sure that we keep track of the linkage type even if there was a
3017 // previous "declare".
3018 $$->setLinkage($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00003019 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00003020 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003021
Reid Spencer950bf602007-01-26 08:19:09 +00003022END
3023 : ENDTOK | '}' // Allow end of '}' to end a function
3024 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003025
Reid Spencer950bf602007-01-26 08:19:09 +00003026Function
3027 : BasicBlockList END {
3028 $$ = $1;
3029 };
Reid Spencere7c3c602006-11-30 06:36:44 +00003030
Reid Spencere77e35e2006-12-01 20:26:20 +00003031FnDeclareLinkage
Reid Spencered96d1e2007-02-08 09:08:52 +00003032 : /*default*/ { $$ = GlobalValue::ExternalLinkage; }
3033 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
3034 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencere7c3c602006-11-30 06:36:44 +00003035 ;
3036
3037FunctionProto
Reid Spencered96d1e2007-02-08 09:08:52 +00003038 : DECLARE { CurFun.isDeclare = true; }
3039 FnDeclareLinkage { CurFun.Linkage = $3; } FunctionHeaderH {
Reid Spencer950bf602007-01-26 08:19:09 +00003040 $$ = CurFun.CurrentFunction;
3041 CurFun.FunctionDone();
3042
3043 }
3044 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003045
3046//===----------------------------------------------------------------------===//
3047// Rules to match Basic Blocks
3048//===----------------------------------------------------------------------===//
3049
Reid Spencer950bf602007-01-26 08:19:09 +00003050OptSideEffect
3051 : /* empty */ { $$ = false; }
3052 | SIDEEFFECT { $$ = true; }
3053 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003054
Reid Spencere77e35e2006-12-01 20:26:20 +00003055ConstValueRef
Reid Spencer950bf602007-01-26 08:19:09 +00003056 // A reference to a direct constant
Reid Spencerbb1fd572007-03-21 17:15:50 +00003057 : ESINT64VAL { $$ = ValID::create($1); }
Reid Spencer950bf602007-01-26 08:19:09 +00003058 | EUINT64VAL { $$ = ValID::create($1); }
3059 | FPVAL { $$ = ValID::create($1); }
Reid Spencerbb1fd572007-03-21 17:15:50 +00003060 | TRUETOK {
3061 $$ = ValID::create(ConstantInt::get(Type::Int1Ty, true));
3062 $$.S.makeUnsigned();
3063 }
3064 | FALSETOK {
3065 $$ = ValID::create(ConstantInt::get(Type::Int1Ty, false));
3066 $$.S.makeUnsigned();
3067 }
Reid Spencer950bf602007-01-26 08:19:09 +00003068 | NULL_TOK { $$ = ValID::createNull(); }
3069 | UNDEF { $$ = ValID::createUndef(); }
3070 | ZEROINITIALIZER { $$ = ValID::createZeroInit(); }
3071 | '<' ConstVector '>' { // Nonempty unsized packed vector
3072 const Type *ETy = (*$2)[0].C->getType();
3073 int NumElements = $2->size();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003074 VectorType* pt = VectorType::get(ETy, NumElements);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003075 $$.S.makeComposite((*$2)[0].S);
3076 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(pt, $$.S));
Reid Spencer950bf602007-01-26 08:19:09 +00003077
3078 // Verify all elements are correct type!
3079 std::vector<Constant*> Elems;
3080 for (unsigned i = 0; i < $2->size(); i++) {
3081 Constant *C = (*$2)[i].C;
3082 const Type *CTy = C->getType();
3083 if (ETy != CTy)
3084 error("Element #" + utostr(i) + " is not of type '" +
3085 ETy->getDescription() +"' as required!\nIt is of type '" +
3086 CTy->getDescription() + "'");
3087 Elems.push_back(C);
Reid Spencere7c3c602006-11-30 06:36:44 +00003088 }
Reid Spencer5eb77c72007-03-15 03:26:42 +00003089 $$ = ValID::create(ConstantVector::get(pt, Elems));
Reid Spencer950bf602007-01-26 08:19:09 +00003090 delete PTy; delete $2;
3091 }
3092 | ConstExpr {
Reid Spencer5eb77c72007-03-15 03:26:42 +00003093 $$ = ValID::create($1.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003094 $$.S.copy($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003095 }
3096 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
3097 char *End = UnEscapeLexed($3, true);
3098 std::string AsmStr = std::string($3, End);
3099 End = UnEscapeLexed($5, true);
3100 std::string Constraints = std::string($5, End);
3101 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
3102 free($3);
3103 free($5);
3104 }
3105 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003106
Christopher Lamb4374f8e2007-12-17 01:17:35 +00003107// SymbolicValueRef - Reference to one of two ways of symbolically refering to
3108// another value.
Reid Spencer950bf602007-01-26 08:19:09 +00003109//
3110SymbolicValueRef
Reid Spencerbb1fd572007-03-21 17:15:50 +00003111 : INTVAL { $$ = ValID::create($1); $$.S.makeSignless(); }
3112 | Name { $$ = ValID::create($1); $$.S.makeSignless(); }
Reid Spencer950bf602007-01-26 08:19:09 +00003113 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003114
3115// ValueRef - A reference to a definition... either constant or symbolic
Reid Spencerf459d392006-12-02 16:19:52 +00003116ValueRef
Reid Spencer950bf602007-01-26 08:19:09 +00003117 : SymbolicValueRef | ConstValueRef
Reid Spencerf459d392006-12-02 16:19:52 +00003118 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003119
Reid Spencer950bf602007-01-26 08:19:09 +00003120
Reid Spencere7c3c602006-11-30 06:36:44 +00003121// ResolvedVal - a <type> <value> pair. This is used only in cases where the
3122// type immediately preceeds the value reference, and allows complex constant
3123// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Reid Spencer950bf602007-01-26 08:19:09 +00003124ResolvedVal
3125 : Types ValueRef {
Reid Spencered96d1e2007-02-08 09:08:52 +00003126 const Type *Ty = $1.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003127 $2.S.copy($1.S);
Reid Spencer5eb77c72007-03-15 03:26:42 +00003128 $$.V = getVal(Ty, $2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003129 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003130 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003131 }
Reid Spencer950bf602007-01-26 08:19:09 +00003132 ;
3133
3134BasicBlockList
3135 : BasicBlockList BasicBlock {
3136 $$ = $1;
3137 }
3138 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
3139 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00003140 };
3141
3142
3143// Basic blocks are terminated by branching instructions:
3144// br, br/cc, switch, ret
3145//
Reid Spencer950bf602007-01-26 08:19:09 +00003146BasicBlock
3147 : InstructionList OptAssign BBTerminatorInst {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003148 ValueInfo VI; VI.V = $3.TI; VI.S.copy($3.S);
3149 setValueName(VI, $2);
3150 InsertValue($3.TI);
3151 $1->getInstList().push_back($3.TI);
Reid Spencer950bf602007-01-26 08:19:09 +00003152 InsertValue($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00003153 $$ = $1;
3154 }
Reid Spencer950bf602007-01-26 08:19:09 +00003155 ;
3156
3157InstructionList
3158 : InstructionList Inst {
3159 if ($2.I)
3160 $1->getInstList().push_back($2.I);
3161 $$ = $1;
3162 }
3163 | /* empty */ {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003164 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++),true);
Reid Spencer950bf602007-01-26 08:19:09 +00003165 // Make sure to move the basic block to the correct location in the
3166 // function, instead of leaving it inserted wherever it was first
3167 // referenced.
3168 Function::BasicBlockListType &BBL =
3169 CurFun.CurrentFunction->getBasicBlockList();
3170 BBL.splice(BBL.end(), BBL, $$);
3171 }
3172 | LABELSTR {
Reid Spencer5eb77c72007-03-15 03:26:42 +00003173 $$ = CurBB = getBBVal(ValID::create($1), true);
Reid Spencer950bf602007-01-26 08:19:09 +00003174 // Make sure to move the basic block to the correct location in the
3175 // function, instead of leaving it inserted wherever it was first
3176 // referenced.
3177 Function::BasicBlockListType &BBL =
3178 CurFun.CurrentFunction->getBasicBlockList();
3179 BBL.splice(BBL.end(), BBL, $$);
3180 }
3181 ;
3182
3183Unwind : UNWIND | EXCEPT;
3184
3185BBTerminatorInst
3186 : RET ResolvedVal { // Return with a result...
Reid Spencerbb1fd572007-03-21 17:15:50 +00003187 $$.TI = new ReturnInst($2.V);
3188 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003189 }
3190 | RET VOID { // Return with no result...
Reid Spencerbb1fd572007-03-21 17:15:50 +00003191 $$.TI = new ReturnInst();
3192 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003193 }
3194 | BR LABEL ValueRef { // Unconditional Branch...
3195 BasicBlock* tmpBB = getBBVal($3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003196 $$.TI = new BranchInst(tmpBB);
3197 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003198 } // Conditional Branch...
3199 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003200 $6.S.makeSignless();
3201 $9.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003202 BasicBlock* tmpBBA = getBBVal($6);
3203 BasicBlock* tmpBBB = getBBVal($9);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003204 $3.S.makeUnsigned();
Reid Spencer950bf602007-01-26 08:19:09 +00003205 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003206 $$.TI = new BranchInst(tmpBBA, tmpBBB, tmpVal);
3207 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003208 }
3209 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003210 $3.S.copy($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003211 Value* tmpVal = getVal($2.T, $3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003212 $6.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003213 BasicBlock* tmpBB = getBBVal($6);
3214 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Reid Spencerbb1fd572007-03-21 17:15:50 +00003215 $$.TI = S;
3216 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003217 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
3218 E = $8->end();
3219 for (; I != E; ++I) {
3220 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
3221 S->addCase(CI, I->second);
3222 else
3223 error("Switch case is constant, but not a simple integer");
3224 }
3225 delete $8;
3226 }
3227 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003228 $3.S.copy($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003229 Value* tmpVal = getVal($2.T, $3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003230 $6.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003231 BasicBlock* tmpBB = getBBVal($6);
3232 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003233 $$.TI = S;
3234 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003235 }
3236 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
3237 TO LABEL ValueRef Unwind LABEL ValueRef {
3238 const PointerType *PFTy;
3239 const FunctionType *Ty;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003240 Signedness FTySign;
Reid Spencer950bf602007-01-26 08:19:09 +00003241
Reid Spencered96d1e2007-02-08 09:08:52 +00003242 if (!(PFTy = dyn_cast<PointerType>($3.PAT->get())) ||
Reid Spencer950bf602007-01-26 08:19:09 +00003243 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3244 // Pull out the types of all of the arguments...
3245 std::vector<const Type*> ParamTypes;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003246 FTySign.makeComposite($3.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003247 if ($6) {
3248 for (std::vector<ValueInfo>::iterator I = $6->begin(), E = $6->end();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003249 I != E; ++I) {
Reid Spencer950bf602007-01-26 08:19:09 +00003250 ParamTypes.push_back((*I).V->getType());
Reid Spencerbb1fd572007-03-21 17:15:50 +00003251 FTySign.add(I->S);
3252 }
Reid Spencer950bf602007-01-26 08:19:09 +00003253 }
3254 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
3255 if (isVarArg) ParamTypes.pop_back();
Duncan Sandsdc024672007-11-27 13:23:08 +00003256 Ty = FunctionType::get($3.PAT->get(), ParamTypes, isVarArg);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00003257 PFTy = PointerType::getUnqual(Ty);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003258 $$.S.copy($3.S);
3259 } else {
3260 FTySign = $3.S;
Reid Spencera3b12dd2007-04-07 16:14:01 +00003261 // Get the signedness of the result type. $3 is the pointer to the
3262 // function type so we get the 0th element to extract the function type,
3263 // and then the 0th element again to get the result type.
3264 $$.S.copy($3.S.get(0).get(0));
Reid Spencer950bf602007-01-26 08:19:09 +00003265 }
Reid Spencer7b5d4662007-04-09 06:16:21 +00003266
Reid Spencerbb1fd572007-03-21 17:15:50 +00003267 $4.S.makeComposite(FTySign);
Reid Spencer950bf602007-01-26 08:19:09 +00003268 Value *V = getVal(PFTy, $4); // Get the function we're calling...
3269 BasicBlock *Normal = getBBVal($10);
3270 BasicBlock *Except = getBBVal($13);
3271
3272 // Create the call node...
3273 if (!$6) { // Has no arguments?
David Greene5fd22a82007-09-04 18:46:50 +00003274 std::vector<Value*> Args;
3275 $$.TI = new InvokeInst(V, Normal, Except, Args.begin(), Args.end());
Reid Spencer950bf602007-01-26 08:19:09 +00003276 } else { // Has arguments?
3277 // Loop through FunctionType's arguments and ensure they are specified
3278 // correctly!
3279 //
3280 FunctionType::param_iterator I = Ty->param_begin();
3281 FunctionType::param_iterator E = Ty->param_end();
3282 std::vector<ValueInfo>::iterator ArgI = $6->begin(), ArgE = $6->end();
3283
3284 std::vector<Value*> Args;
3285 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
3286 if ((*ArgI).V->getType() != *I)
3287 error("Parameter " +(*ArgI).V->getName()+ " is not of type '" +
3288 (*I)->getDescription() + "'");
3289 Args.push_back((*ArgI).V);
3290 }
3291
3292 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
3293 error("Invalid number of parameters detected");
3294
David Greene5fd22a82007-09-04 18:46:50 +00003295 $$.TI = new InvokeInst(V, Normal, Except, Args.begin(), Args.end());
Reid Spencer950bf602007-01-26 08:19:09 +00003296 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00003297 cast<InvokeInst>($$.TI)->setCallingConv(upgradeCallingConv($2));
Duncan Sandsdc024672007-11-27 13:23:08 +00003298 if ($2 == OldCallingConv::CSRet) {
3299 ParamAttrsVector Attrs;
3300 ParamAttrsWithIndex PAWI;
3301 PAWI.index = 1; PAWI.attrs = ParamAttr::StructRet; // first arg
3302 Attrs.push_back(PAWI);
3303 cast<InvokeInst>($$.TI)->setParamAttrs(ParamAttrsList::get(Attrs));
3304 }
Reid Spencered96d1e2007-02-08 09:08:52 +00003305 delete $3.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003306 delete $6;
Reid Spencer7eea8ff2007-05-18 05:48:07 +00003307 lastCallingConv = OldCallingConv::C;
Reid Spencer950bf602007-01-26 08:19:09 +00003308 }
3309 | Unwind {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003310 $$.TI = new UnwindInst();
3311 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003312 }
3313 | UNREACHABLE {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003314 $$.TI = new UnreachableInst();
3315 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003316 }
3317 ;
3318
3319JumpTable
3320 : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
3321 $$ = $1;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003322 $3.S.copy($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003323 Constant *V = cast<Constant>(getExistingValue($2.T, $3));
3324
3325 if (V == 0)
3326 error("May only switch on a constant pool value");
3327
Reid Spencerbb1fd572007-03-21 17:15:50 +00003328 $6.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003329 BasicBlock* tmpBB = getBBVal($6);
3330 $$->push_back(std::make_pair(V, tmpBB));
3331 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003332 | IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencer950bf602007-01-26 08:19:09 +00003333 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003334 $2.S.copy($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003335 Constant *V = cast<Constant>(getExistingValue($1.T, $2));
3336
3337 if (V == 0)
3338 error("May only switch on a constant pool value");
3339
Reid Spencerbb1fd572007-03-21 17:15:50 +00003340 $5.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003341 BasicBlock* tmpBB = getBBVal($5);
3342 $$->push_back(std::make_pair(V, tmpBB));
3343 }
3344 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003345
3346Inst
3347 : OptAssign InstVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003348 bool omit = false;
3349 if ($1)
3350 if (BitCastInst *BCI = dyn_cast<BitCastInst>($2.I))
3351 if (BCI->getSrcTy() == BCI->getDestTy() &&
3352 BCI->getOperand(0)->getName() == $1)
3353 // This is a useless bit cast causing a name redefinition. It is
3354 // a bit cast from a type to the same type of an operand with the
3355 // same name as the name we would give this instruction. Since this
3356 // instruction results in no code generation, it is safe to omit
3357 // the instruction. This situation can occur because of collapsed
3358 // type planes. For example:
3359 // %X = add int %Y, %Z
3360 // %X = cast int %Y to uint
3361 // After upgrade, this looks like:
3362 // %X = add i32 %Y, %Z
3363 // %X = bitcast i32 to i32
3364 // The bitcast is clearly useless so we omit it.
3365 omit = true;
3366 if (omit) {
3367 $$.I = 0;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003368 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003369 } else {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003370 ValueInfo VI; VI.V = $2.I; VI.S.copy($2.S);
3371 setValueName(VI, $1);
Reid Spencer950bf602007-01-26 08:19:09 +00003372 InsertValue($2.I);
3373 $$ = $2;
Reid Spencerf5626a32007-01-01 01:20:41 +00003374 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003375 };
3376
Reid Spencer950bf602007-01-26 08:19:09 +00003377PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
3378 $$.P = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003379 $$.S.copy($1.S);
3380 $3.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003381 Value* tmpVal = getVal($1.PAT->get(), $3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003382 $5.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003383 BasicBlock* tmpBB = getBBVal($5);
3384 $$.P->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencered96d1e2007-02-08 09:08:52 +00003385 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003386 }
3387 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
Reid Spencere7c3c602006-11-30 06:36:44 +00003388 $$ = $1;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003389 $4.S.copy($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003390 Value* tmpVal = getVal($1.P->front().first->getType(), $4);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003391 $6.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003392 BasicBlock* tmpBB = getBBVal($6);
3393 $1.P->push_back(std::make_pair(tmpVal, tmpBB));
3394 }
3395 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003396
Reid Spencer950bf602007-01-26 08:19:09 +00003397ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
3398 $$ = new std::vector<ValueInfo>();
Reid Spencerf8483652006-12-02 15:16:01 +00003399 $$->push_back($1);
3400 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003401 | ValueRefList ',' ResolvedVal {
Reid Spencere7c3c602006-11-30 06:36:44 +00003402 $$ = $1;
Reid Spencer950bf602007-01-26 08:19:09 +00003403 $1->push_back($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00003404 };
3405
3406// ValueRefListE - Just like ValueRefList, except that it may also be empty!
3407ValueRefListE
Reid Spencer950bf602007-01-26 08:19:09 +00003408 : ValueRefList
3409 | /*empty*/ { $$ = 0; }
Reid Spencere7c3c602006-11-30 06:36:44 +00003410 ;
3411
3412OptTailCall
3413 : TAIL CALL {
Reid Spencer950bf602007-01-26 08:19:09 +00003414 $$ = true;
Reid Spencere7c3c602006-11-30 06:36:44 +00003415 }
Reid Spencer950bf602007-01-26 08:19:09 +00003416 | CALL {
3417 $$ = false;
3418 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003419 ;
3420
Reid Spencer950bf602007-01-26 08:19:09 +00003421InstVal
3422 : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003423 $3.S.copy($2.S);
3424 $5.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003425 const Type* Ty = $2.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003426 if (!Ty->isInteger() && !Ty->isFloatingPoint() && !isa<VectorType>(Ty))
Reid Spencer950bf602007-01-26 08:19:09 +00003427 error("Arithmetic operator requires integer, FP, or packed operands");
Reid Spencer9d6565a2007-02-15 02:26:10 +00003428 if (isa<VectorType>(Ty) &&
Reid Spencer950bf602007-01-26 08:19:09 +00003429 ($1 == URemOp || $1 == SRemOp || $1 == FRemOp || $1 == RemOp))
Chris Lattner4227bdb2007-02-19 07:34:02 +00003430 error("Remainder not supported on vector types");
Reid Spencer950bf602007-01-26 08:19:09 +00003431 // Upgrade the opcode from obsolete versions before we do anything with it.
3432 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $2.S);
3433 Value* val1 = getVal(Ty, $3);
3434 Value* val2 = getVal(Ty, $5);
3435 $$.I = BinaryOperator::create(Opcode, val1, val2);
3436 if ($$.I == 0)
3437 error("binary operator returned null");
Reid Spencerbb1fd572007-03-21 17:15:50 +00003438 $$.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003439 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003440 }
3441 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003442 $3.S.copy($2.S);
3443 $5.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003444 const Type *Ty = $2.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003445 if (!Ty->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00003446 if (!isa<VectorType>(Ty) ||
3447 !cast<VectorType>(Ty)->getElementType()->isInteger())
Reid Spencer950bf602007-01-26 08:19:09 +00003448 error("Logical operator requires integral operands");
3449 }
3450 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $2.S);
3451 Value* tmpVal1 = getVal(Ty, $3);
3452 Value* tmpVal2 = getVal(Ty, $5);
3453 $$.I = BinaryOperator::create(Opcode, tmpVal1, tmpVal2);
3454 if ($$.I == 0)
3455 error("binary operator returned null");
Reid Spencerbb1fd572007-03-21 17:15:50 +00003456 $$.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003457 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003458 }
3459 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003460 $3.S.copy($2.S);
3461 $5.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003462 const Type* Ty = $2.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003463 if(isa<VectorType>(Ty))
3464 error("VectorTypes currently not supported in setcc instructions");
Reid Spencer950bf602007-01-26 08:19:09 +00003465 unsigned short pred;
3466 Instruction::OtherOps Opcode = getCompareOp($1, pred, Ty, $2.S);
3467 Value* tmpVal1 = getVal(Ty, $3);
3468 Value* tmpVal2 = getVal(Ty, $5);
3469 $$.I = CmpInst::create(Opcode, pred, tmpVal1, tmpVal2);
3470 if ($$.I == 0)
3471 error("binary operator returned null");
Reid Spencerbb1fd572007-03-21 17:15:50 +00003472 $$.S.makeUnsigned();
Reid Spencered96d1e2007-02-08 09:08:52 +00003473 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003474 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00003475 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003476 $4.S.copy($3.S);
3477 $6.S.copy($3.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003478 const Type *Ty = $3.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003479 if (isa<VectorType>(Ty))
3480 error("VectorTypes currently not supported in icmp instructions");
Reid Spencer950bf602007-01-26 08:19:09 +00003481 else if (!Ty->isInteger() && !isa<PointerType>(Ty))
3482 error("icmp requires integer or pointer typed operands");
3483 Value* tmpVal1 = getVal(Ty, $4);
3484 Value* tmpVal2 = getVal(Ty, $6);
3485 $$.I = new ICmpInst($2, tmpVal1, tmpVal2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003486 $$.S.makeUnsigned();
Reid Spencered96d1e2007-02-08 09:08:52 +00003487 delete $3.PAT;
Reid Spencer57f28f92006-12-03 07:10:26 +00003488 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00003489 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003490 $4.S.copy($3.S);
3491 $6.S.copy($3.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003492 const Type *Ty = $3.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003493 if (isa<VectorType>(Ty))
3494 error("VectorTypes currently not supported in fcmp instructions");
Reid Spencer950bf602007-01-26 08:19:09 +00003495 else if (!Ty->isFloatingPoint())
3496 error("fcmp instruction requires floating point operands");
3497 Value* tmpVal1 = getVal(Ty, $4);
3498 Value* tmpVal2 = getVal(Ty, $6);
3499 $$.I = new FCmpInst($2, tmpVal1, tmpVal2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003500 $$.S.makeUnsigned();
Reid Spencered96d1e2007-02-08 09:08:52 +00003501 delete $3.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003502 }
3503 | NOT ResolvedVal {
3504 warning("Use of obsolete 'not' instruction: Replacing with 'xor");
3505 const Type *Ty = $2.V->getType();
3506 Value *Ones = ConstantInt::getAllOnesValue(Ty);
3507 if (Ones == 0)
3508 error("Expected integral type for not instruction");
3509 $$.I = BinaryOperator::create(Instruction::Xor, $2.V, Ones);
3510 if ($$.I == 0)
3511 error("Could not create a xor instruction");
Reid Spencerbb1fd572007-03-21 17:15:50 +00003512 $$.S.copy($2.S);
Reid Spencer229e9362006-12-02 22:14:11 +00003513 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003514 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003515 if (!$4.V->getType()->isInteger() ||
3516 cast<IntegerType>($4.V->getType())->getBitWidth() != 8)
3517 error("Shift amount must be int8");
Reid Spencer832254e2007-02-02 02:16:23 +00003518 const Type* Ty = $2.V->getType();
3519 if (!Ty->isInteger())
Reid Spencer950bf602007-01-26 08:19:09 +00003520 error("Shift constant expression requires integer operand");
Reid Spencer832254e2007-02-02 02:16:23 +00003521 Value* ShiftAmt = 0;
3522 if (cast<IntegerType>(Ty)->getBitWidth() > Type::Int8Ty->getBitWidth())
3523 if (Constant *C = dyn_cast<Constant>($4.V))
3524 ShiftAmt = ConstantExpr::getZExt(C, Ty);
3525 else
3526 ShiftAmt = new ZExtInst($4.V, Ty, makeNameUnique("shift"), CurBB);
3527 else
3528 ShiftAmt = $4.V;
3529 $$.I = BinaryOperator::create(getBinaryOp($1, Ty, $2.S), $2.V, ShiftAmt);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003530 $$.S.copy($2.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00003531 }
Reid Spencerfcb5df82006-12-01 22:34:43 +00003532 | CastOps ResolvedVal TO Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00003533 const Type *DstTy = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003534 if (!DstTy->isFirstClassType())
3535 error("cast instruction to a non-primitive type: '" +
3536 DstTy->getDescription() + "'");
3537 $$.I = cast<Instruction>(getCast($1, $2.V, $2.S, DstTy, $4.S, true));
Reid Spencerbb1fd572007-03-21 17:15:50 +00003538 $$.S.copy($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003539 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003540 }
3541 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003542 if (!$2.V->getType()->isInteger() ||
3543 cast<IntegerType>($2.V->getType())->getBitWidth() != 1)
3544 error("select condition must be bool");
3545 if ($4.V->getType() != $6.V->getType())
3546 error("select value types should match");
3547 $$.I = new SelectInst($2.V, $4.V, $6.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003548 $$.S.copy($4.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00003549 }
3550 | VAARG ResolvedVal ',' Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00003551 const Type *Ty = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003552 NewVarArgs = true;
3553 $$.I = new VAArgInst($2.V, Ty);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003554 $$.S.copy($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003555 delete $4.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003556 }
3557 | VAARG_old ResolvedVal ',' Types {
3558 const Type* ArgTy = $2.V->getType();
Reid Spencered96d1e2007-02-08 09:08:52 +00003559 const Type* DstTy = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003560 ObsoleteVarArgs = true;
3561 Function* NF = cast<Function>(CurModule.CurrentModule->
3562 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0));
3563
3564 //b = vaarg a, t ->
3565 //foo = alloca 1 of t
3566 //bar = vacopy a
3567 //store bar -> foo
3568 //b = vaarg foo, t
3569 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix");
3570 CurBB->getInstList().push_back(foo);
3571 CallInst* bar = new CallInst(NF, $2.V);
3572 CurBB->getInstList().push_back(bar);
3573 CurBB->getInstList().push_back(new StoreInst(bar, foo));
3574 $$.I = new VAArgInst(foo, DstTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003575 $$.S.copy($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003576 delete $4.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003577 }
3578 | VANEXT_old ResolvedVal ',' Types {
3579 const Type* ArgTy = $2.V->getType();
Reid Spencered96d1e2007-02-08 09:08:52 +00003580 const Type* DstTy = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003581 ObsoleteVarArgs = true;
3582 Function* NF = cast<Function>(CurModule.CurrentModule->
3583 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0));
3584
3585 //b = vanext a, t ->
3586 //foo = alloca 1 of t
3587 //bar = vacopy a
3588 //store bar -> foo
3589 //tmp = vaarg foo, t
3590 //b = load foo
3591 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix");
3592 CurBB->getInstList().push_back(foo);
3593 CallInst* bar = new CallInst(NF, $2.V);
3594 CurBB->getInstList().push_back(bar);
3595 CurBB->getInstList().push_back(new StoreInst(bar, foo));
3596 Instruction* tmp = new VAArgInst(foo, DstTy);
3597 CurBB->getInstList().push_back(tmp);
3598 $$.I = new LoadInst(foo);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003599 $$.S.copy($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003600 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003601 }
3602 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003603 if (!ExtractElementInst::isValidOperands($2.V, $4.V))
3604 error("Invalid extractelement operands");
3605 $$.I = new ExtractElementInst($2.V, $4.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003606 $$.S.copy($2.S.get(0));
Reid Spencere7c3c602006-11-30 06:36:44 +00003607 }
3608 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003609 if (!InsertElementInst::isValidOperands($2.V, $4.V, $6.V))
3610 error("Invalid insertelement operands");
3611 $$.I = new InsertElementInst($2.V, $4.V, $6.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003612 $$.S.copy($2.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00003613 }
3614 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003615 if (!ShuffleVectorInst::isValidOperands($2.V, $4.V, $6.V))
3616 error("Invalid shufflevector operands");
3617 $$.I = new ShuffleVectorInst($2.V, $4.V, $6.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003618 $$.S.copy($2.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00003619 }
3620 | PHI_TOK PHIList {
Reid Spencer950bf602007-01-26 08:19:09 +00003621 const Type *Ty = $2.P->front().first->getType();
3622 if (!Ty->isFirstClassType())
3623 error("PHI node operands must be of first class type");
3624 PHINode *PHI = new PHINode(Ty);
3625 PHI->reserveOperandSpace($2.P->size());
3626 while ($2.P->begin() != $2.P->end()) {
3627 if ($2.P->front().first->getType() != Ty)
3628 error("All elements of a PHI node must be of the same type");
3629 PHI->addIncoming($2.P->front().first, $2.P->front().second);
3630 $2.P->pop_front();
3631 }
3632 $$.I = PHI;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003633 $$.S.copy($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003634 delete $2.P; // Free the list...
Reid Spencere7c3c602006-11-30 06:36:44 +00003635 }
Reid Spencer7b5d4662007-04-09 06:16:21 +00003636 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00003637 // Handle the short call syntax
3638 const PointerType *PFTy;
3639 const FunctionType *FTy;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003640 Signedness FTySign;
Reid Spencered96d1e2007-02-08 09:08:52 +00003641 if (!(PFTy = dyn_cast<PointerType>($3.PAT->get())) ||
Reid Spencer950bf602007-01-26 08:19:09 +00003642 !(FTy = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3643 // Pull out the types of all of the arguments...
3644 std::vector<const Type*> ParamTypes;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003645 FTySign.makeComposite($3.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003646 if ($6) {
3647 for (std::vector<ValueInfo>::iterator I = $6->begin(), E = $6->end();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003648 I != E; ++I) {
Reid Spencer950bf602007-01-26 08:19:09 +00003649 ParamTypes.push_back((*I).V->getType());
Reid Spencerbb1fd572007-03-21 17:15:50 +00003650 FTySign.add(I->S);
3651 }
Reid Spencerc4d96252007-01-13 00:03:30 +00003652 }
Reid Spencer950bf602007-01-26 08:19:09 +00003653
3654 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
3655 if (isVarArg) ParamTypes.pop_back();
3656
Reid Spencered96d1e2007-02-08 09:08:52 +00003657 const Type *RetTy = $3.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003658 if (!RetTy->isFirstClassType() && RetTy != Type::VoidTy)
3659 error("Functions cannot return aggregate types");
3660
Duncan Sandsdc024672007-11-27 13:23:08 +00003661 FTy = FunctionType::get(RetTy, ParamTypes, isVarArg);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00003662 PFTy = PointerType::getUnqual(FTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003663 $$.S.copy($3.S);
3664 } else {
3665 FTySign = $3.S;
Reid Spencera3b12dd2007-04-07 16:14:01 +00003666 // Get the signedness of the result type. $3 is the pointer to the
3667 // function type so we get the 0th element to extract the function type,
3668 // and then the 0th element again to get the result type.
3669 $$.S.copy($3.S.get(0).get(0));
Reid Spencerf8483652006-12-02 15:16:01 +00003670 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00003671 $4.S.makeComposite(FTySign);
Reid Spencer950bf602007-01-26 08:19:09 +00003672
3673 // First upgrade any intrinsic calls.
3674 std::vector<Value*> Args;
3675 if ($6)
3676 for (unsigned i = 0, e = $6->size(); i < e; ++i)
3677 Args.push_back((*$6)[i].V);
Reid Spencer41b213e2007-04-02 01:14:00 +00003678 Instruction *Inst = upgradeIntrinsicCall(FTy->getReturnType(), $4, Args);
Reid Spencer950bf602007-01-26 08:19:09 +00003679
3680 // If we got an upgraded intrinsic
3681 if (Inst) {
3682 $$.I = Inst;
Reid Spencer950bf602007-01-26 08:19:09 +00003683 } else {
3684 // Get the function we're calling
3685 Value *V = getVal(PFTy, $4);
3686
3687 // Check the argument values match
3688 if (!$6) { // Has no arguments?
3689 // Make sure no arguments is a good thing!
3690 if (FTy->getNumParams() != 0)
3691 error("No arguments passed to a function that expects arguments");
3692 } else { // Has arguments?
3693 // Loop through FunctionType's arguments and ensure they are specified
3694 // correctly!
3695 //
3696 FunctionType::param_iterator I = FTy->param_begin();
3697 FunctionType::param_iterator E = FTy->param_end();
3698 std::vector<ValueInfo>::iterator ArgI = $6->begin(), ArgE = $6->end();
3699
3700 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
3701 if ((*ArgI).V->getType() != *I)
3702 error("Parameter " +(*ArgI).V->getName()+ " is not of type '" +
3703 (*I)->getDescription() + "'");
3704
3705 if (I != E || (ArgI != ArgE && !FTy->isVarArg()))
3706 error("Invalid number of parameters detected");
3707 }
3708
3709 // Create the call instruction
David Greene718fda32007-08-01 03:59:32 +00003710 CallInst *CI = new CallInst(V, Args.begin(), Args.end());
Reid Spencer950bf602007-01-26 08:19:09 +00003711 CI->setTailCall($1);
Reid Spencerb7046c72007-01-29 05:41:34 +00003712 CI->setCallingConv(upgradeCallingConv($2));
Duncan Sandsdc024672007-11-27 13:23:08 +00003713
Reid Spencer950bf602007-01-26 08:19:09 +00003714 $$.I = CI;
Reid Spencer950bf602007-01-26 08:19:09 +00003715 }
Duncan Sandsdc024672007-11-27 13:23:08 +00003716 // Deal with CSRetCC
3717 if ($2 == OldCallingConv::CSRet) {
3718 ParamAttrsVector Attrs;
3719 ParamAttrsWithIndex PAWI;
3720 PAWI.index = 1; PAWI.attrs = ParamAttr::StructRet; // first arg
3721 Attrs.push_back(PAWI);
3722 cast<CallInst>($$.I)->setParamAttrs(ParamAttrsList::get(Attrs));
3723 }
Reid Spencered96d1e2007-02-08 09:08:52 +00003724 delete $3.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003725 delete $6;
Reid Spencer7eea8ff2007-05-18 05:48:07 +00003726 lastCallingConv = OldCallingConv::C;
Reid Spencere7c3c602006-11-30 06:36:44 +00003727 }
Reid Spencer950bf602007-01-26 08:19:09 +00003728 | MemoryInst {
3729 $$ = $1;
3730 }
3731 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003732
3733
3734// IndexList - List of indices for GEP based instructions...
3735IndexList
Reid Spencer950bf602007-01-26 08:19:09 +00003736 : ',' ValueRefList { $$ = $2; }
3737 | /* empty */ { $$ = new std::vector<ValueInfo>(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00003738 ;
3739
3740OptVolatile
Reid Spencer950bf602007-01-26 08:19:09 +00003741 : VOLATILE { $$ = true; }
3742 | /* empty */ { $$ = false; }
Reid Spencere7c3c602006-11-30 06:36:44 +00003743 ;
3744
Reid Spencer950bf602007-01-26 08:19:09 +00003745MemoryInst
3746 : MALLOC Types OptCAlign {
Reid Spencered96d1e2007-02-08 09:08:52 +00003747 const Type *Ty = $2.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003748 $$.S.makeComposite($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003749 $$.I = new MallocInst(Ty, 0, $3);
Reid Spencered96d1e2007-02-08 09:08:52 +00003750 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003751 }
3752 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencered96d1e2007-02-08 09:08:52 +00003753 const Type *Ty = $2.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003754 $5.S.makeUnsigned();
3755 $$.S.makeComposite($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003756 $$.I = new MallocInst(Ty, getVal($4.T, $5), $6);
Reid Spencered96d1e2007-02-08 09:08:52 +00003757 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003758 }
3759 | ALLOCA Types OptCAlign {
Reid Spencered96d1e2007-02-08 09:08:52 +00003760 const Type *Ty = $2.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003761 $$.S.makeComposite($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003762 $$.I = new AllocaInst(Ty, 0, $3);
Reid Spencered96d1e2007-02-08 09:08:52 +00003763 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003764 }
3765 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencered96d1e2007-02-08 09:08:52 +00003766 const Type *Ty = $2.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003767 $5.S.makeUnsigned();
3768 $$.S.makeComposite($4.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003769 $$.I = new AllocaInst(Ty, getVal($4.T, $5), $6);
Reid Spencered96d1e2007-02-08 09:08:52 +00003770 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003771 }
3772 | FREE ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003773 const Type *PTy = $2.V->getType();
3774 if (!isa<PointerType>(PTy))
3775 error("Trying to free nonpointer type '" + PTy->getDescription() + "'");
3776 $$.I = new FreeInst($2.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003777 $$.S.makeSignless();
Reid Spencere7c3c602006-11-30 06:36:44 +00003778 }
3779 | OptVolatile LOAD Types ValueRef {
Reid Spencered96d1e2007-02-08 09:08:52 +00003780 const Type* Ty = $3.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003781 $4.S.copy($3.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003782 if (!isa<PointerType>(Ty))
3783 error("Can't load from nonpointer type: " + Ty->getDescription());
3784 if (!cast<PointerType>(Ty)->getElementType()->isFirstClassType())
3785 error("Can't load from pointer of non-first-class type: " +
3786 Ty->getDescription());
3787 Value* tmpVal = getVal(Ty, $4);
3788 $$.I = new LoadInst(tmpVal, "", $1);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003789 $$.S.copy($3.S.get(0));
Reid Spencered96d1e2007-02-08 09:08:52 +00003790 delete $3.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003791 }
3792 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003793 $6.S.copy($5.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003794 const PointerType *PTy = dyn_cast<PointerType>($5.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00003795 if (!PTy)
3796 error("Can't store to a nonpointer type: " +
Reid Spencered96d1e2007-02-08 09:08:52 +00003797 $5.PAT->get()->getDescription());
Reid Spencer950bf602007-01-26 08:19:09 +00003798 const Type *ElTy = PTy->getElementType();
Reid Spencered96d1e2007-02-08 09:08:52 +00003799 Value *StoreVal = $3.V;
Reid Spencer950bf602007-01-26 08:19:09 +00003800 Value* tmpVal = getVal(PTy, $6);
Reid Spencered96d1e2007-02-08 09:08:52 +00003801 if (ElTy != $3.V->getType()) {
Christopher Lamb4374f8e2007-12-17 01:17:35 +00003802 PTy = PointerType::getUnqual(StoreVal->getType());
Duncan Sandsdc024672007-11-27 13:23:08 +00003803 if (Constant *C = dyn_cast<Constant>(tmpVal))
3804 tmpVal = ConstantExpr::getBitCast(C, PTy);
3805 else
3806 tmpVal = new BitCastInst(tmpVal, PTy, "upgrd.cast", CurBB);
Reid Spencered96d1e2007-02-08 09:08:52 +00003807 }
3808 $$.I = new StoreInst(StoreVal, tmpVal, $1);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003809 $$.S.makeSignless();
Reid Spencered96d1e2007-02-08 09:08:52 +00003810 delete $5.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003811 }
3812 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003813 $3.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003814 const Type* Ty = $2.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003815 if (!isa<PointerType>(Ty))
3816 error("getelementptr insn requires pointer operand");
3817
3818 std::vector<Value*> VIndices;
Reid Spencerff0e4482007-04-16 00:40:57 +00003819 upgradeGEPInstIndices(Ty, $4, VIndices);
Reid Spencer950bf602007-01-26 08:19:09 +00003820
3821 Value* tmpVal = getVal(Ty, $3);
David Greene5fd22a82007-09-04 18:46:50 +00003822 $$.I = new GetElementPtrInst(tmpVal, VIndices.begin(), VIndices.end());
Reid Spencerbb1fd572007-03-21 17:15:50 +00003823 ValueInfo VI; VI.V = tmpVal; VI.S.copy($2.S);
3824 $$.S.copy(getElementSign(VI, VIndices));
Reid Spencered96d1e2007-02-08 09:08:52 +00003825 delete $2.PAT;
Reid Spencer30d0c582007-01-15 00:26:18 +00003826 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00003827 };
3828
Reid Spencer950bf602007-01-26 08:19:09 +00003829
Reid Spencere7c3c602006-11-30 06:36:44 +00003830%%
3831
3832int yyerror(const char *ErrorMsg) {
3833 std::string where
3834 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Reid Spencered96d1e2007-02-08 09:08:52 +00003835 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
Reid Spencer950bf602007-01-26 08:19:09 +00003836 std::string errMsg = where + "error: " + std::string(ErrorMsg);
3837 if (yychar != YYEMPTY && yychar != 0)
3838 errMsg += " while reading token '" + std::string(Upgradetext, Upgradeleng) +
3839 "'.";
Reid Spencer71d2ec92006-12-31 06:02:26 +00003840 std::cerr << "llvm-upgrade: " << errMsg << '\n';
Reid Spencer950bf602007-01-26 08:19:09 +00003841 std::cout << "llvm-upgrade: parse failed.\n";
Reid Spencere7c3c602006-11-30 06:36:44 +00003842 exit(1);
3843}
Reid Spencer319a7302007-01-05 17:20:02 +00003844
Reid Spencer30d0c582007-01-15 00:26:18 +00003845void warning(const std::string& ErrorMsg) {
Reid Spencer319a7302007-01-05 17:20:02 +00003846 std::string where
3847 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Reid Spencered96d1e2007-02-08 09:08:52 +00003848 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
Reid Spencer950bf602007-01-26 08:19:09 +00003849 std::string errMsg = where + "warning: " + std::string(ErrorMsg);
3850 if (yychar != YYEMPTY && yychar != 0)
3851 errMsg += " while reading token '" + std::string(Upgradetext, Upgradeleng) +
3852 "'.";
Reid Spencer319a7302007-01-05 17:20:02 +00003853 std::cerr << "llvm-upgrade: " << errMsg << '\n';
3854}
Reid Spencer950bf602007-01-26 08:19:09 +00003855
3856void error(const std::string& ErrorMsg, int LineNo) {
3857 if (LineNo == -1) LineNo = Upgradelineno;
3858 Upgradelineno = LineNo;
3859 yyerror(ErrorMsg.c_str());
3860}
3861