blob: 7028ea3fe0d2564a69ced084bc41219a5da50e98 [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
Eric Christopher2a5196f2008-09-24 04:55:49 +000036// 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
Reid Spencere4f47592006-08-18 17:32:55 +000039// 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
Eric Christopher2a5196f2008-09-24 04:55:49 +000042// immediately invokes YYERROR. This would be so much cleaner if it was a
Reid Spencere4f47592006-08-18 17:32:55 +000043// 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
Eric Christopher2a5196f2008-09-24 04:55:49 +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;
Nuno Lopes8a5f3472008-10-15 12:05:02 +0000143 I->first.second.destroy();
Chris Lattner58af2a12006-02-15 07:22:58 +0000144 GlobalRefs.erase(I);
145 }
146 return Ret;
147 }
Reid Spencer8c8a2dc2007-01-02 21:54:12 +0000148
149 bool TypeIsUnresolved(PATypeHolder* PATy) {
150 // If it isn't abstract, its resolved
151 const Type* Ty = PATy->get();
152 if (!Ty->isAbstract())
153 return false;
154 // Traverse the type looking for abstract types. If it isn't abstract then
Eric Christopher2a5196f2008-09-24 04:55:49 +0000155 // we don't need to traverse that leg of the type.
Reid Spencer8c8a2dc2007-01-02 21:54:12 +0000156 std::vector<const Type*> WorkList, SeenList;
157 WorkList.push_back(Ty);
158 while (!WorkList.empty()) {
159 const Type* Ty = WorkList.back();
160 SeenList.push_back(Ty);
161 WorkList.pop_back();
162 if (const OpaqueType* OpTy = dyn_cast<OpaqueType>(Ty)) {
163 // Check to see if this is an unresolved type
164 std::map<ValID, PATypeHolder>::iterator I = LateResolveTypes.begin();
165 std::map<ValID, PATypeHolder>::iterator E = LateResolveTypes.end();
166 for ( ; I != E; ++I) {
167 if (I->second.get() == OpTy)
168 return true;
169 }
170 } else if (const SequentialType* SeqTy = dyn_cast<SequentialType>(Ty)) {
171 const Type* TheTy = SeqTy->getElementType();
172 if (TheTy->isAbstract() && TheTy != Ty) {
Eric Christopher2a5196f2008-09-24 04:55:49 +0000173 std::vector<const Type*>::iterator I = SeenList.begin(),
Reid Spencer8c8a2dc2007-01-02 21:54:12 +0000174 E = SeenList.end();
175 for ( ; I != E; ++I)
176 if (*I == TheTy)
177 break;
178 if (I == E)
179 WorkList.push_back(TheTy);
180 }
181 } else if (const StructType* StrTy = dyn_cast<StructType>(Ty)) {
182 for (unsigned i = 0; i < StrTy->getNumElements(); ++i) {
183 const Type* TheTy = StrTy->getElementType(i);
184 if (TheTy->isAbstract() && TheTy != Ty) {
Eric Christopher2a5196f2008-09-24 04:55:49 +0000185 std::vector<const Type*>::iterator I = SeenList.begin(),
Reid Spencer8c8a2dc2007-01-02 21:54:12 +0000186 E = SeenList.end();
187 for ( ; I != E; ++I)
188 if (*I == TheTy)
189 break;
190 if (I == E)
191 WorkList.push_back(TheTy);
192 }
193 }
194 }
195 }
196 return false;
197 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000198} CurModule;
199
200static struct PerFunctionInfo {
201 Function *CurrentFunction; // Pointer to current function being created
202
Reid Spencer93c40032007-03-19 18:40:50 +0000203 ValueList Values; // Keep track of #'d definitions
204 unsigned NextValNum;
205 ValueList LateResolveValues;
Reid Spenceref9b9a72007-02-05 20:47:22 +0000206 bool isDeclare; // Is this function a forward declararation?
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000207 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000208 GlobalValue::VisibilityTypes Visibility;
Chris Lattner58af2a12006-02-15 07:22:58 +0000209
210 /// BBForwardRefs - When we see forward references to basic blocks, keep
211 /// track of them here.
Reid Spencer93c40032007-03-19 18:40:50 +0000212 std::map<ValID, BasicBlock*> BBForwardRefs;
Chris Lattner58af2a12006-02-15 07:22:58 +0000213
214 inline PerFunctionInfo() {
215 CurrentFunction = 0;
216 isDeclare = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000217 Linkage = GlobalValue::ExternalLinkage;
218 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner58af2a12006-02-15 07:22:58 +0000219 }
220
221 inline void FunctionStart(Function *M) {
222 CurrentFunction = M;
Reid Spencer93c40032007-03-19 18:40:50 +0000223 NextValNum = 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000224 }
225
226 void FunctionDone() {
Chris Lattner58af2a12006-02-15 07:22:58 +0000227 // Any forward referenced blocks left?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000228 if (!BBForwardRefs.empty()) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000229 GenerateError("Undefined reference to label " +
Reid Spencer93c40032007-03-19 18:40:50 +0000230 BBForwardRefs.begin()->second->getName());
Reid Spencer5b7e7532006-09-28 19:28:24 +0000231 return;
232 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000233
234 // Resolve all forward references now.
235 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
236
237 Values.clear(); // Clear out function local definitions
Reid Spencer93c40032007-03-19 18:40:50 +0000238 BBForwardRefs.clear();
Chris Lattner58af2a12006-02-15 07:22:58 +0000239 CurrentFunction = 0;
240 isDeclare = false;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000241 Linkage = GlobalValue::ExternalLinkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000242 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner58af2a12006-02-15 07:22:58 +0000243 }
244} CurFun; // Info for the current function...
245
246static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
247
248
249//===----------------------------------------------------------------------===//
250// Code to handle definitions of all the types
251//===----------------------------------------------------------------------===//
252
Chris Lattner15bd0952008-08-29 17:20:18 +0000253/// InsertValue - Insert a value into the value table. If it is named, this
254/// returns -1, otherwise it returns the slot number for the value.
255static int InsertValue(Value *V, ValueList &ValueTab = CurFun.Values) {
Reid Spencer93c40032007-03-19 18:40:50 +0000256 // Things that have names or are void typed don't get slot numbers
257 if (V->hasName() || (V->getType() == Type::VoidTy))
Chris Lattner15bd0952008-08-29 17:20:18 +0000258 return -1;
Chris Lattner58af2a12006-02-15 07:22:58 +0000259
Reid Spencer93c40032007-03-19 18:40:50 +0000260 // In the case of function values, we have to allow for the forward reference
261 // of basic blocks, which are included in the numbering. Consequently, we keep
Eric Christopher2a5196f2008-09-24 04:55:49 +0000262 // track of the next insertion location with NextValNum. When a BB gets
Reid Spencer93c40032007-03-19 18:40:50 +0000263 // inserted, it could change the size of the CurFun.Values vector.
264 if (&ValueTab == &CurFun.Values) {
265 if (ValueTab.size() <= CurFun.NextValNum)
266 ValueTab.resize(CurFun.NextValNum+1);
267 ValueTab[CurFun.NextValNum++] = V;
Chris Lattner15bd0952008-08-29 17:20:18 +0000268 return CurFun.NextValNum-1;
Eric Christopher2a5196f2008-09-24 04:55:49 +0000269 }
Reid Spencer93c40032007-03-19 18:40:50 +0000270 // For all other lists, its okay to just tack it on the back of the vector.
271 ValueTab.push_back(V);
Chris Lattner15bd0952008-08-29 17:20:18 +0000272 return ValueTab.size()-1;
Chris Lattner58af2a12006-02-15 07:22:58 +0000273}
274
275static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
276 switch (D.Type) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000277 case ValID::LocalID: // Is it a numbered definition?
Chris Lattner58af2a12006-02-15 07:22:58 +0000278 // Module constants occupy the lowest numbered slots...
Reid Spencer41dff5e2007-01-26 08:05:27 +0000279 if (D.Num < CurModule.Types.size())
280 return CurModule.Types[D.Num];
Chris Lattner58af2a12006-02-15 07:22:58 +0000281 break;
Reid Spencer41dff5e2007-01-26 08:05:27 +0000282 case ValID::LocalName: // Is it a named definition?
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000283 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.getName())) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000284 D.destroy(); // Free old strdup'd memory...
285 return N;
286 }
287 break;
288 default:
Reid Spencerb5334b02007-02-05 10:18:06 +0000289 GenerateError("Internal parser error: Invalid symbol type reference");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000290 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000291 }
292
293 // If we reached here, we referenced either a symbol that we don't know about
294 // or an id number that hasn't been read yet. We may be referencing something
295 // forward, so just create an entry to be resolved later and get to it...
296 //
297 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
298
299
300 if (inFunctionScope()) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000301 if (D.Type == ValID::LocalName) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000302 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000303 return 0;
304 } else {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000305 GenerateError("Reference to an undefined type: #" + utostr(D.Num));
Reid Spencer5b7e7532006-09-28 19:28:24 +0000306 return 0;
307 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000308 }
309
Reid Spencer861d9d62006-11-28 07:29:44 +0000310 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Nuno Lopesf05ff662008-10-15 11:20:21 +0000311 if (I != CurModule.LateResolveTypes.end()) {
312 D.destroy();
Reid Spencer861d9d62006-11-28 07:29:44 +0000313 return I->second;
Nuno Lopesf05ff662008-10-15 11:20:21 +0000314 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000315
Reid Spencer861d9d62006-11-28 07:29:44 +0000316 Type *Typ = OpaqueType::get();
317 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
318 return Typ;
Reid Spencera132e042006-12-03 05:46:11 +0000319 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000320
Reid Spencer93c40032007-03-19 18:40:50 +0000321// getExistingVal - Look up the value specified by the provided type and
Chris Lattner58af2a12006-02-15 07:22:58 +0000322// the provided ValID. If the value exists and has already been defined, return
323// it. Otherwise return null.
324//
Reid Spencer93c40032007-03-19 18:40:50 +0000325static Value *getExistingVal(const Type *Ty, const ValID &D) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000326 if (isa<FunctionType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000327 GenerateError("Functions are not values and "
Chris Lattner58af2a12006-02-15 07:22:58 +0000328 "must be referenced as pointers");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000329 return 0;
330 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000331
332 switch (D.Type) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000333 case ValID::LocalID: { // Is it a numbered definition?
Reid Spencer41dff5e2007-01-26 08:05:27 +0000334 // Check that the number is within bounds.
Eric Christopher2a5196f2008-09-24 04:55:49 +0000335 if (D.Num >= CurFun.Values.size())
Reid Spencer93c40032007-03-19 18:40:50 +0000336 return 0;
337 Value *Result = CurFun.Values[D.Num];
338 if (Ty != Result->getType()) {
339 GenerateError("Numbered value (%" + utostr(D.Num) + ") of type '" +
Eric Christopher2a5196f2008-09-24 04:55:49 +0000340 Result->getType()->getDescription() + "' does not match "
Reid Spencer93c40032007-03-19 18:40:50 +0000341 "expected type, '" + Ty->getDescription() + "'");
342 return 0;
343 }
344 return Result;
Reid Spencer41dff5e2007-01-26 08:05:27 +0000345 }
346 case ValID::GlobalID: { // Is it a numbered definition?
Eric Christopher2a5196f2008-09-24 04:55:49 +0000347 if (D.Num >= CurModule.Values.size())
Reid Spenceref9b9a72007-02-05 20:47:22 +0000348 return 0;
Reid Spencer93c40032007-03-19 18:40:50 +0000349 Value *Result = CurModule.Values[D.Num];
350 if (Ty != Result->getType()) {
351 GenerateError("Numbered value (@" + utostr(D.Num) + ") of type '" +
Eric Christopher2a5196f2008-09-24 04:55:49 +0000352 Result->getType()->getDescription() + "' does not match "
Reid Spencer93c40032007-03-19 18:40:50 +0000353 "expected type, '" + Ty->getDescription() + "'");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000354 return 0;
Reid Spencer93c40032007-03-19 18:40:50 +0000355 }
356 return Result;
Chris Lattner58af2a12006-02-15 07:22:58 +0000357 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000358
Reid Spencer41dff5e2007-01-26 08:05:27 +0000359 case ValID::LocalName: { // Is it a named definition?
Eric Christopher2a5196f2008-09-24 04:55:49 +0000360 if (!inFunctionScope())
Reid Spenceref9b9a72007-02-05 20:47:22 +0000361 return 0;
362 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000363 Value *N = SymTab.lookup(D.getName());
Eric Christopher2a5196f2008-09-24 04:55:49 +0000364 if (N == 0)
Reid Spenceref9b9a72007-02-05 20:47:22 +0000365 return 0;
366 if (N->getType() != Ty)
367 return 0;
Eric Christopher2a5196f2008-09-24 04:55:49 +0000368
Reid Spencer41dff5e2007-01-26 08:05:27 +0000369 D.destroy(); // Free old strdup'd memory...
370 return N;
371 }
372 case ValID::GlobalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000373 ValueSymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable();
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000374 Value *N = SymTab.lookup(D.getName());
Eric Christopher2a5196f2008-09-24 04:55:49 +0000375 if (N == 0)
Reid Spenceref9b9a72007-02-05 20:47:22 +0000376 return 0;
377 if (N->getType() != Ty)
378 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000379
380 D.destroy(); // Free old strdup'd memory...
381 return N;
382 }
383
384 // Check to make sure that "Ty" is an integral type, and that our
385 // value will fit into the specified type...
386 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattner38905612008-02-19 04:36:25 +0000387 if (!isa<IntegerType>(Ty) ||
388 !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000389 GenerateError("Signed integral constant '" +
Chris Lattner58af2a12006-02-15 07:22:58 +0000390 itostr(D.ConstPool64) + "' is invalid for type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000391 Ty->getDescription() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000392 return 0;
393 }
Reid Spencer49d273e2007-03-19 20:40:51 +0000394 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner58af2a12006-02-15 07:22:58 +0000395
396 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattner38905612008-02-19 04:36:25 +0000397 if (isa<IntegerType>(Ty) &&
398 ConstantInt::isValueValidForType(Ty, D.UConstPool64))
Reid Spencerb83eb642006-10-20 07:07:24 +0000399 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattner38905612008-02-19 04:36:25 +0000400
401 if (!isa<IntegerType>(Ty) ||
402 !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
403 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
404 "' is invalid or out of range for type '" +
405 Ty->getDescription() + "'");
406 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000407 }
Chris Lattner38905612008-02-19 04:36:25 +0000408 // This is really a signed reference. Transmogrify.
409 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner58af2a12006-02-15 07:22:58 +0000410
Chris Lattner1913b942008-07-11 00:30:39 +0000411 case ValID::ConstAPInt: // Is it an unsigned const pool reference?
412 if (!isa<IntegerType>(Ty)) {
413 GenerateError("Integral constant '" + D.getName() +
414 "' is invalid or out of range for type '" +
415 Ty->getDescription() + "'");
416 return 0;
417 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000418
Chris Lattner1913b942008-07-11 00:30:39 +0000419 {
420 APSInt Tmp = *D.ConstPoolInt;
Nuno Lopes19830332008-11-04 14:28:33 +0000421 D.destroy();
Chris Lattner1913b942008-07-11 00:30:39 +0000422 Tmp.extOrTrunc(Ty->getPrimitiveSizeInBits());
423 return ConstantInt::get(Tmp);
424 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000425
Chris Lattner58af2a12006-02-15 07:22:58 +0000426 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattner38905612008-02-19 04:36:25 +0000427 if (!Ty->isFloatingPoint() ||
428 !ConstantFP::isValueValidForType(Ty, *D.ConstPoolFP)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000429 GenerateError("FP constant invalid for type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000430 return 0;
431 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000432 // Lexer has no type info, so builds all float and double FP constants
Dale Johannesenc72cd7e2007-09-11 18:33:39 +0000433 // as double. Fix this here. Long double does not need this.
434 if (&D.ConstPoolFP->getSemantics() == &APFloat::IEEEdouble &&
Dale Johannesen236bbd42008-10-09 23:01:34 +0000435 Ty==Type::FloatTy) {
436 bool ignored;
437 D.ConstPoolFP->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
438 &ignored);
439 }
Nuno Lopesaa386d02008-11-04 14:43:20 +0000440 {
441 ConstantFP *tmp = ConstantFP::get(*D.ConstPoolFP);
442 D.destroy();
443 return tmp;
444 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000445
446 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000447 if (!isa<PointerType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000448 GenerateError("Cannot create a a non pointer null");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000449 return 0;
450 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000451 return ConstantPointerNull::get(cast<PointerType>(Ty));
452
453 case ValID::ConstUndefVal: // Is it an undef value?
454 return UndefValue::get(Ty);
455
456 case ValID::ConstZeroVal: // Is it a zero value?
457 return Constant::getNullValue(Ty);
Eric Christopher2a5196f2008-09-24 04:55:49 +0000458
Chris Lattner58af2a12006-02-15 07:22:58 +0000459 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000460 if (D.ConstantValue->getType() != Ty) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000461 GenerateError("Constant expression type different from required type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000462 return 0;
463 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000464 return D.ConstantValue;
465
466 case ValID::InlineAsmVal: { // Inline asm expression
467 const PointerType *PTy = dyn_cast<PointerType>(Ty);
468 const FunctionType *FTy =
469 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000470 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000471 GenerateError("Invalid type for asm constraint string");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000472 return 0;
473 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000474 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
475 D.IAD->HasSideEffects);
476 D.destroy(); // Free InlineAsmDescriptor.
477 return IA;
478 }
479 default:
Reid Spencera9720f52007-02-05 17:04:00 +0000480 assert(0 && "Unhandled case!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000481 return 0;
482 } // End of switch
483
Reid Spencera9720f52007-02-05 17:04:00 +0000484 assert(0 && "Unhandled case!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000485 return 0;
486}
487
Reid Spencer93c40032007-03-19 18:40:50 +0000488// getVal - This function is identical to getExistingVal, except that if a
Chris Lattner58af2a12006-02-15 07:22:58 +0000489// value is not already defined, it "improvises" by creating a placeholder var
490// that looks and acts just like the requested variable. When the value is
491// defined later, all uses of the placeholder variable are replaced with the
492// real thing.
493//
494static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000495 if (Ty == Type::LabelTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000496 GenerateError("Cannot use a basic block here");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000497 return 0;
498 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000499
500 // See if the value has already been defined.
Reid Spencer93c40032007-03-19 18:40:50 +0000501 Value *V = getExistingVal(Ty, ID);
Chris Lattner58af2a12006-02-15 07:22:58 +0000502 if (V) return V;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000503 if (TriggerError) return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000504
Reid Spencer5b7e7532006-09-28 19:28:24 +0000505 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Dan Gohmane4977cf2008-05-23 01:55:30 +0000506 GenerateError("Invalid use of a non-first-class type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000507 return 0;
508 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000509
510 // If we reached here, we referenced either a symbol that we don't know about
511 // or an id number that hasn't been read yet. We may be referencing something
512 // forward, so just create an entry to be resolved later and get to it...
513 //
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000514 switch (ID.Type) {
515 case ValID::GlobalName:
Reid Spencer9c9b63a2007-04-28 16:07:31 +0000516 case ValID::GlobalID: {
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000517 const PointerType *PTy = dyn_cast<PointerType>(Ty);
518 if (!PTy) {
519 GenerateError("Invalid type for reference to global" );
520 return 0;
521 }
522 const Type* ElTy = PTy->getElementType();
523 if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy))
Gabor Greife64d2482008-04-06 23:07:54 +0000524 V = Function::Create(FTy, GlobalValue::ExternalLinkage);
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000525 else
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000526 V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage, 0, "",
527 (Module*)0, false, PTy->getAddressSpace());
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000528 break;
Reid Spencer9c9b63a2007-04-28 16:07:31 +0000529 }
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000530 default:
531 V = new Argument(Ty);
532 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000533
Chris Lattner58af2a12006-02-15 07:22:58 +0000534 // Remember where this forward reference came from. FIXME, shouldn't we try
535 // to recycle these things??
536 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Duncan Sandsdc024672007-11-27 13:23:08 +0000537 LLLgetLineNo())));
Chris Lattner58af2a12006-02-15 07:22:58 +0000538
539 if (inFunctionScope())
540 InsertValue(V, CurFun.LateResolveValues);
541 else
542 InsertValue(V, CurModule.LateResolveValues);
543 return V;
544}
545
Reid Spencer93c40032007-03-19 18:40:50 +0000546/// defineBBVal - This is a definition of a new basic block with the specified
547/// identifier which must be the same as CurFun.NextValNum, if its numeric.
Nick Lewycky280a6e62008-04-25 16:53:59 +0000548static BasicBlock *defineBBVal(const ValID &ID) {
Reid Spencera9720f52007-02-05 17:04:00 +0000549 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000550
Chris Lattner58af2a12006-02-15 07:22:58 +0000551 BasicBlock *BB = 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000552
Reid Spencer93c40032007-03-19 18:40:50 +0000553 // First, see if this was forward referenced
Chris Lattner58af2a12006-02-15 07:22:58 +0000554
Reid Spencer93c40032007-03-19 18:40:50 +0000555 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
556 if (BBI != CurFun.BBForwardRefs.end()) {
557 BB = BBI->second;
Chris Lattner58af2a12006-02-15 07:22:58 +0000558 // The forward declaration could have been inserted anywhere in the
559 // function: insert it into the correct place now.
560 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
561 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Reid Spencer93c40032007-03-19 18:40:50 +0000562
Reid Spencer66728ef2007-03-20 01:13:36 +0000563 // We're about to erase the entry, save the key so we can clean it up.
564 ValID Tmp = BBI->first;
565
Reid Spencer93c40032007-03-19 18:40:50 +0000566 // Erase the forward ref from the map as its no longer "forward"
567 CurFun.BBForwardRefs.erase(ID);
568
Eric Christopher2a5196f2008-09-24 04:55:49 +0000569 // The key has been removed from the map but so we don't want to leave
Reid Spencer66728ef2007-03-20 01:13:36 +0000570 // strdup'd memory around so destroy it too.
571 Tmp.destroy();
572
Reid Spencer93c40032007-03-19 18:40:50 +0000573 // If its a numbered definition, bump the number and set the BB value.
574 if (ID.Type == ValID::LocalID) {
575 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
576 InsertValue(BB);
577 }
Eric Christopher2a5196f2008-09-24 04:55:49 +0000578 } else {
579 // We haven't seen this BB before and its first mention is a definition.
Devang Patel67909432008-03-03 18:58:47 +0000580 // Just create it and return it.
581 std::string Name (ID.Type == ValID::LocalName ? ID.getName() : "");
Gabor Greife64d2482008-04-06 23:07:54 +0000582 BB = BasicBlock::Create(Name, CurFun.CurrentFunction);
Devang Patel67909432008-03-03 18:58:47 +0000583 if (ID.Type == ValID::LocalID) {
584 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
585 InsertValue(BB);
586 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000587 }
Reid Spencer93c40032007-03-19 18:40:50 +0000588
Devang Patel67909432008-03-03 18:58:47 +0000589 ID.destroy();
Reid Spencer93c40032007-03-19 18:40:50 +0000590 return BB;
591}
592
593/// getBBVal - get an existing BB value or create a forward reference for it.
Eric Christopher2a5196f2008-09-24 04:55:49 +0000594///
Reid Spencer93c40032007-03-19 18:40:50 +0000595static BasicBlock *getBBVal(const ValID &ID) {
596 assert(inFunctionScope() && "Can't get basic block at global scope!");
597
598 BasicBlock *BB = 0;
599
600 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
601 if (BBI != CurFun.BBForwardRefs.end()) {
602 BB = BBI->second;
603 } if (ID.Type == ValID::LocalName) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000604 std::string Name = ID.getName();
Reid Spencer93c40032007-03-19 18:40:50 +0000605 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000606 if (N) {
Reid Spencer93c40032007-03-19 18:40:50 +0000607 if (N->getType()->getTypeID() == Type::LabelTyID)
608 BB = cast<BasicBlock>(N);
609 else
610 GenerateError("Reference to label '" + Name + "' is actually of type '"+
611 N->getType()->getDescription() + "'");
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000612 }
Reid Spencer93c40032007-03-19 18:40:50 +0000613 } else if (ID.Type == ValID::LocalID) {
614 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
615 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
616 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
617 else
Eric Christopher2a5196f2008-09-24 04:55:49 +0000618 GenerateError("Reference to label '%" + utostr(ID.Num) +
619 "' is actually of type '"+
Reid Spencer93c40032007-03-19 18:40:50 +0000620 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
621 }
622 } else {
623 GenerateError("Illegal label reference " + ID.getName());
624 return 0;
625 }
626
627 // If its already been defined, return it now.
628 if (BB) {
629 ID.destroy(); // Free strdup'd memory.
630 return BB;
631 }
632
633 // Otherwise, this block has not been seen before, create it.
634 std::string Name;
635 if (ID.Type == ValID::LocalName)
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000636 Name = ID.getName();
Gabor Greife64d2482008-04-06 23:07:54 +0000637 BB = BasicBlock::Create(Name, CurFun.CurrentFunction);
Reid Spencer93c40032007-03-19 18:40:50 +0000638
639 // Insert it in the forward refs map.
640 CurFun.BBForwardRefs[ID] = BB;
641
Chris Lattner58af2a12006-02-15 07:22:58 +0000642 return BB;
643}
644
645
646//===----------------------------------------------------------------------===//
647// Code to handle forward references in instructions
648//===----------------------------------------------------------------------===//
649//
650// This code handles the late binding needed with statements that reference
651// values not defined yet... for example, a forward branch, or the PHI node for
652// a loop body.
653//
654// This keeps a table (CurFun.LateResolveValues) of all such forward references
655// and back patchs after we are done.
656//
657
658// ResolveDefinitions - If we could not resolve some defs at parsing
659// time (forward branches, phi functions for loops, etc...) resolve the
660// defs now...
661//
Eric Christopher2a5196f2008-09-24 04:55:49 +0000662static void
Reid Spencer93c40032007-03-19 18:40:50 +0000663ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000664 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencer93c40032007-03-19 18:40:50 +0000665 while (!LateResolvers.empty()) {
666 Value *V = LateResolvers.back();
667 LateResolvers.pop_back();
Chris Lattner58af2a12006-02-15 07:22:58 +0000668
Reid Spencer93c40032007-03-19 18:40:50 +0000669 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
670 CurModule.PlaceHolderInfo.find(V);
671 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000672
Reid Spencer93c40032007-03-19 18:40:50 +0000673 ValID &DID = PHI->second.first;
Chris Lattner58af2a12006-02-15 07:22:58 +0000674
Reid Spencer93c40032007-03-19 18:40:50 +0000675 Value *TheRealValue = getExistingVal(V->getType(), DID);
676 if (TriggerError)
677 return;
678 if (TheRealValue) {
679 V->replaceAllUsesWith(TheRealValue);
680 delete V;
681 CurModule.PlaceHolderInfo.erase(PHI);
682 } else if (FutureLateResolvers) {
683 // Functions have their unresolved items forwarded to the module late
684 // resolver table
685 InsertValue(V, *FutureLateResolvers);
686 } else {
687 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
688 GenerateError("Reference to an invalid definition: '" +DID.getName()+
689 "' of type '" + V->getType()->getDescription() + "'",
690 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000691 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000692 } else {
Reid Spencer93c40032007-03-19 18:40:50 +0000693 GenerateError("Reference to an invalid definition: #" +
694 itostr(DID.Num) + " of type '" +
695 V->getType()->getDescription() + "'",
696 PHI->second.second);
697 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000698 }
699 }
700 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000701 LateResolvers.clear();
702}
703
704// ResolveTypeTo - A brand new type was just declared. This means that (if
705// name is not null) things referencing Name can be resolved. Otherwise, things
706// refering to the number can be resolved. Do this now.
707//
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000708static void ResolveTypeTo(std::string *Name, const Type *ToTy) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000709 ValID D;
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000710 if (Name)
711 D = ValID::createLocalName(*Name);
Eric Christopher2a5196f2008-09-24 04:55:49 +0000712 else
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000713 D = ValID::createLocalID(CurModule.Types.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000714
Reid Spencer861d9d62006-11-28 07:29:44 +0000715 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattner58af2a12006-02-15 07:22:58 +0000716 CurModule.LateResolveTypes.find(D);
717 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencer861d9d62006-11-28 07:29:44 +0000718 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Nuno Lopes6ec8a252008-10-15 11:11:12 +0000719 I->first.destroy();
Chris Lattner58af2a12006-02-15 07:22:58 +0000720 CurModule.LateResolveTypes.erase(I);
721 }
Nuno Lopes191dfb92008-10-03 15:52:39 +0000722 D.destroy();
Chris Lattner58af2a12006-02-15 07:22:58 +0000723}
724
725// setValueName - Set the specified value to the name given. The name may be
726// null potentially, in which case this is a noop. The string passed in is
727// assumed to be a malloc'd string buffer, and is free'd by this function.
728//
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000729static void setValueName(Value *V, std::string *NameStr) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000730 if (!NameStr) return;
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000731 std::string Name(*NameStr); // Copy string
732 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000733
Reid Spencer41dff5e2007-01-26 08:05:27 +0000734 if (V->getType() == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000735 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000736 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000737 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000738
Reid Spencera9720f52007-02-05 17:04:00 +0000739 assert(inFunctionScope() && "Must be in function scope!");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000740 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
741 if (ST.lookup(Name)) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000742 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000743 V->getType()->getDescription() + "'");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000744 return;
745 }
746
747 // Set the name.
748 V->setName(Name);
Chris Lattner58af2a12006-02-15 07:22:58 +0000749}
750
751/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
752/// this is a declaration, otherwise it is a definition.
753static GlobalVariable *
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000754ParseGlobalVariable(std::string *NameStr,
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000755 GlobalValue::LinkageTypes Linkage,
756 GlobalValue::VisibilityTypes Visibility,
Chris Lattner58af2a12006-02-15 07:22:58 +0000757 bool isConstantGlobal, const Type *Ty,
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000758 Constant *Initializer, bool IsThreadLocal,
759 unsigned AddressSpace = 0) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000760 if (isa<FunctionType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000761 GenerateError("Cannot declare global vars of function type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000762 return 0;
763 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000764 if (Ty == Type::LabelTy) {
765 GenerateError("Cannot declare global vars of label type");
766 return 0;
767 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000768
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000769 const PointerType *PTy = PointerType::get(Ty, AddressSpace);
Chris Lattner58af2a12006-02-15 07:22:58 +0000770
771 std::string Name;
772 if (NameStr) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000773 Name = *NameStr; // Copy string
774 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000775 }
776
777 // See if this global value was forward referenced. If so, recycle the
778 // object.
779 ValID ID;
780 if (!Name.empty()) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000781 ID = ValID::createGlobalName(Name);
Chris Lattner58af2a12006-02-15 07:22:58 +0000782 } else {
Reid Spencer93c40032007-03-19 18:40:50 +0000783 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000784 }
785
786 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
787 // Move the global to the end of the list, from whereever it was
788 // previously inserted.
789 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
790 CurModule.CurrentModule->getGlobalList().remove(GV);
791 CurModule.CurrentModule->getGlobalList().push_back(GV);
792 GV->setInitializer(Initializer);
793 GV->setLinkage(Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000794 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000795 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000796 GV->setThreadLocal(IsThreadLocal);
Chris Lattner58af2a12006-02-15 07:22:58 +0000797 InsertValue(GV, CurModule.Values);
Nuno Lopes191dfb92008-10-03 15:52:39 +0000798 ID.destroy();
Chris Lattner58af2a12006-02-15 07:22:58 +0000799 return GV;
800 }
801
Nuno Lopes191dfb92008-10-03 15:52:39 +0000802 ID.destroy();
803
Reid Spenceref9b9a72007-02-05 20:47:22 +0000804 // If this global has a name
Chris Lattner58af2a12006-02-15 07:22:58 +0000805 if (!Name.empty()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000806 // if the global we're parsing has an initializer (is a definition) and
807 // has external linkage.
808 if (Initializer && Linkage != GlobalValue::InternalLinkage)
809 // If there is already a global with external linkage with this name
810 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
811 // If we allow this GVar to get created, it will be renamed in the
812 // symbol table because it conflicts with an existing GVar. We can't
813 // allow redefinition of GVars whose linking indicates that their name
814 // must stay the same. Issue the error.
815 GenerateError("Redefinition of global variable named '" + Name +
816 "' of type '" + Ty->getDescription() + "'");
817 return 0;
818 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000819 }
820
821 // Otherwise there is no existing GV to use, create one now.
822 GlobalVariable *GV =
823 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000824 CurModule.CurrentModule, IsThreadLocal, AddressSpace);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000825 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000826 InsertValue(GV, CurModule.Values);
827 return GV;
828}
829
830// setTypeName - Set the specified type to the name given. The name may be
831// null potentially, in which case this is a noop. The string passed in is
832// assumed to be a malloc'd string buffer, and is freed by this function.
833//
834// This function returns true if the type has already been defined, but is
835// allowed to be redefined in the specified context. If the name is a new name
836// for the type plane, it is inserted and false is returned.
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000837static bool setTypeName(const Type *T, std::string *NameStr) {
Reid Spencera9720f52007-02-05 17:04:00 +0000838 assert(!inFunctionScope() && "Can't give types function-local names!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000839 if (NameStr == 0) return false;
Eric Christopher2a5196f2008-09-24 04:55:49 +0000840
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000841 std::string Name(*NameStr); // Copy string
842 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000843
844 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000845 if (T == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000846 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000847 return false;
848 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000849
850 // Set the type name, checking for conflicts as we do so.
851 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
852
853 if (AlreadyExists) { // Inserting a name that is already defined???
854 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencera9720f52007-02-05 17:04:00 +0000855 assert(Existing && "Conflict but no matching type?!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000856
857 // There is only one case where this is allowed: when we are refining an
858 // opaque type. In this case, Existing will be an opaque type.
859 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
860 // We ARE replacing an opaque type!
861 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
862 return true;
863 }
864
865 // Otherwise, this is an attempt to redefine a type. That's okay if
866 // the redefinition is identical to the original. This will be so if
867 // Existing and T point to the same Type object. In this one case we
868 // allow the equivalent redefinition.
869 if (Existing == T) return true; // Yes, it's equal.
870
871 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer63c34452007-01-05 21:51:07 +0000872 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000873 T->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +0000874 }
875
876 return false;
877}
878
879//===----------------------------------------------------------------------===//
880// Code for handling upreferences in type names...
881//
882
883// TypeContains - Returns true if Ty directly contains E in it.
884//
885static bool TypeContains(const Type *Ty, const Type *E) {
886 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
887 E) != Ty->subtype_end();
888}
889
890namespace {
891 struct UpRefRecord {
892 // NestingLevel - The number of nesting levels that need to be popped before
893 // this type is resolved.
894 unsigned NestingLevel;
895
896 // LastContainedTy - This is the type at the current binding level for the
897 // type. Every time we reduce the nesting level, this gets updated.
898 const Type *LastContainedTy;
899
900 // UpRefTy - This is the actual opaque type that the upreference is
901 // represented with.
902 OpaqueType *UpRefTy;
903
904 UpRefRecord(unsigned NL, OpaqueType *URTy)
905 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
906 };
907}
908
909// UpRefs - A list of the outstanding upreferences that need to be resolved.
910static std::vector<UpRefRecord> UpRefs;
911
912/// HandleUpRefs - Every time we finish a new layer of types, this function is
913/// called. It loops through the UpRefs vector, which is a list of the
914/// currently active types. For each type, if the up reference is contained in
915/// the newly completed type, we decrement the level count. When the level
916/// count reaches zero, the upreferenced type is the type that is passed in:
917/// thus we can complete the cycle.
918///
919static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner224f84f2006-08-18 17:34:45 +0000920 // If Ty isn't abstract, or if there are no up-references in it, then there is
921 // nothing to resolve here.
922 if (!ty->isAbstract() || UpRefs.empty()) return ty;
Eric Christopher2a5196f2008-09-24 04:55:49 +0000923
Chris Lattner58af2a12006-02-15 07:22:58 +0000924 PATypeHolder Ty(ty);
925 UR_OUT("Type '" << Ty->getDescription() <<
926 "' newly formed. Resolving upreferences.\n" <<
927 UpRefs.size() << " upreferences active!\n");
928
929 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
930 // to zero), we resolve them all together before we resolve them to Ty. At
931 // the end of the loop, if there is anything to resolve to Ty, it will be in
932 // this variable.
933 OpaqueType *TypeToResolve = 0;
934
935 for (unsigned i = 0; i != UpRefs.size(); ++i) {
936 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
937 << UpRefs[i].second->getDescription() << ") = "
938 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
939 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
940 // Decrement level of upreference
941 unsigned Level = --UpRefs[i].NestingLevel;
942 UpRefs[i].LastContainedTy = Ty;
943 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
944 if (Level == 0) { // Upreference should be resolved!
945 if (!TypeToResolve) {
946 TypeToResolve = UpRefs[i].UpRefTy;
947 } else {
948 UR_OUT(" * Resolving upreference for "
949 << UpRefs[i].second->getDescription() << "\n";
950 std::string OldName = UpRefs[i].UpRefTy->getDescription());
951 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
952 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
953 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
954 }
955 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
956 --i; // Do not skip the next element...
957 }
958 }
959 }
960
961 if (TypeToResolve) {
962 UR_OUT(" * Resolving upreference for "
963 << UpRefs[i].second->getDescription() << "\n";
964 std::string OldName = TypeToResolve->getDescription());
965 TypeToResolve->refineAbstractTypeTo(Ty);
966 }
967
968 return Ty;
969}
970
Chris Lattner58af2a12006-02-15 07:22:58 +0000971//===----------------------------------------------------------------------===//
972// RunVMAsmParser - Define an interface to this parser
973//===----------------------------------------------------------------------===//
974//
Reid Spencer14310612006-12-31 05:40:51 +0000975static Module* RunParser(Module * M);
976
Duncan Sandsdc024672007-11-27 13:23:08 +0000977Module *llvm::RunVMAsmParser(llvm::MemoryBuffer *MB) {
978 InitLLLexer(MB);
979 Module *M = RunParser(new Module(LLLgetFilename()));
980 FreeLexer();
981 return M;
Chris Lattner58af2a12006-02-15 07:22:58 +0000982}
983
984%}
985
986%union {
987 llvm::Module *ModuleVal;
988 llvm::Function *FunctionVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000989 llvm::BasicBlock *BasicBlockVal;
990 llvm::TerminatorInst *TermInstVal;
991 llvm::Instruction *InstVal;
Reid Spencera132e042006-12-03 05:46:11 +0000992 llvm::Constant *ConstVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000993
Reid Spencera132e042006-12-03 05:46:11 +0000994 const llvm::Type *PrimType;
Reid Spencer14310612006-12-31 05:40:51 +0000995 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencera132e042006-12-03 05:46:11 +0000996 llvm::PATypeHolder *TypeVal;
997 llvm::Value *ValueVal;
Reid Spencera132e042006-12-03 05:46:11 +0000998 std::vector<llvm::Value*> *ValueList;
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000999 std::vector<unsigned> *ConstantList;
Reid Spencer14310612006-12-31 05:40:51 +00001000 llvm::ArgListType *ArgList;
1001 llvm::TypeWithAttrs TypeWithAttrs;
1002 llvm::TypeWithAttrsList *TypeWithAttrsList;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001003 llvm::ParamList *ParamList;
Reid Spencer14310612006-12-31 05:40:51 +00001004
Chris Lattner58af2a12006-02-15 07:22:58 +00001005 // Represent the RHS of PHI node
Reid Spencera132e042006-12-03 05:46:11 +00001006 std::list<std::pair<llvm::Value*,
1007 llvm::BasicBlock*> > *PHIList;
Chris Lattner58af2a12006-02-15 07:22:58 +00001008 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencera132e042006-12-03 05:46:11 +00001009 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner58af2a12006-02-15 07:22:58 +00001010
1011 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001012 llvm::GlobalValue::VisibilityTypes Visibility;
Devang Patel05988662008-09-25 21:00:45 +00001013 llvm::Attributes Attributes;
Reid Spencer38c91a92007-02-28 02:24:54 +00001014 llvm::APInt *APIntVal;
Chris Lattner58af2a12006-02-15 07:22:58 +00001015 int64_t SInt64Val;
1016 uint64_t UInt64Val;
1017 int SIntVal;
1018 unsigned UIntVal;
Dale Johannesen43421b32007-09-06 18:13:44 +00001019 llvm::APFloat *FPVal;
Chris Lattner58af2a12006-02-15 07:22:58 +00001020 bool BoolVal;
1021
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001022 std::string *StrVal; // This memory must be deleted
1023 llvm::ValID ValIDVal;
Chris Lattner58af2a12006-02-15 07:22:58 +00001024
Reid Spencera132e042006-12-03 05:46:11 +00001025 llvm::Instruction::BinaryOps BinaryOpVal;
1026 llvm::Instruction::TermOps TermOpVal;
1027 llvm::Instruction::MemoryOps MemOpVal;
1028 llvm::Instruction::CastOps CastOpVal;
1029 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencera132e042006-12-03 05:46:11 +00001030 llvm::ICmpInst::Predicate IPredicate;
1031 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner58af2a12006-02-15 07:22:58 +00001032}
1033
Eric Christopher2a5196f2008-09-24 04:55:49 +00001034%type <ModuleVal> Module
Chris Lattner58af2a12006-02-15 07:22:58 +00001035%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
1036%type <BasicBlockVal> BasicBlock InstructionList
1037%type <TermInstVal> BBTerminatorInst
1038%type <InstVal> Inst InstVal MemoryInst
Anton Korobeynikov38e09802007-04-28 13:48:45 +00001039%type <ConstVal> ConstVal ConstExpr AliaseeRef
Chris Lattner58af2a12006-02-15 07:22:58 +00001040%type <ConstVector> ConstVector
1041%type <ArgList> ArgList ArgListH
Chris Lattner58af2a12006-02-15 07:22:58 +00001042%type <PHIList> PHIList
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001043%type <ParamList> ParamList // For call param lists & GEP indices
Reid Spencer14310612006-12-31 05:40:51 +00001044%type <ValueList> IndexList // For GEP indices
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001045%type <ConstantList> ConstantIndexList // For insertvalue/extractvalue indices
Eric Christopher2a5196f2008-09-24 04:55:49 +00001046%type <TypeList> TypeListI
Reid Spencer14310612006-12-31 05:40:51 +00001047%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer218ded22007-01-05 17:07:23 +00001048%type <TypeWithAttrs> ArgType
Chris Lattner58af2a12006-02-15 07:22:58 +00001049%type <JumpTable> JumpTable
1050%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001051%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattner58af2a12006-02-15 07:22:58 +00001052%type <BoolVal> OptVolatile // 'volatile' or not
1053%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1054%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencer14310612006-12-31 05:40:51 +00001055%type <Linkage> GVInternalLinkage GVExternalLinkage
1056%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001057%type <Linkage> AliasLinkage
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001058%type <Visibility> GVVisibilityStyle
Chris Lattner58af2a12006-02-15 07:22:58 +00001059
1060// ValueRef - Unresolved reference to a definition or BB
1061%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1062%type <ValueVal> ResolvedVal // <type> <valref> pair
Devang Patel7990dc72008-02-20 22:40:23 +00001063%type <ValueList> ReturnedVal
Chris Lattner58af2a12006-02-15 07:22:58 +00001064// Tokens and types for handling constant integer values
1065//
1066// ESINT64VAL - A negative number within long long range
1067%token <SInt64Val> ESINT64VAL
1068
1069// EUINT64VAL - A positive number within uns. long long range
1070%token <UInt64Val> EUINT64VAL
Chris Lattner58af2a12006-02-15 07:22:58 +00001071
Eric Christopher2a5196f2008-09-24 04:55:49 +00001072// ESAPINTVAL - A negative number with arbitrary precision
Reid Spencer38c91a92007-02-28 02:24:54 +00001073%token <APIntVal> ESAPINTVAL
1074
Eric Christopher2a5196f2008-09-24 04:55:49 +00001075// EUAPINTVAL - A positive number with arbitrary precision
Reid Spencer38c91a92007-02-28 02:24:54 +00001076%token <APIntVal> EUAPINTVAL
1077
Reid Spencer41dff5e2007-01-26 08:05:27 +00001078%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattner58af2a12006-02-15 07:22:58 +00001079%token <FPVal> FPVAL // Float or Double constant
1080
1081// Built in types...
Reid Spencer218ded22007-01-05 17:07:23 +00001082%type <TypeVal> Types ResultTypes
Chris Lattner740e7092008-10-15 06:16:57 +00001083%type <PrimType> PrimType // Classifications
Eric Christopher2a5196f2008-09-24 04:55:49 +00001084%token <PrimType> VOID INTTYPE
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001085%token <PrimType> FLOAT DOUBLE X86_FP80 FP128 PPC_FP128 LABEL
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001086%token TYPE
Chris Lattner58af2a12006-02-15 07:22:58 +00001087
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001088
Eric Christopher2a5196f2008-09-24 04:55:49 +00001089%token<StrVal> LOCALVAR GLOBALVAR LABELSTR
Reid Spencered951ea2007-05-19 07:22:10 +00001090%token<StrVal> STRINGCONSTANT ATSTRINGCONSTANT PCTSTRINGCONSTANT
Reid Spencer41dff5e2007-01-26 08:05:27 +00001091%type <StrVal> LocalName OptLocalName OptLocalAssign
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001092%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001093%type <StrVal> OptSection SectionString OptGC
Chris Lattner58af2a12006-02-15 07:22:58 +00001094
Christopher Lambbf3348d2007-12-12 08:45:45 +00001095%type <UIntVal> OptAlign OptCAlign OptAddrSpace
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001096
Reid Spencer3d6b71e2007-04-09 01:56:05 +00001097%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001098%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
Reid Spencer14310612006-12-31 05:40:51 +00001099%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Dale Johannesenc7071cc2008-05-14 20:13:36 +00001100%token DLLIMPORT DLLEXPORT EXTERN_WEAK COMMON
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001101%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN ADDRSPACE
Chris Lattner58af2a12006-02-15 07:22:58 +00001102%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001103%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Nick Lewycky280a6e62008-04-25 16:53:59 +00001104%token DATALAYOUT
Chris Lattner15bd0952008-08-29 17:20:18 +00001105%type <UIntVal> OptCallingConv LocalNumber
Devang Patel05988662008-09-25 21:00:45 +00001106%type <Attributes> OptAttributes Attribute
1107%type <Attributes> OptFuncAttrs FuncAttr
Devang Patel652203f2008-09-29 20:49:50 +00001108%type <Attributes> OptRetAttrs RetAttr
Chris Lattner58af2a12006-02-15 07:22:58 +00001109
1110// Basic Block Terminating Operators
1111%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
1112
1113// Binary Operators
Reid Spencere4d87aa2006-12-23 06:05:41 +00001114%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencer3ed469c2006-11-02 20:25:50 +00001115%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer832254e2007-02-02 02:16:23 +00001116%token <BinaryOpVal> SHL LSHR ASHR
1117
Eric Christopher2a5196f2008-09-24 04:55:49 +00001118%token <OtherOpVal> ICMP FCMP VICMP VFCMP
Reid Spencera132e042006-12-03 05:46:11 +00001119%type <IPredicate> IPredicates
Reid Spencera132e042006-12-03 05:46:11 +00001120%type <FPredicate> FPredicates
Eric Christopher2a5196f2008-09-24 04:55:49 +00001121%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001122%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner58af2a12006-02-15 07:22:58 +00001123
1124// Memory Instructions
1125%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1126
Reid Spencer3da59db2006-11-27 01:05:10 +00001127// Cast Operators
1128%type <CastOpVal> CastOps
1129%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1130%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1131
Chris Lattner58af2a12006-02-15 07:22:58 +00001132// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001133%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattnerd5efe842006-04-08 01:18:56 +00001134%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Devang Patel5a970972008-02-19 22:27:01 +00001135%token <OtherOpVal> GETRESULT
Dan Gohmane4977cf2008-05-23 01:55:30 +00001136%token <OtherOpVal> EXTRACTVALUE INSERTVALUE
Chris Lattner58af2a12006-02-15 07:22:58 +00001137
Reid Spencer218ded22007-01-05 17:07:23 +00001138// Function Attributes
Nick Lewyckyd802be32008-12-19 09:41:54 +00001139%token SIGNEXT ZEROEXT NORETURN INREG SRET NOUNWIND NOALIAS NOCAPTURE BYVAL
1140%token READNONE READONLY GC OPTSIZE NOINLINE ALWAYSINLINE SSP SSPREQ NEST
Devang Pateld4980812008-09-02 20:52:40 +00001141
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001142// Visibility Styles
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001143%token DEFAULT HIDDEN PROTECTED
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001144
Chris Lattner58af2a12006-02-15 07:22:58 +00001145%start Module
1146%%
1147
Chris Lattner58af2a12006-02-15 07:22:58 +00001148
Chris Lattner58af2a12006-02-15 07:22:58 +00001149// Operations that are notably excluded from this list include:
1150// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1151//
Reid Spencer3ed469c2006-11-02 20:25:50 +00001152ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer832254e2007-02-02 02:16:23 +00001153LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Eric Christopher2a5196f2008-09-24 04:55:49 +00001154CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
Reid Spencer3da59db2006-11-27 01:05:10 +00001155 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer832254e2007-02-02 02:16:23 +00001156
Eric Christopher2a5196f2008-09-24 04:55:49 +00001157IPredicates
Reid Spencer4012e832006-12-04 05:24:24 +00001158 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001159 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1160 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1161 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001162 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001163 ;
1164
Eric Christopher2a5196f2008-09-24 04:55:49 +00001165FPredicates
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001166 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1167 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1168 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1169 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1170 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1171 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1172 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1173 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1174 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1175 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001176
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001177LocalName : LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001178OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1179
Christopher Lambbf3348d2007-12-12 08:45:45 +00001180OptAddrSpace : ADDRSPACE '(' EUINT64VAL ')' { $$=$3; }
1181 | /*empty*/ { $$=0; };
1182
Reid Spencer41dff5e2007-01-26 08:05:27 +00001183/// OptLocalAssign - Value producing statements have an optional assignment
1184/// component.
1185OptLocalAssign : LocalName '=' {
1186 $$ = $1;
1187 CHECK_FOR_ERROR
1188 }
1189 | /*empty*/ {
1190 $$ = 0;
1191 CHECK_FOR_ERROR
1192 };
1193
Chris Lattner15bd0952008-08-29 17:20:18 +00001194LocalNumber : LOCALVAL_ID '=' {
1195 $$ = $1;
1196 CHECK_FOR_ERROR
1197};
1198
1199
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001200GlobalName : GLOBALVAR | ATSTRINGCONSTANT ;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001201
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001202OptGlobalAssign : GlobalAssign
Chris Lattner58af2a12006-02-15 07:22:58 +00001203 | /*empty*/ {
1204 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001205 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001206 };
1207
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001208GlobalAssign : GlobalName '=' {
1209 $$ = $1;
1210 CHECK_FOR_ERROR
Chris Lattner6cdc6822007-04-26 05:31:05 +00001211 };
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001212
Eric Christopher2a5196f2008-09-24 04:55:49 +00001213GVInternalLinkage
1214 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1215 | WEAK { $$ = GlobalValue::WeakLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001216 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1217 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001218 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Dale Johannesenc7071cc2008-05-14 20:13:36 +00001219 | COMMON { $$ = GlobalValue::CommonLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001220 ;
1221
1222GVExternalLinkage
1223 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1224 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1225 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1226 ;
1227
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001228GVVisibilityStyle
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001229 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1230 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1231 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1232 | PROTECTED { $$ = GlobalValue::ProtectedVisibility; }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001233 ;
1234
Reid Spencer14310612006-12-31 05:40:51 +00001235FunctionDeclareLinkage
1236 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001237 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
Reid Spencer14310612006-12-31 05:40:51 +00001238 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001239 ;
Eric Christopher2a5196f2008-09-24 04:55:49 +00001240
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001241FunctionDefineLinkage
Reid Spencer14310612006-12-31 05:40:51 +00001242 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1243 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001244 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1245 | WEAK { $$ = GlobalValue::WeakLinkage; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001246 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1247 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001248
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001249AliasLinkage
1250 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1251 | WEAK { $$ = GlobalValue::WeakLinkage; }
1252 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1253 ;
1254
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001255OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1256 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001257 FASTCC_TOK { $$ = CallingConv::Fast; } |
1258 COLDCC_TOK { $$ = CallingConv::Cold; } |
1259 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1260 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1261 CC_TOK EUINT64VAL {
Chris Lattner58af2a12006-02-15 07:22:58 +00001262 if ((unsigned)$2 != $2)
Reid Spencerb5334b02007-02-05 10:18:06 +00001263 GEN_ERROR("Calling conv too large");
Chris Lattner58af2a12006-02-15 07:22:58 +00001264 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001265 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001266 };
1267
Nick Lewyckyd802be32008-12-19 09:41:54 +00001268Attribute : ZEROEXT { $$ = Attribute::ZExt; }
1269 | ZEXT { $$ = Attribute::ZExt; }
1270 | SIGNEXT { $$ = Attribute::SExt; }
1271 | SEXT { $$ = Attribute::SExt; }
1272 | INREG { $$ = Attribute::InReg; }
1273 | SRET { $$ = Attribute::StructRet; }
1274 | NOALIAS { $$ = Attribute::NoAlias; }
1275 | NOCAPTURE { $$ = Attribute::NoCapture; }
1276 | BYVAL { $$ = Attribute::ByVal; }
1277 | NEST { $$ = Attribute::Nest; }
Eric Christopher2a5196f2008-09-24 04:55:49 +00001278 | ALIGN EUINT64VAL { $$ =
Devang Patel05988662008-09-25 21:00:45 +00001279 Attribute::constructAlignmentFromInt($2); }
Reid Spencer14310612006-12-31 05:40:51 +00001280 ;
1281
Devang Patel05988662008-09-25 21:00:45 +00001282OptAttributes : /* empty */ { $$ = Attribute::None; }
1283 | OptAttributes Attribute {
Reid Spencer7b5d4662007-04-09 06:16:21 +00001284 $$ = $1 | $2;
Reid Spencer14310612006-12-31 05:40:51 +00001285 }
1286 ;
1287
Nick Lewyckyd802be32008-12-19 09:41:54 +00001288RetAttr : INREG { $$ = Attribute::InReg; }
Devang Patel652203f2008-09-29 20:49:50 +00001289 | ZEROEXT { $$ = Attribute::ZExt; }
1290 | SIGNEXT { $$ = Attribute::SExt; }
Nick Lewyckyd802be32008-12-19 09:41:54 +00001291 | NOALIAS { $$ = Attribute::NoAlias; }
Devang Patel652203f2008-09-29 20:49:50 +00001292 ;
1293
1294OptRetAttrs : /* empty */ { $$ = Attribute::None; }
1295 | OptRetAttrs RetAttr {
1296 $$ = $1 | $2;
1297 }
1298 ;
1299
1300
Devang Patel05988662008-09-25 21:00:45 +00001301FuncAttr : NORETURN { $$ = Attribute::NoReturn; }
1302 | NOUNWIND { $$ = Attribute::NoUnwind; }
1303 | INREG { $$ = Attribute::InReg; }
1304 | ZEROEXT { $$ = Attribute::ZExt; }
1305 | SIGNEXT { $$ = Attribute::SExt; }
1306 | READNONE { $$ = Attribute::ReadNone; }
1307 | READONLY { $$ = Attribute::ReadOnly; }
Chris Lattner9fc4da42008-10-08 06:44:45 +00001308 | NOINLINE { $$ = Attribute::NoInline; }
1309 | ALWAYSINLINE { $$ = Attribute::AlwaysInline; }
Bill Wendling6ff4bfe2008-11-13 01:03:00 +00001310 | OPTSIZE { $$ = Attribute::OptimizeForSize; }
1311 | SSP { $$ = Attribute::StackProtect; }
1312 | SSPREQ { $$ = Attribute::StackProtectReq; }
Reid Spencer218ded22007-01-05 17:07:23 +00001313 ;
1314
Devang Patel05988662008-09-25 21:00:45 +00001315OptFuncAttrs : /* empty */ { $$ = Attribute::None; }
Reid Spencer218ded22007-01-05 17:07:23 +00001316 | OptFuncAttrs FuncAttr {
Reid Spencer7b5d4662007-04-09 06:16:21 +00001317 $$ = $1 | $2;
Reid Spencer218ded22007-01-05 17:07:23 +00001318 }
Reid Spencer14310612006-12-31 05:40:51 +00001319 ;
1320
Devang Patel652203f2008-09-29 20:49:50 +00001321
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001322OptGC : /* empty */ { $$ = 0; }
1323 | GC STRINGCONSTANT {
1324 $$ = $2;
1325 }
1326 ;
1327
Chris Lattner58af2a12006-02-15 07:22:58 +00001328// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1329// a comma before it.
1330OptAlign : /*empty*/ { $$ = 0; } |
1331 ALIGN EUINT64VAL {
1332 $$ = $2;
1333 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001334 GEN_ERROR("Alignment must be a power of two");
Nick Lewyckyd802be32008-12-19 09:41:54 +00001335 if ($$ > 0x40000000)
1336 GEN_ERROR("Alignment too large");
Reid Spencer61c83e02006-08-18 08:43:06 +00001337 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001338};
1339OptCAlign : /*empty*/ { $$ = 0; } |
1340 ',' ALIGN EUINT64VAL {
1341 $$ = $3;
1342 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001343 GEN_ERROR("Alignment must be a power of two");
Nick Lewyckyd802be32008-12-19 09:41:54 +00001344 if ($$ > 0x40000000)
1345 GEN_ERROR("Alignment too large");
Reid Spencer61c83e02006-08-18 08:43:06 +00001346 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001347};
1348
1349
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001350
Chris Lattner58af2a12006-02-15 07:22:58 +00001351SectionString : SECTION STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001352 for (unsigned i = 0, e = $2->length(); i != e; ++i)
1353 if ((*$2)[i] == '"' || (*$2)[i] == '\\')
Reid Spencerb5334b02007-02-05 10:18:06 +00001354 GEN_ERROR("Invalid character in section name");
Chris Lattner58af2a12006-02-15 07:22:58 +00001355 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001356 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001357};
1358
1359OptSection : /*empty*/ { $$ = 0; } |
1360 SectionString { $$ = $1; };
1361
1362// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1363// is set to be the global we are processing.
1364//
1365GlobalVarAttributes : /* empty */ {} |
1366 ',' GlobalVarAttribute GlobalVarAttributes {};
1367GlobalVarAttribute : SectionString {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001368 CurGV->setSection(*$1);
1369 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001370 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00001371 }
Chris Lattner58af2a12006-02-15 07:22:58 +00001372 | ALIGN EUINT64VAL {
1373 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001374 GEN_ERROR("Alignment must be a power of two");
Nick Lewyckyd802be32008-12-19 09:41:54 +00001375 if ($2 > 0x40000000)
1376 GEN_ERROR("Alignment too large");
Chris Lattner58af2a12006-02-15 07:22:58 +00001377 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001378 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001379 };
1380
1381//===----------------------------------------------------------------------===//
1382// Types includes all predefined types... except void, because it can only be
Eric Christopher2a5196f2008-09-24 04:55:49 +00001383// used in specific contexts (function returning void for example).
Chris Lattner58af2a12006-02-15 07:22:58 +00001384
1385// Derived types are added later...
1386//
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001387PrimType : INTTYPE | FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80 | LABEL ;
Reid Spencer14310612006-12-31 05:40:51 +00001388
Eric Christopher2a5196f2008-09-24 04:55:49 +00001389Types
Reid Spencer14310612006-12-31 05:40:51 +00001390 : OPAQUE {
Reid Spencera132e042006-12-03 05:46:11 +00001391 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001392 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001393 }
1394 | PrimType {
Reid Spencera132e042006-12-03 05:46:11 +00001395 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001396 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00001397 }
Christopher Lambbf3348d2007-12-12 08:45:45 +00001398 | Types OptAddrSpace '*' { // Pointer type?
Reid Spencer14310612006-12-31 05:40:51 +00001399 if (*$1 == Type::LabelTy)
1400 GEN_ERROR("Cannot form a pointer to a basic block");
Christopher Lambbf3348d2007-12-12 08:45:45 +00001401 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1, $2)));
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001402 delete $1;
1403 CHECK_FOR_ERROR
1404 }
Reid Spencer14310612006-12-31 05:40:51 +00001405 | SymbolicValueRef { // Named types are also simple types...
1406 const Type* tmp = getTypeVal($1);
1407 CHECK_FOR_ERROR
1408 $$ = new PATypeHolder(tmp);
1409 }
1410 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerb5334b02007-02-05 10:18:06 +00001411 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattner58af2a12006-02-15 07:22:58 +00001412 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1413 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencera132e042006-12-03 05:46:11 +00001414 $$ = new PATypeHolder(OT);
Chris Lattner58af2a12006-02-15 07:22:58 +00001415 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001416 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001417 }
Reid Spencer218ded22007-01-05 17:07:23 +00001418 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001419 // Allow but ignore attributes on function types; this permits auto-upgrade.
1420 // FIXME: remove in LLVM 3.0.
Chris Lattnera925a142008-04-23 05:37:08 +00001421 const Type *RetTy = *$1;
1422 if (!FunctionType::isValidReturnType(RetTy))
1423 GEN_ERROR("Invalid result type for LLVM function");
Eric Christopher2a5196f2008-09-24 04:55:49 +00001424
Chris Lattner58af2a12006-02-15 07:22:58 +00001425 std::vector<const Type*> Params;
Reid Spencer7b5d4662007-04-09 06:16:21 +00001426 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001427 for (; I != E; ++I ) {
Reid Spencer66728ef2007-03-20 01:13:36 +00001428 const Type *Ty = I->Ty->get();
Reid Spencer66728ef2007-03-20 01:13:36 +00001429 Params.push_back(Ty);
Reid Spencer14310612006-12-31 05:40:51 +00001430 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001431
Chris Lattner58af2a12006-02-15 07:22:58 +00001432 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1433 if (isVarArg) Params.pop_back();
1434
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001435 for (unsigned i = 0; i != Params.size(); ++i)
1436 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1437 GEN_ERROR("Function arguments must be value types!");
1438
1439 CHECK_FOR_ERROR
1440
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001441 FunctionType *FT = FunctionType::get(RetTy, Params, isVarArg);
Reid Spencer14310612006-12-31 05:40:51 +00001442 delete $1; // Delete the return type handle
Eric Christopher2a5196f2008-09-24 04:55:49 +00001443 $$ = new PATypeHolder(HandleUpRefs(FT));
Nuno Lopes8a88a372008-10-05 16:49:34 +00001444
1445 // Delete the argument list
1446 for (I = $3->begin() ; I != E; ++I ) {
1447 delete I->Ty;
1448 }
1449 delete $3;
1450
Reid Spencer61c83e02006-08-18 08:43:06 +00001451 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001452 }
Reid Spencer218ded22007-01-05 17:07:23 +00001453 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001454 // Allow but ignore attributes on function types; this permits auto-upgrade.
1455 // FIXME: remove in LLVM 3.0.
Reid Spencer14310612006-12-31 05:40:51 +00001456 std::vector<const Type*> Params;
Reid Spencer7b5d4662007-04-09 06:16:21 +00001457 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001458 for ( ; I != E; ++I ) {
Reid Spencer66728ef2007-03-20 01:13:36 +00001459 const Type* Ty = I->Ty->get();
Reid Spencer66728ef2007-03-20 01:13:36 +00001460 Params.push_back(Ty);
Reid Spencer14310612006-12-31 05:40:51 +00001461 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001462
Reid Spencer14310612006-12-31 05:40:51 +00001463 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1464 if (isVarArg) Params.pop_back();
1465
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001466 for (unsigned i = 0; i != Params.size(); ++i)
1467 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1468 GEN_ERROR("Function arguments must be value types!");
1469
1470 CHECK_FOR_ERROR
1471
Duncan Sandsdc024672007-11-27 13:23:08 +00001472 FunctionType *FT = FunctionType::get($1, Params, isVarArg);
Eric Christopher2a5196f2008-09-24 04:55:49 +00001473 $$ = new PATypeHolder(HandleUpRefs(FT));
Nuno Lopes8a88a372008-10-05 16:49:34 +00001474
1475 // Delete the argument list
1476 for (I = $3->begin() ; I != E; ++I ) {
1477 delete I->Ty;
1478 }
1479 delete $3;
1480
Reid Spencer14310612006-12-31 05:40:51 +00001481 CHECK_FOR_ERROR
1482 }
1483
1484 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001485 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, $2)));
Reid Spencera132e042006-12-03 05:46:11 +00001486 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001487 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001488 }
Chris Lattner32980692007-02-19 07:44:24 +00001489 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencera132e042006-12-03 05:46:11 +00001490 const llvm::Type* ElemTy = $4->get();
1491 if ((unsigned)$2 != $2)
1492 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner42a75512007-01-15 02:27:26 +00001493 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencer9d6565a2007-02-15 02:26:10 +00001494 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001495 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencera132e042006-12-03 05:46:11 +00001496 delete $4;
1497 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001498 }
1499 | '{' TypeListI '}' { // Structure type?
1500 std::vector<const Type*> Elements;
Reid Spencera132e042006-12-03 05:46:11 +00001501 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattner58af2a12006-02-15 07:22:58 +00001502 E = $2->end(); I != E; ++I)
Reid Spencera132e042006-12-03 05:46:11 +00001503 Elements.push_back(*I);
Chris Lattner58af2a12006-02-15 07:22:58 +00001504
Reid Spencera132e042006-12-03 05:46:11 +00001505 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattner58af2a12006-02-15 07:22:58 +00001506 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001507 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001508 }
1509 | '{' '}' { // Empty structure type?
Reid Spencera132e042006-12-03 05:46:11 +00001510 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001511 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001512 }
Andrew Lenharth6353e052006-12-08 18:07:09 +00001513 | '<' '{' TypeListI '}' '>' {
1514 std::vector<const Type*> Elements;
1515 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1516 E = $3->end(); I != E; ++I)
1517 Elements.push_back(*I);
1518
1519 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1520 delete $3;
1521 CHECK_FOR_ERROR
1522 }
1523 | '<' '{' '}' '>' { // Empty structure type?
1524 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1525 CHECK_FOR_ERROR
1526 }
Reid Spencer14310612006-12-31 05:40:51 +00001527 ;
1528
Eric Christopher2a5196f2008-09-24 04:55:49 +00001529ArgType
Devang Patel05988662008-09-25 21:00:45 +00001530 : Types OptAttributes {
Duncan Sandsdc024672007-11-27 13:23:08 +00001531 // Allow but ignore attributes on function types; this permits auto-upgrade.
1532 // FIXME: remove in LLVM 3.0.
Eric Christopher2a5196f2008-09-24 04:55:49 +00001533 $$.Ty = $1;
Devang Patel05988662008-09-25 21:00:45 +00001534 $$.Attrs = Attribute::None;
Reid Spencer14310612006-12-31 05:40:51 +00001535 }
1536 ;
1537
Reid Spencer218ded22007-01-05 17:07:23 +00001538ResultTypes
1539 : Types {
Reid Spencer14310612006-12-31 05:40:51 +00001540 if (!UpRefs.empty())
1541 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Devang Patel20071732008-02-23 01:17:37 +00001542 if (!(*$1)->isFirstClassType() && !isa<StructType>($1->get()))
Reid Spencerb5334b02007-02-05 10:18:06 +00001543 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer218ded22007-01-05 17:07:23 +00001544 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00001545 }
Reid Spencer218ded22007-01-05 17:07:23 +00001546 | VOID {
1547 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencer14310612006-12-31 05:40:51 +00001548 }
1549 ;
1550
1551ArgTypeList : ArgType {
1552 $$ = new TypeWithAttrsList();
1553 $$->push_back($1);
1554 CHECK_FOR_ERROR
1555 }
1556 | ArgTypeList ',' ArgType {
1557 ($$=$1)->push_back($3);
1558 CHECK_FOR_ERROR
1559 }
1560 ;
1561
Eric Christopher2a5196f2008-09-24 04:55:49 +00001562ArgTypeListI
Reid Spencer14310612006-12-31 05:40:51 +00001563 : ArgTypeList
1564 | ArgTypeList ',' DOTDOTDOT {
1565 $$=$1;
Devang Patel05988662008-09-25 21:00:45 +00001566 TypeWithAttrs TWA; TWA.Attrs = Attribute::None;
Reid Spencer14310612006-12-31 05:40:51 +00001567 TWA.Ty = new PATypeHolder(Type::VoidTy);
1568 $$->push_back(TWA);
1569 CHECK_FOR_ERROR
1570 }
1571 | DOTDOTDOT {
1572 $$ = new TypeWithAttrsList;
Devang Patel05988662008-09-25 21:00:45 +00001573 TypeWithAttrs TWA; TWA.Attrs = Attribute::None;
Reid Spencer14310612006-12-31 05:40:51 +00001574 TWA.Ty = new PATypeHolder(Type::VoidTy);
1575 $$->push_back(TWA);
1576 CHECK_FOR_ERROR
1577 }
1578 | /*empty*/ {
1579 $$ = new TypeWithAttrsList();
Reid Spencer61c83e02006-08-18 08:43:06 +00001580 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001581 };
1582
Eric Christopher2a5196f2008-09-24 04:55:49 +00001583// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner58af2a12006-02-15 07:22:58 +00001584// declaration type lists
1585//
Reid Spencer14310612006-12-31 05:40:51 +00001586TypeListI : Types {
Reid Spencera132e042006-12-03 05:46:11 +00001587 $$ = new std::list<PATypeHolder>();
Eric Christopher2a5196f2008-09-24 04:55:49 +00001588 $$->push_back(*$1);
Reid Spencer66728ef2007-03-20 01:13:36 +00001589 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001590 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001591 }
Reid Spencer14310612006-12-31 05:40:51 +00001592 | TypeListI ',' Types {
Eric Christopher2a5196f2008-09-24 04:55:49 +00001593 ($$=$1)->push_back(*$3);
Reid Spencer66728ef2007-03-20 01:13:36 +00001594 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001595 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001596 };
1597
Chris Lattner58af2a12006-02-15 07:22:58 +00001598// ConstVal - The various declarations that go into the constant pool. This
1599// production is used ONLY to represent constants that show up AFTER a 'const',
1600// 'constant' or 'global' token at global scope. Constants that can be inlined
1601// into other expressions (such as integers and constexprs) are handled by the
1602// ResolvedVal, ValueRef and ConstValueRef productions.
1603//
1604ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001605 if (!UpRefs.empty())
1606 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001607 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001608 if (ATy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001609 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001610 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001611 const Type *ETy = ATy->getElementType();
Dan Gohman180c1692008-06-23 18:43:26 +00001612 uint64_t NumElements = ATy->getNumElements();
Chris Lattner58af2a12006-02-15 07:22:58 +00001613
1614 // Verify that we have the correct size...
Mon P Wang28873102008-06-25 08:15:39 +00001615 if (NumElements != uint64_t(-1) && NumElements != $3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001616 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Eric Christopher2a5196f2008-09-24 04:55:49 +00001617 utostr($3->size()) + " arguments, but has size of " +
Mon P Wang28873102008-06-25 08:15:39 +00001618 utostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001619
1620 // Verify all elements are correct type!
1621 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001622 if (ETy != (*$3)[i]->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001623 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001624 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001625 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001626 }
1627
Reid Spencera132e042006-12-03 05:46:11 +00001628 $$ = ConstantArray::get(ATy, *$3);
1629 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001630 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001631 }
1632 | Types '[' ']' {
Reid Spencer14310612006-12-31 05:40:51 +00001633 if (!UpRefs.empty())
1634 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001635 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001636 if (ATy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001637 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001638 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001639
Dan Gohman180c1692008-06-23 18:43:26 +00001640 uint64_t NumElements = ATy->getNumElements();
Eric Christopher2a5196f2008-09-24 04:55:49 +00001641 if (NumElements != uint64_t(-1) && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001642 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Mon P Wang28873102008-06-25 08:15:39 +00001643 " arguments, but has size of " + utostr(NumElements) +"");
Reid Spencera132e042006-12-03 05:46:11 +00001644 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1645 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001646 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001647 }
1648 | Types 'c' STRINGCONSTANT {
Reid Spencer14310612006-12-31 05:40:51 +00001649 if (!UpRefs.empty())
1650 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001651 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001652 if (ATy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001653 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001654 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001655
Dan Gohman180c1692008-06-23 18:43:26 +00001656 uint64_t NumElements = ATy->getNumElements();
Chris Lattner58af2a12006-02-15 07:22:58 +00001657 const Type *ETy = ATy->getElementType();
Mon P Wang28873102008-06-25 08:15:39 +00001658 if (NumElements != uint64_t(-1) && NumElements != $3->length())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001659 GEN_ERROR("Can't build string constant of size " +
Mon P Wang28873102008-06-25 08:15:39 +00001660 utostr($3->length()) +
1661 " when array has size " + utostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001662 std::vector<Constant*> Vals;
Reid Spencer14310612006-12-31 05:40:51 +00001663 if (ETy == Type::Int8Ty) {
Mon P Wang28873102008-06-25 08:15:39 +00001664 for (uint64_t i = 0; i < $3->length(); ++i)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001665 Vals.push_back(ConstantInt::get(ETy, (*$3)[i]));
Chris Lattner58af2a12006-02-15 07:22:58 +00001666 } else {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001667 delete $3;
Reid Spencerb5334b02007-02-05 10:18:06 +00001668 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner58af2a12006-02-15 07:22:58 +00001669 }
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001670 delete $3;
Reid Spencera132e042006-12-03 05:46:11 +00001671 $$ = ConstantArray::get(ATy, Vals);
1672 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001673 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001674 }
1675 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001676 if (!UpRefs.empty())
1677 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00001678 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001679 if (PTy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001680 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001681 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001682 const Type *ETy = PTy->getElementType();
Dan Gohman180c1692008-06-23 18:43:26 +00001683 unsigned NumElements = PTy->getNumElements();
Chris Lattner58af2a12006-02-15 07:22:58 +00001684
1685 // Verify that we have the correct size...
Mon P Wang28873102008-06-25 08:15:39 +00001686 if (NumElements != unsigned(-1) && NumElements != (unsigned)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001687 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Eric Christopher2a5196f2008-09-24 04:55:49 +00001688 utostr($3->size()) + " arguments, but has size of " +
Mon P Wang28873102008-06-25 08:15:39 +00001689 utostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001690
1691 // Verify all elements are correct type!
1692 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001693 if (ETy != (*$3)[i]->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001694 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001695 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001696 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001697 }
1698
Reid Spencer9d6565a2007-02-15 02:26:10 +00001699 $$ = ConstantVector::get(PTy, *$3);
Reid Spencera132e042006-12-03 05:46:11 +00001700 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001701 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001702 }
1703 | Types '{' ConstVector '}' {
Reid Spencera132e042006-12-03 05:46:11 +00001704 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001705 if (STy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001706 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001707 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001708
1709 if ($3->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001710 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001711
1712 // Check to ensure that constants are compatible with the type initializer!
1713 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencera132e042006-12-03 05:46:11 +00001714 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001715 GEN_ERROR("Expected type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001716 STy->getElementType(i)->getDescription() +
1717 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001718 " of structure initializer");
Chris Lattner58af2a12006-02-15 07:22:58 +00001719
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001720 // Check to ensure that Type is not packed
1721 if (STy->isPacked())
Chris Lattner6cdc6822007-04-26 05:31:05 +00001722 GEN_ERROR("Unpacked Initializer to vector type '" +
1723 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001724
Reid Spencera132e042006-12-03 05:46:11 +00001725 $$ = ConstantStruct::get(STy, *$3);
1726 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001727 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001728 }
1729 | Types '{' '}' {
Reid Spencer14310612006-12-31 05:40:51 +00001730 if (!UpRefs.empty())
1731 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001732 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001733 if (STy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001734 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001735 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001736
1737 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001738 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001739
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001740 // Check to ensure that Type is not packed
1741 if (STy->isPacked())
Chris Lattner6cdc6822007-04-26 05:31:05 +00001742 GEN_ERROR("Unpacked Initializer to vector type '" +
1743 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001744
1745 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1746 delete $1;
1747 CHECK_FOR_ERROR
1748 }
1749 | Types '<' '{' ConstVector '}' '>' {
1750 const StructType *STy = dyn_cast<StructType>($1->get());
1751 if (STy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001752 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001753 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001754
1755 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001756 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001757
1758 // Check to ensure that constants are compatible with the type initializer!
1759 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1760 if ((*$4)[i]->getType() != STy->getElementType(i))
1761 GEN_ERROR("Expected type '" +
1762 STy->getElementType(i)->getDescription() +
1763 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001764 " of structure initializer");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001765
1766 // Check to ensure that Type is packed
1767 if (!STy->isPacked())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001768 GEN_ERROR("Vector initializer to non-vector type '" +
Chris Lattner32980692007-02-19 07:44:24 +00001769 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001770
1771 $$ = ConstantStruct::get(STy, *$4);
1772 delete $1; delete $4;
1773 CHECK_FOR_ERROR
1774 }
1775 | Types '<' '{' '}' '>' {
1776 if (!UpRefs.empty())
1777 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1778 const StructType *STy = dyn_cast<StructType>($1->get());
1779 if (STy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001780 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001781 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001782
1783 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001784 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001785
1786 // Check to ensure that Type is packed
1787 if (!STy->isPacked())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001788 GEN_ERROR("Vector initializer to non-vector type '" +
Chris Lattner32980692007-02-19 07:44:24 +00001789 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001790
Reid Spencera132e042006-12-03 05:46:11 +00001791 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1792 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001793 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001794 }
1795 | Types NULL_TOK {
Reid Spencer14310612006-12-31 05:40:51 +00001796 if (!UpRefs.empty())
1797 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001798 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001799 if (PTy == 0)
Eric Christopher2a5196f2008-09-24 04:55:49 +00001800 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001801 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001802
Reid Spencera132e042006-12-03 05:46:11 +00001803 $$ = ConstantPointerNull::get(PTy);
1804 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001805 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001806 }
1807 | Types UNDEF {
Reid Spencer14310612006-12-31 05:40:51 +00001808 if (!UpRefs.empty())
1809 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001810 $$ = UndefValue::get($1->get());
1811 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001812 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001813 }
1814 | Types SymbolicValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00001815 if (!UpRefs.empty())
1816 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001817 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001818 if (Ty == 0)
Devang Patel5a970972008-02-19 22:27:01 +00001819 GEN_ERROR("Global const reference must be a pointer type " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001820
1821 // ConstExprs can exist in the body of a function, thus creating
1822 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencer93c40032007-03-19 18:40:50 +00001823 // the context of a function, getExistingVal will search the functions
Chris Lattner58af2a12006-02-15 07:22:58 +00001824 // symbol table instead of the module symbol table for the global symbol,
1825 // which throws things all off. To get around this, we just tell
Reid Spencer93c40032007-03-19 18:40:50 +00001826 // getExistingVal that we are at global scope here.
Chris Lattner58af2a12006-02-15 07:22:58 +00001827 //
1828 Function *SavedCurFn = CurFun.CurrentFunction;
1829 CurFun.CurrentFunction = 0;
1830
Reid Spencer93c40032007-03-19 18:40:50 +00001831 Value *V = getExistingVal(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001832 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001833
1834 CurFun.CurrentFunction = SavedCurFn;
1835
1836 // If this is an initializer for a constant pointer, which is referencing a
1837 // (currently) undefined variable, create a stub now that shall be replaced
1838 // in the future with the right type of variable.
1839 //
1840 if (V == 0) {
Reid Spencera9720f52007-02-05 17:04:00 +00001841 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001842 const PointerType *PT = cast<PointerType>(Ty);
1843
1844 // First check to see if the forward references value is already created!
1845 PerModuleInfo::GlobalRefsType::iterator I =
1846 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Eric Christopher2a5196f2008-09-24 04:55:49 +00001847
Chris Lattner58af2a12006-02-15 07:22:58 +00001848 if (I != CurModule.GlobalRefs.end()) {
1849 V = I->second; // Placeholder already exists, use it...
1850 $2.destroy();
1851 } else {
1852 std::string Name;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001853 if ($2.Type == ValID::GlobalName)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001854 Name = $2.getName();
Reid Spencer41dff5e2007-01-26 08:05:27 +00001855 else if ($2.Type != ValID::GlobalID)
1856 GEN_ERROR("Invalid reference to global");
Chris Lattner58af2a12006-02-15 07:22:58 +00001857
1858 // Create the forward referenced global.
1859 GlobalValue *GV;
Eric Christopher2a5196f2008-09-24 04:55:49 +00001860 if (const FunctionType *FTy =
Chris Lattner58af2a12006-02-15 07:22:58 +00001861 dyn_cast<FunctionType>(PT->getElementType())) {
Gabor Greife64d2482008-04-06 23:07:54 +00001862 GV = Function::Create(FTy, GlobalValue::ExternalWeakLinkage, Name,
1863 CurModule.CurrentModule);
Chris Lattner58af2a12006-02-15 07:22:58 +00001864 } else {
1865 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattner6cdc6822007-04-26 05:31:05 +00001866 GlobalValue::ExternalWeakLinkage, 0,
Chris Lattner58af2a12006-02-15 07:22:58 +00001867 Name, CurModule.CurrentModule);
1868 }
1869
1870 // Keep track of the fact that we have a forward ref to recycle it
1871 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1872 V = GV;
1873 }
1874 }
1875
Reid Spencera132e042006-12-03 05:46:11 +00001876 $$ = cast<GlobalValue>(V);
1877 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001878 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001879 }
1880 | Types ConstExpr {
Reid Spencer14310612006-12-31 05:40:51 +00001881 if (!UpRefs.empty())
1882 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001883 if ($1->get() != $2->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00001884 GEN_ERROR("Mismatched types for constant expression: " +
Reid Spencere68853b2007-01-04 00:06:14 +00001885 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001886 $$ = $2;
Reid Spencera132e042006-12-03 05:46:11 +00001887 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001888 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001889 }
1890 | Types ZEROINITIALIZER {
Reid Spencer14310612006-12-31 05:40:51 +00001891 if (!UpRefs.empty())
1892 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001893 const Type *Ty = $1->get();
Chris Lattner58af2a12006-02-15 07:22:58 +00001894 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerb5334b02007-02-05 10:18:06 +00001895 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencera132e042006-12-03 05:46:11 +00001896 $$ = Constant::getNullValue(Ty);
1897 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001898 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00001899 }
Chris Lattner740e7092008-10-15 06:16:57 +00001900 | Types ESINT64VAL { // integral constants
1901 if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) {
1902 if (!ConstantInt::isValueValidForType(IT, $2))
1903 GEN_ERROR("Constant value doesn't fit in type");
1904 $$ = ConstantInt::get(IT, $2, true);
1905 } else {
1906 GEN_ERROR("integer constant must have integer type");
1907 }
1908 delete $1;
Reid Spencer38c91a92007-02-28 02:24:54 +00001909 CHECK_FOR_ERROR
1910 }
Chris Lattner740e7092008-10-15 06:16:57 +00001911 | Types ESAPINTVAL { // arbitrary precision integer constants
1912 if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) {
1913 if ($2->getBitWidth() > IT->getBitWidth())
1914 GEN_ERROR("Constant value does not fit in type");
1915 $2->sextOrTrunc(IT->getBitWidth());
1916 $$ = ConstantInt::get(*$2);
1917 } else {
1918 GEN_ERROR("integer constant must have integer type");
Reid Spencer10794272007-03-01 19:41:47 +00001919 }
Chris Lattner740e7092008-10-15 06:16:57 +00001920 delete $1;
Reid Spencer38c91a92007-02-28 02:24:54 +00001921 delete $2;
Reid Spencere4d87aa2006-12-23 06:05:41 +00001922 CHECK_FOR_ERROR
1923 }
Chris Lattner740e7092008-10-15 06:16:57 +00001924 | Types EUINT64VAL { // integral constants
1925 if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) {
1926 if (!ConstantInt::isValueValidForType(IT, $2))
1927 GEN_ERROR("Constant value doesn't fit in type");
1928 $$ = ConstantInt::get(IT, $2, false);
1929 } else {
1930 GEN_ERROR("integer constant must have integer type");
Eric Christopher2a5196f2008-09-24 04:55:49 +00001931 }
Chris Lattner740e7092008-10-15 06:16:57 +00001932 delete $1;
Reid Spencere4d87aa2006-12-23 06:05:41 +00001933 CHECK_FOR_ERROR
1934 }
Chris Lattner740e7092008-10-15 06:16:57 +00001935 | Types EUAPINTVAL { // arbitrary precision integer constants
1936 if (IntegerType *IT = dyn_cast<IntegerType>($1->get())) {
1937 if ($2->getBitWidth() > IT->getBitWidth())
1938 GEN_ERROR("Constant value does not fit in type");
1939 $2->zextOrTrunc(IT->getBitWidth());
1940 $$ = ConstantInt::get(*$2);
1941 } else {
1942 GEN_ERROR("integer constant must have integer type");
1943 }
1944
1945 delete $2;
1946 delete $1;
1947 CHECK_FOR_ERROR
1948 }
1949 | Types TRUETOK { // Boolean constants
1950 if ($1->get() != Type::Int1Ty)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001951 GEN_ERROR("Constant true must have type i1");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001952 $$ = ConstantInt::getTrue();
Chris Lattner740e7092008-10-15 06:16:57 +00001953 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001954 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001955 }
Chris Lattner740e7092008-10-15 06:16:57 +00001956 | Types FALSETOK { // Boolean constants
1957 if ($1->get() != Type::Int1Ty)
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001958 GEN_ERROR("Constant false must have type i1");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001959 $$ = ConstantInt::getFalse();
Chris Lattner740e7092008-10-15 06:16:57 +00001960 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001961 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001962 }
Chris Lattner740e7092008-10-15 06:16:57 +00001963 | Types FPVAL { // Floating point constants
1964 if (!ConstantFP::isValueValidForType($1->get(), *$2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001965 GEN_ERROR("Floating point constant invalid for type");
Chris Lattner740e7092008-10-15 06:16:57 +00001966
Eric Christopher2a5196f2008-09-24 04:55:49 +00001967 // Lexer has no type info, so builds all float and double FP constants
Dale Johannesenc72cd7e2007-09-11 18:33:39 +00001968 // as double. Fix this here. Long double is done right.
Chris Lattner740e7092008-10-15 06:16:57 +00001969 if (&$2->getSemantics()==&APFloat::IEEEdouble && $1->get()==Type::FloatTy) {
Dale Johannesen236bbd42008-10-09 23:01:34 +00001970 bool ignored;
1971 $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
1972 &ignored);
1973 }
Chris Lattnerd8eb63f2008-04-20 00:41:19 +00001974 $$ = ConstantFP::get(*$2);
Chris Lattner740e7092008-10-15 06:16:57 +00001975 delete $1;
Dale Johannesencdd509a2007-09-07 21:07:57 +00001976 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001977 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001978 };
1979
1980
Reid Spencer3da59db2006-11-27 01:05:10 +00001981ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer14310612006-12-31 05:40:51 +00001982 if (!UpRefs.empty())
1983 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001984 Constant *Val = $3;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001985 const Type *DestTy = $5->get();
1986 if (!CastInst::castIsValid($1, $3, DestTy))
1987 GEN_ERROR("invalid cast opcode for cast from '" +
1988 Val->getType()->getDescription() + "' to '" +
Eric Christopher2a5196f2008-09-24 04:55:49 +00001989 DestTy->getDescription() + "'");
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001990 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00001991 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00001992 }
1993 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001994 if (!isa<PointerType>($3->getType()))
Reid Spencerb5334b02007-02-05 10:18:06 +00001995 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00001996
Reid Spencera132e042006-12-03 05:46:11 +00001997 const Type *IdxTy =
Dan Gohman041e2eb2008-05-15 19:50:34 +00001998 GetElementPtrInst::getIndexedType($3->getType(), $4->begin(), $4->end());
Reid Spencera132e042006-12-03 05:46:11 +00001999 if (!IdxTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00002000 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencera132e042006-12-03 05:46:11 +00002001
Chris Lattnerf7469af2007-01-31 04:44:08 +00002002 SmallVector<Constant*, 8> IdxVec;
Reid Spencera132e042006-12-03 05:46:11 +00002003 for (unsigned i = 0, e = $4->size(); i != e; ++i)
2004 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattner58af2a12006-02-15 07:22:58 +00002005 IdxVec.push_back(C);
2006 else
Reid Spencerb5334b02007-02-05 10:18:06 +00002007 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattner58af2a12006-02-15 07:22:58 +00002008
2009 delete $4;
2010
Chris Lattnerf7469af2007-01-31 04:44:08 +00002011 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer61c83e02006-08-18 08:43:06 +00002012 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002013 }
2014 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer4fe16d62007-01-11 18:21:29 +00002015 if ($3->getType() != Type::Int1Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00002016 GEN_ERROR("Select condition must be of boolean type");
Reid Spencera132e042006-12-03 05:46:11 +00002017 if ($5->getType() != $7->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002018 GEN_ERROR("Select operand types must match");
Reid Spencera132e042006-12-03 05:46:11 +00002019 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00002020 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002021 }
2022 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00002023 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002024 GEN_ERROR("Binary operator types must match");
Reid Spencer1628cec2006-10-26 06:15:43 +00002025 CHECK_FOR_ERROR;
Reid Spencer9eef56f2006-12-05 19:16:11 +00002026 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner58af2a12006-02-15 07:22:58 +00002027 }
2028 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00002029 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002030 GEN_ERROR("Logical operator types must match");
Chris Lattner42a75512007-01-15 02:27:26 +00002031 if (!$3->getType()->isInteger()) {
Eric Christopher2a5196f2008-09-24 04:55:49 +00002032 if (!isa<VectorType>($3->getType()) ||
Reid Spencer9d6565a2007-02-15 02:26:10 +00002033 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00002034 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00002035 }
Reid Spencera132e042006-12-03 05:46:11 +00002036 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002037 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002038 }
Reid Spencer4012e832006-12-04 05:24:24 +00002039 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
2040 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002041 GEN_ERROR("icmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00002042 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00002043 }
Reid Spencer4012e832006-12-04 05:24:24 +00002044 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
2045 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002046 GEN_ERROR("fcmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00002047 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00002048 }
Nate Begemanac80ade2008-05-12 19:01:56 +00002049 | VICMP IPredicates '(' ConstVal ',' ConstVal ')' {
2050 if ($4->getType() != $6->getType())
2051 GEN_ERROR("vicmp operand types must match");
2052 $$ = ConstantExpr::getVICmp($2, $4, $6);
2053 }
2054 | VFCMP FPredicates '(' ConstVal ',' ConstVal ')' {
2055 if ($4->getType() != $6->getType())
2056 GEN_ERROR("vfcmp operand types must match");
2057 $$ = ConstantExpr::getVFCmp($2, $4, $6);
2058 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002059 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00002060 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerb5334b02007-02-05 10:18:06 +00002061 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00002062 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002063 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00002064 }
2065 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00002066 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00002067 GEN_ERROR("Invalid insertelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00002068 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00002069 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00002070 }
2071 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00002072 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00002073 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00002074 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00002075 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00002076 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002077 | EXTRACTVALUE '(' ConstVal ConstantIndexList ')' {
Dan Gohmane4977cf2008-05-23 01:55:30 +00002078 if (!isa<StructType>($3->getType()) && !isa<ArrayType>($3->getType()))
2079 GEN_ERROR("ExtractValue requires an aggregate operand");
2080
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002081 $$ = ConstantExpr::getExtractValue($3, &(*$4)[0], $4->size());
Dan Gohmane4977cf2008-05-23 01:55:30 +00002082 delete $4;
Dan Gohmane4977cf2008-05-23 01:55:30 +00002083 CHECK_FOR_ERROR
2084 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002085 | INSERTVALUE '(' ConstVal ',' ConstVal ConstantIndexList ')' {
Dan Gohmane4977cf2008-05-23 01:55:30 +00002086 if (!isa<StructType>($3->getType()) && !isa<ArrayType>($3->getType()))
2087 GEN_ERROR("InsertValue requires an aggregate operand");
2088
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002089 $$ = ConstantExpr::getInsertValue($3, $5, &(*$6)[0], $6->size());
Dan Gohmane4977cf2008-05-23 01:55:30 +00002090 delete $6;
Dan Gohmane4977cf2008-05-23 01:55:30 +00002091 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002092 };
2093
Chris Lattnerd25db202006-04-08 03:55:17 +00002094
Chris Lattner58af2a12006-02-15 07:22:58 +00002095// ConstVector - A list of comma separated constants.
2096ConstVector : ConstVector ',' ConstVal {
2097 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002098 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002099 }
2100 | ConstVal {
Reid Spencera132e042006-12-03 05:46:11 +00002101 $$ = new std::vector<Constant*>();
Chris Lattner58af2a12006-02-15 07:22:58 +00002102 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002103 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002104 };
2105
2106
2107// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
2108GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
2109
Eric Christopher2a5196f2008-09-24 04:55:49 +00002110// ThreadLocal
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002111ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
2112
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002113// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
2114AliaseeRef : ResultTypes SymbolicValueRef {
2115 const Type* VTy = $1->get();
2116 Value *V = getVal(VTy, $2);
Chris Lattner0275cff2007-08-06 21:00:46 +00002117 CHECK_FOR_ERROR
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002118 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
2119 if (!Aliasee)
2120 GEN_ERROR("Aliases can be created only to global values");
2121
2122 $$ = Aliasee;
2123 CHECK_FOR_ERROR
2124 delete $1;
2125 }
2126 | BITCAST '(' AliaseeRef TO Types ')' {
2127 Constant *Val = $3;
2128 const Type *DestTy = $5->get();
2129 if (!CastInst::castIsValid($1, $3, DestTy))
2130 GEN_ERROR("invalid cast opcode for cast from '" +
2131 Val->getType()->getDescription() + "' to '" +
2132 DestTy->getDescription() + "'");
Eric Christopher2a5196f2008-09-24 04:55:49 +00002133
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002134 $$ = ConstantExpr::getCast($1, $3, DestTy);
2135 CHECK_FOR_ERROR
2136 delete $5;
2137 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002138
2139//===----------------------------------------------------------------------===//
2140// Rules to match Modules
2141//===----------------------------------------------------------------------===//
2142
2143// Module rule: Capture the result of parsing the whole file into a result
2144// variable...
2145//
Eric Christopher2a5196f2008-09-24 04:55:49 +00002146Module
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002147 : DefinitionList {
2148 $$ = ParserResult = CurModule.CurrentModule;
2149 CurModule.ModuleDone();
2150 CHECK_FOR_ERROR;
2151 }
2152 | /*empty*/ {
2153 $$ = ParserResult = CurModule.CurrentModule;
2154 CurModule.ModuleDone();
2155 CHECK_FOR_ERROR;
2156 }
2157 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002158
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002159DefinitionList
2160 : Definition
2161 | DefinitionList Definition
2162 ;
2163
Eric Christopher2a5196f2008-09-24 04:55:49 +00002164Definition
Jeff Cohen361c3ef2007-01-21 19:19:31 +00002165 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner58af2a12006-02-15 07:22:58 +00002166 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00002167 CHECK_FOR_ERROR
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002168 }
2169 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer61c83e02006-08-18 08:43:06 +00002170 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002171 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002172 | MODULE ASM_TOK AsmBlock {
Reid Spencer61c83e02006-08-18 08:43:06 +00002173 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00002174 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002175 | OptLocalAssign TYPE Types {
Reid Spencer14310612006-12-31 05:40:51 +00002176 if (!UpRefs.empty())
2177 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002178 // Eagerly resolve types. This is not an optimization, this is a
2179 // requirement that is due to the fact that we could have this:
2180 //
2181 // %list = type { %list * }
2182 // %list = type { %list * } ; repeated type decl
2183 //
2184 // If types are not resolved eagerly, then the two types will not be
2185 // determined to be the same type!
2186 //
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002187 ResolveTypeTo($1, *$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002188
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002189 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002190 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002191 // If this is a named type that is not a redefinition, add it to the slot
2192 // table.
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002193 CurModule.Types.push_back(*$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002194 }
Reid Spencera132e042006-12-03 05:46:11 +00002195
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002196 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002197 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002198 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002199 | OptLocalAssign TYPE VOID {
Reid Spencer14310612006-12-31 05:40:51 +00002200 ResolveTypeTo($1, $3);
2201
2202 if (!setTypeName($3, $1) && !$1) {
2203 CHECK_FOR_ERROR
2204 // If this is a named type that is not a redefinition, add it to the slot
2205 // table.
2206 CurModule.Types.push_back($3);
2207 }
2208 CHECK_FOR_ERROR
2209 }
Eric Christopher2a5196f2008-09-24 04:55:49 +00002210 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal
2211 OptAddrSpace {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002212 /* "Externally Visible" Linkage */
Eric Christopher2a5196f2008-09-24 04:55:49 +00002213 if ($5 == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002214 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002215 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
Christopher Lambbf3348d2007-12-12 08:45:45 +00002216 $2, $4, $5->getType(), $5, $3, $6);
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00002217 CHECK_FOR_ERROR
2218 } GlobalVarAttributes {
2219 CurGV = 0;
2220 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002221 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambbf3348d2007-12-12 08:45:45 +00002222 ConstVal OptAddrSpace {
Eric Christopher2a5196f2008-09-24 04:55:49 +00002223 if ($6 == 0)
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002224 GEN_ERROR("Global value initializer is not a constant");
Christopher Lambbf3348d2007-12-12 08:45:45 +00002225 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4, $7);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002226 CHECK_FOR_ERROR
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002227 } GlobalVarAttributes {
2228 CurGV = 0;
2229 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002230 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambbf3348d2007-12-12 08:45:45 +00002231 Types OptAddrSpace {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002232 if (!UpRefs.empty())
2233 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
Christopher Lambbf3348d2007-12-12 08:45:45 +00002234 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4, $7);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002235 CHECK_FOR_ERROR
2236 delete $6;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002237 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002238 CurGV = 0;
2239 CHECK_FOR_ERROR
2240 }
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002241 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002242 std::string Name;
2243 if ($1) {
2244 Name = *$1;
2245 delete $1;
2246 }
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002247 if (Name.empty())
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002248 GEN_ERROR("Alias name cannot be empty");
Eric Christopher2a5196f2008-09-24 04:55:49 +00002249
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002250 Constant* Aliasee = $5;
2251 if (Aliasee == 0)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002252 GEN_ERROR(std::string("Invalid aliasee for alias: ") + Name);
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002253
2254 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2255 CurModule.CurrentModule);
2256 GA->setVisibility($2);
2257 InsertValue(GA, CurModule.Values);
Eric Christopher2a5196f2008-09-24 04:55:49 +00002258
2259
Chris Lattner569f7372007-09-10 23:24:14 +00002260 // If there was a forward reference of this alias, resolve it now.
Eric Christopher2a5196f2008-09-24 04:55:49 +00002261
Chris Lattner569f7372007-09-10 23:24:14 +00002262 ValID ID;
2263 if (!Name.empty())
2264 ID = ValID::createGlobalName(Name);
2265 else
2266 ID = ValID::createGlobalID(CurModule.Values.size()-1);
Eric Christopher2a5196f2008-09-24 04:55:49 +00002267
Chris Lattner569f7372007-09-10 23:24:14 +00002268 if (GlobalValue *FWGV =
2269 CurModule.GetForwardRefForGlobal(GA->getType(), ID)) {
2270 // Replace uses of the fwdref with the actual alias.
2271 FWGV->replaceAllUsesWith(GA);
2272 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(FWGV))
2273 GV->eraseFromParent();
2274 else
2275 cast<Function>(FWGV)->eraseFromParent();
2276 }
2277 ID.destroy();
Eric Christopher2a5196f2008-09-24 04:55:49 +00002278
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002279 CHECK_FOR_ERROR
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002280 }
Eric Christopher2a5196f2008-09-24 04:55:49 +00002281 | TARGET TargetDefinition {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002282 CHECK_FOR_ERROR
2283 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002284 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00002285 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002286 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002287 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002288
2289
2290AsmBlock : STRINGCONSTANT {
2291 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattner58af2a12006-02-15 07:22:58 +00002292 if (AsmSoFar.empty())
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002293 CurModule.CurrentModule->setModuleInlineAsm(*$1);
Chris Lattner58af2a12006-02-15 07:22:58 +00002294 else
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002295 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+*$1);
2296 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002297 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002298};
2299
Reid Spencer41dff5e2007-01-26 08:05:27 +00002300TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002301 CurModule.CurrentModule->setTargetTriple(*$3);
2302 delete $3;
John Criswell2f6a8b12006-10-24 19:09:48 +00002303 }
Chris Lattner1ae022f2006-10-22 06:08:13 +00002304 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002305 CurModule.CurrentModule->setDataLayout(*$3);
2306 delete $3;
Owen Anderson1dc69692006-10-18 02:21:48 +00002307 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002308
2309LibrariesDefinition : '[' LibList ']';
2310
2311LibList : LibList ',' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002312 CurModule.CurrentModule->addLibrary(*$3);
2313 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002314 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002315 }
2316 | STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002317 CurModule.CurrentModule->addLibrary(*$1);
2318 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002319 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002320 }
2321 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00002322 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002323 }
2324 ;
2325
2326//===----------------------------------------------------------------------===//
2327// Rules to match Function Headers
2328//===----------------------------------------------------------------------===//
2329
Devang Patel05988662008-09-25 21:00:45 +00002330ArgListH : ArgListH ',' Types OptAttributes OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002331 if (!UpRefs.empty())
2332 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002333 if (!(*$3)->isFirstClassType())
2334 GEN_ERROR("Argument types must be first-class");
Reid Spencer14310612006-12-31 05:40:51 +00002335 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00002336 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002337 $1->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002338 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002339 }
Devang Patel05988662008-09-25 21:00:45 +00002340 | Types OptAttributes OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002341 if (!UpRefs.empty())
2342 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002343 if (!(*$1)->isFirstClassType())
2344 GEN_ERROR("Argument types must be first-class");
Reid Spencer14310612006-12-31 05:40:51 +00002345 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2346 $$ = new ArgListType;
2347 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002348 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002349 };
2350
2351ArgList : ArgListH {
2352 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002353 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002354 }
2355 | ArgListH ',' DOTDOTDOT {
2356 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002357 struct ArgListEntry E;
2358 E.Ty = new PATypeHolder(Type::VoidTy);
2359 E.Name = 0;
Devang Patel05988662008-09-25 21:00:45 +00002360 E.Attrs = Attribute::None;
Reid Spencer14310612006-12-31 05:40:51 +00002361 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002362 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002363 }
2364 | DOTDOTDOT {
Reid Spencer14310612006-12-31 05:40:51 +00002365 $$ = new ArgListType;
2366 struct ArgListEntry E;
2367 E.Ty = new PATypeHolder(Type::VoidTy);
2368 E.Name = 0;
Devang Patel05988662008-09-25 21:00:45 +00002369 E.Attrs = Attribute::None;
Reid Spencer14310612006-12-31 05:40:51 +00002370 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002371 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002372 }
2373 | /* empty */ {
2374 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00002375 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002376 };
2377
Devang Patel652203f2008-09-29 20:49:50 +00002378FunctionHeaderH : OptCallingConv OptRetAttrs ResultTypes GlobalName '(' ArgList ')'
Devang Patel2c9c3e72008-09-26 23:51:19 +00002379 OptFuncAttrs OptSection OptAlign OptGC {
Devang Patel652203f2008-09-29 20:49:50 +00002380 std::string FunctionName(*$4);
2381 delete $4; // Free strdup'd memory!
Eric Christopher2a5196f2008-09-24 04:55:49 +00002382
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002383 // Check the function result for abstractness if this is a define. We should
2384 // have no abstract types at this point
Devang Patel652203f2008-09-29 20:49:50 +00002385 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($3))
2386 GEN_ERROR("Reference to abstract result: "+ $3->get()->getDescription());
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002387
Devang Patel652203f2008-09-29 20:49:50 +00002388 if (!FunctionType::isValidReturnType(*$3))
Chris Lattnera925a142008-04-23 05:37:08 +00002389 GEN_ERROR("Invalid result type for LLVM function");
Eric Christopher2a5196f2008-09-24 04:55:49 +00002390
Chris Lattner58af2a12006-02-15 07:22:58 +00002391 std::vector<const Type*> ParamTypeList;
Devang Patel05988662008-09-25 21:00:45 +00002392 SmallVector<AttributeWithIndex, 8> Attrs;
Devang Patel19c87462008-09-26 22:53:05 +00002393 //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
2394 //attributes.
Devang Patel652203f2008-09-29 20:49:50 +00002395 Attributes RetAttrs = $2;
2396 if ($8 != Attribute::None) {
2397 if ($8 & Attribute::ZExt) {
Devang Patel19c87462008-09-26 22:53:05 +00002398 RetAttrs = RetAttrs | Attribute::ZExt;
Devang Patel652203f2008-09-29 20:49:50 +00002399 $8 = $8 ^ Attribute::ZExt;
Devang Patel19c87462008-09-26 22:53:05 +00002400 }
Devang Patel652203f2008-09-29 20:49:50 +00002401 if ($8 & Attribute::SExt) {
Devang Patel19c87462008-09-26 22:53:05 +00002402 RetAttrs = RetAttrs | Attribute::SExt;
Devang Patel652203f2008-09-29 20:49:50 +00002403 $8 = $8 ^ Attribute::SExt;
Devang Patel19c87462008-09-26 22:53:05 +00002404 }
Devang Patel652203f2008-09-29 20:49:50 +00002405 if ($8 & Attribute::InReg) {
Devang Patel19c87462008-09-26 22:53:05 +00002406 RetAttrs = RetAttrs | Attribute::InReg;
Devang Patel652203f2008-09-29 20:49:50 +00002407 $8 = $8 ^ Attribute::InReg;
Devang Patel19c87462008-09-26 22:53:05 +00002408 }
Devang Patel19c87462008-09-26 22:53:05 +00002409 }
Devang Patel652203f2008-09-29 20:49:50 +00002410 if (RetAttrs != Attribute::None)
2411 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
2412 if ($6) { // If there are arguments...
Reid Spencer7b5d4662007-04-09 06:16:21 +00002413 unsigned index = 1;
Devang Patel652203f2008-09-29 20:49:50 +00002414 for (ArgListType::iterator I = $6->begin(); I != $6->end(); ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002415 const Type* Ty = I->Ty->get();
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002416 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2417 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencer14310612006-12-31 05:40:51 +00002418 ParamTypeList.push_back(Ty);
Devang Patel05988662008-09-25 21:00:45 +00002419 if (Ty != Type::VoidTy && I->Attrs != Attribute::None)
2420 Attrs.push_back(AttributeWithIndex::get(index, I->Attrs));
Reid Spencer14310612006-12-31 05:40:51 +00002421 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002422 }
Devang Patel652203f2008-09-29 20:49:50 +00002423 if ($8 != Attribute::None)
2424 Attrs.push_back(AttributeWithIndex::get(~0, $8));
Chris Lattner58af2a12006-02-15 07:22:58 +00002425
2426 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2427 if (isVarArg) ParamTypeList.pop_back();
2428
Devang Patel05988662008-09-25 21:00:45 +00002429 AttrListPtr PAL;
Christopher Lamb5c104242007-04-22 20:09:11 +00002430 if (!Attrs.empty())
Devang Patel05988662008-09-25 21:00:45 +00002431 PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Reid Spencer7b5d4662007-04-09 06:16:21 +00002432
Devang Patel652203f2008-09-29 20:49:50 +00002433 FunctionType *FT = FunctionType::get(*$3, ParamTypeList, isVarArg);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002434 const PointerType *PFT = PointerType::getUnqual(FT);
Devang Patel652203f2008-09-29 20:49:50 +00002435 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00002436
2437 ValID ID;
2438 if (!FunctionName.empty()) {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002439 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattner58af2a12006-02-15 07:22:58 +00002440 } else {
Reid Spencer93c40032007-03-19 18:40:50 +00002441 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002442 }
2443
2444 Function *Fn = 0;
2445 // See if this function was forward referenced. If so, recycle the object.
2446 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
Eric Christopher2a5196f2008-09-24 04:55:49 +00002447 // Move the function to the end of the list, from whereever it was
Chris Lattner58af2a12006-02-15 07:22:58 +00002448 // previously inserted.
2449 Fn = cast<Function>(FWRef);
Devang Patel05988662008-09-25 21:00:45 +00002450 assert(Fn->getAttributes().isEmpty() &&
Chris Lattner58d74912008-03-12 17:45:29 +00002451 "Forward reference has parameter attributes!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002452 CurModule.CurrentModule->getFunctionList().remove(Fn);
2453 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2454 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spenceref9b9a72007-02-05 20:47:22 +00002455 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002456 if (Fn->getFunctionType() != FT ) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002457 // The existing function doesn't have the same type. This is an overload
2458 // error.
2459 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Devang Patel05988662008-09-25 21:00:45 +00002460 } else if (Fn->getAttributes() != PAL) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002461 // The existing function doesn't have the same parameter attributes.
2462 // This is an overload error.
2463 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002464 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
Chris Lattner6cdc6822007-04-26 05:31:05 +00002465 // Neither the existing or the current function is a declaration and they
2466 // have the same name and same type. Clearly this is a redefinition.
2467 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Duncan Sandsdc024672007-11-27 13:23:08 +00002468 } else if (Fn->isDeclaration()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002469 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattner58af2a12006-02-15 07:22:58 +00002470 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2471 AI != AE; ++AI)
2472 AI->setName("");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002473 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002474 } else { // Not already defined?
Gabor Greife64d2482008-04-06 23:07:54 +00002475 Fn = Function::Create(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
2476 CurModule.CurrentModule);
Chris Lattner58af2a12006-02-15 07:22:58 +00002477 InsertValue(Fn, CurModule.Values);
2478 }
2479
Nuno Lopes9e9631d2008-10-03 15:45:58 +00002480 ID.destroy();
Chris Lattner58af2a12006-02-15 07:22:58 +00002481 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002482
2483 if (CurFun.isDeclare) {
2484 // If we have declaration, always overwrite linkage. This will allow us to
2485 // correctly handle cases, when pointer to function is passed as argument to
2486 // another function.
2487 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002488 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002489 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002490 Fn->setCallingConv($1);
Devang Patel05988662008-09-25 21:00:45 +00002491 Fn->setAttributes(PAL);
Devang Patel652203f2008-09-29 20:49:50 +00002492 Fn->setAlignment($10);
2493 if ($9) {
2494 Fn->setSection(*$9);
2495 delete $9;
Chris Lattner58af2a12006-02-15 07:22:58 +00002496 }
Devang Patel652203f2008-09-29 20:49:50 +00002497 if ($11) {
2498 Fn->setGC($11->c_str());
2499 delete $11;
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002500 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002501
2502 // Add all of the arguments we parsed to the function...
Devang Patel652203f2008-09-29 20:49:50 +00002503 if ($6) { // Is null if empty...
Chris Lattner58af2a12006-02-15 07:22:58 +00002504 if (isVarArg) { // Nuke the last entry
Devang Patel652203f2008-09-29 20:49:50 +00002505 assert($6->back().Ty->get() == Type::VoidTy && $6->back().Name == 0 &&
Reid Spencera9720f52007-02-05 17:04:00 +00002506 "Not a varargs marker!");
Devang Patel652203f2008-09-29 20:49:50 +00002507 delete $6->back().Ty;
2508 $6->pop_back(); // Delete the last entry
Chris Lattner58af2a12006-02-15 07:22:58 +00002509 }
2510 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002511 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencer14310612006-12-31 05:40:51 +00002512 unsigned Idx = 1;
Devang Patel652203f2008-09-29 20:49:50 +00002513 for (ArgListType::iterator I = $6->begin();
2514 I != $6->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencer14310612006-12-31 05:40:51 +00002515 delete I->Ty; // Delete the typeholder...
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002516 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002517 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002518 InsertValue(ArgIt);
Reid Spencer14310612006-12-31 05:40:51 +00002519 Idx++;
Chris Lattner58af2a12006-02-15 07:22:58 +00002520 }
Reid Spencera132e042006-12-03 05:46:11 +00002521
Devang Patel652203f2008-09-29 20:49:50 +00002522 delete $6; // We're now done with the argument list
Chris Lattner58af2a12006-02-15 07:22:58 +00002523 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002524 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002525};
2526
2527BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2528
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002529FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner58af2a12006-02-15 07:22:58 +00002530 $$ = CurFun.CurrentFunction;
2531
2532 // Make sure that we keep track of the linkage type even if there was a
2533 // previous "declare".
2534 $$->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002535 $$->setVisibility($2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002536};
2537
2538END : ENDTOK | '}'; // Allow end of '}' to end a function
2539
2540Function : BasicBlockList END {
2541 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002542 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002543};
2544
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002545FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencer14310612006-12-31 05:40:51 +00002546 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002547 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002548 $$ = CurFun.CurrentFunction;
2549 CurFun.FunctionDone();
2550 CHECK_FOR_ERROR
2551 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002552
2553//===----------------------------------------------------------------------===//
2554// Rules to match Basic Blocks
2555//===----------------------------------------------------------------------===//
2556
2557OptSideEffect : /* empty */ {
2558 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002559 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002560 }
2561 | SIDEEFFECT {
2562 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002563 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002564 };
2565
2566ConstValueRef : ESINT64VAL { // A reference to a direct constant
2567 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002568 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002569 }
2570 | EUINT64VAL {
2571 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002572 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002573 }
Chris Lattner1913b942008-07-11 00:30:39 +00002574 | ESAPINTVAL { // arbitrary precision integer constants
2575 $$ = ValID::create(*$1, true);
2576 delete $1;
2577 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00002578 }
Chris Lattner1913b942008-07-11 00:30:39 +00002579 | EUAPINTVAL { // arbitrary precision integer constants
2580 $$ = ValID::create(*$1, false);
2581 delete $1;
2582 CHECK_FOR_ERROR
2583 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002584 | FPVAL { // Perhaps it's an FP constant?
2585 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002586 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002587 }
2588 | TRUETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002589 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002590 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00002591 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002592 | FALSETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002593 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002594 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002595 }
2596 | NULL_TOK {
2597 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002598 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002599 }
2600 | UNDEF {
2601 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002602 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002603 }
2604 | ZEROINITIALIZER { // A vector zero constant.
2605 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002606 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002607 }
2608 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencera132e042006-12-03 05:46:11 +00002609 const Type *ETy = (*$2)[0]->getType();
Eric Christopher2a5196f2008-09-24 04:55:49 +00002610 unsigned NumElements = $2->size();
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002611
2612 if (!ETy->isInteger() && !ETy->isFloatingPoint())
2613 GEN_ERROR("Invalid vector element type: " + ETy->getDescription());
Eric Christopher2a5196f2008-09-24 04:55:49 +00002614
Reid Spencer9d6565a2007-02-15 02:26:10 +00002615 VectorType* pt = VectorType::get(ETy, NumElements);
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002616 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(pt));
Eric Christopher2a5196f2008-09-24 04:55:49 +00002617
Chris Lattner58af2a12006-02-15 07:22:58 +00002618 // Verify all elements are correct type!
2619 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00002620 if (ETy != (*$2)[i]->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00002621 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00002622 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencera132e042006-12-03 05:46:11 +00002623 (*$2)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00002624 }
2625
Reid Spencer9d6565a2007-02-15 02:26:10 +00002626 $$ = ValID::create(ConstantVector::get(pt, *$2));
Chris Lattner58af2a12006-02-15 07:22:58 +00002627 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002628 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002629 }
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002630 | '[' ConstVector ']' { // Nonempty unsized arr
2631 const Type *ETy = (*$2)[0]->getType();
Eric Christopher2a5196f2008-09-24 04:55:49 +00002632 uint64_t NumElements = $2->size();
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002633
2634 if (!ETy->isFirstClassType())
2635 GEN_ERROR("Invalid array element type: " + ETy->getDescription());
2636
2637 ArrayType *ATy = ArrayType::get(ETy, NumElements);
2638 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(ATy));
2639
2640 // Verify all elements are correct type!
2641 for (unsigned i = 0; i < $2->size(); i++) {
2642 if (ETy != (*$2)[i]->getType())
Eric Christopher2a5196f2008-09-24 04:55:49 +00002643 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002644 ETy->getDescription() +"' as required!\nIt is of type '"+
2645 (*$2)[i]->getType()->getDescription() + "'.");
2646 }
2647
2648 $$ = ValID::create(ConstantArray::get(ATy, *$2));
2649 delete PTy; delete $2;
2650 CHECK_FOR_ERROR
2651 }
2652 | '[' ']' {
Dan Gohman180c1692008-06-23 18:43:26 +00002653 // Use undef instead of an array because it's inconvenient to determine
2654 // the element type at this point, there being no elements to examine.
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002655 $$ = ValID::createUndef();
2656 CHECK_FOR_ERROR
2657 }
2658 | 'c' STRINGCONSTANT {
Dan Gohman180c1692008-06-23 18:43:26 +00002659 uint64_t NumElements = $2->length();
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002660 const Type *ETy = Type::Int8Ty;
2661
2662 ArrayType *ATy = ArrayType::get(ETy, NumElements);
2663
2664 std::vector<Constant*> Vals;
2665 for (unsigned i = 0; i < $2->length(); ++i)
2666 Vals.push_back(ConstantInt::get(ETy, (*$2)[i]));
2667 delete $2;
2668 $$ = ValID::create(ConstantArray::get(ATy, Vals));
2669 CHECK_FOR_ERROR
2670 }
2671 | '{' ConstVector '}' {
2672 std::vector<const Type*> Elements($2->size());
2673 for (unsigned i = 0, e = $2->size(); i != e; ++i)
2674 Elements[i] = (*$2)[i]->getType();
2675
2676 const StructType *STy = StructType::get(Elements);
2677 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(STy));
2678
2679 $$ = ValID::create(ConstantStruct::get(STy, *$2));
2680 delete PTy; delete $2;
2681 CHECK_FOR_ERROR
2682 }
2683 | '{' '}' {
2684 const StructType *STy = StructType::get(std::vector<const Type*>());
2685 $$ = ValID::create(ConstantStruct::get(STy, std::vector<Constant*>()));
2686 CHECK_FOR_ERROR
2687 }
2688 | '<' '{' ConstVector '}' '>' {
2689 std::vector<const Type*> Elements($3->size());
2690 for (unsigned i = 0, e = $3->size(); i != e; ++i)
2691 Elements[i] = (*$3)[i]->getType();
2692
2693 const StructType *STy = StructType::get(Elements, /*isPacked=*/true);
2694 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(STy));
2695
2696 $$ = ValID::create(ConstantStruct::get(STy, *$3));
2697 delete PTy; delete $3;
2698 CHECK_FOR_ERROR
2699 }
2700 | '<' '{' '}' '>' {
2701 const StructType *STy = StructType::get(std::vector<const Type*>(),
2702 /*isPacked=*/true);
2703 $$ = ValID::create(ConstantStruct::get(STy, std::vector<Constant*>()));
2704 CHECK_FOR_ERROR
2705 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002706 | ConstExpr {
Reid Spencera132e042006-12-03 05:46:11 +00002707 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002708 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002709 }
2710 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002711 $$ = ValID::createInlineAsm(*$3, *$5, $2);
2712 delete $3;
2713 delete $5;
Reid Spencer61c83e02006-08-18 08:43:06 +00002714 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002715 };
2716
2717// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2718// another value.
2719//
Reid Spencer41dff5e2007-01-26 08:05:27 +00002720SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2721 $$ = ValID::createLocalID($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002722 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002723 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002724 | GLOBALVAL_ID {
2725 $$ = ValID::createGlobalID($1);
2726 CHECK_FOR_ERROR
2727 }
2728 | LocalName { // Is it a named reference...?
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002729 $$ = ValID::createLocalName(*$1);
2730 delete $1;
Reid Spencer41dff5e2007-01-26 08:05:27 +00002731 CHECK_FOR_ERROR
2732 }
2733 | GlobalName { // Is it a named reference...?
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002734 $$ = ValID::createGlobalName(*$1);
2735 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002736 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002737 };
2738
2739// ValueRef - A reference to a definition... either constant or symbolic
2740ValueRef : SymbolicValueRef | ConstValueRef;
2741
2742
2743// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2744// type immediately preceeds the value reference, and allows complex constant
2745// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2746ResolvedVal : Types ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002747 if (!UpRefs.empty())
2748 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Eric Christopher2a5196f2008-09-24 04:55:49 +00002749 $$ = getVal(*$1, $2);
Reid Spencer14310612006-12-31 05:40:51 +00002750 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002751 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00002752 }
2753 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002754
Devang Patel7990dc72008-02-20 22:40:23 +00002755ReturnedVal : ResolvedVal {
2756 $$ = new std::vector<Value *>();
Eric Christopher2a5196f2008-09-24 04:55:49 +00002757 $$->push_back($1);
Devang Patel7990dc72008-02-20 22:40:23 +00002758 CHECK_FOR_ERROR
2759 }
Devang Patel6bfc63b2008-02-23 00:38:56 +00002760 | ReturnedVal ',' ResolvedVal {
Eric Christopher2a5196f2008-09-24 04:55:49 +00002761 ($$=$1)->push_back($3);
Devang Patel7990dc72008-02-20 22:40:23 +00002762 CHECK_FOR_ERROR
2763 };
2764
Chris Lattner58af2a12006-02-15 07:22:58 +00002765BasicBlockList : BasicBlockList BasicBlock {
2766 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002767 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002768 }
Eric Christopher2a5196f2008-09-24 04:55:49 +00002769 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner58af2a12006-02-15 07:22:58 +00002770 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002771 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002772 };
2773
2774
Eric Christopher2a5196f2008-09-24 04:55:49 +00002775// Basic blocks are terminated by branching instructions:
Chris Lattner58af2a12006-02-15 07:22:58 +00002776// br, br/cc, switch, ret
2777//
Chris Lattner15bd0952008-08-29 17:20:18 +00002778BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner58af2a12006-02-15 07:22:58 +00002779 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002780 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002781 InsertValue($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002782 $1->getInstList().push_back($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002783 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002784 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002785 };
2786
Chris Lattner15bd0952008-08-29 17:20:18 +00002787BasicBlock : InstructionList LocalNumber BBTerminatorInst {
2788 CHECK_FOR_ERROR
2789 int ValNum = InsertValue($3);
2790 if (ValNum != (int)$2)
2791 GEN_ERROR("Result value number %" + utostr($2) +
2792 " is incorrect, expected %" + utostr((unsigned)ValNum));
Eric Christopher2a5196f2008-09-24 04:55:49 +00002793
Chris Lattner15bd0952008-08-29 17:20:18 +00002794 $1->getInstList().push_back($3);
2795 $$ = $1;
2796 CHECK_FOR_ERROR
2797};
2798
2799
Chris Lattner58af2a12006-02-15 07:22:58 +00002800InstructionList : InstructionList Inst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002801 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2802 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2803 if (CI2->getParent() == 0)
2804 $1->getInstList().push_back(CI2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002805 $1->getInstList().push_back($2);
2806 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002807 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002808 }
Reid Spencer93c40032007-03-19 18:40:50 +00002809 | /* empty */ { // Empty space between instruction lists
Nick Lewycky280a6e62008-04-25 16:53:59 +00002810 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
Reid Spencer61c83e02006-08-18 08:43:06 +00002811 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002812 }
Reid Spencer93c40032007-03-19 18:40:50 +00002813 | LABELSTR { // Labelled (named) basic block
Nick Lewycky280a6e62008-04-25 16:53:59 +00002814 $$ = defineBBVal(ValID::createLocalName(*$1));
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002815 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002816 CHECK_FOR_ERROR
Nick Lewycky280a6e62008-04-25 16:53:59 +00002817
Chris Lattner58af2a12006-02-15 07:22:58 +00002818 };
2819
Eric Christopher2a5196f2008-09-24 04:55:49 +00002820BBTerminatorInst :
Devang Patel7990dc72008-02-20 22:40:23 +00002821 RET ReturnedVal { // Return with a result...
Devang Patelb82b7f22008-02-26 22:17:48 +00002822 ValueList &VL = *$2;
Devang Patel13b823c2008-02-26 23:19:08 +00002823 assert(!VL.empty() && "Invalid ret operands!");
Dan Gohman1a570242008-07-23 00:54:54 +00002824 const Type *ReturnType = CurFun.CurrentFunction->getReturnType();
2825 if (VL.size() > 1 ||
2826 (isa<StructType>(ReturnType) &&
2827 (VL.empty() || VL[0]->getType() != ReturnType))) {
2828 Value *RV = UndefValue::get(ReturnType);
2829 for (unsigned i = 0, e = VL.size(); i != e; ++i) {
2830 Instruction *I = InsertValueInst::Create(RV, VL[i], i, "mrv");
2831 ($<BasicBlockVal>-1)->getInstList().push_back(I);
2832 RV = I;
2833 }
2834 $$ = ReturnInst::Create(RV);
2835 } else {
2836 $$ = ReturnInst::Create(VL[0]);
2837 }
Devang Patel7990dc72008-02-20 22:40:23 +00002838 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002839 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002840 }
Reid Spencer93c40032007-03-19 18:40:50 +00002841 | RET VOID { // Return with no result...
Gabor Greife64d2482008-04-06 23:07:54 +00002842 $$ = ReturnInst::Create();
Reid Spencer61c83e02006-08-18 08:43:06 +00002843 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002844 }
Reid Spencer93c40032007-03-19 18:40:50 +00002845 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002846 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002847 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002848 $$ = BranchInst::Create(tmpBB);
Reid Spencer93c40032007-03-19 18:40:50 +00002849 } // Conditional Branch...
Eric Christopher2a5196f2008-09-24 04:55:49 +00002850 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002851 if (cast<IntegerType>($2)->getBitWidth() != 1)
2852 GEN_ERROR("Branch condition must have type i1");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002853 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002854 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002855 BasicBlock* tmpBBB = getBBVal($9);
2856 CHECK_FOR_ERROR
Reid Spencer4fe16d62007-01-11 18:21:29 +00002857 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002858 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002859 $$ = BranchInst::Create(tmpBBA, tmpBBB, tmpVal);
Chris Lattner58af2a12006-02-15 07:22:58 +00002860 }
Chris Lattnerf9078f92008-10-15 06:03:48 +00002861 | SWITCH INTTYPE ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002862 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002863 CHECK_FOR_ERROR
2864 BasicBlock* tmpBB = getBBVal($6);
2865 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002866 SwitchInst *S = SwitchInst::Create(tmpVal, tmpBB, $8->size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002867 $$ = S;
2868
2869 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2870 E = $8->end();
2871 for (; I != E; ++I) {
2872 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2873 S->addCase(CI, I->second);
2874 else
Reid Spencerb5334b02007-02-05 10:18:06 +00002875 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattner58af2a12006-02-15 07:22:58 +00002876 }
2877 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002878 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002879 }
Chris Lattnerf9078f92008-10-15 06:03:48 +00002880 | SWITCH INTTYPE ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002881 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002882 CHECK_FOR_ERROR
2883 BasicBlock* tmpBB = getBBVal($6);
2884 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002885 SwitchInst *S = SwitchInst::Create(tmpVal, tmpBB, 0);
Chris Lattner58af2a12006-02-15 07:22:58 +00002886 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002887 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002888 }
Devang Patel652203f2008-09-29 20:49:50 +00002889 | INVOKE OptCallingConv OptRetAttrs ResultTypes ValueRef '(' ParamList ')'
2890 OptFuncAttrs TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattner58af2a12006-02-15 07:22:58 +00002891
Reid Spencer14310612006-12-31 05:40:51 +00002892 // Handle the short syntax
2893 const PointerType *PFTy = 0;
2894 const FunctionType *Ty = 0;
Devang Patel652203f2008-09-29 20:49:50 +00002895 if (!(PFTy = dyn_cast<PointerType>($4->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00002896 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2897 // Pull out the types of all of the arguments...
2898 std::vector<const Type*> ParamTypes;
Devang Patel652203f2008-09-29 20:49:50 +00002899 ParamList::iterator I = $7->begin(), E = $7->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002900 for (; I != E; ++I) {
Reid Spencer14310612006-12-31 05:40:51 +00002901 const Type *Ty = I->Val->getType();
2902 if (Ty == Type::VoidTy)
2903 GEN_ERROR("Short call syntax cannot be used with varargs");
2904 ParamTypes.push_back(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002905 }
Eric Christopher2a5196f2008-09-24 04:55:49 +00002906
Devang Patel652203f2008-09-29 20:49:50 +00002907 if (!FunctionType::isValidReturnType(*$4))
Chris Lattnera925a142008-04-23 05:37:08 +00002908 GEN_ERROR("Invalid result type for LLVM function");
2909
Devang Patel652203f2008-09-29 20:49:50 +00002910 Ty = FunctionType::get($4->get(), ParamTypes, false);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002911 PFTy = PointerType::getUnqual(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002912 }
2913
Devang Patel652203f2008-09-29 20:49:50 +00002914 delete $4;
Reid Spencer66728ef2007-03-20 01:13:36 +00002915
Devang Patel652203f2008-09-29 20:49:50 +00002916 Value *V = getVal(PFTy, $5); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002917 CHECK_FOR_ERROR
Devang Patel652203f2008-09-29 20:49:50 +00002918 BasicBlock *Normal = getBBVal($12);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002919 CHECK_FOR_ERROR
Devang Patel652203f2008-09-29 20:49:50 +00002920 BasicBlock *Except = getBBVal($15);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002921 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002922
Devang Patel05988662008-09-25 21:00:45 +00002923 SmallVector<AttributeWithIndex, 8> Attrs;
Devang Patel19c87462008-09-26 22:53:05 +00002924 //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
2925 //attributes.
Devang Patel652203f2008-09-29 20:49:50 +00002926 Attributes RetAttrs = $3;
2927 if ($9 != Attribute::None) {
2928 if ($9 & Attribute::ZExt) {
Devang Patel19c87462008-09-26 22:53:05 +00002929 RetAttrs = RetAttrs | Attribute::ZExt;
Devang Patel652203f2008-09-29 20:49:50 +00002930 $9 = $9 ^ Attribute::ZExt;
Devang Patel19c87462008-09-26 22:53:05 +00002931 }
Devang Patel652203f2008-09-29 20:49:50 +00002932 if ($9 & Attribute::SExt) {
Devang Patel19c87462008-09-26 22:53:05 +00002933 RetAttrs = RetAttrs | Attribute::SExt;
Devang Patel652203f2008-09-29 20:49:50 +00002934 $9 = $9 ^ Attribute::SExt;
Devang Patel19c87462008-09-26 22:53:05 +00002935 }
Devang Patel652203f2008-09-29 20:49:50 +00002936 if ($9 & Attribute::InReg) {
Devang Patel19c87462008-09-26 22:53:05 +00002937 RetAttrs = RetAttrs | Attribute::InReg;
Devang Patel652203f2008-09-29 20:49:50 +00002938 $9 = $9 ^ Attribute::InReg;
Devang Patel19c87462008-09-26 22:53:05 +00002939 }
Devang Patel19c87462008-09-26 22:53:05 +00002940 }
Devang Patel652203f2008-09-29 20:49:50 +00002941 if (RetAttrs != Attribute::None)
2942 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Devang Patel19c87462008-09-26 22:53:05 +00002943
Reid Spencer14310612006-12-31 05:40:51 +00002944 // Check the arguments
2945 ValueList Args;
Devang Patel652203f2008-09-29 20:49:50 +00002946 if ($7->empty()) { // Has no arguments?
Reid Spencer14310612006-12-31 05:40:51 +00002947 // Make sure no arguments is a good thing!
2948 if (Ty->getNumParams() != 0)
2949 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00002950 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00002951 } else { // Has arguments?
2952 // Loop through FunctionType's arguments and ensure they are specified
2953 // correctly!
Chris Lattner58af2a12006-02-15 07:22:58 +00002954 FunctionType::param_iterator I = Ty->param_begin();
2955 FunctionType::param_iterator E = Ty->param_end();
Devang Patel652203f2008-09-29 20:49:50 +00002956 ParamList::iterator ArgI = $7->begin(), ArgE = $7->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002957 unsigned index = 1;
Chris Lattner58af2a12006-02-15 07:22:58 +00002958
Duncan Sandsdc024672007-11-27 13:23:08 +00002959 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002960 if (ArgI->Val->getType() != *I)
2961 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00002962 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00002963 Args.push_back(ArgI->Val);
Devang Patel05988662008-09-25 21:00:45 +00002964 if (ArgI->Attrs != Attribute::None)
2965 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Reid Spencer14310612006-12-31 05:40:51 +00002966 }
Reid Spencera132e042006-12-03 05:46:11 +00002967
Reid Spencer14310612006-12-31 05:40:51 +00002968 if (Ty->isVarArg()) {
2969 if (I == E)
Chris Lattner38905612008-02-19 04:36:25 +00002970 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002971 Args.push_back(ArgI->Val); // push the remaining varargs
Devang Patel05988662008-09-25 21:00:45 +00002972 if (ArgI->Attrs != Attribute::None)
2973 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Chris Lattner38905612008-02-19 04:36:25 +00002974 }
Reid Spencer14310612006-12-31 05:40:51 +00002975 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00002976 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00002977 }
Devang Patel652203f2008-09-29 20:49:50 +00002978 if ($9 != Attribute::None)
2979 Attrs.push_back(AttributeWithIndex::get(~0, $9));
Devang Patel05988662008-09-25 21:00:45 +00002980 AttrListPtr PAL;
Duncan Sandsdc024672007-11-27 13:23:08 +00002981 if (!Attrs.empty())
Devang Patel05988662008-09-25 21:00:45 +00002982 PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Duncan Sandsdc024672007-11-27 13:23:08 +00002983
Reid Spencer14310612006-12-31 05:40:51 +00002984 // Create the InvokeInst
Dan Gohman041e2eb2008-05-15 19:50:34 +00002985 InvokeInst *II = InvokeInst::Create(V, Normal, Except,
2986 Args.begin(), Args.end());
Reid Spencer14310612006-12-31 05:40:51 +00002987 II->setCallingConv($2);
Devang Patel05988662008-09-25 21:00:45 +00002988 II->setAttributes(PAL);
Reid Spencer14310612006-12-31 05:40:51 +00002989 $$ = II;
Devang Patel652203f2008-09-29 20:49:50 +00002990 delete $7;
Reid Spencer61c83e02006-08-18 08:43:06 +00002991 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002992 }
2993 | UNWIND {
2994 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002995 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002996 }
2997 | UNREACHABLE {
2998 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002999 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003000 };
3001
3002
3003
Chris Lattnerf9078f92008-10-15 06:03:48 +00003004JumpTable : JumpTable INTTYPE ConstValueRef ',' LABEL ValueRef {
Chris Lattner58af2a12006-02-15 07:22:58 +00003005 $$ = $1;
Reid Spencer93c40032007-03-19 18:40:50 +00003006 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00003007 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003008 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003009 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00003010
Reid Spencer5b7e7532006-09-28 19:28:24 +00003011 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003012 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00003013 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00003014 }
Chris Lattnerf9078f92008-10-15 06:03:48 +00003015 | INTTYPE ConstValueRef ',' LABEL ValueRef {
Chris Lattner58af2a12006-02-15 07:22:58 +00003016 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencer93c40032007-03-19 18:40:50 +00003017 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00003018 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003019
3020 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003021 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00003022
Reid Spencer5b7e7532006-09-28 19:28:24 +00003023 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003024 CHECK_FOR_ERROR
Eric Christopher2a5196f2008-09-24 04:55:49 +00003025 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00003026 };
3027
Reid Spencer41dff5e2007-01-26 08:05:27 +00003028Inst : OptLocalAssign InstVal {
Reid Spenceref9b9a72007-02-05 20:47:22 +00003029 // Is this definition named?? if so, assign the name...
3030 setValueName($2, $1);
3031 CHECK_FOR_ERROR
3032 InsertValue($2);
3033 $$ = $2;
3034 CHECK_FOR_ERROR
3035 };
3036
Chris Lattner15bd0952008-08-29 17:20:18 +00003037Inst : LocalNumber InstVal {
3038 CHECK_FOR_ERROR
3039 int ValNum = InsertValue($2);
Eric Christopher2a5196f2008-09-24 04:55:49 +00003040
Chris Lattner15bd0952008-08-29 17:20:18 +00003041 if (ValNum != (int)$1)
3042 GEN_ERROR("Result value number %" + utostr($1) +
3043 " is incorrect, expected %" + utostr((unsigned)ValNum));
3044
3045 $$ = $2;
3046 CHECK_FOR_ERROR
3047 };
3048
Chris Lattner58af2a12006-02-15 07:22:58 +00003049
3050PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer14310612006-12-31 05:40:51 +00003051 if (!UpRefs.empty())
3052 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00003053 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencera132e042006-12-03 05:46:11 +00003054 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003055 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00003056 BasicBlock* tmpBB = getBBVal($5);
3057 CHECK_FOR_ERROR
3058 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencera132e042006-12-03 05:46:11 +00003059 delete $1;
Chris Lattner58af2a12006-02-15 07:22:58 +00003060 }
3061 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
3062 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003063 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003064 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00003065 BasicBlock* tmpBB = getBBVal($6);
3066 CHECK_FOR_ERROR
3067 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00003068 };
3069
3070
Devang Patel05988662008-09-25 21:00:45 +00003071ParamList : Types OptAttributes ValueRef OptAttributes {
3072 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Reid Spencer14310612006-12-31 05:40:51 +00003073 if (!UpRefs.empty())
3074 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
3075 // Used for call and invoke instructions
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003076 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00003077 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getVal($1->get(), $3);
Reid Spencer14310612006-12-31 05:40:51 +00003078 $$->push_back(E);
Reid Spencer66728ef2007-03-20 01:13:36 +00003079 delete $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00003080 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003081 }
Devang Patel05988662008-09-25 21:00:45 +00003082 | LABEL OptAttributes ValueRef OptAttributes {
3083 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003084 // Labels are only valid in ASMs
3085 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00003086 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getBBVal($3);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003087 $$->push_back(E);
Duncan Sandsdc024672007-11-27 13:23:08 +00003088 CHECK_FOR_ERROR
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003089 }
Devang Patel05988662008-09-25 21:00:45 +00003090 | ParamList ',' Types OptAttributes ValueRef OptAttributes {
3091 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Reid Spencer14310612006-12-31 05:40:51 +00003092 if (!UpRefs.empty())
3093 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00003094 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00003095 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getVal($3->get(), $5);
Reid Spencer14310612006-12-31 05:40:51 +00003096 $$->push_back(E);
Reid Spencer66728ef2007-03-20 01:13:36 +00003097 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00003098 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00003099 }
Devang Patel05988662008-09-25 21:00:45 +00003100 | ParamList ',' LABEL OptAttributes ValueRef OptAttributes {
3101 // FIXME: Remove trailing OptAttributes in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003102 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00003103 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getBBVal($5);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003104 $$->push_back(E);
3105 CHECK_FOR_ERROR
3106 }
3107 | /*empty*/ { $$ = new ParamList(); };
Chris Lattner58af2a12006-02-15 07:22:58 +00003108
Reid Spencer14310612006-12-31 05:40:51 +00003109IndexList // Used for gep instructions and constant expressions
Reid Spencerc6c59fd2006-12-31 21:47:02 +00003110 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencer14310612006-12-31 05:40:51 +00003111 | IndexList ',' ResolvedVal {
3112 $$ = $1;
3113 $$->push_back($3);
3114 CHECK_FOR_ERROR
3115 }
Reid Spencerc6c59fd2006-12-31 21:47:02 +00003116 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00003117
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003118ConstantIndexList // Used for insertvalue and extractvalue instructions
3119 : ',' EUINT64VAL {
3120 $$ = new std::vector<unsigned>();
3121 if ((unsigned)$2 != $2)
3122 GEN_ERROR("Index " + utostr($2) + " is not valid for insertvalue or extractvalue.");
3123 $$->push_back($2);
3124 }
3125 | ConstantIndexList ',' EUINT64VAL {
3126 $$ = $1;
3127 if ((unsigned)$3 != $3)
3128 GEN_ERROR("Index " + utostr($3) + " is not valid for insertvalue or extractvalue.");
3129 $$->push_back($3);
3130 CHECK_FOR_ERROR
3131 }
3132 ;
3133
Chris Lattner58af2a12006-02-15 07:22:58 +00003134OptTailCall : TAIL CALL {
3135 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00003136 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003137 }
3138 | CALL {
3139 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00003140 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003141 };
3142
Chris Lattner58af2a12006-02-15 07:22:58 +00003143InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00003144 if (!UpRefs.empty())
3145 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Eric Christopher2a5196f2008-09-24 04:55:49 +00003146 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencer9d6565a2007-02-15 02:26:10 +00003147 !isa<VectorType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003148 GEN_ERROR(
Reid Spencerb5334b02007-02-05 10:18:06 +00003149 "Arithmetic operator requires integer, FP, or packed operands");
Eric Christopher2a5196f2008-09-24 04:55:49 +00003150 Value* val1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003151 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003152 Value* val2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003153 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003154 $$ = BinaryOperator::Create($1, val1, val2);
Chris Lattner58af2a12006-02-15 07:22:58 +00003155 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003156 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00003157 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003158 }
3159 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00003160 if (!UpRefs.empty())
3161 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00003162 if (!(*$2)->isInteger()) {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003163 if (!isa<VectorType>($2->get()) ||
Reid Spencer9d6565a2007-02-15 02:26:10 +00003164 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00003165 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00003166 }
Reid Spencera132e042006-12-03 05:46:11 +00003167 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003168 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003169 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003170 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003171 $$ = BinaryOperator::Create($1, tmpVal1, tmpVal2);
Chris Lattner58af2a12006-02-15 07:22:58 +00003172 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003173 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00003174 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003175 }
Reid Spencera132e042006-12-03 05:46:11 +00003176 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00003177 if (!UpRefs.empty())
3178 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003179 Value* tmpVal1 = getVal(*$3, $4);
3180 CHECK_FOR_ERROR
3181 Value* tmpVal2 = getVal(*$3, $6);
3182 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003183 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Reid Spencera132e042006-12-03 05:46:11 +00003184 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003185 GEN_ERROR("icmp operator returned null");
Reid Spencer66728ef2007-03-20 01:13:36 +00003186 delete $3;
Reid Spencera132e042006-12-03 05:46:11 +00003187 }
3188 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00003189 if (!UpRefs.empty())
3190 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003191 Value* tmpVal1 = getVal(*$3, $4);
3192 CHECK_FOR_ERROR
3193 Value* tmpVal2 = getVal(*$3, $6);
3194 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003195 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Reid Spencera132e042006-12-03 05:46:11 +00003196 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003197 GEN_ERROR("fcmp operator returned null");
Reid Spencer66728ef2007-03-20 01:13:36 +00003198 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00003199 }
Nate Begemanac80ade2008-05-12 19:01:56 +00003200 | VICMP IPredicates Types ValueRef ',' ValueRef {
3201 if (!UpRefs.empty())
3202 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
3203 if (!isa<VectorType>((*$3).get()))
3204 GEN_ERROR("Scalar types not supported by vicmp instruction");
3205 Value* tmpVal1 = getVal(*$3, $4);
3206 CHECK_FOR_ERROR
3207 Value* tmpVal2 = getVal(*$3, $6);
3208 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003209 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Nate Begemanac80ade2008-05-12 19:01:56 +00003210 if ($$ == 0)
Dan Gohmand8ee59b2008-09-09 01:13:24 +00003211 GEN_ERROR("vicmp operator returned null");
Nate Begemanac80ade2008-05-12 19:01:56 +00003212 delete $3;
3213 }
3214 | VFCMP FPredicates Types ValueRef ',' ValueRef {
3215 if (!UpRefs.empty())
3216 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
3217 if (!isa<VectorType>((*$3).get()))
3218 GEN_ERROR("Scalar types not supported by vfcmp instruction");
3219 Value* tmpVal1 = getVal(*$3, $4);
3220 CHECK_FOR_ERROR
3221 Value* tmpVal2 = getVal(*$3, $6);
3222 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003223 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Nate Begemanac80ade2008-05-12 19:01:56 +00003224 if ($$ == 0)
Dan Gohmand8ee59b2008-09-09 01:13:24 +00003225 GEN_ERROR("vfcmp operator returned null");
Nate Begemanac80ade2008-05-12 19:01:56 +00003226 delete $3;
3227 }
Reid Spencer3da59db2006-11-27 01:05:10 +00003228 | CastOps ResolvedVal TO Types {
Reid Spencer14310612006-12-31 05:40:51 +00003229 if (!UpRefs.empty())
3230 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003231 Value* Val = $2;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00003232 const Type* DestTy = $4->get();
3233 if (!CastInst::castIsValid($1, Val, DestTy))
3234 GEN_ERROR("invalid cast opcode for cast from '" +
3235 Val->getType()->getDescription() + "' to '" +
Eric Christopher2a5196f2008-09-24 04:55:49 +00003236 DestTy->getDescription() + "'");
Dan Gohmane4977cf2008-05-23 01:55:30 +00003237 $$ = CastInst::Create($1, Val, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00003238 delete $4;
Chris Lattner58af2a12006-02-15 07:22:58 +00003239 }
3240 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Dan Gohmand8ee59b2008-09-09 01:13:24 +00003241 if (isa<VectorType>($2->getType())) {
3242 // vector select
3243 if (!isa<VectorType>($4->getType())
3244 || !isa<VectorType>($6->getType()) )
3245 GEN_ERROR("vector select value types must be vector types");
3246 const VectorType* cond_type = cast<VectorType>($2->getType());
3247 const VectorType* select_type = cast<VectorType>($4->getType());
3248 if (cond_type->getElementType() != Type::Int1Ty)
3249 GEN_ERROR("vector select condition element type must be boolean");
3250 if (cond_type->getNumElements() != select_type->getNumElements())
3251 GEN_ERROR("vector select number of elements must be the same");
3252 } else {
3253 if ($2->getType() != Type::Int1Ty)
3254 GEN_ERROR("select condition must be boolean");
3255 }
Reid Spencera132e042006-12-03 05:46:11 +00003256 if ($4->getType() != $6->getType())
Dan Gohmand8ee59b2008-09-09 01:13:24 +00003257 GEN_ERROR("select value types must match");
Gabor Greife64d2482008-04-06 23:07:54 +00003258 $$ = SelectInst::Create($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003259 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003260 }
3261 | VAARG ResolvedVal ',' Types {
Reid Spencer14310612006-12-31 05:40:51 +00003262 if (!UpRefs.empty())
3263 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003264 $$ = new VAArgInst($2, *$4);
3265 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00003266 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003267 }
Chris Lattner58af2a12006-02-15 07:22:58 +00003268 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003269 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerb5334b02007-02-05 10:18:06 +00003270 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00003271 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003272 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003273 }
3274 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003275 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00003276 GEN_ERROR("Invalid insertelement operands");
Gabor Greife64d2482008-04-06 23:07:54 +00003277 $$ = InsertElementInst::Create($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003278 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003279 }
Chris Lattnerd5efe842006-04-08 01:18:56 +00003280 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003281 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00003282 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00003283 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003284 CHECK_FOR_ERROR
Chris Lattnerd5efe842006-04-08 01:18:56 +00003285 }
Chris Lattner58af2a12006-02-15 07:22:58 +00003286 | PHI_TOK PHIList {
3287 const Type *Ty = $2->front().first->getType();
3288 if (!Ty->isFirstClassType())
Reid Spencerb5334b02007-02-05 10:18:06 +00003289 GEN_ERROR("PHI node operands must be of first class type");
Gabor Greife64d2482008-04-06 23:07:54 +00003290 $$ = PHINode::Create(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00003291 ((PHINode*)$$)->reserveOperandSpace($2->size());
3292 while ($2->begin() != $2->end()) {
Eric Christopher2a5196f2008-09-24 04:55:49 +00003293 if ($2->front().first->getType() != Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00003294 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattner58af2a12006-02-15 07:22:58 +00003295 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
3296 $2->pop_front();
3297 }
3298 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00003299 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003300 }
Devang Patel652203f2008-09-29 20:49:50 +00003301 | OptTailCall OptCallingConv OptRetAttrs ResultTypes ValueRef '(' ParamList ')'
Reid Spencer218ded22007-01-05 17:07:23 +00003302 OptFuncAttrs {
Reid Spencer14310612006-12-31 05:40:51 +00003303
3304 // Handle the short syntax
Reid Spencer3da59db2006-11-27 01:05:10 +00003305 const PointerType *PFTy = 0;
3306 const FunctionType *Ty = 0;
Devang Patel652203f2008-09-29 20:49:50 +00003307 if (!(PFTy = dyn_cast<PointerType>($4->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00003308 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3309 // Pull out the types of all of the arguments...
3310 std::vector<const Type*> ParamTypes;
Devang Patel652203f2008-09-29 20:49:50 +00003311 ParamList::iterator I = $7->begin(), E = $7->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00003312 for (; I != E; ++I) {
Reid Spencer14310612006-12-31 05:40:51 +00003313 const Type *Ty = I->Val->getType();
3314 if (Ty == Type::VoidTy)
3315 GEN_ERROR("Short call syntax cannot be used with varargs");
3316 ParamTypes.push_back(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00003317 }
Chris Lattnera925a142008-04-23 05:37:08 +00003318
Devang Patel652203f2008-09-29 20:49:50 +00003319 if (!FunctionType::isValidReturnType(*$4))
Chris Lattnera925a142008-04-23 05:37:08 +00003320 GEN_ERROR("Invalid result type for LLVM function");
3321
Devang Patel652203f2008-09-29 20:49:50 +00003322 Ty = FunctionType::get($4->get(), ParamTypes, false);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00003323 PFTy = PointerType::getUnqual(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00003324 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00003325
Devang Patel652203f2008-09-29 20:49:50 +00003326 Value *V = getVal(PFTy, $5); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00003327 CHECK_FOR_ERROR
Chris Lattner6cdc6822007-04-26 05:31:05 +00003328
Reid Spencer7780acb2007-04-16 06:56:07 +00003329 // Check for call to invalid intrinsic to avoid crashing later.
3330 if (Function *theF = dyn_cast<Function>(V)) {
Reid Spencered48de22007-04-16 22:02:23 +00003331 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
Reid Spencer36fdde12007-04-16 20:35:38 +00003332 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
3333 !theF->getIntrinsicID(true))
Reid Spencer7780acb2007-04-16 06:56:07 +00003334 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
3335 theF->getName() + "'");
3336 }
3337
Devang Patel05988662008-09-25 21:00:45 +00003338 // Set up the Attributes for the function
3339 SmallVector<AttributeWithIndex, 8> Attrs;
Devang Patel19c87462008-09-26 22:53:05 +00003340 //FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
3341 //attributes.
Devang Patel652203f2008-09-29 20:49:50 +00003342 Attributes RetAttrs = $3;
3343 if ($9 != Attribute::None) {
3344 if ($9 & Attribute::ZExt) {
Devang Patel19c87462008-09-26 22:53:05 +00003345 RetAttrs = RetAttrs | Attribute::ZExt;
Devang Patel652203f2008-09-29 20:49:50 +00003346 $9 = $9 ^ Attribute::ZExt;
Devang Patel19c87462008-09-26 22:53:05 +00003347 }
Devang Patel652203f2008-09-29 20:49:50 +00003348 if ($9 & Attribute::SExt) {
Devang Patel19c87462008-09-26 22:53:05 +00003349 RetAttrs = RetAttrs | Attribute::SExt;
Devang Patel652203f2008-09-29 20:49:50 +00003350 $9 = $9 ^ Attribute::SExt;
Devang Patel19c87462008-09-26 22:53:05 +00003351 }
Devang Patel652203f2008-09-29 20:49:50 +00003352 if ($9 & Attribute::InReg) {
Devang Patel19c87462008-09-26 22:53:05 +00003353 RetAttrs = RetAttrs | Attribute::InReg;
Devang Patel652203f2008-09-29 20:49:50 +00003354 $9 = $9 ^ Attribute::InReg;
Devang Patel19c87462008-09-26 22:53:05 +00003355 }
Devang Patel19c87462008-09-26 22:53:05 +00003356 }
Devang Patel652203f2008-09-29 20:49:50 +00003357 if (RetAttrs != Attribute::None)
3358 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Devang Patel19c87462008-09-26 22:53:05 +00003359
Eric Christopher2a5196f2008-09-24 04:55:49 +00003360 // Check the arguments
Reid Spencer14310612006-12-31 05:40:51 +00003361 ValueList Args;
Devang Patel652203f2008-09-29 20:49:50 +00003362 if ($7->empty()) { // Has no arguments?
Chris Lattner58af2a12006-02-15 07:22:58 +00003363 // Make sure no arguments is a good thing!
3364 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00003365 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00003366 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00003367 } else { // Has arguments?
3368 // Loop through FunctionType's arguments and ensure they are specified
Duncan Sandsdc024672007-11-27 13:23:08 +00003369 // correctly. Also, gather any parameter attributes.
Chris Lattner58af2a12006-02-15 07:22:58 +00003370 FunctionType::param_iterator I = Ty->param_begin();
3371 FunctionType::param_iterator E = Ty->param_end();
Devang Patel652203f2008-09-29 20:49:50 +00003372 ParamList::iterator ArgI = $7->begin(), ArgE = $7->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00003373 unsigned index = 1;
Chris Lattner58af2a12006-02-15 07:22:58 +00003374
Duncan Sandsdc024672007-11-27 13:23:08 +00003375 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00003376 if (ArgI->Val->getType() != *I)
3377 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00003378 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00003379 Args.push_back(ArgI->Val);
Devang Patel05988662008-09-25 21:00:45 +00003380 if (ArgI->Attrs != Attribute::None)
3381 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Reid Spencer14310612006-12-31 05:40:51 +00003382 }
3383 if (Ty->isVarArg()) {
3384 if (I == E)
Chris Lattner38905612008-02-19 04:36:25 +00003385 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00003386 Args.push_back(ArgI->Val); // push the remaining varargs
Devang Patel05988662008-09-25 21:00:45 +00003387 if (ArgI->Attrs != Attribute::None)
3388 Attrs.push_back(AttributeWithIndex::get(index, ArgI->Attrs));
Chris Lattner38905612008-02-19 04:36:25 +00003389 }
Reid Spencer14310612006-12-31 05:40:51 +00003390 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00003391 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00003392 }
Devang Patel652203f2008-09-29 20:49:50 +00003393 if ($9 != Attribute::None)
3394 Attrs.push_back(AttributeWithIndex::get(~0, $9));
Duncan Sandsdc024672007-11-27 13:23:08 +00003395
Devang Patel05988662008-09-25 21:00:45 +00003396 // Finish off the Attributes and check them
3397 AttrListPtr PAL;
Duncan Sandsdc024672007-11-27 13:23:08 +00003398 if (!Attrs.empty())
Devang Patel05988662008-09-25 21:00:45 +00003399 PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Duncan Sandsdc024672007-11-27 13:23:08 +00003400
Reid Spencer14310612006-12-31 05:40:51 +00003401 // Create the call node
Gabor Greife64d2482008-04-06 23:07:54 +00003402 CallInst *CI = CallInst::Create(V, Args.begin(), Args.end());
Reid Spencer14310612006-12-31 05:40:51 +00003403 CI->setTailCall($1);
3404 CI->setCallingConv($2);
Devang Patel05988662008-09-25 21:00:45 +00003405 CI->setAttributes(PAL);
Reid Spencer14310612006-12-31 05:40:51 +00003406 $$ = CI;
Devang Patel652203f2008-09-29 20:49:50 +00003407 delete $7;
3408 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00003409 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003410 }
3411 | MemoryInst {
3412 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00003413 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003414 };
3415
Chris Lattner58af2a12006-02-15 07:22:58 +00003416OptVolatile : VOLATILE {
3417 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00003418 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003419 }
3420 | /* empty */ {
3421 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00003422 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003423 };
3424
3425
3426
3427MemoryInst : MALLOC Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003428 if (!UpRefs.empty())
3429 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003430 $$ = new MallocInst(*$2, 0, $3);
3431 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003432 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003433 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003434 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003435 if (!UpRefs.empty())
3436 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003437 if ($4 != Type::Int32Ty)
3438 GEN_ERROR("Malloc array size is not a 32-bit integer!");
Reid Spencera132e042006-12-03 05:46:11 +00003439 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003440 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003441 $$ = new MallocInst(*$2, tmpVal, $6);
3442 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003443 }
3444 | ALLOCA Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003445 if (!UpRefs.empty())
3446 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003447 $$ = new AllocaInst(*$2, 0, $3);
3448 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003449 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003450 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003451 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003452 if (!UpRefs.empty())
3453 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003454 if ($4 != Type::Int32Ty)
3455 GEN_ERROR("Alloca array size is not a 32-bit integer!");
Reid Spencera132e042006-12-03 05:46:11 +00003456 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003457 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003458 $$ = new AllocaInst(*$2, tmpVal, $6);
3459 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003460 }
3461 | FREE ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003462 if (!isa<PointerType>($2->getType()))
Eric Christopher2a5196f2008-09-24 04:55:49 +00003463 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerb5334b02007-02-05 10:18:06 +00003464 $2->getType()->getDescription() + "");
Reid Spencera132e042006-12-03 05:46:11 +00003465 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00003466 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003467 }
3468
Christopher Lamb5c104242007-04-22 20:09:11 +00003469 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003470 if (!UpRefs.empty())
3471 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003472 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003473 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003474 (*$3)->getDescription());
3475 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00003476 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003477 (*$3)->getDescription());
3478 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003479 CHECK_FOR_ERROR
Christopher Lamb5c104242007-04-22 20:09:11 +00003480 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencera132e042006-12-03 05:46:11 +00003481 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00003482 }
Christopher Lamb5c104242007-04-22 20:09:11 +00003483 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003484 if (!UpRefs.empty())
3485 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003486 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00003487 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00003488 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003489 (*$5)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00003490 const Type *ElTy = PT->getElementType();
Reid Spencera132e042006-12-03 05:46:11 +00003491 if (ElTy != $3->getType())
3492 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerb5334b02007-02-05 10:18:06 +00003493 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00003494
Reid Spencera132e042006-12-03 05:46:11 +00003495 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003496 CHECK_FOR_ERROR
Christopher Lamb5c104242007-04-22 20:09:11 +00003497 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencera132e042006-12-03 05:46:11 +00003498 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00003499 }
Dan Gohmane4977cf2008-05-23 01:55:30 +00003500 | GETRESULT Types ValueRef ',' EUINT64VAL {
Dan Gohman1a570242008-07-23 00:54:54 +00003501 if (!UpRefs.empty())
3502 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3503 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3504 GEN_ERROR("getresult insn requires an aggregate operand");
3505 if (!ExtractValueInst::getIndexedType(*$2, $5))
3506 GEN_ERROR("Invalid getresult index for type '" +
3507 (*$2)->getDescription()+ "'");
3508
3509 Value *tmpVal = getVal(*$2, $3);
Devang Patel5a970972008-02-19 22:27:01 +00003510 CHECK_FOR_ERROR
Dan Gohman1a570242008-07-23 00:54:54 +00003511 $$ = ExtractValueInst::Create(tmpVal, $5);
3512 delete $2;
Devang Patel5a970972008-02-19 22:27:01 +00003513 }
Chris Lattner58af2a12006-02-15 07:22:58 +00003514 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer14310612006-12-31 05:40:51 +00003515 if (!UpRefs.empty())
3516 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003517 if (!isa<PointerType>($2->get()))
Reid Spencerb5334b02007-02-05 10:18:06 +00003518 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00003519
Dan Gohman041e2eb2008-05-15 19:50:34 +00003520 if (!GetElementPtrInst::getIndexedType(*$2, $4->begin(), $4->end()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003521 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00003522 (*$2)->getDescription()+ "'");
Reid Spencera132e042006-12-03 05:46:11 +00003523 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003524 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00003525 $$ = GetElementPtrInst::Create(tmpVal, $4->begin(), $4->end());
Eric Christopher2a5196f2008-09-24 04:55:49 +00003526 delete $2;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003527 delete $4;
Dan Gohmane4977cf2008-05-23 01:55:30 +00003528 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003529 | EXTRACTVALUE Types ValueRef ConstantIndexList {
Dan Gohmane4977cf2008-05-23 01:55:30 +00003530 if (!UpRefs.empty())
3531 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3532 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3533 GEN_ERROR("extractvalue insn requires an aggregate operand");
3534
3535 if (!ExtractValueInst::getIndexedType(*$2, $4->begin(), $4->end()))
3536 GEN_ERROR("Invalid extractvalue indices for type '" +
3537 (*$2)->getDescription()+ "'");
3538 Value* tmpVal = getVal(*$2, $3);
3539 CHECK_FOR_ERROR
3540 $$ = ExtractValueInst::Create(tmpVal, $4->begin(), $4->end());
Eric Christopher2a5196f2008-09-24 04:55:49 +00003541 delete $2;
Dan Gohmane4977cf2008-05-23 01:55:30 +00003542 delete $4;
3543 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003544 | INSERTVALUE Types ValueRef ',' Types ValueRef ConstantIndexList {
Dan Gohmane4977cf2008-05-23 01:55:30 +00003545 if (!UpRefs.empty())
3546 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3547 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3548 GEN_ERROR("extractvalue insn requires an aggregate operand");
3549
3550 if (ExtractValueInst::getIndexedType(*$2, $7->begin(), $7->end()) != $5->get())
3551 GEN_ERROR("Invalid insertvalue indices for type '" +
3552 (*$2)->getDescription()+ "'");
3553 Value* aggVal = getVal(*$2, $3);
3554 Value* tmpVal = getVal(*$5, $6);
3555 CHECK_FOR_ERROR
3556 $$ = InsertValueInst::Create(aggVal, tmpVal, $7->begin(), $7->end());
Eric Christopher2a5196f2008-09-24 04:55:49 +00003557 delete $2;
Dan Gohmane4977cf2008-05-23 01:55:30 +00003558 delete $5;
3559 delete $7;
Chris Lattner58af2a12006-02-15 07:22:58 +00003560 };
3561
3562
3563%%
Reid Spencer61c83e02006-08-18 08:43:06 +00003564
Reid Spencer14310612006-12-31 05:40:51 +00003565// common code from the two 'RunVMAsmParser' functions
3566static Module* RunParser(Module * M) {
Reid Spencer14310612006-12-31 05:40:51 +00003567 CurModule.CurrentModule = M;
Reid Spencer14310612006-12-31 05:40:51 +00003568 // Check to make sure the parser succeeded
3569 if (yyparse()) {
3570 if (ParserResult)
3571 delete ParserResult;
3572 return 0;
3573 }
3574
Reid Spencer0d60b5a2007-03-30 01:37:39 +00003575 // Emit an error if there are any unresolved types left.
3576 if (!CurModule.LateResolveTypes.empty()) {
3577 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3578 if (DID.Type == ValID::LocalName) {
3579 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3580 } else {
3581 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3582 }
3583 if (ParserResult)
3584 delete ParserResult;
3585 return 0;
3586 }
3587
3588 // Emit an error if there are any unresolved values left.
3589 if (!CurModule.LateResolveValues.empty()) {
3590 Value *V = CurModule.LateResolveValues.back();
3591 std::map<Value*, std::pair<ValID, int> >::iterator I =
3592 CurModule.PlaceHolderInfo.find(V);
3593
3594 if (I != CurModule.PlaceHolderInfo.end()) {
3595 ValID &DID = I->second.first;
3596 if (DID.Type == ValID::LocalName) {
3597 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3598 } else {
3599 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3600 }
3601 if (ParserResult)
3602 delete ParserResult;
3603 return 0;
3604 }
3605 }
3606
Reid Spencer14310612006-12-31 05:40:51 +00003607 // Check to make sure that parsing produced a result
3608 if (!ParserResult)
3609 return 0;
3610
3611 // Reset ParserResult variable while saving its value for the result.
3612 Module *Result = ParserResult;
3613 ParserResult = 0;
3614
3615 return Result;
3616}
3617
Reid Spencer61c83e02006-08-18 08:43:06 +00003618void llvm::GenerateError(const std::string &message, int LineNo) {
Duncan Sandsdc024672007-11-27 13:23:08 +00003619 if (LineNo == -1) LineNo = LLLgetLineNo();
Reid Spencer61c83e02006-08-18 08:43:06 +00003620 // TODO: column number in exception
3621 if (TheParseError)
Duncan Sandsdc024672007-11-27 13:23:08 +00003622 TheParseError->setError(LLLgetFilename(), message, LineNo);
Reid Spencer61c83e02006-08-18 08:43:06 +00003623 TriggerError = 1;
3624}
3625
Chris Lattner58af2a12006-02-15 07:22:58 +00003626int yyerror(const char *ErrorMsg) {
Duncan Sandsdc024672007-11-27 13:23:08 +00003627 std::string where = LLLgetFilename() + ":" + utostr(LLLgetLineNo()) + ": ";
Reid Spenceref9b9a72007-02-05 20:47:22 +00003628 std::string errMsg = where + "error: " + std::string(ErrorMsg);
Duncan Sandsdc024672007-11-27 13:23:08 +00003629 if (yychar != YYEMPTY && yychar != 0) {
3630 errMsg += " while reading token: '";
Eric Christopher2a5196f2008-09-24 04:55:49 +00003631 errMsg += std::string(LLLgetTokenStart(),
Duncan Sandsdc024672007-11-27 13:23:08 +00003632 LLLgetTokenStart()+LLLgetTokenLength()) + "'";
3633 }
Reid Spencer61c83e02006-08-18 08:43:06 +00003634 GenerateError(errMsg);
Chris Lattner58af2a12006-02-15 07:22:58 +00003635 return 0;
3636}