blob: af3a39d3d901f352894444644240f823a26d7846 [file] [log] [blame]
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// 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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the bison parser for LLVM assembly languages files.
11//
12//===----------------------------------------------------------------------===//
13
14%{
15#include "ParserInternals.h"
16#include "llvm/CallingConv.h"
17#include "llvm/InlineAsm.h"
18#include "llvm/Instructions.h"
19#include "llvm/Module.h"
20#include "llvm/SymbolTable.h"
21#include "llvm/Assembly/AutoUpgrade.h"
22#include "llvm/Support/GetElementPtrTypeIterator.h"
23#include "llvm/ADT/STLExtras.h"
24#include "llvm/Support/MathExtras.h"
25#include <algorithm>
26#include <iostream>
27#include <list>
28#include <utility>
29
Reid Spencerb50974a2006-08-18 17:32:55 +000030// The following is a gross hack. In order to rid the libAsmParser library of
31// exceptions, we have to have a way of getting the yyparse function to go into
32// an error situation. So, whenever we want an error to occur, the GenerateError
33// function (see bottom of file) sets TriggerError. Then, at the end of each
34// production in the grammer we use CHECK_FOR_ERROR which will invoke YYERROR
35// (a goto) to put YACC in error state. Furthermore, several calls to
36// GenerateError are made from inside productions and they must simulate the
37// previous exception behavior by exiting the production immediately. We have
38// replaced these with the GEN_ERROR macro which calls GeneratError and then
39// immediately invokes YYERROR. This would be so much cleaner if it was a
40// recursive descent parser.
Reid Spencer713eedc2006-08-18 08:43:06 +000041static bool TriggerError = false;
42#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYERROR; } }
Reid Spencer713eedc2006-08-18 08:43:06 +000043#define GEN_ERROR(msg) { GenerateError(msg); YYERROR; }
44
Chris Lattnerf20e61f2006-02-15 07:22:58 +000045int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
46int yylex(); // declaration" of xxx warnings.
47int yyparse();
48
49namespace llvm {
50 std::string CurFilename;
51}
52using namespace llvm;
53
54static Module *ParserResult;
55
56// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
57// relating to upreferences in the input stream.
58//
59//#define DEBUG_UPREFS 1
60#ifdef DEBUG_UPREFS
61#define UR_OUT(X) std::cerr << X
62#else
63#define UR_OUT(X)
64#endif
65
66#define YYERROR_VERBOSE 1
67
68static bool ObsoleteVarArgs;
69static bool NewVarArgs;
70static BasicBlock *CurBB;
71static GlobalVariable *CurGV;
72
73
74// This contains info used when building the body of a function. It is
75// destroyed when the function is completed.
76//
77typedef std::vector<Value *> ValueList; // Numbered defs
78static void
79ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
80 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
81
82static struct PerModuleInfo {
83 Module *CurrentModule;
84 std::map<const Type *, ValueList> Values; // Module level numbered definitions
85 std::map<const Type *,ValueList> LateResolveValues;
86 std::vector<PATypeHolder> Types;
87 std::map<ValID, PATypeHolder> LateResolveTypes;
88
89 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Chris Lattner7aa45902006-06-21 16:53:00 +000090 /// how they were referenced and on which line of the input they came from so
Chris Lattnerf20e61f2006-02-15 07:22:58 +000091 /// that we can resolve them later and print error messages as appropriate.
92 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
93
94 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
95 // references to global values. Global values may be referenced before they
96 // are defined, and if so, the temporary object that they represent is held
97 // here. This is used for forward references of GlobalValues.
98 //
99 typedef std::map<std::pair<const PointerType *,
100 ValID>, GlobalValue*> GlobalRefsType;
101 GlobalRefsType GlobalRefs;
102
103 void ModuleDone() {
104 // If we could not resolve some functions at function compilation time
105 // (calls to functions before they are defined), resolve them now... Types
106 // are resolved when the constant pool has been completely parsed.
107 //
108 ResolveDefinitions(LateResolveValues);
109
110 // Check to make sure that all global value forward references have been
111 // resolved!
112 //
113 if (!GlobalRefs.empty()) {
114 std::string UndefinedReferences = "Unresolved global references exist:\n";
115
116 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
117 I != E; ++I) {
118 UndefinedReferences += " " + I->first.first->getDescription() + " " +
119 I->first.second.getName() + "\n";
120 }
Reid Spencer713eedc2006-08-18 08:43:06 +0000121 GenerateError(UndefinedReferences);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000122 }
123
124 // Look for intrinsic functions and CallInst that need to be upgraded
Chris Lattner9ff96a72006-04-08 01:18:56 +0000125 for (Module::iterator FI = CurrentModule->begin(),
126 FE = CurrentModule->end(); FI != FE; )
Chris Lattneradf5ec62006-03-04 07:53:41 +0000127 UpgradeCallsToIntrinsic(FI++);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000128
129 Values.clear(); // Clear out function local definitions
130 Types.clear();
131 CurrentModule = 0;
132 }
133
134 // GetForwardRefForGlobal - Check to see if there is a forward reference
135 // for this global. If so, remove it from the GlobalRefs map and return it.
136 // If not, just return null.
137 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
138 // Check to see if there is a forward reference to this global variable...
139 // if there is, eliminate it and patch the reference to use the new def'n.
140 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
141 GlobalValue *Ret = 0;
142 if (I != GlobalRefs.end()) {
143 Ret = I->second;
144 GlobalRefs.erase(I);
145 }
146 return Ret;
147 }
148} CurModule;
149
150static struct PerFunctionInfo {
151 Function *CurrentFunction; // Pointer to current function being created
152
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000153 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000154 std::map<const Type*, ValueList> LateResolveValues;
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000155 bool isDeclare; // Is this function a forward declararation?
156 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000157
158 /// BBForwardRefs - When we see forward references to basic blocks, keep
159 /// track of them here.
160 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
161 std::vector<BasicBlock*> NumberedBlocks;
162 unsigned NextBBNum;
163
164 inline PerFunctionInfo() {
165 CurrentFunction = 0;
166 isDeclare = false;
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000167 Linkage = GlobalValue::ExternalLinkage;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000168 }
169
170 inline void FunctionStart(Function *M) {
171 CurrentFunction = M;
172 NextBBNum = 0;
173 }
174
175 void FunctionDone() {
176 NumberedBlocks.clear();
177
178 // Any forward referenced blocks left?
179 if (!BBForwardRefs.empty())
Reid Spencer713eedc2006-08-18 08:43:06 +0000180 GenerateError("Undefined reference to label " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000181 BBForwardRefs.begin()->first->getName());
182
183 // Resolve all forward references now.
184 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
185
186 Values.clear(); // Clear out function local definitions
187 CurrentFunction = 0;
188 isDeclare = false;
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000189 Linkage = GlobalValue::ExternalLinkage;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000190 }
191} CurFun; // Info for the current function...
192
193static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
194
195
196//===----------------------------------------------------------------------===//
197// Code to handle definitions of all the types
198//===----------------------------------------------------------------------===//
199
200static int InsertValue(Value *V,
201 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
202 if (V->hasName()) return -1; // Is this a numbered definition?
203
204 // Yes, insert the value into the value table...
205 ValueList &List = ValueTab[V->getType()];
206 List.push_back(V);
207 return List.size()-1;
208}
209
210static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
211 switch (D.Type) {
212 case ValID::NumberVal: // Is it a numbered definition?
213 // Module constants occupy the lowest numbered slots...
214 if ((unsigned)D.Num < CurModule.Types.size())
215 return CurModule.Types[(unsigned)D.Num];
216 break;
217 case ValID::NameVal: // Is it a named definition?
218 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
219 D.destroy(); // Free old strdup'd memory...
220 return N;
221 }
222 break;
223 default:
Reid Spencer713eedc2006-08-18 08:43:06 +0000224 GenerateError("Internal parser error: Invalid symbol type reference!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000225 }
226
227 // If we reached here, we referenced either a symbol that we don't know about
228 // or an id number that hasn't been read yet. We may be referencing something
229 // forward, so just create an entry to be resolved later and get to it...
230 //
231 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
232
233
234 if (inFunctionScope()) {
235 if (D.Type == ValID::NameVal)
Reid Spencer713eedc2006-08-18 08:43:06 +0000236 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000237 else
Reid Spencer713eedc2006-08-18 08:43:06 +0000238 GenerateError("Reference to an undefined type: #" + itostr(D.Num));
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000239 }
240
241 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
242 if (I != CurModule.LateResolveTypes.end())
243 return I->second;
244
245 Type *Typ = OpaqueType::get();
246 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
247 return Typ;
248 }
249
250static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
251 SymbolTable &SymTab =
252 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
253 CurModule.CurrentModule->getSymbolTable();
254 return SymTab.lookup(Ty, Name);
255}
256
257// getValNonImprovising - Look up the value specified by the provided type and
258// the provided ValID. If the value exists and has already been defined, return
259// it. Otherwise return null.
260//
261static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
262 if (isa<FunctionType>(Ty))
Reid Spencer713eedc2006-08-18 08:43:06 +0000263 GenerateError("Functions are not values and "
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000264 "must be referenced as pointers");
265
266 switch (D.Type) {
267 case ValID::NumberVal: { // Is it a numbered definition?
268 unsigned Num = (unsigned)D.Num;
269
270 // Module constants occupy the lowest numbered slots...
271 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
272 if (VI != CurModule.Values.end()) {
273 if (Num < VI->second.size())
274 return VI->second[Num];
275 Num -= VI->second.size();
276 }
277
278 // Make sure that our type is within bounds
279 VI = CurFun.Values.find(Ty);
280 if (VI == CurFun.Values.end()) return 0;
281
282 // Check that the number is within bounds...
283 if (VI->second.size() <= Num) return 0;
284
285 return VI->second[Num];
286 }
287
288 case ValID::NameVal: { // Is it a named definition?
289 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
290 if (N == 0) return 0;
291
292 D.destroy(); // Free old strdup'd memory...
293 return N;
294 }
295
296 // Check to make sure that "Ty" is an integral type, and that our
297 // value will fit into the specified type...
298 case ValID::ConstSIntVal: // Is it a constant pool reference??
299 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
Reid Spencer713eedc2006-08-18 08:43:06 +0000300 GenerateError("Signed integral constant '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000301 itostr(D.ConstPool64) + "' is invalid for type '" +
302 Ty->getDescription() + "'!");
303 return ConstantSInt::get(Ty, D.ConstPool64);
304
305 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
306 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
307 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000308 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000309 "' is invalid or out of range!");
310 } else { // This is really a signed reference. Transmogrify.
311 return ConstantSInt::get(Ty, D.ConstPool64);
312 }
313 } else {
314 return ConstantUInt::get(Ty, D.UConstPool64);
315 }
316
317 case ValID::ConstFPVal: // Is it a floating point const pool reference?
318 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Reid Spencer713eedc2006-08-18 08:43:06 +0000319 GenerateError("FP constant invalid for type!!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000320 return ConstantFP::get(Ty, D.ConstPoolFP);
321
322 case ValID::ConstNullVal: // Is it a null value?
323 if (!isa<PointerType>(Ty))
Reid Spencer713eedc2006-08-18 08:43:06 +0000324 GenerateError("Cannot create a a non pointer null!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000325 return ConstantPointerNull::get(cast<PointerType>(Ty));
326
327 case ValID::ConstUndefVal: // Is it an undef value?
328 return UndefValue::get(Ty);
329
330 case ValID::ConstZeroVal: // Is it a zero value?
331 return Constant::getNullValue(Ty);
332
333 case ValID::ConstantVal: // Fully resolved constant?
334 if (D.ConstantValue->getType() != Ty)
Reid Spencer713eedc2006-08-18 08:43:06 +0000335 GenerateError("Constant expression type different from required type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000336 return D.ConstantValue;
337
338 case ValID::InlineAsmVal: { // Inline asm expression
339 const PointerType *PTy = dyn_cast<PointerType>(Ty);
340 const FunctionType *FTy =
341 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
342 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints))
Reid Spencer713eedc2006-08-18 08:43:06 +0000343 GenerateError("Invalid type for asm constraint string!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000344 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
345 D.IAD->HasSideEffects);
346 D.destroy(); // Free InlineAsmDescriptor.
347 return IA;
348 }
349 default:
350 assert(0 && "Unhandled case!");
351 return 0;
352 } // End of switch
353
354 assert(0 && "Unhandled case!");
355 return 0;
356}
357
358// getVal - This function is identical to getValNonImprovising, except that if a
359// value is not already defined, it "improvises" by creating a placeholder var
360// that looks and acts just like the requested variable. When the value is
361// defined later, all uses of the placeholder variable are replaced with the
362// real thing.
363//
364static Value *getVal(const Type *Ty, const ValID &ID) {
365 if (Ty == Type::LabelTy)
Reid Spencer713eedc2006-08-18 08:43:06 +0000366 GenerateError("Cannot use a basic block here");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000367
368 // See if the value has already been defined.
369 Value *V = getValNonImprovising(Ty, ID);
370 if (V) return V;
371
372 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty))
Reid Spencer713eedc2006-08-18 08:43:06 +0000373 GenerateError("Invalid use of a composite type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000374
375 // If we reached here, we referenced either a symbol that we don't know about
376 // or an id number that hasn't been read yet. We may be referencing something
377 // forward, so just create an entry to be resolved later and get to it...
378 //
379 V = new Argument(Ty);
380
381 // Remember where this forward reference came from. FIXME, shouldn't we try
382 // to recycle these things??
383 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
384 llvmAsmlineno)));
385
386 if (inFunctionScope())
387 InsertValue(V, CurFun.LateResolveValues);
388 else
389 InsertValue(V, CurModule.LateResolveValues);
390 return V;
391}
392
393/// getBBVal - This is used for two purposes:
394/// * If isDefinition is true, a new basic block with the specified ID is being
395/// defined.
396/// * If isDefinition is true, this is a reference to a basic block, which may
397/// or may not be a forward reference.
398///
399static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
400 assert(inFunctionScope() && "Can't get basic block at global scope!");
401
402 std::string Name;
403 BasicBlock *BB = 0;
404 switch (ID.Type) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000405 default: GenerateError("Illegal label reference " + ID.getName());
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000406 case ValID::NumberVal: // Is it a numbered definition?
407 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
408 CurFun.NumberedBlocks.resize(ID.Num+1);
409 BB = CurFun.NumberedBlocks[ID.Num];
410 break;
411 case ValID::NameVal: // Is it a named definition?
412 Name = ID.Name;
413 if (Value *N = CurFun.CurrentFunction->
414 getSymbolTable().lookup(Type::LabelTy, Name))
415 BB = cast<BasicBlock>(N);
416 break;
417 }
418
419 // See if the block has already been defined.
420 if (BB) {
421 // If this is the definition of the block, make sure the existing value was
422 // just a forward reference. If it was a forward reference, there will be
423 // an entry for it in the PlaceHolderInfo map.
424 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
425 // The existing value was a definition, not a forward reference.
Reid Spencer713eedc2006-08-18 08:43:06 +0000426 GenerateError("Redefinition of label " + ID.getName());
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000427
428 ID.destroy(); // Free strdup'd memory.
429 return BB;
430 }
431
432 // Otherwise this block has not been seen before.
433 BB = new BasicBlock("", CurFun.CurrentFunction);
434 if (ID.Type == ValID::NameVal) {
435 BB->setName(ID.Name);
436 } else {
437 CurFun.NumberedBlocks[ID.Num] = BB;
438 }
439
440 // If this is not a definition, keep track of it so we can use it as a forward
441 // reference.
442 if (!isDefinition) {
443 // Remember where this forward reference came from.
444 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
445 } else {
446 // The forward declaration could have been inserted anywhere in the
447 // function: insert it into the correct place now.
448 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
449 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
450 }
451 ID.destroy();
452 return BB;
453}
454
455
456//===----------------------------------------------------------------------===//
457// Code to handle forward references in instructions
458//===----------------------------------------------------------------------===//
459//
460// This code handles the late binding needed with statements that reference
461// values not defined yet... for example, a forward branch, or the PHI node for
462// a loop body.
463//
464// This keeps a table (CurFun.LateResolveValues) of all such forward references
465// and back patchs after we are done.
466//
467
468// ResolveDefinitions - If we could not resolve some defs at parsing
469// time (forward branches, phi functions for loops, etc...) resolve the
470// defs now...
471//
472static void
473ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
474 std::map<const Type*,ValueList> *FutureLateResolvers) {
475 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
476 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
477 E = LateResolvers.end(); LRI != E; ++LRI) {
478 ValueList &List = LRI->second;
479 while (!List.empty()) {
480 Value *V = List.back();
481 List.pop_back();
482
483 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
484 CurModule.PlaceHolderInfo.find(V);
485 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
486
487 ValID &DID = PHI->second.first;
488
489 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
490 if (TheRealValue) {
491 V->replaceAllUsesWith(TheRealValue);
492 delete V;
493 CurModule.PlaceHolderInfo.erase(PHI);
494 } else if (FutureLateResolvers) {
495 // Functions have their unresolved items forwarded to the module late
496 // resolver table
497 InsertValue(V, *FutureLateResolvers);
498 } else {
499 if (DID.Type == ValID::NameVal)
Reid Spencer713eedc2006-08-18 08:43:06 +0000500 GenerateError("Reference to an invalid definition: '" +DID.getName()+
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000501 "' of type '" + V->getType()->getDescription() + "'",
502 PHI->second.second);
503 else
Reid Spencer713eedc2006-08-18 08:43:06 +0000504 GenerateError("Reference to an invalid definition: #" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000505 itostr(DID.Num) + " of type '" +
506 V->getType()->getDescription() + "'",
507 PHI->second.second);
508 }
509 }
510 }
511
512 LateResolvers.clear();
513}
514
515// ResolveTypeTo - A brand new type was just declared. This means that (if
516// name is not null) things referencing Name can be resolved. Otherwise, things
517// refering to the number can be resolved. Do this now.
518//
519static void ResolveTypeTo(char *Name, const Type *ToTy) {
520 ValID D;
521 if (Name) D = ValID::create(Name);
522 else D = ValID::create((int)CurModule.Types.size());
523
524 std::map<ValID, PATypeHolder>::iterator I =
525 CurModule.LateResolveTypes.find(D);
526 if (I != CurModule.LateResolveTypes.end()) {
527 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
528 CurModule.LateResolveTypes.erase(I);
529 }
530}
531
532// setValueName - Set the specified value to the name given. The name may be
533// null potentially, in which case this is a noop. The string passed in is
534// assumed to be a malloc'd string buffer, and is free'd by this function.
535//
536static void setValueName(Value *V, char *NameStr) {
537 if (NameStr) {
538 std::string Name(NameStr); // Copy string
539 free(NameStr); // Free old string
540
541 if (V->getType() == Type::VoidTy)
Reid Spencer713eedc2006-08-18 08:43:06 +0000542 GenerateError("Can't assign name '" + Name+"' to value with void type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000543
544 assert(inFunctionScope() && "Must be in function scope!");
545 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
546 if (ST.lookup(V->getType(), Name))
Reid Spencer713eedc2006-08-18 08:43:06 +0000547 GenerateError("Redefinition of value named '" + Name + "' in the '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000548 V->getType()->getDescription() + "' type plane!");
549
550 // Set the name.
551 V->setName(Name);
552 }
553}
554
555/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
556/// this is a declaration, otherwise it is a definition.
557static GlobalVariable *
558ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
559 bool isConstantGlobal, const Type *Ty,
560 Constant *Initializer) {
561 if (isa<FunctionType>(Ty))
Reid Spencer713eedc2006-08-18 08:43:06 +0000562 GenerateError("Cannot declare global vars of function type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000563
564 const PointerType *PTy = PointerType::get(Ty);
565
566 std::string Name;
567 if (NameStr) {
568 Name = NameStr; // Copy string
569 free(NameStr); // Free old string
570 }
571
572 // See if this global value was forward referenced. If so, recycle the
573 // object.
574 ValID ID;
575 if (!Name.empty()) {
576 ID = ValID::create((char*)Name.c_str());
577 } else {
578 ID = ValID::create((int)CurModule.Values[PTy].size());
579 }
580
581 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
582 // Move the global to the end of the list, from whereever it was
583 // previously inserted.
584 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
585 CurModule.CurrentModule->getGlobalList().remove(GV);
586 CurModule.CurrentModule->getGlobalList().push_back(GV);
587 GV->setInitializer(Initializer);
588 GV->setLinkage(Linkage);
589 GV->setConstant(isConstantGlobal);
590 InsertValue(GV, CurModule.Values);
591 return GV;
592 }
593
594 // If this global has a name, check to see if there is already a definition
595 // of this global in the module. If so, merge as appropriate. Note that
596 // this is really just a hack around problems in the CFE. :(
597 if (!Name.empty()) {
598 // We are a simple redefinition of a value, check to see if it is defined
599 // the same as the old one.
600 if (GlobalVariable *EGV =
601 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
602 // We are allowed to redefine a global variable in two circumstances:
603 // 1. If at least one of the globals is uninitialized or
604 // 2. If both initializers have the same value.
605 //
606 if (!EGV->hasInitializer() || !Initializer ||
607 EGV->getInitializer() == Initializer) {
608
609 // Make sure the existing global version gets the initializer! Make
610 // sure that it also gets marked const if the new version is.
611 if (Initializer && !EGV->hasInitializer())
612 EGV->setInitializer(Initializer);
613 if (isConstantGlobal)
614 EGV->setConstant(true);
615 EGV->setLinkage(Linkage);
616 return EGV;
617 }
618
Reid Spencer713eedc2006-08-18 08:43:06 +0000619 GenerateError("Redefinition of global variable named '" + Name +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000620 "' in the '" + Ty->getDescription() + "' type plane!");
621 }
622 }
623
624 // Otherwise there is no existing GV to use, create one now.
625 GlobalVariable *GV =
626 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
627 CurModule.CurrentModule);
628 InsertValue(GV, CurModule.Values);
629 return GV;
630}
631
632// setTypeName - Set the specified type to the name given. The name may be
633// null potentially, in which case this is a noop. The string passed in is
634// assumed to be a malloc'd string buffer, and is freed by this function.
635//
636// This function returns true if the type has already been defined, but is
637// allowed to be redefined in the specified context. If the name is a new name
638// for the type plane, it is inserted and false is returned.
639static bool setTypeName(const Type *T, char *NameStr) {
640 assert(!inFunctionScope() && "Can't give types function-local names!");
641 if (NameStr == 0) return false;
642
643 std::string Name(NameStr); // Copy string
644 free(NameStr); // Free old string
645
646 // We don't allow assigning names to void type
647 if (T == Type::VoidTy)
Reid Spencer713eedc2006-08-18 08:43:06 +0000648 GenerateError("Can't assign name '" + Name + "' to the void type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000649
650 // Set the type name, checking for conflicts as we do so.
651 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
652
653 if (AlreadyExists) { // Inserting a name that is already defined???
654 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
655 assert(Existing && "Conflict but no matching type?");
656
657 // There is only one case where this is allowed: when we are refining an
658 // opaque type. In this case, Existing will be an opaque type.
659 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
660 // We ARE replacing an opaque type!
661 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
662 return true;
663 }
664
665 // Otherwise, this is an attempt to redefine a type. That's okay if
666 // the redefinition is identical to the original. This will be so if
667 // Existing and T point to the same Type object. In this one case we
668 // allow the equivalent redefinition.
669 if (Existing == T) return true; // Yes, it's equal.
670
671 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer713eedc2006-08-18 08:43:06 +0000672 GenerateError("Redefinition of type named '" + Name + "' in the '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000673 T->getDescription() + "' type plane!");
674 }
675
676 return false;
677}
678
679//===----------------------------------------------------------------------===//
680// Code for handling upreferences in type names...
681//
682
683// TypeContains - Returns true if Ty directly contains E in it.
684//
685static bool TypeContains(const Type *Ty, const Type *E) {
686 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
687 E) != Ty->subtype_end();
688}
689
690namespace {
691 struct UpRefRecord {
692 // NestingLevel - The number of nesting levels that need to be popped before
693 // this type is resolved.
694 unsigned NestingLevel;
695
696 // LastContainedTy - This is the type at the current binding level for the
697 // type. Every time we reduce the nesting level, this gets updated.
698 const Type *LastContainedTy;
699
700 // UpRefTy - This is the actual opaque type that the upreference is
701 // represented with.
702 OpaqueType *UpRefTy;
703
704 UpRefRecord(unsigned NL, OpaqueType *URTy)
705 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
706 };
707}
708
709// UpRefs - A list of the outstanding upreferences that need to be resolved.
710static std::vector<UpRefRecord> UpRefs;
711
712/// HandleUpRefs - Every time we finish a new layer of types, this function is
713/// called. It loops through the UpRefs vector, which is a list of the
714/// currently active types. For each type, if the up reference is contained in
715/// the newly completed type, we decrement the level count. When the level
716/// count reaches zero, the upreferenced type is the type that is passed in:
717/// thus we can complete the cycle.
718///
719static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner680aab62006-08-18 17:34:45 +0000720 // If Ty isn't abstract, or if there are no up-references in it, then there is
721 // nothing to resolve here.
722 if (!ty->isAbstract() || UpRefs.empty()) return ty;
723
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000724 PATypeHolder Ty(ty);
725 UR_OUT("Type '" << Ty->getDescription() <<
726 "' newly formed. Resolving upreferences.\n" <<
727 UpRefs.size() << " upreferences active!\n");
728
729 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
730 // to zero), we resolve them all together before we resolve them to Ty. At
731 // the end of the loop, if there is anything to resolve to Ty, it will be in
732 // this variable.
733 OpaqueType *TypeToResolve = 0;
734
735 for (unsigned i = 0; i != UpRefs.size(); ++i) {
736 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
737 << UpRefs[i].second->getDescription() << ") = "
738 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
739 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
740 // Decrement level of upreference
741 unsigned Level = --UpRefs[i].NestingLevel;
742 UpRefs[i].LastContainedTy = Ty;
743 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
744 if (Level == 0) { // Upreference should be resolved!
745 if (!TypeToResolve) {
746 TypeToResolve = UpRefs[i].UpRefTy;
747 } else {
748 UR_OUT(" * Resolving upreference for "
749 << UpRefs[i].second->getDescription() << "\n";
750 std::string OldName = UpRefs[i].UpRefTy->getDescription());
751 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
752 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
753 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
754 }
755 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
756 --i; // Do not skip the next element...
757 }
758 }
759 }
760
761 if (TypeToResolve) {
762 UR_OUT(" * Resolving upreference for "
763 << UpRefs[i].second->getDescription() << "\n";
764 std::string OldName = TypeToResolve->getDescription());
765 TypeToResolve->refineAbstractTypeTo(Ty);
766 }
767
768 return Ty;
769}
770
771
772// common code from the two 'RunVMAsmParser' functions
773 static Module * RunParser(Module * M) {
774
775 llvmAsmlineno = 1; // Reset the current line number...
776 ObsoleteVarArgs = false;
777 NewVarArgs = false;
778
779 CurModule.CurrentModule = M;
780 yyparse(); // Parse the file, potentially throwing exception
Reid Spencer713eedc2006-08-18 08:43:06 +0000781 if (!ParserResult)
782 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000783
784 Module *Result = ParserResult;
785 ParserResult = 0;
786
787 //Not all functions use vaarg, so make a second check for ObsoleteVarArgs
788 {
789 Function* F;
790 if ((F = Result->getNamedFunction("llvm.va_start"))
791 && F->getFunctionType()->getNumParams() == 0)
792 ObsoleteVarArgs = true;
793 if((F = Result->getNamedFunction("llvm.va_copy"))
794 && F->getFunctionType()->getNumParams() == 1)
795 ObsoleteVarArgs = true;
796 }
797
798 if (ObsoleteVarArgs && NewVarArgs)
Reid Spencer713eedc2006-08-18 08:43:06 +0000799 GenerateError("This file is corrupt: it uses both new and old style varargs");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000800
801 if(ObsoleteVarArgs) {
802 if(Function* F = Result->getNamedFunction("llvm.va_start")) {
803 if (F->arg_size() != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +0000804 GenerateError("Obsolete va_start takes 0 argument!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000805
806 //foo = va_start()
807 // ->
808 //bar = alloca typeof(foo)
809 //va_start(bar)
810 //foo = load bar
811
812 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
813 const Type* ArgTy = F->getFunctionType()->getReturnType();
814 const Type* ArgTyPtr = PointerType::get(ArgTy);
815 Function* NF = Result->getOrInsertFunction("llvm.va_start",
816 RetTy, ArgTyPtr, (Type *)0);
817
818 while (!F->use_empty()) {
819 CallInst* CI = cast<CallInst>(F->use_back());
820 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
821 new CallInst(NF, bar, "", CI);
822 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
823 CI->replaceAllUsesWith(foo);
824 CI->getParent()->getInstList().erase(CI);
825 }
826 Result->getFunctionList().erase(F);
827 }
828
829 if(Function* F = Result->getNamedFunction("llvm.va_end")) {
830 if(F->arg_size() != 1)
Reid Spencer713eedc2006-08-18 08:43:06 +0000831 GenerateError("Obsolete va_end takes 1 argument!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000832
833 //vaend foo
834 // ->
835 //bar = alloca 1 of typeof(foo)
836 //vaend bar
837 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
838 const Type* ArgTy = F->getFunctionType()->getParamType(0);
839 const Type* ArgTyPtr = PointerType::get(ArgTy);
840 Function* NF = Result->getOrInsertFunction("llvm.va_end",
841 RetTy, ArgTyPtr, (Type *)0);
842
843 while (!F->use_empty()) {
844 CallInst* CI = cast<CallInst>(F->use_back());
845 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
846 new StoreInst(CI->getOperand(1), bar, CI);
847 new CallInst(NF, bar, "", CI);
848 CI->getParent()->getInstList().erase(CI);
849 }
850 Result->getFunctionList().erase(F);
851 }
852
853 if(Function* F = Result->getNamedFunction("llvm.va_copy")) {
854 if(F->arg_size() != 1)
Reid Spencer713eedc2006-08-18 08:43:06 +0000855 GenerateError("Obsolete va_copy takes 1 argument!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000856 //foo = vacopy(bar)
857 // ->
858 //a = alloca 1 of typeof(foo)
859 //b = alloca 1 of typeof(foo)
860 //store bar -> b
861 //vacopy(a, b)
862 //foo = load a
863
864 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
865 const Type* ArgTy = F->getFunctionType()->getReturnType();
866 const Type* ArgTyPtr = PointerType::get(ArgTy);
867 Function* NF = Result->getOrInsertFunction("llvm.va_copy",
868 RetTy, ArgTyPtr, ArgTyPtr,
869 (Type *)0);
870
871 while (!F->use_empty()) {
872 CallInst* CI = cast<CallInst>(F->use_back());
873 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
874 AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
875 new StoreInst(CI->getOperand(1), b, CI);
876 new CallInst(NF, a, b, "", CI);
877 Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
878 CI->replaceAllUsesWith(foo);
879 CI->getParent()->getInstList().erase(CI);
880 }
881 Result->getFunctionList().erase(F);
882 }
883 }
884
885 return Result;
886
887 }
888
889//===----------------------------------------------------------------------===//
890// RunVMAsmParser - Define an interface to this parser
891//===----------------------------------------------------------------------===//
892//
893Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
894 set_scan_file(F);
895
896 CurFilename = Filename;
897 return RunParser(new Module(CurFilename));
898}
899
900Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
901 set_scan_string(AsmString);
902
903 CurFilename = "from_memory";
904 if (M == NULL) {
905 return RunParser(new Module (CurFilename));
906 } else {
907 return RunParser(M);
908 }
909}
910
911%}
912
913%union {
914 llvm::Module *ModuleVal;
915 llvm::Function *FunctionVal;
916 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
917 llvm::BasicBlock *BasicBlockVal;
918 llvm::TerminatorInst *TermInstVal;
919 llvm::Instruction *InstVal;
920 llvm::Constant *ConstVal;
921
922 const llvm::Type *PrimType;
923 llvm::PATypeHolder *TypeVal;
924 llvm::Value *ValueVal;
925
926 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
927 std::vector<llvm::Value*> *ValueList;
928 std::list<llvm::PATypeHolder> *TypeList;
929 // Represent the RHS of PHI node
930 std::list<std::pair<llvm::Value*,
931 llvm::BasicBlock*> > *PHIList;
932 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
933 std::vector<llvm::Constant*> *ConstVector;
934
935 llvm::GlobalValue::LinkageTypes Linkage;
936 int64_t SInt64Val;
937 uint64_t UInt64Val;
938 int SIntVal;
939 unsigned UIntVal;
940 double FPVal;
941 bool BoolVal;
942
943 char *StrVal; // This memory is strdup'd!
944 llvm::ValID ValIDVal; // strdup'd memory maybe!
945
946 llvm::Instruction::BinaryOps BinaryOpVal;
947 llvm::Instruction::TermOps TermOpVal;
948 llvm::Instruction::MemoryOps MemOpVal;
949 llvm::Instruction::OtherOps OtherOpVal;
950 llvm::Module::Endianness Endianness;
951}
952
953%type <ModuleVal> Module FunctionList
954%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
955%type <BasicBlockVal> BasicBlock InstructionList
956%type <TermInstVal> BBTerminatorInst
957%type <InstVal> Inst InstVal MemoryInst
958%type <ConstVal> ConstVal ConstExpr
959%type <ConstVector> ConstVector
960%type <ArgList> ArgList ArgListH
961%type <ArgVal> ArgVal
962%type <PHIList> PHIList
963%type <ValueList> ValueRefList ValueRefListE // For call param lists
964%type <ValueList> IndexList // For GEP derived indices
965%type <TypeList> TypeListI ArgTypeListI
966%type <JumpTable> JumpTable
967%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
968%type <BoolVal> OptVolatile // 'volatile' or not
969%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
970%type <BoolVal> OptSideEffect // 'sideeffect' or not.
971%type <Linkage> OptLinkage
972%type <Endianness> BigOrLittle
973
974// ValueRef - Unresolved reference to a definition or BB
975%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
976%type <ValueVal> ResolvedVal // <type> <valref> pair
977// Tokens and types for handling constant integer values
978//
979// ESINT64VAL - A negative number within long long range
980%token <SInt64Val> ESINT64VAL
981
982// EUINT64VAL - A positive number within uns. long long range
983%token <UInt64Val> EUINT64VAL
984%type <SInt64Val> EINT64VAL
985
986%token <SIntVal> SINTVAL // Signed 32 bit ints...
987%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
988%type <SIntVal> INTVAL
989%token <FPVal> FPVAL // Float or Double constant
990
991// Built in types...
992%type <TypeVal> Types TypesV UpRTypes UpRTypesV
993%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
994%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
995%token <PrimType> FLOAT DOUBLE TYPE LABEL
996
997%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
998%type <StrVal> Name OptName OptAssign
999%type <UIntVal> OptAlign OptCAlign
1000%type <StrVal> OptSection SectionString
1001
1002%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
1003%token DECLARE GLOBAL CONSTANT SECTION VOLATILE
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001004%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
1005%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001006%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
1007%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Chris Lattner09c0e992006-05-19 21:28:53 +00001008%token CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001009%type <UIntVal> OptCallingConv
1010
1011// Basic Block Terminating Operators
1012%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
1013
1014// Binary Operators
1015%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
1016%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
1017%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
1018
1019// Memory Instructions
1020%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1021
1022// Other Operators
1023%type <OtherOpVal> ShiftOps
1024%token <OtherOpVal> PHI_TOK CAST SELECT SHL SHR VAARG
Chris Lattner9ff96a72006-04-08 01:18:56 +00001025%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001026%token VAARG_old VANEXT_old //OBSOLETE
1027
1028
1029%start Module
1030%%
1031
1032// Handle constant integer size restriction and conversion...
1033//
1034INTVAL : SINTVAL;
1035INTVAL : UINTVAL {
1036 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
Reid Spencer713eedc2006-08-18 08:43:06 +00001037 GEN_ERROR("Value too large for type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001038 $$ = (int32_t)$1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001039 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001040};
1041
1042
1043EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
1044EINT64VAL : EUINT64VAL {
1045 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
Reid Spencer713eedc2006-08-18 08:43:06 +00001046 GEN_ERROR("Value too large for type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001047 $$ = (int64_t)$1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001048 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001049};
1050
1051// Operations that are notably excluded from this list include:
1052// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1053//
1054ArithmeticOps: ADD | SUB | MUL | DIV | REM;
1055LogicalOps : AND | OR | XOR;
1056SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
1057
1058ShiftOps : SHL | SHR;
1059
1060// These are some types that allow classification if we only want a particular
1061// thing... for example, only a signed, unsigned, or integral type.
1062SIntType : LONG | INT | SHORT | SBYTE;
1063UIntType : ULONG | UINT | USHORT | UBYTE;
1064IntType : SIntType | UIntType;
1065FPType : FLOAT | DOUBLE;
1066
1067// OptAssign - Value producing statements have an optional assignment component
1068OptAssign : Name '=' {
1069 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001070 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001071 }
1072 | /*empty*/ {
1073 $$ = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00001074 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001075 };
1076
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001077OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
1078 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
1079 WEAK { $$ = GlobalValue::WeakLinkage; } |
1080 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
1081 DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; } |
1082 DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; } |
1083 EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; } |
1084 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001085
1086OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1087 CCC_TOK { $$ = CallingConv::C; } |
Chris Lattner09c0e992006-05-19 21:28:53 +00001088 CSRETCC_TOK { $$ = CallingConv::CSRet; } |
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001089 FASTCC_TOK { $$ = CallingConv::Fast; } |
1090 COLDCC_TOK { $$ = CallingConv::Cold; } |
1091 CC_TOK EUINT64VAL {
1092 if ((unsigned)$2 != $2)
Reid Spencer713eedc2006-08-18 08:43:06 +00001093 GEN_ERROR("Calling conv too large!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001094 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001095 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001096 };
1097
1098// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1099// a comma before it.
1100OptAlign : /*empty*/ { $$ = 0; } |
1101 ALIGN EUINT64VAL {
1102 $$ = $2;
1103 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencer713eedc2006-08-18 08:43:06 +00001104 GEN_ERROR("Alignment must be a power of two!");
1105 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001106};
1107OptCAlign : /*empty*/ { $$ = 0; } |
1108 ',' ALIGN EUINT64VAL {
1109 $$ = $3;
1110 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencer713eedc2006-08-18 08:43:06 +00001111 GEN_ERROR("Alignment must be a power of two!");
1112 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001113};
1114
1115
1116SectionString : SECTION STRINGCONSTANT {
1117 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1118 if ($2[i] == '"' || $2[i] == '\\')
Reid Spencer713eedc2006-08-18 08:43:06 +00001119 GEN_ERROR("Invalid character in section name!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001120 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001121 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001122};
1123
1124OptSection : /*empty*/ { $$ = 0; } |
1125 SectionString { $$ = $1; };
1126
1127// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1128// is set to be the global we are processing.
1129//
1130GlobalVarAttributes : /* empty */ {} |
1131 ',' GlobalVarAttribute GlobalVarAttributes {};
1132GlobalVarAttribute : SectionString {
1133 CurGV->setSection($1);
1134 free($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001135 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001136 }
1137 | ALIGN EUINT64VAL {
1138 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencer713eedc2006-08-18 08:43:06 +00001139 GEN_ERROR("Alignment must be a power of two!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001140 CurGV->setAlignment($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001141 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001142 };
1143
1144//===----------------------------------------------------------------------===//
1145// Types includes all predefined types... except void, because it can only be
1146// used in specific contexts (function returning void for example). To have
1147// access to it, a user must explicitly use TypesV.
1148//
1149
1150// TypesV includes all of 'Types', but it also includes the void type.
1151TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1152UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
1153
1154Types : UpRTypes {
1155 if (!UpRefs.empty())
Reid Spencer713eedc2006-08-18 08:43:06 +00001156 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001157 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001158 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001159 };
1160
1161
1162// Derived types are added later...
1163//
1164PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1165PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
1166UpRTypes : OPAQUE {
1167 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer713eedc2006-08-18 08:43:06 +00001168 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001169 }
1170 | PrimType {
1171 $$ = new PATypeHolder($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001172 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001173 };
1174UpRTypes : SymbolicValueRef { // Named types are also simple types...
1175 $$ = new PATypeHolder(getTypeVal($1));
Reid Spencer713eedc2006-08-18 08:43:06 +00001176 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001177};
1178
1179// Include derived types in the Types production.
1180//
1181UpRTypes : '\\' EUINT64VAL { // Type UpReference
Reid Spencer713eedc2006-08-18 08:43:06 +00001182 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001183 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1184 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
1185 $$ = new PATypeHolder(OT);
1186 UR_OUT("New Upreference!\n");
Reid Spencer713eedc2006-08-18 08:43:06 +00001187 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001188 }
1189 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
1190 std::vector<const Type*> Params;
1191 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1192 E = $3->end(); I != E; ++I)
1193 Params.push_back(*I);
1194 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1195 if (isVarArg) Params.pop_back();
1196
1197 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
1198 delete $3; // Delete the argument list
1199 delete $1; // Delete the return type handle
Reid Spencer713eedc2006-08-18 08:43:06 +00001200 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001201 }
1202 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
1203 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1204 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00001205 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001206 }
1207 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
1208 const llvm::Type* ElemTy = $4->get();
1209 if ((unsigned)$2 != $2)
Reid Spencer713eedc2006-08-18 08:43:06 +00001210 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001211 if (!ElemTy->isPrimitiveType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001212 GEN_ERROR("Elemental type of a PackedType must be primitive");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001213 if (!isPowerOf2_32($2))
Reid Spencer713eedc2006-08-18 08:43:06 +00001214 GEN_ERROR("Vector length should be a power of 2!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001215 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
1216 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00001217 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001218 }
1219 | '{' TypeListI '}' { // Structure type?
1220 std::vector<const Type*> Elements;
1221 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
1222 E = $2->end(); I != E; ++I)
1223 Elements.push_back(*I);
1224
1225 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
1226 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001227 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001228 }
1229 | '{' '}' { // Empty structure type?
1230 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer713eedc2006-08-18 08:43:06 +00001231 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001232 }
1233 | UpRTypes '*' { // Pointer type?
1234 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1235 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001236 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001237 };
1238
1239// TypeList - Used for struct declarations and as a basis for function type
1240// declaration type lists
1241//
1242TypeListI : UpRTypes {
1243 $$ = new std::list<PATypeHolder>();
1244 $$->push_back(*$1); delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001245 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001246 }
1247 | TypeListI ',' UpRTypes {
1248 ($$=$1)->push_back(*$3); delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001249 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001250 };
1251
1252// ArgTypeList - List of types for a function type declaration...
1253ArgTypeListI : TypeListI
1254 | TypeListI ',' DOTDOTDOT {
1255 ($$=$1)->push_back(Type::VoidTy);
Reid Spencer713eedc2006-08-18 08:43:06 +00001256 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001257 }
1258 | DOTDOTDOT {
1259 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Reid Spencer713eedc2006-08-18 08:43:06 +00001260 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001261 }
1262 | /*empty*/ {
1263 $$ = new std::list<PATypeHolder>();
Reid Spencer713eedc2006-08-18 08:43:06 +00001264 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001265 };
1266
1267// ConstVal - The various declarations that go into the constant pool. This
1268// production is used ONLY to represent constants that show up AFTER a 'const',
1269// 'constant' or 'global' token at global scope. Constants that can be inlined
1270// into other expressions (such as integers and constexprs) are handled by the
1271// ResolvedVal, ValueRef and ConstValueRef productions.
1272//
1273ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
1274 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
1275 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001276 GEN_ERROR("Cannot make array constant with type: '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001277 (*$1)->getDescription() + "'!");
1278 const Type *ETy = ATy->getElementType();
1279 int NumElements = ATy->getNumElements();
1280
1281 // Verify that we have the correct size...
1282 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer713eedc2006-08-18 08:43:06 +00001283 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001284 utostr($3->size()) + " arguments, but has size of " +
1285 itostr(NumElements) + "!");
1286
1287 // Verify all elements are correct type!
1288 for (unsigned i = 0; i < $3->size(); i++) {
1289 if (ETy != (*$3)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001290 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001291 ETy->getDescription() +"' as required!\nIt is of type '"+
1292 (*$3)[i]->getType()->getDescription() + "'.");
1293 }
1294
1295 $$ = ConstantArray::get(ATy, *$3);
1296 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001297 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001298 }
1299 | Types '[' ']' {
1300 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
1301 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001302 GEN_ERROR("Cannot make array constant with type: '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001303 (*$1)->getDescription() + "'!");
1304
1305 int NumElements = ATy->getNumElements();
1306 if (NumElements != -1 && NumElements != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001307 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001308 " arguments, but has size of " + itostr(NumElements) +"!");
1309 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1310 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001311 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001312 }
1313 | Types 'c' STRINGCONSTANT {
1314 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
1315 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001316 GEN_ERROR("Cannot make array constant with type: '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001317 (*$1)->getDescription() + "'!");
1318
1319 int NumElements = ATy->getNumElements();
1320 const Type *ETy = ATy->getElementType();
1321 char *EndStr = UnEscapeLexed($3, true);
1322 if (NumElements != -1 && NumElements != (EndStr-$3))
Reid Spencer713eedc2006-08-18 08:43:06 +00001323 GEN_ERROR("Can't build string constant of size " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001324 itostr((int)(EndStr-$3)) +
1325 " when array has size " + itostr(NumElements) + "!");
1326 std::vector<Constant*> Vals;
1327 if (ETy == Type::SByteTy) {
1328 for (signed char *C = (signed char *)$3; C != (signed char *)EndStr; ++C)
1329 Vals.push_back(ConstantSInt::get(ETy, *C));
1330 } else if (ETy == Type::UByteTy) {
1331 for (unsigned char *C = (unsigned char *)$3;
1332 C != (unsigned char*)EndStr; ++C)
1333 Vals.push_back(ConstantUInt::get(ETy, *C));
1334 } else {
1335 free($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001336 GEN_ERROR("Cannot build string arrays of non byte sized elements!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001337 }
1338 free($3);
1339 $$ = ConstantArray::get(ATy, Vals);
1340 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001341 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001342 }
1343 | Types '<' ConstVector '>' { // Nonempty unsized arr
1344 const PackedType *PTy = dyn_cast<PackedType>($1->get());
1345 if (PTy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001346 GEN_ERROR("Cannot make packed constant with type: '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001347 (*$1)->getDescription() + "'!");
1348 const Type *ETy = PTy->getElementType();
1349 int NumElements = PTy->getNumElements();
1350
1351 // Verify that we have the correct size...
1352 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer713eedc2006-08-18 08:43:06 +00001353 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001354 utostr($3->size()) + " arguments, but has size of " +
1355 itostr(NumElements) + "!");
1356
1357 // Verify all elements are correct type!
1358 for (unsigned i = 0; i < $3->size(); i++) {
1359 if (ETy != (*$3)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001360 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001361 ETy->getDescription() +"' as required!\nIt is of type '"+
1362 (*$3)[i]->getType()->getDescription() + "'.");
1363 }
1364
1365 $$ = ConstantPacked::get(PTy, *$3);
1366 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001367 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001368 }
1369 | Types '{' ConstVector '}' {
1370 const StructType *STy = dyn_cast<StructType>($1->get());
1371 if (STy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001372 GEN_ERROR("Cannot make struct constant with type: '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001373 (*$1)->getDescription() + "'!");
1374
1375 if ($3->size() != STy->getNumContainedTypes())
Reid Spencer713eedc2006-08-18 08:43:06 +00001376 GEN_ERROR("Illegal number of initializers for structure type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001377
1378 // Check to ensure that constants are compatible with the type initializer!
1379 for (unsigned i = 0, e = $3->size(); i != e; ++i)
1380 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer713eedc2006-08-18 08:43:06 +00001381 GEN_ERROR("Expected type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001382 STy->getElementType(i)->getDescription() +
1383 "' for element #" + utostr(i) +
1384 " of structure initializer!");
1385
1386 $$ = ConstantStruct::get(STy, *$3);
1387 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001388 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001389 }
1390 | Types '{' '}' {
1391 const StructType *STy = dyn_cast<StructType>($1->get());
1392 if (STy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001393 GEN_ERROR("Cannot make struct constant with type: '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001394 (*$1)->getDescription() + "'!");
1395
1396 if (STy->getNumContainedTypes() != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001397 GEN_ERROR("Illegal number of initializers for structure type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001398
1399 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1400 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001401 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001402 }
1403 | Types NULL_TOK {
1404 const PointerType *PTy = dyn_cast<PointerType>($1->get());
1405 if (PTy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001406 GEN_ERROR("Cannot make null pointer constant with type: '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001407 (*$1)->getDescription() + "'!");
1408
1409 $$ = ConstantPointerNull::get(PTy);
1410 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001411 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001412 }
1413 | Types UNDEF {
1414 $$ = UndefValue::get($1->get());
1415 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001416 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001417 }
1418 | Types SymbolicValueRef {
1419 const PointerType *Ty = dyn_cast<PointerType>($1->get());
1420 if (Ty == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001421 GEN_ERROR("Global const reference must be a pointer type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001422
1423 // ConstExprs can exist in the body of a function, thus creating
1424 // GlobalValues whenever they refer to a variable. Because we are in
1425 // the context of a function, getValNonImprovising will search the functions
1426 // symbol table instead of the module symbol table for the global symbol,
1427 // which throws things all off. To get around this, we just tell
1428 // getValNonImprovising that we are at global scope here.
1429 //
1430 Function *SavedCurFn = CurFun.CurrentFunction;
1431 CurFun.CurrentFunction = 0;
1432
1433 Value *V = getValNonImprovising(Ty, $2);
1434
1435 CurFun.CurrentFunction = SavedCurFn;
1436
1437 // If this is an initializer for a constant pointer, which is referencing a
1438 // (currently) undefined variable, create a stub now that shall be replaced
1439 // in the future with the right type of variable.
1440 //
1441 if (V == 0) {
1442 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1443 const PointerType *PT = cast<PointerType>(Ty);
1444
1445 // First check to see if the forward references value is already created!
1446 PerModuleInfo::GlobalRefsType::iterator I =
1447 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1448
1449 if (I != CurModule.GlobalRefs.end()) {
1450 V = I->second; // Placeholder already exists, use it...
1451 $2.destroy();
1452 } else {
1453 std::string Name;
1454 if ($2.Type == ValID::NameVal) Name = $2.Name;
1455
1456 // Create the forward referenced global.
1457 GlobalValue *GV;
1458 if (const FunctionType *FTy =
1459 dyn_cast<FunctionType>(PT->getElementType())) {
1460 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1461 CurModule.CurrentModule);
1462 } else {
1463 GV = new GlobalVariable(PT->getElementType(), false,
1464 GlobalValue::ExternalLinkage, 0,
1465 Name, CurModule.CurrentModule);
1466 }
1467
1468 // Keep track of the fact that we have a forward ref to recycle it
1469 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1470 V = GV;
1471 }
1472 }
1473
1474 $$ = cast<GlobalValue>(V);
1475 delete $1; // Free the type handle
Reid Spencer713eedc2006-08-18 08:43:06 +00001476 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001477 }
1478 | Types ConstExpr {
1479 if ($1->get() != $2->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001480 GEN_ERROR("Mismatched types for constant expression!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001481 $$ = $2;
1482 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001483 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001484 }
1485 | Types ZEROINITIALIZER {
1486 const Type *Ty = $1->get();
1487 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencer713eedc2006-08-18 08:43:06 +00001488 GEN_ERROR("Cannot create a null initialized value of this type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001489 $$ = Constant::getNullValue(Ty);
1490 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001491 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001492 };
1493
1494ConstVal : SIntType EINT64VAL { // integral constants
1495 if (!ConstantSInt::isValueValidForType($1, $2))
Reid Spencer713eedc2006-08-18 08:43:06 +00001496 GEN_ERROR("Constant value doesn't fit in type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001497 $$ = ConstantSInt::get($1, $2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001498 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001499 }
1500 | UIntType EUINT64VAL { // integral constants
1501 if (!ConstantUInt::isValueValidForType($1, $2))
Reid Spencer713eedc2006-08-18 08:43:06 +00001502 GEN_ERROR("Constant value doesn't fit in type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001503 $$ = ConstantUInt::get($1, $2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001504 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001505 }
1506 | BOOL TRUETOK { // Boolean constants
1507 $$ = ConstantBool::True;
Reid Spencer713eedc2006-08-18 08:43:06 +00001508 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001509 }
1510 | BOOL FALSETOK { // Boolean constants
1511 $$ = ConstantBool::False;
Reid Spencer713eedc2006-08-18 08:43:06 +00001512 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001513 }
1514 | FPType FPVAL { // Float & Double constants
1515 if (!ConstantFP::isValueValidForType($1, $2))
Reid Spencer713eedc2006-08-18 08:43:06 +00001516 GEN_ERROR("Floating point constant invalid for type!!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001517 $$ = ConstantFP::get($1, $2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001518 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001519 };
1520
1521
1522ConstExpr: CAST '(' ConstVal TO Types ')' {
1523 if (!$3->getType()->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001524 GEN_ERROR("cast constant expression from a non-primitive type: '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001525 $3->getType()->getDescription() + "'!");
1526 if (!$5->get()->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001527 GEN_ERROR("cast constant expression to a non-primitive type: '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001528 $5->get()->getDescription() + "'!");
1529 $$ = ConstantExpr::getCast($3, $5->get());
1530 delete $5;
Reid Spencer713eedc2006-08-18 08:43:06 +00001531 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001532 }
1533 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1534 if (!isa<PointerType>($3->getType()))
Reid Spencer713eedc2006-08-18 08:43:06 +00001535 GEN_ERROR("GetElementPtr requires a pointer operand!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001536
1537 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1538 // indices to uint struct indices for compatibility.
1539 generic_gep_type_iterator<std::vector<Value*>::iterator>
1540 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1541 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1542 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1543 if (isa<StructType>(*GTI)) // Only change struct indices
1544 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1545 if (CUI->getType() == Type::UByteTy)
1546 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1547
1548 const Type *IdxTy =
1549 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
1550 if (!IdxTy)
Reid Spencer713eedc2006-08-18 08:43:06 +00001551 GEN_ERROR("Index list invalid for constant getelementptr!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001552
1553 std::vector<Constant*> IdxVec;
1554 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1555 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
1556 IdxVec.push_back(C);
1557 else
Reid Spencer713eedc2006-08-18 08:43:06 +00001558 GEN_ERROR("Indices to constant getelementptr must be constants!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001559
1560 delete $4;
1561
1562 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Reid Spencer713eedc2006-08-18 08:43:06 +00001563 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001564 }
1565 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1566 if ($3->getType() != Type::BoolTy)
Reid Spencer713eedc2006-08-18 08:43:06 +00001567 GEN_ERROR("Select condition must be of boolean type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001568 if ($5->getType() != $7->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001569 GEN_ERROR("Select operand types must match!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001570 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001571 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001572 }
1573 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
1574 if ($3->getType() != $5->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001575 GEN_ERROR("Binary operator types must match!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001576 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
1577 // To retain backward compatibility with these early compilers, we emit a
1578 // cast to the appropriate integer type automatically if we are in the
1579 // broken case. See PR424 for more information.
1580 if (!isa<PointerType>($3->getType())) {
1581 $$ = ConstantExpr::get($1, $3, $5);
1582 } else {
1583 const Type *IntPtrTy = 0;
1584 switch (CurModule.CurrentModule->getPointerSize()) {
1585 case Module::Pointer32: IntPtrTy = Type::IntTy; break;
1586 case Module::Pointer64: IntPtrTy = Type::LongTy; break;
Reid Spencer713eedc2006-08-18 08:43:06 +00001587 default: GEN_ERROR("invalid pointer binary constant expr!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001588 }
1589 $$ = ConstantExpr::get($1, ConstantExpr::getCast($3, IntPtrTy),
1590 ConstantExpr::getCast($5, IntPtrTy));
1591 $$ = ConstantExpr::getCast($$, $3->getType());
1592 }
Reid Spencer713eedc2006-08-18 08:43:06 +00001593 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001594 }
1595 | LogicalOps '(' ConstVal ',' ConstVal ')' {
1596 if ($3->getType() != $5->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001597 GEN_ERROR("Logical operator types must match!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001598 if (!$3->getType()->isIntegral()) {
1599 if (!isa<PackedType>($3->getType()) ||
1600 !cast<PackedType>($3->getType())->getElementType()->isIntegral())
Reid Spencer713eedc2006-08-18 08:43:06 +00001601 GEN_ERROR("Logical operator requires integral operands!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001602 }
1603 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001604 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001605 }
1606 | SetCondOps '(' ConstVal ',' ConstVal ')' {
1607 if ($3->getType() != $5->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001608 GEN_ERROR("setcc operand types must match!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001609 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001610 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001611 }
1612 | ShiftOps '(' ConstVal ',' ConstVal ')' {
1613 if ($5->getType() != Type::UByteTy)
Reid Spencer713eedc2006-08-18 08:43:06 +00001614 GEN_ERROR("Shift count for shift constant must be unsigned byte!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001615 if (!$3->getType()->isInteger())
Reid Spencer713eedc2006-08-18 08:43:06 +00001616 GEN_ERROR("Shift constant expression requires integer operand!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001617 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001618 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001619 }
1620 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Chris Lattner931565c2006-04-08 04:09:02 +00001621 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencer713eedc2006-08-18 08:43:06 +00001622 GEN_ERROR("Invalid extractelement operands!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001623 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001624 CHECK_FOR_ERROR
Chris Lattneraebccf82006-04-08 03:55:17 +00001625 }
1626 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Chris Lattner931565c2006-04-08 04:09:02 +00001627 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencer713eedc2006-08-18 08:43:06 +00001628 GEN_ERROR("Invalid insertelement operands!");
Chris Lattneraebccf82006-04-08 03:55:17 +00001629 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001630 CHECK_FOR_ERROR
Chris Lattneraebccf82006-04-08 03:55:17 +00001631 }
1632 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1633 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencer713eedc2006-08-18 08:43:06 +00001634 GEN_ERROR("Invalid shufflevector operands!");
Chris Lattneraebccf82006-04-08 03:55:17 +00001635 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001636 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001637 };
1638
Chris Lattneraebccf82006-04-08 03:55:17 +00001639
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001640// ConstVector - A list of comma separated constants.
1641ConstVector : ConstVector ',' ConstVal {
1642 ($$ = $1)->push_back($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001643 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001644 }
1645 | ConstVal {
1646 $$ = new std::vector<Constant*>();
1647 $$->push_back($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001648 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001649 };
1650
1651
1652// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1653GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
1654
1655
1656//===----------------------------------------------------------------------===//
1657// Rules to match Modules
1658//===----------------------------------------------------------------------===//
1659
1660// Module rule: Capture the result of parsing the whole file into a result
1661// variable...
1662//
1663Module : FunctionList {
1664 $$ = ParserResult = $1;
1665 CurModule.ModuleDone();
Reid Spencer713eedc2006-08-18 08:43:06 +00001666 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001667};
1668
1669// FunctionList - A list of functions, preceeded by a constant pool.
1670//
1671FunctionList : FunctionList Function {
1672 $$ = $1;
1673 CurFun.FunctionDone();
Reid Spencer713eedc2006-08-18 08:43:06 +00001674 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001675 }
1676 | FunctionList FunctionProto {
1677 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001678 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001679 }
1680 | FunctionList MODULE ASM_TOK AsmBlock {
1681 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001682 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001683 }
1684 | FunctionList IMPLEMENTATION {
1685 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001686 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001687 }
1688 | ConstPool {
1689 $$ = CurModule.CurrentModule;
1690 // Emit an error if there are any unresolved types left.
1691 if (!CurModule.LateResolveTypes.empty()) {
1692 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
Reid Spencer713eedc2006-08-18 08:43:06 +00001693 if (DID.Type == ValID::NameVal) {
1694 GEN_ERROR("Reference to an undefined type: '"+DID.getName() + "'");
1695 } else {
1696 GEN_ERROR("Reference to an undefined type: #" + itostr(DID.Num));
1697 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001698 }
Reid Spencer713eedc2006-08-18 08:43:06 +00001699 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001700 };
1701
1702// ConstPool - Constants with optional names assigned to them.
1703ConstPool : ConstPool OptAssign TYPE TypesV {
1704 // Eagerly resolve types. This is not an optimization, this is a
1705 // requirement that is due to the fact that we could have this:
1706 //
1707 // %list = type { %list * }
1708 // %list = type { %list * } ; repeated type decl
1709 //
1710 // If types are not resolved eagerly, then the two types will not be
1711 // determined to be the same type!
1712 //
1713 ResolveTypeTo($2, *$4);
1714
1715 if (!setTypeName(*$4, $2) && !$2) {
1716 // If this is a named type that is not a redefinition, add it to the slot
1717 // table.
1718 CurModule.Types.push_back(*$4);
1719 }
1720
1721 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00001722 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001723 }
1724 | ConstPool FunctionProto { // Function prototypes can be in const pool
Reid Spencer713eedc2006-08-18 08:43:06 +00001725 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001726 }
1727 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
Reid Spencer713eedc2006-08-18 08:43:06 +00001728 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001729 }
1730 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Reid Spencer713eedc2006-08-18 08:43:06 +00001731 if ($5 == 0) GEN_ERROR("Global value initializer is not a constant!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001732 CurGV = ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
1733 } GlobalVarAttributes {
1734 CurGV = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00001735 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001736 }
1737 | ConstPool OptAssign EXTERNAL GlobalType Types {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001738 CurGV = ParseGlobalVariable($2,
1739 GlobalValue::ExternalLinkage, $4, *$5, 0);
1740 delete $5;
1741 } GlobalVarAttributes {
1742 CurGV = 0;
1743 CHECK_FOR_ERROR
1744 }
1745 | ConstPool OptAssign DLLIMPORT GlobalType Types {
1746 CurGV = ParseGlobalVariable($2,
1747 GlobalValue::DLLImportLinkage, $4, *$5, 0);
1748 delete $5;
1749 } GlobalVarAttributes {
1750 CurGV = 0;
1751 CHECK_FOR_ERROR
1752 }
1753 | ConstPool OptAssign EXTERN_WEAK GlobalType Types {
1754 CurGV = ParseGlobalVariable($2,
1755 GlobalValue::ExternalWeakLinkage, $4, *$5, 0);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001756 delete $5;
1757 } GlobalVarAttributes {
1758 CurGV = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00001759 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001760 }
1761 | ConstPool TARGET TargetDefinition {
Reid Spencer713eedc2006-08-18 08:43:06 +00001762 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001763 }
1764 | ConstPool DEPLIBS '=' LibrariesDefinition {
Reid Spencer713eedc2006-08-18 08:43:06 +00001765 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001766 }
1767 | /* empty: end of list */ {
1768 };
1769
1770
1771AsmBlock : STRINGCONSTANT {
1772 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
1773 char *EndStr = UnEscapeLexed($1, true);
1774 std::string NewAsm($1, EndStr);
1775 free($1);
1776
1777 if (AsmSoFar.empty())
1778 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
1779 else
1780 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
Reid Spencer713eedc2006-08-18 08:43:06 +00001781 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001782};
1783
1784BigOrLittle : BIG { $$ = Module::BigEndian; };
1785BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1786
1787TargetDefinition : ENDIAN '=' BigOrLittle {
1788 CurModule.CurrentModule->setEndianness($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001789 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001790 }
1791 | POINTERSIZE '=' EUINT64VAL {
1792 if ($3 == 32)
1793 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1794 else if ($3 == 64)
1795 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1796 else
Reid Spencer713eedc2006-08-18 08:43:06 +00001797 GEN_ERROR("Invalid pointer size: '" + utostr($3) + "'!");
1798 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001799 }
1800 | TRIPLE '=' STRINGCONSTANT {
1801 CurModule.CurrentModule->setTargetTriple($3);
1802 free($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001803 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001804 };
1805
1806LibrariesDefinition : '[' LibList ']';
1807
1808LibList : LibList ',' STRINGCONSTANT {
1809 CurModule.CurrentModule->addLibrary($3);
1810 free($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001811 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001812 }
1813 | STRINGCONSTANT {
1814 CurModule.CurrentModule->addLibrary($1);
1815 free($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001816 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001817 }
1818 | /* empty: end of list */ {
Reid Spencer713eedc2006-08-18 08:43:06 +00001819 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001820 }
1821 ;
1822
1823//===----------------------------------------------------------------------===//
1824// Rules to match Function Headers
1825//===----------------------------------------------------------------------===//
1826
1827Name : VAR_ID | STRINGCONSTANT;
1828OptName : Name | /*empty*/ { $$ = 0; };
1829
1830ArgVal : Types OptName {
1831 if (*$1 == Type::VoidTy)
Reid Spencer713eedc2006-08-18 08:43:06 +00001832 GEN_ERROR("void typed arguments are invalid!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001833 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001834 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001835};
1836
1837ArgListH : ArgListH ',' ArgVal {
1838 $$ = $1;
1839 $1->push_back(*$3);
1840 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001841 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001842 }
1843 | ArgVal {
1844 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1845 $$->push_back(*$1);
1846 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001847 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001848 };
1849
1850ArgList : ArgListH {
1851 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001852 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001853 }
1854 | ArgListH ',' DOTDOTDOT {
1855 $$ = $1;
1856 $$->push_back(std::pair<PATypeHolder*,
1857 char*>(new PATypeHolder(Type::VoidTy), 0));
Reid Spencer713eedc2006-08-18 08:43:06 +00001858 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001859 }
1860 | DOTDOTDOT {
1861 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1862 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Reid Spencer713eedc2006-08-18 08:43:06 +00001863 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001864 }
1865 | /* empty */ {
1866 $$ = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00001867 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001868 };
1869
1870FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
1871 OptSection OptAlign {
1872 UnEscapeLexed($3);
1873 std::string FunctionName($3);
1874 free($3); // Free strdup'd memory!
1875
1876 if (!(*$2)->isFirstClassType() && *$2 != Type::VoidTy)
Reid Spencer713eedc2006-08-18 08:43:06 +00001877 GEN_ERROR("LLVM functions cannot return aggregate types!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001878
1879 std::vector<const Type*> ParamTypeList;
1880 if ($5) { // If there are arguments...
1881 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1882 I != $5->end(); ++I)
1883 ParamTypeList.push_back(I->first->get());
1884 }
1885
1886 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1887 if (isVarArg) ParamTypeList.pop_back();
1888
1889 const FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
1890 const PointerType *PFT = PointerType::get(FT);
1891 delete $2;
1892
1893 ValID ID;
1894 if (!FunctionName.empty()) {
1895 ID = ValID::create((char*)FunctionName.c_str());
1896 } else {
1897 ID = ValID::create((int)CurModule.Values[PFT].size());
1898 }
1899
1900 Function *Fn = 0;
1901 // See if this function was forward referenced. If so, recycle the object.
1902 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
1903 // Move the function to the end of the list, from whereever it was
1904 // previously inserted.
1905 Fn = cast<Function>(FWRef);
1906 CurModule.CurrentModule->getFunctionList().remove(Fn);
1907 CurModule.CurrentModule->getFunctionList().push_back(Fn);
1908 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
1909 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1910 // If this is the case, either we need to be a forward decl, or it needs
1911 // to be.
1912 if (!CurFun.isDeclare && !Fn->isExternal())
Reid Spencer713eedc2006-08-18 08:43:06 +00001913 GEN_ERROR("Redefinition of function '" + FunctionName + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001914
1915 // Make sure to strip off any argument names so we can't get conflicts.
1916 if (Fn->isExternal())
1917 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
1918 AI != AE; ++AI)
1919 AI->setName("");
1920
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001921 if (CurFun.isDeclare) {
1922 Fn->setLinkage(CurFun.Linkage);
1923 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001924 } else { // Not already defined?
1925 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1926 CurModule.CurrentModule);
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001927
1928 if (CurFun.isDeclare) {
1929 Fn->setLinkage(CurFun.Linkage);
1930 }
1931
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001932 InsertValue(Fn, CurModule.Values);
1933 }
1934
1935 CurFun.FunctionStart(Fn);
1936 Fn->setCallingConv($1);
1937 Fn->setAlignment($8);
1938 if ($7) {
1939 Fn->setSection($7);
1940 free($7);
1941 }
1942
1943 // Add all of the arguments we parsed to the function...
1944 if ($5) { // Is null if empty...
1945 if (isVarArg) { // Nuke the last entry
1946 assert($5->back().first->get() == Type::VoidTy && $5->back().second == 0&&
1947 "Not a varargs marker!");
1948 delete $5->back().first;
1949 $5->pop_back(); // Delete the last entry
1950 }
1951 Function::arg_iterator ArgIt = Fn->arg_begin();
1952 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1953 I != $5->end(); ++I, ++ArgIt) {
1954 delete I->first; // Delete the typeholder...
1955
1956 setValueName(ArgIt, I->second); // Insert arg into symtab...
1957 InsertValue(ArgIt);
1958 }
1959
1960 delete $5; // We're now done with the argument list
1961 }
Reid Spencer713eedc2006-08-18 08:43:06 +00001962 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001963};
1964
1965BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1966
1967FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
1968 $$ = CurFun.CurrentFunction;
1969
1970 // Make sure that we keep track of the linkage type even if there was a
1971 // previous "declare".
1972 $$->setLinkage($1);
1973};
1974
1975END : ENDTOK | '}'; // Allow end of '}' to end a function
1976
1977Function : BasicBlockList END {
1978 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001979 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001980};
1981
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001982FnDeclareLinkage: /*default*/ |
1983 DLLIMPORT { CurFun.Linkage = GlobalValue::DLLImportLinkage } |
1984 EXTERN_WEAK { CurFun.Linkage = GlobalValue::DLLImportLinkage };
1985
1986FunctionProto : DECLARE { CurFun.isDeclare = true; } FnDeclareLinkage FunctionHeaderH {
1987 $$ = CurFun.CurrentFunction;
1988 CurFun.FunctionDone();
1989 CHECK_FOR_ERROR
1990 };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001991
1992//===----------------------------------------------------------------------===//
1993// Rules to match Basic Blocks
1994//===----------------------------------------------------------------------===//
1995
1996OptSideEffect : /* empty */ {
1997 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00001998 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001999 }
2000 | SIDEEFFECT {
2001 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002002 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002003 };
2004
2005ConstValueRef : ESINT64VAL { // A reference to a direct constant
2006 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002007 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002008 }
2009 | EUINT64VAL {
2010 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002011 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002012 }
2013 | FPVAL { // Perhaps it's an FP constant?
2014 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002015 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002016 }
2017 | TRUETOK {
2018 $$ = ValID::create(ConstantBool::True);
Reid Spencer713eedc2006-08-18 08:43:06 +00002019 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002020 }
2021 | FALSETOK {
2022 $$ = ValID::create(ConstantBool::False);
Reid Spencer713eedc2006-08-18 08:43:06 +00002023 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002024 }
2025 | NULL_TOK {
2026 $$ = ValID::createNull();
Reid Spencer713eedc2006-08-18 08:43:06 +00002027 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002028 }
2029 | UNDEF {
2030 $$ = ValID::createUndef();
Reid Spencer713eedc2006-08-18 08:43:06 +00002031 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002032 }
2033 | ZEROINITIALIZER { // A vector zero constant.
2034 $$ = ValID::createZeroInit();
Reid Spencer713eedc2006-08-18 08:43:06 +00002035 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002036 }
2037 | '<' ConstVector '>' { // Nonempty unsized packed vector
2038 const Type *ETy = (*$2)[0]->getType();
2039 int NumElements = $2->size();
2040
2041 PackedType* pt = PackedType::get(ETy, NumElements);
2042 PATypeHolder* PTy = new PATypeHolder(
2043 HandleUpRefs(
2044 PackedType::get(
2045 ETy,
2046 NumElements)
2047 )
2048 );
2049
2050 // Verify all elements are correct type!
2051 for (unsigned i = 0; i < $2->size(); i++) {
2052 if (ETy != (*$2)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002053 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002054 ETy->getDescription() +"' as required!\nIt is of type '" +
2055 (*$2)[i]->getType()->getDescription() + "'.");
2056 }
2057
2058 $$ = ValID::create(ConstantPacked::get(pt, *$2));
2059 delete PTy; delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002060 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002061 }
2062 | ConstExpr {
2063 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002064 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002065 }
2066 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
2067 char *End = UnEscapeLexed($3, true);
2068 std::string AsmStr = std::string($3, End);
2069 End = UnEscapeLexed($5, true);
2070 std::string Constraints = std::string($5, End);
2071 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
2072 free($3);
2073 free($5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002074 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002075 };
2076
2077// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2078// another value.
2079//
2080SymbolicValueRef : INTVAL { // Is it an integer reference...?
2081 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002082 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002083 }
2084 | Name { // Is it a named reference...?
2085 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002086 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002087 };
2088
2089// ValueRef - A reference to a definition... either constant or symbolic
2090ValueRef : SymbolicValueRef | ConstValueRef;
2091
2092
2093// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2094// type immediately preceeds the value reference, and allows complex constant
2095// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2096ResolvedVal : Types ValueRef {
2097 $$ = getVal(*$1, $2); delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002098 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002099 };
2100
2101BasicBlockList : BasicBlockList BasicBlock {
2102 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002103 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002104 }
2105 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
2106 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002107 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002108 };
2109
2110
2111// Basic blocks are terminated by branching instructions:
2112// br, br/cc, switch, ret
2113//
2114BasicBlock : InstructionList OptAssign BBTerminatorInst {
2115 setValueName($3, $2);
2116 InsertValue($3);
2117
2118 $1->getInstList().push_back($3);
2119 InsertValue($1);
2120 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002121 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002122 };
2123
2124InstructionList : InstructionList Inst {
2125 $1->getInstList().push_back($2);
2126 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002127 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002128 }
2129 | /* empty */ {
2130 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
2131
2132 // Make sure to move the basic block to the correct location in the
2133 // function, instead of leaving it inserted wherever it was first
2134 // referenced.
2135 Function::BasicBlockListType &BBL =
2136 CurFun.CurrentFunction->getBasicBlockList();
2137 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer713eedc2006-08-18 08:43:06 +00002138 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002139 }
2140 | LABELSTR {
2141 $$ = CurBB = getBBVal(ValID::create($1), true);
2142
2143 // Make sure to move the basic block to the correct location in the
2144 // function, instead of leaving it inserted wherever it was first
2145 // referenced.
2146 Function::BasicBlockListType &BBL =
2147 CurFun.CurrentFunction->getBasicBlockList();
2148 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer713eedc2006-08-18 08:43:06 +00002149 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002150 };
2151
2152BBTerminatorInst : RET ResolvedVal { // Return with a result...
2153 $$ = new ReturnInst($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00002154 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002155 }
2156 | RET VOID { // Return with no result...
2157 $$ = new ReturnInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002158 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002159 }
2160 | BR LABEL ValueRef { // Unconditional Branch...
2161 $$ = new BranchInst(getBBVal($3));
Reid Spencer713eedc2006-08-18 08:43:06 +00002162 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002163 } // Conditional Branch...
2164 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
2165 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
Reid Spencer713eedc2006-08-18 08:43:06 +00002166 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002167 }
2168 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
2169 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), $8->size());
2170 $$ = S;
2171
2172 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2173 E = $8->end();
2174 for (; I != E; ++I) {
2175 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2176 S->addCase(CI, I->second);
2177 else
Reid Spencer713eedc2006-08-18 08:43:06 +00002178 GEN_ERROR("Switch case is constant, but not a simple integer!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002179 }
2180 delete $8;
Reid Spencer713eedc2006-08-18 08:43:06 +00002181 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002182 }
2183 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
2184 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), 0);
2185 $$ = S;
Reid Spencer713eedc2006-08-18 08:43:06 +00002186 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002187 }
2188 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
2189 TO LABEL ValueRef UNWIND LABEL ValueRef {
2190 const PointerType *PFTy;
2191 const FunctionType *Ty;
2192
2193 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
2194 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2195 // Pull out the types of all of the arguments...
2196 std::vector<const Type*> ParamTypes;
2197 if ($6) {
2198 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
2199 I != E; ++I)
2200 ParamTypes.push_back((*I)->getType());
2201 }
2202
2203 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2204 if (isVarArg) ParamTypes.pop_back();
2205
2206 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
2207 PFTy = PointerType::get(Ty);
2208 }
2209
2210 Value *V = getVal(PFTy, $4); // Get the function we're calling...
2211
2212 BasicBlock *Normal = getBBVal($10);
2213 BasicBlock *Except = getBBVal($13);
2214
2215 // Create the call node...
2216 if (!$6) { // Has no arguments?
2217 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
2218 } else { // Has arguments?
2219 // Loop through FunctionType's arguments and ensure they are specified
2220 // correctly!
2221 //
2222 FunctionType::param_iterator I = Ty->param_begin();
2223 FunctionType::param_iterator E = Ty->param_end();
2224 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
2225
2226 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
2227 if ((*ArgI)->getType() != *I)
Reid Spencer713eedc2006-08-18 08:43:06 +00002228 GEN_ERROR("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002229 (*I)->getDescription() + "'!");
2230
2231 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002232 GEN_ERROR("Invalid number of parameters detected!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002233
2234 $$ = new InvokeInst(V, Normal, Except, *$6);
2235 }
2236 cast<InvokeInst>($$)->setCallingConv($2);
2237
2238 delete $3;
2239 delete $6;
Reid Spencer713eedc2006-08-18 08:43:06 +00002240 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002241 }
2242 | UNWIND {
2243 $$ = new UnwindInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002244 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002245 }
2246 | UNREACHABLE {
2247 $$ = new UnreachableInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002248 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002249 };
2250
2251
2252
2253JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2254 $$ = $1;
2255 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
2256 if (V == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002257 GEN_ERROR("May only switch on a constant pool value!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002258
2259 $$->push_back(std::make_pair(V, getBBVal($6)));
Reid Spencer713eedc2006-08-18 08:43:06 +00002260 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002261 }
2262 | IntType ConstValueRef ',' LABEL ValueRef {
2263 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
2264 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
2265
2266 if (V == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002267 GEN_ERROR("May only switch on a constant pool value!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002268
2269 $$->push_back(std::make_pair(V, getBBVal($5)));
Reid Spencer713eedc2006-08-18 08:43:06 +00002270 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002271 };
2272
2273Inst : OptAssign InstVal {
2274 // Is this definition named?? if so, assign the name...
2275 setValueName($2, $1);
2276 InsertValue($2);
2277 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002278 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002279};
2280
2281PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
2282 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
2283 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
2284 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002285 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002286 }
2287 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2288 $$ = $1;
2289 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
2290 getBBVal($6)));
Reid Spencer713eedc2006-08-18 08:43:06 +00002291 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002292 };
2293
2294
2295ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
2296 $$ = new std::vector<Value*>();
2297 $$->push_back($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002298 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002299 }
2300 | ValueRefList ',' ResolvedVal {
2301 $$ = $1;
2302 $1->push_back($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002303 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002304 };
2305
2306// ValueRefListE - Just like ValueRefList, except that it may also be empty!
2307ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
2308
2309OptTailCall : TAIL CALL {
2310 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002311 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002312 }
2313 | CALL {
2314 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002315 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002316 };
2317
2318
2319
2320InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
2321 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
2322 !isa<PackedType>((*$2).get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002323 GEN_ERROR(
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002324 "Arithmetic operator requires integer, FP, or packed operands!");
2325 if (isa<PackedType>((*$2).get()) && $1 == Instruction::Rem)
Reid Spencer713eedc2006-08-18 08:43:06 +00002326 GEN_ERROR("Rem not supported on packed types!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002327 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
2328 if ($$ == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002329 GEN_ERROR("binary operator returned null!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002330 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002331 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002332 }
2333 | LogicalOps Types ValueRef ',' ValueRef {
2334 if (!(*$2)->isIntegral()) {
2335 if (!isa<PackedType>($2->get()) ||
2336 !cast<PackedType>($2->get())->getElementType()->isIntegral())
Reid Spencer713eedc2006-08-18 08:43:06 +00002337 GEN_ERROR("Logical operator requires integral operands!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002338 }
2339 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
2340 if ($$ == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002341 GEN_ERROR("binary operator returned null!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002342 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002343 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002344 }
2345 | SetCondOps Types ValueRef ',' ValueRef {
2346 if(isa<PackedType>((*$2).get())) {
Reid Spencer713eedc2006-08-18 08:43:06 +00002347 GEN_ERROR(
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002348 "PackedTypes currently not supported in setcc instructions!");
2349 }
2350 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
2351 if ($$ == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002352 GEN_ERROR("binary operator returned null!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002353 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002354 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002355 }
2356 | NOT ResolvedVal {
2357 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
2358 << " Replacing with 'xor'.\n";
2359
2360 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
2361 if (Ones == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002362 GEN_ERROR("Expected integral type for not instruction!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002363
2364 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
2365 if ($$ == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002366 GEN_ERROR("Could not create a xor instruction!");
2367 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002368 }
2369 | ShiftOps ResolvedVal ',' ResolvedVal {
2370 if ($4->getType() != Type::UByteTy)
Reid Spencer713eedc2006-08-18 08:43:06 +00002371 GEN_ERROR("Shift amount must be ubyte!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002372 if (!$2->getType()->isInteger())
Reid Spencer713eedc2006-08-18 08:43:06 +00002373 GEN_ERROR("Shift constant expression requires integer operand!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002374 $$ = new ShiftInst($1, $2, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002375 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002376 }
2377 | CAST ResolvedVal TO Types {
2378 if (!$4->get()->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002379 GEN_ERROR("cast instruction to a non-primitive type: '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002380 $4->get()->getDescription() + "'!");
2381 $$ = new CastInst($2, *$4);
2382 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00002383 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002384 }
2385 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2386 if ($2->getType() != Type::BoolTy)
Reid Spencer713eedc2006-08-18 08:43:06 +00002387 GEN_ERROR("select condition must be boolean!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002388 if ($4->getType() != $6->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002389 GEN_ERROR("select value types should match!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002390 $$ = new SelectInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002391 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002392 }
2393 | VAARG ResolvedVal ',' Types {
2394 NewVarArgs = true;
2395 $$ = new VAArgInst($2, *$4);
2396 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00002397 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002398 }
2399 | VAARG_old ResolvedVal ',' Types {
2400 ObsoleteVarArgs = true;
2401 const Type* ArgTy = $2->getType();
2402 Function* NF = CurModule.CurrentModule->
2403 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
2404
2405 //b = vaarg a, t ->
2406 //foo = alloca 1 of t
2407 //bar = vacopy a
2408 //store bar -> foo
2409 //b = vaarg foo, t
2410 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix");
2411 CurBB->getInstList().push_back(foo);
2412 CallInst* bar = new CallInst(NF, $2);
2413 CurBB->getInstList().push_back(bar);
2414 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2415 $$ = new VAArgInst(foo, *$4);
2416 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00002417 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002418 }
2419 | VANEXT_old ResolvedVal ',' Types {
2420 ObsoleteVarArgs = true;
2421 const Type* ArgTy = $2->getType();
2422 Function* NF = CurModule.CurrentModule->
2423 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
2424
2425 //b = vanext a, t ->
2426 //foo = alloca 1 of t
2427 //bar = vacopy a
2428 //store bar -> foo
2429 //tmp = vaarg foo, t
2430 //b = load foo
2431 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix");
2432 CurBB->getInstList().push_back(foo);
2433 CallInst* bar = new CallInst(NF, $2);
2434 CurBB->getInstList().push_back(bar);
2435 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2436 Instruction* tmp = new VAArgInst(foo, *$4);
2437 CurBB->getInstList().push_back(tmp);
2438 $$ = new LoadInst(foo);
2439 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00002440 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002441 }
2442 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Chris Lattner931565c2006-04-08 04:09:02 +00002443 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencer713eedc2006-08-18 08:43:06 +00002444 GEN_ERROR("Invalid extractelement operands!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002445 $$ = new ExtractElementInst($2, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002446 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002447 }
2448 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Chris Lattner931565c2006-04-08 04:09:02 +00002449 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencer713eedc2006-08-18 08:43:06 +00002450 GEN_ERROR("Invalid insertelement operands!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002451 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002452 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002453 }
Chris Lattner9ff96a72006-04-08 01:18:56 +00002454 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Chris Lattneraebccf82006-04-08 03:55:17 +00002455 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencer713eedc2006-08-18 08:43:06 +00002456 GEN_ERROR("Invalid shufflevector operands!");
Chris Lattner9ff96a72006-04-08 01:18:56 +00002457 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002458 CHECK_FOR_ERROR
Chris Lattner9ff96a72006-04-08 01:18:56 +00002459 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002460 | PHI_TOK PHIList {
2461 const Type *Ty = $2->front().first->getType();
2462 if (!Ty->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002463 GEN_ERROR("PHI node operands must be of first class type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002464 $$ = new PHINode(Ty);
2465 ((PHINode*)$$)->reserveOperandSpace($2->size());
2466 while ($2->begin() != $2->end()) {
2467 if ($2->front().first->getType() != Ty)
Reid Spencer713eedc2006-08-18 08:43:06 +00002468 GEN_ERROR("All elements of a PHI node must be of the same type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002469 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
2470 $2->pop_front();
2471 }
2472 delete $2; // Free the list...
Reid Spencer713eedc2006-08-18 08:43:06 +00002473 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002474 }
2475 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
2476 const PointerType *PFTy;
2477 const FunctionType *Ty;
2478
2479 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
2480 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2481 // Pull out the types of all of the arguments...
2482 std::vector<const Type*> ParamTypes;
2483 if ($6) {
2484 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
2485 I != E; ++I)
2486 ParamTypes.push_back((*I)->getType());
2487 }
2488
2489 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2490 if (isVarArg) ParamTypes.pop_back();
2491
2492 if (!(*$3)->isFirstClassType() && *$3 != Type::VoidTy)
Reid Spencer713eedc2006-08-18 08:43:06 +00002493 GEN_ERROR("LLVM functions cannot return aggregate types!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002494
2495 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
2496 PFTy = PointerType::get(Ty);
2497 }
2498
2499 Value *V = getVal(PFTy, $4); // Get the function we're calling...
2500
2501 // Create the call node...
2502 if (!$6) { // Has no arguments?
2503 // Make sure no arguments is a good thing!
2504 if (Ty->getNumParams() != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002505 GEN_ERROR("No arguments passed to a function that "
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002506 "expects arguments!");
2507
2508 $$ = new CallInst(V, std::vector<Value*>());
2509 } else { // Has arguments?
2510 // Loop through FunctionType's arguments and ensure they are specified
2511 // correctly!
2512 //
2513 FunctionType::param_iterator I = Ty->param_begin();
2514 FunctionType::param_iterator E = Ty->param_end();
2515 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
2516
2517 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
2518 if ((*ArgI)->getType() != *I)
Reid Spencer713eedc2006-08-18 08:43:06 +00002519 GEN_ERROR("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002520 (*I)->getDescription() + "'!");
2521
2522 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002523 GEN_ERROR("Invalid number of parameters detected!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002524
2525 $$ = new CallInst(V, *$6);
2526 }
2527 cast<CallInst>($$)->setTailCall($1);
2528 cast<CallInst>($$)->setCallingConv($2);
2529 delete $3;
2530 delete $6;
Reid Spencer713eedc2006-08-18 08:43:06 +00002531 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002532 }
2533 | MemoryInst {
2534 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002535 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002536 };
2537
2538
2539// IndexList - List of indices for GEP based instructions...
2540IndexList : ',' ValueRefList {
2541 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002542 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002543 } | /* empty */ {
2544 $$ = new std::vector<Value*>();
Reid Spencer713eedc2006-08-18 08:43:06 +00002545 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002546 };
2547
2548OptVolatile : VOLATILE {
2549 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002550 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002551 }
2552 | /* empty */ {
2553 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002554 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002555 };
2556
2557
2558
2559MemoryInst : MALLOC Types OptCAlign {
2560 $$ = new MallocInst(*$2, 0, $3);
2561 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002562 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002563 }
2564 | MALLOC Types ',' UINT ValueRef OptCAlign {
2565 $$ = new MallocInst(*$2, getVal($4, $5), $6);
2566 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002567 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002568 }
2569 | ALLOCA Types OptCAlign {
2570 $$ = new AllocaInst(*$2, 0, $3);
2571 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002572 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002573 }
2574 | ALLOCA Types ',' UINT ValueRef OptCAlign {
2575 $$ = new AllocaInst(*$2, getVal($4, $5), $6);
2576 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002577 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002578 }
2579 | FREE ResolvedVal {
2580 if (!isa<PointerType>($2->getType()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002581 GEN_ERROR("Trying to free nonpointer type " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002582 $2->getType()->getDescription() + "!");
2583 $$ = new FreeInst($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00002584 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002585 }
2586
2587 | OptVolatile LOAD Types ValueRef {
2588 if (!isa<PointerType>($3->get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002589 GEN_ERROR("Can't load from nonpointer type: " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002590 (*$3)->getDescription());
2591 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002592 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002593 (*$3)->getDescription());
2594 $$ = new LoadInst(getVal(*$3, $4), "", $1);
2595 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00002596 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002597 }
2598 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2599 const PointerType *PT = dyn_cast<PointerType>($5->get());
2600 if (!PT)
Reid Spencer713eedc2006-08-18 08:43:06 +00002601 GEN_ERROR("Can't store to a nonpointer type: " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002602 (*$5)->getDescription());
2603 const Type *ElTy = PT->getElementType();
2604 if (ElTy != $3->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002605 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002606 "' into space of type '" + ElTy->getDescription() + "'!");
2607
2608 $$ = new StoreInst($3, getVal(*$5, $6), $1);
2609 delete $5;
Reid Spencer713eedc2006-08-18 08:43:06 +00002610 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002611 }
2612 | GETELEMENTPTR Types ValueRef IndexList {
2613 if (!isa<PointerType>($2->get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002614 GEN_ERROR("getelementptr insn requires pointer operand!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002615
2616 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2617 // indices to uint struct indices for compatibility.
2618 generic_gep_type_iterator<std::vector<Value*>::iterator>
2619 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2620 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2621 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2622 if (isa<StructType>(*GTI)) // Only change struct indices
2623 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2624 if (CUI->getType() == Type::UByteTy)
2625 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2626
2627 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Reid Spencer713eedc2006-08-18 08:43:06 +00002628 GEN_ERROR("Invalid getelementptr indices for type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002629 (*$2)->getDescription()+ "'!");
2630 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2631 delete $2; delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00002632 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002633 };
2634
2635
2636%%
Reid Spencer713eedc2006-08-18 08:43:06 +00002637
2638void llvm::GenerateError(const std::string &message, int LineNo) {
2639 if (LineNo == -1) LineNo = llvmAsmlineno;
2640 // TODO: column number in exception
2641 if (TheParseError)
2642 TheParseError->setError(CurFilename, message, LineNo);
2643 TriggerError = 1;
2644}
2645
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002646int yyerror(const char *ErrorMsg) {
2647 std::string where
2648 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2649 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
2650 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
2651 if (yychar == YYEMPTY || yychar == 0)
2652 errMsg += "end-of-file.";
2653 else
2654 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Reid Spencer713eedc2006-08-18 08:43:06 +00002655 GenerateError(errMsg);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002656 return 0;
2657}