blob: ffa6be5444fed812430edafeb8eeb7450ecf4bb5 [file] [log] [blame]
Chris Lattner58af2a12006-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 Spenceref9b9a72007-02-05 20:47:22 +000020#include "llvm/ValueSymbolTable.h"
Chris Lattner58af2a12006-02-15 07:22:58 +000021#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer14310612006-12-31 05:40:51 +000022#include "llvm/Support/CommandLine.h"
Chris Lattnerf7469af2007-01-31 04:44:08 +000023#include "llvm/ADT/SmallVector.h"
Chris Lattner58af2a12006-02-15 07:22:58 +000024#include "llvm/ADT/STLExtras.h"
25#include "llvm/Support/MathExtras.h"
Reid Spencer481169e2006-12-01 00:33:46 +000026#include "llvm/Support/Streams.h"
Chris Lattner58af2a12006-02-15 07:22:58 +000027#include <algorithm>
Chris Lattner58af2a12006-02-15 07:22:58 +000028#include <list>
Chris Lattner8adde282007-02-11 21:40:10 +000029#include <map>
Chris Lattner58af2a12006-02-15 07:22:58 +000030#include <utility>
Reid Spencer14310612006-12-31 05:40:51 +000031#ifndef NDEBUG
32#define YYDEBUG 1
33#endif
Chris Lattner58af2a12006-02-15 07:22:58 +000034
Reid Spencere4f47592006-08-18 17:32:55 +000035// The following is a gross hack. In order to rid the libAsmParser library of
36// exceptions, we have to have a way of getting the yyparse function to go into
37// an error situation. So, whenever we want an error to occur, the GenerateError
38// function (see bottom of file) sets TriggerError. Then, at the end of each
39// production in the grammer we use CHECK_FOR_ERROR which will invoke YYERROR
40// (a goto) to put YACC in error state. Furthermore, several calls to
41// GenerateError are made from inside productions and they must simulate the
42// previous exception behavior by exiting the production immediately. We have
43// replaced these with the GEN_ERROR macro which calls GeneratError and then
44// immediately invokes YYERROR. This would be so much cleaner if it was a
45// recursive descent parser.
Reid Spencer61c83e02006-08-18 08:43:06 +000046static bool TriggerError = false;
Reid Spencerf63697d2006-10-09 17:36:59 +000047#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYABORT; } }
Reid Spencer61c83e02006-08-18 08:43:06 +000048#define GEN_ERROR(msg) { GenerateError(msg); YYERROR; }
49
Chris Lattner58af2a12006-02-15 07:22:58 +000050int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
51int yylex(); // declaration" of xxx warnings.
52int yyparse();
53
54namespace llvm {
55 std::string CurFilename;
Reid Spencer14310612006-12-31 05:40:51 +000056#if YYDEBUG
57static cl::opt<bool>
58Debug("debug-yacc", cl::desc("Print yacc debug state changes"),
59 cl::Hidden, cl::init(false));
60#endif
Chris Lattner58af2a12006-02-15 07:22:58 +000061}
62using namespace llvm;
63
64static Module *ParserResult;
65
66// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
67// relating to upreferences in the input stream.
68//
69//#define DEBUG_UPREFS 1
70#ifdef DEBUG_UPREFS
Bill Wendlinge8156192006-12-07 01:30:32 +000071#define UR_OUT(X) cerr << X
Chris Lattner58af2a12006-02-15 07:22:58 +000072#else
73#define UR_OUT(X)
74#endif
75
76#define YYERROR_VERBOSE 1
77
Chris Lattner58af2a12006-02-15 07:22:58 +000078static GlobalVariable *CurGV;
79
80
81// This contains info used when building the body of a function. It is
82// destroyed when the function is completed.
83//
84typedef std::vector<Value *> ValueList; // Numbered defs
Reid Spencer14310612006-12-31 05:40:51 +000085
Chris Lattner58af2a12006-02-15 07:22:58 +000086static void
87ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
88 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
89
90static struct PerModuleInfo {
91 Module *CurrentModule;
92 std::map<const Type *, ValueList> Values; // Module level numbered definitions
93 std::map<const Type *,ValueList> LateResolveValues;
Reid Spencer861d9d62006-11-28 07:29:44 +000094 std::vector<PATypeHolder> Types;
95 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner58af2a12006-02-15 07:22:58 +000096
97 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Chris Lattner0ad19702006-06-21 16:53:00 +000098 /// how they were referenced and on which line of the input they came from so
Chris Lattner58af2a12006-02-15 07:22:58 +000099 /// that we can resolve them later and print error messages as appropriate.
100 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
101
102 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
103 // references to global values. Global values may be referenced before they
104 // are defined, and if so, the temporary object that they represent is held
105 // here. This is used for forward references of GlobalValues.
106 //
107 typedef std::map<std::pair<const PointerType *,
108 ValID>, GlobalValue*> GlobalRefsType;
109 GlobalRefsType GlobalRefs;
110
111 void ModuleDone() {
112 // If we could not resolve some functions at function compilation time
113 // (calls to functions before they are defined), resolve them now... Types
114 // are resolved when the constant pool has been completely parsed.
115 //
116 ResolveDefinitions(LateResolveValues);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000117 if (TriggerError)
118 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000119
120 // Check to make sure that all global value forward references have been
121 // resolved!
122 //
123 if (!GlobalRefs.empty()) {
124 std::string UndefinedReferences = "Unresolved global references exist:\n";
125
126 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
127 I != E; ++I) {
128 UndefinedReferences += " " + I->first.first->getDescription() + " " +
129 I->first.second.getName() + "\n";
130 }
Reid Spencer61c83e02006-08-18 08:43:06 +0000131 GenerateError(UndefinedReferences);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000132 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000133 }
134
Chris Lattner58af2a12006-02-15 07:22:58 +0000135 Values.clear(); // Clear out function local definitions
136 Types.clear();
137 CurrentModule = 0;
138 }
139
140 // GetForwardRefForGlobal - Check to see if there is a forward reference
141 // for this global. If so, remove it from the GlobalRefs map and return it.
142 // If not, just return null.
143 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
144 // Check to see if there is a forward reference to this global variable...
145 // if there is, eliminate it and patch the reference to use the new def'n.
146 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
147 GlobalValue *Ret = 0;
148 if (I != GlobalRefs.end()) {
149 Ret = I->second;
150 GlobalRefs.erase(I);
151 }
152 return Ret;
153 }
Reid Spencer8c8a2dc2007-01-02 21:54:12 +0000154
155 bool TypeIsUnresolved(PATypeHolder* PATy) {
156 // If it isn't abstract, its resolved
157 const Type* Ty = PATy->get();
158 if (!Ty->isAbstract())
159 return false;
160 // Traverse the type looking for abstract types. If it isn't abstract then
161 // we don't need to traverse that leg of the type.
162 std::vector<const Type*> WorkList, SeenList;
163 WorkList.push_back(Ty);
164 while (!WorkList.empty()) {
165 const Type* Ty = WorkList.back();
166 SeenList.push_back(Ty);
167 WorkList.pop_back();
168 if (const OpaqueType* OpTy = dyn_cast<OpaqueType>(Ty)) {
169 // Check to see if this is an unresolved type
170 std::map<ValID, PATypeHolder>::iterator I = LateResolveTypes.begin();
171 std::map<ValID, PATypeHolder>::iterator E = LateResolveTypes.end();
172 for ( ; I != E; ++I) {
173 if (I->second.get() == OpTy)
174 return true;
175 }
176 } else if (const SequentialType* SeqTy = dyn_cast<SequentialType>(Ty)) {
177 const Type* TheTy = SeqTy->getElementType();
178 if (TheTy->isAbstract() && TheTy != Ty) {
179 std::vector<const Type*>::iterator I = SeenList.begin(),
180 E = SeenList.end();
181 for ( ; I != E; ++I)
182 if (*I == TheTy)
183 break;
184 if (I == E)
185 WorkList.push_back(TheTy);
186 }
187 } else if (const StructType* StrTy = dyn_cast<StructType>(Ty)) {
188 for (unsigned i = 0; i < StrTy->getNumElements(); ++i) {
189 const Type* TheTy = StrTy->getElementType(i);
190 if (TheTy->isAbstract() && TheTy != Ty) {
191 std::vector<const Type*>::iterator I = SeenList.begin(),
192 E = SeenList.end();
193 for ( ; I != E; ++I)
194 if (*I == TheTy)
195 break;
196 if (I == E)
197 WorkList.push_back(TheTy);
198 }
199 }
200 }
201 }
202 return false;
203 }
204
205
Chris Lattner58af2a12006-02-15 07:22:58 +0000206} CurModule;
207
208static struct PerFunctionInfo {
209 Function *CurrentFunction; // Pointer to current function being created
210
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000211 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
Chris Lattner58af2a12006-02-15 07:22:58 +0000212 std::map<const Type*, ValueList> LateResolveValues;
Reid Spenceref9b9a72007-02-05 20:47:22 +0000213 bool isDeclare; // Is this function a forward declararation?
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000214 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000215 GlobalValue::VisibilityTypes Visibility;
Chris Lattner58af2a12006-02-15 07:22:58 +0000216
217 /// BBForwardRefs - When we see forward references to basic blocks, keep
218 /// track of them here.
219 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
220 std::vector<BasicBlock*> NumberedBlocks;
221 unsigned NextBBNum;
222
223 inline PerFunctionInfo() {
224 CurrentFunction = 0;
225 isDeclare = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000226 Linkage = GlobalValue::ExternalLinkage;
227 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner58af2a12006-02-15 07:22:58 +0000228 }
229
230 inline void FunctionStart(Function *M) {
231 CurrentFunction = M;
232 NextBBNum = 0;
233 }
234
235 void FunctionDone() {
236 NumberedBlocks.clear();
237
238 // Any forward referenced blocks left?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000239 if (!BBForwardRefs.empty()) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000240 GenerateError("Undefined reference to label " +
Chris Lattner58af2a12006-02-15 07:22:58 +0000241 BBForwardRefs.begin()->first->getName());
Reid Spencer5b7e7532006-09-28 19:28:24 +0000242 return;
243 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000244
245 // Resolve all forward references now.
246 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
247
248 Values.clear(); // Clear out function local definitions
249 CurrentFunction = 0;
250 isDeclare = false;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000251 Linkage = GlobalValue::ExternalLinkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000252 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner58af2a12006-02-15 07:22:58 +0000253 }
254} CurFun; // Info for the current function...
255
256static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
257
258
259//===----------------------------------------------------------------------===//
260// Code to handle definitions of all the types
261//===----------------------------------------------------------------------===//
262
263static int InsertValue(Value *V,
264 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
265 if (V->hasName()) return -1; // Is this a numbered definition?
266
267 // Yes, insert the value into the value table...
268 ValueList &List = ValueTab[V->getType()];
269 List.push_back(V);
270 return List.size()-1;
271}
272
273static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
274 switch (D.Type) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000275 case ValID::LocalID: // Is it a numbered definition?
Chris Lattner58af2a12006-02-15 07:22:58 +0000276 // Module constants occupy the lowest numbered slots...
Reid Spencer41dff5e2007-01-26 08:05:27 +0000277 if (D.Num < CurModule.Types.size())
278 return CurModule.Types[D.Num];
Chris Lattner58af2a12006-02-15 07:22:58 +0000279 break;
Reid Spencer41dff5e2007-01-26 08:05:27 +0000280 case ValID::LocalName: // Is it a named definition?
Chris Lattner58af2a12006-02-15 07:22:58 +0000281 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
282 D.destroy(); // Free old strdup'd memory...
283 return N;
284 }
285 break;
286 default:
Reid Spencerb5334b02007-02-05 10:18:06 +0000287 GenerateError("Internal parser error: Invalid symbol type reference");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000288 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000289 }
290
291 // If we reached here, we referenced either a symbol that we don't know about
292 // or an id number that hasn't been read yet. We may be referencing something
293 // forward, so just create an entry to be resolved later and get to it...
294 //
295 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
296
297
298 if (inFunctionScope()) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000299 if (D.Type == ValID::LocalName) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000300 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000301 return 0;
302 } else {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000303 GenerateError("Reference to an undefined type: #" + utostr(D.Num));
Reid Spencer5b7e7532006-09-28 19:28:24 +0000304 return 0;
305 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000306 }
307
Reid Spencer861d9d62006-11-28 07:29:44 +0000308 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Chris Lattner58af2a12006-02-15 07:22:58 +0000309 if (I != CurModule.LateResolveTypes.end())
Reid Spencer861d9d62006-11-28 07:29:44 +0000310 return I->second;
Chris Lattner58af2a12006-02-15 07:22:58 +0000311
Reid Spencer861d9d62006-11-28 07:29:44 +0000312 Type *Typ = OpaqueType::get();
313 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
314 return Typ;
Reid Spencera132e042006-12-03 05:46:11 +0000315 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000316
Chris Lattner58af2a12006-02-15 07:22:58 +0000317// getValNonImprovising - Look up the value specified by the provided type and
318// the provided ValID. If the value exists and has already been defined, return
319// it. Otherwise return null.
320//
321static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000322 if (isa<FunctionType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000323 GenerateError("Functions are not values and "
Chris Lattner58af2a12006-02-15 07:22:58 +0000324 "must be referenced as pointers");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000325 return 0;
326 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000327
328 switch (D.Type) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000329 case ValID::LocalID: { // Is it a numbered definition?
330 // Module constants occupy the lowest numbered slots.
331 std::map<const Type*,ValueList>::iterator VI = CurFun.Values.find(Ty);
332 // Make sure that our type is within bounds.
Chris Lattner58af2a12006-02-15 07:22:58 +0000333 if (VI == CurFun.Values.end()) return 0;
334
Reid Spencer41dff5e2007-01-26 08:05:27 +0000335 // Check that the number is within bounds.
336 if (D.Num >= VI->second.size()) return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000337
Reid Spencer41dff5e2007-01-26 08:05:27 +0000338 return VI->second[D.Num];
339 }
340 case ValID::GlobalID: { // Is it a numbered definition?
341 unsigned Num = D.Num;
342
343 // Module constants occupy the lowest numbered slots...
344 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Reid Spenceref9b9a72007-02-05 20:47:22 +0000345 if (VI == CurModule.Values.end())
346 return 0;
347 if (D.Num >= VI->second.size())
348 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000349 return VI->second[Num];
350 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000351
352 case ValID::LocalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000353 if (!inFunctionScope())
354 return 0;
355 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
356 Value *N = SymTab.lookup(D.Name);
357 if (N == 0)
358 return 0;
359 if (N->getType() != Ty)
360 return 0;
Reid Spencer41dff5e2007-01-26 08:05:27 +0000361
362 D.destroy(); // Free old strdup'd memory...
363 return N;
364 }
365 case ValID::GlobalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000366 ValueSymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable();
367 Value *N = SymTab.lookup(D.Name);
368 if (N == 0)
369 return 0;
370 if (N->getType() != Ty)
371 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000372
373 D.destroy(); // Free old strdup'd memory...
374 return N;
375 }
376
377 // Check to make sure that "Ty" is an integral type, and that our
378 // value will fit into the specified type...
379 case ValID::ConstSIntVal: // Is it a constant pool reference??
Reid Spencerb83eb642006-10-20 07:07:24 +0000380 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000381 GenerateError("Signed integral constant '" +
Chris Lattner58af2a12006-02-15 07:22:58 +0000382 itostr(D.ConstPool64) + "' is invalid for type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000383 Ty->getDescription() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000384 return 0;
385 }
Reid Spencerb83eb642006-10-20 07:07:24 +0000386 return ConstantInt::get(Ty, D.ConstPool64);
Chris Lattner58af2a12006-02-15 07:22:58 +0000387
388 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Reid Spencerb83eb642006-10-20 07:07:24 +0000389 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
390 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000391 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Reid Spencerb5334b02007-02-05 10:18:06 +0000392 "' is invalid or out of range");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000393 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000394 } else { // This is really a signed reference. Transmogrify.
Reid Spencerb83eb642006-10-20 07:07:24 +0000395 return ConstantInt::get(Ty, D.ConstPool64);
Chris Lattner58af2a12006-02-15 07:22:58 +0000396 }
397 } else {
Reid Spencerb83eb642006-10-20 07:07:24 +0000398 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattner58af2a12006-02-15 07:22:58 +0000399 }
400
401 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000402 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000403 GenerateError("FP constant invalid for type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000404 return 0;
405 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000406 return ConstantFP::get(Ty, D.ConstPoolFP);
407
408 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000409 if (!isa<PointerType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000410 GenerateError("Cannot create a a non pointer null");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000411 return 0;
412 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000413 return ConstantPointerNull::get(cast<PointerType>(Ty));
414
415 case ValID::ConstUndefVal: // Is it an undef value?
416 return UndefValue::get(Ty);
417
418 case ValID::ConstZeroVal: // Is it a zero value?
419 return Constant::getNullValue(Ty);
420
421 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000422 if (D.ConstantValue->getType() != Ty) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000423 GenerateError("Constant expression type different from required type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000424 return 0;
425 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000426 return D.ConstantValue;
427
428 case ValID::InlineAsmVal: { // Inline asm expression
429 const PointerType *PTy = dyn_cast<PointerType>(Ty);
430 const FunctionType *FTy =
431 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000432 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000433 GenerateError("Invalid type for asm constraint string");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000434 return 0;
435 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000436 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
437 D.IAD->HasSideEffects);
438 D.destroy(); // Free InlineAsmDescriptor.
439 return IA;
440 }
441 default:
Reid Spencera9720f52007-02-05 17:04:00 +0000442 assert(0 && "Unhandled case!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000443 return 0;
444 } // End of switch
445
Reid Spencera9720f52007-02-05 17:04:00 +0000446 assert(0 && "Unhandled case!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000447 return 0;
448}
449
450// getVal - This function is identical to getValNonImprovising, except that if a
451// value is not already defined, it "improvises" by creating a placeholder var
452// that looks and acts just like the requested variable. When the value is
453// defined later, all uses of the placeholder variable are replaced with the
454// real thing.
455//
456static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000457 if (Ty == Type::LabelTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000458 GenerateError("Cannot use a basic block here");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000459 return 0;
460 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000461
462 // See if the value has already been defined.
463 Value *V = getValNonImprovising(Ty, ID);
464 if (V) return V;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000465 if (TriggerError) return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000466
Reid Spencer5b7e7532006-09-28 19:28:24 +0000467 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000468 GenerateError("Invalid use of a composite type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000469 return 0;
470 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000471
472 // If we reached here, we referenced either a symbol that we don't know about
473 // or an id number that hasn't been read yet. We may be referencing something
474 // forward, so just create an entry to be resolved later and get to it...
475 //
476 V = new Argument(Ty);
477
478 // Remember where this forward reference came from. FIXME, shouldn't we try
479 // to recycle these things??
480 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
481 llvmAsmlineno)));
482
483 if (inFunctionScope())
484 InsertValue(V, CurFun.LateResolveValues);
485 else
486 InsertValue(V, CurModule.LateResolveValues);
487 return V;
488}
489
490/// getBBVal - This is used for two purposes:
491/// * If isDefinition is true, a new basic block with the specified ID is being
492/// defined.
493/// * If isDefinition is true, this is a reference to a basic block, which may
494/// or may not be a forward reference.
495///
496static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
Reid Spencera9720f52007-02-05 17:04:00 +0000497 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000498
499 std::string Name;
500 BasicBlock *BB = 0;
501 switch (ID.Type) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000502 default:
503 GenerateError("Illegal label reference " + ID.getName());
504 return 0;
Reid Spencer41dff5e2007-01-26 08:05:27 +0000505 case ValID::LocalID: // Is it a numbered definition?
506 if (ID.Num >= CurFun.NumberedBlocks.size())
Chris Lattner58af2a12006-02-15 07:22:58 +0000507 CurFun.NumberedBlocks.resize(ID.Num+1);
508 BB = CurFun.NumberedBlocks[ID.Num];
509 break;
Reid Spencer41dff5e2007-01-26 08:05:27 +0000510 case ValID::LocalName: // Is it a named definition?
Chris Lattner58af2a12006-02-15 07:22:58 +0000511 Name = ID.Name;
Reid Spenceref9b9a72007-02-05 20:47:22 +0000512 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
513 if (N && N->getType()->getTypeID() == Type::LabelTyID)
Chris Lattner58af2a12006-02-15 07:22:58 +0000514 BB = cast<BasicBlock>(N);
515 break;
516 }
517
518 // See if the block has already been defined.
519 if (BB) {
520 // If this is the definition of the block, make sure the existing value was
521 // just a forward reference. If it was a forward reference, there will be
522 // an entry for it in the PlaceHolderInfo map.
Reid Spencer5b7e7532006-09-28 19:28:24 +0000523 if (isDefinition && !CurFun.BBForwardRefs.erase(BB)) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000524 // The existing value was a definition, not a forward reference.
Reid Spencer61c83e02006-08-18 08:43:06 +0000525 GenerateError("Redefinition of label " + ID.getName());
Reid Spencer5b7e7532006-09-28 19:28:24 +0000526 return 0;
527 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000528
529 ID.destroy(); // Free strdup'd memory.
530 return BB;
531 }
532
533 // Otherwise this block has not been seen before.
534 BB = new BasicBlock("", CurFun.CurrentFunction);
Reid Spencer41dff5e2007-01-26 08:05:27 +0000535 if (ID.Type == ValID::LocalName) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000536 BB->setName(ID.Name);
537 } else {
538 CurFun.NumberedBlocks[ID.Num] = BB;
539 }
540
541 // If this is not a definition, keep track of it so we can use it as a forward
542 // reference.
543 if (!isDefinition) {
544 // Remember where this forward reference came from.
545 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
546 } else {
547 // The forward declaration could have been inserted anywhere in the
548 // function: insert it into the correct place now.
549 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
550 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
551 }
552 ID.destroy();
553 return BB;
554}
555
556
557//===----------------------------------------------------------------------===//
558// Code to handle forward references in instructions
559//===----------------------------------------------------------------------===//
560//
561// This code handles the late binding needed with statements that reference
562// values not defined yet... for example, a forward branch, or the PHI node for
563// a loop body.
564//
565// This keeps a table (CurFun.LateResolveValues) of all such forward references
566// and back patchs after we are done.
567//
568
569// ResolveDefinitions - If we could not resolve some defs at parsing
570// time (forward branches, phi functions for loops, etc...) resolve the
571// defs now...
572//
573static void
574ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
575 std::map<const Type*,ValueList> *FutureLateResolvers) {
576 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
577 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
578 E = LateResolvers.end(); LRI != E; ++LRI) {
579 ValueList &List = LRI->second;
580 while (!List.empty()) {
581 Value *V = List.back();
582 List.pop_back();
583
584 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
585 CurModule.PlaceHolderInfo.find(V);
Reid Spencera9720f52007-02-05 17:04:00 +0000586 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000587
588 ValID &DID = PHI->second.first;
589
590 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000591 if (TriggerError)
592 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000593 if (TheRealValue) {
594 V->replaceAllUsesWith(TheRealValue);
595 delete V;
596 CurModule.PlaceHolderInfo.erase(PHI);
597 } else if (FutureLateResolvers) {
598 // Functions have their unresolved items forwarded to the module late
599 // resolver table
600 InsertValue(V, *FutureLateResolvers);
601 } else {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000602 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000603 GenerateError("Reference to an invalid definition: '" +DID.getName()+
Chris Lattner58af2a12006-02-15 07:22:58 +0000604 "' of type '" + V->getType()->getDescription() + "'",
605 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000606 return;
607 } else {
Reid Spencer61c83e02006-08-18 08:43:06 +0000608 GenerateError("Reference to an invalid definition: #" +
Chris Lattner58af2a12006-02-15 07:22:58 +0000609 itostr(DID.Num) + " of type '" +
610 V->getType()->getDescription() + "'",
611 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000612 return;
613 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000614 }
615 }
616 }
617
618 LateResolvers.clear();
619}
620
621// ResolveTypeTo - A brand new type was just declared. This means that (if
622// name is not null) things referencing Name can be resolved. Otherwise, things
623// refering to the number can be resolved. Do this now.
624//
625static void ResolveTypeTo(char *Name, const Type *ToTy) {
626 ValID D;
Reid Spencer41dff5e2007-01-26 08:05:27 +0000627 if (Name) D = ValID::createLocalName(Name);
628 else D = ValID::createLocalID(CurModule.Types.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000629
Reid Spencer861d9d62006-11-28 07:29:44 +0000630 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattner58af2a12006-02-15 07:22:58 +0000631 CurModule.LateResolveTypes.find(D);
632 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencer861d9d62006-11-28 07:29:44 +0000633 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner58af2a12006-02-15 07:22:58 +0000634 CurModule.LateResolveTypes.erase(I);
635 }
636}
637
638// setValueName - Set the specified value to the name given. The name may be
639// null potentially, in which case this is a noop. The string passed in is
640// assumed to be a malloc'd string buffer, and is free'd by this function.
641//
642static void setValueName(Value *V, char *NameStr) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000643 if (!NameStr) return;
644 std::string Name(NameStr); // Copy string
645 free(NameStr); // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000646
Reid Spencer41dff5e2007-01-26 08:05:27 +0000647 if (V->getType() == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000648 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000649 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000650 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000651
Reid Spencera9720f52007-02-05 17:04:00 +0000652 assert(inFunctionScope() && "Must be in function scope!");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000653 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
654 if (ST.lookup(Name)) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000655 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000656 V->getType()->getDescription() + "'");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000657 return;
658 }
659
660 // Set the name.
661 V->setName(Name);
Chris Lattner58af2a12006-02-15 07:22:58 +0000662}
663
664/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
665/// this is a declaration, otherwise it is a definition.
666static GlobalVariable *
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000667ParseGlobalVariable(char *NameStr,
668 GlobalValue::LinkageTypes Linkage,
669 GlobalValue::VisibilityTypes Visibility,
Chris Lattner58af2a12006-02-15 07:22:58 +0000670 bool isConstantGlobal, const Type *Ty,
671 Constant *Initializer) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000672 if (isa<FunctionType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000673 GenerateError("Cannot declare global vars of function type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000674 return 0;
675 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000676
677 const PointerType *PTy = PointerType::get(Ty);
678
679 std::string Name;
680 if (NameStr) {
681 Name = NameStr; // Copy string
682 free(NameStr); // Free old string
683 }
684
685 // See if this global value was forward referenced. If so, recycle the
686 // object.
687 ValID ID;
688 if (!Name.empty()) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000689 ID = ValID::createGlobalName((char*)Name.c_str());
Chris Lattner58af2a12006-02-15 07:22:58 +0000690 } else {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000691 ID = ValID::createGlobalID(CurModule.Values[PTy].size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000692 }
693
694 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
695 // Move the global to the end of the list, from whereever it was
696 // previously inserted.
697 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
698 CurModule.CurrentModule->getGlobalList().remove(GV);
699 CurModule.CurrentModule->getGlobalList().push_back(GV);
700 GV->setInitializer(Initializer);
701 GV->setLinkage(Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000702 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000703 GV->setConstant(isConstantGlobal);
704 InsertValue(GV, CurModule.Values);
705 return GV;
706 }
707
Reid Spenceref9b9a72007-02-05 20:47:22 +0000708 // If this global has a name
Chris Lattner58af2a12006-02-15 07:22:58 +0000709 if (!Name.empty()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000710 // if the global we're parsing has an initializer (is a definition) and
711 // has external linkage.
712 if (Initializer && Linkage != GlobalValue::InternalLinkage)
713 // If there is already a global with external linkage with this name
714 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
715 // If we allow this GVar to get created, it will be renamed in the
716 // symbol table because it conflicts with an existing GVar. We can't
717 // allow redefinition of GVars whose linking indicates that their name
718 // must stay the same. Issue the error.
719 GenerateError("Redefinition of global variable named '" + Name +
720 "' of type '" + Ty->getDescription() + "'");
721 return 0;
722 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000723 }
724
725 // Otherwise there is no existing GV to use, create one now.
726 GlobalVariable *GV =
727 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
728 CurModule.CurrentModule);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000729 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000730 InsertValue(GV, CurModule.Values);
731 return GV;
732}
733
734// setTypeName - Set the specified type to the name given. The name may be
735// null potentially, in which case this is a noop. The string passed in is
736// assumed to be a malloc'd string buffer, and is freed by this function.
737//
738// This function returns true if the type has already been defined, but is
739// allowed to be redefined in the specified context. If the name is a new name
740// for the type plane, it is inserted and false is returned.
741static bool setTypeName(const Type *T, char *NameStr) {
Reid Spencera9720f52007-02-05 17:04:00 +0000742 assert(!inFunctionScope() && "Can't give types function-local names!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000743 if (NameStr == 0) return false;
744
745 std::string Name(NameStr); // Copy string
746 free(NameStr); // Free old string
747
748 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000749 if (T == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000750 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000751 return false;
752 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000753
754 // Set the type name, checking for conflicts as we do so.
755 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
756
757 if (AlreadyExists) { // Inserting a name that is already defined???
758 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencera9720f52007-02-05 17:04:00 +0000759 assert(Existing && "Conflict but no matching type?!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000760
761 // There is only one case where this is allowed: when we are refining an
762 // opaque type. In this case, Existing will be an opaque type.
763 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
764 // We ARE replacing an opaque type!
765 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
766 return true;
767 }
768
769 // Otherwise, this is an attempt to redefine a type. That's okay if
770 // the redefinition is identical to the original. This will be so if
771 // Existing and T point to the same Type object. In this one case we
772 // allow the equivalent redefinition.
773 if (Existing == T) return true; // Yes, it's equal.
774
775 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer63c34452007-01-05 21:51:07 +0000776 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000777 T->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +0000778 }
779
780 return false;
781}
782
783//===----------------------------------------------------------------------===//
784// Code for handling upreferences in type names...
785//
786
787// TypeContains - Returns true if Ty directly contains E in it.
788//
789static bool TypeContains(const Type *Ty, const Type *E) {
790 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
791 E) != Ty->subtype_end();
792}
793
794namespace {
795 struct UpRefRecord {
796 // NestingLevel - The number of nesting levels that need to be popped before
797 // this type is resolved.
798 unsigned NestingLevel;
799
800 // LastContainedTy - This is the type at the current binding level for the
801 // type. Every time we reduce the nesting level, this gets updated.
802 const Type *LastContainedTy;
803
804 // UpRefTy - This is the actual opaque type that the upreference is
805 // represented with.
806 OpaqueType *UpRefTy;
807
808 UpRefRecord(unsigned NL, OpaqueType *URTy)
809 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
810 };
811}
812
813// UpRefs - A list of the outstanding upreferences that need to be resolved.
814static std::vector<UpRefRecord> UpRefs;
815
816/// HandleUpRefs - Every time we finish a new layer of types, this function is
817/// called. It loops through the UpRefs vector, which is a list of the
818/// currently active types. For each type, if the up reference is contained in
819/// the newly completed type, we decrement the level count. When the level
820/// count reaches zero, the upreferenced type is the type that is passed in:
821/// thus we can complete the cycle.
822///
823static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner224f84f2006-08-18 17:34:45 +0000824 // If Ty isn't abstract, or if there are no up-references in it, then there is
825 // nothing to resolve here.
826 if (!ty->isAbstract() || UpRefs.empty()) return ty;
827
Chris Lattner58af2a12006-02-15 07:22:58 +0000828 PATypeHolder Ty(ty);
829 UR_OUT("Type '" << Ty->getDescription() <<
830 "' newly formed. Resolving upreferences.\n" <<
831 UpRefs.size() << " upreferences active!\n");
832
833 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
834 // to zero), we resolve them all together before we resolve them to Ty. At
835 // the end of the loop, if there is anything to resolve to Ty, it will be in
836 // this variable.
837 OpaqueType *TypeToResolve = 0;
838
839 for (unsigned i = 0; i != UpRefs.size(); ++i) {
840 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
841 << UpRefs[i].second->getDescription() << ") = "
842 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
843 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
844 // Decrement level of upreference
845 unsigned Level = --UpRefs[i].NestingLevel;
846 UpRefs[i].LastContainedTy = Ty;
847 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
848 if (Level == 0) { // Upreference should be resolved!
849 if (!TypeToResolve) {
850 TypeToResolve = UpRefs[i].UpRefTy;
851 } else {
852 UR_OUT(" * Resolving upreference for "
853 << UpRefs[i].second->getDescription() << "\n";
854 std::string OldName = UpRefs[i].UpRefTy->getDescription());
855 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
856 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
857 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
858 }
859 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
860 --i; // Do not skip the next element...
861 }
862 }
863 }
864
865 if (TypeToResolve) {
866 UR_OUT(" * Resolving upreference for "
867 << UpRefs[i].second->getDescription() << "\n";
868 std::string OldName = TypeToResolve->getDescription());
869 TypeToResolve->refineAbstractTypeTo(Ty);
870 }
871
872 return Ty;
873}
874
Chris Lattner58af2a12006-02-15 07:22:58 +0000875//===----------------------------------------------------------------------===//
876// RunVMAsmParser - Define an interface to this parser
877//===----------------------------------------------------------------------===//
878//
Reid Spencer14310612006-12-31 05:40:51 +0000879static Module* RunParser(Module * M);
880
Chris Lattner58af2a12006-02-15 07:22:58 +0000881Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
882 set_scan_file(F);
883
884 CurFilename = Filename;
885 return RunParser(new Module(CurFilename));
886}
887
888Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
889 set_scan_string(AsmString);
890
891 CurFilename = "from_memory";
892 if (M == NULL) {
893 return RunParser(new Module (CurFilename));
894 } else {
895 return RunParser(M);
896 }
897}
898
899%}
900
901%union {
902 llvm::Module *ModuleVal;
903 llvm::Function *FunctionVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000904 llvm::BasicBlock *BasicBlockVal;
905 llvm::TerminatorInst *TermInstVal;
906 llvm::Instruction *InstVal;
Reid Spencera132e042006-12-03 05:46:11 +0000907 llvm::Constant *ConstVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000908
Reid Spencera132e042006-12-03 05:46:11 +0000909 const llvm::Type *PrimType;
Reid Spencer14310612006-12-31 05:40:51 +0000910 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencera132e042006-12-03 05:46:11 +0000911 llvm::PATypeHolder *TypeVal;
912 llvm::Value *ValueVal;
Reid Spencera132e042006-12-03 05:46:11 +0000913 std::vector<llvm::Value*> *ValueList;
Reid Spencer14310612006-12-31 05:40:51 +0000914 llvm::ArgListType *ArgList;
915 llvm::TypeWithAttrs TypeWithAttrs;
916 llvm::TypeWithAttrsList *TypeWithAttrsList;
917 llvm::ValueRefList *ValueRefList;
918
Chris Lattner58af2a12006-02-15 07:22:58 +0000919 // Represent the RHS of PHI node
Reid Spencera132e042006-12-03 05:46:11 +0000920 std::list<std::pair<llvm::Value*,
921 llvm::BasicBlock*> > *PHIList;
Chris Lattner58af2a12006-02-15 07:22:58 +0000922 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencera132e042006-12-03 05:46:11 +0000923 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner58af2a12006-02-15 07:22:58 +0000924
925 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000926 llvm::GlobalValue::VisibilityTypes Visibility;
Reid Spencer14310612006-12-31 05:40:51 +0000927 llvm::FunctionType::ParameterAttributes ParamAttrs;
Chris Lattner58af2a12006-02-15 07:22:58 +0000928 int64_t SInt64Val;
929 uint64_t UInt64Val;
930 int SIntVal;
931 unsigned UIntVal;
932 double FPVal;
933 bool BoolVal;
934
935 char *StrVal; // This memory is strdup'd!
Reid Spencer1628cec2006-10-26 06:15:43 +0000936 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner58af2a12006-02-15 07:22:58 +0000937
Reid Spencera132e042006-12-03 05:46:11 +0000938 llvm::Instruction::BinaryOps BinaryOpVal;
939 llvm::Instruction::TermOps TermOpVal;
940 llvm::Instruction::MemoryOps MemOpVal;
941 llvm::Instruction::CastOps CastOpVal;
942 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencera132e042006-12-03 05:46:11 +0000943 llvm::ICmpInst::Predicate IPredicate;
944 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner58af2a12006-02-15 07:22:58 +0000945}
946
Reid Spencer14310612006-12-31 05:40:51 +0000947%type <ModuleVal> Module
Chris Lattner58af2a12006-02-15 07:22:58 +0000948%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
949%type <BasicBlockVal> BasicBlock InstructionList
950%type <TermInstVal> BBTerminatorInst
951%type <InstVal> Inst InstVal MemoryInst
952%type <ConstVal> ConstVal ConstExpr
953%type <ConstVector> ConstVector
954%type <ArgList> ArgList ArgListH
Chris Lattner58af2a12006-02-15 07:22:58 +0000955%type <PHIList> PHIList
Reid Spencer14310612006-12-31 05:40:51 +0000956%type <ValueRefList> ValueRefList // For call param lists & GEP indices
957%type <ValueList> IndexList // For GEP indices
958%type <TypeList> TypeListI
959%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer218ded22007-01-05 17:07:23 +0000960%type <TypeWithAttrs> ArgType
Chris Lattner58af2a12006-02-15 07:22:58 +0000961%type <JumpTable> JumpTable
962%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
963%type <BoolVal> OptVolatile // 'volatile' or not
964%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
965%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencer14310612006-12-31 05:40:51 +0000966%type <Linkage> GVInternalLinkage GVExternalLinkage
967%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000968%type <Visibility> GVVisibilityStyle
Chris Lattner58af2a12006-02-15 07:22:58 +0000969
970// ValueRef - Unresolved reference to a definition or BB
971%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
972%type <ValueVal> ResolvedVal // <type> <valref> pair
973// Tokens and types for handling constant integer values
974//
975// ESINT64VAL - A negative number within long long range
976%token <SInt64Val> ESINT64VAL
977
978// EUINT64VAL - A positive number within uns. long long range
979%token <UInt64Val> EUINT64VAL
Chris Lattner58af2a12006-02-15 07:22:58 +0000980
Reid Spencer41dff5e2007-01-26 08:05:27 +0000981%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattner58af2a12006-02-15 07:22:58 +0000982%token <FPVal> FPVAL // Float or Double constant
983
984// Built in types...
Reid Spencer218ded22007-01-05 17:07:23 +0000985%type <TypeVal> Types ResultTypes
Reid Spencer14310612006-12-31 05:40:51 +0000986%type <PrimType> IntType FPType PrimType // Classifications
Reid Spencer6f407902007-01-13 05:00:46 +0000987%token <PrimType> VOID INTTYPE
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000988%token <PrimType> FLOAT DOUBLE LABEL
989%token TYPE
Chris Lattner58af2a12006-02-15 07:22:58 +0000990
Reid Spencer41dff5e2007-01-26 08:05:27 +0000991%token<StrVal> LOCALVAR GLOBALVAR LABELSTR STRINGCONSTANT ATSTRINGCONSTANT
992%type <StrVal> LocalName OptLocalName OptLocalAssign
993%type <StrVal> GlobalName OptGlobalAssign
994%type <UIntVal> OptAlign OptCAlign
Chris Lattner58af2a12006-02-15 07:22:58 +0000995%type <StrVal> OptSection SectionString
996
997%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000998%token DECLARE DEFINE GLOBAL CONSTANT SECTION VOLATILE
Reid Spencer14310612006-12-31 05:40:51 +0000999%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001000%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Reid Spencer41dff5e2007-01-26 08:05:27 +00001001%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN
Chris Lattner58af2a12006-02-15 07:22:58 +00001002%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001003%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattner1ae022f2006-10-22 06:08:13 +00001004%token DATALAYOUT
Chris Lattner58af2a12006-02-15 07:22:58 +00001005%type <UIntVal> OptCallingConv
Reid Spencer218ded22007-01-05 17:07:23 +00001006%type <ParamAttrs> OptParamAttrs ParamAttr
1007%type <ParamAttrs> OptFuncAttrs FuncAttr
Chris Lattner58af2a12006-02-15 07:22:58 +00001008
1009// Basic Block Terminating Operators
1010%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
1011
1012// Binary Operators
Reid Spencere4d87aa2006-12-23 06:05:41 +00001013%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencer3ed469c2006-11-02 20:25:50 +00001014%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer832254e2007-02-02 02:16:23 +00001015%token <BinaryOpVal> SHL LSHR ASHR
1016
Reid Spencera132e042006-12-03 05:46:11 +00001017%token <OtherOpVal> ICMP FCMP
Reid Spencera132e042006-12-03 05:46:11 +00001018%type <IPredicate> IPredicates
Reid Spencera132e042006-12-03 05:46:11 +00001019%type <FPredicate> FPredicates
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001020%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1021%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner58af2a12006-02-15 07:22:58 +00001022
1023// Memory Instructions
1024%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1025
Reid Spencer3da59db2006-11-27 01:05:10 +00001026// Cast Operators
1027%type <CastOpVal> CastOps
1028%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1029%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1030
Chris Lattner58af2a12006-02-15 07:22:58 +00001031// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001032%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattnerd5efe842006-04-08 01:18:56 +00001033%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Chris Lattner58af2a12006-02-15 07:22:58 +00001034
Reid Spencer218ded22007-01-05 17:07:23 +00001035// Function Attributes
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001036%token NORETURN INREG SRET
Chris Lattner58af2a12006-02-15 07:22:58 +00001037
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001038// Visibility Styles
1039%token DEFAULT HIDDEN
1040
Chris Lattner58af2a12006-02-15 07:22:58 +00001041%start Module
1042%%
1043
Chris Lattner58af2a12006-02-15 07:22:58 +00001044
Chris Lattner58af2a12006-02-15 07:22:58 +00001045// Operations that are notably excluded from this list include:
1046// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1047//
Reid Spencer3ed469c2006-11-02 20:25:50 +00001048ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer832254e2007-02-02 02:16:23 +00001049LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Reid Spencer3da59db2006-11-27 01:05:10 +00001050CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1051 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer832254e2007-02-02 02:16:23 +00001052
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001053IPredicates
Reid Spencer4012e832006-12-04 05:24:24 +00001054 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001055 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1056 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1057 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1058 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1059 ;
1060
1061FPredicates
1062 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1063 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1064 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1065 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1066 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1067 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1068 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1069 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1070 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1071 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001072
1073// These are some types that allow classification if we only want a particular
1074// thing... for example, only a signed, unsigned, or integral type.
Reid Spencera54b7cb2007-01-12 07:05:14 +00001075IntType : INTTYPE;
Chris Lattner58af2a12006-02-15 07:22:58 +00001076FPType : FLOAT | DOUBLE;
1077
Reid Spencer41dff5e2007-01-26 08:05:27 +00001078LocalName : LOCALVAR | STRINGCONSTANT;
1079OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1080
1081/// OptLocalAssign - Value producing statements have an optional assignment
1082/// component.
1083OptLocalAssign : LocalName '=' {
1084 $$ = $1;
1085 CHECK_FOR_ERROR
1086 }
1087 | /*empty*/ {
1088 $$ = 0;
1089 CHECK_FOR_ERROR
1090 };
1091
1092GlobalName : GLOBALVAR | ATSTRINGCONSTANT;
1093
1094OptGlobalAssign : GlobalName '=' {
Chris Lattner58af2a12006-02-15 07:22:58 +00001095 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001096 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001097 }
1098 | /*empty*/ {
1099 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001100 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001101 };
1102
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001103GVInternalLinkage
1104 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1105 | WEAK { $$ = GlobalValue::WeakLinkage; }
1106 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1107 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1108 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1109 ;
1110
1111GVExternalLinkage
1112 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1113 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1114 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1115 ;
1116
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001117GVVisibilityStyle
1118 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1119 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1120 ;
1121
Reid Spencer14310612006-12-31 05:40:51 +00001122FunctionDeclareLinkage
1123 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1124 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1125 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001126 ;
1127
Reid Spencer14310612006-12-31 05:40:51 +00001128FunctionDefineLinkage
1129 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1130 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001131 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1132 | WEAK { $$ = GlobalValue::WeakLinkage; }
1133 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001134 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001135
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001136OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1137 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001138 FASTCC_TOK { $$ = CallingConv::Fast; } |
1139 COLDCC_TOK { $$ = CallingConv::Cold; } |
1140 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1141 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1142 CC_TOK EUINT64VAL {
Chris Lattner58af2a12006-02-15 07:22:58 +00001143 if ((unsigned)$2 != $2)
Reid Spencerb5334b02007-02-05 10:18:06 +00001144 GEN_ERROR("Calling conv too large");
Chris Lattner58af2a12006-02-15 07:22:58 +00001145 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001146 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001147 };
1148
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001149ParamAttr : ZEXT { $$ = FunctionType::ZExtAttribute; }
1150 | SEXT { $$ = FunctionType::SExtAttribute; }
1151 | INREG { $$ = FunctionType::InRegAttribute; }
1152 | SRET { $$ = FunctionType::StructRetAttribute; }
Reid Spencer14310612006-12-31 05:40:51 +00001153 ;
1154
Reid Spencer218ded22007-01-05 17:07:23 +00001155OptParamAttrs : /* empty */ { $$ = FunctionType::NoAttributeSet; }
1156 | OptParamAttrs ParamAttr {
1157 $$ = FunctionType::ParameterAttributes($1 | $2);
Reid Spencer14310612006-12-31 05:40:51 +00001158 }
1159 ;
1160
Reid Spencer218ded22007-01-05 17:07:23 +00001161FuncAttr : NORETURN { $$ = FunctionType::NoReturnAttribute; }
1162 | ParamAttr
1163 ;
1164
1165OptFuncAttrs : /* empty */ { $$ = FunctionType::NoAttributeSet; }
1166 | OptFuncAttrs FuncAttr {
1167 $$ = FunctionType::ParameterAttributes($1 | $2);
1168 }
Reid Spencer14310612006-12-31 05:40:51 +00001169 ;
1170
Chris Lattner58af2a12006-02-15 07:22:58 +00001171// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1172// a comma before it.
1173OptAlign : /*empty*/ { $$ = 0; } |
1174 ALIGN EUINT64VAL {
1175 $$ = $2;
1176 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001177 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001178 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001179};
1180OptCAlign : /*empty*/ { $$ = 0; } |
1181 ',' ALIGN EUINT64VAL {
1182 $$ = $3;
1183 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001184 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001185 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001186};
1187
1188
1189SectionString : SECTION STRINGCONSTANT {
1190 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1191 if ($2[i] == '"' || $2[i] == '\\')
Reid Spencerb5334b02007-02-05 10:18:06 +00001192 GEN_ERROR("Invalid character in section name");
Chris Lattner58af2a12006-02-15 07:22:58 +00001193 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001194 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001195};
1196
1197OptSection : /*empty*/ { $$ = 0; } |
1198 SectionString { $$ = $1; };
1199
1200// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1201// is set to be the global we are processing.
1202//
1203GlobalVarAttributes : /* empty */ {} |
1204 ',' GlobalVarAttribute GlobalVarAttributes {};
1205GlobalVarAttribute : SectionString {
1206 CurGV->setSection($1);
1207 free($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001208 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001209 }
1210 | ALIGN EUINT64VAL {
1211 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001212 GEN_ERROR("Alignment must be a power of two");
Chris Lattner58af2a12006-02-15 07:22:58 +00001213 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001214 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001215 };
1216
1217//===----------------------------------------------------------------------===//
1218// Types includes all predefined types... except void, because it can only be
Reid Spencer14310612006-12-31 05:40:51 +00001219// used in specific contexts (function returning void for example).
Chris Lattner58af2a12006-02-15 07:22:58 +00001220
1221// Derived types are added later...
1222//
Reid Spencer6f407902007-01-13 05:00:46 +00001223PrimType : INTTYPE | FLOAT | DOUBLE | LABEL ;
Reid Spencer14310612006-12-31 05:40:51 +00001224
1225Types
1226 : OPAQUE {
Reid Spencera132e042006-12-03 05:46:11 +00001227 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001228 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001229 }
1230 | PrimType {
Reid Spencera132e042006-12-03 05:46:11 +00001231 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001232 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00001233 }
1234 | Types '*' { // Pointer type?
1235 if (*$1 == Type::LabelTy)
1236 GEN_ERROR("Cannot form a pointer to a basic block");
1237 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1238 delete $1;
1239 CHECK_FOR_ERROR
1240 }
1241 | SymbolicValueRef { // Named types are also simple types...
1242 const Type* tmp = getTypeVal($1);
1243 CHECK_FOR_ERROR
1244 $$ = new PATypeHolder(tmp);
1245 }
1246 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerb5334b02007-02-05 10:18:06 +00001247 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattner58af2a12006-02-15 07:22:58 +00001248 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1249 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencera132e042006-12-03 05:46:11 +00001250 $$ = new PATypeHolder(OT);
Chris Lattner58af2a12006-02-15 07:22:58 +00001251 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001252 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001253 }
Reid Spencer218ded22007-01-05 17:07:23 +00001254 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Chris Lattner58af2a12006-02-15 07:22:58 +00001255 std::vector<const Type*> Params;
Reid Spencer14310612006-12-31 05:40:51 +00001256 std::vector<FunctionType::ParameterAttributes> Attrs;
Reid Spencer218ded22007-01-05 17:07:23 +00001257 Attrs.push_back($5);
1258 for (TypeWithAttrsList::iterator I=$3->begin(), E=$3->end(); I != E; ++I) {
Reid Spencer14310612006-12-31 05:40:51 +00001259 Params.push_back(I->Ty->get());
1260 if (I->Ty->get() != Type::VoidTy)
1261 Attrs.push_back(I->Attrs);
1262 }
Chris Lattner58af2a12006-02-15 07:22:58 +00001263 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1264 if (isVarArg) Params.pop_back();
1265
Reid Spencer14310612006-12-31 05:40:51 +00001266 FunctionType *FT = FunctionType::get(*$1, Params, isVarArg, Attrs);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001267 delete $3; // Delete the argument list
Reid Spencer14310612006-12-31 05:40:51 +00001268 delete $1; // Delete the return type handle
1269 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer61c83e02006-08-18 08:43:06 +00001270 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001271 }
Reid Spencer218ded22007-01-05 17:07:23 +00001272 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Reid Spencer14310612006-12-31 05:40:51 +00001273 std::vector<const Type*> Params;
1274 std::vector<FunctionType::ParameterAttributes> Attrs;
Reid Spencer218ded22007-01-05 17:07:23 +00001275 Attrs.push_back($5);
1276 for (TypeWithAttrsList::iterator I=$3->begin(), E=$3->end(); I != E; ++I) {
Reid Spencer14310612006-12-31 05:40:51 +00001277 Params.push_back(I->Ty->get());
1278 if (I->Ty->get() != Type::VoidTy)
1279 Attrs.push_back(I->Attrs);
1280 }
1281 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1282 if (isVarArg) Params.pop_back();
1283
1284 FunctionType *FT = FunctionType::get($1, Params, isVarArg, Attrs);
Reid Spencer218ded22007-01-05 17:07:23 +00001285 delete $3; // Delete the argument list
Reid Spencer14310612006-12-31 05:40:51 +00001286 $$ = new PATypeHolder(HandleUpRefs(FT));
1287 CHECK_FOR_ERROR
1288 }
1289
1290 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Reid Spencera132e042006-12-03 05:46:11 +00001291 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1292 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001293 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001294 }
Reid Spencer14310612006-12-31 05:40:51 +00001295 | '<' EUINT64VAL 'x' Types '>' { // Packed array type?
Reid Spencera132e042006-12-03 05:46:11 +00001296 const llvm::Type* ElemTy = $4->get();
1297 if ((unsigned)$2 != $2)
1298 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner42a75512007-01-15 02:27:26 +00001299 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencera54b7cb2007-01-12 07:05:14 +00001300 GEN_ERROR("Element type of a PackedType must be primitive");
Reid Spencera132e042006-12-03 05:46:11 +00001301 if (!isPowerOf2_32($2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001302 GEN_ERROR("Vector length should be a power of 2");
Reid Spencera132e042006-12-03 05:46:11 +00001303 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
1304 delete $4;
1305 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001306 }
1307 | '{' TypeListI '}' { // Structure type?
1308 std::vector<const Type*> Elements;
Reid Spencera132e042006-12-03 05:46:11 +00001309 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattner58af2a12006-02-15 07:22:58 +00001310 E = $2->end(); I != E; ++I)
Reid Spencera132e042006-12-03 05:46:11 +00001311 Elements.push_back(*I);
Chris Lattner58af2a12006-02-15 07:22:58 +00001312
Reid Spencera132e042006-12-03 05:46:11 +00001313 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattner58af2a12006-02-15 07:22:58 +00001314 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001315 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001316 }
1317 | '{' '}' { // Empty structure type?
Reid Spencera132e042006-12-03 05:46:11 +00001318 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001319 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001320 }
Andrew Lenharth6353e052006-12-08 18:07:09 +00001321 | '<' '{' TypeListI '}' '>' {
1322 std::vector<const Type*> Elements;
1323 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1324 E = $3->end(); I != E; ++I)
1325 Elements.push_back(*I);
1326
1327 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1328 delete $3;
1329 CHECK_FOR_ERROR
1330 }
1331 | '<' '{' '}' '>' { // Empty structure type?
1332 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1333 CHECK_FOR_ERROR
1334 }
Reid Spencer14310612006-12-31 05:40:51 +00001335 ;
1336
1337ArgType
1338 : Types OptParamAttrs {
1339 $$.Ty = $1;
1340 $$.Attrs = $2;
1341 }
1342 ;
1343
Reid Spencer218ded22007-01-05 17:07:23 +00001344ResultTypes
1345 : Types {
Reid Spencer14310612006-12-31 05:40:51 +00001346 if (!UpRefs.empty())
1347 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1348 if (!(*$1)->isFirstClassType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001349 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer218ded22007-01-05 17:07:23 +00001350 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00001351 }
Reid Spencer218ded22007-01-05 17:07:23 +00001352 | VOID {
1353 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencer14310612006-12-31 05:40:51 +00001354 }
1355 ;
1356
1357ArgTypeList : ArgType {
1358 $$ = new TypeWithAttrsList();
1359 $$->push_back($1);
1360 CHECK_FOR_ERROR
1361 }
1362 | ArgTypeList ',' ArgType {
1363 ($$=$1)->push_back($3);
1364 CHECK_FOR_ERROR
1365 }
1366 ;
1367
1368ArgTypeListI
1369 : ArgTypeList
1370 | ArgTypeList ',' DOTDOTDOT {
1371 $$=$1;
1372 TypeWithAttrs TWA; TWA.Attrs = FunctionType::NoAttributeSet;
1373 TWA.Ty = new PATypeHolder(Type::VoidTy);
1374 $$->push_back(TWA);
1375 CHECK_FOR_ERROR
1376 }
1377 | DOTDOTDOT {
1378 $$ = new TypeWithAttrsList;
1379 TypeWithAttrs TWA; TWA.Attrs = FunctionType::NoAttributeSet;
1380 TWA.Ty = new PATypeHolder(Type::VoidTy);
1381 $$->push_back(TWA);
1382 CHECK_FOR_ERROR
1383 }
1384 | /*empty*/ {
1385 $$ = new TypeWithAttrsList();
Reid Spencer61c83e02006-08-18 08:43:06 +00001386 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001387 };
1388
1389// TypeList - Used for struct declarations and as a basis for function type
1390// declaration type lists
1391//
Reid Spencer14310612006-12-31 05:40:51 +00001392TypeListI : Types {
Reid Spencera132e042006-12-03 05:46:11 +00001393 $$ = new std::list<PATypeHolder>();
1394 $$->push_back(*$1); delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001395 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001396 }
Reid Spencer14310612006-12-31 05:40:51 +00001397 | TypeListI ',' Types {
Reid Spencera132e042006-12-03 05:46:11 +00001398 ($$=$1)->push_back(*$3); delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001399 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001400 };
1401
Chris Lattner58af2a12006-02-15 07:22:58 +00001402// ConstVal - The various declarations that go into the constant pool. This
1403// production is used ONLY to represent constants that show up AFTER a 'const',
1404// 'constant' or 'global' token at global scope. Constants that can be inlined
1405// into other expressions (such as integers and constexprs) are handled by the
1406// ResolvedVal, ValueRef and ConstValueRef productions.
1407//
1408ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001409 if (!UpRefs.empty())
1410 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001411 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001412 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001413 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001414 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001415 const Type *ETy = ATy->getElementType();
1416 int NumElements = ATy->getNumElements();
1417
1418 // Verify that we have the correct size...
1419 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001420 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001421 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerb5334b02007-02-05 10:18:06 +00001422 itostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001423
1424 // Verify all elements are correct type!
1425 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001426 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001427 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001428 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001429 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001430 }
1431
Reid Spencera132e042006-12-03 05:46:11 +00001432 $$ = ConstantArray::get(ATy, *$3);
1433 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001434 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001435 }
1436 | Types '[' ']' {
Reid Spencer14310612006-12-31 05:40:51 +00001437 if (!UpRefs.empty())
1438 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001439 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001440 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001441 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001442 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001443
1444 int NumElements = ATy->getNumElements();
1445 if (NumElements != -1 && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001446 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencerb5334b02007-02-05 10:18:06 +00001447 " arguments, but has size of " + itostr(NumElements) +"");
Reid Spencera132e042006-12-03 05:46:11 +00001448 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1449 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001450 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001451 }
1452 | Types 'c' STRINGCONSTANT {
Reid Spencer14310612006-12-31 05:40:51 +00001453 if (!UpRefs.empty())
1454 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001455 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001456 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001457 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001458 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001459
1460 int NumElements = ATy->getNumElements();
1461 const Type *ETy = ATy->getElementType();
1462 char *EndStr = UnEscapeLexed($3, true);
1463 if (NumElements != -1 && NumElements != (EndStr-$3))
Reid Spencer61c83e02006-08-18 08:43:06 +00001464 GEN_ERROR("Can't build string constant of size " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001465 itostr((int)(EndStr-$3)) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001466 " when array has size " + itostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001467 std::vector<Constant*> Vals;
Reid Spencer14310612006-12-31 05:40:51 +00001468 if (ETy == Type::Int8Ty) {
Chris Lattner58af2a12006-02-15 07:22:58 +00001469 for (unsigned char *C = (unsigned char *)$3;
Reid Spencer14310612006-12-31 05:40:51 +00001470 C != (unsigned char*)EndStr; ++C)
1471 Vals.push_back(ConstantInt::get(ETy, *C));
Chris Lattner58af2a12006-02-15 07:22:58 +00001472 } else {
1473 free($3);
Reid Spencerb5334b02007-02-05 10:18:06 +00001474 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner58af2a12006-02-15 07:22:58 +00001475 }
1476 free($3);
Reid Spencera132e042006-12-03 05:46:11 +00001477 $$ = ConstantArray::get(ATy, Vals);
1478 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001479 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001480 }
1481 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001482 if (!UpRefs.empty())
1483 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001484 const PackedType *PTy = dyn_cast<PackedType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001485 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001486 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001487 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001488 const Type *ETy = PTy->getElementType();
1489 int NumElements = PTy->getNumElements();
1490
1491 // Verify that we have the correct size...
1492 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001493 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001494 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerb5334b02007-02-05 10:18:06 +00001495 itostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001496
1497 // Verify all elements are correct type!
1498 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001499 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001500 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001501 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001502 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001503 }
1504
Reid Spencera132e042006-12-03 05:46:11 +00001505 $$ = ConstantPacked::get(PTy, *$3);
1506 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001507 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001508 }
1509 | Types '{' ConstVector '}' {
Reid Spencera132e042006-12-03 05:46:11 +00001510 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001511 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001512 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001513 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001514
1515 if ($3->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001516 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001517
1518 // Check to ensure that constants are compatible with the type initializer!
1519 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencera132e042006-12-03 05:46:11 +00001520 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001521 GEN_ERROR("Expected type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001522 STy->getElementType(i)->getDescription() +
1523 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001524 " of structure initializer");
Chris Lattner58af2a12006-02-15 07:22:58 +00001525
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001526 // Check to ensure that Type is not packed
1527 if (STy->isPacked())
1528 GEN_ERROR("Unpacked Initializer to packed type '" + STy->getDescription() + "'");
1529
Reid Spencera132e042006-12-03 05:46:11 +00001530 $$ = ConstantStruct::get(STy, *$3);
1531 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001532 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001533 }
1534 | Types '{' '}' {
Reid Spencer14310612006-12-31 05:40:51 +00001535 if (!UpRefs.empty())
1536 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001537 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001538 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001539 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001540 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001541
1542 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001543 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001544
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001545 // Check to ensure that Type is not packed
1546 if (STy->isPacked())
1547 GEN_ERROR("Unpacked Initializer to packed type '" + STy->getDescription() + "'");
1548
1549 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1550 delete $1;
1551 CHECK_FOR_ERROR
1552 }
1553 | Types '<' '{' ConstVector '}' '>' {
1554 const StructType *STy = dyn_cast<StructType>($1->get());
1555 if (STy == 0)
1556 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001557 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001558
1559 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001560 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001561
1562 // Check to ensure that constants are compatible with the type initializer!
1563 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1564 if ((*$4)[i]->getType() != STy->getElementType(i))
1565 GEN_ERROR("Expected type '" +
1566 STy->getElementType(i)->getDescription() +
1567 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001568 " of structure initializer");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001569
1570 // Check to ensure that Type is packed
1571 if (!STy->isPacked())
1572 GEN_ERROR("Packed Initializer to unpacked type '" + STy->getDescription() + "'");
1573
1574 $$ = ConstantStruct::get(STy, *$4);
1575 delete $1; delete $4;
1576 CHECK_FOR_ERROR
1577 }
1578 | Types '<' '{' '}' '>' {
1579 if (!UpRefs.empty())
1580 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1581 const StructType *STy = dyn_cast<StructType>($1->get());
1582 if (STy == 0)
1583 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001584 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001585
1586 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001587 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001588
1589 // Check to ensure that Type is packed
1590 if (!STy->isPacked())
1591 GEN_ERROR("Packed Initializer to unpacked type '" + STy->getDescription() + "'");
1592
Reid Spencera132e042006-12-03 05:46:11 +00001593 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1594 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001595 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001596 }
1597 | Types NULL_TOK {
Reid Spencer14310612006-12-31 05:40:51 +00001598 if (!UpRefs.empty())
1599 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001600 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001601 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001602 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001603 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001604
Reid Spencera132e042006-12-03 05:46:11 +00001605 $$ = ConstantPointerNull::get(PTy);
1606 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001607 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001608 }
1609 | Types UNDEF {
Reid Spencer14310612006-12-31 05:40:51 +00001610 if (!UpRefs.empty())
1611 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001612 $$ = UndefValue::get($1->get());
1613 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001614 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001615 }
1616 | Types SymbolicValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00001617 if (!UpRefs.empty())
1618 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001619 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001620 if (Ty == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001621 GEN_ERROR("Global const reference must be a pointer type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001622
1623 // ConstExprs can exist in the body of a function, thus creating
1624 // GlobalValues whenever they refer to a variable. Because we are in
1625 // the context of a function, getValNonImprovising will search the functions
1626 // symbol table instead of the module symbol table for the global symbol,
1627 // which throws things all off. To get around this, we just tell
1628 // getValNonImprovising that we are at global scope here.
1629 //
1630 Function *SavedCurFn = CurFun.CurrentFunction;
1631 CurFun.CurrentFunction = 0;
1632
1633 Value *V = getValNonImprovising(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001634 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001635
1636 CurFun.CurrentFunction = SavedCurFn;
1637
1638 // If this is an initializer for a constant pointer, which is referencing a
1639 // (currently) undefined variable, create a stub now that shall be replaced
1640 // in the future with the right type of variable.
1641 //
1642 if (V == 0) {
Reid Spencera9720f52007-02-05 17:04:00 +00001643 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001644 const PointerType *PT = cast<PointerType>(Ty);
1645
1646 // First check to see if the forward references value is already created!
1647 PerModuleInfo::GlobalRefsType::iterator I =
1648 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1649
1650 if (I != CurModule.GlobalRefs.end()) {
1651 V = I->second; // Placeholder already exists, use it...
1652 $2.destroy();
1653 } else {
1654 std::string Name;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001655 if ($2.Type == ValID::GlobalName)
1656 Name = $2.Name;
1657 else if ($2.Type != ValID::GlobalID)
1658 GEN_ERROR("Invalid reference to global");
Chris Lattner58af2a12006-02-15 07:22:58 +00001659
1660 // Create the forward referenced global.
1661 GlobalValue *GV;
1662 if (const FunctionType *FTy =
1663 dyn_cast<FunctionType>(PT->getElementType())) {
1664 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1665 CurModule.CurrentModule);
1666 } else {
1667 GV = new GlobalVariable(PT->getElementType(), false,
1668 GlobalValue::ExternalLinkage, 0,
1669 Name, CurModule.CurrentModule);
1670 }
1671
1672 // Keep track of the fact that we have a forward ref to recycle it
1673 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1674 V = GV;
1675 }
1676 }
1677
Reid Spencera132e042006-12-03 05:46:11 +00001678 $$ = cast<GlobalValue>(V);
1679 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001680 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001681 }
1682 | Types ConstExpr {
Reid Spencer14310612006-12-31 05:40:51 +00001683 if (!UpRefs.empty())
1684 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001685 if ($1->get() != $2->getType())
Reid Spencere68853b2007-01-04 00:06:14 +00001686 GEN_ERROR("Mismatched types for constant expression: " +
1687 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001688 $$ = $2;
Reid Spencera132e042006-12-03 05:46:11 +00001689 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001690 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001691 }
1692 | Types ZEROINITIALIZER {
Reid Spencer14310612006-12-31 05:40:51 +00001693 if (!UpRefs.empty())
1694 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001695 const Type *Ty = $1->get();
Chris Lattner58af2a12006-02-15 07:22:58 +00001696 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerb5334b02007-02-05 10:18:06 +00001697 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencera132e042006-12-03 05:46:11 +00001698 $$ = Constant::getNullValue(Ty);
1699 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001700 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00001701 }
Reid Spencer14310612006-12-31 05:40:51 +00001702 | IntType ESINT64VAL { // integral constants
Reid Spencere4d87aa2006-12-23 06:05:41 +00001703 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001704 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencere4d87aa2006-12-23 06:05:41 +00001705 $$ = ConstantInt::get($1, $2);
1706 CHECK_FOR_ERROR
1707 }
Reid Spencer14310612006-12-31 05:40:51 +00001708 | IntType EUINT64VAL { // integral constants
Reid Spencere4d87aa2006-12-23 06:05:41 +00001709 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001710 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencere4d87aa2006-12-23 06:05:41 +00001711 $$ = ConstantInt::get($1, $2);
1712 CHECK_FOR_ERROR
1713 }
Reid Spencer6f407902007-01-13 05:00:46 +00001714 | INTTYPE TRUETOK { // Boolean constants
1715 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001716 $$ = ConstantInt::getTrue();
Reid Spencer61c83e02006-08-18 08:43:06 +00001717 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001718 }
Reid Spencer6f407902007-01-13 05:00:46 +00001719 | INTTYPE FALSETOK { // Boolean constants
1720 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001721 $$ = ConstantInt::getFalse();
Reid Spencer61c83e02006-08-18 08:43:06 +00001722 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001723 }
1724 | FPType FPVAL { // Float & Double constants
Reid Spencera132e042006-12-03 05:46:11 +00001725 if (!ConstantFP::isValueValidForType($1, $2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001726 GEN_ERROR("Floating point constant invalid for type");
Reid Spencera132e042006-12-03 05:46:11 +00001727 $$ = ConstantFP::get($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001728 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001729 };
1730
1731
Reid Spencer3da59db2006-11-27 01:05:10 +00001732ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer14310612006-12-31 05:40:51 +00001733 if (!UpRefs.empty())
1734 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001735 Constant *Val = $3;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001736 const Type *DestTy = $5->get();
1737 if (!CastInst::castIsValid($1, $3, DestTy))
1738 GEN_ERROR("invalid cast opcode for cast from '" +
1739 Val->getType()->getDescription() + "' to '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001740 DestTy->getDescription() + "'");
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001741 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00001742 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00001743 }
1744 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001745 if (!isa<PointerType>($3->getType()))
Reid Spencerb5334b02007-02-05 10:18:06 +00001746 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00001747
Reid Spencera132e042006-12-03 05:46:11 +00001748 const Type *IdxTy =
Chris Lattner7d9801d2007-02-13 00:58:01 +00001749 GetElementPtrInst::getIndexedType($3->getType(), &(*$4)[0], $4->size(),
1750 true);
Reid Spencera132e042006-12-03 05:46:11 +00001751 if (!IdxTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00001752 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencera132e042006-12-03 05:46:11 +00001753
Chris Lattnerf7469af2007-01-31 04:44:08 +00001754 SmallVector<Constant*, 8> IdxVec;
Reid Spencera132e042006-12-03 05:46:11 +00001755 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1756 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattner58af2a12006-02-15 07:22:58 +00001757 IdxVec.push_back(C);
1758 else
Reid Spencerb5334b02007-02-05 10:18:06 +00001759 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattner58af2a12006-02-15 07:22:58 +00001760
1761 delete $4;
1762
Chris Lattnerf7469af2007-01-31 04:44:08 +00001763 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer61c83e02006-08-18 08:43:06 +00001764 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001765 }
1766 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer4fe16d62007-01-11 18:21:29 +00001767 if ($3->getType() != Type::Int1Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00001768 GEN_ERROR("Select condition must be of boolean type");
Reid Spencera132e042006-12-03 05:46:11 +00001769 if ($5->getType() != $7->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001770 GEN_ERROR("Select operand types must match");
Reid Spencera132e042006-12-03 05:46:11 +00001771 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001772 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001773 }
1774 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001775 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001776 GEN_ERROR("Binary operator types must match");
Reid Spencer1628cec2006-10-26 06:15:43 +00001777 CHECK_FOR_ERROR;
Reid Spencer9eef56f2006-12-05 19:16:11 +00001778 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner58af2a12006-02-15 07:22:58 +00001779 }
1780 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001781 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001782 GEN_ERROR("Logical operator types must match");
Chris Lattner42a75512007-01-15 02:27:26 +00001783 if (!$3->getType()->isInteger()) {
Reid Spencer832254e2007-02-02 02:16:23 +00001784 if (Instruction::isShift($1) || !isa<PackedType>($3->getType()) ||
Chris Lattner42a75512007-01-15 02:27:26 +00001785 !cast<PackedType>($3->getType())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00001786 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00001787 }
Reid Spencera132e042006-12-03 05:46:11 +00001788 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001789 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001790 }
Reid Spencer4012e832006-12-04 05:24:24 +00001791 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1792 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001793 GEN_ERROR("icmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00001794 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00001795 }
Reid Spencer4012e832006-12-04 05:24:24 +00001796 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1797 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001798 GEN_ERROR("fcmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00001799 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00001800 }
Chris Lattner58af2a12006-02-15 07:22:58 +00001801 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001802 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerb5334b02007-02-05 10:18:06 +00001803 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00001804 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001805 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00001806 }
1807 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001808 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00001809 GEN_ERROR("Invalid insertelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00001810 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001811 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00001812 }
1813 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001814 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00001815 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00001816 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001817 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001818 };
1819
Chris Lattnerd25db202006-04-08 03:55:17 +00001820
Chris Lattner58af2a12006-02-15 07:22:58 +00001821// ConstVector - A list of comma separated constants.
1822ConstVector : ConstVector ',' ConstVal {
1823 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001824 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001825 }
1826 | ConstVal {
Reid Spencera132e042006-12-03 05:46:11 +00001827 $$ = new std::vector<Constant*>();
Chris Lattner58af2a12006-02-15 07:22:58 +00001828 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001829 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001830 };
1831
1832
1833// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1834GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
1835
1836
1837//===----------------------------------------------------------------------===//
1838// Rules to match Modules
1839//===----------------------------------------------------------------------===//
1840
1841// Module rule: Capture the result of parsing the whole file into a result
1842// variable...
1843//
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001844Module
1845 : DefinitionList {
1846 $$ = ParserResult = CurModule.CurrentModule;
1847 CurModule.ModuleDone();
1848 CHECK_FOR_ERROR;
1849 }
1850 | /*empty*/ {
1851 $$ = ParserResult = CurModule.CurrentModule;
1852 CurModule.ModuleDone();
1853 CHECK_FOR_ERROR;
1854 }
1855 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001856
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001857DefinitionList
1858 : Definition
1859 | DefinitionList Definition
1860 ;
1861
1862Definition
Jeff Cohen361c3ef2007-01-21 19:19:31 +00001863 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner58af2a12006-02-15 07:22:58 +00001864 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00001865 CHECK_FOR_ERROR
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001866 }
1867 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer61c83e02006-08-18 08:43:06 +00001868 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001869 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001870 | MODULE ASM_TOK AsmBlock {
Reid Spencer61c83e02006-08-18 08:43:06 +00001871 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001872 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001873 | IMPLEMENTATION {
Chris Lattner58af2a12006-02-15 07:22:58 +00001874 // Emit an error if there are any unresolved types left.
1875 if (!CurModule.LateResolveTypes.empty()) {
1876 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001877 if (DID.Type == ValID::LocalName) {
Reid Spencer61c83e02006-08-18 08:43:06 +00001878 GEN_ERROR("Reference to an undefined type: '"+DID.getName() + "'");
1879 } else {
1880 GEN_ERROR("Reference to an undefined type: #" + itostr(DID.Num));
1881 }
Chris Lattner58af2a12006-02-15 07:22:58 +00001882 }
Reid Spencer61c83e02006-08-18 08:43:06 +00001883 CHECK_FOR_ERROR
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001884 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00001885 | OptLocalAssign TYPE Types {
Reid Spencer14310612006-12-31 05:40:51 +00001886 if (!UpRefs.empty())
1887 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001888 // Eagerly resolve types. This is not an optimization, this is a
1889 // requirement that is due to the fact that we could have this:
1890 //
1891 // %list = type { %list * }
1892 // %list = type { %list * } ; repeated type decl
1893 //
1894 // If types are not resolved eagerly, then the two types will not be
1895 // determined to be the same type!
1896 //
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001897 ResolveTypeTo($1, *$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00001898
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001899 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00001900 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001901 // If this is a named type that is not a redefinition, add it to the slot
1902 // table.
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001903 CurModule.Types.push_back(*$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00001904 }
Reid Spencera132e042006-12-03 05:46:11 +00001905
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001906 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001907 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001908 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00001909 | OptLocalAssign TYPE VOID {
Reid Spencer14310612006-12-31 05:40:51 +00001910 ResolveTypeTo($1, $3);
1911
1912 if (!setTypeName($3, $1) && !$1) {
1913 CHECK_FOR_ERROR
1914 // If this is a named type that is not a redefinition, add it to the slot
1915 // table.
1916 CurModule.Types.push_back($3);
1917 }
1918 CHECK_FOR_ERROR
1919 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00001920 | OptGlobalAssign GVVisibilityStyle GlobalType ConstVal {
1921 /* "Externally Visible" Linkage */
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001922 if ($4 == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001923 GEN_ERROR("Global value initializer is not a constant");
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001924 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
1925 $2, $3, $4->getType(), $4);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001926 CHECK_FOR_ERROR
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001927 } GlobalVarAttributes {
1928 CurGV = 0;
1929 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00001930 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle GlobalType ConstVal {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001931 if ($5 == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001932 GEN_ERROR("Global value initializer is not a constant");
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001933 CurGV = ParseGlobalVariable($1, $2, $3, $4, $5->getType(), $5);
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001934 CHECK_FOR_ERROR
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001935 } GlobalVarAttributes {
1936 CurGV = 0;
1937 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00001938 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle GlobalType Types {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001939 if (!UpRefs.empty())
1940 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
1941 CurGV = ParseGlobalVariable($1, $2, $3, $4, *$5, 0);
1942 CHECK_FOR_ERROR
1943 delete $5;
Reid Spencer5b7e7532006-09-28 19:28:24 +00001944 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001945 CurGV = 0;
1946 CHECK_FOR_ERROR
1947 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001948 | TARGET TargetDefinition {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001949 CHECK_FOR_ERROR
1950 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001951 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00001952 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001953 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001954 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001955
1956
1957AsmBlock : STRINGCONSTANT {
1958 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
1959 char *EndStr = UnEscapeLexed($1, true);
1960 std::string NewAsm($1, EndStr);
1961 free($1);
1962
1963 if (AsmSoFar.empty())
1964 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
1965 else
1966 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
Reid Spencer61c83e02006-08-18 08:43:06 +00001967 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001968};
1969
Reid Spencer41dff5e2007-01-26 08:05:27 +00001970TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Chris Lattner58af2a12006-02-15 07:22:58 +00001971 CurModule.CurrentModule->setTargetTriple($3);
1972 free($3);
John Criswell2f6a8b12006-10-24 19:09:48 +00001973 }
Chris Lattner1ae022f2006-10-22 06:08:13 +00001974 | DATALAYOUT '=' STRINGCONSTANT {
Owen Anderson1dc69692006-10-18 02:21:48 +00001975 CurModule.CurrentModule->setDataLayout($3);
1976 free($3);
Owen Anderson1dc69692006-10-18 02:21:48 +00001977 };
Chris Lattner58af2a12006-02-15 07:22:58 +00001978
1979LibrariesDefinition : '[' LibList ']';
1980
1981LibList : LibList ',' STRINGCONSTANT {
1982 CurModule.CurrentModule->addLibrary($3);
1983 free($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001984 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001985 }
1986 | STRINGCONSTANT {
1987 CurModule.CurrentModule->addLibrary($1);
1988 free($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001989 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001990 }
1991 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00001992 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001993 }
1994 ;
1995
1996//===----------------------------------------------------------------------===//
1997// Rules to match Function Headers
1998//===----------------------------------------------------------------------===//
1999
Reid Spencer41dff5e2007-01-26 08:05:27 +00002000ArgListH : ArgListH ',' Types OptParamAttrs OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002001 if (!UpRefs.empty())
2002 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
2003 if (*$3 == Type::VoidTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00002004 GEN_ERROR("void typed arguments are invalid");
Reid Spencer14310612006-12-31 05:40:51 +00002005 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00002006 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002007 $1->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002008 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002009 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002010 | Types OptParamAttrs OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002011 if (!UpRefs.empty())
2012 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2013 if (*$1 == Type::VoidTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00002014 GEN_ERROR("void typed arguments are invalid");
Reid Spencer14310612006-12-31 05:40:51 +00002015 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2016 $$ = new ArgListType;
2017 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002018 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002019 };
2020
2021ArgList : ArgListH {
2022 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002023 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002024 }
2025 | ArgListH ',' DOTDOTDOT {
2026 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002027 struct ArgListEntry E;
2028 E.Ty = new PATypeHolder(Type::VoidTy);
2029 E.Name = 0;
2030 E.Attrs = FunctionType::NoAttributeSet;
2031 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002032 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002033 }
2034 | DOTDOTDOT {
Reid Spencer14310612006-12-31 05:40:51 +00002035 $$ = new ArgListType;
2036 struct ArgListEntry E;
2037 E.Ty = new PATypeHolder(Type::VoidTy);
2038 E.Name = 0;
2039 E.Attrs = FunctionType::NoAttributeSet;
2040 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002041 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002042 }
2043 | /* empty */ {
2044 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00002045 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002046 };
2047
Reid Spencer41dff5e2007-01-26 08:05:27 +00002048FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
Reid Spencer218ded22007-01-05 17:07:23 +00002049 OptFuncAttrs OptSection OptAlign {
Chris Lattner58af2a12006-02-15 07:22:58 +00002050 UnEscapeLexed($3);
2051 std::string FunctionName($3);
2052 free($3); // Free strdup'd memory!
2053
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002054 // Check the function result for abstractness if this is a define. We should
2055 // have no abstract types at this point
Reid Spencer218ded22007-01-05 17:07:23 +00002056 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2057 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002058
Chris Lattner58af2a12006-02-15 07:22:58 +00002059 std::vector<const Type*> ParamTypeList;
Reid Spencer14310612006-12-31 05:40:51 +00002060 std::vector<FunctionType::ParameterAttributes> ParamAttrs;
Reid Spencer218ded22007-01-05 17:07:23 +00002061 ParamAttrs.push_back($7);
Chris Lattner58af2a12006-02-15 07:22:58 +00002062 if ($5) { // If there are arguments...
Reid Spencer14310612006-12-31 05:40:51 +00002063 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I) {
2064 const Type* Ty = I->Ty->get();
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002065 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2066 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencer14310612006-12-31 05:40:51 +00002067 ParamTypeList.push_back(Ty);
2068 if (Ty != Type::VoidTy)
2069 ParamAttrs.push_back(I->Attrs);
2070 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002071 }
2072
2073 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2074 if (isVarArg) ParamTypeList.pop_back();
2075
Reid Spencer218ded22007-01-05 17:07:23 +00002076 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg,
Reid Spencer14310612006-12-31 05:40:51 +00002077 ParamAttrs);
Chris Lattner58af2a12006-02-15 07:22:58 +00002078 const PointerType *PFT = PointerType::get(FT);
Reid Spencer218ded22007-01-05 17:07:23 +00002079 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002080
2081 ValID ID;
2082 if (!FunctionName.empty()) {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002083 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattner58af2a12006-02-15 07:22:58 +00002084 } else {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002085 ID = ValID::createGlobalID(CurModule.Values[PFT].size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002086 }
2087
2088 Function *Fn = 0;
2089 // See if this function was forward referenced. If so, recycle the object.
2090 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2091 // Move the function to the end of the list, from whereever it was
2092 // previously inserted.
2093 Fn = cast<Function>(FWRef);
2094 CurModule.CurrentModule->getFunctionList().remove(Fn);
2095 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2096 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spenceref9b9a72007-02-05 20:47:22 +00002097 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
2098 if (Fn->getFunctionType() != FT ) {
2099 // The existing function doesn't have the same type. This is an overload
2100 // error.
2101 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
2102 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
2103 // Neither the existing or the current function is a declaration and they
2104 // have the same name and same type. Clearly this is a redefinition.
Reid Spencerb5334b02007-02-05 10:18:06 +00002105 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002106 } if (Fn->isDeclaration()) {
2107 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattner58af2a12006-02-15 07:22:58 +00002108 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2109 AI != AE; ++AI)
2110 AI->setName("");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002111 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002112 } else { // Not already defined?
2113 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
2114 CurModule.CurrentModule);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002115
Chris Lattner58af2a12006-02-15 07:22:58 +00002116 InsertValue(Fn, CurModule.Values);
2117 }
2118
2119 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002120
2121 if (CurFun.isDeclare) {
2122 // If we have declaration, always overwrite linkage. This will allow us to
2123 // correctly handle cases, when pointer to function is passed as argument to
2124 // another function.
2125 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002126 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002127 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002128 Fn->setCallingConv($1);
Reid Spencer218ded22007-01-05 17:07:23 +00002129 Fn->setAlignment($9);
2130 if ($8) {
2131 Fn->setSection($8);
2132 free($8);
Chris Lattner58af2a12006-02-15 07:22:58 +00002133 }
2134
2135 // Add all of the arguments we parsed to the function...
2136 if ($5) { // Is null if empty...
2137 if (isVarArg) { // Nuke the last entry
Reid Spenceref9b9a72007-02-05 20:47:22 +00002138 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0 &&
Reid Spencera9720f52007-02-05 17:04:00 +00002139 "Not a varargs marker!");
Reid Spencer14310612006-12-31 05:40:51 +00002140 delete $5->back().Ty;
Chris Lattner58af2a12006-02-15 07:22:58 +00002141 $5->pop_back(); // Delete the last entry
2142 }
2143 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002144 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencer14310612006-12-31 05:40:51 +00002145 unsigned Idx = 1;
Reid Spenceref9b9a72007-02-05 20:47:22 +00002146 for (ArgListType::iterator I = $5->begin();
2147 I != $5->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencer14310612006-12-31 05:40:51 +00002148 delete I->Ty; // Delete the typeholder...
Reid Spenceref9b9a72007-02-05 20:47:22 +00002149 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002150 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002151 InsertValue(ArgIt);
Reid Spencer14310612006-12-31 05:40:51 +00002152 Idx++;
Chris Lattner58af2a12006-02-15 07:22:58 +00002153 }
Reid Spencera132e042006-12-03 05:46:11 +00002154
Chris Lattner58af2a12006-02-15 07:22:58 +00002155 delete $5; // We're now done with the argument list
2156 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002157 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002158};
2159
2160BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2161
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002162FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner58af2a12006-02-15 07:22:58 +00002163 $$ = CurFun.CurrentFunction;
2164
2165 // Make sure that we keep track of the linkage type even if there was a
2166 // previous "declare".
2167 $$->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002168 $$->setVisibility($2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002169};
2170
2171END : ENDTOK | '}'; // Allow end of '}' to end a function
2172
2173Function : BasicBlockList END {
2174 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002175 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002176};
2177
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002178FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencer14310612006-12-31 05:40:51 +00002179 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002180 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002181 $$ = CurFun.CurrentFunction;
2182 CurFun.FunctionDone();
2183 CHECK_FOR_ERROR
2184 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002185
2186//===----------------------------------------------------------------------===//
2187// Rules to match Basic Blocks
2188//===----------------------------------------------------------------------===//
2189
2190OptSideEffect : /* empty */ {
2191 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002192 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002193 }
2194 | SIDEEFFECT {
2195 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002196 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002197 };
2198
2199ConstValueRef : ESINT64VAL { // A reference to a direct constant
2200 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002201 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002202 }
2203 | EUINT64VAL {
2204 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002205 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002206 }
2207 | FPVAL { // Perhaps it's an FP constant?
2208 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002209 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002210 }
2211 | TRUETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002212 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002213 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002214 }
2215 | FALSETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002216 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002217 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002218 }
2219 | NULL_TOK {
2220 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002221 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002222 }
2223 | UNDEF {
2224 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002225 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002226 }
2227 | ZEROINITIALIZER { // A vector zero constant.
2228 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002229 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002230 }
2231 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencera132e042006-12-03 05:46:11 +00002232 const Type *ETy = (*$2)[0]->getType();
Chris Lattner58af2a12006-02-15 07:22:58 +00002233 int NumElements = $2->size();
2234
2235 PackedType* pt = PackedType::get(ETy, NumElements);
2236 PATypeHolder* PTy = new PATypeHolder(
Reid Spencera132e042006-12-03 05:46:11 +00002237 HandleUpRefs(
2238 PackedType::get(
2239 ETy,
2240 NumElements)
2241 )
2242 );
Chris Lattner58af2a12006-02-15 07:22:58 +00002243
2244 // Verify all elements are correct type!
2245 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00002246 if (ETy != (*$2)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002247 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00002248 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencera132e042006-12-03 05:46:11 +00002249 (*$2)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00002250 }
2251
Reid Spencera132e042006-12-03 05:46:11 +00002252 $$ = ValID::create(ConstantPacked::get(pt, *$2));
Chris Lattner58af2a12006-02-15 07:22:58 +00002253 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002254 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002255 }
2256 | ConstExpr {
Reid Spencera132e042006-12-03 05:46:11 +00002257 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002258 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002259 }
2260 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
2261 char *End = UnEscapeLexed($3, true);
2262 std::string AsmStr = std::string($3, End);
2263 End = UnEscapeLexed($5, true);
2264 std::string Constraints = std::string($5, End);
2265 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
2266 free($3);
2267 free($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002268 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002269 };
2270
2271// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2272// another value.
2273//
Reid Spencer41dff5e2007-01-26 08:05:27 +00002274SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2275 $$ = ValID::createLocalID($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002276 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002277 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002278 | GLOBALVAL_ID {
2279 $$ = ValID::createGlobalID($1);
2280 CHECK_FOR_ERROR
2281 }
2282 | LocalName { // Is it a named reference...?
2283 $$ = ValID::createLocalName($1);
2284 CHECK_FOR_ERROR
2285 }
2286 | GlobalName { // Is it a named reference...?
2287 $$ = ValID::createGlobalName($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002288 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002289 };
2290
2291// ValueRef - A reference to a definition... either constant or symbolic
2292ValueRef : SymbolicValueRef | ConstValueRef;
2293
2294
2295// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2296// type immediately preceeds the value reference, and allows complex constant
2297// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2298ResolvedVal : Types ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002299 if (!UpRefs.empty())
2300 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2301 $$ = getVal(*$1, $2);
2302 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002303 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00002304 }
2305 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002306
2307BasicBlockList : BasicBlockList BasicBlock {
2308 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002309 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002310 }
2311 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
2312 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002313 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002314 };
2315
2316
2317// Basic blocks are terminated by branching instructions:
2318// br, br/cc, switch, ret
2319//
Reid Spencer41dff5e2007-01-26 08:05:27 +00002320BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner58af2a12006-02-15 07:22:58 +00002321 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002322 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002323 InsertValue($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002324 $1->getInstList().push_back($3);
2325 InsertValue($1);
2326 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002327 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002328 };
2329
2330InstructionList : InstructionList Inst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002331 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2332 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2333 if (CI2->getParent() == 0)
2334 $1->getInstList().push_back(CI2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002335 $1->getInstList().push_back($2);
2336 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002337 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002338 }
2339 | /* empty */ {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002340 $$ = getBBVal(ValID::createLocalID(CurFun.NextBBNum++), true);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002341 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002342
2343 // Make sure to move the basic block to the correct location in the
2344 // function, instead of leaving it inserted wherever it was first
2345 // referenced.
2346 Function::BasicBlockListType &BBL =
2347 CurFun.CurrentFunction->getBasicBlockList();
2348 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer61c83e02006-08-18 08:43:06 +00002349 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002350 }
2351 | LABELSTR {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002352 $$ = getBBVal(ValID::createLocalName($1), true);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002353 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002354
2355 // Make sure to move the basic block to the correct location in the
2356 // function, instead of leaving it inserted wherever it was first
2357 // referenced.
2358 Function::BasicBlockListType &BBL =
2359 CurFun.CurrentFunction->getBasicBlockList();
2360 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer61c83e02006-08-18 08:43:06 +00002361 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002362 };
2363
2364BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencera132e042006-12-03 05:46:11 +00002365 $$ = new ReturnInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002366 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002367 }
2368 | RET VOID { // Return with no result...
2369 $$ = new ReturnInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002370 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002371 }
2372 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002373 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002374 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002375 $$ = new BranchInst(tmpBB);
Chris Lattner58af2a12006-02-15 07:22:58 +00002376 } // Conditional Branch...
Reid Spencer6f407902007-01-13 05:00:46 +00002377 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
2378 assert(cast<IntegerType>($2)->getBitWidth() == 1 && "Not Bool?");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002379 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002380 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002381 BasicBlock* tmpBBB = getBBVal($9);
2382 CHECK_FOR_ERROR
Reid Spencer4fe16d62007-01-11 18:21:29 +00002383 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002384 CHECK_FOR_ERROR
2385 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattner58af2a12006-02-15 07:22:58 +00002386 }
2387 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002388 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002389 CHECK_FOR_ERROR
2390 BasicBlock* tmpBB = getBBVal($6);
2391 CHECK_FOR_ERROR
2392 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002393 $$ = S;
2394
2395 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2396 E = $8->end();
2397 for (; I != E; ++I) {
2398 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2399 S->addCase(CI, I->second);
2400 else
Reid Spencerb5334b02007-02-05 10:18:06 +00002401 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattner58af2a12006-02-15 07:22:58 +00002402 }
2403 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002404 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002405 }
2406 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002407 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002408 CHECK_FOR_ERROR
2409 BasicBlock* tmpBB = getBBVal($6);
2410 CHECK_FOR_ERROR
2411 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattner58af2a12006-02-15 07:22:58 +00002412 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002413 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002414 }
Reid Spencer218ded22007-01-05 17:07:23 +00002415 | INVOKE OptCallingConv ResultTypes ValueRef '(' ValueRefList ')' OptFuncAttrs
Chris Lattner58af2a12006-02-15 07:22:58 +00002416 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattner58af2a12006-02-15 07:22:58 +00002417
Reid Spencer14310612006-12-31 05:40:51 +00002418 // Handle the short syntax
2419 const PointerType *PFTy = 0;
2420 const FunctionType *Ty = 0;
Reid Spencer218ded22007-01-05 17:07:23 +00002421 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00002422 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2423 // Pull out the types of all of the arguments...
2424 std::vector<const Type*> ParamTypes;
Reid Spencer14310612006-12-31 05:40:51 +00002425 FunctionType::ParamAttrsList ParamAttrs;
Reid Spencer218ded22007-01-05 17:07:23 +00002426 ParamAttrs.push_back($8);
Reid Spencer14310612006-12-31 05:40:51 +00002427 for (ValueRefList::iterator I = $6->begin(), E = $6->end(); I != E; ++I) {
2428 const Type *Ty = I->Val->getType();
2429 if (Ty == Type::VoidTy)
2430 GEN_ERROR("Short call syntax cannot be used with varargs");
2431 ParamTypes.push_back(Ty);
2432 ParamAttrs.push_back(I->Attrs);
Chris Lattner58af2a12006-02-15 07:22:58 +00002433 }
2434
Reid Spencer218ded22007-01-05 17:07:23 +00002435 Ty = FunctionType::get($3->get(), ParamTypes, false, ParamAttrs);
Chris Lattner58af2a12006-02-15 07:22:58 +00002436 PFTy = PointerType::get(Ty);
2437 }
2438
2439 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002440 CHECK_FOR_ERROR
Reid Spencer218ded22007-01-05 17:07:23 +00002441 BasicBlock *Normal = getBBVal($11);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002442 CHECK_FOR_ERROR
Reid Spencer218ded22007-01-05 17:07:23 +00002443 BasicBlock *Except = getBBVal($14);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002444 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002445
Reid Spencer14310612006-12-31 05:40:51 +00002446 // Check the arguments
2447 ValueList Args;
2448 if ($6->empty()) { // Has no arguments?
2449 // Make sure no arguments is a good thing!
2450 if (Ty->getNumParams() != 0)
2451 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00002452 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00002453 } else { // Has arguments?
2454 // Loop through FunctionType's arguments and ensure they are specified
2455 // correctly!
Chris Lattner58af2a12006-02-15 07:22:58 +00002456 FunctionType::param_iterator I = Ty->param_begin();
2457 FunctionType::param_iterator E = Ty->param_end();
Reid Spencer14310612006-12-31 05:40:51 +00002458 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner58af2a12006-02-15 07:22:58 +00002459
Reid Spencer14310612006-12-31 05:40:51 +00002460 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2461 if (ArgI->Val->getType() != *I)
2462 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00002463 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00002464 Args.push_back(ArgI->Val);
2465 }
Reid Spencera132e042006-12-03 05:46:11 +00002466
Reid Spencer14310612006-12-31 05:40:51 +00002467 if (Ty->isVarArg()) {
2468 if (I == E)
2469 for (; ArgI != ArgE; ++ArgI)
2470 Args.push_back(ArgI->Val); // push the remaining varargs
2471 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00002472 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00002473 }
Reid Spencer14310612006-12-31 05:40:51 +00002474
2475 // Create the InvokeInst
2476 InvokeInst *II = new InvokeInst(V, Normal, Except, Args);
2477 II->setCallingConv($2);
2478 $$ = II;
Chris Lattner58af2a12006-02-15 07:22:58 +00002479 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002480 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002481 }
2482 | UNWIND {
2483 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002484 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002485 }
2486 | UNREACHABLE {
2487 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002488 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002489 };
2490
2491
2492
2493JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2494 $$ = $1;
Reid Spencera132e042006-12-03 05:46:11 +00002495 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002496 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002497 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002498 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00002499
Reid Spencer5b7e7532006-09-28 19:28:24 +00002500 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002501 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002502 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002503 }
2504 | IntType ConstValueRef ',' LABEL ValueRef {
2505 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencera132e042006-12-03 05:46:11 +00002506 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002507 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002508
2509 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002510 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00002511
Reid Spencer5b7e7532006-09-28 19:28:24 +00002512 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002513 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002514 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002515 };
2516
Reid Spencer41dff5e2007-01-26 08:05:27 +00002517Inst : OptLocalAssign InstVal {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002518 // Is this definition named?? if so, assign the name...
2519 setValueName($2, $1);
2520 CHECK_FOR_ERROR
2521 InsertValue($2);
2522 $$ = $2;
2523 CHECK_FOR_ERROR
2524 };
2525
Chris Lattner58af2a12006-02-15 07:22:58 +00002526
2527PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer14310612006-12-31 05:40:51 +00002528 if (!UpRefs.empty())
2529 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002530 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencera132e042006-12-03 05:46:11 +00002531 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002532 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002533 BasicBlock* tmpBB = getBBVal($5);
2534 CHECK_FOR_ERROR
2535 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencera132e042006-12-03 05:46:11 +00002536 delete $1;
Chris Lattner58af2a12006-02-15 07:22:58 +00002537 }
2538 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2539 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002540 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002541 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002542 BasicBlock* tmpBB = getBBVal($6);
2543 CHECK_FOR_ERROR
2544 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002545 };
2546
2547
Reid Spencer14310612006-12-31 05:40:51 +00002548ValueRefList : Types ValueRef OptParamAttrs {
2549 if (!UpRefs.empty())
2550 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2551 // Used for call and invoke instructions
2552 $$ = new ValueRefList();
2553 ValueRefListEntry E; E.Attrs = $3; E.Val = getVal($1->get(), $2);
2554 $$->push_back(E);
Chris Lattner58af2a12006-02-15 07:22:58 +00002555 }
Reid Spencer14310612006-12-31 05:40:51 +00002556 | ValueRefList ',' Types ValueRef OptParamAttrs {
2557 if (!UpRefs.empty())
2558 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002559 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002560 ValueRefListEntry E; E.Attrs = $5; E.Val = getVal($3->get(), $4);
2561 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002562 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00002563 }
2564 | /*empty*/ { $$ = new ValueRefList(); };
Chris Lattner58af2a12006-02-15 07:22:58 +00002565
Reid Spencer14310612006-12-31 05:40:51 +00002566IndexList // Used for gep instructions and constant expressions
Reid Spencerc6c59fd2006-12-31 21:47:02 +00002567 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencer14310612006-12-31 05:40:51 +00002568 | IndexList ',' ResolvedVal {
2569 $$ = $1;
2570 $$->push_back($3);
2571 CHECK_FOR_ERROR
2572 }
Reid Spencerc6c59fd2006-12-31 21:47:02 +00002573 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002574
2575OptTailCall : TAIL CALL {
2576 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002577 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002578 }
2579 | CALL {
2580 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002581 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002582 };
2583
Chris Lattner58af2a12006-02-15 07:22:58 +00002584InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002585 if (!UpRefs.empty())
2586 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002587 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencera132e042006-12-03 05:46:11 +00002588 !isa<PackedType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002589 GEN_ERROR(
Reid Spencerb5334b02007-02-05 10:18:06 +00002590 "Arithmetic operator requires integer, FP, or packed operands");
Reid Spencera132e042006-12-03 05:46:11 +00002591 if (isa<PackedType>((*$2).get()) &&
2592 ($1 == Instruction::URem ||
2593 $1 == Instruction::SRem ||
2594 $1 == Instruction::FRem))
Reid Spenceref9b9a72007-02-05 20:47:22 +00002595 GEN_ERROR("Remainder not supported on packed types");
Reid Spencera132e042006-12-03 05:46:11 +00002596 Value* val1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002597 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002598 Value* val2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002599 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002600 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002601 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002602 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00002603 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002604 }
2605 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002606 if (!UpRefs.empty())
2607 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002608 if (!(*$2)->isInteger()) {
Reid Spencer832254e2007-02-02 02:16:23 +00002609 if (Instruction::isShift($1) || !isa<PackedType>($2->get()) ||
Chris Lattner42a75512007-01-15 02:27:26 +00002610 !cast<PackedType>($2->get())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00002611 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00002612 }
Reid Spencera132e042006-12-03 05:46:11 +00002613 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002614 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002615 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002616 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002617 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002618 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002619 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00002620 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002621 }
Reid Spencera132e042006-12-03 05:46:11 +00002622 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002623 if (!UpRefs.empty())
2624 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencerac4a1dd2007-01-04 02:57:52 +00002625 if (isa<PackedType>((*$3).get()))
2626 GEN_ERROR("Packed types not supported by icmp instruction");
Reid Spencera132e042006-12-03 05:46:11 +00002627 Value* tmpVal1 = getVal(*$3, $4);
2628 CHECK_FOR_ERROR
2629 Value* tmpVal2 = getVal(*$3, $6);
2630 CHECK_FOR_ERROR
2631 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2632 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002633 GEN_ERROR("icmp operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00002634 }
2635 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002636 if (!UpRefs.empty())
2637 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencerac4a1dd2007-01-04 02:57:52 +00002638 if (isa<PackedType>((*$3).get()))
2639 GEN_ERROR("Packed types not supported by fcmp instruction");
Reid Spencera132e042006-12-03 05:46:11 +00002640 Value* tmpVal1 = getVal(*$3, $4);
2641 CHECK_FOR_ERROR
2642 Value* tmpVal2 = getVal(*$3, $6);
2643 CHECK_FOR_ERROR
2644 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2645 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002646 GEN_ERROR("fcmp operator returned null");
Chris Lattner58af2a12006-02-15 07:22:58 +00002647 }
Reid Spencer3da59db2006-11-27 01:05:10 +00002648 | CastOps ResolvedVal TO Types {
Reid Spencer14310612006-12-31 05:40:51 +00002649 if (!UpRefs.empty())
2650 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00002651 Value* Val = $2;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00002652 const Type* DestTy = $4->get();
2653 if (!CastInst::castIsValid($1, Val, DestTy))
2654 GEN_ERROR("invalid cast opcode for cast from '" +
2655 Val->getType()->getDescription() + "' to '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00002656 DestTy->getDescription() + "'");
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00002657 $$ = CastInst::create($1, Val, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00002658 delete $4;
Chris Lattner58af2a12006-02-15 07:22:58 +00002659 }
2660 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer4fe16d62007-01-11 18:21:29 +00002661 if ($2->getType() != Type::Int1Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00002662 GEN_ERROR("select condition must be boolean");
Reid Spencera132e042006-12-03 05:46:11 +00002663 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002664 GEN_ERROR("select value types should match");
Reid Spencera132e042006-12-03 05:46:11 +00002665 $$ = new SelectInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002666 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002667 }
2668 | VAARG ResolvedVal ',' Types {
Reid Spencer14310612006-12-31 05:40:51 +00002669 if (!UpRefs.empty())
2670 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00002671 $$ = new VAArgInst($2, *$4);
2672 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002673 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002674 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002675 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002676 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerb5334b02007-02-05 10:18:06 +00002677 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00002678 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002679 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002680 }
2681 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002682 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00002683 GEN_ERROR("Invalid insertelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00002684 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002685 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002686 }
Chris Lattnerd5efe842006-04-08 01:18:56 +00002687 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002688 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00002689 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00002690 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002691 CHECK_FOR_ERROR
Chris Lattnerd5efe842006-04-08 01:18:56 +00002692 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002693 | PHI_TOK PHIList {
2694 const Type *Ty = $2->front().first->getType();
2695 if (!Ty->isFirstClassType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002696 GEN_ERROR("PHI node operands must be of first class type");
Chris Lattner58af2a12006-02-15 07:22:58 +00002697 $$ = new PHINode(Ty);
2698 ((PHINode*)$$)->reserveOperandSpace($2->size());
2699 while ($2->begin() != $2->end()) {
2700 if ($2->front().first->getType() != Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00002701 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattner58af2a12006-02-15 07:22:58 +00002702 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
2703 $2->pop_front();
2704 }
2705 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00002706 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002707 }
Reid Spencer218ded22007-01-05 17:07:23 +00002708 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ValueRefList ')'
2709 OptFuncAttrs {
Reid Spencer14310612006-12-31 05:40:51 +00002710
2711 // Handle the short syntax
Reid Spencer3da59db2006-11-27 01:05:10 +00002712 const PointerType *PFTy = 0;
2713 const FunctionType *Ty = 0;
Reid Spencer218ded22007-01-05 17:07:23 +00002714 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00002715 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2716 // Pull out the types of all of the arguments...
2717 std::vector<const Type*> ParamTypes;
Reid Spencer14310612006-12-31 05:40:51 +00002718 FunctionType::ParamAttrsList ParamAttrs;
Reid Spencer218ded22007-01-05 17:07:23 +00002719 ParamAttrs.push_back($8);
Reid Spencer14310612006-12-31 05:40:51 +00002720 for (ValueRefList::iterator I = $6->begin(), E = $6->end(); I != E; ++I) {
2721 const Type *Ty = I->Val->getType();
2722 if (Ty == Type::VoidTy)
2723 GEN_ERROR("Short call syntax cannot be used with varargs");
2724 ParamTypes.push_back(Ty);
2725 ParamAttrs.push_back(I->Attrs);
Chris Lattner58af2a12006-02-15 07:22:58 +00002726 }
2727
Reid Spencer218ded22007-01-05 17:07:23 +00002728 Ty = FunctionType::get($3->get(), ParamTypes, false, ParamAttrs);
Chris Lattner58af2a12006-02-15 07:22:58 +00002729 PFTy = PointerType::get(Ty);
2730 }
2731
2732 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002733 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002734
Reid Spencer14310612006-12-31 05:40:51 +00002735 // Check the arguments
2736 ValueList Args;
2737 if ($6->empty()) { // Has no arguments?
Chris Lattner58af2a12006-02-15 07:22:58 +00002738 // Make sure no arguments is a good thing!
2739 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002740 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00002741 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00002742 } else { // Has arguments?
2743 // Loop through FunctionType's arguments and ensure they are specified
2744 // correctly!
2745 //
2746 FunctionType::param_iterator I = Ty->param_begin();
2747 FunctionType::param_iterator E = Ty->param_end();
Reid Spencer14310612006-12-31 05:40:51 +00002748 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner58af2a12006-02-15 07:22:58 +00002749
Reid Spencer14310612006-12-31 05:40:51 +00002750 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2751 if (ArgI->Val->getType() != *I)
2752 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00002753 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00002754 Args.push_back(ArgI->Val);
2755 }
2756 if (Ty->isVarArg()) {
2757 if (I == E)
2758 for (; ArgI != ArgE; ++ArgI)
2759 Args.push_back(ArgI->Val); // push the remaining varargs
2760 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00002761 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00002762 }
Reid Spencer14310612006-12-31 05:40:51 +00002763 // Create the call node
2764 CallInst *CI = new CallInst(V, Args);
2765 CI->setTailCall($1);
2766 CI->setCallingConv($2);
2767 $$ = CI;
Chris Lattner58af2a12006-02-15 07:22:58 +00002768 delete $6;
Reid Spencer41dff5e2007-01-26 08:05:27 +00002769 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002770 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002771 }
2772 | MemoryInst {
2773 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002774 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002775 };
2776
Chris Lattner58af2a12006-02-15 07:22:58 +00002777OptVolatile : VOLATILE {
2778 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002779 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002780 }
2781 | /* empty */ {
2782 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002783 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002784 };
2785
2786
2787
2788MemoryInst : MALLOC Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00002789 if (!UpRefs.empty())
2790 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00002791 $$ = new MallocInst(*$2, 0, $3);
2792 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002793 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002794 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00002795 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00002796 if (!UpRefs.empty())
2797 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00002798 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002799 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002800 $$ = new MallocInst(*$2, tmpVal, $6);
2801 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002802 }
2803 | ALLOCA Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00002804 if (!UpRefs.empty())
2805 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00002806 $$ = new AllocaInst(*$2, 0, $3);
2807 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002808 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002809 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00002810 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00002811 if (!UpRefs.empty())
2812 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00002813 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002814 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002815 $$ = new AllocaInst(*$2, tmpVal, $6);
2816 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002817 }
2818 | FREE ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002819 if (!isa<PointerType>($2->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002820 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerb5334b02007-02-05 10:18:06 +00002821 $2->getType()->getDescription() + "");
Reid Spencera132e042006-12-03 05:46:11 +00002822 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002823 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002824 }
2825
2826 | OptVolatile LOAD Types ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002827 if (!UpRefs.empty())
2828 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00002829 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002830 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00002831 (*$3)->getDescription());
2832 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002833 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencera132e042006-12-03 05:46:11 +00002834 (*$3)->getDescription());
2835 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002836 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002837 $$ = new LoadInst(tmpVal, "", $1);
Reid Spencera132e042006-12-03 05:46:11 +00002838 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00002839 }
2840 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002841 if (!UpRefs.empty())
2842 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00002843 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00002844 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00002845 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00002846 (*$5)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002847 const Type *ElTy = PT->getElementType();
Reid Spencera132e042006-12-03 05:46:11 +00002848 if (ElTy != $3->getType())
2849 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerb5334b02007-02-05 10:18:06 +00002850 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00002851
Reid Spencera132e042006-12-03 05:46:11 +00002852 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002853 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002854 $$ = new StoreInst($3, tmpVal, $1);
2855 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00002856 }
2857 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer14310612006-12-31 05:40:51 +00002858 if (!UpRefs.empty())
2859 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00002860 if (!isa<PointerType>($2->get()))
Reid Spencerb5334b02007-02-05 10:18:06 +00002861 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00002862
Chris Lattner7d9801d2007-02-13 00:58:01 +00002863 if (!GetElementPtrInst::getIndexedType(*$2, &(*$4)[0], $4->size(), true))
Reid Spencer61c83e02006-08-18 08:43:06 +00002864 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00002865 (*$2)->getDescription()+ "'");
Reid Spencera132e042006-12-03 05:46:11 +00002866 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002867 CHECK_FOR_ERROR
Chris Lattner7d9801d2007-02-13 00:58:01 +00002868 $$ = new GetElementPtrInst(tmpVal, &(*$4)[0], $4->size());
Reid Spencera132e042006-12-03 05:46:11 +00002869 delete $2;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002870 delete $4;
Chris Lattner58af2a12006-02-15 07:22:58 +00002871 };
2872
2873
2874%%
Reid Spencer61c83e02006-08-18 08:43:06 +00002875
Reid Spencer14310612006-12-31 05:40:51 +00002876// common code from the two 'RunVMAsmParser' functions
2877static Module* RunParser(Module * M) {
2878
2879 llvmAsmlineno = 1; // Reset the current line number...
2880 CurModule.CurrentModule = M;
2881#if YYDEBUG
2882 yydebug = Debug;
2883#endif
2884
2885 // Check to make sure the parser succeeded
2886 if (yyparse()) {
2887 if (ParserResult)
2888 delete ParserResult;
2889 return 0;
2890 }
2891
2892 // Check to make sure that parsing produced a result
2893 if (!ParserResult)
2894 return 0;
2895
2896 // Reset ParserResult variable while saving its value for the result.
2897 Module *Result = ParserResult;
2898 ParserResult = 0;
2899
2900 return Result;
2901}
2902
Reid Spencer61c83e02006-08-18 08:43:06 +00002903void llvm::GenerateError(const std::string &message, int LineNo) {
2904 if (LineNo == -1) LineNo = llvmAsmlineno;
2905 // TODO: column number in exception
2906 if (TheParseError)
2907 TheParseError->setError(CurFilename, message, LineNo);
2908 TriggerError = 1;
2909}
2910
Chris Lattner58af2a12006-02-15 07:22:58 +00002911int yyerror(const char *ErrorMsg) {
2912 std::string where
2913 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2914 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Reid Spenceref9b9a72007-02-05 20:47:22 +00002915 std::string errMsg = where + "error: " + std::string(ErrorMsg);
2916 if (yychar != YYEMPTY && yychar != 0)
2917 errMsg += " while reading token: '" + std::string(llvmAsmtext, llvmAsmleng)+
2918 "'";
Reid Spencer61c83e02006-08-18 08:43:06 +00002919 GenerateError(errMsg);
Chris Lattner58af2a12006-02-15 07:22:58 +00002920 return 0;
2921}