blob: 1d1558eb6befbe39dada83a64483ef52d532f24d [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"
Reid Spencerd5e19442006-12-01 00:33:46 +000025#include "llvm/Support/Streams.h"
Chris Lattnerf20e61f2006-02-15 07:22:58 +000026#include <algorithm>
Chris Lattnerf20e61f2006-02-15 07:22:58 +000027#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;
Reid Spencerff359002006-10-09 17:36:59 +000042#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYABORT; } }
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
Reid Spencerd5e19442006-12-01 00:33:46 +000061#define UR_OUT(X) llvm_cerr << X
Chris Lattnerf20e61f2006-02-15 07:22:58 +000062#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;
Reid Spencer55f1fbe2006-11-28 07:29:44 +000086 std::vector<PATypeHolder> Types;
87 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattnerf20e61f2006-02-15 07:22:58 +000088
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);
Reid Spencer309080a2006-09-28 19:28:24 +0000109 if (TriggerError)
110 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000111
112 // Check to make sure that all global value forward references have been
113 // resolved!
114 //
115 if (!GlobalRefs.empty()) {
116 std::string UndefinedReferences = "Unresolved global references exist:\n";
117
118 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
119 I != E; ++I) {
120 UndefinedReferences += " " + I->first.first->getDescription() + " " +
121 I->first.second.getName() + "\n";
122 }
Reid Spencer713eedc2006-08-18 08:43:06 +0000123 GenerateError(UndefinedReferences);
Reid Spencer309080a2006-09-28 19:28:24 +0000124 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000125 }
126
127 // Look for intrinsic functions and CallInst that need to be upgraded
Chris Lattner9ff96a72006-04-08 01:18:56 +0000128 for (Module::iterator FI = CurrentModule->begin(),
129 FE = CurrentModule->end(); FI != FE; )
Chris Lattneradf5ec62006-03-04 07:53:41 +0000130 UpgradeCallsToIntrinsic(FI++);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000131
132 Values.clear(); // Clear out function local definitions
133 Types.clear();
134 CurrentModule = 0;
135 }
136
137 // GetForwardRefForGlobal - Check to see if there is a forward reference
138 // for this global. If so, remove it from the GlobalRefs map and return it.
139 // If not, just return null.
140 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
141 // Check to see if there is a forward reference to this global variable...
142 // if there is, eliminate it and patch the reference to use the new def'n.
143 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
144 GlobalValue *Ret = 0;
145 if (I != GlobalRefs.end()) {
146 Ret = I->second;
147 GlobalRefs.erase(I);
148 }
149 return Ret;
150 }
151} CurModule;
152
153static struct PerFunctionInfo {
154 Function *CurrentFunction; // Pointer to current function being created
155
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000156 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000157 std::map<const Type*, ValueList> LateResolveValues;
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000158 bool isDeclare; // Is this function a forward declararation?
159 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000160
161 /// BBForwardRefs - When we see forward references to basic blocks, keep
162 /// track of them here.
163 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
164 std::vector<BasicBlock*> NumberedBlocks;
165 unsigned NextBBNum;
166
167 inline PerFunctionInfo() {
168 CurrentFunction = 0;
169 isDeclare = false;
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000170 Linkage = GlobalValue::ExternalLinkage;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000171 }
172
173 inline void FunctionStart(Function *M) {
174 CurrentFunction = M;
175 NextBBNum = 0;
176 }
177
178 void FunctionDone() {
179 NumberedBlocks.clear();
180
181 // Any forward referenced blocks left?
Reid Spencer309080a2006-09-28 19:28:24 +0000182 if (!BBForwardRefs.empty()) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000183 GenerateError("Undefined reference to label " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000184 BBForwardRefs.begin()->first->getName());
Reid Spencer309080a2006-09-28 19:28:24 +0000185 return;
186 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000187
188 // Resolve all forward references now.
189 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
190
191 Values.clear(); // Clear out function local definitions
192 CurrentFunction = 0;
193 isDeclare = false;
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000194 Linkage = GlobalValue::ExternalLinkage;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000195 }
196} CurFun; // Info for the current function...
197
198static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
199
200
201//===----------------------------------------------------------------------===//
202// Code to handle definitions of all the types
203//===----------------------------------------------------------------------===//
204
205static int InsertValue(Value *V,
206 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
207 if (V->hasName()) return -1; // Is this a numbered definition?
208
209 // Yes, insert the value into the value table...
210 ValueList &List = ValueTab[V->getType()];
211 List.push_back(V);
212 return List.size()-1;
213}
214
215static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
216 switch (D.Type) {
217 case ValID::NumberVal: // Is it a numbered definition?
218 // Module constants occupy the lowest numbered slots...
219 if ((unsigned)D.Num < CurModule.Types.size())
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000220 return CurModule.Types[(unsigned)D.Num];
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000221 break;
222 case ValID::NameVal: // Is it a named definition?
223 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
224 D.destroy(); // Free old strdup'd memory...
225 return N;
226 }
227 break;
228 default:
Reid Spencer713eedc2006-08-18 08:43:06 +0000229 GenerateError("Internal parser error: Invalid symbol type reference!");
Reid Spencer309080a2006-09-28 19:28:24 +0000230 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000231 }
232
233 // If we reached here, we referenced either a symbol that we don't know about
234 // or an id number that hasn't been read yet. We may be referencing something
235 // forward, so just create an entry to be resolved later and get to it...
236 //
237 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
238
239
240 if (inFunctionScope()) {
Reid Spencer309080a2006-09-28 19:28:24 +0000241 if (D.Type == ValID::NameVal) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000242 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer309080a2006-09-28 19:28:24 +0000243 return 0;
244 } else {
Reid Spencer713eedc2006-08-18 08:43:06 +0000245 GenerateError("Reference to an undefined type: #" + itostr(D.Num));
Reid Spencer309080a2006-09-28 19:28:24 +0000246 return 0;
247 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000248 }
249
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000250 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000251 if (I != CurModule.LateResolveTypes.end())
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000252 return I->second;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000253
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000254 Type *Typ = OpaqueType::get();
255 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
256 return Typ;
257}
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000258
259static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
260 SymbolTable &SymTab =
261 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
262 CurModule.CurrentModule->getSymbolTable();
263 return SymTab.lookup(Ty, Name);
264}
265
266// getValNonImprovising - Look up the value specified by the provided type and
267// the provided ValID. If the value exists and has already been defined, return
268// it. Otherwise return null.
269//
270static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Reid Spencer309080a2006-09-28 19:28:24 +0000271 if (isa<FunctionType>(Ty)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000272 GenerateError("Functions are not values and "
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000273 "must be referenced as pointers");
Reid Spencer309080a2006-09-28 19:28:24 +0000274 return 0;
275 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000276
277 switch (D.Type) {
278 case ValID::NumberVal: { // Is it a numbered definition?
279 unsigned Num = (unsigned)D.Num;
280
281 // Module constants occupy the lowest numbered slots...
282 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
283 if (VI != CurModule.Values.end()) {
284 if (Num < VI->second.size())
285 return VI->second[Num];
286 Num -= VI->second.size();
287 }
288
289 // Make sure that our type is within bounds
290 VI = CurFun.Values.find(Ty);
291 if (VI == CurFun.Values.end()) return 0;
292
293 // Check that the number is within bounds...
294 if (VI->second.size() <= Num) return 0;
295
296 return VI->second[Num];
297 }
298
299 case ValID::NameVal: { // Is it a named definition?
300 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
301 if (N == 0) return 0;
302
303 D.destroy(); // Free old strdup'd memory...
304 return N;
305 }
306
307 // Check to make sure that "Ty" is an integral type, and that our
308 // value will fit into the specified type...
309 case ValID::ConstSIntVal: // Is it a constant pool reference??
Reid Spencere0fc4df2006-10-20 07:07:24 +0000310 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000311 GenerateError("Signed integral constant '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000312 itostr(D.ConstPool64) + "' is invalid for type '" +
313 Ty->getDescription() + "'!");
Reid Spencer309080a2006-09-28 19:28:24 +0000314 return 0;
315 }
Reid Spencere0fc4df2006-10-20 07:07:24 +0000316 return ConstantInt::get(Ty, D.ConstPool64);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000317
318 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Reid Spencere0fc4df2006-10-20 07:07:24 +0000319 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
320 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000321 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000322 "' is invalid or out of range!");
Reid Spencer309080a2006-09-28 19:28:24 +0000323 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000324 } else { // This is really a signed reference. Transmogrify.
Reid Spencere0fc4df2006-10-20 07:07:24 +0000325 return ConstantInt::get(Ty, D.ConstPool64);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000326 }
327 } else {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000328 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000329 }
330
331 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Reid Spencer309080a2006-09-28 19:28:24 +0000332 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000333 GenerateError("FP constant invalid for type!!");
Reid Spencer309080a2006-09-28 19:28:24 +0000334 return 0;
335 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000336 return ConstantFP::get(Ty, D.ConstPoolFP);
337
338 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer309080a2006-09-28 19:28:24 +0000339 if (!isa<PointerType>(Ty)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000340 GenerateError("Cannot create a a non pointer null!");
Reid Spencer309080a2006-09-28 19:28:24 +0000341 return 0;
342 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000343 return ConstantPointerNull::get(cast<PointerType>(Ty));
344
345 case ValID::ConstUndefVal: // Is it an undef value?
346 return UndefValue::get(Ty);
347
348 case ValID::ConstZeroVal: // Is it a zero value?
349 return Constant::getNullValue(Ty);
350
351 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer309080a2006-09-28 19:28:24 +0000352 if (D.ConstantValue->getType() != Ty) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000353 GenerateError("Constant expression type different from required type!");
Reid Spencer309080a2006-09-28 19:28:24 +0000354 return 0;
355 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000356 return D.ConstantValue;
357
358 case ValID::InlineAsmVal: { // Inline asm expression
359 const PointerType *PTy = dyn_cast<PointerType>(Ty);
360 const FunctionType *FTy =
361 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer309080a2006-09-28 19:28:24 +0000362 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000363 GenerateError("Invalid type for asm constraint string!");
Reid Spencer309080a2006-09-28 19:28:24 +0000364 return 0;
365 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000366 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
367 D.IAD->HasSideEffects);
368 D.destroy(); // Free InlineAsmDescriptor.
369 return IA;
370 }
371 default:
372 assert(0 && "Unhandled case!");
373 return 0;
374 } // End of switch
375
376 assert(0 && "Unhandled case!");
377 return 0;
378}
379
380// getVal - This function is identical to getValNonImprovising, except that if a
381// value is not already defined, it "improvises" by creating a placeholder var
382// that looks and acts just like the requested variable. When the value is
383// defined later, all uses of the placeholder variable are replaced with the
384// real thing.
385//
386static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer309080a2006-09-28 19:28:24 +0000387 if (Ty == Type::LabelTy) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000388 GenerateError("Cannot use a basic block here");
Reid Spencer309080a2006-09-28 19:28:24 +0000389 return 0;
390 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000391
392 // See if the value has already been defined.
393 Value *V = getValNonImprovising(Ty, ID);
394 if (V) return V;
Reid Spencer309080a2006-09-28 19:28:24 +0000395 if (TriggerError) return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000396
Reid Spencer309080a2006-09-28 19:28:24 +0000397 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000398 GenerateError("Invalid use of a composite type!");
Reid Spencer309080a2006-09-28 19:28:24 +0000399 return 0;
400 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000401
402 // If we reached here, we referenced either a symbol that we don't know about
403 // or an id number that hasn't been read yet. We may be referencing something
404 // forward, so just create an entry to be resolved later and get to it...
405 //
406 V = new Argument(Ty);
407
408 // Remember where this forward reference came from. FIXME, shouldn't we try
409 // to recycle these things??
410 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
411 llvmAsmlineno)));
412
413 if (inFunctionScope())
414 InsertValue(V, CurFun.LateResolveValues);
415 else
416 InsertValue(V, CurModule.LateResolveValues);
417 return V;
418}
419
420/// getBBVal - This is used for two purposes:
421/// * If isDefinition is true, a new basic block with the specified ID is being
422/// defined.
423/// * If isDefinition is true, this is a reference to a basic block, which may
424/// or may not be a forward reference.
425///
426static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
427 assert(inFunctionScope() && "Can't get basic block at global scope!");
428
429 std::string Name;
430 BasicBlock *BB = 0;
431 switch (ID.Type) {
Reid Spencer309080a2006-09-28 19:28:24 +0000432 default:
433 GenerateError("Illegal label reference " + ID.getName());
434 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000435 case ValID::NumberVal: // Is it a numbered definition?
436 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
437 CurFun.NumberedBlocks.resize(ID.Num+1);
438 BB = CurFun.NumberedBlocks[ID.Num];
439 break;
440 case ValID::NameVal: // Is it a named definition?
441 Name = ID.Name;
442 if (Value *N = CurFun.CurrentFunction->
443 getSymbolTable().lookup(Type::LabelTy, Name))
444 BB = cast<BasicBlock>(N);
445 break;
446 }
447
448 // See if the block has already been defined.
449 if (BB) {
450 // If this is the definition of the block, make sure the existing value was
451 // just a forward reference. If it was a forward reference, there will be
452 // an entry for it in the PlaceHolderInfo map.
Reid Spencer309080a2006-09-28 19:28:24 +0000453 if (isDefinition && !CurFun.BBForwardRefs.erase(BB)) {
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000454 // The existing value was a definition, not a forward reference.
Reid Spencer713eedc2006-08-18 08:43:06 +0000455 GenerateError("Redefinition of label " + ID.getName());
Reid Spencer309080a2006-09-28 19:28:24 +0000456 return 0;
457 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000458
459 ID.destroy(); // Free strdup'd memory.
460 return BB;
461 }
462
463 // Otherwise this block has not been seen before.
464 BB = new BasicBlock("", CurFun.CurrentFunction);
465 if (ID.Type == ValID::NameVal) {
466 BB->setName(ID.Name);
467 } else {
468 CurFun.NumberedBlocks[ID.Num] = BB;
469 }
470
471 // If this is not a definition, keep track of it so we can use it as a forward
472 // reference.
473 if (!isDefinition) {
474 // Remember where this forward reference came from.
475 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
476 } else {
477 // The forward declaration could have been inserted anywhere in the
478 // function: insert it into the correct place now.
479 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
480 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
481 }
482 ID.destroy();
483 return BB;
484}
485
486
487//===----------------------------------------------------------------------===//
488// Code to handle forward references in instructions
489//===----------------------------------------------------------------------===//
490//
491// This code handles the late binding needed with statements that reference
492// values not defined yet... for example, a forward branch, or the PHI node for
493// a loop body.
494//
495// This keeps a table (CurFun.LateResolveValues) of all such forward references
496// and back patchs after we are done.
497//
498
499// ResolveDefinitions - If we could not resolve some defs at parsing
500// time (forward branches, phi functions for loops, etc...) resolve the
501// defs now...
502//
503static void
504ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
505 std::map<const Type*,ValueList> *FutureLateResolvers) {
506 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
507 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
508 E = LateResolvers.end(); LRI != E; ++LRI) {
509 ValueList &List = LRI->second;
510 while (!List.empty()) {
511 Value *V = List.back();
512 List.pop_back();
513
514 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
515 CurModule.PlaceHolderInfo.find(V);
516 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
517
518 ValID &DID = PHI->second.first;
519
520 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Reid Spencer309080a2006-09-28 19:28:24 +0000521 if (TriggerError)
522 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000523 if (TheRealValue) {
524 V->replaceAllUsesWith(TheRealValue);
525 delete V;
526 CurModule.PlaceHolderInfo.erase(PHI);
527 } else if (FutureLateResolvers) {
528 // Functions have their unresolved items forwarded to the module late
529 // resolver table
530 InsertValue(V, *FutureLateResolvers);
531 } else {
Reid Spencer309080a2006-09-28 19:28:24 +0000532 if (DID.Type == ValID::NameVal) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000533 GenerateError("Reference to an invalid definition: '" +DID.getName()+
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000534 "' of type '" + V->getType()->getDescription() + "'",
535 PHI->second.second);
Reid Spencer309080a2006-09-28 19:28:24 +0000536 return;
537 } else {
Reid Spencer713eedc2006-08-18 08:43:06 +0000538 GenerateError("Reference to an invalid definition: #" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000539 itostr(DID.Num) + " of type '" +
540 V->getType()->getDescription() + "'",
541 PHI->second.second);
Reid Spencer309080a2006-09-28 19:28:24 +0000542 return;
543 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000544 }
545 }
546 }
547
548 LateResolvers.clear();
549}
550
551// ResolveTypeTo - A brand new type was just declared. This means that (if
552// name is not null) things referencing Name can be resolved. Otherwise, things
553// refering to the number can be resolved. Do this now.
554//
555static void ResolveTypeTo(char *Name, const Type *ToTy) {
556 ValID D;
557 if (Name) D = ValID::create(Name);
558 else D = ValID::create((int)CurModule.Types.size());
559
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000560 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000561 CurModule.LateResolveTypes.find(D);
562 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000563 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000564 CurModule.LateResolveTypes.erase(I);
565 }
566}
567
568// setValueName - Set the specified value to the name given. The name may be
569// null potentially, in which case this is a noop. The string passed in is
570// assumed to be a malloc'd string buffer, and is free'd by this function.
571//
572static void setValueName(Value *V, char *NameStr) {
573 if (NameStr) {
574 std::string Name(NameStr); // Copy string
575 free(NameStr); // Free old string
576
Reid Spencer309080a2006-09-28 19:28:24 +0000577 if (V->getType() == Type::VoidTy) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000578 GenerateError("Can't assign name '" + Name+"' to value with void type!");
Reid Spencer309080a2006-09-28 19:28:24 +0000579 return;
580 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000581
582 assert(inFunctionScope() && "Must be in function scope!");
583 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
Reid Spencer309080a2006-09-28 19:28:24 +0000584 if (ST.lookup(V->getType(), Name)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000585 GenerateError("Redefinition of value named '" + Name + "' in the '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000586 V->getType()->getDescription() + "' type plane!");
Reid Spencer309080a2006-09-28 19:28:24 +0000587 return;
588 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000589
590 // Set the name.
591 V->setName(Name);
592 }
593}
594
595/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
596/// this is a declaration, otherwise it is a definition.
597static GlobalVariable *
598ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
599 bool isConstantGlobal, const Type *Ty,
600 Constant *Initializer) {
Reid Spencer309080a2006-09-28 19:28:24 +0000601 if (isa<FunctionType>(Ty)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000602 GenerateError("Cannot declare global vars of function type!");
Reid Spencer309080a2006-09-28 19:28:24 +0000603 return 0;
604 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000605
606 const PointerType *PTy = PointerType::get(Ty);
607
608 std::string Name;
609 if (NameStr) {
610 Name = NameStr; // Copy string
611 free(NameStr); // Free old string
612 }
613
614 // See if this global value was forward referenced. If so, recycle the
615 // object.
616 ValID ID;
617 if (!Name.empty()) {
618 ID = ValID::create((char*)Name.c_str());
619 } else {
620 ID = ValID::create((int)CurModule.Values[PTy].size());
621 }
622
623 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
624 // Move the global to the end of the list, from whereever it was
625 // previously inserted.
626 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
627 CurModule.CurrentModule->getGlobalList().remove(GV);
628 CurModule.CurrentModule->getGlobalList().push_back(GV);
629 GV->setInitializer(Initializer);
630 GV->setLinkage(Linkage);
631 GV->setConstant(isConstantGlobal);
632 InsertValue(GV, CurModule.Values);
633 return GV;
634 }
635
636 // If this global has a name, check to see if there is already a definition
637 // of this global in the module. If so, merge as appropriate. Note that
638 // this is really just a hack around problems in the CFE. :(
639 if (!Name.empty()) {
640 // We are a simple redefinition of a value, check to see if it is defined
641 // the same as the old one.
642 if (GlobalVariable *EGV =
643 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
644 // We are allowed to redefine a global variable in two circumstances:
645 // 1. If at least one of the globals is uninitialized or
646 // 2. If both initializers have the same value.
647 //
648 if (!EGV->hasInitializer() || !Initializer ||
649 EGV->getInitializer() == Initializer) {
650
651 // Make sure the existing global version gets the initializer! Make
652 // sure that it also gets marked const if the new version is.
653 if (Initializer && !EGV->hasInitializer())
654 EGV->setInitializer(Initializer);
655 if (isConstantGlobal)
656 EGV->setConstant(true);
657 EGV->setLinkage(Linkage);
658 return EGV;
659 }
660
Reid Spencer713eedc2006-08-18 08:43:06 +0000661 GenerateError("Redefinition of global variable named '" + Name +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000662 "' in the '" + Ty->getDescription() + "' type plane!");
Reid Spencer309080a2006-09-28 19:28:24 +0000663 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000664 }
665 }
666
667 // Otherwise there is no existing GV to use, create one now.
668 GlobalVariable *GV =
669 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
670 CurModule.CurrentModule);
671 InsertValue(GV, CurModule.Values);
672 return GV;
673}
674
675// setTypeName - Set the specified type to the name given. The name may be
676// null potentially, in which case this is a noop. The string passed in is
677// assumed to be a malloc'd string buffer, and is freed by this function.
678//
679// This function returns true if the type has already been defined, but is
680// allowed to be redefined in the specified context. If the name is a new name
681// for the type plane, it is inserted and false is returned.
682static bool setTypeName(const Type *T, char *NameStr) {
683 assert(!inFunctionScope() && "Can't give types function-local names!");
684 if (NameStr == 0) return false;
685
686 std::string Name(NameStr); // Copy string
687 free(NameStr); // Free old string
688
689 // We don't allow assigning names to void type
Reid Spencer309080a2006-09-28 19:28:24 +0000690 if (T == Type::VoidTy) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000691 GenerateError("Can't assign name '" + Name + "' to the void type!");
Reid Spencer309080a2006-09-28 19:28:24 +0000692 return false;
693 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000694
695 // Set the type name, checking for conflicts as we do so.
696 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
697
698 if (AlreadyExists) { // Inserting a name that is already defined???
699 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
700 assert(Existing && "Conflict but no matching type?");
701
702 // There is only one case where this is allowed: when we are refining an
703 // opaque type. In this case, Existing will be an opaque type.
704 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
705 // We ARE replacing an opaque type!
706 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
707 return true;
708 }
709
710 // Otherwise, this is an attempt to redefine a type. That's okay if
711 // the redefinition is identical to the original. This will be so if
712 // Existing and T point to the same Type object. In this one case we
713 // allow the equivalent redefinition.
714 if (Existing == T) return true; // Yes, it's equal.
715
716 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer713eedc2006-08-18 08:43:06 +0000717 GenerateError("Redefinition of type named '" + Name + "' in the '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000718 T->getDescription() + "' type plane!");
719 }
720
721 return false;
722}
723
724//===----------------------------------------------------------------------===//
725// Code for handling upreferences in type names...
726//
727
728// TypeContains - Returns true if Ty directly contains E in it.
729//
730static bool TypeContains(const Type *Ty, const Type *E) {
731 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
732 E) != Ty->subtype_end();
733}
734
735namespace {
736 struct UpRefRecord {
737 // NestingLevel - The number of nesting levels that need to be popped before
738 // this type is resolved.
739 unsigned NestingLevel;
740
741 // LastContainedTy - This is the type at the current binding level for the
742 // type. Every time we reduce the nesting level, this gets updated.
743 const Type *LastContainedTy;
744
745 // UpRefTy - This is the actual opaque type that the upreference is
746 // represented with.
747 OpaqueType *UpRefTy;
748
749 UpRefRecord(unsigned NL, OpaqueType *URTy)
750 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
751 };
752}
753
754// UpRefs - A list of the outstanding upreferences that need to be resolved.
755static std::vector<UpRefRecord> UpRefs;
756
757/// HandleUpRefs - Every time we finish a new layer of types, this function is
758/// called. It loops through the UpRefs vector, which is a list of the
759/// currently active types. For each type, if the up reference is contained in
760/// the newly completed type, we decrement the level count. When the level
761/// count reaches zero, the upreferenced type is the type that is passed in:
762/// thus we can complete the cycle.
763///
764static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner680aab62006-08-18 17:34:45 +0000765 // If Ty isn't abstract, or if there are no up-references in it, then there is
766 // nothing to resolve here.
767 if (!ty->isAbstract() || UpRefs.empty()) return ty;
768
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000769 PATypeHolder Ty(ty);
770 UR_OUT("Type '" << Ty->getDescription() <<
771 "' newly formed. Resolving upreferences.\n" <<
772 UpRefs.size() << " upreferences active!\n");
773
774 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
775 // to zero), we resolve them all together before we resolve them to Ty. At
776 // the end of the loop, if there is anything to resolve to Ty, it will be in
777 // this variable.
778 OpaqueType *TypeToResolve = 0;
779
780 for (unsigned i = 0; i != UpRefs.size(); ++i) {
781 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
782 << UpRefs[i].second->getDescription() << ") = "
783 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
784 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
785 // Decrement level of upreference
786 unsigned Level = --UpRefs[i].NestingLevel;
787 UpRefs[i].LastContainedTy = Ty;
788 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
789 if (Level == 0) { // Upreference should be resolved!
790 if (!TypeToResolve) {
791 TypeToResolve = UpRefs[i].UpRefTy;
792 } else {
793 UR_OUT(" * Resolving upreference for "
794 << UpRefs[i].second->getDescription() << "\n";
795 std::string OldName = UpRefs[i].UpRefTy->getDescription());
796 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
797 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
798 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
799 }
800 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
801 --i; // Do not skip the next element...
802 }
803 }
804 }
805
806 if (TypeToResolve) {
807 UR_OUT(" * Resolving upreference for "
808 << UpRefs[i].second->getDescription() << "\n";
809 std::string OldName = TypeToResolve->getDescription());
810 TypeToResolve->refineAbstractTypeTo(Ty);
811 }
812
813 return Ty;
814}
815
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000816/// This function is used to obtain the correct opcode for an instruction when
817/// an obsolete opcode is encountered. The OI parameter (OpcodeInfo) has both
818/// an opcode and an "obsolete" flag. These are generated by the lexer and
819/// the "obsolete" member will be true when the lexer encounters the token for
820/// an obsolete opcode. For example, "div" was replaced by [usf]div but we need
821/// to maintain backwards compatibility for asm files that still have the "div"
822/// instruction. This function handles converting div -> [usf]div appropriately.
Reid Spencerfdff9382006-11-08 06:47:33 +0000823/// @brief Convert obsolete BinaryOps opcodes to new values
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000824static void
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000825sanitizeOpcode(OpcodeInfo<Instruction::BinaryOps> &OI, const Type *Ty)
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000826{
827 // If its not obsolete, don't do anything
828 if (!OI.obsolete)
829 return;
830
831 // If its a packed type we want to use the element type
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000832 if (const PackedType *PTy = dyn_cast<PackedType>(Ty))
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000833 Ty = PTy->getElementType();
834
835 // Depending on the opcode ..
836 switch (OI.opcode) {
837 default:
Reid Spencerde46e482006-11-02 20:25:50 +0000838 GenerateError("Invalid obsolete opCode (check Lexer.l)");
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000839 break;
840 case Instruction::UDiv:
841 // Handle cases where the opcode needs to change
842 if (Ty->isFloatingPoint())
843 OI.opcode = Instruction::FDiv;
844 else if (Ty->isSigned())
845 OI.opcode = Instruction::SDiv;
846 break;
Reid Spencerde46e482006-11-02 20:25:50 +0000847 case Instruction::URem:
848 if (Ty->isFloatingPoint())
849 OI.opcode = Instruction::FRem;
850 else if (Ty->isSigned())
851 OI.opcode = Instruction::SRem;
852 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000853 }
854 // Its not obsolete any more, we fixed it.
855 OI.obsolete = false;
856}
Reid Spencerfdff9382006-11-08 06:47:33 +0000857
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000858/// This function is similar to the previous overload of sanitizeOpcode but
Reid Spencerfdff9382006-11-08 06:47:33 +0000859/// operates on Instruction::OtherOps instead of Instruction::BinaryOps.
860/// @brief Convert obsolete OtherOps opcodes to new values
861static void
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000862sanitizeOpcode(OpcodeInfo<Instruction::OtherOps> &OI, const Type *Ty)
Reid Spencerfdff9382006-11-08 06:47:33 +0000863{
864 // If its not obsolete, don't do anything
865 if (!OI.obsolete)
866 return;
867
Reid Spencerfdff9382006-11-08 06:47:33 +0000868 switch (OI.opcode) {
869 default:
870 GenerateError("Invalid obsolete opcode (check Lexer.l)");
871 break;
872 case Instruction::LShr:
873 if (Ty->isSigned())
874 OI.opcode = Instruction::AShr;
875 break;
876 }
877 // Its not obsolete any more, we fixed it.
878 OI.obsolete = false;
879}
880
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000881// common code from the two 'RunVMAsmParser' functions
Reid Spencer309080a2006-09-28 19:28:24 +0000882static Module* RunParser(Module * M) {
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000883
884 llvmAsmlineno = 1; // Reset the current line number...
885 ObsoleteVarArgs = false;
886 NewVarArgs = false;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000887 CurModule.CurrentModule = M;
Reid Spencerff359002006-10-09 17:36:59 +0000888
889 // Check to make sure the parser succeeded
890 if (yyparse()) {
891 if (ParserResult)
892 delete ParserResult;
893 return 0;
894 }
895
896 // Check to make sure that parsing produced a result
Reid Spencer713eedc2006-08-18 08:43:06 +0000897 if (!ParserResult)
898 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000899
Reid Spencerff359002006-10-09 17:36:59 +0000900 // Reset ParserResult variable while saving its value for the result.
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000901 Module *Result = ParserResult;
902 ParserResult = 0;
903
904 //Not all functions use vaarg, so make a second check for ObsoleteVarArgs
905 {
906 Function* F;
907 if ((F = Result->getNamedFunction("llvm.va_start"))
908 && F->getFunctionType()->getNumParams() == 0)
909 ObsoleteVarArgs = true;
910 if((F = Result->getNamedFunction("llvm.va_copy"))
911 && F->getFunctionType()->getNumParams() == 1)
912 ObsoleteVarArgs = true;
913 }
914
Reid Spencer309080a2006-09-28 19:28:24 +0000915 if (ObsoleteVarArgs && NewVarArgs) {
916 GenerateError(
917 "This file is corrupt: it uses both new and old style varargs");
918 return 0;
919 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000920
921 if(ObsoleteVarArgs) {
922 if(Function* F = Result->getNamedFunction("llvm.va_start")) {
Reid Spencer309080a2006-09-28 19:28:24 +0000923 if (F->arg_size() != 0) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000924 GenerateError("Obsolete va_start takes 0 argument!");
Reid Spencer309080a2006-09-28 19:28:24 +0000925 return 0;
926 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000927
928 //foo = va_start()
929 // ->
930 //bar = alloca typeof(foo)
931 //va_start(bar)
932 //foo = load bar
933
934 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
935 const Type* ArgTy = F->getFunctionType()->getReturnType();
936 const Type* ArgTyPtr = PointerType::get(ArgTy);
937 Function* NF = Result->getOrInsertFunction("llvm.va_start",
938 RetTy, ArgTyPtr, (Type *)0);
939
940 while (!F->use_empty()) {
941 CallInst* CI = cast<CallInst>(F->use_back());
942 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
943 new CallInst(NF, bar, "", CI);
944 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
945 CI->replaceAllUsesWith(foo);
946 CI->getParent()->getInstList().erase(CI);
947 }
948 Result->getFunctionList().erase(F);
949 }
950
951 if(Function* F = Result->getNamedFunction("llvm.va_end")) {
Reid Spencer309080a2006-09-28 19:28:24 +0000952 if(F->arg_size() != 1) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000953 GenerateError("Obsolete va_end takes 1 argument!");
Reid Spencer309080a2006-09-28 19:28:24 +0000954 return 0;
955 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000956
957 //vaend foo
958 // ->
959 //bar = alloca 1 of typeof(foo)
960 //vaend bar
961 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
962 const Type* ArgTy = F->getFunctionType()->getParamType(0);
963 const Type* ArgTyPtr = PointerType::get(ArgTy);
964 Function* NF = Result->getOrInsertFunction("llvm.va_end",
965 RetTy, ArgTyPtr, (Type *)0);
966
967 while (!F->use_empty()) {
968 CallInst* CI = cast<CallInst>(F->use_back());
969 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
970 new StoreInst(CI->getOperand(1), bar, CI);
971 new CallInst(NF, bar, "", CI);
972 CI->getParent()->getInstList().erase(CI);
973 }
974 Result->getFunctionList().erase(F);
975 }
976
977 if(Function* F = Result->getNamedFunction("llvm.va_copy")) {
Reid Spencer309080a2006-09-28 19:28:24 +0000978 if(F->arg_size() != 1) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000979 GenerateError("Obsolete va_copy takes 1 argument!");
Reid Spencer309080a2006-09-28 19:28:24 +0000980 return 0;
981 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000982 //foo = vacopy(bar)
983 // ->
984 //a = alloca 1 of typeof(foo)
985 //b = alloca 1 of typeof(foo)
986 //store bar -> b
987 //vacopy(a, b)
988 //foo = load a
989
990 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
991 const Type* ArgTy = F->getFunctionType()->getReturnType();
992 const Type* ArgTyPtr = PointerType::get(ArgTy);
993 Function* NF = Result->getOrInsertFunction("llvm.va_copy",
994 RetTy, ArgTyPtr, ArgTyPtr,
995 (Type *)0);
996
997 while (!F->use_empty()) {
998 CallInst* CI = cast<CallInst>(F->use_back());
999 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
1000 AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
1001 new StoreInst(CI->getOperand(1), b, CI);
1002 new CallInst(NF, a, b, "", CI);
1003 Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
1004 CI->replaceAllUsesWith(foo);
1005 CI->getParent()->getInstList().erase(CI);
1006 }
1007 Result->getFunctionList().erase(F);
1008 }
1009 }
1010
1011 return Result;
Reid Spencer309080a2006-09-28 19:28:24 +00001012}
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001013
1014//===----------------------------------------------------------------------===//
1015// RunVMAsmParser - Define an interface to this parser
1016//===----------------------------------------------------------------------===//
1017//
1018Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
1019 set_scan_file(F);
1020
1021 CurFilename = Filename;
1022 return RunParser(new Module(CurFilename));
1023}
1024
1025Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
1026 set_scan_string(AsmString);
1027
1028 CurFilename = "from_memory";
1029 if (M == NULL) {
1030 return RunParser(new Module (CurFilename));
1031 } else {
1032 return RunParser(M);
1033 }
1034}
1035
1036%}
1037
1038%union {
1039 llvm::Module *ModuleVal;
1040 llvm::Function *FunctionVal;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001041 std::pair<TypeInfo, char*> *ArgVal;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001042 llvm::BasicBlock *BasicBlockVal;
1043 llvm::TerminatorInst *TermInstVal;
1044 llvm::Instruction *InstVal;
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001045 ConstInfo ConstVal;
1046 TypeInfo TypeVal;
1047 ValueInfo ValueVal;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001048
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001049 std::vector<std::pair<TypeInfo,char*> >*ArgList;
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001050 std::vector<ValueInfo> *ValueList;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001051 std::list<TypeInfo> *TypeList;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001052 // Represent the RHS of PHI node
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001053 std::list<std::pair<llvm::Value*, llvm::BasicBlock*> > *PHIList;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001054 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001055 std::vector<ConstInfo> *ConstVector;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001056
1057 llvm::GlobalValue::LinkageTypes Linkage;
1058 int64_t SInt64Val;
1059 uint64_t UInt64Val;
1060 int SIntVal;
1061 unsigned UIntVal;
1062 double FPVal;
1063 bool BoolVal;
1064
1065 char *StrVal; // This memory is strdup'd!
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001066 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001067
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001068 BinaryOpInfo BinaryOpVal;
1069 TermOpInfo TermOpVal;
1070 MemOpInfo MemOpVal;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001071 CastOpInfo CastOpVal;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001072 OtherOpInfo OtherOpVal;
1073 llvm::Module::Endianness Endianness;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001074}
1075
1076%type <ModuleVal> Module FunctionList
1077%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
1078%type <BasicBlockVal> BasicBlock InstructionList
1079%type <TermInstVal> BBTerminatorInst
1080%type <InstVal> Inst InstVal MemoryInst
1081%type <ConstVal> ConstVal ConstExpr
1082%type <ConstVector> ConstVector
1083%type <ArgList> ArgList ArgListH
1084%type <ArgVal> ArgVal
1085%type <PHIList> PHIList
1086%type <ValueList> ValueRefList ValueRefListE // For call param lists
1087%type <ValueList> IndexList // For GEP derived indices
1088%type <TypeList> TypeListI ArgTypeListI
1089%type <JumpTable> JumpTable
1090%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
1091%type <BoolVal> OptVolatile // 'volatile' or not
1092%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1093%type <BoolVal> OptSideEffect // 'sideeffect' or not.
1094%type <Linkage> OptLinkage
1095%type <Endianness> BigOrLittle
1096
1097// ValueRef - Unresolved reference to a definition or BB
1098%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1099%type <ValueVal> ResolvedVal // <type> <valref> pair
1100// Tokens and types for handling constant integer values
1101//
1102// ESINT64VAL - A negative number within long long range
1103%token <SInt64Val> ESINT64VAL
1104
1105// EUINT64VAL - A positive number within uns. long long range
1106%token <UInt64Val> EUINT64VAL
1107%type <SInt64Val> EINT64VAL
1108
1109%token <SIntVal> SINTVAL // Signed 32 bit ints...
1110%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
1111%type <SIntVal> INTVAL
1112%token <FPVal> FPVAL // Float or Double constant
1113
1114// Built in types...
1115%type <TypeVal> Types TypesV UpRTypes UpRTypesV
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001116%type <TypeVal> SIntType UIntType IntType FPType PrimType // Classifications
1117%token <TypeVal> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
1118%token <TypeVal> FLOAT DOUBLE TYPE LABEL
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001119
1120%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
1121%type <StrVal> Name OptName OptAssign
1122%type <UIntVal> OptAlign OptCAlign
1123%type <StrVal> OptSection SectionString
1124
1125%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
1126%token DECLARE GLOBAL CONSTANT SECTION VOLATILE
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001127%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
1128%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001129%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
1130%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Chris Lattner09c0e992006-05-19 21:28:53 +00001131%token CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +00001132%token X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattner7d1d0342006-10-22 06:08:13 +00001133%token DATALAYOUT
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001134%type <UIntVal> OptCallingConv
1135
1136// Basic Block Terminating Operators
1137%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
1138
1139// Binary Operators
1140%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Reid Spencerde46e482006-11-02 20:25:50 +00001141%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001142%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001143
1144// Memory Instructions
1145%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1146
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001147// Cast Operators
1148%type <CastOpVal> CastOps
1149%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1150%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1151
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001152// Other Operators
1153%type <OtherOpVal> ShiftOps
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001154%token <OtherOpVal> PHI_TOK SELECT SHL LSHR ASHR VAARG
Chris Lattner9ff96a72006-04-08 01:18:56 +00001155%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001156%token VAARG_old VANEXT_old //OBSOLETE
1157
1158
1159%start Module
1160%%
1161
1162// Handle constant integer size restriction and conversion...
1163//
1164INTVAL : SINTVAL;
1165INTVAL : UINTVAL {
1166 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
Reid Spencer713eedc2006-08-18 08:43:06 +00001167 GEN_ERROR("Value too large for type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001168 $$ = (int32_t)$1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001169 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001170};
1171
1172
1173EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
1174EINT64VAL : EUINT64VAL {
1175 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
Reid Spencer713eedc2006-08-18 08:43:06 +00001176 GEN_ERROR("Value too large for type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001177 $$ = (int64_t)$1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001178 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001179};
1180
1181// Operations that are notably excluded from this list include:
1182// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1183//
Reid Spencerde46e482006-11-02 20:25:50 +00001184ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001185LogicalOps : AND | OR | XOR;
1186SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001187CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1188 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
1189ShiftOps : SHL | LSHR | ASHR;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001190
1191// These are some types that allow classification if we only want a particular
1192// thing... for example, only a signed, unsigned, or integral type.
1193SIntType : LONG | INT | SHORT | SBYTE;
1194UIntType : ULONG | UINT | USHORT | UBYTE;
1195IntType : SIntType | UIntType;
1196FPType : FLOAT | DOUBLE;
1197
1198// OptAssign - Value producing statements have an optional assignment component
1199OptAssign : Name '=' {
1200 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001201 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001202 }
1203 | /*empty*/ {
1204 $$ = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00001205 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001206 };
1207
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001208OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
1209 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
1210 WEAK { $$ = GlobalValue::WeakLinkage; } |
1211 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
1212 DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; } |
1213 DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; } |
1214 EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; } |
1215 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001216
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +00001217OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1218 CCC_TOK { $$ = CallingConv::C; } |
1219 CSRETCC_TOK { $$ = CallingConv::CSRet; } |
1220 FASTCC_TOK { $$ = CallingConv::Fast; } |
1221 COLDCC_TOK { $$ = CallingConv::Cold; } |
1222 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1223 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1224 CC_TOK EUINT64VAL {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001225 if ((unsigned)$2 != $2)
Reid Spencer713eedc2006-08-18 08:43:06 +00001226 GEN_ERROR("Calling conv too large!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001227 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001228 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001229 };
1230
1231// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1232// a comma before it.
1233OptAlign : /*empty*/ { $$ = 0; } |
1234 ALIGN EUINT64VAL {
1235 $$ = $2;
1236 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencer713eedc2006-08-18 08:43:06 +00001237 GEN_ERROR("Alignment must be a power of two!");
1238 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001239};
1240OptCAlign : /*empty*/ { $$ = 0; } |
1241 ',' ALIGN EUINT64VAL {
1242 $$ = $3;
1243 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencer713eedc2006-08-18 08:43:06 +00001244 GEN_ERROR("Alignment must be a power of two!");
1245 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001246};
1247
1248
1249SectionString : SECTION STRINGCONSTANT {
1250 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1251 if ($2[i] == '"' || $2[i] == '\\')
Reid Spencer713eedc2006-08-18 08:43:06 +00001252 GEN_ERROR("Invalid character in section name!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001253 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001254 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001255};
1256
1257OptSection : /*empty*/ { $$ = 0; } |
1258 SectionString { $$ = $1; };
1259
1260// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1261// is set to be the global we are processing.
1262//
1263GlobalVarAttributes : /* empty */ {} |
1264 ',' GlobalVarAttribute GlobalVarAttributes {};
1265GlobalVarAttribute : SectionString {
1266 CurGV->setSection($1);
1267 free($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001268 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001269 }
1270 | ALIGN EUINT64VAL {
1271 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencer713eedc2006-08-18 08:43:06 +00001272 GEN_ERROR("Alignment must be a power of two!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001273 CurGV->setAlignment($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001274 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001275 };
1276
1277//===----------------------------------------------------------------------===//
1278// Types includes all predefined types... except void, because it can only be
1279// used in specific contexts (function returning void for example). To have
1280// access to it, a user must explicitly use TypesV.
1281//
1282
1283// TypesV includes all of 'Types', but it also includes the void type.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001284TypesV : Types | VOID {
1285 $$.type = new PATypeHolder($1.type->get());
1286 $$.signedness = $1.signedness;
1287};
1288UpRTypesV : UpRTypes | VOID {
1289 $$.type = new PATypeHolder($1.type->get());
1290 $$.signedness = $1.signedness;
1291};
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001292
1293Types : UpRTypes {
1294 if (!UpRefs.empty())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001295 GEN_ERROR("Invalid upreference in type: " +
1296 ($1.type->get())->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001297 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001298 CHECK_FOR_ERROR
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001299};
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001300
1301
1302// Derived types are added later...
1303//
1304PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1305PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
1306UpRTypes : OPAQUE {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001307 $$.type = new PATypeHolder(OpaqueType::get());
1308 $$.signedness = isSignless;
Reid Spencer713eedc2006-08-18 08:43:06 +00001309 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001310 }
1311 | PrimType {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001312 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001313 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001314 };
1315UpRTypes : SymbolicValueRef { // Named types are also simple types...
Reid Spencer309080a2006-09-28 19:28:24 +00001316 const Type* tmp = getTypeVal($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001317 CHECK_FOR_ERROR
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001318 $$.type = new PATypeHolder(tmp);
1319 $$.signedness = isSignless;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001320};
1321
1322// Include derived types in the Types production.
1323//
1324UpRTypes : '\\' EUINT64VAL { // Type UpReference
Reid Spencer713eedc2006-08-18 08:43:06 +00001325 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001326 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1327 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001328 $$.type = new PATypeHolder(OT);
1329 $$.signedness = isSignless;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001330 UR_OUT("New Upreference!\n");
Reid Spencer713eedc2006-08-18 08:43:06 +00001331 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001332 }
1333 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
1334 std::vector<const Type*> Params;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001335 for (std::list<TypeInfo>::iterator I = $3->begin(),
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001336 E = $3->end(); I != E; ++I)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001337 Params.push_back(I->type->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001338 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1339 if (isVarArg) Params.pop_back();
1340
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001341 $$.type = new PATypeHolder(HandleUpRefs(
1342 FunctionType::get($1.type->get(),Params,isVarArg)));
1343 $$.signedness = isSignless;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001344 delete $3; // Delete the argument list
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001345 delete $1.type;
Reid Spencer713eedc2006-08-18 08:43:06 +00001346 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001347 }
1348 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001349 $$.type = new PATypeHolder(HandleUpRefs(
1350 ArrayType::get($4.type->get(), (unsigned)$2)));
1351 $$.signedness = isSignless;
1352 delete $4.type;
Reid Spencer713eedc2006-08-18 08:43:06 +00001353 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001354 }
1355 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001356 const llvm::Type* ElemTy = $4.type->get();
1357 if ((unsigned)$2 != $2)
1358 GEN_ERROR("Unsigned result not equal to signed result");
1359 if (!ElemTy->isPrimitiveType())
1360 GEN_ERROR("Elemental type of a PackedType must be primitive");
1361 if (!isPowerOf2_32($2))
1362 GEN_ERROR("Vector length should be a power of 2!");
1363 $$.type = new PATypeHolder(HandleUpRefs(
1364 PackedType::get($4.type->get(), (unsigned)$2)));
1365 $$.signedness = isSignless;
1366 delete $4.type;
1367 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001368 }
1369 | '{' TypeListI '}' { // Structure type?
1370 std::vector<const Type*> Elements;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001371 for (std::list<TypeInfo>::iterator I = $2->begin(),
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001372 E = $2->end(); I != E; ++I)
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001373 Elements.push_back((*I).type->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001374
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001375 $$.type = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
1376 $$.signedness = isSignless;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001377 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001378 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001379 }
1380 | '{' '}' { // Empty structure type?
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001381 $$.type = new PATypeHolder(StructType::get(std::vector<const Type*>()));
1382 $$.signedness = isSignless;
Reid Spencer713eedc2006-08-18 08:43:06 +00001383 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001384 }
1385 | UpRTypes '*' { // Pointer type?
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001386 if ($1.type->get() == Type::LabelTy)
Chris Lattnerff20ba32006-10-15 23:27:25 +00001387 GEN_ERROR("Cannot form a pointer to a basic block");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001388 $$.type = new PATypeHolder(HandleUpRefs(PointerType::get($1.type->get())));
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001389 $$.signedness = isSignless;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001390 delete $1.type;
Reid Spencer713eedc2006-08-18 08:43:06 +00001391 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001392 };
1393
1394// TypeList - Used for struct declarations and as a basis for function type
1395// declaration type lists
1396//
1397TypeListI : UpRTypes {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001398 $$ = new std::list<TypeInfo>();
1399 $$->push_back($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001400 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001401 }
1402 | TypeListI ',' UpRTypes {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001403 ($$=$1)->push_back($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001404 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001405 };
1406
1407// ArgTypeList - List of types for a function type declaration...
1408ArgTypeListI : TypeListI
1409 | TypeListI ',' DOTDOTDOT {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001410 TypeInfo TI;
1411 TI.type = new PATypeHolder(Type::VoidTy); TI.signedness = isSignless;
1412 ($$=$1)->push_back(TI);
Reid Spencer713eedc2006-08-18 08:43:06 +00001413 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001414 }
1415 | DOTDOTDOT {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001416 TypeInfo TI;
1417 TI.type = new PATypeHolder(Type::VoidTy); TI.signedness = isSignless;
1418 ($$ = new std::list<TypeInfo>())->push_back(TI);
Reid Spencer713eedc2006-08-18 08:43:06 +00001419 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001420 }
1421 | /*empty*/ {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001422 $$ = new std::list<TypeInfo>();
Reid Spencer713eedc2006-08-18 08:43:06 +00001423 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001424 };
1425
1426// ConstVal - The various declarations that go into the constant pool. This
1427// production is used ONLY to represent constants that show up AFTER a 'const',
1428// 'constant' or 'global' token at global scope. Constants that can be inlined
1429// into other expressions (such as integers and constexprs) are handled by the
1430// ResolvedVal, ValueRef and ConstValueRef productions.
1431//
1432ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001433 const ArrayType *ATy = dyn_cast<ArrayType>($1.type->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001434 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001435 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001436 ($1.type->get())->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001437 const Type *ETy = ATy->getElementType();
1438 int NumElements = ATy->getNumElements();
1439
1440 // Verify that we have the correct size...
1441 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer713eedc2006-08-18 08:43:06 +00001442 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001443 utostr($3->size()) + " arguments, but has size of " +
1444 itostr(NumElements) + "!");
1445
1446 // Verify all elements are correct type!
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001447 std::vector<Constant*> elems;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001448 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001449 if (ETy != (*$3)[i].cnst->getType()) {
Reid Spencer713eedc2006-08-18 08:43:06 +00001450 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001451 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001452 (*$3)[i].cnst->getType()->getDescription() + "'.");
1453 } else {
1454 elems.push_back((*$3)[i].cnst);
1455 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001456 }
1457
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001458 $$.cnst = ConstantArray::get(ATy, elems);
1459 $$.signedness = isSignless;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001460 delete $1.type; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001461 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001462 }
1463 | Types '[' ']' {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001464 const ArrayType *ATy = dyn_cast<ArrayType>($1.type->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001465 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001466 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001467 ($1.type->get())->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001468
1469 int NumElements = ATy->getNumElements();
1470 if (NumElements != -1 && NumElements != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001471 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001472 " arguments, but has size of " + itostr(NumElements) +"!");
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001473 $$.cnst = ConstantArray::get(ATy, std::vector<llvm::Constant*>());
1474 $$.signedness = isSignless;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001475 delete $1.type;
Reid Spencer713eedc2006-08-18 08:43:06 +00001476 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001477 }
1478 | Types 'c' STRINGCONSTANT {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001479 const ArrayType *ATy = dyn_cast<ArrayType>($1.type->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001480 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001481 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001482 ($1.type->get())->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001483
1484 int NumElements = ATy->getNumElements();
1485 const Type *ETy = ATy->getElementType();
1486 char *EndStr = UnEscapeLexed($3, true);
1487 if (NumElements != -1 && NumElements != (EndStr-$3))
Reid Spencer713eedc2006-08-18 08:43:06 +00001488 GEN_ERROR("Can't build string constant of size " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001489 itostr((int)(EndStr-$3)) +
1490 " when array has size " + itostr(NumElements) + "!");
1491 std::vector<Constant*> Vals;
1492 if (ETy == Type::SByteTy) {
1493 for (signed char *C = (signed char *)$3; C != (signed char *)EndStr; ++C)
Reid Spencere0fc4df2006-10-20 07:07:24 +00001494 Vals.push_back(ConstantInt::get(ETy, *C));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001495 } else if (ETy == Type::UByteTy) {
1496 for (unsigned char *C = (unsigned char *)$3;
1497 C != (unsigned char*)EndStr; ++C)
Reid Spencere0fc4df2006-10-20 07:07:24 +00001498 Vals.push_back(ConstantInt::get(ETy, *C));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001499 } else {
1500 free($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001501 GEN_ERROR("Cannot build string arrays of non byte sized elements!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001502 }
1503 free($3);
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001504 $$.cnst = ConstantArray::get(ATy, Vals);
1505 $$.signedness = isSignless;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001506 delete $1.type;
Reid Spencer713eedc2006-08-18 08:43:06 +00001507 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001508 }
1509 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001510 const PackedType *PTy = dyn_cast<PackedType>($1.type->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001511 if (PTy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001512 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001513 $1.type->get()->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001514 const Type *ETy = PTy->getElementType();
1515 int NumElements = PTy->getNumElements();
1516
1517 // Verify that we have the correct size...
1518 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer713eedc2006-08-18 08:43:06 +00001519 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001520 utostr($3->size()) + " arguments, but has size of " +
1521 itostr(NumElements) + "!");
1522
1523 // Verify all elements are correct type!
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001524 std::vector<Constant*> elems;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001525 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001526 if (ETy != (*$3)[i].cnst->getType()) {
Reid Spencer713eedc2006-08-18 08:43:06 +00001527 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001528 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001529 (*$3)[i].cnst->getType()->getDescription() + "'.");
1530 } else
1531 elems.push_back((*$3)[i].cnst);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001532 }
1533
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001534 $$.cnst = ConstantPacked::get(PTy, elems);
1535 $$.signedness = isSignless;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001536 delete $1.type; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001537 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001538 }
1539 | Types '{' ConstVector '}' {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001540 const StructType *STy = dyn_cast<StructType>($1.type->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001541 if (STy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001542 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001543 $1.type->get()->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001544
1545 if ($3->size() != STy->getNumContainedTypes())
Reid Spencer713eedc2006-08-18 08:43:06 +00001546 GEN_ERROR("Illegal number of initializers for structure type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001547
1548 // Check to ensure that constants are compatible with the type initializer!
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001549 std::vector<Constant*> elems;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001550 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001551 if ((*$3)[i].cnst->getType() != STy->getElementType(i)) {
Reid Spencer713eedc2006-08-18 08:43:06 +00001552 GEN_ERROR("Expected type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001553 STy->getElementType(i)->getDescription() +
1554 "' for element #" + utostr(i) +
1555 " of structure initializer!");
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001556 } else
1557 elems.push_back((*$3)[i].cnst);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001558
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001559 $$.cnst = ConstantStruct::get(STy, elems);
1560 $$.signedness = isSignless;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001561 delete $1.type; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001562 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001563 }
1564 | Types '{' '}' {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001565 const StructType *STy = dyn_cast<StructType>($1.type->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001566 if (STy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001567 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001568 $1.type->get()->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001569
1570 if (STy->getNumContainedTypes() != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001571 GEN_ERROR("Illegal number of initializers for structure type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001572
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001573 $$.cnst = ConstantStruct::get(STy, std::vector<Constant*>());
1574 $$.signedness = isSignless;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001575 delete $1.type;
Reid Spencer713eedc2006-08-18 08:43:06 +00001576 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001577 }
1578 | Types NULL_TOK {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001579 const PointerType *PTy = dyn_cast<PointerType>($1.type->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001580 if (PTy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001581 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001582 $1.type->get()->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001583
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001584 $$.cnst = ConstantPointerNull::get(PTy);
1585 $$.signedness = isSignless;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001586 delete $1.type;
Reid Spencer713eedc2006-08-18 08:43:06 +00001587 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001588 }
1589 | Types UNDEF {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001590 $$.cnst = UndefValue::get($1.type->get());
1591 $$.signedness = isSignless;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001592 delete $1.type;
Reid Spencer713eedc2006-08-18 08:43:06 +00001593 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001594 }
1595 | Types SymbolicValueRef {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001596 const PointerType *Ty = dyn_cast<PointerType>($1.type->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001597 if (Ty == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001598 GEN_ERROR("Global const reference must be a pointer type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001599
1600 // ConstExprs can exist in the body of a function, thus creating
1601 // GlobalValues whenever they refer to a variable. Because we are in
1602 // the context of a function, getValNonImprovising will search the functions
1603 // symbol table instead of the module symbol table for the global symbol,
1604 // which throws things all off. To get around this, we just tell
1605 // getValNonImprovising that we are at global scope here.
1606 //
1607 Function *SavedCurFn = CurFun.CurrentFunction;
1608 CurFun.CurrentFunction = 0;
1609
1610 Value *V = getValNonImprovising(Ty, $2);
Reid Spencer309080a2006-09-28 19:28:24 +00001611 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001612
1613 CurFun.CurrentFunction = SavedCurFn;
1614
1615 // If this is an initializer for a constant pointer, which is referencing a
1616 // (currently) undefined variable, create a stub now that shall be replaced
1617 // in the future with the right type of variable.
1618 //
1619 if (V == 0) {
1620 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1621 const PointerType *PT = cast<PointerType>(Ty);
1622
1623 // First check to see if the forward references value is already created!
1624 PerModuleInfo::GlobalRefsType::iterator I =
1625 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1626
1627 if (I != CurModule.GlobalRefs.end()) {
1628 V = I->second; // Placeholder already exists, use it...
1629 $2.destroy();
1630 } else {
1631 std::string Name;
1632 if ($2.Type == ValID::NameVal) Name = $2.Name;
1633
1634 // Create the forward referenced global.
1635 GlobalValue *GV;
1636 if (const FunctionType *FTy =
1637 dyn_cast<FunctionType>(PT->getElementType())) {
1638 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1639 CurModule.CurrentModule);
1640 } else {
1641 GV = new GlobalVariable(PT->getElementType(), false,
1642 GlobalValue::ExternalLinkage, 0,
1643 Name, CurModule.CurrentModule);
1644 }
1645
1646 // Keep track of the fact that we have a forward ref to recycle it
1647 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1648 V = GV;
1649 }
1650 }
1651
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001652 $$.cnst = cast<GlobalValue>(V);
1653 $$.signedness = $1.signedness;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001654 delete $1.type; // Free the type handle
Reid Spencer713eedc2006-08-18 08:43:06 +00001655 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001656 }
1657 | Types ConstExpr {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001658 if ($1.type->get() != $2.cnst->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001659 GEN_ERROR("Mismatched types for constant expression!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001660 $$ = $2;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001661 delete $1.type;
Reid Spencer713eedc2006-08-18 08:43:06 +00001662 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001663 }
1664 | Types ZEROINITIALIZER {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001665 const Type *Ty = $1.type->get();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001666 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencer713eedc2006-08-18 08:43:06 +00001667 GEN_ERROR("Cannot create a null initialized value of this type!");
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001668 $$.cnst = Constant::getNullValue(Ty);
1669 $$.signedness = isSignless;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001670 delete $1.type;
Reid Spencer713eedc2006-08-18 08:43:06 +00001671 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001672 };
1673
1674ConstVal : SIntType EINT64VAL { // integral constants
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001675 if (!ConstantInt::isValueValidForType($1.type->get(), $2))
Reid Spencer713eedc2006-08-18 08:43:06 +00001676 GEN_ERROR("Constant value doesn't fit in type!");
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001677 $$.cnst = ConstantInt::get($1.type->get(), $2);
1678 $$.signedness = $1.signedness;
Reid Spencer713eedc2006-08-18 08:43:06 +00001679 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001680 }
1681 | UIntType EUINT64VAL { // integral constants
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001682 if (!ConstantInt::isValueValidForType($1.type->get(), $2))
Reid Spencer713eedc2006-08-18 08:43:06 +00001683 GEN_ERROR("Constant value doesn't fit in type!");
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001684 $$.cnst = ConstantInt::get($1.type->get(), $2);
1685 $$.signedness = $1.signedness;
Reid Spencer713eedc2006-08-18 08:43:06 +00001686 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001687 }
1688 | BOOL TRUETOK { // Boolean constants
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001689 $$.cnst = ConstantBool::getTrue();
1690 $$.signedness = isSignless;
Reid Spencer713eedc2006-08-18 08:43:06 +00001691 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001692 }
1693 | BOOL FALSETOK { // Boolean constants
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001694 $$.cnst = ConstantBool::getFalse();
1695 $$.signedness = isSignless;
Reid Spencer713eedc2006-08-18 08:43:06 +00001696 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001697 }
1698 | FPType FPVAL { // Float & Double constants
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001699 if (!ConstantFP::isValueValidForType($1.type->get(), $2))
Reid Spencer713eedc2006-08-18 08:43:06 +00001700 GEN_ERROR("Floating point constant invalid for type!!");
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001701 $$.cnst = ConstantFP::get($1.type->get(), $2);
1702 $$.signedness = isSignless;
Reid Spencer713eedc2006-08-18 08:43:06 +00001703 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001704 };
1705
1706
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001707ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001708 Constant *Val = $3.cnst;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001709 const Type *Ty = $5.type->get();
1710 if (!Val->getType()->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001711 GEN_ERROR("cast constant expression from a non-primitive type: '" +
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001712 Val->getType()->getDescription() + "'!");
1713 if (!Ty->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001714 GEN_ERROR("cast constant expression to a non-primitive type: '" +
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001715 Ty->getDescription() + "'!");
1716 if ($1.obsolete) {
1717 if (Ty == Type::BoolTy) {
1718 // The previous definition of cast to bool was a compare against zero.
1719 // We have to retain that semantic so we do it here.
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001720 $$.cnst = ConstantExpr::get(Instruction::SetNE, Val,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001721 Constant::getNullValue(Val->getType()));
1722 } else if (Val->getType()->isFloatingPoint() && isa<PointerType>(Ty)) {
1723 Constant *CE = ConstantExpr::getFPToUI(Val, Type::ULongTy);
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001724 $$.cnst = ConstantExpr::getIntToPtr(CE, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001725 } else {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001726 $$.cnst = ConstantExpr::getCast(Val, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001727 }
1728 } else {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001729 $$.cnst = ConstantExpr::getCast($1.opcode, $3.cnst, $5.type->get());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001730 }
1731 delete $5.type;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001732 }
1733 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001734 if (!isa<PointerType>($3.cnst->getType()))
Reid Spencer713eedc2006-08-18 08:43:06 +00001735 GEN_ERROR("GetElementPtr requires a pointer operand!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001736
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001737 std::vector<llvm::Value*> IdxVec;
1738 for (unsigned i = 0, e = $4->size(); i < e; ++i)
1739 if (Constant *C = dyn_cast<Constant>((*$4)[i].val))
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001740 IdxVec.push_back(C);
1741 else
Reid Spencer713eedc2006-08-18 08:43:06 +00001742 GEN_ERROR("Indices to constant getelementptr must be constants!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001743
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001744 const Type *IdxTy =
1745 GetElementPtrInst::getIndexedType($3.cnst->getType(), IdxVec, true);
1746 if (!IdxTy)
1747 GEN_ERROR("Index list invalid for constant getelementptr!");
1748
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001749 delete $4;
1750
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001751 $$.cnst = ConstantExpr::getGetElementPtr($3.cnst, IdxVec);
1752 $$.signedness = isSignless;
Reid Spencer713eedc2006-08-18 08:43:06 +00001753 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001754 }
1755 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001756 if ($3.cnst->getType() != Type::BoolTy)
Reid Spencer713eedc2006-08-18 08:43:06 +00001757 GEN_ERROR("Select condition must be of boolean type!");
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001758 if ($5.cnst->getType() != $7.cnst->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001759 GEN_ERROR("Select operand types must match!");
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001760 $$.cnst = ConstantExpr::getSelect($3.cnst, $5.cnst, $7.cnst);
1761 $$.signedness = isSignless;
Reid Spencer713eedc2006-08-18 08:43:06 +00001762 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001763 }
1764 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001765 if ($3.cnst->getType() != $5.cnst->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001766 GEN_ERROR("Binary operator types must match!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001767 // First, make sure we're dealing with the right opcode by upgrading from
1768 // obsolete versions.
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001769 sanitizeOpcode($1, $3.cnst->getType());
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001770 CHECK_FOR_ERROR;
1771
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001772 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
1773 // To retain backward compatibility with these early compilers, we emit a
1774 // cast to the appropriate integer type automatically if we are in the
1775 // broken case. See PR424 for more information.
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001776 if (!isa<PointerType>($3.cnst->getType())) {
1777 $$.cnst = ConstantExpr::get($1.opcode, $3.cnst, $5.cnst);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001778 } else {
1779 const Type *IntPtrTy = 0;
1780 switch (CurModule.CurrentModule->getPointerSize()) {
1781 case Module::Pointer32: IntPtrTy = Type::IntTy; break;
1782 case Module::Pointer64: IntPtrTy = Type::LongTy; break;
Reid Spencer713eedc2006-08-18 08:43:06 +00001783 default: GEN_ERROR("invalid pointer binary constant expr!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001784 }
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001785 $$.cnst = ConstantExpr::get($1.opcode,
1786 ConstantExpr::getCast($3.cnst, IntPtrTy),
1787 ConstantExpr::getCast($5.cnst, IntPtrTy));
1788 $$.cnst = ConstantExpr::getCast($$.cnst, $3.cnst->getType());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001789 }
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001790 $$.signedness = $3.signedness;
Reid Spencer713eedc2006-08-18 08:43:06 +00001791 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001792 }
1793 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001794 if ($3.cnst->getType() != $5.cnst->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001795 GEN_ERROR("Logical operator types must match!");
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001796 if (!$3.cnst->getType()->isIntegral()) {
1797 if (!isa<PackedType>($3.cnst->getType()) ||
1798 !cast<PackedType>($3.cnst->getType())->getElementType()->isIntegral())
Reid Spencer713eedc2006-08-18 08:43:06 +00001799 GEN_ERROR("Logical operator requires integral operands!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001800 }
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001801 $$.cnst = ConstantExpr::get($1.opcode, $3.cnst, $5.cnst);
1802 $$.signedness = $3.signedness;
Reid Spencer713eedc2006-08-18 08:43:06 +00001803 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001804 }
1805 | SetCondOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001806 if ($3.cnst->getType() != $5.cnst->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001807 GEN_ERROR("setcc operand types must match!");
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001808 $$.cnst = ConstantExpr::get($1.opcode, $3.cnst, $5.cnst);
1809 $$.signedness = isSignless;
Reid Spencer713eedc2006-08-18 08:43:06 +00001810 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001811 }
1812 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001813 if ($5.cnst->getType() != Type::UByteTy)
Reid Spencer713eedc2006-08-18 08:43:06 +00001814 GEN_ERROR("Shift count for shift constant must be unsigned byte!");
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001815 if (!$3.cnst->getType()->isInteger())
Reid Spencer713eedc2006-08-18 08:43:06 +00001816 GEN_ERROR("Shift constant expression requires integer operand!");
Reid Spencerfdff9382006-11-08 06:47:33 +00001817 // Handle opcode upgrade situations
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001818 sanitizeOpcode($1, $3.cnst->getType());
Reid Spencerfdff9382006-11-08 06:47:33 +00001819 CHECK_FOR_ERROR;
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001820 $$.cnst = ConstantExpr::get($1.opcode, $3.cnst, $5.cnst);
1821 $$.signedness = $3.signedness;
Reid Spencer713eedc2006-08-18 08:43:06 +00001822 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001823 }
1824 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001825 if (!ExtractElementInst::isValidOperands($3.cnst, $5.cnst))
Reid Spencer713eedc2006-08-18 08:43:06 +00001826 GEN_ERROR("Invalid extractelement operands!");
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001827 $$.cnst = ConstantExpr::getExtractElement($3.cnst, $5.cnst);
1828 $$.signedness = $3.signedness;
Reid Spencer713eedc2006-08-18 08:43:06 +00001829 CHECK_FOR_ERROR
Chris Lattneraebccf82006-04-08 03:55:17 +00001830 }
1831 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001832 if (!InsertElementInst::isValidOperands($3.cnst, $5.cnst, $7.cnst))
Reid Spencer713eedc2006-08-18 08:43:06 +00001833 GEN_ERROR("Invalid insertelement operands!");
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001834 $$.cnst = ConstantExpr::getInsertElement($3.cnst, $5.cnst, $7.cnst);
1835 $$.signedness = isSignless;
Reid Spencer713eedc2006-08-18 08:43:06 +00001836 CHECK_FOR_ERROR
Chris Lattneraebccf82006-04-08 03:55:17 +00001837 }
1838 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001839 if (!ShuffleVectorInst::isValidOperands($3.cnst, $5.cnst, $7.cnst))
Reid Spencer713eedc2006-08-18 08:43:06 +00001840 GEN_ERROR("Invalid shufflevector operands!");
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001841 $$.cnst = ConstantExpr::getShuffleVector($3.cnst, $5.cnst, $7.cnst);
1842 $$.signedness = isSignless;
Reid Spencer713eedc2006-08-18 08:43:06 +00001843 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001844 };
1845
Chris Lattneraebccf82006-04-08 03:55:17 +00001846
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001847// ConstVector - A list of comma separated constants.
1848ConstVector : ConstVector ',' ConstVal {
1849 ($$ = $1)->push_back($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001850 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001851 }
1852 | ConstVal {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001853 $$ = new std::vector<ConstInfo>();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001854 $$->push_back($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001855 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001856 };
1857
1858
1859// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1860GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
1861
1862
1863//===----------------------------------------------------------------------===//
1864// Rules to match Modules
1865//===----------------------------------------------------------------------===//
1866
1867// Module rule: Capture the result of parsing the whole file into a result
1868// variable...
1869//
1870Module : FunctionList {
1871 $$ = ParserResult = $1;
1872 CurModule.ModuleDone();
Reid Spencerff359002006-10-09 17:36:59 +00001873 CHECK_FOR_ERROR;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001874};
1875
1876// FunctionList - A list of functions, preceeded by a constant pool.
1877//
1878FunctionList : FunctionList Function {
1879 $$ = $1;
1880 CurFun.FunctionDone();
Reid Spencer713eedc2006-08-18 08:43:06 +00001881 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001882 }
1883 | FunctionList FunctionProto {
1884 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001885 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001886 }
1887 | FunctionList MODULE ASM_TOK AsmBlock {
1888 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001889 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001890 }
1891 | FunctionList IMPLEMENTATION {
1892 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001893 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001894 }
1895 | ConstPool {
1896 $$ = CurModule.CurrentModule;
1897 // Emit an error if there are any unresolved types left.
1898 if (!CurModule.LateResolveTypes.empty()) {
1899 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
Reid Spencer713eedc2006-08-18 08:43:06 +00001900 if (DID.Type == ValID::NameVal) {
1901 GEN_ERROR("Reference to an undefined type: '"+DID.getName() + "'");
1902 } else {
1903 GEN_ERROR("Reference to an undefined type: #" + itostr(DID.Num));
1904 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001905 }
Reid Spencer713eedc2006-08-18 08:43:06 +00001906 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001907 };
1908
1909// ConstPool - Constants with optional names assigned to them.
1910ConstPool : ConstPool OptAssign TYPE TypesV {
1911 // Eagerly resolve types. This is not an optimization, this is a
1912 // requirement that is due to the fact that we could have this:
1913 //
1914 // %list = type { %list * }
1915 // %list = type { %list * } ; repeated type decl
1916 //
1917 // If types are not resolved eagerly, then the two types will not be
1918 // determined to be the same type!
1919 //
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001920 ResolveTypeTo($2, $4.type->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001921
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001922 if (!setTypeName($4.type->get(), $2) && !$2) {
Reid Spencer309080a2006-09-28 19:28:24 +00001923 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001924 // If this is a named type that is not a redefinition, add it to the slot
1925 // table.
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001926 CurModule.Types.push_back(*($4.type));
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001927 } else {
1928 delete $4.type;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001929 }
Reid Spencer713eedc2006-08-18 08:43:06 +00001930 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001931 }
1932 | ConstPool FunctionProto { // Function prototypes can be in const pool
Reid Spencer713eedc2006-08-18 08:43:06 +00001933 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001934 }
1935 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
Reid Spencer713eedc2006-08-18 08:43:06 +00001936 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001937 }
1938 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001939 if ($5.cnst == 0)
Reid Spencer309080a2006-09-28 19:28:24 +00001940 GEN_ERROR("Global value initializer is not a constant!");
Reid Spencer55f1fbe2006-11-28 07:29:44 +00001941 CurGV = ParseGlobalVariable($2, $3, $4, $5.cnst->getType(), $5.cnst);
Reid Spencer713eedc2006-08-18 08:43:06 +00001942 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00001943 } GlobalVarAttributes {
1944 CurGV = 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001945 }
1946 | ConstPool OptAssign EXTERNAL GlobalType Types {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001947 CurGV = ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4,
1948 $5.type->get(), 0);
Reid Spencer309080a2006-09-28 19:28:24 +00001949 CHECK_FOR_ERROR
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001950 delete $5.type;
Reid Spencer309080a2006-09-28 19:28:24 +00001951 } GlobalVarAttributes {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001952 CurGV = 0;
1953 CHECK_FOR_ERROR
1954 }
1955 | ConstPool OptAssign DLLIMPORT GlobalType Types {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001956 CurGV = ParseGlobalVariable($2, GlobalValue::DLLImportLinkage, $4,
1957 $5.type->get(), 0);
Reid Spencer309080a2006-09-28 19:28:24 +00001958 CHECK_FOR_ERROR
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001959 delete $5.type;
Reid Spencer309080a2006-09-28 19:28:24 +00001960 } GlobalVarAttributes {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001961 CurGV = 0;
1962 CHECK_FOR_ERROR
1963 }
1964 | ConstPool OptAssign EXTERN_WEAK GlobalType Types {
Reid Spencer309080a2006-09-28 19:28:24 +00001965 CurGV =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001966 ParseGlobalVariable($2, GlobalValue::ExternalWeakLinkage, $4,
1967 $5.type->get(), 0);
Reid Spencer309080a2006-09-28 19:28:24 +00001968 CHECK_FOR_ERROR
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001969 delete $5.type;
Reid Spencer309080a2006-09-28 19:28:24 +00001970 } GlobalVarAttributes {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001971 CurGV = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00001972 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001973 }
1974 | ConstPool TARGET TargetDefinition {
Reid Spencer713eedc2006-08-18 08:43:06 +00001975 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001976 }
1977 | ConstPool DEPLIBS '=' LibrariesDefinition {
Reid Spencer713eedc2006-08-18 08:43:06 +00001978 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001979 }
1980 | /* empty: end of list */ {
1981 };
1982
1983
1984AsmBlock : STRINGCONSTANT {
1985 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
1986 char *EndStr = UnEscapeLexed($1, true);
1987 std::string NewAsm($1, EndStr);
1988 free($1);
1989
1990 if (AsmSoFar.empty())
1991 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
1992 else
1993 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
Reid Spencer713eedc2006-08-18 08:43:06 +00001994 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001995};
1996
1997BigOrLittle : BIG { $$ = Module::BigEndian; };
1998BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1999
2000TargetDefinition : ENDIAN '=' BigOrLittle {
2001 CurModule.CurrentModule->setEndianness($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002002 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002003 }
2004 | POINTERSIZE '=' EUINT64VAL {
2005 if ($3 == 32)
2006 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
2007 else if ($3 == 64)
2008 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
2009 else
Reid Spencer713eedc2006-08-18 08:43:06 +00002010 GEN_ERROR("Invalid pointer size: '" + utostr($3) + "'!");
2011 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002012 }
2013 | TRIPLE '=' STRINGCONSTANT {
2014 CurModule.CurrentModule->setTargetTriple($3);
2015 free($3);
John Criswell6af0b122006-10-24 19:09:48 +00002016 }
Chris Lattner7d1d0342006-10-22 06:08:13 +00002017 | DATALAYOUT '=' STRINGCONSTANT {
Owen Anderson85690f32006-10-18 02:21:48 +00002018 CurModule.CurrentModule->setDataLayout($3);
2019 free($3);
Owen Anderson85690f32006-10-18 02:21:48 +00002020 };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002021
2022LibrariesDefinition : '[' LibList ']';
2023
2024LibList : LibList ',' STRINGCONSTANT {
2025 CurModule.CurrentModule->addLibrary($3);
2026 free($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002027 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002028 }
2029 | STRINGCONSTANT {
2030 CurModule.CurrentModule->addLibrary($1);
2031 free($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002032 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002033 }
2034 | /* empty: end of list */ {
Reid Spencer713eedc2006-08-18 08:43:06 +00002035 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002036 }
2037 ;
2038
2039//===----------------------------------------------------------------------===//
2040// Rules to match Function Headers
2041//===----------------------------------------------------------------------===//
2042
2043Name : VAR_ID | STRINGCONSTANT;
2044OptName : Name | /*empty*/ { $$ = 0; };
2045
2046ArgVal : Types OptName {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002047 if ($1.type->get() == Type::VoidTy)
Reid Spencer713eedc2006-08-18 08:43:06 +00002048 GEN_ERROR("void typed arguments are invalid!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002049 $$ = new std::pair<TypeInfo, char*>($1, $2);
Reid Spencer713eedc2006-08-18 08:43:06 +00002050 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002051};
2052
2053ArgListH : ArgListH ',' ArgVal {
2054 $$ = $1;
2055 $1->push_back(*$3);
2056 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00002057 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002058 }
2059 | ArgVal {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002060 $$ = new std::vector<std::pair<TypeInfo,char*> >();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002061 $$->push_back(*$1);
2062 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002063 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002064 };
2065
2066ArgList : ArgListH {
2067 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002068 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002069 }
2070 | ArgListH ',' DOTDOTDOT {
2071 $$ = $1;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002072 TypeInfo TI;
2073 TI.type = new PATypeHolder(Type::VoidTy);
2074 TI.signedness = isSignless;
2075 $$->push_back(std::pair<TypeInfo,char*>(TI,(char*)0));
Reid Spencer713eedc2006-08-18 08:43:06 +00002076 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002077 }
2078 | DOTDOTDOT {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002079 $$ = new std::vector<std::pair<TypeInfo,char*> >();
2080 TypeInfo TI;
2081 TI.type = new PATypeHolder(Type::VoidTy);
2082 TI.signedness = isSignless;
2083 $$->push_back(std::make_pair(TI, (char*)0));
Reid Spencer713eedc2006-08-18 08:43:06 +00002084 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002085 }
2086 | /* empty */ {
2087 $$ = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00002088 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002089 };
2090
2091FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
2092 OptSection OptAlign {
2093 UnEscapeLexed($3);
2094 std::string FunctionName($3);
2095 free($3); // Free strdup'd memory!
2096
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002097 if (!($2.type->get())->isFirstClassType() && $2.type->get() != Type::VoidTy)
Reid Spencer713eedc2006-08-18 08:43:06 +00002098 GEN_ERROR("LLVM functions cannot return aggregate types!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002099
2100 std::vector<const Type*> ParamTypeList;
2101 if ($5) { // If there are arguments...
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002102 for (std::vector<std::pair<TypeInfo,char*> >::iterator I = $5->begin();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002103 I != $5->end(); ++I)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002104 ParamTypeList.push_back(I->first.type->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002105 }
2106
2107 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2108 if (isVarArg) ParamTypeList.pop_back();
2109
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002110 const FunctionType *FT = FunctionType::get($2.type->get(), ParamTypeList,
2111 isVarArg);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002112 const PointerType *PFT = PointerType::get(FT);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002113 delete $2.type;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002114
2115 ValID ID;
2116 if (!FunctionName.empty()) {
2117 ID = ValID::create((char*)FunctionName.c_str());
2118 } else {
2119 ID = ValID::create((int)CurModule.Values[PFT].size());
2120 }
2121
2122 Function *Fn = 0;
2123 // See if this function was forward referenced. If so, recycle the object.
2124 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2125 // Move the function to the end of the list, from whereever it was
2126 // previously inserted.
2127 Fn = cast<Function>(FWRef);
2128 CurModule.CurrentModule->getFunctionList().remove(Fn);
2129 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2130 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
2131 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
2132 // If this is the case, either we need to be a forward decl, or it needs
2133 // to be.
2134 if (!CurFun.isDeclare && !Fn->isExternal())
Reid Spencer713eedc2006-08-18 08:43:06 +00002135 GEN_ERROR("Redefinition of function '" + FunctionName + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002136
2137 // Make sure to strip off any argument names so we can't get conflicts.
2138 if (Fn->isExternal())
2139 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2140 AI != AE; ++AI)
2141 AI->setName("");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002142 } else { // Not already defined?
2143 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
2144 CurModule.CurrentModule);
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002145
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002146 InsertValue(Fn, CurModule.Values);
2147 }
2148
2149 CurFun.FunctionStart(Fn);
Anton Korobeynikov0ab01ff2006-09-17 13:06:18 +00002150
2151 if (CurFun.isDeclare) {
2152 // If we have declaration, always overwrite linkage. This will allow us to
2153 // correctly handle cases, when pointer to function is passed as argument to
2154 // another function.
2155 Fn->setLinkage(CurFun.Linkage);
2156 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002157 Fn->setCallingConv($1);
2158 Fn->setAlignment($8);
2159 if ($7) {
2160 Fn->setSection($7);
2161 free($7);
2162 }
2163
2164 // Add all of the arguments we parsed to the function...
2165 if ($5) { // Is null if empty...
2166 if (isVarArg) { // Nuke the last entry
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002167 assert($5->back().first.type->get() == Type::VoidTy &&
2168 $5->back().second == 0 && "Not a varargs marker!");
2169 delete $5->back().first.type;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002170 $5->pop_back(); // Delete the last entry
2171 }
2172 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002173 for (std::vector<std::pair<TypeInfo,char*> >::iterator I = $5->begin();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002174 I != $5->end(); ++I, ++ArgIt) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002175 delete I->first.type; // Delete the typeholder...
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002176 setValueName(ArgIt, I->second); // Insert arg into symtab...
Reid Spencer309080a2006-09-28 19:28:24 +00002177 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002178 InsertValue(ArgIt);
2179 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002180 delete $5; // We're now done with the argument list
2181 }
Reid Spencer713eedc2006-08-18 08:43:06 +00002182 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002183};
2184
2185BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2186
2187FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
2188 $$ = CurFun.CurrentFunction;
2189
2190 // Make sure that we keep track of the linkage type even if there was a
2191 // previous "declare".
2192 $$->setLinkage($1);
2193};
2194
2195END : ENDTOK | '}'; // Allow end of '}' to end a function
2196
2197Function : BasicBlockList END {
2198 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002199 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002200};
2201
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002202FnDeclareLinkage: /*default*/ |
Chris Lattner6aee6f22006-11-08 05:58:47 +00002203 DLLIMPORT { CurFun.Linkage = GlobalValue::DLLImportLinkage; } |
Reid Spencerd5e19442006-12-01 00:33:46 +00002204 EXTERN_WEAK { CurFun.Linkage = GlobalValue::ExternalWeakLinkage; };
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002205
2206FunctionProto : DECLARE { CurFun.isDeclare = true; } FnDeclareLinkage FunctionHeaderH {
2207 $$ = CurFun.CurrentFunction;
2208 CurFun.FunctionDone();
2209 CHECK_FOR_ERROR
2210 };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002211
2212//===----------------------------------------------------------------------===//
2213// Rules to match Basic Blocks
2214//===----------------------------------------------------------------------===//
2215
2216OptSideEffect : /* empty */ {
2217 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002218 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002219 }
2220 | SIDEEFFECT {
2221 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002222 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002223 };
2224
2225ConstValueRef : ESINT64VAL { // A reference to a direct constant
2226 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002227 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002228 }
2229 | EUINT64VAL {
2230 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002231 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002232 }
2233 | FPVAL { // Perhaps it's an FP constant?
2234 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002235 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002236 }
2237 | TRUETOK {
Chris Lattner6ab03f62006-09-28 23:35:22 +00002238 $$ = ValID::create(ConstantBool::getTrue());
Reid Spencer713eedc2006-08-18 08:43:06 +00002239 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002240 }
2241 | FALSETOK {
Chris Lattner6ab03f62006-09-28 23:35:22 +00002242 $$ = ValID::create(ConstantBool::getFalse());
Reid Spencer713eedc2006-08-18 08:43:06 +00002243 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002244 }
2245 | NULL_TOK {
2246 $$ = ValID::createNull();
Reid Spencer713eedc2006-08-18 08:43:06 +00002247 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002248 }
2249 | UNDEF {
2250 $$ = ValID::createUndef();
Reid Spencer713eedc2006-08-18 08:43:06 +00002251 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002252 }
2253 | ZEROINITIALIZER { // A vector zero constant.
2254 $$ = ValID::createZeroInit();
Reid Spencer713eedc2006-08-18 08:43:06 +00002255 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002256 }
2257 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002258 const Type *ETy = (*$2)[0].cnst->getType();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002259 int NumElements = $2->size();
2260
2261 PackedType* pt = PackedType::get(ETy, NumElements);
2262 PATypeHolder* PTy = new PATypeHolder(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002263 HandleUpRefs(PackedType::get( ETy, NumElements)));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002264
2265 // Verify all elements are correct type!
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002266 std::vector<Constant*> elems;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002267 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002268 if (ETy != (*$2)[i].cnst->getType()) {
Reid Spencer713eedc2006-08-18 08:43:06 +00002269 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002270 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002271 (*$2)[i].cnst->getType()->getDescription() + "'.");
2272 } else
2273 elems.push_back((*$2)[i].cnst);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002274 }
2275
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002276 $$ = ValID::create(ConstantPacked::get(pt, elems));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002277 delete PTy; delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002278 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002279 }
2280 | ConstExpr {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002281 $$ = ValID::create($1.cnst);
Reid Spencer713eedc2006-08-18 08:43:06 +00002282 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002283 }
2284 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
2285 char *End = UnEscapeLexed($3, true);
2286 std::string AsmStr = std::string($3, End);
2287 End = UnEscapeLexed($5, true);
2288 std::string Constraints = std::string($5, End);
2289 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
2290 free($3);
2291 free($5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002292 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002293 };
2294
2295// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2296// another value.
2297//
2298SymbolicValueRef : INTVAL { // Is it an integer reference...?
2299 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002300 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002301 }
2302 | Name { // Is it a named reference...?
2303 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002304 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002305 };
2306
2307// ValueRef - A reference to a definition... either constant or symbolic
2308ValueRef : SymbolicValueRef | ConstValueRef;
2309
2310
2311// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2312// type immediately preceeds the value reference, and allows complex constant
2313// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2314ResolvedVal : Types ValueRef {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002315 $$.val = getVal($1.type->get(), $2);
2316 delete $1.type;
Reid Spencer713eedc2006-08-18 08:43:06 +00002317 CHECK_FOR_ERROR
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002318 $$.signedness = $1.signedness;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002319 };
2320
2321BasicBlockList : BasicBlockList BasicBlock {
2322 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002323 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002324 }
2325 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
2326 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002327 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002328 };
2329
2330
2331// Basic blocks are terminated by branching instructions:
2332// br, br/cc, switch, ret
2333//
2334BasicBlock : InstructionList OptAssign BBTerminatorInst {
2335 setValueName($3, $2);
Reid Spencer309080a2006-09-28 19:28:24 +00002336 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002337 InsertValue($3);
2338
2339 $1->getInstList().push_back($3);
2340 InsertValue($1);
2341 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002342 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002343 };
2344
2345InstructionList : InstructionList Inst {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002346 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2347 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2348 if (CI2->getParent() == 0)
2349 $1->getInstList().push_back(CI2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002350 $1->getInstList().push_back($2);
2351 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002352 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002353 }
2354 | /* empty */ {
2355 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Reid Spencer309080a2006-09-28 19:28:24 +00002356 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002357
2358 // Make sure to move the basic block to the correct location in the
2359 // function, instead of leaving it inserted wherever it was first
2360 // referenced.
2361 Function::BasicBlockListType &BBL =
2362 CurFun.CurrentFunction->getBasicBlockList();
2363 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer713eedc2006-08-18 08:43:06 +00002364 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002365 }
2366 | LABELSTR {
2367 $$ = CurBB = getBBVal(ValID::create($1), true);
Reid Spencer309080a2006-09-28 19:28:24 +00002368 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002369
2370 // Make sure to move the basic block to the correct location in the
2371 // function, instead of leaving it inserted wherever it was first
2372 // referenced.
2373 Function::BasicBlockListType &BBL =
2374 CurFun.CurrentFunction->getBasicBlockList();
2375 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer713eedc2006-08-18 08:43:06 +00002376 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002377 };
2378
2379BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002380 $$ = new ReturnInst($2.val);
Reid Spencer713eedc2006-08-18 08:43:06 +00002381 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002382 }
2383 | RET VOID { // Return with no result...
2384 $$ = new ReturnInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002385 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002386 }
2387 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer309080a2006-09-28 19:28:24 +00002388 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002389 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002390 $$ = new BranchInst(tmpBB);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002391 } // Conditional Branch...
2392 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencer309080a2006-09-28 19:28:24 +00002393 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002394 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002395 BasicBlock* tmpBBB = getBBVal($9);
2396 CHECK_FOR_ERROR
2397 Value* tmpVal = getVal(Type::BoolTy, $3);
2398 CHECK_FOR_ERROR
2399 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002400 }
2401 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002402 Value* tmpVal = getVal($2.type->get(), $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002403 CHECK_FOR_ERROR
2404 BasicBlock* tmpBB = getBBVal($6);
2405 CHECK_FOR_ERROR
2406 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002407 $$ = S;
2408
2409 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2410 E = $8->end();
2411 for (; I != E; ++I) {
2412 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2413 S->addCase(CI, I->second);
2414 else
Reid Spencer713eedc2006-08-18 08:43:06 +00002415 GEN_ERROR("Switch case is constant, but not a simple integer!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002416 }
2417 delete $8;
Reid Spencer713eedc2006-08-18 08:43:06 +00002418 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002419 }
2420 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002421 Value* tmpVal = getVal($2.type->get(), $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002422 CHECK_FOR_ERROR
2423 BasicBlock* tmpBB = getBBVal($6);
2424 CHECK_FOR_ERROR
2425 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002426 $$ = S;
Reid Spencer713eedc2006-08-18 08:43:06 +00002427 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002428 }
2429 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
2430 TO LABEL ValueRef UNWIND LABEL ValueRef {
2431 const PointerType *PFTy;
2432 const FunctionType *Ty;
2433
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002434 if (!(PFTy = dyn_cast<PointerType>($3.type->get())) ||
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002435 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2436 // Pull out the types of all of the arguments...
2437 std::vector<const Type*> ParamTypes;
2438 if ($6) {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002439 for (std::vector<ValueInfo>::iterator I = $6->begin(), E = $6->end();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002440 I != E; ++I)
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002441 ParamTypes.push_back((*I).val->getType());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002442 }
2443
2444 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2445 if (isVarArg) ParamTypes.pop_back();
2446
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002447 Ty = FunctionType::get($3.type->get(), ParamTypes, isVarArg);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002448 PFTy = PointerType::get(Ty);
2449 }
2450
2451 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer309080a2006-09-28 19:28:24 +00002452 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002453 BasicBlock *Normal = getBBVal($10);
Reid Spencer309080a2006-09-28 19:28:24 +00002454 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002455 BasicBlock *Except = getBBVal($13);
Reid Spencer309080a2006-09-28 19:28:24 +00002456 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002457
2458 // Create the call node...
2459 if (!$6) { // Has no arguments?
2460 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
2461 } else { // Has arguments?
2462 // Loop through FunctionType's arguments and ensure they are specified
2463 // correctly!
2464 //
2465 FunctionType::param_iterator I = Ty->param_begin();
2466 FunctionType::param_iterator E = Ty->param_end();
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002467 std::vector<ValueInfo>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002468
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002469 std::vector<Value*> args;
2470 for (; ArgI != ArgE; ++ArgI)
2471 if (I == E) {
2472 if (Ty->isVarArg()) {
2473 args.push_back((*ArgI).val);
2474 } else {
2475 GEN_ERROR("Too many parameters for function of type " +
2476 Ty->getDescription());
2477 }
2478 } else {
2479 if ((*ArgI).val->getType() != *I) {
2480 GEN_ERROR("Parameter " + (*ArgI).val->getName() +
2481 " is not of type '" + (*I)->getDescription() + "'!");
2482 } else
2483 args.push_back((*ArgI).val);
2484 ++I;
2485 }
2486 $$ = new InvokeInst(V, Normal, Except, args);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002487 }
2488 cast<InvokeInst>($$)->setCallingConv($2);
2489
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002490 delete $3.type;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002491 delete $6;
Reid Spencer713eedc2006-08-18 08:43:06 +00002492 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002493 }
2494 | UNWIND {
2495 $$ = new UnwindInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002496 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002497 }
2498 | UNREACHABLE {
2499 $$ = new UnreachableInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002500 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002501 };
2502
2503
2504
2505JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2506 $$ = $1;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002507 Constant *V = cast<Constant>(getValNonImprovising($2.type->get(), $3));
Reid Spencer309080a2006-09-28 19:28:24 +00002508 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002509 if (V == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002510 GEN_ERROR("May only switch on a constant pool value!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002511
Reid Spencer309080a2006-09-28 19:28:24 +00002512 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002513 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002514 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002515 }
2516 | IntType ConstValueRef ',' LABEL ValueRef {
2517 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002518 Constant *V = cast<Constant>(getValNonImprovising($1.type->get(), $2));
Reid Spencer309080a2006-09-28 19:28:24 +00002519 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002520
2521 if (V == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002522 GEN_ERROR("May only switch on a constant pool value!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002523
Reid Spencer309080a2006-09-28 19:28:24 +00002524 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002525 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002526 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002527 };
2528
2529Inst : OptAssign InstVal {
2530 // Is this definition named?? if so, assign the name...
2531 setValueName($2, $1);
Reid Spencer309080a2006-09-28 19:28:24 +00002532 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002533 InsertValue($2);
2534 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002535 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002536};
2537
2538PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
2539 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002540 Value* tmpVal = getVal($1.type->get(), $3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002541 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002542 BasicBlock* tmpBB = getBBVal($5);
2543 CHECK_FOR_ERROR
2544 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002545 delete $1.type;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002546 }
2547 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2548 $$ = $1;
Reid Spencer309080a2006-09-28 19:28:24 +00002549 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002550 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002551 BasicBlock* tmpBB = getBBVal($6);
2552 CHECK_FOR_ERROR
2553 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002554 };
2555
2556
2557ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002558 $$ = new std::vector<ValueInfo>();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002559 $$->push_back($1);
2560 }
2561 | ValueRefList ',' ResolvedVal {
2562 $$ = $1;
2563 $1->push_back($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002564 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002565 };
2566
2567// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002568ValueRefListE : ValueRefList {
2569 $$ = $1;
2570 }
2571 | /*empty*/ {
2572 $$ = 0;
2573 };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002574
2575OptTailCall : TAIL CALL {
2576 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002577 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002578 }
2579 | CALL {
2580 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002581 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002582 };
2583
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002584InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002585 if (!$2.type->get()->isInteger() && !$2.type->get()->isFloatingPoint() &&
2586 !isa<PackedType>($2.type->get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002587 GEN_ERROR(
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002588 "Arithmetic operator requires integer, FP, or packed operands!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002589 if (isa<PackedType>($2.type->get()) &&
Reid Spencerde46e482006-11-02 20:25:50 +00002590 ($1.opcode == Instruction::URem ||
2591 $1.opcode == Instruction::SRem ||
2592 $1.opcode == Instruction::FRem))
2593 GEN_ERROR("U/S/FRem not supported on packed types!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002594 // Upgrade the opcode from obsolete versions before we do anything with it.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002595 sanitizeOpcode($1,$2.type->get());
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002596 CHECK_FOR_ERROR;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002597 Value* val1 = getVal($2.type->get(), $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002598 CHECK_FOR_ERROR
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002599 Value* val2 = getVal($2.type->get(), $5);
Reid Spencer309080a2006-09-28 19:28:24 +00002600 CHECK_FOR_ERROR
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002601 $$ = BinaryOperator::create($1.opcode, val1, val2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002602 if ($$ == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002603 GEN_ERROR("binary operator returned null!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002604 delete $2.type;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002605 }
2606 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002607 if (!$2.type->get()->isIntegral()) {
2608 if (!isa<PackedType>($2.type->get()) ||
2609 !cast<PackedType>($2.type->get())->getElementType()->isIntegral())
Reid Spencer713eedc2006-08-18 08:43:06 +00002610 GEN_ERROR("Logical operator requires integral operands!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002611 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002612 Value* tmpVal1 = getVal($2.type->get(), $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002613 CHECK_FOR_ERROR
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002614 Value* tmpVal2 = getVal($2.type->get(), $5);
Reid Spencer309080a2006-09-28 19:28:24 +00002615 CHECK_FOR_ERROR
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002616 $$ = BinaryOperator::create($1.opcode, tmpVal1, tmpVal2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002617 if ($$ == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002618 GEN_ERROR("binary operator returned null!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002619 delete $2.type;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002620 }
2621 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002622 if(isa<PackedType>($2.type->get())) {
Reid Spencer713eedc2006-08-18 08:43:06 +00002623 GEN_ERROR(
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002624 "PackedTypes currently not supported in setcc instructions!");
2625 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002626 Value* tmpVal1 = getVal($2.type->get(), $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002627 CHECK_FOR_ERROR
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002628 Value* tmpVal2 = getVal($2.type->get(), $5);
Reid Spencer309080a2006-09-28 19:28:24 +00002629 CHECK_FOR_ERROR
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002630 $$ = new SetCondInst($1.opcode, tmpVal1, tmpVal2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002631 if ($$ == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002632 GEN_ERROR("binary operator returned null!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002633 delete $2.type;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002634 }
2635 | NOT ResolvedVal {
Reid Spencerd5e19442006-12-01 00:33:46 +00002636 llvm_cerr << "WARNING: Use of eliminated 'not' instruction:"
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002637 << " Replacing with 'xor'.\n";
2638
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002639 Value *Ones = ConstantIntegral::getAllOnesValue($2.val->getType());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002640 if (Ones == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002641 GEN_ERROR("Expected integral type for not instruction!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002642
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002643 $$ = BinaryOperator::create(Instruction::Xor, $2.val, Ones);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002644 if ($$ == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002645 GEN_ERROR("Could not create a xor instruction!");
2646 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002647 }
2648 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002649 if ($4.val->getType() != Type::UByteTy)
Reid Spencer713eedc2006-08-18 08:43:06 +00002650 GEN_ERROR("Shift amount must be ubyte!");
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002651 if (!$2.val->getType()->isInteger())
Reid Spencer713eedc2006-08-18 08:43:06 +00002652 GEN_ERROR("Shift constant expression requires integer operand!");
Reid Spencerfdff9382006-11-08 06:47:33 +00002653 // Handle opcode upgrade situations
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002654 sanitizeOpcode($1, $2.val->getType());
Reid Spencerfdff9382006-11-08 06:47:33 +00002655 CHECK_FOR_ERROR;
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002656 $$ = new ShiftInst($1.opcode, $2.val, $4.val);
Reid Spencer713eedc2006-08-18 08:43:06 +00002657 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002658 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002659 | CastOps ResolvedVal TO Types {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002660 Value* Val = $2.val;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002661 const Type* Ty = $4.type->get();
2662 if (!Val->getType()->isFirstClassType())
2663 GEN_ERROR("cast from a non-primitive type: '" +
2664 Val->getType()->getDescription() + "'!");
2665 if (!Ty->isFirstClassType())
2666 GEN_ERROR("cast to a non-primitive type: '" + Ty->getDescription() +"'!");
2667
2668 if ($1.obsolete) {
2669 if (Ty == Type::BoolTy) {
2670 // The previous definition of cast to bool was a compare against zero.
2671 // We have to retain that semantic so we do it here.
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002672 $$ = new SetCondInst(Instruction::SetNE, $2.val,
2673 Constant::getNullValue($2.val->getType()));
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002674 } else if (Val->getType()->isFloatingPoint() && isa<PointerType>(Ty)) {
2675 CastInst *CI = new FPToUIInst(Val, Type::ULongTy);
2676 $$ = new IntToPtrInst(CI, Ty);
2677 } else {
2678 $$ = CastInst::createInferredCast(Val, Ty);
2679 }
2680 } else {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002681 $$ = CastInst::create($1.opcode, $2.val, $4.type->get());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002682 }
2683 delete $4.type;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002684 }
2685 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002686 if ($2.val->getType() != Type::BoolTy)
Reid Spencer713eedc2006-08-18 08:43:06 +00002687 GEN_ERROR("select condition must be boolean!");
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002688 if ($4.val->getType() != $6.val->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002689 GEN_ERROR("select value types should match!");
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002690 $$ = new SelectInst($2.val, $4.val, $6.val);
Reid Spencer713eedc2006-08-18 08:43:06 +00002691 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002692 }
2693 | VAARG ResolvedVal ',' Types {
2694 NewVarArgs = true;
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002695 $$ = new VAArgInst($2.val, $4.type->get());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002696 delete $4.type;
Reid Spencer713eedc2006-08-18 08:43:06 +00002697 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002698 }
2699 | VAARG_old ResolvedVal ',' Types {
2700 ObsoleteVarArgs = true;
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002701 const Type* ArgTy = $2.val->getType();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002702 Function* NF = CurModule.CurrentModule->
2703 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
2704
2705 //b = vaarg a, t ->
2706 //foo = alloca 1 of t
2707 //bar = vacopy a
2708 //store bar -> foo
2709 //b = vaarg foo, t
2710 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix");
2711 CurBB->getInstList().push_back(foo);
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002712 CallInst* bar = new CallInst(NF, $2.val);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002713 CurBB->getInstList().push_back(bar);
2714 CurBB->getInstList().push_back(new StoreInst(bar, foo));
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002715 $$ = new VAArgInst(foo, $4.type->get());
2716 delete $4.type;
Reid Spencer713eedc2006-08-18 08:43:06 +00002717 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002718 }
2719 | VANEXT_old ResolvedVal ',' Types {
2720 ObsoleteVarArgs = true;
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002721 const Type* ArgTy = $2.val->getType();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002722 Function* NF = CurModule.CurrentModule->
2723 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
2724
2725 //b = vanext a, t ->
2726 //foo = alloca 1 of t
2727 //bar = vacopy a
2728 //store bar -> foo
2729 //tmp = vaarg foo, t
2730 //b = load foo
2731 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix");
2732 CurBB->getInstList().push_back(foo);
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002733 CallInst* bar = new CallInst(NF, $2.val);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002734 CurBB->getInstList().push_back(bar);
2735 CurBB->getInstList().push_back(new StoreInst(bar, foo));
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002736 Instruction* tmp = new VAArgInst(foo, $4.type->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002737 CurBB->getInstList().push_back(tmp);
2738 $$ = new LoadInst(foo);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002739 delete $4.type;
Reid Spencer713eedc2006-08-18 08:43:06 +00002740 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002741 }
2742 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002743 if (!ExtractElementInst::isValidOperands($2.val, $4.val))
Reid Spencer713eedc2006-08-18 08:43:06 +00002744 GEN_ERROR("Invalid extractelement operands!");
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002745 $$ = new ExtractElementInst($2.val, $4.val);
Reid Spencer713eedc2006-08-18 08:43:06 +00002746 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002747 }
2748 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002749 if (!InsertElementInst::isValidOperands($2.val, $4.val, $6.val))
Reid Spencer713eedc2006-08-18 08:43:06 +00002750 GEN_ERROR("Invalid insertelement operands!");
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002751 $$ = new InsertElementInst($2.val, $4.val, $6.val);
Reid Spencer713eedc2006-08-18 08:43:06 +00002752 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002753 }
Chris Lattner9ff96a72006-04-08 01:18:56 +00002754 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002755 if (!ShuffleVectorInst::isValidOperands($2.val, $4.val, $6.val))
Reid Spencer713eedc2006-08-18 08:43:06 +00002756 GEN_ERROR("Invalid shufflevector operands!");
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002757 $$ = new ShuffleVectorInst($2.val, $4.val, $6.val);
Reid Spencer713eedc2006-08-18 08:43:06 +00002758 CHECK_FOR_ERROR
Chris Lattner9ff96a72006-04-08 01:18:56 +00002759 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002760 | PHI_TOK PHIList {
2761 const Type *Ty = $2->front().first->getType();
2762 if (!Ty->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002763 GEN_ERROR("PHI node operands must be of first class type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002764 $$ = new PHINode(Ty);
2765 ((PHINode*)$$)->reserveOperandSpace($2->size());
2766 while ($2->begin() != $2->end()) {
2767 if ($2->front().first->getType() != Ty)
Reid Spencer713eedc2006-08-18 08:43:06 +00002768 GEN_ERROR("All elements of a PHI node must be of the same type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002769 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
2770 $2->pop_front();
2771 }
2772 delete $2; // Free the list...
Reid Spencer713eedc2006-08-18 08:43:06 +00002773 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002774 }
2775 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002776 const PointerType *PFTy = 0;
2777 const FunctionType *Ty = 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002778
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002779 if (!(PFTy = dyn_cast<PointerType>($3.type->get())) ||
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002780 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2781 // Pull out the types of all of the arguments...
2782 std::vector<const Type*> ParamTypes;
2783 if ($6) {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002784 for (std::vector<ValueInfo>::iterator I = $6->begin(), E = $6->end();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002785 I != E; ++I)
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002786 ParamTypes.push_back((*I).val->getType());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002787 }
2788
2789 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2790 if (isVarArg) ParamTypes.pop_back();
2791
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002792 if (!$3.type->get()->isFirstClassType() &&
2793 $3.type->get() != Type::VoidTy)
Reid Spencer713eedc2006-08-18 08:43:06 +00002794 GEN_ERROR("LLVM functions cannot return aggregate types!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002795
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002796 Ty = FunctionType::get($3.type->get(), ParamTypes, isVarArg);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002797 PFTy = PointerType::get(Ty);
2798 }
2799
2800 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer309080a2006-09-28 19:28:24 +00002801 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002802
2803 // Create the call node...
2804 if (!$6) { // Has no arguments?
2805 // Make sure no arguments is a good thing!
2806 if (Ty->getNumParams() != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002807 GEN_ERROR("No arguments passed to a function that "
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002808 "expects arguments!");
2809
2810 $$ = new CallInst(V, std::vector<Value*>());
2811 } else { // Has arguments?
2812 // Loop through FunctionType's arguments and ensure they are specified
2813 // correctly!
2814 //
2815 FunctionType::param_iterator I = Ty->param_begin();
2816 FunctionType::param_iterator E = Ty->param_end();
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002817 std::vector<ValueInfo>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002818
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002819 std::vector<Value*> args;
2820 for (; ArgI != ArgE ; ++ArgI)
2821 if (I == E) {
2822 if (Ty->isVarArg()) {
2823 args.push_back((*ArgI).val);
2824 } else {
2825 GEN_ERROR("Too many parameters for function of type " +
2826 Ty->getDescription());
2827 }
2828 } else {
2829 if ((*ArgI).val->getType() != *I) {
2830 GEN_ERROR("Parameter " + (*ArgI).val->getName() +
2831 " is not of type '" + (*I)->getDescription() + "'!");
2832 } else
2833 args.push_back((*ArgI).val);
2834 ++I;
2835 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002836
2837 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002838 GEN_ERROR("Invalid number of parameters detected!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002839
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002840 $$ = new CallInst(V, args);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002841 }
2842 cast<CallInst>($$)->setTailCall($1);
2843 cast<CallInst>($$)->setCallingConv($2);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002844 delete $3.type;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002845 delete $6;
Reid Spencer713eedc2006-08-18 08:43:06 +00002846 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002847 }
2848 | MemoryInst {
2849 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002850 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002851 };
2852
2853
2854// IndexList - List of indices for GEP based instructions...
2855IndexList : ',' ValueRefList {
2856 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002857 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002858 } | /* empty */ {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002859 $$ = new std::vector<ValueInfo>();
Reid Spencer713eedc2006-08-18 08:43:06 +00002860 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002861 };
2862
2863OptVolatile : VOLATILE {
2864 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002865 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002866 }
2867 | /* empty */ {
2868 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002869 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002870 };
2871
2872
2873
2874MemoryInst : MALLOC Types OptCAlign {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002875 $$ = new MallocInst($2.type->get(), 0, $3);
2876 delete $2.type;
Reid Spencer713eedc2006-08-18 08:43:06 +00002877 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002878 }
2879 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002880 Value* tmpVal = getVal($4.type->get(), $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002881 CHECK_FOR_ERROR
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002882 $$ = new MallocInst($2.type->get(), tmpVal, $6);
2883 delete $2.type;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002884 }
2885 | ALLOCA Types OptCAlign {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002886 $$ = new AllocaInst($2.type->get(), 0, $3);
2887 delete $2.type;
Reid Spencer713eedc2006-08-18 08:43:06 +00002888 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002889 }
2890 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002891 Value* tmpVal = getVal($4.type->get(), $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002892 CHECK_FOR_ERROR
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002893 $$ = new AllocaInst($2.type->get(), tmpVal, $6);
2894 delete $2.type;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002895 }
2896 | FREE ResolvedVal {
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002897 if (!isa<PointerType>($2.val->getType()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002898 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002899 $2.val->getType()->getDescription() + "!");
2900 $$ = new FreeInst($2.val);
Reid Spencer713eedc2006-08-18 08:43:06 +00002901 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002902 }
2903
2904 | OptVolatile LOAD Types ValueRef {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002905 if (!isa<PointerType>($3.type->get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002906 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002907 $3.type->get()->getDescription());
2908 if (!cast<PointerType>($3.type->get())->getElementType()->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002909 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002910 $3.type->get()->getDescription());
2911 Value* tmpVal = getVal($3.type->get(), $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002912 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002913 $$ = new LoadInst(tmpVal, "", $1);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002914 delete $3.type;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002915 }
2916 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002917 const PointerType *PT = dyn_cast<PointerType>($5.type->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002918 if (!PT)
Reid Spencer713eedc2006-08-18 08:43:06 +00002919 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002920 ($5.type->get())->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002921 const Type *ElTy = PT->getElementType();
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002922 if (ElTy != $3.val->getType())
2923 GEN_ERROR("Can't store '" + $3.val->getType()->getDescription() +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002924 "' into space of type '" + ElTy->getDescription() + "'!");
2925
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002926 Value* tmpVal = getVal($5.type->get(), $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002927 CHECK_FOR_ERROR
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002928 $$ = new StoreInst($3.val, tmpVal, $1);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002929 delete $5.type;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002930 }
2931 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002932 if (!isa<PointerType>($2.type->get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002933 GEN_ERROR("getelementptr insn requires pointer operand!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002934
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002935 std::vector<Value*> indices;
2936 for (unsigned i = 0, e = $4->size(); i != e; ++i)
2937 indices.push_back((*$4)[i].val);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002938
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002939 if (!GetElementPtrInst::getIndexedType($2.type->get(), indices, true))
Reid Spencer713eedc2006-08-18 08:43:06 +00002940 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002941 $2.type->get()->getDescription()+ "'!");
2942 Value* tmpVal = getVal($2.type->get(), $3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002943 CHECK_FOR_ERROR
Reid Spencer55f1fbe2006-11-28 07:29:44 +00002944 $$ = new GetElementPtrInst(tmpVal, indices);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002945 delete $2.type;
Reid Spencer309080a2006-09-28 19:28:24 +00002946 delete $4;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002947 };
2948
2949
2950%%
Reid Spencer713eedc2006-08-18 08:43:06 +00002951
2952void llvm::GenerateError(const std::string &message, int LineNo) {
2953 if (LineNo == -1) LineNo = llvmAsmlineno;
2954 // TODO: column number in exception
2955 if (TheParseError)
2956 TheParseError->setError(CurFilename, message, LineNo);
2957 TriggerError = 1;
2958}
2959
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002960int yyerror(const char *ErrorMsg) {
2961 std::string where
2962 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2963 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
2964 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
2965 if (yychar == YYEMPTY || yychar == 0)
2966 errMsg += "end-of-file.";
2967 else
2968 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Reid Spencer713eedc2006-08-18 08:43:06 +00002969 GenerateError(errMsg);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002970 return 0;
2971}