blob: 52d8847d32965f4b58f700c3c9fdc381ff284c7e [file] [log] [blame]
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the bison parser for LLVM assembly languages files.
11//
12//===----------------------------------------------------------------------===//
13
14%{
15#include "ParserInternals.h"
16#include "llvm/CallingConv.h"
17#include "llvm/InlineAsm.h"
18#include "llvm/Instructions.h"
19#include "llvm/Module.h"
20#include "llvm/SymbolTable.h"
Chris Lattnerf20e61f2006-02-15 07:22:58 +000021#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer42f0cbe2006-12-31 05:40:51 +000022#include "llvm/Support/CommandLine.h"
Chris Lattnerf20e61f2006-02-15 07:22:58 +000023#include "llvm/ADT/STLExtras.h"
24#include "llvm/Support/MathExtras.h"
Reid Spencerd5e19442006-12-01 00:33:46 +000025#include "llvm/Support/Streams.h"
Chris Lattnerf20e61f2006-02-15 07:22:58 +000026#include <algorithm>
Chris Lattnerf20e61f2006-02-15 07:22:58 +000027#include <list>
28#include <utility>
Reid Spencer42f0cbe2006-12-31 05:40:51 +000029#ifndef NDEBUG
30#define YYDEBUG 1
31#endif
Chris Lattnerf20e61f2006-02-15 07:22:58 +000032
Reid Spencerb50974a2006-08-18 17:32:55 +000033// The following is a gross hack. In order to rid the libAsmParser library of
34// exceptions, we have to have a way of getting the yyparse function to go into
35// an error situation. So, whenever we want an error to occur, the GenerateError
36// function (see bottom of file) sets TriggerError. Then, at the end of each
37// production in the grammer we use CHECK_FOR_ERROR which will invoke YYERROR
38// (a goto) to put YACC in error state. Furthermore, several calls to
39// GenerateError are made from inside productions and they must simulate the
40// previous exception behavior by exiting the production immediately. We have
41// replaced these with the GEN_ERROR macro which calls GeneratError and then
42// immediately invokes YYERROR. This would be so much cleaner if it was a
43// recursive descent parser.
Reid Spencer713eedc2006-08-18 08:43:06 +000044static bool TriggerError = false;
Reid Spencerff359002006-10-09 17:36:59 +000045#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYABORT; } }
Reid Spencer713eedc2006-08-18 08:43:06 +000046#define GEN_ERROR(msg) { GenerateError(msg); YYERROR; }
47
Chris Lattnerf20e61f2006-02-15 07:22:58 +000048int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
49int yylex(); // declaration" of xxx warnings.
50int yyparse();
51
52namespace llvm {
53 std::string CurFilename;
Reid Spencer42f0cbe2006-12-31 05:40:51 +000054#if YYDEBUG
55static cl::opt<bool>
56Debug("debug-yacc", cl::desc("Print yacc debug state changes"),
57 cl::Hidden, cl::init(false));
58#endif
Chris Lattnerf20e61f2006-02-15 07:22:58 +000059}
60using namespace llvm;
61
62static Module *ParserResult;
63
64// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
65// relating to upreferences in the input stream.
66//
67//#define DEBUG_UPREFS 1
68#ifdef DEBUG_UPREFS
Bill Wendlingf3baad32006-12-07 01:30:32 +000069#define UR_OUT(X) cerr << X
Chris Lattnerf20e61f2006-02-15 07:22:58 +000070#else
71#define UR_OUT(X)
72#endif
73
74#define YYERROR_VERBOSE 1
75
Chris Lattnerf20e61f2006-02-15 07:22:58 +000076static GlobalVariable *CurGV;
77
78
79// This contains info used when building the body of a function. It is
80// destroyed when the function is completed.
81//
82typedef std::vector<Value *> ValueList; // Numbered defs
Reid Spencer42f0cbe2006-12-31 05:40:51 +000083
Chris Lattnerf20e61f2006-02-15 07:22:58 +000084static void
85ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
86 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
87
88static struct PerModuleInfo {
89 Module *CurrentModule;
90 std::map<const Type *, ValueList> Values; // Module level numbered definitions
91 std::map<const Type *,ValueList> LateResolveValues;
Reid Spencer55f1fbe2006-11-28 07:29:44 +000092 std::vector<PATypeHolder> Types;
93 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattnerf20e61f2006-02-15 07:22:58 +000094
95 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Chris Lattner7aa45902006-06-21 16:53:00 +000096 /// how they were referenced and on which line of the input they came from so
Chris Lattnerf20e61f2006-02-15 07:22:58 +000097 /// that we can resolve them later and print error messages as appropriate.
98 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
99
100 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
101 // references to global values. Global values may be referenced before they
102 // are defined, and if so, the temporary object that they represent is held
103 // here. This is used for forward references of GlobalValues.
104 //
105 typedef std::map<std::pair<const PointerType *,
106 ValID>, GlobalValue*> GlobalRefsType;
107 GlobalRefsType GlobalRefs;
108
109 void ModuleDone() {
110 // If we could not resolve some functions at function compilation time
111 // (calls to functions before they are defined), resolve them now... Types
112 // are resolved when the constant pool has been completely parsed.
113 //
114 ResolveDefinitions(LateResolveValues);
Reid Spencer309080a2006-09-28 19:28:24 +0000115 if (TriggerError)
116 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000117
118 // Check to make sure that all global value forward references have been
119 // resolved!
120 //
121 if (!GlobalRefs.empty()) {
122 std::string UndefinedReferences = "Unresolved global references exist:\n";
123
124 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
125 I != E; ++I) {
126 UndefinedReferences += " " + I->first.first->getDescription() + " " +
127 I->first.second.getName() + "\n";
128 }
Reid Spencer713eedc2006-08-18 08:43:06 +0000129 GenerateError(UndefinedReferences);
Reid Spencer309080a2006-09-28 19:28:24 +0000130 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000131 }
132
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000133 Values.clear(); // Clear out function local definitions
134 Types.clear();
135 CurrentModule = 0;
136 }
137
138 // GetForwardRefForGlobal - Check to see if there is a forward reference
139 // for this global. If so, remove it from the GlobalRefs map and return it.
140 // If not, just return null.
141 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
142 // Check to see if there is a forward reference to this global variable...
143 // if there is, eliminate it and patch the reference to use the new def'n.
144 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
145 GlobalValue *Ret = 0;
146 if (I != GlobalRefs.end()) {
147 Ret = I->second;
148 GlobalRefs.erase(I);
149 }
150 return Ret;
151 }
Reid Spencer87622ae2007-01-02 21:54:12 +0000152
153 bool TypeIsUnresolved(PATypeHolder* PATy) {
154 // If it isn't abstract, its resolved
155 const Type* Ty = PATy->get();
156 if (!Ty->isAbstract())
157 return false;
158 // Traverse the type looking for abstract types. If it isn't abstract then
159 // we don't need to traverse that leg of the type.
160 std::vector<const Type*> WorkList, SeenList;
161 WorkList.push_back(Ty);
162 while (!WorkList.empty()) {
163 const Type* Ty = WorkList.back();
164 SeenList.push_back(Ty);
165 WorkList.pop_back();
166 if (const OpaqueType* OpTy = dyn_cast<OpaqueType>(Ty)) {
167 // Check to see if this is an unresolved type
168 std::map<ValID, PATypeHolder>::iterator I = LateResolveTypes.begin();
169 std::map<ValID, PATypeHolder>::iterator E = LateResolveTypes.end();
170 for ( ; I != E; ++I) {
171 if (I->second.get() == OpTy)
172 return true;
173 }
174 } else if (const SequentialType* SeqTy = dyn_cast<SequentialType>(Ty)) {
175 const Type* TheTy = SeqTy->getElementType();
176 if (TheTy->isAbstract() && TheTy != Ty) {
177 std::vector<const Type*>::iterator I = SeenList.begin(),
178 E = SeenList.end();
179 for ( ; I != E; ++I)
180 if (*I == TheTy)
181 break;
182 if (I == E)
183 WorkList.push_back(TheTy);
184 }
185 } else if (const StructType* StrTy = dyn_cast<StructType>(Ty)) {
186 for (unsigned i = 0; i < StrTy->getNumElements(); ++i) {
187 const Type* TheTy = StrTy->getElementType(i);
188 if (TheTy->isAbstract() && TheTy != Ty) {
189 std::vector<const Type*>::iterator I = SeenList.begin(),
190 E = SeenList.end();
191 for ( ; I != E; ++I)
192 if (*I == TheTy)
193 break;
194 if (I == E)
195 WorkList.push_back(TheTy);
196 }
197 }
198 }
199 }
200 return false;
201 }
202
203
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000204} CurModule;
205
206static struct PerFunctionInfo {
207 Function *CurrentFunction; // Pointer to current function being created
208
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000209 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000210 std::map<const Type*, ValueList> LateResolveValues;
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000211 bool isDeclare; // Is this function a forward declararation?
212 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000213 GlobalValue::VisibilityTypes Visibility;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000214
215 /// BBForwardRefs - When we see forward references to basic blocks, keep
216 /// track of them here.
217 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
218 std::vector<BasicBlock*> NumberedBlocks;
219 unsigned NextBBNum;
220
221 inline PerFunctionInfo() {
222 CurrentFunction = 0;
223 isDeclare = false;
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000224 Linkage = GlobalValue::ExternalLinkage;
225 Visibility = GlobalValue::DefaultVisibility;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000226 }
227
228 inline void FunctionStart(Function *M) {
229 CurrentFunction = M;
230 NextBBNum = 0;
231 }
232
233 void FunctionDone() {
234 NumberedBlocks.clear();
235
236 // Any forward referenced blocks left?
Reid Spencer309080a2006-09-28 19:28:24 +0000237 if (!BBForwardRefs.empty()) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000238 GenerateError("Undefined reference to label " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000239 BBForwardRefs.begin()->first->getName());
Reid Spencer309080a2006-09-28 19:28:24 +0000240 return;
241 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000242
243 // Resolve all forward references now.
244 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
245
246 Values.clear(); // Clear out function local definitions
247 CurrentFunction = 0;
248 isDeclare = false;
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000249 Linkage = GlobalValue::ExternalLinkage;
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000250 Visibility = GlobalValue::DefaultVisibility;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000251 }
252} CurFun; // Info for the current function...
253
254static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
255
256
257//===----------------------------------------------------------------------===//
258// Code to handle definitions of all the types
259//===----------------------------------------------------------------------===//
260
261static int InsertValue(Value *V,
262 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
263 if (V->hasName()) return -1; // Is this a numbered definition?
264
265 // Yes, insert the value into the value table...
266 ValueList &List = ValueTab[V->getType()];
267 List.push_back(V);
268 return List.size()-1;
269}
270
271static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
272 switch (D.Type) {
273 case ValID::NumberVal: // Is it a numbered definition?
274 // Module constants occupy the lowest numbered slots...
275 if ((unsigned)D.Num < CurModule.Types.size())
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000276 return CurModule.Types[(unsigned)D.Num];
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000277 break;
278 case ValID::NameVal: // Is it a named definition?
279 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
280 D.destroy(); // Free old strdup'd memory...
281 return N;
282 }
283 break;
284 default:
Reid Spencer713eedc2006-08-18 08:43:06 +0000285 GenerateError("Internal parser error: Invalid symbol type reference!");
Reid Spencer309080a2006-09-28 19:28:24 +0000286 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000287 }
288
289 // If we reached here, we referenced either a symbol that we don't know about
290 // or an id number that hasn't been read yet. We may be referencing something
291 // forward, so just create an entry to be resolved later and get to it...
292 //
293 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
294
295
296 if (inFunctionScope()) {
Reid Spencer309080a2006-09-28 19:28:24 +0000297 if (D.Type == ValID::NameVal) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000298 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer309080a2006-09-28 19:28:24 +0000299 return 0;
300 } else {
Reid Spencer713eedc2006-08-18 08:43:06 +0000301 GenerateError("Reference to an undefined type: #" + itostr(D.Num));
Reid Spencer309080a2006-09-28 19:28:24 +0000302 return 0;
303 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000304 }
305
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000306 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000307 if (I != CurModule.LateResolveTypes.end())
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000308 return I->second;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000309
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000310 Type *Typ = OpaqueType::get();
311 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
312 return Typ;
Reid Spencere2c32da2006-12-03 05:46:11 +0000313 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000314
315static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
316 SymbolTable &SymTab =
Reid Spencer32af9e82007-01-06 07:24:44 +0000317 inFunctionScope() ? CurFun.CurrentFunction->getValueSymbolTable() :
318 CurModule.CurrentModule->getValueSymbolTable();
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000319 return SymTab.lookup(Ty, Name);
320}
321
322// getValNonImprovising - Look up the value specified by the provided type and
323// the provided ValID. If the value exists and has already been defined, return
324// it. Otherwise return null.
325//
326static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Reid Spencer309080a2006-09-28 19:28:24 +0000327 if (isa<FunctionType>(Ty)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000328 GenerateError("Functions are not values and "
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000329 "must be referenced as pointers");
Reid Spencer309080a2006-09-28 19:28:24 +0000330 return 0;
331 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000332
333 switch (D.Type) {
334 case ValID::NumberVal: { // Is it a numbered definition?
335 unsigned Num = (unsigned)D.Num;
336
337 // Module constants occupy the lowest numbered slots...
338 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
339 if (VI != CurModule.Values.end()) {
340 if (Num < VI->second.size())
341 return VI->second[Num];
342 Num -= VI->second.size();
343 }
344
345 // Make sure that our type is within bounds
346 VI = CurFun.Values.find(Ty);
347 if (VI == CurFun.Values.end()) return 0;
348
349 // Check that the number is within bounds...
350 if (VI->second.size() <= Num) return 0;
351
352 return VI->second[Num];
353 }
354
355 case ValID::NameVal: { // Is it a named definition?
356 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
357 if (N == 0) return 0;
358
359 D.destroy(); // Free old strdup'd memory...
360 return N;
361 }
362
363 // Check to make sure that "Ty" is an integral type, and that our
364 // value will fit into the specified type...
365 case ValID::ConstSIntVal: // Is it a constant pool reference??
Reid Spencere0fc4df2006-10-20 07:07:24 +0000366 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000367 GenerateError("Signed integral constant '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000368 itostr(D.ConstPool64) + "' is invalid for type '" +
369 Ty->getDescription() + "'!");
Reid Spencer309080a2006-09-28 19:28:24 +0000370 return 0;
371 }
Reid Spencere0fc4df2006-10-20 07:07:24 +0000372 return ConstantInt::get(Ty, D.ConstPool64);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000373
374 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Reid Spencere0fc4df2006-10-20 07:07:24 +0000375 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
376 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000377 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000378 "' is invalid or out of range!");
Reid Spencer309080a2006-09-28 19:28:24 +0000379 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000380 } else { // This is really a signed reference. Transmogrify.
Reid Spencere0fc4df2006-10-20 07:07:24 +0000381 return ConstantInt::get(Ty, D.ConstPool64);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000382 }
383 } else {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000384 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000385 }
386
387 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Reid Spencer309080a2006-09-28 19:28:24 +0000388 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000389 GenerateError("FP constant invalid for type!!");
Reid Spencer309080a2006-09-28 19:28:24 +0000390 return 0;
391 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000392 return ConstantFP::get(Ty, D.ConstPoolFP);
393
394 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer309080a2006-09-28 19:28:24 +0000395 if (!isa<PointerType>(Ty)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000396 GenerateError("Cannot create a a non pointer null!");
Reid Spencer309080a2006-09-28 19:28:24 +0000397 return 0;
398 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000399 return ConstantPointerNull::get(cast<PointerType>(Ty));
400
401 case ValID::ConstUndefVal: // Is it an undef value?
402 return UndefValue::get(Ty);
403
404 case ValID::ConstZeroVal: // Is it a zero value?
405 return Constant::getNullValue(Ty);
406
407 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer309080a2006-09-28 19:28:24 +0000408 if (D.ConstantValue->getType() != Ty) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000409 GenerateError("Constant expression type different from required type!");
Reid Spencer309080a2006-09-28 19:28:24 +0000410 return 0;
411 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000412 return D.ConstantValue;
413
414 case ValID::InlineAsmVal: { // Inline asm expression
415 const PointerType *PTy = dyn_cast<PointerType>(Ty);
416 const FunctionType *FTy =
417 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer309080a2006-09-28 19:28:24 +0000418 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000419 GenerateError("Invalid type for asm constraint string!");
Reid Spencer309080a2006-09-28 19:28:24 +0000420 return 0;
421 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000422 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
423 D.IAD->HasSideEffects);
424 D.destroy(); // Free InlineAsmDescriptor.
425 return IA;
426 }
427 default:
428 assert(0 && "Unhandled case!");
429 return 0;
430 } // End of switch
431
432 assert(0 && "Unhandled case!");
433 return 0;
434}
435
436// getVal - This function is identical to getValNonImprovising, except that if a
437// value is not already defined, it "improvises" by creating a placeholder var
438// that looks and acts just like the requested variable. When the value is
439// defined later, all uses of the placeholder variable are replaced with the
440// real thing.
441//
442static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer309080a2006-09-28 19:28:24 +0000443 if (Ty == Type::LabelTy) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000444 GenerateError("Cannot use a basic block here");
Reid Spencer309080a2006-09-28 19:28:24 +0000445 return 0;
446 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000447
448 // See if the value has already been defined.
449 Value *V = getValNonImprovising(Ty, ID);
450 if (V) return V;
Reid Spencer309080a2006-09-28 19:28:24 +0000451 if (TriggerError) return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000452
Reid Spencer309080a2006-09-28 19:28:24 +0000453 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000454 GenerateError("Invalid use of a composite type!");
Reid Spencer309080a2006-09-28 19:28:24 +0000455 return 0;
456 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000457
458 // If we reached here, we referenced either a symbol that we don't know about
459 // or an id number that hasn't been read yet. We may be referencing something
460 // forward, so just create an entry to be resolved later and get to it...
461 //
462 V = new Argument(Ty);
463
464 // Remember where this forward reference came from. FIXME, shouldn't we try
465 // to recycle these things??
466 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
467 llvmAsmlineno)));
468
469 if (inFunctionScope())
470 InsertValue(V, CurFun.LateResolveValues);
471 else
472 InsertValue(V, CurModule.LateResolveValues);
473 return V;
474}
475
476/// getBBVal - This is used for two purposes:
477/// * If isDefinition is true, a new basic block with the specified ID is being
478/// defined.
479/// * If isDefinition is true, this is a reference to a basic block, which may
480/// or may not be a forward reference.
481///
482static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
483 assert(inFunctionScope() && "Can't get basic block at global scope!");
484
485 std::string Name;
486 BasicBlock *BB = 0;
487 switch (ID.Type) {
Reid Spencer309080a2006-09-28 19:28:24 +0000488 default:
489 GenerateError("Illegal label reference " + ID.getName());
490 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000491 case ValID::NumberVal: // Is it a numbered definition?
492 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
493 CurFun.NumberedBlocks.resize(ID.Num+1);
494 BB = CurFun.NumberedBlocks[ID.Num];
495 break;
496 case ValID::NameVal: // Is it a named definition?
497 Name = ID.Name;
498 if (Value *N = CurFun.CurrentFunction->
Reid Spencer32af9e82007-01-06 07:24:44 +0000499 getValueSymbolTable().lookup(Type::LabelTy, Name))
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000500 BB = cast<BasicBlock>(N);
501 break;
502 }
503
504 // See if the block has already been defined.
505 if (BB) {
506 // If this is the definition of the block, make sure the existing value was
507 // just a forward reference. If it was a forward reference, there will be
508 // an entry for it in the PlaceHolderInfo map.
Reid Spencer309080a2006-09-28 19:28:24 +0000509 if (isDefinition && !CurFun.BBForwardRefs.erase(BB)) {
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000510 // The existing value was a definition, not a forward reference.
Reid Spencer713eedc2006-08-18 08:43:06 +0000511 GenerateError("Redefinition of label " + ID.getName());
Reid Spencer309080a2006-09-28 19:28:24 +0000512 return 0;
513 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000514
515 ID.destroy(); // Free strdup'd memory.
516 return BB;
517 }
518
519 // Otherwise this block has not been seen before.
520 BB = new BasicBlock("", CurFun.CurrentFunction);
521 if (ID.Type == ValID::NameVal) {
522 BB->setName(ID.Name);
523 } else {
524 CurFun.NumberedBlocks[ID.Num] = BB;
525 }
526
527 // If this is not a definition, keep track of it so we can use it as a forward
528 // reference.
529 if (!isDefinition) {
530 // Remember where this forward reference came from.
531 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
532 } else {
533 // The forward declaration could have been inserted anywhere in the
534 // function: insert it into the correct place now.
535 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
536 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
537 }
538 ID.destroy();
539 return BB;
540}
541
542
543//===----------------------------------------------------------------------===//
544// Code to handle forward references in instructions
545//===----------------------------------------------------------------------===//
546//
547// This code handles the late binding needed with statements that reference
548// values not defined yet... for example, a forward branch, or the PHI node for
549// a loop body.
550//
551// This keeps a table (CurFun.LateResolveValues) of all such forward references
552// and back patchs after we are done.
553//
554
555// ResolveDefinitions - If we could not resolve some defs at parsing
556// time (forward branches, phi functions for loops, etc...) resolve the
557// defs now...
558//
559static void
560ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
561 std::map<const Type*,ValueList> *FutureLateResolvers) {
562 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
563 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
564 E = LateResolvers.end(); LRI != E; ++LRI) {
565 ValueList &List = LRI->second;
566 while (!List.empty()) {
567 Value *V = List.back();
568 List.pop_back();
569
570 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
571 CurModule.PlaceHolderInfo.find(V);
572 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
573
574 ValID &DID = PHI->second.first;
575
576 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Reid Spencer309080a2006-09-28 19:28:24 +0000577 if (TriggerError)
578 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000579 if (TheRealValue) {
580 V->replaceAllUsesWith(TheRealValue);
581 delete V;
582 CurModule.PlaceHolderInfo.erase(PHI);
583 } else if (FutureLateResolvers) {
584 // Functions have their unresolved items forwarded to the module late
585 // resolver table
586 InsertValue(V, *FutureLateResolvers);
587 } else {
Reid Spencer309080a2006-09-28 19:28:24 +0000588 if (DID.Type == ValID::NameVal) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000589 GenerateError("Reference to an invalid definition: '" +DID.getName()+
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000590 "' of type '" + V->getType()->getDescription() + "'",
591 PHI->second.second);
Reid Spencer309080a2006-09-28 19:28:24 +0000592 return;
593 } else {
Reid Spencer713eedc2006-08-18 08:43:06 +0000594 GenerateError("Reference to an invalid definition: #" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000595 itostr(DID.Num) + " of type '" +
596 V->getType()->getDescription() + "'",
597 PHI->second.second);
Reid Spencer309080a2006-09-28 19:28:24 +0000598 return;
599 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000600 }
601 }
602 }
603
604 LateResolvers.clear();
605}
606
607// ResolveTypeTo - A brand new type was just declared. This means that (if
608// name is not null) things referencing Name can be resolved. Otherwise, things
609// refering to the number can be resolved. Do this now.
610//
611static void ResolveTypeTo(char *Name, const Type *ToTy) {
612 ValID D;
613 if (Name) D = ValID::create(Name);
614 else D = ValID::create((int)CurModule.Types.size());
615
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000616 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000617 CurModule.LateResolveTypes.find(D);
618 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000619 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000620 CurModule.LateResolveTypes.erase(I);
621 }
622}
623
624// setValueName - Set the specified value to the name given. The name may be
625// null potentially, in which case this is a noop. The string passed in is
626// assumed to be a malloc'd string buffer, and is free'd by this function.
627//
628static void setValueName(Value *V, char *NameStr) {
629 if (NameStr) {
630 std::string Name(NameStr); // Copy string
631 free(NameStr); // Free old string
632
Reid Spencer309080a2006-09-28 19:28:24 +0000633 if (V->getType() == Type::VoidTy) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000634 GenerateError("Can't assign name '" + Name+"' to value with void type!");
Reid Spencer309080a2006-09-28 19:28:24 +0000635 return;
636 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000637
638 assert(inFunctionScope() && "Must be in function scope!");
Reid Spencer32af9e82007-01-06 07:24:44 +0000639 SymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
Reid Spencer309080a2006-09-28 19:28:24 +0000640 if (ST.lookup(V->getType(), Name)) {
Reid Spencer33259082007-01-05 21:51:07 +0000641 GenerateError("Redefinition of value '" + Name + "' of type '" +
642 V->getType()->getDescription() + "'!");
Reid Spencer309080a2006-09-28 19:28:24 +0000643 return;
644 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000645
646 // Set the name.
647 V->setName(Name);
648 }
649}
650
651/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
652/// this is a declaration, otherwise it is a definition.
653static GlobalVariable *
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000654ParseGlobalVariable(char *NameStr,
655 GlobalValue::LinkageTypes Linkage,
656 GlobalValue::VisibilityTypes Visibility,
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000657 bool isConstantGlobal, const Type *Ty,
658 Constant *Initializer) {
Reid Spencer309080a2006-09-28 19:28:24 +0000659 if (isa<FunctionType>(Ty)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000660 GenerateError("Cannot declare global vars of function type!");
Reid Spencer309080a2006-09-28 19:28:24 +0000661 return 0;
662 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000663
664 const PointerType *PTy = PointerType::get(Ty);
665
666 std::string Name;
667 if (NameStr) {
668 Name = NameStr; // Copy string
669 free(NameStr); // Free old string
670 }
671
672 // See if this global value was forward referenced. If so, recycle the
673 // object.
674 ValID ID;
675 if (!Name.empty()) {
676 ID = ValID::create((char*)Name.c_str());
677 } else {
678 ID = ValID::create((int)CurModule.Values[PTy].size());
679 }
680
681 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
682 // Move the global to the end of the list, from whereever it was
683 // previously inserted.
684 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
685 CurModule.CurrentModule->getGlobalList().remove(GV);
686 CurModule.CurrentModule->getGlobalList().push_back(GV);
687 GV->setInitializer(Initializer);
688 GV->setLinkage(Linkage);
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000689 GV->setVisibility(Visibility);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000690 GV->setConstant(isConstantGlobal);
691 InsertValue(GV, CurModule.Values);
692 return GV;
693 }
694
695 // If this global has a name, check to see if there is already a definition
Reid Spencer33259082007-01-05 21:51:07 +0000696 // of this global in the module. If so, it is an error.
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000697 if (!Name.empty()) {
698 // We are a simple redefinition of a value, check to see if it is defined
699 // the same as the old one.
Reid Spencer33259082007-01-05 21:51:07 +0000700 if (CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000701 GenerateError("Redefinition of global variable named '" + Name +
Reid Spencer33259082007-01-05 21:51:07 +0000702 "' of type '" + Ty->getDescription() + "'!");
Reid Spencer309080a2006-09-28 19:28:24 +0000703 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000704 }
705 }
706
707 // Otherwise there is no existing GV to use, create one now.
708 GlobalVariable *GV =
709 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
710 CurModule.CurrentModule);
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000711 GV->setVisibility(Visibility);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000712 InsertValue(GV, CurModule.Values);
713 return GV;
714}
715
716// setTypeName - Set the specified type to the name given. The name may be
717// null potentially, in which case this is a noop. The string passed in is
718// assumed to be a malloc'd string buffer, and is freed by this function.
719//
720// This function returns true if the type has already been defined, but is
721// allowed to be redefined in the specified context. If the name is a new name
722// for the type plane, it is inserted and false is returned.
723static bool setTypeName(const Type *T, char *NameStr) {
724 assert(!inFunctionScope() && "Can't give types function-local names!");
725 if (NameStr == 0) return false;
726
727 std::string Name(NameStr); // Copy string
728 free(NameStr); // Free old string
729
730 // We don't allow assigning names to void type
Reid Spencer309080a2006-09-28 19:28:24 +0000731 if (T == Type::VoidTy) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000732 GenerateError("Can't assign name '" + Name + "' to the void type!");
Reid Spencer309080a2006-09-28 19:28:24 +0000733 return false;
734 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000735
736 // Set the type name, checking for conflicts as we do so.
737 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
738
739 if (AlreadyExists) { // Inserting a name that is already defined???
740 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
741 assert(Existing && "Conflict but no matching type?");
742
743 // There is only one case where this is allowed: when we are refining an
744 // opaque type. In this case, Existing will be an opaque type.
745 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
746 // We ARE replacing an opaque type!
747 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
748 return true;
749 }
750
751 // Otherwise, this is an attempt to redefine a type. That's okay if
752 // the redefinition is identical to the original. This will be so if
753 // Existing and T point to the same Type object. In this one case we
754 // allow the equivalent redefinition.
755 if (Existing == T) return true; // Yes, it's equal.
756
757 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer33259082007-01-05 21:51:07 +0000758 GenerateError("Redefinition of type named '" + Name + "' of type '" +
759 T->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000760 }
761
762 return false;
763}
764
765//===----------------------------------------------------------------------===//
766// Code for handling upreferences in type names...
767//
768
769// TypeContains - Returns true if Ty directly contains E in it.
770//
771static bool TypeContains(const Type *Ty, const Type *E) {
772 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
773 E) != Ty->subtype_end();
774}
775
776namespace {
777 struct UpRefRecord {
778 // NestingLevel - The number of nesting levels that need to be popped before
779 // this type is resolved.
780 unsigned NestingLevel;
781
782 // LastContainedTy - This is the type at the current binding level for the
783 // type. Every time we reduce the nesting level, this gets updated.
784 const Type *LastContainedTy;
785
786 // UpRefTy - This is the actual opaque type that the upreference is
787 // represented with.
788 OpaqueType *UpRefTy;
789
790 UpRefRecord(unsigned NL, OpaqueType *URTy)
791 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
792 };
793}
794
795// UpRefs - A list of the outstanding upreferences that need to be resolved.
796static std::vector<UpRefRecord> UpRefs;
797
798/// HandleUpRefs - Every time we finish a new layer of types, this function is
799/// called. It loops through the UpRefs vector, which is a list of the
800/// currently active types. For each type, if the up reference is contained in
801/// the newly completed type, we decrement the level count. When the level
802/// count reaches zero, the upreferenced type is the type that is passed in:
803/// thus we can complete the cycle.
804///
805static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner680aab62006-08-18 17:34:45 +0000806 // If Ty isn't abstract, or if there are no up-references in it, then there is
807 // nothing to resolve here.
808 if (!ty->isAbstract() || UpRefs.empty()) return ty;
809
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000810 PATypeHolder Ty(ty);
811 UR_OUT("Type '" << Ty->getDescription() <<
812 "' newly formed. Resolving upreferences.\n" <<
813 UpRefs.size() << " upreferences active!\n");
814
815 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
816 // to zero), we resolve them all together before we resolve them to Ty. At
817 // the end of the loop, if there is anything to resolve to Ty, it will be in
818 // this variable.
819 OpaqueType *TypeToResolve = 0;
820
821 for (unsigned i = 0; i != UpRefs.size(); ++i) {
822 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
823 << UpRefs[i].second->getDescription() << ") = "
824 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
825 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
826 // Decrement level of upreference
827 unsigned Level = --UpRefs[i].NestingLevel;
828 UpRefs[i].LastContainedTy = Ty;
829 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
830 if (Level == 0) { // Upreference should be resolved!
831 if (!TypeToResolve) {
832 TypeToResolve = UpRefs[i].UpRefTy;
833 } else {
834 UR_OUT(" * Resolving upreference for "
835 << UpRefs[i].second->getDescription() << "\n";
836 std::string OldName = UpRefs[i].UpRefTy->getDescription());
837 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
838 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
839 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
840 }
841 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
842 --i; // Do not skip the next element...
843 }
844 }
845 }
846
847 if (TypeToResolve) {
848 UR_OUT(" * Resolving upreference for "
849 << UpRefs[i].second->getDescription() << "\n";
850 std::string OldName = TypeToResolve->getDescription());
851 TypeToResolve->refineAbstractTypeTo(Ty);
852 }
853
854 return Ty;
855}
856
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000857//===----------------------------------------------------------------------===//
858// RunVMAsmParser - Define an interface to this parser
859//===----------------------------------------------------------------------===//
860//
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000861static Module* RunParser(Module * M);
862
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000863Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
864 set_scan_file(F);
865
866 CurFilename = Filename;
867 return RunParser(new Module(CurFilename));
868}
869
870Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
871 set_scan_string(AsmString);
872
873 CurFilename = "from_memory";
874 if (M == NULL) {
875 return RunParser(new Module (CurFilename));
876 } else {
877 return RunParser(M);
878 }
879}
880
881%}
882
883%union {
884 llvm::Module *ModuleVal;
885 llvm::Function *FunctionVal;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000886 llvm::BasicBlock *BasicBlockVal;
887 llvm::TerminatorInst *TermInstVal;
888 llvm::Instruction *InstVal;
Reid Spencere2c32da2006-12-03 05:46:11 +0000889 llvm::Constant *ConstVal;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000890
Reid Spencere2c32da2006-12-03 05:46:11 +0000891 const llvm::Type *PrimType;
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000892 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencere2c32da2006-12-03 05:46:11 +0000893 llvm::PATypeHolder *TypeVal;
894 llvm::Value *ValueVal;
Reid Spencere2c32da2006-12-03 05:46:11 +0000895 std::vector<llvm::Value*> *ValueList;
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000896 llvm::ArgListType *ArgList;
897 llvm::TypeWithAttrs TypeWithAttrs;
898 llvm::TypeWithAttrsList *TypeWithAttrsList;
899 llvm::ValueRefList *ValueRefList;
900
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000901 // Represent the RHS of PHI node
Reid Spencere2c32da2006-12-03 05:46:11 +0000902 std::list<std::pair<llvm::Value*,
903 llvm::BasicBlock*> > *PHIList;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000904 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencere2c32da2006-12-03 05:46:11 +0000905 std::vector<llvm::Constant*> *ConstVector;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000906
907 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000908 llvm::GlobalValue::VisibilityTypes Visibility;
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000909 llvm::FunctionType::ParameterAttributes ParamAttrs;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000910 int64_t SInt64Val;
911 uint64_t UInt64Val;
912 int SIntVal;
913 unsigned UIntVal;
914 double FPVal;
915 bool BoolVal;
916
917 char *StrVal; // This memory is strdup'd!
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000918 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000919
Reid Spencere2c32da2006-12-03 05:46:11 +0000920 llvm::Instruction::BinaryOps BinaryOpVal;
921 llvm::Instruction::TermOps TermOpVal;
922 llvm::Instruction::MemoryOps MemOpVal;
923 llvm::Instruction::CastOps CastOpVal;
924 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000925 llvm::Module::Endianness Endianness;
Reid Spencere2c32da2006-12-03 05:46:11 +0000926 llvm::ICmpInst::Predicate IPredicate;
927 llvm::FCmpInst::Predicate FPredicate;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000928}
929
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000930%type <ModuleVal> Module
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000931%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
932%type <BasicBlockVal> BasicBlock InstructionList
933%type <TermInstVal> BBTerminatorInst
934%type <InstVal> Inst InstVal MemoryInst
935%type <ConstVal> ConstVal ConstExpr
936%type <ConstVector> ConstVector
937%type <ArgList> ArgList ArgListH
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000938%type <PHIList> PHIList
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000939%type <ValueRefList> ValueRefList // For call param lists & GEP indices
940%type <ValueList> IndexList // For GEP indices
941%type <TypeList> TypeListI
942%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencerbf48e3c2007-01-05 17:07:23 +0000943%type <TypeWithAttrs> ArgType
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000944%type <JumpTable> JumpTable
945%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
946%type <BoolVal> OptVolatile // 'volatile' or not
947%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
948%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000949%type <Linkage> GVInternalLinkage GVExternalLinkage
950%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000951%type <Visibility> GVVisibilityStyle
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000952%type <Endianness> BigOrLittle
953
954// ValueRef - Unresolved reference to a definition or BB
955%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
956%type <ValueVal> ResolvedVal // <type> <valref> pair
957// Tokens and types for handling constant integer values
958//
959// ESINT64VAL - A negative number within long long range
960%token <SInt64Val> ESINT64VAL
961
962// EUINT64VAL - A positive number within uns. long long range
963%token <UInt64Val> EUINT64VAL
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000964
965%token <SIntVal> SINTVAL // Signed 32 bit ints...
966%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
967%type <SIntVal> INTVAL
968%token <FPVal> FPVAL // Float or Double constant
969
970// Built in types...
Reid Spencerbf48e3c2007-01-05 17:07:23 +0000971%type <TypeVal> Types ResultTypes
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000972%type <PrimType> IntType FPType PrimType // Classifications
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000973%token <PrimType> VOID BOOL INTTYPE
Reid Spencerdc0a3a22006-12-29 20:35:03 +0000974%token <PrimType> FLOAT DOUBLE LABEL
975%token TYPE
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000976
977%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
978%type <StrVal> Name OptName OptAssign
979%type <UIntVal> OptAlign OptCAlign
980%type <StrVal> OptSection SectionString
981
982%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Reid Spencerdc0a3a22006-12-29 20:35:03 +0000983%token DECLARE DEFINE GLOBAL CONSTANT SECTION VOLATILE
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000984%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000985%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Chris Lattner67598a82007-01-12 18:33:30 +0000986%token OPAQUE EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000987%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Chris Lattner09c0e992006-05-19 21:28:53 +0000988%token CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +0000989%token X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattner7d1d0342006-10-22 06:08:13 +0000990%token DATALAYOUT
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000991%type <UIntVal> OptCallingConv
Reid Spencerbf48e3c2007-01-05 17:07:23 +0000992%type <ParamAttrs> OptParamAttrs ParamAttr
993%type <ParamAttrs> OptFuncAttrs FuncAttr
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000994
995// Basic Block Terminating Operators
996%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
997
998// Binary Operators
Reid Spencer266e42b2006-12-23 06:05:41 +0000999%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencerde46e482006-11-02 20:25:50 +00001000%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencere2c32da2006-12-03 05:46:11 +00001001%token <OtherOpVal> ICMP FCMP
Reid Spencere2c32da2006-12-03 05:46:11 +00001002%type <IPredicate> IPredicates
Reid Spencere2c32da2006-12-03 05:46:11 +00001003%type <FPredicate> FPredicates
Reid Spencer1960ef32006-12-03 06:59:29 +00001004%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1005%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001006
1007// Memory Instructions
1008%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1009
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001010// Cast Operators
1011%type <CastOpVal> CastOps
1012%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1013%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1014
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001015// Other Operators
1016%type <OtherOpVal> ShiftOps
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001017%token <OtherOpVal> PHI_TOK SELECT SHL LSHR ASHR VAARG
Chris Lattner9ff96a72006-04-08 01:18:56 +00001018%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001019
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001020// Function Attributes
1021%token NORETURN
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001022
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001023// Visibility Styles
1024%token DEFAULT HIDDEN
1025
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001026%start Module
1027%%
1028
1029// Handle constant integer size restriction and conversion...
1030//
1031INTVAL : SINTVAL;
1032INTVAL : UINTVAL {
1033 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
Reid Spencer713eedc2006-08-18 08:43:06 +00001034 GEN_ERROR("Value too large for type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001035 $$ = (int32_t)$1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001036 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001037};
1038
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001039// Operations that are notably excluded from this list include:
1040// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1041//
Reid Spencerde46e482006-11-02 20:25:50 +00001042ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001043LogicalOps : AND | OR | XOR;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001044CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1045 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
1046ShiftOps : SHL | LSHR | ASHR;
Reid Spencer1960ef32006-12-03 06:59:29 +00001047IPredicates
Reid Spencerd2e0c342006-12-04 05:24:24 +00001048 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer1960ef32006-12-03 06:59:29 +00001049 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1050 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1051 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1052 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1053 ;
1054
1055FPredicates
1056 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1057 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1058 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1059 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1060 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1061 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1062 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1063 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1064 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1065 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001066
1067// These are some types that allow classification if we only want a particular
1068// thing... for example, only a signed, unsigned, or integral type.
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001069IntType : INTTYPE;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001070FPType : FLOAT | DOUBLE;
1071
1072// OptAssign - Value producing statements have an optional assignment component
1073OptAssign : Name '=' {
1074 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001075 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001076 }
1077 | /*empty*/ {
1078 $$ = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00001079 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001080 };
1081
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001082GVInternalLinkage
1083 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1084 | WEAK { $$ = GlobalValue::WeakLinkage; }
1085 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1086 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1087 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1088 ;
1089
1090GVExternalLinkage
1091 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1092 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1093 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1094 ;
1095
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001096GVVisibilityStyle
1097 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1098 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1099 ;
1100
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001101FunctionDeclareLinkage
1102 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1103 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1104 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001105 ;
1106
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001107FunctionDefineLinkage
1108 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1109 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001110 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1111 | WEAK { $$ = GlobalValue::WeakLinkage; }
1112 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001113 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001114
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +00001115OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1116 CCC_TOK { $$ = CallingConv::C; } |
1117 CSRETCC_TOK { $$ = CallingConv::CSRet; } |
1118 FASTCC_TOK { $$ = CallingConv::Fast; } |
1119 COLDCC_TOK { $$ = CallingConv::Cold; } |
1120 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1121 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1122 CC_TOK EUINT64VAL {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001123 if ((unsigned)$2 != $2)
Reid Spencer713eedc2006-08-18 08:43:06 +00001124 GEN_ERROR("Calling conv too large!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001125 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001126 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001127 };
1128
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001129ParamAttr : ZEXT { $$ = FunctionType::ZExtAttribute; }
1130 | SEXT { $$ = FunctionType::SExtAttribute; }
1131 ;
1132
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001133OptParamAttrs : /* empty */ { $$ = FunctionType::NoAttributeSet; }
1134 | OptParamAttrs ParamAttr {
1135 $$ = FunctionType::ParameterAttributes($1 | $2);
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001136 }
1137 ;
1138
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001139FuncAttr : NORETURN { $$ = FunctionType::NoReturnAttribute; }
1140 | ParamAttr
1141 ;
1142
1143OptFuncAttrs : /* empty */ { $$ = FunctionType::NoAttributeSet; }
1144 | OptFuncAttrs FuncAttr {
1145 $$ = FunctionType::ParameterAttributes($1 | $2);
1146 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001147 ;
1148
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001149// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1150// a comma before it.
1151OptAlign : /*empty*/ { $$ = 0; } |
1152 ALIGN EUINT64VAL {
1153 $$ = $2;
1154 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencer713eedc2006-08-18 08:43:06 +00001155 GEN_ERROR("Alignment must be a power of two!");
1156 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001157};
1158OptCAlign : /*empty*/ { $$ = 0; } |
1159 ',' ALIGN EUINT64VAL {
1160 $$ = $3;
1161 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencer713eedc2006-08-18 08:43:06 +00001162 GEN_ERROR("Alignment must be a power of two!");
1163 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001164};
1165
1166
1167SectionString : SECTION STRINGCONSTANT {
1168 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1169 if ($2[i] == '"' || $2[i] == '\\')
Reid Spencer713eedc2006-08-18 08:43:06 +00001170 GEN_ERROR("Invalid character in section name!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001171 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001172 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001173};
1174
1175OptSection : /*empty*/ { $$ = 0; } |
1176 SectionString { $$ = $1; };
1177
1178// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1179// is set to be the global we are processing.
1180//
1181GlobalVarAttributes : /* empty */ {} |
1182 ',' GlobalVarAttribute GlobalVarAttributes {};
1183GlobalVarAttribute : SectionString {
1184 CurGV->setSection($1);
1185 free($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001186 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001187 }
1188 | ALIGN EUINT64VAL {
1189 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencer713eedc2006-08-18 08:43:06 +00001190 GEN_ERROR("Alignment must be a power of two!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001191 CurGV->setAlignment($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001192 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001193 };
1194
1195//===----------------------------------------------------------------------===//
1196// Types includes all predefined types... except void, because it can only be
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001197// used in specific contexts (function returning void for example).
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001198
1199// Derived types are added later...
1200//
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001201PrimType : BOOL | INTTYPE | FLOAT | DOUBLE | LABEL ;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001202
1203Types
1204 : OPAQUE {
Reid Spencere2c32da2006-12-03 05:46:11 +00001205 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer713eedc2006-08-18 08:43:06 +00001206 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001207 }
1208 | PrimType {
Reid Spencere2c32da2006-12-03 05:46:11 +00001209 $$ = new PATypeHolder($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001210 CHECK_FOR_ERROR
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001211 }
1212 | Types '*' { // Pointer type?
1213 if (*$1 == Type::LabelTy)
1214 GEN_ERROR("Cannot form a pointer to a basic block");
1215 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1216 delete $1;
1217 CHECK_FOR_ERROR
1218 }
1219 | SymbolicValueRef { // Named types are also simple types...
1220 const Type* tmp = getTypeVal($1);
1221 CHECK_FOR_ERROR
1222 $$ = new PATypeHolder(tmp);
1223 }
1224 | '\\' EUINT64VAL { // Type UpReference
Reid Spencer713eedc2006-08-18 08:43:06 +00001225 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001226 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1227 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencere2c32da2006-12-03 05:46:11 +00001228 $$ = new PATypeHolder(OT);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001229 UR_OUT("New Upreference!\n");
Reid Spencer713eedc2006-08-18 08:43:06 +00001230 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001231 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001232 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001233 std::vector<const Type*> Params;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001234 std::vector<FunctionType::ParameterAttributes> Attrs;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001235 Attrs.push_back($5);
1236 for (TypeWithAttrsList::iterator I=$3->begin(), E=$3->end(); I != E; ++I) {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001237 Params.push_back(I->Ty->get());
1238 if (I->Ty->get() != Type::VoidTy)
1239 Attrs.push_back(I->Attrs);
1240 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001241 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1242 if (isVarArg) Params.pop_back();
1243
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001244 FunctionType *FT = FunctionType::get(*$1, Params, isVarArg, Attrs);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001245 delete $3; // Delete the argument list
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001246 delete $1; // Delete the return type handle
1247 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer713eedc2006-08-18 08:43:06 +00001248 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001249 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001250 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001251 std::vector<const Type*> Params;
1252 std::vector<FunctionType::ParameterAttributes> Attrs;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001253 Attrs.push_back($5);
1254 for (TypeWithAttrsList::iterator I=$3->begin(), E=$3->end(); I != E; ++I) {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001255 Params.push_back(I->Ty->get());
1256 if (I->Ty->get() != Type::VoidTy)
1257 Attrs.push_back(I->Attrs);
1258 }
1259 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1260 if (isVarArg) Params.pop_back();
1261
1262 FunctionType *FT = FunctionType::get($1, Params, isVarArg, Attrs);
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001263 delete $3; // Delete the argument list
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001264 $$ = new PATypeHolder(HandleUpRefs(FT));
1265 CHECK_FOR_ERROR
1266 }
1267
1268 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Reid Spencere2c32da2006-12-03 05:46:11 +00001269 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1270 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00001271 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001272 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001273 | '<' EUINT64VAL 'x' Types '>' { // Packed array type?
Reid Spencere2c32da2006-12-03 05:46:11 +00001274 const llvm::Type* ElemTy = $4->get();
1275 if ((unsigned)$2 != $2)
1276 GEN_ERROR("Unsigned result not equal to signed result");
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001277 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
1278 GEN_ERROR("Element type of a PackedType must be primitive");
Reid Spencere2c32da2006-12-03 05:46:11 +00001279 if (!isPowerOf2_32($2))
1280 GEN_ERROR("Vector length should be a power of 2!");
1281 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
1282 delete $4;
1283 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001284 }
1285 | '{' TypeListI '}' { // Structure type?
1286 std::vector<const Type*> Elements;
Reid Spencere2c32da2006-12-03 05:46:11 +00001287 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001288 E = $2->end(); I != E; ++I)
Reid Spencere2c32da2006-12-03 05:46:11 +00001289 Elements.push_back(*I);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001290
Reid Spencere2c32da2006-12-03 05:46:11 +00001291 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001292 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001293 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001294 }
1295 | '{' '}' { // Empty structure type?
Reid Spencere2c32da2006-12-03 05:46:11 +00001296 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer713eedc2006-08-18 08:43:06 +00001297 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001298 }
Andrew Lenharth2d189ff2006-12-08 18:07:09 +00001299 | '<' '{' TypeListI '}' '>' {
1300 std::vector<const Type*> Elements;
1301 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1302 E = $3->end(); I != E; ++I)
1303 Elements.push_back(*I);
1304
1305 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1306 delete $3;
1307 CHECK_FOR_ERROR
1308 }
1309 | '<' '{' '}' '>' { // Empty structure type?
1310 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1311 CHECK_FOR_ERROR
1312 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001313 ;
1314
1315ArgType
1316 : Types OptParamAttrs {
1317 $$.Ty = $1;
1318 $$.Attrs = $2;
1319 }
1320 ;
1321
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001322ResultTypes
1323 : Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001324 if (!UpRefs.empty())
1325 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1326 if (!(*$1)->isFirstClassType())
1327 GEN_ERROR("LLVM functions cannot return aggregate types!");
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001328 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001329 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001330 | VOID {
1331 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001332 }
1333 ;
1334
1335ArgTypeList : ArgType {
1336 $$ = new TypeWithAttrsList();
1337 $$->push_back($1);
1338 CHECK_FOR_ERROR
1339 }
1340 | ArgTypeList ',' ArgType {
1341 ($$=$1)->push_back($3);
1342 CHECK_FOR_ERROR
1343 }
1344 ;
1345
1346ArgTypeListI
1347 : ArgTypeList
1348 | ArgTypeList ',' DOTDOTDOT {
1349 $$=$1;
1350 TypeWithAttrs TWA; TWA.Attrs = FunctionType::NoAttributeSet;
1351 TWA.Ty = new PATypeHolder(Type::VoidTy);
1352 $$->push_back(TWA);
1353 CHECK_FOR_ERROR
1354 }
1355 | DOTDOTDOT {
1356 $$ = new TypeWithAttrsList;
1357 TypeWithAttrs TWA; TWA.Attrs = FunctionType::NoAttributeSet;
1358 TWA.Ty = new PATypeHolder(Type::VoidTy);
1359 $$->push_back(TWA);
1360 CHECK_FOR_ERROR
1361 }
1362 | /*empty*/ {
1363 $$ = new TypeWithAttrsList();
Reid Spencer713eedc2006-08-18 08:43:06 +00001364 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001365 };
1366
1367// TypeList - Used for struct declarations and as a basis for function type
1368// declaration type lists
1369//
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001370TypeListI : Types {
Reid Spencere2c32da2006-12-03 05:46:11 +00001371 $$ = new std::list<PATypeHolder>();
1372 $$->push_back(*$1); delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001373 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001374 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001375 | TypeListI ',' Types {
Reid Spencere2c32da2006-12-03 05:46:11 +00001376 ($$=$1)->push_back(*$3); delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001377 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001378 };
1379
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001380// ConstVal - The various declarations that go into the constant pool. This
1381// production is used ONLY to represent constants that show up AFTER a 'const',
1382// 'constant' or 'global' token at global scope. Constants that can be inlined
1383// into other expressions (such as integers and constexprs) are handled by the
1384// ResolvedVal, ValueRef and ConstValueRef productions.
1385//
1386ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001387 if (!UpRefs.empty())
1388 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001389 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001390 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001391 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00001392 (*$1)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001393 const Type *ETy = ATy->getElementType();
1394 int NumElements = ATy->getNumElements();
1395
1396 // Verify that we have the correct size...
1397 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer713eedc2006-08-18 08:43:06 +00001398 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001399 utostr($3->size()) + " arguments, but has size of " +
1400 itostr(NumElements) + "!");
1401
1402 // Verify all elements are correct type!
1403 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencere2c32da2006-12-03 05:46:11 +00001404 if (ETy != (*$3)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001405 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001406 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencere2c32da2006-12-03 05:46:11 +00001407 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001408 }
1409
Reid Spencere2c32da2006-12-03 05:46:11 +00001410 $$ = ConstantArray::get(ATy, *$3);
1411 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001412 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001413 }
1414 | Types '[' ']' {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001415 if (!UpRefs.empty())
1416 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001417 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001418 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001419 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00001420 (*$1)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001421
1422 int NumElements = ATy->getNumElements();
1423 if (NumElements != -1 && NumElements != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001424 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001425 " arguments, but has size of " + itostr(NumElements) +"!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001426 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1427 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001428 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001429 }
1430 | Types 'c' STRINGCONSTANT {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001431 if (!UpRefs.empty())
1432 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001433 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001434 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001435 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00001436 (*$1)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001437
1438 int NumElements = ATy->getNumElements();
1439 const Type *ETy = ATy->getElementType();
1440 char *EndStr = UnEscapeLexed($3, true);
1441 if (NumElements != -1 && NumElements != (EndStr-$3))
Reid Spencer713eedc2006-08-18 08:43:06 +00001442 GEN_ERROR("Can't build string constant of size " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001443 itostr((int)(EndStr-$3)) +
1444 " when array has size " + itostr(NumElements) + "!");
1445 std::vector<Constant*> Vals;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001446 if (ETy == Type::Int8Ty) {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001447 for (unsigned char *C = (unsigned char *)$3;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001448 C != (unsigned char*)EndStr; ++C)
1449 Vals.push_back(ConstantInt::get(ETy, *C));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001450 } else {
1451 free($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001452 GEN_ERROR("Cannot build string arrays of non byte sized elements!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001453 }
1454 free($3);
Reid Spencere2c32da2006-12-03 05:46:11 +00001455 $$ = ConstantArray::get(ATy, Vals);
1456 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001457 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001458 }
1459 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001460 if (!UpRefs.empty())
1461 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001462 const PackedType *PTy = dyn_cast<PackedType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001463 if (PTy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001464 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00001465 (*$1)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001466 const Type *ETy = PTy->getElementType();
1467 int NumElements = PTy->getNumElements();
1468
1469 // Verify that we have the correct size...
1470 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer713eedc2006-08-18 08:43:06 +00001471 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001472 utostr($3->size()) + " arguments, but has size of " +
1473 itostr(NumElements) + "!");
1474
1475 // Verify all elements are correct type!
1476 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencere2c32da2006-12-03 05:46:11 +00001477 if (ETy != (*$3)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001478 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001479 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencere2c32da2006-12-03 05:46:11 +00001480 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001481 }
1482
Reid Spencere2c32da2006-12-03 05:46:11 +00001483 $$ = ConstantPacked::get(PTy, *$3);
1484 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001485 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001486 }
1487 | Types '{' ConstVector '}' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001488 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001489 if (STy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001490 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00001491 (*$1)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001492
1493 if ($3->size() != STy->getNumContainedTypes())
Reid Spencer713eedc2006-08-18 08:43:06 +00001494 GEN_ERROR("Illegal number of initializers for structure type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001495
1496 // Check to ensure that constants are compatible with the type initializer!
1497 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencere2c32da2006-12-03 05:46:11 +00001498 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer713eedc2006-08-18 08:43:06 +00001499 GEN_ERROR("Expected type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001500 STy->getElementType(i)->getDescription() +
1501 "' for element #" + utostr(i) +
1502 " of structure initializer!");
1503
Zhou Sheng75b871f2007-01-11 12:24:14 +00001504 // Check to ensure that Type is not packed
1505 if (STy->isPacked())
1506 GEN_ERROR("Unpacked Initializer to packed type '" + STy->getDescription() + "'");
1507
Reid Spencere2c32da2006-12-03 05:46:11 +00001508 $$ = ConstantStruct::get(STy, *$3);
1509 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001510 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001511 }
1512 | Types '{' '}' {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001513 if (!UpRefs.empty())
1514 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001515 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001516 if (STy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001517 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00001518 (*$1)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001519
1520 if (STy->getNumContainedTypes() != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001521 GEN_ERROR("Illegal number of initializers for structure type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001522
Zhou Sheng75b871f2007-01-11 12:24:14 +00001523 // Check to ensure that Type is not packed
1524 if (STy->isPacked())
1525 GEN_ERROR("Unpacked Initializer to packed type '" + STy->getDescription() + "'");
1526
1527 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1528 delete $1;
1529 CHECK_FOR_ERROR
1530 }
1531 | Types '<' '{' ConstVector '}' '>' {
1532 const StructType *STy = dyn_cast<StructType>($1->get());
1533 if (STy == 0)
1534 GEN_ERROR("Cannot make struct constant with type: '" +
1535 (*$1)->getDescription() + "'!");
1536
1537 if ($4->size() != STy->getNumContainedTypes())
1538 GEN_ERROR("Illegal number of initializers for structure type!");
1539
1540 // Check to ensure that constants are compatible with the type initializer!
1541 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1542 if ((*$4)[i]->getType() != STy->getElementType(i))
1543 GEN_ERROR("Expected type '" +
1544 STy->getElementType(i)->getDescription() +
1545 "' for element #" + utostr(i) +
1546 " of structure initializer!");
1547
1548 // Check to ensure that Type is packed
1549 if (!STy->isPacked())
1550 GEN_ERROR("Packed Initializer to unpacked type '" + STy->getDescription() + "'");
1551
1552 $$ = ConstantStruct::get(STy, *$4);
1553 delete $1; delete $4;
1554 CHECK_FOR_ERROR
1555 }
1556 | Types '<' '{' '}' '>' {
1557 if (!UpRefs.empty())
1558 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1559 const StructType *STy = dyn_cast<StructType>($1->get());
1560 if (STy == 0)
1561 GEN_ERROR("Cannot make struct constant with type: '" +
1562 (*$1)->getDescription() + "'!");
1563
1564 if (STy->getNumContainedTypes() != 0)
1565 GEN_ERROR("Illegal number of initializers for structure type!");
1566
1567 // Check to ensure that Type is packed
1568 if (!STy->isPacked())
1569 GEN_ERROR("Packed Initializer to unpacked type '" + STy->getDescription() + "'");
1570
Reid Spencere2c32da2006-12-03 05:46:11 +00001571 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1572 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001573 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001574 }
1575 | Types NULL_TOK {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001576 if (!UpRefs.empty())
1577 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001578 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001579 if (PTy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001580 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00001581 (*$1)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001582
Reid Spencere2c32da2006-12-03 05:46:11 +00001583 $$ = ConstantPointerNull::get(PTy);
1584 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001585 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001586 }
1587 | Types UNDEF {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001588 if (!UpRefs.empty())
1589 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001590 $$ = UndefValue::get($1->get());
1591 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001592 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001593 }
1594 | Types SymbolicValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001595 if (!UpRefs.empty())
1596 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001597 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001598 if (Ty == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001599 GEN_ERROR("Global const reference must be a pointer type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001600
1601 // ConstExprs can exist in the body of a function, thus creating
1602 // GlobalValues whenever they refer to a variable. Because we are in
1603 // the context of a function, getValNonImprovising will search the functions
1604 // symbol table instead of the module symbol table for the global symbol,
1605 // which throws things all off. To get around this, we just tell
1606 // getValNonImprovising that we are at global scope here.
1607 //
1608 Function *SavedCurFn = CurFun.CurrentFunction;
1609 CurFun.CurrentFunction = 0;
1610
1611 Value *V = getValNonImprovising(Ty, $2);
Reid Spencer309080a2006-09-28 19:28:24 +00001612 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001613
1614 CurFun.CurrentFunction = SavedCurFn;
1615
1616 // If this is an initializer for a constant pointer, which is referencing a
1617 // (currently) undefined variable, create a stub now that shall be replaced
1618 // in the future with the right type of variable.
1619 //
1620 if (V == 0) {
1621 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1622 const PointerType *PT = cast<PointerType>(Ty);
1623
1624 // First check to see if the forward references value is already created!
1625 PerModuleInfo::GlobalRefsType::iterator I =
1626 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1627
1628 if (I != CurModule.GlobalRefs.end()) {
1629 V = I->second; // Placeholder already exists, use it...
1630 $2.destroy();
1631 } else {
1632 std::string Name;
1633 if ($2.Type == ValID::NameVal) Name = $2.Name;
1634
1635 // Create the forward referenced global.
1636 GlobalValue *GV;
1637 if (const FunctionType *FTy =
1638 dyn_cast<FunctionType>(PT->getElementType())) {
1639 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1640 CurModule.CurrentModule);
1641 } else {
1642 GV = new GlobalVariable(PT->getElementType(), false,
1643 GlobalValue::ExternalLinkage, 0,
1644 Name, CurModule.CurrentModule);
1645 }
1646
1647 // Keep track of the fact that we have a forward ref to recycle it
1648 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1649 V = GV;
1650 }
1651 }
1652
Reid Spencere2c32da2006-12-03 05:46:11 +00001653 $$ = cast<GlobalValue>(V);
1654 delete $1; // Free the type handle
Reid Spencer713eedc2006-08-18 08:43:06 +00001655 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001656 }
1657 | Types ConstExpr {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001658 if (!UpRefs.empty())
1659 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001660 if ($1->get() != $2->getType())
Reid Spencerca3d1cb2007-01-04 00:06:14 +00001661 GEN_ERROR("Mismatched types for constant expression: " +
1662 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001663 $$ = $2;
Reid Spencere2c32da2006-12-03 05:46:11 +00001664 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001665 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001666 }
1667 | Types ZEROINITIALIZER {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001668 if (!UpRefs.empty())
1669 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001670 const Type *Ty = $1->get();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001671 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencer713eedc2006-08-18 08:43:06 +00001672 GEN_ERROR("Cannot create a null initialized value of this type!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001673 $$ = Constant::getNullValue(Ty);
1674 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001675 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00001676 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001677 | IntType ESINT64VAL { // integral constants
Reid Spencer266e42b2006-12-23 06:05:41 +00001678 if (!ConstantInt::isValueValidForType($1, $2))
1679 GEN_ERROR("Constant value doesn't fit in type!");
1680 $$ = ConstantInt::get($1, $2);
1681 CHECK_FOR_ERROR
1682 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001683 | IntType EUINT64VAL { // integral constants
Reid Spencer266e42b2006-12-23 06:05:41 +00001684 if (!ConstantInt::isValueValidForType($1, $2))
1685 GEN_ERROR("Constant value doesn't fit in type!");
1686 $$ = ConstantInt::get($1, $2);
1687 CHECK_FOR_ERROR
1688 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001689 | BOOL TRUETOK { // Boolean constants
Zhou Sheng75b871f2007-01-11 12:24:14 +00001690 $$ = ConstantInt::getTrue();
Reid Spencer713eedc2006-08-18 08:43:06 +00001691 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001692 }
1693 | BOOL FALSETOK { // Boolean constants
Zhou Sheng75b871f2007-01-11 12:24:14 +00001694 $$ = ConstantInt::getFalse();
Reid Spencer713eedc2006-08-18 08:43:06 +00001695 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001696 }
1697 | FPType FPVAL { // Float & Double constants
Reid Spencere2c32da2006-12-03 05:46:11 +00001698 if (!ConstantFP::isValueValidForType($1, $2))
Reid Spencer713eedc2006-08-18 08:43:06 +00001699 GEN_ERROR("Floating point constant invalid for type!!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001700 $$ = ConstantFP::get($1, $2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001701 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001702 };
1703
1704
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001705ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001706 if (!UpRefs.empty())
1707 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001708 Constant *Val = $3;
1709 const Type *Ty = $5->get();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001710 if (!Val->getType()->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001711 GEN_ERROR("cast constant expression from a non-primitive type: '" +
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001712 Val->getType()->getDescription() + "'!");
1713 if (!Ty->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001714 GEN_ERROR("cast constant expression to a non-primitive type: '" +
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001715 Ty->getDescription() + "'!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001716 $$ = ConstantExpr::getCast($1, $3, $5->get());
1717 delete $5;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001718 }
1719 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001720 if (!isa<PointerType>($3->getType()))
Reid Spencer713eedc2006-08-18 08:43:06 +00001721 GEN_ERROR("GetElementPtr requires a pointer operand!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001722
Reid Spencere2c32da2006-12-03 05:46:11 +00001723 const Type *IdxTy =
1724 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
1725 if (!IdxTy)
1726 GEN_ERROR("Index list invalid for constant getelementptr!");
1727
1728 std::vector<Constant*> IdxVec;
1729 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1730 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001731 IdxVec.push_back(C);
1732 else
Reid Spencer713eedc2006-08-18 08:43:06 +00001733 GEN_ERROR("Indices to constant getelementptr must be constants!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001734
1735 delete $4;
1736
Reid Spencere2c32da2006-12-03 05:46:11 +00001737 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Reid Spencer713eedc2006-08-18 08:43:06 +00001738 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001739 }
1740 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer542964f2007-01-11 18:21:29 +00001741 if ($3->getType() != Type::Int1Ty)
Reid Spencer713eedc2006-08-18 08:43:06 +00001742 GEN_ERROR("Select condition must be of boolean type!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001743 if ($5->getType() != $7->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001744 GEN_ERROR("Select operand types must match!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001745 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001746 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001747 }
1748 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001749 if ($3->getType() != $5->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001750 GEN_ERROR("Binary operator types must match!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001751 CHECK_FOR_ERROR;
Reid Spencer844668d2006-12-05 19:16:11 +00001752 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001753 }
1754 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001755 if ($3->getType() != $5->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001756 GEN_ERROR("Logical operator types must match!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001757 if (!$3->getType()->isIntegral()) {
1758 if (!isa<PackedType>($3->getType()) ||
1759 !cast<PackedType>($3->getType())->getElementType()->isIntegral())
Reid Spencer713eedc2006-08-18 08:43:06 +00001760 GEN_ERROR("Logical operator requires integral operands!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001761 }
Reid Spencere2c32da2006-12-03 05:46:11 +00001762 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001763 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001764 }
Reid Spencerd2e0c342006-12-04 05:24:24 +00001765 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1766 if ($4->getType() != $6->getType())
Reid Spencere2c32da2006-12-03 05:46:11 +00001767 GEN_ERROR("icmp operand types must match!");
Reid Spencerd2e0c342006-12-04 05:24:24 +00001768 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencere2c32da2006-12-03 05:46:11 +00001769 }
Reid Spencerd2e0c342006-12-04 05:24:24 +00001770 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1771 if ($4->getType() != $6->getType())
Reid Spencere2c32da2006-12-03 05:46:11 +00001772 GEN_ERROR("fcmp operand types must match!");
Reid Spencerd2e0c342006-12-04 05:24:24 +00001773 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencere2c32da2006-12-03 05:46:11 +00001774 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001775 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001776 if ($5->getType() != Type::Int8Ty)
1777 GEN_ERROR("Shift count for shift constant must be i8 type!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001778 if (!$3->getType()->isInteger())
Reid Spencer713eedc2006-08-18 08:43:06 +00001779 GEN_ERROR("Shift constant expression requires integer operand!");
Reid Spencerfdff9382006-11-08 06:47:33 +00001780 CHECK_FOR_ERROR;
Reid Spencere2c32da2006-12-03 05:46:11 +00001781 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001782 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001783 }
1784 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001785 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencer713eedc2006-08-18 08:43:06 +00001786 GEN_ERROR("Invalid extractelement operands!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001787 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001788 CHECK_FOR_ERROR
Chris Lattneraebccf82006-04-08 03:55:17 +00001789 }
1790 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001791 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencer713eedc2006-08-18 08:43:06 +00001792 GEN_ERROR("Invalid insertelement operands!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001793 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001794 CHECK_FOR_ERROR
Chris Lattneraebccf82006-04-08 03:55:17 +00001795 }
1796 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001797 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencer713eedc2006-08-18 08:43:06 +00001798 GEN_ERROR("Invalid shufflevector operands!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001799 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001800 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001801 };
1802
Chris Lattneraebccf82006-04-08 03:55:17 +00001803
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001804// ConstVector - A list of comma separated constants.
1805ConstVector : ConstVector ',' ConstVal {
1806 ($$ = $1)->push_back($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001807 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001808 }
1809 | ConstVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00001810 $$ = new std::vector<Constant*>();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001811 $$->push_back($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001812 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001813 };
1814
1815
1816// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1817GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
1818
1819
1820//===----------------------------------------------------------------------===//
1821// Rules to match Modules
1822//===----------------------------------------------------------------------===//
1823
1824// Module rule: Capture the result of parsing the whole file into a result
1825// variable...
1826//
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001827Module
1828 : DefinitionList {
1829 $$ = ParserResult = CurModule.CurrentModule;
1830 CurModule.ModuleDone();
1831 CHECK_FOR_ERROR;
1832 }
1833 | /*empty*/ {
1834 $$ = ParserResult = CurModule.CurrentModule;
1835 CurModule.ModuleDone();
1836 CHECK_FOR_ERROR;
1837 }
1838 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001839
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001840DefinitionList
1841 : Definition
1842 | DefinitionList Definition
1843 ;
1844
1845Definition
1846 : DEFINE { CurFun.isDeclare = false } Function {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001847 CurFun.FunctionDone();
Reid Spencer713eedc2006-08-18 08:43:06 +00001848 CHECK_FOR_ERROR
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001849 }
1850 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer713eedc2006-08-18 08:43:06 +00001851 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001852 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001853 | MODULE ASM_TOK AsmBlock {
Reid Spencer713eedc2006-08-18 08:43:06 +00001854 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001855 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001856 | IMPLEMENTATION {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001857 // Emit an error if there are any unresolved types left.
1858 if (!CurModule.LateResolveTypes.empty()) {
1859 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
Reid Spencer713eedc2006-08-18 08:43:06 +00001860 if (DID.Type == ValID::NameVal) {
1861 GEN_ERROR("Reference to an undefined type: '"+DID.getName() + "'");
1862 } else {
1863 GEN_ERROR("Reference to an undefined type: #" + itostr(DID.Num));
1864 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001865 }
Reid Spencer713eedc2006-08-18 08:43:06 +00001866 CHECK_FOR_ERROR
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001867 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001868 | OptAssign TYPE Types {
1869 if (!UpRefs.empty())
1870 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001871 // Eagerly resolve types. This is not an optimization, this is a
1872 // requirement that is due to the fact that we could have this:
1873 //
1874 // %list = type { %list * }
1875 // %list = type { %list * } ; repeated type decl
1876 //
1877 // If types are not resolved eagerly, then the two types will not be
1878 // determined to be the same type!
1879 //
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001880 ResolveTypeTo($1, *$3);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001881
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001882 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer309080a2006-09-28 19:28:24 +00001883 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001884 // If this is a named type that is not a redefinition, add it to the slot
1885 // table.
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001886 CurModule.Types.push_back(*$3);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001887 }
Reid Spencere2c32da2006-12-03 05:46:11 +00001888
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001889 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001890 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001891 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001892 | OptAssign TYPE VOID {
1893 ResolveTypeTo($1, $3);
1894
1895 if (!setTypeName($3, $1) && !$1) {
1896 CHECK_FOR_ERROR
1897 // If this is a named type that is not a redefinition, add it to the slot
1898 // table.
1899 CurModule.Types.push_back($3);
1900 }
1901 CHECK_FOR_ERROR
1902 }
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001903 | OptAssign GVVisibilityStyle GlobalType ConstVal { /* "Externally Visible" Linkage */
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001904 if ($4 == 0)
1905 GEN_ERROR("Global value initializer is not a constant!");
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001906 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
1907 $2, $3, $4->getType(), $4);
Reid Spencer309080a2006-09-28 19:28:24 +00001908 CHECK_FOR_ERROR
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001909 } GlobalVarAttributes {
1910 CurGV = 0;
1911 }
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001912 | OptAssign GVInternalLinkage GVVisibilityStyle GlobalType ConstVal {
1913 if ($5 == 0)
1914 GEN_ERROR("Global value initializer is not a constant!");
1915 CurGV = ParseGlobalVariable($1, $2, $3, $4, $5->getType(), $5);
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001916 CHECK_FOR_ERROR
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001917 } GlobalVarAttributes {
1918 CurGV = 0;
1919 }
1920 | OptAssign GVExternalLinkage GVVisibilityStyle GlobalType Types {
1921 if (!UpRefs.empty())
1922 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
1923 CurGV = ParseGlobalVariable($1, $2, $3, $4, *$5, 0);
1924 CHECK_FOR_ERROR
1925 delete $5;
Reid Spencer309080a2006-09-28 19:28:24 +00001926 } GlobalVarAttributes {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001927 CurGV = 0;
1928 CHECK_FOR_ERROR
1929 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001930 | TARGET TargetDefinition {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001931 CHECK_FOR_ERROR
1932 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001933 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer713eedc2006-08-18 08:43:06 +00001934 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001935 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001936 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001937
1938
1939AsmBlock : STRINGCONSTANT {
1940 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
1941 char *EndStr = UnEscapeLexed($1, true);
1942 std::string NewAsm($1, EndStr);
1943 free($1);
1944
1945 if (AsmSoFar.empty())
1946 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
1947 else
1948 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
Reid Spencer713eedc2006-08-18 08:43:06 +00001949 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001950};
1951
1952BigOrLittle : BIG { $$ = Module::BigEndian; };
1953BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1954
1955TargetDefinition : ENDIAN '=' BigOrLittle {
1956 CurModule.CurrentModule->setEndianness($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001957 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001958 }
1959 | POINTERSIZE '=' EUINT64VAL {
1960 if ($3 == 32)
1961 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1962 else if ($3 == 64)
1963 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1964 else
Reid Spencer713eedc2006-08-18 08:43:06 +00001965 GEN_ERROR("Invalid pointer size: '" + utostr($3) + "'!");
1966 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001967 }
1968 | TRIPLE '=' STRINGCONSTANT {
1969 CurModule.CurrentModule->setTargetTriple($3);
1970 free($3);
John Criswell6af0b122006-10-24 19:09:48 +00001971 }
Chris Lattner7d1d0342006-10-22 06:08:13 +00001972 | DATALAYOUT '=' STRINGCONSTANT {
Owen Anderson85690f32006-10-18 02:21:48 +00001973 CurModule.CurrentModule->setDataLayout($3);
1974 free($3);
Owen Anderson85690f32006-10-18 02:21:48 +00001975 };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001976
1977LibrariesDefinition : '[' LibList ']';
1978
1979LibList : LibList ',' STRINGCONSTANT {
1980 CurModule.CurrentModule->addLibrary($3);
1981 free($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001982 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001983 }
1984 | STRINGCONSTANT {
1985 CurModule.CurrentModule->addLibrary($1);
1986 free($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001987 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001988 }
1989 | /* empty: end of list */ {
Reid Spencer713eedc2006-08-18 08:43:06 +00001990 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001991 }
1992 ;
1993
1994//===----------------------------------------------------------------------===//
1995// Rules to match Function Headers
1996//===----------------------------------------------------------------------===//
1997
1998Name : VAR_ID | STRINGCONSTANT;
1999OptName : Name | /*empty*/ { $$ = 0; };
2000
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002001ArgListH : ArgListH ',' Types OptParamAttrs OptName {
2002 if (!UpRefs.empty())
2003 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
2004 if (*$3 == Type::VoidTy)
2005 GEN_ERROR("void typed arguments are invalid!");
2006 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002007 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002008 $1->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002009 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002010 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002011 | Types OptParamAttrs OptName {
2012 if (!UpRefs.empty())
2013 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2014 if (*$1 == Type::VoidTy)
2015 GEN_ERROR("void typed arguments are invalid!");
2016 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2017 $$ = new ArgListType;
2018 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002019 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002020 };
2021
2022ArgList : ArgListH {
2023 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002024 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002025 }
2026 | ArgListH ',' DOTDOTDOT {
2027 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002028 struct ArgListEntry E;
2029 E.Ty = new PATypeHolder(Type::VoidTy);
2030 E.Name = 0;
2031 E.Attrs = FunctionType::NoAttributeSet;
2032 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002033 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002034 }
2035 | DOTDOTDOT {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002036 $$ = new ArgListType;
2037 struct ArgListEntry E;
2038 E.Ty = new PATypeHolder(Type::VoidTy);
2039 E.Name = 0;
2040 E.Attrs = FunctionType::NoAttributeSet;
2041 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002042 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002043 }
2044 | /* empty */ {
2045 $$ = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00002046 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002047 };
2048
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002049FunctionHeaderH : OptCallingConv ResultTypes Name '(' ArgList ')'
2050 OptFuncAttrs OptSection OptAlign {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002051 UnEscapeLexed($3);
2052 std::string FunctionName($3);
2053 free($3); // Free strdup'd memory!
2054
Reid Spencer87622ae2007-01-02 21:54:12 +00002055 // Check the function result for abstractness if this is a define. We should
2056 // have no abstract types at this point
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002057 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2058 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer87622ae2007-01-02 21:54:12 +00002059
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002060 std::vector<const Type*> ParamTypeList;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002061 std::vector<FunctionType::ParameterAttributes> ParamAttrs;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002062 ParamAttrs.push_back($7);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002063 if ($5) { // If there are arguments...
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002064 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I) {
2065 const Type* Ty = I->Ty->get();
Reid Spencer87622ae2007-01-02 21:54:12 +00002066 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2067 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002068 ParamTypeList.push_back(Ty);
2069 if (Ty != Type::VoidTy)
2070 ParamAttrs.push_back(I->Attrs);
2071 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002072 }
2073
2074 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2075 if (isVarArg) ParamTypeList.pop_back();
2076
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002077 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg,
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002078 ParamAttrs);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002079 const PointerType *PFT = PointerType::get(FT);
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002080 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002081
2082 ValID ID;
2083 if (!FunctionName.empty()) {
2084 ID = ValID::create((char*)FunctionName.c_str());
2085 } else {
2086 ID = ValID::create((int)CurModule.Values[PFT].size());
2087 }
2088
2089 Function *Fn = 0;
2090 // See if this function was forward referenced. If so, recycle the object.
2091 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2092 // Move the function to the end of the list, from whereever it was
2093 // previously inserted.
2094 Fn = cast<Function>(FWRef);
2095 CurModule.CurrentModule->getFunctionList().remove(Fn);
2096 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2097 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
2098 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
2099 // If this is the case, either we need to be a forward decl, or it needs
2100 // to be.
2101 if (!CurFun.isDeclare && !Fn->isExternal())
Reid Spencer713eedc2006-08-18 08:43:06 +00002102 GEN_ERROR("Redefinition of function '" + FunctionName + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002103
2104 // Make sure to strip off any argument names so we can't get conflicts.
2105 if (Fn->isExternal())
2106 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2107 AI != AE; ++AI)
2108 AI->setName("");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002109 } else { // Not already defined?
2110 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
2111 CurModule.CurrentModule);
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002112
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002113 InsertValue(Fn, CurModule.Values);
2114 }
2115
2116 CurFun.FunctionStart(Fn);
Anton Korobeynikov0ab01ff2006-09-17 13:06:18 +00002117
2118 if (CurFun.isDeclare) {
2119 // If we have declaration, always overwrite linkage. This will allow us to
2120 // correctly handle cases, when pointer to function is passed as argument to
2121 // another function.
2122 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002123 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov0ab01ff2006-09-17 13:06:18 +00002124 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002125 Fn->setCallingConv($1);
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002126 Fn->setAlignment($9);
2127 if ($8) {
2128 Fn->setSection($8);
2129 free($8);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002130 }
2131
2132 // Add all of the arguments we parsed to the function...
2133 if ($5) { // Is null if empty...
2134 if (isVarArg) { // Nuke the last entry
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002135 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0&&
Reid Spencere2c32da2006-12-03 05:46:11 +00002136 "Not a varargs marker!");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002137 delete $5->back().Ty;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002138 $5->pop_back(); // Delete the last entry
2139 }
2140 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002141 unsigned Idx = 1;
2142 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I, ++ArgIt) {
2143 delete I->Ty; // Delete the typeholder...
2144 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer309080a2006-09-28 19:28:24 +00002145 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002146 InsertValue(ArgIt);
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002147 Idx++;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002148 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002149
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002150 delete $5; // We're now done with the argument list
2151 }
Reid Spencer713eedc2006-08-18 08:43:06 +00002152 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002153};
2154
2155BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2156
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002157FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002158 $$ = CurFun.CurrentFunction;
2159
2160 // Make sure that we keep track of the linkage type even if there was a
2161 // previous "declare".
2162 $$->setLinkage($1);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002163 $$->setVisibility($2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002164};
2165
2166END : ENDTOK | '}'; // Allow end of '}' to end a function
2167
2168Function : BasicBlockList END {
2169 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002170 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002171};
2172
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002173FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002174 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002175 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002176 $$ = CurFun.CurrentFunction;
2177 CurFun.FunctionDone();
2178 CHECK_FOR_ERROR
2179 };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002180
2181//===----------------------------------------------------------------------===//
2182// Rules to match Basic Blocks
2183//===----------------------------------------------------------------------===//
2184
2185OptSideEffect : /* empty */ {
2186 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002187 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002188 }
2189 | SIDEEFFECT {
2190 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002191 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002192 };
2193
2194ConstValueRef : ESINT64VAL { // A reference to a direct constant
2195 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002196 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002197 }
2198 | EUINT64VAL {
2199 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002200 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002201 }
2202 | FPVAL { // Perhaps it's an FP constant?
2203 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002204 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002205 }
2206 | TRUETOK {
Zhou Sheng75b871f2007-01-11 12:24:14 +00002207 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer713eedc2006-08-18 08:43:06 +00002208 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002209 }
2210 | FALSETOK {
Zhou Sheng75b871f2007-01-11 12:24:14 +00002211 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer713eedc2006-08-18 08:43:06 +00002212 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002213 }
2214 | NULL_TOK {
2215 $$ = ValID::createNull();
Reid Spencer713eedc2006-08-18 08:43:06 +00002216 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002217 }
2218 | UNDEF {
2219 $$ = ValID::createUndef();
Reid Spencer713eedc2006-08-18 08:43:06 +00002220 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002221 }
2222 | ZEROINITIALIZER { // A vector zero constant.
2223 $$ = ValID::createZeroInit();
Reid Spencer713eedc2006-08-18 08:43:06 +00002224 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002225 }
2226 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencere2c32da2006-12-03 05:46:11 +00002227 const Type *ETy = (*$2)[0]->getType();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002228 int NumElements = $2->size();
2229
2230 PackedType* pt = PackedType::get(ETy, NumElements);
2231 PATypeHolder* PTy = new PATypeHolder(
Reid Spencere2c32da2006-12-03 05:46:11 +00002232 HandleUpRefs(
2233 PackedType::get(
2234 ETy,
2235 NumElements)
2236 )
2237 );
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002238
2239 // Verify all elements are correct type!
2240 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencere2c32da2006-12-03 05:46:11 +00002241 if (ETy != (*$2)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002242 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002243 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00002244 (*$2)[i]->getType()->getDescription() + "'.");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002245 }
2246
Reid Spencere2c32da2006-12-03 05:46:11 +00002247 $$ = ValID::create(ConstantPacked::get(pt, *$2));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002248 delete PTy; delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002249 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002250 }
2251 | ConstExpr {
Reid Spencere2c32da2006-12-03 05:46:11 +00002252 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002253 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002254 }
2255 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
2256 char *End = UnEscapeLexed($3, true);
2257 std::string AsmStr = std::string($3, End);
2258 End = UnEscapeLexed($5, true);
2259 std::string Constraints = std::string($5, End);
2260 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
2261 free($3);
2262 free($5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002263 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002264 };
2265
2266// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2267// another value.
2268//
2269SymbolicValueRef : INTVAL { // Is it an integer reference...?
2270 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002271 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002272 }
2273 | Name { // Is it a named reference...?
2274 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002275 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002276 };
2277
2278// ValueRef - A reference to a definition... either constant or symbolic
2279ValueRef : SymbolicValueRef | ConstValueRef;
2280
2281
2282// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2283// type immediately preceeds the value reference, and allows complex constant
2284// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2285ResolvedVal : Types ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002286 if (!UpRefs.empty())
2287 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2288 $$ = getVal(*$1, $2);
2289 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002290 CHECK_FOR_ERROR
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002291 }
2292 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002293
2294BasicBlockList : BasicBlockList BasicBlock {
2295 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002296 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002297 }
2298 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
2299 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002300 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002301 };
2302
2303
2304// Basic blocks are terminated by branching instructions:
2305// br, br/cc, switch, ret
2306//
2307BasicBlock : InstructionList OptAssign BBTerminatorInst {
2308 setValueName($3, $2);
Reid Spencer309080a2006-09-28 19:28:24 +00002309 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002310 InsertValue($3);
2311
2312 $1->getInstList().push_back($3);
2313 InsertValue($1);
2314 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002315 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002316 };
2317
2318InstructionList : InstructionList Inst {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002319 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2320 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2321 if (CI2->getParent() == 0)
2322 $1->getInstList().push_back(CI2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002323 $1->getInstList().push_back($2);
2324 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002325 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002326 }
2327 | /* empty */ {
Reid Spencer27642192006-12-05 23:29:42 +00002328 $$ = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Reid Spencer309080a2006-09-28 19:28:24 +00002329 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002330
2331 // Make sure to move the basic block to the correct location in the
2332 // function, instead of leaving it inserted wherever it was first
2333 // referenced.
2334 Function::BasicBlockListType &BBL =
2335 CurFun.CurrentFunction->getBasicBlockList();
2336 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer713eedc2006-08-18 08:43:06 +00002337 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002338 }
2339 | LABELSTR {
Reid Spencer27642192006-12-05 23:29:42 +00002340 $$ = getBBVal(ValID::create($1), true);
Reid Spencer309080a2006-09-28 19:28:24 +00002341 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002342
2343 // Make sure to move the basic block to the correct location in the
2344 // function, instead of leaving it inserted wherever it was first
2345 // referenced.
2346 Function::BasicBlockListType &BBL =
2347 CurFun.CurrentFunction->getBasicBlockList();
2348 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer713eedc2006-08-18 08:43:06 +00002349 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002350 };
2351
2352BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencere2c32da2006-12-03 05:46:11 +00002353 $$ = new ReturnInst($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00002354 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002355 }
2356 | RET VOID { // Return with no result...
2357 $$ = new ReturnInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002358 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002359 }
2360 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer309080a2006-09-28 19:28:24 +00002361 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002362 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002363 $$ = new BranchInst(tmpBB);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002364 } // Conditional Branch...
2365 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencer309080a2006-09-28 19:28:24 +00002366 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002367 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002368 BasicBlock* tmpBBB = getBBVal($9);
2369 CHECK_FOR_ERROR
Reid Spencer542964f2007-01-11 18:21:29 +00002370 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002371 CHECK_FOR_ERROR
2372 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002373 }
2374 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencere2c32da2006-12-03 05:46:11 +00002375 Value* tmpVal = getVal($2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002376 CHECK_FOR_ERROR
2377 BasicBlock* tmpBB = getBBVal($6);
2378 CHECK_FOR_ERROR
2379 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002380 $$ = S;
2381
2382 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2383 E = $8->end();
2384 for (; I != E; ++I) {
2385 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2386 S->addCase(CI, I->second);
2387 else
Reid Spencer713eedc2006-08-18 08:43:06 +00002388 GEN_ERROR("Switch case is constant, but not a simple integer!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002389 }
2390 delete $8;
Reid Spencer713eedc2006-08-18 08:43:06 +00002391 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002392 }
2393 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencere2c32da2006-12-03 05:46:11 +00002394 Value* tmpVal = getVal($2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002395 CHECK_FOR_ERROR
2396 BasicBlock* tmpBB = getBBVal($6);
2397 CHECK_FOR_ERROR
2398 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002399 $$ = S;
Reid Spencer713eedc2006-08-18 08:43:06 +00002400 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002401 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002402 | INVOKE OptCallingConv ResultTypes ValueRef '(' ValueRefList ')' OptFuncAttrs
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002403 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002404
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002405 // Handle the short syntax
2406 const PointerType *PFTy = 0;
2407 const FunctionType *Ty = 0;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002408 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002409 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2410 // Pull out the types of all of the arguments...
2411 std::vector<const Type*> ParamTypes;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002412 FunctionType::ParamAttrsList ParamAttrs;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002413 ParamAttrs.push_back($8);
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002414 for (ValueRefList::iterator I = $6->begin(), E = $6->end(); I != E; ++I) {
2415 const Type *Ty = I->Val->getType();
2416 if (Ty == Type::VoidTy)
2417 GEN_ERROR("Short call syntax cannot be used with varargs");
2418 ParamTypes.push_back(Ty);
2419 ParamAttrs.push_back(I->Attrs);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002420 }
2421
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002422 Ty = FunctionType::get($3->get(), ParamTypes, false, ParamAttrs);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002423 PFTy = PointerType::get(Ty);
2424 }
2425
2426 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer309080a2006-09-28 19:28:24 +00002427 CHECK_FOR_ERROR
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002428 BasicBlock *Normal = getBBVal($11);
Reid Spencer309080a2006-09-28 19:28:24 +00002429 CHECK_FOR_ERROR
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002430 BasicBlock *Except = getBBVal($14);
Reid Spencer309080a2006-09-28 19:28:24 +00002431 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002432
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002433 // Check the arguments
2434 ValueList Args;
2435 if ($6->empty()) { // Has no arguments?
2436 // Make sure no arguments is a good thing!
2437 if (Ty->getNumParams() != 0)
2438 GEN_ERROR("No arguments passed to a function that "
2439 "expects arguments!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002440 } else { // Has arguments?
2441 // Loop through FunctionType's arguments and ensure they are specified
2442 // correctly!
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002443 FunctionType::param_iterator I = Ty->param_begin();
2444 FunctionType::param_iterator E = Ty->param_end();
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002445 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002446
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002447 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2448 if (ArgI->Val->getType() != *I)
2449 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00002450 (*I)->getDescription() + "'!");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002451 Args.push_back(ArgI->Val);
2452 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002453
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002454 if (Ty->isVarArg()) {
2455 if (I == E)
2456 for (; ArgI != ArgE; ++ArgI)
2457 Args.push_back(ArgI->Val); // push the remaining varargs
2458 } else if (I != E || ArgI != ArgE)
Reid Spencere2c32da2006-12-03 05:46:11 +00002459 GEN_ERROR("Invalid number of parameters detected!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002460 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002461
2462 // Create the InvokeInst
2463 InvokeInst *II = new InvokeInst(V, Normal, Except, Args);
2464 II->setCallingConv($2);
2465 $$ = II;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002466 delete $6;
Reid Spencer713eedc2006-08-18 08:43:06 +00002467 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002468 }
2469 | UNWIND {
2470 $$ = new UnwindInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002471 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002472 }
2473 | UNREACHABLE {
2474 $$ = new UnreachableInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002475 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002476 };
2477
2478
2479
2480JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2481 $$ = $1;
Reid Spencere2c32da2006-12-03 05:46:11 +00002482 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Reid Spencer309080a2006-09-28 19:28:24 +00002483 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002484 if (V == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002485 GEN_ERROR("May only switch on a constant pool value!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002486
Reid Spencer309080a2006-09-28 19:28:24 +00002487 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002488 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002489 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002490 }
2491 | IntType ConstValueRef ',' LABEL ValueRef {
2492 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencere2c32da2006-12-03 05:46:11 +00002493 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Reid Spencer309080a2006-09-28 19:28:24 +00002494 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002495
2496 if (V == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002497 GEN_ERROR("May only switch on a constant pool value!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002498
Reid Spencer309080a2006-09-28 19:28:24 +00002499 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002500 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002501 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002502 };
2503
2504Inst : OptAssign InstVal {
2505 // Is this definition named?? if so, assign the name...
2506 setValueName($2, $1);
Reid Spencer309080a2006-09-28 19:28:24 +00002507 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002508 InsertValue($2);
2509 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002510 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002511};
2512
2513PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002514 if (!UpRefs.empty())
2515 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002516 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencere2c32da2006-12-03 05:46:11 +00002517 Value* tmpVal = getVal(*$1, $3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002518 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002519 BasicBlock* tmpBB = getBBVal($5);
2520 CHECK_FOR_ERROR
2521 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencere2c32da2006-12-03 05:46:11 +00002522 delete $1;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002523 }
2524 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2525 $$ = $1;
Reid Spencer309080a2006-09-28 19:28:24 +00002526 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002527 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002528 BasicBlock* tmpBB = getBBVal($6);
2529 CHECK_FOR_ERROR
2530 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002531 };
2532
2533
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002534ValueRefList : Types ValueRef OptParamAttrs {
2535 if (!UpRefs.empty())
2536 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2537 // Used for call and invoke instructions
2538 $$ = new ValueRefList();
2539 ValueRefListEntry E; E.Attrs = $3; E.Val = getVal($1->get(), $2);
2540 $$->push_back(E);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002541 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002542 | ValueRefList ',' Types ValueRef OptParamAttrs {
2543 if (!UpRefs.empty())
2544 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002545 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002546 ValueRefListEntry E; E.Attrs = $5; E.Val = getVal($3->get(), $4);
2547 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002548 CHECK_FOR_ERROR
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002549 }
2550 | /*empty*/ { $$ = new ValueRefList(); };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002551
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002552IndexList // Used for gep instructions and constant expressions
Reid Spencerd0da3e22006-12-31 21:47:02 +00002553 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002554 | IndexList ',' ResolvedVal {
2555 $$ = $1;
2556 $$->push_back($3);
2557 CHECK_FOR_ERROR
2558 }
Reid Spencerd0da3e22006-12-31 21:47:02 +00002559 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002560
2561OptTailCall : TAIL CALL {
2562 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002563 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002564 }
2565 | CALL {
2566 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002567 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002568 };
2569
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002570InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002571 if (!UpRefs.empty())
2572 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002573 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
2574 !isa<PackedType>((*$2).get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002575 GEN_ERROR(
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002576 "Arithmetic operator requires integer, FP, or packed operands!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002577 if (isa<PackedType>((*$2).get()) &&
2578 ($1 == Instruction::URem ||
2579 $1 == Instruction::SRem ||
2580 $1 == Instruction::FRem))
Reid Spencerde46e482006-11-02 20:25:50 +00002581 GEN_ERROR("U/S/FRem not supported on packed types!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002582 Value* val1 = getVal(*$2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002583 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002584 Value* val2 = getVal(*$2, $5);
Reid Spencer309080a2006-09-28 19:28:24 +00002585 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002586 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002587 if ($$ == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002588 GEN_ERROR("binary operator returned null!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002589 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002590 }
2591 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002592 if (!UpRefs.empty())
2593 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002594 if (!(*$2)->isIntegral()) {
2595 if (!isa<PackedType>($2->get()) ||
2596 !cast<PackedType>($2->get())->getElementType()->isIntegral())
Reid Spencer713eedc2006-08-18 08:43:06 +00002597 GEN_ERROR("Logical operator requires integral operands!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002598 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002599 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002600 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002601 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer309080a2006-09-28 19:28:24 +00002602 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002603 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002604 if ($$ == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002605 GEN_ERROR("binary operator returned null!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002606 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002607 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002608 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002609 if (!UpRefs.empty())
2610 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer494289f2007-01-04 02:57:52 +00002611 if (isa<PackedType>((*$3).get()))
2612 GEN_ERROR("Packed types not supported by icmp instruction");
Reid Spencere2c32da2006-12-03 05:46:11 +00002613 Value* tmpVal1 = getVal(*$3, $4);
2614 CHECK_FOR_ERROR
2615 Value* tmpVal2 = getVal(*$3, $6);
2616 CHECK_FOR_ERROR
2617 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2618 if ($$ == 0)
2619 GEN_ERROR("icmp operator returned null!");
2620 }
2621 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002622 if (!UpRefs.empty())
2623 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer494289f2007-01-04 02:57:52 +00002624 if (isa<PackedType>((*$3).get()))
2625 GEN_ERROR("Packed types not supported by fcmp instruction");
Reid Spencere2c32da2006-12-03 05:46:11 +00002626 Value* tmpVal1 = getVal(*$3, $4);
2627 CHECK_FOR_ERROR
2628 Value* tmpVal2 = getVal(*$3, $6);
2629 CHECK_FOR_ERROR
2630 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2631 if ($$ == 0)
2632 GEN_ERROR("fcmp operator returned null!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002633 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002634 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002635 if ($4->getType() != Type::Int8Ty)
2636 GEN_ERROR("Shift amount must be i8 type!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002637 if (!$2->getType()->isInteger())
Reid Spencer713eedc2006-08-18 08:43:06 +00002638 GEN_ERROR("Shift constant expression requires integer operand!");
Reid Spencerfdff9382006-11-08 06:47:33 +00002639 CHECK_FOR_ERROR;
Reid Spencere2c32da2006-12-03 05:46:11 +00002640 $$ = new ShiftInst($1, $2, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002641 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002642 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002643 | CastOps ResolvedVal TO Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002644 if (!UpRefs.empty())
2645 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002646 Value* Val = $2;
2647 const Type* Ty = $4->get();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002648 if (!Val->getType()->isFirstClassType())
2649 GEN_ERROR("cast from a non-primitive type: '" +
2650 Val->getType()->getDescription() + "'!");
2651 if (!Ty->isFirstClassType())
2652 GEN_ERROR("cast to a non-primitive type: '" + Ty->getDescription() +"'!");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002653 $$ = CastInst::create($1, Val, $4->get());
Reid Spencere2c32da2006-12-03 05:46:11 +00002654 delete $4;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002655 }
2656 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer542964f2007-01-11 18:21:29 +00002657 if ($2->getType() != Type::Int1Ty)
Reid Spencer713eedc2006-08-18 08:43:06 +00002658 GEN_ERROR("select condition must be boolean!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002659 if ($4->getType() != $6->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002660 GEN_ERROR("select value types should match!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002661 $$ = new SelectInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002662 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002663 }
2664 | VAARG ResolvedVal ',' Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002665 if (!UpRefs.empty())
2666 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002667 $$ = new VAArgInst($2, *$4);
2668 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00002669 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002670 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002671 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002672 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencer713eedc2006-08-18 08:43:06 +00002673 GEN_ERROR("Invalid extractelement operands!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002674 $$ = new ExtractElementInst($2, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002675 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002676 }
2677 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002678 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencer713eedc2006-08-18 08:43:06 +00002679 GEN_ERROR("Invalid insertelement operands!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002680 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002681 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002682 }
Chris Lattner9ff96a72006-04-08 01:18:56 +00002683 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002684 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencer713eedc2006-08-18 08:43:06 +00002685 GEN_ERROR("Invalid shufflevector operands!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002686 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002687 CHECK_FOR_ERROR
Chris Lattner9ff96a72006-04-08 01:18:56 +00002688 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002689 | PHI_TOK PHIList {
2690 const Type *Ty = $2->front().first->getType();
2691 if (!Ty->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002692 GEN_ERROR("PHI node operands must be of first class type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002693 $$ = new PHINode(Ty);
2694 ((PHINode*)$$)->reserveOperandSpace($2->size());
2695 while ($2->begin() != $2->end()) {
2696 if ($2->front().first->getType() != Ty)
Reid Spencer713eedc2006-08-18 08:43:06 +00002697 GEN_ERROR("All elements of a PHI node must be of the same type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002698 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
2699 $2->pop_front();
2700 }
2701 delete $2; // Free the list...
Reid Spencer713eedc2006-08-18 08:43:06 +00002702 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002703 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002704 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ValueRefList ')'
2705 OptFuncAttrs {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002706
2707 // Handle the short syntax
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002708 const PointerType *PFTy = 0;
2709 const FunctionType *Ty = 0;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002710 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002711 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2712 // Pull out the types of all of the arguments...
2713 std::vector<const Type*> ParamTypes;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002714 FunctionType::ParamAttrsList ParamAttrs;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002715 ParamAttrs.push_back($8);
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002716 for (ValueRefList::iterator I = $6->begin(), E = $6->end(); I != E; ++I) {
2717 const Type *Ty = I->Val->getType();
2718 if (Ty == Type::VoidTy)
2719 GEN_ERROR("Short call syntax cannot be used with varargs");
2720 ParamTypes.push_back(Ty);
2721 ParamAttrs.push_back(I->Attrs);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002722 }
2723
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002724 Ty = FunctionType::get($3->get(), ParamTypes, false, ParamAttrs);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002725 PFTy = PointerType::get(Ty);
2726 }
2727
2728 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer309080a2006-09-28 19:28:24 +00002729 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002730
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002731 // Check the arguments
2732 ValueList Args;
2733 if ($6->empty()) { // Has no arguments?
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002734 // Make sure no arguments is a good thing!
2735 if (Ty->getNumParams() != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002736 GEN_ERROR("No arguments passed to a function that "
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002737 "expects arguments!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002738 } else { // Has arguments?
2739 // Loop through FunctionType's arguments and ensure they are specified
2740 // correctly!
2741 //
2742 FunctionType::param_iterator I = Ty->param_begin();
2743 FunctionType::param_iterator E = Ty->param_end();
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002744 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002745
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002746 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2747 if (ArgI->Val->getType() != *I)
2748 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00002749 (*I)->getDescription() + "'!");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002750 Args.push_back(ArgI->Val);
2751 }
2752 if (Ty->isVarArg()) {
2753 if (I == E)
2754 for (; ArgI != ArgE; ++ArgI)
2755 Args.push_back(ArgI->Val); // push the remaining varargs
2756 } else if (I != E || ArgI != ArgE)
Reid Spencer713eedc2006-08-18 08:43:06 +00002757 GEN_ERROR("Invalid number of parameters detected!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002758 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002759 // Create the call node
2760 CallInst *CI = new CallInst(V, Args);
2761 CI->setTailCall($1);
2762 CI->setCallingConv($2);
2763 $$ = CI;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002764 delete $6;
Reid Spencer713eedc2006-08-18 08:43:06 +00002765 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002766 }
2767 | MemoryInst {
2768 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002769 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002770 };
2771
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002772OptVolatile : VOLATILE {
2773 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002774 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002775 }
2776 | /* empty */ {
2777 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002778 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002779 };
2780
2781
2782
2783MemoryInst : MALLOC Types OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002784 if (!UpRefs.empty())
2785 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002786 $$ = new MallocInst(*$2, 0, $3);
2787 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002788 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002789 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +00002790 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002791 if (!UpRefs.empty())
2792 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002793 Value* tmpVal = getVal($4, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002794 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002795 $$ = new MallocInst(*$2, tmpVal, $6);
2796 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002797 }
2798 | ALLOCA Types OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002799 if (!UpRefs.empty())
2800 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002801 $$ = new AllocaInst(*$2, 0, $3);
2802 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002803 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002804 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +00002805 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002806 if (!UpRefs.empty())
2807 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002808 Value* tmpVal = getVal($4, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002809 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002810 $$ = new AllocaInst(*$2, tmpVal, $6);
2811 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002812 }
2813 | FREE ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002814 if (!isa<PointerType>($2->getType()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002815 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencere2c32da2006-12-03 05:46:11 +00002816 $2->getType()->getDescription() + "!");
2817 $$ = new FreeInst($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00002818 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002819 }
2820
2821 | OptVolatile LOAD Types ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002822 if (!UpRefs.empty())
2823 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002824 if (!isa<PointerType>($3->get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002825 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencere2c32da2006-12-03 05:46:11 +00002826 (*$3)->getDescription());
2827 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002828 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencere2c32da2006-12-03 05:46:11 +00002829 (*$3)->getDescription());
2830 Value* tmpVal = getVal(*$3, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002831 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002832 $$ = new LoadInst(tmpVal, "", $1);
Reid Spencere2c32da2006-12-03 05:46:11 +00002833 delete $3;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002834 }
2835 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002836 if (!UpRefs.empty())
2837 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002838 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002839 if (!PT)
Reid Spencer713eedc2006-08-18 08:43:06 +00002840 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencere2c32da2006-12-03 05:46:11 +00002841 (*$5)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002842 const Type *ElTy = PT->getElementType();
Reid Spencere2c32da2006-12-03 05:46:11 +00002843 if (ElTy != $3->getType())
2844 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002845 "' into space of type '" + ElTy->getDescription() + "'!");
2846
Reid Spencere2c32da2006-12-03 05:46:11 +00002847 Value* tmpVal = getVal(*$5, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002848 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002849 $$ = new StoreInst($3, tmpVal, $1);
2850 delete $5;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002851 }
2852 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002853 if (!UpRefs.empty())
2854 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002855 if (!isa<PointerType>($2->get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002856 GEN_ERROR("getelementptr insn requires pointer operand!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002857
Reid Spencere2c32da2006-12-03 05:46:11 +00002858 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Reid Spencer713eedc2006-08-18 08:43:06 +00002859 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00002860 (*$2)->getDescription()+ "'!");
2861 Value* tmpVal = getVal(*$2, $3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002862 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002863 $$ = new GetElementPtrInst(tmpVal, *$4);
2864 delete $2;
Reid Spencer309080a2006-09-28 19:28:24 +00002865 delete $4;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002866 };
2867
2868
2869%%
Reid Spencer713eedc2006-08-18 08:43:06 +00002870
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002871// common code from the two 'RunVMAsmParser' functions
2872static Module* RunParser(Module * M) {
2873
2874 llvmAsmlineno = 1; // Reset the current line number...
2875 CurModule.CurrentModule = M;
2876#if YYDEBUG
2877 yydebug = Debug;
2878#endif
2879
2880 // Check to make sure the parser succeeded
2881 if (yyparse()) {
2882 if (ParserResult)
2883 delete ParserResult;
2884 return 0;
2885 }
2886
2887 // Check to make sure that parsing produced a result
2888 if (!ParserResult)
2889 return 0;
2890
2891 // Reset ParserResult variable while saving its value for the result.
2892 Module *Result = ParserResult;
2893 ParserResult = 0;
2894
2895 return Result;
2896}
2897
Reid Spencer713eedc2006-08-18 08:43:06 +00002898void llvm::GenerateError(const std::string &message, int LineNo) {
2899 if (LineNo == -1) LineNo = llvmAsmlineno;
2900 // TODO: column number in exception
2901 if (TheParseError)
2902 TheParseError->setError(CurFilename, message, LineNo);
2903 TriggerError = 1;
2904}
2905
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002906int yyerror(const char *ErrorMsg) {
2907 std::string where
2908 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2909 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
2910 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
2911 if (yychar == YYEMPTY || yychar == 0)
2912 errMsg += "end-of-file.";
2913 else
2914 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Reid Spencer713eedc2006-08-18 08:43:06 +00002915 GenerateError(errMsg);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002916 return 0;
2917}