blob: 9f317d8703f1bf1233c71b4adfb1aba2519c9e9f [file] [log] [blame]
Chris Lattner58af2a12006-02-15 07:22:58 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner58af2a12006-02-15 07:22:58 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the bison parser for LLVM assembly languages files.
11//
12//===----------------------------------------------------------------------===//
13
14%{
15#include "ParserInternals.h"
16#include "llvm/CallingConv.h"
17#include "llvm/InlineAsm.h"
18#include "llvm/Instructions.h"
19#include "llvm/Module.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000020#include "llvm/ValueSymbolTable.h"
Chandler Carruth02202192007-08-04 01:56:21 +000021#include "llvm/AutoUpgrade.h"
Chris Lattner58af2a12006-02-15 07:22:58 +000022#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer14310612006-12-31 05:40:51 +000023#include "llvm/Support/CommandLine.h"
Chris Lattnerf7469af2007-01-31 04:44:08 +000024#include "llvm/ADT/SmallVector.h"
Chris Lattner58af2a12006-02-15 07:22:58 +000025#include "llvm/ADT/STLExtras.h"
26#include "llvm/Support/MathExtras.h"
Reid Spencer481169e2006-12-01 00:33:46 +000027#include "llvm/Support/Streams.h"
Chris Lattner58af2a12006-02-15 07:22:58 +000028#include <algorithm>
Chris Lattner58af2a12006-02-15 07:22:58 +000029#include <list>
Chris Lattner8adde282007-02-11 21:40:10 +000030#include <map>
Chris Lattner58af2a12006-02-15 07:22:58 +000031#include <utility>
32
Reid Spencere4f47592006-08-18 17:32:55 +000033// The following is a gross hack. In order to rid the libAsmParser library of
34// exceptions, we have to have a way of getting the yyparse function to go into
35// an error situation. So, whenever we want an error to occur, the GenerateError
36// function (see bottom of file) sets TriggerError. Then, at the end of each
37// production in the grammer we use CHECK_FOR_ERROR which will invoke YYERROR
38// (a goto) to put YACC in error state. Furthermore, several calls to
39// GenerateError are made from inside productions and they must simulate the
40// previous exception behavior by exiting the production immediately. We have
41// replaced these with the GEN_ERROR macro which calls GeneratError and then
42// immediately invokes YYERROR. This would be so much cleaner if it was a
43// recursive descent parser.
Reid Spencer61c83e02006-08-18 08:43:06 +000044static bool TriggerError = false;
Reid Spencerf63697d2006-10-09 17:36:59 +000045#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYABORT; } }
Reid Spencer61c83e02006-08-18 08:43:06 +000046#define GEN_ERROR(msg) { GenerateError(msg); YYERROR; }
47
Chris Lattner58af2a12006-02-15 07:22:58 +000048int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
49int yylex(); // declaration" of xxx warnings.
50int yyparse();
Chris Lattner58af2a12006-02-15 07:22:58 +000051using namespace llvm;
52
53static Module *ParserResult;
54
55// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
56// relating to upreferences in the input stream.
57//
58//#define DEBUG_UPREFS 1
59#ifdef DEBUG_UPREFS
Bill Wendlinge8156192006-12-07 01:30:32 +000060#define UR_OUT(X) cerr << X
Chris Lattner58af2a12006-02-15 07:22:58 +000061#else
62#define UR_OUT(X)
63#endif
64
65#define YYERROR_VERBOSE 1
66
Chris Lattner58af2a12006-02-15 07:22:58 +000067static GlobalVariable *CurGV;
68
69
70// This contains info used when building the body of a function. It is
71// destroyed when the function is completed.
72//
73typedef std::vector<Value *> ValueList; // Numbered defs
Reid Spencer14310612006-12-31 05:40:51 +000074
Chris Lattner58af2a12006-02-15 07:22:58 +000075static void
Reid Spencer93c40032007-03-19 18:40:50 +000076ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers=0);
Chris Lattner58af2a12006-02-15 07:22:58 +000077
78static struct PerModuleInfo {
79 Module *CurrentModule;
Reid Spencer93c40032007-03-19 18:40:50 +000080 ValueList Values; // Module level numbered definitions
81 ValueList LateResolveValues;
Reid Spencer861d9d62006-11-28 07:29:44 +000082 std::vector<PATypeHolder> Types;
83 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner58af2a12006-02-15 07:22:58 +000084
85 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Chris Lattner0ad19702006-06-21 16:53:00 +000086 /// how they were referenced and on which line of the input they came from so
Chris Lattner58af2a12006-02-15 07:22:58 +000087 /// that we can resolve them later and print error messages as appropriate.
88 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
89
90 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
91 // references to global values. Global values may be referenced before they
92 // are defined, and if so, the temporary object that they represent is held
93 // here. This is used for forward references of GlobalValues.
94 //
95 typedef std::map<std::pair<const PointerType *,
96 ValID>, GlobalValue*> GlobalRefsType;
97 GlobalRefsType GlobalRefs;
98
99 void ModuleDone() {
100 // If we could not resolve some functions at function compilation time
101 // (calls to functions before they are defined), resolve them now... Types
102 // are resolved when the constant pool has been completely parsed.
103 //
104 ResolveDefinitions(LateResolveValues);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000105 if (TriggerError)
106 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000107
108 // Check to make sure that all global value forward references have been
109 // resolved!
110 //
111 if (!GlobalRefs.empty()) {
112 std::string UndefinedReferences = "Unresolved global references exist:\n";
113
114 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
115 I != E; ++I) {
116 UndefinedReferences += " " + I->first.first->getDescription() + " " +
117 I->first.second.getName() + "\n";
118 }
Reid Spencer61c83e02006-08-18 08:43:06 +0000119 GenerateError(UndefinedReferences);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000120 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000121 }
122
Chandler Carruth02202192007-08-04 01:56:21 +0000123 // Look for intrinsic functions and CallInst that need to be upgraded
124 for (Module::iterator FI = CurrentModule->begin(),
125 FE = CurrentModule->end(); FI != FE; )
126 UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove
127
Chris Lattner58af2a12006-02-15 07:22:58 +0000128 Values.clear(); // Clear out function local definitions
129 Types.clear();
130 CurrentModule = 0;
131 }
132
133 // GetForwardRefForGlobal - Check to see if there is a forward reference
134 // for this global. If so, remove it from the GlobalRefs map and return it.
135 // If not, just return null.
136 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
137 // Check to see if there is a forward reference to this global variable...
138 // if there is, eliminate it and patch the reference to use the new def'n.
139 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
140 GlobalValue *Ret = 0;
141 if (I != GlobalRefs.end()) {
142 Ret = I->second;
143 GlobalRefs.erase(I);
144 }
145 return Ret;
146 }
Reid Spencer8c8a2dc2007-01-02 21:54:12 +0000147
148 bool TypeIsUnresolved(PATypeHolder* PATy) {
149 // If it isn't abstract, its resolved
150 const Type* Ty = PATy->get();
151 if (!Ty->isAbstract())
152 return false;
153 // Traverse the type looking for abstract types. If it isn't abstract then
154 // we don't need to traverse that leg of the type.
155 std::vector<const Type*> WorkList, SeenList;
156 WorkList.push_back(Ty);
157 while (!WorkList.empty()) {
158 const Type* Ty = WorkList.back();
159 SeenList.push_back(Ty);
160 WorkList.pop_back();
161 if (const OpaqueType* OpTy = dyn_cast<OpaqueType>(Ty)) {
162 // Check to see if this is an unresolved type
163 std::map<ValID, PATypeHolder>::iterator I = LateResolveTypes.begin();
164 std::map<ValID, PATypeHolder>::iterator E = LateResolveTypes.end();
165 for ( ; I != E; ++I) {
166 if (I->second.get() == OpTy)
167 return true;
168 }
169 } else if (const SequentialType* SeqTy = dyn_cast<SequentialType>(Ty)) {
170 const Type* TheTy = SeqTy->getElementType();
171 if (TheTy->isAbstract() && TheTy != Ty) {
172 std::vector<const Type*>::iterator I = SeenList.begin(),
173 E = SeenList.end();
174 for ( ; I != E; ++I)
175 if (*I == TheTy)
176 break;
177 if (I == E)
178 WorkList.push_back(TheTy);
179 }
180 } else if (const StructType* StrTy = dyn_cast<StructType>(Ty)) {
181 for (unsigned i = 0; i < StrTy->getNumElements(); ++i) {
182 const Type* TheTy = StrTy->getElementType(i);
183 if (TheTy->isAbstract() && TheTy != Ty) {
184 std::vector<const Type*>::iterator I = SeenList.begin(),
185 E = SeenList.end();
186 for ( ; I != E; ++I)
187 if (*I == TheTy)
188 break;
189 if (I == E)
190 WorkList.push_back(TheTy);
191 }
192 }
193 }
194 }
195 return false;
196 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000197} CurModule;
198
199static struct PerFunctionInfo {
200 Function *CurrentFunction; // Pointer to current function being created
201
Reid Spencer93c40032007-03-19 18:40:50 +0000202 ValueList Values; // Keep track of #'d definitions
203 unsigned NextValNum;
204 ValueList LateResolveValues;
Reid Spenceref9b9a72007-02-05 20:47:22 +0000205 bool isDeclare; // Is this function a forward declararation?
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000206 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000207 GlobalValue::VisibilityTypes Visibility;
Chris Lattner58af2a12006-02-15 07:22:58 +0000208
209 /// BBForwardRefs - When we see forward references to basic blocks, keep
210 /// track of them here.
Reid Spencer93c40032007-03-19 18:40:50 +0000211 std::map<ValID, BasicBlock*> BBForwardRefs;
Chris Lattner58af2a12006-02-15 07:22:58 +0000212
213 inline PerFunctionInfo() {
214 CurrentFunction = 0;
215 isDeclare = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000216 Linkage = GlobalValue::ExternalLinkage;
217 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner58af2a12006-02-15 07:22:58 +0000218 }
219
220 inline void FunctionStart(Function *M) {
221 CurrentFunction = M;
Reid Spencer93c40032007-03-19 18:40:50 +0000222 NextValNum = 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000223 }
224
225 void FunctionDone() {
Chris Lattner58af2a12006-02-15 07:22:58 +0000226 // Any forward referenced blocks left?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000227 if (!BBForwardRefs.empty()) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000228 GenerateError("Undefined reference to label " +
Reid Spencer93c40032007-03-19 18:40:50 +0000229 BBForwardRefs.begin()->second->getName());
Reid Spencer5b7e7532006-09-28 19:28:24 +0000230 return;
231 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000232
233 // Resolve all forward references now.
234 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
235
236 Values.clear(); // Clear out function local definitions
Reid Spencer93c40032007-03-19 18:40:50 +0000237 BBForwardRefs.clear();
Chris Lattner58af2a12006-02-15 07:22:58 +0000238 CurrentFunction = 0;
239 isDeclare = false;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000240 Linkage = GlobalValue::ExternalLinkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000241 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner58af2a12006-02-15 07:22:58 +0000242 }
243} CurFun; // Info for the current function...
244
245static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
246
247
248//===----------------------------------------------------------------------===//
249// Code to handle definitions of all the types
250//===----------------------------------------------------------------------===//
251
Reid Spencer93c40032007-03-19 18:40:50 +0000252static void InsertValue(Value *V, ValueList &ValueTab = CurFun.Values) {
253 // Things that have names or are void typed don't get slot numbers
254 if (V->hasName() || (V->getType() == Type::VoidTy))
255 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000256
Reid Spencer93c40032007-03-19 18:40:50 +0000257 // In the case of function values, we have to allow for the forward reference
258 // of basic blocks, which are included in the numbering. Consequently, we keep
259 // track of the next insertion location with NextValNum. When a BB gets
260 // inserted, it could change the size of the CurFun.Values vector.
261 if (&ValueTab == &CurFun.Values) {
262 if (ValueTab.size() <= CurFun.NextValNum)
263 ValueTab.resize(CurFun.NextValNum+1);
264 ValueTab[CurFun.NextValNum++] = V;
265 return;
266 }
267 // For all other lists, its okay to just tack it on the back of the vector.
268 ValueTab.push_back(V);
Chris Lattner58af2a12006-02-15 07:22:58 +0000269}
270
271static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
272 switch (D.Type) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000273 case ValID::LocalID: // Is it a numbered definition?
Chris Lattner58af2a12006-02-15 07:22:58 +0000274 // Module constants occupy the lowest numbered slots...
Reid Spencer41dff5e2007-01-26 08:05:27 +0000275 if (D.Num < CurModule.Types.size())
276 return CurModule.Types[D.Num];
Chris Lattner58af2a12006-02-15 07:22:58 +0000277 break;
Reid Spencer41dff5e2007-01-26 08:05:27 +0000278 case ValID::LocalName: // Is it a named definition?
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000279 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.getName())) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000280 D.destroy(); // Free old strdup'd memory...
281 return N;
282 }
283 break;
284 default:
Reid Spencerb5334b02007-02-05 10:18:06 +0000285 GenerateError("Internal parser error: Invalid symbol type reference");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000286 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000287 }
288
289 // If we reached here, we referenced either a symbol that we don't know about
290 // or an id number that hasn't been read yet. We may be referencing something
291 // forward, so just create an entry to be resolved later and get to it...
292 //
293 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
294
295
296 if (inFunctionScope()) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000297 if (D.Type == ValID::LocalName) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000298 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000299 return 0;
300 } else {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000301 GenerateError("Reference to an undefined type: #" + utostr(D.Num));
Reid Spencer5b7e7532006-09-28 19:28:24 +0000302 return 0;
303 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000304 }
305
Reid Spencer861d9d62006-11-28 07:29:44 +0000306 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Chris Lattner58af2a12006-02-15 07:22:58 +0000307 if (I != CurModule.LateResolveTypes.end())
Reid Spencer861d9d62006-11-28 07:29:44 +0000308 return I->second;
Chris Lattner58af2a12006-02-15 07:22:58 +0000309
Reid Spencer861d9d62006-11-28 07:29:44 +0000310 Type *Typ = OpaqueType::get();
311 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
312 return Typ;
Reid Spencera132e042006-12-03 05:46:11 +0000313 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000314
Reid Spencer93c40032007-03-19 18:40:50 +0000315// getExistingVal - Look up the value specified by the provided type and
Chris Lattner58af2a12006-02-15 07:22:58 +0000316// the provided ValID. If the value exists and has already been defined, return
317// it. Otherwise return null.
318//
Reid Spencer93c40032007-03-19 18:40:50 +0000319static Value *getExistingVal(const Type *Ty, const ValID &D) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000320 if (isa<FunctionType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000321 GenerateError("Functions are not values and "
Chris Lattner58af2a12006-02-15 07:22:58 +0000322 "must be referenced as pointers");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000323 return 0;
324 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000325
326 switch (D.Type) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000327 case ValID::LocalID: { // Is it a numbered definition?
Reid Spencer41dff5e2007-01-26 08:05:27 +0000328 // Check that the number is within bounds.
Reid Spencer93c40032007-03-19 18:40:50 +0000329 if (D.Num >= CurFun.Values.size())
330 return 0;
331 Value *Result = CurFun.Values[D.Num];
332 if (Ty != Result->getType()) {
333 GenerateError("Numbered value (%" + utostr(D.Num) + ") of type '" +
334 Result->getType()->getDescription() + "' does not match "
335 "expected type, '" + Ty->getDescription() + "'");
336 return 0;
337 }
338 return Result;
Reid Spencer41dff5e2007-01-26 08:05:27 +0000339 }
340 case ValID::GlobalID: { // Is it a numbered definition?
Reid Spencer93c40032007-03-19 18:40:50 +0000341 if (D.Num >= CurModule.Values.size())
Reid Spenceref9b9a72007-02-05 20:47:22 +0000342 return 0;
Reid Spencer93c40032007-03-19 18:40:50 +0000343 Value *Result = CurModule.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() + "'");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000348 return 0;
Reid Spencer93c40032007-03-19 18:40:50 +0000349 }
350 return Result;
Chris Lattner58af2a12006-02-15 07:22:58 +0000351 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000352
353 case ValID::LocalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000354 if (!inFunctionScope())
355 return 0;
356 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000357 Value *N = SymTab.lookup(D.getName());
Reid Spenceref9b9a72007-02-05 20:47:22 +0000358 if (N == 0)
359 return 0;
360 if (N->getType() != Ty)
361 return 0;
Reid Spencer41dff5e2007-01-26 08:05:27 +0000362
363 D.destroy(); // Free old strdup'd memory...
364 return N;
365 }
366 case ValID::GlobalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000367 ValueSymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable();
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000368 Value *N = SymTab.lookup(D.getName());
Reid Spenceref9b9a72007-02-05 20:47:22 +0000369 if (N == 0)
370 return 0;
371 if (N->getType() != Ty)
372 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000373
374 D.destroy(); // Free old strdup'd memory...
375 return N;
376 }
377
378 // Check to make sure that "Ty" is an integral type, and that our
379 // value will fit into the specified type...
380 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattner38905612008-02-19 04:36:25 +0000381 if (!isa<IntegerType>(Ty) ||
382 !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000383 GenerateError("Signed integral constant '" +
Chris Lattner58af2a12006-02-15 07:22:58 +0000384 itostr(D.ConstPool64) + "' is invalid for type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000385 Ty->getDescription() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000386 return 0;
387 }
Reid Spencer49d273e2007-03-19 20:40:51 +0000388 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner58af2a12006-02-15 07:22:58 +0000389
390 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattner38905612008-02-19 04:36:25 +0000391 if (isa<IntegerType>(Ty) &&
392 ConstantInt::isValueValidForType(Ty, D.UConstPool64))
Reid Spencerb83eb642006-10-20 07:07:24 +0000393 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattner38905612008-02-19 04:36:25 +0000394
395 if (!isa<IntegerType>(Ty) ||
396 !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
397 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
398 "' is invalid or out of range for type '" +
399 Ty->getDescription() + "'");
400 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000401 }
Chris Lattner38905612008-02-19 04:36:25 +0000402 // This is really a signed reference. Transmogrify.
403 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner58af2a12006-02-15 07:22:58 +0000404
Chris Lattner1913b942008-07-11 00:30:39 +0000405 case ValID::ConstAPInt: // Is it an unsigned const pool reference?
406 if (!isa<IntegerType>(Ty)) {
407 GenerateError("Integral constant '" + D.getName() +
408 "' is invalid or out of range for type '" +
409 Ty->getDescription() + "'");
410 return 0;
411 }
412
413 {
414 APSInt Tmp = *D.ConstPoolInt;
415 Tmp.extOrTrunc(Ty->getPrimitiveSizeInBits());
416 return ConstantInt::get(Tmp);
417 }
418
Chris Lattner58af2a12006-02-15 07:22:58 +0000419 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattner38905612008-02-19 04:36:25 +0000420 if (!Ty->isFloatingPoint() ||
421 !ConstantFP::isValueValidForType(Ty, *D.ConstPoolFP)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000422 GenerateError("FP constant invalid for type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000423 return 0;
424 }
Chris Lattnerd8eb63f2008-04-20 00:41:19 +0000425 // Lexer has no type info, so builds all float and double FP constants
Dale Johannesenc72cd7e2007-09-11 18:33:39 +0000426 // as double. Fix this here. Long double does not need this.
427 if (&D.ConstPoolFP->getSemantics() == &APFloat::IEEEdouble &&
428 Ty==Type::FloatTy)
Dale Johannesen43421b32007-09-06 18:13:44 +0000429 D.ConstPoolFP->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
Chris Lattnerd8eb63f2008-04-20 00:41:19 +0000430 return ConstantFP::get(*D.ConstPoolFP);
Chris Lattner58af2a12006-02-15 07:22:58 +0000431
432 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000433 if (!isa<PointerType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000434 GenerateError("Cannot create a a non pointer null");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000435 return 0;
436 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000437 return ConstantPointerNull::get(cast<PointerType>(Ty));
438
439 case ValID::ConstUndefVal: // Is it an undef value?
440 return UndefValue::get(Ty);
441
442 case ValID::ConstZeroVal: // Is it a zero value?
443 return Constant::getNullValue(Ty);
444
445 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000446 if (D.ConstantValue->getType() != Ty) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000447 GenerateError("Constant expression type different from required type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000448 return 0;
449 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000450 return D.ConstantValue;
451
452 case ValID::InlineAsmVal: { // Inline asm expression
453 const PointerType *PTy = dyn_cast<PointerType>(Ty);
454 const FunctionType *FTy =
455 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000456 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000457 GenerateError("Invalid type for asm constraint string");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000458 return 0;
459 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000460 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
461 D.IAD->HasSideEffects);
462 D.destroy(); // Free InlineAsmDescriptor.
463 return IA;
464 }
465 default:
Reid Spencera9720f52007-02-05 17:04:00 +0000466 assert(0 && "Unhandled case!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000467 return 0;
468 } // End of switch
469
Reid Spencera9720f52007-02-05 17:04:00 +0000470 assert(0 && "Unhandled case!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000471 return 0;
472}
473
Reid Spencer93c40032007-03-19 18:40:50 +0000474// getVal - This function is identical to getExistingVal, except that if a
Chris Lattner58af2a12006-02-15 07:22:58 +0000475// value is not already defined, it "improvises" by creating a placeholder var
476// that looks and acts just like the requested variable. When the value is
477// defined later, all uses of the placeholder variable are replaced with the
478// real thing.
479//
480static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000481 if (Ty == Type::LabelTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000482 GenerateError("Cannot use a basic block here");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000483 return 0;
484 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000485
486 // See if the value has already been defined.
Reid Spencer93c40032007-03-19 18:40:50 +0000487 Value *V = getExistingVal(Ty, ID);
Chris Lattner58af2a12006-02-15 07:22:58 +0000488 if (V) return V;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000489 if (TriggerError) return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000490
Reid Spencer5b7e7532006-09-28 19:28:24 +0000491 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Dan Gohmane4977cf2008-05-23 01:55:30 +0000492 GenerateError("Invalid use of a non-first-class type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000493 return 0;
494 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000495
496 // If we reached here, we referenced either a symbol that we don't know about
497 // or an id number that hasn't been read yet. We may be referencing something
498 // forward, so just create an entry to be resolved later and get to it...
499 //
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000500 switch (ID.Type) {
501 case ValID::GlobalName:
Reid Spencer9c9b63a2007-04-28 16:07:31 +0000502 case ValID::GlobalID: {
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000503 const PointerType *PTy = dyn_cast<PointerType>(Ty);
504 if (!PTy) {
505 GenerateError("Invalid type for reference to global" );
506 return 0;
507 }
508 const Type* ElTy = PTy->getElementType();
509 if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy))
Gabor Greife64d2482008-04-06 23:07:54 +0000510 V = Function::Create(FTy, GlobalValue::ExternalLinkage);
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000511 else
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000512 V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage, 0, "",
513 (Module*)0, false, PTy->getAddressSpace());
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000514 break;
Reid Spencer9c9b63a2007-04-28 16:07:31 +0000515 }
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000516 default:
517 V = new Argument(Ty);
518 }
519
Chris Lattner58af2a12006-02-15 07:22:58 +0000520 // Remember where this forward reference came from. FIXME, shouldn't we try
521 // to recycle these things??
522 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Duncan Sandsdc024672007-11-27 13:23:08 +0000523 LLLgetLineNo())));
Chris Lattner58af2a12006-02-15 07:22:58 +0000524
525 if (inFunctionScope())
526 InsertValue(V, CurFun.LateResolveValues);
527 else
528 InsertValue(V, CurModule.LateResolveValues);
529 return V;
530}
531
Reid Spencer93c40032007-03-19 18:40:50 +0000532/// defineBBVal - This is a definition of a new basic block with the specified
533/// identifier which must be the same as CurFun.NextValNum, if its numeric.
Nick Lewycky280a6e62008-04-25 16:53:59 +0000534static BasicBlock *defineBBVal(const ValID &ID) {
Reid Spencera9720f52007-02-05 17:04:00 +0000535 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000536
Chris Lattner58af2a12006-02-15 07:22:58 +0000537 BasicBlock *BB = 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000538
Reid Spencer93c40032007-03-19 18:40:50 +0000539 // First, see if this was forward referenced
Chris Lattner58af2a12006-02-15 07:22:58 +0000540
Reid Spencer93c40032007-03-19 18:40:50 +0000541 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
542 if (BBI != CurFun.BBForwardRefs.end()) {
543 BB = BBI->second;
Chris Lattner58af2a12006-02-15 07:22:58 +0000544 // The forward declaration could have been inserted anywhere in the
545 // function: insert it into the correct place now.
546 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
547 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Reid Spencer93c40032007-03-19 18:40:50 +0000548
Reid Spencer66728ef2007-03-20 01:13:36 +0000549 // We're about to erase the entry, save the key so we can clean it up.
550 ValID Tmp = BBI->first;
551
Reid Spencer93c40032007-03-19 18:40:50 +0000552 // Erase the forward ref from the map as its no longer "forward"
553 CurFun.BBForwardRefs.erase(ID);
554
Reid Spencer66728ef2007-03-20 01:13:36 +0000555 // The key has been removed from the map but so we don't want to leave
556 // strdup'd memory around so destroy it too.
557 Tmp.destroy();
558
Reid Spencer93c40032007-03-19 18:40:50 +0000559 // If its a numbered definition, bump the number and set the BB value.
560 if (ID.Type == ValID::LocalID) {
561 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
562 InsertValue(BB);
563 }
Devang Patel67909432008-03-03 18:58:47 +0000564 } else {
565 // We haven't seen this BB before and its first mention is a definition.
566 // Just create it and return it.
567 std::string Name (ID.Type == ValID::LocalName ? ID.getName() : "");
Gabor Greife64d2482008-04-06 23:07:54 +0000568 BB = BasicBlock::Create(Name, CurFun.CurrentFunction);
Devang Patel67909432008-03-03 18:58:47 +0000569 if (ID.Type == ValID::LocalID) {
570 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
571 InsertValue(BB);
572 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000573 }
Reid Spencer93c40032007-03-19 18:40:50 +0000574
Devang Patel67909432008-03-03 18:58:47 +0000575 ID.destroy();
Reid Spencer93c40032007-03-19 18:40:50 +0000576 return BB;
577}
578
579/// getBBVal - get an existing BB value or create a forward reference for it.
580///
581static BasicBlock *getBBVal(const ValID &ID) {
582 assert(inFunctionScope() && "Can't get basic block at global scope!");
583
584 BasicBlock *BB = 0;
585
586 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
587 if (BBI != CurFun.BBForwardRefs.end()) {
588 BB = BBI->second;
589 } if (ID.Type == ValID::LocalName) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000590 std::string Name = ID.getName();
Reid Spencer93c40032007-03-19 18:40:50 +0000591 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000592 if (N) {
Reid Spencer93c40032007-03-19 18:40:50 +0000593 if (N->getType()->getTypeID() == Type::LabelTyID)
594 BB = cast<BasicBlock>(N);
595 else
596 GenerateError("Reference to label '" + Name + "' is actually of type '"+
597 N->getType()->getDescription() + "'");
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000598 }
Reid Spencer93c40032007-03-19 18:40:50 +0000599 } else if (ID.Type == ValID::LocalID) {
600 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
601 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
602 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
603 else
604 GenerateError("Reference to label '%" + utostr(ID.Num) +
605 "' is actually of type '"+
606 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
607 }
608 } else {
609 GenerateError("Illegal label reference " + ID.getName());
610 return 0;
611 }
612
613 // If its already been defined, return it now.
614 if (BB) {
615 ID.destroy(); // Free strdup'd memory.
616 return BB;
617 }
618
619 // Otherwise, this block has not been seen before, create it.
620 std::string Name;
621 if (ID.Type == ValID::LocalName)
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000622 Name = ID.getName();
Gabor Greife64d2482008-04-06 23:07:54 +0000623 BB = BasicBlock::Create(Name, CurFun.CurrentFunction);
Reid Spencer93c40032007-03-19 18:40:50 +0000624
625 // Insert it in the forward refs map.
626 CurFun.BBForwardRefs[ID] = BB;
627
Chris Lattner58af2a12006-02-15 07:22:58 +0000628 return BB;
629}
630
631
632//===----------------------------------------------------------------------===//
633// Code to handle forward references in instructions
634//===----------------------------------------------------------------------===//
635//
636// This code handles the late binding needed with statements that reference
637// values not defined yet... for example, a forward branch, or the PHI node for
638// a loop body.
639//
640// This keeps a table (CurFun.LateResolveValues) of all such forward references
641// and back patchs after we are done.
642//
643
644// ResolveDefinitions - If we could not resolve some defs at parsing
645// time (forward branches, phi functions for loops, etc...) resolve the
646// defs now...
647//
648static void
Reid Spencer93c40032007-03-19 18:40:50 +0000649ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000650 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencer93c40032007-03-19 18:40:50 +0000651 while (!LateResolvers.empty()) {
652 Value *V = LateResolvers.back();
653 LateResolvers.pop_back();
Chris Lattner58af2a12006-02-15 07:22:58 +0000654
Reid Spencer93c40032007-03-19 18:40:50 +0000655 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
656 CurModule.PlaceHolderInfo.find(V);
657 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000658
Reid Spencer93c40032007-03-19 18:40:50 +0000659 ValID &DID = PHI->second.first;
Chris Lattner58af2a12006-02-15 07:22:58 +0000660
Reid Spencer93c40032007-03-19 18:40:50 +0000661 Value *TheRealValue = getExistingVal(V->getType(), DID);
662 if (TriggerError)
663 return;
664 if (TheRealValue) {
665 V->replaceAllUsesWith(TheRealValue);
666 delete V;
667 CurModule.PlaceHolderInfo.erase(PHI);
668 } else if (FutureLateResolvers) {
669 // Functions have their unresolved items forwarded to the module late
670 // resolver table
671 InsertValue(V, *FutureLateResolvers);
672 } else {
673 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
674 GenerateError("Reference to an invalid definition: '" +DID.getName()+
675 "' of type '" + V->getType()->getDescription() + "'",
676 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000677 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000678 } else {
Reid Spencer93c40032007-03-19 18:40:50 +0000679 GenerateError("Reference to an invalid definition: #" +
680 itostr(DID.Num) + " of type '" +
681 V->getType()->getDescription() + "'",
682 PHI->second.second);
683 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000684 }
685 }
686 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000687 LateResolvers.clear();
688}
689
690// ResolveTypeTo - A brand new type was just declared. This means that (if
691// name is not null) things referencing Name can be resolved. Otherwise, things
692// refering to the number can be resolved. Do this now.
693//
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000694static void ResolveTypeTo(std::string *Name, const Type *ToTy) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000695 ValID D;
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000696 if (Name)
697 D = ValID::createLocalName(*Name);
698 else
699 D = ValID::createLocalID(CurModule.Types.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000700
Reid Spencer861d9d62006-11-28 07:29:44 +0000701 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattner58af2a12006-02-15 07:22:58 +0000702 CurModule.LateResolveTypes.find(D);
703 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencer861d9d62006-11-28 07:29:44 +0000704 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner58af2a12006-02-15 07:22:58 +0000705 CurModule.LateResolveTypes.erase(I);
706 }
707}
708
709// setValueName - Set the specified value to the name given. The name may be
710// null potentially, in which case this is a noop. The string passed in is
711// assumed to be a malloc'd string buffer, and is free'd by this function.
712//
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000713static void setValueName(Value *V, std::string *NameStr) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000714 if (!NameStr) return;
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000715 std::string Name(*NameStr); // Copy string
716 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000717
Reid Spencer41dff5e2007-01-26 08:05:27 +0000718 if (V->getType() == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000719 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000720 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000721 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000722
Reid Spencera9720f52007-02-05 17:04:00 +0000723 assert(inFunctionScope() && "Must be in function scope!");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000724 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
725 if (ST.lookup(Name)) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000726 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000727 V->getType()->getDescription() + "'");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000728 return;
729 }
730
731 // Set the name.
732 V->setName(Name);
Chris Lattner58af2a12006-02-15 07:22:58 +0000733}
734
735/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
736/// this is a declaration, otherwise it is a definition.
737static GlobalVariable *
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000738ParseGlobalVariable(std::string *NameStr,
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000739 GlobalValue::LinkageTypes Linkage,
740 GlobalValue::VisibilityTypes Visibility,
Chris Lattner58af2a12006-02-15 07:22:58 +0000741 bool isConstantGlobal, const Type *Ty,
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000742 Constant *Initializer, bool IsThreadLocal,
743 unsigned AddressSpace = 0) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000744 if (isa<FunctionType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000745 GenerateError("Cannot declare global vars of function type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000746 return 0;
747 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000748 if (Ty == Type::LabelTy) {
749 GenerateError("Cannot declare global vars of label type");
750 return 0;
751 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000752
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000753 const PointerType *PTy = PointerType::get(Ty, AddressSpace);
Chris Lattner58af2a12006-02-15 07:22:58 +0000754
755 std::string Name;
756 if (NameStr) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000757 Name = *NameStr; // Copy string
758 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000759 }
760
761 // See if this global value was forward referenced. If so, recycle the
762 // object.
763 ValID ID;
764 if (!Name.empty()) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000765 ID = ValID::createGlobalName(Name);
Chris Lattner58af2a12006-02-15 07:22:58 +0000766 } else {
Reid Spencer93c40032007-03-19 18:40:50 +0000767 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000768 }
769
770 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
771 // Move the global to the end of the list, from whereever it was
772 // previously inserted.
773 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
774 CurModule.CurrentModule->getGlobalList().remove(GV);
775 CurModule.CurrentModule->getGlobalList().push_back(GV);
776 GV->setInitializer(Initializer);
777 GV->setLinkage(Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000778 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000779 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000780 GV->setThreadLocal(IsThreadLocal);
Chris Lattner58af2a12006-02-15 07:22:58 +0000781 InsertValue(GV, CurModule.Values);
782 return GV;
783 }
784
Reid Spenceref9b9a72007-02-05 20:47:22 +0000785 // If this global has a name
Chris Lattner58af2a12006-02-15 07:22:58 +0000786 if (!Name.empty()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000787 // if the global we're parsing has an initializer (is a definition) and
788 // has external linkage.
789 if (Initializer && Linkage != GlobalValue::InternalLinkage)
790 // If there is already a global with external linkage with this name
791 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
792 // If we allow this GVar to get created, it will be renamed in the
793 // symbol table because it conflicts with an existing GVar. We can't
794 // allow redefinition of GVars whose linking indicates that their name
795 // must stay the same. Issue the error.
796 GenerateError("Redefinition of global variable named '" + Name +
797 "' of type '" + Ty->getDescription() + "'");
798 return 0;
799 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000800 }
801
802 // Otherwise there is no existing GV to use, create one now.
803 GlobalVariable *GV =
804 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000805 CurModule.CurrentModule, IsThreadLocal, AddressSpace);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000806 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000807 InsertValue(GV, CurModule.Values);
808 return GV;
809}
810
811// setTypeName - Set the specified type to the name given. The name may be
812// null potentially, in which case this is a noop. The string passed in is
813// assumed to be a malloc'd string buffer, and is freed by this function.
814//
815// This function returns true if the type has already been defined, but is
816// allowed to be redefined in the specified context. If the name is a new name
817// for the type plane, it is inserted and false is returned.
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000818static bool setTypeName(const Type *T, std::string *NameStr) {
Reid Spencera9720f52007-02-05 17:04:00 +0000819 assert(!inFunctionScope() && "Can't give types function-local names!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000820 if (NameStr == 0) return false;
821
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000822 std::string Name(*NameStr); // Copy string
823 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000824
825 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000826 if (T == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000827 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000828 return false;
829 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000830
831 // Set the type name, checking for conflicts as we do so.
832 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
833
834 if (AlreadyExists) { // Inserting a name that is already defined???
835 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencera9720f52007-02-05 17:04:00 +0000836 assert(Existing && "Conflict but no matching type?!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000837
838 // There is only one case where this is allowed: when we are refining an
839 // opaque type. In this case, Existing will be an opaque type.
840 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
841 // We ARE replacing an opaque type!
842 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
843 return true;
844 }
845
846 // Otherwise, this is an attempt to redefine a type. That's okay if
847 // the redefinition is identical to the original. This will be so if
848 // Existing and T point to the same Type object. In this one case we
849 // allow the equivalent redefinition.
850 if (Existing == T) return true; // Yes, it's equal.
851
852 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer63c34452007-01-05 21:51:07 +0000853 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000854 T->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +0000855 }
856
857 return false;
858}
859
860//===----------------------------------------------------------------------===//
861// Code for handling upreferences in type names...
862//
863
864// TypeContains - Returns true if Ty directly contains E in it.
865//
866static bool TypeContains(const Type *Ty, const Type *E) {
867 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
868 E) != Ty->subtype_end();
869}
870
871namespace {
872 struct UpRefRecord {
873 // NestingLevel - The number of nesting levels that need to be popped before
874 // this type is resolved.
875 unsigned NestingLevel;
876
877 // LastContainedTy - This is the type at the current binding level for the
878 // type. Every time we reduce the nesting level, this gets updated.
879 const Type *LastContainedTy;
880
881 // UpRefTy - This is the actual opaque type that the upreference is
882 // represented with.
883 OpaqueType *UpRefTy;
884
885 UpRefRecord(unsigned NL, OpaqueType *URTy)
886 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
887 };
888}
889
890// UpRefs - A list of the outstanding upreferences that need to be resolved.
891static std::vector<UpRefRecord> UpRefs;
892
893/// HandleUpRefs - Every time we finish a new layer of types, this function is
894/// called. It loops through the UpRefs vector, which is a list of the
895/// currently active types. For each type, if the up reference is contained in
896/// the newly completed type, we decrement the level count. When the level
897/// count reaches zero, the upreferenced type is the type that is passed in:
898/// thus we can complete the cycle.
899///
900static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner224f84f2006-08-18 17:34:45 +0000901 // If Ty isn't abstract, or if there are no up-references in it, then there is
902 // nothing to resolve here.
903 if (!ty->isAbstract() || UpRefs.empty()) return ty;
904
Chris Lattner58af2a12006-02-15 07:22:58 +0000905 PATypeHolder Ty(ty);
906 UR_OUT("Type '" << Ty->getDescription() <<
907 "' newly formed. Resolving upreferences.\n" <<
908 UpRefs.size() << " upreferences active!\n");
909
910 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
911 // to zero), we resolve them all together before we resolve them to Ty. At
912 // the end of the loop, if there is anything to resolve to Ty, it will be in
913 // this variable.
914 OpaqueType *TypeToResolve = 0;
915
916 for (unsigned i = 0; i != UpRefs.size(); ++i) {
917 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
918 << UpRefs[i].second->getDescription() << ") = "
919 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
920 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
921 // Decrement level of upreference
922 unsigned Level = --UpRefs[i].NestingLevel;
923 UpRefs[i].LastContainedTy = Ty;
924 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
925 if (Level == 0) { // Upreference should be resolved!
926 if (!TypeToResolve) {
927 TypeToResolve = UpRefs[i].UpRefTy;
928 } else {
929 UR_OUT(" * Resolving upreference for "
930 << UpRefs[i].second->getDescription() << "\n";
931 std::string OldName = UpRefs[i].UpRefTy->getDescription());
932 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
933 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
934 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
935 }
936 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
937 --i; // Do not skip the next element...
938 }
939 }
940 }
941
942 if (TypeToResolve) {
943 UR_OUT(" * Resolving upreference for "
944 << UpRefs[i].second->getDescription() << "\n";
945 std::string OldName = TypeToResolve->getDescription());
946 TypeToResolve->refineAbstractTypeTo(Ty);
947 }
948
949 return Ty;
950}
951
Chris Lattner58af2a12006-02-15 07:22:58 +0000952//===----------------------------------------------------------------------===//
953// RunVMAsmParser - Define an interface to this parser
954//===----------------------------------------------------------------------===//
955//
Reid Spencer14310612006-12-31 05:40:51 +0000956static Module* RunParser(Module * M);
957
Duncan Sandsdc024672007-11-27 13:23:08 +0000958Module *llvm::RunVMAsmParser(llvm::MemoryBuffer *MB) {
959 InitLLLexer(MB);
960 Module *M = RunParser(new Module(LLLgetFilename()));
961 FreeLexer();
962 return M;
Chris Lattner58af2a12006-02-15 07:22:58 +0000963}
964
965%}
966
967%union {
968 llvm::Module *ModuleVal;
969 llvm::Function *FunctionVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000970 llvm::BasicBlock *BasicBlockVal;
971 llvm::TerminatorInst *TermInstVal;
972 llvm::Instruction *InstVal;
Reid Spencera132e042006-12-03 05:46:11 +0000973 llvm::Constant *ConstVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000974
Reid Spencera132e042006-12-03 05:46:11 +0000975 const llvm::Type *PrimType;
Reid Spencer14310612006-12-31 05:40:51 +0000976 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencera132e042006-12-03 05:46:11 +0000977 llvm::PATypeHolder *TypeVal;
978 llvm::Value *ValueVal;
Reid Spencera132e042006-12-03 05:46:11 +0000979 std::vector<llvm::Value*> *ValueList;
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000980 std::vector<unsigned> *ConstantList;
Reid Spencer14310612006-12-31 05:40:51 +0000981 llvm::ArgListType *ArgList;
982 llvm::TypeWithAttrs TypeWithAttrs;
983 llvm::TypeWithAttrsList *TypeWithAttrsList;
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000984 llvm::ParamList *ParamList;
Reid Spencer14310612006-12-31 05:40:51 +0000985
Chris Lattner58af2a12006-02-15 07:22:58 +0000986 // Represent the RHS of PHI node
Reid Spencera132e042006-12-03 05:46:11 +0000987 std::list<std::pair<llvm::Value*,
988 llvm::BasicBlock*> > *PHIList;
Chris Lattner58af2a12006-02-15 07:22:58 +0000989 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencera132e042006-12-03 05:46:11 +0000990 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner58af2a12006-02-15 07:22:58 +0000991
992 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000993 llvm::GlobalValue::VisibilityTypes Visibility;
Dale Johannesen222ebf72008-02-19 21:40:51 +0000994 llvm::ParameterAttributes ParamAttrs;
Reid Spencer38c91a92007-02-28 02:24:54 +0000995 llvm::APInt *APIntVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000996 int64_t SInt64Val;
997 uint64_t UInt64Val;
998 int SIntVal;
999 unsigned UIntVal;
Dale Johannesen43421b32007-09-06 18:13:44 +00001000 llvm::APFloat *FPVal;
Chris Lattner58af2a12006-02-15 07:22:58 +00001001 bool BoolVal;
1002
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001003 std::string *StrVal; // This memory must be deleted
1004 llvm::ValID ValIDVal;
Chris Lattner58af2a12006-02-15 07:22:58 +00001005
Reid Spencera132e042006-12-03 05:46:11 +00001006 llvm::Instruction::BinaryOps BinaryOpVal;
1007 llvm::Instruction::TermOps TermOpVal;
1008 llvm::Instruction::MemoryOps MemOpVal;
1009 llvm::Instruction::CastOps CastOpVal;
1010 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencera132e042006-12-03 05:46:11 +00001011 llvm::ICmpInst::Predicate IPredicate;
1012 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner58af2a12006-02-15 07:22:58 +00001013}
1014
Reid Spencer14310612006-12-31 05:40:51 +00001015%type <ModuleVal> Module
Chris Lattner58af2a12006-02-15 07:22:58 +00001016%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
1017%type <BasicBlockVal> BasicBlock InstructionList
1018%type <TermInstVal> BBTerminatorInst
1019%type <InstVal> Inst InstVal MemoryInst
Anton Korobeynikov38e09802007-04-28 13:48:45 +00001020%type <ConstVal> ConstVal ConstExpr AliaseeRef
Chris Lattner58af2a12006-02-15 07:22:58 +00001021%type <ConstVector> ConstVector
1022%type <ArgList> ArgList ArgListH
Chris Lattner58af2a12006-02-15 07:22:58 +00001023%type <PHIList> PHIList
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001024%type <ParamList> ParamList // For call param lists & GEP indices
Reid Spencer14310612006-12-31 05:40:51 +00001025%type <ValueList> IndexList // For GEP indices
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001026%type <ConstantList> ConstantIndexList // For insertvalue/extractvalue indices
Reid Spencer14310612006-12-31 05:40:51 +00001027%type <TypeList> TypeListI
1028%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer218ded22007-01-05 17:07:23 +00001029%type <TypeWithAttrs> ArgType
Chris Lattner58af2a12006-02-15 07:22:58 +00001030%type <JumpTable> JumpTable
1031%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001032%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattner58af2a12006-02-15 07:22:58 +00001033%type <BoolVal> OptVolatile // 'volatile' or not
1034%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1035%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencer14310612006-12-31 05:40:51 +00001036%type <Linkage> GVInternalLinkage GVExternalLinkage
1037%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001038%type <Linkage> AliasLinkage
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001039%type <Visibility> GVVisibilityStyle
Chris Lattner58af2a12006-02-15 07:22:58 +00001040
1041// ValueRef - Unresolved reference to a definition or BB
1042%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1043%type <ValueVal> ResolvedVal // <type> <valref> pair
Devang Patel7990dc72008-02-20 22:40:23 +00001044%type <ValueList> ReturnedVal
Chris Lattner58af2a12006-02-15 07:22:58 +00001045// Tokens and types for handling constant integer values
1046//
1047// ESINT64VAL - A negative number within long long range
1048%token <SInt64Val> ESINT64VAL
1049
1050// EUINT64VAL - A positive number within uns. long long range
1051%token <UInt64Val> EUINT64VAL
Chris Lattner58af2a12006-02-15 07:22:58 +00001052
Reid Spencer38c91a92007-02-28 02:24:54 +00001053// ESAPINTVAL - A negative number with arbitrary precision
1054%token <APIntVal> ESAPINTVAL
1055
1056// EUAPINTVAL - A positive number with arbitrary precision
1057%token <APIntVal> EUAPINTVAL
1058
Reid Spencer41dff5e2007-01-26 08:05:27 +00001059%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattner58af2a12006-02-15 07:22:58 +00001060%token <FPVal> FPVAL // Float or Double constant
1061
1062// Built in types...
Reid Spencer218ded22007-01-05 17:07:23 +00001063%type <TypeVal> Types ResultTypes
Reid Spencer14310612006-12-31 05:40:51 +00001064%type <PrimType> IntType FPType PrimType // Classifications
Reid Spencer6f407902007-01-13 05:00:46 +00001065%token <PrimType> VOID INTTYPE
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001066%token <PrimType> FLOAT DOUBLE X86_FP80 FP128 PPC_FP128 LABEL
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001067%token TYPE
Chris Lattner58af2a12006-02-15 07:22:58 +00001068
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001069
Reid Spencered951ea2007-05-19 07:22:10 +00001070%token<StrVal> LOCALVAR GLOBALVAR LABELSTR
1071%token<StrVal> STRINGCONSTANT ATSTRINGCONSTANT PCTSTRINGCONSTANT
Reid Spencer41dff5e2007-01-26 08:05:27 +00001072%type <StrVal> LocalName OptLocalName OptLocalAssign
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001073%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001074%type <StrVal> OptSection SectionString OptGC
Chris Lattner58af2a12006-02-15 07:22:58 +00001075
Christopher Lambbf3348d2007-12-12 08:45:45 +00001076%type <UIntVal> OptAlign OptCAlign OptAddrSpace
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001077
Reid Spencer3d6b71e2007-04-09 01:56:05 +00001078%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001079%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
Reid Spencer14310612006-12-31 05:40:51 +00001080%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Dale Johannesenc7071cc2008-05-14 20:13:36 +00001081%token DLLIMPORT DLLEXPORT EXTERN_WEAK COMMON
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001082%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN ADDRSPACE
Chris Lattner58af2a12006-02-15 07:22:58 +00001083%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001084%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Dale Johannesen20ab78b2008-08-13 18:41:46 +00001085%token X86_SSECALLCC_TOK
Nick Lewycky280a6e62008-04-25 16:53:59 +00001086%token DATALAYOUT
Chris Lattner58af2a12006-02-15 07:22:58 +00001087%type <UIntVal> OptCallingConv
Reid Spencer218ded22007-01-05 17:07:23 +00001088%type <ParamAttrs> OptParamAttrs ParamAttr
1089%type <ParamAttrs> OptFuncAttrs FuncAttr
Chris Lattner58af2a12006-02-15 07:22:58 +00001090
1091// Basic Block Terminating Operators
1092%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
1093
1094// Binary Operators
Reid Spencere4d87aa2006-12-23 06:05:41 +00001095%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencer3ed469c2006-11-02 20:25:50 +00001096%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer832254e2007-02-02 02:16:23 +00001097%token <BinaryOpVal> SHL LSHR ASHR
1098
Nate Begemanac80ade2008-05-12 19:01:56 +00001099%token <OtherOpVal> ICMP FCMP VICMP VFCMP
Reid Spencera132e042006-12-03 05:46:11 +00001100%type <IPredicate> IPredicates
Reid Spencera132e042006-12-03 05:46:11 +00001101%type <FPredicate> FPredicates
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001102%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1103%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner58af2a12006-02-15 07:22:58 +00001104
1105// Memory Instructions
1106%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1107
Reid Spencer3da59db2006-11-27 01:05:10 +00001108// Cast Operators
1109%type <CastOpVal> CastOps
1110%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1111%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1112
Chris Lattner58af2a12006-02-15 07:22:58 +00001113// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001114%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattnerd5efe842006-04-08 01:18:56 +00001115%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Devang Patel5a970972008-02-19 22:27:01 +00001116%token <OtherOpVal> GETRESULT
Dan Gohmane4977cf2008-05-23 01:55:30 +00001117%token <OtherOpVal> EXTRACTVALUE INSERTVALUE
Chris Lattner58af2a12006-02-15 07:22:58 +00001118
Reid Spencer218ded22007-01-05 17:07:23 +00001119// Function Attributes
Reid Spencerb8f85052007-07-31 03:50:36 +00001120%token SIGNEXT ZEROEXT NORETURN INREG SRET NOUNWIND NOALIAS BYVAL NEST
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001121%token READNONE READONLY GC
Chris Lattner58af2a12006-02-15 07:22:58 +00001122
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001123// Visibility Styles
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001124%token DEFAULT HIDDEN PROTECTED
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001125
Chris Lattner58af2a12006-02-15 07:22:58 +00001126%start Module
1127%%
1128
Chris Lattner58af2a12006-02-15 07:22:58 +00001129
Chris Lattner58af2a12006-02-15 07:22:58 +00001130// Operations that are notably excluded from this list include:
1131// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1132//
Reid Spencer3ed469c2006-11-02 20:25:50 +00001133ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer832254e2007-02-02 02:16:23 +00001134LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Reid Spencer3da59db2006-11-27 01:05:10 +00001135CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1136 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer832254e2007-02-02 02:16:23 +00001137
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001138IPredicates
Reid Spencer4012e832006-12-04 05:24:24 +00001139 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001140 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1141 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1142 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1143 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1144 ;
1145
1146FPredicates
1147 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1148 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1149 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1150 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1151 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1152 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1153 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1154 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1155 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1156 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001157
1158// These are some types that allow classification if we only want a particular
1159// thing... for example, only a signed, unsigned, or integral type.
Reid Spencera54b7cb2007-01-12 07:05:14 +00001160IntType : INTTYPE;
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001161FPType : FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80;
Chris Lattner58af2a12006-02-15 07:22:58 +00001162
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001163LocalName : LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001164OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1165
Christopher Lambbf3348d2007-12-12 08:45:45 +00001166OptAddrSpace : ADDRSPACE '(' EUINT64VAL ')' { $$=$3; }
1167 | /*empty*/ { $$=0; };
1168
Reid Spencer41dff5e2007-01-26 08:05:27 +00001169/// OptLocalAssign - Value producing statements have an optional assignment
1170/// component.
1171OptLocalAssign : LocalName '=' {
1172 $$ = $1;
1173 CHECK_FOR_ERROR
1174 }
1175 | /*empty*/ {
1176 $$ = 0;
1177 CHECK_FOR_ERROR
1178 };
1179
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001180GlobalName : GLOBALVAR | ATSTRINGCONSTANT ;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001181
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001182OptGlobalAssign : GlobalAssign
Chris Lattner58af2a12006-02-15 07:22:58 +00001183 | /*empty*/ {
1184 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001185 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001186 };
1187
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001188GlobalAssign : GlobalName '=' {
1189 $$ = $1;
1190 CHECK_FOR_ERROR
Chris Lattner6cdc6822007-04-26 05:31:05 +00001191 };
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001192
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001193GVInternalLinkage
1194 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1195 | WEAK { $$ = GlobalValue::WeakLinkage; }
1196 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1197 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1198 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Dale Johannesenc7071cc2008-05-14 20:13:36 +00001199 | COMMON { $$ = GlobalValue::CommonLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001200 ;
1201
1202GVExternalLinkage
1203 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1204 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1205 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1206 ;
1207
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001208GVVisibilityStyle
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001209 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1210 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1211 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1212 | PROTECTED { $$ = GlobalValue::ProtectedVisibility; }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001213 ;
1214
Reid Spencer14310612006-12-31 05:40:51 +00001215FunctionDeclareLinkage
1216 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1217 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1218 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001219 ;
1220
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001221FunctionDefineLinkage
Reid Spencer14310612006-12-31 05:40:51 +00001222 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1223 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001224 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1225 | WEAK { $$ = GlobalValue::WeakLinkage; }
1226 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001227 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001228
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001229AliasLinkage
1230 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1231 | WEAK { $$ = GlobalValue::WeakLinkage; }
1232 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1233 ;
1234
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001235OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1236 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001237 FASTCC_TOK { $$ = CallingConv::Fast; } |
1238 COLDCC_TOK { $$ = CallingConv::Cold; } |
1239 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1240 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
Dale Johannesen20ab78b2008-08-13 18:41:46 +00001241 X86_SSECALLCC_TOK { $$ = CallingConv::X86_SSECall; } |
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001242 CC_TOK EUINT64VAL {
Chris Lattner58af2a12006-02-15 07:22:58 +00001243 if ((unsigned)$2 != $2)
Reid Spencerb5334b02007-02-05 10:18:06 +00001244 GEN_ERROR("Calling conv too large");
Chris Lattner58af2a12006-02-15 07:22:58 +00001245 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001246 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001247 };
1248
Reid Spencerb8f85052007-07-31 03:50:36 +00001249ParamAttr : ZEROEXT { $$ = ParamAttr::ZExt; }
1250 | ZEXT { $$ = ParamAttr::ZExt; }
1251 | SIGNEXT { $$ = ParamAttr::SExt; }
Chris Lattnerce5f24e2007-07-05 17:26:49 +00001252 | SEXT { $$ = ParamAttr::SExt; }
1253 | INREG { $$ = ParamAttr::InReg; }
1254 | SRET { $$ = ParamAttr::StructRet; }
1255 | NOALIAS { $$ = ParamAttr::NoAlias; }
Reid Spencerb8f85052007-07-31 03:50:36 +00001256 | BYVAL { $$ = ParamAttr::ByVal; }
1257 | NEST { $$ = ParamAttr::Nest; }
Dale Johannesendc6c0f12008-02-22 17:50:51 +00001258 | ALIGN EUINT64VAL { $$ =
1259 ParamAttr::constructAlignmentFromInt($2); }
Reid Spencer14310612006-12-31 05:40:51 +00001260 ;
1261
Reid Spencer18da0722007-04-11 02:44:20 +00001262OptParamAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer218ded22007-01-05 17:07:23 +00001263 | OptParamAttrs ParamAttr {
Reid Spencer7b5d4662007-04-09 06:16:21 +00001264 $$ = $1 | $2;
Reid Spencer14310612006-12-31 05:40:51 +00001265 }
1266 ;
1267
Reid Spencer18da0722007-04-11 02:44:20 +00001268FuncAttr : NORETURN { $$ = ParamAttr::NoReturn; }
1269 | NOUNWIND { $$ = ParamAttr::NoUnwind; }
Reid Spencerb8f85052007-07-31 03:50:36 +00001270 | ZEROEXT { $$ = ParamAttr::ZExt; }
1271 | SIGNEXT { $$ = ParamAttr::SExt; }
Duncan Sandsdc024672007-11-27 13:23:08 +00001272 | READNONE { $$ = ParamAttr::ReadNone; }
1273 | READONLY { $$ = ParamAttr::ReadOnly; }
Reid Spencer218ded22007-01-05 17:07:23 +00001274 ;
1275
Reid Spencer18da0722007-04-11 02:44:20 +00001276OptFuncAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer218ded22007-01-05 17:07:23 +00001277 | OptFuncAttrs FuncAttr {
Reid Spencer7b5d4662007-04-09 06:16:21 +00001278 $$ = $1 | $2;
Reid Spencer218ded22007-01-05 17:07:23 +00001279 }
Reid Spencer14310612006-12-31 05:40:51 +00001280 ;
1281
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001282OptGC : /* empty */ { $$ = 0; }
1283 | GC STRINGCONSTANT {
1284 $$ = $2;
1285 }
1286 ;
1287
Chris Lattner58af2a12006-02-15 07:22:58 +00001288// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1289// a comma before it.
1290OptAlign : /*empty*/ { $$ = 0; } |
1291 ALIGN EUINT64VAL {
1292 $$ = $2;
1293 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001294 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001295 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001296};
1297OptCAlign : /*empty*/ { $$ = 0; } |
1298 ',' ALIGN EUINT64VAL {
1299 $$ = $3;
1300 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001301 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001302 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001303};
1304
1305
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001306
Chris Lattner58af2a12006-02-15 07:22:58 +00001307SectionString : SECTION STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001308 for (unsigned i = 0, e = $2->length(); i != e; ++i)
1309 if ((*$2)[i] == '"' || (*$2)[i] == '\\')
Reid Spencerb5334b02007-02-05 10:18:06 +00001310 GEN_ERROR("Invalid character in section name");
Chris Lattner58af2a12006-02-15 07:22:58 +00001311 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001312 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001313};
1314
1315OptSection : /*empty*/ { $$ = 0; } |
1316 SectionString { $$ = $1; };
1317
1318// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1319// is set to be the global we are processing.
1320//
1321GlobalVarAttributes : /* empty */ {} |
1322 ',' GlobalVarAttribute GlobalVarAttributes {};
1323GlobalVarAttribute : SectionString {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001324 CurGV->setSection(*$1);
1325 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001326 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001327 }
1328 | ALIGN EUINT64VAL {
1329 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001330 GEN_ERROR("Alignment must be a power of two");
Chris Lattner58af2a12006-02-15 07:22:58 +00001331 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001332 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001333 };
1334
1335//===----------------------------------------------------------------------===//
1336// Types includes all predefined types... except void, because it can only be
Reid Spencer14310612006-12-31 05:40:51 +00001337// used in specific contexts (function returning void for example).
Chris Lattner58af2a12006-02-15 07:22:58 +00001338
1339// Derived types are added later...
1340//
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001341PrimType : INTTYPE | FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80 | LABEL ;
Reid Spencer14310612006-12-31 05:40:51 +00001342
1343Types
1344 : OPAQUE {
Reid Spencera132e042006-12-03 05:46:11 +00001345 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001346 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001347 }
1348 | PrimType {
Reid Spencera132e042006-12-03 05:46:11 +00001349 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001350 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00001351 }
Christopher Lambbf3348d2007-12-12 08:45:45 +00001352 | Types OptAddrSpace '*' { // Pointer type?
Reid Spencer14310612006-12-31 05:40:51 +00001353 if (*$1 == Type::LabelTy)
1354 GEN_ERROR("Cannot form a pointer to a basic block");
Christopher Lambbf3348d2007-12-12 08:45:45 +00001355 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1, $2)));
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001356 delete $1;
1357 CHECK_FOR_ERROR
1358 }
Reid Spencer14310612006-12-31 05:40:51 +00001359 | SymbolicValueRef { // Named types are also simple types...
1360 const Type* tmp = getTypeVal($1);
1361 CHECK_FOR_ERROR
1362 $$ = new PATypeHolder(tmp);
1363 }
1364 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerb5334b02007-02-05 10:18:06 +00001365 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattner58af2a12006-02-15 07:22:58 +00001366 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1367 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencera132e042006-12-03 05:46:11 +00001368 $$ = new PATypeHolder(OT);
Chris Lattner58af2a12006-02-15 07:22:58 +00001369 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001370 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001371 }
Reid Spencer218ded22007-01-05 17:07:23 +00001372 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001373 // Allow but ignore attributes on function types; this permits auto-upgrade.
1374 // FIXME: remove in LLVM 3.0.
Chris Lattnera925a142008-04-23 05:37:08 +00001375 const Type *RetTy = *$1;
1376 if (!FunctionType::isValidReturnType(RetTy))
1377 GEN_ERROR("Invalid result type for LLVM function");
1378
Chris Lattner58af2a12006-02-15 07:22:58 +00001379 std::vector<const Type*> Params;
Reid Spencer7b5d4662007-04-09 06:16:21 +00001380 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001381 for (; I != E; ++I ) {
Reid Spencer66728ef2007-03-20 01:13:36 +00001382 const Type *Ty = I->Ty->get();
Reid Spencer66728ef2007-03-20 01:13:36 +00001383 Params.push_back(Ty);
Reid Spencer14310612006-12-31 05:40:51 +00001384 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001385
Chris Lattner58af2a12006-02-15 07:22:58 +00001386 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1387 if (isVarArg) Params.pop_back();
1388
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001389 for (unsigned i = 0; i != Params.size(); ++i)
1390 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1391 GEN_ERROR("Function arguments must be value types!");
1392
1393 CHECK_FOR_ERROR
1394
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001395 FunctionType *FT = FunctionType::get(RetTy, Params, isVarArg);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001396 delete $3; // Delete the argument list
Reid Spencer14310612006-12-31 05:40:51 +00001397 delete $1; // Delete the return type handle
1398 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer61c83e02006-08-18 08:43:06 +00001399 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001400 }
Reid Spencer218ded22007-01-05 17:07:23 +00001401 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001402 // Allow but ignore attributes on function types; this permits auto-upgrade.
1403 // FIXME: remove in LLVM 3.0.
Reid Spencer14310612006-12-31 05:40:51 +00001404 std::vector<const Type*> Params;
Reid Spencer7b5d4662007-04-09 06:16:21 +00001405 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001406 for ( ; I != E; ++I ) {
Reid Spencer66728ef2007-03-20 01:13:36 +00001407 const Type* Ty = I->Ty->get();
Reid Spencer66728ef2007-03-20 01:13:36 +00001408 Params.push_back(Ty);
Reid Spencer14310612006-12-31 05:40:51 +00001409 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001410
Reid Spencer14310612006-12-31 05:40:51 +00001411 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1412 if (isVarArg) Params.pop_back();
1413
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001414 for (unsigned i = 0; i != Params.size(); ++i)
1415 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1416 GEN_ERROR("Function arguments must be value types!");
1417
1418 CHECK_FOR_ERROR
1419
Duncan Sandsdc024672007-11-27 13:23:08 +00001420 FunctionType *FT = FunctionType::get($1, Params, isVarArg);
Reid Spencer218ded22007-01-05 17:07:23 +00001421 delete $3; // Delete the argument list
Reid Spencer14310612006-12-31 05:40:51 +00001422 $$ = new PATypeHolder(HandleUpRefs(FT));
1423 CHECK_FOR_ERROR
1424 }
1425
1426 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001427 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, $2)));
Reid Spencera132e042006-12-03 05:46:11 +00001428 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001429 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001430 }
Chris Lattner32980692007-02-19 07:44:24 +00001431 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencera132e042006-12-03 05:46:11 +00001432 const llvm::Type* ElemTy = $4->get();
1433 if ((unsigned)$2 != $2)
1434 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner42a75512007-01-15 02:27:26 +00001435 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencer9d6565a2007-02-15 02:26:10 +00001436 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001437 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencera132e042006-12-03 05:46:11 +00001438 delete $4;
1439 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001440 }
1441 | '{' TypeListI '}' { // Structure type?
1442 std::vector<const Type*> Elements;
Reid Spencera132e042006-12-03 05:46:11 +00001443 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattner58af2a12006-02-15 07:22:58 +00001444 E = $2->end(); I != E; ++I)
Reid Spencera132e042006-12-03 05:46:11 +00001445 Elements.push_back(*I);
Chris Lattner58af2a12006-02-15 07:22:58 +00001446
Reid Spencera132e042006-12-03 05:46:11 +00001447 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattner58af2a12006-02-15 07:22:58 +00001448 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001449 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001450 }
1451 | '{' '}' { // Empty structure type?
Reid Spencera132e042006-12-03 05:46:11 +00001452 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001453 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001454 }
Andrew Lenharth6353e052006-12-08 18:07:09 +00001455 | '<' '{' TypeListI '}' '>' {
1456 std::vector<const Type*> Elements;
1457 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1458 E = $3->end(); I != E; ++I)
1459 Elements.push_back(*I);
1460
1461 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1462 delete $3;
1463 CHECK_FOR_ERROR
1464 }
1465 | '<' '{' '}' '>' { // Empty structure type?
1466 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1467 CHECK_FOR_ERROR
1468 }
Reid Spencer14310612006-12-31 05:40:51 +00001469 ;
1470
1471ArgType
Duncan Sandsdc024672007-11-27 13:23:08 +00001472 : Types OptParamAttrs {
1473 // Allow but ignore attributes on function types; this permits auto-upgrade.
1474 // FIXME: remove in LLVM 3.0.
Reid Spencer14310612006-12-31 05:40:51 +00001475 $$.Ty = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00001476 $$.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00001477 }
1478 ;
1479
Reid Spencer218ded22007-01-05 17:07:23 +00001480ResultTypes
1481 : Types {
Reid Spencer14310612006-12-31 05:40:51 +00001482 if (!UpRefs.empty())
1483 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Devang Patel20071732008-02-23 01:17:37 +00001484 if (!(*$1)->isFirstClassType() && !isa<StructType>($1->get()))
Reid Spencerb5334b02007-02-05 10:18:06 +00001485 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer218ded22007-01-05 17:07:23 +00001486 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00001487 }
Reid Spencer218ded22007-01-05 17:07:23 +00001488 | VOID {
1489 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencer14310612006-12-31 05:40:51 +00001490 }
1491 ;
1492
1493ArgTypeList : ArgType {
1494 $$ = new TypeWithAttrsList();
1495 $$->push_back($1);
1496 CHECK_FOR_ERROR
1497 }
1498 | ArgTypeList ',' ArgType {
1499 ($$=$1)->push_back($3);
1500 CHECK_FOR_ERROR
1501 }
1502 ;
1503
1504ArgTypeListI
1505 : ArgTypeList
1506 | ArgTypeList ',' DOTDOTDOT {
1507 $$=$1;
Reid Spencer18da0722007-04-11 02:44:20 +00001508 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00001509 TWA.Ty = new PATypeHolder(Type::VoidTy);
1510 $$->push_back(TWA);
1511 CHECK_FOR_ERROR
1512 }
1513 | DOTDOTDOT {
1514 $$ = new TypeWithAttrsList;
Reid Spencer18da0722007-04-11 02:44:20 +00001515 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00001516 TWA.Ty = new PATypeHolder(Type::VoidTy);
1517 $$->push_back(TWA);
1518 CHECK_FOR_ERROR
1519 }
1520 | /*empty*/ {
1521 $$ = new TypeWithAttrsList();
Reid Spencer61c83e02006-08-18 08:43:06 +00001522 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001523 };
1524
1525// TypeList - Used for struct declarations and as a basis for function type
1526// declaration type lists
1527//
Reid Spencer14310612006-12-31 05:40:51 +00001528TypeListI : Types {
Reid Spencera132e042006-12-03 05:46:11 +00001529 $$ = new std::list<PATypeHolder>();
Reid Spencer66728ef2007-03-20 01:13:36 +00001530 $$->push_back(*$1);
1531 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001532 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001533 }
Reid Spencer14310612006-12-31 05:40:51 +00001534 | TypeListI ',' Types {
Reid Spencer66728ef2007-03-20 01:13:36 +00001535 ($$=$1)->push_back(*$3);
1536 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001537 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001538 };
1539
Chris Lattner58af2a12006-02-15 07:22:58 +00001540// ConstVal - The various declarations that go into the constant pool. This
1541// production is used ONLY to represent constants that show up AFTER a 'const',
1542// 'constant' or 'global' token at global scope. Constants that can be inlined
1543// into other expressions (such as integers and constexprs) are handled by the
1544// ResolvedVal, ValueRef and ConstValueRef productions.
1545//
1546ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001547 if (!UpRefs.empty())
1548 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001549 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001550 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001551 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001552 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001553 const Type *ETy = ATy->getElementType();
Dan Gohman180c1692008-06-23 18:43:26 +00001554 uint64_t NumElements = ATy->getNumElements();
Chris Lattner58af2a12006-02-15 07:22:58 +00001555
1556 // Verify that we have the correct size...
Mon P Wang28873102008-06-25 08:15:39 +00001557 if (NumElements != uint64_t(-1) && NumElements != $3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001558 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001559 utostr($3->size()) + " arguments, but has size of " +
Mon P Wang28873102008-06-25 08:15:39 +00001560 utostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001561
1562 // Verify all elements are correct type!
1563 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001564 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001565 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001566 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001567 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001568 }
1569
Reid Spencera132e042006-12-03 05:46:11 +00001570 $$ = ConstantArray::get(ATy, *$3);
1571 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001572 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001573 }
1574 | Types '[' ']' {
Reid Spencer14310612006-12-31 05:40:51 +00001575 if (!UpRefs.empty())
1576 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001577 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001578 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001579 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001580 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001581
Dan Gohman180c1692008-06-23 18:43:26 +00001582 uint64_t NumElements = ATy->getNumElements();
Mon P Wang28873102008-06-25 08:15:39 +00001583 if (NumElements != uint64_t(-1) && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001584 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Mon P Wang28873102008-06-25 08:15:39 +00001585 " arguments, but has size of " + utostr(NumElements) +"");
Reid Spencera132e042006-12-03 05:46:11 +00001586 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1587 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001588 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001589 }
1590 | Types 'c' STRINGCONSTANT {
Reid Spencer14310612006-12-31 05:40:51 +00001591 if (!UpRefs.empty())
1592 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001593 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001594 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001595 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001596 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001597
Dan Gohman180c1692008-06-23 18:43:26 +00001598 uint64_t NumElements = ATy->getNumElements();
Chris Lattner58af2a12006-02-15 07:22:58 +00001599 const Type *ETy = ATy->getElementType();
Mon P Wang28873102008-06-25 08:15:39 +00001600 if (NumElements != uint64_t(-1) && NumElements != $3->length())
Reid Spencer61c83e02006-08-18 08:43:06 +00001601 GEN_ERROR("Can't build string constant of size " +
Mon P Wang28873102008-06-25 08:15:39 +00001602 utostr($3->length()) +
1603 " when array has size " + utostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001604 std::vector<Constant*> Vals;
Reid Spencer14310612006-12-31 05:40:51 +00001605 if (ETy == Type::Int8Ty) {
Mon P Wang28873102008-06-25 08:15:39 +00001606 for (uint64_t i = 0; i < $3->length(); ++i)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001607 Vals.push_back(ConstantInt::get(ETy, (*$3)[i]));
Chris Lattner58af2a12006-02-15 07:22:58 +00001608 } else {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001609 delete $3;
Reid Spencerb5334b02007-02-05 10:18:06 +00001610 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner58af2a12006-02-15 07:22:58 +00001611 }
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001612 delete $3;
Reid Spencera132e042006-12-03 05:46:11 +00001613 $$ = ConstantArray::get(ATy, Vals);
1614 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001615 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001616 }
1617 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001618 if (!UpRefs.empty())
1619 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00001620 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001621 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001622 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001623 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001624 const Type *ETy = PTy->getElementType();
Dan Gohman180c1692008-06-23 18:43:26 +00001625 unsigned NumElements = PTy->getNumElements();
Chris Lattner58af2a12006-02-15 07:22:58 +00001626
1627 // Verify that we have the correct size...
Mon P Wang28873102008-06-25 08:15:39 +00001628 if (NumElements != unsigned(-1) && NumElements != (unsigned)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001629 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001630 utostr($3->size()) + " arguments, but has size of " +
Mon P Wang28873102008-06-25 08:15:39 +00001631 utostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001632
1633 // Verify all elements are correct type!
1634 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001635 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001636 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001637 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001638 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001639 }
1640
Reid Spencer9d6565a2007-02-15 02:26:10 +00001641 $$ = ConstantVector::get(PTy, *$3);
Reid Spencera132e042006-12-03 05:46:11 +00001642 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001643 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001644 }
1645 | Types '{' ConstVector '}' {
Reid Spencera132e042006-12-03 05:46:11 +00001646 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001647 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001648 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001649 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001650
1651 if ($3->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001652 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001653
1654 // Check to ensure that constants are compatible with the type initializer!
1655 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencera132e042006-12-03 05:46:11 +00001656 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001657 GEN_ERROR("Expected type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001658 STy->getElementType(i)->getDescription() +
1659 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001660 " of structure initializer");
Chris Lattner58af2a12006-02-15 07:22:58 +00001661
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001662 // Check to ensure that Type is not packed
1663 if (STy->isPacked())
Chris Lattner6cdc6822007-04-26 05:31:05 +00001664 GEN_ERROR("Unpacked Initializer to vector type '" +
1665 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001666
Reid Spencera132e042006-12-03 05:46:11 +00001667 $$ = ConstantStruct::get(STy, *$3);
1668 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001669 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001670 }
1671 | Types '{' '}' {
Reid Spencer14310612006-12-31 05:40:51 +00001672 if (!UpRefs.empty())
1673 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001674 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001675 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001676 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001677 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001678
1679 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001680 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001681
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001682 // Check to ensure that Type is not packed
1683 if (STy->isPacked())
Chris Lattner6cdc6822007-04-26 05:31:05 +00001684 GEN_ERROR("Unpacked Initializer to vector type '" +
1685 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001686
1687 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1688 delete $1;
1689 CHECK_FOR_ERROR
1690 }
1691 | Types '<' '{' ConstVector '}' '>' {
1692 const StructType *STy = dyn_cast<StructType>($1->get());
1693 if (STy == 0)
1694 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001695 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001696
1697 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001698 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001699
1700 // Check to ensure that constants are compatible with the type initializer!
1701 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1702 if ((*$4)[i]->getType() != STy->getElementType(i))
1703 GEN_ERROR("Expected type '" +
1704 STy->getElementType(i)->getDescription() +
1705 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001706 " of structure initializer");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001707
1708 // Check to ensure that Type is packed
1709 if (!STy->isPacked())
Chris Lattner32980692007-02-19 07:44:24 +00001710 GEN_ERROR("Vector initializer to non-vector type '" +
1711 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001712
1713 $$ = ConstantStruct::get(STy, *$4);
1714 delete $1; delete $4;
1715 CHECK_FOR_ERROR
1716 }
1717 | Types '<' '{' '}' '>' {
1718 if (!UpRefs.empty())
1719 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1720 const StructType *STy = dyn_cast<StructType>($1->get());
1721 if (STy == 0)
1722 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001723 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001724
1725 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001726 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001727
1728 // Check to ensure that Type is packed
1729 if (!STy->isPacked())
Chris Lattner32980692007-02-19 07:44:24 +00001730 GEN_ERROR("Vector initializer to non-vector type '" +
1731 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001732
Reid Spencera132e042006-12-03 05:46:11 +00001733 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1734 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001735 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001736 }
1737 | Types NULL_TOK {
Reid Spencer14310612006-12-31 05:40:51 +00001738 if (!UpRefs.empty())
1739 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001740 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001741 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001742 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001743 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001744
Reid Spencera132e042006-12-03 05:46:11 +00001745 $$ = ConstantPointerNull::get(PTy);
1746 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001747 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001748 }
1749 | Types UNDEF {
Reid Spencer14310612006-12-31 05:40:51 +00001750 if (!UpRefs.empty())
1751 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001752 $$ = UndefValue::get($1->get());
1753 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001754 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001755 }
1756 | Types SymbolicValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00001757 if (!UpRefs.empty())
1758 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001759 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001760 if (Ty == 0)
Devang Patel5a970972008-02-19 22:27:01 +00001761 GEN_ERROR("Global const reference must be a pointer type " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001762
1763 // ConstExprs can exist in the body of a function, thus creating
1764 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencer93c40032007-03-19 18:40:50 +00001765 // the context of a function, getExistingVal will search the functions
Chris Lattner58af2a12006-02-15 07:22:58 +00001766 // symbol table instead of the module symbol table for the global symbol,
1767 // which throws things all off. To get around this, we just tell
Reid Spencer93c40032007-03-19 18:40:50 +00001768 // getExistingVal that we are at global scope here.
Chris Lattner58af2a12006-02-15 07:22:58 +00001769 //
1770 Function *SavedCurFn = CurFun.CurrentFunction;
1771 CurFun.CurrentFunction = 0;
1772
Reid Spencer93c40032007-03-19 18:40:50 +00001773 Value *V = getExistingVal(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001774 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001775
1776 CurFun.CurrentFunction = SavedCurFn;
1777
1778 // If this is an initializer for a constant pointer, which is referencing a
1779 // (currently) undefined variable, create a stub now that shall be replaced
1780 // in the future with the right type of variable.
1781 //
1782 if (V == 0) {
Reid Spencera9720f52007-02-05 17:04:00 +00001783 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001784 const PointerType *PT = cast<PointerType>(Ty);
1785
1786 // First check to see if the forward references value is already created!
1787 PerModuleInfo::GlobalRefsType::iterator I =
1788 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1789
1790 if (I != CurModule.GlobalRefs.end()) {
1791 V = I->second; // Placeholder already exists, use it...
1792 $2.destroy();
1793 } else {
1794 std::string Name;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001795 if ($2.Type == ValID::GlobalName)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001796 Name = $2.getName();
Reid Spencer41dff5e2007-01-26 08:05:27 +00001797 else if ($2.Type != ValID::GlobalID)
1798 GEN_ERROR("Invalid reference to global");
Chris Lattner58af2a12006-02-15 07:22:58 +00001799
1800 // Create the forward referenced global.
1801 GlobalValue *GV;
1802 if (const FunctionType *FTy =
1803 dyn_cast<FunctionType>(PT->getElementType())) {
Gabor Greife64d2482008-04-06 23:07:54 +00001804 GV = Function::Create(FTy, GlobalValue::ExternalWeakLinkage, Name,
1805 CurModule.CurrentModule);
Chris Lattner58af2a12006-02-15 07:22:58 +00001806 } else {
1807 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattner6cdc6822007-04-26 05:31:05 +00001808 GlobalValue::ExternalWeakLinkage, 0,
Chris Lattner58af2a12006-02-15 07:22:58 +00001809 Name, CurModule.CurrentModule);
1810 }
1811
1812 // Keep track of the fact that we have a forward ref to recycle it
1813 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1814 V = GV;
1815 }
1816 }
1817
Reid Spencera132e042006-12-03 05:46:11 +00001818 $$ = cast<GlobalValue>(V);
1819 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001820 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001821 }
1822 | Types ConstExpr {
Reid Spencer14310612006-12-31 05:40:51 +00001823 if (!UpRefs.empty())
1824 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001825 if ($1->get() != $2->getType())
Reid Spencere68853b2007-01-04 00:06:14 +00001826 GEN_ERROR("Mismatched types for constant expression: " +
1827 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001828 $$ = $2;
Reid Spencera132e042006-12-03 05:46:11 +00001829 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001830 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001831 }
1832 | Types ZEROINITIALIZER {
Reid Spencer14310612006-12-31 05:40:51 +00001833 if (!UpRefs.empty())
1834 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001835 const Type *Ty = $1->get();
Chris Lattner58af2a12006-02-15 07:22:58 +00001836 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerb5334b02007-02-05 10:18:06 +00001837 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencera132e042006-12-03 05:46:11 +00001838 $$ = Constant::getNullValue(Ty);
1839 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001840 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00001841 }
Reid Spencer14310612006-12-31 05:40:51 +00001842 | IntType ESINT64VAL { // integral constants
Reid Spencere4d87aa2006-12-23 06:05:41 +00001843 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001844 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencer49d273e2007-03-19 20:40:51 +00001845 $$ = ConstantInt::get($1, $2, true);
Reid Spencer38c91a92007-02-28 02:24:54 +00001846 CHECK_FOR_ERROR
1847 }
1848 | IntType ESAPINTVAL { // 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 Spencer10794272007-03-01 19:41:47 +00001852 }
1853 $2->sextOrTrunc(BitWidth);
1854 $$ = ConstantInt::get(*$2);
Reid Spencer38c91a92007-02-28 02:24:54 +00001855 delete $2;
Reid Spencere4d87aa2006-12-23 06:05:41 +00001856 CHECK_FOR_ERROR
1857 }
Reid Spencer14310612006-12-31 05:40:51 +00001858 | IntType EUINT64VAL { // integral constants
Reid Spencere4d87aa2006-12-23 06:05:41 +00001859 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001860 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencer49d273e2007-03-19 20:40:51 +00001861 $$ = ConstantInt::get($1, $2, false);
Reid Spencer38c91a92007-02-28 02:24:54 +00001862 CHECK_FOR_ERROR
1863 }
1864 | IntType EUAPINTVAL { // arbitrary precision integer constants
1865 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1866 if ($2->getBitWidth() > BitWidth) {
1867 GEN_ERROR("Constant value does not fit in type");
Reid Spencer10794272007-03-01 19:41:47 +00001868 }
1869 $2->zextOrTrunc(BitWidth);
1870 $$ = ConstantInt::get(*$2);
Reid Spencer38c91a92007-02-28 02:24:54 +00001871 delete $2;
Reid Spencere4d87aa2006-12-23 06:05:41 +00001872 CHECK_FOR_ERROR
1873 }
Reid Spencer6f407902007-01-13 05:00:46 +00001874 | INTTYPE TRUETOK { // Boolean constants
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001875 if (cast<IntegerType>($1)->getBitWidth() != 1)
1876 GEN_ERROR("Constant true must have type i1");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001877 $$ = ConstantInt::getTrue();
Reid Spencer61c83e02006-08-18 08:43:06 +00001878 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001879 }
Reid Spencer6f407902007-01-13 05:00:46 +00001880 | INTTYPE FALSETOK { // Boolean constants
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001881 if (cast<IntegerType>($1)->getBitWidth() != 1)
1882 GEN_ERROR("Constant false must have type i1");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001883 $$ = ConstantInt::getFalse();
Reid Spencer61c83e02006-08-18 08:43:06 +00001884 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001885 }
Dale Johannesenea583102007-09-12 03:31:28 +00001886 | FPType FPVAL { // Floating point constants
Dale Johannesen43421b32007-09-06 18:13:44 +00001887 if (!ConstantFP::isValueValidForType($1, *$2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001888 GEN_ERROR("Floating point constant invalid for type");
Dale Johannesenc72cd7e2007-09-11 18:33:39 +00001889 // Lexer has no type info, so builds all float and double FP constants
1890 // as double. Fix this here. Long double is done right.
1891 if (&$2->getSemantics()==&APFloat::IEEEdouble && $1==Type::FloatTy)
Dale Johannesen43421b32007-09-06 18:13:44 +00001892 $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
Chris Lattnerd8eb63f2008-04-20 00:41:19 +00001893 $$ = ConstantFP::get(*$2);
Dale Johannesencdd509a2007-09-07 21:07:57 +00001894 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001895 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001896 };
1897
1898
Reid Spencer3da59db2006-11-27 01:05:10 +00001899ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer14310612006-12-31 05:40:51 +00001900 if (!UpRefs.empty())
1901 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001902 Constant *Val = $3;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001903 const Type *DestTy = $5->get();
1904 if (!CastInst::castIsValid($1, $3, DestTy))
1905 GEN_ERROR("invalid cast opcode for cast from '" +
1906 Val->getType()->getDescription() + "' to '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001907 DestTy->getDescription() + "'");
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001908 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00001909 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00001910 }
1911 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001912 if (!isa<PointerType>($3->getType()))
Reid Spencerb5334b02007-02-05 10:18:06 +00001913 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00001914
Reid Spencera132e042006-12-03 05:46:11 +00001915 const Type *IdxTy =
Dan Gohman041e2eb2008-05-15 19:50:34 +00001916 GetElementPtrInst::getIndexedType($3->getType(), $4->begin(), $4->end());
Reid Spencera132e042006-12-03 05:46:11 +00001917 if (!IdxTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00001918 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencera132e042006-12-03 05:46:11 +00001919
Chris Lattnerf7469af2007-01-31 04:44:08 +00001920 SmallVector<Constant*, 8> IdxVec;
Reid Spencera132e042006-12-03 05:46:11 +00001921 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1922 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattner58af2a12006-02-15 07:22:58 +00001923 IdxVec.push_back(C);
1924 else
Reid Spencerb5334b02007-02-05 10:18:06 +00001925 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattner58af2a12006-02-15 07:22:58 +00001926
1927 delete $4;
1928
Chris Lattnerf7469af2007-01-31 04:44:08 +00001929 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer61c83e02006-08-18 08:43:06 +00001930 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001931 }
1932 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer4fe16d62007-01-11 18:21:29 +00001933 if ($3->getType() != Type::Int1Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00001934 GEN_ERROR("Select condition must be of boolean type");
Reid Spencera132e042006-12-03 05:46:11 +00001935 if ($5->getType() != $7->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001936 GEN_ERROR("Select operand types must match");
Reid Spencera132e042006-12-03 05:46:11 +00001937 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001938 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001939 }
1940 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001941 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001942 GEN_ERROR("Binary operator types must match");
Reid Spencer1628cec2006-10-26 06:15:43 +00001943 CHECK_FOR_ERROR;
Reid Spencer9eef56f2006-12-05 19:16:11 +00001944 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner58af2a12006-02-15 07:22:58 +00001945 }
1946 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001947 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001948 GEN_ERROR("Logical operator types must match");
Chris Lattner42a75512007-01-15 02:27:26 +00001949 if (!$3->getType()->isInteger()) {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00001950 if (!isa<VectorType>($3->getType()) ||
Reid Spencer9d6565a2007-02-15 02:26:10 +00001951 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00001952 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00001953 }
Reid Spencera132e042006-12-03 05:46:11 +00001954 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001955 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001956 }
Reid Spencer4012e832006-12-04 05:24:24 +00001957 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1958 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001959 GEN_ERROR("icmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00001960 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00001961 }
Reid Spencer4012e832006-12-04 05:24:24 +00001962 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1963 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001964 GEN_ERROR("fcmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00001965 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00001966 }
Nate Begemanac80ade2008-05-12 19:01:56 +00001967 | VICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1968 if ($4->getType() != $6->getType())
1969 GEN_ERROR("vicmp operand types must match");
1970 $$ = ConstantExpr::getVICmp($2, $4, $6);
1971 }
1972 | VFCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1973 if ($4->getType() != $6->getType())
1974 GEN_ERROR("vfcmp operand types must match");
1975 $$ = ConstantExpr::getVFCmp($2, $4, $6);
1976 }
Chris Lattner58af2a12006-02-15 07:22:58 +00001977 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001978 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerb5334b02007-02-05 10:18:06 +00001979 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00001980 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001981 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00001982 }
1983 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001984 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00001985 GEN_ERROR("Invalid insertelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00001986 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001987 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00001988 }
1989 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001990 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00001991 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00001992 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001993 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00001994 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001995 | EXTRACTVALUE '(' ConstVal ConstantIndexList ')' {
Dan Gohmane4977cf2008-05-23 01:55:30 +00001996 if (!isa<StructType>($3->getType()) && !isa<ArrayType>($3->getType()))
1997 GEN_ERROR("ExtractValue requires an aggregate operand");
1998
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001999 $$ = ConstantExpr::getExtractValue($3, &(*$4)[0], $4->size());
Dan Gohmane4977cf2008-05-23 01:55:30 +00002000 delete $4;
Dan Gohmane4977cf2008-05-23 01:55:30 +00002001 CHECK_FOR_ERROR
2002 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002003 | INSERTVALUE '(' ConstVal ',' ConstVal ConstantIndexList ')' {
Dan Gohmane4977cf2008-05-23 01:55:30 +00002004 if (!isa<StructType>($3->getType()) && !isa<ArrayType>($3->getType()))
2005 GEN_ERROR("InsertValue requires an aggregate operand");
2006
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002007 $$ = ConstantExpr::getInsertValue($3, $5, &(*$6)[0], $6->size());
Dan Gohmane4977cf2008-05-23 01:55:30 +00002008 delete $6;
Dan Gohmane4977cf2008-05-23 01:55:30 +00002009 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002010 };
2011
Chris Lattnerd25db202006-04-08 03:55:17 +00002012
Chris Lattner58af2a12006-02-15 07:22:58 +00002013// ConstVector - A list of comma separated constants.
2014ConstVector : ConstVector ',' ConstVal {
2015 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002016 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002017 }
2018 | ConstVal {
Reid Spencera132e042006-12-03 05:46:11 +00002019 $$ = new std::vector<Constant*>();
Chris Lattner58af2a12006-02-15 07:22:58 +00002020 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002021 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002022 };
2023
2024
2025// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
2026GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
2027
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002028// ThreadLocal
2029ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
2030
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002031// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
2032AliaseeRef : ResultTypes SymbolicValueRef {
2033 const Type* VTy = $1->get();
2034 Value *V = getVal(VTy, $2);
Chris Lattner0275cff2007-08-06 21:00:46 +00002035 CHECK_FOR_ERROR
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002036 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
2037 if (!Aliasee)
2038 GEN_ERROR("Aliases can be created only to global values");
2039
2040 $$ = Aliasee;
2041 CHECK_FOR_ERROR
2042 delete $1;
2043 }
2044 | BITCAST '(' AliaseeRef TO Types ')' {
2045 Constant *Val = $3;
2046 const Type *DestTy = $5->get();
2047 if (!CastInst::castIsValid($1, $3, DestTy))
2048 GEN_ERROR("invalid cast opcode for cast from '" +
2049 Val->getType()->getDescription() + "' to '" +
2050 DestTy->getDescription() + "'");
2051
2052 $$ = ConstantExpr::getCast($1, $3, DestTy);
2053 CHECK_FOR_ERROR
2054 delete $5;
2055 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002056
2057//===----------------------------------------------------------------------===//
2058// Rules to match Modules
2059//===----------------------------------------------------------------------===//
2060
2061// Module rule: Capture the result of parsing the whole file into a result
2062// variable...
2063//
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002064Module
2065 : DefinitionList {
2066 $$ = ParserResult = CurModule.CurrentModule;
2067 CurModule.ModuleDone();
2068 CHECK_FOR_ERROR;
2069 }
2070 | /*empty*/ {
2071 $$ = ParserResult = CurModule.CurrentModule;
2072 CurModule.ModuleDone();
2073 CHECK_FOR_ERROR;
2074 }
2075 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002076
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002077DefinitionList
2078 : Definition
2079 | DefinitionList Definition
2080 ;
2081
2082Definition
Jeff Cohen361c3ef2007-01-21 19:19:31 +00002083 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner58af2a12006-02-15 07:22:58 +00002084 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00002085 CHECK_FOR_ERROR
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002086 }
2087 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer61c83e02006-08-18 08:43:06 +00002088 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002089 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002090 | MODULE ASM_TOK AsmBlock {
Reid Spencer61c83e02006-08-18 08:43:06 +00002091 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002092 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002093 | OptLocalAssign TYPE Types {
Reid Spencer14310612006-12-31 05:40:51 +00002094 if (!UpRefs.empty())
2095 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002096 // Eagerly resolve types. This is not an optimization, this is a
2097 // requirement that is due to the fact that we could have this:
2098 //
2099 // %list = type { %list * }
2100 // %list = type { %list * } ; repeated type decl
2101 //
2102 // If types are not resolved eagerly, then the two types will not be
2103 // determined to be the same type!
2104 //
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002105 ResolveTypeTo($1, *$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002106
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002107 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002108 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002109 // If this is a named type that is not a redefinition, add it to the slot
2110 // table.
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002111 CurModule.Types.push_back(*$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002112 }
Reid Spencera132e042006-12-03 05:46:11 +00002113
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002114 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002115 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002116 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002117 | OptLocalAssign TYPE VOID {
Reid Spencer14310612006-12-31 05:40:51 +00002118 ResolveTypeTo($1, $3);
2119
2120 if (!setTypeName($3, $1) && !$1) {
2121 CHECK_FOR_ERROR
2122 // If this is a named type that is not a redefinition, add it to the slot
2123 // table.
2124 CurModule.Types.push_back($3);
2125 }
2126 CHECK_FOR_ERROR
2127 }
Christopher Lambbf3348d2007-12-12 08:45:45 +00002128 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal
2129 OptAddrSpace {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002130 /* "Externally Visible" Linkage */
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002131 if ($5 == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002132 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002133 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
Christopher Lambbf3348d2007-12-12 08:45:45 +00002134 $2, $4, $5->getType(), $5, $3, $6);
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00002135 CHECK_FOR_ERROR
2136 } GlobalVarAttributes {
2137 CurGV = 0;
2138 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002139 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambbf3348d2007-12-12 08:45:45 +00002140 ConstVal OptAddrSpace {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002141 if ($6 == 0)
2142 GEN_ERROR("Global value initializer is not a constant");
Christopher Lambbf3348d2007-12-12 08:45:45 +00002143 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4, $7);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002144 CHECK_FOR_ERROR
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002145 } GlobalVarAttributes {
2146 CurGV = 0;
2147 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002148 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambbf3348d2007-12-12 08:45:45 +00002149 Types OptAddrSpace {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002150 if (!UpRefs.empty())
2151 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
Christopher Lambbf3348d2007-12-12 08:45:45 +00002152 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4, $7);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002153 CHECK_FOR_ERROR
2154 delete $6;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002155 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002156 CurGV = 0;
2157 CHECK_FOR_ERROR
2158 }
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002159 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002160 std::string Name;
2161 if ($1) {
2162 Name = *$1;
2163 delete $1;
2164 }
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002165 if (Name.empty())
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002166 GEN_ERROR("Alias name cannot be empty");
2167
2168 Constant* Aliasee = $5;
2169 if (Aliasee == 0)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002170 GEN_ERROR(std::string("Invalid aliasee for alias: ") + Name);
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002171
2172 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2173 CurModule.CurrentModule);
2174 GA->setVisibility($2);
2175 InsertValue(GA, CurModule.Values);
Chris Lattner569f7372007-09-10 23:24:14 +00002176
2177
2178 // If there was a forward reference of this alias, resolve it now.
2179
2180 ValID ID;
2181 if (!Name.empty())
2182 ID = ValID::createGlobalName(Name);
2183 else
2184 ID = ValID::createGlobalID(CurModule.Values.size()-1);
2185
2186 if (GlobalValue *FWGV =
2187 CurModule.GetForwardRefForGlobal(GA->getType(), ID)) {
2188 // Replace uses of the fwdref with the actual alias.
2189 FWGV->replaceAllUsesWith(GA);
2190 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(FWGV))
2191 GV->eraseFromParent();
2192 else
2193 cast<Function>(FWGV)->eraseFromParent();
2194 }
2195 ID.destroy();
2196
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002197 CHECK_FOR_ERROR
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002198 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002199 | TARGET TargetDefinition {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002200 CHECK_FOR_ERROR
2201 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002202 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00002203 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002204 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002205 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002206
2207
2208AsmBlock : STRINGCONSTANT {
2209 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattner58af2a12006-02-15 07:22:58 +00002210 if (AsmSoFar.empty())
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002211 CurModule.CurrentModule->setModuleInlineAsm(*$1);
Chris Lattner58af2a12006-02-15 07:22:58 +00002212 else
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002213 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+*$1);
2214 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002215 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002216};
2217
Reid Spencer41dff5e2007-01-26 08:05:27 +00002218TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002219 CurModule.CurrentModule->setTargetTriple(*$3);
2220 delete $3;
John Criswell2f6a8b12006-10-24 19:09:48 +00002221 }
Chris Lattner1ae022f2006-10-22 06:08:13 +00002222 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002223 CurModule.CurrentModule->setDataLayout(*$3);
2224 delete $3;
Owen Anderson1dc69692006-10-18 02:21:48 +00002225 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002226
2227LibrariesDefinition : '[' LibList ']';
2228
2229LibList : LibList ',' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002230 CurModule.CurrentModule->addLibrary(*$3);
2231 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002232 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002233 }
2234 | STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002235 CurModule.CurrentModule->addLibrary(*$1);
2236 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002237 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002238 }
2239 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00002240 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002241 }
2242 ;
2243
2244//===----------------------------------------------------------------------===//
2245// Rules to match Function Headers
2246//===----------------------------------------------------------------------===//
2247
Reid Spencer41dff5e2007-01-26 08:05:27 +00002248ArgListH : ArgListH ',' Types OptParamAttrs OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002249 if (!UpRefs.empty())
2250 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002251 if (!(*$3)->isFirstClassType())
2252 GEN_ERROR("Argument types must be first-class");
Reid Spencer14310612006-12-31 05:40:51 +00002253 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00002254 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002255 $1->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002256 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002257 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002258 | Types OptParamAttrs OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002259 if (!UpRefs.empty())
2260 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002261 if (!(*$1)->isFirstClassType())
2262 GEN_ERROR("Argument types must be first-class");
Reid Spencer14310612006-12-31 05:40:51 +00002263 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2264 $$ = new ArgListType;
2265 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002266 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002267 };
2268
2269ArgList : ArgListH {
2270 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002271 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002272 }
2273 | ArgListH ',' DOTDOTDOT {
2274 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002275 struct ArgListEntry E;
2276 E.Ty = new PATypeHolder(Type::VoidTy);
2277 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002278 E.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00002279 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002280 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002281 }
2282 | DOTDOTDOT {
Reid Spencer14310612006-12-31 05:40:51 +00002283 $$ = new ArgListType;
2284 struct ArgListEntry E;
2285 E.Ty = new PATypeHolder(Type::VoidTy);
2286 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002287 E.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00002288 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002289 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002290 }
2291 | /* empty */ {
2292 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00002293 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002294 };
2295
Reid Spencer41dff5e2007-01-26 08:05:27 +00002296FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002297 OptFuncAttrs OptSection OptAlign OptGC {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002298 std::string FunctionName(*$3);
2299 delete $3; // Free strdup'd memory!
Chris Lattner58af2a12006-02-15 07:22:58 +00002300
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002301 // Check the function result for abstractness if this is a define. We should
2302 // have no abstract types at this point
Reid Spencer218ded22007-01-05 17:07:23 +00002303 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2304 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002305
Chris Lattnera925a142008-04-23 05:37:08 +00002306 if (!FunctionType::isValidReturnType(*$2))
2307 GEN_ERROR("Invalid result type for LLVM function");
2308
Chris Lattner58af2a12006-02-15 07:22:58 +00002309 std::vector<const Type*> ParamTypeList;
Chris Lattner58d74912008-03-12 17:45:29 +00002310 SmallVector<ParamAttrsWithIndex, 8> Attrs;
2311 if ($7 != ParamAttr::None)
2312 Attrs.push_back(ParamAttrsWithIndex::get(0, $7));
Chris Lattner58af2a12006-02-15 07:22:58 +00002313 if ($5) { // If there are arguments...
Reid Spencer7b5d4662007-04-09 06:16:21 +00002314 unsigned index = 1;
2315 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002316 const Type* Ty = I->Ty->get();
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002317 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2318 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencer14310612006-12-31 05:40:51 +00002319 ParamTypeList.push_back(Ty);
Chris Lattner58d74912008-03-12 17:45:29 +00002320 if (Ty != Type::VoidTy && I->Attrs != ParamAttr::None)
2321 Attrs.push_back(ParamAttrsWithIndex::get(index, I->Attrs));
Reid Spencer14310612006-12-31 05:40:51 +00002322 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002323 }
2324
2325 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2326 if (isVarArg) ParamTypeList.pop_back();
2327
Chris Lattner58d74912008-03-12 17:45:29 +00002328 PAListPtr PAL;
Christopher Lamb5c104242007-04-22 20:09:11 +00002329 if (!Attrs.empty())
Chris Lattner58d74912008-03-12 17:45:29 +00002330 PAL = PAListPtr::get(Attrs.begin(), Attrs.end());
Reid Spencer7b5d4662007-04-09 06:16:21 +00002331
Duncan Sandsdc024672007-11-27 13:23:08 +00002332 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002333 const PointerType *PFT = PointerType::getUnqual(FT);
Reid Spencer218ded22007-01-05 17:07:23 +00002334 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002335
2336 ValID ID;
2337 if (!FunctionName.empty()) {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002338 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattner58af2a12006-02-15 07:22:58 +00002339 } else {
Reid Spencer93c40032007-03-19 18:40:50 +00002340 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002341 }
2342
2343 Function *Fn = 0;
2344 // See if this function was forward referenced. If so, recycle the object.
2345 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2346 // Move the function to the end of the list, from whereever it was
2347 // previously inserted.
2348 Fn = cast<Function>(FWRef);
Chris Lattner58d74912008-03-12 17:45:29 +00002349 assert(Fn->getParamAttrs().isEmpty() &&
2350 "Forward reference has parameter attributes!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002351 CurModule.CurrentModule->getFunctionList().remove(Fn);
2352 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2353 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spenceref9b9a72007-02-05 20:47:22 +00002354 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002355 if (Fn->getFunctionType() != FT ) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002356 // The existing function doesn't have the same type. This is an overload
2357 // error.
2358 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Duncan Sandsdc024672007-11-27 13:23:08 +00002359 } else if (Fn->getParamAttrs() != PAL) {
2360 // The existing function doesn't have the same parameter attributes.
2361 // This is an overload error.
2362 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002363 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
Chris Lattner6cdc6822007-04-26 05:31:05 +00002364 // Neither the existing or the current function is a declaration and they
2365 // have the same name and same type. Clearly this is a redefinition.
2366 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Duncan Sandsdc024672007-11-27 13:23:08 +00002367 } else if (Fn->isDeclaration()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002368 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattner58af2a12006-02-15 07:22:58 +00002369 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2370 AI != AE; ++AI)
2371 AI->setName("");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002372 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002373 } else { // Not already defined?
Gabor Greife64d2482008-04-06 23:07:54 +00002374 Fn = Function::Create(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
2375 CurModule.CurrentModule);
Chris Lattner58af2a12006-02-15 07:22:58 +00002376 InsertValue(Fn, CurModule.Values);
2377 }
2378
2379 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002380
2381 if (CurFun.isDeclare) {
2382 // If we have declaration, always overwrite linkage. This will allow us to
2383 // correctly handle cases, when pointer to function is passed as argument to
2384 // another function.
2385 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002386 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002387 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002388 Fn->setCallingConv($1);
Duncan Sandsdc024672007-11-27 13:23:08 +00002389 Fn->setParamAttrs(PAL);
Reid Spencer218ded22007-01-05 17:07:23 +00002390 Fn->setAlignment($9);
2391 if ($8) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002392 Fn->setSection(*$8);
2393 delete $8;
Chris Lattner58af2a12006-02-15 07:22:58 +00002394 }
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002395 if ($10) {
2396 Fn->setCollector($10->c_str());
2397 delete $10;
2398 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002399
2400 // Add all of the arguments we parsed to the function...
2401 if ($5) { // Is null if empty...
2402 if (isVarArg) { // Nuke the last entry
Reid Spenceref9b9a72007-02-05 20:47:22 +00002403 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0 &&
Reid Spencera9720f52007-02-05 17:04:00 +00002404 "Not a varargs marker!");
Reid Spencer14310612006-12-31 05:40:51 +00002405 delete $5->back().Ty;
Chris Lattner58af2a12006-02-15 07:22:58 +00002406 $5->pop_back(); // Delete the last entry
2407 }
2408 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002409 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencer14310612006-12-31 05:40:51 +00002410 unsigned Idx = 1;
Reid Spenceref9b9a72007-02-05 20:47:22 +00002411 for (ArgListType::iterator I = $5->begin();
2412 I != $5->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencer14310612006-12-31 05:40:51 +00002413 delete I->Ty; // Delete the typeholder...
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002414 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002415 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002416 InsertValue(ArgIt);
Reid Spencer14310612006-12-31 05:40:51 +00002417 Idx++;
Chris Lattner58af2a12006-02-15 07:22:58 +00002418 }
Reid Spencera132e042006-12-03 05:46:11 +00002419
Chris Lattner58af2a12006-02-15 07:22:58 +00002420 delete $5; // We're now done with the argument list
2421 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002422 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002423};
2424
2425BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2426
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002427FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner58af2a12006-02-15 07:22:58 +00002428 $$ = CurFun.CurrentFunction;
2429
2430 // Make sure that we keep track of the linkage type even if there was a
2431 // previous "declare".
2432 $$->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002433 $$->setVisibility($2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002434};
2435
2436END : ENDTOK | '}'; // Allow end of '}' to end a function
2437
2438Function : BasicBlockList END {
2439 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002440 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002441};
2442
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002443FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencer14310612006-12-31 05:40:51 +00002444 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002445 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002446 $$ = CurFun.CurrentFunction;
2447 CurFun.FunctionDone();
2448 CHECK_FOR_ERROR
2449 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002450
2451//===----------------------------------------------------------------------===//
2452// Rules to match Basic Blocks
2453//===----------------------------------------------------------------------===//
2454
2455OptSideEffect : /* empty */ {
2456 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002457 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002458 }
2459 | SIDEEFFECT {
2460 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002461 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002462 };
2463
2464ConstValueRef : ESINT64VAL { // A reference to a direct constant
2465 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002466 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002467 }
2468 | EUINT64VAL {
2469 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002470 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002471 }
Chris Lattner1913b942008-07-11 00:30:39 +00002472 | ESAPINTVAL { // arbitrary precision integer constants
2473 $$ = ValID::create(*$1, true);
2474 delete $1;
2475 CHECK_FOR_ERROR
2476 }
2477 | EUAPINTVAL { // arbitrary precision integer constants
2478 $$ = ValID::create(*$1, false);
2479 delete $1;
2480 CHECK_FOR_ERROR
2481 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002482 | FPVAL { // Perhaps it's an FP constant?
2483 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002484 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002485 }
2486 | TRUETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002487 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002488 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002489 }
2490 | FALSETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002491 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002492 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002493 }
2494 | NULL_TOK {
2495 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002496 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002497 }
2498 | UNDEF {
2499 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002500 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002501 }
2502 | ZEROINITIALIZER { // A vector zero constant.
2503 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002504 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002505 }
2506 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencera132e042006-12-03 05:46:11 +00002507 const Type *ETy = (*$2)[0]->getType();
Dan Gohman180c1692008-06-23 18:43:26 +00002508 unsigned NumElements = $2->size();
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002509
2510 if (!ETy->isInteger() && !ETy->isFloatingPoint())
2511 GEN_ERROR("Invalid vector element type: " + ETy->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002512
Reid Spencer9d6565a2007-02-15 02:26:10 +00002513 VectorType* pt = VectorType::get(ETy, NumElements);
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002514 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(pt));
Chris Lattner58af2a12006-02-15 07:22:58 +00002515
2516 // Verify all elements are correct type!
2517 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00002518 if (ETy != (*$2)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002519 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00002520 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencera132e042006-12-03 05:46:11 +00002521 (*$2)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00002522 }
2523
Reid Spencer9d6565a2007-02-15 02:26:10 +00002524 $$ = ValID::create(ConstantVector::get(pt, *$2));
Chris Lattner58af2a12006-02-15 07:22:58 +00002525 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002526 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002527 }
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002528 | '[' ConstVector ']' { // Nonempty unsized arr
2529 const Type *ETy = (*$2)[0]->getType();
Dan Gohman180c1692008-06-23 18:43:26 +00002530 uint64_t NumElements = $2->size();
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002531
2532 if (!ETy->isFirstClassType())
2533 GEN_ERROR("Invalid array element type: " + ETy->getDescription());
2534
2535 ArrayType *ATy = ArrayType::get(ETy, NumElements);
2536 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(ATy));
2537
2538 // Verify all elements are correct type!
2539 for (unsigned i = 0; i < $2->size(); i++) {
2540 if (ETy != (*$2)[i]->getType())
2541 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
2542 ETy->getDescription() +"' as required!\nIt is of type '"+
2543 (*$2)[i]->getType()->getDescription() + "'.");
2544 }
2545
2546 $$ = ValID::create(ConstantArray::get(ATy, *$2));
2547 delete PTy; delete $2;
2548 CHECK_FOR_ERROR
2549 }
2550 | '[' ']' {
Dan Gohman180c1692008-06-23 18:43:26 +00002551 // Use undef instead of an array because it's inconvenient to determine
2552 // the element type at this point, there being no elements to examine.
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002553 $$ = ValID::createUndef();
2554 CHECK_FOR_ERROR
2555 }
2556 | 'c' STRINGCONSTANT {
Dan Gohman180c1692008-06-23 18:43:26 +00002557 uint64_t NumElements = $2->length();
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002558 const Type *ETy = Type::Int8Ty;
2559
2560 ArrayType *ATy = ArrayType::get(ETy, NumElements);
2561
2562 std::vector<Constant*> Vals;
2563 for (unsigned i = 0; i < $2->length(); ++i)
2564 Vals.push_back(ConstantInt::get(ETy, (*$2)[i]));
2565 delete $2;
2566 $$ = ValID::create(ConstantArray::get(ATy, Vals));
2567 CHECK_FOR_ERROR
2568 }
2569 | '{' ConstVector '}' {
2570 std::vector<const Type*> Elements($2->size());
2571 for (unsigned i = 0, e = $2->size(); i != e; ++i)
2572 Elements[i] = (*$2)[i]->getType();
2573
2574 const StructType *STy = StructType::get(Elements);
2575 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(STy));
2576
2577 $$ = ValID::create(ConstantStruct::get(STy, *$2));
2578 delete PTy; delete $2;
2579 CHECK_FOR_ERROR
2580 }
2581 | '{' '}' {
2582 const StructType *STy = StructType::get(std::vector<const Type*>());
2583 $$ = ValID::create(ConstantStruct::get(STy, std::vector<Constant*>()));
2584 CHECK_FOR_ERROR
2585 }
2586 | '<' '{' ConstVector '}' '>' {
2587 std::vector<const Type*> Elements($3->size());
2588 for (unsigned i = 0, e = $3->size(); i != e; ++i)
2589 Elements[i] = (*$3)[i]->getType();
2590
2591 const StructType *STy = StructType::get(Elements, /*isPacked=*/true);
2592 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(STy));
2593
2594 $$ = ValID::create(ConstantStruct::get(STy, *$3));
2595 delete PTy; delete $3;
2596 CHECK_FOR_ERROR
2597 }
2598 | '<' '{' '}' '>' {
2599 const StructType *STy = StructType::get(std::vector<const Type*>(),
2600 /*isPacked=*/true);
2601 $$ = ValID::create(ConstantStruct::get(STy, std::vector<Constant*>()));
2602 CHECK_FOR_ERROR
2603 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002604 | ConstExpr {
Reid Spencera132e042006-12-03 05:46:11 +00002605 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002606 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002607 }
2608 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002609 $$ = ValID::createInlineAsm(*$3, *$5, $2);
2610 delete $3;
2611 delete $5;
Reid Spencer61c83e02006-08-18 08:43:06 +00002612 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002613 };
2614
2615// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2616// another value.
2617//
Reid Spencer41dff5e2007-01-26 08:05:27 +00002618SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2619 $$ = ValID::createLocalID($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002620 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002621 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002622 | GLOBALVAL_ID {
2623 $$ = ValID::createGlobalID($1);
2624 CHECK_FOR_ERROR
2625 }
2626 | LocalName { // Is it a named reference...?
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002627 $$ = ValID::createLocalName(*$1);
2628 delete $1;
Reid Spencer41dff5e2007-01-26 08:05:27 +00002629 CHECK_FOR_ERROR
2630 }
2631 | GlobalName { // Is it a named reference...?
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002632 $$ = ValID::createGlobalName(*$1);
2633 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002634 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002635 };
2636
2637// ValueRef - A reference to a definition... either constant or symbolic
2638ValueRef : SymbolicValueRef | ConstValueRef;
2639
2640
2641// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2642// type immediately preceeds the value reference, and allows complex constant
2643// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2644ResolvedVal : Types ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002645 if (!UpRefs.empty())
2646 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2647 $$ = getVal(*$1, $2);
2648 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002649 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00002650 }
2651 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002652
Devang Patel7990dc72008-02-20 22:40:23 +00002653ReturnedVal : ResolvedVal {
2654 $$ = new std::vector<Value *>();
2655 $$->push_back($1);
2656 CHECK_FOR_ERROR
2657 }
Devang Patel6bfc63b2008-02-23 00:38:56 +00002658 | ReturnedVal ',' ResolvedVal {
Devang Patel7990dc72008-02-20 22:40:23 +00002659 ($$=$1)->push_back($3);
2660 CHECK_FOR_ERROR
2661 };
2662
Chris Lattner58af2a12006-02-15 07:22:58 +00002663BasicBlockList : BasicBlockList BasicBlock {
2664 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002665 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002666 }
2667 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
2668 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002669 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002670 };
2671
2672
2673// Basic blocks are terminated by branching instructions:
2674// br, br/cc, switch, ret
2675//
Reid Spencer41dff5e2007-01-26 08:05:27 +00002676BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner58af2a12006-02-15 07:22:58 +00002677 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002678 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002679 InsertValue($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002680 $1->getInstList().push_back($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002681 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002682 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002683 };
2684
2685InstructionList : InstructionList Inst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002686 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2687 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2688 if (CI2->getParent() == 0)
2689 $1->getInstList().push_back(CI2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002690 $1->getInstList().push_back($2);
2691 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002692 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002693 }
Reid Spencer93c40032007-03-19 18:40:50 +00002694 | /* empty */ { // Empty space between instruction lists
Nick Lewycky280a6e62008-04-25 16:53:59 +00002695 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
Reid Spencer61c83e02006-08-18 08:43:06 +00002696 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002697 }
Reid Spencer93c40032007-03-19 18:40:50 +00002698 | LABELSTR { // Labelled (named) basic block
Nick Lewycky280a6e62008-04-25 16:53:59 +00002699 $$ = defineBBVal(ValID::createLocalName(*$1));
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002700 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002701 CHECK_FOR_ERROR
Nick Lewycky280a6e62008-04-25 16:53:59 +00002702
Chris Lattner58af2a12006-02-15 07:22:58 +00002703 };
2704
Devang Patel7990dc72008-02-20 22:40:23 +00002705BBTerminatorInst :
2706 RET ReturnedVal { // Return with a result...
Devang Patelb82b7f22008-02-26 22:17:48 +00002707 ValueList &VL = *$2;
Devang Patel13b823c2008-02-26 23:19:08 +00002708 assert(!VL.empty() && "Invalid ret operands!");
Dan Gohman1a570242008-07-23 00:54:54 +00002709 const Type *ReturnType = CurFun.CurrentFunction->getReturnType();
2710 if (VL.size() > 1 ||
2711 (isa<StructType>(ReturnType) &&
2712 (VL.empty() || VL[0]->getType() != ReturnType))) {
2713 Value *RV = UndefValue::get(ReturnType);
2714 for (unsigned i = 0, e = VL.size(); i != e; ++i) {
2715 Instruction *I = InsertValueInst::Create(RV, VL[i], i, "mrv");
2716 ($<BasicBlockVal>-1)->getInstList().push_back(I);
2717 RV = I;
2718 }
2719 $$ = ReturnInst::Create(RV);
2720 } else {
2721 $$ = ReturnInst::Create(VL[0]);
2722 }
Devang Patel7990dc72008-02-20 22:40:23 +00002723 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002724 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002725 }
Reid Spencer93c40032007-03-19 18:40:50 +00002726 | RET VOID { // Return with no result...
Gabor Greife64d2482008-04-06 23:07:54 +00002727 $$ = ReturnInst::Create();
Reid Spencer61c83e02006-08-18 08:43:06 +00002728 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002729 }
Reid Spencer93c40032007-03-19 18:40:50 +00002730 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002731 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002732 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002733 $$ = BranchInst::Create(tmpBB);
Reid Spencer93c40032007-03-19 18:40:50 +00002734 } // Conditional Branch...
Reid Spencer6f407902007-01-13 05:00:46 +00002735 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002736 if (cast<IntegerType>($2)->getBitWidth() != 1)
2737 GEN_ERROR("Branch condition must have type i1");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002738 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002739 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002740 BasicBlock* tmpBBB = getBBVal($9);
2741 CHECK_FOR_ERROR
Reid Spencer4fe16d62007-01-11 18:21:29 +00002742 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002743 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002744 $$ = BranchInst::Create(tmpBBA, tmpBBB, tmpVal);
Chris Lattner58af2a12006-02-15 07:22:58 +00002745 }
2746 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002747 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002748 CHECK_FOR_ERROR
2749 BasicBlock* tmpBB = getBBVal($6);
2750 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002751 SwitchInst *S = SwitchInst::Create(tmpVal, tmpBB, $8->size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002752 $$ = S;
2753
2754 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2755 E = $8->end();
2756 for (; I != E; ++I) {
2757 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2758 S->addCase(CI, I->second);
2759 else
Reid Spencerb5334b02007-02-05 10:18:06 +00002760 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattner58af2a12006-02-15 07:22:58 +00002761 }
2762 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002763 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002764 }
2765 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002766 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002767 CHECK_FOR_ERROR
2768 BasicBlock* tmpBB = getBBVal($6);
2769 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002770 SwitchInst *S = SwitchInst::Create(tmpVal, tmpBB, 0);
Chris Lattner58af2a12006-02-15 07:22:58 +00002771 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002772 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002773 }
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002774 | INVOKE OptCallingConv ResultTypes ValueRef '(' ParamList ')' OptFuncAttrs
Chris Lattner58af2a12006-02-15 07:22:58 +00002775 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattner58af2a12006-02-15 07:22:58 +00002776
Reid Spencer14310612006-12-31 05:40:51 +00002777 // Handle the short syntax
2778 const PointerType *PFTy = 0;
2779 const FunctionType *Ty = 0;
Reid Spencer218ded22007-01-05 17:07:23 +00002780 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00002781 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2782 // Pull out the types of all of the arguments...
2783 std::vector<const Type*> ParamTypes;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002784 ParamList::iterator I = $6->begin(), E = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002785 for (; I != E; ++I) {
Reid Spencer14310612006-12-31 05:40:51 +00002786 const Type *Ty = I->Val->getType();
2787 if (Ty == Type::VoidTy)
2788 GEN_ERROR("Short call syntax cannot be used with varargs");
2789 ParamTypes.push_back(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002790 }
Chris Lattnera925a142008-04-23 05:37:08 +00002791
2792 if (!FunctionType::isValidReturnType(*$3))
2793 GEN_ERROR("Invalid result type for LLVM function");
2794
Duncan Sandsdc024672007-11-27 13:23:08 +00002795 Ty = FunctionType::get($3->get(), ParamTypes, false);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002796 PFTy = PointerType::getUnqual(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002797 }
2798
Reid Spencer66728ef2007-03-20 01:13:36 +00002799 delete $3;
2800
Chris Lattner58af2a12006-02-15 07:22:58 +00002801 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002802 CHECK_FOR_ERROR
Reid Spencer218ded22007-01-05 17:07:23 +00002803 BasicBlock *Normal = getBBVal($11);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002804 CHECK_FOR_ERROR
Reid Spencer218ded22007-01-05 17:07:23 +00002805 BasicBlock *Except = getBBVal($14);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002806 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002807
Chris Lattner58d74912008-03-12 17:45:29 +00002808 SmallVector<ParamAttrsWithIndex, 8> Attrs;
2809 if ($8 != ParamAttr::None)
2810 Attrs.push_back(ParamAttrsWithIndex::get(0, $8));
Duncan Sandsdc024672007-11-27 13:23:08 +00002811
Reid Spencer14310612006-12-31 05:40:51 +00002812 // Check the arguments
2813 ValueList Args;
2814 if ($6->empty()) { // Has no arguments?
2815 // Make sure no arguments is a good thing!
2816 if (Ty->getNumParams() != 0)
2817 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00002818 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00002819 } else { // Has arguments?
2820 // Loop through FunctionType's arguments and ensure they are specified
2821 // correctly!
Chris Lattner58af2a12006-02-15 07:22:58 +00002822 FunctionType::param_iterator I = Ty->param_begin();
2823 FunctionType::param_iterator E = Ty->param_end();
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002824 ParamList::iterator ArgI = $6->begin(), ArgE = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002825 unsigned index = 1;
Chris Lattner58af2a12006-02-15 07:22:58 +00002826
Duncan Sandsdc024672007-11-27 13:23:08 +00002827 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002828 if (ArgI->Val->getType() != *I)
2829 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00002830 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00002831 Args.push_back(ArgI->Val);
Chris Lattner58d74912008-03-12 17:45:29 +00002832 if (ArgI->Attrs != ParamAttr::None)
2833 Attrs.push_back(ParamAttrsWithIndex::get(index, ArgI->Attrs));
Reid Spencer14310612006-12-31 05:40:51 +00002834 }
Reid Spencera132e042006-12-03 05:46:11 +00002835
Reid Spencer14310612006-12-31 05:40:51 +00002836 if (Ty->isVarArg()) {
2837 if (I == E)
Chris Lattner38905612008-02-19 04:36:25 +00002838 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002839 Args.push_back(ArgI->Val); // push the remaining varargs
Chris Lattner58d74912008-03-12 17:45:29 +00002840 if (ArgI->Attrs != ParamAttr::None)
2841 Attrs.push_back(ParamAttrsWithIndex::get(index, ArgI->Attrs));
Chris Lattner38905612008-02-19 04:36:25 +00002842 }
Reid Spencer14310612006-12-31 05:40:51 +00002843 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00002844 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00002845 }
Reid Spencer14310612006-12-31 05:40:51 +00002846
Chris Lattner58d74912008-03-12 17:45:29 +00002847 PAListPtr PAL;
Duncan Sandsdc024672007-11-27 13:23:08 +00002848 if (!Attrs.empty())
Chris Lattner58d74912008-03-12 17:45:29 +00002849 PAL = PAListPtr::get(Attrs.begin(), Attrs.end());
Duncan Sandsdc024672007-11-27 13:23:08 +00002850
Reid Spencer14310612006-12-31 05:40:51 +00002851 // Create the InvokeInst
Dan Gohman041e2eb2008-05-15 19:50:34 +00002852 InvokeInst *II = InvokeInst::Create(V, Normal, Except,
2853 Args.begin(), Args.end());
Reid Spencer14310612006-12-31 05:40:51 +00002854 II->setCallingConv($2);
Duncan Sandsdc024672007-11-27 13:23:08 +00002855 II->setParamAttrs(PAL);
Reid Spencer14310612006-12-31 05:40:51 +00002856 $$ = II;
Chris Lattner58af2a12006-02-15 07:22:58 +00002857 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002858 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002859 }
2860 | UNWIND {
2861 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002862 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002863 }
2864 | UNREACHABLE {
2865 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002866 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002867 };
2868
2869
2870
2871JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2872 $$ = $1;
Reid Spencer93c40032007-03-19 18:40:50 +00002873 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002874 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002875 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002876 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00002877
Reid Spencer5b7e7532006-09-28 19:28:24 +00002878 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002879 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002880 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002881 }
2882 | IntType ConstValueRef ',' LABEL ValueRef {
2883 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencer93c40032007-03-19 18:40:50 +00002884 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002885 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002886
2887 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002888 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00002889
Reid Spencer5b7e7532006-09-28 19:28:24 +00002890 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002891 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002892 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002893 };
2894
Reid Spencer41dff5e2007-01-26 08:05:27 +00002895Inst : OptLocalAssign InstVal {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002896 // Is this definition named?? if so, assign the name...
2897 setValueName($2, $1);
2898 CHECK_FOR_ERROR
2899 InsertValue($2);
2900 $$ = $2;
2901 CHECK_FOR_ERROR
2902 };
2903
Chris Lattner58af2a12006-02-15 07:22:58 +00002904
2905PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer14310612006-12-31 05:40:51 +00002906 if (!UpRefs.empty())
2907 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002908 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencera132e042006-12-03 05:46:11 +00002909 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002910 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002911 BasicBlock* tmpBB = getBBVal($5);
2912 CHECK_FOR_ERROR
2913 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencera132e042006-12-03 05:46:11 +00002914 delete $1;
Chris Lattner58af2a12006-02-15 07:22:58 +00002915 }
2916 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2917 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002918 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002919 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002920 BasicBlock* tmpBB = getBBVal($6);
2921 CHECK_FOR_ERROR
2922 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002923 };
2924
2925
Duncan Sandsdc024672007-11-27 13:23:08 +00002926ParamList : Types OptParamAttrs ValueRef OptParamAttrs {
2927 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Reid Spencer14310612006-12-31 05:40:51 +00002928 if (!UpRefs.empty())
2929 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2930 // Used for call and invoke instructions
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002931 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00002932 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getVal($1->get(), $3);
Reid Spencer14310612006-12-31 05:40:51 +00002933 $$->push_back(E);
Reid Spencer66728ef2007-03-20 01:13:36 +00002934 delete $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002935 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002936 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002937 | LABEL OptParamAttrs ValueRef OptParamAttrs {
2938 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002939 // Labels are only valid in ASMs
2940 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00002941 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getBBVal($3);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002942 $$->push_back(E);
Duncan Sandsdc024672007-11-27 13:23:08 +00002943 CHECK_FOR_ERROR
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002944 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002945 | ParamList ',' Types OptParamAttrs ValueRef OptParamAttrs {
2946 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Reid Spencer14310612006-12-31 05:40:51 +00002947 if (!UpRefs.empty())
2948 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002949 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002950 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getVal($3->get(), $5);
Reid Spencer14310612006-12-31 05:40:51 +00002951 $$->push_back(E);
Reid Spencer66728ef2007-03-20 01:13:36 +00002952 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002953 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00002954 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002955 | ParamList ',' LABEL OptParamAttrs ValueRef OptParamAttrs {
2956 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002957 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002958 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getBBVal($5);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002959 $$->push_back(E);
2960 CHECK_FOR_ERROR
2961 }
2962 | /*empty*/ { $$ = new ParamList(); };
Chris Lattner58af2a12006-02-15 07:22:58 +00002963
Reid Spencer14310612006-12-31 05:40:51 +00002964IndexList // Used for gep instructions and constant expressions
Reid Spencerc6c59fd2006-12-31 21:47:02 +00002965 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencer14310612006-12-31 05:40:51 +00002966 | IndexList ',' ResolvedVal {
2967 $$ = $1;
2968 $$->push_back($3);
2969 CHECK_FOR_ERROR
2970 }
Reid Spencerc6c59fd2006-12-31 21:47:02 +00002971 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002972
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002973ConstantIndexList // Used for insertvalue and extractvalue instructions
2974 : ',' EUINT64VAL {
2975 $$ = new std::vector<unsigned>();
2976 if ((unsigned)$2 != $2)
2977 GEN_ERROR("Index " + utostr($2) + " is not valid for insertvalue or extractvalue.");
2978 $$->push_back($2);
2979 }
2980 | ConstantIndexList ',' EUINT64VAL {
2981 $$ = $1;
2982 if ((unsigned)$3 != $3)
2983 GEN_ERROR("Index " + utostr($3) + " is not valid for insertvalue or extractvalue.");
2984 $$->push_back($3);
2985 CHECK_FOR_ERROR
2986 }
2987 ;
2988
Chris Lattner58af2a12006-02-15 07:22:58 +00002989OptTailCall : TAIL CALL {
2990 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002991 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002992 }
2993 | CALL {
2994 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002995 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002996 };
2997
Chris Lattner58af2a12006-02-15 07:22:58 +00002998InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002999 if (!UpRefs.empty())
3000 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00003001 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencer9d6565a2007-02-15 02:26:10 +00003002 !isa<VectorType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003003 GEN_ERROR(
Reid Spencerb5334b02007-02-05 10:18:06 +00003004 "Arithmetic operator requires integer, FP, or packed operands");
Reid Spencera132e042006-12-03 05:46:11 +00003005 Value* val1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003006 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003007 Value* val2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003008 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003009 $$ = BinaryOperator::Create($1, val1, val2);
Chris Lattner58af2a12006-02-15 07:22:58 +00003010 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003011 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00003012 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003013 }
3014 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00003015 if (!UpRefs.empty())
3016 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00003017 if (!(*$2)->isInteger()) {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003018 if (!isa<VectorType>($2->get()) ||
Reid Spencer9d6565a2007-02-15 02:26:10 +00003019 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00003020 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00003021 }
Reid Spencera132e042006-12-03 05:46:11 +00003022 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003023 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003024 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00003025 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003026 $$ = BinaryOperator::Create($1, tmpVal1, tmpVal2);
Chris Lattner58af2a12006-02-15 07:22:58 +00003027 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003028 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00003029 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003030 }
Reid Spencera132e042006-12-03 05:46:11 +00003031 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00003032 if (!UpRefs.empty())
3033 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00003034 if (isa<VectorType>((*$3).get()))
Chris Lattner32980692007-02-19 07:44:24 +00003035 GEN_ERROR("Vector types not supported by icmp instruction");
Reid Spencera132e042006-12-03 05:46:11 +00003036 Value* tmpVal1 = getVal(*$3, $4);
3037 CHECK_FOR_ERROR
3038 Value* tmpVal2 = getVal(*$3, $6);
3039 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003040 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Reid Spencera132e042006-12-03 05:46:11 +00003041 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003042 GEN_ERROR("icmp operator returned null");
Reid Spencer66728ef2007-03-20 01:13:36 +00003043 delete $3;
Reid Spencera132e042006-12-03 05:46:11 +00003044 }
3045 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00003046 if (!UpRefs.empty())
3047 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00003048 if (isa<VectorType>((*$3).get()))
Chris Lattner32980692007-02-19 07:44:24 +00003049 GEN_ERROR("Vector types not supported by fcmp instruction");
Reid Spencera132e042006-12-03 05:46:11 +00003050 Value* tmpVal1 = getVal(*$3, $4);
3051 CHECK_FOR_ERROR
3052 Value* tmpVal2 = getVal(*$3, $6);
3053 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003054 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Reid Spencera132e042006-12-03 05:46:11 +00003055 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003056 GEN_ERROR("fcmp operator returned null");
Reid Spencer66728ef2007-03-20 01:13:36 +00003057 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00003058 }
Nate Begemanac80ade2008-05-12 19:01:56 +00003059 | VICMP IPredicates Types ValueRef ',' ValueRef {
3060 if (!UpRefs.empty())
3061 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
3062 if (!isa<VectorType>((*$3).get()))
3063 GEN_ERROR("Scalar types not supported by vicmp instruction");
3064 Value* tmpVal1 = getVal(*$3, $4);
3065 CHECK_FOR_ERROR
3066 Value* tmpVal2 = getVal(*$3, $6);
3067 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003068 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Nate Begemanac80ade2008-05-12 19:01:56 +00003069 if ($$ == 0)
3070 GEN_ERROR("icmp operator returned null");
3071 delete $3;
3072 }
3073 | VFCMP FPredicates Types ValueRef ',' ValueRef {
3074 if (!UpRefs.empty())
3075 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
3076 if (!isa<VectorType>((*$3).get()))
3077 GEN_ERROR("Scalar types not supported by vfcmp instruction");
3078 Value* tmpVal1 = getVal(*$3, $4);
3079 CHECK_FOR_ERROR
3080 Value* tmpVal2 = getVal(*$3, $6);
3081 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003082 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Nate Begemanac80ade2008-05-12 19:01:56 +00003083 if ($$ == 0)
3084 GEN_ERROR("fcmp operator returned null");
3085 delete $3;
3086 }
Reid Spencer3da59db2006-11-27 01:05:10 +00003087 | CastOps ResolvedVal TO Types {
Reid Spencer14310612006-12-31 05:40:51 +00003088 if (!UpRefs.empty())
3089 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003090 Value* Val = $2;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00003091 const Type* DestTy = $4->get();
3092 if (!CastInst::castIsValid($1, Val, DestTy))
3093 GEN_ERROR("invalid cast opcode for cast from '" +
3094 Val->getType()->getDescription() + "' to '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00003095 DestTy->getDescription() + "'");
Dan Gohmane4977cf2008-05-23 01:55:30 +00003096 $$ = CastInst::Create($1, Val, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00003097 delete $4;
Chris Lattner58af2a12006-02-15 07:22:58 +00003098 }
3099 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer4fe16d62007-01-11 18:21:29 +00003100 if ($2->getType() != Type::Int1Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00003101 GEN_ERROR("select condition must be boolean");
Reid Spencera132e042006-12-03 05:46:11 +00003102 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00003103 GEN_ERROR("select value types should match");
Gabor Greife64d2482008-04-06 23:07:54 +00003104 $$ = SelectInst::Create($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003105 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003106 }
3107 | VAARG ResolvedVal ',' Types {
Reid Spencer14310612006-12-31 05:40:51 +00003108 if (!UpRefs.empty())
3109 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003110 $$ = new VAArgInst($2, *$4);
3111 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00003112 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003113 }
Chris Lattner58af2a12006-02-15 07:22:58 +00003114 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003115 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerb5334b02007-02-05 10:18:06 +00003116 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00003117 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003118 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003119 }
3120 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003121 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00003122 GEN_ERROR("Invalid insertelement operands");
Gabor Greife64d2482008-04-06 23:07:54 +00003123 $$ = InsertElementInst::Create($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003124 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003125 }
Chris Lattnerd5efe842006-04-08 01:18:56 +00003126 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003127 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00003128 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00003129 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003130 CHECK_FOR_ERROR
Chris Lattnerd5efe842006-04-08 01:18:56 +00003131 }
Chris Lattner58af2a12006-02-15 07:22:58 +00003132 | PHI_TOK PHIList {
3133 const Type *Ty = $2->front().first->getType();
3134 if (!Ty->isFirstClassType())
Reid Spencerb5334b02007-02-05 10:18:06 +00003135 GEN_ERROR("PHI node operands must be of first class type");
Gabor Greife64d2482008-04-06 23:07:54 +00003136 $$ = PHINode::Create(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00003137 ((PHINode*)$$)->reserveOperandSpace($2->size());
3138 while ($2->begin() != $2->end()) {
3139 if ($2->front().first->getType() != Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00003140 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattner58af2a12006-02-15 07:22:58 +00003141 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
3142 $2->pop_front();
3143 }
3144 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00003145 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003146 }
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003147 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ParamList ')'
Reid Spencer218ded22007-01-05 17:07:23 +00003148 OptFuncAttrs {
Reid Spencer14310612006-12-31 05:40:51 +00003149
3150 // Handle the short syntax
Reid Spencer3da59db2006-11-27 01:05:10 +00003151 const PointerType *PFTy = 0;
3152 const FunctionType *Ty = 0;
Reid Spencer218ded22007-01-05 17:07:23 +00003153 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00003154 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3155 // Pull out the types of all of the arguments...
3156 std::vector<const Type*> ParamTypes;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003157 ParamList::iterator I = $6->begin(), E = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00003158 for (; I != E; ++I) {
Reid Spencer14310612006-12-31 05:40:51 +00003159 const Type *Ty = I->Val->getType();
3160 if (Ty == Type::VoidTy)
3161 GEN_ERROR("Short call syntax cannot be used with varargs");
3162 ParamTypes.push_back(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00003163 }
Chris Lattnera925a142008-04-23 05:37:08 +00003164
3165 if (!FunctionType::isValidReturnType(*$3))
3166 GEN_ERROR("Invalid result type for LLVM function");
3167
Duncan Sandsdc024672007-11-27 13:23:08 +00003168 Ty = FunctionType::get($3->get(), ParamTypes, false);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00003169 PFTy = PointerType::getUnqual(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00003170 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00003171
Chris Lattner58af2a12006-02-15 07:22:58 +00003172 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00003173 CHECK_FOR_ERROR
Chris Lattner6cdc6822007-04-26 05:31:05 +00003174
Reid Spencer7780acb2007-04-16 06:56:07 +00003175 // Check for call to invalid intrinsic to avoid crashing later.
3176 if (Function *theF = dyn_cast<Function>(V)) {
Reid Spencered48de22007-04-16 22:02:23 +00003177 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
Reid Spencer36fdde12007-04-16 20:35:38 +00003178 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
3179 !theF->getIntrinsicID(true))
Reid Spencer7780acb2007-04-16 06:56:07 +00003180 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
3181 theF->getName() + "'");
3182 }
3183
Duncan Sandsdc024672007-11-27 13:23:08 +00003184 // Set up the ParamAttrs for the function
Chris Lattner58d74912008-03-12 17:45:29 +00003185 SmallVector<ParamAttrsWithIndex, 8> Attrs;
3186 if ($8 != ParamAttr::None)
3187 Attrs.push_back(ParamAttrsWithIndex::get(0, $8));
Reid Spencer14310612006-12-31 05:40:51 +00003188 // Check the arguments
3189 ValueList Args;
3190 if ($6->empty()) { // Has no arguments?
Chris Lattner58af2a12006-02-15 07:22:58 +00003191 // Make sure no arguments is a good thing!
3192 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00003193 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00003194 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00003195 } else { // Has arguments?
3196 // Loop through FunctionType's arguments and ensure they are specified
Duncan Sandsdc024672007-11-27 13:23:08 +00003197 // correctly. Also, gather any parameter attributes.
Chris Lattner58af2a12006-02-15 07:22:58 +00003198 FunctionType::param_iterator I = Ty->param_begin();
3199 FunctionType::param_iterator E = Ty->param_end();
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003200 ParamList::iterator ArgI = $6->begin(), ArgE = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00003201 unsigned index = 1;
Chris Lattner58af2a12006-02-15 07:22:58 +00003202
Duncan Sandsdc024672007-11-27 13:23:08 +00003203 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00003204 if (ArgI->Val->getType() != *I)
3205 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00003206 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00003207 Args.push_back(ArgI->Val);
Chris Lattner58d74912008-03-12 17:45:29 +00003208 if (ArgI->Attrs != ParamAttr::None)
3209 Attrs.push_back(ParamAttrsWithIndex::get(index, ArgI->Attrs));
Reid Spencer14310612006-12-31 05:40:51 +00003210 }
3211 if (Ty->isVarArg()) {
3212 if (I == E)
Chris Lattner38905612008-02-19 04:36:25 +00003213 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00003214 Args.push_back(ArgI->Val); // push the remaining varargs
Chris Lattner58d74912008-03-12 17:45:29 +00003215 if (ArgI->Attrs != ParamAttr::None)
3216 Attrs.push_back(ParamAttrsWithIndex::get(index, ArgI->Attrs));
Chris Lattner38905612008-02-19 04:36:25 +00003217 }
Reid Spencer14310612006-12-31 05:40:51 +00003218 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00003219 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00003220 }
Duncan Sandsdc024672007-11-27 13:23:08 +00003221
3222 // Finish off the ParamAttrs and check them
Chris Lattner58d74912008-03-12 17:45:29 +00003223 PAListPtr PAL;
Duncan Sandsdc024672007-11-27 13:23:08 +00003224 if (!Attrs.empty())
Chris Lattner58d74912008-03-12 17:45:29 +00003225 PAL = PAListPtr::get(Attrs.begin(), Attrs.end());
Duncan Sandsdc024672007-11-27 13:23:08 +00003226
Reid Spencer14310612006-12-31 05:40:51 +00003227 // Create the call node
Gabor Greife64d2482008-04-06 23:07:54 +00003228 CallInst *CI = CallInst::Create(V, Args.begin(), Args.end());
Reid Spencer14310612006-12-31 05:40:51 +00003229 CI->setTailCall($1);
3230 CI->setCallingConv($2);
Duncan Sandsdc024672007-11-27 13:23:08 +00003231 CI->setParamAttrs(PAL);
Reid Spencer14310612006-12-31 05:40:51 +00003232 $$ = CI;
Chris Lattner58af2a12006-02-15 07:22:58 +00003233 delete $6;
Reid Spencer41dff5e2007-01-26 08:05:27 +00003234 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00003235 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003236 }
3237 | MemoryInst {
3238 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00003239 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003240 };
3241
Chris Lattner58af2a12006-02-15 07:22:58 +00003242OptVolatile : VOLATILE {
3243 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00003244 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003245 }
3246 | /* empty */ {
3247 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00003248 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003249 };
3250
3251
3252
3253MemoryInst : MALLOC Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003254 if (!UpRefs.empty())
3255 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003256 $$ = new MallocInst(*$2, 0, $3);
3257 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003258 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003259 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003260 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003261 if (!UpRefs.empty())
3262 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003263 if ($4 != Type::Int32Ty)
3264 GEN_ERROR("Malloc array size is not a 32-bit integer!");
Reid Spencera132e042006-12-03 05:46:11 +00003265 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003266 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003267 $$ = new MallocInst(*$2, tmpVal, $6);
3268 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003269 }
3270 | ALLOCA Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003271 if (!UpRefs.empty())
3272 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003273 $$ = new AllocaInst(*$2, 0, $3);
3274 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003275 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003276 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003277 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003278 if (!UpRefs.empty())
3279 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003280 if ($4 != Type::Int32Ty)
3281 GEN_ERROR("Alloca array size is not a 32-bit integer!");
Reid Spencera132e042006-12-03 05:46:11 +00003282 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003283 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003284 $$ = new AllocaInst(*$2, tmpVal, $6);
3285 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003286 }
3287 | FREE ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003288 if (!isa<PointerType>($2->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003289 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerb5334b02007-02-05 10:18:06 +00003290 $2->getType()->getDescription() + "");
Reid Spencera132e042006-12-03 05:46:11 +00003291 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00003292 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003293 }
3294
Christopher Lamb5c104242007-04-22 20:09:11 +00003295 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003296 if (!UpRefs.empty())
3297 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003298 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003299 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003300 (*$3)->getDescription());
3301 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00003302 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003303 (*$3)->getDescription());
3304 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003305 CHECK_FOR_ERROR
Christopher Lamb5c104242007-04-22 20:09:11 +00003306 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencera132e042006-12-03 05:46:11 +00003307 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00003308 }
Christopher Lamb5c104242007-04-22 20:09:11 +00003309 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003310 if (!UpRefs.empty())
3311 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003312 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00003313 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00003314 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003315 (*$5)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00003316 const Type *ElTy = PT->getElementType();
Reid Spencera132e042006-12-03 05:46:11 +00003317 if (ElTy != $3->getType())
3318 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerb5334b02007-02-05 10:18:06 +00003319 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00003320
Reid Spencera132e042006-12-03 05:46:11 +00003321 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003322 CHECK_FOR_ERROR
Christopher Lamb5c104242007-04-22 20:09:11 +00003323 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencera132e042006-12-03 05:46:11 +00003324 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00003325 }
Dan Gohmane4977cf2008-05-23 01:55:30 +00003326 | GETRESULT Types ValueRef ',' EUINT64VAL {
Dan Gohman1a570242008-07-23 00:54:54 +00003327 if (!UpRefs.empty())
3328 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3329 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3330 GEN_ERROR("getresult insn requires an aggregate operand");
3331 if (!ExtractValueInst::getIndexedType(*$2, $5))
3332 GEN_ERROR("Invalid getresult index for type '" +
3333 (*$2)->getDescription()+ "'");
3334
3335 Value *tmpVal = getVal(*$2, $3);
Devang Patel5a970972008-02-19 22:27:01 +00003336 CHECK_FOR_ERROR
Dan Gohman1a570242008-07-23 00:54:54 +00003337 $$ = ExtractValueInst::Create(tmpVal, $5);
3338 delete $2;
Devang Patel5a970972008-02-19 22:27:01 +00003339 }
Chris Lattner58af2a12006-02-15 07:22:58 +00003340 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer14310612006-12-31 05:40:51 +00003341 if (!UpRefs.empty())
3342 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003343 if (!isa<PointerType>($2->get()))
Reid Spencerb5334b02007-02-05 10:18:06 +00003344 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00003345
Dan Gohman041e2eb2008-05-15 19:50:34 +00003346 if (!GetElementPtrInst::getIndexedType(*$2, $4->begin(), $4->end()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003347 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00003348 (*$2)->getDescription()+ "'");
Reid Spencera132e042006-12-03 05:46:11 +00003349 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003350 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00003351 $$ = GetElementPtrInst::Create(tmpVal, $4->begin(), $4->end());
Reid Spencera132e042006-12-03 05:46:11 +00003352 delete $2;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003353 delete $4;
Dan Gohmane4977cf2008-05-23 01:55:30 +00003354 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003355 | EXTRACTVALUE Types ValueRef ConstantIndexList {
Dan Gohmane4977cf2008-05-23 01:55:30 +00003356 if (!UpRefs.empty())
3357 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3358 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3359 GEN_ERROR("extractvalue insn requires an aggregate operand");
3360
3361 if (!ExtractValueInst::getIndexedType(*$2, $4->begin(), $4->end()))
3362 GEN_ERROR("Invalid extractvalue indices for type '" +
3363 (*$2)->getDescription()+ "'");
3364 Value* tmpVal = getVal(*$2, $3);
3365 CHECK_FOR_ERROR
3366 $$ = ExtractValueInst::Create(tmpVal, $4->begin(), $4->end());
3367 delete $2;
3368 delete $4;
3369 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003370 | INSERTVALUE Types ValueRef ',' Types ValueRef ConstantIndexList {
Dan Gohmane4977cf2008-05-23 01:55:30 +00003371 if (!UpRefs.empty())
3372 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3373 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3374 GEN_ERROR("extractvalue insn requires an aggregate operand");
3375
3376 if (ExtractValueInst::getIndexedType(*$2, $7->begin(), $7->end()) != $5->get())
3377 GEN_ERROR("Invalid insertvalue indices for type '" +
3378 (*$2)->getDescription()+ "'");
3379 Value* aggVal = getVal(*$2, $3);
3380 Value* tmpVal = getVal(*$5, $6);
3381 CHECK_FOR_ERROR
3382 $$ = InsertValueInst::Create(aggVal, tmpVal, $7->begin(), $7->end());
3383 delete $2;
3384 delete $5;
3385 delete $7;
Chris Lattner58af2a12006-02-15 07:22:58 +00003386 };
3387
3388
3389%%
Reid Spencer61c83e02006-08-18 08:43:06 +00003390
Reid Spencer14310612006-12-31 05:40:51 +00003391// common code from the two 'RunVMAsmParser' functions
3392static Module* RunParser(Module * M) {
Reid Spencer14310612006-12-31 05:40:51 +00003393 CurModule.CurrentModule = M;
Reid Spencer14310612006-12-31 05:40:51 +00003394 // Check to make sure the parser succeeded
3395 if (yyparse()) {
3396 if (ParserResult)
3397 delete ParserResult;
3398 return 0;
3399 }
3400
Reid Spencer0d60b5a2007-03-30 01:37:39 +00003401 // Emit an error if there are any unresolved types left.
3402 if (!CurModule.LateResolveTypes.empty()) {
3403 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3404 if (DID.Type == ValID::LocalName) {
3405 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3406 } else {
3407 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3408 }
3409 if (ParserResult)
3410 delete ParserResult;
3411 return 0;
3412 }
3413
3414 // Emit an error if there are any unresolved values left.
3415 if (!CurModule.LateResolveValues.empty()) {
3416 Value *V = CurModule.LateResolveValues.back();
3417 std::map<Value*, std::pair<ValID, int> >::iterator I =
3418 CurModule.PlaceHolderInfo.find(V);
3419
3420 if (I != CurModule.PlaceHolderInfo.end()) {
3421 ValID &DID = I->second.first;
3422 if (DID.Type == ValID::LocalName) {
3423 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3424 } else {
3425 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3426 }
3427 if (ParserResult)
3428 delete ParserResult;
3429 return 0;
3430 }
3431 }
3432
Reid Spencer14310612006-12-31 05:40:51 +00003433 // Check to make sure that parsing produced a result
3434 if (!ParserResult)
3435 return 0;
3436
3437 // Reset ParserResult variable while saving its value for the result.
3438 Module *Result = ParserResult;
3439 ParserResult = 0;
3440
3441 return Result;
3442}
3443
Reid Spencer61c83e02006-08-18 08:43:06 +00003444void llvm::GenerateError(const std::string &message, int LineNo) {
Duncan Sandsdc024672007-11-27 13:23:08 +00003445 if (LineNo == -1) LineNo = LLLgetLineNo();
Reid Spencer61c83e02006-08-18 08:43:06 +00003446 // TODO: column number in exception
3447 if (TheParseError)
Duncan Sandsdc024672007-11-27 13:23:08 +00003448 TheParseError->setError(LLLgetFilename(), message, LineNo);
Reid Spencer61c83e02006-08-18 08:43:06 +00003449 TriggerError = 1;
3450}
3451
Chris Lattner58af2a12006-02-15 07:22:58 +00003452int yyerror(const char *ErrorMsg) {
Duncan Sandsdc024672007-11-27 13:23:08 +00003453 std::string where = LLLgetFilename() + ":" + utostr(LLLgetLineNo()) + ": ";
Reid Spenceref9b9a72007-02-05 20:47:22 +00003454 std::string errMsg = where + "error: " + std::string(ErrorMsg);
Duncan Sandsdc024672007-11-27 13:23:08 +00003455 if (yychar != YYEMPTY && yychar != 0) {
3456 errMsg += " while reading token: '";
3457 errMsg += std::string(LLLgetTokenStart(),
3458 LLLgetTokenStart()+LLLgetTokenLength()) + "'";
3459 }
Reid Spencer61c83e02006-08-18 08:43:06 +00003460 GenerateError(errMsg);
Chris Lattner58af2a12006-02-15 07:22:58 +00003461 return 0;
3462}