blob: 76ad346fedb7358b7e21a4b3a9783a81eda7a29b [file] [log] [blame]
Chris Lattner58af2a12006-02-15 07:22:58 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner58af2a12006-02-15 07:22:58 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the bison parser for LLVM assembly languages files.
11//
12//===----------------------------------------------------------------------===//
13
14%{
15#include "ParserInternals.h"
16#include "llvm/CallingConv.h"
17#include "llvm/InlineAsm.h"
18#include "llvm/Instructions.h"
19#include "llvm/Module.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000020#include "llvm/ValueSymbolTable.h"
Chandler Carruth02202192007-08-04 01:56:21 +000021#include "llvm/AutoUpgrade.h"
Chris Lattner58af2a12006-02-15 07:22:58 +000022#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer14310612006-12-31 05:40:51 +000023#include "llvm/Support/CommandLine.h"
Chris Lattnerf7469af2007-01-31 04:44:08 +000024#include "llvm/ADT/SmallVector.h"
Chris Lattner58af2a12006-02-15 07:22:58 +000025#include "llvm/ADT/STLExtras.h"
26#include "llvm/Support/MathExtras.h"
Reid Spencer481169e2006-12-01 00:33:46 +000027#include "llvm/Support/Streams.h"
Dale Johannesen22c39792008-02-22 22:17:59 +000028#include "llvm/ParamAttrsList.h"
Chris Lattner58af2a12006-02-15 07:22:58 +000029#include <algorithm>
Chris Lattner58af2a12006-02-15 07:22:58 +000030#include <list>
Chris Lattner8adde282007-02-11 21:40:10 +000031#include <map>
Chris Lattner58af2a12006-02-15 07:22:58 +000032#include <utility>
33
Reid Spencere4f47592006-08-18 17:32:55 +000034// The following is a gross hack. In order to rid the libAsmParser library of
35// exceptions, we have to have a way of getting the yyparse function to go into
36// an error situation. So, whenever we want an error to occur, the GenerateError
37// function (see bottom of file) sets TriggerError. Then, at the end of each
38// production in the grammer we use CHECK_FOR_ERROR which will invoke YYERROR
39// (a goto) to put YACC in error state. Furthermore, several calls to
40// GenerateError are made from inside productions and they must simulate the
41// previous exception behavior by exiting the production immediately. We have
42// replaced these with the GEN_ERROR macro which calls GeneratError and then
43// immediately invokes YYERROR. This would be so much cleaner if it was a
44// recursive descent parser.
Reid Spencer61c83e02006-08-18 08:43:06 +000045static bool TriggerError = false;
Reid Spencerf63697d2006-10-09 17:36:59 +000046#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYABORT; } }
Reid Spencer61c83e02006-08-18 08:43:06 +000047#define GEN_ERROR(msg) { GenerateError(msg); YYERROR; }
48
Chris Lattner58af2a12006-02-15 07:22:58 +000049int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
50int yylex(); // declaration" of xxx warnings.
51int yyparse();
Chris Lattner58af2a12006-02-15 07:22:58 +000052using namespace llvm;
53
54static Module *ParserResult;
55
56// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
57// relating to upreferences in the input stream.
58//
59//#define DEBUG_UPREFS 1
60#ifdef DEBUG_UPREFS
Bill Wendlinge8156192006-12-07 01:30:32 +000061#define UR_OUT(X) cerr << X
Chris Lattner58af2a12006-02-15 07:22:58 +000062#else
63#define UR_OUT(X)
64#endif
65
66#define YYERROR_VERBOSE 1
67
Chris Lattner58af2a12006-02-15 07:22:58 +000068static GlobalVariable *CurGV;
69
70
71// This contains info used when building the body of a function. It is
72// destroyed when the function is completed.
73//
74typedef std::vector<Value *> ValueList; // Numbered defs
Reid Spencer14310612006-12-31 05:40:51 +000075
Chris Lattner58af2a12006-02-15 07:22:58 +000076static void
Reid Spencer93c40032007-03-19 18:40:50 +000077ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers=0);
Chris Lattner58af2a12006-02-15 07:22:58 +000078
79static struct PerModuleInfo {
80 Module *CurrentModule;
Reid Spencer93c40032007-03-19 18:40:50 +000081 ValueList Values; // Module level numbered definitions
82 ValueList LateResolveValues;
Reid Spencer861d9d62006-11-28 07:29:44 +000083 std::vector<PATypeHolder> Types;
84 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner58af2a12006-02-15 07:22:58 +000085
86 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Chris Lattner0ad19702006-06-21 16:53:00 +000087 /// how they were referenced and on which line of the input they came from so
Chris Lattner58af2a12006-02-15 07:22:58 +000088 /// that we can resolve them later and print error messages as appropriate.
89 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
90
91 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
92 // references to global values. Global values may be referenced before they
93 // are defined, and if so, the temporary object that they represent is held
94 // here. This is used for forward references of GlobalValues.
95 //
96 typedef std::map<std::pair<const PointerType *,
97 ValID>, GlobalValue*> GlobalRefsType;
98 GlobalRefsType GlobalRefs;
99
100 void ModuleDone() {
101 // If we could not resolve some functions at function compilation time
102 // (calls to functions before they are defined), resolve them now... Types
103 // are resolved when the constant pool has been completely parsed.
104 //
105 ResolveDefinitions(LateResolveValues);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000106 if (TriggerError)
107 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000108
109 // Check to make sure that all global value forward references have been
110 // resolved!
111 //
112 if (!GlobalRefs.empty()) {
113 std::string UndefinedReferences = "Unresolved global references exist:\n";
114
115 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
116 I != E; ++I) {
117 UndefinedReferences += " " + I->first.first->getDescription() + " " +
118 I->first.second.getName() + "\n";
119 }
Reid Spencer61c83e02006-08-18 08:43:06 +0000120 GenerateError(UndefinedReferences);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000121 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000122 }
123
Chandler Carruth02202192007-08-04 01:56:21 +0000124 // Look for intrinsic functions and CallInst that need to be upgraded
125 for (Module::iterator FI = CurrentModule->begin(),
126 FE = CurrentModule->end(); FI != FE; )
127 UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove
128
Chris Lattner58af2a12006-02-15 07:22:58 +0000129 Values.clear(); // Clear out function local definitions
130 Types.clear();
131 CurrentModule = 0;
132 }
133
134 // GetForwardRefForGlobal - Check to see if there is a forward reference
135 // for this global. If so, remove it from the GlobalRefs map and return it.
136 // If not, just return null.
137 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
138 // Check to see if there is a forward reference to this global variable...
139 // if there is, eliminate it and patch the reference to use the new def'n.
140 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
141 GlobalValue *Ret = 0;
142 if (I != GlobalRefs.end()) {
143 Ret = I->second;
144 GlobalRefs.erase(I);
145 }
146 return Ret;
147 }
Reid Spencer8c8a2dc2007-01-02 21:54:12 +0000148
149 bool TypeIsUnresolved(PATypeHolder* PATy) {
150 // If it isn't abstract, its resolved
151 const Type* Ty = PATy->get();
152 if (!Ty->isAbstract())
153 return false;
154 // Traverse the type looking for abstract types. If it isn't abstract then
155 // we don't need to traverse that leg of the type.
156 std::vector<const Type*> WorkList, SeenList;
157 WorkList.push_back(Ty);
158 while (!WorkList.empty()) {
159 const Type* Ty = WorkList.back();
160 SeenList.push_back(Ty);
161 WorkList.pop_back();
162 if (const OpaqueType* OpTy = dyn_cast<OpaqueType>(Ty)) {
163 // Check to see if this is an unresolved type
164 std::map<ValID, PATypeHolder>::iterator I = LateResolveTypes.begin();
165 std::map<ValID, PATypeHolder>::iterator E = LateResolveTypes.end();
166 for ( ; I != E; ++I) {
167 if (I->second.get() == OpTy)
168 return true;
169 }
170 } else if (const SequentialType* SeqTy = dyn_cast<SequentialType>(Ty)) {
171 const Type* TheTy = SeqTy->getElementType();
172 if (TheTy->isAbstract() && TheTy != Ty) {
173 std::vector<const Type*>::iterator I = SeenList.begin(),
174 E = SeenList.end();
175 for ( ; I != E; ++I)
176 if (*I == TheTy)
177 break;
178 if (I == E)
179 WorkList.push_back(TheTy);
180 }
181 } else if (const StructType* StrTy = dyn_cast<StructType>(Ty)) {
182 for (unsigned i = 0; i < StrTy->getNumElements(); ++i) {
183 const Type* TheTy = StrTy->getElementType(i);
184 if (TheTy->isAbstract() && TheTy != Ty) {
185 std::vector<const Type*>::iterator I = SeenList.begin(),
186 E = SeenList.end();
187 for ( ; I != E; ++I)
188 if (*I == TheTy)
189 break;
190 if (I == E)
191 WorkList.push_back(TheTy);
192 }
193 }
194 }
195 }
196 return false;
197 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000198} CurModule;
199
200static struct PerFunctionInfo {
201 Function *CurrentFunction; // Pointer to current function being created
202
Reid Spencer93c40032007-03-19 18:40:50 +0000203 ValueList Values; // Keep track of #'d definitions
204 unsigned NextValNum;
205 ValueList LateResolveValues;
Reid Spenceref9b9a72007-02-05 20:47:22 +0000206 bool isDeclare; // Is this function a forward declararation?
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000207 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000208 GlobalValue::VisibilityTypes Visibility;
Chris Lattner58af2a12006-02-15 07:22:58 +0000209
210 /// BBForwardRefs - When we see forward references to basic blocks, keep
211 /// track of them here.
Reid Spencer93c40032007-03-19 18:40:50 +0000212 std::map<ValID, BasicBlock*> BBForwardRefs;
Chris Lattner58af2a12006-02-15 07:22:58 +0000213
214 inline PerFunctionInfo() {
215 CurrentFunction = 0;
216 isDeclare = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000217 Linkage = GlobalValue::ExternalLinkage;
218 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner58af2a12006-02-15 07:22:58 +0000219 }
220
221 inline void FunctionStart(Function *M) {
222 CurrentFunction = M;
Reid Spencer93c40032007-03-19 18:40:50 +0000223 NextValNum = 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000224 }
225
226 void FunctionDone() {
Chris Lattner58af2a12006-02-15 07:22:58 +0000227 // Any forward referenced blocks left?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000228 if (!BBForwardRefs.empty()) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000229 GenerateError("Undefined reference to label " +
Reid Spencer93c40032007-03-19 18:40:50 +0000230 BBForwardRefs.begin()->second->getName());
Reid Spencer5b7e7532006-09-28 19:28:24 +0000231 return;
232 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000233
234 // Resolve all forward references now.
235 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
236
237 Values.clear(); // Clear out function local definitions
Reid Spencer93c40032007-03-19 18:40:50 +0000238 BBForwardRefs.clear();
Chris Lattner58af2a12006-02-15 07:22:58 +0000239 CurrentFunction = 0;
240 isDeclare = false;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000241 Linkage = GlobalValue::ExternalLinkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000242 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner58af2a12006-02-15 07:22:58 +0000243 }
244} CurFun; // Info for the current function...
245
246static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
247
248
249//===----------------------------------------------------------------------===//
250// Code to handle definitions of all the types
251//===----------------------------------------------------------------------===//
252
Reid Spencer93c40032007-03-19 18:40:50 +0000253static void InsertValue(Value *V, ValueList &ValueTab = CurFun.Values) {
254 // Things that have names or are void typed don't get slot numbers
255 if (V->hasName() || (V->getType() == Type::VoidTy))
256 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000257
Reid Spencer93c40032007-03-19 18:40:50 +0000258 // In the case of function values, we have to allow for the forward reference
259 // of basic blocks, which are included in the numbering. Consequently, we keep
260 // track of the next insertion location with NextValNum. When a BB gets
261 // inserted, it could change the size of the CurFun.Values vector.
262 if (&ValueTab == &CurFun.Values) {
263 if (ValueTab.size() <= CurFun.NextValNum)
264 ValueTab.resize(CurFun.NextValNum+1);
265 ValueTab[CurFun.NextValNum++] = V;
266 return;
267 }
268 // For all other lists, its okay to just tack it on the back of the vector.
269 ValueTab.push_back(V);
Chris Lattner58af2a12006-02-15 07:22:58 +0000270}
271
272static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
273 switch (D.Type) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000274 case ValID::LocalID: // Is it a numbered definition?
Chris Lattner58af2a12006-02-15 07:22:58 +0000275 // Module constants occupy the lowest numbered slots...
Reid Spencer41dff5e2007-01-26 08:05:27 +0000276 if (D.Num < CurModule.Types.size())
277 return CurModule.Types[D.Num];
Chris Lattner58af2a12006-02-15 07:22:58 +0000278 break;
Reid Spencer41dff5e2007-01-26 08:05:27 +0000279 case ValID::LocalName: // Is it a named definition?
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000280 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.getName())) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000281 D.destroy(); // Free old strdup'd memory...
282 return N;
283 }
284 break;
285 default:
Reid Spencerb5334b02007-02-05 10:18:06 +0000286 GenerateError("Internal parser error: Invalid symbol type reference");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000287 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000288 }
289
290 // If we reached here, we referenced either a symbol that we don't know about
291 // or an id number that hasn't been read yet. We may be referencing something
292 // forward, so just create an entry to be resolved later and get to it...
293 //
294 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
295
296
297 if (inFunctionScope()) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000298 if (D.Type == ValID::LocalName) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000299 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000300 return 0;
301 } else {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000302 GenerateError("Reference to an undefined type: #" + utostr(D.Num));
Reid Spencer5b7e7532006-09-28 19:28:24 +0000303 return 0;
304 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000305 }
306
Reid Spencer861d9d62006-11-28 07:29:44 +0000307 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Chris Lattner58af2a12006-02-15 07:22:58 +0000308 if (I != CurModule.LateResolveTypes.end())
Reid Spencer861d9d62006-11-28 07:29:44 +0000309 return I->second;
Chris Lattner58af2a12006-02-15 07:22:58 +0000310
Reid Spencer861d9d62006-11-28 07:29:44 +0000311 Type *Typ = OpaqueType::get();
312 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
313 return Typ;
Reid Spencera132e042006-12-03 05:46:11 +0000314 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000315
Reid Spencer93c40032007-03-19 18:40:50 +0000316// getExistingVal - Look up the value specified by the provided type and
Chris Lattner58af2a12006-02-15 07:22:58 +0000317// the provided ValID. If the value exists and has already been defined, return
318// it. Otherwise return null.
319//
Reid Spencer93c40032007-03-19 18:40:50 +0000320static Value *getExistingVal(const Type *Ty, const ValID &D) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000321 if (isa<FunctionType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000322 GenerateError("Functions are not values and "
Chris Lattner58af2a12006-02-15 07:22:58 +0000323 "must be referenced as pointers");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000324 return 0;
325 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000326
327 switch (D.Type) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000328 case ValID::LocalID: { // Is it a numbered definition?
Reid Spencer41dff5e2007-01-26 08:05:27 +0000329 // Check that the number is within bounds.
Reid Spencer93c40032007-03-19 18:40:50 +0000330 if (D.Num >= CurFun.Values.size())
331 return 0;
332 Value *Result = CurFun.Values[D.Num];
333 if (Ty != Result->getType()) {
334 GenerateError("Numbered value (%" + utostr(D.Num) + ") of type '" +
335 Result->getType()->getDescription() + "' does not match "
336 "expected type, '" + Ty->getDescription() + "'");
337 return 0;
338 }
339 return Result;
Reid Spencer41dff5e2007-01-26 08:05:27 +0000340 }
341 case ValID::GlobalID: { // Is it a numbered definition?
Reid Spencer93c40032007-03-19 18:40:50 +0000342 if (D.Num >= CurModule.Values.size())
Reid Spenceref9b9a72007-02-05 20:47:22 +0000343 return 0;
Reid Spencer93c40032007-03-19 18:40:50 +0000344 Value *Result = CurModule.Values[D.Num];
345 if (Ty != Result->getType()) {
346 GenerateError("Numbered value (@" + utostr(D.Num) + ") of type '" +
347 Result->getType()->getDescription() + "' does not match "
348 "expected type, '" + Ty->getDescription() + "'");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000349 return 0;
Reid Spencer93c40032007-03-19 18:40:50 +0000350 }
351 return Result;
Chris Lattner58af2a12006-02-15 07:22:58 +0000352 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000353
354 case ValID::LocalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000355 if (!inFunctionScope())
356 return 0;
357 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000358 Value *N = SymTab.lookup(D.getName());
Reid Spenceref9b9a72007-02-05 20:47:22 +0000359 if (N == 0)
360 return 0;
361 if (N->getType() != Ty)
362 return 0;
Reid Spencer41dff5e2007-01-26 08:05:27 +0000363
364 D.destroy(); // Free old strdup'd memory...
365 return N;
366 }
367 case ValID::GlobalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000368 ValueSymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable();
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000369 Value *N = SymTab.lookup(D.getName());
Reid Spenceref9b9a72007-02-05 20:47:22 +0000370 if (N == 0)
371 return 0;
372 if (N->getType() != Ty)
373 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000374
375 D.destroy(); // Free old strdup'd memory...
376 return N;
377 }
378
379 // Check to make sure that "Ty" is an integral type, and that our
380 // value will fit into the specified type...
381 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattner38905612008-02-19 04:36:25 +0000382 if (!isa<IntegerType>(Ty) ||
383 !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000384 GenerateError("Signed integral constant '" +
Chris Lattner58af2a12006-02-15 07:22:58 +0000385 itostr(D.ConstPool64) + "' is invalid for type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000386 Ty->getDescription() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000387 return 0;
388 }
Reid Spencer49d273e2007-03-19 20:40:51 +0000389 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner58af2a12006-02-15 07:22:58 +0000390
391 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattner38905612008-02-19 04:36:25 +0000392 if (isa<IntegerType>(Ty) &&
393 ConstantInt::isValueValidForType(Ty, D.UConstPool64))
Reid Spencerb83eb642006-10-20 07:07:24 +0000394 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattner38905612008-02-19 04:36:25 +0000395
396 if (!isa<IntegerType>(Ty) ||
397 !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
398 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
399 "' is invalid or out of range for type '" +
400 Ty->getDescription() + "'");
401 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000402 }
Chris Lattner38905612008-02-19 04:36:25 +0000403 // This is really a signed reference. Transmogrify.
404 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner58af2a12006-02-15 07:22:58 +0000405
406 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattner38905612008-02-19 04:36:25 +0000407 if (!Ty->isFloatingPoint() ||
408 !ConstantFP::isValueValidForType(Ty, *D.ConstPoolFP)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000409 GenerateError("FP constant invalid for type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000410 return 0;
411 }
Dale Johannesenc72cd7e2007-09-11 18:33:39 +0000412 // Lexer has no type info, so builds all float and double FP constants
413 // as double. Fix this here. Long double does not need this.
414 if (&D.ConstPoolFP->getSemantics() == &APFloat::IEEEdouble &&
415 Ty==Type::FloatTy)
Dale Johannesen43421b32007-09-06 18:13:44 +0000416 D.ConstPoolFP->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
417 return ConstantFP::get(Ty, *D.ConstPoolFP);
Chris Lattner58af2a12006-02-15 07:22:58 +0000418
419 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000420 if (!isa<PointerType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000421 GenerateError("Cannot create a a non pointer null");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000422 return 0;
423 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000424 return ConstantPointerNull::get(cast<PointerType>(Ty));
425
426 case ValID::ConstUndefVal: // Is it an undef value?
427 return UndefValue::get(Ty);
428
429 case ValID::ConstZeroVal: // Is it a zero value?
430 return Constant::getNullValue(Ty);
431
432 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000433 if (D.ConstantValue->getType() != Ty) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000434 GenerateError("Constant expression type different from required type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000435 return 0;
436 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000437 return D.ConstantValue;
438
439 case ValID::InlineAsmVal: { // Inline asm expression
440 const PointerType *PTy = dyn_cast<PointerType>(Ty);
441 const FunctionType *FTy =
442 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000443 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000444 GenerateError("Invalid type for asm constraint string");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000445 return 0;
446 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000447 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
448 D.IAD->HasSideEffects);
449 D.destroy(); // Free InlineAsmDescriptor.
450 return IA;
451 }
452 default:
Reid Spencera9720f52007-02-05 17:04:00 +0000453 assert(0 && "Unhandled case!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000454 return 0;
455 } // End of switch
456
Reid Spencera9720f52007-02-05 17:04:00 +0000457 assert(0 && "Unhandled case!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000458 return 0;
459}
460
Reid Spencer93c40032007-03-19 18:40:50 +0000461// getVal - This function is identical to getExistingVal, except that if a
Chris Lattner58af2a12006-02-15 07:22:58 +0000462// value is not already defined, it "improvises" by creating a placeholder var
463// that looks and acts just like the requested variable. When the value is
464// defined later, all uses of the placeholder variable are replaced with the
465// real thing.
466//
467static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000468 if (Ty == Type::LabelTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000469 GenerateError("Cannot use a basic block here");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000470 return 0;
471 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000472
473 // See if the value has already been defined.
Reid Spencer93c40032007-03-19 18:40:50 +0000474 Value *V = getExistingVal(Ty, ID);
Chris Lattner58af2a12006-02-15 07:22:58 +0000475 if (V) return V;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000476 if (TriggerError) return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000477
Reid Spencer5b7e7532006-09-28 19:28:24 +0000478 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000479 GenerateError("Invalid use of a composite type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000480 return 0;
481 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000482
483 // If we reached here, we referenced either a symbol that we don't know about
484 // or an id number that hasn't been read yet. We may be referencing something
485 // forward, so just create an entry to be resolved later and get to it...
486 //
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000487 switch (ID.Type) {
488 case ValID::GlobalName:
Reid Spencer9c9b63a2007-04-28 16:07:31 +0000489 case ValID::GlobalID: {
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000490 const PointerType *PTy = dyn_cast<PointerType>(Ty);
491 if (!PTy) {
492 GenerateError("Invalid type for reference to global" );
493 return 0;
494 }
495 const Type* ElTy = PTy->getElementType();
496 if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy))
497 V = new Function(FTy, GlobalValue::ExternalLinkage);
498 else
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000499 V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage, 0, "",
500 (Module*)0, false, PTy->getAddressSpace());
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000501 break;
Reid Spencer9c9b63a2007-04-28 16:07:31 +0000502 }
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000503 default:
504 V = new Argument(Ty);
505 }
506
Chris Lattner58af2a12006-02-15 07:22:58 +0000507 // Remember where this forward reference came from. FIXME, shouldn't we try
508 // to recycle these things??
509 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Duncan Sandsdc024672007-11-27 13:23:08 +0000510 LLLgetLineNo())));
Chris Lattner58af2a12006-02-15 07:22:58 +0000511
512 if (inFunctionScope())
513 InsertValue(V, CurFun.LateResolveValues);
514 else
515 InsertValue(V, CurModule.LateResolveValues);
516 return V;
517}
518
Reid Spencer93c40032007-03-19 18:40:50 +0000519/// defineBBVal - This is a definition of a new basic block with the specified
520/// identifier which must be the same as CurFun.NextValNum, if its numeric.
Devang Patel67909432008-03-03 18:58:47 +0000521static BasicBlock *defineBBVal(const ValID &ID, BasicBlock *unwindDest) {
Reid Spencera9720f52007-02-05 17:04:00 +0000522 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000523
Chris Lattner58af2a12006-02-15 07:22:58 +0000524 BasicBlock *BB = 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000525
Reid Spencer93c40032007-03-19 18:40:50 +0000526 // First, see if this was forward referenced
Chris Lattner58af2a12006-02-15 07:22:58 +0000527
Reid Spencer93c40032007-03-19 18:40:50 +0000528 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
529 if (BBI != CurFun.BBForwardRefs.end()) {
530 BB = BBI->second;
Chris Lattner58af2a12006-02-15 07:22:58 +0000531 // The forward declaration could have been inserted anywhere in the
532 // function: insert it into the correct place now.
533 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
534 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Reid Spencer93c40032007-03-19 18:40:50 +0000535
Reid Spencer66728ef2007-03-20 01:13:36 +0000536 // We're about to erase the entry, save the key so we can clean it up.
537 ValID Tmp = BBI->first;
538
Reid Spencer93c40032007-03-19 18:40:50 +0000539 // Erase the forward ref from the map as its no longer "forward"
540 CurFun.BBForwardRefs.erase(ID);
541
Reid Spencer66728ef2007-03-20 01:13:36 +0000542 // The key has been removed from the map but so we don't want to leave
543 // strdup'd memory around so destroy it too.
544 Tmp.destroy();
545
Reid Spencer93c40032007-03-19 18:40:50 +0000546 // If its a numbered definition, bump the number and set the BB value.
547 if (ID.Type == ValID::LocalID) {
548 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
549 InsertValue(BB);
550 }
Devang Patel67909432008-03-03 18:58:47 +0000551 } else {
552 // We haven't seen this BB before and its first mention is a definition.
553 // Just create it and return it.
554 std::string Name (ID.Type == ValID::LocalName ? ID.getName() : "");
555 BB = new BasicBlock(Name, CurFun.CurrentFunction);
556 if (ID.Type == ValID::LocalID) {
557 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
558 InsertValue(BB);
559 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000560 }
Reid Spencer93c40032007-03-19 18:40:50 +0000561
Devang Patel67909432008-03-03 18:58:47 +0000562 ID.destroy();
563 BB->setUnwindDest(unwindDest);
Reid Spencer93c40032007-03-19 18:40:50 +0000564 return BB;
565}
566
567/// getBBVal - get an existing BB value or create a forward reference for it.
568///
569static BasicBlock *getBBVal(const ValID &ID) {
570 assert(inFunctionScope() && "Can't get basic block at global scope!");
571
572 BasicBlock *BB = 0;
573
574 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
575 if (BBI != CurFun.BBForwardRefs.end()) {
576 BB = BBI->second;
577 } if (ID.Type == ValID::LocalName) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000578 std::string Name = ID.getName();
Reid Spencer93c40032007-03-19 18:40:50 +0000579 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000580 if (N) {
Reid Spencer93c40032007-03-19 18:40:50 +0000581 if (N->getType()->getTypeID() == Type::LabelTyID)
582 BB = cast<BasicBlock>(N);
583 else
584 GenerateError("Reference to label '" + Name + "' is actually of type '"+
585 N->getType()->getDescription() + "'");
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000586 }
Reid Spencer93c40032007-03-19 18:40:50 +0000587 } else if (ID.Type == ValID::LocalID) {
588 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
589 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
590 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
591 else
592 GenerateError("Reference to label '%" + utostr(ID.Num) +
593 "' is actually of type '"+
594 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
595 }
596 } else {
597 GenerateError("Illegal label reference " + ID.getName());
598 return 0;
599 }
600
601 // If its already been defined, return it now.
602 if (BB) {
603 ID.destroy(); // Free strdup'd memory.
604 return BB;
605 }
606
607 // Otherwise, this block has not been seen before, create it.
608 std::string Name;
609 if (ID.Type == ValID::LocalName)
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000610 Name = ID.getName();
Reid Spencer93c40032007-03-19 18:40:50 +0000611 BB = new BasicBlock(Name, CurFun.CurrentFunction);
612
613 // Insert it in the forward refs map.
614 CurFun.BBForwardRefs[ID] = BB;
615
Chris Lattner58af2a12006-02-15 07:22:58 +0000616 return BB;
617}
618
619
620//===----------------------------------------------------------------------===//
621// Code to handle forward references in instructions
622//===----------------------------------------------------------------------===//
623//
624// This code handles the late binding needed with statements that reference
625// values not defined yet... for example, a forward branch, or the PHI node for
626// a loop body.
627//
628// This keeps a table (CurFun.LateResolveValues) of all such forward references
629// and back patchs after we are done.
630//
631
632// ResolveDefinitions - If we could not resolve some defs at parsing
633// time (forward branches, phi functions for loops, etc...) resolve the
634// defs now...
635//
636static void
Reid Spencer93c40032007-03-19 18:40:50 +0000637ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000638 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencer93c40032007-03-19 18:40:50 +0000639 while (!LateResolvers.empty()) {
640 Value *V = LateResolvers.back();
641 LateResolvers.pop_back();
Chris Lattner58af2a12006-02-15 07:22:58 +0000642
Reid Spencer93c40032007-03-19 18:40:50 +0000643 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
644 CurModule.PlaceHolderInfo.find(V);
645 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000646
Reid Spencer93c40032007-03-19 18:40:50 +0000647 ValID &DID = PHI->second.first;
Chris Lattner58af2a12006-02-15 07:22:58 +0000648
Reid Spencer93c40032007-03-19 18:40:50 +0000649 Value *TheRealValue = getExistingVal(V->getType(), DID);
650 if (TriggerError)
651 return;
652 if (TheRealValue) {
653 V->replaceAllUsesWith(TheRealValue);
654 delete V;
655 CurModule.PlaceHolderInfo.erase(PHI);
656 } else if (FutureLateResolvers) {
657 // Functions have their unresolved items forwarded to the module late
658 // resolver table
659 InsertValue(V, *FutureLateResolvers);
660 } else {
661 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
662 GenerateError("Reference to an invalid definition: '" +DID.getName()+
663 "' of type '" + V->getType()->getDescription() + "'",
664 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000665 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000666 } else {
Reid Spencer93c40032007-03-19 18:40:50 +0000667 GenerateError("Reference to an invalid definition: #" +
668 itostr(DID.Num) + " of type '" +
669 V->getType()->getDescription() + "'",
670 PHI->second.second);
671 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000672 }
673 }
674 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000675 LateResolvers.clear();
676}
677
678// ResolveTypeTo - A brand new type was just declared. This means that (if
679// name is not null) things referencing Name can be resolved. Otherwise, things
680// refering to the number can be resolved. Do this now.
681//
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000682static void ResolveTypeTo(std::string *Name, const Type *ToTy) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000683 ValID D;
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000684 if (Name)
685 D = ValID::createLocalName(*Name);
686 else
687 D = ValID::createLocalID(CurModule.Types.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000688
Reid Spencer861d9d62006-11-28 07:29:44 +0000689 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattner58af2a12006-02-15 07:22:58 +0000690 CurModule.LateResolveTypes.find(D);
691 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencer861d9d62006-11-28 07:29:44 +0000692 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner58af2a12006-02-15 07:22:58 +0000693 CurModule.LateResolveTypes.erase(I);
694 }
695}
696
697// setValueName - Set the specified value to the name given. The name may be
698// null potentially, in which case this is a noop. The string passed in is
699// assumed to be a malloc'd string buffer, and is free'd by this function.
700//
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000701static void setValueName(Value *V, std::string *NameStr) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000702 if (!NameStr) return;
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000703 std::string Name(*NameStr); // Copy string
704 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000705
Reid Spencer41dff5e2007-01-26 08:05:27 +0000706 if (V->getType() == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000707 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000708 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000709 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000710
Reid Spencera9720f52007-02-05 17:04:00 +0000711 assert(inFunctionScope() && "Must be in function scope!");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000712 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
713 if (ST.lookup(Name)) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000714 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000715 V->getType()->getDescription() + "'");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000716 return;
717 }
718
719 // Set the name.
720 V->setName(Name);
Chris Lattner58af2a12006-02-15 07:22:58 +0000721}
722
723/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
724/// this is a declaration, otherwise it is a definition.
725static GlobalVariable *
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000726ParseGlobalVariable(std::string *NameStr,
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000727 GlobalValue::LinkageTypes Linkage,
728 GlobalValue::VisibilityTypes Visibility,
Chris Lattner58af2a12006-02-15 07:22:58 +0000729 bool isConstantGlobal, const Type *Ty,
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000730 Constant *Initializer, bool IsThreadLocal,
731 unsigned AddressSpace = 0) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000732 if (isa<FunctionType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000733 GenerateError("Cannot declare global vars of function type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000734 return 0;
735 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000736
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000737 const PointerType *PTy = PointerType::get(Ty, AddressSpace);
Chris Lattner58af2a12006-02-15 07:22:58 +0000738
739 std::string Name;
740 if (NameStr) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000741 Name = *NameStr; // Copy string
742 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000743 }
744
745 // See if this global value was forward referenced. If so, recycle the
746 // object.
747 ValID ID;
748 if (!Name.empty()) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000749 ID = ValID::createGlobalName(Name);
Chris Lattner58af2a12006-02-15 07:22:58 +0000750 } else {
Reid Spencer93c40032007-03-19 18:40:50 +0000751 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000752 }
753
754 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
755 // Move the global to the end of the list, from whereever it was
756 // previously inserted.
757 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
758 CurModule.CurrentModule->getGlobalList().remove(GV);
759 CurModule.CurrentModule->getGlobalList().push_back(GV);
760 GV->setInitializer(Initializer);
761 GV->setLinkage(Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000762 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000763 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000764 GV->setThreadLocal(IsThreadLocal);
Chris Lattner58af2a12006-02-15 07:22:58 +0000765 InsertValue(GV, CurModule.Values);
766 return GV;
767 }
768
Reid Spenceref9b9a72007-02-05 20:47:22 +0000769 // If this global has a name
Chris Lattner58af2a12006-02-15 07:22:58 +0000770 if (!Name.empty()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000771 // if the global we're parsing has an initializer (is a definition) and
772 // has external linkage.
773 if (Initializer && Linkage != GlobalValue::InternalLinkage)
774 // If there is already a global with external linkage with this name
775 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
776 // If we allow this GVar to get created, it will be renamed in the
777 // symbol table because it conflicts with an existing GVar. We can't
778 // allow redefinition of GVars whose linking indicates that their name
779 // must stay the same. Issue the error.
780 GenerateError("Redefinition of global variable named '" + Name +
781 "' of type '" + Ty->getDescription() + "'");
782 return 0;
783 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000784 }
785
786 // Otherwise there is no existing GV to use, create one now.
787 GlobalVariable *GV =
788 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000789 CurModule.CurrentModule, IsThreadLocal, AddressSpace);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000790 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000791 InsertValue(GV, CurModule.Values);
792 return GV;
793}
794
795// setTypeName - Set the specified type to the name given. The name may be
796// null potentially, in which case this is a noop. The string passed in is
797// assumed to be a malloc'd string buffer, and is freed by this function.
798//
799// This function returns true if the type has already been defined, but is
800// allowed to be redefined in the specified context. If the name is a new name
801// for the type plane, it is inserted and false is returned.
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000802static bool setTypeName(const Type *T, std::string *NameStr) {
Reid Spencera9720f52007-02-05 17:04:00 +0000803 assert(!inFunctionScope() && "Can't give types function-local names!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000804 if (NameStr == 0) return false;
805
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000806 std::string Name(*NameStr); // Copy string
807 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000808
809 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000810 if (T == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000811 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000812 return false;
813 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000814
815 // Set the type name, checking for conflicts as we do so.
816 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
817
818 if (AlreadyExists) { // Inserting a name that is already defined???
819 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencera9720f52007-02-05 17:04:00 +0000820 assert(Existing && "Conflict but no matching type?!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000821
822 // There is only one case where this is allowed: when we are refining an
823 // opaque type. In this case, Existing will be an opaque type.
824 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
825 // We ARE replacing an opaque type!
826 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
827 return true;
828 }
829
830 // Otherwise, this is an attempt to redefine a type. That's okay if
831 // the redefinition is identical to the original. This will be so if
832 // Existing and T point to the same Type object. In this one case we
833 // allow the equivalent redefinition.
834 if (Existing == T) return true; // Yes, it's equal.
835
836 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer63c34452007-01-05 21:51:07 +0000837 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000838 T->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +0000839 }
840
841 return false;
842}
843
844//===----------------------------------------------------------------------===//
845// Code for handling upreferences in type names...
846//
847
848// TypeContains - Returns true if Ty directly contains E in it.
849//
850static bool TypeContains(const Type *Ty, const Type *E) {
851 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
852 E) != Ty->subtype_end();
853}
854
855namespace {
856 struct UpRefRecord {
857 // NestingLevel - The number of nesting levels that need to be popped before
858 // this type is resolved.
859 unsigned NestingLevel;
860
861 // LastContainedTy - This is the type at the current binding level for the
862 // type. Every time we reduce the nesting level, this gets updated.
863 const Type *LastContainedTy;
864
865 // UpRefTy - This is the actual opaque type that the upreference is
866 // represented with.
867 OpaqueType *UpRefTy;
868
869 UpRefRecord(unsigned NL, OpaqueType *URTy)
870 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
871 };
872}
873
874// UpRefs - A list of the outstanding upreferences that need to be resolved.
875static std::vector<UpRefRecord> UpRefs;
876
877/// HandleUpRefs - Every time we finish a new layer of types, this function is
878/// called. It loops through the UpRefs vector, which is a list of the
879/// currently active types. For each type, if the up reference is contained in
880/// the newly completed type, we decrement the level count. When the level
881/// count reaches zero, the upreferenced type is the type that is passed in:
882/// thus we can complete the cycle.
883///
884static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner224f84f2006-08-18 17:34:45 +0000885 // If Ty isn't abstract, or if there are no up-references in it, then there is
886 // nothing to resolve here.
887 if (!ty->isAbstract() || UpRefs.empty()) return ty;
888
Chris Lattner58af2a12006-02-15 07:22:58 +0000889 PATypeHolder Ty(ty);
890 UR_OUT("Type '" << Ty->getDescription() <<
891 "' newly formed. Resolving upreferences.\n" <<
892 UpRefs.size() << " upreferences active!\n");
893
894 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
895 // to zero), we resolve them all together before we resolve them to Ty. At
896 // the end of the loop, if there is anything to resolve to Ty, it will be in
897 // this variable.
898 OpaqueType *TypeToResolve = 0;
899
900 for (unsigned i = 0; i != UpRefs.size(); ++i) {
901 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
902 << UpRefs[i].second->getDescription() << ") = "
903 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
904 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
905 // Decrement level of upreference
906 unsigned Level = --UpRefs[i].NestingLevel;
907 UpRefs[i].LastContainedTy = Ty;
908 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
909 if (Level == 0) { // Upreference should be resolved!
910 if (!TypeToResolve) {
911 TypeToResolve = UpRefs[i].UpRefTy;
912 } else {
913 UR_OUT(" * Resolving upreference for "
914 << UpRefs[i].second->getDescription() << "\n";
915 std::string OldName = UpRefs[i].UpRefTy->getDescription());
916 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
917 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
918 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
919 }
920 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
921 --i; // Do not skip the next element...
922 }
923 }
924 }
925
926 if (TypeToResolve) {
927 UR_OUT(" * Resolving upreference for "
928 << UpRefs[i].second->getDescription() << "\n";
929 std::string OldName = TypeToResolve->getDescription());
930 TypeToResolve->refineAbstractTypeTo(Ty);
931 }
932
933 return Ty;
934}
935
Chris Lattner58af2a12006-02-15 07:22:58 +0000936//===----------------------------------------------------------------------===//
937// RunVMAsmParser - Define an interface to this parser
938//===----------------------------------------------------------------------===//
939//
Reid Spencer14310612006-12-31 05:40:51 +0000940static Module* RunParser(Module * M);
941
Duncan Sandsdc024672007-11-27 13:23:08 +0000942Module *llvm::RunVMAsmParser(llvm::MemoryBuffer *MB) {
943 InitLLLexer(MB);
944 Module *M = RunParser(new Module(LLLgetFilename()));
945 FreeLexer();
946 return M;
Chris Lattner58af2a12006-02-15 07:22:58 +0000947}
948
949%}
950
951%union {
952 llvm::Module *ModuleVal;
953 llvm::Function *FunctionVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000954 llvm::BasicBlock *BasicBlockVal;
955 llvm::TerminatorInst *TermInstVal;
956 llvm::Instruction *InstVal;
Reid Spencera132e042006-12-03 05:46:11 +0000957 llvm::Constant *ConstVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000958
Reid Spencera132e042006-12-03 05:46:11 +0000959 const llvm::Type *PrimType;
Reid Spencer14310612006-12-31 05:40:51 +0000960 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencera132e042006-12-03 05:46:11 +0000961 llvm::PATypeHolder *TypeVal;
962 llvm::Value *ValueVal;
Reid Spencera132e042006-12-03 05:46:11 +0000963 std::vector<llvm::Value*> *ValueList;
Reid Spencer14310612006-12-31 05:40:51 +0000964 llvm::ArgListType *ArgList;
965 llvm::TypeWithAttrs TypeWithAttrs;
966 llvm::TypeWithAttrsList *TypeWithAttrsList;
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000967 llvm::ParamList *ParamList;
Reid Spencer14310612006-12-31 05:40:51 +0000968
Chris Lattner58af2a12006-02-15 07:22:58 +0000969 // Represent the RHS of PHI node
Reid Spencera132e042006-12-03 05:46:11 +0000970 std::list<std::pair<llvm::Value*,
971 llvm::BasicBlock*> > *PHIList;
Chris Lattner58af2a12006-02-15 07:22:58 +0000972 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencera132e042006-12-03 05:46:11 +0000973 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner58af2a12006-02-15 07:22:58 +0000974
975 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000976 llvm::GlobalValue::VisibilityTypes Visibility;
Dale Johannesen222ebf72008-02-19 21:40:51 +0000977 llvm::ParameterAttributes ParamAttrs;
Reid Spencer38c91a92007-02-28 02:24:54 +0000978 llvm::APInt *APIntVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000979 int64_t SInt64Val;
980 uint64_t UInt64Val;
981 int SIntVal;
982 unsigned UIntVal;
Dale Johannesen43421b32007-09-06 18:13:44 +0000983 llvm::APFloat *FPVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000984 bool BoolVal;
985
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000986 std::string *StrVal; // This memory must be deleted
987 llvm::ValID ValIDVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000988
Reid Spencera132e042006-12-03 05:46:11 +0000989 llvm::Instruction::BinaryOps BinaryOpVal;
990 llvm::Instruction::TermOps TermOpVal;
991 llvm::Instruction::MemoryOps MemOpVal;
992 llvm::Instruction::CastOps CastOpVal;
993 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencera132e042006-12-03 05:46:11 +0000994 llvm::ICmpInst::Predicate IPredicate;
995 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner58af2a12006-02-15 07:22:58 +0000996}
997
Reid Spencer14310612006-12-31 05:40:51 +0000998%type <ModuleVal> Module
Chris Lattner58af2a12006-02-15 07:22:58 +0000999%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
1000%type <BasicBlockVal> BasicBlock InstructionList
1001%type <TermInstVal> BBTerminatorInst
1002%type <InstVal> Inst InstVal MemoryInst
Anton Korobeynikov38e09802007-04-28 13:48:45 +00001003%type <ConstVal> ConstVal ConstExpr AliaseeRef
Chris Lattner58af2a12006-02-15 07:22:58 +00001004%type <ConstVector> ConstVector
1005%type <ArgList> ArgList ArgListH
Chris Lattner58af2a12006-02-15 07:22:58 +00001006%type <PHIList> PHIList
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001007%type <ParamList> ParamList // For call param lists & GEP indices
Reid Spencer14310612006-12-31 05:40:51 +00001008%type <ValueList> IndexList // For GEP indices
1009%type <TypeList> TypeListI
1010%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer218ded22007-01-05 17:07:23 +00001011%type <TypeWithAttrs> ArgType
Chris Lattner58af2a12006-02-15 07:22:58 +00001012%type <JumpTable> JumpTable
1013%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001014%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattner58af2a12006-02-15 07:22:58 +00001015%type <BoolVal> OptVolatile // 'volatile' or not
1016%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1017%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencer14310612006-12-31 05:40:51 +00001018%type <Linkage> GVInternalLinkage GVExternalLinkage
1019%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001020%type <Linkage> AliasLinkage
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001021%type <Visibility> GVVisibilityStyle
Chris Lattner58af2a12006-02-15 07:22:58 +00001022
1023// ValueRef - Unresolved reference to a definition or BB
1024%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1025%type <ValueVal> ResolvedVal // <type> <valref> pair
Devang Patel7990dc72008-02-20 22:40:23 +00001026%type <ValueList> ReturnedVal
Chris Lattner58af2a12006-02-15 07:22:58 +00001027// Tokens and types for handling constant integer values
1028//
1029// ESINT64VAL - A negative number within long long range
1030%token <SInt64Val> ESINT64VAL
1031
1032// EUINT64VAL - A positive number within uns. long long range
1033%token <UInt64Val> EUINT64VAL
Chris Lattner58af2a12006-02-15 07:22:58 +00001034
Reid Spencer38c91a92007-02-28 02:24:54 +00001035// ESAPINTVAL - A negative number with arbitrary precision
1036%token <APIntVal> ESAPINTVAL
1037
1038// EUAPINTVAL - A positive number with arbitrary precision
1039%token <APIntVal> EUAPINTVAL
1040
Reid Spencer41dff5e2007-01-26 08:05:27 +00001041%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattner58af2a12006-02-15 07:22:58 +00001042%token <FPVal> FPVAL // Float or Double constant
1043
1044// Built in types...
Reid Spencer218ded22007-01-05 17:07:23 +00001045%type <TypeVal> Types ResultTypes
Reid Spencer14310612006-12-31 05:40:51 +00001046%type <PrimType> IntType FPType PrimType // Classifications
Reid Spencer6f407902007-01-13 05:00:46 +00001047%token <PrimType> VOID INTTYPE
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001048%token <PrimType> FLOAT DOUBLE X86_FP80 FP128 PPC_FP128 LABEL
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001049%token TYPE
Chris Lattner58af2a12006-02-15 07:22:58 +00001050
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001051
Reid Spencered951ea2007-05-19 07:22:10 +00001052%token<StrVal> LOCALVAR GLOBALVAR LABELSTR
1053%token<StrVal> STRINGCONSTANT ATSTRINGCONSTANT PCTSTRINGCONSTANT
Reid Spencer41dff5e2007-01-26 08:05:27 +00001054%type <StrVal> LocalName OptLocalName OptLocalAssign
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001055%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001056%type <StrVal> OptSection SectionString OptGC
Chris Lattner58af2a12006-02-15 07:22:58 +00001057
Christopher Lambbf3348d2007-12-12 08:45:45 +00001058%type <UIntVal> OptAlign OptCAlign OptAddrSpace
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001059
Reid Spencer3d6b71e2007-04-09 01:56:05 +00001060%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001061%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
Reid Spencer14310612006-12-31 05:40:51 +00001062%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001063%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001064%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN ADDRSPACE
Chris Lattner58af2a12006-02-15 07:22:58 +00001065%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001066%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Nick Lewycky7e93e162008-03-10 05:01:34 +00001067%token DATALAYOUT UNWINDS
Chris Lattner58af2a12006-02-15 07:22:58 +00001068%type <UIntVal> OptCallingConv
Reid Spencer218ded22007-01-05 17:07:23 +00001069%type <ParamAttrs> OptParamAttrs ParamAttr
1070%type <ParamAttrs> OptFuncAttrs FuncAttr
Chris Lattner58af2a12006-02-15 07:22:58 +00001071
1072// Basic Block Terminating Operators
1073%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
1074
1075// Binary Operators
Reid Spencere4d87aa2006-12-23 06:05:41 +00001076%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencer3ed469c2006-11-02 20:25:50 +00001077%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer832254e2007-02-02 02:16:23 +00001078%token <BinaryOpVal> SHL LSHR ASHR
1079
Reid Spencera132e042006-12-03 05:46:11 +00001080%token <OtherOpVal> ICMP FCMP
Reid Spencera132e042006-12-03 05:46:11 +00001081%type <IPredicate> IPredicates
Reid Spencera132e042006-12-03 05:46:11 +00001082%type <FPredicate> FPredicates
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001083%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1084%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner58af2a12006-02-15 07:22:58 +00001085
1086// Memory Instructions
1087%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1088
Reid Spencer3da59db2006-11-27 01:05:10 +00001089// Cast Operators
1090%type <CastOpVal> CastOps
1091%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1092%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1093
Chris Lattner58af2a12006-02-15 07:22:58 +00001094// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001095%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattnerd5efe842006-04-08 01:18:56 +00001096%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Devang Patel5a970972008-02-19 22:27:01 +00001097%token <OtherOpVal> GETRESULT
Chris Lattner58af2a12006-02-15 07:22:58 +00001098
Reid Spencer218ded22007-01-05 17:07:23 +00001099// Function Attributes
Reid Spencerb8f85052007-07-31 03:50:36 +00001100%token SIGNEXT ZEROEXT NORETURN INREG SRET NOUNWIND NOALIAS BYVAL NEST
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001101%token READNONE READONLY GC
Chris Lattner58af2a12006-02-15 07:22:58 +00001102
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001103// Visibility Styles
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001104%token DEFAULT HIDDEN PROTECTED
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001105
Chris Lattner58af2a12006-02-15 07:22:58 +00001106%start Module
1107%%
1108
Chris Lattner58af2a12006-02-15 07:22:58 +00001109
Chris Lattner58af2a12006-02-15 07:22:58 +00001110// Operations that are notably excluded from this list include:
1111// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1112//
Reid Spencer3ed469c2006-11-02 20:25:50 +00001113ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer832254e2007-02-02 02:16:23 +00001114LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Reid Spencer3da59db2006-11-27 01:05:10 +00001115CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1116 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer832254e2007-02-02 02:16:23 +00001117
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001118IPredicates
Reid Spencer4012e832006-12-04 05:24:24 +00001119 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001120 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1121 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1122 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1123 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1124 ;
1125
1126FPredicates
1127 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1128 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1129 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1130 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1131 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1132 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1133 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1134 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1135 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1136 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001137
1138// These are some types that allow classification if we only want a particular
1139// thing... for example, only a signed, unsigned, or integral type.
Reid Spencera54b7cb2007-01-12 07:05:14 +00001140IntType : INTTYPE;
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001141FPType : FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80;
Chris Lattner58af2a12006-02-15 07:22:58 +00001142
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001143LocalName : LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001144OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1145
Christopher Lambbf3348d2007-12-12 08:45:45 +00001146OptAddrSpace : ADDRSPACE '(' EUINT64VAL ')' { $$=$3; }
1147 | /*empty*/ { $$=0; };
1148
Reid Spencer41dff5e2007-01-26 08:05:27 +00001149/// OptLocalAssign - Value producing statements have an optional assignment
1150/// component.
1151OptLocalAssign : LocalName '=' {
1152 $$ = $1;
1153 CHECK_FOR_ERROR
1154 }
1155 | /*empty*/ {
1156 $$ = 0;
1157 CHECK_FOR_ERROR
1158 };
1159
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001160GlobalName : GLOBALVAR | ATSTRINGCONSTANT ;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001161
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001162OptGlobalAssign : GlobalAssign
Chris Lattner58af2a12006-02-15 07:22:58 +00001163 | /*empty*/ {
1164 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001165 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001166 };
1167
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001168GlobalAssign : GlobalName '=' {
1169 $$ = $1;
1170 CHECK_FOR_ERROR
Chris Lattner6cdc6822007-04-26 05:31:05 +00001171 };
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001172
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001173GVInternalLinkage
1174 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1175 | WEAK { $$ = GlobalValue::WeakLinkage; }
1176 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1177 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1178 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1179 ;
1180
1181GVExternalLinkage
1182 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1183 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1184 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1185 ;
1186
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001187GVVisibilityStyle
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001188 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1189 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1190 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1191 | PROTECTED { $$ = GlobalValue::ProtectedVisibility; }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001192 ;
1193
Reid Spencer14310612006-12-31 05:40:51 +00001194FunctionDeclareLinkage
1195 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1196 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1197 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001198 ;
1199
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001200FunctionDefineLinkage
Reid Spencer14310612006-12-31 05:40:51 +00001201 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1202 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001203 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1204 | WEAK { $$ = GlobalValue::WeakLinkage; }
1205 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001206 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001207
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001208AliasLinkage
1209 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1210 | WEAK { $$ = GlobalValue::WeakLinkage; }
1211 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1212 ;
1213
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001214OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1215 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001216 FASTCC_TOK { $$ = CallingConv::Fast; } |
1217 COLDCC_TOK { $$ = CallingConv::Cold; } |
1218 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1219 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1220 CC_TOK EUINT64VAL {
Chris Lattner58af2a12006-02-15 07:22:58 +00001221 if ((unsigned)$2 != $2)
Reid Spencerb5334b02007-02-05 10:18:06 +00001222 GEN_ERROR("Calling conv too large");
Chris Lattner58af2a12006-02-15 07:22:58 +00001223 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001224 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001225 };
1226
Reid Spencerb8f85052007-07-31 03:50:36 +00001227ParamAttr : ZEROEXT { $$ = ParamAttr::ZExt; }
1228 | ZEXT { $$ = ParamAttr::ZExt; }
1229 | SIGNEXT { $$ = ParamAttr::SExt; }
Chris Lattnerce5f24e2007-07-05 17:26:49 +00001230 | SEXT { $$ = ParamAttr::SExt; }
1231 | INREG { $$ = ParamAttr::InReg; }
1232 | SRET { $$ = ParamAttr::StructRet; }
1233 | NOALIAS { $$ = ParamAttr::NoAlias; }
Reid Spencerb8f85052007-07-31 03:50:36 +00001234 | BYVAL { $$ = ParamAttr::ByVal; }
1235 | NEST { $$ = ParamAttr::Nest; }
Dale Johannesendc6c0f12008-02-22 17:50:51 +00001236 | ALIGN EUINT64VAL { $$ =
1237 ParamAttr::constructAlignmentFromInt($2); }
Reid Spencer14310612006-12-31 05:40:51 +00001238 ;
1239
Reid Spencer18da0722007-04-11 02:44:20 +00001240OptParamAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer218ded22007-01-05 17:07:23 +00001241 | OptParamAttrs ParamAttr {
Reid Spencer7b5d4662007-04-09 06:16:21 +00001242 $$ = $1 | $2;
Reid Spencer14310612006-12-31 05:40:51 +00001243 }
1244 ;
1245
Reid Spencer18da0722007-04-11 02:44:20 +00001246FuncAttr : NORETURN { $$ = ParamAttr::NoReturn; }
1247 | NOUNWIND { $$ = ParamAttr::NoUnwind; }
Reid Spencerb8f85052007-07-31 03:50:36 +00001248 | ZEROEXT { $$ = ParamAttr::ZExt; }
1249 | SIGNEXT { $$ = ParamAttr::SExt; }
Duncan Sandsdc024672007-11-27 13:23:08 +00001250 | READNONE { $$ = ParamAttr::ReadNone; }
1251 | READONLY { $$ = ParamAttr::ReadOnly; }
Reid Spencer218ded22007-01-05 17:07:23 +00001252 ;
1253
Reid Spencer18da0722007-04-11 02:44:20 +00001254OptFuncAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer218ded22007-01-05 17:07:23 +00001255 | OptFuncAttrs FuncAttr {
Reid Spencer7b5d4662007-04-09 06:16:21 +00001256 $$ = $1 | $2;
Reid Spencer218ded22007-01-05 17:07:23 +00001257 }
Reid Spencer14310612006-12-31 05:40:51 +00001258 ;
1259
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001260OptGC : /* empty */ { $$ = 0; }
1261 | GC STRINGCONSTANT {
1262 $$ = $2;
1263 }
1264 ;
1265
Chris Lattner58af2a12006-02-15 07:22:58 +00001266// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1267// a comma before it.
1268OptAlign : /*empty*/ { $$ = 0; } |
1269 ALIGN EUINT64VAL {
1270 $$ = $2;
1271 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001272 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001273 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001274};
1275OptCAlign : /*empty*/ { $$ = 0; } |
1276 ',' ALIGN EUINT64VAL {
1277 $$ = $3;
1278 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001279 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001280 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001281};
1282
1283
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001284
Chris Lattner58af2a12006-02-15 07:22:58 +00001285SectionString : SECTION STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001286 for (unsigned i = 0, e = $2->length(); i != e; ++i)
1287 if ((*$2)[i] == '"' || (*$2)[i] == '\\')
Reid Spencerb5334b02007-02-05 10:18:06 +00001288 GEN_ERROR("Invalid character in section name");
Chris Lattner58af2a12006-02-15 07:22:58 +00001289 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001290 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001291};
1292
1293OptSection : /*empty*/ { $$ = 0; } |
1294 SectionString { $$ = $1; };
1295
1296// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1297// is set to be the global we are processing.
1298//
1299GlobalVarAttributes : /* empty */ {} |
1300 ',' GlobalVarAttribute GlobalVarAttributes {};
1301GlobalVarAttribute : SectionString {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001302 CurGV->setSection(*$1);
1303 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001304 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001305 }
1306 | ALIGN EUINT64VAL {
1307 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001308 GEN_ERROR("Alignment must be a power of two");
Chris Lattner58af2a12006-02-15 07:22:58 +00001309 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001310 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001311 };
1312
1313//===----------------------------------------------------------------------===//
1314// Types includes all predefined types... except void, because it can only be
Reid Spencer14310612006-12-31 05:40:51 +00001315// used in specific contexts (function returning void for example).
Chris Lattner58af2a12006-02-15 07:22:58 +00001316
1317// Derived types are added later...
1318//
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001319PrimType : INTTYPE | FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80 | LABEL ;
Reid Spencer14310612006-12-31 05:40:51 +00001320
1321Types
1322 : OPAQUE {
Reid Spencera132e042006-12-03 05:46:11 +00001323 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001324 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001325 }
1326 | PrimType {
Reid Spencera132e042006-12-03 05:46:11 +00001327 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001328 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00001329 }
Christopher Lambbf3348d2007-12-12 08:45:45 +00001330 | Types OptAddrSpace '*' { // Pointer type?
Reid Spencer14310612006-12-31 05:40:51 +00001331 if (*$1 == Type::LabelTy)
1332 GEN_ERROR("Cannot form a pointer to a basic block");
Christopher Lambbf3348d2007-12-12 08:45:45 +00001333 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1, $2)));
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001334 delete $1;
1335 CHECK_FOR_ERROR
1336 }
Reid Spencer14310612006-12-31 05:40:51 +00001337 | SymbolicValueRef { // Named types are also simple types...
1338 const Type* tmp = getTypeVal($1);
1339 CHECK_FOR_ERROR
1340 $$ = new PATypeHolder(tmp);
1341 }
1342 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerb5334b02007-02-05 10:18:06 +00001343 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattner58af2a12006-02-15 07:22:58 +00001344 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1345 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencera132e042006-12-03 05:46:11 +00001346 $$ = new PATypeHolder(OT);
Chris Lattner58af2a12006-02-15 07:22:58 +00001347 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001348 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001349 }
Reid Spencer218ded22007-01-05 17:07:23 +00001350 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001351 // Allow but ignore attributes on function types; this permits auto-upgrade.
1352 // FIXME: remove in LLVM 3.0.
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001353 const Type* RetTy = *$1;
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001354 if (!(RetTy->isFirstClassType() || RetTy == Type::VoidTy ||
1355 isa<OpaqueType>(RetTy)))
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001356 GEN_ERROR("LLVM Functions cannot return aggregates");
1357
Chris Lattner58af2a12006-02-15 07:22:58 +00001358 std::vector<const Type*> Params;
Reid Spencer7b5d4662007-04-09 06:16:21 +00001359 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001360 for (; I != E; ++I ) {
Reid Spencer66728ef2007-03-20 01:13:36 +00001361 const Type *Ty = I->Ty->get();
Reid Spencer66728ef2007-03-20 01:13:36 +00001362 Params.push_back(Ty);
Reid Spencer14310612006-12-31 05:40:51 +00001363 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001364
Chris Lattner58af2a12006-02-15 07:22:58 +00001365 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1366 if (isVarArg) Params.pop_back();
1367
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001368 for (unsigned i = 0; i != Params.size(); ++i)
1369 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1370 GEN_ERROR("Function arguments must be value types!");
1371
1372 CHECK_FOR_ERROR
1373
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001374 FunctionType *FT = FunctionType::get(RetTy, Params, isVarArg);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001375 delete $3; // Delete the argument list
Reid Spencer14310612006-12-31 05:40:51 +00001376 delete $1; // Delete the return type handle
1377 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer61c83e02006-08-18 08:43:06 +00001378 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001379 }
Reid Spencer218ded22007-01-05 17:07:23 +00001380 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001381 // Allow but ignore attributes on function types; this permits auto-upgrade.
1382 // FIXME: remove in LLVM 3.0.
Reid Spencer14310612006-12-31 05:40:51 +00001383 std::vector<const Type*> Params;
Reid Spencer7b5d4662007-04-09 06:16:21 +00001384 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001385 for ( ; I != E; ++I ) {
Reid Spencer66728ef2007-03-20 01:13:36 +00001386 const Type* Ty = I->Ty->get();
Reid Spencer66728ef2007-03-20 01:13:36 +00001387 Params.push_back(Ty);
Reid Spencer14310612006-12-31 05:40:51 +00001388 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001389
Reid Spencer14310612006-12-31 05:40:51 +00001390 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1391 if (isVarArg) Params.pop_back();
1392
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001393 for (unsigned i = 0; i != Params.size(); ++i)
1394 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1395 GEN_ERROR("Function arguments must be value types!");
1396
1397 CHECK_FOR_ERROR
1398
Duncan Sandsdc024672007-11-27 13:23:08 +00001399 FunctionType *FT = FunctionType::get($1, Params, isVarArg);
Reid Spencer218ded22007-01-05 17:07:23 +00001400 delete $3; // Delete the argument list
Reid Spencer14310612006-12-31 05:40:51 +00001401 $$ = new PATypeHolder(HandleUpRefs(FT));
1402 CHECK_FOR_ERROR
1403 }
1404
1405 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Reid Spencera132e042006-12-03 05:46:11 +00001406 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1407 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001408 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001409 }
Chris Lattner32980692007-02-19 07:44:24 +00001410 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencera132e042006-12-03 05:46:11 +00001411 const llvm::Type* ElemTy = $4->get();
1412 if ((unsigned)$2 != $2)
1413 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner42a75512007-01-15 02:27:26 +00001414 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencer9d6565a2007-02-15 02:26:10 +00001415 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001416 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencera132e042006-12-03 05:46:11 +00001417 delete $4;
1418 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001419 }
1420 | '{' TypeListI '}' { // Structure type?
1421 std::vector<const Type*> Elements;
Reid Spencera132e042006-12-03 05:46:11 +00001422 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattner58af2a12006-02-15 07:22:58 +00001423 E = $2->end(); I != E; ++I)
Reid Spencera132e042006-12-03 05:46:11 +00001424 Elements.push_back(*I);
Chris Lattner58af2a12006-02-15 07:22:58 +00001425
Reid Spencera132e042006-12-03 05:46:11 +00001426 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattner58af2a12006-02-15 07:22:58 +00001427 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001428 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001429 }
1430 | '{' '}' { // Empty structure type?
Reid Spencera132e042006-12-03 05:46:11 +00001431 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001432 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001433 }
Andrew Lenharth6353e052006-12-08 18:07:09 +00001434 | '<' '{' TypeListI '}' '>' {
1435 std::vector<const Type*> Elements;
1436 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1437 E = $3->end(); I != E; ++I)
1438 Elements.push_back(*I);
1439
1440 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1441 delete $3;
1442 CHECK_FOR_ERROR
1443 }
1444 | '<' '{' '}' '>' { // Empty structure type?
1445 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1446 CHECK_FOR_ERROR
1447 }
Reid Spencer14310612006-12-31 05:40:51 +00001448 ;
1449
1450ArgType
Duncan Sandsdc024672007-11-27 13:23:08 +00001451 : Types OptParamAttrs {
1452 // Allow but ignore attributes on function types; this permits auto-upgrade.
1453 // FIXME: remove in LLVM 3.0.
Reid Spencer14310612006-12-31 05:40:51 +00001454 $$.Ty = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00001455 $$.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00001456 }
1457 ;
1458
Reid Spencer218ded22007-01-05 17:07:23 +00001459ResultTypes
1460 : Types {
Reid Spencer14310612006-12-31 05:40:51 +00001461 if (!UpRefs.empty())
1462 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Devang Patel20071732008-02-23 01:17:37 +00001463 if (!(*$1)->isFirstClassType() && !isa<StructType>($1->get()))
Reid Spencerb5334b02007-02-05 10:18:06 +00001464 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer218ded22007-01-05 17:07:23 +00001465 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00001466 }
Reid Spencer218ded22007-01-05 17:07:23 +00001467 | VOID {
1468 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencer14310612006-12-31 05:40:51 +00001469 }
1470 ;
1471
1472ArgTypeList : ArgType {
1473 $$ = new TypeWithAttrsList();
1474 $$->push_back($1);
1475 CHECK_FOR_ERROR
1476 }
1477 | ArgTypeList ',' ArgType {
1478 ($$=$1)->push_back($3);
1479 CHECK_FOR_ERROR
1480 }
1481 ;
1482
1483ArgTypeListI
1484 : ArgTypeList
1485 | ArgTypeList ',' DOTDOTDOT {
1486 $$=$1;
Reid Spencer18da0722007-04-11 02:44:20 +00001487 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00001488 TWA.Ty = new PATypeHolder(Type::VoidTy);
1489 $$->push_back(TWA);
1490 CHECK_FOR_ERROR
1491 }
1492 | DOTDOTDOT {
1493 $$ = new TypeWithAttrsList;
Reid Spencer18da0722007-04-11 02:44:20 +00001494 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00001495 TWA.Ty = new PATypeHolder(Type::VoidTy);
1496 $$->push_back(TWA);
1497 CHECK_FOR_ERROR
1498 }
1499 | /*empty*/ {
1500 $$ = new TypeWithAttrsList();
Reid Spencer61c83e02006-08-18 08:43:06 +00001501 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001502 };
1503
1504// TypeList - Used for struct declarations and as a basis for function type
1505// declaration type lists
1506//
Reid Spencer14310612006-12-31 05:40:51 +00001507TypeListI : Types {
Reid Spencera132e042006-12-03 05:46:11 +00001508 $$ = new std::list<PATypeHolder>();
Reid Spencer66728ef2007-03-20 01:13:36 +00001509 $$->push_back(*$1);
1510 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001511 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001512 }
Reid Spencer14310612006-12-31 05:40:51 +00001513 | TypeListI ',' Types {
Reid Spencer66728ef2007-03-20 01:13:36 +00001514 ($$=$1)->push_back(*$3);
1515 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001516 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001517 };
1518
Chris Lattner58af2a12006-02-15 07:22:58 +00001519// ConstVal - The various declarations that go into the constant pool. This
1520// production is used ONLY to represent constants that show up AFTER a 'const',
1521// 'constant' or 'global' token at global scope. Constants that can be inlined
1522// into other expressions (such as integers and constexprs) are handled by the
1523// ResolvedVal, ValueRef and ConstValueRef productions.
1524//
1525ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001526 if (!UpRefs.empty())
1527 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001528 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001529 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001530 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001531 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001532 const Type *ETy = ATy->getElementType();
1533 int NumElements = ATy->getNumElements();
1534
1535 // Verify that we have the correct size...
1536 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001537 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001538 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerb5334b02007-02-05 10:18:06 +00001539 itostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001540
1541 // Verify all elements are correct type!
1542 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001543 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001544 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001545 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001546 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001547 }
1548
Reid Spencera132e042006-12-03 05:46:11 +00001549 $$ = ConstantArray::get(ATy, *$3);
1550 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001551 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001552 }
1553 | Types '[' ']' {
Reid Spencer14310612006-12-31 05:40:51 +00001554 if (!UpRefs.empty())
1555 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001556 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001557 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001558 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001559 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001560
1561 int NumElements = ATy->getNumElements();
1562 if (NumElements != -1 && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001563 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencerb5334b02007-02-05 10:18:06 +00001564 " arguments, but has size of " + itostr(NumElements) +"");
Reid Spencera132e042006-12-03 05:46:11 +00001565 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1566 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001567 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001568 }
1569 | Types 'c' STRINGCONSTANT {
Reid Spencer14310612006-12-31 05:40:51 +00001570 if (!UpRefs.empty())
1571 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001572 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001573 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001574 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001575 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001576
1577 int NumElements = ATy->getNumElements();
1578 const Type *ETy = ATy->getElementType();
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001579 if (NumElements != -1 && NumElements != int($3->length()))
Reid Spencer61c83e02006-08-18 08:43:06 +00001580 GEN_ERROR("Can't build string constant of size " +
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001581 itostr((int)($3->length())) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001582 " when array has size " + itostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001583 std::vector<Constant*> Vals;
Reid Spencer14310612006-12-31 05:40:51 +00001584 if (ETy == Type::Int8Ty) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001585 for (unsigned i = 0; i < $3->length(); ++i)
1586 Vals.push_back(ConstantInt::get(ETy, (*$3)[i]));
Chris Lattner58af2a12006-02-15 07:22:58 +00001587 } else {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001588 delete $3;
Reid Spencerb5334b02007-02-05 10:18:06 +00001589 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner58af2a12006-02-15 07:22:58 +00001590 }
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001591 delete $3;
Reid Spencera132e042006-12-03 05:46:11 +00001592 $$ = ConstantArray::get(ATy, Vals);
1593 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001594 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001595 }
1596 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001597 if (!UpRefs.empty())
1598 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00001599 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001600 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001601 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001602 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001603 const Type *ETy = PTy->getElementType();
1604 int NumElements = PTy->getNumElements();
1605
1606 // Verify that we have the correct size...
1607 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001608 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001609 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerb5334b02007-02-05 10:18:06 +00001610 itostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001611
1612 // Verify all elements are correct type!
1613 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001614 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001615 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001616 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001617 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001618 }
1619
Reid Spencer9d6565a2007-02-15 02:26:10 +00001620 $$ = ConstantVector::get(PTy, *$3);
Reid Spencera132e042006-12-03 05:46:11 +00001621 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001622 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001623 }
1624 | Types '{' ConstVector '}' {
Reid Spencera132e042006-12-03 05:46:11 +00001625 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001626 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001627 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001628 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001629
1630 if ($3->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001631 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001632
1633 // Check to ensure that constants are compatible with the type initializer!
1634 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencera132e042006-12-03 05:46:11 +00001635 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001636 GEN_ERROR("Expected type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001637 STy->getElementType(i)->getDescription() +
1638 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001639 " of structure initializer");
Chris Lattner58af2a12006-02-15 07:22:58 +00001640
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001641 // Check to ensure that Type is not packed
1642 if (STy->isPacked())
Chris Lattner6cdc6822007-04-26 05:31:05 +00001643 GEN_ERROR("Unpacked Initializer to vector type '" +
1644 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001645
Reid Spencera132e042006-12-03 05:46:11 +00001646 $$ = ConstantStruct::get(STy, *$3);
1647 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001648 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001649 }
1650 | Types '{' '}' {
Reid Spencer14310612006-12-31 05:40:51 +00001651 if (!UpRefs.empty())
1652 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001653 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001654 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001655 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001656 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001657
1658 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001659 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001660
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001661 // Check to ensure that Type is not packed
1662 if (STy->isPacked())
Chris Lattner6cdc6822007-04-26 05:31:05 +00001663 GEN_ERROR("Unpacked Initializer to vector type '" +
1664 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001665
1666 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1667 delete $1;
1668 CHECK_FOR_ERROR
1669 }
1670 | Types '<' '{' ConstVector '}' '>' {
1671 const StructType *STy = dyn_cast<StructType>($1->get());
1672 if (STy == 0)
1673 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001674 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001675
1676 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001677 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001678
1679 // Check to ensure that constants are compatible with the type initializer!
1680 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1681 if ((*$4)[i]->getType() != STy->getElementType(i))
1682 GEN_ERROR("Expected type '" +
1683 STy->getElementType(i)->getDescription() +
1684 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001685 " of structure initializer");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001686
1687 // Check to ensure that Type is packed
1688 if (!STy->isPacked())
Chris Lattner32980692007-02-19 07:44:24 +00001689 GEN_ERROR("Vector initializer to non-vector type '" +
1690 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001691
1692 $$ = ConstantStruct::get(STy, *$4);
1693 delete $1; delete $4;
1694 CHECK_FOR_ERROR
1695 }
1696 | Types '<' '{' '}' '>' {
1697 if (!UpRefs.empty())
1698 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1699 const StructType *STy = dyn_cast<StructType>($1->get());
1700 if (STy == 0)
1701 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001702 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001703
1704 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001705 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001706
1707 // Check to ensure that Type is packed
1708 if (!STy->isPacked())
Chris Lattner32980692007-02-19 07:44:24 +00001709 GEN_ERROR("Vector initializer to non-vector type '" +
1710 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001711
Reid Spencera132e042006-12-03 05:46:11 +00001712 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1713 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001714 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001715 }
1716 | Types NULL_TOK {
Reid Spencer14310612006-12-31 05:40:51 +00001717 if (!UpRefs.empty())
1718 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001719 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001720 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001721 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001722 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001723
Reid Spencera132e042006-12-03 05:46:11 +00001724 $$ = ConstantPointerNull::get(PTy);
1725 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001726 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001727 }
1728 | Types UNDEF {
Reid Spencer14310612006-12-31 05:40:51 +00001729 if (!UpRefs.empty())
1730 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001731 $$ = UndefValue::get($1->get());
1732 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001733 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001734 }
1735 | Types SymbolicValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00001736 if (!UpRefs.empty())
1737 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001738 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001739 if (Ty == 0)
Devang Patel5a970972008-02-19 22:27:01 +00001740 GEN_ERROR("Global const reference must be a pointer type " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001741
1742 // ConstExprs can exist in the body of a function, thus creating
1743 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencer93c40032007-03-19 18:40:50 +00001744 // the context of a function, getExistingVal will search the functions
Chris Lattner58af2a12006-02-15 07:22:58 +00001745 // symbol table instead of the module symbol table for the global symbol,
1746 // which throws things all off. To get around this, we just tell
Reid Spencer93c40032007-03-19 18:40:50 +00001747 // getExistingVal that we are at global scope here.
Chris Lattner58af2a12006-02-15 07:22:58 +00001748 //
1749 Function *SavedCurFn = CurFun.CurrentFunction;
1750 CurFun.CurrentFunction = 0;
1751
Reid Spencer93c40032007-03-19 18:40:50 +00001752 Value *V = getExistingVal(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001753 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001754
1755 CurFun.CurrentFunction = SavedCurFn;
1756
1757 // If this is an initializer for a constant pointer, which is referencing a
1758 // (currently) undefined variable, create a stub now that shall be replaced
1759 // in the future with the right type of variable.
1760 //
1761 if (V == 0) {
Reid Spencera9720f52007-02-05 17:04:00 +00001762 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001763 const PointerType *PT = cast<PointerType>(Ty);
1764
1765 // First check to see if the forward references value is already created!
1766 PerModuleInfo::GlobalRefsType::iterator I =
1767 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1768
1769 if (I != CurModule.GlobalRefs.end()) {
1770 V = I->second; // Placeholder already exists, use it...
1771 $2.destroy();
1772 } else {
1773 std::string Name;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001774 if ($2.Type == ValID::GlobalName)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001775 Name = $2.getName();
Reid Spencer41dff5e2007-01-26 08:05:27 +00001776 else if ($2.Type != ValID::GlobalID)
1777 GEN_ERROR("Invalid reference to global");
Chris Lattner58af2a12006-02-15 07:22:58 +00001778
1779 // Create the forward referenced global.
1780 GlobalValue *GV;
1781 if (const FunctionType *FTy =
1782 dyn_cast<FunctionType>(PT->getElementType())) {
Chris Lattner6cdc6822007-04-26 05:31:05 +00001783 GV = new Function(FTy, GlobalValue::ExternalWeakLinkage, Name,
Chris Lattner58af2a12006-02-15 07:22:58 +00001784 CurModule.CurrentModule);
1785 } else {
1786 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattner6cdc6822007-04-26 05:31:05 +00001787 GlobalValue::ExternalWeakLinkage, 0,
Chris Lattner58af2a12006-02-15 07:22:58 +00001788 Name, CurModule.CurrentModule);
1789 }
1790
1791 // Keep track of the fact that we have a forward ref to recycle it
1792 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1793 V = GV;
1794 }
1795 }
1796
Reid Spencera132e042006-12-03 05:46:11 +00001797 $$ = cast<GlobalValue>(V);
1798 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001799 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001800 }
1801 | Types ConstExpr {
Reid Spencer14310612006-12-31 05:40:51 +00001802 if (!UpRefs.empty())
1803 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001804 if ($1->get() != $2->getType())
Reid Spencere68853b2007-01-04 00:06:14 +00001805 GEN_ERROR("Mismatched types for constant expression: " +
1806 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001807 $$ = $2;
Reid Spencera132e042006-12-03 05:46:11 +00001808 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001809 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001810 }
1811 | Types ZEROINITIALIZER {
Reid Spencer14310612006-12-31 05:40:51 +00001812 if (!UpRefs.empty())
1813 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001814 const Type *Ty = $1->get();
Chris Lattner58af2a12006-02-15 07:22:58 +00001815 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerb5334b02007-02-05 10:18:06 +00001816 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencera132e042006-12-03 05:46:11 +00001817 $$ = Constant::getNullValue(Ty);
1818 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001819 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00001820 }
Reid Spencer14310612006-12-31 05:40:51 +00001821 | IntType ESINT64VAL { // integral constants
Reid Spencere4d87aa2006-12-23 06:05:41 +00001822 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001823 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencer49d273e2007-03-19 20:40:51 +00001824 $$ = ConstantInt::get($1, $2, true);
Reid Spencer38c91a92007-02-28 02:24:54 +00001825 CHECK_FOR_ERROR
1826 }
1827 | IntType ESAPINTVAL { // arbitrary precision integer constants
1828 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1829 if ($2->getBitWidth() > BitWidth) {
1830 GEN_ERROR("Constant value does not fit in type");
Reid Spencer10794272007-03-01 19:41:47 +00001831 }
1832 $2->sextOrTrunc(BitWidth);
1833 $$ = ConstantInt::get(*$2);
Reid Spencer38c91a92007-02-28 02:24:54 +00001834 delete $2;
Reid Spencere4d87aa2006-12-23 06:05:41 +00001835 CHECK_FOR_ERROR
1836 }
Reid Spencer14310612006-12-31 05:40:51 +00001837 | IntType EUINT64VAL { // integral constants
Reid Spencere4d87aa2006-12-23 06:05:41 +00001838 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001839 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencer49d273e2007-03-19 20:40:51 +00001840 $$ = ConstantInt::get($1, $2, false);
Reid Spencer38c91a92007-02-28 02:24:54 +00001841 CHECK_FOR_ERROR
1842 }
1843 | IntType EUAPINTVAL { // arbitrary precision integer constants
1844 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1845 if ($2->getBitWidth() > BitWidth) {
1846 GEN_ERROR("Constant value does not fit in type");
Reid Spencer10794272007-03-01 19:41:47 +00001847 }
1848 $2->zextOrTrunc(BitWidth);
1849 $$ = ConstantInt::get(*$2);
Reid Spencer38c91a92007-02-28 02:24:54 +00001850 delete $2;
Reid Spencere4d87aa2006-12-23 06:05:41 +00001851 CHECK_FOR_ERROR
1852 }
Reid Spencer6f407902007-01-13 05:00:46 +00001853 | INTTYPE TRUETOK { // Boolean constants
1854 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001855 $$ = ConstantInt::getTrue();
Reid Spencer61c83e02006-08-18 08:43:06 +00001856 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001857 }
Reid Spencer6f407902007-01-13 05:00:46 +00001858 | INTTYPE FALSETOK { // Boolean constants
1859 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001860 $$ = ConstantInt::getFalse();
Reid Spencer61c83e02006-08-18 08:43:06 +00001861 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001862 }
Dale Johannesenea583102007-09-12 03:31:28 +00001863 | FPType FPVAL { // Floating point constants
Dale Johannesen43421b32007-09-06 18:13:44 +00001864 if (!ConstantFP::isValueValidForType($1, *$2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001865 GEN_ERROR("Floating point constant invalid for type");
Dale Johannesenc72cd7e2007-09-11 18:33:39 +00001866 // Lexer has no type info, so builds all float and double FP constants
1867 // as double. Fix this here. Long double is done right.
1868 if (&$2->getSemantics()==&APFloat::IEEEdouble && $1==Type::FloatTy)
Dale Johannesen43421b32007-09-06 18:13:44 +00001869 $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
1870 $$ = ConstantFP::get($1, *$2);
Dale Johannesencdd509a2007-09-07 21:07:57 +00001871 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001872 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001873 };
1874
1875
Reid Spencer3da59db2006-11-27 01:05:10 +00001876ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer14310612006-12-31 05:40:51 +00001877 if (!UpRefs.empty())
1878 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001879 Constant *Val = $3;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001880 const Type *DestTy = $5->get();
1881 if (!CastInst::castIsValid($1, $3, DestTy))
1882 GEN_ERROR("invalid cast opcode for cast from '" +
1883 Val->getType()->getDescription() + "' to '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001884 DestTy->getDescription() + "'");
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001885 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00001886 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00001887 }
1888 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001889 if (!isa<PointerType>($3->getType()))
Reid Spencerb5334b02007-02-05 10:18:06 +00001890 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00001891
Reid Spencera132e042006-12-03 05:46:11 +00001892 const Type *IdxTy =
David Greene5fd22a82007-09-04 18:46:50 +00001893 GetElementPtrInst::getIndexedType($3->getType(), $4->begin(), $4->end(),
Chris Lattner7d9801d2007-02-13 00:58:01 +00001894 true);
Reid Spencera132e042006-12-03 05:46:11 +00001895 if (!IdxTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00001896 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencera132e042006-12-03 05:46:11 +00001897
Chris Lattnerf7469af2007-01-31 04:44:08 +00001898 SmallVector<Constant*, 8> IdxVec;
Reid Spencera132e042006-12-03 05:46:11 +00001899 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1900 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattner58af2a12006-02-15 07:22:58 +00001901 IdxVec.push_back(C);
1902 else
Reid Spencerb5334b02007-02-05 10:18:06 +00001903 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattner58af2a12006-02-15 07:22:58 +00001904
1905 delete $4;
1906
Chris Lattnerf7469af2007-01-31 04:44:08 +00001907 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer61c83e02006-08-18 08:43:06 +00001908 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001909 }
1910 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer4fe16d62007-01-11 18:21:29 +00001911 if ($3->getType() != Type::Int1Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00001912 GEN_ERROR("Select condition must be of boolean type");
Reid Spencera132e042006-12-03 05:46:11 +00001913 if ($5->getType() != $7->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001914 GEN_ERROR("Select operand types must match");
Reid Spencera132e042006-12-03 05:46:11 +00001915 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001916 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001917 }
1918 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001919 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001920 GEN_ERROR("Binary operator types must match");
Reid Spencer1628cec2006-10-26 06:15:43 +00001921 CHECK_FOR_ERROR;
Reid Spencer9eef56f2006-12-05 19:16:11 +00001922 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner58af2a12006-02-15 07:22:58 +00001923 }
1924 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001925 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001926 GEN_ERROR("Logical operator types must match");
Chris Lattner42a75512007-01-15 02:27:26 +00001927 if (!$3->getType()->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001928 if (Instruction::isShift($1) || !isa<VectorType>($3->getType()) ||
1929 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00001930 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00001931 }
Reid Spencera132e042006-12-03 05:46:11 +00001932 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001933 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001934 }
Reid Spencer4012e832006-12-04 05:24:24 +00001935 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1936 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001937 GEN_ERROR("icmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00001938 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00001939 }
Reid Spencer4012e832006-12-04 05:24:24 +00001940 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1941 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001942 GEN_ERROR("fcmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00001943 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00001944 }
Chris Lattner58af2a12006-02-15 07:22:58 +00001945 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001946 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerb5334b02007-02-05 10:18:06 +00001947 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00001948 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001949 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00001950 }
1951 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001952 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00001953 GEN_ERROR("Invalid insertelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00001954 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001955 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00001956 }
1957 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001958 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00001959 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00001960 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001961 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001962 };
1963
Chris Lattnerd25db202006-04-08 03:55:17 +00001964
Chris Lattner58af2a12006-02-15 07:22:58 +00001965// ConstVector - A list of comma separated constants.
1966ConstVector : ConstVector ',' ConstVal {
1967 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001968 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001969 }
1970 | ConstVal {
Reid Spencera132e042006-12-03 05:46:11 +00001971 $$ = new std::vector<Constant*>();
Chris Lattner58af2a12006-02-15 07:22:58 +00001972 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001973 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001974 };
1975
1976
1977// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1978GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
1979
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001980// ThreadLocal
1981ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
1982
Anton Korobeynikov38e09802007-04-28 13:48:45 +00001983// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
1984AliaseeRef : ResultTypes SymbolicValueRef {
1985 const Type* VTy = $1->get();
1986 Value *V = getVal(VTy, $2);
Chris Lattner0275cff2007-08-06 21:00:46 +00001987 CHECK_FOR_ERROR
Anton Korobeynikov38e09802007-04-28 13:48:45 +00001988 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
1989 if (!Aliasee)
1990 GEN_ERROR("Aliases can be created only to global values");
1991
1992 $$ = Aliasee;
1993 CHECK_FOR_ERROR
1994 delete $1;
1995 }
1996 | BITCAST '(' AliaseeRef TO Types ')' {
1997 Constant *Val = $3;
1998 const Type *DestTy = $5->get();
1999 if (!CastInst::castIsValid($1, $3, DestTy))
2000 GEN_ERROR("invalid cast opcode for cast from '" +
2001 Val->getType()->getDescription() + "' to '" +
2002 DestTy->getDescription() + "'");
2003
2004 $$ = ConstantExpr::getCast($1, $3, DestTy);
2005 CHECK_FOR_ERROR
2006 delete $5;
2007 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002008
2009//===----------------------------------------------------------------------===//
2010// Rules to match Modules
2011//===----------------------------------------------------------------------===//
2012
2013// Module rule: Capture the result of parsing the whole file into a result
2014// variable...
2015//
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002016Module
2017 : DefinitionList {
2018 $$ = ParserResult = CurModule.CurrentModule;
2019 CurModule.ModuleDone();
2020 CHECK_FOR_ERROR;
2021 }
2022 | /*empty*/ {
2023 $$ = ParserResult = CurModule.CurrentModule;
2024 CurModule.ModuleDone();
2025 CHECK_FOR_ERROR;
2026 }
2027 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002028
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002029DefinitionList
2030 : Definition
2031 | DefinitionList Definition
2032 ;
2033
2034Definition
Jeff Cohen361c3ef2007-01-21 19:19:31 +00002035 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner58af2a12006-02-15 07:22:58 +00002036 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00002037 CHECK_FOR_ERROR
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002038 }
2039 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer61c83e02006-08-18 08:43:06 +00002040 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002041 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002042 | MODULE ASM_TOK AsmBlock {
Reid Spencer61c83e02006-08-18 08:43:06 +00002043 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002044 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002045 | OptLocalAssign TYPE Types {
Reid Spencer14310612006-12-31 05:40:51 +00002046 if (!UpRefs.empty())
2047 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002048 // Eagerly resolve types. This is not an optimization, this is a
2049 // requirement that is due to the fact that we could have this:
2050 //
2051 // %list = type { %list * }
2052 // %list = type { %list * } ; repeated type decl
2053 //
2054 // If types are not resolved eagerly, then the two types will not be
2055 // determined to be the same type!
2056 //
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002057 ResolveTypeTo($1, *$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002058
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002059 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002060 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002061 // If this is a named type that is not a redefinition, add it to the slot
2062 // table.
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002063 CurModule.Types.push_back(*$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002064 }
Reid Spencera132e042006-12-03 05:46:11 +00002065
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002066 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002067 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002068 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002069 | OptLocalAssign TYPE VOID {
Reid Spencer14310612006-12-31 05:40:51 +00002070 ResolveTypeTo($1, $3);
2071
2072 if (!setTypeName($3, $1) && !$1) {
2073 CHECK_FOR_ERROR
2074 // If this is a named type that is not a redefinition, add it to the slot
2075 // table.
2076 CurModule.Types.push_back($3);
2077 }
2078 CHECK_FOR_ERROR
2079 }
Christopher Lambbf3348d2007-12-12 08:45:45 +00002080 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal
2081 OptAddrSpace {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002082 /* "Externally Visible" Linkage */
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002083 if ($5 == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002084 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002085 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
Christopher Lambbf3348d2007-12-12 08:45:45 +00002086 $2, $4, $5->getType(), $5, $3, $6);
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00002087 CHECK_FOR_ERROR
2088 } GlobalVarAttributes {
2089 CurGV = 0;
2090 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002091 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambbf3348d2007-12-12 08:45:45 +00002092 ConstVal OptAddrSpace {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002093 if ($6 == 0)
2094 GEN_ERROR("Global value initializer is not a constant");
Christopher Lambbf3348d2007-12-12 08:45:45 +00002095 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4, $7);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002096 CHECK_FOR_ERROR
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002097 } GlobalVarAttributes {
2098 CurGV = 0;
2099 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002100 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambbf3348d2007-12-12 08:45:45 +00002101 Types OptAddrSpace {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002102 if (!UpRefs.empty())
2103 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
Christopher Lambbf3348d2007-12-12 08:45:45 +00002104 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4, $7);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002105 CHECK_FOR_ERROR
2106 delete $6;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002107 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002108 CurGV = 0;
2109 CHECK_FOR_ERROR
2110 }
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002111 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002112 std::string Name;
2113 if ($1) {
2114 Name = *$1;
2115 delete $1;
2116 }
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002117 if (Name.empty())
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002118 GEN_ERROR("Alias name cannot be empty");
2119
2120 Constant* Aliasee = $5;
2121 if (Aliasee == 0)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002122 GEN_ERROR(std::string("Invalid aliasee for alias: ") + Name);
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002123
2124 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2125 CurModule.CurrentModule);
2126 GA->setVisibility($2);
2127 InsertValue(GA, CurModule.Values);
Chris Lattner569f7372007-09-10 23:24:14 +00002128
2129
2130 // If there was a forward reference of this alias, resolve it now.
2131
2132 ValID ID;
2133 if (!Name.empty())
2134 ID = ValID::createGlobalName(Name);
2135 else
2136 ID = ValID::createGlobalID(CurModule.Values.size()-1);
2137
2138 if (GlobalValue *FWGV =
2139 CurModule.GetForwardRefForGlobal(GA->getType(), ID)) {
2140 // Replace uses of the fwdref with the actual alias.
2141 FWGV->replaceAllUsesWith(GA);
2142 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(FWGV))
2143 GV->eraseFromParent();
2144 else
2145 cast<Function>(FWGV)->eraseFromParent();
2146 }
2147 ID.destroy();
2148
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002149 CHECK_FOR_ERROR
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002150 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002151 | TARGET TargetDefinition {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002152 CHECK_FOR_ERROR
2153 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002154 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00002155 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002156 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002157 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002158
2159
2160AsmBlock : STRINGCONSTANT {
2161 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattner58af2a12006-02-15 07:22:58 +00002162 if (AsmSoFar.empty())
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002163 CurModule.CurrentModule->setModuleInlineAsm(*$1);
Chris Lattner58af2a12006-02-15 07:22:58 +00002164 else
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002165 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+*$1);
2166 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002167 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002168};
2169
Reid Spencer41dff5e2007-01-26 08:05:27 +00002170TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002171 CurModule.CurrentModule->setTargetTriple(*$3);
2172 delete $3;
John Criswell2f6a8b12006-10-24 19:09:48 +00002173 }
Chris Lattner1ae022f2006-10-22 06:08:13 +00002174 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002175 CurModule.CurrentModule->setDataLayout(*$3);
2176 delete $3;
Owen Anderson1dc69692006-10-18 02:21:48 +00002177 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002178
2179LibrariesDefinition : '[' LibList ']';
2180
2181LibList : LibList ',' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002182 CurModule.CurrentModule->addLibrary(*$3);
2183 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002184 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002185 }
2186 | STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002187 CurModule.CurrentModule->addLibrary(*$1);
2188 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002189 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002190 }
2191 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00002192 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002193 }
2194 ;
2195
2196//===----------------------------------------------------------------------===//
2197// Rules to match Function Headers
2198//===----------------------------------------------------------------------===//
2199
Reid Spencer41dff5e2007-01-26 08:05:27 +00002200ArgListH : ArgListH ',' Types OptParamAttrs OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002201 if (!UpRefs.empty())
2202 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
2203 if (*$3 == Type::VoidTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00002204 GEN_ERROR("void typed arguments are invalid");
Reid Spencer14310612006-12-31 05:40:51 +00002205 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00002206 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002207 $1->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002208 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002209 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002210 | Types OptParamAttrs OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002211 if (!UpRefs.empty())
2212 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2213 if (*$1 == Type::VoidTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00002214 GEN_ERROR("void typed arguments are invalid");
Reid Spencer14310612006-12-31 05:40:51 +00002215 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2216 $$ = new ArgListType;
2217 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002218 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002219 };
2220
2221ArgList : ArgListH {
2222 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002223 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002224 }
2225 | ArgListH ',' DOTDOTDOT {
2226 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002227 struct ArgListEntry E;
2228 E.Ty = new PATypeHolder(Type::VoidTy);
2229 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002230 E.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00002231 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002232 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002233 }
2234 | DOTDOTDOT {
Reid Spencer14310612006-12-31 05:40:51 +00002235 $$ = new ArgListType;
2236 struct ArgListEntry E;
2237 E.Ty = new PATypeHolder(Type::VoidTy);
2238 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002239 E.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00002240 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002241 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002242 }
2243 | /* empty */ {
2244 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00002245 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002246 };
2247
Reid Spencer41dff5e2007-01-26 08:05:27 +00002248FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002249 OptFuncAttrs OptSection OptAlign OptGC {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002250 std::string FunctionName(*$3);
2251 delete $3; // Free strdup'd memory!
Chris Lattner58af2a12006-02-15 07:22:58 +00002252
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002253 // Check the function result for abstractness if this is a define. We should
2254 // have no abstract types at this point
Reid Spencer218ded22007-01-05 17:07:23 +00002255 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2256 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002257
Chris Lattner58af2a12006-02-15 07:22:58 +00002258 std::vector<const Type*> ParamTypeList;
Christopher Lamb5c104242007-04-22 20:09:11 +00002259 ParamAttrsVector Attrs;
2260 if ($7 != ParamAttr::None) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002261 ParamAttrsWithIndex PAWI;
2262 PAWI.index = 0;
2263 PAWI.attrs = $7;
Christopher Lamb5c104242007-04-22 20:09:11 +00002264 Attrs.push_back(PAWI);
2265 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002266 if ($5) { // If there are arguments...
Reid Spencer7b5d4662007-04-09 06:16:21 +00002267 unsigned index = 1;
2268 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002269 const Type* Ty = I->Ty->get();
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002270 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2271 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencer14310612006-12-31 05:40:51 +00002272 ParamTypeList.push_back(Ty);
2273 if (Ty != Type::VoidTy)
Christopher Lamb5c104242007-04-22 20:09:11 +00002274 if (I->Attrs != ParamAttr::None) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002275 ParamAttrsWithIndex PAWI;
2276 PAWI.index = index;
2277 PAWI.attrs = I->Attrs;
Christopher Lamb5c104242007-04-22 20:09:11 +00002278 Attrs.push_back(PAWI);
2279 }
Reid Spencer14310612006-12-31 05:40:51 +00002280 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002281 }
2282
2283 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2284 if (isVarArg) ParamTypeList.pop_back();
2285
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00002286 const ParamAttrsList *PAL = 0;
Christopher Lamb5c104242007-04-22 20:09:11 +00002287 if (!Attrs.empty())
2288 PAL = ParamAttrsList::get(Attrs);
Reid Spencer7b5d4662007-04-09 06:16:21 +00002289
Duncan Sandsdc024672007-11-27 13:23:08 +00002290 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002291 const PointerType *PFT = PointerType::getUnqual(FT);
Reid Spencer218ded22007-01-05 17:07:23 +00002292 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002293
2294 ValID ID;
2295 if (!FunctionName.empty()) {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002296 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattner58af2a12006-02-15 07:22:58 +00002297 } else {
Reid Spencer93c40032007-03-19 18:40:50 +00002298 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002299 }
2300
2301 Function *Fn = 0;
2302 // See if this function was forward referenced. If so, recycle the object.
2303 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2304 // Move the function to the end of the list, from whereever it was
2305 // previously inserted.
2306 Fn = cast<Function>(FWRef);
Duncan Sandsdc024672007-11-27 13:23:08 +00002307 assert(!Fn->getParamAttrs() && "Forward reference has parameter attributes!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002308 CurModule.CurrentModule->getFunctionList().remove(Fn);
2309 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2310 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spenceref9b9a72007-02-05 20:47:22 +00002311 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002312 if (Fn->getFunctionType() != FT ) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002313 // The existing function doesn't have the same type. This is an overload
2314 // error.
2315 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Duncan Sandsdc024672007-11-27 13:23:08 +00002316 } else if (Fn->getParamAttrs() != PAL) {
2317 // The existing function doesn't have the same parameter attributes.
2318 // This is an overload error.
2319 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002320 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
Chris Lattner6cdc6822007-04-26 05:31:05 +00002321 // Neither the existing or the current function is a declaration and they
2322 // have the same name and same type. Clearly this is a redefinition.
2323 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Duncan Sandsdc024672007-11-27 13:23:08 +00002324 } else if (Fn->isDeclaration()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002325 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattner58af2a12006-02-15 07:22:58 +00002326 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2327 AI != AE; ++AI)
2328 AI->setName("");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002329 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002330 } else { // Not already defined?
Chris Lattner6cdc6822007-04-26 05:31:05 +00002331 Fn = new Function(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
Chris Lattner58af2a12006-02-15 07:22:58 +00002332 CurModule.CurrentModule);
2333 InsertValue(Fn, CurModule.Values);
2334 }
2335
2336 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002337
2338 if (CurFun.isDeclare) {
2339 // If we have declaration, always overwrite linkage. This will allow us to
2340 // correctly handle cases, when pointer to function is passed as argument to
2341 // another function.
2342 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002343 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002344 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002345 Fn->setCallingConv($1);
Duncan Sandsdc024672007-11-27 13:23:08 +00002346 Fn->setParamAttrs(PAL);
Reid Spencer218ded22007-01-05 17:07:23 +00002347 Fn->setAlignment($9);
2348 if ($8) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002349 Fn->setSection(*$8);
2350 delete $8;
Chris Lattner58af2a12006-02-15 07:22:58 +00002351 }
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002352 if ($10) {
2353 Fn->setCollector($10->c_str());
2354 delete $10;
2355 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002356
2357 // Add all of the arguments we parsed to the function...
2358 if ($5) { // Is null if empty...
2359 if (isVarArg) { // Nuke the last entry
Reid Spenceref9b9a72007-02-05 20:47:22 +00002360 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0 &&
Reid Spencera9720f52007-02-05 17:04:00 +00002361 "Not a varargs marker!");
Reid Spencer14310612006-12-31 05:40:51 +00002362 delete $5->back().Ty;
Chris Lattner58af2a12006-02-15 07:22:58 +00002363 $5->pop_back(); // Delete the last entry
2364 }
2365 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002366 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencer14310612006-12-31 05:40:51 +00002367 unsigned Idx = 1;
Reid Spenceref9b9a72007-02-05 20:47:22 +00002368 for (ArgListType::iterator I = $5->begin();
2369 I != $5->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencer14310612006-12-31 05:40:51 +00002370 delete I->Ty; // Delete the typeholder...
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002371 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002372 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002373 InsertValue(ArgIt);
Reid Spencer14310612006-12-31 05:40:51 +00002374 Idx++;
Chris Lattner58af2a12006-02-15 07:22:58 +00002375 }
Reid Spencera132e042006-12-03 05:46:11 +00002376
Chris Lattner58af2a12006-02-15 07:22:58 +00002377 delete $5; // We're now done with the argument list
2378 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002379 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002380};
2381
2382BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2383
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002384FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner58af2a12006-02-15 07:22:58 +00002385 $$ = CurFun.CurrentFunction;
2386
2387 // Make sure that we keep track of the linkage type even if there was a
2388 // previous "declare".
2389 $$->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002390 $$->setVisibility($2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002391};
2392
2393END : ENDTOK | '}'; // Allow end of '}' to end a function
2394
2395Function : BasicBlockList END {
2396 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002397 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002398};
2399
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002400FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencer14310612006-12-31 05:40:51 +00002401 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002402 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002403 $$ = CurFun.CurrentFunction;
2404 CurFun.FunctionDone();
2405 CHECK_FOR_ERROR
2406 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002407
2408//===----------------------------------------------------------------------===//
2409// Rules to match Basic Blocks
2410//===----------------------------------------------------------------------===//
2411
2412OptSideEffect : /* empty */ {
2413 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002414 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002415 }
2416 | SIDEEFFECT {
2417 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002418 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002419 };
2420
2421ConstValueRef : ESINT64VAL { // A reference to a direct constant
2422 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002423 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002424 }
2425 | EUINT64VAL {
2426 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002427 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002428 }
2429 | FPVAL { // Perhaps it's an FP constant?
2430 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002431 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002432 }
2433 | TRUETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002434 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002435 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002436 }
2437 | FALSETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002438 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002439 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002440 }
2441 | NULL_TOK {
2442 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002443 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002444 }
2445 | UNDEF {
2446 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002447 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002448 }
2449 | ZEROINITIALIZER { // A vector zero constant.
2450 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002451 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002452 }
2453 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencera132e042006-12-03 05:46:11 +00002454 const Type *ETy = (*$2)[0]->getType();
Chris Lattner58af2a12006-02-15 07:22:58 +00002455 int NumElements = $2->size();
2456
Reid Spencer9d6565a2007-02-15 02:26:10 +00002457 VectorType* pt = VectorType::get(ETy, NumElements);
Chris Lattner58af2a12006-02-15 07:22:58 +00002458 PATypeHolder* PTy = new PATypeHolder(
Reid Spencera132e042006-12-03 05:46:11 +00002459 HandleUpRefs(
Reid Spencer9d6565a2007-02-15 02:26:10 +00002460 VectorType::get(
Reid Spencera132e042006-12-03 05:46:11 +00002461 ETy,
2462 NumElements)
2463 )
2464 );
Chris Lattner58af2a12006-02-15 07:22:58 +00002465
2466 // Verify all elements are correct type!
2467 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00002468 if (ETy != (*$2)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002469 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00002470 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencera132e042006-12-03 05:46:11 +00002471 (*$2)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00002472 }
2473
Reid Spencer9d6565a2007-02-15 02:26:10 +00002474 $$ = ValID::create(ConstantVector::get(pt, *$2));
Chris Lattner58af2a12006-02-15 07:22:58 +00002475 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002476 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002477 }
2478 | ConstExpr {
Reid Spencera132e042006-12-03 05:46:11 +00002479 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002480 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002481 }
2482 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002483 $$ = ValID::createInlineAsm(*$3, *$5, $2);
2484 delete $3;
2485 delete $5;
Reid Spencer61c83e02006-08-18 08:43:06 +00002486 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002487 };
2488
2489// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2490// another value.
2491//
Reid Spencer41dff5e2007-01-26 08:05:27 +00002492SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2493 $$ = ValID::createLocalID($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002494 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002495 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002496 | GLOBALVAL_ID {
2497 $$ = ValID::createGlobalID($1);
2498 CHECK_FOR_ERROR
2499 }
2500 | LocalName { // Is it a named reference...?
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002501 $$ = ValID::createLocalName(*$1);
2502 delete $1;
Reid Spencer41dff5e2007-01-26 08:05:27 +00002503 CHECK_FOR_ERROR
2504 }
2505 | GlobalName { // Is it a named reference...?
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002506 $$ = ValID::createGlobalName(*$1);
2507 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002508 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002509 };
2510
2511// ValueRef - A reference to a definition... either constant or symbolic
2512ValueRef : SymbolicValueRef | ConstValueRef;
2513
2514
2515// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2516// type immediately preceeds the value reference, and allows complex constant
2517// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2518ResolvedVal : Types ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002519 if (!UpRefs.empty())
2520 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2521 $$ = getVal(*$1, $2);
2522 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002523 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00002524 }
2525 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002526
Devang Patel7990dc72008-02-20 22:40:23 +00002527ReturnedVal : ResolvedVal {
2528 $$ = new std::vector<Value *>();
2529 $$->push_back($1);
2530 CHECK_FOR_ERROR
2531 }
Devang Patel6bfc63b2008-02-23 00:38:56 +00002532 | ReturnedVal ',' ResolvedVal {
Devang Patel7990dc72008-02-20 22:40:23 +00002533 ($$=$1)->push_back($3);
2534 CHECK_FOR_ERROR
2535 };
2536
Chris Lattner58af2a12006-02-15 07:22:58 +00002537BasicBlockList : BasicBlockList BasicBlock {
2538 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002539 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002540 }
2541 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
2542 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002543 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002544 };
2545
2546
2547// Basic blocks are terminated by branching instructions:
2548// br, br/cc, switch, ret
2549//
Reid Spencer41dff5e2007-01-26 08:05:27 +00002550BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner58af2a12006-02-15 07:22:58 +00002551 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002552 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002553 InsertValue($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002554 $1->getInstList().push_back($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002555 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002556 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002557 };
2558
2559InstructionList : InstructionList Inst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002560 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2561 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2562 if (CI2->getParent() == 0)
2563 $1->getInstList().push_back(CI2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002564 $1->getInstList().push_back($2);
2565 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002566 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002567 }
Reid Spencer93c40032007-03-19 18:40:50 +00002568 | /* empty */ { // Empty space between instruction lists
Devang Patel67909432008-03-03 18:58:47 +00002569 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum), 0);
2570 CHECK_FOR_ERROR
2571 }
Nick Lewycky7e93e162008-03-10 05:01:34 +00002572 | UNWINDS TO ValueRef { // Only the unwind to block
2573 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum), getBBVal($3));
Reid Spencer61c83e02006-08-18 08:43:06 +00002574 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002575 }
Reid Spencer93c40032007-03-19 18:40:50 +00002576 | LABELSTR { // Labelled (named) basic block
Devang Patel67909432008-03-03 18:58:47 +00002577 $$ = defineBBVal(ValID::createLocalName(*$1), 0);
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002578 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002579 CHECK_FOR_ERROR
Devang Patel67909432008-03-03 18:58:47 +00002580 }
Nick Lewycky7e93e162008-03-10 05:01:34 +00002581 | LABELSTR UNWINDS TO ValueRef {
2582 $$ = defineBBVal(ValID::createLocalName(*$1), getBBVal($4));
Devang Patel67909432008-03-03 18:58:47 +00002583 delete $1;
2584 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002585 };
2586
Devang Patel7990dc72008-02-20 22:40:23 +00002587BBTerminatorInst :
2588 RET ReturnedVal { // Return with a result...
Devang Patelb82b7f22008-02-26 22:17:48 +00002589 ValueList &VL = *$2;
Devang Patel13b823c2008-02-26 23:19:08 +00002590 assert(!VL.empty() && "Invalid ret operands!");
2591 $$ = new ReturnInst(&VL[0], VL.size());
Devang Patel7990dc72008-02-20 22:40:23 +00002592 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002593 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002594 }
Reid Spencer93c40032007-03-19 18:40:50 +00002595 | RET VOID { // Return with no result...
Chris Lattner58af2a12006-02-15 07:22:58 +00002596 $$ = new ReturnInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002597 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002598 }
Reid Spencer93c40032007-03-19 18:40:50 +00002599 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002600 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002601 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002602 $$ = new BranchInst(tmpBB);
Reid Spencer93c40032007-03-19 18:40:50 +00002603 } // Conditional Branch...
Reid Spencer6f407902007-01-13 05:00:46 +00002604 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
2605 assert(cast<IntegerType>($2)->getBitWidth() == 1 && "Not Bool?");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002606 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002607 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002608 BasicBlock* tmpBBB = getBBVal($9);
2609 CHECK_FOR_ERROR
Reid Spencer4fe16d62007-01-11 18:21:29 +00002610 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002611 CHECK_FOR_ERROR
2612 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattner58af2a12006-02-15 07:22:58 +00002613 }
2614 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002615 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002616 CHECK_FOR_ERROR
2617 BasicBlock* tmpBB = getBBVal($6);
2618 CHECK_FOR_ERROR
2619 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002620 $$ = S;
2621
2622 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2623 E = $8->end();
2624 for (; I != E; ++I) {
2625 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2626 S->addCase(CI, I->second);
2627 else
Reid Spencerb5334b02007-02-05 10:18:06 +00002628 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattner58af2a12006-02-15 07:22:58 +00002629 }
2630 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002631 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002632 }
2633 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002634 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002635 CHECK_FOR_ERROR
2636 BasicBlock* tmpBB = getBBVal($6);
2637 CHECK_FOR_ERROR
2638 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattner58af2a12006-02-15 07:22:58 +00002639 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002640 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002641 }
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002642 | INVOKE OptCallingConv ResultTypes ValueRef '(' ParamList ')' OptFuncAttrs
Chris Lattner58af2a12006-02-15 07:22:58 +00002643 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattner58af2a12006-02-15 07:22:58 +00002644
Reid Spencer14310612006-12-31 05:40:51 +00002645 // Handle the short syntax
2646 const PointerType *PFTy = 0;
2647 const FunctionType *Ty = 0;
Reid Spencer218ded22007-01-05 17:07:23 +00002648 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00002649 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2650 // Pull out the types of all of the arguments...
2651 std::vector<const Type*> ParamTypes;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002652 ParamList::iterator I = $6->begin(), E = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002653 for (; I != E; ++I) {
Reid Spencer14310612006-12-31 05:40:51 +00002654 const Type *Ty = I->Val->getType();
2655 if (Ty == Type::VoidTy)
2656 GEN_ERROR("Short call syntax cannot be used with varargs");
2657 ParamTypes.push_back(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002658 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002659 Ty = FunctionType::get($3->get(), ParamTypes, false);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002660 PFTy = PointerType::getUnqual(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002661 }
2662
Reid Spencer66728ef2007-03-20 01:13:36 +00002663 delete $3;
2664
Chris Lattner58af2a12006-02-15 07:22:58 +00002665 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002666 CHECK_FOR_ERROR
Reid Spencer218ded22007-01-05 17:07:23 +00002667 BasicBlock *Normal = getBBVal($11);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002668 CHECK_FOR_ERROR
Reid Spencer218ded22007-01-05 17:07:23 +00002669 BasicBlock *Except = getBBVal($14);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002670 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002671
Duncan Sandsdc024672007-11-27 13:23:08 +00002672 ParamAttrsVector Attrs;
2673 if ($8 != ParamAttr::None) {
2674 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $8;
2675 Attrs.push_back(PAWI);
2676 }
2677
Reid Spencer14310612006-12-31 05:40:51 +00002678 // Check the arguments
2679 ValueList Args;
2680 if ($6->empty()) { // Has no arguments?
2681 // Make sure no arguments is a good thing!
2682 if (Ty->getNumParams() != 0)
2683 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00002684 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00002685 } else { // Has arguments?
2686 // Loop through FunctionType's arguments and ensure they are specified
2687 // correctly!
Chris Lattner58af2a12006-02-15 07:22:58 +00002688 FunctionType::param_iterator I = Ty->param_begin();
2689 FunctionType::param_iterator E = Ty->param_end();
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002690 ParamList::iterator ArgI = $6->begin(), ArgE = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002691 unsigned index = 1;
Chris Lattner58af2a12006-02-15 07:22:58 +00002692
Duncan Sandsdc024672007-11-27 13:23:08 +00002693 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002694 if (ArgI->Val->getType() != *I)
2695 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00002696 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00002697 Args.push_back(ArgI->Val);
Duncan Sandsdc024672007-11-27 13:23:08 +00002698 if (ArgI->Attrs != ParamAttr::None) {
2699 ParamAttrsWithIndex PAWI;
2700 PAWI.index = index;
2701 PAWI.attrs = ArgI->Attrs;
2702 Attrs.push_back(PAWI);
2703 }
Reid Spencer14310612006-12-31 05:40:51 +00002704 }
Reid Spencera132e042006-12-03 05:46:11 +00002705
Reid Spencer14310612006-12-31 05:40:51 +00002706 if (Ty->isVarArg()) {
2707 if (I == E)
Chris Lattner38905612008-02-19 04:36:25 +00002708 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002709 Args.push_back(ArgI->Val); // push the remaining varargs
Chris Lattner38905612008-02-19 04:36:25 +00002710 if (ArgI->Attrs != ParamAttr::None) {
2711 ParamAttrsWithIndex PAWI;
2712 PAWI.index = index;
2713 PAWI.attrs = ArgI->Attrs;
2714 Attrs.push_back(PAWI);
2715 }
2716 }
Reid Spencer14310612006-12-31 05:40:51 +00002717 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00002718 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00002719 }
Reid Spencer14310612006-12-31 05:40:51 +00002720
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00002721 const ParamAttrsList *PAL = 0;
Duncan Sandsdc024672007-11-27 13:23:08 +00002722 if (!Attrs.empty())
2723 PAL = ParamAttrsList::get(Attrs);
2724
Reid Spencer14310612006-12-31 05:40:51 +00002725 // Create the InvokeInst
Chris Lattnerd80fb8b2007-08-29 16:15:23 +00002726 InvokeInst *II = new InvokeInst(V, Normal, Except, Args.begin(), Args.end());
Reid Spencer14310612006-12-31 05:40:51 +00002727 II->setCallingConv($2);
Duncan Sandsdc024672007-11-27 13:23:08 +00002728 II->setParamAttrs(PAL);
Reid Spencer14310612006-12-31 05:40:51 +00002729 $$ = II;
Chris Lattner58af2a12006-02-15 07:22:58 +00002730 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002731 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002732 }
2733 | UNWIND {
2734 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002735 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002736 }
2737 | UNREACHABLE {
2738 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002739 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002740 };
2741
2742
2743
2744JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2745 $$ = $1;
Reid Spencer93c40032007-03-19 18:40:50 +00002746 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002747 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002748 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002749 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00002750
Reid Spencer5b7e7532006-09-28 19:28:24 +00002751 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002752 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002753 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002754 }
2755 | IntType ConstValueRef ',' LABEL ValueRef {
2756 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencer93c40032007-03-19 18:40:50 +00002757 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002758 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002759
2760 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002761 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00002762
Reid Spencer5b7e7532006-09-28 19:28:24 +00002763 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002764 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002765 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002766 };
2767
Reid Spencer41dff5e2007-01-26 08:05:27 +00002768Inst : OptLocalAssign InstVal {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002769 // Is this definition named?? if so, assign the name...
2770 setValueName($2, $1);
2771 CHECK_FOR_ERROR
2772 InsertValue($2);
2773 $$ = $2;
2774 CHECK_FOR_ERROR
2775 };
2776
Chris Lattner58af2a12006-02-15 07:22:58 +00002777
2778PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer14310612006-12-31 05:40:51 +00002779 if (!UpRefs.empty())
2780 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002781 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencera132e042006-12-03 05:46:11 +00002782 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002783 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002784 BasicBlock* tmpBB = getBBVal($5);
2785 CHECK_FOR_ERROR
2786 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencera132e042006-12-03 05:46:11 +00002787 delete $1;
Chris Lattner58af2a12006-02-15 07:22:58 +00002788 }
2789 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2790 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002791 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002792 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002793 BasicBlock* tmpBB = getBBVal($6);
2794 CHECK_FOR_ERROR
2795 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002796 };
2797
2798
Duncan Sandsdc024672007-11-27 13:23:08 +00002799ParamList : Types OptParamAttrs ValueRef OptParamAttrs {
2800 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Reid Spencer14310612006-12-31 05:40:51 +00002801 if (!UpRefs.empty())
2802 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2803 // Used for call and invoke instructions
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002804 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00002805 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getVal($1->get(), $3);
Reid Spencer14310612006-12-31 05:40:51 +00002806 $$->push_back(E);
Reid Spencer66728ef2007-03-20 01:13:36 +00002807 delete $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002808 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002809 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002810 | LABEL OptParamAttrs ValueRef OptParamAttrs {
2811 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002812 // Labels are only valid in ASMs
2813 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00002814 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getBBVal($3);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002815 $$->push_back(E);
Duncan Sandsdc024672007-11-27 13:23:08 +00002816 CHECK_FOR_ERROR
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002817 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002818 | ParamList ',' Types OptParamAttrs ValueRef OptParamAttrs {
2819 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Reid Spencer14310612006-12-31 05:40:51 +00002820 if (!UpRefs.empty())
2821 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002822 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002823 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getVal($3->get(), $5);
Reid Spencer14310612006-12-31 05:40:51 +00002824 $$->push_back(E);
Reid Spencer66728ef2007-03-20 01:13:36 +00002825 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002826 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00002827 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002828 | ParamList ',' LABEL OptParamAttrs ValueRef OptParamAttrs {
2829 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002830 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002831 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getBBVal($5);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002832 $$->push_back(E);
2833 CHECK_FOR_ERROR
2834 }
2835 | /*empty*/ { $$ = new ParamList(); };
Chris Lattner58af2a12006-02-15 07:22:58 +00002836
Reid Spencer14310612006-12-31 05:40:51 +00002837IndexList // Used for gep instructions and constant expressions
Reid Spencerc6c59fd2006-12-31 21:47:02 +00002838 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencer14310612006-12-31 05:40:51 +00002839 | IndexList ',' ResolvedVal {
2840 $$ = $1;
2841 $$->push_back($3);
2842 CHECK_FOR_ERROR
2843 }
Reid Spencerc6c59fd2006-12-31 21:47:02 +00002844 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002845
2846OptTailCall : TAIL CALL {
2847 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002848 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002849 }
2850 | CALL {
2851 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002852 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002853 };
2854
Chris Lattner58af2a12006-02-15 07:22:58 +00002855InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002856 if (!UpRefs.empty())
2857 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002858 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencer9d6565a2007-02-15 02:26:10 +00002859 !isa<VectorType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002860 GEN_ERROR(
Reid Spencerb5334b02007-02-05 10:18:06 +00002861 "Arithmetic operator requires integer, FP, or packed operands");
Reid Spencera132e042006-12-03 05:46:11 +00002862 Value* val1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002863 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002864 Value* val2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002865 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002866 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002867 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002868 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00002869 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002870 }
2871 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002872 if (!UpRefs.empty())
2873 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002874 if (!(*$2)->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00002875 if (Instruction::isShift($1) || !isa<VectorType>($2->get()) ||
2876 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00002877 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00002878 }
Reid Spencera132e042006-12-03 05:46:11 +00002879 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002880 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002881 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002882 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002883 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002884 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002885 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00002886 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002887 }
Reid Spencera132e042006-12-03 05:46:11 +00002888 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002889 if (!UpRefs.empty())
2890 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002891 if (isa<VectorType>((*$3).get()))
Chris Lattner32980692007-02-19 07:44:24 +00002892 GEN_ERROR("Vector types not supported by icmp instruction");
Reid Spencera132e042006-12-03 05:46:11 +00002893 Value* tmpVal1 = getVal(*$3, $4);
2894 CHECK_FOR_ERROR
2895 Value* tmpVal2 = getVal(*$3, $6);
2896 CHECK_FOR_ERROR
2897 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2898 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002899 GEN_ERROR("icmp operator returned null");
Reid Spencer66728ef2007-03-20 01:13:36 +00002900 delete $3;
Reid Spencera132e042006-12-03 05:46:11 +00002901 }
2902 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002903 if (!UpRefs.empty())
2904 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002905 if (isa<VectorType>((*$3).get()))
Chris Lattner32980692007-02-19 07:44:24 +00002906 GEN_ERROR("Vector types not supported by fcmp instruction");
Reid Spencera132e042006-12-03 05:46:11 +00002907 Value* tmpVal1 = getVal(*$3, $4);
2908 CHECK_FOR_ERROR
2909 Value* tmpVal2 = getVal(*$3, $6);
2910 CHECK_FOR_ERROR
2911 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2912 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002913 GEN_ERROR("fcmp operator returned null");
Reid Spencer66728ef2007-03-20 01:13:36 +00002914 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00002915 }
Reid Spencer3da59db2006-11-27 01:05:10 +00002916 | CastOps ResolvedVal TO Types {
Reid Spencer14310612006-12-31 05:40:51 +00002917 if (!UpRefs.empty())
2918 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00002919 Value* Val = $2;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00002920 const Type* DestTy = $4->get();
2921 if (!CastInst::castIsValid($1, Val, DestTy))
2922 GEN_ERROR("invalid cast opcode for cast from '" +
2923 Val->getType()->getDescription() + "' to '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00002924 DestTy->getDescription() + "'");
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00002925 $$ = CastInst::create($1, Val, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00002926 delete $4;
Chris Lattner58af2a12006-02-15 07:22:58 +00002927 }
2928 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer4fe16d62007-01-11 18:21:29 +00002929 if ($2->getType() != Type::Int1Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00002930 GEN_ERROR("select condition must be boolean");
Reid Spencera132e042006-12-03 05:46:11 +00002931 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002932 GEN_ERROR("select value types should match");
Reid Spencera132e042006-12-03 05:46:11 +00002933 $$ = new SelectInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002934 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002935 }
2936 | VAARG ResolvedVal ',' Types {
Reid Spencer14310612006-12-31 05:40:51 +00002937 if (!UpRefs.empty())
2938 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00002939 $$ = new VAArgInst($2, *$4);
2940 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002941 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002942 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002943 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002944 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerb5334b02007-02-05 10:18:06 +00002945 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00002946 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002947 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002948 }
2949 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002950 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00002951 GEN_ERROR("Invalid insertelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00002952 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002953 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002954 }
Chris Lattnerd5efe842006-04-08 01:18:56 +00002955 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002956 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00002957 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00002958 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002959 CHECK_FOR_ERROR
Chris Lattnerd5efe842006-04-08 01:18:56 +00002960 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002961 | PHI_TOK PHIList {
2962 const Type *Ty = $2->front().first->getType();
2963 if (!Ty->isFirstClassType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002964 GEN_ERROR("PHI node operands must be of first class type");
Chris Lattner58af2a12006-02-15 07:22:58 +00002965 $$ = new PHINode(Ty);
2966 ((PHINode*)$$)->reserveOperandSpace($2->size());
2967 while ($2->begin() != $2->end()) {
2968 if ($2->front().first->getType() != Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00002969 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattner58af2a12006-02-15 07:22:58 +00002970 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
2971 $2->pop_front();
2972 }
2973 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00002974 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002975 }
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002976 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ParamList ')'
Reid Spencer218ded22007-01-05 17:07:23 +00002977 OptFuncAttrs {
Reid Spencer14310612006-12-31 05:40:51 +00002978
2979 // Handle the short syntax
Reid Spencer3da59db2006-11-27 01:05:10 +00002980 const PointerType *PFTy = 0;
2981 const FunctionType *Ty = 0;
Reid Spencer218ded22007-01-05 17:07:23 +00002982 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00002983 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2984 // Pull out the types of all of the arguments...
2985 std::vector<const Type*> ParamTypes;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002986 ParamList::iterator I = $6->begin(), E = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002987 for (; I != E; ++I) {
Reid Spencer14310612006-12-31 05:40:51 +00002988 const Type *Ty = I->Val->getType();
2989 if (Ty == Type::VoidTy)
2990 GEN_ERROR("Short call syntax cannot be used with varargs");
2991 ParamTypes.push_back(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002992 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002993 Ty = FunctionType::get($3->get(), ParamTypes, false);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002994 PFTy = PointerType::getUnqual(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002995 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002996
Chris Lattner58af2a12006-02-15 07:22:58 +00002997 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002998 CHECK_FOR_ERROR
Chris Lattner6cdc6822007-04-26 05:31:05 +00002999
Reid Spencer7780acb2007-04-16 06:56:07 +00003000 // Check for call to invalid intrinsic to avoid crashing later.
3001 if (Function *theF = dyn_cast<Function>(V)) {
Reid Spencered48de22007-04-16 22:02:23 +00003002 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
Reid Spencer36fdde12007-04-16 20:35:38 +00003003 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
3004 !theF->getIntrinsicID(true))
Reid Spencer7780acb2007-04-16 06:56:07 +00003005 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
3006 theF->getName() + "'");
3007 }
3008
Duncan Sandsdc024672007-11-27 13:23:08 +00003009 // Set up the ParamAttrs for the function
3010 ParamAttrsVector Attrs;
3011 if ($8 != ParamAttr::None) {
3012 ParamAttrsWithIndex PAWI;
3013 PAWI.index = 0;
3014 PAWI.attrs = $8;
3015 Attrs.push_back(PAWI);
3016 }
Reid Spencer14310612006-12-31 05:40:51 +00003017 // Check the arguments
3018 ValueList Args;
3019 if ($6->empty()) { // Has no arguments?
Chris Lattner58af2a12006-02-15 07:22:58 +00003020 // Make sure no arguments is a good thing!
3021 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00003022 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00003023 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00003024 } else { // Has arguments?
3025 // Loop through FunctionType's arguments and ensure they are specified
Duncan Sandsdc024672007-11-27 13:23:08 +00003026 // correctly. Also, gather any parameter attributes.
Chris Lattner58af2a12006-02-15 07:22:58 +00003027 FunctionType::param_iterator I = Ty->param_begin();
3028 FunctionType::param_iterator E = Ty->param_end();
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003029 ParamList::iterator ArgI = $6->begin(), ArgE = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00003030 unsigned index = 1;
Chris Lattner58af2a12006-02-15 07:22:58 +00003031
Duncan Sandsdc024672007-11-27 13:23:08 +00003032 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00003033 if (ArgI->Val->getType() != *I)
3034 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00003035 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00003036 Args.push_back(ArgI->Val);
Duncan Sandsdc024672007-11-27 13:23:08 +00003037 if (ArgI->Attrs != ParamAttr::None) {
3038 ParamAttrsWithIndex PAWI;
3039 PAWI.index = index;
3040 PAWI.attrs = ArgI->Attrs;
3041 Attrs.push_back(PAWI);
3042 }
Reid Spencer14310612006-12-31 05:40:51 +00003043 }
3044 if (Ty->isVarArg()) {
3045 if (I == E)
Chris Lattner38905612008-02-19 04:36:25 +00003046 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00003047 Args.push_back(ArgI->Val); // push the remaining varargs
Chris Lattner38905612008-02-19 04:36:25 +00003048 if (ArgI->Attrs != ParamAttr::None) {
3049 ParamAttrsWithIndex PAWI;
3050 PAWI.index = index;
3051 PAWI.attrs = ArgI->Attrs;
3052 Attrs.push_back(PAWI);
3053 }
3054 }
Reid Spencer14310612006-12-31 05:40:51 +00003055 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00003056 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00003057 }
Duncan Sandsdc024672007-11-27 13:23:08 +00003058
3059 // Finish off the ParamAttrs and check them
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00003060 const ParamAttrsList *PAL = 0;
Duncan Sandsdc024672007-11-27 13:23:08 +00003061 if (!Attrs.empty())
3062 PAL = ParamAttrsList::get(Attrs);
3063
Reid Spencer14310612006-12-31 05:40:51 +00003064 // Create the call node
David Greene718fda32007-08-01 03:59:32 +00003065 CallInst *CI = new CallInst(V, Args.begin(), Args.end());
Reid Spencer14310612006-12-31 05:40:51 +00003066 CI->setTailCall($1);
3067 CI->setCallingConv($2);
Duncan Sandsdc024672007-11-27 13:23:08 +00003068 CI->setParamAttrs(PAL);
Reid Spencer14310612006-12-31 05:40:51 +00003069 $$ = CI;
Chris Lattner58af2a12006-02-15 07:22:58 +00003070 delete $6;
Reid Spencer41dff5e2007-01-26 08:05:27 +00003071 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00003072 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003073 }
3074 | MemoryInst {
3075 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00003076 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003077 };
3078
Chris Lattner58af2a12006-02-15 07:22:58 +00003079OptVolatile : VOLATILE {
3080 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00003081 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003082 }
3083 | /* empty */ {
3084 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00003085 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003086 };
3087
3088
3089
3090MemoryInst : MALLOC Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003091 if (!UpRefs.empty())
3092 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003093 $$ = new MallocInst(*$2, 0, $3);
3094 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003095 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003096 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003097 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003098 if (!UpRefs.empty())
3099 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003100 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003101 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003102 $$ = new MallocInst(*$2, tmpVal, $6);
3103 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003104 }
3105 | ALLOCA Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003106 if (!UpRefs.empty())
3107 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003108 $$ = new AllocaInst(*$2, 0, $3);
3109 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003110 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003111 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003112 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003113 if (!UpRefs.empty())
3114 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003115 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003116 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003117 $$ = new AllocaInst(*$2, tmpVal, $6);
3118 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003119 }
3120 | FREE ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003121 if (!isa<PointerType>($2->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003122 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerb5334b02007-02-05 10:18:06 +00003123 $2->getType()->getDescription() + "");
Reid Spencera132e042006-12-03 05:46:11 +00003124 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00003125 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003126 }
3127
Christopher Lamb5c104242007-04-22 20:09:11 +00003128 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003129 if (!UpRefs.empty())
3130 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003131 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003132 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003133 (*$3)->getDescription());
3134 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00003135 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003136 (*$3)->getDescription());
3137 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003138 CHECK_FOR_ERROR
Christopher Lamb5c104242007-04-22 20:09:11 +00003139 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencera132e042006-12-03 05:46:11 +00003140 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00003141 }
Christopher Lamb5c104242007-04-22 20:09:11 +00003142 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003143 if (!UpRefs.empty())
3144 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003145 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00003146 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00003147 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003148 (*$5)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00003149 const Type *ElTy = PT->getElementType();
Reid Spencera132e042006-12-03 05:46:11 +00003150 if (ElTy != $3->getType())
3151 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerb5334b02007-02-05 10:18:06 +00003152 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00003153
Reid Spencera132e042006-12-03 05:46:11 +00003154 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003155 CHECK_FOR_ERROR
Christopher Lamb5c104242007-04-22 20:09:11 +00003156 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencera132e042006-12-03 05:46:11 +00003157 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00003158 }
Devang Patelbd41a062008-02-22 19:31:30 +00003159| GETRESULT Types SymbolicValueRef ',' EUINT64VAL {
3160 Value *TmpVal = getVal($2->get(), $3);
Devang Patel5a970972008-02-19 22:27:01 +00003161 if (!GetResultInst::isValidOperands(TmpVal, $5))
3162 GEN_ERROR("Invalid getresult operands");
3163 $$ = new GetResultInst(TmpVal, $5);
Devang Patel6bfc63b2008-02-23 00:38:56 +00003164 delete $2;
Devang Patel5a970972008-02-19 22:27:01 +00003165 CHECK_FOR_ERROR
3166 }
Chris Lattner58af2a12006-02-15 07:22:58 +00003167 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer14310612006-12-31 05:40:51 +00003168 if (!UpRefs.empty())
3169 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003170 if (!isa<PointerType>($2->get()))
Reid Spencerb5334b02007-02-05 10:18:06 +00003171 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00003172
David Greene5fd22a82007-09-04 18:46:50 +00003173 if (!GetElementPtrInst::getIndexedType(*$2, $4->begin(), $4->end(), true))
Reid Spencer61c83e02006-08-18 08:43:06 +00003174 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00003175 (*$2)->getDescription()+ "'");
Reid Spencera132e042006-12-03 05:46:11 +00003176 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003177 CHECK_FOR_ERROR
David Greene5fd22a82007-09-04 18:46:50 +00003178 $$ = new GetElementPtrInst(tmpVal, $4->begin(), $4->end());
Reid Spencera132e042006-12-03 05:46:11 +00003179 delete $2;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003180 delete $4;
Chris Lattner58af2a12006-02-15 07:22:58 +00003181 };
3182
3183
3184%%
Reid Spencer61c83e02006-08-18 08:43:06 +00003185
Reid Spencer14310612006-12-31 05:40:51 +00003186// common code from the two 'RunVMAsmParser' functions
3187static Module* RunParser(Module * M) {
Reid Spencer14310612006-12-31 05:40:51 +00003188 CurModule.CurrentModule = M;
Reid Spencer14310612006-12-31 05:40:51 +00003189 // Check to make sure the parser succeeded
3190 if (yyparse()) {
3191 if (ParserResult)
3192 delete ParserResult;
3193 return 0;
3194 }
3195
Reid Spencer0d60b5a2007-03-30 01:37:39 +00003196 // Emit an error if there are any unresolved types left.
3197 if (!CurModule.LateResolveTypes.empty()) {
3198 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3199 if (DID.Type == ValID::LocalName) {
3200 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3201 } else {
3202 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3203 }
3204 if (ParserResult)
3205 delete ParserResult;
3206 return 0;
3207 }
3208
3209 // Emit an error if there are any unresolved values left.
3210 if (!CurModule.LateResolveValues.empty()) {
3211 Value *V = CurModule.LateResolveValues.back();
3212 std::map<Value*, std::pair<ValID, int> >::iterator I =
3213 CurModule.PlaceHolderInfo.find(V);
3214
3215 if (I != CurModule.PlaceHolderInfo.end()) {
3216 ValID &DID = I->second.first;
3217 if (DID.Type == ValID::LocalName) {
3218 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3219 } else {
3220 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3221 }
3222 if (ParserResult)
3223 delete ParserResult;
3224 return 0;
3225 }
3226 }
3227
Reid Spencer14310612006-12-31 05:40:51 +00003228 // Check to make sure that parsing produced a result
3229 if (!ParserResult)
3230 return 0;
3231
3232 // Reset ParserResult variable while saving its value for the result.
3233 Module *Result = ParserResult;
3234 ParserResult = 0;
3235
3236 return Result;
3237}
3238
Reid Spencer61c83e02006-08-18 08:43:06 +00003239void llvm::GenerateError(const std::string &message, int LineNo) {
Duncan Sandsdc024672007-11-27 13:23:08 +00003240 if (LineNo == -1) LineNo = LLLgetLineNo();
Reid Spencer61c83e02006-08-18 08:43:06 +00003241 // TODO: column number in exception
3242 if (TheParseError)
Duncan Sandsdc024672007-11-27 13:23:08 +00003243 TheParseError->setError(LLLgetFilename(), message, LineNo);
Reid Spencer61c83e02006-08-18 08:43:06 +00003244 TriggerError = 1;
3245}
3246
Chris Lattner58af2a12006-02-15 07:22:58 +00003247int yyerror(const char *ErrorMsg) {
Duncan Sandsdc024672007-11-27 13:23:08 +00003248 std::string where = LLLgetFilename() + ":" + utostr(LLLgetLineNo()) + ": ";
Reid Spenceref9b9a72007-02-05 20:47:22 +00003249 std::string errMsg = where + "error: " + std::string(ErrorMsg);
Duncan Sandsdc024672007-11-27 13:23:08 +00003250 if (yychar != YYEMPTY && yychar != 0) {
3251 errMsg += " while reading token: '";
3252 errMsg += std::string(LLLgetTokenStart(),
3253 LLLgetTokenStart()+LLLgetTokenLength()) + "'";
3254 }
Reid Spencer61c83e02006-08-18 08:43:06 +00003255 GenerateError(errMsg);
Chris Lattner58af2a12006-02-15 07:22:58 +00003256 return 0;
3257}