blob: 24b0abfdd344912a0de86f1ca889bc436e3d9367 [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>
29#include <utility>
Reid Spencer42f0cbe2006-12-31 05:40:51 +000030#ifndef NDEBUG
31#define YYDEBUG 1
32#endif
Chris Lattnerf20e61f2006-02-15 07:22:58 +000033
Reid Spencerb50974a2006-08-18 17:32:55 +000034// The following is a gross hack. In order to rid the libAsmParser library of
35// exceptions, we have to have a way of getting the yyparse function to go into
36// an error situation. So, whenever we want an error to occur, the GenerateError
37// function (see bottom of file) sets TriggerError. Then, at the end of each
38// production in the grammer we use CHECK_FOR_ERROR which will invoke YYERROR
39// (a goto) to put YACC in error state. Furthermore, several calls to
40// GenerateError are made from inside productions and they must simulate the
41// previous exception behavior by exiting the production immediately. We have
42// replaced these with the GEN_ERROR macro which calls GeneratError and then
43// immediately invokes YYERROR. This would be so much cleaner if it was a
44// recursive descent parser.
Reid Spencer713eedc2006-08-18 08:43:06 +000045static bool TriggerError = false;
Reid Spencerff359002006-10-09 17:36:59 +000046#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYABORT; } }
Reid Spencer713eedc2006-08-18 08:43:06 +000047#define GEN_ERROR(msg) { GenerateError(msg); YYERROR; }
48
Chris Lattnerf20e61f2006-02-15 07:22:58 +000049int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
50int yylex(); // declaration" of xxx warnings.
51int yyparse();
52
53namespace llvm {
54 std::string CurFilename;
Reid Spencer42f0cbe2006-12-31 05:40:51 +000055#if YYDEBUG
56static cl::opt<bool>
57Debug("debug-yacc", cl::desc("Print yacc debug state changes"),
58 cl::Hidden, cl::init(false));
59#endif
Chris Lattnerf20e61f2006-02-15 07:22:58 +000060}
61using namespace llvm;
62
63static Module *ParserResult;
64
65// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
66// relating to upreferences in the input stream.
67//
68//#define DEBUG_UPREFS 1
69#ifdef DEBUG_UPREFS
Bill Wendlingf3baad32006-12-07 01:30:32 +000070#define UR_OUT(X) cerr << X
Chris Lattnerf20e61f2006-02-15 07:22:58 +000071#else
72#define UR_OUT(X)
73#endif
74
75#define YYERROR_VERBOSE 1
76
Chris Lattnerf20e61f2006-02-15 07:22:58 +000077static GlobalVariable *CurGV;
78
79
80// This contains info used when building the body of a function. It is
81// destroyed when the function is completed.
82//
83typedef std::vector<Value *> ValueList; // Numbered defs
Reid Spencer42f0cbe2006-12-31 05:40:51 +000084
Chris Lattnerf20e61f2006-02-15 07:22:58 +000085static void
86ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
87 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
88
89static struct PerModuleInfo {
90 Module *CurrentModule;
91 std::map<const Type *, ValueList> Values; // Module level numbered definitions
92 std::map<const Type *,ValueList> LateResolveValues;
Reid Spencer55f1fbe2006-11-28 07:29:44 +000093 std::vector<PATypeHolder> Types;
94 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattnerf20e61f2006-02-15 07:22:58 +000095
96 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Chris Lattner7aa45902006-06-21 16:53:00 +000097 /// how they were referenced and on which line of the input they came from so
Chris Lattnerf20e61f2006-02-15 07:22:58 +000098 /// that we can resolve them later and print error messages as appropriate.
99 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
100
101 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
102 // references to global values. Global values may be referenced before they
103 // are defined, and if so, the temporary object that they represent is held
104 // here. This is used for forward references of GlobalValues.
105 //
106 typedef std::map<std::pair<const PointerType *,
107 ValID>, GlobalValue*> GlobalRefsType;
108 GlobalRefsType GlobalRefs;
109
110 void ModuleDone() {
111 // If we could not resolve some functions at function compilation time
112 // (calls to functions before they are defined), resolve them now... Types
113 // are resolved when the constant pool has been completely parsed.
114 //
115 ResolveDefinitions(LateResolveValues);
Reid Spencer309080a2006-09-28 19:28:24 +0000116 if (TriggerError)
117 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000118
119 // Check to make sure that all global value forward references have been
120 // resolved!
121 //
122 if (!GlobalRefs.empty()) {
123 std::string UndefinedReferences = "Unresolved global references exist:\n";
124
125 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
126 I != E; ++I) {
127 UndefinedReferences += " " + I->first.first->getDescription() + " " +
128 I->first.second.getName() + "\n";
129 }
Reid Spencer713eedc2006-08-18 08:43:06 +0000130 GenerateError(UndefinedReferences);
Reid Spencer309080a2006-09-28 19:28:24 +0000131 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000132 }
133
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000134 Values.clear(); // Clear out function local definitions
135 Types.clear();
136 CurrentModule = 0;
137 }
138
139 // GetForwardRefForGlobal - Check to see if there is a forward reference
140 // for this global. If so, remove it from the GlobalRefs map and return it.
141 // If not, just return null.
142 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
143 // Check to see if there is a forward reference to this global variable...
144 // if there is, eliminate it and patch the reference to use the new def'n.
145 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
146 GlobalValue *Ret = 0;
147 if (I != GlobalRefs.end()) {
148 Ret = I->second;
149 GlobalRefs.erase(I);
150 }
151 return Ret;
152 }
Reid Spencer87622ae2007-01-02 21:54:12 +0000153
154 bool TypeIsUnresolved(PATypeHolder* PATy) {
155 // If it isn't abstract, its resolved
156 const Type* Ty = PATy->get();
157 if (!Ty->isAbstract())
158 return false;
159 // Traverse the type looking for abstract types. If it isn't abstract then
160 // we don't need to traverse that leg of the type.
161 std::vector<const Type*> WorkList, SeenList;
162 WorkList.push_back(Ty);
163 while (!WorkList.empty()) {
164 const Type* Ty = WorkList.back();
165 SeenList.push_back(Ty);
166 WorkList.pop_back();
167 if (const OpaqueType* OpTy = dyn_cast<OpaqueType>(Ty)) {
168 // Check to see if this is an unresolved type
169 std::map<ValID, PATypeHolder>::iterator I = LateResolveTypes.begin();
170 std::map<ValID, PATypeHolder>::iterator E = LateResolveTypes.end();
171 for ( ; I != E; ++I) {
172 if (I->second.get() == OpTy)
173 return true;
174 }
175 } else if (const SequentialType* SeqTy = dyn_cast<SequentialType>(Ty)) {
176 const Type* TheTy = SeqTy->getElementType();
177 if (TheTy->isAbstract() && TheTy != Ty) {
178 std::vector<const Type*>::iterator I = SeenList.begin(),
179 E = SeenList.end();
180 for ( ; I != E; ++I)
181 if (*I == TheTy)
182 break;
183 if (I == E)
184 WorkList.push_back(TheTy);
185 }
186 } else if (const StructType* StrTy = dyn_cast<StructType>(Ty)) {
187 for (unsigned i = 0; i < StrTy->getNumElements(); ++i) {
188 const Type* TheTy = StrTy->getElementType(i);
189 if (TheTy->isAbstract() && TheTy != Ty) {
190 std::vector<const Type*>::iterator I = SeenList.begin(),
191 E = SeenList.end();
192 for ( ; I != E; ++I)
193 if (*I == TheTy)
194 break;
195 if (I == E)
196 WorkList.push_back(TheTy);
197 }
198 }
199 }
200 }
201 return false;
202 }
203
204
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000205} CurModule;
206
207static struct PerFunctionInfo {
208 Function *CurrentFunction; // Pointer to current function being created
209
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000210 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000211 std::map<const Type*, ValueList> LateResolveValues;
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000212 bool isDeclare; // Is this function a forward declararation?
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000213 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000214 GlobalValue::VisibilityTypes Visibility;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000215
216 /// BBForwardRefs - When we see forward references to basic blocks, keep
217 /// track of them here.
218 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
219 std::vector<BasicBlock*> NumberedBlocks;
220 unsigned NextBBNum;
221
222 inline PerFunctionInfo() {
223 CurrentFunction = 0;
224 isDeclare = false;
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000225 Linkage = GlobalValue::ExternalLinkage;
226 Visibility = GlobalValue::DefaultVisibility;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000227 }
228
229 inline void FunctionStart(Function *M) {
230 CurrentFunction = M;
231 NextBBNum = 0;
232 }
233
234 void FunctionDone() {
235 NumberedBlocks.clear();
236
237 // Any forward referenced blocks left?
Reid Spencer309080a2006-09-28 19:28:24 +0000238 if (!BBForwardRefs.empty()) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000239 GenerateError("Undefined reference to label " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000240 BBForwardRefs.begin()->first->getName());
Reid Spencer309080a2006-09-28 19:28:24 +0000241 return;
242 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000243
244 // Resolve all forward references now.
245 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
246
247 Values.clear(); // Clear out function local definitions
248 CurrentFunction = 0;
249 isDeclare = false;
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000250 Linkage = GlobalValue::ExternalLinkage;
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000251 Visibility = GlobalValue::DefaultVisibility;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000252 }
253} CurFun; // Info for the current function...
254
255static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
256
257
258//===----------------------------------------------------------------------===//
259// Code to handle definitions of all the types
260//===----------------------------------------------------------------------===//
261
262static int InsertValue(Value *V,
263 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
264 if (V->hasName()) return -1; // Is this a numbered definition?
265
266 // Yes, insert the value into the value table...
267 ValueList &List = ValueTab[V->getType()];
268 List.push_back(V);
269 return List.size()-1;
270}
271
272static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
273 switch (D.Type) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000274 case ValID::LocalID: // Is it a numbered definition?
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000275 // Module constants occupy the lowest numbered slots...
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000276 if (D.Num < CurModule.Types.size())
277 return CurModule.Types[D.Num];
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000278 break;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000279 case ValID::LocalName: // Is it a named definition?
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000280 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
281 D.destroy(); // Free old strdup'd memory...
282 return N;
283 }
284 break;
285 default:
Reid Spencera7bb3e92007-02-05 10:18:06 +0000286 GenerateError("Internal parser error: Invalid symbol type reference");
Reid Spencer309080a2006-09-28 19:28:24 +0000287 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000288 }
289
290 // If we reached here, we referenced either a symbol that we don't know about
291 // or an id number that hasn't been read yet. We may be referencing something
292 // forward, so just create an entry to be resolved later and get to it...
293 //
294 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
295
296
297 if (inFunctionScope()) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000298 if (D.Type == ValID::LocalName) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000299 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer309080a2006-09-28 19:28:24 +0000300 return 0;
301 } else {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000302 GenerateError("Reference to an undefined type: #" + utostr(D.Num));
Reid Spencer309080a2006-09-28 19:28:24 +0000303 return 0;
304 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000305 }
306
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000307 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000308 if (I != CurModule.LateResolveTypes.end())
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000309 return I->second;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000310
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000311 Type *Typ = OpaqueType::get();
312 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
313 return Typ;
Reid Spencere2c32da2006-12-03 05:46:11 +0000314 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000315
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000316// getValNonImprovising - Look up the value specified by the provided type and
317// the provided ValID. If the value exists and has already been defined, return
318// it. Otherwise return null.
319//
320static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Reid Spencer309080a2006-09-28 19:28:24 +0000321 if (isa<FunctionType>(Ty)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000322 GenerateError("Functions are not values and "
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000323 "must be referenced as pointers");
Reid Spencer309080a2006-09-28 19:28:24 +0000324 return 0;
325 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000326
327 switch (D.Type) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000328 case ValID::LocalID: { // Is it a numbered definition?
329 // Module constants occupy the lowest numbered slots.
330 std::map<const Type*,ValueList>::iterator VI = CurFun.Values.find(Ty);
331 // Make sure that our type is within bounds.
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000332 if (VI == CurFun.Values.end()) return 0;
333
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000334 // Check that the number is within bounds.
335 if (D.Num >= VI->second.size()) return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000336
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000337 return VI->second[D.Num];
338 }
339 case ValID::GlobalID: { // Is it a numbered definition?
340 unsigned Num = D.Num;
341
342 // Module constants occupy the lowest numbered slots...
343 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000344 if (VI == CurModule.Values.end())
345 return 0;
346 if (D.Num >= VI->second.size())
347 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000348 return VI->second[Num];
349 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000350
351 case ValID::LocalName: { // Is it a named definition?
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000352 if (!inFunctionScope())
353 return 0;
354 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
355 Value *N = SymTab.lookup(D.Name);
356 if (N == 0)
357 return 0;
358 if (N->getType() != Ty)
359 return 0;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000360
361 D.destroy(); // Free old strdup'd memory...
362 return N;
363 }
364 case ValID::GlobalName: { // Is it a named definition?
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000365 ValueSymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable();
366 Value *N = SymTab.lookup(D.Name);
367 if (N == 0)
368 return 0;
369 if (N->getType() != Ty)
370 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000371
372 D.destroy(); // Free old strdup'd memory...
373 return N;
374 }
375
376 // Check to make sure that "Ty" is an integral type, and that our
377 // value will fit into the specified type...
378 case ValID::ConstSIntVal: // Is it a constant pool reference??
Reid Spencere0fc4df2006-10-20 07:07:24 +0000379 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000380 GenerateError("Signed integral constant '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000381 itostr(D.ConstPool64) + "' is invalid for type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +0000382 Ty->getDescription() + "'");
Reid Spencer309080a2006-09-28 19:28:24 +0000383 return 0;
384 }
Reid Spencere0fc4df2006-10-20 07:07:24 +0000385 return ConstantInt::get(Ty, D.ConstPool64);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000386
387 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Reid Spencere0fc4df2006-10-20 07:07:24 +0000388 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
389 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000390 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Reid Spencera7bb3e92007-02-05 10:18:06 +0000391 "' is invalid or out of range");
Reid Spencer309080a2006-09-28 19:28:24 +0000392 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000393 } else { // This is really a signed reference. Transmogrify.
Reid Spencere0fc4df2006-10-20 07:07:24 +0000394 return ConstantInt::get(Ty, D.ConstPool64);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000395 }
396 } else {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000397 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000398 }
399
400 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Reid Spencer309080a2006-09-28 19:28:24 +0000401 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP)) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000402 GenerateError("FP constant invalid for type");
Reid Spencer309080a2006-09-28 19:28:24 +0000403 return 0;
404 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000405 return ConstantFP::get(Ty, D.ConstPoolFP);
406
407 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer309080a2006-09-28 19:28:24 +0000408 if (!isa<PointerType>(Ty)) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000409 GenerateError("Cannot create a a non pointer null");
Reid Spencer309080a2006-09-28 19:28:24 +0000410 return 0;
411 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000412 return ConstantPointerNull::get(cast<PointerType>(Ty));
413
414 case ValID::ConstUndefVal: // Is it an undef value?
415 return UndefValue::get(Ty);
416
417 case ValID::ConstZeroVal: // Is it a zero value?
418 return Constant::getNullValue(Ty);
419
420 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer309080a2006-09-28 19:28:24 +0000421 if (D.ConstantValue->getType() != Ty) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000422 GenerateError("Constant expression type different from required type");
Reid Spencer309080a2006-09-28 19:28:24 +0000423 return 0;
424 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000425 return D.ConstantValue;
426
427 case ValID::InlineAsmVal: { // Inline asm expression
428 const PointerType *PTy = dyn_cast<PointerType>(Ty);
429 const FunctionType *FTy =
430 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer309080a2006-09-28 19:28:24 +0000431 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000432 GenerateError("Invalid type for asm constraint string");
Reid Spencer309080a2006-09-28 19:28:24 +0000433 return 0;
434 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000435 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
436 D.IAD->HasSideEffects);
437 D.destroy(); // Free InlineAsmDescriptor.
438 return IA;
439 }
440 default:
Reid Spencerac6bfc52007-02-05 17:04:00 +0000441 assert(0 && "Unhandled case!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000442 return 0;
443 } // End of switch
444
Reid Spencerac6bfc52007-02-05 17:04:00 +0000445 assert(0 && "Unhandled case!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000446 return 0;
447}
448
449// getVal - This function is identical to getValNonImprovising, except that if a
450// value is not already defined, it "improvises" by creating a placeholder var
451// that looks and acts just like the requested variable. When the value is
452// defined later, all uses of the placeholder variable are replaced with the
453// real thing.
454//
455static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer309080a2006-09-28 19:28:24 +0000456 if (Ty == Type::LabelTy) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000457 GenerateError("Cannot use a basic block here");
Reid Spencer309080a2006-09-28 19:28:24 +0000458 return 0;
459 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000460
461 // See if the value has already been defined.
462 Value *V = getValNonImprovising(Ty, ID);
463 if (V) return V;
Reid Spencer309080a2006-09-28 19:28:24 +0000464 if (TriggerError) return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000465
Reid Spencer309080a2006-09-28 19:28:24 +0000466 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000467 GenerateError("Invalid use of a composite type");
Reid Spencer309080a2006-09-28 19:28:24 +0000468 return 0;
469 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000470
471 // If we reached here, we referenced either a symbol that we don't know about
472 // or an id number that hasn't been read yet. We may be referencing something
473 // forward, so just create an entry to be resolved later and get to it...
474 //
475 V = new Argument(Ty);
476
477 // Remember where this forward reference came from. FIXME, shouldn't we try
478 // to recycle these things??
479 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
480 llvmAsmlineno)));
481
482 if (inFunctionScope())
483 InsertValue(V, CurFun.LateResolveValues);
484 else
485 InsertValue(V, CurModule.LateResolveValues);
486 return V;
487}
488
489/// getBBVal - This is used for two purposes:
490/// * If isDefinition is true, a new basic block with the specified ID is being
491/// defined.
492/// * If isDefinition is true, this is a reference to a basic block, which may
493/// or may not be a forward reference.
494///
495static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
Reid Spencerac6bfc52007-02-05 17:04:00 +0000496 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000497
498 std::string Name;
499 BasicBlock *BB = 0;
500 switch (ID.Type) {
Reid Spencer309080a2006-09-28 19:28:24 +0000501 default:
502 GenerateError("Illegal label reference " + ID.getName());
503 return 0;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000504 case ValID::LocalID: // Is it a numbered definition?
505 if (ID.Num >= CurFun.NumberedBlocks.size())
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000506 CurFun.NumberedBlocks.resize(ID.Num+1);
507 BB = CurFun.NumberedBlocks[ID.Num];
508 break;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000509 case ValID::LocalName: // Is it a named definition?
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000510 Name = ID.Name;
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000511 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
512 if (N && N->getType()->getTypeID() == Type::LabelTyID)
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000513 BB = cast<BasicBlock>(N);
514 break;
515 }
516
517 // See if the block has already been defined.
518 if (BB) {
519 // If this is the definition of the block, make sure the existing value was
520 // just a forward reference. If it was a forward reference, there will be
521 // an entry for it in the PlaceHolderInfo map.
Reid Spencer309080a2006-09-28 19:28:24 +0000522 if (isDefinition && !CurFun.BBForwardRefs.erase(BB)) {
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000523 // The existing value was a definition, not a forward reference.
Reid Spencer713eedc2006-08-18 08:43:06 +0000524 GenerateError("Redefinition of label " + ID.getName());
Reid Spencer309080a2006-09-28 19:28:24 +0000525 return 0;
526 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000527
528 ID.destroy(); // Free strdup'd memory.
529 return BB;
530 }
531
532 // Otherwise this block has not been seen before.
533 BB = new BasicBlock("", CurFun.CurrentFunction);
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000534 if (ID.Type == ValID::LocalName) {
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000535 BB->setName(ID.Name);
536 } else {
537 CurFun.NumberedBlocks[ID.Num] = BB;
538 }
539
540 // If this is not a definition, keep track of it so we can use it as a forward
541 // reference.
542 if (!isDefinition) {
543 // Remember where this forward reference came from.
544 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
545 } else {
546 // The forward declaration could have been inserted anywhere in the
547 // function: insert it into the correct place now.
548 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
549 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
550 }
551 ID.destroy();
552 return BB;
553}
554
555
556//===----------------------------------------------------------------------===//
557// Code to handle forward references in instructions
558//===----------------------------------------------------------------------===//
559//
560// This code handles the late binding needed with statements that reference
561// values not defined yet... for example, a forward branch, or the PHI node for
562// a loop body.
563//
564// This keeps a table (CurFun.LateResolveValues) of all such forward references
565// and back patchs after we are done.
566//
567
568// ResolveDefinitions - If we could not resolve some defs at parsing
569// time (forward branches, phi functions for loops, etc...) resolve the
570// defs now...
571//
572static void
573ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
574 std::map<const Type*,ValueList> *FutureLateResolvers) {
575 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
576 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
577 E = LateResolvers.end(); LRI != E; ++LRI) {
578 ValueList &List = LRI->second;
579 while (!List.empty()) {
580 Value *V = List.back();
581 List.pop_back();
582
583 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
584 CurModule.PlaceHolderInfo.find(V);
Reid Spencerac6bfc52007-02-05 17:04:00 +0000585 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000586
587 ValID &DID = PHI->second.first;
588
589 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Reid Spencer309080a2006-09-28 19:28:24 +0000590 if (TriggerError)
591 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000592 if (TheRealValue) {
593 V->replaceAllUsesWith(TheRealValue);
594 delete V;
595 CurModule.PlaceHolderInfo.erase(PHI);
596 } else if (FutureLateResolvers) {
597 // Functions have their unresolved items forwarded to the module late
598 // resolver table
599 InsertValue(V, *FutureLateResolvers);
600 } else {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000601 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000602 GenerateError("Reference to an invalid definition: '" +DID.getName()+
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000603 "' of type '" + V->getType()->getDescription() + "'",
604 PHI->second.second);
Reid Spencer309080a2006-09-28 19:28:24 +0000605 return;
606 } else {
Reid Spencer713eedc2006-08-18 08:43:06 +0000607 GenerateError("Reference to an invalid definition: #" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000608 itostr(DID.Num) + " of type '" +
609 V->getType()->getDescription() + "'",
610 PHI->second.second);
Reid Spencer309080a2006-09-28 19:28:24 +0000611 return;
612 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000613 }
614 }
615 }
616
617 LateResolvers.clear();
618}
619
620// ResolveTypeTo - A brand new type was just declared. This means that (if
621// name is not null) things referencing Name can be resolved. Otherwise, things
622// refering to the number can be resolved. Do this now.
623//
624static void ResolveTypeTo(char *Name, const Type *ToTy) {
625 ValID D;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000626 if (Name) D = ValID::createLocalName(Name);
627 else D = ValID::createLocalID(CurModule.Types.size());
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000628
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000629 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000630 CurModule.LateResolveTypes.find(D);
631 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000632 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000633 CurModule.LateResolveTypes.erase(I);
634 }
635}
636
637// setValueName - Set the specified value to the name given. The name may be
638// null potentially, in which case this is a noop. The string passed in is
639// assumed to be a malloc'd string buffer, and is free'd by this function.
640//
641static void setValueName(Value *V, char *NameStr) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000642 if (!NameStr) return;
643 std::string Name(NameStr); // Copy string
644 free(NameStr); // Free old string
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000645
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000646 if (V->getType() == Type::VoidTy) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000647 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000648 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000649 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000650
Reid Spencerac6bfc52007-02-05 17:04:00 +0000651 assert(inFunctionScope() && "Must be in function scope!");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000652 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
653 if (ST.lookup(Name)) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000654 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +0000655 V->getType()->getDescription() + "'");
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000656 return;
657 }
658
659 // Set the name.
660 V->setName(Name);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000661}
662
663/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
664/// this is a declaration, otherwise it is a definition.
665static GlobalVariable *
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000666ParseGlobalVariable(char *NameStr,
667 GlobalValue::LinkageTypes Linkage,
668 GlobalValue::VisibilityTypes Visibility,
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000669 bool isConstantGlobal, const Type *Ty,
670 Constant *Initializer) {
Reid Spencer309080a2006-09-28 19:28:24 +0000671 if (isa<FunctionType>(Ty)) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000672 GenerateError("Cannot declare global vars of function type");
Reid Spencer309080a2006-09-28 19:28:24 +0000673 return 0;
674 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000675
676 const PointerType *PTy = PointerType::get(Ty);
677
678 std::string Name;
679 if (NameStr) {
680 Name = NameStr; // Copy string
681 free(NameStr); // Free old string
682 }
683
684 // See if this global value was forward referenced. If so, recycle the
685 // object.
686 ValID ID;
687 if (!Name.empty()) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000688 ID = ValID::createGlobalName((char*)Name.c_str());
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000689 } else {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000690 ID = ValID::createGlobalID(CurModule.Values[PTy].size());
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000691 }
692
693 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
694 // Move the global to the end of the list, from whereever it was
695 // previously inserted.
696 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
697 CurModule.CurrentModule->getGlobalList().remove(GV);
698 CurModule.CurrentModule->getGlobalList().push_back(GV);
699 GV->setInitializer(Initializer);
700 GV->setLinkage(Linkage);
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000701 GV->setVisibility(Visibility);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000702 GV->setConstant(isConstantGlobal);
703 InsertValue(GV, CurModule.Values);
704 return GV;
705 }
706
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000707 // If this global has a name
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000708 if (!Name.empty()) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000709 // if the global we're parsing has an initializer (is a definition) and
710 // has external linkage.
711 if (Initializer && Linkage != GlobalValue::InternalLinkage)
712 // If there is already a global with external linkage with this name
713 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
714 // If we allow this GVar to get created, it will be renamed in the
715 // symbol table because it conflicts with an existing GVar. We can't
716 // allow redefinition of GVars whose linking indicates that their name
717 // must stay the same. Issue the error.
718 GenerateError("Redefinition of global variable named '" + Name +
719 "' of type '" + Ty->getDescription() + "'");
720 return 0;
721 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000722 }
723
724 // Otherwise there is no existing GV to use, create one now.
725 GlobalVariable *GV =
726 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
727 CurModule.CurrentModule);
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000728 GV->setVisibility(Visibility);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000729 InsertValue(GV, CurModule.Values);
730 return GV;
731}
732
733// setTypeName - Set the specified type to the name given. The name may be
734// null potentially, in which case this is a noop. The string passed in is
735// assumed to be a malloc'd string buffer, and is freed by this function.
736//
737// This function returns true if the type has already been defined, but is
738// allowed to be redefined in the specified context. If the name is a new name
739// for the type plane, it is inserted and false is returned.
740static bool setTypeName(const Type *T, char *NameStr) {
Reid Spencerac6bfc52007-02-05 17:04:00 +0000741 assert(!inFunctionScope() && "Can't give types function-local names!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000742 if (NameStr == 0) return false;
743
744 std::string Name(NameStr); // Copy string
745 free(NameStr); // Free old string
746
747 // We don't allow assigning names to void type
Reid Spencer309080a2006-09-28 19:28:24 +0000748 if (T == Type::VoidTy) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000749 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer309080a2006-09-28 19:28:24 +0000750 return false;
751 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000752
753 // Set the type name, checking for conflicts as we do so.
754 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
755
756 if (AlreadyExists) { // Inserting a name that is already defined???
757 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencerac6bfc52007-02-05 17:04:00 +0000758 assert(Existing && "Conflict but no matching type?!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000759
760 // There is only one case where this is allowed: when we are refining an
761 // opaque type. In this case, Existing will be an opaque type.
762 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
763 // We ARE replacing an opaque type!
764 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
765 return true;
766 }
767
768 // Otherwise, this is an attempt to redefine a type. That's okay if
769 // the redefinition is identical to the original. This will be so if
770 // Existing and T point to the same Type object. In this one case we
771 // allow the equivalent redefinition.
772 if (Existing == T) return true; // Yes, it's equal.
773
774 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer33259082007-01-05 21:51:07 +0000775 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +0000776 T->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000777 }
778
779 return false;
780}
781
782//===----------------------------------------------------------------------===//
783// Code for handling upreferences in type names...
784//
785
786// TypeContains - Returns true if Ty directly contains E in it.
787//
788static bool TypeContains(const Type *Ty, const Type *E) {
789 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
790 E) != Ty->subtype_end();
791}
792
793namespace {
794 struct UpRefRecord {
795 // NestingLevel - The number of nesting levels that need to be popped before
796 // this type is resolved.
797 unsigned NestingLevel;
798
799 // LastContainedTy - This is the type at the current binding level for the
800 // type. Every time we reduce the nesting level, this gets updated.
801 const Type *LastContainedTy;
802
803 // UpRefTy - This is the actual opaque type that the upreference is
804 // represented with.
805 OpaqueType *UpRefTy;
806
807 UpRefRecord(unsigned NL, OpaqueType *URTy)
808 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
809 };
810}
811
812// UpRefs - A list of the outstanding upreferences that need to be resolved.
813static std::vector<UpRefRecord> UpRefs;
814
815/// HandleUpRefs - Every time we finish a new layer of types, this function is
816/// called. It loops through the UpRefs vector, which is a list of the
817/// currently active types. For each type, if the up reference is contained in
818/// the newly completed type, we decrement the level count. When the level
819/// count reaches zero, the upreferenced type is the type that is passed in:
820/// thus we can complete the cycle.
821///
822static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner680aab62006-08-18 17:34:45 +0000823 // If Ty isn't abstract, or if there are no up-references in it, then there is
824 // nothing to resolve here.
825 if (!ty->isAbstract() || UpRefs.empty()) return ty;
826
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000827 PATypeHolder Ty(ty);
828 UR_OUT("Type '" << Ty->getDescription() <<
829 "' newly formed. Resolving upreferences.\n" <<
830 UpRefs.size() << " upreferences active!\n");
831
832 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
833 // to zero), we resolve them all together before we resolve them to Ty. At
834 // the end of the loop, if there is anything to resolve to Ty, it will be in
835 // this variable.
836 OpaqueType *TypeToResolve = 0;
837
838 for (unsigned i = 0; i != UpRefs.size(); ++i) {
839 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
840 << UpRefs[i].second->getDescription() << ") = "
841 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
842 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
843 // Decrement level of upreference
844 unsigned Level = --UpRefs[i].NestingLevel;
845 UpRefs[i].LastContainedTy = Ty;
846 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
847 if (Level == 0) { // Upreference should be resolved!
848 if (!TypeToResolve) {
849 TypeToResolve = UpRefs[i].UpRefTy;
850 } else {
851 UR_OUT(" * Resolving upreference for "
852 << UpRefs[i].second->getDescription() << "\n";
853 std::string OldName = UpRefs[i].UpRefTy->getDescription());
854 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
855 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
856 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
857 }
858 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
859 --i; // Do not skip the next element...
860 }
861 }
862 }
863
864 if (TypeToResolve) {
865 UR_OUT(" * Resolving upreference for "
866 << UpRefs[i].second->getDescription() << "\n";
867 std::string OldName = TypeToResolve->getDescription());
868 TypeToResolve->refineAbstractTypeTo(Ty);
869 }
870
871 return Ty;
872}
873
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000874//===----------------------------------------------------------------------===//
875// RunVMAsmParser - Define an interface to this parser
876//===----------------------------------------------------------------------===//
877//
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000878static Module* RunParser(Module * M);
879
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000880Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
881 set_scan_file(F);
882
883 CurFilename = Filename;
884 return RunParser(new Module(CurFilename));
885}
886
887Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
888 set_scan_string(AsmString);
889
890 CurFilename = "from_memory";
891 if (M == NULL) {
892 return RunParser(new Module (CurFilename));
893 } else {
894 return RunParser(M);
895 }
896}
897
898%}
899
900%union {
901 llvm::Module *ModuleVal;
902 llvm::Function *FunctionVal;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000903 llvm::BasicBlock *BasicBlockVal;
904 llvm::TerminatorInst *TermInstVal;
905 llvm::Instruction *InstVal;
Reid Spencere2c32da2006-12-03 05:46:11 +0000906 llvm::Constant *ConstVal;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000907
Reid Spencere2c32da2006-12-03 05:46:11 +0000908 const llvm::Type *PrimType;
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000909 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencere2c32da2006-12-03 05:46:11 +0000910 llvm::PATypeHolder *TypeVal;
911 llvm::Value *ValueVal;
Reid Spencere2c32da2006-12-03 05:46:11 +0000912 std::vector<llvm::Value*> *ValueList;
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000913 llvm::ArgListType *ArgList;
914 llvm::TypeWithAttrs TypeWithAttrs;
915 llvm::TypeWithAttrsList *TypeWithAttrsList;
916 llvm::ValueRefList *ValueRefList;
917
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000918 // Represent the RHS of PHI node
Reid Spencere2c32da2006-12-03 05:46:11 +0000919 std::list<std::pair<llvm::Value*,
920 llvm::BasicBlock*> > *PHIList;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000921 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencere2c32da2006-12-03 05:46:11 +0000922 std::vector<llvm::Constant*> *ConstVector;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000923
924 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000925 llvm::GlobalValue::VisibilityTypes Visibility;
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000926 llvm::FunctionType::ParameterAttributes ParamAttrs;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000927 int64_t SInt64Val;
928 uint64_t UInt64Val;
929 int SIntVal;
930 unsigned UIntVal;
931 double FPVal;
932 bool BoolVal;
933
934 char *StrVal; // This memory is strdup'd!
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000935 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000936
Reid Spencere2c32da2006-12-03 05:46:11 +0000937 llvm::Instruction::BinaryOps BinaryOpVal;
938 llvm::Instruction::TermOps TermOpVal;
939 llvm::Instruction::MemoryOps MemOpVal;
940 llvm::Instruction::CastOps CastOpVal;
941 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencere2c32da2006-12-03 05:46:11 +0000942 llvm::ICmpInst::Predicate IPredicate;
943 llvm::FCmpInst::Predicate FPredicate;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000944}
945
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000946%type <ModuleVal> Module
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000947%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
948%type <BasicBlockVal> BasicBlock InstructionList
949%type <TermInstVal> BBTerminatorInst
950%type <InstVal> Inst InstVal MemoryInst
951%type <ConstVal> ConstVal ConstExpr
952%type <ConstVector> ConstVector
953%type <ArgList> ArgList ArgListH
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000954%type <PHIList> PHIList
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000955%type <ValueRefList> ValueRefList // For call param lists & GEP indices
956%type <ValueList> IndexList // For GEP indices
957%type <TypeList> TypeListI
958%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencerbf48e3c2007-01-05 17:07:23 +0000959%type <TypeWithAttrs> ArgType
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000960%type <JumpTable> JumpTable
961%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
962%type <BoolVal> OptVolatile // 'volatile' or not
963%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
964%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000965%type <Linkage> GVInternalLinkage GVExternalLinkage
966%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000967%type <Visibility> GVVisibilityStyle
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000968
969// ValueRef - Unresolved reference to a definition or BB
970%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
971%type <ValueVal> ResolvedVal // <type> <valref> pair
972// Tokens and types for handling constant integer values
973//
974// ESINT64VAL - A negative number within long long range
975%token <SInt64Val> ESINT64VAL
976
977// EUINT64VAL - A positive number within uns. long long range
978%token <UInt64Val> EUINT64VAL
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000979
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000980%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000981%token <FPVal> FPVAL // Float or Double constant
982
983// Built in types...
Reid Spencerbf48e3c2007-01-05 17:07:23 +0000984%type <TypeVal> Types ResultTypes
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000985%type <PrimType> IntType FPType PrimType // Classifications
Reid Spencer58a8db02007-01-13 05:00:46 +0000986%token <PrimType> VOID INTTYPE
Reid Spencerdc0a3a22006-12-29 20:35:03 +0000987%token <PrimType> FLOAT DOUBLE LABEL
988%token TYPE
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000989
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000990%token<StrVal> LOCALVAR GLOBALVAR LABELSTR STRINGCONSTANT ATSTRINGCONSTANT
991%type <StrVal> LocalName OptLocalName OptLocalAssign
992%type <StrVal> GlobalName OptGlobalAssign
993%type <UIntVal> OptAlign OptCAlign
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000994%type <StrVal> OptSection SectionString
995
996%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Reid Spencerdc0a3a22006-12-29 20:35:03 +0000997%token DECLARE DEFINE GLOBAL CONSTANT SECTION VOLATILE
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000998%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000999%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001000%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001001%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001002%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattner7d1d0342006-10-22 06:08:13 +00001003%token DATALAYOUT
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001004%type <UIntVal> OptCallingConv
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001005%type <ParamAttrs> OptParamAttrs ParamAttr
1006%type <ParamAttrs> OptFuncAttrs FuncAttr
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001007
1008// Basic Block Terminating Operators
1009%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
1010
1011// Binary Operators
Reid Spencer266e42b2006-12-23 06:05:41 +00001012%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencerde46e482006-11-02 20:25:50 +00001013%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer2341c222007-02-02 02:16:23 +00001014%token <BinaryOpVal> SHL LSHR ASHR
1015
Reid Spencere2c32da2006-12-03 05:46:11 +00001016%token <OtherOpVal> ICMP FCMP
Reid Spencere2c32da2006-12-03 05:46:11 +00001017%type <IPredicate> IPredicates
Reid Spencere2c32da2006-12-03 05:46:11 +00001018%type <FPredicate> FPredicates
Reid Spencer1960ef32006-12-03 06:59:29 +00001019%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1020%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001021
1022// Memory Instructions
1023%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1024
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001025// Cast Operators
1026%type <CastOpVal> CastOps
1027%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1028%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1029
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001030// Other Operators
Reid Spencer2341c222007-02-02 02:16:23 +00001031%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattner9ff96a72006-04-08 01:18:56 +00001032%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001033
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001034// Function Attributes
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001035%token NORETURN INREG SRET
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001036
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001037// Visibility Styles
1038%token DEFAULT HIDDEN
1039
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001040%start Module
1041%%
1042
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001043
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001044// Operations that are notably excluded from this list include:
1045// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1046//
Reid Spencerde46e482006-11-02 20:25:50 +00001047ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer2341c222007-02-02 02:16:23 +00001048LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001049CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1050 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer2341c222007-02-02 02:16:23 +00001051
Reid Spencer1960ef32006-12-03 06:59:29 +00001052IPredicates
Reid Spencerd2e0c342006-12-04 05:24:24 +00001053 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer1960ef32006-12-03 06:59:29 +00001054 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1055 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1056 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1057 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1058 ;
1059
1060FPredicates
1061 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1062 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1063 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1064 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1065 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1066 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1067 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1068 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1069 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1070 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001071
1072// These are some types that allow classification if we only want a particular
1073// thing... for example, only a signed, unsigned, or integral type.
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001074IntType : INTTYPE;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001075FPType : FLOAT | DOUBLE;
1076
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001077LocalName : LOCALVAR | STRINGCONSTANT;
1078OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1079
1080/// OptLocalAssign - Value producing statements have an optional assignment
1081/// component.
1082OptLocalAssign : LocalName '=' {
1083 $$ = $1;
1084 CHECK_FOR_ERROR
1085 }
1086 | /*empty*/ {
1087 $$ = 0;
1088 CHECK_FOR_ERROR
1089 };
1090
1091GlobalName : GLOBALVAR | ATSTRINGCONSTANT;
1092
1093OptGlobalAssign : GlobalName '=' {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001094 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001095 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001096 }
1097 | /*empty*/ {
1098 $$ = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00001099 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001100 };
1101
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001102GVInternalLinkage
1103 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1104 | WEAK { $$ = GlobalValue::WeakLinkage; }
1105 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1106 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1107 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1108 ;
1109
1110GVExternalLinkage
1111 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1112 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1113 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1114 ;
1115
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001116GVVisibilityStyle
1117 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1118 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1119 ;
1120
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001121FunctionDeclareLinkage
1122 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1123 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1124 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001125 ;
1126
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001127FunctionDefineLinkage
1128 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1129 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001130 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1131 | WEAK { $$ = GlobalValue::WeakLinkage; }
1132 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001133 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001134
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +00001135OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1136 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +00001137 FASTCC_TOK { $$ = CallingConv::Fast; } |
1138 COLDCC_TOK { $$ = CallingConv::Cold; } |
1139 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1140 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1141 CC_TOK EUINT64VAL {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001142 if ((unsigned)$2 != $2)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001143 GEN_ERROR("Calling conv too large");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001144 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001145 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001146 };
1147
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001148ParamAttr : ZEXT { $$ = FunctionType::ZExtAttribute; }
1149 | SEXT { $$ = FunctionType::SExtAttribute; }
1150 | INREG { $$ = FunctionType::InRegAttribute; }
1151 | SRET { $$ = FunctionType::StructRetAttribute; }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001152 ;
1153
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001154OptParamAttrs : /* empty */ { $$ = FunctionType::NoAttributeSet; }
1155 | OptParamAttrs ParamAttr {
1156 $$ = FunctionType::ParameterAttributes($1 | $2);
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001157 }
1158 ;
1159
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001160FuncAttr : NORETURN { $$ = FunctionType::NoReturnAttribute; }
1161 | ParamAttr
1162 ;
1163
1164OptFuncAttrs : /* empty */ { $$ = FunctionType::NoAttributeSet; }
1165 | OptFuncAttrs FuncAttr {
1166 $$ = FunctionType::ParameterAttributes($1 | $2);
1167 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001168 ;
1169
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001170// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1171// a comma before it.
1172OptAlign : /*empty*/ { $$ = 0; } |
1173 ALIGN EUINT64VAL {
1174 $$ = $2;
1175 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001176 GEN_ERROR("Alignment must be a power of two");
Reid Spencer713eedc2006-08-18 08:43:06 +00001177 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001178};
1179OptCAlign : /*empty*/ { $$ = 0; } |
1180 ',' ALIGN EUINT64VAL {
1181 $$ = $3;
1182 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001183 GEN_ERROR("Alignment must be a power of two");
Reid Spencer713eedc2006-08-18 08:43:06 +00001184 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001185};
1186
1187
1188SectionString : SECTION STRINGCONSTANT {
1189 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1190 if ($2[i] == '"' || $2[i] == '\\')
Reid Spencera7bb3e92007-02-05 10:18:06 +00001191 GEN_ERROR("Invalid character in section name");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001192 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001193 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001194};
1195
1196OptSection : /*empty*/ { $$ = 0; } |
1197 SectionString { $$ = $1; };
1198
1199// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1200// is set to be the global we are processing.
1201//
1202GlobalVarAttributes : /* empty */ {} |
1203 ',' GlobalVarAttribute GlobalVarAttributes {};
1204GlobalVarAttribute : SectionString {
1205 CurGV->setSection($1);
1206 free($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001207 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001208 }
1209 | ALIGN EUINT64VAL {
1210 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001211 GEN_ERROR("Alignment must be a power of two");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001212 CurGV->setAlignment($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001213 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001214 };
1215
1216//===----------------------------------------------------------------------===//
1217// Types includes all predefined types... except void, because it can only be
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001218// used in specific contexts (function returning void for example).
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001219
1220// Derived types are added later...
1221//
Reid Spencer58a8db02007-01-13 05:00:46 +00001222PrimType : INTTYPE | FLOAT | DOUBLE | LABEL ;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001223
1224Types
1225 : OPAQUE {
Reid Spencere2c32da2006-12-03 05:46:11 +00001226 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer713eedc2006-08-18 08:43:06 +00001227 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001228 }
1229 | PrimType {
Reid Spencere2c32da2006-12-03 05:46:11 +00001230 $$ = new PATypeHolder($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001231 CHECK_FOR_ERROR
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001232 }
1233 | Types '*' { // Pointer type?
1234 if (*$1 == Type::LabelTy)
1235 GEN_ERROR("Cannot form a pointer to a basic block");
1236 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1237 delete $1;
1238 CHECK_FOR_ERROR
1239 }
1240 | SymbolicValueRef { // Named types are also simple types...
1241 const Type* tmp = getTypeVal($1);
1242 CHECK_FOR_ERROR
1243 $$ = new PATypeHolder(tmp);
1244 }
1245 | '\\' EUINT64VAL { // Type UpReference
Reid Spencera7bb3e92007-02-05 10:18:06 +00001246 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001247 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1248 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencere2c32da2006-12-03 05:46:11 +00001249 $$ = new PATypeHolder(OT);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001250 UR_OUT("New Upreference!\n");
Reid Spencer713eedc2006-08-18 08:43:06 +00001251 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001252 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001253 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001254 std::vector<const Type*> Params;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001255 std::vector<FunctionType::ParameterAttributes> Attrs;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001256 Attrs.push_back($5);
1257 for (TypeWithAttrsList::iterator I=$3->begin(), E=$3->end(); I != E; ++I) {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001258 Params.push_back(I->Ty->get());
1259 if (I->Ty->get() != Type::VoidTy)
1260 Attrs.push_back(I->Attrs);
1261 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001262 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1263 if (isVarArg) Params.pop_back();
1264
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001265 FunctionType *FT = FunctionType::get(*$1, Params, isVarArg, Attrs);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001266 delete $3; // Delete the argument list
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001267 delete $1; // Delete the return type handle
1268 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer713eedc2006-08-18 08:43:06 +00001269 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001270 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001271 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001272 std::vector<const Type*> Params;
1273 std::vector<FunctionType::ParameterAttributes> Attrs;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001274 Attrs.push_back($5);
1275 for (TypeWithAttrsList::iterator I=$3->begin(), E=$3->end(); I != E; ++I) {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001276 Params.push_back(I->Ty->get());
1277 if (I->Ty->get() != Type::VoidTy)
1278 Attrs.push_back(I->Attrs);
1279 }
1280 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1281 if (isVarArg) Params.pop_back();
1282
1283 FunctionType *FT = FunctionType::get($1, Params, isVarArg, Attrs);
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001284 delete $3; // Delete the argument list
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001285 $$ = new PATypeHolder(HandleUpRefs(FT));
1286 CHECK_FOR_ERROR
1287 }
1288
1289 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Reid Spencere2c32da2006-12-03 05:46:11 +00001290 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1291 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00001292 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001293 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001294 | '<' EUINT64VAL 'x' Types '>' { // Packed array type?
Reid Spencere2c32da2006-12-03 05:46:11 +00001295 const llvm::Type* ElemTy = $4->get();
1296 if ((unsigned)$2 != $2)
1297 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner03c49532007-01-15 02:27:26 +00001298 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001299 GEN_ERROR("Element type of a PackedType must be primitive");
Reid Spencere2c32da2006-12-03 05:46:11 +00001300 if (!isPowerOf2_32($2))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001301 GEN_ERROR("Vector length should be a power of 2");
Reid Spencere2c32da2006-12-03 05:46:11 +00001302 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
1303 delete $4;
1304 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001305 }
1306 | '{' TypeListI '}' { // Structure type?
1307 std::vector<const Type*> Elements;
Reid Spencere2c32da2006-12-03 05:46:11 +00001308 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001309 E = $2->end(); I != E; ++I)
Reid Spencere2c32da2006-12-03 05:46:11 +00001310 Elements.push_back(*I);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001311
Reid Spencere2c32da2006-12-03 05:46:11 +00001312 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001313 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001314 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001315 }
1316 | '{' '}' { // Empty structure type?
Reid Spencere2c32da2006-12-03 05:46:11 +00001317 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer713eedc2006-08-18 08:43:06 +00001318 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001319 }
Andrew Lenharth2d189ff2006-12-08 18:07:09 +00001320 | '<' '{' TypeListI '}' '>' {
1321 std::vector<const Type*> Elements;
1322 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1323 E = $3->end(); I != E; ++I)
1324 Elements.push_back(*I);
1325
1326 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1327 delete $3;
1328 CHECK_FOR_ERROR
1329 }
1330 | '<' '{' '}' '>' { // Empty structure type?
1331 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1332 CHECK_FOR_ERROR
1333 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001334 ;
1335
1336ArgType
1337 : Types OptParamAttrs {
1338 $$.Ty = $1;
1339 $$.Attrs = $2;
1340 }
1341 ;
1342
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001343ResultTypes
1344 : Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001345 if (!UpRefs.empty())
1346 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1347 if (!(*$1)->isFirstClassType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001348 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001349 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001350 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001351 | VOID {
1352 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001353 }
1354 ;
1355
1356ArgTypeList : ArgType {
1357 $$ = new TypeWithAttrsList();
1358 $$->push_back($1);
1359 CHECK_FOR_ERROR
1360 }
1361 | ArgTypeList ',' ArgType {
1362 ($$=$1)->push_back($3);
1363 CHECK_FOR_ERROR
1364 }
1365 ;
1366
1367ArgTypeListI
1368 : ArgTypeList
1369 | ArgTypeList ',' DOTDOTDOT {
1370 $$=$1;
1371 TypeWithAttrs TWA; TWA.Attrs = FunctionType::NoAttributeSet;
1372 TWA.Ty = new PATypeHolder(Type::VoidTy);
1373 $$->push_back(TWA);
1374 CHECK_FOR_ERROR
1375 }
1376 | DOTDOTDOT {
1377 $$ = new TypeWithAttrsList;
1378 TypeWithAttrs TWA; TWA.Attrs = FunctionType::NoAttributeSet;
1379 TWA.Ty = new PATypeHolder(Type::VoidTy);
1380 $$->push_back(TWA);
1381 CHECK_FOR_ERROR
1382 }
1383 | /*empty*/ {
1384 $$ = new TypeWithAttrsList();
Reid Spencer713eedc2006-08-18 08:43:06 +00001385 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001386 };
1387
1388// TypeList - Used for struct declarations and as a basis for function type
1389// declaration type lists
1390//
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001391TypeListI : Types {
Reid Spencere2c32da2006-12-03 05:46:11 +00001392 $$ = new std::list<PATypeHolder>();
1393 $$->push_back(*$1); delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001394 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001395 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001396 | TypeListI ',' Types {
Reid Spencere2c32da2006-12-03 05:46:11 +00001397 ($$=$1)->push_back(*$3); delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001398 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001399 };
1400
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001401// ConstVal - The various declarations that go into the constant pool. This
1402// production is used ONLY to represent constants that show up AFTER a 'const',
1403// 'constant' or 'global' token at global scope. Constants that can be inlined
1404// into other expressions (such as integers and constexprs) are handled by the
1405// ResolvedVal, ValueRef and ConstValueRef productions.
1406//
1407ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001408 if (!UpRefs.empty())
1409 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001410 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001411 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001412 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001413 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001414 const Type *ETy = ATy->getElementType();
1415 int NumElements = ATy->getNumElements();
1416
1417 // Verify that we have the correct size...
1418 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer713eedc2006-08-18 08:43:06 +00001419 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001420 utostr($3->size()) + " arguments, but has size of " +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001421 itostr(NumElements) + "");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001422
1423 // Verify all elements are correct type!
1424 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencere2c32da2006-12-03 05:46:11 +00001425 if (ETy != (*$3)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001426 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001427 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencere2c32da2006-12-03 05:46:11 +00001428 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001429 }
1430
Reid Spencere2c32da2006-12-03 05:46:11 +00001431 $$ = ConstantArray::get(ATy, *$3);
1432 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001433 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001434 }
1435 | Types '[' ']' {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001436 if (!UpRefs.empty())
1437 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001438 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001439 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001440 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001441 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001442
1443 int NumElements = ATy->getNumElements();
1444 if (NumElements != -1 && NumElements != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001445 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencera7bb3e92007-02-05 10:18:06 +00001446 " arguments, but has size of " + itostr(NumElements) +"");
Reid Spencere2c32da2006-12-03 05:46:11 +00001447 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1448 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001449 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001450 }
1451 | Types 'c' STRINGCONSTANT {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001452 if (!UpRefs.empty())
1453 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001454 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001455 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001456 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001457 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001458
1459 int NumElements = ATy->getNumElements();
1460 const Type *ETy = ATy->getElementType();
1461 char *EndStr = UnEscapeLexed($3, true);
1462 if (NumElements != -1 && NumElements != (EndStr-$3))
Reid Spencer713eedc2006-08-18 08:43:06 +00001463 GEN_ERROR("Can't build string constant of size " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001464 itostr((int)(EndStr-$3)) +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001465 " when array has size " + itostr(NumElements) + "");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001466 std::vector<Constant*> Vals;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001467 if (ETy == Type::Int8Ty) {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001468 for (unsigned char *C = (unsigned char *)$3;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001469 C != (unsigned char*)EndStr; ++C)
1470 Vals.push_back(ConstantInt::get(ETy, *C));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001471 } else {
1472 free($3);
Reid Spencera7bb3e92007-02-05 10:18:06 +00001473 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001474 }
1475 free($3);
Reid Spencere2c32da2006-12-03 05:46:11 +00001476 $$ = ConstantArray::get(ATy, Vals);
1477 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001478 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001479 }
1480 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001481 if (!UpRefs.empty())
1482 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001483 const PackedType *PTy = dyn_cast<PackedType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001484 if (PTy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001485 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001486 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001487 const Type *ETy = PTy->getElementType();
1488 int NumElements = PTy->getNumElements();
1489
1490 // Verify that we have the correct size...
1491 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer713eedc2006-08-18 08:43:06 +00001492 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001493 utostr($3->size()) + " arguments, but has size of " +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001494 itostr(NumElements) + "");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001495
1496 // Verify all elements are correct type!
1497 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencere2c32da2006-12-03 05:46:11 +00001498 if (ETy != (*$3)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001499 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001500 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencere2c32da2006-12-03 05:46:11 +00001501 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001502 }
1503
Reid Spencere2c32da2006-12-03 05:46:11 +00001504 $$ = ConstantPacked::get(PTy, *$3);
1505 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001506 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001507 }
1508 | Types '{' ConstVector '}' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001509 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001510 if (STy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001511 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001512 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001513
1514 if ($3->size() != STy->getNumContainedTypes())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001515 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001516
1517 // Check to ensure that constants are compatible with the type initializer!
1518 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencere2c32da2006-12-03 05:46:11 +00001519 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer713eedc2006-08-18 08:43:06 +00001520 GEN_ERROR("Expected type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001521 STy->getElementType(i)->getDescription() +
1522 "' for element #" + utostr(i) +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001523 " of structure initializer");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001524
Zhou Sheng75b871f2007-01-11 12:24:14 +00001525 // Check to ensure that Type is not packed
1526 if (STy->isPacked())
1527 GEN_ERROR("Unpacked Initializer to packed type '" + STy->getDescription() + "'");
1528
Reid Spencere2c32da2006-12-03 05:46:11 +00001529 $$ = ConstantStruct::get(STy, *$3);
1530 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001531 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001532 }
1533 | Types '{' '}' {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001534 if (!UpRefs.empty())
1535 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001536 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001537 if (STy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001538 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001539 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001540
1541 if (STy->getNumContainedTypes() != 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001542 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001543
Zhou Sheng75b871f2007-01-11 12:24:14 +00001544 // Check to ensure that Type is not packed
1545 if (STy->isPacked())
1546 GEN_ERROR("Unpacked Initializer to packed type '" + STy->getDescription() + "'");
1547
1548 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1549 delete $1;
1550 CHECK_FOR_ERROR
1551 }
1552 | Types '<' '{' ConstVector '}' '>' {
1553 const StructType *STy = dyn_cast<StructType>($1->get());
1554 if (STy == 0)
1555 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001556 (*$1)->getDescription() + "'");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001557
1558 if ($4->size() != STy->getNumContainedTypes())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001559 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001560
1561 // Check to ensure that constants are compatible with the type initializer!
1562 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1563 if ((*$4)[i]->getType() != STy->getElementType(i))
1564 GEN_ERROR("Expected type '" +
1565 STy->getElementType(i)->getDescription() +
1566 "' for element #" + utostr(i) +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001567 " of structure initializer");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001568
1569 // Check to ensure that Type is packed
1570 if (!STy->isPacked())
1571 GEN_ERROR("Packed Initializer to unpacked type '" + STy->getDescription() + "'");
1572
1573 $$ = ConstantStruct::get(STy, *$4);
1574 delete $1; delete $4;
1575 CHECK_FOR_ERROR
1576 }
1577 | Types '<' '{' '}' '>' {
1578 if (!UpRefs.empty())
1579 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1580 const StructType *STy = dyn_cast<StructType>($1->get());
1581 if (STy == 0)
1582 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001583 (*$1)->getDescription() + "'");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001584
1585 if (STy->getNumContainedTypes() != 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001586 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001587
1588 // Check to ensure that Type is packed
1589 if (!STy->isPacked())
1590 GEN_ERROR("Packed Initializer to unpacked type '" + STy->getDescription() + "'");
1591
Reid Spencere2c32da2006-12-03 05:46:11 +00001592 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1593 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001594 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001595 }
1596 | Types NULL_TOK {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001597 if (!UpRefs.empty())
1598 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001599 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001600 if (PTy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001601 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001602 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001603
Reid Spencere2c32da2006-12-03 05:46:11 +00001604 $$ = ConstantPointerNull::get(PTy);
1605 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001606 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001607 }
1608 | Types UNDEF {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001609 if (!UpRefs.empty())
1610 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001611 $$ = UndefValue::get($1->get());
1612 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001613 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001614 }
1615 | Types SymbolicValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001616 if (!UpRefs.empty())
1617 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001618 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001619 if (Ty == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001620 GEN_ERROR("Global const reference must be a pointer type");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001621
1622 // ConstExprs can exist in the body of a function, thus creating
1623 // GlobalValues whenever they refer to a variable. Because we are in
1624 // the context of a function, getValNonImprovising will search the functions
1625 // symbol table instead of the module symbol table for the global symbol,
1626 // which throws things all off. To get around this, we just tell
1627 // getValNonImprovising that we are at global scope here.
1628 //
1629 Function *SavedCurFn = CurFun.CurrentFunction;
1630 CurFun.CurrentFunction = 0;
1631
1632 Value *V = getValNonImprovising(Ty, $2);
Reid Spencer309080a2006-09-28 19:28:24 +00001633 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001634
1635 CurFun.CurrentFunction = SavedCurFn;
1636
1637 // If this is an initializer for a constant pointer, which is referencing a
1638 // (currently) undefined variable, create a stub now that shall be replaced
1639 // in the future with the right type of variable.
1640 //
1641 if (V == 0) {
Reid Spencerac6bfc52007-02-05 17:04:00 +00001642 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001643 const PointerType *PT = cast<PointerType>(Ty);
1644
1645 // First check to see if the forward references value is already created!
1646 PerModuleInfo::GlobalRefsType::iterator I =
1647 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1648
1649 if (I != CurModule.GlobalRefs.end()) {
1650 V = I->second; // Placeholder already exists, use it...
1651 $2.destroy();
1652 } else {
1653 std::string Name;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001654 if ($2.Type == ValID::GlobalName)
1655 Name = $2.Name;
1656 else if ($2.Type != ValID::GlobalID)
1657 GEN_ERROR("Invalid reference to global");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001658
1659 // Create the forward referenced global.
1660 GlobalValue *GV;
1661 if (const FunctionType *FTy =
1662 dyn_cast<FunctionType>(PT->getElementType())) {
1663 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1664 CurModule.CurrentModule);
1665 } else {
1666 GV = new GlobalVariable(PT->getElementType(), false,
1667 GlobalValue::ExternalLinkage, 0,
1668 Name, CurModule.CurrentModule);
1669 }
1670
1671 // Keep track of the fact that we have a forward ref to recycle it
1672 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1673 V = GV;
1674 }
1675 }
1676
Reid Spencere2c32da2006-12-03 05:46:11 +00001677 $$ = cast<GlobalValue>(V);
1678 delete $1; // Free the type handle
Reid Spencer713eedc2006-08-18 08:43:06 +00001679 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001680 }
1681 | Types ConstExpr {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001682 if (!UpRefs.empty())
1683 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001684 if ($1->get() != $2->getType())
Reid Spencerca3d1cb2007-01-04 00:06:14 +00001685 GEN_ERROR("Mismatched types for constant expression: " +
1686 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001687 $$ = $2;
Reid Spencere2c32da2006-12-03 05:46:11 +00001688 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001689 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001690 }
1691 | Types ZEROINITIALIZER {
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 const Type *Ty = $1->get();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001695 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001696 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencere2c32da2006-12-03 05:46:11 +00001697 $$ = Constant::getNullValue(Ty);
1698 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001699 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00001700 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001701 | IntType ESINT64VAL { // integral constants
Reid Spencer266e42b2006-12-23 06:05:41 +00001702 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001703 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencer266e42b2006-12-23 06:05:41 +00001704 $$ = ConstantInt::get($1, $2);
1705 CHECK_FOR_ERROR
1706 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001707 | IntType EUINT64VAL { // integral constants
Reid Spencer266e42b2006-12-23 06:05:41 +00001708 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001709 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencer266e42b2006-12-23 06:05:41 +00001710 $$ = ConstantInt::get($1, $2);
1711 CHECK_FOR_ERROR
1712 }
Reid Spencer58a8db02007-01-13 05:00:46 +00001713 | INTTYPE TRUETOK { // Boolean constants
1714 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001715 $$ = ConstantInt::getTrue();
Reid Spencer713eedc2006-08-18 08:43:06 +00001716 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001717 }
Reid Spencer58a8db02007-01-13 05:00:46 +00001718 | INTTYPE FALSETOK { // Boolean constants
1719 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001720 $$ = ConstantInt::getFalse();
Reid Spencer713eedc2006-08-18 08:43:06 +00001721 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001722 }
1723 | FPType FPVAL { // Float & Double constants
Reid Spencere2c32da2006-12-03 05:46:11 +00001724 if (!ConstantFP::isValueValidForType($1, $2))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001725 GEN_ERROR("Floating point constant invalid for type");
Reid Spencere2c32da2006-12-03 05:46:11 +00001726 $$ = ConstantFP::get($1, $2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001727 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001728 };
1729
1730
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001731ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001732 if (!UpRefs.empty())
1733 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001734 Constant *Val = $3;
Reid Spencerceb84592007-01-17 02:48:45 +00001735 const Type *DestTy = $5->get();
1736 if (!CastInst::castIsValid($1, $3, DestTy))
1737 GEN_ERROR("invalid cast opcode for cast from '" +
1738 Val->getType()->getDescription() + "' to '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001739 DestTy->getDescription() + "'");
Reid Spencerceb84592007-01-17 02:48:45 +00001740 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencere2c32da2006-12-03 05:46:11 +00001741 delete $5;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001742 }
1743 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001744 if (!isa<PointerType>($3->getType()))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001745 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001746
Reid Spencere2c32da2006-12-03 05:46:11 +00001747 const Type *IdxTy =
1748 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
1749 if (!IdxTy)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001750 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencere2c32da2006-12-03 05:46:11 +00001751
Chris Lattner55ce85c2007-01-31 04:44:08 +00001752 SmallVector<Constant*, 8> IdxVec;
Reid Spencere2c32da2006-12-03 05:46:11 +00001753 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1754 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001755 IdxVec.push_back(C);
1756 else
Reid Spencera7bb3e92007-02-05 10:18:06 +00001757 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001758
1759 delete $4;
1760
Chris Lattner55ce85c2007-01-31 04:44:08 +00001761 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer713eedc2006-08-18 08:43:06 +00001762 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001763 }
1764 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer542964f2007-01-11 18:21:29 +00001765 if ($3->getType() != Type::Int1Ty)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001766 GEN_ERROR("Select condition must be of boolean type");
Reid Spencere2c32da2006-12-03 05:46:11 +00001767 if ($5->getType() != $7->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001768 GEN_ERROR("Select operand types must match");
Reid Spencere2c32da2006-12-03 05:46:11 +00001769 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001770 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001771 }
1772 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001773 if ($3->getType() != $5->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001774 GEN_ERROR("Binary operator types must match");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001775 CHECK_FOR_ERROR;
Reid Spencer844668d2006-12-05 19:16:11 +00001776 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001777 }
1778 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001779 if ($3->getType() != $5->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001780 GEN_ERROR("Logical operator types must match");
Chris Lattner03c49532007-01-15 02:27:26 +00001781 if (!$3->getType()->isInteger()) {
Reid Spencer2341c222007-02-02 02:16:23 +00001782 if (Instruction::isShift($1) || !isa<PackedType>($3->getType()) ||
Chris Lattner03c49532007-01-15 02:27:26 +00001783 !cast<PackedType>($3->getType())->getElementType()->isInteger())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001784 GEN_ERROR("Logical operator requires integral operands");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001785 }
Reid Spencere2c32da2006-12-03 05:46:11 +00001786 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001787 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001788 }
Reid Spencerd2e0c342006-12-04 05:24:24 +00001789 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1790 if ($4->getType() != $6->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001791 GEN_ERROR("icmp operand types must match");
Reid Spencerd2e0c342006-12-04 05:24:24 +00001792 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencere2c32da2006-12-03 05:46:11 +00001793 }
Reid Spencerd2e0c342006-12-04 05:24:24 +00001794 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1795 if ($4->getType() != $6->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001796 GEN_ERROR("fcmp operand types must match");
Reid Spencerd2e0c342006-12-04 05:24:24 +00001797 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencere2c32da2006-12-03 05:46:11 +00001798 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001799 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001800 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001801 GEN_ERROR("Invalid extractelement operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00001802 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001803 CHECK_FOR_ERROR
Chris Lattneraebccf82006-04-08 03:55:17 +00001804 }
1805 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001806 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001807 GEN_ERROR("Invalid insertelement operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00001808 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001809 CHECK_FOR_ERROR
Chris Lattneraebccf82006-04-08 03:55:17 +00001810 }
1811 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001812 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001813 GEN_ERROR("Invalid shufflevector operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00001814 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001815 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001816 };
1817
Chris Lattneraebccf82006-04-08 03:55:17 +00001818
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001819// ConstVector - A list of comma separated constants.
1820ConstVector : ConstVector ',' ConstVal {
1821 ($$ = $1)->push_back($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001822 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001823 }
1824 | ConstVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00001825 $$ = new std::vector<Constant*>();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001826 $$->push_back($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001827 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001828 };
1829
1830
1831// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1832GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
1833
1834
1835//===----------------------------------------------------------------------===//
1836// Rules to match Modules
1837//===----------------------------------------------------------------------===//
1838
1839// Module rule: Capture the result of parsing the whole file into a result
1840// variable...
1841//
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001842Module
1843 : DefinitionList {
1844 $$ = ParserResult = CurModule.CurrentModule;
1845 CurModule.ModuleDone();
1846 CHECK_FOR_ERROR;
1847 }
1848 | /*empty*/ {
1849 $$ = ParserResult = CurModule.CurrentModule;
1850 CurModule.ModuleDone();
1851 CHECK_FOR_ERROR;
1852 }
1853 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001854
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001855DefinitionList
1856 : Definition
1857 | DefinitionList Definition
1858 ;
1859
1860Definition
Jeff Cohen5d956e42007-01-21 19:19:31 +00001861 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001862 CurFun.FunctionDone();
Reid Spencer713eedc2006-08-18 08:43:06 +00001863 CHECK_FOR_ERROR
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001864 }
1865 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer713eedc2006-08-18 08:43:06 +00001866 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001867 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001868 | MODULE ASM_TOK AsmBlock {
Reid Spencer713eedc2006-08-18 08:43:06 +00001869 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001870 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001871 | IMPLEMENTATION {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001872 // Emit an error if there are any unresolved types left.
1873 if (!CurModule.LateResolveTypes.empty()) {
1874 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001875 if (DID.Type == ValID::LocalName) {
Reid Spencer713eedc2006-08-18 08:43:06 +00001876 GEN_ERROR("Reference to an undefined type: '"+DID.getName() + "'");
1877 } else {
1878 GEN_ERROR("Reference to an undefined type: #" + itostr(DID.Num));
1879 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001880 }
Reid Spencer713eedc2006-08-18 08:43:06 +00001881 CHECK_FOR_ERROR
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001882 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001883 | OptLocalAssign TYPE Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001884 if (!UpRefs.empty())
1885 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001886 // Eagerly resolve types. This is not an optimization, this is a
1887 // requirement that is due to the fact that we could have this:
1888 //
1889 // %list = type { %list * }
1890 // %list = type { %list * } ; repeated type decl
1891 //
1892 // If types are not resolved eagerly, then the two types will not be
1893 // determined to be the same type!
1894 //
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001895 ResolveTypeTo($1, *$3);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001896
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001897 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer309080a2006-09-28 19:28:24 +00001898 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001899 // If this is a named type that is not a redefinition, add it to the slot
1900 // table.
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001901 CurModule.Types.push_back(*$3);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001902 }
Reid Spencere2c32da2006-12-03 05:46:11 +00001903
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001904 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001905 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001906 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001907 | OptLocalAssign TYPE VOID {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001908 ResolveTypeTo($1, $3);
1909
1910 if (!setTypeName($3, $1) && !$1) {
1911 CHECK_FOR_ERROR
1912 // If this is a named type that is not a redefinition, add it to the slot
1913 // table.
1914 CurModule.Types.push_back($3);
1915 }
1916 CHECK_FOR_ERROR
1917 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001918 | OptGlobalAssign GVVisibilityStyle GlobalType ConstVal {
1919 /* "Externally Visible" Linkage */
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001920 if ($4 == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001921 GEN_ERROR("Global value initializer is not a constant");
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001922 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
1923 $2, $3, $4->getType(), $4);
Reid Spencer309080a2006-09-28 19:28:24 +00001924 CHECK_FOR_ERROR
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001925 } GlobalVarAttributes {
1926 CurGV = 0;
1927 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001928 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle GlobalType ConstVal {
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001929 if ($5 == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001930 GEN_ERROR("Global value initializer is not a constant");
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001931 CurGV = ParseGlobalVariable($1, $2, $3, $4, $5->getType(), $5);
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001932 CHECK_FOR_ERROR
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001933 } GlobalVarAttributes {
1934 CurGV = 0;
1935 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001936 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle GlobalType Types {
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001937 if (!UpRefs.empty())
1938 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
1939 CurGV = ParseGlobalVariable($1, $2, $3, $4, *$5, 0);
1940 CHECK_FOR_ERROR
1941 delete $5;
Reid Spencer309080a2006-09-28 19:28:24 +00001942 } GlobalVarAttributes {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001943 CurGV = 0;
1944 CHECK_FOR_ERROR
1945 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001946 | TARGET TargetDefinition {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001947 CHECK_FOR_ERROR
1948 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001949 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer713eedc2006-08-18 08:43:06 +00001950 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001951 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001952 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001953
1954
1955AsmBlock : STRINGCONSTANT {
1956 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
1957 char *EndStr = UnEscapeLexed($1, true);
1958 std::string NewAsm($1, EndStr);
1959 free($1);
1960
1961 if (AsmSoFar.empty())
1962 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
1963 else
1964 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
Reid Spencer713eedc2006-08-18 08:43:06 +00001965 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001966};
1967
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001968TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001969 CurModule.CurrentModule->setTargetTriple($3);
1970 free($3);
John Criswell6af0b122006-10-24 19:09:48 +00001971 }
Chris Lattner7d1d0342006-10-22 06:08:13 +00001972 | DATALAYOUT '=' STRINGCONSTANT {
Owen Anderson85690f32006-10-18 02:21:48 +00001973 CurModule.CurrentModule->setDataLayout($3);
1974 free($3);
Owen Anderson85690f32006-10-18 02:21:48 +00001975 };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001976
1977LibrariesDefinition : '[' LibList ']';
1978
1979LibList : LibList ',' STRINGCONSTANT {
1980 CurModule.CurrentModule->addLibrary($3);
1981 free($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001982 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001983 }
1984 | STRINGCONSTANT {
1985 CurModule.CurrentModule->addLibrary($1);
1986 free($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001987 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001988 }
1989 | /* empty: end of list */ {
Reid Spencer713eedc2006-08-18 08:43:06 +00001990 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001991 }
1992 ;
1993
1994//===----------------------------------------------------------------------===//
1995// Rules to match Function Headers
1996//===----------------------------------------------------------------------===//
1997
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001998ArgListH : ArgListH ',' Types OptParamAttrs OptLocalName {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001999 if (!UpRefs.empty())
2000 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
2001 if (*$3 == Type::VoidTy)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002002 GEN_ERROR("void typed arguments are invalid");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002003 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002004 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002005 $1->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002006 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002007 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002008 | Types OptParamAttrs OptLocalName {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002009 if (!UpRefs.empty())
2010 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2011 if (*$1 == Type::VoidTy)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002012 GEN_ERROR("void typed arguments are invalid");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002013 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2014 $$ = new ArgListType;
2015 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002016 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002017 };
2018
2019ArgList : ArgListH {
2020 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002021 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002022 }
2023 | ArgListH ',' DOTDOTDOT {
2024 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002025 struct ArgListEntry E;
2026 E.Ty = new PATypeHolder(Type::VoidTy);
2027 E.Name = 0;
2028 E.Attrs = FunctionType::NoAttributeSet;
2029 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002030 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002031 }
2032 | DOTDOTDOT {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002033 $$ = new ArgListType;
2034 struct ArgListEntry E;
2035 E.Ty = new PATypeHolder(Type::VoidTy);
2036 E.Name = 0;
2037 E.Attrs = FunctionType::NoAttributeSet;
2038 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002039 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002040 }
2041 | /* empty */ {
2042 $$ = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00002043 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002044 };
2045
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002046FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002047 OptFuncAttrs OptSection OptAlign {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002048 UnEscapeLexed($3);
2049 std::string FunctionName($3);
2050 free($3); // Free strdup'd memory!
2051
Reid Spencer87622ae2007-01-02 21:54:12 +00002052 // Check the function result for abstractness if this is a define. We should
2053 // have no abstract types at this point
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002054 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2055 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer87622ae2007-01-02 21:54:12 +00002056
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002057 std::vector<const Type*> ParamTypeList;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002058 std::vector<FunctionType::ParameterAttributes> ParamAttrs;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002059 ParamAttrs.push_back($7);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002060 if ($5) { // If there are arguments...
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002061 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I) {
2062 const Type* Ty = I->Ty->get();
Reid Spencer87622ae2007-01-02 21:54:12 +00002063 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2064 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002065 ParamTypeList.push_back(Ty);
2066 if (Ty != Type::VoidTy)
2067 ParamAttrs.push_back(I->Attrs);
2068 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002069 }
2070
2071 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2072 if (isVarArg) ParamTypeList.pop_back();
2073
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002074 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg,
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002075 ParamAttrs);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002076 const PointerType *PFT = PointerType::get(FT);
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002077 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002078
2079 ValID ID;
2080 if (!FunctionName.empty()) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002081 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002082 } else {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002083 ID = ValID::createGlobalID(CurModule.Values[PFT].size());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002084 }
2085
2086 Function *Fn = 0;
2087 // See if this function was forward referenced. If so, recycle the object.
2088 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2089 // Move the function to the end of the list, from whereever it was
2090 // previously inserted.
2091 Fn = cast<Function>(FWRef);
2092 CurModule.CurrentModule->getFunctionList().remove(Fn);
2093 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2094 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002095 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
2096 if (Fn->getFunctionType() != FT ) {
2097 // The existing function doesn't have the same type. This is an overload
2098 // error.
2099 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
2100 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
2101 // Neither the existing or the current function is a declaration and they
2102 // have the same name and same type. Clearly this is a redefinition.
Reid Spencera7bb3e92007-02-05 10:18:06 +00002103 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002104 } if (Fn->isDeclaration()) {
2105 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002106 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2107 AI != AE; ++AI)
2108 AI->setName("");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002109 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002110 } else { // Not already defined?
2111 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
2112 CurModule.CurrentModule);
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002113
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002114 InsertValue(Fn, CurModule.Values);
2115 }
2116
2117 CurFun.FunctionStart(Fn);
Anton Korobeynikov0ab01ff2006-09-17 13:06:18 +00002118
2119 if (CurFun.isDeclare) {
2120 // If we have declaration, always overwrite linkage. This will allow us to
2121 // correctly handle cases, when pointer to function is passed as argument to
2122 // another function.
2123 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002124 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov0ab01ff2006-09-17 13:06:18 +00002125 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002126 Fn->setCallingConv($1);
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002127 Fn->setAlignment($9);
2128 if ($8) {
2129 Fn->setSection($8);
2130 free($8);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002131 }
2132
2133 // Add all of the arguments we parsed to the function...
2134 if ($5) { // Is null if empty...
2135 if (isVarArg) { // Nuke the last entry
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002136 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0 &&
Reid Spencerac6bfc52007-02-05 17:04:00 +00002137 "Not a varargs marker!");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002138 delete $5->back().Ty;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002139 $5->pop_back(); // Delete the last entry
2140 }
2141 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002142 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002143 unsigned Idx = 1;
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002144 for (ArgListType::iterator I = $5->begin();
2145 I != $5->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002146 delete I->Ty; // Delete the typeholder...
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002147 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer309080a2006-09-28 19:28:24 +00002148 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002149 InsertValue(ArgIt);
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002150 Idx++;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002151 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002152
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002153 delete $5; // We're now done with the argument list
2154 }
Reid Spencer713eedc2006-08-18 08:43:06 +00002155 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002156};
2157
2158BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2159
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002160FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002161 $$ = CurFun.CurrentFunction;
2162
2163 // Make sure that we keep track of the linkage type even if there was a
2164 // previous "declare".
2165 $$->setLinkage($1);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002166 $$->setVisibility($2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002167};
2168
2169END : ENDTOK | '}'; // Allow end of '}' to end a function
2170
2171Function : BasicBlockList END {
2172 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002173 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002174};
2175
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002176FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002177 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002178 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002179 $$ = CurFun.CurrentFunction;
2180 CurFun.FunctionDone();
2181 CHECK_FOR_ERROR
2182 };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002183
2184//===----------------------------------------------------------------------===//
2185// Rules to match Basic Blocks
2186//===----------------------------------------------------------------------===//
2187
2188OptSideEffect : /* empty */ {
2189 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002190 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002191 }
2192 | SIDEEFFECT {
2193 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002194 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002195 };
2196
2197ConstValueRef : ESINT64VAL { // A reference to a direct constant
2198 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002199 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002200 }
2201 | EUINT64VAL {
2202 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002203 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002204 }
2205 | FPVAL { // Perhaps it's an FP constant?
2206 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002207 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002208 }
2209 | TRUETOK {
Zhou Sheng75b871f2007-01-11 12:24:14 +00002210 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer713eedc2006-08-18 08:43:06 +00002211 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002212 }
2213 | FALSETOK {
Zhou Sheng75b871f2007-01-11 12:24:14 +00002214 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer713eedc2006-08-18 08:43:06 +00002215 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002216 }
2217 | NULL_TOK {
2218 $$ = ValID::createNull();
Reid Spencer713eedc2006-08-18 08:43:06 +00002219 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002220 }
2221 | UNDEF {
2222 $$ = ValID::createUndef();
Reid Spencer713eedc2006-08-18 08:43:06 +00002223 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002224 }
2225 | ZEROINITIALIZER { // A vector zero constant.
2226 $$ = ValID::createZeroInit();
Reid Spencer713eedc2006-08-18 08:43:06 +00002227 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002228 }
2229 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencere2c32da2006-12-03 05:46:11 +00002230 const Type *ETy = (*$2)[0]->getType();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002231 int NumElements = $2->size();
2232
2233 PackedType* pt = PackedType::get(ETy, NumElements);
2234 PATypeHolder* PTy = new PATypeHolder(
Reid Spencere2c32da2006-12-03 05:46:11 +00002235 HandleUpRefs(
2236 PackedType::get(
2237 ETy,
2238 NumElements)
2239 )
2240 );
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002241
2242 // Verify all elements are correct type!
2243 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencere2c32da2006-12-03 05:46:11 +00002244 if (ETy != (*$2)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002245 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002246 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00002247 (*$2)[i]->getType()->getDescription() + "'.");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002248 }
2249
Reid Spencere2c32da2006-12-03 05:46:11 +00002250 $$ = ValID::create(ConstantPacked::get(pt, *$2));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002251 delete PTy; delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002252 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002253 }
2254 | ConstExpr {
Reid Spencere2c32da2006-12-03 05:46:11 +00002255 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002256 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002257 }
2258 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
2259 char *End = UnEscapeLexed($3, true);
2260 std::string AsmStr = std::string($3, End);
2261 End = UnEscapeLexed($5, true);
2262 std::string Constraints = std::string($5, End);
2263 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
2264 free($3);
2265 free($5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002266 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002267 };
2268
2269// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2270// another value.
2271//
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002272SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2273 $$ = ValID::createLocalID($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002274 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002275 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002276 | GLOBALVAL_ID {
2277 $$ = ValID::createGlobalID($1);
2278 CHECK_FOR_ERROR
2279 }
2280 | LocalName { // Is it a named reference...?
2281 $$ = ValID::createLocalName($1);
2282 CHECK_FOR_ERROR
2283 }
2284 | GlobalName { // Is it a named reference...?
2285 $$ = ValID::createGlobalName($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002286 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002287 };
2288
2289// ValueRef - A reference to a definition... either constant or symbolic
2290ValueRef : SymbolicValueRef | ConstValueRef;
2291
2292
2293// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2294// type immediately preceeds the value reference, and allows complex constant
2295// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2296ResolvedVal : Types ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002297 if (!UpRefs.empty())
2298 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2299 $$ = getVal(*$1, $2);
2300 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002301 CHECK_FOR_ERROR
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002302 }
2303 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002304
2305BasicBlockList : BasicBlockList BasicBlock {
2306 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002307 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002308 }
2309 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
2310 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002311 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002312 };
2313
2314
2315// Basic blocks are terminated by branching instructions:
2316// br, br/cc, switch, ret
2317//
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002318BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002319 setValueName($3, $2);
Reid Spencer309080a2006-09-28 19:28:24 +00002320 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002321 InsertValue($3);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002322 $1->getInstList().push_back($3);
2323 InsertValue($1);
2324 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002325 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002326 };
2327
2328InstructionList : InstructionList Inst {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002329 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2330 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2331 if (CI2->getParent() == 0)
2332 $1->getInstList().push_back(CI2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002333 $1->getInstList().push_back($2);
2334 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002335 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002336 }
2337 | /* empty */ {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002338 $$ = getBBVal(ValID::createLocalID(CurFun.NextBBNum++), true);
Reid Spencer309080a2006-09-28 19:28:24 +00002339 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002340
2341 // Make sure to move the basic block to the correct location in the
2342 // function, instead of leaving it inserted wherever it was first
2343 // referenced.
2344 Function::BasicBlockListType &BBL =
2345 CurFun.CurrentFunction->getBasicBlockList();
2346 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer713eedc2006-08-18 08:43:06 +00002347 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002348 }
2349 | LABELSTR {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002350 $$ = getBBVal(ValID::createLocalName($1), true);
Reid Spencer309080a2006-09-28 19:28:24 +00002351 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002352
2353 // Make sure to move the basic block to the correct location in the
2354 // function, instead of leaving it inserted wherever it was first
2355 // referenced.
2356 Function::BasicBlockListType &BBL =
2357 CurFun.CurrentFunction->getBasicBlockList();
2358 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer713eedc2006-08-18 08:43:06 +00002359 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002360 };
2361
2362BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencere2c32da2006-12-03 05:46:11 +00002363 $$ = new ReturnInst($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00002364 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002365 }
2366 | RET VOID { // Return with no result...
2367 $$ = new ReturnInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002368 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002369 }
2370 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer309080a2006-09-28 19:28:24 +00002371 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002372 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002373 $$ = new BranchInst(tmpBB);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002374 } // Conditional Branch...
Reid Spencer58a8db02007-01-13 05:00:46 +00002375 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
2376 assert(cast<IntegerType>($2)->getBitWidth() == 1 && "Not Bool?");
Reid Spencer309080a2006-09-28 19:28:24 +00002377 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002378 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002379 BasicBlock* tmpBBB = getBBVal($9);
2380 CHECK_FOR_ERROR
Reid Spencer542964f2007-01-11 18:21:29 +00002381 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002382 CHECK_FOR_ERROR
2383 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002384 }
2385 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencere2c32da2006-12-03 05:46:11 +00002386 Value* tmpVal = getVal($2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002387 CHECK_FOR_ERROR
2388 BasicBlock* tmpBB = getBBVal($6);
2389 CHECK_FOR_ERROR
2390 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002391 $$ = S;
2392
2393 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2394 E = $8->end();
2395 for (; I != E; ++I) {
2396 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2397 S->addCase(CI, I->second);
2398 else
Reid Spencera7bb3e92007-02-05 10:18:06 +00002399 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002400 }
2401 delete $8;
Reid Spencer713eedc2006-08-18 08:43:06 +00002402 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002403 }
2404 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencere2c32da2006-12-03 05:46:11 +00002405 Value* tmpVal = getVal($2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002406 CHECK_FOR_ERROR
2407 BasicBlock* tmpBB = getBBVal($6);
2408 CHECK_FOR_ERROR
2409 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002410 $$ = S;
Reid Spencer713eedc2006-08-18 08:43:06 +00002411 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002412 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002413 | INVOKE OptCallingConv ResultTypes ValueRef '(' ValueRefList ')' OptFuncAttrs
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002414 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002415
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002416 // Handle the short syntax
2417 const PointerType *PFTy = 0;
2418 const FunctionType *Ty = 0;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002419 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002420 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2421 // Pull out the types of all of the arguments...
2422 std::vector<const Type*> ParamTypes;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002423 FunctionType::ParamAttrsList ParamAttrs;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002424 ParamAttrs.push_back($8);
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002425 for (ValueRefList::iterator I = $6->begin(), E = $6->end(); I != E; ++I) {
2426 const Type *Ty = I->Val->getType();
2427 if (Ty == Type::VoidTy)
2428 GEN_ERROR("Short call syntax cannot be used with varargs");
2429 ParamTypes.push_back(Ty);
2430 ParamAttrs.push_back(I->Attrs);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002431 }
2432
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002433 Ty = FunctionType::get($3->get(), ParamTypes, false, ParamAttrs);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002434 PFTy = PointerType::get(Ty);
2435 }
2436
2437 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer309080a2006-09-28 19:28:24 +00002438 CHECK_FOR_ERROR
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002439 BasicBlock *Normal = getBBVal($11);
Reid Spencer309080a2006-09-28 19:28:24 +00002440 CHECK_FOR_ERROR
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002441 BasicBlock *Except = getBBVal($14);
Reid Spencer309080a2006-09-28 19:28:24 +00002442 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002443
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002444 // Check the arguments
2445 ValueList Args;
2446 if ($6->empty()) { // Has no arguments?
2447 // Make sure no arguments is a good thing!
2448 if (Ty->getNumParams() != 0)
2449 GEN_ERROR("No arguments passed to a function that "
Reid Spencera7bb3e92007-02-05 10:18:06 +00002450 "expects arguments");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002451 } else { // Has arguments?
2452 // Loop through FunctionType's arguments and ensure they are specified
2453 // correctly!
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002454 FunctionType::param_iterator I = Ty->param_begin();
2455 FunctionType::param_iterator E = Ty->param_end();
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002456 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002457
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002458 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2459 if (ArgI->Val->getType() != *I)
2460 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00002461 (*I)->getDescription() + "'");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002462 Args.push_back(ArgI->Val);
2463 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002464
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002465 if (Ty->isVarArg()) {
2466 if (I == E)
2467 for (; ArgI != ArgE; ++ArgI)
2468 Args.push_back(ArgI->Val); // push the remaining varargs
2469 } else if (I != E || ArgI != ArgE)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002470 GEN_ERROR("Invalid number of parameters detected");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002471 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002472
2473 // Create the InvokeInst
2474 InvokeInst *II = new InvokeInst(V, Normal, Except, Args);
2475 II->setCallingConv($2);
2476 $$ = II;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002477 delete $6;
Reid Spencer713eedc2006-08-18 08:43:06 +00002478 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002479 }
2480 | UNWIND {
2481 $$ = new UnwindInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002482 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002483 }
2484 | UNREACHABLE {
2485 $$ = new UnreachableInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002486 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002487 };
2488
2489
2490
2491JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2492 $$ = $1;
Reid Spencere2c32da2006-12-03 05:46:11 +00002493 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Reid Spencer309080a2006-09-28 19:28:24 +00002494 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002495 if (V == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002496 GEN_ERROR("May only switch on a constant pool value");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002497
Reid Spencer309080a2006-09-28 19:28:24 +00002498 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002499 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002500 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002501 }
2502 | IntType ConstValueRef ',' LABEL ValueRef {
2503 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencere2c32da2006-12-03 05:46:11 +00002504 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Reid Spencer309080a2006-09-28 19:28:24 +00002505 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002506
2507 if (V == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002508 GEN_ERROR("May only switch on a constant pool value");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002509
Reid Spencer309080a2006-09-28 19:28:24 +00002510 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002511 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002512 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002513 };
2514
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002515Inst : OptLocalAssign InstVal {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002516 // Is this definition named?? if so, assign the name...
2517 setValueName($2, $1);
2518 CHECK_FOR_ERROR
2519 InsertValue($2);
2520 $$ = $2;
2521 CHECK_FOR_ERROR
2522 };
2523
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002524
2525PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002526 if (!UpRefs.empty())
2527 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002528 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencere2c32da2006-12-03 05:46:11 +00002529 Value* tmpVal = getVal(*$1, $3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002530 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002531 BasicBlock* tmpBB = getBBVal($5);
2532 CHECK_FOR_ERROR
2533 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencere2c32da2006-12-03 05:46:11 +00002534 delete $1;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002535 }
2536 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2537 $$ = $1;
Reid Spencer309080a2006-09-28 19:28:24 +00002538 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002539 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002540 BasicBlock* tmpBB = getBBVal($6);
2541 CHECK_FOR_ERROR
2542 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002543 };
2544
2545
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002546ValueRefList : Types ValueRef OptParamAttrs {
2547 if (!UpRefs.empty())
2548 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2549 // Used for call and invoke instructions
2550 $$ = new ValueRefList();
2551 ValueRefListEntry E; E.Attrs = $3; E.Val = getVal($1->get(), $2);
2552 $$->push_back(E);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002553 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002554 | ValueRefList ',' Types ValueRef OptParamAttrs {
2555 if (!UpRefs.empty())
2556 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002557 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002558 ValueRefListEntry E; E.Attrs = $5; E.Val = getVal($3->get(), $4);
2559 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002560 CHECK_FOR_ERROR
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002561 }
2562 | /*empty*/ { $$ = new ValueRefList(); };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002563
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002564IndexList // Used for gep instructions and constant expressions
Reid Spencerd0da3e22006-12-31 21:47:02 +00002565 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002566 | IndexList ',' ResolvedVal {
2567 $$ = $1;
2568 $$->push_back($3);
2569 CHECK_FOR_ERROR
2570 }
Reid Spencerd0da3e22006-12-31 21:47:02 +00002571 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002572
2573OptTailCall : TAIL CALL {
2574 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002575 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002576 }
2577 | CALL {
2578 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002579 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002580 };
2581
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002582InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002583 if (!UpRefs.empty())
2584 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner03c49532007-01-15 02:27:26 +00002585 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencere2c32da2006-12-03 05:46:11 +00002586 !isa<PackedType>((*$2).get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002587 GEN_ERROR(
Reid Spencera7bb3e92007-02-05 10:18:06 +00002588 "Arithmetic operator requires integer, FP, or packed operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00002589 if (isa<PackedType>((*$2).get()) &&
2590 ($1 == Instruction::URem ||
2591 $1 == Instruction::SRem ||
2592 $1 == Instruction::FRem))
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002593 GEN_ERROR("Remainder not supported on packed types");
Reid Spencere2c32da2006-12-03 05:46:11 +00002594 Value* val1 = getVal(*$2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002595 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002596 Value* val2 = getVal(*$2, $5);
Reid Spencer309080a2006-09-28 19:28:24 +00002597 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002598 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002599 if ($$ == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002600 GEN_ERROR("binary operator returned null");
Reid Spencere2c32da2006-12-03 05:46:11 +00002601 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002602 }
2603 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002604 if (!UpRefs.empty())
2605 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner03c49532007-01-15 02:27:26 +00002606 if (!(*$2)->isInteger()) {
Reid Spencer2341c222007-02-02 02:16:23 +00002607 if (Instruction::isShift($1) || !isa<PackedType>($2->get()) ||
Chris Lattner03c49532007-01-15 02:27:26 +00002608 !cast<PackedType>($2->get())->getElementType()->isInteger())
Reid Spencera7bb3e92007-02-05 10:18:06 +00002609 GEN_ERROR("Logical operator requires integral operands");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002610 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002611 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002612 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002613 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer309080a2006-09-28 19:28:24 +00002614 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002615 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002616 if ($$ == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002617 GEN_ERROR("binary operator returned null");
Reid Spencere2c32da2006-12-03 05:46:11 +00002618 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002619 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002620 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002621 if (!UpRefs.empty())
2622 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer494289f2007-01-04 02:57:52 +00002623 if (isa<PackedType>((*$3).get()))
2624 GEN_ERROR("Packed types not supported by icmp instruction");
Reid Spencere2c32da2006-12-03 05:46:11 +00002625 Value* tmpVal1 = getVal(*$3, $4);
2626 CHECK_FOR_ERROR
2627 Value* tmpVal2 = getVal(*$3, $6);
2628 CHECK_FOR_ERROR
2629 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2630 if ($$ == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002631 GEN_ERROR("icmp operator returned null");
Reid Spencere2c32da2006-12-03 05:46:11 +00002632 }
2633 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002634 if (!UpRefs.empty())
2635 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer494289f2007-01-04 02:57:52 +00002636 if (isa<PackedType>((*$3).get()))
2637 GEN_ERROR("Packed types not supported by fcmp instruction");
Reid Spencere2c32da2006-12-03 05:46:11 +00002638 Value* tmpVal1 = getVal(*$3, $4);
2639 CHECK_FOR_ERROR
2640 Value* tmpVal2 = getVal(*$3, $6);
2641 CHECK_FOR_ERROR
2642 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2643 if ($$ == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002644 GEN_ERROR("fcmp operator returned null");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002645 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002646 | CastOps ResolvedVal TO Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002647 if (!UpRefs.empty())
2648 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002649 Value* Val = $2;
Reid Spencerceb84592007-01-17 02:48:45 +00002650 const Type* DestTy = $4->get();
2651 if (!CastInst::castIsValid($1, Val, DestTy))
2652 GEN_ERROR("invalid cast opcode for cast from '" +
2653 Val->getType()->getDescription() + "' to '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00002654 DestTy->getDescription() + "'");
Reid Spencerceb84592007-01-17 02:48:45 +00002655 $$ = CastInst::create($1, Val, DestTy);
Reid Spencere2c32da2006-12-03 05:46:11 +00002656 delete $4;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002657 }
2658 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer542964f2007-01-11 18:21:29 +00002659 if ($2->getType() != Type::Int1Ty)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002660 GEN_ERROR("select condition must be boolean");
Reid Spencere2c32da2006-12-03 05:46:11 +00002661 if ($4->getType() != $6->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00002662 GEN_ERROR("select value types should match");
Reid Spencere2c32da2006-12-03 05:46:11 +00002663 $$ = new SelectInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002664 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002665 }
2666 | VAARG ResolvedVal ',' Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002667 if (!UpRefs.empty())
2668 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002669 $$ = new VAArgInst($2, *$4);
2670 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00002671 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002672 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002673 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002674 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencera7bb3e92007-02-05 10:18:06 +00002675 GEN_ERROR("Invalid extractelement operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00002676 $$ = new ExtractElementInst($2, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002677 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002678 }
2679 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002680 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencera7bb3e92007-02-05 10:18:06 +00002681 GEN_ERROR("Invalid insertelement operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00002682 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002683 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002684 }
Chris Lattner9ff96a72006-04-08 01:18:56 +00002685 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002686 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencera7bb3e92007-02-05 10:18:06 +00002687 GEN_ERROR("Invalid shufflevector operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00002688 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002689 CHECK_FOR_ERROR
Chris Lattner9ff96a72006-04-08 01:18:56 +00002690 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002691 | PHI_TOK PHIList {
2692 const Type *Ty = $2->front().first->getType();
2693 if (!Ty->isFirstClassType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00002694 GEN_ERROR("PHI node operands must be of first class type");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002695 $$ = new PHINode(Ty);
2696 ((PHINode*)$$)->reserveOperandSpace($2->size());
2697 while ($2->begin() != $2->end()) {
2698 if ($2->front().first->getType() != Ty)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002699 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002700 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
2701 $2->pop_front();
2702 }
2703 delete $2; // Free the list...
Reid Spencer713eedc2006-08-18 08:43:06 +00002704 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002705 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002706 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ValueRefList ')'
2707 OptFuncAttrs {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002708
2709 // Handle the short syntax
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002710 const PointerType *PFTy = 0;
2711 const FunctionType *Ty = 0;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002712 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002713 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2714 // Pull out the types of all of the arguments...
2715 std::vector<const Type*> ParamTypes;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002716 FunctionType::ParamAttrsList ParamAttrs;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002717 ParamAttrs.push_back($8);
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002718 for (ValueRefList::iterator I = $6->begin(), E = $6->end(); I != E; ++I) {
2719 const Type *Ty = I->Val->getType();
2720 if (Ty == Type::VoidTy)
2721 GEN_ERROR("Short call syntax cannot be used with varargs");
2722 ParamTypes.push_back(Ty);
2723 ParamAttrs.push_back(I->Attrs);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002724 }
2725
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002726 Ty = FunctionType::get($3->get(), ParamTypes, false, ParamAttrs);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002727 PFTy = PointerType::get(Ty);
2728 }
2729
2730 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer309080a2006-09-28 19:28:24 +00002731 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002732
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002733 // Check the arguments
2734 ValueList Args;
2735 if ($6->empty()) { // Has no arguments?
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002736 // Make sure no arguments is a good thing!
2737 if (Ty->getNumParams() != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002738 GEN_ERROR("No arguments passed to a function that "
Reid Spencera7bb3e92007-02-05 10:18:06 +00002739 "expects arguments");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002740 } else { // Has arguments?
2741 // Loop through FunctionType's arguments and ensure they are specified
2742 // correctly!
2743 //
2744 FunctionType::param_iterator I = Ty->param_begin();
2745 FunctionType::param_iterator E = Ty->param_end();
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002746 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002747
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002748 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2749 if (ArgI->Val->getType() != *I)
2750 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00002751 (*I)->getDescription() + "'");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002752 Args.push_back(ArgI->Val);
2753 }
2754 if (Ty->isVarArg()) {
2755 if (I == E)
2756 for (; ArgI != ArgE; ++ArgI)
2757 Args.push_back(ArgI->Val); // push the remaining varargs
2758 } else if (I != E || ArgI != ArgE)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002759 GEN_ERROR("Invalid number of parameters detected");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002760 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002761 // Create the call node
2762 CallInst *CI = new CallInst(V, Args);
2763 CI->setTailCall($1);
2764 CI->setCallingConv($2);
2765 $$ = CI;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002766 delete $6;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002767 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00002768 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002769 }
2770 | MemoryInst {
2771 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002772 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002773 };
2774
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002775OptVolatile : VOLATILE {
2776 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002777 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002778 }
2779 | /* empty */ {
2780 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002781 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002782 };
2783
2784
2785
2786MemoryInst : MALLOC Types OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002787 if (!UpRefs.empty())
2788 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002789 $$ = new MallocInst(*$2, 0, $3);
2790 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002791 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002792 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +00002793 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002794 if (!UpRefs.empty())
2795 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002796 Value* tmpVal = getVal($4, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002797 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002798 $$ = new MallocInst(*$2, tmpVal, $6);
2799 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002800 }
2801 | ALLOCA Types OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002802 if (!UpRefs.empty())
2803 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002804 $$ = new AllocaInst(*$2, 0, $3);
2805 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002806 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002807 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +00002808 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002809 if (!UpRefs.empty())
2810 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002811 Value* tmpVal = getVal($4, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002812 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002813 $$ = new AllocaInst(*$2, tmpVal, $6);
2814 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002815 }
2816 | FREE ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002817 if (!isa<PointerType>($2->getType()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002818 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencera7bb3e92007-02-05 10:18:06 +00002819 $2->getType()->getDescription() + "");
Reid Spencere2c32da2006-12-03 05:46:11 +00002820 $$ = new FreeInst($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00002821 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002822 }
2823
2824 | OptVolatile LOAD Types ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002825 if (!UpRefs.empty())
2826 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002827 if (!isa<PointerType>($3->get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002828 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencere2c32da2006-12-03 05:46:11 +00002829 (*$3)->getDescription());
2830 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002831 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencere2c32da2006-12-03 05:46:11 +00002832 (*$3)->getDescription());
2833 Value* tmpVal = getVal(*$3, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002834 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002835 $$ = new LoadInst(tmpVal, "", $1);
Reid Spencere2c32da2006-12-03 05:46:11 +00002836 delete $3;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002837 }
2838 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002839 if (!UpRefs.empty())
2840 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002841 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002842 if (!PT)
Reid Spencer713eedc2006-08-18 08:43:06 +00002843 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencere2c32da2006-12-03 05:46:11 +00002844 (*$5)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002845 const Type *ElTy = PT->getElementType();
Reid Spencere2c32da2006-12-03 05:46:11 +00002846 if (ElTy != $3->getType())
2847 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencera7bb3e92007-02-05 10:18:06 +00002848 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002849
Reid Spencere2c32da2006-12-03 05:46:11 +00002850 Value* tmpVal = getVal(*$5, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002851 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002852 $$ = new StoreInst($3, tmpVal, $1);
2853 delete $5;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002854 }
2855 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002856 if (!UpRefs.empty())
2857 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002858 if (!isa<PointerType>($2->get()))
Reid Spencera7bb3e92007-02-05 10:18:06 +00002859 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002860
Reid Spencere2c32da2006-12-03 05:46:11 +00002861 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Reid Spencer713eedc2006-08-18 08:43:06 +00002862 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00002863 (*$2)->getDescription()+ "'");
Reid Spencere2c32da2006-12-03 05:46:11 +00002864 Value* tmpVal = getVal(*$2, $3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002865 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002866 $$ = new GetElementPtrInst(tmpVal, *$4);
2867 delete $2;
Reid Spencer309080a2006-09-28 19:28:24 +00002868 delete $4;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002869 };
2870
2871
2872%%
Reid Spencer713eedc2006-08-18 08:43:06 +00002873
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002874// common code from the two 'RunVMAsmParser' functions
2875static Module* RunParser(Module * M) {
2876
2877 llvmAsmlineno = 1; // Reset the current line number...
2878 CurModule.CurrentModule = M;
2879#if YYDEBUG
2880 yydebug = Debug;
2881#endif
2882
2883 // Check to make sure the parser succeeded
2884 if (yyparse()) {
2885 if (ParserResult)
2886 delete ParserResult;
2887 return 0;
2888 }
2889
2890 // Check to make sure that parsing produced a result
2891 if (!ParserResult)
2892 return 0;
2893
2894 // Reset ParserResult variable while saving its value for the result.
2895 Module *Result = ParserResult;
2896 ParserResult = 0;
2897
2898 return Result;
2899}
2900
Reid Spencer713eedc2006-08-18 08:43:06 +00002901void llvm::GenerateError(const std::string &message, int LineNo) {
2902 if (LineNo == -1) LineNo = llvmAsmlineno;
2903 // TODO: column number in exception
2904 if (TheParseError)
2905 TheParseError->setError(CurFilename, message, LineNo);
2906 TriggerError = 1;
2907}
2908
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002909int yyerror(const char *ErrorMsg) {
2910 std::string where
2911 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2912 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002913 std::string errMsg = where + "error: " + std::string(ErrorMsg);
2914 if (yychar != YYEMPTY && yychar != 0)
2915 errMsg += " while reading token: '" + std::string(llvmAsmtext, llvmAsmleng)+
2916 "'";
Reid Spencer713eedc2006-08-18 08:43:06 +00002917 GenerateError(errMsg);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002918 return 0;
2919}