blob: e70f25a7cf5a04cec08b9f0f7df27056a0ec70b8 [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"
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000020#include "llvm/ValueSymbolTable.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 Lattner55ce85c2007-01-31 04:44:08 +000023#include "llvm/ADT/SmallVector.h"
Chris Lattnerf20e61f2006-02-15 07:22:58 +000024#include "llvm/ADT/STLExtras.h"
25#include "llvm/Support/MathExtras.h"
Reid Spencerd5e19442006-12-01 00:33:46 +000026#include "llvm/Support/Streams.h"
Chris Lattnerf20e61f2006-02-15 07:22:58 +000027#include <algorithm>
Chris Lattnerf20e61f2006-02-15 07:22:58 +000028#include <list>
Chris Lattnerac26f382007-02-11 21:40:10 +000029#include <map>
Chris Lattnerf20e61f2006-02-15 07:22:58 +000030#include <utility>
Reid Spencer42f0cbe2006-12-31 05:40:51 +000031#ifndef NDEBUG
32#define YYDEBUG 1
33#endif
Chris Lattnerf20e61f2006-02-15 07:22:58 +000034
Reid Spencerb50974a2006-08-18 17:32:55 +000035// The following is a gross hack. In order to rid the libAsmParser library of
36// exceptions, we have to have a way of getting the yyparse function to go into
37// an error situation. So, whenever we want an error to occur, the GenerateError
38// function (see bottom of file) sets TriggerError. Then, at the end of each
39// production in the grammer we use CHECK_FOR_ERROR which will invoke YYERROR
40// (a goto) to put YACC in error state. Furthermore, several calls to
41// GenerateError are made from inside productions and they must simulate the
42// previous exception behavior by exiting the production immediately. We have
43// replaced these with the GEN_ERROR macro which calls GeneratError and then
44// immediately invokes YYERROR. This would be so much cleaner if it was a
45// recursive descent parser.
Reid Spencer713eedc2006-08-18 08:43:06 +000046static bool TriggerError = false;
Reid Spencerff359002006-10-09 17:36:59 +000047#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYABORT; } }
Reid Spencer713eedc2006-08-18 08:43:06 +000048#define GEN_ERROR(msg) { GenerateError(msg); YYERROR; }
49
Chris Lattnerf20e61f2006-02-15 07:22:58 +000050int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
51int yylex(); // declaration" of xxx warnings.
52int yyparse();
53
54namespace llvm {
55 std::string CurFilename;
Reid Spencer42f0cbe2006-12-31 05:40:51 +000056#if YYDEBUG
57static cl::opt<bool>
58Debug("debug-yacc", cl::desc("Print yacc debug state changes"),
59 cl::Hidden, cl::init(false));
60#endif
Chris Lattnerf20e61f2006-02-15 07:22:58 +000061}
62using namespace llvm;
63
64static Module *ParserResult;
65
66// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
67// relating to upreferences in the input stream.
68//
69//#define DEBUG_UPREFS 1
70#ifdef DEBUG_UPREFS
Bill Wendlingf3baad32006-12-07 01:30:32 +000071#define UR_OUT(X) cerr << X
Chris Lattnerf20e61f2006-02-15 07:22:58 +000072#else
73#define UR_OUT(X)
74#endif
75
76#define YYERROR_VERBOSE 1
77
Chris Lattnerf20e61f2006-02-15 07:22:58 +000078static GlobalVariable *CurGV;
79
80
81// This contains info used when building the body of a function. It is
82// destroyed when the function is completed.
83//
84typedef std::vector<Value *> ValueList; // Numbered defs
Reid Spencer42f0cbe2006-12-31 05:40:51 +000085
Chris Lattnerf20e61f2006-02-15 07:22:58 +000086static void
87ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
88 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
89
90static struct PerModuleInfo {
91 Module *CurrentModule;
92 std::map<const Type *, ValueList> Values; // Module level numbered definitions
93 std::map<const Type *,ValueList> LateResolveValues;
Reid Spencer55f1fbe2006-11-28 07:29:44 +000094 std::vector<PATypeHolder> Types;
95 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattnerf20e61f2006-02-15 07:22:58 +000096
97 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Chris Lattner7aa45902006-06-21 16:53:00 +000098 /// how they were referenced and on which line of the input they came from so
Chris Lattnerf20e61f2006-02-15 07:22:58 +000099 /// that we can resolve them later and print error messages as appropriate.
100 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
101
102 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
103 // references to global values. Global values may be referenced before they
104 // are defined, and if so, the temporary object that they represent is held
105 // here. This is used for forward references of GlobalValues.
106 //
107 typedef std::map<std::pair<const PointerType *,
108 ValID>, GlobalValue*> GlobalRefsType;
109 GlobalRefsType GlobalRefs;
110
111 void ModuleDone() {
112 // If we could not resolve some functions at function compilation time
113 // (calls to functions before they are defined), resolve them now... Types
114 // are resolved when the constant pool has been completely parsed.
115 //
116 ResolveDefinitions(LateResolveValues);
Reid Spencer309080a2006-09-28 19:28:24 +0000117 if (TriggerError)
118 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000119
120 // Check to make sure that all global value forward references have been
121 // resolved!
122 //
123 if (!GlobalRefs.empty()) {
124 std::string UndefinedReferences = "Unresolved global references exist:\n";
125
126 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
127 I != E; ++I) {
128 UndefinedReferences += " " + I->first.first->getDescription() + " " +
129 I->first.second.getName() + "\n";
130 }
Reid Spencer713eedc2006-08-18 08:43:06 +0000131 GenerateError(UndefinedReferences);
Reid Spencer309080a2006-09-28 19:28:24 +0000132 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000133 }
134
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000135 Values.clear(); // Clear out function local definitions
136 Types.clear();
137 CurrentModule = 0;
138 }
139
140 // GetForwardRefForGlobal - Check to see if there is a forward reference
141 // for this global. If so, remove it from the GlobalRefs map and return it.
142 // If not, just return null.
143 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
144 // Check to see if there is a forward reference to this global variable...
145 // if there is, eliminate it and patch the reference to use the new def'n.
146 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
147 GlobalValue *Ret = 0;
148 if (I != GlobalRefs.end()) {
149 Ret = I->second;
150 GlobalRefs.erase(I);
151 }
152 return Ret;
153 }
Reid Spencer87622ae2007-01-02 21:54:12 +0000154
155 bool TypeIsUnresolved(PATypeHolder* PATy) {
156 // If it isn't abstract, its resolved
157 const Type* Ty = PATy->get();
158 if (!Ty->isAbstract())
159 return false;
160 // Traverse the type looking for abstract types. If it isn't abstract then
161 // we don't need to traverse that leg of the type.
162 std::vector<const Type*> WorkList, SeenList;
163 WorkList.push_back(Ty);
164 while (!WorkList.empty()) {
165 const Type* Ty = WorkList.back();
166 SeenList.push_back(Ty);
167 WorkList.pop_back();
168 if (const OpaqueType* OpTy = dyn_cast<OpaqueType>(Ty)) {
169 // Check to see if this is an unresolved type
170 std::map<ValID, PATypeHolder>::iterator I = LateResolveTypes.begin();
171 std::map<ValID, PATypeHolder>::iterator E = LateResolveTypes.end();
172 for ( ; I != E; ++I) {
173 if (I->second.get() == OpTy)
174 return true;
175 }
176 } else if (const SequentialType* SeqTy = dyn_cast<SequentialType>(Ty)) {
177 const Type* TheTy = SeqTy->getElementType();
178 if (TheTy->isAbstract() && TheTy != Ty) {
179 std::vector<const Type*>::iterator I = SeenList.begin(),
180 E = SeenList.end();
181 for ( ; I != E; ++I)
182 if (*I == TheTy)
183 break;
184 if (I == E)
185 WorkList.push_back(TheTy);
186 }
187 } else if (const StructType* StrTy = dyn_cast<StructType>(Ty)) {
188 for (unsigned i = 0; i < StrTy->getNumElements(); ++i) {
189 const Type* TheTy = StrTy->getElementType(i);
190 if (TheTy->isAbstract() && TheTy != Ty) {
191 std::vector<const Type*>::iterator I = SeenList.begin(),
192 E = SeenList.end();
193 for ( ; I != E; ++I)
194 if (*I == TheTy)
195 break;
196 if (I == E)
197 WorkList.push_back(TheTy);
198 }
199 }
200 }
201 }
202 return false;
203 }
204
205
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000206} CurModule;
207
208static struct PerFunctionInfo {
209 Function *CurrentFunction; // Pointer to current function being created
210
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000211 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000212 std::map<const Type*, ValueList> LateResolveValues;
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000213 bool isDeclare; // Is this function a forward declararation?
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000214 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000215 GlobalValue::VisibilityTypes Visibility;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000216
217 /// BBForwardRefs - When we see forward references to basic blocks, keep
218 /// track of them here.
219 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
220 std::vector<BasicBlock*> NumberedBlocks;
221 unsigned NextBBNum;
222
223 inline PerFunctionInfo() {
224 CurrentFunction = 0;
225 isDeclare = false;
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000226 Linkage = GlobalValue::ExternalLinkage;
227 Visibility = GlobalValue::DefaultVisibility;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000228 }
229
230 inline void FunctionStart(Function *M) {
231 CurrentFunction = M;
232 NextBBNum = 0;
233 }
234
235 void FunctionDone() {
236 NumberedBlocks.clear();
237
238 // Any forward referenced blocks left?
Reid Spencer309080a2006-09-28 19:28:24 +0000239 if (!BBForwardRefs.empty()) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000240 GenerateError("Undefined reference to label " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000241 BBForwardRefs.begin()->first->getName());
Reid Spencer309080a2006-09-28 19:28:24 +0000242 return;
243 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000244
245 // Resolve all forward references now.
246 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
247
248 Values.clear(); // Clear out function local definitions
249 CurrentFunction = 0;
250 isDeclare = false;
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000251 Linkage = GlobalValue::ExternalLinkage;
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000252 Visibility = GlobalValue::DefaultVisibility;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000253 }
254} CurFun; // Info for the current function...
255
256static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
257
258
259//===----------------------------------------------------------------------===//
260// Code to handle definitions of all the types
261//===----------------------------------------------------------------------===//
262
263static int InsertValue(Value *V,
264 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
265 if (V->hasName()) return -1; // Is this a numbered definition?
266
267 // Yes, insert the value into the value table...
268 ValueList &List = ValueTab[V->getType()];
269 List.push_back(V);
270 return List.size()-1;
271}
272
273static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
274 switch (D.Type) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000275 case ValID::LocalID: // Is it a numbered definition?
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000276 // Module constants occupy the lowest numbered slots...
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000277 if (D.Num < CurModule.Types.size())
278 return CurModule.Types[D.Num];
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000279 break;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000280 case ValID::LocalName: // Is it a named definition?
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000281 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
282 D.destroy(); // Free old strdup'd memory...
283 return N;
284 }
285 break;
286 default:
Reid Spencera7bb3e92007-02-05 10:18:06 +0000287 GenerateError("Internal parser error: Invalid symbol type reference");
Reid Spencer309080a2006-09-28 19:28:24 +0000288 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000289 }
290
291 // If we reached here, we referenced either a symbol that we don't know about
292 // or an id number that hasn't been read yet. We may be referencing something
293 // forward, so just create an entry to be resolved later and get to it...
294 //
295 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
296
297
298 if (inFunctionScope()) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000299 if (D.Type == ValID::LocalName) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000300 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer309080a2006-09-28 19:28:24 +0000301 return 0;
302 } else {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000303 GenerateError("Reference to an undefined type: #" + utostr(D.Num));
Reid Spencer309080a2006-09-28 19:28:24 +0000304 return 0;
305 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000306 }
307
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000308 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000309 if (I != CurModule.LateResolveTypes.end())
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000310 return I->second;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000311
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000312 Type *Typ = OpaqueType::get();
313 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
314 return Typ;
Reid Spencere2c32da2006-12-03 05:46:11 +0000315 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000316
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000317// getValNonImprovising - Look up the value specified by the provided type and
318// the provided ValID. If the value exists and has already been defined, return
319// it. Otherwise return null.
320//
321static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Reid Spencer309080a2006-09-28 19:28:24 +0000322 if (isa<FunctionType>(Ty)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000323 GenerateError("Functions are not values and "
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000324 "must be referenced as pointers");
Reid Spencer309080a2006-09-28 19:28:24 +0000325 return 0;
326 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000327
328 switch (D.Type) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000329 case ValID::LocalID: { // Is it a numbered definition?
330 // Module constants occupy the lowest numbered slots.
331 std::map<const Type*,ValueList>::iterator VI = CurFun.Values.find(Ty);
332 // Make sure that our type is within bounds.
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000333 if (VI == CurFun.Values.end()) return 0;
334
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000335 // Check that the number is within bounds.
336 if (D.Num >= VI->second.size()) return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000337
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000338 return VI->second[D.Num];
339 }
340 case ValID::GlobalID: { // Is it a numbered definition?
341 unsigned Num = D.Num;
342
343 // Module constants occupy the lowest numbered slots...
344 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000345 if (VI == CurModule.Values.end())
346 return 0;
347 if (D.Num >= VI->second.size())
348 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000349 return VI->second[Num];
350 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000351
352 case ValID::LocalName: { // Is it a named definition?
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000353 if (!inFunctionScope())
354 return 0;
355 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
356 Value *N = SymTab.lookup(D.Name);
357 if (N == 0)
358 return 0;
359 if (N->getType() != Ty)
360 return 0;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000361
362 D.destroy(); // Free old strdup'd memory...
363 return N;
364 }
365 case ValID::GlobalName: { // Is it a named definition?
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000366 ValueSymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable();
367 Value *N = SymTab.lookup(D.Name);
368 if (N == 0)
369 return 0;
370 if (N->getType() != Ty)
371 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000372
373 D.destroy(); // Free old strdup'd memory...
374 return N;
375 }
376
377 // Check to make sure that "Ty" is an integral type, and that our
378 // value will fit into the specified type...
379 case ValID::ConstSIntVal: // Is it a constant pool reference??
Reid Spencere0fc4df2006-10-20 07:07:24 +0000380 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000381 GenerateError("Signed integral constant '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000382 itostr(D.ConstPool64) + "' is invalid for type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +0000383 Ty->getDescription() + "'");
Reid Spencer309080a2006-09-28 19:28:24 +0000384 return 0;
385 }
Reid Spencere0fc4df2006-10-20 07:07:24 +0000386 return ConstantInt::get(Ty, D.ConstPool64);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000387
388 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Reid Spencere0fc4df2006-10-20 07:07:24 +0000389 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
390 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000391 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Reid Spencera7bb3e92007-02-05 10:18:06 +0000392 "' is invalid or out of range");
Reid Spencer309080a2006-09-28 19:28:24 +0000393 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000394 } else { // This is really a signed reference. Transmogrify.
Reid Spencere0fc4df2006-10-20 07:07:24 +0000395 return ConstantInt::get(Ty, D.ConstPool64);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000396 }
397 } else {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000398 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000399 }
400
401 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Reid Spencer309080a2006-09-28 19:28:24 +0000402 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP)) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000403 GenerateError("FP constant invalid for type");
Reid Spencer309080a2006-09-28 19:28:24 +0000404 return 0;
405 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000406 return ConstantFP::get(Ty, D.ConstPoolFP);
407
408 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer309080a2006-09-28 19:28:24 +0000409 if (!isa<PointerType>(Ty)) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000410 GenerateError("Cannot create a a non pointer null");
Reid Spencer309080a2006-09-28 19:28:24 +0000411 return 0;
412 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000413 return ConstantPointerNull::get(cast<PointerType>(Ty));
414
415 case ValID::ConstUndefVal: // Is it an undef value?
416 return UndefValue::get(Ty);
417
418 case ValID::ConstZeroVal: // Is it a zero value?
419 return Constant::getNullValue(Ty);
420
421 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer309080a2006-09-28 19:28:24 +0000422 if (D.ConstantValue->getType() != Ty) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000423 GenerateError("Constant expression type different from required type");
Reid Spencer309080a2006-09-28 19:28:24 +0000424 return 0;
425 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000426 return D.ConstantValue;
427
428 case ValID::InlineAsmVal: { // Inline asm expression
429 const PointerType *PTy = dyn_cast<PointerType>(Ty);
430 const FunctionType *FTy =
431 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer309080a2006-09-28 19:28:24 +0000432 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000433 GenerateError("Invalid type for asm constraint string");
Reid Spencer309080a2006-09-28 19:28:24 +0000434 return 0;
435 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000436 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
437 D.IAD->HasSideEffects);
438 D.destroy(); // Free InlineAsmDescriptor.
439 return IA;
440 }
441 default:
Reid Spencerac6bfc52007-02-05 17:04:00 +0000442 assert(0 && "Unhandled case!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000443 return 0;
444 } // End of switch
445
Reid Spencerac6bfc52007-02-05 17:04:00 +0000446 assert(0 && "Unhandled case!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000447 return 0;
448}
449
450// getVal - This function is identical to getValNonImprovising, except that if a
451// value is not already defined, it "improvises" by creating a placeholder var
452// that looks and acts just like the requested variable. When the value is
453// defined later, all uses of the placeholder variable are replaced with the
454// real thing.
455//
456static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer309080a2006-09-28 19:28:24 +0000457 if (Ty == Type::LabelTy) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000458 GenerateError("Cannot use a basic block here");
Reid Spencer309080a2006-09-28 19:28:24 +0000459 return 0;
460 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000461
462 // See if the value has already been defined.
463 Value *V = getValNonImprovising(Ty, ID);
464 if (V) return V;
Reid Spencer309080a2006-09-28 19:28:24 +0000465 if (TriggerError) return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000466
Reid Spencer309080a2006-09-28 19:28:24 +0000467 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000468 GenerateError("Invalid use of a composite type");
Reid Spencer309080a2006-09-28 19:28:24 +0000469 return 0;
470 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000471
472 // If we reached here, we referenced either a symbol that we don't know about
473 // or an id number that hasn't been read yet. We may be referencing something
474 // forward, so just create an entry to be resolved later and get to it...
475 //
476 V = new Argument(Ty);
477
478 // Remember where this forward reference came from. FIXME, shouldn't we try
479 // to recycle these things??
480 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
481 llvmAsmlineno)));
482
483 if (inFunctionScope())
484 InsertValue(V, CurFun.LateResolveValues);
485 else
486 InsertValue(V, CurModule.LateResolveValues);
487 return V;
488}
489
490/// getBBVal - This is used for two purposes:
491/// * If isDefinition is true, a new basic block with the specified ID is being
492/// defined.
493/// * If isDefinition is true, this is a reference to a basic block, which may
494/// or may not be a forward reference.
495///
496static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
Reid Spencerac6bfc52007-02-05 17:04:00 +0000497 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000498
499 std::string Name;
500 BasicBlock *BB = 0;
501 switch (ID.Type) {
Reid Spencer309080a2006-09-28 19:28:24 +0000502 default:
503 GenerateError("Illegal label reference " + ID.getName());
504 return 0;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000505 case ValID::LocalID: // Is it a numbered definition?
506 if (ID.Num >= CurFun.NumberedBlocks.size())
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000507 CurFun.NumberedBlocks.resize(ID.Num+1);
508 BB = CurFun.NumberedBlocks[ID.Num];
509 break;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000510 case ValID::LocalName: // Is it a named definition?
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000511 Name = ID.Name;
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000512 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
513 if (N && N->getType()->getTypeID() == Type::LabelTyID)
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000514 BB = cast<BasicBlock>(N);
515 break;
516 }
517
518 // See if the block has already been defined.
519 if (BB) {
520 // If this is the definition of the block, make sure the existing value was
521 // just a forward reference. If it was a forward reference, there will be
522 // an entry for it in the PlaceHolderInfo map.
Reid Spencer309080a2006-09-28 19:28:24 +0000523 if (isDefinition && !CurFun.BBForwardRefs.erase(BB)) {
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000524 // The existing value was a definition, not a forward reference.
Reid Spencer713eedc2006-08-18 08:43:06 +0000525 GenerateError("Redefinition of label " + ID.getName());
Reid Spencer309080a2006-09-28 19:28:24 +0000526 return 0;
527 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000528
529 ID.destroy(); // Free strdup'd memory.
530 return BB;
531 }
532
533 // Otherwise this block has not been seen before.
534 BB = new BasicBlock("", CurFun.CurrentFunction);
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000535 if (ID.Type == ValID::LocalName) {
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000536 BB->setName(ID.Name);
537 } else {
538 CurFun.NumberedBlocks[ID.Num] = BB;
539 }
540
541 // If this is not a definition, keep track of it so we can use it as a forward
542 // reference.
543 if (!isDefinition) {
544 // Remember where this forward reference came from.
545 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
546 } else {
547 // The forward declaration could have been inserted anywhere in the
548 // function: insert it into the correct place now.
549 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
550 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
551 }
552 ID.destroy();
553 return BB;
554}
555
556
557//===----------------------------------------------------------------------===//
558// Code to handle forward references in instructions
559//===----------------------------------------------------------------------===//
560//
561// This code handles the late binding needed with statements that reference
562// values not defined yet... for example, a forward branch, or the PHI node for
563// a loop body.
564//
565// This keeps a table (CurFun.LateResolveValues) of all such forward references
566// and back patchs after we are done.
567//
568
569// ResolveDefinitions - If we could not resolve some defs at parsing
570// time (forward branches, phi functions for loops, etc...) resolve the
571// defs now...
572//
573static void
574ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
575 std::map<const Type*,ValueList> *FutureLateResolvers) {
576 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
577 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
578 E = LateResolvers.end(); LRI != E; ++LRI) {
579 ValueList &List = LRI->second;
580 while (!List.empty()) {
581 Value *V = List.back();
582 List.pop_back();
583
584 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
585 CurModule.PlaceHolderInfo.find(V);
Reid Spencerac6bfc52007-02-05 17:04:00 +0000586 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000587
588 ValID &DID = PHI->second.first;
589
590 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Reid Spencer309080a2006-09-28 19:28:24 +0000591 if (TriggerError)
592 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000593 if (TheRealValue) {
594 V->replaceAllUsesWith(TheRealValue);
595 delete V;
596 CurModule.PlaceHolderInfo.erase(PHI);
597 } else if (FutureLateResolvers) {
598 // Functions have their unresolved items forwarded to the module late
599 // resolver table
600 InsertValue(V, *FutureLateResolvers);
601 } else {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000602 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000603 GenerateError("Reference to an invalid definition: '" +DID.getName()+
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000604 "' of type '" + V->getType()->getDescription() + "'",
605 PHI->second.second);
Reid Spencer309080a2006-09-28 19:28:24 +0000606 return;
607 } else {
Reid Spencer713eedc2006-08-18 08:43:06 +0000608 GenerateError("Reference to an invalid definition: #" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000609 itostr(DID.Num) + " of type '" +
610 V->getType()->getDescription() + "'",
611 PHI->second.second);
Reid Spencer309080a2006-09-28 19:28:24 +0000612 return;
613 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000614 }
615 }
616 }
617
618 LateResolvers.clear();
619}
620
621// ResolveTypeTo - A brand new type was just declared. This means that (if
622// name is not null) things referencing Name can be resolved. Otherwise, things
623// refering to the number can be resolved. Do this now.
624//
625static void ResolveTypeTo(char *Name, const Type *ToTy) {
626 ValID D;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000627 if (Name) D = ValID::createLocalName(Name);
628 else D = ValID::createLocalID(CurModule.Types.size());
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000629
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000630 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000631 CurModule.LateResolveTypes.find(D);
632 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000633 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000634 CurModule.LateResolveTypes.erase(I);
635 }
636}
637
638// setValueName - Set the specified value to the name given. The name may be
639// null potentially, in which case this is a noop. The string passed in is
640// assumed to be a malloc'd string buffer, and is free'd by this function.
641//
642static void setValueName(Value *V, char *NameStr) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000643 if (!NameStr) return;
644 std::string Name(NameStr); // Copy string
645 free(NameStr); // Free old string
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000646
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000647 if (V->getType() == Type::VoidTy) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000648 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000649 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000650 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000651
Reid Spencerac6bfc52007-02-05 17:04:00 +0000652 assert(inFunctionScope() && "Must be in function scope!");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000653 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
654 if (ST.lookup(Name)) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000655 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +0000656 V->getType()->getDescription() + "'");
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000657 return;
658 }
659
660 // Set the name.
661 V->setName(Name);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000662}
663
664/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
665/// this is a declaration, otherwise it is a definition.
666static GlobalVariable *
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000667ParseGlobalVariable(char *NameStr,
668 GlobalValue::LinkageTypes Linkage,
669 GlobalValue::VisibilityTypes Visibility,
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000670 bool isConstantGlobal, const Type *Ty,
671 Constant *Initializer) {
Reid Spencer309080a2006-09-28 19:28:24 +0000672 if (isa<FunctionType>(Ty)) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000673 GenerateError("Cannot declare global vars of function type");
Reid Spencer309080a2006-09-28 19:28:24 +0000674 return 0;
675 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000676
677 const PointerType *PTy = PointerType::get(Ty);
678
679 std::string Name;
680 if (NameStr) {
681 Name = NameStr; // Copy string
682 free(NameStr); // Free old string
683 }
684
685 // See if this global value was forward referenced. If so, recycle the
686 // object.
687 ValID ID;
688 if (!Name.empty()) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000689 ID = ValID::createGlobalName((char*)Name.c_str());
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000690 } else {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000691 ID = ValID::createGlobalID(CurModule.Values[PTy].size());
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000692 }
693
694 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
695 // Move the global to the end of the list, from whereever it was
696 // previously inserted.
697 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
698 CurModule.CurrentModule->getGlobalList().remove(GV);
699 CurModule.CurrentModule->getGlobalList().push_back(GV);
700 GV->setInitializer(Initializer);
701 GV->setLinkage(Linkage);
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000702 GV->setVisibility(Visibility);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000703 GV->setConstant(isConstantGlobal);
704 InsertValue(GV, CurModule.Values);
705 return GV;
706 }
707
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000708 // If this global has a name
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000709 if (!Name.empty()) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000710 // if the global we're parsing has an initializer (is a definition) and
711 // has external linkage.
712 if (Initializer && Linkage != GlobalValue::InternalLinkage)
713 // If there is already a global with external linkage with this name
714 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
715 // If we allow this GVar to get created, it will be renamed in the
716 // symbol table because it conflicts with an existing GVar. We can't
717 // allow redefinition of GVars whose linking indicates that their name
718 // must stay the same. Issue the error.
719 GenerateError("Redefinition of global variable named '" + Name +
720 "' of type '" + Ty->getDescription() + "'");
721 return 0;
722 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000723 }
724
725 // Otherwise there is no existing GV to use, create one now.
726 GlobalVariable *GV =
727 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
728 CurModule.CurrentModule);
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000729 GV->setVisibility(Visibility);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000730 InsertValue(GV, CurModule.Values);
731 return GV;
732}
733
734// setTypeName - Set the specified type to the name given. The name may be
735// null potentially, in which case this is a noop. The string passed in is
736// assumed to be a malloc'd string buffer, and is freed by this function.
737//
738// This function returns true if the type has already been defined, but is
739// allowed to be redefined in the specified context. If the name is a new name
740// for the type plane, it is inserted and false is returned.
741static bool setTypeName(const Type *T, char *NameStr) {
Reid Spencerac6bfc52007-02-05 17:04:00 +0000742 assert(!inFunctionScope() && "Can't give types function-local names!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000743 if (NameStr == 0) return false;
744
745 std::string Name(NameStr); // Copy string
746 free(NameStr); // Free old string
747
748 // We don't allow assigning names to void type
Reid Spencer309080a2006-09-28 19:28:24 +0000749 if (T == Type::VoidTy) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000750 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer309080a2006-09-28 19:28:24 +0000751 return false;
752 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000753
754 // Set the type name, checking for conflicts as we do so.
755 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
756
757 if (AlreadyExists) { // Inserting a name that is already defined???
758 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencerac6bfc52007-02-05 17:04:00 +0000759 assert(Existing && "Conflict but no matching type?!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000760
761 // There is only one case where this is allowed: when we are refining an
762 // opaque type. In this case, Existing will be an opaque type.
763 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
764 // We ARE replacing an opaque type!
765 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
766 return true;
767 }
768
769 // Otherwise, this is an attempt to redefine a type. That's okay if
770 // the redefinition is identical to the original. This will be so if
771 // Existing and T point to the same Type object. In this one case we
772 // allow the equivalent redefinition.
773 if (Existing == T) return true; // Yes, it's equal.
774
775 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer33259082007-01-05 21:51:07 +0000776 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +0000777 T->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000778 }
779
780 return false;
781}
782
783//===----------------------------------------------------------------------===//
784// Code for handling upreferences in type names...
785//
786
787// TypeContains - Returns true if Ty directly contains E in it.
788//
789static bool TypeContains(const Type *Ty, const Type *E) {
790 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
791 E) != Ty->subtype_end();
792}
793
794namespace {
795 struct UpRefRecord {
796 // NestingLevel - The number of nesting levels that need to be popped before
797 // this type is resolved.
798 unsigned NestingLevel;
799
800 // LastContainedTy - This is the type at the current binding level for the
801 // type. Every time we reduce the nesting level, this gets updated.
802 const Type *LastContainedTy;
803
804 // UpRefTy - This is the actual opaque type that the upreference is
805 // represented with.
806 OpaqueType *UpRefTy;
807
808 UpRefRecord(unsigned NL, OpaqueType *URTy)
809 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
810 };
811}
812
813// UpRefs - A list of the outstanding upreferences that need to be resolved.
814static std::vector<UpRefRecord> UpRefs;
815
816/// HandleUpRefs - Every time we finish a new layer of types, this function is
817/// called. It loops through the UpRefs vector, which is a list of the
818/// currently active types. For each type, if the up reference is contained in
819/// the newly completed type, we decrement the level count. When the level
820/// count reaches zero, the upreferenced type is the type that is passed in:
821/// thus we can complete the cycle.
822///
823static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner680aab62006-08-18 17:34:45 +0000824 // If Ty isn't abstract, or if there are no up-references in it, then there is
825 // nothing to resolve here.
826 if (!ty->isAbstract() || UpRefs.empty()) return ty;
827
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000828 PATypeHolder Ty(ty);
829 UR_OUT("Type '" << Ty->getDescription() <<
830 "' newly formed. Resolving upreferences.\n" <<
831 UpRefs.size() << " upreferences active!\n");
832
833 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
834 // to zero), we resolve them all together before we resolve them to Ty. At
835 // the end of the loop, if there is anything to resolve to Ty, it will be in
836 // this variable.
837 OpaqueType *TypeToResolve = 0;
838
839 for (unsigned i = 0; i != UpRefs.size(); ++i) {
840 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
841 << UpRefs[i].second->getDescription() << ") = "
842 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
843 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
844 // Decrement level of upreference
845 unsigned Level = --UpRefs[i].NestingLevel;
846 UpRefs[i].LastContainedTy = Ty;
847 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
848 if (Level == 0) { // Upreference should be resolved!
849 if (!TypeToResolve) {
850 TypeToResolve = UpRefs[i].UpRefTy;
851 } else {
852 UR_OUT(" * Resolving upreference for "
853 << UpRefs[i].second->getDescription() << "\n";
854 std::string OldName = UpRefs[i].UpRefTy->getDescription());
855 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
856 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
857 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
858 }
859 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
860 --i; // Do not skip the next element...
861 }
862 }
863 }
864
865 if (TypeToResolve) {
866 UR_OUT(" * Resolving upreference for "
867 << UpRefs[i].second->getDescription() << "\n";
868 std::string OldName = TypeToResolve->getDescription());
869 TypeToResolve->refineAbstractTypeTo(Ty);
870 }
871
872 return Ty;
873}
874
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000875//===----------------------------------------------------------------------===//
876// RunVMAsmParser - Define an interface to this parser
877//===----------------------------------------------------------------------===//
878//
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000879static Module* RunParser(Module * M);
880
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000881Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
882 set_scan_file(F);
883
884 CurFilename = Filename;
885 return RunParser(new Module(CurFilename));
886}
887
888Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
889 set_scan_string(AsmString);
890
891 CurFilename = "from_memory";
892 if (M == NULL) {
893 return RunParser(new Module (CurFilename));
894 } else {
895 return RunParser(M);
896 }
897}
898
899%}
900
901%union {
902 llvm::Module *ModuleVal;
903 llvm::Function *FunctionVal;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000904 llvm::BasicBlock *BasicBlockVal;
905 llvm::TerminatorInst *TermInstVal;
906 llvm::Instruction *InstVal;
Reid Spencere2c32da2006-12-03 05:46:11 +0000907 llvm::Constant *ConstVal;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000908
Reid Spencere2c32da2006-12-03 05:46:11 +0000909 const llvm::Type *PrimType;
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000910 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencere2c32da2006-12-03 05:46:11 +0000911 llvm::PATypeHolder *TypeVal;
912 llvm::Value *ValueVal;
Reid Spencere2c32da2006-12-03 05:46:11 +0000913 std::vector<llvm::Value*> *ValueList;
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000914 llvm::ArgListType *ArgList;
915 llvm::TypeWithAttrs TypeWithAttrs;
916 llvm::TypeWithAttrsList *TypeWithAttrsList;
917 llvm::ValueRefList *ValueRefList;
918
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000919 // Represent the RHS of PHI node
Reid Spencere2c32da2006-12-03 05:46:11 +0000920 std::list<std::pair<llvm::Value*,
921 llvm::BasicBlock*> > *PHIList;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000922 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencere2c32da2006-12-03 05:46:11 +0000923 std::vector<llvm::Constant*> *ConstVector;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000924
925 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000926 llvm::GlobalValue::VisibilityTypes Visibility;
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000927 llvm::FunctionType::ParameterAttributes ParamAttrs;
Reid Spencerc7a686b2007-02-28 02:24:54 +0000928 llvm::APInt *APIntVal;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000929 int64_t SInt64Val;
930 uint64_t UInt64Val;
931 int SIntVal;
932 unsigned UIntVal;
933 double FPVal;
934 bool BoolVal;
935
936 char *StrVal; // This memory is strdup'd!
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000937 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000938
Reid Spencere2c32da2006-12-03 05:46:11 +0000939 llvm::Instruction::BinaryOps BinaryOpVal;
940 llvm::Instruction::TermOps TermOpVal;
941 llvm::Instruction::MemoryOps MemOpVal;
942 llvm::Instruction::CastOps CastOpVal;
943 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencere2c32da2006-12-03 05:46:11 +0000944 llvm::ICmpInst::Predicate IPredicate;
945 llvm::FCmpInst::Predicate FPredicate;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000946}
947
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000948%type <ModuleVal> Module
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000949%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
950%type <BasicBlockVal> BasicBlock InstructionList
951%type <TermInstVal> BBTerminatorInst
952%type <InstVal> Inst InstVal MemoryInst
953%type <ConstVal> ConstVal ConstExpr
954%type <ConstVector> ConstVector
955%type <ArgList> ArgList ArgListH
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000956%type <PHIList> PHIList
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000957%type <ValueRefList> ValueRefList // For call param lists & GEP indices
958%type <ValueList> IndexList // For GEP indices
959%type <TypeList> TypeListI
960%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencerbf48e3c2007-01-05 17:07:23 +0000961%type <TypeWithAttrs> ArgType
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000962%type <JumpTable> JumpTable
963%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
964%type <BoolVal> OptVolatile // 'volatile' or not
965%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
966%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000967%type <Linkage> GVInternalLinkage GVExternalLinkage
968%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000969%type <Visibility> GVVisibilityStyle
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000970
971// ValueRef - Unresolved reference to a definition or BB
972%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
973%type <ValueVal> ResolvedVal // <type> <valref> pair
974// Tokens and types for handling constant integer values
975//
976// ESINT64VAL - A negative number within long long range
977%token <SInt64Val> ESINT64VAL
978
979// EUINT64VAL - A positive number within uns. long long range
980%token <UInt64Val> EUINT64VAL
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000981
Reid Spencerc7a686b2007-02-28 02:24:54 +0000982// ESAPINTVAL - A negative number with arbitrary precision
983%token <APIntVal> ESAPINTVAL
984
985// EUAPINTVAL - A positive number with arbitrary precision
986%token <APIntVal> EUAPINTVAL
987
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000988%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000989%token <FPVal> FPVAL // Float or Double constant
990
991// Built in types...
Reid Spencerbf48e3c2007-01-05 17:07:23 +0000992%type <TypeVal> Types ResultTypes
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000993%type <PrimType> IntType FPType PrimType // Classifications
Reid Spencer58a8db02007-01-13 05:00:46 +0000994%token <PrimType> VOID INTTYPE
Reid Spencerdc0a3a22006-12-29 20:35:03 +0000995%token <PrimType> FLOAT DOUBLE LABEL
996%token TYPE
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000997
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000998%token<StrVal> LOCALVAR GLOBALVAR LABELSTR STRINGCONSTANT ATSTRINGCONSTANT
999%type <StrVal> LocalName OptLocalName OptLocalAssign
1000%type <StrVal> GlobalName OptGlobalAssign
1001%type <UIntVal> OptAlign OptCAlign
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001002%type <StrVal> OptSection SectionString
1003
1004%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001005%token DECLARE DEFINE GLOBAL CONSTANT SECTION VOLATILE
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001006%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001007%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001008%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001009%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001010%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattner7d1d0342006-10-22 06:08:13 +00001011%token DATALAYOUT
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001012%type <UIntVal> OptCallingConv
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001013%type <ParamAttrs> OptParamAttrs ParamAttr
1014%type <ParamAttrs> OptFuncAttrs FuncAttr
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001015
1016// Basic Block Terminating Operators
1017%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
1018
1019// Binary Operators
Reid Spencer266e42b2006-12-23 06:05:41 +00001020%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencerde46e482006-11-02 20:25:50 +00001021%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer2341c222007-02-02 02:16:23 +00001022%token <BinaryOpVal> SHL LSHR ASHR
1023
Reid Spencere2c32da2006-12-03 05:46:11 +00001024%token <OtherOpVal> ICMP FCMP
Reid Spencere2c32da2006-12-03 05:46:11 +00001025%type <IPredicate> IPredicates
Reid Spencere2c32da2006-12-03 05:46:11 +00001026%type <FPredicate> FPredicates
Reid Spencer1960ef32006-12-03 06:59:29 +00001027%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1028%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001029
1030// Memory Instructions
1031%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1032
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001033// Cast Operators
1034%type <CastOpVal> CastOps
1035%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1036%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1037
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001038// Other Operators
Reid Spencer2341c222007-02-02 02:16:23 +00001039%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattner9ff96a72006-04-08 01:18:56 +00001040%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001041
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001042// Function Attributes
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001043%token NORETURN INREG SRET
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001044
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001045// Visibility Styles
1046%token DEFAULT HIDDEN
1047
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001048%start Module
1049%%
1050
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001051
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001052// Operations that are notably excluded from this list include:
1053// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1054//
Reid Spencerde46e482006-11-02 20:25:50 +00001055ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer2341c222007-02-02 02:16:23 +00001056LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001057CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1058 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer2341c222007-02-02 02:16:23 +00001059
Reid Spencer1960ef32006-12-03 06:59:29 +00001060IPredicates
Reid Spencerd2e0c342006-12-04 05:24:24 +00001061 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer1960ef32006-12-03 06:59:29 +00001062 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1063 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1064 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1065 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1066 ;
1067
1068FPredicates
1069 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1070 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1071 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1072 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1073 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1074 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1075 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1076 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1077 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1078 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001079
1080// These are some types that allow classification if we only want a particular
1081// thing... for example, only a signed, unsigned, or integral type.
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001082IntType : INTTYPE;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001083FPType : FLOAT | DOUBLE;
1084
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001085LocalName : LOCALVAR | STRINGCONSTANT;
1086OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1087
1088/// OptLocalAssign - Value producing statements have an optional assignment
1089/// component.
1090OptLocalAssign : LocalName '=' {
1091 $$ = $1;
1092 CHECK_FOR_ERROR
1093 }
1094 | /*empty*/ {
1095 $$ = 0;
1096 CHECK_FOR_ERROR
1097 };
1098
1099GlobalName : GLOBALVAR | ATSTRINGCONSTANT;
1100
1101OptGlobalAssign : GlobalName '=' {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001102 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001103 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001104 }
1105 | /*empty*/ {
1106 $$ = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00001107 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001108 };
1109
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001110GVInternalLinkage
1111 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1112 | WEAK { $$ = GlobalValue::WeakLinkage; }
1113 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1114 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1115 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1116 ;
1117
1118GVExternalLinkage
1119 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1120 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1121 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1122 ;
1123
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001124GVVisibilityStyle
1125 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1126 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1127 ;
1128
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001129FunctionDeclareLinkage
1130 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1131 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1132 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001133 ;
1134
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001135FunctionDefineLinkage
1136 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1137 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001138 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1139 | WEAK { $$ = GlobalValue::WeakLinkage; }
1140 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001141 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001142
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +00001143OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1144 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +00001145 FASTCC_TOK { $$ = CallingConv::Fast; } |
1146 COLDCC_TOK { $$ = CallingConv::Cold; } |
1147 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1148 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1149 CC_TOK EUINT64VAL {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001150 if ((unsigned)$2 != $2)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001151 GEN_ERROR("Calling conv too large");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001152 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001153 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001154 };
1155
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001156ParamAttr : ZEXT { $$ = FunctionType::ZExtAttribute; }
1157 | SEXT { $$ = FunctionType::SExtAttribute; }
1158 | INREG { $$ = FunctionType::InRegAttribute; }
1159 | SRET { $$ = FunctionType::StructRetAttribute; }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001160 ;
1161
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001162OptParamAttrs : /* empty */ { $$ = FunctionType::NoAttributeSet; }
1163 | OptParamAttrs ParamAttr {
1164 $$ = FunctionType::ParameterAttributes($1 | $2);
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001165 }
1166 ;
1167
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001168FuncAttr : NORETURN { $$ = FunctionType::NoReturnAttribute; }
1169 | ParamAttr
1170 ;
1171
1172OptFuncAttrs : /* empty */ { $$ = FunctionType::NoAttributeSet; }
1173 | OptFuncAttrs FuncAttr {
1174 $$ = FunctionType::ParameterAttributes($1 | $2);
1175 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001176 ;
1177
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001178// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1179// a comma before it.
1180OptAlign : /*empty*/ { $$ = 0; } |
1181 ALIGN EUINT64VAL {
1182 $$ = $2;
1183 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001184 GEN_ERROR("Alignment must be a power of two");
Reid Spencer713eedc2006-08-18 08:43:06 +00001185 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001186};
1187OptCAlign : /*empty*/ { $$ = 0; } |
1188 ',' ALIGN EUINT64VAL {
1189 $$ = $3;
1190 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001191 GEN_ERROR("Alignment must be a power of two");
Reid Spencer713eedc2006-08-18 08:43:06 +00001192 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001193};
1194
1195
1196SectionString : SECTION STRINGCONSTANT {
1197 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1198 if ($2[i] == '"' || $2[i] == '\\')
Reid Spencera7bb3e92007-02-05 10:18:06 +00001199 GEN_ERROR("Invalid character in section name");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001200 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001201 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001202};
1203
1204OptSection : /*empty*/ { $$ = 0; } |
1205 SectionString { $$ = $1; };
1206
1207// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1208// is set to be the global we are processing.
1209//
1210GlobalVarAttributes : /* empty */ {} |
1211 ',' GlobalVarAttribute GlobalVarAttributes {};
1212GlobalVarAttribute : SectionString {
1213 CurGV->setSection($1);
1214 free($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001215 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001216 }
1217 | ALIGN EUINT64VAL {
1218 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001219 GEN_ERROR("Alignment must be a power of two");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001220 CurGV->setAlignment($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001221 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001222 };
1223
1224//===----------------------------------------------------------------------===//
1225// Types includes all predefined types... except void, because it can only be
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001226// used in specific contexts (function returning void for example).
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001227
1228// Derived types are added later...
1229//
Reid Spencer58a8db02007-01-13 05:00:46 +00001230PrimType : INTTYPE | FLOAT | DOUBLE | LABEL ;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001231
1232Types
1233 : OPAQUE {
Reid Spencere2c32da2006-12-03 05:46:11 +00001234 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer713eedc2006-08-18 08:43:06 +00001235 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001236 }
1237 | PrimType {
Reid Spencere2c32da2006-12-03 05:46:11 +00001238 $$ = new PATypeHolder($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001239 CHECK_FOR_ERROR
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001240 }
1241 | Types '*' { // Pointer type?
1242 if (*$1 == Type::LabelTy)
1243 GEN_ERROR("Cannot form a pointer to a basic block");
1244 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1245 delete $1;
1246 CHECK_FOR_ERROR
1247 }
1248 | SymbolicValueRef { // Named types are also simple types...
1249 const Type* tmp = getTypeVal($1);
1250 CHECK_FOR_ERROR
1251 $$ = new PATypeHolder(tmp);
1252 }
1253 | '\\' EUINT64VAL { // Type UpReference
Reid Spencera7bb3e92007-02-05 10:18:06 +00001254 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001255 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1256 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencere2c32da2006-12-03 05:46:11 +00001257 $$ = new PATypeHolder(OT);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001258 UR_OUT("New Upreference!\n");
Reid Spencer713eedc2006-08-18 08:43:06 +00001259 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001260 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001261 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001262 std::vector<const Type*> Params;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001263 std::vector<FunctionType::ParameterAttributes> Attrs;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001264 Attrs.push_back($5);
1265 for (TypeWithAttrsList::iterator I=$3->begin(), E=$3->end(); I != E; ++I) {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001266 Params.push_back(I->Ty->get());
1267 if (I->Ty->get() != Type::VoidTy)
1268 Attrs.push_back(I->Attrs);
1269 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001270 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1271 if (isVarArg) Params.pop_back();
1272
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001273 FunctionType *FT = FunctionType::get(*$1, Params, isVarArg, Attrs);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001274 delete $3; // Delete the argument list
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001275 delete $1; // Delete the return type handle
1276 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer713eedc2006-08-18 08:43:06 +00001277 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001278 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001279 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001280 std::vector<const Type*> Params;
1281 std::vector<FunctionType::ParameterAttributes> Attrs;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001282 Attrs.push_back($5);
1283 for (TypeWithAttrsList::iterator I=$3->begin(), E=$3->end(); I != E; ++I) {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001284 Params.push_back(I->Ty->get());
1285 if (I->Ty->get() != Type::VoidTy)
1286 Attrs.push_back(I->Attrs);
1287 }
1288 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1289 if (isVarArg) Params.pop_back();
1290
1291 FunctionType *FT = FunctionType::get($1, Params, isVarArg, Attrs);
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001292 delete $3; // Delete the argument list
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001293 $$ = new PATypeHolder(HandleUpRefs(FT));
1294 CHECK_FOR_ERROR
1295 }
1296
1297 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Reid Spencere2c32da2006-12-03 05:46:11 +00001298 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1299 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00001300 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001301 }
Chris Lattner4669b0b2007-02-19 07:44:24 +00001302 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencere2c32da2006-12-03 05:46:11 +00001303 const llvm::Type* ElemTy = $4->get();
1304 if ((unsigned)$2 != $2)
1305 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner03c49532007-01-15 02:27:26 +00001306 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencerd84d35b2007-02-15 02:26:10 +00001307 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencere2c32da2006-12-03 05:46:11 +00001308 if (!isPowerOf2_32($2))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001309 GEN_ERROR("Vector length should be a power of 2");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001310 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencere2c32da2006-12-03 05:46:11 +00001311 delete $4;
1312 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001313 }
1314 | '{' TypeListI '}' { // Structure type?
1315 std::vector<const Type*> Elements;
Reid Spencere2c32da2006-12-03 05:46:11 +00001316 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001317 E = $2->end(); I != E; ++I)
Reid Spencere2c32da2006-12-03 05:46:11 +00001318 Elements.push_back(*I);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001319
Reid Spencere2c32da2006-12-03 05:46:11 +00001320 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001321 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001322 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001323 }
1324 | '{' '}' { // Empty structure type?
Reid Spencere2c32da2006-12-03 05:46:11 +00001325 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer713eedc2006-08-18 08:43:06 +00001326 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001327 }
Andrew Lenharth2d189ff2006-12-08 18:07:09 +00001328 | '<' '{' TypeListI '}' '>' {
1329 std::vector<const Type*> Elements;
1330 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1331 E = $3->end(); I != E; ++I)
1332 Elements.push_back(*I);
1333
1334 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1335 delete $3;
1336 CHECK_FOR_ERROR
1337 }
1338 | '<' '{' '}' '>' { // Empty structure type?
1339 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1340 CHECK_FOR_ERROR
1341 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001342 ;
1343
1344ArgType
1345 : Types OptParamAttrs {
1346 $$.Ty = $1;
1347 $$.Attrs = $2;
1348 }
1349 ;
1350
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001351ResultTypes
1352 : Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001353 if (!UpRefs.empty())
1354 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1355 if (!(*$1)->isFirstClassType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001356 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001357 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001358 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001359 | VOID {
1360 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001361 }
1362 ;
1363
1364ArgTypeList : ArgType {
1365 $$ = new TypeWithAttrsList();
1366 $$->push_back($1);
1367 CHECK_FOR_ERROR
1368 }
1369 | ArgTypeList ',' ArgType {
1370 ($$=$1)->push_back($3);
1371 CHECK_FOR_ERROR
1372 }
1373 ;
1374
1375ArgTypeListI
1376 : ArgTypeList
1377 | ArgTypeList ',' DOTDOTDOT {
1378 $$=$1;
1379 TypeWithAttrs TWA; TWA.Attrs = FunctionType::NoAttributeSet;
1380 TWA.Ty = new PATypeHolder(Type::VoidTy);
1381 $$->push_back(TWA);
1382 CHECK_FOR_ERROR
1383 }
1384 | DOTDOTDOT {
1385 $$ = new TypeWithAttrsList;
1386 TypeWithAttrs TWA; TWA.Attrs = FunctionType::NoAttributeSet;
1387 TWA.Ty = new PATypeHolder(Type::VoidTy);
1388 $$->push_back(TWA);
1389 CHECK_FOR_ERROR
1390 }
1391 | /*empty*/ {
1392 $$ = new TypeWithAttrsList();
Reid Spencer713eedc2006-08-18 08:43:06 +00001393 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001394 };
1395
1396// TypeList - Used for struct declarations and as a basis for function type
1397// declaration type lists
1398//
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001399TypeListI : Types {
Reid Spencere2c32da2006-12-03 05:46:11 +00001400 $$ = new std::list<PATypeHolder>();
1401 $$->push_back(*$1); delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001402 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001403 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001404 | TypeListI ',' Types {
Reid Spencere2c32da2006-12-03 05:46:11 +00001405 ($$=$1)->push_back(*$3); delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001406 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001407 };
1408
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001409// ConstVal - The various declarations that go into the constant pool. This
1410// production is used ONLY to represent constants that show up AFTER a 'const',
1411// 'constant' or 'global' token at global scope. Constants that can be inlined
1412// into other expressions (such as integers and constexprs) are handled by the
1413// ResolvedVal, ValueRef and ConstValueRef productions.
1414//
1415ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001416 if (!UpRefs.empty())
1417 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001418 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001419 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001420 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001421 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001422 const Type *ETy = ATy->getElementType();
1423 int NumElements = ATy->getNumElements();
1424
1425 // Verify that we have the correct size...
1426 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer713eedc2006-08-18 08:43:06 +00001427 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001428 utostr($3->size()) + " arguments, but has size of " +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001429 itostr(NumElements) + "");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001430
1431 // Verify all elements are correct type!
1432 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencere2c32da2006-12-03 05:46:11 +00001433 if (ETy != (*$3)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001434 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001435 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencere2c32da2006-12-03 05:46:11 +00001436 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001437 }
1438
Reid Spencere2c32da2006-12-03 05:46:11 +00001439 $$ = ConstantArray::get(ATy, *$3);
1440 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001441 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001442 }
1443 | Types '[' ']' {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001444 if (!UpRefs.empty())
1445 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001446 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001447 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001448 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001449 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001450
1451 int NumElements = ATy->getNumElements();
1452 if (NumElements != -1 && NumElements != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001453 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencera7bb3e92007-02-05 10:18:06 +00001454 " arguments, but has size of " + itostr(NumElements) +"");
Reid Spencere2c32da2006-12-03 05:46:11 +00001455 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1456 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001457 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001458 }
1459 | Types 'c' STRINGCONSTANT {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001460 if (!UpRefs.empty())
1461 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001462 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001463 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001464 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001465 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001466
1467 int NumElements = ATy->getNumElements();
1468 const Type *ETy = ATy->getElementType();
1469 char *EndStr = UnEscapeLexed($3, true);
1470 if (NumElements != -1 && NumElements != (EndStr-$3))
Reid Spencer713eedc2006-08-18 08:43:06 +00001471 GEN_ERROR("Can't build string constant of size " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001472 itostr((int)(EndStr-$3)) +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001473 " when array has size " + itostr(NumElements) + "");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001474 std::vector<Constant*> Vals;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001475 if (ETy == Type::Int8Ty) {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001476 for (unsigned char *C = (unsigned char *)$3;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001477 C != (unsigned char*)EndStr; ++C)
1478 Vals.push_back(ConstantInt::get(ETy, *C));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001479 } else {
1480 free($3);
Reid Spencera7bb3e92007-02-05 10:18:06 +00001481 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001482 }
1483 free($3);
Reid Spencere2c32da2006-12-03 05:46:11 +00001484 $$ = ConstantArray::get(ATy, Vals);
1485 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001486 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001487 }
1488 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001489 if (!UpRefs.empty())
1490 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001491 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001492 if (PTy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001493 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001494 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001495 const Type *ETy = PTy->getElementType();
1496 int NumElements = PTy->getNumElements();
1497
1498 // Verify that we have the correct size...
1499 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer713eedc2006-08-18 08:43:06 +00001500 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001501 utostr($3->size()) + " arguments, but has size of " +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001502 itostr(NumElements) + "");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001503
1504 // Verify all elements are correct type!
1505 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencere2c32da2006-12-03 05:46:11 +00001506 if (ETy != (*$3)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001507 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001508 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencere2c32da2006-12-03 05:46:11 +00001509 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001510 }
1511
Reid Spencerd84d35b2007-02-15 02:26:10 +00001512 $$ = ConstantVector::get(PTy, *$3);
Reid Spencere2c32da2006-12-03 05:46:11 +00001513 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001514 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001515 }
1516 | Types '{' ConstVector '}' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001517 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001518 if (STy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001519 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001520 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001521
1522 if ($3->size() != STy->getNumContainedTypes())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001523 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001524
1525 // Check to ensure that constants are compatible with the type initializer!
1526 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencere2c32da2006-12-03 05:46:11 +00001527 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer713eedc2006-08-18 08:43:06 +00001528 GEN_ERROR("Expected type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001529 STy->getElementType(i)->getDescription() +
1530 "' for element #" + utostr(i) +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001531 " of structure initializer");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001532
Zhou Sheng75b871f2007-01-11 12:24:14 +00001533 // Check to ensure that Type is not packed
1534 if (STy->isPacked())
Chris Lattner4669b0b2007-02-19 07:44:24 +00001535 GEN_ERROR("Unpacked Initializer to vector type '" + STy->getDescription() + "'");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001536
Reid Spencere2c32da2006-12-03 05:46:11 +00001537 $$ = ConstantStruct::get(STy, *$3);
1538 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001539 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001540 }
1541 | Types '{' '}' {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001542 if (!UpRefs.empty())
1543 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001544 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001545 if (STy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001546 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001547 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001548
1549 if (STy->getNumContainedTypes() != 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001550 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001551
Zhou Sheng75b871f2007-01-11 12:24:14 +00001552 // Check to ensure that Type is not packed
1553 if (STy->isPacked())
Chris Lattner4669b0b2007-02-19 07:44:24 +00001554 GEN_ERROR("Unpacked Initializer to vector type '" + STy->getDescription() + "'");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001555
1556 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1557 delete $1;
1558 CHECK_FOR_ERROR
1559 }
1560 | Types '<' '{' ConstVector '}' '>' {
1561 const StructType *STy = dyn_cast<StructType>($1->get());
1562 if (STy == 0)
1563 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001564 (*$1)->getDescription() + "'");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001565
1566 if ($4->size() != STy->getNumContainedTypes())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001567 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001568
1569 // Check to ensure that constants are compatible with the type initializer!
1570 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1571 if ((*$4)[i]->getType() != STy->getElementType(i))
1572 GEN_ERROR("Expected type '" +
1573 STy->getElementType(i)->getDescription() +
1574 "' for element #" + utostr(i) +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001575 " of structure initializer");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001576
1577 // Check to ensure that Type is packed
1578 if (!STy->isPacked())
Chris Lattner4669b0b2007-02-19 07:44:24 +00001579 GEN_ERROR("Vector initializer to non-vector type '" +
1580 STy->getDescription() + "'");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001581
1582 $$ = ConstantStruct::get(STy, *$4);
1583 delete $1; delete $4;
1584 CHECK_FOR_ERROR
1585 }
1586 | Types '<' '{' '}' '>' {
1587 if (!UpRefs.empty())
1588 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1589 const StructType *STy = dyn_cast<StructType>($1->get());
1590 if (STy == 0)
1591 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001592 (*$1)->getDescription() + "'");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001593
1594 if (STy->getNumContainedTypes() != 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001595 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001596
1597 // Check to ensure that Type is packed
1598 if (!STy->isPacked())
Chris Lattner4669b0b2007-02-19 07:44:24 +00001599 GEN_ERROR("Vector initializer to non-vector type '" +
1600 STy->getDescription() + "'");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001601
Reid Spencere2c32da2006-12-03 05:46:11 +00001602 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1603 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001604 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001605 }
1606 | Types NULL_TOK {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001607 if (!UpRefs.empty())
1608 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001609 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001610 if (PTy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001611 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001612 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001613
Reid Spencere2c32da2006-12-03 05:46:11 +00001614 $$ = ConstantPointerNull::get(PTy);
1615 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001616 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001617 }
1618 | Types UNDEF {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001619 if (!UpRefs.empty())
1620 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001621 $$ = UndefValue::get($1->get());
1622 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001623 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001624 }
1625 | Types SymbolicValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001626 if (!UpRefs.empty())
1627 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001628 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001629 if (Ty == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001630 GEN_ERROR("Global const reference must be a pointer type");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001631
1632 // ConstExprs can exist in the body of a function, thus creating
1633 // GlobalValues whenever they refer to a variable. Because we are in
1634 // the context of a function, getValNonImprovising will search the functions
1635 // symbol table instead of the module symbol table for the global symbol,
1636 // which throws things all off. To get around this, we just tell
1637 // getValNonImprovising that we are at global scope here.
1638 //
1639 Function *SavedCurFn = CurFun.CurrentFunction;
1640 CurFun.CurrentFunction = 0;
1641
1642 Value *V = getValNonImprovising(Ty, $2);
Reid Spencer309080a2006-09-28 19:28:24 +00001643 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001644
1645 CurFun.CurrentFunction = SavedCurFn;
1646
1647 // If this is an initializer for a constant pointer, which is referencing a
1648 // (currently) undefined variable, create a stub now that shall be replaced
1649 // in the future with the right type of variable.
1650 //
1651 if (V == 0) {
Reid Spencerac6bfc52007-02-05 17:04:00 +00001652 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001653 const PointerType *PT = cast<PointerType>(Ty);
1654
1655 // First check to see if the forward references value is already created!
1656 PerModuleInfo::GlobalRefsType::iterator I =
1657 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1658
1659 if (I != CurModule.GlobalRefs.end()) {
1660 V = I->second; // Placeholder already exists, use it...
1661 $2.destroy();
1662 } else {
1663 std::string Name;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001664 if ($2.Type == ValID::GlobalName)
1665 Name = $2.Name;
1666 else if ($2.Type != ValID::GlobalID)
1667 GEN_ERROR("Invalid reference to global");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001668
1669 // Create the forward referenced global.
1670 GlobalValue *GV;
1671 if (const FunctionType *FTy =
1672 dyn_cast<FunctionType>(PT->getElementType())) {
1673 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1674 CurModule.CurrentModule);
1675 } else {
1676 GV = new GlobalVariable(PT->getElementType(), false,
1677 GlobalValue::ExternalLinkage, 0,
1678 Name, CurModule.CurrentModule);
1679 }
1680
1681 // Keep track of the fact that we have a forward ref to recycle it
1682 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1683 V = GV;
1684 }
1685 }
1686
Reid Spencere2c32da2006-12-03 05:46:11 +00001687 $$ = cast<GlobalValue>(V);
1688 delete $1; // Free the type handle
Reid Spencer713eedc2006-08-18 08:43:06 +00001689 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001690 }
1691 | Types ConstExpr {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001692 if (!UpRefs.empty())
1693 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001694 if ($1->get() != $2->getType())
Reid Spencerca3d1cb2007-01-04 00:06:14 +00001695 GEN_ERROR("Mismatched types for constant expression: " +
1696 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001697 $$ = $2;
Reid Spencere2c32da2006-12-03 05:46:11 +00001698 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001699 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001700 }
1701 | Types ZEROINITIALIZER {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001702 if (!UpRefs.empty())
1703 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001704 const Type *Ty = $1->get();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001705 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001706 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencere2c32da2006-12-03 05:46:11 +00001707 $$ = Constant::getNullValue(Ty);
1708 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001709 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00001710 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001711 | IntType ESINT64VAL { // integral constants
Reid Spencer266e42b2006-12-23 06:05:41 +00001712 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001713 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencerc7a686b2007-02-28 02:24:54 +00001714 APInt Val(64, $2);
1715 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1716 if (BitWidth > 64)
1717 Val.sext(BitWidth);
1718 else if (BitWidth < 64)
1719 Val.trunc(BitWidth);
1720 $$ = ConstantInt::get($1, Val);
1721 CHECK_FOR_ERROR
1722 }
1723 | IntType ESAPINTVAL { // arbitrary precision integer constants
1724 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1725 if ($2->getBitWidth() > BitWidth) {
1726 GEN_ERROR("Constant value does not fit in type");
1727 } else if ($2->getBitWidth() < BitWidth)
1728 $2->sext(BitWidth);
1729 else if ($2->getBitWidth() > BitWidth)
1730 $2->trunc(BitWidth);
1731 $$ = ConstantInt::get($1, *$2);
1732 delete $2;
Reid Spencer266e42b2006-12-23 06:05:41 +00001733 CHECK_FOR_ERROR
1734 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001735 | IntType EUINT64VAL { // integral constants
Reid Spencer266e42b2006-12-23 06:05:41 +00001736 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001737 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencerc7a686b2007-02-28 02:24:54 +00001738 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1739 APInt Val(BitWidth, $2);
1740 $$ = ConstantInt::get($1, Val);
1741 CHECK_FOR_ERROR
1742 }
1743 | IntType EUAPINTVAL { // arbitrary precision integer constants
1744 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1745 if ($2->getBitWidth() > BitWidth) {
1746 GEN_ERROR("Constant value does not fit in type");
1747 } else if ($2->getBitWidth() < BitWidth)
1748 $2->zext(BitWidth);
1749 else if ($2->getBitWidth() > BitWidth)
1750 $2->trunc(BitWidth);
1751 $$ = ConstantInt::get($1, *$2);
1752 delete $2;
Reid Spencer266e42b2006-12-23 06:05:41 +00001753 CHECK_FOR_ERROR
1754 }
Reid Spencer58a8db02007-01-13 05:00:46 +00001755 | INTTYPE TRUETOK { // Boolean constants
1756 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001757 $$ = ConstantInt::getTrue();
Reid Spencer713eedc2006-08-18 08:43:06 +00001758 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001759 }
Reid Spencer58a8db02007-01-13 05:00:46 +00001760 | INTTYPE FALSETOK { // Boolean constants
1761 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001762 $$ = ConstantInt::getFalse();
Reid Spencer713eedc2006-08-18 08:43:06 +00001763 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001764 }
1765 | FPType FPVAL { // Float & Double constants
Reid Spencere2c32da2006-12-03 05:46:11 +00001766 if (!ConstantFP::isValueValidForType($1, $2))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001767 GEN_ERROR("Floating point constant invalid for type");
Reid Spencere2c32da2006-12-03 05:46:11 +00001768 $$ = ConstantFP::get($1, $2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001769 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001770 };
1771
1772
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001773ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001774 if (!UpRefs.empty())
1775 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001776 Constant *Val = $3;
Reid Spencerceb84592007-01-17 02:48:45 +00001777 const Type *DestTy = $5->get();
1778 if (!CastInst::castIsValid($1, $3, DestTy))
1779 GEN_ERROR("invalid cast opcode for cast from '" +
1780 Val->getType()->getDescription() + "' to '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001781 DestTy->getDescription() + "'");
Reid Spencerceb84592007-01-17 02:48:45 +00001782 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencere2c32da2006-12-03 05:46:11 +00001783 delete $5;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001784 }
1785 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001786 if (!isa<PointerType>($3->getType()))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001787 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001788
Reid Spencere2c32da2006-12-03 05:46:11 +00001789 const Type *IdxTy =
Chris Lattnerf79508f2007-02-13 00:58:01 +00001790 GetElementPtrInst::getIndexedType($3->getType(), &(*$4)[0], $4->size(),
1791 true);
Reid Spencere2c32da2006-12-03 05:46:11 +00001792 if (!IdxTy)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001793 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencere2c32da2006-12-03 05:46:11 +00001794
Chris Lattner55ce85c2007-01-31 04:44:08 +00001795 SmallVector<Constant*, 8> IdxVec;
Reid Spencere2c32da2006-12-03 05:46:11 +00001796 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1797 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001798 IdxVec.push_back(C);
1799 else
Reid Spencera7bb3e92007-02-05 10:18:06 +00001800 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001801
1802 delete $4;
1803
Chris Lattner55ce85c2007-01-31 04:44:08 +00001804 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer713eedc2006-08-18 08:43:06 +00001805 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001806 }
1807 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer542964f2007-01-11 18:21:29 +00001808 if ($3->getType() != Type::Int1Ty)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001809 GEN_ERROR("Select condition must be of boolean type");
Reid Spencere2c32da2006-12-03 05:46:11 +00001810 if ($5->getType() != $7->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001811 GEN_ERROR("Select operand types must match");
Reid Spencere2c32da2006-12-03 05:46:11 +00001812 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001813 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001814 }
1815 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001816 if ($3->getType() != $5->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001817 GEN_ERROR("Binary operator types must match");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001818 CHECK_FOR_ERROR;
Reid Spencer844668d2006-12-05 19:16:11 +00001819 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001820 }
1821 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001822 if ($3->getType() != $5->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001823 GEN_ERROR("Logical operator types must match");
Chris Lattner03c49532007-01-15 02:27:26 +00001824 if (!$3->getType()->isInteger()) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001825 if (Instruction::isShift($1) || !isa<VectorType>($3->getType()) ||
1826 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001827 GEN_ERROR("Logical operator requires integral operands");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001828 }
Reid Spencere2c32da2006-12-03 05:46:11 +00001829 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001830 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001831 }
Reid Spencerd2e0c342006-12-04 05:24:24 +00001832 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1833 if ($4->getType() != $6->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001834 GEN_ERROR("icmp operand types must match");
Reid Spencerd2e0c342006-12-04 05:24:24 +00001835 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencere2c32da2006-12-03 05:46:11 +00001836 }
Reid Spencerd2e0c342006-12-04 05:24:24 +00001837 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1838 if ($4->getType() != $6->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001839 GEN_ERROR("fcmp operand types must match");
Reid Spencerd2e0c342006-12-04 05:24:24 +00001840 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencere2c32da2006-12-03 05:46:11 +00001841 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001842 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001843 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001844 GEN_ERROR("Invalid extractelement operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00001845 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001846 CHECK_FOR_ERROR
Chris Lattneraebccf82006-04-08 03:55:17 +00001847 }
1848 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001849 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001850 GEN_ERROR("Invalid insertelement operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00001851 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001852 CHECK_FOR_ERROR
Chris Lattneraebccf82006-04-08 03:55:17 +00001853 }
1854 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001855 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001856 GEN_ERROR("Invalid shufflevector operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00001857 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001858 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001859 };
1860
Chris Lattneraebccf82006-04-08 03:55:17 +00001861
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001862// ConstVector - A list of comma separated constants.
1863ConstVector : ConstVector ',' ConstVal {
1864 ($$ = $1)->push_back($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001865 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001866 }
1867 | ConstVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00001868 $$ = new std::vector<Constant*>();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001869 $$->push_back($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001870 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001871 };
1872
1873
1874// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1875GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
1876
1877
1878//===----------------------------------------------------------------------===//
1879// Rules to match Modules
1880//===----------------------------------------------------------------------===//
1881
1882// Module rule: Capture the result of parsing the whole file into a result
1883// variable...
1884//
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001885Module
1886 : DefinitionList {
1887 $$ = ParserResult = CurModule.CurrentModule;
1888 CurModule.ModuleDone();
1889 CHECK_FOR_ERROR;
1890 }
1891 | /*empty*/ {
1892 $$ = ParserResult = CurModule.CurrentModule;
1893 CurModule.ModuleDone();
1894 CHECK_FOR_ERROR;
1895 }
1896 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001897
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001898DefinitionList
1899 : Definition
1900 | DefinitionList Definition
1901 ;
1902
1903Definition
Jeff Cohen5d956e42007-01-21 19:19:31 +00001904 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001905 CurFun.FunctionDone();
Reid Spencer713eedc2006-08-18 08:43:06 +00001906 CHECK_FOR_ERROR
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001907 }
1908 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer713eedc2006-08-18 08:43:06 +00001909 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001910 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001911 | MODULE ASM_TOK AsmBlock {
Reid Spencer713eedc2006-08-18 08:43:06 +00001912 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001913 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001914 | IMPLEMENTATION {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001915 // Emit an error if there are any unresolved types left.
1916 if (!CurModule.LateResolveTypes.empty()) {
1917 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001918 if (DID.Type == ValID::LocalName) {
Reid Spencer713eedc2006-08-18 08:43:06 +00001919 GEN_ERROR("Reference to an undefined type: '"+DID.getName() + "'");
1920 } else {
1921 GEN_ERROR("Reference to an undefined type: #" + itostr(DID.Num));
1922 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001923 }
Reid Spencer713eedc2006-08-18 08:43:06 +00001924 CHECK_FOR_ERROR
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001925 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001926 | OptLocalAssign TYPE Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001927 if (!UpRefs.empty())
1928 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001929 // Eagerly resolve types. This is not an optimization, this is a
1930 // requirement that is due to the fact that we could have this:
1931 //
1932 // %list = type { %list * }
1933 // %list = type { %list * } ; repeated type decl
1934 //
1935 // If types are not resolved eagerly, then the two types will not be
1936 // determined to be the same type!
1937 //
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001938 ResolveTypeTo($1, *$3);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001939
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001940 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer309080a2006-09-28 19:28:24 +00001941 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001942 // If this is a named type that is not a redefinition, add it to the slot
1943 // table.
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001944 CurModule.Types.push_back(*$3);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001945 }
Reid Spencere2c32da2006-12-03 05:46:11 +00001946
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001947 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001948 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001949 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001950 | OptLocalAssign TYPE VOID {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001951 ResolveTypeTo($1, $3);
1952
1953 if (!setTypeName($3, $1) && !$1) {
1954 CHECK_FOR_ERROR
1955 // If this is a named type that is not a redefinition, add it to the slot
1956 // table.
1957 CurModule.Types.push_back($3);
1958 }
1959 CHECK_FOR_ERROR
1960 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001961 | OptGlobalAssign GVVisibilityStyle GlobalType ConstVal {
1962 /* "Externally Visible" Linkage */
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001963 if ($4 == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001964 GEN_ERROR("Global value initializer is not a constant");
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001965 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
1966 $2, $3, $4->getType(), $4);
Reid Spencer309080a2006-09-28 19:28:24 +00001967 CHECK_FOR_ERROR
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001968 } GlobalVarAttributes {
1969 CurGV = 0;
1970 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001971 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle GlobalType ConstVal {
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001972 if ($5 == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001973 GEN_ERROR("Global value initializer is not a constant");
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001974 CurGV = ParseGlobalVariable($1, $2, $3, $4, $5->getType(), $5);
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001975 CHECK_FOR_ERROR
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001976 } GlobalVarAttributes {
1977 CurGV = 0;
1978 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001979 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle GlobalType Types {
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001980 if (!UpRefs.empty())
1981 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
1982 CurGV = ParseGlobalVariable($1, $2, $3, $4, *$5, 0);
1983 CHECK_FOR_ERROR
1984 delete $5;
Reid Spencer309080a2006-09-28 19:28:24 +00001985 } GlobalVarAttributes {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001986 CurGV = 0;
1987 CHECK_FOR_ERROR
1988 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001989 | TARGET TargetDefinition {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001990 CHECK_FOR_ERROR
1991 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001992 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer713eedc2006-08-18 08:43:06 +00001993 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001994 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001995 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001996
1997
1998AsmBlock : STRINGCONSTANT {
1999 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
2000 char *EndStr = UnEscapeLexed($1, true);
2001 std::string NewAsm($1, EndStr);
2002 free($1);
2003
2004 if (AsmSoFar.empty())
2005 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
2006 else
2007 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
Reid Spencer713eedc2006-08-18 08:43:06 +00002008 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002009};
2010
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002011TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002012 CurModule.CurrentModule->setTargetTriple($3);
2013 free($3);
John Criswell6af0b122006-10-24 19:09:48 +00002014 }
Chris Lattner7d1d0342006-10-22 06:08:13 +00002015 | DATALAYOUT '=' STRINGCONSTANT {
Owen Anderson85690f32006-10-18 02:21:48 +00002016 CurModule.CurrentModule->setDataLayout($3);
2017 free($3);
Owen Anderson85690f32006-10-18 02:21:48 +00002018 };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002019
2020LibrariesDefinition : '[' LibList ']';
2021
2022LibList : LibList ',' STRINGCONSTANT {
2023 CurModule.CurrentModule->addLibrary($3);
2024 free($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002025 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002026 }
2027 | STRINGCONSTANT {
2028 CurModule.CurrentModule->addLibrary($1);
2029 free($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002030 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002031 }
2032 | /* empty: end of list */ {
Reid Spencer713eedc2006-08-18 08:43:06 +00002033 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002034 }
2035 ;
2036
2037//===----------------------------------------------------------------------===//
2038// Rules to match Function Headers
2039//===----------------------------------------------------------------------===//
2040
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002041ArgListH : ArgListH ',' Types OptParamAttrs OptLocalName {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002042 if (!UpRefs.empty())
2043 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
2044 if (*$3 == Type::VoidTy)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002045 GEN_ERROR("void typed arguments are invalid");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002046 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002047 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002048 $1->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002049 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002050 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002051 | Types OptParamAttrs OptLocalName {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002052 if (!UpRefs.empty())
2053 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2054 if (*$1 == Type::VoidTy)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002055 GEN_ERROR("void typed arguments are invalid");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002056 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2057 $$ = new ArgListType;
2058 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002059 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002060 };
2061
2062ArgList : ArgListH {
2063 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002064 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002065 }
2066 | ArgListH ',' DOTDOTDOT {
2067 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002068 struct ArgListEntry E;
2069 E.Ty = new PATypeHolder(Type::VoidTy);
2070 E.Name = 0;
2071 E.Attrs = FunctionType::NoAttributeSet;
2072 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002073 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002074 }
2075 | DOTDOTDOT {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002076 $$ = new ArgListType;
2077 struct ArgListEntry E;
2078 E.Ty = new PATypeHolder(Type::VoidTy);
2079 E.Name = 0;
2080 E.Attrs = FunctionType::NoAttributeSet;
2081 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002082 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002083 }
2084 | /* empty */ {
2085 $$ = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00002086 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002087 };
2088
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002089FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002090 OptFuncAttrs OptSection OptAlign {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002091 UnEscapeLexed($3);
2092 std::string FunctionName($3);
2093 free($3); // Free strdup'd memory!
2094
Reid Spencer87622ae2007-01-02 21:54:12 +00002095 // Check the function result for abstractness if this is a define. We should
2096 // have no abstract types at this point
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002097 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2098 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer87622ae2007-01-02 21:54:12 +00002099
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002100 std::vector<const Type*> ParamTypeList;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002101 std::vector<FunctionType::ParameterAttributes> ParamAttrs;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002102 ParamAttrs.push_back($7);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002103 if ($5) { // If there are arguments...
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002104 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I) {
2105 const Type* Ty = I->Ty->get();
Reid Spencer87622ae2007-01-02 21:54:12 +00002106 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2107 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002108 ParamTypeList.push_back(Ty);
2109 if (Ty != Type::VoidTy)
2110 ParamAttrs.push_back(I->Attrs);
2111 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002112 }
2113
2114 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2115 if (isVarArg) ParamTypeList.pop_back();
2116
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002117 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg,
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002118 ParamAttrs);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002119 const PointerType *PFT = PointerType::get(FT);
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002120 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002121
2122 ValID ID;
2123 if (!FunctionName.empty()) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002124 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002125 } else {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002126 ID = ValID::createGlobalID(CurModule.Values[PFT].size());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002127 }
2128
2129 Function *Fn = 0;
2130 // See if this function was forward referenced. If so, recycle the object.
2131 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2132 // Move the function to the end of the list, from whereever it was
2133 // previously inserted.
2134 Fn = cast<Function>(FWRef);
2135 CurModule.CurrentModule->getFunctionList().remove(Fn);
2136 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2137 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002138 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
2139 if (Fn->getFunctionType() != FT ) {
2140 // The existing function doesn't have the same type. This is an overload
2141 // error.
2142 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
2143 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
2144 // Neither the existing or the current function is a declaration and they
2145 // have the same name and same type. Clearly this is a redefinition.
Reid Spencera7bb3e92007-02-05 10:18:06 +00002146 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002147 } if (Fn->isDeclaration()) {
2148 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002149 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2150 AI != AE; ++AI)
2151 AI->setName("");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002152 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002153 } else { // Not already defined?
2154 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
2155 CurModule.CurrentModule);
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002156
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002157 InsertValue(Fn, CurModule.Values);
2158 }
2159
2160 CurFun.FunctionStart(Fn);
Anton Korobeynikov0ab01ff2006-09-17 13:06:18 +00002161
2162 if (CurFun.isDeclare) {
2163 // If we have declaration, always overwrite linkage. This will allow us to
2164 // correctly handle cases, when pointer to function is passed as argument to
2165 // another function.
2166 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002167 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov0ab01ff2006-09-17 13:06:18 +00002168 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002169 Fn->setCallingConv($1);
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002170 Fn->setAlignment($9);
2171 if ($8) {
2172 Fn->setSection($8);
2173 free($8);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002174 }
2175
2176 // Add all of the arguments we parsed to the function...
2177 if ($5) { // Is null if empty...
2178 if (isVarArg) { // Nuke the last entry
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002179 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0 &&
Reid Spencerac6bfc52007-02-05 17:04:00 +00002180 "Not a varargs marker!");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002181 delete $5->back().Ty;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002182 $5->pop_back(); // Delete the last entry
2183 }
2184 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002185 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002186 unsigned Idx = 1;
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002187 for (ArgListType::iterator I = $5->begin();
2188 I != $5->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002189 delete I->Ty; // Delete the typeholder...
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002190 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer309080a2006-09-28 19:28:24 +00002191 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002192 InsertValue(ArgIt);
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002193 Idx++;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002194 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002195
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002196 delete $5; // We're now done with the argument list
2197 }
Reid Spencer713eedc2006-08-18 08:43:06 +00002198 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002199};
2200
2201BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2202
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002203FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002204 $$ = CurFun.CurrentFunction;
2205
2206 // Make sure that we keep track of the linkage type even if there was a
2207 // previous "declare".
2208 $$->setLinkage($1);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002209 $$->setVisibility($2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002210};
2211
2212END : ENDTOK | '}'; // Allow end of '}' to end a function
2213
2214Function : BasicBlockList END {
2215 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002216 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002217};
2218
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002219FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002220 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002221 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002222 $$ = CurFun.CurrentFunction;
2223 CurFun.FunctionDone();
2224 CHECK_FOR_ERROR
2225 };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002226
2227//===----------------------------------------------------------------------===//
2228// Rules to match Basic Blocks
2229//===----------------------------------------------------------------------===//
2230
2231OptSideEffect : /* empty */ {
2232 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002233 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002234 }
2235 | SIDEEFFECT {
2236 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002237 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002238 };
2239
2240ConstValueRef : ESINT64VAL { // A reference to a direct constant
2241 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002242 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002243 }
2244 | EUINT64VAL {
2245 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002246 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002247 }
2248 | FPVAL { // Perhaps it's an FP constant?
2249 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002250 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002251 }
2252 | TRUETOK {
Zhou Sheng75b871f2007-01-11 12:24:14 +00002253 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer713eedc2006-08-18 08:43:06 +00002254 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002255 }
2256 | FALSETOK {
Zhou Sheng75b871f2007-01-11 12:24:14 +00002257 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer713eedc2006-08-18 08:43:06 +00002258 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002259 }
2260 | NULL_TOK {
2261 $$ = ValID::createNull();
Reid Spencer713eedc2006-08-18 08:43:06 +00002262 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002263 }
2264 | UNDEF {
2265 $$ = ValID::createUndef();
Reid Spencer713eedc2006-08-18 08:43:06 +00002266 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002267 }
2268 | ZEROINITIALIZER { // A vector zero constant.
2269 $$ = ValID::createZeroInit();
Reid Spencer713eedc2006-08-18 08:43:06 +00002270 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002271 }
2272 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencere2c32da2006-12-03 05:46:11 +00002273 const Type *ETy = (*$2)[0]->getType();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002274 int NumElements = $2->size();
2275
Reid Spencerd84d35b2007-02-15 02:26:10 +00002276 VectorType* pt = VectorType::get(ETy, NumElements);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002277 PATypeHolder* PTy = new PATypeHolder(
Reid Spencere2c32da2006-12-03 05:46:11 +00002278 HandleUpRefs(
Reid Spencerd84d35b2007-02-15 02:26:10 +00002279 VectorType::get(
Reid Spencere2c32da2006-12-03 05:46:11 +00002280 ETy,
2281 NumElements)
2282 )
2283 );
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002284
2285 // Verify all elements are correct type!
2286 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencere2c32da2006-12-03 05:46:11 +00002287 if (ETy != (*$2)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002288 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002289 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00002290 (*$2)[i]->getType()->getDescription() + "'.");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002291 }
2292
Reid Spencerd84d35b2007-02-15 02:26:10 +00002293 $$ = ValID::create(ConstantVector::get(pt, *$2));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002294 delete PTy; delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002295 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002296 }
2297 | ConstExpr {
Reid Spencere2c32da2006-12-03 05:46:11 +00002298 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002299 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002300 }
2301 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
2302 char *End = UnEscapeLexed($3, true);
2303 std::string AsmStr = std::string($3, End);
2304 End = UnEscapeLexed($5, true);
2305 std::string Constraints = std::string($5, End);
2306 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
2307 free($3);
2308 free($5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002309 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002310 };
2311
2312// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2313// another value.
2314//
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002315SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2316 $$ = ValID::createLocalID($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002317 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002318 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002319 | GLOBALVAL_ID {
2320 $$ = ValID::createGlobalID($1);
2321 CHECK_FOR_ERROR
2322 }
2323 | LocalName { // Is it a named reference...?
2324 $$ = ValID::createLocalName($1);
2325 CHECK_FOR_ERROR
2326 }
2327 | GlobalName { // Is it a named reference...?
2328 $$ = ValID::createGlobalName($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002329 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002330 };
2331
2332// ValueRef - A reference to a definition... either constant or symbolic
2333ValueRef : SymbolicValueRef | ConstValueRef;
2334
2335
2336// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2337// type immediately preceeds the value reference, and allows complex constant
2338// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2339ResolvedVal : Types ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002340 if (!UpRefs.empty())
2341 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2342 $$ = getVal(*$1, $2);
2343 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002344 CHECK_FOR_ERROR
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002345 }
2346 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002347
2348BasicBlockList : BasicBlockList BasicBlock {
2349 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002350 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002351 }
2352 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
2353 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002354 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002355 };
2356
2357
2358// Basic blocks are terminated by branching instructions:
2359// br, br/cc, switch, ret
2360//
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002361BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002362 setValueName($3, $2);
Reid Spencer309080a2006-09-28 19:28:24 +00002363 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002364 InsertValue($3);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002365 $1->getInstList().push_back($3);
2366 InsertValue($1);
2367 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002368 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002369 };
2370
2371InstructionList : InstructionList Inst {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002372 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2373 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2374 if (CI2->getParent() == 0)
2375 $1->getInstList().push_back(CI2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002376 $1->getInstList().push_back($2);
2377 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002378 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002379 }
2380 | /* empty */ {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002381 $$ = getBBVal(ValID::createLocalID(CurFun.NextBBNum++), true);
Reid Spencer309080a2006-09-28 19:28:24 +00002382 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002383
2384 // Make sure to move the basic block to the correct location in the
2385 // function, instead of leaving it inserted wherever it was first
2386 // referenced.
2387 Function::BasicBlockListType &BBL =
2388 CurFun.CurrentFunction->getBasicBlockList();
2389 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer713eedc2006-08-18 08:43:06 +00002390 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002391 }
2392 | LABELSTR {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002393 $$ = getBBVal(ValID::createLocalName($1), true);
Reid Spencer309080a2006-09-28 19:28:24 +00002394 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002395
2396 // Make sure to move the basic block to the correct location in the
2397 // function, instead of leaving it inserted wherever it was first
2398 // referenced.
2399 Function::BasicBlockListType &BBL =
2400 CurFun.CurrentFunction->getBasicBlockList();
2401 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer713eedc2006-08-18 08:43:06 +00002402 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002403 };
2404
2405BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencere2c32da2006-12-03 05:46:11 +00002406 $$ = new ReturnInst($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00002407 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002408 }
2409 | RET VOID { // Return with no result...
2410 $$ = new ReturnInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002411 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002412 }
2413 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer309080a2006-09-28 19:28:24 +00002414 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002415 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002416 $$ = new BranchInst(tmpBB);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002417 } // Conditional Branch...
Reid Spencer58a8db02007-01-13 05:00:46 +00002418 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
2419 assert(cast<IntegerType>($2)->getBitWidth() == 1 && "Not Bool?");
Reid Spencer309080a2006-09-28 19:28:24 +00002420 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002421 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002422 BasicBlock* tmpBBB = getBBVal($9);
2423 CHECK_FOR_ERROR
Reid Spencer542964f2007-01-11 18:21:29 +00002424 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002425 CHECK_FOR_ERROR
2426 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002427 }
2428 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencere2c32da2006-12-03 05:46:11 +00002429 Value* tmpVal = getVal($2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002430 CHECK_FOR_ERROR
2431 BasicBlock* tmpBB = getBBVal($6);
2432 CHECK_FOR_ERROR
2433 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002434 $$ = S;
2435
2436 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2437 E = $8->end();
2438 for (; I != E; ++I) {
2439 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2440 S->addCase(CI, I->second);
2441 else
Reid Spencera7bb3e92007-02-05 10:18:06 +00002442 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002443 }
2444 delete $8;
Reid Spencer713eedc2006-08-18 08:43:06 +00002445 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002446 }
2447 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencere2c32da2006-12-03 05:46:11 +00002448 Value* tmpVal = getVal($2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002449 CHECK_FOR_ERROR
2450 BasicBlock* tmpBB = getBBVal($6);
2451 CHECK_FOR_ERROR
2452 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002453 $$ = S;
Reid Spencer713eedc2006-08-18 08:43:06 +00002454 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002455 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002456 | INVOKE OptCallingConv ResultTypes ValueRef '(' ValueRefList ')' OptFuncAttrs
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002457 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002458
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002459 // Handle the short syntax
2460 const PointerType *PFTy = 0;
2461 const FunctionType *Ty = 0;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002462 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002463 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2464 // Pull out the types of all of the arguments...
2465 std::vector<const Type*> ParamTypes;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002466 FunctionType::ParamAttrsList ParamAttrs;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002467 ParamAttrs.push_back($8);
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002468 for (ValueRefList::iterator I = $6->begin(), E = $6->end(); I != E; ++I) {
2469 const Type *Ty = I->Val->getType();
2470 if (Ty == Type::VoidTy)
2471 GEN_ERROR("Short call syntax cannot be used with varargs");
2472 ParamTypes.push_back(Ty);
2473 ParamAttrs.push_back(I->Attrs);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002474 }
2475
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002476 Ty = FunctionType::get($3->get(), ParamTypes, false, ParamAttrs);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002477 PFTy = PointerType::get(Ty);
2478 }
2479
2480 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer309080a2006-09-28 19:28:24 +00002481 CHECK_FOR_ERROR
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002482 BasicBlock *Normal = getBBVal($11);
Reid Spencer309080a2006-09-28 19:28:24 +00002483 CHECK_FOR_ERROR
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002484 BasicBlock *Except = getBBVal($14);
Reid Spencer309080a2006-09-28 19:28:24 +00002485 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002486
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002487 // Check the arguments
2488 ValueList Args;
2489 if ($6->empty()) { // Has no arguments?
2490 // Make sure no arguments is a good thing!
2491 if (Ty->getNumParams() != 0)
2492 GEN_ERROR("No arguments passed to a function that "
Reid Spencera7bb3e92007-02-05 10:18:06 +00002493 "expects arguments");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002494 } else { // Has arguments?
2495 // Loop through FunctionType's arguments and ensure they are specified
2496 // correctly!
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002497 FunctionType::param_iterator I = Ty->param_begin();
2498 FunctionType::param_iterator E = Ty->param_end();
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002499 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002500
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002501 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2502 if (ArgI->Val->getType() != *I)
2503 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00002504 (*I)->getDescription() + "'");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002505 Args.push_back(ArgI->Val);
2506 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002507
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002508 if (Ty->isVarArg()) {
2509 if (I == E)
2510 for (; ArgI != ArgE; ++ArgI)
2511 Args.push_back(ArgI->Val); // push the remaining varargs
2512 } else if (I != E || ArgI != ArgE)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002513 GEN_ERROR("Invalid number of parameters detected");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002514 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002515
2516 // Create the InvokeInst
Chris Lattnerd8028242007-02-13 05:53:56 +00002517 InvokeInst *II = new InvokeInst(V, Normal, Except, &Args[0], Args.size());
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002518 II->setCallingConv($2);
2519 $$ = II;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002520 delete $6;
Reid Spencer713eedc2006-08-18 08:43:06 +00002521 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002522 }
2523 | UNWIND {
2524 $$ = new UnwindInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002525 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002526 }
2527 | UNREACHABLE {
2528 $$ = new UnreachableInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002529 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002530 };
2531
2532
2533
2534JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2535 $$ = $1;
Reid Spencere2c32da2006-12-03 05:46:11 +00002536 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Reid Spencer309080a2006-09-28 19:28:24 +00002537 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002538 if (V == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002539 GEN_ERROR("May only switch on a constant pool value");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002540
Reid Spencer309080a2006-09-28 19:28:24 +00002541 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002542 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002543 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002544 }
2545 | IntType ConstValueRef ',' LABEL ValueRef {
2546 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencere2c32da2006-12-03 05:46:11 +00002547 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Reid Spencer309080a2006-09-28 19:28:24 +00002548 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002549
2550 if (V == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002551 GEN_ERROR("May only switch on a constant pool value");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002552
Reid Spencer309080a2006-09-28 19:28:24 +00002553 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002554 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002555 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002556 };
2557
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002558Inst : OptLocalAssign InstVal {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002559 // Is this definition named?? if so, assign the name...
2560 setValueName($2, $1);
2561 CHECK_FOR_ERROR
2562 InsertValue($2);
2563 $$ = $2;
2564 CHECK_FOR_ERROR
2565 };
2566
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002567
2568PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002569 if (!UpRefs.empty())
2570 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002571 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencere2c32da2006-12-03 05:46:11 +00002572 Value* tmpVal = getVal(*$1, $3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002573 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002574 BasicBlock* tmpBB = getBBVal($5);
2575 CHECK_FOR_ERROR
2576 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencere2c32da2006-12-03 05:46:11 +00002577 delete $1;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002578 }
2579 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2580 $$ = $1;
Reid Spencer309080a2006-09-28 19:28:24 +00002581 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002582 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002583 BasicBlock* tmpBB = getBBVal($6);
2584 CHECK_FOR_ERROR
2585 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002586 };
2587
2588
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002589ValueRefList : Types ValueRef OptParamAttrs {
2590 if (!UpRefs.empty())
2591 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2592 // Used for call and invoke instructions
2593 $$ = new ValueRefList();
2594 ValueRefListEntry E; E.Attrs = $3; E.Val = getVal($1->get(), $2);
2595 $$->push_back(E);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002596 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002597 | ValueRefList ',' Types ValueRef OptParamAttrs {
2598 if (!UpRefs.empty())
2599 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002600 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002601 ValueRefListEntry E; E.Attrs = $5; E.Val = getVal($3->get(), $4);
2602 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002603 CHECK_FOR_ERROR
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002604 }
2605 | /*empty*/ { $$ = new ValueRefList(); };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002606
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002607IndexList // Used for gep instructions and constant expressions
Reid Spencerd0da3e22006-12-31 21:47:02 +00002608 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002609 | IndexList ',' ResolvedVal {
2610 $$ = $1;
2611 $$->push_back($3);
2612 CHECK_FOR_ERROR
2613 }
Reid Spencerd0da3e22006-12-31 21:47:02 +00002614 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002615
2616OptTailCall : TAIL CALL {
2617 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002618 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002619 }
2620 | CALL {
2621 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002622 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002623 };
2624
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002625InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002626 if (!UpRefs.empty())
2627 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner03c49532007-01-15 02:27:26 +00002628 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencerd84d35b2007-02-15 02:26:10 +00002629 !isa<VectorType>((*$2).get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002630 GEN_ERROR(
Reid Spencera7bb3e92007-02-05 10:18:06 +00002631 "Arithmetic operator requires integer, FP, or packed operands");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002632 if (isa<VectorType>((*$2).get()) &&
Reid Spencere2c32da2006-12-03 05:46:11 +00002633 ($1 == Instruction::URem ||
2634 $1 == Instruction::SRem ||
2635 $1 == Instruction::FRem))
Chris Lattner4669b0b2007-02-19 07:44:24 +00002636 GEN_ERROR("Remainder not supported on vector types");
Reid Spencere2c32da2006-12-03 05:46:11 +00002637 Value* val1 = getVal(*$2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002638 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002639 Value* val2 = getVal(*$2, $5);
Reid Spencer309080a2006-09-28 19:28:24 +00002640 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002641 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002642 if ($$ == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002643 GEN_ERROR("binary operator returned null");
Reid Spencere2c32da2006-12-03 05:46:11 +00002644 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002645 }
2646 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002647 if (!UpRefs.empty())
2648 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner03c49532007-01-15 02:27:26 +00002649 if (!(*$2)->isInteger()) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002650 if (Instruction::isShift($1) || !isa<VectorType>($2->get()) ||
2651 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencera7bb3e92007-02-05 10:18:06 +00002652 GEN_ERROR("Logical operator requires integral operands");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002653 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002654 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002655 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002656 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer309080a2006-09-28 19:28:24 +00002657 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002658 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002659 if ($$ == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002660 GEN_ERROR("binary operator returned null");
Reid Spencere2c32da2006-12-03 05:46:11 +00002661 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002662 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002663 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002664 if (!UpRefs.empty())
2665 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencerd84d35b2007-02-15 02:26:10 +00002666 if (isa<VectorType>((*$3).get()))
Chris Lattner4669b0b2007-02-19 07:44:24 +00002667 GEN_ERROR("Vector types not supported by icmp instruction");
Reid Spencere2c32da2006-12-03 05:46:11 +00002668 Value* tmpVal1 = getVal(*$3, $4);
2669 CHECK_FOR_ERROR
2670 Value* tmpVal2 = getVal(*$3, $6);
2671 CHECK_FOR_ERROR
2672 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2673 if ($$ == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002674 GEN_ERROR("icmp operator returned null");
Reid Spencere2c32da2006-12-03 05:46:11 +00002675 }
2676 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002677 if (!UpRefs.empty())
2678 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencerd84d35b2007-02-15 02:26:10 +00002679 if (isa<VectorType>((*$3).get()))
Chris Lattner4669b0b2007-02-19 07:44:24 +00002680 GEN_ERROR("Vector types not supported by fcmp instruction");
Reid Spencere2c32da2006-12-03 05:46:11 +00002681 Value* tmpVal1 = getVal(*$3, $4);
2682 CHECK_FOR_ERROR
2683 Value* tmpVal2 = getVal(*$3, $6);
2684 CHECK_FOR_ERROR
2685 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2686 if ($$ == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002687 GEN_ERROR("fcmp operator returned null");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002688 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002689 | CastOps ResolvedVal TO Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002690 if (!UpRefs.empty())
2691 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002692 Value* Val = $2;
Reid Spencerceb84592007-01-17 02:48:45 +00002693 const Type* DestTy = $4->get();
2694 if (!CastInst::castIsValid($1, Val, DestTy))
2695 GEN_ERROR("invalid cast opcode for cast from '" +
2696 Val->getType()->getDescription() + "' to '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00002697 DestTy->getDescription() + "'");
Reid Spencerceb84592007-01-17 02:48:45 +00002698 $$ = CastInst::create($1, Val, DestTy);
Reid Spencere2c32da2006-12-03 05:46:11 +00002699 delete $4;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002700 }
2701 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer542964f2007-01-11 18:21:29 +00002702 if ($2->getType() != Type::Int1Ty)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002703 GEN_ERROR("select condition must be boolean");
Reid Spencere2c32da2006-12-03 05:46:11 +00002704 if ($4->getType() != $6->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00002705 GEN_ERROR("select value types should match");
Reid Spencere2c32da2006-12-03 05:46:11 +00002706 $$ = new SelectInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002707 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002708 }
2709 | VAARG ResolvedVal ',' Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002710 if (!UpRefs.empty())
2711 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002712 $$ = new VAArgInst($2, *$4);
2713 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00002714 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002715 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002716 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002717 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencera7bb3e92007-02-05 10:18:06 +00002718 GEN_ERROR("Invalid extractelement operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00002719 $$ = new ExtractElementInst($2, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002720 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002721 }
2722 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002723 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencera7bb3e92007-02-05 10:18:06 +00002724 GEN_ERROR("Invalid insertelement operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00002725 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002726 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002727 }
Chris Lattner9ff96a72006-04-08 01:18:56 +00002728 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002729 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencera7bb3e92007-02-05 10:18:06 +00002730 GEN_ERROR("Invalid shufflevector operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00002731 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002732 CHECK_FOR_ERROR
Chris Lattner9ff96a72006-04-08 01:18:56 +00002733 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002734 | PHI_TOK PHIList {
2735 const Type *Ty = $2->front().first->getType();
2736 if (!Ty->isFirstClassType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00002737 GEN_ERROR("PHI node operands must be of first class type");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002738 $$ = new PHINode(Ty);
2739 ((PHINode*)$$)->reserveOperandSpace($2->size());
2740 while ($2->begin() != $2->end()) {
2741 if ($2->front().first->getType() != Ty)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002742 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002743 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
2744 $2->pop_front();
2745 }
2746 delete $2; // Free the list...
Reid Spencer713eedc2006-08-18 08:43:06 +00002747 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002748 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002749 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ValueRefList ')'
2750 OptFuncAttrs {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002751
2752 // Handle the short syntax
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002753 const PointerType *PFTy = 0;
2754 const FunctionType *Ty = 0;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002755 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002756 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2757 // Pull out the types of all of the arguments...
2758 std::vector<const Type*> ParamTypes;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002759 FunctionType::ParamAttrsList ParamAttrs;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002760 ParamAttrs.push_back($8);
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002761 for (ValueRefList::iterator I = $6->begin(), E = $6->end(); I != E; ++I) {
2762 const Type *Ty = I->Val->getType();
2763 if (Ty == Type::VoidTy)
2764 GEN_ERROR("Short call syntax cannot be used with varargs");
2765 ParamTypes.push_back(Ty);
2766 ParamAttrs.push_back(I->Attrs);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002767 }
2768
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002769 Ty = FunctionType::get($3->get(), ParamTypes, false, ParamAttrs);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002770 PFTy = PointerType::get(Ty);
2771 }
2772
2773 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer309080a2006-09-28 19:28:24 +00002774 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002775
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002776 // Check the arguments
2777 ValueList Args;
2778 if ($6->empty()) { // Has no arguments?
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002779 // Make sure no arguments is a good thing!
2780 if (Ty->getNumParams() != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002781 GEN_ERROR("No arguments passed to a function that "
Reid Spencera7bb3e92007-02-05 10:18:06 +00002782 "expects arguments");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002783 } else { // Has arguments?
2784 // Loop through FunctionType's arguments and ensure they are specified
2785 // correctly!
2786 //
2787 FunctionType::param_iterator I = Ty->param_begin();
2788 FunctionType::param_iterator E = Ty->param_end();
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002789 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002790
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002791 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2792 if (ArgI->Val->getType() != *I)
2793 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00002794 (*I)->getDescription() + "'");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002795 Args.push_back(ArgI->Val);
2796 }
2797 if (Ty->isVarArg()) {
2798 if (I == E)
2799 for (; ArgI != ArgE; ++ArgI)
2800 Args.push_back(ArgI->Val); // push the remaining varargs
2801 } else if (I != E || ArgI != ArgE)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002802 GEN_ERROR("Invalid number of parameters detected");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002803 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002804 // Create the call node
Chris Lattnerd8028242007-02-13 05:53:56 +00002805 CallInst *CI = new CallInst(V, &Args[0], Args.size());
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002806 CI->setTailCall($1);
2807 CI->setCallingConv($2);
2808 $$ = CI;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002809 delete $6;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002810 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00002811 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002812 }
2813 | MemoryInst {
2814 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002815 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002816 };
2817
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002818OptVolatile : VOLATILE {
2819 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002820 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002821 }
2822 | /* empty */ {
2823 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002824 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002825 };
2826
2827
2828
2829MemoryInst : MALLOC Types OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002830 if (!UpRefs.empty())
2831 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002832 $$ = new MallocInst(*$2, 0, $3);
2833 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002834 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002835 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +00002836 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002837 if (!UpRefs.empty())
2838 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002839 Value* tmpVal = getVal($4, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002840 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002841 $$ = new MallocInst(*$2, tmpVal, $6);
2842 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002843 }
2844 | ALLOCA Types OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002845 if (!UpRefs.empty())
2846 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002847 $$ = new AllocaInst(*$2, 0, $3);
2848 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002849 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002850 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +00002851 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
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 Value* tmpVal = getVal($4, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002855 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002856 $$ = new AllocaInst(*$2, tmpVal, $6);
2857 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002858 }
2859 | FREE ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002860 if (!isa<PointerType>($2->getType()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002861 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencera7bb3e92007-02-05 10:18:06 +00002862 $2->getType()->getDescription() + "");
Reid Spencere2c32da2006-12-03 05:46:11 +00002863 $$ = new FreeInst($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00002864 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002865 }
2866
2867 | OptVolatile LOAD Types ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002868 if (!UpRefs.empty())
2869 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002870 if (!isa<PointerType>($3->get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002871 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencere2c32da2006-12-03 05:46:11 +00002872 (*$3)->getDescription());
2873 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002874 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencere2c32da2006-12-03 05:46:11 +00002875 (*$3)->getDescription());
2876 Value* tmpVal = getVal(*$3, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002877 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002878 $$ = new LoadInst(tmpVal, "", $1);
Reid Spencere2c32da2006-12-03 05:46:11 +00002879 delete $3;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002880 }
2881 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002882 if (!UpRefs.empty())
2883 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002884 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002885 if (!PT)
Reid Spencer713eedc2006-08-18 08:43:06 +00002886 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencere2c32da2006-12-03 05:46:11 +00002887 (*$5)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002888 const Type *ElTy = PT->getElementType();
Reid Spencere2c32da2006-12-03 05:46:11 +00002889 if (ElTy != $3->getType())
2890 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencera7bb3e92007-02-05 10:18:06 +00002891 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002892
Reid Spencere2c32da2006-12-03 05:46:11 +00002893 Value* tmpVal = getVal(*$5, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002894 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002895 $$ = new StoreInst($3, tmpVal, $1);
2896 delete $5;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002897 }
2898 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002899 if (!UpRefs.empty())
2900 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002901 if (!isa<PointerType>($2->get()))
Reid Spencera7bb3e92007-02-05 10:18:06 +00002902 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002903
Chris Lattnerf79508f2007-02-13 00:58:01 +00002904 if (!GetElementPtrInst::getIndexedType(*$2, &(*$4)[0], $4->size(), true))
Reid Spencer713eedc2006-08-18 08:43:06 +00002905 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00002906 (*$2)->getDescription()+ "'");
Reid Spencere2c32da2006-12-03 05:46:11 +00002907 Value* tmpVal = getVal(*$2, $3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002908 CHECK_FOR_ERROR
Chris Lattnerf79508f2007-02-13 00:58:01 +00002909 $$ = new GetElementPtrInst(tmpVal, &(*$4)[0], $4->size());
Reid Spencere2c32da2006-12-03 05:46:11 +00002910 delete $2;
Reid Spencer309080a2006-09-28 19:28:24 +00002911 delete $4;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002912 };
2913
2914
2915%%
Reid Spencer713eedc2006-08-18 08:43:06 +00002916
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002917// common code from the two 'RunVMAsmParser' functions
2918static Module* RunParser(Module * M) {
2919
2920 llvmAsmlineno = 1; // Reset the current line number...
2921 CurModule.CurrentModule = M;
2922#if YYDEBUG
2923 yydebug = Debug;
2924#endif
2925
2926 // Check to make sure the parser succeeded
2927 if (yyparse()) {
2928 if (ParserResult)
2929 delete ParserResult;
2930 return 0;
2931 }
2932
2933 // Check to make sure that parsing produced a result
2934 if (!ParserResult)
2935 return 0;
2936
2937 // Reset ParserResult variable while saving its value for the result.
2938 Module *Result = ParserResult;
2939 ParserResult = 0;
2940
2941 return Result;
2942}
2943
Reid Spencer713eedc2006-08-18 08:43:06 +00002944void llvm::GenerateError(const std::string &message, int LineNo) {
2945 if (LineNo == -1) LineNo = llvmAsmlineno;
2946 // TODO: column number in exception
2947 if (TheParseError)
2948 TheParseError->setError(CurFilename, message, LineNo);
2949 TriggerError = 1;
2950}
2951
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002952int yyerror(const char *ErrorMsg) {
2953 std::string where
2954 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2955 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002956 std::string errMsg = where + "error: " + std::string(ErrorMsg);
2957 if (yychar != YYEMPTY && yychar != 0)
2958 errMsg += " while reading token: '" + std::string(llvmAsmtext, llvmAsmleng)+
2959 "'";
Reid Spencer713eedc2006-08-18 08:43:06 +00002960 GenerateError(errMsg);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002961 return 0;
2962}