blob: 229845d5e3b887f2251c3e87e09a0c9ed5eefa29 [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) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000273 case ValID::LocalID: // Is it a numbered definition?
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000274 // Module constants occupy the lowest numbered slots...
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000275 if (D.Num < CurModule.Types.size())
276 return CurModule.Types[D.Num];
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000277 break;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000278 case ValID::LocalName: // Is it a named definition?
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000279 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 Spencer8d6d4b8e2007-01-26 08:05:27 +0000297 if (D.Type == ValID::LocalName) {
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 Spencer8d6d4b8e2007-01-26 08:05:27 +0000301 GenerateError("Reference to an undefined type: #" + utostr(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
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000315// getValNonImprovising - Look up the value specified by the provided type and
316// the provided ValID. If the value exists and has already been defined, return
317// it. Otherwise return null.
318//
319static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Reid Spencer309080a2006-09-28 19:28:24 +0000320 if (isa<FunctionType>(Ty)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000321 GenerateError("Functions are not values and "
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000322 "must be referenced as pointers");
Reid Spencer309080a2006-09-28 19:28:24 +0000323 return 0;
324 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000325
326 switch (D.Type) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000327 case ValID::LocalID: { // Is it a numbered definition?
328 // Module constants occupy the lowest numbered slots.
329 std::map<const Type*,ValueList>::iterator VI = CurFun.Values.find(Ty);
330 // Make sure that our type is within bounds.
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000331 if (VI == CurFun.Values.end()) return 0;
332
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000333 // Check that the number is within bounds.
334 if (D.Num >= VI->second.size()) return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000335
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000336 return VI->second[D.Num];
337 }
338 case ValID::GlobalID: { // Is it a numbered definition?
339 unsigned Num = D.Num;
340
341 // Module constants occupy the lowest numbered slots...
342 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
343 if (VI == CurModule.Values.end()) return 0;
344 if (D.Num >= VI->second.size()) return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000345 return VI->second[Num];
346 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000347
348 case ValID::LocalName: { // Is it a named definition?
349 if (!inFunctionScope()) return 0;
350 SymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
351 Value *N = SymTab.lookup(Ty, D.Name);
352 if (N == 0) return 0;
353
354 D.destroy(); // Free old strdup'd memory...
355 return N;
356 }
357 case ValID::GlobalName: { // Is it a named definition?
358 SymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable();
359 Value *N = SymTab.lookup(Ty, D.Name);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000360 if (N == 0) return 0;
361
362 D.destroy(); // Free old strdup'd memory...
363 return N;
364 }
365
366 // Check to make sure that "Ty" is an integral type, and that our
367 // value will fit into the specified type...
368 case ValID::ConstSIntVal: // Is it a constant pool reference??
Reid Spencere0fc4df2006-10-20 07:07:24 +0000369 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000370 GenerateError("Signed integral constant '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000371 itostr(D.ConstPool64) + "' is invalid for type '" +
372 Ty->getDescription() + "'!");
Reid Spencer309080a2006-09-28 19:28:24 +0000373 return 0;
374 }
Reid Spencere0fc4df2006-10-20 07:07:24 +0000375 return ConstantInt::get(Ty, D.ConstPool64);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000376
377 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Reid Spencere0fc4df2006-10-20 07:07:24 +0000378 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
379 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000380 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000381 "' is invalid or out of range!");
Reid Spencer309080a2006-09-28 19:28:24 +0000382 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000383 } else { // This is really a signed reference. Transmogrify.
Reid Spencere0fc4df2006-10-20 07:07:24 +0000384 return ConstantInt::get(Ty, D.ConstPool64);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000385 }
386 } else {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000387 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000388 }
389
390 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Reid Spencer309080a2006-09-28 19:28:24 +0000391 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000392 GenerateError("FP constant invalid for type!!");
Reid Spencer309080a2006-09-28 19:28:24 +0000393 return 0;
394 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000395 return ConstantFP::get(Ty, D.ConstPoolFP);
396
397 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer309080a2006-09-28 19:28:24 +0000398 if (!isa<PointerType>(Ty)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000399 GenerateError("Cannot create a a non pointer null!");
Reid Spencer309080a2006-09-28 19:28:24 +0000400 return 0;
401 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000402 return ConstantPointerNull::get(cast<PointerType>(Ty));
403
404 case ValID::ConstUndefVal: // Is it an undef value?
405 return UndefValue::get(Ty);
406
407 case ValID::ConstZeroVal: // Is it a zero value?
408 return Constant::getNullValue(Ty);
409
410 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer309080a2006-09-28 19:28:24 +0000411 if (D.ConstantValue->getType() != Ty) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000412 GenerateError("Constant expression type different from required type!");
Reid Spencer309080a2006-09-28 19:28:24 +0000413 return 0;
414 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000415 return D.ConstantValue;
416
417 case ValID::InlineAsmVal: { // Inline asm expression
418 const PointerType *PTy = dyn_cast<PointerType>(Ty);
419 const FunctionType *FTy =
420 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer309080a2006-09-28 19:28:24 +0000421 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000422 GenerateError("Invalid type for asm constraint string!");
Reid Spencer309080a2006-09-28 19:28:24 +0000423 return 0;
424 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000425 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
426 D.IAD->HasSideEffects);
427 D.destroy(); // Free InlineAsmDescriptor.
428 return IA;
429 }
430 default:
431 assert(0 && "Unhandled case!");
432 return 0;
433 } // End of switch
434
435 assert(0 && "Unhandled case!");
436 return 0;
437}
438
439// getVal - This function is identical to getValNonImprovising, except that if a
440// value is not already defined, it "improvises" by creating a placeholder var
441// that looks and acts just like the requested variable. When the value is
442// defined later, all uses of the placeholder variable are replaced with the
443// real thing.
444//
445static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer309080a2006-09-28 19:28:24 +0000446 if (Ty == Type::LabelTy) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000447 GenerateError("Cannot use a basic block here");
Reid Spencer309080a2006-09-28 19:28:24 +0000448 return 0;
449 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000450
451 // See if the value has already been defined.
452 Value *V = getValNonImprovising(Ty, ID);
453 if (V) return V;
Reid Spencer309080a2006-09-28 19:28:24 +0000454 if (TriggerError) return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000455
Reid Spencer309080a2006-09-28 19:28:24 +0000456 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000457 GenerateError("Invalid use of a composite type!");
Reid Spencer309080a2006-09-28 19:28:24 +0000458 return 0;
459 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000460
461 // If we reached here, we referenced either a symbol that we don't know about
462 // or an id number that hasn't been read yet. We may be referencing something
463 // forward, so just create an entry to be resolved later and get to it...
464 //
465 V = new Argument(Ty);
466
467 // Remember where this forward reference came from. FIXME, shouldn't we try
468 // to recycle these things??
469 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
470 llvmAsmlineno)));
471
472 if (inFunctionScope())
473 InsertValue(V, CurFun.LateResolveValues);
474 else
475 InsertValue(V, CurModule.LateResolveValues);
476 return V;
477}
478
479/// getBBVal - This is used for two purposes:
480/// * If isDefinition is true, a new basic block with the specified ID is being
481/// defined.
482/// * If isDefinition is true, this is a reference to a basic block, which may
483/// or may not be a forward reference.
484///
485static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
486 assert(inFunctionScope() && "Can't get basic block at global scope!");
487
488 std::string Name;
489 BasicBlock *BB = 0;
490 switch (ID.Type) {
Reid Spencer309080a2006-09-28 19:28:24 +0000491 default:
492 GenerateError("Illegal label reference " + ID.getName());
493 return 0;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000494 case ValID::LocalID: // Is it a numbered definition?
495 if (ID.Num >= CurFun.NumberedBlocks.size())
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000496 CurFun.NumberedBlocks.resize(ID.Num+1);
497 BB = CurFun.NumberedBlocks[ID.Num];
498 break;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000499 case ValID::LocalName: // Is it a named definition?
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000500 Name = ID.Name;
501 if (Value *N = CurFun.CurrentFunction->
Reid Spencer32af9e82007-01-06 07:24:44 +0000502 getValueSymbolTable().lookup(Type::LabelTy, Name))
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000503 BB = cast<BasicBlock>(N);
504 break;
505 }
506
507 // See if the block has already been defined.
508 if (BB) {
509 // If this is the definition of the block, make sure the existing value was
510 // just a forward reference. If it was a forward reference, there will be
511 // an entry for it in the PlaceHolderInfo map.
Reid Spencer309080a2006-09-28 19:28:24 +0000512 if (isDefinition && !CurFun.BBForwardRefs.erase(BB)) {
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000513 // The existing value was a definition, not a forward reference.
Reid Spencer713eedc2006-08-18 08:43:06 +0000514 GenerateError("Redefinition of label " + ID.getName());
Reid Spencer309080a2006-09-28 19:28:24 +0000515 return 0;
516 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000517
518 ID.destroy(); // Free strdup'd memory.
519 return BB;
520 }
521
522 // Otherwise this block has not been seen before.
523 BB = new BasicBlock("", CurFun.CurrentFunction);
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000524 if (ID.Type == ValID::LocalName) {
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000525 BB->setName(ID.Name);
526 } else {
527 CurFun.NumberedBlocks[ID.Num] = BB;
528 }
529
530 // If this is not a definition, keep track of it so we can use it as a forward
531 // reference.
532 if (!isDefinition) {
533 // Remember where this forward reference came from.
534 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
535 } else {
536 // The forward declaration could have been inserted anywhere in the
537 // function: insert it into the correct place now.
538 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
539 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
540 }
541 ID.destroy();
542 return BB;
543}
544
545
546//===----------------------------------------------------------------------===//
547// Code to handle forward references in instructions
548//===----------------------------------------------------------------------===//
549//
550// This code handles the late binding needed with statements that reference
551// values not defined yet... for example, a forward branch, or the PHI node for
552// a loop body.
553//
554// This keeps a table (CurFun.LateResolveValues) of all such forward references
555// and back patchs after we are done.
556//
557
558// ResolveDefinitions - If we could not resolve some defs at parsing
559// time (forward branches, phi functions for loops, etc...) resolve the
560// defs now...
561//
562static void
563ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
564 std::map<const Type*,ValueList> *FutureLateResolvers) {
565 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
566 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
567 E = LateResolvers.end(); LRI != E; ++LRI) {
568 ValueList &List = LRI->second;
569 while (!List.empty()) {
570 Value *V = List.back();
571 List.pop_back();
572
573 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
574 CurModule.PlaceHolderInfo.find(V);
575 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
576
577 ValID &DID = PHI->second.first;
578
579 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Reid Spencer309080a2006-09-28 19:28:24 +0000580 if (TriggerError)
581 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000582 if (TheRealValue) {
583 V->replaceAllUsesWith(TheRealValue);
584 delete V;
585 CurModule.PlaceHolderInfo.erase(PHI);
586 } else if (FutureLateResolvers) {
587 // Functions have their unresolved items forwarded to the module late
588 // resolver table
589 InsertValue(V, *FutureLateResolvers);
590 } else {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000591 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000592 GenerateError("Reference to an invalid definition: '" +DID.getName()+
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000593 "' of type '" + V->getType()->getDescription() + "'",
594 PHI->second.second);
Reid Spencer309080a2006-09-28 19:28:24 +0000595 return;
596 } else {
Reid Spencer713eedc2006-08-18 08:43:06 +0000597 GenerateError("Reference to an invalid definition: #" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000598 itostr(DID.Num) + " of type '" +
599 V->getType()->getDescription() + "'",
600 PHI->second.second);
Reid Spencer309080a2006-09-28 19:28:24 +0000601 return;
602 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000603 }
604 }
605 }
606
607 LateResolvers.clear();
608}
609
610// ResolveTypeTo - A brand new type was just declared. This means that (if
611// name is not null) things referencing Name can be resolved. Otherwise, things
612// refering to the number can be resolved. Do this now.
613//
614static void ResolveTypeTo(char *Name, const Type *ToTy) {
615 ValID D;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000616 if (Name) D = ValID::createLocalName(Name);
617 else D = ValID::createLocalID(CurModule.Types.size());
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000618
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000619 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000620 CurModule.LateResolveTypes.find(D);
621 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000622 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000623 CurModule.LateResolveTypes.erase(I);
624 }
625}
626
627// setValueName - Set the specified value to the name given. The name may be
628// null potentially, in which case this is a noop. The string passed in is
629// assumed to be a malloc'd string buffer, and is free'd by this function.
630//
631static void setValueName(Value *V, char *NameStr) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000632 if (!NameStr) return;
633 std::string Name(NameStr); // Copy string
634 free(NameStr); // Free old string
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000635
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000636 if (V->getType() == Type::VoidTy) {
637 GenerateError("Can't assign name '" + Name+"' to value with void type!");
638 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000639 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000640
641 assert(inFunctionScope() && "Must be in function scope!");
642 SymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
643 if (ST.lookup(V->getType(), Name)) {
644 GenerateError("Redefinition of value '" + Name + "' of type '" +
645 V->getType()->getDescription() + "'!");
646 return;
647 }
648
649 // Set the name.
650 V->setName(Name);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000651}
652
653/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
654/// this is a declaration, otherwise it is a definition.
655static GlobalVariable *
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000656ParseGlobalVariable(char *NameStr,
657 GlobalValue::LinkageTypes Linkage,
658 GlobalValue::VisibilityTypes Visibility,
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000659 bool isConstantGlobal, const Type *Ty,
660 Constant *Initializer) {
Reid Spencer309080a2006-09-28 19:28:24 +0000661 if (isa<FunctionType>(Ty)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000662 GenerateError("Cannot declare global vars of function type!");
Reid Spencer309080a2006-09-28 19:28:24 +0000663 return 0;
664 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000665
666 const PointerType *PTy = PointerType::get(Ty);
667
668 std::string Name;
669 if (NameStr) {
670 Name = NameStr; // Copy string
671 free(NameStr); // Free old string
672 }
673
674 // See if this global value was forward referenced. If so, recycle the
675 // object.
676 ValID ID;
677 if (!Name.empty()) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000678 ID = ValID::createGlobalName((char*)Name.c_str());
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000679 } else {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000680 ID = ValID::createGlobalID(CurModule.Values[PTy].size());
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000681 }
682
683 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
684 // Move the global to the end of the list, from whereever it was
685 // previously inserted.
686 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
687 CurModule.CurrentModule->getGlobalList().remove(GV);
688 CurModule.CurrentModule->getGlobalList().push_back(GV);
689 GV->setInitializer(Initializer);
690 GV->setLinkage(Linkage);
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000691 GV->setVisibility(Visibility);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000692 GV->setConstant(isConstantGlobal);
693 InsertValue(GV, CurModule.Values);
694 return GV;
695 }
696
697 // If this global has a name, check to see if there is already a definition
Reid Spencer33259082007-01-05 21:51:07 +0000698 // of this global in the module. If so, it is an error.
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000699 if (!Name.empty()) {
700 // We are a simple redefinition of a value, check to see if it is defined
701 // the same as the old one.
Reid Spencer33259082007-01-05 21:51:07 +0000702 if (CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000703 GenerateError("Redefinition of global variable named '" + Name +
Reid Spencer33259082007-01-05 21:51:07 +0000704 "' of type '" + Ty->getDescription() + "'!");
Reid Spencer309080a2006-09-28 19:28:24 +0000705 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000706 }
707 }
708
709 // Otherwise there is no existing GV to use, create one now.
710 GlobalVariable *GV =
711 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
712 CurModule.CurrentModule);
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000713 GV->setVisibility(Visibility);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000714 InsertValue(GV, CurModule.Values);
715 return GV;
716}
717
718// setTypeName - Set the specified type to the name given. The name may be
719// null potentially, in which case this is a noop. The string passed in is
720// assumed to be a malloc'd string buffer, and is freed by this function.
721//
722// This function returns true if the type has already been defined, but is
723// allowed to be redefined in the specified context. If the name is a new name
724// for the type plane, it is inserted and false is returned.
725static bool setTypeName(const Type *T, char *NameStr) {
726 assert(!inFunctionScope() && "Can't give types function-local names!");
727 if (NameStr == 0) return false;
728
729 std::string Name(NameStr); // Copy string
730 free(NameStr); // Free old string
731
732 // We don't allow assigning names to void type
Reid Spencer309080a2006-09-28 19:28:24 +0000733 if (T == Type::VoidTy) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000734 GenerateError("Can't assign name '" + Name + "' to the void type!");
Reid Spencer309080a2006-09-28 19:28:24 +0000735 return false;
736 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000737
738 // Set the type name, checking for conflicts as we do so.
739 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
740
741 if (AlreadyExists) { // Inserting a name that is already defined???
742 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
743 assert(Existing && "Conflict but no matching type?");
744
745 // There is only one case where this is allowed: when we are refining an
746 // opaque type. In this case, Existing will be an opaque type.
747 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
748 // We ARE replacing an opaque type!
749 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
750 return true;
751 }
752
753 // Otherwise, this is an attempt to redefine a type. That's okay if
754 // the redefinition is identical to the original. This will be so if
755 // Existing and T point to the same Type object. In this one case we
756 // allow the equivalent redefinition.
757 if (Existing == T) return true; // Yes, it's equal.
758
759 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer33259082007-01-05 21:51:07 +0000760 GenerateError("Redefinition of type named '" + Name + "' of type '" +
761 T->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000762 }
763
764 return false;
765}
766
767//===----------------------------------------------------------------------===//
768// Code for handling upreferences in type names...
769//
770
771// TypeContains - Returns true if Ty directly contains E in it.
772//
773static bool TypeContains(const Type *Ty, const Type *E) {
774 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
775 E) != Ty->subtype_end();
776}
777
778namespace {
779 struct UpRefRecord {
780 // NestingLevel - The number of nesting levels that need to be popped before
781 // this type is resolved.
782 unsigned NestingLevel;
783
784 // LastContainedTy - This is the type at the current binding level for the
785 // type. Every time we reduce the nesting level, this gets updated.
786 const Type *LastContainedTy;
787
788 // UpRefTy - This is the actual opaque type that the upreference is
789 // represented with.
790 OpaqueType *UpRefTy;
791
792 UpRefRecord(unsigned NL, OpaqueType *URTy)
793 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
794 };
795}
796
797// UpRefs - A list of the outstanding upreferences that need to be resolved.
798static std::vector<UpRefRecord> UpRefs;
799
800/// HandleUpRefs - Every time we finish a new layer of types, this function is
801/// called. It loops through the UpRefs vector, which is a list of the
802/// currently active types. For each type, if the up reference is contained in
803/// the newly completed type, we decrement the level count. When the level
804/// count reaches zero, the upreferenced type is the type that is passed in:
805/// thus we can complete the cycle.
806///
807static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner680aab62006-08-18 17:34:45 +0000808 // If Ty isn't abstract, or if there are no up-references in it, then there is
809 // nothing to resolve here.
810 if (!ty->isAbstract() || UpRefs.empty()) return ty;
811
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000812 PATypeHolder Ty(ty);
813 UR_OUT("Type '" << Ty->getDescription() <<
814 "' newly formed. Resolving upreferences.\n" <<
815 UpRefs.size() << " upreferences active!\n");
816
817 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
818 // to zero), we resolve them all together before we resolve them to Ty. At
819 // the end of the loop, if there is anything to resolve to Ty, it will be in
820 // this variable.
821 OpaqueType *TypeToResolve = 0;
822
823 for (unsigned i = 0; i != UpRefs.size(); ++i) {
824 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
825 << UpRefs[i].second->getDescription() << ") = "
826 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
827 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
828 // Decrement level of upreference
829 unsigned Level = --UpRefs[i].NestingLevel;
830 UpRefs[i].LastContainedTy = Ty;
831 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
832 if (Level == 0) { // Upreference should be resolved!
833 if (!TypeToResolve) {
834 TypeToResolve = UpRefs[i].UpRefTy;
835 } else {
836 UR_OUT(" * Resolving upreference for "
837 << UpRefs[i].second->getDescription() << "\n";
838 std::string OldName = UpRefs[i].UpRefTy->getDescription());
839 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
840 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
841 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
842 }
843 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
844 --i; // Do not skip the next element...
845 }
846 }
847 }
848
849 if (TypeToResolve) {
850 UR_OUT(" * Resolving upreference for "
851 << UpRefs[i].second->getDescription() << "\n";
852 std::string OldName = TypeToResolve->getDescription());
853 TypeToResolve->refineAbstractTypeTo(Ty);
854 }
855
856 return Ty;
857}
858
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000859//===----------------------------------------------------------------------===//
860// RunVMAsmParser - Define an interface to this parser
861//===----------------------------------------------------------------------===//
862//
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000863static Module* RunParser(Module * M);
864
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000865Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
866 set_scan_file(F);
867
868 CurFilename = Filename;
869 return RunParser(new Module(CurFilename));
870}
871
872Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
873 set_scan_string(AsmString);
874
875 CurFilename = "from_memory";
876 if (M == NULL) {
877 return RunParser(new Module (CurFilename));
878 } else {
879 return RunParser(M);
880 }
881}
882
883%}
884
885%union {
886 llvm::Module *ModuleVal;
887 llvm::Function *FunctionVal;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000888 llvm::BasicBlock *BasicBlockVal;
889 llvm::TerminatorInst *TermInstVal;
890 llvm::Instruction *InstVal;
Reid Spencere2c32da2006-12-03 05:46:11 +0000891 llvm::Constant *ConstVal;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000892
Reid Spencere2c32da2006-12-03 05:46:11 +0000893 const llvm::Type *PrimType;
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000894 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencere2c32da2006-12-03 05:46:11 +0000895 llvm::PATypeHolder *TypeVal;
896 llvm::Value *ValueVal;
Reid Spencere2c32da2006-12-03 05:46:11 +0000897 std::vector<llvm::Value*> *ValueList;
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000898 llvm::ArgListType *ArgList;
899 llvm::TypeWithAttrs TypeWithAttrs;
900 llvm::TypeWithAttrsList *TypeWithAttrsList;
901 llvm::ValueRefList *ValueRefList;
902
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000903 // Represent the RHS of PHI node
Reid Spencere2c32da2006-12-03 05:46:11 +0000904 std::list<std::pair<llvm::Value*,
905 llvm::BasicBlock*> > *PHIList;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000906 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencere2c32da2006-12-03 05:46:11 +0000907 std::vector<llvm::Constant*> *ConstVector;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000908
909 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000910 llvm::GlobalValue::VisibilityTypes Visibility;
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000911 llvm::FunctionType::ParameterAttributes ParamAttrs;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000912 int64_t SInt64Val;
913 uint64_t UInt64Val;
914 int SIntVal;
915 unsigned UIntVal;
916 double FPVal;
917 bool BoolVal;
918
919 char *StrVal; // This memory is strdup'd!
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000920 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000921
Reid Spencere2c32da2006-12-03 05:46:11 +0000922 llvm::Instruction::BinaryOps BinaryOpVal;
923 llvm::Instruction::TermOps TermOpVal;
924 llvm::Instruction::MemoryOps MemOpVal;
925 llvm::Instruction::CastOps CastOpVal;
926 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencere2c32da2006-12-03 05:46:11 +0000927 llvm::ICmpInst::Predicate IPredicate;
928 llvm::FCmpInst::Predicate FPredicate;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000929}
930
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000931%type <ModuleVal> Module
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000932%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
933%type <BasicBlockVal> BasicBlock InstructionList
934%type <TermInstVal> BBTerminatorInst
935%type <InstVal> Inst InstVal MemoryInst
936%type <ConstVal> ConstVal ConstExpr
937%type <ConstVector> ConstVector
938%type <ArgList> ArgList ArgListH
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000939%type <PHIList> PHIList
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000940%type <ValueRefList> ValueRefList // For call param lists & GEP indices
941%type <ValueList> IndexList // For GEP indices
942%type <TypeList> TypeListI
943%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencerbf48e3c2007-01-05 17:07:23 +0000944%type <TypeWithAttrs> ArgType
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000945%type <JumpTable> JumpTable
946%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
947%type <BoolVal> OptVolatile // 'volatile' or not
948%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
949%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000950%type <Linkage> GVInternalLinkage GVExternalLinkage
951%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000952%type <Visibility> GVVisibilityStyle
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000953
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
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000965%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000966%token <FPVal> FPVAL // Float or Double constant
967
968// Built in types...
Reid Spencerbf48e3c2007-01-05 17:07:23 +0000969%type <TypeVal> Types ResultTypes
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000970%type <PrimType> IntType FPType PrimType // Classifications
Reid Spencer58a8db02007-01-13 05:00:46 +0000971%token <PrimType> VOID INTTYPE
Reid Spencerdc0a3a22006-12-29 20:35:03 +0000972%token <PrimType> FLOAT DOUBLE LABEL
973%token TYPE
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000974
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000975%token<StrVal> LOCALVAR GLOBALVAR LABELSTR STRINGCONSTANT ATSTRINGCONSTANT
976%type <StrVal> LocalName OptLocalName OptLocalAssign
977%type <StrVal> GlobalName OptGlobalAssign
978%type <UIntVal> OptAlign OptCAlign
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000979%type <StrVal> OptSection SectionString
980
981%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Reid Spencerdc0a3a22006-12-29 20:35:03 +0000982%token DECLARE DEFINE GLOBAL CONSTANT SECTION VOLATILE
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000983%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000984%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000985%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000986%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000987%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattner7d1d0342006-10-22 06:08:13 +0000988%token DATALAYOUT
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000989%type <UIntVal> OptCallingConv
Reid Spencerbf48e3c2007-01-05 17:07:23 +0000990%type <ParamAttrs> OptParamAttrs ParamAttr
991%type <ParamAttrs> OptFuncAttrs FuncAttr
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000992
993// Basic Block Terminating Operators
994%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
995
996// Binary Operators
Reid Spencer266e42b2006-12-23 06:05:41 +0000997%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencerde46e482006-11-02 20:25:50 +0000998%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencere2c32da2006-12-03 05:46:11 +0000999%token <OtherOpVal> ICMP FCMP
Reid Spencere2c32da2006-12-03 05:46:11 +00001000%type <IPredicate> IPredicates
Reid Spencere2c32da2006-12-03 05:46:11 +00001001%type <FPredicate> FPredicates
Reid Spencer1960ef32006-12-03 06:59:29 +00001002%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1003%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001004
1005// Memory Instructions
1006%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1007
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001008// Cast Operators
1009%type <CastOpVal> CastOps
1010%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1011%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1012
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001013// Other Operators
1014%type <OtherOpVal> ShiftOps
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001015%token <OtherOpVal> PHI_TOK SELECT SHL LSHR ASHR VAARG
Chris Lattner9ff96a72006-04-08 01:18:56 +00001016%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001017
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001018// Function Attributes
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001019%token NORETURN INREG SRET
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001020
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001021// Visibility Styles
1022%token DEFAULT HIDDEN
1023
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001024%start Module
1025%%
1026
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001027
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001028// Operations that are notably excluded from this list include:
1029// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1030//
Reid Spencerde46e482006-11-02 20:25:50 +00001031ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001032LogicalOps : AND | OR | XOR;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001033CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1034 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
1035ShiftOps : SHL | LSHR | ASHR;
Reid Spencer1960ef32006-12-03 06:59:29 +00001036IPredicates
Reid Spencerd2e0c342006-12-04 05:24:24 +00001037 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer1960ef32006-12-03 06:59:29 +00001038 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1039 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1040 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1041 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1042 ;
1043
1044FPredicates
1045 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1046 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1047 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1048 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1049 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1050 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1051 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1052 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1053 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1054 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001055
1056// These are some types that allow classification if we only want a particular
1057// thing... for example, only a signed, unsigned, or integral type.
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001058IntType : INTTYPE;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001059FPType : FLOAT | DOUBLE;
1060
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001061LocalName : LOCALVAR | STRINGCONSTANT;
1062OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1063
1064/// OptLocalAssign - Value producing statements have an optional assignment
1065/// component.
1066OptLocalAssign : LocalName '=' {
1067 $$ = $1;
1068 CHECK_FOR_ERROR
1069 }
1070 | /*empty*/ {
1071 $$ = 0;
1072 CHECK_FOR_ERROR
1073 };
1074
1075GlobalName : GLOBALVAR | ATSTRINGCONSTANT;
1076
1077OptGlobalAssign : GlobalName '=' {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001078 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001079 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001080 }
1081 | /*empty*/ {
1082 $$ = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00001083 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001084 };
1085
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001086GVInternalLinkage
1087 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1088 | WEAK { $$ = GlobalValue::WeakLinkage; }
1089 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1090 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1091 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1092 ;
1093
1094GVExternalLinkage
1095 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1096 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1097 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1098 ;
1099
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001100GVVisibilityStyle
1101 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1102 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1103 ;
1104
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001105FunctionDeclareLinkage
1106 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1107 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1108 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001109 ;
1110
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001111FunctionDefineLinkage
1112 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1113 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001114 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1115 | WEAK { $$ = GlobalValue::WeakLinkage; }
1116 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001117 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001118
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +00001119OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1120 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +00001121 FASTCC_TOK { $$ = CallingConv::Fast; } |
1122 COLDCC_TOK { $$ = CallingConv::Cold; } |
1123 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1124 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1125 CC_TOK EUINT64VAL {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001126 if ((unsigned)$2 != $2)
Reid Spencer713eedc2006-08-18 08:43:06 +00001127 GEN_ERROR("Calling conv too large!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001128 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001129 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001130 };
1131
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001132ParamAttr : ZEXT { $$ = FunctionType::ZExtAttribute; }
1133 | SEXT { $$ = FunctionType::SExtAttribute; }
1134 | INREG { $$ = FunctionType::InRegAttribute; }
1135 | SRET { $$ = FunctionType::StructRetAttribute; }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001136 ;
1137
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001138OptParamAttrs : /* empty */ { $$ = FunctionType::NoAttributeSet; }
1139 | OptParamAttrs ParamAttr {
1140 $$ = FunctionType::ParameterAttributes($1 | $2);
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001141 }
1142 ;
1143
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001144FuncAttr : NORETURN { $$ = FunctionType::NoReturnAttribute; }
1145 | ParamAttr
1146 ;
1147
1148OptFuncAttrs : /* empty */ { $$ = FunctionType::NoAttributeSet; }
1149 | OptFuncAttrs FuncAttr {
1150 $$ = FunctionType::ParameterAttributes($1 | $2);
1151 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001152 ;
1153
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001154// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1155// a comma before it.
1156OptAlign : /*empty*/ { $$ = 0; } |
1157 ALIGN EUINT64VAL {
1158 $$ = $2;
1159 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencer713eedc2006-08-18 08:43:06 +00001160 GEN_ERROR("Alignment must be a power of two!");
1161 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001162};
1163OptCAlign : /*empty*/ { $$ = 0; } |
1164 ',' ALIGN EUINT64VAL {
1165 $$ = $3;
1166 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencer713eedc2006-08-18 08:43:06 +00001167 GEN_ERROR("Alignment must be a power of two!");
1168 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001169};
1170
1171
1172SectionString : SECTION STRINGCONSTANT {
1173 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1174 if ($2[i] == '"' || $2[i] == '\\')
Reid Spencer713eedc2006-08-18 08:43:06 +00001175 GEN_ERROR("Invalid character in section name!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001176 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001177 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001178};
1179
1180OptSection : /*empty*/ { $$ = 0; } |
1181 SectionString { $$ = $1; };
1182
1183// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1184// is set to be the global we are processing.
1185//
1186GlobalVarAttributes : /* empty */ {} |
1187 ',' GlobalVarAttribute GlobalVarAttributes {};
1188GlobalVarAttribute : SectionString {
1189 CurGV->setSection($1);
1190 free($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001191 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001192 }
1193 | ALIGN EUINT64VAL {
1194 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencer713eedc2006-08-18 08:43:06 +00001195 GEN_ERROR("Alignment must be a power of two!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001196 CurGV->setAlignment($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001197 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001198 };
1199
1200//===----------------------------------------------------------------------===//
1201// Types includes all predefined types... except void, because it can only be
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001202// used in specific contexts (function returning void for example).
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001203
1204// Derived types are added later...
1205//
Reid Spencer58a8db02007-01-13 05:00:46 +00001206PrimType : INTTYPE | FLOAT | DOUBLE | LABEL ;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001207
1208Types
1209 : OPAQUE {
Reid Spencere2c32da2006-12-03 05:46:11 +00001210 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer713eedc2006-08-18 08:43:06 +00001211 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001212 }
1213 | PrimType {
Reid Spencere2c32da2006-12-03 05:46:11 +00001214 $$ = new PATypeHolder($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001215 CHECK_FOR_ERROR
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001216 }
1217 | Types '*' { // Pointer type?
1218 if (*$1 == Type::LabelTy)
1219 GEN_ERROR("Cannot form a pointer to a basic block");
1220 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1221 delete $1;
1222 CHECK_FOR_ERROR
1223 }
1224 | SymbolicValueRef { // Named types are also simple types...
1225 const Type* tmp = getTypeVal($1);
1226 CHECK_FOR_ERROR
1227 $$ = new PATypeHolder(tmp);
1228 }
1229 | '\\' EUINT64VAL { // Type UpReference
Reid Spencer713eedc2006-08-18 08:43:06 +00001230 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001231 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1232 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencere2c32da2006-12-03 05:46:11 +00001233 $$ = new PATypeHolder(OT);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001234 UR_OUT("New Upreference!\n");
Reid Spencer713eedc2006-08-18 08:43:06 +00001235 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001236 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001237 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001238 std::vector<const Type*> Params;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001239 std::vector<FunctionType::ParameterAttributes> Attrs;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001240 Attrs.push_back($5);
1241 for (TypeWithAttrsList::iterator I=$3->begin(), E=$3->end(); I != E; ++I) {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001242 Params.push_back(I->Ty->get());
1243 if (I->Ty->get() != Type::VoidTy)
1244 Attrs.push_back(I->Attrs);
1245 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001246 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1247 if (isVarArg) Params.pop_back();
1248
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001249 FunctionType *FT = FunctionType::get(*$1, Params, isVarArg, Attrs);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001250 delete $3; // Delete the argument list
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001251 delete $1; // Delete the return type handle
1252 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer713eedc2006-08-18 08:43:06 +00001253 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001254 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001255 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001256 std::vector<const Type*> Params;
1257 std::vector<FunctionType::ParameterAttributes> Attrs;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001258 Attrs.push_back($5);
1259 for (TypeWithAttrsList::iterator I=$3->begin(), E=$3->end(); I != E; ++I) {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001260 Params.push_back(I->Ty->get());
1261 if (I->Ty->get() != Type::VoidTy)
1262 Attrs.push_back(I->Attrs);
1263 }
1264 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1265 if (isVarArg) Params.pop_back();
1266
1267 FunctionType *FT = FunctionType::get($1, Params, isVarArg, Attrs);
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001268 delete $3; // Delete the argument list
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001269 $$ = new PATypeHolder(HandleUpRefs(FT));
1270 CHECK_FOR_ERROR
1271 }
1272
1273 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Reid Spencere2c32da2006-12-03 05:46:11 +00001274 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1275 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00001276 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001277 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001278 | '<' EUINT64VAL 'x' Types '>' { // Packed array type?
Reid Spencere2c32da2006-12-03 05:46:11 +00001279 const llvm::Type* ElemTy = $4->get();
1280 if ((unsigned)$2 != $2)
1281 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner03c49532007-01-15 02:27:26 +00001282 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001283 GEN_ERROR("Element type of a PackedType must be primitive");
Reid Spencere2c32da2006-12-03 05:46:11 +00001284 if (!isPowerOf2_32($2))
1285 GEN_ERROR("Vector length should be a power of 2!");
1286 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
1287 delete $4;
1288 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001289 }
1290 | '{' TypeListI '}' { // Structure type?
1291 std::vector<const Type*> Elements;
Reid Spencere2c32da2006-12-03 05:46:11 +00001292 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001293 E = $2->end(); I != E; ++I)
Reid Spencere2c32da2006-12-03 05:46:11 +00001294 Elements.push_back(*I);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001295
Reid Spencere2c32da2006-12-03 05:46:11 +00001296 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001297 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001298 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001299 }
1300 | '{' '}' { // Empty structure type?
Reid Spencere2c32da2006-12-03 05:46:11 +00001301 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer713eedc2006-08-18 08:43:06 +00001302 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001303 }
Andrew Lenharth2d189ff2006-12-08 18:07:09 +00001304 | '<' '{' TypeListI '}' '>' {
1305 std::vector<const Type*> Elements;
1306 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1307 E = $3->end(); I != E; ++I)
1308 Elements.push_back(*I);
1309
1310 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1311 delete $3;
1312 CHECK_FOR_ERROR
1313 }
1314 | '<' '{' '}' '>' { // Empty structure type?
1315 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1316 CHECK_FOR_ERROR
1317 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001318 ;
1319
1320ArgType
1321 : Types OptParamAttrs {
1322 $$.Ty = $1;
1323 $$.Attrs = $2;
1324 }
1325 ;
1326
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001327ResultTypes
1328 : Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001329 if (!UpRefs.empty())
1330 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1331 if (!(*$1)->isFirstClassType())
1332 GEN_ERROR("LLVM functions cannot return aggregate types!");
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001333 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001334 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001335 | VOID {
1336 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001337 }
1338 ;
1339
1340ArgTypeList : ArgType {
1341 $$ = new TypeWithAttrsList();
1342 $$->push_back($1);
1343 CHECK_FOR_ERROR
1344 }
1345 | ArgTypeList ',' ArgType {
1346 ($$=$1)->push_back($3);
1347 CHECK_FOR_ERROR
1348 }
1349 ;
1350
1351ArgTypeListI
1352 : ArgTypeList
1353 | ArgTypeList ',' DOTDOTDOT {
1354 $$=$1;
1355 TypeWithAttrs TWA; TWA.Attrs = FunctionType::NoAttributeSet;
1356 TWA.Ty = new PATypeHolder(Type::VoidTy);
1357 $$->push_back(TWA);
1358 CHECK_FOR_ERROR
1359 }
1360 | DOTDOTDOT {
1361 $$ = new TypeWithAttrsList;
1362 TypeWithAttrs TWA; TWA.Attrs = FunctionType::NoAttributeSet;
1363 TWA.Ty = new PATypeHolder(Type::VoidTy);
1364 $$->push_back(TWA);
1365 CHECK_FOR_ERROR
1366 }
1367 | /*empty*/ {
1368 $$ = new TypeWithAttrsList();
Reid Spencer713eedc2006-08-18 08:43:06 +00001369 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001370 };
1371
1372// TypeList - Used for struct declarations and as a basis for function type
1373// declaration type lists
1374//
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001375TypeListI : Types {
Reid Spencere2c32da2006-12-03 05:46:11 +00001376 $$ = new std::list<PATypeHolder>();
1377 $$->push_back(*$1); delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001378 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001379 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001380 | TypeListI ',' Types {
Reid Spencere2c32da2006-12-03 05:46:11 +00001381 ($$=$1)->push_back(*$3); delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001382 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001383 };
1384
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001385// ConstVal - The various declarations that go into the constant pool. This
1386// production is used ONLY to represent constants that show up AFTER a 'const',
1387// 'constant' or 'global' token at global scope. Constants that can be inlined
1388// into other expressions (such as integers and constexprs) are handled by the
1389// ResolvedVal, ValueRef and ConstValueRef productions.
1390//
1391ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001392 if (!UpRefs.empty())
1393 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001394 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001395 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001396 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00001397 (*$1)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001398 const Type *ETy = ATy->getElementType();
1399 int NumElements = ATy->getNumElements();
1400
1401 // Verify that we have the correct size...
1402 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer713eedc2006-08-18 08:43:06 +00001403 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001404 utostr($3->size()) + " arguments, but has size of " +
1405 itostr(NumElements) + "!");
1406
1407 // Verify all elements are correct type!
1408 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencere2c32da2006-12-03 05:46:11 +00001409 if (ETy != (*$3)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001410 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001411 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencere2c32da2006-12-03 05:46:11 +00001412 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001413 }
1414
Reid Spencere2c32da2006-12-03 05:46:11 +00001415 $$ = ConstantArray::get(ATy, *$3);
1416 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001417 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001418 }
1419 | Types '[' ']' {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001420 if (!UpRefs.empty())
1421 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001422 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001423 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001424 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00001425 (*$1)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001426
1427 int NumElements = ATy->getNumElements();
1428 if (NumElements != -1 && NumElements != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001429 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001430 " arguments, but has size of " + itostr(NumElements) +"!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001431 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1432 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001433 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001434 }
1435 | Types 'c' STRINGCONSTANT {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001436 if (!UpRefs.empty())
1437 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001438 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001439 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001440 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00001441 (*$1)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001442
1443 int NumElements = ATy->getNumElements();
1444 const Type *ETy = ATy->getElementType();
1445 char *EndStr = UnEscapeLexed($3, true);
1446 if (NumElements != -1 && NumElements != (EndStr-$3))
Reid Spencer713eedc2006-08-18 08:43:06 +00001447 GEN_ERROR("Can't build string constant of size " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001448 itostr((int)(EndStr-$3)) +
1449 " when array has size " + itostr(NumElements) + "!");
1450 std::vector<Constant*> Vals;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001451 if (ETy == Type::Int8Ty) {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001452 for (unsigned char *C = (unsigned char *)$3;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001453 C != (unsigned char*)EndStr; ++C)
1454 Vals.push_back(ConstantInt::get(ETy, *C));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001455 } else {
1456 free($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001457 GEN_ERROR("Cannot build string arrays of non byte sized elements!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001458 }
1459 free($3);
Reid Spencere2c32da2006-12-03 05:46:11 +00001460 $$ = ConstantArray::get(ATy, Vals);
1461 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001462 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001463 }
1464 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001465 if (!UpRefs.empty())
1466 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001467 const PackedType *PTy = dyn_cast<PackedType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001468 if (PTy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001469 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00001470 (*$1)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001471 const Type *ETy = PTy->getElementType();
1472 int NumElements = PTy->getNumElements();
1473
1474 // Verify that we have the correct size...
1475 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer713eedc2006-08-18 08:43:06 +00001476 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001477 utostr($3->size()) + " arguments, but has size of " +
1478 itostr(NumElements) + "!");
1479
1480 // Verify all elements are correct type!
1481 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencere2c32da2006-12-03 05:46:11 +00001482 if (ETy != (*$3)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001483 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001484 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencere2c32da2006-12-03 05:46:11 +00001485 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001486 }
1487
Reid Spencere2c32da2006-12-03 05:46:11 +00001488 $$ = ConstantPacked::get(PTy, *$3);
1489 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001490 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001491 }
1492 | Types '{' ConstVector '}' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001493 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001494 if (STy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001495 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00001496 (*$1)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001497
1498 if ($3->size() != STy->getNumContainedTypes())
Reid Spencer713eedc2006-08-18 08:43:06 +00001499 GEN_ERROR("Illegal number of initializers for structure type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001500
1501 // Check to ensure that constants are compatible with the type initializer!
1502 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencere2c32da2006-12-03 05:46:11 +00001503 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer713eedc2006-08-18 08:43:06 +00001504 GEN_ERROR("Expected type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001505 STy->getElementType(i)->getDescription() +
1506 "' for element #" + utostr(i) +
1507 " of structure initializer!");
1508
Zhou Sheng75b871f2007-01-11 12:24:14 +00001509 // Check to ensure that Type is not packed
1510 if (STy->isPacked())
1511 GEN_ERROR("Unpacked Initializer to packed type '" + STy->getDescription() + "'");
1512
Reid Spencere2c32da2006-12-03 05:46:11 +00001513 $$ = ConstantStruct::get(STy, *$3);
1514 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001515 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001516 }
1517 | Types '{' '}' {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001518 if (!UpRefs.empty())
1519 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001520 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001521 if (STy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001522 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00001523 (*$1)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001524
1525 if (STy->getNumContainedTypes() != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001526 GEN_ERROR("Illegal number of initializers for structure type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001527
Zhou Sheng75b871f2007-01-11 12:24:14 +00001528 // Check to ensure that Type is not packed
1529 if (STy->isPacked())
1530 GEN_ERROR("Unpacked Initializer to packed type '" + STy->getDescription() + "'");
1531
1532 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1533 delete $1;
1534 CHECK_FOR_ERROR
1535 }
1536 | Types '<' '{' ConstVector '}' '>' {
1537 const StructType *STy = dyn_cast<StructType>($1->get());
1538 if (STy == 0)
1539 GEN_ERROR("Cannot make struct constant with type: '" +
1540 (*$1)->getDescription() + "'!");
1541
1542 if ($4->size() != STy->getNumContainedTypes())
1543 GEN_ERROR("Illegal number of initializers for structure type!");
1544
1545 // Check to ensure that constants are compatible with the type initializer!
1546 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1547 if ((*$4)[i]->getType() != STy->getElementType(i))
1548 GEN_ERROR("Expected type '" +
1549 STy->getElementType(i)->getDescription() +
1550 "' for element #" + utostr(i) +
1551 " of structure initializer!");
1552
1553 // Check to ensure that Type is packed
1554 if (!STy->isPacked())
1555 GEN_ERROR("Packed Initializer to unpacked type '" + STy->getDescription() + "'");
1556
1557 $$ = ConstantStruct::get(STy, *$4);
1558 delete $1; delete $4;
1559 CHECK_FOR_ERROR
1560 }
1561 | Types '<' '{' '}' '>' {
1562 if (!UpRefs.empty())
1563 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1564 const StructType *STy = dyn_cast<StructType>($1->get());
1565 if (STy == 0)
1566 GEN_ERROR("Cannot make struct constant with type: '" +
1567 (*$1)->getDescription() + "'!");
1568
1569 if (STy->getNumContainedTypes() != 0)
1570 GEN_ERROR("Illegal number of initializers for structure type!");
1571
1572 // Check to ensure that Type is packed
1573 if (!STy->isPacked())
1574 GEN_ERROR("Packed Initializer to unpacked type '" + STy->getDescription() + "'");
1575
Reid Spencere2c32da2006-12-03 05:46:11 +00001576 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1577 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001578 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001579 }
1580 | Types NULL_TOK {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001581 if (!UpRefs.empty())
1582 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001583 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001584 if (PTy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001585 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00001586 (*$1)->getDescription() + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001587
Reid Spencere2c32da2006-12-03 05:46:11 +00001588 $$ = ConstantPointerNull::get(PTy);
1589 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001590 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001591 }
1592 | Types UNDEF {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001593 if (!UpRefs.empty())
1594 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001595 $$ = UndefValue::get($1->get());
1596 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001597 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001598 }
1599 | Types SymbolicValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001600 if (!UpRefs.empty())
1601 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001602 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001603 if (Ty == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001604 GEN_ERROR("Global const reference must be a pointer type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001605
1606 // ConstExprs can exist in the body of a function, thus creating
1607 // GlobalValues whenever they refer to a variable. Because we are in
1608 // the context of a function, getValNonImprovising will search the functions
1609 // symbol table instead of the module symbol table for the global symbol,
1610 // which throws things all off. To get around this, we just tell
1611 // getValNonImprovising that we are at global scope here.
1612 //
1613 Function *SavedCurFn = CurFun.CurrentFunction;
1614 CurFun.CurrentFunction = 0;
1615
1616 Value *V = getValNonImprovising(Ty, $2);
Reid Spencer309080a2006-09-28 19:28:24 +00001617 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001618
1619 CurFun.CurrentFunction = SavedCurFn;
1620
1621 // If this is an initializer for a constant pointer, which is referencing a
1622 // (currently) undefined variable, create a stub now that shall be replaced
1623 // in the future with the right type of variable.
1624 //
1625 if (V == 0) {
1626 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1627 const PointerType *PT = cast<PointerType>(Ty);
1628
1629 // First check to see if the forward references value is already created!
1630 PerModuleInfo::GlobalRefsType::iterator I =
1631 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1632
1633 if (I != CurModule.GlobalRefs.end()) {
1634 V = I->second; // Placeholder already exists, use it...
1635 $2.destroy();
1636 } else {
1637 std::string Name;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001638 if ($2.Type == ValID::GlobalName)
1639 Name = $2.Name;
1640 else if ($2.Type != ValID::GlobalID)
1641 GEN_ERROR("Invalid reference to global");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001642
1643 // Create the forward referenced global.
1644 GlobalValue *GV;
1645 if (const FunctionType *FTy =
1646 dyn_cast<FunctionType>(PT->getElementType())) {
1647 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1648 CurModule.CurrentModule);
1649 } else {
1650 GV = new GlobalVariable(PT->getElementType(), false,
1651 GlobalValue::ExternalLinkage, 0,
1652 Name, CurModule.CurrentModule);
1653 }
1654
1655 // Keep track of the fact that we have a forward ref to recycle it
1656 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1657 V = GV;
1658 }
1659 }
1660
Reid Spencere2c32da2006-12-03 05:46:11 +00001661 $$ = cast<GlobalValue>(V);
1662 delete $1; // Free the type handle
Reid Spencer713eedc2006-08-18 08:43:06 +00001663 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001664 }
1665 | Types ConstExpr {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001666 if (!UpRefs.empty())
1667 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001668 if ($1->get() != $2->getType())
Reid Spencerca3d1cb2007-01-04 00:06:14 +00001669 GEN_ERROR("Mismatched types for constant expression: " +
1670 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001671 $$ = $2;
Reid Spencere2c32da2006-12-03 05:46:11 +00001672 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001673 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001674 }
1675 | Types ZEROINITIALIZER {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001676 if (!UpRefs.empty())
1677 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001678 const Type *Ty = $1->get();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001679 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencer713eedc2006-08-18 08:43:06 +00001680 GEN_ERROR("Cannot create a null initialized value of this type!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001681 $$ = Constant::getNullValue(Ty);
1682 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001683 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00001684 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001685 | IntType ESINT64VAL { // integral constants
Reid Spencer266e42b2006-12-23 06:05:41 +00001686 if (!ConstantInt::isValueValidForType($1, $2))
1687 GEN_ERROR("Constant value doesn't fit in type!");
1688 $$ = ConstantInt::get($1, $2);
1689 CHECK_FOR_ERROR
1690 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001691 | IntType EUINT64VAL { // integral constants
Reid Spencer266e42b2006-12-23 06:05:41 +00001692 if (!ConstantInt::isValueValidForType($1, $2))
1693 GEN_ERROR("Constant value doesn't fit in type!");
1694 $$ = ConstantInt::get($1, $2);
1695 CHECK_FOR_ERROR
1696 }
Reid Spencer58a8db02007-01-13 05:00:46 +00001697 | INTTYPE TRUETOK { // Boolean constants
1698 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001699 $$ = ConstantInt::getTrue();
Reid Spencer713eedc2006-08-18 08:43:06 +00001700 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001701 }
Reid Spencer58a8db02007-01-13 05:00:46 +00001702 | INTTYPE FALSETOK { // Boolean constants
1703 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001704 $$ = ConstantInt::getFalse();
Reid Spencer713eedc2006-08-18 08:43:06 +00001705 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001706 }
1707 | FPType FPVAL { // Float & Double constants
Reid Spencere2c32da2006-12-03 05:46:11 +00001708 if (!ConstantFP::isValueValidForType($1, $2))
Reid Spencer713eedc2006-08-18 08:43:06 +00001709 GEN_ERROR("Floating point constant invalid for type!!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001710 $$ = ConstantFP::get($1, $2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001711 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001712 };
1713
1714
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001715ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001716 if (!UpRefs.empty())
1717 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001718 Constant *Val = $3;
Reid Spencerceb84592007-01-17 02:48:45 +00001719 const Type *DestTy = $5->get();
1720 if (!CastInst::castIsValid($1, $3, DestTy))
1721 GEN_ERROR("invalid cast opcode for cast from '" +
1722 Val->getType()->getDescription() + "' to '" +
1723 DestTy->getDescription() + "'!");
1724 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencere2c32da2006-12-03 05:46:11 +00001725 delete $5;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001726 }
1727 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001728 if (!isa<PointerType>($3->getType()))
Reid Spencer713eedc2006-08-18 08:43:06 +00001729 GEN_ERROR("GetElementPtr requires a pointer operand!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001730
Reid Spencere2c32da2006-12-03 05:46:11 +00001731 const Type *IdxTy =
1732 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
1733 if (!IdxTy)
1734 GEN_ERROR("Index list invalid for constant getelementptr!");
1735
1736 std::vector<Constant*> IdxVec;
1737 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1738 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001739 IdxVec.push_back(C);
1740 else
Reid Spencer713eedc2006-08-18 08:43:06 +00001741 GEN_ERROR("Indices to constant getelementptr must be constants!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001742
1743 delete $4;
1744
Reid Spencere2c32da2006-12-03 05:46:11 +00001745 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Reid Spencer713eedc2006-08-18 08:43:06 +00001746 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001747 }
1748 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer542964f2007-01-11 18:21:29 +00001749 if ($3->getType() != Type::Int1Ty)
Reid Spencer713eedc2006-08-18 08:43:06 +00001750 GEN_ERROR("Select condition must be of boolean type!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001751 if ($5->getType() != $7->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001752 GEN_ERROR("Select operand types must match!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001753 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001754 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001755 }
1756 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001757 if ($3->getType() != $5->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001758 GEN_ERROR("Binary operator types must match!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001759 CHECK_FOR_ERROR;
Reid Spencer844668d2006-12-05 19:16:11 +00001760 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001761 }
1762 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001763 if ($3->getType() != $5->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001764 GEN_ERROR("Logical operator types must match!");
Chris Lattner03c49532007-01-15 02:27:26 +00001765 if (!$3->getType()->isInteger()) {
Reid Spencere2c32da2006-12-03 05:46:11 +00001766 if (!isa<PackedType>($3->getType()) ||
Chris Lattner03c49532007-01-15 02:27:26 +00001767 !cast<PackedType>($3->getType())->getElementType()->isInteger())
Reid Spencer713eedc2006-08-18 08:43:06 +00001768 GEN_ERROR("Logical operator requires integral operands!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001769 }
Reid Spencere2c32da2006-12-03 05:46:11 +00001770 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001771 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001772 }
Reid Spencerd2e0c342006-12-04 05:24:24 +00001773 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1774 if ($4->getType() != $6->getType())
Reid Spencere2c32da2006-12-03 05:46:11 +00001775 GEN_ERROR("icmp operand types must match!");
Reid Spencerd2e0c342006-12-04 05:24:24 +00001776 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencere2c32da2006-12-03 05:46:11 +00001777 }
Reid Spencerd2e0c342006-12-04 05:24:24 +00001778 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1779 if ($4->getType() != $6->getType())
Reid Spencere2c32da2006-12-03 05:46:11 +00001780 GEN_ERROR("fcmp operand types must match!");
Reid Spencerd2e0c342006-12-04 05:24:24 +00001781 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencere2c32da2006-12-03 05:46:11 +00001782 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001783 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001784 if ($5->getType() != Type::Int8Ty)
1785 GEN_ERROR("Shift count for shift constant must be i8 type!");
Chris Lattner03c49532007-01-15 02:27:26 +00001786 if (!$3->getType()->isInteger())
Reid Spencer713eedc2006-08-18 08:43:06 +00001787 GEN_ERROR("Shift constant expression requires integer operand!");
Reid Spencerfdff9382006-11-08 06:47:33 +00001788 CHECK_FOR_ERROR;
Reid Spencere2c32da2006-12-03 05:46:11 +00001789 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001790 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001791 }
1792 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001793 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencer713eedc2006-08-18 08:43:06 +00001794 GEN_ERROR("Invalid extractelement operands!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001795 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001796 CHECK_FOR_ERROR
Chris Lattneraebccf82006-04-08 03:55:17 +00001797 }
1798 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001799 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencer713eedc2006-08-18 08:43:06 +00001800 GEN_ERROR("Invalid insertelement operands!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001801 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001802 CHECK_FOR_ERROR
Chris Lattneraebccf82006-04-08 03:55:17 +00001803 }
1804 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001805 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencer713eedc2006-08-18 08:43:06 +00001806 GEN_ERROR("Invalid shufflevector operands!");
Reid Spencere2c32da2006-12-03 05:46:11 +00001807 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001808 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001809 };
1810
Chris Lattneraebccf82006-04-08 03:55:17 +00001811
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001812// ConstVector - A list of comma separated constants.
1813ConstVector : ConstVector ',' ConstVal {
1814 ($$ = $1)->push_back($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001815 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001816 }
1817 | ConstVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00001818 $$ = new std::vector<Constant*>();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001819 $$->push_back($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001820 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001821 };
1822
1823
1824// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1825GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
1826
1827
1828//===----------------------------------------------------------------------===//
1829// Rules to match Modules
1830//===----------------------------------------------------------------------===//
1831
1832// Module rule: Capture the result of parsing the whole file into a result
1833// variable...
1834//
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001835Module
1836 : DefinitionList {
1837 $$ = ParserResult = CurModule.CurrentModule;
1838 CurModule.ModuleDone();
1839 CHECK_FOR_ERROR;
1840 }
1841 | /*empty*/ {
1842 $$ = ParserResult = CurModule.CurrentModule;
1843 CurModule.ModuleDone();
1844 CHECK_FOR_ERROR;
1845 }
1846 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001847
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001848DefinitionList
1849 : Definition
1850 | DefinitionList Definition
1851 ;
1852
1853Definition
Jeff Cohen5d956e42007-01-21 19:19:31 +00001854 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001855 CurFun.FunctionDone();
Reid Spencer713eedc2006-08-18 08:43:06 +00001856 CHECK_FOR_ERROR
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001857 }
1858 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer713eedc2006-08-18 08:43:06 +00001859 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001860 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001861 | MODULE ASM_TOK AsmBlock {
Reid Spencer713eedc2006-08-18 08:43:06 +00001862 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001863 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001864 | IMPLEMENTATION {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001865 // Emit an error if there are any unresolved types left.
1866 if (!CurModule.LateResolveTypes.empty()) {
1867 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001868 if (DID.Type == ValID::LocalName) {
Reid Spencer713eedc2006-08-18 08:43:06 +00001869 GEN_ERROR("Reference to an undefined type: '"+DID.getName() + "'");
1870 } else {
1871 GEN_ERROR("Reference to an undefined type: #" + itostr(DID.Num));
1872 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001873 }
Reid Spencer713eedc2006-08-18 08:43:06 +00001874 CHECK_FOR_ERROR
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001875 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001876 | OptLocalAssign TYPE Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001877 if (!UpRefs.empty())
1878 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001879 // Eagerly resolve types. This is not an optimization, this is a
1880 // requirement that is due to the fact that we could have this:
1881 //
1882 // %list = type { %list * }
1883 // %list = type { %list * } ; repeated type decl
1884 //
1885 // If types are not resolved eagerly, then the two types will not be
1886 // determined to be the same type!
1887 //
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001888 ResolveTypeTo($1, *$3);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001889
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001890 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer309080a2006-09-28 19:28:24 +00001891 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001892 // If this is a named type that is not a redefinition, add it to the slot
1893 // table.
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001894 CurModule.Types.push_back(*$3);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001895 }
Reid Spencere2c32da2006-12-03 05:46:11 +00001896
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001897 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001898 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001899 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001900 | OptLocalAssign TYPE VOID {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001901 ResolveTypeTo($1, $3);
1902
1903 if (!setTypeName($3, $1) && !$1) {
1904 CHECK_FOR_ERROR
1905 // If this is a named type that is not a redefinition, add it to the slot
1906 // table.
1907 CurModule.Types.push_back($3);
1908 }
1909 CHECK_FOR_ERROR
1910 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001911 | OptGlobalAssign GVVisibilityStyle GlobalType ConstVal {
1912 /* "Externally Visible" Linkage */
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001913 if ($4 == 0)
1914 GEN_ERROR("Global value initializer is not a constant!");
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001915 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
1916 $2, $3, $4->getType(), $4);
Reid Spencer309080a2006-09-28 19:28:24 +00001917 CHECK_FOR_ERROR
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001918 } GlobalVarAttributes {
1919 CurGV = 0;
1920 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001921 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle GlobalType ConstVal {
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001922 if ($5 == 0)
1923 GEN_ERROR("Global value initializer is not a constant!");
1924 CurGV = ParseGlobalVariable($1, $2, $3, $4, $5->getType(), $5);
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001925 CHECK_FOR_ERROR
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001926 } GlobalVarAttributes {
1927 CurGV = 0;
1928 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001929 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle GlobalType Types {
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001930 if (!UpRefs.empty())
1931 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
1932 CurGV = ParseGlobalVariable($1, $2, $3, $4, *$5, 0);
1933 CHECK_FOR_ERROR
1934 delete $5;
Reid Spencer309080a2006-09-28 19:28:24 +00001935 } GlobalVarAttributes {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001936 CurGV = 0;
1937 CHECK_FOR_ERROR
1938 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001939 | TARGET TargetDefinition {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001940 CHECK_FOR_ERROR
1941 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001942 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer713eedc2006-08-18 08:43:06 +00001943 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001944 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001945 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001946
1947
1948AsmBlock : STRINGCONSTANT {
1949 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
1950 char *EndStr = UnEscapeLexed($1, true);
1951 std::string NewAsm($1, EndStr);
1952 free($1);
1953
1954 if (AsmSoFar.empty())
1955 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
1956 else
1957 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
Reid Spencer713eedc2006-08-18 08:43:06 +00001958 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001959};
1960
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001961TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001962 CurModule.CurrentModule->setTargetTriple($3);
1963 free($3);
John Criswell6af0b122006-10-24 19:09:48 +00001964 }
Chris Lattner7d1d0342006-10-22 06:08:13 +00001965 | DATALAYOUT '=' STRINGCONSTANT {
Owen Anderson85690f32006-10-18 02:21:48 +00001966 CurModule.CurrentModule->setDataLayout($3);
1967 free($3);
Owen Anderson85690f32006-10-18 02:21:48 +00001968 };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001969
1970LibrariesDefinition : '[' LibList ']';
1971
1972LibList : LibList ',' STRINGCONSTANT {
1973 CurModule.CurrentModule->addLibrary($3);
1974 free($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001975 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001976 }
1977 | STRINGCONSTANT {
1978 CurModule.CurrentModule->addLibrary($1);
1979 free($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001980 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001981 }
1982 | /* empty: end of list */ {
Reid Spencer713eedc2006-08-18 08:43:06 +00001983 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001984 }
1985 ;
1986
1987//===----------------------------------------------------------------------===//
1988// Rules to match Function Headers
1989//===----------------------------------------------------------------------===//
1990
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001991ArgListH : ArgListH ',' Types OptParamAttrs OptLocalName {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001992 if (!UpRefs.empty())
1993 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
1994 if (*$3 == Type::VoidTy)
1995 GEN_ERROR("void typed arguments are invalid!");
1996 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001997 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001998 $1->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00001999 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002000 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002001 | Types OptParamAttrs OptLocalName {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002002 if (!UpRefs.empty())
2003 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2004 if (*$1 == Type::VoidTy)
2005 GEN_ERROR("void typed arguments are invalid!");
2006 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2007 $$ = new ArgListType;
2008 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002009 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002010 };
2011
2012ArgList : ArgListH {
2013 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002014 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002015 }
2016 | ArgListH ',' DOTDOTDOT {
2017 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002018 struct ArgListEntry E;
2019 E.Ty = new PATypeHolder(Type::VoidTy);
2020 E.Name = 0;
2021 E.Attrs = FunctionType::NoAttributeSet;
2022 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002023 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002024 }
2025 | DOTDOTDOT {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002026 $$ = new ArgListType;
2027 struct ArgListEntry E;
2028 E.Ty = new PATypeHolder(Type::VoidTy);
2029 E.Name = 0;
2030 E.Attrs = FunctionType::NoAttributeSet;
2031 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002032 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002033 }
2034 | /* empty */ {
2035 $$ = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00002036 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002037 };
2038
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002039FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002040 OptFuncAttrs OptSection OptAlign {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002041 UnEscapeLexed($3);
2042 std::string FunctionName($3);
2043 free($3); // Free strdup'd memory!
2044
Reid Spencer87622ae2007-01-02 21:54:12 +00002045 // Check the function result for abstractness if this is a define. We should
2046 // have no abstract types at this point
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002047 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2048 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer87622ae2007-01-02 21:54:12 +00002049
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002050 std::vector<const Type*> ParamTypeList;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002051 std::vector<FunctionType::ParameterAttributes> ParamAttrs;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002052 ParamAttrs.push_back($7);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002053 if ($5) { // If there are arguments...
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002054 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I) {
2055 const Type* Ty = I->Ty->get();
Reid Spencer87622ae2007-01-02 21:54:12 +00002056 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2057 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002058 ParamTypeList.push_back(Ty);
2059 if (Ty != Type::VoidTy)
2060 ParamAttrs.push_back(I->Attrs);
2061 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002062 }
2063
2064 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2065 if (isVarArg) ParamTypeList.pop_back();
2066
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002067 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg,
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002068 ParamAttrs);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002069 const PointerType *PFT = PointerType::get(FT);
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002070 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002071
2072 ValID ID;
2073 if (!FunctionName.empty()) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002074 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002075 } else {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002076 ID = ValID::createGlobalID(CurModule.Values[PFT].size());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002077 }
2078
2079 Function *Fn = 0;
2080 // See if this function was forward referenced. If so, recycle the object.
2081 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2082 // Move the function to the end of the list, from whereever it was
2083 // previously inserted.
2084 Fn = cast<Function>(FWRef);
2085 CurModule.CurrentModule->getFunctionList().remove(Fn);
2086 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2087 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
2088 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
2089 // If this is the case, either we need to be a forward decl, or it needs
2090 // to be.
Reid Spencer5301e7c2007-01-30 20:08:39 +00002091 if (!CurFun.isDeclare && !Fn->isDeclaration())
Reid Spencer713eedc2006-08-18 08:43:06 +00002092 GEN_ERROR("Redefinition of function '" + FunctionName + "'!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002093
2094 // Make sure to strip off any argument names so we can't get conflicts.
Reid Spencer5301e7c2007-01-30 20:08:39 +00002095 if (Fn->isDeclaration())
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002096 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2097 AI != AE; ++AI)
2098 AI->setName("");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002099 } else { // Not already defined?
2100 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
2101 CurModule.CurrentModule);
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002102
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002103 InsertValue(Fn, CurModule.Values);
2104 }
2105
2106 CurFun.FunctionStart(Fn);
Anton Korobeynikov0ab01ff2006-09-17 13:06:18 +00002107
2108 if (CurFun.isDeclare) {
2109 // If we have declaration, always overwrite linkage. This will allow us to
2110 // correctly handle cases, when pointer to function is passed as argument to
2111 // another function.
2112 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002113 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov0ab01ff2006-09-17 13:06:18 +00002114 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002115 Fn->setCallingConv($1);
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002116 Fn->setAlignment($9);
2117 if ($8) {
2118 Fn->setSection($8);
2119 free($8);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002120 }
2121
2122 // Add all of the arguments we parsed to the function...
2123 if ($5) { // Is null if empty...
2124 if (isVarArg) { // Nuke the last entry
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002125 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0&&
Reid Spencere2c32da2006-12-03 05:46:11 +00002126 "Not a varargs marker!");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002127 delete $5->back().Ty;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002128 $5->pop_back(); // Delete the last entry
2129 }
2130 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002131 unsigned Idx = 1;
2132 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I, ++ArgIt) {
2133 delete I->Ty; // Delete the typeholder...
2134 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer309080a2006-09-28 19:28:24 +00002135 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002136 InsertValue(ArgIt);
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002137 Idx++;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002138 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002139
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002140 delete $5; // We're now done with the argument list
2141 }
Reid Spencer713eedc2006-08-18 08:43:06 +00002142 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002143};
2144
2145BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2146
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002147FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002148 $$ = CurFun.CurrentFunction;
2149
2150 // Make sure that we keep track of the linkage type even if there was a
2151 // previous "declare".
2152 $$->setLinkage($1);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002153 $$->setVisibility($2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002154};
2155
2156END : ENDTOK | '}'; // Allow end of '}' to end a function
2157
2158Function : BasicBlockList END {
2159 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002160 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002161};
2162
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002163FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002164 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002165 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002166 $$ = CurFun.CurrentFunction;
2167 CurFun.FunctionDone();
2168 CHECK_FOR_ERROR
2169 };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002170
2171//===----------------------------------------------------------------------===//
2172// Rules to match Basic Blocks
2173//===----------------------------------------------------------------------===//
2174
2175OptSideEffect : /* empty */ {
2176 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002177 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002178 }
2179 | SIDEEFFECT {
2180 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002181 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002182 };
2183
2184ConstValueRef : ESINT64VAL { // A reference to a direct constant
2185 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002186 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002187 }
2188 | EUINT64VAL {
2189 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002190 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002191 }
2192 | FPVAL { // Perhaps it's an FP constant?
2193 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002194 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002195 }
2196 | TRUETOK {
Zhou Sheng75b871f2007-01-11 12:24:14 +00002197 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer713eedc2006-08-18 08:43:06 +00002198 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002199 }
2200 | FALSETOK {
Zhou Sheng75b871f2007-01-11 12:24:14 +00002201 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer713eedc2006-08-18 08:43:06 +00002202 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002203 }
2204 | NULL_TOK {
2205 $$ = ValID::createNull();
Reid Spencer713eedc2006-08-18 08:43:06 +00002206 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002207 }
2208 | UNDEF {
2209 $$ = ValID::createUndef();
Reid Spencer713eedc2006-08-18 08:43:06 +00002210 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002211 }
2212 | ZEROINITIALIZER { // A vector zero constant.
2213 $$ = ValID::createZeroInit();
Reid Spencer713eedc2006-08-18 08:43:06 +00002214 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002215 }
2216 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencere2c32da2006-12-03 05:46:11 +00002217 const Type *ETy = (*$2)[0]->getType();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002218 int NumElements = $2->size();
2219
2220 PackedType* pt = PackedType::get(ETy, NumElements);
2221 PATypeHolder* PTy = new PATypeHolder(
Reid Spencere2c32da2006-12-03 05:46:11 +00002222 HandleUpRefs(
2223 PackedType::get(
2224 ETy,
2225 NumElements)
2226 )
2227 );
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002228
2229 // Verify all elements are correct type!
2230 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencere2c32da2006-12-03 05:46:11 +00002231 if (ETy != (*$2)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002232 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002233 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00002234 (*$2)[i]->getType()->getDescription() + "'.");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002235 }
2236
Reid Spencere2c32da2006-12-03 05:46:11 +00002237 $$ = ValID::create(ConstantPacked::get(pt, *$2));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002238 delete PTy; delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002239 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002240 }
2241 | ConstExpr {
Reid Spencere2c32da2006-12-03 05:46:11 +00002242 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002243 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002244 }
2245 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
2246 char *End = UnEscapeLexed($3, true);
2247 std::string AsmStr = std::string($3, End);
2248 End = UnEscapeLexed($5, true);
2249 std::string Constraints = std::string($5, End);
2250 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
2251 free($3);
2252 free($5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002253 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002254 };
2255
2256// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2257// another value.
2258//
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002259SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2260 $$ = ValID::createLocalID($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002261 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002262 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002263 | GLOBALVAL_ID {
2264 $$ = ValID::createGlobalID($1);
2265 CHECK_FOR_ERROR
2266 }
2267 | LocalName { // Is it a named reference...?
2268 $$ = ValID::createLocalName($1);
2269 CHECK_FOR_ERROR
2270 }
2271 | GlobalName { // Is it a named reference...?
2272 $$ = ValID::createGlobalName($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002273 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002274 };
2275
2276// ValueRef - A reference to a definition... either constant or symbolic
2277ValueRef : SymbolicValueRef | ConstValueRef;
2278
2279
2280// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2281// type immediately preceeds the value reference, and allows complex constant
2282// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2283ResolvedVal : Types ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002284 if (!UpRefs.empty())
2285 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2286 $$ = getVal(*$1, $2);
2287 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002288 CHECK_FOR_ERROR
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002289 }
2290 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002291
2292BasicBlockList : BasicBlockList BasicBlock {
2293 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002294 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002295 }
2296 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
2297 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002298 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002299 };
2300
2301
2302// Basic blocks are terminated by branching instructions:
2303// br, br/cc, switch, ret
2304//
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002305BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002306 setValueName($3, $2);
Reid Spencer309080a2006-09-28 19:28:24 +00002307 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002308 InsertValue($3);
2309
2310 $1->getInstList().push_back($3);
2311 InsertValue($1);
2312 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002313 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002314 };
2315
2316InstructionList : InstructionList Inst {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002317 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2318 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2319 if (CI2->getParent() == 0)
2320 $1->getInstList().push_back(CI2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002321 $1->getInstList().push_back($2);
2322 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002323 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002324 }
2325 | /* empty */ {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002326 $$ = getBBVal(ValID::createLocalID(CurFun.NextBBNum++), true);
Reid Spencer309080a2006-09-28 19:28:24 +00002327 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002328
2329 // Make sure to move the basic block to the correct location in the
2330 // function, instead of leaving it inserted wherever it was first
2331 // referenced.
2332 Function::BasicBlockListType &BBL =
2333 CurFun.CurrentFunction->getBasicBlockList();
2334 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer713eedc2006-08-18 08:43:06 +00002335 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002336 }
2337 | LABELSTR {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002338 $$ = getBBVal(ValID::createLocalName($1), true);
Reid Spencer309080a2006-09-28 19:28:24 +00002339 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002340
2341 // Make sure to move the basic block to the correct location in the
2342 // function, instead of leaving it inserted wherever it was first
2343 // referenced.
2344 Function::BasicBlockListType &BBL =
2345 CurFun.CurrentFunction->getBasicBlockList();
2346 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer713eedc2006-08-18 08:43:06 +00002347 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002348 };
2349
2350BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencere2c32da2006-12-03 05:46:11 +00002351 $$ = new ReturnInst($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00002352 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002353 }
2354 | RET VOID { // Return with no result...
2355 $$ = new ReturnInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002356 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002357 }
2358 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer309080a2006-09-28 19:28:24 +00002359 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002360 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002361 $$ = new BranchInst(tmpBB);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002362 } // Conditional Branch...
Reid Spencer58a8db02007-01-13 05:00:46 +00002363 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
2364 assert(cast<IntegerType>($2)->getBitWidth() == 1 && "Not Bool?");
Reid Spencer309080a2006-09-28 19:28:24 +00002365 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002366 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002367 BasicBlock* tmpBBB = getBBVal($9);
2368 CHECK_FOR_ERROR
Reid Spencer542964f2007-01-11 18:21:29 +00002369 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002370 CHECK_FOR_ERROR
2371 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002372 }
2373 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencere2c32da2006-12-03 05:46:11 +00002374 Value* tmpVal = getVal($2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002375 CHECK_FOR_ERROR
2376 BasicBlock* tmpBB = getBBVal($6);
2377 CHECK_FOR_ERROR
2378 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002379 $$ = S;
2380
2381 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2382 E = $8->end();
2383 for (; I != E; ++I) {
2384 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2385 S->addCase(CI, I->second);
2386 else
Reid Spencer713eedc2006-08-18 08:43:06 +00002387 GEN_ERROR("Switch case is constant, but not a simple integer!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002388 }
2389 delete $8;
Reid Spencer713eedc2006-08-18 08:43:06 +00002390 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002391 }
2392 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencere2c32da2006-12-03 05:46:11 +00002393 Value* tmpVal = getVal($2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002394 CHECK_FOR_ERROR
2395 BasicBlock* tmpBB = getBBVal($6);
2396 CHECK_FOR_ERROR
2397 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002398 $$ = S;
Reid Spencer713eedc2006-08-18 08:43:06 +00002399 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002400 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002401 | INVOKE OptCallingConv ResultTypes ValueRef '(' ValueRefList ')' OptFuncAttrs
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002402 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002403
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002404 // Handle the short syntax
2405 const PointerType *PFTy = 0;
2406 const FunctionType *Ty = 0;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002407 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002408 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2409 // Pull out the types of all of the arguments...
2410 std::vector<const Type*> ParamTypes;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002411 FunctionType::ParamAttrsList ParamAttrs;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002412 ParamAttrs.push_back($8);
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002413 for (ValueRefList::iterator I = $6->begin(), E = $6->end(); I != E; ++I) {
2414 const Type *Ty = I->Val->getType();
2415 if (Ty == Type::VoidTy)
2416 GEN_ERROR("Short call syntax cannot be used with varargs");
2417 ParamTypes.push_back(Ty);
2418 ParamAttrs.push_back(I->Attrs);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002419 }
2420
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002421 Ty = FunctionType::get($3->get(), ParamTypes, false, ParamAttrs);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002422 PFTy = PointerType::get(Ty);
2423 }
2424
2425 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer309080a2006-09-28 19:28:24 +00002426 CHECK_FOR_ERROR
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002427 BasicBlock *Normal = getBBVal($11);
Reid Spencer309080a2006-09-28 19:28:24 +00002428 CHECK_FOR_ERROR
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002429 BasicBlock *Except = getBBVal($14);
Reid Spencer309080a2006-09-28 19:28:24 +00002430 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002431
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002432 // Check the arguments
2433 ValueList Args;
2434 if ($6->empty()) { // Has no arguments?
2435 // Make sure no arguments is a good thing!
2436 if (Ty->getNumParams() != 0)
2437 GEN_ERROR("No arguments passed to a function that "
2438 "expects arguments!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002439 } else { // Has arguments?
2440 // Loop through FunctionType's arguments and ensure they are specified
2441 // correctly!
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002442 FunctionType::param_iterator I = Ty->param_begin();
2443 FunctionType::param_iterator E = Ty->param_end();
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002444 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002445
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002446 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2447 if (ArgI->Val->getType() != *I)
2448 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00002449 (*I)->getDescription() + "'!");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002450 Args.push_back(ArgI->Val);
2451 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002452
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002453 if (Ty->isVarArg()) {
2454 if (I == E)
2455 for (; ArgI != ArgE; ++ArgI)
2456 Args.push_back(ArgI->Val); // push the remaining varargs
2457 } else if (I != E || ArgI != ArgE)
Reid Spencere2c32da2006-12-03 05:46:11 +00002458 GEN_ERROR("Invalid number of parameters detected!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002459 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002460
2461 // Create the InvokeInst
2462 InvokeInst *II = new InvokeInst(V, Normal, Except, Args);
2463 II->setCallingConv($2);
2464 $$ = II;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002465 delete $6;
Reid Spencer713eedc2006-08-18 08:43:06 +00002466 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002467 }
2468 | UNWIND {
2469 $$ = new UnwindInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002470 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002471 }
2472 | UNREACHABLE {
2473 $$ = new UnreachableInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002474 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002475 };
2476
2477
2478
2479JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2480 $$ = $1;
Reid Spencere2c32da2006-12-03 05:46:11 +00002481 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Reid Spencer309080a2006-09-28 19:28:24 +00002482 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002483 if (V == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002484 GEN_ERROR("May only switch on a constant pool value!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002485
Reid Spencer309080a2006-09-28 19:28:24 +00002486 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002487 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002488 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002489 }
2490 | IntType ConstValueRef ',' LABEL ValueRef {
2491 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencere2c32da2006-12-03 05:46:11 +00002492 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Reid Spencer309080a2006-09-28 19:28:24 +00002493 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002494
2495 if (V == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002496 GEN_ERROR("May only switch on a constant pool value!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002497
Reid Spencer309080a2006-09-28 19:28:24 +00002498 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002499 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002500 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002501 };
2502
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002503Inst : OptLocalAssign InstVal {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002504 // Is this definition named?? if so, assign the name...
2505 setValueName($2, $1);
Reid Spencer309080a2006-09-28 19:28:24 +00002506 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002507 InsertValue($2);
2508 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002509 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002510};
2511
2512PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002513 if (!UpRefs.empty())
2514 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002515 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencere2c32da2006-12-03 05:46:11 +00002516 Value* tmpVal = getVal(*$1, $3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002517 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002518 BasicBlock* tmpBB = getBBVal($5);
2519 CHECK_FOR_ERROR
2520 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencere2c32da2006-12-03 05:46:11 +00002521 delete $1;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002522 }
2523 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2524 $$ = $1;
Reid Spencer309080a2006-09-28 19:28:24 +00002525 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002526 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002527 BasicBlock* tmpBB = getBBVal($6);
2528 CHECK_FOR_ERROR
2529 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002530 };
2531
2532
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002533ValueRefList : Types ValueRef OptParamAttrs {
2534 if (!UpRefs.empty())
2535 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2536 // Used for call and invoke instructions
2537 $$ = new ValueRefList();
2538 ValueRefListEntry E; E.Attrs = $3; E.Val = getVal($1->get(), $2);
2539 $$->push_back(E);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002540 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002541 | ValueRefList ',' Types ValueRef OptParamAttrs {
2542 if (!UpRefs.empty())
2543 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002544 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002545 ValueRefListEntry E; E.Attrs = $5; E.Val = getVal($3->get(), $4);
2546 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002547 CHECK_FOR_ERROR
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002548 }
2549 | /*empty*/ { $$ = new ValueRefList(); };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002550
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002551IndexList // Used for gep instructions and constant expressions
Reid Spencerd0da3e22006-12-31 21:47:02 +00002552 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002553 | IndexList ',' ResolvedVal {
2554 $$ = $1;
2555 $$->push_back($3);
2556 CHECK_FOR_ERROR
2557 }
Reid Spencerd0da3e22006-12-31 21:47:02 +00002558 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002559
2560OptTailCall : TAIL CALL {
2561 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002562 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002563 }
2564 | CALL {
2565 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002566 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002567 };
2568
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002569InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002570 if (!UpRefs.empty())
2571 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner03c49532007-01-15 02:27:26 +00002572 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencere2c32da2006-12-03 05:46:11 +00002573 !isa<PackedType>((*$2).get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002574 GEN_ERROR(
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002575 "Arithmetic operator requires integer, FP, or packed operands!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002576 if (isa<PackedType>((*$2).get()) &&
2577 ($1 == Instruction::URem ||
2578 $1 == Instruction::SRem ||
2579 $1 == Instruction::FRem))
Reid Spencerde46e482006-11-02 20:25:50 +00002580 GEN_ERROR("U/S/FRem not supported on packed types!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002581 Value* val1 = getVal(*$2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002582 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002583 Value* val2 = getVal(*$2, $5);
Reid Spencer309080a2006-09-28 19:28:24 +00002584 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002585 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002586 if ($$ == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002587 GEN_ERROR("binary operator returned null!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002588 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002589 }
2590 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002591 if (!UpRefs.empty())
2592 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner03c49532007-01-15 02:27:26 +00002593 if (!(*$2)->isInteger()) {
Reid Spencere2c32da2006-12-03 05:46:11 +00002594 if (!isa<PackedType>($2->get()) ||
Chris Lattner03c49532007-01-15 02:27:26 +00002595 !cast<PackedType>($2->get())->getElementType()->isInteger())
Reid Spencer713eedc2006-08-18 08:43:06 +00002596 GEN_ERROR("Logical operator requires integral operands!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002597 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002598 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002599 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002600 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer309080a2006-09-28 19:28:24 +00002601 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002602 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002603 if ($$ == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002604 GEN_ERROR("binary operator returned null!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002605 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002606 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002607 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002608 if (!UpRefs.empty())
2609 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer494289f2007-01-04 02:57:52 +00002610 if (isa<PackedType>((*$3).get()))
2611 GEN_ERROR("Packed types not supported by icmp instruction");
Reid Spencere2c32da2006-12-03 05:46:11 +00002612 Value* tmpVal1 = getVal(*$3, $4);
2613 CHECK_FOR_ERROR
2614 Value* tmpVal2 = getVal(*$3, $6);
2615 CHECK_FOR_ERROR
2616 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2617 if ($$ == 0)
2618 GEN_ERROR("icmp operator returned null!");
2619 }
2620 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002621 if (!UpRefs.empty())
2622 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer494289f2007-01-04 02:57:52 +00002623 if (isa<PackedType>((*$3).get()))
2624 GEN_ERROR("Packed types not supported by fcmp instruction");
Reid Spencere2c32da2006-12-03 05:46:11 +00002625 Value* tmpVal1 = getVal(*$3, $4);
2626 CHECK_FOR_ERROR
2627 Value* tmpVal2 = getVal(*$3, $6);
2628 CHECK_FOR_ERROR
2629 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2630 if ($$ == 0)
2631 GEN_ERROR("fcmp operator returned null!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002632 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002633 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002634 if ($4->getType() != Type::Int8Ty)
2635 GEN_ERROR("Shift amount must be i8 type!");
Chris Lattner03c49532007-01-15 02:27:26 +00002636 if (!$2->getType()->isInteger())
Reid Spencer713eedc2006-08-18 08:43:06 +00002637 GEN_ERROR("Shift constant expression requires integer operand!");
Reid Spencerfdff9382006-11-08 06:47:33 +00002638 CHECK_FOR_ERROR;
Reid Spencere2c32da2006-12-03 05:46:11 +00002639 $$ = new ShiftInst($1, $2, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002640 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002641 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002642 | CastOps ResolvedVal TO Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002643 if (!UpRefs.empty())
2644 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002645 Value* Val = $2;
Reid Spencerceb84592007-01-17 02:48:45 +00002646 const Type* DestTy = $4->get();
2647 if (!CastInst::castIsValid($1, Val, DestTy))
2648 GEN_ERROR("invalid cast opcode for cast from '" +
2649 Val->getType()->getDescription() + "' to '" +
2650 DestTy->getDescription() + "'!");
2651 $$ = CastInst::create($1, Val, DestTy);
Reid Spencere2c32da2006-12-03 05:46:11 +00002652 delete $4;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002653 }
2654 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer542964f2007-01-11 18:21:29 +00002655 if ($2->getType() != Type::Int1Ty)
Reid Spencer713eedc2006-08-18 08:43:06 +00002656 GEN_ERROR("select condition must be boolean!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002657 if ($4->getType() != $6->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002658 GEN_ERROR("select value types should match!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002659 $$ = new SelectInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002660 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002661 }
2662 | VAARG ResolvedVal ',' Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002663 if (!UpRefs.empty())
2664 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002665 $$ = new VAArgInst($2, *$4);
2666 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00002667 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002668 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002669 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002670 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencer713eedc2006-08-18 08:43:06 +00002671 GEN_ERROR("Invalid extractelement operands!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002672 $$ = new ExtractElementInst($2, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002673 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002674 }
2675 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002676 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencer713eedc2006-08-18 08:43:06 +00002677 GEN_ERROR("Invalid insertelement operands!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002678 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002679 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002680 }
Chris Lattner9ff96a72006-04-08 01:18:56 +00002681 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002682 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencer713eedc2006-08-18 08:43:06 +00002683 GEN_ERROR("Invalid shufflevector operands!");
Reid Spencere2c32da2006-12-03 05:46:11 +00002684 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002685 CHECK_FOR_ERROR
Chris Lattner9ff96a72006-04-08 01:18:56 +00002686 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002687 | PHI_TOK PHIList {
2688 const Type *Ty = $2->front().first->getType();
2689 if (!Ty->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002690 GEN_ERROR("PHI node operands must be of first class type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002691 $$ = new PHINode(Ty);
2692 ((PHINode*)$$)->reserveOperandSpace($2->size());
2693 while ($2->begin() != $2->end()) {
2694 if ($2->front().first->getType() != Ty)
Reid Spencer713eedc2006-08-18 08:43:06 +00002695 GEN_ERROR("All elements of a PHI node must be of the same type!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002696 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
2697 $2->pop_front();
2698 }
2699 delete $2; // Free the list...
Reid Spencer713eedc2006-08-18 08:43:06 +00002700 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002701 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002702 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ValueRefList ')'
2703 OptFuncAttrs {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002704
2705 // Handle the short syntax
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002706 const PointerType *PFTy = 0;
2707 const FunctionType *Ty = 0;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002708 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002709 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2710 // Pull out the types of all of the arguments...
2711 std::vector<const Type*> ParamTypes;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002712 FunctionType::ParamAttrsList ParamAttrs;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002713 ParamAttrs.push_back($8);
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002714 for (ValueRefList::iterator I = $6->begin(), E = $6->end(); I != E; ++I) {
2715 const Type *Ty = I->Val->getType();
2716 if (Ty == Type::VoidTy)
2717 GEN_ERROR("Short call syntax cannot be used with varargs");
2718 ParamTypes.push_back(Ty);
2719 ParamAttrs.push_back(I->Attrs);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002720 }
2721
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002722 Ty = FunctionType::get($3->get(), ParamTypes, false, ParamAttrs);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002723 PFTy = PointerType::get(Ty);
2724 }
2725
2726 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer309080a2006-09-28 19:28:24 +00002727 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002728
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002729 // Check the arguments
2730 ValueList Args;
2731 if ($6->empty()) { // Has no arguments?
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002732 // Make sure no arguments is a good thing!
2733 if (Ty->getNumParams() != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002734 GEN_ERROR("No arguments passed to a function that "
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002735 "expects arguments!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002736 } else { // Has arguments?
2737 // Loop through FunctionType's arguments and ensure they are specified
2738 // correctly!
2739 //
2740 FunctionType::param_iterator I = Ty->param_begin();
2741 FunctionType::param_iterator E = Ty->param_end();
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002742 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002743
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002744 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2745 if (ArgI->Val->getType() != *I)
2746 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00002747 (*I)->getDescription() + "'!");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002748 Args.push_back(ArgI->Val);
2749 }
2750 if (Ty->isVarArg()) {
2751 if (I == E)
2752 for (; ArgI != ArgE; ++ArgI)
2753 Args.push_back(ArgI->Val); // push the remaining varargs
2754 } else if (I != E || ArgI != ArgE)
Reid Spencer713eedc2006-08-18 08:43:06 +00002755 GEN_ERROR("Invalid number of parameters detected!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002756 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002757 // Create the call node
2758 CallInst *CI = new CallInst(V, Args);
2759 CI->setTailCall($1);
2760 CI->setCallingConv($2);
2761 $$ = CI;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002762 delete $6;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002763 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00002764 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002765 }
2766 | MemoryInst {
2767 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002768 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002769 };
2770
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002771OptVolatile : VOLATILE {
2772 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002773 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002774 }
2775 | /* empty */ {
2776 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002777 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002778 };
2779
2780
2781
2782MemoryInst : MALLOC Types OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002783 if (!UpRefs.empty())
2784 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002785 $$ = new MallocInst(*$2, 0, $3);
2786 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002787 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002788 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +00002789 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002790 if (!UpRefs.empty())
2791 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002792 Value* tmpVal = getVal($4, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002793 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002794 $$ = new MallocInst(*$2, tmpVal, $6);
2795 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002796 }
2797 | ALLOCA Types OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002798 if (!UpRefs.empty())
2799 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002800 $$ = new AllocaInst(*$2, 0, $3);
2801 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002802 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002803 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +00002804 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002805 if (!UpRefs.empty())
2806 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002807 Value* tmpVal = getVal($4, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002808 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002809 $$ = new AllocaInst(*$2, tmpVal, $6);
2810 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002811 }
2812 | FREE ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002813 if (!isa<PointerType>($2->getType()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002814 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencere2c32da2006-12-03 05:46:11 +00002815 $2->getType()->getDescription() + "!");
2816 $$ = new FreeInst($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00002817 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002818 }
2819
2820 | OptVolatile LOAD Types ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002821 if (!UpRefs.empty())
2822 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002823 if (!isa<PointerType>($3->get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002824 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencere2c32da2006-12-03 05:46:11 +00002825 (*$3)->getDescription());
2826 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002827 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencere2c32da2006-12-03 05:46:11 +00002828 (*$3)->getDescription());
2829 Value* tmpVal = getVal(*$3, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002830 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002831 $$ = new LoadInst(tmpVal, "", $1);
Reid Spencere2c32da2006-12-03 05:46:11 +00002832 delete $3;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002833 }
2834 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002835 if (!UpRefs.empty())
2836 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002837 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002838 if (!PT)
Reid Spencer713eedc2006-08-18 08:43:06 +00002839 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencere2c32da2006-12-03 05:46:11 +00002840 (*$5)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002841 const Type *ElTy = PT->getElementType();
Reid Spencere2c32da2006-12-03 05:46:11 +00002842 if (ElTy != $3->getType())
2843 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002844 "' into space of type '" + ElTy->getDescription() + "'!");
2845
Reid Spencere2c32da2006-12-03 05:46:11 +00002846 Value* tmpVal = getVal(*$5, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002847 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002848 $$ = new StoreInst($3, tmpVal, $1);
2849 delete $5;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002850 }
2851 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002852 if (!UpRefs.empty())
2853 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002854 if (!isa<PointerType>($2->get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002855 GEN_ERROR("getelementptr insn requires pointer operand!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002856
Reid Spencere2c32da2006-12-03 05:46:11 +00002857 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Reid Spencer713eedc2006-08-18 08:43:06 +00002858 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00002859 (*$2)->getDescription()+ "'!");
2860 Value* tmpVal = getVal(*$2, $3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002861 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002862 $$ = new GetElementPtrInst(tmpVal, *$4);
2863 delete $2;
Reid Spencer309080a2006-09-28 19:28:24 +00002864 delete $4;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002865 };
2866
2867
2868%%
Reid Spencer713eedc2006-08-18 08:43:06 +00002869
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002870// common code from the two 'RunVMAsmParser' functions
2871static Module* RunParser(Module * M) {
2872
2873 llvmAsmlineno = 1; // Reset the current line number...
2874 CurModule.CurrentModule = M;
2875#if YYDEBUG
2876 yydebug = Debug;
2877#endif
2878
2879 // Check to make sure the parser succeeded
2880 if (yyparse()) {
2881 if (ParserResult)
2882 delete ParserResult;
2883 return 0;
2884 }
2885
2886 // Check to make sure that parsing produced a result
2887 if (!ParserResult)
2888 return 0;
2889
2890 // Reset ParserResult variable while saving its value for the result.
2891 Module *Result = ParserResult;
2892 ParserResult = 0;
2893
2894 return Result;
2895}
2896
Reid Spencer713eedc2006-08-18 08:43:06 +00002897void llvm::GenerateError(const std::string &message, int LineNo) {
2898 if (LineNo == -1) LineNo = llvmAsmlineno;
2899 // TODO: column number in exception
2900 if (TheParseError)
2901 TheParseError->setError(CurFilename, message, LineNo);
2902 TriggerError = 1;
2903}
2904
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002905int yyerror(const char *ErrorMsg) {
2906 std::string where
2907 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2908 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
2909 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
2910 if (yychar == YYEMPTY || yychar == 0)
2911 errMsg += "end-of-file.";
2912 else
2913 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Reid Spencer713eedc2006-08-18 08:43:06 +00002914 GenerateError(errMsg);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002915 return 0;
2916}