blob: 36b56eabc74648786dd347d531dba54f1d554ee8 [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"
Chris Lattner58af2a12006-02-15 07:22:58 +000028#include <algorithm>
Chris Lattner58af2a12006-02-15 07:22:58 +000029#include <list>
Chris Lattner8adde282007-02-11 21:40:10 +000030#include <map>
Chris Lattner58af2a12006-02-15 07:22:58 +000031#include <utility>
32
Reid Spencere4f47592006-08-18 17:32:55 +000033// The following is a gross hack. In order to rid the libAsmParser library of
34// exceptions, we have to have a way of getting the yyparse function to go into
35// an error situation. So, whenever we want an error to occur, the GenerateError
36// function (see bottom of file) sets TriggerError. Then, at the end of each
37// production in the grammer we use CHECK_FOR_ERROR which will invoke YYERROR
38// (a goto) to put YACC in error state. Furthermore, several calls to
39// GenerateError are made from inside productions and they must simulate the
40// previous exception behavior by exiting the production immediately. We have
41// replaced these with the GEN_ERROR macro which calls GeneratError and then
42// immediately invokes YYERROR. This would be so much cleaner if it was a
43// recursive descent parser.
Reid Spencer61c83e02006-08-18 08:43:06 +000044static bool TriggerError = false;
Reid Spencerf63697d2006-10-09 17:36:59 +000045#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYABORT; } }
Reid Spencer61c83e02006-08-18 08:43:06 +000046#define GEN_ERROR(msg) { GenerateError(msg); YYERROR; }
47
Chris Lattner58af2a12006-02-15 07:22:58 +000048int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
49int yylex(); // declaration" of xxx warnings.
50int yyparse();
Chris Lattner58af2a12006-02-15 07:22:58 +000051using namespace llvm;
52
53static Module *ParserResult;
54
55// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
56// relating to upreferences in the input stream.
57//
58//#define DEBUG_UPREFS 1
59#ifdef DEBUG_UPREFS
Bill Wendlinge8156192006-12-07 01:30:32 +000060#define UR_OUT(X) cerr << X
Chris Lattner58af2a12006-02-15 07:22:58 +000061#else
62#define UR_OUT(X)
63#endif
64
65#define YYERROR_VERBOSE 1
66
Chris Lattner58af2a12006-02-15 07:22:58 +000067static GlobalVariable *CurGV;
68
69
70// This contains info used when building the body of a function. It is
71// destroyed when the function is completed.
72//
73typedef std::vector<Value *> ValueList; // Numbered defs
Reid Spencer14310612006-12-31 05:40:51 +000074
Chris Lattner58af2a12006-02-15 07:22:58 +000075static void
Reid Spencer93c40032007-03-19 18:40:50 +000076ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers=0);
Chris Lattner58af2a12006-02-15 07:22:58 +000077
78static struct PerModuleInfo {
79 Module *CurrentModule;
Reid Spencer93c40032007-03-19 18:40:50 +000080 ValueList Values; // Module level numbered definitions
81 ValueList LateResolveValues;
Reid Spencer861d9d62006-11-28 07:29:44 +000082 std::vector<PATypeHolder> Types;
83 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner58af2a12006-02-15 07:22:58 +000084
85 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Chris Lattner0ad19702006-06-21 16:53:00 +000086 /// how they were referenced and on which line of the input they came from so
Chris Lattner58af2a12006-02-15 07:22:58 +000087 /// that we can resolve them later and print error messages as appropriate.
88 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
89
90 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
91 // references to global values. Global values may be referenced before they
92 // are defined, and if so, the temporary object that they represent is held
93 // here. This is used for forward references of GlobalValues.
94 //
95 typedef std::map<std::pair<const PointerType *,
96 ValID>, GlobalValue*> GlobalRefsType;
97 GlobalRefsType GlobalRefs;
98
99 void ModuleDone() {
100 // If we could not resolve some functions at function compilation time
101 // (calls to functions before they are defined), resolve them now... Types
102 // are resolved when the constant pool has been completely parsed.
103 //
104 ResolveDefinitions(LateResolveValues);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000105 if (TriggerError)
106 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000107
108 // Check to make sure that all global value forward references have been
109 // resolved!
110 //
111 if (!GlobalRefs.empty()) {
112 std::string UndefinedReferences = "Unresolved global references exist:\n";
113
114 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
115 I != E; ++I) {
116 UndefinedReferences += " " + I->first.first->getDescription() + " " +
117 I->first.second.getName() + "\n";
118 }
Reid Spencer61c83e02006-08-18 08:43:06 +0000119 GenerateError(UndefinedReferences);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000120 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000121 }
122
Chandler Carruth02202192007-08-04 01:56:21 +0000123 // Look for intrinsic functions and CallInst that need to be upgraded
124 for (Module::iterator FI = CurrentModule->begin(),
125 FE = CurrentModule->end(); FI != FE; )
126 UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove
127
Chris Lattner58af2a12006-02-15 07:22:58 +0000128 Values.clear(); // Clear out function local definitions
129 Types.clear();
130 CurrentModule = 0;
131 }
132
133 // GetForwardRefForGlobal - Check to see if there is a forward reference
134 // for this global. If so, remove it from the GlobalRefs map and return it.
135 // If not, just return null.
136 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
137 // Check to see if there is a forward reference to this global variable...
138 // if there is, eliminate it and patch the reference to use the new def'n.
139 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
140 GlobalValue *Ret = 0;
141 if (I != GlobalRefs.end()) {
142 Ret = I->second;
143 GlobalRefs.erase(I);
144 }
145 return Ret;
146 }
Reid Spencer8c8a2dc2007-01-02 21:54:12 +0000147
148 bool TypeIsUnresolved(PATypeHolder* PATy) {
149 // If it isn't abstract, its resolved
150 const Type* Ty = PATy->get();
151 if (!Ty->isAbstract())
152 return false;
153 // Traverse the type looking for abstract types. If it isn't abstract then
154 // we don't need to traverse that leg of the type.
155 std::vector<const Type*> WorkList, SeenList;
156 WorkList.push_back(Ty);
157 while (!WorkList.empty()) {
158 const Type* Ty = WorkList.back();
159 SeenList.push_back(Ty);
160 WorkList.pop_back();
161 if (const OpaqueType* OpTy = dyn_cast<OpaqueType>(Ty)) {
162 // Check to see if this is an unresolved type
163 std::map<ValID, PATypeHolder>::iterator I = LateResolveTypes.begin();
164 std::map<ValID, PATypeHolder>::iterator E = LateResolveTypes.end();
165 for ( ; I != E; ++I) {
166 if (I->second.get() == OpTy)
167 return true;
168 }
169 } else if (const SequentialType* SeqTy = dyn_cast<SequentialType>(Ty)) {
170 const Type* TheTy = SeqTy->getElementType();
171 if (TheTy->isAbstract() && TheTy != Ty) {
172 std::vector<const Type*>::iterator I = SeenList.begin(),
173 E = SeenList.end();
174 for ( ; I != E; ++I)
175 if (*I == TheTy)
176 break;
177 if (I == E)
178 WorkList.push_back(TheTy);
179 }
180 } else if (const StructType* StrTy = dyn_cast<StructType>(Ty)) {
181 for (unsigned i = 0; i < StrTy->getNumElements(); ++i) {
182 const Type* TheTy = StrTy->getElementType(i);
183 if (TheTy->isAbstract() && TheTy != Ty) {
184 std::vector<const Type*>::iterator I = SeenList.begin(),
185 E = SeenList.end();
186 for ( ; I != E; ++I)
187 if (*I == TheTy)
188 break;
189 if (I == E)
190 WorkList.push_back(TheTy);
191 }
192 }
193 }
194 }
195 return false;
196 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000197} CurModule;
198
199static struct PerFunctionInfo {
200 Function *CurrentFunction; // Pointer to current function being created
201
Reid Spencer93c40032007-03-19 18:40:50 +0000202 ValueList Values; // Keep track of #'d definitions
203 unsigned NextValNum;
204 ValueList LateResolveValues;
Reid Spenceref9b9a72007-02-05 20:47:22 +0000205 bool isDeclare; // Is this function a forward declararation?
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000206 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000207 GlobalValue::VisibilityTypes Visibility;
Chris Lattner58af2a12006-02-15 07:22:58 +0000208
209 /// BBForwardRefs - When we see forward references to basic blocks, keep
210 /// track of them here.
Reid Spencer93c40032007-03-19 18:40:50 +0000211 std::map<ValID, BasicBlock*> BBForwardRefs;
Chris Lattner58af2a12006-02-15 07:22:58 +0000212
213 inline PerFunctionInfo() {
214 CurrentFunction = 0;
215 isDeclare = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000216 Linkage = GlobalValue::ExternalLinkage;
217 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner58af2a12006-02-15 07:22:58 +0000218 }
219
220 inline void FunctionStart(Function *M) {
221 CurrentFunction = M;
Reid Spencer93c40032007-03-19 18:40:50 +0000222 NextValNum = 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000223 }
224
225 void FunctionDone() {
Chris Lattner58af2a12006-02-15 07:22:58 +0000226 // Any forward referenced blocks left?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000227 if (!BBForwardRefs.empty()) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000228 GenerateError("Undefined reference to label " +
Reid Spencer93c40032007-03-19 18:40:50 +0000229 BBForwardRefs.begin()->second->getName());
Reid Spencer5b7e7532006-09-28 19:28:24 +0000230 return;
231 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000232
233 // Resolve all forward references now.
234 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
235
236 Values.clear(); // Clear out function local definitions
Reid Spencer93c40032007-03-19 18:40:50 +0000237 BBForwardRefs.clear();
Chris Lattner58af2a12006-02-15 07:22:58 +0000238 CurrentFunction = 0;
239 isDeclare = false;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000240 Linkage = GlobalValue::ExternalLinkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000241 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner58af2a12006-02-15 07:22:58 +0000242 }
243} CurFun; // Info for the current function...
244
245static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
246
247
248//===----------------------------------------------------------------------===//
249// Code to handle definitions of all the types
250//===----------------------------------------------------------------------===//
251
Chris Lattner15bd0952008-08-29 17:20:18 +0000252/// InsertValue - Insert a value into the value table. If it is named, this
253/// returns -1, otherwise it returns the slot number for the value.
254static int InsertValue(Value *V, ValueList &ValueTab = CurFun.Values) {
Reid Spencer93c40032007-03-19 18:40:50 +0000255 // Things that have names or are void typed don't get slot numbers
256 if (V->hasName() || (V->getType() == Type::VoidTy))
Chris Lattner15bd0952008-08-29 17:20:18 +0000257 return -1;
Chris Lattner58af2a12006-02-15 07:22:58 +0000258
Reid Spencer93c40032007-03-19 18:40:50 +0000259 // In the case of function values, we have to allow for the forward reference
260 // of basic blocks, which are included in the numbering. Consequently, we keep
261 // track of the next insertion location with NextValNum. When a BB gets
262 // inserted, it could change the size of the CurFun.Values vector.
263 if (&ValueTab == &CurFun.Values) {
264 if (ValueTab.size() <= CurFun.NextValNum)
265 ValueTab.resize(CurFun.NextValNum+1);
266 ValueTab[CurFun.NextValNum++] = V;
Chris Lattner15bd0952008-08-29 17:20:18 +0000267 return CurFun.NextValNum-1;
Reid Spencer93c40032007-03-19 18:40:50 +0000268 }
269 // For all other lists, its okay to just tack it on the back of the vector.
270 ValueTab.push_back(V);
Chris Lattner15bd0952008-08-29 17:20:18 +0000271 return ValueTab.size()-1;
Chris Lattner58af2a12006-02-15 07:22:58 +0000272}
273
274static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
275 switch (D.Type) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000276 case ValID::LocalID: // Is it a numbered definition?
Chris Lattner58af2a12006-02-15 07:22:58 +0000277 // Module constants occupy the lowest numbered slots...
Reid Spencer41dff5e2007-01-26 08:05:27 +0000278 if (D.Num < CurModule.Types.size())
279 return CurModule.Types[D.Num];
Chris Lattner58af2a12006-02-15 07:22:58 +0000280 break;
Reid Spencer41dff5e2007-01-26 08:05:27 +0000281 case ValID::LocalName: // Is it a named definition?
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000282 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.getName())) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000283 D.destroy(); // Free old strdup'd memory...
284 return N;
285 }
286 break;
287 default:
Reid Spencerb5334b02007-02-05 10:18:06 +0000288 GenerateError("Internal parser error: Invalid symbol type reference");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000289 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000290 }
291
292 // If we reached here, we referenced either a symbol that we don't know about
293 // or an id number that hasn't been read yet. We may be referencing something
294 // forward, so just create an entry to be resolved later and get to it...
295 //
296 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
297
298
299 if (inFunctionScope()) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000300 if (D.Type == ValID::LocalName) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000301 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000302 return 0;
303 } else {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000304 GenerateError("Reference to an undefined type: #" + utostr(D.Num));
Reid Spencer5b7e7532006-09-28 19:28:24 +0000305 return 0;
306 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000307 }
308
Reid Spencer861d9d62006-11-28 07:29:44 +0000309 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Chris Lattner58af2a12006-02-15 07:22:58 +0000310 if (I != CurModule.LateResolveTypes.end())
Reid Spencer861d9d62006-11-28 07:29:44 +0000311 return I->second;
Chris Lattner58af2a12006-02-15 07:22:58 +0000312
Reid Spencer861d9d62006-11-28 07:29:44 +0000313 Type *Typ = OpaqueType::get();
314 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
315 return Typ;
Reid Spencera132e042006-12-03 05:46:11 +0000316 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000317
Reid Spencer93c40032007-03-19 18:40:50 +0000318// getExistingVal - Look up the value specified by the provided type and
Chris Lattner58af2a12006-02-15 07:22:58 +0000319// the provided ValID. If the value exists and has already been defined, return
320// it. Otherwise return null.
321//
Reid Spencer93c40032007-03-19 18:40:50 +0000322static Value *getExistingVal(const Type *Ty, const ValID &D) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000323 if (isa<FunctionType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000324 GenerateError("Functions are not values and "
Chris Lattner58af2a12006-02-15 07:22:58 +0000325 "must be referenced as pointers");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000326 return 0;
327 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000328
329 switch (D.Type) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000330 case ValID::LocalID: { // Is it a numbered definition?
Reid Spencer41dff5e2007-01-26 08:05:27 +0000331 // Check that the number is within bounds.
Reid Spencer93c40032007-03-19 18:40:50 +0000332 if (D.Num >= CurFun.Values.size())
333 return 0;
334 Value *Result = CurFun.Values[D.Num];
335 if (Ty != Result->getType()) {
336 GenerateError("Numbered value (%" + utostr(D.Num) + ") of type '" +
337 Result->getType()->getDescription() + "' does not match "
338 "expected type, '" + Ty->getDescription() + "'");
339 return 0;
340 }
341 return Result;
Reid Spencer41dff5e2007-01-26 08:05:27 +0000342 }
343 case ValID::GlobalID: { // Is it a numbered definition?
Reid Spencer93c40032007-03-19 18:40:50 +0000344 if (D.Num >= CurModule.Values.size())
Reid Spenceref9b9a72007-02-05 20:47:22 +0000345 return 0;
Reid Spencer93c40032007-03-19 18:40:50 +0000346 Value *Result = CurModule.Values[D.Num];
347 if (Ty != Result->getType()) {
348 GenerateError("Numbered value (@" + utostr(D.Num) + ") of type '" +
349 Result->getType()->getDescription() + "' does not match "
350 "expected type, '" + Ty->getDescription() + "'");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000351 return 0;
Reid Spencer93c40032007-03-19 18:40:50 +0000352 }
353 return Result;
Chris Lattner58af2a12006-02-15 07:22:58 +0000354 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000355
356 case ValID::LocalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000357 if (!inFunctionScope())
358 return 0;
359 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000360 Value *N = SymTab.lookup(D.getName());
Reid Spenceref9b9a72007-02-05 20:47:22 +0000361 if (N == 0)
362 return 0;
363 if (N->getType() != Ty)
364 return 0;
Reid Spencer41dff5e2007-01-26 08:05:27 +0000365
366 D.destroy(); // Free old strdup'd memory...
367 return N;
368 }
369 case ValID::GlobalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000370 ValueSymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable();
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000371 Value *N = SymTab.lookup(D.getName());
Reid Spenceref9b9a72007-02-05 20:47:22 +0000372 if (N == 0)
373 return 0;
374 if (N->getType() != Ty)
375 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000376
377 D.destroy(); // Free old strdup'd memory...
378 return N;
379 }
380
381 // Check to make sure that "Ty" is an integral type, and that our
382 // value will fit into the specified type...
383 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattner38905612008-02-19 04:36:25 +0000384 if (!isa<IntegerType>(Ty) ||
385 !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000386 GenerateError("Signed integral constant '" +
Chris Lattner58af2a12006-02-15 07:22:58 +0000387 itostr(D.ConstPool64) + "' is invalid for type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000388 Ty->getDescription() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000389 return 0;
390 }
Reid Spencer49d273e2007-03-19 20:40:51 +0000391 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner58af2a12006-02-15 07:22:58 +0000392
393 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattner38905612008-02-19 04:36:25 +0000394 if (isa<IntegerType>(Ty) &&
395 ConstantInt::isValueValidForType(Ty, D.UConstPool64))
Reid Spencerb83eb642006-10-20 07:07:24 +0000396 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattner38905612008-02-19 04:36:25 +0000397
398 if (!isa<IntegerType>(Ty) ||
399 !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
400 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
401 "' is invalid or out of range for type '" +
402 Ty->getDescription() + "'");
403 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000404 }
Chris Lattner38905612008-02-19 04:36:25 +0000405 // This is really a signed reference. Transmogrify.
406 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner58af2a12006-02-15 07:22:58 +0000407
Chris Lattner1913b942008-07-11 00:30:39 +0000408 case ValID::ConstAPInt: // Is it an unsigned const pool reference?
409 if (!isa<IntegerType>(Ty)) {
410 GenerateError("Integral constant '" + D.getName() +
411 "' is invalid or out of range for type '" +
412 Ty->getDescription() + "'");
413 return 0;
414 }
415
416 {
417 APSInt Tmp = *D.ConstPoolInt;
418 Tmp.extOrTrunc(Ty->getPrimitiveSizeInBits());
419 return ConstantInt::get(Tmp);
420 }
421
Chris Lattner58af2a12006-02-15 07:22:58 +0000422 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattner38905612008-02-19 04:36:25 +0000423 if (!Ty->isFloatingPoint() ||
424 !ConstantFP::isValueValidForType(Ty, *D.ConstPoolFP)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000425 GenerateError("FP constant invalid for type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000426 return 0;
427 }
Chris Lattnerd8eb63f2008-04-20 00:41:19 +0000428 // Lexer has no type info, so builds all float and double FP constants
Dale Johannesenc72cd7e2007-09-11 18:33:39 +0000429 // as double. Fix this here. Long double does not need this.
430 if (&D.ConstPoolFP->getSemantics() == &APFloat::IEEEdouble &&
431 Ty==Type::FloatTy)
Dale Johannesen43421b32007-09-06 18:13:44 +0000432 D.ConstPoolFP->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
Chris Lattnerd8eb63f2008-04-20 00:41:19 +0000433 return ConstantFP::get(*D.ConstPoolFP);
Chris Lattner58af2a12006-02-15 07:22:58 +0000434
435 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000436 if (!isa<PointerType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000437 GenerateError("Cannot create a a non pointer null");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000438 return 0;
439 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000440 return ConstantPointerNull::get(cast<PointerType>(Ty));
441
442 case ValID::ConstUndefVal: // Is it an undef value?
443 return UndefValue::get(Ty);
444
445 case ValID::ConstZeroVal: // Is it a zero value?
446 return Constant::getNullValue(Ty);
447
448 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000449 if (D.ConstantValue->getType() != Ty) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000450 GenerateError("Constant expression type different from required type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000451 return 0;
452 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000453 return D.ConstantValue;
454
455 case ValID::InlineAsmVal: { // Inline asm expression
456 const PointerType *PTy = dyn_cast<PointerType>(Ty);
457 const FunctionType *FTy =
458 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000459 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000460 GenerateError("Invalid type for asm constraint string");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000461 return 0;
462 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000463 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
464 D.IAD->HasSideEffects);
465 D.destroy(); // Free InlineAsmDescriptor.
466 return IA;
467 }
468 default:
Reid Spencera9720f52007-02-05 17:04:00 +0000469 assert(0 && "Unhandled case!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000470 return 0;
471 } // End of switch
472
Reid Spencera9720f52007-02-05 17:04:00 +0000473 assert(0 && "Unhandled case!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000474 return 0;
475}
476
Reid Spencer93c40032007-03-19 18:40:50 +0000477// getVal - This function is identical to getExistingVal, except that if a
Chris Lattner58af2a12006-02-15 07:22:58 +0000478// value is not already defined, it "improvises" by creating a placeholder var
479// that looks and acts just like the requested variable. When the value is
480// defined later, all uses of the placeholder variable are replaced with the
481// real thing.
482//
483static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000484 if (Ty == Type::LabelTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000485 GenerateError("Cannot use a basic block here");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000486 return 0;
487 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000488
489 // See if the value has already been defined.
Reid Spencer93c40032007-03-19 18:40:50 +0000490 Value *V = getExistingVal(Ty, ID);
Chris Lattner58af2a12006-02-15 07:22:58 +0000491 if (V) return V;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000492 if (TriggerError) return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000493
Reid Spencer5b7e7532006-09-28 19:28:24 +0000494 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Dan Gohmane4977cf2008-05-23 01:55:30 +0000495 GenerateError("Invalid use of a non-first-class type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000496 return 0;
497 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000498
499 // If we reached here, we referenced either a symbol that we don't know about
500 // or an id number that hasn't been read yet. We may be referencing something
501 // forward, so just create an entry to be resolved later and get to it...
502 //
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000503 switch (ID.Type) {
504 case ValID::GlobalName:
Reid Spencer9c9b63a2007-04-28 16:07:31 +0000505 case ValID::GlobalID: {
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000506 const PointerType *PTy = dyn_cast<PointerType>(Ty);
507 if (!PTy) {
508 GenerateError("Invalid type for reference to global" );
509 return 0;
510 }
511 const Type* ElTy = PTy->getElementType();
512 if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy))
Gabor Greife64d2482008-04-06 23:07:54 +0000513 V = Function::Create(FTy, GlobalValue::ExternalLinkage);
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000514 else
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000515 V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage, 0, "",
516 (Module*)0, false, PTy->getAddressSpace());
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000517 break;
Reid Spencer9c9b63a2007-04-28 16:07:31 +0000518 }
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000519 default:
520 V = new Argument(Ty);
521 }
522
Chris Lattner58af2a12006-02-15 07:22:58 +0000523 // Remember where this forward reference came from. FIXME, shouldn't we try
524 // to recycle these things??
525 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Duncan Sandsdc024672007-11-27 13:23:08 +0000526 LLLgetLineNo())));
Chris Lattner58af2a12006-02-15 07:22:58 +0000527
528 if (inFunctionScope())
529 InsertValue(V, CurFun.LateResolveValues);
530 else
531 InsertValue(V, CurModule.LateResolveValues);
532 return V;
533}
534
Reid Spencer93c40032007-03-19 18:40:50 +0000535/// defineBBVal - This is a definition of a new basic block with the specified
536/// identifier which must be the same as CurFun.NextValNum, if its numeric.
Nick Lewycky280a6e62008-04-25 16:53:59 +0000537static BasicBlock *defineBBVal(const ValID &ID) {
Reid Spencera9720f52007-02-05 17:04:00 +0000538 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000539
Chris Lattner58af2a12006-02-15 07:22:58 +0000540 BasicBlock *BB = 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000541
Reid Spencer93c40032007-03-19 18:40:50 +0000542 // First, see if this was forward referenced
Chris Lattner58af2a12006-02-15 07:22:58 +0000543
Reid Spencer93c40032007-03-19 18:40:50 +0000544 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
545 if (BBI != CurFun.BBForwardRefs.end()) {
546 BB = BBI->second;
Chris Lattner58af2a12006-02-15 07:22:58 +0000547 // The forward declaration could have been inserted anywhere in the
548 // function: insert it into the correct place now.
549 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
550 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Reid Spencer93c40032007-03-19 18:40:50 +0000551
Reid Spencer66728ef2007-03-20 01:13:36 +0000552 // We're about to erase the entry, save the key so we can clean it up.
553 ValID Tmp = BBI->first;
554
Reid Spencer93c40032007-03-19 18:40:50 +0000555 // Erase the forward ref from the map as its no longer "forward"
556 CurFun.BBForwardRefs.erase(ID);
557
Reid Spencer66728ef2007-03-20 01:13:36 +0000558 // The key has been removed from the map but so we don't want to leave
559 // strdup'd memory around so destroy it too.
560 Tmp.destroy();
561
Reid Spencer93c40032007-03-19 18:40:50 +0000562 // If its a numbered definition, bump the number and set the BB value.
563 if (ID.Type == ValID::LocalID) {
564 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
565 InsertValue(BB);
566 }
Devang Patel67909432008-03-03 18:58:47 +0000567 } else {
568 // We haven't seen this BB before and its first mention is a definition.
569 // Just create it and return it.
570 std::string Name (ID.Type == ValID::LocalName ? ID.getName() : "");
Gabor Greife64d2482008-04-06 23:07:54 +0000571 BB = BasicBlock::Create(Name, CurFun.CurrentFunction);
Devang Patel67909432008-03-03 18:58:47 +0000572 if (ID.Type == ValID::LocalID) {
573 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
574 InsertValue(BB);
575 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000576 }
Reid Spencer93c40032007-03-19 18:40:50 +0000577
Devang Patel67909432008-03-03 18:58:47 +0000578 ID.destroy();
Reid Spencer93c40032007-03-19 18:40:50 +0000579 return BB;
580}
581
582/// getBBVal - get an existing BB value or create a forward reference for it.
583///
584static BasicBlock *getBBVal(const ValID &ID) {
585 assert(inFunctionScope() && "Can't get basic block at global scope!");
586
587 BasicBlock *BB = 0;
588
589 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
590 if (BBI != CurFun.BBForwardRefs.end()) {
591 BB = BBI->second;
592 } if (ID.Type == ValID::LocalName) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000593 std::string Name = ID.getName();
Reid Spencer93c40032007-03-19 18:40:50 +0000594 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000595 if (N) {
Reid Spencer93c40032007-03-19 18:40:50 +0000596 if (N->getType()->getTypeID() == Type::LabelTyID)
597 BB = cast<BasicBlock>(N);
598 else
599 GenerateError("Reference to label '" + Name + "' is actually of type '"+
600 N->getType()->getDescription() + "'");
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000601 }
Reid Spencer93c40032007-03-19 18:40:50 +0000602 } else if (ID.Type == ValID::LocalID) {
603 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
604 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
605 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
606 else
607 GenerateError("Reference to label '%" + utostr(ID.Num) +
608 "' is actually of type '"+
609 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
610 }
611 } else {
612 GenerateError("Illegal label reference " + ID.getName());
613 return 0;
614 }
615
616 // If its already been defined, return it now.
617 if (BB) {
618 ID.destroy(); // Free strdup'd memory.
619 return BB;
620 }
621
622 // Otherwise, this block has not been seen before, create it.
623 std::string Name;
624 if (ID.Type == ValID::LocalName)
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000625 Name = ID.getName();
Gabor Greife64d2482008-04-06 23:07:54 +0000626 BB = BasicBlock::Create(Name, CurFun.CurrentFunction);
Reid Spencer93c40032007-03-19 18:40:50 +0000627
628 // Insert it in the forward refs map.
629 CurFun.BBForwardRefs[ID] = BB;
630
Chris Lattner58af2a12006-02-15 07:22:58 +0000631 return BB;
632}
633
634
635//===----------------------------------------------------------------------===//
636// Code to handle forward references in instructions
637//===----------------------------------------------------------------------===//
638//
639// This code handles the late binding needed with statements that reference
640// values not defined yet... for example, a forward branch, or the PHI node for
641// a loop body.
642//
643// This keeps a table (CurFun.LateResolveValues) of all such forward references
644// and back patchs after we are done.
645//
646
647// ResolveDefinitions - If we could not resolve some defs at parsing
648// time (forward branches, phi functions for loops, etc...) resolve the
649// defs now...
650//
651static void
Reid Spencer93c40032007-03-19 18:40:50 +0000652ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000653 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencer93c40032007-03-19 18:40:50 +0000654 while (!LateResolvers.empty()) {
655 Value *V = LateResolvers.back();
656 LateResolvers.pop_back();
Chris Lattner58af2a12006-02-15 07:22:58 +0000657
Reid Spencer93c40032007-03-19 18:40:50 +0000658 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
659 CurModule.PlaceHolderInfo.find(V);
660 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000661
Reid Spencer93c40032007-03-19 18:40:50 +0000662 ValID &DID = PHI->second.first;
Chris Lattner58af2a12006-02-15 07:22:58 +0000663
Reid Spencer93c40032007-03-19 18:40:50 +0000664 Value *TheRealValue = getExistingVal(V->getType(), DID);
665 if (TriggerError)
666 return;
667 if (TheRealValue) {
668 V->replaceAllUsesWith(TheRealValue);
669 delete V;
670 CurModule.PlaceHolderInfo.erase(PHI);
671 } else if (FutureLateResolvers) {
672 // Functions have their unresolved items forwarded to the module late
673 // resolver table
674 InsertValue(V, *FutureLateResolvers);
675 } else {
676 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
677 GenerateError("Reference to an invalid definition: '" +DID.getName()+
678 "' of type '" + V->getType()->getDescription() + "'",
679 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000680 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000681 } else {
Reid Spencer93c40032007-03-19 18:40:50 +0000682 GenerateError("Reference to an invalid definition: #" +
683 itostr(DID.Num) + " of type '" +
684 V->getType()->getDescription() + "'",
685 PHI->second.second);
686 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000687 }
688 }
689 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000690 LateResolvers.clear();
691}
692
693// ResolveTypeTo - A brand new type was just declared. This means that (if
694// name is not null) things referencing Name can be resolved. Otherwise, things
695// refering to the number can be resolved. Do this now.
696//
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000697static void ResolveTypeTo(std::string *Name, const Type *ToTy) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000698 ValID D;
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000699 if (Name)
700 D = ValID::createLocalName(*Name);
701 else
702 D = ValID::createLocalID(CurModule.Types.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000703
Reid Spencer861d9d62006-11-28 07:29:44 +0000704 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattner58af2a12006-02-15 07:22:58 +0000705 CurModule.LateResolveTypes.find(D);
706 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencer861d9d62006-11-28 07:29:44 +0000707 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner58af2a12006-02-15 07:22:58 +0000708 CurModule.LateResolveTypes.erase(I);
709 }
710}
711
712// setValueName - Set the specified value to the name given. The name may be
713// null potentially, in which case this is a noop. The string passed in is
714// assumed to be a malloc'd string buffer, and is free'd by this function.
715//
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000716static void setValueName(Value *V, std::string *NameStr) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000717 if (!NameStr) return;
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000718 std::string Name(*NameStr); // Copy string
719 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000720
Reid Spencer41dff5e2007-01-26 08:05:27 +0000721 if (V->getType() == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000722 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000723 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000724 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000725
Reid Spencera9720f52007-02-05 17:04:00 +0000726 assert(inFunctionScope() && "Must be in function scope!");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000727 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
728 if (ST.lookup(Name)) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000729 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000730 V->getType()->getDescription() + "'");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000731 return;
732 }
733
734 // Set the name.
735 V->setName(Name);
Chris Lattner58af2a12006-02-15 07:22:58 +0000736}
737
738/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
739/// this is a declaration, otherwise it is a definition.
740static GlobalVariable *
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000741ParseGlobalVariable(std::string *NameStr,
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000742 GlobalValue::LinkageTypes Linkage,
743 GlobalValue::VisibilityTypes Visibility,
Chris Lattner58af2a12006-02-15 07:22:58 +0000744 bool isConstantGlobal, const Type *Ty,
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000745 Constant *Initializer, bool IsThreadLocal,
746 unsigned AddressSpace = 0) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000747 if (isa<FunctionType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000748 GenerateError("Cannot declare global vars of function type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000749 return 0;
750 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000751 if (Ty == Type::LabelTy) {
752 GenerateError("Cannot declare global vars of label type");
753 return 0;
754 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000755
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000756 const PointerType *PTy = PointerType::get(Ty, AddressSpace);
Chris Lattner58af2a12006-02-15 07:22:58 +0000757
758 std::string Name;
759 if (NameStr) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000760 Name = *NameStr; // Copy string
761 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000762 }
763
764 // See if this global value was forward referenced. If so, recycle the
765 // object.
766 ValID ID;
767 if (!Name.empty()) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000768 ID = ValID::createGlobalName(Name);
Chris Lattner58af2a12006-02-15 07:22:58 +0000769 } else {
Reid Spencer93c40032007-03-19 18:40:50 +0000770 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000771 }
772
773 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
774 // Move the global to the end of the list, from whereever it was
775 // previously inserted.
776 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
777 CurModule.CurrentModule->getGlobalList().remove(GV);
778 CurModule.CurrentModule->getGlobalList().push_back(GV);
779 GV->setInitializer(Initializer);
780 GV->setLinkage(Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000781 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000782 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000783 GV->setThreadLocal(IsThreadLocal);
Chris Lattner58af2a12006-02-15 07:22:58 +0000784 InsertValue(GV, CurModule.Values);
785 return GV;
786 }
787
Reid Spenceref9b9a72007-02-05 20:47:22 +0000788 // If this global has a name
Chris Lattner58af2a12006-02-15 07:22:58 +0000789 if (!Name.empty()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000790 // if the global we're parsing has an initializer (is a definition) and
791 // has external linkage.
792 if (Initializer && Linkage != GlobalValue::InternalLinkage)
793 // If there is already a global with external linkage with this name
794 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
795 // If we allow this GVar to get created, it will be renamed in the
796 // symbol table because it conflicts with an existing GVar. We can't
797 // allow redefinition of GVars whose linking indicates that their name
798 // must stay the same. Issue the error.
799 GenerateError("Redefinition of global variable named '" + Name +
800 "' of type '" + Ty->getDescription() + "'");
801 return 0;
802 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000803 }
804
805 // Otherwise there is no existing GV to use, create one now.
806 GlobalVariable *GV =
807 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000808 CurModule.CurrentModule, IsThreadLocal, AddressSpace);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000809 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000810 InsertValue(GV, CurModule.Values);
811 return GV;
812}
813
814// setTypeName - Set the specified type to the name given. The name may be
815// null potentially, in which case this is a noop. The string passed in is
816// assumed to be a malloc'd string buffer, and is freed by this function.
817//
818// This function returns true if the type has already been defined, but is
819// allowed to be redefined in the specified context. If the name is a new name
820// for the type plane, it is inserted and false is returned.
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000821static bool setTypeName(const Type *T, std::string *NameStr) {
Reid Spencera9720f52007-02-05 17:04:00 +0000822 assert(!inFunctionScope() && "Can't give types function-local names!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000823 if (NameStr == 0) return false;
824
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000825 std::string Name(*NameStr); // Copy string
826 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000827
828 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000829 if (T == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000830 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000831 return false;
832 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000833
834 // Set the type name, checking for conflicts as we do so.
835 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
836
837 if (AlreadyExists) { // Inserting a name that is already defined???
838 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencera9720f52007-02-05 17:04:00 +0000839 assert(Existing && "Conflict but no matching type?!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000840
841 // There is only one case where this is allowed: when we are refining an
842 // opaque type. In this case, Existing will be an opaque type.
843 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
844 // We ARE replacing an opaque type!
845 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
846 return true;
847 }
848
849 // Otherwise, this is an attempt to redefine a type. That's okay if
850 // the redefinition is identical to the original. This will be so if
851 // Existing and T point to the same Type object. In this one case we
852 // allow the equivalent redefinition.
853 if (Existing == T) return true; // Yes, it's equal.
854
855 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer63c34452007-01-05 21:51:07 +0000856 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000857 T->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +0000858 }
859
860 return false;
861}
862
863//===----------------------------------------------------------------------===//
864// Code for handling upreferences in type names...
865//
866
867// TypeContains - Returns true if Ty directly contains E in it.
868//
869static bool TypeContains(const Type *Ty, const Type *E) {
870 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
871 E) != Ty->subtype_end();
872}
873
874namespace {
875 struct UpRefRecord {
876 // NestingLevel - The number of nesting levels that need to be popped before
877 // this type is resolved.
878 unsigned NestingLevel;
879
880 // LastContainedTy - This is the type at the current binding level for the
881 // type. Every time we reduce the nesting level, this gets updated.
882 const Type *LastContainedTy;
883
884 // UpRefTy - This is the actual opaque type that the upreference is
885 // represented with.
886 OpaqueType *UpRefTy;
887
888 UpRefRecord(unsigned NL, OpaqueType *URTy)
889 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
890 };
891}
892
893// UpRefs - A list of the outstanding upreferences that need to be resolved.
894static std::vector<UpRefRecord> UpRefs;
895
896/// HandleUpRefs - Every time we finish a new layer of types, this function is
897/// called. It loops through the UpRefs vector, which is a list of the
898/// currently active types. For each type, if the up reference is contained in
899/// the newly completed type, we decrement the level count. When the level
900/// count reaches zero, the upreferenced type is the type that is passed in:
901/// thus we can complete the cycle.
902///
903static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner224f84f2006-08-18 17:34:45 +0000904 // If Ty isn't abstract, or if there are no up-references in it, then there is
905 // nothing to resolve here.
906 if (!ty->isAbstract() || UpRefs.empty()) return ty;
907
Chris Lattner58af2a12006-02-15 07:22:58 +0000908 PATypeHolder Ty(ty);
909 UR_OUT("Type '" << Ty->getDescription() <<
910 "' newly formed. Resolving upreferences.\n" <<
911 UpRefs.size() << " upreferences active!\n");
912
913 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
914 // to zero), we resolve them all together before we resolve them to Ty. At
915 // the end of the loop, if there is anything to resolve to Ty, it will be in
916 // this variable.
917 OpaqueType *TypeToResolve = 0;
918
919 for (unsigned i = 0; i != UpRefs.size(); ++i) {
920 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
921 << UpRefs[i].second->getDescription() << ") = "
922 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
923 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
924 // Decrement level of upreference
925 unsigned Level = --UpRefs[i].NestingLevel;
926 UpRefs[i].LastContainedTy = Ty;
927 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
928 if (Level == 0) { // Upreference should be resolved!
929 if (!TypeToResolve) {
930 TypeToResolve = UpRefs[i].UpRefTy;
931 } else {
932 UR_OUT(" * Resolving upreference for "
933 << UpRefs[i].second->getDescription() << "\n";
934 std::string OldName = UpRefs[i].UpRefTy->getDescription());
935 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
936 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
937 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
938 }
939 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
940 --i; // Do not skip the next element...
941 }
942 }
943 }
944
945 if (TypeToResolve) {
946 UR_OUT(" * Resolving upreference for "
947 << UpRefs[i].second->getDescription() << "\n";
948 std::string OldName = TypeToResolve->getDescription());
949 TypeToResolve->refineAbstractTypeTo(Ty);
950 }
951
952 return Ty;
953}
954
Chris Lattner58af2a12006-02-15 07:22:58 +0000955//===----------------------------------------------------------------------===//
956// RunVMAsmParser - Define an interface to this parser
957//===----------------------------------------------------------------------===//
958//
Reid Spencer14310612006-12-31 05:40:51 +0000959static Module* RunParser(Module * M);
960
Duncan Sandsdc024672007-11-27 13:23:08 +0000961Module *llvm::RunVMAsmParser(llvm::MemoryBuffer *MB) {
962 InitLLLexer(MB);
963 Module *M = RunParser(new Module(LLLgetFilename()));
964 FreeLexer();
965 return M;
Chris Lattner58af2a12006-02-15 07:22:58 +0000966}
967
968%}
969
970%union {
971 llvm::Module *ModuleVal;
972 llvm::Function *FunctionVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000973 llvm::BasicBlock *BasicBlockVal;
974 llvm::TerminatorInst *TermInstVal;
975 llvm::Instruction *InstVal;
Reid Spencera132e042006-12-03 05:46:11 +0000976 llvm::Constant *ConstVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000977
Reid Spencera132e042006-12-03 05:46:11 +0000978 const llvm::Type *PrimType;
Reid Spencer14310612006-12-31 05:40:51 +0000979 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencera132e042006-12-03 05:46:11 +0000980 llvm::PATypeHolder *TypeVal;
981 llvm::Value *ValueVal;
Reid Spencera132e042006-12-03 05:46:11 +0000982 std::vector<llvm::Value*> *ValueList;
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000983 std::vector<unsigned> *ConstantList;
Reid Spencer14310612006-12-31 05:40:51 +0000984 llvm::ArgListType *ArgList;
985 llvm::TypeWithAttrs TypeWithAttrs;
986 llvm::TypeWithAttrsList *TypeWithAttrsList;
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000987 llvm::ParamList *ParamList;
Reid Spencer14310612006-12-31 05:40:51 +0000988
Chris Lattner58af2a12006-02-15 07:22:58 +0000989 // Represent the RHS of PHI node
Reid Spencera132e042006-12-03 05:46:11 +0000990 std::list<std::pair<llvm::Value*,
991 llvm::BasicBlock*> > *PHIList;
Chris Lattner58af2a12006-02-15 07:22:58 +0000992 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencera132e042006-12-03 05:46:11 +0000993 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner58af2a12006-02-15 07:22:58 +0000994
995 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000996 llvm::GlobalValue::VisibilityTypes Visibility;
Dale Johannesen222ebf72008-02-19 21:40:51 +0000997 llvm::ParameterAttributes ParamAttrs;
Reid Spencer38c91a92007-02-28 02:24:54 +0000998 llvm::APInt *APIntVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000999 int64_t SInt64Val;
1000 uint64_t UInt64Val;
1001 int SIntVal;
1002 unsigned UIntVal;
Dale Johannesen43421b32007-09-06 18:13:44 +00001003 llvm::APFloat *FPVal;
Chris Lattner58af2a12006-02-15 07:22:58 +00001004 bool BoolVal;
1005
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001006 std::string *StrVal; // This memory must be deleted
1007 llvm::ValID ValIDVal;
Chris Lattner58af2a12006-02-15 07:22:58 +00001008
Reid Spencera132e042006-12-03 05:46:11 +00001009 llvm::Instruction::BinaryOps BinaryOpVal;
1010 llvm::Instruction::TermOps TermOpVal;
1011 llvm::Instruction::MemoryOps MemOpVal;
1012 llvm::Instruction::CastOps CastOpVal;
1013 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencera132e042006-12-03 05:46:11 +00001014 llvm::ICmpInst::Predicate IPredicate;
1015 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner58af2a12006-02-15 07:22:58 +00001016}
1017
Reid Spencer14310612006-12-31 05:40:51 +00001018%type <ModuleVal> Module
Chris Lattner58af2a12006-02-15 07:22:58 +00001019%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
1020%type <BasicBlockVal> BasicBlock InstructionList
1021%type <TermInstVal> BBTerminatorInst
1022%type <InstVal> Inst InstVal MemoryInst
Anton Korobeynikov38e09802007-04-28 13:48:45 +00001023%type <ConstVal> ConstVal ConstExpr AliaseeRef
Chris Lattner58af2a12006-02-15 07:22:58 +00001024%type <ConstVector> ConstVector
1025%type <ArgList> ArgList ArgListH
Chris Lattner58af2a12006-02-15 07:22:58 +00001026%type <PHIList> PHIList
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001027%type <ParamList> ParamList // For call param lists & GEP indices
Reid Spencer14310612006-12-31 05:40:51 +00001028%type <ValueList> IndexList // For GEP indices
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001029%type <ConstantList> ConstantIndexList // For insertvalue/extractvalue indices
Reid Spencer14310612006-12-31 05:40:51 +00001030%type <TypeList> TypeListI
1031%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer218ded22007-01-05 17:07:23 +00001032%type <TypeWithAttrs> ArgType
Chris Lattner58af2a12006-02-15 07:22:58 +00001033%type <JumpTable> JumpTable
1034%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001035%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattner58af2a12006-02-15 07:22:58 +00001036%type <BoolVal> OptVolatile // 'volatile' or not
1037%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1038%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencer14310612006-12-31 05:40:51 +00001039%type <Linkage> GVInternalLinkage GVExternalLinkage
1040%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001041%type <Linkage> AliasLinkage
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001042%type <Visibility> GVVisibilityStyle
Chris Lattner58af2a12006-02-15 07:22:58 +00001043
1044// ValueRef - Unresolved reference to a definition or BB
1045%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1046%type <ValueVal> ResolvedVal // <type> <valref> pair
Devang Patel7990dc72008-02-20 22:40:23 +00001047%type <ValueList> ReturnedVal
Chris Lattner58af2a12006-02-15 07:22:58 +00001048// Tokens and types for handling constant integer values
1049//
1050// ESINT64VAL - A negative number within long long range
1051%token <SInt64Val> ESINT64VAL
1052
1053// EUINT64VAL - A positive number within uns. long long range
1054%token <UInt64Val> EUINT64VAL
Chris Lattner58af2a12006-02-15 07:22:58 +00001055
Reid Spencer38c91a92007-02-28 02:24:54 +00001056// ESAPINTVAL - A negative number with arbitrary precision
1057%token <APIntVal> ESAPINTVAL
1058
1059// EUAPINTVAL - A positive number with arbitrary precision
1060%token <APIntVal> EUAPINTVAL
1061
Reid Spencer41dff5e2007-01-26 08:05:27 +00001062%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattner58af2a12006-02-15 07:22:58 +00001063%token <FPVal> FPVAL // Float or Double constant
1064
1065// Built in types...
Reid Spencer218ded22007-01-05 17:07:23 +00001066%type <TypeVal> Types ResultTypes
Reid Spencer14310612006-12-31 05:40:51 +00001067%type <PrimType> IntType FPType PrimType // Classifications
Reid Spencer6f407902007-01-13 05:00:46 +00001068%token <PrimType> VOID INTTYPE
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001069%token <PrimType> FLOAT DOUBLE X86_FP80 FP128 PPC_FP128 LABEL
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001070%token TYPE
Chris Lattner58af2a12006-02-15 07:22:58 +00001071
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001072
Reid Spencered951ea2007-05-19 07:22:10 +00001073%token<StrVal> LOCALVAR GLOBALVAR LABELSTR
1074%token<StrVal> STRINGCONSTANT ATSTRINGCONSTANT PCTSTRINGCONSTANT
Reid Spencer41dff5e2007-01-26 08:05:27 +00001075%type <StrVal> LocalName OptLocalName OptLocalAssign
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001076%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001077%type <StrVal> OptSection SectionString OptGC
Chris Lattner58af2a12006-02-15 07:22:58 +00001078
Christopher Lambbf3348d2007-12-12 08:45:45 +00001079%type <UIntVal> OptAlign OptCAlign OptAddrSpace
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001080
Reid Spencer3d6b71e2007-04-09 01:56:05 +00001081%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001082%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
Reid Spencer14310612006-12-31 05:40:51 +00001083%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Dale Johannesenc7071cc2008-05-14 20:13:36 +00001084%token DLLIMPORT DLLEXPORT EXTERN_WEAK COMMON
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001085%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN ADDRSPACE
Chris Lattner58af2a12006-02-15 07:22:58 +00001086%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001087%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Dale Johannesen20ab78b2008-08-13 18:41:46 +00001088%token X86_SSECALLCC_TOK
Nick Lewycky280a6e62008-04-25 16:53:59 +00001089%token DATALAYOUT
Chris Lattner15bd0952008-08-29 17:20:18 +00001090%type <UIntVal> OptCallingConv LocalNumber
Reid Spencer218ded22007-01-05 17:07:23 +00001091%type <ParamAttrs> OptParamAttrs ParamAttr
1092%type <ParamAttrs> OptFuncAttrs FuncAttr
Chris Lattner58af2a12006-02-15 07:22:58 +00001093
1094// Basic Block Terminating Operators
1095%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
1096
1097// Binary Operators
Reid Spencere4d87aa2006-12-23 06:05:41 +00001098%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencer3ed469c2006-11-02 20:25:50 +00001099%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer832254e2007-02-02 02:16:23 +00001100%token <BinaryOpVal> SHL LSHR ASHR
1101
Nate Begemanac80ade2008-05-12 19:01:56 +00001102%token <OtherOpVal> ICMP FCMP VICMP VFCMP
Reid Spencera132e042006-12-03 05:46:11 +00001103%type <IPredicate> IPredicates
Reid Spencera132e042006-12-03 05:46:11 +00001104%type <FPredicate> FPredicates
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001105%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1106%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner58af2a12006-02-15 07:22:58 +00001107
1108// Memory Instructions
1109%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1110
Reid Spencer3da59db2006-11-27 01:05:10 +00001111// Cast Operators
1112%type <CastOpVal> CastOps
1113%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1114%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1115
Chris Lattner58af2a12006-02-15 07:22:58 +00001116// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001117%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattnerd5efe842006-04-08 01:18:56 +00001118%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Devang Patel5a970972008-02-19 22:27:01 +00001119%token <OtherOpVal> GETRESULT
Dan Gohmane4977cf2008-05-23 01:55:30 +00001120%token <OtherOpVal> EXTRACTVALUE INSERTVALUE
Chris Lattner58af2a12006-02-15 07:22:58 +00001121
Reid Spencer218ded22007-01-05 17:07:23 +00001122// Function Attributes
Reid Spencerb8f85052007-07-31 03:50:36 +00001123%token SIGNEXT ZEROEXT NORETURN INREG SRET NOUNWIND NOALIAS BYVAL NEST
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001124%token READNONE READONLY GC
Chris Lattner58af2a12006-02-15 07:22:58 +00001125
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001126// Visibility Styles
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001127%token DEFAULT HIDDEN PROTECTED
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001128
Chris Lattner58af2a12006-02-15 07:22:58 +00001129%start Module
1130%%
1131
Chris Lattner58af2a12006-02-15 07:22:58 +00001132
Chris Lattner58af2a12006-02-15 07:22:58 +00001133// Operations that are notably excluded from this list include:
1134// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1135//
Reid Spencer3ed469c2006-11-02 20:25:50 +00001136ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer832254e2007-02-02 02:16:23 +00001137LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Reid Spencer3da59db2006-11-27 01:05:10 +00001138CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1139 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer832254e2007-02-02 02:16:23 +00001140
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001141IPredicates
Reid Spencer4012e832006-12-04 05:24:24 +00001142 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001143 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1144 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1145 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1146 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1147 ;
1148
1149FPredicates
1150 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1151 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1152 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1153 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1154 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1155 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1156 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1157 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1158 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1159 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001160
1161// These are some types that allow classification if we only want a particular
1162// thing... for example, only a signed, unsigned, or integral type.
Reid Spencera54b7cb2007-01-12 07:05:14 +00001163IntType : INTTYPE;
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001164FPType : FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80;
Chris Lattner58af2a12006-02-15 07:22:58 +00001165
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001166LocalName : LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001167OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1168
Christopher Lambbf3348d2007-12-12 08:45:45 +00001169OptAddrSpace : ADDRSPACE '(' EUINT64VAL ')' { $$=$3; }
1170 | /*empty*/ { $$=0; };
1171
Reid Spencer41dff5e2007-01-26 08:05:27 +00001172/// OptLocalAssign - Value producing statements have an optional assignment
1173/// component.
1174OptLocalAssign : LocalName '=' {
1175 $$ = $1;
1176 CHECK_FOR_ERROR
1177 }
1178 | /*empty*/ {
1179 $$ = 0;
1180 CHECK_FOR_ERROR
1181 };
1182
Chris Lattner15bd0952008-08-29 17:20:18 +00001183LocalNumber : LOCALVAL_ID '=' {
1184 $$ = $1;
1185 CHECK_FOR_ERROR
1186};
1187
1188
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001189GlobalName : GLOBALVAR | ATSTRINGCONSTANT ;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001190
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001191OptGlobalAssign : GlobalAssign
Chris Lattner58af2a12006-02-15 07:22:58 +00001192 | /*empty*/ {
1193 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001194 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001195 };
1196
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001197GlobalAssign : GlobalName '=' {
1198 $$ = $1;
1199 CHECK_FOR_ERROR
Chris Lattner6cdc6822007-04-26 05:31:05 +00001200 };
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001201
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001202GVInternalLinkage
1203 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1204 | WEAK { $$ = GlobalValue::WeakLinkage; }
1205 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1206 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1207 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Dale Johannesenc7071cc2008-05-14 20:13:36 +00001208 | COMMON { $$ = GlobalValue::CommonLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001209 ;
1210
1211GVExternalLinkage
1212 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1213 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1214 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1215 ;
1216
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001217GVVisibilityStyle
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001218 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1219 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1220 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1221 | PROTECTED { $$ = GlobalValue::ProtectedVisibility; }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001222 ;
1223
Reid Spencer14310612006-12-31 05:40:51 +00001224FunctionDeclareLinkage
1225 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1226 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1227 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001228 ;
1229
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001230FunctionDefineLinkage
Reid Spencer14310612006-12-31 05:40:51 +00001231 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1232 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001233 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1234 | WEAK { $$ = GlobalValue::WeakLinkage; }
1235 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001236 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001237
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001238AliasLinkage
1239 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1240 | WEAK { $$ = GlobalValue::WeakLinkage; }
1241 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1242 ;
1243
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001244OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1245 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001246 FASTCC_TOK { $$ = CallingConv::Fast; } |
1247 COLDCC_TOK { $$ = CallingConv::Cold; } |
1248 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1249 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
Dale Johannesen20ab78b2008-08-13 18:41:46 +00001250 X86_SSECALLCC_TOK { $$ = CallingConv::X86_SSECall; } |
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001251 CC_TOK EUINT64VAL {
Chris Lattner58af2a12006-02-15 07:22:58 +00001252 if ((unsigned)$2 != $2)
Reid Spencerb5334b02007-02-05 10:18:06 +00001253 GEN_ERROR("Calling conv too large");
Chris Lattner58af2a12006-02-15 07:22:58 +00001254 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001255 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001256 };
1257
Reid Spencerb8f85052007-07-31 03:50:36 +00001258ParamAttr : ZEROEXT { $$ = ParamAttr::ZExt; }
1259 | ZEXT { $$ = ParamAttr::ZExt; }
1260 | SIGNEXT { $$ = ParamAttr::SExt; }
Chris Lattnerce5f24e2007-07-05 17:26:49 +00001261 | SEXT { $$ = ParamAttr::SExt; }
1262 | INREG { $$ = ParamAttr::InReg; }
1263 | SRET { $$ = ParamAttr::StructRet; }
1264 | NOALIAS { $$ = ParamAttr::NoAlias; }
Reid Spencerb8f85052007-07-31 03:50:36 +00001265 | BYVAL { $$ = ParamAttr::ByVal; }
1266 | NEST { $$ = ParamAttr::Nest; }
Dale Johannesendc6c0f12008-02-22 17:50:51 +00001267 | ALIGN EUINT64VAL { $$ =
1268 ParamAttr::constructAlignmentFromInt($2); }
Reid Spencer14310612006-12-31 05:40:51 +00001269 ;
1270
Reid Spencer18da0722007-04-11 02:44:20 +00001271OptParamAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer218ded22007-01-05 17:07:23 +00001272 | OptParamAttrs ParamAttr {
Reid Spencer7b5d4662007-04-09 06:16:21 +00001273 $$ = $1 | $2;
Reid Spencer14310612006-12-31 05:40:51 +00001274 }
1275 ;
1276
Reid Spencer18da0722007-04-11 02:44:20 +00001277FuncAttr : NORETURN { $$ = ParamAttr::NoReturn; }
1278 | NOUNWIND { $$ = ParamAttr::NoUnwind; }
Reid Spencerb8f85052007-07-31 03:50:36 +00001279 | ZEROEXT { $$ = ParamAttr::ZExt; }
1280 | SIGNEXT { $$ = ParamAttr::SExt; }
Duncan Sandsdc024672007-11-27 13:23:08 +00001281 | READNONE { $$ = ParamAttr::ReadNone; }
1282 | READONLY { $$ = ParamAttr::ReadOnly; }
Reid Spencer218ded22007-01-05 17:07:23 +00001283 ;
1284
Reid Spencer18da0722007-04-11 02:44:20 +00001285OptFuncAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer218ded22007-01-05 17:07:23 +00001286 | OptFuncAttrs FuncAttr {
Reid Spencer7b5d4662007-04-09 06:16:21 +00001287 $$ = $1 | $2;
Reid Spencer218ded22007-01-05 17:07:23 +00001288 }
Reid Spencer14310612006-12-31 05:40:51 +00001289 ;
1290
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001291OptGC : /* empty */ { $$ = 0; }
1292 | GC STRINGCONSTANT {
1293 $$ = $2;
1294 }
1295 ;
1296
Chris Lattner58af2a12006-02-15 07:22:58 +00001297// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1298// a comma before it.
1299OptAlign : /*empty*/ { $$ = 0; } |
1300 ALIGN EUINT64VAL {
1301 $$ = $2;
1302 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001303 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001304 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001305};
1306OptCAlign : /*empty*/ { $$ = 0; } |
1307 ',' ALIGN EUINT64VAL {
1308 $$ = $3;
1309 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001310 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001311 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001312};
1313
1314
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001315
Chris Lattner58af2a12006-02-15 07:22:58 +00001316SectionString : SECTION STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001317 for (unsigned i = 0, e = $2->length(); i != e; ++i)
1318 if ((*$2)[i] == '"' || (*$2)[i] == '\\')
Reid Spencerb5334b02007-02-05 10:18:06 +00001319 GEN_ERROR("Invalid character in section name");
Chris Lattner58af2a12006-02-15 07:22:58 +00001320 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001321 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001322};
1323
1324OptSection : /*empty*/ { $$ = 0; } |
1325 SectionString { $$ = $1; };
1326
1327// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1328// is set to be the global we are processing.
1329//
1330GlobalVarAttributes : /* empty */ {} |
1331 ',' GlobalVarAttribute GlobalVarAttributes {};
1332GlobalVarAttribute : SectionString {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001333 CurGV->setSection(*$1);
1334 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001335 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001336 }
1337 | ALIGN EUINT64VAL {
1338 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001339 GEN_ERROR("Alignment must be a power of two");
Chris Lattner58af2a12006-02-15 07:22:58 +00001340 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001341 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001342 };
1343
1344//===----------------------------------------------------------------------===//
1345// Types includes all predefined types... except void, because it can only be
Reid Spencer14310612006-12-31 05:40:51 +00001346// used in specific contexts (function returning void for example).
Chris Lattner58af2a12006-02-15 07:22:58 +00001347
1348// Derived types are added later...
1349//
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001350PrimType : INTTYPE | FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80 | LABEL ;
Reid Spencer14310612006-12-31 05:40:51 +00001351
1352Types
1353 : OPAQUE {
Reid Spencera132e042006-12-03 05:46:11 +00001354 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001355 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001356 }
1357 | PrimType {
Reid Spencera132e042006-12-03 05:46:11 +00001358 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001359 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00001360 }
Christopher Lambbf3348d2007-12-12 08:45:45 +00001361 | Types OptAddrSpace '*' { // Pointer type?
Reid Spencer14310612006-12-31 05:40:51 +00001362 if (*$1 == Type::LabelTy)
1363 GEN_ERROR("Cannot form a pointer to a basic block");
Christopher Lambbf3348d2007-12-12 08:45:45 +00001364 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1, $2)));
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001365 delete $1;
1366 CHECK_FOR_ERROR
1367 }
Reid Spencer14310612006-12-31 05:40:51 +00001368 | SymbolicValueRef { // Named types are also simple types...
1369 const Type* tmp = getTypeVal($1);
1370 CHECK_FOR_ERROR
1371 $$ = new PATypeHolder(tmp);
1372 }
1373 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerb5334b02007-02-05 10:18:06 +00001374 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattner58af2a12006-02-15 07:22:58 +00001375 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1376 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencera132e042006-12-03 05:46:11 +00001377 $$ = new PATypeHolder(OT);
Chris Lattner58af2a12006-02-15 07:22:58 +00001378 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001379 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001380 }
Reid Spencer218ded22007-01-05 17:07:23 +00001381 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001382 // Allow but ignore attributes on function types; this permits auto-upgrade.
1383 // FIXME: remove in LLVM 3.0.
Chris Lattnera925a142008-04-23 05:37:08 +00001384 const Type *RetTy = *$1;
1385 if (!FunctionType::isValidReturnType(RetTy))
1386 GEN_ERROR("Invalid result type for LLVM function");
1387
Chris Lattner58af2a12006-02-15 07:22:58 +00001388 std::vector<const Type*> Params;
Reid Spencer7b5d4662007-04-09 06:16:21 +00001389 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001390 for (; I != E; ++I ) {
Reid Spencer66728ef2007-03-20 01:13:36 +00001391 const Type *Ty = I->Ty->get();
Reid Spencer66728ef2007-03-20 01:13:36 +00001392 Params.push_back(Ty);
Reid Spencer14310612006-12-31 05:40:51 +00001393 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001394
Chris Lattner58af2a12006-02-15 07:22:58 +00001395 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1396 if (isVarArg) Params.pop_back();
1397
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001398 for (unsigned i = 0; i != Params.size(); ++i)
1399 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1400 GEN_ERROR("Function arguments must be value types!");
1401
1402 CHECK_FOR_ERROR
1403
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001404 FunctionType *FT = FunctionType::get(RetTy, Params, isVarArg);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001405 delete $3; // Delete the argument list
Reid Spencer14310612006-12-31 05:40:51 +00001406 delete $1; // Delete the return type handle
1407 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer61c83e02006-08-18 08:43:06 +00001408 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001409 }
Reid Spencer218ded22007-01-05 17:07:23 +00001410 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001411 // Allow but ignore attributes on function types; this permits auto-upgrade.
1412 // FIXME: remove in LLVM 3.0.
Reid Spencer14310612006-12-31 05:40:51 +00001413 std::vector<const Type*> Params;
Reid Spencer7b5d4662007-04-09 06:16:21 +00001414 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001415 for ( ; I != E; ++I ) {
Reid Spencer66728ef2007-03-20 01:13:36 +00001416 const Type* Ty = I->Ty->get();
Reid Spencer66728ef2007-03-20 01:13:36 +00001417 Params.push_back(Ty);
Reid Spencer14310612006-12-31 05:40:51 +00001418 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001419
Reid Spencer14310612006-12-31 05:40:51 +00001420 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1421 if (isVarArg) Params.pop_back();
1422
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001423 for (unsigned i = 0; i != Params.size(); ++i)
1424 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1425 GEN_ERROR("Function arguments must be value types!");
1426
1427 CHECK_FOR_ERROR
1428
Duncan Sandsdc024672007-11-27 13:23:08 +00001429 FunctionType *FT = FunctionType::get($1, Params, isVarArg);
Reid Spencer218ded22007-01-05 17:07:23 +00001430 delete $3; // Delete the argument list
Reid Spencer14310612006-12-31 05:40:51 +00001431 $$ = new PATypeHolder(HandleUpRefs(FT));
1432 CHECK_FOR_ERROR
1433 }
1434
1435 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001436 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, $2)));
Reid Spencera132e042006-12-03 05:46:11 +00001437 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001438 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001439 }
Chris Lattner32980692007-02-19 07:44:24 +00001440 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencera132e042006-12-03 05:46:11 +00001441 const llvm::Type* ElemTy = $4->get();
1442 if ((unsigned)$2 != $2)
1443 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner42a75512007-01-15 02:27:26 +00001444 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencer9d6565a2007-02-15 02:26:10 +00001445 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001446 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencera132e042006-12-03 05:46:11 +00001447 delete $4;
1448 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001449 }
1450 | '{' TypeListI '}' { // Structure type?
1451 std::vector<const Type*> Elements;
Reid Spencera132e042006-12-03 05:46:11 +00001452 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattner58af2a12006-02-15 07:22:58 +00001453 E = $2->end(); I != E; ++I)
Reid Spencera132e042006-12-03 05:46:11 +00001454 Elements.push_back(*I);
Chris Lattner58af2a12006-02-15 07:22:58 +00001455
Reid Spencera132e042006-12-03 05:46:11 +00001456 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattner58af2a12006-02-15 07:22:58 +00001457 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001458 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001459 }
1460 | '{' '}' { // Empty structure type?
Reid Spencera132e042006-12-03 05:46:11 +00001461 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001462 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001463 }
Andrew Lenharth6353e052006-12-08 18:07:09 +00001464 | '<' '{' TypeListI '}' '>' {
1465 std::vector<const Type*> Elements;
1466 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1467 E = $3->end(); I != E; ++I)
1468 Elements.push_back(*I);
1469
1470 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1471 delete $3;
1472 CHECK_FOR_ERROR
1473 }
1474 | '<' '{' '}' '>' { // Empty structure type?
1475 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1476 CHECK_FOR_ERROR
1477 }
Reid Spencer14310612006-12-31 05:40:51 +00001478 ;
1479
1480ArgType
Duncan Sandsdc024672007-11-27 13:23:08 +00001481 : Types OptParamAttrs {
1482 // Allow but ignore attributes on function types; this permits auto-upgrade.
1483 // FIXME: remove in LLVM 3.0.
Reid Spencer14310612006-12-31 05:40:51 +00001484 $$.Ty = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00001485 $$.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00001486 }
1487 ;
1488
Reid Spencer218ded22007-01-05 17:07:23 +00001489ResultTypes
1490 : Types {
Reid Spencer14310612006-12-31 05:40:51 +00001491 if (!UpRefs.empty())
1492 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Devang Patel20071732008-02-23 01:17:37 +00001493 if (!(*$1)->isFirstClassType() && !isa<StructType>($1->get()))
Reid Spencerb5334b02007-02-05 10:18:06 +00001494 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer218ded22007-01-05 17:07:23 +00001495 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00001496 }
Reid Spencer218ded22007-01-05 17:07:23 +00001497 | VOID {
1498 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencer14310612006-12-31 05:40:51 +00001499 }
1500 ;
1501
1502ArgTypeList : ArgType {
1503 $$ = new TypeWithAttrsList();
1504 $$->push_back($1);
1505 CHECK_FOR_ERROR
1506 }
1507 | ArgTypeList ',' ArgType {
1508 ($$=$1)->push_back($3);
1509 CHECK_FOR_ERROR
1510 }
1511 ;
1512
1513ArgTypeListI
1514 : ArgTypeList
1515 | ArgTypeList ',' DOTDOTDOT {
1516 $$=$1;
Reid Spencer18da0722007-04-11 02:44:20 +00001517 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00001518 TWA.Ty = new PATypeHolder(Type::VoidTy);
1519 $$->push_back(TWA);
1520 CHECK_FOR_ERROR
1521 }
1522 | DOTDOTDOT {
1523 $$ = new TypeWithAttrsList;
Reid Spencer18da0722007-04-11 02:44:20 +00001524 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00001525 TWA.Ty = new PATypeHolder(Type::VoidTy);
1526 $$->push_back(TWA);
1527 CHECK_FOR_ERROR
1528 }
1529 | /*empty*/ {
1530 $$ = new TypeWithAttrsList();
Reid Spencer61c83e02006-08-18 08:43:06 +00001531 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001532 };
1533
1534// TypeList - Used for struct declarations and as a basis for function type
1535// declaration type lists
1536//
Reid Spencer14310612006-12-31 05:40:51 +00001537TypeListI : Types {
Reid Spencera132e042006-12-03 05:46:11 +00001538 $$ = new std::list<PATypeHolder>();
Reid Spencer66728ef2007-03-20 01:13:36 +00001539 $$->push_back(*$1);
1540 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001541 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001542 }
Reid Spencer14310612006-12-31 05:40:51 +00001543 | TypeListI ',' Types {
Reid Spencer66728ef2007-03-20 01:13:36 +00001544 ($$=$1)->push_back(*$3);
1545 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001546 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001547 };
1548
Chris Lattner58af2a12006-02-15 07:22:58 +00001549// ConstVal - The various declarations that go into the constant pool. This
1550// production is used ONLY to represent constants that show up AFTER a 'const',
1551// 'constant' or 'global' token at global scope. Constants that can be inlined
1552// into other expressions (such as integers and constexprs) are handled by the
1553// ResolvedVal, ValueRef and ConstValueRef productions.
1554//
1555ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001556 if (!UpRefs.empty())
1557 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001558 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001559 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001560 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001561 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001562 const Type *ETy = ATy->getElementType();
Dan Gohman180c1692008-06-23 18:43:26 +00001563 uint64_t NumElements = ATy->getNumElements();
Chris Lattner58af2a12006-02-15 07:22:58 +00001564
1565 // Verify that we have the correct size...
Mon P Wang28873102008-06-25 08:15:39 +00001566 if (NumElements != uint64_t(-1) && NumElements != $3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001567 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001568 utostr($3->size()) + " arguments, but has size of " +
Mon P Wang28873102008-06-25 08:15:39 +00001569 utostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001570
1571 // Verify all elements are correct type!
1572 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001573 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001574 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001575 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001576 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001577 }
1578
Reid Spencera132e042006-12-03 05:46:11 +00001579 $$ = ConstantArray::get(ATy, *$3);
1580 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001581 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001582 }
1583 | Types '[' ']' {
Reid Spencer14310612006-12-31 05:40:51 +00001584 if (!UpRefs.empty())
1585 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001586 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001587 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001588 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001589 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001590
Dan Gohman180c1692008-06-23 18:43:26 +00001591 uint64_t NumElements = ATy->getNumElements();
Mon P Wang28873102008-06-25 08:15:39 +00001592 if (NumElements != uint64_t(-1) && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001593 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Mon P Wang28873102008-06-25 08:15:39 +00001594 " arguments, but has size of " + utostr(NumElements) +"");
Reid Spencera132e042006-12-03 05:46:11 +00001595 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1596 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001597 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001598 }
1599 | Types 'c' STRINGCONSTANT {
Reid Spencer14310612006-12-31 05:40:51 +00001600 if (!UpRefs.empty())
1601 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001602 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001603 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001604 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001605 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001606
Dan Gohman180c1692008-06-23 18:43:26 +00001607 uint64_t NumElements = ATy->getNumElements();
Chris Lattner58af2a12006-02-15 07:22:58 +00001608 const Type *ETy = ATy->getElementType();
Mon P Wang28873102008-06-25 08:15:39 +00001609 if (NumElements != uint64_t(-1) && NumElements != $3->length())
Reid Spencer61c83e02006-08-18 08:43:06 +00001610 GEN_ERROR("Can't build string constant of size " +
Mon P Wang28873102008-06-25 08:15:39 +00001611 utostr($3->length()) +
1612 " when array has size " + utostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001613 std::vector<Constant*> Vals;
Reid Spencer14310612006-12-31 05:40:51 +00001614 if (ETy == Type::Int8Ty) {
Mon P Wang28873102008-06-25 08:15:39 +00001615 for (uint64_t i = 0; i < $3->length(); ++i)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001616 Vals.push_back(ConstantInt::get(ETy, (*$3)[i]));
Chris Lattner58af2a12006-02-15 07:22:58 +00001617 } else {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001618 delete $3;
Reid Spencerb5334b02007-02-05 10:18:06 +00001619 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner58af2a12006-02-15 07:22:58 +00001620 }
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001621 delete $3;
Reid Spencera132e042006-12-03 05:46:11 +00001622 $$ = ConstantArray::get(ATy, Vals);
1623 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001624 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001625 }
1626 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001627 if (!UpRefs.empty())
1628 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00001629 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001630 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001631 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001632 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001633 const Type *ETy = PTy->getElementType();
Dan Gohman180c1692008-06-23 18:43:26 +00001634 unsigned NumElements = PTy->getNumElements();
Chris Lattner58af2a12006-02-15 07:22:58 +00001635
1636 // Verify that we have the correct size...
Mon P Wang28873102008-06-25 08:15:39 +00001637 if (NumElements != unsigned(-1) && NumElements != (unsigned)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001638 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001639 utostr($3->size()) + " arguments, but has size of " +
Mon P Wang28873102008-06-25 08:15:39 +00001640 utostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001641
1642 // Verify all elements are correct type!
1643 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001644 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001645 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001646 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001647 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001648 }
1649
Reid Spencer9d6565a2007-02-15 02:26:10 +00001650 $$ = ConstantVector::get(PTy, *$3);
Reid Spencera132e042006-12-03 05:46:11 +00001651 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001652 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001653 }
1654 | Types '{' ConstVector '}' {
Reid Spencera132e042006-12-03 05:46:11 +00001655 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001656 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001657 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001658 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001659
1660 if ($3->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001661 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001662
1663 // Check to ensure that constants are compatible with the type initializer!
1664 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencera132e042006-12-03 05:46:11 +00001665 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001666 GEN_ERROR("Expected type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001667 STy->getElementType(i)->getDescription() +
1668 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001669 " of structure initializer");
Chris Lattner58af2a12006-02-15 07:22:58 +00001670
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001671 // Check to ensure that Type is not packed
1672 if (STy->isPacked())
Chris Lattner6cdc6822007-04-26 05:31:05 +00001673 GEN_ERROR("Unpacked Initializer to vector type '" +
1674 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001675
Reid Spencera132e042006-12-03 05:46:11 +00001676 $$ = ConstantStruct::get(STy, *$3);
1677 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001678 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001679 }
1680 | Types '{' '}' {
Reid Spencer14310612006-12-31 05:40:51 +00001681 if (!UpRefs.empty())
1682 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001683 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001684 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001685 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001686 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001687
1688 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001689 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001690
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001691 // Check to ensure that Type is not packed
1692 if (STy->isPacked())
Chris Lattner6cdc6822007-04-26 05:31:05 +00001693 GEN_ERROR("Unpacked Initializer to vector type '" +
1694 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001695
1696 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1697 delete $1;
1698 CHECK_FOR_ERROR
1699 }
1700 | Types '<' '{' ConstVector '}' '>' {
1701 const StructType *STy = dyn_cast<StructType>($1->get());
1702 if (STy == 0)
1703 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001704 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001705
1706 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001707 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001708
1709 // Check to ensure that constants are compatible with the type initializer!
1710 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1711 if ((*$4)[i]->getType() != STy->getElementType(i))
1712 GEN_ERROR("Expected type '" +
1713 STy->getElementType(i)->getDescription() +
1714 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001715 " of structure initializer");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001716
1717 // Check to ensure that Type is packed
1718 if (!STy->isPacked())
Chris Lattner32980692007-02-19 07:44:24 +00001719 GEN_ERROR("Vector initializer to non-vector type '" +
1720 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001721
1722 $$ = ConstantStruct::get(STy, *$4);
1723 delete $1; delete $4;
1724 CHECK_FOR_ERROR
1725 }
1726 | Types '<' '{' '}' '>' {
1727 if (!UpRefs.empty())
1728 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1729 const StructType *STy = dyn_cast<StructType>($1->get());
1730 if (STy == 0)
1731 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001732 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001733
1734 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001735 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001736
1737 // Check to ensure that Type is packed
1738 if (!STy->isPacked())
Chris Lattner32980692007-02-19 07:44:24 +00001739 GEN_ERROR("Vector initializer to non-vector type '" +
1740 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001741
Reid Spencera132e042006-12-03 05:46:11 +00001742 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1743 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001744 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001745 }
1746 | Types NULL_TOK {
Reid Spencer14310612006-12-31 05:40:51 +00001747 if (!UpRefs.empty())
1748 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001749 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001750 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001751 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001752 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001753
Reid Spencera132e042006-12-03 05:46:11 +00001754 $$ = ConstantPointerNull::get(PTy);
1755 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001756 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001757 }
1758 | Types UNDEF {
Reid Spencer14310612006-12-31 05:40:51 +00001759 if (!UpRefs.empty())
1760 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001761 $$ = UndefValue::get($1->get());
1762 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001763 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001764 }
1765 | Types SymbolicValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00001766 if (!UpRefs.empty())
1767 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001768 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001769 if (Ty == 0)
Devang Patel5a970972008-02-19 22:27:01 +00001770 GEN_ERROR("Global const reference must be a pointer type " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001771
1772 // ConstExprs can exist in the body of a function, thus creating
1773 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencer93c40032007-03-19 18:40:50 +00001774 // the context of a function, getExistingVal will search the functions
Chris Lattner58af2a12006-02-15 07:22:58 +00001775 // symbol table instead of the module symbol table for the global symbol,
1776 // which throws things all off. To get around this, we just tell
Reid Spencer93c40032007-03-19 18:40:50 +00001777 // getExistingVal that we are at global scope here.
Chris Lattner58af2a12006-02-15 07:22:58 +00001778 //
1779 Function *SavedCurFn = CurFun.CurrentFunction;
1780 CurFun.CurrentFunction = 0;
1781
Reid Spencer93c40032007-03-19 18:40:50 +00001782 Value *V = getExistingVal(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001783 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001784
1785 CurFun.CurrentFunction = SavedCurFn;
1786
1787 // If this is an initializer for a constant pointer, which is referencing a
1788 // (currently) undefined variable, create a stub now that shall be replaced
1789 // in the future with the right type of variable.
1790 //
1791 if (V == 0) {
Reid Spencera9720f52007-02-05 17:04:00 +00001792 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001793 const PointerType *PT = cast<PointerType>(Ty);
1794
1795 // First check to see if the forward references value is already created!
1796 PerModuleInfo::GlobalRefsType::iterator I =
1797 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1798
1799 if (I != CurModule.GlobalRefs.end()) {
1800 V = I->second; // Placeholder already exists, use it...
1801 $2.destroy();
1802 } else {
1803 std::string Name;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001804 if ($2.Type == ValID::GlobalName)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001805 Name = $2.getName();
Reid Spencer41dff5e2007-01-26 08:05:27 +00001806 else if ($2.Type != ValID::GlobalID)
1807 GEN_ERROR("Invalid reference to global");
Chris Lattner58af2a12006-02-15 07:22:58 +00001808
1809 // Create the forward referenced global.
1810 GlobalValue *GV;
1811 if (const FunctionType *FTy =
1812 dyn_cast<FunctionType>(PT->getElementType())) {
Gabor Greife64d2482008-04-06 23:07:54 +00001813 GV = Function::Create(FTy, GlobalValue::ExternalWeakLinkage, Name,
1814 CurModule.CurrentModule);
Chris Lattner58af2a12006-02-15 07:22:58 +00001815 } else {
1816 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattner6cdc6822007-04-26 05:31:05 +00001817 GlobalValue::ExternalWeakLinkage, 0,
Chris Lattner58af2a12006-02-15 07:22:58 +00001818 Name, CurModule.CurrentModule);
1819 }
1820
1821 // Keep track of the fact that we have a forward ref to recycle it
1822 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1823 V = GV;
1824 }
1825 }
1826
Reid Spencera132e042006-12-03 05:46:11 +00001827 $$ = cast<GlobalValue>(V);
1828 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001829 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001830 }
1831 | Types ConstExpr {
Reid Spencer14310612006-12-31 05:40:51 +00001832 if (!UpRefs.empty())
1833 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001834 if ($1->get() != $2->getType())
Reid Spencere68853b2007-01-04 00:06:14 +00001835 GEN_ERROR("Mismatched types for constant expression: " +
1836 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001837 $$ = $2;
Reid Spencera132e042006-12-03 05:46:11 +00001838 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001839 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001840 }
1841 | Types ZEROINITIALIZER {
Reid Spencer14310612006-12-31 05:40:51 +00001842 if (!UpRefs.empty())
1843 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001844 const Type *Ty = $1->get();
Chris Lattner58af2a12006-02-15 07:22:58 +00001845 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerb5334b02007-02-05 10:18:06 +00001846 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencera132e042006-12-03 05:46:11 +00001847 $$ = Constant::getNullValue(Ty);
1848 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001849 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00001850 }
Reid Spencer14310612006-12-31 05:40:51 +00001851 | IntType ESINT64VAL { // integral constants
Reid Spencere4d87aa2006-12-23 06:05:41 +00001852 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001853 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencer49d273e2007-03-19 20:40:51 +00001854 $$ = ConstantInt::get($1, $2, true);
Reid Spencer38c91a92007-02-28 02:24:54 +00001855 CHECK_FOR_ERROR
1856 }
1857 | IntType ESAPINTVAL { // arbitrary precision integer constants
1858 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1859 if ($2->getBitWidth() > BitWidth) {
1860 GEN_ERROR("Constant value does not fit in type");
Reid Spencer10794272007-03-01 19:41:47 +00001861 }
1862 $2->sextOrTrunc(BitWidth);
1863 $$ = ConstantInt::get(*$2);
Reid Spencer38c91a92007-02-28 02:24:54 +00001864 delete $2;
Reid Spencere4d87aa2006-12-23 06:05:41 +00001865 CHECK_FOR_ERROR
1866 }
Reid Spencer14310612006-12-31 05:40:51 +00001867 | IntType EUINT64VAL { // integral constants
Reid Spencere4d87aa2006-12-23 06:05:41 +00001868 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001869 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencer49d273e2007-03-19 20:40:51 +00001870 $$ = ConstantInt::get($1, $2, false);
Reid Spencer38c91a92007-02-28 02:24:54 +00001871 CHECK_FOR_ERROR
1872 }
1873 | IntType EUAPINTVAL { // arbitrary precision integer constants
1874 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1875 if ($2->getBitWidth() > BitWidth) {
1876 GEN_ERROR("Constant value does not fit in type");
Reid Spencer10794272007-03-01 19:41:47 +00001877 }
1878 $2->zextOrTrunc(BitWidth);
1879 $$ = ConstantInt::get(*$2);
Reid Spencer38c91a92007-02-28 02:24:54 +00001880 delete $2;
Reid Spencere4d87aa2006-12-23 06:05:41 +00001881 CHECK_FOR_ERROR
1882 }
Reid Spencer6f407902007-01-13 05:00:46 +00001883 | INTTYPE TRUETOK { // Boolean constants
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001884 if (cast<IntegerType>($1)->getBitWidth() != 1)
1885 GEN_ERROR("Constant true must have type i1");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001886 $$ = ConstantInt::getTrue();
Reid Spencer61c83e02006-08-18 08:43:06 +00001887 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001888 }
Reid Spencer6f407902007-01-13 05:00:46 +00001889 | INTTYPE FALSETOK { // Boolean constants
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001890 if (cast<IntegerType>($1)->getBitWidth() != 1)
1891 GEN_ERROR("Constant false must have type i1");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001892 $$ = ConstantInt::getFalse();
Reid Spencer61c83e02006-08-18 08:43:06 +00001893 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001894 }
Dale Johannesenea583102007-09-12 03:31:28 +00001895 | FPType FPVAL { // Floating point constants
Dale Johannesen43421b32007-09-06 18:13:44 +00001896 if (!ConstantFP::isValueValidForType($1, *$2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001897 GEN_ERROR("Floating point constant invalid for type");
Dale Johannesenc72cd7e2007-09-11 18:33:39 +00001898 // Lexer has no type info, so builds all float and double FP constants
1899 // as double. Fix this here. Long double is done right.
1900 if (&$2->getSemantics()==&APFloat::IEEEdouble && $1==Type::FloatTy)
Dale Johannesen43421b32007-09-06 18:13:44 +00001901 $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
Chris Lattnerd8eb63f2008-04-20 00:41:19 +00001902 $$ = ConstantFP::get(*$2);
Dale Johannesencdd509a2007-09-07 21:07:57 +00001903 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001904 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001905 };
1906
1907
Reid Spencer3da59db2006-11-27 01:05:10 +00001908ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer14310612006-12-31 05:40:51 +00001909 if (!UpRefs.empty())
1910 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001911 Constant *Val = $3;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001912 const Type *DestTy = $5->get();
1913 if (!CastInst::castIsValid($1, $3, DestTy))
1914 GEN_ERROR("invalid cast opcode for cast from '" +
1915 Val->getType()->getDescription() + "' to '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001916 DestTy->getDescription() + "'");
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001917 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00001918 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00001919 }
1920 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001921 if (!isa<PointerType>($3->getType()))
Reid Spencerb5334b02007-02-05 10:18:06 +00001922 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00001923
Reid Spencera132e042006-12-03 05:46:11 +00001924 const Type *IdxTy =
Dan Gohman041e2eb2008-05-15 19:50:34 +00001925 GetElementPtrInst::getIndexedType($3->getType(), $4->begin(), $4->end());
Reid Spencera132e042006-12-03 05:46:11 +00001926 if (!IdxTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00001927 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencera132e042006-12-03 05:46:11 +00001928
Chris Lattnerf7469af2007-01-31 04:44:08 +00001929 SmallVector<Constant*, 8> IdxVec;
Reid Spencera132e042006-12-03 05:46:11 +00001930 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1931 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattner58af2a12006-02-15 07:22:58 +00001932 IdxVec.push_back(C);
1933 else
Reid Spencerb5334b02007-02-05 10:18:06 +00001934 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattner58af2a12006-02-15 07:22:58 +00001935
1936 delete $4;
1937
Chris Lattnerf7469af2007-01-31 04:44:08 +00001938 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer61c83e02006-08-18 08:43:06 +00001939 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001940 }
1941 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer4fe16d62007-01-11 18:21:29 +00001942 if ($3->getType() != Type::Int1Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00001943 GEN_ERROR("Select condition must be of boolean type");
Reid Spencera132e042006-12-03 05:46:11 +00001944 if ($5->getType() != $7->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001945 GEN_ERROR("Select operand types must match");
Reid Spencera132e042006-12-03 05:46:11 +00001946 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001947 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001948 }
1949 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001950 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001951 GEN_ERROR("Binary operator types must match");
Reid Spencer1628cec2006-10-26 06:15:43 +00001952 CHECK_FOR_ERROR;
Reid Spencer9eef56f2006-12-05 19:16:11 +00001953 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner58af2a12006-02-15 07:22:58 +00001954 }
1955 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001956 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001957 GEN_ERROR("Logical operator types must match");
Chris Lattner42a75512007-01-15 02:27:26 +00001958 if (!$3->getType()->isInteger()) {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00001959 if (!isa<VectorType>($3->getType()) ||
Reid Spencer9d6565a2007-02-15 02:26:10 +00001960 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00001961 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00001962 }
Reid Spencera132e042006-12-03 05:46:11 +00001963 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001964 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001965 }
Reid Spencer4012e832006-12-04 05:24:24 +00001966 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1967 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001968 GEN_ERROR("icmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00001969 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00001970 }
Reid Spencer4012e832006-12-04 05:24:24 +00001971 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1972 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001973 GEN_ERROR("fcmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00001974 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00001975 }
Nate Begemanac80ade2008-05-12 19:01:56 +00001976 | VICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1977 if ($4->getType() != $6->getType())
1978 GEN_ERROR("vicmp operand types must match");
1979 $$ = ConstantExpr::getVICmp($2, $4, $6);
1980 }
1981 | VFCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1982 if ($4->getType() != $6->getType())
1983 GEN_ERROR("vfcmp operand types must match");
1984 $$ = ConstantExpr::getVFCmp($2, $4, $6);
1985 }
Chris Lattner58af2a12006-02-15 07:22:58 +00001986 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001987 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerb5334b02007-02-05 10:18:06 +00001988 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00001989 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001990 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00001991 }
1992 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001993 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00001994 GEN_ERROR("Invalid insertelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00001995 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001996 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00001997 }
1998 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001999 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00002000 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00002001 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00002002 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00002003 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002004 | EXTRACTVALUE '(' ConstVal ConstantIndexList ')' {
Dan Gohmane4977cf2008-05-23 01:55:30 +00002005 if (!isa<StructType>($3->getType()) && !isa<ArrayType>($3->getType()))
2006 GEN_ERROR("ExtractValue requires an aggregate operand");
2007
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002008 $$ = ConstantExpr::getExtractValue($3, &(*$4)[0], $4->size());
Dan Gohmane4977cf2008-05-23 01:55:30 +00002009 delete $4;
Dan Gohmane4977cf2008-05-23 01:55:30 +00002010 CHECK_FOR_ERROR
2011 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002012 | INSERTVALUE '(' ConstVal ',' ConstVal ConstantIndexList ')' {
Dan Gohmane4977cf2008-05-23 01:55:30 +00002013 if (!isa<StructType>($3->getType()) && !isa<ArrayType>($3->getType()))
2014 GEN_ERROR("InsertValue requires an aggregate operand");
2015
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002016 $$ = ConstantExpr::getInsertValue($3, $5, &(*$6)[0], $6->size());
Dan Gohmane4977cf2008-05-23 01:55:30 +00002017 delete $6;
Dan Gohmane4977cf2008-05-23 01:55:30 +00002018 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002019 };
2020
Chris Lattnerd25db202006-04-08 03:55:17 +00002021
Chris Lattner58af2a12006-02-15 07:22:58 +00002022// ConstVector - A list of comma separated constants.
2023ConstVector : ConstVector ',' ConstVal {
2024 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002025 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002026 }
2027 | ConstVal {
Reid Spencera132e042006-12-03 05:46:11 +00002028 $$ = new std::vector<Constant*>();
Chris Lattner58af2a12006-02-15 07:22:58 +00002029 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002030 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002031 };
2032
2033
2034// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
2035GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
2036
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002037// ThreadLocal
2038ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
2039
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002040// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
2041AliaseeRef : ResultTypes SymbolicValueRef {
2042 const Type* VTy = $1->get();
2043 Value *V = getVal(VTy, $2);
Chris Lattner0275cff2007-08-06 21:00:46 +00002044 CHECK_FOR_ERROR
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002045 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
2046 if (!Aliasee)
2047 GEN_ERROR("Aliases can be created only to global values");
2048
2049 $$ = Aliasee;
2050 CHECK_FOR_ERROR
2051 delete $1;
2052 }
2053 | BITCAST '(' AliaseeRef TO Types ')' {
2054 Constant *Val = $3;
2055 const Type *DestTy = $5->get();
2056 if (!CastInst::castIsValid($1, $3, DestTy))
2057 GEN_ERROR("invalid cast opcode for cast from '" +
2058 Val->getType()->getDescription() + "' to '" +
2059 DestTy->getDescription() + "'");
2060
2061 $$ = ConstantExpr::getCast($1, $3, DestTy);
2062 CHECK_FOR_ERROR
2063 delete $5;
2064 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002065
2066//===----------------------------------------------------------------------===//
2067// Rules to match Modules
2068//===----------------------------------------------------------------------===//
2069
2070// Module rule: Capture the result of parsing the whole file into a result
2071// variable...
2072//
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002073Module
2074 : DefinitionList {
2075 $$ = ParserResult = CurModule.CurrentModule;
2076 CurModule.ModuleDone();
2077 CHECK_FOR_ERROR;
2078 }
2079 | /*empty*/ {
2080 $$ = ParserResult = CurModule.CurrentModule;
2081 CurModule.ModuleDone();
2082 CHECK_FOR_ERROR;
2083 }
2084 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002085
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002086DefinitionList
2087 : Definition
2088 | DefinitionList Definition
2089 ;
2090
2091Definition
Jeff Cohen361c3ef2007-01-21 19:19:31 +00002092 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner58af2a12006-02-15 07:22:58 +00002093 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00002094 CHECK_FOR_ERROR
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002095 }
2096 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer61c83e02006-08-18 08:43:06 +00002097 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002098 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002099 | MODULE ASM_TOK AsmBlock {
Reid Spencer61c83e02006-08-18 08:43:06 +00002100 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002101 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002102 | OptLocalAssign TYPE Types {
Reid Spencer14310612006-12-31 05:40:51 +00002103 if (!UpRefs.empty())
2104 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002105 // Eagerly resolve types. This is not an optimization, this is a
2106 // requirement that is due to the fact that we could have this:
2107 //
2108 // %list = type { %list * }
2109 // %list = type { %list * } ; repeated type decl
2110 //
2111 // If types are not resolved eagerly, then the two types will not be
2112 // determined to be the same type!
2113 //
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002114 ResolveTypeTo($1, *$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002115
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002116 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002117 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002118 // If this is a named type that is not a redefinition, add it to the slot
2119 // table.
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002120 CurModule.Types.push_back(*$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002121 }
Reid Spencera132e042006-12-03 05:46:11 +00002122
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002123 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002124 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002125 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002126 | OptLocalAssign TYPE VOID {
Reid Spencer14310612006-12-31 05:40:51 +00002127 ResolveTypeTo($1, $3);
2128
2129 if (!setTypeName($3, $1) && !$1) {
2130 CHECK_FOR_ERROR
2131 // If this is a named type that is not a redefinition, add it to the slot
2132 // table.
2133 CurModule.Types.push_back($3);
2134 }
2135 CHECK_FOR_ERROR
2136 }
Christopher Lambbf3348d2007-12-12 08:45:45 +00002137 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal
2138 OptAddrSpace {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002139 /* "Externally Visible" Linkage */
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002140 if ($5 == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002141 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002142 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
Christopher Lambbf3348d2007-12-12 08:45:45 +00002143 $2, $4, $5->getType(), $5, $3, $6);
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00002144 CHECK_FOR_ERROR
2145 } GlobalVarAttributes {
2146 CurGV = 0;
2147 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002148 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambbf3348d2007-12-12 08:45:45 +00002149 ConstVal OptAddrSpace {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002150 if ($6 == 0)
2151 GEN_ERROR("Global value initializer is not a constant");
Christopher Lambbf3348d2007-12-12 08:45:45 +00002152 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4, $7);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002153 CHECK_FOR_ERROR
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002154 } GlobalVarAttributes {
2155 CurGV = 0;
2156 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002157 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambbf3348d2007-12-12 08:45:45 +00002158 Types OptAddrSpace {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002159 if (!UpRefs.empty())
2160 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
Christopher Lambbf3348d2007-12-12 08:45:45 +00002161 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4, $7);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002162 CHECK_FOR_ERROR
2163 delete $6;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002164 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002165 CurGV = 0;
2166 CHECK_FOR_ERROR
2167 }
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002168 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002169 std::string Name;
2170 if ($1) {
2171 Name = *$1;
2172 delete $1;
2173 }
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002174 if (Name.empty())
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002175 GEN_ERROR("Alias name cannot be empty");
2176
2177 Constant* Aliasee = $5;
2178 if (Aliasee == 0)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002179 GEN_ERROR(std::string("Invalid aliasee for alias: ") + Name);
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002180
2181 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2182 CurModule.CurrentModule);
2183 GA->setVisibility($2);
2184 InsertValue(GA, CurModule.Values);
Chris Lattner569f7372007-09-10 23:24:14 +00002185
2186
2187 // If there was a forward reference of this alias, resolve it now.
2188
2189 ValID ID;
2190 if (!Name.empty())
2191 ID = ValID::createGlobalName(Name);
2192 else
2193 ID = ValID::createGlobalID(CurModule.Values.size()-1);
2194
2195 if (GlobalValue *FWGV =
2196 CurModule.GetForwardRefForGlobal(GA->getType(), ID)) {
2197 // Replace uses of the fwdref with the actual alias.
2198 FWGV->replaceAllUsesWith(GA);
2199 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(FWGV))
2200 GV->eraseFromParent();
2201 else
2202 cast<Function>(FWGV)->eraseFromParent();
2203 }
2204 ID.destroy();
2205
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002206 CHECK_FOR_ERROR
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002207 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002208 | TARGET TargetDefinition {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002209 CHECK_FOR_ERROR
2210 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002211 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00002212 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002213 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002214 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002215
2216
2217AsmBlock : STRINGCONSTANT {
2218 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattner58af2a12006-02-15 07:22:58 +00002219 if (AsmSoFar.empty())
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002220 CurModule.CurrentModule->setModuleInlineAsm(*$1);
Chris Lattner58af2a12006-02-15 07:22:58 +00002221 else
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002222 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+*$1);
2223 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002224 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002225};
2226
Reid Spencer41dff5e2007-01-26 08:05:27 +00002227TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002228 CurModule.CurrentModule->setTargetTriple(*$3);
2229 delete $3;
John Criswell2f6a8b12006-10-24 19:09:48 +00002230 }
Chris Lattner1ae022f2006-10-22 06:08:13 +00002231 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002232 CurModule.CurrentModule->setDataLayout(*$3);
2233 delete $3;
Owen Anderson1dc69692006-10-18 02:21:48 +00002234 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002235
2236LibrariesDefinition : '[' LibList ']';
2237
2238LibList : LibList ',' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002239 CurModule.CurrentModule->addLibrary(*$3);
2240 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002241 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002242 }
2243 | STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002244 CurModule.CurrentModule->addLibrary(*$1);
2245 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002246 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002247 }
2248 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00002249 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002250 }
2251 ;
2252
2253//===----------------------------------------------------------------------===//
2254// Rules to match Function Headers
2255//===----------------------------------------------------------------------===//
2256
Reid Spencer41dff5e2007-01-26 08:05:27 +00002257ArgListH : ArgListH ',' Types OptParamAttrs OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002258 if (!UpRefs.empty())
2259 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002260 if (!(*$3)->isFirstClassType())
2261 GEN_ERROR("Argument types must be first-class");
Reid Spencer14310612006-12-31 05:40:51 +00002262 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00002263 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002264 $1->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002265 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002266 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002267 | Types OptParamAttrs OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002268 if (!UpRefs.empty())
2269 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002270 if (!(*$1)->isFirstClassType())
2271 GEN_ERROR("Argument types must be first-class");
Reid Spencer14310612006-12-31 05:40:51 +00002272 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2273 $$ = new ArgListType;
2274 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002275 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002276 };
2277
2278ArgList : ArgListH {
2279 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002280 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002281 }
2282 | ArgListH ',' DOTDOTDOT {
2283 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002284 struct ArgListEntry E;
2285 E.Ty = new PATypeHolder(Type::VoidTy);
2286 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002287 E.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00002288 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002289 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002290 }
2291 | DOTDOTDOT {
Reid Spencer14310612006-12-31 05:40:51 +00002292 $$ = new ArgListType;
2293 struct ArgListEntry E;
2294 E.Ty = new PATypeHolder(Type::VoidTy);
2295 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002296 E.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00002297 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002298 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002299 }
2300 | /* empty */ {
2301 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00002302 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002303 };
2304
Reid Spencer41dff5e2007-01-26 08:05:27 +00002305FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002306 OptFuncAttrs OptSection OptAlign OptGC {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002307 std::string FunctionName(*$3);
2308 delete $3; // Free strdup'd memory!
Chris Lattner58af2a12006-02-15 07:22:58 +00002309
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002310 // Check the function result for abstractness if this is a define. We should
2311 // have no abstract types at this point
Reid Spencer218ded22007-01-05 17:07:23 +00002312 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2313 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002314
Chris Lattnera925a142008-04-23 05:37:08 +00002315 if (!FunctionType::isValidReturnType(*$2))
2316 GEN_ERROR("Invalid result type for LLVM function");
2317
Chris Lattner58af2a12006-02-15 07:22:58 +00002318 std::vector<const Type*> ParamTypeList;
Chris Lattner58d74912008-03-12 17:45:29 +00002319 SmallVector<ParamAttrsWithIndex, 8> Attrs;
2320 if ($7 != ParamAttr::None)
2321 Attrs.push_back(ParamAttrsWithIndex::get(0, $7));
Chris Lattner58af2a12006-02-15 07:22:58 +00002322 if ($5) { // If there are arguments...
Reid Spencer7b5d4662007-04-09 06:16:21 +00002323 unsigned index = 1;
2324 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002325 const Type* Ty = I->Ty->get();
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002326 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2327 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencer14310612006-12-31 05:40:51 +00002328 ParamTypeList.push_back(Ty);
Chris Lattner58d74912008-03-12 17:45:29 +00002329 if (Ty != Type::VoidTy && I->Attrs != ParamAttr::None)
2330 Attrs.push_back(ParamAttrsWithIndex::get(index, I->Attrs));
Reid Spencer14310612006-12-31 05:40:51 +00002331 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002332 }
2333
2334 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2335 if (isVarArg) ParamTypeList.pop_back();
2336
Chris Lattner58d74912008-03-12 17:45:29 +00002337 PAListPtr PAL;
Christopher Lamb5c104242007-04-22 20:09:11 +00002338 if (!Attrs.empty())
Chris Lattner58d74912008-03-12 17:45:29 +00002339 PAL = PAListPtr::get(Attrs.begin(), Attrs.end());
Reid Spencer7b5d4662007-04-09 06:16:21 +00002340
Duncan Sandsdc024672007-11-27 13:23:08 +00002341 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002342 const PointerType *PFT = PointerType::getUnqual(FT);
Reid Spencer218ded22007-01-05 17:07:23 +00002343 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002344
2345 ValID ID;
2346 if (!FunctionName.empty()) {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002347 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattner58af2a12006-02-15 07:22:58 +00002348 } else {
Reid Spencer93c40032007-03-19 18:40:50 +00002349 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002350 }
2351
2352 Function *Fn = 0;
2353 // See if this function was forward referenced. If so, recycle the object.
2354 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2355 // Move the function to the end of the list, from whereever it was
2356 // previously inserted.
2357 Fn = cast<Function>(FWRef);
Chris Lattner58d74912008-03-12 17:45:29 +00002358 assert(Fn->getParamAttrs().isEmpty() &&
2359 "Forward reference has parameter attributes!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002360 CurModule.CurrentModule->getFunctionList().remove(Fn);
2361 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2362 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spenceref9b9a72007-02-05 20:47:22 +00002363 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002364 if (Fn->getFunctionType() != FT ) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002365 // The existing function doesn't have the same type. This is an overload
2366 // error.
2367 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Duncan Sandsdc024672007-11-27 13:23:08 +00002368 } else if (Fn->getParamAttrs() != PAL) {
2369 // The existing function doesn't have the same parameter attributes.
2370 // This is an overload error.
2371 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002372 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
Chris Lattner6cdc6822007-04-26 05:31:05 +00002373 // Neither the existing or the current function is a declaration and they
2374 // have the same name and same type. Clearly this is a redefinition.
2375 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Duncan Sandsdc024672007-11-27 13:23:08 +00002376 } else if (Fn->isDeclaration()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002377 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattner58af2a12006-02-15 07:22:58 +00002378 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2379 AI != AE; ++AI)
2380 AI->setName("");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002381 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002382 } else { // Not already defined?
Gabor Greife64d2482008-04-06 23:07:54 +00002383 Fn = Function::Create(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
2384 CurModule.CurrentModule);
Chris Lattner58af2a12006-02-15 07:22:58 +00002385 InsertValue(Fn, CurModule.Values);
2386 }
2387
2388 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002389
2390 if (CurFun.isDeclare) {
2391 // If we have declaration, always overwrite linkage. This will allow us to
2392 // correctly handle cases, when pointer to function is passed as argument to
2393 // another function.
2394 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002395 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002396 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002397 Fn->setCallingConv($1);
Duncan Sandsdc024672007-11-27 13:23:08 +00002398 Fn->setParamAttrs(PAL);
Reid Spencer218ded22007-01-05 17:07:23 +00002399 Fn->setAlignment($9);
2400 if ($8) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002401 Fn->setSection(*$8);
2402 delete $8;
Chris Lattner58af2a12006-02-15 07:22:58 +00002403 }
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002404 if ($10) {
Gordon Henriksen5d82cd32008-08-17 18:48:50 +00002405 Fn->setGC($10->c_str());
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002406 delete $10;
2407 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002408
2409 // Add all of the arguments we parsed to the function...
2410 if ($5) { // Is null if empty...
2411 if (isVarArg) { // Nuke the last entry
Reid Spenceref9b9a72007-02-05 20:47:22 +00002412 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0 &&
Reid Spencera9720f52007-02-05 17:04:00 +00002413 "Not a varargs marker!");
Reid Spencer14310612006-12-31 05:40:51 +00002414 delete $5->back().Ty;
Chris Lattner58af2a12006-02-15 07:22:58 +00002415 $5->pop_back(); // Delete the last entry
2416 }
2417 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002418 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencer14310612006-12-31 05:40:51 +00002419 unsigned Idx = 1;
Reid Spenceref9b9a72007-02-05 20:47:22 +00002420 for (ArgListType::iterator I = $5->begin();
2421 I != $5->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencer14310612006-12-31 05:40:51 +00002422 delete I->Ty; // Delete the typeholder...
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002423 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002424 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002425 InsertValue(ArgIt);
Reid Spencer14310612006-12-31 05:40:51 +00002426 Idx++;
Chris Lattner58af2a12006-02-15 07:22:58 +00002427 }
Reid Spencera132e042006-12-03 05:46:11 +00002428
Chris Lattner58af2a12006-02-15 07:22:58 +00002429 delete $5; // We're now done with the argument list
2430 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002431 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002432};
2433
2434BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2435
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002436FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner58af2a12006-02-15 07:22:58 +00002437 $$ = CurFun.CurrentFunction;
2438
2439 // Make sure that we keep track of the linkage type even if there was a
2440 // previous "declare".
2441 $$->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002442 $$->setVisibility($2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002443};
2444
2445END : ENDTOK | '}'; // Allow end of '}' to end a function
2446
2447Function : BasicBlockList END {
2448 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002449 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002450};
2451
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002452FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencer14310612006-12-31 05:40:51 +00002453 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002454 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002455 $$ = CurFun.CurrentFunction;
2456 CurFun.FunctionDone();
2457 CHECK_FOR_ERROR
2458 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002459
2460//===----------------------------------------------------------------------===//
2461// Rules to match Basic Blocks
2462//===----------------------------------------------------------------------===//
2463
2464OptSideEffect : /* empty */ {
2465 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002466 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002467 }
2468 | SIDEEFFECT {
2469 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002470 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002471 };
2472
2473ConstValueRef : ESINT64VAL { // A reference to a direct constant
2474 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002475 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002476 }
2477 | EUINT64VAL {
2478 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002479 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002480 }
Chris Lattner1913b942008-07-11 00:30:39 +00002481 | ESAPINTVAL { // arbitrary precision integer constants
2482 $$ = ValID::create(*$1, true);
2483 delete $1;
2484 CHECK_FOR_ERROR
2485 }
2486 | EUAPINTVAL { // arbitrary precision integer constants
2487 $$ = ValID::create(*$1, false);
2488 delete $1;
2489 CHECK_FOR_ERROR
2490 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002491 | FPVAL { // Perhaps it's an FP constant?
2492 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002493 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002494 }
2495 | TRUETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002496 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002497 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002498 }
2499 | FALSETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002500 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002501 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002502 }
2503 | NULL_TOK {
2504 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002505 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002506 }
2507 | UNDEF {
2508 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002509 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002510 }
2511 | ZEROINITIALIZER { // A vector zero constant.
2512 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002513 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002514 }
2515 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencera132e042006-12-03 05:46:11 +00002516 const Type *ETy = (*$2)[0]->getType();
Dan Gohman180c1692008-06-23 18:43:26 +00002517 unsigned NumElements = $2->size();
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002518
2519 if (!ETy->isInteger() && !ETy->isFloatingPoint())
2520 GEN_ERROR("Invalid vector element type: " + ETy->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002521
Reid Spencer9d6565a2007-02-15 02:26:10 +00002522 VectorType* pt = VectorType::get(ETy, NumElements);
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002523 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(pt));
Chris Lattner58af2a12006-02-15 07:22:58 +00002524
2525 // Verify all elements are correct type!
2526 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00002527 if (ETy != (*$2)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002528 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00002529 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencera132e042006-12-03 05:46:11 +00002530 (*$2)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00002531 }
2532
Reid Spencer9d6565a2007-02-15 02:26:10 +00002533 $$ = ValID::create(ConstantVector::get(pt, *$2));
Chris Lattner58af2a12006-02-15 07:22:58 +00002534 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002535 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002536 }
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002537 | '[' ConstVector ']' { // Nonempty unsized arr
2538 const Type *ETy = (*$2)[0]->getType();
Dan Gohman180c1692008-06-23 18:43:26 +00002539 uint64_t NumElements = $2->size();
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002540
2541 if (!ETy->isFirstClassType())
2542 GEN_ERROR("Invalid array element type: " + ETy->getDescription());
2543
2544 ArrayType *ATy = ArrayType::get(ETy, NumElements);
2545 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(ATy));
2546
2547 // Verify all elements are correct type!
2548 for (unsigned i = 0; i < $2->size(); i++) {
2549 if (ETy != (*$2)[i]->getType())
2550 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
2551 ETy->getDescription() +"' as required!\nIt is of type '"+
2552 (*$2)[i]->getType()->getDescription() + "'.");
2553 }
2554
2555 $$ = ValID::create(ConstantArray::get(ATy, *$2));
2556 delete PTy; delete $2;
2557 CHECK_FOR_ERROR
2558 }
2559 | '[' ']' {
Dan Gohman180c1692008-06-23 18:43:26 +00002560 // Use undef instead of an array because it's inconvenient to determine
2561 // the element type at this point, there being no elements to examine.
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002562 $$ = ValID::createUndef();
2563 CHECK_FOR_ERROR
2564 }
2565 | 'c' STRINGCONSTANT {
Dan Gohman180c1692008-06-23 18:43:26 +00002566 uint64_t NumElements = $2->length();
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002567 const Type *ETy = Type::Int8Ty;
2568
2569 ArrayType *ATy = ArrayType::get(ETy, NumElements);
2570
2571 std::vector<Constant*> Vals;
2572 for (unsigned i = 0; i < $2->length(); ++i)
2573 Vals.push_back(ConstantInt::get(ETy, (*$2)[i]));
2574 delete $2;
2575 $$ = ValID::create(ConstantArray::get(ATy, Vals));
2576 CHECK_FOR_ERROR
2577 }
2578 | '{' ConstVector '}' {
2579 std::vector<const Type*> Elements($2->size());
2580 for (unsigned i = 0, e = $2->size(); i != e; ++i)
2581 Elements[i] = (*$2)[i]->getType();
2582
2583 const StructType *STy = StructType::get(Elements);
2584 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(STy));
2585
2586 $$ = ValID::create(ConstantStruct::get(STy, *$2));
2587 delete PTy; delete $2;
2588 CHECK_FOR_ERROR
2589 }
2590 | '{' '}' {
2591 const StructType *STy = StructType::get(std::vector<const Type*>());
2592 $$ = ValID::create(ConstantStruct::get(STy, std::vector<Constant*>()));
2593 CHECK_FOR_ERROR
2594 }
2595 | '<' '{' ConstVector '}' '>' {
2596 std::vector<const Type*> Elements($3->size());
2597 for (unsigned i = 0, e = $3->size(); i != e; ++i)
2598 Elements[i] = (*$3)[i]->getType();
2599
2600 const StructType *STy = StructType::get(Elements, /*isPacked=*/true);
2601 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(STy));
2602
2603 $$ = ValID::create(ConstantStruct::get(STy, *$3));
2604 delete PTy; delete $3;
2605 CHECK_FOR_ERROR
2606 }
2607 | '<' '{' '}' '>' {
2608 const StructType *STy = StructType::get(std::vector<const Type*>(),
2609 /*isPacked=*/true);
2610 $$ = ValID::create(ConstantStruct::get(STy, std::vector<Constant*>()));
2611 CHECK_FOR_ERROR
2612 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002613 | ConstExpr {
Reid Spencera132e042006-12-03 05:46:11 +00002614 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002615 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002616 }
2617 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002618 $$ = ValID::createInlineAsm(*$3, *$5, $2);
2619 delete $3;
2620 delete $5;
Reid Spencer61c83e02006-08-18 08:43:06 +00002621 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002622 };
2623
2624// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2625// another value.
2626//
Reid Spencer41dff5e2007-01-26 08:05:27 +00002627SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2628 $$ = ValID::createLocalID($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002629 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002630 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002631 | GLOBALVAL_ID {
2632 $$ = ValID::createGlobalID($1);
2633 CHECK_FOR_ERROR
2634 }
2635 | LocalName { // Is it a named reference...?
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002636 $$ = ValID::createLocalName(*$1);
2637 delete $1;
Reid Spencer41dff5e2007-01-26 08:05:27 +00002638 CHECK_FOR_ERROR
2639 }
2640 | GlobalName { // Is it a named reference...?
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002641 $$ = ValID::createGlobalName(*$1);
2642 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002643 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002644 };
2645
2646// ValueRef - A reference to a definition... either constant or symbolic
2647ValueRef : SymbolicValueRef | ConstValueRef;
2648
2649
2650// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2651// type immediately preceeds the value reference, and allows complex constant
2652// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2653ResolvedVal : Types ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002654 if (!UpRefs.empty())
2655 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2656 $$ = getVal(*$1, $2);
2657 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002658 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00002659 }
2660 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002661
Devang Patel7990dc72008-02-20 22:40:23 +00002662ReturnedVal : ResolvedVal {
2663 $$ = new std::vector<Value *>();
2664 $$->push_back($1);
2665 CHECK_FOR_ERROR
2666 }
Devang Patel6bfc63b2008-02-23 00:38:56 +00002667 | ReturnedVal ',' ResolvedVal {
Devang Patel7990dc72008-02-20 22:40:23 +00002668 ($$=$1)->push_back($3);
2669 CHECK_FOR_ERROR
2670 };
2671
Chris Lattner58af2a12006-02-15 07:22:58 +00002672BasicBlockList : BasicBlockList BasicBlock {
2673 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002674 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002675 }
2676 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
2677 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002678 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002679 };
2680
2681
2682// Basic blocks are terminated by branching instructions:
2683// br, br/cc, switch, ret
2684//
Chris Lattner15bd0952008-08-29 17:20:18 +00002685BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner58af2a12006-02-15 07:22:58 +00002686 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002687 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002688 InsertValue($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002689 $1->getInstList().push_back($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002690 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002691 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002692 };
2693
Chris Lattner15bd0952008-08-29 17:20:18 +00002694BasicBlock : InstructionList LocalNumber BBTerminatorInst {
2695 CHECK_FOR_ERROR
2696 int ValNum = InsertValue($3);
2697 if (ValNum != (int)$2)
2698 GEN_ERROR("Result value number %" + utostr($2) +
2699 " is incorrect, expected %" + utostr((unsigned)ValNum));
2700
2701 $1->getInstList().push_back($3);
2702 $$ = $1;
2703 CHECK_FOR_ERROR
2704};
2705
2706
Chris Lattner58af2a12006-02-15 07:22:58 +00002707InstructionList : InstructionList Inst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002708 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2709 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2710 if (CI2->getParent() == 0)
2711 $1->getInstList().push_back(CI2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002712 $1->getInstList().push_back($2);
2713 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002714 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002715 }
Reid Spencer93c40032007-03-19 18:40:50 +00002716 | /* empty */ { // Empty space between instruction lists
Nick Lewycky280a6e62008-04-25 16:53:59 +00002717 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
Reid Spencer61c83e02006-08-18 08:43:06 +00002718 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002719 }
Reid Spencer93c40032007-03-19 18:40:50 +00002720 | LABELSTR { // Labelled (named) basic block
Nick Lewycky280a6e62008-04-25 16:53:59 +00002721 $$ = defineBBVal(ValID::createLocalName(*$1));
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002722 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002723 CHECK_FOR_ERROR
Nick Lewycky280a6e62008-04-25 16:53:59 +00002724
Chris Lattner58af2a12006-02-15 07:22:58 +00002725 };
2726
Devang Patel7990dc72008-02-20 22:40:23 +00002727BBTerminatorInst :
2728 RET ReturnedVal { // Return with a result...
Devang Patelb82b7f22008-02-26 22:17:48 +00002729 ValueList &VL = *$2;
Devang Patel13b823c2008-02-26 23:19:08 +00002730 assert(!VL.empty() && "Invalid ret operands!");
Dan Gohman1a570242008-07-23 00:54:54 +00002731 const Type *ReturnType = CurFun.CurrentFunction->getReturnType();
2732 if (VL.size() > 1 ||
2733 (isa<StructType>(ReturnType) &&
2734 (VL.empty() || VL[0]->getType() != ReturnType))) {
2735 Value *RV = UndefValue::get(ReturnType);
2736 for (unsigned i = 0, e = VL.size(); i != e; ++i) {
2737 Instruction *I = InsertValueInst::Create(RV, VL[i], i, "mrv");
2738 ($<BasicBlockVal>-1)->getInstList().push_back(I);
2739 RV = I;
2740 }
2741 $$ = ReturnInst::Create(RV);
2742 } else {
2743 $$ = ReturnInst::Create(VL[0]);
2744 }
Devang Patel7990dc72008-02-20 22:40:23 +00002745 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002746 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002747 }
Reid Spencer93c40032007-03-19 18:40:50 +00002748 | RET VOID { // Return with no result...
Gabor Greife64d2482008-04-06 23:07:54 +00002749 $$ = ReturnInst::Create();
Reid Spencer61c83e02006-08-18 08:43:06 +00002750 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002751 }
Reid Spencer93c40032007-03-19 18:40:50 +00002752 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002753 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002754 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002755 $$ = BranchInst::Create(tmpBB);
Reid Spencer93c40032007-03-19 18:40:50 +00002756 } // Conditional Branch...
Reid Spencer6f407902007-01-13 05:00:46 +00002757 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002758 if (cast<IntegerType>($2)->getBitWidth() != 1)
2759 GEN_ERROR("Branch condition must have type i1");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002760 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002761 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002762 BasicBlock* tmpBBB = getBBVal($9);
2763 CHECK_FOR_ERROR
Reid Spencer4fe16d62007-01-11 18:21:29 +00002764 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002765 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002766 $$ = BranchInst::Create(tmpBBA, tmpBBB, tmpVal);
Chris Lattner58af2a12006-02-15 07:22:58 +00002767 }
2768 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002769 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002770 CHECK_FOR_ERROR
2771 BasicBlock* tmpBB = getBBVal($6);
2772 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002773 SwitchInst *S = SwitchInst::Create(tmpVal, tmpBB, $8->size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002774 $$ = S;
2775
2776 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2777 E = $8->end();
2778 for (; I != E; ++I) {
2779 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2780 S->addCase(CI, I->second);
2781 else
Reid Spencerb5334b02007-02-05 10:18:06 +00002782 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattner58af2a12006-02-15 07:22:58 +00002783 }
2784 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002785 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002786 }
2787 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002788 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002789 CHECK_FOR_ERROR
2790 BasicBlock* tmpBB = getBBVal($6);
2791 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002792 SwitchInst *S = SwitchInst::Create(tmpVal, tmpBB, 0);
Chris Lattner58af2a12006-02-15 07:22:58 +00002793 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002794 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002795 }
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002796 | INVOKE OptCallingConv ResultTypes ValueRef '(' ParamList ')' OptFuncAttrs
Chris Lattner58af2a12006-02-15 07:22:58 +00002797 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattner58af2a12006-02-15 07:22:58 +00002798
Reid Spencer14310612006-12-31 05:40:51 +00002799 // Handle the short syntax
2800 const PointerType *PFTy = 0;
2801 const FunctionType *Ty = 0;
Reid Spencer218ded22007-01-05 17:07:23 +00002802 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00002803 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2804 // Pull out the types of all of the arguments...
2805 std::vector<const Type*> ParamTypes;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002806 ParamList::iterator I = $6->begin(), E = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002807 for (; I != E; ++I) {
Reid Spencer14310612006-12-31 05:40:51 +00002808 const Type *Ty = I->Val->getType();
2809 if (Ty == Type::VoidTy)
2810 GEN_ERROR("Short call syntax cannot be used with varargs");
2811 ParamTypes.push_back(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002812 }
Chris Lattnera925a142008-04-23 05:37:08 +00002813
2814 if (!FunctionType::isValidReturnType(*$3))
2815 GEN_ERROR("Invalid result type for LLVM function");
2816
Duncan Sandsdc024672007-11-27 13:23:08 +00002817 Ty = FunctionType::get($3->get(), ParamTypes, false);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002818 PFTy = PointerType::getUnqual(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002819 }
2820
Reid Spencer66728ef2007-03-20 01:13:36 +00002821 delete $3;
2822
Chris Lattner58af2a12006-02-15 07:22:58 +00002823 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002824 CHECK_FOR_ERROR
Reid Spencer218ded22007-01-05 17:07:23 +00002825 BasicBlock *Normal = getBBVal($11);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002826 CHECK_FOR_ERROR
Reid Spencer218ded22007-01-05 17:07:23 +00002827 BasicBlock *Except = getBBVal($14);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002828 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002829
Chris Lattner58d74912008-03-12 17:45:29 +00002830 SmallVector<ParamAttrsWithIndex, 8> Attrs;
2831 if ($8 != ParamAttr::None)
2832 Attrs.push_back(ParamAttrsWithIndex::get(0, $8));
Duncan Sandsdc024672007-11-27 13:23:08 +00002833
Reid Spencer14310612006-12-31 05:40:51 +00002834 // Check the arguments
2835 ValueList Args;
2836 if ($6->empty()) { // Has no arguments?
2837 // Make sure no arguments is a good thing!
2838 if (Ty->getNumParams() != 0)
2839 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00002840 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00002841 } else { // Has arguments?
2842 // Loop through FunctionType's arguments and ensure they are specified
2843 // correctly!
Chris Lattner58af2a12006-02-15 07:22:58 +00002844 FunctionType::param_iterator I = Ty->param_begin();
2845 FunctionType::param_iterator E = Ty->param_end();
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002846 ParamList::iterator ArgI = $6->begin(), ArgE = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002847 unsigned index = 1;
Chris Lattner58af2a12006-02-15 07:22:58 +00002848
Duncan Sandsdc024672007-11-27 13:23:08 +00002849 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002850 if (ArgI->Val->getType() != *I)
2851 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00002852 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00002853 Args.push_back(ArgI->Val);
Chris Lattner58d74912008-03-12 17:45:29 +00002854 if (ArgI->Attrs != ParamAttr::None)
2855 Attrs.push_back(ParamAttrsWithIndex::get(index, ArgI->Attrs));
Reid Spencer14310612006-12-31 05:40:51 +00002856 }
Reid Spencera132e042006-12-03 05:46:11 +00002857
Reid Spencer14310612006-12-31 05:40:51 +00002858 if (Ty->isVarArg()) {
2859 if (I == E)
Chris Lattner38905612008-02-19 04:36:25 +00002860 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002861 Args.push_back(ArgI->Val); // push the remaining varargs
Chris Lattner58d74912008-03-12 17:45:29 +00002862 if (ArgI->Attrs != ParamAttr::None)
2863 Attrs.push_back(ParamAttrsWithIndex::get(index, ArgI->Attrs));
Chris Lattner38905612008-02-19 04:36:25 +00002864 }
Reid Spencer14310612006-12-31 05:40:51 +00002865 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00002866 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00002867 }
Reid Spencer14310612006-12-31 05:40:51 +00002868
Chris Lattner58d74912008-03-12 17:45:29 +00002869 PAListPtr PAL;
Duncan Sandsdc024672007-11-27 13:23:08 +00002870 if (!Attrs.empty())
Chris Lattner58d74912008-03-12 17:45:29 +00002871 PAL = PAListPtr::get(Attrs.begin(), Attrs.end());
Duncan Sandsdc024672007-11-27 13:23:08 +00002872
Reid Spencer14310612006-12-31 05:40:51 +00002873 // Create the InvokeInst
Dan Gohman041e2eb2008-05-15 19:50:34 +00002874 InvokeInst *II = InvokeInst::Create(V, Normal, Except,
2875 Args.begin(), Args.end());
Reid Spencer14310612006-12-31 05:40:51 +00002876 II->setCallingConv($2);
Duncan Sandsdc024672007-11-27 13:23:08 +00002877 II->setParamAttrs(PAL);
Reid Spencer14310612006-12-31 05:40:51 +00002878 $$ = II;
Chris Lattner58af2a12006-02-15 07:22:58 +00002879 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002880 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002881 }
2882 | UNWIND {
2883 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002884 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002885 }
2886 | UNREACHABLE {
2887 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002888 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002889 };
2890
2891
2892
2893JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2894 $$ = $1;
Reid Spencer93c40032007-03-19 18:40:50 +00002895 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002896 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002897 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002898 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00002899
Reid Spencer5b7e7532006-09-28 19:28:24 +00002900 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002901 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002902 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002903 }
2904 | IntType ConstValueRef ',' LABEL ValueRef {
2905 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencer93c40032007-03-19 18:40:50 +00002906 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002907 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002908
2909 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002910 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00002911
Reid Spencer5b7e7532006-09-28 19:28:24 +00002912 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002913 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002914 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002915 };
2916
Reid Spencer41dff5e2007-01-26 08:05:27 +00002917Inst : OptLocalAssign InstVal {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002918 // Is this definition named?? if so, assign the name...
2919 setValueName($2, $1);
2920 CHECK_FOR_ERROR
2921 InsertValue($2);
2922 $$ = $2;
2923 CHECK_FOR_ERROR
2924 };
2925
Chris Lattner15bd0952008-08-29 17:20:18 +00002926Inst : LocalNumber InstVal {
2927 CHECK_FOR_ERROR
2928 int ValNum = InsertValue($2);
2929
2930 if (ValNum != (int)$1)
2931 GEN_ERROR("Result value number %" + utostr($1) +
2932 " is incorrect, expected %" + utostr((unsigned)ValNum));
2933
2934 $$ = $2;
2935 CHECK_FOR_ERROR
2936 };
2937
Chris Lattner58af2a12006-02-15 07:22:58 +00002938
2939PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer14310612006-12-31 05:40:51 +00002940 if (!UpRefs.empty())
2941 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002942 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencera132e042006-12-03 05:46:11 +00002943 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002944 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002945 BasicBlock* tmpBB = getBBVal($5);
2946 CHECK_FOR_ERROR
2947 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencera132e042006-12-03 05:46:11 +00002948 delete $1;
Chris Lattner58af2a12006-02-15 07:22:58 +00002949 }
2950 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2951 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002952 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002953 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002954 BasicBlock* tmpBB = getBBVal($6);
2955 CHECK_FOR_ERROR
2956 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002957 };
2958
2959
Duncan Sandsdc024672007-11-27 13:23:08 +00002960ParamList : Types OptParamAttrs ValueRef OptParamAttrs {
2961 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Reid Spencer14310612006-12-31 05:40:51 +00002962 if (!UpRefs.empty())
2963 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2964 // Used for call and invoke instructions
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002965 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00002966 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getVal($1->get(), $3);
Reid Spencer14310612006-12-31 05:40:51 +00002967 $$->push_back(E);
Reid Spencer66728ef2007-03-20 01:13:36 +00002968 delete $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002969 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002970 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002971 | LABEL OptParamAttrs ValueRef OptParamAttrs {
2972 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002973 // Labels are only valid in ASMs
2974 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00002975 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getBBVal($3);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002976 $$->push_back(E);
Duncan Sandsdc024672007-11-27 13:23:08 +00002977 CHECK_FOR_ERROR
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002978 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002979 | ParamList ',' Types OptParamAttrs ValueRef OptParamAttrs {
2980 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Reid Spencer14310612006-12-31 05:40:51 +00002981 if (!UpRefs.empty())
2982 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002983 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002984 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getVal($3->get(), $5);
Reid Spencer14310612006-12-31 05:40:51 +00002985 $$->push_back(E);
Reid Spencer66728ef2007-03-20 01:13:36 +00002986 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002987 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00002988 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002989 | ParamList ',' LABEL OptParamAttrs ValueRef OptParamAttrs {
2990 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002991 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002992 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getBBVal($5);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002993 $$->push_back(E);
2994 CHECK_FOR_ERROR
2995 }
2996 | /*empty*/ { $$ = new ParamList(); };
Chris Lattner58af2a12006-02-15 07:22:58 +00002997
Reid Spencer14310612006-12-31 05:40:51 +00002998IndexList // Used for gep instructions and constant expressions
Reid Spencerc6c59fd2006-12-31 21:47:02 +00002999 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencer14310612006-12-31 05:40:51 +00003000 | IndexList ',' ResolvedVal {
3001 $$ = $1;
3002 $$->push_back($3);
3003 CHECK_FOR_ERROR
3004 }
Reid Spencerc6c59fd2006-12-31 21:47:02 +00003005 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00003006
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003007ConstantIndexList // Used for insertvalue and extractvalue instructions
3008 : ',' EUINT64VAL {
3009 $$ = new std::vector<unsigned>();
3010 if ((unsigned)$2 != $2)
3011 GEN_ERROR("Index " + utostr($2) + " is not valid for insertvalue or extractvalue.");
3012 $$->push_back($2);
3013 }
3014 | ConstantIndexList ',' EUINT64VAL {
3015 $$ = $1;
3016 if ((unsigned)$3 != $3)
3017 GEN_ERROR("Index " + utostr($3) + " is not valid for insertvalue or extractvalue.");
3018 $$->push_back($3);
3019 CHECK_FOR_ERROR
3020 }
3021 ;
3022
Chris Lattner58af2a12006-02-15 07:22:58 +00003023OptTailCall : TAIL CALL {
3024 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00003025 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003026 }
3027 | CALL {
3028 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00003029 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003030 };
3031
Chris Lattner58af2a12006-02-15 07:22:58 +00003032InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00003033 if (!UpRefs.empty())
3034 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00003035 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencer9d6565a2007-02-15 02:26:10 +00003036 !isa<VectorType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003037 GEN_ERROR(
Reid Spencerb5334b02007-02-05 10:18:06 +00003038 "Arithmetic operator requires integer, FP, or packed operands");
Reid Spencera132e042006-12-03 05:46:11 +00003039 Value* val1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003040 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003041 Value* val2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003042 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003043 $$ = BinaryOperator::Create($1, val1, val2);
Chris Lattner58af2a12006-02-15 07:22:58 +00003044 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003045 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00003046 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003047 }
3048 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00003049 if (!UpRefs.empty())
3050 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00003051 if (!(*$2)->isInteger()) {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003052 if (!isa<VectorType>($2->get()) ||
Reid Spencer9d6565a2007-02-15 02:26:10 +00003053 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00003054 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00003055 }
Reid Spencera132e042006-12-03 05:46:11 +00003056 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003057 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003058 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003059 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003060 $$ = BinaryOperator::Create($1, tmpVal1, tmpVal2);
Chris Lattner58af2a12006-02-15 07:22:58 +00003061 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003062 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00003063 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003064 }
Reid Spencera132e042006-12-03 05:46:11 +00003065 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00003066 if (!UpRefs.empty())
3067 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00003068 if (isa<VectorType>((*$3).get()))
Chris Lattner32980692007-02-19 07:44:24 +00003069 GEN_ERROR("Vector types not supported by icmp instruction");
Reid Spencera132e042006-12-03 05:46:11 +00003070 Value* tmpVal1 = getVal(*$3, $4);
3071 CHECK_FOR_ERROR
3072 Value* tmpVal2 = getVal(*$3, $6);
3073 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003074 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Reid Spencera132e042006-12-03 05:46:11 +00003075 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003076 GEN_ERROR("icmp operator returned null");
Reid Spencer66728ef2007-03-20 01:13:36 +00003077 delete $3;
Reid Spencera132e042006-12-03 05:46:11 +00003078 }
3079 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00003080 if (!UpRefs.empty())
3081 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00003082 if (isa<VectorType>((*$3).get()))
Chris Lattner32980692007-02-19 07:44:24 +00003083 GEN_ERROR("Vector types not supported by fcmp instruction");
Reid Spencera132e042006-12-03 05:46:11 +00003084 Value* tmpVal1 = getVal(*$3, $4);
3085 CHECK_FOR_ERROR
3086 Value* tmpVal2 = getVal(*$3, $6);
3087 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003088 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Reid Spencera132e042006-12-03 05:46:11 +00003089 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003090 GEN_ERROR("fcmp operator returned null");
Reid Spencer66728ef2007-03-20 01:13:36 +00003091 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00003092 }
Nate Begemanac80ade2008-05-12 19:01:56 +00003093 | VICMP IPredicates Types ValueRef ',' ValueRef {
3094 if (!UpRefs.empty())
3095 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
3096 if (!isa<VectorType>((*$3).get()))
3097 GEN_ERROR("Scalar types not supported by vicmp instruction");
3098 Value* tmpVal1 = getVal(*$3, $4);
3099 CHECK_FOR_ERROR
3100 Value* tmpVal2 = getVal(*$3, $6);
3101 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003102 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Nate Begemanac80ade2008-05-12 19:01:56 +00003103 if ($$ == 0)
3104 GEN_ERROR("icmp operator returned null");
3105 delete $3;
3106 }
3107 | VFCMP FPredicates Types ValueRef ',' ValueRef {
3108 if (!UpRefs.empty())
3109 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
3110 if (!isa<VectorType>((*$3).get()))
3111 GEN_ERROR("Scalar types not supported by vfcmp instruction");
3112 Value* tmpVal1 = getVal(*$3, $4);
3113 CHECK_FOR_ERROR
3114 Value* tmpVal2 = getVal(*$3, $6);
3115 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003116 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Nate Begemanac80ade2008-05-12 19:01:56 +00003117 if ($$ == 0)
3118 GEN_ERROR("fcmp operator returned null");
3119 delete $3;
3120 }
Reid Spencer3da59db2006-11-27 01:05:10 +00003121 | CastOps ResolvedVal TO Types {
Reid Spencer14310612006-12-31 05:40:51 +00003122 if (!UpRefs.empty())
3123 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003124 Value* Val = $2;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00003125 const Type* DestTy = $4->get();
3126 if (!CastInst::castIsValid($1, Val, DestTy))
3127 GEN_ERROR("invalid cast opcode for cast from '" +
3128 Val->getType()->getDescription() + "' to '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00003129 DestTy->getDescription() + "'");
Dan Gohmane4977cf2008-05-23 01:55:30 +00003130 $$ = CastInst::Create($1, Val, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00003131 delete $4;
Chris Lattner58af2a12006-02-15 07:22:58 +00003132 }
3133 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer4fe16d62007-01-11 18:21:29 +00003134 if ($2->getType() != Type::Int1Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00003135 GEN_ERROR("select condition must be boolean");
Reid Spencera132e042006-12-03 05:46:11 +00003136 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00003137 GEN_ERROR("select value types should match");
Gabor Greife64d2482008-04-06 23:07:54 +00003138 $$ = SelectInst::Create($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003139 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003140 }
3141 | VAARG ResolvedVal ',' Types {
Reid Spencer14310612006-12-31 05:40:51 +00003142 if (!UpRefs.empty())
3143 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003144 $$ = new VAArgInst($2, *$4);
3145 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00003146 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003147 }
Chris Lattner58af2a12006-02-15 07:22:58 +00003148 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003149 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerb5334b02007-02-05 10:18:06 +00003150 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00003151 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003152 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003153 }
3154 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003155 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00003156 GEN_ERROR("Invalid insertelement operands");
Gabor Greife64d2482008-04-06 23:07:54 +00003157 $$ = InsertElementInst::Create($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003158 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003159 }
Chris Lattnerd5efe842006-04-08 01:18:56 +00003160 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003161 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00003162 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00003163 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003164 CHECK_FOR_ERROR
Chris Lattnerd5efe842006-04-08 01:18:56 +00003165 }
Chris Lattner58af2a12006-02-15 07:22:58 +00003166 | PHI_TOK PHIList {
3167 const Type *Ty = $2->front().first->getType();
3168 if (!Ty->isFirstClassType())
Reid Spencerb5334b02007-02-05 10:18:06 +00003169 GEN_ERROR("PHI node operands must be of first class type");
Gabor Greife64d2482008-04-06 23:07:54 +00003170 $$ = PHINode::Create(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00003171 ((PHINode*)$$)->reserveOperandSpace($2->size());
3172 while ($2->begin() != $2->end()) {
3173 if ($2->front().first->getType() != Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00003174 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattner58af2a12006-02-15 07:22:58 +00003175 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
3176 $2->pop_front();
3177 }
3178 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00003179 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003180 }
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003181 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ParamList ')'
Reid Spencer218ded22007-01-05 17:07:23 +00003182 OptFuncAttrs {
Reid Spencer14310612006-12-31 05:40:51 +00003183
3184 // Handle the short syntax
Reid Spencer3da59db2006-11-27 01:05:10 +00003185 const PointerType *PFTy = 0;
3186 const FunctionType *Ty = 0;
Reid Spencer218ded22007-01-05 17:07:23 +00003187 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00003188 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3189 // Pull out the types of all of the arguments...
3190 std::vector<const Type*> ParamTypes;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003191 ParamList::iterator I = $6->begin(), E = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00003192 for (; I != E; ++I) {
Reid Spencer14310612006-12-31 05:40:51 +00003193 const Type *Ty = I->Val->getType();
3194 if (Ty == Type::VoidTy)
3195 GEN_ERROR("Short call syntax cannot be used with varargs");
3196 ParamTypes.push_back(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00003197 }
Chris Lattnera925a142008-04-23 05:37:08 +00003198
3199 if (!FunctionType::isValidReturnType(*$3))
3200 GEN_ERROR("Invalid result type for LLVM function");
3201
Duncan Sandsdc024672007-11-27 13:23:08 +00003202 Ty = FunctionType::get($3->get(), ParamTypes, false);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00003203 PFTy = PointerType::getUnqual(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00003204 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00003205
Chris Lattner58af2a12006-02-15 07:22:58 +00003206 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00003207 CHECK_FOR_ERROR
Chris Lattner6cdc6822007-04-26 05:31:05 +00003208
Reid Spencer7780acb2007-04-16 06:56:07 +00003209 // Check for call to invalid intrinsic to avoid crashing later.
3210 if (Function *theF = dyn_cast<Function>(V)) {
Reid Spencered48de22007-04-16 22:02:23 +00003211 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
Reid Spencer36fdde12007-04-16 20:35:38 +00003212 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
3213 !theF->getIntrinsicID(true))
Reid Spencer7780acb2007-04-16 06:56:07 +00003214 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
3215 theF->getName() + "'");
3216 }
3217
Duncan Sandsdc024672007-11-27 13:23:08 +00003218 // Set up the ParamAttrs for the function
Chris Lattner58d74912008-03-12 17:45:29 +00003219 SmallVector<ParamAttrsWithIndex, 8> Attrs;
3220 if ($8 != ParamAttr::None)
3221 Attrs.push_back(ParamAttrsWithIndex::get(0, $8));
Reid Spencer14310612006-12-31 05:40:51 +00003222 // Check the arguments
3223 ValueList Args;
3224 if ($6->empty()) { // Has no arguments?
Chris Lattner58af2a12006-02-15 07:22:58 +00003225 // Make sure no arguments is a good thing!
3226 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00003227 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00003228 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00003229 } else { // Has arguments?
3230 // Loop through FunctionType's arguments and ensure they are specified
Duncan Sandsdc024672007-11-27 13:23:08 +00003231 // correctly. Also, gather any parameter attributes.
Chris Lattner58af2a12006-02-15 07:22:58 +00003232 FunctionType::param_iterator I = Ty->param_begin();
3233 FunctionType::param_iterator E = Ty->param_end();
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003234 ParamList::iterator ArgI = $6->begin(), ArgE = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00003235 unsigned index = 1;
Chris Lattner58af2a12006-02-15 07:22:58 +00003236
Duncan Sandsdc024672007-11-27 13:23:08 +00003237 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00003238 if (ArgI->Val->getType() != *I)
3239 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00003240 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00003241 Args.push_back(ArgI->Val);
Chris Lattner58d74912008-03-12 17:45:29 +00003242 if (ArgI->Attrs != ParamAttr::None)
3243 Attrs.push_back(ParamAttrsWithIndex::get(index, ArgI->Attrs));
Reid Spencer14310612006-12-31 05:40:51 +00003244 }
3245 if (Ty->isVarArg()) {
3246 if (I == E)
Chris Lattner38905612008-02-19 04:36:25 +00003247 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00003248 Args.push_back(ArgI->Val); // push the remaining varargs
Chris Lattner58d74912008-03-12 17:45:29 +00003249 if (ArgI->Attrs != ParamAttr::None)
3250 Attrs.push_back(ParamAttrsWithIndex::get(index, ArgI->Attrs));
Chris Lattner38905612008-02-19 04:36:25 +00003251 }
Reid Spencer14310612006-12-31 05:40:51 +00003252 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00003253 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00003254 }
Duncan Sandsdc024672007-11-27 13:23:08 +00003255
3256 // Finish off the ParamAttrs and check them
Chris Lattner58d74912008-03-12 17:45:29 +00003257 PAListPtr PAL;
Duncan Sandsdc024672007-11-27 13:23:08 +00003258 if (!Attrs.empty())
Chris Lattner58d74912008-03-12 17:45:29 +00003259 PAL = PAListPtr::get(Attrs.begin(), Attrs.end());
Duncan Sandsdc024672007-11-27 13:23:08 +00003260
Reid Spencer14310612006-12-31 05:40:51 +00003261 // Create the call node
Gabor Greife64d2482008-04-06 23:07:54 +00003262 CallInst *CI = CallInst::Create(V, Args.begin(), Args.end());
Reid Spencer14310612006-12-31 05:40:51 +00003263 CI->setTailCall($1);
3264 CI->setCallingConv($2);
Duncan Sandsdc024672007-11-27 13:23:08 +00003265 CI->setParamAttrs(PAL);
Reid Spencer14310612006-12-31 05:40:51 +00003266 $$ = CI;
Chris Lattner58af2a12006-02-15 07:22:58 +00003267 delete $6;
Reid Spencer41dff5e2007-01-26 08:05:27 +00003268 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00003269 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003270 }
3271 | MemoryInst {
3272 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00003273 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003274 };
3275
Chris Lattner58af2a12006-02-15 07:22:58 +00003276OptVolatile : VOLATILE {
3277 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00003278 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003279 }
3280 | /* empty */ {
3281 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00003282 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003283 };
3284
3285
3286
3287MemoryInst : MALLOC Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003288 if (!UpRefs.empty())
3289 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003290 $$ = new MallocInst(*$2, 0, $3);
3291 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003292 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003293 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003294 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003295 if (!UpRefs.empty())
3296 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003297 if ($4 != Type::Int32Ty)
3298 GEN_ERROR("Malloc array size is not a 32-bit integer!");
Reid Spencera132e042006-12-03 05:46:11 +00003299 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003300 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003301 $$ = new MallocInst(*$2, tmpVal, $6);
3302 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003303 }
3304 | ALLOCA Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003305 if (!UpRefs.empty())
3306 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003307 $$ = new AllocaInst(*$2, 0, $3);
3308 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003309 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003310 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003311 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003312 if (!UpRefs.empty())
3313 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003314 if ($4 != Type::Int32Ty)
3315 GEN_ERROR("Alloca array size is not a 32-bit integer!");
Reid Spencera132e042006-12-03 05:46:11 +00003316 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003317 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003318 $$ = new AllocaInst(*$2, tmpVal, $6);
3319 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003320 }
3321 | FREE ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003322 if (!isa<PointerType>($2->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003323 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerb5334b02007-02-05 10:18:06 +00003324 $2->getType()->getDescription() + "");
Reid Spencera132e042006-12-03 05:46:11 +00003325 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00003326 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003327 }
3328
Christopher Lamb5c104242007-04-22 20:09:11 +00003329 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003330 if (!UpRefs.empty())
3331 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003332 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003333 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003334 (*$3)->getDescription());
3335 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00003336 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003337 (*$3)->getDescription());
3338 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003339 CHECK_FOR_ERROR
Christopher Lamb5c104242007-04-22 20:09:11 +00003340 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencera132e042006-12-03 05:46:11 +00003341 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00003342 }
Christopher Lamb5c104242007-04-22 20:09:11 +00003343 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003344 if (!UpRefs.empty())
3345 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003346 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00003347 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00003348 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003349 (*$5)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00003350 const Type *ElTy = PT->getElementType();
Reid Spencera132e042006-12-03 05:46:11 +00003351 if (ElTy != $3->getType())
3352 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerb5334b02007-02-05 10:18:06 +00003353 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00003354
Reid Spencera132e042006-12-03 05:46:11 +00003355 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003356 CHECK_FOR_ERROR
Christopher Lamb5c104242007-04-22 20:09:11 +00003357 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencera132e042006-12-03 05:46:11 +00003358 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00003359 }
Dan Gohmane4977cf2008-05-23 01:55:30 +00003360 | GETRESULT Types ValueRef ',' EUINT64VAL {
Dan Gohman1a570242008-07-23 00:54:54 +00003361 if (!UpRefs.empty())
3362 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3363 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3364 GEN_ERROR("getresult insn requires an aggregate operand");
3365 if (!ExtractValueInst::getIndexedType(*$2, $5))
3366 GEN_ERROR("Invalid getresult index for type '" +
3367 (*$2)->getDescription()+ "'");
3368
3369 Value *tmpVal = getVal(*$2, $3);
Devang Patel5a970972008-02-19 22:27:01 +00003370 CHECK_FOR_ERROR
Dan Gohman1a570242008-07-23 00:54:54 +00003371 $$ = ExtractValueInst::Create(tmpVal, $5);
3372 delete $2;
Devang Patel5a970972008-02-19 22:27:01 +00003373 }
Chris Lattner58af2a12006-02-15 07:22:58 +00003374 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer14310612006-12-31 05:40:51 +00003375 if (!UpRefs.empty())
3376 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003377 if (!isa<PointerType>($2->get()))
Reid Spencerb5334b02007-02-05 10:18:06 +00003378 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00003379
Dan Gohman041e2eb2008-05-15 19:50:34 +00003380 if (!GetElementPtrInst::getIndexedType(*$2, $4->begin(), $4->end()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003381 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00003382 (*$2)->getDescription()+ "'");
Reid Spencera132e042006-12-03 05:46:11 +00003383 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003384 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00003385 $$ = GetElementPtrInst::Create(tmpVal, $4->begin(), $4->end());
Reid Spencera132e042006-12-03 05:46:11 +00003386 delete $2;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003387 delete $4;
Dan Gohmane4977cf2008-05-23 01:55:30 +00003388 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003389 | EXTRACTVALUE Types ValueRef ConstantIndexList {
Dan Gohmane4977cf2008-05-23 01:55:30 +00003390 if (!UpRefs.empty())
3391 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3392 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3393 GEN_ERROR("extractvalue insn requires an aggregate operand");
3394
3395 if (!ExtractValueInst::getIndexedType(*$2, $4->begin(), $4->end()))
3396 GEN_ERROR("Invalid extractvalue indices for type '" +
3397 (*$2)->getDescription()+ "'");
3398 Value* tmpVal = getVal(*$2, $3);
3399 CHECK_FOR_ERROR
3400 $$ = ExtractValueInst::Create(tmpVal, $4->begin(), $4->end());
3401 delete $2;
3402 delete $4;
3403 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003404 | INSERTVALUE Types ValueRef ',' Types ValueRef ConstantIndexList {
Dan Gohmane4977cf2008-05-23 01:55:30 +00003405 if (!UpRefs.empty())
3406 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3407 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3408 GEN_ERROR("extractvalue insn requires an aggregate operand");
3409
3410 if (ExtractValueInst::getIndexedType(*$2, $7->begin(), $7->end()) != $5->get())
3411 GEN_ERROR("Invalid insertvalue indices for type '" +
3412 (*$2)->getDescription()+ "'");
3413 Value* aggVal = getVal(*$2, $3);
3414 Value* tmpVal = getVal(*$5, $6);
3415 CHECK_FOR_ERROR
3416 $$ = InsertValueInst::Create(aggVal, tmpVal, $7->begin(), $7->end());
3417 delete $2;
3418 delete $5;
3419 delete $7;
Chris Lattner58af2a12006-02-15 07:22:58 +00003420 };
3421
3422
3423%%
Reid Spencer61c83e02006-08-18 08:43:06 +00003424
Reid Spencer14310612006-12-31 05:40:51 +00003425// common code from the two 'RunVMAsmParser' functions
3426static Module* RunParser(Module * M) {
Reid Spencer14310612006-12-31 05:40:51 +00003427 CurModule.CurrentModule = M;
Reid Spencer14310612006-12-31 05:40:51 +00003428 // Check to make sure the parser succeeded
3429 if (yyparse()) {
3430 if (ParserResult)
3431 delete ParserResult;
3432 return 0;
3433 }
3434
Reid Spencer0d60b5a2007-03-30 01:37:39 +00003435 // Emit an error if there are any unresolved types left.
3436 if (!CurModule.LateResolveTypes.empty()) {
3437 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3438 if (DID.Type == ValID::LocalName) {
3439 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3440 } else {
3441 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3442 }
3443 if (ParserResult)
3444 delete ParserResult;
3445 return 0;
3446 }
3447
3448 // Emit an error if there are any unresolved values left.
3449 if (!CurModule.LateResolveValues.empty()) {
3450 Value *V = CurModule.LateResolveValues.back();
3451 std::map<Value*, std::pair<ValID, int> >::iterator I =
3452 CurModule.PlaceHolderInfo.find(V);
3453
3454 if (I != CurModule.PlaceHolderInfo.end()) {
3455 ValID &DID = I->second.first;
3456 if (DID.Type == ValID::LocalName) {
3457 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3458 } else {
3459 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3460 }
3461 if (ParserResult)
3462 delete ParserResult;
3463 return 0;
3464 }
3465 }
3466
Reid Spencer14310612006-12-31 05:40:51 +00003467 // Check to make sure that parsing produced a result
3468 if (!ParserResult)
3469 return 0;
3470
3471 // Reset ParserResult variable while saving its value for the result.
3472 Module *Result = ParserResult;
3473 ParserResult = 0;
3474
3475 return Result;
3476}
3477
Reid Spencer61c83e02006-08-18 08:43:06 +00003478void llvm::GenerateError(const std::string &message, int LineNo) {
Duncan Sandsdc024672007-11-27 13:23:08 +00003479 if (LineNo == -1) LineNo = LLLgetLineNo();
Reid Spencer61c83e02006-08-18 08:43:06 +00003480 // TODO: column number in exception
3481 if (TheParseError)
Duncan Sandsdc024672007-11-27 13:23:08 +00003482 TheParseError->setError(LLLgetFilename(), message, LineNo);
Reid Spencer61c83e02006-08-18 08:43:06 +00003483 TriggerError = 1;
3484}
3485
Chris Lattner58af2a12006-02-15 07:22:58 +00003486int yyerror(const char *ErrorMsg) {
Duncan Sandsdc024672007-11-27 13:23:08 +00003487 std::string where = LLLgetFilename() + ":" + utostr(LLLgetLineNo()) + ": ";
Reid Spenceref9b9a72007-02-05 20:47:22 +00003488 std::string errMsg = where + "error: " + std::string(ErrorMsg);
Duncan Sandsdc024672007-11-27 13:23:08 +00003489 if (yychar != YYEMPTY && yychar != 0) {
3490 errMsg += " while reading token: '";
3491 errMsg += std::string(LLLgetTokenStart(),
3492 LLLgetTokenStart()+LLLgetTokenLength()) + "'";
3493 }
Reid Spencer61c83e02006-08-18 08:43:06 +00003494 GenerateError(errMsg);
Chris Lattner58af2a12006-02-15 07:22:58 +00003495 return 0;
3496}