blob: 15524baf8625f0cc5b12417cbc3b5edf99f04603 [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"
22#include "llvm/ADT/STLExtras.h"
23#include "llvm/Support/MathExtras.h"
Reid Spencerd5e19442006-12-01 00:33:46 +000024#include "llvm/Support/Streams.h"
Chris Lattnerf20e61f2006-02-15 07:22:58 +000025#include <algorithm>
Chris Lattnerf20e61f2006-02-15 07:22:58 +000026#include <list>
27#include <utility>
28
Reid Spencerb50974a2006-08-18 17:32:55 +000029// The following is a gross hack. In order to rid the libAsmParser library of
30// exceptions, we have to have a way of getting the yyparse function to go into
31// an error situation. So, whenever we want an error to occur, the GenerateError
32// function (see bottom of file) sets TriggerError. Then, at the end of each
33// production in the grammer we use CHECK_FOR_ERROR which will invoke YYERROR
34// (a goto) to put YACC in error state. Furthermore, several calls to
35// GenerateError are made from inside productions and they must simulate the
36// previous exception behavior by exiting the production immediately. We have
37// replaced these with the GEN_ERROR macro which calls GeneratError and then
38// immediately invokes YYERROR. This would be so much cleaner if it was a
39// recursive descent parser.
Reid Spencer713eedc2006-08-18 08:43:06 +000040static bool TriggerError = false;
Reid Spencerff359002006-10-09 17:36:59 +000041#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYABORT; } }
Reid Spencer713eedc2006-08-18 08:43:06 +000042#define GEN_ERROR(msg) { GenerateError(msg); YYERROR; }
43
Chris Lattnerf20e61f2006-02-15 07:22:58 +000044int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
45int yylex(); // declaration" of xxx warnings.
46int yyparse();
47
48namespace llvm {
49 std::string CurFilename;
50}
51using namespace llvm;
52
53static Module *ParserResult;
54
55// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
56// relating to upreferences in the input stream.
57//
58//#define DEBUG_UPREFS 1
59#ifdef DEBUG_UPREFS
Bill Wendlingf3baad32006-12-07 01:30:32 +000060#define UR_OUT(X) cerr << X
Chris Lattnerf20e61f2006-02-15 07:22:58 +000061#else
62#define UR_OUT(X)
63#endif
64
65#define YYERROR_VERBOSE 1
66
Chris Lattnerf20e61f2006-02-15 07:22:58 +000067static GlobalVariable *CurGV;
68
69
70// This contains info used when building the body of a function. It is
71// destroyed when the function is completed.
72//
73typedef std::vector<Value *> ValueList; // Numbered defs
74static void
75ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
76 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
77
78static struct PerModuleInfo {
79 Module *CurrentModule;
80 std::map<const Type *, ValueList> Values; // Module level numbered definitions
81 std::map<const Type *,ValueList> LateResolveValues;
Reid Spencer55f1fbe2006-11-28 07:29:44 +000082 std::vector<PATypeHolder> Types;
83 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattnerf20e61f2006-02-15 07:22:58 +000084
85 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Chris Lattner7aa45902006-06-21 16:53:00 +000086 /// how they were referenced and on which line of the input they came from so
Chris Lattnerf20e61f2006-02-15 07:22:58 +000087 /// that we can resolve them later and print error messages as appropriate.
88 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
89
90 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
91 // references to global values. Global values may be referenced before they
92 // are defined, and if so, the temporary object that they represent is held
93 // here. This is used for forward references of GlobalValues.
94 //
95 typedef std::map<std::pair<const PointerType *,
96 ValID>, GlobalValue*> GlobalRefsType;
97 GlobalRefsType GlobalRefs;
98
99 void ModuleDone() {
100 // If we could not resolve some functions at function compilation time
101 // (calls to functions before they are defined), resolve them now... Types
102 // are resolved when the constant pool has been completely parsed.
103 //
104 ResolveDefinitions(LateResolveValues);
Reid Spencer309080a2006-09-28 19:28:24 +0000105 if (TriggerError)
106 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000107
108 // Check to make sure that all global value forward references have been
109 // resolved!
110 //
111 if (!GlobalRefs.empty()) {
112 std::string UndefinedReferences = "Unresolved global references exist:\n";
113
114 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
115 I != E; ++I) {
116 UndefinedReferences += " " + I->first.first->getDescription() + " " +
117 I->first.second.getName() + "\n";
118 }
Reid Spencer713eedc2006-08-18 08:43:06 +0000119 GenerateError(UndefinedReferences);
Reid Spencer309080a2006-09-28 19:28:24 +0000120 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000121 }
122
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000123 Values.clear(); // Clear out function local definitions
124 Types.clear();
125 CurrentModule = 0;
126 }
127
128 // GetForwardRefForGlobal - Check to see if there is a forward reference
129 // for this global. If so, remove it from the GlobalRefs map and return it.
130 // If not, just return null.
131 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
132 // Check to see if there is a forward reference to this global variable...
133 // if there is, eliminate it and patch the reference to use the new def'n.
134 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
135 GlobalValue *Ret = 0;
136 if (I != GlobalRefs.end()) {
137 Ret = I->second;
138 GlobalRefs.erase(I);
139 }
140 return Ret;
141 }
142} CurModule;
143
144static struct PerFunctionInfo {
145 Function *CurrentFunction; // Pointer to current function being created
146
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000147 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000148 std::map<const Type*, ValueList> LateResolveValues;
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000149 bool isDeclare; // Is this function a forward declararation?
150 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000151
152 /// BBForwardRefs - When we see forward references to basic blocks, keep
153 /// track of them here.
154 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
155 std::vector<BasicBlock*> NumberedBlocks;
156 unsigned NextBBNum;
157
158 inline PerFunctionInfo() {
159 CurrentFunction = 0;
160 isDeclare = false;
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000161 Linkage = GlobalValue::ExternalLinkage;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000162 }
163
164 inline void FunctionStart(Function *M) {
165 CurrentFunction = M;
166 NextBBNum = 0;
167 }
168
169 void FunctionDone() {
170 NumberedBlocks.clear();
171
172 // Any forward referenced blocks left?
Reid Spencer309080a2006-09-28 19:28:24 +0000173 if (!BBForwardRefs.empty()) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000174 GenerateError("Undefined reference to label " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000175 BBForwardRefs.begin()->first->getName());
Reid Spencer309080a2006-09-28 19:28:24 +0000176 return;
177 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000178
179 // Resolve all forward references now.
180 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
181
182 Values.clear(); // Clear out function local definitions
183 CurrentFunction = 0;
184 isDeclare = false;
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000185 Linkage = GlobalValue::ExternalLinkage;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000186 }
187} CurFun; // Info for the current function...
188
189static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
190
191
192//===----------------------------------------------------------------------===//
193// Code to handle definitions of all the types
194//===----------------------------------------------------------------------===//
195
196static int InsertValue(Value *V,
197 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
198 if (V->hasName()) return -1; // Is this a numbered definition?
199
200 // Yes, insert the value into the value table...
201 ValueList &List = ValueTab[V->getType()];
202 List.push_back(V);
203 return List.size()-1;
204}
205
206static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
207 switch (D.Type) {
208 case ValID::NumberVal: // Is it a numbered definition?
209 // Module constants occupy the lowest numbered slots...
210 if ((unsigned)D.Num < CurModule.Types.size())
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000211 return CurModule.Types[(unsigned)D.Num];
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000212 break;
213 case ValID::NameVal: // Is it a named definition?
214 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
215 D.destroy(); // Free old strdup'd memory...
216 return N;
217 }
218 break;
219 default:
Reid Spencer713eedc2006-08-18 08:43:06 +0000220 GenerateError("Internal parser error: Invalid symbol type reference!");
Reid Spencer309080a2006-09-28 19:28:24 +0000221 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000222 }
223
224 // If we reached here, we referenced either a symbol that we don't know about
225 // or an id number that hasn't been read yet. We may be referencing something
226 // forward, so just create an entry to be resolved later and get to it...
227 //
228 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
229
230
231 if (inFunctionScope()) {
Reid Spencer309080a2006-09-28 19:28:24 +0000232 if (D.Type == ValID::NameVal) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000233 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer309080a2006-09-28 19:28:24 +0000234 return 0;
235 } else {
Reid Spencer713eedc2006-08-18 08:43:06 +0000236 GenerateError("Reference to an undefined type: #" + itostr(D.Num));
Reid Spencer309080a2006-09-28 19:28:24 +0000237 return 0;
238 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000239 }
240
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000241 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000242 if (I != CurModule.LateResolveTypes.end())
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000243 return I->second;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000244
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000245 Type *Typ = OpaqueType::get();
246 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
247 return Typ;
Reid Spencere2c32da2006-12-03 05:46:11 +0000248 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000249
250static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
251 SymbolTable &SymTab =
252 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
253 CurModule.CurrentModule->getSymbolTable();
254 return SymTab.lookup(Ty, Name);
255}
256
257// getValNonImprovising - Look up the value specified by the provided type and
258// the provided ValID. If the value exists and has already been defined, return
259// it. Otherwise return null.
260//
261static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Reid Spencer309080a2006-09-28 19:28:24 +0000262 if (isa<FunctionType>(Ty)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000263 GenerateError("Functions are not values and "
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000264 "must be referenced as pointers");
Reid Spencer309080a2006-09-28 19:28:24 +0000265 return 0;
266 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000267
268 switch (D.Type) {
269 case ValID::NumberVal: { // Is it a numbered definition?
270 unsigned Num = (unsigned)D.Num;
271
272 // Module constants occupy the lowest numbered slots...
273 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
274 if (VI != CurModule.Values.end()) {
275 if (Num < VI->second.size())
276 return VI->second[Num];
277 Num -= VI->second.size();
278 }
279
280 // Make sure that our type is within bounds
281 VI = CurFun.Values.find(Ty);
282 if (VI == CurFun.Values.end()) return 0;
283
284 // Check that the number is within bounds...
285 if (VI->second.size() <= Num) return 0;
286
287 return VI->second[Num];
288 }
289
290 case ValID::NameVal: { // Is it a named definition?
291 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
292 if (N == 0) return 0;
293
294 D.destroy(); // Free old strdup'd memory...
295 return N;
296 }
297
298 // Check to make sure that "Ty" is an integral type, and that our
299 // value will fit into the specified type...
300 case ValID::ConstSIntVal: // Is it a constant pool reference??
Reid Spencere0fc4df2006-10-20 07:07:24 +0000301 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000302 GenerateError("Signed integral constant '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000303 itostr(D.ConstPool64) + "' is invalid for type '" +
304 Ty->getDescription() + "'!");
Reid Spencer309080a2006-09-28 19:28:24 +0000305 return 0;
306 }
Reid Spencere0fc4df2006-10-20 07:07:24 +0000307 return ConstantInt::get(Ty, D.ConstPool64);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000308
309 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Reid Spencere0fc4df2006-10-20 07:07:24 +0000310 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
311 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000312 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000313 "' is invalid or out of range!");
Reid Spencer309080a2006-09-28 19:28:24 +0000314 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000315 } else { // This is really a signed reference. Transmogrify.
Reid Spencere0fc4df2006-10-20 07:07:24 +0000316 return ConstantInt::get(Ty, D.ConstPool64);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000317 }
318 } else {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000319 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000320 }
321
322 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Reid Spencer309080a2006-09-28 19:28:24 +0000323 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000324 GenerateError("FP constant invalid for type!!");
Reid Spencer309080a2006-09-28 19:28:24 +0000325 return 0;
326 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000327 return ConstantFP::get(Ty, D.ConstPoolFP);
328
329 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer309080a2006-09-28 19:28:24 +0000330 if (!isa<PointerType>(Ty)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000331 GenerateError("Cannot create a a non pointer null!");
Reid Spencer309080a2006-09-28 19:28:24 +0000332 return 0;
333 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000334 return ConstantPointerNull::get(cast<PointerType>(Ty));
335
336 case ValID::ConstUndefVal: // Is it an undef value?
337 return UndefValue::get(Ty);
338
339 case ValID::ConstZeroVal: // Is it a zero value?
340 return Constant::getNullValue(Ty);
341
342 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer309080a2006-09-28 19:28:24 +0000343 if (D.ConstantValue->getType() != Ty) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000344 GenerateError("Constant expression type different from required type!");
Reid Spencer309080a2006-09-28 19:28:24 +0000345 return 0;
346 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000347 return D.ConstantValue;
348
349 case ValID::InlineAsmVal: { // Inline asm expression
350 const PointerType *PTy = dyn_cast<PointerType>(Ty);
351 const FunctionType *FTy =
352 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer309080a2006-09-28 19:28:24 +0000353 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000354 GenerateError("Invalid type for asm constraint string!");
Reid Spencer309080a2006-09-28 19:28:24 +0000355 return 0;
356 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000357 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
358 D.IAD->HasSideEffects);
359 D.destroy(); // Free InlineAsmDescriptor.
360 return IA;
361 }
362 default:
363 assert(0 && "Unhandled case!");
364 return 0;
365 } // End of switch
366
367 assert(0 && "Unhandled case!");
368 return 0;
369}
370
371// getVal - This function is identical to getValNonImprovising, except that if a
372// value is not already defined, it "improvises" by creating a placeholder var
373// that looks and acts just like the requested variable. When the value is
374// defined later, all uses of the placeholder variable are replaced with the
375// real thing.
376//
377static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer309080a2006-09-28 19:28:24 +0000378 if (Ty == Type::LabelTy) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000379 GenerateError("Cannot use a basic block here");
Reid Spencer309080a2006-09-28 19:28:24 +0000380 return 0;
381 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000382
383 // See if the value has already been defined.
384 Value *V = getValNonImprovising(Ty, ID);
385 if (V) return V;
Reid Spencer309080a2006-09-28 19:28:24 +0000386 if (TriggerError) return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000387
Reid Spencer309080a2006-09-28 19:28:24 +0000388 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000389 GenerateError("Invalid use of a composite type!");
Reid Spencer309080a2006-09-28 19:28:24 +0000390 return 0;
391 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000392
393 // If we reached here, we referenced either a symbol that we don't know about
394 // or an id number that hasn't been read yet. We may be referencing something
395 // forward, so just create an entry to be resolved later and get to it...
396 //
397 V = new Argument(Ty);
398
399 // Remember where this forward reference came from. FIXME, shouldn't we try
400 // to recycle these things??
401 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
402 llvmAsmlineno)));
403
404 if (inFunctionScope())
405 InsertValue(V, CurFun.LateResolveValues);
406 else
407 InsertValue(V, CurModule.LateResolveValues);
408 return V;
409}
410
411/// getBBVal - This is used for two purposes:
412/// * If isDefinition is true, a new basic block with the specified ID is being
413/// defined.
414/// * If isDefinition is true, this is a reference to a basic block, which may
415/// or may not be a forward reference.
416///
417static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
418 assert(inFunctionScope() && "Can't get basic block at global scope!");
419
420 std::string Name;
421 BasicBlock *BB = 0;
422 switch (ID.Type) {
Reid Spencer309080a2006-09-28 19:28:24 +0000423 default:
424 GenerateError("Illegal label reference " + ID.getName());
425 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000426 case ValID::NumberVal: // Is it a numbered definition?
427 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
428 CurFun.NumberedBlocks.resize(ID.Num+1);
429 BB = CurFun.NumberedBlocks[ID.Num];
430 break;
431 case ValID::NameVal: // Is it a named definition?
432 Name = ID.Name;
433 if (Value *N = CurFun.CurrentFunction->
434 getSymbolTable().lookup(Type::LabelTy, Name))
435 BB = cast<BasicBlock>(N);
436 break;
437 }
438
439 // See if the block has already been defined.
440 if (BB) {
441 // If this is the definition of the block, make sure the existing value was
442 // just a forward reference. If it was a forward reference, there will be
443 // an entry for it in the PlaceHolderInfo map.
Reid Spencer309080a2006-09-28 19:28:24 +0000444 if (isDefinition && !CurFun.BBForwardRefs.erase(BB)) {
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000445 // The existing value was a definition, not a forward reference.
Reid Spencer713eedc2006-08-18 08:43:06 +0000446 GenerateError("Redefinition of label " + ID.getName());
Reid Spencer309080a2006-09-28 19:28:24 +0000447 return 0;
448 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000449
450 ID.destroy(); // Free strdup'd memory.
451 return BB;
452 }
453
454 // Otherwise this block has not been seen before.
455 BB = new BasicBlock("", CurFun.CurrentFunction);
456 if (ID.Type == ValID::NameVal) {
457 BB->setName(ID.Name);
458 } else {
459 CurFun.NumberedBlocks[ID.Num] = BB;
460 }
461
462 // If this is not a definition, keep track of it so we can use it as a forward
463 // reference.
464 if (!isDefinition) {
465 // Remember where this forward reference came from.
466 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
467 } else {
468 // The forward declaration could have been inserted anywhere in the
469 // function: insert it into the correct place now.
470 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
471 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
472 }
473 ID.destroy();
474 return BB;
475}
476
477
478//===----------------------------------------------------------------------===//
479// Code to handle forward references in instructions
480//===----------------------------------------------------------------------===//
481//
482// This code handles the late binding needed with statements that reference
483// values not defined yet... for example, a forward branch, or the PHI node for
484// a loop body.
485//
486// This keeps a table (CurFun.LateResolveValues) of all such forward references
487// and back patchs after we are done.
488//
489
490// ResolveDefinitions - If we could not resolve some defs at parsing
491// time (forward branches, phi functions for loops, etc...) resolve the
492// defs now...
493//
494static void
495ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
496 std::map<const Type*,ValueList> *FutureLateResolvers) {
497 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
498 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
499 E = LateResolvers.end(); LRI != E; ++LRI) {
500 ValueList &List = LRI->second;
501 while (!List.empty()) {
502 Value *V = List.back();
503 List.pop_back();
504
505 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
506 CurModule.PlaceHolderInfo.find(V);
507 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
508
509 ValID &DID = PHI->second.first;
510
511 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Reid Spencer309080a2006-09-28 19:28:24 +0000512 if (TriggerError)
513 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000514 if (TheRealValue) {
515 V->replaceAllUsesWith(TheRealValue);
516 delete V;
517 CurModule.PlaceHolderInfo.erase(PHI);
518 } else if (FutureLateResolvers) {
519 // Functions have their unresolved items forwarded to the module late
520 // resolver table
521 InsertValue(V, *FutureLateResolvers);
522 } else {
Reid Spencer309080a2006-09-28 19:28:24 +0000523 if (DID.Type == ValID::NameVal) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000524 GenerateError("Reference to an invalid definition: '" +DID.getName()+
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000525 "' of type '" + V->getType()->getDescription() + "'",
526 PHI->second.second);
Reid Spencer309080a2006-09-28 19:28:24 +0000527 return;
528 } else {
Reid Spencer713eedc2006-08-18 08:43:06 +0000529 GenerateError("Reference to an invalid definition: #" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000530 itostr(DID.Num) + " of type '" +
531 V->getType()->getDescription() + "'",
532 PHI->second.second);
Reid Spencer309080a2006-09-28 19:28:24 +0000533 return;
534 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000535 }
536 }
537 }
538
539 LateResolvers.clear();
540}
541
542// ResolveTypeTo - A brand new type was just declared. This means that (if
543// name is not null) things referencing Name can be resolved. Otherwise, things
544// refering to the number can be resolved. Do this now.
545//
546static void ResolveTypeTo(char *Name, const Type *ToTy) {
547 ValID D;
548 if (Name) D = ValID::create(Name);
549 else D = ValID::create((int)CurModule.Types.size());
550
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000551 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000552 CurModule.LateResolveTypes.find(D);
553 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000554 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000555 CurModule.LateResolveTypes.erase(I);
556 }
557}
558
559// setValueName - Set the specified value to the name given. The name may be
560// null potentially, in which case this is a noop. The string passed in is
561// assumed to be a malloc'd string buffer, and is free'd by this function.
562//
563static void setValueName(Value *V, char *NameStr) {
564 if (NameStr) {
565 std::string Name(NameStr); // Copy string
566 free(NameStr); // Free old string
567
Reid Spencer309080a2006-09-28 19:28:24 +0000568 if (V->getType() == Type::VoidTy) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000569 GenerateError("Can't assign name '" + Name+"' to value with void type!");
Reid Spencer309080a2006-09-28 19:28:24 +0000570 return;
571 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000572
573 assert(inFunctionScope() && "Must be in function scope!");
574 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
Reid Spencer309080a2006-09-28 19:28:24 +0000575 if (ST.lookup(V->getType(), Name)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000576 GenerateError("Redefinition of value named '" + Name + "' in the '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000577 V->getType()->getDescription() + "' type plane!");
Reid Spencer309080a2006-09-28 19:28:24 +0000578 return;
579 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000580
581 // Set the name.
582 V->setName(Name);
583 }
584}
585
586/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
587/// this is a declaration, otherwise it is a definition.
588static GlobalVariable *
589ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
590 bool isConstantGlobal, const Type *Ty,
591 Constant *Initializer) {
Reid Spencer309080a2006-09-28 19:28:24 +0000592 if (isa<FunctionType>(Ty)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000593 GenerateError("Cannot declare global vars of function type!");
Reid Spencer309080a2006-09-28 19:28:24 +0000594 return 0;
595 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000596
597 const PointerType *PTy = PointerType::get(Ty);
598
599 std::string Name;
600 if (NameStr) {
601 Name = NameStr; // Copy string
602 free(NameStr); // Free old string
603 }
604
605 // See if this global value was forward referenced. If so, recycle the
606 // object.
607 ValID ID;
608 if (!Name.empty()) {
609 ID = ValID::create((char*)Name.c_str());
610 } else {
611 ID = ValID::create((int)CurModule.Values[PTy].size());
612 }
613
614 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
615 // Move the global to the end of the list, from whereever it was
616 // previously inserted.
617 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
618 CurModule.CurrentModule->getGlobalList().remove(GV);
619 CurModule.CurrentModule->getGlobalList().push_back(GV);
620 GV->setInitializer(Initializer);
621 GV->setLinkage(Linkage);
622 GV->setConstant(isConstantGlobal);
623 InsertValue(GV, CurModule.Values);
624 return GV;
625 }
626
627 // If this global has a name, check to see if there is already a definition
628 // of this global in the module. If so, merge as appropriate. Note that
629 // this is really just a hack around problems in the CFE. :(
630 if (!Name.empty()) {
631 // We are a simple redefinition of a value, check to see if it is defined
632 // the same as the old one.
633 if (GlobalVariable *EGV =
634 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
635 // We are allowed to redefine a global variable in two circumstances:
636 // 1. If at least one of the globals is uninitialized or
637 // 2. If both initializers have the same value.
638 //
639 if (!EGV->hasInitializer() || !Initializer ||
640 EGV->getInitializer() == Initializer) {
641
642 // Make sure the existing global version gets the initializer! Make
643 // sure that it also gets marked const if the new version is.
644 if (Initializer && !EGV->hasInitializer())
645 EGV->setInitializer(Initializer);
646 if (isConstantGlobal)
647 EGV->setConstant(true);
648 EGV->setLinkage(Linkage);
649 return EGV;
650 }
651
Reid Spencer713eedc2006-08-18 08:43:06 +0000652 GenerateError("Redefinition of global variable named '" + Name +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000653 "' in the '" + Ty->getDescription() + "' type plane!");
Reid Spencer309080a2006-09-28 19:28:24 +0000654 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000655 }
656 }
657
658 // Otherwise there is no existing GV to use, create one now.
659 GlobalVariable *GV =
660 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
661 CurModule.CurrentModule);
662 InsertValue(GV, CurModule.Values);
663 return GV;
664}
665
666// setTypeName - Set the specified type to the name given. The name may be
667// null potentially, in which case this is a noop. The string passed in is
668// assumed to be a malloc'd string buffer, and is freed by this function.
669//
670// This function returns true if the type has already been defined, but is
671// allowed to be redefined in the specified context. If the name is a new name
672// for the type plane, it is inserted and false is returned.
673static bool setTypeName(const Type *T, char *NameStr) {
674 assert(!inFunctionScope() && "Can't give types function-local names!");
675 if (NameStr == 0) return false;
676
677 std::string Name(NameStr); // Copy string
678 free(NameStr); // Free old string
679
680 // We don't allow assigning names to void type
Reid Spencer309080a2006-09-28 19:28:24 +0000681 if (T == Type::VoidTy) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000682 GenerateError("Can't assign name '" + Name + "' to the void type!");
Reid Spencer309080a2006-09-28 19:28:24 +0000683 return false;
684 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000685
686 // Set the type name, checking for conflicts as we do so.
687 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
688
689 if (AlreadyExists) { // Inserting a name that is already defined???
690 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
691 assert(Existing && "Conflict but no matching type?");
692
693 // There is only one case where this is allowed: when we are refining an
694 // opaque type. In this case, Existing will be an opaque type.
695 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
696 // We ARE replacing an opaque type!
697 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
698 return true;
699 }
700
701 // Otherwise, this is an attempt to redefine a type. That's okay if
702 // the redefinition is identical to the original. This will be so if
703 // Existing and T point to the same Type object. In this one case we
704 // allow the equivalent redefinition.
705 if (Existing == T) return true; // Yes, it's equal.
706
707 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer713eedc2006-08-18 08:43:06 +0000708 GenerateError("Redefinition of type named '" + Name + "' in the '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000709 T->getDescription() + "' type plane!");
710 }
711
712 return false;
713}
714
715//===----------------------------------------------------------------------===//
716// Code for handling upreferences in type names...
717//
718
719// TypeContains - Returns true if Ty directly contains E in it.
720//
721static bool TypeContains(const Type *Ty, const Type *E) {
722 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
723 E) != Ty->subtype_end();
724}
725
726namespace {
727 struct UpRefRecord {
728 // NestingLevel - The number of nesting levels that need to be popped before
729 // this type is resolved.
730 unsigned NestingLevel;
731
732 // LastContainedTy - This is the type at the current binding level for the
733 // type. Every time we reduce the nesting level, this gets updated.
734 const Type *LastContainedTy;
735
736 // UpRefTy - This is the actual opaque type that the upreference is
737 // represented with.
738 OpaqueType *UpRefTy;
739
740 UpRefRecord(unsigned NL, OpaqueType *URTy)
741 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
742 };
743}
744
745// UpRefs - A list of the outstanding upreferences that need to be resolved.
746static std::vector<UpRefRecord> UpRefs;
747
748/// HandleUpRefs - Every time we finish a new layer of types, this function is
749/// called. It loops through the UpRefs vector, which is a list of the
750/// currently active types. For each type, if the up reference is contained in
751/// the newly completed type, we decrement the level count. When the level
752/// count reaches zero, the upreferenced type is the type that is passed in:
753/// thus we can complete the cycle.
754///
755static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner680aab62006-08-18 17:34:45 +0000756 // If Ty isn't abstract, or if there are no up-references in it, then there is
757 // nothing to resolve here.
758 if (!ty->isAbstract() || UpRefs.empty()) return ty;
759
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000760 PATypeHolder Ty(ty);
761 UR_OUT("Type '" << Ty->getDescription() <<
762 "' newly formed. Resolving upreferences.\n" <<
763 UpRefs.size() << " upreferences active!\n");
764
765 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
766 // to zero), we resolve them all together before we resolve them to Ty. At
767 // the end of the loop, if there is anything to resolve to Ty, it will be in
768 // this variable.
769 OpaqueType *TypeToResolve = 0;
770
771 for (unsigned i = 0; i != UpRefs.size(); ++i) {
772 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
773 << UpRefs[i].second->getDescription() << ") = "
774 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
775 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
776 // Decrement level of upreference
777 unsigned Level = --UpRefs[i].NestingLevel;
778 UpRefs[i].LastContainedTy = Ty;
779 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
780 if (Level == 0) { // Upreference should be resolved!
781 if (!TypeToResolve) {
782 TypeToResolve = UpRefs[i].UpRefTy;
783 } else {
784 UR_OUT(" * Resolving upreference for "
785 << UpRefs[i].second->getDescription() << "\n";
786 std::string OldName = UpRefs[i].UpRefTy->getDescription());
787 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
788 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
789 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
790 }
791 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
792 --i; // Do not skip the next element...
793 }
794 }
795 }
796
797 if (TypeToResolve) {
798 UR_OUT(" * Resolving upreference for "
799 << UpRefs[i].second->getDescription() << "\n";
800 std::string OldName = TypeToResolve->getDescription());
801 TypeToResolve->refineAbstractTypeTo(Ty);
802 }
803
804 return Ty;
805}
806
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000807// common code from the two 'RunVMAsmParser' functions
Reid Spencer309080a2006-09-28 19:28:24 +0000808static Module* RunParser(Module * M) {
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000809
810 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000811 CurModule.CurrentModule = M;
Reid Spencerff359002006-10-09 17:36:59 +0000812
813 // Check to make sure the parser succeeded
814 if (yyparse()) {
815 if (ParserResult)
816 delete ParserResult;
817 return 0;
818 }
819
820 // Check to make sure that parsing produced a result
Reid Spencer713eedc2006-08-18 08:43:06 +0000821 if (!ParserResult)
822 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000823
Reid Spencerff359002006-10-09 17:36:59 +0000824 // Reset ParserResult variable while saving its value for the result.
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000825 Module *Result = ParserResult;
826 ParserResult = 0;
827
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000828 return Result;
Reid Spencer309080a2006-09-28 19:28:24 +0000829}
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000830
831//===----------------------------------------------------------------------===//
832// RunVMAsmParser - Define an interface to this parser
833//===----------------------------------------------------------------------===//
834//
835Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
836 set_scan_file(F);
837
838 CurFilename = Filename;
839 return RunParser(new Module(CurFilename));
840}
841
842Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
843 set_scan_string(AsmString);
844
845 CurFilename = "from_memory";
846 if (M == NULL) {
847 return RunParser(new Module (CurFilename));
848 } else {
849 return RunParser(M);
850 }
851}
852
853%}
854
855%union {
856 llvm::Module *ModuleVal;
857 llvm::Function *FunctionVal;
Reid Spencere2c32da2006-12-03 05:46:11 +0000858 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000859 llvm::BasicBlock *BasicBlockVal;
860 llvm::TerminatorInst *TermInstVal;
861 llvm::Instruction *InstVal;
Reid Spencere2c32da2006-12-03 05:46:11 +0000862 llvm::Constant *ConstVal;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000863
Reid Spencere2c32da2006-12-03 05:46:11 +0000864 const llvm::Type *PrimType;
865 llvm::PATypeHolder *TypeVal;
866 llvm::Value *ValueVal;
867
868 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
869 std::vector<llvm::Value*> *ValueList;
870 std::list<llvm::PATypeHolder> *TypeList;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000871 // Represent the RHS of PHI node
Reid Spencere2c32da2006-12-03 05:46:11 +0000872 std::list<std::pair<llvm::Value*,
873 llvm::BasicBlock*> > *PHIList;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000874 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencere2c32da2006-12-03 05:46:11 +0000875 std::vector<llvm::Constant*> *ConstVector;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000876
877 llvm::GlobalValue::LinkageTypes Linkage;
878 int64_t SInt64Val;
879 uint64_t UInt64Val;
880 int SIntVal;
881 unsigned UIntVal;
882 double FPVal;
883 bool BoolVal;
884
885 char *StrVal; // This memory is strdup'd!
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000886 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000887
Reid Spencere2c32da2006-12-03 05:46:11 +0000888 llvm::Instruction::BinaryOps BinaryOpVal;
889 llvm::Instruction::TermOps TermOpVal;
890 llvm::Instruction::MemoryOps MemOpVal;
891 llvm::Instruction::CastOps CastOpVal;
892 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000893 llvm::Module::Endianness Endianness;
Reid Spencere2c32da2006-12-03 05:46:11 +0000894 llvm::ICmpInst::Predicate IPredicate;
895 llvm::FCmpInst::Predicate FPredicate;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000896}
897
898%type <ModuleVal> Module FunctionList
899%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
900%type <BasicBlockVal> BasicBlock InstructionList
901%type <TermInstVal> BBTerminatorInst
902%type <InstVal> Inst InstVal MemoryInst
903%type <ConstVal> ConstVal ConstExpr
904%type <ConstVector> ConstVector
905%type <ArgList> ArgList ArgListH
906%type <ArgVal> ArgVal
907%type <PHIList> PHIList
908%type <ValueList> ValueRefList ValueRefListE // For call param lists
909%type <ValueList> IndexList // For GEP derived indices
910%type <TypeList> TypeListI ArgTypeListI
911%type <JumpTable> JumpTable
912%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
913%type <BoolVal> OptVolatile // 'volatile' or not
914%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
915%type <BoolVal> OptSideEffect // 'sideeffect' or not.
916%type <Linkage> OptLinkage
917%type <Endianness> BigOrLittle
918
919// ValueRef - Unresolved reference to a definition or BB
920%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
921%type <ValueVal> ResolvedVal // <type> <valref> pair
922// Tokens and types for handling constant integer values
923//
924// ESINT64VAL - A negative number within long long range
925%token <SInt64Val> ESINT64VAL
926
927// EUINT64VAL - A positive number within uns. long long range
928%token <UInt64Val> EUINT64VAL
929%type <SInt64Val> EINT64VAL
930
931%token <SIntVal> SINTVAL // Signed 32 bit ints...
932%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
933%type <SIntVal> INTVAL
934%token <FPVal> FPVAL // Float or Double constant
935
936// Built in types...
937%type <TypeVal> Types TypesV UpRTypes UpRTypesV
Reid Spencere2c32da2006-12-03 05:46:11 +0000938%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
939%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
940%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000941
942%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
943%type <StrVal> Name OptName OptAssign
944%type <UIntVal> OptAlign OptCAlign
945%type <StrVal> OptSection SectionString
946
947%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
948%token DECLARE GLOBAL CONSTANT SECTION VOLATILE
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000949%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
950%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000951%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
952%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Chris Lattner09c0e992006-05-19 21:28:53 +0000953%token CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +0000954%token X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattner7d1d0342006-10-22 06:08:13 +0000955%token DATALAYOUT
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000956%type <UIntVal> OptCallingConv
957
958// Basic Block Terminating Operators
959%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
960
961// Binary Operators
962%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Reid Spencerde46e482006-11-02 20:25:50 +0000963%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000964%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
Reid Spencere2c32da2006-12-03 05:46:11 +0000965%token <OtherOpVal> ICMP FCMP
Reid Spencere2c32da2006-12-03 05:46:11 +0000966%type <IPredicate> IPredicates
Reid Spencere2c32da2006-12-03 05:46:11 +0000967%type <FPredicate> FPredicates
Reid Spencer1960ef32006-12-03 06:59:29 +0000968%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
969%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000970
971// Memory Instructions
972%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
973
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000974// Cast Operators
975%type <CastOpVal> CastOps
976%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
977%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
978
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000979// Other Operators
980%type <OtherOpVal> ShiftOps
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000981%token <OtherOpVal> PHI_TOK SELECT SHL LSHR ASHR VAARG
Chris Lattner9ff96a72006-04-08 01:18:56 +0000982%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000983
984
985%start Module
986%%
987
988// Handle constant integer size restriction and conversion...
989//
990INTVAL : SINTVAL;
991INTVAL : UINTVAL {
992 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
Reid Spencer713eedc2006-08-18 08:43:06 +0000993 GEN_ERROR("Value too large for type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000994 $$ = (int32_t)$1;
Reid Spencer713eedc2006-08-18 08:43:06 +0000995 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000996};
997
998
999EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
1000EINT64VAL : EUINT64VAL {
1001 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
Reid Spencer713eedc2006-08-18 08:43:06 +00001002 GEN_ERROR("Value too large for type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001003 $$ = (int64_t)$1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001004 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001005};
1006
1007// Operations that are notably excluded from this list include:
1008// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1009//
Reid Spencerde46e482006-11-02 20:25:50 +00001010ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001011LogicalOps : AND | OR | XOR;
1012SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001013CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1014 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
1015ShiftOps : SHL | LSHR | ASHR;
Reid Spencer1960ef32006-12-03 06:59:29 +00001016IPredicates
Reid Spencerd2e0c342006-12-04 05:24:24 +00001017 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer1960ef32006-12-03 06:59:29 +00001018 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1019 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1020 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1021 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1022 ;
1023
1024FPredicates
1025 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1026 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1027 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1028 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1029 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1030 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1031 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1032 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1033 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1034 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001035
1036// These are some types that allow classification if we only want a particular
1037// thing... for example, only a signed, unsigned, or integral type.
1038SIntType : LONG | INT | SHORT | SBYTE;
1039UIntType : ULONG | UINT | USHORT | UBYTE;
1040IntType : SIntType | UIntType;
1041FPType : FLOAT | DOUBLE;
1042
1043// OptAssign - Value producing statements have an optional assignment component
1044OptAssign : Name '=' {
1045 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001046 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001047 }
1048 | /*empty*/ {
1049 $$ = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00001050 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001051 };
1052
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001053OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
1054 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
1055 WEAK { $$ = GlobalValue::WeakLinkage; } |
1056 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
1057 DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; } |
1058 DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; } |
1059 EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; } |
1060 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
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
1076// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1077// a comma before it.
1078OptAlign : /*empty*/ { $$ = 0; } |
1079 ALIGN EUINT64VAL {
1080 $$ = $2;
1081 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencer713eedc2006-08-18 08:43:06 +00001082 GEN_ERROR("Alignment must be a power of two!");
1083 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001084};
1085OptCAlign : /*empty*/ { $$ = 0; } |
1086 ',' ALIGN EUINT64VAL {
1087 $$ = $3;
1088 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencer713eedc2006-08-18 08:43:06 +00001089 GEN_ERROR("Alignment must be a power of two!");
1090 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001091};
1092
1093
1094SectionString : SECTION STRINGCONSTANT {
1095 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1096 if ($2[i] == '"' || $2[i] == '\\')
Reid Spencer713eedc2006-08-18 08:43:06 +00001097 GEN_ERROR("Invalid character in section name!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001098 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001099 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001100};
1101
1102OptSection : /*empty*/ { $$ = 0; } |
1103 SectionString { $$ = $1; };
1104
1105// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1106// is set to be the global we are processing.
1107//
1108GlobalVarAttributes : /* empty */ {} |
1109 ',' GlobalVarAttribute GlobalVarAttributes {};
1110GlobalVarAttribute : SectionString {
1111 CurGV->setSection($1);
1112 free($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001113 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001114 }
1115 | ALIGN EUINT64VAL {
1116 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencer713eedc2006-08-18 08:43:06 +00001117 GEN_ERROR("Alignment must be a power of two!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001118 CurGV->setAlignment($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001119 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001120 };
1121
1122//===----------------------------------------------------------------------===//
1123// Types includes all predefined types... except void, because it can only be
1124// used in specific contexts (function returning void for example). To have
1125// access to it, a user must explicitly use TypesV.
1126//
1127
1128// TypesV includes all of 'Types', but it also includes the void type.
Reid Spencere2c32da2006-12-03 05:46:11 +00001129TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1130UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001131
1132Types : UpRTypes {
1133 if (!UpRefs.empty())
Reid Spencere2c32da2006-12-03 05:46:11 +00001134 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001135 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001136 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00001137 };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001138
1139
1140// Derived types are added later...
1141//
1142PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1143PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
1144UpRTypes : OPAQUE {
Reid Spencere2c32da2006-12-03 05:46:11 +00001145 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer713eedc2006-08-18 08:43:06 +00001146 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001147 }
1148 | PrimType {
Reid Spencere2c32da2006-12-03 05:46:11 +00001149 $$ = new PATypeHolder($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001150 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001151 };
1152UpRTypes : SymbolicValueRef { // Named types are also simple types...
Reid Spencer309080a2006-09-28 19:28:24 +00001153 const Type* tmp = getTypeVal($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001154 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00001155 $$ = new PATypeHolder(tmp);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001156};
1157
1158// Include derived types in the Types production.
1159//
1160UpRTypes : '\\' EUINT64VAL { // Type UpReference
Reid Spencer713eedc2006-08-18 08:43:06 +00001161 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001162 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1163 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencere2c32da2006-12-03 05:46:11 +00001164 $$ = new PATypeHolder(OT);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001165 UR_OUT("New Upreference!\n");
Reid Spencer713eedc2006-08-18 08:43:06 +00001166 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001167 }
1168 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
1169 std::vector<const Type*> Params;
Reid Spencere2c32da2006-12-03 05:46:11 +00001170 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001171 E = $3->end(); I != E; ++I)
Reid Spencere2c32da2006-12-03 05:46:11 +00001172 Params.push_back(*I);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001173 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1174 if (isVarArg) Params.pop_back();
1175
Reid Spencere2c32da2006-12-03 05:46:11 +00001176 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001177 delete $3; // Delete the argument list
Reid Spencere2c32da2006-12-03 05:46:11 +00001178 delete $1; // Delete the return type handle
Reid Spencer713eedc2006-08-18 08:43:06 +00001179 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001180 }
1181 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencere2c32da2006-12-03 05:46:11 +00001182 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1183 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00001184 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001185 }
1186 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
Reid Spencere2c32da2006-12-03 05:46:11 +00001187 const llvm::Type* ElemTy = $4->get();
1188 if ((unsigned)$2 != $2)
1189 GEN_ERROR("Unsigned result not equal to signed result");
1190 if (!ElemTy->isPrimitiveType())
1191 GEN_ERROR("Elemental type of a PackedType must be primitive");
1192 if (!isPowerOf2_32($2))
1193 GEN_ERROR("Vector length should be a power of 2!");
1194 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
1195 delete $4;
1196 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001197 }
1198 | '{' TypeListI '}' { // Structure type?
1199 std::vector<const Type*> Elements;
Reid Spencere2c32da2006-12-03 05:46:11 +00001200 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001201 E = $2->end(); I != E; ++I)
Reid Spencere2c32da2006-12-03 05:46:11 +00001202 Elements.push_back(*I);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001203
Reid Spencere2c32da2006-12-03 05:46:11 +00001204 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001205 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001206 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001207 }
1208 | '{' '}' { // Empty structure type?
Reid Spencere2c32da2006-12-03 05:46:11 +00001209 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer713eedc2006-08-18 08:43:06 +00001210 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001211 }
1212 | UpRTypes '*' { // Pointer type?
Reid Spencere2c32da2006-12-03 05:46:11 +00001213 if (*$1 == Type::LabelTy)
Chris Lattnerff20ba32006-10-15 23:27:25 +00001214 GEN_ERROR("Cannot form a pointer to a basic block");
Reid Spencere2c32da2006-12-03 05:46:11 +00001215 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1216 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001217 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001218 };
1219
1220// TypeList - Used for struct declarations and as a basis for function type
1221// declaration type lists
1222//
1223TypeListI : UpRTypes {
Reid Spencere2c32da2006-12-03 05:46:11 +00001224 $$ = new std::list<PATypeHolder>();
1225 $$->push_back(*$1); delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001226 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001227 }
1228 | TypeListI ',' UpRTypes {
Reid Spencere2c32da2006-12-03 05:46:11 +00001229 ($$=$1)->push_back(*$3); delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001230 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001231 };
1232
1233// ArgTypeList - List of types for a function type declaration...
1234ArgTypeListI : TypeListI
1235 | TypeListI ',' DOTDOTDOT {
Reid Spencere2c32da2006-12-03 05:46:11 +00001236 ($$=$1)->push_back(Type::VoidTy);
Reid Spencer713eedc2006-08-18 08:43:06 +00001237 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001238 }
1239 | DOTDOTDOT {
Reid Spencere2c32da2006-12-03 05:46:11 +00001240 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Reid Spencer713eedc2006-08-18 08:43:06 +00001241 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001242 }
1243 | /*empty*/ {
Reid Spencere2c32da2006-12-03 05:46:11 +00001244 $$ = new std::list<PATypeHolder>();
Reid Spencer713eedc2006-08-18 08:43:06 +00001245 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001246 };
1247
1248// ConstVal - The various declarations that go into the constant pool. This
1249// production is used ONLY to represent constants that show up AFTER a 'const',
1250// 'constant' or 'global' token at global scope. Constants that can be inlined
1251// into other expressions (such as integers and constexprs) are handled by the
1252// ResolvedVal, ValueRef and ConstValueRef productions.
1253//
1254ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencere2c32da2006-12-03 05:46:11 +00001255 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001256 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001257 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00001258 (*$1)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001259 const Type *ETy = ATy->getElementType();
1260 int NumElements = ATy->getNumElements();
1261
1262 // Verify that we have the correct size...
1263 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer713eedc2006-08-18 08:43:06 +00001264 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001265 utostr($3->size()) + " arguments, but has size of " +
1266 itostr(NumElements) + "!");
1267
1268 // Verify all elements are correct type!
1269 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencere2c32da2006-12-03 05:46:11 +00001270 if (ETy != (*$3)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001271 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001272 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencere2c32da2006-12-03 05:46:11 +00001273 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001274 }
1275
Reid Spencere2c32da2006-12-03 05:46:11 +00001276 $$ = ConstantArray::get(ATy, *$3);
1277 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001278 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001279 }
1280 | Types '[' ']' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001281 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001282 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001283 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00001284 (*$1)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001285
1286 int NumElements = ATy->getNumElements();
1287 if (NumElements != -1 && NumElements != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001288 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001289 " arguments, but has size of " + itostr(NumElements) +"!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001290 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1291 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001292 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001293 }
1294 | Types 'c' STRINGCONSTANT {
Reid Spencere2c32da2006-12-03 05:46:11 +00001295 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001296 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001297 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00001298 (*$1)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001299
1300 int NumElements = ATy->getNumElements();
1301 const Type *ETy = ATy->getElementType();
1302 char *EndStr = UnEscapeLexed($3, true);
1303 if (NumElements != -1 && NumElements != (EndStr-$3))
Reid Spencer713eedc2006-08-18 08:43:06 +00001304 GEN_ERROR("Can't build string constant of size " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001305 itostr((int)(EndStr-$3)) +
1306 " when array has size " + itostr(NumElements) + "!");
1307 std::vector<Constant*> Vals;
1308 if (ETy == Type::SByteTy) {
1309 for (signed char *C = (signed char *)$3; C != (signed char *)EndStr; ++C)
Reid Spencere0fc4df2006-10-20 07:07:24 +00001310 Vals.push_back(ConstantInt::get(ETy, *C));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001311 } else if (ETy == Type::UByteTy) {
1312 for (unsigned char *C = (unsigned char *)$3;
1313 C != (unsigned char*)EndStr; ++C)
Reid Spencere0fc4df2006-10-20 07:07:24 +00001314 Vals.push_back(ConstantInt::get(ETy, *C));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001315 } else {
1316 free($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001317 GEN_ERROR("Cannot build string arrays of non byte sized elements!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001318 }
1319 free($3);
Reid Spencere2c32da2006-12-03 05:46:11 +00001320 $$ = ConstantArray::get(ATy, Vals);
1321 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001322 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001323 }
1324 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencere2c32da2006-12-03 05:46:11 +00001325 const PackedType *PTy = dyn_cast<PackedType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001326 if (PTy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001327 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00001328 (*$1)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001329 const Type *ETy = PTy->getElementType();
1330 int NumElements = PTy->getNumElements();
1331
1332 // Verify that we have the correct size...
1333 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer713eedc2006-08-18 08:43:06 +00001334 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001335 utostr($3->size()) + " arguments, but has size of " +
1336 itostr(NumElements) + "!");
1337
1338 // Verify all elements are correct type!
1339 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencere2c32da2006-12-03 05:46:11 +00001340 if (ETy != (*$3)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001341 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001342 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencere2c32da2006-12-03 05:46:11 +00001343 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001344 }
1345
Reid Spencere2c32da2006-12-03 05:46:11 +00001346 $$ = ConstantPacked::get(PTy, *$3);
1347 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001348 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001349 }
1350 | Types '{' ConstVector '}' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001351 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001352 if (STy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001353 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00001354 (*$1)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001355
1356 if ($3->size() != STy->getNumContainedTypes())
Reid Spencer713eedc2006-08-18 08:43:06 +00001357 GEN_ERROR("Illegal number of initializers for structure type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001358
1359 // Check to ensure that constants are compatible with the type initializer!
1360 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencere2c32da2006-12-03 05:46:11 +00001361 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer713eedc2006-08-18 08:43:06 +00001362 GEN_ERROR("Expected type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001363 STy->getElementType(i)->getDescription() +
1364 "' for element #" + utostr(i) +
1365 " of structure initializer!");
1366
Reid Spencere2c32da2006-12-03 05:46:11 +00001367 $$ = ConstantStruct::get(STy, *$3);
1368 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001369 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001370 }
1371 | Types '{' '}' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001372 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001373 if (STy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001374 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00001375 (*$1)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001376
1377 if (STy->getNumContainedTypes() != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001378 GEN_ERROR("Illegal number of initializers for structure type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001379
Reid Spencere2c32da2006-12-03 05:46:11 +00001380 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1381 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001382 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001383 }
1384 | Types NULL_TOK {
Reid Spencere2c32da2006-12-03 05:46:11 +00001385 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001386 if (PTy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001387 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00001388 (*$1)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001389
Reid Spencere2c32da2006-12-03 05:46:11 +00001390 $$ = ConstantPointerNull::get(PTy);
1391 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001392 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001393 }
1394 | Types UNDEF {
Reid Spencere2c32da2006-12-03 05:46:11 +00001395 $$ = UndefValue::get($1->get());
1396 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001397 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001398 }
1399 | Types SymbolicValueRef {
Reid Spencere2c32da2006-12-03 05:46:11 +00001400 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001401 if (Ty == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001402 GEN_ERROR("Global const reference must be a pointer type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001403
1404 // ConstExprs can exist in the body of a function, thus creating
1405 // GlobalValues whenever they refer to a variable. Because we are in
1406 // the context of a function, getValNonImprovising will search the functions
1407 // symbol table instead of the module symbol table for the global symbol,
1408 // which throws things all off. To get around this, we just tell
1409 // getValNonImprovising that we are at global scope here.
1410 //
1411 Function *SavedCurFn = CurFun.CurrentFunction;
1412 CurFun.CurrentFunction = 0;
1413
1414 Value *V = getValNonImprovising(Ty, $2);
Reid Spencer309080a2006-09-28 19:28:24 +00001415 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001416
1417 CurFun.CurrentFunction = SavedCurFn;
1418
1419 // If this is an initializer for a constant pointer, which is referencing a
1420 // (currently) undefined variable, create a stub now that shall be replaced
1421 // in the future with the right type of variable.
1422 //
1423 if (V == 0) {
1424 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1425 const PointerType *PT = cast<PointerType>(Ty);
1426
1427 // First check to see if the forward references value is already created!
1428 PerModuleInfo::GlobalRefsType::iterator I =
1429 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1430
1431 if (I != CurModule.GlobalRefs.end()) {
1432 V = I->second; // Placeholder already exists, use it...
1433 $2.destroy();
1434 } else {
1435 std::string Name;
1436 if ($2.Type == ValID::NameVal) Name = $2.Name;
1437
1438 // Create the forward referenced global.
1439 GlobalValue *GV;
1440 if (const FunctionType *FTy =
1441 dyn_cast<FunctionType>(PT->getElementType())) {
1442 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1443 CurModule.CurrentModule);
1444 } else {
1445 GV = new GlobalVariable(PT->getElementType(), false,
1446 GlobalValue::ExternalLinkage, 0,
1447 Name, CurModule.CurrentModule);
1448 }
1449
1450 // Keep track of the fact that we have a forward ref to recycle it
1451 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1452 V = GV;
1453 }
1454 }
1455
Reid Spencere2c32da2006-12-03 05:46:11 +00001456 $$ = cast<GlobalValue>(V);
1457 delete $1; // Free the type handle
Reid Spencer713eedc2006-08-18 08:43:06 +00001458 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001459 }
1460 | Types ConstExpr {
Reid Spencere2c32da2006-12-03 05:46:11 +00001461 if ($1->get() != $2->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001462 GEN_ERROR("Mismatched types for constant expression!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001463 $$ = $2;
Reid Spencere2c32da2006-12-03 05:46:11 +00001464 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001465 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001466 }
1467 | Types ZEROINITIALIZER {
Reid Spencere2c32da2006-12-03 05:46:11 +00001468 const Type *Ty = $1->get();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001469 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencer713eedc2006-08-18 08:43:06 +00001470 GEN_ERROR("Cannot create a null initialized value of this type!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001471 $$ = Constant::getNullValue(Ty);
1472 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001473 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00001474 }
1475 | SIntType EINT64VAL { // integral constants
1476 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencer713eedc2006-08-18 08:43:06 +00001477 GEN_ERROR("Constant value doesn't fit in type!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001478 $$ = ConstantInt::get($1, $2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001479 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001480 }
1481 | UIntType EUINT64VAL { // integral constants
Reid Spencere2c32da2006-12-03 05:46:11 +00001482 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencer713eedc2006-08-18 08:43:06 +00001483 GEN_ERROR("Constant value doesn't fit in type!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001484 $$ = ConstantInt::get($1, $2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001485 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001486 }
1487 | BOOL TRUETOK { // Boolean constants
Reid Spencere2c32da2006-12-03 05:46:11 +00001488 $$ = ConstantBool::getTrue();
Reid Spencer713eedc2006-08-18 08:43:06 +00001489 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001490 }
1491 | BOOL FALSETOK { // Boolean constants
Reid Spencere2c32da2006-12-03 05:46:11 +00001492 $$ = ConstantBool::getFalse();
Reid Spencer713eedc2006-08-18 08:43:06 +00001493 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001494 }
1495 | FPType FPVAL { // Float & Double constants
Reid Spencere2c32da2006-12-03 05:46:11 +00001496 if (!ConstantFP::isValueValidForType($1, $2))
Reid Spencer713eedc2006-08-18 08:43:06 +00001497 GEN_ERROR("Floating point constant invalid for type!!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001498 $$ = ConstantFP::get($1, $2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001499 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001500 };
1501
1502
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001503ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001504 Constant *Val = $3;
1505 const Type *Ty = $5->get();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001506 if (!Val->getType()->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001507 GEN_ERROR("cast constant expression from a non-primitive type: '" +
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001508 Val->getType()->getDescription() + "'!");
1509 if (!Ty->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001510 GEN_ERROR("cast constant expression to a non-primitive type: '" +
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001511 Ty->getDescription() + "'!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001512 $$ = ConstantExpr::getCast($1, $3, $5->get());
1513 delete $5;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001514 }
1515 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001516 if (!isa<PointerType>($3->getType()))
Reid Spencer713eedc2006-08-18 08:43:06 +00001517 GEN_ERROR("GetElementPtr requires a pointer operand!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001518
Reid Spencere2c32da2006-12-03 05:46:11 +00001519 const Type *IdxTy =
1520 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
1521 if (!IdxTy)
1522 GEN_ERROR("Index list invalid for constant getelementptr!");
1523
1524 std::vector<Constant*> IdxVec;
1525 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1526 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001527 IdxVec.push_back(C);
1528 else
Reid Spencer713eedc2006-08-18 08:43:06 +00001529 GEN_ERROR("Indices to constant getelementptr must be constants!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001530
1531 delete $4;
1532
Reid Spencere2c32da2006-12-03 05:46:11 +00001533 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Reid Spencer713eedc2006-08-18 08:43:06 +00001534 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001535 }
1536 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001537 if ($3->getType() != Type::BoolTy)
Reid Spencer713eedc2006-08-18 08:43:06 +00001538 GEN_ERROR("Select condition must be of boolean type!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001539 if ($5->getType() != $7->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001540 GEN_ERROR("Select operand types must match!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001541 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001542 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001543 }
1544 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001545 if ($3->getType() != $5->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001546 GEN_ERROR("Binary operator types must match!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001547 CHECK_FOR_ERROR;
Reid Spencer844668d2006-12-05 19:16:11 +00001548 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001549 }
1550 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001551 if ($3->getType() != $5->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001552 GEN_ERROR("Logical operator types must match!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001553 if (!$3->getType()->isIntegral()) {
1554 if (!isa<PackedType>($3->getType()) ||
1555 !cast<PackedType>($3->getType())->getElementType()->isIntegral())
Reid Spencer713eedc2006-08-18 08:43:06 +00001556 GEN_ERROR("Logical operator requires integral operands!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001557 }
Reid Spencere2c32da2006-12-03 05:46:11 +00001558 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001559 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001560 }
1561 | SetCondOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001562 if ($3->getType() != $5->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001563 GEN_ERROR("setcc operand types must match!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001564 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001565 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001566 }
Reid Spencerd2e0c342006-12-04 05:24:24 +00001567 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1568 if ($4->getType() != $6->getType())
Reid Spencere2c32da2006-12-03 05:46:11 +00001569 GEN_ERROR("icmp operand types must match!");
Reid Spencerd2e0c342006-12-04 05:24:24 +00001570 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencere2c32da2006-12-03 05:46:11 +00001571 }
Reid Spencerd2e0c342006-12-04 05:24:24 +00001572 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1573 if ($4->getType() != $6->getType())
Reid Spencere2c32da2006-12-03 05:46:11 +00001574 GEN_ERROR("fcmp operand types must match!");
Reid Spencerd2e0c342006-12-04 05:24:24 +00001575 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencere2c32da2006-12-03 05:46:11 +00001576 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001577 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001578 if ($5->getType() != Type::UByteTy)
Reid Spencer713eedc2006-08-18 08:43:06 +00001579 GEN_ERROR("Shift count for shift constant must be unsigned byte!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001580 if (!$3->getType()->isInteger())
Reid Spencer713eedc2006-08-18 08:43:06 +00001581 GEN_ERROR("Shift constant expression requires integer operand!");
Reid Spencerfdff9382006-11-08 06:47:33 +00001582 CHECK_FOR_ERROR;
Reid Spencere2c32da2006-12-03 05:46:11 +00001583 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001584 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001585 }
1586 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001587 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencer713eedc2006-08-18 08:43:06 +00001588 GEN_ERROR("Invalid extractelement operands!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001589 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001590 CHECK_FOR_ERROR
Chris Lattneraebccf82006-04-08 03:55:17 +00001591 }
1592 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001593 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencer713eedc2006-08-18 08:43:06 +00001594 GEN_ERROR("Invalid insertelement operands!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001595 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001596 CHECK_FOR_ERROR
Chris Lattneraebccf82006-04-08 03:55:17 +00001597 }
1598 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001599 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencer713eedc2006-08-18 08:43:06 +00001600 GEN_ERROR("Invalid shufflevector operands!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001601 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001602 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001603 };
1604
Chris Lattneraebccf82006-04-08 03:55:17 +00001605
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001606// ConstVector - A list of comma separated constants.
1607ConstVector : ConstVector ',' ConstVal {
1608 ($$ = $1)->push_back($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001609 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001610 }
1611 | ConstVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00001612 $$ = new std::vector<Constant*>();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001613 $$->push_back($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001614 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001615 };
1616
1617
1618// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1619GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
1620
1621
1622//===----------------------------------------------------------------------===//
1623// Rules to match Modules
1624//===----------------------------------------------------------------------===//
1625
1626// Module rule: Capture the result of parsing the whole file into a result
1627// variable...
1628//
1629Module : FunctionList {
1630 $$ = ParserResult = $1;
1631 CurModule.ModuleDone();
Reid Spencerff359002006-10-09 17:36:59 +00001632 CHECK_FOR_ERROR;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001633};
1634
1635// FunctionList - A list of functions, preceeded by a constant pool.
1636//
1637FunctionList : FunctionList Function {
1638 $$ = $1;
1639 CurFun.FunctionDone();
Reid Spencer713eedc2006-08-18 08:43:06 +00001640 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001641 }
1642 | FunctionList FunctionProto {
1643 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001644 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001645 }
1646 | FunctionList MODULE ASM_TOK AsmBlock {
1647 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001648 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001649 }
1650 | FunctionList IMPLEMENTATION {
1651 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001652 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001653 }
1654 | ConstPool {
1655 $$ = CurModule.CurrentModule;
1656 // Emit an error if there are any unresolved types left.
1657 if (!CurModule.LateResolveTypes.empty()) {
1658 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
Reid Spencer713eedc2006-08-18 08:43:06 +00001659 if (DID.Type == ValID::NameVal) {
1660 GEN_ERROR("Reference to an undefined type: '"+DID.getName() + "'");
1661 } else {
1662 GEN_ERROR("Reference to an undefined type: #" + itostr(DID.Num));
1663 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001664 }
Reid Spencer713eedc2006-08-18 08:43:06 +00001665 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001666 };
1667
1668// ConstPool - Constants with optional names assigned to them.
1669ConstPool : ConstPool OptAssign TYPE TypesV {
1670 // Eagerly resolve types. This is not an optimization, this is a
1671 // requirement that is due to the fact that we could have this:
1672 //
1673 // %list = type { %list * }
1674 // %list = type { %list * } ; repeated type decl
1675 //
1676 // If types are not resolved eagerly, then the two types will not be
1677 // determined to be the same type!
1678 //
Reid Spencere2c32da2006-12-03 05:46:11 +00001679 ResolveTypeTo($2, *$4);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001680
Reid Spencere2c32da2006-12-03 05:46:11 +00001681 if (!setTypeName(*$4, $2) && !$2) {
Reid Spencer309080a2006-09-28 19:28:24 +00001682 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001683 // If this is a named type that is not a redefinition, add it to the slot
1684 // table.
Reid Spencere2c32da2006-12-03 05:46:11 +00001685 CurModule.Types.push_back(*$4);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001686 }
Reid Spencere2c32da2006-12-03 05:46:11 +00001687
1688 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00001689 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001690 }
1691 | ConstPool FunctionProto { // Function prototypes can be in const pool
Reid Spencer713eedc2006-08-18 08:43:06 +00001692 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001693 }
1694 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
Reid Spencer713eedc2006-08-18 08:43:06 +00001695 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001696 }
1697 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00001698 if ($5 == 0)
Reid Spencer309080a2006-09-28 19:28:24 +00001699 GEN_ERROR("Global value initializer is not a constant!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001700 CurGV = ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001701 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00001702 } GlobalVarAttributes {
1703 CurGV = 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001704 }
1705 | ConstPool OptAssign EXTERNAL GlobalType Types {
Reid Spencere2c32da2006-12-03 05:46:11 +00001706 CurGV = ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, *$5, 0);
Reid Spencer309080a2006-09-28 19:28:24 +00001707 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00001708 delete $5;
Reid Spencer309080a2006-09-28 19:28:24 +00001709 } GlobalVarAttributes {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001710 CurGV = 0;
1711 CHECK_FOR_ERROR
1712 }
1713 | ConstPool OptAssign DLLIMPORT GlobalType Types {
Reid Spencere2c32da2006-12-03 05:46:11 +00001714 CurGV = ParseGlobalVariable($2, GlobalValue::DLLImportLinkage, $4, *$5, 0);
Reid Spencer309080a2006-09-28 19:28:24 +00001715 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00001716 delete $5;
Reid Spencer309080a2006-09-28 19:28:24 +00001717 } GlobalVarAttributes {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001718 CurGV = 0;
1719 CHECK_FOR_ERROR
1720 }
1721 | ConstPool OptAssign EXTERN_WEAK GlobalType Types {
Reid Spencer309080a2006-09-28 19:28:24 +00001722 CurGV =
Reid Spencere2c32da2006-12-03 05:46:11 +00001723 ParseGlobalVariable($2, GlobalValue::ExternalWeakLinkage, $4, *$5, 0);
Reid Spencer309080a2006-09-28 19:28:24 +00001724 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00001725 delete $5;
Reid Spencer309080a2006-09-28 19:28:24 +00001726 } GlobalVarAttributes {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001727 CurGV = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00001728 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001729 }
1730 | ConstPool TARGET TargetDefinition {
Reid Spencer713eedc2006-08-18 08:43:06 +00001731 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001732 }
1733 | ConstPool DEPLIBS '=' LibrariesDefinition {
Reid Spencer713eedc2006-08-18 08:43:06 +00001734 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001735 }
1736 | /* empty: end of list */ {
1737 };
1738
1739
1740AsmBlock : STRINGCONSTANT {
1741 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
1742 char *EndStr = UnEscapeLexed($1, true);
1743 std::string NewAsm($1, EndStr);
1744 free($1);
1745
1746 if (AsmSoFar.empty())
1747 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
1748 else
1749 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
Reid Spencer713eedc2006-08-18 08:43:06 +00001750 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001751};
1752
1753BigOrLittle : BIG { $$ = Module::BigEndian; };
1754BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1755
1756TargetDefinition : ENDIAN '=' BigOrLittle {
1757 CurModule.CurrentModule->setEndianness($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001758 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001759 }
1760 | POINTERSIZE '=' EUINT64VAL {
1761 if ($3 == 32)
1762 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1763 else if ($3 == 64)
1764 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1765 else
Reid Spencer713eedc2006-08-18 08:43:06 +00001766 GEN_ERROR("Invalid pointer size: '" + utostr($3) + "'!");
1767 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001768 }
1769 | TRIPLE '=' STRINGCONSTANT {
1770 CurModule.CurrentModule->setTargetTriple($3);
1771 free($3);
John Criswell6af0b122006-10-24 19:09:48 +00001772 }
Chris Lattner7d1d0342006-10-22 06:08:13 +00001773 | DATALAYOUT '=' STRINGCONSTANT {
Owen Anderson85690f32006-10-18 02:21:48 +00001774 CurModule.CurrentModule->setDataLayout($3);
1775 free($3);
Owen Anderson85690f32006-10-18 02:21:48 +00001776 };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001777
1778LibrariesDefinition : '[' LibList ']';
1779
1780LibList : LibList ',' STRINGCONSTANT {
1781 CurModule.CurrentModule->addLibrary($3);
1782 free($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001783 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001784 }
1785 | STRINGCONSTANT {
1786 CurModule.CurrentModule->addLibrary($1);
1787 free($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001788 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001789 }
1790 | /* empty: end of list */ {
Reid Spencer713eedc2006-08-18 08:43:06 +00001791 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001792 }
1793 ;
1794
1795//===----------------------------------------------------------------------===//
1796// Rules to match Function Headers
1797//===----------------------------------------------------------------------===//
1798
1799Name : VAR_ID | STRINGCONSTANT;
1800OptName : Name | /*empty*/ { $$ = 0; };
1801
1802ArgVal : Types OptName {
Reid Spencere2c32da2006-12-03 05:46:11 +00001803 if (*$1 == Type::VoidTy)
Reid Spencer713eedc2006-08-18 08:43:06 +00001804 GEN_ERROR("void typed arguments are invalid!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001805 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001806 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001807};
1808
1809ArgListH : ArgListH ',' ArgVal {
1810 $$ = $1;
1811 $1->push_back(*$3);
1812 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001813 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001814 }
1815 | ArgVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00001816 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001817 $$->push_back(*$1);
1818 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001819 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001820 };
1821
1822ArgList : ArgListH {
1823 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001824 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001825 }
1826 | ArgListH ',' DOTDOTDOT {
1827 $$ = $1;
Reid Spencere2c32da2006-12-03 05:46:11 +00001828 $$->push_back(std::pair<PATypeHolder*,
1829 char*>(new PATypeHolder(Type::VoidTy), 0));
Reid Spencer713eedc2006-08-18 08:43:06 +00001830 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001831 }
1832 | DOTDOTDOT {
Reid Spencere2c32da2006-12-03 05:46:11 +00001833 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1834 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Reid Spencer713eedc2006-08-18 08:43:06 +00001835 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001836 }
1837 | /* empty */ {
1838 $$ = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00001839 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001840 };
1841
1842FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
1843 OptSection OptAlign {
1844 UnEscapeLexed($3);
1845 std::string FunctionName($3);
1846 free($3); // Free strdup'd memory!
1847
Reid Spencere2c32da2006-12-03 05:46:11 +00001848 if (!(*$2)->isFirstClassType() && *$2 != Type::VoidTy)
Reid Spencer713eedc2006-08-18 08:43:06 +00001849 GEN_ERROR("LLVM functions cannot return aggregate types!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001850
1851 std::vector<const Type*> ParamTypeList;
1852 if ($5) { // If there are arguments...
Reid Spencere2c32da2006-12-03 05:46:11 +00001853 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001854 I != $5->end(); ++I)
Reid Spencere2c32da2006-12-03 05:46:11 +00001855 ParamTypeList.push_back(I->first->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001856 }
1857
1858 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1859 if (isVarArg) ParamTypeList.pop_back();
1860
Reid Spencere2c32da2006-12-03 05:46:11 +00001861 const FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001862 const PointerType *PFT = PointerType::get(FT);
Reid Spencere2c32da2006-12-03 05:46:11 +00001863 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001864
1865 ValID ID;
1866 if (!FunctionName.empty()) {
1867 ID = ValID::create((char*)FunctionName.c_str());
1868 } else {
1869 ID = ValID::create((int)CurModule.Values[PFT].size());
1870 }
1871
1872 Function *Fn = 0;
1873 // See if this function was forward referenced. If so, recycle the object.
1874 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
1875 // Move the function to the end of the list, from whereever it was
1876 // previously inserted.
1877 Fn = cast<Function>(FWRef);
1878 CurModule.CurrentModule->getFunctionList().remove(Fn);
1879 CurModule.CurrentModule->getFunctionList().push_back(Fn);
1880 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
1881 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1882 // If this is the case, either we need to be a forward decl, or it needs
1883 // to be.
1884 if (!CurFun.isDeclare && !Fn->isExternal())
Reid Spencer713eedc2006-08-18 08:43:06 +00001885 GEN_ERROR("Redefinition of function '" + FunctionName + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001886
1887 // Make sure to strip off any argument names so we can't get conflicts.
1888 if (Fn->isExternal())
1889 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
1890 AI != AE; ++AI)
1891 AI->setName("");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001892 } else { // Not already defined?
1893 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1894 CurModule.CurrentModule);
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001895
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001896 InsertValue(Fn, CurModule.Values);
1897 }
1898
1899 CurFun.FunctionStart(Fn);
Anton Korobeynikov0ab01ff2006-09-17 13:06:18 +00001900
1901 if (CurFun.isDeclare) {
1902 // If we have declaration, always overwrite linkage. This will allow us to
1903 // correctly handle cases, when pointer to function is passed as argument to
1904 // another function.
1905 Fn->setLinkage(CurFun.Linkage);
1906 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001907 Fn->setCallingConv($1);
1908 Fn->setAlignment($8);
1909 if ($7) {
1910 Fn->setSection($7);
1911 free($7);
1912 }
1913
1914 // Add all of the arguments we parsed to the function...
1915 if ($5) { // Is null if empty...
1916 if (isVarArg) { // Nuke the last entry
Reid Spencere2c32da2006-12-03 05:46:11 +00001917 assert($5->back().first->get() == Type::VoidTy && $5->back().second == 0&&
1918 "Not a varargs marker!");
1919 delete $5->back().first;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001920 $5->pop_back(); // Delete the last entry
1921 }
1922 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spencere2c32da2006-12-03 05:46:11 +00001923 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001924 I != $5->end(); ++I, ++ArgIt) {
Reid Spencere2c32da2006-12-03 05:46:11 +00001925 delete I->first; // Delete the typeholder...
1926
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001927 setValueName(ArgIt, I->second); // Insert arg into symtab...
Reid Spencer309080a2006-09-28 19:28:24 +00001928 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001929 InsertValue(ArgIt);
1930 }
Reid Spencere2c32da2006-12-03 05:46:11 +00001931
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001932 delete $5; // We're now done with the argument list
1933 }
Reid Spencer713eedc2006-08-18 08:43:06 +00001934 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001935};
1936
1937BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1938
1939FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
1940 $$ = CurFun.CurrentFunction;
1941
1942 // Make sure that we keep track of the linkage type even if there was a
1943 // previous "declare".
1944 $$->setLinkage($1);
1945};
1946
1947END : ENDTOK | '}'; // Allow end of '}' to end a function
1948
1949Function : BasicBlockList END {
1950 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001951 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001952};
1953
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001954FnDeclareLinkage: /*default*/ |
Chris Lattner6aee6f22006-11-08 05:58:47 +00001955 DLLIMPORT { CurFun.Linkage = GlobalValue::DLLImportLinkage; } |
Reid Spencerd5e19442006-12-01 00:33:46 +00001956 EXTERN_WEAK { CurFun.Linkage = GlobalValue::ExternalWeakLinkage; };
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001957
1958FunctionProto : DECLARE { CurFun.isDeclare = true; } FnDeclareLinkage FunctionHeaderH {
1959 $$ = CurFun.CurrentFunction;
1960 CurFun.FunctionDone();
1961 CHECK_FOR_ERROR
1962 };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001963
1964//===----------------------------------------------------------------------===//
1965// Rules to match Basic Blocks
1966//===----------------------------------------------------------------------===//
1967
1968OptSideEffect : /* empty */ {
1969 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00001970 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001971 }
1972 | SIDEEFFECT {
1973 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00001974 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001975 };
1976
1977ConstValueRef : ESINT64VAL { // A reference to a direct constant
1978 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001979 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001980 }
1981 | EUINT64VAL {
1982 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001983 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001984 }
1985 | FPVAL { // Perhaps it's an FP constant?
1986 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001987 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001988 }
1989 | TRUETOK {
Chris Lattner6ab03f62006-09-28 23:35:22 +00001990 $$ = ValID::create(ConstantBool::getTrue());
Reid Spencer713eedc2006-08-18 08:43:06 +00001991 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001992 }
1993 | FALSETOK {
Chris Lattner6ab03f62006-09-28 23:35:22 +00001994 $$ = ValID::create(ConstantBool::getFalse());
Reid Spencer713eedc2006-08-18 08:43:06 +00001995 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001996 }
1997 | NULL_TOK {
1998 $$ = ValID::createNull();
Reid Spencer713eedc2006-08-18 08:43:06 +00001999 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002000 }
2001 | UNDEF {
2002 $$ = ValID::createUndef();
Reid Spencer713eedc2006-08-18 08:43:06 +00002003 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002004 }
2005 | ZEROINITIALIZER { // A vector zero constant.
2006 $$ = ValID::createZeroInit();
Reid Spencer713eedc2006-08-18 08:43:06 +00002007 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002008 }
2009 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencere2c32da2006-12-03 05:46:11 +00002010 const Type *ETy = (*$2)[0]->getType();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002011 int NumElements = $2->size();
2012
2013 PackedType* pt = PackedType::get(ETy, NumElements);
2014 PATypeHolder* PTy = new PATypeHolder(
Reid Spencere2c32da2006-12-03 05:46:11 +00002015 HandleUpRefs(
2016 PackedType::get(
2017 ETy,
2018 NumElements)
2019 )
2020 );
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002021
2022 // Verify all elements are correct type!
2023 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencere2c32da2006-12-03 05:46:11 +00002024 if (ETy != (*$2)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002025 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002026 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00002027 (*$2)[i]->getType()->getDescription() + "'.");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002028 }
2029
Reid Spencere2c32da2006-12-03 05:46:11 +00002030 $$ = ValID::create(ConstantPacked::get(pt, *$2));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002031 delete PTy; delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002032 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002033 }
2034 | ConstExpr {
Reid Spencere2c32da2006-12-03 05:46:11 +00002035 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002036 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002037 }
2038 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
2039 char *End = UnEscapeLexed($3, true);
2040 std::string AsmStr = std::string($3, End);
2041 End = UnEscapeLexed($5, true);
2042 std::string Constraints = std::string($5, End);
2043 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
2044 free($3);
2045 free($5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002046 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002047 };
2048
2049// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2050// another value.
2051//
2052SymbolicValueRef : INTVAL { // Is it an integer reference...?
2053 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002054 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002055 }
2056 | Name { // Is it a named reference...?
2057 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002058 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002059 };
2060
2061// ValueRef - A reference to a definition... either constant or symbolic
2062ValueRef : SymbolicValueRef | ConstValueRef;
2063
2064
2065// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2066// type immediately preceeds the value reference, and allows complex constant
2067// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2068ResolvedVal : Types ValueRef {
Reid Spencere2c32da2006-12-03 05:46:11 +00002069 $$ = getVal(*$1, $2); delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002070 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002071 };
2072
2073BasicBlockList : BasicBlockList BasicBlock {
2074 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002075 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002076 }
2077 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
2078 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002079 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002080 };
2081
2082
2083// Basic blocks are terminated by branching instructions:
2084// br, br/cc, switch, ret
2085//
2086BasicBlock : InstructionList OptAssign BBTerminatorInst {
2087 setValueName($3, $2);
Reid Spencer309080a2006-09-28 19:28:24 +00002088 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002089 InsertValue($3);
2090
2091 $1->getInstList().push_back($3);
2092 InsertValue($1);
2093 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002094 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002095 };
2096
2097InstructionList : InstructionList Inst {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002098 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2099 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2100 if (CI2->getParent() == 0)
2101 $1->getInstList().push_back(CI2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002102 $1->getInstList().push_back($2);
2103 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002104 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002105 }
2106 | /* empty */ {
Reid Spencer27642192006-12-05 23:29:42 +00002107 $$ = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Reid Spencer309080a2006-09-28 19:28:24 +00002108 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002109
2110 // Make sure to move the basic block to the correct location in the
2111 // function, instead of leaving it inserted wherever it was first
2112 // referenced.
2113 Function::BasicBlockListType &BBL =
2114 CurFun.CurrentFunction->getBasicBlockList();
2115 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer713eedc2006-08-18 08:43:06 +00002116 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002117 }
2118 | LABELSTR {
Reid Spencer27642192006-12-05 23:29:42 +00002119 $$ = getBBVal(ValID::create($1), true);
Reid Spencer309080a2006-09-28 19:28:24 +00002120 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002121
2122 // Make sure to move the basic block to the correct location in the
2123 // function, instead of leaving it inserted wherever it was first
2124 // referenced.
2125 Function::BasicBlockListType &BBL =
2126 CurFun.CurrentFunction->getBasicBlockList();
2127 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer713eedc2006-08-18 08:43:06 +00002128 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002129 };
2130
2131BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencere2c32da2006-12-03 05:46:11 +00002132 $$ = new ReturnInst($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00002133 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002134 }
2135 | RET VOID { // Return with no result...
2136 $$ = new ReturnInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002137 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002138 }
2139 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer309080a2006-09-28 19:28:24 +00002140 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002141 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002142 $$ = new BranchInst(tmpBB);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002143 } // Conditional Branch...
2144 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencer309080a2006-09-28 19:28:24 +00002145 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002146 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002147 BasicBlock* tmpBBB = getBBVal($9);
2148 CHECK_FOR_ERROR
2149 Value* tmpVal = getVal(Type::BoolTy, $3);
2150 CHECK_FOR_ERROR
2151 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002152 }
2153 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencere2c32da2006-12-03 05:46:11 +00002154 Value* tmpVal = getVal($2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002155 CHECK_FOR_ERROR
2156 BasicBlock* tmpBB = getBBVal($6);
2157 CHECK_FOR_ERROR
2158 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002159 $$ = S;
2160
2161 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2162 E = $8->end();
2163 for (; I != E; ++I) {
2164 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2165 S->addCase(CI, I->second);
2166 else
Reid Spencer713eedc2006-08-18 08:43:06 +00002167 GEN_ERROR("Switch case is constant, but not a simple integer!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002168 }
2169 delete $8;
Reid Spencer713eedc2006-08-18 08:43:06 +00002170 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002171 }
2172 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencere2c32da2006-12-03 05:46:11 +00002173 Value* tmpVal = getVal($2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002174 CHECK_FOR_ERROR
2175 BasicBlock* tmpBB = getBBVal($6);
2176 CHECK_FOR_ERROR
2177 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002178 $$ = S;
Reid Spencer713eedc2006-08-18 08:43:06 +00002179 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002180 }
2181 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
2182 TO LABEL ValueRef UNWIND LABEL ValueRef {
2183 const PointerType *PFTy;
2184 const FunctionType *Ty;
2185
Reid Spencere2c32da2006-12-03 05:46:11 +00002186 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002187 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2188 // Pull out the types of all of the arguments...
2189 std::vector<const Type*> ParamTypes;
2190 if ($6) {
Reid Spencere2c32da2006-12-03 05:46:11 +00002191 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002192 I != E; ++I)
Reid Spencere2c32da2006-12-03 05:46:11 +00002193 ParamTypes.push_back((*I)->getType());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002194 }
2195
2196 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2197 if (isVarArg) ParamTypes.pop_back();
2198
Reid Spencere2c32da2006-12-03 05:46:11 +00002199 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002200 PFTy = PointerType::get(Ty);
2201 }
2202
2203 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer309080a2006-09-28 19:28:24 +00002204 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002205 BasicBlock *Normal = getBBVal($10);
Reid Spencer309080a2006-09-28 19:28:24 +00002206 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002207 BasicBlock *Except = getBBVal($13);
Reid Spencer309080a2006-09-28 19:28:24 +00002208 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002209
2210 // Create the call node...
2211 if (!$6) { // Has no arguments?
2212 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
2213 } else { // Has arguments?
2214 // Loop through FunctionType's arguments and ensure they are specified
2215 // correctly!
2216 //
2217 FunctionType::param_iterator I = Ty->param_begin();
2218 FunctionType::param_iterator E = Ty->param_end();
Reid Spencere2c32da2006-12-03 05:46:11 +00002219 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002220
Reid Spencere2c32da2006-12-03 05:46:11 +00002221 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
2222 if ((*ArgI)->getType() != *I)
2223 GEN_ERROR("Parameter " +(*ArgI)->getName()+ " is not of type '" +
2224 (*I)->getDescription() + "'!");
2225
2226 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
2227 GEN_ERROR("Invalid number of parameters detected!");
2228
2229 $$ = new InvokeInst(V, Normal, Except, *$6);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002230 }
2231 cast<InvokeInst>($$)->setCallingConv($2);
2232
Reid Spencere2c32da2006-12-03 05:46:11 +00002233 delete $3;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002234 delete $6;
Reid Spencer713eedc2006-08-18 08:43:06 +00002235 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002236 }
2237 | UNWIND {
2238 $$ = new UnwindInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002239 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002240 }
2241 | UNREACHABLE {
2242 $$ = new UnreachableInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002243 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002244 };
2245
2246
2247
2248JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2249 $$ = $1;
Reid Spencere2c32da2006-12-03 05:46:11 +00002250 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Reid Spencer309080a2006-09-28 19:28:24 +00002251 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002252 if (V == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002253 GEN_ERROR("May only switch on a constant pool value!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002254
Reid Spencer309080a2006-09-28 19:28:24 +00002255 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002256 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002257 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002258 }
2259 | IntType ConstValueRef ',' LABEL ValueRef {
2260 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencere2c32da2006-12-03 05:46:11 +00002261 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Reid Spencer309080a2006-09-28 19:28:24 +00002262 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002263
2264 if (V == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002265 GEN_ERROR("May only switch on a constant pool value!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002266
Reid Spencer309080a2006-09-28 19:28:24 +00002267 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002268 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002269 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002270 };
2271
2272Inst : OptAssign InstVal {
2273 // Is this definition named?? if so, assign the name...
2274 setValueName($2, $1);
Reid Spencer309080a2006-09-28 19:28:24 +00002275 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002276 InsertValue($2);
2277 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002278 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002279};
2280
2281PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
2282 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencere2c32da2006-12-03 05:46:11 +00002283 Value* tmpVal = getVal(*$1, $3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002284 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002285 BasicBlock* tmpBB = getBBVal($5);
2286 CHECK_FOR_ERROR
2287 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencere2c32da2006-12-03 05:46:11 +00002288 delete $1;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002289 }
2290 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2291 $$ = $1;
Reid Spencer309080a2006-09-28 19:28:24 +00002292 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002293 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002294 BasicBlock* tmpBB = getBBVal($6);
2295 CHECK_FOR_ERROR
2296 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002297 };
2298
2299
2300ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Reid Spencere2c32da2006-12-03 05:46:11 +00002301 $$ = new std::vector<Value*>();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002302 $$->push_back($1);
2303 }
2304 | ValueRefList ',' ResolvedVal {
2305 $$ = $1;
2306 $1->push_back($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002307 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002308 };
2309
2310// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Reid Spencere2c32da2006-12-03 05:46:11 +00002311ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002312
2313OptTailCall : TAIL CALL {
2314 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002315 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002316 }
2317 | CALL {
2318 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002319 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002320 };
2321
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002322InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencere2c32da2006-12-03 05:46:11 +00002323 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
2324 !isa<PackedType>((*$2).get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002325 GEN_ERROR(
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002326 "Arithmetic operator requires integer, FP, or packed operands!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002327 if (isa<PackedType>((*$2).get()) &&
2328 ($1 == Instruction::URem ||
2329 $1 == Instruction::SRem ||
2330 $1 == Instruction::FRem))
Reid Spencerde46e482006-11-02 20:25:50 +00002331 GEN_ERROR("U/S/FRem not supported on packed types!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002332 Value* val1 = getVal(*$2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002333 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002334 Value* val2 = getVal(*$2, $5);
Reid Spencer309080a2006-09-28 19:28:24 +00002335 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002336 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002337 if ($$ == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002338 GEN_ERROR("binary operator returned null!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002339 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002340 }
2341 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencere2c32da2006-12-03 05:46:11 +00002342 if (!(*$2)->isIntegral()) {
2343 if (!isa<PackedType>($2->get()) ||
2344 !cast<PackedType>($2->get())->getElementType()->isIntegral())
Reid Spencer713eedc2006-08-18 08:43:06 +00002345 GEN_ERROR("Logical operator requires integral operands!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002346 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002347 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002348 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002349 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer309080a2006-09-28 19:28:24 +00002350 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002351 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002352 if ($$ == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002353 GEN_ERROR("binary operator returned null!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002354 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002355 }
2356 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencere2c32da2006-12-03 05:46:11 +00002357 if(isa<PackedType>((*$2).get())) {
Reid Spencer713eedc2006-08-18 08:43:06 +00002358 GEN_ERROR(
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002359 "PackedTypes currently not supported in setcc instructions!");
2360 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002361 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002362 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002363 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer309080a2006-09-28 19:28:24 +00002364 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002365 $$ = new SetCondInst($1, tmpVal1, tmpVal2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002366 if ($$ == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002367 GEN_ERROR("binary operator returned null!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002368 delete $2;
2369 }
2370 | ICMP IPredicates Types ValueRef ',' ValueRef {
2371 if (isa<PackedType>((*$3).get()))
2372 GEN_ERROR("Packed types not supported by icmp instruction");
2373 Value* tmpVal1 = getVal(*$3, $4);
2374 CHECK_FOR_ERROR
2375 Value* tmpVal2 = getVal(*$3, $6);
2376 CHECK_FOR_ERROR
2377 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2378 if ($$ == 0)
2379 GEN_ERROR("icmp operator returned null!");
2380 }
2381 | FCMP FPredicates Types ValueRef ',' ValueRef {
2382 if (isa<PackedType>((*$3).get()))
2383 GEN_ERROR("Packed types not supported by fcmp instruction");
2384 Value* tmpVal1 = getVal(*$3, $4);
2385 CHECK_FOR_ERROR
2386 Value* tmpVal2 = getVal(*$3, $6);
2387 CHECK_FOR_ERROR
2388 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2389 if ($$ == 0)
2390 GEN_ERROR("fcmp operator returned null!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002391 }
2392 | NOT ResolvedVal {
Bill Wendlingf3baad32006-12-07 01:30:32 +00002393 cerr << "WARNING: Use of eliminated 'not' instruction:"
2394 << " Replacing with 'xor'.\n";
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002395
Reid Spencere2c32da2006-12-03 05:46:11 +00002396 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002397 if (Ones == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002398 GEN_ERROR("Expected integral type for not instruction!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002399
Reid Spencere2c32da2006-12-03 05:46:11 +00002400 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002401 if ($$ == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002402 GEN_ERROR("Could not create a xor instruction!");
2403 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002404 }
2405 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002406 if ($4->getType() != Type::UByteTy)
Reid Spencer713eedc2006-08-18 08:43:06 +00002407 GEN_ERROR("Shift amount must be ubyte!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002408 if (!$2->getType()->isInteger())
Reid Spencer713eedc2006-08-18 08:43:06 +00002409 GEN_ERROR("Shift constant expression requires integer operand!");
Reid Spencerfdff9382006-11-08 06:47:33 +00002410 CHECK_FOR_ERROR;
Reid Spencere2c32da2006-12-03 05:46:11 +00002411 $$ = new ShiftInst($1, $2, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002412 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002413 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002414 | CastOps ResolvedVal TO Types {
Reid Spencere2c32da2006-12-03 05:46:11 +00002415 Value* Val = $2;
2416 const Type* Ty = $4->get();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002417 if (!Val->getType()->isFirstClassType())
2418 GEN_ERROR("cast from a non-primitive type: '" +
2419 Val->getType()->getDescription() + "'!");
2420 if (!Ty->isFirstClassType())
2421 GEN_ERROR("cast to a non-primitive type: '" + Ty->getDescription() +"'!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002422 $$ = CastInst::create($1, $2, $4->get());
2423 delete $4;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002424 }
2425 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002426 if ($2->getType() != Type::BoolTy)
Reid Spencer713eedc2006-08-18 08:43:06 +00002427 GEN_ERROR("select condition must be boolean!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002428 if ($4->getType() != $6->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002429 GEN_ERROR("select value types should match!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002430 $$ = new SelectInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002431 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002432 }
2433 | VAARG ResolvedVal ',' Types {
Reid Spencere2c32da2006-12-03 05:46:11 +00002434 $$ = new VAArgInst($2, *$4);
2435 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00002436 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002437 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002438 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002439 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencer713eedc2006-08-18 08:43:06 +00002440 GEN_ERROR("Invalid extractelement operands!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002441 $$ = new ExtractElementInst($2, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002442 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002443 }
2444 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002445 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencer713eedc2006-08-18 08:43:06 +00002446 GEN_ERROR("Invalid insertelement operands!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002447 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002448 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002449 }
Chris Lattner9ff96a72006-04-08 01:18:56 +00002450 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002451 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencer713eedc2006-08-18 08:43:06 +00002452 GEN_ERROR("Invalid shufflevector operands!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002453 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002454 CHECK_FOR_ERROR
Chris Lattner9ff96a72006-04-08 01:18:56 +00002455 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002456 | PHI_TOK PHIList {
2457 const Type *Ty = $2->front().first->getType();
2458 if (!Ty->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002459 GEN_ERROR("PHI node operands must be of first class type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002460 $$ = new PHINode(Ty);
2461 ((PHINode*)$$)->reserveOperandSpace($2->size());
2462 while ($2->begin() != $2->end()) {
2463 if ($2->front().first->getType() != Ty)
Reid Spencer713eedc2006-08-18 08:43:06 +00002464 GEN_ERROR("All elements of a PHI node must be of the same type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002465 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
2466 $2->pop_front();
2467 }
2468 delete $2; // Free the list...
Reid Spencer713eedc2006-08-18 08:43:06 +00002469 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002470 }
2471 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002472 const PointerType *PFTy = 0;
2473 const FunctionType *Ty = 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002474
Reid Spencere2c32da2006-12-03 05:46:11 +00002475 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002476 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2477 // Pull out the types of all of the arguments...
2478 std::vector<const Type*> ParamTypes;
2479 if ($6) {
Reid Spencere2c32da2006-12-03 05:46:11 +00002480 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002481 I != E; ++I)
Reid Spencere2c32da2006-12-03 05:46:11 +00002482 ParamTypes.push_back((*I)->getType());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002483 }
2484
2485 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2486 if (isVarArg) ParamTypes.pop_back();
2487
Reid Spencere2c32da2006-12-03 05:46:11 +00002488 if (!(*$3)->isFirstClassType() && *$3 != Type::VoidTy)
Reid Spencer713eedc2006-08-18 08:43:06 +00002489 GEN_ERROR("LLVM functions cannot return aggregate types!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002490
Reid Spencere2c32da2006-12-03 05:46:11 +00002491 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002492 PFTy = PointerType::get(Ty);
2493 }
2494
2495 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer309080a2006-09-28 19:28:24 +00002496 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002497
2498 // Create the call node...
2499 if (!$6) { // Has no arguments?
2500 // Make sure no arguments is a good thing!
2501 if (Ty->getNumParams() != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002502 GEN_ERROR("No arguments passed to a function that "
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002503 "expects arguments!");
2504
2505 $$ = new CallInst(V, std::vector<Value*>());
2506 } else { // Has arguments?
2507 // Loop through FunctionType's arguments and ensure they are specified
2508 // correctly!
2509 //
2510 FunctionType::param_iterator I = Ty->param_begin();
2511 FunctionType::param_iterator E = Ty->param_end();
Reid Spencere2c32da2006-12-03 05:46:11 +00002512 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002513
Reid Spencere2c32da2006-12-03 05:46:11 +00002514 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
2515 if ((*ArgI)->getType() != *I)
2516 GEN_ERROR("Parameter " +(*ArgI)->getName()+ " is not of type '" +
2517 (*I)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002518
2519 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002520 GEN_ERROR("Invalid number of parameters detected!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002521
Reid Spencere2c32da2006-12-03 05:46:11 +00002522 $$ = new CallInst(V, *$6);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002523 }
2524 cast<CallInst>($$)->setTailCall($1);
2525 cast<CallInst>($$)->setCallingConv($2);
Reid Spencere2c32da2006-12-03 05:46:11 +00002526 delete $3;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002527 delete $6;
Reid Spencer713eedc2006-08-18 08:43:06 +00002528 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002529 }
2530 | MemoryInst {
2531 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002532 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002533 };
2534
2535
2536// IndexList - List of indices for GEP based instructions...
2537IndexList : ',' ValueRefList {
2538 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002539 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002540 } | /* empty */ {
Reid Spencere2c32da2006-12-03 05:46:11 +00002541 $$ = new std::vector<Value*>();
Reid Spencer713eedc2006-08-18 08:43:06 +00002542 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002543 };
2544
2545OptVolatile : VOLATILE {
2546 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002547 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002548 }
2549 | /* empty */ {
2550 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002551 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002552 };
2553
2554
2555
2556MemoryInst : MALLOC Types OptCAlign {
Reid Spencere2c32da2006-12-03 05:46:11 +00002557 $$ = new MallocInst(*$2, 0, $3);
2558 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002559 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002560 }
2561 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencere2c32da2006-12-03 05:46:11 +00002562 Value* tmpVal = getVal($4, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002563 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002564 $$ = new MallocInst(*$2, tmpVal, $6);
2565 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002566 }
2567 | ALLOCA Types OptCAlign {
Reid Spencere2c32da2006-12-03 05:46:11 +00002568 $$ = new AllocaInst(*$2, 0, $3);
2569 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002570 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002571 }
2572 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencere2c32da2006-12-03 05:46:11 +00002573 Value* tmpVal = getVal($4, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002574 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002575 $$ = new AllocaInst(*$2, tmpVal, $6);
2576 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002577 }
2578 | FREE ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002579 if (!isa<PointerType>($2->getType()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002580 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencere2c32da2006-12-03 05:46:11 +00002581 $2->getType()->getDescription() + "!");
2582 $$ = new FreeInst($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00002583 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002584 }
2585
2586 | OptVolatile LOAD Types ValueRef {
Reid Spencere2c32da2006-12-03 05:46:11 +00002587 if (!isa<PointerType>($3->get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002588 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencere2c32da2006-12-03 05:46:11 +00002589 (*$3)->getDescription());
2590 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002591 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencere2c32da2006-12-03 05:46:11 +00002592 (*$3)->getDescription());
2593 Value* tmpVal = getVal(*$3, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002594 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002595 $$ = new LoadInst(tmpVal, "", $1);
Reid Spencere2c32da2006-12-03 05:46:11 +00002596 delete $3;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002597 }
2598 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
Reid Spencere2c32da2006-12-03 05:46:11 +00002599 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002600 if (!PT)
Reid Spencer713eedc2006-08-18 08:43:06 +00002601 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencere2c32da2006-12-03 05:46:11 +00002602 (*$5)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002603 const Type *ElTy = PT->getElementType();
Reid Spencere2c32da2006-12-03 05:46:11 +00002604 if (ElTy != $3->getType())
2605 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002606 "' into space of type '" + ElTy->getDescription() + "'!");
2607
Reid Spencere2c32da2006-12-03 05:46:11 +00002608 Value* tmpVal = getVal(*$5, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002609 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002610 $$ = new StoreInst($3, tmpVal, $1);
2611 delete $5;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002612 }
2613 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencere2c32da2006-12-03 05:46:11 +00002614 if (!isa<PointerType>($2->get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002615 GEN_ERROR("getelementptr insn requires pointer operand!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002616
Reid Spencere2c32da2006-12-03 05:46:11 +00002617 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Reid Spencer713eedc2006-08-18 08:43:06 +00002618 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00002619 (*$2)->getDescription()+ "'!");
2620 Value* tmpVal = getVal(*$2, $3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002621 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002622 $$ = new GetElementPtrInst(tmpVal, *$4);
2623 delete $2;
Reid Spencer309080a2006-09-28 19:28:24 +00002624 delete $4;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002625 };
2626
2627
2628%%
Reid Spencer713eedc2006-08-18 08:43:06 +00002629
2630void llvm::GenerateError(const std::string &message, int LineNo) {
2631 if (LineNo == -1) LineNo = llvmAsmlineno;
2632 // TODO: column number in exception
2633 if (TheParseError)
2634 TheParseError->setError(CurFilename, message, LineNo);
2635 TriggerError = 1;
2636}
2637
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002638int yyerror(const char *ErrorMsg) {
2639 std::string where
2640 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2641 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
2642 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
2643 if (yychar == YYEMPTY || yychar == 0)
2644 errMsg += "end-of-file.";
2645 else
2646 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Reid Spencer713eedc2006-08-18 08:43:06 +00002647 GenerateError(errMsg);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002648 return 0;
2649}