blob: 30a1fe6ffb19e507b7d5f4f34f5dad0b0b347e85 [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
Misha Brukman8b477072005-05-10 22:02:28 +00002//
John Criswell29265fe2003-10-21 15:17:13 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman8b477072005-05-10 22:02:28 +00007//
John Criswell29265fe2003-10-21 15:17:13 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This file implements the bison parser for LLVM assembly languages files.
11//
Chris Lattner44d2c352003-10-13 03:32:08 +000012//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +000013
Chris Lattner2f7c9632001-06-06 20:29:01 +000014%{
15#include "ParserInternals.h"
Chris Lattner53bdd312005-05-06 20:27:19 +000016#include "llvm/CallingConv.h"
Chris Lattnera02d6032006-01-25 22:26:43 +000017#include "llvm/InlineAsm.h"
Alkis Evlogimenosfd7a2d42004-07-29 12:17:34 +000018#include "llvm/Instructions.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000019#include "llvm/Module.h"
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000020#include "llvm/ValueSymbolTable.h"
Chris Lattnerfd9fbe12004-04-05 01:30:04 +000021#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencerfa35bb32006-12-31 05:40:12 +000022#include "llvm/Support/CommandLine.h"
Chris Lattner10aad382007-01-31 04:43:46 +000023#include "llvm/ADT/SmallVector.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000024#include "llvm/ADT/STLExtras.h"
Chris Lattnerd57ed892005-11-06 06:46:28 +000025#include "llvm/Support/MathExtras.h"
Bill Wendling6c1740f2006-11-28 22:47:12 +000026#include "llvm/Support/Streams.h"
Reid Spencer832fa922004-07-04 12:19:05 +000027#include <algorithm>
Chris Lattner2f7c9632001-06-06 20:29:01 +000028#include <list>
Chris Lattner988278c2007-02-11 21:39:35 +000029#include <map>
Chris Lattner3cd8c562002-07-30 18:54:25 +000030#include <utility>
Reid Spencerfa35bb32006-12-31 05:40:12 +000031#ifndef NDEBUG
32#define YYDEBUG 1
33#endif
Chris Lattner2f7c9632001-06-06 20:29:01 +000034
Reid Spencerb50974a2006-08-18 17:32:55 +000035// The following is a gross hack. In order to rid the libAsmParser library of
36// exceptions, we have to have a way of getting the yyparse function to go into
37// an error situation. So, whenever we want an error to occur, the GenerateError
38// function (see bottom of file) sets TriggerError. Then, at the end of each
39// production in the grammer we use CHECK_FOR_ERROR which will invoke YYERROR
40// (a goto) to put YACC in error state. Furthermore, several calls to
41// GenerateError are made from inside productions and they must simulate the
42// previous exception behavior by exiting the production immediately. We have
43// replaced these with the GEN_ERROR macro which calls GeneratError and then
44// immediately invokes YYERROR. This would be so much cleaner if it was a
45// recursive descent parser.
Reid Spencer713eedc2006-08-18 08:43:06 +000046static bool TriggerError = false;
Reid Spencerff359002006-10-09 17:36:59 +000047#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYABORT; } }
Reid Spencer713eedc2006-08-18 08:43:06 +000048#define GEN_ERROR(msg) { GenerateError(msg); YYERROR; }
49
Chris Lattner322e49a2001-10-16 19:54:17 +000050int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattnera6821822001-07-08 04:57:15 +000051int yylex(); // declaration" of xxx warnings.
Chris Lattner2f7c9632001-06-06 20:29:01 +000052int yyparse();
53
Brian Gaeke960707c2003-11-11 22:41:34 +000054namespace llvm {
Chris Lattner88357932004-07-14 06:39:48 +000055 std::string CurFilename;
Reid Spencerfa35bb32006-12-31 05:40:12 +000056#if YYDEBUG
57static cl::opt<bool>
58Debug("debug-yacc", cl::desc("Print yacc debug state changes"),
59 cl::Hidden, cl::init(false));
60#endif
Chris Lattner88357932004-07-14 06:39:48 +000061}
62using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000063
Chris Lattner2f7c9632001-06-06 20:29:01 +000064static Module *ParserResult;
Chris Lattner2f7c9632001-06-06 20:29:01 +000065
Chris Lattneraa534bf2001-09-07 16:35:17 +000066// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
67// relating to upreferences in the input stream.
68//
69//#define DEBUG_UPREFS 1
70#ifdef DEBUG_UPREFS
Bill Wendlingf3baad32006-12-07 01:30:32 +000071#define UR_OUT(X) cerr << X
Chris Lattneraa534bf2001-09-07 16:35:17 +000072#else
73#define UR_OUT(X)
74#endif
75
Vikram S. Adve7064eaf2002-07-14 22:59:28 +000076#define YYERROR_VERBOSE 1
77
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +000078static GlobalVariable *CurGV;
Andrew Lenharth9144ec42005-06-18 18:34:52 +000079
80
Chris Lattner113f4f42002-06-25 16:13:24 +000081// This contains info used when building the body of a function. It is
82// destroyed when the function is completed.
Chris Lattner2f7c9632001-06-06 20:29:01 +000083//
Chris Lattner89da8a32003-04-22 18:42:41 +000084typedef std::vector<Value *> ValueList; // Numbered defs
Reid Spencerfa35bb32006-12-31 05:40:12 +000085
Misha Brukman8b477072005-05-10 22:02:28 +000086static void
Reid Spencerfe65ae82007-03-19 18:39:36 +000087ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers=0);
Chris Lattner2f7c9632001-06-06 20:29:01 +000088
89static struct PerModuleInfo {
90 Module *CurrentModule;
Reid Spencerfe65ae82007-03-19 18:39:36 +000091 ValueList Values; // Module level numbered definitions
92 ValueList LateResolveValues;
Reid Spencerd4701792006-11-28 07:28:14 +000093 std::vector<PATypeHolder> Types;
94 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner2f7c9632001-06-06 20:29:01 +000095
Chris Lattnerd9c9c492004-07-13 08:28:21 +000096 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Reid Spenceredcf47e2006-05-29 02:34:34 +000097 /// how they were referenced and on which line of the input they came from so
Chris Lattnere84a2ba2004-07-13 08:39:15 +000098 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerd9c9c492004-07-13 08:28:21 +000099 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
100
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000101 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
102 // references to global values. Global values may be referenced before they
103 // are defined, and if so, the temporary object that they represent is held
Reid Spencerce6adaf2004-07-18 00:08:11 +0000104 // here. This is used for forward references of GlobalValues.
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000105 //
Chris Lattner89da8a32003-04-22 18:42:41 +0000106 typedef std::map<std::pair<const PointerType *,
Chris Lattner75998c02004-03-08 06:09:57 +0000107 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000108 GlobalRefsType GlobalRefs;
109
Chris Lattner2f7c9632001-06-06 20:29:01 +0000110 void ModuleDone() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000111 // If we could not resolve some functions at function compilation time
112 // (calls to functions before they are defined), resolve them now... Types
113 // are resolved when the constant pool has been completely parsed.
Chris Lattneraa534bf2001-09-07 16:35:17 +0000114 //
Chris Lattner2f7c9632001-06-06 20:29:01 +0000115 ResolveDefinitions(LateResolveValues);
Reid Spencer309080a2006-09-28 19:28:24 +0000116 if (TriggerError)
117 return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000118
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000119 // Check to make sure that all global value forward references have been
120 // resolved!
121 //
122 if (!GlobalRefs.empty()) {
Chris Lattner89da8a32003-04-22 18:42:41 +0000123 std::string UndefinedReferences = "Unresolved global references exist:\n";
Misha Brukman8b477072005-05-10 22:02:28 +0000124
Chris Lattner1a8ea8a2002-03-11 22:12:39 +0000125 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
126 I != E; ++I) {
127 UndefinedReferences += " " + I->first.first->getDescription() + " " +
128 I->first.second.getName() + "\n";
129 }
Reid Spencer713eedc2006-08-18 08:43:06 +0000130 GenerateError(UndefinedReferences);
Reid Spencer309080a2006-09-28 19:28:24 +0000131 return;
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000132 }
133
Chris Lattner113f4f42002-06-25 16:13:24 +0000134 Values.clear(); // Clear out function local definitions
Chris Lattneraa534bf2001-09-07 16:35:17 +0000135 Types.clear();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000136 CurrentModule = 0;
137 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000138
Chris Lattner4c9210e2004-07-14 21:44:00 +0000139 // GetForwardRefForGlobal - Check to see if there is a forward reference
140 // for this global. If so, remove it from the GlobalRefs map and return it.
141 // If not, just return null.
Reid Spencer78517652007-04-28 16:06:50 +0000142 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
Chris Lattner4c9210e2004-07-14 21:44:00 +0000143 // Check to see if there is a forward reference to this global variable...
144 // if there is, eliminate it and patch the reference to use the new def'n.
145 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
146 GlobalValue *Ret = 0;
147 if (I != GlobalRefs.end()) {
148 Ret = I->second;
Reid Spencer78517652007-04-28 16:06:50 +0000149 GlobalRefs.erase(I);
Chris Lattner4c9210e2004-07-14 21:44:00 +0000150 }
151 return Ret;
152 }
Reid Spencer6130ae02007-01-02 21:53:43 +0000153
154 bool TypeIsUnresolved(PATypeHolder* PATy) {
155 // If it isn't abstract, its resolved
156 const Type* Ty = PATy->get();
157 if (!Ty->isAbstract())
158 return false;
159 // Traverse the type looking for abstract types. If it isn't abstract then
160 // we don't need to traverse that leg of the type.
161 std::vector<const Type*> WorkList, SeenList;
162 WorkList.push_back(Ty);
163 while (!WorkList.empty()) {
164 const Type* Ty = WorkList.back();
165 SeenList.push_back(Ty);
166 WorkList.pop_back();
167 if (const OpaqueType* OpTy = dyn_cast<OpaqueType>(Ty)) {
168 // Check to see if this is an unresolved type
169 std::map<ValID, PATypeHolder>::iterator I = LateResolveTypes.begin();
170 std::map<ValID, PATypeHolder>::iterator E = LateResolveTypes.end();
171 for ( ; I != E; ++I) {
172 if (I->second.get() == OpTy)
173 return true;
174 }
175 } else if (const SequentialType* SeqTy = dyn_cast<SequentialType>(Ty)) {
176 const Type* TheTy = SeqTy->getElementType();
177 if (TheTy->isAbstract() && TheTy != Ty) {
178 std::vector<const Type*>::iterator I = SeenList.begin(),
179 E = SeenList.end();
180 for ( ; I != E; ++I)
181 if (*I == TheTy)
182 break;
183 if (I == E)
184 WorkList.push_back(TheTy);
185 }
186 } else if (const StructType* StrTy = dyn_cast<StructType>(Ty)) {
187 for (unsigned i = 0; i < StrTy->getNumElements(); ++i) {
188 const Type* TheTy = StrTy->getElementType(i);
189 if (TheTy->isAbstract() && TheTy != Ty) {
190 std::vector<const Type*>::iterator I = SeenList.begin(),
191 E = SeenList.end();
192 for ( ; I != E; ++I)
193 if (*I == TheTy)
194 break;
195 if (I == E)
196 WorkList.push_back(TheTy);
197 }
198 }
199 }
200 }
201 return false;
202 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000203} CurModule;
204
Chris Lattner57698e22002-03-26 18:01:55 +0000205static struct PerFunctionInfo {
Chris Lattner113f4f42002-06-25 16:13:24 +0000206 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner2f7c9632001-06-06 20:29:01 +0000207
Reid Spencerfe65ae82007-03-19 18:39:36 +0000208 ValueList Values; // Keep track of #'d definitions
209 unsigned NextValNum;
210 ValueList LateResolveValues;
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000211 bool isDeclare; // Is this function a forward declararation?
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000212 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000213 GlobalValue::VisibilityTypes Visibility;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000214
Chris Lattner4accae92004-07-14 06:28:35 +0000215 /// BBForwardRefs - When we see forward references to basic blocks, keep
216 /// track of them here.
Reid Spencerfe65ae82007-03-19 18:39:36 +0000217 std::map<ValID, BasicBlock*> BBForwardRefs;
Chris Lattner4accae92004-07-14 06:28:35 +0000218
Chris Lattner57698e22002-03-26 18:01:55 +0000219 inline PerFunctionInfo() {
220 CurrentFunction = 0;
Chris Lattner17f729e2001-07-15 06:35:53 +0000221 isDeclare = false;
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000222 Linkage = GlobalValue::ExternalLinkage;
223 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000224 }
225
Chris Lattner57698e22002-03-26 18:01:55 +0000226 inline void FunctionStart(Function *M) {
227 CurrentFunction = M;
Reid Spencerfe65ae82007-03-19 18:39:36 +0000228 NextValNum = 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000229 }
230
Chris Lattner57698e22002-03-26 18:01:55 +0000231 void FunctionDone() {
Chris Lattner4accae92004-07-14 06:28:35 +0000232 // Any forward referenced blocks left?
Reid Spencer309080a2006-09-28 19:28:24 +0000233 if (!BBForwardRefs.empty()) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000234 GenerateError("Undefined reference to label " +
Reid Spencerfe65ae82007-03-19 18:39:36 +0000235 BBForwardRefs.begin()->second->getName());
Reid Spencer309080a2006-09-28 19:28:24 +0000236 return;
237 }
Chris Lattner4accae92004-07-14 06:28:35 +0000238
239 // Resolve all forward references now.
Chris Lattner322e49a2001-10-16 19:54:17 +0000240 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000241
Chris Lattner113f4f42002-06-25 16:13:24 +0000242 Values.clear(); // Clear out function local definitions
Reid Spencerfe65ae82007-03-19 18:39:36 +0000243 BBForwardRefs.clear();
Chris Lattner57698e22002-03-26 18:01:55 +0000244 CurrentFunction = 0;
Chris Lattner17f729e2001-07-15 06:35:53 +0000245 isDeclare = false;
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000246 Linkage = GlobalValue::ExternalLinkage;
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000247 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000248 }
Chris Lattner61877742003-10-16 20:12:13 +0000249} CurFun; // Info for the current function...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000250
Chris Lattner61877742003-10-16 20:12:13 +0000251static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000252
Chris Lattner2f7c9632001-06-06 20:29:01 +0000253
254//===----------------------------------------------------------------------===//
255// Code to handle definitions of all the types
256//===----------------------------------------------------------------------===//
257
Reid Spencerfe65ae82007-03-19 18:39:36 +0000258static void InsertValue(Value *V, ValueList &ValueTab = CurFun.Values) {
259 // Things that have names or are void typed don't get slot numbers
260 if (V->hasName() || (V->getType() == Type::VoidTy))
261 return;
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000262
Reid Spencerfe65ae82007-03-19 18:39:36 +0000263 // In the case of function values, we have to allow for the forward reference
264 // of basic blocks, which are included in the numbering. Consequently, we keep
265 // track of the next insertion location with NextValNum. When a BB gets
266 // inserted, it could change the size of the CurFun.Values vector.
267 if (&ValueTab == &CurFun.Values) {
268 if (ValueTab.size() <= CurFun.NextValNum)
269 ValueTab.resize(CurFun.NextValNum+1);
270 ValueTab[CurFun.NextValNum++] = V;
271 return;
272 }
273 // For all other lists, its okay to just tack it on the back of the vector.
274 ValueTab.push_back(V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000275}
276
Chris Lattneraa534bf2001-09-07 16:35:17 +0000277static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000278 switch (D.Type) {
Reid Spencer791b8ef2007-01-26 08:04:51 +0000279 case ValID::LocalID: // Is it a numbered definition?
Chris Lattneraa534bf2001-09-07 16:35:17 +0000280 // Module constants occupy the lowest numbered slots...
Reid Spencer791b8ef2007-01-26 08:04:51 +0000281 if (D.Num < CurModule.Types.size())
282 return CurModule.Types[D.Num];
Chris Lattnerc27b1d72001-10-20 09:32:59 +0000283 break;
Reid Spencer791b8ef2007-01-26 08:04:51 +0000284 case ValID::LocalName: // Is it a named definition?
Chris Lattner8abe1a12004-07-14 23:03:46 +0000285 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
286 D.destroy(); // Free old strdup'd memory...
287 return N;
Chris Lattner98cf1f52002-11-20 18:36:02 +0000288 }
Chris Lattner8abe1a12004-07-14 23:03:46 +0000289 break;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000290 default:
Reid Spencerde1d05f2007-02-05 10:16:10 +0000291 GenerateError("Internal parser error: Invalid symbol type reference");
Reid Spencer309080a2006-09-28 19:28:24 +0000292 return 0;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000293 }
294
295 // If we reached here, we referenced either a symbol that we don't know about
296 // or an id number that hasn't been read yet. We may be referencing something
297 // forward, so just create an entry to be resolved later and get to it...
298 //
299 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
300
Chris Lattnera5f4b772005-03-21 06:27:42 +0000301
302 if (inFunctionScope()) {
Reid Spencer791b8ef2007-01-26 08:04:51 +0000303 if (D.Type == ValID::LocalName) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000304 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer309080a2006-09-28 19:28:24 +0000305 return 0;
306 } else {
Reid Spencer791b8ef2007-01-26 08:04:51 +0000307 GenerateError("Reference to an undefined type: #" + utostr(D.Num));
Reid Spencer309080a2006-09-28 19:28:24 +0000308 return 0;
309 }
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000310 }
Chris Lattneraa534bf2001-09-07 16:35:17 +0000311
Reid Spencerd4701792006-11-28 07:28:14 +0000312 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Chris Lattnera5f4b772005-03-21 06:27:42 +0000313 if (I != CurModule.LateResolveTypes.end())
Reid Spencerd4701792006-11-28 07:28:14 +0000314 return I->second;
Chris Lattnera5f4b772005-03-21 06:27:42 +0000315
Reid Spencerd4701792006-11-28 07:28:14 +0000316 Type *Typ = OpaqueType::get();
317 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
318 return Typ;
Reid Spencer812724e2006-12-03 05:45:44 +0000319 }
Chris Lattneraa534bf2001-09-07 16:35:17 +0000320
Reid Spencerfe65ae82007-03-19 18:39:36 +0000321// getExistingVal - Look up the value specified by the provided type and
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000322// the provided ValID. If the value exists and has already been defined, return
323// it. Otherwise return null.
324//
Reid Spencerfe65ae82007-03-19 18:39:36 +0000325static Value *getExistingVal(const Type *Ty, const ValID &D) {
Reid Spencer309080a2006-09-28 19:28:24 +0000326 if (isa<FunctionType>(Ty)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000327 GenerateError("Functions are not values and "
Chris Lattner57698e22002-03-26 18:01:55 +0000328 "must be referenced as pointers");
Reid Spencer309080a2006-09-28 19:28:24 +0000329 return 0;
330 }
Chris Lattner322e49a2001-10-16 19:54:17 +0000331
Chris Lattneraa534bf2001-09-07 16:35:17 +0000332 switch (D.Type) {
Reid Spencer791b8ef2007-01-26 08:04:51 +0000333 case ValID::LocalID: { // Is it a numbered definition?
Reid Spencer791b8ef2007-01-26 08:04:51 +0000334 // Check that the number is within bounds.
Reid Spencerfe65ae82007-03-19 18:39:36 +0000335 if (D.Num >= CurFun.Values.size())
336 return 0;
337 Value *Result = CurFun.Values[D.Num];
338 if (Ty != Result->getType()) {
339 GenerateError("Numbered value (%" + utostr(D.Num) + ") of type '" +
340 Result->getType()->getDescription() + "' does not match "
341 "expected type, '" + Ty->getDescription() + "'");
342 return 0;
343 }
344 return Result;
Reid Spencer791b8ef2007-01-26 08:04:51 +0000345 }
346 case ValID::GlobalID: { // Is it a numbered definition?
Reid Spencerfe65ae82007-03-19 18:39:36 +0000347 if (D.Num >= CurModule.Values.size())
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000348 return 0;
Reid Spencerfe65ae82007-03-19 18:39:36 +0000349 Value *Result = CurModule.Values[D.Num];
350 if (Ty != Result->getType()) {
351 GenerateError("Numbered value (@" + utostr(D.Num) + ") of type '" +
352 Result->getType()->getDescription() + "' does not match "
353 "expected type, '" + Ty->getDescription() + "'");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000354 return 0;
Reid Spencerfe65ae82007-03-19 18:39:36 +0000355 }
356 return Result;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000357 }
Reid Spencer791b8ef2007-01-26 08:04:51 +0000358
359 case ValID::LocalName: { // Is it a named definition?
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000360 if (!inFunctionScope())
361 return 0;
362 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
363 Value *N = SymTab.lookup(D.Name);
364 if (N == 0)
365 return 0;
366 if (N->getType() != Ty)
367 return 0;
Reid Spencer791b8ef2007-01-26 08:04:51 +0000368
369 D.destroy(); // Free old strdup'd memory...
370 return N;
371 }
372 case ValID::GlobalName: { // Is it a named definition?
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000373 ValueSymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable();
374 Value *N = SymTab.lookup(D.Name);
375 if (N == 0)
376 return 0;
377 if (N->getType() != Ty)
378 return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000379
380 D.destroy(); // Free old strdup'd memory...
381 return N;
382 }
383
Misha Brukman8b477072005-05-10 22:02:28 +0000384 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000385 // value will fit into the specified type...
386 case ValID::ConstSIntVal: // Is it a constant pool reference??
Reid Spencere0fc4df2006-10-20 07:07:24 +0000387 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000388 GenerateError("Signed integral constant '" +
Misha Brukman8b477072005-05-10 22:02:28 +0000389 itostr(D.ConstPool64) + "' is invalid for type '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +0000390 Ty->getDescription() + "'");
Reid Spencer309080a2006-09-28 19:28:24 +0000391 return 0;
392 }
Reid Spencer363fd462007-03-19 20:40:22 +0000393 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000394
395 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Reid Spencere0fc4df2006-10-20 07:07:24 +0000396 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
397 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000398 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Reid Spencerde1d05f2007-02-05 10:16:10 +0000399 "' is invalid or out of range");
Reid Spencer309080a2006-09-28 19:28:24 +0000400 return 0;
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000401 } else { // This is really a signed reference. Transmogrify.
Reid Spencer363fd462007-03-19 20:40:22 +0000402 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000403 }
404 } else {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000405 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000406 }
407
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000408 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Reid Spencer309080a2006-09-28 19:28:24 +0000409 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP)) {
Reid Spencerde1d05f2007-02-05 10:16:10 +0000410 GenerateError("FP constant invalid for type");
Reid Spencer309080a2006-09-28 19:28:24 +0000411 return 0;
412 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000413 return ConstantFP::get(Ty, D.ConstPoolFP);
Misha Brukman8b477072005-05-10 22:02:28 +0000414
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000415 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer309080a2006-09-28 19:28:24 +0000416 if (!isa<PointerType>(Ty)) {
Reid Spencerde1d05f2007-02-05 10:16:10 +0000417 GenerateError("Cannot create a a non pointer null");
Reid Spencer309080a2006-09-28 19:28:24 +0000418 return 0;
419 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000420 return ConstantPointerNull::get(cast<PointerType>(Ty));
Misha Brukman8b477072005-05-10 22:02:28 +0000421
Chris Lattner4ff31492004-10-16 18:17:13 +0000422 case ValID::ConstUndefVal: // Is it an undef value?
423 return UndefValue::get(Ty);
Misha Brukman8b477072005-05-10 22:02:28 +0000424
Chris Lattner8c7bda52005-12-21 17:53:02 +0000425 case ValID::ConstZeroVal: // Is it a zero value?
426 return Constant::getNullValue(Ty);
427
Chris Lattner7f325cd2002-08-16 21:14:40 +0000428 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer309080a2006-09-28 19:28:24 +0000429 if (D.ConstantValue->getType() != Ty) {
Reid Spencerde1d05f2007-02-05 10:16:10 +0000430 GenerateError("Constant expression type different from required type");
Reid Spencer309080a2006-09-28 19:28:24 +0000431 return 0;
432 }
Chris Lattner7f325cd2002-08-16 21:14:40 +0000433 return D.ConstantValue;
434
Chris Lattnera02d6032006-01-25 22:26:43 +0000435 case ValID::InlineAsmVal: { // Inline asm expression
436 const PointerType *PTy = dyn_cast<PointerType>(Ty);
437 const FunctionType *FTy =
438 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer309080a2006-09-28 19:28:24 +0000439 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencerde1d05f2007-02-05 10:16:10 +0000440 GenerateError("Invalid type for asm constraint string");
Reid Spencer309080a2006-09-28 19:28:24 +0000441 return 0;
442 }
Chris Lattnera02d6032006-01-25 22:26:43 +0000443 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
444 D.IAD->HasSideEffects);
445 D.destroy(); // Free InlineAsmDescriptor.
446 return IA;
447 }
Chris Lattneraa534bf2001-09-07 16:35:17 +0000448 default:
Reid Spencer9894d6c2007-02-05 17:01:20 +0000449 assert(0 && "Unhandled case!");
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000450 return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000451 } // End of switch
452
Reid Spencer9894d6c2007-02-05 17:01:20 +0000453 assert(0 && "Unhandled case!");
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000454 return 0;
455}
456
Reid Spencerfe65ae82007-03-19 18:39:36 +0000457// getVal - This function is identical to getExistingVal, except that if a
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000458// value is not already defined, it "improvises" by creating a placeholder var
459// that looks and acts just like the requested variable. When the value is
460// defined later, all uses of the placeholder variable are replaced with the
461// real thing.
462//
Chris Lattner95230b02004-07-14 01:33:11 +0000463static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer309080a2006-09-28 19:28:24 +0000464 if (Ty == Type::LabelTy) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000465 GenerateError("Cannot use a basic block here");
Reid Spencer309080a2006-09-28 19:28:24 +0000466 return 0;
467 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000468
Chris Lattner95230b02004-07-14 01:33:11 +0000469 // See if the value has already been defined.
Reid Spencerfe65ae82007-03-19 18:39:36 +0000470 Value *V = getExistingVal(Ty, ID);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000471 if (V) return V;
Reid Spencer309080a2006-09-28 19:28:24 +0000472 if (TriggerError) return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000473
Reid Spencer309080a2006-09-28 19:28:24 +0000474 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencerde1d05f2007-02-05 10:16:10 +0000475 GenerateError("Invalid use of a composite type");
Reid Spencer309080a2006-09-28 19:28:24 +0000476 return 0;
477 }
Chris Lattnerdcb82f52005-03-23 01:29:26 +0000478
Chris Lattner2f7c9632001-06-06 20:29:01 +0000479 // If we reached here, we referenced either a symbol that we don't know about
480 // or an id number that hasn't been read yet. We may be referencing something
481 // forward, so just create an entry to be resolved later and get to it...
482 //
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000483 switch (ID.Type) {
484 case ValID::GlobalName:
Reid Spencerb19636e2007-04-28 14:13:42 +0000485 case ValID::GlobalID: {
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000486 const PointerType *PTy = dyn_cast<PointerType>(Ty);
487 if (!PTy) {
488 GenerateError("Invalid type for reference to global" );
489 return 0;
490 }
491 const Type* ElTy = PTy->getElementType();
492 if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy))
493 V = new Function(FTy, GlobalValue::ExternalLinkage);
494 else
495 V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage);
496 break;
Reid Spencerb19636e2007-04-28 14:13:42 +0000497 }
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000498 default:
499 V = new Argument(Ty);
500 }
501
Chris Lattnerd9c9c492004-07-13 08:28:21 +0000502 // Remember where this forward reference came from. FIXME, shouldn't we try
503 // to recycle these things??
Chris Lattner95230b02004-07-14 01:33:11 +0000504 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerd9c9c492004-07-13 08:28:21 +0000505 llvmAsmlineno)));
506
Chris Lattner57698e22002-03-26 18:01:55 +0000507 if (inFunctionScope())
Chris Lattnerd9c9c492004-07-13 08:28:21 +0000508 InsertValue(V, CurFun.LateResolveValues);
Misha Brukman8b477072005-05-10 22:02:28 +0000509 else
Chris Lattnerd9c9c492004-07-13 08:28:21 +0000510 InsertValue(V, CurModule.LateResolveValues);
511 return V;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000512}
513
Reid Spencerfe65ae82007-03-19 18:39:36 +0000514/// defineBBVal - This is a definition of a new basic block with the specified
515/// identifier which must be the same as CurFun.NextValNum, if its numeric.
516static BasicBlock *defineBBVal(const ValID &ID) {
Reid Spencer9894d6c2007-02-05 17:01:20 +0000517 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattner95230b02004-07-14 01:33:11 +0000518
Chris Lattner4accae92004-07-14 06:28:35 +0000519 BasicBlock *BB = 0;
Chris Lattner95230b02004-07-14 01:33:11 +0000520
Reid Spencerfe65ae82007-03-19 18:39:36 +0000521 // First, see if this was forward referenced
Chris Lattner95230b02004-07-14 01:33:11 +0000522
Reid Spencerfe65ae82007-03-19 18:39:36 +0000523 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
524 if (BBI != CurFun.BBForwardRefs.end()) {
525 BB = BBI->second;
Chris Lattner4accae92004-07-14 06:28:35 +0000526 // The forward declaration could have been inserted anywhere in the
527 // function: insert it into the correct place now.
528 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
529 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Reid Spencerfe65ae82007-03-19 18:39:36 +0000530
Reid Spencerd05bc522007-03-20 01:13:00 +0000531 // We're about to erase the entry, save the key so we can clean it up.
532 ValID Tmp = BBI->first;
533
Reid Spencerfe65ae82007-03-19 18:39:36 +0000534 // Erase the forward ref from the map as its no longer "forward"
535 CurFun.BBForwardRefs.erase(ID);
536
Reid Spencerd05bc522007-03-20 01:13:00 +0000537 // The key has been removed from the map but so we don't want to leave
538 // strdup'd memory around so destroy it too.
539 Tmp.destroy();
540
Reid Spencerfe65ae82007-03-19 18:39:36 +0000541 // If its a numbered definition, bump the number and set the BB value.
542 if (ID.Type == ValID::LocalID) {
543 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
544 InsertValue(BB);
545 }
546
547 ID.destroy();
548 return BB;
549 }
550
551 // We haven't seen this BB before and its first mention is a definition.
552 // Just create it and return it.
553 std::string Name (ID.Type == ValID::LocalName ? ID.Name : "");
554 BB = new BasicBlock(Name, CurFun.CurrentFunction);
555 if (ID.Type == ValID::LocalID) {
556 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
557 InsertValue(BB);
Chris Lattner4accae92004-07-14 06:28:35 +0000558 }
Reid Spencerfe65ae82007-03-19 18:39:36 +0000559
560 ID.destroy(); // Free strdup'd memory
561 return BB;
562}
563
564/// getBBVal - get an existing BB value or create a forward reference for it.
565///
566static BasicBlock *getBBVal(const ValID &ID) {
567 assert(inFunctionScope() && "Can't get basic block at global scope!");
568
569 BasicBlock *BB = 0;
570
571 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
572 if (BBI != CurFun.BBForwardRefs.end()) {
573 BB = BBI->second;
574 } if (ID.Type == ValID::LocalName) {
575 std::string Name = ID.Name;
576 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
577 if (N)
578 if (N->getType()->getTypeID() == Type::LabelTyID)
579 BB = cast<BasicBlock>(N);
580 else
581 GenerateError("Reference to label '" + Name + "' is actually of type '"+
582 N->getType()->getDescription() + "'");
583 } else if (ID.Type == ValID::LocalID) {
584 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
585 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
586 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
587 else
588 GenerateError("Reference to label '%" + utostr(ID.Num) +
589 "' is actually of type '"+
590 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
591 }
592 } else {
593 GenerateError("Illegal label reference " + ID.getName());
594 return 0;
595 }
596
597 // If its already been defined, return it now.
598 if (BB) {
599 ID.destroy(); // Free strdup'd memory.
600 return BB;
601 }
602
603 // Otherwise, this block has not been seen before, create it.
604 std::string Name;
605 if (ID.Type == ValID::LocalName)
606 Name = ID.Name;
607 BB = new BasicBlock(Name, CurFun.CurrentFunction);
608
609 // Insert it in the forward refs map.
610 CurFun.BBForwardRefs[ID] = BB;
611
Chris Lattner95230b02004-07-14 01:33:11 +0000612 return BB;
613}
614
Chris Lattner2f7c9632001-06-06 20:29:01 +0000615
616//===----------------------------------------------------------------------===//
617// Code to handle forward references in instructions
618//===----------------------------------------------------------------------===//
619//
620// This code handles the late binding needed with statements that reference
621// values not defined yet... for example, a forward branch, or the PHI node for
622// a loop body.
623//
Chris Lattner61877742003-10-16 20:12:13 +0000624// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner2f7c9632001-06-06 20:29:01 +0000625// and back patchs after we are done.
626//
627
Misha Brukman8b477072005-05-10 22:02:28 +0000628// ResolveDefinitions - If we could not resolve some defs at parsing
629// time (forward branches, phi functions for loops, etc...) resolve the
Chris Lattner2f7c9632001-06-06 20:29:01 +0000630// defs now...
631//
Misha Brukman8b477072005-05-10 22:02:28 +0000632static void
Reid Spencerfe65ae82007-03-19 18:39:36 +0000633ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000634 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencerfe65ae82007-03-19 18:39:36 +0000635 while (!LateResolvers.empty()) {
636 Value *V = LateResolvers.back();
637 LateResolvers.pop_back();
Chris Lattnerd9c9c492004-07-13 08:28:21 +0000638
Reid Spencerfe65ae82007-03-19 18:39:36 +0000639 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
640 CurModule.PlaceHolderInfo.find(V);
641 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattnerd9c9c492004-07-13 08:28:21 +0000642
Reid Spencerfe65ae82007-03-19 18:39:36 +0000643 ValID &DID = PHI->second.first;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000644
Reid Spencerfe65ae82007-03-19 18:39:36 +0000645 Value *TheRealValue = getExistingVal(V->getType(), DID);
646 if (TriggerError)
647 return;
648 if (TheRealValue) {
649 V->replaceAllUsesWith(TheRealValue);
650 delete V;
651 CurModule.PlaceHolderInfo.erase(PHI);
652 } else if (FutureLateResolvers) {
653 // Functions have their unresolved items forwarded to the module late
654 // resolver table
655 InsertValue(V, *FutureLateResolvers);
656 } else {
657 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
658 GenerateError("Reference to an invalid definition: '" +DID.getName()+
659 "' of type '" + V->getType()->getDescription() + "'",
660 PHI->second.second);
Reid Spencer309080a2006-09-28 19:28:24 +0000661 return;
Chris Lattner322e49a2001-10-16 19:54:17 +0000662 } else {
Reid Spencerfe65ae82007-03-19 18:39:36 +0000663 GenerateError("Reference to an invalid definition: #" +
664 itostr(DID.Num) + " of type '" +
665 V->getType()->getDescription() + "'",
666 PHI->second.second);
667 return;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000668 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000669 }
670 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000671 LateResolvers.clear();
672}
673
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000674// ResolveTypeTo - A brand new type was just declared. This means that (if
675// name is not null) things referencing Name can be resolved. Otherwise, things
676// refering to the number can be resolved. Do this now.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000677//
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000678static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattnera5f4b772005-03-21 06:27:42 +0000679 ValID D;
Reid Spencer791b8ef2007-01-26 08:04:51 +0000680 if (Name) D = ValID::createLocalName(Name);
681 else D = ValID::createLocalID(CurModule.Types.size());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000682
Reid Spencerd4701792006-11-28 07:28:14 +0000683 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattnera5f4b772005-03-21 06:27:42 +0000684 CurModule.LateResolveTypes.find(D);
685 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencerd4701792006-11-28 07:28:14 +0000686 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattnera5f4b772005-03-21 06:27:42 +0000687 CurModule.LateResolveTypes.erase(I);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000688 }
689}
690
Chris Lattner7fb14f52001-09-18 04:00:54 +0000691// setValueName - Set the specified value to the name given. The name may be
692// null potentially, in which case this is a noop. The string passed in is
Chris Lattner2ed776b2004-07-13 07:59:27 +0000693// assumed to be a malloc'd string buffer, and is free'd by this function.
694//
695static void setValueName(Value *V, char *NameStr) {
Reid Spencer791b8ef2007-01-26 08:04:51 +0000696 if (!NameStr) return;
697 std::string Name(NameStr); // Copy string
698 free(NameStr); // Free old string
Chris Lattner30b35cf2004-07-13 08:12:39 +0000699
Reid Spencer791b8ef2007-01-26 08:04:51 +0000700 if (V->getType() == Type::VoidTy) {
Reid Spencerde1d05f2007-02-05 10:16:10 +0000701 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencer791b8ef2007-01-26 08:04:51 +0000702 return;
Chris Lattner2ed776b2004-07-13 07:59:27 +0000703 }
Reid Spencer791b8ef2007-01-26 08:04:51 +0000704
Reid Spencer9894d6c2007-02-05 17:01:20 +0000705 assert(inFunctionScope() && "Must be in function scope!");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000706 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
707 if (ST.lookup(Name)) {
Reid Spencer791b8ef2007-01-26 08:04:51 +0000708 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +0000709 V->getType()->getDescription() + "'");
Reid Spencer791b8ef2007-01-26 08:04:51 +0000710 return;
711 }
712
713 // Set the name.
714 V->setName(Name);
Chris Lattner2ed776b2004-07-13 07:59:27 +0000715}
716
Chris Lattner42dd4742004-07-14 08:23:52 +0000717/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
718/// this is a declaration, otherwise it is a definition.
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +0000719static GlobalVariable *
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000720ParseGlobalVariable(char *NameStr,
721 GlobalValue::LinkageTypes Linkage,
722 GlobalValue::VisibilityTypes Visibility,
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +0000723 bool isConstantGlobal, const Type *Ty,
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000724 Constant *Initializer, bool IsThreadLocal) {
Reid Spencer309080a2006-09-28 19:28:24 +0000725 if (isa<FunctionType>(Ty)) {
Reid Spencerde1d05f2007-02-05 10:16:10 +0000726 GenerateError("Cannot declare global vars of function type");
Reid Spencer309080a2006-09-28 19:28:24 +0000727 return 0;
728 }
Chris Lattnere875d482004-07-14 20:42:57 +0000729
Misha Brukman8b477072005-05-10 22:02:28 +0000730 const PointerType *PTy = PointerType::get(Ty);
Chris Lattner8abe1a12004-07-14 23:03:46 +0000731
Chris Lattnere875d482004-07-14 20:42:57 +0000732 std::string Name;
733 if (NameStr) {
734 Name = NameStr; // Copy string
735 free(NameStr); // Free old string
Chris Lattner42dd4742004-07-14 08:23:52 +0000736 }
Chris Lattnere875d482004-07-14 20:42:57 +0000737
Chris Lattner4c9210e2004-07-14 21:44:00 +0000738 // See if this global value was forward referenced. If so, recycle the
739 // object.
Misha Brukman8b477072005-05-10 22:02:28 +0000740 ValID ID;
Chris Lattner4c9210e2004-07-14 21:44:00 +0000741 if (!Name.empty()) {
Reid Spencer791b8ef2007-01-26 08:04:51 +0000742 ID = ValID::createGlobalName((char*)Name.c_str());
Chris Lattnere875d482004-07-14 20:42:57 +0000743 } else {
Reid Spencerfe65ae82007-03-19 18:39:36 +0000744 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner4c9210e2004-07-14 21:44:00 +0000745 }
746
Reid Spencer78517652007-04-28 16:06:50 +0000747 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
Misha Brukman8b477072005-05-10 22:02:28 +0000748 // Move the global to the end of the list, from whereever it was
Chris Lattner4c9210e2004-07-14 21:44:00 +0000749 // previously inserted.
750 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
751 CurModule.CurrentModule->getGlobalList().remove(GV);
752 CurModule.CurrentModule->getGlobalList().push_back(GV);
753 GV->setInitializer(Initializer);
754 GV->setLinkage(Linkage);
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000755 GV->setVisibility(Visibility);
Chris Lattner4c9210e2004-07-14 21:44:00 +0000756 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000757 GV->setThreadLocal(IsThreadLocal);
Chris Lattnerfe050242004-07-16 01:18:09 +0000758 InsertValue(GV, CurModule.Values);
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +0000759 return GV;
Chris Lattnere875d482004-07-14 20:42:57 +0000760 }
Chris Lattner8abe1a12004-07-14 23:03:46 +0000761
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000762 // If this global has a name
Chris Lattner8abe1a12004-07-14 23:03:46 +0000763 if (!Name.empty()) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000764 // if the global we're parsing has an initializer (is a definition) and
765 // has external linkage.
766 if (Initializer && Linkage != GlobalValue::InternalLinkage)
767 // If there is already a global with external linkage with this name
768 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
769 // If we allow this GVar to get created, it will be renamed in the
770 // symbol table because it conflicts with an existing GVar. We can't
771 // allow redefinition of GVars whose linking indicates that their name
772 // must stay the same. Issue the error.
773 GenerateError("Redefinition of global variable named '" + Name +
774 "' of type '" + Ty->getDescription() + "'");
775 return 0;
776 }
Chris Lattner8abe1a12004-07-14 23:03:46 +0000777 }
778
779 // Otherwise there is no existing GV to use, create one now.
Chris Lattnerfe050242004-07-16 01:18:09 +0000780 GlobalVariable *GV =
Misha Brukman8b477072005-05-10 22:02:28 +0000781 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000782 CurModule.CurrentModule, IsThreadLocal);
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000783 GV->setVisibility(Visibility);
Chris Lattnerfe050242004-07-16 01:18:09 +0000784 InsertValue(GV, CurModule.Values);
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +0000785 return GV;
Chris Lattner42dd4742004-07-14 08:23:52 +0000786}
Chris Lattner2ed776b2004-07-13 07:59:27 +0000787
Reid Spencer5b4413c2004-05-26 21:48:31 +0000788// setTypeName - Set the specified type to the name given. The name may be
789// null potentially, in which case this is a noop. The string passed in is
790// assumed to be a malloc'd string buffer, and is freed by this function.
791//
792// This function returns true if the type has already been defined, but is
793// allowed to be redefined in the specified context. If the name is a new name
794// for the type plane, it is inserted and false is returned.
Chris Lattner85a351f2004-07-13 08:10:10 +0000795static bool setTypeName(const Type *T, char *NameStr) {
Reid Spencer9894d6c2007-02-05 17:01:20 +0000796 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer5b4413c2004-05-26 21:48:31 +0000797 if (NameStr == 0) return false;
Misha Brukman8b477072005-05-10 22:02:28 +0000798
Reid Spencer5b4413c2004-05-26 21:48:31 +0000799 std::string Name(NameStr); // Copy string
800 free(NameStr); // Free old string
801
802 // We don't allow assigning names to void type
Reid Spencer309080a2006-09-28 19:28:24 +0000803 if (T == Type::VoidTy) {
Reid Spencerde1d05f2007-02-05 10:16:10 +0000804 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer309080a2006-09-28 19:28:24 +0000805 return false;
806 }
Reid Spencer5b4413c2004-05-26 21:48:31 +0000807
Chris Lattner8abe1a12004-07-14 23:03:46 +0000808 // Set the type name, checking for conflicts as we do so.
809 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer5b4413c2004-05-26 21:48:31 +0000810
Chris Lattner8abe1a12004-07-14 23:03:46 +0000811 if (AlreadyExists) { // Inserting a name that is already defined???
812 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencer9894d6c2007-02-05 17:01:20 +0000813 assert(Existing && "Conflict but no matching type?!");
Chris Lattner8abe1a12004-07-14 23:03:46 +0000814
Reid Spencer5b4413c2004-05-26 21:48:31 +0000815 // There is only one case where this is allowed: when we are refining an
816 // opaque type. In this case, Existing will be an opaque type.
817 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
818 // We ARE replacing an opaque type!
Chris Lattner85a351f2004-07-13 08:10:10 +0000819 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer5b4413c2004-05-26 21:48:31 +0000820 return true;
821 }
822
823 // Otherwise, this is an attempt to redefine a type. That's okay if
824 // the redefinition is identical to the original. This will be so if
825 // Existing and T point to the same Type object. In this one case we
826 // allow the equivalent redefinition.
827 if (Existing == T) return true; // Yes, it's equal.
828
829 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer6c435f02007-01-05 21:50:38 +0000830 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +0000831 T->getDescription() + "'");
Reid Spencer5b4413c2004-05-26 21:48:31 +0000832 }
833
Reid Spencer5b4413c2004-05-26 21:48:31 +0000834 return false;
835}
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000836
Chris Lattneraa534bf2001-09-07 16:35:17 +0000837//===----------------------------------------------------------------------===//
838// Code for handling upreferences in type names...
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000839//
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000840
Chris Lattner3cb41672004-02-09 03:19:29 +0000841// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattneraa534bf2001-09-07 16:35:17 +0000842//
843static bool TypeContains(const Type *Ty, const Type *E) {
Misha Brukman8b477072005-05-10 22:02:28 +0000844 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
Chris Lattner4270bcb2004-12-08 16:13:53 +0000845 E) != Ty->subtype_end();
Chris Lattner3cb41672004-02-09 03:19:29 +0000846}
847
848namespace {
849 struct UpRefRecord {
850 // NestingLevel - The number of nesting levels that need to be popped before
851 // this type is resolved.
852 unsigned NestingLevel;
Misha Brukman8b477072005-05-10 22:02:28 +0000853
Chris Lattner3cb41672004-02-09 03:19:29 +0000854 // LastContainedTy - This is the type at the current binding level for the
855 // type. Every time we reduce the nesting level, this gets updated.
856 const Type *LastContainedTy;
857
858 // UpRefTy - This is the actual opaque type that the upreference is
859 // represented with.
860 OpaqueType *UpRefTy;
861
862 UpRefRecord(unsigned NL, OpaqueType *URTy)
863 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
864 };
Chris Lattneraa534bf2001-09-07 16:35:17 +0000865}
Chris Lattner1caf0bb2001-07-20 19:15:08 +0000866
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000867// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3cb41672004-02-09 03:19:29 +0000868static std::vector<UpRefRecord> UpRefs;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000869
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000870/// HandleUpRefs - Every time we finish a new layer of types, this function is
871/// called. It loops through the UpRefs vector, which is a list of the
872/// currently active types. For each type, if the up reference is contained in
873/// the newly completed type, we decrement the level count. When the level
874/// count reaches zero, the upreferenced type is the type that is passed in:
875/// thus we can complete the cycle.
876///
Chris Lattner30752bd92002-04-04 19:23:55 +0000877static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner682e17c2006-08-18 17:34:24 +0000878 // If Ty isn't abstract, or if there are no up-references in it, then there is
879 // nothing to resolve here.
880 if (!ty->isAbstract() || UpRefs.empty()) return ty;
881
Chris Lattner30752bd92002-04-04 19:23:55 +0000882 PATypeHolder Ty(ty);
Misha Brukman8b477072005-05-10 22:02:28 +0000883 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattnerf29b2312001-11-02 07:46:26 +0000884 "' newly formed. Resolving upreferences.\n" <<
885 UpRefs.size() << " upreferences active!\n");
Chris Lattneredd45002004-02-09 18:53:54 +0000886
887 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
888 // to zero), we resolve them all together before we resolve them to Ty. At
889 // the end of the loop, if there is anything to resolve to Ty, it will be in
890 // this variable.
891 OpaqueType *TypeToResolve = 0;
892
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000893 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Misha Brukman8b477072005-05-10 22:02:28 +0000894 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
895 << UpRefs[i].second->getDescription() << ") = "
Reid Spencerce6adaf2004-07-18 00:08:11 +0000896 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3cb41672004-02-09 03:19:29 +0000897 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
898 // Decrement level of upreference
899 unsigned Level = --UpRefs[i].NestingLevel;
900 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000901 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Misha Brukman8b477072005-05-10 22:02:28 +0000902 if (Level == 0) { // Upreference should be resolved!
Chris Lattneredd45002004-02-09 18:53:54 +0000903 if (!TypeToResolve) {
904 TypeToResolve = UpRefs[i].UpRefTy;
905 } else {
906 UR_OUT(" * Resolving upreference for "
907 << UpRefs[i].second->getDescription() << "\n";
908 std::string OldName = UpRefs[i].UpRefTy->getDescription());
909 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
910 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
911 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
912 }
Reid Spencerce6adaf2004-07-18 00:08:11 +0000913 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000914 --i; // Do not skip the next element...
Chris Lattneraa534bf2001-09-07 16:35:17 +0000915 }
916 }
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000917 }
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000918
Chris Lattneredd45002004-02-09 18:53:54 +0000919 if (TypeToResolve) {
920 UR_OUT(" * Resolving upreference for "
921 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner7289e622004-02-09 21:03:38 +0000922 std::string OldName = TypeToResolve->getDescription());
Chris Lattneredd45002004-02-09 18:53:54 +0000923 TypeToResolve->refineAbstractTypeTo(Ty);
924 }
925
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000926 return Ty;
927}
928
Chris Lattner2f7c9632001-06-06 20:29:01 +0000929//===----------------------------------------------------------------------===//
930// RunVMAsmParser - Define an interface to this parser
931//===----------------------------------------------------------------------===//
932//
Reid Spencerfa35bb32006-12-31 05:40:12 +0000933static Module* RunParser(Module * M);
934
Chris Lattner88357932004-07-14 06:39:48 +0000935Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner416a0d42005-05-20 03:25:47 +0000936 set_scan_file(F);
937
Chris Lattnerf2d1e792001-07-22 18:36:00 +0000938 CurFilename = Filename;
Chris Lattner416a0d42005-05-20 03:25:47 +0000939 return RunParser(new Module(CurFilename));
940}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000941
Chris Lattner416a0d42005-05-20 03:25:47 +0000942Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
943 set_scan_string(AsmString);
Chris Lattner6789a0b2003-11-26 07:24:58 +0000944
Chris Lattner416a0d42005-05-20 03:25:47 +0000945 CurFilename = "from_memory";
946 if (M == NULL) {
947 return RunParser(new Module (CurFilename));
948 } else {
949 return RunParser(M);
950 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000951}
952
953%}
954
955%union {
Brian Gaeke960707c2003-11-11 22:41:34 +0000956 llvm::Module *ModuleVal;
957 llvm::Function *FunctionVal;
Brian Gaeke960707c2003-11-11 22:41:34 +0000958 llvm::BasicBlock *BasicBlockVal;
959 llvm::TerminatorInst *TermInstVal;
960 llvm::Instruction *InstVal;
Reid Spencer812724e2006-12-03 05:45:44 +0000961 llvm::Constant *ConstVal;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000962
Reid Spencer812724e2006-12-03 05:45:44 +0000963 const llvm::Type *PrimType;
Reid Spencerfa35bb32006-12-31 05:40:12 +0000964 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencer812724e2006-12-03 05:45:44 +0000965 llvm::PATypeHolder *TypeVal;
966 llvm::Value *ValueVal;
Reid Spencer812724e2006-12-03 05:45:44 +0000967 std::vector<llvm::Value*> *ValueList;
Reid Spencerfa35bb32006-12-31 05:40:12 +0000968 llvm::ArgListType *ArgList;
969 llvm::TypeWithAttrs TypeWithAttrs;
970 llvm::TypeWithAttrsList *TypeWithAttrsList;
971 llvm::ValueRefList *ValueRefList;
972
Misha Brukman8b477072005-05-10 22:02:28 +0000973 // Represent the RHS of PHI node
Reid Spencer812724e2006-12-03 05:45:44 +0000974 std::list<std::pair<llvm::Value*,
975 llvm::BasicBlock*> > *PHIList;
Brian Gaeke960707c2003-11-11 22:41:34 +0000976 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencer812724e2006-12-03 05:45:44 +0000977 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000978
Brian Gaeke960707c2003-11-11 22:41:34 +0000979 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000980 llvm::GlobalValue::VisibilityTypes Visibility;
Reid Spencer71b79e32007-04-09 06:17:21 +0000981 uint16_t ParamAttrs;
Reid Spencer9875fc12007-02-28 02:23:44 +0000982 llvm::APInt *APIntVal;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000983 int64_t SInt64Val;
984 uint64_t UInt64Val;
985 int SIntVal;
986 unsigned UIntVal;
987 double FPVal;
Chris Lattner7fb14f52001-09-18 04:00:54 +0000988 bool BoolVal;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000989
Chris Lattneraa534bf2001-09-07 16:35:17 +0000990 char *StrVal; // This memory is strdup'd!
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000991 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000992
Reid Spencer812724e2006-12-03 05:45:44 +0000993 llvm::Instruction::BinaryOps BinaryOpVal;
994 llvm::Instruction::TermOps TermOpVal;
995 llvm::Instruction::MemoryOps MemOpVal;
996 llvm::Instruction::CastOps CastOpVal;
997 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencer812724e2006-12-03 05:45:44 +0000998 llvm::ICmpInst::Predicate IPredicate;
999 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001000}
1001
Reid Spencerfa35bb32006-12-31 05:40:12 +00001002%type <ModuleVal> Module
Chris Lattner57698e22002-03-26 18:01:55 +00001003%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner2f7c9632001-06-06 20:29:01 +00001004%type <BasicBlockVal> BasicBlock InstructionList
1005%type <TermInstVal> BBTerminatorInst
1006%type <InstVal> Inst InstVal MemoryInst
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +00001007%type <ConstVal> ConstVal ConstExpr AliaseeRef
Chris Lattner476148f2001-11-26 16:54:11 +00001008%type <ConstVector> ConstVector
Chris Lattnerae234e12002-04-09 19:41:42 +00001009%type <ArgList> ArgList ArgListH
Chris Lattner931ef3b2001-06-11 15:04:20 +00001010%type <PHIList> PHIList
Reid Spencerfa35bb32006-12-31 05:40:12 +00001011%type <ValueRefList> ValueRefList // For call param lists & GEP indices
1012%type <ValueList> IndexList // For GEP indices
1013%type <TypeList> TypeListI
1014%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer136a91c2007-01-05 17:06:19 +00001015%type <TypeWithAttrs> ArgType
Chris Lattner2f7c9632001-06-06 20:29:01 +00001016%type <JumpTable> JumpTable
Chris Lattner379a8d22003-04-16 20:28:45 +00001017%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00001018%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattner8b1680e2003-09-08 18:20:29 +00001019%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner06038452005-05-06 05:51:46 +00001020%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattnera02d6032006-01-25 22:26:43 +00001021%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencerfa35bb32006-12-31 05:40:12 +00001022%type <Linkage> GVInternalLinkage GVExternalLinkage
1023%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001024%type <Linkage> AliasLinkage
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001025%type <Visibility> GVVisibilityStyle
Chris Lattner2f7c9632001-06-06 20:29:01 +00001026
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001027// ValueRef - Unresolved reference to a definition or BB
1028%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattner252afba2001-07-26 16:29:15 +00001029%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner2f7c9632001-06-06 20:29:01 +00001030// Tokens and types for handling constant integer values
1031//
1032// ESINT64VAL - A negative number within long long range
1033%token <SInt64Val> ESINT64VAL
1034
1035// EUINT64VAL - A positive number within uns. long long range
1036%token <UInt64Val> EUINT64VAL
Chris Lattner2f7c9632001-06-06 20:29:01 +00001037
Reid Spencer9875fc12007-02-28 02:23:44 +00001038// ESAPINTVAL - A negative number with arbitrary precision
1039%token <APIntVal> ESAPINTVAL
1040
1041// EUAPINTVAL - A positive number with arbitrary precision
1042%token <APIntVal> EUAPINTVAL
1043
Reid Spencer791b8ef2007-01-26 08:04:51 +00001044%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattner212f70d2001-07-15 00:17:01 +00001045%token <FPVal> FPVAL // Float or Double constant
Chris Lattner2f7c9632001-06-06 20:29:01 +00001046
1047// Built in types...
Reid Spencer136a91c2007-01-05 17:06:19 +00001048%type <TypeVal> Types ResultTypes
Reid Spencerfa35bb32006-12-31 05:40:12 +00001049%type <PrimType> IntType FPType PrimType // Classifications
Reid Spencer502d64e2007-01-13 05:00:20 +00001050%token <PrimType> VOID INTTYPE
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001051%token <PrimType> FLOAT DOUBLE LABEL
1052%token TYPE
Chris Lattner2f7c9632001-06-06 20:29:01 +00001053
Reid Spenceraba0cc72007-05-19 07:21:26 +00001054%token<StrVal> LOCALVAR GLOBALVAR LABELSTR
1055%token<StrVal> STRINGCONSTANT ATSTRINGCONSTANT PCTSTRINGCONSTANT
Reid Spencer791b8ef2007-01-26 08:04:51 +00001056%type <StrVal> LocalName OptLocalName OptLocalAssign
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001057%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Reid Spencer791b8ef2007-01-26 08:04:51 +00001058%type <UIntVal> OptAlign OptCAlign
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +00001059%type <StrVal> OptSection SectionString
Chris Lattner2f7c9632001-06-06 20:29:01 +00001060
Reid Spencerbef90fe2007-04-09 01:55:42 +00001061%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001062%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
Reid Spencerfa35bb32006-12-31 05:40:12 +00001063%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001064%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Reid Spencer791b8ef2007-01-26 08:04:51 +00001065%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN
Chris Lattnera02d6032006-01-25 22:26:43 +00001066%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001067%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattnerfd3d29a2006-10-22 06:07:41 +00001068%token DATALAYOUT
Chris Lattner53bdd312005-05-06 20:27:19 +00001069%type <UIntVal> OptCallingConv
Reid Spencer136a91c2007-01-05 17:06:19 +00001070%type <ParamAttrs> OptParamAttrs ParamAttr
1071%type <ParamAttrs> OptFuncAttrs FuncAttr
Chris Lattner2f7c9632001-06-06 20:29:01 +00001072
Misha Brukman8b477072005-05-10 22:02:28 +00001073// Basic Block Terminating Operators
Chris Lattner4ff31492004-10-16 18:17:13 +00001074%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner2f7c9632001-06-06 20:29:01 +00001075
Misha Brukman8b477072005-05-10 22:02:28 +00001076// Binary Operators
Reid Spencer266e42b2006-12-23 06:05:41 +00001077%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencer7eb55b32006-11-02 01:53:59 +00001078%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer2341c222007-02-02 02:16:23 +00001079%token <BinaryOpVal> SHL LSHR ASHR
1080
Reid Spencer812724e2006-12-03 05:45:44 +00001081%token <OtherOpVal> ICMP FCMP
Reid Spencer812724e2006-12-03 05:45:44 +00001082%type <IPredicate> IPredicates
Reid Spencer812724e2006-12-03 05:45:44 +00001083%type <FPredicate> FPredicates
Reid Spencercfc7f5d2006-12-03 06:58:07 +00001084%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1085%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner2f7c9632001-06-06 20:29:01 +00001086
1087// Memory Instructions
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001088%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner2f7c9632001-06-06 20:29:01 +00001089
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001090// Cast Operators
1091%type <CastOpVal> CastOps
1092%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1093%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1094
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001095// Other Operators
Reid Spencer2341c222007-02-02 02:16:23 +00001096%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattner8ec3c2e2006-04-08 01:18:35 +00001097%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Chris Lattner06038452005-05-06 05:51:46 +00001098
Reid Spencer136a91c2007-01-05 17:06:19 +00001099// Function Attributes
Reid Spencera4835772007-03-22 02:13:23 +00001100%token NORETURN INREG SRET NOUNWIND
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001101
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001102// Visibility Styles
Anton Korobeynikov39f3cff2007-04-29 18:35:00 +00001103%token DEFAULT HIDDEN PROTECTED
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001104
Chris Lattner2f7c9632001-06-06 20:29:01 +00001105%start Module
1106%%
1107
Chris Lattner2f7c9632001-06-06 20:29:01 +00001108
Misha Brukman8b477072005-05-10 22:02:28 +00001109// Operations that are notably excluded from this list include:
Chris Lattner2f7c9632001-06-06 20:29:01 +00001110// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1111//
Reid Spencer7eb55b32006-11-02 01:53:59 +00001112ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer2341c222007-02-02 02:16:23 +00001113LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001114CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1115 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer2341c222007-02-02 02:16:23 +00001116
Reid Spencercfc7f5d2006-12-03 06:58:07 +00001117IPredicates
Reid Spencerf3490692006-12-04 05:20:06 +00001118 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencercfc7f5d2006-12-03 06:58:07 +00001119 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1120 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1121 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1122 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1123 ;
1124
1125FPredicates
1126 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1127 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1128 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1129 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1130 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1131 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1132 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1133 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1134 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1135 ;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001136
Chris Lattner56f73d42001-07-14 06:10:16 +00001137// These are some types that allow classification if we only want a particular
1138// thing... for example, only a signed, unsigned, or integral type.
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001139IntType : INTTYPE;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001140FPType : FLOAT | DOUBLE;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001141
Reid Spenceraba0cc72007-05-19 07:21:26 +00001142LocalName : LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT
Reid Spencer791b8ef2007-01-26 08:04:51 +00001143OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1144
1145/// OptLocalAssign - Value producing statements have an optional assignment
1146/// component.
1147OptLocalAssign : LocalName '=' {
1148 $$ = $1;
1149 CHECK_FOR_ERROR
1150 }
1151 | /*empty*/ {
1152 $$ = 0;
1153 CHECK_FOR_ERROR
1154 };
1155
1156GlobalName : GLOBALVAR | ATSTRINGCONSTANT;
1157
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001158OptGlobalAssign : GlobalAssign
Misha Brukman8b477072005-05-10 22:02:28 +00001159 | /*empty*/ {
1160 $$ = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00001161 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001162 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001163
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001164GlobalAssign : GlobalName '=' {
1165 $$ = $1;
1166 CHECK_FOR_ERROR
Anton Korobeynikov5e4e8f52007-04-25 18:07:40 +00001167 };
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001168
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001169GVInternalLinkage
1170 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1171 | WEAK { $$ = GlobalValue::WeakLinkage; }
1172 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1173 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1174 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1175 ;
1176
1177GVExternalLinkage
1178 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1179 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1180 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1181 ;
1182
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001183GVVisibilityStyle
Anton Korobeynikov39f3cff2007-04-29 18:35:00 +00001184 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1185 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1186 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1187 | PROTECTED { $$ = GlobalValue::ProtectedVisibility; }
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001188 ;
1189
Reid Spencerfa35bb32006-12-31 05:40:12 +00001190FunctionDeclareLinkage
1191 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1192 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1193 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001194 ;
1195
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001196FunctionDefineLinkage
Reid Spencerfa35bb32006-12-31 05:40:12 +00001197 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1198 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001199 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1200 | WEAK { $$ = GlobalValue::WeakLinkage; }
1201 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001202 ;
Chris Lattneraa534bf2001-09-07 16:35:17 +00001203
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001204AliasLinkage
1205 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1206 | WEAK { $$ = GlobalValue::WeakLinkage; }
1207 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1208 ;
1209
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +00001210OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1211 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +00001212 FASTCC_TOK { $$ = CallingConv::Fast; } |
1213 COLDCC_TOK { $$ = CallingConv::Cold; } |
1214 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1215 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1216 CC_TOK EUINT64VAL {
Chris Lattner53bdd312005-05-06 20:27:19 +00001217 if ((unsigned)$2 != $2)
Reid Spencerde1d05f2007-02-05 10:16:10 +00001218 GEN_ERROR("Calling conv too large");
Chris Lattner53bdd312005-05-06 20:27:19 +00001219 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001220 CHECK_FOR_ERROR
Chris Lattner53bdd312005-05-06 20:27:19 +00001221 };
1222
Reid Spencera472f662007-04-11 02:44:20 +00001223ParamAttr : ZEXT { $$ = ParamAttr::ZExt; }
1224 | SEXT { $$ = ParamAttr::SExt; }
1225 | INREG { $$ = ParamAttr::InReg; }
1226 | SRET { $$ = ParamAttr::StructRet; }
Reid Spencerfa35bb32006-12-31 05:40:12 +00001227 ;
1228
Reid Spencera472f662007-04-11 02:44:20 +00001229OptParamAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer136a91c2007-01-05 17:06:19 +00001230 | OptParamAttrs ParamAttr {
Reid Spencer71b79e32007-04-09 06:17:21 +00001231 $$ = $1 | $2;
Reid Spencerfa35bb32006-12-31 05:40:12 +00001232 }
1233 ;
1234
Reid Spencera472f662007-04-11 02:44:20 +00001235FuncAttr : NORETURN { $$ = ParamAttr::NoReturn; }
1236 | NOUNWIND { $$ = ParamAttr::NoUnwind; }
Reid Spencer136a91c2007-01-05 17:06:19 +00001237 | ParamAttr
1238 ;
1239
Reid Spencera472f662007-04-11 02:44:20 +00001240OptFuncAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer136a91c2007-01-05 17:06:19 +00001241 | OptFuncAttrs FuncAttr {
Reid Spencer71b79e32007-04-09 06:17:21 +00001242 $$ = $1 | $2;
Reid Spencer136a91c2007-01-05 17:06:19 +00001243 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00001244 ;
1245
Chris Lattnerc7de8362005-11-06 06:34:12 +00001246// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1247// a comma before it.
Chris Lattnerd57ed892005-11-06 06:46:28 +00001248OptAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +00001249 ALIGN EUINT64VAL {
1250 $$ = $2;
1251 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001252 GEN_ERROR("Alignment must be a power of two");
Reid Spencer713eedc2006-08-18 08:43:06 +00001253 CHECK_FOR_ERROR
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +00001254};
Chris Lattnerc7de8362005-11-06 06:34:12 +00001255OptCAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +00001256 ',' ALIGN EUINT64VAL {
1257 $$ = $3;
1258 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001259 GEN_ERROR("Alignment must be a power of two");
Reid Spencer713eedc2006-08-18 08:43:06 +00001260 CHECK_FOR_ERROR
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +00001261};
1262
Chris Lattnerc7de8362005-11-06 06:34:12 +00001263
Chris Lattner71b936c2005-11-12 00:11:10 +00001264SectionString : SECTION STRINGCONSTANT {
1265 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1266 if ($2[i] == '"' || $2[i] == '\\')
Reid Spencerde1d05f2007-02-05 10:16:10 +00001267 GEN_ERROR("Invalid character in section name");
Chris Lattner71b936c2005-11-12 00:11:10 +00001268 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001269 CHECK_FOR_ERROR
Chris Lattner71b936c2005-11-12 00:11:10 +00001270};
1271
1272OptSection : /*empty*/ { $$ = 0; } |
1273 SectionString { $$ = $1; };
Chris Lattner71b936c2005-11-12 00:11:10 +00001274
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +00001275// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1276// is set to be the global we are processing.
1277//
1278GlobalVarAttributes : /* empty */ {} |
1279 ',' GlobalVarAttribute GlobalVarAttributes {};
1280GlobalVarAttribute : SectionString {
1281 CurGV->setSection($1);
1282 free($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001283 CHECK_FOR_ERROR
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +00001284 }
1285 | ALIGN EUINT64VAL {
1286 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001287 GEN_ERROR("Alignment must be a power of two");
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +00001288 CurGV->setAlignment($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001289 CHECK_FOR_ERROR
Chris Lattnerbce7ca8a2005-11-12 18:21:21 +00001290 };
Chris Lattner71b936c2005-11-12 00:11:10 +00001291
Chris Lattneraa534bf2001-09-07 16:35:17 +00001292//===----------------------------------------------------------------------===//
1293// Types includes all predefined types... except void, because it can only be
Reid Spencerfa35bb32006-12-31 05:40:12 +00001294// used in specific contexts (function returning void for example).
Chris Lattneraa534bf2001-09-07 16:35:17 +00001295
1296// Derived types are added later...
1297//
Reid Spencer502d64e2007-01-13 05:00:20 +00001298PrimType : INTTYPE | FLOAT | DOUBLE | LABEL ;
Reid Spencerfa35bb32006-12-31 05:40:12 +00001299
1300Types
1301 : OPAQUE {
Reid Spencer812724e2006-12-03 05:45:44 +00001302 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer713eedc2006-08-18 08:43:06 +00001303 CHECK_FOR_ERROR
Chris Lattner30752bd92002-04-04 19:23:55 +00001304 }
1305 | PrimType {
Reid Spencer812724e2006-12-03 05:45:44 +00001306 $$ = new PATypeHolder($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001307 CHECK_FOR_ERROR
Reid Spencerfa35bb32006-12-31 05:40:12 +00001308 }
1309 | Types '*' { // Pointer type?
1310 if (*$1 == Type::LabelTy)
1311 GEN_ERROR("Cannot form a pointer to a basic block");
1312 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1313 delete $1;
1314 CHECK_FOR_ERROR
1315 }
1316 | SymbolicValueRef { // Named types are also simple types...
1317 const Type* tmp = getTypeVal($1);
1318 CHECK_FOR_ERROR
1319 $$ = new PATypeHolder(tmp);
1320 }
1321 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerde1d05f2007-02-05 10:16:10 +00001322 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattneraa534bf2001-09-07 16:35:17 +00001323 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3cb41672004-02-09 03:19:29 +00001324 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencer812724e2006-12-03 05:45:44 +00001325 $$ = new PATypeHolder(OT);
Chris Lattneraa534bf2001-09-07 16:35:17 +00001326 UR_OUT("New Upreference!\n");
Reid Spencer713eedc2006-08-18 08:43:06 +00001327 CHECK_FOR_ERROR
Chris Lattneraa534bf2001-09-07 16:35:17 +00001328 }
Reid Spencer78517652007-04-28 16:06:50 +00001329 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Chris Lattner89da8a32003-04-22 18:42:41 +00001330 std::vector<const Type*> Params;
Reid Spencer78517652007-04-28 16:06:50 +00001331 ParamAttrsVector Attrs;
1332 if ($5 != ParamAttr::None) {
1333 ParamAttrsWithIndex X; X.index = 0; X.attrs = $5;
1334 Attrs.push_back(X);
1335 }
Reid Spencer71b79e32007-04-09 06:17:21 +00001336 unsigned index = 1;
1337 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
1338 for (; I != E; ++I, ++index) {
Reid Spencerd05bc522007-03-20 01:13:00 +00001339 const Type *Ty = I->Ty->get();
Reid Spencerd05bc522007-03-20 01:13:00 +00001340 Params.push_back(Ty);
Reid Spencer78517652007-04-28 16:06:50 +00001341 if (Ty != Type::VoidTy)
1342 if (I->Attrs != ParamAttr::None) {
1343 ParamAttrsWithIndex X; X.index = index; X.attrs = I->Attrs;
1344 Attrs.push_back(X);
1345 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00001346 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001347 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1348 if (isVarArg) Params.pop_back();
1349
Reid Spencer78517652007-04-28 16:06:50 +00001350 ParamAttrsList *ActualAttrs = 0;
1351 if (!Attrs.empty())
1352 ActualAttrs = ParamAttrsList::get(Attrs);
1353 FunctionType *FT = FunctionType::get(*$1, Params, isVarArg, ActualAttrs);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001354 delete $3; // Delete the argument list
Reid Spencerfa35bb32006-12-31 05:40:12 +00001355 delete $1; // Delete the return type handle
1356 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer713eedc2006-08-18 08:43:06 +00001357 CHECK_FOR_ERROR
Chris Lattneraa534bf2001-09-07 16:35:17 +00001358 }
Reid Spencer78517652007-04-28 16:06:50 +00001359 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001360 std::vector<const Type*> Params;
Reid Spencer78517652007-04-28 16:06:50 +00001361 ParamAttrsVector Attrs;
1362 if ($5 != ParamAttr::None) {
1363 ParamAttrsWithIndex X; X.index = 0; X.attrs = $5;
1364 Attrs.push_back(X);
1365 }
Reid Spencer71b79e32007-04-09 06:17:21 +00001366 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
1367 unsigned index = 1;
1368 for ( ; I != E; ++I, ++index) {
Reid Spencerd05bc522007-03-20 01:13:00 +00001369 const Type* Ty = I->Ty->get();
Reid Spencerd05bc522007-03-20 01:13:00 +00001370 Params.push_back(Ty);
Reid Spencer78517652007-04-28 16:06:50 +00001371 if (Ty != Type::VoidTy)
1372 if (I->Attrs != ParamAttr::None) {
1373 ParamAttrsWithIndex X; X.index = index; X.attrs = I->Attrs;
1374 Attrs.push_back(X);
1375 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00001376 }
1377 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1378 if (isVarArg) Params.pop_back();
1379
Reid Spencer78517652007-04-28 16:06:50 +00001380 ParamAttrsList *ActualAttrs = 0;
1381 if (!Attrs.empty())
1382 ActualAttrs = ParamAttrsList::get(Attrs);
1383
1384 FunctionType *FT = FunctionType::get($1, Params, isVarArg, ActualAttrs);
Reid Spencer136a91c2007-01-05 17:06:19 +00001385 delete $3; // Delete the argument list
Reid Spencerfa35bb32006-12-31 05:40:12 +00001386 $$ = new PATypeHolder(HandleUpRefs(FT));
1387 CHECK_FOR_ERROR
1388 }
1389
1390 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Reid Spencer812724e2006-12-03 05:45:44 +00001391 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1392 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00001393 CHECK_FOR_ERROR
Chris Lattneraa534bf2001-09-07 16:35:17 +00001394 }
Reid Spencer09575ba2007-02-15 03:39:18 +00001395 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencer812724e2006-12-03 05:45:44 +00001396 const llvm::Type* ElemTy = $4->get();
1397 if ((unsigned)$2 != $2)
1398 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner03c49532007-01-15 02:27:26 +00001399 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencerd84d35b2007-02-15 02:26:10 +00001400 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencer812724e2006-12-03 05:45:44 +00001401 if (!isPowerOf2_32($2))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001402 GEN_ERROR("Vector length should be a power of 2");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001403 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencer812724e2006-12-03 05:45:44 +00001404 delete $4;
1405 CHECK_FOR_ERROR
Brian Gaeke02209042004-08-20 06:00:58 +00001406 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001407 | '{' TypeListI '}' { // Structure type?
Chris Lattner89da8a32003-04-22 18:42:41 +00001408 std::vector<const Type*> Elements;
Reid Spencer812724e2006-12-03 05:45:44 +00001409 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattner06b6c4b2005-02-22 23:13:58 +00001410 E = $2->end(); I != E; ++I)
Reid Spencer812724e2006-12-03 05:45:44 +00001411 Elements.push_back(*I);
Misha Brukman8b477072005-05-10 22:02:28 +00001412
Reid Spencer812724e2006-12-03 05:45:44 +00001413 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001414 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001415 CHECK_FOR_ERROR
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001416 }
1417 | '{' '}' { // Empty structure type?
Reid Spencer812724e2006-12-03 05:45:44 +00001418 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer713eedc2006-08-18 08:43:06 +00001419 CHECK_FOR_ERROR
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001420 }
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001421 | '<' '{' TypeListI '}' '>' {
1422 std::vector<const Type*> Elements;
1423 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1424 E = $3->end(); I != E; ++I)
1425 Elements.push_back(*I);
1426
1427 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1428 delete $3;
1429 CHECK_FOR_ERROR
1430 }
1431 | '<' '{' '}' '>' { // Empty structure type?
1432 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1433 CHECK_FOR_ERROR
1434 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00001435 ;
1436
1437ArgType
Reid Spencer78517652007-04-28 16:06:50 +00001438 : Types OptParamAttrs {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001439 $$.Ty = $1;
Reid Spencer78517652007-04-28 16:06:50 +00001440 $$.Attrs = $2;
Reid Spencerfa35bb32006-12-31 05:40:12 +00001441 }
1442 ;
1443
Reid Spencer136a91c2007-01-05 17:06:19 +00001444ResultTypes
1445 : Types {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001446 if (!UpRefs.empty())
1447 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1448 if (!(*$1)->isFirstClassType())
Reid Spencerde1d05f2007-02-05 10:16:10 +00001449 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer136a91c2007-01-05 17:06:19 +00001450 $$ = $1;
Reid Spencerfa35bb32006-12-31 05:40:12 +00001451 }
Reid Spencer136a91c2007-01-05 17:06:19 +00001452 | VOID {
1453 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencerfa35bb32006-12-31 05:40:12 +00001454 }
1455 ;
1456
1457ArgTypeList : ArgType {
1458 $$ = new TypeWithAttrsList();
1459 $$->push_back($1);
1460 CHECK_FOR_ERROR
1461 }
1462 | ArgTypeList ',' ArgType {
1463 ($$=$1)->push_back($3);
1464 CHECK_FOR_ERROR
1465 }
1466 ;
1467
1468ArgTypeListI
1469 : ArgTypeList
1470 | ArgTypeList ',' DOTDOTDOT {
1471 $$=$1;
Reid Spencera472f662007-04-11 02:44:20 +00001472 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencerfa35bb32006-12-31 05:40:12 +00001473 TWA.Ty = new PATypeHolder(Type::VoidTy);
1474 $$->push_back(TWA);
1475 CHECK_FOR_ERROR
1476 }
1477 | DOTDOTDOT {
1478 $$ = new TypeWithAttrsList;
Reid Spencera472f662007-04-11 02:44:20 +00001479 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencerfa35bb32006-12-31 05:40:12 +00001480 TWA.Ty = new PATypeHolder(Type::VoidTy);
1481 $$->push_back(TWA);
1482 CHECK_FOR_ERROR
1483 }
1484 | /*empty*/ {
1485 $$ = new TypeWithAttrsList();
Reid Spencer713eedc2006-08-18 08:43:06 +00001486 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001487 };
Chris Lattneraa534bf2001-09-07 16:35:17 +00001488
Chris Lattner113f4f42002-06-25 16:13:24 +00001489// TypeList - Used for struct declarations and as a basis for function type
Chris Lattneraa534bf2001-09-07 16:35:17 +00001490// declaration type lists
1491//
Reid Spencerfa35bb32006-12-31 05:40:12 +00001492TypeListI : Types {
Reid Spencer812724e2006-12-03 05:45:44 +00001493 $$ = new std::list<PATypeHolder>();
Reid Spencerd05bc522007-03-20 01:13:00 +00001494 $$->push_back(*$1);
1495 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001496 CHECK_FOR_ERROR
Chris Lattneraa534bf2001-09-07 16:35:17 +00001497 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00001498 | TypeListI ',' Types {
Reid Spencerd05bc522007-03-20 01:13:00 +00001499 ($$=$1)->push_back(*$3);
1500 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001501 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001502 };
Chris Lattneraa534bf2001-09-07 16:35:17 +00001503
Chris Lattner56f73d42001-07-14 06:10:16 +00001504// ConstVal - The various declarations that go into the constant pool. This
Chris Lattner7f325cd2002-08-16 21:14:40 +00001505// production is used ONLY to represent constants that show up AFTER a 'const',
1506// 'constant' or 'global' token at global scope. Constants that can be inlined
1507// into other expressions (such as integers and constexprs) are handled by the
1508// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattner56f73d42001-07-14 06:10:16 +00001509//
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001510ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencerfa35bb32006-12-31 05:40:12 +00001511 if (!UpRefs.empty())
1512 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001513 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001514 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001515 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001516 (*$1)->getDescription() + "'");
Chris Lattneraa534bf2001-09-07 16:35:17 +00001517 const Type *ETy = ATy->getElementType();
1518 int NumElements = ATy->getNumElements();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001519
Chris Lattneraa534bf2001-09-07 16:35:17 +00001520 // Verify that we have the correct size...
1521 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer713eedc2006-08-18 08:43:06 +00001522 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Reid Spencerce6adaf2004-07-18 00:08:11 +00001523 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001524 itostr(NumElements) + "");
Chris Lattner2f7c9632001-06-06 20:29:01 +00001525
Chris Lattneraa534bf2001-09-07 16:35:17 +00001526 // Verify all elements are correct type!
1527 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer812724e2006-12-03 05:45:44 +00001528 if (ETy != (*$3)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001529 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Reid Spencerce6adaf2004-07-18 00:08:11 +00001530 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer812724e2006-12-03 05:45:44 +00001531 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner2f7c9632001-06-06 20:29:01 +00001532 }
1533
Reid Spencer812724e2006-12-03 05:45:44 +00001534 $$ = ConstantArray::get(ATy, *$3);
1535 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001536 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00001537 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001538 | Types '[' ']' {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001539 if (!UpRefs.empty())
1540 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001541 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001542 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001543 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001544 (*$1)->getDescription() + "'");
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001545
1546 int NumElements = ATy->getNumElements();
Chris Lattneraa534bf2001-09-07 16:35:17 +00001547 if (NumElements != -1 && NumElements != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001548 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencerde1d05f2007-02-05 10:16:10 +00001549 " arguments, but has size of " + itostr(NumElements) +"");
Reid Spencer812724e2006-12-03 05:45:44 +00001550 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1551 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001552 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00001553 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001554 | Types 'c' STRINGCONSTANT {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001555 if (!UpRefs.empty())
1556 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001557 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001558 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001559 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001560 (*$1)->getDescription() + "'");
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001561
Chris Lattneraa534bf2001-09-07 16:35:17 +00001562 int NumElements = ATy->getNumElements();
1563 const Type *ETy = ATy->getElementType();
1564 char *EndStr = UnEscapeLexed($3, true);
1565 if (NumElements != -1 && NumElements != (EndStr-$3))
Reid Spencer713eedc2006-08-18 08:43:06 +00001566 GEN_ERROR("Can't build string constant of size " +
Reid Spencerce6adaf2004-07-18 00:08:11 +00001567 itostr((int)(EndStr-$3)) +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001568 " when array has size " + itostr(NumElements) + "");
Chris Lattner89da8a32003-04-22 18:42:41 +00001569 std::vector<Constant*> Vals;
Reid Spencerfa35bb32006-12-31 05:40:12 +00001570 if (ETy == Type::Int8Ty) {
Chris Lattnerf09741f2006-01-23 23:05:15 +00001571 for (unsigned char *C = (unsigned char *)$3;
Reid Spencerfa35bb32006-12-31 05:40:12 +00001572 C != (unsigned char*)EndStr; ++C)
1573 Vals.push_back(ConstantInt::get(ETy, *C));
Chris Lattner26e50dc2001-07-28 17:48:55 +00001574 } else {
Chris Lattneraa534bf2001-09-07 16:35:17 +00001575 free($3);
Reid Spencerde1d05f2007-02-05 10:16:10 +00001576 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner26e50dc2001-07-28 17:48:55 +00001577 }
Chris Lattneraa534bf2001-09-07 16:35:17 +00001578 free($3);
Reid Spencer812724e2006-12-03 05:45:44 +00001579 $$ = ConstantArray::get(ATy, Vals);
1580 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001581 CHECK_FOR_ERROR
Chris Lattner26e50dc2001-07-28 17:48:55 +00001582 }
Brian Gaeke02209042004-08-20 06:00:58 +00001583 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencerfa35bb32006-12-31 05:40:12 +00001584 if (!UpRefs.empty())
1585 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001586 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Brian Gaeke02209042004-08-20 06:00:58 +00001587 if (PTy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001588 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001589 (*$1)->getDescription() + "'");
Brian Gaeke02209042004-08-20 06:00:58 +00001590 const Type *ETy = PTy->getElementType();
1591 int NumElements = PTy->getNumElements();
1592
1593 // Verify that we have the correct size...
1594 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer713eedc2006-08-18 08:43:06 +00001595 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Brian Gaeke02209042004-08-20 06:00:58 +00001596 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001597 itostr(NumElements) + "");
Brian Gaeke02209042004-08-20 06:00:58 +00001598
1599 // Verify all elements are correct type!
1600 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer812724e2006-12-03 05:45:44 +00001601 if (ETy != (*$3)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001602 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke02209042004-08-20 06:00:58 +00001603 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer812724e2006-12-03 05:45:44 +00001604 (*$3)[i]->getType()->getDescription() + "'.");
Brian Gaeke02209042004-08-20 06:00:58 +00001605 }
1606
Reid Spencerd84d35b2007-02-15 02:26:10 +00001607 $$ = ConstantVector::get(PTy, *$3);
Reid Spencer812724e2006-12-03 05:45:44 +00001608 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001609 CHECK_FOR_ERROR
Brian Gaeke02209042004-08-20 06:00:58 +00001610 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001611 | Types '{' ConstVector '}' {
Reid Spencer812724e2006-12-03 05:45:44 +00001612 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001613 if (STy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001614 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001615 (*$1)->getDescription() + "'");
Chris Lattner4b1d1062003-04-15 16:09:31 +00001616
Chris Lattner5bb23152003-05-21 16:06:56 +00001617 if ($3->size() != STy->getNumContainedTypes())
Reid Spencerde1d05f2007-02-05 10:16:10 +00001618 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner5bb23152003-05-21 16:06:56 +00001619
Chris Lattner4b1d1062003-04-15 16:09:31 +00001620 // Check to ensure that constants are compatible with the type initializer!
1621 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencer812724e2006-12-03 05:45:44 +00001622 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer713eedc2006-08-18 08:43:06 +00001623 GEN_ERROR("Expected type '" +
Chris Lattnerac6db752004-02-09 04:37:31 +00001624 STy->getElementType(i)->getDescription() +
Chris Lattner4b1d1062003-04-15 16:09:31 +00001625 "' for element #" + utostr(i) +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001626 " of structure initializer");
Chris Lattner4b1d1062003-04-15 16:09:31 +00001627
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001628 // Check to ensure that Type is not packed
1629 if (STy->isPacked())
Chris Lattnereee35bf2007-04-26 05:30:35 +00001630 GEN_ERROR("Unpacked Initializer to vector type '" +
1631 STy->getDescription() + "'");
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001632
Reid Spencer812724e2006-12-03 05:45:44 +00001633 $$ = ConstantStruct::get(STy, *$3);
1634 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001635 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00001636 }
Chris Lattner5bb23152003-05-21 16:06:56 +00001637 | Types '{' '}' {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001638 if (!UpRefs.empty())
1639 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001640 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner5bb23152003-05-21 16:06:56 +00001641 if (STy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001642 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001643 (*$1)->getDescription() + "'");
Chris Lattner5bb23152003-05-21 16:06:56 +00001644
1645 if (STy->getNumContainedTypes() != 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00001646 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner5bb23152003-05-21 16:06:56 +00001647
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001648 // Check to ensure that Type is not packed
1649 if (STy->isPacked())
Chris Lattnereee35bf2007-04-26 05:30:35 +00001650 GEN_ERROR("Unpacked Initializer to vector type '" +
1651 STy->getDescription() + "'");
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001652
1653 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1654 delete $1;
1655 CHECK_FOR_ERROR
1656 }
1657 | Types '<' '{' ConstVector '}' '>' {
1658 const StructType *STy = dyn_cast<StructType>($1->get());
1659 if (STy == 0)
1660 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001661 (*$1)->getDescription() + "'");
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001662
1663 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerde1d05f2007-02-05 10:16:10 +00001664 GEN_ERROR("Illegal number of initializers for structure type");
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001665
1666 // Check to ensure that constants are compatible with the type initializer!
1667 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1668 if ((*$4)[i]->getType() != STy->getElementType(i))
1669 GEN_ERROR("Expected type '" +
1670 STy->getElementType(i)->getDescription() +
1671 "' for element #" + utostr(i) +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001672 " of structure initializer");
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001673
1674 // Check to ensure that Type is packed
1675 if (!STy->isPacked())
Reid Spencer09575ba2007-02-15 03:39:18 +00001676 GEN_ERROR("Vector initializer to non-vector type '" +
1677 STy->getDescription() + "'");
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001678
1679 $$ = ConstantStruct::get(STy, *$4);
1680 delete $1; delete $4;
1681 CHECK_FOR_ERROR
1682 }
1683 | Types '<' '{' '}' '>' {
1684 if (!UpRefs.empty())
1685 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1686 const StructType *STy = dyn_cast<StructType>($1->get());
1687 if (STy == 0)
1688 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001689 (*$1)->getDescription() + "'");
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001690
1691 if (STy->getNumContainedTypes() != 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00001692 GEN_ERROR("Illegal number of initializers for structure type");
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001693
1694 // Check to ensure that Type is packed
1695 if (!STy->isPacked())
Reid Spencer09575ba2007-02-15 03:39:18 +00001696 GEN_ERROR("Vector initializer to non-vector type '" +
1697 STy->getDescription() + "'");
Andrew Lenharthe3c5a4c2007-01-08 18:16:47 +00001698
Reid Spencer812724e2006-12-03 05:45:44 +00001699 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1700 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001701 CHECK_FOR_ERROR
Chris Lattner5bb23152003-05-21 16:06:56 +00001702 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001703 | Types NULL_TOK {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001704 if (!UpRefs.empty())
1705 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001706 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001707 if (PTy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001708 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001709 (*$1)->getDescription() + "'");
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001710
Reid Spencer812724e2006-12-03 05:45:44 +00001711 $$ = ConstantPointerNull::get(PTy);
1712 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001713 CHECK_FOR_ERROR
Chris Lattner571a6b02001-10-03 01:49:25 +00001714 }
Chris Lattner4ff31492004-10-16 18:17:13 +00001715 | Types UNDEF {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001716 if (!UpRefs.empty())
1717 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001718 $$ = UndefValue::get($1->get());
1719 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001720 CHECK_FOR_ERROR
Chris Lattner4ff31492004-10-16 18:17:13 +00001721 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001722 | Types SymbolicValueRef {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001723 if (!UpRefs.empty())
1724 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001725 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattner60e0dd72001-10-03 06:12:09 +00001726 if (Ty == 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00001727 GEN_ERROR("Global const reference must be a pointer type");
Chris Lattner60e0dd72001-10-03 06:12:09 +00001728
Chris Lattner61643a02002-08-15 17:58:33 +00001729 // ConstExprs can exist in the body of a function, thus creating
Reid Spencerce6adaf2004-07-18 00:08:11 +00001730 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencerfe65ae82007-03-19 18:39:36 +00001731 // the context of a function, getExistingVal will search the functions
Chris Lattner61643a02002-08-15 17:58:33 +00001732 // symbol table instead of the module symbol table for the global symbol,
1733 // which throws things all off. To get around this, we just tell
Reid Spencerfe65ae82007-03-19 18:39:36 +00001734 // getExistingVal that we are at global scope here.
Chris Lattner61643a02002-08-15 17:58:33 +00001735 //
Chris Lattner61877742003-10-16 20:12:13 +00001736 Function *SavedCurFn = CurFun.CurrentFunction;
1737 CurFun.CurrentFunction = 0;
Chris Lattner61643a02002-08-15 17:58:33 +00001738
Reid Spencerfe65ae82007-03-19 18:39:36 +00001739 Value *V = getExistingVal(Ty, $2);
Reid Spencer309080a2006-09-28 19:28:24 +00001740 CHECK_FOR_ERROR
Chris Lattner60e0dd72001-10-03 06:12:09 +00001741
Chris Lattner61877742003-10-16 20:12:13 +00001742 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner61643a02002-08-15 17:58:33 +00001743
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001744 // If this is an initializer for a constant pointer, which is referencing a
1745 // (currently) undefined variable, create a stub now that shall be replaced
1746 // in the future with the right type of variable.
1747 //
1748 if (V == 0) {
Reid Spencer9894d6c2007-02-05 17:01:20 +00001749 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001750 const PointerType *PT = cast<PointerType>(Ty);
1751
1752 // First check to see if the forward references value is already created!
1753 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencerce6adaf2004-07-18 00:08:11 +00001754 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001755
1756 if (I != CurModule.GlobalRefs.end()) {
Reid Spencerce6adaf2004-07-18 00:08:11 +00001757 V = I->second; // Placeholder already exists, use it...
Chris Lattnere9703012003-12-23 20:05:15 +00001758 $2.destroy();
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001759 } else {
Chris Lattner4c9210e2004-07-14 21:44:00 +00001760 std::string Name;
Reid Spencer791b8ef2007-01-26 08:04:51 +00001761 if ($2.Type == ValID::GlobalName)
1762 Name = $2.Name;
1763 else if ($2.Type != ValID::GlobalID)
1764 GEN_ERROR("Invalid reference to global");
Chris Lattner4c9210e2004-07-14 21:44:00 +00001765
Reid Spencerce6adaf2004-07-18 00:08:11 +00001766 // Create the forward referenced global.
Chris Lattner8abe1a12004-07-14 23:03:46 +00001767 GlobalValue *GV;
1768 if (const FunctionType *FTy =
1769 dyn_cast<FunctionType>(PT->getElementType())) {
Chris Lattnereee35bf2007-04-26 05:30:35 +00001770 GV = new Function(FTy, GlobalValue::ExternalWeakLinkage, Name,
Chris Lattner8abe1a12004-07-14 23:03:46 +00001771 CurModule.CurrentModule);
1772 } else {
Reid Spencerce6adaf2004-07-18 00:08:11 +00001773 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattnereee35bf2007-04-26 05:30:35 +00001774 GlobalValue::ExternalWeakLinkage, 0,
Chris Lattner8abe1a12004-07-14 23:03:46 +00001775 Name, CurModule.CurrentModule);
1776 }
Chris Lattner4c9210e2004-07-14 21:44:00 +00001777
Reid Spencerce6adaf2004-07-18 00:08:11 +00001778 // Keep track of the fact that we have a forward ref to recycle it
1779 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1780 V = GV;
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001781 }
Chris Lattner60e0dd72001-10-03 06:12:09 +00001782 }
1783
Reid Spencer812724e2006-12-03 05:45:44 +00001784 $$ = cast<GlobalValue>(V);
1785 delete $1; // Free the type handle
Reid Spencer713eedc2006-08-18 08:43:06 +00001786 CHECK_FOR_ERROR
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001787 }
Chris Lattner7f325cd2002-08-16 21:14:40 +00001788 | Types ConstExpr {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001789 if (!UpRefs.empty())
1790 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001791 if ($1->get() != $2->getType())
Reid Spencer4eda71e2007-01-04 00:05:48 +00001792 GEN_ERROR("Mismatched types for constant expression: " +
1793 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattner7f325cd2002-08-16 21:14:40 +00001794 $$ = $2;
Reid Spencer812724e2006-12-03 05:45:44 +00001795 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001796 CHECK_FOR_ERROR
Chris Lattner79694012003-06-28 20:01:34 +00001797 }
1798 | Types ZEROINITIALIZER {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001799 if (!UpRefs.empty())
1800 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001801 const Type *Ty = $1->get();
Chris Lattneraef74b52004-11-28 16:45:45 +00001802 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001803 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencer812724e2006-12-03 05:45:44 +00001804 $$ = Constant::getNullValue(Ty);
1805 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001806 CHECK_FOR_ERROR
Reid Spencer812724e2006-12-03 05:45:44 +00001807 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00001808 | IntType ESINT64VAL { // integral constants
Reid Spencer3f46fba2006-12-20 17:20:09 +00001809 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001810 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencer363fd462007-03-19 20:40:22 +00001811 $$ = ConstantInt::get($1, $2, true);
Reid Spencer9875fc12007-02-28 02:23:44 +00001812 CHECK_FOR_ERROR
1813 }
1814 | IntType ESAPINTVAL { // arbitrary precision integer constants
1815 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1816 if ($2->getBitWidth() > BitWidth) {
1817 GEN_ERROR("Constant value does not fit in type");
Reid Spencer666ea0d2007-03-01 19:32:01 +00001818 }
1819 $2->sextOrTrunc(BitWidth);
1820 $$ = ConstantInt::get(*$2);
Reid Spencer9875fc12007-02-28 02:23:44 +00001821 delete $2;
Reid Spencer3f46fba2006-12-20 17:20:09 +00001822 CHECK_FOR_ERROR
1823 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00001824 | IntType EUINT64VAL { // integral constants
Reid Spencer3f46fba2006-12-20 17:20:09 +00001825 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001826 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencer363fd462007-03-19 20:40:22 +00001827 $$ = ConstantInt::get($1, $2, false);
Reid Spencer9875fc12007-02-28 02:23:44 +00001828 CHECK_FOR_ERROR
1829 }
1830 | IntType EUAPINTVAL { // arbitrary precision integer constants
1831 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1832 if ($2->getBitWidth() > BitWidth) {
1833 GEN_ERROR("Constant value does not fit in type");
Reid Spencer666ea0d2007-03-01 19:32:01 +00001834 }
1835 $2->zextOrTrunc(BitWidth);
1836 $$ = ConstantInt::get(*$2);
Reid Spencer9875fc12007-02-28 02:23:44 +00001837 delete $2;
Reid Spencer3f46fba2006-12-20 17:20:09 +00001838 CHECK_FOR_ERROR
1839 }
Reid Spencer502d64e2007-01-13 05:00:20 +00001840 | INTTYPE TRUETOK { // Boolean constants
1841 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001842 $$ = ConstantInt::getTrue();
Reid Spencer713eedc2006-08-18 08:43:06 +00001843 CHECK_FOR_ERROR
Chris Lattner7f1c98d2002-08-15 18:17:28 +00001844 }
Reid Spencer502d64e2007-01-13 05:00:20 +00001845 | INTTYPE FALSETOK { // Boolean constants
1846 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001847 $$ = ConstantInt::getFalse();
Reid Spencer713eedc2006-08-18 08:43:06 +00001848 CHECK_FOR_ERROR
Chris Lattner7f1c98d2002-08-15 18:17:28 +00001849 }
1850 | FPType FPVAL { // Float & Double constants
Reid Spencer812724e2006-12-03 05:45:44 +00001851 if (!ConstantFP::isValueValidForType($1, $2))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001852 GEN_ERROR("Floating point constant invalid for type");
Reid Spencer812724e2006-12-03 05:45:44 +00001853 $$ = ConstantFP::get($1, $2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001854 CHECK_FOR_ERROR
Chris Lattner7f1c98d2002-08-15 18:17:28 +00001855 };
1856
Chris Lattner2f7c9632001-06-06 20:29:01 +00001857
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001858ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencerfa35bb32006-12-31 05:40:12 +00001859 if (!UpRefs.empty())
1860 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00001861 Constant *Val = $3;
Reid Spencer082a77f2007-01-17 02:47:33 +00001862 const Type *DestTy = $5->get();
1863 if (!CastInst::castIsValid($1, $3, DestTy))
1864 GEN_ERROR("invalid cast opcode for cast from '" +
1865 Val->getType()->getDescription() + "' to '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00001866 DestTy->getDescription() + "'");
Reid Spencer082a77f2007-01-17 02:47:33 +00001867 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencer812724e2006-12-03 05:45:44 +00001868 delete $5;
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001869 }
Chris Lattner7f325cd2002-08-16 21:14:40 +00001870 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencer812724e2006-12-03 05:45:44 +00001871 if (!isa<PointerType>($3->getType()))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001872 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattner3cd8c562002-07-30 18:54:25 +00001873
Reid Spencer812724e2006-12-03 05:45:44 +00001874 const Type *IdxTy =
Chris Lattner04a2d762007-02-13 00:57:40 +00001875 GetElementPtrInst::getIndexedType($3->getType(), &(*$4)[0], $4->size(),
1876 true);
Reid Spencer812724e2006-12-03 05:45:44 +00001877 if (!IdxTy)
Reid Spencerde1d05f2007-02-05 10:16:10 +00001878 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencer812724e2006-12-03 05:45:44 +00001879
Chris Lattner10aad382007-01-31 04:43:46 +00001880 SmallVector<Constant*, 8> IdxVec;
Reid Spencer812724e2006-12-03 05:45:44 +00001881 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1882 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattner980ddf52002-07-18 00:14:27 +00001883 IdxVec.push_back(C);
1884 else
Reid Spencerde1d05f2007-02-05 10:16:10 +00001885 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattner980ddf52002-07-18 00:14:27 +00001886
Chris Lattner7f325cd2002-08-16 21:14:40 +00001887 delete $4;
Chris Lattner980ddf52002-07-18 00:14:27 +00001888
Chris Lattner10aad382007-01-31 04:43:46 +00001889 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer713eedc2006-08-18 08:43:06 +00001890 CHECK_FOR_ERROR
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001891 }
Chris Lattner6536f0c2004-03-12 05:51:36 +00001892 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer542964f2007-01-11 18:21:29 +00001893 if ($3->getType() != Type::Int1Ty)
Reid Spencerde1d05f2007-02-05 10:16:10 +00001894 GEN_ERROR("Select condition must be of boolean type");
Reid Spencer812724e2006-12-03 05:45:44 +00001895 if ($5->getType() != $7->getType())
Reid Spencerde1d05f2007-02-05 10:16:10 +00001896 GEN_ERROR("Select operand types must match");
Reid Spencer812724e2006-12-03 05:45:44 +00001897 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001898 CHECK_FOR_ERROR
Chris Lattner6536f0c2004-03-12 05:51:36 +00001899 }
Chris Lattnerfca28332004-08-17 17:26:41 +00001900 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer812724e2006-12-03 05:45:44 +00001901 if ($3->getType() != $5->getType())
Reid Spencerde1d05f2007-02-05 10:16:10 +00001902 GEN_ERROR("Binary operator types must match");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001903 CHECK_FOR_ERROR;
Reid Spencer5bcee9a2006-12-05 19:15:41 +00001904 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattnerfca28332004-08-17 17:26:41 +00001905 }
1906 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer812724e2006-12-03 05:45:44 +00001907 if ($3->getType() != $5->getType())
Reid Spencerde1d05f2007-02-05 10:16:10 +00001908 GEN_ERROR("Logical operator types must match");
Chris Lattner03c49532007-01-15 02:27:26 +00001909 if (!$3->getType()->isInteger()) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001910 if (Instruction::isShift($1) || !isa<VectorType>($3->getType()) ||
1911 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencerde1d05f2007-02-05 10:16:10 +00001912 GEN_ERROR("Logical operator requires integral operands");
Chris Lattnera5d70e92005-12-21 18:31:29 +00001913 }
Reid Spencer812724e2006-12-03 05:45:44 +00001914 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001915 CHECK_FOR_ERROR
Chris Lattnerfca28332004-08-17 17:26:41 +00001916 }
Reid Spencerf3490692006-12-04 05:20:06 +00001917 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1918 if ($4->getType() != $6->getType())
Reid Spencerde1d05f2007-02-05 10:16:10 +00001919 GEN_ERROR("icmp operand types must match");
Reid Spencerf3490692006-12-04 05:20:06 +00001920 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencer812724e2006-12-03 05:45:44 +00001921 }
Reid Spencerf3490692006-12-04 05:20:06 +00001922 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1923 if ($4->getType() != $6->getType())
Reid Spencerde1d05f2007-02-05 10:16:10 +00001924 GEN_ERROR("fcmp operand types must match");
Reid Spencerf3490692006-12-04 05:20:06 +00001925 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencer812724e2006-12-03 05:45:44 +00001926 }
Robert Bocchinoaa1cf542006-01-10 19:04:32 +00001927 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencer812724e2006-12-03 05:45:44 +00001928 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001929 GEN_ERROR("Invalid extractelement operands");
Reid Spencer812724e2006-12-03 05:45:44 +00001930 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001931 CHECK_FOR_ERROR
Chris Lattner8c32ad02006-04-08 03:53:34 +00001932 }
1933 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer812724e2006-12-03 05:45:44 +00001934 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001935 GEN_ERROR("Invalid insertelement operands");
Reid Spencer812724e2006-12-03 05:45:44 +00001936 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001937 CHECK_FOR_ERROR
Chris Lattner8c32ad02006-04-08 03:53:34 +00001938 }
1939 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer812724e2006-12-03 05:45:44 +00001940 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerde1d05f2007-02-05 10:16:10 +00001941 GEN_ERROR("Invalid shufflevector operands");
Reid Spencer812724e2006-12-03 05:45:44 +00001942 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001943 CHECK_FOR_ERROR
Chris Lattnerdb3b2022002-08-14 17:12:33 +00001944 };
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001945
Chris Lattner8c32ad02006-04-08 03:53:34 +00001946
Misha Brukman7fdaab42003-07-14 17:20:40 +00001947// ConstVector - A list of comma separated constants.
Chris Lattner2f7c9632001-06-06 20:29:01 +00001948ConstVector : ConstVector ',' ConstVal {
Chris Lattneraa534bf2001-09-07 16:35:17 +00001949 ($$ = $1)->push_back($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001950 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00001951 }
1952 | ConstVal {
Reid Spencer812724e2006-12-03 05:45:44 +00001953 $$ = new std::vector<Constant*>();
Chris Lattneraa534bf2001-09-07 16:35:17 +00001954 $$->push_back($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001955 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001956 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001957
Chris Lattner252afba2001-07-26 16:29:15 +00001958
Chris Lattner7fb14f52001-09-18 04:00:54 +00001959// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001960GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner7fb14f52001-09-18 04:00:54 +00001961
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00001962// ThreadLocal
1963ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
1964
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +00001965// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
1966AliaseeRef : ResultTypes SymbolicValueRef {
1967 const Type* VTy = $1->get();
1968 Value *V = getVal(VTy, $2);
1969 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
1970 if (!Aliasee)
1971 GEN_ERROR("Aliases can be created only to global values");
1972
1973 $$ = Aliasee;
1974 CHECK_FOR_ERROR
1975 delete $1;
1976 }
1977 | BITCAST '(' AliaseeRef TO Types ')' {
1978 Constant *Val = $3;
1979 const Type *DestTy = $5->get();
1980 if (!CastInst::castIsValid($1, $3, DestTy))
1981 GEN_ERROR("invalid cast opcode for cast from '" +
1982 Val->getType()->getDescription() + "' to '" +
1983 DestTy->getDescription() + "'");
1984
1985 $$ = ConstantExpr::getCast($1, $3, DestTy);
1986 CHECK_FOR_ERROR
1987 delete $5;
1988 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001989
Chris Lattner7b804d62002-05-02 19:11:13 +00001990//===----------------------------------------------------------------------===//
1991// Rules to match Modules
1992//===----------------------------------------------------------------------===//
1993
1994// Module rule: Capture the result of parsing the whole file into a result
1995// variable...
1996//
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001997Module
1998 : DefinitionList {
1999 $$ = ParserResult = CurModule.CurrentModule;
2000 CurModule.ModuleDone();
2001 CHECK_FOR_ERROR;
2002 }
2003 | /*empty*/ {
2004 $$ = ParserResult = CurModule.CurrentModule;
2005 CurModule.ModuleDone();
2006 CHECK_FOR_ERROR;
2007 }
2008 ;
Chris Lattner7b804d62002-05-02 19:11:13 +00002009
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002010DefinitionList
2011 : Definition
2012 | DefinitionList Definition
2013 ;
2014
2015Definition
Jeff Cohen5d956e42007-01-21 19:19:31 +00002016 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner61877742003-10-16 20:12:13 +00002017 CurFun.FunctionDone();
Reid Spencer713eedc2006-08-18 08:43:06 +00002018 CHECK_FOR_ERROR
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002019 }
2020 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer713eedc2006-08-18 08:43:06 +00002021 CHECK_FOR_ERROR
Chris Lattner7b804d62002-05-02 19:11:13 +00002022 }
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002023 | MODULE ASM_TOK AsmBlock {
Reid Spencer713eedc2006-08-18 08:43:06 +00002024 CHECK_FOR_ERROR
Chris Lattnerf09741f2006-01-23 23:05:15 +00002025 }
Reid Spencer791b8ef2007-01-26 08:04:51 +00002026 | OptLocalAssign TYPE Types {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002027 if (!UpRefs.empty())
2028 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattnerd50fa5a2001-10-22 05:56:09 +00002029 // Eagerly resolve types. This is not an optimization, this is a
2030 // requirement that is due to the fact that we could have this:
2031 //
2032 // %list = type { %list * }
2033 // %list = type { %list * } ; repeated type decl
2034 //
2035 // If types are not resolved eagerly, then the two types will not be
2036 // determined to be the same type!
2037 //
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002038 ResolveTypeTo($1, *$3);
Chris Lattnerd50fa5a2001-10-22 05:56:09 +00002039
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002040 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer309080a2006-09-28 19:28:24 +00002041 CHECK_FOR_ERROR
Chris Lattner85a351f2004-07-13 08:10:10 +00002042 // If this is a named type that is not a redefinition, add it to the slot
2043 // table.
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002044 CurModule.Types.push_back(*$3);
Chris Lattneraa534bf2001-09-07 16:35:17 +00002045 }
Reid Spencer812724e2006-12-03 05:45:44 +00002046
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002047 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00002048 CHECK_FOR_ERROR
Chris Lattneraa534bf2001-09-07 16:35:17 +00002049 }
Reid Spencer791b8ef2007-01-26 08:04:51 +00002050 | OptLocalAssign TYPE VOID {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002051 ResolveTypeTo($1, $3);
2052
2053 if (!setTypeName($3, $1) && !$1) {
2054 CHECK_FOR_ERROR
2055 // If this is a named type that is not a redefinition, add it to the slot
2056 // table.
2057 CurModule.Types.push_back($3);
2058 }
2059 CHECK_FOR_ERROR
2060 }
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002061 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal {
Reid Spencer791b8ef2007-01-26 08:04:51 +00002062 /* "Externally Visible" Linkage */
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002063 if ($5 == 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002064 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002065 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
2066 $2, $4, $5->getType(), $5, $3);
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002067 CHECK_FOR_ERROR
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002068 } GlobalVarAttributes {
2069 CurGV = 0;
2070 }
Chris Lattnereee35bf2007-04-26 05:30:35 +00002071 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
2072 ConstVal {
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002073 if ($6 == 0)
2074 GEN_ERROR("Global value initializer is not a constant");
2075 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002076 CHECK_FOR_ERROR
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002077 } GlobalVarAttributes {
2078 CurGV = 0;
2079 }
Chris Lattnereee35bf2007-04-26 05:30:35 +00002080 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
2081 Types {
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002082 if (!UpRefs.empty())
2083 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
2084 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4);
2085 CHECK_FOR_ERROR
2086 delete $6;
Reid Spencer309080a2006-09-28 19:28:24 +00002087 } GlobalVarAttributes {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002088 CurGV = 0;
2089 CHECK_FOR_ERROR
2090 }
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +00002091 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
Anton Korobeynikova97b6942007-04-25 14:27:10 +00002092 std::string Name($1);
2093 if (Name.empty())
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +00002094 GEN_ERROR("Alias name cannot be empty");
2095
2096 Constant* Aliasee = $5;
2097 if (Aliasee == 0)
Anton Korobeynikova97b6942007-04-25 14:27:10 +00002098 GEN_ERROR(std::string("Invalid aliasee for alias: ") + $1);
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +00002099
2100 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2101 CurModule.CurrentModule);
2102 GA->setVisibility($2);
2103 InsertValue(GA, CurModule.Values);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00002104 CHECK_FOR_ERROR
Anton Korobeynikova97b6942007-04-25 14:27:10 +00002105 }
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002106 | TARGET TargetDefinition {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002107 CHECK_FOR_ERROR
2108 }
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002109 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer713eedc2006-08-18 08:43:06 +00002110 CHECK_FOR_ERROR
Chris Lattner56f73d42001-07-14 06:10:16 +00002111 }
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002112 ;
Chris Lattner2f7c9632001-06-06 20:29:01 +00002113
2114
Chris Lattnerf09741f2006-01-23 23:05:15 +00002115AsmBlock : STRINGCONSTANT {
Chris Lattner8ebd2162006-01-24 04:14:29 +00002116 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattnerf09741f2006-01-23 23:05:15 +00002117 char *EndStr = UnEscapeLexed($1, true);
2118 std::string NewAsm($1, EndStr);
2119 free($1);
2120
2121 if (AsmSoFar.empty())
Chris Lattner8ebd2162006-01-24 04:14:29 +00002122 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
Chris Lattnerf09741f2006-01-23 23:05:15 +00002123 else
Chris Lattner8ebd2162006-01-24 04:14:29 +00002124 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
Reid Spencer713eedc2006-08-18 08:43:06 +00002125 CHECK_FOR_ERROR
Chris Lattnerf09741f2006-01-23 23:05:15 +00002126};
Chris Lattner20126132003-04-22 19:07:06 +00002127
Reid Spencer791b8ef2007-01-26 08:04:51 +00002128TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Reid Spencer62c6da92004-07-25 21:30:51 +00002129 CurModule.CurrentModule->setTargetTriple($3);
2130 free($3);
John Criswell6af0b122006-10-24 19:09:48 +00002131 }
Chris Lattnerfd3d29a2006-10-22 06:07:41 +00002132 | DATALAYOUT '=' STRINGCONSTANT {
Owen Andersone2237542006-10-18 02:21:12 +00002133 CurModule.CurrentModule->setDataLayout($3);
2134 free($3);
Owen Andersone2237542006-10-18 02:21:12 +00002135 };
Chris Lattner20126132003-04-22 19:07:06 +00002136
Reid Spencer62c6da92004-07-25 21:30:51 +00002137LibrariesDefinition : '[' LibList ']';
Reid Spencera24de0d2004-07-25 17:58:28 +00002138
2139LibList : LibList ',' STRINGCONSTANT {
Reid Spencer62c6da92004-07-25 21:30:51 +00002140 CurModule.CurrentModule->addLibrary($3);
2141 free($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002142 CHECK_FOR_ERROR
Reid Spencera24de0d2004-07-25 17:58:28 +00002143 }
2144 | STRINGCONSTANT {
Reid Spencer62c6da92004-07-25 21:30:51 +00002145 CurModule.CurrentModule->addLibrary($1);
2146 free($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002147 CHECK_FOR_ERROR
Reid Spencera24de0d2004-07-25 17:58:28 +00002148 }
2149 | /* empty: end of list */ {
Reid Spencer713eedc2006-08-18 08:43:06 +00002150 CHECK_FOR_ERROR
Reid Spencera24de0d2004-07-25 17:58:28 +00002151 }
2152 ;
Chris Lattner20126132003-04-22 19:07:06 +00002153
Chris Lattner2f7c9632001-06-06 20:29:01 +00002154//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +00002155// Rules to match Function Headers
Chris Lattner2f7c9632001-06-06 20:29:01 +00002156//===----------------------------------------------------------------------===//
2157
Reid Spencer791b8ef2007-01-26 08:04:51 +00002158ArgListH : ArgListH ',' Types OptParamAttrs OptLocalName {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002159 if (!UpRefs.empty())
2160 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
2161 if (*$3 == Type::VoidTy)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002162 GEN_ERROR("void typed arguments are invalid");
Reid Spencerfa35bb32006-12-31 05:40:12 +00002163 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner149376d2002-10-13 20:57:00 +00002164 $$ = $1;
Reid Spencerfa35bb32006-12-31 05:40:12 +00002165 $1->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002166 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002167 }
Reid Spencer791b8ef2007-01-26 08:04:51 +00002168 | Types OptParamAttrs OptLocalName {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002169 if (!UpRefs.empty())
2170 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2171 if (*$1 == Type::VoidTy)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002172 GEN_ERROR("void typed arguments are invalid");
Reid Spencerfa35bb32006-12-31 05:40:12 +00002173 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2174 $$ = new ArgListType;
2175 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002176 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002177 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00002178
2179ArgList : ArgListH {
2180 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002181 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002182 }
Chris Lattner149376d2002-10-13 20:57:00 +00002183 | ArgListH ',' DOTDOTDOT {
2184 $$ = $1;
Reid Spencerfa35bb32006-12-31 05:40:12 +00002185 struct ArgListEntry E;
2186 E.Ty = new PATypeHolder(Type::VoidTy);
2187 E.Name = 0;
Reid Spencera472f662007-04-11 02:44:20 +00002188 E.Attrs = ParamAttr::None;
Reid Spencerfa35bb32006-12-31 05:40:12 +00002189 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002190 CHECK_FOR_ERROR
Chris Lattner149376d2002-10-13 20:57:00 +00002191 }
2192 | DOTDOTDOT {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002193 $$ = new ArgListType;
2194 struct ArgListEntry E;
2195 E.Ty = new PATypeHolder(Type::VoidTy);
2196 E.Name = 0;
Reid Spencera472f662007-04-11 02:44:20 +00002197 E.Attrs = ParamAttr::None;
Reid Spencerfa35bb32006-12-31 05:40:12 +00002198 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002199 CHECK_FOR_ERROR
Chris Lattner149376d2002-10-13 20:57:00 +00002200 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002201 | /* empty */ {
2202 $$ = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00002203 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002204 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00002205
Reid Spencer791b8ef2007-01-26 08:04:51 +00002206FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
Reid Spencer136a91c2007-01-05 17:06:19 +00002207 OptFuncAttrs OptSection OptAlign {
Chris Lattner53bdd312005-05-06 20:27:19 +00002208 UnEscapeLexed($3);
2209 std::string FunctionName($3);
2210 free($3); // Free strdup'd memory!
Chris Lattner841d8b92001-11-26 18:54:16 +00002211
Reid Spencer6130ae02007-01-02 21:53:43 +00002212 // Check the function result for abstractness if this is a define. We should
2213 // have no abstract types at this point
Reid Spencer136a91c2007-01-05 17:06:19 +00002214 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2215 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer6130ae02007-01-02 21:53:43 +00002216
Chris Lattner89da8a32003-04-22 18:42:41 +00002217 std::vector<const Type*> ParamTypeList;
Reid Spencer4388f0b2007-04-22 05:46:44 +00002218 ParamAttrsVector Attrs;
2219 if ($7 != ParamAttr::None) {
2220 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $7;
2221 Attrs.push_back(PAWI);
2222 }
Chris Lattner53bdd312005-05-06 20:27:19 +00002223 if ($5) { // If there are arguments...
Reid Spencer71b79e32007-04-09 06:17:21 +00002224 unsigned index = 1;
2225 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I, ++index) {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002226 const Type* Ty = I->Ty->get();
Reid Spencer6130ae02007-01-02 21:53:43 +00002227 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2228 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencerfa35bb32006-12-31 05:40:12 +00002229 ParamTypeList.push_back(Ty);
2230 if (Ty != Type::VoidTy)
Reid Spencer4388f0b2007-04-22 05:46:44 +00002231 if (I->Attrs != ParamAttr::None) {
2232 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2233 Attrs.push_back(PAWI);
2234 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00002235 }
Chris Lattner19613292002-10-15 21:41:14 +00002236 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002237
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002238 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2239 if (isVarArg) ParamTypeList.pop_back();
2240
Reid Spencer4388f0b2007-04-22 05:46:44 +00002241 ParamAttrsList *PAL = 0;
2242 if (!Attrs.empty())
2243 PAL = ParamAttrsList::get(Attrs);
Reid Spencer71b79e32007-04-09 06:17:21 +00002244
Reid Spencer78517652007-04-28 16:06:50 +00002245 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg, PAL);
Chris Lattner19613292002-10-15 21:41:14 +00002246 const PointerType *PFT = PointerType::get(FT);
Reid Spencer136a91c2007-01-05 17:06:19 +00002247 delete $2;
Chris Lattner2f7c9632001-06-06 20:29:01 +00002248
Chris Lattner8abe1a12004-07-14 23:03:46 +00002249 ValID ID;
2250 if (!FunctionName.empty()) {
Reid Spencer791b8ef2007-01-26 08:04:51 +00002251 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattner8abe1a12004-07-14 23:03:46 +00002252 } else {
Reid Spencerfe65ae82007-03-19 18:39:36 +00002253 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner8abe1a12004-07-14 23:03:46 +00002254 }
2255
Chris Lattner91393ee2004-07-14 19:33:47 +00002256 Function *Fn = 0;
Chris Lattner8abe1a12004-07-14 23:03:46 +00002257 // See if this function was forward referenced. If so, recycle the object.
Reid Spencer78517652007-04-28 16:06:50 +00002258 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
Chris Lattner8abe1a12004-07-14 23:03:46 +00002259 // Move the function to the end of the list, from whereever it was
2260 // previously inserted.
2261 Fn = cast<Function>(FWRef);
2262 CurModule.CurrentModule->getFunctionList().remove(Fn);
2263 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2264 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002265 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Reid Spencer78517652007-04-28 16:06:50 +00002266 if (Fn->getFunctionType() != FT) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002267 // The existing function doesn't have the same type. This is an overload
2268 // error.
2269 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
2270 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
2271 // Neither the existing or the current function is a declaration and they
2272 // have the same name and same type. Clearly this is a redefinition.
Reid Spencerde1d05f2007-02-05 10:16:10 +00002273 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002274 } if (Fn->isDeclaration()) {
2275 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattner531f9e92005-03-15 04:54:21 +00002276 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattner8abe1a12004-07-14 23:03:46 +00002277 AI != AE; ++AI)
2278 AI->setName("");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002279 }
Chris Lattner91393ee2004-07-14 19:33:47 +00002280 } else { // Not already defined?
Chris Lattnereee35bf2007-04-26 05:30:35 +00002281 Fn = new Function(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
Chris Lattner91393ee2004-07-14 19:33:47 +00002282 CurModule.CurrentModule);
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002283
Chris Lattner91393ee2004-07-14 19:33:47 +00002284 InsertValue(Fn, CurModule.Values);
Chris Lattner91393ee2004-07-14 19:33:47 +00002285 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002286
Chris Lattner61877742003-10-16 20:12:13 +00002287 CurFun.FunctionStart(Fn);
Anton Korobeynikov0ab01ff2006-09-17 13:06:18 +00002288
2289 if (CurFun.isDeclare) {
2290 // If we have declaration, always overwrite linkage. This will allow us to
2291 // correctly handle cases, when pointer to function is passed as argument to
2292 // another function.
2293 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002294 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov0ab01ff2006-09-17 13:06:18 +00002295 }
Chris Lattner53bdd312005-05-06 20:27:19 +00002296 Fn->setCallingConv($1);
Reid Spencer136a91c2007-01-05 17:06:19 +00002297 Fn->setAlignment($9);
2298 if ($8) {
2299 Fn->setSection($8);
2300 free($8);
Chris Lattner71b936c2005-11-12 00:11:10 +00002301 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002302
Chris Lattner113f4f42002-06-25 16:13:24 +00002303 // Add all of the arguments we parsed to the function...
Chris Lattner53bdd312005-05-06 20:27:19 +00002304 if ($5) { // Is null if empty...
Chris Lattner149376d2002-10-13 20:57:00 +00002305 if (isVarArg) { // Nuke the last entry
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002306 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0 &&
Reid Spencer9894d6c2007-02-05 17:01:20 +00002307 "Not a varargs marker!");
Reid Spencerfa35bb32006-12-31 05:40:12 +00002308 delete $5->back().Ty;
Chris Lattner53bdd312005-05-06 20:27:19 +00002309 $5->pop_back(); // Delete the last entry
Chris Lattner149376d2002-10-13 20:57:00 +00002310 }
Chris Lattner531f9e92005-03-15 04:54:21 +00002311 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002312 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencerfa35bb32006-12-31 05:40:12 +00002313 unsigned Idx = 1;
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002314 for (ArgListType::iterator I = $5->begin();
2315 I != $5->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002316 delete I->Ty; // Delete the typeholder...
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002317 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer309080a2006-09-28 19:28:24 +00002318 CHECK_FOR_ERROR
Chris Lattner149376d2002-10-13 20:57:00 +00002319 InsertValue(ArgIt);
Reid Spencerfa35bb32006-12-31 05:40:12 +00002320 Idx++;
Chris Lattner2f7c9632001-06-06 20:29:01 +00002321 }
Reid Spencer812724e2006-12-03 05:45:44 +00002322
Chris Lattner53bdd312005-05-06 20:27:19 +00002323 delete $5; // We're now done with the argument list
Chris Lattner2f7c9632001-06-06 20:29:01 +00002324 }
Reid Spencer713eedc2006-08-18 08:43:06 +00002325 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002326};
Chris Lattner2f7c9632001-06-06 20:29:01 +00002327
Chris Lattner1e1a9b42002-05-03 18:23:48 +00002328BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2329
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002330FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner61877742003-10-16 20:12:13 +00002331 $$ = CurFun.CurrentFunction;
Chris Lattneraa534bf2001-09-07 16:35:17 +00002332
Chris Lattner379a8d22003-04-16 20:28:45 +00002333 // Make sure that we keep track of the linkage type even if there was a
2334 // previous "declare".
2335 $$->setLinkage($1);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002336 $$->setVisibility($2);
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002337};
Chris Lattner2f7c9632001-06-06 20:29:01 +00002338
Chris Lattner1e1a9b42002-05-03 18:23:48 +00002339END : ENDTOK | '}'; // Allow end of '}' to end a function
2340
Chris Lattner57698e22002-03-26 18:01:55 +00002341Function : BasicBlockList END {
Chris Lattner2f7c9632001-06-06 20:29:01 +00002342 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002343 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002344};
Chris Lattner2f7c9632001-06-06 20:29:01 +00002345
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002346FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002347 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002348 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002349 $$ = CurFun.CurrentFunction;
2350 CurFun.FunctionDone();
2351 CHECK_FOR_ERROR
2352 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00002353
2354//===----------------------------------------------------------------------===//
2355// Rules to match Basic Blocks
2356//===----------------------------------------------------------------------===//
2357
Chris Lattnera02d6032006-01-25 22:26:43 +00002358OptSideEffect : /* empty */ {
2359 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002360 CHECK_FOR_ERROR
Chris Lattnera02d6032006-01-25 22:26:43 +00002361 }
2362 | SIDEEFFECT {
2363 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002364 CHECK_FOR_ERROR
Chris Lattnera02d6032006-01-25 22:26:43 +00002365 };
2366
Chris Lattner2f7c9632001-06-06 20:29:01 +00002367ConstValueRef : ESINT64VAL { // A reference to a direct constant
2368 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002369 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002370 }
2371 | EUINT64VAL {
2372 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002373 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002374 }
Chris Lattner212f70d2001-07-15 00:17:01 +00002375 | FPVAL { // Perhaps it's an FP constant?
2376 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002377 CHECK_FOR_ERROR
Chris Lattner212f70d2001-07-15 00:17:01 +00002378 }
Chris Lattnerc0ba90e2004-03-31 03:49:47 +00002379 | TRUETOK {
Zhou Sheng75b871f2007-01-11 12:24:14 +00002380 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer713eedc2006-08-18 08:43:06 +00002381 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002382 }
Chris Lattnerc0ba90e2004-03-31 03:49:47 +00002383 | FALSETOK {
Zhou Sheng75b871f2007-01-11 12:24:14 +00002384 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer713eedc2006-08-18 08:43:06 +00002385 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002386 }
Chris Lattnerfbdec252001-09-30 22:46:54 +00002387 | NULL_TOK {
2388 $$ = ValID::createNull();
Reid Spencer713eedc2006-08-18 08:43:06 +00002389 CHECK_FOR_ERROR
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00002390 }
Chris Lattner4ff31492004-10-16 18:17:13 +00002391 | UNDEF {
2392 $$ = ValID::createUndef();
Reid Spencer713eedc2006-08-18 08:43:06 +00002393 CHECK_FOR_ERROR
Chris Lattner4ff31492004-10-16 18:17:13 +00002394 }
Chris Lattner8c7bda52005-12-21 17:53:02 +00002395 | ZEROINITIALIZER { // A vector zero constant.
2396 $$ = ValID::createZeroInit();
Reid Spencer713eedc2006-08-18 08:43:06 +00002397 CHECK_FOR_ERROR
Chris Lattner8c7bda52005-12-21 17:53:02 +00002398 }
Brian Gaeke02209042004-08-20 06:00:58 +00002399 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencer812724e2006-12-03 05:45:44 +00002400 const Type *ETy = (*$2)[0]->getType();
Brian Gaeke02209042004-08-20 06:00:58 +00002401 int NumElements = $2->size();
2402
Reid Spencerd84d35b2007-02-15 02:26:10 +00002403 VectorType* pt = VectorType::get(ETy, NumElements);
Brian Gaeke02209042004-08-20 06:00:58 +00002404 PATypeHolder* PTy = new PATypeHolder(
Reid Spencer812724e2006-12-03 05:45:44 +00002405 HandleUpRefs(
Reid Spencerd84d35b2007-02-15 02:26:10 +00002406 VectorType::get(
Reid Spencer812724e2006-12-03 05:45:44 +00002407 ETy,
2408 NumElements)
2409 )
2410 );
Brian Gaeke02209042004-08-20 06:00:58 +00002411
2412 // Verify all elements are correct type!
2413 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencer812724e2006-12-03 05:45:44 +00002414 if (ETy != (*$2)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002415 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke02209042004-08-20 06:00:58 +00002416 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencer812724e2006-12-03 05:45:44 +00002417 (*$2)[i]->getType()->getDescription() + "'.");
Brian Gaeke02209042004-08-20 06:00:58 +00002418 }
2419
Reid Spencerd84d35b2007-02-15 02:26:10 +00002420 $$ = ValID::create(ConstantVector::get(pt, *$2));
Brian Gaeke02209042004-08-20 06:00:58 +00002421 delete PTy; delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002422 CHECK_FOR_ERROR
Brian Gaeke02209042004-08-20 06:00:58 +00002423 }
Chris Lattner7f325cd2002-08-16 21:14:40 +00002424 | ConstExpr {
Reid Spencer812724e2006-12-03 05:45:44 +00002425 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002426 CHECK_FOR_ERROR
Chris Lattnera02d6032006-01-25 22:26:43 +00002427 }
2428 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
2429 char *End = UnEscapeLexed($3, true);
2430 std::string AsmStr = std::string($3, End);
2431 End = UnEscapeLexed($5, true);
2432 std::string Constraints = std::string($5, End);
2433 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
2434 free($3);
2435 free($5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002436 CHECK_FOR_ERROR
Chris Lattner7f325cd2002-08-16 21:14:40 +00002437 };
Chris Lattnerfbdec252001-09-30 22:46:54 +00002438
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002439// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2440// another value.
2441//
Reid Spencer791b8ef2007-01-26 08:04:51 +00002442SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2443 $$ = ValID::createLocalID($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002444 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002445 }
Reid Spencer791b8ef2007-01-26 08:04:51 +00002446 | GLOBALVAL_ID {
2447 $$ = ValID::createGlobalID($1);
2448 CHECK_FOR_ERROR
2449 }
2450 | LocalName { // Is it a named reference...?
2451 $$ = ValID::createLocalName($1);
2452 CHECK_FOR_ERROR
2453 }
2454 | GlobalName { // Is it a named reference...?
2455 $$ = ValID::createGlobalName($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002456 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002457 };
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002458
2459// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002460ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002461
Chris Lattner2f7c9632001-06-06 20:29:01 +00002462
Chris Lattner252afba2001-07-26 16:29:15 +00002463// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2464// type immediately preceeds the value reference, and allows complex constant
2465// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattner571a6b02001-10-03 01:49:25 +00002466ResolvedVal : Types ValueRef {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002467 if (!UpRefs.empty())
2468 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2469 $$ = getVal(*$1, $2);
2470 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002471 CHECK_FOR_ERROR
Reid Spencerfa35bb32006-12-31 05:40:12 +00002472 }
2473 ;
Chris Lattner42b5a8a2001-07-25 22:47:46 +00002474
Chris Lattner2f7c9632001-06-06 20:29:01 +00002475BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner2ed776b2004-07-13 07:59:27 +00002476 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002477 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002478 }
Chris Lattner113f4f42002-06-25 16:13:24 +00002479 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner2ed776b2004-07-13 07:59:27 +00002480 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002481 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002482 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00002483
2484
2485// Basic blocks are terminated by branching instructions:
2486// br, br/cc, switch, ret
2487//
Reid Spencer791b8ef2007-01-26 08:04:51 +00002488BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner2ed776b2004-07-13 07:59:27 +00002489 setValueName($3, $2);
Reid Spencer309080a2006-09-28 19:28:24 +00002490 CHECK_FOR_ERROR
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002491 InsertValue($3);
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002492 $1->getInstList().push_back($3);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002493 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002494 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002495 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00002496
2497InstructionList : InstructionList Inst {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002498 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2499 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2500 if (CI2->getParent() == 0)
2501 $1->getInstList().push_back(CI2);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002502 $1->getInstList().push_back($2);
2503 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002504 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002505 }
Reid Spencerfe65ae82007-03-19 18:39:36 +00002506 | /* empty */ { // Empty space between instruction lists
2507 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
Reid Spencer713eedc2006-08-18 08:43:06 +00002508 CHECK_FOR_ERROR
Chris Lattnere84a2ba2004-07-13 08:39:15 +00002509 }
Reid Spencerfe65ae82007-03-19 18:39:36 +00002510 | LABELSTR { // Labelled (named) basic block
2511 $$ = defineBBVal(ValID::createLocalName($1));
Reid Spencer713eedc2006-08-18 08:43:06 +00002512 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002513 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00002514
Chris Lattner252afba2001-07-26 16:29:15 +00002515BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencer812724e2006-12-03 05:45:44 +00002516 $$ = new ReturnInst($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00002517 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002518 }
Reid Spencerfe65ae82007-03-19 18:39:36 +00002519 | RET VOID { // Return with no result...
Chris Lattner2f7c9632001-06-06 20:29:01 +00002520 $$ = new ReturnInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002521 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002522 }
Reid Spencerfe65ae82007-03-19 18:39:36 +00002523 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer309080a2006-09-28 19:28:24 +00002524 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002525 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002526 $$ = new BranchInst(tmpBB);
Reid Spencerfe65ae82007-03-19 18:39:36 +00002527 } // Conditional Branch...
Reid Spencer502d64e2007-01-13 05:00:20 +00002528 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
2529 assert(cast<IntegerType>($2)->getBitWidth() == 1 && "Not Bool?");
Reid Spencer309080a2006-09-28 19:28:24 +00002530 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002531 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002532 BasicBlock* tmpBBB = getBBVal($9);
2533 CHECK_FOR_ERROR
Reid Spencer542964f2007-01-11 18:21:29 +00002534 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002535 CHECK_FOR_ERROR
2536 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002537 }
2538 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencer812724e2006-12-03 05:45:44 +00002539 Value* tmpVal = getVal($2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002540 CHECK_FOR_ERROR
2541 BasicBlock* tmpBB = getBBVal($6);
2542 CHECK_FOR_ERROR
2543 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattner2f7c9632001-06-06 20:29:01 +00002544 $$ = S;
2545
Chris Lattner89da8a32003-04-22 18:42:41 +00002546 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattnerae234e12002-04-09 19:41:42 +00002547 E = $8->end();
Chris Lattnerfc824c12005-02-24 05:25:17 +00002548 for (; I != E; ++I) {
2549 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2550 S->addCase(CI, I->second);
2551 else
Reid Spencerde1d05f2007-02-05 10:16:10 +00002552 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattnerfc824c12005-02-24 05:25:17 +00002553 }
Chris Lattnerca96cee2004-04-17 23:49:15 +00002554 delete $8;
Reid Spencer713eedc2006-08-18 08:43:06 +00002555 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002556 }
Chris Lattner9f3648b2003-05-15 21:30:00 +00002557 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencer812724e2006-12-03 05:45:44 +00002558 Value* tmpVal = getVal($2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002559 CHECK_FOR_ERROR
2560 BasicBlock* tmpBB = getBBVal($6);
2561 CHECK_FOR_ERROR
2562 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattner9f3648b2003-05-15 21:30:00 +00002563 $$ = S;
Reid Spencer713eedc2006-08-18 08:43:06 +00002564 CHECK_FOR_ERROR
Chris Lattner9f3648b2003-05-15 21:30:00 +00002565 }
Reid Spencer136a91c2007-01-05 17:06:19 +00002566 | INVOKE OptCallingConv ResultTypes ValueRef '(' ValueRefList ')' OptFuncAttrs
Chris Lattner53bdd312005-05-06 20:27:19 +00002567 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002568
Reid Spencerfa35bb32006-12-31 05:40:12 +00002569 // Handle the short syntax
2570 const PointerType *PFTy = 0;
2571 const FunctionType *Ty = 0;
Reid Spencer136a91c2007-01-05 17:06:19 +00002572 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner19613292002-10-15 21:41:14 +00002573 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002574 // Pull out the types of all of the arguments...
Chris Lattner89da8a32003-04-22 18:42:41 +00002575 std::vector<const Type*> ParamTypes;
Reid Spencer78517652007-04-28 16:06:50 +00002576 ParamAttrsVector Attrs;
2577 if ($8 != ParamAttr::None) {
Chris Lattnercf40d502007-05-04 04:01:07 +00002578 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $8;
Reid Spencer78517652007-04-28 16:06:50 +00002579 Attrs.push_back(PAWI);
2580 }
Reid Spencer71b79e32007-04-09 06:17:21 +00002581 ValueRefList::iterator I = $6->begin(), E = $6->end();
Reid Spencer78517652007-04-28 16:06:50 +00002582 unsigned index = 1;
2583 for (; I != E; ++I, ++index) {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002584 const Type *Ty = I->Val->getType();
2585 if (Ty == Type::VoidTy)
2586 GEN_ERROR("Short call syntax cannot be used with varargs");
2587 ParamTypes.push_back(Ty);
Reid Spencer78517652007-04-28 16:06:50 +00002588 if (I->Attrs != ParamAttr::None) {
2589 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2590 Attrs.push_back(PAWI);
2591 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002592 }
Reid Spencer78517652007-04-28 16:06:50 +00002593
2594 ParamAttrsList *PAL = 0;
2595 if (!Attrs.empty())
2596 PAL = ParamAttrsList::get(Attrs);
2597 Ty = FunctionType::get($3->get(), ParamTypes, false, PAL);
Chris Lattner19613292002-10-15 21:41:14 +00002598 PFTy = PointerType::get(Ty);
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002599 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002600
Reid Spencerd05bc522007-03-20 01:13:00 +00002601 delete $3;
2602
Chris Lattner53bdd312005-05-06 20:27:19 +00002603 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer309080a2006-09-28 19:28:24 +00002604 CHECK_FOR_ERROR
Reid Spencer136a91c2007-01-05 17:06:19 +00002605 BasicBlock *Normal = getBBVal($11);
Reid Spencer309080a2006-09-28 19:28:24 +00002606 CHECK_FOR_ERROR
Reid Spencer136a91c2007-01-05 17:06:19 +00002607 BasicBlock *Except = getBBVal($14);
Reid Spencer309080a2006-09-28 19:28:24 +00002608 CHECK_FOR_ERROR
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002609
Reid Spencerfa35bb32006-12-31 05:40:12 +00002610 // Check the arguments
2611 ValueList Args;
2612 if ($6->empty()) { // Has no arguments?
2613 // Make sure no arguments is a good thing!
2614 if (Ty->getNumParams() != 0)
2615 GEN_ERROR("No arguments passed to a function that "
Reid Spencerde1d05f2007-02-05 10:16:10 +00002616 "expects arguments");
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002617 } else { // Has arguments?
Chris Lattner57698e22002-03-26 18:01:55 +00002618 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002619 // correctly!
Chris Lattnerfa829be2004-02-09 04:14:01 +00002620 FunctionType::param_iterator I = Ty->param_begin();
2621 FunctionType::param_iterator E = Ty->param_end();
Reid Spencerfa35bb32006-12-31 05:40:12 +00002622 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Reid Spencer78517652007-04-28 16:06:50 +00002623
Reid Spencerfa35bb32006-12-31 05:40:12 +00002624 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2625 if (ArgI->Val->getType() != *I)
2626 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00002627 (*I)->getDescription() + "'");
Reid Spencerfa35bb32006-12-31 05:40:12 +00002628 Args.push_back(ArgI->Val);
2629 }
Reid Spencer812724e2006-12-03 05:45:44 +00002630
Reid Spencerfa35bb32006-12-31 05:40:12 +00002631 if (Ty->isVarArg()) {
2632 if (I == E)
2633 for (; ArgI != ArgE; ++ArgI)
2634 Args.push_back(ArgI->Val); // push the remaining varargs
2635 } else if (I != E || ArgI != ArgE)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002636 GEN_ERROR("Invalid number of parameters detected");
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002637 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00002638
2639 // Create the InvokeInst
Chris Lattnerd8028242007-02-13 05:53:56 +00002640 InvokeInst *II = new InvokeInst(V, Normal, Except, &Args[0], Args.size());
Reid Spencerfa35bb32006-12-31 05:40:12 +00002641 II->setCallingConv($2);
2642 $$ = II;
Chris Lattner53bdd312005-05-06 20:27:19 +00002643 delete $6;
Reid Spencer713eedc2006-08-18 08:43:06 +00002644 CHECK_FOR_ERROR
Chris Lattner9c58cf62003-09-08 18:54:55 +00002645 }
2646 | UNWIND {
2647 $$ = new UnwindInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002648 CHECK_FOR_ERROR
Chris Lattner4ff31492004-10-16 18:17:13 +00002649 }
2650 | UNREACHABLE {
2651 $$ = new UnreachableInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002652 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002653 };
Chris Lattnerf26e0d92001-10-13 06:41:08 +00002654
2655
Chris Lattner2f7c9632001-06-06 20:29:01 +00002656
2657JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2658 $$ = $1;
Reid Spencerfe65ae82007-03-19 18:39:36 +00002659 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer309080a2006-09-28 19:28:24 +00002660 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002661 if (V == 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002662 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner2f7c9632001-06-06 20:29:01 +00002663
Reid Spencer309080a2006-09-28 19:28:24 +00002664 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002665 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002666 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner2f7c9632001-06-06 20:29:01 +00002667 }
2668 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner89da8a32003-04-22 18:42:41 +00002669 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencerfe65ae82007-03-19 18:39:36 +00002670 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer309080a2006-09-28 19:28:24 +00002671 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002672
2673 if (V == 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002674 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner2f7c9632001-06-06 20:29:01 +00002675
Reid Spencer309080a2006-09-28 19:28:24 +00002676 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002677 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002678 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002679 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00002680
Reid Spencer791b8ef2007-01-26 08:04:51 +00002681Inst : OptLocalAssign InstVal {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002682 // Is this definition named?? if so, assign the name...
2683 setValueName($2, $1);
2684 CHECK_FOR_ERROR
2685 InsertValue($2);
2686 $$ = $2;
2687 CHECK_FOR_ERROR
2688 };
2689
Chris Lattner2f7c9632001-06-06 20:29:01 +00002690
Chris Lattner931ef3b2001-06-11 15:04:20 +00002691PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencerfa35bb32006-12-31 05:40:12 +00002692 if (!UpRefs.empty())
2693 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner89da8a32003-04-22 18:42:41 +00002694 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencer812724e2006-12-03 05:45:44 +00002695 Value* tmpVal = getVal(*$1, $3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002696 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002697 BasicBlock* tmpBB = getBBVal($5);
2698 CHECK_FOR_ERROR
2699 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencer812724e2006-12-03 05:45:44 +00002700 delete $1;
Chris Lattner931ef3b2001-06-11 15:04:20 +00002701 }
2702 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2703 $$ = $1;
Reid Spencer309080a2006-09-28 19:28:24 +00002704 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002705 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002706 BasicBlock* tmpBB = getBBVal($6);
2707 CHECK_FOR_ERROR
2708 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002709 };
Chris Lattner931ef3b2001-06-11 15:04:20 +00002710
2711
Reid Spencerfa35bb32006-12-31 05:40:12 +00002712ValueRefList : Types ValueRef OptParamAttrs {
2713 if (!UpRefs.empty())
2714 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2715 // Used for call and invoke instructions
2716 $$ = new ValueRefList();
2717 ValueRefListEntry E; E.Attrs = $3; E.Val = getVal($1->get(), $2);
2718 $$->push_back(E);
Reid Spencerd05bc522007-03-20 01:13:00 +00002719 delete $1;
Chris Lattner2f7c9632001-06-06 20:29:01 +00002720 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00002721 | ValueRefList ',' Types ValueRef OptParamAttrs {
2722 if (!UpRefs.empty())
2723 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner2f7c9632001-06-06 20:29:01 +00002724 $$ = $1;
Reid Spencerfa35bb32006-12-31 05:40:12 +00002725 ValueRefListEntry E; E.Attrs = $5; E.Val = getVal($3->get(), $4);
2726 $$->push_back(E);
Reid Spencerd05bc522007-03-20 01:13:00 +00002727 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00002728 CHECK_FOR_ERROR
Reid Spencerfa35bb32006-12-31 05:40:12 +00002729 }
2730 | /*empty*/ { $$ = new ValueRefList(); };
Chris Lattner2f7c9632001-06-06 20:29:01 +00002731
Reid Spencerfa35bb32006-12-31 05:40:12 +00002732IndexList // Used for gep instructions and constant expressions
Reid Spencerd6979032006-12-31 21:46:36 +00002733 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencerfa35bb32006-12-31 05:40:12 +00002734 | IndexList ',' ResolvedVal {
2735 $$ = $1;
2736 $$->push_back($3);
2737 CHECK_FOR_ERROR
2738 }
Reid Spencer6829c252006-12-31 21:25:25 +00002739 ;
Chris Lattner2f7c9632001-06-06 20:29:01 +00002740
Chris Lattner06038452005-05-06 05:51:46 +00002741OptTailCall : TAIL CALL {
2742 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002743 CHECK_FOR_ERROR
Chris Lattner06038452005-05-06 05:51:46 +00002744 }
2745 | CALL {
2746 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002747 CHECK_FOR_ERROR
Chris Lattner06038452005-05-06 05:51:46 +00002748 };
2749
Chris Lattnerc0686fe2002-09-10 19:57:26 +00002750InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002751 if (!UpRefs.empty())
2752 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner03c49532007-01-15 02:27:26 +00002753 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencerd84d35b2007-02-15 02:26:10 +00002754 !isa<VectorType>((*$2).get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002755 GEN_ERROR(
Reid Spencerde1d05f2007-02-05 10:16:10 +00002756 "Arithmetic operator requires integer, FP, or packed operands");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002757 if (isa<VectorType>((*$2).get()) &&
Reid Spencer812724e2006-12-03 05:45:44 +00002758 ($1 == Instruction::URem ||
2759 $1 == Instruction::SRem ||
2760 $1 == Instruction::FRem))
Reid Spencer09575ba2007-02-15 03:39:18 +00002761 GEN_ERROR("Remainder not supported on vector types");
Reid Spencer812724e2006-12-03 05:45:44 +00002762 Value* val1 = getVal(*$2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002763 CHECK_FOR_ERROR
Reid Spencer812724e2006-12-03 05:45:44 +00002764 Value* val2 = getVal(*$2, $5);
Reid Spencer309080a2006-09-28 19:28:24 +00002765 CHECK_FOR_ERROR
Reid Spencer812724e2006-12-03 05:45:44 +00002766 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattnerc0686fe2002-09-10 19:57:26 +00002767 if ($$ == 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002768 GEN_ERROR("binary operator returned null");
Reid Spencer812724e2006-12-03 05:45:44 +00002769 delete $2;
Chris Lattnerc0686fe2002-09-10 19:57:26 +00002770 }
2771 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002772 if (!UpRefs.empty())
2773 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner03c49532007-01-15 02:27:26 +00002774 if (!(*$2)->isInteger()) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002775 if (Instruction::isShift($1) || !isa<VectorType>($2->get()) ||
2776 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencerde1d05f2007-02-05 10:16:10 +00002777 GEN_ERROR("Logical operator requires integral operands");
Chris Lattnera5d70e92005-12-21 18:31:29 +00002778 }
Reid Spencer812724e2006-12-03 05:45:44 +00002779 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002780 CHECK_FOR_ERROR
Reid Spencer812724e2006-12-03 05:45:44 +00002781 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer309080a2006-09-28 19:28:24 +00002782 CHECK_FOR_ERROR
Reid Spencer812724e2006-12-03 05:45:44 +00002783 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattnerc0686fe2002-09-10 19:57:26 +00002784 if ($$ == 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002785 GEN_ERROR("binary operator returned null");
Reid Spencer812724e2006-12-03 05:45:44 +00002786 delete $2;
Chris Lattnerc0686fe2002-09-10 19:57:26 +00002787 }
Reid Spencer812724e2006-12-03 05:45:44 +00002788 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002789 if (!UpRefs.empty())
2790 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencerd84d35b2007-02-15 02:26:10 +00002791 if (isa<VectorType>((*$3).get()))
Reid Spencer09575ba2007-02-15 03:39:18 +00002792 GEN_ERROR("Vector types not supported by icmp instruction");
Reid Spencer812724e2006-12-03 05:45:44 +00002793 Value* tmpVal1 = getVal(*$3, $4);
2794 CHECK_FOR_ERROR
2795 Value* tmpVal2 = getVal(*$3, $6);
2796 CHECK_FOR_ERROR
2797 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2798 if ($$ == 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002799 GEN_ERROR("icmp operator returned null");
Reid Spencerd05bc522007-03-20 01:13:00 +00002800 delete $3;
Reid Spencer812724e2006-12-03 05:45:44 +00002801 }
2802 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002803 if (!UpRefs.empty())
2804 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencerd84d35b2007-02-15 02:26:10 +00002805 if (isa<VectorType>((*$3).get()))
Reid Spencer09575ba2007-02-15 03:39:18 +00002806 GEN_ERROR("Vector types not supported by fcmp instruction");
Reid Spencer812724e2006-12-03 05:45:44 +00002807 Value* tmpVal1 = getVal(*$3, $4);
2808 CHECK_FOR_ERROR
2809 Value* tmpVal2 = getVal(*$3, $6);
2810 CHECK_FOR_ERROR
2811 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2812 if ($$ == 0)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002813 GEN_ERROR("fcmp operator returned null");
Reid Spencerd05bc522007-03-20 01:13:00 +00002814 delete $3;
Chris Lattner2f7c9632001-06-06 20:29:01 +00002815 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002816 | CastOps ResolvedVal TO Types {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002817 if (!UpRefs.empty())
2818 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00002819 Value* Val = $2;
Reid Spencer082a77f2007-01-17 02:47:33 +00002820 const Type* DestTy = $4->get();
2821 if (!CastInst::castIsValid($1, Val, DestTy))
2822 GEN_ERROR("invalid cast opcode for cast from '" +
2823 Val->getType()->getDescription() + "' to '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00002824 DestTy->getDescription() + "'");
Reid Spencer082a77f2007-01-17 02:47:33 +00002825 $$ = CastInst::create($1, Val, DestTy);
Reid Spencer812724e2006-12-03 05:45:44 +00002826 delete $4;
Chris Lattnera6821822001-07-08 04:57:15 +00002827 }
Chris Lattner6536f0c2004-03-12 05:51:36 +00002828 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer542964f2007-01-11 18:21:29 +00002829 if ($2->getType() != Type::Int1Ty)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002830 GEN_ERROR("select condition must be boolean");
Reid Spencer812724e2006-12-03 05:45:44 +00002831 if ($4->getType() != $6->getType())
Reid Spencerde1d05f2007-02-05 10:16:10 +00002832 GEN_ERROR("select value types should match");
Reid Spencer812724e2006-12-03 05:45:44 +00002833 $$ = new SelectInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002834 CHECK_FOR_ERROR
Chris Lattner6536f0c2004-03-12 05:51:36 +00002835 }
Chris Lattner0079e7d2003-10-18 05:53:13 +00002836 | VAARG ResolvedVal ',' Types {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002837 if (!UpRefs.empty())
2838 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00002839 $$ = new VAArgInst($2, *$4);
2840 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00002841 CHECK_FOR_ERROR
Chris Lattner0079e7d2003-10-18 05:53:13 +00002842 }
Robert Bocchinoaa1cf542006-01-10 19:04:32 +00002843 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencer812724e2006-12-03 05:45:44 +00002844 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerde1d05f2007-02-05 10:16:10 +00002845 GEN_ERROR("Invalid extractelement operands");
Reid Spencer812724e2006-12-03 05:45:44 +00002846 $$ = new ExtractElementInst($2, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002847 CHECK_FOR_ERROR
Robert Bocchinoaa1cf542006-01-10 19:04:32 +00002848 }
Robert Bocchinofdf9e412006-01-17 20:06:25 +00002849 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer812724e2006-12-03 05:45:44 +00002850 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerde1d05f2007-02-05 10:16:10 +00002851 GEN_ERROR("Invalid insertelement operands");
Reid Spencer812724e2006-12-03 05:45:44 +00002852 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002853 CHECK_FOR_ERROR
Robert Bocchinofdf9e412006-01-17 20:06:25 +00002854 }
Chris Lattner8ec3c2e2006-04-08 01:18:35 +00002855 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer812724e2006-12-03 05:45:44 +00002856 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerde1d05f2007-02-05 10:16:10 +00002857 GEN_ERROR("Invalid shufflevector operands");
Reid Spencer812724e2006-12-03 05:45:44 +00002858 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002859 CHECK_FOR_ERROR
Chris Lattner8ec3c2e2006-04-08 01:18:35 +00002860 }
Chris Lattnerb94550e2003-10-19 21:34:28 +00002861 | PHI_TOK PHIList {
Chris Lattner931ef3b2001-06-11 15:04:20 +00002862 const Type *Ty = $2->front().first->getType();
Chris Lattner0fc43a62003-10-30 01:38:18 +00002863 if (!Ty->isFirstClassType())
Reid Spencerde1d05f2007-02-05 10:16:10 +00002864 GEN_ERROR("PHI node operands must be of first class type");
Chris Lattner931ef3b2001-06-11 15:04:20 +00002865 $$ = new PHINode(Ty);
Chris Lattner2c089492005-01-29 00:35:55 +00002866 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner2f7c9632001-06-06 20:29:01 +00002867 while ($2->begin() != $2->end()) {
Chris Lattner931ef3b2001-06-11 15:04:20 +00002868 if ($2->front().first->getType() != Ty)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002869 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattnerda558102001-10-02 03:41:24 +00002870 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002871 $2->pop_front();
2872 }
2873 delete $2; // Free the list...
Reid Spencer713eedc2006-08-18 08:43:06 +00002874 CHECK_FOR_ERROR
Chris Lattner06038452005-05-06 05:51:46 +00002875 }
Reid Spencer136a91c2007-01-05 17:06:19 +00002876 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ValueRefList ')'
2877 OptFuncAttrs {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002878
2879 // Handle the short syntax
Bill Wendlingad753612006-11-12 11:10:39 +00002880 const PointerType *PFTy = 0;
2881 const FunctionType *Ty = 0;
Reid Spencer136a91c2007-01-05 17:06:19 +00002882 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner19613292002-10-15 21:41:14 +00002883 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner42b5a8a2001-07-25 22:47:46 +00002884 // Pull out the types of all of the arguments...
Chris Lattner89da8a32003-04-22 18:42:41 +00002885 std::vector<const Type*> ParamTypes;
Reid Spencer78517652007-04-28 16:06:50 +00002886 ParamAttrsVector Attrs;
2887 if ($8 != ParamAttr::None) {
2888 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $8;
2889 Attrs.push_back(PAWI);
2890 }
2891 unsigned index = 1;
Reid Spencer71b79e32007-04-09 06:17:21 +00002892 ValueRefList::iterator I = $6->begin(), E = $6->end();
Reid Spencer78517652007-04-28 16:06:50 +00002893 for (; I != E; ++I, ++index) {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002894 const Type *Ty = I->Val->getType();
2895 if (Ty == Type::VoidTy)
2896 GEN_ERROR("Short call syntax cannot be used with varargs");
2897 ParamTypes.push_back(Ty);
Reid Spencer78517652007-04-28 16:06:50 +00002898 if (I->Attrs != ParamAttr::None) {
2899 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2900 Attrs.push_back(PAWI);
2901 }
Chris Lattner7fac0702001-10-03 14:53:21 +00002902 }
Reid Spencer78517652007-04-28 16:06:50 +00002903
2904 ParamAttrsList *PAL = 0;
2905 if (!Attrs.empty())
2906 PAL = ParamAttrsList::get(Attrs);
2907
2908 Ty = FunctionType::get($3->get(), ParamTypes, false, PAL);
Chris Lattner19613292002-10-15 21:41:14 +00002909 PFTy = PointerType::get(Ty);
Chris Lattner42b5a8a2001-07-25 22:47:46 +00002910 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002911
Chris Lattner53bdd312005-05-06 20:27:19 +00002912 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer309080a2006-09-28 19:28:24 +00002913 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002914
Reid Spencer94bae692007-04-16 06:55:42 +00002915 // Check for call to invalid intrinsic to avoid crashing later.
2916 if (Function *theF = dyn_cast<Function>(V)) {
Reid Spencer9c9741e2007-04-16 22:01:57 +00002917 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
Reid Spencer4339f7d2007-04-16 20:31:06 +00002918 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
2919 !theF->getIntrinsicID(true))
Reid Spencer94bae692007-04-16 06:55:42 +00002920 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
2921 theF->getName() + "'");
2922 }
2923
Reid Spencerfa35bb32006-12-31 05:40:12 +00002924 // Check the arguments
2925 ValueList Args;
2926 if ($6->empty()) { // Has no arguments?
Chris Lattner91e08322002-07-25 20:52:56 +00002927 // Make sure no arguments is a good thing!
2928 if (Ty->getNumParams() != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002929 GEN_ERROR("No arguments passed to a function that "
Reid Spencerde1d05f2007-02-05 10:16:10 +00002930 "expects arguments");
Chris Lattner42b5a8a2001-07-25 22:47:46 +00002931 } else { // Has arguments?
Chris Lattner57698e22002-03-26 18:01:55 +00002932 // Loop through FunctionType's arguments and ensure they are specified
Reid Spencer78517652007-04-28 16:06:50 +00002933 // correctly!
2934 //
Chris Lattnerfa829be2004-02-09 04:14:01 +00002935 FunctionType::param_iterator I = Ty->param_begin();
2936 FunctionType::param_iterator E = Ty->param_end();
Reid Spencerfa35bb32006-12-31 05:40:12 +00002937 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Reid Spencer78517652007-04-28 16:06:50 +00002938
Reid Spencerfa35bb32006-12-31 05:40:12 +00002939 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2940 if (ArgI->Val->getType() != *I)
2941 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00002942 (*I)->getDescription() + "'");
Reid Spencerfa35bb32006-12-31 05:40:12 +00002943 Args.push_back(ArgI->Val);
2944 }
2945 if (Ty->isVarArg()) {
2946 if (I == E)
2947 for (; ArgI != ArgE; ++ArgI)
2948 Args.push_back(ArgI->Val); // push the remaining varargs
2949 } else if (I != E || ArgI != ArgE)
Reid Spencerde1d05f2007-02-05 10:16:10 +00002950 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner42b5a8a2001-07-25 22:47:46 +00002951 }
Reid Spencerfa35bb32006-12-31 05:40:12 +00002952 // Create the call node
Chris Lattnerd8028242007-02-13 05:53:56 +00002953 CallInst *CI = new CallInst(V, &Args[0], Args.size());
Reid Spencerfa35bb32006-12-31 05:40:12 +00002954 CI->setTailCall($1);
2955 CI->setCallingConv($2);
2956 $$ = CI;
Chris Lattner53bdd312005-05-06 20:27:19 +00002957 delete $6;
Reid Spencer791b8ef2007-01-26 08:04:51 +00002958 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00002959 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002960 }
2961 | MemoryInst {
2962 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002963 CHECK_FOR_ERROR
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00002964 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00002965
Chris Lattner8b1680e2003-09-08 18:20:29 +00002966OptVolatile : VOLATILE {
2967 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002968 CHECK_FOR_ERROR
Chris Lattner8b1680e2003-09-08 18:20:29 +00002969 }
2970 | /* empty */ {
2971 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002972 CHECK_FOR_ERROR
Chris Lattner8b1680e2003-09-08 18:20:29 +00002973 };
2974
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00002975
Chris Lattner06038452005-05-06 05:51:46 +00002976
Chris Lattnerc7de8362005-11-06 06:34:12 +00002977MemoryInst : MALLOC Types OptCAlign {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002978 if (!UpRefs.empty())
2979 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00002980 $$ = new MallocInst(*$2, 0, $3);
2981 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002982 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00002983 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +00002984 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002985 if (!UpRefs.empty())
2986 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00002987 Value* tmpVal = getVal($4, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002988 CHECK_FOR_ERROR
Reid Spencer812724e2006-12-03 05:45:44 +00002989 $$ = new MallocInst(*$2, tmpVal, $6);
2990 delete $2;
Nate Begeman848622f2005-11-05 09:21:28 +00002991 }
Chris Lattnerc7de8362005-11-06 06:34:12 +00002992 | ALLOCA Types OptCAlign {
Reid Spencerfa35bb32006-12-31 05:40:12 +00002993 if (!UpRefs.empty())
2994 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00002995 $$ = new AllocaInst(*$2, 0, $3);
2996 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002997 CHECK_FOR_ERROR
Nate Begeman848622f2005-11-05 09:21:28 +00002998 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +00002999 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencerfa35bb32006-12-31 05:40:12 +00003000 if (!UpRefs.empty())
3001 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00003002 Value* tmpVal = getVal($4, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00003003 CHECK_FOR_ERROR
Reid Spencer812724e2006-12-03 05:45:44 +00003004 $$ = new AllocaInst(*$2, tmpVal, $6);
3005 delete $2;
Nate Begeman848622f2005-11-05 09:21:28 +00003006 }
Chris Lattner252afba2001-07-26 16:29:15 +00003007 | FREE ResolvedVal {
Reid Spencer812724e2006-12-03 05:45:44 +00003008 if (!isa<PointerType>($2->getType()))
Reid Spencer713eedc2006-08-18 08:43:06 +00003009 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerde1d05f2007-02-05 10:16:10 +00003010 $2->getType()->getDescription() + "");
Reid Spencer812724e2006-12-03 05:45:44 +00003011 $$ = new FreeInst($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00003012 CHECK_FOR_ERROR
Chris Lattner2f7c9632001-06-06 20:29:01 +00003013 }
3014
Christopher Lamb84485702007-04-22 19:24:39 +00003015 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencerfa35bb32006-12-31 05:40:12 +00003016 if (!UpRefs.empty())
3017 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00003018 if (!isa<PointerType>($3->get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00003019 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencer812724e2006-12-03 05:45:44 +00003020 (*$3)->getDescription());
3021 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00003022 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencer812724e2006-12-03 05:45:44 +00003023 (*$3)->getDescription());
3024 Value* tmpVal = getVal(*$3, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00003025 CHECK_FOR_ERROR
Christopher Lamb84485702007-04-22 19:24:39 +00003026 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencer812724e2006-12-03 05:45:44 +00003027 delete $3;
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00003028 }
Christopher Lamb84485702007-04-22 19:24:39 +00003029 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencerfa35bb32006-12-31 05:40:12 +00003030 if (!UpRefs.empty())
3031 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00003032 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner667ded92003-09-01 16:31:28 +00003033 if (!PT)
Reid Spencer713eedc2006-08-18 08:43:06 +00003034 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencer812724e2006-12-03 05:45:44 +00003035 (*$5)->getDescription());
Chris Lattner667ded92003-09-01 16:31:28 +00003036 const Type *ElTy = PT->getElementType();
Reid Spencer812724e2006-12-03 05:45:44 +00003037 if (ElTy != $3->getType())
3038 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerde1d05f2007-02-05 10:16:10 +00003039 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner319c47a2002-08-21 23:51:21 +00003040
Reid Spencer812724e2006-12-03 05:45:44 +00003041 Value* tmpVal = getVal(*$5, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00003042 CHECK_FOR_ERROR
Christopher Lamb84485702007-04-22 19:24:39 +00003043 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencer812724e2006-12-03 05:45:44 +00003044 delete $5;
Chris Lattner62ecb4a2001-07-08 23:22:50 +00003045 }
Chris Lattner476148f2001-11-26 16:54:11 +00003046 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencerfa35bb32006-12-31 05:40:12 +00003047 if (!UpRefs.empty())
3048 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer812724e2006-12-03 05:45:44 +00003049 if (!isa<PointerType>($2->get()))
Reid Spencerde1d05f2007-02-05 10:16:10 +00003050 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattnerfd9fbe12004-04-05 01:30:04 +00003051
Chris Lattner04a2d762007-02-13 00:57:40 +00003052 if (!GetElementPtrInst::getIndexedType(*$2, &(*$4)[0], $4->size(), true))
Reid Spencer713eedc2006-08-18 08:43:06 +00003053 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerde1d05f2007-02-05 10:16:10 +00003054 (*$2)->getDescription()+ "'");
Reid Spencer812724e2006-12-03 05:45:44 +00003055 Value* tmpVal = getVal(*$2, $3);
Reid Spencer713eedc2006-08-18 08:43:06 +00003056 CHECK_FOR_ERROR
Chris Lattner04a2d762007-02-13 00:57:40 +00003057 $$ = new GetElementPtrInst(tmpVal, &(*$4)[0], $4->size());
Reid Spencer812724e2006-12-03 05:45:44 +00003058 delete $2;
Reid Spencer309080a2006-09-28 19:28:24 +00003059 delete $4;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00003060 };
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00003061
Brian Gaeke960707c2003-11-11 22:41:34 +00003062
Chris Lattner2f7c9632001-06-06 20:29:01 +00003063%%
Reid Spencer713eedc2006-08-18 08:43:06 +00003064
Reid Spencerfa35bb32006-12-31 05:40:12 +00003065// common code from the two 'RunVMAsmParser' functions
3066static Module* RunParser(Module * M) {
3067
3068 llvmAsmlineno = 1; // Reset the current line number...
3069 CurModule.CurrentModule = M;
3070#if YYDEBUG
3071 yydebug = Debug;
3072#endif
3073
3074 // Check to make sure the parser succeeded
3075 if (yyparse()) {
3076 if (ParserResult)
3077 delete ParserResult;
3078 return 0;
3079 }
3080
Reid Spencercf2ccbf2007-03-30 01:37:13 +00003081 // Emit an error if there are any unresolved types left.
3082 if (!CurModule.LateResolveTypes.empty()) {
3083 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3084 if (DID.Type == ValID::LocalName) {
3085 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3086 } else {
3087 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3088 }
3089 if (ParserResult)
3090 delete ParserResult;
3091 return 0;
3092 }
3093
3094 // Emit an error if there are any unresolved values left.
3095 if (!CurModule.LateResolveValues.empty()) {
3096 Value *V = CurModule.LateResolveValues.back();
3097 std::map<Value*, std::pair<ValID, int> >::iterator I =
3098 CurModule.PlaceHolderInfo.find(V);
3099
3100 if (I != CurModule.PlaceHolderInfo.end()) {
3101 ValID &DID = I->second.first;
3102 if (DID.Type == ValID::LocalName) {
3103 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3104 } else {
3105 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3106 }
3107 if (ParserResult)
3108 delete ParserResult;
3109 return 0;
3110 }
3111 }
3112
Reid Spencerfa35bb32006-12-31 05:40:12 +00003113 // Check to make sure that parsing produced a result
3114 if (!ParserResult)
3115 return 0;
3116
3117 // Reset ParserResult variable while saving its value for the result.
3118 Module *Result = ParserResult;
3119 ParserResult = 0;
3120
3121 return Result;
3122}
3123
Reid Spencer713eedc2006-08-18 08:43:06 +00003124void llvm::GenerateError(const std::string &message, int LineNo) {
3125 if (LineNo == -1) LineNo = llvmAsmlineno;
3126 // TODO: column number in exception
3127 if (TheParseError)
3128 TheParseError->setError(CurFilename, message, LineNo);
3129 TriggerError = 1;
3130}
3131
Chris Lattnera6821822001-07-08 04:57:15 +00003132int yyerror(const char *ErrorMsg) {
Chris Lattner89da8a32003-04-22 18:42:41 +00003133 std::string where
3134 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00003135 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00003136 std::string errMsg = where + "error: " + std::string(ErrorMsg);
3137 if (yychar != YYEMPTY && yychar != 0)
3138 errMsg += " while reading token: '" + std::string(llvmAsmtext, llvmAsmleng)+
3139 "'";
Reid Spencer713eedc2006-08-18 08:43:06 +00003140 GenerateError(errMsg);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003141 return 0;
3142}