blob: 9f12b3eaa3990795bdaf8fdce58028f64da2ce18 [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??
Reid Spencerb83eb642006-10-20 07:07:24 +0000381 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000382 GenerateError("Signed integral constant '" +
Chris Lattner58af2a12006-02-15 07:22:58 +0000383 itostr(D.ConstPool64) + "' is invalid for type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000384 Ty->getDescription() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000385 return 0;
386 }
Reid Spencer49d273e2007-03-19 20:40:51 +0000387 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner58af2a12006-02-15 07:22:58 +0000388
389 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Reid Spencerb83eb642006-10-20 07:07:24 +0000390 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
391 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000392 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Reid Spencerb5334b02007-02-05 10:18:06 +0000393 "' is invalid or out of range");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000394 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000395 } else { // This is really a signed reference. Transmogrify.
Reid Spencer49d273e2007-03-19 20:40:51 +0000396 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner58af2a12006-02-15 07:22:58 +0000397 }
398 } else {
Reid Spencerb83eb642006-10-20 07:07:24 +0000399 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattner58af2a12006-02-15 07:22:58 +0000400 }
401
402 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Dale Johannesen43421b32007-09-06 18:13:44 +0000403 if (!ConstantFP::isValueValidForType(Ty, *D.ConstPoolFP)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000404 GenerateError("FP constant invalid for type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000405 return 0;
406 }
Dale Johannesenc72cd7e2007-09-11 18:33:39 +0000407 // Lexer has no type info, so builds all float and double FP constants
408 // as double. Fix this here. Long double does not need this.
409 if (&D.ConstPoolFP->getSemantics() == &APFloat::IEEEdouble &&
410 Ty==Type::FloatTy)
Dale Johannesen43421b32007-09-06 18:13:44 +0000411 D.ConstPoolFP->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
412 return ConstantFP::get(Ty, *D.ConstPoolFP);
Chris Lattner58af2a12006-02-15 07:22:58 +0000413
414 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000415 if (!isa<PointerType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000416 GenerateError("Cannot create a a non pointer null");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000417 return 0;
418 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000419 return ConstantPointerNull::get(cast<PointerType>(Ty));
420
421 case ValID::ConstUndefVal: // Is it an undef value?
422 return UndefValue::get(Ty);
423
424 case ValID::ConstZeroVal: // Is it a zero value?
425 return Constant::getNullValue(Ty);
426
427 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000428 if (D.ConstantValue->getType() != Ty) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000429 GenerateError("Constant expression type different from required type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000430 return 0;
431 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000432 return D.ConstantValue;
433
434 case ValID::InlineAsmVal: { // Inline asm expression
435 const PointerType *PTy = dyn_cast<PointerType>(Ty);
436 const FunctionType *FTy =
437 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000438 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000439 GenerateError("Invalid type for asm constraint string");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000440 return 0;
441 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000442 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
443 D.IAD->HasSideEffects);
444 D.destroy(); // Free InlineAsmDescriptor.
445 return IA;
446 }
447 default:
Reid Spencera9720f52007-02-05 17:04:00 +0000448 assert(0 && "Unhandled case!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000449 return 0;
450 } // End of switch
451
Reid Spencera9720f52007-02-05 17:04:00 +0000452 assert(0 && "Unhandled case!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000453 return 0;
454}
455
Reid Spencer93c40032007-03-19 18:40:50 +0000456// getVal - This function is identical to getExistingVal, except that if a
Chris Lattner58af2a12006-02-15 07:22:58 +0000457// value is not already defined, it "improvises" by creating a placeholder var
458// that looks and acts just like the requested variable. When the value is
459// defined later, all uses of the placeholder variable are replaced with the
460// real thing.
461//
462static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000463 if (Ty == Type::LabelTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000464 GenerateError("Cannot use a basic block here");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000465 return 0;
466 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000467
468 // See if the value has already been defined.
Reid Spencer93c40032007-03-19 18:40:50 +0000469 Value *V = getExistingVal(Ty, ID);
Chris Lattner58af2a12006-02-15 07:22:58 +0000470 if (V) return V;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000471 if (TriggerError) return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000472
Reid Spencer5b7e7532006-09-28 19:28:24 +0000473 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000474 GenerateError("Invalid use of a composite type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000475 return 0;
476 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000477
478 // If we reached here, we referenced either a symbol that we don't know about
479 // or an id number that hasn't been read yet. We may be referencing something
480 // forward, so just create an entry to be resolved later and get to it...
481 //
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000482 switch (ID.Type) {
483 case ValID::GlobalName:
Reid Spencer9c9b63a2007-04-28 16:07:31 +0000484 case ValID::GlobalID: {
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000485 const PointerType *PTy = dyn_cast<PointerType>(Ty);
486 if (!PTy) {
487 GenerateError("Invalid type for reference to global" );
488 return 0;
489 }
490 const Type* ElTy = PTy->getElementType();
491 if (const FunctionType *FTy = dyn_cast<FunctionType>(ElTy))
492 V = new Function(FTy, GlobalValue::ExternalLinkage);
493 else
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000494 V = new GlobalVariable(ElTy, false, GlobalValue::ExternalLinkage, 0, "",
495 (Module*)0, false, PTy->getAddressSpace());
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000496 break;
Reid Spencer9c9b63a2007-04-28 16:07:31 +0000497 }
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000498 default:
499 V = new Argument(Ty);
500 }
501
Chris Lattner58af2a12006-02-15 07:22:58 +0000502 // Remember where this forward reference came from. FIXME, shouldn't we try
503 // to recycle these things??
504 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Duncan Sandsdc024672007-11-27 13:23:08 +0000505 LLLgetLineNo())));
Chris Lattner58af2a12006-02-15 07:22:58 +0000506
507 if (inFunctionScope())
508 InsertValue(V, CurFun.LateResolveValues);
509 else
510 InsertValue(V, CurModule.LateResolveValues);
511 return V;
512}
513
Reid Spencer93c40032007-03-19 18:40:50 +0000514/// defineBBVal - This is a definition of a new basic block with the specified
515/// identifier which must be the same as CurFun.NextValNum, if its numeric.
516static BasicBlock *defineBBVal(const ValID &ID) {
Reid Spencera9720f52007-02-05 17:04:00 +0000517 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000518
Chris Lattner58af2a12006-02-15 07:22:58 +0000519 BasicBlock *BB = 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000520
Reid Spencer93c40032007-03-19 18:40:50 +0000521 // First, see if this was forward referenced
Chris Lattner58af2a12006-02-15 07:22:58 +0000522
Reid Spencer93c40032007-03-19 18:40:50 +0000523 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
524 if (BBI != CurFun.BBForwardRefs.end()) {
525 BB = BBI->second;
Chris Lattner58af2a12006-02-15 07:22:58 +0000526 // The forward declaration could have been inserted anywhere in the
527 // function: insert it into the correct place now.
528 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
529 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Reid Spencer93c40032007-03-19 18:40:50 +0000530
Reid Spencer66728ef2007-03-20 01:13:36 +0000531 // We're about to erase the entry, save the key so we can clean it up.
532 ValID Tmp = BBI->first;
533
Reid Spencer93c40032007-03-19 18:40:50 +0000534 // Erase the forward ref from the map as its no longer "forward"
535 CurFun.BBForwardRefs.erase(ID);
536
Reid Spencer66728ef2007-03-20 01:13:36 +0000537 // The key has been removed from the map but so we don't want to leave
538 // strdup'd memory around so destroy it too.
539 Tmp.destroy();
540
Reid Spencer93c40032007-03-19 18:40:50 +0000541 // If its a numbered definition, bump the number and set the BB value.
542 if (ID.Type == ValID::LocalID) {
543 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
544 InsertValue(BB);
545 }
546
547 ID.destroy();
548 return BB;
549 }
550
551 // We haven't seen this BB before and its first mention is a definition.
552 // Just create it and return it.
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000553 std::string Name (ID.Type == ValID::LocalName ? ID.getName() : "");
Reid Spencer93c40032007-03-19 18:40:50 +0000554 BB = new BasicBlock(Name, CurFun.CurrentFunction);
555 if (ID.Type == ValID::LocalID) {
556 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
557 InsertValue(BB);
Chris Lattner58af2a12006-02-15 07:22:58 +0000558 }
Reid Spencer93c40032007-03-19 18:40:50 +0000559
560 ID.destroy(); // Free strdup'd memory
561 return BB;
562}
563
564/// getBBVal - get an existing BB value or create a forward reference for it.
565///
566static BasicBlock *getBBVal(const ValID &ID) {
567 assert(inFunctionScope() && "Can't get basic block at global scope!");
568
569 BasicBlock *BB = 0;
570
571 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
572 if (BBI != CurFun.BBForwardRefs.end()) {
573 BB = BBI->second;
574 } if (ID.Type == ValID::LocalName) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000575 std::string Name = ID.getName();
Reid Spencer93c40032007-03-19 18:40:50 +0000576 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
577 if (N)
578 if (N->getType()->getTypeID() == Type::LabelTyID)
579 BB = cast<BasicBlock>(N);
580 else
581 GenerateError("Reference to label '" + Name + "' is actually of type '"+
582 N->getType()->getDescription() + "'");
583 } else if (ID.Type == ValID::LocalID) {
584 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
585 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
586 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
587 else
588 GenerateError("Reference to label '%" + utostr(ID.Num) +
589 "' is actually of type '"+
590 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
591 }
592 } else {
593 GenerateError("Illegal label reference " + ID.getName());
594 return 0;
595 }
596
597 // If its already been defined, return it now.
598 if (BB) {
599 ID.destroy(); // Free strdup'd memory.
600 return BB;
601 }
602
603 // Otherwise, this block has not been seen before, create it.
604 std::string Name;
605 if (ID.Type == ValID::LocalName)
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000606 Name = ID.getName();
Reid Spencer93c40032007-03-19 18:40:50 +0000607 BB = new BasicBlock(Name, CurFun.CurrentFunction);
608
609 // Insert it in the forward refs map.
610 CurFun.BBForwardRefs[ID] = BB;
611
Chris Lattner58af2a12006-02-15 07:22:58 +0000612 return BB;
613}
614
615
616//===----------------------------------------------------------------------===//
617// Code to handle forward references in instructions
618//===----------------------------------------------------------------------===//
619//
620// This code handles the late binding needed with statements that reference
621// values not defined yet... for example, a forward branch, or the PHI node for
622// a loop body.
623//
624// This keeps a table (CurFun.LateResolveValues) of all such forward references
625// and back patchs after we are done.
626//
627
628// ResolveDefinitions - If we could not resolve some defs at parsing
629// time (forward branches, phi functions for loops, etc...) resolve the
630// defs now...
631//
632static void
Reid Spencer93c40032007-03-19 18:40:50 +0000633ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000634 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencer93c40032007-03-19 18:40:50 +0000635 while (!LateResolvers.empty()) {
636 Value *V = LateResolvers.back();
637 LateResolvers.pop_back();
Chris Lattner58af2a12006-02-15 07:22:58 +0000638
Reid Spencer93c40032007-03-19 18:40:50 +0000639 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
640 CurModule.PlaceHolderInfo.find(V);
641 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000642
Reid Spencer93c40032007-03-19 18:40:50 +0000643 ValID &DID = PHI->second.first;
Chris Lattner58af2a12006-02-15 07:22:58 +0000644
Reid Spencer93c40032007-03-19 18:40:50 +0000645 Value *TheRealValue = getExistingVal(V->getType(), DID);
646 if (TriggerError)
647 return;
648 if (TheRealValue) {
649 V->replaceAllUsesWith(TheRealValue);
650 delete V;
651 CurModule.PlaceHolderInfo.erase(PHI);
652 } else if (FutureLateResolvers) {
653 // Functions have their unresolved items forwarded to the module late
654 // resolver table
655 InsertValue(V, *FutureLateResolvers);
656 } else {
657 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
658 GenerateError("Reference to an invalid definition: '" +DID.getName()+
659 "' of type '" + V->getType()->getDescription() + "'",
660 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000661 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000662 } else {
Reid Spencer93c40032007-03-19 18:40:50 +0000663 GenerateError("Reference to an invalid definition: #" +
664 itostr(DID.Num) + " of type '" +
665 V->getType()->getDescription() + "'",
666 PHI->second.second);
667 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000668 }
669 }
670 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000671 LateResolvers.clear();
672}
673
674// ResolveTypeTo - A brand new type was just declared. This means that (if
675// name is not null) things referencing Name can be resolved. Otherwise, things
676// refering to the number can be resolved. Do this now.
677//
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000678static void ResolveTypeTo(std::string *Name, const Type *ToTy) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000679 ValID D;
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000680 if (Name)
681 D = ValID::createLocalName(*Name);
682 else
683 D = ValID::createLocalID(CurModule.Types.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000684
Reid Spencer861d9d62006-11-28 07:29:44 +0000685 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattner58af2a12006-02-15 07:22:58 +0000686 CurModule.LateResolveTypes.find(D);
687 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencer861d9d62006-11-28 07:29:44 +0000688 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner58af2a12006-02-15 07:22:58 +0000689 CurModule.LateResolveTypes.erase(I);
690 }
691}
692
693// setValueName - Set the specified value to the name given. The name may be
694// null potentially, in which case this is a noop. The string passed in is
695// assumed to be a malloc'd string buffer, and is free'd by this function.
696//
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000697static void setValueName(Value *V, std::string *NameStr) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000698 if (!NameStr) return;
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000699 std::string Name(*NameStr); // Copy string
700 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000701
Reid Spencer41dff5e2007-01-26 08:05:27 +0000702 if (V->getType() == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000703 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000704 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000705 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000706
Reid Spencera9720f52007-02-05 17:04:00 +0000707 assert(inFunctionScope() && "Must be in function scope!");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000708 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
709 if (ST.lookup(Name)) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000710 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000711 V->getType()->getDescription() + "'");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000712 return;
713 }
714
715 // Set the name.
716 V->setName(Name);
Chris Lattner58af2a12006-02-15 07:22:58 +0000717}
718
719/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
720/// this is a declaration, otherwise it is a definition.
721static GlobalVariable *
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000722ParseGlobalVariable(std::string *NameStr,
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000723 GlobalValue::LinkageTypes Linkage,
724 GlobalValue::VisibilityTypes Visibility,
Chris Lattner58af2a12006-02-15 07:22:58 +0000725 bool isConstantGlobal, const Type *Ty,
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000726 Constant *Initializer, bool IsThreadLocal,
727 unsigned AddressSpace = 0) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000728 if (isa<FunctionType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000729 GenerateError("Cannot declare global vars of function type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000730 return 0;
731 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000732
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000733 const PointerType *PTy = PointerType::get(Ty, AddressSpace);
Chris Lattner58af2a12006-02-15 07:22:58 +0000734
735 std::string Name;
736 if (NameStr) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000737 Name = *NameStr; // Copy string
738 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000739 }
740
741 // See if this global value was forward referenced. If so, recycle the
742 // object.
743 ValID ID;
744 if (!Name.empty()) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000745 ID = ValID::createGlobalName(Name);
Chris Lattner58af2a12006-02-15 07:22:58 +0000746 } else {
Reid Spencer93c40032007-03-19 18:40:50 +0000747 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000748 }
749
750 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
751 // Move the global to the end of the list, from whereever it was
752 // previously inserted.
753 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
754 CurModule.CurrentModule->getGlobalList().remove(GV);
755 CurModule.CurrentModule->getGlobalList().push_back(GV);
756 GV->setInitializer(Initializer);
757 GV->setLinkage(Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000758 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000759 GV->setConstant(isConstantGlobal);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000760 GV->setThreadLocal(IsThreadLocal);
Chris Lattner58af2a12006-02-15 07:22:58 +0000761 InsertValue(GV, CurModule.Values);
762 return GV;
763 }
764
Reid Spenceref9b9a72007-02-05 20:47:22 +0000765 // If this global has a name
Chris Lattner58af2a12006-02-15 07:22:58 +0000766 if (!Name.empty()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000767 // if the global we're parsing has an initializer (is a definition) and
768 // has external linkage.
769 if (Initializer && Linkage != GlobalValue::InternalLinkage)
770 // If there is already a global with external linkage with this name
771 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
772 // If we allow this GVar to get created, it will be renamed in the
773 // symbol table because it conflicts with an existing GVar. We can't
774 // allow redefinition of GVars whose linking indicates that their name
775 // must stay the same. Issue the error.
776 GenerateError("Redefinition of global variable named '" + Name +
777 "' of type '" + Ty->getDescription() + "'");
778 return 0;
779 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000780 }
781
782 // Otherwise there is no existing GV to use, create one now.
783 GlobalVariable *GV =
784 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Christopher Lamba8ed9bf2007-12-11 09:02:08 +0000785 CurModule.CurrentModule, IsThreadLocal, AddressSpace);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000786 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000787 InsertValue(GV, CurModule.Values);
788 return GV;
789}
790
791// setTypeName - Set the specified type to the name given. The name may be
792// null potentially, in which case this is a noop. The string passed in is
793// assumed to be a malloc'd string buffer, and is freed by this function.
794//
795// This function returns true if the type has already been defined, but is
796// allowed to be redefined in the specified context. If the name is a new name
797// for the type plane, it is inserted and false is returned.
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000798static bool setTypeName(const Type *T, std::string *NameStr) {
Reid Spencera9720f52007-02-05 17:04:00 +0000799 assert(!inFunctionScope() && "Can't give types function-local names!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000800 if (NameStr == 0) return false;
801
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000802 std::string Name(*NameStr); // Copy string
803 delete NameStr; // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000804
805 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000806 if (T == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000807 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000808 return false;
809 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000810
811 // Set the type name, checking for conflicts as we do so.
812 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
813
814 if (AlreadyExists) { // Inserting a name that is already defined???
815 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencera9720f52007-02-05 17:04:00 +0000816 assert(Existing && "Conflict but no matching type?!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000817
818 // There is only one case where this is allowed: when we are refining an
819 // opaque type. In this case, Existing will be an opaque type.
820 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
821 // We ARE replacing an opaque type!
822 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
823 return true;
824 }
825
826 // Otherwise, this is an attempt to redefine a type. That's okay if
827 // the redefinition is identical to the original. This will be so if
828 // Existing and T point to the same Type object. In this one case we
829 // allow the equivalent redefinition.
830 if (Existing == T) return true; // Yes, it's equal.
831
832 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer63c34452007-01-05 21:51:07 +0000833 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000834 T->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +0000835 }
836
837 return false;
838}
839
840//===----------------------------------------------------------------------===//
841// Code for handling upreferences in type names...
842//
843
844// TypeContains - Returns true if Ty directly contains E in it.
845//
846static bool TypeContains(const Type *Ty, const Type *E) {
847 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
848 E) != Ty->subtype_end();
849}
850
851namespace {
852 struct UpRefRecord {
853 // NestingLevel - The number of nesting levels that need to be popped before
854 // this type is resolved.
855 unsigned NestingLevel;
856
857 // LastContainedTy - This is the type at the current binding level for the
858 // type. Every time we reduce the nesting level, this gets updated.
859 const Type *LastContainedTy;
860
861 // UpRefTy - This is the actual opaque type that the upreference is
862 // represented with.
863 OpaqueType *UpRefTy;
864
865 UpRefRecord(unsigned NL, OpaqueType *URTy)
866 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
867 };
868}
869
870// UpRefs - A list of the outstanding upreferences that need to be resolved.
871static std::vector<UpRefRecord> UpRefs;
872
873/// HandleUpRefs - Every time we finish a new layer of types, this function is
874/// called. It loops through the UpRefs vector, which is a list of the
875/// currently active types. For each type, if the up reference is contained in
876/// the newly completed type, we decrement the level count. When the level
877/// count reaches zero, the upreferenced type is the type that is passed in:
878/// thus we can complete the cycle.
879///
880static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner224f84f2006-08-18 17:34:45 +0000881 // If Ty isn't abstract, or if there are no up-references in it, then there is
882 // nothing to resolve here.
883 if (!ty->isAbstract() || UpRefs.empty()) return ty;
884
Chris Lattner58af2a12006-02-15 07:22:58 +0000885 PATypeHolder Ty(ty);
886 UR_OUT("Type '" << Ty->getDescription() <<
887 "' newly formed. Resolving upreferences.\n" <<
888 UpRefs.size() << " upreferences active!\n");
889
890 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
891 // to zero), we resolve them all together before we resolve them to Ty. At
892 // the end of the loop, if there is anything to resolve to Ty, it will be in
893 // this variable.
894 OpaqueType *TypeToResolve = 0;
895
896 for (unsigned i = 0; i != UpRefs.size(); ++i) {
897 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
898 << UpRefs[i].second->getDescription() << ") = "
899 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
900 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
901 // Decrement level of upreference
902 unsigned Level = --UpRefs[i].NestingLevel;
903 UpRefs[i].LastContainedTy = Ty;
904 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
905 if (Level == 0) { // Upreference should be resolved!
906 if (!TypeToResolve) {
907 TypeToResolve = UpRefs[i].UpRefTy;
908 } else {
909 UR_OUT(" * Resolving upreference for "
910 << UpRefs[i].second->getDescription() << "\n";
911 std::string OldName = UpRefs[i].UpRefTy->getDescription());
912 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
913 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
914 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
915 }
916 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
917 --i; // Do not skip the next element...
918 }
919 }
920 }
921
922 if (TypeToResolve) {
923 UR_OUT(" * Resolving upreference for "
924 << UpRefs[i].second->getDescription() << "\n";
925 std::string OldName = TypeToResolve->getDescription());
926 TypeToResolve->refineAbstractTypeTo(Ty);
927 }
928
929 return Ty;
930}
931
Chris Lattner58af2a12006-02-15 07:22:58 +0000932//===----------------------------------------------------------------------===//
933// RunVMAsmParser - Define an interface to this parser
934//===----------------------------------------------------------------------===//
935//
Reid Spencer14310612006-12-31 05:40:51 +0000936static Module* RunParser(Module * M);
937
Duncan Sandsdc024672007-11-27 13:23:08 +0000938Module *llvm::RunVMAsmParser(llvm::MemoryBuffer *MB) {
939 InitLLLexer(MB);
940 Module *M = RunParser(new Module(LLLgetFilename()));
941 FreeLexer();
942 return M;
Chris Lattner58af2a12006-02-15 07:22:58 +0000943}
944
945%}
946
947%union {
948 llvm::Module *ModuleVal;
949 llvm::Function *FunctionVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000950 llvm::BasicBlock *BasicBlockVal;
951 llvm::TerminatorInst *TermInstVal;
952 llvm::Instruction *InstVal;
Reid Spencera132e042006-12-03 05:46:11 +0000953 llvm::Constant *ConstVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000954
Reid Spencera132e042006-12-03 05:46:11 +0000955 const llvm::Type *PrimType;
Reid Spencer14310612006-12-31 05:40:51 +0000956 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencera132e042006-12-03 05:46:11 +0000957 llvm::PATypeHolder *TypeVal;
958 llvm::Value *ValueVal;
Reid Spencera132e042006-12-03 05:46:11 +0000959 std::vector<llvm::Value*> *ValueList;
Reid Spencer14310612006-12-31 05:40:51 +0000960 llvm::ArgListType *ArgList;
961 llvm::TypeWithAttrs TypeWithAttrs;
962 llvm::TypeWithAttrsList *TypeWithAttrsList;
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000963 llvm::ParamList *ParamList;
Reid Spencer14310612006-12-31 05:40:51 +0000964
Chris Lattner58af2a12006-02-15 07:22:58 +0000965 // Represent the RHS of PHI node
Reid Spencera132e042006-12-03 05:46:11 +0000966 std::list<std::pair<llvm::Value*,
967 llvm::BasicBlock*> > *PHIList;
Chris Lattner58af2a12006-02-15 07:22:58 +0000968 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencera132e042006-12-03 05:46:11 +0000969 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner58af2a12006-02-15 07:22:58 +0000970
971 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000972 llvm::GlobalValue::VisibilityTypes Visibility;
Reid Spencer7b5d4662007-04-09 06:16:21 +0000973 uint16_t ParamAttrs;
Reid Spencer38c91a92007-02-28 02:24:54 +0000974 llvm::APInt *APIntVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000975 int64_t SInt64Val;
976 uint64_t UInt64Val;
977 int SIntVal;
978 unsigned UIntVal;
Dale Johannesen43421b32007-09-06 18:13:44 +0000979 llvm::APFloat *FPVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000980 bool BoolVal;
981
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000982 std::string *StrVal; // This memory must be deleted
983 llvm::ValID ValIDVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000984
Reid Spencera132e042006-12-03 05:46:11 +0000985 llvm::Instruction::BinaryOps BinaryOpVal;
986 llvm::Instruction::TermOps TermOpVal;
987 llvm::Instruction::MemoryOps MemOpVal;
988 llvm::Instruction::CastOps CastOpVal;
989 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencera132e042006-12-03 05:46:11 +0000990 llvm::ICmpInst::Predicate IPredicate;
991 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner58af2a12006-02-15 07:22:58 +0000992}
993
Reid Spencer14310612006-12-31 05:40:51 +0000994%type <ModuleVal> Module
Chris Lattner58af2a12006-02-15 07:22:58 +0000995%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
996%type <BasicBlockVal> BasicBlock InstructionList
997%type <TermInstVal> BBTerminatorInst
998%type <InstVal> Inst InstVal MemoryInst
Anton Korobeynikov38e09802007-04-28 13:48:45 +0000999%type <ConstVal> ConstVal ConstExpr AliaseeRef
Chris Lattner58af2a12006-02-15 07:22:58 +00001000%type <ConstVector> ConstVector
1001%type <ArgList> ArgList ArgListH
Chris Lattner58af2a12006-02-15 07:22:58 +00001002%type <PHIList> PHIList
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001003%type <ParamList> ParamList // For call param lists & GEP indices
Reid Spencer14310612006-12-31 05:40:51 +00001004%type <ValueList> IndexList // For GEP indices
1005%type <TypeList> TypeListI
1006%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer218ded22007-01-05 17:07:23 +00001007%type <TypeWithAttrs> ArgType
Chris Lattner58af2a12006-02-15 07:22:58 +00001008%type <JumpTable> JumpTable
1009%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001010%type <BoolVal> ThreadLocal // 'thread_local' or not
Chris Lattner58af2a12006-02-15 07:22:58 +00001011%type <BoolVal> OptVolatile // 'volatile' or not
1012%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1013%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencer14310612006-12-31 05:40:51 +00001014%type <Linkage> GVInternalLinkage GVExternalLinkage
1015%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001016%type <Linkage> AliasLinkage
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001017%type <Visibility> GVVisibilityStyle
Chris Lattner58af2a12006-02-15 07:22:58 +00001018
1019// ValueRef - Unresolved reference to a definition or BB
1020%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1021%type <ValueVal> ResolvedVal // <type> <valref> pair
1022// Tokens and types for handling constant integer values
1023//
1024// ESINT64VAL - A negative number within long long range
1025%token <SInt64Val> ESINT64VAL
1026
1027// EUINT64VAL - A positive number within uns. long long range
1028%token <UInt64Val> EUINT64VAL
Chris Lattner58af2a12006-02-15 07:22:58 +00001029
Reid Spencer38c91a92007-02-28 02:24:54 +00001030// ESAPINTVAL - A negative number with arbitrary precision
1031%token <APIntVal> ESAPINTVAL
1032
1033// EUAPINTVAL - A positive number with arbitrary precision
1034%token <APIntVal> EUAPINTVAL
1035
Reid Spencer41dff5e2007-01-26 08:05:27 +00001036%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattner58af2a12006-02-15 07:22:58 +00001037%token <FPVal> FPVAL // Float or Double constant
1038
1039// Built in types...
Reid Spencer218ded22007-01-05 17:07:23 +00001040%type <TypeVal> Types ResultTypes
Reid Spencer14310612006-12-31 05:40:51 +00001041%type <PrimType> IntType FPType PrimType // Classifications
Reid Spencer6f407902007-01-13 05:00:46 +00001042%token <PrimType> VOID INTTYPE
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001043%token <PrimType> FLOAT DOUBLE X86_FP80 FP128 PPC_FP128 LABEL
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001044%token TYPE
Chris Lattner58af2a12006-02-15 07:22:58 +00001045
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001046
Reid Spencered951ea2007-05-19 07:22:10 +00001047%token<StrVal> LOCALVAR GLOBALVAR LABELSTR
1048%token<StrVal> STRINGCONSTANT ATSTRINGCONSTANT PCTSTRINGCONSTANT
Reid Spencer41dff5e2007-01-26 08:05:27 +00001049%type <StrVal> LocalName OptLocalName OptLocalAssign
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001050%type <StrVal> GlobalName OptGlobalAssign GlobalAssign
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001051%type <StrVal> OptSection SectionString OptGC
Chris Lattner58af2a12006-02-15 07:22:58 +00001052
Christopher Lambbf3348d2007-12-12 08:45:45 +00001053%type <UIntVal> OptAlign OptCAlign OptAddrSpace
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001054
Reid Spencer3d6b71e2007-04-09 01:56:05 +00001055%token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001056%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL
Reid Spencer14310612006-12-31 05:40:51 +00001057%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001058%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001059%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN ADDRSPACE
Chris Lattner58af2a12006-02-15 07:22:58 +00001060%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001061%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattner1ae022f2006-10-22 06:08:13 +00001062%token DATALAYOUT
Chris Lattner58af2a12006-02-15 07:22:58 +00001063%type <UIntVal> OptCallingConv
Reid Spencer218ded22007-01-05 17:07:23 +00001064%type <ParamAttrs> OptParamAttrs ParamAttr
1065%type <ParamAttrs> OptFuncAttrs FuncAttr
Chris Lattner58af2a12006-02-15 07:22:58 +00001066
1067// Basic Block Terminating Operators
1068%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
1069
1070// Binary Operators
Reid Spencere4d87aa2006-12-23 06:05:41 +00001071%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencer3ed469c2006-11-02 20:25:50 +00001072%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer832254e2007-02-02 02:16:23 +00001073%token <BinaryOpVal> SHL LSHR ASHR
1074
Reid Spencera132e042006-12-03 05:46:11 +00001075%token <OtherOpVal> ICMP FCMP
Reid Spencera132e042006-12-03 05:46:11 +00001076%type <IPredicate> IPredicates
Reid Spencera132e042006-12-03 05:46:11 +00001077%type <FPredicate> FPredicates
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001078%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1079%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner58af2a12006-02-15 07:22:58 +00001080
1081// Memory Instructions
1082%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1083
Reid Spencer3da59db2006-11-27 01:05:10 +00001084// Cast Operators
1085%type <CastOpVal> CastOps
1086%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1087%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1088
Chris Lattner58af2a12006-02-15 07:22:58 +00001089// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001090%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattnerd5efe842006-04-08 01:18:56 +00001091%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Chris Lattner58af2a12006-02-15 07:22:58 +00001092
Reid Spencer218ded22007-01-05 17:07:23 +00001093// Function Attributes
Reid Spencerb8f85052007-07-31 03:50:36 +00001094%token SIGNEXT ZEROEXT NORETURN INREG SRET NOUNWIND NOALIAS BYVAL NEST
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001095%token READNONE READONLY GC
Chris Lattner58af2a12006-02-15 07:22:58 +00001096
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001097// Visibility Styles
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001098%token DEFAULT HIDDEN PROTECTED
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001099
Chris Lattner58af2a12006-02-15 07:22:58 +00001100%start Module
1101%%
1102
Chris Lattner58af2a12006-02-15 07:22:58 +00001103
Chris Lattner58af2a12006-02-15 07:22:58 +00001104// Operations that are notably excluded from this list include:
1105// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1106//
Reid Spencer3ed469c2006-11-02 20:25:50 +00001107ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer832254e2007-02-02 02:16:23 +00001108LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Reid Spencer3da59db2006-11-27 01:05:10 +00001109CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1110 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer832254e2007-02-02 02:16:23 +00001111
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001112IPredicates
Reid Spencer4012e832006-12-04 05:24:24 +00001113 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001114 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1115 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1116 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1117 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1118 ;
1119
1120FPredicates
1121 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1122 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1123 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1124 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1125 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1126 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1127 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1128 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1129 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1130 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001131
1132// These are some types that allow classification if we only want a particular
1133// thing... for example, only a signed, unsigned, or integral type.
Reid Spencera54b7cb2007-01-12 07:05:14 +00001134IntType : INTTYPE;
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001135FPType : FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80;
Chris Lattner58af2a12006-02-15 07:22:58 +00001136
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001137LocalName : LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001138OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1139
Christopher Lambbf3348d2007-12-12 08:45:45 +00001140OptAddrSpace : ADDRSPACE '(' EUINT64VAL ')' { $$=$3; }
1141 | /*empty*/ { $$=0; };
1142
Reid Spencer41dff5e2007-01-26 08:05:27 +00001143/// OptLocalAssign - Value producing statements have an optional assignment
1144/// component.
1145OptLocalAssign : LocalName '=' {
1146 $$ = $1;
1147 CHECK_FOR_ERROR
1148 }
1149 | /*empty*/ {
1150 $$ = 0;
1151 CHECK_FOR_ERROR
1152 };
1153
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001154GlobalName : GLOBALVAR | ATSTRINGCONSTANT ;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001155
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001156OptGlobalAssign : GlobalAssign
Chris Lattner58af2a12006-02-15 07:22:58 +00001157 | /*empty*/ {
1158 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001159 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001160 };
1161
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001162GlobalAssign : GlobalName '=' {
1163 $$ = $1;
1164 CHECK_FOR_ERROR
Chris Lattner6cdc6822007-04-26 05:31:05 +00001165 };
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001166
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001167GVInternalLinkage
1168 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1169 | WEAK { $$ = GlobalValue::WeakLinkage; }
1170 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1171 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1172 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1173 ;
1174
1175GVExternalLinkage
1176 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1177 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1178 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1179 ;
1180
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001181GVVisibilityStyle
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +00001182 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1183 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1184 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1185 | PROTECTED { $$ = GlobalValue::ProtectedVisibility; }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001186 ;
1187
Reid Spencer14310612006-12-31 05:40:51 +00001188FunctionDeclareLinkage
1189 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1190 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1191 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001192 ;
1193
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001194FunctionDefineLinkage
Reid Spencer14310612006-12-31 05:40:51 +00001195 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1196 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001197 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1198 | WEAK { $$ = GlobalValue::WeakLinkage; }
1199 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001200 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001201
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00001202AliasLinkage
1203 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1204 | WEAK { $$ = GlobalValue::WeakLinkage; }
1205 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
1206 ;
1207
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001208OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1209 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001210 FASTCC_TOK { $$ = CallingConv::Fast; } |
1211 COLDCC_TOK { $$ = CallingConv::Cold; } |
1212 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1213 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1214 CC_TOK EUINT64VAL {
Chris Lattner58af2a12006-02-15 07:22:58 +00001215 if ((unsigned)$2 != $2)
Reid Spencerb5334b02007-02-05 10:18:06 +00001216 GEN_ERROR("Calling conv too large");
Chris Lattner58af2a12006-02-15 07:22:58 +00001217 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001218 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001219 };
1220
Reid Spencerb8f85052007-07-31 03:50:36 +00001221ParamAttr : ZEROEXT { $$ = ParamAttr::ZExt; }
1222 | ZEXT { $$ = ParamAttr::ZExt; }
1223 | SIGNEXT { $$ = ParamAttr::SExt; }
Chris Lattnerce5f24e2007-07-05 17:26:49 +00001224 | SEXT { $$ = ParamAttr::SExt; }
1225 | INREG { $$ = ParamAttr::InReg; }
1226 | SRET { $$ = ParamAttr::StructRet; }
1227 | NOALIAS { $$ = ParamAttr::NoAlias; }
Reid Spencerb8f85052007-07-31 03:50:36 +00001228 | BYVAL { $$ = ParamAttr::ByVal; }
1229 | NEST { $$ = ParamAttr::Nest; }
Reid Spencer14310612006-12-31 05:40:51 +00001230 ;
1231
Reid Spencer18da0722007-04-11 02:44:20 +00001232OptParamAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer218ded22007-01-05 17:07:23 +00001233 | OptParamAttrs ParamAttr {
Reid Spencer7b5d4662007-04-09 06:16:21 +00001234 $$ = $1 | $2;
Reid Spencer14310612006-12-31 05:40:51 +00001235 }
1236 ;
1237
Reid Spencer18da0722007-04-11 02:44:20 +00001238FuncAttr : NORETURN { $$ = ParamAttr::NoReturn; }
1239 | NOUNWIND { $$ = ParamAttr::NoUnwind; }
Reid Spencerb8f85052007-07-31 03:50:36 +00001240 | ZEROEXT { $$ = ParamAttr::ZExt; }
1241 | SIGNEXT { $$ = ParamAttr::SExt; }
Duncan Sandsdc024672007-11-27 13:23:08 +00001242 | READNONE { $$ = ParamAttr::ReadNone; }
1243 | READONLY { $$ = ParamAttr::ReadOnly; }
Reid Spencer218ded22007-01-05 17:07:23 +00001244 ;
1245
Reid Spencer18da0722007-04-11 02:44:20 +00001246OptFuncAttrs : /* empty */ { $$ = ParamAttr::None; }
Reid Spencer218ded22007-01-05 17:07:23 +00001247 | OptFuncAttrs FuncAttr {
Reid Spencer7b5d4662007-04-09 06:16:21 +00001248 $$ = $1 | $2;
Reid Spencer218ded22007-01-05 17:07:23 +00001249 }
Reid Spencer14310612006-12-31 05:40:51 +00001250 ;
1251
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00001252OptGC : /* empty */ { $$ = 0; }
1253 | GC STRINGCONSTANT {
1254 $$ = $2;
1255 }
1256 ;
1257
Chris Lattner58af2a12006-02-15 07:22:58 +00001258// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1259// a comma before it.
1260OptAlign : /*empty*/ { $$ = 0; } |
1261 ALIGN EUINT64VAL {
1262 $$ = $2;
1263 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001264 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001265 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001266};
1267OptCAlign : /*empty*/ { $$ = 0; } |
1268 ',' ALIGN EUINT64VAL {
1269 $$ = $3;
1270 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001271 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001272 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001273};
1274
1275
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001276
Chris Lattner58af2a12006-02-15 07:22:58 +00001277SectionString : SECTION STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001278 for (unsigned i = 0, e = $2->length(); i != e; ++i)
1279 if ((*$2)[i] == '"' || (*$2)[i] == '\\')
Reid Spencerb5334b02007-02-05 10:18:06 +00001280 GEN_ERROR("Invalid character in section name");
Chris Lattner58af2a12006-02-15 07:22:58 +00001281 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001282 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001283};
1284
1285OptSection : /*empty*/ { $$ = 0; } |
1286 SectionString { $$ = $1; };
1287
1288// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1289// is set to be the global we are processing.
1290//
1291GlobalVarAttributes : /* empty */ {} |
1292 ',' GlobalVarAttribute GlobalVarAttributes {};
1293GlobalVarAttribute : SectionString {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001294 CurGV->setSection(*$1);
1295 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001296 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001297 }
1298 | ALIGN EUINT64VAL {
1299 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001300 GEN_ERROR("Alignment must be a power of two");
Chris Lattner58af2a12006-02-15 07:22:58 +00001301 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001302 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001303 };
1304
1305//===----------------------------------------------------------------------===//
1306// Types includes all predefined types... except void, because it can only be
Reid Spencer14310612006-12-31 05:40:51 +00001307// used in specific contexts (function returning void for example).
Chris Lattner58af2a12006-02-15 07:22:58 +00001308
1309// Derived types are added later...
1310//
Dale Johannesen320fc8a2007-08-03 01:03:46 +00001311PrimType : INTTYPE | FLOAT | DOUBLE | PPC_FP128 | FP128 | X86_FP80 | LABEL ;
Reid Spencer14310612006-12-31 05:40:51 +00001312
1313Types
1314 : OPAQUE {
Reid Spencera132e042006-12-03 05:46:11 +00001315 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001316 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001317 }
1318 | PrimType {
Reid Spencera132e042006-12-03 05:46:11 +00001319 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001320 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00001321 }
Christopher Lambbf3348d2007-12-12 08:45:45 +00001322 | Types OptAddrSpace '*' { // Pointer type?
Reid Spencer14310612006-12-31 05:40:51 +00001323 if (*$1 == Type::LabelTy)
1324 GEN_ERROR("Cannot form a pointer to a basic block");
Christopher Lambbf3348d2007-12-12 08:45:45 +00001325 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1, $2)));
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00001326 delete $1;
1327 CHECK_FOR_ERROR
1328 }
Reid Spencer14310612006-12-31 05:40:51 +00001329 | SymbolicValueRef { // Named types are also simple types...
1330 const Type* tmp = getTypeVal($1);
1331 CHECK_FOR_ERROR
1332 $$ = new PATypeHolder(tmp);
1333 }
1334 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerb5334b02007-02-05 10:18:06 +00001335 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattner58af2a12006-02-15 07:22:58 +00001336 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1337 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencera132e042006-12-03 05:46:11 +00001338 $$ = new PATypeHolder(OT);
Chris Lattner58af2a12006-02-15 07:22:58 +00001339 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001340 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001341 }
Reid Spencer218ded22007-01-05 17:07:23 +00001342 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001343 // Allow but ignore attributes on function types; this permits auto-upgrade.
1344 // FIXME: remove in LLVM 3.0.
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001345 const Type* RetTy = *$1;
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001346 if (!(RetTy->isFirstClassType() || RetTy == Type::VoidTy ||
1347 isa<OpaqueType>(RetTy)))
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001348 GEN_ERROR("LLVM Functions cannot return aggregates");
1349
Chris Lattner58af2a12006-02-15 07:22:58 +00001350 std::vector<const Type*> Params;
Reid Spencer7b5d4662007-04-09 06:16:21 +00001351 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001352 for (; I != E; ++I ) {
Reid Spencer66728ef2007-03-20 01:13:36 +00001353 const Type *Ty = I->Ty->get();
Reid Spencer66728ef2007-03-20 01:13:36 +00001354 Params.push_back(Ty);
Reid Spencer14310612006-12-31 05:40:51 +00001355 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001356
Chris Lattner58af2a12006-02-15 07:22:58 +00001357 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1358 if (isVarArg) Params.pop_back();
1359
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001360 for (unsigned i = 0; i != Params.size(); ++i)
1361 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1362 GEN_ERROR("Function arguments must be value types!");
1363
1364 CHECK_FOR_ERROR
1365
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001366 FunctionType *FT = FunctionType::get(RetTy, Params, isVarArg);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001367 delete $3; // Delete the argument list
Reid Spencer14310612006-12-31 05:40:51 +00001368 delete $1; // Delete the return type handle
1369 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer61c83e02006-08-18 08:43:06 +00001370 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001371 }
Reid Spencer218ded22007-01-05 17:07:23 +00001372 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Duncan Sandsdc024672007-11-27 13:23:08 +00001373 // Allow but ignore attributes on function types; this permits auto-upgrade.
1374 // FIXME: remove in LLVM 3.0.
Reid Spencer14310612006-12-31 05:40:51 +00001375 std::vector<const Type*> Params;
Reid Spencer7b5d4662007-04-09 06:16:21 +00001376 TypeWithAttrsList::iterator I = $3->begin(), E = $3->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00001377 for ( ; I != E; ++I ) {
Reid Spencer66728ef2007-03-20 01:13:36 +00001378 const Type* Ty = I->Ty->get();
Reid Spencer66728ef2007-03-20 01:13:36 +00001379 Params.push_back(Ty);
Reid Spencer14310612006-12-31 05:40:51 +00001380 }
Anton Korobeynikovc1d848d2007-12-03 19:16:54 +00001381
Reid Spencer14310612006-12-31 05:40:51 +00001382 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1383 if (isVarArg) Params.pop_back();
1384
Anton Korobeynikov05e5a742007-12-03 21:01:29 +00001385 for (unsigned i = 0; i != Params.size(); ++i)
1386 if (!(Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])))
1387 GEN_ERROR("Function arguments must be value types!");
1388
1389 CHECK_FOR_ERROR
1390
Duncan Sandsdc024672007-11-27 13:23:08 +00001391 FunctionType *FT = FunctionType::get($1, Params, isVarArg);
Reid Spencer218ded22007-01-05 17:07:23 +00001392 delete $3; // Delete the argument list
Reid Spencer14310612006-12-31 05:40:51 +00001393 $$ = new PATypeHolder(HandleUpRefs(FT));
1394 CHECK_FOR_ERROR
1395 }
1396
1397 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Reid Spencera132e042006-12-03 05:46:11 +00001398 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1399 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001400 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001401 }
Chris Lattner32980692007-02-19 07:44:24 +00001402 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencera132e042006-12-03 05:46:11 +00001403 const llvm::Type* ElemTy = $4->get();
1404 if ((unsigned)$2 != $2)
1405 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner42a75512007-01-15 02:27:26 +00001406 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencer9d6565a2007-02-15 02:26:10 +00001407 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001408 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencera132e042006-12-03 05:46:11 +00001409 delete $4;
1410 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001411 }
1412 | '{' TypeListI '}' { // Structure type?
1413 std::vector<const Type*> Elements;
Reid Spencera132e042006-12-03 05:46:11 +00001414 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattner58af2a12006-02-15 07:22:58 +00001415 E = $2->end(); I != E; ++I)
Reid Spencera132e042006-12-03 05:46:11 +00001416 Elements.push_back(*I);
Chris Lattner58af2a12006-02-15 07:22:58 +00001417
Reid Spencera132e042006-12-03 05:46:11 +00001418 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattner58af2a12006-02-15 07:22:58 +00001419 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001420 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001421 }
1422 | '{' '}' { // Empty structure type?
Reid Spencera132e042006-12-03 05:46:11 +00001423 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001424 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001425 }
Andrew Lenharth6353e052006-12-08 18:07:09 +00001426 | '<' '{' TypeListI '}' '>' {
1427 std::vector<const Type*> Elements;
1428 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1429 E = $3->end(); I != E; ++I)
1430 Elements.push_back(*I);
1431
1432 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1433 delete $3;
1434 CHECK_FOR_ERROR
1435 }
1436 | '<' '{' '}' '>' { // Empty structure type?
1437 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1438 CHECK_FOR_ERROR
1439 }
Reid Spencer14310612006-12-31 05:40:51 +00001440 ;
1441
1442ArgType
Duncan Sandsdc024672007-11-27 13:23:08 +00001443 : Types OptParamAttrs {
1444 // Allow but ignore attributes on function types; this permits auto-upgrade.
1445 // FIXME: remove in LLVM 3.0.
Reid Spencer14310612006-12-31 05:40:51 +00001446 $$.Ty = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00001447 $$.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00001448 }
1449 ;
1450
Reid Spencer218ded22007-01-05 17:07:23 +00001451ResultTypes
1452 : Types {
Reid Spencer14310612006-12-31 05:40:51 +00001453 if (!UpRefs.empty())
1454 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1455 if (!(*$1)->isFirstClassType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001456 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer218ded22007-01-05 17:07:23 +00001457 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00001458 }
Reid Spencer218ded22007-01-05 17:07:23 +00001459 | VOID {
1460 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencer14310612006-12-31 05:40:51 +00001461 }
1462 ;
1463
1464ArgTypeList : ArgType {
1465 $$ = new TypeWithAttrsList();
1466 $$->push_back($1);
1467 CHECK_FOR_ERROR
1468 }
1469 | ArgTypeList ',' ArgType {
1470 ($$=$1)->push_back($3);
1471 CHECK_FOR_ERROR
1472 }
1473 ;
1474
1475ArgTypeListI
1476 : ArgTypeList
1477 | ArgTypeList ',' DOTDOTDOT {
1478 $$=$1;
Reid Spencer18da0722007-04-11 02:44:20 +00001479 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00001480 TWA.Ty = new PATypeHolder(Type::VoidTy);
1481 $$->push_back(TWA);
1482 CHECK_FOR_ERROR
1483 }
1484 | DOTDOTDOT {
1485 $$ = new TypeWithAttrsList;
Reid Spencer18da0722007-04-11 02:44:20 +00001486 TypeWithAttrs TWA; TWA.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00001487 TWA.Ty = new PATypeHolder(Type::VoidTy);
1488 $$->push_back(TWA);
1489 CHECK_FOR_ERROR
1490 }
1491 | /*empty*/ {
1492 $$ = new TypeWithAttrsList();
Reid Spencer61c83e02006-08-18 08:43:06 +00001493 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001494 };
1495
1496// TypeList - Used for struct declarations and as a basis for function type
1497// declaration type lists
1498//
Reid Spencer14310612006-12-31 05:40:51 +00001499TypeListI : Types {
Reid Spencera132e042006-12-03 05:46:11 +00001500 $$ = new std::list<PATypeHolder>();
Reid Spencer66728ef2007-03-20 01:13:36 +00001501 $$->push_back(*$1);
1502 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001503 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001504 }
Reid Spencer14310612006-12-31 05:40:51 +00001505 | TypeListI ',' Types {
Reid Spencer66728ef2007-03-20 01:13:36 +00001506 ($$=$1)->push_back(*$3);
1507 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001508 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001509 };
1510
Chris Lattner58af2a12006-02-15 07:22:58 +00001511// ConstVal - The various declarations that go into the constant pool. This
1512// production is used ONLY to represent constants that show up AFTER a 'const',
1513// 'constant' or 'global' token at global scope. Constants that can be inlined
1514// into other expressions (such as integers and constexprs) are handled by the
1515// ResolvedVal, ValueRef and ConstValueRef productions.
1516//
1517ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001518 if (!UpRefs.empty())
1519 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001520 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001521 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001522 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001523 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001524 const Type *ETy = ATy->getElementType();
1525 int NumElements = ATy->getNumElements();
1526
1527 // Verify that we have the correct size...
1528 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001529 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001530 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerb5334b02007-02-05 10:18:06 +00001531 itostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001532
1533 // Verify all elements are correct type!
1534 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001535 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001536 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001537 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001538 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001539 }
1540
Reid Spencera132e042006-12-03 05:46:11 +00001541 $$ = ConstantArray::get(ATy, *$3);
1542 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001543 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001544 }
1545 | Types '[' ']' {
Reid Spencer14310612006-12-31 05:40:51 +00001546 if (!UpRefs.empty())
1547 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001548 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001549 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001550 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001551 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001552
1553 int NumElements = ATy->getNumElements();
1554 if (NumElements != -1 && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001555 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencerb5334b02007-02-05 10:18:06 +00001556 " arguments, but has size of " + itostr(NumElements) +"");
Reid Spencera132e042006-12-03 05:46:11 +00001557 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1558 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001559 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001560 }
1561 | Types 'c' STRINGCONSTANT {
Reid Spencer14310612006-12-31 05:40:51 +00001562 if (!UpRefs.empty())
1563 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001564 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001565 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001566 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001567 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001568
1569 int NumElements = ATy->getNumElements();
1570 const Type *ETy = ATy->getElementType();
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001571 if (NumElements != -1 && NumElements != int($3->length()))
Reid Spencer61c83e02006-08-18 08:43:06 +00001572 GEN_ERROR("Can't build string constant of size " +
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001573 itostr((int)($3->length())) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001574 " when array has size " + itostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001575 std::vector<Constant*> Vals;
Reid Spencer14310612006-12-31 05:40:51 +00001576 if (ETy == Type::Int8Ty) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001577 for (unsigned i = 0; i < $3->length(); ++i)
1578 Vals.push_back(ConstantInt::get(ETy, (*$3)[i]));
Chris Lattner58af2a12006-02-15 07:22:58 +00001579 } else {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001580 delete $3;
Reid Spencerb5334b02007-02-05 10:18:06 +00001581 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner58af2a12006-02-15 07:22:58 +00001582 }
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001583 delete $3;
Reid Spencera132e042006-12-03 05:46:11 +00001584 $$ = ConstantArray::get(ATy, Vals);
1585 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001586 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001587 }
1588 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001589 if (!UpRefs.empty())
1590 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00001591 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001592 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001593 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001594 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001595 const Type *ETy = PTy->getElementType();
1596 int NumElements = PTy->getNumElements();
1597
1598 // Verify that we have the correct size...
1599 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001600 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001601 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerb5334b02007-02-05 10:18:06 +00001602 itostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001603
1604 // Verify all elements are correct type!
1605 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001606 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001607 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001608 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001609 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001610 }
1611
Reid Spencer9d6565a2007-02-15 02:26:10 +00001612 $$ = ConstantVector::get(PTy, *$3);
Reid Spencera132e042006-12-03 05:46:11 +00001613 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001614 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001615 }
1616 | Types '{' ConstVector '}' {
Reid Spencera132e042006-12-03 05:46:11 +00001617 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001618 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001619 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001620 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001621
1622 if ($3->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001623 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001624
1625 // Check to ensure that constants are compatible with the type initializer!
1626 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencera132e042006-12-03 05:46:11 +00001627 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001628 GEN_ERROR("Expected type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001629 STy->getElementType(i)->getDescription() +
1630 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001631 " of structure initializer");
Chris Lattner58af2a12006-02-15 07:22:58 +00001632
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001633 // Check to ensure that Type is not packed
1634 if (STy->isPacked())
Chris Lattner6cdc6822007-04-26 05:31:05 +00001635 GEN_ERROR("Unpacked Initializer to vector type '" +
1636 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001637
Reid Spencera132e042006-12-03 05:46:11 +00001638 $$ = ConstantStruct::get(STy, *$3);
1639 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001640 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001641 }
1642 | Types '{' '}' {
Reid Spencer14310612006-12-31 05:40:51 +00001643 if (!UpRefs.empty())
1644 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001645 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001646 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001647 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001648 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001649
1650 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001651 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001652
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001653 // Check to ensure that Type is not packed
1654 if (STy->isPacked())
Chris Lattner6cdc6822007-04-26 05:31:05 +00001655 GEN_ERROR("Unpacked Initializer to vector type '" +
1656 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001657
1658 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1659 delete $1;
1660 CHECK_FOR_ERROR
1661 }
1662 | Types '<' '{' ConstVector '}' '>' {
1663 const StructType *STy = dyn_cast<StructType>($1->get());
1664 if (STy == 0)
1665 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001666 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001667
1668 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001669 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001670
1671 // Check to ensure that constants are compatible with the type initializer!
1672 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1673 if ((*$4)[i]->getType() != STy->getElementType(i))
1674 GEN_ERROR("Expected type '" +
1675 STy->getElementType(i)->getDescription() +
1676 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001677 " of structure initializer");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001678
1679 // Check to ensure that Type is packed
1680 if (!STy->isPacked())
Chris Lattner32980692007-02-19 07:44:24 +00001681 GEN_ERROR("Vector initializer to non-vector type '" +
1682 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001683
1684 $$ = ConstantStruct::get(STy, *$4);
1685 delete $1; delete $4;
1686 CHECK_FOR_ERROR
1687 }
1688 | Types '<' '{' '}' '>' {
1689 if (!UpRefs.empty())
1690 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1691 const StructType *STy = dyn_cast<StructType>($1->get());
1692 if (STy == 0)
1693 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001694 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001695
1696 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001697 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001698
1699 // Check to ensure that Type is packed
1700 if (!STy->isPacked())
Chris Lattner32980692007-02-19 07:44:24 +00001701 GEN_ERROR("Vector initializer to non-vector type '" +
1702 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001703
Reid Spencera132e042006-12-03 05:46:11 +00001704 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1705 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001706 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001707 }
1708 | Types NULL_TOK {
Reid Spencer14310612006-12-31 05:40:51 +00001709 if (!UpRefs.empty())
1710 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001711 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001712 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001713 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001714 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001715
Reid Spencera132e042006-12-03 05:46:11 +00001716 $$ = ConstantPointerNull::get(PTy);
1717 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001718 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001719 }
1720 | Types UNDEF {
Reid Spencer14310612006-12-31 05:40:51 +00001721 if (!UpRefs.empty())
1722 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001723 $$ = UndefValue::get($1->get());
1724 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001725 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001726 }
1727 | Types SymbolicValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00001728 if (!UpRefs.empty())
1729 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001730 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001731 if (Ty == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001732 GEN_ERROR("Global const reference must be a pointer type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001733
1734 // ConstExprs can exist in the body of a function, thus creating
1735 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencer93c40032007-03-19 18:40:50 +00001736 // the context of a function, getExistingVal will search the functions
Chris Lattner58af2a12006-02-15 07:22:58 +00001737 // symbol table instead of the module symbol table for the global symbol,
1738 // which throws things all off. To get around this, we just tell
Reid Spencer93c40032007-03-19 18:40:50 +00001739 // getExistingVal that we are at global scope here.
Chris Lattner58af2a12006-02-15 07:22:58 +00001740 //
1741 Function *SavedCurFn = CurFun.CurrentFunction;
1742 CurFun.CurrentFunction = 0;
1743
Reid Spencer93c40032007-03-19 18:40:50 +00001744 Value *V = getExistingVal(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001745 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001746
1747 CurFun.CurrentFunction = SavedCurFn;
1748
1749 // If this is an initializer for a constant pointer, which is referencing a
1750 // (currently) undefined variable, create a stub now that shall be replaced
1751 // in the future with the right type of variable.
1752 //
1753 if (V == 0) {
Reid Spencera9720f52007-02-05 17:04:00 +00001754 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001755 const PointerType *PT = cast<PointerType>(Ty);
1756
1757 // First check to see if the forward references value is already created!
1758 PerModuleInfo::GlobalRefsType::iterator I =
1759 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1760
1761 if (I != CurModule.GlobalRefs.end()) {
1762 V = I->second; // Placeholder already exists, use it...
1763 $2.destroy();
1764 } else {
1765 std::string Name;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001766 if ($2.Type == ValID::GlobalName)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00001767 Name = $2.getName();
Reid Spencer41dff5e2007-01-26 08:05:27 +00001768 else if ($2.Type != ValID::GlobalID)
1769 GEN_ERROR("Invalid reference to global");
Chris Lattner58af2a12006-02-15 07:22:58 +00001770
1771 // Create the forward referenced global.
1772 GlobalValue *GV;
1773 if (const FunctionType *FTy =
1774 dyn_cast<FunctionType>(PT->getElementType())) {
Chris Lattner6cdc6822007-04-26 05:31:05 +00001775 GV = new Function(FTy, GlobalValue::ExternalWeakLinkage, Name,
Chris Lattner58af2a12006-02-15 07:22:58 +00001776 CurModule.CurrentModule);
1777 } else {
1778 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattner6cdc6822007-04-26 05:31:05 +00001779 GlobalValue::ExternalWeakLinkage, 0,
Chris Lattner58af2a12006-02-15 07:22:58 +00001780 Name, CurModule.CurrentModule);
1781 }
1782
1783 // Keep track of the fact that we have a forward ref to recycle it
1784 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1785 V = GV;
1786 }
1787 }
1788
Reid Spencera132e042006-12-03 05:46:11 +00001789 $$ = cast<GlobalValue>(V);
1790 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001791 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001792 }
1793 | Types ConstExpr {
Reid Spencer14310612006-12-31 05:40:51 +00001794 if (!UpRefs.empty())
1795 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001796 if ($1->get() != $2->getType())
Reid Spencere68853b2007-01-04 00:06:14 +00001797 GEN_ERROR("Mismatched types for constant expression: " +
1798 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001799 $$ = $2;
Reid Spencera132e042006-12-03 05:46:11 +00001800 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001801 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001802 }
1803 | Types ZEROINITIALIZER {
Reid Spencer14310612006-12-31 05:40:51 +00001804 if (!UpRefs.empty())
1805 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001806 const Type *Ty = $1->get();
Chris Lattner58af2a12006-02-15 07:22:58 +00001807 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerb5334b02007-02-05 10:18:06 +00001808 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencera132e042006-12-03 05:46:11 +00001809 $$ = Constant::getNullValue(Ty);
1810 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001811 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00001812 }
Reid Spencer14310612006-12-31 05:40:51 +00001813 | IntType ESINT64VAL { // integral constants
Reid Spencere4d87aa2006-12-23 06:05:41 +00001814 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001815 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencer49d273e2007-03-19 20:40:51 +00001816 $$ = ConstantInt::get($1, $2, true);
Reid Spencer38c91a92007-02-28 02:24:54 +00001817 CHECK_FOR_ERROR
1818 }
1819 | IntType ESAPINTVAL { // arbitrary precision integer constants
1820 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1821 if ($2->getBitWidth() > BitWidth) {
1822 GEN_ERROR("Constant value does not fit in type");
Reid Spencer10794272007-03-01 19:41:47 +00001823 }
1824 $2->sextOrTrunc(BitWidth);
1825 $$ = ConstantInt::get(*$2);
Reid Spencer38c91a92007-02-28 02:24:54 +00001826 delete $2;
Reid Spencere4d87aa2006-12-23 06:05:41 +00001827 CHECK_FOR_ERROR
1828 }
Reid Spencer14310612006-12-31 05:40:51 +00001829 | IntType EUINT64VAL { // integral constants
Reid Spencere4d87aa2006-12-23 06:05:41 +00001830 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001831 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencer49d273e2007-03-19 20:40:51 +00001832 $$ = ConstantInt::get($1, $2, false);
Reid Spencer38c91a92007-02-28 02:24:54 +00001833 CHECK_FOR_ERROR
1834 }
1835 | IntType EUAPINTVAL { // arbitrary precision integer constants
1836 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1837 if ($2->getBitWidth() > BitWidth) {
1838 GEN_ERROR("Constant value does not fit in type");
Reid Spencer10794272007-03-01 19:41:47 +00001839 }
1840 $2->zextOrTrunc(BitWidth);
1841 $$ = ConstantInt::get(*$2);
Reid Spencer38c91a92007-02-28 02:24:54 +00001842 delete $2;
Reid Spencere4d87aa2006-12-23 06:05:41 +00001843 CHECK_FOR_ERROR
1844 }
Reid Spencer6f407902007-01-13 05:00:46 +00001845 | INTTYPE TRUETOK { // Boolean constants
1846 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001847 $$ = ConstantInt::getTrue();
Reid Spencer61c83e02006-08-18 08:43:06 +00001848 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001849 }
Reid Spencer6f407902007-01-13 05:00:46 +00001850 | INTTYPE FALSETOK { // Boolean constants
1851 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001852 $$ = ConstantInt::getFalse();
Reid Spencer61c83e02006-08-18 08:43:06 +00001853 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001854 }
Dale Johannesenea583102007-09-12 03:31:28 +00001855 | FPType FPVAL { // Floating point constants
Dale Johannesen43421b32007-09-06 18:13:44 +00001856 if (!ConstantFP::isValueValidForType($1, *$2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001857 GEN_ERROR("Floating point constant invalid for type");
Dale Johannesenc72cd7e2007-09-11 18:33:39 +00001858 // Lexer has no type info, so builds all float and double FP constants
1859 // as double. Fix this here. Long double is done right.
1860 if (&$2->getSemantics()==&APFloat::IEEEdouble && $1==Type::FloatTy)
Dale Johannesen43421b32007-09-06 18:13:44 +00001861 $2->convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven);
1862 $$ = ConstantFP::get($1, *$2);
Dale Johannesencdd509a2007-09-07 21:07:57 +00001863 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001864 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001865 };
1866
1867
Reid Spencer3da59db2006-11-27 01:05:10 +00001868ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer14310612006-12-31 05:40:51 +00001869 if (!UpRefs.empty())
1870 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001871 Constant *Val = $3;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001872 const Type *DestTy = $5->get();
1873 if (!CastInst::castIsValid($1, $3, DestTy))
1874 GEN_ERROR("invalid cast opcode for cast from '" +
1875 Val->getType()->getDescription() + "' to '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001876 DestTy->getDescription() + "'");
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001877 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00001878 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00001879 }
1880 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001881 if (!isa<PointerType>($3->getType()))
Reid Spencerb5334b02007-02-05 10:18:06 +00001882 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00001883
Reid Spencera132e042006-12-03 05:46:11 +00001884 const Type *IdxTy =
David Greene5fd22a82007-09-04 18:46:50 +00001885 GetElementPtrInst::getIndexedType($3->getType(), $4->begin(), $4->end(),
Chris Lattner7d9801d2007-02-13 00:58:01 +00001886 true);
Reid Spencera132e042006-12-03 05:46:11 +00001887 if (!IdxTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00001888 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencera132e042006-12-03 05:46:11 +00001889
Chris Lattnerf7469af2007-01-31 04:44:08 +00001890 SmallVector<Constant*, 8> IdxVec;
Reid Spencera132e042006-12-03 05:46:11 +00001891 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1892 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattner58af2a12006-02-15 07:22:58 +00001893 IdxVec.push_back(C);
1894 else
Reid Spencerb5334b02007-02-05 10:18:06 +00001895 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattner58af2a12006-02-15 07:22:58 +00001896
1897 delete $4;
1898
Chris Lattnerf7469af2007-01-31 04:44:08 +00001899 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer61c83e02006-08-18 08:43:06 +00001900 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001901 }
1902 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer4fe16d62007-01-11 18:21:29 +00001903 if ($3->getType() != Type::Int1Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00001904 GEN_ERROR("Select condition must be of boolean type");
Reid Spencera132e042006-12-03 05:46:11 +00001905 if ($5->getType() != $7->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001906 GEN_ERROR("Select operand types must match");
Reid Spencera132e042006-12-03 05:46:11 +00001907 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001908 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001909 }
1910 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001911 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001912 GEN_ERROR("Binary operator types must match");
Reid Spencer1628cec2006-10-26 06:15:43 +00001913 CHECK_FOR_ERROR;
Reid Spencer9eef56f2006-12-05 19:16:11 +00001914 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner58af2a12006-02-15 07:22:58 +00001915 }
1916 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001917 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001918 GEN_ERROR("Logical operator types must match");
Chris Lattner42a75512007-01-15 02:27:26 +00001919 if (!$3->getType()->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001920 if (Instruction::isShift($1) || !isa<VectorType>($3->getType()) ||
1921 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00001922 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00001923 }
Reid Spencera132e042006-12-03 05:46:11 +00001924 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001925 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001926 }
Reid Spencer4012e832006-12-04 05:24:24 +00001927 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1928 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001929 GEN_ERROR("icmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00001930 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00001931 }
Reid Spencer4012e832006-12-04 05:24:24 +00001932 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1933 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001934 GEN_ERROR("fcmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00001935 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00001936 }
Chris Lattner58af2a12006-02-15 07:22:58 +00001937 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001938 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerb5334b02007-02-05 10:18:06 +00001939 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00001940 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001941 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00001942 }
1943 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001944 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00001945 GEN_ERROR("Invalid insertelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00001946 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001947 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00001948 }
1949 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001950 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00001951 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00001952 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001953 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001954 };
1955
Chris Lattnerd25db202006-04-08 03:55:17 +00001956
Chris Lattner58af2a12006-02-15 07:22:58 +00001957// ConstVector - A list of comma separated constants.
1958ConstVector : ConstVector ',' ConstVal {
1959 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001960 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001961 }
1962 | ConstVal {
Reid Spencera132e042006-12-03 05:46:11 +00001963 $$ = new std::vector<Constant*>();
Chris Lattner58af2a12006-02-15 07:22:58 +00001964 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001965 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001966 };
1967
1968
1969// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1970GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
1971
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001972// ThreadLocal
1973ThreadLocal : THREAD_LOCAL { $$ = true; } | { $$ = false; };
1974
Anton Korobeynikov38e09802007-04-28 13:48:45 +00001975// AliaseeRef - Match either GlobalValue or bitcast to GlobalValue.
1976AliaseeRef : ResultTypes SymbolicValueRef {
1977 const Type* VTy = $1->get();
1978 Value *V = getVal(VTy, $2);
Chris Lattner0275cff2007-08-06 21:00:46 +00001979 CHECK_FOR_ERROR
Anton Korobeynikov38e09802007-04-28 13:48:45 +00001980 GlobalValue* Aliasee = dyn_cast<GlobalValue>(V);
1981 if (!Aliasee)
1982 GEN_ERROR("Aliases can be created only to global values");
1983
1984 $$ = Aliasee;
1985 CHECK_FOR_ERROR
1986 delete $1;
1987 }
1988 | BITCAST '(' AliaseeRef TO Types ')' {
1989 Constant *Val = $3;
1990 const Type *DestTy = $5->get();
1991 if (!CastInst::castIsValid($1, $3, DestTy))
1992 GEN_ERROR("invalid cast opcode for cast from '" +
1993 Val->getType()->getDescription() + "' to '" +
1994 DestTy->getDescription() + "'");
1995
1996 $$ = ConstantExpr::getCast($1, $3, DestTy);
1997 CHECK_FOR_ERROR
1998 delete $5;
1999 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002000
2001//===----------------------------------------------------------------------===//
2002// Rules to match Modules
2003//===----------------------------------------------------------------------===//
2004
2005// Module rule: Capture the result of parsing the whole file into a result
2006// variable...
2007//
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002008Module
2009 : DefinitionList {
2010 $$ = ParserResult = CurModule.CurrentModule;
2011 CurModule.ModuleDone();
2012 CHECK_FOR_ERROR;
2013 }
2014 | /*empty*/ {
2015 $$ = ParserResult = CurModule.CurrentModule;
2016 CurModule.ModuleDone();
2017 CHECK_FOR_ERROR;
2018 }
2019 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002020
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002021DefinitionList
2022 : Definition
2023 | DefinitionList Definition
2024 ;
2025
2026Definition
Jeff Cohen361c3ef2007-01-21 19:19:31 +00002027 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner58af2a12006-02-15 07:22:58 +00002028 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00002029 CHECK_FOR_ERROR
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002030 }
2031 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer61c83e02006-08-18 08:43:06 +00002032 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002033 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002034 | MODULE ASM_TOK AsmBlock {
Reid Spencer61c83e02006-08-18 08:43:06 +00002035 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002036 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002037 | OptLocalAssign TYPE Types {
Reid Spencer14310612006-12-31 05:40:51 +00002038 if (!UpRefs.empty())
2039 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002040 // Eagerly resolve types. This is not an optimization, this is a
2041 // requirement that is due to the fact that we could have this:
2042 //
2043 // %list = type { %list * }
2044 // %list = type { %list * } ; repeated type decl
2045 //
2046 // If types are not resolved eagerly, then the two types will not be
2047 // determined to be the same type!
2048 //
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002049 ResolveTypeTo($1, *$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002050
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002051 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002052 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002053 // If this is a named type that is not a redefinition, add it to the slot
2054 // table.
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002055 CurModule.Types.push_back(*$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002056 }
Reid Spencera132e042006-12-03 05:46:11 +00002057
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002058 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002059 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002060 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002061 | OptLocalAssign TYPE VOID {
Reid Spencer14310612006-12-31 05:40:51 +00002062 ResolveTypeTo($1, $3);
2063
2064 if (!setTypeName($3, $1) && !$1) {
2065 CHECK_FOR_ERROR
2066 // If this is a named type that is not a redefinition, add it to the slot
2067 // table.
2068 CurModule.Types.push_back($3);
2069 }
2070 CHECK_FOR_ERROR
2071 }
Christopher Lambbf3348d2007-12-12 08:45:45 +00002072 | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal
2073 OptAddrSpace {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002074 /* "Externally Visible" Linkage */
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002075 if ($5 == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002076 GEN_ERROR("Global value initializer is not a constant");
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002077 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
Christopher Lambbf3348d2007-12-12 08:45:45 +00002078 $2, $4, $5->getType(), $5, $3, $6);
Christopher Lamba8ed9bf2007-12-11 09:02:08 +00002079 CHECK_FOR_ERROR
2080 } GlobalVarAttributes {
2081 CurGV = 0;
2082 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002083 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambbf3348d2007-12-12 08:45:45 +00002084 ConstVal OptAddrSpace {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002085 if ($6 == 0)
2086 GEN_ERROR("Global value initializer is not a constant");
Christopher Lambbf3348d2007-12-12 08:45:45 +00002087 CurGV = ParseGlobalVariable($1, $2, $3, $5, $6->getType(), $6, $4, $7);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002088 CHECK_FOR_ERROR
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002089 } GlobalVarAttributes {
2090 CurGV = 0;
2091 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002092 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
Christopher Lambbf3348d2007-12-12 08:45:45 +00002093 Types OptAddrSpace {
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002094 if (!UpRefs.empty())
2095 GEN_ERROR("Invalid upreference in type: " + (*$6)->getDescription());
Christopher Lambbf3348d2007-12-12 08:45:45 +00002096 CurGV = ParseGlobalVariable($1, $2, $3, $5, *$6, 0, $4, $7);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00002097 CHECK_FOR_ERROR
2098 delete $6;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002099 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002100 CurGV = 0;
2101 CHECK_FOR_ERROR
2102 }
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002103 | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage AliaseeRef {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002104 std::string Name;
2105 if ($1) {
2106 Name = *$1;
2107 delete $1;
2108 }
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002109 if (Name.empty())
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002110 GEN_ERROR("Alias name cannot be empty");
2111
2112 Constant* Aliasee = $5;
2113 if (Aliasee == 0)
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002114 GEN_ERROR(std::string("Invalid aliasee for alias: ") + Name);
Anton Korobeynikov38e09802007-04-28 13:48:45 +00002115
2116 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), $4, Name, Aliasee,
2117 CurModule.CurrentModule);
2118 GA->setVisibility($2);
2119 InsertValue(GA, CurModule.Values);
Chris Lattner569f7372007-09-10 23:24:14 +00002120
2121
2122 // If there was a forward reference of this alias, resolve it now.
2123
2124 ValID ID;
2125 if (!Name.empty())
2126 ID = ValID::createGlobalName(Name);
2127 else
2128 ID = ValID::createGlobalID(CurModule.Values.size()-1);
2129
2130 if (GlobalValue *FWGV =
2131 CurModule.GetForwardRefForGlobal(GA->getType(), ID)) {
2132 // Replace uses of the fwdref with the actual alias.
2133 FWGV->replaceAllUsesWith(GA);
2134 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(FWGV))
2135 GV->eraseFromParent();
2136 else
2137 cast<Function>(FWGV)->eraseFromParent();
2138 }
2139 ID.destroy();
2140
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002141 CHECK_FOR_ERROR
Anton Korobeynikov77d0f972007-04-25 14:29:12 +00002142 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002143 | TARGET TargetDefinition {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002144 CHECK_FOR_ERROR
2145 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002146 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00002147 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002148 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002149 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002150
2151
2152AsmBlock : STRINGCONSTANT {
2153 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattner58af2a12006-02-15 07:22:58 +00002154 if (AsmSoFar.empty())
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002155 CurModule.CurrentModule->setModuleInlineAsm(*$1);
Chris Lattner58af2a12006-02-15 07:22:58 +00002156 else
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002157 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+*$1);
2158 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002159 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002160};
2161
Reid Spencer41dff5e2007-01-26 08:05:27 +00002162TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002163 CurModule.CurrentModule->setTargetTriple(*$3);
2164 delete $3;
John Criswell2f6a8b12006-10-24 19:09:48 +00002165 }
Chris Lattner1ae022f2006-10-22 06:08:13 +00002166 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002167 CurModule.CurrentModule->setDataLayout(*$3);
2168 delete $3;
Owen Anderson1dc69692006-10-18 02:21:48 +00002169 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002170
2171LibrariesDefinition : '[' LibList ']';
2172
2173LibList : LibList ',' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002174 CurModule.CurrentModule->addLibrary(*$3);
2175 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002176 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002177 }
2178 | STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002179 CurModule.CurrentModule->addLibrary(*$1);
2180 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002181 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002182 }
2183 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00002184 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002185 }
2186 ;
2187
2188//===----------------------------------------------------------------------===//
2189// Rules to match Function Headers
2190//===----------------------------------------------------------------------===//
2191
Reid Spencer41dff5e2007-01-26 08:05:27 +00002192ArgListH : ArgListH ',' Types OptParamAttrs OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002193 if (!UpRefs.empty())
2194 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
2195 if (*$3 == Type::VoidTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00002196 GEN_ERROR("void typed arguments are invalid");
Reid Spencer14310612006-12-31 05:40:51 +00002197 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00002198 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002199 $1->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002200 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002201 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002202 | Types OptParamAttrs OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002203 if (!UpRefs.empty())
2204 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2205 if (*$1 == Type::VoidTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00002206 GEN_ERROR("void typed arguments are invalid");
Reid Spencer14310612006-12-31 05:40:51 +00002207 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2208 $$ = new ArgListType;
2209 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002210 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002211 };
2212
2213ArgList : ArgListH {
2214 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002215 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002216 }
2217 | ArgListH ',' DOTDOTDOT {
2218 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002219 struct ArgListEntry E;
2220 E.Ty = new PATypeHolder(Type::VoidTy);
2221 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002222 E.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00002223 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002224 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002225 }
2226 | DOTDOTDOT {
Reid Spencer14310612006-12-31 05:40:51 +00002227 $$ = new ArgListType;
2228 struct ArgListEntry E;
2229 E.Ty = new PATypeHolder(Type::VoidTy);
2230 E.Name = 0;
Reid Spencer18da0722007-04-11 02:44:20 +00002231 E.Attrs = ParamAttr::None;
Reid Spencer14310612006-12-31 05:40:51 +00002232 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002233 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002234 }
2235 | /* empty */ {
2236 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00002237 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002238 };
2239
Reid Spencer41dff5e2007-01-26 08:05:27 +00002240FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002241 OptFuncAttrs OptSection OptAlign OptGC {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002242 std::string FunctionName(*$3);
2243 delete $3; // Free strdup'd memory!
Chris Lattner58af2a12006-02-15 07:22:58 +00002244
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002245 // Check the function result for abstractness if this is a define. We should
2246 // have no abstract types at this point
Reid Spencer218ded22007-01-05 17:07:23 +00002247 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2248 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002249
Chris Lattner58af2a12006-02-15 07:22:58 +00002250 std::vector<const Type*> ParamTypeList;
Christopher Lamb5c104242007-04-22 20:09:11 +00002251 ParamAttrsVector Attrs;
2252 if ($7 != ParamAttr::None) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002253 ParamAttrsWithIndex PAWI;
2254 PAWI.index = 0;
2255 PAWI.attrs = $7;
Christopher Lamb5c104242007-04-22 20:09:11 +00002256 Attrs.push_back(PAWI);
2257 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002258 if ($5) { // If there are arguments...
Reid Spencer7b5d4662007-04-09 06:16:21 +00002259 unsigned index = 1;
2260 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002261 const Type* Ty = I->Ty->get();
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002262 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2263 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencer14310612006-12-31 05:40:51 +00002264 ParamTypeList.push_back(Ty);
2265 if (Ty != Type::VoidTy)
Christopher Lamb5c104242007-04-22 20:09:11 +00002266 if (I->Attrs != ParamAttr::None) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002267 ParamAttrsWithIndex PAWI;
2268 PAWI.index = index;
2269 PAWI.attrs = I->Attrs;
Christopher Lamb5c104242007-04-22 20:09:11 +00002270 Attrs.push_back(PAWI);
2271 }
Reid Spencer14310612006-12-31 05:40:51 +00002272 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002273 }
2274
2275 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2276 if (isVarArg) ParamTypeList.pop_back();
2277
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00002278 const ParamAttrsList *PAL = 0;
Christopher Lamb5c104242007-04-22 20:09:11 +00002279 if (!Attrs.empty())
2280 PAL = ParamAttrsList::get(Attrs);
Reid Spencer7b5d4662007-04-09 06:16:21 +00002281
Duncan Sandsdc024672007-11-27 13:23:08 +00002282 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002283 const PointerType *PFT = PointerType::getUnqual(FT);
Reid Spencer218ded22007-01-05 17:07:23 +00002284 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002285
2286 ValID ID;
2287 if (!FunctionName.empty()) {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002288 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattner58af2a12006-02-15 07:22:58 +00002289 } else {
Reid Spencer93c40032007-03-19 18:40:50 +00002290 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002291 }
2292
2293 Function *Fn = 0;
2294 // See if this function was forward referenced. If so, recycle the object.
2295 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2296 // Move the function to the end of the list, from whereever it was
2297 // previously inserted.
2298 Fn = cast<Function>(FWRef);
Duncan Sandsdc024672007-11-27 13:23:08 +00002299 assert(!Fn->getParamAttrs() && "Forward reference has parameter attributes!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002300 CurModule.CurrentModule->getFunctionList().remove(Fn);
2301 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2302 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spenceref9b9a72007-02-05 20:47:22 +00002303 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002304 if (Fn->getFunctionType() != FT ) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002305 // The existing function doesn't have the same type. This is an overload
2306 // error.
2307 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Duncan Sandsdc024672007-11-27 13:23:08 +00002308 } else if (Fn->getParamAttrs() != PAL) {
2309 // The existing function doesn't have the same parameter attributes.
2310 // This is an overload error.
2311 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002312 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
Chris Lattner6cdc6822007-04-26 05:31:05 +00002313 // Neither the existing or the current function is a declaration and they
2314 // have the same name and same type. Clearly this is a redefinition.
2315 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Duncan Sandsdc024672007-11-27 13:23:08 +00002316 } else if (Fn->isDeclaration()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002317 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattner58af2a12006-02-15 07:22:58 +00002318 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2319 AI != AE; ++AI)
2320 AI->setName("");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002321 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002322 } else { // Not already defined?
Chris Lattner6cdc6822007-04-26 05:31:05 +00002323 Fn = new Function(FT, GlobalValue::ExternalWeakLinkage, FunctionName,
Chris Lattner58af2a12006-02-15 07:22:58 +00002324 CurModule.CurrentModule);
2325 InsertValue(Fn, CurModule.Values);
2326 }
2327
2328 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002329
2330 if (CurFun.isDeclare) {
2331 // If we have declaration, always overwrite linkage. This will allow us to
2332 // correctly handle cases, when pointer to function is passed as argument to
2333 // another function.
2334 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002335 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002336 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002337 Fn->setCallingConv($1);
Duncan Sandsdc024672007-11-27 13:23:08 +00002338 Fn->setParamAttrs(PAL);
Reid Spencer218ded22007-01-05 17:07:23 +00002339 Fn->setAlignment($9);
2340 if ($8) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002341 Fn->setSection(*$8);
2342 delete $8;
Chris Lattner58af2a12006-02-15 07:22:58 +00002343 }
Gordon Henriksen80a75bf2007-12-10 03:18:06 +00002344 if ($10) {
2345 Fn->setCollector($10->c_str());
2346 delete $10;
2347 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002348
2349 // Add all of the arguments we parsed to the function...
2350 if ($5) { // Is null if empty...
2351 if (isVarArg) { // Nuke the last entry
Reid Spenceref9b9a72007-02-05 20:47:22 +00002352 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0 &&
Reid Spencera9720f52007-02-05 17:04:00 +00002353 "Not a varargs marker!");
Reid Spencer14310612006-12-31 05:40:51 +00002354 delete $5->back().Ty;
Chris Lattner58af2a12006-02-15 07:22:58 +00002355 $5->pop_back(); // Delete the last entry
2356 }
2357 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002358 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencer14310612006-12-31 05:40:51 +00002359 unsigned Idx = 1;
Reid Spenceref9b9a72007-02-05 20:47:22 +00002360 for (ArgListType::iterator I = $5->begin();
2361 I != $5->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencer14310612006-12-31 05:40:51 +00002362 delete I->Ty; // Delete the typeholder...
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002363 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002364 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002365 InsertValue(ArgIt);
Reid Spencer14310612006-12-31 05:40:51 +00002366 Idx++;
Chris Lattner58af2a12006-02-15 07:22:58 +00002367 }
Reid Spencera132e042006-12-03 05:46:11 +00002368
Chris Lattner58af2a12006-02-15 07:22:58 +00002369 delete $5; // We're now done with the argument list
2370 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002371 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002372};
2373
2374BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2375
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002376FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner58af2a12006-02-15 07:22:58 +00002377 $$ = CurFun.CurrentFunction;
2378
2379 // Make sure that we keep track of the linkage type even if there was a
2380 // previous "declare".
2381 $$->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002382 $$->setVisibility($2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002383};
2384
2385END : ENDTOK | '}'; // Allow end of '}' to end a function
2386
2387Function : BasicBlockList END {
2388 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002389 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002390};
2391
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002392FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencer14310612006-12-31 05:40:51 +00002393 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002394 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002395 $$ = CurFun.CurrentFunction;
2396 CurFun.FunctionDone();
2397 CHECK_FOR_ERROR
2398 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002399
2400//===----------------------------------------------------------------------===//
2401// Rules to match Basic Blocks
2402//===----------------------------------------------------------------------===//
2403
2404OptSideEffect : /* empty */ {
2405 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002406 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002407 }
2408 | SIDEEFFECT {
2409 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002410 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002411 };
2412
2413ConstValueRef : ESINT64VAL { // A reference to a direct constant
2414 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002415 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002416 }
2417 | EUINT64VAL {
2418 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002419 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002420 }
2421 | FPVAL { // Perhaps it's an FP constant?
2422 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002423 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002424 }
2425 | TRUETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002426 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002427 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002428 }
2429 | FALSETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002430 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002431 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002432 }
2433 | NULL_TOK {
2434 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002435 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002436 }
2437 | UNDEF {
2438 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002439 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002440 }
2441 | ZEROINITIALIZER { // A vector zero constant.
2442 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002443 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002444 }
2445 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencera132e042006-12-03 05:46:11 +00002446 const Type *ETy = (*$2)[0]->getType();
Chris Lattner58af2a12006-02-15 07:22:58 +00002447 int NumElements = $2->size();
2448
Reid Spencer9d6565a2007-02-15 02:26:10 +00002449 VectorType* pt = VectorType::get(ETy, NumElements);
Chris Lattner58af2a12006-02-15 07:22:58 +00002450 PATypeHolder* PTy = new PATypeHolder(
Reid Spencera132e042006-12-03 05:46:11 +00002451 HandleUpRefs(
Reid Spencer9d6565a2007-02-15 02:26:10 +00002452 VectorType::get(
Reid Spencera132e042006-12-03 05:46:11 +00002453 ETy,
2454 NumElements)
2455 )
2456 );
Chris Lattner58af2a12006-02-15 07:22:58 +00002457
2458 // Verify all elements are correct type!
2459 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00002460 if (ETy != (*$2)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002461 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00002462 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencera132e042006-12-03 05:46:11 +00002463 (*$2)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00002464 }
2465
Reid Spencer9d6565a2007-02-15 02:26:10 +00002466 $$ = ValID::create(ConstantVector::get(pt, *$2));
Chris Lattner58af2a12006-02-15 07:22:58 +00002467 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002468 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002469 }
2470 | ConstExpr {
Reid Spencera132e042006-12-03 05:46:11 +00002471 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002472 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002473 }
2474 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002475 $$ = ValID::createInlineAsm(*$3, *$5, $2);
2476 delete $3;
2477 delete $5;
Reid Spencer61c83e02006-08-18 08:43:06 +00002478 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002479 };
2480
2481// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2482// another value.
2483//
Reid Spencer41dff5e2007-01-26 08:05:27 +00002484SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2485 $$ = ValID::createLocalID($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002486 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002487 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002488 | GLOBALVAL_ID {
2489 $$ = ValID::createGlobalID($1);
2490 CHECK_FOR_ERROR
2491 }
2492 | LocalName { // Is it a named reference...?
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002493 $$ = ValID::createLocalName(*$1);
2494 delete $1;
Reid Spencer41dff5e2007-01-26 08:05:27 +00002495 CHECK_FOR_ERROR
2496 }
2497 | GlobalName { // Is it a named reference...?
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002498 $$ = ValID::createGlobalName(*$1);
2499 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002500 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002501 };
2502
2503// ValueRef - A reference to a definition... either constant or symbolic
2504ValueRef : SymbolicValueRef | ConstValueRef;
2505
2506
2507// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2508// type immediately preceeds the value reference, and allows complex constant
2509// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2510ResolvedVal : Types ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002511 if (!UpRefs.empty())
2512 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2513 $$ = getVal(*$1, $2);
2514 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002515 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00002516 }
2517 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002518
2519BasicBlockList : BasicBlockList BasicBlock {
2520 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002521 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002522 }
2523 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
2524 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002525 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002526 };
2527
2528
2529// Basic blocks are terminated by branching instructions:
2530// br, br/cc, switch, ret
2531//
Reid Spencer41dff5e2007-01-26 08:05:27 +00002532BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner58af2a12006-02-15 07:22:58 +00002533 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002534 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002535 InsertValue($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002536 $1->getInstList().push_back($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002537 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002538 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002539 };
2540
2541InstructionList : InstructionList Inst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002542 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2543 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2544 if (CI2->getParent() == 0)
2545 $1->getInstList().push_back(CI2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002546 $1->getInstList().push_back($2);
2547 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002548 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002549 }
Reid Spencer93c40032007-03-19 18:40:50 +00002550 | /* empty */ { // Empty space between instruction lists
2551 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
Reid Spencer61c83e02006-08-18 08:43:06 +00002552 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002553 }
Reid Spencer93c40032007-03-19 18:40:50 +00002554 | LABELSTR { // Labelled (named) basic block
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002555 $$ = defineBBVal(ValID::createLocalName(*$1));
2556 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002557 CHECK_FOR_ERROR
Reid Spencer0a8a16b2007-05-22 18:52:55 +00002558
Chris Lattner58af2a12006-02-15 07:22:58 +00002559 };
2560
2561BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencera132e042006-12-03 05:46:11 +00002562 $$ = new ReturnInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002563 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002564 }
Reid Spencer93c40032007-03-19 18:40:50 +00002565 | RET VOID { // Return with no result...
Chris Lattner58af2a12006-02-15 07:22:58 +00002566 $$ = new ReturnInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002567 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002568 }
Reid Spencer93c40032007-03-19 18:40:50 +00002569 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002570 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002571 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002572 $$ = new BranchInst(tmpBB);
Reid Spencer93c40032007-03-19 18:40:50 +00002573 } // Conditional Branch...
Reid Spencer6f407902007-01-13 05:00:46 +00002574 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
2575 assert(cast<IntegerType>($2)->getBitWidth() == 1 && "Not Bool?");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002576 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002577 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002578 BasicBlock* tmpBBB = getBBVal($9);
2579 CHECK_FOR_ERROR
Reid Spencer4fe16d62007-01-11 18:21:29 +00002580 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002581 CHECK_FOR_ERROR
2582 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattner58af2a12006-02-15 07:22:58 +00002583 }
2584 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002585 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002586 CHECK_FOR_ERROR
2587 BasicBlock* tmpBB = getBBVal($6);
2588 CHECK_FOR_ERROR
2589 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002590 $$ = S;
2591
2592 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2593 E = $8->end();
2594 for (; I != E; ++I) {
2595 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2596 S->addCase(CI, I->second);
2597 else
Reid Spencerb5334b02007-02-05 10:18:06 +00002598 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattner58af2a12006-02-15 07:22:58 +00002599 }
2600 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002601 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002602 }
2603 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002604 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002605 CHECK_FOR_ERROR
2606 BasicBlock* tmpBB = getBBVal($6);
2607 CHECK_FOR_ERROR
2608 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattner58af2a12006-02-15 07:22:58 +00002609 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002610 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002611 }
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002612 | INVOKE OptCallingConv ResultTypes ValueRef '(' ParamList ')' OptFuncAttrs
Chris Lattner58af2a12006-02-15 07:22:58 +00002613 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattner58af2a12006-02-15 07:22:58 +00002614
Reid Spencer14310612006-12-31 05:40:51 +00002615 // Handle the short syntax
2616 const PointerType *PFTy = 0;
2617 const FunctionType *Ty = 0;
Reid Spencer218ded22007-01-05 17:07:23 +00002618 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00002619 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2620 // Pull out the types of all of the arguments...
2621 std::vector<const Type*> ParamTypes;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002622 ParamList::iterator I = $6->begin(), E = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002623 for (; I != E; ++I) {
Reid Spencer14310612006-12-31 05:40:51 +00002624 const Type *Ty = I->Val->getType();
2625 if (Ty == Type::VoidTy)
2626 GEN_ERROR("Short call syntax cannot be used with varargs");
2627 ParamTypes.push_back(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002628 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002629 Ty = FunctionType::get($3->get(), ParamTypes, false);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002630 PFTy = PointerType::getUnqual(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002631 }
2632
Reid Spencer66728ef2007-03-20 01:13:36 +00002633 delete $3;
2634
Chris Lattner58af2a12006-02-15 07:22:58 +00002635 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002636 CHECK_FOR_ERROR
Reid Spencer218ded22007-01-05 17:07:23 +00002637 BasicBlock *Normal = getBBVal($11);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002638 CHECK_FOR_ERROR
Reid Spencer218ded22007-01-05 17:07:23 +00002639 BasicBlock *Except = getBBVal($14);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002640 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002641
Duncan Sandsdc024672007-11-27 13:23:08 +00002642 ParamAttrsVector Attrs;
2643 if ($8 != ParamAttr::None) {
2644 ParamAttrsWithIndex PAWI; PAWI.index = 0; PAWI.attrs = $8;
2645 Attrs.push_back(PAWI);
2646 }
2647
Reid Spencer14310612006-12-31 05:40:51 +00002648 // Check the arguments
2649 ValueList Args;
2650 if ($6->empty()) { // Has no arguments?
2651 // Make sure no arguments is a good thing!
2652 if (Ty->getNumParams() != 0)
2653 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00002654 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00002655 } else { // Has arguments?
2656 // Loop through FunctionType's arguments and ensure they are specified
2657 // correctly!
Chris Lattner58af2a12006-02-15 07:22:58 +00002658 FunctionType::param_iterator I = Ty->param_begin();
2659 FunctionType::param_iterator E = Ty->param_end();
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002660 ParamList::iterator ArgI = $6->begin(), ArgE = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002661 unsigned index = 1;
Chris Lattner58af2a12006-02-15 07:22:58 +00002662
Duncan Sandsdc024672007-11-27 13:23:08 +00002663 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002664 if (ArgI->Val->getType() != *I)
2665 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00002666 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00002667 Args.push_back(ArgI->Val);
Duncan Sandsdc024672007-11-27 13:23:08 +00002668 if (ArgI->Attrs != ParamAttr::None) {
2669 ParamAttrsWithIndex PAWI;
2670 PAWI.index = index;
2671 PAWI.attrs = ArgI->Attrs;
2672 Attrs.push_back(PAWI);
2673 }
Reid Spencer14310612006-12-31 05:40:51 +00002674 }
Reid Spencera132e042006-12-03 05:46:11 +00002675
Reid Spencer14310612006-12-31 05:40:51 +00002676 if (Ty->isVarArg()) {
2677 if (I == E)
2678 for (; ArgI != ArgE; ++ArgI)
2679 Args.push_back(ArgI->Val); // push the remaining varargs
2680 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00002681 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00002682 }
Reid Spencer14310612006-12-31 05:40:51 +00002683
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00002684 const ParamAttrsList *PAL = 0;
Duncan Sandsdc024672007-11-27 13:23:08 +00002685 if (!Attrs.empty())
2686 PAL = ParamAttrsList::get(Attrs);
2687
Reid Spencer14310612006-12-31 05:40:51 +00002688 // Create the InvokeInst
Chris Lattnerd80fb8b2007-08-29 16:15:23 +00002689 InvokeInst *II = new InvokeInst(V, Normal, Except, Args.begin(), Args.end());
Reid Spencer14310612006-12-31 05:40:51 +00002690 II->setCallingConv($2);
Duncan Sandsdc024672007-11-27 13:23:08 +00002691 II->setParamAttrs(PAL);
Reid Spencer14310612006-12-31 05:40:51 +00002692 $$ = II;
Chris Lattner58af2a12006-02-15 07:22:58 +00002693 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002694 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002695 }
2696 | UNWIND {
2697 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002698 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002699 }
2700 | UNREACHABLE {
2701 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002702 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002703 };
2704
2705
2706
2707JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2708 $$ = $1;
Reid Spencer93c40032007-03-19 18:40:50 +00002709 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002710 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002711 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002712 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00002713
Reid Spencer5b7e7532006-09-28 19:28:24 +00002714 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002715 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002716 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002717 }
2718 | IntType ConstValueRef ',' LABEL ValueRef {
2719 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencer93c40032007-03-19 18:40:50 +00002720 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002721 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002722
2723 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($5);
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
Reid Spencer41dff5e2007-01-26 08:05:27 +00002731Inst : OptLocalAssign InstVal {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002732 // Is this definition named?? if so, assign the name...
2733 setValueName($2, $1);
2734 CHECK_FOR_ERROR
2735 InsertValue($2);
2736 $$ = $2;
2737 CHECK_FOR_ERROR
2738 };
2739
Chris Lattner58af2a12006-02-15 07:22:58 +00002740
2741PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer14310612006-12-31 05:40:51 +00002742 if (!UpRefs.empty())
2743 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002744 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencera132e042006-12-03 05:46:11 +00002745 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002746 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002747 BasicBlock* tmpBB = getBBVal($5);
2748 CHECK_FOR_ERROR
2749 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencera132e042006-12-03 05:46:11 +00002750 delete $1;
Chris Lattner58af2a12006-02-15 07:22:58 +00002751 }
2752 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2753 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002754 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002755 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002756 BasicBlock* tmpBB = getBBVal($6);
2757 CHECK_FOR_ERROR
2758 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002759 };
2760
2761
Duncan Sandsdc024672007-11-27 13:23:08 +00002762ParamList : Types OptParamAttrs ValueRef OptParamAttrs {
2763 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Reid Spencer14310612006-12-31 05:40:51 +00002764 if (!UpRefs.empty())
2765 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2766 // Used for call and invoke instructions
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002767 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00002768 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getVal($1->get(), $3);
Reid Spencer14310612006-12-31 05:40:51 +00002769 $$->push_back(E);
Reid Spencer66728ef2007-03-20 01:13:36 +00002770 delete $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002771 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002772 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002773 | LABEL OptParamAttrs ValueRef OptParamAttrs {
2774 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002775 // Labels are only valid in ASMs
2776 $$ = new ParamList();
Duncan Sandsdc024672007-11-27 13:23:08 +00002777 ParamListEntry E; E.Attrs = $2 | $4; E.Val = getBBVal($3);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002778 $$->push_back(E);
Duncan Sandsdc024672007-11-27 13:23:08 +00002779 CHECK_FOR_ERROR
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002780 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002781 | ParamList ',' Types OptParamAttrs ValueRef OptParamAttrs {
2782 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Reid Spencer14310612006-12-31 05:40:51 +00002783 if (!UpRefs.empty())
2784 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002785 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002786 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getVal($3->get(), $5);
Reid Spencer14310612006-12-31 05:40:51 +00002787 $$->push_back(E);
Reid Spencer66728ef2007-03-20 01:13:36 +00002788 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002789 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00002790 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002791 | ParamList ',' LABEL OptParamAttrs ValueRef OptParamAttrs {
2792 // FIXME: Remove trailing OptParamAttrs in LLVM 3.0, it was a mistake in 2.0
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002793 $$ = $1;
Duncan Sandsdc024672007-11-27 13:23:08 +00002794 ParamListEntry E; E.Attrs = $4 | $6; E.Val = getBBVal($5);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002795 $$->push_back(E);
2796 CHECK_FOR_ERROR
2797 }
2798 | /*empty*/ { $$ = new ParamList(); };
Chris Lattner58af2a12006-02-15 07:22:58 +00002799
Reid Spencer14310612006-12-31 05:40:51 +00002800IndexList // Used for gep instructions and constant expressions
Reid Spencerc6c59fd2006-12-31 21:47:02 +00002801 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencer14310612006-12-31 05:40:51 +00002802 | IndexList ',' ResolvedVal {
2803 $$ = $1;
2804 $$->push_back($3);
2805 CHECK_FOR_ERROR
2806 }
Reid Spencerc6c59fd2006-12-31 21:47:02 +00002807 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002808
2809OptTailCall : TAIL CALL {
2810 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002811 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002812 }
2813 | CALL {
2814 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002815 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002816 };
2817
Chris Lattner58af2a12006-02-15 07:22:58 +00002818InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002819 if (!UpRefs.empty())
2820 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002821 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencer9d6565a2007-02-15 02:26:10 +00002822 !isa<VectorType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002823 GEN_ERROR(
Reid Spencerb5334b02007-02-05 10:18:06 +00002824 "Arithmetic operator requires integer, FP, or packed operands");
Reid Spencera132e042006-12-03 05:46:11 +00002825 Value* val1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002826 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002827 Value* val2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002828 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002829 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002830 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002831 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00002832 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002833 }
2834 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002835 if (!UpRefs.empty())
2836 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002837 if (!(*$2)->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00002838 if (Instruction::isShift($1) || !isa<VectorType>($2->get()) ||
2839 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00002840 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00002841 }
Reid Spencera132e042006-12-03 05:46:11 +00002842 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002843 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002844 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002845 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002846 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002847 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002848 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00002849 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002850 }
Reid Spencera132e042006-12-03 05:46:11 +00002851 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002852 if (!UpRefs.empty())
2853 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002854 if (isa<VectorType>((*$3).get()))
Chris Lattner32980692007-02-19 07:44:24 +00002855 GEN_ERROR("Vector types not supported by icmp instruction");
Reid Spencera132e042006-12-03 05:46:11 +00002856 Value* tmpVal1 = getVal(*$3, $4);
2857 CHECK_FOR_ERROR
2858 Value* tmpVal2 = getVal(*$3, $6);
2859 CHECK_FOR_ERROR
2860 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2861 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002862 GEN_ERROR("icmp operator returned null");
Reid Spencer66728ef2007-03-20 01:13:36 +00002863 delete $3;
Reid Spencera132e042006-12-03 05:46:11 +00002864 }
2865 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002866 if (!UpRefs.empty())
2867 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002868 if (isa<VectorType>((*$3).get()))
Chris Lattner32980692007-02-19 07:44:24 +00002869 GEN_ERROR("Vector types not supported by fcmp instruction");
Reid Spencera132e042006-12-03 05:46:11 +00002870 Value* tmpVal1 = getVal(*$3, $4);
2871 CHECK_FOR_ERROR
2872 Value* tmpVal2 = getVal(*$3, $6);
2873 CHECK_FOR_ERROR
2874 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2875 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002876 GEN_ERROR("fcmp operator returned null");
Reid Spencer66728ef2007-03-20 01:13:36 +00002877 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00002878 }
Reid Spencer3da59db2006-11-27 01:05:10 +00002879 | CastOps ResolvedVal TO Types {
Reid Spencer14310612006-12-31 05:40:51 +00002880 if (!UpRefs.empty())
2881 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00002882 Value* Val = $2;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00002883 const Type* DestTy = $4->get();
2884 if (!CastInst::castIsValid($1, Val, DestTy))
2885 GEN_ERROR("invalid cast opcode for cast from '" +
2886 Val->getType()->getDescription() + "' to '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00002887 DestTy->getDescription() + "'");
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00002888 $$ = CastInst::create($1, Val, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00002889 delete $4;
Chris Lattner58af2a12006-02-15 07:22:58 +00002890 }
2891 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer4fe16d62007-01-11 18:21:29 +00002892 if ($2->getType() != Type::Int1Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00002893 GEN_ERROR("select condition must be boolean");
Reid Spencera132e042006-12-03 05:46:11 +00002894 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002895 GEN_ERROR("select value types should match");
Reid Spencera132e042006-12-03 05:46:11 +00002896 $$ = new SelectInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002897 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002898 }
2899 | VAARG ResolvedVal ',' Types {
Reid Spencer14310612006-12-31 05:40:51 +00002900 if (!UpRefs.empty())
2901 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00002902 $$ = new VAArgInst($2, *$4);
2903 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002904 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002905 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002906 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002907 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerb5334b02007-02-05 10:18:06 +00002908 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00002909 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002910 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002911 }
2912 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002913 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00002914 GEN_ERROR("Invalid insertelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00002915 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002916 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002917 }
Chris Lattnerd5efe842006-04-08 01:18:56 +00002918 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002919 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00002920 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00002921 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002922 CHECK_FOR_ERROR
Chris Lattnerd5efe842006-04-08 01:18:56 +00002923 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002924 | PHI_TOK PHIList {
2925 const Type *Ty = $2->front().first->getType();
2926 if (!Ty->isFirstClassType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002927 GEN_ERROR("PHI node operands must be of first class type");
Chris Lattner58af2a12006-02-15 07:22:58 +00002928 $$ = new PHINode(Ty);
2929 ((PHINode*)$$)->reserveOperandSpace($2->size());
2930 while ($2->begin() != $2->end()) {
2931 if ($2->front().first->getType() != Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00002932 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattner58af2a12006-02-15 07:22:58 +00002933 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
2934 $2->pop_front();
2935 }
2936 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00002937 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002938 }
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002939 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ParamList ')'
Reid Spencer218ded22007-01-05 17:07:23 +00002940 OptFuncAttrs {
Reid Spencer14310612006-12-31 05:40:51 +00002941
2942 // Handle the short syntax
Reid Spencer3da59db2006-11-27 01:05:10 +00002943 const PointerType *PFTy = 0;
2944 const FunctionType *Ty = 0;
Reid Spencer218ded22007-01-05 17:07:23 +00002945 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00002946 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2947 // Pull out the types of all of the arguments...
2948 std::vector<const Type*> ParamTypes;
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002949 ParamList::iterator I = $6->begin(), E = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002950 for (; I != E; ++I) {
Reid Spencer14310612006-12-31 05:40:51 +00002951 const Type *Ty = I->Val->getType();
2952 if (Ty == Type::VoidTy)
2953 GEN_ERROR("Short call syntax cannot be used with varargs");
2954 ParamTypes.push_back(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002955 }
Duncan Sandsdc024672007-11-27 13:23:08 +00002956 Ty = FunctionType::get($3->get(), ParamTypes, false);
Christopher Lamb4374f8e2007-12-17 01:17:35 +00002957 PFTy = PointerType::getUnqual(Ty);
Chris Lattner58af2a12006-02-15 07:22:58 +00002958 }
Chris Lattner6cdc6822007-04-26 05:31:05 +00002959
Chris Lattner58af2a12006-02-15 07:22:58 +00002960 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002961 CHECK_FOR_ERROR
Chris Lattner6cdc6822007-04-26 05:31:05 +00002962
Reid Spencer7780acb2007-04-16 06:56:07 +00002963 // Check for call to invalid intrinsic to avoid crashing later.
2964 if (Function *theF = dyn_cast<Function>(V)) {
Reid Spencered48de22007-04-16 22:02:23 +00002965 if (theF->hasName() && (theF->getValueName()->getKeyLength() >= 5) &&
Reid Spencer36fdde12007-04-16 20:35:38 +00002966 (0 == strncmp(theF->getValueName()->getKeyData(), "llvm.", 5)) &&
2967 !theF->getIntrinsicID(true))
Reid Spencer7780acb2007-04-16 06:56:07 +00002968 GEN_ERROR("Call to invalid LLVM intrinsic function '" +
2969 theF->getName() + "'");
2970 }
2971
Duncan Sandsdc024672007-11-27 13:23:08 +00002972 // Set up the ParamAttrs for the function
2973 ParamAttrsVector Attrs;
2974 if ($8 != ParamAttr::None) {
2975 ParamAttrsWithIndex PAWI;
2976 PAWI.index = 0;
2977 PAWI.attrs = $8;
2978 Attrs.push_back(PAWI);
2979 }
Reid Spencer14310612006-12-31 05:40:51 +00002980 // Check the arguments
2981 ValueList Args;
2982 if ($6->empty()) { // Has no arguments?
Chris Lattner58af2a12006-02-15 07:22:58 +00002983 // Make sure no arguments is a good thing!
2984 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002985 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00002986 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00002987 } else { // Has arguments?
2988 // Loop through FunctionType's arguments and ensure they are specified
Duncan Sandsdc024672007-11-27 13:23:08 +00002989 // correctly. Also, gather any parameter attributes.
Chris Lattner58af2a12006-02-15 07:22:58 +00002990 FunctionType::param_iterator I = Ty->param_begin();
2991 FunctionType::param_iterator E = Ty->param_end();
Dale Johanneseneb57ea72007-11-05 21:20:28 +00002992 ParamList::iterator ArgI = $6->begin(), ArgE = $6->end();
Duncan Sandsdc024672007-11-27 13:23:08 +00002993 unsigned index = 1;
Chris Lattner58af2a12006-02-15 07:22:58 +00002994
Duncan Sandsdc024672007-11-27 13:23:08 +00002995 for (; ArgI != ArgE && I != E; ++ArgI, ++I, ++index) {
Reid Spencer14310612006-12-31 05:40:51 +00002996 if (ArgI->Val->getType() != *I)
2997 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00002998 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00002999 Args.push_back(ArgI->Val);
Duncan Sandsdc024672007-11-27 13:23:08 +00003000 if (ArgI->Attrs != ParamAttr::None) {
3001 ParamAttrsWithIndex PAWI;
3002 PAWI.index = index;
3003 PAWI.attrs = ArgI->Attrs;
3004 Attrs.push_back(PAWI);
3005 }
Reid Spencer14310612006-12-31 05:40:51 +00003006 }
3007 if (Ty->isVarArg()) {
3008 if (I == E)
3009 for (; ArgI != ArgE; ++ArgI)
3010 Args.push_back(ArgI->Val); // push the remaining varargs
3011 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00003012 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00003013 }
Duncan Sandsdc024672007-11-27 13:23:08 +00003014
3015 // Finish off the ParamAttrs and check them
Duncan Sandsafa3b6d2007-11-28 17:07:01 +00003016 const ParamAttrsList *PAL = 0;
Duncan Sandsdc024672007-11-27 13:23:08 +00003017 if (!Attrs.empty())
3018 PAL = ParamAttrsList::get(Attrs);
3019
Reid Spencer14310612006-12-31 05:40:51 +00003020 // Create the call node
David Greene718fda32007-08-01 03:59:32 +00003021 CallInst *CI = new CallInst(V, Args.begin(), Args.end());
Reid Spencer14310612006-12-31 05:40:51 +00003022 CI->setTailCall($1);
3023 CI->setCallingConv($2);
Duncan Sandsdc024672007-11-27 13:23:08 +00003024 CI->setParamAttrs(PAL);
Reid Spencer14310612006-12-31 05:40:51 +00003025 $$ = CI;
Chris Lattner58af2a12006-02-15 07:22:58 +00003026 delete $6;
Reid Spencer41dff5e2007-01-26 08:05:27 +00003027 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00003028 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003029 }
3030 | MemoryInst {
3031 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00003032 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003033 };
3034
Chris Lattner58af2a12006-02-15 07:22:58 +00003035OptVolatile : VOLATILE {
3036 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00003037 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003038 }
3039 | /* empty */ {
3040 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00003041 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003042 };
3043
3044
3045
3046MemoryInst : MALLOC Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003047 if (!UpRefs.empty())
3048 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003049 $$ = new MallocInst(*$2, 0, $3);
3050 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003051 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003052 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003053 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003054 if (!UpRefs.empty())
3055 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003056 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003057 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003058 $$ = new MallocInst(*$2, tmpVal, $6);
3059 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003060 }
3061 | ALLOCA Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003062 if (!UpRefs.empty())
3063 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003064 $$ = new AllocaInst(*$2, 0, $3);
3065 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00003066 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003067 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00003068 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003069 if (!UpRefs.empty())
3070 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003071 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00003072 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00003073 $$ = new AllocaInst(*$2, tmpVal, $6);
3074 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00003075 }
3076 | FREE ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00003077 if (!isa<PointerType>($2->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003078 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerb5334b02007-02-05 10:18:06 +00003079 $2->getType()->getDescription() + "");
Reid Spencera132e042006-12-03 05:46:11 +00003080 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00003081 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00003082 }
3083
Christopher Lamb5c104242007-04-22 20:09:11 +00003084 | OptVolatile LOAD Types ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003085 if (!UpRefs.empty())
3086 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003087 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00003088 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003089 (*$3)->getDescription());
3090 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00003091 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003092 (*$3)->getDescription());
3093 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00003094 CHECK_FOR_ERROR
Christopher Lamb5c104242007-04-22 20:09:11 +00003095 $$ = new LoadInst(tmpVal, "", $1, $5);
Reid Spencera132e042006-12-03 05:46:11 +00003096 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00003097 }
Christopher Lamb5c104242007-04-22 20:09:11 +00003098 | OptVolatile STORE ResolvedVal ',' Types ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00003099 if (!UpRefs.empty())
3100 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003101 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00003102 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00003103 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00003104 (*$5)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00003105 const Type *ElTy = PT->getElementType();
Reid Spencera132e042006-12-03 05:46:11 +00003106 if (ElTy != $3->getType())
3107 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerb5334b02007-02-05 10:18:06 +00003108 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00003109
Reid Spencera132e042006-12-03 05:46:11 +00003110 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00003111 CHECK_FOR_ERROR
Christopher Lamb5c104242007-04-22 20:09:11 +00003112 $$ = new StoreInst($3, tmpVal, $1, $7);
Reid Spencera132e042006-12-03 05:46:11 +00003113 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00003114 }
3115 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer14310612006-12-31 05:40:51 +00003116 if (!UpRefs.empty())
3117 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00003118 if (!isa<PointerType>($2->get()))
Reid Spencerb5334b02007-02-05 10:18:06 +00003119 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00003120
David Greene5fd22a82007-09-04 18:46:50 +00003121 if (!GetElementPtrInst::getIndexedType(*$2, $4->begin(), $4->end(), true))
Reid Spencer61c83e02006-08-18 08:43:06 +00003122 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00003123 (*$2)->getDescription()+ "'");
Reid Spencera132e042006-12-03 05:46:11 +00003124 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00003125 CHECK_FOR_ERROR
David Greene5fd22a82007-09-04 18:46:50 +00003126 $$ = new GetElementPtrInst(tmpVal, $4->begin(), $4->end());
Reid Spencera132e042006-12-03 05:46:11 +00003127 delete $2;
Reid Spencer5b7e7532006-09-28 19:28:24 +00003128 delete $4;
Chris Lattner58af2a12006-02-15 07:22:58 +00003129 };
3130
3131
3132%%
Reid Spencer61c83e02006-08-18 08:43:06 +00003133
Reid Spencer14310612006-12-31 05:40:51 +00003134// common code from the two 'RunVMAsmParser' functions
3135static Module* RunParser(Module * M) {
Reid Spencer14310612006-12-31 05:40:51 +00003136 CurModule.CurrentModule = M;
Reid Spencer14310612006-12-31 05:40:51 +00003137 // Check to make sure the parser succeeded
3138 if (yyparse()) {
3139 if (ParserResult)
3140 delete ParserResult;
3141 return 0;
3142 }
3143
Reid Spencer0d60b5a2007-03-30 01:37:39 +00003144 // Emit an error if there are any unresolved types left.
3145 if (!CurModule.LateResolveTypes.empty()) {
3146 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
3147 if (DID.Type == ValID::LocalName) {
3148 GenerateError("Undefined type remains at eof: '"+DID.getName() + "'");
3149 } else {
3150 GenerateError("Undefined type remains at eof: #" + itostr(DID.Num));
3151 }
3152 if (ParserResult)
3153 delete ParserResult;
3154 return 0;
3155 }
3156
3157 // Emit an error if there are any unresolved values left.
3158 if (!CurModule.LateResolveValues.empty()) {
3159 Value *V = CurModule.LateResolveValues.back();
3160 std::map<Value*, std::pair<ValID, int> >::iterator I =
3161 CurModule.PlaceHolderInfo.find(V);
3162
3163 if (I != CurModule.PlaceHolderInfo.end()) {
3164 ValID &DID = I->second.first;
3165 if (DID.Type == ValID::LocalName) {
3166 GenerateError("Undefined value remains at eof: "+DID.getName() + "'");
3167 } else {
3168 GenerateError("Undefined value remains at eof: #" + itostr(DID.Num));
3169 }
3170 if (ParserResult)
3171 delete ParserResult;
3172 return 0;
3173 }
3174 }
3175
Reid Spencer14310612006-12-31 05:40:51 +00003176 // Check to make sure that parsing produced a result
3177 if (!ParserResult)
3178 return 0;
3179
3180 // Reset ParserResult variable while saving its value for the result.
3181 Module *Result = ParserResult;
3182 ParserResult = 0;
3183
3184 return Result;
3185}
3186
Reid Spencer61c83e02006-08-18 08:43:06 +00003187void llvm::GenerateError(const std::string &message, int LineNo) {
Duncan Sandsdc024672007-11-27 13:23:08 +00003188 if (LineNo == -1) LineNo = LLLgetLineNo();
Reid Spencer61c83e02006-08-18 08:43:06 +00003189 // TODO: column number in exception
3190 if (TheParseError)
Duncan Sandsdc024672007-11-27 13:23:08 +00003191 TheParseError->setError(LLLgetFilename(), message, LineNo);
Reid Spencer61c83e02006-08-18 08:43:06 +00003192 TriggerError = 1;
3193}
3194
Chris Lattner58af2a12006-02-15 07:22:58 +00003195int yyerror(const char *ErrorMsg) {
Duncan Sandsdc024672007-11-27 13:23:08 +00003196 std::string where = LLLgetFilename() + ":" + utostr(LLLgetLineNo()) + ": ";
Reid Spenceref9b9a72007-02-05 20:47:22 +00003197 std::string errMsg = where + "error: " + std::string(ErrorMsg);
Duncan Sandsdc024672007-11-27 13:23:08 +00003198 if (yychar != YYEMPTY && yychar != 0) {
3199 errMsg += " while reading token: '";
3200 errMsg += std::string(LLLgetTokenStart(),
3201 LLLgetTokenStart()+LLLgetTokenLength()) + "'";
3202 }
Reid Spencer61c83e02006-08-18 08:43:06 +00003203 GenerateError(errMsg);
Chris Lattner58af2a12006-02-15 07:22:58 +00003204 return 0;
3205}