blob: 83dfa31f03a3c11174d7f0ef05834708232456b8 [file] [log] [blame]
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
2//
3// 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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the bison parser for LLVM assembly languages files.
11//
12//===----------------------------------------------------------------------===//
13
14%{
15#include "ParserInternals.h"
16#include "llvm/CallingConv.h"
17#include "llvm/InlineAsm.h"
18#include "llvm/Instructions.h"
19#include "llvm/Module.h"
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000020#include "llvm/ValueSymbolTable.h"
Chandler Carruth450f95c2007-08-04 01:56:21 +000021#include "llvm/AutoUpgrade.h"
Chris Lattnerf20e61f2006-02-15 07:22:58 +000022#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer42f0cbe2006-12-31 05:40:51 +000023#include "llvm/Support/CommandLine.h"
Chris Lattner55ce85c2007-01-31 04:44:08 +000024#include "llvm/ADT/SmallVector.h"
Chris Lattnerf20e61f2006-02-15 07:22:58 +000025#include "llvm/ADT/STLExtras.h"
26#include "llvm/Support/MathExtras.h"
Reid Spencerd5e19442006-12-01 00:33:46 +000027#include "llvm/Support/Streams.h"
Chris Lattnerf20e61f2006-02-15 07:22:58 +000028#include <algorithm>
Chris Lattnerf20e61f2006-02-15 07:22:58 +000029#include <list>
Chris Lattnerac26f382007-02-11 21:40:10 +000030#include <map>
Chris Lattnerf20e61f2006-02-15 07:22:58 +000031#include <utility>
Reid Spencer42f0cbe2006-12-31 05:40:51 +000032#ifndef NDEBUG
33#define YYDEBUG 1
34#endif
Chris Lattnerf20e61f2006-02-15 07:22:58 +000035
Reid Spencerb50974a2006-08-18 17:32:55 +000036// The following is a gross hack. In order to rid the libAsmParser library of
37// exceptions, we have to have a way of getting the yyparse function to go into
38// an error situation. So, whenever we want an error to occur, the GenerateError
39// function (see bottom of file) sets TriggerError. Then, at the end of each
40// production in the grammer we use CHECK_FOR_ERROR which will invoke YYERROR
41// (a goto) to put YACC in error state. Furthermore, several calls to
42// GenerateError are made from inside productions and they must simulate the
43// previous exception behavior by exiting the production immediately. We have
44// replaced these with the GEN_ERROR macro which calls GeneratError and then
45// immediately invokes YYERROR. This would be so much cleaner if it was a
46// recursive descent parser.
Reid Spencer713eedc2006-08-18 08:43:06 +000047static bool TriggerError = false;
Reid Spencerff359002006-10-09 17:36:59 +000048#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYABORT; } }
Reid Spencer713eedc2006-08-18 08:43:06 +000049#define GEN_ERROR(msg) { GenerateError(msg); YYERROR; }
50
Chris Lattnerf20e61f2006-02-15 07:22:58 +000051int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
52int yylex(); // declaration" of xxx warnings.
53int yyparse();
54
55namespace llvm {
56 std::string CurFilename;
Reid Spencer42f0cbe2006-12-31 05:40:51 +000057#if YYDEBUG
58static cl::opt<bool>
59Debug("debug-yacc", cl::desc("Print yacc debug state changes"),
60 cl::Hidden, cl::init(false));
61#endif
Chris Lattnerf20e61f2006-02-15 07:22:58 +000062}
63using namespace llvm;
64
65static Module *ParserResult;
66
67// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
68// relating to upreferences in the input stream.
69//
70//#define DEBUG_UPREFS 1
71#ifdef DEBUG_UPREFS
Bill Wendlingf3baad32006-12-07 01:30:32 +000072#define UR_OUT(X) cerr << X
Chris Lattnerf20e61f2006-02-15 07:22:58 +000073#else
74#define UR_OUT(X)
75#endif
76
77#define YYERROR_VERBOSE 1
78
Chris Lattnerf20e61f2006-02-15 07:22:58 +000079static GlobalVariable *CurGV;
80
81
82// This contains info used when building the body of a function. It is
83// destroyed when the function is completed.
84//
85typedef std::vector<Value *> ValueList; // Numbered defs
Reid Spencer42f0cbe2006-12-31 05:40:51 +000086
Chris Lattnerf20e61f2006-02-15 07:22:58 +000087static void
Reid Spencerd0e8d382007-03-19 18:40:50 +000088ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers=0);
Chris Lattnerf20e61f2006-02-15 07:22:58 +000089
90static struct PerModuleInfo {
91 Module *CurrentModule;
Reid Spencerd0e8d382007-03-19 18:40:50 +000092 ValueList Values; // Module level numbered definitions
93 ValueList LateResolveValues;
Reid Spencer55f1fbe2006-11-28 07:29:44 +000094 std::vector<PATypeHolder> Types;
95 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattnerf20e61f2006-02-15 07:22:58 +000096
97 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Chris Lattner7aa45902006-06-21 16:53:00 +000098 /// how they were referenced and on which line of the input they came from so
Chris Lattnerf20e61f2006-02-15 07:22:58 +000099 /// that we can resolve them later and print error messages as appropriate.
100 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
101
102 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
103 // references to global values. Global values may be referenced before they
104 // are defined, and if so, the temporary object that they represent is held
105 // here. This is used for forward references of GlobalValues.
106 //
107 typedef std::map<std::pair<const PointerType *,
108 ValID>, GlobalValue*> GlobalRefsType;
109 GlobalRefsType GlobalRefs;
110
111 void ModuleDone() {
112 // If we could not resolve some functions at function compilation time
113 // (calls to functions before they are defined), resolve them now... Types
114 // are resolved when the constant pool has been completely parsed.
115 //
116 ResolveDefinitions(LateResolveValues);
Reid Spencer309080a2006-09-28 19:28:24 +0000117 if (TriggerError)
118 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000119
120 // Check to make sure that all global value forward references have been
121 // resolved!
122 //
123 if (!GlobalRefs.empty()) {
124 std::string UndefinedReferences = "Unresolved global references exist:\n";
125
126 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
127 I != E; ++I) {
128 UndefinedReferences += " " + I->first.first->getDescription() + " " +
129 I->first.second.getName() + "\n";
130 }
Reid Spencer713eedc2006-08-18 08:43:06 +0000131 GenerateError(UndefinedReferences);
Reid Spencer309080a2006-09-28 19:28:24 +0000132 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000133 }
134
Chandler Carruth450f95c2007-08-04 01:56:21 +0000135 // Look for intrinsic functions and CallInst that need to be upgraded
136 for (Module::iterator FI = CurrentModule->begin(),
137 FE = CurrentModule->end(); FI != FE; )
138 UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove
139
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000140 Values.clear(); // Clear out function local definitions
141 Types.clear();
142 CurrentModule = 0;
143 }
144
145 // GetForwardRefForGlobal - Check to see if there is a forward reference
146 // for this global. If so, remove it from the GlobalRefs map and return it.
147 // If not, just return null.
148 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
149 // Check to see if there is a forward reference to this global variable...
150 // if there is, eliminate it and patch the reference to use the new def'n.
151 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
152 GlobalValue *Ret = 0;
153 if (I != GlobalRefs.end()) {
154 Ret = I->second;
155 GlobalRefs.erase(I);
156 }
157 return Ret;
158 }
Reid Spencer87622ae2007-01-02 21:54:12 +0000159
160 bool TypeIsUnresolved(PATypeHolder* PATy) {
161 // If it isn't abstract, its resolved
162 const Type* Ty = PATy->get();
163 if (!Ty->isAbstract())
164 return false;
165 // Traverse the type looking for abstract types. If it isn't abstract then
166 // we don't need to traverse that leg of the type.
167 std::vector<const Type*> WorkList, SeenList;
168 WorkList.push_back(Ty);
169 while (!WorkList.empty()) {
170 const Type* Ty = WorkList.back();
171 SeenList.push_back(Ty);
172 WorkList.pop_back();
173 if (const OpaqueType* OpTy = dyn_cast<OpaqueType>(Ty)) {
174 // Check to see if this is an unresolved type
175 std::map<ValID, PATypeHolder>::iterator I = LateResolveTypes.begin();
176 std::map<ValID, PATypeHolder>::iterator E = LateResolveTypes.end();
177 for ( ; I != E; ++I) {
178 if (I->second.get() == OpTy)
179 return true;
180 }
181 } else if (const SequentialType* SeqTy = dyn_cast<SequentialType>(Ty)) {
182 const Type* TheTy = SeqTy->getElementType();
183 if (TheTy->isAbstract() && TheTy != Ty) {
184 std::vector<const Type*>::iterator I = SeenList.begin(),
185 E = SeenList.end();
186 for ( ; I != E; ++I)
187 if (*I == TheTy)
188 break;
189 if (I == E)
190 WorkList.push_back(TheTy);
191 }
192 } else if (const StructType* StrTy = dyn_cast<StructType>(Ty)) {
193 for (unsigned i = 0; i < StrTy->getNumElements(); ++i) {
194 const Type* TheTy = StrTy->getElementType(i);
195 if (TheTy->isAbstract() && TheTy != Ty) {
196 std::vector<const Type*>::iterator I = SeenList.begin(),
197 E = SeenList.end();
198 for ( ; I != E; ++I)
199 if (*I == TheTy)
200 break;
201 if (I == E)
202 WorkList.push_back(TheTy);
203 }
204 }
205 }
206 }
207 return false;
208 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000209} CurModule;
210
211static struct PerFunctionInfo {
212 Function *CurrentFunction; // Pointer to current function being created
213
Reid Spencerd0e8d382007-03-19 18:40:50 +0000214 ValueList Values; // Keep track of #'d definitions
215 unsigned NextValNum;
216 ValueList LateResolveValues;
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000217 bool isDeclare; // Is this function a forward declararation?
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000218 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000219 GlobalValue::VisibilityTypes Visibility;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000220
221 /// BBForwardRefs - When we see forward references to basic blocks, keep
222 /// track of them here.
Reid Spencerd0e8d382007-03-19 18:40:50 +0000223 std::map<ValID, BasicBlock*> BBForwardRefs;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000224
225 inline PerFunctionInfo() {
226 CurrentFunction = 0;
227 isDeclare = false;
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000228 Linkage = GlobalValue::ExternalLinkage;
229 Visibility = GlobalValue::DefaultVisibility;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000230 }
231
232 inline void FunctionStart(Function *M) {
233 CurrentFunction = M;
Reid Spencerd0e8d382007-03-19 18:40:50 +0000234 NextValNum = 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000235 }
236
237 void FunctionDone() {
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000238 // Any forward referenced blocks left?
Reid Spencer309080a2006-09-28 19:28:24 +0000239 if (!BBForwardRefs.empty()) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000240 GenerateError("Undefined reference to label " +
Reid Spencerd0e8d382007-03-19 18:40:50 +0000241 BBForwardRefs.begin()->second->getName());
Reid Spencer309080a2006-09-28 19:28:24 +0000242 return;
243 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000244
245 // Resolve all forward references now.
246 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
247
248 Values.clear(); // Clear out function local definitions
Reid Spencerd0e8d382007-03-19 18:40:50 +0000249 BBForwardRefs.clear();
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000250 CurrentFunction = 0;
251 isDeclare = false;
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000252 Linkage = GlobalValue::ExternalLinkage;
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000253 Visibility = GlobalValue::DefaultVisibility;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000254 }
255} CurFun; // Info for the current function...
256
257static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
258
259
260//===----------------------------------------------------------------------===//
261// Code to handle definitions of all the types
262//===----------------------------------------------------------------------===//
263
Reid Spencerd0e8d382007-03-19 18:40:50 +0000264static void InsertValue(Value *V, ValueList &ValueTab = CurFun.Values) {
265 // Things that have names or are void typed don't get slot numbers
266 if (V->hasName() || (V->getType() == Type::VoidTy))
267 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000268
Reid Spencerd0e8d382007-03-19 18:40:50 +0000269 // In the case of function values, we have to allow for the forward reference
270 // of basic blocks, which are included in the numbering. Consequently, we keep
271 // track of the next insertion location with NextValNum. When a BB gets
272 // inserted, it could change the size of the CurFun.Values vector.
273 if (&ValueTab == &CurFun.Values) {
274 if (ValueTab.size() <= CurFun.NextValNum)
275 ValueTab.resize(CurFun.NextValNum+1);
276 ValueTab[CurFun.NextValNum++] = V;
277 return;
278 }
279 // For all other lists, its okay to just tack it on the back of the vector.
280 ValueTab.push_back(V);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000281}
282
283static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
284 switch (D.Type) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000285 case ValID::LocalID: // Is it a numbered definition?
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000286 // Module constants occupy the lowest numbered slots...
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000287 if (D.Num < CurModule.Types.size())
288 return CurModule.Types[D.Num];
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000289 break;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000290 case ValID::LocalName: // Is it a named definition?
Reid Spenceracfe6672007-05-22 18:52:55 +0000291 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.getName())) {
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000292 D.destroy(); // Free old strdup'd memory...
293 return N;
294 }
295 break;
296 default:
Reid Spencera7bb3e92007-02-05 10:18:06 +0000297 GenerateError("Internal parser error: Invalid symbol type reference");
Reid Spencer309080a2006-09-28 19:28:24 +0000298 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000299 }
300
301 // If we reached here, we referenced either a symbol that we don't know about
302 // or an id number that hasn't been read yet. We may be referencing something
303 // forward, so just create an entry to be resolved later and get to it...
304 //
305 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
306
307
308 if (inFunctionScope()) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000309 if (D.Type == ValID::LocalName) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000310 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer309080a2006-09-28 19:28:24 +0000311 return 0;
312 } else {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000313 GenerateError("Reference to an undefined type: #" + utostr(D.Num));
Reid Spencer309080a2006-09-28 19:28:24 +0000314 return 0;
315 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000316 }
317
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000318 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000319 if (I != CurModule.LateResolveTypes.end())
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000320 return I->second;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000321
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000322 Type *Typ = OpaqueType::get();
323 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
324 return Typ;
Reid Spencere2c32da2006-12-03 05:46:11 +0000325 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000326
Reid Spencerd0e8d382007-03-19 18:40:50 +0000327// getExistingVal - Look up the value specified by the provided type and
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000328// the provided ValID. If the value exists and has already been defined, return
329// it. Otherwise return null.
330//
Reid Spencerd0e8d382007-03-19 18:40:50 +0000331static Value *getExistingVal(const Type *Ty, const ValID &D) {
Reid Spencer309080a2006-09-28 19:28:24 +0000332 if (isa<FunctionType>(Ty)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000333 GenerateError("Functions are not values and "
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000334 "must be referenced as pointers");
Reid Spencer309080a2006-09-28 19:28:24 +0000335 return 0;
336 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000337
338 switch (D.Type) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000339 case ValID::LocalID: { // Is it a numbered definition?
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000340 // Check that the number is within bounds.
Reid Spencerd0e8d382007-03-19 18:40:50 +0000341 if (D.Num >= CurFun.Values.size())
342 return 0;
343 Value *Result = CurFun.Values[D.Num];
344 if (Ty != Result->getType()) {
345 GenerateError("Numbered value (%" + utostr(D.Num) + ") of type '" +
346 Result->getType()->getDescription() + "' does not match "
347 "expected type, '" + Ty->getDescription() + "'");
348 return 0;
349 }
350 return Result;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000351 }
352 case ValID::GlobalID: { // Is it a numbered definition?
Reid Spencerd0e8d382007-03-19 18:40:50 +0000353 if (D.Num >= CurModule.Values.size())
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000354 return 0;
Reid Spencerd0e8d382007-03-19 18:40:50 +0000355 Value *Result = CurModule.Values[D.Num];
356 if (Ty != Result->getType()) {
357 GenerateError("Numbered value (@" + utostr(D.Num) + ") of type '" +
358 Result->getType()->getDescription() + "' does not match "
359 "expected type, '" + Ty->getDescription() + "'");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000360 return 0;
Reid Spencerd0e8d382007-03-19 18:40:50 +0000361 }
362 return Result;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000363 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000364
365 case ValID::LocalName: { // Is it a named definition?
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000366 if (!inFunctionScope())
367 return 0;
368 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
Reid Spenceracfe6672007-05-22 18:52:55 +0000369 Value *N = SymTab.lookup(D.getName());
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000370 if (N == 0)
371 return 0;
372 if (N->getType() != Ty)
373 return 0;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000374
375 D.destroy(); // Free old strdup'd memory...
376 return N;
377 }
378 case ValID::GlobalName: { // Is it a named definition?
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000379 ValueSymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable();
Reid Spenceracfe6672007-05-22 18:52:55 +0000380 Value *N = SymTab.lookup(D.getName());
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000381 if (N == 0)
382 return 0;
383 if (N->getType() != Ty)
384 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000385
386 D.destroy(); // Free old strdup'd memory...
387 return N;
388 }
389
390 // Check to make sure that "Ty" is an integral type, and that our
391 // value will fit into the specified type...
392 case ValID::ConstSIntVal: // Is it a constant pool reference??
Reid Spencere0fc4df2006-10-20 07:07:24 +0000393 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000394 GenerateError("Signed integral constant '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000395 itostr(D.ConstPool64) + "' is invalid for type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +0000396 Ty->getDescription() + "'");
Reid Spencer309080a2006-09-28 19:28:24 +0000397 return 0;
398 }
Reid Spencera7bed60a2007-03-19 20:40:51 +0000399 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000400
401 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Reid Spencere0fc4df2006-10-20 07:07:24 +0000402 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
403 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000404 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Reid Spencera7bb3e92007-02-05 10:18:06 +0000405 "' is invalid or out of range");
Reid Spencer309080a2006-09-28 19:28:24 +0000406 return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000407 } else { // This is really a signed reference. Transmogrify.
Reid Spencera7bed60a2007-03-19 20:40:51 +0000408 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000409 }
410 } else {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000411 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000412 }
413
414 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000415 if (!ConstantFP::isValueValidForType(Ty, *D.ConstPoolFP)) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000416 GenerateError("FP constant invalid for type");
Reid Spencer309080a2006-09-28 19:28:24 +0000417 return 0;
418 }
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000419 // Lexer has no type info, so builds all FP constants as double.
420 // Fix this here.
421 if (Ty==Type::FloatTy)
422 D.ConstPoolFP->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
423 return ConstantFP::get(Ty, *D.ConstPoolFP);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000424
425 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer309080a2006-09-28 19:28:24 +0000426 if (!isa<PointerType>(Ty)) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000427 GenerateError("Cannot create a a non pointer null");
Reid Spencer309080a2006-09-28 19:28:24 +0000428 return 0;
429 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000430 return ConstantPointerNull::get(cast<PointerType>(Ty));
431
432 case ValID::ConstUndefVal: // Is it an undef value?
433 return UndefValue::get(Ty);
434
435 case ValID::ConstZeroVal: // Is it a zero value?
436 return Constant::getNullValue(Ty);
437
438 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer309080a2006-09-28 19:28:24 +0000439 if (D.ConstantValue->getType() != Ty) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000440 GenerateError("Constant expression type different from required type");
Reid Spencer309080a2006-09-28 19:28:24 +0000441 return 0;
442 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000443 return D.ConstantValue;
444
445 case ValID::InlineAsmVal: { // Inline asm expression
446 const PointerType *PTy = dyn_cast<PointerType>(Ty);
447 const FunctionType *FTy =
448 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer309080a2006-09-28 19:28:24 +0000449 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000450 GenerateError("Invalid type for asm constraint string");
Reid Spencer309080a2006-09-28 19:28:24 +0000451 return 0;
452 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000453 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
454 D.IAD->HasSideEffects);
455 D.destroy(); // Free InlineAsmDescriptor.
456 return IA;
457 }
458 default:
Reid Spencerac6bfc52007-02-05 17:04:00 +0000459 assert(0 && "Unhandled case!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000460 return 0;
461 } // End of switch
462
Reid Spencerac6bfc52007-02-05 17:04:00 +0000463 assert(0 && "Unhandled case!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000464 return 0;
465}
466
Reid Spencerd0e8d382007-03-19 18:40:50 +0000467// getVal - This function is identical to getExistingVal, except that if a
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000468// value is not already defined, it "improvises" by creating a placeholder var
469// that looks and acts just like the requested variable. When the value is
470// defined later, all uses of the placeholder variable are replaced with the
471// real thing.
472//
473static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer309080a2006-09-28 19:28:24 +0000474 if (Ty == Type::LabelTy) {
Reid Spencer713eedc2006-08-18 08:43:06 +0000475 GenerateError("Cannot use a basic block here");
Reid Spencer309080a2006-09-28 19:28:24 +0000476 return 0;
477 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000478
479 // See if the value has already been defined.
Reid Spencerd0e8d382007-03-19 18:40:50 +0000480 Value *V = getExistingVal(Ty, ID);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000481 if (V) return V;
Reid Spencer309080a2006-09-28 19:28:24 +0000482 if (TriggerError) return 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000483
Reid Spencer309080a2006-09-28 19:28:24 +0000484 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000485 GenerateError("Invalid use of a composite type");
Reid Spencer309080a2006-09-28 19:28:24 +0000486 return 0;
487 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000488
489 // If we reached here, we referenced either a symbol that we don't know about
490 // or an id number that hasn't been read yet. We may be referencing something
491 // forward, so just create an entry to be resolved later and get to it...
492 //
Anton Korobeynikov511d54f2007-04-28 13:48:45 +0000493 switch (ID.Type) {
494 case ValID::GlobalName:
Reid Spencer25d797b2007-04-28 16:07:31 +0000495 case ValID::GlobalID: {
Anton Korobeynikov511d54f2007-04-28 13:48:45 +0000496 const PointerType *PTy = dyn_cast<PointerType>(Ty);
497 if (!PTy) {
498 GenerateError("Invalid type for reference to global" );
499 return 0;
500 }
501 const Type* ElTy = PTy->getElementType();
502 if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy))
503 V = new Function(FTy, GlobalValue::ExternalLinkage);
504 else
505 V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage);
506 break;
Reid Spencer25d797b2007-04-28 16:07:31 +0000507 }
Anton Korobeynikov511d54f2007-04-28 13:48:45 +0000508 default:
509 V = new Argument(Ty);
510 }
511
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000512 // Remember where this forward reference came from. FIXME, shouldn't we try
513 // to recycle these things??
514 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
515 llvmAsmlineno)));
516
517 if (inFunctionScope())
518 InsertValue(V, CurFun.LateResolveValues);
519 else
520 InsertValue(V, CurModule.LateResolveValues);
521 return V;
522}
523
Reid Spencerd0e8d382007-03-19 18:40:50 +0000524/// defineBBVal - This is a definition of a new basic block with the specified
525/// identifier which must be the same as CurFun.NextValNum, if its numeric.
526static BasicBlock *defineBBVal(const ValID &ID) {
Reid Spencerac6bfc52007-02-05 17:04:00 +0000527 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000528
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000529 BasicBlock *BB = 0;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000530
Reid Spencerd0e8d382007-03-19 18:40:50 +0000531 // First, see if this was forward referenced
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000532
Reid Spencerd0e8d382007-03-19 18:40:50 +0000533 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
534 if (BBI != CurFun.BBForwardRefs.end()) {
535 BB = BBI->second;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000536 // The forward declaration could have been inserted anywhere in the
537 // function: insert it into the correct place now.
538 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
539 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Reid Spencerd0e8d382007-03-19 18:40:50 +0000540
Reid Spencer6fb989c2007-03-20 01:13:36 +0000541 // We're about to erase the entry, save the key so we can clean it up.
542 ValID Tmp = BBI->first;
543
Reid Spencerd0e8d382007-03-19 18:40:50 +0000544 // Erase the forward ref from the map as its no longer "forward"
545 CurFun.BBForwardRefs.erase(ID);
546
Reid Spencer6fb989c2007-03-20 01:13:36 +0000547 // The key has been removed from the map but so we don't want to leave
548 // strdup'd memory around so destroy it too.
549 Tmp.destroy();
550
Reid Spencerd0e8d382007-03-19 18:40:50 +0000551 // If its a numbered definition, bump the number and set the BB value.
552 if (ID.Type == ValID::LocalID) {
553 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
554 InsertValue(BB);
555 }
556
557 ID.destroy();
558 return BB;
559 }
560
561 // We haven't seen this BB before and its first mention is a definition.
562 // Just create it and return it.
Reid Spenceracfe6672007-05-22 18:52:55 +0000563 std::string Name (ID.Type == ValID::LocalName ? ID.getName() : "");
Reid Spencerd0e8d382007-03-19 18:40:50 +0000564 BB = new BasicBlock(Name, CurFun.CurrentFunction);
565 if (ID.Type == ValID::LocalID) {
566 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
567 InsertValue(BB);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000568 }
Reid Spencerd0e8d382007-03-19 18:40:50 +0000569
570 ID.destroy(); // Free strdup'd memory
571 return BB;
572}
573
574/// getBBVal - get an existing BB value or create a forward reference for it.
575///
576static BasicBlock *getBBVal(const ValID &ID) {
577 assert(inFunctionScope() && "Can't get basic block at global scope!");
578
579 BasicBlock *BB = 0;
580
581 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
582 if (BBI != CurFun.BBForwardRefs.end()) {
583 BB = BBI->second;
584 } if (ID.Type == ValID::LocalName) {
Reid Spenceracfe6672007-05-22 18:52:55 +0000585 std::string Name = ID.getName();
Reid Spencerd0e8d382007-03-19 18:40:50 +0000586 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
587 if (N)
588 if (N->getType()->getTypeID() == Type::LabelTyID)
589 BB = cast<BasicBlock>(N);
590 else
591 GenerateError("Reference to label '" + Name + "' is actually of type '"+
592 N->getType()->getDescription() + "'");
593 } else if (ID.Type == ValID::LocalID) {
594 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
595 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
596 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
597 else
598 GenerateError("Reference to label '%" + utostr(ID.Num) +
599 "' is actually of type '"+
600 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
601 }
602 } else {
603 GenerateError("Illegal label reference " + ID.getName());
604 return 0;
605 }
606
607 // If its already been defined, return it now.
608 if (BB) {
609 ID.destroy(); // Free strdup'd memory.
610 return BB;
611 }
612
613 // Otherwise, this block has not been seen before, create it.
614 std::string Name;
615 if (ID.Type == ValID::LocalName)
Reid Spenceracfe6672007-05-22 18:52:55 +0000616 Name = ID.getName();
Reid Spencerd0e8d382007-03-19 18:40:50 +0000617 BB = new BasicBlock(Name, CurFun.CurrentFunction);
618
619 // Insert it in the forward refs map.
620 CurFun.BBForwardRefs[ID] = BB;
621
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000622 return BB;
623}
624
625
626//===----------------------------------------------------------------------===//
627// Code to handle forward references in instructions
628//===----------------------------------------------------------------------===//
629//
630// This code handles the late binding needed with statements that reference
631// values not defined yet... for example, a forward branch, or the PHI node for
632// a loop body.
633//
634// This keeps a table (CurFun.LateResolveValues) of all such forward references
635// and back patchs after we are done.
636//
637
638// ResolveDefinitions - If we could not resolve some defs at parsing
639// time (forward branches, phi functions for loops, etc...) resolve the
640// defs now...
641//
642static void
Reid Spencerd0e8d382007-03-19 18:40:50 +0000643ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000644 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencerd0e8d382007-03-19 18:40:50 +0000645 while (!LateResolvers.empty()) {
646 Value *V = LateResolvers.back();
647 LateResolvers.pop_back();
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000648
Reid Spencerd0e8d382007-03-19 18:40:50 +0000649 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
650 CurModule.PlaceHolderInfo.find(V);
651 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000652
Reid Spencerd0e8d382007-03-19 18:40:50 +0000653 ValID &DID = PHI->second.first;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000654
Reid Spencerd0e8d382007-03-19 18:40:50 +0000655 Value *TheRealValue = getExistingVal(V->getType(), DID);
656 if (TriggerError)
657 return;
658 if (TheRealValue) {
659 V->replaceAllUsesWith(TheRealValue);
660 delete V;
661 CurModule.PlaceHolderInfo.erase(PHI);
662 } else if (FutureLateResolvers) {
663 // Functions have their unresolved items forwarded to the module late
664 // resolver table
665 InsertValue(V, *FutureLateResolvers);
666 } else {
667 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
668 GenerateError("Reference to an invalid definition: '" +DID.getName()+
669 "' of type '" + V->getType()->getDescription() + "'",
670 PHI->second.second);
Reid Spencer309080a2006-09-28 19:28:24 +0000671 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000672 } else {
Reid Spencerd0e8d382007-03-19 18:40:50 +0000673 GenerateError("Reference to an invalid definition: #" +
674 itostr(DID.Num) + " of type '" +
675 V->getType()->getDescription() + "'",
676 PHI->second.second);
677 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000678 }
679 }
680 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000681 LateResolvers.clear();
682}
683
684// ResolveTypeTo - A brand new type was just declared. This means that (if
685// name is not null) things referencing Name can be resolved. Otherwise, things
686// refering to the number can be resolved. Do this now.
687//
Reid Spenceracfe6672007-05-22 18:52:55 +0000688static void ResolveTypeTo(std::string *Name, const Type *ToTy) {
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000689 ValID D;
Reid Spenceracfe6672007-05-22 18:52:55 +0000690 if (Name)
691 D = ValID::createLocalName(*Name);
692 else
693 D = ValID::createLocalID(CurModule.Types.size());
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000694
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000695 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000696 CurModule.LateResolveTypes.find(D);
697 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencer55f1fbe2006-11-28 07:29:44 +0000698 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000699 CurModule.LateResolveTypes.erase(I);
700 }
701}
702
703// setValueName - Set the specified value to the name given. The name may be
704// null potentially, in which case this is a noop. The string passed in is
705// assumed to be a malloc'd string buffer, and is free'd by this function.
706//
Reid Spenceracfe6672007-05-22 18:52:55 +0000707static void setValueName(Value *V, std::string *NameStr) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000708 if (!NameStr) return;
Reid Spenceracfe6672007-05-22 18:52:55 +0000709 std::string Name(*NameStr); // Copy string
710 delete NameStr; // Free old string
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000711
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000712 if (V->getType() == Type::VoidTy) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000713 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000714 return;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000715 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000716
Reid Spencerac6bfc52007-02-05 17:04:00 +0000717 assert(inFunctionScope() && "Must be in function scope!");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000718 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
719 if (ST.lookup(Name)) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000720 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +0000721 V->getType()->getDescription() + "'");
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +0000722 return;
723 }
724
725 // Set the name.
726 V->setName(Name);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000727}
728
729/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
730/// this is a declaration, otherwise it is a definition.
731static GlobalVariable *
Reid Spenceracfe6672007-05-22 18:52:55 +0000732ParseGlobalVariable(std::string *NameStr,
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000733 GlobalValue::LinkageTypes Linkage,
734 GlobalValue::VisibilityTypes Visibility,
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000735 bool isConstantGlobal, const Type *Ty,
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000736 Constant *Initializer, bool IsThreadLocal) {
Reid Spencer309080a2006-09-28 19:28:24 +0000737 if (isa<FunctionType>(Ty)) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000738 GenerateError("Cannot declare global vars of function type");
Reid Spencer309080a2006-09-28 19:28:24 +0000739 return 0;
740 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000741
742 const PointerType *PTy = PointerType::get(Ty);
743
744 std::string Name;
745 if (NameStr) {
Reid Spenceracfe6672007-05-22 18:52:55 +0000746 Name = *NameStr; // Copy string
747 delete NameStr; // Free old string
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000748 }
749
750 // See if this global value was forward referenced. If so, recycle the
751 // object.
752 ValID ID;
753 if (!Name.empty()) {
Reid Spenceracfe6672007-05-22 18:52:55 +0000754 ID = ValID::createGlobalName(Name);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000755 } else {
Reid Spencerd0e8d382007-03-19 18:40:50 +0000756 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000757 }
758
759 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
760 // Move the global to the end of the list, from whereever it was
761 // previously inserted.
762 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
763 CurModule.CurrentModule->getGlobalList().remove(GV);
764 CurModule.CurrentModule->getGlobalList().push_back(GV);
765 GV->setInitializer(Initializer);
766 GV->setLinkage(Linkage);
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000767 GV->setVisibility(Visibility);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000768 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000769 GV->setThreadLocal(IsThreadLocal);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000770 InsertValue(GV, CurModule.Values);
771 return GV;
772 }
773
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000774 // If this global has a name
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000775 if (!Name.empty()) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000776 // if the global we're parsing has an initializer (is a definition) and
777 // has external linkage.
778 if (Initializer && Linkage != GlobalValue::InternalLinkage)
779 // If there is already a global with external linkage with this name
780 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
781 // If we allow this GVar to get created, it will be renamed in the
782 // symbol table because it conflicts with an existing GVar. We can't
783 // allow redefinition of GVars whose linking indicates that their name
784 // must stay the same. Issue the error.
785 GenerateError("Redefinition of global variable named '" + Name +
786 "' of type '" + Ty->getDescription() + "'");
787 return 0;
788 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000789 }
790
791 // Otherwise there is no existing GV to use, create one now.
792 GlobalVariable *GV =
793 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +0000794 CurModule.CurrentModule, IsThreadLocal);
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000795 GV->setVisibility(Visibility);
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000796 InsertValue(GV, CurModule.Values);
797 return GV;
798}
799
800// setTypeName - Set the specified type to the name given. The name may be
801// null potentially, in which case this is a noop. The string passed in is
802// assumed to be a malloc'd string buffer, and is freed by this function.
803//
804// This function returns true if the type has already been defined, but is
805// allowed to be redefined in the specified context. If the name is a new name
806// for the type plane, it is inserted and false is returned.
Reid Spenceracfe6672007-05-22 18:52:55 +0000807static bool setTypeName(const Type *T, std::string *NameStr) {
Reid Spencerac6bfc52007-02-05 17:04:00 +0000808 assert(!inFunctionScope() && "Can't give types function-local names!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000809 if (NameStr == 0) return false;
810
Reid Spenceracfe6672007-05-22 18:52:55 +0000811 std::string Name(*NameStr); // Copy string
812 delete NameStr; // Free old string
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000813
814 // We don't allow assigning names to void type
Reid Spencer309080a2006-09-28 19:28:24 +0000815 if (T == Type::VoidTy) {
Reid Spencera7bb3e92007-02-05 10:18:06 +0000816 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer309080a2006-09-28 19:28:24 +0000817 return false;
818 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000819
820 // Set the type name, checking for conflicts as we do so.
821 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
822
823 if (AlreadyExists) { // Inserting a name that is already defined???
824 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencerac6bfc52007-02-05 17:04:00 +0000825 assert(Existing && "Conflict but no matching type?!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000826
827 // There is only one case where this is allowed: when we are refining an
828 // opaque type. In this case, Existing will be an opaque type.
829 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
830 // We ARE replacing an opaque type!
831 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
832 return true;
833 }
834
835 // Otherwise, this is an attempt to redefine a type. That's okay if
836 // the redefinition is identical to the original. This will be so if
837 // Existing and T point to the same Type object. In this one case we
838 // allow the equivalent redefinition.
839 if (Existing == T) return true; // Yes, it's equal.
840
841 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer33259082007-01-05 21:51:07 +0000842 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +0000843 T->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000844 }
845
846 return false;
847}
848
849//===----------------------------------------------------------------------===//
850// Code for handling upreferences in type names...
851//
852
853// TypeContains - Returns true if Ty directly contains E in it.
854//
855static bool TypeContains(const Type *Ty, const Type *E) {
856 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
857 E) != Ty->subtype_end();
858}
859
860namespace {
861 struct UpRefRecord {
862 // NestingLevel - The number of nesting levels that need to be popped before
863 // this type is resolved.
864 unsigned NestingLevel;
865
866 // LastContainedTy - This is the type at the current binding level for the
867 // type. Every time we reduce the nesting level, this gets updated.
868 const Type *LastContainedTy;
869
870 // UpRefTy - This is the actual opaque type that the upreference is
871 // represented with.
872 OpaqueType *UpRefTy;
873
874 UpRefRecord(unsigned NL, OpaqueType *URTy)
875 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
876 };
877}
878
879// UpRefs - A list of the outstanding upreferences that need to be resolved.
880static std::vector<UpRefRecord> UpRefs;
881
882/// HandleUpRefs - Every time we finish a new layer of types, this function is
883/// called. It loops through the UpRefs vector, which is a list of the
884/// currently active types. For each type, if the up reference is contained in
885/// the newly completed type, we decrement the level count. When the level
886/// count reaches zero, the upreferenced type is the type that is passed in:
887/// thus we can complete the cycle.
888///
889static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner680aab62006-08-18 17:34:45 +0000890 // If Ty isn't abstract, or if there are no up-references in it, then there is
891 // nothing to resolve here.
892 if (!ty->isAbstract() || UpRefs.empty()) return ty;
893
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000894 PATypeHolder Ty(ty);
895 UR_OUT("Type '" << Ty->getDescription() <<
896 "' newly formed. Resolving upreferences.\n" <<
897 UpRefs.size() << " upreferences active!\n");
898
899 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
900 // to zero), we resolve them all together before we resolve them to Ty. At
901 // the end of the loop, if there is anything to resolve to Ty, it will be in
902 // this variable.
903 OpaqueType *TypeToResolve = 0;
904
905 for (unsigned i = 0; i != UpRefs.size(); ++i) {
906 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
907 << UpRefs[i].second->getDescription() << ") = "
908 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
909 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
910 // Decrement level of upreference
911 unsigned Level = --UpRefs[i].NestingLevel;
912 UpRefs[i].LastContainedTy = Ty;
913 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
914 if (Level == 0) { // Upreference should be resolved!
915 if (!TypeToResolve) {
916 TypeToResolve = UpRefs[i].UpRefTy;
917 } else {
918 UR_OUT(" * Resolving upreference for "
919 << UpRefs[i].second->getDescription() << "\n";
920 std::string OldName = UpRefs[i].UpRefTy->getDescription());
921 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
922 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
923 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
924 }
925 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
926 --i; // Do not skip the next element...
927 }
928 }
929 }
930
931 if (TypeToResolve) {
932 UR_OUT(" * Resolving upreference for "
933 << UpRefs[i].second->getDescription() << "\n";
934 std::string OldName = TypeToResolve->getDescription());
935 TypeToResolve->refineAbstractTypeTo(Ty);
936 }
937
938 return Ty;
939}
940
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000941//===----------------------------------------------------------------------===//
942// RunVMAsmParser - Define an interface to this parser
943//===----------------------------------------------------------------------===//
944//
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000945static Module* RunParser(Module * M);
946
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000947Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
948 set_scan_file(F);
949
950 CurFilename = Filename;
951 return RunParser(new Module(CurFilename));
952}
953
954Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
955 set_scan_string(AsmString);
956
957 CurFilename = "from_memory";
958 if (M == NULL) {
959 return RunParser(new Module (CurFilename));
960 } else {
961 return RunParser(M);
962 }
963}
964
965%}
966
967%union {
968 llvm::Module *ModuleVal;
969 llvm::Function *FunctionVal;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000970 llvm::BasicBlock *BasicBlockVal;
971 llvm::TerminatorInst *TermInstVal;
972 llvm::Instruction *InstVal;
Reid Spencere2c32da2006-12-03 05:46:11 +0000973 llvm::Constant *ConstVal;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000974
Reid Spencere2c32da2006-12-03 05:46:11 +0000975 const llvm::Type *PrimType;
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000976 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencere2c32da2006-12-03 05:46:11 +0000977 llvm::PATypeHolder *TypeVal;
978 llvm::Value *ValueVal;
Reid Spencere2c32da2006-12-03 05:46:11 +0000979 std::vector<llvm::Value*> *ValueList;
Reid Spencer42f0cbe2006-12-31 05:40:51 +0000980 llvm::ArgListType *ArgList;
981 llvm::TypeWithAttrs TypeWithAttrs;
982 llvm::TypeWithAttrsList *TypeWithAttrsList;
983 llvm::ValueRefList *ValueRefList;
984
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000985 // Represent the RHS of PHI node
Reid Spencere2c32da2006-12-03 05:46:11 +0000986 std::list<std::pair<llvm::Value*,
987 llvm::BasicBlock*> > *PHIList;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000988 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencere2c32da2006-12-03 05:46:11 +0000989 std::vector<llvm::Constant*> *ConstVector;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000990
991 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000992 llvm::GlobalValue::VisibilityTypes Visibility;
Reid Spencerf51a7052007-04-09 06:16:21 +0000993 uint16_t ParamAttrs;
Reid Spencerc7a686b2007-02-28 02:24:54 +0000994 llvm::APInt *APIntVal;
Chris Lattnerf20e61f2006-02-15 07:22:58 +0000995 int64_t SInt64Val;
996 uint64_t UInt64Val;
997 int SIntVal;
998 unsigned UIntVal;
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000999 llvm::APFloat *FPVal;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001000 bool BoolVal;
1001
Reid Spenceracfe6672007-05-22 18:52:55 +00001002 std::string *StrVal; // This memory must be deleted
1003 llvm::ValID ValIDVal;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001004
Reid Spencere2c32da2006-12-03 05:46:11 +00001005 llvm::Instruction::BinaryOps BinaryOpVal;
1006 llvm::Instruction::TermOps TermOpVal;
1007 llvm::Instruction::MemoryOps MemOpVal;
1008 llvm::Instruction::CastOps CastOpVal;
1009 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencere2c32da2006-12-03 05:46:11 +00001010 llvm::ICmpInst::Predicate IPredicate;
1011 llvm::FCmpInst::Predicate FPredicate;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001012}
1013
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001014%type <ModuleVal> Module
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001015%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
1016%type <BasicBlockVal> BasicBlock InstructionList
1017%type <TermInstVal> BBTerminatorInst
1018%type <InstVal> Inst InstVal MemoryInst
Anton Korobeynikov511d54f2007-04-28 13:48:45 +00001019%type <ConstVal> ConstVal ConstExpr AliaseeRef
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001020%type <ConstVector> ConstVector
1021%type <ArgList> ArgList ArgListH
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001022%type <PHIList> PHIList
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001023%type <ValueRefList> ValueRefList // For call param lists & GEP indices
1024%type <ValueList> IndexList // For GEP indices
1025%type <TypeList> TypeListI
1026%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001027%type <TypeWithAttrs> ArgType
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001028%type <JumpTable> JumpTable
1029%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00001030%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001031%type <BoolVal> OptVolatile // 'volatile' or not
1032%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1033%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001034%type <Linkage> GVInternalLinkage GVExternalLinkage
1035%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00001036%type <Linkage> AliasLinkage
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001037%type <Visibility> GVVisibilityStyle
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001038
1039// ValueRef - Unresolved reference to a definition or BB
1040%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1041%type <ValueVal> ResolvedVal // <type> <valref> pair
1042// Tokens and types for handling constant integer values
1043//
1044// ESINT64VAL - A negative number within long long range
1045%token <SInt64Val> ESINT64VAL
1046
1047// EUINT64VAL - A positive number within uns. long long range
1048%token <UInt64Val> EUINT64VAL
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001049
Reid Spencerc7a686b2007-02-28 02:24:54 +00001050// ESAPINTVAL - A negative number with arbitrary precision
1051%token <APIntVal> ESAPINTVAL
1052
1053// EUAPINTVAL - A positive number with arbitrary precision
1054%token <APIntVal> EUAPINTVAL
1055
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001056%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001057%token <FPVal> FPVAL // Float or Double constant
1058
1059// Built in types...
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001060%type <TypeVal> Types ResultTypes
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001061%type <PrimType> IntType FPType PrimType // Classifications
Reid Spencer58a8db02007-01-13 05:00:46 +00001062%token <PrimType> VOID INTTYPE
Dale Johannesenff4c3be2007-08-03 01:03:46 +00001063%token <PrimType> FLOAT DOUBLE X86_FP80 FP128 PPC_FP128 LABEL
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001064%token TYPE
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001065
Reid Spenceracfe6672007-05-22 18:52:55 +00001066
Reid Spencer21526d62007-05-19 07:22:10 +00001067%token<StrVal> LOCALVAR GLOBALVAR LABELSTR
1068%token<StrVal> STRINGCONSTANT ATSTRINGCONSTANT PCTSTRINGCONSTANT
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001069%type <StrVal> LocalName OptLocalName OptLocalAssign
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00001070%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001071%type <StrVal> OptSection SectionString
1072
Reid Spenceracfe6672007-05-22 18:52:55 +00001073%type <UIntVal> OptAlign OptCAlign
1074
Reid Spencer7ae03fc2007-04-09 01:56:05 +00001075%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00001076%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001077%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00001078%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001079%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001080%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001081%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattner7d1d0342006-10-22 06:08:13 +00001082%token DATALAYOUT
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001083%type <UIntVal> OptCallingConv
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001084%type <ParamAttrs> OptParamAttrs ParamAttr
1085%type <ParamAttrs> OptFuncAttrs FuncAttr
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001086
1087// Basic Block Terminating Operators
1088%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
1089
1090// Binary Operators
Reid Spencer266e42b2006-12-23 06:05:41 +00001091%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencerde46e482006-11-02 20:25:50 +00001092%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer2341c222007-02-02 02:16:23 +00001093%token <BinaryOpVal> SHL LSHR ASHR
1094
Reid Spencere2c32da2006-12-03 05:46:11 +00001095%token <OtherOpVal> ICMP FCMP
Reid Spencere2c32da2006-12-03 05:46:11 +00001096%type <IPredicate> IPredicates
Reid Spencere2c32da2006-12-03 05:46:11 +00001097%type <FPredicate> FPredicates
Reid Spencer1960ef32006-12-03 06:59:29 +00001098%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1099%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001100
1101// Memory Instructions
1102%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1103
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001104// Cast Operators
1105%type <CastOpVal> CastOps
1106%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1107%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1108
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001109// Other Operators
Reid Spencer2341c222007-02-02 02:16:23 +00001110%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattner9ff96a72006-04-08 01:18:56 +00001111%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001112
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001113// Function Attributes
Reid Spencer22910a02007-07-31 03:50:36 +00001114%token SIGNEXT ZEROEXT NORETURN INREG SRET NOUNWIND NOALIAS BYVAL NEST
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001115
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001116// Visibility Styles
Anton Korobeynikov39f3cff2007-04-29 18:35:00 +00001117%token DEFAULT HIDDEN PROTECTED
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001118
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001119%start Module
1120%%
1121
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001122
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001123// Operations that are notably excluded from this list include:
1124// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1125//
Reid Spencerde46e482006-11-02 20:25:50 +00001126ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer2341c222007-02-02 02:16:23 +00001127LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001128CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1129 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer2341c222007-02-02 02:16:23 +00001130
Reid Spencer1960ef32006-12-03 06:59:29 +00001131IPredicates
Reid Spencerd2e0c342006-12-04 05:24:24 +00001132 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer1960ef32006-12-03 06:59:29 +00001133 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1134 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1135 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1136 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1137 ;
1138
1139FPredicates
1140 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1141 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1142 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1143 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1144 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1145 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1146 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1147 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1148 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1149 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001150
1151// These are some types that allow classification if we only want a particular
1152// thing... for example, only a signed, unsigned, or integral type.
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001153IntType : INTTYPE;
Dale Johannesenff4c3be2007-08-03 01:03:46 +00001154FPType : FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001155
Reid Spenceracfe6672007-05-22 18:52:55 +00001156LocalName : LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001157OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1158
1159/// OptLocalAssign - Value producing statements have an optional assignment
1160/// component.
1161OptLocalAssign : LocalName '=' {
1162 $$ = $1;
1163 CHECK_FOR_ERROR
1164 }
1165 | /*empty*/ {
1166 $$ = 0;
1167 CHECK_FOR_ERROR
1168 };
1169
Reid Spenceracfe6672007-05-22 18:52:55 +00001170GlobalName : GLOBALVAR | ATSTRINGCONSTANT ;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001171
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00001172OptGlobalAssign : GlobalAssign
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001173 | /*empty*/ {
1174 $$ = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00001175 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001176 };
1177
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00001178GlobalAssign : GlobalName '=' {
1179 $$ = $1;
1180 CHECK_FOR_ERROR
Chris Lattner720367c2007-04-26 05:31:05 +00001181 };
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00001182
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001183GVInternalLinkage
1184 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1185 | WEAK { $$ = GlobalValue::WeakLinkage; }
1186 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1187 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1188 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1189 ;
1190
1191GVExternalLinkage
1192 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1193 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1194 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1195 ;
1196
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001197GVVisibilityStyle
Anton Korobeynikov39f3cff2007-04-29 18:35:00 +00001198 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1199 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1200 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1201 | PROTECTED { $$ = GlobalValue::ProtectedVisibility; }
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001202 ;
1203
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001204FunctionDeclareLinkage
1205 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1206 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1207 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001208 ;
1209
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00001210FunctionDefineLinkage
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001211 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1212 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001213 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1214 | WEAK { $$ = GlobalValue::WeakLinkage; }
1215 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00001216 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001217
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00001218AliasLinkage
1219 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1220 | WEAK { $$ = GlobalValue::WeakLinkage; }
1221 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1222 ;
1223
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +00001224OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1225 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +00001226 FASTCC_TOK { $$ = CallingConv::Fast; } |
1227 COLDCC_TOK { $$ = CallingConv::Cold; } |
1228 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1229 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1230 CC_TOK EUINT64VAL {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001231 if ((unsigned)$2 != $2)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001232 GEN_ERROR("Calling conv too large");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001233 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001234 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001235 };
1236
Reid Spencer22910a02007-07-31 03:50:36 +00001237ParamAttr : ZEROEXT { $$ = ParamAttr::ZExt; }
1238 | ZEXT { $$ = ParamAttr::ZExt; }
1239 | SIGNEXT { $$ = ParamAttr::SExt; }
Chris Lattnerf997e372007-07-05 17:26:49 +00001240 | SEXT { $$ = ParamAttr::SExt; }
1241 | INREG { $$ = ParamAttr::InReg; }
1242 | SRET { $$ = ParamAttr::StructRet; }
1243 | NOALIAS { $$ = ParamAttr::NoAlias; }
Reid Spencer22910a02007-07-31 03:50:36 +00001244 | BYVAL { $$ = ParamAttr::ByVal; }
1245 | NEST { $$ = ParamAttr::Nest; }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001246 ;
1247
Reid Spencera472f662007-04-11 02:44:20 +00001248OptParamAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001249 | OptParamAttrs ParamAttr {
Reid Spencerf51a7052007-04-09 06:16:21 +00001250 $$ = $1 | $2;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001251 }
1252 ;
1253
Reid Spencera472f662007-04-11 02:44:20 +00001254FuncAttr : NORETURN { $$ = ParamAttr::NoReturn; }
1255 | NOUNWIND { $$ = ParamAttr::NoUnwind; }
Reid Spencer22910a02007-07-31 03:50:36 +00001256 | ZEROEXT { $$ = ParamAttr::ZExt; }
1257 | SIGNEXT { $$ = ParamAttr::SExt; }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001258 ;
1259
Reid Spencera472f662007-04-11 02:44:20 +00001260OptFuncAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001261 | OptFuncAttrs FuncAttr {
Reid Spencerf51a7052007-04-09 06:16:21 +00001262 $$ = $1 | $2;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001263 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001264 ;
1265
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001266// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1267// a comma before it.
1268OptAlign : /*empty*/ { $$ = 0; } |
1269 ALIGN EUINT64VAL {
1270 $$ = $2;
1271 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001272 GEN_ERROR("Alignment must be a power of two");
Reid Spencer713eedc2006-08-18 08:43:06 +00001273 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001274};
1275OptCAlign : /*empty*/ { $$ = 0; } |
1276 ',' ALIGN EUINT64VAL {
1277 $$ = $3;
1278 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001279 GEN_ERROR("Alignment must be a power of two");
Reid Spencer713eedc2006-08-18 08:43:06 +00001280 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001281};
1282
1283
1284SectionString : SECTION STRINGCONSTANT {
Reid Spenceracfe6672007-05-22 18:52:55 +00001285 for (unsigned i = 0, e = $2->length(); i != e; ++i)
1286 if ((*$2)[i] == '"' || (*$2)[i] == '\\')
Reid Spencera7bb3e92007-02-05 10:18:06 +00001287 GEN_ERROR("Invalid character in section name");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001288 $$ = $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001289 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001290};
1291
1292OptSection : /*empty*/ { $$ = 0; } |
1293 SectionString { $$ = $1; };
1294
1295// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1296// is set to be the global we are processing.
1297//
1298GlobalVarAttributes : /* empty */ {} |
1299 ',' GlobalVarAttribute GlobalVarAttributes {};
1300GlobalVarAttribute : SectionString {
Reid Spenceracfe6672007-05-22 18:52:55 +00001301 CurGV->setSection(*$1);
1302 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001303 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001304 }
1305 | ALIGN EUINT64VAL {
1306 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001307 GEN_ERROR("Alignment must be a power of two");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001308 CurGV->setAlignment($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001309 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001310 };
1311
1312//===----------------------------------------------------------------------===//
1313// Types includes all predefined types... except void, because it can only be
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001314// used in specific contexts (function returning void for example).
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001315
1316// Derived types are added later...
1317//
Dale Johannesenff4c3be2007-08-03 01:03:46 +00001318PrimType : INTTYPE | FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80 | LABEL ;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001319
1320Types
1321 : OPAQUE {
Reid Spencere2c32da2006-12-03 05:46:11 +00001322 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer713eedc2006-08-18 08:43:06 +00001323 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001324 }
1325 | PrimType {
Reid Spencere2c32da2006-12-03 05:46:11 +00001326 $$ = new PATypeHolder($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001327 CHECK_FOR_ERROR
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001328 }
1329 | Types '*' { // Pointer type?
1330 if (*$1 == Type::LabelTy)
1331 GEN_ERROR("Cannot form a pointer to a basic block");
1332 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1333 delete $1;
1334 CHECK_FOR_ERROR
1335 }
1336 | SymbolicValueRef { // Named types are also simple types...
1337 const Type* tmp = getTypeVal($1);
1338 CHECK_FOR_ERROR
1339 $$ = new PATypeHolder(tmp);
1340 }
1341 | '\\' EUINT64VAL { // Type UpReference
Reid Spencera7bb3e92007-02-05 10:18:06 +00001342 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001343 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1344 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencere2c32da2006-12-03 05:46:11 +00001345 $$ = new PATypeHolder(OT);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001346 UR_OUT("New Upreference!\n");
Reid Spencer713eedc2006-08-18 08:43:06 +00001347 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001348 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001349 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001350 std::vector<const Type*> Params;
Christopher Lamb3f706f22007-04-22 20:09:11 +00001351 ParamAttrsVector Attrs;
1352 if ($5 != ParamAttr::None) {
1353 ParamAttrsWithIndex X; X.index = 0; X.attrs = $5;
1354 Attrs.push_back(X);
1355 }
Reid Spencerf51a7052007-04-09 06:16:21 +00001356 unsigned index = 1;
1357 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
1358 for (; I != E; ++I, ++index) {
Reid Spencer6fb989c2007-03-20 01:13:36 +00001359 const Type *Ty = I->Ty->get();
Reid Spencer6fb989c2007-03-20 01:13:36 +00001360 Params.push_back(Ty);
1361 if (Ty != Type::VoidTy)
Christopher Lamb3f706f22007-04-22 20:09:11 +00001362 if (I->Attrs != ParamAttr::None) {
1363 ParamAttrsWithIndex X; X.index = index; X.attrs = I->Attrs;
1364 Attrs.push_back(X);
1365 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001366 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001367 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1368 if (isVarArg) Params.pop_back();
1369
Reid Spencerf51a7052007-04-09 06:16:21 +00001370 ParamAttrsList *ActualAttrs = 0;
1371 if (!Attrs.empty())
Christopher Lamb3f706f22007-04-22 20:09:11 +00001372 ActualAttrs = ParamAttrsList::get(Attrs);
Reid Spencerf51a7052007-04-09 06:16:21 +00001373 FunctionType *FT = FunctionType::get(*$1, Params, isVarArg, ActualAttrs);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00001374 delete $3; // Delete the argument list
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001375 delete $1; // Delete the return type handle
1376 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer713eedc2006-08-18 08:43:06 +00001377 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001378 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001379 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001380 std::vector<const Type*> Params;
Christopher Lamb3f706f22007-04-22 20:09:11 +00001381 ParamAttrsVector Attrs;
1382 if ($5 != ParamAttr::None) {
1383 ParamAttrsWithIndex X; X.index = 0; X.attrs = $5;
1384 Attrs.push_back(X);
1385 }
Reid Spencerf51a7052007-04-09 06:16:21 +00001386 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
1387 unsigned index = 1;
1388 for ( ; I != E; ++I, ++index) {
Reid Spencer6fb989c2007-03-20 01:13:36 +00001389 const Type* Ty = I->Ty->get();
Reid Spencer6fb989c2007-03-20 01:13:36 +00001390 Params.push_back(Ty);
1391 if (Ty != Type::VoidTy)
Christopher Lamb3f706f22007-04-22 20:09:11 +00001392 if (I->Attrs != ParamAttr::None) {
1393 ParamAttrsWithIndex X; X.index = index; X.attrs = I->Attrs;
1394 Attrs.push_back(X);
1395 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001396 }
1397 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1398 if (isVarArg) Params.pop_back();
1399
Reid Spencerf51a7052007-04-09 06:16:21 +00001400 ParamAttrsList *ActualAttrs = 0;
1401 if (!Attrs.empty())
Christopher Lamb3f706f22007-04-22 20:09:11 +00001402 ActualAttrs = ParamAttrsList::get(Attrs);
Reid Spencerf51a7052007-04-09 06:16:21 +00001403
1404 FunctionType *FT = FunctionType::get($1, Params, isVarArg, ActualAttrs);
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001405 delete $3; // Delete the argument list
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001406 $$ = new PATypeHolder(HandleUpRefs(FT));
1407 CHECK_FOR_ERROR
1408 }
1409
1410 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Reid Spencere2c32da2006-12-03 05:46:11 +00001411 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1412 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00001413 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001414 }
Chris Lattner4669b0b2007-02-19 07:44:24 +00001415 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencere2c32da2006-12-03 05:46:11 +00001416 const llvm::Type* ElemTy = $4->get();
1417 if ((unsigned)$2 != $2)
1418 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner03c49532007-01-15 02:27:26 +00001419 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencerd84d35b2007-02-15 02:26:10 +00001420 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencere2c32da2006-12-03 05:46:11 +00001421 if (!isPowerOf2_32($2))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001422 GEN_ERROR("Vector length should be a power of 2");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001423 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencere2c32da2006-12-03 05:46:11 +00001424 delete $4;
1425 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001426 }
1427 | '{' TypeListI '}' { // Structure type?
1428 std::vector<const Type*> Elements;
Reid Spencere2c32da2006-12-03 05:46:11 +00001429 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001430 E = $2->end(); I != E; ++I)
Reid Spencere2c32da2006-12-03 05:46:11 +00001431 Elements.push_back(*I);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001432
Reid Spencere2c32da2006-12-03 05:46:11 +00001433 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001434 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00001435 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001436 }
1437 | '{' '}' { // Empty structure type?
Reid Spencere2c32da2006-12-03 05:46:11 +00001438 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer713eedc2006-08-18 08:43:06 +00001439 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001440 }
Andrew Lenharth2d189ff2006-12-08 18:07:09 +00001441 | '<' '{' TypeListI '}' '>' {
1442 std::vector<const Type*> Elements;
1443 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1444 E = $3->end(); I != E; ++I)
1445 Elements.push_back(*I);
1446
1447 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1448 delete $3;
1449 CHECK_FOR_ERROR
1450 }
1451 | '<' '{' '}' '>' { // Empty structure type?
1452 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1453 CHECK_FOR_ERROR
1454 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001455 ;
1456
1457ArgType
1458 : Types OptParamAttrs {
1459 $$.Ty = $1;
1460 $$.Attrs = $2;
1461 }
1462 ;
1463
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001464ResultTypes
1465 : Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001466 if (!UpRefs.empty())
1467 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1468 if (!(*$1)->isFirstClassType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001469 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001470 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001471 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00001472 | VOID {
1473 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001474 }
1475 ;
1476
1477ArgTypeList : ArgType {
1478 $$ = new TypeWithAttrsList();
1479 $$->push_back($1);
1480 CHECK_FOR_ERROR
1481 }
1482 | ArgTypeList ',' ArgType {
1483 ($$=$1)->push_back($3);
1484 CHECK_FOR_ERROR
1485 }
1486 ;
1487
1488ArgTypeListI
1489 : ArgTypeList
1490 | ArgTypeList ',' DOTDOTDOT {
1491 $$=$1;
Reid Spencera472f662007-04-11 02:44:20 +00001492 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001493 TWA.Ty = new PATypeHolder(Type::VoidTy);
1494 $$->push_back(TWA);
1495 CHECK_FOR_ERROR
1496 }
1497 | DOTDOTDOT {
1498 $$ = new TypeWithAttrsList;
Reid Spencera472f662007-04-11 02:44:20 +00001499 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001500 TWA.Ty = new PATypeHolder(Type::VoidTy);
1501 $$->push_back(TWA);
1502 CHECK_FOR_ERROR
1503 }
1504 | /*empty*/ {
1505 $$ = new TypeWithAttrsList();
Reid Spencer713eedc2006-08-18 08:43:06 +00001506 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001507 };
1508
1509// TypeList - Used for struct declarations and as a basis for function type
1510// declaration type lists
1511//
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001512TypeListI : Types {
Reid Spencere2c32da2006-12-03 05:46:11 +00001513 $$ = new std::list<PATypeHolder>();
Reid Spencer6fb989c2007-03-20 01:13:36 +00001514 $$->push_back(*$1);
1515 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001516 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001517 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001518 | TypeListI ',' Types {
Reid Spencer6fb989c2007-03-20 01:13:36 +00001519 ($$=$1)->push_back(*$3);
1520 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001521 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001522 };
1523
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001524// ConstVal - The various declarations that go into the constant pool. This
1525// production is used ONLY to represent constants that show up AFTER a 'const',
1526// 'constant' or 'global' token at global scope. Constants that can be inlined
1527// into other expressions (such as integers and constexprs) are handled by the
1528// ResolvedVal, ValueRef and ConstValueRef productions.
1529//
1530ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001531 if (!UpRefs.empty())
1532 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001533 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001534 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001535 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001536 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001537 const Type *ETy = ATy->getElementType();
1538 int NumElements = ATy->getNumElements();
1539
1540 // Verify that we have the correct size...
1541 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer713eedc2006-08-18 08:43:06 +00001542 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001543 utostr($3->size()) + " arguments, but has size of " +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001544 itostr(NumElements) + "");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001545
1546 // Verify all elements are correct type!
1547 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencere2c32da2006-12-03 05:46:11 +00001548 if (ETy != (*$3)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001549 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001550 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencere2c32da2006-12-03 05:46:11 +00001551 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001552 }
1553
Reid Spencere2c32da2006-12-03 05:46:11 +00001554 $$ = ConstantArray::get(ATy, *$3);
1555 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001556 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001557 }
1558 | Types '[' ']' {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001559 if (!UpRefs.empty())
1560 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001561 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001562 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001563 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001564 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001565
1566 int NumElements = ATy->getNumElements();
1567 if (NumElements != -1 && NumElements != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001568 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencera7bb3e92007-02-05 10:18:06 +00001569 " arguments, but has size of " + itostr(NumElements) +"");
Reid Spencere2c32da2006-12-03 05:46:11 +00001570 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1571 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001572 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001573 }
1574 | Types 'c' STRINGCONSTANT {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001575 if (!UpRefs.empty())
1576 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001577 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001578 if (ATy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001579 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001580 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001581
1582 int NumElements = ATy->getNumElements();
1583 const Type *ETy = ATy->getElementType();
Reid Spenceracfe6672007-05-22 18:52:55 +00001584 if (NumElements != -1 && NumElements != int($3->length()))
Reid Spencer713eedc2006-08-18 08:43:06 +00001585 GEN_ERROR("Can't build string constant of size " +
Reid Spenceracfe6672007-05-22 18:52:55 +00001586 itostr((int)($3->length())) +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001587 " when array has size " + itostr(NumElements) + "");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001588 std::vector<Constant*> Vals;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001589 if (ETy == Type::Int8Ty) {
Reid Spenceracfe6672007-05-22 18:52:55 +00001590 for (unsigned i = 0; i < $3->length(); ++i)
1591 Vals.push_back(ConstantInt::get(ETy, (*$3)[i]));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001592 } else {
Reid Spenceracfe6672007-05-22 18:52:55 +00001593 delete $3;
Reid Spencera7bb3e92007-02-05 10:18:06 +00001594 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001595 }
Reid Spenceracfe6672007-05-22 18:52:55 +00001596 delete $3;
Reid Spencere2c32da2006-12-03 05:46:11 +00001597 $$ = ConstantArray::get(ATy, Vals);
1598 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001599 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001600 }
1601 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001602 if (!UpRefs.empty())
1603 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001604 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001605 if (PTy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001606 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001607 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001608 const Type *ETy = PTy->getElementType();
1609 int NumElements = PTy->getNumElements();
1610
1611 // Verify that we have the correct size...
1612 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer713eedc2006-08-18 08:43:06 +00001613 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001614 utostr($3->size()) + " arguments, but has size of " +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001615 itostr(NumElements) + "");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001616
1617 // Verify all elements are correct type!
1618 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencere2c32da2006-12-03 05:46:11 +00001619 if (ETy != (*$3)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00001620 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001621 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencere2c32da2006-12-03 05:46:11 +00001622 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001623 }
1624
Reid Spencerd84d35b2007-02-15 02:26:10 +00001625 $$ = ConstantVector::get(PTy, *$3);
Reid Spencere2c32da2006-12-03 05:46:11 +00001626 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001627 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001628 }
1629 | Types '{' ConstVector '}' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001630 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001631 if (STy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001632 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001633 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001634
1635 if ($3->size() != STy->getNumContainedTypes())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001636 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001637
1638 // Check to ensure that constants are compatible with the type initializer!
1639 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencere2c32da2006-12-03 05:46:11 +00001640 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer713eedc2006-08-18 08:43:06 +00001641 GEN_ERROR("Expected type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001642 STy->getElementType(i)->getDescription() +
1643 "' for element #" + utostr(i) +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001644 " of structure initializer");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001645
Zhou Sheng75b871f2007-01-11 12:24:14 +00001646 // Check to ensure that Type is not packed
1647 if (STy->isPacked())
Chris Lattner720367c2007-04-26 05:31:05 +00001648 GEN_ERROR("Unpacked Initializer to vector type '" +
1649 STy->getDescription() + "'");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001650
Reid Spencere2c32da2006-12-03 05:46:11 +00001651 $$ = ConstantStruct::get(STy, *$3);
1652 delete $1; delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00001653 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001654 }
1655 | Types '{' '}' {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001656 if (!UpRefs.empty())
1657 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001658 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001659 if (STy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001660 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001661 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001662
1663 if (STy->getNumContainedTypes() != 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001664 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001665
Zhou Sheng75b871f2007-01-11 12:24:14 +00001666 // Check to ensure that Type is not packed
1667 if (STy->isPacked())
Chris Lattner720367c2007-04-26 05:31:05 +00001668 GEN_ERROR("Unpacked Initializer to vector type '" +
1669 STy->getDescription() + "'");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001670
1671 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1672 delete $1;
1673 CHECK_FOR_ERROR
1674 }
1675 | Types '<' '{' ConstVector '}' '>' {
1676 const StructType *STy = dyn_cast<StructType>($1->get());
1677 if (STy == 0)
1678 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001679 (*$1)->getDescription() + "'");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001680
1681 if ($4->size() != STy->getNumContainedTypes())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001682 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001683
1684 // Check to ensure that constants are compatible with the type initializer!
1685 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1686 if ((*$4)[i]->getType() != STy->getElementType(i))
1687 GEN_ERROR("Expected type '" +
1688 STy->getElementType(i)->getDescription() +
1689 "' for element #" + utostr(i) +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001690 " of structure initializer");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001691
1692 // Check to ensure that Type is packed
1693 if (!STy->isPacked())
Chris Lattner4669b0b2007-02-19 07:44:24 +00001694 GEN_ERROR("Vector initializer to non-vector type '" +
1695 STy->getDescription() + "'");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001696
1697 $$ = ConstantStruct::get(STy, *$4);
1698 delete $1; delete $4;
1699 CHECK_FOR_ERROR
1700 }
1701 | Types '<' '{' '}' '>' {
1702 if (!UpRefs.empty())
1703 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1704 const StructType *STy = dyn_cast<StructType>($1->get());
1705 if (STy == 0)
1706 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001707 (*$1)->getDescription() + "'");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001708
1709 if (STy->getNumContainedTypes() != 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001710 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001711
1712 // Check to ensure that Type is packed
1713 if (!STy->isPacked())
Chris Lattner4669b0b2007-02-19 07:44:24 +00001714 GEN_ERROR("Vector initializer to non-vector type '" +
1715 STy->getDescription() + "'");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001716
Reid Spencere2c32da2006-12-03 05:46:11 +00001717 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1718 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001719 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001720 }
1721 | Types NULL_TOK {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001722 if (!UpRefs.empty())
1723 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001724 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001725 if (PTy == 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00001726 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001727 (*$1)->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001728
Reid Spencere2c32da2006-12-03 05:46:11 +00001729 $$ = ConstantPointerNull::get(PTy);
1730 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001731 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001732 }
1733 | Types UNDEF {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001734 if (!UpRefs.empty())
1735 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001736 $$ = UndefValue::get($1->get());
1737 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001738 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001739 }
1740 | Types SymbolicValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001741 if (!UpRefs.empty())
1742 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001743 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001744 if (Ty == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001745 GEN_ERROR("Global const reference must be a pointer type");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001746
1747 // ConstExprs can exist in the body of a function, thus creating
1748 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencerd0e8d382007-03-19 18:40:50 +00001749 // the context of a function, getExistingVal will search the functions
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001750 // symbol table instead of the module symbol table for the global symbol,
1751 // which throws things all off. To get around this, we just tell
Reid Spencerd0e8d382007-03-19 18:40:50 +00001752 // getExistingVal that we are at global scope here.
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001753 //
1754 Function *SavedCurFn = CurFun.CurrentFunction;
1755 CurFun.CurrentFunction = 0;
1756
Reid Spencerd0e8d382007-03-19 18:40:50 +00001757 Value *V = getExistingVal(Ty, $2);
Reid Spencer309080a2006-09-28 19:28:24 +00001758 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001759
1760 CurFun.CurrentFunction = SavedCurFn;
1761
1762 // If this is an initializer for a constant pointer, which is referencing a
1763 // (currently) undefined variable, create a stub now that shall be replaced
1764 // in the future with the right type of variable.
1765 //
1766 if (V == 0) {
Reid Spencerac6bfc52007-02-05 17:04:00 +00001767 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001768 const PointerType *PT = cast<PointerType>(Ty);
1769
1770 // First check to see if the forward references value is already created!
1771 PerModuleInfo::GlobalRefsType::iterator I =
1772 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1773
1774 if (I != CurModule.GlobalRefs.end()) {
1775 V = I->second; // Placeholder already exists, use it...
1776 $2.destroy();
1777 } else {
1778 std::string Name;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001779 if ($2.Type == ValID::GlobalName)
Reid Spenceracfe6672007-05-22 18:52:55 +00001780 Name = $2.getName();
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00001781 else if ($2.Type != ValID::GlobalID)
1782 GEN_ERROR("Invalid reference to global");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001783
1784 // Create the forward referenced global.
1785 GlobalValue *GV;
1786 if (const FunctionType *FTy =
1787 dyn_cast<FunctionType>(PT->getElementType())) {
Chris Lattner720367c2007-04-26 05:31:05 +00001788 GV = new Function(FTy, GlobalValue::ExternalWeakLinkage, Name,
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001789 CurModule.CurrentModule);
1790 } else {
1791 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattner720367c2007-04-26 05:31:05 +00001792 GlobalValue::ExternalWeakLinkage, 0,
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001793 Name, CurModule.CurrentModule);
1794 }
1795
1796 // Keep track of the fact that we have a forward ref to recycle it
1797 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1798 V = GV;
1799 }
1800 }
1801
Reid Spencere2c32da2006-12-03 05:46:11 +00001802 $$ = cast<GlobalValue>(V);
1803 delete $1; // Free the type handle
Reid Spencer713eedc2006-08-18 08:43:06 +00001804 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001805 }
1806 | Types ConstExpr {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001807 if (!UpRefs.empty())
1808 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001809 if ($1->get() != $2->getType())
Reid Spencerca3d1cb2007-01-04 00:06:14 +00001810 GEN_ERROR("Mismatched types for constant expression: " +
1811 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001812 $$ = $2;
Reid Spencere2c32da2006-12-03 05:46:11 +00001813 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001814 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001815 }
1816 | Types ZEROINITIALIZER {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001817 if (!UpRefs.empty())
1818 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001819 const Type *Ty = $1->get();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001820 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001821 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencere2c32da2006-12-03 05:46:11 +00001822 $$ = Constant::getNullValue(Ty);
1823 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00001824 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00001825 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001826 | IntType ESINT64VAL { // integral constants
Reid Spencer266e42b2006-12-23 06:05:41 +00001827 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001828 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencera7bed60a2007-03-19 20:40:51 +00001829 $$ = ConstantInt::get($1, $2, true);
Reid Spencerc7a686b2007-02-28 02:24:54 +00001830 CHECK_FOR_ERROR
1831 }
1832 | IntType ESAPINTVAL { // arbitrary precision integer constants
1833 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1834 if ($2->getBitWidth() > BitWidth) {
1835 GEN_ERROR("Constant value does not fit in type");
Reid Spencer8ad254d2007-03-01 19:41:47 +00001836 }
1837 $2->sextOrTrunc(BitWidth);
1838 $$ = ConstantInt::get(*$2);
Reid Spencerc7a686b2007-02-28 02:24:54 +00001839 delete $2;
Reid Spencer266e42b2006-12-23 06:05:41 +00001840 CHECK_FOR_ERROR
1841 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001842 | IntType EUINT64VAL { // integral constants
Reid Spencer266e42b2006-12-23 06:05:41 +00001843 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001844 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencera7bed60a2007-03-19 20:40:51 +00001845 $$ = ConstantInt::get($1, $2, false);
Reid Spencerc7a686b2007-02-28 02:24:54 +00001846 CHECK_FOR_ERROR
1847 }
1848 | IntType EUAPINTVAL { // arbitrary precision integer constants
1849 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1850 if ($2->getBitWidth() > BitWidth) {
1851 GEN_ERROR("Constant value does not fit in type");
Reid Spencer8ad254d2007-03-01 19:41:47 +00001852 }
1853 $2->zextOrTrunc(BitWidth);
1854 $$ = ConstantInt::get(*$2);
Reid Spencerc7a686b2007-02-28 02:24:54 +00001855 delete $2;
Reid Spencer266e42b2006-12-23 06:05:41 +00001856 CHECK_FOR_ERROR
1857 }
Reid Spencer58a8db02007-01-13 05:00:46 +00001858 | INTTYPE TRUETOK { // Boolean constants
1859 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001860 $$ = ConstantInt::getTrue();
Reid Spencer713eedc2006-08-18 08:43:06 +00001861 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001862 }
Reid Spencer58a8db02007-01-13 05:00:46 +00001863 | INTTYPE FALSETOK { // Boolean constants
1864 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng75b871f2007-01-11 12:24:14 +00001865 $$ = ConstantInt::getFalse();
Reid Spencer713eedc2006-08-18 08:43:06 +00001866 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001867 }
1868 | FPType FPVAL { // Float & Double constants
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001869 if (!ConstantFP::isValueValidForType($1, *$2))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001870 GEN_ERROR("Floating point constant invalid for type");
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001871 // Lexer has no type info, so builds all FP constants as double.
1872 // Fix this here.
1873 if ($1==Type::FloatTy)
1874 $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
1875 $$ = ConstantFP::get($1, *$2);
Reid Spencer713eedc2006-08-18 08:43:06 +00001876 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001877 };
1878
1879
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001880ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00001881 if (!UpRefs.empty())
1882 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00001883 Constant *Val = $3;
Reid Spencerceb84592007-01-17 02:48:45 +00001884 const Type *DestTy = $5->get();
1885 if (!CastInst::castIsValid($1, $3, DestTy))
1886 GEN_ERROR("invalid cast opcode for cast from '" +
1887 Val->getType()->getDescription() + "' to '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00001888 DestTy->getDescription() + "'");
Reid Spencerceb84592007-01-17 02:48:45 +00001889 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencere2c32da2006-12-03 05:46:11 +00001890 delete $5;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001891 }
1892 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001893 if (!isa<PointerType>($3->getType()))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001894 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001895
Reid Spencere2c32da2006-12-03 05:46:11 +00001896 const Type *IdxTy =
David Greene85948d742007-09-04 18:46:50 +00001897 GetElementPtrInst::getIndexedType($3->getType(), $4->begin(), $4->end(),
Chris Lattnerf79508f2007-02-13 00:58:01 +00001898 true);
Reid Spencere2c32da2006-12-03 05:46:11 +00001899 if (!IdxTy)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001900 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencere2c32da2006-12-03 05:46:11 +00001901
Chris Lattner55ce85c2007-01-31 04:44:08 +00001902 SmallVector<Constant*, 8> IdxVec;
Reid Spencere2c32da2006-12-03 05:46:11 +00001903 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1904 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001905 IdxVec.push_back(C);
1906 else
Reid Spencera7bb3e92007-02-05 10:18:06 +00001907 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001908
1909 delete $4;
1910
Chris Lattner55ce85c2007-01-31 04:44:08 +00001911 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer713eedc2006-08-18 08:43:06 +00001912 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001913 }
1914 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer542964f2007-01-11 18:21:29 +00001915 if ($3->getType() != Type::Int1Ty)
Reid Spencera7bb3e92007-02-05 10:18:06 +00001916 GEN_ERROR("Select condition must be of boolean type");
Reid Spencere2c32da2006-12-03 05:46:11 +00001917 if ($5->getType() != $7->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001918 GEN_ERROR("Select operand types must match");
Reid Spencere2c32da2006-12-03 05:46:11 +00001919 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001920 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001921 }
1922 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001923 if ($3->getType() != $5->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001924 GEN_ERROR("Binary operator types must match");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001925 CHECK_FOR_ERROR;
Reid Spencer844668d2006-12-05 19:16:11 +00001926 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001927 }
1928 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001929 if ($3->getType() != $5->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001930 GEN_ERROR("Logical operator types must match");
Chris Lattner03c49532007-01-15 02:27:26 +00001931 if (!$3->getType()->isInteger()) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001932 if (Instruction::isShift($1) || !isa<VectorType>($3->getType()) ||
1933 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001934 GEN_ERROR("Logical operator requires integral operands");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001935 }
Reid Spencere2c32da2006-12-03 05:46:11 +00001936 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001937 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001938 }
Reid Spencerd2e0c342006-12-04 05:24:24 +00001939 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1940 if ($4->getType() != $6->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001941 GEN_ERROR("icmp operand types must match");
Reid Spencerd2e0c342006-12-04 05:24:24 +00001942 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencere2c32da2006-12-03 05:46:11 +00001943 }
Reid Spencerd2e0c342006-12-04 05:24:24 +00001944 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1945 if ($4->getType() != $6->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00001946 GEN_ERROR("fcmp operand types must match");
Reid Spencerd2e0c342006-12-04 05:24:24 +00001947 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencere2c32da2006-12-03 05:46:11 +00001948 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001949 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001950 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001951 GEN_ERROR("Invalid extractelement operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00001952 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00001953 CHECK_FOR_ERROR
Chris Lattneraebccf82006-04-08 03:55:17 +00001954 }
1955 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001956 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001957 GEN_ERROR("Invalid insertelement operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00001958 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001959 CHECK_FOR_ERROR
Chris Lattneraebccf82006-04-08 03:55:17 +00001960 }
1961 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere2c32da2006-12-03 05:46:11 +00001962 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencera7bb3e92007-02-05 10:18:06 +00001963 GEN_ERROR("Invalid shufflevector operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00001964 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer713eedc2006-08-18 08:43:06 +00001965 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001966 };
1967
Chris Lattneraebccf82006-04-08 03:55:17 +00001968
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001969// ConstVector - A list of comma separated constants.
1970ConstVector : ConstVector ',' ConstVal {
1971 ($$ = $1)->push_back($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00001972 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001973 }
1974 | ConstVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00001975 $$ = new std::vector<Constant*>();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001976 $$->push_back($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00001977 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00001978 };
1979
1980
1981// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1982GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
1983
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00001984// ThreadLocal
1985ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
1986
Anton Korobeynikov511d54f2007-04-28 13:48:45 +00001987// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
1988AliaseeRef : ResultTypes SymbolicValueRef {
1989 const Type* VTy = $1->get();
1990 Value *V = getVal(VTy, $2);
Chris Lattnerf72a2db2007-08-06 21:00:46 +00001991 CHECK_FOR_ERROR
Anton Korobeynikov511d54f2007-04-28 13:48:45 +00001992 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
1993 if (!Aliasee)
1994 GEN_ERROR("Aliases can be created only to global values");
1995
1996 $$ = Aliasee;
1997 CHECK_FOR_ERROR
1998 delete $1;
1999 }
2000 | BITCAST '(' AliaseeRef TO Types ')' {
2001 Constant *Val = $3;
2002 const Type *DestTy = $5->get();
2003 if (!CastInst::castIsValid($1, $3, DestTy))
2004 GEN_ERROR("invalid cast opcode for cast from '" +
2005 Val->getType()->getDescription() + "' to '" +
2006 DestTy->getDescription() + "'");
2007
2008 $$ = ConstantExpr::getCast($1, $3, DestTy);
2009 CHECK_FOR_ERROR
2010 delete $5;
2011 };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002012
2013//===----------------------------------------------------------------------===//
2014// Rules to match Modules
2015//===----------------------------------------------------------------------===//
2016
2017// Module rule: Capture the result of parsing the whole file into a result
2018// variable...
2019//
Reid Spencerdc0a3a22006-12-29 20:35:03 +00002020Module
2021 : DefinitionList {
2022 $$ = ParserResult = CurModule.CurrentModule;
2023 CurModule.ModuleDone();
2024 CHECK_FOR_ERROR;
2025 }
2026 | /*empty*/ {
2027 $$ = ParserResult = CurModule.CurrentModule;
2028 CurModule.ModuleDone();
2029 CHECK_FOR_ERROR;
2030 }
2031 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002032
Reid Spencerdc0a3a22006-12-29 20:35:03 +00002033DefinitionList
2034 : Definition
2035 | DefinitionList Definition
2036 ;
2037
2038Definition
Jeff Cohen5d956e42007-01-21 19:19:31 +00002039 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002040 CurFun.FunctionDone();
Reid Spencer713eedc2006-08-18 08:43:06 +00002041 CHECK_FOR_ERROR
Reid Spencerdc0a3a22006-12-29 20:35:03 +00002042 }
2043 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer713eedc2006-08-18 08:43:06 +00002044 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002045 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00002046 | MODULE ASM_TOK AsmBlock {
Reid Spencer713eedc2006-08-18 08:43:06 +00002047 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002048 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002049 | OptLocalAssign TYPE Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002050 if (!UpRefs.empty())
2051 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002052 // Eagerly resolve types. This is not an optimization, this is a
2053 // requirement that is due to the fact that we could have this:
2054 //
2055 // %list = type { %list * }
2056 // %list = type { %list * } ; repeated type decl
2057 //
2058 // If types are not resolved eagerly, then the two types will not be
2059 // determined to be the same type!
2060 //
Reid Spencerdc0a3a22006-12-29 20:35:03 +00002061 ResolveTypeTo($1, *$3);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002062
Reid Spencerdc0a3a22006-12-29 20:35:03 +00002063 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer309080a2006-09-28 19:28:24 +00002064 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002065 // If this is a named type that is not a redefinition, add it to the slot
2066 // table.
Reid Spencerdc0a3a22006-12-29 20:35:03 +00002067 CurModule.Types.push_back(*$3);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002068 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002069
Reid Spencerdc0a3a22006-12-29 20:35:03 +00002070 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00002071 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002072 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002073 | OptLocalAssign TYPE VOID {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002074 ResolveTypeTo($1, $3);
2075
2076 if (!setTypeName($3, $1) && !$1) {
2077 CHECK_FOR_ERROR
2078 // If this is a named type that is not a redefinition, add it to the slot
2079 // table.
2080 CurModule.Types.push_back($3);
2081 }
2082 CHECK_FOR_ERROR
2083 }
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002084 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002085 /* "Externally Visible" Linkage */
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002086 if ($5 == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002087 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002088 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
2089 $2, $4, $5->getType(), $5, $3);
Reid Spencerdc0a3a22006-12-29 20:35:03 +00002090 CHECK_FOR_ERROR
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002091 } GlobalVarAttributes {
2092 CurGV = 0;
2093 }
Chris Lattner720367c2007-04-26 05:31:05 +00002094 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
2095 ConstVal {
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002096 if ($6 == 0)
2097 GEN_ERROR("Global value initializer is not a constant");
2098 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002099 CHECK_FOR_ERROR
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002100 } GlobalVarAttributes {
2101 CurGV = 0;
2102 }
Chris Lattner720367c2007-04-26 05:31:05 +00002103 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
2104 Types {
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002105 if (!UpRefs.empty())
2106 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
2107 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4);
2108 CHECK_FOR_ERROR
2109 delete $6;
Reid Spencer309080a2006-09-28 19:28:24 +00002110 } GlobalVarAttributes {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002111 CurGV = 0;
2112 CHECK_FOR_ERROR
2113 }
Anton Korobeynikov511d54f2007-04-28 13:48:45 +00002114 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
Reid Spenceracfe6672007-05-22 18:52:55 +00002115 std::string Name;
2116 if ($1) {
2117 Name = *$1;
2118 delete $1;
2119 }
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00002120 if (Name.empty())
Anton Korobeynikov511d54f2007-04-28 13:48:45 +00002121 GEN_ERROR("Alias name cannot be empty");
2122
2123 Constant* Aliasee = $5;
2124 if (Aliasee == 0)
Reid Spenceracfe6672007-05-22 18:52:55 +00002125 GEN_ERROR(std::string("Invalid aliasee for alias: ") + Name);
Anton Korobeynikov511d54f2007-04-28 13:48:45 +00002126
2127 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2128 CurModule.CurrentModule);
2129 GA->setVisibility($2);
2130 InsertValue(GA, CurModule.Values);
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00002131 CHECK_FOR_ERROR
Anton Korobeynikove9fcbef2007-04-25 14:29:12 +00002132 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00002133 | TARGET TargetDefinition {
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002134 CHECK_FOR_ERROR
2135 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00002136 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer713eedc2006-08-18 08:43:06 +00002137 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002138 }
Reid Spencerdc0a3a22006-12-29 20:35:03 +00002139 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002140
2141
2142AsmBlock : STRINGCONSTANT {
2143 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002144 if (AsmSoFar.empty())
Reid Spenceracfe6672007-05-22 18:52:55 +00002145 CurModule.CurrentModule->setModuleInlineAsm(*$1);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002146 else
Reid Spenceracfe6672007-05-22 18:52:55 +00002147 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+*$1);
2148 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002149 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002150};
2151
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002152TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Reid Spenceracfe6672007-05-22 18:52:55 +00002153 CurModule.CurrentModule->setTargetTriple(*$3);
2154 delete $3;
John Criswell6af0b122006-10-24 19:09:48 +00002155 }
Chris Lattner7d1d0342006-10-22 06:08:13 +00002156 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spenceracfe6672007-05-22 18:52:55 +00002157 CurModule.CurrentModule->setDataLayout(*$3);
2158 delete $3;
Owen Anderson85690f32006-10-18 02:21:48 +00002159 };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002160
2161LibrariesDefinition : '[' LibList ']';
2162
2163LibList : LibList ',' STRINGCONSTANT {
Reid Spenceracfe6672007-05-22 18:52:55 +00002164 CurModule.CurrentModule->addLibrary(*$3);
2165 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00002166 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002167 }
2168 | STRINGCONSTANT {
Reid Spenceracfe6672007-05-22 18:52:55 +00002169 CurModule.CurrentModule->addLibrary(*$1);
2170 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002171 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002172 }
2173 | /* empty: end of list */ {
Reid Spencer713eedc2006-08-18 08:43:06 +00002174 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002175 }
2176 ;
2177
2178//===----------------------------------------------------------------------===//
2179// Rules to match Function Headers
2180//===----------------------------------------------------------------------===//
2181
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002182ArgListH : ArgListH ',' Types OptParamAttrs OptLocalName {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002183 if (!UpRefs.empty())
2184 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
2185 if (*$3 == Type::VoidTy)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002186 GEN_ERROR("void typed arguments are invalid");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002187 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002188 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002189 $1->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002190 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002191 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002192 | Types OptParamAttrs OptLocalName {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002193 if (!UpRefs.empty())
2194 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2195 if (*$1 == Type::VoidTy)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002196 GEN_ERROR("void typed arguments are invalid");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002197 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2198 $$ = new ArgListType;
2199 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002200 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002201 };
2202
2203ArgList : ArgListH {
2204 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002205 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002206 }
2207 | ArgListH ',' DOTDOTDOT {
2208 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002209 struct ArgListEntry E;
2210 E.Ty = new PATypeHolder(Type::VoidTy);
2211 E.Name = 0;
Reid Spencera472f662007-04-11 02:44:20 +00002212 E.Attrs = ParamAttr::None;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002213 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002214 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002215 }
2216 | DOTDOTDOT {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002217 $$ = new ArgListType;
2218 struct ArgListEntry E;
2219 E.Ty = new PATypeHolder(Type::VoidTy);
2220 E.Name = 0;
Reid Spencera472f662007-04-11 02:44:20 +00002221 E.Attrs = ParamAttr::None;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002222 $$->push_back(E);
Reid Spencer713eedc2006-08-18 08:43:06 +00002223 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002224 }
2225 | /* empty */ {
2226 $$ = 0;
Reid Spencer713eedc2006-08-18 08:43:06 +00002227 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002228 };
2229
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002230FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002231 OptFuncAttrs OptSection OptAlign {
Reid Spenceracfe6672007-05-22 18:52:55 +00002232 std::string FunctionName(*$3);
2233 delete $3; // Free strdup'd memory!
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002234
Reid Spencer87622ae2007-01-02 21:54:12 +00002235 // Check the function result for abstractness if this is a define. We should
2236 // have no abstract types at this point
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002237 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2238 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer87622ae2007-01-02 21:54:12 +00002239
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002240 std::vector<const Type*> ParamTypeList;
Christopher Lamb3f706f22007-04-22 20:09:11 +00002241 ParamAttrsVector Attrs;
2242 if ($7 != ParamAttr::None) {
2243 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $7;
2244 Attrs.push_back(PAWI);
2245 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002246 if ($5) { // If there are arguments...
Reid Spencerf51a7052007-04-09 06:16:21 +00002247 unsigned index = 1;
2248 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I, ++index) {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002249 const Type* Ty = I->Ty->get();
Reid Spencer87622ae2007-01-02 21:54:12 +00002250 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2251 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002252 ParamTypeList.push_back(Ty);
2253 if (Ty != Type::VoidTy)
Christopher Lamb3f706f22007-04-22 20:09:11 +00002254 if (I->Attrs != ParamAttr::None) {
2255 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2256 Attrs.push_back(PAWI);
2257 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002258 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002259 }
2260
2261 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2262 if (isVarArg) ParamTypeList.pop_back();
2263
Christopher Lamb3f706f22007-04-22 20:09:11 +00002264 ParamAttrsList *PAL = 0;
2265 if (!Attrs.empty())
2266 PAL = ParamAttrsList::get(Attrs);
Reid Spencerf51a7052007-04-09 06:16:21 +00002267
Christopher Lamb3f706f22007-04-22 20:09:11 +00002268 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg, PAL);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002269 const PointerType *PFT = PointerType::get(FT);
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002270 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002271
2272 ValID ID;
2273 if (!FunctionName.empty()) {
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002274 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002275 } else {
Reid Spencerd0e8d382007-03-19 18:40:50 +00002276 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002277 }
2278
2279 Function *Fn = 0;
2280 // See if this function was forward referenced. If so, recycle the object.
2281 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2282 // Move the function to the end of the list, from whereever it was
2283 // previously inserted.
2284 Fn = cast<Function>(FWRef);
2285 CurModule.CurrentModule->getFunctionList().remove(Fn);
2286 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2287 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002288 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Chris Lattner720367c2007-04-26 05:31:05 +00002289 if (Fn->getFunctionType() != FT) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002290 // The existing function doesn't have the same type. This is an overload
2291 // error.
2292 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
2293 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
Chris Lattner720367c2007-04-26 05:31:05 +00002294 // Neither the existing or the current function is a declaration and they
2295 // have the same name and same type. Clearly this is a redefinition.
2296 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002297 } if (Fn->isDeclaration()) {
2298 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002299 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2300 AI != AE; ++AI)
2301 AI->setName("");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002302 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002303 } else { // Not already defined?
Chris Lattner720367c2007-04-26 05:31:05 +00002304 Fn = new Function(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002305 CurModule.CurrentModule);
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002306
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002307 InsertValue(Fn, CurModule.Values);
2308 }
2309
2310 CurFun.FunctionStart(Fn);
Anton Korobeynikov0ab01ff2006-09-17 13:06:18 +00002311
2312 if (CurFun.isDeclare) {
2313 // If we have declaration, always overwrite linkage. This will allow us to
2314 // correctly handle cases, when pointer to function is passed as argument to
2315 // another function.
2316 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002317 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov0ab01ff2006-09-17 13:06:18 +00002318 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002319 Fn->setCallingConv($1);
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002320 Fn->setAlignment($9);
2321 if ($8) {
Reid Spenceracfe6672007-05-22 18:52:55 +00002322 Fn->setSection(*$8);
2323 delete $8;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002324 }
2325
2326 // Add all of the arguments we parsed to the function...
2327 if ($5) { // Is null if empty...
2328 if (isVarArg) { // Nuke the last entry
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002329 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0 &&
Reid Spencerac6bfc52007-02-05 17:04:00 +00002330 "Not a varargs marker!");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002331 delete $5->back().Ty;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002332 $5->pop_back(); // Delete the last entry
2333 }
2334 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002335 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002336 unsigned Idx = 1;
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002337 for (ArgListType::iterator I = $5->begin();
2338 I != $5->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002339 delete I->Ty; // Delete the typeholder...
Reid Spenceracfe6672007-05-22 18:52:55 +00002340 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer309080a2006-09-28 19:28:24 +00002341 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002342 InsertValue(ArgIt);
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002343 Idx++;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002344 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002345
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002346 delete $5; // We're now done with the argument list
2347 }
Reid Spencer713eedc2006-08-18 08:43:06 +00002348 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002349};
2350
2351BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2352
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002353FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002354 $$ = CurFun.CurrentFunction;
2355
2356 // Make sure that we keep track of the linkage type even if there was a
2357 // previous "declare".
2358 $$->setLinkage($1);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002359 $$->setVisibility($2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002360};
2361
2362END : ENDTOK | '}'; // Allow end of '}' to end a function
2363
2364Function : BasicBlockList END {
2365 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002366 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002367};
2368
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002369FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002370 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00002371 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +00002372 $$ = CurFun.CurrentFunction;
2373 CurFun.FunctionDone();
2374 CHECK_FOR_ERROR
2375 };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002376
2377//===----------------------------------------------------------------------===//
2378// Rules to match Basic Blocks
2379//===----------------------------------------------------------------------===//
2380
2381OptSideEffect : /* empty */ {
2382 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002383 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002384 }
2385 | SIDEEFFECT {
2386 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002387 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002388 };
2389
2390ConstValueRef : ESINT64VAL { // A reference to a direct constant
2391 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002392 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002393 }
2394 | EUINT64VAL {
2395 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002396 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002397 }
2398 | FPVAL { // Perhaps it's an FP constant?
2399 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002400 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002401 }
2402 | TRUETOK {
Zhou Sheng75b871f2007-01-11 12:24:14 +00002403 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer713eedc2006-08-18 08:43:06 +00002404 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002405 }
2406 | FALSETOK {
Zhou Sheng75b871f2007-01-11 12:24:14 +00002407 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer713eedc2006-08-18 08:43:06 +00002408 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002409 }
2410 | NULL_TOK {
2411 $$ = ValID::createNull();
Reid Spencer713eedc2006-08-18 08:43:06 +00002412 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002413 }
2414 | UNDEF {
2415 $$ = ValID::createUndef();
Reid Spencer713eedc2006-08-18 08:43:06 +00002416 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002417 }
2418 | ZEROINITIALIZER { // A vector zero constant.
2419 $$ = ValID::createZeroInit();
Reid Spencer713eedc2006-08-18 08:43:06 +00002420 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002421 }
2422 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencere2c32da2006-12-03 05:46:11 +00002423 const Type *ETy = (*$2)[0]->getType();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002424 int NumElements = $2->size();
2425
Reid Spencerd84d35b2007-02-15 02:26:10 +00002426 VectorType* pt = VectorType::get(ETy, NumElements);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002427 PATypeHolder* PTy = new PATypeHolder(
Reid Spencere2c32da2006-12-03 05:46:11 +00002428 HandleUpRefs(
Reid Spencerd84d35b2007-02-15 02:26:10 +00002429 VectorType::get(
Reid Spencere2c32da2006-12-03 05:46:11 +00002430 ETy,
2431 NumElements)
2432 )
2433 );
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002434
2435 // Verify all elements are correct type!
2436 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencere2c32da2006-12-03 05:46:11 +00002437 if (ETy != (*$2)[i]->getType())
Reid Spencer713eedc2006-08-18 08:43:06 +00002438 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002439 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencere2c32da2006-12-03 05:46:11 +00002440 (*$2)[i]->getType()->getDescription() + "'.");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002441 }
2442
Reid Spencerd84d35b2007-02-15 02:26:10 +00002443 $$ = ValID::create(ConstantVector::get(pt, *$2));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002444 delete PTy; delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00002445 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002446 }
2447 | ConstExpr {
Reid Spencere2c32da2006-12-03 05:46:11 +00002448 $$ = ValID::create($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002449 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002450 }
2451 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
Reid Spenceracfe6672007-05-22 18:52:55 +00002452 $$ = ValID::createInlineAsm(*$3, *$5, $2);
2453 delete $3;
2454 delete $5;
Reid Spencer713eedc2006-08-18 08:43:06 +00002455 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002456 };
2457
2458// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2459// another value.
2460//
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002461SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2462 $$ = ValID::createLocalID($1);
Reid Spencer713eedc2006-08-18 08:43:06 +00002463 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002464 }
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002465 | GLOBALVAL_ID {
2466 $$ = ValID::createGlobalID($1);
2467 CHECK_FOR_ERROR
2468 }
2469 | LocalName { // Is it a named reference...?
Reid Spenceracfe6672007-05-22 18:52:55 +00002470 $$ = ValID::createLocalName(*$1);
2471 delete $1;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002472 CHECK_FOR_ERROR
2473 }
2474 | GlobalName { // Is it a named reference...?
Reid Spenceracfe6672007-05-22 18:52:55 +00002475 $$ = ValID::createGlobalName(*$1);
2476 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002477 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002478 };
2479
2480// ValueRef - A reference to a definition... either constant or symbolic
2481ValueRef : SymbolicValueRef | ConstValueRef;
2482
2483
2484// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2485// type immediately preceeds the value reference, and allows complex constant
2486// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2487ResolvedVal : Types ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002488 if (!UpRefs.empty())
2489 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2490 $$ = getVal(*$1, $2);
2491 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002492 CHECK_FOR_ERROR
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002493 }
2494 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002495
2496BasicBlockList : BasicBlockList BasicBlock {
2497 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002498 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002499 }
2500 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
2501 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002502 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002503 };
2504
2505
2506// Basic blocks are terminated by branching instructions:
2507// br, br/cc, switch, ret
2508//
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002509BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002510 setValueName($3, $2);
Reid Spencer309080a2006-09-28 19:28:24 +00002511 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002512 InsertValue($3);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002513 $1->getInstList().push_back($3);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002514 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002515 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002516 };
2517
2518InstructionList : InstructionList Inst {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002519 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2520 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2521 if (CI2->getParent() == 0)
2522 $1->getInstList().push_back(CI2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002523 $1->getInstList().push_back($2);
2524 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002525 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002526 }
Reid Spencerd0e8d382007-03-19 18:40:50 +00002527 | /* empty */ { // Empty space between instruction lists
2528 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
Reid Spencer713eedc2006-08-18 08:43:06 +00002529 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002530 }
Reid Spencerd0e8d382007-03-19 18:40:50 +00002531 | LABELSTR { // Labelled (named) basic block
Reid Spenceracfe6672007-05-22 18:52:55 +00002532 $$ = defineBBVal(ValID::createLocalName(*$1));
2533 delete $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002534 CHECK_FOR_ERROR
Reid Spenceracfe6672007-05-22 18:52:55 +00002535
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002536 };
2537
2538BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencere2c32da2006-12-03 05:46:11 +00002539 $$ = new ReturnInst($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00002540 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002541 }
Reid Spencerd0e8d382007-03-19 18:40:50 +00002542 | RET VOID { // Return with no result...
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002543 $$ = new ReturnInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002544 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002545 }
Reid Spencerd0e8d382007-03-19 18:40:50 +00002546 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer309080a2006-09-28 19:28:24 +00002547 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002548 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002549 $$ = new BranchInst(tmpBB);
Reid Spencerd0e8d382007-03-19 18:40:50 +00002550 } // Conditional Branch...
Reid Spencer58a8db02007-01-13 05:00:46 +00002551 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
2552 assert(cast<IntegerType>($2)->getBitWidth() == 1 && "Not Bool?");
Reid Spencer309080a2006-09-28 19:28:24 +00002553 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002554 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002555 BasicBlock* tmpBBB = getBBVal($9);
2556 CHECK_FOR_ERROR
Reid Spencer542964f2007-01-11 18:21:29 +00002557 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002558 CHECK_FOR_ERROR
2559 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002560 }
2561 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencere2c32da2006-12-03 05:46:11 +00002562 Value* tmpVal = getVal($2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002563 CHECK_FOR_ERROR
2564 BasicBlock* tmpBB = getBBVal($6);
2565 CHECK_FOR_ERROR
2566 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002567 $$ = S;
2568
2569 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2570 E = $8->end();
2571 for (; I != E; ++I) {
2572 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2573 S->addCase(CI, I->second);
2574 else
Reid Spencera7bb3e92007-02-05 10:18:06 +00002575 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002576 }
2577 delete $8;
Reid Spencer713eedc2006-08-18 08:43:06 +00002578 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002579 }
2580 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencere2c32da2006-12-03 05:46:11 +00002581 Value* tmpVal = getVal($2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002582 CHECK_FOR_ERROR
2583 BasicBlock* tmpBB = getBBVal($6);
2584 CHECK_FOR_ERROR
2585 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002586 $$ = S;
Reid Spencer713eedc2006-08-18 08:43:06 +00002587 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002588 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002589 | INVOKE OptCallingConv ResultTypes ValueRef '(' ValueRefList ')' OptFuncAttrs
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002590 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002591
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002592 // Handle the short syntax
2593 const PointerType *PFTy = 0;
2594 const FunctionType *Ty = 0;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002595 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002596 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2597 // Pull out the types of all of the arguments...
2598 std::vector<const Type*> ParamTypes;
Christopher Lamb3f706f22007-04-22 20:09:11 +00002599 ParamAttrsVector Attrs;
2600 if ($8 != ParamAttr::None) {
Chris Lattner5c455c32007-05-04 04:01:37 +00002601 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $8;
Christopher Lamb3f706f22007-04-22 20:09:11 +00002602 Attrs.push_back(PAWI);
2603 }
Reid Spencerf51a7052007-04-09 06:16:21 +00002604 ValueRefList::iterator I = $6->begin(), E = $6->end();
2605 unsigned index = 1;
2606 for (; I != E; ++I, ++index) {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002607 const Type *Ty = I->Val->getType();
2608 if (Ty == Type::VoidTy)
2609 GEN_ERROR("Short call syntax cannot be used with varargs");
2610 ParamTypes.push_back(Ty);
Christopher Lamb3f706f22007-04-22 20:09:11 +00002611 if (I->Attrs != ParamAttr::None) {
2612 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2613 Attrs.push_back(PAWI);
2614 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002615 }
2616
Christopher Lamb3f706f22007-04-22 20:09:11 +00002617 ParamAttrsList *PAL = 0;
2618 if (!Attrs.empty())
2619 PAL = ParamAttrsList::get(Attrs);
2620 Ty = FunctionType::get($3->get(), ParamTypes, false, PAL);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002621 PFTy = PointerType::get(Ty);
2622 }
2623
Reid Spencer6fb989c2007-03-20 01:13:36 +00002624 delete $3;
2625
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002626 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer309080a2006-09-28 19:28:24 +00002627 CHECK_FOR_ERROR
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002628 BasicBlock *Normal = getBBVal($11);
Reid Spencer309080a2006-09-28 19:28:24 +00002629 CHECK_FOR_ERROR
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002630 BasicBlock *Except = getBBVal($14);
Reid Spencer309080a2006-09-28 19:28:24 +00002631 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002632
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002633 // Check the arguments
2634 ValueList Args;
2635 if ($6->empty()) { // Has no arguments?
2636 // Make sure no arguments is a good thing!
2637 if (Ty->getNumParams() != 0)
2638 GEN_ERROR("No arguments passed to a function that "
Reid Spencera7bb3e92007-02-05 10:18:06 +00002639 "expects arguments");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002640 } else { // Has arguments?
2641 // Loop through FunctionType's arguments and ensure they are specified
2642 // correctly!
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002643 FunctionType::param_iterator I = Ty->param_begin();
2644 FunctionType::param_iterator E = Ty->param_end();
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002645 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002646
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002647 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2648 if (ArgI->Val->getType() != *I)
2649 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00002650 (*I)->getDescription() + "'");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002651 Args.push_back(ArgI->Val);
2652 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002653
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002654 if (Ty->isVarArg()) {
2655 if (I == E)
2656 for (; ArgI != ArgE; ++ArgI)
2657 Args.push_back(ArgI->Val); // push the remaining varargs
2658 } else if (I != E || ArgI != ArgE)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002659 GEN_ERROR("Invalid number of parameters detected");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002660 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002661
2662 // Create the InvokeInst
Chris Lattner8d22b4e2007-08-29 16:15:23 +00002663 InvokeInst *II = new InvokeInst(V, Normal, Except, Args.begin(), Args.end());
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002664 II->setCallingConv($2);
2665 $$ = II;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002666 delete $6;
Reid Spencer713eedc2006-08-18 08:43:06 +00002667 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002668 }
2669 | UNWIND {
2670 $$ = new UnwindInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002671 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002672 }
2673 | UNREACHABLE {
2674 $$ = new UnreachableInst();
Reid Spencer713eedc2006-08-18 08:43:06 +00002675 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002676 };
2677
2678
2679
2680JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2681 $$ = $1;
Reid Spencerd0e8d382007-03-19 18:40:50 +00002682 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer309080a2006-09-28 19:28:24 +00002683 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002684 if (V == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002685 GEN_ERROR("May only switch on a constant pool value");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002686
Reid Spencer309080a2006-09-28 19:28:24 +00002687 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002688 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002689 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002690 }
2691 | IntType ConstValueRef ',' LABEL ValueRef {
2692 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencerd0e8d382007-03-19 18:40:50 +00002693 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer309080a2006-09-28 19:28:24 +00002694 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002695
2696 if (V == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002697 GEN_ERROR("May only switch on a constant pool value");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002698
Reid Spencer309080a2006-09-28 19:28:24 +00002699 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer713eedc2006-08-18 08:43:06 +00002700 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002701 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002702 };
2703
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002704Inst : OptLocalAssign InstVal {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00002705 // Is this definition named?? if so, assign the name...
2706 setValueName($2, $1);
2707 CHECK_FOR_ERROR
2708 InsertValue($2);
2709 $$ = $2;
2710 CHECK_FOR_ERROR
2711 };
2712
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002713
2714PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002715 if (!UpRefs.empty())
2716 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002717 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencere2c32da2006-12-03 05:46:11 +00002718 Value* tmpVal = getVal(*$1, $3);
Reid Spencer713eedc2006-08-18 08:43:06 +00002719 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002720 BasicBlock* tmpBB = getBBVal($5);
2721 CHECK_FOR_ERROR
2722 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencere2c32da2006-12-03 05:46:11 +00002723 delete $1;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002724 }
2725 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2726 $$ = $1;
Reid Spencer309080a2006-09-28 19:28:24 +00002727 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002728 CHECK_FOR_ERROR
Reid Spencer309080a2006-09-28 19:28:24 +00002729 BasicBlock* tmpBB = getBBVal($6);
2730 CHECK_FOR_ERROR
2731 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002732 };
2733
2734
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002735ValueRefList : Types ValueRef OptParamAttrs {
2736 if (!UpRefs.empty())
2737 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2738 // Used for call and invoke instructions
2739 $$ = new ValueRefList();
2740 ValueRefListEntry E; E.Attrs = $3; E.Val = getVal($1->get(), $2);
2741 $$->push_back(E);
Reid Spencer6fb989c2007-03-20 01:13:36 +00002742 delete $1;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002743 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002744 | ValueRefList ',' Types ValueRef OptParamAttrs {
2745 if (!UpRefs.empty())
2746 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002747 $$ = $1;
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002748 ValueRefListEntry E; E.Attrs = $5; E.Val = getVal($3->get(), $4);
2749 $$->push_back(E);
Reid Spencer6fb989c2007-03-20 01:13:36 +00002750 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00002751 CHECK_FOR_ERROR
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002752 }
2753 | /*empty*/ { $$ = new ValueRefList(); };
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002754
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002755IndexList // Used for gep instructions and constant expressions
Reid Spencerd0da3e22006-12-31 21:47:02 +00002756 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002757 | IndexList ',' ResolvedVal {
2758 $$ = $1;
2759 $$->push_back($3);
2760 CHECK_FOR_ERROR
2761 }
Reid Spencerd0da3e22006-12-31 21:47:02 +00002762 ;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002763
2764OptTailCall : TAIL CALL {
2765 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002766 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002767 }
2768 | CALL {
2769 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002770 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002771 };
2772
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002773InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002774 if (!UpRefs.empty())
2775 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner03c49532007-01-15 02:27:26 +00002776 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencerd84d35b2007-02-15 02:26:10 +00002777 !isa<VectorType>((*$2).get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00002778 GEN_ERROR(
Reid Spencera7bb3e92007-02-05 10:18:06 +00002779 "Arithmetic operator requires integer, FP, or packed operands");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002780 if (isa<VectorType>((*$2).get()) &&
Reid Spencere2c32da2006-12-03 05:46:11 +00002781 ($1 == Instruction::URem ||
2782 $1 == Instruction::SRem ||
2783 $1 == Instruction::FRem))
Chris Lattner4669b0b2007-02-19 07:44:24 +00002784 GEN_ERROR("Remainder not supported on vector types");
Reid Spencere2c32da2006-12-03 05:46:11 +00002785 Value* val1 = getVal(*$2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002786 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002787 Value* val2 = getVal(*$2, $5);
Reid Spencer309080a2006-09-28 19:28:24 +00002788 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002789 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002790 if ($$ == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002791 GEN_ERROR("binary operator returned null");
Reid Spencere2c32da2006-12-03 05:46:11 +00002792 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002793 }
2794 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002795 if (!UpRefs.empty())
2796 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner03c49532007-01-15 02:27:26 +00002797 if (!(*$2)->isInteger()) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002798 if (Instruction::isShift($1) || !isa<VectorType>($2->get()) ||
2799 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencera7bb3e92007-02-05 10:18:06 +00002800 GEN_ERROR("Logical operator requires integral operands");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002801 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002802 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer309080a2006-09-28 19:28:24 +00002803 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002804 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer309080a2006-09-28 19:28:24 +00002805 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00002806 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002807 if ($$ == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002808 GEN_ERROR("binary operator returned null");
Reid Spencere2c32da2006-12-03 05:46:11 +00002809 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002810 }
Reid Spencere2c32da2006-12-03 05:46:11 +00002811 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002812 if (!UpRefs.empty())
2813 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencerd84d35b2007-02-15 02:26:10 +00002814 if (isa<VectorType>((*$3).get()))
Chris Lattner4669b0b2007-02-19 07:44:24 +00002815 GEN_ERROR("Vector types not supported by icmp instruction");
Reid Spencere2c32da2006-12-03 05:46:11 +00002816 Value* tmpVal1 = getVal(*$3, $4);
2817 CHECK_FOR_ERROR
2818 Value* tmpVal2 = getVal(*$3, $6);
2819 CHECK_FOR_ERROR
2820 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2821 if ($$ == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002822 GEN_ERROR("icmp operator returned null");
Reid Spencer6fb989c2007-03-20 01:13:36 +00002823 delete $3;
Reid Spencere2c32da2006-12-03 05:46:11 +00002824 }
2825 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002826 if (!UpRefs.empty())
2827 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencerd84d35b2007-02-15 02:26:10 +00002828 if (isa<VectorType>((*$3).get()))
Chris Lattner4669b0b2007-02-19 07:44:24 +00002829 GEN_ERROR("Vector types not supported by fcmp instruction");
Reid Spencere2c32da2006-12-03 05:46:11 +00002830 Value* tmpVal1 = getVal(*$3, $4);
2831 CHECK_FOR_ERROR
2832 Value* tmpVal2 = getVal(*$3, $6);
2833 CHECK_FOR_ERROR
2834 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2835 if ($$ == 0)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002836 GEN_ERROR("fcmp operator returned null");
Reid Spencer6fb989c2007-03-20 01:13:36 +00002837 delete $3;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002838 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002839 | CastOps ResolvedVal TO Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002840 if (!UpRefs.empty())
2841 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002842 Value* Val = $2;
Reid Spencerceb84592007-01-17 02:48:45 +00002843 const Type* DestTy = $4->get();
2844 if (!CastInst::castIsValid($1, Val, DestTy))
2845 GEN_ERROR("invalid cast opcode for cast from '" +
2846 Val->getType()->getDescription() + "' to '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00002847 DestTy->getDescription() + "'");
Reid Spencerceb84592007-01-17 02:48:45 +00002848 $$ = CastInst::create($1, Val, DestTy);
Reid Spencere2c32da2006-12-03 05:46:11 +00002849 delete $4;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002850 }
2851 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer542964f2007-01-11 18:21:29 +00002852 if ($2->getType() != Type::Int1Ty)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002853 GEN_ERROR("select condition must be boolean");
Reid Spencere2c32da2006-12-03 05:46:11 +00002854 if ($4->getType() != $6->getType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00002855 GEN_ERROR("select value types should match");
Reid Spencere2c32da2006-12-03 05:46:11 +00002856 $$ = new SelectInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002857 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002858 }
2859 | VAARG ResolvedVal ',' Types {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002860 if (!UpRefs.empty())
2861 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00002862 $$ = new VAArgInst($2, *$4);
2863 delete $4;
Reid Spencer713eedc2006-08-18 08:43:06 +00002864 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002865 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002866 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002867 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencera7bb3e92007-02-05 10:18:06 +00002868 GEN_ERROR("Invalid extractelement operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00002869 $$ = new ExtractElementInst($2, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00002870 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002871 }
2872 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002873 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencera7bb3e92007-02-05 10:18:06 +00002874 GEN_ERROR("Invalid insertelement operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00002875 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002876 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002877 }
Chris Lattner9ff96a72006-04-08 01:18:56 +00002878 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00002879 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencera7bb3e92007-02-05 10:18:06 +00002880 GEN_ERROR("Invalid shufflevector operands");
Reid Spencere2c32da2006-12-03 05:46:11 +00002881 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00002882 CHECK_FOR_ERROR
Chris Lattner9ff96a72006-04-08 01:18:56 +00002883 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002884 | PHI_TOK PHIList {
2885 const Type *Ty = $2->front().first->getType();
2886 if (!Ty->isFirstClassType())
Reid Spencera7bb3e92007-02-05 10:18:06 +00002887 GEN_ERROR("PHI node operands must be of first class type");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002888 $$ = new PHINode(Ty);
2889 ((PHINode*)$$)->reserveOperandSpace($2->size());
2890 while ($2->begin() != $2->end()) {
2891 if ($2->front().first->getType() != Ty)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002892 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002893 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
2894 $2->pop_front();
2895 }
2896 delete $2; // Free the list...
Reid Spencer713eedc2006-08-18 08:43:06 +00002897 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002898 }
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002899 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ValueRefList ')'
2900 OptFuncAttrs {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002901
2902 // Handle the short syntax
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002903 const PointerType *PFTy = 0;
2904 const FunctionType *Ty = 0;
Reid Spencerbf48e3c2007-01-05 17:07:23 +00002905 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002906 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2907 // Pull out the types of all of the arguments...
2908 std::vector<const Type*> ParamTypes;
Christopher Lamb3f706f22007-04-22 20:09:11 +00002909 ParamAttrsVector Attrs;
2910 if ($8 != ParamAttr::None) {
2911 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $8;
2912 Attrs.push_back(PAWI);
2913 }
Reid Spencerf51a7052007-04-09 06:16:21 +00002914 unsigned index = 1;
2915 ValueRefList::iterator I = $6->begin(), E = $6->end();
2916 for (; I != E; ++I, ++index) {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002917 const Type *Ty = I->Val->getType();
2918 if (Ty == Type::VoidTy)
2919 GEN_ERROR("Short call syntax cannot be used with varargs");
2920 ParamTypes.push_back(Ty);
Christopher Lamb3f706f22007-04-22 20:09:11 +00002921 if (I->Attrs != ParamAttr::None) {
2922 ParamAttrsWithIndex PAWI; PAWI.index = index; PAWI.attrs = I->Attrs;
2923 Attrs.push_back(PAWI);
2924 }
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002925 }
2926
Christopher Lamb3f706f22007-04-22 20:09:11 +00002927 ParamAttrsList *PAL = 0;
2928 if (!Attrs.empty())
2929 PAL = ParamAttrsList::get(Attrs);
Reid Spencerf51a7052007-04-09 06:16:21 +00002930
Christopher Lamb3f706f22007-04-22 20:09:11 +00002931 Ty = FunctionType::get($3->get(), ParamTypes, false, PAL);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002932 PFTy = PointerType::get(Ty);
2933 }
Chris Lattner720367c2007-04-26 05:31:05 +00002934
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002935 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer309080a2006-09-28 19:28:24 +00002936 CHECK_FOR_ERROR
Chris Lattner720367c2007-04-26 05:31:05 +00002937
Reid Spencere6a50a12007-04-16 06:56:07 +00002938 // Check for call to invalid intrinsic to avoid crashing later.
2939 if (Function *theF = dyn_cast<Function>(V)) {
Reid Spencer0ff60612007-04-16 22:02:23 +00002940 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
Reid Spencer660fa7b2007-04-16 20:35:38 +00002941 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
2942 !theF->getIntrinsicID(true))
Reid Spencere6a50a12007-04-16 06:56:07 +00002943 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
2944 theF->getName() + "'");
2945 }
2946
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002947 // Check the arguments
2948 ValueList Args;
2949 if ($6->empty()) { // Has no arguments?
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002950 // Make sure no arguments is a good thing!
2951 if (Ty->getNumParams() != 0)
Reid Spencer713eedc2006-08-18 08:43:06 +00002952 GEN_ERROR("No arguments passed to a function that "
Reid Spencera7bb3e92007-02-05 10:18:06 +00002953 "expects arguments");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002954 } else { // Has arguments?
2955 // Loop through FunctionType's arguments and ensure they are specified
2956 // correctly!
2957 //
2958 FunctionType::param_iterator I = Ty->param_begin();
2959 FunctionType::param_iterator E = Ty->param_end();
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002960 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002961
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002962 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2963 if (ArgI->Val->getType() != *I)
2964 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00002965 (*I)->getDescription() + "'");
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002966 Args.push_back(ArgI->Val);
2967 }
2968 if (Ty->isVarArg()) {
2969 if (I == E)
2970 for (; ArgI != ArgE; ++ArgI)
2971 Args.push_back(ArgI->Val); // push the remaining varargs
2972 } else if (I != E || ArgI != ArgE)
Reid Spencera7bb3e92007-02-05 10:18:06 +00002973 GEN_ERROR("Invalid number of parameters detected");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002974 }
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002975 // Create the call node
David Greenec8e65082007-08-01 03:59:32 +00002976 CallInst *CI = new CallInst(V, Args.begin(), Args.end());
Reid Spencer42f0cbe2006-12-31 05:40:51 +00002977 CI->setTailCall($1);
2978 CI->setCallingConv($2);
2979 $$ = CI;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002980 delete $6;
Reid Spencer8d6d4b8e2007-01-26 08:05:27 +00002981 delete $3;
Reid Spencer713eedc2006-08-18 08:43:06 +00002982 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002983 }
2984 | MemoryInst {
2985 $$ = $1;
Reid Spencer713eedc2006-08-18 08:43:06 +00002986 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002987 };
2988
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002989OptVolatile : VOLATILE {
2990 $$ = true;
Reid Spencer713eedc2006-08-18 08:43:06 +00002991 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002992 }
2993 | /* empty */ {
2994 $$ = false;
Reid Spencer713eedc2006-08-18 08:43:06 +00002995 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00002996 };
2997
2998
2999
3000MemoryInst : MALLOC Types OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00003001 if (!UpRefs.empty())
3002 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00003003 $$ = new MallocInst(*$2, 0, $3);
3004 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00003005 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003006 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +00003007 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00003008 if (!UpRefs.empty())
3009 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00003010 Value* tmpVal = getVal($4, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00003011 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00003012 $$ = new MallocInst(*$2, tmpVal, $6);
3013 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003014 }
3015 | ALLOCA Types OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00003016 if (!UpRefs.empty())
3017 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00003018 $$ = new AllocaInst(*$2, 0, $3);
3019 delete $2;
Reid Spencer713eedc2006-08-18 08:43:06 +00003020 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003021 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +00003022 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00003023 if (!UpRefs.empty())
3024 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00003025 Value* tmpVal = getVal($4, $5);
Reid Spencer713eedc2006-08-18 08:43:06 +00003026 CHECK_FOR_ERROR
Reid Spencere2c32da2006-12-03 05:46:11 +00003027 $$ = new AllocaInst(*$2, tmpVal, $6);
3028 delete $2;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003029 }
3030 | FREE ResolvedVal {
Reid Spencere2c32da2006-12-03 05:46:11 +00003031 if (!isa<PointerType>($2->getType()))
Reid Spencer713eedc2006-08-18 08:43:06 +00003032 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencera7bb3e92007-02-05 10:18:06 +00003033 $2->getType()->getDescription() + "");
Reid Spencere2c32da2006-12-03 05:46:11 +00003034 $$ = new FreeInst($2);
Reid Spencer713eedc2006-08-18 08:43:06 +00003035 CHECK_FOR_ERROR
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003036 }
3037
Christopher Lamb3f706f22007-04-22 20:09:11 +00003038 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00003039 if (!UpRefs.empty())
3040 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00003041 if (!isa<PointerType>($3->get()))
Reid Spencer713eedc2006-08-18 08:43:06 +00003042 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencere2c32da2006-12-03 05:46:11 +00003043 (*$3)->getDescription());
3044 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer713eedc2006-08-18 08:43:06 +00003045 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencere2c32da2006-12-03 05:46:11 +00003046 (*$3)->getDescription());
3047 Value* tmpVal = getVal(*$3, $4);
Reid Spencer713eedc2006-08-18 08:43:06 +00003048 CHECK_FOR_ERROR
Christopher Lamb3f706f22007-04-22 20:09:11 +00003049 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencere2c32da2006-12-03 05:46:11 +00003050 delete $3;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003051 }
Christopher Lamb3f706f22007-04-22 20:09:11 +00003052 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00003053 if (!UpRefs.empty())
3054 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00003055 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003056 if (!PT)
Reid Spencer713eedc2006-08-18 08:43:06 +00003057 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencere2c32da2006-12-03 05:46:11 +00003058 (*$5)->getDescription());
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003059 const Type *ElTy = PT->getElementType();
Reid Spencere2c32da2006-12-03 05:46:11 +00003060 if (ElTy != $3->getType())
3061 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencera7bb3e92007-02-05 10:18:06 +00003062 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003063
Reid Spencere2c32da2006-12-03 05:46:11 +00003064 Value* tmpVal = getVal(*$5, $6);
Reid Spencer713eedc2006-08-18 08:43:06 +00003065 CHECK_FOR_ERROR
Christopher Lamb3f706f22007-04-22 20:09:11 +00003066 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencere2c32da2006-12-03 05:46:11 +00003067 delete $5;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003068 }
3069 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer42f0cbe2006-12-31 05:40:51 +00003070 if (!UpRefs.empty())
3071 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencere2c32da2006-12-03 05:46:11 +00003072 if (!isa<PointerType>($2->get()))
Reid Spencera7bb3e92007-02-05 10:18:06 +00003073 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003074
David Greene85948d742007-09-04 18:46:50 +00003075 if (!GetElementPtrInst::getIndexedType(*$2, $4->begin(), $4->end(), true))
Reid Spencer713eedc2006-08-18 08:43:06 +00003076 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencera7bb3e92007-02-05 10:18:06 +00003077 (*$2)->getDescription()+ "'");
Reid Spencere2c32da2006-12-03 05:46:11 +00003078 Value* tmpVal = getVal(*$2, $3);
Reid Spencer713eedc2006-08-18 08:43:06 +00003079 CHECK_FOR_ERROR
David Greene85948d742007-09-04 18:46:50 +00003080 $$ = new GetElementPtrInst(tmpVal, $4->begin(), $4->end());
Reid Spencere2c32da2006-12-03 05:46:11 +00003081 delete $2;
Reid Spencer309080a2006-09-28 19:28:24 +00003082 delete $4;
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003083 };
3084
3085
3086%%
Reid Spencer713eedc2006-08-18 08:43:06 +00003087
Reid Spencer42f0cbe2006-12-31 05:40:51 +00003088// common code from the two 'RunVMAsmParser' functions
3089static Module* RunParser(Module * M) {
3090
3091 llvmAsmlineno = 1; // Reset the current line number...
3092 CurModule.CurrentModule = M;
3093#if YYDEBUG
3094 yydebug = Debug;
3095#endif
3096
3097 // Check to make sure the parser succeeded
3098 if (yyparse()) {
3099 if (ParserResult)
3100 delete ParserResult;
3101 return 0;
3102 }
3103
Reid Spenceref01c472007-03-30 01:37:39 +00003104 // Emit an error if there are any unresolved types left.
3105 if (!CurModule.LateResolveTypes.empty()) {
3106 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3107 if (DID.Type == ValID::LocalName) {
3108 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3109 } else {
3110 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3111 }
3112 if (ParserResult)
3113 delete ParserResult;
3114 return 0;
3115 }
3116
3117 // Emit an error if there are any unresolved values left.
3118 if (!CurModule.LateResolveValues.empty()) {
3119 Value *V = CurModule.LateResolveValues.back();
3120 std::map<Value*, std::pair<ValID, int> >::iterator I =
3121 CurModule.PlaceHolderInfo.find(V);
3122
3123 if (I != CurModule.PlaceHolderInfo.end()) {
3124 ValID &DID = I->second.first;
3125 if (DID.Type == ValID::LocalName) {
3126 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3127 } else {
3128 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3129 }
3130 if (ParserResult)
3131 delete ParserResult;
3132 return 0;
3133 }
3134 }
3135
Reid Spencer42f0cbe2006-12-31 05:40:51 +00003136 // Check to make sure that parsing produced a result
3137 if (!ParserResult)
3138 return 0;
3139
3140 // Reset ParserResult variable while saving its value for the result.
3141 Module *Result = ParserResult;
3142 ParserResult = 0;
3143
3144 return Result;
3145}
3146
Reid Spencer713eedc2006-08-18 08:43:06 +00003147void llvm::GenerateError(const std::string &message, int LineNo) {
3148 if (LineNo == -1) LineNo = llvmAsmlineno;
3149 // TODO: column number in exception
3150 if (TheParseError)
3151 TheParseError->setError(CurFilename, message, LineNo);
3152 TriggerError = 1;
3153}
3154
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003155int yyerror(const char *ErrorMsg) {
3156 std::string where
3157 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
3158 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Reid Spencer3aaaa0b2007-02-05 20:47:22 +00003159 std::string errMsg = where + "error: " + std::string(ErrorMsg);
3160 if (yychar != YYEMPTY && yychar != 0)
3161 errMsg += " while reading token: '" + std::string(llvmAsmtext, llvmAsmleng)+
3162 "'";
Reid Spencer713eedc2006-08-18 08:43:06 +00003163 GenerateError(errMsg);
Chris Lattnerf20e61f2006-02-15 07:22:58 +00003164 return 0;
3165}