blob: 5a3f17c1470a313515763edd60e15302de08c1f9 [file] [log] [blame]
Chris Lattner58af2a12006-02-15 07:22:58 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner58af2a12006-02-15 07:22:58 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the bison parser for LLVM assembly languages files.
11//
12//===----------------------------------------------------------------------===//
13
14%{
15#include "ParserInternals.h"
16#include "llvm/CallingConv.h"
17#include "llvm/InlineAsm.h"
18#include "llvm/Instructions.h"
19#include "llvm/Module.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000020#include "llvm/ValueSymbolTable.h"
Chandler Carruth02202192007-08-04 01:56:21 +000021#include "llvm/AutoUpgrade.h"
Chris Lattner58af2a12006-02-15 07:22:58 +000022#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer14310612006-12-31 05:40:51 +000023#include "llvm/Support/CommandLine.h"
Chris Lattnerf7469af2007-01-31 04:44:08 +000024#include "llvm/ADT/SmallVector.h"
Chris Lattner58af2a12006-02-15 07:22:58 +000025#include "llvm/ADT/STLExtras.h"
26#include "llvm/Support/MathExtras.h"
Reid Spencer481169e2006-12-01 00:33:46 +000027#include "llvm/Support/Streams.h"
Chris Lattner58af2a12006-02-15 07:22:58 +000028#include <algorithm>
Chris Lattner58af2a12006-02-15 07:22:58 +000029#include <list>
Chris Lattner8adde282007-02-11 21:40:10 +000030#include <map>
Chris Lattner58af2a12006-02-15 07:22:58 +000031#include <utility>
32
Reid Spencere4f47592006-08-18 17:32:55 +000033// The following is a gross hack. In order to rid the libAsmParser library of
34// exceptions, we have to have a way of getting the yyparse function to go into
35// an error situation. So, whenever we want an error to occur, the GenerateError
36// function (see bottom of file) sets TriggerError. Then, at the end of each
37// production in the grammer we use CHECK_FOR_ERROR which will invoke YYERROR
38// (a goto) to put YACC in error state. Furthermore, several calls to
39// GenerateError are made from inside productions and they must simulate the
40// previous exception behavior by exiting the production immediately. We have
41// replaced these with the GEN_ERROR macro which calls GeneratError and then
42// immediately invokes YYERROR. This would be so much cleaner if it was a
43// recursive descent parser.
Reid Spencer61c83e02006-08-18 08:43:06 +000044static bool TriggerError = false;
Reid Spencerf63697d2006-10-09 17:36:59 +000045#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYABORT; } }
Reid Spencer61c83e02006-08-18 08:43:06 +000046#define GEN_ERROR(msg) { GenerateError(msg); YYERROR; }
47
Chris Lattner58af2a12006-02-15 07:22:58 +000048int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
49int yylex(); // declaration" of xxx warnings.
50int yyparse();
Chris Lattner58af2a12006-02-15 07:22:58 +000051using namespace llvm;
52
53static Module *ParserResult;
54
55// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
56// relating to upreferences in the input stream.
57//
58//#define DEBUG_UPREFS 1
59#ifdef DEBUG_UPREFS
Bill Wendlinge8156192006-12-07 01:30:32 +000060#define UR_OUT(X) cerr << X
Chris Lattner58af2a12006-02-15 07:22:58 +000061#else
62#define UR_OUT(X)
63#endif
64
65#define YYERROR_VERBOSE 1
66
Chris Lattner58af2a12006-02-15 07:22:58 +000067static GlobalVariable *CurGV;
68
69
70// This contains info used when building the body of a function. It is
71// destroyed when the function is completed.
72//
73typedef std::vector<Value *> ValueList; // Numbered defs
Reid Spencer14310612006-12-31 05:40:51 +000074
Chris Lattner58af2a12006-02-15 07:22:58 +000075static void
Reid Spencer93c40032007-03-19 18:40:50 +000076ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers=0);
Chris Lattner58af2a12006-02-15 07:22:58 +000077
78static struct PerModuleInfo {
79 Module *CurrentModule;
Reid Spencer93c40032007-03-19 18:40:50 +000080 ValueList Values; // Module level numbered definitions
81 ValueList LateResolveValues;
Reid Spencer861d9d62006-11-28 07:29:44 +000082 std::vector<PATypeHolder> Types;
83 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner58af2a12006-02-15 07:22:58 +000084
85 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Chris Lattner0ad19702006-06-21 16:53:00 +000086 /// how they were referenced and on which line of the input they came from so
Chris Lattner58af2a12006-02-15 07:22:58 +000087 /// that we can resolve them later and print error messages as appropriate.
88 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
89
90 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
91 // references to global values. Global values may be referenced before they
92 // are defined, and if so, the temporary object that they represent is held
93 // here. This is used for forward references of GlobalValues.
94 //
95 typedef std::map<std::pair<const PointerType *,
96 ValID>, GlobalValue*> GlobalRefsType;
97 GlobalRefsType GlobalRefs;
98
99 void ModuleDone() {
100 // If we could not resolve some functions at function compilation time
101 // (calls to functions before they are defined), resolve them now... Types
102 // are resolved when the constant pool has been completely parsed.
103 //
104 ResolveDefinitions(LateResolveValues);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000105 if (TriggerError)
106 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000107
108 // Check to make sure that all global value forward references have been
109 // resolved!
110 //
111 if (!GlobalRefs.empty()) {
112 std::string UndefinedReferences = "Unresolved global references exist:\n";
113
114 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
115 I != E; ++I) {
116 UndefinedReferences += " " + I->first.first->getDescription() + " " +
117 I->first.second.getName() + "\n";
118 }
Reid Spencer61c83e02006-08-18 08:43:06 +0000119 GenerateError(UndefinedReferences);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000120 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000121 }
122
Chandler Carruth02202192007-08-04 01:56:21 +0000123 // Look for intrinsic functions and CallInst that need to be upgraded
124 for (Module::iterator FI = CurrentModule->begin(),
125 FE = CurrentModule->end(); FI != FE; )
126 UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove
127
Chris Lattner58af2a12006-02-15 07:22:58 +0000128 Values.clear(); // Clear out function local definitions
129 Types.clear();
130 CurrentModule = 0;
131 }
132
133 // GetForwardRefForGlobal - Check to see if there is a forward reference
134 // for this global. If so, remove it from the GlobalRefs map and return it.
135 // If not, just return null.
136 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
137 // Check to see if there is a forward reference to this global variable...
138 // if there is, eliminate it and patch the reference to use the new def'n.
139 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
140 GlobalValue *Ret = 0;
141 if (I != GlobalRefs.end()) {
142 Ret = I->second;
143 GlobalRefs.erase(I);
144 }
145 return Ret;
146 }
Reid Spencer8c8a2dc2007-01-02 21:54:12 +0000147
148 bool TypeIsUnresolved(PATypeHolder* PATy) {
149 // If it isn't abstract, its resolved
150 const Type* Ty = PATy->get();
151 if (!Ty->isAbstract())
152 return false;
153 // Traverse the type looking for abstract types. If it isn't abstract then
154 // we don't need to traverse that leg of the type.
155 std::vector<const Type*> WorkList, SeenList;
156 WorkList.push_back(Ty);
157 while (!WorkList.empty()) {
158 const Type* Ty = WorkList.back();
159 SeenList.push_back(Ty);
160 WorkList.pop_back();
161 if (const OpaqueType* OpTy = dyn_cast<OpaqueType>(Ty)) {
162 // Check to see if this is an unresolved type
163 std::map<ValID, PATypeHolder>::iterator I = LateResolveTypes.begin();
164 std::map<ValID, PATypeHolder>::iterator E = LateResolveTypes.end();
165 for ( ; I != E; ++I) {
166 if (I->second.get() == OpTy)
167 return true;
168 }
169 } else if (const SequentialType* SeqTy = dyn_cast<SequentialType>(Ty)) {
170 const Type* TheTy = SeqTy->getElementType();
171 if (TheTy->isAbstract() && TheTy != Ty) {
172 std::vector<const Type*>::iterator I = SeenList.begin(),
173 E = SeenList.end();
174 for ( ; I != E; ++I)
175 if (*I == TheTy)
176 break;
177 if (I == E)
178 WorkList.push_back(TheTy);
179 }
180 } else if (const StructType* StrTy = dyn_cast<StructType>(Ty)) {
181 for (unsigned i = 0; i < StrTy->getNumElements(); ++i) {
182 const Type* TheTy = StrTy->getElementType(i);
183 if (TheTy->isAbstract() && TheTy != Ty) {
184 std::vector<const Type*>::iterator I = SeenList.begin(),
185 E = SeenList.end();
186 for ( ; I != E; ++I)
187 if (*I == TheTy)
188 break;
189 if (I == E)
190 WorkList.push_back(TheTy);
191 }
192 }
193 }
194 }
195 return false;
196 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000197} CurModule;
198
199static struct PerFunctionInfo {
200 Function *CurrentFunction; // Pointer to current function being created
201
Reid Spencer93c40032007-03-19 18:40:50 +0000202 ValueList Values; // Keep track of #'d definitions
203 unsigned NextValNum;
204 ValueList LateResolveValues;
Reid Spenceref9b9a72007-02-05 20:47:22 +0000205 bool isDeclare; // Is this function a forward declararation?
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000206 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000207 GlobalValue::VisibilityTypes Visibility;
Chris Lattner58af2a12006-02-15 07:22:58 +0000208
209 /// BBForwardRefs - When we see forward references to basic blocks, keep
210 /// track of them here.
Reid Spencer93c40032007-03-19 18:40:50 +0000211 std::map<ValID, BasicBlock*> BBForwardRefs;
Chris Lattner58af2a12006-02-15 07:22:58 +0000212
213 inline PerFunctionInfo() {
214 CurrentFunction = 0;
215 isDeclare = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000216 Linkage = GlobalValue::ExternalLinkage;
217 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner58af2a12006-02-15 07:22:58 +0000218 }
219
220 inline void FunctionStart(Function *M) {
221 CurrentFunction = M;
Reid Spencer93c40032007-03-19 18:40:50 +0000222 NextValNum = 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000223 }
224
225 void FunctionDone() {
Chris Lattner58af2a12006-02-15 07:22:58 +0000226 // Any forward referenced blocks left?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000227 if (!BBForwardRefs.empty()) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000228 GenerateError("Undefined reference to label " +
Reid Spencer93c40032007-03-19 18:40:50 +0000229 BBForwardRefs.begin()->second->getName());
Reid Spencer5b7e7532006-09-28 19:28:24 +0000230 return;
231 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000232
233 // Resolve all forward references now.
234 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
235
236 Values.clear(); // Clear out function local definitions
Reid Spencer93c40032007-03-19 18:40:50 +0000237 BBForwardRefs.clear();
Chris Lattner58af2a12006-02-15 07:22:58 +0000238 CurrentFunction = 0;
239 isDeclare = false;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000240 Linkage = GlobalValue::ExternalLinkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000241 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner58af2a12006-02-15 07:22:58 +0000242 }
243} CurFun; // Info for the current function...
244
245static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
246
247
248//===----------------------------------------------------------------------===//
249// Code to handle definitions of all the types
250//===----------------------------------------------------------------------===//
251
Reid Spencer93c40032007-03-19 18:40:50 +0000252static void InsertValue(Value *V, ValueList &ValueTab = CurFun.Values) {
253 // Things that have names or are void typed don't get slot numbers
254 if (V->hasName() || (V->getType() == Type::VoidTy))
255 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000256
Reid Spencer93c40032007-03-19 18:40:50 +0000257 // In the case of function values, we have to allow for the forward reference
258 // of basic blocks, which are included in the numbering. Consequently, we keep
259 // track of the next insertion location with NextValNum. When a BB gets
260 // inserted, it could change the size of the CurFun.Values vector.
261 if (&ValueTab == &CurFun.Values) {
262 if (ValueTab.size() <= CurFun.NextValNum)
263 ValueTab.resize(CurFun.NextValNum+1);
264 ValueTab[CurFun.NextValNum++] = V;
265 return;
266 }
267 // For all other lists, its okay to just tack it on the back of the vector.
268 ValueTab.push_back(V);
Chris Lattner58af2a12006-02-15 07:22:58 +0000269}
270
271static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
272 switch (D.Type) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000273 case ValID::LocalID: // Is it a numbered definition?
Chris Lattner58af2a12006-02-15 07:22:58 +0000274 // Module constants occupy the lowest numbered slots...
Reid Spencer41dff5e2007-01-26 08:05:27 +0000275 if (D.Num < CurModule.Types.size())
276 return CurModule.Types[D.Num];
Chris Lattner58af2a12006-02-15 07:22:58 +0000277 break;
Reid Spencer41dff5e2007-01-26 08:05:27 +0000278 case ValID::LocalName: // Is it a named definition?
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000279 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.getName())) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000280 D.destroy(); // Free old strdup'd memory...
281 return N;
282 }
283 break;
284 default:
Reid Spencerb5334b02007-02-05 10:18:06 +0000285 GenerateError("Internal parser error: Invalid symbol type reference");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000286 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000287 }
288
289 // If we reached here, we referenced either a symbol that we don't know about
290 // or an id number that hasn't been read yet. We may be referencing something
291 // forward, so just create an entry to be resolved later and get to it...
292 //
293 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
294
295
296 if (inFunctionScope()) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000297 if (D.Type == ValID::LocalName) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000298 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000299 return 0;
300 } else {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000301 GenerateError("Reference to an undefined type: #" + utostr(D.Num));
Reid Spencer5b7e7532006-09-28 19:28:24 +0000302 return 0;
303 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000304 }
305
Reid Spencer861d9d62006-11-28 07:29:44 +0000306 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Chris Lattner58af2a12006-02-15 07:22:58 +0000307 if (I != CurModule.LateResolveTypes.end())
Reid Spencer861d9d62006-11-28 07:29:44 +0000308 return I->second;
Chris Lattner58af2a12006-02-15 07:22:58 +0000309
Reid Spencer861d9d62006-11-28 07:29:44 +0000310 Type *Typ = OpaqueType::get();
311 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
312 return Typ;
Reid Spencera132e042006-12-03 05:46:11 +0000313 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000314
Reid Spencer93c40032007-03-19 18:40:50 +0000315// getExistingVal - Look up the value specified by the provided type and
Chris Lattner58af2a12006-02-15 07:22:58 +0000316// the provided ValID. If the value exists and has already been defined, return
317// it. Otherwise return null.
318//
Reid Spencer93c40032007-03-19 18:40:50 +0000319static Value *getExistingVal(const Type *Ty, const ValID &D) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000320 if (isa<FunctionType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000321 GenerateError("Functions are not values and "
Chris Lattner58af2a12006-02-15 07:22:58 +0000322 "must be referenced as pointers");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000323 return 0;
324 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000325
326 switch (D.Type) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000327 case ValID::LocalID: { // Is it a numbered definition?
Reid Spencer41dff5e2007-01-26 08:05:27 +0000328 // Check that the number is within bounds.
Reid Spencer93c40032007-03-19 18:40:50 +0000329 if (D.Num >= CurFun.Values.size())
330 return 0;
331 Value *Result = CurFun.Values[D.Num];
332 if (Ty != Result->getType()) {
333 GenerateError("Numbered value (%" + utostr(D.Num) + ") of type '" +
334 Result->getType()->getDescription() + "' does not match "
335 "expected type, '" + Ty->getDescription() + "'");
336 return 0;
337 }
338 return Result;
Reid Spencer41dff5e2007-01-26 08:05:27 +0000339 }
340 case ValID::GlobalID: { // Is it a numbered definition?
Reid Spencer93c40032007-03-19 18:40:50 +0000341 if (D.Num >= CurModule.Values.size())
Reid Spenceref9b9a72007-02-05 20:47:22 +0000342 return 0;
Reid Spencer93c40032007-03-19 18:40:50 +0000343 Value *Result = CurModule.Values[D.Num];
344 if (Ty != Result->getType()) {
345 GenerateError("Numbered value (@" + utostr(D.Num) + ") of type '" +
346 Result->getType()->getDescription() + "' does not match "
347 "expected type, '" + Ty->getDescription() + "'");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000348 return 0;
Reid Spencer93c40032007-03-19 18:40:50 +0000349 }
350 return Result;
Chris Lattner58af2a12006-02-15 07:22:58 +0000351 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000352
353 case ValID::LocalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000354 if (!inFunctionScope())
355 return 0;
356 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000357 Value *N = SymTab.lookup(D.getName());
Reid Spenceref9b9a72007-02-05 20:47:22 +0000358 if (N == 0)
359 return 0;
360 if (N->getType() != Ty)
361 return 0;
Reid Spencer41dff5e2007-01-26 08:05:27 +0000362
363 D.destroy(); // Free old strdup'd memory...
364 return N;
365 }
366 case ValID::GlobalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000367 ValueSymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable();
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000368 Value *N = SymTab.lookup(D.getName());
Reid Spenceref9b9a72007-02-05 20:47:22 +0000369 if (N == 0)
370 return 0;
371 if (N->getType() != Ty)
372 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000373
374 D.destroy(); // Free old strdup'd memory...
375 return N;
376 }
377
378 // Check to make sure that "Ty" is an integral type, and that our
379 // value will fit into the specified type...
380 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattner38905612008-02-19 04:36:25 +0000381 if (!isa<IntegerType>(Ty) ||
382 !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000383 GenerateError("Signed integral constant '" +
Chris Lattner58af2a12006-02-15 07:22:58 +0000384 itostr(D.ConstPool64) + "' is invalid for type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000385 Ty->getDescription() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000386 return 0;
387 }
Reid Spencer49d273e2007-03-19 20:40:51 +0000388 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner58af2a12006-02-15 07:22:58 +0000389
390 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattner38905612008-02-19 04:36:25 +0000391 if (isa<IntegerType>(Ty) &&
392 ConstantInt::isValueValidForType(Ty, D.UConstPool64))
Reid Spencerb83eb642006-10-20 07:07:24 +0000393 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattner38905612008-02-19 04:36:25 +0000394
395 if (!isa<IntegerType>(Ty) ||
396 !ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
397 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
398 "' is invalid or out of range for type '" +
399 Ty->getDescription() + "'");
400 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000401 }
Chris Lattner38905612008-02-19 04:36:25 +0000402 // This is really a signed reference. Transmogrify.
403 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner58af2a12006-02-15 07:22:58 +0000404
405 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattner38905612008-02-19 04:36:25 +0000406 if (!Ty->isFloatingPoint() ||
407 !ConstantFP::isValueValidForType(Ty, *D.ConstPoolFP)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000408 GenerateError("FP constant invalid for type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000409 return 0;
410 }
Dale Johannesenc72cd7e2007-09-11 18:33:39 +0000411 // Lexer has no type info, so builds all float and double FP constants
412 // as double. Fix this here. Long double does not need this.
413 if (&D.ConstPoolFP->getSemantics() == &APFloat::IEEEdouble &&
414 Ty==Type::FloatTy)
Dale Johannesen43421b32007-09-06 18:13:44 +0000415 D.ConstPoolFP->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
416 return ConstantFP::get(Ty, *D.ConstPoolFP);
Chris Lattner58af2a12006-02-15 07:22:58 +0000417
418 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000419 if (!isa<PointerType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000420 GenerateError("Cannot create a a non pointer null");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000421 return 0;
422 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000423 return ConstantPointerNull::get(cast<PointerType>(Ty));
424
425 case ValID::ConstUndefVal: // Is it an undef value?
426 return UndefValue::get(Ty);
427
428 case ValID::ConstZeroVal: // Is it a zero value?
429 return Constant::getNullValue(Ty);
430
431 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000432 if (D.ConstantValue->getType() != Ty) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000433 GenerateError("Constant expression type different from required type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000434 return 0;
435 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000436 return D.ConstantValue;
437
438 case ValID::InlineAsmVal: { // Inline asm expression
439 const PointerType *PTy = dyn_cast<PointerType>(Ty);
440 const FunctionType *FTy =
441 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000442 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000443 GenerateError("Invalid type for asm constraint string");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000444 return 0;
445 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000446 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
447 D.IAD->HasSideEffects);
448 D.destroy(); // Free InlineAsmDescriptor.
449 return IA;
450 }
451 default:
Reid Spencera9720f52007-02-05 17:04:00 +0000452 assert(0 && "Unhandled case!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000453 return 0;
454 } // End of switch
455
Reid Spencera9720f52007-02-05 17:04:00 +0000456 assert(0 && "Unhandled case!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000457 return 0;
458}
459
Reid Spencer93c40032007-03-19 18:40:50 +0000460// getVal - This function is identical to getExistingVal, except that if a
Chris Lattner58af2a12006-02-15 07:22:58 +0000461// value is not already defined, it "improvises" by creating a placeholder var
462// that looks and acts just like the requested variable. When the value is
463// defined later, all uses of the placeholder variable are replaced with the
464// real thing.
465//
466static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000467 if (Ty == Type::LabelTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000468 GenerateError("Cannot use a basic block here");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000469 return 0;
470 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000471
472 // See if the value has already been defined.
Reid Spencer93c40032007-03-19 18:40:50 +0000473 Value *V = getExistingVal(Ty, ID);
Chris Lattner58af2a12006-02-15 07:22:58 +0000474 if (V) return V;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000475 if (TriggerError) return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000476
Reid Spencer5b7e7532006-09-28 19:28:24 +0000477 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000478 GenerateError("Invalid use of a composite type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000479 return 0;
480 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000481
482 // If we reached here, we referenced either a symbol that we don't know about
483 // or an id number that hasn't been read yet. We may be referencing something
484 // forward, so just create an entry to be resolved later and get to it...
485 //
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000486 switch (ID.Type) {
487 case ValID::GlobalName:
Reid Spencer9c9b63a2007-04-28 16:07:31 +0000488 case ValID::GlobalID: {
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000489 const PointerType *PTy = dyn_cast<PointerType>(Ty);
490 if (!PTy) {
491 GenerateError("Invalid type for reference to global" );
492 return 0;
493 }
494 const Type* ElTy = PTy->getElementType();
495 if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy))
496 V = new Function(FTy, GlobalValue::ExternalLinkage);
497 else
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000498 V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage, 0, "",
499 (Module*)0, false, PTy->getAddressSpace());
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000500 break;
Reid Spencer9c9b63a2007-04-28 16:07:31 +0000501 }
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000502 default:
503 V = new Argument(Ty);
504 }
505
Chris Lattner58af2a12006-02-15 07:22:58 +0000506 // Remember where this forward reference came from. FIXME, shouldn't we try
507 // to recycle these things??
508 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Duncan Sandsdc024672007-11-27 13:23:08 +0000509 LLLgetLineNo())));
Chris Lattner58af2a12006-02-15 07:22:58 +0000510
511 if (inFunctionScope())
512 InsertValue(V, CurFun.LateResolveValues);
513 else
514 InsertValue(V, CurModule.LateResolveValues);
515 return V;
516}
517
Reid Spencer93c40032007-03-19 18:40:50 +0000518/// defineBBVal - This is a definition of a new basic block with the specified
519/// identifier which must be the same as CurFun.NextValNum, if its numeric.
520static BasicBlock *defineBBVal(const ValID &ID) {
Reid Spencera9720f52007-02-05 17:04:00 +0000521 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000522
Chris Lattner58af2a12006-02-15 07:22:58 +0000523 BasicBlock *BB = 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000524
Reid Spencer93c40032007-03-19 18:40:50 +0000525 // First, see if this was forward referenced
Chris Lattner58af2a12006-02-15 07:22:58 +0000526
Reid Spencer93c40032007-03-19 18:40:50 +0000527 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
528 if (BBI != CurFun.BBForwardRefs.end()) {
529 BB = BBI->second;
Chris Lattner58af2a12006-02-15 07:22:58 +0000530 // The forward declaration could have been inserted anywhere in the
531 // function: insert it into the correct place now.
532 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
533 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Reid Spencer93c40032007-03-19 18:40:50 +0000534
Reid Spencer66728ef2007-03-20 01:13:36 +0000535 // We're about to erase the entry, save the key so we can clean it up.
536 ValID Tmp = BBI->first;
537
Reid Spencer93c40032007-03-19 18:40:50 +0000538 // Erase the forward ref from the map as its no longer "forward"
539 CurFun.BBForwardRefs.erase(ID);
540
Reid Spencer66728ef2007-03-20 01:13:36 +0000541 // The key has been removed from the map but so we don't want to leave
542 // strdup'd memory around so destroy it too.
543 Tmp.destroy();
544
Reid Spencer93c40032007-03-19 18:40:50 +0000545 // If its a numbered definition, bump the number and set the BB value.
546 if (ID.Type == ValID::LocalID) {
547 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
548 InsertValue(BB);
549 }
550
551 ID.destroy();
552 return BB;
553 }
554
555 // We haven't seen this BB before and its first mention is a definition.
556 // Just create it and return it.
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000557 std::string Name (ID.Type == ValID::LocalName ? ID.getName() : "");
Reid Spencer93c40032007-03-19 18:40:50 +0000558 BB = new BasicBlock(Name, CurFun.CurrentFunction);
559 if (ID.Type == ValID::LocalID) {
560 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
561 InsertValue(BB);
Chris Lattner58af2a12006-02-15 07:22:58 +0000562 }
Reid Spencer93c40032007-03-19 18:40:50 +0000563
564 ID.destroy(); // Free strdup'd memory
565 return BB;
566}
567
568/// getBBVal - get an existing BB value or create a forward reference for it.
569///
570static BasicBlock *getBBVal(const ValID &ID) {
571 assert(inFunctionScope() && "Can't get basic block at global scope!");
572
573 BasicBlock *BB = 0;
574
575 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
576 if (BBI != CurFun.BBForwardRefs.end()) {
577 BB = BBI->second;
578 } if (ID.Type == ValID::LocalName) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000579 std::string Name = ID.getName();
Reid Spencer93c40032007-03-19 18:40:50 +0000580 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
581 if (N)
582 if (N->getType()->getTypeID() == Type::LabelTyID)
583 BB = cast<BasicBlock>(N);
584 else
585 GenerateError("Reference to label '" + Name + "' is actually of type '"+
586 N->getType()->getDescription() + "'");
587 } else if (ID.Type == ValID::LocalID) {
588 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
589 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
590 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
591 else
592 GenerateError("Reference to label '%" + utostr(ID.Num) +
593 "' is actually of type '"+
594 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
595 }
596 } else {
597 GenerateError("Illegal label reference " + ID.getName());
598 return 0;
599 }
600
601 // If its already been defined, return it now.
602 if (BB) {
603 ID.destroy(); // Free strdup'd memory.
604 return BB;
605 }
606
607 // Otherwise, this block has not been seen before, create it.
608 std::string Name;
609 if (ID.Type == ValID::LocalName)
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000610 Name = ID.getName();
Reid Spencer93c40032007-03-19 18:40:50 +0000611 BB = new BasicBlock(Name, CurFun.CurrentFunction);
612
613 // Insert it in the forward refs map.
614 CurFun.BBForwardRefs[ID] = BB;
615
Chris Lattner58af2a12006-02-15 07:22:58 +0000616 return BB;
617}
618
619
620//===----------------------------------------------------------------------===//
621// Code to handle forward references in instructions
622//===----------------------------------------------------------------------===//
623//
624// This code handles the late binding needed with statements that reference
625// values not defined yet... for example, a forward branch, or the PHI node for
626// a loop body.
627//
628// This keeps a table (CurFun.LateResolveValues) of all such forward references
629// and back patchs after we are done.
630//
631
632// ResolveDefinitions - If we could not resolve some defs at parsing
633// time (forward branches, phi functions for loops, etc...) resolve the
634// defs now...
635//
636static void
Reid Spencer93c40032007-03-19 18:40:50 +0000637ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000638 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencer93c40032007-03-19 18:40:50 +0000639 while (!LateResolvers.empty()) {
640 Value *V = LateResolvers.back();
641 LateResolvers.pop_back();
Chris Lattner58af2a12006-02-15 07:22:58 +0000642
Reid Spencer93c40032007-03-19 18:40:50 +0000643 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
644 CurModule.PlaceHolderInfo.find(V);
645 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000646
Reid Spencer93c40032007-03-19 18:40:50 +0000647 ValID &DID = PHI->second.first;
Chris Lattner58af2a12006-02-15 07:22:58 +0000648
Reid Spencer93c40032007-03-19 18:40:50 +0000649 Value *TheRealValue = getExistingVal(V->getType(), DID);
650 if (TriggerError)
651 return;
652 if (TheRealValue) {
653 V->replaceAllUsesWith(TheRealValue);
654 delete V;
655 CurModule.PlaceHolderInfo.erase(PHI);
656 } else if (FutureLateResolvers) {
657 // Functions have their unresolved items forwarded to the module late
658 // resolver table
659 InsertValue(V, *FutureLateResolvers);
660 } else {
661 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
662 GenerateError("Reference to an invalid definition: '" +DID.getName()+
663 "' of type '" + V->getType()->getDescription() + "'",
664 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000665 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000666 } else {
Reid Spencer93c40032007-03-19 18:40:50 +0000667 GenerateError("Reference to an invalid definition: #" +
668 itostr(DID.Num) + " of type '" +
669 V->getType()->getDescription() + "'",
670 PHI->second.second);
671 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000672 }
673 }
674 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000675 LateResolvers.clear();
676}
677
678// ResolveTypeTo - A brand new type was just declared. This means that (if
679// name is not null) things referencing Name can be resolved. Otherwise, things
680// refering to the number can be resolved. Do this now.
681//
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000682static void ResolveTypeTo(std::string *Name, const Type *ToTy) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000683 ValID D;
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000684 if (Name)
685 D = ValID::createLocalName(*Name);
686 else
687 D = ValID::createLocalID(CurModule.Types.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000688
Reid Spencer861d9d62006-11-28 07:29:44 +0000689 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattner58af2a12006-02-15 07:22:58 +0000690 CurModule.LateResolveTypes.find(D);
691 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencer861d9d62006-11-28 07:29:44 +0000692 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner58af2a12006-02-15 07:22:58 +0000693 CurModule.LateResolveTypes.erase(I);
694 }
695}
696
697// setValueName - Set the specified value to the name given. The name may be
698// null potentially, in which case this is a noop. The string passed in is
699// assumed to be a malloc'd string buffer, and is free'd by this function.
700//
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000701static void setValueName(Value *V, std::string *NameStr) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000702 if (!NameStr) return;
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000703 std::string Name(*NameStr); // Copy string
704 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000705
Reid Spencer41dff5e2007-01-26 08:05:27 +0000706 if (V->getType() == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000707 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000708 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000709 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000710
Reid Spencera9720f52007-02-05 17:04:00 +0000711 assert(inFunctionScope() && "Must be in function scope!");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000712 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
713 if (ST.lookup(Name)) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000714 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000715 V->getType()->getDescription() + "'");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000716 return;
717 }
718
719 // Set the name.
720 V->setName(Name);
Chris Lattner58af2a12006-02-15 07:22:58 +0000721}
722
723/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
724/// this is a declaration, otherwise it is a definition.
725static GlobalVariable *
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000726ParseGlobalVariable(std::string *NameStr,
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000727 GlobalValue::LinkageTypes Linkage,
728 GlobalValue::VisibilityTypes Visibility,
Chris Lattner58af2a12006-02-15 07:22:58 +0000729 bool isConstantGlobal, const Type *Ty,
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000730 Constant *Initializer, bool IsThreadLocal,
731 unsigned AddressSpace = 0) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000732 if (isa<FunctionType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000733 GenerateError("Cannot declare global vars of function type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000734 return 0;
735 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000736
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000737 const PointerType *PTy = PointerType::get(Ty, AddressSpace);
Chris Lattner58af2a12006-02-15 07:22:58 +0000738
739 std::string Name;
740 if (NameStr) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000741 Name = *NameStr; // Copy string
742 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000743 }
744
745 // See if this global value was forward referenced. If so, recycle the
746 // object.
747 ValID ID;
748 if (!Name.empty()) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000749 ID = ValID::createGlobalName(Name);
Chris Lattner58af2a12006-02-15 07:22:58 +0000750 } else {
Reid Spencer93c40032007-03-19 18:40:50 +0000751 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000752 }
753
754 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
755 // Move the global to the end of the list, from whereever it was
756 // previously inserted.
757 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
758 CurModule.CurrentModule->getGlobalList().remove(GV);
759 CurModule.CurrentModule->getGlobalList().push_back(GV);
760 GV->setInitializer(Initializer);
761 GV->setLinkage(Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000762 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000763 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000764 GV->setThreadLocal(IsThreadLocal);
Chris Lattner58af2a12006-02-15 07:22:58 +0000765 InsertValue(GV, CurModule.Values);
766 return GV;
767 }
768
Reid Spenceref9b9a72007-02-05 20:47:22 +0000769 // If this global has a name
Chris Lattner58af2a12006-02-15 07:22:58 +0000770 if (!Name.empty()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000771 // if the global we're parsing has an initializer (is a definition) and
772 // has external linkage.
773 if (Initializer && Linkage != GlobalValue::InternalLinkage)
774 // If there is already a global with external linkage with this name
775 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
776 // If we allow this GVar to get created, it will be renamed in the
777 // symbol table because it conflicts with an existing GVar. We can't
778 // allow redefinition of GVars whose linking indicates that their name
779 // must stay the same. Issue the error.
780 GenerateError("Redefinition of global variable named '" + Name +
781 "' of type '" + Ty->getDescription() + "'");
782 return 0;
783 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000784 }
785
786 // Otherwise there is no existing GV to use, create one now.
787 GlobalVariable *GV =
788 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000789 CurModule.CurrentModule, IsThreadLocal, AddressSpace);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000790 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000791 InsertValue(GV, CurModule.Values);
792 return GV;
793}
794
795// setTypeName - Set the specified type to the name given. The name may be
796// null potentially, in which case this is a noop. The string passed in is
797// assumed to be a malloc'd string buffer, and is freed by this function.
798//
799// This function returns true if the type has already been defined, but is
800// allowed to be redefined in the specified context. If the name is a new name
801// for the type plane, it is inserted and false is returned.
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000802static bool setTypeName(const Type *T, std::string *NameStr) {
Reid Spencera9720f52007-02-05 17:04:00 +0000803 assert(!inFunctionScope() && "Can't give types function-local names!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000804 if (NameStr == 0) return false;
805
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000806 std::string Name(*NameStr); // Copy string
807 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000808
809 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000810 if (T == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000811 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000812 return false;
813 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000814
815 // Set the type name, checking for conflicts as we do so.
816 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
817
818 if (AlreadyExists) { // Inserting a name that is already defined???
819 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencera9720f52007-02-05 17:04:00 +0000820 assert(Existing && "Conflict but no matching type?!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000821
822 // There is only one case where this is allowed: when we are refining an
823 // opaque type. In this case, Existing will be an opaque type.
824 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
825 // We ARE replacing an opaque type!
826 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
827 return true;
828 }
829
830 // Otherwise, this is an attempt to redefine a type. That's okay if
831 // the redefinition is identical to the original. This will be so if
832 // Existing and T point to the same Type object. In this one case we
833 // allow the equivalent redefinition.
834 if (Existing == T) return true; // Yes, it's equal.
835
836 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer63c34452007-01-05 21:51:07 +0000837 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000838 T->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +0000839 }
840
841 return false;
842}
843
844//===----------------------------------------------------------------------===//
845// Code for handling upreferences in type names...
846//
847
848// TypeContains - Returns true if Ty directly contains E in it.
849//
850static bool TypeContains(const Type *Ty, const Type *E) {
851 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
852 E) != Ty->subtype_end();
853}
854
855namespace {
856 struct UpRefRecord {
857 // NestingLevel - The number of nesting levels that need to be popped before
858 // this type is resolved.
859 unsigned NestingLevel;
860
861 // LastContainedTy - This is the type at the current binding level for the
862 // type. Every time we reduce the nesting level, this gets updated.
863 const Type *LastContainedTy;
864
865 // UpRefTy - This is the actual opaque type that the upreference is
866 // represented with.
867 OpaqueType *UpRefTy;
868
869 UpRefRecord(unsigned NL, OpaqueType *URTy)
870 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
871 };
872}
873
874// UpRefs - A list of the outstanding upreferences that need to be resolved.
875static std::vector<UpRefRecord> UpRefs;
876
877/// HandleUpRefs - Every time we finish a new layer of types, this function is
878/// called. It loops through the UpRefs vector, which is a list of the
879/// currently active types. For each type, if the up reference is contained in
880/// the newly completed type, we decrement the level count. When the level
881/// count reaches zero, the upreferenced type is the type that is passed in:
882/// thus we can complete the cycle.
883///
884static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner224f84f2006-08-18 17:34:45 +0000885 // If Ty isn't abstract, or if there are no up-references in it, then there is
886 // nothing to resolve here.
887 if (!ty->isAbstract() || UpRefs.empty()) return ty;
888
Chris Lattner58af2a12006-02-15 07:22:58 +0000889 PATypeHolder Ty(ty);
890 UR_OUT("Type '" << Ty->getDescription() <<
891 "' newly formed. Resolving upreferences.\n" <<
892 UpRefs.size() << " upreferences active!\n");
893
894 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
895 // to zero), we resolve them all together before we resolve them to Ty. At
896 // the end of the loop, if there is anything to resolve to Ty, it will be in
897 // this variable.
898 OpaqueType *TypeToResolve = 0;
899
900 for (unsigned i = 0; i != UpRefs.size(); ++i) {
901 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
902 << UpRefs[i].second->getDescription() << ") = "
903 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
904 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
905 // Decrement level of upreference
906 unsigned Level = --UpRefs[i].NestingLevel;
907 UpRefs[i].LastContainedTy = Ty;
908 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
909 if (Level == 0) { // Upreference should be resolved!
910 if (!TypeToResolve) {
911 TypeToResolve = UpRefs[i].UpRefTy;
912 } else {
913 UR_OUT(" * Resolving upreference for "
914 << UpRefs[i].second->getDescription() << "\n";
915 std::string OldName = UpRefs[i].UpRefTy->getDescription());
916 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
917 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
918 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
919 }
920 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
921 --i; // Do not skip the next element...
922 }
923 }
924 }
925
926 if (TypeToResolve) {
927 UR_OUT(" * Resolving upreference for "
928 << UpRefs[i].second->getDescription() << "\n";
929 std::string OldName = TypeToResolve->getDescription());
930 TypeToResolve->refineAbstractTypeTo(Ty);
931 }
932
933 return Ty;
934}
935
Chris Lattner58af2a12006-02-15 07:22:58 +0000936//===----------------------------------------------------------------------===//
937// RunVMAsmParser - Define an interface to this parser
938//===----------------------------------------------------------------------===//
939//
Reid Spencer14310612006-12-31 05:40:51 +0000940static Module* RunParser(Module * M);
941
Duncan Sandsdc024672007-11-27 13:23:08 +0000942Module *llvm::RunVMAsmParser(llvm::MemoryBuffer *MB) {
943 InitLLLexer(MB);
944 Module *M = RunParser(new Module(LLLgetFilename()));
945 FreeLexer();
946 return M;
Chris Lattner58af2a12006-02-15 07:22:58 +0000947}
948
949%}
950
951%union {
952 llvm::Module *ModuleVal;
953 llvm::Function *FunctionVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000954 llvm::BasicBlock *BasicBlockVal;
955 llvm::TerminatorInst *TermInstVal;
956 llvm::Instruction *InstVal;
Reid Spencera132e042006-12-03 05:46:11 +0000957 llvm::Constant *ConstVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000958
Reid Spencera132e042006-12-03 05:46:11 +0000959 const llvm::Type *PrimType;
Reid Spencer14310612006-12-31 05:40:51 +0000960 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencera132e042006-12-03 05:46:11 +0000961 llvm::PATypeHolder *TypeVal;
962 llvm::Value *ValueVal;
Reid Spencera132e042006-12-03 05:46:11 +0000963 std::vector<llvm::Value*> *ValueList;
Reid Spencer14310612006-12-31 05:40:51 +0000964 llvm::ArgListType *ArgList;
965 llvm::TypeWithAttrs TypeWithAttrs;
966 llvm::TypeWithAttrsList *TypeWithAttrsList;
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000967 llvm::ParamList *ParamList;
Reid Spencer14310612006-12-31 05:40:51 +0000968
Chris Lattner58af2a12006-02-15 07:22:58 +0000969 // Represent the RHS of PHI node
Reid Spencera132e042006-12-03 05:46:11 +0000970 std::list<std::pair<llvm::Value*,
971 llvm::BasicBlock*> > *PHIList;
Chris Lattner58af2a12006-02-15 07:22:58 +0000972 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencera132e042006-12-03 05:46:11 +0000973 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner58af2a12006-02-15 07:22:58 +0000974
975 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000976 llvm::GlobalValue::VisibilityTypes Visibility;
Dale Johannesen222ebf72008-02-19 21:40:51 +0000977 llvm::ParameterAttributes ParamAttrs;
Reid Spencer38c91a92007-02-28 02:24:54 +0000978 llvm::APInt *APIntVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000979 int64_t SInt64Val;
980 uint64_t UInt64Val;
981 int SIntVal;
982 unsigned UIntVal;
Dale Johannesen43421b32007-09-06 18:13:44 +0000983 llvm::APFloat *FPVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000984 bool BoolVal;
985
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000986 std::string *StrVal; // This memory must be deleted
987 llvm::ValID ValIDVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000988
Reid Spencera132e042006-12-03 05:46:11 +0000989 llvm::Instruction::BinaryOps BinaryOpVal;
990 llvm::Instruction::TermOps TermOpVal;
991 llvm::Instruction::MemoryOps MemOpVal;
992 llvm::Instruction::CastOps CastOpVal;
993 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencera132e042006-12-03 05:46:11 +0000994 llvm::ICmpInst::Predicate IPredicate;
995 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner58af2a12006-02-15 07:22:58 +0000996}
997
Reid Spencer14310612006-12-31 05:40:51 +0000998%type <ModuleVal> Module
Chris Lattner58af2a12006-02-15 07:22:58 +0000999%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
1000%type <BasicBlockVal> BasicBlock InstructionList
1001%type <TermInstVal> BBTerminatorInst
1002%type <InstVal> Inst InstVal MemoryInst
Anton Korobeynikov38e09802007-04-28 13:48:45 +00001003%type <ConstVal> ConstVal ConstExpr AliaseeRef
Chris Lattner58af2a12006-02-15 07:22:58 +00001004%type <ConstVector> ConstVector
1005%type <ArgList> ArgList ArgListH
Chris Lattner58af2a12006-02-15 07:22:58 +00001006%type <PHIList> PHIList
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001007%type <ParamList> ParamList // For call param lists & GEP indices
Reid Spencer14310612006-12-31 05:40:51 +00001008%type <ValueList> IndexList // For GEP indices
1009%type <TypeList> TypeListI
1010%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer218ded22007-01-05 17:07:23 +00001011%type <TypeWithAttrs> ArgType
Chris Lattner58af2a12006-02-15 07:22:58 +00001012%type <JumpTable> JumpTable
1013%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001014%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattner58af2a12006-02-15 07:22:58 +00001015%type <BoolVal> OptVolatile // 'volatile' or not
1016%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1017%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencer14310612006-12-31 05:40:51 +00001018%type <Linkage> GVInternalLinkage GVExternalLinkage
1019%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001020%type <Linkage> AliasLinkage
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001021%type <Visibility> GVVisibilityStyle
Chris Lattner58af2a12006-02-15 07:22:58 +00001022
1023// ValueRef - Unresolved reference to a definition or BB
1024%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1025%type <ValueVal> ResolvedVal // <type> <valref> pair
1026// 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
Chris Lattner1ae022f2006-10-22 06:08:13 +00001066%token DATALAYOUT
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; }
Reid Spencer14310612006-12-31 05:40:51 +00001235 ;
1236
Reid Spencer18da0722007-04-11 02:44:20 +00001237OptParamAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer218ded22007-01-05 17:07:23 +00001238 | OptParamAttrs ParamAttr {
Reid Spencer7b5d4662007-04-09 06:16:21 +00001239 $$ = $1 | $2;
Reid Spencer14310612006-12-31 05:40:51 +00001240 }
1241 ;
1242
Reid Spencer18da0722007-04-11 02:44:20 +00001243FuncAttr : NORETURN { $$ = ParamAttr::NoReturn; }
1244 | NOUNWIND { $$ = ParamAttr::NoUnwind; }
Reid Spencerb8f85052007-07-31 03:50:36 +00001245 | ZEROEXT { $$ = ParamAttr::ZExt; }
1246 | SIGNEXT { $$ = ParamAttr::SExt; }
Duncan Sandsdc024672007-11-27 13:23:08 +00001247 | READNONE { $$ = ParamAttr::ReadNone; }
1248 | READONLY { $$ = ParamAttr::ReadOnly; }
Reid Spencer218ded22007-01-05 17:07:23 +00001249 ;
1250
Reid Spencer18da0722007-04-11 02:44:20 +00001251OptFuncAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer218ded22007-01-05 17:07:23 +00001252 | OptFuncAttrs FuncAttr {
Reid Spencer7b5d4662007-04-09 06:16:21 +00001253 $$ = $1 | $2;
Reid Spencer218ded22007-01-05 17:07:23 +00001254 }
Reid Spencer14310612006-12-31 05:40:51 +00001255 ;
1256
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001257OptGC : /* empty */ { $$ = 0; }
1258 | GC STRINGCONSTANT {
1259 $$ = $2;
1260 }
1261 ;
1262
Chris Lattner58af2a12006-02-15 07:22:58 +00001263// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1264// a comma before it.
1265OptAlign : /*empty*/ { $$ = 0; } |
1266 ALIGN EUINT64VAL {
1267 $$ = $2;
1268 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001269 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001270 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001271};
1272OptCAlign : /*empty*/ { $$ = 0; } |
1273 ',' ALIGN EUINT64VAL {
1274 $$ = $3;
1275 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001276 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001277 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001278};
1279
1280
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001281
Chris Lattner58af2a12006-02-15 07:22:58 +00001282SectionString : SECTION STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001283 for (unsigned i = 0, e = $2->length(); i != e; ++i)
1284 if ((*$2)[i] == '"' || (*$2)[i] == '\\')
Reid Spencerb5334b02007-02-05 10:18:06 +00001285 GEN_ERROR("Invalid character in section name");
Chris Lattner58af2a12006-02-15 07:22:58 +00001286 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001287 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001288};
1289
1290OptSection : /*empty*/ { $$ = 0; } |
1291 SectionString { $$ = $1; };
1292
1293// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1294// is set to be the global we are processing.
1295//
1296GlobalVarAttributes : /* empty */ {} |
1297 ',' GlobalVarAttribute GlobalVarAttributes {};
1298GlobalVarAttribute : SectionString {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001299 CurGV->setSection(*$1);
1300 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001301 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001302 }
1303 | ALIGN EUINT64VAL {
1304 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001305 GEN_ERROR("Alignment must be a power of two");
Chris Lattner58af2a12006-02-15 07:22:58 +00001306 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001307 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001308 };
1309
1310//===----------------------------------------------------------------------===//
1311// Types includes all predefined types... except void, because it can only be
Reid Spencer14310612006-12-31 05:40:51 +00001312// used in specific contexts (function returning void for example).
Chris Lattner58af2a12006-02-15 07:22:58 +00001313
1314// Derived types are added later...
1315//
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001316PrimType : INTTYPE | FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80 | LABEL ;
Reid Spencer14310612006-12-31 05:40:51 +00001317
1318Types
1319 : OPAQUE {
Reid Spencera132e042006-12-03 05:46:11 +00001320 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001321 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001322 }
1323 | PrimType {
Reid Spencera132e042006-12-03 05:46:11 +00001324 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001325 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00001326 }
Christopher Lambbf3348d2007-12-12 08:45:45 +00001327 | Types OptAddrSpace '*' { // Pointer type?
Reid Spencer14310612006-12-31 05:40:51 +00001328 if (*$1 == Type::LabelTy)
1329 GEN_ERROR("Cannot form a pointer to a basic block");
Christopher Lambbf3348d2007-12-12 08:45:45 +00001330 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1, $2)));
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001331 delete $1;
1332 CHECK_FOR_ERROR
1333 }
Reid Spencer14310612006-12-31 05:40:51 +00001334 | SymbolicValueRef { // Named types are also simple types...
1335 const Type* tmp = getTypeVal($1);
1336 CHECK_FOR_ERROR
1337 $$ = new PATypeHolder(tmp);
1338 }
1339 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerb5334b02007-02-05 10:18:06 +00001340 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattner58af2a12006-02-15 07:22:58 +00001341 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1342 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencera132e042006-12-03 05:46:11 +00001343 $$ = new PATypeHolder(OT);
Chris Lattner58af2a12006-02-15 07:22:58 +00001344 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001345 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001346 }
Reid Spencer218ded22007-01-05 17:07:23 +00001347 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001348 // Allow but ignore attributes on function types; this permits auto-upgrade.
1349 // FIXME: remove in LLVM 3.0.
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001350 const Type* RetTy = *$1;
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001351 if (!(RetTy->isFirstClassType() || RetTy == Type::VoidTy ||
1352 isa<OpaqueType>(RetTy)))
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001353 GEN_ERROR("LLVM Functions cannot return aggregates");
1354
Chris Lattner58af2a12006-02-15 07:22:58 +00001355 std::vector<const Type*> Params;
Reid Spencer7b5d4662007-04-09 06:16:21 +00001356 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001357 for (; I != E; ++I ) {
Reid Spencer66728ef2007-03-20 01:13:36 +00001358 const Type *Ty = I->Ty->get();
Reid Spencer66728ef2007-03-20 01:13:36 +00001359 Params.push_back(Ty);
Reid Spencer14310612006-12-31 05:40:51 +00001360 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001361
Chris Lattner58af2a12006-02-15 07:22:58 +00001362 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1363 if (isVarArg) Params.pop_back();
1364
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001365 for (unsigned i = 0; i != Params.size(); ++i)
1366 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1367 GEN_ERROR("Function arguments must be value types!");
1368
1369 CHECK_FOR_ERROR
1370
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001371 FunctionType *FT = FunctionType::get(RetTy, Params, isVarArg);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001372 delete $3; // Delete the argument list
Reid Spencer14310612006-12-31 05:40:51 +00001373 delete $1; // Delete the return type handle
1374 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer61c83e02006-08-18 08:43:06 +00001375 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001376 }
Reid Spencer218ded22007-01-05 17:07:23 +00001377 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001378 // Allow but ignore attributes on function types; this permits auto-upgrade.
1379 // FIXME: remove in LLVM 3.0.
Reid Spencer14310612006-12-31 05:40:51 +00001380 std::vector<const Type*> Params;
Reid Spencer7b5d4662007-04-09 06:16:21 +00001381 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001382 for ( ; I != E; ++I ) {
Reid Spencer66728ef2007-03-20 01:13:36 +00001383 const Type* Ty = I->Ty->get();
Reid Spencer66728ef2007-03-20 01:13:36 +00001384 Params.push_back(Ty);
Reid Spencer14310612006-12-31 05:40:51 +00001385 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001386
Reid Spencer14310612006-12-31 05:40:51 +00001387 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1388 if (isVarArg) Params.pop_back();
1389
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001390 for (unsigned i = 0; i != Params.size(); ++i)
1391 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1392 GEN_ERROR("Function arguments must be value types!");
1393
1394 CHECK_FOR_ERROR
1395
Duncan Sandsdc024672007-11-27 13:23:08 +00001396 FunctionType *FT = FunctionType::get($1, Params, isVarArg);
Reid Spencer218ded22007-01-05 17:07:23 +00001397 delete $3; // Delete the argument list
Reid Spencer14310612006-12-31 05:40:51 +00001398 $$ = new PATypeHolder(HandleUpRefs(FT));
1399 CHECK_FOR_ERROR
1400 }
1401
1402 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Reid Spencera132e042006-12-03 05:46:11 +00001403 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1404 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001405 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001406 }
Chris Lattner32980692007-02-19 07:44:24 +00001407 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencera132e042006-12-03 05:46:11 +00001408 const llvm::Type* ElemTy = $4->get();
1409 if ((unsigned)$2 != $2)
1410 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner42a75512007-01-15 02:27:26 +00001411 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencer9d6565a2007-02-15 02:26:10 +00001412 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001413 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencera132e042006-12-03 05:46:11 +00001414 delete $4;
1415 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001416 }
1417 | '{' TypeListI '}' { // Structure type?
1418 std::vector<const Type*> Elements;
Reid Spencera132e042006-12-03 05:46:11 +00001419 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattner58af2a12006-02-15 07:22:58 +00001420 E = $2->end(); I != E; ++I)
Reid Spencera132e042006-12-03 05:46:11 +00001421 Elements.push_back(*I);
Chris Lattner58af2a12006-02-15 07:22:58 +00001422
Reid Spencera132e042006-12-03 05:46:11 +00001423 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattner58af2a12006-02-15 07:22:58 +00001424 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001425 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001426 }
1427 | '{' '}' { // Empty structure type?
Reid Spencera132e042006-12-03 05:46:11 +00001428 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001429 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001430 }
Andrew Lenharth6353e052006-12-08 18:07:09 +00001431 | '<' '{' TypeListI '}' '>' {
1432 std::vector<const Type*> Elements;
1433 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1434 E = $3->end(); I != E; ++I)
1435 Elements.push_back(*I);
1436
1437 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1438 delete $3;
1439 CHECK_FOR_ERROR
1440 }
1441 | '<' '{' '}' '>' { // Empty structure type?
1442 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1443 CHECK_FOR_ERROR
1444 }
Reid Spencer14310612006-12-31 05:40:51 +00001445 ;
1446
1447ArgType
Duncan Sandsdc024672007-11-27 13:23:08 +00001448 : Types OptParamAttrs {
1449 // Allow but ignore attributes on function types; this permits auto-upgrade.
1450 // FIXME: remove in LLVM 3.0.
Reid Spencer14310612006-12-31 05:40:51 +00001451 $$.Ty = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00001452 $$.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00001453 }
1454 ;
1455
Reid Spencer218ded22007-01-05 17:07:23 +00001456ResultTypes
1457 : Types {
Reid Spencer14310612006-12-31 05:40:51 +00001458 if (!UpRefs.empty())
1459 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1460 if (!(*$1)->isFirstClassType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001461 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer218ded22007-01-05 17:07:23 +00001462 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00001463 }
Reid Spencer218ded22007-01-05 17:07:23 +00001464 | VOID {
1465 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencer14310612006-12-31 05:40:51 +00001466 }
1467 ;
1468
1469ArgTypeList : ArgType {
1470 $$ = new TypeWithAttrsList();
1471 $$->push_back($1);
1472 CHECK_FOR_ERROR
1473 }
1474 | ArgTypeList ',' ArgType {
1475 ($$=$1)->push_back($3);
1476 CHECK_FOR_ERROR
1477 }
1478 ;
1479
1480ArgTypeListI
1481 : ArgTypeList
1482 | ArgTypeList ',' DOTDOTDOT {
1483 $$=$1;
Reid Spencer18da0722007-04-11 02:44:20 +00001484 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00001485 TWA.Ty = new PATypeHolder(Type::VoidTy);
1486 $$->push_back(TWA);
1487 CHECK_FOR_ERROR
1488 }
1489 | DOTDOTDOT {
1490 $$ = new TypeWithAttrsList;
Reid Spencer18da0722007-04-11 02:44:20 +00001491 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00001492 TWA.Ty = new PATypeHolder(Type::VoidTy);
1493 $$->push_back(TWA);
1494 CHECK_FOR_ERROR
1495 }
1496 | /*empty*/ {
1497 $$ = new TypeWithAttrsList();
Reid Spencer61c83e02006-08-18 08:43:06 +00001498 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001499 };
1500
1501// TypeList - Used for struct declarations and as a basis for function type
1502// declaration type lists
1503//
Reid Spencer14310612006-12-31 05:40:51 +00001504TypeListI : Types {
Reid Spencera132e042006-12-03 05:46:11 +00001505 $$ = new std::list<PATypeHolder>();
Reid Spencer66728ef2007-03-20 01:13:36 +00001506 $$->push_back(*$1);
1507 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001508 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001509 }
Reid Spencer14310612006-12-31 05:40:51 +00001510 | TypeListI ',' Types {
Reid Spencer66728ef2007-03-20 01:13:36 +00001511 ($$=$1)->push_back(*$3);
1512 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001513 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001514 };
1515
Chris Lattner58af2a12006-02-15 07:22:58 +00001516// ConstVal - The various declarations that go into the constant pool. This
1517// production is used ONLY to represent constants that show up AFTER a 'const',
1518// 'constant' or 'global' token at global scope. Constants that can be inlined
1519// into other expressions (such as integers and constexprs) are handled by the
1520// ResolvedVal, ValueRef and ConstValueRef productions.
1521//
1522ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001523 if (!UpRefs.empty())
1524 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001525 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001526 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001527 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001528 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001529 const Type *ETy = ATy->getElementType();
1530 int NumElements = ATy->getNumElements();
1531
1532 // Verify that we have the correct size...
1533 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001534 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001535 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerb5334b02007-02-05 10:18:06 +00001536 itostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001537
1538 // Verify all elements are correct type!
1539 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001540 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001541 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001542 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001543 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001544 }
1545
Reid Spencera132e042006-12-03 05:46:11 +00001546 $$ = ConstantArray::get(ATy, *$3);
1547 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001548 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001549 }
1550 | Types '[' ']' {
Reid Spencer14310612006-12-31 05:40:51 +00001551 if (!UpRefs.empty())
1552 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001553 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001554 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001555 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001556 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001557
1558 int NumElements = ATy->getNumElements();
1559 if (NumElements != -1 && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001560 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencerb5334b02007-02-05 10:18:06 +00001561 " arguments, but has size of " + itostr(NumElements) +"");
Reid Spencera132e042006-12-03 05:46:11 +00001562 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1563 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001564 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001565 }
1566 | Types 'c' STRINGCONSTANT {
Reid Spencer14310612006-12-31 05:40:51 +00001567 if (!UpRefs.empty())
1568 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001569 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001570 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001571 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001572 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001573
1574 int NumElements = ATy->getNumElements();
1575 const Type *ETy = ATy->getElementType();
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001576 if (NumElements != -1 && NumElements != int($3->length()))
Reid Spencer61c83e02006-08-18 08:43:06 +00001577 GEN_ERROR("Can't build string constant of size " +
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001578 itostr((int)($3->length())) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001579 " when array has size " + itostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001580 std::vector<Constant*> Vals;
Reid Spencer14310612006-12-31 05:40:51 +00001581 if (ETy == Type::Int8Ty) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001582 for (unsigned i = 0; i < $3->length(); ++i)
1583 Vals.push_back(ConstantInt::get(ETy, (*$3)[i]));
Chris Lattner58af2a12006-02-15 07:22:58 +00001584 } else {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001585 delete $3;
Reid Spencerb5334b02007-02-05 10:18:06 +00001586 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner58af2a12006-02-15 07:22:58 +00001587 }
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001588 delete $3;
Reid Spencera132e042006-12-03 05:46:11 +00001589 $$ = ConstantArray::get(ATy, Vals);
1590 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001591 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001592 }
1593 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001594 if (!UpRefs.empty())
1595 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00001596 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001597 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001598 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001599 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001600 const Type *ETy = PTy->getElementType();
1601 int NumElements = PTy->getNumElements();
1602
1603 // Verify that we have the correct size...
1604 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001605 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001606 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerb5334b02007-02-05 10:18:06 +00001607 itostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001608
1609 // Verify all elements are correct type!
1610 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001611 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001612 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001613 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001614 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001615 }
1616
Reid Spencer9d6565a2007-02-15 02:26:10 +00001617 $$ = ConstantVector::get(PTy, *$3);
Reid Spencera132e042006-12-03 05:46:11 +00001618 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001619 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001620 }
1621 | Types '{' ConstVector '}' {
Reid Spencera132e042006-12-03 05:46:11 +00001622 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001623 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001624 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001625 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001626
1627 if ($3->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001628 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001629
1630 // Check to ensure that constants are compatible with the type initializer!
1631 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencera132e042006-12-03 05:46:11 +00001632 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001633 GEN_ERROR("Expected type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001634 STy->getElementType(i)->getDescription() +
1635 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001636 " of structure initializer");
Chris Lattner58af2a12006-02-15 07:22:58 +00001637
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001638 // Check to ensure that Type is not packed
1639 if (STy->isPacked())
Chris Lattner6cdc6822007-04-26 05:31:05 +00001640 GEN_ERROR("Unpacked Initializer to vector type '" +
1641 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001642
Reid Spencera132e042006-12-03 05:46:11 +00001643 $$ = ConstantStruct::get(STy, *$3);
1644 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001645 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001646 }
1647 | Types '{' '}' {
Reid Spencer14310612006-12-31 05:40:51 +00001648 if (!UpRefs.empty())
1649 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001650 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001651 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001652 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001653 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001654
1655 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001656 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001657
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001658 // Check to ensure that Type is not packed
1659 if (STy->isPacked())
Chris Lattner6cdc6822007-04-26 05:31:05 +00001660 GEN_ERROR("Unpacked Initializer to vector type '" +
1661 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001662
1663 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1664 delete $1;
1665 CHECK_FOR_ERROR
1666 }
1667 | Types '<' '{' ConstVector '}' '>' {
1668 const StructType *STy = dyn_cast<StructType>($1->get());
1669 if (STy == 0)
1670 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001671 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001672
1673 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001674 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001675
1676 // Check to ensure that constants are compatible with the type initializer!
1677 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1678 if ((*$4)[i]->getType() != STy->getElementType(i))
1679 GEN_ERROR("Expected type '" +
1680 STy->getElementType(i)->getDescription() +
1681 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001682 " of structure initializer");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001683
1684 // Check to ensure that Type is packed
1685 if (!STy->isPacked())
Chris Lattner32980692007-02-19 07:44:24 +00001686 GEN_ERROR("Vector initializer to non-vector type '" +
1687 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001688
1689 $$ = ConstantStruct::get(STy, *$4);
1690 delete $1; delete $4;
1691 CHECK_FOR_ERROR
1692 }
1693 | Types '<' '{' '}' '>' {
1694 if (!UpRefs.empty())
1695 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1696 const StructType *STy = dyn_cast<StructType>($1->get());
1697 if (STy == 0)
1698 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001699 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001700
1701 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001702 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001703
1704 // Check to ensure that Type is packed
1705 if (!STy->isPacked())
Chris Lattner32980692007-02-19 07:44:24 +00001706 GEN_ERROR("Vector initializer to non-vector type '" +
1707 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001708
Reid Spencera132e042006-12-03 05:46:11 +00001709 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1710 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001711 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001712 }
1713 | Types NULL_TOK {
Reid Spencer14310612006-12-31 05:40:51 +00001714 if (!UpRefs.empty())
1715 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001716 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001717 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001718 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001719 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001720
Reid Spencera132e042006-12-03 05:46:11 +00001721 $$ = ConstantPointerNull::get(PTy);
1722 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001723 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001724 }
1725 | Types UNDEF {
Reid Spencer14310612006-12-31 05:40:51 +00001726 if (!UpRefs.empty())
1727 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001728 $$ = UndefValue::get($1->get());
1729 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001730 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001731 }
1732 | Types SymbolicValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00001733 if (!UpRefs.empty())
1734 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001735 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001736 if (Ty == 0)
Devang Patel5a970972008-02-19 22:27:01 +00001737 GEN_ERROR("Global const reference must be a pointer type " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001738
1739 // ConstExprs can exist in the body of a function, thus creating
1740 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencer93c40032007-03-19 18:40:50 +00001741 // the context of a function, getExistingVal will search the functions
Chris Lattner58af2a12006-02-15 07:22:58 +00001742 // symbol table instead of the module symbol table for the global symbol,
1743 // which throws things all off. To get around this, we just tell
Reid Spencer93c40032007-03-19 18:40:50 +00001744 // getExistingVal that we are at global scope here.
Chris Lattner58af2a12006-02-15 07:22:58 +00001745 //
1746 Function *SavedCurFn = CurFun.CurrentFunction;
1747 CurFun.CurrentFunction = 0;
1748
Reid Spencer93c40032007-03-19 18:40:50 +00001749 Value *V = getExistingVal(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001750 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001751
1752 CurFun.CurrentFunction = SavedCurFn;
1753
1754 // If this is an initializer for a constant pointer, which is referencing a
1755 // (currently) undefined variable, create a stub now that shall be replaced
1756 // in the future with the right type of variable.
1757 //
1758 if (V == 0) {
Reid Spencera9720f52007-02-05 17:04:00 +00001759 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001760 const PointerType *PT = cast<PointerType>(Ty);
1761
1762 // First check to see if the forward references value is already created!
1763 PerModuleInfo::GlobalRefsType::iterator I =
1764 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1765
1766 if (I != CurModule.GlobalRefs.end()) {
1767 V = I->second; // Placeholder already exists, use it...
1768 $2.destroy();
1769 } else {
1770 std::string Name;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001771 if ($2.Type == ValID::GlobalName)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001772 Name = $2.getName();
Reid Spencer41dff5e2007-01-26 08:05:27 +00001773 else if ($2.Type != ValID::GlobalID)
1774 GEN_ERROR("Invalid reference to global");
Chris Lattner58af2a12006-02-15 07:22:58 +00001775
1776 // Create the forward referenced global.
1777 GlobalValue *GV;
1778 if (const FunctionType *FTy =
1779 dyn_cast<FunctionType>(PT->getElementType())) {
Chris Lattner6cdc6822007-04-26 05:31:05 +00001780 GV = new Function(FTy, GlobalValue::ExternalWeakLinkage, Name,
Chris Lattner58af2a12006-02-15 07:22:58 +00001781 CurModule.CurrentModule);
1782 } else {
1783 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattner6cdc6822007-04-26 05:31:05 +00001784 GlobalValue::ExternalWeakLinkage, 0,
Chris Lattner58af2a12006-02-15 07:22:58 +00001785 Name, CurModule.CurrentModule);
1786 }
1787
1788 // Keep track of the fact that we have a forward ref to recycle it
1789 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1790 V = GV;
1791 }
1792 }
1793
Reid Spencera132e042006-12-03 05:46:11 +00001794 $$ = cast<GlobalValue>(V);
1795 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001796 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001797 }
1798 | Types ConstExpr {
Reid Spencer14310612006-12-31 05:40:51 +00001799 if (!UpRefs.empty())
1800 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001801 if ($1->get() != $2->getType())
Reid Spencere68853b2007-01-04 00:06:14 +00001802 GEN_ERROR("Mismatched types for constant expression: " +
1803 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001804 $$ = $2;
Reid Spencera132e042006-12-03 05:46:11 +00001805 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001806 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001807 }
1808 | Types ZEROINITIALIZER {
Reid Spencer14310612006-12-31 05:40:51 +00001809 if (!UpRefs.empty())
1810 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001811 const Type *Ty = $1->get();
Chris Lattner58af2a12006-02-15 07:22:58 +00001812 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerb5334b02007-02-05 10:18:06 +00001813 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencera132e042006-12-03 05:46:11 +00001814 $$ = Constant::getNullValue(Ty);
1815 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001816 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00001817 }
Reid Spencer14310612006-12-31 05:40:51 +00001818 | IntType ESINT64VAL { // integral constants
Reid Spencere4d87aa2006-12-23 06:05:41 +00001819 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001820 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencer49d273e2007-03-19 20:40:51 +00001821 $$ = ConstantInt::get($1, $2, true);
Reid Spencer38c91a92007-02-28 02:24:54 +00001822 CHECK_FOR_ERROR
1823 }
1824 | IntType ESAPINTVAL { // arbitrary precision integer constants
1825 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1826 if ($2->getBitWidth() > BitWidth) {
1827 GEN_ERROR("Constant value does not fit in type");
Reid Spencer10794272007-03-01 19:41:47 +00001828 }
1829 $2->sextOrTrunc(BitWidth);
1830 $$ = ConstantInt::get(*$2);
Reid Spencer38c91a92007-02-28 02:24:54 +00001831 delete $2;
Reid Spencere4d87aa2006-12-23 06:05:41 +00001832 CHECK_FOR_ERROR
1833 }
Reid Spencer14310612006-12-31 05:40:51 +00001834 | IntType EUINT64VAL { // integral constants
Reid Spencere4d87aa2006-12-23 06:05:41 +00001835 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001836 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencer49d273e2007-03-19 20:40:51 +00001837 $$ = ConstantInt::get($1, $2, false);
Reid Spencer38c91a92007-02-28 02:24:54 +00001838 CHECK_FOR_ERROR
1839 }
1840 | IntType EUAPINTVAL { // arbitrary precision integer constants
1841 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1842 if ($2->getBitWidth() > BitWidth) {
1843 GEN_ERROR("Constant value does not fit in type");
Reid Spencer10794272007-03-01 19:41:47 +00001844 }
1845 $2->zextOrTrunc(BitWidth);
1846 $$ = ConstantInt::get(*$2);
Reid Spencer38c91a92007-02-28 02:24:54 +00001847 delete $2;
Reid Spencere4d87aa2006-12-23 06:05:41 +00001848 CHECK_FOR_ERROR
1849 }
Reid Spencer6f407902007-01-13 05:00:46 +00001850 | INTTYPE TRUETOK { // Boolean constants
1851 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001852 $$ = ConstantInt::getTrue();
Reid Spencer61c83e02006-08-18 08:43:06 +00001853 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001854 }
Reid Spencer6f407902007-01-13 05:00:46 +00001855 | INTTYPE FALSETOK { // Boolean constants
1856 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001857 $$ = ConstantInt::getFalse();
Reid Spencer61c83e02006-08-18 08:43:06 +00001858 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001859 }
Dale Johannesenea583102007-09-12 03:31:28 +00001860 | FPType FPVAL { // Floating point constants
Dale Johannesen43421b32007-09-06 18:13:44 +00001861 if (!ConstantFP::isValueValidForType($1, *$2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001862 GEN_ERROR("Floating point constant invalid for type");
Dale Johannesenc72cd7e2007-09-11 18:33:39 +00001863 // Lexer has no type info, so builds all float and double FP constants
1864 // as double. Fix this here. Long double is done right.
1865 if (&$2->getSemantics()==&APFloat::IEEEdouble && $1==Type::FloatTy)
Dale Johannesen43421b32007-09-06 18:13:44 +00001866 $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
1867 $$ = ConstantFP::get($1, *$2);
Dale Johannesencdd509a2007-09-07 21:07:57 +00001868 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001869 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001870 };
1871
1872
Reid Spencer3da59db2006-11-27 01:05:10 +00001873ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer14310612006-12-31 05:40:51 +00001874 if (!UpRefs.empty())
1875 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001876 Constant *Val = $3;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001877 const Type *DestTy = $5->get();
1878 if (!CastInst::castIsValid($1, $3, DestTy))
1879 GEN_ERROR("invalid cast opcode for cast from '" +
1880 Val->getType()->getDescription() + "' to '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001881 DestTy->getDescription() + "'");
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001882 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00001883 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00001884 }
1885 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001886 if (!isa<PointerType>($3->getType()))
Reid Spencerb5334b02007-02-05 10:18:06 +00001887 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00001888
Reid Spencera132e042006-12-03 05:46:11 +00001889 const Type *IdxTy =
David Greene5fd22a82007-09-04 18:46:50 +00001890 GetElementPtrInst::getIndexedType($3->getType(), $4->begin(), $4->end(),
Chris Lattner7d9801d2007-02-13 00:58:01 +00001891 true);
Reid Spencera132e042006-12-03 05:46:11 +00001892 if (!IdxTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00001893 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencera132e042006-12-03 05:46:11 +00001894
Chris Lattnerf7469af2007-01-31 04:44:08 +00001895 SmallVector<Constant*, 8> IdxVec;
Reid Spencera132e042006-12-03 05:46:11 +00001896 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1897 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattner58af2a12006-02-15 07:22:58 +00001898 IdxVec.push_back(C);
1899 else
Reid Spencerb5334b02007-02-05 10:18:06 +00001900 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattner58af2a12006-02-15 07:22:58 +00001901
1902 delete $4;
1903
Chris Lattnerf7469af2007-01-31 04:44:08 +00001904 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer61c83e02006-08-18 08:43:06 +00001905 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001906 }
1907 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer4fe16d62007-01-11 18:21:29 +00001908 if ($3->getType() != Type::Int1Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00001909 GEN_ERROR("Select condition must be of boolean type");
Reid Spencera132e042006-12-03 05:46:11 +00001910 if ($5->getType() != $7->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001911 GEN_ERROR("Select operand types must match");
Reid Spencera132e042006-12-03 05:46:11 +00001912 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001913 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001914 }
1915 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001916 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001917 GEN_ERROR("Binary operator types must match");
Reid Spencer1628cec2006-10-26 06:15:43 +00001918 CHECK_FOR_ERROR;
Reid Spencer9eef56f2006-12-05 19:16:11 +00001919 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner58af2a12006-02-15 07:22:58 +00001920 }
1921 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001922 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001923 GEN_ERROR("Logical operator types must match");
Chris Lattner42a75512007-01-15 02:27:26 +00001924 if (!$3->getType()->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001925 if (Instruction::isShift($1) || !isa<VectorType>($3->getType()) ||
1926 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00001927 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00001928 }
Reid Spencera132e042006-12-03 05:46:11 +00001929 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001930 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001931 }
Reid Spencer4012e832006-12-04 05:24:24 +00001932 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1933 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001934 GEN_ERROR("icmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00001935 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00001936 }
Reid Spencer4012e832006-12-04 05:24:24 +00001937 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1938 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001939 GEN_ERROR("fcmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00001940 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00001941 }
Chris Lattner58af2a12006-02-15 07:22:58 +00001942 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001943 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerb5334b02007-02-05 10:18:06 +00001944 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00001945 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001946 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00001947 }
1948 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001949 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00001950 GEN_ERROR("Invalid insertelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00001951 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001952 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00001953 }
1954 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001955 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00001956 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00001957 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001958 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001959 };
1960
Chris Lattnerd25db202006-04-08 03:55:17 +00001961
Chris Lattner58af2a12006-02-15 07:22:58 +00001962// ConstVector - A list of comma separated constants.
1963ConstVector : ConstVector ',' ConstVal {
1964 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001965 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001966 }
1967 | ConstVal {
Reid Spencera132e042006-12-03 05:46:11 +00001968 $$ = new std::vector<Constant*>();
Chris Lattner58af2a12006-02-15 07:22:58 +00001969 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001970 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001971 };
1972
1973
1974// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1975GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
1976
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001977// ThreadLocal
1978ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
1979
Anton Korobeynikov38e09802007-04-28 13:48:45 +00001980// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
1981AliaseeRef : ResultTypes SymbolicValueRef {
1982 const Type* VTy = $1->get();
1983 Value *V = getVal(VTy, $2);
Chris Lattner0275cff2007-08-06 21:00:46 +00001984 CHECK_FOR_ERROR
Anton Korobeynikov38e09802007-04-28 13:48:45 +00001985 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
1986 if (!Aliasee)
1987 GEN_ERROR("Aliases can be created only to global values");
1988
1989 $$ = Aliasee;
1990 CHECK_FOR_ERROR
1991 delete $1;
1992 }
1993 | BITCAST '(' AliaseeRef TO Types ')' {
1994 Constant *Val = $3;
1995 const Type *DestTy = $5->get();
1996 if (!CastInst::castIsValid($1, $3, DestTy))
1997 GEN_ERROR("invalid cast opcode for cast from '" +
1998 Val->getType()->getDescription() + "' to '" +
1999 DestTy->getDescription() + "'");
2000
2001 $$ = ConstantExpr::getCast($1, $3, DestTy);
2002 CHECK_FOR_ERROR
2003 delete $5;
2004 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002005
2006//===----------------------------------------------------------------------===//
2007// Rules to match Modules
2008//===----------------------------------------------------------------------===//
2009
2010// Module rule: Capture the result of parsing the whole file into a result
2011// variable...
2012//
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002013Module
2014 : DefinitionList {
2015 $$ = ParserResult = CurModule.CurrentModule;
2016 CurModule.ModuleDone();
2017 CHECK_FOR_ERROR;
2018 }
2019 | /*empty*/ {
2020 $$ = ParserResult = CurModule.CurrentModule;
2021 CurModule.ModuleDone();
2022 CHECK_FOR_ERROR;
2023 }
2024 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002025
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002026DefinitionList
2027 : Definition
2028 | DefinitionList Definition
2029 ;
2030
2031Definition
Jeff Cohen361c3ef2007-01-21 19:19:31 +00002032 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner58af2a12006-02-15 07:22:58 +00002033 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00002034 CHECK_FOR_ERROR
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002035 }
2036 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer61c83e02006-08-18 08:43:06 +00002037 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002038 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002039 | MODULE ASM_TOK AsmBlock {
Reid Spencer61c83e02006-08-18 08:43:06 +00002040 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002041 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002042 | OptLocalAssign TYPE Types {
Reid Spencer14310612006-12-31 05:40:51 +00002043 if (!UpRefs.empty())
2044 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002045 // Eagerly resolve types. This is not an optimization, this is a
2046 // requirement that is due to the fact that we could have this:
2047 //
2048 // %list = type { %list * }
2049 // %list = type { %list * } ; repeated type decl
2050 //
2051 // If types are not resolved eagerly, then the two types will not be
2052 // determined to be the same type!
2053 //
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002054 ResolveTypeTo($1, *$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002055
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002056 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002057 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002058 // If this is a named type that is not a redefinition, add it to the slot
2059 // table.
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002060 CurModule.Types.push_back(*$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002061 }
Reid Spencera132e042006-12-03 05:46:11 +00002062
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002063 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002064 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002065 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002066 | OptLocalAssign TYPE VOID {
Reid Spencer14310612006-12-31 05:40:51 +00002067 ResolveTypeTo($1, $3);
2068
2069 if (!setTypeName($3, $1) && !$1) {
2070 CHECK_FOR_ERROR
2071 // If this is a named type that is not a redefinition, add it to the slot
2072 // table.
2073 CurModule.Types.push_back($3);
2074 }
2075 CHECK_FOR_ERROR
2076 }
Christopher Lambbf3348d2007-12-12 08:45:45 +00002077 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal
2078 OptAddrSpace {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002079 /* "Externally Visible" Linkage */
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002080 if ($5 == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002081 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002082 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
Christopher Lambbf3348d2007-12-12 08:45:45 +00002083 $2, $4, $5->getType(), $5, $3, $6);
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00002084 CHECK_FOR_ERROR
2085 } GlobalVarAttributes {
2086 CurGV = 0;
2087 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002088 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambbf3348d2007-12-12 08:45:45 +00002089 ConstVal OptAddrSpace {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002090 if ($6 == 0)
2091 GEN_ERROR("Global value initializer is not a constant");
Christopher Lambbf3348d2007-12-12 08:45:45 +00002092 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4, $7);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002093 CHECK_FOR_ERROR
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002094 } GlobalVarAttributes {
2095 CurGV = 0;
2096 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002097 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambbf3348d2007-12-12 08:45:45 +00002098 Types OptAddrSpace {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002099 if (!UpRefs.empty())
2100 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
Christopher Lambbf3348d2007-12-12 08:45:45 +00002101 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4, $7);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002102 CHECK_FOR_ERROR
2103 delete $6;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002104 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002105 CurGV = 0;
2106 CHECK_FOR_ERROR
2107 }
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002108 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002109 std::string Name;
2110 if ($1) {
2111 Name = *$1;
2112 delete $1;
2113 }
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002114 if (Name.empty())
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002115 GEN_ERROR("Alias name cannot be empty");
2116
2117 Constant* Aliasee = $5;
2118 if (Aliasee == 0)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002119 GEN_ERROR(std::string("Invalid aliasee for alias: ") + Name);
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002120
2121 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2122 CurModule.CurrentModule);
2123 GA->setVisibility($2);
2124 InsertValue(GA, CurModule.Values);
Chris Lattner569f7372007-09-10 23:24:14 +00002125
2126
2127 // If there was a forward reference of this alias, resolve it now.
2128
2129 ValID ID;
2130 if (!Name.empty())
2131 ID = ValID::createGlobalName(Name);
2132 else
2133 ID = ValID::createGlobalID(CurModule.Values.size()-1);
2134
2135 if (GlobalValue *FWGV =
2136 CurModule.GetForwardRefForGlobal(GA->getType(), ID)) {
2137 // Replace uses of the fwdref with the actual alias.
2138 FWGV->replaceAllUsesWith(GA);
2139 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(FWGV))
2140 GV->eraseFromParent();
2141 else
2142 cast<Function>(FWGV)->eraseFromParent();
2143 }
2144 ID.destroy();
2145
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002146 CHECK_FOR_ERROR
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002147 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002148 | TARGET TargetDefinition {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002149 CHECK_FOR_ERROR
2150 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002151 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00002152 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002153 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002154 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002155
2156
2157AsmBlock : STRINGCONSTANT {
2158 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattner58af2a12006-02-15 07:22:58 +00002159 if (AsmSoFar.empty())
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002160 CurModule.CurrentModule->setModuleInlineAsm(*$1);
Chris Lattner58af2a12006-02-15 07:22:58 +00002161 else
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002162 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+*$1);
2163 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002164 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002165};
2166
Reid Spencer41dff5e2007-01-26 08:05:27 +00002167TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002168 CurModule.CurrentModule->setTargetTriple(*$3);
2169 delete $3;
John Criswell2f6a8b12006-10-24 19:09:48 +00002170 }
Chris Lattner1ae022f2006-10-22 06:08:13 +00002171 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002172 CurModule.CurrentModule->setDataLayout(*$3);
2173 delete $3;
Owen Anderson1dc69692006-10-18 02:21:48 +00002174 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002175
2176LibrariesDefinition : '[' LibList ']';
2177
2178LibList : LibList ',' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002179 CurModule.CurrentModule->addLibrary(*$3);
2180 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002181 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002182 }
2183 | STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002184 CurModule.CurrentModule->addLibrary(*$1);
2185 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002186 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002187 }
2188 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00002189 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002190 }
2191 ;
2192
2193//===----------------------------------------------------------------------===//
2194// Rules to match Function Headers
2195//===----------------------------------------------------------------------===//
2196
Reid Spencer41dff5e2007-01-26 08:05:27 +00002197ArgListH : ArgListH ',' Types OptParamAttrs OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002198 if (!UpRefs.empty())
2199 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
2200 if (*$3 == Type::VoidTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00002201 GEN_ERROR("void typed arguments are invalid");
Reid Spencer14310612006-12-31 05:40:51 +00002202 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00002203 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002204 $1->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002205 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002206 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002207 | Types OptParamAttrs OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002208 if (!UpRefs.empty())
2209 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2210 if (*$1 == Type::VoidTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00002211 GEN_ERROR("void typed arguments are invalid");
Reid Spencer14310612006-12-31 05:40:51 +00002212 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2213 $$ = new ArgListType;
2214 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002215 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002216 };
2217
2218ArgList : ArgListH {
2219 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002220 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002221 }
2222 | ArgListH ',' DOTDOTDOT {
2223 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002224 struct ArgListEntry E;
2225 E.Ty = new PATypeHolder(Type::VoidTy);
2226 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002227 E.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00002228 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002229 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002230 }
2231 | DOTDOTDOT {
Reid Spencer14310612006-12-31 05:40:51 +00002232 $$ = new ArgListType;
2233 struct ArgListEntry E;
2234 E.Ty = new PATypeHolder(Type::VoidTy);
2235 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002236 E.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00002237 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002238 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002239 }
2240 | /* empty */ {
2241 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00002242 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002243 };
2244
Reid Spencer41dff5e2007-01-26 08:05:27 +00002245FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002246 OptFuncAttrs OptSection OptAlign OptGC {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002247 std::string FunctionName(*$3);
2248 delete $3; // Free strdup'd memory!
Chris Lattner58af2a12006-02-15 07:22:58 +00002249
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002250 // Check the function result for abstractness if this is a define. We should
2251 // have no abstract types at this point
Reid Spencer218ded22007-01-05 17:07:23 +00002252 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2253 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002254
Chris Lattner58af2a12006-02-15 07:22:58 +00002255 std::vector<const Type*> ParamTypeList;
Christopher Lamb5c104242007-04-22 20:09:11 +00002256 ParamAttrsVector Attrs;
2257 if ($7 != ParamAttr::None) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002258 ParamAttrsWithIndex PAWI;
2259 PAWI.index = 0;
2260 PAWI.attrs = $7;
Christopher Lamb5c104242007-04-22 20:09:11 +00002261 Attrs.push_back(PAWI);
2262 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002263 if ($5) { // If there are arguments...
Reid Spencer7b5d4662007-04-09 06:16:21 +00002264 unsigned index = 1;
2265 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002266 const Type* Ty = I->Ty->get();
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002267 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2268 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencer14310612006-12-31 05:40:51 +00002269 ParamTypeList.push_back(Ty);
2270 if (Ty != Type::VoidTy)
Christopher Lamb5c104242007-04-22 20:09:11 +00002271 if (I->Attrs != ParamAttr::None) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002272 ParamAttrsWithIndex PAWI;
2273 PAWI.index = index;
2274 PAWI.attrs = I->Attrs;
Christopher Lamb5c104242007-04-22 20:09:11 +00002275 Attrs.push_back(PAWI);
2276 }
Reid Spencer14310612006-12-31 05:40:51 +00002277 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002278 }
2279
2280 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2281 if (isVarArg) ParamTypeList.pop_back();
2282
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00002283 const ParamAttrsList *PAL = 0;
Christopher Lamb5c104242007-04-22 20:09:11 +00002284 if (!Attrs.empty())
2285 PAL = ParamAttrsList::get(Attrs);
Reid Spencer7b5d4662007-04-09 06:16:21 +00002286
Duncan Sandsdc024672007-11-27 13:23:08 +00002287 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002288 const PointerType *PFT = PointerType::getUnqual(FT);
Reid Spencer218ded22007-01-05 17:07:23 +00002289 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002290
2291 ValID ID;
2292 if (!FunctionName.empty()) {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002293 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattner58af2a12006-02-15 07:22:58 +00002294 } else {
Reid Spencer93c40032007-03-19 18:40:50 +00002295 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002296 }
2297
2298 Function *Fn = 0;
2299 // See if this function was forward referenced. If so, recycle the object.
2300 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2301 // Move the function to the end of the list, from whereever it was
2302 // previously inserted.
2303 Fn = cast<Function>(FWRef);
Duncan Sandsdc024672007-11-27 13:23:08 +00002304 assert(!Fn->getParamAttrs() && "Forward reference has parameter attributes!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002305 CurModule.CurrentModule->getFunctionList().remove(Fn);
2306 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2307 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spenceref9b9a72007-02-05 20:47:22 +00002308 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002309 if (Fn->getFunctionType() != FT ) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002310 // The existing function doesn't have the same type. This is an overload
2311 // error.
2312 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Duncan Sandsdc024672007-11-27 13:23:08 +00002313 } else if (Fn->getParamAttrs() != PAL) {
2314 // The existing function doesn't have the same parameter attributes.
2315 // This is an overload error.
2316 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002317 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
Chris Lattner6cdc6822007-04-26 05:31:05 +00002318 // Neither the existing or the current function is a declaration and they
2319 // have the same name and same type. Clearly this is a redefinition.
2320 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Duncan Sandsdc024672007-11-27 13:23:08 +00002321 } else if (Fn->isDeclaration()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002322 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattner58af2a12006-02-15 07:22:58 +00002323 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2324 AI != AE; ++AI)
2325 AI->setName("");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002326 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002327 } else { // Not already defined?
Chris Lattner6cdc6822007-04-26 05:31:05 +00002328 Fn = new Function(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
Chris Lattner58af2a12006-02-15 07:22:58 +00002329 CurModule.CurrentModule);
2330 InsertValue(Fn, CurModule.Values);
2331 }
2332
2333 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002334
2335 if (CurFun.isDeclare) {
2336 // If we have declaration, always overwrite linkage. This will allow us to
2337 // correctly handle cases, when pointer to function is passed as argument to
2338 // another function.
2339 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002340 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002341 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002342 Fn->setCallingConv($1);
Duncan Sandsdc024672007-11-27 13:23:08 +00002343 Fn->setParamAttrs(PAL);
Reid Spencer218ded22007-01-05 17:07:23 +00002344 Fn->setAlignment($9);
2345 if ($8) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002346 Fn->setSection(*$8);
2347 delete $8;
Chris Lattner58af2a12006-02-15 07:22:58 +00002348 }
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002349 if ($10) {
2350 Fn->setCollector($10->c_str());
2351 delete $10;
2352 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002353
2354 // Add all of the arguments we parsed to the function...
2355 if ($5) { // Is null if empty...
2356 if (isVarArg) { // Nuke the last entry
Reid Spenceref9b9a72007-02-05 20:47:22 +00002357 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0 &&
Reid Spencera9720f52007-02-05 17:04:00 +00002358 "Not a varargs marker!");
Reid Spencer14310612006-12-31 05:40:51 +00002359 delete $5->back().Ty;
Chris Lattner58af2a12006-02-15 07:22:58 +00002360 $5->pop_back(); // Delete the last entry
2361 }
2362 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002363 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencer14310612006-12-31 05:40:51 +00002364 unsigned Idx = 1;
Reid Spenceref9b9a72007-02-05 20:47:22 +00002365 for (ArgListType::iterator I = $5->begin();
2366 I != $5->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencer14310612006-12-31 05:40:51 +00002367 delete I->Ty; // Delete the typeholder...
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002368 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002369 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002370 InsertValue(ArgIt);
Reid Spencer14310612006-12-31 05:40:51 +00002371 Idx++;
Chris Lattner58af2a12006-02-15 07:22:58 +00002372 }
Reid Spencera132e042006-12-03 05:46:11 +00002373
Chris Lattner58af2a12006-02-15 07:22:58 +00002374 delete $5; // We're now done with the argument list
2375 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002376 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002377};
2378
2379BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2380
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002381FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner58af2a12006-02-15 07:22:58 +00002382 $$ = CurFun.CurrentFunction;
2383
2384 // Make sure that we keep track of the linkage type even if there was a
2385 // previous "declare".
2386 $$->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002387 $$->setVisibility($2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002388};
2389
2390END : ENDTOK | '}'; // Allow end of '}' to end a function
2391
2392Function : BasicBlockList END {
2393 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002394 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002395};
2396
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002397FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencer14310612006-12-31 05:40:51 +00002398 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002399 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002400 $$ = CurFun.CurrentFunction;
2401 CurFun.FunctionDone();
2402 CHECK_FOR_ERROR
2403 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002404
2405//===----------------------------------------------------------------------===//
2406// Rules to match Basic Blocks
2407//===----------------------------------------------------------------------===//
2408
2409OptSideEffect : /* empty */ {
2410 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002411 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002412 }
2413 | SIDEEFFECT {
2414 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002415 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002416 };
2417
2418ConstValueRef : ESINT64VAL { // A reference to a direct constant
2419 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002420 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002421 }
2422 | EUINT64VAL {
2423 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002424 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002425 }
2426 | FPVAL { // Perhaps it's an FP constant?
2427 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002428 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002429 }
2430 | TRUETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002431 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002432 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002433 }
2434 | FALSETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002435 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002436 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002437 }
2438 | NULL_TOK {
2439 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002440 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002441 }
2442 | UNDEF {
2443 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002444 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002445 }
2446 | ZEROINITIALIZER { // A vector zero constant.
2447 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002448 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002449 }
2450 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencera132e042006-12-03 05:46:11 +00002451 const Type *ETy = (*$2)[0]->getType();
Chris Lattner58af2a12006-02-15 07:22:58 +00002452 int NumElements = $2->size();
2453
Reid Spencer9d6565a2007-02-15 02:26:10 +00002454 VectorType* pt = VectorType::get(ETy, NumElements);
Chris Lattner58af2a12006-02-15 07:22:58 +00002455 PATypeHolder* PTy = new PATypeHolder(
Reid Spencera132e042006-12-03 05:46:11 +00002456 HandleUpRefs(
Reid Spencer9d6565a2007-02-15 02:26:10 +00002457 VectorType::get(
Reid Spencera132e042006-12-03 05:46:11 +00002458 ETy,
2459 NumElements)
2460 )
2461 );
Chris Lattner58af2a12006-02-15 07:22:58 +00002462
2463 // Verify all elements are correct type!
2464 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00002465 if (ETy != (*$2)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002466 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00002467 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencera132e042006-12-03 05:46:11 +00002468 (*$2)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00002469 }
2470
Reid Spencer9d6565a2007-02-15 02:26:10 +00002471 $$ = ValID::create(ConstantVector::get(pt, *$2));
Chris Lattner58af2a12006-02-15 07:22:58 +00002472 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002473 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002474 }
2475 | ConstExpr {
Reid Spencera132e042006-12-03 05:46:11 +00002476 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002477 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002478 }
2479 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002480 $$ = ValID::createInlineAsm(*$3, *$5, $2);
2481 delete $3;
2482 delete $5;
Reid Spencer61c83e02006-08-18 08:43:06 +00002483 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002484 };
2485
2486// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2487// another value.
2488//
Reid Spencer41dff5e2007-01-26 08:05:27 +00002489SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2490 $$ = ValID::createLocalID($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002491 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002492 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002493 | GLOBALVAL_ID {
2494 $$ = ValID::createGlobalID($1);
2495 CHECK_FOR_ERROR
2496 }
2497 | LocalName { // Is it a named reference...?
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002498 $$ = ValID::createLocalName(*$1);
2499 delete $1;
Reid Spencer41dff5e2007-01-26 08:05:27 +00002500 CHECK_FOR_ERROR
2501 }
2502 | GlobalName { // Is it a named reference...?
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002503 $$ = ValID::createGlobalName(*$1);
2504 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002505 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002506 };
2507
2508// ValueRef - A reference to a definition... either constant or symbolic
2509ValueRef : SymbolicValueRef | ConstValueRef;
2510
2511
2512// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2513// type immediately preceeds the value reference, and allows complex constant
2514// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2515ResolvedVal : Types ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002516 if (!UpRefs.empty())
2517 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2518 $$ = getVal(*$1, $2);
2519 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002520 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00002521 }
2522 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002523
2524BasicBlockList : BasicBlockList BasicBlock {
2525 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002526 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002527 }
2528 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
2529 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002530 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002531 };
2532
2533
2534// Basic blocks are terminated by branching instructions:
2535// br, br/cc, switch, ret
2536//
Reid Spencer41dff5e2007-01-26 08:05:27 +00002537BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner58af2a12006-02-15 07:22:58 +00002538 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002539 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002540 InsertValue($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002541 $1->getInstList().push_back($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002542 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002543 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002544 };
2545
2546InstructionList : InstructionList Inst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002547 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2548 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2549 if (CI2->getParent() == 0)
2550 $1->getInstList().push_back(CI2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002551 $1->getInstList().push_back($2);
2552 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002553 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002554 }
Reid Spencer93c40032007-03-19 18:40:50 +00002555 | /* empty */ { // Empty space between instruction lists
2556 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
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 | LABELSTR { // Labelled (named) basic block
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002560 $$ = defineBBVal(ValID::createLocalName(*$1));
2561 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002562 CHECK_FOR_ERROR
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002563
Chris Lattner58af2a12006-02-15 07:22:58 +00002564 };
2565
2566BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencera132e042006-12-03 05:46:11 +00002567 $$ = new ReturnInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002568 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002569 }
Reid Spencer93c40032007-03-19 18:40:50 +00002570 | RET VOID { // Return with no result...
Chris Lattner58af2a12006-02-15 07:22:58 +00002571 $$ = new ReturnInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002572 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002573 }
Reid Spencer93c40032007-03-19 18:40:50 +00002574 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002575 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002576 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002577 $$ = new BranchInst(tmpBB);
Reid Spencer93c40032007-03-19 18:40:50 +00002578 } // Conditional Branch...
Reid Spencer6f407902007-01-13 05:00:46 +00002579 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
2580 assert(cast<IntegerType>($2)->getBitWidth() == 1 && "Not Bool?");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002581 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002582 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002583 BasicBlock* tmpBBB = getBBVal($9);
2584 CHECK_FOR_ERROR
Reid Spencer4fe16d62007-01-11 18:21:29 +00002585 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002586 CHECK_FOR_ERROR
2587 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattner58af2a12006-02-15 07:22:58 +00002588 }
2589 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002590 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002591 CHECK_FOR_ERROR
2592 BasicBlock* tmpBB = getBBVal($6);
2593 CHECK_FOR_ERROR
2594 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002595 $$ = S;
2596
2597 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2598 E = $8->end();
2599 for (; I != E; ++I) {
2600 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2601 S->addCase(CI, I->second);
2602 else
Reid Spencerb5334b02007-02-05 10:18:06 +00002603 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattner58af2a12006-02-15 07:22:58 +00002604 }
2605 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002606 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002607 }
2608 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002609 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002610 CHECK_FOR_ERROR
2611 BasicBlock* tmpBB = getBBVal($6);
2612 CHECK_FOR_ERROR
2613 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattner58af2a12006-02-15 07:22:58 +00002614 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002615 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002616 }
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002617 | INVOKE OptCallingConv ResultTypes ValueRef '(' ParamList ')' OptFuncAttrs
Chris Lattner58af2a12006-02-15 07:22:58 +00002618 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattner58af2a12006-02-15 07:22:58 +00002619
Reid Spencer14310612006-12-31 05:40:51 +00002620 // Handle the short syntax
2621 const PointerType *PFTy = 0;
2622 const FunctionType *Ty = 0;
Reid Spencer218ded22007-01-05 17:07:23 +00002623 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00002624 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2625 // Pull out the types of all of the arguments...
2626 std::vector<const Type*> ParamTypes;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002627 ParamList::iterator I = $6->begin(), E = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002628 for (; I != E; ++I) {
Reid Spencer14310612006-12-31 05:40:51 +00002629 const Type *Ty = I->Val->getType();
2630 if (Ty == Type::VoidTy)
2631 GEN_ERROR("Short call syntax cannot be used with varargs");
2632 ParamTypes.push_back(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002633 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002634 Ty = FunctionType::get($3->get(), ParamTypes, false);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002635 PFTy = PointerType::getUnqual(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002636 }
2637
Reid Spencer66728ef2007-03-20 01:13:36 +00002638 delete $3;
2639
Chris Lattner58af2a12006-02-15 07:22:58 +00002640 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002641 CHECK_FOR_ERROR
Reid Spencer218ded22007-01-05 17:07:23 +00002642 BasicBlock *Normal = getBBVal($11);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002643 CHECK_FOR_ERROR
Reid Spencer218ded22007-01-05 17:07:23 +00002644 BasicBlock *Except = getBBVal($14);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002645 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002646
Duncan Sandsdc024672007-11-27 13:23:08 +00002647 ParamAttrsVector Attrs;
2648 if ($8 != ParamAttr::None) {
2649 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $8;
2650 Attrs.push_back(PAWI);
2651 }
2652
Reid Spencer14310612006-12-31 05:40:51 +00002653 // Check the arguments
2654 ValueList Args;
2655 if ($6->empty()) { // Has no arguments?
2656 // Make sure no arguments is a good thing!
2657 if (Ty->getNumParams() != 0)
2658 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00002659 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00002660 } else { // Has arguments?
2661 // Loop through FunctionType's arguments and ensure they are specified
2662 // correctly!
Chris Lattner58af2a12006-02-15 07:22:58 +00002663 FunctionType::param_iterator I = Ty->param_begin();
2664 FunctionType::param_iterator E = Ty->param_end();
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002665 ParamList::iterator ArgI = $6->begin(), ArgE = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002666 unsigned index = 1;
Chris Lattner58af2a12006-02-15 07:22:58 +00002667
Duncan Sandsdc024672007-11-27 13:23:08 +00002668 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002669 if (ArgI->Val->getType() != *I)
2670 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00002671 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00002672 Args.push_back(ArgI->Val);
Duncan Sandsdc024672007-11-27 13:23:08 +00002673 if (ArgI->Attrs != ParamAttr::None) {
2674 ParamAttrsWithIndex PAWI;
2675 PAWI.index = index;
2676 PAWI.attrs = ArgI->Attrs;
2677 Attrs.push_back(PAWI);
2678 }
Reid Spencer14310612006-12-31 05:40:51 +00002679 }
Reid Spencera132e042006-12-03 05:46:11 +00002680
Reid Spencer14310612006-12-31 05:40:51 +00002681 if (Ty->isVarArg()) {
2682 if (I == E)
Chris Lattner38905612008-02-19 04:36:25 +00002683 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002684 Args.push_back(ArgI->Val); // push the remaining varargs
Chris Lattner38905612008-02-19 04:36:25 +00002685 if (ArgI->Attrs != ParamAttr::None) {
2686 ParamAttrsWithIndex PAWI;
2687 PAWI.index = index;
2688 PAWI.attrs = ArgI->Attrs;
2689 Attrs.push_back(PAWI);
2690 }
2691 }
Reid Spencer14310612006-12-31 05:40:51 +00002692 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00002693 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00002694 }
Reid Spencer14310612006-12-31 05:40:51 +00002695
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00002696 const ParamAttrsList *PAL = 0;
Duncan Sandsdc024672007-11-27 13:23:08 +00002697 if (!Attrs.empty())
2698 PAL = ParamAttrsList::get(Attrs);
2699
Reid Spencer14310612006-12-31 05:40:51 +00002700 // Create the InvokeInst
Chris Lattnerd80fb8b2007-08-29 16:15:23 +00002701 InvokeInst *II = new InvokeInst(V, Normal, Except, Args.begin(), Args.end());
Reid Spencer14310612006-12-31 05:40:51 +00002702 II->setCallingConv($2);
Duncan Sandsdc024672007-11-27 13:23:08 +00002703 II->setParamAttrs(PAL);
Reid Spencer14310612006-12-31 05:40:51 +00002704 $$ = II;
Chris Lattner58af2a12006-02-15 07:22:58 +00002705 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002706 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002707 }
2708 | UNWIND {
2709 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002710 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002711 }
2712 | UNREACHABLE {
2713 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002714 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002715 };
2716
2717
2718
2719JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2720 $$ = $1;
Reid Spencer93c40032007-03-19 18:40:50 +00002721 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002722 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002723 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002724 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00002725
Reid Spencer5b7e7532006-09-28 19:28:24 +00002726 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002727 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002728 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002729 }
2730 | IntType ConstValueRef ',' LABEL ValueRef {
2731 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencer93c40032007-03-19 18:40:50 +00002732 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002733 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002734
2735 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002736 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00002737
Reid Spencer5b7e7532006-09-28 19:28:24 +00002738 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002739 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002740 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002741 };
2742
Reid Spencer41dff5e2007-01-26 08:05:27 +00002743Inst : OptLocalAssign InstVal {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002744 // Is this definition named?? if so, assign the name...
2745 setValueName($2, $1);
2746 CHECK_FOR_ERROR
2747 InsertValue($2);
2748 $$ = $2;
2749 CHECK_FOR_ERROR
2750 };
2751
Chris Lattner58af2a12006-02-15 07:22:58 +00002752
2753PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer14310612006-12-31 05:40:51 +00002754 if (!UpRefs.empty())
2755 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002756 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencera132e042006-12-03 05:46:11 +00002757 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002758 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002759 BasicBlock* tmpBB = getBBVal($5);
2760 CHECK_FOR_ERROR
2761 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencera132e042006-12-03 05:46:11 +00002762 delete $1;
Chris Lattner58af2a12006-02-15 07:22:58 +00002763 }
2764 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2765 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002766 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002767 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002768 BasicBlock* tmpBB = getBBVal($6);
2769 CHECK_FOR_ERROR
2770 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002771 };
2772
2773
Duncan Sandsdc024672007-11-27 13:23:08 +00002774ParamList : Types OptParamAttrs ValueRef OptParamAttrs {
2775 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Reid Spencer14310612006-12-31 05:40:51 +00002776 if (!UpRefs.empty())
2777 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2778 // Used for call and invoke instructions
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002779 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00002780 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getVal($1->get(), $3);
Reid Spencer14310612006-12-31 05:40:51 +00002781 $$->push_back(E);
Reid Spencer66728ef2007-03-20 01:13:36 +00002782 delete $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002783 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002784 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002785 | LABEL OptParamAttrs ValueRef OptParamAttrs {
2786 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002787 // Labels are only valid in ASMs
2788 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00002789 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getBBVal($3);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002790 $$->push_back(E);
Duncan Sandsdc024672007-11-27 13:23:08 +00002791 CHECK_FOR_ERROR
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002792 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002793 | ParamList ',' Types OptParamAttrs ValueRef OptParamAttrs {
2794 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Reid Spencer14310612006-12-31 05:40:51 +00002795 if (!UpRefs.empty())
2796 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002797 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002798 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getVal($3->get(), $5);
Reid Spencer14310612006-12-31 05:40:51 +00002799 $$->push_back(E);
Reid Spencer66728ef2007-03-20 01:13:36 +00002800 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002801 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00002802 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002803 | ParamList ',' LABEL OptParamAttrs ValueRef OptParamAttrs {
2804 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002805 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002806 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getBBVal($5);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002807 $$->push_back(E);
2808 CHECK_FOR_ERROR
2809 }
2810 | /*empty*/ { $$ = new ParamList(); };
Chris Lattner58af2a12006-02-15 07:22:58 +00002811
Reid Spencer14310612006-12-31 05:40:51 +00002812IndexList // Used for gep instructions and constant expressions
Reid Spencerc6c59fd2006-12-31 21:47:02 +00002813 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencer14310612006-12-31 05:40:51 +00002814 | IndexList ',' ResolvedVal {
2815 $$ = $1;
2816 $$->push_back($3);
2817 CHECK_FOR_ERROR
2818 }
Reid Spencerc6c59fd2006-12-31 21:47:02 +00002819 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002820
2821OptTailCall : TAIL CALL {
2822 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002823 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002824 }
2825 | CALL {
2826 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002827 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002828 };
2829
Chris Lattner58af2a12006-02-15 07:22:58 +00002830InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002831 if (!UpRefs.empty())
2832 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002833 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencer9d6565a2007-02-15 02:26:10 +00002834 !isa<VectorType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002835 GEN_ERROR(
Reid Spencerb5334b02007-02-05 10:18:06 +00002836 "Arithmetic operator requires integer, FP, or packed operands");
Reid Spencera132e042006-12-03 05:46:11 +00002837 Value* val1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002838 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002839 Value* val2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002840 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002841 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002842 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002843 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00002844 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002845 }
2846 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002847 if (!UpRefs.empty())
2848 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002849 if (!(*$2)->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00002850 if (Instruction::isShift($1) || !isa<VectorType>($2->get()) ||
2851 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00002852 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00002853 }
Reid Spencera132e042006-12-03 05:46:11 +00002854 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002855 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002856 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002857 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002858 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002859 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002860 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00002861 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002862 }
Reid Spencera132e042006-12-03 05:46:11 +00002863 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002864 if (!UpRefs.empty())
2865 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002866 if (isa<VectorType>((*$3).get()))
Chris Lattner32980692007-02-19 07:44:24 +00002867 GEN_ERROR("Vector types not supported by icmp instruction");
Reid Spencera132e042006-12-03 05:46:11 +00002868 Value* tmpVal1 = getVal(*$3, $4);
2869 CHECK_FOR_ERROR
2870 Value* tmpVal2 = getVal(*$3, $6);
2871 CHECK_FOR_ERROR
2872 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2873 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002874 GEN_ERROR("icmp operator returned null");
Reid Spencer66728ef2007-03-20 01:13:36 +00002875 delete $3;
Reid Spencera132e042006-12-03 05:46:11 +00002876 }
2877 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002878 if (!UpRefs.empty())
2879 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002880 if (isa<VectorType>((*$3).get()))
Chris Lattner32980692007-02-19 07:44:24 +00002881 GEN_ERROR("Vector types not supported by fcmp instruction");
Reid Spencera132e042006-12-03 05:46:11 +00002882 Value* tmpVal1 = getVal(*$3, $4);
2883 CHECK_FOR_ERROR
2884 Value* tmpVal2 = getVal(*$3, $6);
2885 CHECK_FOR_ERROR
2886 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2887 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002888 GEN_ERROR("fcmp operator returned null");
Reid Spencer66728ef2007-03-20 01:13:36 +00002889 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00002890 }
Reid Spencer3da59db2006-11-27 01:05:10 +00002891 | CastOps ResolvedVal TO Types {
Reid Spencer14310612006-12-31 05:40:51 +00002892 if (!UpRefs.empty())
2893 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00002894 Value* Val = $2;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00002895 const Type* DestTy = $4->get();
2896 if (!CastInst::castIsValid($1, Val, DestTy))
2897 GEN_ERROR("invalid cast opcode for cast from '" +
2898 Val->getType()->getDescription() + "' to '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00002899 DestTy->getDescription() + "'");
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00002900 $$ = CastInst::create($1, Val, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00002901 delete $4;
Chris Lattner58af2a12006-02-15 07:22:58 +00002902 }
2903 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer4fe16d62007-01-11 18:21:29 +00002904 if ($2->getType() != Type::Int1Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00002905 GEN_ERROR("select condition must be boolean");
Reid Spencera132e042006-12-03 05:46:11 +00002906 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002907 GEN_ERROR("select value types should match");
Reid Spencera132e042006-12-03 05:46:11 +00002908 $$ = new SelectInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002909 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002910 }
2911 | VAARG ResolvedVal ',' Types {
Reid Spencer14310612006-12-31 05:40:51 +00002912 if (!UpRefs.empty())
2913 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00002914 $$ = new VAArgInst($2, *$4);
2915 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002916 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002917 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002918 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002919 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerb5334b02007-02-05 10:18:06 +00002920 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00002921 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002922 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002923 }
2924 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002925 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00002926 GEN_ERROR("Invalid insertelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00002927 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002928 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002929 }
Chris Lattnerd5efe842006-04-08 01:18:56 +00002930 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002931 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00002932 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00002933 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002934 CHECK_FOR_ERROR
Chris Lattnerd5efe842006-04-08 01:18:56 +00002935 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002936 | PHI_TOK PHIList {
2937 const Type *Ty = $2->front().first->getType();
2938 if (!Ty->isFirstClassType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002939 GEN_ERROR("PHI node operands must be of first class type");
Chris Lattner58af2a12006-02-15 07:22:58 +00002940 $$ = new PHINode(Ty);
2941 ((PHINode*)$$)->reserveOperandSpace($2->size());
2942 while ($2->begin() != $2->end()) {
2943 if ($2->front().first->getType() != Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00002944 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattner58af2a12006-02-15 07:22:58 +00002945 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
2946 $2->pop_front();
2947 }
2948 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00002949 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002950 }
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002951 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ParamList ')'
Reid Spencer218ded22007-01-05 17:07:23 +00002952 OptFuncAttrs {
Reid Spencer14310612006-12-31 05:40:51 +00002953
2954 // Handle the short syntax
Reid Spencer3da59db2006-11-27 01:05:10 +00002955 const PointerType *PFTy = 0;
2956 const FunctionType *Ty = 0;
Reid Spencer218ded22007-01-05 17:07:23 +00002957 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00002958 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2959 // Pull out the types of all of the arguments...
2960 std::vector<const Type*> ParamTypes;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002961 ParamList::iterator I = $6->begin(), E = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002962 for (; I != E; ++I) {
Reid Spencer14310612006-12-31 05:40:51 +00002963 const Type *Ty = I->Val->getType();
2964 if (Ty == Type::VoidTy)
2965 GEN_ERROR("Short call syntax cannot be used with varargs");
2966 ParamTypes.push_back(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002967 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002968 Ty = FunctionType::get($3->get(), ParamTypes, false);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002969 PFTy = PointerType::getUnqual(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002970 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002971
Chris Lattner58af2a12006-02-15 07:22:58 +00002972 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002973 CHECK_FOR_ERROR
Chris Lattner6cdc6822007-04-26 05:31:05 +00002974
Reid Spencer7780acb2007-04-16 06:56:07 +00002975 // Check for call to invalid intrinsic to avoid crashing later.
2976 if (Function *theF = dyn_cast<Function>(V)) {
Reid Spencered48de22007-04-16 22:02:23 +00002977 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
Reid Spencer36fdde12007-04-16 20:35:38 +00002978 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
2979 !theF->getIntrinsicID(true))
Reid Spencer7780acb2007-04-16 06:56:07 +00002980 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
2981 theF->getName() + "'");
2982 }
2983
Duncan Sandsdc024672007-11-27 13:23:08 +00002984 // Set up the ParamAttrs for the function
2985 ParamAttrsVector Attrs;
2986 if ($8 != ParamAttr::None) {
2987 ParamAttrsWithIndex PAWI;
2988 PAWI.index = 0;
2989 PAWI.attrs = $8;
2990 Attrs.push_back(PAWI);
2991 }
Reid Spencer14310612006-12-31 05:40:51 +00002992 // Check the arguments
2993 ValueList Args;
2994 if ($6->empty()) { // Has no arguments?
Chris Lattner58af2a12006-02-15 07:22:58 +00002995 // Make sure no arguments is a good thing!
2996 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002997 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00002998 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00002999 } else { // Has arguments?
3000 // Loop through FunctionType's arguments and ensure they are specified
Duncan Sandsdc024672007-11-27 13:23:08 +00003001 // correctly. Also, gather any parameter attributes.
Chris Lattner58af2a12006-02-15 07:22:58 +00003002 FunctionType::param_iterator I = Ty->param_begin();
3003 FunctionType::param_iterator E = Ty->param_end();
Dale Johanneseneb57ea72007-11-05 21:20:28 +00003004 ParamList::iterator ArgI = $6->begin(), ArgE = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00003005 unsigned index = 1;
Chris Lattner58af2a12006-02-15 07:22:58 +00003006
Duncan Sandsdc024672007-11-27 13:23:08 +00003007 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00003008 if (ArgI->Val->getType() != *I)
3009 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00003010 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00003011 Args.push_back(ArgI->Val);
Duncan Sandsdc024672007-11-27 13:23:08 +00003012 if (ArgI->Attrs != ParamAttr::None) {
3013 ParamAttrsWithIndex PAWI;
3014 PAWI.index = index;
3015 PAWI.attrs = ArgI->Attrs;
3016 Attrs.push_back(PAWI);
3017 }
Reid Spencer14310612006-12-31 05:40:51 +00003018 }
3019 if (Ty->isVarArg()) {
3020 if (I == E)
Chris Lattner38905612008-02-19 04:36:25 +00003021 for (; ArgI != ArgE; ++ArgI, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00003022 Args.push_back(ArgI->Val); // push the remaining varargs
Chris Lattner38905612008-02-19 04:36:25 +00003023 if (ArgI->Attrs != ParamAttr::None) {
3024 ParamAttrsWithIndex PAWI;
3025 PAWI.index = index;
3026 PAWI.attrs = ArgI->Attrs;
3027 Attrs.push_back(PAWI);
3028 }
3029 }
Reid Spencer14310612006-12-31 05:40:51 +00003030 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00003031 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00003032 }
Duncan Sandsdc024672007-11-27 13:23:08 +00003033
3034 // Finish off the ParamAttrs and check them
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00003035 const ParamAttrsList *PAL = 0;
Duncan Sandsdc024672007-11-27 13:23:08 +00003036 if (!Attrs.empty())
3037 PAL = ParamAttrsList::get(Attrs);
3038
Reid Spencer14310612006-12-31 05:40:51 +00003039 // Create the call node
David Greene718fda32007-08-01 03:59:32 +00003040 CallInst *CI = new CallInst(V, Args.begin(), Args.end());
Reid Spencer14310612006-12-31 05:40:51 +00003041 CI->setTailCall($1);
3042 CI->setCallingConv($2);
Duncan Sandsdc024672007-11-27 13:23:08 +00003043 CI->setParamAttrs(PAL);
Reid Spencer14310612006-12-31 05:40:51 +00003044 $$ = CI;
Chris Lattner58af2a12006-02-15 07:22:58 +00003045 delete $6;
Reid Spencer41dff5e2007-01-26 08:05:27 +00003046 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00003047 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003048 }
3049 | MemoryInst {
3050 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00003051 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003052 };
3053
Chris Lattner58af2a12006-02-15 07:22:58 +00003054OptVolatile : VOLATILE {
3055 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00003056 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003057 }
3058 | /* empty */ {
3059 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00003060 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003061 };
3062
3063
3064
3065MemoryInst : MALLOC Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003066 if (!UpRefs.empty())
3067 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003068 $$ = new MallocInst(*$2, 0, $3);
3069 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003070 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003071 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003072 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003073 if (!UpRefs.empty())
3074 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003075 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003076 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003077 $$ = new MallocInst(*$2, tmpVal, $6);
3078 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003079 }
3080 | ALLOCA Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003081 if (!UpRefs.empty())
3082 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003083 $$ = new AllocaInst(*$2, 0, $3);
3084 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003085 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003086 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003087 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003088 if (!UpRefs.empty())
3089 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003090 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003091 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003092 $$ = new AllocaInst(*$2, tmpVal, $6);
3093 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003094 }
3095 | FREE ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003096 if (!isa<PointerType>($2->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003097 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerb5334b02007-02-05 10:18:06 +00003098 $2->getType()->getDescription() + "");
Reid Spencera132e042006-12-03 05:46:11 +00003099 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00003100 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003101 }
3102
Christopher Lamb5c104242007-04-22 20:09:11 +00003103 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003104 if (!UpRefs.empty())
3105 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003106 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003107 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003108 (*$3)->getDescription());
3109 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00003110 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003111 (*$3)->getDescription());
3112 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003113 CHECK_FOR_ERROR
Christopher Lamb5c104242007-04-22 20:09:11 +00003114 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencera132e042006-12-03 05:46:11 +00003115 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00003116 }
Christopher Lamb5c104242007-04-22 20:09:11 +00003117 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003118 if (!UpRefs.empty())
3119 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003120 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00003121 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00003122 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003123 (*$5)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00003124 const Type *ElTy = PT->getElementType();
Reid Spencera132e042006-12-03 05:46:11 +00003125 if (ElTy != $3->getType())
3126 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerb5334b02007-02-05 10:18:06 +00003127 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00003128
Reid Spencera132e042006-12-03 05:46:11 +00003129 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003130 CHECK_FOR_ERROR
Christopher Lamb5c104242007-04-22 20:09:11 +00003131 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencera132e042006-12-03 05:46:11 +00003132 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00003133 }
Devang Patel5a970972008-02-19 22:27:01 +00003134| GETRESULT Types LocalName ',' ConstVal {
3135 ValID TmpVID = ValID::createLocalName(*$3);
3136 Value *TmpVal = getVal($2->get(), TmpVID);
3137 if (!GetResultInst::isValidOperands(TmpVal, $5))
3138 GEN_ERROR("Invalid getresult operands");
3139 $$ = new GetResultInst(TmpVal, $5);
3140 CHECK_FOR_ERROR
3141 }
Chris Lattner58af2a12006-02-15 07:22:58 +00003142 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer14310612006-12-31 05:40:51 +00003143 if (!UpRefs.empty())
3144 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003145 if (!isa<PointerType>($2->get()))
Reid Spencerb5334b02007-02-05 10:18:06 +00003146 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00003147
David Greene5fd22a82007-09-04 18:46:50 +00003148 if (!GetElementPtrInst::getIndexedType(*$2, $4->begin(), $4->end(), true))
Reid Spencer61c83e02006-08-18 08:43:06 +00003149 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00003150 (*$2)->getDescription()+ "'");
Reid Spencera132e042006-12-03 05:46:11 +00003151 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003152 CHECK_FOR_ERROR
David Greene5fd22a82007-09-04 18:46:50 +00003153 $$ = new GetElementPtrInst(tmpVal, $4->begin(), $4->end());
Reid Spencera132e042006-12-03 05:46:11 +00003154 delete $2;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003155 delete $4;
Chris Lattner58af2a12006-02-15 07:22:58 +00003156 };
3157
3158
3159%%
Reid Spencer61c83e02006-08-18 08:43:06 +00003160
Reid Spencer14310612006-12-31 05:40:51 +00003161// common code from the two 'RunVMAsmParser' functions
3162static Module* RunParser(Module * M) {
Reid Spencer14310612006-12-31 05:40:51 +00003163 CurModule.CurrentModule = M;
Reid Spencer14310612006-12-31 05:40:51 +00003164 // Check to make sure the parser succeeded
3165 if (yyparse()) {
3166 if (ParserResult)
3167 delete ParserResult;
3168 return 0;
3169 }
3170
Reid Spencer0d60b5a2007-03-30 01:37:39 +00003171 // Emit an error if there are any unresolved types left.
3172 if (!CurModule.LateResolveTypes.empty()) {
3173 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3174 if (DID.Type == ValID::LocalName) {
3175 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3176 } else {
3177 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3178 }
3179 if (ParserResult)
3180 delete ParserResult;
3181 return 0;
3182 }
3183
3184 // Emit an error if there are any unresolved values left.
3185 if (!CurModule.LateResolveValues.empty()) {
3186 Value *V = CurModule.LateResolveValues.back();
3187 std::map<Value*, std::pair<ValID, int> >::iterator I =
3188 CurModule.PlaceHolderInfo.find(V);
3189
3190 if (I != CurModule.PlaceHolderInfo.end()) {
3191 ValID &DID = I->second.first;
3192 if (DID.Type == ValID::LocalName) {
3193 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3194 } else {
3195 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3196 }
3197 if (ParserResult)
3198 delete ParserResult;
3199 return 0;
3200 }
3201 }
3202
Reid Spencer14310612006-12-31 05:40:51 +00003203 // Check to make sure that parsing produced a result
3204 if (!ParserResult)
3205 return 0;
3206
3207 // Reset ParserResult variable while saving its value for the result.
3208 Module *Result = ParserResult;
3209 ParserResult = 0;
3210
3211 return Result;
3212}
3213
Reid Spencer61c83e02006-08-18 08:43:06 +00003214void llvm::GenerateError(const std::string &message, int LineNo) {
Duncan Sandsdc024672007-11-27 13:23:08 +00003215 if (LineNo == -1) LineNo = LLLgetLineNo();
Reid Spencer61c83e02006-08-18 08:43:06 +00003216 // TODO: column number in exception
3217 if (TheParseError)
Duncan Sandsdc024672007-11-27 13:23:08 +00003218 TheParseError->setError(LLLgetFilename(), message, LineNo);
Reid Spencer61c83e02006-08-18 08:43:06 +00003219 TriggerError = 1;
3220}
3221
Chris Lattner58af2a12006-02-15 07:22:58 +00003222int yyerror(const char *ErrorMsg) {
Duncan Sandsdc024672007-11-27 13:23:08 +00003223 std::string where = LLLgetFilename() + ":" + utostr(LLLgetLineNo()) + ": ";
Reid Spenceref9b9a72007-02-05 20:47:22 +00003224 std::string errMsg = where + "error: " + std::string(ErrorMsg);
Duncan Sandsdc024672007-11-27 13:23:08 +00003225 if (yychar != YYEMPTY && yychar != 0) {
3226 errMsg += " while reading token: '";
3227 errMsg += std::string(LLLgetTokenStart(),
3228 LLLgetTokenStart()+LLLgetTokenLength()) + "'";
3229 }
Reid Spencer61c83e02006-08-18 08:43:06 +00003230 GenerateError(errMsg);
Chris Lattner58af2a12006-02-15 07:22:58 +00003231 return 0;
3232}