blob: b9a26cb95726ec043c2df2e79463df41bf2cbc41 [file] [log] [blame]
Reid Spencer950bf602007-01-26 08:19:09 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
Reid Spencere7c3c602006-11-30 06:36:44 +00002//
3// The LLVM Compiler Infrastructure
4//
Reid Spencer950bf602007-01-26 08:19:09 +00005// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Reid Spencere7c3c602006-11-30 06:36:44 +00007//
8//===----------------------------------------------------------------------===//
9//
Reid Spencer950bf602007-01-26 08:19:09 +000010// This file implements the bison parser for LLVM assembly languages files.
Reid Spencere7c3c602006-11-30 06:36:44 +000011//
12//===----------------------------------------------------------------------===//
13
14%{
Reid Spencer319a7302007-01-05 17:20:02 +000015#include "UpgradeInternals.h"
Reid Spencer950bf602007-01-26 08:19:09 +000016#include "llvm/CallingConv.h"
17#include "llvm/InlineAsm.h"
18#include "llvm/Instructions.h"
19#include "llvm/Module.h"
Reid Spencer7b5d4662007-04-09 06:16:21 +000020#include "llvm/ParameterAttributes.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000021#include "llvm/ValueSymbolTable.h"
Reid Spencer950bf602007-01-26 08:19:09 +000022#include "llvm/Support/GetElementPtrTypeIterator.h"
23#include "llvm/ADT/STLExtras.h"
24#include "llvm/Support/MathExtras.h"
Reid Spencere7c3c602006-11-30 06:36:44 +000025#include <algorithm>
Reid Spencere7c3c602006-11-30 06:36:44 +000026#include <iostream>
Chris Lattner8adde282007-02-11 21:40:10 +000027#include <map>
Reid Spencer950bf602007-01-26 08:19:09 +000028#include <list>
29#include <utility>
30
31// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
32// relating to upreferences in the input stream.
33//
34//#define DEBUG_UPREFS 1
35#ifdef DEBUG_UPREFS
36#define UR_OUT(X) std::cerr << X
37#else
38#define UR_OUT(X)
39#endif
Reid Spencere7c3c602006-11-30 06:36:44 +000040
Reid Spencere77e35e2006-12-01 20:26:20 +000041#define YYERROR_VERBOSE 1
Reid Spencer96839be2006-11-30 16:50:26 +000042#define YYINCLUDED_STDLIB_H
Reid Spencere77e35e2006-12-01 20:26:20 +000043#define YYDEBUG 1
Reid Spencere7c3c602006-11-30 06:36:44 +000044
Reid Spencer950bf602007-01-26 08:19:09 +000045int yylex();
Reid Spencere7c3c602006-11-30 06:36:44 +000046int yyparse();
47
Reid Spencer950bf602007-01-26 08:19:09 +000048int yyerror(const char*);
49static void warning(const std::string& WarningMsg);
50
51namespace llvm {
52
Reid Spencer950bf602007-01-26 08:19:09 +000053std::istream* LexInput;
Reid Spencere7c3c602006-11-30 06:36:44 +000054static std::string CurFilename;
Reid Spencer96839be2006-11-30 16:50:26 +000055
Reid Spencer71d2ec92006-12-31 06:02:26 +000056// This bool controls whether attributes are ever added to function declarations
57// definitions and calls.
58static bool AddAttributes = false;
59
Reid Spencer950bf602007-01-26 08:19:09 +000060static Module *ParserResult;
61static bool ObsoleteVarArgs;
62static bool NewVarArgs;
63static BasicBlock *CurBB;
64static GlobalVariable *CurGV;
Reid Spencer7eea8ff2007-05-18 05:48:07 +000065static unsigned lastCallingConv;
Reid Spencera50d5962006-12-02 04:11:07 +000066
Reid Spencer950bf602007-01-26 08:19:09 +000067// This contains info used when building the body of a function. It is
68// destroyed when the function is completed.
69//
70typedef std::vector<Value *> ValueList; // Numbered defs
71
Reid Spencerbb1fd572007-03-21 17:15:50 +000072typedef std::pair<std::string,TypeInfo> RenameMapKey;
Reid Spencer950bf602007-01-26 08:19:09 +000073typedef std::map<RenameMapKey,std::string> RenameMapType;
74
75static void
76ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
77 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
78
79static struct PerModuleInfo {
80 Module *CurrentModule;
81 std::map<const Type *, ValueList> Values; // Module level numbered definitions
82 std::map<const Type *,ValueList> LateResolveValues;
Reid Spencerbb1fd572007-03-21 17:15:50 +000083 std::vector<PATypeHolder> Types;
84 std::vector<Signedness> TypeSigns;
85 std::map<std::string,Signedness> NamedTypeSigns;
86 std::map<std::string,Signedness> NamedValueSigns;
Reid Spencer950bf602007-01-26 08:19:09 +000087 std::map<ValID, PATypeHolder> LateResolveTypes;
88 static Module::Endianness Endian;
89 static Module::PointerSize PointerSize;
90 RenameMapType RenameMap;
91
92 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
93 /// how they were referenced and on which line of the input they came from so
94 /// that we can resolve them later and print error messages as appropriate.
95 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
96
97 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
98 // references to global values. Global values may be referenced before they
99 // are defined, and if so, the temporary object that they represent is held
100 // here. This is used for forward references of GlobalValues.
101 //
102 typedef std::map<std::pair<const PointerType *, ValID>, GlobalValue*>
103 GlobalRefsType;
104 GlobalRefsType GlobalRefs;
105
106 void ModuleDone() {
107 // If we could not resolve some functions at function compilation time
108 // (calls to functions before they are defined), resolve them now... Types
109 // are resolved when the constant pool has been completely parsed.
110 //
111 ResolveDefinitions(LateResolveValues);
112
113 // Check to make sure that all global value forward references have been
114 // resolved!
115 //
116 if (!GlobalRefs.empty()) {
117 std::string UndefinedReferences = "Unresolved global references exist:\n";
118
119 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
120 I != E; ++I) {
121 UndefinedReferences += " " + I->first.first->getDescription() + " " +
122 I->first.second.getName() + "\n";
123 }
124 error(UndefinedReferences);
125 return;
126 }
127
128 if (CurrentModule->getDataLayout().empty()) {
129 std::string dataLayout;
130 if (Endian != Module::AnyEndianness)
131 dataLayout.append(Endian == Module::BigEndian ? "E" : "e");
132 if (PointerSize != Module::AnyPointerSize) {
133 if (!dataLayout.empty())
134 dataLayout += "-";
135 dataLayout.append(PointerSize == Module::Pointer64 ?
136 "p:64:64" : "p:32:32");
137 }
138 CurrentModule->setDataLayout(dataLayout);
139 }
140
141 Values.clear(); // Clear out function local definitions
142 Types.clear();
Reid Spencerbb1fd572007-03-21 17:15:50 +0000143 TypeSigns.clear();
144 NamedTypeSigns.clear();
145 NamedValueSigns.clear();
Reid Spencer950bf602007-01-26 08:19:09 +0000146 CurrentModule = 0;
147 }
148
149 // GetForwardRefForGlobal - Check to see if there is a forward reference
150 // for this global. If so, remove it from the GlobalRefs map and return it.
151 // If not, just return null.
152 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
153 // Check to see if there is a forward reference to this global variable...
154 // if there is, eliminate it and patch the reference to use the new def'n.
155 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
156 GlobalValue *Ret = 0;
157 if (I != GlobalRefs.end()) {
158 Ret = I->second;
159 GlobalRefs.erase(I);
160 }
161 return Ret;
162 }
163 void setEndianness(Module::Endianness E) { Endian = E; }
164 void setPointerSize(Module::PointerSize sz) { PointerSize = sz; }
165} CurModule;
166
167Module::Endianness PerModuleInfo::Endian = Module::AnyEndianness;
168Module::PointerSize PerModuleInfo::PointerSize = Module::AnyPointerSize;
169
170static struct PerFunctionInfo {
171 Function *CurrentFunction; // Pointer to current function being created
172
173 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
174 std::map<const Type*, ValueList> LateResolveValues;
175 bool isDeclare; // Is this function a forward declararation?
176 GlobalValue::LinkageTypes Linkage;// Linkage for forward declaration.
177
178 /// BBForwardRefs - When we see forward references to basic blocks, keep
179 /// track of them here.
180 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
181 std::vector<BasicBlock*> NumberedBlocks;
182 RenameMapType RenameMap;
Reid Spencer950bf602007-01-26 08:19:09 +0000183 unsigned NextBBNum;
184
185 inline PerFunctionInfo() {
186 CurrentFunction = 0;
187 isDeclare = false;
188 Linkage = GlobalValue::ExternalLinkage;
189 }
190
191 inline void FunctionStart(Function *M) {
192 CurrentFunction = M;
193 NextBBNum = 0;
194 }
195
196 void FunctionDone() {
197 NumberedBlocks.clear();
198
199 // Any forward referenced blocks left?
200 if (!BBForwardRefs.empty()) {
201 error("Undefined reference to label " +
202 BBForwardRefs.begin()->first->getName());
203 return;
204 }
205
206 // Resolve all forward references now.
207 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
208
209 Values.clear(); // Clear out function local definitions
210 RenameMap.clear();
Reid Spencer950bf602007-01-26 08:19:09 +0000211 CurrentFunction = 0;
212 isDeclare = false;
213 Linkage = GlobalValue::ExternalLinkage;
214 }
215} CurFun; // Info for the current function...
216
217static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
218
Reid Spencerbb1fd572007-03-21 17:15:50 +0000219/// This function is just a utility to make a Key value for the rename map.
220/// The Key is a combination of the name, type, Signedness of the original
221/// value (global/function). This just constructs the key and ensures that
222/// named Signedness values are resolved to the actual Signedness.
223/// @brief Make a key for the RenameMaps
224static RenameMapKey makeRenameMapKey(const std::string &Name, const Type* Ty,
225 const Signedness &Sign) {
226 TypeInfo TI;
227 TI.T = Ty;
228 if (Sign.isNamed())
229 // Don't allow Named Signedness nodes because they won't match. The actual
230 // Signedness must be looked up in the NamedTypeSigns map.
231 TI.S.copy(CurModule.NamedTypeSigns[Sign.getName()]);
232 else
233 TI.S.copy(Sign);
234 return std::make_pair(Name, TI);
235}
236
Reid Spencer950bf602007-01-26 08:19:09 +0000237
238//===----------------------------------------------------------------------===//
239// Code to handle definitions of all the types
240//===----------------------------------------------------------------------===//
241
242static int InsertValue(Value *V,
243 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
244 if (V->hasName()) return -1; // Is this a numbered definition?
245
246 // Yes, insert the value into the value table...
247 ValueList &List = ValueTab[V->getType()];
248 List.push_back(V);
249 return List.size()-1;
250}
251
Reid Spencerd7c4f8c2007-01-26 19:59:25 +0000252static const Type *getType(const ValID &D, bool DoNotImprovise = false) {
Reid Spencer950bf602007-01-26 08:19:09 +0000253 switch (D.Type) {
254 case ValID::NumberVal: // Is it a numbered definition?
255 // Module constants occupy the lowest numbered slots...
256 if ((unsigned)D.Num < CurModule.Types.size()) {
257 return CurModule.Types[(unsigned)D.Num];
258 }
259 break;
260 case ValID::NameVal: // Is it a named definition?
261 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
Reid Spencer950bf602007-01-26 08:19:09 +0000262 return N;
263 }
264 break;
265 default:
266 error("Internal parser error: Invalid symbol type reference");
267 return 0;
268 }
269
270 // If we reached here, we referenced either a symbol that we don't know about
271 // or an id number that hasn't been read yet. We may be referencing something
272 // forward, so just create an entry to be resolved later and get to it...
273 //
274 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
275
Reid Spencer950bf602007-01-26 08:19:09 +0000276 if (inFunctionScope()) {
277 if (D.Type == ValID::NameVal) {
278 error("Reference to an undefined type: '" + D.getName() + "'");
279 return 0;
280 } else {
281 error("Reference to an undefined type: #" + itostr(D.Num));
282 return 0;
283 }
284 }
285
286 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
287 if (I != CurModule.LateResolveTypes.end())
288 return I->second;
289
290 Type *Typ = OpaqueType::get();
291 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
292 return Typ;
Reid Spencerbb1fd572007-03-21 17:15:50 +0000293}
294
295/// This is like the getType method except that instead of looking up the type
296/// for a given ID, it looks up that type's sign.
297/// @brief Get the signedness of a referenced type
298static Signedness getTypeSign(const ValID &D) {
299 switch (D.Type) {
300 case ValID::NumberVal: // Is it a numbered definition?
301 // Module constants occupy the lowest numbered slots...
302 if ((unsigned)D.Num < CurModule.TypeSigns.size()) {
303 return CurModule.TypeSigns[(unsigned)D.Num];
304 }
305 break;
306 case ValID::NameVal: { // Is it a named definition?
307 std::map<std::string,Signedness>::const_iterator I =
308 CurModule.NamedTypeSigns.find(D.Name);
309 if (I != CurModule.NamedTypeSigns.end())
310 return I->second;
311 // Perhaps its a named forward .. just cache the name
312 Signedness S;
313 S.makeNamed(D.Name);
314 return S;
315 }
316 default:
317 break;
318 }
319 // If we don't find it, its signless
320 Signedness S;
321 S.makeSignless();
322 return S;
323}
324
325/// This function is analagous to getElementType in LLVM. It provides the same
326/// function except that it looks up the Signedness instead of the type. This is
327/// used when processing GEP instructions that need to extract the type of an
328/// indexed struct/array/ptr member.
329/// @brief Look up an element's sign.
330static Signedness getElementSign(const ValueInfo& VI,
331 const std::vector<Value*> &Indices) {
332 const Type *Ptr = VI.V->getType();
333 assert(isa<PointerType>(Ptr) && "Need pointer type");
334
335 unsigned CurIdx = 0;
336 Signedness S(VI.S);
337 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
338 if (CurIdx == Indices.size())
339 break;
340
341 Value *Index = Indices[CurIdx++];
342 assert(!isa<PointerType>(CT) || CurIdx == 1 && "Invalid type");
343 Ptr = CT->getTypeAtIndex(Index);
344 if (const Type* Ty = Ptr->getForwardedType())
345 Ptr = Ty;
346 assert(S.isComposite() && "Bad Signedness type");
347 if (isa<StructType>(CT)) {
348 S = S.get(cast<ConstantInt>(Index)->getZExtValue());
349 } else {
350 S = S.get(0UL);
351 }
352 if (S.isNamed())
353 S = CurModule.NamedTypeSigns[S.getName()];
354 }
355 Signedness Result;
356 Result.makeComposite(S);
357 return Result;
358}
359
360/// This function just translates a ConstantInfo into a ValueInfo and calls
361/// getElementSign(ValueInfo,...). Its just a convenience.
362/// @brief ConstantInfo version of getElementSign.
363static Signedness getElementSign(const ConstInfo& CI,
364 const std::vector<Constant*> &Indices) {
365 ValueInfo VI;
366 VI.V = CI.C;
367 VI.S.copy(CI.S);
368 std::vector<Value*> Idx;
369 for (unsigned i = 0; i < Indices.size(); ++i)
370 Idx.push_back(Indices[i]);
371 Signedness result = getElementSign(VI, Idx);
372 VI.destroy();
373 return result;
374}
Reid Spencer950bf602007-01-26 08:19:09 +0000375
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
832 const PointerType *PTy = PointerType::get(Ty);
833
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
Reid Spencerbb1fd572007-03-21 17:15:50 +0000886 RenameMapKey Key = makeRenameMapKey(Name, PointerType::get(Ty), ID.S);
Reid Spencer950bf602007-01-26 08:19:09 +0000887 CurModule.RenameMap[Key] = NewName;
888
889 // Rename it
890 Name = NewName;
891 }
892 }
893
894 // Otherwise there is no existing GV to use, create one now.
895 GlobalVariable *GV =
896 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
897 CurModule.CurrentModule);
898 InsertValue(GV, CurModule.Values);
Reid Spencerbb1fd572007-03-21 17:15:50 +0000899 // Remember the sign of this global.
900 CurModule.NamedValueSigns[Name] = ID.S;
Reid Spencer950bf602007-01-26 08:19:09 +0000901 return GV;
902}
903
904// setTypeName - Set the specified type to the name given. The name may be
905// null potentially, in which case this is a noop. The string passed in is
906// assumed to be a malloc'd string buffer, and is freed by this function.
907//
908// This function returns true if the type has already been defined, but is
909// allowed to be redefined in the specified context. If the name is a new name
910// for the type plane, it is inserted and false is returned.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000911static bool setTypeName(const PATypeInfo& TI, char *NameStr) {
Reid Spencer950bf602007-01-26 08:19:09 +0000912 assert(!inFunctionScope() && "Can't give types function-local names");
913 if (NameStr == 0) return false;
914
915 std::string Name(NameStr); // Copy string
916 free(NameStr); // Free old string
917
Reid Spencerbb1fd572007-03-21 17:15:50 +0000918 const Type* Ty = TI.PAT->get();
919
Reid Spencer950bf602007-01-26 08:19:09 +0000920 // We don't allow assigning names to void type
Reid Spencerbb1fd572007-03-21 17:15:50 +0000921 if (Ty == Type::VoidTy) {
Reid Spencer950bf602007-01-26 08:19:09 +0000922 error("Can't assign name '" + Name + "' to the void type");
923 return false;
924 }
925
926 // Set the type name, checking for conflicts as we do so.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000927 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, Ty);
928
929 // Save the sign information for later use
930 CurModule.NamedTypeSigns[Name] = TI.S;
Reid Spencer950bf602007-01-26 08:19:09 +0000931
932 if (AlreadyExists) { // Inserting a name that is already defined???
933 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
934 assert(Existing && "Conflict but no matching type?");
935
936 // There is only one case where this is allowed: when we are refining an
937 // opaque type. In this case, Existing will be an opaque type.
938 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
939 // We ARE replacing an opaque type!
Reid Spencerbb1fd572007-03-21 17:15:50 +0000940 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(Ty);
Reid Spencer950bf602007-01-26 08:19:09 +0000941 return true;
942 }
943
944 // Otherwise, this is an attempt to redefine a type. That's okay if
945 // the redefinition is identical to the original. This will be so if
946 // Existing and T point to the same Type object. In this one case we
947 // allow the equivalent redefinition.
Reid Spencerbb1fd572007-03-21 17:15:50 +0000948 if (Existing == Ty) return true; // Yes, it's equal.
Reid Spencer950bf602007-01-26 08:19:09 +0000949
950 // Any other kind of (non-equivalent) redefinition is an error.
951 error("Redefinition of type named '" + Name + "' in the '" +
Reid Spencerbb1fd572007-03-21 17:15:50 +0000952 Ty->getDescription() + "' type plane");
Reid Spencer950bf602007-01-26 08:19:09 +0000953 }
954
955 return false;
956}
957
958//===----------------------------------------------------------------------===//
959// Code for handling upreferences in type names...
960//
961
962// TypeContains - Returns true if Ty directly contains E in it.
963//
964static bool TypeContains(const Type *Ty, const Type *E) {
965 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
966 E) != Ty->subtype_end();
967}
968
969namespace {
970 struct UpRefRecord {
971 // NestingLevel - The number of nesting levels that need to be popped before
972 // this type is resolved.
973 unsigned NestingLevel;
974
975 // LastContainedTy - This is the type at the current binding level for the
976 // type. Every time we reduce the nesting level, this gets updated.
977 const Type *LastContainedTy;
978
979 // UpRefTy - This is the actual opaque type that the upreference is
980 // represented with.
981 OpaqueType *UpRefTy;
982
983 UpRefRecord(unsigned NL, OpaqueType *URTy)
Reid Spencerbb1fd572007-03-21 17:15:50 +0000984 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) { }
Reid Spencer950bf602007-01-26 08:19:09 +0000985 };
986}
987
988// UpRefs - A list of the outstanding upreferences that need to be resolved.
989static std::vector<UpRefRecord> UpRefs;
990
991/// HandleUpRefs - Every time we finish a new layer of types, this function is
992/// called. It loops through the UpRefs vector, which is a list of the
993/// currently active types. For each type, if the up reference is contained in
994/// the newly completed type, we decrement the level count. When the level
995/// count reaches zero, the upreferenced type is the type that is passed in:
996/// thus we can complete the cycle.
997///
Reid Spencerbb1fd572007-03-21 17:15:50 +0000998static PATypeHolder HandleUpRefs(const Type *ty, const Signedness& Sign) {
Reid Spencer950bf602007-01-26 08:19:09 +0000999 // If Ty isn't abstract, or if there are no up-references in it, then there is
1000 // nothing to resolve here.
1001 if (!ty->isAbstract() || UpRefs.empty()) return ty;
1002
1003 PATypeHolder Ty(ty);
1004 UR_OUT("Type '" << Ty->getDescription() <<
1005 "' newly formed. Resolving upreferences.\n" <<
1006 UpRefs.size() << " upreferences active!\n");
1007
1008 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
1009 // to zero), we resolve them all together before we resolve them to Ty. At
1010 // the end of the loop, if there is anything to resolve to Ty, it will be in
1011 // this variable.
1012 OpaqueType *TypeToResolve = 0;
1013
Reid Spencerbb1fd572007-03-21 17:15:50 +00001014 unsigned i = 0;
1015 for (; i != UpRefs.size(); ++i) {
Reid Spencer950bf602007-01-26 08:19:09 +00001016 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Reid Spencerbb1fd572007-03-21 17:15:50 +00001017 << UpRefs[i].UpRefTy->getDescription() << ") = "
1018 << (TypeContains(Ty, UpRefs[i].UpRefTy) ? "true" : "false") << "\n");
Reid Spencer950bf602007-01-26 08:19:09 +00001019 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
1020 // Decrement level of upreference
1021 unsigned Level = --UpRefs[i].NestingLevel;
1022 UpRefs[i].LastContainedTy = Ty;
1023 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
1024 if (Level == 0) { // Upreference should be resolved!
1025 if (!TypeToResolve) {
1026 TypeToResolve = UpRefs[i].UpRefTy;
1027 } else {
1028 UR_OUT(" * Resolving upreference for "
Reid Spencerbb1fd572007-03-21 17:15:50 +00001029 << UpRefs[i].UpRefTy->getDescription() << "\n";
1030 std::string OldName = UpRefs[i].UpRefTy->getDescription());
1031 ResolveTypeSign(UpRefs[i].UpRefTy, Sign);
Reid Spencer950bf602007-01-26 08:19:09 +00001032 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
1033 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
1034 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
1035 }
1036 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
1037 --i; // Do not skip the next element...
1038 }
1039 }
1040 }
1041
1042 if (TypeToResolve) {
1043 UR_OUT(" * Resolving upreference for "
Reid Spencerbb1fd572007-03-21 17:15:50 +00001044 << UpRefs[i].UpRefTy->getDescription() << "\n";
Reid Spencer950bf602007-01-26 08:19:09 +00001045 std::string OldName = TypeToResolve->getDescription());
Reid Spencerbb1fd572007-03-21 17:15:50 +00001046 ResolveTypeSign(TypeToResolve, Sign);
Reid Spencer950bf602007-01-26 08:19:09 +00001047 TypeToResolve->refineAbstractTypeTo(Ty);
1048 }
1049
1050 return Ty;
1051}
1052
Reid Spencerbb1fd572007-03-21 17:15:50 +00001053bool Signedness::operator<(const Signedness &that) const {
1054 if (isNamed()) {
1055 if (that.isNamed())
1056 return *(this->name) < *(that.name);
1057 else
1058 return CurModule.NamedTypeSigns[*name] < that;
1059 } else if (that.isNamed()) {
1060 return *this < CurModule.NamedTypeSigns[*that.name];
1061 }
1062
1063 if (isComposite() && that.isComposite()) {
1064 if (sv->size() == that.sv->size()) {
1065 SignVector::const_iterator thisI = sv->begin(), thisE = sv->end();
1066 SignVector::const_iterator thatI = that.sv->begin(),
1067 thatE = that.sv->end();
1068 for (; thisI != thisE; ++thisI, ++thatI) {
1069 if (*thisI < *thatI)
1070 return true;
1071 else if (!(*thisI == *thatI))
1072 return false;
1073 }
1074 return false;
1075 }
1076 return sv->size() < that.sv->size();
1077 }
1078 return kind < that.kind;
1079}
1080
1081bool Signedness::operator==(const Signedness &that) const {
1082 if (isNamed())
1083 if (that.isNamed())
1084 return *(this->name) == *(that.name);
1085 else
1086 return CurModule.NamedTypeSigns[*(this->name)] == that;
1087 else if (that.isNamed())
1088 return *this == CurModule.NamedTypeSigns[*(that.name)];
1089 if (isComposite() && that.isComposite()) {
1090 if (sv->size() == that.sv->size()) {
1091 SignVector::const_iterator thisI = sv->begin(), thisE = sv->end();
1092 SignVector::const_iterator thatI = that.sv->begin(),
1093 thatE = that.sv->end();
1094 for (; thisI != thisE; ++thisI, ++thatI) {
1095 if (!(*thisI == *thatI))
1096 return false;
1097 }
1098 return true;
1099 }
1100 return false;
1101 }
1102 return kind == that.kind;
1103}
1104
1105void Signedness::copy(const Signedness &that) {
1106 if (that.isNamed()) {
1107 kind = Named;
1108 name = new std::string(*that.name);
1109 } else if (that.isComposite()) {
1110 kind = Composite;
1111 sv = new SignVector();
1112 *sv = *that.sv;
1113 } else {
1114 kind = that.kind;
1115 sv = 0;
1116 }
1117}
1118
1119void Signedness::destroy() {
1120 if (isNamed()) {
1121 delete name;
1122 } else if (isComposite()) {
1123 delete sv;
1124 }
1125}
1126
Evan Cheng2b484202007-03-22 07:43:51 +00001127#ifndef NDEBUG
Reid Spencerbb1fd572007-03-21 17:15:50 +00001128void Signedness::dump() const {
1129 if (isComposite()) {
1130 if (sv->size() == 1) {
1131 (*sv)[0].dump();
1132 std::cerr << "*";
1133 } else {
1134 std::cerr << "{ " ;
1135 for (unsigned i = 0; i < sv->size(); ++i) {
1136 if (i != 0)
1137 std::cerr << ", ";
1138 (*sv)[i].dump();
1139 }
1140 std::cerr << "} " ;
1141 }
1142 } else if (isNamed()) {
1143 std::cerr << *name;
1144 } else if (isSigned()) {
1145 std::cerr << "S";
1146 } else if (isUnsigned()) {
1147 std::cerr << "U";
1148 } else
1149 std::cerr << ".";
1150}
Evan Cheng2b484202007-03-22 07:43:51 +00001151#endif
Reid Spencerbb1fd572007-03-21 17:15:50 +00001152
Reid Spencer950bf602007-01-26 08:19:09 +00001153static inline Instruction::TermOps
1154getTermOp(TermOps op) {
1155 switch (op) {
1156 default : assert(0 && "Invalid OldTermOp");
1157 case RetOp : return Instruction::Ret;
1158 case BrOp : return Instruction::Br;
1159 case SwitchOp : return Instruction::Switch;
1160 case InvokeOp : return Instruction::Invoke;
1161 case UnwindOp : return Instruction::Unwind;
1162 case UnreachableOp: return Instruction::Unreachable;
1163 }
1164}
1165
1166static inline Instruction::BinaryOps
Reid Spencerbb1fd572007-03-21 17:15:50 +00001167getBinaryOp(BinaryOps op, const Type *Ty, const Signedness& Sign) {
Reid Spencer950bf602007-01-26 08:19:09 +00001168 switch (op) {
1169 default : assert(0 && "Invalid OldBinaryOps");
1170 case SetEQ :
1171 case SetNE :
1172 case SetLE :
1173 case SetGE :
1174 case SetLT :
1175 case SetGT : assert(0 && "Should use getCompareOp");
1176 case AddOp : return Instruction::Add;
1177 case SubOp : return Instruction::Sub;
1178 case MulOp : return Instruction::Mul;
1179 case DivOp : {
1180 // This is an obsolete instruction so we must upgrade it based on the
1181 // types of its operands.
1182 bool isFP = Ty->isFloatingPoint();
Reid Spencer9d6565a2007-02-15 02:26:10 +00001183 if (const VectorType* PTy = dyn_cast<VectorType>(Ty))
Chris Lattner4227bdb2007-02-19 07:34:02 +00001184 // If its a vector type we want to use the element type
Reid Spencer950bf602007-01-26 08:19:09 +00001185 isFP = PTy->getElementType()->isFloatingPoint();
1186 if (isFP)
1187 return Instruction::FDiv;
Reid Spencerbb1fd572007-03-21 17:15:50 +00001188 else if (Sign.isSigned())
Reid Spencer950bf602007-01-26 08:19:09 +00001189 return Instruction::SDiv;
1190 return Instruction::UDiv;
1191 }
1192 case UDivOp : return Instruction::UDiv;
1193 case SDivOp : return Instruction::SDiv;
1194 case FDivOp : return Instruction::FDiv;
1195 case RemOp : {
1196 // This is an obsolete instruction so we must upgrade it based on the
1197 // types of its operands.
1198 bool isFP = Ty->isFloatingPoint();
Reid Spencer9d6565a2007-02-15 02:26:10 +00001199 if (const VectorType* PTy = dyn_cast<VectorType>(Ty))
Chris Lattner4227bdb2007-02-19 07:34:02 +00001200 // If its a vector type we want to use the element type
Reid Spencer950bf602007-01-26 08:19:09 +00001201 isFP = PTy->getElementType()->isFloatingPoint();
1202 // Select correct opcode
1203 if (isFP)
1204 return Instruction::FRem;
Reid Spencerbb1fd572007-03-21 17:15:50 +00001205 else if (Sign.isSigned())
Reid Spencer950bf602007-01-26 08:19:09 +00001206 return Instruction::SRem;
1207 return Instruction::URem;
1208 }
1209 case URemOp : return Instruction::URem;
1210 case SRemOp : return Instruction::SRem;
1211 case FRemOp : return Instruction::FRem;
Reid Spencer832254e2007-02-02 02:16:23 +00001212 case LShrOp : return Instruction::LShr;
1213 case AShrOp : return Instruction::AShr;
1214 case ShlOp : return Instruction::Shl;
1215 case ShrOp :
Reid Spencerbb1fd572007-03-21 17:15:50 +00001216 if (Sign.isSigned())
Reid Spencer832254e2007-02-02 02:16:23 +00001217 return Instruction::AShr;
1218 return Instruction::LShr;
Reid Spencer950bf602007-01-26 08:19:09 +00001219 case AndOp : return Instruction::And;
1220 case OrOp : return Instruction::Or;
1221 case XorOp : return Instruction::Xor;
1222 }
1223}
1224
1225static inline Instruction::OtherOps
1226getCompareOp(BinaryOps op, unsigned short &predicate, const Type* &Ty,
Reid Spencerbb1fd572007-03-21 17:15:50 +00001227 const Signedness &Sign) {
1228 bool isSigned = Sign.isSigned();
Reid Spencer950bf602007-01-26 08:19:09 +00001229 bool isFP = Ty->isFloatingPoint();
1230 switch (op) {
1231 default : assert(0 && "Invalid OldSetCC");
1232 case SetEQ :
1233 if (isFP) {
1234 predicate = FCmpInst::FCMP_OEQ;
1235 return Instruction::FCmp;
1236 } else {
1237 predicate = ICmpInst::ICMP_EQ;
1238 return Instruction::ICmp;
1239 }
1240 case SetNE :
1241 if (isFP) {
1242 predicate = FCmpInst::FCMP_UNE;
1243 return Instruction::FCmp;
1244 } else {
1245 predicate = ICmpInst::ICMP_NE;
1246 return Instruction::ICmp;
1247 }
1248 case SetLE :
1249 if (isFP) {
1250 predicate = FCmpInst::FCMP_OLE;
1251 return Instruction::FCmp;
1252 } else {
1253 if (isSigned)
1254 predicate = ICmpInst::ICMP_SLE;
1255 else
1256 predicate = ICmpInst::ICMP_ULE;
1257 return Instruction::ICmp;
1258 }
1259 case SetGE :
1260 if (isFP) {
1261 predicate = FCmpInst::FCMP_OGE;
1262 return Instruction::FCmp;
1263 } else {
1264 if (isSigned)
1265 predicate = ICmpInst::ICMP_SGE;
1266 else
1267 predicate = ICmpInst::ICMP_UGE;
1268 return Instruction::ICmp;
1269 }
1270 case SetLT :
1271 if (isFP) {
1272 predicate = FCmpInst::FCMP_OLT;
1273 return Instruction::FCmp;
1274 } else {
1275 if (isSigned)
1276 predicate = ICmpInst::ICMP_SLT;
1277 else
1278 predicate = ICmpInst::ICMP_ULT;
1279 return Instruction::ICmp;
1280 }
1281 case SetGT :
1282 if (isFP) {
1283 predicate = FCmpInst::FCMP_OGT;
1284 return Instruction::FCmp;
1285 } else {
1286 if (isSigned)
1287 predicate = ICmpInst::ICMP_SGT;
1288 else
1289 predicate = ICmpInst::ICMP_UGT;
1290 return Instruction::ICmp;
1291 }
1292 }
1293}
1294
1295static inline Instruction::MemoryOps getMemoryOp(MemoryOps op) {
1296 switch (op) {
1297 default : assert(0 && "Invalid OldMemoryOps");
1298 case MallocOp : return Instruction::Malloc;
1299 case FreeOp : return Instruction::Free;
1300 case AllocaOp : return Instruction::Alloca;
1301 case LoadOp : return Instruction::Load;
1302 case StoreOp : return Instruction::Store;
1303 case GetElementPtrOp : return Instruction::GetElementPtr;
1304 }
1305}
1306
1307static inline Instruction::OtherOps
Reid Spencerbb1fd572007-03-21 17:15:50 +00001308getOtherOp(OtherOps op, const Signedness &Sign) {
Reid Spencer950bf602007-01-26 08:19:09 +00001309 switch (op) {
1310 default : assert(0 && "Invalid OldOtherOps");
1311 case PHIOp : return Instruction::PHI;
1312 case CallOp : return Instruction::Call;
Reid Spencer950bf602007-01-26 08:19:09 +00001313 case SelectOp : return Instruction::Select;
1314 case UserOp1 : return Instruction::UserOp1;
1315 case UserOp2 : return Instruction::UserOp2;
1316 case VAArg : return Instruction::VAArg;
1317 case ExtractElementOp : return Instruction::ExtractElement;
1318 case InsertElementOp : return Instruction::InsertElement;
1319 case ShuffleVectorOp : return Instruction::ShuffleVector;
1320 case ICmpOp : return Instruction::ICmp;
1321 case FCmpOp : return Instruction::FCmp;
Reid Spencer950bf602007-01-26 08:19:09 +00001322 };
1323}
1324
1325static inline Value*
Reid Spencerbb1fd572007-03-21 17:15:50 +00001326getCast(CastOps op, Value *Src, const Signedness &SrcSign, const Type *DstTy,
1327 const Signedness &DstSign, bool ForceInstruction = false) {
Reid Spencer950bf602007-01-26 08:19:09 +00001328 Instruction::CastOps Opcode;
1329 const Type* SrcTy = Src->getType();
1330 if (op == CastOp) {
1331 if (SrcTy->isFloatingPoint() && isa<PointerType>(DstTy)) {
1332 // fp -> ptr cast is no longer supported but we must upgrade this
1333 // by doing a double cast: fp -> int -> ptr
1334 SrcTy = Type::Int64Ty;
1335 Opcode = Instruction::IntToPtr;
1336 if (isa<Constant>(Src)) {
1337 Src = ConstantExpr::getCast(Instruction::FPToUI,
1338 cast<Constant>(Src), SrcTy);
1339 } else {
1340 std::string NewName(makeNameUnique(Src->getName()));
1341 Src = new FPToUIInst(Src, SrcTy, NewName, CurBB);
1342 }
1343 } else if (isa<IntegerType>(DstTy) &&
1344 cast<IntegerType>(DstTy)->getBitWidth() == 1) {
1345 // cast type %x to bool was previously defined as setne type %x, null
1346 // The cast semantic is now to truncate, not compare so we must retain
1347 // the original intent by replacing the cast with a setne
1348 Constant* Null = Constant::getNullValue(SrcTy);
1349 Instruction::OtherOps Opcode = Instruction::ICmp;
1350 unsigned short predicate = ICmpInst::ICMP_NE;
1351 if (SrcTy->isFloatingPoint()) {
1352 Opcode = Instruction::FCmp;
1353 predicate = FCmpInst::FCMP_ONE;
1354 } else if (!SrcTy->isInteger() && !isa<PointerType>(SrcTy)) {
1355 error("Invalid cast to bool");
1356 }
1357 if (isa<Constant>(Src) && !ForceInstruction)
1358 return ConstantExpr::getCompare(predicate, cast<Constant>(Src), Null);
1359 else
1360 return CmpInst::create(Opcode, predicate, Src, Null);
1361 }
1362 // Determine the opcode to use by calling CastInst::getCastOpcode
1363 Opcode =
Reid Spencerbb1fd572007-03-21 17:15:50 +00001364 CastInst::getCastOpcode(Src, SrcSign.isSigned(), DstTy,
1365 DstSign.isSigned());
Reid Spencer950bf602007-01-26 08:19:09 +00001366
1367 } else switch (op) {
1368 default: assert(0 && "Invalid cast token");
1369 case TruncOp: Opcode = Instruction::Trunc; break;
1370 case ZExtOp: Opcode = Instruction::ZExt; break;
1371 case SExtOp: Opcode = Instruction::SExt; break;
1372 case FPTruncOp: Opcode = Instruction::FPTrunc; break;
1373 case FPExtOp: Opcode = Instruction::FPExt; break;
1374 case FPToUIOp: Opcode = Instruction::FPToUI; break;
1375 case FPToSIOp: Opcode = Instruction::FPToSI; break;
1376 case UIToFPOp: Opcode = Instruction::UIToFP; break;
1377 case SIToFPOp: Opcode = Instruction::SIToFP; break;
1378 case PtrToIntOp: Opcode = Instruction::PtrToInt; break;
1379 case IntToPtrOp: Opcode = Instruction::IntToPtr; break;
1380 case BitCastOp: Opcode = Instruction::BitCast; break;
1381 }
1382
1383 if (isa<Constant>(Src) && !ForceInstruction)
1384 return ConstantExpr::getCast(Opcode, cast<Constant>(Src), DstTy);
1385 return CastInst::create(Opcode, Src, DstTy);
1386}
1387
1388static Instruction *
1389upgradeIntrinsicCall(const Type* RetTy, const ValID &ID,
1390 std::vector<Value*>& Args) {
1391
1392 std::string Name = ID.Type == ValID::NameVal ? ID.Name : "";
Reid Spencer7eea8ff2007-05-18 05:48:07 +00001393 if (Name.length() <= 5 || Name[0] != 'l' || Name[1] != 'l' ||
1394 Name[2] != 'v' || Name[3] != 'm' || Name[4] != '.')
1395 return 0;
1396
Reid Spencer41b213e2007-04-02 01:14:00 +00001397 switch (Name[5]) {
1398 case 'i':
1399 if (Name == "llvm.isunordered.f32" || Name == "llvm.isunordered.f64") {
1400 if (Args.size() != 2)
1401 error("Invalid prototype for " + Name);
1402 return new FCmpInst(FCmpInst::FCMP_UNO, Args[0], Args[1]);
1403 }
1404 break;
Reid Spencer8166a6c2007-04-02 02:08:35 +00001405
Reid Spencer41b213e2007-04-02 01:14:00 +00001406 case 'v' : {
1407 const Type* PtrTy = PointerType::get(Type::Int8Ty);
1408 std::vector<const Type*> Params;
1409 if (Name == "llvm.va_start" || Name == "llvm.va_end") {
1410 if (Args.size() != 1)
1411 error("Invalid prototype for " + Name + " prototype");
1412 Params.push_back(PtrTy);
1413 const FunctionType *FTy =
1414 FunctionType::get(Type::VoidTy, Params, false);
1415 const PointerType *PFTy = PointerType::get(FTy);
1416 Value* Func = getVal(PFTy, ID);
1417 Args[0] = new BitCastInst(Args[0], PtrTy, makeNameUnique("va"), CurBB);
David Greene718fda32007-08-01 03:59:32 +00001418 return new CallInst(Func, Args.begin(), Args.end());
Reid Spencer41b213e2007-04-02 01:14:00 +00001419 } else if (Name == "llvm.va_copy") {
1420 if (Args.size() != 2)
1421 error("Invalid prototype for " + Name + " prototype");
1422 Params.push_back(PtrTy);
1423 Params.push_back(PtrTy);
1424 const FunctionType *FTy =
1425 FunctionType::get(Type::VoidTy, Params, false);
1426 const PointerType *PFTy = PointerType::get(FTy);
1427 Value* Func = getVal(PFTy, ID);
1428 std::string InstName0(makeNameUnique("va0"));
1429 std::string InstName1(makeNameUnique("va1"));
1430 Args[0] = new BitCastInst(Args[0], PtrTy, InstName0, CurBB);
1431 Args[1] = new BitCastInst(Args[1], PtrTy, InstName1, CurBB);
David Greene718fda32007-08-01 03:59:32 +00001432 return new CallInst(Func, Args.begin(), Args.end());
Reid Spencer41b213e2007-04-02 01:14:00 +00001433 }
Reid Spencer950bf602007-01-26 08:19:09 +00001434 }
1435 }
1436 return 0;
1437}
1438
Reid Spencerff0e4482007-04-16 00:40:57 +00001439const Type* upgradeGEPCEIndices(const Type* PTy,
1440 std::vector<ValueInfo> *Indices,
1441 std::vector<Constant*> &Result) {
1442 const Type *Ty = PTy;
1443 Result.clear();
1444 for (unsigned i = 0, e = Indices->size(); i != e ; ++i) {
1445 Constant *Index = cast<Constant>((*Indices)[i].V);
1446
1447 if (ConstantInt *CI = dyn_cast<ConstantInt>(Index)) {
1448 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte
1449 // struct indices to i32 struct indices with ZExt for compatibility.
1450 if (CI->getBitWidth() < 32)
1451 Index = ConstantExpr::getCast(Instruction::ZExt, CI, Type::Int32Ty);
1452 }
1453
1454 if (isa<SequentialType>(Ty)) {
1455 // Make sure that unsigned SequentialType indices are zext'd to
1456 // 64-bits if they were smaller than that because LLVM 2.0 will sext
1457 // all indices for SequentialType elements. We must retain the same
1458 // semantic (zext) for unsigned types.
1459 if (const IntegerType *Ity = dyn_cast<IntegerType>(Index->getType())) {
1460 if (Ity->getBitWidth() < 64 && (*Indices)[i].S.isUnsigned()) {
1461 Index = ConstantExpr::getCast(Instruction::ZExt, Index,Type::Int64Ty);
1462 }
1463 }
1464 }
1465 Result.push_back(Index);
David Greene5fd22a82007-09-04 18:46:50 +00001466 Ty = GetElementPtrInst::getIndexedType(PTy, Result.begin(),
1467 Result.end(),true);
Reid Spencerff0e4482007-04-16 00:40:57 +00001468 if (!Ty)
1469 error("Index list invalid for constant getelementptr");
1470 }
1471 return Ty;
1472}
1473
1474const Type* upgradeGEPInstIndices(const Type* PTy,
1475 std::vector<ValueInfo> *Indices,
1476 std::vector<Value*> &Result) {
1477 const Type *Ty = PTy;
1478 Result.clear();
1479 for (unsigned i = 0, e = Indices->size(); i != e ; ++i) {
1480 Value *Index = (*Indices)[i].V;
1481
1482 if (ConstantInt *CI = dyn_cast<ConstantInt>(Index)) {
1483 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte
1484 // struct indices to i32 struct indices with ZExt for compatibility.
1485 if (CI->getBitWidth() < 32)
1486 Index = ConstantExpr::getCast(Instruction::ZExt, CI, Type::Int32Ty);
1487 }
1488
1489
1490 if (isa<StructType>(Ty)) { // Only change struct indices
1491 if (!isa<Constant>(Index)) {
1492 error("Invalid non-constant structure index");
1493 return 0;
1494 }
Reid Spencer950bf602007-01-26 08:19:09 +00001495 } else {
1496 // Make sure that unsigned SequentialType indices are zext'd to
1497 // 64-bits if they were smaller than that because LLVM 2.0 will sext
1498 // all indices for SequentialType elements. We must retain the same
1499 // semantic (zext) for unsigned types.
Reid Spencerff0e4482007-04-16 00:40:57 +00001500 if (const IntegerType *Ity = dyn_cast<IntegerType>(Index->getType())) {
Reid Spencerbb1fd572007-03-21 17:15:50 +00001501 if (Ity->getBitWidth() < 64 && (*Indices)[i].S.isUnsigned()) {
Reid Spencerff0e4482007-04-16 00:40:57 +00001502 if (isa<Constant>(Index))
Reid Spencer950bf602007-01-26 08:19:09 +00001503 Index = ConstantExpr::getCast(Instruction::ZExt,
1504 cast<Constant>(Index), Type::Int64Ty);
1505 else
1506 Index = CastInst::create(Instruction::ZExt, Index, Type::Int64Ty,
Reid Spencer832254e2007-02-02 02:16:23 +00001507 makeNameUnique("gep"), CurBB);
Reid Spencer38f682b2007-01-26 20:31:18 +00001508 }
Reid Spencerff0e4482007-04-16 00:40:57 +00001509 }
Reid Spencer950bf602007-01-26 08:19:09 +00001510 }
Reid Spencerff0e4482007-04-16 00:40:57 +00001511 Result.push_back(Index);
David Greene5fd22a82007-09-04 18:46:50 +00001512 Ty = GetElementPtrInst::getIndexedType(PTy, Result.begin(),
1513 Result.end(),true);
Reid Spencerff0e4482007-04-16 00:40:57 +00001514 if (!Ty)
Reid Spencer950bf602007-01-26 08:19:09 +00001515 error("Index list invalid for constant getelementptr");
Reid Spencerff0e4482007-04-16 00:40:57 +00001516 }
1517 return Ty;
Reid Spencer950bf602007-01-26 08:19:09 +00001518}
1519
Reid Spencerb7046c72007-01-29 05:41:34 +00001520unsigned upgradeCallingConv(unsigned CC) {
1521 switch (CC) {
1522 case OldCallingConv::C : return CallingConv::C;
1523 case OldCallingConv::CSRet : return CallingConv::C;
1524 case OldCallingConv::Fast : return CallingConv::Fast;
1525 case OldCallingConv::Cold : return CallingConv::Cold;
1526 case OldCallingConv::X86_StdCall : return CallingConv::X86_StdCall;
1527 case OldCallingConv::X86_FastCall: return CallingConv::X86_FastCall;
1528 default:
1529 return CC;
1530 }
1531}
1532
Reid Spencer950bf602007-01-26 08:19:09 +00001533Module* UpgradeAssembly(const std::string &infile, std::istream& in,
1534 bool debug, bool addAttrs)
Reid Spencere7c3c602006-11-30 06:36:44 +00001535{
1536 Upgradelineno = 1;
1537 CurFilename = infile;
Reid Spencer96839be2006-11-30 16:50:26 +00001538 LexInput = &in;
Reid Spencere77e35e2006-12-01 20:26:20 +00001539 yydebug = debug;
Reid Spencer71d2ec92006-12-31 06:02:26 +00001540 AddAttributes = addAttrs;
Reid Spencer950bf602007-01-26 08:19:09 +00001541 ObsoleteVarArgs = false;
1542 NewVarArgs = false;
Reid Spencere7c3c602006-11-30 06:36:44 +00001543
Reid Spencer950bf602007-01-26 08:19:09 +00001544 CurModule.CurrentModule = new Module(CurFilename);
1545
1546 // Check to make sure the parser succeeded
Reid Spencere7c3c602006-11-30 06:36:44 +00001547 if (yyparse()) {
Reid Spencer950bf602007-01-26 08:19:09 +00001548 if (ParserResult)
1549 delete ParserResult;
Reid Spencer30d0c582007-01-15 00:26:18 +00001550 std::cerr << "llvm-upgrade: parse failed.\n";
Reid Spencer30d0c582007-01-15 00:26:18 +00001551 return 0;
1552 }
1553
Reid Spencer950bf602007-01-26 08:19:09 +00001554 // Check to make sure that parsing produced a result
1555 if (!ParserResult) {
1556 std::cerr << "llvm-upgrade: no parse result.\n";
1557 return 0;
Reid Spencer30d0c582007-01-15 00:26:18 +00001558 }
1559
Reid Spencer950bf602007-01-26 08:19:09 +00001560 // Reset ParserResult variable while saving its value for the result.
1561 Module *Result = ParserResult;
1562 ParserResult = 0;
Reid Spencer30d0c582007-01-15 00:26:18 +00001563
Reid Spencer950bf602007-01-26 08:19:09 +00001564 //Not all functions use vaarg, so make a second check for ObsoleteVarArgs
Reid Spencer30d0c582007-01-15 00:26:18 +00001565 {
Reid Spencer950bf602007-01-26 08:19:09 +00001566 Function* F;
Reid Spencer688b0492007-02-05 21:19:13 +00001567 if ((F = Result->getFunction("llvm.va_start"))
Reid Spencer950bf602007-01-26 08:19:09 +00001568 && F->getFunctionType()->getNumParams() == 0)
1569 ObsoleteVarArgs = true;
Reid Spencer688b0492007-02-05 21:19:13 +00001570 if((F = Result->getFunction("llvm.va_copy"))
Reid Spencer950bf602007-01-26 08:19:09 +00001571 && F->getFunctionType()->getNumParams() == 1)
1572 ObsoleteVarArgs = true;
Reid Spencer280d8012006-12-01 23:40:53 +00001573 }
Reid Spencer319a7302007-01-05 17:20:02 +00001574
Reid Spencer950bf602007-01-26 08:19:09 +00001575 if (ObsoleteVarArgs && NewVarArgs) {
1576 error("This file is corrupt: it uses both new and old style varargs");
1577 return 0;
Reid Spencer319a7302007-01-05 17:20:02 +00001578 }
Reid Spencer319a7302007-01-05 17:20:02 +00001579
Reid Spencer950bf602007-01-26 08:19:09 +00001580 if(ObsoleteVarArgs) {
Reid Spencer688b0492007-02-05 21:19:13 +00001581 if(Function* F = Result->getFunction("llvm.va_start")) {
Reid Spencer950bf602007-01-26 08:19:09 +00001582 if (F->arg_size() != 0) {
1583 error("Obsolete va_start takes 0 argument");
Reid Spencer319a7302007-01-05 17:20:02 +00001584 return 0;
1585 }
Reid Spencer950bf602007-01-26 08:19:09 +00001586
1587 //foo = va_start()
1588 // ->
1589 //bar = alloca typeof(foo)
1590 //va_start(bar)
1591 //foo = load bar
Reid Spencer319a7302007-01-05 17:20:02 +00001592
Reid Spencer950bf602007-01-26 08:19:09 +00001593 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
1594 const Type* ArgTy = F->getFunctionType()->getReturnType();
1595 const Type* ArgTyPtr = PointerType::get(ArgTy);
1596 Function* NF = cast<Function>(Result->getOrInsertFunction(
1597 "llvm.va_start", RetTy, ArgTyPtr, (Type *)0));
1598
1599 while (!F->use_empty()) {
1600 CallInst* CI = cast<CallInst>(F->use_back());
1601 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
1602 new CallInst(NF, bar, "", CI);
1603 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
1604 CI->replaceAllUsesWith(foo);
1605 CI->getParent()->getInstList().erase(CI);
Reid Spencerf8383de2007-01-06 06:04:32 +00001606 }
Reid Spencer950bf602007-01-26 08:19:09 +00001607 Result->getFunctionList().erase(F);
Reid Spencerf8383de2007-01-06 06:04:32 +00001608 }
Reid Spencer950bf602007-01-26 08:19:09 +00001609
Reid Spencer688b0492007-02-05 21:19:13 +00001610 if(Function* F = Result->getFunction("llvm.va_end")) {
Reid Spencer950bf602007-01-26 08:19:09 +00001611 if(F->arg_size() != 1) {
1612 error("Obsolete va_end takes 1 argument");
1613 return 0;
Reid Spencerf8383de2007-01-06 06:04:32 +00001614 }
Reid Spencerf8383de2007-01-06 06:04:32 +00001615
Reid Spencer950bf602007-01-26 08:19:09 +00001616 //vaend foo
1617 // ->
1618 //bar = alloca 1 of typeof(foo)
1619 //vaend bar
1620 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
1621 const Type* ArgTy = F->getFunctionType()->getParamType(0);
1622 const Type* ArgTyPtr = PointerType::get(ArgTy);
1623 Function* NF = cast<Function>(Result->getOrInsertFunction(
1624 "llvm.va_end", RetTy, ArgTyPtr, (Type *)0));
Reid Spencerf8383de2007-01-06 06:04:32 +00001625
Reid Spencer950bf602007-01-26 08:19:09 +00001626 while (!F->use_empty()) {
1627 CallInst* CI = cast<CallInst>(F->use_back());
1628 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
1629 new StoreInst(CI->getOperand(1), bar, CI);
1630 new CallInst(NF, bar, "", CI);
1631 CI->getParent()->getInstList().erase(CI);
Reid Spencere77e35e2006-12-01 20:26:20 +00001632 }
Reid Spencer950bf602007-01-26 08:19:09 +00001633 Result->getFunctionList().erase(F);
Reid Spencere77e35e2006-12-01 20:26:20 +00001634 }
Reid Spencer950bf602007-01-26 08:19:09 +00001635
Reid Spencer688b0492007-02-05 21:19:13 +00001636 if(Function* F = Result->getFunction("llvm.va_copy")) {
Reid Spencer950bf602007-01-26 08:19:09 +00001637 if(F->arg_size() != 1) {
1638 error("Obsolete va_copy takes 1 argument");
1639 return 0;
Reid Spencere77e35e2006-12-01 20:26:20 +00001640 }
Reid Spencer950bf602007-01-26 08:19:09 +00001641 //foo = vacopy(bar)
1642 // ->
1643 //a = alloca 1 of typeof(foo)
1644 //b = alloca 1 of typeof(foo)
1645 //store bar -> b
1646 //vacopy(a, b)
1647 //foo = load a
1648
1649 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
1650 const Type* ArgTy = F->getFunctionType()->getReturnType();
1651 const Type* ArgTyPtr = PointerType::get(ArgTy);
1652 Function* NF = cast<Function>(Result->getOrInsertFunction(
1653 "llvm.va_copy", RetTy, ArgTyPtr, ArgTyPtr, (Type *)0));
Reid Spencere77e35e2006-12-01 20:26:20 +00001654
Reid Spencer950bf602007-01-26 08:19:09 +00001655 while (!F->use_empty()) {
1656 CallInst* CI = cast<CallInst>(F->use_back());
David Greeneae5115d2007-08-15 17:58:51 +00001657 Value *Args[2] = {
1658 new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI),
1659 new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI)
1660 };
David Greene718fda32007-08-01 03:59:32 +00001661 new StoreInst(CI->getOperand(1), Args[1], CI);
David Greeneae5115d2007-08-15 17:58:51 +00001662 new CallInst(NF, Args, Args + 2, "", CI);
David Greene718fda32007-08-01 03:59:32 +00001663 Value* foo = new LoadInst(Args[0], "vacopy.fix.3", CI);
Reid Spencer950bf602007-01-26 08:19:09 +00001664 CI->replaceAllUsesWith(foo);
1665 CI->getParent()->getInstList().erase(CI);
1666 }
1667 Result->getFunctionList().erase(F);
Reid Spencer319a7302007-01-05 17:20:02 +00001668 }
1669 }
1670
Reid Spencer52402b02007-01-02 05:45:11 +00001671 return Result;
1672}
1673
Reid Spencer950bf602007-01-26 08:19:09 +00001674} // end llvm namespace
Reid Spencer319a7302007-01-05 17:20:02 +00001675
Reid Spencer950bf602007-01-26 08:19:09 +00001676using namespace llvm;
Reid Spencer30d0c582007-01-15 00:26:18 +00001677
Reid Spencere7c3c602006-11-30 06:36:44 +00001678%}
1679
Reid Spencere77e35e2006-12-01 20:26:20 +00001680%union {
Reid Spencer950bf602007-01-26 08:19:09 +00001681 llvm::Module *ModuleVal;
1682 llvm::Function *FunctionVal;
1683 std::pair<llvm::PATypeInfo, char*> *ArgVal;
1684 llvm::BasicBlock *BasicBlockVal;
Reid Spencerbb1fd572007-03-21 17:15:50 +00001685 llvm::TermInstInfo TermInstVal;
Reid Spencer950bf602007-01-26 08:19:09 +00001686 llvm::InstrInfo InstVal;
1687 llvm::ConstInfo ConstVal;
1688 llvm::ValueInfo ValueVal;
1689 llvm::PATypeInfo TypeVal;
1690 llvm::TypeInfo PrimType;
1691 llvm::PHIListInfo PHIList;
1692 std::list<llvm::PATypeInfo> *TypeList;
1693 std::vector<llvm::ValueInfo> *ValueList;
1694 std::vector<llvm::ConstInfo> *ConstVector;
1695
1696
1697 std::vector<std::pair<llvm::PATypeInfo,char*> > *ArgList;
1698 // Represent the RHS of PHI node
1699 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
1700
1701 llvm::GlobalValue::LinkageTypes Linkage;
1702 int64_t SInt64Val;
1703 uint64_t UInt64Val;
1704 int SIntVal;
1705 unsigned UIntVal;
Dale Johannesen43421b32007-09-06 18:13:44 +00001706 llvm::APFloat *FPVal;
Reid Spencer950bf602007-01-26 08:19:09 +00001707 bool BoolVal;
1708
1709 char *StrVal; // This memory is strdup'd!
1710 llvm::ValID ValIDVal; // strdup'd memory maybe!
1711
1712 llvm::BinaryOps BinaryOpVal;
1713 llvm::TermOps TermOpVal;
1714 llvm::MemoryOps MemOpVal;
1715 llvm::OtherOps OtherOpVal;
1716 llvm::CastOps CastOpVal;
1717 llvm::ICmpInst::Predicate IPred;
1718 llvm::FCmpInst::Predicate FPred;
1719 llvm::Module::Endianness Endianness;
Reid Spencere77e35e2006-12-01 20:26:20 +00001720}
1721
Reid Spencer950bf602007-01-26 08:19:09 +00001722%type <ModuleVal> Module FunctionList
1723%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
1724%type <BasicBlockVal> BasicBlock InstructionList
1725%type <TermInstVal> BBTerminatorInst
1726%type <InstVal> Inst InstVal MemoryInst
1727%type <ConstVal> ConstVal ConstExpr
1728%type <ConstVector> ConstVector
1729%type <ArgList> ArgList ArgListH
1730%type <ArgVal> ArgVal
1731%type <PHIList> PHIList
1732%type <ValueList> ValueRefList ValueRefListE // For call param lists
1733%type <ValueList> IndexList // For GEP derived indices
1734%type <TypeList> TypeListI ArgTypeListI
1735%type <JumpTable> JumpTable
1736%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
1737%type <BoolVal> OptVolatile // 'volatile' or not
1738%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1739%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencered96d1e2007-02-08 09:08:52 +00001740%type <Linkage> OptLinkage FnDeclareLinkage
Reid Spencer950bf602007-01-26 08:19:09 +00001741%type <Endianness> BigOrLittle
Reid Spencere77e35e2006-12-01 20:26:20 +00001742
Reid Spencer950bf602007-01-26 08:19:09 +00001743// ValueRef - Unresolved reference to a definition or BB
1744%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1745%type <ValueVal> ResolvedVal // <type> <valref> pair
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001746
Reid Spencer950bf602007-01-26 08:19:09 +00001747// Tokens and types for handling constant integer values
1748//
1749// ESINT64VAL - A negative number within long long range
1750%token <SInt64Val> ESINT64VAL
Reid Spencere77e35e2006-12-01 20:26:20 +00001751
Reid Spencer950bf602007-01-26 08:19:09 +00001752// EUINT64VAL - A positive number within uns. long long range
1753%token <UInt64Val> EUINT64VAL
1754%type <SInt64Val> EINT64VAL
Reid Spencere77e35e2006-12-01 20:26:20 +00001755
Reid Spencer950bf602007-01-26 08:19:09 +00001756%token <SIntVal> SINTVAL // Signed 32 bit ints...
1757%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
1758%type <SIntVal> INTVAL
1759%token <FPVal> FPVAL // Float or Double constant
Reid Spencere77e35e2006-12-01 20:26:20 +00001760
Reid Spencer950bf602007-01-26 08:19:09 +00001761// Built in types...
1762%type <TypeVal> Types TypesV UpRTypes UpRTypesV
1763%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
1764%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
1765%token <PrimType> FLOAT DOUBLE TYPE LABEL
Reid Spencere77e35e2006-12-01 20:26:20 +00001766
Reid Spencer950bf602007-01-26 08:19:09 +00001767%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
1768%type <StrVal> Name OptName OptAssign
1769%type <UIntVal> OptAlign OptCAlign
1770%type <StrVal> OptSection SectionString
1771
1772%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
1773%token DECLARE GLOBAL CONSTANT SECTION VOLATILE
1774%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
1775%token DLLIMPORT DLLEXPORT EXTERN_WEAK
1776%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
1777%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
1778%token CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
1779%token X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
1780%token DATALAYOUT
1781%type <UIntVal> OptCallingConv
1782
1783// Basic Block Terminating Operators
1784%token <TermOpVal> RET BR SWITCH INVOKE UNREACHABLE
1785%token UNWIND EXCEPT
1786
1787// Binary Operators
1788%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Reid Spencer832254e2007-02-02 02:16:23 +00001789%type <BinaryOpVal> ShiftOps
Reid Spencer950bf602007-01-26 08:19:09 +00001790%token <BinaryOpVal> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM
Reid Spencer832254e2007-02-02 02:16:23 +00001791%token <BinaryOpVal> AND OR XOR SHL SHR ASHR LSHR
Reid Spencer950bf602007-01-26 08:19:09 +00001792%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
1793%token <OtherOpVal> ICMP FCMP
1794
1795// Memory Instructions
1796%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1797
1798// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001799%token <OtherOpVal> PHI_TOK SELECT VAARG
Reid Spencer950bf602007-01-26 08:19:09 +00001800%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
1801%token VAARG_old VANEXT_old //OBSOLETE
1802
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001803// Support for ICmp/FCmp Predicates, which is 1.9++ but not 2.0
Reid Spencer950bf602007-01-26 08:19:09 +00001804%type <IPred> IPredicates
1805%type <FPred> FPredicates
1806%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1807%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
1808
1809%token <CastOpVal> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI
1810%token <CastOpVal> UITOFP SITOFP PTRTOINT INTTOPTR BITCAST
1811%type <CastOpVal> CastOps
Reid Spencere7c3c602006-11-30 06:36:44 +00001812
1813%start Module
1814
1815%%
1816
1817// Handle constant integer size restriction and conversion...
Reid Spencer950bf602007-01-26 08:19:09 +00001818//
1819INTVAL
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001820 : SINTVAL
Reid Spencer950bf602007-01-26 08:19:09 +00001821 | UINTVAL {
1822 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
1823 error("Value too large for type");
1824 $$ = (int32_t)$1;
1825 }
1826 ;
1827
1828EINT64VAL
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00001829 : ESINT64VAL // These have same type and can't cause problems...
Reid Spencer950bf602007-01-26 08:19:09 +00001830 | EUINT64VAL {
1831 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
1832 error("Value too large for type");
1833 $$ = (int64_t)$1;
1834 };
Reid Spencere7c3c602006-11-30 06:36:44 +00001835
1836// Operations that are notably excluded from this list include:
1837// RET, BR, & SWITCH because they end basic blocks and are treated specially.
Reid Spencer950bf602007-01-26 08:19:09 +00001838//
1839ArithmeticOps
1840 : ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV | REM | UREM | SREM | FREM
1841 ;
1842
1843LogicalOps
1844 : AND | OR | XOR
1845 ;
1846
1847SetCondOps
1848 : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
1849 ;
1850
1851IPredicates
1852 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
1853 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1854 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1855 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1856 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1857 ;
1858
1859FPredicates
1860 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1861 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1862 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1863 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1864 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1865 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1866 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1867 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1868 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1869 ;
1870ShiftOps
1871 : SHL | SHR | ASHR | LSHR
1872 ;
1873
1874CastOps
1875 : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI
1876 | UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
1877 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001878
1879// These are some types that allow classification if we only want a particular
1880// thing... for example, only a signed, unsigned, or integral type.
Reid Spencer950bf602007-01-26 08:19:09 +00001881SIntType
1882 : LONG | INT | SHORT | SBYTE
1883 ;
1884
1885UIntType
1886 : ULONG | UINT | USHORT | UBYTE
1887 ;
1888
1889IntType
1890 : SIntType | UIntType
1891 ;
1892
1893FPType
1894 : FLOAT | DOUBLE
1895 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001896
1897// OptAssign - Value producing statements have an optional assignment component
Reid Spencer950bf602007-01-26 08:19:09 +00001898OptAssign
1899 : Name '=' {
Reid Spencere7c3c602006-11-30 06:36:44 +00001900 $$ = $1;
1901 }
1902 | /*empty*/ {
Reid Spencer950bf602007-01-26 08:19:09 +00001903 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001904 };
1905
1906OptLinkage
Reid Spencer785a5ae2007-02-08 00:21:40 +00001907 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencer950bf602007-01-26 08:19:09 +00001908 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1909 | WEAK { $$ = GlobalValue::WeakLinkage; }
1910 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1911 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1912 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencer785a5ae2007-02-08 00:21:40 +00001913 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencer950bf602007-01-26 08:19:09 +00001914 | /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1915 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001916
1917OptCallingConv
Reid Spencer7eea8ff2007-05-18 05:48:07 +00001918 : /*empty*/ { $$ = lastCallingConv = OldCallingConv::C; }
1919 | CCC_TOK { $$ = lastCallingConv = OldCallingConv::C; }
1920 | CSRETCC_TOK { $$ = lastCallingConv = OldCallingConv::CSRet; }
1921 | FASTCC_TOK { $$ = lastCallingConv = OldCallingConv::Fast; }
1922 | COLDCC_TOK { $$ = lastCallingConv = OldCallingConv::Cold; }
1923 | X86_STDCALLCC_TOK { $$ = lastCallingConv = OldCallingConv::X86_StdCall; }
1924 | X86_FASTCALLCC_TOK { $$ = lastCallingConv = OldCallingConv::X86_FastCall; }
Reid Spencer950bf602007-01-26 08:19:09 +00001925 | CC_TOK EUINT64VAL {
1926 if ((unsigned)$2 != $2)
1927 error("Calling conv too large");
Reid Spencer7eea8ff2007-05-18 05:48:07 +00001928 $$ = lastCallingConv = $2;
Reid Spencer950bf602007-01-26 08:19:09 +00001929 }
1930 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001931
1932// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1933// a comma before it.
1934OptAlign
Reid Spencer950bf602007-01-26 08:19:09 +00001935 : /*empty*/ { $$ = 0; }
1936 | ALIGN EUINT64VAL {
1937 $$ = $2;
1938 if ($$ != 0 && !isPowerOf2_32($$))
1939 error("Alignment must be a power of two");
1940 }
1941 ;
Reid Spencerf0cf1322006-12-07 04:23:03 +00001942
Reid Spencere7c3c602006-11-30 06:36:44 +00001943OptCAlign
Reid Spencer950bf602007-01-26 08:19:09 +00001944 : /*empty*/ { $$ = 0; }
1945 | ',' ALIGN EUINT64VAL {
1946 $$ = $3;
1947 if ($$ != 0 && !isPowerOf2_32($$))
1948 error("Alignment must be a power of two");
1949 }
1950 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001951
1952SectionString
Reid Spencer950bf602007-01-26 08:19:09 +00001953 : SECTION STRINGCONSTANT {
1954 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1955 if ($2[i] == '"' || $2[i] == '\\')
1956 error("Invalid character in section name");
1957 $$ = $2;
1958 }
1959 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001960
Reid Spencer950bf602007-01-26 08:19:09 +00001961OptSection
1962 : /*empty*/ { $$ = 0; }
1963 | SectionString { $$ = $1; }
1964 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001965
Reid Spencer950bf602007-01-26 08:19:09 +00001966// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1967// is set to be the global we are processing.
1968//
Reid Spencere7c3c602006-11-30 06:36:44 +00001969GlobalVarAttributes
Reid Spencer950bf602007-01-26 08:19:09 +00001970 : /* empty */ {}
1971 | ',' GlobalVarAttribute GlobalVarAttributes {}
1972 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001973
Reid Spencer950bf602007-01-26 08:19:09 +00001974GlobalVarAttribute
1975 : SectionString {
1976 CurGV->setSection($1);
1977 free($1);
1978 }
1979 | ALIGN EUINT64VAL {
1980 if ($2 != 0 && !isPowerOf2_32($2))
1981 error("Alignment must be a power of two");
1982 CurGV->setAlignment($2);
1983
1984 }
1985 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001986
1987//===----------------------------------------------------------------------===//
1988// Types includes all predefined types... except void, because it can only be
1989// used in specific contexts (function returning void for example). To have
1990// access to it, a user must explicitly use TypesV.
1991//
1992
1993// TypesV includes all of 'Types', but it also includes the void type.
Reid Spencer950bf602007-01-26 08:19:09 +00001994TypesV
1995 : Types
1996 | VOID {
Reid Spencered96d1e2007-02-08 09:08:52 +00001997 $$.PAT = new PATypeHolder($1.T);
Reid Spencerbb1fd572007-03-21 17:15:50 +00001998 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00001999 }
2000 ;
2001
2002UpRTypesV
2003 : UpRTypes
2004 | VOID {
Reid Spencered96d1e2007-02-08 09:08:52 +00002005 $$.PAT = new PATypeHolder($1.T);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002006 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002007 }
2008 ;
2009
2010Types
2011 : UpRTypes {
2012 if (!UpRefs.empty())
Reid Spencered96d1e2007-02-08 09:08:52 +00002013 error("Invalid upreference in type: " + (*$1.PAT)->getDescription());
Reid Spencer950bf602007-01-26 08:19:09 +00002014 $$ = $1;
2015 }
2016 ;
2017
2018PrimType
2019 : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
2020 | LONG | ULONG | FLOAT | DOUBLE | LABEL
2021 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002022
2023// Derived types are added later...
Reid Spencera50d5962006-12-02 04:11:07 +00002024UpRTypes
Reid Spencer950bf602007-01-26 08:19:09 +00002025 : PrimType {
Reid Spencered96d1e2007-02-08 09:08:52 +00002026 $$.PAT = new PATypeHolder($1.T);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002027 $$.S.copy($1.S);
Reid Spencera50d5962006-12-02 04:11:07 +00002028 }
Reid Spencer950bf602007-01-26 08:19:09 +00002029 | OPAQUE {
Reid Spencered96d1e2007-02-08 09:08:52 +00002030 $$.PAT = new PATypeHolder(OpaqueType::get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002031 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002032 }
2033 | SymbolicValueRef { // Named types are also simple types...
Reid Spencerbb1fd572007-03-21 17:15:50 +00002034 $$.S.copy(getTypeSign($1));
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00002035 const Type* tmp = getType($1);
Reid Spencered96d1e2007-02-08 09:08:52 +00002036 $$.PAT = new PATypeHolder(tmp);
Reid Spencer78720742006-12-02 20:21:22 +00002037 }
2038 | '\\' EUINT64VAL { // Type UpReference
Reid Spencer950bf602007-01-26 08:19:09 +00002039 if ($2 > (uint64_t)~0U)
2040 error("Value out of range");
2041 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
2042 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencered96d1e2007-02-08 09:08:52 +00002043 $$.PAT = new PATypeHolder(OT);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002044 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002045 UR_OUT("New Upreference!\n");
Reid Spencere7c3c602006-11-30 06:36:44 +00002046 }
2047 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Reid Spencerbb1fd572007-03-21 17:15:50 +00002048 $$.S.makeComposite($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002049 std::vector<const Type*> Params;
2050 for (std::list<llvm::PATypeInfo>::iterator I = $3->begin(),
2051 E = $3->end(); I != E; ++I) {
Reid Spencered96d1e2007-02-08 09:08:52 +00002052 Params.push_back(I->PAT->get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002053 $$.S.add(I->S);
Reid Spencer52402b02007-01-02 05:45:11 +00002054 }
Reid Spencer950bf602007-01-26 08:19:09 +00002055 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
2056 if (isVarArg) Params.pop_back();
2057
Reid Spencer7eea8ff2007-05-18 05:48:07 +00002058 ParamAttrsList *PAL = 0;
2059 if (lastCallingConv == OldCallingConv::CSRet) {
2060 ParamAttrsVector Attrs;
2061 ParamAttrsWithIndex PAWI;
2062 PAWI.index = 1; PAWI.attrs = ParamAttr::StructRet; // first arg
2063 Attrs.push_back(PAWI);
2064 PAL = ParamAttrsList::get(Attrs);
2065 }
2066
Reid Spencer7b5d4662007-04-09 06:16:21 +00002067 const FunctionType *FTy =
Duncan Sandsdc024672007-11-27 13:23:08 +00002068 FunctionType::get($1.PAT->get(), Params, isVarArg);
Reid Spencer7b5d4662007-04-09 06:16:21 +00002069
2070 $$.PAT = new PATypeHolder( HandleUpRefs(FTy, $$.S) );
Reid Spencerbb1fd572007-03-21 17:15:50 +00002071 delete $1.PAT; // Delete the return type handle
Reid Spencer950bf602007-01-26 08:19:09 +00002072 delete $3; // Delete the argument list
Reid Spencere7c3c602006-11-30 06:36:44 +00002073 }
2074 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencerbb1fd572007-03-21 17:15:50 +00002075 $$.S.makeComposite($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002076 $$.PAT = new PATypeHolder(HandleUpRefs(ArrayType::get($4.PAT->get(),
Reid Spencerbb1fd572007-03-21 17:15:50 +00002077 (unsigned)$2), $$.S));
Reid Spencered96d1e2007-02-08 09:08:52 +00002078 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002079 }
Chris Lattner4227bdb2007-02-19 07:34:02 +00002080 | '<' EUINT64VAL 'x' UpRTypes '>' { // Vector type?
Reid Spencerbb1fd572007-03-21 17:15:50 +00002081 const llvm::Type* ElemTy = $4.PAT->get();
2082 if ((unsigned)$2 != $2)
2083 error("Unsigned result not equal to signed result");
2084 if (!(ElemTy->isInteger() || ElemTy->isFloatingPoint()))
2085 error("Elements of a VectorType must be integer or floating point");
2086 if (!isPowerOf2_32($2))
2087 error("VectorType length should be a power of 2");
2088 $$.S.makeComposite($4.S);
2089 $$.PAT = new PATypeHolder(HandleUpRefs(VectorType::get(ElemTy,
2090 (unsigned)$2), $$.S));
2091 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002092 }
2093 | '{' TypeListI '}' { // Structure type?
Reid Spencer950bf602007-01-26 08:19:09 +00002094 std::vector<const Type*> Elements;
Reid Spencerbb1fd572007-03-21 17:15:50 +00002095 $$.S.makeComposite();
Reid Spencer950bf602007-01-26 08:19:09 +00002096 for (std::list<llvm::PATypeInfo>::iterator I = $2->begin(),
Reid Spencerbb1fd572007-03-21 17:15:50 +00002097 E = $2->end(); I != E; ++I) {
Reid Spencered96d1e2007-02-08 09:08:52 +00002098 Elements.push_back(I->PAT->get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002099 $$.S.add(I->S);
2100 }
2101 $$.PAT = new PATypeHolder(HandleUpRefs(StructType::get(Elements), $$.S));
Reid Spencer950bf602007-01-26 08:19:09 +00002102 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00002103 }
2104 | '{' '}' { // Empty structure type?
Reid Spencered96d1e2007-02-08 09:08:52 +00002105 $$.PAT = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencerbb1fd572007-03-21 17:15:50 +00002106 $$.S.makeComposite();
Reid Spencere7c3c602006-11-30 06:36:44 +00002107 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002108 | '<' '{' TypeListI '}' '>' { // Packed Structure type?
Reid Spencerbb1fd572007-03-21 17:15:50 +00002109 $$.S.makeComposite();
Reid Spencer950bf602007-01-26 08:19:09 +00002110 std::vector<const Type*> Elements;
2111 for (std::list<llvm::PATypeInfo>::iterator I = $3->begin(),
2112 E = $3->end(); I != E; ++I) {
Reid Spencered96d1e2007-02-08 09:08:52 +00002113 Elements.push_back(I->PAT->get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002114 $$.S.add(I->S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002115 delete I->PAT;
Reid Spencer52402b02007-01-02 05:45:11 +00002116 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00002117 $$.PAT = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true),
2118 $$.S));
Reid Spencer950bf602007-01-26 08:19:09 +00002119 delete $3;
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002120 }
2121 | '<' '{' '}' '>' { // Empty packed structure type?
Reid Spencered96d1e2007-02-08 09:08:52 +00002122 $$.PAT = new PATypeHolder(StructType::get(std::vector<const Type*>(),true));
Reid Spencerbb1fd572007-03-21 17:15:50 +00002123 $$.S.makeComposite();
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002124 }
Reid Spencere7c3c602006-11-30 06:36:44 +00002125 | UpRTypes '*' { // Pointer type?
Reid Spencered96d1e2007-02-08 09:08:52 +00002126 if ($1.PAT->get() == Type::LabelTy)
Reid Spencer950bf602007-01-26 08:19:09 +00002127 error("Cannot form a pointer to a basic block");
Reid Spencerbb1fd572007-03-21 17:15:50 +00002128 $$.S.makeComposite($1.S);
2129 $$.PAT = new PATypeHolder(HandleUpRefs(PointerType::get($1.PAT->get()),
2130 $$.S));
Reid Spencered96d1e2007-02-08 09:08:52 +00002131 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002132 }
2133 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002134
2135// TypeList - Used for struct declarations and as a basis for function type
2136// declaration type lists
2137//
Reid Spencere77e35e2006-12-01 20:26:20 +00002138TypeListI
2139 : UpRTypes {
Reid Spencer950bf602007-01-26 08:19:09 +00002140 $$ = new std::list<PATypeInfo>();
2141 $$->push_back($1);
Reid Spencere77e35e2006-12-01 20:26:20 +00002142 }
2143 | TypeListI ',' UpRTypes {
Reid Spencer950bf602007-01-26 08:19:09 +00002144 ($$=$1)->push_back($3);
2145 }
2146 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002147
2148// ArgTypeList - List of types for a function type declaration...
Reid Spencere77e35e2006-12-01 20:26:20 +00002149ArgTypeListI
Reid Spencer950bf602007-01-26 08:19:09 +00002150 : TypeListI
Reid Spencere7c3c602006-11-30 06:36:44 +00002151 | TypeListI ',' DOTDOTDOT {
Reid Spencer950bf602007-01-26 08:19:09 +00002152 PATypeInfo VoidTI;
Reid Spencered96d1e2007-02-08 09:08:52 +00002153 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002154 VoidTI.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002155 ($$=$1)->push_back(VoidTI);
Reid Spencere7c3c602006-11-30 06:36:44 +00002156 }
2157 | DOTDOTDOT {
Reid Spencer950bf602007-01-26 08:19:09 +00002158 $$ = new std::list<PATypeInfo>();
2159 PATypeInfo VoidTI;
Reid Spencered96d1e2007-02-08 09:08:52 +00002160 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002161 VoidTI.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002162 $$->push_back(VoidTI);
Reid Spencere7c3c602006-11-30 06:36:44 +00002163 }
2164 | /*empty*/ {
Reid Spencer950bf602007-01-26 08:19:09 +00002165 $$ = new std::list<PATypeInfo>();
2166 }
2167 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002168
2169// ConstVal - The various declarations that go into the constant pool. This
2170// production is used ONLY to represent constants that show up AFTER a 'const',
2171// 'constant' or 'global' token at global scope. Constants that can be inlined
2172// into other expressions (such as integers and constexprs) are handled by the
2173// ResolvedVal, ValueRef and ConstValueRef productions.
2174//
Reid Spencer950bf602007-01-26 08:19:09 +00002175ConstVal
2176 : Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencered96d1e2007-02-08 09:08:52 +00002177 const ArrayType *ATy = dyn_cast<ArrayType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002178 if (ATy == 0)
2179 error("Cannot make array constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002180 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002181 const Type *ETy = ATy->getElementType();
2182 int NumElements = ATy->getNumElements();
2183
2184 // Verify that we have the correct size...
2185 if (NumElements != -1 && NumElements != (int)$3->size())
2186 error("Type mismatch: constant sized array initialized with " +
2187 utostr($3->size()) + " arguments, but has size of " +
2188 itostr(NumElements) + "");
2189
2190 // Verify all elements are correct type!
2191 std::vector<Constant*> Elems;
2192 for (unsigned i = 0; i < $3->size(); i++) {
2193 Constant *C = (*$3)[i].C;
2194 const Type* ValTy = C->getType();
2195 if (ETy != ValTy)
2196 error("Element #" + utostr(i) + " is not of type '" +
2197 ETy->getDescription() +"' as required!\nIt is of type '"+
2198 ValTy->getDescription() + "'");
2199 Elems.push_back(C);
2200 }
2201 $$.C = ConstantArray::get(ATy, Elems);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002202 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002203 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002204 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002205 }
2206 | Types '[' ']' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002207 const ArrayType *ATy = dyn_cast<ArrayType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002208 if (ATy == 0)
2209 error("Cannot make array constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002210 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002211 int NumElements = ATy->getNumElements();
2212 if (NumElements != -1 && NumElements != 0)
2213 error("Type mismatch: constant sized array initialized with 0"
2214 " arguments, but has size of " + itostr(NumElements) +"");
2215 $$.C = ConstantArray::get(ATy, std::vector<Constant*>());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002216 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002217 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002218 }
2219 | Types 'c' STRINGCONSTANT {
Reid Spencered96d1e2007-02-08 09:08:52 +00002220 const ArrayType *ATy = dyn_cast<ArrayType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002221 if (ATy == 0)
2222 error("Cannot make array constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002223 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002224 int NumElements = ATy->getNumElements();
2225 const Type *ETy = dyn_cast<IntegerType>(ATy->getElementType());
2226 if (!ETy || cast<IntegerType>(ETy)->getBitWidth() != 8)
2227 error("String arrays require type i8, not '" + ETy->getDescription() +
2228 "'");
2229 char *EndStr = UnEscapeLexed($3, true);
2230 if (NumElements != -1 && NumElements != (EndStr-$3))
2231 error("Can't build string constant of size " +
2232 itostr((int)(EndStr-$3)) + " when array has size " +
2233 itostr(NumElements) + "");
2234 std::vector<Constant*> Vals;
2235 for (char *C = (char *)$3; C != (char *)EndStr; ++C)
2236 Vals.push_back(ConstantInt::get(ETy, *C));
2237 free($3);
2238 $$.C = ConstantArray::get(ATy, Vals);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002239 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002240 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002241 }
2242 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer9d6565a2007-02-15 02:26:10 +00002243 const VectorType *PTy = dyn_cast<VectorType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002244 if (PTy == 0)
2245 error("Cannot make packed constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002246 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002247 const Type *ETy = PTy->getElementType();
2248 int NumElements = PTy->getNumElements();
2249 // Verify that we have the correct size...
2250 if (NumElements != -1 && NumElements != (int)$3->size())
2251 error("Type mismatch: constant sized packed initialized with " +
2252 utostr($3->size()) + " arguments, but has size of " +
2253 itostr(NumElements) + "");
2254 // Verify all elements are correct type!
2255 std::vector<Constant*> Elems;
2256 for (unsigned i = 0; i < $3->size(); i++) {
2257 Constant *C = (*$3)[i].C;
2258 const Type* ValTy = C->getType();
2259 if (ETy != ValTy)
2260 error("Element #" + utostr(i) + " is not of type '" +
2261 ETy->getDescription() +"' as required!\nIt is of type '"+
2262 ValTy->getDescription() + "'");
2263 Elems.push_back(C);
2264 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00002265 $$.C = ConstantVector::get(PTy, Elems);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002266 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002267 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002268 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002269 }
2270 | Types '{' ConstVector '}' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002271 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002272 if (STy == 0)
2273 error("Cannot make struct constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002274 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002275 if ($3->size() != STy->getNumContainedTypes())
2276 error("Illegal number of initializers for structure type");
2277
2278 // Check to ensure that constants are compatible with the type initializer!
2279 std::vector<Constant*> Fields;
2280 for (unsigned i = 0, e = $3->size(); i != e; ++i) {
2281 Constant *C = (*$3)[i].C;
2282 if (C->getType() != STy->getElementType(i))
2283 error("Expected type '" + STy->getElementType(i)->getDescription() +
2284 "' for element #" + utostr(i) + " of structure initializer");
2285 Fields.push_back(C);
2286 }
2287 $$.C = ConstantStruct::get(STy, Fields);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002288 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002289 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002290 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002291 }
2292 | Types '{' '}' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002293 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002294 if (STy == 0)
2295 error("Cannot make struct constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002296 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002297 if (STy->getNumContainedTypes() != 0)
2298 error("Illegal number of initializers for structure type");
2299 $$.C = ConstantStruct::get(STy, std::vector<Constant*>());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002300 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002301 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002302 }
Reid Spencer950bf602007-01-26 08:19:09 +00002303 | Types '<' '{' ConstVector '}' '>' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002304 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002305 if (STy == 0)
2306 error("Cannot make packed struct constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002307 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002308 if ($4->size() != STy->getNumContainedTypes())
2309 error("Illegal number of initializers for packed structure type");
Reid Spencere7c3c602006-11-30 06:36:44 +00002310
Reid Spencer950bf602007-01-26 08:19:09 +00002311 // Check to ensure that constants are compatible with the type initializer!
2312 std::vector<Constant*> Fields;
2313 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
2314 Constant *C = (*$4)[i].C;
2315 if (C->getType() != STy->getElementType(i))
2316 error("Expected type '" + STy->getElementType(i)->getDescription() +
2317 "' for element #" + utostr(i) + " of packed struct initializer");
2318 Fields.push_back(C);
Reid Spencer280d8012006-12-01 23:40:53 +00002319 }
Reid Spencer950bf602007-01-26 08:19:09 +00002320 $$.C = ConstantStruct::get(STy, Fields);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002321 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002322 delete $1.PAT;
Reid Spencere77e35e2006-12-01 20:26:20 +00002323 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00002324 }
Reid Spencer950bf602007-01-26 08:19:09 +00002325 | Types '<' '{' '}' '>' {
Reid Spencered96d1e2007-02-08 09:08:52 +00002326 const StructType *STy = dyn_cast<StructType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002327 if (STy == 0)
2328 error("Cannot make packed struct constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002329 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002330 if (STy->getNumContainedTypes() != 0)
2331 error("Illegal number of initializers for packed structure type");
2332 $$.C = ConstantStruct::get(STy, std::vector<Constant*>());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002333 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002334 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002335 }
2336 | Types NULL_TOK {
Reid Spencered96d1e2007-02-08 09:08:52 +00002337 const PointerType *PTy = dyn_cast<PointerType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002338 if (PTy == 0)
2339 error("Cannot make null pointer constant with type: '" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002340 $1.PAT->get()->getDescription() + "'");
Reid Spencer950bf602007-01-26 08:19:09 +00002341 $$.C = ConstantPointerNull::get(PTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002342 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002343 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002344 }
2345 | Types UNDEF {
Reid Spencered96d1e2007-02-08 09:08:52 +00002346 $$.C = UndefValue::get($1.PAT->get());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002347 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002348 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002349 }
2350 | Types SymbolicValueRef {
Reid Spencered96d1e2007-02-08 09:08:52 +00002351 const PointerType *Ty = dyn_cast<PointerType>($1.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00002352 if (Ty == 0)
2353 error("Global const reference must be a pointer type, not" +
Reid Spencered96d1e2007-02-08 09:08:52 +00002354 $1.PAT->get()->getDescription());
Reid Spencer950bf602007-01-26 08:19:09 +00002355
2356 // ConstExprs can exist in the body of a function, thus creating
2357 // GlobalValues whenever they refer to a variable. Because we are in
2358 // the context of a function, getExistingValue will search the functions
2359 // symbol table instead of the module symbol table for the global symbol,
2360 // which throws things all off. To get around this, we just tell
2361 // getExistingValue that we are at global scope here.
2362 //
2363 Function *SavedCurFn = CurFun.CurrentFunction;
2364 CurFun.CurrentFunction = 0;
Reid Spencerbb1fd572007-03-21 17:15:50 +00002365 $2.S.copy($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002366 Value *V = getExistingValue(Ty, $2);
2367 CurFun.CurrentFunction = SavedCurFn;
2368
2369 // If this is an initializer for a constant pointer, which is referencing a
2370 // (currently) undefined variable, create a stub now that shall be replaced
2371 // in the future with the right type of variable.
2372 //
2373 if (V == 0) {
2374 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers");
2375 const PointerType *PT = cast<PointerType>(Ty);
2376
2377 // First check to see if the forward references value is already created!
2378 PerModuleInfo::GlobalRefsType::iterator I =
2379 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
2380
2381 if (I != CurModule.GlobalRefs.end()) {
2382 V = I->second; // Placeholder already exists, use it...
2383 $2.destroy();
2384 } else {
2385 std::string Name;
2386 if ($2.Type == ValID::NameVal) Name = $2.Name;
2387
2388 // Create the forward referenced global.
2389 GlobalValue *GV;
2390 if (const FunctionType *FTy =
2391 dyn_cast<FunctionType>(PT->getElementType())) {
2392 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
2393 CurModule.CurrentModule);
2394 } else {
2395 GV = new GlobalVariable(PT->getElementType(), false,
2396 GlobalValue::ExternalLinkage, 0,
2397 Name, CurModule.CurrentModule);
2398 }
2399
2400 // Keep track of the fact that we have a forward ref to recycle it
2401 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
2402 V = GV;
2403 }
2404 }
2405 $$.C = cast<GlobalValue>(V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002406 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002407 delete $1.PAT; // Free the type handle
Reid Spencer950bf602007-01-26 08:19:09 +00002408 }
2409 | Types ConstExpr {
Reid Spencered96d1e2007-02-08 09:08:52 +00002410 if ($1.PAT->get() != $2.C->getType())
Reid Spencer950bf602007-01-26 08:19:09 +00002411 error("Mismatched types for constant expression");
2412 $$ = $2;
Reid Spencerbb1fd572007-03-21 17:15:50 +00002413 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002414 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002415 }
2416 | Types ZEROINITIALIZER {
Reid Spencered96d1e2007-02-08 09:08:52 +00002417 const Type *Ty = $1.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00002418 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
2419 error("Cannot create a null initialized value of this type");
2420 $$.C = Constant::getNullValue(Ty);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002421 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002422 delete $1.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002423 }
2424 | SIntType EINT64VAL { // integral constants
2425 const Type *Ty = $1.T;
2426 if (!ConstantInt::isValueValidForType(Ty, $2))
2427 error("Constant value doesn't fit in type");
2428 $$.C = ConstantInt::get(Ty, $2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002429 $$.S.makeSigned();
Reid Spencer950bf602007-01-26 08:19:09 +00002430 }
2431 | UIntType EUINT64VAL { // integral constants
2432 const Type *Ty = $1.T;
2433 if (!ConstantInt::isValueValidForType(Ty, $2))
2434 error("Constant value doesn't fit in type");
2435 $$.C = ConstantInt::get(Ty, $2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002436 $$.S.makeUnsigned();
Reid Spencer950bf602007-01-26 08:19:09 +00002437 }
2438 | BOOL TRUETOK { // Boolean constants
2439 $$.C = ConstantInt::get(Type::Int1Ty, true);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002440 $$.S.makeUnsigned();
Reid Spencer950bf602007-01-26 08:19:09 +00002441 }
2442 | BOOL FALSETOK { // Boolean constants
2443 $$.C = ConstantInt::get(Type::Int1Ty, false);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002444 $$.S.makeUnsigned();
Reid Spencer950bf602007-01-26 08:19:09 +00002445 }
2446 | FPType FPVAL { // Float & Double constants
Dale Johannesen43421b32007-09-06 18:13:44 +00002447 if (!ConstantFP::isValueValidForType($1.T, *$2))
Reid Spencer950bf602007-01-26 08:19:09 +00002448 error("Floating point constant invalid for type");
Dale Johannesen43421b32007-09-06 18:13:44 +00002449 // Lexer has no type info, so builds all FP constants as double.
2450 // Fix this here.
2451 if ($1.T==Type::FloatTy)
2452 $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
2453 $$.C = ConstantFP::get($1.T, *$2);
Dale Johannesencdd509a2007-09-07 21:07:57 +00002454 delete $2;
Reid Spencerbb1fd572007-03-21 17:15:50 +00002455 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002456 }
2457 ;
2458
2459ConstExpr
2460 : CastOps '(' ConstVal TO Types ')' {
2461 const Type* SrcTy = $3.C->getType();
Reid Spencered96d1e2007-02-08 09:08:52 +00002462 const Type* DstTy = $5.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00002463 Signedness SrcSign($3.S);
2464 Signedness DstSign($5.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002465 if (!SrcTy->isFirstClassType())
2466 error("cast constant expression from a non-primitive type: '" +
2467 SrcTy->getDescription() + "'");
2468 if (!DstTy->isFirstClassType())
2469 error("cast constant expression to a non-primitive type: '" +
2470 DstTy->getDescription() + "'");
2471 $$.C = cast<Constant>(getCast($1, $3.C, SrcSign, DstTy, DstSign));
Reid Spencerbb1fd572007-03-21 17:15:50 +00002472 $$.S.copy(DstSign);
Reid Spencered96d1e2007-02-08 09:08:52 +00002473 delete $5.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002474 }
2475 | GETELEMENTPTR '(' ConstVal IndexList ')' {
2476 const Type *Ty = $3.C->getType();
2477 if (!isa<PointerType>(Ty))
2478 error("GetElementPtr requires a pointer operand");
2479
Reid Spencer950bf602007-01-26 08:19:09 +00002480 std::vector<Constant*> CIndices;
Reid Spencerff0e4482007-04-16 00:40:57 +00002481 upgradeGEPCEIndices($3.C->getType(), $4, CIndices);
Reid Spencer950bf602007-01-26 08:19:09 +00002482
2483 delete $4;
Chris Lattner4227bdb2007-02-19 07:34:02 +00002484 $$.C = ConstantExpr::getGetElementPtr($3.C, &CIndices[0], CIndices.size());
Reid Spencerbb1fd572007-03-21 17:15:50 +00002485 $$.S.copy(getElementSign($3, CIndices));
Reid Spencer950bf602007-01-26 08:19:09 +00002486 }
Reid Spencere7c3c602006-11-30 06:36:44 +00002487 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002488 if (!$3.C->getType()->isInteger() ||
2489 cast<IntegerType>($3.C->getType())->getBitWidth() != 1)
2490 error("Select condition must be bool type");
2491 if ($5.C->getType() != $7.C->getType())
2492 error("Select operand types must match");
2493 $$.C = ConstantExpr::getSelect($3.C, $5.C, $7.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002494 $$.S.copy($5.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002495 }
2496 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002497 const Type *Ty = $3.C->getType();
2498 if (Ty != $5.C->getType())
2499 error("Binary operator types must match");
2500 // First, make sure we're dealing with the right opcode by upgrading from
2501 // obsolete versions.
2502 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $3.S);
2503
2504 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
2505 // To retain backward compatibility with these early compilers, we emit a
2506 // cast to the appropriate integer type automatically if we are in the
2507 // broken case. See PR424 for more information.
2508 if (!isa<PointerType>(Ty)) {
2509 $$.C = ConstantExpr::get(Opcode, $3.C, $5.C);
2510 } else {
2511 const Type *IntPtrTy = 0;
2512 switch (CurModule.CurrentModule->getPointerSize()) {
2513 case Module::Pointer32: IntPtrTy = Type::Int32Ty; break;
2514 case Module::Pointer64: IntPtrTy = Type::Int64Ty; break;
2515 default: error("invalid pointer binary constant expr");
2516 }
2517 $$.C = ConstantExpr::get(Opcode,
2518 ConstantExpr::getCast(Instruction::PtrToInt, $3.C, IntPtrTy),
2519 ConstantExpr::getCast(Instruction::PtrToInt, $5.C, IntPtrTy));
2520 $$.C = ConstantExpr::getCast(Instruction::IntToPtr, $$.C, Ty);
2521 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00002522 $$.S.copy($3.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002523 }
2524 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002525 const Type* Ty = $3.C->getType();
2526 if (Ty != $5.C->getType())
2527 error("Logical operator types must match");
2528 if (!Ty->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00002529 if (!isa<VectorType>(Ty) ||
2530 !cast<VectorType>(Ty)->getElementType()->isInteger())
Reid Spencer950bf602007-01-26 08:19:09 +00002531 error("Logical operator requires integer operands");
2532 }
2533 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $3.S);
2534 $$.C = ConstantExpr::get(Opcode, $3.C, $5.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002535 $$.S.copy($3.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002536 }
2537 | SetCondOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002538 const Type* Ty = $3.C->getType();
2539 if (Ty != $5.C->getType())
2540 error("setcc operand types must match");
2541 unsigned short pred;
2542 Instruction::OtherOps Opcode = getCompareOp($1, pred, Ty, $3.S);
2543 $$.C = ConstantExpr::getCompare(Opcode, $3.C, $5.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002544 $$.S.makeUnsigned();
Reid Spencere7c3c602006-11-30 06:36:44 +00002545 }
Reid Spencer57f28f92006-12-03 07:10:26 +00002546 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002547 if ($4.C->getType() != $6.C->getType())
2548 error("icmp operand types must match");
2549 $$.C = ConstantExpr::getCompare($2, $4.C, $6.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002550 $$.S.makeUnsigned();
Reid Spencer57f28f92006-12-03 07:10:26 +00002551 }
2552 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002553 if ($4.C->getType() != $6.C->getType())
2554 error("fcmp operand types must match");
2555 $$.C = ConstantExpr::getCompare($2, $4.C, $6.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002556 $$.S.makeUnsigned();
Reid Spencer229e9362006-12-02 22:14:11 +00002557 }
Reid Spencere7c3c602006-11-30 06:36:44 +00002558 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002559 if (!$5.C->getType()->isInteger() ||
2560 cast<IntegerType>($5.C->getType())->getBitWidth() != 8)
2561 error("Shift count for shift constant must be unsigned byte");
Reid Spencer832254e2007-02-02 02:16:23 +00002562 const Type* Ty = $3.C->getType();
Reid Spencer950bf602007-01-26 08:19:09 +00002563 if (!$3.C->getType()->isInteger())
2564 error("Shift constant expression requires integer operand");
Reid Spencer832254e2007-02-02 02:16:23 +00002565 Constant *ShiftAmt = ConstantExpr::getZExt($5.C, Ty);
2566 $$.C = ConstantExpr::get(getBinaryOp($1, Ty, $3.S), $3.C, ShiftAmt);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002567 $$.S.copy($3.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002568 }
2569 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002570 if (!ExtractElementInst::isValidOperands($3.C, $5.C))
2571 error("Invalid extractelement operands");
2572 $$.C = ConstantExpr::getExtractElement($3.C, $5.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002573 $$.S.copy($3.S.get(0));
Reid Spencere7c3c602006-11-30 06:36:44 +00002574 }
2575 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002576 if (!InsertElementInst::isValidOperands($3.C, $5.C, $7.C))
2577 error("Invalid insertelement operands");
2578 $$.C = ConstantExpr::getInsertElement($3.C, $5.C, $7.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002579 $$.S.copy($3.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00002580 }
2581 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00002582 if (!ShuffleVectorInst::isValidOperands($3.C, $5.C, $7.C))
2583 error("Invalid shufflevector operands");
2584 $$.C = ConstantExpr::getShuffleVector($3.C, $5.C, $7.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002585 $$.S.copy($3.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002586 }
2587 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002588
2589
2590// ConstVector - A list of comma separated constants.
Reid Spencere77e35e2006-12-01 20:26:20 +00002591ConstVector
Reid Spencer950bf602007-01-26 08:19:09 +00002592 : ConstVector ',' ConstVal { ($$ = $1)->push_back($3); }
2593 | ConstVal {
2594 $$ = new std::vector<ConstInfo>();
2595 $$->push_back($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00002596 }
Reid Spencere77e35e2006-12-01 20:26:20 +00002597 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002598
2599
2600// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Reid Spencer950bf602007-01-26 08:19:09 +00002601GlobalType
2602 : GLOBAL { $$ = false; }
2603 | CONSTANT { $$ = true; }
2604 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002605
2606
2607//===----------------------------------------------------------------------===//
2608// Rules to match Modules
2609//===----------------------------------------------------------------------===//
2610
2611// Module rule: Capture the result of parsing the whole file into a result
2612// variable...
2613//
Reid Spencer950bf602007-01-26 08:19:09 +00002614Module
2615 : FunctionList {
2616 $$ = ParserResult = $1;
2617 CurModule.ModuleDone();
Reid Spencere7c3c602006-11-30 06:36:44 +00002618 }
Jeff Cohenac2dca92007-01-21 19:30:52 +00002619 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002620
Reid Spencer950bf602007-01-26 08:19:09 +00002621// FunctionList - A list of functions, preceeded by a constant pool.
2622//
2623FunctionList
2624 : FunctionList Function { $$ = $1; CurFun.FunctionDone(); }
2625 | FunctionList FunctionProto { $$ = $1; }
2626 | FunctionList MODULE ASM_TOK AsmBlock { $$ = $1; }
2627 | FunctionList IMPLEMENTATION { $$ = $1; }
2628 | ConstPool {
2629 $$ = CurModule.CurrentModule;
2630 // Emit an error if there are any unresolved types left.
2631 if (!CurModule.LateResolveTypes.empty()) {
2632 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
2633 if (DID.Type == ValID::NameVal) {
2634 error("Reference to an undefined type: '"+DID.getName() + "'");
2635 } else {
2636 error("Reference to an undefined type: #" + itostr(DID.Num));
2637 }
2638 }
2639 }
2640 ;
Reid Spencer78720742006-12-02 20:21:22 +00002641
Reid Spencere7c3c602006-11-30 06:36:44 +00002642// ConstPool - Constants with optional names assigned to them.
Reid Spencer950bf602007-01-26 08:19:09 +00002643ConstPool
2644 : ConstPool OptAssign TYPE TypesV {
2645 // Eagerly resolve types. This is not an optimization, this is a
2646 // requirement that is due to the fact that we could have this:
2647 //
2648 // %list = type { %list * }
2649 // %list = type { %list * } ; repeated type decl
2650 //
2651 // If types are not resolved eagerly, then the two types will not be
2652 // determined to be the same type!
2653 //
Reid Spencerbb1fd572007-03-21 17:15:50 +00002654 ResolveTypeTo($2, $4.PAT->get(), $4.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002655
Reid Spencerbb1fd572007-03-21 17:15:50 +00002656 if (!setTypeName($4, $2) && !$2) {
2657 // If this is a numbered type that is not a redefinition, add it to the
2658 // slot table.
2659 CurModule.Types.push_back($4.PAT->get());
2660 CurModule.TypeSigns.push_back($4.S);
Reid Spencera50d5962006-12-02 04:11:07 +00002661 }
Reid Spencered96d1e2007-02-08 09:08:52 +00002662 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00002663 }
2664 | ConstPool FunctionProto { // Function prototypes can be in const pool
Reid Spencere7c3c602006-11-30 06:36:44 +00002665 }
2666 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
Reid Spencere7c3c602006-11-30 06:36:44 +00002667 }
Reid Spencer950bf602007-01-26 08:19:09 +00002668 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
2669 if ($5.C == 0)
2670 error("Global value initializer is not a constant");
Reid Spencerbb1fd572007-03-21 17:15:50 +00002671 CurGV = ParseGlobalVariable($2, $3, $4, $5.C->getType(), $5.C, $5.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002672 } GlobalVarAttributes {
2673 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002674 }
Reid Spencer950bf602007-01-26 08:19:09 +00002675 | ConstPool OptAssign EXTERNAL GlobalType Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00002676 const Type *Ty = $5.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00002677 CurGV = ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, Ty, 0,
2678 $5.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002679 delete $5.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002680 } GlobalVarAttributes {
2681 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002682 }
Reid Spencer950bf602007-01-26 08:19:09 +00002683 | ConstPool OptAssign DLLIMPORT GlobalType Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00002684 const Type *Ty = $5.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00002685 CurGV = ParseGlobalVariable($2, GlobalValue::DLLImportLinkage, $4, Ty, 0,
2686 $5.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002687 delete $5.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002688 } GlobalVarAttributes {
2689 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002690 }
Reid Spencer950bf602007-01-26 08:19:09 +00002691 | ConstPool OptAssign EXTERN_WEAK GlobalType Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00002692 const Type *Ty = $5.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00002693 CurGV =
Reid Spencerbb1fd572007-03-21 17:15:50 +00002694 ParseGlobalVariable($2, GlobalValue::ExternalWeakLinkage, $4, Ty, 0,
2695 $5.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002696 delete $5.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002697 } GlobalVarAttributes {
2698 CurGV = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00002699 }
2700 | ConstPool TARGET TargetDefinition {
Reid Spencere7c3c602006-11-30 06:36:44 +00002701 }
2702 | ConstPool DEPLIBS '=' LibrariesDefinition {
Reid Spencere7c3c602006-11-30 06:36:44 +00002703 }
2704 | /* empty: end of list */ {
Reid Spencer950bf602007-01-26 08:19:09 +00002705 }
2706 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002707
Reid Spencer950bf602007-01-26 08:19:09 +00002708AsmBlock
2709 : STRINGCONSTANT {
2710 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
2711 char *EndStr = UnEscapeLexed($1, true);
2712 std::string NewAsm($1, EndStr);
2713 free($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00002714
Reid Spencer950bf602007-01-26 08:19:09 +00002715 if (AsmSoFar.empty())
2716 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
2717 else
2718 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
2719 }
2720 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002721
Reid Spencer950bf602007-01-26 08:19:09 +00002722BigOrLittle
Reid Spencerd7c4f8c2007-01-26 19:59:25 +00002723 : BIG { $$ = Module::BigEndian; }
Reid Spencer950bf602007-01-26 08:19:09 +00002724 | LITTLE { $$ = Module::LittleEndian; }
2725 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002726
2727TargetDefinition
2728 : ENDIAN '=' BigOrLittle {
Reid Spencer950bf602007-01-26 08:19:09 +00002729 CurModule.setEndianness($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00002730 }
2731 | POINTERSIZE '=' EUINT64VAL {
Reid Spencer950bf602007-01-26 08:19:09 +00002732 if ($3 == 32)
2733 CurModule.setPointerSize(Module::Pointer32);
2734 else if ($3 == 64)
2735 CurModule.setPointerSize(Module::Pointer64);
2736 else
2737 error("Invalid pointer size: '" + utostr($3) + "'");
Reid Spencere7c3c602006-11-30 06:36:44 +00002738 }
2739 | TRIPLE '=' STRINGCONSTANT {
Reid Spencer950bf602007-01-26 08:19:09 +00002740 CurModule.CurrentModule->setTargetTriple($3);
2741 free($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00002742 }
2743 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer950bf602007-01-26 08:19:09 +00002744 CurModule.CurrentModule->setDataLayout($3);
2745 free($3);
2746 }
2747 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002748
2749LibrariesDefinition
Reid Spencer950bf602007-01-26 08:19:09 +00002750 : '[' LibList ']'
2751 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002752
2753LibList
2754 : LibList ',' STRINGCONSTANT {
Reid Spencer950bf602007-01-26 08:19:09 +00002755 CurModule.CurrentModule->addLibrary($3);
2756 free($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00002757 }
Reid Spencer950bf602007-01-26 08:19:09 +00002758 | STRINGCONSTANT {
2759 CurModule.CurrentModule->addLibrary($1);
2760 free($1);
2761 }
2762 | /* empty: end of list */ { }
2763 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002764
2765//===----------------------------------------------------------------------===//
2766// Rules to match Function Headers
2767//===----------------------------------------------------------------------===//
2768
Reid Spencer950bf602007-01-26 08:19:09 +00002769Name
2770 : VAR_ID | STRINGCONSTANT
2771 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002772
Reid Spencer950bf602007-01-26 08:19:09 +00002773OptName
2774 : Name
2775 | /*empty*/ { $$ = 0; }
2776 ;
2777
2778ArgVal
2779 : Types OptName {
Reid Spencered96d1e2007-02-08 09:08:52 +00002780 if ($1.PAT->get() == Type::VoidTy)
Reid Spencer950bf602007-01-26 08:19:09 +00002781 error("void typed arguments are invalid");
2782 $$ = new std::pair<PATypeInfo, char*>($1, $2);
Reid Spencer52402b02007-01-02 05:45:11 +00002783 }
Reid Spencer950bf602007-01-26 08:19:09 +00002784 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002785
Reid Spencer950bf602007-01-26 08:19:09 +00002786ArgListH
2787 : ArgListH ',' ArgVal {
2788 $$ = $1;
2789 $$->push_back(*$3);
Reid Spencere77e35e2006-12-01 20:26:20 +00002790 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00002791 }
2792 | ArgVal {
Reid Spencer950bf602007-01-26 08:19:09 +00002793 $$ = new std::vector<std::pair<PATypeInfo,char*> >();
2794 $$->push_back(*$1);
2795 delete $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00002796 }
Reid Spencer950bf602007-01-26 08:19:09 +00002797 ;
2798
2799ArgList
2800 : ArgListH { $$ = $1; }
Reid Spencere7c3c602006-11-30 06:36:44 +00002801 | ArgListH ',' DOTDOTDOT {
Reid Spencere7c3c602006-11-30 06:36:44 +00002802 $$ = $1;
Reid Spencer950bf602007-01-26 08:19:09 +00002803 PATypeInfo VoidTI;
Reid Spencered96d1e2007-02-08 09:08:52 +00002804 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002805 VoidTI.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002806 $$->push_back(std::pair<PATypeInfo, char*>(VoidTI, 0));
Reid Spencere7c3c602006-11-30 06:36:44 +00002807 }
2808 | DOTDOTDOT {
Reid Spencer950bf602007-01-26 08:19:09 +00002809 $$ = new std::vector<std::pair<PATypeInfo,char*> >();
2810 PATypeInfo VoidTI;
Reid Spencered96d1e2007-02-08 09:08:52 +00002811 VoidTI.PAT = new PATypeHolder(Type::VoidTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002812 VoidTI.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00002813 $$->push_back(std::pair<PATypeInfo, char*>(VoidTI, 0));
Reid Spencere7c3c602006-11-30 06:36:44 +00002814 }
Reid Spencer950bf602007-01-26 08:19:09 +00002815 | /* empty */ { $$ = 0; }
2816 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00002817
Reid Spencer71d2ec92006-12-31 06:02:26 +00002818FunctionHeaderH
2819 : OptCallingConv TypesV Name '(' ArgList ')' OptSection OptAlign {
Reid Spencer950bf602007-01-26 08:19:09 +00002820 UnEscapeLexed($3);
2821 std::string FunctionName($3);
2822 free($3); // Free strdup'd memory!
Reid Spencere7c3c602006-11-30 06:36:44 +00002823
Reid Spencered96d1e2007-02-08 09:08:52 +00002824 const Type* RetTy = $2.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00002825
2826 if (!RetTy->isFirstClassType() && RetTy != Type::VoidTy)
2827 error("LLVM functions cannot return aggregate types");
2828
Reid Spencerbb1fd572007-03-21 17:15:50 +00002829 Signedness FTySign;
2830 FTySign.makeComposite($2.S);
Reid Spenceref9b9a72007-02-05 20:47:22 +00002831 std::vector<const Type*> ParamTyList;
Reid Spencer950bf602007-01-26 08:19:09 +00002832
2833 // In LLVM 2.0 the signatures of three varargs intrinsics changed to take
2834 // i8*. We check here for those names and override the parameter list
2835 // types to ensure the prototype is correct.
2836 if (FunctionName == "llvm.va_start" || FunctionName == "llvm.va_end") {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002837 ParamTyList.push_back(PointerType::get(Type::Int8Ty));
Reid Spencer950bf602007-01-26 08:19:09 +00002838 } else if (FunctionName == "llvm.va_copy") {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002839 ParamTyList.push_back(PointerType::get(Type::Int8Ty));
2840 ParamTyList.push_back(PointerType::get(Type::Int8Ty));
Reid Spencer950bf602007-01-26 08:19:09 +00002841 } else if ($5) { // If there are arguments...
2842 for (std::vector<std::pair<PATypeInfo,char*> >::iterator
2843 I = $5->begin(), E = $5->end(); I != E; ++I) {
Reid Spencered96d1e2007-02-08 09:08:52 +00002844 const Type *Ty = I->first.PAT->get();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002845 ParamTyList.push_back(Ty);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002846 FTySign.add(I->first.S);
Reid Spencer950bf602007-01-26 08:19:09 +00002847 }
2848 }
2849
Reid Spenceref9b9a72007-02-05 20:47:22 +00002850 bool isVarArg = ParamTyList.size() && ParamTyList.back() == Type::VoidTy;
2851 if (isVarArg)
2852 ParamTyList.pop_back();
Reid Spencer950bf602007-01-26 08:19:09 +00002853
Duncan Sandsdc024672007-11-27 13:23:08 +00002854 const FunctionType *FT = FunctionType::get(RetTy, ParamTyList, isVarArg);
Reid Spencer950bf602007-01-26 08:19:09 +00002855 const PointerType *PFT = PointerType::get(FT);
Reid Spencered96d1e2007-02-08 09:08:52 +00002856 delete $2.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002857
2858 ValID ID;
2859 if (!FunctionName.empty()) {
Reid Spencer5eb77c72007-03-15 03:26:42 +00002860 ID = ValID::create((char*)FunctionName.c_str());
Reid Spencer950bf602007-01-26 08:19:09 +00002861 } else {
Reid Spencer5eb77c72007-03-15 03:26:42 +00002862 ID = ValID::create((int)CurModule.Values[PFT].size());
Reid Spencer950bf602007-01-26 08:19:09 +00002863 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00002864 ID.S.makeComposite(FTySign);
Reid Spencer950bf602007-01-26 08:19:09 +00002865
2866 Function *Fn = 0;
Reid Spencered96d1e2007-02-08 09:08:52 +00002867 Module* M = CurModule.CurrentModule;
2868
Reid Spencer950bf602007-01-26 08:19:09 +00002869 // See if this function was forward referenced. If so, recycle the object.
2870 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2871 // Move the function to the end of the list, from whereever it was
2872 // previously inserted.
2873 Fn = cast<Function>(FWRef);
Reid Spencered96d1e2007-02-08 09:08:52 +00002874 M->getFunctionList().remove(Fn);
2875 M->getFunctionList().push_back(Fn);
2876 } else if (!FunctionName.empty()) {
2877 GlobalValue *Conflict = M->getFunction(FunctionName);
2878 if (!Conflict)
2879 Conflict = M->getNamedGlobal(FunctionName);
2880 if (Conflict && PFT == Conflict->getType()) {
2881 if (!CurFun.isDeclare && !Conflict->isDeclaration()) {
2882 // We have two function definitions that conflict, same type, same
2883 // name. We should really check to make sure that this is the result
2884 // of integer type planes collapsing and generate an error if it is
2885 // not, but we'll just rename on the assumption that it is. However,
2886 // let's do it intelligently and rename the internal linkage one
2887 // if there is one.
2888 std::string NewName(makeNameUnique(FunctionName));
2889 if (Conflict->hasInternalLinkage()) {
2890 Conflict->setName(NewName);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002891 RenameMapKey Key =
2892 makeRenameMapKey(FunctionName, Conflict->getType(), ID.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002893 CurModule.RenameMap[Key] = NewName;
2894 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
2895 InsertValue(Fn, CurModule.Values);
2896 } else {
2897 Fn = new Function(FT, CurFun.Linkage, NewName, M);
2898 InsertValue(Fn, CurModule.Values);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002899 RenameMapKey Key =
2900 makeRenameMapKey(FunctionName, PFT, ID.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002901 CurModule.RenameMap[Key] = NewName;
2902 }
2903 } else {
2904 // If they are not both definitions, then just use the function we
2905 // found since the types are the same.
2906 Fn = cast<Function>(Conflict);
Reid Spenceref9b9a72007-02-05 20:47:22 +00002907
Reid Spencered96d1e2007-02-08 09:08:52 +00002908 // Make sure to strip off any argument names so we can't get
2909 // conflicts.
2910 if (Fn->isDeclaration())
2911 for (Function::arg_iterator AI = Fn->arg_begin(),
2912 AE = Fn->arg_end(); AI != AE; ++AI)
2913 AI->setName("");
2914 }
2915 } else if (Conflict) {
Reid Spencere59f4932007-04-16 03:05:01 +00002916 // We have two globals with the same name and different types.
Reid Spencered96d1e2007-02-08 09:08:52 +00002917 // Previously, this was permitted because the symbol table had
2918 // "type planes" and names only needed to be distinct within a
2919 // type plane. After PR411 was fixed, this is no loner the case.
2920 // To resolve this we must rename one of the two.
2921 if (Conflict->hasInternalLinkage()) {
Reid Spencerbb1fd572007-03-21 17:15:50 +00002922 // We can safely rename the Conflict.
2923 RenameMapKey Key =
2924 makeRenameMapKey(Conflict->getName(), Conflict->getType(),
2925 CurModule.NamedValueSigns[Conflict->getName()]);
Reid Spencered96d1e2007-02-08 09:08:52 +00002926 Conflict->setName(makeNameUnique(Conflict->getName()));
Reid Spencered96d1e2007-02-08 09:08:52 +00002927 CurModule.RenameMap[Key] = Conflict->getName();
2928 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
2929 InsertValue(Fn, CurModule.Values);
Reid Spencerd2920cd2007-03-21 17:27:53 +00002930 } else {
Reid Spencered96d1e2007-02-08 09:08:52 +00002931 // We can't quietly rename either of these things, but we must
Reid Spencerd2920cd2007-03-21 17:27:53 +00002932 // rename one of them. Only if the function's linkage is internal can
2933 // we forgo a warning message about the renamed function.
Reid Spencered96d1e2007-02-08 09:08:52 +00002934 std::string NewName = makeNameUnique(FunctionName);
Reid Spencerd2920cd2007-03-21 17:27:53 +00002935 if (CurFun.Linkage != GlobalValue::InternalLinkage) {
2936 warning("Renaming function '" + FunctionName + "' as '" + NewName +
2937 "' may cause linkage errors");
2938 }
2939 // Elect to rename the thing we're now defining.
Reid Spencered96d1e2007-02-08 09:08:52 +00002940 Fn = new Function(FT, CurFun.Linkage, NewName, M);
2941 InsertValue(Fn, CurModule.Values);
Reid Spencerbb1fd572007-03-21 17:15:50 +00002942 RenameMapKey Key = makeRenameMapKey(FunctionName, PFT, ID.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00002943 CurModule.RenameMap[Key] = NewName;
Reid Spencerd2920cd2007-03-21 17:27:53 +00002944 }
Reid Spenceref9b9a72007-02-05 20:47:22 +00002945 } else {
Reid Spencered96d1e2007-02-08 09:08:52 +00002946 // There's no conflict, just define the function
2947 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
2948 InsertValue(Fn, CurModule.Values);
Reid Spenceref9b9a72007-02-05 20:47:22 +00002949 }
Reid Spencere59f4932007-04-16 03:05:01 +00002950 } else {
2951 // There's no conflict, just define the function
2952 Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
2953 InsertValue(Fn, CurModule.Values);
Reid Spencer950bf602007-01-26 08:19:09 +00002954 }
2955
Reid Spencere59f4932007-04-16 03:05:01 +00002956
Reid Spencer950bf602007-01-26 08:19:09 +00002957 CurFun.FunctionStart(Fn);
2958
2959 if (CurFun.isDeclare) {
2960 // If we have declaration, always overwrite linkage. This will allow us
2961 // to correctly handle cases, when pointer to function is passed as
2962 // argument to another function.
2963 Fn->setLinkage(CurFun.Linkage);
2964 }
Reid Spencerb7046c72007-01-29 05:41:34 +00002965 Fn->setCallingConv(upgradeCallingConv($1));
Reid Spencer950bf602007-01-26 08:19:09 +00002966 Fn->setAlignment($8);
2967 if ($7) {
2968 Fn->setSection($7);
2969 free($7);
2970 }
2971
Duncan Sandsdc024672007-11-27 13:23:08 +00002972 // Convert the CSRet calling convention into the corresponding parameter
2973 // attribute.
2974 if ($1 == OldCallingConv::CSRet) {
2975 ParamAttrsVector Attrs;
2976 ParamAttrsWithIndex PAWI;
2977 PAWI.index = 1; PAWI.attrs = ParamAttr::StructRet; // first arg
2978 Attrs.push_back(PAWI);
2979 Fn->setParamAttrs(ParamAttrsList::get(Attrs));
2980 }
2981
Reid Spencer950bf602007-01-26 08:19:09 +00002982 // Add all of the arguments we parsed to the function...
2983 if ($5) { // Is null if empty...
2984 if (isVarArg) { // Nuke the last entry
Reid Spencered96d1e2007-02-08 09:08:52 +00002985 assert($5->back().first.PAT->get() == Type::VoidTy &&
Reid Spencer950bf602007-01-26 08:19:09 +00002986 $5->back().second == 0 && "Not a varargs marker");
Reid Spencered96d1e2007-02-08 09:08:52 +00002987 delete $5->back().first.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00002988 $5->pop_back(); // Delete the last entry
2989 }
2990 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002991 Function::arg_iterator ArgEnd = Fn->arg_end();
2992 std::vector<std::pair<PATypeInfo,char*> >::iterator I = $5->begin();
2993 std::vector<std::pair<PATypeInfo,char*> >::iterator E = $5->end();
2994 for ( ; I != E && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencered96d1e2007-02-08 09:08:52 +00002995 delete I->first.PAT; // Delete the typeholder...
Reid Spencerbb1fd572007-03-21 17:15:50 +00002996 ValueInfo VI; VI.V = ArgIt; VI.S.copy(I->first.S);
2997 setValueName(VI, I->second); // Insert arg into symtab...
Reid Spencer950bf602007-01-26 08:19:09 +00002998 InsertValue(ArgIt);
2999 }
3000 delete $5; // We're now done with the argument list
3001 }
Reid Spencer7eea8ff2007-05-18 05:48:07 +00003002 lastCallingConv = OldCallingConv::C;
Reid Spencer950bf602007-01-26 08:19:09 +00003003 }
3004 ;
3005
3006BEGIN
3007 : BEGINTOK | '{' // Allow BEGIN or '{' to start a function
Jeff Cohenac2dca92007-01-21 19:30:52 +00003008 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003009
Reid Spencer6fd36ab2006-12-29 20:35:03 +00003010FunctionHeader
Reid Spencerd2920cd2007-03-21 17:27:53 +00003011 : OptLinkage { CurFun.Linkage = $1; } FunctionHeaderH BEGIN {
Reid Spencer950bf602007-01-26 08:19:09 +00003012 $$ = CurFun.CurrentFunction;
3013
3014 // Make sure that we keep track of the linkage type even if there was a
3015 // previous "declare".
3016 $$->setLinkage($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00003017 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00003018 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003019
Reid Spencer950bf602007-01-26 08:19:09 +00003020END
3021 : ENDTOK | '}' // Allow end of '}' to end a function
3022 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003023
Reid Spencer950bf602007-01-26 08:19:09 +00003024Function
3025 : BasicBlockList END {
3026 $$ = $1;
3027 };
Reid Spencere7c3c602006-11-30 06:36:44 +00003028
Reid Spencere77e35e2006-12-01 20:26:20 +00003029FnDeclareLinkage
Reid Spencered96d1e2007-02-08 09:08:52 +00003030 : /*default*/ { $$ = GlobalValue::ExternalLinkage; }
3031 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
3032 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencere7c3c602006-11-30 06:36:44 +00003033 ;
3034
3035FunctionProto
Reid Spencered96d1e2007-02-08 09:08:52 +00003036 : DECLARE { CurFun.isDeclare = true; }
3037 FnDeclareLinkage { CurFun.Linkage = $3; } FunctionHeaderH {
Reid Spencer950bf602007-01-26 08:19:09 +00003038 $$ = CurFun.CurrentFunction;
3039 CurFun.FunctionDone();
3040
3041 }
3042 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003043
3044//===----------------------------------------------------------------------===//
3045// Rules to match Basic Blocks
3046//===----------------------------------------------------------------------===//
3047
Reid Spencer950bf602007-01-26 08:19:09 +00003048OptSideEffect
3049 : /* empty */ { $$ = false; }
3050 | SIDEEFFECT { $$ = true; }
3051 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003052
Reid Spencere77e35e2006-12-01 20:26:20 +00003053ConstValueRef
Reid Spencer950bf602007-01-26 08:19:09 +00003054 // A reference to a direct constant
Reid Spencerbb1fd572007-03-21 17:15:50 +00003055 : ESINT64VAL { $$ = ValID::create($1); }
Reid Spencer950bf602007-01-26 08:19:09 +00003056 | EUINT64VAL { $$ = ValID::create($1); }
3057 | FPVAL { $$ = ValID::create($1); }
Reid Spencerbb1fd572007-03-21 17:15:50 +00003058 | TRUETOK {
3059 $$ = ValID::create(ConstantInt::get(Type::Int1Ty, true));
3060 $$.S.makeUnsigned();
3061 }
3062 | FALSETOK {
3063 $$ = ValID::create(ConstantInt::get(Type::Int1Ty, false));
3064 $$.S.makeUnsigned();
3065 }
Reid Spencer950bf602007-01-26 08:19:09 +00003066 | NULL_TOK { $$ = ValID::createNull(); }
3067 | UNDEF { $$ = ValID::createUndef(); }
3068 | ZEROINITIALIZER { $$ = ValID::createZeroInit(); }
3069 | '<' ConstVector '>' { // Nonempty unsized packed vector
3070 const Type *ETy = (*$2)[0].C->getType();
3071 int NumElements = $2->size();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003072 VectorType* pt = VectorType::get(ETy, NumElements);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003073 $$.S.makeComposite((*$2)[0].S);
3074 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(pt, $$.S));
Reid Spencer950bf602007-01-26 08:19:09 +00003075
3076 // Verify all elements are correct type!
3077 std::vector<Constant*> Elems;
3078 for (unsigned i = 0; i < $2->size(); i++) {
3079 Constant *C = (*$2)[i].C;
3080 const Type *CTy = C->getType();
3081 if (ETy != CTy)
3082 error("Element #" + utostr(i) + " is not of type '" +
3083 ETy->getDescription() +"' as required!\nIt is of type '" +
3084 CTy->getDescription() + "'");
3085 Elems.push_back(C);
Reid Spencere7c3c602006-11-30 06:36:44 +00003086 }
Reid Spencer5eb77c72007-03-15 03:26:42 +00003087 $$ = ValID::create(ConstantVector::get(pt, Elems));
Reid Spencer950bf602007-01-26 08:19:09 +00003088 delete PTy; delete $2;
3089 }
3090 | ConstExpr {
Reid Spencer5eb77c72007-03-15 03:26:42 +00003091 $$ = ValID::create($1.C);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003092 $$.S.copy($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003093 }
3094 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
3095 char *End = UnEscapeLexed($3, true);
3096 std::string AsmStr = std::string($3, End);
3097 End = UnEscapeLexed($5, true);
3098 std::string Constraints = std::string($5, End);
3099 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
3100 free($3);
3101 free($5);
3102 }
3103 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003104
Reid Spencerbb1fd572007-03-21 17:15:50 +00003105// SymbolicValueRef - Reference to one of two ways of symbolically refering to // another value.
Reid Spencer950bf602007-01-26 08:19:09 +00003106//
3107SymbolicValueRef
Reid Spencerbb1fd572007-03-21 17:15:50 +00003108 : INTVAL { $$ = ValID::create($1); $$.S.makeSignless(); }
3109 | Name { $$ = ValID::create($1); $$.S.makeSignless(); }
Reid Spencer950bf602007-01-26 08:19:09 +00003110 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003111
3112// ValueRef - A reference to a definition... either constant or symbolic
Reid Spencerf459d392006-12-02 16:19:52 +00003113ValueRef
Reid Spencer950bf602007-01-26 08:19:09 +00003114 : SymbolicValueRef | ConstValueRef
Reid Spencerf459d392006-12-02 16:19:52 +00003115 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003116
Reid Spencer950bf602007-01-26 08:19:09 +00003117
Reid Spencere7c3c602006-11-30 06:36:44 +00003118// ResolvedVal - a <type> <value> pair. This is used only in cases where the
3119// type immediately preceeds the value reference, and allows complex constant
3120// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Reid Spencer950bf602007-01-26 08:19:09 +00003121ResolvedVal
3122 : Types ValueRef {
Reid Spencered96d1e2007-02-08 09:08:52 +00003123 const Type *Ty = $1.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003124 $2.S.copy($1.S);
Reid Spencer5eb77c72007-03-15 03:26:42 +00003125 $$.V = getVal(Ty, $2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003126 $$.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003127 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003128 }
Reid Spencer950bf602007-01-26 08:19:09 +00003129 ;
3130
3131BasicBlockList
3132 : BasicBlockList BasicBlock {
3133 $$ = $1;
3134 }
3135 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
3136 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00003137 };
3138
3139
3140// Basic blocks are terminated by branching instructions:
3141// br, br/cc, switch, ret
3142//
Reid Spencer950bf602007-01-26 08:19:09 +00003143BasicBlock
3144 : InstructionList OptAssign BBTerminatorInst {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003145 ValueInfo VI; VI.V = $3.TI; VI.S.copy($3.S);
3146 setValueName(VI, $2);
3147 InsertValue($3.TI);
3148 $1->getInstList().push_back($3.TI);
Reid Spencer950bf602007-01-26 08:19:09 +00003149 InsertValue($1);
Reid Spencere7c3c602006-11-30 06:36:44 +00003150 $$ = $1;
3151 }
Reid Spencer950bf602007-01-26 08:19:09 +00003152 ;
3153
3154InstructionList
3155 : InstructionList Inst {
3156 if ($2.I)
3157 $1->getInstList().push_back($2.I);
3158 $$ = $1;
3159 }
3160 | /* empty */ {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003161 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++),true);
Reid Spencer950bf602007-01-26 08:19:09 +00003162 // Make sure to move the basic block to the correct location in the
3163 // function, instead of leaving it inserted wherever it was first
3164 // referenced.
3165 Function::BasicBlockListType &BBL =
3166 CurFun.CurrentFunction->getBasicBlockList();
3167 BBL.splice(BBL.end(), BBL, $$);
3168 }
3169 | LABELSTR {
Reid Spencer5eb77c72007-03-15 03:26:42 +00003170 $$ = CurBB = getBBVal(ValID::create($1), true);
Reid Spencer950bf602007-01-26 08:19:09 +00003171 // Make sure to move the basic block to the correct location in the
3172 // function, instead of leaving it inserted wherever it was first
3173 // referenced.
3174 Function::BasicBlockListType &BBL =
3175 CurFun.CurrentFunction->getBasicBlockList();
3176 BBL.splice(BBL.end(), BBL, $$);
3177 }
3178 ;
3179
3180Unwind : UNWIND | EXCEPT;
3181
3182BBTerminatorInst
3183 : RET ResolvedVal { // Return with a result...
Reid Spencerbb1fd572007-03-21 17:15:50 +00003184 $$.TI = new ReturnInst($2.V);
3185 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003186 }
3187 | RET VOID { // Return with no result...
Reid Spencerbb1fd572007-03-21 17:15:50 +00003188 $$.TI = new ReturnInst();
3189 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003190 }
3191 | BR LABEL ValueRef { // Unconditional Branch...
3192 BasicBlock* tmpBB = getBBVal($3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003193 $$.TI = new BranchInst(tmpBB);
3194 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003195 } // Conditional Branch...
3196 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003197 $6.S.makeSignless();
3198 $9.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003199 BasicBlock* tmpBBA = getBBVal($6);
3200 BasicBlock* tmpBBB = getBBVal($9);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003201 $3.S.makeUnsigned();
Reid Spencer950bf602007-01-26 08:19:09 +00003202 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003203 $$.TI = new BranchInst(tmpBBA, tmpBBB, tmpVal);
3204 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003205 }
3206 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003207 $3.S.copy($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003208 Value* tmpVal = getVal($2.T, $3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003209 $6.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003210 BasicBlock* tmpBB = getBBVal($6);
3211 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Reid Spencerbb1fd572007-03-21 17:15:50 +00003212 $$.TI = S;
3213 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003214 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
3215 E = $8->end();
3216 for (; I != E; ++I) {
3217 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
3218 S->addCase(CI, I->second);
3219 else
3220 error("Switch case is constant, but not a simple integer");
3221 }
3222 delete $8;
3223 }
3224 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003225 $3.S.copy($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003226 Value* tmpVal = getVal($2.T, $3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003227 $6.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003228 BasicBlock* tmpBB = getBBVal($6);
3229 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003230 $$.TI = S;
3231 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003232 }
3233 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
3234 TO LABEL ValueRef Unwind LABEL ValueRef {
3235 const PointerType *PFTy;
3236 const FunctionType *Ty;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003237 Signedness FTySign;
Reid Spencer950bf602007-01-26 08:19:09 +00003238
Reid Spencered96d1e2007-02-08 09:08:52 +00003239 if (!(PFTy = dyn_cast<PointerType>($3.PAT->get())) ||
Reid Spencer950bf602007-01-26 08:19:09 +00003240 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3241 // Pull out the types of all of the arguments...
3242 std::vector<const Type*> ParamTypes;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003243 FTySign.makeComposite($3.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003244 if ($6) {
3245 for (std::vector<ValueInfo>::iterator I = $6->begin(), E = $6->end();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003246 I != E; ++I) {
Reid Spencer950bf602007-01-26 08:19:09 +00003247 ParamTypes.push_back((*I).V->getType());
Reid Spencerbb1fd572007-03-21 17:15:50 +00003248 FTySign.add(I->S);
3249 }
Reid Spencer950bf602007-01-26 08:19:09 +00003250 }
3251 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
3252 if (isVarArg) ParamTypes.pop_back();
Duncan Sandsdc024672007-11-27 13:23:08 +00003253 Ty = FunctionType::get($3.PAT->get(), ParamTypes, isVarArg);
Reid Spencer950bf602007-01-26 08:19:09 +00003254 PFTy = PointerType::get(Ty);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003255 $$.S.copy($3.S);
3256 } else {
3257 FTySign = $3.S;
Reid Spencera3b12dd2007-04-07 16:14:01 +00003258 // Get the signedness of the result type. $3 is the pointer to the
3259 // function type so we get the 0th element to extract the function type,
3260 // and then the 0th element again to get the result type.
3261 $$.S.copy($3.S.get(0).get(0));
Reid Spencer950bf602007-01-26 08:19:09 +00003262 }
Reid Spencer7b5d4662007-04-09 06:16:21 +00003263
Reid Spencerbb1fd572007-03-21 17:15:50 +00003264 $4.S.makeComposite(FTySign);
Reid Spencer950bf602007-01-26 08:19:09 +00003265 Value *V = getVal(PFTy, $4); // Get the function we're calling...
3266 BasicBlock *Normal = getBBVal($10);
3267 BasicBlock *Except = getBBVal($13);
3268
3269 // Create the call node...
3270 if (!$6) { // Has no arguments?
David Greene5fd22a82007-09-04 18:46:50 +00003271 std::vector<Value*> Args;
3272 $$.TI = new InvokeInst(V, Normal, Except, Args.begin(), Args.end());
Reid Spencer950bf602007-01-26 08:19:09 +00003273 } else { // Has arguments?
3274 // Loop through FunctionType's arguments and ensure they are specified
3275 // correctly!
3276 //
3277 FunctionType::param_iterator I = Ty->param_begin();
3278 FunctionType::param_iterator E = Ty->param_end();
3279 std::vector<ValueInfo>::iterator ArgI = $6->begin(), ArgE = $6->end();
3280
3281 std::vector<Value*> Args;
3282 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
3283 if ((*ArgI).V->getType() != *I)
3284 error("Parameter " +(*ArgI).V->getName()+ " is not of type '" +
3285 (*I)->getDescription() + "'");
3286 Args.push_back((*ArgI).V);
3287 }
3288
3289 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
3290 error("Invalid number of parameters detected");
3291
David Greene5fd22a82007-09-04 18:46:50 +00003292 $$.TI = new InvokeInst(V, Normal, Except, Args.begin(), Args.end());
Reid Spencer950bf602007-01-26 08:19:09 +00003293 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00003294 cast<InvokeInst>($$.TI)->setCallingConv(upgradeCallingConv($2));
Duncan Sandsdc024672007-11-27 13:23:08 +00003295 if ($2 == OldCallingConv::CSRet) {
3296 ParamAttrsVector Attrs;
3297 ParamAttrsWithIndex PAWI;
3298 PAWI.index = 1; PAWI.attrs = ParamAttr::StructRet; // first arg
3299 Attrs.push_back(PAWI);
3300 cast<InvokeInst>($$.TI)->setParamAttrs(ParamAttrsList::get(Attrs));
3301 }
Reid Spencered96d1e2007-02-08 09:08:52 +00003302 delete $3.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003303 delete $6;
Reid Spencer7eea8ff2007-05-18 05:48:07 +00003304 lastCallingConv = OldCallingConv::C;
Reid Spencer950bf602007-01-26 08:19:09 +00003305 }
3306 | Unwind {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003307 $$.TI = new UnwindInst();
3308 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003309 }
3310 | UNREACHABLE {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003311 $$.TI = new UnreachableInst();
3312 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003313 }
3314 ;
3315
3316JumpTable
3317 : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
3318 $$ = $1;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003319 $3.S.copy($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003320 Constant *V = cast<Constant>(getExistingValue($2.T, $3));
3321
3322 if (V == 0)
3323 error("May only switch on a constant pool value");
3324
Reid Spencerbb1fd572007-03-21 17:15:50 +00003325 $6.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003326 BasicBlock* tmpBB = getBBVal($6);
3327 $$->push_back(std::make_pair(V, tmpBB));
3328 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003329 | IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencer950bf602007-01-26 08:19:09 +00003330 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003331 $2.S.copy($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003332 Constant *V = cast<Constant>(getExistingValue($1.T, $2));
3333
3334 if (V == 0)
3335 error("May only switch on a constant pool value");
3336
Reid Spencerbb1fd572007-03-21 17:15:50 +00003337 $5.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003338 BasicBlock* tmpBB = getBBVal($5);
3339 $$->push_back(std::make_pair(V, tmpBB));
3340 }
3341 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003342
3343Inst
3344 : OptAssign InstVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003345 bool omit = false;
3346 if ($1)
3347 if (BitCastInst *BCI = dyn_cast<BitCastInst>($2.I))
3348 if (BCI->getSrcTy() == BCI->getDestTy() &&
3349 BCI->getOperand(0)->getName() == $1)
3350 // This is a useless bit cast causing a name redefinition. It is
3351 // a bit cast from a type to the same type of an operand with the
3352 // same name as the name we would give this instruction. Since this
3353 // instruction results in no code generation, it is safe to omit
3354 // the instruction. This situation can occur because of collapsed
3355 // type planes. For example:
3356 // %X = add int %Y, %Z
3357 // %X = cast int %Y to uint
3358 // After upgrade, this looks like:
3359 // %X = add i32 %Y, %Z
3360 // %X = bitcast i32 to i32
3361 // The bitcast is clearly useless so we omit it.
3362 omit = true;
3363 if (omit) {
3364 $$.I = 0;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003365 $$.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003366 } else {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003367 ValueInfo VI; VI.V = $2.I; VI.S.copy($2.S);
3368 setValueName(VI, $1);
Reid Spencer950bf602007-01-26 08:19:09 +00003369 InsertValue($2.I);
3370 $$ = $2;
Reid Spencerf5626a32007-01-01 01:20:41 +00003371 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003372 };
3373
Reid Spencer950bf602007-01-26 08:19:09 +00003374PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
3375 $$.P = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003376 $$.S.copy($1.S);
3377 $3.S.copy($1.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003378 Value* tmpVal = getVal($1.PAT->get(), $3);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003379 $5.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003380 BasicBlock* tmpBB = getBBVal($5);
3381 $$.P->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencered96d1e2007-02-08 09:08:52 +00003382 delete $1.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003383 }
3384 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
Reid Spencere7c3c602006-11-30 06:36:44 +00003385 $$ = $1;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003386 $4.S.copy($1.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003387 Value* tmpVal = getVal($1.P->front().first->getType(), $4);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003388 $6.S.makeSignless();
Reid Spencer950bf602007-01-26 08:19:09 +00003389 BasicBlock* tmpBB = getBBVal($6);
3390 $1.P->push_back(std::make_pair(tmpVal, tmpBB));
3391 }
3392 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003393
Reid Spencer950bf602007-01-26 08:19:09 +00003394ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
3395 $$ = new std::vector<ValueInfo>();
Reid Spencerf8483652006-12-02 15:16:01 +00003396 $$->push_back($1);
3397 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003398 | ValueRefList ',' ResolvedVal {
Reid Spencere7c3c602006-11-30 06:36:44 +00003399 $$ = $1;
Reid Spencer950bf602007-01-26 08:19:09 +00003400 $1->push_back($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00003401 };
3402
3403// ValueRefListE - Just like ValueRefList, except that it may also be empty!
3404ValueRefListE
Reid Spencer950bf602007-01-26 08:19:09 +00003405 : ValueRefList
3406 | /*empty*/ { $$ = 0; }
Reid Spencere7c3c602006-11-30 06:36:44 +00003407 ;
3408
3409OptTailCall
3410 : TAIL CALL {
Reid Spencer950bf602007-01-26 08:19:09 +00003411 $$ = true;
Reid Spencere7c3c602006-11-30 06:36:44 +00003412 }
Reid Spencer950bf602007-01-26 08:19:09 +00003413 | CALL {
3414 $$ = false;
3415 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003416 ;
3417
Reid Spencer950bf602007-01-26 08:19:09 +00003418InstVal
3419 : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003420 $3.S.copy($2.S);
3421 $5.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003422 const Type* Ty = $2.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003423 if (!Ty->isInteger() && !Ty->isFloatingPoint() && !isa<VectorType>(Ty))
Reid Spencer950bf602007-01-26 08:19:09 +00003424 error("Arithmetic operator requires integer, FP, or packed operands");
Reid Spencer9d6565a2007-02-15 02:26:10 +00003425 if (isa<VectorType>(Ty) &&
Reid Spencer950bf602007-01-26 08:19:09 +00003426 ($1 == URemOp || $1 == SRemOp || $1 == FRemOp || $1 == RemOp))
Chris Lattner4227bdb2007-02-19 07:34:02 +00003427 error("Remainder not supported on vector types");
Reid Spencer950bf602007-01-26 08:19:09 +00003428 // Upgrade the opcode from obsolete versions before we do anything with it.
3429 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $2.S);
3430 Value* val1 = getVal(Ty, $3);
3431 Value* val2 = getVal(Ty, $5);
3432 $$.I = BinaryOperator::create(Opcode, val1, val2);
3433 if ($$.I == 0)
3434 error("binary operator returned null");
Reid Spencerbb1fd572007-03-21 17:15:50 +00003435 $$.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003436 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003437 }
3438 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003439 $3.S.copy($2.S);
3440 $5.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003441 const Type *Ty = $2.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003442 if (!Ty->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00003443 if (!isa<VectorType>(Ty) ||
3444 !cast<VectorType>(Ty)->getElementType()->isInteger())
Reid Spencer950bf602007-01-26 08:19:09 +00003445 error("Logical operator requires integral operands");
3446 }
3447 Instruction::BinaryOps Opcode = getBinaryOp($1, Ty, $2.S);
3448 Value* tmpVal1 = getVal(Ty, $3);
3449 Value* tmpVal2 = getVal(Ty, $5);
3450 $$.I = BinaryOperator::create(Opcode, tmpVal1, tmpVal2);
3451 if ($$.I == 0)
3452 error("binary operator returned null");
Reid Spencerbb1fd572007-03-21 17:15:50 +00003453 $$.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003454 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003455 }
3456 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003457 $3.S.copy($2.S);
3458 $5.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003459 const Type* Ty = $2.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003460 if(isa<VectorType>(Ty))
3461 error("VectorTypes currently not supported in setcc instructions");
Reid Spencer950bf602007-01-26 08:19:09 +00003462 unsigned short pred;
3463 Instruction::OtherOps Opcode = getCompareOp($1, pred, Ty, $2.S);
3464 Value* tmpVal1 = getVal(Ty, $3);
3465 Value* tmpVal2 = getVal(Ty, $5);
3466 $$.I = CmpInst::create(Opcode, pred, tmpVal1, tmpVal2);
3467 if ($$.I == 0)
3468 error("binary operator returned null");
Reid Spencerbb1fd572007-03-21 17:15:50 +00003469 $$.S.makeUnsigned();
Reid Spencered96d1e2007-02-08 09:08:52 +00003470 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003471 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00003472 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003473 $4.S.copy($3.S);
3474 $6.S.copy($3.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003475 const Type *Ty = $3.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003476 if (isa<VectorType>(Ty))
3477 error("VectorTypes currently not supported in icmp instructions");
Reid Spencer950bf602007-01-26 08:19:09 +00003478 else if (!Ty->isInteger() && !isa<PointerType>(Ty))
3479 error("icmp requires integer or pointer typed operands");
3480 Value* tmpVal1 = getVal(Ty, $4);
3481 Value* tmpVal2 = getVal(Ty, $6);
3482 $$.I = new ICmpInst($2, tmpVal1, tmpVal2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003483 $$.S.makeUnsigned();
Reid Spencered96d1e2007-02-08 09:08:52 +00003484 delete $3.PAT;
Reid Spencer57f28f92006-12-03 07:10:26 +00003485 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00003486 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003487 $4.S.copy($3.S);
3488 $6.S.copy($3.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003489 const Type *Ty = $3.PAT->get();
Reid Spencer9d6565a2007-02-15 02:26:10 +00003490 if (isa<VectorType>(Ty))
3491 error("VectorTypes currently not supported in fcmp instructions");
Reid Spencer950bf602007-01-26 08:19:09 +00003492 else if (!Ty->isFloatingPoint())
3493 error("fcmp instruction requires floating point operands");
3494 Value* tmpVal1 = getVal(Ty, $4);
3495 Value* tmpVal2 = getVal(Ty, $6);
3496 $$.I = new FCmpInst($2, tmpVal1, tmpVal2);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003497 $$.S.makeUnsigned();
Reid Spencered96d1e2007-02-08 09:08:52 +00003498 delete $3.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003499 }
3500 | NOT ResolvedVal {
3501 warning("Use of obsolete 'not' instruction: Replacing with 'xor");
3502 const Type *Ty = $2.V->getType();
3503 Value *Ones = ConstantInt::getAllOnesValue(Ty);
3504 if (Ones == 0)
3505 error("Expected integral type for not instruction");
3506 $$.I = BinaryOperator::create(Instruction::Xor, $2.V, Ones);
3507 if ($$.I == 0)
3508 error("Could not create a xor instruction");
Reid Spencerbb1fd572007-03-21 17:15:50 +00003509 $$.S.copy($2.S);
Reid Spencer229e9362006-12-02 22:14:11 +00003510 }
Reid Spencere7c3c602006-11-30 06:36:44 +00003511 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003512 if (!$4.V->getType()->isInteger() ||
3513 cast<IntegerType>($4.V->getType())->getBitWidth() != 8)
3514 error("Shift amount must be int8");
Reid Spencer832254e2007-02-02 02:16:23 +00003515 const Type* Ty = $2.V->getType();
3516 if (!Ty->isInteger())
Reid Spencer950bf602007-01-26 08:19:09 +00003517 error("Shift constant expression requires integer operand");
Reid Spencer832254e2007-02-02 02:16:23 +00003518 Value* ShiftAmt = 0;
3519 if (cast<IntegerType>(Ty)->getBitWidth() > Type::Int8Ty->getBitWidth())
3520 if (Constant *C = dyn_cast<Constant>($4.V))
3521 ShiftAmt = ConstantExpr::getZExt(C, Ty);
3522 else
3523 ShiftAmt = new ZExtInst($4.V, Ty, makeNameUnique("shift"), CurBB);
3524 else
3525 ShiftAmt = $4.V;
3526 $$.I = BinaryOperator::create(getBinaryOp($1, Ty, $2.S), $2.V, ShiftAmt);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003527 $$.S.copy($2.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00003528 }
Reid Spencerfcb5df82006-12-01 22:34:43 +00003529 | CastOps ResolvedVal TO Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00003530 const Type *DstTy = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003531 if (!DstTy->isFirstClassType())
3532 error("cast instruction to a non-primitive type: '" +
3533 DstTy->getDescription() + "'");
3534 $$.I = cast<Instruction>(getCast($1, $2.V, $2.S, DstTy, $4.S, true));
Reid Spencerbb1fd572007-03-21 17:15:50 +00003535 $$.S.copy($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003536 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003537 }
3538 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003539 if (!$2.V->getType()->isInteger() ||
3540 cast<IntegerType>($2.V->getType())->getBitWidth() != 1)
3541 error("select condition must be bool");
3542 if ($4.V->getType() != $6.V->getType())
3543 error("select value types should match");
3544 $$.I = new SelectInst($2.V, $4.V, $6.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003545 $$.S.copy($4.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00003546 }
3547 | VAARG ResolvedVal ',' Types {
Reid Spencered96d1e2007-02-08 09:08:52 +00003548 const Type *Ty = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003549 NewVarArgs = true;
3550 $$.I = new VAArgInst($2.V, Ty);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003551 $$.S.copy($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003552 delete $4.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003553 }
3554 | VAARG_old ResolvedVal ',' Types {
3555 const Type* ArgTy = $2.V->getType();
Reid Spencered96d1e2007-02-08 09:08:52 +00003556 const Type* DstTy = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003557 ObsoleteVarArgs = true;
3558 Function* NF = cast<Function>(CurModule.CurrentModule->
3559 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0));
3560
3561 //b = vaarg a, t ->
3562 //foo = alloca 1 of t
3563 //bar = vacopy a
3564 //store bar -> foo
3565 //b = vaarg foo, t
3566 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix");
3567 CurBB->getInstList().push_back(foo);
3568 CallInst* bar = new CallInst(NF, $2.V);
3569 CurBB->getInstList().push_back(bar);
3570 CurBB->getInstList().push_back(new StoreInst(bar, foo));
3571 $$.I = new VAArgInst(foo, DstTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003572 $$.S.copy($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003573 delete $4.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003574 }
3575 | VANEXT_old ResolvedVal ',' Types {
3576 const Type* ArgTy = $2.V->getType();
Reid Spencered96d1e2007-02-08 09:08:52 +00003577 const Type* DstTy = $4.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003578 ObsoleteVarArgs = true;
3579 Function* NF = cast<Function>(CurModule.CurrentModule->
3580 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0));
3581
3582 //b = vanext a, t ->
3583 //foo = alloca 1 of t
3584 //bar = vacopy a
3585 //store bar -> foo
3586 //tmp = vaarg foo, t
3587 //b = load foo
3588 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix");
3589 CurBB->getInstList().push_back(foo);
3590 CallInst* bar = new CallInst(NF, $2.V);
3591 CurBB->getInstList().push_back(bar);
3592 CurBB->getInstList().push_back(new StoreInst(bar, foo));
3593 Instruction* tmp = new VAArgInst(foo, DstTy);
3594 CurBB->getInstList().push_back(tmp);
3595 $$.I = new LoadInst(foo);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003596 $$.S.copy($4.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003597 delete $4.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003598 }
3599 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003600 if (!ExtractElementInst::isValidOperands($2.V, $4.V))
3601 error("Invalid extractelement operands");
3602 $$.I = new ExtractElementInst($2.V, $4.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003603 $$.S.copy($2.S.get(0));
Reid Spencere7c3c602006-11-30 06:36:44 +00003604 }
3605 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003606 if (!InsertElementInst::isValidOperands($2.V, $4.V, $6.V))
3607 error("Invalid insertelement operands");
3608 $$.I = new InsertElementInst($2.V, $4.V, $6.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003609 $$.S.copy($2.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00003610 }
3611 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003612 if (!ShuffleVectorInst::isValidOperands($2.V, $4.V, $6.V))
3613 error("Invalid shufflevector operands");
3614 $$.I = new ShuffleVectorInst($2.V, $4.V, $6.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003615 $$.S.copy($2.S);
Reid Spencere7c3c602006-11-30 06:36:44 +00003616 }
3617 | PHI_TOK PHIList {
Reid Spencer950bf602007-01-26 08:19:09 +00003618 const Type *Ty = $2.P->front().first->getType();
3619 if (!Ty->isFirstClassType())
3620 error("PHI node operands must be of first class type");
3621 PHINode *PHI = new PHINode(Ty);
3622 PHI->reserveOperandSpace($2.P->size());
3623 while ($2.P->begin() != $2.P->end()) {
3624 if ($2.P->front().first->getType() != Ty)
3625 error("All elements of a PHI node must be of the same type");
3626 PHI->addIncoming($2.P->front().first, $2.P->front().second);
3627 $2.P->pop_front();
3628 }
3629 $$.I = PHI;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003630 $$.S.copy($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003631 delete $2.P; // Free the list...
Reid Spencere7c3c602006-11-30 06:36:44 +00003632 }
Reid Spencer7b5d4662007-04-09 06:16:21 +00003633 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Reid Spencer950bf602007-01-26 08:19:09 +00003634 // Handle the short call syntax
3635 const PointerType *PFTy;
3636 const FunctionType *FTy;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003637 Signedness FTySign;
Reid Spencered96d1e2007-02-08 09:08:52 +00003638 if (!(PFTy = dyn_cast<PointerType>($3.PAT->get())) ||
Reid Spencer950bf602007-01-26 08:19:09 +00003639 !(FTy = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3640 // Pull out the types of all of the arguments...
3641 std::vector<const Type*> ParamTypes;
Reid Spencerbb1fd572007-03-21 17:15:50 +00003642 FTySign.makeComposite($3.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003643 if ($6) {
3644 for (std::vector<ValueInfo>::iterator I = $6->begin(), E = $6->end();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003645 I != E; ++I) {
Reid Spencer950bf602007-01-26 08:19:09 +00003646 ParamTypes.push_back((*I).V->getType());
Reid Spencerbb1fd572007-03-21 17:15:50 +00003647 FTySign.add(I->S);
3648 }
Reid Spencerc4d96252007-01-13 00:03:30 +00003649 }
Reid Spencer950bf602007-01-26 08:19:09 +00003650
3651 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
3652 if (isVarArg) ParamTypes.pop_back();
3653
Reid Spencered96d1e2007-02-08 09:08:52 +00003654 const Type *RetTy = $3.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003655 if (!RetTy->isFirstClassType() && RetTy != Type::VoidTy)
3656 error("Functions cannot return aggregate types");
3657
Duncan Sandsdc024672007-11-27 13:23:08 +00003658 FTy = FunctionType::get(RetTy, ParamTypes, isVarArg);
Reid Spencer950bf602007-01-26 08:19:09 +00003659 PFTy = PointerType::get(FTy);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003660 $$.S.copy($3.S);
3661 } else {
3662 FTySign = $3.S;
Reid Spencera3b12dd2007-04-07 16:14:01 +00003663 // Get the signedness of the result type. $3 is the pointer to the
3664 // function type so we get the 0th element to extract the function type,
3665 // and then the 0th element again to get the result type.
3666 $$.S.copy($3.S.get(0).get(0));
Reid Spencerf8483652006-12-02 15:16:01 +00003667 }
Reid Spencerbb1fd572007-03-21 17:15:50 +00003668 $4.S.makeComposite(FTySign);
Reid Spencer950bf602007-01-26 08:19:09 +00003669
3670 // First upgrade any intrinsic calls.
3671 std::vector<Value*> Args;
3672 if ($6)
3673 for (unsigned i = 0, e = $6->size(); i < e; ++i)
3674 Args.push_back((*$6)[i].V);
Reid Spencer41b213e2007-04-02 01:14:00 +00003675 Instruction *Inst = upgradeIntrinsicCall(FTy->getReturnType(), $4, Args);
Reid Spencer950bf602007-01-26 08:19:09 +00003676
3677 // If we got an upgraded intrinsic
3678 if (Inst) {
3679 $$.I = Inst;
Reid Spencer950bf602007-01-26 08:19:09 +00003680 } else {
3681 // Get the function we're calling
3682 Value *V = getVal(PFTy, $4);
3683
3684 // Check the argument values match
3685 if (!$6) { // Has no arguments?
3686 // Make sure no arguments is a good thing!
3687 if (FTy->getNumParams() != 0)
3688 error("No arguments passed to a function that expects arguments");
3689 } else { // Has arguments?
3690 // Loop through FunctionType's arguments and ensure they are specified
3691 // correctly!
3692 //
3693 FunctionType::param_iterator I = FTy->param_begin();
3694 FunctionType::param_iterator E = FTy->param_end();
3695 std::vector<ValueInfo>::iterator ArgI = $6->begin(), ArgE = $6->end();
3696
3697 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
3698 if ((*ArgI).V->getType() != *I)
3699 error("Parameter " +(*ArgI).V->getName()+ " is not of type '" +
3700 (*I)->getDescription() + "'");
3701
3702 if (I != E || (ArgI != ArgE && !FTy->isVarArg()))
3703 error("Invalid number of parameters detected");
3704 }
3705
3706 // Create the call instruction
David Greene718fda32007-08-01 03:59:32 +00003707 CallInst *CI = new CallInst(V, Args.begin(), Args.end());
Reid Spencer950bf602007-01-26 08:19:09 +00003708 CI->setTailCall($1);
Reid Spencerb7046c72007-01-29 05:41:34 +00003709 CI->setCallingConv(upgradeCallingConv($2));
Duncan Sandsdc024672007-11-27 13:23:08 +00003710
Reid Spencer950bf602007-01-26 08:19:09 +00003711 $$.I = CI;
Reid Spencer950bf602007-01-26 08:19:09 +00003712 }
Duncan Sandsdc024672007-11-27 13:23:08 +00003713 // Deal with CSRetCC
3714 if ($2 == OldCallingConv::CSRet) {
3715 ParamAttrsVector Attrs;
3716 ParamAttrsWithIndex PAWI;
3717 PAWI.index = 1; PAWI.attrs = ParamAttr::StructRet; // first arg
3718 Attrs.push_back(PAWI);
3719 cast<CallInst>($$.I)->setParamAttrs(ParamAttrsList::get(Attrs));
3720 }
Reid Spencered96d1e2007-02-08 09:08:52 +00003721 delete $3.PAT;
Reid Spencer950bf602007-01-26 08:19:09 +00003722 delete $6;
Reid Spencer7eea8ff2007-05-18 05:48:07 +00003723 lastCallingConv = OldCallingConv::C;
Reid Spencere7c3c602006-11-30 06:36:44 +00003724 }
Reid Spencer950bf602007-01-26 08:19:09 +00003725 | MemoryInst {
3726 $$ = $1;
3727 }
3728 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00003729
3730
3731// IndexList - List of indices for GEP based instructions...
3732IndexList
Reid Spencer950bf602007-01-26 08:19:09 +00003733 : ',' ValueRefList { $$ = $2; }
3734 | /* empty */ { $$ = new std::vector<ValueInfo>(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00003735 ;
3736
3737OptVolatile
Reid Spencer950bf602007-01-26 08:19:09 +00003738 : VOLATILE { $$ = true; }
3739 | /* empty */ { $$ = false; }
Reid Spencere7c3c602006-11-30 06:36:44 +00003740 ;
3741
Reid Spencer950bf602007-01-26 08:19:09 +00003742MemoryInst
3743 : MALLOC Types OptCAlign {
Reid Spencered96d1e2007-02-08 09:08:52 +00003744 const Type *Ty = $2.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003745 $$.S.makeComposite($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003746 $$.I = new MallocInst(Ty, 0, $3);
Reid Spencered96d1e2007-02-08 09:08:52 +00003747 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003748 }
3749 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencered96d1e2007-02-08 09:08:52 +00003750 const Type *Ty = $2.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003751 $5.S.makeUnsigned();
3752 $$.S.makeComposite($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003753 $$.I = new MallocInst(Ty, getVal($4.T, $5), $6);
Reid Spencered96d1e2007-02-08 09:08:52 +00003754 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003755 }
3756 | ALLOCA Types OptCAlign {
Reid Spencered96d1e2007-02-08 09:08:52 +00003757 const Type *Ty = $2.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003758 $$.S.makeComposite($2.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003759 $$.I = new AllocaInst(Ty, 0, $3);
Reid Spencered96d1e2007-02-08 09:08:52 +00003760 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003761 }
3762 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencered96d1e2007-02-08 09:08:52 +00003763 const Type *Ty = $2.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003764 $5.S.makeUnsigned();
3765 $$.S.makeComposite($4.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003766 $$.I = new AllocaInst(Ty, getVal($4.T, $5), $6);
Reid Spencered96d1e2007-02-08 09:08:52 +00003767 delete $2.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003768 }
3769 | FREE ResolvedVal {
Reid Spencer950bf602007-01-26 08:19:09 +00003770 const Type *PTy = $2.V->getType();
3771 if (!isa<PointerType>(PTy))
3772 error("Trying to free nonpointer type '" + PTy->getDescription() + "'");
3773 $$.I = new FreeInst($2.V);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003774 $$.S.makeSignless();
Reid Spencere7c3c602006-11-30 06:36:44 +00003775 }
3776 | OptVolatile LOAD Types ValueRef {
Reid Spencered96d1e2007-02-08 09:08:52 +00003777 const Type* Ty = $3.PAT->get();
Reid Spencerbb1fd572007-03-21 17:15:50 +00003778 $4.S.copy($3.S);
Reid Spencer950bf602007-01-26 08:19:09 +00003779 if (!isa<PointerType>(Ty))
3780 error("Can't load from nonpointer type: " + Ty->getDescription());
3781 if (!cast<PointerType>(Ty)->getElementType()->isFirstClassType())
3782 error("Can't load from pointer of non-first-class type: " +
3783 Ty->getDescription());
3784 Value* tmpVal = getVal(Ty, $4);
3785 $$.I = new LoadInst(tmpVal, "", $1);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003786 $$.S.copy($3.S.get(0));
Reid Spencered96d1e2007-02-08 09:08:52 +00003787 delete $3.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003788 }
3789 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003790 $6.S.copy($5.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003791 const PointerType *PTy = dyn_cast<PointerType>($5.PAT->get());
Reid Spencer950bf602007-01-26 08:19:09 +00003792 if (!PTy)
3793 error("Can't store to a nonpointer type: " +
Reid Spencered96d1e2007-02-08 09:08:52 +00003794 $5.PAT->get()->getDescription());
Reid Spencer950bf602007-01-26 08:19:09 +00003795 const Type *ElTy = PTy->getElementType();
Reid Spencered96d1e2007-02-08 09:08:52 +00003796 Value *StoreVal = $3.V;
Reid Spencer950bf602007-01-26 08:19:09 +00003797 Value* tmpVal = getVal(PTy, $6);
Reid Spencered96d1e2007-02-08 09:08:52 +00003798 if (ElTy != $3.V->getType()) {
Duncan Sandsdc024672007-11-27 13:23:08 +00003799 PTy = PointerType::get(StoreVal->getType());
3800 if (Constant *C = dyn_cast<Constant>(tmpVal))
3801 tmpVal = ConstantExpr::getBitCast(C, PTy);
3802 else
3803 tmpVal = new BitCastInst(tmpVal, PTy, "upgrd.cast", CurBB);
Reid Spencered96d1e2007-02-08 09:08:52 +00003804 }
3805 $$.I = new StoreInst(StoreVal, tmpVal, $1);
Reid Spencerbb1fd572007-03-21 17:15:50 +00003806 $$.S.makeSignless();
Reid Spencered96d1e2007-02-08 09:08:52 +00003807 delete $5.PAT;
Reid Spencere7c3c602006-11-30 06:36:44 +00003808 }
3809 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencerbb1fd572007-03-21 17:15:50 +00003810 $3.S.copy($2.S);
Reid Spencered96d1e2007-02-08 09:08:52 +00003811 const Type* Ty = $2.PAT->get();
Reid Spencer950bf602007-01-26 08:19:09 +00003812 if (!isa<PointerType>(Ty))
3813 error("getelementptr insn requires pointer operand");
3814
3815 std::vector<Value*> VIndices;
Reid Spencerff0e4482007-04-16 00:40:57 +00003816 upgradeGEPInstIndices(Ty, $4, VIndices);
Reid Spencer950bf602007-01-26 08:19:09 +00003817
3818 Value* tmpVal = getVal(Ty, $3);
David Greene5fd22a82007-09-04 18:46:50 +00003819 $$.I = new GetElementPtrInst(tmpVal, VIndices.begin(), VIndices.end());
Reid Spencerbb1fd572007-03-21 17:15:50 +00003820 ValueInfo VI; VI.V = tmpVal; VI.S.copy($2.S);
3821 $$.S.copy(getElementSign(VI, VIndices));
Reid Spencered96d1e2007-02-08 09:08:52 +00003822 delete $2.PAT;
Reid Spencer30d0c582007-01-15 00:26:18 +00003823 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00003824 };
3825
Reid Spencer950bf602007-01-26 08:19:09 +00003826
Reid Spencere7c3c602006-11-30 06:36:44 +00003827%%
3828
3829int yyerror(const char *ErrorMsg) {
3830 std::string where
3831 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Reid Spencered96d1e2007-02-08 09:08:52 +00003832 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
Reid Spencer950bf602007-01-26 08:19:09 +00003833 std::string errMsg = where + "error: " + std::string(ErrorMsg);
3834 if (yychar != YYEMPTY && yychar != 0)
3835 errMsg += " while reading token '" + std::string(Upgradetext, Upgradeleng) +
3836 "'.";
Reid Spencer71d2ec92006-12-31 06:02:26 +00003837 std::cerr << "llvm-upgrade: " << errMsg << '\n';
Reid Spencer950bf602007-01-26 08:19:09 +00003838 std::cout << "llvm-upgrade: parse failed.\n";
Reid Spencere7c3c602006-11-30 06:36:44 +00003839 exit(1);
3840}
Reid Spencer319a7302007-01-05 17:20:02 +00003841
Reid Spencer30d0c582007-01-15 00:26:18 +00003842void warning(const std::string& ErrorMsg) {
Reid Spencer319a7302007-01-05 17:20:02 +00003843 std::string where
3844 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Reid Spencered96d1e2007-02-08 09:08:52 +00003845 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
Reid Spencer950bf602007-01-26 08:19:09 +00003846 std::string errMsg = where + "warning: " + std::string(ErrorMsg);
3847 if (yychar != YYEMPTY && yychar != 0)
3848 errMsg += " while reading token '" + std::string(Upgradetext, Upgradeleng) +
3849 "'.";
Reid Spencer319a7302007-01-05 17:20:02 +00003850 std::cerr << "llvm-upgrade: " << errMsg << '\n';
3851}
Reid Spencer950bf602007-01-26 08:19:09 +00003852
3853void error(const std::string& ErrorMsg, int LineNo) {
3854 if (LineNo == -1) LineNo = Upgradelineno;
3855 Upgradelineno = LineNo;
3856 yyerror(ErrorMsg.c_str());
3857}
3858