blob: 6aab1fe40b306220340c835395e30bc252104f2f [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.
Devang Patel67909432008-03-03 18:58:47 +0000520static BasicBlock *defineBBVal(const ValID &ID, BasicBlock *unwindDest) {
Reid Spencera9720f52007-02-05 17:04:00 +0000521 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000522
Chris Lattner58af2a12006-02-15 07:22:58 +0000523 BasicBlock *BB = 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000524
Reid Spencer93c40032007-03-19 18:40:50 +0000525 // First, see if this was forward referenced
Chris Lattner58af2a12006-02-15 07:22:58 +0000526
Reid Spencer93c40032007-03-19 18:40:50 +0000527 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
528 if (BBI != CurFun.BBForwardRefs.end()) {
529 BB = BBI->second;
Chris Lattner58af2a12006-02-15 07:22:58 +0000530 // The forward declaration could have been inserted anywhere in the
531 // function: insert it into the correct place now.
532 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
533 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Reid Spencer93c40032007-03-19 18:40:50 +0000534
Reid Spencer66728ef2007-03-20 01:13:36 +0000535 // We're about to erase the entry, save the key so we can clean it up.
536 ValID Tmp = BBI->first;
537
Reid Spencer93c40032007-03-19 18:40:50 +0000538 // Erase the forward ref from the map as its no longer "forward"
539 CurFun.BBForwardRefs.erase(ID);
540
Reid Spencer66728ef2007-03-20 01:13:36 +0000541 // The key has been removed from the map but so we don't want to leave
542 // strdup'd memory around so destroy it too.
543 Tmp.destroy();
544
Reid Spencer93c40032007-03-19 18:40:50 +0000545 // If its a numbered definition, bump the number and set the BB value.
546 if (ID.Type == ValID::LocalID) {
547 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
548 InsertValue(BB);
549 }
Devang Patel67909432008-03-03 18:58:47 +0000550 } else {
551 // We haven't seen this BB before and its first mention is a definition.
552 // Just create it and return it.
553 std::string Name (ID.Type == ValID::LocalName ? ID.getName() : "");
554 BB = new BasicBlock(Name, CurFun.CurrentFunction);
555 if (ID.Type == ValID::LocalID) {
556 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
557 InsertValue(BB);
558 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000559 }
Reid Spencer93c40032007-03-19 18:40:50 +0000560
Devang Patel67909432008-03-03 18:58:47 +0000561 ID.destroy();
562 BB->setUnwindDest(unwindDest);
Reid Spencer93c40032007-03-19 18:40:50 +0000563 return BB;
564}
565
566/// getBBVal - get an existing BB value or create a forward reference for it.
567///
568static BasicBlock *getBBVal(const ValID &ID) {
569 assert(inFunctionScope() && "Can't get basic block at global scope!");
570
571 BasicBlock *BB = 0;
572
573 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
574 if (BBI != CurFun.BBForwardRefs.end()) {
575 BB = BBI->second;
576 } if (ID.Type == ValID::LocalName) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000577 std::string Name = ID.getName();
Reid Spencer93c40032007-03-19 18:40:50 +0000578 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000579 if (N) {
Reid Spencer93c40032007-03-19 18:40:50 +0000580 if (N->getType()->getTypeID() == Type::LabelTyID)
581 BB = cast<BasicBlock>(N);
582 else
583 GenerateError("Reference to label '" + Name + "' is actually of type '"+
584 N->getType()->getDescription() + "'");
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000585 }
Reid Spencer93c40032007-03-19 18:40:50 +0000586 } else if (ID.Type == ValID::LocalID) {
587 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
588 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
589 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
590 else
591 GenerateError("Reference to label '%" + utostr(ID.Num) +
592 "' is actually of type '"+
593 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
594 }
595 } else {
596 GenerateError("Illegal label reference " + ID.getName());
597 return 0;
598 }
599
600 // If its already been defined, return it now.
601 if (BB) {
602 ID.destroy(); // Free strdup'd memory.
603 return BB;
604 }
605
606 // Otherwise, this block has not been seen before, create it.
607 std::string Name;
608 if (ID.Type == ValID::LocalName)
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000609 Name = ID.getName();
Reid Spencer93c40032007-03-19 18:40:50 +0000610 BB = new BasicBlock(Name, CurFun.CurrentFunction);
611
612 // Insert it in the forward refs map.
613 CurFun.BBForwardRefs[ID] = BB;
614
Chris Lattner58af2a12006-02-15 07:22:58 +0000615 return BB;
616}
617
618
619//===----------------------------------------------------------------------===//
620// Code to handle forward references in instructions
621//===----------------------------------------------------------------------===//
622//
623// This code handles the late binding needed with statements that reference
624// values not defined yet... for example, a forward branch, or the PHI node for
625// a loop body.
626//
627// This keeps a table (CurFun.LateResolveValues) of all such forward references
628// and back patchs after we are done.
629//
630
631// ResolveDefinitions - If we could not resolve some defs at parsing
632// time (forward branches, phi functions for loops, etc...) resolve the
633// defs now...
634//
635static void
Reid Spencer93c40032007-03-19 18:40:50 +0000636ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000637 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencer93c40032007-03-19 18:40:50 +0000638 while (!LateResolvers.empty()) {
639 Value *V = LateResolvers.back();
640 LateResolvers.pop_back();
Chris Lattner58af2a12006-02-15 07:22:58 +0000641
Reid Spencer93c40032007-03-19 18:40:50 +0000642 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
643 CurModule.PlaceHolderInfo.find(V);
644 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000645
Reid Spencer93c40032007-03-19 18:40:50 +0000646 ValID &DID = PHI->second.first;
Chris Lattner58af2a12006-02-15 07:22:58 +0000647
Reid Spencer93c40032007-03-19 18:40:50 +0000648 Value *TheRealValue = getExistingVal(V->getType(), DID);
649 if (TriggerError)
650 return;
651 if (TheRealValue) {
652 V->replaceAllUsesWith(TheRealValue);
653 delete V;
654 CurModule.PlaceHolderInfo.erase(PHI);
655 } else if (FutureLateResolvers) {
656 // Functions have their unresolved items forwarded to the module late
657 // resolver table
658 InsertValue(V, *FutureLateResolvers);
659 } else {
660 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
661 GenerateError("Reference to an invalid definition: '" +DID.getName()+
662 "' of type '" + V->getType()->getDescription() + "'",
663 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000664 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000665 } else {
Reid Spencer93c40032007-03-19 18:40:50 +0000666 GenerateError("Reference to an invalid definition: #" +
667 itostr(DID.Num) + " of type '" +
668 V->getType()->getDescription() + "'",
669 PHI->second.second);
670 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000671 }
672 }
673 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000674 LateResolvers.clear();
675}
676
677// ResolveTypeTo - A brand new type was just declared. This means that (if
678// name is not null) things referencing Name can be resolved. Otherwise, things
679// refering to the number can be resolved. Do this now.
680//
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000681static void ResolveTypeTo(std::string *Name, const Type *ToTy) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000682 ValID D;
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000683 if (Name)
684 D = ValID::createLocalName(*Name);
685 else
686 D = ValID::createLocalID(CurModule.Types.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000687
Reid Spencer861d9d62006-11-28 07:29:44 +0000688 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattner58af2a12006-02-15 07:22:58 +0000689 CurModule.LateResolveTypes.find(D);
690 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencer861d9d62006-11-28 07:29:44 +0000691 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner58af2a12006-02-15 07:22:58 +0000692 CurModule.LateResolveTypes.erase(I);
693 }
694}
695
696// setValueName - Set the specified value to the name given. The name may be
697// null potentially, in which case this is a noop. The string passed in is
698// assumed to be a malloc'd string buffer, and is free'd by this function.
699//
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000700static void setValueName(Value *V, std::string *NameStr) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000701 if (!NameStr) return;
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000702 std::string Name(*NameStr); // Copy string
703 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000704
Reid Spencer41dff5e2007-01-26 08:05:27 +0000705 if (V->getType() == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000706 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000707 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000708 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000709
Reid Spencera9720f52007-02-05 17:04:00 +0000710 assert(inFunctionScope() && "Must be in function scope!");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000711 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
712 if (ST.lookup(Name)) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000713 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000714 V->getType()->getDescription() + "'");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000715 return;
716 }
717
718 // Set the name.
719 V->setName(Name);
Chris Lattner58af2a12006-02-15 07:22:58 +0000720}
721
722/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
723/// this is a declaration, otherwise it is a definition.
724static GlobalVariable *
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000725ParseGlobalVariable(std::string *NameStr,
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000726 GlobalValue::LinkageTypes Linkage,
727 GlobalValue::VisibilityTypes Visibility,
Chris Lattner58af2a12006-02-15 07:22:58 +0000728 bool isConstantGlobal, const Type *Ty,
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000729 Constant *Initializer, bool IsThreadLocal,
730 unsigned AddressSpace = 0) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000731 if (isa<FunctionType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000732 GenerateError("Cannot declare global vars of function type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000733 return 0;
734 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000735
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000736 const PointerType *PTy = PointerType::get(Ty, AddressSpace);
Chris Lattner58af2a12006-02-15 07:22:58 +0000737
738 std::string Name;
739 if (NameStr) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000740 Name = *NameStr; // Copy string
741 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000742 }
743
744 // See if this global value was forward referenced. If so, recycle the
745 // object.
746 ValID ID;
747 if (!Name.empty()) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000748 ID = ValID::createGlobalName(Name);
Chris Lattner58af2a12006-02-15 07:22:58 +0000749 } else {
Reid Spencer93c40032007-03-19 18:40:50 +0000750 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000751 }
752
753 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
754 // Move the global to the end of the list, from whereever it was
755 // previously inserted.
756 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
757 CurModule.CurrentModule->getGlobalList().remove(GV);
758 CurModule.CurrentModule->getGlobalList().push_back(GV);
759 GV->setInitializer(Initializer);
760 GV->setLinkage(Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000761 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000762 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000763 GV->setThreadLocal(IsThreadLocal);
Chris Lattner58af2a12006-02-15 07:22:58 +0000764 InsertValue(GV, CurModule.Values);
765 return GV;
766 }
767
Reid Spenceref9b9a72007-02-05 20:47:22 +0000768 // If this global has a name
Chris Lattner58af2a12006-02-15 07:22:58 +0000769 if (!Name.empty()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000770 // if the global we're parsing has an initializer (is a definition) and
771 // has external linkage.
772 if (Initializer && Linkage != GlobalValue::InternalLinkage)
773 // If there is already a global with external linkage with this name
774 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
775 // If we allow this GVar to get created, it will be renamed in the
776 // symbol table because it conflicts with an existing GVar. We can't
777 // allow redefinition of GVars whose linking indicates that their name
778 // must stay the same. Issue the error.
779 GenerateError("Redefinition of global variable named '" + Name +
780 "' of type '" + Ty->getDescription() + "'");
781 return 0;
782 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000783 }
784
785 // Otherwise there is no existing GV to use, create one now.
786 GlobalVariable *GV =
787 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000788 CurModule.CurrentModule, IsThreadLocal, AddressSpace);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000789 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000790 InsertValue(GV, CurModule.Values);
791 return GV;
792}
793
794// setTypeName - Set the specified type to the name given. The name may be
795// null potentially, in which case this is a noop. The string passed in is
796// assumed to be a malloc'd string buffer, and is freed by this function.
797//
798// This function returns true if the type has already been defined, but is
799// allowed to be redefined in the specified context. If the name is a new name
800// for the type plane, it is inserted and false is returned.
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000801static bool setTypeName(const Type *T, std::string *NameStr) {
Reid Spencera9720f52007-02-05 17:04:00 +0000802 assert(!inFunctionScope() && "Can't give types function-local names!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000803 if (NameStr == 0) return false;
804
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000805 std::string Name(*NameStr); // Copy string
806 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000807
808 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000809 if (T == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000810 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000811 return false;
812 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000813
814 // Set the type name, checking for conflicts as we do so.
815 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
816
817 if (AlreadyExists) { // Inserting a name that is already defined???
818 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencera9720f52007-02-05 17:04:00 +0000819 assert(Existing && "Conflict but no matching type?!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000820
821 // There is only one case where this is allowed: when we are refining an
822 // opaque type. In this case, Existing will be an opaque type.
823 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
824 // We ARE replacing an opaque type!
825 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
826 return true;
827 }
828
829 // Otherwise, this is an attempt to redefine a type. That's okay if
830 // the redefinition is identical to the original. This will be so if
831 // Existing and T point to the same Type object. In this one case we
832 // allow the equivalent redefinition.
833 if (Existing == T) return true; // Yes, it's equal.
834
835 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer63c34452007-01-05 21:51:07 +0000836 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000837 T->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +0000838 }
839
840 return false;
841}
842
843//===----------------------------------------------------------------------===//
844// Code for handling upreferences in type names...
845//
846
847// TypeContains - Returns true if Ty directly contains E in it.
848//
849static bool TypeContains(const Type *Ty, const Type *E) {
850 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
851 E) != Ty->subtype_end();
852}
853
854namespace {
855 struct UpRefRecord {
856 // NestingLevel - The number of nesting levels that need to be popped before
857 // this type is resolved.
858 unsigned NestingLevel;
859
860 // LastContainedTy - This is the type at the current binding level for the
861 // type. Every time we reduce the nesting level, this gets updated.
862 const Type *LastContainedTy;
863
864 // UpRefTy - This is the actual opaque type that the upreference is
865 // represented with.
866 OpaqueType *UpRefTy;
867
868 UpRefRecord(unsigned NL, OpaqueType *URTy)
869 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
870 };
871}
872
873// UpRefs - A list of the outstanding upreferences that need to be resolved.
874static std::vector<UpRefRecord> UpRefs;
875
876/// HandleUpRefs - Every time we finish a new layer of types, this function is
877/// called. It loops through the UpRefs vector, which is a list of the
878/// currently active types. For each type, if the up reference is contained in
879/// the newly completed type, we decrement the level count. When the level
880/// count reaches zero, the upreferenced type is the type that is passed in:
881/// thus we can complete the cycle.
882///
883static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner224f84f2006-08-18 17:34:45 +0000884 // If Ty isn't abstract, or if there are no up-references in it, then there is
885 // nothing to resolve here.
886 if (!ty->isAbstract() || UpRefs.empty()) return ty;
887
Chris Lattner58af2a12006-02-15 07:22:58 +0000888 PATypeHolder Ty(ty);
889 UR_OUT("Type '" << Ty->getDescription() <<
890 "' newly formed. Resolving upreferences.\n" <<
891 UpRefs.size() << " upreferences active!\n");
892
893 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
894 // to zero), we resolve them all together before we resolve them to Ty. At
895 // the end of the loop, if there is anything to resolve to Ty, it will be in
896 // this variable.
897 OpaqueType *TypeToResolve = 0;
898
899 for (unsigned i = 0; i != UpRefs.size(); ++i) {
900 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
901 << UpRefs[i].second->getDescription() << ") = "
902 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
903 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
904 // Decrement level of upreference
905 unsigned Level = --UpRefs[i].NestingLevel;
906 UpRefs[i].LastContainedTy = Ty;
907 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
908 if (Level == 0) { // Upreference should be resolved!
909 if (!TypeToResolve) {
910 TypeToResolve = UpRefs[i].UpRefTy;
911 } else {
912 UR_OUT(" * Resolving upreference for "
913 << UpRefs[i].second->getDescription() << "\n";
914 std::string OldName = UpRefs[i].UpRefTy->getDescription());
915 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
916 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
917 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
918 }
919 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
920 --i; // Do not skip the next element...
921 }
922 }
923 }
924
925 if (TypeToResolve) {
926 UR_OUT(" * Resolving upreference for "
927 << UpRefs[i].second->getDescription() << "\n";
928 std::string OldName = TypeToResolve->getDescription());
929 TypeToResolve->refineAbstractTypeTo(Ty);
930 }
931
932 return Ty;
933}
934
Chris Lattner58af2a12006-02-15 07:22:58 +0000935//===----------------------------------------------------------------------===//
936// RunVMAsmParser - Define an interface to this parser
937//===----------------------------------------------------------------------===//
938//
Reid Spencer14310612006-12-31 05:40:51 +0000939static Module* RunParser(Module * M);
940
Duncan Sandsdc024672007-11-27 13:23:08 +0000941Module *llvm::RunVMAsmParser(llvm::MemoryBuffer *MB) {
942 InitLLLexer(MB);
943 Module *M = RunParser(new Module(LLLgetFilename()));
944 FreeLexer();
945 return M;
Chris Lattner58af2a12006-02-15 07:22:58 +0000946}
947
948%}
949
950%union {
951 llvm::Module *ModuleVal;
952 llvm::Function *FunctionVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000953 llvm::BasicBlock *BasicBlockVal;
954 llvm::TerminatorInst *TermInstVal;
955 llvm::Instruction *InstVal;
Reid Spencera132e042006-12-03 05:46:11 +0000956 llvm::Constant *ConstVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000957
Reid Spencera132e042006-12-03 05:46:11 +0000958 const llvm::Type *PrimType;
Reid Spencer14310612006-12-31 05:40:51 +0000959 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencera132e042006-12-03 05:46:11 +0000960 llvm::PATypeHolder *TypeVal;
961 llvm::Value *ValueVal;
Reid Spencera132e042006-12-03 05:46:11 +0000962 std::vector<llvm::Value*> *ValueList;
Reid Spencer14310612006-12-31 05:40:51 +0000963 llvm::ArgListType *ArgList;
964 llvm::TypeWithAttrs TypeWithAttrs;
965 llvm::TypeWithAttrsList *TypeWithAttrsList;
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000966 llvm::ParamList *ParamList;
Reid Spencer14310612006-12-31 05:40:51 +0000967
Chris Lattner58af2a12006-02-15 07:22:58 +0000968 // Represent the RHS of PHI node
Reid Spencera132e042006-12-03 05:46:11 +0000969 std::list<std::pair<llvm::Value*,
970 llvm::BasicBlock*> > *PHIList;
Chris Lattner58af2a12006-02-15 07:22:58 +0000971 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencera132e042006-12-03 05:46:11 +0000972 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner58af2a12006-02-15 07:22:58 +0000973
974 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000975 llvm::GlobalValue::VisibilityTypes Visibility;
Dale Johannesen222ebf72008-02-19 21:40:51 +0000976 llvm::ParameterAttributes ParamAttrs;
Reid Spencer38c91a92007-02-28 02:24:54 +0000977 llvm::APInt *APIntVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000978 int64_t SInt64Val;
979 uint64_t UInt64Val;
980 int SIntVal;
981 unsigned UIntVal;
Dale Johannesen43421b32007-09-06 18:13:44 +0000982 llvm::APFloat *FPVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000983 bool BoolVal;
984
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000985 std::string *StrVal; // This memory must be deleted
986 llvm::ValID ValIDVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000987
Reid Spencera132e042006-12-03 05:46:11 +0000988 llvm::Instruction::BinaryOps BinaryOpVal;
989 llvm::Instruction::TermOps TermOpVal;
990 llvm::Instruction::MemoryOps MemOpVal;
991 llvm::Instruction::CastOps CastOpVal;
992 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencera132e042006-12-03 05:46:11 +0000993 llvm::ICmpInst::Predicate IPredicate;
994 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner58af2a12006-02-15 07:22:58 +0000995}
996
Reid Spencer14310612006-12-31 05:40:51 +0000997%type <ModuleVal> Module
Chris Lattner58af2a12006-02-15 07:22:58 +0000998%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
999%type <BasicBlockVal> BasicBlock InstructionList
1000%type <TermInstVal> BBTerminatorInst
1001%type <InstVal> Inst InstVal MemoryInst
Anton Korobeynikov38e09802007-04-28 13:48:45 +00001002%type <ConstVal> ConstVal ConstExpr AliaseeRef
Chris Lattner58af2a12006-02-15 07:22:58 +00001003%type <ConstVector> ConstVector
1004%type <ArgList> ArgList ArgListH
Chris Lattner58af2a12006-02-15 07:22:58 +00001005%type <PHIList> PHIList
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001006%type <ParamList> ParamList // For call param lists & GEP indices
Reid Spencer14310612006-12-31 05:40:51 +00001007%type <ValueList> IndexList // For GEP indices
1008%type <TypeList> TypeListI
1009%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer218ded22007-01-05 17:07:23 +00001010%type <TypeWithAttrs> ArgType
Chris Lattner58af2a12006-02-15 07:22:58 +00001011%type <JumpTable> JumpTable
1012%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001013%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattner58af2a12006-02-15 07:22:58 +00001014%type <BoolVal> OptVolatile // 'volatile' or not
1015%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1016%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencer14310612006-12-31 05:40:51 +00001017%type <Linkage> GVInternalLinkage GVExternalLinkage
1018%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001019%type <Linkage> AliasLinkage
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001020%type <Visibility> GVVisibilityStyle
Chris Lattner58af2a12006-02-15 07:22:58 +00001021
1022// ValueRef - Unresolved reference to a definition or BB
1023%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1024%type <ValueVal> ResolvedVal // <type> <valref> pair
Devang Patel7990dc72008-02-20 22:40:23 +00001025%type <ValueList> ReturnedVal
Chris Lattner58af2a12006-02-15 07:22:58 +00001026// Tokens and types for handling constant integer values
1027//
1028// ESINT64VAL - A negative number within long long range
1029%token <SInt64Val> ESINT64VAL
1030
1031// EUINT64VAL - A positive number within uns. long long range
1032%token <UInt64Val> EUINT64VAL
Chris Lattner58af2a12006-02-15 07:22:58 +00001033
Reid Spencer38c91a92007-02-28 02:24:54 +00001034// ESAPINTVAL - A negative number with arbitrary precision
1035%token <APIntVal> ESAPINTVAL
1036
1037// EUAPINTVAL - A positive number with arbitrary precision
1038%token <APIntVal> EUAPINTVAL
1039
Reid Spencer41dff5e2007-01-26 08:05:27 +00001040%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattner58af2a12006-02-15 07:22:58 +00001041%token <FPVal> FPVAL // Float or Double constant
1042
1043// Built in types...
Reid Spencer218ded22007-01-05 17:07:23 +00001044%type <TypeVal> Types ResultTypes
Reid Spencer14310612006-12-31 05:40:51 +00001045%type <PrimType> IntType FPType PrimType // Classifications
Reid Spencer6f407902007-01-13 05:00:46 +00001046%token <PrimType> VOID INTTYPE
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001047%token <PrimType> FLOAT DOUBLE X86_FP80 FP128 PPC_FP128 LABEL
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001048%token TYPE
Chris Lattner58af2a12006-02-15 07:22:58 +00001049
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001050
Reid Spencered951ea2007-05-19 07:22:10 +00001051%token<StrVal> LOCALVAR GLOBALVAR LABELSTR
1052%token<StrVal> STRINGCONSTANT ATSTRINGCONSTANT PCTSTRINGCONSTANT
Reid Spencer41dff5e2007-01-26 08:05:27 +00001053%type <StrVal> LocalName OptLocalName OptLocalAssign
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001054%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001055%type <StrVal> OptSection SectionString OptGC
Chris Lattner58af2a12006-02-15 07:22:58 +00001056
Christopher Lambbf3348d2007-12-12 08:45:45 +00001057%type <UIntVal> OptAlign OptCAlign OptAddrSpace
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001058
Reid Spencer3d6b71e2007-04-09 01:56:05 +00001059%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001060%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
Reid Spencer14310612006-12-31 05:40:51 +00001061%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001062%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001063%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN ADDRSPACE
Chris Lattner58af2a12006-02-15 07:22:58 +00001064%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001065%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Nick Lewycky7e93e162008-03-10 05:01:34 +00001066%token DATALAYOUT UNWINDS
Chris Lattner58af2a12006-02-15 07:22:58 +00001067%type <UIntVal> OptCallingConv
Reid Spencer218ded22007-01-05 17:07:23 +00001068%type <ParamAttrs> OptParamAttrs ParamAttr
1069%type <ParamAttrs> OptFuncAttrs FuncAttr
Chris Lattner58af2a12006-02-15 07:22:58 +00001070
1071// Basic Block Terminating Operators
1072%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
1073
1074// Binary Operators
Reid Spencere4d87aa2006-12-23 06:05:41 +00001075%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencer3ed469c2006-11-02 20:25:50 +00001076%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer832254e2007-02-02 02:16:23 +00001077%token <BinaryOpVal> SHL LSHR ASHR
1078
Reid Spencera132e042006-12-03 05:46:11 +00001079%token <OtherOpVal> ICMP FCMP
Reid Spencera132e042006-12-03 05:46:11 +00001080%type <IPredicate> IPredicates
Reid Spencera132e042006-12-03 05:46:11 +00001081%type <FPredicate> FPredicates
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001082%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1083%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner58af2a12006-02-15 07:22:58 +00001084
1085// Memory Instructions
1086%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1087
Reid Spencer3da59db2006-11-27 01:05:10 +00001088// Cast Operators
1089%type <CastOpVal> CastOps
1090%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1091%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1092
Chris Lattner58af2a12006-02-15 07:22:58 +00001093// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001094%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattnerd5efe842006-04-08 01:18:56 +00001095%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Devang Patel5a970972008-02-19 22:27:01 +00001096%token <OtherOpVal> GETRESULT
Chris Lattner58af2a12006-02-15 07:22:58 +00001097
Reid Spencer218ded22007-01-05 17:07:23 +00001098// Function Attributes
Reid Spencerb8f85052007-07-31 03:50:36 +00001099%token SIGNEXT ZEROEXT NORETURN INREG SRET NOUNWIND NOALIAS BYVAL NEST
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001100%token READNONE READONLY GC
Chris Lattner58af2a12006-02-15 07:22:58 +00001101
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001102// Visibility Styles
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001103%token DEFAULT HIDDEN PROTECTED
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001104
Chris Lattner58af2a12006-02-15 07:22:58 +00001105%start Module
1106%%
1107
Chris Lattner58af2a12006-02-15 07:22:58 +00001108
Chris Lattner58af2a12006-02-15 07:22:58 +00001109// Operations that are notably excluded from this list include:
1110// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1111//
Reid Spencer3ed469c2006-11-02 20:25:50 +00001112ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer832254e2007-02-02 02:16:23 +00001113LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Reid Spencer3da59db2006-11-27 01:05:10 +00001114CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1115 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer832254e2007-02-02 02:16:23 +00001116
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001117IPredicates
Reid Spencer4012e832006-12-04 05:24:24 +00001118 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001119 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1120 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1121 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1122 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1123 ;
1124
1125FPredicates
1126 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1127 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1128 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1129 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1130 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1131 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1132 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1133 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1134 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1135 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001136
1137// These are some types that allow classification if we only want a particular
1138// thing... for example, only a signed, unsigned, or integral type.
Reid Spencera54b7cb2007-01-12 07:05:14 +00001139IntType : INTTYPE;
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001140FPType : FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80;
Chris Lattner58af2a12006-02-15 07:22:58 +00001141
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001142LocalName : LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001143OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1144
Christopher Lambbf3348d2007-12-12 08:45:45 +00001145OptAddrSpace : ADDRSPACE '(' EUINT64VAL ')' { $$=$3; }
1146 | /*empty*/ { $$=0; };
1147
Reid Spencer41dff5e2007-01-26 08:05:27 +00001148/// OptLocalAssign - Value producing statements have an optional assignment
1149/// component.
1150OptLocalAssign : LocalName '=' {
1151 $$ = $1;
1152 CHECK_FOR_ERROR
1153 }
1154 | /*empty*/ {
1155 $$ = 0;
1156 CHECK_FOR_ERROR
1157 };
1158
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001159GlobalName : GLOBALVAR | ATSTRINGCONSTANT ;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001160
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001161OptGlobalAssign : GlobalAssign
Chris Lattner58af2a12006-02-15 07:22:58 +00001162 | /*empty*/ {
1163 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001164 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001165 };
1166
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001167GlobalAssign : GlobalName '=' {
1168 $$ = $1;
1169 CHECK_FOR_ERROR
Chris Lattner6cdc6822007-04-26 05:31:05 +00001170 };
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001171
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001172GVInternalLinkage
1173 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1174 | WEAK { $$ = GlobalValue::WeakLinkage; }
1175 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1176 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1177 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1178 ;
1179
1180GVExternalLinkage
1181 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1182 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1183 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1184 ;
1185
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001186GVVisibilityStyle
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001187 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1188 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1189 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1190 | PROTECTED { $$ = GlobalValue::ProtectedVisibility; }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001191 ;
1192
Reid Spencer14310612006-12-31 05:40:51 +00001193FunctionDeclareLinkage
1194 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1195 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1196 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001197 ;
1198
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001199FunctionDefineLinkage
Reid Spencer14310612006-12-31 05:40:51 +00001200 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1201 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001202 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1203 | WEAK { $$ = GlobalValue::WeakLinkage; }
1204 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001205 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001206
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001207AliasLinkage
1208 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1209 | WEAK { $$ = GlobalValue::WeakLinkage; }
1210 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1211 ;
1212
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001213OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1214 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001215 FASTCC_TOK { $$ = CallingConv::Fast; } |
1216 COLDCC_TOK { $$ = CallingConv::Cold; } |
1217 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1218 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1219 CC_TOK EUINT64VAL {
Chris Lattner58af2a12006-02-15 07:22:58 +00001220 if ((unsigned)$2 != $2)
Reid Spencerb5334b02007-02-05 10:18:06 +00001221 GEN_ERROR("Calling conv too large");
Chris Lattner58af2a12006-02-15 07:22:58 +00001222 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001223 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001224 };
1225
Reid Spencerb8f85052007-07-31 03:50:36 +00001226ParamAttr : ZEROEXT { $$ = ParamAttr::ZExt; }
1227 | ZEXT { $$ = ParamAttr::ZExt; }
1228 | SIGNEXT { $$ = ParamAttr::SExt; }
Chris Lattnerce5f24e2007-07-05 17:26:49 +00001229 | SEXT { $$ = ParamAttr::SExt; }
1230 | INREG { $$ = ParamAttr::InReg; }
1231 | SRET { $$ = ParamAttr::StructRet; }
1232 | NOALIAS { $$ = ParamAttr::NoAlias; }
Reid Spencerb8f85052007-07-31 03:50:36 +00001233 | BYVAL { $$ = ParamAttr::ByVal; }
1234 | NEST { $$ = ParamAttr::Nest; }
Dale Johannesendc6c0f12008-02-22 17:50:51 +00001235 | ALIGN EUINT64VAL { $$ =
1236 ParamAttr::constructAlignmentFromInt($2); }
Reid Spencer14310612006-12-31 05:40:51 +00001237 ;
1238
Reid Spencer18da0722007-04-11 02:44:20 +00001239OptParamAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer218ded22007-01-05 17:07:23 +00001240 | OptParamAttrs ParamAttr {
Reid Spencer7b5d4662007-04-09 06:16:21 +00001241 $$ = $1 | $2;
Reid Spencer14310612006-12-31 05:40:51 +00001242 }
1243 ;
1244
Reid Spencer18da0722007-04-11 02:44:20 +00001245FuncAttr : NORETURN { $$ = ParamAttr::NoReturn; }
1246 | NOUNWIND { $$ = ParamAttr::NoUnwind; }
Reid Spencerb8f85052007-07-31 03:50:36 +00001247 | ZEROEXT { $$ = ParamAttr::ZExt; }
1248 | SIGNEXT { $$ = ParamAttr::SExt; }
Duncan Sandsdc024672007-11-27 13:23:08 +00001249 | READNONE { $$ = ParamAttr::ReadNone; }
1250 | READONLY { $$ = ParamAttr::ReadOnly; }
Reid Spencer218ded22007-01-05 17:07:23 +00001251 ;
1252
Reid Spencer18da0722007-04-11 02:44:20 +00001253OptFuncAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer218ded22007-01-05 17:07:23 +00001254 | OptFuncAttrs FuncAttr {
Reid Spencer7b5d4662007-04-09 06:16:21 +00001255 $$ = $1 | $2;
Reid Spencer218ded22007-01-05 17:07:23 +00001256 }
Reid Spencer14310612006-12-31 05:40:51 +00001257 ;
1258
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001259OptGC : /* empty */ { $$ = 0; }
1260 | GC STRINGCONSTANT {
1261 $$ = $2;
1262 }
1263 ;
1264
Chris Lattner58af2a12006-02-15 07:22:58 +00001265// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1266// a comma before it.
1267OptAlign : /*empty*/ { $$ = 0; } |
1268 ALIGN EUINT64VAL {
1269 $$ = $2;
1270 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001271 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001272 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001273};
1274OptCAlign : /*empty*/ { $$ = 0; } |
1275 ',' ALIGN EUINT64VAL {
1276 $$ = $3;
1277 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001278 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001279 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001280};
1281
1282
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001283
Chris Lattner58af2a12006-02-15 07:22:58 +00001284SectionString : SECTION STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001285 for (unsigned i = 0, e = $2->length(); i != e; ++i)
1286 if ((*$2)[i] == '"' || (*$2)[i] == '\\')
Reid Spencerb5334b02007-02-05 10:18:06 +00001287 GEN_ERROR("Invalid character in section name");
Chris Lattner58af2a12006-02-15 07:22:58 +00001288 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001289 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001290};
1291
1292OptSection : /*empty*/ { $$ = 0; } |
1293 SectionString { $$ = $1; };
1294
1295// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1296// is set to be the global we are processing.
1297//
1298GlobalVarAttributes : /* empty */ {} |
1299 ',' GlobalVarAttribute GlobalVarAttributes {};
1300GlobalVarAttribute : SectionString {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001301 CurGV->setSection(*$1);
1302 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001303 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001304 }
1305 | ALIGN EUINT64VAL {
1306 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001307 GEN_ERROR("Alignment must be a power of two");
Chris Lattner58af2a12006-02-15 07:22:58 +00001308 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001309 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001310 };
1311
1312//===----------------------------------------------------------------------===//
1313// Types includes all predefined types... except void, because it can only be
Reid Spencer14310612006-12-31 05:40:51 +00001314// used in specific contexts (function returning void for example).
Chris Lattner58af2a12006-02-15 07:22:58 +00001315
1316// Derived types are added later...
1317//
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001318PrimType : INTTYPE | FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80 | LABEL ;
Reid Spencer14310612006-12-31 05:40:51 +00001319
1320Types
1321 : OPAQUE {
Reid Spencera132e042006-12-03 05:46:11 +00001322 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001323 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001324 }
1325 | PrimType {
Reid Spencera132e042006-12-03 05:46:11 +00001326 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001327 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00001328 }
Christopher Lambbf3348d2007-12-12 08:45:45 +00001329 | Types OptAddrSpace '*' { // Pointer type?
Reid Spencer14310612006-12-31 05:40:51 +00001330 if (*$1 == Type::LabelTy)
1331 GEN_ERROR("Cannot form a pointer to a basic block");
Christopher Lambbf3348d2007-12-12 08:45:45 +00001332 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1, $2)));
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001333 delete $1;
1334 CHECK_FOR_ERROR
1335 }
Reid Spencer14310612006-12-31 05:40:51 +00001336 | SymbolicValueRef { // Named types are also simple types...
1337 const Type* tmp = getTypeVal($1);
1338 CHECK_FOR_ERROR
1339 $$ = new PATypeHolder(tmp);
1340 }
1341 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerb5334b02007-02-05 10:18:06 +00001342 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattner58af2a12006-02-15 07:22:58 +00001343 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1344 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencera132e042006-12-03 05:46:11 +00001345 $$ = new PATypeHolder(OT);
Chris Lattner58af2a12006-02-15 07:22:58 +00001346 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001347 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001348 }
Reid Spencer218ded22007-01-05 17:07:23 +00001349 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001350 // Allow but ignore attributes on function types; this permits auto-upgrade.
1351 // FIXME: remove in LLVM 3.0.
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001352 const Type* RetTy = *$1;
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001353 if (!(RetTy->isFirstClassType() || RetTy == Type::VoidTy ||
1354 isa<OpaqueType>(RetTy)))
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001355 GEN_ERROR("LLVM Functions cannot return aggregates");
1356
Chris Lattner58af2a12006-02-15 07:22:58 +00001357 std::vector<const Type*> Params;
Reid Spencer7b5d4662007-04-09 06:16:21 +00001358 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001359 for (; I != E; ++I ) {
Reid Spencer66728ef2007-03-20 01:13:36 +00001360 const Type *Ty = I->Ty->get();
Reid Spencer66728ef2007-03-20 01:13:36 +00001361 Params.push_back(Ty);
Reid Spencer14310612006-12-31 05:40:51 +00001362 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001363
Chris Lattner58af2a12006-02-15 07:22:58 +00001364 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1365 if (isVarArg) Params.pop_back();
1366
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001367 for (unsigned i = 0; i != Params.size(); ++i)
1368 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1369 GEN_ERROR("Function arguments must be value types!");
1370
1371 CHECK_FOR_ERROR
1372
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001373 FunctionType *FT = FunctionType::get(RetTy, Params, isVarArg);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001374 delete $3; // Delete the argument list
Reid Spencer14310612006-12-31 05:40:51 +00001375 delete $1; // Delete the return type handle
1376 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer61c83e02006-08-18 08:43:06 +00001377 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001378 }
Reid Spencer218ded22007-01-05 17:07:23 +00001379 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001380 // Allow but ignore attributes on function types; this permits auto-upgrade.
1381 // FIXME: remove in LLVM 3.0.
Reid Spencer14310612006-12-31 05:40:51 +00001382 std::vector<const Type*> Params;
Reid Spencer7b5d4662007-04-09 06:16:21 +00001383 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001384 for ( ; I != E; ++I ) {
Reid Spencer66728ef2007-03-20 01:13:36 +00001385 const Type* Ty = I->Ty->get();
Reid Spencer66728ef2007-03-20 01:13:36 +00001386 Params.push_back(Ty);
Reid Spencer14310612006-12-31 05:40:51 +00001387 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001388
Reid Spencer14310612006-12-31 05:40:51 +00001389 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1390 if (isVarArg) Params.pop_back();
1391
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001392 for (unsigned i = 0; i != Params.size(); ++i)
1393 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1394 GEN_ERROR("Function arguments must be value types!");
1395
1396 CHECK_FOR_ERROR
1397
Duncan Sandsdc024672007-11-27 13:23:08 +00001398 FunctionType *FT = FunctionType::get($1, Params, isVarArg);
Reid Spencer218ded22007-01-05 17:07:23 +00001399 delete $3; // Delete the argument list
Reid Spencer14310612006-12-31 05:40:51 +00001400 $$ = new PATypeHolder(HandleUpRefs(FT));
1401 CHECK_FOR_ERROR
1402 }
1403
1404 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Reid Spencera132e042006-12-03 05:46:11 +00001405 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1406 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001407 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001408 }
Chris Lattner32980692007-02-19 07:44:24 +00001409 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencera132e042006-12-03 05:46:11 +00001410 const llvm::Type* ElemTy = $4->get();
1411 if ((unsigned)$2 != $2)
1412 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner42a75512007-01-15 02:27:26 +00001413 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencer9d6565a2007-02-15 02:26:10 +00001414 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001415 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencera132e042006-12-03 05:46:11 +00001416 delete $4;
1417 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001418 }
1419 | '{' TypeListI '}' { // Structure type?
1420 std::vector<const Type*> Elements;
Reid Spencera132e042006-12-03 05:46:11 +00001421 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattner58af2a12006-02-15 07:22:58 +00001422 E = $2->end(); I != E; ++I)
Reid Spencera132e042006-12-03 05:46:11 +00001423 Elements.push_back(*I);
Chris Lattner58af2a12006-02-15 07:22:58 +00001424
Reid Spencera132e042006-12-03 05:46:11 +00001425 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattner58af2a12006-02-15 07:22:58 +00001426 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001427 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001428 }
1429 | '{' '}' { // Empty structure type?
Reid Spencera132e042006-12-03 05:46:11 +00001430 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001431 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001432 }
Andrew Lenharth6353e052006-12-08 18:07:09 +00001433 | '<' '{' TypeListI '}' '>' {
1434 std::vector<const Type*> Elements;
1435 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1436 E = $3->end(); I != E; ++I)
1437 Elements.push_back(*I);
1438
1439 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1440 delete $3;
1441 CHECK_FOR_ERROR
1442 }
1443 | '<' '{' '}' '>' { // Empty structure type?
1444 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1445 CHECK_FOR_ERROR
1446 }
Reid Spencer14310612006-12-31 05:40:51 +00001447 ;
1448
1449ArgType
Duncan Sandsdc024672007-11-27 13:23:08 +00001450 : Types OptParamAttrs {
1451 // Allow but ignore attributes on function types; this permits auto-upgrade.
1452 // FIXME: remove in LLVM 3.0.
Reid Spencer14310612006-12-31 05:40:51 +00001453 $$.Ty = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00001454 $$.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00001455 }
1456 ;
1457
Reid Spencer218ded22007-01-05 17:07:23 +00001458ResultTypes
1459 : Types {
Reid Spencer14310612006-12-31 05:40:51 +00001460 if (!UpRefs.empty())
1461 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Devang Patel20071732008-02-23 01:17:37 +00001462 if (!(*$1)->isFirstClassType() && !isa<StructType>($1->get()))
Reid Spencerb5334b02007-02-05 10:18:06 +00001463 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer218ded22007-01-05 17:07:23 +00001464 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00001465 }
Reid Spencer218ded22007-01-05 17:07:23 +00001466 | VOID {
1467 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencer14310612006-12-31 05:40:51 +00001468 }
1469 ;
1470
1471ArgTypeList : ArgType {
1472 $$ = new TypeWithAttrsList();
1473 $$->push_back($1);
1474 CHECK_FOR_ERROR
1475 }
1476 | ArgTypeList ',' ArgType {
1477 ($$=$1)->push_back($3);
1478 CHECK_FOR_ERROR
1479 }
1480 ;
1481
1482ArgTypeListI
1483 : ArgTypeList
1484 | ArgTypeList ',' DOTDOTDOT {
1485 $$=$1;
Reid Spencer18da0722007-04-11 02:44:20 +00001486 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00001487 TWA.Ty = new PATypeHolder(Type::VoidTy);
1488 $$->push_back(TWA);
1489 CHECK_FOR_ERROR
1490 }
1491 | DOTDOTDOT {
1492 $$ = new TypeWithAttrsList;
Reid Spencer18da0722007-04-11 02:44:20 +00001493 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00001494 TWA.Ty = new PATypeHolder(Type::VoidTy);
1495 $$->push_back(TWA);
1496 CHECK_FOR_ERROR
1497 }
1498 | /*empty*/ {
1499 $$ = new TypeWithAttrsList();
Reid Spencer61c83e02006-08-18 08:43:06 +00001500 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001501 };
1502
1503// TypeList - Used for struct declarations and as a basis for function type
1504// declaration type lists
1505//
Reid Spencer14310612006-12-31 05:40:51 +00001506TypeListI : Types {
Reid Spencera132e042006-12-03 05:46:11 +00001507 $$ = new std::list<PATypeHolder>();
Reid Spencer66728ef2007-03-20 01:13:36 +00001508 $$->push_back(*$1);
1509 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001510 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001511 }
Reid Spencer14310612006-12-31 05:40:51 +00001512 | TypeListI ',' Types {
Reid Spencer66728ef2007-03-20 01:13:36 +00001513 ($$=$1)->push_back(*$3);
1514 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001515 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001516 };
1517
Chris Lattner58af2a12006-02-15 07:22:58 +00001518// ConstVal - The various declarations that go into the constant pool. This
1519// production is used ONLY to represent constants that show up AFTER a 'const',
1520// 'constant' or 'global' token at global scope. Constants that can be inlined
1521// into other expressions (such as integers and constexprs) are handled by the
1522// ResolvedVal, ValueRef and ConstValueRef productions.
1523//
1524ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001525 if (!UpRefs.empty())
1526 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001527 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001528 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001529 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001530 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001531 const Type *ETy = ATy->getElementType();
1532 int NumElements = ATy->getNumElements();
1533
1534 // Verify that we have the correct size...
1535 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001536 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001537 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerb5334b02007-02-05 10:18:06 +00001538 itostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001539
1540 // Verify all elements are correct type!
1541 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001542 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001543 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001544 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001545 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001546 }
1547
Reid Spencera132e042006-12-03 05:46:11 +00001548 $$ = ConstantArray::get(ATy, *$3);
1549 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001550 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001551 }
1552 | Types '[' ']' {
Reid Spencer14310612006-12-31 05:40:51 +00001553 if (!UpRefs.empty())
1554 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001555 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001556 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001557 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001558 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001559
1560 int NumElements = ATy->getNumElements();
1561 if (NumElements != -1 && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001562 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencerb5334b02007-02-05 10:18:06 +00001563 " arguments, but has size of " + itostr(NumElements) +"");
Reid Spencera132e042006-12-03 05:46:11 +00001564 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1565 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001566 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001567 }
1568 | Types 'c' STRINGCONSTANT {
Reid Spencer14310612006-12-31 05:40:51 +00001569 if (!UpRefs.empty())
1570 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001571 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001572 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001573 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001574 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001575
1576 int NumElements = ATy->getNumElements();
1577 const Type *ETy = ATy->getElementType();
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001578 if (NumElements != -1 && NumElements != int($3->length()))
Reid Spencer61c83e02006-08-18 08:43:06 +00001579 GEN_ERROR("Can't build string constant of size " +
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001580 itostr((int)($3->length())) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001581 " when array has size " + itostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001582 std::vector<Constant*> Vals;
Reid Spencer14310612006-12-31 05:40:51 +00001583 if (ETy == Type::Int8Ty) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001584 for (unsigned i = 0; i < $3->length(); ++i)
1585 Vals.push_back(ConstantInt::get(ETy, (*$3)[i]));
Chris Lattner58af2a12006-02-15 07:22:58 +00001586 } else {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001587 delete $3;
Reid Spencerb5334b02007-02-05 10:18:06 +00001588 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner58af2a12006-02-15 07:22:58 +00001589 }
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001590 delete $3;
Reid Spencera132e042006-12-03 05:46:11 +00001591 $$ = ConstantArray::get(ATy, Vals);
1592 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001593 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001594 }
1595 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001596 if (!UpRefs.empty())
1597 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00001598 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001599 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001600 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001601 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001602 const Type *ETy = PTy->getElementType();
1603 int NumElements = PTy->getNumElements();
1604
1605 // Verify that we have the correct size...
1606 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001607 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001608 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerb5334b02007-02-05 10:18:06 +00001609 itostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001610
1611 // Verify all elements are correct type!
1612 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001613 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001614 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001615 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001616 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001617 }
1618
Reid Spencer9d6565a2007-02-15 02:26:10 +00001619 $$ = ConstantVector::get(PTy, *$3);
Reid Spencera132e042006-12-03 05:46:11 +00001620 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001621 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001622 }
1623 | Types '{' ConstVector '}' {
Reid Spencera132e042006-12-03 05:46:11 +00001624 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001625 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001626 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001627 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001628
1629 if ($3->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001630 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001631
1632 // Check to ensure that constants are compatible with the type initializer!
1633 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencera132e042006-12-03 05:46:11 +00001634 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001635 GEN_ERROR("Expected type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001636 STy->getElementType(i)->getDescription() +
1637 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001638 " of structure initializer");
Chris Lattner58af2a12006-02-15 07:22:58 +00001639
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001640 // Check to ensure that Type is not packed
1641 if (STy->isPacked())
Chris Lattner6cdc6822007-04-26 05:31:05 +00001642 GEN_ERROR("Unpacked Initializer to vector type '" +
1643 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001644
Reid Spencera132e042006-12-03 05:46:11 +00001645 $$ = ConstantStruct::get(STy, *$3);
1646 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001647 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001648 }
1649 | Types '{' '}' {
Reid Spencer14310612006-12-31 05:40:51 +00001650 if (!UpRefs.empty())
1651 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001652 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001653 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001654 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001655 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001656
1657 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001658 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001659
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001660 // Check to ensure that Type is not packed
1661 if (STy->isPacked())
Chris Lattner6cdc6822007-04-26 05:31:05 +00001662 GEN_ERROR("Unpacked Initializer to vector type '" +
1663 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001664
1665 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1666 delete $1;
1667 CHECK_FOR_ERROR
1668 }
1669 | Types '<' '{' ConstVector '}' '>' {
1670 const StructType *STy = dyn_cast<StructType>($1->get());
1671 if (STy == 0)
1672 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001673 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001674
1675 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001676 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001677
1678 // Check to ensure that constants are compatible with the type initializer!
1679 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1680 if ((*$4)[i]->getType() != STy->getElementType(i))
1681 GEN_ERROR("Expected type '" +
1682 STy->getElementType(i)->getDescription() +
1683 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001684 " of structure initializer");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001685
1686 // Check to ensure that Type is packed
1687 if (!STy->isPacked())
Chris Lattner32980692007-02-19 07:44:24 +00001688 GEN_ERROR("Vector initializer to non-vector type '" +
1689 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001690
1691 $$ = ConstantStruct::get(STy, *$4);
1692 delete $1; delete $4;
1693 CHECK_FOR_ERROR
1694 }
1695 | Types '<' '{' '}' '>' {
1696 if (!UpRefs.empty())
1697 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1698 const StructType *STy = dyn_cast<StructType>($1->get());
1699 if (STy == 0)
1700 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001701 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001702
1703 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001704 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001705
1706 // Check to ensure that Type is packed
1707 if (!STy->isPacked())
Chris Lattner32980692007-02-19 07:44:24 +00001708 GEN_ERROR("Vector initializer to non-vector type '" +
1709 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001710
Reid Spencera132e042006-12-03 05:46:11 +00001711 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1712 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001713 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001714 }
1715 | Types NULL_TOK {
Reid Spencer14310612006-12-31 05:40:51 +00001716 if (!UpRefs.empty())
1717 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001718 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001719 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001720 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001721 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001722
Reid Spencera132e042006-12-03 05:46:11 +00001723 $$ = ConstantPointerNull::get(PTy);
1724 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001725 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001726 }
1727 | Types UNDEF {
Reid Spencer14310612006-12-31 05:40:51 +00001728 if (!UpRefs.empty())
1729 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001730 $$ = UndefValue::get($1->get());
1731 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001732 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001733 }
1734 | Types SymbolicValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00001735 if (!UpRefs.empty())
1736 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001737 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001738 if (Ty == 0)
Devang Patel5a970972008-02-19 22:27:01 +00001739 GEN_ERROR("Global const reference must be a pointer type " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001740
1741 // ConstExprs can exist in the body of a function, thus creating
1742 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencer93c40032007-03-19 18:40:50 +00001743 // the context of a function, getExistingVal will search the functions
Chris Lattner58af2a12006-02-15 07:22:58 +00001744 // symbol table instead of the module symbol table for the global symbol,
1745 // which throws things all off. To get around this, we just tell
Reid Spencer93c40032007-03-19 18:40:50 +00001746 // getExistingVal that we are at global scope here.
Chris Lattner58af2a12006-02-15 07:22:58 +00001747 //
1748 Function *SavedCurFn = CurFun.CurrentFunction;
1749 CurFun.CurrentFunction = 0;
1750
Reid Spencer93c40032007-03-19 18:40:50 +00001751 Value *V = getExistingVal(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001752 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001753
1754 CurFun.CurrentFunction = SavedCurFn;
1755
1756 // If this is an initializer for a constant pointer, which is referencing a
1757 // (currently) undefined variable, create a stub now that shall be replaced
1758 // in the future with the right type of variable.
1759 //
1760 if (V == 0) {
Reid Spencera9720f52007-02-05 17:04:00 +00001761 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001762 const PointerType *PT = cast<PointerType>(Ty);
1763
1764 // First check to see if the forward references value is already created!
1765 PerModuleInfo::GlobalRefsType::iterator I =
1766 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1767
1768 if (I != CurModule.GlobalRefs.end()) {
1769 V = I->second; // Placeholder already exists, use it...
1770 $2.destroy();
1771 } else {
1772 std::string Name;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001773 if ($2.Type == ValID::GlobalName)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001774 Name = $2.getName();
Reid Spencer41dff5e2007-01-26 08:05:27 +00001775 else if ($2.Type != ValID::GlobalID)
1776 GEN_ERROR("Invalid reference to global");
Chris Lattner58af2a12006-02-15 07:22:58 +00001777
1778 // Create the forward referenced global.
1779 GlobalValue *GV;
1780 if (const FunctionType *FTy =
1781 dyn_cast<FunctionType>(PT->getElementType())) {
Chris Lattner6cdc6822007-04-26 05:31:05 +00001782 GV = new Function(FTy, GlobalValue::ExternalWeakLinkage, Name,
Chris Lattner58af2a12006-02-15 07:22:58 +00001783 CurModule.CurrentModule);
1784 } else {
1785 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattner6cdc6822007-04-26 05:31:05 +00001786 GlobalValue::ExternalWeakLinkage, 0,
Chris Lattner58af2a12006-02-15 07:22:58 +00001787 Name, CurModule.CurrentModule);
1788 }
1789
1790 // Keep track of the fact that we have a forward ref to recycle it
1791 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1792 V = GV;
1793 }
1794 }
1795
Reid Spencera132e042006-12-03 05:46:11 +00001796 $$ = cast<GlobalValue>(V);
1797 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001798 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001799 }
1800 | Types ConstExpr {
Reid Spencer14310612006-12-31 05:40:51 +00001801 if (!UpRefs.empty())
1802 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001803 if ($1->get() != $2->getType())
Reid Spencere68853b2007-01-04 00:06:14 +00001804 GEN_ERROR("Mismatched types for constant expression: " +
1805 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001806 $$ = $2;
Reid Spencera132e042006-12-03 05:46:11 +00001807 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001808 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001809 }
1810 | Types ZEROINITIALIZER {
Reid Spencer14310612006-12-31 05:40:51 +00001811 if (!UpRefs.empty())
1812 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001813 const Type *Ty = $1->get();
Chris Lattner58af2a12006-02-15 07:22:58 +00001814 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerb5334b02007-02-05 10:18:06 +00001815 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencera132e042006-12-03 05:46:11 +00001816 $$ = Constant::getNullValue(Ty);
1817 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001818 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00001819 }
Reid Spencer14310612006-12-31 05:40:51 +00001820 | IntType ESINT64VAL { // integral constants
Reid Spencere4d87aa2006-12-23 06:05:41 +00001821 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001822 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencer49d273e2007-03-19 20:40:51 +00001823 $$ = ConstantInt::get($1, $2, true);
Reid Spencer38c91a92007-02-28 02:24:54 +00001824 CHECK_FOR_ERROR
1825 }
1826 | IntType ESAPINTVAL { // arbitrary precision integer constants
1827 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1828 if ($2->getBitWidth() > BitWidth) {
1829 GEN_ERROR("Constant value does not fit in type");
Reid Spencer10794272007-03-01 19:41:47 +00001830 }
1831 $2->sextOrTrunc(BitWidth);
1832 $$ = ConstantInt::get(*$2);
Reid Spencer38c91a92007-02-28 02:24:54 +00001833 delete $2;
Reid Spencere4d87aa2006-12-23 06:05:41 +00001834 CHECK_FOR_ERROR
1835 }
Reid Spencer14310612006-12-31 05:40:51 +00001836 | IntType EUINT64VAL { // integral constants
Reid Spencere4d87aa2006-12-23 06:05:41 +00001837 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001838 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencer49d273e2007-03-19 20:40:51 +00001839 $$ = ConstantInt::get($1, $2, false);
Reid Spencer38c91a92007-02-28 02:24:54 +00001840 CHECK_FOR_ERROR
1841 }
1842 | IntType EUAPINTVAL { // arbitrary precision integer constants
1843 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1844 if ($2->getBitWidth() > BitWidth) {
1845 GEN_ERROR("Constant value does not fit in type");
Reid Spencer10794272007-03-01 19:41:47 +00001846 }
1847 $2->zextOrTrunc(BitWidth);
1848 $$ = ConstantInt::get(*$2);
Reid Spencer38c91a92007-02-28 02:24:54 +00001849 delete $2;
Reid Spencere4d87aa2006-12-23 06:05:41 +00001850 CHECK_FOR_ERROR
1851 }
Reid Spencer6f407902007-01-13 05:00:46 +00001852 | INTTYPE TRUETOK { // Boolean constants
1853 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001854 $$ = ConstantInt::getTrue();
Reid Spencer61c83e02006-08-18 08:43:06 +00001855 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001856 }
Reid Spencer6f407902007-01-13 05:00:46 +00001857 | INTTYPE FALSETOK { // Boolean constants
1858 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001859 $$ = ConstantInt::getFalse();
Reid Spencer61c83e02006-08-18 08:43:06 +00001860 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001861 }
Dale Johannesenea583102007-09-12 03:31:28 +00001862 | FPType FPVAL { // Floating point constants
Dale Johannesen43421b32007-09-06 18:13:44 +00001863 if (!ConstantFP::isValueValidForType($1, *$2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001864 GEN_ERROR("Floating point constant invalid for type");
Dale Johannesenc72cd7e2007-09-11 18:33:39 +00001865 // Lexer has no type info, so builds all float and double FP constants
1866 // as double. Fix this here. Long double is done right.
1867 if (&$2->getSemantics()==&APFloat::IEEEdouble && $1==Type::FloatTy)
Dale Johannesen43421b32007-09-06 18:13:44 +00001868 $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
1869 $$ = ConstantFP::get($1, *$2);
Dale Johannesencdd509a2007-09-07 21:07:57 +00001870 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001871 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001872 };
1873
1874
Reid Spencer3da59db2006-11-27 01:05:10 +00001875ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer14310612006-12-31 05:40:51 +00001876 if (!UpRefs.empty())
1877 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001878 Constant *Val = $3;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001879 const Type *DestTy = $5->get();
1880 if (!CastInst::castIsValid($1, $3, DestTy))
1881 GEN_ERROR("invalid cast opcode for cast from '" +
1882 Val->getType()->getDescription() + "' to '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001883 DestTy->getDescription() + "'");
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001884 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00001885 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00001886 }
1887 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001888 if (!isa<PointerType>($3->getType()))
Reid Spencerb5334b02007-02-05 10:18:06 +00001889 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00001890
Reid Spencera132e042006-12-03 05:46:11 +00001891 const Type *IdxTy =
David Greene5fd22a82007-09-04 18:46:50 +00001892 GetElementPtrInst::getIndexedType($3->getType(), $4->begin(), $4->end(),
Chris Lattner7d9801d2007-02-13 00:58:01 +00001893 true);
Reid Spencera132e042006-12-03 05:46:11 +00001894 if (!IdxTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00001895 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencera132e042006-12-03 05:46:11 +00001896
Chris Lattnerf7469af2007-01-31 04:44:08 +00001897 SmallVector<Constant*, 8> IdxVec;
Reid Spencera132e042006-12-03 05:46:11 +00001898 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1899 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattner58af2a12006-02-15 07:22:58 +00001900 IdxVec.push_back(C);
1901 else
Reid Spencerb5334b02007-02-05 10:18:06 +00001902 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattner58af2a12006-02-15 07:22:58 +00001903
1904 delete $4;
1905
Chris Lattnerf7469af2007-01-31 04:44:08 +00001906 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer61c83e02006-08-18 08:43:06 +00001907 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001908 }
1909 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer4fe16d62007-01-11 18:21:29 +00001910 if ($3->getType() != Type::Int1Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00001911 GEN_ERROR("Select condition must be of boolean type");
Reid Spencera132e042006-12-03 05:46:11 +00001912 if ($5->getType() != $7->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001913 GEN_ERROR("Select operand types must match");
Reid Spencera132e042006-12-03 05:46:11 +00001914 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001915 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001916 }
1917 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001918 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001919 GEN_ERROR("Binary operator types must match");
Reid Spencer1628cec2006-10-26 06:15:43 +00001920 CHECK_FOR_ERROR;
Reid Spencer9eef56f2006-12-05 19:16:11 +00001921 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner58af2a12006-02-15 07:22:58 +00001922 }
1923 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001924 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001925 GEN_ERROR("Logical operator types must match");
Chris Lattner42a75512007-01-15 02:27:26 +00001926 if (!$3->getType()->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001927 if (Instruction::isShift($1) || !isa<VectorType>($3->getType()) ||
1928 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00001929 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00001930 }
Reid Spencera132e042006-12-03 05:46:11 +00001931 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001932 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001933 }
Reid Spencer4012e832006-12-04 05:24:24 +00001934 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1935 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001936 GEN_ERROR("icmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00001937 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00001938 }
Reid Spencer4012e832006-12-04 05:24:24 +00001939 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1940 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001941 GEN_ERROR("fcmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00001942 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00001943 }
Chris Lattner58af2a12006-02-15 07:22:58 +00001944 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001945 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerb5334b02007-02-05 10:18:06 +00001946 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00001947 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001948 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00001949 }
1950 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001951 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00001952 GEN_ERROR("Invalid insertelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00001953 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001954 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00001955 }
1956 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001957 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00001958 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00001959 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001960 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001961 };
1962
Chris Lattnerd25db202006-04-08 03:55:17 +00001963
Chris Lattner58af2a12006-02-15 07:22:58 +00001964// ConstVector - A list of comma separated constants.
1965ConstVector : ConstVector ',' ConstVal {
1966 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001967 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001968 }
1969 | ConstVal {
Reid Spencera132e042006-12-03 05:46:11 +00001970 $$ = new std::vector<Constant*>();
Chris Lattner58af2a12006-02-15 07:22:58 +00001971 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001972 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001973 };
1974
1975
1976// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1977GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
1978
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001979// ThreadLocal
1980ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
1981
Anton Korobeynikov38e09802007-04-28 13:48:45 +00001982// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
1983AliaseeRef : ResultTypes SymbolicValueRef {
1984 const Type* VTy = $1->get();
1985 Value *V = getVal(VTy, $2);
Chris Lattner0275cff2007-08-06 21:00:46 +00001986 CHECK_FOR_ERROR
Anton Korobeynikov38e09802007-04-28 13:48:45 +00001987 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
1988 if (!Aliasee)
1989 GEN_ERROR("Aliases can be created only to global values");
1990
1991 $$ = Aliasee;
1992 CHECK_FOR_ERROR
1993 delete $1;
1994 }
1995 | BITCAST '(' AliaseeRef TO Types ')' {
1996 Constant *Val = $3;
1997 const Type *DestTy = $5->get();
1998 if (!CastInst::castIsValid($1, $3, DestTy))
1999 GEN_ERROR("invalid cast opcode for cast from '" +
2000 Val->getType()->getDescription() + "' to '" +
2001 DestTy->getDescription() + "'");
2002
2003 $$ = ConstantExpr::getCast($1, $3, DestTy);
2004 CHECK_FOR_ERROR
2005 delete $5;
2006 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002007
2008//===----------------------------------------------------------------------===//
2009// Rules to match Modules
2010//===----------------------------------------------------------------------===//
2011
2012// Module rule: Capture the result of parsing the whole file into a result
2013// variable...
2014//
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002015Module
2016 : DefinitionList {
2017 $$ = ParserResult = CurModule.CurrentModule;
2018 CurModule.ModuleDone();
2019 CHECK_FOR_ERROR;
2020 }
2021 | /*empty*/ {
2022 $$ = ParserResult = CurModule.CurrentModule;
2023 CurModule.ModuleDone();
2024 CHECK_FOR_ERROR;
2025 }
2026 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002027
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002028DefinitionList
2029 : Definition
2030 | DefinitionList Definition
2031 ;
2032
2033Definition
Jeff Cohen361c3ef2007-01-21 19:19:31 +00002034 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner58af2a12006-02-15 07:22:58 +00002035 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00002036 CHECK_FOR_ERROR
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002037 }
2038 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer61c83e02006-08-18 08:43:06 +00002039 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002040 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002041 | MODULE ASM_TOK AsmBlock {
Reid Spencer61c83e02006-08-18 08:43:06 +00002042 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002043 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002044 | OptLocalAssign TYPE Types {
Reid Spencer14310612006-12-31 05:40:51 +00002045 if (!UpRefs.empty())
2046 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002047 // Eagerly resolve types. This is not an optimization, this is a
2048 // requirement that is due to the fact that we could have this:
2049 //
2050 // %list = type { %list * }
2051 // %list = type { %list * } ; repeated type decl
2052 //
2053 // If types are not resolved eagerly, then the two types will not be
2054 // determined to be the same type!
2055 //
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002056 ResolveTypeTo($1, *$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002057
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002058 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002059 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002060 // If this is a named type that is not a redefinition, add it to the slot
2061 // table.
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002062 CurModule.Types.push_back(*$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002063 }
Reid Spencera132e042006-12-03 05:46:11 +00002064
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002065 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002066 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002067 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002068 | OptLocalAssign TYPE VOID {
Reid Spencer14310612006-12-31 05:40:51 +00002069 ResolveTypeTo($1, $3);
2070
2071 if (!setTypeName($3, $1) && !$1) {
2072 CHECK_FOR_ERROR
2073 // If this is a named type that is not a redefinition, add it to the slot
2074 // table.
2075 CurModule.Types.push_back($3);
2076 }
2077 CHECK_FOR_ERROR
2078 }
Christopher Lambbf3348d2007-12-12 08:45:45 +00002079 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal
2080 OptAddrSpace {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002081 /* "Externally Visible" Linkage */
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002082 if ($5 == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002083 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002084 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
Christopher Lambbf3348d2007-12-12 08:45:45 +00002085 $2, $4, $5->getType(), $5, $3, $6);
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00002086 CHECK_FOR_ERROR
2087 } GlobalVarAttributes {
2088 CurGV = 0;
2089 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002090 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambbf3348d2007-12-12 08:45:45 +00002091 ConstVal OptAddrSpace {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002092 if ($6 == 0)
2093 GEN_ERROR("Global value initializer is not a constant");
Christopher Lambbf3348d2007-12-12 08:45:45 +00002094 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4, $7);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002095 CHECK_FOR_ERROR
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002096 } GlobalVarAttributes {
2097 CurGV = 0;
2098 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002099 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambbf3348d2007-12-12 08:45:45 +00002100 Types OptAddrSpace {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002101 if (!UpRefs.empty())
2102 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
Christopher Lambbf3348d2007-12-12 08:45:45 +00002103 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4, $7);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002104 CHECK_FOR_ERROR
2105 delete $6;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002106 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002107 CurGV = 0;
2108 CHECK_FOR_ERROR
2109 }
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002110 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002111 std::string Name;
2112 if ($1) {
2113 Name = *$1;
2114 delete $1;
2115 }
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002116 if (Name.empty())
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002117 GEN_ERROR("Alias name cannot be empty");
2118
2119 Constant* Aliasee = $5;
2120 if (Aliasee == 0)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002121 GEN_ERROR(std::string("Invalid aliasee for alias: ") + Name);
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002122
2123 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2124 CurModule.CurrentModule);
2125 GA->setVisibility($2);
2126 InsertValue(GA, CurModule.Values);
Chris Lattner569f7372007-09-10 23:24:14 +00002127
2128
2129 // If there was a forward reference of this alias, resolve it now.
2130
2131 ValID ID;
2132 if (!Name.empty())
2133 ID = ValID::createGlobalName(Name);
2134 else
2135 ID = ValID::createGlobalID(CurModule.Values.size()-1);
2136
2137 if (GlobalValue *FWGV =
2138 CurModule.GetForwardRefForGlobal(GA->getType(), ID)) {
2139 // Replace uses of the fwdref with the actual alias.
2140 FWGV->replaceAllUsesWith(GA);
2141 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(FWGV))
2142 GV->eraseFromParent();
2143 else
2144 cast<Function>(FWGV)->eraseFromParent();
2145 }
2146 ID.destroy();
2147
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002148 CHECK_FOR_ERROR
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002149 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002150 | TARGET TargetDefinition {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002151 CHECK_FOR_ERROR
2152 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002153 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00002154 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002155 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002156 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002157
2158
2159AsmBlock : STRINGCONSTANT {
2160 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattner58af2a12006-02-15 07:22:58 +00002161 if (AsmSoFar.empty())
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002162 CurModule.CurrentModule->setModuleInlineAsm(*$1);
Chris Lattner58af2a12006-02-15 07:22:58 +00002163 else
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002164 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+*$1);
2165 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002166 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002167};
2168
Reid Spencer41dff5e2007-01-26 08:05:27 +00002169TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002170 CurModule.CurrentModule->setTargetTriple(*$3);
2171 delete $3;
John Criswell2f6a8b12006-10-24 19:09:48 +00002172 }
Chris Lattner1ae022f2006-10-22 06:08:13 +00002173 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002174 CurModule.CurrentModule->setDataLayout(*$3);
2175 delete $3;
Owen Anderson1dc69692006-10-18 02:21:48 +00002176 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002177
2178LibrariesDefinition : '[' LibList ']';
2179
2180LibList : LibList ',' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002181 CurModule.CurrentModule->addLibrary(*$3);
2182 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002183 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002184 }
2185 | STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002186 CurModule.CurrentModule->addLibrary(*$1);
2187 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002188 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002189 }
2190 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00002191 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002192 }
2193 ;
2194
2195//===----------------------------------------------------------------------===//
2196// Rules to match Function Headers
2197//===----------------------------------------------------------------------===//
2198
Reid Spencer41dff5e2007-01-26 08:05:27 +00002199ArgListH : ArgListH ',' Types OptParamAttrs OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002200 if (!UpRefs.empty())
2201 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
2202 if (*$3 == Type::VoidTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00002203 GEN_ERROR("void typed arguments are invalid");
Reid Spencer14310612006-12-31 05:40:51 +00002204 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00002205 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002206 $1->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002207 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002208 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002209 | Types OptParamAttrs OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002210 if (!UpRefs.empty())
2211 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2212 if (*$1 == Type::VoidTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00002213 GEN_ERROR("void typed arguments are invalid");
Reid Spencer14310612006-12-31 05:40:51 +00002214 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2215 $$ = new ArgListType;
2216 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002217 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002218 };
2219
2220ArgList : ArgListH {
2221 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002222 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002223 }
2224 | ArgListH ',' DOTDOTDOT {
2225 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002226 struct ArgListEntry E;
2227 E.Ty = new PATypeHolder(Type::VoidTy);
2228 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002229 E.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00002230 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002231 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002232 }
2233 | DOTDOTDOT {
Reid Spencer14310612006-12-31 05:40:51 +00002234 $$ = new ArgListType;
2235 struct ArgListEntry E;
2236 E.Ty = new PATypeHolder(Type::VoidTy);
2237 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002238 E.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00002239 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002240 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002241 }
2242 | /* empty */ {
2243 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00002244 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002245 };
2246
Reid Spencer41dff5e2007-01-26 08:05:27 +00002247FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002248 OptFuncAttrs OptSection OptAlign OptGC {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002249 std::string FunctionName(*$3);
2250 delete $3; // Free strdup'd memory!
Chris Lattner58af2a12006-02-15 07:22:58 +00002251
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002252 // Check the function result for abstractness if this is a define. We should
2253 // have no abstract types at this point
Reid Spencer218ded22007-01-05 17:07:23 +00002254 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2255 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002256
Chris Lattner58af2a12006-02-15 07:22:58 +00002257 std::vector<const Type*> ParamTypeList;
Chris Lattner58d74912008-03-12 17:45:29 +00002258 SmallVector<ParamAttrsWithIndex, 8> Attrs;
2259 if ($7 != ParamAttr::None)
2260 Attrs.push_back(ParamAttrsWithIndex::get(0, $7));
Chris Lattner58af2a12006-02-15 07:22:58 +00002261 if ($5) { // If there are arguments...
Reid Spencer7b5d4662007-04-09 06:16:21 +00002262 unsigned index = 1;
2263 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002264 const Type* Ty = I->Ty->get();
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002265 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2266 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencer14310612006-12-31 05:40:51 +00002267 ParamTypeList.push_back(Ty);
Chris Lattner58d74912008-03-12 17:45:29 +00002268 if (Ty != Type::VoidTy && I->Attrs != ParamAttr::None)
2269 Attrs.push_back(ParamAttrsWithIndex::get(index, I->Attrs));
Reid Spencer14310612006-12-31 05:40:51 +00002270 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002271 }
2272
2273 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2274 if (isVarArg) ParamTypeList.pop_back();
2275
Chris Lattner58d74912008-03-12 17:45:29 +00002276 PAListPtr PAL;
Christopher Lamb5c104242007-04-22 20:09:11 +00002277 if (!Attrs.empty())
Chris Lattner58d74912008-03-12 17:45:29 +00002278 PAL = PAListPtr::get(Attrs.begin(), Attrs.end());
Reid Spencer7b5d4662007-04-09 06:16:21 +00002279
Duncan Sandsdc024672007-11-27 13:23:08 +00002280 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002281 const PointerType *PFT = PointerType::getUnqual(FT);
Reid Spencer218ded22007-01-05 17:07:23 +00002282 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002283
2284 ValID ID;
2285 if (!FunctionName.empty()) {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002286 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattner58af2a12006-02-15 07:22:58 +00002287 } else {
Reid Spencer93c40032007-03-19 18:40:50 +00002288 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002289 }
2290
2291 Function *Fn = 0;
2292 // See if this function was forward referenced. If so, recycle the object.
2293 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2294 // Move the function to the end of the list, from whereever it was
2295 // previously inserted.
2296 Fn = cast<Function>(FWRef);
Chris Lattner58d74912008-03-12 17:45:29 +00002297 assert(Fn->getParamAttrs().isEmpty() &&
2298 "Forward reference has parameter attributes!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002299 CurModule.CurrentModule->getFunctionList().remove(Fn);
2300 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2301 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spenceref9b9a72007-02-05 20:47:22 +00002302 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002303 if (Fn->getFunctionType() != FT ) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002304 // The existing function doesn't have the same type. This is an overload
2305 // error.
2306 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Duncan Sandsdc024672007-11-27 13:23:08 +00002307 } else if (Fn->getParamAttrs() != PAL) {
2308 // The existing function doesn't have the same parameter attributes.
2309 // This is an overload error.
2310 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002311 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
Chris Lattner6cdc6822007-04-26 05:31:05 +00002312 // Neither the existing or the current function is a declaration and they
2313 // have the same name and same type. Clearly this is a redefinition.
2314 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Duncan Sandsdc024672007-11-27 13:23:08 +00002315 } else if (Fn->isDeclaration()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002316 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattner58af2a12006-02-15 07:22:58 +00002317 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2318 AI != AE; ++AI)
2319 AI->setName("");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002320 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002321 } else { // Not already defined?
Chris Lattner6cdc6822007-04-26 05:31:05 +00002322 Fn = new Function(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
Chris Lattner58af2a12006-02-15 07:22:58 +00002323 CurModule.CurrentModule);
2324 InsertValue(Fn, CurModule.Values);
2325 }
2326
2327 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002328
2329 if (CurFun.isDeclare) {
2330 // If we have declaration, always overwrite linkage. This will allow us to
2331 // correctly handle cases, when pointer to function is passed as argument to
2332 // another function.
2333 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002334 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002335 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002336 Fn->setCallingConv($1);
Duncan Sandsdc024672007-11-27 13:23:08 +00002337 Fn->setParamAttrs(PAL);
Reid Spencer218ded22007-01-05 17:07:23 +00002338 Fn->setAlignment($9);
2339 if ($8) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002340 Fn->setSection(*$8);
2341 delete $8;
Chris Lattner58af2a12006-02-15 07:22:58 +00002342 }
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002343 if ($10) {
2344 Fn->setCollector($10->c_str());
2345 delete $10;
2346 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002347
2348 // Add all of the arguments we parsed to the function...
2349 if ($5) { // Is null if empty...
2350 if (isVarArg) { // Nuke the last entry
Reid Spenceref9b9a72007-02-05 20:47:22 +00002351 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0 &&
Reid Spencera9720f52007-02-05 17:04:00 +00002352 "Not a varargs marker!");
Reid Spencer14310612006-12-31 05:40:51 +00002353 delete $5->back().Ty;
Chris Lattner58af2a12006-02-15 07:22:58 +00002354 $5->pop_back(); // Delete the last entry
2355 }
2356 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002357 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencer14310612006-12-31 05:40:51 +00002358 unsigned Idx = 1;
Reid Spenceref9b9a72007-02-05 20:47:22 +00002359 for (ArgListType::iterator I = $5->begin();
2360 I != $5->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencer14310612006-12-31 05:40:51 +00002361 delete I->Ty; // Delete the typeholder...
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002362 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002363 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002364 InsertValue(ArgIt);
Reid Spencer14310612006-12-31 05:40:51 +00002365 Idx++;
Chris Lattner58af2a12006-02-15 07:22:58 +00002366 }
Reid Spencera132e042006-12-03 05:46:11 +00002367
Chris Lattner58af2a12006-02-15 07:22:58 +00002368 delete $5; // We're now done with the argument list
2369 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002370 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002371};
2372
2373BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2374
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002375FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner58af2a12006-02-15 07:22:58 +00002376 $$ = CurFun.CurrentFunction;
2377
2378 // Make sure that we keep track of the linkage type even if there was a
2379 // previous "declare".
2380 $$->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002381 $$->setVisibility($2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002382};
2383
2384END : ENDTOK | '}'; // Allow end of '}' to end a function
2385
2386Function : BasicBlockList END {
2387 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002388 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002389};
2390
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002391FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencer14310612006-12-31 05:40:51 +00002392 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002393 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002394 $$ = CurFun.CurrentFunction;
2395 CurFun.FunctionDone();
2396 CHECK_FOR_ERROR
2397 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002398
2399//===----------------------------------------------------------------------===//
2400// Rules to match Basic Blocks
2401//===----------------------------------------------------------------------===//
2402
2403OptSideEffect : /* empty */ {
2404 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002405 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002406 }
2407 | SIDEEFFECT {
2408 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002409 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002410 };
2411
2412ConstValueRef : ESINT64VAL { // A reference to a direct constant
2413 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002414 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002415 }
2416 | EUINT64VAL {
2417 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002418 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002419 }
2420 | FPVAL { // Perhaps it's an FP constant?
2421 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002422 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002423 }
2424 | TRUETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002425 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002426 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002427 }
2428 | FALSETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002429 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002430 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002431 }
2432 | NULL_TOK {
2433 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002434 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002435 }
2436 | UNDEF {
2437 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002438 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002439 }
2440 | ZEROINITIALIZER { // A vector zero constant.
2441 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002442 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002443 }
2444 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencera132e042006-12-03 05:46:11 +00002445 const Type *ETy = (*$2)[0]->getType();
Chris Lattner58af2a12006-02-15 07:22:58 +00002446 int NumElements = $2->size();
2447
Reid Spencer9d6565a2007-02-15 02:26:10 +00002448 VectorType* pt = VectorType::get(ETy, NumElements);
Chris Lattner58af2a12006-02-15 07:22:58 +00002449 PATypeHolder* PTy = new PATypeHolder(
Reid Spencera132e042006-12-03 05:46:11 +00002450 HandleUpRefs(
Reid Spencer9d6565a2007-02-15 02:26:10 +00002451 VectorType::get(
Reid Spencera132e042006-12-03 05:46:11 +00002452 ETy,
2453 NumElements)
2454 )
2455 );
Chris Lattner58af2a12006-02-15 07:22:58 +00002456
2457 // Verify all elements are correct type!
2458 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00002459 if (ETy != (*$2)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002460 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00002461 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencera132e042006-12-03 05:46:11 +00002462 (*$2)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00002463 }
2464
Reid Spencer9d6565a2007-02-15 02:26:10 +00002465 $$ = ValID::create(ConstantVector::get(pt, *$2));
Chris Lattner58af2a12006-02-15 07:22:58 +00002466 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002467 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002468 }
2469 | ConstExpr {
Reid Spencera132e042006-12-03 05:46:11 +00002470 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002471 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002472 }
2473 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002474 $$ = ValID::createInlineAsm(*$3, *$5, $2);
2475 delete $3;
2476 delete $5;
Reid Spencer61c83e02006-08-18 08:43:06 +00002477 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002478 };
2479
2480// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2481// another value.
2482//
Reid Spencer41dff5e2007-01-26 08:05:27 +00002483SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2484 $$ = ValID::createLocalID($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002485 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002486 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002487 | GLOBALVAL_ID {
2488 $$ = ValID::createGlobalID($1);
2489 CHECK_FOR_ERROR
2490 }
2491 | LocalName { // Is it a named reference...?
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002492 $$ = ValID::createLocalName(*$1);
2493 delete $1;
Reid Spencer41dff5e2007-01-26 08:05:27 +00002494 CHECK_FOR_ERROR
2495 }
2496 | GlobalName { // Is it a named reference...?
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002497 $$ = ValID::createGlobalName(*$1);
2498 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002499 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002500 };
2501
2502// ValueRef - A reference to a definition... either constant or symbolic
2503ValueRef : SymbolicValueRef | ConstValueRef;
2504
2505
2506// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2507// type immediately preceeds the value reference, and allows complex constant
2508// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2509ResolvedVal : Types ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002510 if (!UpRefs.empty())
2511 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2512 $$ = getVal(*$1, $2);
2513 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002514 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00002515 }
2516 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002517
Devang Patel7990dc72008-02-20 22:40:23 +00002518ReturnedVal : ResolvedVal {
2519 $$ = new std::vector<Value *>();
2520 $$->push_back($1);
2521 CHECK_FOR_ERROR
2522 }
Devang Patel6bfc63b2008-02-23 00:38:56 +00002523 | ReturnedVal ',' ResolvedVal {
Devang Patel7990dc72008-02-20 22:40:23 +00002524 ($$=$1)->push_back($3);
2525 CHECK_FOR_ERROR
2526 };
2527
Chris Lattner58af2a12006-02-15 07:22:58 +00002528BasicBlockList : BasicBlockList BasicBlock {
2529 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002530 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002531 }
2532 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
2533 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002534 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002535 };
2536
2537
2538// Basic blocks are terminated by branching instructions:
2539// br, br/cc, switch, ret
2540//
Reid Spencer41dff5e2007-01-26 08:05:27 +00002541BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner58af2a12006-02-15 07:22:58 +00002542 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002543 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002544 InsertValue($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002545 $1->getInstList().push_back($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002546 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002547 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002548 };
2549
2550InstructionList : InstructionList Inst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002551 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2552 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2553 if (CI2->getParent() == 0)
2554 $1->getInstList().push_back(CI2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002555 $1->getInstList().push_back($2);
2556 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002557 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002558 }
Reid Spencer93c40032007-03-19 18:40:50 +00002559 | /* empty */ { // Empty space between instruction lists
Devang Patel67909432008-03-03 18:58:47 +00002560 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum), 0);
2561 CHECK_FOR_ERROR
2562 }
Nick Lewycky7e93e162008-03-10 05:01:34 +00002563 | UNWINDS TO ValueRef { // Only the unwind to block
2564 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum), getBBVal($3));
Reid Spencer61c83e02006-08-18 08:43:06 +00002565 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002566 }
Reid Spencer93c40032007-03-19 18:40:50 +00002567 | LABELSTR { // Labelled (named) basic block
Devang Patel67909432008-03-03 18:58:47 +00002568 $$ = defineBBVal(ValID::createLocalName(*$1), 0);
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002569 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002570 CHECK_FOR_ERROR
Devang Patel67909432008-03-03 18:58:47 +00002571 }
Nick Lewycky7e93e162008-03-10 05:01:34 +00002572 | LABELSTR UNWINDS TO ValueRef {
2573 $$ = defineBBVal(ValID::createLocalName(*$1), getBBVal($4));
Devang Patel67909432008-03-03 18:58:47 +00002574 delete $1;
2575 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002576 };
2577
Devang Patel7990dc72008-02-20 22:40:23 +00002578BBTerminatorInst :
2579 RET ReturnedVal { // Return with a result...
Devang Patelb82b7f22008-02-26 22:17:48 +00002580 ValueList &VL = *$2;
Devang Patel13b823c2008-02-26 23:19:08 +00002581 assert(!VL.empty() && "Invalid ret operands!");
2582 $$ = new ReturnInst(&VL[0], VL.size());
Devang Patel7990dc72008-02-20 22:40:23 +00002583 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002584 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002585 }
Reid Spencer93c40032007-03-19 18:40:50 +00002586 | RET VOID { // Return with no result...
Chris Lattner58af2a12006-02-15 07:22:58 +00002587 $$ = new ReturnInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002588 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002589 }
Reid Spencer93c40032007-03-19 18:40:50 +00002590 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002591 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002592 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002593 $$ = new BranchInst(tmpBB);
Reid Spencer93c40032007-03-19 18:40:50 +00002594 } // Conditional Branch...
Reid Spencer6f407902007-01-13 05:00:46 +00002595 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
2596 assert(cast<IntegerType>($2)->getBitWidth() == 1 && "Not Bool?");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002597 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002598 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002599 BasicBlock* tmpBBB = getBBVal($9);
2600 CHECK_FOR_ERROR
Reid Spencer4fe16d62007-01-11 18:21:29 +00002601 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002602 CHECK_FOR_ERROR
2603 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattner58af2a12006-02-15 07:22:58 +00002604 }
2605 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002606 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002607 CHECK_FOR_ERROR
2608 BasicBlock* tmpBB = getBBVal($6);
2609 CHECK_FOR_ERROR
2610 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002611 $$ = S;
2612
2613 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2614 E = $8->end();
2615 for (; I != E; ++I) {
2616 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2617 S->addCase(CI, I->second);
2618 else
Reid Spencerb5334b02007-02-05 10:18:06 +00002619 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattner58af2a12006-02-15 07:22:58 +00002620 }
2621 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002622 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002623 }
2624 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002625 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002626 CHECK_FOR_ERROR
2627 BasicBlock* tmpBB = getBBVal($6);
2628 CHECK_FOR_ERROR
2629 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattner58af2a12006-02-15 07:22:58 +00002630 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002631 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002632 }
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002633 | INVOKE OptCallingConv ResultTypes ValueRef '(' ParamList ')' OptFuncAttrs
Chris Lattner58af2a12006-02-15 07:22:58 +00002634 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattner58af2a12006-02-15 07:22:58 +00002635
Reid Spencer14310612006-12-31 05:40:51 +00002636 // Handle the short syntax
2637 const PointerType *PFTy = 0;
2638 const FunctionType *Ty = 0;
Reid Spencer218ded22007-01-05 17:07:23 +00002639 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00002640 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2641 // Pull out the types of all of the arguments...
2642 std::vector<const Type*> ParamTypes;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002643 ParamList::iterator I = $6->begin(), E = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002644 for (; I != E; ++I) {
Reid Spencer14310612006-12-31 05:40:51 +00002645 const Type *Ty = I->Val->getType();
2646 if (Ty == Type::VoidTy)
2647 GEN_ERROR("Short call syntax cannot be used with varargs");
2648 ParamTypes.push_back(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002649 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002650 Ty = FunctionType::get($3->get(), ParamTypes, false);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002651 PFTy = PointerType::getUnqual(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002652 }
2653
Reid Spencer66728ef2007-03-20 01:13:36 +00002654 delete $3;
2655
Chris Lattner58af2a12006-02-15 07:22:58 +00002656 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002657 CHECK_FOR_ERROR
Reid Spencer218ded22007-01-05 17:07:23 +00002658 BasicBlock *Normal = getBBVal($11);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002659 CHECK_FOR_ERROR
Reid Spencer218ded22007-01-05 17:07:23 +00002660 BasicBlock *Except = getBBVal($14);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002661 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002662
Chris Lattner58d74912008-03-12 17:45:29 +00002663 SmallVector<ParamAttrsWithIndex, 8> Attrs;
2664 if ($8 != ParamAttr::None)
2665 Attrs.push_back(ParamAttrsWithIndex::get(0, $8));
Duncan Sandsdc024672007-11-27 13:23:08 +00002666
Reid Spencer14310612006-12-31 05:40:51 +00002667 // Check the arguments
2668 ValueList Args;
2669 if ($6->empty()) { // Has no arguments?
2670 // Make sure no arguments is a good thing!
2671 if (Ty->getNumParams() != 0)
2672 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00002673 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00002674 } else { // Has arguments?
2675 // Loop through FunctionType's arguments and ensure they are specified
2676 // correctly!
Chris Lattner58af2a12006-02-15 07:22:58 +00002677 FunctionType::param_iterator I = Ty->param_begin();
2678 FunctionType::param_iterator E = Ty->param_end();
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002679 ParamList::iterator ArgI = $6->begin(), ArgE = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002680 unsigned index = 1;
Chris Lattner58af2a12006-02-15 07:22:58 +00002681
Duncan Sandsdc024672007-11-27 13:23:08 +00002682 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002683 if (ArgI->Val->getType() != *I)
2684 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00002685 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00002686 Args.push_back(ArgI->Val);
Chris Lattner58d74912008-03-12 17:45:29 +00002687 if (ArgI->Attrs != ParamAttr::None)
2688 Attrs.push_back(ParamAttrsWithIndex::get(index, ArgI->Attrs));
Reid Spencer14310612006-12-31 05:40:51 +00002689 }
Reid Spencera132e042006-12-03 05:46:11 +00002690
Reid Spencer14310612006-12-31 05:40:51 +00002691 if (Ty->isVarArg()) {
2692 if (I == E)
Chris Lattner38905612008-02-19 04:36:25 +00002693 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002694 Args.push_back(ArgI->Val); // push the remaining varargs
Chris Lattner58d74912008-03-12 17:45:29 +00002695 if (ArgI->Attrs != ParamAttr::None)
2696 Attrs.push_back(ParamAttrsWithIndex::get(index, ArgI->Attrs));
Chris Lattner38905612008-02-19 04:36:25 +00002697 }
Reid Spencer14310612006-12-31 05:40:51 +00002698 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00002699 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00002700 }
Reid Spencer14310612006-12-31 05:40:51 +00002701
Chris Lattner58d74912008-03-12 17:45:29 +00002702 PAListPtr PAL;
Duncan Sandsdc024672007-11-27 13:23:08 +00002703 if (!Attrs.empty())
Chris Lattner58d74912008-03-12 17:45:29 +00002704 PAL = PAListPtr::get(Attrs.begin(), Attrs.end());
Duncan Sandsdc024672007-11-27 13:23:08 +00002705
Reid Spencer14310612006-12-31 05:40:51 +00002706 // Create the InvokeInst
Chris Lattner58d74912008-03-12 17:45:29 +00002707 InvokeInst *II = new InvokeInst(V, Normal, Except, Args.begin(),Args.end());
Reid Spencer14310612006-12-31 05:40:51 +00002708 II->setCallingConv($2);
Duncan Sandsdc024672007-11-27 13:23:08 +00002709 II->setParamAttrs(PAL);
Reid Spencer14310612006-12-31 05:40:51 +00002710 $$ = II;
Chris Lattner58af2a12006-02-15 07:22:58 +00002711 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002712 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002713 }
2714 | UNWIND {
2715 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002716 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002717 }
2718 | UNREACHABLE {
2719 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002720 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002721 };
2722
2723
2724
2725JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2726 $$ = $1;
Reid Spencer93c40032007-03-19 18:40:50 +00002727 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002728 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002729 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002730 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00002731
Reid Spencer5b7e7532006-09-28 19:28:24 +00002732 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002733 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002734 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002735 }
2736 | IntType ConstValueRef ',' LABEL ValueRef {
2737 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencer93c40032007-03-19 18:40:50 +00002738 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002739 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002740
2741 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002742 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00002743
Reid Spencer5b7e7532006-09-28 19:28:24 +00002744 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002745 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002746 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002747 };
2748
Reid Spencer41dff5e2007-01-26 08:05:27 +00002749Inst : OptLocalAssign InstVal {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002750 // Is this definition named?? if so, assign the name...
2751 setValueName($2, $1);
2752 CHECK_FOR_ERROR
2753 InsertValue($2);
2754 $$ = $2;
2755 CHECK_FOR_ERROR
2756 };
2757
Chris Lattner58af2a12006-02-15 07:22:58 +00002758
2759PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer14310612006-12-31 05:40:51 +00002760 if (!UpRefs.empty())
2761 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002762 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencera132e042006-12-03 05:46:11 +00002763 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002764 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002765 BasicBlock* tmpBB = getBBVal($5);
2766 CHECK_FOR_ERROR
2767 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencera132e042006-12-03 05:46:11 +00002768 delete $1;
Chris Lattner58af2a12006-02-15 07:22:58 +00002769 }
2770 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2771 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002772 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002773 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002774 BasicBlock* tmpBB = getBBVal($6);
2775 CHECK_FOR_ERROR
2776 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002777 };
2778
2779
Duncan Sandsdc024672007-11-27 13:23:08 +00002780ParamList : Types OptParamAttrs ValueRef OptParamAttrs {
2781 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Reid Spencer14310612006-12-31 05:40:51 +00002782 if (!UpRefs.empty())
2783 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2784 // Used for call and invoke instructions
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002785 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00002786 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getVal($1->get(), $3);
Reid Spencer14310612006-12-31 05:40:51 +00002787 $$->push_back(E);
Reid Spencer66728ef2007-03-20 01:13:36 +00002788 delete $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002789 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002790 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002791 | LABEL OptParamAttrs ValueRef OptParamAttrs {
2792 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002793 // Labels are only valid in ASMs
2794 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00002795 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getBBVal($3);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002796 $$->push_back(E);
Duncan Sandsdc024672007-11-27 13:23:08 +00002797 CHECK_FOR_ERROR
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002798 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002799 | ParamList ',' Types OptParamAttrs ValueRef OptParamAttrs {
2800 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Reid Spencer14310612006-12-31 05:40:51 +00002801 if (!UpRefs.empty())
2802 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002803 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002804 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getVal($3->get(), $5);
Reid Spencer14310612006-12-31 05:40:51 +00002805 $$->push_back(E);
Reid Spencer66728ef2007-03-20 01:13:36 +00002806 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002807 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00002808 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002809 | ParamList ',' LABEL OptParamAttrs ValueRef OptParamAttrs {
2810 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002811 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002812 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getBBVal($5);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002813 $$->push_back(E);
2814 CHECK_FOR_ERROR
2815 }
2816 | /*empty*/ { $$ = new ParamList(); };
Chris Lattner58af2a12006-02-15 07:22:58 +00002817
Reid Spencer14310612006-12-31 05:40:51 +00002818IndexList // Used for gep instructions and constant expressions
Reid Spencerc6c59fd2006-12-31 21:47:02 +00002819 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencer14310612006-12-31 05:40:51 +00002820 | IndexList ',' ResolvedVal {
2821 $$ = $1;
2822 $$->push_back($3);
2823 CHECK_FOR_ERROR
2824 }
Reid Spencerc6c59fd2006-12-31 21:47:02 +00002825 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002826
2827OptTailCall : TAIL CALL {
2828 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002829 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002830 }
2831 | CALL {
2832 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002833 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002834 };
2835
Chris Lattner58af2a12006-02-15 07:22:58 +00002836InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002837 if (!UpRefs.empty())
2838 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002839 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencer9d6565a2007-02-15 02:26:10 +00002840 !isa<VectorType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002841 GEN_ERROR(
Reid Spencerb5334b02007-02-05 10:18:06 +00002842 "Arithmetic operator requires integer, FP, or packed operands");
Reid Spencera132e042006-12-03 05:46:11 +00002843 Value* val1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002844 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002845 Value* val2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002846 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002847 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002848 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002849 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00002850 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002851 }
2852 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002853 if (!UpRefs.empty())
2854 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002855 if (!(*$2)->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00002856 if (Instruction::isShift($1) || !isa<VectorType>($2->get()) ||
2857 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00002858 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00002859 }
Reid Spencera132e042006-12-03 05:46:11 +00002860 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002861 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002862 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002863 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002864 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002865 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002866 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00002867 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002868 }
Reid Spencera132e042006-12-03 05:46:11 +00002869 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002870 if (!UpRefs.empty())
2871 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002872 if (isa<VectorType>((*$3).get()))
Chris Lattner32980692007-02-19 07:44:24 +00002873 GEN_ERROR("Vector types not supported by icmp instruction");
Reid Spencera132e042006-12-03 05:46:11 +00002874 Value* tmpVal1 = getVal(*$3, $4);
2875 CHECK_FOR_ERROR
2876 Value* tmpVal2 = getVal(*$3, $6);
2877 CHECK_FOR_ERROR
2878 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2879 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002880 GEN_ERROR("icmp operator returned null");
Reid Spencer66728ef2007-03-20 01:13:36 +00002881 delete $3;
Reid Spencera132e042006-12-03 05:46:11 +00002882 }
2883 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002884 if (!UpRefs.empty())
2885 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002886 if (isa<VectorType>((*$3).get()))
Chris Lattner32980692007-02-19 07:44:24 +00002887 GEN_ERROR("Vector types not supported by fcmp instruction");
Reid Spencera132e042006-12-03 05:46:11 +00002888 Value* tmpVal1 = getVal(*$3, $4);
2889 CHECK_FOR_ERROR
2890 Value* tmpVal2 = getVal(*$3, $6);
2891 CHECK_FOR_ERROR
2892 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2893 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002894 GEN_ERROR("fcmp operator returned null");
Reid Spencer66728ef2007-03-20 01:13:36 +00002895 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00002896 }
Reid Spencer3da59db2006-11-27 01:05:10 +00002897 | CastOps ResolvedVal TO Types {
Reid Spencer14310612006-12-31 05:40:51 +00002898 if (!UpRefs.empty())
2899 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00002900 Value* Val = $2;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00002901 const Type* DestTy = $4->get();
2902 if (!CastInst::castIsValid($1, Val, DestTy))
2903 GEN_ERROR("invalid cast opcode for cast from '" +
2904 Val->getType()->getDescription() + "' to '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00002905 DestTy->getDescription() + "'");
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00002906 $$ = CastInst::create($1, Val, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00002907 delete $4;
Chris Lattner58af2a12006-02-15 07:22:58 +00002908 }
2909 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer4fe16d62007-01-11 18:21:29 +00002910 if ($2->getType() != Type::Int1Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00002911 GEN_ERROR("select condition must be boolean");
Reid Spencera132e042006-12-03 05:46:11 +00002912 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002913 GEN_ERROR("select value types should match");
Reid Spencera132e042006-12-03 05:46:11 +00002914 $$ = new SelectInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002915 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002916 }
2917 | VAARG ResolvedVal ',' Types {
Reid Spencer14310612006-12-31 05:40:51 +00002918 if (!UpRefs.empty())
2919 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00002920 $$ = new VAArgInst($2, *$4);
2921 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002922 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002923 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002924 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002925 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerb5334b02007-02-05 10:18:06 +00002926 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00002927 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002928 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002929 }
2930 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002931 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00002932 GEN_ERROR("Invalid insertelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00002933 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002934 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002935 }
Chris Lattnerd5efe842006-04-08 01:18:56 +00002936 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002937 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00002938 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00002939 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002940 CHECK_FOR_ERROR
Chris Lattnerd5efe842006-04-08 01:18:56 +00002941 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002942 | PHI_TOK PHIList {
2943 const Type *Ty = $2->front().first->getType();
2944 if (!Ty->isFirstClassType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002945 GEN_ERROR("PHI node operands must be of first class type");
Chris Lattner58af2a12006-02-15 07:22:58 +00002946 $$ = new PHINode(Ty);
2947 ((PHINode*)$$)->reserveOperandSpace($2->size());
2948 while ($2->begin() != $2->end()) {
2949 if ($2->front().first->getType() != Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00002950 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattner58af2a12006-02-15 07:22:58 +00002951 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
2952 $2->pop_front();
2953 }
2954 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00002955 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002956 }
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002957 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ParamList ')'
Reid Spencer218ded22007-01-05 17:07:23 +00002958 OptFuncAttrs {
Reid Spencer14310612006-12-31 05:40:51 +00002959
2960 // Handle the short syntax
Reid Spencer3da59db2006-11-27 01:05:10 +00002961 const PointerType *PFTy = 0;
2962 const FunctionType *Ty = 0;
Reid Spencer218ded22007-01-05 17:07:23 +00002963 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00002964 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2965 // Pull out the types of all of the arguments...
2966 std::vector<const Type*> ParamTypes;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002967 ParamList::iterator I = $6->begin(), E = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002968 for (; I != E; ++I) {
Reid Spencer14310612006-12-31 05:40:51 +00002969 const Type *Ty = I->Val->getType();
2970 if (Ty == Type::VoidTy)
2971 GEN_ERROR("Short call syntax cannot be used with varargs");
2972 ParamTypes.push_back(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002973 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002974 Ty = FunctionType::get($3->get(), ParamTypes, false);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002975 PFTy = PointerType::getUnqual(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002976 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002977
Chris Lattner58af2a12006-02-15 07:22:58 +00002978 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002979 CHECK_FOR_ERROR
Chris Lattner6cdc6822007-04-26 05:31:05 +00002980
Reid Spencer7780acb2007-04-16 06:56:07 +00002981 // Check for call to invalid intrinsic to avoid crashing later.
2982 if (Function *theF = dyn_cast<Function>(V)) {
Reid Spencered48de22007-04-16 22:02:23 +00002983 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
Reid Spencer36fdde12007-04-16 20:35:38 +00002984 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
2985 !theF->getIntrinsicID(true))
Reid Spencer7780acb2007-04-16 06:56:07 +00002986 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
2987 theF->getName() + "'");
2988 }
2989
Duncan Sandsdc024672007-11-27 13:23:08 +00002990 // Set up the ParamAttrs for the function
Chris Lattner58d74912008-03-12 17:45:29 +00002991 SmallVector<ParamAttrsWithIndex, 8> Attrs;
2992 if ($8 != ParamAttr::None)
2993 Attrs.push_back(ParamAttrsWithIndex::get(0, $8));
Reid Spencer14310612006-12-31 05:40:51 +00002994 // Check the arguments
2995 ValueList Args;
2996 if ($6->empty()) { // Has no arguments?
Chris Lattner58af2a12006-02-15 07:22:58 +00002997 // Make sure no arguments is a good thing!
2998 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002999 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00003000 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00003001 } else { // Has arguments?
3002 // Loop through FunctionType's arguments and ensure they are specified
Duncan Sandsdc024672007-11-27 13:23:08 +00003003 // correctly. Also, gather any parameter attributes.
Chris Lattner58af2a12006-02-15 07:22:58 +00003004 FunctionType::param_iterator I = Ty->param_begin();
3005 FunctionType::param_iterator E = Ty->param_end();
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003006 ParamList::iterator ArgI = $6->begin(), ArgE = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00003007 unsigned index = 1;
Chris Lattner58af2a12006-02-15 07:22:58 +00003008
Duncan Sandsdc024672007-11-27 13:23:08 +00003009 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00003010 if (ArgI->Val->getType() != *I)
3011 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00003012 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00003013 Args.push_back(ArgI->Val);
Chris Lattner58d74912008-03-12 17:45:29 +00003014 if (ArgI->Attrs != ParamAttr::None)
3015 Attrs.push_back(ParamAttrsWithIndex::get(index, ArgI->Attrs));
Reid Spencer14310612006-12-31 05:40:51 +00003016 }
3017 if (Ty->isVarArg()) {
3018 if (I == E)
Chris Lattner38905612008-02-19 04:36:25 +00003019 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00003020 Args.push_back(ArgI->Val); // push the remaining varargs
Chris Lattner58d74912008-03-12 17:45:29 +00003021 if (ArgI->Attrs != ParamAttr::None)
3022 Attrs.push_back(ParamAttrsWithIndex::get(index, ArgI->Attrs));
Chris Lattner38905612008-02-19 04:36:25 +00003023 }
Reid Spencer14310612006-12-31 05:40:51 +00003024 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00003025 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00003026 }
Duncan Sandsdc024672007-11-27 13:23:08 +00003027
3028 // Finish off the ParamAttrs and check them
Chris Lattner58d74912008-03-12 17:45:29 +00003029 PAListPtr PAL;
Duncan Sandsdc024672007-11-27 13:23:08 +00003030 if (!Attrs.empty())
Chris Lattner58d74912008-03-12 17:45:29 +00003031 PAL = PAListPtr::get(Attrs.begin(), Attrs.end());
Duncan Sandsdc024672007-11-27 13:23:08 +00003032
Reid Spencer14310612006-12-31 05:40:51 +00003033 // Create the call node
David Greene718fda32007-08-01 03:59:32 +00003034 CallInst *CI = new CallInst(V, Args.begin(), Args.end());
Reid Spencer14310612006-12-31 05:40:51 +00003035 CI->setTailCall($1);
3036 CI->setCallingConv($2);
Duncan Sandsdc024672007-11-27 13:23:08 +00003037 CI->setParamAttrs(PAL);
Reid Spencer14310612006-12-31 05:40:51 +00003038 $$ = CI;
Chris Lattner58af2a12006-02-15 07:22:58 +00003039 delete $6;
Reid Spencer41dff5e2007-01-26 08:05:27 +00003040 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00003041 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003042 }
3043 | MemoryInst {
3044 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00003045 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003046 };
3047
Chris Lattner58af2a12006-02-15 07:22:58 +00003048OptVolatile : VOLATILE {
3049 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00003050 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003051 }
3052 | /* empty */ {
3053 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00003054 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003055 };
3056
3057
3058
3059MemoryInst : MALLOC Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003060 if (!UpRefs.empty())
3061 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003062 $$ = new MallocInst(*$2, 0, $3);
3063 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003064 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003065 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003066 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003067 if (!UpRefs.empty())
3068 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003069 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003070 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003071 $$ = new MallocInst(*$2, tmpVal, $6);
3072 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003073 }
3074 | ALLOCA Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003075 if (!UpRefs.empty())
3076 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003077 $$ = new AllocaInst(*$2, 0, $3);
3078 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003079 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003080 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003081 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003082 if (!UpRefs.empty())
3083 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003084 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003085 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003086 $$ = new AllocaInst(*$2, tmpVal, $6);
3087 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003088 }
3089 | FREE ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003090 if (!isa<PointerType>($2->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003091 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerb5334b02007-02-05 10:18:06 +00003092 $2->getType()->getDescription() + "");
Reid Spencera132e042006-12-03 05:46:11 +00003093 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00003094 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003095 }
3096
Christopher Lamb5c104242007-04-22 20:09:11 +00003097 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003098 if (!UpRefs.empty())
3099 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003100 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003101 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003102 (*$3)->getDescription());
3103 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00003104 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003105 (*$3)->getDescription());
3106 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003107 CHECK_FOR_ERROR
Christopher Lamb5c104242007-04-22 20:09:11 +00003108 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencera132e042006-12-03 05:46:11 +00003109 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00003110 }
Christopher Lamb5c104242007-04-22 20:09:11 +00003111 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003112 if (!UpRefs.empty())
3113 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003114 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00003115 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00003116 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003117 (*$5)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00003118 const Type *ElTy = PT->getElementType();
Reid Spencera132e042006-12-03 05:46:11 +00003119 if (ElTy != $3->getType())
3120 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerb5334b02007-02-05 10:18:06 +00003121 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00003122
Reid Spencera132e042006-12-03 05:46:11 +00003123 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003124 CHECK_FOR_ERROR
Christopher Lamb5c104242007-04-22 20:09:11 +00003125 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencera132e042006-12-03 05:46:11 +00003126 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00003127 }
Devang Patelbd41a062008-02-22 19:31:30 +00003128| GETRESULT Types SymbolicValueRef ',' EUINT64VAL {
3129 Value *TmpVal = getVal($2->get(), $3);
Devang Patel5a970972008-02-19 22:27:01 +00003130 if (!GetResultInst::isValidOperands(TmpVal, $5))
3131 GEN_ERROR("Invalid getresult operands");
3132 $$ = new GetResultInst(TmpVal, $5);
Devang Patel6bfc63b2008-02-23 00:38:56 +00003133 delete $2;
Devang Patel5a970972008-02-19 22:27:01 +00003134 CHECK_FOR_ERROR
3135 }
Chris Lattner58af2a12006-02-15 07:22:58 +00003136 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer14310612006-12-31 05:40:51 +00003137 if (!UpRefs.empty())
3138 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003139 if (!isa<PointerType>($2->get()))
Reid Spencerb5334b02007-02-05 10:18:06 +00003140 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00003141
David Greene5fd22a82007-09-04 18:46:50 +00003142 if (!GetElementPtrInst::getIndexedType(*$2, $4->begin(), $4->end(), true))
Reid Spencer61c83e02006-08-18 08:43:06 +00003143 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00003144 (*$2)->getDescription()+ "'");
Reid Spencera132e042006-12-03 05:46:11 +00003145 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003146 CHECK_FOR_ERROR
David Greene5fd22a82007-09-04 18:46:50 +00003147 $$ = new GetElementPtrInst(tmpVal, $4->begin(), $4->end());
Reid Spencera132e042006-12-03 05:46:11 +00003148 delete $2;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003149 delete $4;
Chris Lattner58af2a12006-02-15 07:22:58 +00003150 };
3151
3152
3153%%
Reid Spencer61c83e02006-08-18 08:43:06 +00003154
Reid Spencer14310612006-12-31 05:40:51 +00003155// common code from the two 'RunVMAsmParser' functions
3156static Module* RunParser(Module * M) {
Reid Spencer14310612006-12-31 05:40:51 +00003157 CurModule.CurrentModule = M;
Reid Spencer14310612006-12-31 05:40:51 +00003158 // Check to make sure the parser succeeded
3159 if (yyparse()) {
3160 if (ParserResult)
3161 delete ParserResult;
3162 return 0;
3163 }
3164
Reid Spencer0d60b5a2007-03-30 01:37:39 +00003165 // Emit an error if there are any unresolved types left.
3166 if (!CurModule.LateResolveTypes.empty()) {
3167 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3168 if (DID.Type == ValID::LocalName) {
3169 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3170 } else {
3171 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3172 }
3173 if (ParserResult)
3174 delete ParserResult;
3175 return 0;
3176 }
3177
3178 // Emit an error if there are any unresolved values left.
3179 if (!CurModule.LateResolveValues.empty()) {
3180 Value *V = CurModule.LateResolveValues.back();
3181 std::map<Value*, std::pair<ValID, int> >::iterator I =
3182 CurModule.PlaceHolderInfo.find(V);
3183
3184 if (I != CurModule.PlaceHolderInfo.end()) {
3185 ValID &DID = I->second.first;
3186 if (DID.Type == ValID::LocalName) {
3187 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3188 } else {
3189 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3190 }
3191 if (ParserResult)
3192 delete ParserResult;
3193 return 0;
3194 }
3195 }
3196
Reid Spencer14310612006-12-31 05:40:51 +00003197 // Check to make sure that parsing produced a result
3198 if (!ParserResult)
3199 return 0;
3200
3201 // Reset ParserResult variable while saving its value for the result.
3202 Module *Result = ParserResult;
3203 ParserResult = 0;
3204
3205 return Result;
3206}
3207
Reid Spencer61c83e02006-08-18 08:43:06 +00003208void llvm::GenerateError(const std::string &message, int LineNo) {
Duncan Sandsdc024672007-11-27 13:23:08 +00003209 if (LineNo == -1) LineNo = LLLgetLineNo();
Reid Spencer61c83e02006-08-18 08:43:06 +00003210 // TODO: column number in exception
3211 if (TheParseError)
Duncan Sandsdc024672007-11-27 13:23:08 +00003212 TheParseError->setError(LLLgetFilename(), message, LineNo);
Reid Spencer61c83e02006-08-18 08:43:06 +00003213 TriggerError = 1;
3214}
3215
Chris Lattner58af2a12006-02-15 07:22:58 +00003216int yyerror(const char *ErrorMsg) {
Duncan Sandsdc024672007-11-27 13:23:08 +00003217 std::string where = LLLgetFilename() + ":" + utostr(LLLgetLineNo()) + ": ";
Reid Spenceref9b9a72007-02-05 20:47:22 +00003218 std::string errMsg = where + "error: " + std::string(ErrorMsg);
Duncan Sandsdc024672007-11-27 13:23:08 +00003219 if (yychar != YYEMPTY && yychar != 0) {
3220 errMsg += " while reading token: '";
3221 errMsg += std::string(LLLgetTokenStart(),
3222 LLLgetTokenStart()+LLLgetTokenLength()) + "'";
3223 }
Reid Spencer61c83e02006-08-18 08:43:06 +00003224 GenerateError(errMsg);
Chris Lattner58af2a12006-02-15 07:22:58 +00003225 return 0;
3226}