blob: a03f617ae5d99e8bc9dac2cebdd21f1ac3bb6ab9 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
Misha Brukman5a2a3822005-05-10 22:02:28 +00002//
John Criswell856ba762003-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 Brukman5a2a3822005-05-10 22:02:28 +00007//
John Criswell856ba762003-10-21 15:17:13 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the bison parser for LLVM assembly languages files.
11//
Chris Lattnercf3056d2003-10-13 03:32:08 +000012//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000013
Chris Lattner00950542001-06-06 20:29:01 +000014%{
15#include "ParserInternals.h"
Chris Lattnera8e8f162005-05-06 20:27:19 +000016#include "llvm/CallingConv.h"
Chris Lattneraa2c8532006-01-25 22:26:43 +000017#include "llvm/InlineAsm.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000018#include "llvm/Instructions.h"
Chris Lattner00950542001-06-06 20:29:01 +000019#include "llvm/Module.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000020#include "llvm/SymbolTable.h"
Chris Lattner830b6f92004-04-05 01:30:04 +000021#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencere1553cc2006-12-31 05:40:12 +000022#include "llvm/Support/CommandLine.h"
Chris Lattnere0135402007-01-31 04:43:46 +000023#include "llvm/ADT/SmallVector.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/ADT/STLExtras.h"
Chris Lattner87ac9722005-11-06 06:46:28 +000025#include "llvm/Support/MathExtras.h"
Bill Wendling3e3bcbf2006-11-28 22:47:12 +000026#include "llvm/Support/Streams.h"
Reid Spencer8ce1da72004-07-04 12:19:05 +000027#include <algorithm>
Chris Lattner00950542001-06-06 20:29:01 +000028#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000029#include <utility>
Reid Spencere1553cc2006-12-31 05:40:12 +000030#ifndef NDEBUG
31#define YYDEBUG 1
32#endif
Chris Lattner00950542001-06-06 20:29:01 +000033
Reid Spencere4f47592006-08-18 17:32:55 +000034// The following is a gross hack. In order to rid the libAsmParser library of
35// exceptions, we have to have a way of getting the yyparse function to go into
36// an error situation. So, whenever we want an error to occur, the GenerateError
37// function (see bottom of file) sets TriggerError. Then, at the end of each
38// production in the grammer we use CHECK_FOR_ERROR which will invoke YYERROR
39// (a goto) to put YACC in error state. Furthermore, several calls to
40// GenerateError are made from inside productions and they must simulate the
41// previous exception behavior by exiting the production immediately. We have
42// replaced these with the GEN_ERROR macro which calls GeneratError and then
43// immediately invokes YYERROR. This would be so much cleaner if it was a
44// recursive descent parser.
Reid Spencer61c83e02006-08-18 08:43:06 +000045static bool TriggerError = false;
Reid Spencerf63697d2006-10-09 17:36:59 +000046#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYABORT; } }
Reid Spencer61c83e02006-08-18 08:43:06 +000047#define GEN_ERROR(msg) { GenerateError(msg); YYERROR; }
48
Chris Lattner386a3b72001-10-16 19:54:17 +000049int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000050int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000051int yyparse();
52
Brian Gaeked0fde302003-11-11 22:41:34 +000053namespace llvm {
Chris Lattner86427632004-07-14 06:39:48 +000054 std::string CurFilename;
Reid Spencere1553cc2006-12-31 05:40:12 +000055#if YYDEBUG
56static cl::opt<bool>
57Debug("debug-yacc", cl::desc("Print yacc debug state changes"),
58 cl::Hidden, cl::init(false));
59#endif
Chris Lattner86427632004-07-14 06:39:48 +000060}
61using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000062
Chris Lattner00950542001-06-06 20:29:01 +000063static Module *ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +000064
Chris Lattner30c89792001-09-07 16:35:17 +000065// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
66// relating to upreferences in the input stream.
67//
68//#define DEBUG_UPREFS 1
69#ifdef DEBUG_UPREFS
Bill Wendlinge8156192006-12-07 01:30:32 +000070#define UR_OUT(X) cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000071#else
72#define UR_OUT(X)
73#endif
74
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000075#define YYERROR_VERBOSE 1
76
Chris Lattnerb2b96672005-11-12 18:21:21 +000077static GlobalVariable *CurGV;
Andrew Lenharth558bc882005-06-18 18:34:52 +000078
79
Chris Lattner7e708292002-06-25 16:13:24 +000080// This contains info used when building the body of a function. It is
81// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000082//
Chris Lattner9232b992003-04-22 18:42:41 +000083typedef std::vector<Value *> ValueList; // Numbered defs
Reid Spencere1553cc2006-12-31 05:40:12 +000084
Misha Brukman5a2a3822005-05-10 22:02:28 +000085static void
86ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
87 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000088
89static struct PerModuleInfo {
90 Module *CurrentModule;
Chris Lattner735f2702004-07-08 22:30:50 +000091 std::map<const Type *, ValueList> Values; // Module level numbered definitions
92 std::map<const Type *,ValueList> LateResolveValues;
Reid Spencerb78b9082006-11-28 07:28:14 +000093 std::vector<PATypeHolder> Types;
94 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000095
Chris Lattnerbc721ed2004-07-13 08:28:21 +000096 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Reid Spencerfd20c0a2006-05-29 02:34:34 +000097 /// how they were referenced and on which line of the input they came from so
Chris Lattner22ae2322004-07-13 08:39:15 +000098 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerbc721ed2004-07-13 08:28:21 +000099 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
100
Chris Lattner2079fde2001-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 Spencer3271ed52004-07-18 00:08:11 +0000104 // here. This is used for forward references of GlobalValues.
Chris Lattner2079fde2001-10-13 06:41:08 +0000105 //
Chris Lattner9232b992003-04-22 18:42:41 +0000106 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +0000107 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +0000108 GlobalRefsType GlobalRefs;
109
Chris Lattner00950542001-06-06 20:29:01 +0000110 void ModuleDone() {
Chris Lattner7e708292002-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 Lattner30c89792001-09-07 16:35:17 +0000114 //
Chris Lattner00950542001-06-06 20:29:01 +0000115 ResolveDefinitions(LateResolveValues);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000116 if (TriggerError)
117 return;
Chris Lattner00950542001-06-06 20:29:01 +0000118
Chris Lattner2079fde2001-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 Lattner9232b992003-04-22 18:42:41 +0000123 std::string UndefinedReferences = "Unresolved global references exist:\n";
Misha Brukman5a2a3822005-05-10 22:02:28 +0000124
Chris Lattner749ce032002-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 Spencer61c83e02006-08-18 08:43:06 +0000130 GenerateError(UndefinedReferences);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000131 return;
Chris Lattner2079fde2001-10-13 06:41:08 +0000132 }
133
Chris Lattner7e708292002-06-25 16:13:24 +0000134 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000135 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000136 CurrentModule = 0;
137 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000138
Chris Lattner8a327842004-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.
142 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
143 // Check to see if there is a forward reference to this global variable...
144 // if there is, eliminate it and patch the reference to use the new def'n.
145 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
146 GlobalValue *Ret = 0;
147 if (I != GlobalRefs.end()) {
148 Ret = I->second;
149 GlobalRefs.erase(I);
150 }
151 return Ret;
152 }
Reid Spencer98b3c5c2007-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 }
203
204
Chris Lattner00950542001-06-06 20:29:01 +0000205} CurModule;
206
Chris Lattner79df7c02002-03-26 18:01:55 +0000207static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000208 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000209
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000210 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
Chris Lattner735f2702004-07-08 22:30:50 +0000211 std::map<const Type*, ValueList> LateResolveValues;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000212 bool isDeclare; // Is this function a forward declararation?
213 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000214 GlobalValue::VisibilityTypes Visibility;
Chris Lattner00950542001-06-06 20:29:01 +0000215
Chris Lattner2e2ed292004-07-14 06:28:35 +0000216 /// BBForwardRefs - When we see forward references to basic blocks, keep
217 /// track of them here.
218 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
219 std::vector<BasicBlock*> NumberedBlocks;
220 unsigned NextBBNum;
221
Chris Lattner79df7c02002-03-26 18:01:55 +0000222 inline PerFunctionInfo() {
223 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000224 isDeclare = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000225 Linkage = GlobalValue::ExternalLinkage;
226 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner00950542001-06-06 20:29:01 +0000227 }
228
Chris Lattner79df7c02002-03-26 18:01:55 +0000229 inline void FunctionStart(Function *M) {
230 CurrentFunction = M;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000231 NextBBNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000232 }
233
Chris Lattner79df7c02002-03-26 18:01:55 +0000234 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000235 NumberedBlocks.clear();
236
237 // Any forward referenced blocks left?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000238 if (!BBForwardRefs.empty()) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000239 GenerateError("Undefined reference to label " +
Chris Lattner83beacd2005-02-24 04:59:49 +0000240 BBForwardRefs.begin()->first->getName());
Reid Spencer5b7e7532006-09-28 19:28:24 +0000241 return;
242 }
Chris Lattner2e2ed292004-07-14 06:28:35 +0000243
244 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000245 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000246
Chris Lattner7e708292002-06-25 16:13:24 +0000247 Values.clear(); // Clear out function local definitions
Chris Lattner79df7c02002-03-26 18:01:55 +0000248 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000249 isDeclare = false;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000250 Linkage = GlobalValue::ExternalLinkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000251 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner00950542001-06-06 20:29:01 +0000252 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000253} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000254
Chris Lattner394f2eb2003-10-16 20:12:13 +0000255static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000256
Chris Lattner00950542001-06-06 20:29:01 +0000257
258//===----------------------------------------------------------------------===//
259// Code to handle definitions of all the types
260//===----------------------------------------------------------------------===//
261
Chris Lattner735f2702004-07-08 22:30:50 +0000262static int InsertValue(Value *V,
263 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
264 if (V->hasName()) return -1; // Is this a numbered definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000265
266 // Yes, insert the value into the value table...
Chris Lattner735f2702004-07-08 22:30:50 +0000267 ValueList &List = ValueTab[V->getType()];
268 List.push_back(V);
Chris Lattner16af11d2004-02-09 21:03:38 +0000269 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000270}
271
Chris Lattner30c89792001-09-07 16:35:17 +0000272static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000273 switch (D.Type) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000274 case ValID::LocalID: // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000275 // Module constants occupy the lowest numbered slots...
Reid Spencerb2d17862007-01-26 08:04:51 +0000276 if (D.Num < CurModule.Types.size())
277 return CurModule.Types[D.Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000278 break;
Reid Spencerb2d17862007-01-26 08:04:51 +0000279 case ValID::LocalName: // Is it a named definition?
Chris Lattnera09000d2004-07-14 23:03:46 +0000280 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
281 D.destroy(); // Free old strdup'd memory...
282 return N;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000283 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000284 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000285 default:
Reid Spencerf4fa5902007-02-05 10:16:10 +0000286 GenerateError("Internal parser error: Invalid symbol type reference");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000287 return 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000288 }
289
290 // If we reached here, we referenced either a symbol that we don't know about
291 // or an id number that hasn't been read yet. We may be referencing something
292 // forward, so just create an entry to be resolved later and get to it...
293 //
294 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
295
Chris Lattner355ad1f2005-03-21 06:27:42 +0000296
297 if (inFunctionScope()) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000298 if (D.Type == ValID::LocalName) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000299 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000300 return 0;
301 } else {
Reid Spencerb2d17862007-01-26 08:04:51 +0000302 GenerateError("Reference to an undefined type: #" + utostr(D.Num));
Reid Spencer5b7e7532006-09-28 19:28:24 +0000303 return 0;
304 }
Chris Lattner4a42e902001-10-22 05:56:09 +0000305 }
Chris Lattner30c89792001-09-07 16:35:17 +0000306
Reid Spencerb78b9082006-11-28 07:28:14 +0000307 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Chris Lattner355ad1f2005-03-21 06:27:42 +0000308 if (I != CurModule.LateResolveTypes.end())
Reid Spencerb78b9082006-11-28 07:28:14 +0000309 return I->second;
Chris Lattner355ad1f2005-03-21 06:27:42 +0000310
Reid Spencerb78b9082006-11-28 07:28:14 +0000311 Type *Typ = OpaqueType::get();
312 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
313 return Typ;
Reid Spencer08de34b2006-12-03 05:45:44 +0000314 }
Chris Lattner30c89792001-09-07 16:35:17 +0000315
Chris Lattner2079fde2001-10-13 06:41:08 +0000316// getValNonImprovising - Look up the value specified by the provided type and
317// the provided ValID. If the value exists and has already been defined, return
318// it. Otherwise return null.
319//
320static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000321 if (isa<FunctionType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000322 GenerateError("Functions are not values and "
Chris Lattner79df7c02002-03-26 18:01:55 +0000323 "must be referenced as pointers");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000324 return 0;
325 }
Chris Lattner386a3b72001-10-16 19:54:17 +0000326
Chris Lattner30c89792001-09-07 16:35:17 +0000327 switch (D.Type) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000328 case ValID::LocalID: { // Is it a numbered definition?
329 // Module constants occupy the lowest numbered slots.
330 std::map<const Type*,ValueList>::iterator VI = CurFun.Values.find(Ty);
331 // Make sure that our type is within bounds.
Chris Lattner16af11d2004-02-09 21:03:38 +0000332 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000333
Reid Spencerb2d17862007-01-26 08:04:51 +0000334 // Check that the number is within bounds.
335 if (D.Num >= VI->second.size()) return 0;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000336
Reid Spencerb2d17862007-01-26 08:04:51 +0000337 return VI->second[D.Num];
338 }
339 case ValID::GlobalID: { // Is it a numbered definition?
340 unsigned Num = D.Num;
341
342 // Module constants occupy the lowest numbered slots...
343 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
344 if (VI == CurModule.Values.end()) return 0;
345 if (D.Num >= VI->second.size()) return 0;
Chris Lattner16af11d2004-02-09 21:03:38 +0000346 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000347 }
Reid Spencerb2d17862007-01-26 08:04:51 +0000348
349 case ValID::LocalName: { // Is it a named definition?
350 if (!inFunctionScope()) return 0;
351 SymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
352 Value *N = SymTab.lookup(Ty, D.Name);
353 if (N == 0) return 0;
354
355 D.destroy(); // Free old strdup'd memory...
356 return N;
357 }
358 case ValID::GlobalName: { // Is it a named definition?
359 SymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable();
360 Value *N = SymTab.lookup(Ty, D.Name);
Chris Lattner2079fde2001-10-13 06:41:08 +0000361 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000362
363 D.destroy(); // Free old strdup'd memory...
364 return N;
365 }
366
Misha Brukman5a2a3822005-05-10 22:02:28 +0000367 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner2079fde2001-10-13 06:41:08 +0000368 // value will fit into the specified type...
369 case ValID::ConstSIntVal: // Is it a constant pool reference??
Reid Spencerb83eb642006-10-20 07:07:24 +0000370 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000371 GenerateError("Signed integral constant '" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000372 itostr(D.ConstPool64) + "' is invalid for type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000373 Ty->getDescription() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000374 return 0;
375 }
Reid Spencerb83eb642006-10-20 07:07:24 +0000376 return ConstantInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000377
378 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Reid Spencerb83eb642006-10-20 07:07:24 +0000379 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
380 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000381 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000382 "' is invalid or out of range");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000383 return 0;
Chris Lattner2079fde2001-10-13 06:41:08 +0000384 } else { // This is really a signed reference. Transmogrify.
Reid Spencerb83eb642006-10-20 07:07:24 +0000385 return ConstantInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000386 }
387 } else {
Reid Spencerb83eb642006-10-20 07:07:24 +0000388 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000389 }
390
Chris Lattner2079fde2001-10-13 06:41:08 +0000391 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000392 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000393 GenerateError("FP constant invalid for type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000394 return 0;
395 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000396 return ConstantFP::get(Ty, D.ConstPoolFP);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000397
Chris Lattner2079fde2001-10-13 06:41:08 +0000398 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000399 if (!isa<PointerType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000400 GenerateError("Cannot create a a non pointer null");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000401 return 0;
402 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000403 return ConstantPointerNull::get(cast<PointerType>(Ty));
Misha Brukman5a2a3822005-05-10 22:02:28 +0000404
Chris Lattner16710e92004-10-16 18:17:13 +0000405 case ValID::ConstUndefVal: // Is it an undef value?
406 return UndefValue::get(Ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000407
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000408 case ValID::ConstZeroVal: // Is it a zero value?
409 return Constant::getNullValue(Ty);
410
Chris Lattnerd78700d2002-08-16 21:14:40 +0000411 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000412 if (D.ConstantValue->getType() != Ty) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000413 GenerateError("Constant expression type different from required type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000414 return 0;
415 }
Chris Lattnerd78700d2002-08-16 21:14:40 +0000416 return D.ConstantValue;
417
Chris Lattneraa2c8532006-01-25 22:26:43 +0000418 case ValID::InlineAsmVal: { // Inline asm expression
419 const PointerType *PTy = dyn_cast<PointerType>(Ty);
420 const FunctionType *FTy =
421 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000422 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000423 GenerateError("Invalid type for asm constraint string");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000424 return 0;
425 }
Chris Lattneraa2c8532006-01-25 22:26:43 +0000426 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
427 D.IAD->HasSideEffects);
428 D.destroy(); // Free InlineAsmDescriptor.
429 return IA;
430 }
Chris Lattner30c89792001-09-07 16:35:17 +0000431 default:
Reid Spencerf4fa5902007-02-05 10:16:10 +0000432 assert(0 && "Unhandled case");
Chris Lattner2079fde2001-10-13 06:41:08 +0000433 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000434 } // End of switch
435
Reid Spencerf4fa5902007-02-05 10:16:10 +0000436 assert(0 && "Unhandled case");
Chris Lattner2079fde2001-10-13 06:41:08 +0000437 return 0;
438}
439
Chris Lattner2079fde2001-10-13 06:41:08 +0000440// getVal - This function is identical to getValNonImprovising, except that if a
441// value is not already defined, it "improvises" by creating a placeholder var
442// that looks and acts just like the requested variable. When the value is
443// defined later, all uses of the placeholder variable are replaced with the
444// real thing.
445//
Chris Lattner64116f92004-07-14 01:33:11 +0000446static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000447 if (Ty == Type::LabelTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000448 GenerateError("Cannot use a basic block here");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000449 return 0;
450 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000451
Chris Lattner64116f92004-07-14 01:33:11 +0000452 // See if the value has already been defined.
453 Value *V = getValNonImprovising(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000454 if (V) return V;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000455 if (TriggerError) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000456
Reid Spencer5b7e7532006-09-28 19:28:24 +0000457 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000458 GenerateError("Invalid use of a composite type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000459 return 0;
460 }
Chris Lattner60cd9552005-03-23 01:29:26 +0000461
Chris Lattner00950542001-06-06 20:29:01 +0000462 // If we reached here, we referenced either a symbol that we don't know about
463 // or an id number that hasn't been read yet. We may be referencing something
464 // forward, so just create an entry to be resolved later and get to it...
465 //
Chris Lattner64116f92004-07-14 01:33:11 +0000466 V = new Argument(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000467
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000468 // Remember where this forward reference came from. FIXME, shouldn't we try
469 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000470 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000471 llvmAsmlineno)));
472
Chris Lattner79df7c02002-03-26 18:01:55 +0000473 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000474 InsertValue(V, CurFun.LateResolveValues);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000475 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000476 InsertValue(V, CurModule.LateResolveValues);
477 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000478}
479
Chris Lattner2e2ed292004-07-14 06:28:35 +0000480/// getBBVal - This is used for two purposes:
481/// * If isDefinition is true, a new basic block with the specified ID is being
482/// defined.
483/// * If isDefinition is true, this is a reference to a basic block, which may
484/// or may not be a forward reference.
485///
486static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000487 assert(inFunctionScope() && "Can't get basic block at global scope");
Chris Lattner64116f92004-07-14 01:33:11 +0000488
Chris Lattner2e2ed292004-07-14 06:28:35 +0000489 std::string Name;
490 BasicBlock *BB = 0;
491 switch (ID.Type) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000492 default:
493 GenerateError("Illegal label reference " + ID.getName());
494 return 0;
Reid Spencerb2d17862007-01-26 08:04:51 +0000495 case ValID::LocalID: // Is it a numbered definition?
496 if (ID.Num >= CurFun.NumberedBlocks.size())
Chris Lattner2e2ed292004-07-14 06:28:35 +0000497 CurFun.NumberedBlocks.resize(ID.Num+1);
498 BB = CurFun.NumberedBlocks[ID.Num];
499 break;
Reid Spencerb2d17862007-01-26 08:04:51 +0000500 case ValID::LocalName: // Is it a named definition?
Chris Lattner2e2ed292004-07-14 06:28:35 +0000501 Name = ID.Name;
Chris Lattnera09000d2004-07-14 23:03:46 +0000502 if (Value *N = CurFun.CurrentFunction->
Reid Spencer78d033e2007-01-06 07:24:44 +0000503 getValueSymbolTable().lookup(Type::LabelTy, Name))
Chris Lattner2e2ed292004-07-14 06:28:35 +0000504 BB = cast<BasicBlock>(N);
505 break;
506 }
Chris Lattner64116f92004-07-14 01:33:11 +0000507
Chris Lattner2e2ed292004-07-14 06:28:35 +0000508 // See if the block has already been defined.
509 if (BB) {
510 // If this is the definition of the block, make sure the existing value was
511 // just a forward reference. If it was a forward reference, there will be
512 // an entry for it in the PlaceHolderInfo map.
Reid Spencer5b7e7532006-09-28 19:28:24 +0000513 if (isDefinition && !CurFun.BBForwardRefs.erase(BB)) {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000514 // The existing value was a definition, not a forward reference.
Reid Spencer61c83e02006-08-18 08:43:06 +0000515 GenerateError("Redefinition of label " + ID.getName());
Reid Spencer5b7e7532006-09-28 19:28:24 +0000516 return 0;
517 }
Chris Lattner64116f92004-07-14 01:33:11 +0000518
Chris Lattner2e2ed292004-07-14 06:28:35 +0000519 ID.destroy(); // Free strdup'd memory.
520 return BB;
521 }
522
523 // Otherwise this block has not been seen before.
524 BB = new BasicBlock("", CurFun.CurrentFunction);
Reid Spencerb2d17862007-01-26 08:04:51 +0000525 if (ID.Type == ValID::LocalName) {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000526 BB->setName(ID.Name);
527 } else {
528 CurFun.NumberedBlocks[ID.Num] = BB;
529 }
530
531 // If this is not a definition, keep track of it so we can use it as a forward
532 // reference.
533 if (!isDefinition) {
534 // Remember where this forward reference came from.
535 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
536 } else {
537 // The forward declaration could have been inserted anywhere in the
538 // function: insert it into the correct place now.
539 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
540 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
541 }
Chris Lattnere8343a52004-10-26 18:26:14 +0000542 ID.destroy();
Chris Lattner64116f92004-07-14 01:33:11 +0000543 return BB;
544}
545
Chris Lattner00950542001-06-06 20:29:01 +0000546
547//===----------------------------------------------------------------------===//
548// Code to handle forward references in instructions
549//===----------------------------------------------------------------------===//
550//
551// This code handles the late binding needed with statements that reference
552// values not defined yet... for example, a forward branch, or the PHI node for
553// a loop body.
554//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000555// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000556// and back patchs after we are done.
557//
558
Misha Brukman5a2a3822005-05-10 22:02:28 +0000559// ResolveDefinitions - If we could not resolve some defs at parsing
560// time (forward branches, phi functions for loops, etc...) resolve the
Chris Lattner00950542001-06-06 20:29:01 +0000561// defs now...
562//
Misha Brukman5a2a3822005-05-10 22:02:28 +0000563static void
564ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
565 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000566 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000567 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000568 E = LateResolvers.end(); LRI != E; ++LRI) {
569 ValueList &List = LRI->second;
570 while (!List.empty()) {
571 Value *V = List.back();
572 List.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000573
574 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
575 CurModule.PlaceHolderInfo.find(V);
Reid Spencerf4fa5902007-02-05 10:16:10 +0000576 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error");
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000577
578 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000579
Chris Lattner735f2702004-07-08 22:30:50 +0000580 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000581 if (TriggerError)
582 return;
Chris Lattner386a3b72001-10-16 19:54:17 +0000583 if (TheRealValue) {
584 V->replaceAllUsesWith(TheRealValue);
585 delete V;
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000586 CurModule.PlaceHolderInfo.erase(PHI);
Chris Lattner386a3b72001-10-16 19:54:17 +0000587 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000588 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000589 // resolver table
590 InsertValue(V, *FutureLateResolvers);
591 } else {
Reid Spencerb2d17862007-01-26 08:04:51 +0000592 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000593 GenerateError("Reference to an invalid definition: '" +DID.getName()+
Reid Spencer3271ed52004-07-18 00:08:11 +0000594 "' of type '" + V->getType()->getDescription() + "'",
595 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000596 return;
597 } else {
Reid Spencer61c83e02006-08-18 08:43:06 +0000598 GenerateError("Reference to an invalid definition: #" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000599 itostr(DID.Num) + " of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000600 V->getType()->getDescription() + "'",
601 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000602 return;
603 }
Chris Lattner30c89792001-09-07 16:35:17 +0000604 }
Chris Lattner00950542001-06-06 20:29:01 +0000605 }
606 }
607
608 LateResolvers.clear();
609}
610
Chris Lattner4a42e902001-10-22 05:56:09 +0000611// ResolveTypeTo - A brand new type was just declared. This means that (if
612// name is not null) things referencing Name can be resolved. Otherwise, things
613// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000614//
Chris Lattner4a42e902001-10-22 05:56:09 +0000615static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner355ad1f2005-03-21 06:27:42 +0000616 ValID D;
Reid Spencerb2d17862007-01-26 08:04:51 +0000617 if (Name) D = ValID::createLocalName(Name);
618 else D = ValID::createLocalID(CurModule.Types.size());
Chris Lattner00950542001-06-06 20:29:01 +0000619
Reid Spencerb78b9082006-11-28 07:28:14 +0000620 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattner355ad1f2005-03-21 06:27:42 +0000621 CurModule.LateResolveTypes.find(D);
622 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencerb78b9082006-11-28 07:28:14 +0000623 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner355ad1f2005-03-21 06:27:42 +0000624 CurModule.LateResolveTypes.erase(I);
Chris Lattner30c89792001-09-07 16:35:17 +0000625 }
626}
627
Chris Lattner1781aca2001-09-18 04:00:54 +0000628// setValueName - Set the specified value to the name given. The name may be
629// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000630// assumed to be a malloc'd string buffer, and is free'd by this function.
631//
632static void setValueName(Value *V, char *NameStr) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000633 if (!NameStr) return;
634 std::string Name(NameStr); // Copy string
635 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000636
Reid Spencerb2d17862007-01-26 08:04:51 +0000637 if (V->getType() == Type::VoidTy) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000638 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencerb2d17862007-01-26 08:04:51 +0000639 return;
Chris Lattner8ab406d2004-07-13 07:59:27 +0000640 }
Reid Spencerb2d17862007-01-26 08:04:51 +0000641
Reid Spencerf4fa5902007-02-05 10:16:10 +0000642 assert(inFunctionScope() && "Must be in function scope");
Reid Spencerb2d17862007-01-26 08:04:51 +0000643 SymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
644 if (ST.lookup(V->getType(), Name)) {
645 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000646 V->getType()->getDescription() + "'");
Reid Spencerb2d17862007-01-26 08:04:51 +0000647 return;
648 }
649
650 // Set the name.
651 V->setName(Name);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000652}
653
Chris Lattnerd88998d2004-07-14 08:23:52 +0000654/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
655/// this is a declaration, otherwise it is a definition.
Chris Lattnerb2b96672005-11-12 18:21:21 +0000656static GlobalVariable *
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000657ParseGlobalVariable(char *NameStr,
658 GlobalValue::LinkageTypes Linkage,
659 GlobalValue::VisibilityTypes Visibility,
Chris Lattnerb2b96672005-11-12 18:21:21 +0000660 bool isConstantGlobal, const Type *Ty,
661 Constant *Initializer) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000662 if (isa<FunctionType>(Ty)) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000663 GenerateError("Cannot declare global vars of function type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000664 return 0;
665 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000666
Misha Brukman5a2a3822005-05-10 22:02:28 +0000667 const PointerType *PTy = PointerType::get(Ty);
Chris Lattnera09000d2004-07-14 23:03:46 +0000668
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000669 std::string Name;
670 if (NameStr) {
671 Name = NameStr; // Copy string
672 free(NameStr); // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000673 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000674
Chris Lattner8a327842004-07-14 21:44:00 +0000675 // See if this global value was forward referenced. If so, recycle the
676 // object.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000677 ValID ID;
Chris Lattner8a327842004-07-14 21:44:00 +0000678 if (!Name.empty()) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000679 ID = ValID::createGlobalName((char*)Name.c_str());
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000680 } else {
Reid Spencerb2d17862007-01-26 08:04:51 +0000681 ID = ValID::createGlobalID(CurModule.Values[PTy].size());
Chris Lattner8a327842004-07-14 21:44:00 +0000682 }
683
684 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000685 // Move the global to the end of the list, from whereever it was
Chris Lattner8a327842004-07-14 21:44:00 +0000686 // previously inserted.
687 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
688 CurModule.CurrentModule->getGlobalList().remove(GV);
689 CurModule.CurrentModule->getGlobalList().push_back(GV);
690 GV->setInitializer(Initializer);
691 GV->setLinkage(Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000692 GV->setVisibility(Visibility);
Chris Lattner8a327842004-07-14 21:44:00 +0000693 GV->setConstant(isConstantGlobal);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000694 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000695 return GV;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000696 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000697
698 // If this global has a name, check to see if there is already a definition
Reid Spencer90e2f632007-01-05 21:50:38 +0000699 // of this global in the module. If so, it is an error.
Chris Lattnera09000d2004-07-14 23:03:46 +0000700 if (!Name.empty()) {
701 // We are a simple redefinition of a value, check to see if it is defined
702 // the same as the old one.
Reid Spencer90e2f632007-01-05 21:50:38 +0000703 if (CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000704 GenerateError("Redefinition of global variable named '" + Name +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000705 "' of type '" + Ty->getDescription() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000706 return 0;
Chris Lattnera09000d2004-07-14 23:03:46 +0000707 }
708 }
709
710 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000711 GlobalVariable *GV =
Misha Brukman5a2a3822005-05-10 22:02:28 +0000712 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000713 CurModule.CurrentModule);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000714 GV->setVisibility(Visibility);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000715 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000716 return GV;
Chris Lattnerd88998d2004-07-14 08:23:52 +0000717}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000718
Reid Spencer75719bf2004-05-26 21:48:31 +0000719// setTypeName - Set the specified type to the name given. The name may be
720// null potentially, in which case this is a noop. The string passed in is
721// assumed to be a malloc'd string buffer, and is freed by this function.
722//
723// This function returns true if the type has already been defined, but is
724// allowed to be redefined in the specified context. If the name is a new name
725// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000726static bool setTypeName(const Type *T, char *NameStr) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000727 assert(!inFunctionScope() && "Can't give types function-local names");
Reid Spencer75719bf2004-05-26 21:48:31 +0000728 if (NameStr == 0) return false;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000729
Reid Spencer75719bf2004-05-26 21:48:31 +0000730 std::string Name(NameStr); // Copy string
731 free(NameStr); // Free old string
732
733 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000734 if (T == Type::VoidTy) {
Reid Spencerf4fa5902007-02-05 10:16:10 +0000735 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000736 return false;
737 }
Reid Spencer75719bf2004-05-26 21:48:31 +0000738
Chris Lattnera09000d2004-07-14 23:03:46 +0000739 // Set the type name, checking for conflicts as we do so.
740 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000741
Chris Lattnera09000d2004-07-14 23:03:46 +0000742 if (AlreadyExists) { // Inserting a name that is already defined???
743 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
744 assert(Existing && "Conflict but no matching type?");
745
Reid Spencer75719bf2004-05-26 21:48:31 +0000746 // There is only one case where this is allowed: when we are refining an
747 // opaque type. In this case, Existing will be an opaque type.
748 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
749 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000750 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000751 return true;
752 }
753
754 // Otherwise, this is an attempt to redefine a type. That's okay if
755 // the redefinition is identical to the original. This will be so if
756 // Existing and T point to the same Type object. In this one case we
757 // allow the equivalent redefinition.
758 if (Existing == T) return true; // Yes, it's equal.
759
760 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer90e2f632007-01-05 21:50:38 +0000761 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +0000762 T->getDescription() + "'");
Reid Spencer75719bf2004-05-26 21:48:31 +0000763 }
764
Reid Spencer75719bf2004-05-26 21:48:31 +0000765 return false;
766}
Chris Lattner8896eda2001-07-09 19:38:36 +0000767
Chris Lattner30c89792001-09-07 16:35:17 +0000768//===----------------------------------------------------------------------===//
769// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000770//
Chris Lattner8896eda2001-07-09 19:38:36 +0000771
Chris Lattner3b073862004-02-09 03:19:29 +0000772// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000773//
774static bool TypeContains(const Type *Ty, const Type *E) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000775 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
Chris Lattner6d8dbec2004-12-08 16:13:53 +0000776 E) != Ty->subtype_end();
Chris Lattner3b073862004-02-09 03:19:29 +0000777}
778
779namespace {
780 struct UpRefRecord {
781 // NestingLevel - The number of nesting levels that need to be popped before
782 // this type is resolved.
783 unsigned NestingLevel;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000784
Chris Lattner3b073862004-02-09 03:19:29 +0000785 // LastContainedTy - This is the type at the current binding level for the
786 // type. Every time we reduce the nesting level, this gets updated.
787 const Type *LastContainedTy;
788
789 // UpRefTy - This is the actual opaque type that the upreference is
790 // represented with.
791 OpaqueType *UpRefTy;
792
793 UpRefRecord(unsigned NL, OpaqueType *URTy)
794 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
795 };
Chris Lattner30c89792001-09-07 16:35:17 +0000796}
Chris Lattner698b56e2001-07-20 19:15:08 +0000797
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000798// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000799static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000800
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000801/// HandleUpRefs - Every time we finish a new layer of types, this function is
802/// called. It loops through the UpRefs vector, which is a list of the
803/// currently active types. For each type, if the up reference is contained in
804/// the newly completed type, we decrement the level count. When the level
805/// count reaches zero, the upreferenced type is the type that is passed in:
806/// thus we can complete the cycle.
807///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000808static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner703e92f2006-08-18 17:34:24 +0000809 // If Ty isn't abstract, or if there are no up-references in it, then there is
810 // nothing to resolve here.
811 if (!ty->isAbstract() || UpRefs.empty()) return ty;
812
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000813 PATypeHolder Ty(ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000814 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000815 "' newly formed. Resolving upreferences.\n" <<
816 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000817
818 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
819 // to zero), we resolve them all together before we resolve them to Ty. At
820 // the end of the loop, if there is anything to resolve to Ty, it will be in
821 // this variable.
822 OpaqueType *TypeToResolve = 0;
823
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000824 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000825 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
826 << UpRefs[i].second->getDescription() << ") = "
Reid Spencer3271ed52004-07-18 00:08:11 +0000827 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000828 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
829 // Decrement level of upreference
830 unsigned Level = --UpRefs[i].NestingLevel;
831 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000832 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000833 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000834 if (!TypeToResolve) {
835 TypeToResolve = UpRefs[i].UpRefTy;
836 } else {
837 UR_OUT(" * Resolving upreference for "
838 << UpRefs[i].second->getDescription() << "\n";
839 std::string OldName = UpRefs[i].UpRefTy->getDescription());
840 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
841 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
842 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
843 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000844 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000845 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000846 }
847 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000848 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000849
Chris Lattner026a8ce2004-02-09 18:53:54 +0000850 if (TypeToResolve) {
851 UR_OUT(" * Resolving upreference for "
852 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000853 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000854 TypeToResolve->refineAbstractTypeTo(Ty);
855 }
856
Chris Lattner8896eda2001-07-09 19:38:36 +0000857 return Ty;
858}
859
Chris Lattner00950542001-06-06 20:29:01 +0000860//===----------------------------------------------------------------------===//
861// RunVMAsmParser - Define an interface to this parser
862//===----------------------------------------------------------------------===//
863//
Reid Spencere1553cc2006-12-31 05:40:12 +0000864static Module* RunParser(Module * M);
865
Chris Lattner86427632004-07-14 06:39:48 +0000866Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner6184feb2005-05-20 03:25:47 +0000867 set_scan_file(F);
868
Chris Lattnera2850432001-07-22 18:36:00 +0000869 CurFilename = Filename;
Chris Lattner6184feb2005-05-20 03:25:47 +0000870 return RunParser(new Module(CurFilename));
871}
Chris Lattner00950542001-06-06 20:29:01 +0000872
Chris Lattner6184feb2005-05-20 03:25:47 +0000873Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
874 set_scan_string(AsmString);
Chris Lattner42570982003-11-26 07:24:58 +0000875
Chris Lattner6184feb2005-05-20 03:25:47 +0000876 CurFilename = "from_memory";
877 if (M == NULL) {
878 return RunParser(new Module (CurFilename));
879 } else {
880 return RunParser(M);
881 }
Chris Lattner00950542001-06-06 20:29:01 +0000882}
883
884%}
885
886%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000887 llvm::Module *ModuleVal;
888 llvm::Function *FunctionVal;
Brian Gaeked0fde302003-11-11 22:41:34 +0000889 llvm::BasicBlock *BasicBlockVal;
890 llvm::TerminatorInst *TermInstVal;
891 llvm::Instruction *InstVal;
Reid Spencer08de34b2006-12-03 05:45:44 +0000892 llvm::Constant *ConstVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000893
Reid Spencer08de34b2006-12-03 05:45:44 +0000894 const llvm::Type *PrimType;
Reid Spencere1553cc2006-12-31 05:40:12 +0000895 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencer08de34b2006-12-03 05:45:44 +0000896 llvm::PATypeHolder *TypeVal;
897 llvm::Value *ValueVal;
Reid Spencer08de34b2006-12-03 05:45:44 +0000898 std::vector<llvm::Value*> *ValueList;
Reid Spencere1553cc2006-12-31 05:40:12 +0000899 llvm::ArgListType *ArgList;
900 llvm::TypeWithAttrs TypeWithAttrs;
901 llvm::TypeWithAttrsList *TypeWithAttrsList;
902 llvm::ValueRefList *ValueRefList;
903
Misha Brukman5a2a3822005-05-10 22:02:28 +0000904 // Represent the RHS of PHI node
Reid Spencer08de34b2006-12-03 05:45:44 +0000905 std::list<std::pair<llvm::Value*,
906 llvm::BasicBlock*> > *PHIList;
Brian Gaeked0fde302003-11-11 22:41:34 +0000907 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencer08de34b2006-12-03 05:45:44 +0000908 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000909
Brian Gaeked0fde302003-11-11 22:41:34 +0000910 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000911 llvm::GlobalValue::VisibilityTypes Visibility;
Reid Spencere1553cc2006-12-31 05:40:12 +0000912 llvm::FunctionType::ParameterAttributes ParamAttrs;
Chris Lattner30c89792001-09-07 16:35:17 +0000913 int64_t SInt64Val;
914 uint64_t UInt64Val;
915 int SIntVal;
916 unsigned UIntVal;
917 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000918 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000919
Chris Lattner30c89792001-09-07 16:35:17 +0000920 char *StrVal; // This memory is strdup'd!
Reid Spencer1628cec2006-10-26 06:15:43 +0000921 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000922
Reid Spencer08de34b2006-12-03 05:45:44 +0000923 llvm::Instruction::BinaryOps BinaryOpVal;
924 llvm::Instruction::TermOps TermOpVal;
925 llvm::Instruction::MemoryOps MemOpVal;
926 llvm::Instruction::CastOps CastOpVal;
927 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencer08de34b2006-12-03 05:45:44 +0000928 llvm::ICmpInst::Predicate IPredicate;
929 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner00950542001-06-06 20:29:01 +0000930}
931
Reid Spencere1553cc2006-12-31 05:40:12 +0000932%type <ModuleVal> Module
Chris Lattner79df7c02002-03-26 18:01:55 +0000933%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000934%type <BasicBlockVal> BasicBlock InstructionList
935%type <TermInstVal> BBTerminatorInst
936%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000937%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000938%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000939%type <ArgList> ArgList ArgListH
Chris Lattnerc24d2082001-06-11 15:04:20 +0000940%type <PHIList> PHIList
Reid Spencere1553cc2006-12-31 05:40:12 +0000941%type <ValueRefList> ValueRefList // For call param lists & GEP indices
942%type <ValueList> IndexList // For GEP indices
943%type <TypeList> TypeListI
944%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer2c261782007-01-05 17:06:19 +0000945%type <TypeWithAttrs> ArgType
Chris Lattner00950542001-06-06 20:29:01 +0000946%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000947%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000948%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattnerddb6db42005-05-06 05:51:46 +0000949%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattneraa2c8532006-01-25 22:26:43 +0000950%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencere1553cc2006-12-31 05:40:12 +0000951%type <Linkage> GVInternalLinkage GVExternalLinkage
952%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000953%type <Visibility> GVVisibilityStyle
Chris Lattner00950542001-06-06 20:29:01 +0000954
Chris Lattner2079fde2001-10-13 06:41:08 +0000955// ValueRef - Unresolved reference to a definition or BB
956%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000957%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000958// Tokens and types for handling constant integer values
959//
960// ESINT64VAL - A negative number within long long range
961%token <SInt64Val> ESINT64VAL
962
963// EUINT64VAL - A positive number within uns. long long range
964%token <UInt64Val> EUINT64VAL
Chris Lattner00950542001-06-06 20:29:01 +0000965
Reid Spencerb2d17862007-01-26 08:04:51 +0000966%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000967%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000968
969// Built in types...
Reid Spencer2c261782007-01-05 17:06:19 +0000970%type <TypeVal> Types ResultTypes
Reid Spencere1553cc2006-12-31 05:40:12 +0000971%type <PrimType> IntType FPType PrimType // Classifications
Reid Spencer8088e9d2007-01-13 05:00:20 +0000972%token <PrimType> VOID INTTYPE
Reid Spencerb951bc02006-12-29 20:29:48 +0000973%token <PrimType> FLOAT DOUBLE LABEL
974%token TYPE
Chris Lattner00950542001-06-06 20:29:01 +0000975
Reid Spencerb2d17862007-01-26 08:04:51 +0000976%token<StrVal> LOCALVAR GLOBALVAR LABELSTR STRINGCONSTANT ATSTRINGCONSTANT
977%type <StrVal> LocalName OptLocalName OptLocalAssign
978%type <StrVal> GlobalName OptGlobalAssign
979%type <UIntVal> OptAlign OptCAlign
Chris Lattnerb2b96672005-11-12 18:21:21 +0000980%type <StrVal> OptSection SectionString
Chris Lattner00950542001-06-06 20:29:01 +0000981
Chris Lattner91ef4602004-03-31 03:49:47 +0000982%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Reid Spencerb951bc02006-12-29 20:29:48 +0000983%token DECLARE DEFINE GLOBAL CONSTANT SECTION VOLATILE
Reid Spencere1553cc2006-12-31 05:40:12 +0000984%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000985%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Reid Spencerb2d17862007-01-26 08:04:51 +0000986%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN
Chris Lattneraa2c8532006-01-25 22:26:43 +0000987%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000988%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattner10b27112006-10-22 06:07:41 +0000989%token DATALAYOUT
Chris Lattnera8e8f162005-05-06 20:27:19 +0000990%type <UIntVal> OptCallingConv
Reid Spencer2c261782007-01-05 17:06:19 +0000991%type <ParamAttrs> OptParamAttrs ParamAttr
992%type <ParamAttrs> OptFuncAttrs FuncAttr
Chris Lattner00950542001-06-06 20:29:01 +0000993
Misha Brukman5a2a3822005-05-10 22:02:28 +0000994// Basic Block Terminating Operators
Chris Lattner16710e92004-10-16 18:17:13 +0000995%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner00950542001-06-06 20:29:01 +0000996
Misha Brukman5a2a3822005-05-10 22:02:28 +0000997// Binary Operators
Reid Spencere4d87aa2006-12-23 06:05:41 +0000998%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencer0a783f72006-11-02 01:53:59 +0000999%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer832254e2007-02-02 02:16:23 +00001000%token <BinaryOpVal> SHL LSHR ASHR
1001
Reid Spencer08de34b2006-12-03 05:45:44 +00001002%token <OtherOpVal> ICMP FCMP
Reid Spencer08de34b2006-12-03 05:45:44 +00001003%type <IPredicate> IPredicates
Reid Spencer08de34b2006-12-03 05:45:44 +00001004%type <FPredicate> FPredicates
Reid Spencer9f746f42006-12-03 06:58:07 +00001005%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1006%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner00950542001-06-06 20:29:01 +00001007
1008// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001009%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +00001010
Reid Spencer3da59db2006-11-27 01:05:10 +00001011// Cast Operators
1012%type <CastOpVal> CastOps
1013%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1014%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1015
Chris Lattner027dcc52001-07-08 21:10:27 +00001016// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001017%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattner4c949082006-04-08 01:18:35 +00001018%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Chris Lattnerddb6db42005-05-06 05:51:46 +00001019
Reid Spencer2c261782007-01-05 17:06:19 +00001020// Function Attributes
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001021%token NORETURN INREG SRET
Chris Lattner027dcc52001-07-08 21:10:27 +00001022
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001023// Visibility Styles
1024%token DEFAULT HIDDEN
1025
Chris Lattner00950542001-06-06 20:29:01 +00001026%start Module
1027%%
1028
Chris Lattner00950542001-06-06 20:29:01 +00001029
Misha Brukman5a2a3822005-05-10 22:02:28 +00001030// Operations that are notably excluded from this list include:
Chris Lattner00950542001-06-06 20:29:01 +00001031// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1032//
Reid Spencer0a783f72006-11-02 01:53:59 +00001033ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer832254e2007-02-02 02:16:23 +00001034LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Reid Spencer3da59db2006-11-27 01:05:10 +00001035CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1036 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer832254e2007-02-02 02:16:23 +00001037
Reid Spencer9f746f42006-12-03 06:58:07 +00001038IPredicates
Reid Spencer763ed5e2006-12-04 05:20:06 +00001039 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer9f746f42006-12-03 06:58:07 +00001040 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1041 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1042 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1043 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1044 ;
1045
1046FPredicates
1047 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1048 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1049 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1050 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1051 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1052 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1053 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1054 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1055 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1056 ;
Chris Lattner00950542001-06-06 20:29:01 +00001057
Chris Lattnere98dda62001-07-14 06:10:16 +00001058// These are some types that allow classification if we only want a particular
1059// thing... for example, only a signed, unsigned, or integral type.
Reid Spencera54b7cb2007-01-12 07:05:14 +00001060IntType : INTTYPE;
Chris Lattner51727be2002-06-04 21:58:56 +00001061FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +00001062
Reid Spencerb2d17862007-01-26 08:04:51 +00001063LocalName : LOCALVAR | STRINGCONSTANT;
1064OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1065
1066/// OptLocalAssign - Value producing statements have an optional assignment
1067/// component.
1068OptLocalAssign : LocalName '=' {
1069 $$ = $1;
1070 CHECK_FOR_ERROR
1071 }
1072 | /*empty*/ {
1073 $$ = 0;
1074 CHECK_FOR_ERROR
1075 };
1076
1077GlobalName : GLOBALVAR | ATSTRINGCONSTANT;
1078
1079OptGlobalAssign : GlobalName '=' {
Chris Lattner00950542001-06-06 20:29:01 +00001080 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001081 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001082 }
Misha Brukman5a2a3822005-05-10 22:02:28 +00001083 | /*empty*/ {
1084 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001085 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001086 };
Chris Lattner00950542001-06-06 20:29:01 +00001087
Reid Spencerb951bc02006-12-29 20:29:48 +00001088GVInternalLinkage
1089 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1090 | WEAK { $$ = GlobalValue::WeakLinkage; }
1091 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1092 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1093 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1094 ;
1095
1096GVExternalLinkage
1097 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1098 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1099 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1100 ;
1101
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001102GVVisibilityStyle
1103 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1104 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1105 ;
1106
Reid Spencere1553cc2006-12-31 05:40:12 +00001107FunctionDeclareLinkage
1108 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1109 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1110 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001111 ;
1112
Reid Spencere1553cc2006-12-31 05:40:12 +00001113FunctionDefineLinkage
1114 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1115 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001116 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1117 | WEAK { $$ = GlobalValue::WeakLinkage; }
1118 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencerb951bc02006-12-29 20:29:48 +00001119 ;
Chris Lattner30c89792001-09-07 16:35:17 +00001120
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001121OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1122 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001123 FASTCC_TOK { $$ = CallingConv::Fast; } |
1124 COLDCC_TOK { $$ = CallingConv::Cold; } |
1125 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1126 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1127 CC_TOK EUINT64VAL {
Chris Lattnera8e8f162005-05-06 20:27:19 +00001128 if ((unsigned)$2 != $2)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001129 GEN_ERROR("Calling conv too large");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001130 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001131 CHECK_FOR_ERROR
Chris Lattnera8e8f162005-05-06 20:27:19 +00001132 };
1133
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001134ParamAttr : ZEXT { $$ = FunctionType::ZExtAttribute; }
1135 | SEXT { $$ = FunctionType::SExtAttribute; }
1136 | INREG { $$ = FunctionType::InRegAttribute; }
1137 | SRET { $$ = FunctionType::StructRetAttribute; }
Reid Spencere1553cc2006-12-31 05:40:12 +00001138 ;
1139
Reid Spencer2c261782007-01-05 17:06:19 +00001140OptParamAttrs : /* empty */ { $$ = FunctionType::NoAttributeSet; }
1141 | OptParamAttrs ParamAttr {
1142 $$ = FunctionType::ParameterAttributes($1 | $2);
Reid Spencere1553cc2006-12-31 05:40:12 +00001143 }
1144 ;
1145
Reid Spencer2c261782007-01-05 17:06:19 +00001146FuncAttr : NORETURN { $$ = FunctionType::NoReturnAttribute; }
1147 | ParamAttr
1148 ;
1149
1150OptFuncAttrs : /* empty */ { $$ = FunctionType::NoAttributeSet; }
1151 | OptFuncAttrs FuncAttr {
1152 $$ = FunctionType::ParameterAttributes($1 | $2);
1153 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001154 ;
1155
Chris Lattner66db8e42005-11-06 06:34:12 +00001156// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1157// a comma before it.
Chris Lattner87ac9722005-11-06 06:46:28 +00001158OptAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001159 ALIGN EUINT64VAL {
1160 $$ = $2;
1161 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001162 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001163 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001164};
Chris Lattner66db8e42005-11-06 06:34:12 +00001165OptCAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001166 ',' ALIGN EUINT64VAL {
1167 $$ = $3;
1168 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001169 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001170 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001171};
1172
Chris Lattner66db8e42005-11-06 06:34:12 +00001173
Chris Lattner164c3782005-11-12 00:11:10 +00001174SectionString : SECTION STRINGCONSTANT {
1175 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1176 if ($2[i] == '"' || $2[i] == '\\')
Reid Spencerf4fa5902007-02-05 10:16:10 +00001177 GEN_ERROR("Invalid character in section name");
Chris Lattner164c3782005-11-12 00:11:10 +00001178 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001179 CHECK_FOR_ERROR
Chris Lattner164c3782005-11-12 00:11:10 +00001180};
1181
1182OptSection : /*empty*/ { $$ = 0; } |
1183 SectionString { $$ = $1; };
Chris Lattner164c3782005-11-12 00:11:10 +00001184
Chris Lattnerb2b96672005-11-12 18:21:21 +00001185// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1186// is set to be the global we are processing.
1187//
1188GlobalVarAttributes : /* empty */ {} |
1189 ',' GlobalVarAttribute GlobalVarAttributes {};
1190GlobalVarAttribute : SectionString {
1191 CurGV->setSection($1);
1192 free($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001193 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001194 }
1195 | ALIGN EUINT64VAL {
1196 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001197 GEN_ERROR("Alignment must be a power of two");
Chris Lattnerb2b96672005-11-12 18:21:21 +00001198 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001199 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001200 };
Chris Lattner164c3782005-11-12 00:11:10 +00001201
Chris Lattner30c89792001-09-07 16:35:17 +00001202//===----------------------------------------------------------------------===//
1203// Types includes all predefined types... except void, because it can only be
Reid Spencere1553cc2006-12-31 05:40:12 +00001204// used in specific contexts (function returning void for example).
Chris Lattner30c89792001-09-07 16:35:17 +00001205
1206// Derived types are added later...
1207//
Reid Spencer8088e9d2007-01-13 05:00:20 +00001208PrimType : INTTYPE | FLOAT | DOUBLE | LABEL ;
Reid Spencere1553cc2006-12-31 05:40:12 +00001209
1210Types
1211 : OPAQUE {
Reid Spencer08de34b2006-12-03 05:45:44 +00001212 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001213 CHECK_FOR_ERROR
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001214 }
1215 | PrimType {
Reid Spencer08de34b2006-12-03 05:45:44 +00001216 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001217 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00001218 }
1219 | Types '*' { // Pointer type?
1220 if (*$1 == Type::LabelTy)
1221 GEN_ERROR("Cannot form a pointer to a basic block");
1222 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1223 delete $1;
1224 CHECK_FOR_ERROR
1225 }
1226 | SymbolicValueRef { // Named types are also simple types...
1227 const Type* tmp = getTypeVal($1);
1228 CHECK_FOR_ERROR
1229 $$ = new PATypeHolder(tmp);
1230 }
1231 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerf4fa5902007-02-05 10:16:10 +00001232 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattner30c89792001-09-07 16:35:17 +00001233 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001234 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencer08de34b2006-12-03 05:45:44 +00001235 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001236 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001237 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001238 }
Reid Spencer2c261782007-01-05 17:06:19 +00001239 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Chris Lattner9232b992003-04-22 18:42:41 +00001240 std::vector<const Type*> Params;
Reid Spencere1553cc2006-12-31 05:40:12 +00001241 std::vector<FunctionType::ParameterAttributes> Attrs;
Reid Spencer2c261782007-01-05 17:06:19 +00001242 Attrs.push_back($5);
1243 for (TypeWithAttrsList::iterator I=$3->begin(), E=$3->end(); I != E; ++I) {
Reid Spencere1553cc2006-12-31 05:40:12 +00001244 Params.push_back(I->Ty->get());
1245 if (I->Ty->get() != Type::VoidTy)
1246 Attrs.push_back(I->Attrs);
1247 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001248 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1249 if (isVarArg) Params.pop_back();
1250
Reid Spencere1553cc2006-12-31 05:40:12 +00001251 FunctionType *FT = FunctionType::get(*$1, Params, isVarArg, Attrs);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001252 delete $3; // Delete the argument list
Reid Spencere1553cc2006-12-31 05:40:12 +00001253 delete $1; // Delete the return type handle
1254 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer61c83e02006-08-18 08:43:06 +00001255 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001256 }
Reid Spencer2c261782007-01-05 17:06:19 +00001257 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Reid Spencere1553cc2006-12-31 05:40:12 +00001258 std::vector<const Type*> Params;
1259 std::vector<FunctionType::ParameterAttributes> Attrs;
Reid Spencer2c261782007-01-05 17:06:19 +00001260 Attrs.push_back($5);
1261 for (TypeWithAttrsList::iterator I=$3->begin(), E=$3->end(); I != E; ++I) {
Reid Spencere1553cc2006-12-31 05:40:12 +00001262 Params.push_back(I->Ty->get());
1263 if (I->Ty->get() != Type::VoidTy)
1264 Attrs.push_back(I->Attrs);
1265 }
1266 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1267 if (isVarArg) Params.pop_back();
1268
1269 FunctionType *FT = FunctionType::get($1, Params, isVarArg, Attrs);
Reid Spencer2c261782007-01-05 17:06:19 +00001270 delete $3; // Delete the argument list
Reid Spencere1553cc2006-12-31 05:40:12 +00001271 $$ = new PATypeHolder(HandleUpRefs(FT));
1272 CHECK_FOR_ERROR
1273 }
1274
1275 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001276 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1277 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001278 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001279 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001280 | '<' EUINT64VAL 'x' Types '>' { // Packed array type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001281 const llvm::Type* ElemTy = $4->get();
1282 if ((unsigned)$2 != $2)
1283 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner42a75512007-01-15 02:27:26 +00001284 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencera54b7cb2007-01-12 07:05:14 +00001285 GEN_ERROR("Element type of a PackedType must be primitive");
Reid Spencer08de34b2006-12-03 05:45:44 +00001286 if (!isPowerOf2_32($2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001287 GEN_ERROR("Vector length should be a power of 2");
Reid Spencer08de34b2006-12-03 05:45:44 +00001288 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
1289 delete $4;
1290 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001291 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001292 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001293 std::vector<const Type*> Elements;
Reid Spencer08de34b2006-12-03 05:45:44 +00001294 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattnera08976b2005-02-22 23:13:58 +00001295 E = $2->end(); I != E; ++I)
Reid Spencer08de34b2006-12-03 05:45:44 +00001296 Elements.push_back(*I);
Misha Brukman5a2a3822005-05-10 22:02:28 +00001297
Reid Spencer08de34b2006-12-03 05:45:44 +00001298 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001299 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001300 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001301 }
1302 | '{' '}' { // Empty structure type?
Reid Spencer08de34b2006-12-03 05:45:44 +00001303 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001304 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001305 }
Andrew Lenharth38ecbf12006-12-08 18:06:16 +00001306 | '<' '{' TypeListI '}' '>' {
1307 std::vector<const Type*> Elements;
1308 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1309 E = $3->end(); I != E; ++I)
1310 Elements.push_back(*I);
1311
1312 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1313 delete $3;
1314 CHECK_FOR_ERROR
1315 }
1316 | '<' '{' '}' '>' { // Empty structure type?
1317 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1318 CHECK_FOR_ERROR
1319 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001320 ;
1321
1322ArgType
1323 : Types OptParamAttrs {
1324 $$.Ty = $1;
1325 $$.Attrs = $2;
1326 }
1327 ;
1328
Reid Spencer2c261782007-01-05 17:06:19 +00001329ResultTypes
1330 : Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00001331 if (!UpRefs.empty())
1332 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1333 if (!(*$1)->isFirstClassType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001334 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer2c261782007-01-05 17:06:19 +00001335 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00001336 }
Reid Spencer2c261782007-01-05 17:06:19 +00001337 | VOID {
1338 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencere1553cc2006-12-31 05:40:12 +00001339 }
1340 ;
1341
1342ArgTypeList : ArgType {
1343 $$ = new TypeWithAttrsList();
1344 $$->push_back($1);
1345 CHECK_FOR_ERROR
1346 }
1347 | ArgTypeList ',' ArgType {
1348 ($$=$1)->push_back($3);
1349 CHECK_FOR_ERROR
1350 }
1351 ;
1352
1353ArgTypeListI
1354 : ArgTypeList
1355 | ArgTypeList ',' DOTDOTDOT {
1356 $$=$1;
1357 TypeWithAttrs TWA; TWA.Attrs = FunctionType::NoAttributeSet;
1358 TWA.Ty = new PATypeHolder(Type::VoidTy);
1359 $$->push_back(TWA);
1360 CHECK_FOR_ERROR
1361 }
1362 | DOTDOTDOT {
1363 $$ = new TypeWithAttrsList;
1364 TypeWithAttrs TWA; TWA.Attrs = FunctionType::NoAttributeSet;
1365 TWA.Ty = new PATypeHolder(Type::VoidTy);
1366 $$->push_back(TWA);
1367 CHECK_FOR_ERROR
1368 }
1369 | /*empty*/ {
1370 $$ = new TypeWithAttrsList();
Reid Spencer61c83e02006-08-18 08:43:06 +00001371 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001372 };
Chris Lattner30c89792001-09-07 16:35:17 +00001373
Chris Lattner7e708292002-06-25 16:13:24 +00001374// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001375// declaration type lists
1376//
Reid Spencere1553cc2006-12-31 05:40:12 +00001377TypeListI : Types {
Reid Spencer08de34b2006-12-03 05:45:44 +00001378 $$ = new std::list<PATypeHolder>();
1379 $$->push_back(*$1); delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001380 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001381 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001382 | TypeListI ',' Types {
Reid Spencer08de34b2006-12-03 05:45:44 +00001383 ($$=$1)->push_back(*$3); delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001384 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001385 };
Chris Lattner30c89792001-09-07 16:35:17 +00001386
Chris Lattnere98dda62001-07-14 06:10:16 +00001387// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001388// production is used ONLY to represent constants that show up AFTER a 'const',
1389// 'constant' or 'global' token at global scope. Constants that can be inlined
1390// into other expressions (such as integers and constexprs) are handled by the
1391// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001392//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001393ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencere1553cc2006-12-31 05:40:12 +00001394 if (!UpRefs.empty())
1395 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001396 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001397 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001398 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001399 (*$1)->getDescription() + "'");
Chris Lattner30c89792001-09-07 16:35:17 +00001400 const Type *ETy = ATy->getElementType();
1401 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001402
Chris Lattner30c89792001-09-07 16:35:17 +00001403 // Verify that we have the correct size...
1404 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001405 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001406 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001407 itostr(NumElements) + "");
Chris Lattner00950542001-06-06 20:29:01 +00001408
Chris Lattner30c89792001-09-07 16:35:17 +00001409 // Verify all elements are correct type!
1410 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00001411 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001412 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +00001413 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer08de34b2006-12-03 05:45:44 +00001414 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001415 }
1416
Reid Spencer08de34b2006-12-03 05:45:44 +00001417 $$ = ConstantArray::get(ATy, *$3);
1418 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001419 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001420 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001421 | Types '[' ']' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001422 if (!UpRefs.empty())
1423 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001424 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001425 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001426 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001427 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001428
1429 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001430 if (NumElements != -1 && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001431 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencerf4fa5902007-02-05 10:16:10 +00001432 " arguments, but has size of " + itostr(NumElements) +"");
Reid Spencer08de34b2006-12-03 05:45:44 +00001433 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1434 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001435 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001436 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001437 | Types 'c' STRINGCONSTANT {
Reid Spencere1553cc2006-12-31 05:40:12 +00001438 if (!UpRefs.empty())
1439 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001440 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001441 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001442 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001443 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001444
Chris Lattner30c89792001-09-07 16:35:17 +00001445 int NumElements = ATy->getNumElements();
1446 const Type *ETy = ATy->getElementType();
1447 char *EndStr = UnEscapeLexed($3, true);
1448 if (NumElements != -1 && NumElements != (EndStr-$3))
Reid Spencer61c83e02006-08-18 08:43:06 +00001449 GEN_ERROR("Can't build string constant of size " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001450 itostr((int)(EndStr-$3)) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001451 " when array has size " + itostr(NumElements) + "");
Chris Lattner9232b992003-04-22 18:42:41 +00001452 std::vector<Constant*> Vals;
Reid Spencere1553cc2006-12-31 05:40:12 +00001453 if (ETy == Type::Int8Ty) {
Chris Lattneree454772006-01-23 23:05:15 +00001454 for (unsigned char *C = (unsigned char *)$3;
Reid Spencere1553cc2006-12-31 05:40:12 +00001455 C != (unsigned char*)EndStr; ++C)
1456 Vals.push_back(ConstantInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001457 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001458 free($3);
Reid Spencerf4fa5902007-02-05 10:16:10 +00001459 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner93750fa2001-07-28 17:48:55 +00001460 }
Chris Lattner30c89792001-09-07 16:35:17 +00001461 free($3);
Reid Spencer08de34b2006-12-03 05:45:44 +00001462 $$ = ConstantArray::get(ATy, Vals);
1463 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001464 CHECK_FOR_ERROR
Chris Lattner93750fa2001-07-28 17:48:55 +00001465 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001466 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencere1553cc2006-12-31 05:40:12 +00001467 if (!UpRefs.empty())
1468 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001469 const PackedType *PTy = dyn_cast<PackedType>($1->get());
Brian Gaeke715c90b2004-08-20 06:00:58 +00001470 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001471 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001472 (*$1)->getDescription() + "'");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001473 const Type *ETy = PTy->getElementType();
1474 int NumElements = PTy->getNumElements();
1475
1476 // Verify that we have the correct size...
1477 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001478 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001479 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001480 itostr(NumElements) + "");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001481
1482 // Verify all elements are correct type!
1483 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00001484 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001485 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001486 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencer08de34b2006-12-03 05:45:44 +00001487 (*$3)[i]->getType()->getDescription() + "'.");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001488 }
1489
Reid Spencer08de34b2006-12-03 05:45:44 +00001490 $$ = ConstantPacked::get(PTy, *$3);
1491 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001492 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001493 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001494 | Types '{' ConstVector '}' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001495 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001496 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001497 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001498 (*$1)->getDescription() + "'");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001499
Chris Lattnerc6212f12003-05-21 16:06:56 +00001500 if ($3->size() != STy->getNumContainedTypes())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001501 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001502
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001503 // Check to ensure that constants are compatible with the type initializer!
1504 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencer08de34b2006-12-03 05:45:44 +00001505 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001506 GEN_ERROR("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001507 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001508 "' for element #" + utostr(i) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001509 " of structure initializer");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001510
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001511 // Check to ensure that Type is not packed
1512 if (STy->isPacked())
1513 GEN_ERROR("Unpacked Initializer to packed type '" + STy->getDescription() + "'");
1514
Reid Spencer08de34b2006-12-03 05:45:44 +00001515 $$ = ConstantStruct::get(STy, *$3);
1516 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001517 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001518 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001519 | Types '{' '}' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001520 if (!UpRefs.empty())
1521 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001522 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001523 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001524 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001525 (*$1)->getDescription() + "'");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001526
1527 if (STy->getNumContainedTypes() != 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001528 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001529
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001530 // Check to ensure that Type is not packed
1531 if (STy->isPacked())
1532 GEN_ERROR("Unpacked Initializer to packed type '" + STy->getDescription() + "'");
1533
1534 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1535 delete $1;
1536 CHECK_FOR_ERROR
1537 }
1538 | Types '<' '{' ConstVector '}' '>' {
1539 const StructType *STy = dyn_cast<StructType>($1->get());
1540 if (STy == 0)
1541 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001542 (*$1)->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001543
1544 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001545 GEN_ERROR("Illegal number of initializers for structure type");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001546
1547 // Check to ensure that constants are compatible with the type initializer!
1548 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1549 if ((*$4)[i]->getType() != STy->getElementType(i))
1550 GEN_ERROR("Expected type '" +
1551 STy->getElementType(i)->getDescription() +
1552 "' for element #" + utostr(i) +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001553 " of structure initializer");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001554
1555 // Check to ensure that Type is packed
1556 if (!STy->isPacked())
1557 GEN_ERROR("Packed Initializer to unpacked type '" + STy->getDescription() + "'");
1558
1559 $$ = ConstantStruct::get(STy, *$4);
1560 delete $1; delete $4;
1561 CHECK_FOR_ERROR
1562 }
1563 | Types '<' '{' '}' '>' {
1564 if (!UpRefs.empty())
1565 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1566 const StructType *STy = dyn_cast<StructType>($1->get());
1567 if (STy == 0)
1568 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001569 (*$1)->getDescription() + "'");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001570
1571 if (STy->getNumContainedTypes() != 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001572 GEN_ERROR("Illegal number of initializers for structure type");
Andrew Lenharth1acc5a42007-01-08 18:16:47 +00001573
1574 // Check to ensure that Type is packed
1575 if (!STy->isPacked())
1576 GEN_ERROR("Packed Initializer to unpacked type '" + STy->getDescription() + "'");
1577
Reid Spencer08de34b2006-12-03 05:45:44 +00001578 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1579 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001580 CHECK_FOR_ERROR
Chris Lattnerc6212f12003-05-21 16:06:56 +00001581 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001582 | Types NULL_TOK {
Reid Spencere1553cc2006-12-31 05:40:12 +00001583 if (!UpRefs.empty())
1584 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001585 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001586 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001587 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001588 (*$1)->getDescription() + "'");
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001589
Reid Spencer08de34b2006-12-03 05:45:44 +00001590 $$ = ConstantPointerNull::get(PTy);
1591 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001592 CHECK_FOR_ERROR
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001593 }
Chris Lattner16710e92004-10-16 18:17:13 +00001594 | Types UNDEF {
Reid Spencere1553cc2006-12-31 05:40:12 +00001595 if (!UpRefs.empty())
1596 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001597 $$ = UndefValue::get($1->get());
1598 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001599 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00001600 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001601 | Types SymbolicValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00001602 if (!UpRefs.empty())
1603 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001604 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001605 if (Ty == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001606 GEN_ERROR("Global const reference must be a pointer type");
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001607
Chris Lattner3101c252002-08-15 17:58:33 +00001608 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001609 // GlobalValues whenever they refer to a variable. Because we are in
Chris Lattner3101c252002-08-15 17:58:33 +00001610 // the context of a function, getValNonImprovising will search the functions
1611 // symbol table instead of the module symbol table for the global symbol,
1612 // which throws things all off. To get around this, we just tell
1613 // getValNonImprovising that we are at global scope here.
1614 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001615 Function *SavedCurFn = CurFun.CurrentFunction;
1616 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001617
Chris Lattner2079fde2001-10-13 06:41:08 +00001618 Value *V = getValNonImprovising(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001619 CHECK_FOR_ERROR
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001620
Chris Lattner394f2eb2003-10-16 20:12:13 +00001621 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001622
Chris Lattner2079fde2001-10-13 06:41:08 +00001623 // If this is an initializer for a constant pointer, which is referencing a
1624 // (currently) undefined variable, create a stub now that shall be replaced
1625 // in the future with the right type of variable.
1626 //
1627 if (V == 0) {
Reid Spencerf4fa5902007-02-05 10:16:10 +00001628 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers");
Chris Lattner2079fde2001-10-13 06:41:08 +00001629 const PointerType *PT = cast<PointerType>(Ty);
1630
1631 // First check to see if the forward references value is already created!
1632 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001633 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001634
1635 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001636 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001637 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001638 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001639 std::string Name;
Reid Spencerb2d17862007-01-26 08:04:51 +00001640 if ($2.Type == ValID::GlobalName)
1641 Name = $2.Name;
1642 else if ($2.Type != ValID::GlobalID)
1643 GEN_ERROR("Invalid reference to global");
Chris Lattner8a327842004-07-14 21:44:00 +00001644
Reid Spencer3271ed52004-07-18 00:08:11 +00001645 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001646 GlobalValue *GV;
1647 if (const FunctionType *FTy =
1648 dyn_cast<FunctionType>(PT->getElementType())) {
1649 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1650 CurModule.CurrentModule);
1651 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001652 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattnera09000d2004-07-14 23:03:46 +00001653 GlobalValue::ExternalLinkage, 0,
1654 Name, CurModule.CurrentModule);
1655 }
Chris Lattner8a327842004-07-14 21:44:00 +00001656
Reid Spencer3271ed52004-07-18 00:08:11 +00001657 // Keep track of the fact that we have a forward ref to recycle it
1658 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1659 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001660 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001661 }
1662
Reid Spencer08de34b2006-12-03 05:45:44 +00001663 $$ = cast<GlobalValue>(V);
1664 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001665 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001666 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001667 | Types ConstExpr {
Reid Spencere1553cc2006-12-31 05:40:12 +00001668 if (!UpRefs.empty())
1669 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001670 if ($1->get() != $2->getType())
Reid Spencerb4fdfdb2007-01-04 00:05:48 +00001671 GEN_ERROR("Mismatched types for constant expression: " +
1672 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattnerd78700d2002-08-16 21:14:40 +00001673 $$ = $2;
Reid Spencer08de34b2006-12-03 05:45:44 +00001674 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001675 CHECK_FOR_ERROR
Chris Lattnerf4600482003-06-28 20:01:34 +00001676 }
1677 | Types ZEROINITIALIZER {
Reid Spencere1553cc2006-12-31 05:40:12 +00001678 if (!UpRefs.empty())
1679 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001680 const Type *Ty = $1->get();
Chris Lattner78915932004-11-28 16:45:45 +00001681 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001682 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencer08de34b2006-12-03 05:45:44 +00001683 $$ = Constant::getNullValue(Ty);
1684 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001685 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00001686 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001687 | IntType ESINT64VAL { // integral constants
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001688 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001689 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001690 $$ = ConstantInt::get($1, $2);
1691 CHECK_FOR_ERROR
1692 }
Reid Spencere1553cc2006-12-31 05:40:12 +00001693 | IntType EUINT64VAL { // integral constants
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001694 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001695 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencer9ffad0a2006-12-20 17:20:09 +00001696 $$ = ConstantInt::get($1, $2);
1697 CHECK_FOR_ERROR
1698 }
Reid Spencer8088e9d2007-01-13 05:00:20 +00001699 | INTTYPE TRUETOK { // Boolean constants
1700 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001701 $$ = ConstantInt::getTrue();
Reid Spencer61c83e02006-08-18 08:43:06 +00001702 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001703 }
Reid Spencer8088e9d2007-01-13 05:00:20 +00001704 | INTTYPE FALSETOK { // Boolean constants
1705 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001706 $$ = ConstantInt::getFalse();
Reid Spencer61c83e02006-08-18 08:43:06 +00001707 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001708 }
1709 | FPType FPVAL { // Float & Double constants
Reid Spencer08de34b2006-12-03 05:45:44 +00001710 if (!ConstantFP::isValueValidForType($1, $2))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001711 GEN_ERROR("Floating point constant invalid for type");
Reid Spencer08de34b2006-12-03 05:45:44 +00001712 $$ = ConstantFP::get($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001713 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001714 };
1715
Chris Lattner00950542001-06-06 20:29:01 +00001716
Reid Spencer3da59db2006-11-27 01:05:10 +00001717ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencere1553cc2006-12-31 05:40:12 +00001718 if (!UpRefs.empty())
1719 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00001720 Constant *Val = $3;
Reid Spencer93947c32007-01-17 02:47:33 +00001721 const Type *DestTy = $5->get();
1722 if (!CastInst::castIsValid($1, $3, DestTy))
1723 GEN_ERROR("invalid cast opcode for cast from '" +
1724 Val->getType()->getDescription() + "' to '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00001725 DestTy->getDescription() + "'");
Reid Spencer93947c32007-01-17 02:47:33 +00001726 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencer08de34b2006-12-03 05:45:44 +00001727 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001728 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001729 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001730 if (!isa<PointerType>($3->getType()))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001731 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001732
Reid Spencer08de34b2006-12-03 05:45:44 +00001733 const Type *IdxTy =
1734 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
1735 if (!IdxTy)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001736 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencer08de34b2006-12-03 05:45:44 +00001737
Chris Lattnere0135402007-01-31 04:43:46 +00001738 SmallVector<Constant*, 8> IdxVec;
Reid Spencer08de34b2006-12-03 05:45:44 +00001739 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1740 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001741 IdxVec.push_back(C);
1742 else
Reid Spencerf4fa5902007-02-05 10:16:10 +00001743 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001744
Chris Lattnerd78700d2002-08-16 21:14:40 +00001745 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001746
Chris Lattnere0135402007-01-31 04:43:46 +00001747 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer61c83e02006-08-18 08:43:06 +00001748 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001749 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001750 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer4fe16d62007-01-11 18:21:29 +00001751 if ($3->getType() != Type::Int1Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001752 GEN_ERROR("Select condition must be of boolean type");
Reid Spencer08de34b2006-12-03 05:45:44 +00001753 if ($5->getType() != $7->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001754 GEN_ERROR("Select operand types must match");
Reid Spencer08de34b2006-12-03 05:45:44 +00001755 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001756 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00001757 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001758 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001759 if ($3->getType() != $5->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001760 GEN_ERROR("Binary operator types must match");
Reid Spencer1628cec2006-10-26 06:15:43 +00001761 CHECK_FOR_ERROR;
Reid Spencerc6e956e2006-12-05 19:15:41 +00001762 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001763 }
1764 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001765 if ($3->getType() != $5->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001766 GEN_ERROR("Logical operator types must match");
Chris Lattner42a75512007-01-15 02:27:26 +00001767 if (!$3->getType()->isInteger()) {
Reid Spencer832254e2007-02-02 02:16:23 +00001768 if (Instruction::isShift($1) || !isa<PackedType>($3->getType()) ||
Chris Lattner42a75512007-01-15 02:27:26 +00001769 !cast<PackedType>($3->getType())->getElementType()->isInteger())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001770 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner0a017832005-12-21 18:31:29 +00001771 }
Reid Spencer08de34b2006-12-03 05:45:44 +00001772 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001773 CHECK_FOR_ERROR
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001774 }
Reid Spencer763ed5e2006-12-04 05:20:06 +00001775 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1776 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001777 GEN_ERROR("icmp operand types must match");
Reid Spencer763ed5e2006-12-04 05:20:06 +00001778 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencer08de34b2006-12-03 05:45:44 +00001779 }
Reid Spencer763ed5e2006-12-04 05:20:06 +00001780 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1781 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00001782 GEN_ERROR("fcmp operand types must match");
Reid Spencer763ed5e2006-12-04 05:20:06 +00001783 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencer08de34b2006-12-03 05:45:44 +00001784 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00001785 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001786 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001787 GEN_ERROR("Invalid extractelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00001788 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001789 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00001790 }
1791 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001792 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001793 GEN_ERROR("Invalid insertelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00001794 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001795 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00001796 }
1797 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer08de34b2006-12-03 05:45:44 +00001798 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerf4fa5902007-02-05 10:16:10 +00001799 GEN_ERROR("Invalid shufflevector operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00001800 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001801 CHECK_FOR_ERROR
Chris Lattner699f1eb2002-08-14 17:12:33 +00001802 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001803
Chris Lattnerca73cd82006-04-08 03:53:34 +00001804
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001805// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001806ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001807 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001808 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001809 }
1810 | ConstVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00001811 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001812 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001813 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001814 };
Chris Lattner00950542001-06-06 20:29:01 +00001815
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001816
Chris Lattner1781aca2001-09-18 04:00:54 +00001817// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001818GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001819
Chris Lattner00950542001-06-06 20:29:01 +00001820
Chris Lattner0e73ce62002-05-02 19:11:13 +00001821//===----------------------------------------------------------------------===//
1822// Rules to match Modules
1823//===----------------------------------------------------------------------===//
1824
1825// Module rule: Capture the result of parsing the whole file into a result
1826// variable...
1827//
Reid Spencerb951bc02006-12-29 20:29:48 +00001828Module
1829 : DefinitionList {
1830 $$ = ParserResult = CurModule.CurrentModule;
1831 CurModule.ModuleDone();
1832 CHECK_FOR_ERROR;
1833 }
1834 | /*empty*/ {
1835 $$ = ParserResult = CurModule.CurrentModule;
1836 CurModule.ModuleDone();
1837 CHECK_FOR_ERROR;
1838 }
1839 ;
Chris Lattner0e73ce62002-05-02 19:11:13 +00001840
Reid Spencerb951bc02006-12-29 20:29:48 +00001841DefinitionList
1842 : Definition
1843 | DefinitionList Definition
1844 ;
1845
1846Definition
Jeff Cohen361c3ef2007-01-21 19:19:31 +00001847 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001848 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00001849 CHECK_FOR_ERROR
Reid Spencerb951bc02006-12-29 20:29:48 +00001850 }
1851 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer61c83e02006-08-18 08:43:06 +00001852 CHECK_FOR_ERROR
Chris Lattner0e73ce62002-05-02 19:11:13 +00001853 }
Reid Spencerb951bc02006-12-29 20:29:48 +00001854 | MODULE ASM_TOK AsmBlock {
Reid Spencer61c83e02006-08-18 08:43:06 +00001855 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00001856 }
Reid Spencerb951bc02006-12-29 20:29:48 +00001857 | IMPLEMENTATION {
Chris Lattner355ad1f2005-03-21 06:27:42 +00001858 // Emit an error if there are any unresolved types left.
1859 if (!CurModule.LateResolveTypes.empty()) {
1860 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
Reid Spencerb2d17862007-01-26 08:04:51 +00001861 if (DID.Type == ValID::LocalName) {
Reid Spencer61c83e02006-08-18 08:43:06 +00001862 GEN_ERROR("Reference to an undefined type: '"+DID.getName() + "'");
1863 } else {
1864 GEN_ERROR("Reference to an undefined type: #" + itostr(DID.Num));
1865 }
Chris Lattner355ad1f2005-03-21 06:27:42 +00001866 }
Reid Spencer61c83e02006-08-18 08:43:06 +00001867 CHECK_FOR_ERROR
Reid Spencerb951bc02006-12-29 20:29:48 +00001868 }
Reid Spencerb2d17862007-01-26 08:04:51 +00001869 | OptLocalAssign TYPE Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00001870 if (!UpRefs.empty())
1871 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner4a42e902001-10-22 05:56:09 +00001872 // Eagerly resolve types. This is not an optimization, this is a
1873 // requirement that is due to the fact that we could have this:
1874 //
1875 // %list = type { %list * }
1876 // %list = type { %list * } ; repeated type decl
1877 //
1878 // If types are not resolved eagerly, then the two types will not be
1879 // determined to be the same type!
1880 //
Reid Spencerb951bc02006-12-29 20:29:48 +00001881 ResolveTypeTo($1, *$3);
Chris Lattner4a42e902001-10-22 05:56:09 +00001882
Reid Spencerb951bc02006-12-29 20:29:48 +00001883 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00001884 CHECK_FOR_ERROR
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001885 // If this is a named type that is not a redefinition, add it to the slot
1886 // table.
Reid Spencerb951bc02006-12-29 20:29:48 +00001887 CurModule.Types.push_back(*$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001888 }
Reid Spencer08de34b2006-12-03 05:45:44 +00001889
Reid Spencerb951bc02006-12-29 20:29:48 +00001890 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001891 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001892 }
Reid Spencerb2d17862007-01-26 08:04:51 +00001893 | OptLocalAssign TYPE VOID {
Reid Spencere1553cc2006-12-31 05:40:12 +00001894 ResolveTypeTo($1, $3);
1895
1896 if (!setTypeName($3, $1) && !$1) {
1897 CHECK_FOR_ERROR
1898 // If this is a named type that is not a redefinition, add it to the slot
1899 // table.
1900 CurModule.Types.push_back($3);
1901 }
1902 CHECK_FOR_ERROR
1903 }
Reid Spencerb2d17862007-01-26 08:04:51 +00001904 | OptGlobalAssign GVVisibilityStyle GlobalType ConstVal {
1905 /* "Externally Visible" Linkage */
Reid Spencerb951bc02006-12-29 20:29:48 +00001906 if ($4 == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001907 GEN_ERROR("Global value initializer is not a constant");
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001908 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
1909 $2, $3, $4->getType(), $4);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001910 CHECK_FOR_ERROR
Reid Spencerb951bc02006-12-29 20:29:48 +00001911 } GlobalVarAttributes {
1912 CurGV = 0;
1913 }
Reid Spencerb2d17862007-01-26 08:04:51 +00001914 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle GlobalType ConstVal {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001915 if ($5 == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001916 GEN_ERROR("Global value initializer is not a constant");
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001917 CurGV = ParseGlobalVariable($1, $2, $3, $4, $5->getType(), $5);
Reid Spencerb951bc02006-12-29 20:29:48 +00001918 CHECK_FOR_ERROR
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001919 } GlobalVarAttributes {
1920 CurGV = 0;
1921 }
Reid Spencerb2d17862007-01-26 08:04:51 +00001922 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle GlobalType Types {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001923 if (!UpRefs.empty())
1924 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
1925 CurGV = ParseGlobalVariable($1, $2, $3, $4, *$5, 0);
1926 CHECK_FOR_ERROR
1927 delete $5;
Reid Spencer5b7e7532006-09-28 19:28:24 +00001928 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001929 CurGV = 0;
1930 CHECK_FOR_ERROR
1931 }
Reid Spencerb951bc02006-12-29 20:29:48 +00001932 | TARGET TargetDefinition {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001933 CHECK_FOR_ERROR
1934 }
Reid Spencerb951bc02006-12-29 20:29:48 +00001935 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00001936 CHECK_FOR_ERROR
Chris Lattnere98dda62001-07-14 06:10:16 +00001937 }
Reid Spencerb951bc02006-12-29 20:29:48 +00001938 ;
Chris Lattner00950542001-06-06 20:29:01 +00001939
1940
Chris Lattneree454772006-01-23 23:05:15 +00001941AsmBlock : STRINGCONSTANT {
Chris Lattner66316012006-01-24 04:14:29 +00001942 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattneree454772006-01-23 23:05:15 +00001943 char *EndStr = UnEscapeLexed($1, true);
1944 std::string NewAsm($1, EndStr);
1945 free($1);
1946
1947 if (AsmSoFar.empty())
Chris Lattner66316012006-01-24 04:14:29 +00001948 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
Chris Lattneree454772006-01-23 23:05:15 +00001949 else
Chris Lattner66316012006-01-24 04:14:29 +00001950 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
Reid Spencer61c83e02006-08-18 08:43:06 +00001951 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00001952};
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001953
Reid Spencerb2d17862007-01-26 08:04:51 +00001954TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001955 CurModule.CurrentModule->setTargetTriple($3);
1956 free($3);
John Criswell2f6a8b12006-10-24 19:09:48 +00001957 }
Chris Lattner10b27112006-10-22 06:07:41 +00001958 | DATALAYOUT '=' STRINGCONSTANT {
Owen Andersoncf7ff2b2006-10-18 02:21:12 +00001959 CurModule.CurrentModule->setDataLayout($3);
1960 free($3);
Owen Andersoncf7ff2b2006-10-18 02:21:12 +00001961 };
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001962
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001963LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00001964
1965LibList : LibList ',' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001966 CurModule.CurrentModule->addLibrary($3);
1967 free($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001968 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00001969 }
1970 | STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001971 CurModule.CurrentModule->addLibrary($1);
1972 free($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001973 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00001974 }
1975 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00001976 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00001977 }
1978 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001979
Chris Lattner00950542001-06-06 20:29:01 +00001980//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001981// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001982//===----------------------------------------------------------------------===//
1983
Reid Spencerb2d17862007-01-26 08:04:51 +00001984ArgListH : ArgListH ',' Types OptParamAttrs OptLocalName {
Reid Spencere1553cc2006-12-31 05:40:12 +00001985 if (!UpRefs.empty())
1986 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
1987 if (*$3 == Type::VoidTy)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001988 GEN_ERROR("void typed arguments are invalid");
Reid Spencere1553cc2006-12-31 05:40:12 +00001989 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner69da5cf2002-10-13 20:57:00 +00001990 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00001991 $1->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00001992 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001993 }
Reid Spencerb2d17862007-01-26 08:04:51 +00001994 | Types OptParamAttrs OptLocalName {
Reid Spencere1553cc2006-12-31 05:40:12 +00001995 if (!UpRefs.empty())
1996 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1997 if (*$1 == Type::VoidTy)
Reid Spencerf4fa5902007-02-05 10:16:10 +00001998 GEN_ERROR("void typed arguments are invalid");
Reid Spencere1553cc2006-12-31 05:40:12 +00001999 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2000 $$ = new ArgListType;
2001 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002002 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002003 };
Chris Lattner00950542001-06-06 20:29:01 +00002004
2005ArgList : ArgListH {
2006 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002007 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002008 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00002009 | ArgListH ',' DOTDOTDOT {
2010 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00002011 struct ArgListEntry E;
2012 E.Ty = new PATypeHolder(Type::VoidTy);
2013 E.Name = 0;
2014 E.Attrs = FunctionType::NoAttributeSet;
2015 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002016 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002017 }
2018 | DOTDOTDOT {
Reid Spencere1553cc2006-12-31 05:40:12 +00002019 $$ = new ArgListType;
2020 struct ArgListEntry E;
2021 E.Ty = new PATypeHolder(Type::VoidTy);
2022 E.Name = 0;
2023 E.Attrs = FunctionType::NoAttributeSet;
2024 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002025 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002026 }
Chris Lattner00950542001-06-06 20:29:01 +00002027 | /* empty */ {
2028 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00002029 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002030 };
Chris Lattner00950542001-06-06 20:29:01 +00002031
Reid Spencerb2d17862007-01-26 08:04:51 +00002032FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
Reid Spencer2c261782007-01-05 17:06:19 +00002033 OptFuncAttrs OptSection OptAlign {
Chris Lattnera8e8f162005-05-06 20:27:19 +00002034 UnEscapeLexed($3);
2035 std::string FunctionName($3);
2036 free($3); // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00002037
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002038 // Check the function result for abstractness if this is a define. We should
2039 // have no abstract types at this point
Reid Spencer2c261782007-01-05 17:06:19 +00002040 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2041 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002042
Chris Lattner9232b992003-04-22 18:42:41 +00002043 std::vector<const Type*> ParamTypeList;
Reid Spencere1553cc2006-12-31 05:40:12 +00002044 std::vector<FunctionType::ParameterAttributes> ParamAttrs;
Reid Spencer2c261782007-01-05 17:06:19 +00002045 ParamAttrs.push_back($7);
Chris Lattnera8e8f162005-05-06 20:27:19 +00002046 if ($5) { // If there are arguments...
Reid Spencere1553cc2006-12-31 05:40:12 +00002047 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I) {
2048 const Type* Ty = I->Ty->get();
Reid Spencer98b3c5c2007-01-02 21:53:43 +00002049 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2050 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencere1553cc2006-12-31 05:40:12 +00002051 ParamTypeList.push_back(Ty);
2052 if (Ty != Type::VoidTy)
2053 ParamAttrs.push_back(I->Attrs);
2054 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002055 }
Chris Lattner00950542001-06-06 20:29:01 +00002056
Chris Lattner2079fde2001-10-13 06:41:08 +00002057 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2058 if (isVarArg) ParamTypeList.pop_back();
2059
Reid Spencer2c261782007-01-05 17:06:19 +00002060 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg,
Reid Spencere1553cc2006-12-31 05:40:12 +00002061 ParamAttrs);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002062 const PointerType *PFT = PointerType::get(FT);
Reid Spencer2c261782007-01-05 17:06:19 +00002063 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002064
Chris Lattnera09000d2004-07-14 23:03:46 +00002065 ValID ID;
2066 if (!FunctionName.empty()) {
Reid Spencerb2d17862007-01-26 08:04:51 +00002067 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattnera09000d2004-07-14 23:03:46 +00002068 } else {
Reid Spencerb2d17862007-01-26 08:04:51 +00002069 ID = ValID::createGlobalID(CurModule.Values[PFT].size());
Chris Lattnera09000d2004-07-14 23:03:46 +00002070 }
2071
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002072 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00002073 // See if this function was forward referenced. If so, recycle the object.
2074 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2075 // Move the function to the end of the list, from whereever it was
2076 // previously inserted.
2077 Fn = cast<Function>(FWRef);
2078 CurModule.CurrentModule->getFunctionList().remove(Fn);
2079 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2080 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
2081 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
2082 // If this is the case, either we need to be a forward decl, or it needs
2083 // to be.
Reid Spencer5cbf9852007-01-30 20:08:39 +00002084 if (!CurFun.isDeclare && !Fn->isDeclaration())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002085 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002086
Chris Lattnera09000d2004-07-14 23:03:46 +00002087 // Make sure to strip off any argument names so we can't get conflicts.
Reid Spencer5cbf9852007-01-30 20:08:39 +00002088 if (Fn->isDeclaration())
Chris Lattnere4d5c442005-03-15 04:54:21 +00002089 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattnera09000d2004-07-14 23:03:46 +00002090 AI != AE; ++AI)
2091 AI->setName("");
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002092 } else { // Not already defined?
2093 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
2094 CurModule.CurrentModule);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002095
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002096 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002097 }
Chris Lattner00950542001-06-06 20:29:01 +00002098
Chris Lattner394f2eb2003-10-16 20:12:13 +00002099 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002100
2101 if (CurFun.isDeclare) {
2102 // If we have declaration, always overwrite linkage. This will allow us to
2103 // correctly handle cases, when pointer to function is passed as argument to
2104 // another function.
2105 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002106 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002107 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002108 Fn->setCallingConv($1);
Reid Spencer2c261782007-01-05 17:06:19 +00002109 Fn->setAlignment($9);
2110 if ($8) {
2111 Fn->setSection($8);
2112 free($8);
Chris Lattner164c3782005-11-12 00:11:10 +00002113 }
Chris Lattner00950542001-06-06 20:29:01 +00002114
Chris Lattner7e708292002-06-25 16:13:24 +00002115 // Add all of the arguments we parsed to the function...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002116 if ($5) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00002117 if (isVarArg) { // Nuke the last entry
Reid Spencere1553cc2006-12-31 05:40:12 +00002118 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0&&
Reid Spencerf4fa5902007-02-05 10:16:10 +00002119 "Not a varargs marker");
Reid Spencere1553cc2006-12-31 05:40:12 +00002120 delete $5->back().Ty;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002121 $5->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00002122 }
Chris Lattnere4d5c442005-03-15 04:54:21 +00002123 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spencere1553cc2006-12-31 05:40:12 +00002124 unsigned Idx = 1;
2125 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I, ++ArgIt) {
2126 delete I->Ty; // Delete the typeholder...
2127 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002128 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002129 InsertValue(ArgIt);
Reid Spencere1553cc2006-12-31 05:40:12 +00002130 Idx++;
Chris Lattner00950542001-06-06 20:29:01 +00002131 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002132
Chris Lattnera8e8f162005-05-06 20:27:19 +00002133 delete $5; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00002134 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002135 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002136};
Chris Lattner00950542001-06-06 20:29:01 +00002137
Chris Lattner9b02cc32002-05-03 18:23:48 +00002138BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2139
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002140FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00002141 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00002142
Chris Lattner4ad02e72003-04-16 20:28:45 +00002143 // Make sure that we keep track of the linkage type even if there was a
2144 // previous "declare".
2145 $$->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002146 $$->setVisibility($2);
Chris Lattner51727be2002-06-04 21:58:56 +00002147};
Chris Lattner00950542001-06-06 20:29:01 +00002148
Chris Lattner9b02cc32002-05-03 18:23:48 +00002149END : ENDTOK | '}'; // Allow end of '}' to end a function
2150
Chris Lattner79df7c02002-03-26 18:01:55 +00002151Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00002152 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002153 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002154};
Chris Lattner00950542001-06-06 20:29:01 +00002155
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002156FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencere1553cc2006-12-31 05:40:12 +00002157 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002158 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002159 $$ = CurFun.CurrentFunction;
2160 CurFun.FunctionDone();
2161 CHECK_FOR_ERROR
2162 };
Chris Lattner00950542001-06-06 20:29:01 +00002163
2164//===----------------------------------------------------------------------===//
2165// Rules to match Basic Blocks
2166//===----------------------------------------------------------------------===//
2167
Chris Lattneraa2c8532006-01-25 22:26:43 +00002168OptSideEffect : /* empty */ {
2169 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002170 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002171 }
2172 | SIDEEFFECT {
2173 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002174 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002175 };
2176
Chris Lattner00950542001-06-06 20:29:01 +00002177ConstValueRef : ESINT64VAL { // A reference to a direct constant
2178 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002179 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002180 }
2181 | EUINT64VAL {
2182 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002183 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002184 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00002185 | FPVAL { // Perhaps it's an FP constant?
2186 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002187 CHECK_FOR_ERROR
Chris Lattner3d52b2f2001-07-15 00:17:01 +00002188 }
Chris Lattner91ef4602004-03-31 03:49:47 +00002189 | TRUETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002190 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002191 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002192 }
Chris Lattner91ef4602004-03-31 03:49:47 +00002193 | FALSETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002194 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002195 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002196 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00002197 | NULL_TOK {
2198 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002199 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002200 }
Chris Lattner16710e92004-10-16 18:17:13 +00002201 | UNDEF {
2202 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002203 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00002204 }
Chris Lattnerf1f03df2005-12-21 17:53:02 +00002205 | ZEROINITIALIZER { // A vector zero constant.
2206 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002207 CHECK_FOR_ERROR
Chris Lattnerf1f03df2005-12-21 17:53:02 +00002208 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00002209 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencer08de34b2006-12-03 05:45:44 +00002210 const Type *ETy = (*$2)[0]->getType();
Brian Gaeke715c90b2004-08-20 06:00:58 +00002211 int NumElements = $2->size();
2212
2213 PackedType* pt = PackedType::get(ETy, NumElements);
2214 PATypeHolder* PTy = new PATypeHolder(
Reid Spencer08de34b2006-12-03 05:45:44 +00002215 HandleUpRefs(
2216 PackedType::get(
2217 ETy,
2218 NumElements)
2219 )
2220 );
Brian Gaeke715c90b2004-08-20 06:00:58 +00002221
2222 // Verify all elements are correct type!
2223 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencer08de34b2006-12-03 05:45:44 +00002224 if (ETy != (*$2)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002225 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00002226 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencer08de34b2006-12-03 05:45:44 +00002227 (*$2)[i]->getType()->getDescription() + "'.");
Brian Gaeke715c90b2004-08-20 06:00:58 +00002228 }
2229
Reid Spencer08de34b2006-12-03 05:45:44 +00002230 $$ = ValID::create(ConstantPacked::get(pt, *$2));
Brian Gaeke715c90b2004-08-20 06:00:58 +00002231 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002232 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00002233 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00002234 | ConstExpr {
Reid Spencer08de34b2006-12-03 05:45:44 +00002235 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002236 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002237 }
2238 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
2239 char *End = UnEscapeLexed($3, true);
2240 std::string AsmStr = std::string($3, End);
2241 End = UnEscapeLexed($5, true);
2242 std::string Constraints = std::string($5, End);
2243 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
2244 free($3);
2245 free($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002246 CHECK_FOR_ERROR
Chris Lattnerd78700d2002-08-16 21:14:40 +00002247 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00002248
Chris Lattner2079fde2001-10-13 06:41:08 +00002249// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2250// another value.
2251//
Reid Spencerb2d17862007-01-26 08:04:51 +00002252SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2253 $$ = ValID::createLocalID($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002254 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002255 }
Reid Spencerb2d17862007-01-26 08:04:51 +00002256 | GLOBALVAL_ID {
2257 $$ = ValID::createGlobalID($1);
2258 CHECK_FOR_ERROR
2259 }
2260 | LocalName { // Is it a named reference...?
2261 $$ = ValID::createLocalName($1);
2262 CHECK_FOR_ERROR
2263 }
2264 | GlobalName { // Is it a named reference...?
2265 $$ = ValID::createGlobalName($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002266 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002267 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002268
2269// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00002270ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00002271
Chris Lattner00950542001-06-06 20:29:01 +00002272
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002273// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2274// type immediately preceeds the value reference, and allows complex constant
2275// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00002276ResolvedVal : Types ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002277 if (!UpRefs.empty())
2278 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2279 $$ = getVal(*$1, $2);
2280 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002281 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00002282 }
2283 ;
Chris Lattner8b81bf52001-07-25 22:47:46 +00002284
Chris Lattner00950542001-06-06 20:29:01 +00002285BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002286 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002287 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002288 }
Chris Lattner7e708292002-06-25 16:13:24 +00002289 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00002290 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002291 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002292 };
Chris Lattner00950542001-06-06 20:29:01 +00002293
2294
2295// Basic blocks are terminated by branching instructions:
2296// br, br/cc, switch, ret
2297//
Reid Spencerb2d17862007-01-26 08:04:51 +00002298BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002299 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002300 CHECK_FOR_ERROR
Chris Lattner2079fde2001-10-13 06:41:08 +00002301 InsertValue($3);
2302
2303 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00002304 InsertValue($1);
2305 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002306 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002307 };
Chris Lattner00950542001-06-06 20:29:01 +00002308
2309InstructionList : InstructionList Inst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002310 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2311 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2312 if (CI2->getParent() == 0)
2313 $1->getInstList().push_back(CI2);
Chris Lattner00950542001-06-06 20:29:01 +00002314 $1->getInstList().push_back($2);
2315 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002316 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002317 }
2318 | /* empty */ {
Reid Spencerb2d17862007-01-26 08:04:51 +00002319 $$ = getBBVal(ValID::createLocalID(CurFun.NextBBNum++), true);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002320 CHECK_FOR_ERROR
Chris Lattner8d65a062004-07-26 01:40:20 +00002321
2322 // Make sure to move the basic block to the correct location in the
2323 // function, instead of leaving it inserted wherever it was first
2324 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00002325 Function::BasicBlockListType &BBL =
2326 CurFun.CurrentFunction->getBasicBlockList();
2327 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer61c83e02006-08-18 08:43:06 +00002328 CHECK_FOR_ERROR
Chris Lattner22ae2322004-07-13 08:39:15 +00002329 }
2330 | LABELSTR {
Reid Spencerb2d17862007-01-26 08:04:51 +00002331 $$ = getBBVal(ValID::createLocalName($1), true);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002332 CHECK_FOR_ERROR
Chris Lattner8d65a062004-07-26 01:40:20 +00002333
2334 // Make sure to move the basic block to the correct location in the
2335 // function, instead of leaving it inserted wherever it was first
2336 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00002337 Function::BasicBlockListType &BBL =
2338 CurFun.CurrentFunction->getBasicBlockList();
2339 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer61c83e02006-08-18 08:43:06 +00002340 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002341 };
Chris Lattner00950542001-06-06 20:29:01 +00002342
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002343BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencer08de34b2006-12-03 05:45:44 +00002344 $$ = new ReturnInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002345 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002346 }
2347 | RET VOID { // Return with no result...
2348 $$ = new ReturnInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002349 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002350 }
2351 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002352 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002353 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002354 $$ = new BranchInst(tmpBB);
Chris Lattner00950542001-06-06 20:29:01 +00002355 } // Conditional Branch...
Reid Spencer8088e9d2007-01-13 05:00:20 +00002356 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
2357 assert(cast<IntegerType>($2)->getBitWidth() == 1 && "Not Bool?");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002358 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002359 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002360 BasicBlock* tmpBBB = getBBVal($9);
2361 CHECK_FOR_ERROR
Reid Spencer4fe16d62007-01-11 18:21:29 +00002362 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002363 CHECK_FOR_ERROR
2364 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattner00950542001-06-06 20:29:01 +00002365 }
2366 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002367 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002368 CHECK_FOR_ERROR
2369 BasicBlock* tmpBB = getBBVal($6);
2370 CHECK_FOR_ERROR
2371 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00002372 $$ = S;
2373
Chris Lattner9232b992003-04-22 18:42:41 +00002374 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00002375 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00002376 for (; I != E; ++I) {
2377 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2378 S->addCase(CI, I->second);
2379 else
Reid Spencerf4fa5902007-02-05 10:16:10 +00002380 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattner69331f52005-02-24 05:25:17 +00002381 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00002382 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002383 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002384 }
Chris Lattner196850c2003-05-15 21:30:00 +00002385 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencer08de34b2006-12-03 05:45:44 +00002386 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002387 CHECK_FOR_ERROR
2388 BasicBlock* tmpBB = getBBVal($6);
2389 CHECK_FOR_ERROR
2390 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattner196850c2003-05-15 21:30:00 +00002391 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002392 CHECK_FOR_ERROR
Chris Lattner196850c2003-05-15 21:30:00 +00002393 }
Reid Spencer2c261782007-01-05 17:06:19 +00002394 | INVOKE OptCallingConv ResultTypes ValueRef '(' ValueRefList ')' OptFuncAttrs
Chris Lattnera8e8f162005-05-06 20:27:19 +00002395 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattner2079fde2001-10-13 06:41:08 +00002396
Reid Spencere1553cc2006-12-31 05:40:12 +00002397 // Handle the short syntax
2398 const PointerType *PFTy = 0;
2399 const FunctionType *Ty = 0;
Reid Spencer2c261782007-01-05 17:06:19 +00002400 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002401 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00002402 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002403 std::vector<const Type*> ParamTypes;
Reid Spencere1553cc2006-12-31 05:40:12 +00002404 FunctionType::ParamAttrsList ParamAttrs;
Reid Spencer2c261782007-01-05 17:06:19 +00002405 ParamAttrs.push_back($8);
Reid Spencere1553cc2006-12-31 05:40:12 +00002406 for (ValueRefList::iterator I = $6->begin(), E = $6->end(); I != E; ++I) {
2407 const Type *Ty = I->Val->getType();
2408 if (Ty == Type::VoidTy)
2409 GEN_ERROR("Short call syntax cannot be used with varargs");
2410 ParamTypes.push_back(Ty);
2411 ParamAttrs.push_back(I->Attrs);
Chris Lattner2079fde2001-10-13 06:41:08 +00002412 }
2413
Reid Spencer2c261782007-01-05 17:06:19 +00002414 Ty = FunctionType::get($3->get(), ParamTypes, false, ParamAttrs);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002415 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00002416 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002417
Chris Lattnera8e8f162005-05-06 20:27:19 +00002418 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002419 CHECK_FOR_ERROR
Reid Spencer2c261782007-01-05 17:06:19 +00002420 BasicBlock *Normal = getBBVal($11);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002421 CHECK_FOR_ERROR
Reid Spencer2c261782007-01-05 17:06:19 +00002422 BasicBlock *Except = getBBVal($14);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002423 CHECK_FOR_ERROR
Chris Lattner2079fde2001-10-13 06:41:08 +00002424
Reid Spencere1553cc2006-12-31 05:40:12 +00002425 // Check the arguments
2426 ValueList Args;
2427 if ($6->empty()) { // Has no arguments?
2428 // Make sure no arguments is a good thing!
2429 if (Ty->getNumParams() != 0)
2430 GEN_ERROR("No arguments passed to a function that "
Reid Spencerf4fa5902007-02-05 10:16:10 +00002431 "expects arguments");
Chris Lattner2079fde2001-10-13 06:41:08 +00002432 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002433 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00002434 // correctly!
Chris Lattnerd5d89962004-02-09 04:14:01 +00002435 FunctionType::param_iterator I = Ty->param_begin();
2436 FunctionType::param_iterator E = Ty->param_end();
Reid Spencere1553cc2006-12-31 05:40:12 +00002437 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00002438
Reid Spencere1553cc2006-12-31 05:40:12 +00002439 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2440 if (ArgI->Val->getType() != *I)
2441 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002442 (*I)->getDescription() + "'");
Reid Spencere1553cc2006-12-31 05:40:12 +00002443 Args.push_back(ArgI->Val);
2444 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002445
Reid Spencere1553cc2006-12-31 05:40:12 +00002446 if (Ty->isVarArg()) {
2447 if (I == E)
2448 for (; ArgI != ArgE; ++ArgI)
2449 Args.push_back(ArgI->Val); // push the remaining varargs
2450 } else if (I != E || ArgI != ArgE)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002451 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner2079fde2001-10-13 06:41:08 +00002452 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002453
2454 // Create the InvokeInst
2455 InvokeInst *II = new InvokeInst(V, Normal, Except, Args);
2456 II->setCallingConv($2);
2457 $$ = II;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002458 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002459 CHECK_FOR_ERROR
Chris Lattner36143fc2003-09-08 18:54:55 +00002460 }
2461 | UNWIND {
2462 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002463 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00002464 }
2465 | UNREACHABLE {
2466 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002467 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002468 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002469
2470
Chris Lattner00950542001-06-06 20:29:01 +00002471
2472JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2473 $$ = $1;
Reid Spencer08de34b2006-12-03 05:45:44 +00002474 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002475 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002476 if (V == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002477 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner00950542001-06-06 20:29:01 +00002478
Reid Spencer5b7e7532006-09-28 19:28:24 +00002479 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002480 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002481 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner00950542001-06-06 20:29:01 +00002482 }
2483 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00002484 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencer08de34b2006-12-03 05:45:44 +00002485 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002486 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002487
2488 if (V == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002489 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner00950542001-06-06 20:29:01 +00002490
Reid Spencer5b7e7532006-09-28 19:28:24 +00002491 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002492 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002493 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner51727be2002-06-04 21:58:56 +00002494 };
Chris Lattner00950542001-06-06 20:29:01 +00002495
Reid Spencerb2d17862007-01-26 08:04:51 +00002496Inst : OptLocalAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00002497 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00002498 setValueName($2, $1);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002499 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002500 InsertValue($2);
2501 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002502 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002503};
Chris Lattner00950542001-06-06 20:29:01 +00002504
Chris Lattnerc24d2082001-06-11 15:04:20 +00002505PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencere1553cc2006-12-31 05:40:12 +00002506 if (!UpRefs.empty())
2507 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner9232b992003-04-22 18:42:41 +00002508 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencer08de34b2006-12-03 05:45:44 +00002509 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002510 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002511 BasicBlock* tmpBB = getBBVal($5);
2512 CHECK_FOR_ERROR
2513 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencer08de34b2006-12-03 05:45:44 +00002514 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00002515 }
2516 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2517 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002518 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002519 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002520 BasicBlock* tmpBB = getBBVal($6);
2521 CHECK_FOR_ERROR
2522 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner51727be2002-06-04 21:58:56 +00002523 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00002524
2525
Reid Spencere1553cc2006-12-31 05:40:12 +00002526ValueRefList : Types ValueRef OptParamAttrs {
2527 if (!UpRefs.empty())
2528 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2529 // Used for call and invoke instructions
2530 $$ = new ValueRefList();
2531 ValueRefListEntry E; E.Attrs = $3; E.Val = getVal($1->get(), $2);
2532 $$->push_back(E);
Chris Lattner00950542001-06-06 20:29:01 +00002533 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002534 | ValueRefList ',' Types ValueRef OptParamAttrs {
2535 if (!UpRefs.empty())
2536 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner00950542001-06-06 20:29:01 +00002537 $$ = $1;
Reid Spencere1553cc2006-12-31 05:40:12 +00002538 ValueRefListEntry E; E.Attrs = $5; E.Val = getVal($3->get(), $4);
2539 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002540 CHECK_FOR_ERROR
Reid Spencere1553cc2006-12-31 05:40:12 +00002541 }
2542 | /*empty*/ { $$ = new ValueRefList(); };
Chris Lattner00950542001-06-06 20:29:01 +00002543
Reid Spencere1553cc2006-12-31 05:40:12 +00002544IndexList // Used for gep instructions and constant expressions
Reid Spencere03969f2006-12-31 21:46:36 +00002545 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencere1553cc2006-12-31 05:40:12 +00002546 | IndexList ',' ResolvedVal {
2547 $$ = $1;
2548 $$->push_back($3);
2549 CHECK_FOR_ERROR
2550 }
Reid Spencer71305d72006-12-31 21:25:25 +00002551 ;
Chris Lattner00950542001-06-06 20:29:01 +00002552
Chris Lattnerddb6db42005-05-06 05:51:46 +00002553OptTailCall : TAIL CALL {
2554 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002555 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002556 }
2557 | CALL {
2558 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002559 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002560 };
2561
Chris Lattner4a6482b2002-09-10 19:57:26 +00002562InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002563 if (!UpRefs.empty())
2564 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002565 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencer08de34b2006-12-03 05:45:44 +00002566 !isa<PackedType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002567 GEN_ERROR(
Reid Spencerf4fa5902007-02-05 10:16:10 +00002568 "Arithmetic operator requires integer, FP, or packed operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002569 if (isa<PackedType>((*$2).get()) &&
2570 ($1 == Instruction::URem ||
2571 $1 == Instruction::SRem ||
2572 $1 == Instruction::FRem))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002573 GEN_ERROR("U/S/FRem not supported on packed types");
Reid Spencer08de34b2006-12-03 05:45:44 +00002574 Value* val1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002575 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002576 Value* val2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002577 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002578 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattner4a6482b2002-09-10 19:57:26 +00002579 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002580 GEN_ERROR("binary operator returned null");
Reid Spencer08de34b2006-12-03 05:45:44 +00002581 delete $2;
Chris Lattner4a6482b2002-09-10 19:57:26 +00002582 }
2583 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002584 if (!UpRefs.empty())
2585 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002586 if (!(*$2)->isInteger()) {
Reid Spencer832254e2007-02-02 02:16:23 +00002587 if (Instruction::isShift($1) || !isa<PackedType>($2->get()) ||
Chris Lattner42a75512007-01-15 02:27:26 +00002588 !cast<PackedType>($2->get())->getElementType()->isInteger())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002589 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner0a017832005-12-21 18:31:29 +00002590 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002591 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002592 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002593 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002594 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002595 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattner4a6482b2002-09-10 19:57:26 +00002596 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002597 GEN_ERROR("binary operator returned null");
Reid Spencer08de34b2006-12-03 05:45:44 +00002598 delete $2;
Chris Lattner4a6482b2002-09-10 19:57:26 +00002599 }
Reid Spencer08de34b2006-12-03 05:45:44 +00002600 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002601 if (!UpRefs.empty())
2602 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer539b4712007-01-04 02:57:22 +00002603 if (isa<PackedType>((*$3).get()))
2604 GEN_ERROR("Packed types not supported by icmp instruction");
Reid Spencer08de34b2006-12-03 05:45:44 +00002605 Value* tmpVal1 = getVal(*$3, $4);
2606 CHECK_FOR_ERROR
2607 Value* tmpVal2 = getVal(*$3, $6);
2608 CHECK_FOR_ERROR
2609 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2610 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002611 GEN_ERROR("icmp operator returned null");
Reid Spencer08de34b2006-12-03 05:45:44 +00002612 }
2613 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002614 if (!UpRefs.empty())
2615 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer539b4712007-01-04 02:57:22 +00002616 if (isa<PackedType>((*$3).get()))
2617 GEN_ERROR("Packed types not supported by fcmp instruction");
Reid Spencer08de34b2006-12-03 05:45:44 +00002618 Value* tmpVal1 = getVal(*$3, $4);
2619 CHECK_FOR_ERROR
2620 Value* tmpVal2 = getVal(*$3, $6);
2621 CHECK_FOR_ERROR
2622 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2623 if ($$ == 0)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002624 GEN_ERROR("fcmp operator returned null");
Chris Lattner00950542001-06-06 20:29:01 +00002625 }
Reid Spencer3da59db2006-11-27 01:05:10 +00002626 | CastOps ResolvedVal TO Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00002627 if (!UpRefs.empty())
2628 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002629 Value* Val = $2;
Reid Spencer93947c32007-01-17 02:47:33 +00002630 const Type* DestTy = $4->get();
2631 if (!CastInst::castIsValid($1, Val, DestTy))
2632 GEN_ERROR("invalid cast opcode for cast from '" +
2633 Val->getType()->getDescription() + "' to '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002634 DestTy->getDescription() + "'");
Reid Spencer93947c32007-01-17 02:47:33 +00002635 $$ = CastInst::create($1, Val, DestTy);
Reid Spencer08de34b2006-12-03 05:45:44 +00002636 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00002637 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00002638 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer4fe16d62007-01-11 18:21:29 +00002639 if ($2->getType() != Type::Int1Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002640 GEN_ERROR("select condition must be boolean");
Reid Spencer08de34b2006-12-03 05:45:44 +00002641 if ($4->getType() != $6->getType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002642 GEN_ERROR("select value types should match");
Reid Spencer08de34b2006-12-03 05:45:44 +00002643 $$ = new SelectInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002644 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00002645 }
Chris Lattner99e7ab72003-10-18 05:53:13 +00002646 | VAARG ResolvedVal ',' Types {
Reid Spencere1553cc2006-12-31 05:40:12 +00002647 if (!UpRefs.empty())
2648 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002649 $$ = new VAArgInst($2, *$4);
2650 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002651 CHECK_FOR_ERROR
Chris Lattner99e7ab72003-10-18 05:53:13 +00002652 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00002653 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002654 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002655 GEN_ERROR("Invalid extractelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002656 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002657 CHECK_FOR_ERROR
Robert Bocchino9c62b562006-01-10 19:04:32 +00002658 }
Robert Bocchino2def1b32006-01-17 20:06:25 +00002659 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002660 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002661 GEN_ERROR("Invalid insertelement operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002662 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002663 CHECK_FOR_ERROR
Robert Bocchino2def1b32006-01-17 20:06:25 +00002664 }
Chris Lattner4c949082006-04-08 01:18:35 +00002665 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002666 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002667 GEN_ERROR("Invalid shufflevector operands");
Reid Spencer08de34b2006-12-03 05:45:44 +00002668 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002669 CHECK_FOR_ERROR
Chris Lattner4c949082006-04-08 01:18:35 +00002670 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00002671 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002672 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00002673 if (!Ty->isFirstClassType())
Reid Spencerf4fa5902007-02-05 10:16:10 +00002674 GEN_ERROR("PHI node operands must be of first class type");
Chris Lattnerc24d2082001-06-11 15:04:20 +00002675 $$ = new PHINode(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002676 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00002677 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002678 if ($2->front().first->getType() != Ty)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002679 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattnerb00c5822001-10-02 03:41:24 +00002680 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00002681 $2->pop_front();
2682 }
2683 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00002684 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002685 }
Reid Spencer2c261782007-01-05 17:06:19 +00002686 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ValueRefList ')'
2687 OptFuncAttrs {
Reid Spencere1553cc2006-12-31 05:40:12 +00002688
2689 // Handle the short syntax
Bill Wendlingb39a55a2006-11-12 11:10:39 +00002690 const PointerType *PFTy = 0;
2691 const FunctionType *Ty = 0;
Reid Spencer2c261782007-01-05 17:06:19 +00002692 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002693 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00002694 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002695 std::vector<const Type*> ParamTypes;
Reid Spencere1553cc2006-12-31 05:40:12 +00002696 FunctionType::ParamAttrsList ParamAttrs;
Reid Spencer2c261782007-01-05 17:06:19 +00002697 ParamAttrs.push_back($8);
Reid Spencere1553cc2006-12-31 05:40:12 +00002698 for (ValueRefList::iterator I = $6->begin(), E = $6->end(); I != E; ++I) {
2699 const Type *Ty = I->Val->getType();
2700 if (Ty == Type::VoidTy)
2701 GEN_ERROR("Short call syntax cannot be used with varargs");
2702 ParamTypes.push_back(Ty);
2703 ParamAttrs.push_back(I->Attrs);
Chris Lattneref9c23f2001-10-03 14:53:21 +00002704 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002705
Reid Spencer2c261782007-01-05 17:06:19 +00002706 Ty = FunctionType::get($3->get(), ParamTypes, false, ParamAttrs);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002707 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002708 }
Chris Lattner00950542001-06-06 20:29:01 +00002709
Chris Lattnera8e8f162005-05-06 20:27:19 +00002710 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002711 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002712
Reid Spencere1553cc2006-12-31 05:40:12 +00002713 // Check the arguments
2714 ValueList Args;
2715 if ($6->empty()) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00002716 // Make sure no arguments is a good thing!
2717 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002718 GEN_ERROR("No arguments passed to a function that "
Reid Spencerf4fa5902007-02-05 10:16:10 +00002719 "expects arguments");
Chris Lattner8b81bf52001-07-25 22:47:46 +00002720 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002721 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00002722 // correctly!
2723 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002724 FunctionType::param_iterator I = Ty->param_begin();
2725 FunctionType::param_iterator E = Ty->param_end();
Reid Spencere1553cc2006-12-31 05:40:12 +00002726 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00002727
Reid Spencere1553cc2006-12-31 05:40:12 +00002728 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2729 if (ArgI->Val->getType() != *I)
2730 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002731 (*I)->getDescription() + "'");
Reid Spencere1553cc2006-12-31 05:40:12 +00002732 Args.push_back(ArgI->Val);
2733 }
2734 if (Ty->isVarArg()) {
2735 if (I == E)
2736 for (; ArgI != ArgE; ++ArgI)
2737 Args.push_back(ArgI->Val); // push the remaining varargs
2738 } else if (I != E || ArgI != ArgE)
Reid Spencerf4fa5902007-02-05 10:16:10 +00002739 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner8b81bf52001-07-25 22:47:46 +00002740 }
Reid Spencere1553cc2006-12-31 05:40:12 +00002741 // Create the call node
2742 CallInst *CI = new CallInst(V, Args);
2743 CI->setTailCall($1);
2744 CI->setCallingConv($2);
2745 $$ = CI;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002746 delete $6;
Reid Spencerb2d17862007-01-26 08:04:51 +00002747 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002748 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002749 }
2750 | MemoryInst {
2751 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002752 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002753 };
Chris Lattner00950542001-06-06 20:29:01 +00002754
Chris Lattner15c9c032003-09-08 18:20:29 +00002755OptVolatile : VOLATILE {
2756 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002757 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00002758 }
2759 | /* empty */ {
2760 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002761 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00002762 };
2763
Chris Lattner027dcc52001-07-08 21:10:27 +00002764
Chris Lattnerddb6db42005-05-06 05:51:46 +00002765
Chris Lattner66db8e42005-11-06 06:34:12 +00002766MemoryInst : MALLOC Types OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00002767 if (!UpRefs.empty())
2768 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002769 $$ = new MallocInst(*$2, 0, $3);
2770 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002771 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002772 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00002773 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00002774 if (!UpRefs.empty())
2775 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002776 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002777 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002778 $$ = new MallocInst(*$2, tmpVal, $6);
2779 delete $2;
Nate Begeman14b05292005-11-05 09:21:28 +00002780 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002781 | ALLOCA Types OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00002782 if (!UpRefs.empty())
2783 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002784 $$ = new AllocaInst(*$2, 0, $3);
2785 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002786 CHECK_FOR_ERROR
Nate Begeman14b05292005-11-05 09:21:28 +00002787 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00002788 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencere1553cc2006-12-31 05:40:12 +00002789 if (!UpRefs.empty())
2790 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002791 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002792 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002793 $$ = new AllocaInst(*$2, tmpVal, $6);
2794 delete $2;
Nate Begeman14b05292005-11-05 09:21:28 +00002795 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002796 | FREE ResolvedVal {
Reid Spencer08de34b2006-12-03 05:45:44 +00002797 if (!isa<PointerType>($2->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002798 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002799 $2->getType()->getDescription() + "");
Reid Spencer08de34b2006-12-03 05:45:44 +00002800 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002801 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002802 }
2803
Chris Lattner15c9c032003-09-08 18:20:29 +00002804 | OptVolatile LOAD Types ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002805 if (!UpRefs.empty())
2806 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002807 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002808 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00002809 (*$3)->getDescription());
2810 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002811 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00002812 (*$3)->getDescription());
2813 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002814 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002815 $$ = new LoadInst(tmpVal, "", $1);
Reid Spencer08de34b2006-12-03 05:45:44 +00002816 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002817 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002818 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
Reid Spencere1553cc2006-12-31 05:40:12 +00002819 if (!UpRefs.empty())
2820 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002821 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002822 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00002823 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencer08de34b2006-12-03 05:45:44 +00002824 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002825 const Type *ElTy = PT->getElementType();
Reid Spencer08de34b2006-12-03 05:45:44 +00002826 if (ElTy != $3->getType())
2827 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002828 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner0383cc42002-08-21 23:51:21 +00002829
Reid Spencer08de34b2006-12-03 05:45:44 +00002830 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002831 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002832 $$ = new StoreInst($3, tmpVal, $1);
2833 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002834 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002835 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencere1553cc2006-12-31 05:40:12 +00002836 if (!UpRefs.empty())
2837 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencer08de34b2006-12-03 05:45:44 +00002838 if (!isa<PointerType>($2->get()))
Reid Spencerf4fa5902007-02-05 10:16:10 +00002839 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattner830b6f92004-04-05 01:30:04 +00002840
Reid Spencer08de34b2006-12-03 05:45:44 +00002841 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Reid Spencer61c83e02006-08-18 08:43:06 +00002842 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerf4fa5902007-02-05 10:16:10 +00002843 (*$2)->getDescription()+ "'");
Reid Spencer08de34b2006-12-03 05:45:44 +00002844 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002845 CHECK_FOR_ERROR
Reid Spencer08de34b2006-12-03 05:45:44 +00002846 $$ = new GetElementPtrInst(tmpVal, *$4);
2847 delete $2;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002848 delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002849 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002850
Brian Gaeked0fde302003-11-11 22:41:34 +00002851
Chris Lattner00950542001-06-06 20:29:01 +00002852%%
Reid Spencer61c83e02006-08-18 08:43:06 +00002853
Reid Spencere1553cc2006-12-31 05:40:12 +00002854// common code from the two 'RunVMAsmParser' functions
2855static Module* RunParser(Module * M) {
2856
2857 llvmAsmlineno = 1; // Reset the current line number...
2858 CurModule.CurrentModule = M;
2859#if YYDEBUG
2860 yydebug = Debug;
2861#endif
2862
2863 // Check to make sure the parser succeeded
2864 if (yyparse()) {
2865 if (ParserResult)
2866 delete ParserResult;
2867 return 0;
2868 }
2869
2870 // Check to make sure that parsing produced a result
2871 if (!ParserResult)
2872 return 0;
2873
2874 // Reset ParserResult variable while saving its value for the result.
2875 Module *Result = ParserResult;
2876 ParserResult = 0;
2877
2878 return Result;
2879}
2880
Reid Spencer61c83e02006-08-18 08:43:06 +00002881void llvm::GenerateError(const std::string &message, int LineNo) {
2882 if (LineNo == -1) LineNo = llvmAsmlineno;
2883 // TODO: column number in exception
2884 if (TheParseError)
2885 TheParseError->setError(CurFilename, message, LineNo);
2886 TriggerError = 1;
2887}
2888
Chris Lattner09083092001-07-08 04:57:15 +00002889int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002890 std::string where
2891 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002892 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002893 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002894 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002895 errMsg += "end-of-file.";
2896 else
Chris Lattner9232b992003-04-22 18:42:41 +00002897 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Reid Spencer61c83e02006-08-18 08:43:06 +00002898 GenerateError(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002899 return 0;
2900}