blob: 2ef3d8b9f1afaad69b01fb32f13d5fa293f51e4b [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 }
Dale Johannesenc72cd7e2007-09-11 18:33:39 +0000411 // Lexer has no type info, so builds all float and double FP constants
412 // 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);
416 return ConstantFP::get(Ty, *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)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000478 GenerateError("Invalid use of a composite 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))
496 V = new Function(FTy, GlobalValue::ExternalLinkage);
497 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.
520static 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 }
550
551 ID.destroy();
552 return BB;
553 }
554
555 // We haven't seen this BB before and its first mention is a definition.
556 // Just create it and return it.
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000557 std::string Name (ID.Type == ValID::LocalName ? ID.getName() : "");
Reid Spencer93c40032007-03-19 18:40:50 +0000558 BB = new BasicBlock(Name, CurFun.CurrentFunction);
559 if (ID.Type == ValID::LocalID) {
560 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
561 InsertValue(BB);
Chris Lattner58af2a12006-02-15 07:22:58 +0000562 }
Reid Spencer93c40032007-03-19 18:40:50 +0000563
564 ID.destroy(); // Free strdup'd memory
565 return BB;
566}
567
568/// getBBVal - get an existing BB value or create a forward reference for it.
569///
570static BasicBlock *getBBVal(const ValID &ID) {
571 assert(inFunctionScope() && "Can't get basic block at global scope!");
572
573 BasicBlock *BB = 0;
574
575 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
576 if (BBI != CurFun.BBForwardRefs.end()) {
577 BB = BBI->second;
578 } if (ID.Type == ValID::LocalName) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000579 std::string Name = ID.getName();
Reid Spencer93c40032007-03-19 18:40:50 +0000580 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000581 if (N) {
Reid Spencer93c40032007-03-19 18:40:50 +0000582 if (N->getType()->getTypeID() == Type::LabelTyID)
583 BB = cast<BasicBlock>(N);
584 else
585 GenerateError("Reference to label '" + Name + "' is actually of type '"+
586 N->getType()->getDescription() + "'");
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000587 }
Reid Spencer93c40032007-03-19 18:40:50 +0000588 } else if (ID.Type == ValID::LocalID) {
589 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
590 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
591 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
592 else
593 GenerateError("Reference to label '%" + utostr(ID.Num) +
594 "' is actually of type '"+
595 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
596 }
597 } else {
598 GenerateError("Illegal label reference " + ID.getName());
599 return 0;
600 }
601
602 // If its already been defined, return it now.
603 if (BB) {
604 ID.destroy(); // Free strdup'd memory.
605 return BB;
606 }
607
608 // Otherwise, this block has not been seen before, create it.
609 std::string Name;
610 if (ID.Type == ValID::LocalName)
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000611 Name = ID.getName();
Reid Spencer93c40032007-03-19 18:40:50 +0000612 BB = new BasicBlock(Name, CurFun.CurrentFunction);
613
614 // Insert it in the forward refs map.
615 CurFun.BBForwardRefs[ID] = BB;
616
Chris Lattner58af2a12006-02-15 07:22:58 +0000617 return BB;
618}
619
620
621//===----------------------------------------------------------------------===//
622// Code to handle forward references in instructions
623//===----------------------------------------------------------------------===//
624//
625// This code handles the late binding needed with statements that reference
626// values not defined yet... for example, a forward branch, or the PHI node for
627// a loop body.
628//
629// This keeps a table (CurFun.LateResolveValues) of all such forward references
630// and back patchs after we are done.
631//
632
633// ResolveDefinitions - If we could not resolve some defs at parsing
634// time (forward branches, phi functions for loops, etc...) resolve the
635// defs now...
636//
637static void
Reid Spencer93c40032007-03-19 18:40:50 +0000638ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000639 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencer93c40032007-03-19 18:40:50 +0000640 while (!LateResolvers.empty()) {
641 Value *V = LateResolvers.back();
642 LateResolvers.pop_back();
Chris Lattner58af2a12006-02-15 07:22:58 +0000643
Reid Spencer93c40032007-03-19 18:40:50 +0000644 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
645 CurModule.PlaceHolderInfo.find(V);
646 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000647
Reid Spencer93c40032007-03-19 18:40:50 +0000648 ValID &DID = PHI->second.first;
Chris Lattner58af2a12006-02-15 07:22:58 +0000649
Reid Spencer93c40032007-03-19 18:40:50 +0000650 Value *TheRealValue = getExistingVal(V->getType(), DID);
651 if (TriggerError)
652 return;
653 if (TheRealValue) {
654 V->replaceAllUsesWith(TheRealValue);
655 delete V;
656 CurModule.PlaceHolderInfo.erase(PHI);
657 } else if (FutureLateResolvers) {
658 // Functions have their unresolved items forwarded to the module late
659 // resolver table
660 InsertValue(V, *FutureLateResolvers);
661 } else {
662 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
663 GenerateError("Reference to an invalid definition: '" +DID.getName()+
664 "' of type '" + V->getType()->getDescription() + "'",
665 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000666 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000667 } else {
Reid Spencer93c40032007-03-19 18:40:50 +0000668 GenerateError("Reference to an invalid definition: #" +
669 itostr(DID.Num) + " of type '" +
670 V->getType()->getDescription() + "'",
671 PHI->second.second);
672 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000673 }
674 }
675 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000676 LateResolvers.clear();
677}
678
679// ResolveTypeTo - A brand new type was just declared. This means that (if
680// name is not null) things referencing Name can be resolved. Otherwise, things
681// refering to the number can be resolved. Do this now.
682//
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000683static void ResolveTypeTo(std::string *Name, const Type *ToTy) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000684 ValID D;
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000685 if (Name)
686 D = ValID::createLocalName(*Name);
687 else
688 D = ValID::createLocalID(CurModule.Types.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000689
Reid Spencer861d9d62006-11-28 07:29:44 +0000690 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattner58af2a12006-02-15 07:22:58 +0000691 CurModule.LateResolveTypes.find(D);
692 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencer861d9d62006-11-28 07:29:44 +0000693 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner58af2a12006-02-15 07:22:58 +0000694 CurModule.LateResolveTypes.erase(I);
695 }
696}
697
698// setValueName - Set the specified value to the name given. The name may be
699// null potentially, in which case this is a noop. The string passed in is
700// assumed to be a malloc'd string buffer, and is free'd by this function.
701//
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000702static void setValueName(Value *V, std::string *NameStr) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000703 if (!NameStr) return;
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000704 std::string Name(*NameStr); // Copy string
705 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000706
Reid Spencer41dff5e2007-01-26 08:05:27 +0000707 if (V->getType() == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000708 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000709 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000710 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000711
Reid Spencera9720f52007-02-05 17:04:00 +0000712 assert(inFunctionScope() && "Must be in function scope!");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000713 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
714 if (ST.lookup(Name)) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000715 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000716 V->getType()->getDescription() + "'");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000717 return;
718 }
719
720 // Set the name.
721 V->setName(Name);
Chris Lattner58af2a12006-02-15 07:22:58 +0000722}
723
724/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
725/// this is a declaration, otherwise it is a definition.
726static GlobalVariable *
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000727ParseGlobalVariable(std::string *NameStr,
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000728 GlobalValue::LinkageTypes Linkage,
729 GlobalValue::VisibilityTypes Visibility,
Chris Lattner58af2a12006-02-15 07:22:58 +0000730 bool isConstantGlobal, const Type *Ty,
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000731 Constant *Initializer, bool IsThreadLocal,
732 unsigned AddressSpace = 0) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000733 if (isa<FunctionType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000734 GenerateError("Cannot declare global vars of function type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000735 return 0;
736 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000737
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000738 const PointerType *PTy = PointerType::get(Ty, AddressSpace);
Chris Lattner58af2a12006-02-15 07:22:58 +0000739
740 std::string Name;
741 if (NameStr) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000742 Name = *NameStr; // Copy string
743 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000744 }
745
746 // See if this global value was forward referenced. If so, recycle the
747 // object.
748 ValID ID;
749 if (!Name.empty()) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000750 ID = ValID::createGlobalName(Name);
Chris Lattner58af2a12006-02-15 07:22:58 +0000751 } else {
Reid Spencer93c40032007-03-19 18:40:50 +0000752 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000753 }
754
755 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
756 // Move the global to the end of the list, from whereever it was
757 // previously inserted.
758 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
759 CurModule.CurrentModule->getGlobalList().remove(GV);
760 CurModule.CurrentModule->getGlobalList().push_back(GV);
761 GV->setInitializer(Initializer);
762 GV->setLinkage(Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000763 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000764 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000765 GV->setThreadLocal(IsThreadLocal);
Chris Lattner58af2a12006-02-15 07:22:58 +0000766 InsertValue(GV, CurModule.Values);
767 return GV;
768 }
769
Reid Spenceref9b9a72007-02-05 20:47:22 +0000770 // If this global has a name
Chris Lattner58af2a12006-02-15 07:22:58 +0000771 if (!Name.empty()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000772 // if the global we're parsing has an initializer (is a definition) and
773 // has external linkage.
774 if (Initializer && Linkage != GlobalValue::InternalLinkage)
775 // If there is already a global with external linkage with this name
776 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
777 // If we allow this GVar to get created, it will be renamed in the
778 // symbol table because it conflicts with an existing GVar. We can't
779 // allow redefinition of GVars whose linking indicates that their name
780 // must stay the same. Issue the error.
781 GenerateError("Redefinition of global variable named '" + Name +
782 "' of type '" + Ty->getDescription() + "'");
783 return 0;
784 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000785 }
786
787 // Otherwise there is no existing GV to use, create one now.
788 GlobalVariable *GV =
789 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000790 CurModule.CurrentModule, IsThreadLocal, AddressSpace);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000791 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000792 InsertValue(GV, CurModule.Values);
793 return GV;
794}
795
796// setTypeName - Set the specified type to the name given. The name may be
797// null potentially, in which case this is a noop. The string passed in is
798// assumed to be a malloc'd string buffer, and is freed by this function.
799//
800// This function returns true if the type has already been defined, but is
801// allowed to be redefined in the specified context. If the name is a new name
802// for the type plane, it is inserted and false is returned.
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000803static bool setTypeName(const Type *T, std::string *NameStr) {
Reid Spencera9720f52007-02-05 17:04:00 +0000804 assert(!inFunctionScope() && "Can't give types function-local names!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000805 if (NameStr == 0) return false;
806
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000807 std::string Name(*NameStr); // Copy string
808 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000809
810 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000811 if (T == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000812 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000813 return false;
814 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000815
816 // Set the type name, checking for conflicts as we do so.
817 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
818
819 if (AlreadyExists) { // Inserting a name that is already defined???
820 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencera9720f52007-02-05 17:04:00 +0000821 assert(Existing && "Conflict but no matching type?!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000822
823 // There is only one case where this is allowed: when we are refining an
824 // opaque type. In this case, Existing will be an opaque type.
825 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
826 // We ARE replacing an opaque type!
827 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
828 return true;
829 }
830
831 // Otherwise, this is an attempt to redefine a type. That's okay if
832 // the redefinition is identical to the original. This will be so if
833 // Existing and T point to the same Type object. In this one case we
834 // allow the equivalent redefinition.
835 if (Existing == T) return true; // Yes, it's equal.
836
837 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer63c34452007-01-05 21:51:07 +0000838 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000839 T->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +0000840 }
841
842 return false;
843}
844
845//===----------------------------------------------------------------------===//
846// Code for handling upreferences in type names...
847//
848
849// TypeContains - Returns true if Ty directly contains E in it.
850//
851static bool TypeContains(const Type *Ty, const Type *E) {
852 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
853 E) != Ty->subtype_end();
854}
855
856namespace {
857 struct UpRefRecord {
858 // NestingLevel - The number of nesting levels that need to be popped before
859 // this type is resolved.
860 unsigned NestingLevel;
861
862 // LastContainedTy - This is the type at the current binding level for the
863 // type. Every time we reduce the nesting level, this gets updated.
864 const Type *LastContainedTy;
865
866 // UpRefTy - This is the actual opaque type that the upreference is
867 // represented with.
868 OpaqueType *UpRefTy;
869
870 UpRefRecord(unsigned NL, OpaqueType *URTy)
871 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
872 };
873}
874
875// UpRefs - A list of the outstanding upreferences that need to be resolved.
876static std::vector<UpRefRecord> UpRefs;
877
878/// HandleUpRefs - Every time we finish a new layer of types, this function is
879/// called. It loops through the UpRefs vector, which is a list of the
880/// currently active types. For each type, if the up reference is contained in
881/// the newly completed type, we decrement the level count. When the level
882/// count reaches zero, the upreferenced type is the type that is passed in:
883/// thus we can complete the cycle.
884///
885static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner224f84f2006-08-18 17:34:45 +0000886 // If Ty isn't abstract, or if there are no up-references in it, then there is
887 // nothing to resolve here.
888 if (!ty->isAbstract() || UpRefs.empty()) return ty;
889
Chris Lattner58af2a12006-02-15 07:22:58 +0000890 PATypeHolder Ty(ty);
891 UR_OUT("Type '" << Ty->getDescription() <<
892 "' newly formed. Resolving upreferences.\n" <<
893 UpRefs.size() << " upreferences active!\n");
894
895 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
896 // to zero), we resolve them all together before we resolve them to Ty. At
897 // the end of the loop, if there is anything to resolve to Ty, it will be in
898 // this variable.
899 OpaqueType *TypeToResolve = 0;
900
901 for (unsigned i = 0; i != UpRefs.size(); ++i) {
902 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
903 << UpRefs[i].second->getDescription() << ") = "
904 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
905 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
906 // Decrement level of upreference
907 unsigned Level = --UpRefs[i].NestingLevel;
908 UpRefs[i].LastContainedTy = Ty;
909 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
910 if (Level == 0) { // Upreference should be resolved!
911 if (!TypeToResolve) {
912 TypeToResolve = UpRefs[i].UpRefTy;
913 } else {
914 UR_OUT(" * Resolving upreference for "
915 << UpRefs[i].second->getDescription() << "\n";
916 std::string OldName = UpRefs[i].UpRefTy->getDescription());
917 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
918 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
919 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
920 }
921 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
922 --i; // Do not skip the next element...
923 }
924 }
925 }
926
927 if (TypeToResolve) {
928 UR_OUT(" * Resolving upreference for "
929 << UpRefs[i].second->getDescription() << "\n";
930 std::string OldName = TypeToResolve->getDescription());
931 TypeToResolve->refineAbstractTypeTo(Ty);
932 }
933
934 return Ty;
935}
936
Chris Lattner58af2a12006-02-15 07:22:58 +0000937//===----------------------------------------------------------------------===//
938// RunVMAsmParser - Define an interface to this parser
939//===----------------------------------------------------------------------===//
940//
Reid Spencer14310612006-12-31 05:40:51 +0000941static Module* RunParser(Module * M);
942
Duncan Sandsdc024672007-11-27 13:23:08 +0000943Module *llvm::RunVMAsmParser(llvm::MemoryBuffer *MB) {
944 InitLLLexer(MB);
945 Module *M = RunParser(new Module(LLLgetFilename()));
946 FreeLexer();
947 return M;
Chris Lattner58af2a12006-02-15 07:22:58 +0000948}
949
950%}
951
952%union {
953 llvm::Module *ModuleVal;
954 llvm::Function *FunctionVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000955 llvm::BasicBlock *BasicBlockVal;
956 llvm::TerminatorInst *TermInstVal;
957 llvm::Instruction *InstVal;
Reid Spencera132e042006-12-03 05:46:11 +0000958 llvm::Constant *ConstVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000959
Reid Spencera132e042006-12-03 05:46:11 +0000960 const llvm::Type *PrimType;
Reid Spencer14310612006-12-31 05:40:51 +0000961 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencera132e042006-12-03 05:46:11 +0000962 llvm::PATypeHolder *TypeVal;
963 llvm::Value *ValueVal;
Reid Spencera132e042006-12-03 05:46:11 +0000964 std::vector<llvm::Value*> *ValueList;
Reid Spencer14310612006-12-31 05:40:51 +0000965 llvm::ArgListType *ArgList;
966 llvm::TypeWithAttrs TypeWithAttrs;
967 llvm::TypeWithAttrsList *TypeWithAttrsList;
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000968 llvm::ParamList *ParamList;
Reid Spencer14310612006-12-31 05:40:51 +0000969
Chris Lattner58af2a12006-02-15 07:22:58 +0000970 // Represent the RHS of PHI node
Reid Spencera132e042006-12-03 05:46:11 +0000971 std::list<std::pair<llvm::Value*,
972 llvm::BasicBlock*> > *PHIList;
Chris Lattner58af2a12006-02-15 07:22:58 +0000973 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencera132e042006-12-03 05:46:11 +0000974 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner58af2a12006-02-15 07:22:58 +0000975
976 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000977 llvm::GlobalValue::VisibilityTypes Visibility;
Dale Johannesen222ebf72008-02-19 21:40:51 +0000978 llvm::ParameterAttributes ParamAttrs;
Reid Spencer38c91a92007-02-28 02:24:54 +0000979 llvm::APInt *APIntVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000980 int64_t SInt64Val;
981 uint64_t UInt64Val;
982 int SIntVal;
983 unsigned UIntVal;
Dale Johannesen43421b32007-09-06 18:13:44 +0000984 llvm::APFloat *FPVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000985 bool BoolVal;
986
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000987 std::string *StrVal; // This memory must be deleted
988 llvm::ValID ValIDVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000989
Reid Spencera132e042006-12-03 05:46:11 +0000990 llvm::Instruction::BinaryOps BinaryOpVal;
991 llvm::Instruction::TermOps TermOpVal;
992 llvm::Instruction::MemoryOps MemOpVal;
993 llvm::Instruction::CastOps CastOpVal;
994 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencera132e042006-12-03 05:46:11 +0000995 llvm::ICmpInst::Predicate IPredicate;
996 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner58af2a12006-02-15 07:22:58 +0000997}
998
Reid Spencer14310612006-12-31 05:40:51 +0000999%type <ModuleVal> Module
Chris Lattner58af2a12006-02-15 07:22:58 +00001000%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
1001%type <BasicBlockVal> BasicBlock InstructionList
1002%type <TermInstVal> BBTerminatorInst
1003%type <InstVal> Inst InstVal MemoryInst
Anton Korobeynikov38e09802007-04-28 13:48:45 +00001004%type <ConstVal> ConstVal ConstExpr AliaseeRef
Chris Lattner58af2a12006-02-15 07:22:58 +00001005%type <ConstVector> ConstVector
1006%type <ArgList> ArgList ArgListH
Chris Lattner58af2a12006-02-15 07:22:58 +00001007%type <PHIList> PHIList
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001008%type <ParamList> ParamList // For call param lists & GEP indices
Reid Spencer14310612006-12-31 05:40:51 +00001009%type <ValueList> IndexList // For GEP indices
1010%type <TypeList> TypeListI
1011%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer218ded22007-01-05 17:07:23 +00001012%type <TypeWithAttrs> ArgType
Chris Lattner58af2a12006-02-15 07:22:58 +00001013%type <JumpTable> JumpTable
1014%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001015%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattner58af2a12006-02-15 07:22:58 +00001016%type <BoolVal> OptVolatile // 'volatile' or not
1017%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1018%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencer14310612006-12-31 05:40:51 +00001019%type <Linkage> GVInternalLinkage GVExternalLinkage
1020%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001021%type <Linkage> AliasLinkage
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001022%type <Visibility> GVVisibilityStyle
Chris Lattner58af2a12006-02-15 07:22:58 +00001023
1024// ValueRef - Unresolved reference to a definition or BB
1025%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1026%type <ValueVal> ResolvedVal // <type> <valref> pair
Devang Patel7990dc72008-02-20 22:40:23 +00001027%type <ValueList> ReturnedVal
Chris Lattner58af2a12006-02-15 07:22:58 +00001028// Tokens and types for handling constant integer values
1029//
1030// ESINT64VAL - A negative number within long long range
1031%token <SInt64Val> ESINT64VAL
1032
1033// EUINT64VAL - A positive number within uns. long long range
1034%token <UInt64Val> EUINT64VAL
Chris Lattner58af2a12006-02-15 07:22:58 +00001035
Reid Spencer38c91a92007-02-28 02:24:54 +00001036// ESAPINTVAL - A negative number with arbitrary precision
1037%token <APIntVal> ESAPINTVAL
1038
1039// EUAPINTVAL - A positive number with arbitrary precision
1040%token <APIntVal> EUAPINTVAL
1041
Reid Spencer41dff5e2007-01-26 08:05:27 +00001042%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattner58af2a12006-02-15 07:22:58 +00001043%token <FPVal> FPVAL // Float or Double constant
1044
1045// Built in types...
Reid Spencer218ded22007-01-05 17:07:23 +00001046%type <TypeVal> Types ResultTypes
Reid Spencer14310612006-12-31 05:40:51 +00001047%type <PrimType> IntType FPType PrimType // Classifications
Reid Spencer6f407902007-01-13 05:00:46 +00001048%token <PrimType> VOID INTTYPE
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001049%token <PrimType> FLOAT DOUBLE X86_FP80 FP128 PPC_FP128 LABEL
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001050%token TYPE
Chris Lattner58af2a12006-02-15 07:22:58 +00001051
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001052
Reid Spencered951ea2007-05-19 07:22:10 +00001053%token<StrVal> LOCALVAR GLOBALVAR LABELSTR
1054%token<StrVal> STRINGCONSTANT ATSTRINGCONSTANT PCTSTRINGCONSTANT
Reid Spencer41dff5e2007-01-26 08:05:27 +00001055%type <StrVal> LocalName OptLocalName OptLocalAssign
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001056%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001057%type <StrVal> OptSection SectionString OptGC
Chris Lattner58af2a12006-02-15 07:22:58 +00001058
Christopher Lambbf3348d2007-12-12 08:45:45 +00001059%type <UIntVal> OptAlign OptCAlign OptAddrSpace
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001060
Reid Spencer3d6b71e2007-04-09 01:56:05 +00001061%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001062%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
Reid Spencer14310612006-12-31 05:40:51 +00001063%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001064%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001065%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN ADDRSPACE
Chris Lattner58af2a12006-02-15 07:22:58 +00001066%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001067%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattner1ae022f2006-10-22 06:08:13 +00001068%token DATALAYOUT
Chris Lattner58af2a12006-02-15 07:22:58 +00001069%type <UIntVal> OptCallingConv
Reid Spencer218ded22007-01-05 17:07:23 +00001070%type <ParamAttrs> OptParamAttrs ParamAttr
1071%type <ParamAttrs> OptFuncAttrs FuncAttr
Chris Lattner58af2a12006-02-15 07:22:58 +00001072
1073// Basic Block Terminating Operators
1074%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
1075
1076// Binary Operators
Reid Spencere4d87aa2006-12-23 06:05:41 +00001077%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencer3ed469c2006-11-02 20:25:50 +00001078%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer832254e2007-02-02 02:16:23 +00001079%token <BinaryOpVal> SHL LSHR ASHR
1080
Reid Spencera132e042006-12-03 05:46:11 +00001081%token <OtherOpVal> ICMP FCMP
Reid Spencera132e042006-12-03 05:46:11 +00001082%type <IPredicate> IPredicates
Reid Spencera132e042006-12-03 05:46:11 +00001083%type <FPredicate> FPredicates
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001084%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1085%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner58af2a12006-02-15 07:22:58 +00001086
1087// Memory Instructions
1088%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1089
Reid Spencer3da59db2006-11-27 01:05:10 +00001090// Cast Operators
1091%type <CastOpVal> CastOps
1092%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1093%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1094
Chris Lattner58af2a12006-02-15 07:22:58 +00001095// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001096%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattnerd5efe842006-04-08 01:18:56 +00001097%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Devang Patel5a970972008-02-19 22:27:01 +00001098%token <OtherOpVal> GETRESULT
Chris Lattner58af2a12006-02-15 07:22:58 +00001099
Reid Spencer218ded22007-01-05 17:07:23 +00001100// Function Attributes
Reid Spencerb8f85052007-07-31 03:50:36 +00001101%token SIGNEXT ZEROEXT NORETURN INREG SRET NOUNWIND NOALIAS BYVAL NEST
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001102%token READNONE READONLY GC
Chris Lattner58af2a12006-02-15 07:22:58 +00001103
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001104// Visibility Styles
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001105%token DEFAULT HIDDEN PROTECTED
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001106
Chris Lattner58af2a12006-02-15 07:22:58 +00001107%start Module
1108%%
1109
Chris Lattner58af2a12006-02-15 07:22:58 +00001110
Chris Lattner58af2a12006-02-15 07:22:58 +00001111// Operations that are notably excluded from this list include:
1112// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1113//
Reid Spencer3ed469c2006-11-02 20:25:50 +00001114ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer832254e2007-02-02 02:16:23 +00001115LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Reid Spencer3da59db2006-11-27 01:05:10 +00001116CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1117 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer832254e2007-02-02 02:16:23 +00001118
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001119IPredicates
Reid Spencer4012e832006-12-04 05:24:24 +00001120 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001121 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1122 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1123 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1124 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1125 ;
1126
1127FPredicates
1128 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1129 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1130 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1131 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1132 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1133 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1134 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1135 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1136 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1137 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001138
1139// These are some types that allow classification if we only want a particular
1140// thing... for example, only a signed, unsigned, or integral type.
Reid Spencera54b7cb2007-01-12 07:05:14 +00001141IntType : INTTYPE;
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001142FPType : FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80;
Chris Lattner58af2a12006-02-15 07:22:58 +00001143
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001144LocalName : LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001145OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1146
Christopher Lambbf3348d2007-12-12 08:45:45 +00001147OptAddrSpace : ADDRSPACE '(' EUINT64VAL ')' { $$=$3; }
1148 | /*empty*/ { $$=0; };
1149
Reid Spencer41dff5e2007-01-26 08:05:27 +00001150/// OptLocalAssign - Value producing statements have an optional assignment
1151/// component.
1152OptLocalAssign : LocalName '=' {
1153 $$ = $1;
1154 CHECK_FOR_ERROR
1155 }
1156 | /*empty*/ {
1157 $$ = 0;
1158 CHECK_FOR_ERROR
1159 };
1160
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001161GlobalName : GLOBALVAR | ATSTRINGCONSTANT ;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001162
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001163OptGlobalAssign : GlobalAssign
Chris Lattner58af2a12006-02-15 07:22:58 +00001164 | /*empty*/ {
1165 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001166 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001167 };
1168
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001169GlobalAssign : GlobalName '=' {
1170 $$ = $1;
1171 CHECK_FOR_ERROR
Chris Lattner6cdc6822007-04-26 05:31:05 +00001172 };
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001173
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001174GVInternalLinkage
1175 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1176 | WEAK { $$ = GlobalValue::WeakLinkage; }
1177 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1178 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1179 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1180 ;
1181
1182GVExternalLinkage
1183 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1184 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1185 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1186 ;
1187
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001188GVVisibilityStyle
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001189 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1190 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1191 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1192 | PROTECTED { $$ = GlobalValue::ProtectedVisibility; }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001193 ;
1194
Reid Spencer14310612006-12-31 05:40:51 +00001195FunctionDeclareLinkage
1196 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1197 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1198 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001199 ;
1200
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001201FunctionDefineLinkage
Reid Spencer14310612006-12-31 05:40:51 +00001202 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1203 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001204 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1205 | WEAK { $$ = GlobalValue::WeakLinkage; }
1206 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001207 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001208
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001209AliasLinkage
1210 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1211 | WEAK { $$ = GlobalValue::WeakLinkage; }
1212 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1213 ;
1214
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001215OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1216 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001217 FASTCC_TOK { $$ = CallingConv::Fast; } |
1218 COLDCC_TOK { $$ = CallingConv::Cold; } |
1219 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1220 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1221 CC_TOK EUINT64VAL {
Chris Lattner58af2a12006-02-15 07:22:58 +00001222 if ((unsigned)$2 != $2)
Reid Spencerb5334b02007-02-05 10:18:06 +00001223 GEN_ERROR("Calling conv too large");
Chris Lattner58af2a12006-02-15 07:22:58 +00001224 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001225 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001226 };
1227
Reid Spencerb8f85052007-07-31 03:50:36 +00001228ParamAttr : ZEROEXT { $$ = ParamAttr::ZExt; }
1229 | ZEXT { $$ = ParamAttr::ZExt; }
1230 | SIGNEXT { $$ = ParamAttr::SExt; }
Chris Lattnerce5f24e2007-07-05 17:26:49 +00001231 | SEXT { $$ = ParamAttr::SExt; }
1232 | INREG { $$ = ParamAttr::InReg; }
1233 | SRET { $$ = ParamAttr::StructRet; }
1234 | NOALIAS { $$ = ParamAttr::NoAlias; }
Reid Spencerb8f85052007-07-31 03:50:36 +00001235 | BYVAL { $$ = ParamAttr::ByVal; }
1236 | NEST { $$ = ParamAttr::Nest; }
Dale Johannesendc6c0f12008-02-22 17:50:51 +00001237 | ALIGN EUINT64VAL { $$ =
1238 ParamAttr::constructAlignmentFromInt($2); }
Reid Spencer14310612006-12-31 05:40:51 +00001239 ;
1240
Reid Spencer18da0722007-04-11 02:44:20 +00001241OptParamAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer218ded22007-01-05 17:07:23 +00001242 | OptParamAttrs ParamAttr {
Reid Spencer7b5d4662007-04-09 06:16:21 +00001243 $$ = $1 | $2;
Reid Spencer14310612006-12-31 05:40:51 +00001244 }
1245 ;
1246
Reid Spencer18da0722007-04-11 02:44:20 +00001247FuncAttr : NORETURN { $$ = ParamAttr::NoReturn; }
1248 | NOUNWIND { $$ = ParamAttr::NoUnwind; }
Reid Spencerb8f85052007-07-31 03:50:36 +00001249 | ZEROEXT { $$ = ParamAttr::ZExt; }
1250 | SIGNEXT { $$ = ParamAttr::SExt; }
Duncan Sandsdc024672007-11-27 13:23:08 +00001251 | READNONE { $$ = ParamAttr::ReadNone; }
1252 | READONLY { $$ = ParamAttr::ReadOnly; }
Reid Spencer218ded22007-01-05 17:07:23 +00001253 ;
1254
Reid Spencer18da0722007-04-11 02:44:20 +00001255OptFuncAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer218ded22007-01-05 17:07:23 +00001256 | OptFuncAttrs FuncAttr {
Reid Spencer7b5d4662007-04-09 06:16:21 +00001257 $$ = $1 | $2;
Reid Spencer218ded22007-01-05 17:07:23 +00001258 }
Reid Spencer14310612006-12-31 05:40:51 +00001259 ;
1260
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001261OptGC : /* empty */ { $$ = 0; }
1262 | GC STRINGCONSTANT {
1263 $$ = $2;
1264 }
1265 ;
1266
Chris Lattner58af2a12006-02-15 07:22:58 +00001267// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1268// a comma before it.
1269OptAlign : /*empty*/ { $$ = 0; } |
1270 ALIGN EUINT64VAL {
1271 $$ = $2;
1272 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001273 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001274 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001275};
1276OptCAlign : /*empty*/ { $$ = 0; } |
1277 ',' ALIGN EUINT64VAL {
1278 $$ = $3;
1279 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001280 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001281 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001282};
1283
1284
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001285
Chris Lattner58af2a12006-02-15 07:22:58 +00001286SectionString : SECTION STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001287 for (unsigned i = 0, e = $2->length(); i != e; ++i)
1288 if ((*$2)[i] == '"' || (*$2)[i] == '\\')
Reid Spencerb5334b02007-02-05 10:18:06 +00001289 GEN_ERROR("Invalid character in section name");
Chris Lattner58af2a12006-02-15 07:22:58 +00001290 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001291 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001292};
1293
1294OptSection : /*empty*/ { $$ = 0; } |
1295 SectionString { $$ = $1; };
1296
1297// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1298// is set to be the global we are processing.
1299//
1300GlobalVarAttributes : /* empty */ {} |
1301 ',' GlobalVarAttribute GlobalVarAttributes {};
1302GlobalVarAttribute : SectionString {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001303 CurGV->setSection(*$1);
1304 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001305 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001306 }
1307 | ALIGN EUINT64VAL {
1308 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001309 GEN_ERROR("Alignment must be a power of two");
Chris Lattner58af2a12006-02-15 07:22:58 +00001310 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001311 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001312 };
1313
1314//===----------------------------------------------------------------------===//
1315// Types includes all predefined types... except void, because it can only be
Reid Spencer14310612006-12-31 05:40:51 +00001316// used in specific contexts (function returning void for example).
Chris Lattner58af2a12006-02-15 07:22:58 +00001317
1318// Derived types are added later...
1319//
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001320PrimType : INTTYPE | FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80 | LABEL ;
Reid Spencer14310612006-12-31 05:40:51 +00001321
1322Types
1323 : OPAQUE {
Reid Spencera132e042006-12-03 05:46:11 +00001324 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001325 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001326 }
1327 | PrimType {
Reid Spencera132e042006-12-03 05:46:11 +00001328 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001329 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00001330 }
Christopher Lambbf3348d2007-12-12 08:45:45 +00001331 | Types OptAddrSpace '*' { // Pointer type?
Reid Spencer14310612006-12-31 05:40:51 +00001332 if (*$1 == Type::LabelTy)
1333 GEN_ERROR("Cannot form a pointer to a basic block");
Christopher Lambbf3348d2007-12-12 08:45:45 +00001334 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1, $2)));
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001335 delete $1;
1336 CHECK_FOR_ERROR
1337 }
Reid Spencer14310612006-12-31 05:40:51 +00001338 | SymbolicValueRef { // Named types are also simple types...
1339 const Type* tmp = getTypeVal($1);
1340 CHECK_FOR_ERROR
1341 $$ = new PATypeHolder(tmp);
1342 }
1343 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerb5334b02007-02-05 10:18:06 +00001344 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattner58af2a12006-02-15 07:22:58 +00001345 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1346 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencera132e042006-12-03 05:46:11 +00001347 $$ = new PATypeHolder(OT);
Chris Lattner58af2a12006-02-15 07:22:58 +00001348 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001349 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001350 }
Reid Spencer218ded22007-01-05 17:07:23 +00001351 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001352 // Allow but ignore attributes on function types; this permits auto-upgrade.
1353 // FIXME: remove in LLVM 3.0.
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001354 const Type* RetTy = *$1;
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001355 if (!(RetTy->isFirstClassType() || RetTy == Type::VoidTy ||
1356 isa<OpaqueType>(RetTy)))
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001357 GEN_ERROR("LLVM Functions cannot return aggregates");
1358
Chris Lattner58af2a12006-02-15 07:22:58 +00001359 std::vector<const Type*> Params;
Reid Spencer7b5d4662007-04-09 06:16:21 +00001360 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001361 for (; I != E; ++I ) {
Reid Spencer66728ef2007-03-20 01:13:36 +00001362 const Type *Ty = I->Ty->get();
Reid Spencer66728ef2007-03-20 01:13:36 +00001363 Params.push_back(Ty);
Reid Spencer14310612006-12-31 05:40:51 +00001364 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001365
Chris Lattner58af2a12006-02-15 07:22:58 +00001366 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1367 if (isVarArg) Params.pop_back();
1368
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001369 for (unsigned i = 0; i != Params.size(); ++i)
1370 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1371 GEN_ERROR("Function arguments must be value types!");
1372
1373 CHECK_FOR_ERROR
1374
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001375 FunctionType *FT = FunctionType::get(RetTy, Params, isVarArg);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001376 delete $3; // Delete the argument list
Reid Spencer14310612006-12-31 05:40:51 +00001377 delete $1; // Delete the return type handle
1378 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer61c83e02006-08-18 08:43:06 +00001379 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001380 }
Reid Spencer218ded22007-01-05 17:07:23 +00001381 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001382 // Allow but ignore attributes on function types; this permits auto-upgrade.
1383 // FIXME: remove in LLVM 3.0.
Reid Spencer14310612006-12-31 05:40:51 +00001384 std::vector<const Type*> Params;
Reid Spencer7b5d4662007-04-09 06:16:21 +00001385 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001386 for ( ; I != E; ++I ) {
Reid Spencer66728ef2007-03-20 01:13:36 +00001387 const Type* Ty = I->Ty->get();
Reid Spencer66728ef2007-03-20 01:13:36 +00001388 Params.push_back(Ty);
Reid Spencer14310612006-12-31 05:40:51 +00001389 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001390
Reid Spencer14310612006-12-31 05:40:51 +00001391 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1392 if (isVarArg) Params.pop_back();
1393
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001394 for (unsigned i = 0; i != Params.size(); ++i)
1395 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1396 GEN_ERROR("Function arguments must be value types!");
1397
1398 CHECK_FOR_ERROR
1399
Duncan Sandsdc024672007-11-27 13:23:08 +00001400 FunctionType *FT = FunctionType::get($1, Params, isVarArg);
Reid Spencer218ded22007-01-05 17:07:23 +00001401 delete $3; // Delete the argument list
Reid Spencer14310612006-12-31 05:40:51 +00001402 $$ = new PATypeHolder(HandleUpRefs(FT));
1403 CHECK_FOR_ERROR
1404 }
1405
1406 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Reid Spencera132e042006-12-03 05:46:11 +00001407 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1408 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001409 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001410 }
Chris Lattner32980692007-02-19 07:44:24 +00001411 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencera132e042006-12-03 05:46:11 +00001412 const llvm::Type* ElemTy = $4->get();
1413 if ((unsigned)$2 != $2)
1414 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner42a75512007-01-15 02:27:26 +00001415 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencer9d6565a2007-02-15 02:26:10 +00001416 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001417 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencera132e042006-12-03 05:46:11 +00001418 delete $4;
1419 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001420 }
1421 | '{' TypeListI '}' { // Structure type?
1422 std::vector<const Type*> Elements;
Reid Spencera132e042006-12-03 05:46:11 +00001423 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattner58af2a12006-02-15 07:22:58 +00001424 E = $2->end(); I != E; ++I)
Reid Spencera132e042006-12-03 05:46:11 +00001425 Elements.push_back(*I);
Chris Lattner58af2a12006-02-15 07:22:58 +00001426
Reid Spencera132e042006-12-03 05:46:11 +00001427 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattner58af2a12006-02-15 07:22:58 +00001428 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001429 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001430 }
1431 | '{' '}' { // Empty structure type?
Reid Spencera132e042006-12-03 05:46:11 +00001432 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001433 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001434 }
Andrew Lenharth6353e052006-12-08 18:07:09 +00001435 | '<' '{' TypeListI '}' '>' {
1436 std::vector<const Type*> Elements;
1437 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1438 E = $3->end(); I != E; ++I)
1439 Elements.push_back(*I);
1440
1441 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1442 delete $3;
1443 CHECK_FOR_ERROR
1444 }
1445 | '<' '{' '}' '>' { // Empty structure type?
1446 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1447 CHECK_FOR_ERROR
1448 }
Reid Spencer14310612006-12-31 05:40:51 +00001449 ;
1450
1451ArgType
Duncan Sandsdc024672007-11-27 13:23:08 +00001452 : Types OptParamAttrs {
1453 // Allow but ignore attributes on function types; this permits auto-upgrade.
1454 // FIXME: remove in LLVM 3.0.
Reid Spencer14310612006-12-31 05:40:51 +00001455 $$.Ty = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00001456 $$.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00001457 }
1458 ;
1459
Reid Spencer218ded22007-01-05 17:07:23 +00001460ResultTypes
1461 : Types {
Reid Spencer14310612006-12-31 05:40:51 +00001462 if (!UpRefs.empty())
1463 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Devang Patel7990dc72008-02-20 22:40:23 +00001464 if (!(*$1)->isFirstClassType() && (*$1)->getTypeID() != Type::StructTyID)
Reid Spencerb5334b02007-02-05 10:18:06 +00001465 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer218ded22007-01-05 17:07:23 +00001466 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00001467 }
Reid Spencer218ded22007-01-05 17:07:23 +00001468 | VOID {
1469 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencer14310612006-12-31 05:40:51 +00001470 }
1471 ;
1472
1473ArgTypeList : ArgType {
1474 $$ = new TypeWithAttrsList();
1475 $$->push_back($1);
1476 CHECK_FOR_ERROR
1477 }
1478 | ArgTypeList ',' ArgType {
1479 ($$=$1)->push_back($3);
1480 CHECK_FOR_ERROR
1481 }
1482 ;
1483
1484ArgTypeListI
1485 : ArgTypeList
1486 | ArgTypeList ',' DOTDOTDOT {
1487 $$=$1;
Reid Spencer18da0722007-04-11 02:44:20 +00001488 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00001489 TWA.Ty = new PATypeHolder(Type::VoidTy);
1490 $$->push_back(TWA);
1491 CHECK_FOR_ERROR
1492 }
1493 | DOTDOTDOT {
1494 $$ = new TypeWithAttrsList;
Reid Spencer18da0722007-04-11 02:44:20 +00001495 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00001496 TWA.Ty = new PATypeHolder(Type::VoidTy);
1497 $$->push_back(TWA);
1498 CHECK_FOR_ERROR
1499 }
1500 | /*empty*/ {
1501 $$ = new TypeWithAttrsList();
Reid Spencer61c83e02006-08-18 08:43:06 +00001502 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001503 };
1504
1505// TypeList - Used for struct declarations and as a basis for function type
1506// declaration type lists
1507//
Reid Spencer14310612006-12-31 05:40:51 +00001508TypeListI : Types {
Reid Spencera132e042006-12-03 05:46:11 +00001509 $$ = new std::list<PATypeHolder>();
Reid Spencer66728ef2007-03-20 01:13:36 +00001510 $$->push_back(*$1);
1511 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001512 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001513 }
Reid Spencer14310612006-12-31 05:40:51 +00001514 | TypeListI ',' Types {
Reid Spencer66728ef2007-03-20 01:13:36 +00001515 ($$=$1)->push_back(*$3);
1516 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001517 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001518 };
1519
Chris Lattner58af2a12006-02-15 07:22:58 +00001520// ConstVal - The various declarations that go into the constant pool. This
1521// production is used ONLY to represent constants that show up AFTER a 'const',
1522// 'constant' or 'global' token at global scope. Constants that can be inlined
1523// into other expressions (such as integers and constexprs) are handled by the
1524// ResolvedVal, ValueRef and ConstValueRef productions.
1525//
1526ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001527 if (!UpRefs.empty())
1528 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001529 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001530 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001531 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001532 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001533 const Type *ETy = ATy->getElementType();
1534 int NumElements = ATy->getNumElements();
1535
1536 // Verify that we have the correct size...
1537 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001538 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001539 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerb5334b02007-02-05 10:18:06 +00001540 itostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001541
1542 // Verify all elements are correct type!
1543 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001544 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001545 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001546 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001547 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001548 }
1549
Reid Spencera132e042006-12-03 05:46:11 +00001550 $$ = ConstantArray::get(ATy, *$3);
1551 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001552 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001553 }
1554 | Types '[' ']' {
Reid Spencer14310612006-12-31 05:40:51 +00001555 if (!UpRefs.empty())
1556 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001557 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001558 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001559 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001560 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001561
1562 int NumElements = ATy->getNumElements();
1563 if (NumElements != -1 && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001564 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencerb5334b02007-02-05 10:18:06 +00001565 " arguments, but has size of " + itostr(NumElements) +"");
Reid Spencera132e042006-12-03 05:46:11 +00001566 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1567 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001568 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001569 }
1570 | Types 'c' STRINGCONSTANT {
Reid Spencer14310612006-12-31 05:40:51 +00001571 if (!UpRefs.empty())
1572 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001573 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001574 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001575 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001576 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001577
1578 int NumElements = ATy->getNumElements();
1579 const Type *ETy = ATy->getElementType();
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001580 if (NumElements != -1 && NumElements != int($3->length()))
Reid Spencer61c83e02006-08-18 08:43:06 +00001581 GEN_ERROR("Can't build string constant of size " +
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001582 itostr((int)($3->length())) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001583 " when array has size " + itostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001584 std::vector<Constant*> Vals;
Reid Spencer14310612006-12-31 05:40:51 +00001585 if (ETy == Type::Int8Ty) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001586 for (unsigned i = 0; i < $3->length(); ++i)
1587 Vals.push_back(ConstantInt::get(ETy, (*$3)[i]));
Chris Lattner58af2a12006-02-15 07:22:58 +00001588 } else {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001589 delete $3;
Reid Spencerb5334b02007-02-05 10:18:06 +00001590 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner58af2a12006-02-15 07:22:58 +00001591 }
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001592 delete $3;
Reid Spencera132e042006-12-03 05:46:11 +00001593 $$ = ConstantArray::get(ATy, Vals);
1594 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001595 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001596 }
1597 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001598 if (!UpRefs.empty())
1599 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00001600 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001601 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001602 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001603 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001604 const Type *ETy = PTy->getElementType();
1605 int NumElements = PTy->getNumElements();
1606
1607 // Verify that we have the correct size...
1608 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001609 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001610 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerb5334b02007-02-05 10:18:06 +00001611 itostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001612
1613 // Verify all elements are correct type!
1614 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001615 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001616 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001617 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001618 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001619 }
1620
Reid Spencer9d6565a2007-02-15 02:26:10 +00001621 $$ = ConstantVector::get(PTy, *$3);
Reid Spencera132e042006-12-03 05:46:11 +00001622 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001623 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001624 }
1625 | Types '{' ConstVector '}' {
Reid Spencera132e042006-12-03 05:46:11 +00001626 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001627 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001628 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001629 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001630
1631 if ($3->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001632 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001633
1634 // Check to ensure that constants are compatible with the type initializer!
1635 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencera132e042006-12-03 05:46:11 +00001636 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001637 GEN_ERROR("Expected type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001638 STy->getElementType(i)->getDescription() +
1639 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001640 " of structure initializer");
Chris Lattner58af2a12006-02-15 07:22:58 +00001641
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001642 // Check to ensure that Type is not packed
1643 if (STy->isPacked())
Chris Lattner6cdc6822007-04-26 05:31:05 +00001644 GEN_ERROR("Unpacked Initializer to vector type '" +
1645 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001646
Reid Spencera132e042006-12-03 05:46:11 +00001647 $$ = ConstantStruct::get(STy, *$3);
1648 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001649 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001650 }
1651 | Types '{' '}' {
Reid Spencer14310612006-12-31 05:40:51 +00001652 if (!UpRefs.empty())
1653 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001654 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001655 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001656 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001657 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001658
1659 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001660 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001661
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001662 // Check to ensure that Type is not packed
1663 if (STy->isPacked())
Chris Lattner6cdc6822007-04-26 05:31:05 +00001664 GEN_ERROR("Unpacked Initializer to vector type '" +
1665 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001666
1667 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1668 delete $1;
1669 CHECK_FOR_ERROR
1670 }
1671 | Types '<' '{' ConstVector '}' '>' {
1672 const StructType *STy = dyn_cast<StructType>($1->get());
1673 if (STy == 0)
1674 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001675 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001676
1677 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001678 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001679
1680 // Check to ensure that constants are compatible with the type initializer!
1681 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1682 if ((*$4)[i]->getType() != STy->getElementType(i))
1683 GEN_ERROR("Expected type '" +
1684 STy->getElementType(i)->getDescription() +
1685 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001686 " of structure initializer");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001687
1688 // Check to ensure that Type is packed
1689 if (!STy->isPacked())
Chris Lattner32980692007-02-19 07:44:24 +00001690 GEN_ERROR("Vector initializer to non-vector type '" +
1691 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001692
1693 $$ = ConstantStruct::get(STy, *$4);
1694 delete $1; delete $4;
1695 CHECK_FOR_ERROR
1696 }
1697 | Types '<' '{' '}' '>' {
1698 if (!UpRefs.empty())
1699 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1700 const StructType *STy = dyn_cast<StructType>($1->get());
1701 if (STy == 0)
1702 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001703 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001704
1705 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001706 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001707
1708 // Check to ensure that Type is packed
1709 if (!STy->isPacked())
Chris Lattner32980692007-02-19 07:44:24 +00001710 GEN_ERROR("Vector initializer to non-vector type '" +
1711 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001712
Reid Spencera132e042006-12-03 05:46:11 +00001713 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1714 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001715 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001716 }
1717 | Types NULL_TOK {
Reid Spencer14310612006-12-31 05:40:51 +00001718 if (!UpRefs.empty())
1719 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001720 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001721 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001722 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001723 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001724
Reid Spencera132e042006-12-03 05:46:11 +00001725 $$ = ConstantPointerNull::get(PTy);
1726 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001727 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001728 }
1729 | Types UNDEF {
Reid Spencer14310612006-12-31 05:40:51 +00001730 if (!UpRefs.empty())
1731 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001732 $$ = UndefValue::get($1->get());
1733 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001734 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001735 }
1736 | Types SymbolicValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00001737 if (!UpRefs.empty())
1738 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001739 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001740 if (Ty == 0)
Devang Patel5a970972008-02-19 22:27:01 +00001741 GEN_ERROR("Global const reference must be a pointer type " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001742
1743 // ConstExprs can exist in the body of a function, thus creating
1744 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencer93c40032007-03-19 18:40:50 +00001745 // the context of a function, getExistingVal will search the functions
Chris Lattner58af2a12006-02-15 07:22:58 +00001746 // symbol table instead of the module symbol table for the global symbol,
1747 // which throws things all off. To get around this, we just tell
Reid Spencer93c40032007-03-19 18:40:50 +00001748 // getExistingVal that we are at global scope here.
Chris Lattner58af2a12006-02-15 07:22:58 +00001749 //
1750 Function *SavedCurFn = CurFun.CurrentFunction;
1751 CurFun.CurrentFunction = 0;
1752
Reid Spencer93c40032007-03-19 18:40:50 +00001753 Value *V = getExistingVal(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001754 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001755
1756 CurFun.CurrentFunction = SavedCurFn;
1757
1758 // If this is an initializer for a constant pointer, which is referencing a
1759 // (currently) undefined variable, create a stub now that shall be replaced
1760 // in the future with the right type of variable.
1761 //
1762 if (V == 0) {
Reid Spencera9720f52007-02-05 17:04:00 +00001763 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001764 const PointerType *PT = cast<PointerType>(Ty);
1765
1766 // First check to see if the forward references value is already created!
1767 PerModuleInfo::GlobalRefsType::iterator I =
1768 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1769
1770 if (I != CurModule.GlobalRefs.end()) {
1771 V = I->second; // Placeholder already exists, use it...
1772 $2.destroy();
1773 } else {
1774 std::string Name;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001775 if ($2.Type == ValID::GlobalName)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001776 Name = $2.getName();
Reid Spencer41dff5e2007-01-26 08:05:27 +00001777 else if ($2.Type != ValID::GlobalID)
1778 GEN_ERROR("Invalid reference to global");
Chris Lattner58af2a12006-02-15 07:22:58 +00001779
1780 // Create the forward referenced global.
1781 GlobalValue *GV;
1782 if (const FunctionType *FTy =
1783 dyn_cast<FunctionType>(PT->getElementType())) {
Chris Lattner6cdc6822007-04-26 05:31:05 +00001784 GV = new Function(FTy, GlobalValue::ExternalWeakLinkage, Name,
Chris Lattner58af2a12006-02-15 07:22:58 +00001785 CurModule.CurrentModule);
1786 } else {
1787 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattner6cdc6822007-04-26 05:31:05 +00001788 GlobalValue::ExternalWeakLinkage, 0,
Chris Lattner58af2a12006-02-15 07:22:58 +00001789 Name, CurModule.CurrentModule);
1790 }
1791
1792 // Keep track of the fact that we have a forward ref to recycle it
1793 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1794 V = GV;
1795 }
1796 }
1797
Reid Spencera132e042006-12-03 05:46:11 +00001798 $$ = cast<GlobalValue>(V);
1799 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001800 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001801 }
1802 | Types ConstExpr {
Reid Spencer14310612006-12-31 05:40:51 +00001803 if (!UpRefs.empty())
1804 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001805 if ($1->get() != $2->getType())
Reid Spencere68853b2007-01-04 00:06:14 +00001806 GEN_ERROR("Mismatched types for constant expression: " +
1807 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001808 $$ = $2;
Reid Spencera132e042006-12-03 05:46:11 +00001809 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001810 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001811 }
1812 | Types ZEROINITIALIZER {
Reid Spencer14310612006-12-31 05:40:51 +00001813 if (!UpRefs.empty())
1814 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001815 const Type *Ty = $1->get();
Chris Lattner58af2a12006-02-15 07:22:58 +00001816 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerb5334b02007-02-05 10:18:06 +00001817 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencera132e042006-12-03 05:46:11 +00001818 $$ = Constant::getNullValue(Ty);
1819 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001820 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00001821 }
Reid Spencer14310612006-12-31 05:40:51 +00001822 | IntType ESINT64VAL { // integral constants
Reid Spencere4d87aa2006-12-23 06:05:41 +00001823 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001824 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencer49d273e2007-03-19 20:40:51 +00001825 $$ = ConstantInt::get($1, $2, true);
Reid Spencer38c91a92007-02-28 02:24:54 +00001826 CHECK_FOR_ERROR
1827 }
1828 | IntType ESAPINTVAL { // arbitrary precision integer constants
1829 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1830 if ($2->getBitWidth() > BitWidth) {
1831 GEN_ERROR("Constant value does not fit in type");
Reid Spencer10794272007-03-01 19:41:47 +00001832 }
1833 $2->sextOrTrunc(BitWidth);
1834 $$ = ConstantInt::get(*$2);
Reid Spencer38c91a92007-02-28 02:24:54 +00001835 delete $2;
Reid Spencere4d87aa2006-12-23 06:05:41 +00001836 CHECK_FOR_ERROR
1837 }
Reid Spencer14310612006-12-31 05:40:51 +00001838 | IntType EUINT64VAL { // integral constants
Reid Spencere4d87aa2006-12-23 06:05:41 +00001839 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001840 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencer49d273e2007-03-19 20:40:51 +00001841 $$ = ConstantInt::get($1, $2, false);
Reid Spencer38c91a92007-02-28 02:24:54 +00001842 CHECK_FOR_ERROR
1843 }
1844 | IntType EUAPINTVAL { // arbitrary precision integer constants
1845 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1846 if ($2->getBitWidth() > BitWidth) {
1847 GEN_ERROR("Constant value does not fit in type");
Reid Spencer10794272007-03-01 19:41:47 +00001848 }
1849 $2->zextOrTrunc(BitWidth);
1850 $$ = ConstantInt::get(*$2);
Reid Spencer38c91a92007-02-28 02:24:54 +00001851 delete $2;
Reid Spencere4d87aa2006-12-23 06:05:41 +00001852 CHECK_FOR_ERROR
1853 }
Reid Spencer6f407902007-01-13 05:00:46 +00001854 | INTTYPE TRUETOK { // Boolean constants
1855 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001856 $$ = ConstantInt::getTrue();
Reid Spencer61c83e02006-08-18 08:43:06 +00001857 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001858 }
Reid Spencer6f407902007-01-13 05:00:46 +00001859 | INTTYPE FALSETOK { // Boolean constants
1860 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001861 $$ = ConstantInt::getFalse();
Reid Spencer61c83e02006-08-18 08:43:06 +00001862 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001863 }
Dale Johannesenea583102007-09-12 03:31:28 +00001864 | FPType FPVAL { // Floating point constants
Dale Johannesen43421b32007-09-06 18:13:44 +00001865 if (!ConstantFP::isValueValidForType($1, *$2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001866 GEN_ERROR("Floating point constant invalid for type");
Dale Johannesenc72cd7e2007-09-11 18:33:39 +00001867 // Lexer has no type info, so builds all float and double FP constants
1868 // as double. Fix this here. Long double is done right.
1869 if (&$2->getSemantics()==&APFloat::IEEEdouble && $1==Type::FloatTy)
Dale Johannesen43421b32007-09-06 18:13:44 +00001870 $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
1871 $$ = ConstantFP::get($1, *$2);
Dale Johannesencdd509a2007-09-07 21:07:57 +00001872 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001873 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001874 };
1875
1876
Reid Spencer3da59db2006-11-27 01:05:10 +00001877ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer14310612006-12-31 05:40:51 +00001878 if (!UpRefs.empty())
1879 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001880 Constant *Val = $3;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001881 const Type *DestTy = $5->get();
1882 if (!CastInst::castIsValid($1, $3, DestTy))
1883 GEN_ERROR("invalid cast opcode for cast from '" +
1884 Val->getType()->getDescription() + "' to '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001885 DestTy->getDescription() + "'");
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001886 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00001887 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00001888 }
1889 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001890 if (!isa<PointerType>($3->getType()))
Reid Spencerb5334b02007-02-05 10:18:06 +00001891 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00001892
Reid Spencera132e042006-12-03 05:46:11 +00001893 const Type *IdxTy =
David Greene5fd22a82007-09-04 18:46:50 +00001894 GetElementPtrInst::getIndexedType($3->getType(), $4->begin(), $4->end(),
Chris Lattner7d9801d2007-02-13 00:58:01 +00001895 true);
Reid Spencera132e042006-12-03 05:46:11 +00001896 if (!IdxTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00001897 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencera132e042006-12-03 05:46:11 +00001898
Chris Lattnerf7469af2007-01-31 04:44:08 +00001899 SmallVector<Constant*, 8> IdxVec;
Reid Spencera132e042006-12-03 05:46:11 +00001900 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1901 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattner58af2a12006-02-15 07:22:58 +00001902 IdxVec.push_back(C);
1903 else
Reid Spencerb5334b02007-02-05 10:18:06 +00001904 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattner58af2a12006-02-15 07:22:58 +00001905
1906 delete $4;
1907
Chris Lattnerf7469af2007-01-31 04:44:08 +00001908 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer61c83e02006-08-18 08:43:06 +00001909 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001910 }
1911 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer4fe16d62007-01-11 18:21:29 +00001912 if ($3->getType() != Type::Int1Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00001913 GEN_ERROR("Select condition must be of boolean type");
Reid Spencera132e042006-12-03 05:46:11 +00001914 if ($5->getType() != $7->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001915 GEN_ERROR("Select operand types must match");
Reid Spencera132e042006-12-03 05:46:11 +00001916 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001917 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001918 }
1919 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001920 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001921 GEN_ERROR("Binary operator types must match");
Reid Spencer1628cec2006-10-26 06:15:43 +00001922 CHECK_FOR_ERROR;
Reid Spencer9eef56f2006-12-05 19:16:11 +00001923 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner58af2a12006-02-15 07:22:58 +00001924 }
1925 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001926 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001927 GEN_ERROR("Logical operator types must match");
Chris Lattner42a75512007-01-15 02:27:26 +00001928 if (!$3->getType()->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001929 if (Instruction::isShift($1) || !isa<VectorType>($3->getType()) ||
1930 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00001931 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00001932 }
Reid Spencera132e042006-12-03 05:46:11 +00001933 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001934 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001935 }
Reid Spencer4012e832006-12-04 05:24:24 +00001936 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1937 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001938 GEN_ERROR("icmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00001939 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00001940 }
Reid Spencer4012e832006-12-04 05:24:24 +00001941 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1942 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001943 GEN_ERROR("fcmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00001944 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00001945 }
Chris Lattner58af2a12006-02-15 07:22:58 +00001946 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001947 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerb5334b02007-02-05 10:18:06 +00001948 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00001949 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001950 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00001951 }
1952 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001953 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00001954 GEN_ERROR("Invalid insertelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00001955 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001956 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00001957 }
1958 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001959 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00001960 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00001961 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001962 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001963 };
1964
Chris Lattnerd25db202006-04-08 03:55:17 +00001965
Chris Lattner58af2a12006-02-15 07:22:58 +00001966// ConstVector - A list of comma separated constants.
1967ConstVector : ConstVector ',' ConstVal {
1968 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001969 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001970 }
1971 | ConstVal {
Reid Spencera132e042006-12-03 05:46:11 +00001972 $$ = new std::vector<Constant*>();
Chris Lattner58af2a12006-02-15 07:22:58 +00001973 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001974 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001975 };
1976
1977
1978// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1979GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
1980
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001981// ThreadLocal
1982ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
1983
Anton Korobeynikov38e09802007-04-28 13:48:45 +00001984// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
1985AliaseeRef : ResultTypes SymbolicValueRef {
1986 const Type* VTy = $1->get();
1987 Value *V = getVal(VTy, $2);
Chris Lattner0275cff2007-08-06 21:00:46 +00001988 CHECK_FOR_ERROR
Anton Korobeynikov38e09802007-04-28 13:48:45 +00001989 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
1990 if (!Aliasee)
1991 GEN_ERROR("Aliases can be created only to global values");
1992
1993 $$ = Aliasee;
1994 CHECK_FOR_ERROR
1995 delete $1;
1996 }
1997 | BITCAST '(' AliaseeRef TO Types ')' {
1998 Constant *Val = $3;
1999 const Type *DestTy = $5->get();
2000 if (!CastInst::castIsValid($1, $3, DestTy))
2001 GEN_ERROR("invalid cast opcode for cast from '" +
2002 Val->getType()->getDescription() + "' to '" +
2003 DestTy->getDescription() + "'");
2004
2005 $$ = ConstantExpr::getCast($1, $3, DestTy);
2006 CHECK_FOR_ERROR
2007 delete $5;
2008 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002009
2010//===----------------------------------------------------------------------===//
2011// Rules to match Modules
2012//===----------------------------------------------------------------------===//
2013
2014// Module rule: Capture the result of parsing the whole file into a result
2015// variable...
2016//
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002017Module
2018 : DefinitionList {
2019 $$ = ParserResult = CurModule.CurrentModule;
2020 CurModule.ModuleDone();
2021 CHECK_FOR_ERROR;
2022 }
2023 | /*empty*/ {
2024 $$ = ParserResult = CurModule.CurrentModule;
2025 CurModule.ModuleDone();
2026 CHECK_FOR_ERROR;
2027 }
2028 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002029
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002030DefinitionList
2031 : Definition
2032 | DefinitionList Definition
2033 ;
2034
2035Definition
Jeff Cohen361c3ef2007-01-21 19:19:31 +00002036 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner58af2a12006-02-15 07:22:58 +00002037 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00002038 CHECK_FOR_ERROR
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002039 }
2040 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer61c83e02006-08-18 08:43:06 +00002041 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002042 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002043 | MODULE ASM_TOK AsmBlock {
Reid Spencer61c83e02006-08-18 08:43:06 +00002044 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002045 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002046 | OptLocalAssign TYPE Types {
Reid Spencer14310612006-12-31 05:40:51 +00002047 if (!UpRefs.empty())
2048 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002049 // Eagerly resolve types. This is not an optimization, this is a
2050 // requirement that is due to the fact that we could have this:
2051 //
2052 // %list = type { %list * }
2053 // %list = type { %list * } ; repeated type decl
2054 //
2055 // If types are not resolved eagerly, then the two types will not be
2056 // determined to be the same type!
2057 //
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002058 ResolveTypeTo($1, *$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002059
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002060 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002061 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002062 // If this is a named type that is not a redefinition, add it to the slot
2063 // table.
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002064 CurModule.Types.push_back(*$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002065 }
Reid Spencera132e042006-12-03 05:46:11 +00002066
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002067 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002068 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002069 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002070 | OptLocalAssign TYPE VOID {
Reid Spencer14310612006-12-31 05:40:51 +00002071 ResolveTypeTo($1, $3);
2072
2073 if (!setTypeName($3, $1) && !$1) {
2074 CHECK_FOR_ERROR
2075 // If this is a named type that is not a redefinition, add it to the slot
2076 // table.
2077 CurModule.Types.push_back($3);
2078 }
2079 CHECK_FOR_ERROR
2080 }
Christopher Lambbf3348d2007-12-12 08:45:45 +00002081 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal
2082 OptAddrSpace {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002083 /* "Externally Visible" Linkage */
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002084 if ($5 == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002085 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002086 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
Christopher Lambbf3348d2007-12-12 08:45:45 +00002087 $2, $4, $5->getType(), $5, $3, $6);
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00002088 CHECK_FOR_ERROR
2089 } GlobalVarAttributes {
2090 CurGV = 0;
2091 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002092 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambbf3348d2007-12-12 08:45:45 +00002093 ConstVal OptAddrSpace {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002094 if ($6 == 0)
2095 GEN_ERROR("Global value initializer is not a constant");
Christopher Lambbf3348d2007-12-12 08:45:45 +00002096 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4, $7);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002097 CHECK_FOR_ERROR
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002098 } GlobalVarAttributes {
2099 CurGV = 0;
2100 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002101 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambbf3348d2007-12-12 08:45:45 +00002102 Types OptAddrSpace {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002103 if (!UpRefs.empty())
2104 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
Christopher Lambbf3348d2007-12-12 08:45:45 +00002105 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4, $7);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002106 CHECK_FOR_ERROR
2107 delete $6;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002108 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002109 CurGV = 0;
2110 CHECK_FOR_ERROR
2111 }
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002112 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002113 std::string Name;
2114 if ($1) {
2115 Name = *$1;
2116 delete $1;
2117 }
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002118 if (Name.empty())
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002119 GEN_ERROR("Alias name cannot be empty");
2120
2121 Constant* Aliasee = $5;
2122 if (Aliasee == 0)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002123 GEN_ERROR(std::string("Invalid aliasee for alias: ") + Name);
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002124
2125 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2126 CurModule.CurrentModule);
2127 GA->setVisibility($2);
2128 InsertValue(GA, CurModule.Values);
Chris Lattner569f7372007-09-10 23:24:14 +00002129
2130
2131 // If there was a forward reference of this alias, resolve it now.
2132
2133 ValID ID;
2134 if (!Name.empty())
2135 ID = ValID::createGlobalName(Name);
2136 else
2137 ID = ValID::createGlobalID(CurModule.Values.size()-1);
2138
2139 if (GlobalValue *FWGV =
2140 CurModule.GetForwardRefForGlobal(GA->getType(), ID)) {
2141 // Replace uses of the fwdref with the actual alias.
2142 FWGV->replaceAllUsesWith(GA);
2143 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(FWGV))
2144 GV->eraseFromParent();
2145 else
2146 cast<Function>(FWGV)->eraseFromParent();
2147 }
2148 ID.destroy();
2149
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002150 CHECK_FOR_ERROR
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002151 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002152 | TARGET TargetDefinition {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002153 CHECK_FOR_ERROR
2154 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002155 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00002156 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002157 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002158 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002159
2160
2161AsmBlock : STRINGCONSTANT {
2162 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattner58af2a12006-02-15 07:22:58 +00002163 if (AsmSoFar.empty())
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002164 CurModule.CurrentModule->setModuleInlineAsm(*$1);
Chris Lattner58af2a12006-02-15 07:22:58 +00002165 else
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002166 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+*$1);
2167 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002168 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002169};
2170
Reid Spencer41dff5e2007-01-26 08:05:27 +00002171TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002172 CurModule.CurrentModule->setTargetTriple(*$3);
2173 delete $3;
John Criswell2f6a8b12006-10-24 19:09:48 +00002174 }
Chris Lattner1ae022f2006-10-22 06:08:13 +00002175 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002176 CurModule.CurrentModule->setDataLayout(*$3);
2177 delete $3;
Owen Anderson1dc69692006-10-18 02:21:48 +00002178 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002179
2180LibrariesDefinition : '[' LibList ']';
2181
2182LibList : LibList ',' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002183 CurModule.CurrentModule->addLibrary(*$3);
2184 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002185 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002186 }
2187 | STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002188 CurModule.CurrentModule->addLibrary(*$1);
2189 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002190 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002191 }
2192 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00002193 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002194 }
2195 ;
2196
2197//===----------------------------------------------------------------------===//
2198// Rules to match Function Headers
2199//===----------------------------------------------------------------------===//
2200
Reid Spencer41dff5e2007-01-26 08:05:27 +00002201ArgListH : ArgListH ',' Types OptParamAttrs OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002202 if (!UpRefs.empty())
2203 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
2204 if (*$3 == Type::VoidTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00002205 GEN_ERROR("void typed arguments are invalid");
Reid Spencer14310612006-12-31 05:40:51 +00002206 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00002207 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002208 $1->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002209 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002210 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002211 | Types OptParamAttrs OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002212 if (!UpRefs.empty())
2213 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2214 if (*$1 == Type::VoidTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00002215 GEN_ERROR("void typed arguments are invalid");
Reid Spencer14310612006-12-31 05:40:51 +00002216 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2217 $$ = new ArgListType;
2218 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002219 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002220 };
2221
2222ArgList : ArgListH {
2223 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002224 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002225 }
2226 | ArgListH ',' DOTDOTDOT {
2227 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002228 struct ArgListEntry E;
2229 E.Ty = new PATypeHolder(Type::VoidTy);
2230 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002231 E.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00002232 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002233 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002234 }
2235 | DOTDOTDOT {
Reid Spencer14310612006-12-31 05:40:51 +00002236 $$ = new ArgListType;
2237 struct ArgListEntry E;
2238 E.Ty = new PATypeHolder(Type::VoidTy);
2239 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002240 E.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00002241 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002242 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002243 }
2244 | /* empty */ {
2245 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00002246 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002247 };
2248
Reid Spencer41dff5e2007-01-26 08:05:27 +00002249FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002250 OptFuncAttrs OptSection OptAlign OptGC {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002251 std::string FunctionName(*$3);
2252 delete $3; // Free strdup'd memory!
Chris Lattner58af2a12006-02-15 07:22:58 +00002253
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002254 // Check the function result for abstractness if this is a define. We should
2255 // have no abstract types at this point
Reid Spencer218ded22007-01-05 17:07:23 +00002256 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2257 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002258
Chris Lattner58af2a12006-02-15 07:22:58 +00002259 std::vector<const Type*> ParamTypeList;
Christopher Lamb5c104242007-04-22 20:09:11 +00002260 ParamAttrsVector Attrs;
2261 if ($7 != ParamAttr::None) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002262 ParamAttrsWithIndex PAWI;
2263 PAWI.index = 0;
2264 PAWI.attrs = $7;
Christopher Lamb5c104242007-04-22 20:09:11 +00002265 Attrs.push_back(PAWI);
2266 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002267 if ($5) { // If there are arguments...
Reid Spencer7b5d4662007-04-09 06:16:21 +00002268 unsigned index = 1;
2269 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002270 const Type* Ty = I->Ty->get();
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002271 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2272 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencer14310612006-12-31 05:40:51 +00002273 ParamTypeList.push_back(Ty);
2274 if (Ty != Type::VoidTy)
Christopher Lamb5c104242007-04-22 20:09:11 +00002275 if (I->Attrs != ParamAttr::None) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002276 ParamAttrsWithIndex PAWI;
2277 PAWI.index = index;
2278 PAWI.attrs = I->Attrs;
Christopher Lamb5c104242007-04-22 20:09:11 +00002279 Attrs.push_back(PAWI);
2280 }
Reid Spencer14310612006-12-31 05:40:51 +00002281 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002282 }
2283
2284 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2285 if (isVarArg) ParamTypeList.pop_back();
2286
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00002287 const ParamAttrsList *PAL = 0;
Christopher Lamb5c104242007-04-22 20:09:11 +00002288 if (!Attrs.empty())
2289 PAL = ParamAttrsList::get(Attrs);
Reid Spencer7b5d4662007-04-09 06:16:21 +00002290
Duncan Sandsdc024672007-11-27 13:23:08 +00002291 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002292 const PointerType *PFT = PointerType::getUnqual(FT);
Reid Spencer218ded22007-01-05 17:07:23 +00002293 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002294
2295 ValID ID;
2296 if (!FunctionName.empty()) {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002297 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattner58af2a12006-02-15 07:22:58 +00002298 } else {
Reid Spencer93c40032007-03-19 18:40:50 +00002299 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002300 }
2301
2302 Function *Fn = 0;
2303 // See if this function was forward referenced. If so, recycle the object.
2304 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2305 // Move the function to the end of the list, from whereever it was
2306 // previously inserted.
2307 Fn = cast<Function>(FWRef);
Duncan Sandsdc024672007-11-27 13:23:08 +00002308 assert(!Fn->getParamAttrs() && "Forward reference has parameter attributes!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002309 CurModule.CurrentModule->getFunctionList().remove(Fn);
2310 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2311 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spenceref9b9a72007-02-05 20:47:22 +00002312 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002313 if (Fn->getFunctionType() != FT ) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002314 // The existing function doesn't have the same type. This is an overload
2315 // error.
2316 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Duncan Sandsdc024672007-11-27 13:23:08 +00002317 } else if (Fn->getParamAttrs() != PAL) {
2318 // The existing function doesn't have the same parameter attributes.
2319 // This is an overload error.
2320 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002321 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
Chris Lattner6cdc6822007-04-26 05:31:05 +00002322 // Neither the existing or the current function is a declaration and they
2323 // have the same name and same type. Clearly this is a redefinition.
2324 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Duncan Sandsdc024672007-11-27 13:23:08 +00002325 } else if (Fn->isDeclaration()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002326 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattner58af2a12006-02-15 07:22:58 +00002327 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2328 AI != AE; ++AI)
2329 AI->setName("");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002330 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002331 } else { // Not already defined?
Chris Lattner6cdc6822007-04-26 05:31:05 +00002332 Fn = new Function(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
Chris Lattner58af2a12006-02-15 07:22:58 +00002333 CurModule.CurrentModule);
2334 InsertValue(Fn, CurModule.Values);
2335 }
2336
2337 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002338
2339 if (CurFun.isDeclare) {
2340 // If we have declaration, always overwrite linkage. This will allow us to
2341 // correctly handle cases, when pointer to function is passed as argument to
2342 // another function.
2343 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002344 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002345 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002346 Fn->setCallingConv($1);
Duncan Sandsdc024672007-11-27 13:23:08 +00002347 Fn->setParamAttrs(PAL);
Reid Spencer218ded22007-01-05 17:07:23 +00002348 Fn->setAlignment($9);
2349 if ($8) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002350 Fn->setSection(*$8);
2351 delete $8;
Chris Lattner58af2a12006-02-15 07:22:58 +00002352 }
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002353 if ($10) {
2354 Fn->setCollector($10->c_str());
2355 delete $10;
2356 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002357
2358 // Add all of the arguments we parsed to the function...
2359 if ($5) { // Is null if empty...
2360 if (isVarArg) { // Nuke the last entry
Reid Spenceref9b9a72007-02-05 20:47:22 +00002361 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0 &&
Reid Spencera9720f52007-02-05 17:04:00 +00002362 "Not a varargs marker!");
Reid Spencer14310612006-12-31 05:40:51 +00002363 delete $5->back().Ty;
Chris Lattner58af2a12006-02-15 07:22:58 +00002364 $5->pop_back(); // Delete the last entry
2365 }
2366 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002367 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencer14310612006-12-31 05:40:51 +00002368 unsigned Idx = 1;
Reid Spenceref9b9a72007-02-05 20:47:22 +00002369 for (ArgListType::iterator I = $5->begin();
2370 I != $5->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencer14310612006-12-31 05:40:51 +00002371 delete I->Ty; // Delete the typeholder...
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002372 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002373 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002374 InsertValue(ArgIt);
Reid Spencer14310612006-12-31 05:40:51 +00002375 Idx++;
Chris Lattner58af2a12006-02-15 07:22:58 +00002376 }
Reid Spencera132e042006-12-03 05:46:11 +00002377
Chris Lattner58af2a12006-02-15 07:22:58 +00002378 delete $5; // We're now done with the argument list
2379 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002380 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002381};
2382
2383BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2384
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002385FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner58af2a12006-02-15 07:22:58 +00002386 $$ = CurFun.CurrentFunction;
2387
2388 // Make sure that we keep track of the linkage type even if there was a
2389 // previous "declare".
2390 $$->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002391 $$->setVisibility($2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002392};
2393
2394END : ENDTOK | '}'; // Allow end of '}' to end a function
2395
2396Function : BasicBlockList END {
2397 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002398 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002399};
2400
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002401FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencer14310612006-12-31 05:40:51 +00002402 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002403 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002404 $$ = CurFun.CurrentFunction;
2405 CurFun.FunctionDone();
2406 CHECK_FOR_ERROR
2407 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002408
2409//===----------------------------------------------------------------------===//
2410// Rules to match Basic Blocks
2411//===----------------------------------------------------------------------===//
2412
2413OptSideEffect : /* empty */ {
2414 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002415 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002416 }
2417 | SIDEEFFECT {
2418 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002419 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002420 };
2421
2422ConstValueRef : ESINT64VAL { // A reference to a direct constant
2423 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002424 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002425 }
2426 | EUINT64VAL {
2427 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002428 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002429 }
2430 | FPVAL { // Perhaps it's an FP constant?
2431 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002432 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002433 }
2434 | TRUETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002435 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002436 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002437 }
2438 | FALSETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002439 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002440 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002441 }
2442 | NULL_TOK {
2443 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002444 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002445 }
2446 | UNDEF {
2447 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002448 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002449 }
2450 | ZEROINITIALIZER { // A vector zero constant.
2451 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002452 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002453 }
2454 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencera132e042006-12-03 05:46:11 +00002455 const Type *ETy = (*$2)[0]->getType();
Chris Lattner58af2a12006-02-15 07:22:58 +00002456 int NumElements = $2->size();
2457
Reid Spencer9d6565a2007-02-15 02:26:10 +00002458 VectorType* pt = VectorType::get(ETy, NumElements);
Chris Lattner58af2a12006-02-15 07:22:58 +00002459 PATypeHolder* PTy = new PATypeHolder(
Reid Spencera132e042006-12-03 05:46:11 +00002460 HandleUpRefs(
Reid Spencer9d6565a2007-02-15 02:26:10 +00002461 VectorType::get(
Reid Spencera132e042006-12-03 05:46:11 +00002462 ETy,
2463 NumElements)
2464 )
2465 );
Chris Lattner58af2a12006-02-15 07:22:58 +00002466
2467 // Verify all elements are correct type!
2468 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00002469 if (ETy != (*$2)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002470 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00002471 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencera132e042006-12-03 05:46:11 +00002472 (*$2)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00002473 }
2474
Reid Spencer9d6565a2007-02-15 02:26:10 +00002475 $$ = ValID::create(ConstantVector::get(pt, *$2));
Chris Lattner58af2a12006-02-15 07:22:58 +00002476 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002477 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002478 }
2479 | ConstExpr {
Reid Spencera132e042006-12-03 05:46:11 +00002480 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002481 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002482 }
2483 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002484 $$ = ValID::createInlineAsm(*$3, *$5, $2);
2485 delete $3;
2486 delete $5;
Reid Spencer61c83e02006-08-18 08:43:06 +00002487 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002488 };
2489
2490// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2491// another value.
2492//
Reid Spencer41dff5e2007-01-26 08:05:27 +00002493SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2494 $$ = ValID::createLocalID($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002495 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002496 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002497 | GLOBALVAL_ID {
2498 $$ = ValID::createGlobalID($1);
2499 CHECK_FOR_ERROR
2500 }
2501 | LocalName { // Is it a named reference...?
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002502 $$ = ValID::createLocalName(*$1);
2503 delete $1;
Reid Spencer41dff5e2007-01-26 08:05:27 +00002504 CHECK_FOR_ERROR
2505 }
2506 | GlobalName { // Is it a named reference...?
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002507 $$ = ValID::createGlobalName(*$1);
2508 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002509 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002510 };
2511
2512// ValueRef - A reference to a definition... either constant or symbolic
2513ValueRef : SymbolicValueRef | ConstValueRef;
2514
2515
2516// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2517// type immediately preceeds the value reference, and allows complex constant
2518// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2519ResolvedVal : Types ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002520 if (!UpRefs.empty())
2521 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2522 $$ = getVal(*$1, $2);
2523 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002524 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00002525 }
2526 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002527
Devang Patel7990dc72008-02-20 22:40:23 +00002528ReturnedVal : ResolvedVal {
2529 $$ = new std::vector<Value *>();
2530 $$->push_back($1);
2531 CHECK_FOR_ERROR
2532 }
2533 | ReturnedVal ',' ConstVal {
2534 ($$=$1)->push_back($3);
2535 CHECK_FOR_ERROR
2536 };
2537
Chris Lattner58af2a12006-02-15 07:22:58 +00002538BasicBlockList : BasicBlockList BasicBlock {
2539 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002540 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002541 }
2542 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
2543 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002544 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002545 };
2546
2547
2548// Basic blocks are terminated by branching instructions:
2549// br, br/cc, switch, ret
2550//
Reid Spencer41dff5e2007-01-26 08:05:27 +00002551BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner58af2a12006-02-15 07:22:58 +00002552 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002553 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002554 InsertValue($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002555 $1->getInstList().push_back($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002556 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002557 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002558 };
2559
2560InstructionList : InstructionList Inst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002561 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2562 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2563 if (CI2->getParent() == 0)
2564 $1->getInstList().push_back(CI2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002565 $1->getInstList().push_back($2);
2566 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002567 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002568 }
Reid Spencer93c40032007-03-19 18:40:50 +00002569 | /* empty */ { // Empty space between instruction lists
2570 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
Reid Spencer61c83e02006-08-18 08:43:06 +00002571 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002572 }
Reid Spencer93c40032007-03-19 18:40:50 +00002573 | LABELSTR { // Labelled (named) basic block
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002574 $$ = defineBBVal(ValID::createLocalName(*$1));
2575 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002576 CHECK_FOR_ERROR
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002577
Chris Lattner58af2a12006-02-15 07:22:58 +00002578 };
2579
Devang Patel7990dc72008-02-20 22:40:23 +00002580BBTerminatorInst :
2581 RET ReturnedVal { // Return with a result...
2582 if($2->size() == 1)
2583 $$ = new ReturnInst($2->back());
2584 else {
2585
2586 std::vector<const Type*> Elements;
2587 std::vector<Constant*> Vals;
2588 for (std::vector<Value *>::iterator I = $2->begin(),
2589 E = $2->end(); I != E; ++I) {
2590 Value *V = *I;
2591 Constant *C = cast<Constant>(V);
2592 Elements.push_back(V->getType());
2593 Vals.push_back(C);
2594 }
2595
2596 const StructType *STy = StructType::get(Elements);
2597 PATypeHolder *PTy =
2598 new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
2599
2600 Constant *CS = ConstantStruct::get(STy, Vals); // *$2);
2601 $$ = new ReturnInst(CS);
2602 delete PTy;
2603 }
2604 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002605 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002606 }
Reid Spencer93c40032007-03-19 18:40:50 +00002607 | RET VOID { // Return with no result...
Chris Lattner58af2a12006-02-15 07:22:58 +00002608 $$ = new ReturnInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002609 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002610 }
Reid Spencer93c40032007-03-19 18:40:50 +00002611 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002612 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002613 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002614 $$ = new BranchInst(tmpBB);
Reid Spencer93c40032007-03-19 18:40:50 +00002615 } // Conditional Branch...
Reid Spencer6f407902007-01-13 05:00:46 +00002616 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
2617 assert(cast<IntegerType>($2)->getBitWidth() == 1 && "Not Bool?");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002618 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002619 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002620 BasicBlock* tmpBBB = getBBVal($9);
2621 CHECK_FOR_ERROR
Reid Spencer4fe16d62007-01-11 18:21:29 +00002622 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002623 CHECK_FOR_ERROR
2624 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattner58af2a12006-02-15 07:22:58 +00002625 }
2626 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002627 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002628 CHECK_FOR_ERROR
2629 BasicBlock* tmpBB = getBBVal($6);
2630 CHECK_FOR_ERROR
2631 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002632 $$ = S;
2633
2634 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2635 E = $8->end();
2636 for (; I != E; ++I) {
2637 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2638 S->addCase(CI, I->second);
2639 else
Reid Spencerb5334b02007-02-05 10:18:06 +00002640 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattner58af2a12006-02-15 07:22:58 +00002641 }
2642 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002643 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002644 }
2645 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002646 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002647 CHECK_FOR_ERROR
2648 BasicBlock* tmpBB = getBBVal($6);
2649 CHECK_FOR_ERROR
2650 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattner58af2a12006-02-15 07:22:58 +00002651 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002652 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002653 }
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002654 | INVOKE OptCallingConv ResultTypes ValueRef '(' ParamList ')' OptFuncAttrs
Chris Lattner58af2a12006-02-15 07:22:58 +00002655 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattner58af2a12006-02-15 07:22:58 +00002656
Reid Spencer14310612006-12-31 05:40:51 +00002657 // Handle the short syntax
2658 const PointerType *PFTy = 0;
2659 const FunctionType *Ty = 0;
Reid Spencer218ded22007-01-05 17:07:23 +00002660 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00002661 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2662 // Pull out the types of all of the arguments...
2663 std::vector<const Type*> ParamTypes;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002664 ParamList::iterator I = $6->begin(), E = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002665 for (; I != E; ++I) {
Reid Spencer14310612006-12-31 05:40:51 +00002666 const Type *Ty = I->Val->getType();
2667 if (Ty == Type::VoidTy)
2668 GEN_ERROR("Short call syntax cannot be used with varargs");
2669 ParamTypes.push_back(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002670 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002671 Ty = FunctionType::get($3->get(), ParamTypes, false);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002672 PFTy = PointerType::getUnqual(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002673 }
2674
Reid Spencer66728ef2007-03-20 01:13:36 +00002675 delete $3;
2676
Chris Lattner58af2a12006-02-15 07:22:58 +00002677 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002678 CHECK_FOR_ERROR
Reid Spencer218ded22007-01-05 17:07:23 +00002679 BasicBlock *Normal = getBBVal($11);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002680 CHECK_FOR_ERROR
Reid Spencer218ded22007-01-05 17:07:23 +00002681 BasicBlock *Except = getBBVal($14);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002682 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002683
Duncan Sandsdc024672007-11-27 13:23:08 +00002684 ParamAttrsVector Attrs;
2685 if ($8 != ParamAttr::None) {
2686 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $8;
2687 Attrs.push_back(PAWI);
2688 }
2689
Reid Spencer14310612006-12-31 05:40:51 +00002690 // Check the arguments
2691 ValueList Args;
2692 if ($6->empty()) { // Has no arguments?
2693 // Make sure no arguments is a good thing!
2694 if (Ty->getNumParams() != 0)
2695 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00002696 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00002697 } else { // Has arguments?
2698 // Loop through FunctionType's arguments and ensure they are specified
2699 // correctly!
Chris Lattner58af2a12006-02-15 07:22:58 +00002700 FunctionType::param_iterator I = Ty->param_begin();
2701 FunctionType::param_iterator E = Ty->param_end();
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002702 ParamList::iterator ArgI = $6->begin(), ArgE = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002703 unsigned index = 1;
Chris Lattner58af2a12006-02-15 07:22:58 +00002704
Duncan Sandsdc024672007-11-27 13:23:08 +00002705 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002706 if (ArgI->Val->getType() != *I)
2707 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00002708 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00002709 Args.push_back(ArgI->Val);
Duncan Sandsdc024672007-11-27 13:23:08 +00002710 if (ArgI->Attrs != ParamAttr::None) {
2711 ParamAttrsWithIndex PAWI;
2712 PAWI.index = index;
2713 PAWI.attrs = ArgI->Attrs;
2714 Attrs.push_back(PAWI);
2715 }
Reid Spencer14310612006-12-31 05:40:51 +00002716 }
Reid Spencera132e042006-12-03 05:46:11 +00002717
Reid Spencer14310612006-12-31 05:40:51 +00002718 if (Ty->isVarArg()) {
2719 if (I == E)
Chris Lattner38905612008-02-19 04:36:25 +00002720 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002721 Args.push_back(ArgI->Val); // push the remaining varargs
Chris Lattner38905612008-02-19 04:36:25 +00002722 if (ArgI->Attrs != ParamAttr::None) {
2723 ParamAttrsWithIndex PAWI;
2724 PAWI.index = index;
2725 PAWI.attrs = ArgI->Attrs;
2726 Attrs.push_back(PAWI);
2727 }
2728 }
Reid Spencer14310612006-12-31 05:40:51 +00002729 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00002730 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00002731 }
Reid Spencer14310612006-12-31 05:40:51 +00002732
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00002733 const ParamAttrsList *PAL = 0;
Duncan Sandsdc024672007-11-27 13:23:08 +00002734 if (!Attrs.empty())
2735 PAL = ParamAttrsList::get(Attrs);
2736
Reid Spencer14310612006-12-31 05:40:51 +00002737 // Create the InvokeInst
Chris Lattnerd80fb8b2007-08-29 16:15:23 +00002738 InvokeInst *II = new InvokeInst(V, Normal, Except, Args.begin(), Args.end());
Reid Spencer14310612006-12-31 05:40:51 +00002739 II->setCallingConv($2);
Duncan Sandsdc024672007-11-27 13:23:08 +00002740 II->setParamAttrs(PAL);
Reid Spencer14310612006-12-31 05:40:51 +00002741 $$ = II;
Chris Lattner58af2a12006-02-15 07:22:58 +00002742 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002743 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002744 }
2745 | UNWIND {
2746 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002747 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002748 }
2749 | UNREACHABLE {
2750 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002751 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002752 };
2753
2754
2755
2756JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2757 $$ = $1;
Reid Spencer93c40032007-03-19 18:40:50 +00002758 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002759 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002760 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002761 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00002762
Reid Spencer5b7e7532006-09-28 19:28:24 +00002763 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002764 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002765 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002766 }
2767 | IntType ConstValueRef ',' LABEL ValueRef {
2768 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencer93c40032007-03-19 18:40:50 +00002769 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002770 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002771
2772 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002773 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00002774
Reid Spencer5b7e7532006-09-28 19:28:24 +00002775 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002776 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002777 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002778 };
2779
Reid Spencer41dff5e2007-01-26 08:05:27 +00002780Inst : OptLocalAssign InstVal {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002781 // Is this definition named?? if so, assign the name...
2782 setValueName($2, $1);
2783 CHECK_FOR_ERROR
2784 InsertValue($2);
2785 $$ = $2;
2786 CHECK_FOR_ERROR
2787 };
2788
Chris Lattner58af2a12006-02-15 07:22:58 +00002789
2790PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer14310612006-12-31 05:40:51 +00002791 if (!UpRefs.empty())
2792 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002793 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencera132e042006-12-03 05:46:11 +00002794 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002795 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002796 BasicBlock* tmpBB = getBBVal($5);
2797 CHECK_FOR_ERROR
2798 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencera132e042006-12-03 05:46:11 +00002799 delete $1;
Chris Lattner58af2a12006-02-15 07:22:58 +00002800 }
2801 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2802 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002803 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002804 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002805 BasicBlock* tmpBB = getBBVal($6);
2806 CHECK_FOR_ERROR
2807 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002808 };
2809
2810
Duncan Sandsdc024672007-11-27 13:23:08 +00002811ParamList : Types OptParamAttrs ValueRef OptParamAttrs {
2812 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Reid Spencer14310612006-12-31 05:40:51 +00002813 if (!UpRefs.empty())
2814 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2815 // Used for call and invoke instructions
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002816 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00002817 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getVal($1->get(), $3);
Reid Spencer14310612006-12-31 05:40:51 +00002818 $$->push_back(E);
Reid Spencer66728ef2007-03-20 01:13:36 +00002819 delete $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002820 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002821 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002822 | LABEL OptParamAttrs ValueRef OptParamAttrs {
2823 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002824 // Labels are only valid in ASMs
2825 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00002826 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getBBVal($3);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002827 $$->push_back(E);
Duncan Sandsdc024672007-11-27 13:23:08 +00002828 CHECK_FOR_ERROR
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002829 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002830 | ParamList ',' Types OptParamAttrs ValueRef OptParamAttrs {
2831 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Reid Spencer14310612006-12-31 05:40:51 +00002832 if (!UpRefs.empty())
2833 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002834 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002835 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getVal($3->get(), $5);
Reid Spencer14310612006-12-31 05:40:51 +00002836 $$->push_back(E);
Reid Spencer66728ef2007-03-20 01:13:36 +00002837 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002838 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00002839 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002840 | ParamList ',' LABEL OptParamAttrs ValueRef OptParamAttrs {
2841 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002842 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002843 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getBBVal($5);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002844 $$->push_back(E);
2845 CHECK_FOR_ERROR
2846 }
2847 | /*empty*/ { $$ = new ParamList(); };
Chris Lattner58af2a12006-02-15 07:22:58 +00002848
Reid Spencer14310612006-12-31 05:40:51 +00002849IndexList // Used for gep instructions and constant expressions
Reid Spencerc6c59fd2006-12-31 21:47:02 +00002850 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencer14310612006-12-31 05:40:51 +00002851 | IndexList ',' ResolvedVal {
2852 $$ = $1;
2853 $$->push_back($3);
2854 CHECK_FOR_ERROR
2855 }
Reid Spencerc6c59fd2006-12-31 21:47:02 +00002856 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002857
2858OptTailCall : TAIL CALL {
2859 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002860 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002861 }
2862 | CALL {
2863 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002864 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002865 };
2866
Chris Lattner58af2a12006-02-15 07:22:58 +00002867InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002868 if (!UpRefs.empty())
2869 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002870 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencer9d6565a2007-02-15 02:26:10 +00002871 !isa<VectorType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002872 GEN_ERROR(
Reid Spencerb5334b02007-02-05 10:18:06 +00002873 "Arithmetic operator requires integer, FP, or packed operands");
Reid Spencera132e042006-12-03 05:46:11 +00002874 Value* val1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002875 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002876 Value* val2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002877 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002878 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002879 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002880 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00002881 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002882 }
2883 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002884 if (!UpRefs.empty())
2885 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002886 if (!(*$2)->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00002887 if (Instruction::isShift($1) || !isa<VectorType>($2->get()) ||
2888 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00002889 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00002890 }
Reid Spencera132e042006-12-03 05:46:11 +00002891 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002892 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002893 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002894 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002895 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002896 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002897 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00002898 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002899 }
Reid Spencera132e042006-12-03 05:46:11 +00002900 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002901 if (!UpRefs.empty())
2902 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002903 if (isa<VectorType>((*$3).get()))
Chris Lattner32980692007-02-19 07:44:24 +00002904 GEN_ERROR("Vector types not supported by icmp instruction");
Reid Spencera132e042006-12-03 05:46:11 +00002905 Value* tmpVal1 = getVal(*$3, $4);
2906 CHECK_FOR_ERROR
2907 Value* tmpVal2 = getVal(*$3, $6);
2908 CHECK_FOR_ERROR
2909 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2910 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002911 GEN_ERROR("icmp operator returned null");
Reid Spencer66728ef2007-03-20 01:13:36 +00002912 delete $3;
Reid Spencera132e042006-12-03 05:46:11 +00002913 }
2914 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002915 if (!UpRefs.empty())
2916 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002917 if (isa<VectorType>((*$3).get()))
Chris Lattner32980692007-02-19 07:44:24 +00002918 GEN_ERROR("Vector types not supported by fcmp instruction");
Reid Spencera132e042006-12-03 05:46:11 +00002919 Value* tmpVal1 = getVal(*$3, $4);
2920 CHECK_FOR_ERROR
2921 Value* tmpVal2 = getVal(*$3, $6);
2922 CHECK_FOR_ERROR
2923 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2924 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002925 GEN_ERROR("fcmp operator returned null");
Reid Spencer66728ef2007-03-20 01:13:36 +00002926 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00002927 }
Reid Spencer3da59db2006-11-27 01:05:10 +00002928 | CastOps ResolvedVal TO Types {
Reid Spencer14310612006-12-31 05:40:51 +00002929 if (!UpRefs.empty())
2930 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00002931 Value* Val = $2;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00002932 const Type* DestTy = $4->get();
2933 if (!CastInst::castIsValid($1, Val, DestTy))
2934 GEN_ERROR("invalid cast opcode for cast from '" +
2935 Val->getType()->getDescription() + "' to '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00002936 DestTy->getDescription() + "'");
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00002937 $$ = CastInst::create($1, Val, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00002938 delete $4;
Chris Lattner58af2a12006-02-15 07:22:58 +00002939 }
2940 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer4fe16d62007-01-11 18:21:29 +00002941 if ($2->getType() != Type::Int1Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00002942 GEN_ERROR("select condition must be boolean");
Reid Spencera132e042006-12-03 05:46:11 +00002943 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002944 GEN_ERROR("select value types should match");
Reid Spencera132e042006-12-03 05:46:11 +00002945 $$ = new SelectInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002946 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002947 }
2948 | VAARG ResolvedVal ',' Types {
Reid Spencer14310612006-12-31 05:40:51 +00002949 if (!UpRefs.empty())
2950 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00002951 $$ = new VAArgInst($2, *$4);
2952 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002953 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002954 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002955 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002956 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerb5334b02007-02-05 10:18:06 +00002957 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00002958 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002959 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002960 }
2961 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002962 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00002963 GEN_ERROR("Invalid insertelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00002964 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002965 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002966 }
Chris Lattnerd5efe842006-04-08 01:18:56 +00002967 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002968 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00002969 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00002970 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002971 CHECK_FOR_ERROR
Chris Lattnerd5efe842006-04-08 01:18:56 +00002972 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002973 | PHI_TOK PHIList {
2974 const Type *Ty = $2->front().first->getType();
2975 if (!Ty->isFirstClassType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002976 GEN_ERROR("PHI node operands must be of first class type");
Chris Lattner58af2a12006-02-15 07:22:58 +00002977 $$ = new PHINode(Ty);
2978 ((PHINode*)$$)->reserveOperandSpace($2->size());
2979 while ($2->begin() != $2->end()) {
2980 if ($2->front().first->getType() != Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00002981 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattner58af2a12006-02-15 07:22:58 +00002982 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
2983 $2->pop_front();
2984 }
2985 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00002986 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002987 }
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002988 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ParamList ')'
Reid Spencer218ded22007-01-05 17:07:23 +00002989 OptFuncAttrs {
Reid Spencer14310612006-12-31 05:40:51 +00002990
2991 // Handle the short syntax
Reid Spencer3da59db2006-11-27 01:05:10 +00002992 const PointerType *PFTy = 0;
2993 const FunctionType *Ty = 0;
Reid Spencer218ded22007-01-05 17:07:23 +00002994 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00002995 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2996 // Pull out the types of all of the arguments...
2997 std::vector<const Type*> ParamTypes;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002998 ParamList::iterator I = $6->begin(), E = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002999 for (; I != E; ++I) {
Reid Spencer14310612006-12-31 05:40:51 +00003000 const Type *Ty = I->Val->getType();
3001 if (Ty == Type::VoidTy)
3002 GEN_ERROR("Short call syntax cannot be used with varargs");
3003 ParamTypes.push_back(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00003004 }
Duncan Sandsdc024672007-11-27 13:23:08 +00003005 Ty = FunctionType::get($3->get(), ParamTypes, false);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00003006 PFTy = PointerType::getUnqual(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00003007 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00003008
Chris Lattner58af2a12006-02-15 07:22:58 +00003009 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00003010 CHECK_FOR_ERROR
Chris Lattner6cdc6822007-04-26 05:31:05 +00003011
Reid Spencer7780acb2007-04-16 06:56:07 +00003012 // Check for call to invalid intrinsic to avoid crashing later.
3013 if (Function *theF = dyn_cast<Function>(V)) {
Reid Spencered48de22007-04-16 22:02:23 +00003014 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
Reid Spencer36fdde12007-04-16 20:35:38 +00003015 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
3016 !theF->getIntrinsicID(true))
Reid Spencer7780acb2007-04-16 06:56:07 +00003017 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
3018 theF->getName() + "'");
3019 }
3020
Duncan Sandsdc024672007-11-27 13:23:08 +00003021 // Set up the ParamAttrs for the function
3022 ParamAttrsVector Attrs;
3023 if ($8 != ParamAttr::None) {
3024 ParamAttrsWithIndex PAWI;
3025 PAWI.index = 0;
3026 PAWI.attrs = $8;
3027 Attrs.push_back(PAWI);
3028 }
Reid Spencer14310612006-12-31 05:40:51 +00003029 // Check the arguments
3030 ValueList Args;
3031 if ($6->empty()) { // Has no arguments?
Chris Lattner58af2a12006-02-15 07:22:58 +00003032 // Make sure no arguments is a good thing!
3033 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00003034 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00003035 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00003036 } else { // Has arguments?
3037 // Loop through FunctionType's arguments and ensure they are specified
Duncan Sandsdc024672007-11-27 13:23:08 +00003038 // correctly. Also, gather any parameter attributes.
Chris Lattner58af2a12006-02-15 07:22:58 +00003039 FunctionType::param_iterator I = Ty->param_begin();
3040 FunctionType::param_iterator E = Ty->param_end();
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003041 ParamList::iterator ArgI = $6->begin(), ArgE = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00003042 unsigned index = 1;
Chris Lattner58af2a12006-02-15 07:22:58 +00003043
Duncan Sandsdc024672007-11-27 13:23:08 +00003044 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00003045 if (ArgI->Val->getType() != *I)
3046 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00003047 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00003048 Args.push_back(ArgI->Val);
Duncan Sandsdc024672007-11-27 13:23:08 +00003049 if (ArgI->Attrs != ParamAttr::None) {
3050 ParamAttrsWithIndex PAWI;
3051 PAWI.index = index;
3052 PAWI.attrs = ArgI->Attrs;
3053 Attrs.push_back(PAWI);
3054 }
Reid Spencer14310612006-12-31 05:40:51 +00003055 }
3056 if (Ty->isVarArg()) {
3057 if (I == E)
Chris Lattner38905612008-02-19 04:36:25 +00003058 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00003059 Args.push_back(ArgI->Val); // push the remaining varargs
Chris Lattner38905612008-02-19 04:36:25 +00003060 if (ArgI->Attrs != ParamAttr::None) {
3061 ParamAttrsWithIndex PAWI;
3062 PAWI.index = index;
3063 PAWI.attrs = ArgI->Attrs;
3064 Attrs.push_back(PAWI);
3065 }
3066 }
Reid Spencer14310612006-12-31 05:40:51 +00003067 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00003068 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00003069 }
Duncan Sandsdc024672007-11-27 13:23:08 +00003070
3071 // Finish off the ParamAttrs and check them
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00003072 const ParamAttrsList *PAL = 0;
Duncan Sandsdc024672007-11-27 13:23:08 +00003073 if (!Attrs.empty())
3074 PAL = ParamAttrsList::get(Attrs);
3075
Reid Spencer14310612006-12-31 05:40:51 +00003076 // Create the call node
David Greene718fda32007-08-01 03:59:32 +00003077 CallInst *CI = new CallInst(V, Args.begin(), Args.end());
Reid Spencer14310612006-12-31 05:40:51 +00003078 CI->setTailCall($1);
3079 CI->setCallingConv($2);
Duncan Sandsdc024672007-11-27 13:23:08 +00003080 CI->setParamAttrs(PAL);
Reid Spencer14310612006-12-31 05:40:51 +00003081 $$ = CI;
Chris Lattner58af2a12006-02-15 07:22:58 +00003082 delete $6;
Reid Spencer41dff5e2007-01-26 08:05:27 +00003083 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00003084 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003085 }
3086 | MemoryInst {
3087 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00003088 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003089 };
3090
Chris Lattner58af2a12006-02-15 07:22:58 +00003091OptVolatile : VOLATILE {
3092 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00003093 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003094 }
3095 | /* empty */ {
3096 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00003097 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003098 };
3099
3100
3101
3102MemoryInst : MALLOC Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003103 if (!UpRefs.empty())
3104 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003105 $$ = new MallocInst(*$2, 0, $3);
3106 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003107 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003108 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003109 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003110 if (!UpRefs.empty())
3111 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003112 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003113 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003114 $$ = new MallocInst(*$2, tmpVal, $6);
3115 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003116 }
3117 | ALLOCA Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003118 if (!UpRefs.empty())
3119 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003120 $$ = new AllocaInst(*$2, 0, $3);
3121 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003122 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003123 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003124 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003125 if (!UpRefs.empty())
3126 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003127 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003128 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003129 $$ = new AllocaInst(*$2, tmpVal, $6);
3130 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003131 }
3132 | FREE ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003133 if (!isa<PointerType>($2->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003134 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerb5334b02007-02-05 10:18:06 +00003135 $2->getType()->getDescription() + "");
Reid Spencera132e042006-12-03 05:46:11 +00003136 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00003137 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003138 }
3139
Christopher Lamb5c104242007-04-22 20:09:11 +00003140 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003141 if (!UpRefs.empty())
3142 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003143 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003144 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003145 (*$3)->getDescription());
3146 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00003147 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003148 (*$3)->getDescription());
3149 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003150 CHECK_FOR_ERROR
Christopher Lamb5c104242007-04-22 20:09:11 +00003151 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencera132e042006-12-03 05:46:11 +00003152 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00003153 }
Christopher Lamb5c104242007-04-22 20:09:11 +00003154 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003155 if (!UpRefs.empty())
3156 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003157 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00003158 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00003159 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003160 (*$5)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00003161 const Type *ElTy = PT->getElementType();
Reid Spencera132e042006-12-03 05:46:11 +00003162 if (ElTy != $3->getType())
3163 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerb5334b02007-02-05 10:18:06 +00003164 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00003165
Reid Spencera132e042006-12-03 05:46:11 +00003166 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003167 CHECK_FOR_ERROR
Christopher Lamb5c104242007-04-22 20:09:11 +00003168 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencera132e042006-12-03 05:46:11 +00003169 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00003170 }
Devang Patelbd41a062008-02-22 19:31:30 +00003171| GETRESULT Types SymbolicValueRef ',' EUINT64VAL {
3172 Value *TmpVal = getVal($2->get(), $3);
Devang Patel5a970972008-02-19 22:27:01 +00003173 if (!GetResultInst::isValidOperands(TmpVal, $5))
3174 GEN_ERROR("Invalid getresult operands");
3175 $$ = new GetResultInst(TmpVal, $5);
3176 CHECK_FOR_ERROR
3177 }
Chris Lattner58af2a12006-02-15 07:22:58 +00003178 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer14310612006-12-31 05:40:51 +00003179 if (!UpRefs.empty())
3180 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003181 if (!isa<PointerType>($2->get()))
Reid Spencerb5334b02007-02-05 10:18:06 +00003182 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00003183
David Greene5fd22a82007-09-04 18:46:50 +00003184 if (!GetElementPtrInst::getIndexedType(*$2, $4->begin(), $4->end(), true))
Reid Spencer61c83e02006-08-18 08:43:06 +00003185 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00003186 (*$2)->getDescription()+ "'");
Reid Spencera132e042006-12-03 05:46:11 +00003187 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003188 CHECK_FOR_ERROR
David Greene5fd22a82007-09-04 18:46:50 +00003189 $$ = new GetElementPtrInst(tmpVal, $4->begin(), $4->end());
Reid Spencera132e042006-12-03 05:46:11 +00003190 delete $2;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003191 delete $4;
Chris Lattner58af2a12006-02-15 07:22:58 +00003192 };
3193
3194
3195%%
Reid Spencer61c83e02006-08-18 08:43:06 +00003196
Reid Spencer14310612006-12-31 05:40:51 +00003197// common code from the two 'RunVMAsmParser' functions
3198static Module* RunParser(Module * M) {
Reid Spencer14310612006-12-31 05:40:51 +00003199 CurModule.CurrentModule = M;
Reid Spencer14310612006-12-31 05:40:51 +00003200 // Check to make sure the parser succeeded
3201 if (yyparse()) {
3202 if (ParserResult)
3203 delete ParserResult;
3204 return 0;
3205 }
3206
Reid Spencer0d60b5a2007-03-30 01:37:39 +00003207 // Emit an error if there are any unresolved types left.
3208 if (!CurModule.LateResolveTypes.empty()) {
3209 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3210 if (DID.Type == ValID::LocalName) {
3211 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3212 } else {
3213 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3214 }
3215 if (ParserResult)
3216 delete ParserResult;
3217 return 0;
3218 }
3219
3220 // Emit an error if there are any unresolved values left.
3221 if (!CurModule.LateResolveValues.empty()) {
3222 Value *V = CurModule.LateResolveValues.back();
3223 std::map<Value*, std::pair<ValID, int> >::iterator I =
3224 CurModule.PlaceHolderInfo.find(V);
3225
3226 if (I != CurModule.PlaceHolderInfo.end()) {
3227 ValID &DID = I->second.first;
3228 if (DID.Type == ValID::LocalName) {
3229 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3230 } else {
3231 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3232 }
3233 if (ParserResult)
3234 delete ParserResult;
3235 return 0;
3236 }
3237 }
3238
Reid Spencer14310612006-12-31 05:40:51 +00003239 // Check to make sure that parsing produced a result
3240 if (!ParserResult)
3241 return 0;
3242
3243 // Reset ParserResult variable while saving its value for the result.
3244 Module *Result = ParserResult;
3245 ParserResult = 0;
3246
3247 return Result;
3248}
3249
Reid Spencer61c83e02006-08-18 08:43:06 +00003250void llvm::GenerateError(const std::string &message, int LineNo) {
Duncan Sandsdc024672007-11-27 13:23:08 +00003251 if (LineNo == -1) LineNo = LLLgetLineNo();
Reid Spencer61c83e02006-08-18 08:43:06 +00003252 // TODO: column number in exception
3253 if (TheParseError)
Duncan Sandsdc024672007-11-27 13:23:08 +00003254 TheParseError->setError(LLLgetFilename(), message, LineNo);
Reid Spencer61c83e02006-08-18 08:43:06 +00003255 TriggerError = 1;
3256}
3257
Chris Lattner58af2a12006-02-15 07:22:58 +00003258int yyerror(const char *ErrorMsg) {
Duncan Sandsdc024672007-11-27 13:23:08 +00003259 std::string where = LLLgetFilename() + ":" + utostr(LLLgetLineNo()) + ": ";
Reid Spenceref9b9a72007-02-05 20:47:22 +00003260 std::string errMsg = where + "error: " + std::string(ErrorMsg);
Duncan Sandsdc024672007-11-27 13:23:08 +00003261 if (yychar != YYEMPTY && yychar != 0) {
3262 errMsg += " while reading token: '";
3263 errMsg += std::string(LLLgetTokenStart(),
3264 LLLgetTokenStart()+LLLgetTokenLength()) + "'";
3265 }
Reid Spencer61c83e02006-08-18 08:43:06 +00003266 GenerateError(errMsg);
Chris Lattner58af2a12006-02-15 07:22:58 +00003267 return 0;
3268}