blob: 0eba227a2dd543077e08b738d683a5112c168a42 [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"
Chris Lattnerf20e61f2006-02-15 07:22:58 +000021#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer42f0cbe2006-12-31 05:40:51 +000022#include "llvm/Support/CommandLine.h"
Chris Lattnerf20e61f2006-02-15 07:22:58 +000023#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>
Reid Spencer42f0cbe2006-12-31 05:40:51 +000029#ifndef NDEBUG
30#define YYDEBUG 1
31#endif
Chris Lattnerf20e61f2006-02-15 07:22:58 +000032
Reid Spencerb50974a2006-08-18 17:32:55 +000033// The following is a gross hack. In order to rid the libAsmParser library of
34// exceptions, we have to have a way of getting the yyparse function to go into
35// an error situation. So, whenever we want an error to occur, the GenerateError
36// function (see bottom of file) sets TriggerError. Then, at the end of each
37// production in the grammer we use CHECK_FOR_ERROR which will invoke YYERROR
38// (a goto) to put YACC in error state. Furthermore, several calls to
39// GenerateError are made from inside productions and they must simulate the
40// previous exception behavior by exiting the production immediately. We have
41// replaced these with the GEN_ERROR macro which calls GeneratError and then
42// immediately invokes YYERROR. This would be so much cleaner if it was a
43// recursive descent parser.
Reid Spencer713eedc2006-08-18 08:43:06 +000044static bool TriggerError = false;
Reid Spencerff359002006-10-09 17:36:59 +000045#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYABORT; } }
Reid Spencer713eedc2006-08-18 08:43:06 +000046#define GEN_ERROR(msg) { GenerateError(msg); YYERROR; }
47
Chris Lattnerf20e61f2006-02-15 07:22:58 +000048int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
49int yylex(); // declaration" of xxx warnings.
50int yyparse();
51
52namespace llvm {
53 std::string CurFilename;
Reid Spencer42f0cbe2006-12-31 05:40:51 +000054#if YYDEBUG
55static cl::opt<bool>
56Debug("debug-yacc", cl::desc("Print yacc debug state changes"),
57 cl::Hidden, cl::init(false));
58#endif
Chris Lattnerf20e61f2006-02-15 07:22:58 +000059}
60using namespace llvm;
61
62static Module *ParserResult;
63
64// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
65// relating to upreferences in the input stream.
66//
67//#define DEBUG_UPREFS 1
68#ifdef DEBUG_UPREFS
Bill Wendlingf3baad32006-12-07 01:30:32 +000069#define UR_OUT(X) cerr << X
Chris Lattnerf20e61f2006-02-15 07:22:58 +000070#else
71#define UR_OUT(X)
72#endif
73
74#define YYERROR_VERBOSE 1
75
Chris Lattnerf20e61f2006-02-15 07:22:58 +000076static GlobalVariable *CurGV;
77
78
79// This contains info used when building the body of a function. It is
80// destroyed when the function is completed.
81//
82typedef std::vector<Value *> ValueList; // Numbered defs
Reid Spencer42f0cbe2006-12-31 05:40:51 +000083
Chris Lattnerf20e61f2006-02-15 07:22:58 +000084static void
85ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
86 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
87
88static struct PerModuleInfo {
89 Module *CurrentModule;
90 std::map<const Type *, ValueList> Values; // Module level numbered definitions
91 std::map<const Type *,ValueList> LateResolveValues;
Reid Spencer55f1fbe2006-11-28 07:29:44 +000092 std::vector<PATypeHolder> Types;
93 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattnerf20e61f2006-02-15 07:22:58 +000094
95 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Chris Lattner7aa45902006-06-21 16:53:00 +000096 /// how they were referenced and on which line of the input they came from so
Chris Lattnerf20e61f2006-02-15 07:22:58 +000097 /// that we can resolve them later and print error messages as appropriate.
98 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
99
100 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
101 // references to global values. Global values may be referenced before they
102 // are defined, and if so, the temporary object that they represent is held
103 // here. This is used for forward references of GlobalValues.
104 //
105 typedef std::map<std::pair<const PointerType *,
106 ValID>, GlobalValue*> GlobalRefsType;
107 GlobalRefsType GlobalRefs;
108
109 void ModuleDone() {
110 // If we could not resolve some functions at function compilation time
111 // (calls to functions before they are defined), resolve them now... Types
112 // are resolved when the constant pool has been completely parsed.
113 //
114 ResolveDefinitions(LateResolveValues);
Reid Spencer309080a2006-09-28 19:28:24 +0000115 if (TriggerError)
116 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000117
118 // Check to make sure that all global value forward references have been
119 // resolved!
120 //
121 if (!GlobalRefs.empty()) {
122 std::string UndefinedReferences = "Unresolved global references exist:\n";
123
124 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
125 I != E; ++I) {
126 UndefinedReferences += " " + I->first.first->getDescription() + " " +
127 I->first.second.getName() + "\n";
128 }
Reid Spencer713eedc2006-08-18 08:43:06 +0000129 GenerateError(UndefinedReferences);
Reid Spencer309080a2006-09-28 19:28:24 +0000130 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000131 }
132
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000133 Values.clear(); // Clear out function local definitions
134 Types.clear();
135 CurrentModule = 0;
136 }
137
138 // GetForwardRefForGlobal - Check to see if there is a forward reference
139 // for this global. If so, remove it from the GlobalRefs map and return it.
140 // If not, just return null.
141 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
142 // Check to see if there is a forward reference to this global variable...
143 // if there is, eliminate it and patch the reference to use the new def'n.
144 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
145 GlobalValue *Ret = 0;
146 if (I != GlobalRefs.end()) {
147 Ret = I->second;
148 GlobalRefs.erase(I);
149 }
150 return Ret;
151 }
152} CurModule;
153
154static struct PerFunctionInfo {
155 Function *CurrentFunction; // Pointer to current function being created
156
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000157 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000158 std::map<const Type*, ValueList> LateResolveValues;
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000159 bool isDeclare; // Is this function a forward declararation?
160 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000161
162 /// BBForwardRefs - When we see forward references to basic blocks, keep
163 /// track of them here.
164 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
165 std::vector<BasicBlock*> NumberedBlocks;
166 unsigned NextBBNum;
167
168 inline PerFunctionInfo() {
169 CurrentFunction = 0;
170 isDeclare = false;
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000171 Linkage = GlobalValue::ExternalLinkage;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000172 }
173
174 inline void FunctionStart(Function *M) {
175 CurrentFunction = M;
176 NextBBNum = 0;
177 }
178
179 void FunctionDone() {
180 NumberedBlocks.clear();
181
182 // Any forward referenced blocks left?
Reid Spencer309080a2006-09-28 19:28:24 +0000183 if (!BBForwardRefs.empty()) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000184 GenerateError("Undefined reference to label " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000185 BBForwardRefs.begin()->first->getName());
Reid Spencer309080a2006-09-28 19:28:24 +0000186 return;
187 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000188
189 // Resolve all forward references now.
190 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
191
192 Values.clear(); // Clear out function local definitions
193 CurrentFunction = 0;
194 isDeclare = false;
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000195 Linkage = GlobalValue::ExternalLinkage;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000196 }
197} CurFun; // Info for the current function...
198
199static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
200
201
202//===----------------------------------------------------------------------===//
203// Code to handle definitions of all the types
204//===----------------------------------------------------------------------===//
205
206static int InsertValue(Value *V,
207 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
208 if (V->hasName()) return -1; // Is this a numbered definition?
209
210 // Yes, insert the value into the value table...
211 ValueList &List = ValueTab[V->getType()];
212 List.push_back(V);
213 return List.size()-1;
214}
215
216static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
217 switch (D.Type) {
218 case ValID::NumberVal: // Is it a numbered definition?
219 // Module constants occupy the lowest numbered slots...
220 if ((unsigned)D.Num < CurModule.Types.size())
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000221 return CurModule.Types[(unsigned)D.Num];
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000222 break;
223 case ValID::NameVal: // Is it a named definition?
224 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
225 D.destroy(); // Free old strdup'd memory...
226 return N;
227 }
228 break;
229 default:
Reid Spencer713eedc2006-08-18 08:43:06 +0000230 GenerateError("Internal parser error: Invalid symbol type reference!");
Reid Spencer309080a2006-09-28 19:28:24 +0000231 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000232 }
233
234 // If we reached here, we referenced either a symbol that we don't know about
235 // or an id number that hasn't been read yet. We may be referencing something
236 // forward, so just create an entry to be resolved later and get to it...
237 //
238 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
239
240
241 if (inFunctionScope()) {
Reid Spencer309080a2006-09-28 19:28:24 +0000242 if (D.Type == ValID::NameVal) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000243 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer309080a2006-09-28 19:28:24 +0000244 return 0;
245 } else {
Reid Spencer713eedc2006-08-18 08:43:06 +0000246 GenerateError("Reference to an undefined type: #" + itostr(D.Num));
Reid Spencer309080a2006-09-28 19:28:24 +0000247 return 0;
248 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000249 }
250
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000251 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000252 if (I != CurModule.LateResolveTypes.end())
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000253 return I->second;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000254
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000255 Type *Typ = OpaqueType::get();
256 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
257 return Typ;
Reid Spencere2c32da2006-12-03 05:46:11 +0000258 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000259
260static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
261 SymbolTable &SymTab =
262 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
263 CurModule.CurrentModule->getSymbolTable();
264 return SymTab.lookup(Ty, Name);
265}
266
267// getValNonImprovising - Look up the value specified by the provided type and
268// the provided ValID. If the value exists and has already been defined, return
269// it. Otherwise return null.
270//
271static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Reid Spencer309080a2006-09-28 19:28:24 +0000272 if (isa<FunctionType>(Ty)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000273 GenerateError("Functions are not values and "
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000274 "must be referenced as pointers");
Reid Spencer309080a2006-09-28 19:28:24 +0000275 return 0;
276 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000277
278 switch (D.Type) {
279 case ValID::NumberVal: { // Is it a numbered definition?
280 unsigned Num = (unsigned)D.Num;
281
282 // Module constants occupy the lowest numbered slots...
283 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
284 if (VI != CurModule.Values.end()) {
285 if (Num < VI->second.size())
286 return VI->second[Num];
287 Num -= VI->second.size();
288 }
289
290 // Make sure that our type is within bounds
291 VI = CurFun.Values.find(Ty);
292 if (VI == CurFun.Values.end()) return 0;
293
294 // Check that the number is within bounds...
295 if (VI->second.size() <= Num) return 0;
296
297 return VI->second[Num];
298 }
299
300 case ValID::NameVal: { // Is it a named definition?
301 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
302 if (N == 0) return 0;
303
304 D.destroy(); // Free old strdup'd memory...
305 return N;
306 }
307
308 // Check to make sure that "Ty" is an integral type, and that our
309 // value will fit into the specified type...
310 case ValID::ConstSIntVal: // Is it a constant pool reference??
Reid Spencere0fc4df2006-10-20 07:07:24 +0000311 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000312 GenerateError("Signed integral constant '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000313 itostr(D.ConstPool64) + "' is invalid for type '" +
314 Ty->getDescription() + "'!");
Reid Spencer309080a2006-09-28 19:28:24 +0000315 return 0;
316 }
Reid Spencere0fc4df2006-10-20 07:07:24 +0000317 return ConstantInt::get(Ty, D.ConstPool64);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000318
319 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Reid Spencere0fc4df2006-10-20 07:07:24 +0000320 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
321 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000322 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000323 "' is invalid or out of range!");
Reid Spencer309080a2006-09-28 19:28:24 +0000324 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000325 } else { // This is really a signed reference. Transmogrify.
Reid Spencere0fc4df2006-10-20 07:07:24 +0000326 return ConstantInt::get(Ty, D.ConstPool64);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000327 }
328 } else {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000329 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000330 }
331
332 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Reid Spencer309080a2006-09-28 19:28:24 +0000333 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000334 GenerateError("FP constant invalid for type!!");
Reid Spencer309080a2006-09-28 19:28:24 +0000335 return 0;
336 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000337 return ConstantFP::get(Ty, D.ConstPoolFP);
338
339 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer309080a2006-09-28 19:28:24 +0000340 if (!isa<PointerType>(Ty)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000341 GenerateError("Cannot create a a non pointer null!");
Reid Spencer309080a2006-09-28 19:28:24 +0000342 return 0;
343 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000344 return ConstantPointerNull::get(cast<PointerType>(Ty));
345
346 case ValID::ConstUndefVal: // Is it an undef value?
347 return UndefValue::get(Ty);
348
349 case ValID::ConstZeroVal: // Is it a zero value?
350 return Constant::getNullValue(Ty);
351
352 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer309080a2006-09-28 19:28:24 +0000353 if (D.ConstantValue->getType() != Ty) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000354 GenerateError("Constant expression type different from required type!");
Reid Spencer309080a2006-09-28 19:28:24 +0000355 return 0;
356 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000357 return D.ConstantValue;
358
359 case ValID::InlineAsmVal: { // Inline asm expression
360 const PointerType *PTy = dyn_cast<PointerType>(Ty);
361 const FunctionType *FTy =
362 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer309080a2006-09-28 19:28:24 +0000363 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000364 GenerateError("Invalid type for asm constraint string!");
Reid Spencer309080a2006-09-28 19:28:24 +0000365 return 0;
366 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000367 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
368 D.IAD->HasSideEffects);
369 D.destroy(); // Free InlineAsmDescriptor.
370 return IA;
371 }
372 default:
373 assert(0 && "Unhandled case!");
374 return 0;
375 } // End of switch
376
377 assert(0 && "Unhandled case!");
378 return 0;
379}
380
381// getVal - This function is identical to getValNonImprovising, except that if a
382// value is not already defined, it "improvises" by creating a placeholder var
383// that looks and acts just like the requested variable. When the value is
384// defined later, all uses of the placeholder variable are replaced with the
385// real thing.
386//
387static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer309080a2006-09-28 19:28:24 +0000388 if (Ty == Type::LabelTy) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000389 GenerateError("Cannot use a basic block here");
Reid Spencer309080a2006-09-28 19:28:24 +0000390 return 0;
391 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000392
393 // See if the value has already been defined.
394 Value *V = getValNonImprovising(Ty, ID);
395 if (V) return V;
Reid Spencer309080a2006-09-28 19:28:24 +0000396 if (TriggerError) return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000397
Reid Spencer309080a2006-09-28 19:28:24 +0000398 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000399 GenerateError("Invalid use of a composite type!");
Reid Spencer309080a2006-09-28 19:28:24 +0000400 return 0;
401 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000402
403 // If we reached here, we referenced either a symbol that we don't know about
404 // or an id number that hasn't been read yet. We may be referencing something
405 // forward, so just create an entry to be resolved later and get to it...
406 //
407 V = new Argument(Ty);
408
409 // Remember where this forward reference came from. FIXME, shouldn't we try
410 // to recycle these things??
411 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
412 llvmAsmlineno)));
413
414 if (inFunctionScope())
415 InsertValue(V, CurFun.LateResolveValues);
416 else
417 InsertValue(V, CurModule.LateResolveValues);
418 return V;
419}
420
421/// getBBVal - This is used for two purposes:
422/// * If isDefinition is true, a new basic block with the specified ID is being
423/// defined.
424/// * If isDefinition is true, this is a reference to a basic block, which may
425/// or may not be a forward reference.
426///
427static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
428 assert(inFunctionScope() && "Can't get basic block at global scope!");
429
430 std::string Name;
431 BasicBlock *BB = 0;
432 switch (ID.Type) {
Reid Spencer309080a2006-09-28 19:28:24 +0000433 default:
434 GenerateError("Illegal label reference " + ID.getName());
435 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000436 case ValID::NumberVal: // Is it a numbered definition?
437 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
438 CurFun.NumberedBlocks.resize(ID.Num+1);
439 BB = CurFun.NumberedBlocks[ID.Num];
440 break;
441 case ValID::NameVal: // Is it a named definition?
442 Name = ID.Name;
443 if (Value *N = CurFun.CurrentFunction->
444 getSymbolTable().lookup(Type::LabelTy, Name))
445 BB = cast<BasicBlock>(N);
446 break;
447 }
448
449 // See if the block has already been defined.
450 if (BB) {
451 // If this is the definition of the block, make sure the existing value was
452 // just a forward reference. If it was a forward reference, there will be
453 // an entry for it in the PlaceHolderInfo map.
Reid Spencer309080a2006-09-28 19:28:24 +0000454 if (isDefinition && !CurFun.BBForwardRefs.erase(BB)) {
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000455 // The existing value was a definition, not a forward reference.
Reid Spencer713eedc2006-08-18 08:43:06 +0000456 GenerateError("Redefinition of label " + ID.getName());
Reid Spencer309080a2006-09-28 19:28:24 +0000457 return 0;
458 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000459
460 ID.destroy(); // Free strdup'd memory.
461 return BB;
462 }
463
464 // Otherwise this block has not been seen before.
465 BB = new BasicBlock("", CurFun.CurrentFunction);
466 if (ID.Type == ValID::NameVal) {
467 BB->setName(ID.Name);
468 } else {
469 CurFun.NumberedBlocks[ID.Num] = BB;
470 }
471
472 // If this is not a definition, keep track of it so we can use it as a forward
473 // reference.
474 if (!isDefinition) {
475 // Remember where this forward reference came from.
476 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
477 } else {
478 // The forward declaration could have been inserted anywhere in the
479 // function: insert it into the correct place now.
480 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
481 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
482 }
483 ID.destroy();
484 return BB;
485}
486
487
488//===----------------------------------------------------------------------===//
489// Code to handle forward references in instructions
490//===----------------------------------------------------------------------===//
491//
492// This code handles the late binding needed with statements that reference
493// values not defined yet... for example, a forward branch, or the PHI node for
494// a loop body.
495//
496// This keeps a table (CurFun.LateResolveValues) of all such forward references
497// and back patchs after we are done.
498//
499
500// ResolveDefinitions - If we could not resolve some defs at parsing
501// time (forward branches, phi functions for loops, etc...) resolve the
502// defs now...
503//
504static void
505ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
506 std::map<const Type*,ValueList> *FutureLateResolvers) {
507 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
508 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
509 E = LateResolvers.end(); LRI != E; ++LRI) {
510 ValueList &List = LRI->second;
511 while (!List.empty()) {
512 Value *V = List.back();
513 List.pop_back();
514
515 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
516 CurModule.PlaceHolderInfo.find(V);
517 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
518
519 ValID &DID = PHI->second.first;
520
521 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Reid Spencer309080a2006-09-28 19:28:24 +0000522 if (TriggerError)
523 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000524 if (TheRealValue) {
525 V->replaceAllUsesWith(TheRealValue);
526 delete V;
527 CurModule.PlaceHolderInfo.erase(PHI);
528 } else if (FutureLateResolvers) {
529 // Functions have their unresolved items forwarded to the module late
530 // resolver table
531 InsertValue(V, *FutureLateResolvers);
532 } else {
Reid Spencer309080a2006-09-28 19:28:24 +0000533 if (DID.Type == ValID::NameVal) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000534 GenerateError("Reference to an invalid definition: '" +DID.getName()+
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000535 "' of type '" + V->getType()->getDescription() + "'",
536 PHI->second.second);
Reid Spencer309080a2006-09-28 19:28:24 +0000537 return;
538 } else {
Reid Spencer713eedc2006-08-18 08:43:06 +0000539 GenerateError("Reference to an invalid definition: #" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000540 itostr(DID.Num) + " of type '" +
541 V->getType()->getDescription() + "'",
542 PHI->second.second);
Reid Spencer309080a2006-09-28 19:28:24 +0000543 return;
544 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000545 }
546 }
547 }
548
549 LateResolvers.clear();
550}
551
552// ResolveTypeTo - A brand new type was just declared. This means that (if
553// name is not null) things referencing Name can be resolved. Otherwise, things
554// refering to the number can be resolved. Do this now.
555//
556static void ResolveTypeTo(char *Name, const Type *ToTy) {
557 ValID D;
558 if (Name) D = ValID::create(Name);
559 else D = ValID::create((int)CurModule.Types.size());
560
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000561 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000562 CurModule.LateResolveTypes.find(D);
563 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000564 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000565 CurModule.LateResolveTypes.erase(I);
566 }
567}
568
569// setValueName - Set the specified value to the name given. The name may be
570// null potentially, in which case this is a noop. The string passed in is
571// assumed to be a malloc'd string buffer, and is free'd by this function.
572//
573static void setValueName(Value *V, char *NameStr) {
574 if (NameStr) {
575 std::string Name(NameStr); // Copy string
576 free(NameStr); // Free old string
577
Reid Spencer309080a2006-09-28 19:28:24 +0000578 if (V->getType() == Type::VoidTy) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000579 GenerateError("Can't assign name '" + Name+"' to value with void type!");
Reid Spencer309080a2006-09-28 19:28:24 +0000580 return;
581 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000582
583 assert(inFunctionScope() && "Must be in function scope!");
584 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
Reid Spencer309080a2006-09-28 19:28:24 +0000585 if (ST.lookup(V->getType(), Name)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000586 GenerateError("Redefinition of value named '" + Name + "' in the '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000587 V->getType()->getDescription() + "' type plane!");
Reid Spencer309080a2006-09-28 19:28:24 +0000588 return;
589 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000590
591 // Set the name.
592 V->setName(Name);
593 }
594}
595
596/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
597/// this is a declaration, otherwise it is a definition.
598static GlobalVariable *
599ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
600 bool isConstantGlobal, const Type *Ty,
601 Constant *Initializer) {
Reid Spencer309080a2006-09-28 19:28:24 +0000602 if (isa<FunctionType>(Ty)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000603 GenerateError("Cannot declare global vars of function type!");
Reid Spencer309080a2006-09-28 19:28:24 +0000604 return 0;
605 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000606
607 const PointerType *PTy = PointerType::get(Ty);
608
609 std::string Name;
610 if (NameStr) {
611 Name = NameStr; // Copy string
612 free(NameStr); // Free old string
613 }
614
615 // See if this global value was forward referenced. If so, recycle the
616 // object.
617 ValID ID;
618 if (!Name.empty()) {
619 ID = ValID::create((char*)Name.c_str());
620 } else {
621 ID = ValID::create((int)CurModule.Values[PTy].size());
622 }
623
624 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
625 // Move the global to the end of the list, from whereever it was
626 // previously inserted.
627 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
628 CurModule.CurrentModule->getGlobalList().remove(GV);
629 CurModule.CurrentModule->getGlobalList().push_back(GV);
630 GV->setInitializer(Initializer);
631 GV->setLinkage(Linkage);
632 GV->setConstant(isConstantGlobal);
633 InsertValue(GV, CurModule.Values);
634 return GV;
635 }
636
637 // If this global has a name, check to see if there is already a definition
638 // of this global in the module. If so, merge as appropriate. Note that
639 // this is really just a hack around problems in the CFE. :(
640 if (!Name.empty()) {
641 // We are a simple redefinition of a value, check to see if it is defined
642 // the same as the old one.
643 if (GlobalVariable *EGV =
644 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
645 // We are allowed to redefine a global variable in two circumstances:
646 // 1. If at least one of the globals is uninitialized or
647 // 2. If both initializers have the same value.
648 //
649 if (!EGV->hasInitializer() || !Initializer ||
650 EGV->getInitializer() == Initializer) {
651
652 // Make sure the existing global version gets the initializer! Make
653 // sure that it also gets marked const if the new version is.
654 if (Initializer && !EGV->hasInitializer())
655 EGV->setInitializer(Initializer);
656 if (isConstantGlobal)
657 EGV->setConstant(true);
658 EGV->setLinkage(Linkage);
659 return EGV;
660 }
661
Reid Spencer713eedc2006-08-18 08:43:06 +0000662 GenerateError("Redefinition of global variable named '" + Name +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000663 "' in the '" + Ty->getDescription() + "' type plane!");
Reid Spencer309080a2006-09-28 19:28:24 +0000664 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000665 }
666 }
667
668 // Otherwise there is no existing GV to use, create one now.
669 GlobalVariable *GV =
670 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
671 CurModule.CurrentModule);
672 InsertValue(GV, CurModule.Values);
673 return GV;
674}
675
676// setTypeName - Set the specified type to the name given. The name may be
677// null potentially, in which case this is a noop. The string passed in is
678// assumed to be a malloc'd string buffer, and is freed by this function.
679//
680// This function returns true if the type has already been defined, but is
681// allowed to be redefined in the specified context. If the name is a new name
682// for the type plane, it is inserted and false is returned.
683static bool setTypeName(const Type *T, char *NameStr) {
684 assert(!inFunctionScope() && "Can't give types function-local names!");
685 if (NameStr == 0) return false;
686
687 std::string Name(NameStr); // Copy string
688 free(NameStr); // Free old string
689
690 // We don't allow assigning names to void type
Reid Spencer309080a2006-09-28 19:28:24 +0000691 if (T == Type::VoidTy) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000692 GenerateError("Can't assign name '" + Name + "' to the void type!");
Reid Spencer309080a2006-09-28 19:28:24 +0000693 return false;
694 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000695
696 // Set the type name, checking for conflicts as we do so.
697 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
698
699 if (AlreadyExists) { // Inserting a name that is already defined???
700 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
701 assert(Existing && "Conflict but no matching type?");
702
703 // There is only one case where this is allowed: when we are refining an
704 // opaque type. In this case, Existing will be an opaque type.
705 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
706 // We ARE replacing an opaque type!
707 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
708 return true;
709 }
710
711 // Otherwise, this is an attempt to redefine a type. That's okay if
712 // the redefinition is identical to the original. This will be so if
713 // Existing and T point to the same Type object. In this one case we
714 // allow the equivalent redefinition.
715 if (Existing == T) return true; // Yes, it's equal.
716
717 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer713eedc2006-08-18 08:43:06 +0000718 GenerateError("Redefinition of type named '" + Name + "' in the '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000719 T->getDescription() + "' type plane!");
720 }
721
722 return false;
723}
724
725//===----------------------------------------------------------------------===//
726// Code for handling upreferences in type names...
727//
728
729// TypeContains - Returns true if Ty directly contains E in it.
730//
731static bool TypeContains(const Type *Ty, const Type *E) {
732 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
733 E) != Ty->subtype_end();
734}
735
736namespace {
737 struct UpRefRecord {
738 // NestingLevel - The number of nesting levels that need to be popped before
739 // this type is resolved.
740 unsigned NestingLevel;
741
742 // LastContainedTy - This is the type at the current binding level for the
743 // type. Every time we reduce the nesting level, this gets updated.
744 const Type *LastContainedTy;
745
746 // UpRefTy - This is the actual opaque type that the upreference is
747 // represented with.
748 OpaqueType *UpRefTy;
749
750 UpRefRecord(unsigned NL, OpaqueType *URTy)
751 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
752 };
753}
754
755// UpRefs - A list of the outstanding upreferences that need to be resolved.
756static std::vector<UpRefRecord> UpRefs;
757
758/// HandleUpRefs - Every time we finish a new layer of types, this function is
759/// called. It loops through the UpRefs vector, which is a list of the
760/// currently active types. For each type, if the up reference is contained in
761/// the newly completed type, we decrement the level count. When the level
762/// count reaches zero, the upreferenced type is the type that is passed in:
763/// thus we can complete the cycle.
764///
765static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner680aab62006-08-18 17:34:45 +0000766 // If Ty isn't abstract, or if there are no up-references in it, then there is
767 // nothing to resolve here.
768 if (!ty->isAbstract() || UpRefs.empty()) return ty;
769
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000770 PATypeHolder Ty(ty);
771 UR_OUT("Type '" << Ty->getDescription() <<
772 "' newly formed. Resolving upreferences.\n" <<
773 UpRefs.size() << " upreferences active!\n");
774
775 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
776 // to zero), we resolve them all together before we resolve them to Ty. At
777 // the end of the loop, if there is anything to resolve to Ty, it will be in
778 // this variable.
779 OpaqueType *TypeToResolve = 0;
780
781 for (unsigned i = 0; i != UpRefs.size(); ++i) {
782 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
783 << UpRefs[i].second->getDescription() << ") = "
784 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
785 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
786 // Decrement level of upreference
787 unsigned Level = --UpRefs[i].NestingLevel;
788 UpRefs[i].LastContainedTy = Ty;
789 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
790 if (Level == 0) { // Upreference should be resolved!
791 if (!TypeToResolve) {
792 TypeToResolve = UpRefs[i].UpRefTy;
793 } else {
794 UR_OUT(" * Resolving upreference for "
795 << UpRefs[i].second->getDescription() << "\n";
796 std::string OldName = UpRefs[i].UpRefTy->getDescription());
797 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
798 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
799 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
800 }
801 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
802 --i; // Do not skip the next element...
803 }
804 }
805 }
806
807 if (TypeToResolve) {
808 UR_OUT(" * Resolving upreference for "
809 << UpRefs[i].second->getDescription() << "\n";
810 std::string OldName = TypeToResolve->getDescription());
811 TypeToResolve->refineAbstractTypeTo(Ty);
812 }
813
814 return Ty;
815}
816
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000817//===----------------------------------------------------------------------===//
818// RunVMAsmParser - Define an interface to this parser
819//===----------------------------------------------------------------------===//
820//
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000821static Module* RunParser(Module * M);
822
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000823Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
824 set_scan_file(F);
825
826 CurFilename = Filename;
827 return RunParser(new Module(CurFilename));
828}
829
830Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
831 set_scan_string(AsmString);
832
833 CurFilename = "from_memory";
834 if (M == NULL) {
835 return RunParser(new Module (CurFilename));
836 } else {
837 return RunParser(M);
838 }
839}
840
841%}
842
843%union {
844 llvm::Module *ModuleVal;
845 llvm::Function *FunctionVal;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000846 llvm::BasicBlock *BasicBlockVal;
847 llvm::TerminatorInst *TermInstVal;
848 llvm::Instruction *InstVal;
Reid Spencere2c32da2006-12-03 05:46:11 +0000849 llvm::Constant *ConstVal;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000850
Reid Spencere2c32da2006-12-03 05:46:11 +0000851 const llvm::Type *PrimType;
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000852 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencere2c32da2006-12-03 05:46:11 +0000853 llvm::PATypeHolder *TypeVal;
854 llvm::Value *ValueVal;
Reid Spencere2c32da2006-12-03 05:46:11 +0000855 std::vector<llvm::Value*> *ValueList;
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000856 llvm::ArgListType *ArgList;
857 llvm::TypeWithAttrs TypeWithAttrs;
858 llvm::TypeWithAttrsList *TypeWithAttrsList;
859 llvm::ValueRefList *ValueRefList;
860
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000861 // Represent the RHS of PHI node
Reid Spencere2c32da2006-12-03 05:46:11 +0000862 std::list<std::pair<llvm::Value*,
863 llvm::BasicBlock*> > *PHIList;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000864 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencere2c32da2006-12-03 05:46:11 +0000865 std::vector<llvm::Constant*> *ConstVector;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000866
867 llvm::GlobalValue::LinkageTypes Linkage;
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000868 llvm::FunctionType::ParameterAttributes ParamAttrs;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000869 int64_t SInt64Val;
870 uint64_t UInt64Val;
871 int SIntVal;
872 unsigned UIntVal;
873 double FPVal;
874 bool BoolVal;
875
876 char *StrVal; // This memory is strdup'd!
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000877 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000878
Reid Spencere2c32da2006-12-03 05:46:11 +0000879 llvm::Instruction::BinaryOps BinaryOpVal;
880 llvm::Instruction::TermOps TermOpVal;
881 llvm::Instruction::MemoryOps MemOpVal;
882 llvm::Instruction::CastOps CastOpVal;
883 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000884 llvm::Module::Endianness Endianness;
Reid Spencere2c32da2006-12-03 05:46:11 +0000885 llvm::ICmpInst::Predicate IPredicate;
886 llvm::FCmpInst::Predicate FPredicate;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000887}
888
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000889%type <ModuleVal> Module
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000890%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
891%type <BasicBlockVal> BasicBlock InstructionList
892%type <TermInstVal> BBTerminatorInst
893%type <InstVal> Inst InstVal MemoryInst
894%type <ConstVal> ConstVal ConstExpr
895%type <ConstVector> ConstVector
896%type <ArgList> ArgList ArgListH
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000897%type <PHIList> PHIList
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000898%type <ValueRefList> ValueRefList // For call param lists & GEP indices
899%type <ValueList> IndexList // For GEP indices
900%type <TypeList> TypeListI
901%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
902%type <TypeWithAttrs> ArgType ResultType
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000903%type <JumpTable> JumpTable
904%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
905%type <BoolVal> OptVolatile // 'volatile' or not
906%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
907%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000908%type <Linkage> GVInternalLinkage GVExternalLinkage
909%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000910%type <Endianness> BigOrLittle
911
912// ValueRef - Unresolved reference to a definition or BB
913%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
914%type <ValueVal> ResolvedVal // <type> <valref> pair
915// Tokens and types for handling constant integer values
916//
917// ESINT64VAL - A negative number within long long range
918%token <SInt64Val> ESINT64VAL
919
920// EUINT64VAL - A positive number within uns. long long range
921%token <UInt64Val> EUINT64VAL
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000922
923%token <SIntVal> SINTVAL // Signed 32 bit ints...
924%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
925%type <SIntVal> INTVAL
926%token <FPVal> FPVAL // Float or Double constant
927
928// Built in types...
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000929%type <TypeVal> Types
930%type <PrimType> IntType FPType PrimType // Classifications
931%token <PrimType> VOID BOOL INT8 INT16 INT32 INT64
Reid Spencerdc0a3a22006-12-29 20:35:03 +0000932%token <PrimType> FLOAT DOUBLE LABEL
933%token TYPE
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000934
935%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
936%type <StrVal> Name OptName OptAssign
937%type <UIntVal> OptAlign OptCAlign
938%type <StrVal> OptSection SectionString
939
940%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Reid Spencerdc0a3a22006-12-29 20:35:03 +0000941%token DECLARE DEFINE GLOBAL CONSTANT SECTION VOLATILE
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000942%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000943%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000944%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
945%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Chris Lattner09c0e992006-05-19 21:28:53 +0000946%token CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +0000947%token X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattner7d1d0342006-10-22 06:08:13 +0000948%token DATALAYOUT
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000949%type <UIntVal> OptCallingConv
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000950%type <ParamAttrs> OptParamAttrs ParamAttrList ParamAttr
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000951
952// Basic Block Terminating Operators
953%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
954
955// Binary Operators
Reid Spencer266e42b2006-12-23 06:05:41 +0000956%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencerde46e482006-11-02 20:25:50 +0000957%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencere2c32da2006-12-03 05:46:11 +0000958%token <OtherOpVal> ICMP FCMP
Reid Spencere2c32da2006-12-03 05:46:11 +0000959%type <IPredicate> IPredicates
Reid Spencere2c32da2006-12-03 05:46:11 +0000960%type <FPredicate> FPredicates
Reid Spencer1960ef32006-12-03 06:59:29 +0000961%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
962%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000963
964// Memory Instructions
965%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
966
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000967// Cast Operators
968%type <CastOpVal> CastOps
969%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
970%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
971
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000972// Other Operators
973%type <OtherOpVal> ShiftOps
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000974%token <OtherOpVal> PHI_TOK SELECT SHL LSHR ASHR VAARG
Chris Lattner9ff96a72006-04-08 01:18:56 +0000975%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000976
977
978%start Module
979%%
980
981// Handle constant integer size restriction and conversion...
982//
983INTVAL : SINTVAL;
984INTVAL : UINTVAL {
985 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
Reid Spencer713eedc2006-08-18 08:43:06 +0000986 GEN_ERROR("Value too large for type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000987 $$ = (int32_t)$1;
Reid Spencer713eedc2006-08-18 08:43:06 +0000988 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000989};
990
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000991// Operations that are notably excluded from this list include:
992// RET, BR, & SWITCH because they end basic blocks and are treated specially.
993//
Reid Spencerde46e482006-11-02 20:25:50 +0000994ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000995LogicalOps : AND | OR | XOR;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000996CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
997 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
998ShiftOps : SHL | LSHR | ASHR;
Reid Spencer1960ef32006-12-03 06:59:29 +0000999IPredicates
Reid Spencerd2e0c342006-12-04 05:24:24 +00001000 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer1960ef32006-12-03 06:59:29 +00001001 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1002 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1003 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1004 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1005 ;
1006
1007FPredicates
1008 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1009 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1010 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1011 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1012 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1013 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1014 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1015 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1016 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1017 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001018
1019// These are some types that allow classification if we only want a particular
1020// thing... for example, only a signed, unsigned, or integral type.
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001021IntType : INT64 | INT32 | INT16 | INT8;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001022FPType : FLOAT | DOUBLE;
1023
1024// OptAssign - Value producing statements have an optional assignment component
1025OptAssign : Name '=' {
1026 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001027 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001028 }
1029 | /*empty*/ {
1030 $$ = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00001031 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001032 };
1033
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001034GVInternalLinkage
1035 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1036 | WEAK { $$ = GlobalValue::WeakLinkage; }
1037 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1038 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1039 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1040 ;
1041
1042GVExternalLinkage
1043 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1044 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1045 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1046 ;
1047
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001048FunctionDeclareLinkage
1049 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1050 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1051 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001052 ;
1053
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001054FunctionDefineLinkage
1055 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1056 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001057 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1058 | WEAK { $$ = GlobalValue::WeakLinkage; }
1059 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001060 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001061
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +00001062OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1063 CCC_TOK { $$ = CallingConv::C; } |
1064 CSRETCC_TOK { $$ = CallingConv::CSRet; } |
1065 FASTCC_TOK { $$ = CallingConv::Fast; } |
1066 COLDCC_TOK { $$ = CallingConv::Cold; } |
1067 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1068 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1069 CC_TOK EUINT64VAL {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001070 if ((unsigned)$2 != $2)
Reid Spencer713eedc2006-08-18 08:43:06 +00001071 GEN_ERROR("Calling conv too large!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001072 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001073 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001074 };
1075
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001076ParamAttr : ZEXT { $$ = FunctionType::ZExtAttribute; }
1077 | SEXT { $$ = FunctionType::SExtAttribute; }
1078 ;
1079
1080ParamAttrList : ParamAttr { $$ = $1; }
1081 | ParamAttrList ',' ParamAttr {
1082 $$ = FunctionType::ParameterAttributes($1 | $3);
1083 }
1084 ;
1085
1086OptParamAttrs : /* empty */ { $$ = FunctionType::NoAttributeSet; }
1087 | '@' ParamAttr { $$ = $2; }
1088 | '@' '(' ParamAttrList ')' { $$ = $3; }
1089 ;
1090
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001091// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1092// a comma before it.
1093OptAlign : /*empty*/ { $$ = 0; } |
1094 ALIGN EUINT64VAL {
1095 $$ = $2;
1096 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencer713eedc2006-08-18 08:43:06 +00001097 GEN_ERROR("Alignment must be a power of two!");
1098 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001099};
1100OptCAlign : /*empty*/ { $$ = 0; } |
1101 ',' ALIGN EUINT64VAL {
1102 $$ = $3;
1103 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencer713eedc2006-08-18 08:43:06 +00001104 GEN_ERROR("Alignment must be a power of two!");
1105 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001106};
1107
1108
1109SectionString : SECTION STRINGCONSTANT {
1110 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1111 if ($2[i] == '"' || $2[i] == '\\')
Reid Spencer713eedc2006-08-18 08:43:06 +00001112 GEN_ERROR("Invalid character in section name!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001113 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001114 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001115};
1116
1117OptSection : /*empty*/ { $$ = 0; } |
1118 SectionString { $$ = $1; };
1119
1120// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1121// is set to be the global we are processing.
1122//
1123GlobalVarAttributes : /* empty */ {} |
1124 ',' GlobalVarAttribute GlobalVarAttributes {};
1125GlobalVarAttribute : SectionString {
1126 CurGV->setSection($1);
1127 free($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001128 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001129 }
1130 | ALIGN EUINT64VAL {
1131 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencer713eedc2006-08-18 08:43:06 +00001132 GEN_ERROR("Alignment must be a power of two!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001133 CurGV->setAlignment($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001134 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001135 };
1136
1137//===----------------------------------------------------------------------===//
1138// Types includes all predefined types... except void, because it can only be
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001139// used in specific contexts (function returning void for example).
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001140
1141// Derived types are added later...
1142//
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001143PrimType : BOOL | INT8 | INT16 | INT32 | INT64 | FLOAT | DOUBLE | LABEL ;
1144
1145Types
1146 : OPAQUE {
Reid Spencere2c32da2006-12-03 05:46:11 +00001147 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer713eedc2006-08-18 08:43:06 +00001148 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001149 }
1150 | PrimType {
Reid Spencere2c32da2006-12-03 05:46:11 +00001151 $$ = new PATypeHolder($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001152 CHECK_FOR_ERROR
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001153 }
1154 | Types '*' { // Pointer type?
1155 if (*$1 == Type::LabelTy)
1156 GEN_ERROR("Cannot form a pointer to a basic block");
1157 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1158 delete $1;
1159 CHECK_FOR_ERROR
1160 }
1161 | SymbolicValueRef { // Named types are also simple types...
1162 const Type* tmp = getTypeVal($1);
1163 CHECK_FOR_ERROR
1164 $$ = new PATypeHolder(tmp);
1165 }
1166 | '\\' EUINT64VAL { // Type UpReference
Reid Spencer713eedc2006-08-18 08:43:06 +00001167 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001168 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1169 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencere2c32da2006-12-03 05:46:11 +00001170 $$ = new PATypeHolder(OT);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001171 UR_OUT("New Upreference!\n");
Reid Spencer713eedc2006-08-18 08:43:06 +00001172 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001173 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001174 | Types OptParamAttrs '(' ArgTypeListI ')' {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001175 std::vector<const Type*> Params;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001176 std::vector<FunctionType::ParameterAttributes> Attrs;
1177 Attrs.push_back($2);
1178 for (TypeWithAttrsList::iterator I=$4->begin(), E=$4->end(); I != E; ++I) {
1179 Params.push_back(I->Ty->get());
1180 if (I->Ty->get() != Type::VoidTy)
1181 Attrs.push_back(I->Attrs);
1182 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001183 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1184 if (isVarArg) Params.pop_back();
1185
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001186 FunctionType *FT = FunctionType::get(*$1, Params, isVarArg, Attrs);
1187 delete $4; // Delete the argument list
1188 delete $1; // Delete the return type handle
1189 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer713eedc2006-08-18 08:43:06 +00001190 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001191 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001192 | VOID OptParamAttrs '(' ArgTypeListI ')' {
1193 std::vector<const Type*> Params;
1194 std::vector<FunctionType::ParameterAttributes> Attrs;
1195 Attrs.push_back($2);
1196 for (TypeWithAttrsList::iterator I=$4->begin(), E=$4->end(); I != E; ++I) {
1197 Params.push_back(I->Ty->get());
1198 if (I->Ty->get() != Type::VoidTy)
1199 Attrs.push_back(I->Attrs);
1200 }
1201 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1202 if (isVarArg) Params.pop_back();
1203
1204 FunctionType *FT = FunctionType::get($1, Params, isVarArg, Attrs);
1205 delete $4; // Delete the argument list
1206 $$ = new PATypeHolder(HandleUpRefs(FT));
1207 CHECK_FOR_ERROR
1208 }
1209
1210 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Reid Spencere2c32da2006-12-03 05:46:11 +00001211 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1212 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00001213 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001214 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001215 | '<' EUINT64VAL 'x' Types '>' { // Packed array type?
Reid Spencere2c32da2006-12-03 05:46:11 +00001216 const llvm::Type* ElemTy = $4->get();
1217 if ((unsigned)$2 != $2)
1218 GEN_ERROR("Unsigned result not equal to signed result");
1219 if (!ElemTy->isPrimitiveType())
1220 GEN_ERROR("Elemental type of a PackedType must be primitive");
1221 if (!isPowerOf2_32($2))
1222 GEN_ERROR("Vector length should be a power of 2!");
1223 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
1224 delete $4;
1225 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001226 }
1227 | '{' TypeListI '}' { // Structure type?
1228 std::vector<const Type*> Elements;
Reid Spencere2c32da2006-12-03 05:46:11 +00001229 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001230 E = $2->end(); I != E; ++I)
Reid Spencere2c32da2006-12-03 05:46:11 +00001231 Elements.push_back(*I);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001232
Reid Spencere2c32da2006-12-03 05:46:11 +00001233 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001234 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001235 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001236 }
1237 | '{' '}' { // Empty structure type?
Reid Spencere2c32da2006-12-03 05:46:11 +00001238 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer713eedc2006-08-18 08:43:06 +00001239 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001240 }
Andrew Lenharth2d189ff2006-12-08 18:07:09 +00001241 | '<' '{' TypeListI '}' '>' {
1242 std::vector<const Type*> Elements;
1243 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1244 E = $3->end(); I != E; ++I)
1245 Elements.push_back(*I);
1246
1247 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1248 delete $3;
1249 CHECK_FOR_ERROR
1250 }
1251 | '<' '{' '}' '>' { // Empty structure type?
1252 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1253 CHECK_FOR_ERROR
1254 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001255 ;
1256
1257ArgType
1258 : Types OptParamAttrs {
1259 $$.Ty = $1;
1260 $$.Attrs = $2;
1261 }
1262 ;
1263
1264ResultType
1265 : Types OptParamAttrs {
1266 if (!UpRefs.empty())
1267 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1268 if (!(*$1)->isFirstClassType())
1269 GEN_ERROR("LLVM functions cannot return aggregate types!");
1270 $$.Ty = $1;
1271 $$.Attrs = $2;
1272 }
1273 | VOID OptParamAttrs {
1274 $$.Ty = new PATypeHolder(Type::VoidTy);
1275 $$.Attrs = $2;
1276 }
1277 ;
1278
1279ArgTypeList : ArgType {
1280 $$ = new TypeWithAttrsList();
1281 $$->push_back($1);
1282 CHECK_FOR_ERROR
1283 }
1284 | ArgTypeList ',' ArgType {
1285 ($$=$1)->push_back($3);
1286 CHECK_FOR_ERROR
1287 }
1288 ;
1289
1290ArgTypeListI
1291 : ArgTypeList
1292 | ArgTypeList ',' DOTDOTDOT {
1293 $$=$1;
1294 TypeWithAttrs TWA; TWA.Attrs = FunctionType::NoAttributeSet;
1295 TWA.Ty = new PATypeHolder(Type::VoidTy);
1296 $$->push_back(TWA);
1297 CHECK_FOR_ERROR
1298 }
1299 | DOTDOTDOT {
1300 $$ = new TypeWithAttrsList;
1301 TypeWithAttrs TWA; TWA.Attrs = FunctionType::NoAttributeSet;
1302 TWA.Ty = new PATypeHolder(Type::VoidTy);
1303 $$->push_back(TWA);
1304 CHECK_FOR_ERROR
1305 }
1306 | /*empty*/ {
1307 $$ = new TypeWithAttrsList();
Reid Spencer713eedc2006-08-18 08:43:06 +00001308 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001309 };
1310
1311// TypeList - Used for struct declarations and as a basis for function type
1312// declaration type lists
1313//
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001314TypeListI : Types {
Reid Spencere2c32da2006-12-03 05:46:11 +00001315 $$ = new std::list<PATypeHolder>();
1316 $$->push_back(*$1); delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001317 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001318 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001319 | TypeListI ',' Types {
Reid Spencere2c32da2006-12-03 05:46:11 +00001320 ($$=$1)->push_back(*$3); delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001321 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001322 };
1323
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001324// ConstVal - The various declarations that go into the constant pool. This
1325// production is used ONLY to represent constants that show up AFTER a 'const',
1326// 'constant' or 'global' token at global scope. Constants that can be inlined
1327// into other expressions (such as integers and constexprs) are handled by the
1328// ResolvedVal, ValueRef and ConstValueRef productions.
1329//
1330ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001331 if (!UpRefs.empty())
1332 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001333 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001334 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001335 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00001336 (*$1)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001337 const Type *ETy = ATy->getElementType();
1338 int NumElements = ATy->getNumElements();
1339
1340 // Verify that we have the correct size...
1341 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer713eedc2006-08-18 08:43:06 +00001342 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001343 utostr($3->size()) + " arguments, but has size of " +
1344 itostr(NumElements) + "!");
1345
1346 // Verify all elements are correct type!
1347 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencere2c32da2006-12-03 05:46:11 +00001348 if (ETy != (*$3)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001349 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001350 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencere2c32da2006-12-03 05:46:11 +00001351 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001352 }
1353
Reid Spencere2c32da2006-12-03 05:46:11 +00001354 $$ = ConstantArray::get(ATy, *$3);
1355 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001356 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001357 }
1358 | Types '[' ']' {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001359 if (!UpRefs.empty())
1360 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001361 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001362 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001363 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00001364 (*$1)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001365
1366 int NumElements = ATy->getNumElements();
1367 if (NumElements != -1 && NumElements != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001368 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001369 " arguments, but has size of " + itostr(NumElements) +"!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001370 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1371 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001372 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001373 }
1374 | Types 'c' STRINGCONSTANT {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001375 if (!UpRefs.empty())
1376 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001377 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001378 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001379 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00001380 (*$1)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001381
1382 int NumElements = ATy->getNumElements();
1383 const Type *ETy = ATy->getElementType();
1384 char *EndStr = UnEscapeLexed($3, true);
1385 if (NumElements != -1 && NumElements != (EndStr-$3))
Reid Spencer713eedc2006-08-18 08:43:06 +00001386 GEN_ERROR("Can't build string constant of size " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001387 itostr((int)(EndStr-$3)) +
1388 " when array has size " + itostr(NumElements) + "!");
1389 std::vector<Constant*> Vals;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001390 if (ETy == Type::Int8Ty) {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001391 for (unsigned char *C = (unsigned char *)$3;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001392 C != (unsigned char*)EndStr; ++C)
1393 Vals.push_back(ConstantInt::get(ETy, *C));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001394 } else {
1395 free($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001396 GEN_ERROR("Cannot build string arrays of non byte sized elements!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001397 }
1398 free($3);
Reid Spencere2c32da2006-12-03 05:46:11 +00001399 $$ = ConstantArray::get(ATy, Vals);
1400 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001401 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001402 }
1403 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001404 if (!UpRefs.empty())
1405 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001406 const PackedType *PTy = dyn_cast<PackedType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001407 if (PTy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001408 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00001409 (*$1)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001410 const Type *ETy = PTy->getElementType();
1411 int NumElements = PTy->getNumElements();
1412
1413 // Verify that we have the correct size...
1414 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer713eedc2006-08-18 08:43:06 +00001415 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001416 utostr($3->size()) + " arguments, but has size of " +
1417 itostr(NumElements) + "!");
1418
1419 // Verify all elements are correct type!
1420 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencere2c32da2006-12-03 05:46:11 +00001421 if (ETy != (*$3)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001422 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001423 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencere2c32da2006-12-03 05:46:11 +00001424 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001425 }
1426
Reid Spencere2c32da2006-12-03 05:46:11 +00001427 $$ = ConstantPacked::get(PTy, *$3);
1428 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001429 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001430 }
1431 | Types '{' ConstVector '}' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001432 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001433 if (STy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001434 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00001435 (*$1)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001436
1437 if ($3->size() != STy->getNumContainedTypes())
Reid Spencer713eedc2006-08-18 08:43:06 +00001438 GEN_ERROR("Illegal number of initializers for structure type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001439
1440 // Check to ensure that constants are compatible with the type initializer!
1441 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencere2c32da2006-12-03 05:46:11 +00001442 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer713eedc2006-08-18 08:43:06 +00001443 GEN_ERROR("Expected type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001444 STy->getElementType(i)->getDescription() +
1445 "' for element #" + utostr(i) +
1446 " of structure initializer!");
1447
Reid Spencere2c32da2006-12-03 05:46:11 +00001448 $$ = ConstantStruct::get(STy, *$3);
1449 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001450 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001451 }
1452 | Types '{' '}' {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001453 if (!UpRefs.empty())
1454 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001455 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001456 if (STy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001457 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00001458 (*$1)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001459
1460 if (STy->getNumContainedTypes() != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001461 GEN_ERROR("Illegal number of initializers for structure type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001462
Reid Spencere2c32da2006-12-03 05:46:11 +00001463 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1464 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001465 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001466 }
1467 | Types NULL_TOK {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001468 if (!UpRefs.empty())
1469 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001470 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001471 if (PTy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001472 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00001473 (*$1)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001474
Reid Spencere2c32da2006-12-03 05:46:11 +00001475 $$ = ConstantPointerNull::get(PTy);
1476 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001477 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001478 }
1479 | Types UNDEF {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001480 if (!UpRefs.empty())
1481 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001482 $$ = UndefValue::get($1->get());
1483 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001484 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001485 }
1486 | Types SymbolicValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001487 if (!UpRefs.empty())
1488 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001489 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001490 if (Ty == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001491 GEN_ERROR("Global const reference must be a pointer type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001492
1493 // ConstExprs can exist in the body of a function, thus creating
1494 // GlobalValues whenever they refer to a variable. Because we are in
1495 // the context of a function, getValNonImprovising will search the functions
1496 // symbol table instead of the module symbol table for the global symbol,
1497 // which throws things all off. To get around this, we just tell
1498 // getValNonImprovising that we are at global scope here.
1499 //
1500 Function *SavedCurFn = CurFun.CurrentFunction;
1501 CurFun.CurrentFunction = 0;
1502
1503 Value *V = getValNonImprovising(Ty, $2);
Reid Spencer309080a2006-09-28 19:28:24 +00001504 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001505
1506 CurFun.CurrentFunction = SavedCurFn;
1507
1508 // If this is an initializer for a constant pointer, which is referencing a
1509 // (currently) undefined variable, create a stub now that shall be replaced
1510 // in the future with the right type of variable.
1511 //
1512 if (V == 0) {
1513 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1514 const PointerType *PT = cast<PointerType>(Ty);
1515
1516 // First check to see if the forward references value is already created!
1517 PerModuleInfo::GlobalRefsType::iterator I =
1518 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1519
1520 if (I != CurModule.GlobalRefs.end()) {
1521 V = I->second; // Placeholder already exists, use it...
1522 $2.destroy();
1523 } else {
1524 std::string Name;
1525 if ($2.Type == ValID::NameVal) Name = $2.Name;
1526
1527 // Create the forward referenced global.
1528 GlobalValue *GV;
1529 if (const FunctionType *FTy =
1530 dyn_cast<FunctionType>(PT->getElementType())) {
1531 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1532 CurModule.CurrentModule);
1533 } else {
1534 GV = new GlobalVariable(PT->getElementType(), false,
1535 GlobalValue::ExternalLinkage, 0,
1536 Name, CurModule.CurrentModule);
1537 }
1538
1539 // Keep track of the fact that we have a forward ref to recycle it
1540 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1541 V = GV;
1542 }
1543 }
1544
Reid Spencere2c32da2006-12-03 05:46:11 +00001545 $$ = cast<GlobalValue>(V);
1546 delete $1; // Free the type handle
Reid Spencer713eedc2006-08-18 08:43:06 +00001547 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001548 }
1549 | Types ConstExpr {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001550 if (!UpRefs.empty())
1551 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001552 if ($1->get() != $2->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001553 GEN_ERROR("Mismatched types for constant expression!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001554 $$ = $2;
Reid Spencere2c32da2006-12-03 05:46:11 +00001555 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001556 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001557 }
1558 | Types ZEROINITIALIZER {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001559 if (!UpRefs.empty())
1560 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001561 const Type *Ty = $1->get();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001562 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencer713eedc2006-08-18 08:43:06 +00001563 GEN_ERROR("Cannot create a null initialized value of this type!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001564 $$ = Constant::getNullValue(Ty);
1565 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001566 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00001567 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001568 | IntType ESINT64VAL { // integral constants
Reid Spencer266e42b2006-12-23 06:05:41 +00001569 if (!ConstantInt::isValueValidForType($1, $2))
1570 GEN_ERROR("Constant value doesn't fit in type!");
1571 $$ = ConstantInt::get($1, $2);
1572 CHECK_FOR_ERROR
1573 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001574 | IntType EUINT64VAL { // integral constants
Reid Spencer266e42b2006-12-23 06:05:41 +00001575 if (!ConstantInt::isValueValidForType($1, $2))
1576 GEN_ERROR("Constant value doesn't fit in type!");
1577 $$ = ConstantInt::get($1, $2);
1578 CHECK_FOR_ERROR
1579 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001580 | BOOL TRUETOK { // Boolean constants
Reid Spencere2c32da2006-12-03 05:46:11 +00001581 $$ = ConstantBool::getTrue();
Reid Spencer713eedc2006-08-18 08:43:06 +00001582 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001583 }
1584 | BOOL FALSETOK { // Boolean constants
Reid Spencere2c32da2006-12-03 05:46:11 +00001585 $$ = ConstantBool::getFalse();
Reid Spencer713eedc2006-08-18 08:43:06 +00001586 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001587 }
1588 | FPType FPVAL { // Float & Double constants
Reid Spencere2c32da2006-12-03 05:46:11 +00001589 if (!ConstantFP::isValueValidForType($1, $2))
Reid Spencer713eedc2006-08-18 08:43:06 +00001590 GEN_ERROR("Floating point constant invalid for type!!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001591 $$ = ConstantFP::get($1, $2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001592 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001593 };
1594
1595
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001596ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001597 if (!UpRefs.empty())
1598 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001599 Constant *Val = $3;
1600 const Type *Ty = $5->get();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001601 if (!Val->getType()->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001602 GEN_ERROR("cast constant expression from a non-primitive type: '" +
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001603 Val->getType()->getDescription() + "'!");
1604 if (!Ty->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001605 GEN_ERROR("cast constant expression to a non-primitive type: '" +
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001606 Ty->getDescription() + "'!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001607 $$ = ConstantExpr::getCast($1, $3, $5->get());
1608 delete $5;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001609 }
1610 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001611 if (!isa<PointerType>($3->getType()))
Reid Spencer713eedc2006-08-18 08:43:06 +00001612 GEN_ERROR("GetElementPtr requires a pointer operand!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001613
Reid Spencere2c32da2006-12-03 05:46:11 +00001614 const Type *IdxTy =
1615 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
1616 if (!IdxTy)
1617 GEN_ERROR("Index list invalid for constant getelementptr!");
1618
1619 std::vector<Constant*> IdxVec;
1620 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1621 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001622 IdxVec.push_back(C);
1623 else
Reid Spencer713eedc2006-08-18 08:43:06 +00001624 GEN_ERROR("Indices to constant getelementptr must be constants!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001625
1626 delete $4;
1627
Reid Spencere2c32da2006-12-03 05:46:11 +00001628 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Reid Spencer713eedc2006-08-18 08:43:06 +00001629 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001630 }
1631 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001632 if ($3->getType() != Type::BoolTy)
Reid Spencer713eedc2006-08-18 08:43:06 +00001633 GEN_ERROR("Select condition must be of boolean type!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001634 if ($5->getType() != $7->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001635 GEN_ERROR("Select operand types must match!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001636 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001637 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001638 }
1639 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001640 if ($3->getType() != $5->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001641 GEN_ERROR("Binary operator types must match!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001642 CHECK_FOR_ERROR;
Reid Spencer844668d2006-12-05 19:16:11 +00001643 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001644 }
1645 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001646 if ($3->getType() != $5->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001647 GEN_ERROR("Logical operator types must match!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001648 if (!$3->getType()->isIntegral()) {
1649 if (!isa<PackedType>($3->getType()) ||
1650 !cast<PackedType>($3->getType())->getElementType()->isIntegral())
Reid Spencer713eedc2006-08-18 08:43:06 +00001651 GEN_ERROR("Logical operator requires integral operands!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001652 }
Reid Spencere2c32da2006-12-03 05:46:11 +00001653 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001654 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001655 }
Reid Spencerd2e0c342006-12-04 05:24:24 +00001656 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1657 if ($4->getType() != $6->getType())
Reid Spencere2c32da2006-12-03 05:46:11 +00001658 GEN_ERROR("icmp operand types must match!");
Reid Spencerd2e0c342006-12-04 05:24:24 +00001659 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencere2c32da2006-12-03 05:46:11 +00001660 }
Reid Spencerd2e0c342006-12-04 05:24:24 +00001661 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1662 if ($4->getType() != $6->getType())
Reid Spencere2c32da2006-12-03 05:46:11 +00001663 GEN_ERROR("fcmp operand types must match!");
Reid Spencerd2e0c342006-12-04 05:24:24 +00001664 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencere2c32da2006-12-03 05:46:11 +00001665 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001666 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001667 if ($5->getType() != Type::Int8Ty)
1668 GEN_ERROR("Shift count for shift constant must be i8 type!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001669 if (!$3->getType()->isInteger())
Reid Spencer713eedc2006-08-18 08:43:06 +00001670 GEN_ERROR("Shift constant expression requires integer operand!");
Reid Spencerfdff9382006-11-08 06:47:33 +00001671 CHECK_FOR_ERROR;
Reid Spencere2c32da2006-12-03 05:46:11 +00001672 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001673 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001674 }
1675 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001676 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencer713eedc2006-08-18 08:43:06 +00001677 GEN_ERROR("Invalid extractelement operands!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001678 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001679 CHECK_FOR_ERROR
Chris Lattneraebccf82006-04-08 03:55:17 +00001680 }
1681 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001682 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencer713eedc2006-08-18 08:43:06 +00001683 GEN_ERROR("Invalid insertelement operands!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001684 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001685 CHECK_FOR_ERROR
Chris Lattneraebccf82006-04-08 03:55:17 +00001686 }
1687 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001688 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencer713eedc2006-08-18 08:43:06 +00001689 GEN_ERROR("Invalid shufflevector operands!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001690 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001691 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001692 };
1693
Chris Lattneraebccf82006-04-08 03:55:17 +00001694
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001695// ConstVector - A list of comma separated constants.
1696ConstVector : ConstVector ',' ConstVal {
1697 ($$ = $1)->push_back($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001698 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001699 }
1700 | ConstVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00001701 $$ = new std::vector<Constant*>();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001702 $$->push_back($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001703 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001704 };
1705
1706
1707// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1708GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
1709
1710
1711//===----------------------------------------------------------------------===//
1712// Rules to match Modules
1713//===----------------------------------------------------------------------===//
1714
1715// Module rule: Capture the result of parsing the whole file into a result
1716// variable...
1717//
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001718Module
1719 : DefinitionList {
1720 $$ = ParserResult = CurModule.CurrentModule;
1721 CurModule.ModuleDone();
1722 CHECK_FOR_ERROR;
1723 }
1724 | /*empty*/ {
1725 $$ = ParserResult = CurModule.CurrentModule;
1726 CurModule.ModuleDone();
1727 CHECK_FOR_ERROR;
1728 }
1729 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001730
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001731DefinitionList
1732 : Definition
1733 | DefinitionList Definition
1734 ;
1735
1736Definition
1737 : DEFINE { CurFun.isDeclare = false } Function {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001738 CurFun.FunctionDone();
Reid Spencer713eedc2006-08-18 08:43:06 +00001739 CHECK_FOR_ERROR
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001740 }
1741 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer713eedc2006-08-18 08:43:06 +00001742 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001743 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001744 | MODULE ASM_TOK AsmBlock {
Reid Spencer713eedc2006-08-18 08:43:06 +00001745 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001746 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001747 | IMPLEMENTATION {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001748 // Emit an error if there are any unresolved types left.
1749 if (!CurModule.LateResolveTypes.empty()) {
1750 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
Reid Spencer713eedc2006-08-18 08:43:06 +00001751 if (DID.Type == ValID::NameVal) {
1752 GEN_ERROR("Reference to an undefined type: '"+DID.getName() + "'");
1753 } else {
1754 GEN_ERROR("Reference to an undefined type: #" + itostr(DID.Num));
1755 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001756 }
Reid Spencer713eedc2006-08-18 08:43:06 +00001757 CHECK_FOR_ERROR
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001758 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001759 | OptAssign TYPE Types {
1760 if (!UpRefs.empty())
1761 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001762 // Eagerly resolve types. This is not an optimization, this is a
1763 // requirement that is due to the fact that we could have this:
1764 //
1765 // %list = type { %list * }
1766 // %list = type { %list * } ; repeated type decl
1767 //
1768 // If types are not resolved eagerly, then the two types will not be
1769 // determined to be the same type!
1770 //
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001771 ResolveTypeTo($1, *$3);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001772
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001773 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer309080a2006-09-28 19:28:24 +00001774 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001775 // If this is a named type that is not a redefinition, add it to the slot
1776 // table.
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001777 CurModule.Types.push_back(*$3);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001778 }
Reid Spencere2c32da2006-12-03 05:46:11 +00001779
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001780 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001781 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001782 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001783 | OptAssign TYPE VOID {
1784 ResolveTypeTo($1, $3);
1785
1786 if (!setTypeName($3, $1) && !$1) {
1787 CHECK_FOR_ERROR
1788 // If this is a named type that is not a redefinition, add it to the slot
1789 // table.
1790 CurModule.Types.push_back($3);
1791 }
1792 CHECK_FOR_ERROR
1793 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001794 | OptAssign GlobalType ConstVal { /* "Externally Visible" Linkage */
1795 if ($3 == 0)
Reid Spencer309080a2006-09-28 19:28:24 +00001796 GEN_ERROR("Global value initializer is not a constant!");
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001797 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage, $2,
1798 $3->getType(), $3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001799 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00001800 } GlobalVarAttributes {
1801 CurGV = 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001802 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001803 | OptAssign GVInternalLinkage GlobalType ConstVal {
1804 if ($4 == 0)
1805 GEN_ERROR("Global value initializer is not a constant!");
1806 CurGV = ParseGlobalVariable($1, $2, $3, $4->getType(), $4);
Reid Spencer309080a2006-09-28 19:28:24 +00001807 CHECK_FOR_ERROR
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001808 } GlobalVarAttributes {
1809 CurGV = 0;
1810 }
1811 | OptAssign GVExternalLinkage GlobalType Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001812 if (!UpRefs.empty())
1813 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001814 CurGV = ParseGlobalVariable($1, $2, $3, *$4, 0);
1815 CHECK_FOR_ERROR
1816 delete $4;
Reid Spencer309080a2006-09-28 19:28:24 +00001817 } GlobalVarAttributes {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001818 CurGV = 0;
1819 CHECK_FOR_ERROR
1820 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001821 | TARGET TargetDefinition {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001822 CHECK_FOR_ERROR
1823 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001824 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer713eedc2006-08-18 08:43:06 +00001825 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001826 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001827 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001828
1829
1830AsmBlock : STRINGCONSTANT {
1831 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
1832 char *EndStr = UnEscapeLexed($1, true);
1833 std::string NewAsm($1, EndStr);
1834 free($1);
1835
1836 if (AsmSoFar.empty())
1837 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
1838 else
1839 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
Reid Spencer713eedc2006-08-18 08:43:06 +00001840 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001841};
1842
1843BigOrLittle : BIG { $$ = Module::BigEndian; };
1844BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1845
1846TargetDefinition : ENDIAN '=' BigOrLittle {
1847 CurModule.CurrentModule->setEndianness($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001848 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001849 }
1850 | POINTERSIZE '=' EUINT64VAL {
1851 if ($3 == 32)
1852 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1853 else if ($3 == 64)
1854 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1855 else
Reid Spencer713eedc2006-08-18 08:43:06 +00001856 GEN_ERROR("Invalid pointer size: '" + utostr($3) + "'!");
1857 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001858 }
1859 | TRIPLE '=' STRINGCONSTANT {
1860 CurModule.CurrentModule->setTargetTriple($3);
1861 free($3);
John Criswell6af0b122006-10-24 19:09:48 +00001862 }
Chris Lattner7d1d0342006-10-22 06:08:13 +00001863 | DATALAYOUT '=' STRINGCONSTANT {
Owen Anderson85690f32006-10-18 02:21:48 +00001864 CurModule.CurrentModule->setDataLayout($3);
1865 free($3);
Owen Anderson85690f32006-10-18 02:21:48 +00001866 };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001867
1868LibrariesDefinition : '[' LibList ']';
1869
1870LibList : LibList ',' STRINGCONSTANT {
1871 CurModule.CurrentModule->addLibrary($3);
1872 free($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001873 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001874 }
1875 | STRINGCONSTANT {
1876 CurModule.CurrentModule->addLibrary($1);
1877 free($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001878 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001879 }
1880 | /* empty: end of list */ {
Reid Spencer713eedc2006-08-18 08:43:06 +00001881 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001882 }
1883 ;
1884
1885//===----------------------------------------------------------------------===//
1886// Rules to match Function Headers
1887//===----------------------------------------------------------------------===//
1888
1889Name : VAR_ID | STRINGCONSTANT;
1890OptName : Name | /*empty*/ { $$ = 0; };
1891
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001892ArgListH : ArgListH ',' Types OptParamAttrs OptName {
1893 if (!UpRefs.empty())
1894 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
1895 if (*$3 == Type::VoidTy)
1896 GEN_ERROR("void typed arguments are invalid!");
1897 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001898 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001899 $1->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00001900 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001901 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001902 | Types OptParamAttrs OptName {
1903 if (!UpRefs.empty())
1904 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1905 if (*$1 == Type::VoidTy)
1906 GEN_ERROR("void typed arguments are invalid!");
1907 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
1908 $$ = new ArgListType;
1909 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00001910 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001911 };
1912
1913ArgList : ArgListH {
1914 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001915 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001916 }
1917 | ArgListH ',' DOTDOTDOT {
1918 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001919 struct ArgListEntry E;
1920 E.Ty = new PATypeHolder(Type::VoidTy);
1921 E.Name = 0;
1922 E.Attrs = FunctionType::NoAttributeSet;
1923 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00001924 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001925 }
1926 | DOTDOTDOT {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001927 $$ = new ArgListType;
1928 struct ArgListEntry E;
1929 E.Ty = new PATypeHolder(Type::VoidTy);
1930 E.Name = 0;
1931 E.Attrs = FunctionType::NoAttributeSet;
1932 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00001933 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001934 }
1935 | /* empty */ {
1936 $$ = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00001937 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001938 };
1939
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001940FunctionHeaderH : OptCallingConv ResultType Name '(' ArgList ')'
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001941 OptSection OptAlign {
1942 UnEscapeLexed($3);
1943 std::string FunctionName($3);
1944 free($3); // Free strdup'd memory!
1945
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001946 std::vector<const Type*> ParamTypeList;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001947 std::vector<FunctionType::ParameterAttributes> ParamAttrs;
1948 ParamAttrs.push_back($2.Attrs);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001949 if ($5) { // If there are arguments...
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001950 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I) {
1951 const Type* Ty = I->Ty->get();
1952 ParamTypeList.push_back(Ty);
1953 if (Ty != Type::VoidTy)
1954 ParamAttrs.push_back(I->Attrs);
1955 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001956 }
1957
1958 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1959 if (isVarArg) ParamTypeList.pop_back();
1960
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001961 FunctionType *FT = FunctionType::get(*$2.Ty, ParamTypeList, isVarArg,
1962 ParamAttrs);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001963 const PointerType *PFT = PointerType::get(FT);
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001964 delete $2.Ty;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001965
1966 ValID ID;
1967 if (!FunctionName.empty()) {
1968 ID = ValID::create((char*)FunctionName.c_str());
1969 } else {
1970 ID = ValID::create((int)CurModule.Values[PFT].size());
1971 }
1972
1973 Function *Fn = 0;
1974 // See if this function was forward referenced. If so, recycle the object.
1975 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
1976 // Move the function to the end of the list, from whereever it was
1977 // previously inserted.
1978 Fn = cast<Function>(FWRef);
1979 CurModule.CurrentModule->getFunctionList().remove(Fn);
1980 CurModule.CurrentModule->getFunctionList().push_back(Fn);
1981 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
1982 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1983 // If this is the case, either we need to be a forward decl, or it needs
1984 // to be.
1985 if (!CurFun.isDeclare && !Fn->isExternal())
Reid Spencer713eedc2006-08-18 08:43:06 +00001986 GEN_ERROR("Redefinition of function '" + FunctionName + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001987
1988 // Make sure to strip off any argument names so we can't get conflicts.
1989 if (Fn->isExternal())
1990 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
1991 AI != AE; ++AI)
1992 AI->setName("");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001993 } else { // Not already defined?
1994 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1995 CurModule.CurrentModule);
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001996
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001997 InsertValue(Fn, CurModule.Values);
1998 }
1999
2000 CurFun.FunctionStart(Fn);
Anton Korobeynikov0ab01ff2006-09-17 13:06:18 +00002001
2002 if (CurFun.isDeclare) {
2003 // If we have declaration, always overwrite linkage. This will allow us to
2004 // correctly handle cases, when pointer to function is passed as argument to
2005 // another function.
2006 Fn->setLinkage(CurFun.Linkage);
2007 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002008 Fn->setCallingConv($1);
2009 Fn->setAlignment($8);
2010 if ($7) {
2011 Fn->setSection($7);
2012 free($7);
2013 }
2014
2015 // Add all of the arguments we parsed to the function...
2016 if ($5) { // Is null if empty...
2017 if (isVarArg) { // Nuke the last entry
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002018 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0&&
Reid Spencere2c32da2006-12-03 05:46:11 +00002019 "Not a varargs marker!");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002020 delete $5->back().Ty;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002021 $5->pop_back(); // Delete the last entry
2022 }
2023 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002024 unsigned Idx = 1;
2025 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I, ++ArgIt) {
2026 delete I->Ty; // Delete the typeholder...
2027 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer309080a2006-09-28 19:28:24 +00002028 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002029 InsertValue(ArgIt);
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002030 Idx++;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002031 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002032
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002033 delete $5; // We're now done with the argument list
2034 }
Reid Spencer713eedc2006-08-18 08:43:06 +00002035 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002036};
2037
2038BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2039
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002040FunctionHeader : FunctionDefineLinkage FunctionHeaderH BEGIN {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002041 $$ = CurFun.CurrentFunction;
2042
2043 // Make sure that we keep track of the linkage type even if there was a
2044 // previous "declare".
2045 $$->setLinkage($1);
2046};
2047
2048END : ENDTOK | '}'; // Allow end of '}' to end a function
2049
2050Function : BasicBlockList END {
2051 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002052 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002053};
2054
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002055FunctionProto : FunctionDeclareLinkage FunctionHeaderH {
2056 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002057 $$ = CurFun.CurrentFunction;
2058 CurFun.FunctionDone();
2059 CHECK_FOR_ERROR
2060 };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002061
2062//===----------------------------------------------------------------------===//
2063// Rules to match Basic Blocks
2064//===----------------------------------------------------------------------===//
2065
2066OptSideEffect : /* empty */ {
2067 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002068 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002069 }
2070 | SIDEEFFECT {
2071 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002072 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002073 };
2074
2075ConstValueRef : ESINT64VAL { // A reference to a direct constant
2076 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002077 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002078 }
2079 | EUINT64VAL {
2080 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002081 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002082 }
2083 | FPVAL { // Perhaps it's an FP constant?
2084 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002085 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002086 }
2087 | TRUETOK {
Chris Lattner6ab03f62006-09-28 23:35:22 +00002088 $$ = ValID::create(ConstantBool::getTrue());
Reid Spencer713eedc2006-08-18 08:43:06 +00002089 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002090 }
2091 | FALSETOK {
Chris Lattner6ab03f62006-09-28 23:35:22 +00002092 $$ = ValID::create(ConstantBool::getFalse());
Reid Spencer713eedc2006-08-18 08:43:06 +00002093 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002094 }
2095 | NULL_TOK {
2096 $$ = ValID::createNull();
Reid Spencer713eedc2006-08-18 08:43:06 +00002097 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002098 }
2099 | UNDEF {
2100 $$ = ValID::createUndef();
Reid Spencer713eedc2006-08-18 08:43:06 +00002101 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002102 }
2103 | ZEROINITIALIZER { // A vector zero constant.
2104 $$ = ValID::createZeroInit();
Reid Spencer713eedc2006-08-18 08:43:06 +00002105 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002106 }
2107 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencere2c32da2006-12-03 05:46:11 +00002108 const Type *ETy = (*$2)[0]->getType();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002109 int NumElements = $2->size();
2110
2111 PackedType* pt = PackedType::get(ETy, NumElements);
2112 PATypeHolder* PTy = new PATypeHolder(
Reid Spencere2c32da2006-12-03 05:46:11 +00002113 HandleUpRefs(
2114 PackedType::get(
2115 ETy,
2116 NumElements)
2117 )
2118 );
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002119
2120 // Verify all elements are correct type!
2121 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencere2c32da2006-12-03 05:46:11 +00002122 if (ETy != (*$2)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002123 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002124 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00002125 (*$2)[i]->getType()->getDescription() + "'.");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002126 }
2127
Reid Spencere2c32da2006-12-03 05:46:11 +00002128 $$ = ValID::create(ConstantPacked::get(pt, *$2));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002129 delete PTy; delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002130 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002131 }
2132 | ConstExpr {
Reid Spencere2c32da2006-12-03 05:46:11 +00002133 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002134 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002135 }
2136 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
2137 char *End = UnEscapeLexed($3, true);
2138 std::string AsmStr = std::string($3, End);
2139 End = UnEscapeLexed($5, true);
2140 std::string Constraints = std::string($5, End);
2141 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
2142 free($3);
2143 free($5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002144 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002145 };
2146
2147// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2148// another value.
2149//
2150SymbolicValueRef : INTVAL { // Is it an integer reference...?
2151 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002152 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002153 }
2154 | Name { // Is it a named reference...?
2155 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002156 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002157 };
2158
2159// ValueRef - A reference to a definition... either constant or symbolic
2160ValueRef : SymbolicValueRef | ConstValueRef;
2161
2162
2163// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2164// type immediately preceeds the value reference, and allows complex constant
2165// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2166ResolvedVal : Types ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002167 if (!UpRefs.empty())
2168 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2169 $$ = getVal(*$1, $2);
2170 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002171 CHECK_FOR_ERROR
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002172 }
2173 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002174
2175BasicBlockList : BasicBlockList BasicBlock {
2176 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002177 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002178 }
2179 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
2180 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002181 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002182 };
2183
2184
2185// Basic blocks are terminated by branching instructions:
2186// br, br/cc, switch, ret
2187//
2188BasicBlock : InstructionList OptAssign BBTerminatorInst {
2189 setValueName($3, $2);
Reid Spencer309080a2006-09-28 19:28:24 +00002190 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002191 InsertValue($3);
2192
2193 $1->getInstList().push_back($3);
2194 InsertValue($1);
2195 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002196 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002197 };
2198
2199InstructionList : InstructionList Inst {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002200 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2201 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2202 if (CI2->getParent() == 0)
2203 $1->getInstList().push_back(CI2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002204 $1->getInstList().push_back($2);
2205 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002206 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002207 }
2208 | /* empty */ {
Reid Spencer27642192006-12-05 23:29:42 +00002209 $$ = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Reid Spencer309080a2006-09-28 19:28:24 +00002210 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002211
2212 // Make sure to move the basic block to the correct location in the
2213 // function, instead of leaving it inserted wherever it was first
2214 // referenced.
2215 Function::BasicBlockListType &BBL =
2216 CurFun.CurrentFunction->getBasicBlockList();
2217 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer713eedc2006-08-18 08:43:06 +00002218 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002219 }
2220 | LABELSTR {
Reid Spencer27642192006-12-05 23:29:42 +00002221 $$ = getBBVal(ValID::create($1), true);
Reid Spencer309080a2006-09-28 19:28:24 +00002222 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002223
2224 // Make sure to move the basic block to the correct location in the
2225 // function, instead of leaving it inserted wherever it was first
2226 // referenced.
2227 Function::BasicBlockListType &BBL =
2228 CurFun.CurrentFunction->getBasicBlockList();
2229 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer713eedc2006-08-18 08:43:06 +00002230 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002231 };
2232
2233BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencere2c32da2006-12-03 05:46:11 +00002234 $$ = new ReturnInst($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00002235 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002236 }
2237 | RET VOID { // Return with no result...
2238 $$ = new ReturnInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002239 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002240 }
2241 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer309080a2006-09-28 19:28:24 +00002242 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002243 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002244 $$ = new BranchInst(tmpBB);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002245 } // Conditional Branch...
2246 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencer309080a2006-09-28 19:28:24 +00002247 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002248 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002249 BasicBlock* tmpBBB = getBBVal($9);
2250 CHECK_FOR_ERROR
2251 Value* tmpVal = getVal(Type::BoolTy, $3);
2252 CHECK_FOR_ERROR
2253 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002254 }
2255 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencere2c32da2006-12-03 05:46:11 +00002256 Value* tmpVal = getVal($2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002257 CHECK_FOR_ERROR
2258 BasicBlock* tmpBB = getBBVal($6);
2259 CHECK_FOR_ERROR
2260 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002261 $$ = S;
2262
2263 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2264 E = $8->end();
2265 for (; I != E; ++I) {
2266 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2267 S->addCase(CI, I->second);
2268 else
Reid Spencer713eedc2006-08-18 08:43:06 +00002269 GEN_ERROR("Switch case is constant, but not a simple integer!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002270 }
2271 delete $8;
Reid Spencer713eedc2006-08-18 08:43:06 +00002272 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002273 }
2274 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencere2c32da2006-12-03 05:46:11 +00002275 Value* tmpVal = getVal($2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002276 CHECK_FOR_ERROR
2277 BasicBlock* tmpBB = getBBVal($6);
2278 CHECK_FOR_ERROR
2279 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002280 $$ = S;
Reid Spencer713eedc2006-08-18 08:43:06 +00002281 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002282 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002283 | INVOKE OptCallingConv ResultType ValueRef '(' ValueRefList ')'
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002284 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002285
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002286 // Handle the short syntax
2287 const PointerType *PFTy = 0;
2288 const FunctionType *Ty = 0;
2289 if (!(PFTy = dyn_cast<PointerType>($3.Ty->get())) ||
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002290 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2291 // Pull out the types of all of the arguments...
2292 std::vector<const Type*> ParamTypes;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002293 FunctionType::ParamAttrsList ParamAttrs;
2294 ParamAttrs.push_back($3.Attrs);
2295 for (ValueRefList::iterator I = $6->begin(), E = $6->end(); I != E; ++I) {
2296 const Type *Ty = I->Val->getType();
2297 if (Ty == Type::VoidTy)
2298 GEN_ERROR("Short call syntax cannot be used with varargs");
2299 ParamTypes.push_back(Ty);
2300 ParamAttrs.push_back(I->Attrs);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002301 }
2302
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002303 Ty = FunctionType::get($3.Ty->get(), ParamTypes, false, ParamAttrs);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002304 PFTy = PointerType::get(Ty);
2305 }
2306
2307 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer309080a2006-09-28 19:28:24 +00002308 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002309 BasicBlock *Normal = getBBVal($10);
Reid Spencer309080a2006-09-28 19:28:24 +00002310 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002311 BasicBlock *Except = getBBVal($13);
Reid Spencer309080a2006-09-28 19:28:24 +00002312 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002313
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002314 // Check the arguments
2315 ValueList Args;
2316 if ($6->empty()) { // Has no arguments?
2317 // Make sure no arguments is a good thing!
2318 if (Ty->getNumParams() != 0)
2319 GEN_ERROR("No arguments passed to a function that "
2320 "expects arguments!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002321 } else { // Has arguments?
2322 // Loop through FunctionType's arguments and ensure they are specified
2323 // correctly!
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002324 FunctionType::param_iterator I = Ty->param_begin();
2325 FunctionType::param_iterator E = Ty->param_end();
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002326 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002327
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002328 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2329 if (ArgI->Val->getType() != *I)
2330 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00002331 (*I)->getDescription() + "'!");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002332 Args.push_back(ArgI->Val);
2333 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002334
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002335 if (Ty->isVarArg()) {
2336 if (I == E)
2337 for (; ArgI != ArgE; ++ArgI)
2338 Args.push_back(ArgI->Val); // push the remaining varargs
2339 } else if (I != E || ArgI != ArgE)
Reid Spencere2c32da2006-12-03 05:46:11 +00002340 GEN_ERROR("Invalid number of parameters detected!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002341 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002342
2343 // Create the InvokeInst
2344 InvokeInst *II = new InvokeInst(V, Normal, Except, Args);
2345 II->setCallingConv($2);
2346 $$ = II;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002347 delete $6;
Reid Spencer713eedc2006-08-18 08:43:06 +00002348 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002349 }
2350 | UNWIND {
2351 $$ = new UnwindInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002352 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002353 }
2354 | UNREACHABLE {
2355 $$ = new UnreachableInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002356 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002357 };
2358
2359
2360
2361JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2362 $$ = $1;
Reid Spencere2c32da2006-12-03 05:46:11 +00002363 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Reid Spencer309080a2006-09-28 19:28:24 +00002364 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002365 if (V == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002366 GEN_ERROR("May only switch on a constant pool value!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002367
Reid Spencer309080a2006-09-28 19:28:24 +00002368 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002369 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002370 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002371 }
2372 | IntType ConstValueRef ',' LABEL ValueRef {
2373 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencere2c32da2006-12-03 05:46:11 +00002374 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Reid Spencer309080a2006-09-28 19:28:24 +00002375 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002376
2377 if (V == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002378 GEN_ERROR("May only switch on a constant pool value!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002379
Reid Spencer309080a2006-09-28 19:28:24 +00002380 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002381 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002382 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002383 };
2384
2385Inst : OptAssign InstVal {
2386 // Is this definition named?? if so, assign the name...
2387 setValueName($2, $1);
Reid Spencer309080a2006-09-28 19:28:24 +00002388 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002389 InsertValue($2);
2390 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002391 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002392};
2393
2394PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002395 if (!UpRefs.empty())
2396 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002397 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencere2c32da2006-12-03 05:46:11 +00002398 Value* tmpVal = getVal(*$1, $3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002399 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002400 BasicBlock* tmpBB = getBBVal($5);
2401 CHECK_FOR_ERROR
2402 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencere2c32da2006-12-03 05:46:11 +00002403 delete $1;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002404 }
2405 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2406 $$ = $1;
Reid Spencer309080a2006-09-28 19:28:24 +00002407 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002408 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002409 BasicBlock* tmpBB = getBBVal($6);
2410 CHECK_FOR_ERROR
2411 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002412 };
2413
2414
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002415ValueRefList : Types ValueRef OptParamAttrs {
2416 if (!UpRefs.empty())
2417 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2418 // Used for call and invoke instructions
2419 $$ = new ValueRefList();
2420 ValueRefListEntry E; E.Attrs = $3; E.Val = getVal($1->get(), $2);
2421 $$->push_back(E);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002422 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002423 | ValueRefList ',' Types ValueRef OptParamAttrs {
2424 if (!UpRefs.empty())
2425 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002426 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002427 ValueRefListEntry E; E.Attrs = $5; E.Val = getVal($3->get(), $4);
2428 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002429 CHECK_FOR_ERROR
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002430 }
2431 | /*empty*/ { $$ = new ValueRefList(); };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002432
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002433IndexList // Used for gep instructions and constant expressions
2434 : /*empty*/ { $$ = new std::vector<Value*>(); };
2435 | IndexList ',' ResolvedVal {
2436 $$ = $1;
2437 $$->push_back($3);
2438 CHECK_FOR_ERROR
2439 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002440
2441OptTailCall : TAIL CALL {
2442 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002443 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002444 }
2445 | CALL {
2446 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002447 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002448 };
2449
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002450InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002451 if (!UpRefs.empty())
2452 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002453 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
2454 !isa<PackedType>((*$2).get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002455 GEN_ERROR(
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002456 "Arithmetic operator requires integer, FP, or packed operands!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002457 if (isa<PackedType>((*$2).get()) &&
2458 ($1 == Instruction::URem ||
2459 $1 == Instruction::SRem ||
2460 $1 == Instruction::FRem))
Reid Spencerde46e482006-11-02 20:25:50 +00002461 GEN_ERROR("U/S/FRem not supported on packed types!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002462 Value* val1 = getVal(*$2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002463 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002464 Value* val2 = getVal(*$2, $5);
Reid Spencer309080a2006-09-28 19:28:24 +00002465 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002466 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002467 if ($$ == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002468 GEN_ERROR("binary operator returned null!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002469 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002470 }
2471 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002472 if (!UpRefs.empty())
2473 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002474 if (!(*$2)->isIntegral()) {
2475 if (!isa<PackedType>($2->get()) ||
2476 !cast<PackedType>($2->get())->getElementType()->isIntegral())
Reid Spencer713eedc2006-08-18 08:43:06 +00002477 GEN_ERROR("Logical operator requires integral operands!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002478 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002479 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002480 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002481 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer309080a2006-09-28 19:28:24 +00002482 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002483 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002484 if ($$ == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002485 GEN_ERROR("binary operator returned null!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002486 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002487 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002488 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002489 if (!UpRefs.empty())
2490 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002491 if (isa<PackedType>((*$3).get()))
2492 GEN_ERROR("Packed types not supported by icmp instruction");
2493 Value* tmpVal1 = getVal(*$3, $4);
2494 CHECK_FOR_ERROR
2495 Value* tmpVal2 = getVal(*$3, $6);
2496 CHECK_FOR_ERROR
2497 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2498 if ($$ == 0)
2499 GEN_ERROR("icmp operator returned null!");
2500 }
2501 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002502 if (!UpRefs.empty())
2503 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002504 if (isa<PackedType>((*$3).get()))
2505 GEN_ERROR("Packed types not supported by fcmp instruction");
2506 Value* tmpVal1 = getVal(*$3, $4);
2507 CHECK_FOR_ERROR
2508 Value* tmpVal2 = getVal(*$3, $6);
2509 CHECK_FOR_ERROR
2510 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2511 if ($$ == 0)
2512 GEN_ERROR("fcmp operator returned null!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002513 }
2514 | NOT ResolvedVal {
Bill Wendlingf3baad32006-12-07 01:30:32 +00002515 cerr << "WARNING: Use of eliminated 'not' instruction:"
2516 << " Replacing with 'xor'.\n";
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002517
Reid Spencere2c32da2006-12-03 05:46:11 +00002518 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002519 if (Ones == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002520 GEN_ERROR("Expected integral type for not instruction!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002521
Reid Spencere2c32da2006-12-03 05:46:11 +00002522 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002523 if ($$ == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002524 GEN_ERROR("Could not create a xor instruction!");
2525 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002526 }
2527 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002528 if ($4->getType() != Type::Int8Ty)
2529 GEN_ERROR("Shift amount must be i8 type!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002530 if (!$2->getType()->isInteger())
Reid Spencer713eedc2006-08-18 08:43:06 +00002531 GEN_ERROR("Shift constant expression requires integer operand!");
Reid Spencerfdff9382006-11-08 06:47:33 +00002532 CHECK_FOR_ERROR;
Reid Spencere2c32da2006-12-03 05:46:11 +00002533 $$ = new ShiftInst($1, $2, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002534 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002535 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002536 | CastOps ResolvedVal TO Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002537 if (!UpRefs.empty())
2538 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002539 Value* Val = $2;
2540 const Type* Ty = $4->get();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002541 if (!Val->getType()->isFirstClassType())
2542 GEN_ERROR("cast from a non-primitive type: '" +
2543 Val->getType()->getDescription() + "'!");
2544 if (!Ty->isFirstClassType())
2545 GEN_ERROR("cast to a non-primitive type: '" + Ty->getDescription() +"'!");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002546 $$ = CastInst::create($1, Val, $4->get());
Reid Spencere2c32da2006-12-03 05:46:11 +00002547 delete $4;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002548 }
2549 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002550 if ($2->getType() != Type::BoolTy)
Reid Spencer713eedc2006-08-18 08:43:06 +00002551 GEN_ERROR("select condition must be boolean!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002552 if ($4->getType() != $6->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002553 GEN_ERROR("select value types should match!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002554 $$ = new SelectInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002555 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002556 }
2557 | VAARG ResolvedVal ',' Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002558 if (!UpRefs.empty())
2559 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002560 $$ = new VAArgInst($2, *$4);
2561 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00002562 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002563 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002564 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002565 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencer713eedc2006-08-18 08:43:06 +00002566 GEN_ERROR("Invalid extractelement operands!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002567 $$ = new ExtractElementInst($2, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002568 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002569 }
2570 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002571 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencer713eedc2006-08-18 08:43:06 +00002572 GEN_ERROR("Invalid insertelement operands!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002573 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002574 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002575 }
Chris Lattner9ff96a72006-04-08 01:18:56 +00002576 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002577 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencer713eedc2006-08-18 08:43:06 +00002578 GEN_ERROR("Invalid shufflevector operands!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002579 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002580 CHECK_FOR_ERROR
Chris Lattner9ff96a72006-04-08 01:18:56 +00002581 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002582 | PHI_TOK PHIList {
2583 const Type *Ty = $2->front().first->getType();
2584 if (!Ty->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002585 GEN_ERROR("PHI node operands must be of first class type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002586 $$ = new PHINode(Ty);
2587 ((PHINode*)$$)->reserveOperandSpace($2->size());
2588 while ($2->begin() != $2->end()) {
2589 if ($2->front().first->getType() != Ty)
Reid Spencer713eedc2006-08-18 08:43:06 +00002590 GEN_ERROR("All elements of a PHI node must be of the same type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002591 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
2592 $2->pop_front();
2593 }
2594 delete $2; // Free the list...
Reid Spencer713eedc2006-08-18 08:43:06 +00002595 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002596 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002597 | OptTailCall OptCallingConv ResultType ValueRef '(' ValueRefList ')' {
2598
2599 // Handle the short syntax
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002600 const PointerType *PFTy = 0;
2601 const FunctionType *Ty = 0;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002602 if (!(PFTy = dyn_cast<PointerType>($3.Ty->get())) ||
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002603 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2604 // Pull out the types of all of the arguments...
2605 std::vector<const Type*> ParamTypes;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002606 FunctionType::ParamAttrsList ParamAttrs;
2607 ParamAttrs.push_back($3.Attrs);
2608 for (ValueRefList::iterator I = $6->begin(), E = $6->end(); I != E; ++I) {
2609 const Type *Ty = I->Val->getType();
2610 if (Ty == Type::VoidTy)
2611 GEN_ERROR("Short call syntax cannot be used with varargs");
2612 ParamTypes.push_back(Ty);
2613 ParamAttrs.push_back(I->Attrs);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002614 }
2615
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002616 Ty = FunctionType::get($3.Ty->get(), ParamTypes, false, ParamAttrs);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002617 PFTy = PointerType::get(Ty);
2618 }
2619
2620 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer309080a2006-09-28 19:28:24 +00002621 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002622
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002623 // Check the arguments
2624 ValueList Args;
2625 if ($6->empty()) { // Has no arguments?
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002626 // Make sure no arguments is a good thing!
2627 if (Ty->getNumParams() != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002628 GEN_ERROR("No arguments passed to a function that "
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002629 "expects arguments!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002630 } else { // Has arguments?
2631 // Loop through FunctionType's arguments and ensure they are specified
2632 // correctly!
2633 //
2634 FunctionType::param_iterator I = Ty->param_begin();
2635 FunctionType::param_iterator E = Ty->param_end();
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002636 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002637
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002638 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2639 if (ArgI->Val->getType() != *I)
2640 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00002641 (*I)->getDescription() + "'!");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002642 Args.push_back(ArgI->Val);
2643 }
2644 if (Ty->isVarArg()) {
2645 if (I == E)
2646 for (; ArgI != ArgE; ++ArgI)
2647 Args.push_back(ArgI->Val); // push the remaining varargs
2648 } else if (I != E || ArgI != ArgE)
Reid Spencer713eedc2006-08-18 08:43:06 +00002649 GEN_ERROR("Invalid number of parameters detected!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002650 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002651 // Create the call node
2652 CallInst *CI = new CallInst(V, Args);
2653 CI->setTailCall($1);
2654 CI->setCallingConv($2);
2655 $$ = CI;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002656 delete $6;
Reid Spencer713eedc2006-08-18 08:43:06 +00002657 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002658 }
2659 | MemoryInst {
2660 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002661 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002662 };
2663
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002664OptVolatile : VOLATILE {
2665 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002666 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002667 }
2668 | /* empty */ {
2669 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002670 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002671 };
2672
2673
2674
2675MemoryInst : MALLOC Types OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002676 if (!UpRefs.empty())
2677 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002678 $$ = new MallocInst(*$2, 0, $3);
2679 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002680 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002681 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002682 | MALLOC Types ',' INT32 ValueRef OptCAlign {
2683 if (!UpRefs.empty())
2684 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002685 Value* tmpVal = getVal($4, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002686 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002687 $$ = new MallocInst(*$2, tmpVal, $6);
2688 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002689 }
2690 | ALLOCA Types OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002691 if (!UpRefs.empty())
2692 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002693 $$ = new AllocaInst(*$2, 0, $3);
2694 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002695 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002696 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002697 | ALLOCA Types ',' INT32 ValueRef OptCAlign {
2698 if (!UpRefs.empty())
2699 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002700 Value* tmpVal = getVal($4, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002701 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002702 $$ = new AllocaInst(*$2, tmpVal, $6);
2703 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002704 }
2705 | FREE ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002706 if (!isa<PointerType>($2->getType()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002707 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencere2c32da2006-12-03 05:46:11 +00002708 $2->getType()->getDescription() + "!");
2709 $$ = new FreeInst($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00002710 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002711 }
2712
2713 | OptVolatile LOAD Types ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002714 if (!UpRefs.empty())
2715 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002716 if (!isa<PointerType>($3->get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002717 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencere2c32da2006-12-03 05:46:11 +00002718 (*$3)->getDescription());
2719 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002720 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencere2c32da2006-12-03 05:46:11 +00002721 (*$3)->getDescription());
2722 Value* tmpVal = getVal(*$3, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002723 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002724 $$ = new LoadInst(tmpVal, "", $1);
Reid Spencere2c32da2006-12-03 05:46:11 +00002725 delete $3;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002726 }
2727 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002728 if (!UpRefs.empty())
2729 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002730 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002731 if (!PT)
Reid Spencer713eedc2006-08-18 08:43:06 +00002732 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencere2c32da2006-12-03 05:46:11 +00002733 (*$5)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002734 const Type *ElTy = PT->getElementType();
Reid Spencere2c32da2006-12-03 05:46:11 +00002735 if (ElTy != $3->getType())
2736 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002737 "' into space of type '" + ElTy->getDescription() + "'!");
2738
Reid Spencere2c32da2006-12-03 05:46:11 +00002739 Value* tmpVal = getVal(*$5, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002740 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002741 $$ = new StoreInst($3, tmpVal, $1);
2742 delete $5;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002743 }
2744 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002745 if (!UpRefs.empty())
2746 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002747 if (!isa<PointerType>($2->get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002748 GEN_ERROR("getelementptr insn requires pointer operand!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002749
Reid Spencere2c32da2006-12-03 05:46:11 +00002750 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Reid Spencer713eedc2006-08-18 08:43:06 +00002751 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00002752 (*$2)->getDescription()+ "'!");
2753 Value* tmpVal = getVal(*$2, $3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002754 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002755 $$ = new GetElementPtrInst(tmpVal, *$4);
2756 delete $2;
Reid Spencer309080a2006-09-28 19:28:24 +00002757 delete $4;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002758 };
2759
2760
2761%%
Reid Spencer713eedc2006-08-18 08:43:06 +00002762
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002763// common code from the two 'RunVMAsmParser' functions
2764static Module* RunParser(Module * M) {
2765
2766 llvmAsmlineno = 1; // Reset the current line number...
2767 CurModule.CurrentModule = M;
2768#if YYDEBUG
2769 yydebug = Debug;
2770#endif
2771
2772 // Check to make sure the parser succeeded
2773 if (yyparse()) {
2774 if (ParserResult)
2775 delete ParserResult;
2776 return 0;
2777 }
2778
2779 // Check to make sure that parsing produced a result
2780 if (!ParserResult)
2781 return 0;
2782
2783 // Reset ParserResult variable while saving its value for the result.
2784 Module *Result = ParserResult;
2785 ParserResult = 0;
2786
2787 return Result;
2788}
2789
Reid Spencer713eedc2006-08-18 08:43:06 +00002790void llvm::GenerateError(const std::string &message, int LineNo) {
2791 if (LineNo == -1) LineNo = llvmAsmlineno;
2792 // TODO: column number in exception
2793 if (TheParseError)
2794 TheParseError->setError(CurFilename, message, LineNo);
2795 TriggerError = 1;
2796}
2797
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002798int yyerror(const char *ErrorMsg) {
2799 std::string where
2800 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2801 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
2802 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
2803 if (yychar == YYEMPTY || yychar == 0)
2804 errMsg += "end-of-file.";
2805 else
2806 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Reid Spencer713eedc2006-08-18 08:43:06 +00002807 GenerateError(errMsg);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002808 return 0;
2809}