blob: f84fffdcd62468e78ac60da16fcd0e9b4cf20767 [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
405 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattner38905612008-02-19 04:36:25 +0000406 if (!Ty->isFloatingPoint() ||
407 !ConstantFP::isValueValidForType(Ty, *D.ConstPoolFP)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000408 GenerateError("FP constant invalid for type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000409 return 0;
410 }
Chris Lattnerd8eb63f2008-04-20 00:41:19 +0000411 // Lexer has no type info, so builds all float and double FP constants
Dale Johannesenc72cd7e2007-09-11 18:33:39 +0000412 // as double. Fix this here. Long double does not need this.
413 if (&D.ConstPoolFP->getSemantics() == &APFloat::IEEEdouble &&
414 Ty==Type::FloatTy)
Dale Johannesen43421b32007-09-06 18:13:44 +0000415 D.ConstPoolFP->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
Chris Lattnerd8eb63f2008-04-20 00:41:19 +0000416 return ConstantFP::get(*D.ConstPoolFP);
Chris Lattner58af2a12006-02-15 07:22:58 +0000417
418 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000419 if (!isa<PointerType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000420 GenerateError("Cannot create a a non pointer null");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000421 return 0;
422 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000423 return ConstantPointerNull::get(cast<PointerType>(Ty));
424
425 case ValID::ConstUndefVal: // Is it an undef value?
426 return UndefValue::get(Ty);
427
428 case ValID::ConstZeroVal: // Is it a zero value?
429 return Constant::getNullValue(Ty);
430
431 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000432 if (D.ConstantValue->getType() != Ty) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000433 GenerateError("Constant expression type different from required type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000434 return 0;
435 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000436 return D.ConstantValue;
437
438 case ValID::InlineAsmVal: { // Inline asm expression
439 const PointerType *PTy = dyn_cast<PointerType>(Ty);
440 const FunctionType *FTy =
441 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000442 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000443 GenerateError("Invalid type for asm constraint string");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000444 return 0;
445 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000446 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
447 D.IAD->HasSideEffects);
448 D.destroy(); // Free InlineAsmDescriptor.
449 return IA;
450 }
451 default:
Reid Spencera9720f52007-02-05 17:04:00 +0000452 assert(0 && "Unhandled case!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000453 return 0;
454 } // End of switch
455
Reid Spencera9720f52007-02-05 17:04:00 +0000456 assert(0 && "Unhandled case!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000457 return 0;
458}
459
Reid Spencer93c40032007-03-19 18:40:50 +0000460// getVal - This function is identical to getExistingVal, except that if a
Chris Lattner58af2a12006-02-15 07:22:58 +0000461// value is not already defined, it "improvises" by creating a placeholder var
462// that looks and acts just like the requested variable. When the value is
463// defined later, all uses of the placeholder variable are replaced with the
464// real thing.
465//
466static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000467 if (Ty == Type::LabelTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000468 GenerateError("Cannot use a basic block here");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000469 return 0;
470 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000471
472 // See if the value has already been defined.
Reid Spencer93c40032007-03-19 18:40:50 +0000473 Value *V = getExistingVal(Ty, ID);
Chris Lattner58af2a12006-02-15 07:22:58 +0000474 if (V) return V;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000475 if (TriggerError) return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000476
Reid Spencer5b7e7532006-09-28 19:28:24 +0000477 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Dan Gohmane4977cf2008-05-23 01:55:30 +0000478 GenerateError("Invalid use of a non-first-class type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000479 return 0;
480 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000481
482 // If we reached here, we referenced either a symbol that we don't know about
483 // or an id number that hasn't been read yet. We may be referencing something
484 // forward, so just create an entry to be resolved later and get to it...
485 //
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000486 switch (ID.Type) {
487 case ValID::GlobalName:
Reid Spencer9c9b63a2007-04-28 16:07:31 +0000488 case ValID::GlobalID: {
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000489 const PointerType *PTy = dyn_cast<PointerType>(Ty);
490 if (!PTy) {
491 GenerateError("Invalid type for reference to global" );
492 return 0;
493 }
494 const Type* ElTy = PTy->getElementType();
495 if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy))
Gabor Greife64d2482008-04-06 23:07:54 +0000496 V = Function::Create(FTy, GlobalValue::ExternalLinkage);
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000497 else
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000498 V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage, 0, "",
499 (Module*)0, false, PTy->getAddressSpace());
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000500 break;
Reid Spencer9c9b63a2007-04-28 16:07:31 +0000501 }
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000502 default:
503 V = new Argument(Ty);
504 }
505
Chris Lattner58af2a12006-02-15 07:22:58 +0000506 // Remember where this forward reference came from. FIXME, shouldn't we try
507 // to recycle these things??
508 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Duncan Sandsdc024672007-11-27 13:23:08 +0000509 LLLgetLineNo())));
Chris Lattner58af2a12006-02-15 07:22:58 +0000510
511 if (inFunctionScope())
512 InsertValue(V, CurFun.LateResolveValues);
513 else
514 InsertValue(V, CurModule.LateResolveValues);
515 return V;
516}
517
Reid Spencer93c40032007-03-19 18:40:50 +0000518/// defineBBVal - This is a definition of a new basic block with the specified
519/// identifier which must be the same as CurFun.NextValNum, if its numeric.
Nick Lewycky280a6e62008-04-25 16:53:59 +0000520static BasicBlock *defineBBVal(const ValID &ID) {
Reid Spencera9720f52007-02-05 17:04:00 +0000521 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000522
Chris Lattner58af2a12006-02-15 07:22:58 +0000523 BasicBlock *BB = 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000524
Reid Spencer93c40032007-03-19 18:40:50 +0000525 // First, see if this was forward referenced
Chris Lattner58af2a12006-02-15 07:22:58 +0000526
Reid Spencer93c40032007-03-19 18:40:50 +0000527 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
528 if (BBI != CurFun.BBForwardRefs.end()) {
529 BB = BBI->second;
Chris Lattner58af2a12006-02-15 07:22:58 +0000530 // The forward declaration could have been inserted anywhere in the
531 // function: insert it into the correct place now.
532 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
533 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Reid Spencer93c40032007-03-19 18:40:50 +0000534
Reid Spencer66728ef2007-03-20 01:13:36 +0000535 // We're about to erase the entry, save the key so we can clean it up.
536 ValID Tmp = BBI->first;
537
Reid Spencer93c40032007-03-19 18:40:50 +0000538 // Erase the forward ref from the map as its no longer "forward"
539 CurFun.BBForwardRefs.erase(ID);
540
Reid Spencer66728ef2007-03-20 01:13:36 +0000541 // The key has been removed from the map but so we don't want to leave
542 // strdup'd memory around so destroy it too.
543 Tmp.destroy();
544
Reid Spencer93c40032007-03-19 18:40:50 +0000545 // If its a numbered definition, bump the number and set the BB value.
546 if (ID.Type == ValID::LocalID) {
547 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
548 InsertValue(BB);
549 }
Devang Patel67909432008-03-03 18:58:47 +0000550 } else {
551 // We haven't seen this BB before and its first mention is a definition.
552 // Just create it and return it.
553 std::string Name (ID.Type == ValID::LocalName ? ID.getName() : "");
Gabor Greife64d2482008-04-06 23:07:54 +0000554 BB = BasicBlock::Create(Name, CurFun.CurrentFunction);
Devang Patel67909432008-03-03 18:58:47 +0000555 if (ID.Type == ValID::LocalID) {
556 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
557 InsertValue(BB);
558 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000559 }
Reid Spencer93c40032007-03-19 18:40:50 +0000560
Devang Patel67909432008-03-03 18:58:47 +0000561 ID.destroy();
Reid Spencer93c40032007-03-19 18:40:50 +0000562 return BB;
563}
564
565/// getBBVal - get an existing BB value or create a forward reference for it.
566///
567static BasicBlock *getBBVal(const ValID &ID) {
568 assert(inFunctionScope() && "Can't get basic block at global scope!");
569
570 BasicBlock *BB = 0;
571
572 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
573 if (BBI != CurFun.BBForwardRefs.end()) {
574 BB = BBI->second;
575 } if (ID.Type == ValID::LocalName) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000576 std::string Name = ID.getName();
Reid Spencer93c40032007-03-19 18:40:50 +0000577 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000578 if (N) {
Reid Spencer93c40032007-03-19 18:40:50 +0000579 if (N->getType()->getTypeID() == Type::LabelTyID)
580 BB = cast<BasicBlock>(N);
581 else
582 GenerateError("Reference to label '" + Name + "' is actually of type '"+
583 N->getType()->getDescription() + "'");
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000584 }
Reid Spencer93c40032007-03-19 18:40:50 +0000585 } else if (ID.Type == ValID::LocalID) {
586 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
587 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
588 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
589 else
590 GenerateError("Reference to label '%" + utostr(ID.Num) +
591 "' is actually of type '"+
592 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
593 }
594 } else {
595 GenerateError("Illegal label reference " + ID.getName());
596 return 0;
597 }
598
599 // If its already been defined, return it now.
600 if (BB) {
601 ID.destroy(); // Free strdup'd memory.
602 return BB;
603 }
604
605 // Otherwise, this block has not been seen before, create it.
606 std::string Name;
607 if (ID.Type == ValID::LocalName)
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000608 Name = ID.getName();
Gabor Greife64d2482008-04-06 23:07:54 +0000609 BB = BasicBlock::Create(Name, CurFun.CurrentFunction);
Reid Spencer93c40032007-03-19 18:40:50 +0000610
611 // Insert it in the forward refs map.
612 CurFun.BBForwardRefs[ID] = BB;
613
Chris Lattner58af2a12006-02-15 07:22:58 +0000614 return BB;
615}
616
617
618//===----------------------------------------------------------------------===//
619// Code to handle forward references in instructions
620//===----------------------------------------------------------------------===//
621//
622// This code handles the late binding needed with statements that reference
623// values not defined yet... for example, a forward branch, or the PHI node for
624// a loop body.
625//
626// This keeps a table (CurFun.LateResolveValues) of all such forward references
627// and back patchs after we are done.
628//
629
630// ResolveDefinitions - If we could not resolve some defs at parsing
631// time (forward branches, phi functions for loops, etc...) resolve the
632// defs now...
633//
634static void
Reid Spencer93c40032007-03-19 18:40:50 +0000635ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000636 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencer93c40032007-03-19 18:40:50 +0000637 while (!LateResolvers.empty()) {
638 Value *V = LateResolvers.back();
639 LateResolvers.pop_back();
Chris Lattner58af2a12006-02-15 07:22:58 +0000640
Reid Spencer93c40032007-03-19 18:40:50 +0000641 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
642 CurModule.PlaceHolderInfo.find(V);
643 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000644
Reid Spencer93c40032007-03-19 18:40:50 +0000645 ValID &DID = PHI->second.first;
Chris Lattner58af2a12006-02-15 07:22:58 +0000646
Reid Spencer93c40032007-03-19 18:40:50 +0000647 Value *TheRealValue = getExistingVal(V->getType(), DID);
648 if (TriggerError)
649 return;
650 if (TheRealValue) {
651 V->replaceAllUsesWith(TheRealValue);
652 delete V;
653 CurModule.PlaceHolderInfo.erase(PHI);
654 } else if (FutureLateResolvers) {
655 // Functions have their unresolved items forwarded to the module late
656 // resolver table
657 InsertValue(V, *FutureLateResolvers);
658 } else {
659 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
660 GenerateError("Reference to an invalid definition: '" +DID.getName()+
661 "' of type '" + V->getType()->getDescription() + "'",
662 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000663 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000664 } else {
Reid Spencer93c40032007-03-19 18:40:50 +0000665 GenerateError("Reference to an invalid definition: #" +
666 itostr(DID.Num) + " of type '" +
667 V->getType()->getDescription() + "'",
668 PHI->second.second);
669 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000670 }
671 }
672 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000673 LateResolvers.clear();
674}
675
676// ResolveTypeTo - A brand new type was just declared. This means that (if
677// name is not null) things referencing Name can be resolved. Otherwise, things
678// refering to the number can be resolved. Do this now.
679//
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000680static void ResolveTypeTo(std::string *Name, const Type *ToTy) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000681 ValID D;
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000682 if (Name)
683 D = ValID::createLocalName(*Name);
684 else
685 D = ValID::createLocalID(CurModule.Types.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000686
Reid Spencer861d9d62006-11-28 07:29:44 +0000687 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattner58af2a12006-02-15 07:22:58 +0000688 CurModule.LateResolveTypes.find(D);
689 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencer861d9d62006-11-28 07:29:44 +0000690 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner58af2a12006-02-15 07:22:58 +0000691 CurModule.LateResolveTypes.erase(I);
692 }
693}
694
695// setValueName - Set the specified value to the name given. The name may be
696// null potentially, in which case this is a noop. The string passed in is
697// assumed to be a malloc'd string buffer, and is free'd by this function.
698//
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000699static void setValueName(Value *V, std::string *NameStr) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000700 if (!NameStr) return;
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000701 std::string Name(*NameStr); // Copy string
702 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000703
Reid Spencer41dff5e2007-01-26 08:05:27 +0000704 if (V->getType() == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000705 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000706 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000707 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000708
Reid Spencera9720f52007-02-05 17:04:00 +0000709 assert(inFunctionScope() && "Must be in function scope!");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000710 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
711 if (ST.lookup(Name)) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000712 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000713 V->getType()->getDescription() + "'");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000714 return;
715 }
716
717 // Set the name.
718 V->setName(Name);
Chris Lattner58af2a12006-02-15 07:22:58 +0000719}
720
721/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
722/// this is a declaration, otherwise it is a definition.
723static GlobalVariable *
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000724ParseGlobalVariable(std::string *NameStr,
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000725 GlobalValue::LinkageTypes Linkage,
726 GlobalValue::VisibilityTypes Visibility,
Chris Lattner58af2a12006-02-15 07:22:58 +0000727 bool isConstantGlobal, const Type *Ty,
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000728 Constant *Initializer, bool IsThreadLocal,
729 unsigned AddressSpace = 0) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000730 if (isa<FunctionType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000731 GenerateError("Cannot declare global vars of function type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000732 return 0;
733 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000734 if (Ty == Type::LabelTy) {
735 GenerateError("Cannot declare global vars of label type");
736 return 0;
737 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000738
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000739 const PointerType *PTy = PointerType::get(Ty, AddressSpace);
Chris Lattner58af2a12006-02-15 07:22:58 +0000740
741 std::string Name;
742 if (NameStr) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000743 Name = *NameStr; // Copy string
744 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000745 }
746
747 // See if this global value was forward referenced. If so, recycle the
748 // object.
749 ValID ID;
750 if (!Name.empty()) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000751 ID = ValID::createGlobalName(Name);
Chris Lattner58af2a12006-02-15 07:22:58 +0000752 } else {
Reid Spencer93c40032007-03-19 18:40:50 +0000753 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000754 }
755
756 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
757 // Move the global to the end of the list, from whereever it was
758 // previously inserted.
759 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
760 CurModule.CurrentModule->getGlobalList().remove(GV);
761 CurModule.CurrentModule->getGlobalList().push_back(GV);
762 GV->setInitializer(Initializer);
763 GV->setLinkage(Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000764 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000765 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000766 GV->setThreadLocal(IsThreadLocal);
Chris Lattner58af2a12006-02-15 07:22:58 +0000767 InsertValue(GV, CurModule.Values);
768 return GV;
769 }
770
Reid Spenceref9b9a72007-02-05 20:47:22 +0000771 // If this global has a name
Chris Lattner58af2a12006-02-15 07:22:58 +0000772 if (!Name.empty()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000773 // if the global we're parsing has an initializer (is a definition) and
774 // has external linkage.
775 if (Initializer && Linkage != GlobalValue::InternalLinkage)
776 // If there is already a global with external linkage with this name
777 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
778 // If we allow this GVar to get created, it will be renamed in the
779 // symbol table because it conflicts with an existing GVar. We can't
780 // allow redefinition of GVars whose linking indicates that their name
781 // must stay the same. Issue the error.
782 GenerateError("Redefinition of global variable named '" + Name +
783 "' of type '" + Ty->getDescription() + "'");
784 return 0;
785 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000786 }
787
788 // Otherwise there is no existing GV to use, create one now.
789 GlobalVariable *GV =
790 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000791 CurModule.CurrentModule, IsThreadLocal, AddressSpace);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000792 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000793 InsertValue(GV, CurModule.Values);
794 return GV;
795}
796
797// setTypeName - Set the specified type to the name given. The name may be
798// null potentially, in which case this is a noop. The string passed in is
799// assumed to be a malloc'd string buffer, and is freed by this function.
800//
801// This function returns true if the type has already been defined, but is
802// allowed to be redefined in the specified context. If the name is a new name
803// for the type plane, it is inserted and false is returned.
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000804static bool setTypeName(const Type *T, std::string *NameStr) {
Reid Spencera9720f52007-02-05 17:04:00 +0000805 assert(!inFunctionScope() && "Can't give types function-local names!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000806 if (NameStr == 0) return false;
807
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000808 std::string Name(*NameStr); // Copy string
809 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000810
811 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000812 if (T == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000813 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000814 return false;
815 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000816
817 // Set the type name, checking for conflicts as we do so.
818 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
819
820 if (AlreadyExists) { // Inserting a name that is already defined???
821 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencera9720f52007-02-05 17:04:00 +0000822 assert(Existing && "Conflict but no matching type?!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000823
824 // There is only one case where this is allowed: when we are refining an
825 // opaque type. In this case, Existing will be an opaque type.
826 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
827 // We ARE replacing an opaque type!
828 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
829 return true;
830 }
831
832 // Otherwise, this is an attempt to redefine a type. That's okay if
833 // the redefinition is identical to the original. This will be so if
834 // Existing and T point to the same Type object. In this one case we
835 // allow the equivalent redefinition.
836 if (Existing == T) return true; // Yes, it's equal.
837
838 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer63c34452007-01-05 21:51:07 +0000839 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000840 T->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +0000841 }
842
843 return false;
844}
845
846//===----------------------------------------------------------------------===//
847// Code for handling upreferences in type names...
848//
849
850// TypeContains - Returns true if Ty directly contains E in it.
851//
852static bool TypeContains(const Type *Ty, const Type *E) {
853 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
854 E) != Ty->subtype_end();
855}
856
857namespace {
858 struct UpRefRecord {
859 // NestingLevel - The number of nesting levels that need to be popped before
860 // this type is resolved.
861 unsigned NestingLevel;
862
863 // LastContainedTy - This is the type at the current binding level for the
864 // type. Every time we reduce the nesting level, this gets updated.
865 const Type *LastContainedTy;
866
867 // UpRefTy - This is the actual opaque type that the upreference is
868 // represented with.
869 OpaqueType *UpRefTy;
870
871 UpRefRecord(unsigned NL, OpaqueType *URTy)
872 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
873 };
874}
875
876// UpRefs - A list of the outstanding upreferences that need to be resolved.
877static std::vector<UpRefRecord> UpRefs;
878
879/// HandleUpRefs - Every time we finish a new layer of types, this function is
880/// called. It loops through the UpRefs vector, which is a list of the
881/// currently active types. For each type, if the up reference is contained in
882/// the newly completed type, we decrement the level count. When the level
883/// count reaches zero, the upreferenced type is the type that is passed in:
884/// thus we can complete the cycle.
885///
886static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner224f84f2006-08-18 17:34:45 +0000887 // If Ty isn't abstract, or if there are no up-references in it, then there is
888 // nothing to resolve here.
889 if (!ty->isAbstract() || UpRefs.empty()) return ty;
890
Chris Lattner58af2a12006-02-15 07:22:58 +0000891 PATypeHolder Ty(ty);
892 UR_OUT("Type '" << Ty->getDescription() <<
893 "' newly formed. Resolving upreferences.\n" <<
894 UpRefs.size() << " upreferences active!\n");
895
896 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
897 // to zero), we resolve them all together before we resolve them to Ty. At
898 // the end of the loop, if there is anything to resolve to Ty, it will be in
899 // this variable.
900 OpaqueType *TypeToResolve = 0;
901
902 for (unsigned i = 0; i != UpRefs.size(); ++i) {
903 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
904 << UpRefs[i].second->getDescription() << ") = "
905 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
906 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
907 // Decrement level of upreference
908 unsigned Level = --UpRefs[i].NestingLevel;
909 UpRefs[i].LastContainedTy = Ty;
910 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
911 if (Level == 0) { // Upreference should be resolved!
912 if (!TypeToResolve) {
913 TypeToResolve = UpRefs[i].UpRefTy;
914 } else {
915 UR_OUT(" * Resolving upreference for "
916 << UpRefs[i].second->getDescription() << "\n";
917 std::string OldName = UpRefs[i].UpRefTy->getDescription());
918 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
919 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
920 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
921 }
922 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
923 --i; // Do not skip the next element...
924 }
925 }
926 }
927
928 if (TypeToResolve) {
929 UR_OUT(" * Resolving upreference for "
930 << UpRefs[i].second->getDescription() << "\n";
931 std::string OldName = TypeToResolve->getDescription());
932 TypeToResolve->refineAbstractTypeTo(Ty);
933 }
934
935 return Ty;
936}
937
Chris Lattner58af2a12006-02-15 07:22:58 +0000938//===----------------------------------------------------------------------===//
939// RunVMAsmParser - Define an interface to this parser
940//===----------------------------------------------------------------------===//
941//
Reid Spencer14310612006-12-31 05:40:51 +0000942static Module* RunParser(Module * M);
943
Duncan Sandsdc024672007-11-27 13:23:08 +0000944Module *llvm::RunVMAsmParser(llvm::MemoryBuffer *MB) {
945 InitLLLexer(MB);
946 Module *M = RunParser(new Module(LLLgetFilename()));
947 FreeLexer();
948 return M;
Chris Lattner58af2a12006-02-15 07:22:58 +0000949}
950
951%}
952
953%union {
954 llvm::Module *ModuleVal;
955 llvm::Function *FunctionVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000956 llvm::BasicBlock *BasicBlockVal;
957 llvm::TerminatorInst *TermInstVal;
958 llvm::Instruction *InstVal;
Reid Spencera132e042006-12-03 05:46:11 +0000959 llvm::Constant *ConstVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000960
Reid Spencera132e042006-12-03 05:46:11 +0000961 const llvm::Type *PrimType;
Reid Spencer14310612006-12-31 05:40:51 +0000962 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencera132e042006-12-03 05:46:11 +0000963 llvm::PATypeHolder *TypeVal;
964 llvm::Value *ValueVal;
Reid Spencera132e042006-12-03 05:46:11 +0000965 std::vector<llvm::Value*> *ValueList;
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000966 std::vector<unsigned> *ConstantList;
Reid Spencer14310612006-12-31 05:40:51 +0000967 llvm::ArgListType *ArgList;
968 llvm::TypeWithAttrs TypeWithAttrs;
969 llvm::TypeWithAttrsList *TypeWithAttrsList;
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000970 llvm::ParamList *ParamList;
Reid Spencer14310612006-12-31 05:40:51 +0000971
Chris Lattner58af2a12006-02-15 07:22:58 +0000972 // Represent the RHS of PHI node
Reid Spencera132e042006-12-03 05:46:11 +0000973 std::list<std::pair<llvm::Value*,
974 llvm::BasicBlock*> > *PHIList;
Chris Lattner58af2a12006-02-15 07:22:58 +0000975 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencera132e042006-12-03 05:46:11 +0000976 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner58af2a12006-02-15 07:22:58 +0000977
978 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000979 llvm::GlobalValue::VisibilityTypes Visibility;
Dale Johannesen222ebf72008-02-19 21:40:51 +0000980 llvm::ParameterAttributes ParamAttrs;
Reid Spencer38c91a92007-02-28 02:24:54 +0000981 llvm::APInt *APIntVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000982 int64_t SInt64Val;
983 uint64_t UInt64Val;
984 int SIntVal;
985 unsigned UIntVal;
Dale Johannesen43421b32007-09-06 18:13:44 +0000986 llvm::APFloat *FPVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000987 bool BoolVal;
988
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000989 std::string *StrVal; // This memory must be deleted
990 llvm::ValID ValIDVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000991
Reid Spencera132e042006-12-03 05:46:11 +0000992 llvm::Instruction::BinaryOps BinaryOpVal;
993 llvm::Instruction::TermOps TermOpVal;
994 llvm::Instruction::MemoryOps MemOpVal;
995 llvm::Instruction::CastOps CastOpVal;
996 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencera132e042006-12-03 05:46:11 +0000997 llvm::ICmpInst::Predicate IPredicate;
998 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner58af2a12006-02-15 07:22:58 +0000999}
1000
Reid Spencer14310612006-12-31 05:40:51 +00001001%type <ModuleVal> Module
Chris Lattner58af2a12006-02-15 07:22:58 +00001002%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
1003%type <BasicBlockVal> BasicBlock InstructionList
1004%type <TermInstVal> BBTerminatorInst
1005%type <InstVal> Inst InstVal MemoryInst
Anton Korobeynikov38e09802007-04-28 13:48:45 +00001006%type <ConstVal> ConstVal ConstExpr AliaseeRef
Chris Lattner58af2a12006-02-15 07:22:58 +00001007%type <ConstVector> ConstVector
1008%type <ArgList> ArgList ArgListH
Chris Lattner58af2a12006-02-15 07:22:58 +00001009%type <PHIList> PHIList
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001010%type <ParamList> ParamList // For call param lists & GEP indices
Reid Spencer14310612006-12-31 05:40:51 +00001011%type <ValueList> IndexList // For GEP indices
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001012%type <ConstantList> ConstantIndexList // For insertvalue/extractvalue indices
Reid Spencer14310612006-12-31 05:40:51 +00001013%type <TypeList> TypeListI
1014%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer218ded22007-01-05 17:07:23 +00001015%type <TypeWithAttrs> ArgType
Chris Lattner58af2a12006-02-15 07:22:58 +00001016%type <JumpTable> JumpTable
1017%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001018%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattner58af2a12006-02-15 07:22:58 +00001019%type <BoolVal> OptVolatile // 'volatile' or not
1020%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1021%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencer14310612006-12-31 05:40:51 +00001022%type <Linkage> GVInternalLinkage GVExternalLinkage
1023%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001024%type <Linkage> AliasLinkage
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001025%type <Visibility> GVVisibilityStyle
Chris Lattner58af2a12006-02-15 07:22:58 +00001026
1027// ValueRef - Unresolved reference to a definition or BB
1028%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1029%type <ValueVal> ResolvedVal // <type> <valref> pair
Devang Patel7990dc72008-02-20 22:40:23 +00001030%type <ValueList> ReturnedVal
Chris Lattner58af2a12006-02-15 07:22:58 +00001031// Tokens and types for handling constant integer values
1032//
1033// ESINT64VAL - A negative number within long long range
1034%token <SInt64Val> ESINT64VAL
1035
1036// EUINT64VAL - A positive number within uns. long long range
1037%token <UInt64Val> EUINT64VAL
Chris Lattner58af2a12006-02-15 07:22:58 +00001038
Reid Spencer38c91a92007-02-28 02:24:54 +00001039// ESAPINTVAL - A negative number with arbitrary precision
1040%token <APIntVal> ESAPINTVAL
1041
1042// EUAPINTVAL - A positive number with arbitrary precision
1043%token <APIntVal> EUAPINTVAL
1044
Reid Spencer41dff5e2007-01-26 08:05:27 +00001045%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattner58af2a12006-02-15 07:22:58 +00001046%token <FPVal> FPVAL // Float or Double constant
1047
1048// Built in types...
Reid Spencer218ded22007-01-05 17:07:23 +00001049%type <TypeVal> Types ResultTypes
Reid Spencer14310612006-12-31 05:40:51 +00001050%type <PrimType> IntType FPType PrimType // Classifications
Reid Spencer6f407902007-01-13 05:00:46 +00001051%token <PrimType> VOID INTTYPE
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001052%token <PrimType> FLOAT DOUBLE X86_FP80 FP128 PPC_FP128 LABEL
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001053%token TYPE
Chris Lattner58af2a12006-02-15 07:22:58 +00001054
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001055
Reid Spencered951ea2007-05-19 07:22:10 +00001056%token<StrVal> LOCALVAR GLOBALVAR LABELSTR
1057%token<StrVal> STRINGCONSTANT ATSTRINGCONSTANT PCTSTRINGCONSTANT
Reid Spencer41dff5e2007-01-26 08:05:27 +00001058%type <StrVal> LocalName OptLocalName OptLocalAssign
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001059%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001060%type <StrVal> OptSection SectionString OptGC
Chris Lattner58af2a12006-02-15 07:22:58 +00001061
Christopher Lambbf3348d2007-12-12 08:45:45 +00001062%type <UIntVal> OptAlign OptCAlign OptAddrSpace
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001063
Reid Spencer3d6b71e2007-04-09 01:56:05 +00001064%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001065%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
Reid Spencer14310612006-12-31 05:40:51 +00001066%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Dale Johannesenc7071cc2008-05-14 20:13:36 +00001067%token DLLIMPORT DLLEXPORT EXTERN_WEAK COMMON
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001068%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN ADDRSPACE
Chris Lattner58af2a12006-02-15 07:22:58 +00001069%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001070%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Nick Lewycky280a6e62008-04-25 16:53:59 +00001071%token DATALAYOUT
Chris Lattner58af2a12006-02-15 07:22:58 +00001072%type <UIntVal> OptCallingConv
Reid Spencer218ded22007-01-05 17:07:23 +00001073%type <ParamAttrs> OptParamAttrs ParamAttr
1074%type <ParamAttrs> OptFuncAttrs FuncAttr
Chris Lattner58af2a12006-02-15 07:22:58 +00001075
1076// Basic Block Terminating Operators
1077%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
1078
1079// Binary Operators
Reid Spencere4d87aa2006-12-23 06:05:41 +00001080%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencer3ed469c2006-11-02 20:25:50 +00001081%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer832254e2007-02-02 02:16:23 +00001082%token <BinaryOpVal> SHL LSHR ASHR
1083
Nate Begemanac80ade2008-05-12 19:01:56 +00001084%token <OtherOpVal> ICMP FCMP VICMP VFCMP
Reid Spencera132e042006-12-03 05:46:11 +00001085%type <IPredicate> IPredicates
Reid Spencera132e042006-12-03 05:46:11 +00001086%type <FPredicate> FPredicates
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001087%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1088%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner58af2a12006-02-15 07:22:58 +00001089
1090// Memory Instructions
1091%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1092
Reid Spencer3da59db2006-11-27 01:05:10 +00001093// Cast Operators
1094%type <CastOpVal> CastOps
1095%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1096%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1097
Chris Lattner58af2a12006-02-15 07:22:58 +00001098// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001099%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattnerd5efe842006-04-08 01:18:56 +00001100%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Devang Patel5a970972008-02-19 22:27:01 +00001101%token <OtherOpVal> GETRESULT
Dan Gohmane4977cf2008-05-23 01:55:30 +00001102%token <OtherOpVal> EXTRACTVALUE INSERTVALUE
Chris Lattner58af2a12006-02-15 07:22:58 +00001103
Reid Spencer218ded22007-01-05 17:07:23 +00001104// Function Attributes
Reid Spencerb8f85052007-07-31 03:50:36 +00001105%token SIGNEXT ZEROEXT NORETURN INREG SRET NOUNWIND NOALIAS BYVAL NEST
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001106%token READNONE READONLY GC
Chris Lattner58af2a12006-02-15 07:22:58 +00001107
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001108// Visibility Styles
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001109%token DEFAULT HIDDEN PROTECTED
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001110
Chris Lattner58af2a12006-02-15 07:22:58 +00001111%start Module
1112%%
1113
Chris Lattner58af2a12006-02-15 07:22:58 +00001114
Chris Lattner58af2a12006-02-15 07:22:58 +00001115// Operations that are notably excluded from this list include:
1116// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1117//
Reid Spencer3ed469c2006-11-02 20:25:50 +00001118ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer832254e2007-02-02 02:16:23 +00001119LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Reid Spencer3da59db2006-11-27 01:05:10 +00001120CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1121 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer832254e2007-02-02 02:16:23 +00001122
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001123IPredicates
Reid Spencer4012e832006-12-04 05:24:24 +00001124 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001125 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1126 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1127 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1128 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1129 ;
1130
1131FPredicates
1132 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1133 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1134 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1135 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1136 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1137 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1138 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1139 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1140 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1141 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001142
1143// These are some types that allow classification if we only want a particular
1144// thing... for example, only a signed, unsigned, or integral type.
Reid Spencera54b7cb2007-01-12 07:05:14 +00001145IntType : INTTYPE;
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001146FPType : FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80;
Chris Lattner58af2a12006-02-15 07:22:58 +00001147
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001148LocalName : LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001149OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1150
Christopher Lambbf3348d2007-12-12 08:45:45 +00001151OptAddrSpace : ADDRSPACE '(' EUINT64VAL ')' { $$=$3; }
1152 | /*empty*/ { $$=0; };
1153
Reid Spencer41dff5e2007-01-26 08:05:27 +00001154/// OptLocalAssign - Value producing statements have an optional assignment
1155/// component.
1156OptLocalAssign : LocalName '=' {
1157 $$ = $1;
1158 CHECK_FOR_ERROR
1159 }
1160 | /*empty*/ {
1161 $$ = 0;
1162 CHECK_FOR_ERROR
1163 };
1164
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001165GlobalName : GLOBALVAR | ATSTRINGCONSTANT ;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001166
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001167OptGlobalAssign : GlobalAssign
Chris Lattner58af2a12006-02-15 07:22:58 +00001168 | /*empty*/ {
1169 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001170 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001171 };
1172
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001173GlobalAssign : GlobalName '=' {
1174 $$ = $1;
1175 CHECK_FOR_ERROR
Chris Lattner6cdc6822007-04-26 05:31:05 +00001176 };
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001177
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001178GVInternalLinkage
1179 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1180 | WEAK { $$ = GlobalValue::WeakLinkage; }
1181 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1182 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1183 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Dale Johannesenc7071cc2008-05-14 20:13:36 +00001184 | COMMON { $$ = GlobalValue::CommonLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001185 ;
1186
1187GVExternalLinkage
1188 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1189 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1190 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1191 ;
1192
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001193GVVisibilityStyle
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001194 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1195 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1196 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1197 | PROTECTED { $$ = GlobalValue::ProtectedVisibility; }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001198 ;
1199
Reid Spencer14310612006-12-31 05:40:51 +00001200FunctionDeclareLinkage
1201 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1202 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1203 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001204 ;
1205
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001206FunctionDefineLinkage
Reid Spencer14310612006-12-31 05:40:51 +00001207 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1208 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001209 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1210 | WEAK { $$ = GlobalValue::WeakLinkage; }
1211 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001212 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001213
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001214AliasLinkage
1215 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1216 | WEAK { $$ = GlobalValue::WeakLinkage; }
1217 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1218 ;
1219
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001220OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1221 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001222 FASTCC_TOK { $$ = CallingConv::Fast; } |
1223 COLDCC_TOK { $$ = CallingConv::Cold; } |
1224 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1225 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1226 CC_TOK EUINT64VAL {
Chris Lattner58af2a12006-02-15 07:22:58 +00001227 if ((unsigned)$2 != $2)
Reid Spencerb5334b02007-02-05 10:18:06 +00001228 GEN_ERROR("Calling conv too large");
Chris Lattner58af2a12006-02-15 07:22:58 +00001229 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001230 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001231 };
1232
Reid Spencerb8f85052007-07-31 03:50:36 +00001233ParamAttr : ZEROEXT { $$ = ParamAttr::ZExt; }
1234 | ZEXT { $$ = ParamAttr::ZExt; }
1235 | SIGNEXT { $$ = ParamAttr::SExt; }
Chris Lattnerce5f24e2007-07-05 17:26:49 +00001236 | SEXT { $$ = ParamAttr::SExt; }
1237 | INREG { $$ = ParamAttr::InReg; }
1238 | SRET { $$ = ParamAttr::StructRet; }
1239 | NOALIAS { $$ = ParamAttr::NoAlias; }
Reid Spencerb8f85052007-07-31 03:50:36 +00001240 | BYVAL { $$ = ParamAttr::ByVal; }
1241 | NEST { $$ = ParamAttr::Nest; }
Dale Johannesendc6c0f12008-02-22 17:50:51 +00001242 | ALIGN EUINT64VAL { $$ =
1243 ParamAttr::constructAlignmentFromInt($2); }
Reid Spencer14310612006-12-31 05:40:51 +00001244 ;
1245
Reid Spencer18da0722007-04-11 02:44:20 +00001246OptParamAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer218ded22007-01-05 17:07:23 +00001247 | OptParamAttrs ParamAttr {
Reid Spencer7b5d4662007-04-09 06:16:21 +00001248 $$ = $1 | $2;
Reid Spencer14310612006-12-31 05:40:51 +00001249 }
1250 ;
1251
Reid Spencer18da0722007-04-11 02:44:20 +00001252FuncAttr : NORETURN { $$ = ParamAttr::NoReturn; }
1253 | NOUNWIND { $$ = ParamAttr::NoUnwind; }
Reid Spencerb8f85052007-07-31 03:50:36 +00001254 | ZEROEXT { $$ = ParamAttr::ZExt; }
1255 | SIGNEXT { $$ = ParamAttr::SExt; }
Duncan Sandsdc024672007-11-27 13:23:08 +00001256 | READNONE { $$ = ParamAttr::ReadNone; }
1257 | READONLY { $$ = ParamAttr::ReadOnly; }
Reid Spencer218ded22007-01-05 17:07:23 +00001258 ;
1259
Reid Spencer18da0722007-04-11 02:44:20 +00001260OptFuncAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer218ded22007-01-05 17:07:23 +00001261 | OptFuncAttrs FuncAttr {
Reid Spencer7b5d4662007-04-09 06:16:21 +00001262 $$ = $1 | $2;
Reid Spencer218ded22007-01-05 17:07:23 +00001263 }
Reid Spencer14310612006-12-31 05:40:51 +00001264 ;
1265
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001266OptGC : /* empty */ { $$ = 0; }
1267 | GC STRINGCONSTANT {
1268 $$ = $2;
1269 }
1270 ;
1271
Chris Lattner58af2a12006-02-15 07:22:58 +00001272// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1273// a comma before it.
1274OptAlign : /*empty*/ { $$ = 0; } |
1275 ALIGN EUINT64VAL {
1276 $$ = $2;
1277 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001278 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001279 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001280};
1281OptCAlign : /*empty*/ { $$ = 0; } |
1282 ',' ALIGN EUINT64VAL {
1283 $$ = $3;
1284 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001285 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001286 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001287};
1288
1289
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001290
Chris Lattner58af2a12006-02-15 07:22:58 +00001291SectionString : SECTION STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001292 for (unsigned i = 0, e = $2->length(); i != e; ++i)
1293 if ((*$2)[i] == '"' || (*$2)[i] == '\\')
Reid Spencerb5334b02007-02-05 10:18:06 +00001294 GEN_ERROR("Invalid character in section name");
Chris Lattner58af2a12006-02-15 07:22:58 +00001295 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001296 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001297};
1298
1299OptSection : /*empty*/ { $$ = 0; } |
1300 SectionString { $$ = $1; };
1301
1302// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1303// is set to be the global we are processing.
1304//
1305GlobalVarAttributes : /* empty */ {} |
1306 ',' GlobalVarAttribute GlobalVarAttributes {};
1307GlobalVarAttribute : SectionString {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001308 CurGV->setSection(*$1);
1309 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001310 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001311 }
1312 | ALIGN EUINT64VAL {
1313 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001314 GEN_ERROR("Alignment must be a power of two");
Chris Lattner58af2a12006-02-15 07:22:58 +00001315 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001316 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001317 };
1318
1319//===----------------------------------------------------------------------===//
1320// Types includes all predefined types... except void, because it can only be
Reid Spencer14310612006-12-31 05:40:51 +00001321// used in specific contexts (function returning void for example).
Chris Lattner58af2a12006-02-15 07:22:58 +00001322
1323// Derived types are added later...
1324//
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001325PrimType : INTTYPE | FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80 | LABEL ;
Reid Spencer14310612006-12-31 05:40:51 +00001326
1327Types
1328 : OPAQUE {
Reid Spencera132e042006-12-03 05:46:11 +00001329 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001330 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001331 }
1332 | PrimType {
Reid Spencera132e042006-12-03 05:46:11 +00001333 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001334 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00001335 }
Christopher Lambbf3348d2007-12-12 08:45:45 +00001336 | Types OptAddrSpace '*' { // Pointer type?
Reid Spencer14310612006-12-31 05:40:51 +00001337 if (*$1 == Type::LabelTy)
1338 GEN_ERROR("Cannot form a pointer to a basic block");
Christopher Lambbf3348d2007-12-12 08:45:45 +00001339 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1, $2)));
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001340 delete $1;
1341 CHECK_FOR_ERROR
1342 }
Reid Spencer14310612006-12-31 05:40:51 +00001343 | SymbolicValueRef { // Named types are also simple types...
1344 const Type* tmp = getTypeVal($1);
1345 CHECK_FOR_ERROR
1346 $$ = new PATypeHolder(tmp);
1347 }
1348 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerb5334b02007-02-05 10:18:06 +00001349 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattner58af2a12006-02-15 07:22:58 +00001350 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1351 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencera132e042006-12-03 05:46:11 +00001352 $$ = new PATypeHolder(OT);
Chris Lattner58af2a12006-02-15 07:22:58 +00001353 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001354 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001355 }
Reid Spencer218ded22007-01-05 17:07:23 +00001356 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001357 // Allow but ignore attributes on function types; this permits auto-upgrade.
1358 // FIXME: remove in LLVM 3.0.
Chris Lattnera925a142008-04-23 05:37:08 +00001359 const Type *RetTy = *$1;
1360 if (!FunctionType::isValidReturnType(RetTy))
1361 GEN_ERROR("Invalid result type for LLVM function");
1362
Chris Lattner58af2a12006-02-15 07:22:58 +00001363 std::vector<const Type*> Params;
Reid Spencer7b5d4662007-04-09 06:16:21 +00001364 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001365 for (; I != E; ++I ) {
Reid Spencer66728ef2007-03-20 01:13:36 +00001366 const Type *Ty = I->Ty->get();
Reid Spencer66728ef2007-03-20 01:13:36 +00001367 Params.push_back(Ty);
Reid Spencer14310612006-12-31 05:40:51 +00001368 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001369
Chris Lattner58af2a12006-02-15 07:22:58 +00001370 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1371 if (isVarArg) Params.pop_back();
1372
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001373 for (unsigned i = 0; i != Params.size(); ++i)
1374 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1375 GEN_ERROR("Function arguments must be value types!");
1376
1377 CHECK_FOR_ERROR
1378
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001379 FunctionType *FT = FunctionType::get(RetTy, Params, isVarArg);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001380 delete $3; // Delete the argument list
Reid Spencer14310612006-12-31 05:40:51 +00001381 delete $1; // Delete the return type handle
1382 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer61c83e02006-08-18 08:43:06 +00001383 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001384 }
Reid Spencer218ded22007-01-05 17:07:23 +00001385 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001386 // Allow but ignore attributes on function types; this permits auto-upgrade.
1387 // FIXME: remove in LLVM 3.0.
Reid Spencer14310612006-12-31 05:40:51 +00001388 std::vector<const Type*> Params;
Reid Spencer7b5d4662007-04-09 06:16:21 +00001389 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001390 for ( ; I != E; ++I ) {
Reid Spencer66728ef2007-03-20 01:13:36 +00001391 const Type* Ty = I->Ty->get();
Reid Spencer66728ef2007-03-20 01:13:36 +00001392 Params.push_back(Ty);
Reid Spencer14310612006-12-31 05:40:51 +00001393 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001394
Reid Spencer14310612006-12-31 05:40:51 +00001395 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1396 if (isVarArg) Params.pop_back();
1397
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001398 for (unsigned i = 0; i != Params.size(); ++i)
1399 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1400 GEN_ERROR("Function arguments must be value types!");
1401
1402 CHECK_FOR_ERROR
1403
Duncan Sandsdc024672007-11-27 13:23:08 +00001404 FunctionType *FT = FunctionType::get($1, Params, isVarArg);
Reid Spencer218ded22007-01-05 17:07:23 +00001405 delete $3; // Delete the argument list
Reid Spencer14310612006-12-31 05:40:51 +00001406 $$ = new PATypeHolder(HandleUpRefs(FT));
1407 CHECK_FOR_ERROR
1408 }
1409
1410 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001411 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, $2)));
Reid Spencera132e042006-12-03 05:46:11 +00001412 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001413 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001414 }
Chris Lattner32980692007-02-19 07:44:24 +00001415 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencera132e042006-12-03 05:46:11 +00001416 const llvm::Type* ElemTy = $4->get();
1417 if ((unsigned)$2 != $2)
1418 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner42a75512007-01-15 02:27:26 +00001419 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencer9d6565a2007-02-15 02:26:10 +00001420 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001421 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencera132e042006-12-03 05:46:11 +00001422 delete $4;
1423 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001424 }
1425 | '{' TypeListI '}' { // Structure type?
1426 std::vector<const Type*> Elements;
Reid Spencera132e042006-12-03 05:46:11 +00001427 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattner58af2a12006-02-15 07:22:58 +00001428 E = $2->end(); I != E; ++I)
Reid Spencera132e042006-12-03 05:46:11 +00001429 Elements.push_back(*I);
Chris Lattner58af2a12006-02-15 07:22:58 +00001430
Reid Spencera132e042006-12-03 05:46:11 +00001431 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattner58af2a12006-02-15 07:22:58 +00001432 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001433 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001434 }
1435 | '{' '}' { // Empty structure type?
Reid Spencera132e042006-12-03 05:46:11 +00001436 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001437 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001438 }
Andrew Lenharth6353e052006-12-08 18:07:09 +00001439 | '<' '{' TypeListI '}' '>' {
1440 std::vector<const Type*> Elements;
1441 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1442 E = $3->end(); I != E; ++I)
1443 Elements.push_back(*I);
1444
1445 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1446 delete $3;
1447 CHECK_FOR_ERROR
1448 }
1449 | '<' '{' '}' '>' { // Empty structure type?
1450 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1451 CHECK_FOR_ERROR
1452 }
Reid Spencer14310612006-12-31 05:40:51 +00001453 ;
1454
1455ArgType
Duncan Sandsdc024672007-11-27 13:23:08 +00001456 : Types OptParamAttrs {
1457 // Allow but ignore attributes on function types; this permits auto-upgrade.
1458 // FIXME: remove in LLVM 3.0.
Reid Spencer14310612006-12-31 05:40:51 +00001459 $$.Ty = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00001460 $$.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00001461 }
1462 ;
1463
Reid Spencer218ded22007-01-05 17:07:23 +00001464ResultTypes
1465 : Types {
Reid Spencer14310612006-12-31 05:40:51 +00001466 if (!UpRefs.empty())
1467 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Devang Patel20071732008-02-23 01:17:37 +00001468 if (!(*$1)->isFirstClassType() && !isa<StructType>($1->get()))
Reid Spencerb5334b02007-02-05 10:18:06 +00001469 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer218ded22007-01-05 17:07:23 +00001470 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00001471 }
Reid Spencer218ded22007-01-05 17:07:23 +00001472 | VOID {
1473 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencer14310612006-12-31 05:40:51 +00001474 }
1475 ;
1476
1477ArgTypeList : ArgType {
1478 $$ = new TypeWithAttrsList();
1479 $$->push_back($1);
1480 CHECK_FOR_ERROR
1481 }
1482 | ArgTypeList ',' ArgType {
1483 ($$=$1)->push_back($3);
1484 CHECK_FOR_ERROR
1485 }
1486 ;
1487
1488ArgTypeListI
1489 : ArgTypeList
1490 | ArgTypeList ',' DOTDOTDOT {
1491 $$=$1;
Reid Spencer18da0722007-04-11 02:44:20 +00001492 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00001493 TWA.Ty = new PATypeHolder(Type::VoidTy);
1494 $$->push_back(TWA);
1495 CHECK_FOR_ERROR
1496 }
1497 | DOTDOTDOT {
1498 $$ = new TypeWithAttrsList;
Reid Spencer18da0722007-04-11 02:44:20 +00001499 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00001500 TWA.Ty = new PATypeHolder(Type::VoidTy);
1501 $$->push_back(TWA);
1502 CHECK_FOR_ERROR
1503 }
1504 | /*empty*/ {
1505 $$ = new TypeWithAttrsList();
Reid Spencer61c83e02006-08-18 08:43:06 +00001506 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001507 };
1508
1509// TypeList - Used for struct declarations and as a basis for function type
1510// declaration type lists
1511//
Reid Spencer14310612006-12-31 05:40:51 +00001512TypeListI : Types {
Reid Spencera132e042006-12-03 05:46:11 +00001513 $$ = new std::list<PATypeHolder>();
Reid Spencer66728ef2007-03-20 01:13:36 +00001514 $$->push_back(*$1);
1515 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001516 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001517 }
Reid Spencer14310612006-12-31 05:40:51 +00001518 | TypeListI ',' Types {
Reid Spencer66728ef2007-03-20 01:13:36 +00001519 ($$=$1)->push_back(*$3);
1520 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001521 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001522 };
1523
Chris Lattner58af2a12006-02-15 07:22:58 +00001524// ConstVal - The various declarations that go into the constant pool. This
1525// production is used ONLY to represent constants that show up AFTER a 'const',
1526// 'constant' or 'global' token at global scope. Constants that can be inlined
1527// into other expressions (such as integers and constexprs) are handled by the
1528// ResolvedVal, ValueRef and ConstValueRef productions.
1529//
1530ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001531 if (!UpRefs.empty())
1532 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001533 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001534 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001535 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001536 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001537 const Type *ETy = ATy->getElementType();
Dan Gohman180c1692008-06-23 18:43:26 +00001538 uint64_t NumElements = ATy->getNumElements();
Chris Lattner58af2a12006-02-15 07:22:58 +00001539
1540 // Verify that we have the correct size...
1541 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001542 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001543 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerb5334b02007-02-05 10:18:06 +00001544 itostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001545
1546 // Verify all elements are correct type!
1547 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001548 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001549 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001550 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001551 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001552 }
1553
Reid Spencera132e042006-12-03 05:46:11 +00001554 $$ = ConstantArray::get(ATy, *$3);
1555 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001556 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001557 }
1558 | Types '[' ']' {
Reid Spencer14310612006-12-31 05:40:51 +00001559 if (!UpRefs.empty())
1560 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001561 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001562 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001563 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001564 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001565
Dan Gohman180c1692008-06-23 18:43:26 +00001566 uint64_t NumElements = ATy->getNumElements();
Chris Lattner58af2a12006-02-15 07:22:58 +00001567 if (NumElements != -1 && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001568 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencerb5334b02007-02-05 10:18:06 +00001569 " arguments, but has size of " + itostr(NumElements) +"");
Reid Spencera132e042006-12-03 05:46:11 +00001570 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1571 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001572 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001573 }
1574 | Types 'c' STRINGCONSTANT {
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();
Chris Lattner58af2a12006-02-15 07:22:58 +00001583 const Type *ETy = ATy->getElementType();
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001584 if (NumElements != -1 && NumElements != int($3->length()))
Reid Spencer61c83e02006-08-18 08:43:06 +00001585 GEN_ERROR("Can't build string constant of size " +
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001586 itostr((int)($3->length())) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001587 " when array has size " + itostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001588 std::vector<Constant*> Vals;
Reid Spencer14310612006-12-31 05:40:51 +00001589 if (ETy == Type::Int8Ty) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001590 for (unsigned i = 0; i < $3->length(); ++i)
1591 Vals.push_back(ConstantInt::get(ETy, (*$3)[i]));
Chris Lattner58af2a12006-02-15 07:22:58 +00001592 } else {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001593 delete $3;
Reid Spencerb5334b02007-02-05 10:18:06 +00001594 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner58af2a12006-02-15 07:22:58 +00001595 }
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001596 delete $3;
Reid Spencera132e042006-12-03 05:46:11 +00001597 $$ = ConstantArray::get(ATy, Vals);
1598 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001599 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001600 }
1601 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001602 if (!UpRefs.empty())
1603 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00001604 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001605 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001606 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001607 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001608 const Type *ETy = PTy->getElementType();
Dan Gohman180c1692008-06-23 18:43:26 +00001609 unsigned NumElements = PTy->getNumElements();
Chris Lattner58af2a12006-02-15 07:22:58 +00001610
1611 // Verify that we have the correct size...
1612 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001613 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001614 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerb5334b02007-02-05 10:18:06 +00001615 itostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001616
1617 // Verify all elements are correct type!
1618 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001619 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001620 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001621 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001622 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001623 }
1624
Reid Spencer9d6565a2007-02-15 02:26:10 +00001625 $$ = ConstantVector::get(PTy, *$3);
Reid Spencera132e042006-12-03 05:46:11 +00001626 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001627 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001628 }
1629 | Types '{' ConstVector '}' {
Reid Spencera132e042006-12-03 05:46:11 +00001630 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001631 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001632 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001633 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001634
1635 if ($3->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001636 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001637
1638 // Check to ensure that constants are compatible with the type initializer!
1639 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencera132e042006-12-03 05:46:11 +00001640 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001641 GEN_ERROR("Expected type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001642 STy->getElementType(i)->getDescription() +
1643 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001644 " of structure initializer");
Chris Lattner58af2a12006-02-15 07:22:58 +00001645
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001646 // Check to ensure that Type is not packed
1647 if (STy->isPacked())
Chris Lattner6cdc6822007-04-26 05:31:05 +00001648 GEN_ERROR("Unpacked Initializer to vector type '" +
1649 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001650
Reid Spencera132e042006-12-03 05:46:11 +00001651 $$ = ConstantStruct::get(STy, *$3);
1652 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001653 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001654 }
1655 | Types '{' '}' {
Reid Spencer14310612006-12-31 05:40:51 +00001656 if (!UpRefs.empty())
1657 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001658 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001659 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001660 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001661 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001662
1663 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001664 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001665
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001666 // Check to ensure that Type is not packed
1667 if (STy->isPacked())
Chris Lattner6cdc6822007-04-26 05:31:05 +00001668 GEN_ERROR("Unpacked Initializer to vector type '" +
1669 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001670
1671 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1672 delete $1;
1673 CHECK_FOR_ERROR
1674 }
1675 | Types '<' '{' ConstVector '}' '>' {
1676 const StructType *STy = dyn_cast<StructType>($1->get());
1677 if (STy == 0)
1678 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001679 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001680
1681 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001682 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001683
1684 // Check to ensure that constants are compatible with the type initializer!
1685 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1686 if ((*$4)[i]->getType() != STy->getElementType(i))
1687 GEN_ERROR("Expected type '" +
1688 STy->getElementType(i)->getDescription() +
1689 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001690 " of structure initializer");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001691
1692 // Check to ensure that Type is packed
1693 if (!STy->isPacked())
Chris Lattner32980692007-02-19 07:44:24 +00001694 GEN_ERROR("Vector initializer to non-vector type '" +
1695 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001696
1697 $$ = ConstantStruct::get(STy, *$4);
1698 delete $1; delete $4;
1699 CHECK_FOR_ERROR
1700 }
1701 | Types '<' '{' '}' '>' {
1702 if (!UpRefs.empty())
1703 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1704 const StructType *STy = dyn_cast<StructType>($1->get());
1705 if (STy == 0)
1706 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001707 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001708
1709 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001710 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001711
1712 // Check to ensure that Type is packed
1713 if (!STy->isPacked())
Chris Lattner32980692007-02-19 07:44:24 +00001714 GEN_ERROR("Vector initializer to non-vector type '" +
1715 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001716
Reid Spencera132e042006-12-03 05:46:11 +00001717 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1718 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001719 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001720 }
1721 | Types NULL_TOK {
Reid Spencer14310612006-12-31 05:40:51 +00001722 if (!UpRefs.empty())
1723 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001724 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001725 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001726 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001727 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001728
Reid Spencera132e042006-12-03 05:46:11 +00001729 $$ = ConstantPointerNull::get(PTy);
1730 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001731 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001732 }
1733 | Types UNDEF {
Reid Spencer14310612006-12-31 05:40:51 +00001734 if (!UpRefs.empty())
1735 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001736 $$ = UndefValue::get($1->get());
1737 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001738 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001739 }
1740 | Types SymbolicValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00001741 if (!UpRefs.empty())
1742 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001743 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001744 if (Ty == 0)
Devang Patel5a970972008-02-19 22:27:01 +00001745 GEN_ERROR("Global const reference must be a pointer type " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001746
1747 // ConstExprs can exist in the body of a function, thus creating
1748 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencer93c40032007-03-19 18:40:50 +00001749 // the context of a function, getExistingVal will search the functions
Chris Lattner58af2a12006-02-15 07:22:58 +00001750 // symbol table instead of the module symbol table for the global symbol,
1751 // which throws things all off. To get around this, we just tell
Reid Spencer93c40032007-03-19 18:40:50 +00001752 // getExistingVal that we are at global scope here.
Chris Lattner58af2a12006-02-15 07:22:58 +00001753 //
1754 Function *SavedCurFn = CurFun.CurrentFunction;
1755 CurFun.CurrentFunction = 0;
1756
Reid Spencer93c40032007-03-19 18:40:50 +00001757 Value *V = getExistingVal(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001758 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001759
1760 CurFun.CurrentFunction = SavedCurFn;
1761
1762 // If this is an initializer for a constant pointer, which is referencing a
1763 // (currently) undefined variable, create a stub now that shall be replaced
1764 // in the future with the right type of variable.
1765 //
1766 if (V == 0) {
Reid Spencera9720f52007-02-05 17:04:00 +00001767 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001768 const PointerType *PT = cast<PointerType>(Ty);
1769
1770 // First check to see if the forward references value is already created!
1771 PerModuleInfo::GlobalRefsType::iterator I =
1772 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1773
1774 if (I != CurModule.GlobalRefs.end()) {
1775 V = I->second; // Placeholder already exists, use it...
1776 $2.destroy();
1777 } else {
1778 std::string Name;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001779 if ($2.Type == ValID::GlobalName)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001780 Name = $2.getName();
Reid Spencer41dff5e2007-01-26 08:05:27 +00001781 else if ($2.Type != ValID::GlobalID)
1782 GEN_ERROR("Invalid reference to global");
Chris Lattner58af2a12006-02-15 07:22:58 +00001783
1784 // Create the forward referenced global.
1785 GlobalValue *GV;
1786 if (const FunctionType *FTy =
1787 dyn_cast<FunctionType>(PT->getElementType())) {
Gabor Greife64d2482008-04-06 23:07:54 +00001788 GV = Function::Create(FTy, GlobalValue::ExternalWeakLinkage, Name,
1789 CurModule.CurrentModule);
Chris Lattner58af2a12006-02-15 07:22:58 +00001790 } else {
1791 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattner6cdc6822007-04-26 05:31:05 +00001792 GlobalValue::ExternalWeakLinkage, 0,
Chris Lattner58af2a12006-02-15 07:22:58 +00001793 Name, CurModule.CurrentModule);
1794 }
1795
1796 // Keep track of the fact that we have a forward ref to recycle it
1797 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1798 V = GV;
1799 }
1800 }
1801
Reid Spencera132e042006-12-03 05:46:11 +00001802 $$ = cast<GlobalValue>(V);
1803 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001804 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001805 }
1806 | Types ConstExpr {
Reid Spencer14310612006-12-31 05:40:51 +00001807 if (!UpRefs.empty())
1808 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001809 if ($1->get() != $2->getType())
Reid Spencere68853b2007-01-04 00:06:14 +00001810 GEN_ERROR("Mismatched types for constant expression: " +
1811 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001812 $$ = $2;
Reid Spencera132e042006-12-03 05:46:11 +00001813 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001814 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001815 }
1816 | Types ZEROINITIALIZER {
Reid Spencer14310612006-12-31 05:40:51 +00001817 if (!UpRefs.empty())
1818 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001819 const Type *Ty = $1->get();
Chris Lattner58af2a12006-02-15 07:22:58 +00001820 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerb5334b02007-02-05 10:18:06 +00001821 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencera132e042006-12-03 05:46:11 +00001822 $$ = Constant::getNullValue(Ty);
1823 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001824 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00001825 }
Reid Spencer14310612006-12-31 05:40:51 +00001826 | IntType ESINT64VAL { // integral constants
Reid Spencere4d87aa2006-12-23 06:05:41 +00001827 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001828 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencer49d273e2007-03-19 20:40:51 +00001829 $$ = ConstantInt::get($1, $2, true);
Reid Spencer38c91a92007-02-28 02:24:54 +00001830 CHECK_FOR_ERROR
1831 }
1832 | IntType ESAPINTVAL { // arbitrary precision integer constants
1833 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1834 if ($2->getBitWidth() > BitWidth) {
1835 GEN_ERROR("Constant value does not fit in type");
Reid Spencer10794272007-03-01 19:41:47 +00001836 }
1837 $2->sextOrTrunc(BitWidth);
1838 $$ = ConstantInt::get(*$2);
Reid Spencer38c91a92007-02-28 02:24:54 +00001839 delete $2;
Reid Spencere4d87aa2006-12-23 06:05:41 +00001840 CHECK_FOR_ERROR
1841 }
Reid Spencer14310612006-12-31 05:40:51 +00001842 | IntType EUINT64VAL { // 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, false);
Reid Spencer38c91a92007-02-28 02:24:54 +00001846 CHECK_FOR_ERROR
1847 }
1848 | IntType EUAPINTVAL { // arbitrary precision integer constants
1849 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1850 if ($2->getBitWidth() > BitWidth) {
1851 GEN_ERROR("Constant value does not fit in type");
Reid Spencer10794272007-03-01 19:41:47 +00001852 }
1853 $2->zextOrTrunc(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 Spencer6f407902007-01-13 05:00:46 +00001858 | INTTYPE TRUETOK { // Boolean constants
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001859 if (cast<IntegerType>($1)->getBitWidth() != 1)
1860 GEN_ERROR("Constant true must have type i1");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001861 $$ = ConstantInt::getTrue();
Reid Spencer61c83e02006-08-18 08:43:06 +00001862 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001863 }
Reid Spencer6f407902007-01-13 05:00:46 +00001864 | INTTYPE FALSETOK { // Boolean constants
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001865 if (cast<IntegerType>($1)->getBitWidth() != 1)
1866 GEN_ERROR("Constant false must have type i1");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001867 $$ = ConstantInt::getFalse();
Reid Spencer61c83e02006-08-18 08:43:06 +00001868 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001869 }
Dale Johannesenea583102007-09-12 03:31:28 +00001870 | FPType FPVAL { // Floating point constants
Dale Johannesen43421b32007-09-06 18:13:44 +00001871 if (!ConstantFP::isValueValidForType($1, *$2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001872 GEN_ERROR("Floating point constant invalid for type");
Dale Johannesenc72cd7e2007-09-11 18:33:39 +00001873 // Lexer has no type info, so builds all float and double FP constants
1874 // as double. Fix this here. Long double is done right.
1875 if (&$2->getSemantics()==&APFloat::IEEEdouble && $1==Type::FloatTy)
Dale Johannesen43421b32007-09-06 18:13:44 +00001876 $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
Chris Lattnerd8eb63f2008-04-20 00:41:19 +00001877 $$ = ConstantFP::get(*$2);
Dale Johannesencdd509a2007-09-07 21:07:57 +00001878 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001879 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001880 };
1881
1882
Reid Spencer3da59db2006-11-27 01:05:10 +00001883ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer14310612006-12-31 05:40:51 +00001884 if (!UpRefs.empty())
1885 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001886 Constant *Val = $3;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001887 const Type *DestTy = $5->get();
1888 if (!CastInst::castIsValid($1, $3, DestTy))
1889 GEN_ERROR("invalid cast opcode for cast from '" +
1890 Val->getType()->getDescription() + "' to '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001891 DestTy->getDescription() + "'");
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001892 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00001893 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00001894 }
1895 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001896 if (!isa<PointerType>($3->getType()))
Reid Spencerb5334b02007-02-05 10:18:06 +00001897 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00001898
Reid Spencera132e042006-12-03 05:46:11 +00001899 const Type *IdxTy =
Dan Gohman041e2eb2008-05-15 19:50:34 +00001900 GetElementPtrInst::getIndexedType($3->getType(), $4->begin(), $4->end());
Reid Spencera132e042006-12-03 05:46:11 +00001901 if (!IdxTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00001902 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencera132e042006-12-03 05:46:11 +00001903
Chris Lattnerf7469af2007-01-31 04:44:08 +00001904 SmallVector<Constant*, 8> IdxVec;
Reid Spencera132e042006-12-03 05:46:11 +00001905 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1906 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattner58af2a12006-02-15 07:22:58 +00001907 IdxVec.push_back(C);
1908 else
Reid Spencerb5334b02007-02-05 10:18:06 +00001909 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattner58af2a12006-02-15 07:22:58 +00001910
1911 delete $4;
1912
Chris Lattnerf7469af2007-01-31 04:44:08 +00001913 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer61c83e02006-08-18 08:43:06 +00001914 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001915 }
1916 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer4fe16d62007-01-11 18:21:29 +00001917 if ($3->getType() != Type::Int1Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00001918 GEN_ERROR("Select condition must be of boolean type");
Reid Spencera132e042006-12-03 05:46:11 +00001919 if ($5->getType() != $7->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001920 GEN_ERROR("Select operand types must match");
Reid Spencera132e042006-12-03 05:46:11 +00001921 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001922 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001923 }
1924 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001925 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001926 GEN_ERROR("Binary operator types must match");
Reid Spencer1628cec2006-10-26 06:15:43 +00001927 CHECK_FOR_ERROR;
Reid Spencer9eef56f2006-12-05 19:16:11 +00001928 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner58af2a12006-02-15 07:22:58 +00001929 }
1930 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001931 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001932 GEN_ERROR("Logical operator types must match");
Chris Lattner42a75512007-01-15 02:27:26 +00001933 if (!$3->getType()->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001934 if (Instruction::isShift($1) || !isa<VectorType>($3->getType()) ||
1935 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00001936 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00001937 }
Reid Spencera132e042006-12-03 05:46:11 +00001938 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001939 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001940 }
Reid Spencer4012e832006-12-04 05:24:24 +00001941 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1942 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001943 GEN_ERROR("icmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00001944 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00001945 }
Reid Spencer4012e832006-12-04 05:24:24 +00001946 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1947 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001948 GEN_ERROR("fcmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00001949 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00001950 }
Nate Begemanac80ade2008-05-12 19:01:56 +00001951 | VICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1952 if ($4->getType() != $6->getType())
1953 GEN_ERROR("vicmp operand types must match");
1954 $$ = ConstantExpr::getVICmp($2, $4, $6);
1955 }
1956 | VFCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1957 if ($4->getType() != $6->getType())
1958 GEN_ERROR("vfcmp operand types must match");
1959 $$ = ConstantExpr::getVFCmp($2, $4, $6);
1960 }
Chris Lattner58af2a12006-02-15 07:22:58 +00001961 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001962 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerb5334b02007-02-05 10:18:06 +00001963 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00001964 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001965 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00001966 }
1967 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001968 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00001969 GEN_ERROR("Invalid insertelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00001970 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001971 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00001972 }
1973 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001974 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00001975 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00001976 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001977 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00001978 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001979 | EXTRACTVALUE '(' ConstVal ConstantIndexList ')' {
Dan Gohmane4977cf2008-05-23 01:55:30 +00001980 if (!isa<StructType>($3->getType()) && !isa<ArrayType>($3->getType()))
1981 GEN_ERROR("ExtractValue requires an aggregate operand");
1982
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001983 $$ = ConstantExpr::getExtractValue($3, &(*$4)[0], $4->size());
Dan Gohmane4977cf2008-05-23 01:55:30 +00001984 delete $4;
Dan Gohmane4977cf2008-05-23 01:55:30 +00001985 CHECK_FOR_ERROR
1986 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001987 | INSERTVALUE '(' ConstVal ',' ConstVal ConstantIndexList ')' {
Dan Gohmane4977cf2008-05-23 01:55:30 +00001988 if (!isa<StructType>($3->getType()) && !isa<ArrayType>($3->getType()))
1989 GEN_ERROR("InsertValue requires an aggregate operand");
1990
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001991 $$ = ConstantExpr::getInsertValue($3, $5, &(*$6)[0], $6->size());
Dan Gohmane4977cf2008-05-23 01:55:30 +00001992 delete $6;
Dan Gohmane4977cf2008-05-23 01:55:30 +00001993 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001994 };
1995
Chris Lattnerd25db202006-04-08 03:55:17 +00001996
Chris Lattner58af2a12006-02-15 07:22:58 +00001997// ConstVector - A list of comma separated constants.
1998ConstVector : ConstVector ',' ConstVal {
1999 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002000 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002001 }
2002 | ConstVal {
Reid Spencera132e042006-12-03 05:46:11 +00002003 $$ = new std::vector<Constant*>();
Chris Lattner58af2a12006-02-15 07:22:58 +00002004 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002005 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002006 };
2007
2008
2009// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
2010GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
2011
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002012// ThreadLocal
2013ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
2014
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002015// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
2016AliaseeRef : ResultTypes SymbolicValueRef {
2017 const Type* VTy = $1->get();
2018 Value *V = getVal(VTy, $2);
Chris Lattner0275cff2007-08-06 21:00:46 +00002019 CHECK_FOR_ERROR
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002020 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
2021 if (!Aliasee)
2022 GEN_ERROR("Aliases can be created only to global values");
2023
2024 $$ = Aliasee;
2025 CHECK_FOR_ERROR
2026 delete $1;
2027 }
2028 | BITCAST '(' AliaseeRef TO Types ')' {
2029 Constant *Val = $3;
2030 const Type *DestTy = $5->get();
2031 if (!CastInst::castIsValid($1, $3, DestTy))
2032 GEN_ERROR("invalid cast opcode for cast from '" +
2033 Val->getType()->getDescription() + "' to '" +
2034 DestTy->getDescription() + "'");
2035
2036 $$ = ConstantExpr::getCast($1, $3, DestTy);
2037 CHECK_FOR_ERROR
2038 delete $5;
2039 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002040
2041//===----------------------------------------------------------------------===//
2042// Rules to match Modules
2043//===----------------------------------------------------------------------===//
2044
2045// Module rule: Capture the result of parsing the whole file into a result
2046// variable...
2047//
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002048Module
2049 : DefinitionList {
2050 $$ = ParserResult = CurModule.CurrentModule;
2051 CurModule.ModuleDone();
2052 CHECK_FOR_ERROR;
2053 }
2054 | /*empty*/ {
2055 $$ = ParserResult = CurModule.CurrentModule;
2056 CurModule.ModuleDone();
2057 CHECK_FOR_ERROR;
2058 }
2059 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002060
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002061DefinitionList
2062 : Definition
2063 | DefinitionList Definition
2064 ;
2065
2066Definition
Jeff Cohen361c3ef2007-01-21 19:19:31 +00002067 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner58af2a12006-02-15 07:22:58 +00002068 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00002069 CHECK_FOR_ERROR
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002070 }
2071 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer61c83e02006-08-18 08:43:06 +00002072 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002073 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002074 | MODULE ASM_TOK AsmBlock {
Reid Spencer61c83e02006-08-18 08:43:06 +00002075 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002076 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002077 | OptLocalAssign TYPE Types {
Reid Spencer14310612006-12-31 05:40:51 +00002078 if (!UpRefs.empty())
2079 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002080 // Eagerly resolve types. This is not an optimization, this is a
2081 // requirement that is due to the fact that we could have this:
2082 //
2083 // %list = type { %list * }
2084 // %list = type { %list * } ; repeated type decl
2085 //
2086 // If types are not resolved eagerly, then the two types will not be
2087 // determined to be the same type!
2088 //
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002089 ResolveTypeTo($1, *$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002090
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002091 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002092 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002093 // If this is a named type that is not a redefinition, add it to the slot
2094 // table.
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002095 CurModule.Types.push_back(*$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002096 }
Reid Spencera132e042006-12-03 05:46:11 +00002097
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002098 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002099 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002100 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002101 | OptLocalAssign TYPE VOID {
Reid Spencer14310612006-12-31 05:40:51 +00002102 ResolveTypeTo($1, $3);
2103
2104 if (!setTypeName($3, $1) && !$1) {
2105 CHECK_FOR_ERROR
2106 // If this is a named type that is not a redefinition, add it to the slot
2107 // table.
2108 CurModule.Types.push_back($3);
2109 }
2110 CHECK_FOR_ERROR
2111 }
Christopher Lambbf3348d2007-12-12 08:45:45 +00002112 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal
2113 OptAddrSpace {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002114 /* "Externally Visible" Linkage */
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002115 if ($5 == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002116 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002117 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
Christopher Lambbf3348d2007-12-12 08:45:45 +00002118 $2, $4, $5->getType(), $5, $3, $6);
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00002119 CHECK_FOR_ERROR
2120 } GlobalVarAttributes {
2121 CurGV = 0;
2122 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002123 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambbf3348d2007-12-12 08:45:45 +00002124 ConstVal OptAddrSpace {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002125 if ($6 == 0)
2126 GEN_ERROR("Global value initializer is not a constant");
Christopher Lambbf3348d2007-12-12 08:45:45 +00002127 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4, $7);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002128 CHECK_FOR_ERROR
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002129 } GlobalVarAttributes {
2130 CurGV = 0;
2131 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002132 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambbf3348d2007-12-12 08:45:45 +00002133 Types OptAddrSpace {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002134 if (!UpRefs.empty())
2135 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
Christopher Lambbf3348d2007-12-12 08:45:45 +00002136 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4, $7);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002137 CHECK_FOR_ERROR
2138 delete $6;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002139 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002140 CurGV = 0;
2141 CHECK_FOR_ERROR
2142 }
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002143 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002144 std::string Name;
2145 if ($1) {
2146 Name = *$1;
2147 delete $1;
2148 }
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002149 if (Name.empty())
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002150 GEN_ERROR("Alias name cannot be empty");
2151
2152 Constant* Aliasee = $5;
2153 if (Aliasee == 0)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002154 GEN_ERROR(std::string("Invalid aliasee for alias: ") + Name);
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002155
2156 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2157 CurModule.CurrentModule);
2158 GA->setVisibility($2);
2159 InsertValue(GA, CurModule.Values);
Chris Lattner569f7372007-09-10 23:24:14 +00002160
2161
2162 // If there was a forward reference of this alias, resolve it now.
2163
2164 ValID ID;
2165 if (!Name.empty())
2166 ID = ValID::createGlobalName(Name);
2167 else
2168 ID = ValID::createGlobalID(CurModule.Values.size()-1);
2169
2170 if (GlobalValue *FWGV =
2171 CurModule.GetForwardRefForGlobal(GA->getType(), ID)) {
2172 // Replace uses of the fwdref with the actual alias.
2173 FWGV->replaceAllUsesWith(GA);
2174 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(FWGV))
2175 GV->eraseFromParent();
2176 else
2177 cast<Function>(FWGV)->eraseFromParent();
2178 }
2179 ID.destroy();
2180
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002181 CHECK_FOR_ERROR
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002182 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002183 | TARGET TargetDefinition {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002184 CHECK_FOR_ERROR
2185 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002186 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00002187 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002188 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002189 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002190
2191
2192AsmBlock : STRINGCONSTANT {
2193 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattner58af2a12006-02-15 07:22:58 +00002194 if (AsmSoFar.empty())
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002195 CurModule.CurrentModule->setModuleInlineAsm(*$1);
Chris Lattner58af2a12006-02-15 07:22:58 +00002196 else
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002197 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+*$1);
2198 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002199 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002200};
2201
Reid Spencer41dff5e2007-01-26 08:05:27 +00002202TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002203 CurModule.CurrentModule->setTargetTriple(*$3);
2204 delete $3;
John Criswell2f6a8b12006-10-24 19:09:48 +00002205 }
Chris Lattner1ae022f2006-10-22 06:08:13 +00002206 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002207 CurModule.CurrentModule->setDataLayout(*$3);
2208 delete $3;
Owen Anderson1dc69692006-10-18 02:21:48 +00002209 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002210
2211LibrariesDefinition : '[' LibList ']';
2212
2213LibList : LibList ',' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002214 CurModule.CurrentModule->addLibrary(*$3);
2215 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002216 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002217 }
2218 | STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002219 CurModule.CurrentModule->addLibrary(*$1);
2220 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002221 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002222 }
2223 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00002224 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002225 }
2226 ;
2227
2228//===----------------------------------------------------------------------===//
2229// Rules to match Function Headers
2230//===----------------------------------------------------------------------===//
2231
Reid Spencer41dff5e2007-01-26 08:05:27 +00002232ArgListH : ArgListH ',' Types OptParamAttrs OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002233 if (!UpRefs.empty())
2234 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002235 if (!(*$3)->isFirstClassType())
2236 GEN_ERROR("Argument types must be first-class");
Reid Spencer14310612006-12-31 05:40:51 +00002237 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00002238 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002239 $1->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002240 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002241 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002242 | Types OptParamAttrs OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002243 if (!UpRefs.empty())
2244 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002245 if (!(*$1)->isFirstClassType())
2246 GEN_ERROR("Argument types must be first-class");
Reid Spencer14310612006-12-31 05:40:51 +00002247 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2248 $$ = new ArgListType;
2249 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002250 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002251 };
2252
2253ArgList : ArgListH {
2254 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002255 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002256 }
2257 | ArgListH ',' DOTDOTDOT {
2258 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002259 struct ArgListEntry E;
2260 E.Ty = new PATypeHolder(Type::VoidTy);
2261 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002262 E.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00002263 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002264 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002265 }
2266 | DOTDOTDOT {
Reid Spencer14310612006-12-31 05:40:51 +00002267 $$ = new ArgListType;
2268 struct ArgListEntry E;
2269 E.Ty = new PATypeHolder(Type::VoidTy);
2270 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002271 E.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00002272 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002273 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002274 }
2275 | /* empty */ {
2276 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00002277 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002278 };
2279
Reid Spencer41dff5e2007-01-26 08:05:27 +00002280FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002281 OptFuncAttrs OptSection OptAlign OptGC {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002282 std::string FunctionName(*$3);
2283 delete $3; // Free strdup'd memory!
Chris Lattner58af2a12006-02-15 07:22:58 +00002284
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002285 // Check the function result for abstractness if this is a define. We should
2286 // have no abstract types at this point
Reid Spencer218ded22007-01-05 17:07:23 +00002287 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2288 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002289
Chris Lattnera925a142008-04-23 05:37:08 +00002290 if (!FunctionType::isValidReturnType(*$2))
2291 GEN_ERROR("Invalid result type for LLVM function");
2292
Chris Lattner58af2a12006-02-15 07:22:58 +00002293 std::vector<const Type*> ParamTypeList;
Chris Lattner58d74912008-03-12 17:45:29 +00002294 SmallVector<ParamAttrsWithIndex, 8> Attrs;
2295 if ($7 != ParamAttr::None)
2296 Attrs.push_back(ParamAttrsWithIndex::get(0, $7));
Chris Lattner58af2a12006-02-15 07:22:58 +00002297 if ($5) { // If there are arguments...
Reid Spencer7b5d4662007-04-09 06:16:21 +00002298 unsigned index = 1;
2299 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002300 const Type* Ty = I->Ty->get();
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002301 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2302 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencer14310612006-12-31 05:40:51 +00002303 ParamTypeList.push_back(Ty);
Chris Lattner58d74912008-03-12 17:45:29 +00002304 if (Ty != Type::VoidTy && I->Attrs != ParamAttr::None)
2305 Attrs.push_back(ParamAttrsWithIndex::get(index, I->Attrs));
Reid Spencer14310612006-12-31 05:40:51 +00002306 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002307 }
2308
2309 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2310 if (isVarArg) ParamTypeList.pop_back();
2311
Chris Lattner58d74912008-03-12 17:45:29 +00002312 PAListPtr PAL;
Christopher Lamb5c104242007-04-22 20:09:11 +00002313 if (!Attrs.empty())
Chris Lattner58d74912008-03-12 17:45:29 +00002314 PAL = PAListPtr::get(Attrs.begin(), Attrs.end());
Reid Spencer7b5d4662007-04-09 06:16:21 +00002315
Duncan Sandsdc024672007-11-27 13:23:08 +00002316 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002317 const PointerType *PFT = PointerType::getUnqual(FT);
Reid Spencer218ded22007-01-05 17:07:23 +00002318 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002319
2320 ValID ID;
2321 if (!FunctionName.empty()) {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002322 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattner58af2a12006-02-15 07:22:58 +00002323 } else {
Reid Spencer93c40032007-03-19 18:40:50 +00002324 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002325 }
2326
2327 Function *Fn = 0;
2328 // See if this function was forward referenced. If so, recycle the object.
2329 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2330 // Move the function to the end of the list, from whereever it was
2331 // previously inserted.
2332 Fn = cast<Function>(FWRef);
Chris Lattner58d74912008-03-12 17:45:29 +00002333 assert(Fn->getParamAttrs().isEmpty() &&
2334 "Forward reference has parameter attributes!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002335 CurModule.CurrentModule->getFunctionList().remove(Fn);
2336 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2337 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spenceref9b9a72007-02-05 20:47:22 +00002338 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002339 if (Fn->getFunctionType() != FT ) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002340 // The existing function doesn't have the same type. This is an overload
2341 // error.
2342 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Duncan Sandsdc024672007-11-27 13:23:08 +00002343 } else if (Fn->getParamAttrs() != PAL) {
2344 // The existing function doesn't have the same parameter attributes.
2345 // This is an overload error.
2346 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002347 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
Chris Lattner6cdc6822007-04-26 05:31:05 +00002348 // Neither the existing or the current function is a declaration and they
2349 // have the same name and same type. Clearly this is a redefinition.
2350 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Duncan Sandsdc024672007-11-27 13:23:08 +00002351 } else if (Fn->isDeclaration()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002352 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattner58af2a12006-02-15 07:22:58 +00002353 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2354 AI != AE; ++AI)
2355 AI->setName("");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002356 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002357 } else { // Not already defined?
Gabor Greife64d2482008-04-06 23:07:54 +00002358 Fn = Function::Create(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
2359 CurModule.CurrentModule);
Chris Lattner58af2a12006-02-15 07:22:58 +00002360 InsertValue(Fn, CurModule.Values);
2361 }
2362
2363 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002364
2365 if (CurFun.isDeclare) {
2366 // If we have declaration, always overwrite linkage. This will allow us to
2367 // correctly handle cases, when pointer to function is passed as argument to
2368 // another function.
2369 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002370 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002371 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002372 Fn->setCallingConv($1);
Duncan Sandsdc024672007-11-27 13:23:08 +00002373 Fn->setParamAttrs(PAL);
Reid Spencer218ded22007-01-05 17:07:23 +00002374 Fn->setAlignment($9);
2375 if ($8) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002376 Fn->setSection(*$8);
2377 delete $8;
Chris Lattner58af2a12006-02-15 07:22:58 +00002378 }
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002379 if ($10) {
2380 Fn->setCollector($10->c_str());
2381 delete $10;
2382 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002383
2384 // Add all of the arguments we parsed to the function...
2385 if ($5) { // Is null if empty...
2386 if (isVarArg) { // Nuke the last entry
Reid Spenceref9b9a72007-02-05 20:47:22 +00002387 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0 &&
Reid Spencera9720f52007-02-05 17:04:00 +00002388 "Not a varargs marker!");
Reid Spencer14310612006-12-31 05:40:51 +00002389 delete $5->back().Ty;
Chris Lattner58af2a12006-02-15 07:22:58 +00002390 $5->pop_back(); // Delete the last entry
2391 }
2392 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002393 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencer14310612006-12-31 05:40:51 +00002394 unsigned Idx = 1;
Reid Spenceref9b9a72007-02-05 20:47:22 +00002395 for (ArgListType::iterator I = $5->begin();
2396 I != $5->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencer14310612006-12-31 05:40:51 +00002397 delete I->Ty; // Delete the typeholder...
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002398 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002399 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002400 InsertValue(ArgIt);
Reid Spencer14310612006-12-31 05:40:51 +00002401 Idx++;
Chris Lattner58af2a12006-02-15 07:22:58 +00002402 }
Reid Spencera132e042006-12-03 05:46:11 +00002403
Chris Lattner58af2a12006-02-15 07:22:58 +00002404 delete $5; // We're now done with the argument list
2405 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002406 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002407};
2408
2409BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2410
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002411FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner58af2a12006-02-15 07:22:58 +00002412 $$ = CurFun.CurrentFunction;
2413
2414 // Make sure that we keep track of the linkage type even if there was a
2415 // previous "declare".
2416 $$->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002417 $$->setVisibility($2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002418};
2419
2420END : ENDTOK | '}'; // Allow end of '}' to end a function
2421
2422Function : BasicBlockList END {
2423 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002424 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002425};
2426
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002427FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencer14310612006-12-31 05:40:51 +00002428 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002429 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002430 $$ = CurFun.CurrentFunction;
2431 CurFun.FunctionDone();
2432 CHECK_FOR_ERROR
2433 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002434
2435//===----------------------------------------------------------------------===//
2436// Rules to match Basic Blocks
2437//===----------------------------------------------------------------------===//
2438
2439OptSideEffect : /* empty */ {
2440 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002441 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002442 }
2443 | SIDEEFFECT {
2444 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002445 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002446 };
2447
2448ConstValueRef : ESINT64VAL { // A reference to a direct constant
2449 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002450 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002451 }
2452 | EUINT64VAL {
2453 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002454 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002455 }
2456 | FPVAL { // Perhaps it's an FP constant?
2457 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002458 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002459 }
2460 | TRUETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002461 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002462 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002463 }
2464 | FALSETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002465 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002466 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002467 }
2468 | NULL_TOK {
2469 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002470 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002471 }
2472 | UNDEF {
2473 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002474 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002475 }
2476 | ZEROINITIALIZER { // A vector zero constant.
2477 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002478 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002479 }
2480 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencera132e042006-12-03 05:46:11 +00002481 const Type *ETy = (*$2)[0]->getType();
Dan Gohman180c1692008-06-23 18:43:26 +00002482 unsigned NumElements = $2->size();
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002483
2484 if (!ETy->isInteger() && !ETy->isFloatingPoint())
2485 GEN_ERROR("Invalid vector element type: " + ETy->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002486
Reid Spencer9d6565a2007-02-15 02:26:10 +00002487 VectorType* pt = VectorType::get(ETy, NumElements);
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002488 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(pt));
Chris Lattner58af2a12006-02-15 07:22:58 +00002489
2490 // Verify all elements are correct type!
2491 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00002492 if (ETy != (*$2)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002493 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00002494 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencera132e042006-12-03 05:46:11 +00002495 (*$2)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00002496 }
2497
Reid Spencer9d6565a2007-02-15 02:26:10 +00002498 $$ = ValID::create(ConstantVector::get(pt, *$2));
Chris Lattner58af2a12006-02-15 07:22:58 +00002499 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002500 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002501 }
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002502 | '[' ConstVector ']' { // Nonempty unsized arr
2503 const Type *ETy = (*$2)[0]->getType();
Dan Gohman180c1692008-06-23 18:43:26 +00002504 uint64_t NumElements = $2->size();
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002505
2506 if (!ETy->isFirstClassType())
2507 GEN_ERROR("Invalid array element type: " + ETy->getDescription());
2508
2509 ArrayType *ATy = ArrayType::get(ETy, NumElements);
2510 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(ATy));
2511
2512 // Verify all elements are correct type!
2513 for (unsigned i = 0; i < $2->size(); i++) {
2514 if (ETy != (*$2)[i]->getType())
2515 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
2516 ETy->getDescription() +"' as required!\nIt is of type '"+
2517 (*$2)[i]->getType()->getDescription() + "'.");
2518 }
2519
2520 $$ = ValID::create(ConstantArray::get(ATy, *$2));
2521 delete PTy; delete $2;
2522 CHECK_FOR_ERROR
2523 }
2524 | '[' ']' {
Dan Gohman180c1692008-06-23 18:43:26 +00002525 // Use undef instead of an array because it's inconvenient to determine
2526 // the element type at this point, there being no elements to examine.
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002527 $$ = ValID::createUndef();
2528 CHECK_FOR_ERROR
2529 }
2530 | 'c' STRINGCONSTANT {
Dan Gohman180c1692008-06-23 18:43:26 +00002531 uint64_t NumElements = $2->length();
Dan Gohmanf910eaa2008-06-09 14:45:02 +00002532 const Type *ETy = Type::Int8Ty;
2533
2534 ArrayType *ATy = ArrayType::get(ETy, NumElements);
2535
2536 std::vector<Constant*> Vals;
2537 for (unsigned i = 0; i < $2->length(); ++i)
2538 Vals.push_back(ConstantInt::get(ETy, (*$2)[i]));
2539 delete $2;
2540 $$ = ValID::create(ConstantArray::get(ATy, Vals));
2541 CHECK_FOR_ERROR
2542 }
2543 | '{' ConstVector '}' {
2544 std::vector<const Type*> Elements($2->size());
2545 for (unsigned i = 0, e = $2->size(); i != e; ++i)
2546 Elements[i] = (*$2)[i]->getType();
2547
2548 const StructType *STy = StructType::get(Elements);
2549 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(STy));
2550
2551 $$ = ValID::create(ConstantStruct::get(STy, *$2));
2552 delete PTy; delete $2;
2553 CHECK_FOR_ERROR
2554 }
2555 | '{' '}' {
2556 const StructType *STy = StructType::get(std::vector<const Type*>());
2557 $$ = ValID::create(ConstantStruct::get(STy, std::vector<Constant*>()));
2558 CHECK_FOR_ERROR
2559 }
2560 | '<' '{' ConstVector '}' '>' {
2561 std::vector<const Type*> Elements($3->size());
2562 for (unsigned i = 0, e = $3->size(); i != e; ++i)
2563 Elements[i] = (*$3)[i]->getType();
2564
2565 const StructType *STy = StructType::get(Elements, /*isPacked=*/true);
2566 PATypeHolder* PTy = new PATypeHolder(HandleUpRefs(STy));
2567
2568 $$ = ValID::create(ConstantStruct::get(STy, *$3));
2569 delete PTy; delete $3;
2570 CHECK_FOR_ERROR
2571 }
2572 | '<' '{' '}' '>' {
2573 const StructType *STy = StructType::get(std::vector<const Type*>(),
2574 /*isPacked=*/true);
2575 $$ = ValID::create(ConstantStruct::get(STy, std::vector<Constant*>()));
2576 CHECK_FOR_ERROR
2577 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002578 | ConstExpr {
Reid Spencera132e042006-12-03 05:46:11 +00002579 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002580 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002581 }
2582 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002583 $$ = ValID::createInlineAsm(*$3, *$5, $2);
2584 delete $3;
2585 delete $5;
Reid Spencer61c83e02006-08-18 08:43:06 +00002586 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002587 };
2588
2589// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2590// another value.
2591//
Reid Spencer41dff5e2007-01-26 08:05:27 +00002592SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2593 $$ = ValID::createLocalID($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002594 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002595 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002596 | GLOBALVAL_ID {
2597 $$ = ValID::createGlobalID($1);
2598 CHECK_FOR_ERROR
2599 }
2600 | LocalName { // Is it a named reference...?
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002601 $$ = ValID::createLocalName(*$1);
2602 delete $1;
Reid Spencer41dff5e2007-01-26 08:05:27 +00002603 CHECK_FOR_ERROR
2604 }
2605 | GlobalName { // Is it a named reference...?
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002606 $$ = ValID::createGlobalName(*$1);
2607 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002608 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002609 };
2610
2611// ValueRef - A reference to a definition... either constant or symbolic
2612ValueRef : SymbolicValueRef | ConstValueRef;
2613
2614
2615// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2616// type immediately preceeds the value reference, and allows complex constant
2617// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2618ResolvedVal : Types ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002619 if (!UpRefs.empty())
2620 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2621 $$ = getVal(*$1, $2);
2622 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002623 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00002624 }
2625 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002626
Devang Patel7990dc72008-02-20 22:40:23 +00002627ReturnedVal : ResolvedVal {
2628 $$ = new std::vector<Value *>();
2629 $$->push_back($1);
2630 CHECK_FOR_ERROR
2631 }
Devang Patel6bfc63b2008-02-23 00:38:56 +00002632 | ReturnedVal ',' ResolvedVal {
Devang Patel7990dc72008-02-20 22:40:23 +00002633 ($$=$1)->push_back($3);
2634 CHECK_FOR_ERROR
2635 };
2636
Chris Lattner58af2a12006-02-15 07:22:58 +00002637BasicBlockList : BasicBlockList BasicBlock {
2638 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002639 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002640 }
2641 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
2642 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002643 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002644 };
2645
2646
2647// Basic blocks are terminated by branching instructions:
2648// br, br/cc, switch, ret
2649//
Reid Spencer41dff5e2007-01-26 08:05:27 +00002650BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner58af2a12006-02-15 07:22:58 +00002651 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002652 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002653 InsertValue($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002654 $1->getInstList().push_back($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002655 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002656 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002657 };
2658
2659InstructionList : InstructionList Inst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002660 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2661 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2662 if (CI2->getParent() == 0)
2663 $1->getInstList().push_back(CI2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002664 $1->getInstList().push_back($2);
2665 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002666 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002667 }
Reid Spencer93c40032007-03-19 18:40:50 +00002668 | /* empty */ { // Empty space between instruction lists
Nick Lewycky280a6e62008-04-25 16:53:59 +00002669 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
Reid Spencer61c83e02006-08-18 08:43:06 +00002670 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002671 }
Reid Spencer93c40032007-03-19 18:40:50 +00002672 | LABELSTR { // Labelled (named) basic block
Nick Lewycky280a6e62008-04-25 16:53:59 +00002673 $$ = defineBBVal(ValID::createLocalName(*$1));
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002674 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002675 CHECK_FOR_ERROR
Nick Lewycky280a6e62008-04-25 16:53:59 +00002676
Chris Lattner58af2a12006-02-15 07:22:58 +00002677 };
2678
Devang Patel7990dc72008-02-20 22:40:23 +00002679BBTerminatorInst :
2680 RET ReturnedVal { // Return with a result...
Devang Patelb82b7f22008-02-26 22:17:48 +00002681 ValueList &VL = *$2;
Devang Patel13b823c2008-02-26 23:19:08 +00002682 assert(!VL.empty() && "Invalid ret operands!");
Gabor Greife64d2482008-04-06 23:07:54 +00002683 $$ = ReturnInst::Create(&VL[0], VL.size());
Devang Patel7990dc72008-02-20 22:40:23 +00002684 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002685 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002686 }
Reid Spencer93c40032007-03-19 18:40:50 +00002687 | RET VOID { // Return with no result...
Gabor Greife64d2482008-04-06 23:07:54 +00002688 $$ = ReturnInst::Create();
Reid Spencer61c83e02006-08-18 08:43:06 +00002689 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002690 }
Reid Spencer93c40032007-03-19 18:40:50 +00002691 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002692 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002693 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002694 $$ = BranchInst::Create(tmpBB);
Reid Spencer93c40032007-03-19 18:40:50 +00002695 } // Conditional Branch...
Reid Spencer6f407902007-01-13 05:00:46 +00002696 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002697 if (cast<IntegerType>($2)->getBitWidth() != 1)
2698 GEN_ERROR("Branch condition must have type i1");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002699 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002700 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002701 BasicBlock* tmpBBB = getBBVal($9);
2702 CHECK_FOR_ERROR
Reid Spencer4fe16d62007-01-11 18:21:29 +00002703 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002704 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002705 $$ = BranchInst::Create(tmpBBA, tmpBBB, tmpVal);
Chris Lattner58af2a12006-02-15 07:22:58 +00002706 }
2707 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002708 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002709 CHECK_FOR_ERROR
2710 BasicBlock* tmpBB = getBBVal($6);
2711 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002712 SwitchInst *S = SwitchInst::Create(tmpVal, tmpBB, $8->size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002713 $$ = S;
2714
2715 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2716 E = $8->end();
2717 for (; I != E; ++I) {
2718 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2719 S->addCase(CI, I->second);
2720 else
Reid Spencerb5334b02007-02-05 10:18:06 +00002721 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattner58af2a12006-02-15 07:22:58 +00002722 }
2723 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002724 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002725 }
2726 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002727 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002728 CHECK_FOR_ERROR
2729 BasicBlock* tmpBB = getBBVal($6);
2730 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00002731 SwitchInst *S = SwitchInst::Create(tmpVal, tmpBB, 0);
Chris Lattner58af2a12006-02-15 07:22:58 +00002732 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002733 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002734 }
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002735 | INVOKE OptCallingConv ResultTypes ValueRef '(' ParamList ')' OptFuncAttrs
Chris Lattner58af2a12006-02-15 07:22:58 +00002736 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattner58af2a12006-02-15 07:22:58 +00002737
Reid Spencer14310612006-12-31 05:40:51 +00002738 // Handle the short syntax
2739 const PointerType *PFTy = 0;
2740 const FunctionType *Ty = 0;
Reid Spencer218ded22007-01-05 17:07:23 +00002741 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00002742 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2743 // Pull out the types of all of the arguments...
2744 std::vector<const Type*> ParamTypes;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002745 ParamList::iterator I = $6->begin(), E = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002746 for (; I != E; ++I) {
Reid Spencer14310612006-12-31 05:40:51 +00002747 const Type *Ty = I->Val->getType();
2748 if (Ty == Type::VoidTy)
2749 GEN_ERROR("Short call syntax cannot be used with varargs");
2750 ParamTypes.push_back(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002751 }
Chris Lattnera925a142008-04-23 05:37:08 +00002752
2753 if (!FunctionType::isValidReturnType(*$3))
2754 GEN_ERROR("Invalid result type for LLVM function");
2755
Duncan Sandsdc024672007-11-27 13:23:08 +00002756 Ty = FunctionType::get($3->get(), ParamTypes, false);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002757 PFTy = PointerType::getUnqual(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002758 }
2759
Reid Spencer66728ef2007-03-20 01:13:36 +00002760 delete $3;
2761
Chris Lattner58af2a12006-02-15 07:22:58 +00002762 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002763 CHECK_FOR_ERROR
Reid Spencer218ded22007-01-05 17:07:23 +00002764 BasicBlock *Normal = getBBVal($11);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002765 CHECK_FOR_ERROR
Reid Spencer218ded22007-01-05 17:07:23 +00002766 BasicBlock *Except = getBBVal($14);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002767 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002768
Chris Lattner58d74912008-03-12 17:45:29 +00002769 SmallVector<ParamAttrsWithIndex, 8> Attrs;
2770 if ($8 != ParamAttr::None)
2771 Attrs.push_back(ParamAttrsWithIndex::get(0, $8));
Duncan Sandsdc024672007-11-27 13:23:08 +00002772
Reid Spencer14310612006-12-31 05:40:51 +00002773 // Check the arguments
2774 ValueList Args;
2775 if ($6->empty()) { // Has no arguments?
2776 // Make sure no arguments is a good thing!
2777 if (Ty->getNumParams() != 0)
2778 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00002779 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00002780 } else { // Has arguments?
2781 // Loop through FunctionType's arguments and ensure they are specified
2782 // correctly!
Chris Lattner58af2a12006-02-15 07:22:58 +00002783 FunctionType::param_iterator I = Ty->param_begin();
2784 FunctionType::param_iterator E = Ty->param_end();
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002785 ParamList::iterator ArgI = $6->begin(), ArgE = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002786 unsigned index = 1;
Chris Lattner58af2a12006-02-15 07:22:58 +00002787
Duncan Sandsdc024672007-11-27 13:23:08 +00002788 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002789 if (ArgI->Val->getType() != *I)
2790 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00002791 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00002792 Args.push_back(ArgI->Val);
Chris Lattner58d74912008-03-12 17:45:29 +00002793 if (ArgI->Attrs != ParamAttr::None)
2794 Attrs.push_back(ParamAttrsWithIndex::get(index, ArgI->Attrs));
Reid Spencer14310612006-12-31 05:40:51 +00002795 }
Reid Spencera132e042006-12-03 05:46:11 +00002796
Reid Spencer14310612006-12-31 05:40:51 +00002797 if (Ty->isVarArg()) {
2798 if (I == E)
Chris Lattner38905612008-02-19 04:36:25 +00002799 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002800 Args.push_back(ArgI->Val); // push the remaining varargs
Chris Lattner58d74912008-03-12 17:45:29 +00002801 if (ArgI->Attrs != ParamAttr::None)
2802 Attrs.push_back(ParamAttrsWithIndex::get(index, ArgI->Attrs));
Chris Lattner38905612008-02-19 04:36:25 +00002803 }
Reid Spencer14310612006-12-31 05:40:51 +00002804 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00002805 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00002806 }
Reid Spencer14310612006-12-31 05:40:51 +00002807
Chris Lattner58d74912008-03-12 17:45:29 +00002808 PAListPtr PAL;
Duncan Sandsdc024672007-11-27 13:23:08 +00002809 if (!Attrs.empty())
Chris Lattner58d74912008-03-12 17:45:29 +00002810 PAL = PAListPtr::get(Attrs.begin(), Attrs.end());
Duncan Sandsdc024672007-11-27 13:23:08 +00002811
Reid Spencer14310612006-12-31 05:40:51 +00002812 // Create the InvokeInst
Dan Gohman041e2eb2008-05-15 19:50:34 +00002813 InvokeInst *II = InvokeInst::Create(V, Normal, Except,
2814 Args.begin(), Args.end());
Reid Spencer14310612006-12-31 05:40:51 +00002815 II->setCallingConv($2);
Duncan Sandsdc024672007-11-27 13:23:08 +00002816 II->setParamAttrs(PAL);
Reid Spencer14310612006-12-31 05:40:51 +00002817 $$ = II;
Chris Lattner58af2a12006-02-15 07:22:58 +00002818 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002819 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002820 }
2821 | UNWIND {
2822 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002823 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002824 }
2825 | UNREACHABLE {
2826 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002827 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002828 };
2829
2830
2831
2832JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2833 $$ = $1;
Reid Spencer93c40032007-03-19 18:40:50 +00002834 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002835 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002836 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002837 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00002838
Reid Spencer5b7e7532006-09-28 19:28:24 +00002839 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002840 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002841 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002842 }
2843 | IntType ConstValueRef ',' LABEL ValueRef {
2844 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencer93c40032007-03-19 18:40:50 +00002845 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002846 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002847
2848 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002849 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00002850
Reid Spencer5b7e7532006-09-28 19:28:24 +00002851 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002852 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002853 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002854 };
2855
Reid Spencer41dff5e2007-01-26 08:05:27 +00002856Inst : OptLocalAssign InstVal {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002857 // Is this definition named?? if so, assign the name...
2858 setValueName($2, $1);
2859 CHECK_FOR_ERROR
2860 InsertValue($2);
2861 $$ = $2;
2862 CHECK_FOR_ERROR
2863 };
2864
Chris Lattner58af2a12006-02-15 07:22:58 +00002865
2866PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer14310612006-12-31 05:40:51 +00002867 if (!UpRefs.empty())
2868 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002869 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencera132e042006-12-03 05:46:11 +00002870 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002871 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002872 BasicBlock* tmpBB = getBBVal($5);
2873 CHECK_FOR_ERROR
2874 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencera132e042006-12-03 05:46:11 +00002875 delete $1;
Chris Lattner58af2a12006-02-15 07:22:58 +00002876 }
2877 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2878 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002879 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002880 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002881 BasicBlock* tmpBB = getBBVal($6);
2882 CHECK_FOR_ERROR
2883 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002884 };
2885
2886
Duncan Sandsdc024672007-11-27 13:23:08 +00002887ParamList : Types OptParamAttrs ValueRef OptParamAttrs {
2888 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Reid Spencer14310612006-12-31 05:40:51 +00002889 if (!UpRefs.empty())
2890 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2891 // Used for call and invoke instructions
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002892 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00002893 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getVal($1->get(), $3);
Reid Spencer14310612006-12-31 05:40:51 +00002894 $$->push_back(E);
Reid Spencer66728ef2007-03-20 01:13:36 +00002895 delete $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002896 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002897 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002898 | LABEL OptParamAttrs ValueRef OptParamAttrs {
2899 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002900 // Labels are only valid in ASMs
2901 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00002902 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getBBVal($3);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002903 $$->push_back(E);
Duncan Sandsdc024672007-11-27 13:23:08 +00002904 CHECK_FOR_ERROR
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002905 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002906 | ParamList ',' Types OptParamAttrs ValueRef OptParamAttrs {
2907 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Reid Spencer14310612006-12-31 05:40:51 +00002908 if (!UpRefs.empty())
2909 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002910 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002911 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getVal($3->get(), $5);
Reid Spencer14310612006-12-31 05:40:51 +00002912 $$->push_back(E);
Reid Spencer66728ef2007-03-20 01:13:36 +00002913 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002914 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00002915 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002916 | ParamList ',' LABEL OptParamAttrs ValueRef OptParamAttrs {
2917 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002918 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002919 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getBBVal($5);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002920 $$->push_back(E);
2921 CHECK_FOR_ERROR
2922 }
2923 | /*empty*/ { $$ = new ParamList(); };
Chris Lattner58af2a12006-02-15 07:22:58 +00002924
Reid Spencer14310612006-12-31 05:40:51 +00002925IndexList // Used for gep instructions and constant expressions
Reid Spencerc6c59fd2006-12-31 21:47:02 +00002926 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencer14310612006-12-31 05:40:51 +00002927 | IndexList ',' ResolvedVal {
2928 $$ = $1;
2929 $$->push_back($3);
2930 CHECK_FOR_ERROR
2931 }
Reid Spencerc6c59fd2006-12-31 21:47:02 +00002932 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002933
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002934ConstantIndexList // Used for insertvalue and extractvalue instructions
2935 : ',' EUINT64VAL {
2936 $$ = new std::vector<unsigned>();
2937 if ((unsigned)$2 != $2)
2938 GEN_ERROR("Index " + utostr($2) + " is not valid for insertvalue or extractvalue.");
2939 $$->push_back($2);
2940 }
2941 | ConstantIndexList ',' EUINT64VAL {
2942 $$ = $1;
2943 if ((unsigned)$3 != $3)
2944 GEN_ERROR("Index " + utostr($3) + " is not valid for insertvalue or extractvalue.");
2945 $$->push_back($3);
2946 CHECK_FOR_ERROR
2947 }
2948 ;
2949
Chris Lattner58af2a12006-02-15 07:22:58 +00002950OptTailCall : TAIL CALL {
2951 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002952 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002953 }
2954 | CALL {
2955 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002956 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002957 };
2958
Chris Lattner58af2a12006-02-15 07:22:58 +00002959InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002960 if (!UpRefs.empty())
2961 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002962 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencer9d6565a2007-02-15 02:26:10 +00002963 !isa<VectorType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002964 GEN_ERROR(
Reid Spencerb5334b02007-02-05 10:18:06 +00002965 "Arithmetic operator requires integer, FP, or packed operands");
Reid Spencera132e042006-12-03 05:46:11 +00002966 Value* val1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002967 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002968 Value* val2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002969 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00002970 $$ = BinaryOperator::Create($1, val1, val2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002971 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002972 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00002973 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002974 }
2975 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002976 if (!UpRefs.empty())
2977 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002978 if (!(*$2)->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00002979 if (Instruction::isShift($1) || !isa<VectorType>($2->get()) ||
2980 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00002981 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00002982 }
Reid Spencera132e042006-12-03 05:46:11 +00002983 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002984 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002985 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002986 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00002987 $$ = BinaryOperator::Create($1, tmpVal1, tmpVal2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002988 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002989 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00002990 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002991 }
Reid Spencera132e042006-12-03 05:46:11 +00002992 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002993 if (!UpRefs.empty())
2994 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002995 if (isa<VectorType>((*$3).get()))
Chris Lattner32980692007-02-19 07:44:24 +00002996 GEN_ERROR("Vector types not supported by icmp instruction");
Reid Spencera132e042006-12-03 05:46:11 +00002997 Value* tmpVal1 = getVal(*$3, $4);
2998 CHECK_FOR_ERROR
2999 Value* tmpVal2 = getVal(*$3, $6);
3000 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003001 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Reid Spencera132e042006-12-03 05:46:11 +00003002 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003003 GEN_ERROR("icmp operator returned null");
Reid Spencer66728ef2007-03-20 01:13:36 +00003004 delete $3;
Reid Spencera132e042006-12-03 05:46:11 +00003005 }
3006 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00003007 if (!UpRefs.empty())
3008 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00003009 if (isa<VectorType>((*$3).get()))
Chris Lattner32980692007-02-19 07:44:24 +00003010 GEN_ERROR("Vector types not supported by fcmp instruction");
Reid Spencera132e042006-12-03 05:46:11 +00003011 Value* tmpVal1 = getVal(*$3, $4);
3012 CHECK_FOR_ERROR
3013 Value* tmpVal2 = getVal(*$3, $6);
3014 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003015 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Reid Spencera132e042006-12-03 05:46:11 +00003016 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00003017 GEN_ERROR("fcmp operator returned null");
Reid Spencer66728ef2007-03-20 01:13:36 +00003018 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00003019 }
Nate Begemanac80ade2008-05-12 19:01:56 +00003020 | VICMP IPredicates Types ValueRef ',' ValueRef {
3021 if (!UpRefs.empty())
3022 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
3023 if (!isa<VectorType>((*$3).get()))
3024 GEN_ERROR("Scalar types not supported by vicmp instruction");
3025 Value* tmpVal1 = getVal(*$3, $4);
3026 CHECK_FOR_ERROR
3027 Value* tmpVal2 = getVal(*$3, $6);
3028 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003029 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Nate Begemanac80ade2008-05-12 19:01:56 +00003030 if ($$ == 0)
3031 GEN_ERROR("icmp operator returned null");
3032 delete $3;
3033 }
3034 | VFCMP FPredicates Types ValueRef ',' ValueRef {
3035 if (!UpRefs.empty())
3036 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
3037 if (!isa<VectorType>((*$3).get()))
3038 GEN_ERROR("Scalar types not supported by vfcmp instruction");
3039 Value* tmpVal1 = getVal(*$3, $4);
3040 CHECK_FOR_ERROR
3041 Value* tmpVal2 = getVal(*$3, $6);
3042 CHECK_FOR_ERROR
Dan Gohmane4977cf2008-05-23 01:55:30 +00003043 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
Nate Begemanac80ade2008-05-12 19:01:56 +00003044 if ($$ == 0)
3045 GEN_ERROR("fcmp operator returned null");
3046 delete $3;
3047 }
Reid Spencer3da59db2006-11-27 01:05:10 +00003048 | CastOps ResolvedVal TO Types {
Reid Spencer14310612006-12-31 05:40:51 +00003049 if (!UpRefs.empty())
3050 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003051 Value* Val = $2;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00003052 const Type* DestTy = $4->get();
3053 if (!CastInst::castIsValid($1, Val, DestTy))
3054 GEN_ERROR("invalid cast opcode for cast from '" +
3055 Val->getType()->getDescription() + "' to '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00003056 DestTy->getDescription() + "'");
Dan Gohmane4977cf2008-05-23 01:55:30 +00003057 $$ = CastInst::Create($1, Val, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00003058 delete $4;
Chris Lattner58af2a12006-02-15 07:22:58 +00003059 }
3060 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer4fe16d62007-01-11 18:21:29 +00003061 if ($2->getType() != Type::Int1Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00003062 GEN_ERROR("select condition must be boolean");
Reid Spencera132e042006-12-03 05:46:11 +00003063 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00003064 GEN_ERROR("select value types should match");
Gabor Greife64d2482008-04-06 23:07:54 +00003065 $$ = SelectInst::Create($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003066 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003067 }
3068 | VAARG ResolvedVal ',' Types {
Reid Spencer14310612006-12-31 05:40:51 +00003069 if (!UpRefs.empty())
3070 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003071 $$ = new VAArgInst($2, *$4);
3072 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00003073 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003074 }
Chris Lattner58af2a12006-02-15 07:22:58 +00003075 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003076 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerb5334b02007-02-05 10:18:06 +00003077 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00003078 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003079 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003080 }
3081 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003082 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00003083 GEN_ERROR("Invalid insertelement operands");
Gabor Greife64d2482008-04-06 23:07:54 +00003084 $$ = InsertElementInst::Create($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003085 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003086 }
Chris Lattnerd5efe842006-04-08 01:18:56 +00003087 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003088 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00003089 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00003090 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003091 CHECK_FOR_ERROR
Chris Lattnerd5efe842006-04-08 01:18:56 +00003092 }
Chris Lattner58af2a12006-02-15 07:22:58 +00003093 | PHI_TOK PHIList {
3094 const Type *Ty = $2->front().first->getType();
3095 if (!Ty->isFirstClassType())
Reid Spencerb5334b02007-02-05 10:18:06 +00003096 GEN_ERROR("PHI node operands must be of first class type");
Gabor Greife64d2482008-04-06 23:07:54 +00003097 $$ = PHINode::Create(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00003098 ((PHINode*)$$)->reserveOperandSpace($2->size());
3099 while ($2->begin() != $2->end()) {
3100 if ($2->front().first->getType() != Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00003101 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattner58af2a12006-02-15 07:22:58 +00003102 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
3103 $2->pop_front();
3104 }
3105 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00003106 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003107 }
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003108 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ParamList ')'
Reid Spencer218ded22007-01-05 17:07:23 +00003109 OptFuncAttrs {
Reid Spencer14310612006-12-31 05:40:51 +00003110
3111 // Handle the short syntax
Reid Spencer3da59db2006-11-27 01:05:10 +00003112 const PointerType *PFTy = 0;
3113 const FunctionType *Ty = 0;
Reid Spencer218ded22007-01-05 17:07:23 +00003114 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00003115 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3116 // Pull out the types of all of the arguments...
3117 std::vector<const Type*> ParamTypes;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003118 ParamList::iterator I = $6->begin(), E = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00003119 for (; I != E; ++I) {
Reid Spencer14310612006-12-31 05:40:51 +00003120 const Type *Ty = I->Val->getType();
3121 if (Ty == Type::VoidTy)
3122 GEN_ERROR("Short call syntax cannot be used with varargs");
3123 ParamTypes.push_back(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00003124 }
Chris Lattnera925a142008-04-23 05:37:08 +00003125
3126 if (!FunctionType::isValidReturnType(*$3))
3127 GEN_ERROR("Invalid result type for LLVM function");
3128
Duncan Sandsdc024672007-11-27 13:23:08 +00003129 Ty = FunctionType::get($3->get(), ParamTypes, false);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00003130 PFTy = PointerType::getUnqual(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00003131 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00003132
Chris Lattner58af2a12006-02-15 07:22:58 +00003133 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00003134 CHECK_FOR_ERROR
Chris Lattner6cdc6822007-04-26 05:31:05 +00003135
Reid Spencer7780acb2007-04-16 06:56:07 +00003136 // Check for call to invalid intrinsic to avoid crashing later.
3137 if (Function *theF = dyn_cast<Function>(V)) {
Reid Spencered48de22007-04-16 22:02:23 +00003138 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
Reid Spencer36fdde12007-04-16 20:35:38 +00003139 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
3140 !theF->getIntrinsicID(true))
Reid Spencer7780acb2007-04-16 06:56:07 +00003141 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
3142 theF->getName() + "'");
3143 }
3144
Duncan Sandsdc024672007-11-27 13:23:08 +00003145 // Set up the ParamAttrs for the function
Chris Lattner58d74912008-03-12 17:45:29 +00003146 SmallVector<ParamAttrsWithIndex, 8> Attrs;
3147 if ($8 != ParamAttr::None)
3148 Attrs.push_back(ParamAttrsWithIndex::get(0, $8));
Reid Spencer14310612006-12-31 05:40:51 +00003149 // Check the arguments
3150 ValueList Args;
3151 if ($6->empty()) { // Has no arguments?
Chris Lattner58af2a12006-02-15 07:22:58 +00003152 // Make sure no arguments is a good thing!
3153 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00003154 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00003155 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00003156 } else { // Has arguments?
3157 // Loop through FunctionType's arguments and ensure they are specified
Duncan Sandsdc024672007-11-27 13:23:08 +00003158 // correctly. Also, gather any parameter attributes.
Chris Lattner58af2a12006-02-15 07:22:58 +00003159 FunctionType::param_iterator I = Ty->param_begin();
3160 FunctionType::param_iterator E = Ty->param_end();
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003161 ParamList::iterator ArgI = $6->begin(), ArgE = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00003162 unsigned index = 1;
Chris Lattner58af2a12006-02-15 07:22:58 +00003163
Duncan Sandsdc024672007-11-27 13:23:08 +00003164 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00003165 if (ArgI->Val->getType() != *I)
3166 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00003167 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00003168 Args.push_back(ArgI->Val);
Chris Lattner58d74912008-03-12 17:45:29 +00003169 if (ArgI->Attrs != ParamAttr::None)
3170 Attrs.push_back(ParamAttrsWithIndex::get(index, ArgI->Attrs));
Reid Spencer14310612006-12-31 05:40:51 +00003171 }
3172 if (Ty->isVarArg()) {
3173 if (I == E)
Chris Lattner38905612008-02-19 04:36:25 +00003174 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00003175 Args.push_back(ArgI->Val); // push the remaining varargs
Chris Lattner58d74912008-03-12 17:45:29 +00003176 if (ArgI->Attrs != ParamAttr::None)
3177 Attrs.push_back(ParamAttrsWithIndex::get(index, ArgI->Attrs));
Chris Lattner38905612008-02-19 04:36:25 +00003178 }
Reid Spencer14310612006-12-31 05:40:51 +00003179 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00003180 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00003181 }
Duncan Sandsdc024672007-11-27 13:23:08 +00003182
3183 // Finish off the ParamAttrs and check them
Chris Lattner58d74912008-03-12 17:45:29 +00003184 PAListPtr PAL;
Duncan Sandsdc024672007-11-27 13:23:08 +00003185 if (!Attrs.empty())
Chris Lattner58d74912008-03-12 17:45:29 +00003186 PAL = PAListPtr::get(Attrs.begin(), Attrs.end());
Duncan Sandsdc024672007-11-27 13:23:08 +00003187
Reid Spencer14310612006-12-31 05:40:51 +00003188 // Create the call node
Gabor Greife64d2482008-04-06 23:07:54 +00003189 CallInst *CI = CallInst::Create(V, Args.begin(), Args.end());
Reid Spencer14310612006-12-31 05:40:51 +00003190 CI->setTailCall($1);
3191 CI->setCallingConv($2);
Duncan Sandsdc024672007-11-27 13:23:08 +00003192 CI->setParamAttrs(PAL);
Reid Spencer14310612006-12-31 05:40:51 +00003193 $$ = CI;
Chris Lattner58af2a12006-02-15 07:22:58 +00003194 delete $6;
Reid Spencer41dff5e2007-01-26 08:05:27 +00003195 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00003196 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003197 }
3198 | MemoryInst {
3199 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00003200 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003201 };
3202
Chris Lattner58af2a12006-02-15 07:22:58 +00003203OptVolatile : VOLATILE {
3204 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00003205 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003206 }
3207 | /* empty */ {
3208 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00003209 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003210 };
3211
3212
3213
3214MemoryInst : MALLOC Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003215 if (!UpRefs.empty())
3216 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003217 $$ = new MallocInst(*$2, 0, $3);
3218 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003219 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003220 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003221 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003222 if (!UpRefs.empty())
3223 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003224 if ($4 != Type::Int32Ty)
3225 GEN_ERROR("Malloc array size is not a 32-bit integer!");
Reid Spencera132e042006-12-03 05:46:11 +00003226 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003227 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003228 $$ = new MallocInst(*$2, tmpVal, $6);
3229 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003230 }
3231 | ALLOCA Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003232 if (!UpRefs.empty())
3233 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003234 $$ = new AllocaInst(*$2, 0, $3);
3235 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003236 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003237 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003238 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003239 if (!UpRefs.empty())
3240 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003241 if ($4 != Type::Int32Ty)
3242 GEN_ERROR("Alloca array size is not a 32-bit integer!");
Reid Spencera132e042006-12-03 05:46:11 +00003243 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003244 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003245 $$ = new AllocaInst(*$2, tmpVal, $6);
3246 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003247 }
3248 | FREE ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003249 if (!isa<PointerType>($2->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003250 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerb5334b02007-02-05 10:18:06 +00003251 $2->getType()->getDescription() + "");
Reid Spencera132e042006-12-03 05:46:11 +00003252 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00003253 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003254 }
3255
Christopher Lamb5c104242007-04-22 20:09:11 +00003256 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003257 if (!UpRefs.empty())
3258 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003259 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003260 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003261 (*$3)->getDescription());
3262 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00003263 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003264 (*$3)->getDescription());
3265 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003266 CHECK_FOR_ERROR
Christopher Lamb5c104242007-04-22 20:09:11 +00003267 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencera132e042006-12-03 05:46:11 +00003268 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00003269 }
Christopher Lamb5c104242007-04-22 20:09:11 +00003270 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003271 if (!UpRefs.empty())
3272 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003273 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00003274 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00003275 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003276 (*$5)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00003277 const Type *ElTy = PT->getElementType();
Reid Spencera132e042006-12-03 05:46:11 +00003278 if (ElTy != $3->getType())
3279 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerb5334b02007-02-05 10:18:06 +00003280 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00003281
Reid Spencera132e042006-12-03 05:46:11 +00003282 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003283 CHECK_FOR_ERROR
Christopher Lamb5c104242007-04-22 20:09:11 +00003284 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencera132e042006-12-03 05:46:11 +00003285 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00003286 }
Dan Gohmane4977cf2008-05-23 01:55:30 +00003287 | GETRESULT Types ValueRef ',' EUINT64VAL {
Devang Patelbd41a062008-02-22 19:31:30 +00003288 Value *TmpVal = getVal($2->get(), $3);
Devang Patel5a970972008-02-19 22:27:01 +00003289 if (!GetResultInst::isValidOperands(TmpVal, $5))
3290 GEN_ERROR("Invalid getresult operands");
3291 $$ = new GetResultInst(TmpVal, $5);
Devang Patel6bfc63b2008-02-23 00:38:56 +00003292 delete $2;
Devang Patel5a970972008-02-19 22:27:01 +00003293 CHECK_FOR_ERROR
3294 }
Chris Lattner58af2a12006-02-15 07:22:58 +00003295 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer14310612006-12-31 05:40:51 +00003296 if (!UpRefs.empty())
3297 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003298 if (!isa<PointerType>($2->get()))
Reid Spencerb5334b02007-02-05 10:18:06 +00003299 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00003300
Dan Gohman041e2eb2008-05-15 19:50:34 +00003301 if (!GetElementPtrInst::getIndexedType(*$2, $4->begin(), $4->end()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003302 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00003303 (*$2)->getDescription()+ "'");
Reid Spencera132e042006-12-03 05:46:11 +00003304 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003305 CHECK_FOR_ERROR
Gabor Greife64d2482008-04-06 23:07:54 +00003306 $$ = GetElementPtrInst::Create(tmpVal, $4->begin(), $4->end());
Reid Spencera132e042006-12-03 05:46:11 +00003307 delete $2;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003308 delete $4;
Dan Gohmane4977cf2008-05-23 01:55:30 +00003309 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003310 | EXTRACTVALUE Types ValueRef ConstantIndexList {
Dan Gohmane4977cf2008-05-23 01:55:30 +00003311 if (!UpRefs.empty())
3312 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3313 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3314 GEN_ERROR("extractvalue insn requires an aggregate operand");
3315
3316 if (!ExtractValueInst::getIndexedType(*$2, $4->begin(), $4->end()))
3317 GEN_ERROR("Invalid extractvalue indices for type '" +
3318 (*$2)->getDescription()+ "'");
3319 Value* tmpVal = getVal(*$2, $3);
3320 CHECK_FOR_ERROR
3321 $$ = ExtractValueInst::Create(tmpVal, $4->begin(), $4->end());
3322 delete $2;
3323 delete $4;
3324 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +00003325 | INSERTVALUE Types ValueRef ',' Types ValueRef ConstantIndexList {
Dan Gohmane4977cf2008-05-23 01:55:30 +00003326 if (!UpRefs.empty())
3327 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
3328 if (!isa<StructType>($2->get()) && !isa<ArrayType>($2->get()))
3329 GEN_ERROR("extractvalue insn requires an aggregate operand");
3330
3331 if (ExtractValueInst::getIndexedType(*$2, $7->begin(), $7->end()) != $5->get())
3332 GEN_ERROR("Invalid insertvalue indices for type '" +
3333 (*$2)->getDescription()+ "'");
3334 Value* aggVal = getVal(*$2, $3);
3335 Value* tmpVal = getVal(*$5, $6);
3336 CHECK_FOR_ERROR
3337 $$ = InsertValueInst::Create(aggVal, tmpVal, $7->begin(), $7->end());
3338 delete $2;
3339 delete $5;
3340 delete $7;
Chris Lattner58af2a12006-02-15 07:22:58 +00003341 };
3342
3343
3344%%
Reid Spencer61c83e02006-08-18 08:43:06 +00003345
Reid Spencer14310612006-12-31 05:40:51 +00003346// common code from the two 'RunVMAsmParser' functions
3347static Module* RunParser(Module * M) {
Reid Spencer14310612006-12-31 05:40:51 +00003348 CurModule.CurrentModule = M;
Reid Spencer14310612006-12-31 05:40:51 +00003349 // Check to make sure the parser succeeded
3350 if (yyparse()) {
3351 if (ParserResult)
3352 delete ParserResult;
3353 return 0;
3354 }
3355
Reid Spencer0d60b5a2007-03-30 01:37:39 +00003356 // Emit an error if there are any unresolved types left.
3357 if (!CurModule.LateResolveTypes.empty()) {
3358 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3359 if (DID.Type == ValID::LocalName) {
3360 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3361 } else {
3362 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3363 }
3364 if (ParserResult)
3365 delete ParserResult;
3366 return 0;
3367 }
3368
3369 // Emit an error if there are any unresolved values left.
3370 if (!CurModule.LateResolveValues.empty()) {
3371 Value *V = CurModule.LateResolveValues.back();
3372 std::map<Value*, std::pair<ValID, int> >::iterator I =
3373 CurModule.PlaceHolderInfo.find(V);
3374
3375 if (I != CurModule.PlaceHolderInfo.end()) {
3376 ValID &DID = I->second.first;
3377 if (DID.Type == ValID::LocalName) {
3378 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3379 } else {
3380 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3381 }
3382 if (ParserResult)
3383 delete ParserResult;
3384 return 0;
3385 }
3386 }
3387
Reid Spencer14310612006-12-31 05:40:51 +00003388 // Check to make sure that parsing produced a result
3389 if (!ParserResult)
3390 return 0;
3391
3392 // Reset ParserResult variable while saving its value for the result.
3393 Module *Result = ParserResult;
3394 ParserResult = 0;
3395
3396 return Result;
3397}
3398
Reid Spencer61c83e02006-08-18 08:43:06 +00003399void llvm::GenerateError(const std::string &message, int LineNo) {
Duncan Sandsdc024672007-11-27 13:23:08 +00003400 if (LineNo == -1) LineNo = LLLgetLineNo();
Reid Spencer61c83e02006-08-18 08:43:06 +00003401 // TODO: column number in exception
3402 if (TheParseError)
Duncan Sandsdc024672007-11-27 13:23:08 +00003403 TheParseError->setError(LLLgetFilename(), message, LineNo);
Reid Spencer61c83e02006-08-18 08:43:06 +00003404 TriggerError = 1;
3405}
3406
Chris Lattner58af2a12006-02-15 07:22:58 +00003407int yyerror(const char *ErrorMsg) {
Duncan Sandsdc024672007-11-27 13:23:08 +00003408 std::string where = LLLgetFilename() + ":" + utostr(LLLgetLineNo()) + ": ";
Reid Spenceref9b9a72007-02-05 20:47:22 +00003409 std::string errMsg = where + "error: " + std::string(ErrorMsg);
Duncan Sandsdc024672007-11-27 13:23:08 +00003410 if (yychar != YYEMPTY && yychar != 0) {
3411 errMsg += " while reading token: '";
3412 errMsg += std::string(LLLgetTokenStart(),
3413 LLLgetTokenStart()+LLLgetTokenLength()) + "'";
3414 }
Reid Spencer61c83e02006-08-18 08:43:06 +00003415 GenerateError(errMsg);
Chris Lattner58af2a12006-02-15 07:22:58 +00003416 return 0;
3417}