blob: 21aa30b332ca538d3871db6f48745c9771350766 [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 //
483 V = new Argument(Ty);
484
485 // Remember where this forward reference came from. FIXME, shouldn't we try
486 // to recycle these things??
487 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
488 llvmAsmlineno)));
489
490 if (inFunctionScope())
491 InsertValue(V, CurFun.LateResolveValues);
492 else
493 InsertValue(V, CurModule.LateResolveValues);
494 return V;
495}
496
Reid Spencerd0e8d382007-03-19 18:40:50 +0000497/// defineBBVal - This is a definition of a new basic block with the specified
498/// identifier which must be the same as CurFun.NextValNum, if its numeric.
499static BasicBlock *defineBBVal(const ValID &ID) {
Reid Spencerac6bfc52007-02-05 17:04:00 +0000500 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000501
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000502 BasicBlock *BB = 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000503
Reid Spencerd0e8d382007-03-19 18:40:50 +0000504 // First, see if this was forward referenced
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000505
Reid Spencerd0e8d382007-03-19 18:40:50 +0000506 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
507 if (BBI != CurFun.BBForwardRefs.end()) {
508 BB = BBI->second;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000509 // The forward declaration could have been inserted anywhere in the
510 // function: insert it into the correct place now.
511 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
512 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Reid Spencerd0e8d382007-03-19 18:40:50 +0000513
Reid Spencer6fb989c2007-03-20 01:13:36 +0000514 // We're about to erase the entry, save the key so we can clean it up.
515 ValID Tmp = BBI->first;
516
Reid Spencerd0e8d382007-03-19 18:40:50 +0000517 // Erase the forward ref from the map as its no longer "forward"
518 CurFun.BBForwardRefs.erase(ID);
519
Reid Spencer6fb989c2007-03-20 01:13:36 +0000520 // The key has been removed from the map but so we don't want to leave
521 // strdup'd memory around so destroy it too.
522 Tmp.destroy();
523
Reid Spencerd0e8d382007-03-19 18:40:50 +0000524 // If its a numbered definition, bump the number and set the BB value.
525 if (ID.Type == ValID::LocalID) {
526 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
527 InsertValue(BB);
528 }
529
530 ID.destroy();
531 return BB;
532 }
533
534 // We haven't seen this BB before and its first mention is a definition.
535 // Just create it and return it.
536 std::string Name (ID.Type == ValID::LocalName ? ID.Name : "");
537 BB = new BasicBlock(Name, CurFun.CurrentFunction);
538 if (ID.Type == ValID::LocalID) {
539 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
540 InsertValue(BB);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000541 }
Reid Spencerd0e8d382007-03-19 18:40:50 +0000542
543 ID.destroy(); // Free strdup'd memory
544 return BB;
545}
546
547/// getBBVal - get an existing BB value or create a forward reference for it.
548///
549static BasicBlock *getBBVal(const ValID &ID) {
550 assert(inFunctionScope() && "Can't get basic block at global scope!");
551
552 BasicBlock *BB = 0;
553
554 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
555 if (BBI != CurFun.BBForwardRefs.end()) {
556 BB = BBI->second;
557 } if (ID.Type == ValID::LocalName) {
558 std::string Name = ID.Name;
559 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
560 if (N)
561 if (N->getType()->getTypeID() == Type::LabelTyID)
562 BB = cast<BasicBlock>(N);
563 else
564 GenerateError("Reference to label '" + Name + "' is actually of type '"+
565 N->getType()->getDescription() + "'");
566 } else if (ID.Type == ValID::LocalID) {
567 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
568 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
569 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
570 else
571 GenerateError("Reference to label '%" + utostr(ID.Num) +
572 "' is actually of type '"+
573 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
574 }
575 } else {
576 GenerateError("Illegal label reference " + ID.getName());
577 return 0;
578 }
579
580 // If its already been defined, return it now.
581 if (BB) {
582 ID.destroy(); // Free strdup'd memory.
583 return BB;
584 }
585
586 // Otherwise, this block has not been seen before, create it.
587 std::string Name;
588 if (ID.Type == ValID::LocalName)
589 Name = ID.Name;
590 BB = new BasicBlock(Name, CurFun.CurrentFunction);
591
592 // Insert it in the forward refs map.
593 CurFun.BBForwardRefs[ID] = BB;
594
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000595 return BB;
596}
597
598
599//===----------------------------------------------------------------------===//
600// Code to handle forward references in instructions
601//===----------------------------------------------------------------------===//
602//
603// This code handles the late binding needed with statements that reference
604// values not defined yet... for example, a forward branch, or the PHI node for
605// a loop body.
606//
607// This keeps a table (CurFun.LateResolveValues) of all such forward references
608// and back patchs after we are done.
609//
610
611// ResolveDefinitions - If we could not resolve some defs at parsing
612// time (forward branches, phi functions for loops, etc...) resolve the
613// defs now...
614//
615static void
Reid Spencerd0e8d382007-03-19 18:40:50 +0000616ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000617 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencerd0e8d382007-03-19 18:40:50 +0000618 while (!LateResolvers.empty()) {
619 Value *V = LateResolvers.back();
620 LateResolvers.pop_back();
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000621
Reid Spencerd0e8d382007-03-19 18:40:50 +0000622 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
623 CurModule.PlaceHolderInfo.find(V);
624 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000625
Reid Spencerd0e8d382007-03-19 18:40:50 +0000626 ValID &DID = PHI->second.first;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000627
Reid Spencerd0e8d382007-03-19 18:40:50 +0000628 Value *TheRealValue = getExistingVal(V->getType(), DID);
629 if (TriggerError)
630 return;
631 if (TheRealValue) {
632 V->replaceAllUsesWith(TheRealValue);
633 delete V;
634 CurModule.PlaceHolderInfo.erase(PHI);
635 } else if (FutureLateResolvers) {
636 // Functions have their unresolved items forwarded to the module late
637 // resolver table
638 InsertValue(V, *FutureLateResolvers);
639 } else {
640 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
641 GenerateError("Reference to an invalid definition: '" +DID.getName()+
642 "' of type '" + V->getType()->getDescription() + "'",
643 PHI->second.second);
Reid Spencer309080a2006-09-28 19:28:24 +0000644 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000645 } else {
Reid Spencerd0e8d382007-03-19 18:40:50 +0000646 GenerateError("Reference to an invalid definition: #" +
647 itostr(DID.Num) + " of type '" +
648 V->getType()->getDescription() + "'",
649 PHI->second.second);
650 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000651 }
652 }
653 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000654 LateResolvers.clear();
655}
656
657// ResolveTypeTo - A brand new type was just declared. This means that (if
658// name is not null) things referencing Name can be resolved. Otherwise, things
659// refering to the number can be resolved. Do this now.
660//
661static void ResolveTypeTo(char *Name, const Type *ToTy) {
662 ValID D;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000663 if (Name) D = ValID::createLocalName(Name);
664 else D = ValID::createLocalID(CurModule.Types.size());
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000665
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000666 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000667 CurModule.LateResolveTypes.find(D);
668 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000669 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000670 CurModule.LateResolveTypes.erase(I);
671 }
672}
673
674// setValueName - Set the specified value to the name given. The name may be
675// null potentially, in which case this is a noop. The string passed in is
676// assumed to be a malloc'd string buffer, and is free'd by this function.
677//
678static void setValueName(Value *V, char *NameStr) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000679 if (!NameStr) return;
680 std::string Name(NameStr); // Copy string
681 free(NameStr); // Free old string
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000682
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000683 if (V->getType() == Type::VoidTy) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000684 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000685 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000686 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000687
Reid Spencerac6bfc52007-02-05 17:04:00 +0000688 assert(inFunctionScope() && "Must be in function scope!");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000689 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
690 if (ST.lookup(Name)) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000691 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +0000692 V->getType()->getDescription() + "'");
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000693 return;
694 }
695
696 // Set the name.
697 V->setName(Name);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000698}
699
700/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
701/// this is a declaration, otherwise it is a definition.
702static GlobalVariable *
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000703ParseGlobalVariable(char *NameStr,
704 GlobalValue::LinkageTypes Linkage,
705 GlobalValue::VisibilityTypes Visibility,
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000706 bool isConstantGlobal, const Type *Ty,
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000707 Constant *Initializer, bool IsThreadLocal) {
Reid Spencer309080a2006-09-28 19:28:24 +0000708 if (isa<FunctionType>(Ty)) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000709 GenerateError("Cannot declare global vars of function type");
Reid Spencer309080a2006-09-28 19:28:24 +0000710 return 0;
711 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000712
713 const PointerType *PTy = PointerType::get(Ty);
714
715 std::string Name;
716 if (NameStr) {
717 Name = NameStr; // Copy string
718 free(NameStr); // Free old string
719 }
720
721 // See if this global value was forward referenced. If so, recycle the
722 // object.
723 ValID ID;
724 if (!Name.empty()) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000725 ID = ValID::createGlobalName((char*)Name.c_str());
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000726 } else {
Reid Spencerd0e8d382007-03-19 18:40:50 +0000727 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000728 }
729
730 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
731 // Move the global to the end of the list, from whereever it was
732 // previously inserted.
733 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
734 CurModule.CurrentModule->getGlobalList().remove(GV);
735 CurModule.CurrentModule->getGlobalList().push_back(GV);
736 GV->setInitializer(Initializer);
737 GV->setLinkage(Linkage);
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000738 GV->setVisibility(Visibility);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000739 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000740 GV->setThreadLocal(IsThreadLocal);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000741 InsertValue(GV, CurModule.Values);
742 return GV;
743 }
744
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000745 // If this global has a name
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000746 if (!Name.empty()) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000747 // if the global we're parsing has an initializer (is a definition) and
748 // has external linkage.
749 if (Initializer && Linkage != GlobalValue::InternalLinkage)
750 // If there is already a global with external linkage with this name
751 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
752 // If we allow this GVar to get created, it will be renamed in the
753 // symbol table because it conflicts with an existing GVar. We can't
754 // allow redefinition of GVars whose linking indicates that their name
755 // must stay the same. Issue the error.
756 GenerateError("Redefinition of global variable named '" + Name +
757 "' of type '" + Ty->getDescription() + "'");
758 return 0;
759 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000760 }
761
762 // Otherwise there is no existing GV to use, create one now.
763 GlobalVariable *GV =
764 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000765 CurModule.CurrentModule, IsThreadLocal);
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000766 GV->setVisibility(Visibility);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000767 InsertValue(GV, CurModule.Values);
768 return GV;
769}
770
771// setTypeName - Set the specified type to the name given. The name may be
772// null potentially, in which case this is a noop. The string passed in is
773// assumed to be a malloc'd string buffer, and is freed by this function.
774//
775// This function returns true if the type has already been defined, but is
776// allowed to be redefined in the specified context. If the name is a new name
777// for the type plane, it is inserted and false is returned.
778static bool setTypeName(const Type *T, char *NameStr) {
Reid Spencerac6bfc52007-02-05 17:04:00 +0000779 assert(!inFunctionScope() && "Can't give types function-local names!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000780 if (NameStr == 0) return false;
781
782 std::string Name(NameStr); // Copy string
783 free(NameStr); // Free old string
784
785 // We don't allow assigning names to void type
Reid Spencer309080a2006-09-28 19:28:24 +0000786 if (T == Type::VoidTy) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000787 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer309080a2006-09-28 19:28:24 +0000788 return false;
789 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000790
791 // Set the type name, checking for conflicts as we do so.
792 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
793
794 if (AlreadyExists) { // Inserting a name that is already defined???
795 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencerac6bfc52007-02-05 17:04:00 +0000796 assert(Existing && "Conflict but no matching type?!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000797
798 // There is only one case where this is allowed: when we are refining an
799 // opaque type. In this case, Existing will be an opaque type.
800 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
801 // We ARE replacing an opaque type!
802 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
803 return true;
804 }
805
806 // Otherwise, this is an attempt to redefine a type. That's okay if
807 // the redefinition is identical to the original. This will be so if
808 // Existing and T point to the same Type object. In this one case we
809 // allow the equivalent redefinition.
810 if (Existing == T) return true; // Yes, it's equal.
811
812 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer33259082007-01-05 21:51:07 +0000813 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +0000814 T->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000815 }
816
817 return false;
818}
819
820//===----------------------------------------------------------------------===//
821// Code for handling upreferences in type names...
822//
823
824// TypeContains - Returns true if Ty directly contains E in it.
825//
826static bool TypeContains(const Type *Ty, const Type *E) {
827 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
828 E) != Ty->subtype_end();
829}
830
831namespace {
832 struct UpRefRecord {
833 // NestingLevel - The number of nesting levels that need to be popped before
834 // this type is resolved.
835 unsigned NestingLevel;
836
837 // LastContainedTy - This is the type at the current binding level for the
838 // type. Every time we reduce the nesting level, this gets updated.
839 const Type *LastContainedTy;
840
841 // UpRefTy - This is the actual opaque type that the upreference is
842 // represented with.
843 OpaqueType *UpRefTy;
844
845 UpRefRecord(unsigned NL, OpaqueType *URTy)
846 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
847 };
848}
849
850// UpRefs - A list of the outstanding upreferences that need to be resolved.
851static std::vector<UpRefRecord> UpRefs;
852
853/// HandleUpRefs - Every time we finish a new layer of types, this function is
854/// called. It loops through the UpRefs vector, which is a list of the
855/// currently active types. For each type, if the up reference is contained in
856/// the newly completed type, we decrement the level count. When the level
857/// count reaches zero, the upreferenced type is the type that is passed in:
858/// thus we can complete the cycle.
859///
860static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner680aab62006-08-18 17:34:45 +0000861 // If Ty isn't abstract, or if there are no up-references in it, then there is
862 // nothing to resolve here.
863 if (!ty->isAbstract() || UpRefs.empty()) return ty;
864
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000865 PATypeHolder Ty(ty);
866 UR_OUT("Type '" << Ty->getDescription() <<
867 "' newly formed. Resolving upreferences.\n" <<
868 UpRefs.size() << " upreferences active!\n");
869
870 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
871 // to zero), we resolve them all together before we resolve them to Ty. At
872 // the end of the loop, if there is anything to resolve to Ty, it will be in
873 // this variable.
874 OpaqueType *TypeToResolve = 0;
875
876 for (unsigned i = 0; i != UpRefs.size(); ++i) {
877 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
878 << UpRefs[i].second->getDescription() << ") = "
879 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
880 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
881 // Decrement level of upreference
882 unsigned Level = --UpRefs[i].NestingLevel;
883 UpRefs[i].LastContainedTy = Ty;
884 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
885 if (Level == 0) { // Upreference should be resolved!
886 if (!TypeToResolve) {
887 TypeToResolve = UpRefs[i].UpRefTy;
888 } else {
889 UR_OUT(" * Resolving upreference for "
890 << UpRefs[i].second->getDescription() << "\n";
891 std::string OldName = UpRefs[i].UpRefTy->getDescription());
892 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
893 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
894 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
895 }
896 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
897 --i; // Do not skip the next element...
898 }
899 }
900 }
901
902 if (TypeToResolve) {
903 UR_OUT(" * Resolving upreference for "
904 << UpRefs[i].second->getDescription() << "\n";
905 std::string OldName = TypeToResolve->getDescription());
906 TypeToResolve->refineAbstractTypeTo(Ty);
907 }
908
909 return Ty;
910}
911
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000912//===----------------------------------------------------------------------===//
913// RunVMAsmParser - Define an interface to this parser
914//===----------------------------------------------------------------------===//
915//
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000916static Module* RunParser(Module * M);
917
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000918Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
919 set_scan_file(F);
920
921 CurFilename = Filename;
922 return RunParser(new Module(CurFilename));
923}
924
925Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
926 set_scan_string(AsmString);
927
928 CurFilename = "from_memory";
929 if (M == NULL) {
930 return RunParser(new Module (CurFilename));
931 } else {
932 return RunParser(M);
933 }
934}
935
936%}
937
938%union {
939 llvm::Module *ModuleVal;
940 llvm::Function *FunctionVal;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000941 llvm::BasicBlock *BasicBlockVal;
942 llvm::TerminatorInst *TermInstVal;
943 llvm::Instruction *InstVal;
Reid Spencere2c32da2006-12-03 05:46:11 +0000944 llvm::Constant *ConstVal;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000945
Reid Spencere2c32da2006-12-03 05:46:11 +0000946 const llvm::Type *PrimType;
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000947 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencere2c32da2006-12-03 05:46:11 +0000948 llvm::PATypeHolder *TypeVal;
949 llvm::Value *ValueVal;
Reid Spencere2c32da2006-12-03 05:46:11 +0000950 std::vector<llvm::Value*> *ValueList;
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000951 llvm::ArgListType *ArgList;
952 llvm::TypeWithAttrs TypeWithAttrs;
953 llvm::TypeWithAttrsList *TypeWithAttrsList;
954 llvm::ValueRefList *ValueRefList;
955
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000956 // Represent the RHS of PHI node
Reid Spencere2c32da2006-12-03 05:46:11 +0000957 std::list<std::pair<llvm::Value*,
958 llvm::BasicBlock*> > *PHIList;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000959 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencere2c32da2006-12-03 05:46:11 +0000960 std::vector<llvm::Constant*> *ConstVector;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000961
962 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000963 llvm::GlobalValue::VisibilityTypes Visibility;
Reid Spencerf51a7052007-04-09 06:16:21 +0000964 uint16_t ParamAttrs;
Reid Spencerc7a686b2007-02-28 02:24:54 +0000965 llvm::APInt *APIntVal;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000966 int64_t SInt64Val;
967 uint64_t UInt64Val;
968 int SIntVal;
969 unsigned UIntVal;
970 double FPVal;
971 bool BoolVal;
972
973 char *StrVal; // This memory is strdup'd!
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000974 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000975
Reid Spencere2c32da2006-12-03 05:46:11 +0000976 llvm::Instruction::BinaryOps BinaryOpVal;
977 llvm::Instruction::TermOps TermOpVal;
978 llvm::Instruction::MemoryOps MemOpVal;
979 llvm::Instruction::CastOps CastOpVal;
980 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencere2c32da2006-12-03 05:46:11 +0000981 llvm::ICmpInst::Predicate IPredicate;
982 llvm::FCmpInst::Predicate FPredicate;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000983}
984
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000985%type <ModuleVal> Module
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000986%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
987%type <BasicBlockVal> BasicBlock InstructionList
988%type <TermInstVal> BBTerminatorInst
989%type <InstVal> Inst InstVal MemoryInst
990%type <ConstVal> ConstVal ConstExpr
991%type <ConstVector> ConstVector
992%type <ArgList> ArgList ArgListH
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000993%type <PHIList> PHIList
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000994%type <ValueRefList> ValueRefList // For call param lists & GEP indices
995%type <ValueList> IndexList // For GEP indices
996%type <TypeList> TypeListI
997%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencerbf48e3c2007-01-05 17:07:23 +0000998%type <TypeWithAttrs> ArgType
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000999%type <JumpTable> JumpTable
1000%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00001001%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001002%type <BoolVal> OptVolatile // 'volatile' or not
1003%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1004%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001005%type <Linkage> GVInternalLinkage GVExternalLinkage
1006%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00001007%type <Linkage> AliasLinkage
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001008%type <Visibility> GVVisibilityStyle
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001009
1010// ValueRef - Unresolved reference to a definition or BB
1011%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1012%type <ValueVal> ResolvedVal // <type> <valref> pair
1013// Tokens and types for handling constant integer values
1014//
1015// ESINT64VAL - A negative number within long long range
1016%token <SInt64Val> ESINT64VAL
1017
1018// EUINT64VAL - A positive number within uns. long long range
1019%token <UInt64Val> EUINT64VAL
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001020
Reid Spencerc7a686b2007-02-28 02:24:54 +00001021// ESAPINTVAL - A negative number with arbitrary precision
1022%token <APIntVal> ESAPINTVAL
1023
1024// EUAPINTVAL - A positive number with arbitrary precision
1025%token <APIntVal> EUAPINTVAL
1026
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001027%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001028%token <FPVal> FPVAL // Float or Double constant
1029
1030// Built in types...
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001031%type <TypeVal> Types ResultTypes
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001032%type <PrimType> IntType FPType PrimType // Classifications
Reid Spencer58a8db02007-01-13 05:00:46 +00001033%token <PrimType> VOID INTTYPE
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001034%token <PrimType> FLOAT DOUBLE LABEL
1035%token TYPE
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001036
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001037%token<StrVal> LOCALVAR GLOBALVAR LABELSTR STRINGCONSTANT ATSTRINGCONSTANT
1038%type <StrVal> LocalName OptLocalName OptLocalAssign
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00001039%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001040%type <UIntVal> OptAlign OptCAlign
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001041%type <StrVal> OptSection SectionString
1042
Reid Spencer7ae03fc2007-04-09 01:56:05 +00001043%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00001044%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001045%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001046%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001047%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001048%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001049%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattner7d1d0342006-10-22 06:08:13 +00001050%token DATALAYOUT
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001051%type <UIntVal> OptCallingConv
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001052%type <ParamAttrs> OptParamAttrs ParamAttr
1053%type <ParamAttrs> OptFuncAttrs FuncAttr
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001054
1055// Basic Block Terminating Operators
1056%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
1057
1058// Binary Operators
Reid Spencer266e42b2006-12-23 06:05:41 +00001059%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencerde46e482006-11-02 20:25:50 +00001060%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer2341c222007-02-02 02:16:23 +00001061%token <BinaryOpVal> SHL LSHR ASHR
1062
Reid Spencere2c32da2006-12-03 05:46:11 +00001063%token <OtherOpVal> ICMP FCMP
Reid Spencere2c32da2006-12-03 05:46:11 +00001064%type <IPredicate> IPredicates
Reid Spencere2c32da2006-12-03 05:46:11 +00001065%type <FPredicate> FPredicates
Reid Spencer1960ef32006-12-03 06:59:29 +00001066%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1067%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001068
1069// Memory Instructions
1070%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1071
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001072// Cast Operators
1073%type <CastOpVal> CastOps
1074%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1075%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1076
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001077// Other Operators
Reid Spencer2341c222007-02-02 02:16:23 +00001078%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattner9ff96a72006-04-08 01:18:56 +00001079%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001080
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001081// Function Attributes
Reid Spencer32096782007-03-22 02:14:08 +00001082%token NORETURN INREG SRET NOUNWIND
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001083
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001084// Visibility Styles
1085%token DEFAULT HIDDEN
1086
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001087%start Module
1088%%
1089
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001090
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001091// Operations that are notably excluded from this list include:
1092// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1093//
Reid Spencerde46e482006-11-02 20:25:50 +00001094ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer2341c222007-02-02 02:16:23 +00001095LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001096CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1097 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer2341c222007-02-02 02:16:23 +00001098
Reid Spencer1960ef32006-12-03 06:59:29 +00001099IPredicates
Reid Spencerd2e0c342006-12-04 05:24:24 +00001100 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer1960ef32006-12-03 06:59:29 +00001101 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1102 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1103 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1104 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1105 ;
1106
1107FPredicates
1108 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1109 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1110 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1111 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1112 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1113 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1114 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1115 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1116 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1117 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001118
1119// These are some types that allow classification if we only want a particular
1120// thing... for example, only a signed, unsigned, or integral type.
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001121IntType : INTTYPE;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001122FPType : FLOAT | DOUBLE;
1123
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001124LocalName : LOCALVAR | STRINGCONSTANT;
1125OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1126
1127/// OptLocalAssign - Value producing statements have an optional assignment
1128/// component.
1129OptLocalAssign : LocalName '=' {
1130 $$ = $1;
1131 CHECK_FOR_ERROR
1132 }
1133 | /*empty*/ {
1134 $$ = 0;
1135 CHECK_FOR_ERROR
1136 };
1137
1138GlobalName : GLOBALVAR | ATSTRINGCONSTANT;
1139
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00001140OptGlobalAssign : GlobalAssign
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001141 | /*empty*/ {
1142 $$ = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00001143 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001144 };
1145
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00001146GlobalAssign : GlobalName '=' {
1147 $$ = $1;
1148 CHECK_FOR_ERROR
1149 }
1150
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001151GVInternalLinkage
1152 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1153 | WEAK { $$ = GlobalValue::WeakLinkage; }
1154 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1155 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1156 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1157 ;
1158
1159GVExternalLinkage
1160 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1161 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1162 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1163 ;
1164
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001165GVVisibilityStyle
1166 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00001167 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001168 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1169 ;
1170
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001171FunctionDeclareLinkage
1172 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1173 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1174 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001175 ;
1176
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00001177FunctionDefineLinkage
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001178 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1179 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001180 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1181 | WEAK { $$ = GlobalValue::WeakLinkage; }
1182 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001183 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001184
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00001185AliasLinkage
1186 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1187 | WEAK { $$ = GlobalValue::WeakLinkage; }
1188 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1189 ;
1190
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +00001191OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1192 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +00001193 FASTCC_TOK { $$ = CallingConv::Fast; } |
1194 COLDCC_TOK { $$ = CallingConv::Cold; } |
1195 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1196 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1197 CC_TOK EUINT64VAL {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001198 if ((unsigned)$2 != $2)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001199 GEN_ERROR("Calling conv too large");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001200 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001201 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001202 };
1203
Reid Spencera472f662007-04-11 02:44:20 +00001204ParamAttr : ZEXT { $$ = ParamAttr::ZExt; }
1205 | SEXT { $$ = ParamAttr::SExt; }
1206 | INREG { $$ = ParamAttr::InReg; }
1207 | SRET { $$ = ParamAttr::StructRet; }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001208 ;
1209
Reid Spencera472f662007-04-11 02:44:20 +00001210OptParamAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001211 | OptParamAttrs ParamAttr {
Reid Spencerf51a7052007-04-09 06:16:21 +00001212 $$ = $1 | $2;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001213 }
1214 ;
1215
Reid Spencera472f662007-04-11 02:44:20 +00001216FuncAttr : NORETURN { $$ = ParamAttr::NoReturn; }
1217 | NOUNWIND { $$ = ParamAttr::NoUnwind; }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001218 | ParamAttr
1219 ;
1220
Reid Spencera472f662007-04-11 02:44:20 +00001221OptFuncAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001222 | OptFuncAttrs FuncAttr {
Reid Spencerf51a7052007-04-09 06:16:21 +00001223 $$ = $1 | $2;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001224 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001225 ;
1226
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001227// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1228// a comma before it.
1229OptAlign : /*empty*/ { $$ = 0; } |
1230 ALIGN EUINT64VAL {
1231 $$ = $2;
1232 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001233 GEN_ERROR("Alignment must be a power of two");
Reid Spencer713eedc2006-08-18 08:43:06 +00001234 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001235};
1236OptCAlign : /*empty*/ { $$ = 0; } |
1237 ',' ALIGN EUINT64VAL {
1238 $$ = $3;
1239 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001240 GEN_ERROR("Alignment must be a power of two");
Reid Spencer713eedc2006-08-18 08:43:06 +00001241 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001242};
1243
1244
1245SectionString : SECTION STRINGCONSTANT {
1246 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1247 if ($2[i] == '"' || $2[i] == '\\')
Reid Spencera7bb3e92007-02-05 10:18:06 +00001248 GEN_ERROR("Invalid character in section name");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001249 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001250 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001251};
1252
1253OptSection : /*empty*/ { $$ = 0; } |
1254 SectionString { $$ = $1; };
1255
1256// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1257// is set to be the global we are processing.
1258//
1259GlobalVarAttributes : /* empty */ {} |
1260 ',' GlobalVarAttribute GlobalVarAttributes {};
1261GlobalVarAttribute : SectionString {
1262 CurGV->setSection($1);
1263 free($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001264 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001265 }
1266 | ALIGN EUINT64VAL {
1267 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001268 GEN_ERROR("Alignment must be a power of two");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001269 CurGV->setAlignment($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001270 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001271 };
1272
1273//===----------------------------------------------------------------------===//
1274// Types includes all predefined types... except void, because it can only be
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001275// used in specific contexts (function returning void for example).
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001276
1277// Derived types are added later...
1278//
Reid Spencer58a8db02007-01-13 05:00:46 +00001279PrimType : INTTYPE | FLOAT | DOUBLE | LABEL ;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001280
1281Types
1282 : OPAQUE {
Reid Spencere2c32da2006-12-03 05:46:11 +00001283 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer713eedc2006-08-18 08:43:06 +00001284 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001285 }
1286 | PrimType {
Reid Spencere2c32da2006-12-03 05:46:11 +00001287 $$ = new PATypeHolder($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001288 CHECK_FOR_ERROR
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001289 }
1290 | Types '*' { // Pointer type?
1291 if (*$1 == Type::LabelTy)
1292 GEN_ERROR("Cannot form a pointer to a basic block");
1293 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1294 delete $1;
1295 CHECK_FOR_ERROR
1296 }
1297 | SymbolicValueRef { // Named types are also simple types...
1298 const Type* tmp = getTypeVal($1);
1299 CHECK_FOR_ERROR
1300 $$ = new PATypeHolder(tmp);
1301 }
1302 | '\\' EUINT64VAL { // Type UpReference
Reid Spencera7bb3e92007-02-05 10:18:06 +00001303 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001304 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1305 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencere2c32da2006-12-03 05:46:11 +00001306 $$ = new PATypeHolder(OT);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001307 UR_OUT("New Upreference!\n");
Reid Spencer713eedc2006-08-18 08:43:06 +00001308 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001309 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001310 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001311 std::vector<const Type*> Params;
Christopher Lamb3f706f22007-04-22 20:09:11 +00001312 ParamAttrsVector Attrs;
1313 if ($5 != ParamAttr::None) {
1314 ParamAttrsWithIndex X; X.index = 0; X.attrs = $5;
1315 Attrs.push_back(X);
1316 }
Reid Spencerf51a7052007-04-09 06:16:21 +00001317 unsigned index = 1;
1318 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
1319 for (; I != E; ++I, ++index) {
Reid Spencer6fb989c2007-03-20 01:13:36 +00001320 const Type *Ty = I->Ty->get();
Reid Spencer6fb989c2007-03-20 01:13:36 +00001321 Params.push_back(Ty);
1322 if (Ty != Type::VoidTy)
Christopher Lamb3f706f22007-04-22 20:09:11 +00001323 if (I->Attrs != ParamAttr::None) {
1324 ParamAttrsWithIndex X; X.index = index; X.attrs = I->Attrs;
1325 Attrs.push_back(X);
1326 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001327 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001328 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1329 if (isVarArg) Params.pop_back();
1330
Reid Spencerf51a7052007-04-09 06:16:21 +00001331 ParamAttrsList *ActualAttrs = 0;
1332 if (!Attrs.empty())
Christopher Lamb3f706f22007-04-22 20:09:11 +00001333 ActualAttrs = ParamAttrsList::get(Attrs);
Reid Spencerf51a7052007-04-09 06:16:21 +00001334 FunctionType *FT = FunctionType::get(*$1, Params, isVarArg, ActualAttrs);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001335 delete $3; // Delete the argument list
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001336 delete $1; // Delete the return type handle
1337 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer713eedc2006-08-18 08:43:06 +00001338 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001339 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001340 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001341 std::vector<const Type*> Params;
Christopher Lamb3f706f22007-04-22 20:09:11 +00001342 ParamAttrsVector Attrs;
1343 if ($5 != ParamAttr::None) {
1344 ParamAttrsWithIndex X; X.index = 0; X.attrs = $5;
1345 Attrs.push_back(X);
1346 }
Reid Spencerf51a7052007-04-09 06:16:21 +00001347 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
1348 unsigned index = 1;
1349 for ( ; I != E; ++I, ++index) {
Reid Spencer6fb989c2007-03-20 01:13:36 +00001350 const Type* Ty = I->Ty->get();
Reid Spencer6fb989c2007-03-20 01:13:36 +00001351 Params.push_back(Ty);
1352 if (Ty != Type::VoidTy)
Christopher Lamb3f706f22007-04-22 20:09:11 +00001353 if (I->Attrs != ParamAttr::None) {
1354 ParamAttrsWithIndex X; X.index = index; X.attrs = I->Attrs;
1355 Attrs.push_back(X);
1356 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001357 }
1358 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1359 if (isVarArg) Params.pop_back();
1360
Reid Spencerf51a7052007-04-09 06:16:21 +00001361 ParamAttrsList *ActualAttrs = 0;
1362 if (!Attrs.empty())
Christopher Lamb3f706f22007-04-22 20:09:11 +00001363 ActualAttrs = ParamAttrsList::get(Attrs);
Reid Spencerf51a7052007-04-09 06:16:21 +00001364
1365 FunctionType *FT = FunctionType::get($1, Params, isVarArg, ActualAttrs);
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001366 delete $3; // Delete the argument list
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001367 $$ = new PATypeHolder(HandleUpRefs(FT));
1368 CHECK_FOR_ERROR
1369 }
1370
1371 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Reid Spencere2c32da2006-12-03 05:46:11 +00001372 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1373 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00001374 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001375 }
Chris Lattner4669b0b2007-02-19 07:44:24 +00001376 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencere2c32da2006-12-03 05:46:11 +00001377 const llvm::Type* ElemTy = $4->get();
1378 if ((unsigned)$2 != $2)
1379 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner03c49532007-01-15 02:27:26 +00001380 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencerd84d35b2007-02-15 02:26:10 +00001381 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencere2c32da2006-12-03 05:46:11 +00001382 if (!isPowerOf2_32($2))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001383 GEN_ERROR("Vector length should be a power of 2");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001384 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencere2c32da2006-12-03 05:46:11 +00001385 delete $4;
1386 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001387 }
1388 | '{' TypeListI '}' { // Structure type?
1389 std::vector<const Type*> Elements;
Reid Spencere2c32da2006-12-03 05:46:11 +00001390 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001391 E = $2->end(); I != E; ++I)
Reid Spencere2c32da2006-12-03 05:46:11 +00001392 Elements.push_back(*I);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001393
Reid Spencere2c32da2006-12-03 05:46:11 +00001394 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001395 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001396 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001397 }
1398 | '{' '}' { // Empty structure type?
Reid Spencere2c32da2006-12-03 05:46:11 +00001399 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer713eedc2006-08-18 08:43:06 +00001400 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001401 }
Andrew Lenharth2d189ff2006-12-08 18:07:09 +00001402 | '<' '{' TypeListI '}' '>' {
1403 std::vector<const Type*> Elements;
1404 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1405 E = $3->end(); I != E; ++I)
1406 Elements.push_back(*I);
1407
1408 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1409 delete $3;
1410 CHECK_FOR_ERROR
1411 }
1412 | '<' '{' '}' '>' { // Empty structure type?
1413 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1414 CHECK_FOR_ERROR
1415 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001416 ;
1417
1418ArgType
1419 : Types OptParamAttrs {
1420 $$.Ty = $1;
1421 $$.Attrs = $2;
1422 }
1423 ;
1424
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001425ResultTypes
1426 : Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001427 if (!UpRefs.empty())
1428 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1429 if (!(*$1)->isFirstClassType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001430 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001431 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001432 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001433 | VOID {
1434 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001435 }
1436 ;
1437
1438ArgTypeList : ArgType {
1439 $$ = new TypeWithAttrsList();
1440 $$->push_back($1);
1441 CHECK_FOR_ERROR
1442 }
1443 | ArgTypeList ',' ArgType {
1444 ($$=$1)->push_back($3);
1445 CHECK_FOR_ERROR
1446 }
1447 ;
1448
1449ArgTypeListI
1450 : ArgTypeList
1451 | ArgTypeList ',' DOTDOTDOT {
1452 $$=$1;
Reid Spencera472f662007-04-11 02:44:20 +00001453 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001454 TWA.Ty = new PATypeHolder(Type::VoidTy);
1455 $$->push_back(TWA);
1456 CHECK_FOR_ERROR
1457 }
1458 | DOTDOTDOT {
1459 $$ = new TypeWithAttrsList;
Reid Spencera472f662007-04-11 02:44:20 +00001460 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001461 TWA.Ty = new PATypeHolder(Type::VoidTy);
1462 $$->push_back(TWA);
1463 CHECK_FOR_ERROR
1464 }
1465 | /*empty*/ {
1466 $$ = new TypeWithAttrsList();
Reid Spencer713eedc2006-08-18 08:43:06 +00001467 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001468 };
1469
1470// TypeList - Used for struct declarations and as a basis for function type
1471// declaration type lists
1472//
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001473TypeListI : Types {
Reid Spencere2c32da2006-12-03 05:46:11 +00001474 $$ = new std::list<PATypeHolder>();
Reid Spencer6fb989c2007-03-20 01:13:36 +00001475 $$->push_back(*$1);
1476 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001477 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001478 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001479 | TypeListI ',' Types {
Reid Spencer6fb989c2007-03-20 01:13:36 +00001480 ($$=$1)->push_back(*$3);
1481 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001482 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001483 };
1484
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001485// ConstVal - The various declarations that go into the constant pool. This
1486// production is used ONLY to represent constants that show up AFTER a 'const',
1487// 'constant' or 'global' token at global scope. Constants that can be inlined
1488// into other expressions (such as integers and constexprs) are handled by the
1489// ResolvedVal, ValueRef and ConstValueRef productions.
1490//
1491ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001492 if (!UpRefs.empty())
1493 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001494 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001495 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001496 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001497 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001498 const Type *ETy = ATy->getElementType();
1499 int NumElements = ATy->getNumElements();
1500
1501 // Verify that we have the correct size...
1502 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer713eedc2006-08-18 08:43:06 +00001503 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001504 utostr($3->size()) + " arguments, but has size of " +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001505 itostr(NumElements) + "");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001506
1507 // Verify all elements are correct type!
1508 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencere2c32da2006-12-03 05:46:11 +00001509 if (ETy != (*$3)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001510 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001511 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencere2c32da2006-12-03 05:46:11 +00001512 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001513 }
1514
Reid Spencere2c32da2006-12-03 05:46:11 +00001515 $$ = ConstantArray::get(ATy, *$3);
1516 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001517 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001518 }
1519 | Types '[' ']' {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001520 if (!UpRefs.empty())
1521 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001522 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001523 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001524 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001525 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001526
1527 int NumElements = ATy->getNumElements();
1528 if (NumElements != -1 && NumElements != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001529 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencera7bb3e92007-02-05 10:18:06 +00001530 " arguments, but has size of " + itostr(NumElements) +"");
Reid Spencere2c32da2006-12-03 05:46:11 +00001531 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1532 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001533 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001534 }
1535 | Types 'c' STRINGCONSTANT {
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 const Type *ETy = ATy->getElementType();
1545 char *EndStr = UnEscapeLexed($3, true);
1546 if (NumElements != -1 && NumElements != (EndStr-$3))
Reid Spencer713eedc2006-08-18 08:43:06 +00001547 GEN_ERROR("Can't build string constant of size " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001548 itostr((int)(EndStr-$3)) +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001549 " when array has size " + itostr(NumElements) + "");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001550 std::vector<Constant*> Vals;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001551 if (ETy == Type::Int8Ty) {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001552 for (unsigned char *C = (unsigned char *)$3;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001553 C != (unsigned char*)EndStr; ++C)
1554 Vals.push_back(ConstantInt::get(ETy, *C));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001555 } else {
1556 free($3);
Reid Spencera7bb3e92007-02-05 10:18:06 +00001557 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001558 }
1559 free($3);
Reid Spencere2c32da2006-12-03 05:46:11 +00001560 $$ = ConstantArray::get(ATy, Vals);
1561 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001562 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001563 }
1564 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001565 if (!UpRefs.empty())
1566 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001567 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001568 if (PTy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001569 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001570 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001571 const Type *ETy = PTy->getElementType();
1572 int NumElements = PTy->getNumElements();
1573
1574 // Verify that we have the correct size...
1575 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer713eedc2006-08-18 08:43:06 +00001576 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001577 utostr($3->size()) + " arguments, but has size of " +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001578 itostr(NumElements) + "");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001579
1580 // Verify all elements are correct type!
1581 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencere2c32da2006-12-03 05:46:11 +00001582 if (ETy != (*$3)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001583 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001584 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencere2c32da2006-12-03 05:46:11 +00001585 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001586 }
1587
Reid Spencerd84d35b2007-02-15 02:26:10 +00001588 $$ = ConstantVector::get(PTy, *$3);
Reid Spencere2c32da2006-12-03 05:46:11 +00001589 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001590 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001591 }
1592 | Types '{' ConstVector '}' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001593 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001594 if (STy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001595 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001596 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001597
1598 if ($3->size() != STy->getNumContainedTypes())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001599 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001600
1601 // Check to ensure that constants are compatible with the type initializer!
1602 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencere2c32da2006-12-03 05:46:11 +00001603 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer713eedc2006-08-18 08:43:06 +00001604 GEN_ERROR("Expected type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001605 STy->getElementType(i)->getDescription() +
1606 "' for element #" + utostr(i) +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001607 " of structure initializer");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001608
Zhou Sheng75b871f2007-01-11 12:24:14 +00001609 // Check to ensure that Type is not packed
1610 if (STy->isPacked())
Chris Lattner4669b0b2007-02-19 07:44:24 +00001611 GEN_ERROR("Unpacked Initializer to vector type '" + STy->getDescription() + "'");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001612
Reid Spencere2c32da2006-12-03 05:46:11 +00001613 $$ = ConstantStruct::get(STy, *$3);
1614 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001615 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001616 }
1617 | Types '{' '}' {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001618 if (!UpRefs.empty())
1619 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001620 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001621 if (STy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001622 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001623 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001624
1625 if (STy->getNumContainedTypes() != 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001626 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001627
Zhou Sheng75b871f2007-01-11 12:24:14 +00001628 // Check to ensure that Type is not packed
1629 if (STy->isPacked())
Chris Lattner4669b0b2007-02-19 07:44:24 +00001630 GEN_ERROR("Unpacked Initializer to vector type '" + STy->getDescription() + "'");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001631
1632 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1633 delete $1;
1634 CHECK_FOR_ERROR
1635 }
1636 | Types '<' '{' ConstVector '}' '>' {
1637 const StructType *STy = dyn_cast<StructType>($1->get());
1638 if (STy == 0)
1639 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001640 (*$1)->getDescription() + "'");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001641
1642 if ($4->size() != STy->getNumContainedTypes())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001643 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001644
1645 // Check to ensure that constants are compatible with the type initializer!
1646 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1647 if ((*$4)[i]->getType() != STy->getElementType(i))
1648 GEN_ERROR("Expected type '" +
1649 STy->getElementType(i)->getDescription() +
1650 "' for element #" + utostr(i) +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001651 " of structure initializer");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001652
1653 // Check to ensure that Type is packed
1654 if (!STy->isPacked())
Chris Lattner4669b0b2007-02-19 07:44:24 +00001655 GEN_ERROR("Vector initializer to non-vector type '" +
1656 STy->getDescription() + "'");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001657
1658 $$ = ConstantStruct::get(STy, *$4);
1659 delete $1; delete $4;
1660 CHECK_FOR_ERROR
1661 }
1662 | Types '<' '{' '}' '>' {
1663 if (!UpRefs.empty())
1664 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1665 const StructType *STy = dyn_cast<StructType>($1->get());
1666 if (STy == 0)
1667 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001668 (*$1)->getDescription() + "'");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001669
1670 if (STy->getNumContainedTypes() != 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001671 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001672
1673 // Check to ensure that Type is packed
1674 if (!STy->isPacked())
Chris Lattner4669b0b2007-02-19 07:44:24 +00001675 GEN_ERROR("Vector initializer to non-vector type '" +
1676 STy->getDescription() + "'");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001677
Reid Spencere2c32da2006-12-03 05:46:11 +00001678 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1679 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001680 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001681 }
1682 | Types NULL_TOK {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001683 if (!UpRefs.empty())
1684 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001685 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001686 if (PTy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001687 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001688 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001689
Reid Spencere2c32da2006-12-03 05:46:11 +00001690 $$ = ConstantPointerNull::get(PTy);
1691 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001692 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001693 }
1694 | Types UNDEF {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001695 if (!UpRefs.empty())
1696 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001697 $$ = UndefValue::get($1->get());
1698 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001699 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001700 }
1701 | Types SymbolicValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001702 if (!UpRefs.empty())
1703 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001704 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001705 if (Ty == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001706 GEN_ERROR("Global const reference must be a pointer type");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001707
1708 // ConstExprs can exist in the body of a function, thus creating
1709 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencerd0e8d382007-03-19 18:40:50 +00001710 // the context of a function, getExistingVal will search the functions
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001711 // symbol table instead of the module symbol table for the global symbol,
1712 // which throws things all off. To get around this, we just tell
Reid Spencerd0e8d382007-03-19 18:40:50 +00001713 // getExistingVal that we are at global scope here.
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001714 //
1715 Function *SavedCurFn = CurFun.CurrentFunction;
1716 CurFun.CurrentFunction = 0;
1717
Reid Spencerd0e8d382007-03-19 18:40:50 +00001718 Value *V = getExistingVal(Ty, $2);
Reid Spencer309080a2006-09-28 19:28:24 +00001719 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001720
1721 CurFun.CurrentFunction = SavedCurFn;
1722
1723 // If this is an initializer for a constant pointer, which is referencing a
1724 // (currently) undefined variable, create a stub now that shall be replaced
1725 // in the future with the right type of variable.
1726 //
1727 if (V == 0) {
Reid Spencerac6bfc52007-02-05 17:04:00 +00001728 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001729 const PointerType *PT = cast<PointerType>(Ty);
1730
1731 // First check to see if the forward references value is already created!
1732 PerModuleInfo::GlobalRefsType::iterator I =
1733 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1734
1735 if (I != CurModule.GlobalRefs.end()) {
1736 V = I->second; // Placeholder already exists, use it...
1737 $2.destroy();
1738 } else {
1739 std::string Name;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001740 if ($2.Type == ValID::GlobalName)
1741 Name = $2.Name;
1742 else if ($2.Type != ValID::GlobalID)
1743 GEN_ERROR("Invalid reference to global");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001744
1745 // Create the forward referenced global.
1746 GlobalValue *GV;
1747 if (const FunctionType *FTy =
1748 dyn_cast<FunctionType>(PT->getElementType())) {
1749 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1750 CurModule.CurrentModule);
1751 } else {
1752 GV = new GlobalVariable(PT->getElementType(), false,
1753 GlobalValue::ExternalLinkage, 0,
1754 Name, CurModule.CurrentModule);
1755 }
1756
1757 // Keep track of the fact that we have a forward ref to recycle it
1758 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1759 V = GV;
1760 }
1761 }
1762
Reid Spencere2c32da2006-12-03 05:46:11 +00001763 $$ = cast<GlobalValue>(V);
1764 delete $1; // Free the type handle
Reid Spencer713eedc2006-08-18 08:43:06 +00001765 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001766 }
1767 | Types ConstExpr {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001768 if (!UpRefs.empty())
1769 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001770 if ($1->get() != $2->getType())
Reid Spencerca3d1cb2007-01-04 00:06:14 +00001771 GEN_ERROR("Mismatched types for constant expression: " +
1772 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001773 $$ = $2;
Reid Spencere2c32da2006-12-03 05:46:11 +00001774 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001775 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001776 }
1777 | Types ZEROINITIALIZER {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001778 if (!UpRefs.empty())
1779 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001780 const Type *Ty = $1->get();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001781 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001782 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencere2c32da2006-12-03 05:46:11 +00001783 $$ = Constant::getNullValue(Ty);
1784 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001785 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00001786 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001787 | IntType ESINT64VAL { // integral constants
Reid Spencer266e42b2006-12-23 06:05:41 +00001788 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001789 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencera7bed60a2007-03-19 20:40:51 +00001790 $$ = ConstantInt::get($1, $2, true);
Reid Spencerc7a686b2007-02-28 02:24:54 +00001791 CHECK_FOR_ERROR
1792 }
1793 | IntType ESAPINTVAL { // arbitrary precision integer constants
1794 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1795 if ($2->getBitWidth() > BitWidth) {
1796 GEN_ERROR("Constant value does not fit in type");
Reid Spencer8ad254d2007-03-01 19:41:47 +00001797 }
1798 $2->sextOrTrunc(BitWidth);
1799 $$ = ConstantInt::get(*$2);
Reid Spencerc7a686b2007-02-28 02:24:54 +00001800 delete $2;
Reid Spencer266e42b2006-12-23 06:05:41 +00001801 CHECK_FOR_ERROR
1802 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001803 | IntType EUINT64VAL { // integral constants
Reid Spencer266e42b2006-12-23 06:05:41 +00001804 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001805 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencera7bed60a2007-03-19 20:40:51 +00001806 $$ = ConstantInt::get($1, $2, false);
Reid Spencerc7a686b2007-02-28 02:24:54 +00001807 CHECK_FOR_ERROR
1808 }
1809 | IntType EUAPINTVAL { // arbitrary precision integer constants
1810 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1811 if ($2->getBitWidth() > BitWidth) {
1812 GEN_ERROR("Constant value does not fit in type");
Reid Spencer8ad254d2007-03-01 19:41:47 +00001813 }
1814 $2->zextOrTrunc(BitWidth);
1815 $$ = ConstantInt::get(*$2);
Reid Spencerc7a686b2007-02-28 02:24:54 +00001816 delete $2;
Reid Spencer266e42b2006-12-23 06:05:41 +00001817 CHECK_FOR_ERROR
1818 }
Reid Spencer58a8db02007-01-13 05:00:46 +00001819 | INTTYPE TRUETOK { // Boolean constants
1820 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001821 $$ = ConstantInt::getTrue();
Reid Spencer713eedc2006-08-18 08:43:06 +00001822 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001823 }
Reid Spencer58a8db02007-01-13 05:00:46 +00001824 | INTTYPE FALSETOK { // Boolean constants
1825 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001826 $$ = ConstantInt::getFalse();
Reid Spencer713eedc2006-08-18 08:43:06 +00001827 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001828 }
1829 | FPType FPVAL { // Float & Double constants
Reid Spencere2c32da2006-12-03 05:46:11 +00001830 if (!ConstantFP::isValueValidForType($1, $2))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001831 GEN_ERROR("Floating point constant invalid for type");
Reid Spencere2c32da2006-12-03 05:46:11 +00001832 $$ = ConstantFP::get($1, $2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001833 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001834 };
1835
1836
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001837ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001838 if (!UpRefs.empty())
1839 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001840 Constant *Val = $3;
Reid Spencerceb84592007-01-17 02:48:45 +00001841 const Type *DestTy = $5->get();
1842 if (!CastInst::castIsValid($1, $3, DestTy))
1843 GEN_ERROR("invalid cast opcode for cast from '" +
1844 Val->getType()->getDescription() + "' to '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001845 DestTy->getDescription() + "'");
Reid Spencerceb84592007-01-17 02:48:45 +00001846 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencere2c32da2006-12-03 05:46:11 +00001847 delete $5;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001848 }
1849 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001850 if (!isa<PointerType>($3->getType()))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001851 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001852
Reid Spencere2c32da2006-12-03 05:46:11 +00001853 const Type *IdxTy =
Chris Lattnerf79508f2007-02-13 00:58:01 +00001854 GetElementPtrInst::getIndexedType($3->getType(), &(*$4)[0], $4->size(),
1855 true);
Reid Spencere2c32da2006-12-03 05:46:11 +00001856 if (!IdxTy)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001857 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencere2c32da2006-12-03 05:46:11 +00001858
Chris Lattner55ce85c2007-01-31 04:44:08 +00001859 SmallVector<Constant*, 8> IdxVec;
Reid Spencere2c32da2006-12-03 05:46:11 +00001860 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1861 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001862 IdxVec.push_back(C);
1863 else
Reid Spencera7bb3e92007-02-05 10:18:06 +00001864 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001865
1866 delete $4;
1867
Chris Lattner55ce85c2007-01-31 04:44:08 +00001868 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer713eedc2006-08-18 08:43:06 +00001869 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001870 }
1871 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer542964f2007-01-11 18:21:29 +00001872 if ($3->getType() != Type::Int1Ty)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001873 GEN_ERROR("Select condition must be of boolean type");
Reid Spencere2c32da2006-12-03 05:46:11 +00001874 if ($5->getType() != $7->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001875 GEN_ERROR("Select operand types must match");
Reid Spencere2c32da2006-12-03 05:46:11 +00001876 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001877 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001878 }
1879 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001880 if ($3->getType() != $5->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001881 GEN_ERROR("Binary operator types must match");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001882 CHECK_FOR_ERROR;
Reid Spencer844668d2006-12-05 19:16:11 +00001883 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001884 }
1885 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001886 if ($3->getType() != $5->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001887 GEN_ERROR("Logical operator types must match");
Chris Lattner03c49532007-01-15 02:27:26 +00001888 if (!$3->getType()->isInteger()) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001889 if (Instruction::isShift($1) || !isa<VectorType>($3->getType()) ||
1890 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001891 GEN_ERROR("Logical operator requires integral operands");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001892 }
Reid Spencere2c32da2006-12-03 05:46:11 +00001893 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001894 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001895 }
Reid Spencerd2e0c342006-12-04 05:24:24 +00001896 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1897 if ($4->getType() != $6->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001898 GEN_ERROR("icmp operand types must match");
Reid Spencerd2e0c342006-12-04 05:24:24 +00001899 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencere2c32da2006-12-03 05:46:11 +00001900 }
Reid Spencerd2e0c342006-12-04 05:24:24 +00001901 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1902 if ($4->getType() != $6->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001903 GEN_ERROR("fcmp operand types must match");
Reid Spencerd2e0c342006-12-04 05:24:24 +00001904 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencere2c32da2006-12-03 05:46:11 +00001905 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001906 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001907 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001908 GEN_ERROR("Invalid extractelement operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00001909 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001910 CHECK_FOR_ERROR
Chris Lattneraebccf82006-04-08 03:55:17 +00001911 }
1912 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001913 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001914 GEN_ERROR("Invalid insertelement operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00001915 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001916 CHECK_FOR_ERROR
Chris Lattneraebccf82006-04-08 03:55:17 +00001917 }
1918 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001919 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001920 GEN_ERROR("Invalid shufflevector operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00001921 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001922 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001923 };
1924
Chris Lattneraebccf82006-04-08 03:55:17 +00001925
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001926// ConstVector - A list of comma separated constants.
1927ConstVector : ConstVector ',' ConstVal {
1928 ($$ = $1)->push_back($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001929 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001930 }
1931 | ConstVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00001932 $$ = new std::vector<Constant*>();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001933 $$->push_back($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001934 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001935 };
1936
1937
1938// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1939GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
1940
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00001941// ThreadLocal
1942ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
1943
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001944
1945//===----------------------------------------------------------------------===//
1946// Rules to match Modules
1947//===----------------------------------------------------------------------===//
1948
1949// Module rule: Capture the result of parsing the whole file into a result
1950// variable...
1951//
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001952Module
1953 : DefinitionList {
1954 $$ = ParserResult = CurModule.CurrentModule;
1955 CurModule.ModuleDone();
1956 CHECK_FOR_ERROR;
1957 }
1958 | /*empty*/ {
1959 $$ = ParserResult = CurModule.CurrentModule;
1960 CurModule.ModuleDone();
1961 CHECK_FOR_ERROR;
1962 }
1963 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001964
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001965DefinitionList
1966 : Definition
1967 | DefinitionList Definition
1968 ;
1969
1970Definition
Jeff Cohen5d956e42007-01-21 19:19:31 +00001971 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001972 CurFun.FunctionDone();
Reid Spencer713eedc2006-08-18 08:43:06 +00001973 CHECK_FOR_ERROR
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001974 }
1975 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer713eedc2006-08-18 08:43:06 +00001976 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001977 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001978 | MODULE ASM_TOK AsmBlock {
Reid Spencer713eedc2006-08-18 08:43:06 +00001979 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001980 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001981 | OptLocalAssign TYPE Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001982 if (!UpRefs.empty())
1983 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001984 // Eagerly resolve types. This is not an optimization, this is a
1985 // requirement that is due to the fact that we could have this:
1986 //
1987 // %list = type { %list * }
1988 // %list = type { %list * } ; repeated type decl
1989 //
1990 // If types are not resolved eagerly, then the two types will not be
1991 // determined to be the same type!
1992 //
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001993 ResolveTypeTo($1, *$3);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001994
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001995 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer309080a2006-09-28 19:28:24 +00001996 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001997 // If this is a named type that is not a redefinition, add it to the slot
1998 // table.
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001999 CurModule.Types.push_back(*$3);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002000 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002001
Reid Spencerdc0a3a22006-12-29 20:35:03 +00002002 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00002003 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002004 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002005 | OptLocalAssign TYPE VOID {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002006 ResolveTypeTo($1, $3);
2007
2008 if (!setTypeName($3, $1) && !$1) {
2009 CHECK_FOR_ERROR
2010 // If this is a named type that is not a redefinition, add it to the slot
2011 // table.
2012 CurModule.Types.push_back($3);
2013 }
2014 CHECK_FOR_ERROR
2015 }
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002016 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002017 /* "Externally Visible" Linkage */
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002018 if ($5 == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002019 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002020 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
2021 $2, $4, $5->getType(), $5, $3);
Reid Spencerdc0a3a22006-12-29 20:35:03 +00002022 CHECK_FOR_ERROR
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002023 } GlobalVarAttributes {
2024 CurGV = 0;
2025 }
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002026 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType ConstVal {
2027 if ($6 == 0)
2028 GEN_ERROR("Global value initializer is not a constant");
2029 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002030 CHECK_FOR_ERROR
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002031 } GlobalVarAttributes {
2032 CurGV = 0;
2033 }
2034 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType Types {
2035 if (!UpRefs.empty())
2036 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
2037 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4);
2038 CHECK_FOR_ERROR
2039 delete $6;
Reid Spencer309080a2006-09-28 19:28:24 +00002040 } GlobalVarAttributes {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002041 CurGV = 0;
2042 CHECK_FOR_ERROR
2043 }
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00002044 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage ResultTypes SymbolicValueRef {
2045 std::string Name($1);
2046 if (Name.empty())
2047 GEN_ERROR("Alias name cannot be empty")
2048 const PointerType *PFTy = 0;
2049 const FunctionType *Ty = 0;
2050 Value* V = 0;
2051 const Type* VTy = 0;
2052 if (!(PFTy = dyn_cast<PointerType>($5->get())) ||
2053 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2054 VTy = $5->get();
2055 V = getExistingVal(VTy, $6);
2056 } else {
2057 VTy = PFTy;
2058 V = getExistingVal(PFTy, $6);
2059 }
2060 if (V == 0)
2061 GEN_ERROR(std::string("Invalid aliasee for alias: ") + $1);
2062 GlobalValue* Aliasee;
2063 if (Aliasee = dyn_cast<GlobalValue>(V)) {
2064 GlobalAlias* GA = new GlobalAlias(VTy, $4, Name, Aliasee, CurModule.CurrentModule);
2065 GA->setVisibility($2);
2066 InsertValue(GA, CurModule.Values);
2067 } else
2068 GEN_ERROR("Aliases can be created only to global values");
2069 CHECK_FOR_ERROR
2070 delete $5;
2071 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00002072 | TARGET TargetDefinition {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002073 CHECK_FOR_ERROR
2074 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00002075 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer713eedc2006-08-18 08:43:06 +00002076 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002077 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00002078 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002079
2080
2081AsmBlock : STRINGCONSTANT {
2082 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
2083 char *EndStr = UnEscapeLexed($1, true);
2084 std::string NewAsm($1, EndStr);
2085 free($1);
2086
2087 if (AsmSoFar.empty())
2088 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
2089 else
2090 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
Reid Spencer713eedc2006-08-18 08:43:06 +00002091 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002092};
2093
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002094TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002095 CurModule.CurrentModule->setTargetTriple($3);
2096 free($3);
John Criswell6af0b122006-10-24 19:09:48 +00002097 }
Chris Lattner7d1d0342006-10-22 06:08:13 +00002098 | DATALAYOUT '=' STRINGCONSTANT {
Owen Anderson85690f32006-10-18 02:21:48 +00002099 CurModule.CurrentModule->setDataLayout($3);
2100 free($3);
Owen Anderson85690f32006-10-18 02:21:48 +00002101 };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002102
2103LibrariesDefinition : '[' LibList ']';
2104
2105LibList : LibList ',' STRINGCONSTANT {
2106 CurModule.CurrentModule->addLibrary($3);
2107 free($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002108 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002109 }
2110 | STRINGCONSTANT {
2111 CurModule.CurrentModule->addLibrary($1);
2112 free($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002113 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002114 }
2115 | /* empty: end of list */ {
Reid Spencer713eedc2006-08-18 08:43:06 +00002116 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002117 }
2118 ;
2119
2120//===----------------------------------------------------------------------===//
2121// Rules to match Function Headers
2122//===----------------------------------------------------------------------===//
2123
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002124ArgListH : ArgListH ',' Types OptParamAttrs OptLocalName {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002125 if (!UpRefs.empty())
2126 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
2127 if (*$3 == Type::VoidTy)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002128 GEN_ERROR("void typed arguments are invalid");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002129 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002130 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002131 $1->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002132 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002133 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002134 | Types OptParamAttrs OptLocalName {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002135 if (!UpRefs.empty())
2136 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2137 if (*$1 == Type::VoidTy)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002138 GEN_ERROR("void typed arguments are invalid");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002139 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2140 $$ = new ArgListType;
2141 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002142 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002143 };
2144
2145ArgList : ArgListH {
2146 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002147 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002148 }
2149 | ArgListH ',' DOTDOTDOT {
2150 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002151 struct ArgListEntry E;
2152 E.Ty = new PATypeHolder(Type::VoidTy);
2153 E.Name = 0;
Reid Spencera472f662007-04-11 02:44:20 +00002154 E.Attrs = ParamAttr::None;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002155 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002156 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002157 }
2158 | DOTDOTDOT {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002159 $$ = new ArgListType;
2160 struct ArgListEntry E;
2161 E.Ty = new PATypeHolder(Type::VoidTy);
2162 E.Name = 0;
Reid Spencera472f662007-04-11 02:44:20 +00002163 E.Attrs = ParamAttr::None;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002164 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002165 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002166 }
2167 | /* empty */ {
2168 $$ = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00002169 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002170 };
2171
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002172FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002173 OptFuncAttrs OptSection OptAlign {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002174 UnEscapeLexed($3);
2175 std::string FunctionName($3);
2176 free($3); // Free strdup'd memory!
2177
Reid Spencer87622ae2007-01-02 21:54:12 +00002178 // Check the function result for abstractness if this is a define. We should
2179 // have no abstract types at this point
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002180 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2181 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer87622ae2007-01-02 21:54:12 +00002182
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002183 std::vector<const Type*> ParamTypeList;
Christopher Lamb3f706f22007-04-22 20:09:11 +00002184 ParamAttrsVector Attrs;
2185 if ($7 != ParamAttr::None) {
2186 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $7;
2187 Attrs.push_back(PAWI);
2188 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002189 if ($5) { // If there are arguments...
Reid Spencerf51a7052007-04-09 06:16:21 +00002190 unsigned index = 1;
2191 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I, ++index) {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002192 const Type* Ty = I->Ty->get();
Reid Spencer87622ae2007-01-02 21:54:12 +00002193 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2194 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002195 ParamTypeList.push_back(Ty);
2196 if (Ty != Type::VoidTy)
Christopher Lamb3f706f22007-04-22 20:09:11 +00002197 if (I->Attrs != ParamAttr::None) {
2198 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2199 Attrs.push_back(PAWI);
2200 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002201 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002202 }
2203
2204 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2205 if (isVarArg) ParamTypeList.pop_back();
2206
Christopher Lamb3f706f22007-04-22 20:09:11 +00002207 ParamAttrsList *PAL = 0;
2208 if (!Attrs.empty())
2209 PAL = ParamAttrsList::get(Attrs);
Reid Spencerf51a7052007-04-09 06:16:21 +00002210
Christopher Lamb3f706f22007-04-22 20:09:11 +00002211 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg, PAL);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002212 const PointerType *PFT = PointerType::get(FT);
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002213 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002214
2215 ValID ID;
2216 if (!FunctionName.empty()) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002217 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002218 } else {
Reid Spencerd0e8d382007-03-19 18:40:50 +00002219 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002220 }
2221
2222 Function *Fn = 0;
2223 // See if this function was forward referenced. If so, recycle the object.
2224 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2225 // Move the function to the end of the list, from whereever it was
2226 // previously inserted.
2227 Fn = cast<Function>(FWRef);
2228 CurModule.CurrentModule->getFunctionList().remove(Fn);
2229 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2230 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002231 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
2232 if (Fn->getFunctionType() != FT ) {
2233 // The existing function doesn't have the same type. This is an overload
2234 // error.
2235 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
2236 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00002237 // Neither the existing or the current function is a declaration and they
2238 // have the same name and same type. Clearly this is a redefinition.
2239 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002240 } if (Fn->isDeclaration()) {
2241 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002242 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2243 AI != AE; ++AI)
2244 AI->setName("");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002245 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002246 } else { // Not already defined?
2247 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
2248 CurModule.CurrentModule);
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002249
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002250 InsertValue(Fn, CurModule.Values);
2251 }
2252
2253 CurFun.FunctionStart(Fn);
Anton Korobeynikov0ab01ff2006-09-17 13:06:18 +00002254
2255 if (CurFun.isDeclare) {
2256 // If we have declaration, always overwrite linkage. This will allow us to
2257 // correctly handle cases, when pointer to function is passed as argument to
2258 // another function.
2259 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002260 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov0ab01ff2006-09-17 13:06:18 +00002261 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002262 Fn->setCallingConv($1);
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002263 Fn->setAlignment($9);
2264 if ($8) {
2265 Fn->setSection($8);
2266 free($8);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002267 }
2268
2269 // Add all of the arguments we parsed to the function...
2270 if ($5) { // Is null if empty...
2271 if (isVarArg) { // Nuke the last entry
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002272 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0 &&
Reid Spencerac6bfc52007-02-05 17:04:00 +00002273 "Not a varargs marker!");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002274 delete $5->back().Ty;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002275 $5->pop_back(); // Delete the last entry
2276 }
2277 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002278 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002279 unsigned Idx = 1;
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002280 for (ArgListType::iterator I = $5->begin();
2281 I != $5->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002282 delete I->Ty; // Delete the typeholder...
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002283 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer309080a2006-09-28 19:28:24 +00002284 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002285 InsertValue(ArgIt);
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002286 Idx++;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002287 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002288
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002289 delete $5; // We're now done with the argument list
2290 }
Reid Spencer713eedc2006-08-18 08:43:06 +00002291 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002292};
2293
2294BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2295
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002296FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002297 $$ = CurFun.CurrentFunction;
2298
2299 // Make sure that we keep track of the linkage type even if there was a
2300 // previous "declare".
2301 $$->setLinkage($1);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002302 $$->setVisibility($2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002303};
2304
2305END : ENDTOK | '}'; // Allow end of '}' to end a function
2306
2307Function : BasicBlockList END {
2308 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002309 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002310};
2311
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002312FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002313 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002314 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002315 $$ = CurFun.CurrentFunction;
2316 CurFun.FunctionDone();
2317 CHECK_FOR_ERROR
2318 };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002319
2320//===----------------------------------------------------------------------===//
2321// Rules to match Basic Blocks
2322//===----------------------------------------------------------------------===//
2323
2324OptSideEffect : /* empty */ {
2325 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002326 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002327 }
2328 | SIDEEFFECT {
2329 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002330 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002331 };
2332
2333ConstValueRef : ESINT64VAL { // A reference to a direct constant
2334 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002335 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002336 }
2337 | EUINT64VAL {
2338 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002339 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002340 }
2341 | FPVAL { // Perhaps it's an FP constant?
2342 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002343 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002344 }
2345 | TRUETOK {
Zhou Sheng75b871f2007-01-11 12:24:14 +00002346 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer713eedc2006-08-18 08:43:06 +00002347 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002348 }
2349 | FALSETOK {
Zhou Sheng75b871f2007-01-11 12:24:14 +00002350 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer713eedc2006-08-18 08:43:06 +00002351 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002352 }
2353 | NULL_TOK {
2354 $$ = ValID::createNull();
Reid Spencer713eedc2006-08-18 08:43:06 +00002355 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002356 }
2357 | UNDEF {
2358 $$ = ValID::createUndef();
Reid Spencer713eedc2006-08-18 08:43:06 +00002359 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002360 }
2361 | ZEROINITIALIZER { // A vector zero constant.
2362 $$ = ValID::createZeroInit();
Reid Spencer713eedc2006-08-18 08:43:06 +00002363 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002364 }
2365 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencere2c32da2006-12-03 05:46:11 +00002366 const Type *ETy = (*$2)[0]->getType();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002367 int NumElements = $2->size();
2368
Reid Spencerd84d35b2007-02-15 02:26:10 +00002369 VectorType* pt = VectorType::get(ETy, NumElements);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002370 PATypeHolder* PTy = new PATypeHolder(
Reid Spencere2c32da2006-12-03 05:46:11 +00002371 HandleUpRefs(
Reid Spencerd84d35b2007-02-15 02:26:10 +00002372 VectorType::get(
Reid Spencere2c32da2006-12-03 05:46:11 +00002373 ETy,
2374 NumElements)
2375 )
2376 );
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002377
2378 // Verify all elements are correct type!
2379 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencere2c32da2006-12-03 05:46:11 +00002380 if (ETy != (*$2)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002381 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002382 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00002383 (*$2)[i]->getType()->getDescription() + "'.");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002384 }
2385
Reid Spencerd84d35b2007-02-15 02:26:10 +00002386 $$ = ValID::create(ConstantVector::get(pt, *$2));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002387 delete PTy; delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002388 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002389 }
2390 | ConstExpr {
Reid Spencere2c32da2006-12-03 05:46:11 +00002391 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002392 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002393 }
2394 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
2395 char *End = UnEscapeLexed($3, true);
2396 std::string AsmStr = std::string($3, End);
2397 End = UnEscapeLexed($5, true);
2398 std::string Constraints = std::string($5, End);
2399 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
2400 free($3);
2401 free($5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002402 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002403 };
2404
2405// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2406// another value.
2407//
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002408SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2409 $$ = ValID::createLocalID($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002410 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002411 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002412 | GLOBALVAL_ID {
2413 $$ = ValID::createGlobalID($1);
2414 CHECK_FOR_ERROR
2415 }
2416 | LocalName { // Is it a named reference...?
2417 $$ = ValID::createLocalName($1);
2418 CHECK_FOR_ERROR
2419 }
2420 | GlobalName { // Is it a named reference...?
2421 $$ = ValID::createGlobalName($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002422 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002423 };
2424
2425// ValueRef - A reference to a definition... either constant or symbolic
2426ValueRef : SymbolicValueRef | ConstValueRef;
2427
2428
2429// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2430// type immediately preceeds the value reference, and allows complex constant
2431// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2432ResolvedVal : Types ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002433 if (!UpRefs.empty())
2434 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2435 $$ = getVal(*$1, $2);
2436 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002437 CHECK_FOR_ERROR
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002438 }
2439 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002440
2441BasicBlockList : BasicBlockList BasicBlock {
2442 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002443 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002444 }
2445 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
2446 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002447 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002448 };
2449
2450
2451// Basic blocks are terminated by branching instructions:
2452// br, br/cc, switch, ret
2453//
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002454BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002455 setValueName($3, $2);
Reid Spencer309080a2006-09-28 19:28:24 +00002456 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002457 InsertValue($3);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002458 $1->getInstList().push_back($3);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002459 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002460 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002461 };
2462
2463InstructionList : InstructionList Inst {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002464 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2465 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2466 if (CI2->getParent() == 0)
2467 $1->getInstList().push_back(CI2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002468 $1->getInstList().push_back($2);
2469 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002470 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002471 }
Reid Spencerd0e8d382007-03-19 18:40:50 +00002472 | /* empty */ { // Empty space between instruction lists
2473 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
Reid Spencer713eedc2006-08-18 08:43:06 +00002474 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002475 }
Reid Spencerd0e8d382007-03-19 18:40:50 +00002476 | LABELSTR { // Labelled (named) basic block
2477 $$ = defineBBVal(ValID::createLocalName($1));
Reid Spencer713eedc2006-08-18 08:43:06 +00002478 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002479 };
2480
2481BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencere2c32da2006-12-03 05:46:11 +00002482 $$ = new ReturnInst($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00002483 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002484 }
Reid Spencerd0e8d382007-03-19 18:40:50 +00002485 | RET VOID { // Return with no result...
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002486 $$ = new ReturnInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002487 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002488 }
Reid Spencerd0e8d382007-03-19 18:40:50 +00002489 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer309080a2006-09-28 19:28:24 +00002490 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002491 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002492 $$ = new BranchInst(tmpBB);
Reid Spencerd0e8d382007-03-19 18:40:50 +00002493 } // Conditional Branch...
Reid Spencer58a8db02007-01-13 05:00:46 +00002494 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
2495 assert(cast<IntegerType>($2)->getBitWidth() == 1 && "Not Bool?");
Reid Spencer309080a2006-09-28 19:28:24 +00002496 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002497 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002498 BasicBlock* tmpBBB = getBBVal($9);
2499 CHECK_FOR_ERROR
Reid Spencer542964f2007-01-11 18:21:29 +00002500 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002501 CHECK_FOR_ERROR
2502 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002503 }
2504 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencere2c32da2006-12-03 05:46:11 +00002505 Value* tmpVal = getVal($2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002506 CHECK_FOR_ERROR
2507 BasicBlock* tmpBB = getBBVal($6);
2508 CHECK_FOR_ERROR
2509 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002510 $$ = S;
2511
2512 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2513 E = $8->end();
2514 for (; I != E; ++I) {
2515 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2516 S->addCase(CI, I->second);
2517 else
Reid Spencera7bb3e92007-02-05 10:18:06 +00002518 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002519 }
2520 delete $8;
Reid Spencer713eedc2006-08-18 08:43:06 +00002521 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002522 }
2523 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencere2c32da2006-12-03 05:46:11 +00002524 Value* tmpVal = getVal($2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002525 CHECK_FOR_ERROR
2526 BasicBlock* tmpBB = getBBVal($6);
2527 CHECK_FOR_ERROR
2528 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002529 $$ = S;
Reid Spencer713eedc2006-08-18 08:43:06 +00002530 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002531 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002532 | INVOKE OptCallingConv ResultTypes ValueRef '(' ValueRefList ')' OptFuncAttrs
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002533 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002534
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002535 // Handle the short syntax
2536 const PointerType *PFTy = 0;
2537 const FunctionType *Ty = 0;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002538 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002539 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2540 // Pull out the types of all of the arguments...
2541 std::vector<const Type*> ParamTypes;
Christopher Lamb3f706f22007-04-22 20:09:11 +00002542 ParamAttrsVector Attrs;
2543 if ($8 != ParamAttr::None) {
2544 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = 8;
2545 Attrs.push_back(PAWI);
2546 }
Reid Spencerf51a7052007-04-09 06:16:21 +00002547 ValueRefList::iterator I = $6->begin(), E = $6->end();
2548 unsigned index = 1;
2549 for (; I != E; ++I, ++index) {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002550 const Type *Ty = I->Val->getType();
2551 if (Ty == Type::VoidTy)
2552 GEN_ERROR("Short call syntax cannot be used with varargs");
2553 ParamTypes.push_back(Ty);
Christopher Lamb3f706f22007-04-22 20:09:11 +00002554 if (I->Attrs != ParamAttr::None) {
2555 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2556 Attrs.push_back(PAWI);
2557 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002558 }
2559
Christopher Lamb3f706f22007-04-22 20:09:11 +00002560 ParamAttrsList *PAL = 0;
2561 if (!Attrs.empty())
2562 PAL = ParamAttrsList::get(Attrs);
2563 Ty = FunctionType::get($3->get(), ParamTypes, false, PAL);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002564 PFTy = PointerType::get(Ty);
2565 }
2566
Reid Spencer6fb989c2007-03-20 01:13:36 +00002567 delete $3;
2568
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002569 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer309080a2006-09-28 19:28:24 +00002570 CHECK_FOR_ERROR
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002571 BasicBlock *Normal = getBBVal($11);
Reid Spencer309080a2006-09-28 19:28:24 +00002572 CHECK_FOR_ERROR
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002573 BasicBlock *Except = getBBVal($14);
Reid Spencer309080a2006-09-28 19:28:24 +00002574 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002575
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002576 // Check the arguments
2577 ValueList Args;
2578 if ($6->empty()) { // Has no arguments?
2579 // Make sure no arguments is a good thing!
2580 if (Ty->getNumParams() != 0)
2581 GEN_ERROR("No arguments passed to a function that "
Reid Spencera7bb3e92007-02-05 10:18:06 +00002582 "expects arguments");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002583 } else { // Has arguments?
2584 // Loop through FunctionType's arguments and ensure they are specified
2585 // correctly!
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002586 FunctionType::param_iterator I = Ty->param_begin();
2587 FunctionType::param_iterator E = Ty->param_end();
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002588 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002589
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002590 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2591 if (ArgI->Val->getType() != *I)
2592 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00002593 (*I)->getDescription() + "'");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002594 Args.push_back(ArgI->Val);
2595 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002596
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002597 if (Ty->isVarArg()) {
2598 if (I == E)
2599 for (; ArgI != ArgE; ++ArgI)
2600 Args.push_back(ArgI->Val); // push the remaining varargs
2601 } else if (I != E || ArgI != ArgE)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002602 GEN_ERROR("Invalid number of parameters detected");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002603 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002604
2605 // Create the InvokeInst
Chris Lattnerd8028242007-02-13 05:53:56 +00002606 InvokeInst *II = new InvokeInst(V, Normal, Except, &Args[0], Args.size());
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002607 II->setCallingConv($2);
2608 $$ = II;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002609 delete $6;
Reid Spencer713eedc2006-08-18 08:43:06 +00002610 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002611 }
2612 | UNWIND {
2613 $$ = new UnwindInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002614 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002615 }
2616 | UNREACHABLE {
2617 $$ = new UnreachableInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002618 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002619 };
2620
2621
2622
2623JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2624 $$ = $1;
Reid Spencerd0e8d382007-03-19 18:40:50 +00002625 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer309080a2006-09-28 19:28:24 +00002626 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002627 if (V == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002628 GEN_ERROR("May only switch on a constant pool value");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002629
Reid Spencer309080a2006-09-28 19:28:24 +00002630 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002631 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002632 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002633 }
2634 | IntType ConstValueRef ',' LABEL ValueRef {
2635 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencerd0e8d382007-03-19 18:40:50 +00002636 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer309080a2006-09-28 19:28:24 +00002637 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002638
2639 if (V == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002640 GEN_ERROR("May only switch on a constant pool value");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002641
Reid Spencer309080a2006-09-28 19:28:24 +00002642 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002643 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002644 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002645 };
2646
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002647Inst : OptLocalAssign InstVal {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002648 // Is this definition named?? if so, assign the name...
2649 setValueName($2, $1);
2650 CHECK_FOR_ERROR
2651 InsertValue($2);
2652 $$ = $2;
2653 CHECK_FOR_ERROR
2654 };
2655
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002656
2657PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002658 if (!UpRefs.empty())
2659 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002660 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencere2c32da2006-12-03 05:46:11 +00002661 Value* tmpVal = getVal(*$1, $3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002662 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002663 BasicBlock* tmpBB = getBBVal($5);
2664 CHECK_FOR_ERROR
2665 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencere2c32da2006-12-03 05:46:11 +00002666 delete $1;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002667 }
2668 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2669 $$ = $1;
Reid Spencer309080a2006-09-28 19:28:24 +00002670 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002671 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002672 BasicBlock* tmpBB = getBBVal($6);
2673 CHECK_FOR_ERROR
2674 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002675 };
2676
2677
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002678ValueRefList : Types ValueRef OptParamAttrs {
2679 if (!UpRefs.empty())
2680 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2681 // Used for call and invoke instructions
2682 $$ = new ValueRefList();
2683 ValueRefListEntry E; E.Attrs = $3; E.Val = getVal($1->get(), $2);
2684 $$->push_back(E);
Reid Spencer6fb989c2007-03-20 01:13:36 +00002685 delete $1;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002686 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002687 | ValueRefList ',' Types ValueRef OptParamAttrs {
2688 if (!UpRefs.empty())
2689 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002690 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002691 ValueRefListEntry E; E.Attrs = $5; E.Val = getVal($3->get(), $4);
2692 $$->push_back(E);
Reid Spencer6fb989c2007-03-20 01:13:36 +00002693 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00002694 CHECK_FOR_ERROR
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002695 }
2696 | /*empty*/ { $$ = new ValueRefList(); };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002697
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002698IndexList // Used for gep instructions and constant expressions
Reid Spencerd0da3e22006-12-31 21:47:02 +00002699 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002700 | IndexList ',' ResolvedVal {
2701 $$ = $1;
2702 $$->push_back($3);
2703 CHECK_FOR_ERROR
2704 }
Reid Spencerd0da3e22006-12-31 21:47:02 +00002705 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002706
2707OptTailCall : TAIL CALL {
2708 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002709 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002710 }
2711 | CALL {
2712 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002713 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002714 };
2715
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002716InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002717 if (!UpRefs.empty())
2718 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner03c49532007-01-15 02:27:26 +00002719 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencerd84d35b2007-02-15 02:26:10 +00002720 !isa<VectorType>((*$2).get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002721 GEN_ERROR(
Reid Spencera7bb3e92007-02-05 10:18:06 +00002722 "Arithmetic operator requires integer, FP, or packed operands");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002723 if (isa<VectorType>((*$2).get()) &&
Reid Spencere2c32da2006-12-03 05:46:11 +00002724 ($1 == Instruction::URem ||
2725 $1 == Instruction::SRem ||
2726 $1 == Instruction::FRem))
Chris Lattner4669b0b2007-02-19 07:44:24 +00002727 GEN_ERROR("Remainder not supported on vector types");
Reid Spencere2c32da2006-12-03 05:46:11 +00002728 Value* val1 = getVal(*$2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002729 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002730 Value* val2 = getVal(*$2, $5);
Reid Spencer309080a2006-09-28 19:28:24 +00002731 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002732 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002733 if ($$ == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002734 GEN_ERROR("binary operator returned null");
Reid Spencere2c32da2006-12-03 05:46:11 +00002735 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002736 }
2737 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002738 if (!UpRefs.empty())
2739 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner03c49532007-01-15 02:27:26 +00002740 if (!(*$2)->isInteger()) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002741 if (Instruction::isShift($1) || !isa<VectorType>($2->get()) ||
2742 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencera7bb3e92007-02-05 10:18:06 +00002743 GEN_ERROR("Logical operator requires integral operands");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002744 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002745 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002746 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002747 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer309080a2006-09-28 19:28:24 +00002748 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002749 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002750 if ($$ == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002751 GEN_ERROR("binary operator returned null");
Reid Spencere2c32da2006-12-03 05:46:11 +00002752 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002753 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002754 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002755 if (!UpRefs.empty())
2756 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencerd84d35b2007-02-15 02:26:10 +00002757 if (isa<VectorType>((*$3).get()))
Chris Lattner4669b0b2007-02-19 07:44:24 +00002758 GEN_ERROR("Vector types not supported by icmp instruction");
Reid Spencere2c32da2006-12-03 05:46:11 +00002759 Value* tmpVal1 = getVal(*$3, $4);
2760 CHECK_FOR_ERROR
2761 Value* tmpVal2 = getVal(*$3, $6);
2762 CHECK_FOR_ERROR
2763 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2764 if ($$ == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002765 GEN_ERROR("icmp operator returned null");
Reid Spencer6fb989c2007-03-20 01:13:36 +00002766 delete $3;
Reid Spencere2c32da2006-12-03 05:46:11 +00002767 }
2768 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002769 if (!UpRefs.empty())
2770 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencerd84d35b2007-02-15 02:26:10 +00002771 if (isa<VectorType>((*$3).get()))
Chris Lattner4669b0b2007-02-19 07:44:24 +00002772 GEN_ERROR("Vector types not supported by fcmp instruction");
Reid Spencere2c32da2006-12-03 05:46:11 +00002773 Value* tmpVal1 = getVal(*$3, $4);
2774 CHECK_FOR_ERROR
2775 Value* tmpVal2 = getVal(*$3, $6);
2776 CHECK_FOR_ERROR
2777 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2778 if ($$ == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002779 GEN_ERROR("fcmp operator returned null");
Reid Spencer6fb989c2007-03-20 01:13:36 +00002780 delete $3;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002781 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002782 | CastOps ResolvedVal TO Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002783 if (!UpRefs.empty())
2784 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002785 Value* Val = $2;
Reid Spencerceb84592007-01-17 02:48:45 +00002786 const Type* DestTy = $4->get();
2787 if (!CastInst::castIsValid($1, Val, DestTy))
2788 GEN_ERROR("invalid cast opcode for cast from '" +
2789 Val->getType()->getDescription() + "' to '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00002790 DestTy->getDescription() + "'");
Reid Spencerceb84592007-01-17 02:48:45 +00002791 $$ = CastInst::create($1, Val, DestTy);
Reid Spencere2c32da2006-12-03 05:46:11 +00002792 delete $4;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002793 }
2794 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer542964f2007-01-11 18:21:29 +00002795 if ($2->getType() != Type::Int1Ty)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002796 GEN_ERROR("select condition must be boolean");
Reid Spencere2c32da2006-12-03 05:46:11 +00002797 if ($4->getType() != $6->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00002798 GEN_ERROR("select value types should match");
Reid Spencere2c32da2006-12-03 05:46:11 +00002799 $$ = new SelectInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002800 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002801 }
2802 | VAARG ResolvedVal ',' Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002803 if (!UpRefs.empty())
2804 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002805 $$ = new VAArgInst($2, *$4);
2806 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00002807 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002808 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002809 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002810 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencera7bb3e92007-02-05 10:18:06 +00002811 GEN_ERROR("Invalid extractelement operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00002812 $$ = new ExtractElementInst($2, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002813 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002814 }
2815 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002816 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencera7bb3e92007-02-05 10:18:06 +00002817 GEN_ERROR("Invalid insertelement operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00002818 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002819 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002820 }
Chris Lattner9ff96a72006-04-08 01:18:56 +00002821 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002822 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencera7bb3e92007-02-05 10:18:06 +00002823 GEN_ERROR("Invalid shufflevector operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00002824 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002825 CHECK_FOR_ERROR
Chris Lattner9ff96a72006-04-08 01:18:56 +00002826 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002827 | PHI_TOK PHIList {
2828 const Type *Ty = $2->front().first->getType();
2829 if (!Ty->isFirstClassType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00002830 GEN_ERROR("PHI node operands must be of first class type");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002831 $$ = new PHINode(Ty);
2832 ((PHINode*)$$)->reserveOperandSpace($2->size());
2833 while ($2->begin() != $2->end()) {
2834 if ($2->front().first->getType() != Ty)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002835 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002836 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
2837 $2->pop_front();
2838 }
2839 delete $2; // Free the list...
Reid Spencer713eedc2006-08-18 08:43:06 +00002840 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002841 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002842 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ValueRefList ')'
2843 OptFuncAttrs {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002844
2845 // Handle the short syntax
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002846 const PointerType *PFTy = 0;
2847 const FunctionType *Ty = 0;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002848 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002849 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2850 // Pull out the types of all of the arguments...
2851 std::vector<const Type*> ParamTypes;
Christopher Lamb3f706f22007-04-22 20:09:11 +00002852 ParamAttrsVector Attrs;
2853 if ($8 != ParamAttr::None) {
2854 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $8;
2855 Attrs.push_back(PAWI);
2856 }
Reid Spencerf51a7052007-04-09 06:16:21 +00002857 unsigned index = 1;
2858 ValueRefList::iterator I = $6->begin(), E = $6->end();
2859 for (; I != E; ++I, ++index) {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002860 const Type *Ty = I->Val->getType();
2861 if (Ty == Type::VoidTy)
2862 GEN_ERROR("Short call syntax cannot be used with varargs");
2863 ParamTypes.push_back(Ty);
Christopher Lamb3f706f22007-04-22 20:09:11 +00002864 if (I->Attrs != ParamAttr::None) {
2865 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2866 Attrs.push_back(PAWI);
2867 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002868 }
2869
Christopher Lamb3f706f22007-04-22 20:09:11 +00002870 ParamAttrsList *PAL = 0;
2871 if (!Attrs.empty())
2872 PAL = ParamAttrsList::get(Attrs);
Reid Spencerf51a7052007-04-09 06:16:21 +00002873
Christopher Lamb3f706f22007-04-22 20:09:11 +00002874 Ty = FunctionType::get($3->get(), ParamTypes, false, PAL);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002875 PFTy = PointerType::get(Ty);
2876 }
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00002877
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002878 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer309080a2006-09-28 19:28:24 +00002879 CHECK_FOR_ERROR
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00002880
Reid Spencere6a50a12007-04-16 06:56:07 +00002881 // Check for call to invalid intrinsic to avoid crashing later.
2882 if (Function *theF = dyn_cast<Function>(V)) {
Reid Spencer0ff60612007-04-16 22:02:23 +00002883 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
Reid Spencer660fa7b2007-04-16 20:35:38 +00002884 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
2885 !theF->getIntrinsicID(true))
Reid Spencere6a50a12007-04-16 06:56:07 +00002886 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
2887 theF->getName() + "'");
2888 }
2889
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002890 // Check the arguments
2891 ValueList Args;
2892 if ($6->empty()) { // Has no arguments?
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002893 // Make sure no arguments is a good thing!
2894 if (Ty->getNumParams() != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002895 GEN_ERROR("No arguments passed to a function that "
Reid Spencera7bb3e92007-02-05 10:18:06 +00002896 "expects arguments");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002897 } else { // Has arguments?
2898 // Loop through FunctionType's arguments and ensure they are specified
2899 // correctly!
2900 //
2901 FunctionType::param_iterator I = Ty->param_begin();
2902 FunctionType::param_iterator E = Ty->param_end();
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002903 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002904
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002905 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2906 if (ArgI->Val->getType() != *I)
2907 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00002908 (*I)->getDescription() + "'");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002909 Args.push_back(ArgI->Val);
2910 }
2911 if (Ty->isVarArg()) {
2912 if (I == E)
2913 for (; ArgI != ArgE; ++ArgI)
2914 Args.push_back(ArgI->Val); // push the remaining varargs
2915 } else if (I != E || ArgI != ArgE)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002916 GEN_ERROR("Invalid number of parameters detected");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002917 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002918 // Create the call node
Chris Lattnerd8028242007-02-13 05:53:56 +00002919 CallInst *CI = new CallInst(V, &Args[0], Args.size());
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002920 CI->setTailCall($1);
2921 CI->setCallingConv($2);
2922 $$ = CI;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002923 delete $6;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002924 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00002925 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002926 }
2927 | MemoryInst {
2928 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002929 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002930 };
2931
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002932OptVolatile : VOLATILE {
2933 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002934 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002935 }
2936 | /* empty */ {
2937 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002938 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002939 };
2940
2941
2942
2943MemoryInst : MALLOC Types OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002944 if (!UpRefs.empty())
2945 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002946 $$ = new MallocInst(*$2, 0, $3);
2947 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002948 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002949 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +00002950 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002951 if (!UpRefs.empty())
2952 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002953 Value* tmpVal = getVal($4, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002954 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002955 $$ = new MallocInst(*$2, tmpVal, $6);
2956 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002957 }
2958 | ALLOCA Types OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002959 if (!UpRefs.empty())
2960 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002961 $$ = new AllocaInst(*$2, 0, $3);
2962 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002963 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002964 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +00002965 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002966 if (!UpRefs.empty())
2967 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002968 Value* tmpVal = getVal($4, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002969 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002970 $$ = new AllocaInst(*$2, tmpVal, $6);
2971 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002972 }
2973 | FREE ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002974 if (!isa<PointerType>($2->getType()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002975 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencera7bb3e92007-02-05 10:18:06 +00002976 $2->getType()->getDescription() + "");
Reid Spencere2c32da2006-12-03 05:46:11 +00002977 $$ = new FreeInst($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00002978 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002979 }
2980
Christopher Lamb3f706f22007-04-22 20:09:11 +00002981 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002982 if (!UpRefs.empty())
2983 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002984 if (!isa<PointerType>($3->get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002985 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencere2c32da2006-12-03 05:46:11 +00002986 (*$3)->getDescription());
2987 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002988 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencere2c32da2006-12-03 05:46:11 +00002989 (*$3)->getDescription());
2990 Value* tmpVal = getVal(*$3, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002991 CHECK_FOR_ERROR
Christopher Lamb3f706f22007-04-22 20:09:11 +00002992 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencere2c32da2006-12-03 05:46:11 +00002993 delete $3;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002994 }
Christopher Lamb3f706f22007-04-22 20:09:11 +00002995 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002996 if (!UpRefs.empty())
2997 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002998 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002999 if (!PT)
Reid Spencer713eedc2006-08-18 08:43:06 +00003000 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencere2c32da2006-12-03 05:46:11 +00003001 (*$5)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003002 const Type *ElTy = PT->getElementType();
Reid Spencere2c32da2006-12-03 05:46:11 +00003003 if (ElTy != $3->getType())
3004 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencera7bb3e92007-02-05 10:18:06 +00003005 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003006
Reid Spencere2c32da2006-12-03 05:46:11 +00003007 Value* tmpVal = getVal(*$5, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00003008 CHECK_FOR_ERROR
Christopher Lamb3f706f22007-04-22 20:09:11 +00003009 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencere2c32da2006-12-03 05:46:11 +00003010 delete $5;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003011 }
3012 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00003013 if (!UpRefs.empty())
3014 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00003015 if (!isa<PointerType>($2->get()))
Reid Spencera7bb3e92007-02-05 10:18:06 +00003016 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003017
Chris Lattnerf79508f2007-02-13 00:58:01 +00003018 if (!GetElementPtrInst::getIndexedType(*$2, &(*$4)[0], $4->size(), true))
Reid Spencer713eedc2006-08-18 08:43:06 +00003019 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00003020 (*$2)->getDescription()+ "'");
Reid Spencere2c32da2006-12-03 05:46:11 +00003021 Value* tmpVal = getVal(*$2, $3);
Reid Spencer713eedc2006-08-18 08:43:06 +00003022 CHECK_FOR_ERROR
Chris Lattnerf79508f2007-02-13 00:58:01 +00003023 $$ = new GetElementPtrInst(tmpVal, &(*$4)[0], $4->size());
Reid Spencere2c32da2006-12-03 05:46:11 +00003024 delete $2;
Reid Spencer309080a2006-09-28 19:28:24 +00003025 delete $4;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003026 };
3027
3028
3029%%
Reid Spencer713eedc2006-08-18 08:43:06 +00003030
Reid Spencer42f0cbe2006-12-31 05:40:51 +00003031// common code from the two 'RunVMAsmParser' functions
3032static Module* RunParser(Module * M) {
3033
3034 llvmAsmlineno = 1; // Reset the current line number...
3035 CurModule.CurrentModule = M;
3036#if YYDEBUG
3037 yydebug = Debug;
3038#endif
3039
3040 // Check to make sure the parser succeeded
3041 if (yyparse()) {
3042 if (ParserResult)
3043 delete ParserResult;
3044 return 0;
3045 }
3046
Reid Spenceref01c472007-03-30 01:37:39 +00003047 // Emit an error if there are any unresolved types left.
3048 if (!CurModule.LateResolveTypes.empty()) {
3049 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3050 if (DID.Type == ValID::LocalName) {
3051 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3052 } else {
3053 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3054 }
3055 if (ParserResult)
3056 delete ParserResult;
3057 return 0;
3058 }
3059
3060 // Emit an error if there are any unresolved values left.
3061 if (!CurModule.LateResolveValues.empty()) {
3062 Value *V = CurModule.LateResolveValues.back();
3063 std::map<Value*, std::pair<ValID, int> >::iterator I =
3064 CurModule.PlaceHolderInfo.find(V);
3065
3066 if (I != CurModule.PlaceHolderInfo.end()) {
3067 ValID &DID = I->second.first;
3068 if (DID.Type == ValID::LocalName) {
3069 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3070 } else {
3071 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3072 }
3073 if (ParserResult)
3074 delete ParserResult;
3075 return 0;
3076 }
3077 }
3078
Reid Spencer42f0cbe2006-12-31 05:40:51 +00003079 // Check to make sure that parsing produced a result
3080 if (!ParserResult)
3081 return 0;
3082
3083 // Reset ParserResult variable while saving its value for the result.
3084 Module *Result = ParserResult;
3085 ParserResult = 0;
3086
3087 return Result;
3088}
3089
Reid Spencer713eedc2006-08-18 08:43:06 +00003090void llvm::GenerateError(const std::string &message, int LineNo) {
3091 if (LineNo == -1) LineNo = llvmAsmlineno;
3092 // TODO: column number in exception
3093 if (TheParseError)
3094 TheParseError->setError(CurFilename, message, LineNo);
3095 TriggerError = 1;
3096}
3097
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003098int yyerror(const char *ErrorMsg) {
3099 std::string where
3100 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
3101 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00003102 std::string errMsg = where + "error: " + std::string(ErrorMsg);
3103 if (yychar != YYEMPTY && yychar != 0)
3104 errMsg += " while reading token: '" + std::string(llvmAsmtext, llvmAsmleng)+
3105 "'";
Reid Spencer713eedc2006-08-18 08:43:06 +00003106 GenerateError(errMsg);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003107 return 0;
3108}