blob: f2b9fb0d2f6f694794961a07e39cb4a1dad48e26 [file] [log] [blame]
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the bison parser for LLVM assembly languages files.
11//
12//===----------------------------------------------------------------------===//
13
14%{
15#include "ParserInternals.h"
16#include "llvm/CallingConv.h"
17#include "llvm/InlineAsm.h"
18#include "llvm/Instructions.h"
19#include "llvm/Module.h"
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000020#include "llvm/ValueSymbolTable.h"
Chris Lattnerf20e61f2006-02-15 07:22:58 +000021#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer42f0cbe2006-12-31 05:40:51 +000022#include "llvm/Support/CommandLine.h"
Chris Lattner55ce85c2007-01-31 04:44:08 +000023#include "llvm/ADT/SmallVector.h"
Chris Lattnerf20e61f2006-02-15 07:22:58 +000024#include "llvm/ADT/STLExtras.h"
25#include "llvm/Support/MathExtras.h"
Reid Spencerd5e19442006-12-01 00:33:46 +000026#include "llvm/Support/Streams.h"
Chris Lattnerf20e61f2006-02-15 07:22:58 +000027#include <algorithm>
Chris Lattnerf20e61f2006-02-15 07:22:58 +000028#include <list>
Chris Lattnerac26f382007-02-11 21:40:10 +000029#include <map>
Chris Lattnerf20e61f2006-02-15 07:22:58 +000030#include <utility>
Reid Spencer42f0cbe2006-12-31 05:40:51 +000031#ifndef NDEBUG
32#define YYDEBUG 1
33#endif
Chris Lattnerf20e61f2006-02-15 07:22:58 +000034
Reid Spencerb50974a2006-08-18 17:32:55 +000035// The following is a gross hack. In order to rid the libAsmParser library of
36// exceptions, we have to have a way of getting the yyparse function to go into
37// an error situation. So, whenever we want an error to occur, the GenerateError
38// function (see bottom of file) sets TriggerError. Then, at the end of each
39// production in the grammer we use CHECK_FOR_ERROR which will invoke YYERROR
40// (a goto) to put YACC in error state. Furthermore, several calls to
41// GenerateError are made from inside productions and they must simulate the
42// previous exception behavior by exiting the production immediately. We have
43// replaced these with the GEN_ERROR macro which calls GeneratError and then
44// immediately invokes YYERROR. This would be so much cleaner if it was a
45// recursive descent parser.
Reid Spencer713eedc2006-08-18 08:43:06 +000046static bool TriggerError = false;
Reid Spencerff359002006-10-09 17:36:59 +000047#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYABORT; } }
Reid Spencer713eedc2006-08-18 08:43:06 +000048#define GEN_ERROR(msg) { GenerateError(msg); YYERROR; }
49
Chris Lattnerf20e61f2006-02-15 07:22:58 +000050int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
51int yylex(); // declaration" of xxx warnings.
52int yyparse();
53
54namespace llvm {
55 std::string CurFilename;
Reid Spencer42f0cbe2006-12-31 05:40:51 +000056#if YYDEBUG
57static cl::opt<bool>
58Debug("debug-yacc", cl::desc("Print yacc debug state changes"),
59 cl::Hidden, cl::init(false));
60#endif
Chris Lattnerf20e61f2006-02-15 07:22:58 +000061}
62using namespace llvm;
63
64static Module *ParserResult;
65
66// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
67// relating to upreferences in the input stream.
68//
69//#define DEBUG_UPREFS 1
70#ifdef DEBUG_UPREFS
Bill Wendlingf3baad32006-12-07 01:30:32 +000071#define UR_OUT(X) cerr << X
Chris Lattnerf20e61f2006-02-15 07:22:58 +000072#else
73#define UR_OUT(X)
74#endif
75
76#define YYERROR_VERBOSE 1
77
Chris Lattnerf20e61f2006-02-15 07:22:58 +000078static GlobalVariable *CurGV;
79
80
81// This contains info used when building the body of a function. It is
82// destroyed when the function is completed.
83//
84typedef std::vector<Value *> ValueList; // Numbered defs
Reid Spencer42f0cbe2006-12-31 05:40:51 +000085
Chris Lattnerf20e61f2006-02-15 07:22:58 +000086static void
Reid Spencerd0e8d382007-03-19 18:40:50 +000087ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers=0);
Chris Lattnerf20e61f2006-02-15 07:22:58 +000088
89static struct PerModuleInfo {
90 Module *CurrentModule;
Reid Spencerd0e8d382007-03-19 18:40:50 +000091 ValueList Values; // Module level numbered definitions
92 ValueList LateResolveValues;
Reid Spencer55f1fbe2006-11-28 07:29:44 +000093 std::vector<PATypeHolder> Types;
94 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattnerf20e61f2006-02-15 07:22:58 +000095
96 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Chris Lattner7aa45902006-06-21 16:53:00 +000097 /// how they were referenced and on which line of the input they came from so
Chris Lattnerf20e61f2006-02-15 07:22:58 +000098 /// that we can resolve them later and print error messages as appropriate.
99 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
100
101 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
102 // references to global values. Global values may be referenced before they
103 // are defined, and if so, the temporary object that they represent is held
104 // here. This is used for forward references of GlobalValues.
105 //
106 typedef std::map<std::pair<const PointerType *,
107 ValID>, GlobalValue*> GlobalRefsType;
108 GlobalRefsType GlobalRefs;
109
110 void ModuleDone() {
111 // If we could not resolve some functions at function compilation time
112 // (calls to functions before they are defined), resolve them now... Types
113 // are resolved when the constant pool has been completely parsed.
114 //
115 ResolveDefinitions(LateResolveValues);
Reid Spencer309080a2006-09-28 19:28:24 +0000116 if (TriggerError)
117 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000118
119 // Check to make sure that all global value forward references have been
120 // resolved!
121 //
122 if (!GlobalRefs.empty()) {
123 std::string UndefinedReferences = "Unresolved global references exist:\n";
124
125 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
126 I != E; ++I) {
127 UndefinedReferences += " " + I->first.first->getDescription() + " " +
128 I->first.second.getName() + "\n";
129 }
Reid Spencer713eedc2006-08-18 08:43:06 +0000130 GenerateError(UndefinedReferences);
Reid Spencer309080a2006-09-28 19:28:24 +0000131 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000132 }
133
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000134 Values.clear(); // Clear out function local definitions
135 Types.clear();
136 CurrentModule = 0;
137 }
138
139 // GetForwardRefForGlobal - Check to see if there is a forward reference
140 // for this global. If so, remove it from the GlobalRefs map and return it.
141 // If not, just return null.
142 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
143 // Check to see if there is a forward reference to this global variable...
144 // if there is, eliminate it and patch the reference to use the new def'n.
145 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
146 GlobalValue *Ret = 0;
147 if (I != GlobalRefs.end()) {
148 Ret = I->second;
149 GlobalRefs.erase(I);
150 }
151 return Ret;
152 }
Reid Spencer87622ae2007-01-02 21:54:12 +0000153
154 bool TypeIsUnresolved(PATypeHolder* PATy) {
155 // If it isn't abstract, its resolved
156 const Type* Ty = PATy->get();
157 if (!Ty->isAbstract())
158 return false;
159 // Traverse the type looking for abstract types. If it isn't abstract then
160 // we don't need to traverse that leg of the type.
161 std::vector<const Type*> WorkList, SeenList;
162 WorkList.push_back(Ty);
163 while (!WorkList.empty()) {
164 const Type* Ty = WorkList.back();
165 SeenList.push_back(Ty);
166 WorkList.pop_back();
167 if (const OpaqueType* OpTy = dyn_cast<OpaqueType>(Ty)) {
168 // Check to see if this is an unresolved type
169 std::map<ValID, PATypeHolder>::iterator I = LateResolveTypes.begin();
170 std::map<ValID, PATypeHolder>::iterator E = LateResolveTypes.end();
171 for ( ; I != E; ++I) {
172 if (I->second.get() == OpTy)
173 return true;
174 }
175 } else if (const SequentialType* SeqTy = dyn_cast<SequentialType>(Ty)) {
176 const Type* TheTy = SeqTy->getElementType();
177 if (TheTy->isAbstract() && TheTy != Ty) {
178 std::vector<const Type*>::iterator I = SeenList.begin(),
179 E = SeenList.end();
180 for ( ; I != E; ++I)
181 if (*I == TheTy)
182 break;
183 if (I == E)
184 WorkList.push_back(TheTy);
185 }
186 } else if (const StructType* StrTy = dyn_cast<StructType>(Ty)) {
187 for (unsigned i = 0; i < StrTy->getNumElements(); ++i) {
188 const Type* TheTy = StrTy->getElementType(i);
189 if (TheTy->isAbstract() && TheTy != Ty) {
190 std::vector<const Type*>::iterator I = SeenList.begin(),
191 E = SeenList.end();
192 for ( ; I != E; ++I)
193 if (*I == TheTy)
194 break;
195 if (I == E)
196 WorkList.push_back(TheTy);
197 }
198 }
199 }
200 }
201 return false;
202 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000203} CurModule;
204
205static struct PerFunctionInfo {
206 Function *CurrentFunction; // Pointer to current function being created
207
Reid Spencerd0e8d382007-03-19 18:40:50 +0000208 ValueList Values; // Keep track of #'d definitions
209 unsigned NextValNum;
210 ValueList LateResolveValues;
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000211 bool isDeclare; // Is this function a forward declararation?
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000212 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000213 GlobalValue::VisibilityTypes Visibility;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000214
215 /// BBForwardRefs - When we see forward references to basic blocks, keep
216 /// track of them here.
Reid Spencerd0e8d382007-03-19 18:40:50 +0000217 std::map<ValID, BasicBlock*> BBForwardRefs;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000218
219 inline PerFunctionInfo() {
220 CurrentFunction = 0;
221 isDeclare = false;
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000222 Linkage = GlobalValue::ExternalLinkage;
223 Visibility = GlobalValue::DefaultVisibility;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000224 }
225
226 inline void FunctionStart(Function *M) {
227 CurrentFunction = M;
Reid Spencerd0e8d382007-03-19 18:40:50 +0000228 NextValNum = 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000229 }
230
231 void FunctionDone() {
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000232 // Any forward referenced blocks left?
Reid Spencer309080a2006-09-28 19:28:24 +0000233 if (!BBForwardRefs.empty()) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000234 GenerateError("Undefined reference to label " +
Reid Spencerd0e8d382007-03-19 18:40:50 +0000235 BBForwardRefs.begin()->second->getName());
Reid Spencer309080a2006-09-28 19:28:24 +0000236 return;
237 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000238
239 // Resolve all forward references now.
240 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
241
242 Values.clear(); // Clear out function local definitions
Reid Spencerd0e8d382007-03-19 18:40:50 +0000243 BBForwardRefs.clear();
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000244 CurrentFunction = 0;
245 isDeclare = false;
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000246 Linkage = GlobalValue::ExternalLinkage;
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000247 Visibility = GlobalValue::DefaultVisibility;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000248 }
249} CurFun; // Info for the current function...
250
251static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
252
253
254//===----------------------------------------------------------------------===//
255// Code to handle definitions of all the types
256//===----------------------------------------------------------------------===//
257
Reid Spencerd0e8d382007-03-19 18:40:50 +0000258static void InsertValue(Value *V, ValueList &ValueTab = CurFun.Values) {
259 // Things that have names or are void typed don't get slot numbers
260 if (V->hasName() || (V->getType() == Type::VoidTy))
261 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000262
Reid Spencerd0e8d382007-03-19 18:40:50 +0000263 // In the case of function values, we have to allow for the forward reference
264 // of basic blocks, which are included in the numbering. Consequently, we keep
265 // track of the next insertion location with NextValNum. When a BB gets
266 // inserted, it could change the size of the CurFun.Values vector.
267 if (&ValueTab == &CurFun.Values) {
268 if (ValueTab.size() <= CurFun.NextValNum)
269 ValueTab.resize(CurFun.NextValNum+1);
270 ValueTab[CurFun.NextValNum++] = V;
271 return;
272 }
273 // For all other lists, its okay to just tack it on the back of the vector.
274 ValueTab.push_back(V);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000275}
276
277static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
278 switch (D.Type) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000279 case ValID::LocalID: // Is it a numbered definition?
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000280 // Module constants occupy the lowest numbered slots...
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000281 if (D.Num < CurModule.Types.size())
282 return CurModule.Types[D.Num];
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000283 break;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000284 case ValID::LocalName: // Is it a named definition?
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000285 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
286 D.destroy(); // Free old strdup'd memory...
287 return N;
288 }
289 break;
290 default:
Reid Spencera7bb3e92007-02-05 10:18:06 +0000291 GenerateError("Internal parser error: Invalid symbol type reference");
Reid Spencer309080a2006-09-28 19:28:24 +0000292 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000293 }
294
295 // If we reached here, we referenced either a symbol that we don't know about
296 // or an id number that hasn't been read yet. We may be referencing something
297 // forward, so just create an entry to be resolved later and get to it...
298 //
299 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
300
301
302 if (inFunctionScope()) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000303 if (D.Type == ValID::LocalName) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000304 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer309080a2006-09-28 19:28:24 +0000305 return 0;
306 } else {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000307 GenerateError("Reference to an undefined type: #" + utostr(D.Num));
Reid Spencer309080a2006-09-28 19:28:24 +0000308 return 0;
309 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000310 }
311
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000312 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000313 if (I != CurModule.LateResolveTypes.end())
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000314 return I->second;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000315
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000316 Type *Typ = OpaqueType::get();
317 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
318 return Typ;
Reid Spencere2c32da2006-12-03 05:46:11 +0000319 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000320
Reid Spencerd0e8d382007-03-19 18:40:50 +0000321// getExistingVal - Look up the value specified by the provided type and
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000322// the provided ValID. If the value exists and has already been defined, return
323// it. Otherwise return null.
324//
Reid Spencerd0e8d382007-03-19 18:40:50 +0000325static Value *getExistingVal(const Type *Ty, const ValID &D) {
Reid Spencer309080a2006-09-28 19:28:24 +0000326 if (isa<FunctionType>(Ty)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000327 GenerateError("Functions are not values and "
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000328 "must be referenced as pointers");
Reid Spencer309080a2006-09-28 19:28:24 +0000329 return 0;
330 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000331
332 switch (D.Type) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000333 case ValID::LocalID: { // Is it a numbered definition?
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000334 // Check that the number is within bounds.
Reid Spencerd0e8d382007-03-19 18:40:50 +0000335 if (D.Num >= CurFun.Values.size())
336 return 0;
337 Value *Result = CurFun.Values[D.Num];
338 if (Ty != Result->getType()) {
339 GenerateError("Numbered value (%" + utostr(D.Num) + ") of type '" +
340 Result->getType()->getDescription() + "' does not match "
341 "expected type, '" + Ty->getDescription() + "'");
342 return 0;
343 }
344 return Result;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000345 }
346 case ValID::GlobalID: { // Is it a numbered definition?
Reid Spencerd0e8d382007-03-19 18:40:50 +0000347 if (D.Num >= CurModule.Values.size())
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000348 return 0;
Reid Spencerd0e8d382007-03-19 18:40:50 +0000349 Value *Result = CurModule.Values[D.Num];
350 if (Ty != Result->getType()) {
351 GenerateError("Numbered value (@" + utostr(D.Num) + ") of type '" +
352 Result->getType()->getDescription() + "' does not match "
353 "expected type, '" + Ty->getDescription() + "'");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000354 return 0;
Reid Spencerd0e8d382007-03-19 18:40:50 +0000355 }
356 return Result;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000357 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000358
359 case ValID::LocalName: { // Is it a named definition?
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000360 if (!inFunctionScope())
361 return 0;
362 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
363 Value *N = SymTab.lookup(D.Name);
364 if (N == 0)
365 return 0;
366 if (N->getType() != Ty)
367 return 0;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000368
369 D.destroy(); // Free old strdup'd memory...
370 return N;
371 }
372 case ValID::GlobalName: { // Is it a named definition?
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000373 ValueSymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable();
374 Value *N = SymTab.lookup(D.Name);
375 if (N == 0)
376 return 0;
377 if (N->getType() != Ty)
378 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000379
380 D.destroy(); // Free old strdup'd memory...
381 return N;
382 }
383
384 // Check to make sure that "Ty" is an integral type, and that our
385 // value will fit into the specified type...
386 case ValID::ConstSIntVal: // Is it a constant pool reference??
Reid Spencere0fc4df2006-10-20 07:07:24 +0000387 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000388 GenerateError("Signed integral constant '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000389 itostr(D.ConstPool64) + "' is invalid for type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +0000390 Ty->getDescription() + "'");
Reid Spencer309080a2006-09-28 19:28:24 +0000391 return 0;
392 }
Reid Spencera7bed60a2007-03-19 20:40:51 +0000393 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000394
395 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Reid Spencere0fc4df2006-10-20 07:07:24 +0000396 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
397 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000398 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Reid Spencera7bb3e92007-02-05 10:18:06 +0000399 "' is invalid or out of range");
Reid Spencer309080a2006-09-28 19:28:24 +0000400 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000401 } else { // This is really a signed reference. Transmogrify.
Reid Spencera7bed60a2007-03-19 20:40:51 +0000402 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000403 }
404 } else {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000405 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000406 }
407
408 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Reid Spencer309080a2006-09-28 19:28:24 +0000409 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP)) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000410 GenerateError("FP constant invalid for type");
Reid Spencer309080a2006-09-28 19:28:24 +0000411 return 0;
412 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000413 return ConstantFP::get(Ty, D.ConstPoolFP);
414
415 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer309080a2006-09-28 19:28:24 +0000416 if (!isa<PointerType>(Ty)) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000417 GenerateError("Cannot create a a non pointer null");
Reid Spencer309080a2006-09-28 19:28:24 +0000418 return 0;
419 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000420 return ConstantPointerNull::get(cast<PointerType>(Ty));
421
422 case ValID::ConstUndefVal: // Is it an undef value?
423 return UndefValue::get(Ty);
424
425 case ValID::ConstZeroVal: // Is it a zero value?
426 return Constant::getNullValue(Ty);
427
428 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer309080a2006-09-28 19:28:24 +0000429 if (D.ConstantValue->getType() != Ty) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000430 GenerateError("Constant expression type different from required type");
Reid Spencer309080a2006-09-28 19:28:24 +0000431 return 0;
432 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000433 return D.ConstantValue;
434
435 case ValID::InlineAsmVal: { // Inline asm expression
436 const PointerType *PTy = dyn_cast<PointerType>(Ty);
437 const FunctionType *FTy =
438 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer309080a2006-09-28 19:28:24 +0000439 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000440 GenerateError("Invalid type for asm constraint string");
Reid Spencer309080a2006-09-28 19:28:24 +0000441 return 0;
442 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000443 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
444 D.IAD->HasSideEffects);
445 D.destroy(); // Free InlineAsmDescriptor.
446 return IA;
447 }
448 default:
Reid Spencerac6bfc52007-02-05 17:04:00 +0000449 assert(0 && "Unhandled case!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000450 return 0;
451 } // End of switch
452
Reid Spencerac6bfc52007-02-05 17:04:00 +0000453 assert(0 && "Unhandled case!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000454 return 0;
455}
456
Reid Spencerd0e8d382007-03-19 18:40:50 +0000457// getVal - This function is identical to getExistingVal, except that if a
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000458// value is not already defined, it "improvises" by creating a placeholder var
459// that looks and acts just like the requested variable. When the value is
460// defined later, all uses of the placeholder variable are replaced with the
461// real thing.
462//
463static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer309080a2006-09-28 19:28:24 +0000464 if (Ty == Type::LabelTy) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000465 GenerateError("Cannot use a basic block here");
Reid Spencer309080a2006-09-28 19:28:24 +0000466 return 0;
467 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000468
469 // See if the value has already been defined.
Reid Spencerd0e8d382007-03-19 18:40:50 +0000470 Value *V = getExistingVal(Ty, ID);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000471 if (V) return V;
Reid Spencer309080a2006-09-28 19:28:24 +0000472 if (TriggerError) return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000473
Reid Spencer309080a2006-09-28 19:28:24 +0000474 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000475 GenerateError("Invalid use of a composite type");
Reid Spencer309080a2006-09-28 19:28:24 +0000476 return 0;
477 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000478
479 // If we reached here, we referenced either a symbol that we don't know about
480 // or an id number that hasn't been read yet. We may be referencing something
481 // forward, so just create an entry to be resolved later and get to it...
482 //
Anton Korobeynikov511d54f2007-04-28 13:48:45 +0000483 switch (ID.Type) {
484 case ValID::GlobalName:
485 case ValID::GlobalID:
486 const PointerType *PTy = dyn_cast<PointerType>(Ty);
487 if (!PTy) {
488 GenerateError("Invalid type for reference to global" );
489 return 0;
490 }
491 const Type* ElTy = PTy->getElementType();
492 if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy))
493 V = new Function(FTy, GlobalValue::ExternalLinkage);
494 else
495 V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage);
496 break;
497 default:
498 V = new Argument(Ty);
499 }
500
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000501 // Remember where this forward reference came from. FIXME, shouldn't we try
502 // to recycle these things??
503 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
504 llvmAsmlineno)));
505
506 if (inFunctionScope())
507 InsertValue(V, CurFun.LateResolveValues);
508 else
509 InsertValue(V, CurModule.LateResolveValues);
510 return V;
511}
512
Reid Spencerd0e8d382007-03-19 18:40:50 +0000513/// defineBBVal - This is a definition of a new basic block with the specified
514/// identifier which must be the same as CurFun.NextValNum, if its numeric.
515static BasicBlock *defineBBVal(const ValID &ID) {
Reid Spencerac6bfc52007-02-05 17:04:00 +0000516 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000517
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000518 BasicBlock *BB = 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000519
Reid Spencerd0e8d382007-03-19 18:40:50 +0000520 // First, see if this was forward referenced
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000521
Reid Spencerd0e8d382007-03-19 18:40:50 +0000522 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
523 if (BBI != CurFun.BBForwardRefs.end()) {
524 BB = BBI->second;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000525 // The forward declaration could have been inserted anywhere in the
526 // function: insert it into the correct place now.
527 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
528 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Reid Spencerd0e8d382007-03-19 18:40:50 +0000529
Reid Spencer6fb989c2007-03-20 01:13:36 +0000530 // We're about to erase the entry, save the key so we can clean it up.
531 ValID Tmp = BBI->first;
532
Reid Spencerd0e8d382007-03-19 18:40:50 +0000533 // Erase the forward ref from the map as its no longer "forward"
534 CurFun.BBForwardRefs.erase(ID);
535
Reid Spencer6fb989c2007-03-20 01:13:36 +0000536 // The key has been removed from the map but so we don't want to leave
537 // strdup'd memory around so destroy it too.
538 Tmp.destroy();
539
Reid Spencerd0e8d382007-03-19 18:40:50 +0000540 // If its a numbered definition, bump the number and set the BB value.
541 if (ID.Type == ValID::LocalID) {
542 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
543 InsertValue(BB);
544 }
545
546 ID.destroy();
547 return BB;
548 }
549
550 // We haven't seen this BB before and its first mention is a definition.
551 // Just create it and return it.
552 std::string Name (ID.Type == ValID::LocalName ? ID.Name : "");
553 BB = new BasicBlock(Name, CurFun.CurrentFunction);
554 if (ID.Type == ValID::LocalID) {
555 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
556 InsertValue(BB);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000557 }
Reid Spencerd0e8d382007-03-19 18:40:50 +0000558
559 ID.destroy(); // Free strdup'd memory
560 return BB;
561}
562
563/// getBBVal - get an existing BB value or create a forward reference for it.
564///
565static BasicBlock *getBBVal(const ValID &ID) {
566 assert(inFunctionScope() && "Can't get basic block at global scope!");
567
568 BasicBlock *BB = 0;
569
570 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
571 if (BBI != CurFun.BBForwardRefs.end()) {
572 BB = BBI->second;
573 } if (ID.Type == ValID::LocalName) {
574 std::string Name = ID.Name;
575 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
576 if (N)
577 if (N->getType()->getTypeID() == Type::LabelTyID)
578 BB = cast<BasicBlock>(N);
579 else
580 GenerateError("Reference to label '" + Name + "' is actually of type '"+
581 N->getType()->getDescription() + "'");
582 } else if (ID.Type == ValID::LocalID) {
583 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
584 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
585 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
586 else
587 GenerateError("Reference to label '%" + utostr(ID.Num) +
588 "' is actually of type '"+
589 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
590 }
591 } else {
592 GenerateError("Illegal label reference " + ID.getName());
593 return 0;
594 }
595
596 // If its already been defined, return it now.
597 if (BB) {
598 ID.destroy(); // Free strdup'd memory.
599 return BB;
600 }
601
602 // Otherwise, this block has not been seen before, create it.
603 std::string Name;
604 if (ID.Type == ValID::LocalName)
605 Name = ID.Name;
606 BB = new BasicBlock(Name, CurFun.CurrentFunction);
607
608 // Insert it in the forward refs map.
609 CurFun.BBForwardRefs[ID] = BB;
610
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000611 return BB;
612}
613
614
615//===----------------------------------------------------------------------===//
616// Code to handle forward references in instructions
617//===----------------------------------------------------------------------===//
618//
619// This code handles the late binding needed with statements that reference
620// values not defined yet... for example, a forward branch, or the PHI node for
621// a loop body.
622//
623// This keeps a table (CurFun.LateResolveValues) of all such forward references
624// and back patchs after we are done.
625//
626
627// ResolveDefinitions - If we could not resolve some defs at parsing
628// time (forward branches, phi functions for loops, etc...) resolve the
629// defs now...
630//
631static void
Reid Spencerd0e8d382007-03-19 18:40:50 +0000632ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000633 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencerd0e8d382007-03-19 18:40:50 +0000634 while (!LateResolvers.empty()) {
635 Value *V = LateResolvers.back();
636 LateResolvers.pop_back();
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000637
Reid Spencerd0e8d382007-03-19 18:40:50 +0000638 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
639 CurModule.PlaceHolderInfo.find(V);
640 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000641
Reid Spencerd0e8d382007-03-19 18:40:50 +0000642 ValID &DID = PHI->second.first;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000643
Reid Spencerd0e8d382007-03-19 18:40:50 +0000644 Value *TheRealValue = getExistingVal(V->getType(), DID);
645 if (TriggerError)
646 return;
647 if (TheRealValue) {
648 V->replaceAllUsesWith(TheRealValue);
649 delete V;
650 CurModule.PlaceHolderInfo.erase(PHI);
651 } else if (FutureLateResolvers) {
652 // Functions have their unresolved items forwarded to the module late
653 // resolver table
654 InsertValue(V, *FutureLateResolvers);
655 } else {
656 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
657 GenerateError("Reference to an invalid definition: '" +DID.getName()+
658 "' of type '" + V->getType()->getDescription() + "'",
659 PHI->second.second);
Reid Spencer309080a2006-09-28 19:28:24 +0000660 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000661 } else {
Reid Spencerd0e8d382007-03-19 18:40:50 +0000662 GenerateError("Reference to an invalid definition: #" +
663 itostr(DID.Num) + " of type '" +
664 V->getType()->getDescription() + "'",
665 PHI->second.second);
666 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000667 }
668 }
669 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000670 LateResolvers.clear();
671}
672
673// ResolveTypeTo - A brand new type was just declared. This means that (if
674// name is not null) things referencing Name can be resolved. Otherwise, things
675// refering to the number can be resolved. Do this now.
676//
677static void ResolveTypeTo(char *Name, const Type *ToTy) {
678 ValID D;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000679 if (Name) D = ValID::createLocalName(Name);
680 else D = ValID::createLocalID(CurModule.Types.size());
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000681
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000682 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000683 CurModule.LateResolveTypes.find(D);
684 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000685 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000686 CurModule.LateResolveTypes.erase(I);
687 }
688}
689
690// setValueName - Set the specified value to the name given. The name may be
691// null potentially, in which case this is a noop. The string passed in is
692// assumed to be a malloc'd string buffer, and is free'd by this function.
693//
694static void setValueName(Value *V, char *NameStr) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000695 if (!NameStr) return;
696 std::string Name(NameStr); // Copy string
697 free(NameStr); // Free old string
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000698
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000699 if (V->getType() == Type::VoidTy) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000700 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000701 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000702 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000703
Reid Spencerac6bfc52007-02-05 17:04:00 +0000704 assert(inFunctionScope() && "Must be in function scope!");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000705 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
706 if (ST.lookup(Name)) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000707 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +0000708 V->getType()->getDescription() + "'");
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000709 return;
710 }
711
712 // Set the name.
713 V->setName(Name);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000714}
715
716/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
717/// this is a declaration, otherwise it is a definition.
718static GlobalVariable *
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000719ParseGlobalVariable(char *NameStr,
720 GlobalValue::LinkageTypes Linkage,
721 GlobalValue::VisibilityTypes Visibility,
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000722 bool isConstantGlobal, const Type *Ty,
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000723 Constant *Initializer, bool IsThreadLocal) {
Reid Spencer309080a2006-09-28 19:28:24 +0000724 if (isa<FunctionType>(Ty)) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000725 GenerateError("Cannot declare global vars of function type");
Reid Spencer309080a2006-09-28 19:28:24 +0000726 return 0;
727 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000728
729 const PointerType *PTy = PointerType::get(Ty);
730
731 std::string Name;
732 if (NameStr) {
733 Name = NameStr; // Copy string
734 free(NameStr); // Free old string
735 }
736
737 // See if this global value was forward referenced. If so, recycle the
738 // object.
739 ValID ID;
740 if (!Name.empty()) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000741 ID = ValID::createGlobalName((char*)Name.c_str());
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000742 } else {
Reid Spencerd0e8d382007-03-19 18:40:50 +0000743 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000744 }
745
746 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
747 // Move the global to the end of the list, from whereever it was
748 // previously inserted.
749 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
750 CurModule.CurrentModule->getGlobalList().remove(GV);
751 CurModule.CurrentModule->getGlobalList().push_back(GV);
752 GV->setInitializer(Initializer);
753 GV->setLinkage(Linkage);
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000754 GV->setVisibility(Visibility);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000755 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000756 GV->setThreadLocal(IsThreadLocal);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000757 InsertValue(GV, CurModule.Values);
758 return GV;
759 }
760
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000761 // If this global has a name
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000762 if (!Name.empty()) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000763 // if the global we're parsing has an initializer (is a definition) and
764 // has external linkage.
765 if (Initializer && Linkage != GlobalValue::InternalLinkage)
766 // If there is already a global with external linkage with this name
767 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
768 // If we allow this GVar to get created, it will be renamed in the
769 // symbol table because it conflicts with an existing GVar. We can't
770 // allow redefinition of GVars whose linking indicates that their name
771 // must stay the same. Issue the error.
772 GenerateError("Redefinition of global variable named '" + Name +
773 "' of type '" + Ty->getDescription() + "'");
774 return 0;
775 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000776 }
777
778 // Otherwise there is no existing GV to use, create one now.
779 GlobalVariable *GV =
780 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000781 CurModule.CurrentModule, IsThreadLocal);
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000782 GV->setVisibility(Visibility);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000783 InsertValue(GV, CurModule.Values);
784 return GV;
785}
786
787// setTypeName - Set the specified type to the name given. The name may be
788// null potentially, in which case this is a noop. The string passed in is
789// assumed to be a malloc'd string buffer, and is freed by this function.
790//
791// This function returns true if the type has already been defined, but is
792// allowed to be redefined in the specified context. If the name is a new name
793// for the type plane, it is inserted and false is returned.
794static bool setTypeName(const Type *T, char *NameStr) {
Reid Spencerac6bfc52007-02-05 17:04:00 +0000795 assert(!inFunctionScope() && "Can't give types function-local names!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000796 if (NameStr == 0) return false;
797
798 std::string Name(NameStr); // Copy string
799 free(NameStr); // Free old string
800
801 // We don't allow assigning names to void type
Reid Spencer309080a2006-09-28 19:28:24 +0000802 if (T == Type::VoidTy) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000803 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer309080a2006-09-28 19:28:24 +0000804 return false;
805 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000806
807 // Set the type name, checking for conflicts as we do so.
808 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
809
810 if (AlreadyExists) { // Inserting a name that is already defined???
811 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencerac6bfc52007-02-05 17:04:00 +0000812 assert(Existing && "Conflict but no matching type?!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000813
814 // There is only one case where this is allowed: when we are refining an
815 // opaque type. In this case, Existing will be an opaque type.
816 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
817 // We ARE replacing an opaque type!
818 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
819 return true;
820 }
821
822 // Otherwise, this is an attempt to redefine a type. That's okay if
823 // the redefinition is identical to the original. This will be so if
824 // Existing and T point to the same Type object. In this one case we
825 // allow the equivalent redefinition.
826 if (Existing == T) return true; // Yes, it's equal.
827
828 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer33259082007-01-05 21:51:07 +0000829 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +0000830 T->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000831 }
832
833 return false;
834}
835
836//===----------------------------------------------------------------------===//
837// Code for handling upreferences in type names...
838//
839
840// TypeContains - Returns true if Ty directly contains E in it.
841//
842static bool TypeContains(const Type *Ty, const Type *E) {
843 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
844 E) != Ty->subtype_end();
845}
846
847namespace {
848 struct UpRefRecord {
849 // NestingLevel - The number of nesting levels that need to be popped before
850 // this type is resolved.
851 unsigned NestingLevel;
852
853 // LastContainedTy - This is the type at the current binding level for the
854 // type. Every time we reduce the nesting level, this gets updated.
855 const Type *LastContainedTy;
856
857 // UpRefTy - This is the actual opaque type that the upreference is
858 // represented with.
859 OpaqueType *UpRefTy;
860
861 UpRefRecord(unsigned NL, OpaqueType *URTy)
862 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
863 };
864}
865
866// UpRefs - A list of the outstanding upreferences that need to be resolved.
867static std::vector<UpRefRecord> UpRefs;
868
869/// HandleUpRefs - Every time we finish a new layer of types, this function is
870/// called. It loops through the UpRefs vector, which is a list of the
871/// currently active types. For each type, if the up reference is contained in
872/// the newly completed type, we decrement the level count. When the level
873/// count reaches zero, the upreferenced type is the type that is passed in:
874/// thus we can complete the cycle.
875///
876static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner680aab62006-08-18 17:34:45 +0000877 // If Ty isn't abstract, or if there are no up-references in it, then there is
878 // nothing to resolve here.
879 if (!ty->isAbstract() || UpRefs.empty()) return ty;
880
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000881 PATypeHolder Ty(ty);
882 UR_OUT("Type '" << Ty->getDescription() <<
883 "' newly formed. Resolving upreferences.\n" <<
884 UpRefs.size() << " upreferences active!\n");
885
886 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
887 // to zero), we resolve them all together before we resolve them to Ty. At
888 // the end of the loop, if there is anything to resolve to Ty, it will be in
889 // this variable.
890 OpaqueType *TypeToResolve = 0;
891
892 for (unsigned i = 0; i != UpRefs.size(); ++i) {
893 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
894 << UpRefs[i].second->getDescription() << ") = "
895 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
896 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
897 // Decrement level of upreference
898 unsigned Level = --UpRefs[i].NestingLevel;
899 UpRefs[i].LastContainedTy = Ty;
900 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
901 if (Level == 0) { // Upreference should be resolved!
902 if (!TypeToResolve) {
903 TypeToResolve = UpRefs[i].UpRefTy;
904 } else {
905 UR_OUT(" * Resolving upreference for "
906 << UpRefs[i].second->getDescription() << "\n";
907 std::string OldName = UpRefs[i].UpRefTy->getDescription());
908 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
909 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
910 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
911 }
912 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
913 --i; // Do not skip the next element...
914 }
915 }
916 }
917
918 if (TypeToResolve) {
919 UR_OUT(" * Resolving upreference for "
920 << UpRefs[i].second->getDescription() << "\n";
921 std::string OldName = TypeToResolve->getDescription());
922 TypeToResolve->refineAbstractTypeTo(Ty);
923 }
924
925 return Ty;
926}
927
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000928//===----------------------------------------------------------------------===//
929// RunVMAsmParser - Define an interface to this parser
930//===----------------------------------------------------------------------===//
931//
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000932static Module* RunParser(Module * M);
933
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000934Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
935 set_scan_file(F);
936
937 CurFilename = Filename;
938 return RunParser(new Module(CurFilename));
939}
940
941Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
942 set_scan_string(AsmString);
943
944 CurFilename = "from_memory";
945 if (M == NULL) {
946 return RunParser(new Module (CurFilename));
947 } else {
948 return RunParser(M);
949 }
950}
951
952%}
953
954%union {
955 llvm::Module *ModuleVal;
956 llvm::Function *FunctionVal;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000957 llvm::BasicBlock *BasicBlockVal;
958 llvm::TerminatorInst *TermInstVal;
959 llvm::Instruction *InstVal;
Reid Spencere2c32da2006-12-03 05:46:11 +0000960 llvm::Constant *ConstVal;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000961
Reid Spencere2c32da2006-12-03 05:46:11 +0000962 const llvm::Type *PrimType;
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000963 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencere2c32da2006-12-03 05:46:11 +0000964 llvm::PATypeHolder *TypeVal;
965 llvm::Value *ValueVal;
Reid Spencere2c32da2006-12-03 05:46:11 +0000966 std::vector<llvm::Value*> *ValueList;
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000967 llvm::ArgListType *ArgList;
968 llvm::TypeWithAttrs TypeWithAttrs;
969 llvm::TypeWithAttrsList *TypeWithAttrsList;
970 llvm::ValueRefList *ValueRefList;
971
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000972 // Represent the RHS of PHI node
Reid Spencere2c32da2006-12-03 05:46:11 +0000973 std::list<std::pair<llvm::Value*,
974 llvm::BasicBlock*> > *PHIList;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000975 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencere2c32da2006-12-03 05:46:11 +0000976 std::vector<llvm::Constant*> *ConstVector;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000977
978 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000979 llvm::GlobalValue::VisibilityTypes Visibility;
Reid Spencerf51a7052007-04-09 06:16:21 +0000980 uint16_t ParamAttrs;
Reid Spencerc7a686b2007-02-28 02:24:54 +0000981 llvm::APInt *APIntVal;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000982 int64_t SInt64Val;
983 uint64_t UInt64Val;
984 int SIntVal;
985 unsigned UIntVal;
986 double FPVal;
987 bool BoolVal;
988
989 char *StrVal; // This memory is strdup'd!
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000990 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000991
Reid Spencere2c32da2006-12-03 05:46:11 +0000992 llvm::Instruction::BinaryOps BinaryOpVal;
993 llvm::Instruction::TermOps TermOpVal;
994 llvm::Instruction::MemoryOps MemOpVal;
995 llvm::Instruction::CastOps CastOpVal;
996 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencere2c32da2006-12-03 05:46:11 +0000997 llvm::ICmpInst::Predicate IPredicate;
998 llvm::FCmpInst::Predicate FPredicate;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000999}
1000
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001001%type <ModuleVal> Module
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001002%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
1003%type <BasicBlockVal> BasicBlock InstructionList
1004%type <TermInstVal> BBTerminatorInst
1005%type <InstVal> Inst InstVal MemoryInst
Anton Korobeynikov511d54f2007-04-28 13:48:45 +00001006%type <ConstVal> ConstVal ConstExpr AliaseeRef
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001007%type <ConstVector> ConstVector
1008%type <ArgList> ArgList ArgListH
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001009%type <PHIList> PHIList
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001010%type <ValueRefList> ValueRefList // For call param lists & GEP indices
1011%type <ValueList> IndexList // For GEP indices
1012%type <TypeList> TypeListI
1013%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001014%type <TypeWithAttrs> ArgType
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001015%type <JumpTable> JumpTable
1016%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00001017%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001018%type <BoolVal> OptVolatile // 'volatile' or not
1019%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1020%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001021%type <Linkage> GVInternalLinkage GVExternalLinkage
1022%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00001023%type <Linkage> AliasLinkage
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001024%type <Visibility> GVVisibilityStyle
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001025
1026// ValueRef - Unresolved reference to a definition or BB
1027%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1028%type <ValueVal> ResolvedVal // <type> <valref> pair
1029// Tokens and types for handling constant integer values
1030//
1031// ESINT64VAL - A negative number within long long range
1032%token <SInt64Val> ESINT64VAL
1033
1034// EUINT64VAL - A positive number within uns. long long range
1035%token <UInt64Val> EUINT64VAL
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001036
Reid Spencerc7a686b2007-02-28 02:24:54 +00001037// ESAPINTVAL - A negative number with arbitrary precision
1038%token <APIntVal> ESAPINTVAL
1039
1040// EUAPINTVAL - A positive number with arbitrary precision
1041%token <APIntVal> EUAPINTVAL
1042
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001043%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001044%token <FPVal> FPVAL // Float or Double constant
1045
1046// Built in types...
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001047%type <TypeVal> Types ResultTypes
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001048%type <PrimType> IntType FPType PrimType // Classifications
Reid Spencer58a8db02007-01-13 05:00:46 +00001049%token <PrimType> VOID INTTYPE
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001050%token <PrimType> FLOAT DOUBLE LABEL
1051%token TYPE
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001052
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001053%token<StrVal> LOCALVAR GLOBALVAR LABELSTR STRINGCONSTANT ATSTRINGCONSTANT
1054%type <StrVal> LocalName OptLocalName OptLocalAssign
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00001055%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001056%type <UIntVal> OptAlign OptCAlign
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001057%type <StrVal> OptSection SectionString
1058
Reid Spencer7ae03fc2007-04-09 01:56:05 +00001059%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00001060%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001061%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001062%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001063%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001064%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001065%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattner7d1d0342006-10-22 06:08:13 +00001066%token DATALAYOUT
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001067%type <UIntVal> OptCallingConv
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001068%type <ParamAttrs> OptParamAttrs ParamAttr
1069%type <ParamAttrs> OptFuncAttrs FuncAttr
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001070
1071// Basic Block Terminating Operators
1072%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
1073
1074// Binary Operators
Reid Spencer266e42b2006-12-23 06:05:41 +00001075%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencerde46e482006-11-02 20:25:50 +00001076%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer2341c222007-02-02 02:16:23 +00001077%token <BinaryOpVal> SHL LSHR ASHR
1078
Reid Spencere2c32da2006-12-03 05:46:11 +00001079%token <OtherOpVal> ICMP FCMP
Reid Spencere2c32da2006-12-03 05:46:11 +00001080%type <IPredicate> IPredicates
Reid Spencere2c32da2006-12-03 05:46:11 +00001081%type <FPredicate> FPredicates
Reid Spencer1960ef32006-12-03 06:59:29 +00001082%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1083%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001084
1085// Memory Instructions
1086%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1087
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001088// Cast Operators
1089%type <CastOpVal> CastOps
1090%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1091%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1092
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001093// Other Operators
Reid Spencer2341c222007-02-02 02:16:23 +00001094%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattner9ff96a72006-04-08 01:18:56 +00001095%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001096
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001097// Function Attributes
Reid Spencer32096782007-03-22 02:14:08 +00001098%token NORETURN INREG SRET NOUNWIND
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001099
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001100// Visibility Styles
1101%token DEFAULT HIDDEN
1102
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001103%start Module
1104%%
1105
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001106
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001107// Operations that are notably excluded from this list include:
1108// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1109//
Reid Spencerde46e482006-11-02 20:25:50 +00001110ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer2341c222007-02-02 02:16:23 +00001111LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001112CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1113 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer2341c222007-02-02 02:16:23 +00001114
Reid Spencer1960ef32006-12-03 06:59:29 +00001115IPredicates
Reid Spencerd2e0c342006-12-04 05:24:24 +00001116 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer1960ef32006-12-03 06:59:29 +00001117 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1118 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1119 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1120 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1121 ;
1122
1123FPredicates
1124 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1125 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1126 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1127 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1128 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1129 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1130 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1131 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1132 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1133 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001134
1135// These are some types that allow classification if we only want a particular
1136// thing... for example, only a signed, unsigned, or integral type.
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001137IntType : INTTYPE;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001138FPType : FLOAT | DOUBLE;
1139
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001140LocalName : LOCALVAR | STRINGCONSTANT;
1141OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1142
1143/// OptLocalAssign - Value producing statements have an optional assignment
1144/// component.
1145OptLocalAssign : LocalName '=' {
1146 $$ = $1;
1147 CHECK_FOR_ERROR
1148 }
1149 | /*empty*/ {
1150 $$ = 0;
1151 CHECK_FOR_ERROR
1152 };
1153
1154GlobalName : GLOBALVAR | ATSTRINGCONSTANT;
1155
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00001156OptGlobalAssign : GlobalAssign
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001157 | /*empty*/ {
1158 $$ = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00001159 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001160 };
1161
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00001162GlobalAssign : GlobalName '=' {
1163 $$ = $1;
1164 CHECK_FOR_ERROR
Chris Lattner720367c2007-04-26 05:31:05 +00001165 };
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00001166
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001167GVInternalLinkage
1168 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1169 | WEAK { $$ = GlobalValue::WeakLinkage; }
1170 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1171 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1172 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1173 ;
1174
1175GVExternalLinkage
1176 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1177 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1178 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1179 ;
1180
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001181GVVisibilityStyle
1182 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00001183 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001184 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1185 ;
1186
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001187FunctionDeclareLinkage
1188 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1189 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1190 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001191 ;
1192
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00001193FunctionDefineLinkage
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001194 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1195 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001196 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1197 | WEAK { $$ = GlobalValue::WeakLinkage; }
1198 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001199 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001200
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00001201AliasLinkage
1202 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1203 | WEAK { $$ = GlobalValue::WeakLinkage; }
1204 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1205 ;
1206
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +00001207OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1208 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +00001209 FASTCC_TOK { $$ = CallingConv::Fast; } |
1210 COLDCC_TOK { $$ = CallingConv::Cold; } |
1211 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1212 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1213 CC_TOK EUINT64VAL {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001214 if ((unsigned)$2 != $2)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001215 GEN_ERROR("Calling conv too large");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001216 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001217 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001218 };
1219
Reid Spencera472f662007-04-11 02:44:20 +00001220ParamAttr : ZEXT { $$ = ParamAttr::ZExt; }
1221 | SEXT { $$ = ParamAttr::SExt; }
1222 | INREG { $$ = ParamAttr::InReg; }
1223 | SRET { $$ = ParamAttr::StructRet; }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001224 ;
1225
Reid Spencera472f662007-04-11 02:44:20 +00001226OptParamAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001227 | OptParamAttrs ParamAttr {
Reid Spencerf51a7052007-04-09 06:16:21 +00001228 $$ = $1 | $2;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001229 }
1230 ;
1231
Reid Spencera472f662007-04-11 02:44:20 +00001232FuncAttr : NORETURN { $$ = ParamAttr::NoReturn; }
1233 | NOUNWIND { $$ = ParamAttr::NoUnwind; }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001234 | ParamAttr
1235 ;
1236
Reid Spencera472f662007-04-11 02:44:20 +00001237OptFuncAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001238 | OptFuncAttrs FuncAttr {
Reid Spencerf51a7052007-04-09 06:16:21 +00001239 $$ = $1 | $2;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001240 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001241 ;
1242
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001243// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1244// a comma before it.
1245OptAlign : /*empty*/ { $$ = 0; } |
1246 ALIGN EUINT64VAL {
1247 $$ = $2;
1248 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001249 GEN_ERROR("Alignment must be a power of two");
Reid Spencer713eedc2006-08-18 08:43:06 +00001250 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001251};
1252OptCAlign : /*empty*/ { $$ = 0; } |
1253 ',' ALIGN EUINT64VAL {
1254 $$ = $3;
1255 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001256 GEN_ERROR("Alignment must be a power of two");
Reid Spencer713eedc2006-08-18 08:43:06 +00001257 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001258};
1259
1260
1261SectionString : SECTION STRINGCONSTANT {
1262 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1263 if ($2[i] == '"' || $2[i] == '\\')
Reid Spencera7bb3e92007-02-05 10:18:06 +00001264 GEN_ERROR("Invalid character in section name");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001265 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001266 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001267};
1268
1269OptSection : /*empty*/ { $$ = 0; } |
1270 SectionString { $$ = $1; };
1271
1272// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1273// is set to be the global we are processing.
1274//
1275GlobalVarAttributes : /* empty */ {} |
1276 ',' GlobalVarAttribute GlobalVarAttributes {};
1277GlobalVarAttribute : SectionString {
1278 CurGV->setSection($1);
1279 free($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001280 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001281 }
1282 | ALIGN EUINT64VAL {
1283 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001284 GEN_ERROR("Alignment must be a power of two");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001285 CurGV->setAlignment($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001286 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001287 };
1288
1289//===----------------------------------------------------------------------===//
1290// Types includes all predefined types... except void, because it can only be
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001291// used in specific contexts (function returning void for example).
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001292
1293// Derived types are added later...
1294//
Reid Spencer58a8db02007-01-13 05:00:46 +00001295PrimType : INTTYPE | FLOAT | DOUBLE | LABEL ;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001296
1297Types
1298 : OPAQUE {
Reid Spencere2c32da2006-12-03 05:46:11 +00001299 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer713eedc2006-08-18 08:43:06 +00001300 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001301 }
1302 | PrimType {
Reid Spencere2c32da2006-12-03 05:46:11 +00001303 $$ = new PATypeHolder($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001304 CHECK_FOR_ERROR
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001305 }
1306 | Types '*' { // Pointer type?
1307 if (*$1 == Type::LabelTy)
1308 GEN_ERROR("Cannot form a pointer to a basic block");
1309 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1310 delete $1;
1311 CHECK_FOR_ERROR
1312 }
1313 | SymbolicValueRef { // Named types are also simple types...
1314 const Type* tmp = getTypeVal($1);
1315 CHECK_FOR_ERROR
1316 $$ = new PATypeHolder(tmp);
1317 }
1318 | '\\' EUINT64VAL { // Type UpReference
Reid Spencera7bb3e92007-02-05 10:18:06 +00001319 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001320 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1321 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencere2c32da2006-12-03 05:46:11 +00001322 $$ = new PATypeHolder(OT);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001323 UR_OUT("New Upreference!\n");
Reid Spencer713eedc2006-08-18 08:43:06 +00001324 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001325 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001326 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001327 std::vector<const Type*> Params;
Christopher Lamb3f706f22007-04-22 20:09:11 +00001328 ParamAttrsVector Attrs;
1329 if ($5 != ParamAttr::None) {
1330 ParamAttrsWithIndex X; X.index = 0; X.attrs = $5;
1331 Attrs.push_back(X);
1332 }
Reid Spencerf51a7052007-04-09 06:16:21 +00001333 unsigned index = 1;
1334 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
1335 for (; I != E; ++I, ++index) {
Reid Spencer6fb989c2007-03-20 01:13:36 +00001336 const Type *Ty = I->Ty->get();
Reid Spencer6fb989c2007-03-20 01:13:36 +00001337 Params.push_back(Ty);
1338 if (Ty != Type::VoidTy)
Christopher Lamb3f706f22007-04-22 20:09:11 +00001339 if (I->Attrs != ParamAttr::None) {
1340 ParamAttrsWithIndex X; X.index = index; X.attrs = I->Attrs;
1341 Attrs.push_back(X);
1342 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001343 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001344 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1345 if (isVarArg) Params.pop_back();
1346
Reid Spencerf51a7052007-04-09 06:16:21 +00001347 ParamAttrsList *ActualAttrs = 0;
1348 if (!Attrs.empty())
Christopher Lamb3f706f22007-04-22 20:09:11 +00001349 ActualAttrs = ParamAttrsList::get(Attrs);
Reid Spencerf51a7052007-04-09 06:16:21 +00001350 FunctionType *FT = FunctionType::get(*$1, Params, isVarArg, ActualAttrs);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001351 delete $3; // Delete the argument list
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001352 delete $1; // Delete the return type handle
1353 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer713eedc2006-08-18 08:43:06 +00001354 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001355 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001356 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001357 std::vector<const Type*> Params;
Christopher Lamb3f706f22007-04-22 20:09:11 +00001358 ParamAttrsVector Attrs;
1359 if ($5 != ParamAttr::None) {
1360 ParamAttrsWithIndex X; X.index = 0; X.attrs = $5;
1361 Attrs.push_back(X);
1362 }
Reid Spencerf51a7052007-04-09 06:16:21 +00001363 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
1364 unsigned index = 1;
1365 for ( ; I != E; ++I, ++index) {
Reid Spencer6fb989c2007-03-20 01:13:36 +00001366 const Type* Ty = I->Ty->get();
Reid Spencer6fb989c2007-03-20 01:13:36 +00001367 Params.push_back(Ty);
1368 if (Ty != Type::VoidTy)
Christopher Lamb3f706f22007-04-22 20:09:11 +00001369 if (I->Attrs != ParamAttr::None) {
1370 ParamAttrsWithIndex X; X.index = index; X.attrs = I->Attrs;
1371 Attrs.push_back(X);
1372 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001373 }
1374 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1375 if (isVarArg) Params.pop_back();
1376
Reid Spencerf51a7052007-04-09 06:16:21 +00001377 ParamAttrsList *ActualAttrs = 0;
1378 if (!Attrs.empty())
Christopher Lamb3f706f22007-04-22 20:09:11 +00001379 ActualAttrs = ParamAttrsList::get(Attrs);
Reid Spencerf51a7052007-04-09 06:16:21 +00001380
1381 FunctionType *FT = FunctionType::get($1, Params, isVarArg, ActualAttrs);
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001382 delete $3; // Delete the argument list
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001383 $$ = new PATypeHolder(HandleUpRefs(FT));
1384 CHECK_FOR_ERROR
1385 }
1386
1387 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Reid Spencere2c32da2006-12-03 05:46:11 +00001388 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1389 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00001390 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001391 }
Chris Lattner4669b0b2007-02-19 07:44:24 +00001392 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencere2c32da2006-12-03 05:46:11 +00001393 const llvm::Type* ElemTy = $4->get();
1394 if ((unsigned)$2 != $2)
1395 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner03c49532007-01-15 02:27:26 +00001396 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencerd84d35b2007-02-15 02:26:10 +00001397 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencere2c32da2006-12-03 05:46:11 +00001398 if (!isPowerOf2_32($2))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001399 GEN_ERROR("Vector length should be a power of 2");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001400 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencere2c32da2006-12-03 05:46:11 +00001401 delete $4;
1402 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001403 }
1404 | '{' TypeListI '}' { // Structure type?
1405 std::vector<const Type*> Elements;
Reid Spencere2c32da2006-12-03 05:46:11 +00001406 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001407 E = $2->end(); I != E; ++I)
Reid Spencere2c32da2006-12-03 05:46:11 +00001408 Elements.push_back(*I);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001409
Reid Spencere2c32da2006-12-03 05:46:11 +00001410 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001411 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001412 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001413 }
1414 | '{' '}' { // Empty structure type?
Reid Spencere2c32da2006-12-03 05:46:11 +00001415 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer713eedc2006-08-18 08:43:06 +00001416 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001417 }
Andrew Lenharth2d189ff2006-12-08 18:07:09 +00001418 | '<' '{' TypeListI '}' '>' {
1419 std::vector<const Type*> Elements;
1420 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1421 E = $3->end(); I != E; ++I)
1422 Elements.push_back(*I);
1423
1424 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1425 delete $3;
1426 CHECK_FOR_ERROR
1427 }
1428 | '<' '{' '}' '>' { // Empty structure type?
1429 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1430 CHECK_FOR_ERROR
1431 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001432 ;
1433
1434ArgType
1435 : Types OptParamAttrs {
1436 $$.Ty = $1;
1437 $$.Attrs = $2;
1438 }
1439 ;
1440
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001441ResultTypes
1442 : Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001443 if (!UpRefs.empty())
1444 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1445 if (!(*$1)->isFirstClassType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001446 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001447 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001448 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001449 | VOID {
1450 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001451 }
1452 ;
1453
1454ArgTypeList : ArgType {
1455 $$ = new TypeWithAttrsList();
1456 $$->push_back($1);
1457 CHECK_FOR_ERROR
1458 }
1459 | ArgTypeList ',' ArgType {
1460 ($$=$1)->push_back($3);
1461 CHECK_FOR_ERROR
1462 }
1463 ;
1464
1465ArgTypeListI
1466 : ArgTypeList
1467 | ArgTypeList ',' DOTDOTDOT {
1468 $$=$1;
Reid Spencera472f662007-04-11 02:44:20 +00001469 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001470 TWA.Ty = new PATypeHolder(Type::VoidTy);
1471 $$->push_back(TWA);
1472 CHECK_FOR_ERROR
1473 }
1474 | DOTDOTDOT {
1475 $$ = new TypeWithAttrsList;
Reid Spencera472f662007-04-11 02:44:20 +00001476 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001477 TWA.Ty = new PATypeHolder(Type::VoidTy);
1478 $$->push_back(TWA);
1479 CHECK_FOR_ERROR
1480 }
1481 | /*empty*/ {
1482 $$ = new TypeWithAttrsList();
Reid Spencer713eedc2006-08-18 08:43:06 +00001483 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001484 };
1485
1486// TypeList - Used for struct declarations and as a basis for function type
1487// declaration type lists
1488//
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001489TypeListI : Types {
Reid Spencere2c32da2006-12-03 05:46:11 +00001490 $$ = new std::list<PATypeHolder>();
Reid Spencer6fb989c2007-03-20 01:13:36 +00001491 $$->push_back(*$1);
1492 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001493 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001494 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001495 | TypeListI ',' Types {
Reid Spencer6fb989c2007-03-20 01:13:36 +00001496 ($$=$1)->push_back(*$3);
1497 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001498 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001499 };
1500
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001501// ConstVal - The various declarations that go into the constant pool. This
1502// production is used ONLY to represent constants that show up AFTER a 'const',
1503// 'constant' or 'global' token at global scope. Constants that can be inlined
1504// into other expressions (such as integers and constexprs) are handled by the
1505// ResolvedVal, ValueRef and ConstValueRef productions.
1506//
1507ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001508 if (!UpRefs.empty())
1509 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001510 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001511 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001512 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001513 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001514 const Type *ETy = ATy->getElementType();
1515 int NumElements = ATy->getNumElements();
1516
1517 // Verify that we have the correct size...
1518 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer713eedc2006-08-18 08:43:06 +00001519 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001520 utostr($3->size()) + " arguments, but has size of " +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001521 itostr(NumElements) + "");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001522
1523 // Verify all elements are correct type!
1524 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencere2c32da2006-12-03 05:46:11 +00001525 if (ETy != (*$3)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001526 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001527 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencere2c32da2006-12-03 05:46:11 +00001528 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001529 }
1530
Reid Spencere2c32da2006-12-03 05:46:11 +00001531 $$ = ConstantArray::get(ATy, *$3);
1532 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001533 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001534 }
1535 | Types '[' ']' {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001536 if (!UpRefs.empty())
1537 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001538 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001539 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001540 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001541 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001542
1543 int NumElements = ATy->getNumElements();
1544 if (NumElements != -1 && NumElements != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001545 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencera7bb3e92007-02-05 10:18:06 +00001546 " arguments, but has size of " + itostr(NumElements) +"");
Reid Spencere2c32da2006-12-03 05:46:11 +00001547 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1548 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001549 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001550 }
1551 | Types 'c' STRINGCONSTANT {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001552 if (!UpRefs.empty())
1553 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001554 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001555 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001556 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001557 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001558
1559 int NumElements = ATy->getNumElements();
1560 const Type *ETy = ATy->getElementType();
1561 char *EndStr = UnEscapeLexed($3, true);
1562 if (NumElements != -1 && NumElements != (EndStr-$3))
Reid Spencer713eedc2006-08-18 08:43:06 +00001563 GEN_ERROR("Can't build string constant of size " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001564 itostr((int)(EndStr-$3)) +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001565 " when array has size " + itostr(NumElements) + "");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001566 std::vector<Constant*> Vals;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001567 if (ETy == Type::Int8Ty) {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001568 for (unsigned char *C = (unsigned char *)$3;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001569 C != (unsigned char*)EndStr; ++C)
1570 Vals.push_back(ConstantInt::get(ETy, *C));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001571 } else {
1572 free($3);
Reid Spencera7bb3e92007-02-05 10:18:06 +00001573 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001574 }
1575 free($3);
Reid Spencere2c32da2006-12-03 05:46:11 +00001576 $$ = ConstantArray::get(ATy, Vals);
1577 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001578 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001579 }
1580 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001581 if (!UpRefs.empty())
1582 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001583 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001584 if (PTy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001585 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001586 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001587 const Type *ETy = PTy->getElementType();
1588 int NumElements = PTy->getNumElements();
1589
1590 // Verify that we have the correct size...
1591 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer713eedc2006-08-18 08:43:06 +00001592 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001593 utostr($3->size()) + " arguments, but has size of " +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001594 itostr(NumElements) + "");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001595
1596 // Verify all elements are correct type!
1597 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencere2c32da2006-12-03 05:46:11 +00001598 if (ETy != (*$3)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001599 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001600 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencere2c32da2006-12-03 05:46:11 +00001601 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001602 }
1603
Reid Spencerd84d35b2007-02-15 02:26:10 +00001604 $$ = ConstantVector::get(PTy, *$3);
Reid Spencere2c32da2006-12-03 05:46:11 +00001605 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001606 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001607 }
1608 | Types '{' ConstVector '}' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001609 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001610 if (STy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001611 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001612 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001613
1614 if ($3->size() != STy->getNumContainedTypes())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001615 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001616
1617 // Check to ensure that constants are compatible with the type initializer!
1618 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencere2c32da2006-12-03 05:46:11 +00001619 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer713eedc2006-08-18 08:43:06 +00001620 GEN_ERROR("Expected type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001621 STy->getElementType(i)->getDescription() +
1622 "' for element #" + utostr(i) +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001623 " of structure initializer");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001624
Zhou Sheng75b871f2007-01-11 12:24:14 +00001625 // Check to ensure that Type is not packed
1626 if (STy->isPacked())
Chris Lattner720367c2007-04-26 05:31:05 +00001627 GEN_ERROR("Unpacked Initializer to vector type '" +
1628 STy->getDescription() + "'");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001629
Reid Spencere2c32da2006-12-03 05:46:11 +00001630 $$ = ConstantStruct::get(STy, *$3);
1631 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001632 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001633 }
1634 | Types '{' '}' {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001635 if (!UpRefs.empty())
1636 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001637 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001638 if (STy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001639 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001640 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001641
1642 if (STy->getNumContainedTypes() != 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001643 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001644
Zhou Sheng75b871f2007-01-11 12:24:14 +00001645 // Check to ensure that Type is not packed
1646 if (STy->isPacked())
Chris Lattner720367c2007-04-26 05:31:05 +00001647 GEN_ERROR("Unpacked Initializer to vector type '" +
1648 STy->getDescription() + "'");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001649
1650 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1651 delete $1;
1652 CHECK_FOR_ERROR
1653 }
1654 | Types '<' '{' ConstVector '}' '>' {
1655 const StructType *STy = dyn_cast<StructType>($1->get());
1656 if (STy == 0)
1657 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001658 (*$1)->getDescription() + "'");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001659
1660 if ($4->size() != STy->getNumContainedTypes())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001661 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001662
1663 // Check to ensure that constants are compatible with the type initializer!
1664 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1665 if ((*$4)[i]->getType() != STy->getElementType(i))
1666 GEN_ERROR("Expected type '" +
1667 STy->getElementType(i)->getDescription() +
1668 "' for element #" + utostr(i) +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001669 " of structure initializer");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001670
1671 // Check to ensure that Type is packed
1672 if (!STy->isPacked())
Chris Lattner4669b0b2007-02-19 07:44:24 +00001673 GEN_ERROR("Vector initializer to non-vector type '" +
1674 STy->getDescription() + "'");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001675
1676 $$ = ConstantStruct::get(STy, *$4);
1677 delete $1; delete $4;
1678 CHECK_FOR_ERROR
1679 }
1680 | Types '<' '{' '}' '>' {
1681 if (!UpRefs.empty())
1682 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1683 const StructType *STy = dyn_cast<StructType>($1->get());
1684 if (STy == 0)
1685 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001686 (*$1)->getDescription() + "'");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001687
1688 if (STy->getNumContainedTypes() != 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001689 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001690
1691 // Check to ensure that Type is packed
1692 if (!STy->isPacked())
Chris Lattner4669b0b2007-02-19 07:44:24 +00001693 GEN_ERROR("Vector initializer to non-vector type '" +
1694 STy->getDescription() + "'");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001695
Reid Spencere2c32da2006-12-03 05:46:11 +00001696 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1697 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001698 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001699 }
1700 | Types NULL_TOK {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001701 if (!UpRefs.empty())
1702 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001703 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001704 if (PTy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001705 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001706 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001707
Reid Spencere2c32da2006-12-03 05:46:11 +00001708 $$ = ConstantPointerNull::get(PTy);
1709 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001710 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001711 }
1712 | Types UNDEF {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001713 if (!UpRefs.empty())
1714 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001715 $$ = UndefValue::get($1->get());
1716 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001717 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001718 }
1719 | Types SymbolicValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001720 if (!UpRefs.empty())
1721 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001722 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001723 if (Ty == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001724 GEN_ERROR("Global const reference must be a pointer type");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001725
1726 // ConstExprs can exist in the body of a function, thus creating
1727 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencerd0e8d382007-03-19 18:40:50 +00001728 // the context of a function, getExistingVal will search the functions
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001729 // symbol table instead of the module symbol table for the global symbol,
1730 // which throws things all off. To get around this, we just tell
Reid Spencerd0e8d382007-03-19 18:40:50 +00001731 // getExistingVal that we are at global scope here.
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001732 //
1733 Function *SavedCurFn = CurFun.CurrentFunction;
1734 CurFun.CurrentFunction = 0;
1735
Reid Spencerd0e8d382007-03-19 18:40:50 +00001736 Value *V = getExistingVal(Ty, $2);
Reid Spencer309080a2006-09-28 19:28:24 +00001737 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001738
1739 CurFun.CurrentFunction = SavedCurFn;
1740
1741 // If this is an initializer for a constant pointer, which is referencing a
1742 // (currently) undefined variable, create a stub now that shall be replaced
1743 // in the future with the right type of variable.
1744 //
1745 if (V == 0) {
Reid Spencerac6bfc52007-02-05 17:04:00 +00001746 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001747 const PointerType *PT = cast<PointerType>(Ty);
1748
1749 // First check to see if the forward references value is already created!
1750 PerModuleInfo::GlobalRefsType::iterator I =
1751 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1752
1753 if (I != CurModule.GlobalRefs.end()) {
1754 V = I->second; // Placeholder already exists, use it...
1755 $2.destroy();
1756 } else {
1757 std::string Name;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001758 if ($2.Type == ValID::GlobalName)
1759 Name = $2.Name;
1760 else if ($2.Type != ValID::GlobalID)
1761 GEN_ERROR("Invalid reference to global");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001762
1763 // Create the forward referenced global.
1764 GlobalValue *GV;
1765 if (const FunctionType *FTy =
1766 dyn_cast<FunctionType>(PT->getElementType())) {
Chris Lattner720367c2007-04-26 05:31:05 +00001767 GV = new Function(FTy, GlobalValue::ExternalWeakLinkage, Name,
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001768 CurModule.CurrentModule);
1769 } else {
1770 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattner720367c2007-04-26 05:31:05 +00001771 GlobalValue::ExternalWeakLinkage, 0,
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001772 Name, CurModule.CurrentModule);
1773 }
1774
1775 // Keep track of the fact that we have a forward ref to recycle it
1776 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1777 V = GV;
1778 }
1779 }
1780
Reid Spencere2c32da2006-12-03 05:46:11 +00001781 $$ = cast<GlobalValue>(V);
1782 delete $1; // Free the type handle
Reid Spencer713eedc2006-08-18 08:43:06 +00001783 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001784 }
1785 | Types ConstExpr {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001786 if (!UpRefs.empty())
1787 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001788 if ($1->get() != $2->getType())
Reid Spencerca3d1cb2007-01-04 00:06:14 +00001789 GEN_ERROR("Mismatched types for constant expression: " +
1790 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001791 $$ = $2;
Reid Spencere2c32da2006-12-03 05:46:11 +00001792 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001793 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001794 }
1795 | Types ZEROINITIALIZER {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001796 if (!UpRefs.empty())
1797 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001798 const Type *Ty = $1->get();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001799 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001800 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencere2c32da2006-12-03 05:46:11 +00001801 $$ = Constant::getNullValue(Ty);
1802 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001803 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00001804 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001805 | IntType ESINT64VAL { // integral constants
Reid Spencer266e42b2006-12-23 06:05:41 +00001806 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001807 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencera7bed60a2007-03-19 20:40:51 +00001808 $$ = ConstantInt::get($1, $2, true);
Reid Spencerc7a686b2007-02-28 02:24:54 +00001809 CHECK_FOR_ERROR
1810 }
1811 | IntType ESAPINTVAL { // arbitrary precision integer constants
1812 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1813 if ($2->getBitWidth() > BitWidth) {
1814 GEN_ERROR("Constant value does not fit in type");
Reid Spencer8ad254d2007-03-01 19:41:47 +00001815 }
1816 $2->sextOrTrunc(BitWidth);
1817 $$ = ConstantInt::get(*$2);
Reid Spencerc7a686b2007-02-28 02:24:54 +00001818 delete $2;
Reid Spencer266e42b2006-12-23 06:05:41 +00001819 CHECK_FOR_ERROR
1820 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001821 | IntType EUINT64VAL { // integral constants
Reid Spencer266e42b2006-12-23 06:05:41 +00001822 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001823 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencera7bed60a2007-03-19 20:40:51 +00001824 $$ = ConstantInt::get($1, $2, false);
Reid Spencerc7a686b2007-02-28 02:24:54 +00001825 CHECK_FOR_ERROR
1826 }
1827 | IntType EUAPINTVAL { // arbitrary precision integer constants
1828 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1829 if ($2->getBitWidth() > BitWidth) {
1830 GEN_ERROR("Constant value does not fit in type");
Reid Spencer8ad254d2007-03-01 19:41:47 +00001831 }
1832 $2->zextOrTrunc(BitWidth);
1833 $$ = ConstantInt::get(*$2);
Reid Spencerc7a686b2007-02-28 02:24:54 +00001834 delete $2;
Reid Spencer266e42b2006-12-23 06:05:41 +00001835 CHECK_FOR_ERROR
1836 }
Reid Spencer58a8db02007-01-13 05:00:46 +00001837 | INTTYPE TRUETOK { // Boolean constants
1838 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001839 $$ = ConstantInt::getTrue();
Reid Spencer713eedc2006-08-18 08:43:06 +00001840 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001841 }
Reid Spencer58a8db02007-01-13 05:00:46 +00001842 | INTTYPE FALSETOK { // Boolean constants
1843 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001844 $$ = ConstantInt::getFalse();
Reid Spencer713eedc2006-08-18 08:43:06 +00001845 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001846 }
1847 | FPType FPVAL { // Float & Double constants
Reid Spencere2c32da2006-12-03 05:46:11 +00001848 if (!ConstantFP::isValueValidForType($1, $2))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001849 GEN_ERROR("Floating point constant invalid for type");
Reid Spencere2c32da2006-12-03 05:46:11 +00001850 $$ = ConstantFP::get($1, $2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001851 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001852 };
1853
1854
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001855ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001856 if (!UpRefs.empty())
1857 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001858 Constant *Val = $3;
Reid Spencerceb84592007-01-17 02:48:45 +00001859 const Type *DestTy = $5->get();
1860 if (!CastInst::castIsValid($1, $3, DestTy))
1861 GEN_ERROR("invalid cast opcode for cast from '" +
1862 Val->getType()->getDescription() + "' to '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001863 DestTy->getDescription() + "'");
Reid Spencerceb84592007-01-17 02:48:45 +00001864 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencere2c32da2006-12-03 05:46:11 +00001865 delete $5;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001866 }
1867 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001868 if (!isa<PointerType>($3->getType()))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001869 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001870
Reid Spencere2c32da2006-12-03 05:46:11 +00001871 const Type *IdxTy =
Chris Lattnerf79508f2007-02-13 00:58:01 +00001872 GetElementPtrInst::getIndexedType($3->getType(), &(*$4)[0], $4->size(),
1873 true);
Reid Spencere2c32da2006-12-03 05:46:11 +00001874 if (!IdxTy)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001875 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencere2c32da2006-12-03 05:46:11 +00001876
Chris Lattner55ce85c2007-01-31 04:44:08 +00001877 SmallVector<Constant*, 8> IdxVec;
Reid Spencere2c32da2006-12-03 05:46:11 +00001878 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1879 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001880 IdxVec.push_back(C);
1881 else
Reid Spencera7bb3e92007-02-05 10:18:06 +00001882 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001883
1884 delete $4;
1885
Chris Lattner55ce85c2007-01-31 04:44:08 +00001886 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer713eedc2006-08-18 08:43:06 +00001887 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001888 }
1889 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer542964f2007-01-11 18:21:29 +00001890 if ($3->getType() != Type::Int1Ty)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001891 GEN_ERROR("Select condition must be of boolean type");
Reid Spencere2c32da2006-12-03 05:46:11 +00001892 if ($5->getType() != $7->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001893 GEN_ERROR("Select operand types must match");
Reid Spencere2c32da2006-12-03 05:46:11 +00001894 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001895 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001896 }
1897 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001898 if ($3->getType() != $5->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001899 GEN_ERROR("Binary operator types must match");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001900 CHECK_FOR_ERROR;
Reid Spencer844668d2006-12-05 19:16:11 +00001901 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001902 }
1903 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001904 if ($3->getType() != $5->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001905 GEN_ERROR("Logical operator types must match");
Chris Lattner03c49532007-01-15 02:27:26 +00001906 if (!$3->getType()->isInteger()) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001907 if (Instruction::isShift($1) || !isa<VectorType>($3->getType()) ||
1908 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001909 GEN_ERROR("Logical operator requires integral operands");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001910 }
Reid Spencere2c32da2006-12-03 05:46:11 +00001911 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001912 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001913 }
Reid Spencerd2e0c342006-12-04 05:24:24 +00001914 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1915 if ($4->getType() != $6->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001916 GEN_ERROR("icmp operand types must match");
Reid Spencerd2e0c342006-12-04 05:24:24 +00001917 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencere2c32da2006-12-03 05:46:11 +00001918 }
Reid Spencerd2e0c342006-12-04 05:24:24 +00001919 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1920 if ($4->getType() != $6->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001921 GEN_ERROR("fcmp operand types must match");
Reid Spencerd2e0c342006-12-04 05:24:24 +00001922 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencere2c32da2006-12-03 05:46:11 +00001923 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001924 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001925 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001926 GEN_ERROR("Invalid extractelement operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00001927 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001928 CHECK_FOR_ERROR
Chris Lattneraebccf82006-04-08 03:55:17 +00001929 }
1930 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001931 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001932 GEN_ERROR("Invalid insertelement operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00001933 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001934 CHECK_FOR_ERROR
Chris Lattneraebccf82006-04-08 03:55:17 +00001935 }
1936 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001937 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001938 GEN_ERROR("Invalid shufflevector operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00001939 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001940 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001941 };
1942
Chris Lattneraebccf82006-04-08 03:55:17 +00001943
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001944// ConstVector - A list of comma separated constants.
1945ConstVector : ConstVector ',' ConstVal {
1946 ($$ = $1)->push_back($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001947 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001948 }
1949 | ConstVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00001950 $$ = new std::vector<Constant*>();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001951 $$->push_back($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001952 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001953 };
1954
1955
1956// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1957GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
1958
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00001959// ThreadLocal
1960ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
1961
Anton Korobeynikov511d54f2007-04-28 13:48:45 +00001962// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
1963AliaseeRef : ResultTypes SymbolicValueRef {
1964 const Type* VTy = $1->get();
1965 Value *V = getVal(VTy, $2);
1966 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
1967 if (!Aliasee)
1968 GEN_ERROR("Aliases can be created only to global values");
1969
1970 $$ = Aliasee;
1971 CHECK_FOR_ERROR
1972 delete $1;
1973 }
1974 | BITCAST '(' AliaseeRef TO Types ')' {
1975 Constant *Val = $3;
1976 const Type *DestTy = $5->get();
1977 if (!CastInst::castIsValid($1, $3, DestTy))
1978 GEN_ERROR("invalid cast opcode for cast from '" +
1979 Val->getType()->getDescription() + "' to '" +
1980 DestTy->getDescription() + "'");
1981
1982 $$ = ConstantExpr::getCast($1, $3, DestTy);
1983 CHECK_FOR_ERROR
1984 delete $5;
1985 };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001986
1987//===----------------------------------------------------------------------===//
1988// Rules to match Modules
1989//===----------------------------------------------------------------------===//
1990
1991// Module rule: Capture the result of parsing the whole file into a result
1992// variable...
1993//
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001994Module
1995 : DefinitionList {
1996 $$ = ParserResult = CurModule.CurrentModule;
1997 CurModule.ModuleDone();
1998 CHECK_FOR_ERROR;
1999 }
2000 | /*empty*/ {
2001 $$ = ParserResult = CurModule.CurrentModule;
2002 CurModule.ModuleDone();
2003 CHECK_FOR_ERROR;
2004 }
2005 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002006
Reid Spencerdc0a3a22006-12-29 20:35:03 +00002007DefinitionList
2008 : Definition
2009 | DefinitionList Definition
2010 ;
2011
2012Definition
Jeff Cohen5d956e42007-01-21 19:19:31 +00002013 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002014 CurFun.FunctionDone();
Reid Spencer713eedc2006-08-18 08:43:06 +00002015 CHECK_FOR_ERROR
Reid Spencerdc0a3a22006-12-29 20:35:03 +00002016 }
2017 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer713eedc2006-08-18 08:43:06 +00002018 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002019 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00002020 | MODULE ASM_TOK AsmBlock {
Reid Spencer713eedc2006-08-18 08:43:06 +00002021 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002022 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002023 | OptLocalAssign TYPE Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002024 if (!UpRefs.empty())
2025 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002026 // Eagerly resolve types. This is not an optimization, this is a
2027 // requirement that is due to the fact that we could have this:
2028 //
2029 // %list = type { %list * }
2030 // %list = type { %list * } ; repeated type decl
2031 //
2032 // If types are not resolved eagerly, then the two types will not be
2033 // determined to be the same type!
2034 //
Reid Spencerdc0a3a22006-12-29 20:35:03 +00002035 ResolveTypeTo($1, *$3);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002036
Reid Spencerdc0a3a22006-12-29 20:35:03 +00002037 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer309080a2006-09-28 19:28:24 +00002038 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002039 // If this is a named type that is not a redefinition, add it to the slot
2040 // table.
Reid Spencerdc0a3a22006-12-29 20:35:03 +00002041 CurModule.Types.push_back(*$3);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002042 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002043
Reid Spencerdc0a3a22006-12-29 20:35:03 +00002044 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00002045 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002046 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002047 | OptLocalAssign TYPE VOID {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002048 ResolveTypeTo($1, $3);
2049
2050 if (!setTypeName($3, $1) && !$1) {
2051 CHECK_FOR_ERROR
2052 // If this is a named type that is not a redefinition, add it to the slot
2053 // table.
2054 CurModule.Types.push_back($3);
2055 }
2056 CHECK_FOR_ERROR
2057 }
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002058 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002059 /* "Externally Visible" Linkage */
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002060 if ($5 == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002061 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002062 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
2063 $2, $4, $5->getType(), $5, $3);
Reid Spencerdc0a3a22006-12-29 20:35:03 +00002064 CHECK_FOR_ERROR
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002065 } GlobalVarAttributes {
2066 CurGV = 0;
2067 }
Chris Lattner720367c2007-04-26 05:31:05 +00002068 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
2069 ConstVal {
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002070 if ($6 == 0)
2071 GEN_ERROR("Global value initializer is not a constant");
2072 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002073 CHECK_FOR_ERROR
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002074 } GlobalVarAttributes {
2075 CurGV = 0;
2076 }
Chris Lattner720367c2007-04-26 05:31:05 +00002077 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
2078 Types {
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002079 if (!UpRefs.empty())
2080 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
2081 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4);
2082 CHECK_FOR_ERROR
2083 delete $6;
Reid Spencer309080a2006-09-28 19:28:24 +00002084 } GlobalVarAttributes {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002085 CurGV = 0;
2086 CHECK_FOR_ERROR
2087 }
Anton Korobeynikov511d54f2007-04-28 13:48:45 +00002088 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00002089 std::string Name($1);
2090 if (Name.empty())
Anton Korobeynikov511d54f2007-04-28 13:48:45 +00002091 GEN_ERROR("Alias name cannot be empty");
2092
2093 Constant* Aliasee = $5;
2094 if (Aliasee == 0)
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00002095 GEN_ERROR(std::string("Invalid aliasee for alias: ") + $1);
Anton Korobeynikov511d54f2007-04-28 13:48:45 +00002096
2097 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2098 CurModule.CurrentModule);
2099 GA->setVisibility($2);
2100 InsertValue(GA, CurModule.Values);
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00002101 CHECK_FOR_ERROR
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00002102 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00002103 | TARGET TargetDefinition {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002104 CHECK_FOR_ERROR
2105 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00002106 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer713eedc2006-08-18 08:43:06 +00002107 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002108 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00002109 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002110
2111
2112AsmBlock : STRINGCONSTANT {
2113 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
2114 char *EndStr = UnEscapeLexed($1, true);
2115 std::string NewAsm($1, EndStr);
2116 free($1);
2117
2118 if (AsmSoFar.empty())
2119 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
2120 else
2121 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
Reid Spencer713eedc2006-08-18 08:43:06 +00002122 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002123};
2124
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002125TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002126 CurModule.CurrentModule->setTargetTriple($3);
2127 free($3);
John Criswell6af0b122006-10-24 19:09:48 +00002128 }
Chris Lattner7d1d0342006-10-22 06:08:13 +00002129 | DATALAYOUT '=' STRINGCONSTANT {
Owen Anderson85690f32006-10-18 02:21:48 +00002130 CurModule.CurrentModule->setDataLayout($3);
2131 free($3);
Owen Anderson85690f32006-10-18 02:21:48 +00002132 };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002133
2134LibrariesDefinition : '[' LibList ']';
2135
2136LibList : LibList ',' STRINGCONSTANT {
2137 CurModule.CurrentModule->addLibrary($3);
2138 free($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002139 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002140 }
2141 | STRINGCONSTANT {
2142 CurModule.CurrentModule->addLibrary($1);
2143 free($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002144 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002145 }
2146 | /* empty: end of list */ {
Reid Spencer713eedc2006-08-18 08:43:06 +00002147 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002148 }
2149 ;
2150
2151//===----------------------------------------------------------------------===//
2152// Rules to match Function Headers
2153//===----------------------------------------------------------------------===//
2154
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002155ArgListH : ArgListH ',' Types OptParamAttrs OptLocalName {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002156 if (!UpRefs.empty())
2157 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
2158 if (*$3 == Type::VoidTy)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002159 GEN_ERROR("void typed arguments are invalid");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002160 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002161 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002162 $1->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002163 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002164 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002165 | Types OptParamAttrs OptLocalName {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002166 if (!UpRefs.empty())
2167 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2168 if (*$1 == Type::VoidTy)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002169 GEN_ERROR("void typed arguments are invalid");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002170 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2171 $$ = new ArgListType;
2172 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002173 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002174 };
2175
2176ArgList : ArgListH {
2177 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002178 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002179 }
2180 | ArgListH ',' DOTDOTDOT {
2181 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002182 struct ArgListEntry E;
2183 E.Ty = new PATypeHolder(Type::VoidTy);
2184 E.Name = 0;
Reid Spencera472f662007-04-11 02:44:20 +00002185 E.Attrs = ParamAttr::None;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002186 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002187 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002188 }
2189 | DOTDOTDOT {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002190 $$ = new ArgListType;
2191 struct ArgListEntry E;
2192 E.Ty = new PATypeHolder(Type::VoidTy);
2193 E.Name = 0;
Reid Spencera472f662007-04-11 02:44:20 +00002194 E.Attrs = ParamAttr::None;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002195 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002196 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002197 }
2198 | /* empty */ {
2199 $$ = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00002200 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002201 };
2202
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002203FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002204 OptFuncAttrs OptSection OptAlign {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002205 UnEscapeLexed($3);
2206 std::string FunctionName($3);
2207 free($3); // Free strdup'd memory!
2208
Reid Spencer87622ae2007-01-02 21:54:12 +00002209 // Check the function result for abstractness if this is a define. We should
2210 // have no abstract types at this point
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002211 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2212 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer87622ae2007-01-02 21:54:12 +00002213
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002214 std::vector<const Type*> ParamTypeList;
Christopher Lamb3f706f22007-04-22 20:09:11 +00002215 ParamAttrsVector Attrs;
2216 if ($7 != ParamAttr::None) {
2217 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $7;
2218 Attrs.push_back(PAWI);
2219 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002220 if ($5) { // If there are arguments...
Reid Spencerf51a7052007-04-09 06:16:21 +00002221 unsigned index = 1;
2222 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I, ++index) {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002223 const Type* Ty = I->Ty->get();
Reid Spencer87622ae2007-01-02 21:54:12 +00002224 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2225 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002226 ParamTypeList.push_back(Ty);
2227 if (Ty != Type::VoidTy)
Christopher Lamb3f706f22007-04-22 20:09:11 +00002228 if (I->Attrs != ParamAttr::None) {
2229 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2230 Attrs.push_back(PAWI);
2231 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002232 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002233 }
2234
2235 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2236 if (isVarArg) ParamTypeList.pop_back();
2237
Christopher Lamb3f706f22007-04-22 20:09:11 +00002238 ParamAttrsList *PAL = 0;
2239 if (!Attrs.empty())
2240 PAL = ParamAttrsList::get(Attrs);
Reid Spencerf51a7052007-04-09 06:16:21 +00002241
Christopher Lamb3f706f22007-04-22 20:09:11 +00002242 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg, PAL);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002243 const PointerType *PFT = PointerType::get(FT);
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002244 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002245
2246 ValID ID;
2247 if (!FunctionName.empty()) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002248 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002249 } else {
Reid Spencerd0e8d382007-03-19 18:40:50 +00002250 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002251 }
2252
2253 Function *Fn = 0;
2254 // See if this function was forward referenced. If so, recycle the object.
2255 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2256 // Move the function to the end of the list, from whereever it was
2257 // previously inserted.
2258 Fn = cast<Function>(FWRef);
2259 CurModule.CurrentModule->getFunctionList().remove(Fn);
2260 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2261 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002262 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Chris Lattner720367c2007-04-26 05:31:05 +00002263 if (Fn->getFunctionType() != FT) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002264 // The existing function doesn't have the same type. This is an overload
2265 // error.
2266 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
2267 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
Chris Lattner720367c2007-04-26 05:31:05 +00002268 // Neither the existing or the current function is a declaration and they
2269 // have the same name and same type. Clearly this is a redefinition.
2270 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002271 } if (Fn->isDeclaration()) {
2272 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002273 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2274 AI != AE; ++AI)
2275 AI->setName("");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002276 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002277 } else { // Not already defined?
Chris Lattner720367c2007-04-26 05:31:05 +00002278 Fn = new Function(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002279 CurModule.CurrentModule);
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002280
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002281 InsertValue(Fn, CurModule.Values);
2282 }
2283
2284 CurFun.FunctionStart(Fn);
Anton Korobeynikov0ab01ff2006-09-17 13:06:18 +00002285
2286 if (CurFun.isDeclare) {
2287 // If we have declaration, always overwrite linkage. This will allow us to
2288 // correctly handle cases, when pointer to function is passed as argument to
2289 // another function.
2290 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002291 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov0ab01ff2006-09-17 13:06:18 +00002292 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002293 Fn->setCallingConv($1);
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002294 Fn->setAlignment($9);
2295 if ($8) {
2296 Fn->setSection($8);
2297 free($8);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002298 }
2299
2300 // Add all of the arguments we parsed to the function...
2301 if ($5) { // Is null if empty...
2302 if (isVarArg) { // Nuke the last entry
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002303 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0 &&
Reid Spencerac6bfc52007-02-05 17:04:00 +00002304 "Not a varargs marker!");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002305 delete $5->back().Ty;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002306 $5->pop_back(); // Delete the last entry
2307 }
2308 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002309 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002310 unsigned Idx = 1;
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002311 for (ArgListType::iterator I = $5->begin();
2312 I != $5->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002313 delete I->Ty; // Delete the typeholder...
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002314 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer309080a2006-09-28 19:28:24 +00002315 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002316 InsertValue(ArgIt);
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002317 Idx++;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002318 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002319
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002320 delete $5; // We're now done with the argument list
2321 }
Reid Spencer713eedc2006-08-18 08:43:06 +00002322 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002323};
2324
2325BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2326
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002327FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002328 $$ = CurFun.CurrentFunction;
2329
2330 // Make sure that we keep track of the linkage type even if there was a
2331 // previous "declare".
2332 $$->setLinkage($1);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002333 $$->setVisibility($2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002334};
2335
2336END : ENDTOK | '}'; // Allow end of '}' to end a function
2337
2338Function : BasicBlockList END {
2339 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002340 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002341};
2342
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002343FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002344 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002345 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002346 $$ = CurFun.CurrentFunction;
2347 CurFun.FunctionDone();
2348 CHECK_FOR_ERROR
2349 };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002350
2351//===----------------------------------------------------------------------===//
2352// Rules to match Basic Blocks
2353//===----------------------------------------------------------------------===//
2354
2355OptSideEffect : /* empty */ {
2356 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002357 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002358 }
2359 | SIDEEFFECT {
2360 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002361 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002362 };
2363
2364ConstValueRef : ESINT64VAL { // A reference to a direct constant
2365 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002366 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002367 }
2368 | EUINT64VAL {
2369 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002370 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002371 }
2372 | FPVAL { // Perhaps it's an FP constant?
2373 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002374 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002375 }
2376 | TRUETOK {
Zhou Sheng75b871f2007-01-11 12:24:14 +00002377 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer713eedc2006-08-18 08:43:06 +00002378 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002379 }
2380 | FALSETOK {
Zhou Sheng75b871f2007-01-11 12:24:14 +00002381 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer713eedc2006-08-18 08:43:06 +00002382 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002383 }
2384 | NULL_TOK {
2385 $$ = ValID::createNull();
Reid Spencer713eedc2006-08-18 08:43:06 +00002386 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002387 }
2388 | UNDEF {
2389 $$ = ValID::createUndef();
Reid Spencer713eedc2006-08-18 08:43:06 +00002390 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002391 }
2392 | ZEROINITIALIZER { // A vector zero constant.
2393 $$ = ValID::createZeroInit();
Reid Spencer713eedc2006-08-18 08:43:06 +00002394 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002395 }
2396 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencere2c32da2006-12-03 05:46:11 +00002397 const Type *ETy = (*$2)[0]->getType();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002398 int NumElements = $2->size();
2399
Reid Spencerd84d35b2007-02-15 02:26:10 +00002400 VectorType* pt = VectorType::get(ETy, NumElements);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002401 PATypeHolder* PTy = new PATypeHolder(
Reid Spencere2c32da2006-12-03 05:46:11 +00002402 HandleUpRefs(
Reid Spencerd84d35b2007-02-15 02:26:10 +00002403 VectorType::get(
Reid Spencere2c32da2006-12-03 05:46:11 +00002404 ETy,
2405 NumElements)
2406 )
2407 );
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002408
2409 // Verify all elements are correct type!
2410 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencere2c32da2006-12-03 05:46:11 +00002411 if (ETy != (*$2)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002412 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002413 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00002414 (*$2)[i]->getType()->getDescription() + "'.");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002415 }
2416
Reid Spencerd84d35b2007-02-15 02:26:10 +00002417 $$ = ValID::create(ConstantVector::get(pt, *$2));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002418 delete PTy; delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002419 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002420 }
2421 | ConstExpr {
Reid Spencere2c32da2006-12-03 05:46:11 +00002422 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002423 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002424 }
2425 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
2426 char *End = UnEscapeLexed($3, true);
2427 std::string AsmStr = std::string($3, End);
2428 End = UnEscapeLexed($5, true);
2429 std::string Constraints = std::string($5, End);
2430 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
2431 free($3);
2432 free($5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002433 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002434 };
2435
2436// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2437// another value.
2438//
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002439SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2440 $$ = ValID::createLocalID($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002441 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002442 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002443 | GLOBALVAL_ID {
2444 $$ = ValID::createGlobalID($1);
2445 CHECK_FOR_ERROR
2446 }
2447 | LocalName { // Is it a named reference...?
2448 $$ = ValID::createLocalName($1);
2449 CHECK_FOR_ERROR
2450 }
2451 | GlobalName { // Is it a named reference...?
2452 $$ = ValID::createGlobalName($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002453 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002454 };
2455
2456// ValueRef - A reference to a definition... either constant or symbolic
2457ValueRef : SymbolicValueRef | ConstValueRef;
2458
2459
2460// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2461// type immediately preceeds the value reference, and allows complex constant
2462// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2463ResolvedVal : Types ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002464 if (!UpRefs.empty())
2465 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2466 $$ = getVal(*$1, $2);
2467 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002468 CHECK_FOR_ERROR
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002469 }
2470 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002471
2472BasicBlockList : BasicBlockList BasicBlock {
2473 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002474 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002475 }
2476 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
2477 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002478 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002479 };
2480
2481
2482// Basic blocks are terminated by branching instructions:
2483// br, br/cc, switch, ret
2484//
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002485BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002486 setValueName($3, $2);
Reid Spencer309080a2006-09-28 19:28:24 +00002487 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002488 InsertValue($3);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002489 $1->getInstList().push_back($3);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002490 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002491 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002492 };
2493
2494InstructionList : InstructionList Inst {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002495 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2496 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2497 if (CI2->getParent() == 0)
2498 $1->getInstList().push_back(CI2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002499 $1->getInstList().push_back($2);
2500 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002501 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002502 }
Reid Spencerd0e8d382007-03-19 18:40:50 +00002503 | /* empty */ { // Empty space between instruction lists
2504 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
Reid Spencer713eedc2006-08-18 08:43:06 +00002505 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002506 }
Reid Spencerd0e8d382007-03-19 18:40:50 +00002507 | LABELSTR { // Labelled (named) basic block
2508 $$ = defineBBVal(ValID::createLocalName($1));
Reid Spencer713eedc2006-08-18 08:43:06 +00002509 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002510 };
2511
2512BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencere2c32da2006-12-03 05:46:11 +00002513 $$ = new ReturnInst($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00002514 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002515 }
Reid Spencerd0e8d382007-03-19 18:40:50 +00002516 | RET VOID { // Return with no result...
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002517 $$ = new ReturnInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002518 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002519 }
Reid Spencerd0e8d382007-03-19 18:40:50 +00002520 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer309080a2006-09-28 19:28:24 +00002521 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002522 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002523 $$ = new BranchInst(tmpBB);
Reid Spencerd0e8d382007-03-19 18:40:50 +00002524 } // Conditional Branch...
Reid Spencer58a8db02007-01-13 05:00:46 +00002525 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
2526 assert(cast<IntegerType>($2)->getBitWidth() == 1 && "Not Bool?");
Reid Spencer309080a2006-09-28 19:28:24 +00002527 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002528 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002529 BasicBlock* tmpBBB = getBBVal($9);
2530 CHECK_FOR_ERROR
Reid Spencer542964f2007-01-11 18:21:29 +00002531 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002532 CHECK_FOR_ERROR
2533 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002534 }
2535 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencere2c32da2006-12-03 05:46:11 +00002536 Value* tmpVal = getVal($2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002537 CHECK_FOR_ERROR
2538 BasicBlock* tmpBB = getBBVal($6);
2539 CHECK_FOR_ERROR
2540 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002541 $$ = S;
2542
2543 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2544 E = $8->end();
2545 for (; I != E; ++I) {
2546 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2547 S->addCase(CI, I->second);
2548 else
Reid Spencera7bb3e92007-02-05 10:18:06 +00002549 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002550 }
2551 delete $8;
Reid Spencer713eedc2006-08-18 08:43:06 +00002552 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002553 }
2554 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencere2c32da2006-12-03 05:46:11 +00002555 Value* tmpVal = getVal($2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002556 CHECK_FOR_ERROR
2557 BasicBlock* tmpBB = getBBVal($6);
2558 CHECK_FOR_ERROR
2559 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002560 $$ = S;
Reid Spencer713eedc2006-08-18 08:43:06 +00002561 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002562 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002563 | INVOKE OptCallingConv ResultTypes ValueRef '(' ValueRefList ')' OptFuncAttrs
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002564 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002565
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002566 // Handle the short syntax
2567 const PointerType *PFTy = 0;
2568 const FunctionType *Ty = 0;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002569 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002570 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2571 // Pull out the types of all of the arguments...
2572 std::vector<const Type*> ParamTypes;
Christopher Lamb3f706f22007-04-22 20:09:11 +00002573 ParamAttrsVector Attrs;
2574 if ($8 != ParamAttr::None) {
2575 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = 8;
2576 Attrs.push_back(PAWI);
2577 }
Reid Spencerf51a7052007-04-09 06:16:21 +00002578 ValueRefList::iterator I = $6->begin(), E = $6->end();
2579 unsigned index = 1;
2580 for (; I != E; ++I, ++index) {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002581 const Type *Ty = I->Val->getType();
2582 if (Ty == Type::VoidTy)
2583 GEN_ERROR("Short call syntax cannot be used with varargs");
2584 ParamTypes.push_back(Ty);
Christopher Lamb3f706f22007-04-22 20:09:11 +00002585 if (I->Attrs != ParamAttr::None) {
2586 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2587 Attrs.push_back(PAWI);
2588 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002589 }
2590
Christopher Lamb3f706f22007-04-22 20:09:11 +00002591 ParamAttrsList *PAL = 0;
2592 if (!Attrs.empty())
2593 PAL = ParamAttrsList::get(Attrs);
2594 Ty = FunctionType::get($3->get(), ParamTypes, false, PAL);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002595 PFTy = PointerType::get(Ty);
2596 }
2597
Reid Spencer6fb989c2007-03-20 01:13:36 +00002598 delete $3;
2599
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002600 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer309080a2006-09-28 19:28:24 +00002601 CHECK_FOR_ERROR
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002602 BasicBlock *Normal = getBBVal($11);
Reid Spencer309080a2006-09-28 19:28:24 +00002603 CHECK_FOR_ERROR
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002604 BasicBlock *Except = getBBVal($14);
Reid Spencer309080a2006-09-28 19:28:24 +00002605 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002606
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002607 // Check the arguments
2608 ValueList Args;
2609 if ($6->empty()) { // Has no arguments?
2610 // Make sure no arguments is a good thing!
2611 if (Ty->getNumParams() != 0)
2612 GEN_ERROR("No arguments passed to a function that "
Reid Spencera7bb3e92007-02-05 10:18:06 +00002613 "expects arguments");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002614 } else { // Has arguments?
2615 // Loop through FunctionType's arguments and ensure they are specified
2616 // correctly!
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002617 FunctionType::param_iterator I = Ty->param_begin();
2618 FunctionType::param_iterator E = Ty->param_end();
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002619 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002620
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002621 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2622 if (ArgI->Val->getType() != *I)
2623 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00002624 (*I)->getDescription() + "'");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002625 Args.push_back(ArgI->Val);
2626 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002627
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002628 if (Ty->isVarArg()) {
2629 if (I == E)
2630 for (; ArgI != ArgE; ++ArgI)
2631 Args.push_back(ArgI->Val); // push the remaining varargs
2632 } else if (I != E || ArgI != ArgE)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002633 GEN_ERROR("Invalid number of parameters detected");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002634 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002635
2636 // Create the InvokeInst
Chris Lattnerd8028242007-02-13 05:53:56 +00002637 InvokeInst *II = new InvokeInst(V, Normal, Except, &Args[0], Args.size());
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002638 II->setCallingConv($2);
2639 $$ = II;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002640 delete $6;
Reid Spencer713eedc2006-08-18 08:43:06 +00002641 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002642 }
2643 | UNWIND {
2644 $$ = new UnwindInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002645 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002646 }
2647 | UNREACHABLE {
2648 $$ = new UnreachableInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002649 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002650 };
2651
2652
2653
2654JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2655 $$ = $1;
Reid Spencerd0e8d382007-03-19 18:40:50 +00002656 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer309080a2006-09-28 19:28:24 +00002657 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002658 if (V == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002659 GEN_ERROR("May only switch on a constant pool value");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002660
Reid Spencer309080a2006-09-28 19:28:24 +00002661 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002662 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002663 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002664 }
2665 | IntType ConstValueRef ',' LABEL ValueRef {
2666 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencerd0e8d382007-03-19 18:40:50 +00002667 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer309080a2006-09-28 19:28:24 +00002668 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002669
2670 if (V == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002671 GEN_ERROR("May only switch on a constant pool value");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002672
Reid Spencer309080a2006-09-28 19:28:24 +00002673 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002674 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002675 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002676 };
2677
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002678Inst : OptLocalAssign InstVal {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002679 // Is this definition named?? if so, assign the name...
2680 setValueName($2, $1);
2681 CHECK_FOR_ERROR
2682 InsertValue($2);
2683 $$ = $2;
2684 CHECK_FOR_ERROR
2685 };
2686
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002687
2688PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002689 if (!UpRefs.empty())
2690 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002691 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencere2c32da2006-12-03 05:46:11 +00002692 Value* tmpVal = getVal(*$1, $3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002693 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002694 BasicBlock* tmpBB = getBBVal($5);
2695 CHECK_FOR_ERROR
2696 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencere2c32da2006-12-03 05:46:11 +00002697 delete $1;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002698 }
2699 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2700 $$ = $1;
Reid Spencer309080a2006-09-28 19:28:24 +00002701 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002702 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002703 BasicBlock* tmpBB = getBBVal($6);
2704 CHECK_FOR_ERROR
2705 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002706 };
2707
2708
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002709ValueRefList : Types ValueRef OptParamAttrs {
2710 if (!UpRefs.empty())
2711 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2712 // Used for call and invoke instructions
2713 $$ = new ValueRefList();
2714 ValueRefListEntry E; E.Attrs = $3; E.Val = getVal($1->get(), $2);
2715 $$->push_back(E);
Reid Spencer6fb989c2007-03-20 01:13:36 +00002716 delete $1;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002717 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002718 | ValueRefList ',' Types ValueRef OptParamAttrs {
2719 if (!UpRefs.empty())
2720 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002721 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002722 ValueRefListEntry E; E.Attrs = $5; E.Val = getVal($3->get(), $4);
2723 $$->push_back(E);
Reid Spencer6fb989c2007-03-20 01:13:36 +00002724 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00002725 CHECK_FOR_ERROR
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002726 }
2727 | /*empty*/ { $$ = new ValueRefList(); };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002728
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002729IndexList // Used for gep instructions and constant expressions
Reid Spencerd0da3e22006-12-31 21:47:02 +00002730 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002731 | IndexList ',' ResolvedVal {
2732 $$ = $1;
2733 $$->push_back($3);
2734 CHECK_FOR_ERROR
2735 }
Reid Spencerd0da3e22006-12-31 21:47:02 +00002736 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002737
2738OptTailCall : TAIL CALL {
2739 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002740 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002741 }
2742 | CALL {
2743 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002744 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002745 };
2746
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002747InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002748 if (!UpRefs.empty())
2749 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner03c49532007-01-15 02:27:26 +00002750 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencerd84d35b2007-02-15 02:26:10 +00002751 !isa<VectorType>((*$2).get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002752 GEN_ERROR(
Reid Spencera7bb3e92007-02-05 10:18:06 +00002753 "Arithmetic operator requires integer, FP, or packed operands");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002754 if (isa<VectorType>((*$2).get()) &&
Reid Spencere2c32da2006-12-03 05:46:11 +00002755 ($1 == Instruction::URem ||
2756 $1 == Instruction::SRem ||
2757 $1 == Instruction::FRem))
Chris Lattner4669b0b2007-02-19 07:44:24 +00002758 GEN_ERROR("Remainder not supported on vector types");
Reid Spencere2c32da2006-12-03 05:46:11 +00002759 Value* val1 = getVal(*$2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002760 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002761 Value* val2 = getVal(*$2, $5);
Reid Spencer309080a2006-09-28 19:28:24 +00002762 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002763 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002764 if ($$ == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002765 GEN_ERROR("binary operator returned null");
Reid Spencere2c32da2006-12-03 05:46:11 +00002766 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002767 }
2768 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002769 if (!UpRefs.empty())
2770 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner03c49532007-01-15 02:27:26 +00002771 if (!(*$2)->isInteger()) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002772 if (Instruction::isShift($1) || !isa<VectorType>($2->get()) ||
2773 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencera7bb3e92007-02-05 10:18:06 +00002774 GEN_ERROR("Logical operator requires integral operands");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002775 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002776 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002777 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002778 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer309080a2006-09-28 19:28:24 +00002779 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002780 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002781 if ($$ == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002782 GEN_ERROR("binary operator returned null");
Reid Spencere2c32da2006-12-03 05:46:11 +00002783 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002784 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002785 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002786 if (!UpRefs.empty())
2787 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencerd84d35b2007-02-15 02:26:10 +00002788 if (isa<VectorType>((*$3).get()))
Chris Lattner4669b0b2007-02-19 07:44:24 +00002789 GEN_ERROR("Vector types not supported by icmp instruction");
Reid Spencere2c32da2006-12-03 05:46:11 +00002790 Value* tmpVal1 = getVal(*$3, $4);
2791 CHECK_FOR_ERROR
2792 Value* tmpVal2 = getVal(*$3, $6);
2793 CHECK_FOR_ERROR
2794 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2795 if ($$ == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002796 GEN_ERROR("icmp operator returned null");
Reid Spencer6fb989c2007-03-20 01:13:36 +00002797 delete $3;
Reid Spencere2c32da2006-12-03 05:46:11 +00002798 }
2799 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002800 if (!UpRefs.empty())
2801 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencerd84d35b2007-02-15 02:26:10 +00002802 if (isa<VectorType>((*$3).get()))
Chris Lattner4669b0b2007-02-19 07:44:24 +00002803 GEN_ERROR("Vector types not supported by fcmp instruction");
Reid Spencere2c32da2006-12-03 05:46:11 +00002804 Value* tmpVal1 = getVal(*$3, $4);
2805 CHECK_FOR_ERROR
2806 Value* tmpVal2 = getVal(*$3, $6);
2807 CHECK_FOR_ERROR
2808 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2809 if ($$ == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002810 GEN_ERROR("fcmp operator returned null");
Reid Spencer6fb989c2007-03-20 01:13:36 +00002811 delete $3;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002812 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002813 | CastOps ResolvedVal TO Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002814 if (!UpRefs.empty())
2815 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002816 Value* Val = $2;
Reid Spencerceb84592007-01-17 02:48:45 +00002817 const Type* DestTy = $4->get();
2818 if (!CastInst::castIsValid($1, Val, DestTy))
2819 GEN_ERROR("invalid cast opcode for cast from '" +
2820 Val->getType()->getDescription() + "' to '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00002821 DestTy->getDescription() + "'");
Reid Spencerceb84592007-01-17 02:48:45 +00002822 $$ = CastInst::create($1, Val, DestTy);
Reid Spencere2c32da2006-12-03 05:46:11 +00002823 delete $4;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002824 }
2825 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer542964f2007-01-11 18:21:29 +00002826 if ($2->getType() != Type::Int1Ty)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002827 GEN_ERROR("select condition must be boolean");
Reid Spencere2c32da2006-12-03 05:46:11 +00002828 if ($4->getType() != $6->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00002829 GEN_ERROR("select value types should match");
Reid Spencere2c32da2006-12-03 05:46:11 +00002830 $$ = new SelectInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002831 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002832 }
2833 | VAARG ResolvedVal ',' Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002834 if (!UpRefs.empty())
2835 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002836 $$ = new VAArgInst($2, *$4);
2837 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00002838 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002839 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002840 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002841 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencera7bb3e92007-02-05 10:18:06 +00002842 GEN_ERROR("Invalid extractelement operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00002843 $$ = new ExtractElementInst($2, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002844 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002845 }
2846 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002847 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencera7bb3e92007-02-05 10:18:06 +00002848 GEN_ERROR("Invalid insertelement operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00002849 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002850 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002851 }
Chris Lattner9ff96a72006-04-08 01:18:56 +00002852 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002853 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencera7bb3e92007-02-05 10:18:06 +00002854 GEN_ERROR("Invalid shufflevector operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00002855 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002856 CHECK_FOR_ERROR
Chris Lattner9ff96a72006-04-08 01:18:56 +00002857 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002858 | PHI_TOK PHIList {
2859 const Type *Ty = $2->front().first->getType();
2860 if (!Ty->isFirstClassType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00002861 GEN_ERROR("PHI node operands must be of first class type");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002862 $$ = new PHINode(Ty);
2863 ((PHINode*)$$)->reserveOperandSpace($2->size());
2864 while ($2->begin() != $2->end()) {
2865 if ($2->front().first->getType() != Ty)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002866 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002867 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
2868 $2->pop_front();
2869 }
2870 delete $2; // Free the list...
Reid Spencer713eedc2006-08-18 08:43:06 +00002871 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002872 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002873 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ValueRefList ')'
2874 OptFuncAttrs {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002875
2876 // Handle the short syntax
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002877 const PointerType *PFTy = 0;
2878 const FunctionType *Ty = 0;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002879 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002880 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2881 // Pull out the types of all of the arguments...
2882 std::vector<const Type*> ParamTypes;
Christopher Lamb3f706f22007-04-22 20:09:11 +00002883 ParamAttrsVector Attrs;
2884 if ($8 != ParamAttr::None) {
2885 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $8;
2886 Attrs.push_back(PAWI);
2887 }
Reid Spencerf51a7052007-04-09 06:16:21 +00002888 unsigned index = 1;
2889 ValueRefList::iterator I = $6->begin(), E = $6->end();
2890 for (; I != E; ++I, ++index) {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002891 const Type *Ty = I->Val->getType();
2892 if (Ty == Type::VoidTy)
2893 GEN_ERROR("Short call syntax cannot be used with varargs");
2894 ParamTypes.push_back(Ty);
Christopher Lamb3f706f22007-04-22 20:09:11 +00002895 if (I->Attrs != ParamAttr::None) {
2896 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2897 Attrs.push_back(PAWI);
2898 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002899 }
2900
Christopher Lamb3f706f22007-04-22 20:09:11 +00002901 ParamAttrsList *PAL = 0;
2902 if (!Attrs.empty())
2903 PAL = ParamAttrsList::get(Attrs);
Reid Spencerf51a7052007-04-09 06:16:21 +00002904
Christopher Lamb3f706f22007-04-22 20:09:11 +00002905 Ty = FunctionType::get($3->get(), ParamTypes, false, PAL);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002906 PFTy = PointerType::get(Ty);
2907 }
Chris Lattner720367c2007-04-26 05:31:05 +00002908
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002909 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer309080a2006-09-28 19:28:24 +00002910 CHECK_FOR_ERROR
Chris Lattner720367c2007-04-26 05:31:05 +00002911
Reid Spencere6a50a12007-04-16 06:56:07 +00002912 // Check for call to invalid intrinsic to avoid crashing later.
2913 if (Function *theF = dyn_cast<Function>(V)) {
Reid Spencer0ff60612007-04-16 22:02:23 +00002914 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
Reid Spencer660fa7b2007-04-16 20:35:38 +00002915 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
2916 !theF->getIntrinsicID(true))
Reid Spencere6a50a12007-04-16 06:56:07 +00002917 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
2918 theF->getName() + "'");
2919 }
2920
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002921 // Check the arguments
2922 ValueList Args;
2923 if ($6->empty()) { // Has no arguments?
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002924 // Make sure no arguments is a good thing!
2925 if (Ty->getNumParams() != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002926 GEN_ERROR("No arguments passed to a function that "
Reid Spencera7bb3e92007-02-05 10:18:06 +00002927 "expects arguments");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002928 } else { // Has arguments?
2929 // Loop through FunctionType's arguments and ensure they are specified
2930 // correctly!
2931 //
2932 FunctionType::param_iterator I = Ty->param_begin();
2933 FunctionType::param_iterator E = Ty->param_end();
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002934 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002935
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002936 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2937 if (ArgI->Val->getType() != *I)
2938 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00002939 (*I)->getDescription() + "'");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002940 Args.push_back(ArgI->Val);
2941 }
2942 if (Ty->isVarArg()) {
2943 if (I == E)
2944 for (; ArgI != ArgE; ++ArgI)
2945 Args.push_back(ArgI->Val); // push the remaining varargs
2946 } else if (I != E || ArgI != ArgE)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002947 GEN_ERROR("Invalid number of parameters detected");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002948 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002949 // Create the call node
Chris Lattnerd8028242007-02-13 05:53:56 +00002950 CallInst *CI = new CallInst(V, &Args[0], Args.size());
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002951 CI->setTailCall($1);
2952 CI->setCallingConv($2);
2953 $$ = CI;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002954 delete $6;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002955 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00002956 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002957 }
2958 | MemoryInst {
2959 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002960 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002961 };
2962
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002963OptVolatile : VOLATILE {
2964 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002965 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002966 }
2967 | /* empty */ {
2968 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002969 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002970 };
2971
2972
2973
2974MemoryInst : MALLOC Types OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002975 if (!UpRefs.empty())
2976 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002977 $$ = new MallocInst(*$2, 0, $3);
2978 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002979 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002980 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +00002981 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002982 if (!UpRefs.empty())
2983 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002984 Value* tmpVal = getVal($4, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002985 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002986 $$ = new MallocInst(*$2, tmpVal, $6);
2987 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002988 }
2989 | ALLOCA Types OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002990 if (!UpRefs.empty())
2991 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002992 $$ = new AllocaInst(*$2, 0, $3);
2993 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002994 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002995 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +00002996 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002997 if (!UpRefs.empty())
2998 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002999 Value* tmpVal = getVal($4, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00003000 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00003001 $$ = new AllocaInst(*$2, tmpVal, $6);
3002 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003003 }
3004 | FREE ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00003005 if (!isa<PointerType>($2->getType()))
Reid Spencer713eedc2006-08-18 08:43:06 +00003006 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencera7bb3e92007-02-05 10:18:06 +00003007 $2->getType()->getDescription() + "");
Reid Spencere2c32da2006-12-03 05:46:11 +00003008 $$ = new FreeInst($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00003009 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003010 }
3011
Christopher Lamb3f706f22007-04-22 20:09:11 +00003012 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00003013 if (!UpRefs.empty())
3014 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00003015 if (!isa<PointerType>($3->get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00003016 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencere2c32da2006-12-03 05:46:11 +00003017 (*$3)->getDescription());
3018 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00003019 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencere2c32da2006-12-03 05:46:11 +00003020 (*$3)->getDescription());
3021 Value* tmpVal = getVal(*$3, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00003022 CHECK_FOR_ERROR
Christopher Lamb3f706f22007-04-22 20:09:11 +00003023 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencere2c32da2006-12-03 05:46:11 +00003024 delete $3;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003025 }
Christopher Lamb3f706f22007-04-22 20:09:11 +00003026 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00003027 if (!UpRefs.empty())
3028 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00003029 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003030 if (!PT)
Reid Spencer713eedc2006-08-18 08:43:06 +00003031 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencere2c32da2006-12-03 05:46:11 +00003032 (*$5)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003033 const Type *ElTy = PT->getElementType();
Reid Spencere2c32da2006-12-03 05:46:11 +00003034 if (ElTy != $3->getType())
3035 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencera7bb3e92007-02-05 10:18:06 +00003036 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003037
Reid Spencere2c32da2006-12-03 05:46:11 +00003038 Value* tmpVal = getVal(*$5, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00003039 CHECK_FOR_ERROR
Christopher Lamb3f706f22007-04-22 20:09:11 +00003040 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencere2c32da2006-12-03 05:46:11 +00003041 delete $5;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003042 }
3043 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00003044 if (!UpRefs.empty())
3045 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00003046 if (!isa<PointerType>($2->get()))
Reid Spencera7bb3e92007-02-05 10:18:06 +00003047 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003048
Chris Lattnerf79508f2007-02-13 00:58:01 +00003049 if (!GetElementPtrInst::getIndexedType(*$2, &(*$4)[0], $4->size(), true))
Reid Spencer713eedc2006-08-18 08:43:06 +00003050 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00003051 (*$2)->getDescription()+ "'");
Reid Spencere2c32da2006-12-03 05:46:11 +00003052 Value* tmpVal = getVal(*$2, $3);
Reid Spencer713eedc2006-08-18 08:43:06 +00003053 CHECK_FOR_ERROR
Chris Lattnerf79508f2007-02-13 00:58:01 +00003054 $$ = new GetElementPtrInst(tmpVal, &(*$4)[0], $4->size());
Reid Spencere2c32da2006-12-03 05:46:11 +00003055 delete $2;
Reid Spencer309080a2006-09-28 19:28:24 +00003056 delete $4;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003057 };
3058
3059
3060%%
Reid Spencer713eedc2006-08-18 08:43:06 +00003061
Reid Spencer42f0cbe2006-12-31 05:40:51 +00003062// common code from the two 'RunVMAsmParser' functions
3063static Module* RunParser(Module * M) {
3064
3065 llvmAsmlineno = 1; // Reset the current line number...
3066 CurModule.CurrentModule = M;
3067#if YYDEBUG
3068 yydebug = Debug;
3069#endif
3070
3071 // Check to make sure the parser succeeded
3072 if (yyparse()) {
3073 if (ParserResult)
3074 delete ParserResult;
3075 return 0;
3076 }
3077
Reid Spenceref01c472007-03-30 01:37:39 +00003078 // Emit an error if there are any unresolved types left.
3079 if (!CurModule.LateResolveTypes.empty()) {
3080 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3081 if (DID.Type == ValID::LocalName) {
3082 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3083 } else {
3084 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3085 }
3086 if (ParserResult)
3087 delete ParserResult;
3088 return 0;
3089 }
3090
3091 // Emit an error if there are any unresolved values left.
3092 if (!CurModule.LateResolveValues.empty()) {
3093 Value *V = CurModule.LateResolveValues.back();
3094 std::map<Value*, std::pair<ValID, int> >::iterator I =
3095 CurModule.PlaceHolderInfo.find(V);
3096
3097 if (I != CurModule.PlaceHolderInfo.end()) {
3098 ValID &DID = I->second.first;
3099 if (DID.Type == ValID::LocalName) {
3100 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3101 } else {
3102 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3103 }
3104 if (ParserResult)
3105 delete ParserResult;
3106 return 0;
3107 }
3108 }
3109
Reid Spencer42f0cbe2006-12-31 05:40:51 +00003110 // Check to make sure that parsing produced a result
3111 if (!ParserResult)
3112 return 0;
3113
3114 // Reset ParserResult variable while saving its value for the result.
3115 Module *Result = ParserResult;
3116 ParserResult = 0;
3117
3118 return Result;
3119}
3120
Reid Spencer713eedc2006-08-18 08:43:06 +00003121void llvm::GenerateError(const std::string &message, int LineNo) {
3122 if (LineNo == -1) LineNo = llvmAsmlineno;
3123 // TODO: column number in exception
3124 if (TheParseError)
3125 TheParseError->setError(CurFilename, message, LineNo);
3126 TriggerError = 1;
3127}
3128
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003129int yyerror(const char *ErrorMsg) {
3130 std::string where
3131 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
3132 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00003133 std::string errMsg = where + "error: " + std::string(ErrorMsg);
3134 if (yychar != YYEMPTY && yychar != 0)
3135 errMsg += " while reading token: '" + std::string(llvmAsmtext, llvmAsmleng)+
3136 "'";
Reid Spencer713eedc2006-08-18 08:43:06 +00003137 GenerateError(errMsg);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003138 return 0;
3139}